1b45d2f2cSJed Brown #include <petsc-private/dmimpl.h> /*I "petscdm.h" I*/ 2*0c312b8eSJed Brown #include <petscsf.h> 347c6ae99SBarry Smith 4732e2eb9SMatthew G Knepley PetscClassId DM_CLASSID; 567a56275SMatthew G Knepley PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal; 667a56275SMatthew G Knepley 747c6ae99SBarry Smith #undef __FUNCT__ 8ca266f36SBarry Smith #define __FUNCT__ "DMViewFromOptions" 9ca266f36SBarry Smith /* 10ca266f36SBarry Smith Processes command line options to determine if/how a dm 11ca266f36SBarry Smith is to be viewed. 12ca266f36SBarry Smith 13ca266f36SBarry Smith */ 14ca266f36SBarry Smith PetscErrorCode DMViewFromOptions(DM dm,const char optionname[]) 15ca266f36SBarry Smith { 16ca266f36SBarry Smith PetscErrorCode ierr; 17ca266f36SBarry Smith PetscBool flg; 18ca266f36SBarry Smith PetscViewer viewer; 19cffb1e40SBarry Smith PetscViewerFormat format; 20ca266f36SBarry Smith 21ca266f36SBarry Smith PetscFunctionBegin; 22ce94432eSBarry Smith ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)dm),((PetscObject)dm)->prefix,optionname,&viewer,&format,&flg);CHKERRQ(ierr); 23ca266f36SBarry Smith if (flg) { 24cffb1e40SBarry Smith ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr); 25ca266f36SBarry Smith ierr = DMView(dm,viewer);CHKERRQ(ierr); 26cffb1e40SBarry Smith ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 27cffb1e40SBarry Smith ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 28ca266f36SBarry Smith } 29ca266f36SBarry Smith PetscFunctionReturn(0); 30ca266f36SBarry Smith } 31ca266f36SBarry Smith 32ca266f36SBarry Smith 33ca266f36SBarry Smith #undef __FUNCT__ 34a4121054SBarry Smith #define __FUNCT__ "DMCreate" 35a4121054SBarry Smith /*@ 36de043629SMatthew G Knepley DMCreate - Creates an empty DM object. The type can then be set with DMSetType(). 37a4121054SBarry Smith 38a4121054SBarry Smith If you never call DMSetType() it will generate an 39a4121054SBarry Smith error when you try to use the vector. 40a4121054SBarry Smith 41a4121054SBarry Smith Collective on MPI_Comm 42a4121054SBarry Smith 43a4121054SBarry Smith Input Parameter: 44a4121054SBarry Smith . comm - The communicator for the DM object 45a4121054SBarry Smith 46a4121054SBarry Smith Output Parameter: 47a4121054SBarry Smith . dm - The DM object 48a4121054SBarry Smith 49a4121054SBarry Smith Level: beginner 50a4121054SBarry Smith 51a4121054SBarry Smith .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE 52a4121054SBarry Smith @*/ 537087cfbeSBarry Smith PetscErrorCode DMCreate(MPI_Comm comm,DM *dm) 54a4121054SBarry Smith { 55a4121054SBarry Smith DM v; 56a4121054SBarry Smith PetscErrorCode ierr; 57a4121054SBarry Smith 58a4121054SBarry Smith PetscFunctionBegin; 591411c6eeSJed Brown PetscValidPointer(dm,2); 600298fd71SBarry Smith *dm = NULL; 61519f805aSKarl Rupp #if !defined(PETSC_USE_DYNAMIC_LIBRARIES) 62607a6623SBarry Smith ierr = VecInitializePackage();CHKERRQ(ierr); 63607a6623SBarry Smith ierr = MatInitializePackage();CHKERRQ(ierr); 64607a6623SBarry Smith ierr = DMInitializePackage();CHKERRQ(ierr); 65a4121054SBarry Smith #endif 66a4121054SBarry Smith 6767c2884eSBarry Smith ierr = PetscHeaderCreate(v, _p_DM, struct _DMOps, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr); 68a4121054SBarry Smith ierr = PetscMemzero(v->ops, sizeof(struct _DMOps));CHKERRQ(ierr); 691411c6eeSJed Brown 70e7c4fc90SDmitry Karpeev 710298fd71SBarry Smith v->ltogmap = NULL; 720298fd71SBarry Smith v->ltogmapb = NULL; 731411c6eeSJed Brown v->bs = 1; 74171400e9SBarry Smith v->coloringtype = IS_COLORING_GLOBAL; 7588ed4aceSMatthew G Knepley ierr = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr); 7688ed4aceSMatthew G Knepley ierr = PetscSFCreate(comm, &v->defaultSF);CHKERRQ(ierr); 770298fd71SBarry Smith v->defaultSection = NULL; 780298fd71SBarry Smith v->defaultGlobalSection = NULL; 79435a35e8SMatthew G Knepley { 80435a35e8SMatthew G Knepley PetscInt i; 81435a35e8SMatthew G Knepley for (i = 0; i < 10; ++i) { 820298fd71SBarry Smith v->nullspaceConstructors[i] = NULL; 83435a35e8SMatthew G Knepley } 84435a35e8SMatthew G Knepley } 85af122d2aSMatthew G Knepley v->numFields = 0; 860298fd71SBarry Smith v->fields = NULL; 871411c6eeSJed Brown 881411c6eeSJed Brown *dm = v; 89a4121054SBarry Smith PetscFunctionReturn(0); 90a4121054SBarry Smith } 91a4121054SBarry Smith 92a4121054SBarry Smith #undef __FUNCT__ 939a42bb27SBarry Smith #define __FUNCT__ "DMSetVecType" 949a42bb27SBarry Smith /*@C 95564755cdSBarry Smith DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector() 969a42bb27SBarry Smith 97aa219208SBarry Smith Logically Collective on DMDA 989a42bb27SBarry Smith 999a42bb27SBarry Smith Input Parameter: 1009a42bb27SBarry Smith + da - initial distributed array 1018154be41SBarry Smith . ctype - the vector type, currently either VECSTANDARD or VECCUSP 1029a42bb27SBarry Smith 1039a42bb27SBarry Smith Options Database: 104dd85299cSBarry Smith . -dm_vec_type ctype 1059a42bb27SBarry Smith 1069a42bb27SBarry Smith Level: intermediate 1079a42bb27SBarry Smith 108aa219208SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMDestroy(), DMDA, DMDAInterpolationType, VecType 1099a42bb27SBarry Smith @*/ 11019fd82e9SBarry Smith PetscErrorCode DMSetVecType(DM da,VecType ctype) 1119a42bb27SBarry Smith { 1129a42bb27SBarry Smith PetscErrorCode ierr; 1139a42bb27SBarry Smith 1149a42bb27SBarry Smith PetscFunctionBegin; 1159a42bb27SBarry Smith PetscValidHeaderSpecific(da,DM_CLASSID,1); 1169a42bb27SBarry Smith ierr = PetscFree(da->vectype);CHKERRQ(ierr); 11719fd82e9SBarry Smith ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr); 1189a42bb27SBarry Smith PetscFunctionReturn(0); 1199a42bb27SBarry Smith } 1209a42bb27SBarry Smith 1219a42bb27SBarry Smith #undef __FUNCT__ 1225f1ad066SMatthew G Knepley #define __FUNCT__ "VecGetDM" 1235f1ad066SMatthew G Knepley /*@ 12434f98d34SBarry Smith VecGetDM - Gets the DM defining the data layout of the vector 1255f1ad066SMatthew G Knepley 1265f1ad066SMatthew G Knepley Not collective 1275f1ad066SMatthew G Knepley 1285f1ad066SMatthew G Knepley Input Parameter: 1295f1ad066SMatthew G Knepley . v - The Vec 1305f1ad066SMatthew G Knepley 1315f1ad066SMatthew G Knepley Output Parameter: 1325f1ad066SMatthew G Knepley . dm - The DM 1335f1ad066SMatthew G Knepley 1345f1ad066SMatthew G Knepley Level: intermediate 1355f1ad066SMatthew G Knepley 1365f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType() 1375f1ad066SMatthew G Knepley @*/ 1385f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm) 1395f1ad066SMatthew G Knepley { 1405f1ad066SMatthew G Knepley PetscErrorCode ierr; 1415f1ad066SMatthew G Knepley 1425f1ad066SMatthew G Knepley PetscFunctionBegin; 1435f1ad066SMatthew G Knepley PetscValidHeaderSpecific(v,VEC_CLASSID,1); 1445f1ad066SMatthew G Knepley PetscValidPointer(dm,2); 1455f1ad066SMatthew G Knepley ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr); 1465f1ad066SMatthew G Knepley PetscFunctionReturn(0); 1475f1ad066SMatthew G Knepley } 1485f1ad066SMatthew G Knepley 1495f1ad066SMatthew G Knepley #undef __FUNCT__ 1505f1ad066SMatthew G Knepley #define __FUNCT__ "VecSetDM" 1515f1ad066SMatthew G Knepley /*@ 1525f1ad066SMatthew G Knepley VecSetDM - Sets the DM defining the data layout of the vector 1535f1ad066SMatthew G Knepley 1545f1ad066SMatthew G Knepley Not collective 1555f1ad066SMatthew G Knepley 1565f1ad066SMatthew G Knepley Input Parameters: 1575f1ad066SMatthew G Knepley + v - The Vec 1585f1ad066SMatthew G Knepley - dm - The DM 1595f1ad066SMatthew G Knepley 1605f1ad066SMatthew G Knepley Level: intermediate 1615f1ad066SMatthew G Knepley 1625f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType() 1635f1ad066SMatthew G Knepley @*/ 1645f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm) 1655f1ad066SMatthew G Knepley { 1665f1ad066SMatthew G Knepley PetscErrorCode ierr; 1675f1ad066SMatthew G Knepley 1685f1ad066SMatthew G Knepley PetscFunctionBegin; 1695f1ad066SMatthew G Knepley PetscValidHeaderSpecific(v,VEC_CLASSID,1); 1705f1ad066SMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,2); 1715f1ad066SMatthew G Knepley ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr); 1725f1ad066SMatthew G Knepley PetscFunctionReturn(0); 1735f1ad066SMatthew G Knepley } 1745f1ad066SMatthew G Knepley 1755f1ad066SMatthew G Knepley #undef __FUNCT__ 176521d9a4cSLisandro Dalcin #define __FUNCT__ "DMSetMatType" 177521d9a4cSLisandro Dalcin /*@C 178521d9a4cSLisandro Dalcin DMSetMatType - Sets the type of matrix created with DMCreateMatrix() 179521d9a4cSLisandro Dalcin 180521d9a4cSLisandro Dalcin Logically Collective on DM 181521d9a4cSLisandro Dalcin 182521d9a4cSLisandro Dalcin Input Parameter: 183521d9a4cSLisandro Dalcin + dm - the DM context 184521d9a4cSLisandro Dalcin . ctype - the matrix type 185521d9a4cSLisandro Dalcin 186521d9a4cSLisandro Dalcin Options Database: 187521d9a4cSLisandro Dalcin . -dm_mat_type ctype 188521d9a4cSLisandro Dalcin 189521d9a4cSLisandro Dalcin Level: intermediate 190521d9a4cSLisandro Dalcin 191521d9a4cSLisandro Dalcin .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType 192521d9a4cSLisandro Dalcin @*/ 19319fd82e9SBarry Smith PetscErrorCode DMSetMatType(DM dm,MatType ctype) 194521d9a4cSLisandro Dalcin { 195521d9a4cSLisandro Dalcin PetscErrorCode ierr; 19688f0584fSBarry Smith 197521d9a4cSLisandro Dalcin PetscFunctionBegin; 198521d9a4cSLisandro Dalcin PetscValidHeaderSpecific(dm,DM_CLASSID,1); 199521d9a4cSLisandro Dalcin ierr = PetscFree(dm->mattype);CHKERRQ(ierr); 20019fd82e9SBarry Smith ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr); 201521d9a4cSLisandro Dalcin PetscFunctionReturn(0); 202521d9a4cSLisandro Dalcin } 203521d9a4cSLisandro Dalcin 204521d9a4cSLisandro Dalcin #undef __FUNCT__ 205c688c046SMatthew G Knepley #define __FUNCT__ "MatGetDM" 206c688c046SMatthew G Knepley /*@ 20734f98d34SBarry Smith MatGetDM - Gets the DM defining the data layout of the matrix 208c688c046SMatthew G Knepley 209c688c046SMatthew G Knepley Not collective 210c688c046SMatthew G Knepley 211c688c046SMatthew G Knepley Input Parameter: 212c688c046SMatthew G Knepley . A - The Mat 213c688c046SMatthew G Knepley 214c688c046SMatthew G Knepley Output Parameter: 215c688c046SMatthew G Knepley . dm - The DM 216c688c046SMatthew G Knepley 217c688c046SMatthew G Knepley Level: intermediate 218c688c046SMatthew G Knepley 219c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType() 220c688c046SMatthew G Knepley @*/ 221c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm) 222c688c046SMatthew G Knepley { 223c688c046SMatthew G Knepley PetscErrorCode ierr; 224c688c046SMatthew G Knepley 225c688c046SMatthew G Knepley PetscFunctionBegin; 226c688c046SMatthew G Knepley PetscValidHeaderSpecific(A,MAT_CLASSID,1); 227c688c046SMatthew G Knepley PetscValidPointer(dm,2); 228c688c046SMatthew G Knepley ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr); 229c688c046SMatthew G Knepley PetscFunctionReturn(0); 230c688c046SMatthew G Knepley } 231c688c046SMatthew G Knepley 232c688c046SMatthew G Knepley #undef __FUNCT__ 233c688c046SMatthew G Knepley #define __FUNCT__ "MatSetDM" 234c688c046SMatthew G Knepley /*@ 235c688c046SMatthew G Knepley MatSetDM - Sets the DM defining the data layout of the matrix 236c688c046SMatthew G Knepley 237c688c046SMatthew G Knepley Not collective 238c688c046SMatthew G Knepley 239c688c046SMatthew G Knepley Input Parameters: 240c688c046SMatthew G Knepley + A - The Mat 241c688c046SMatthew G Knepley - dm - The DM 242c688c046SMatthew G Knepley 243c688c046SMatthew G Knepley Level: intermediate 244c688c046SMatthew G Knepley 245c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType() 246c688c046SMatthew G Knepley @*/ 247c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm) 248c688c046SMatthew G Knepley { 249c688c046SMatthew G Knepley PetscErrorCode ierr; 250c688c046SMatthew G Knepley 251c688c046SMatthew G Knepley PetscFunctionBegin; 252c688c046SMatthew G Knepley PetscValidHeaderSpecific(A,MAT_CLASSID,1); 2538865f1eaSKarl Rupp if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2); 254c688c046SMatthew G Knepley ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr); 255c688c046SMatthew G Knepley PetscFunctionReturn(0); 256c688c046SMatthew G Knepley } 257c688c046SMatthew G Knepley 258c688c046SMatthew G Knepley #undef __FUNCT__ 2599a42bb27SBarry Smith #define __FUNCT__ "DMSetOptionsPrefix" 2609a42bb27SBarry Smith /*@C 2619a42bb27SBarry Smith DMSetOptionsPrefix - Sets the prefix used for searching for all 262aa219208SBarry Smith DMDA options in the database. 2639a42bb27SBarry Smith 264aa219208SBarry Smith Logically Collective on DMDA 2659a42bb27SBarry Smith 2669a42bb27SBarry Smith Input Parameter: 267aa219208SBarry Smith + da - the DMDA context 2689a42bb27SBarry Smith - prefix - the prefix to prepend to all option names 2699a42bb27SBarry Smith 2709a42bb27SBarry Smith Notes: 2719a42bb27SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 2729a42bb27SBarry Smith The first character of all runtime options is AUTOMATICALLY the hyphen. 2739a42bb27SBarry Smith 2749a42bb27SBarry Smith Level: advanced 2759a42bb27SBarry Smith 276aa219208SBarry Smith .keywords: DMDA, set, options, prefix, database 2779a42bb27SBarry Smith 2789a42bb27SBarry Smith .seealso: DMSetFromOptions() 2799a42bb27SBarry Smith @*/ 2807087cfbeSBarry Smith PetscErrorCode DMSetOptionsPrefix(DM dm,const char prefix[]) 2819a42bb27SBarry Smith { 2829a42bb27SBarry Smith PetscErrorCode ierr; 2839a42bb27SBarry Smith 2849a42bb27SBarry Smith PetscFunctionBegin; 2859a42bb27SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 2869a42bb27SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr); 2879a42bb27SBarry Smith PetscFunctionReturn(0); 2889a42bb27SBarry Smith } 2899a42bb27SBarry Smith 2909a42bb27SBarry Smith #undef __FUNCT__ 29147c6ae99SBarry Smith #define __FUNCT__ "DMDestroy" 29247c6ae99SBarry Smith /*@ 293aa219208SBarry Smith DMDestroy - Destroys a vector packer or DMDA. 29447c6ae99SBarry Smith 29547c6ae99SBarry Smith Collective on DM 29647c6ae99SBarry Smith 29747c6ae99SBarry Smith Input Parameter: 29847c6ae99SBarry Smith . dm - the DM object to destroy 29947c6ae99SBarry Smith 30047c6ae99SBarry Smith Level: developer 30147c6ae99SBarry Smith 302e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix() 30347c6ae99SBarry Smith 30447c6ae99SBarry Smith @*/ 305fcfd50ebSBarry Smith PetscErrorCode DMDestroy(DM *dm) 30647c6ae99SBarry Smith { 307af122d2aSMatthew G Knepley PetscInt i, cnt = 0, f; 308dfe15315SJed Brown DMNamedVecLink nlink,nnext; 30947c6ae99SBarry Smith PetscErrorCode ierr; 31047c6ae99SBarry Smith 31147c6ae99SBarry Smith PetscFunctionBegin; 3126bf464f9SBarry Smith if (!*dm) PetscFunctionReturn(0); 3136bf464f9SBarry Smith PetscValidHeaderSpecific((*dm),DM_CLASSID,1); 31487e657c6SBarry Smith 31587e657c6SBarry Smith /* count all the circular references of DM and its contained Vecs */ 316732e2eb9SMatthew G Knepley for (i=0; i<DM_MAX_WORK_VECTORS; i++) { 3178865f1eaSKarl Rupp if ((*dm)->localin[i]) cnt++; 3188865f1eaSKarl Rupp if ((*dm)->globalin[i]) cnt++; 319732e2eb9SMatthew G Knepley } 320dfe15315SJed Brown for (nlink=(*dm)->namedglobal; nlink; nlink=nlink->next) cnt++; 32171cd77b2SBarry Smith if ((*dm)->x) { 322c688c046SMatthew G Knepley DM obj; 323c688c046SMatthew G Knepley ierr = VecGetDM((*dm)->x, &obj);CHKERRQ(ierr); 324c688c046SMatthew G Knepley if (obj == *dm) cnt++; 32571cd77b2SBarry Smith } 3262348bcf4SPeter Brune for (nlink=(*dm)->namedlocal; nlink; nlink=nlink->next) cnt++; 3272348bcf4SPeter Brune if ((*dm)->x) { 3282348bcf4SPeter Brune DM obj; 3292348bcf4SPeter Brune ierr = VecGetDM((*dm)->x, &obj);CHKERRQ(ierr); 3302348bcf4SPeter Brune if (obj == *dm) cnt++; 3312348bcf4SPeter Brune } 332732e2eb9SMatthew G Knepley 3336bf464f9SBarry Smith if (--((PetscObject)(*dm))->refct - cnt > 0) {*dm = 0; PetscFunctionReturn(0);} 334732e2eb9SMatthew G Knepley /* 335732e2eb9SMatthew G Knepley Need this test because the dm references the vectors that 336732e2eb9SMatthew G Knepley reference the dm, so destroying the dm calls destroy on the 337732e2eb9SMatthew G Knepley vectors that cause another destroy on the dm 338732e2eb9SMatthew G Knepley */ 3396bf464f9SBarry Smith if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0); 3406bf464f9SBarry Smith ((PetscObject) (*dm))->refct = 0; 341732e2eb9SMatthew G Knepley for (i=0; i<DM_MAX_WORK_VECTORS; i++) { 3426bf464f9SBarry Smith if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()"); 3436bf464f9SBarry Smith ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr); 344732e2eb9SMatthew G Knepley } 345dfe15315SJed Brown for (nlink=(*dm)->namedglobal; nlink; nlink=nnext) { /* Destroy the named vectors */ 346dfe15315SJed Brown nnext = nlink->next; 347dfe15315SJed Brown if (nlink->status != DMVEC_STATUS_IN) SETERRQ1(((PetscObject)*dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"DM still has Vec named '%s' checked out",nlink->name); 348dfe15315SJed Brown ierr = PetscFree(nlink->name);CHKERRQ(ierr); 349dfe15315SJed Brown ierr = VecDestroy(&nlink->X);CHKERRQ(ierr); 350dfe15315SJed Brown ierr = PetscFree(nlink);CHKERRQ(ierr); 351dfe15315SJed Brown } 3520298fd71SBarry Smith (*dm)->namedglobal = NULL; 3531a266240SBarry Smith 3542348bcf4SPeter Brune for (nlink=(*dm)->namedlocal; nlink; nlink=nnext) { /* Destroy the named local vectors */ 3552348bcf4SPeter Brune nnext = nlink->next; 3562348bcf4SPeter Brune if (nlink->status != DMVEC_STATUS_IN) SETERRQ1(((PetscObject)*dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"DM still has Vec named '%s' checked out",nlink->name); 3572348bcf4SPeter Brune ierr = PetscFree(nlink->name);CHKERRQ(ierr); 3582348bcf4SPeter Brune ierr = VecDestroy(&nlink->X);CHKERRQ(ierr); 3592348bcf4SPeter Brune ierr = PetscFree(nlink);CHKERRQ(ierr); 3602348bcf4SPeter Brune } 3610298fd71SBarry Smith (*dm)->namedlocal = NULL; 3622348bcf4SPeter Brune 363b17ce1afSJed Brown /* Destroy the list of hooks */ 364c833c3b5SJed Brown { 365c833c3b5SJed Brown DMCoarsenHookLink link,next; 366b17ce1afSJed Brown for (link=(*dm)->coarsenhook; link; link=next) { 367b17ce1afSJed Brown next = link->next; 368b17ce1afSJed Brown ierr = PetscFree(link);CHKERRQ(ierr); 369b17ce1afSJed Brown } 3700298fd71SBarry Smith (*dm)->coarsenhook = NULL; 371c833c3b5SJed Brown } 372c833c3b5SJed Brown { 373c833c3b5SJed Brown DMRefineHookLink link,next; 374c833c3b5SJed Brown for (link=(*dm)->refinehook; link; link=next) { 375c833c3b5SJed Brown next = link->next; 376c833c3b5SJed Brown ierr = PetscFree(link);CHKERRQ(ierr); 377c833c3b5SJed Brown } 3780298fd71SBarry Smith (*dm)->refinehook = NULL; 379c833c3b5SJed Brown } 380be081cd6SPeter Brune { 381be081cd6SPeter Brune DMSubDomainHookLink link,next; 382be081cd6SPeter Brune for (link=(*dm)->subdomainhook; link; link=next) { 383be081cd6SPeter Brune next = link->next; 384be081cd6SPeter Brune ierr = PetscFree(link);CHKERRQ(ierr); 385be081cd6SPeter Brune } 3860298fd71SBarry Smith (*dm)->subdomainhook = NULL; 387be081cd6SPeter Brune } 388baf369e7SPeter Brune { 389baf369e7SPeter Brune DMGlobalToLocalHookLink link,next; 390baf369e7SPeter Brune for (link=(*dm)->gtolhook; link; link=next) { 391baf369e7SPeter Brune next = link->next; 392baf369e7SPeter Brune ierr = PetscFree(link);CHKERRQ(ierr); 393baf369e7SPeter Brune } 3940298fd71SBarry Smith (*dm)->gtolhook = NULL; 395baf369e7SPeter Brune } 396aa1993deSMatthew G Knepley /* Destroy the work arrays */ 397aa1993deSMatthew G Knepley { 398aa1993deSMatthew G Knepley DMWorkLink link,next; 399aa1993deSMatthew G Knepley if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out"); 400aa1993deSMatthew G Knepley for (link=(*dm)->workin; link; link=next) { 401aa1993deSMatthew G Knepley next = link->next; 402aa1993deSMatthew G Knepley ierr = PetscFree(link->mem);CHKERRQ(ierr); 403aa1993deSMatthew G Knepley ierr = PetscFree(link);CHKERRQ(ierr); 404aa1993deSMatthew G Knepley } 4050298fd71SBarry Smith (*dm)->workin = NULL; 406aa1993deSMatthew G Knepley } 407b17ce1afSJed Brown 40852536dc3SBarry Smith ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr); 40952536dc3SBarry Smith ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr); 41052536dc3SBarry Smith ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr); 41152536dc3SBarry Smith 4121a266240SBarry Smith if ((*dm)->ctx && (*dm)->ctxdestroy) { 4131a266240SBarry Smith ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr); 4141a266240SBarry Smith } 41587e657c6SBarry Smith ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr); 41671cd77b2SBarry Smith ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr); 4174dcab191SBarry Smith ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr); 4186bf464f9SBarry Smith ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr); 4196bf464f9SBarry Smith ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmapb);CHKERRQ(ierr); 4206bf464f9SBarry Smith ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr); 421073dac72SJed Brown ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr); 42288ed4aceSMatthew G Knepley 42388ed4aceSMatthew G Knepley ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr); 42488ed4aceSMatthew G Knepley ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr); 4258b1ab98fSJed Brown ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr); 42688ed4aceSMatthew G Knepley ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr); 42788ed4aceSMatthew G Knepley ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr); 428af122d2aSMatthew G Knepley 4296636e97aSMatthew G Knepley ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr); 4306636e97aSMatthew G Knepley ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr); 4316636e97aSMatthew G Knepley ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr); 4326636e97aSMatthew G Knepley 433af122d2aSMatthew G Knepley for (f = 0; f < (*dm)->numFields; ++f) { 434af122d2aSMatthew G Knepley ierr = PetscObjectDestroy(&(*dm)->fields[f]);CHKERRQ(ierr); 435af122d2aSMatthew G Knepley } 436af122d2aSMatthew G Knepley ierr = PetscFree((*dm)->fields);CHKERRQ(ierr); 437732e2eb9SMatthew G Knepley /* if memory was published with AMS then destroy it */ 438f05ece33SBarry Smith ierr = PetscObjectAMSViewOff((PetscObject)*dm);CHKERRQ(ierr); 439732e2eb9SMatthew G Knepley 4406bf464f9SBarry Smith ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr); 441435a35e8SMatthew G Knepley /* We do not destroy (*dm)->data here so that we can reference count backend objects */ 442732e2eb9SMatthew G Knepley ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr); 44347c6ae99SBarry Smith PetscFunctionReturn(0); 44447c6ae99SBarry Smith } 44547c6ae99SBarry Smith 44647c6ae99SBarry Smith #undef __FUNCT__ 447d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp" 448d7bf68aeSBarry Smith /*@ 449d7bf68aeSBarry Smith DMSetUp - sets up the data structures inside a DM object 450d7bf68aeSBarry Smith 451d7bf68aeSBarry Smith Collective on DM 452d7bf68aeSBarry Smith 453d7bf68aeSBarry Smith Input Parameter: 454d7bf68aeSBarry Smith . dm - the DM object to setup 455d7bf68aeSBarry Smith 456d7bf68aeSBarry Smith Level: developer 457d7bf68aeSBarry Smith 458e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix() 459d7bf68aeSBarry Smith 460d7bf68aeSBarry Smith @*/ 4617087cfbeSBarry Smith PetscErrorCode DMSetUp(DM dm) 462d7bf68aeSBarry Smith { 463d7bf68aeSBarry Smith PetscErrorCode ierr; 464d7bf68aeSBarry Smith 465d7bf68aeSBarry Smith PetscFunctionBegin; 466171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 4678387afaaSJed Brown if (dm->setupcalled) PetscFunctionReturn(0); 468d7bf68aeSBarry Smith if (dm->ops->setup) { 469d7bf68aeSBarry Smith ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr); 470d7bf68aeSBarry Smith } 4718387afaaSJed Brown dm->setupcalled = PETSC_TRUE; 472d7bf68aeSBarry Smith PetscFunctionReturn(0); 473d7bf68aeSBarry Smith } 474d7bf68aeSBarry Smith 475d7bf68aeSBarry Smith #undef __FUNCT__ 476d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions" 477d7bf68aeSBarry Smith /*@ 478d7bf68aeSBarry Smith DMSetFromOptions - sets parameters in a DM from the options database 479d7bf68aeSBarry Smith 480d7bf68aeSBarry Smith Collective on DM 481d7bf68aeSBarry Smith 482d7bf68aeSBarry Smith Input Parameter: 483d7bf68aeSBarry Smith . dm - the DM object to set options for 484d7bf68aeSBarry Smith 485732e2eb9SMatthew G Knepley Options Database: 486dd85299cSBarry Smith + -dm_preallocate_only: Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros 487dd85299cSBarry Smith . -dm_vec_type <type> type of vector to create inside DM 488171400e9SBarry Smith . -dm_mat_type <type> type of matrix to create inside DM 489171400e9SBarry Smith - -dm_coloring_type <global or ghosted> 490732e2eb9SMatthew G Knepley 491d7bf68aeSBarry Smith Level: developer 492d7bf68aeSBarry Smith 493e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix() 494d7bf68aeSBarry Smith 495d7bf68aeSBarry Smith @*/ 4967087cfbeSBarry Smith PetscErrorCode DMSetFromOptions(DM dm) 497d7bf68aeSBarry Smith { 498f55f929bSMatthew G Knepley char typeName[256] = MATAIJ; 499ca266f36SBarry Smith PetscBool flg; 500d7bf68aeSBarry Smith PetscErrorCode ierr; 501d7bf68aeSBarry Smith 502d7bf68aeSBarry Smith PetscFunctionBegin; 503171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 5043194b578SJed Brown ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr); 5050298fd71SBarry Smith ierr = PetscOptionsBool("-dm_preallocate_only","only preallocate matrix, but do not set column indices","DMSetMatrixPreallocateOnly",dm->prealloc_only,&dm->prealloc_only,NULL);CHKERRQ(ierr); 506f9ba7244SBarry Smith ierr = PetscOptionsList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr); 507f9ba7244SBarry Smith if (flg) { 508f9ba7244SBarry Smith ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr); 509f9ba7244SBarry Smith } 5108caf3d72SBarry Smith ierr = PetscOptionsList("-dm_mat_type","Matrix type used for created matrices","DMSetMatType",MatList,dm->mattype ? dm->mattype : typeName,typeName,sizeof(typeName),&flg);CHKERRQ(ierr); 511073dac72SJed Brown if (flg) { 512521d9a4cSLisandro Dalcin ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr); 513073dac72SJed Brown } 5140298fd71SBarry Smith ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","ISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr); 515f9ba7244SBarry Smith if (dm->ops->setfromoptions) { 516f9ba7244SBarry Smith ierr = (*dm->ops->setfromoptions)(dm);CHKERRQ(ierr); 517f9ba7244SBarry Smith } 518f9ba7244SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 519f9ba7244SBarry Smith ierr = PetscObjectProcessOptionsHandlers((PetscObject) dm);CHKERRQ(ierr); 52082fcb398SMatthew G Knepley ierr = PetscOptionsEnd();CHKERRQ(ierr); 521ca266f36SBarry Smith ierr = DMViewFromOptions(dm,"-dm_view");CHKERRQ(ierr); 522d7bf68aeSBarry Smith PetscFunctionReturn(0); 523d7bf68aeSBarry Smith } 524d7bf68aeSBarry Smith 525d7bf68aeSBarry Smith #undef __FUNCT__ 52647c6ae99SBarry Smith #define __FUNCT__ "DMView" 527fc9bc008SSatish Balay /*@C 528aa219208SBarry Smith DMView - Views a vector packer or DMDA. 52947c6ae99SBarry Smith 53047c6ae99SBarry Smith Collective on DM 53147c6ae99SBarry Smith 53247c6ae99SBarry Smith Input Parameter: 53347c6ae99SBarry Smith + dm - the DM object to view 53447c6ae99SBarry Smith - v - the viewer 53547c6ae99SBarry Smith 53647c6ae99SBarry Smith Level: developer 53747c6ae99SBarry Smith 538e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix() 53947c6ae99SBarry Smith 54047c6ae99SBarry Smith @*/ 5417087cfbeSBarry Smith PetscErrorCode DMView(DM dm,PetscViewer v) 54247c6ae99SBarry Smith { 54347c6ae99SBarry Smith PetscErrorCode ierr; 54432c0f0efSBarry Smith PetscBool isbinary; 54547c6ae99SBarry Smith 54647c6ae99SBarry Smith PetscFunctionBegin; 547171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 5483014e516SBarry Smith if (!v) { 549ce94432eSBarry Smith ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr); 5503014e516SBarry Smith } 55132c0f0efSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 55232c0f0efSBarry Smith if (isbinary) { 55355849f57SBarry Smith PetscInt classid = DM_FILE_CLASSID; 55432c0f0efSBarry Smith char type[256]; 55532c0f0efSBarry Smith 55632c0f0efSBarry Smith ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 55732c0f0efSBarry Smith ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr); 55832c0f0efSBarry Smith ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr); 55932c0f0efSBarry Smith } 5600c010503SBarry Smith if (dm->ops->view) { 5610c010503SBarry Smith ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr); 56247c6ae99SBarry Smith } 56347c6ae99SBarry Smith PetscFunctionReturn(0); 56447c6ae99SBarry Smith } 56547c6ae99SBarry Smith 56647c6ae99SBarry Smith #undef __FUNCT__ 56747c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector" 56847c6ae99SBarry Smith /*@ 569aa219208SBarry Smith DMCreateGlobalVector - Creates a global vector from a DMDA or DMComposite object 57047c6ae99SBarry Smith 57147c6ae99SBarry Smith Collective on DM 57247c6ae99SBarry Smith 57347c6ae99SBarry Smith Input Parameter: 57447c6ae99SBarry Smith . dm - the DM object 57547c6ae99SBarry Smith 57647c6ae99SBarry Smith Output Parameter: 57747c6ae99SBarry Smith . vec - the global vector 57847c6ae99SBarry Smith 579073dac72SJed Brown Level: beginner 58047c6ae99SBarry Smith 581e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix() 58247c6ae99SBarry Smith 58347c6ae99SBarry Smith @*/ 5847087cfbeSBarry Smith PetscErrorCode DMCreateGlobalVector(DM dm,Vec *vec) 58547c6ae99SBarry Smith { 58647c6ae99SBarry Smith PetscErrorCode ierr; 58747c6ae99SBarry Smith 58847c6ae99SBarry Smith PetscFunctionBegin; 589171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 59047c6ae99SBarry Smith ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr); 59147c6ae99SBarry Smith PetscFunctionReturn(0); 59247c6ae99SBarry Smith } 59347c6ae99SBarry Smith 59447c6ae99SBarry Smith #undef __FUNCT__ 59547c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector" 59647c6ae99SBarry Smith /*@ 597aa219208SBarry Smith DMCreateLocalVector - Creates a local vector from a DMDA or DMComposite object 59847c6ae99SBarry Smith 59947c6ae99SBarry Smith Not Collective 60047c6ae99SBarry Smith 60147c6ae99SBarry Smith Input Parameter: 60247c6ae99SBarry Smith . dm - the DM object 60347c6ae99SBarry Smith 60447c6ae99SBarry Smith Output Parameter: 60547c6ae99SBarry Smith . vec - the local vector 60647c6ae99SBarry Smith 607073dac72SJed Brown Level: beginner 60847c6ae99SBarry Smith 609e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix() 61047c6ae99SBarry Smith 61147c6ae99SBarry Smith @*/ 6127087cfbeSBarry Smith PetscErrorCode DMCreateLocalVector(DM dm,Vec *vec) 61347c6ae99SBarry Smith { 61447c6ae99SBarry Smith PetscErrorCode ierr; 61547c6ae99SBarry Smith 61647c6ae99SBarry Smith PetscFunctionBegin; 617171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 61847c6ae99SBarry Smith ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr); 61947c6ae99SBarry Smith PetscFunctionReturn(0); 62047c6ae99SBarry Smith } 62147c6ae99SBarry Smith 62247c6ae99SBarry Smith #undef __FUNCT__ 6231411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping" 6241411c6eeSJed Brown /*@ 6251411c6eeSJed Brown DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM. 6261411c6eeSJed Brown 6271411c6eeSJed Brown Collective on DM 6281411c6eeSJed Brown 6291411c6eeSJed Brown Input Parameter: 6301411c6eeSJed Brown . dm - the DM that provides the mapping 6311411c6eeSJed Brown 6321411c6eeSJed Brown Output Parameter: 6331411c6eeSJed Brown . ltog - the mapping 6341411c6eeSJed Brown 6351411c6eeSJed Brown Level: intermediate 6361411c6eeSJed Brown 6371411c6eeSJed Brown Notes: 6381411c6eeSJed Brown This mapping can then be used by VecSetLocalToGlobalMapping() or 6391411c6eeSJed Brown MatSetLocalToGlobalMapping(). 6401411c6eeSJed Brown 6411411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMappingBlock() 6421411c6eeSJed Brown @*/ 6437087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog) 6441411c6eeSJed Brown { 6451411c6eeSJed Brown PetscErrorCode ierr; 6461411c6eeSJed Brown 6471411c6eeSJed Brown PetscFunctionBegin; 6481411c6eeSJed Brown PetscValidHeaderSpecific(dm,DM_CLASSID,1); 6491411c6eeSJed Brown PetscValidPointer(ltog,2); 6501411c6eeSJed Brown if (!dm->ltogmap) { 65137d0c07bSMatthew G Knepley PetscSection section, sectionGlobal; 65237d0c07bSMatthew G Knepley 65337d0c07bSMatthew G Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 65437d0c07bSMatthew G Knepley if (section) { 65537d0c07bSMatthew G Knepley PetscInt *ltog; 65637d0c07bSMatthew G Knepley PetscInt pStart, pEnd, size, p, l; 65737d0c07bSMatthew G Knepley 65837d0c07bSMatthew G Knepley ierr = DMGetDefaultGlobalSection(dm, §ionGlobal);CHKERRQ(ierr); 65937d0c07bSMatthew G Knepley ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr); 66037d0c07bSMatthew G Knepley ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr); 66137d0c07bSMatthew G Knepley ierr = PetscMalloc(size * sizeof(PetscInt), <og);CHKERRQ(ierr); /* We want the local+overlap size */ 66237d0c07bSMatthew G Knepley for (p = pStart, l = 0; p < pEnd; ++p) { 66337d0c07bSMatthew G Knepley PetscInt dof, off, c; 66437d0c07bSMatthew G Knepley 66537d0c07bSMatthew G Knepley /* Should probably use constrained dofs */ 66637d0c07bSMatthew G Knepley ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr); 66737d0c07bSMatthew G Knepley ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr); 66837d0c07bSMatthew G Knepley for (c = 0; c < dof; ++c, ++l) { 66937d0c07bSMatthew G Knepley ltog[l] = off+c; 67037d0c07bSMatthew G Knepley } 67137d0c07bSMatthew G Knepley } 67237d0c07bSMatthew G Knepley ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_SELF, size, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr); 67337d0c07bSMatthew G Knepley ierr = PetscLogObjectParent(dm, dm->ltogmap);CHKERRQ(ierr); 67437d0c07bSMatthew G Knepley } else { 675184d77edSJed Brown if (!dm->ops->getlocaltoglobalmapping) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping"); 676184d77edSJed Brown ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr); 6771411c6eeSJed Brown } 67837d0c07bSMatthew G Knepley } 6791411c6eeSJed Brown *ltog = dm->ltogmap; 6801411c6eeSJed Brown PetscFunctionReturn(0); 6811411c6eeSJed Brown } 6821411c6eeSJed Brown 6831411c6eeSJed Brown #undef __FUNCT__ 6841411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMappingBlock" 6851411c6eeSJed Brown /*@ 6861411c6eeSJed Brown DMGetLocalToGlobalMappingBlock - Accesses the blocked local-to-global mapping in a DM. 6871411c6eeSJed Brown 6881411c6eeSJed Brown Collective on DM 6891411c6eeSJed Brown 6901411c6eeSJed Brown Input Parameter: 6911411c6eeSJed Brown . da - the distributed array that provides the mapping 6921411c6eeSJed Brown 6931411c6eeSJed Brown Output Parameter: 6941411c6eeSJed Brown . ltog - the block mapping 6951411c6eeSJed Brown 6961411c6eeSJed Brown Level: intermediate 6971411c6eeSJed Brown 6981411c6eeSJed Brown Notes: 6991411c6eeSJed Brown This mapping can then be used by VecSetLocalToGlobalMappingBlock() or 7001411c6eeSJed Brown MatSetLocalToGlobalMappingBlock(). 7011411c6eeSJed Brown 7021411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMapping(), DMGetBlockSize(), VecSetBlockSize(), MatSetBlockSize() 7031411c6eeSJed Brown @*/ 7047087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMappingBlock(DM dm,ISLocalToGlobalMapping *ltog) 7051411c6eeSJed Brown { 7061411c6eeSJed Brown PetscErrorCode ierr; 7071411c6eeSJed Brown 7081411c6eeSJed Brown PetscFunctionBegin; 7091411c6eeSJed Brown PetscValidHeaderSpecific(dm,DM_CLASSID,1); 7101411c6eeSJed Brown PetscValidPointer(ltog,2); 7111411c6eeSJed Brown if (!dm->ltogmapb) { 7121411c6eeSJed Brown PetscInt bs; 7131411c6eeSJed Brown ierr = DMGetBlockSize(dm,&bs);CHKERRQ(ierr); 7141411c6eeSJed Brown if (bs > 1) { 715184d77edSJed Brown if (!dm->ops->getlocaltoglobalmappingblock) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMappingBlock"); 716184d77edSJed Brown ierr = (*dm->ops->getlocaltoglobalmappingblock)(dm);CHKERRQ(ierr); 7171411c6eeSJed Brown } else { 7181411c6eeSJed Brown ierr = DMGetLocalToGlobalMapping(dm,&dm->ltogmapb);CHKERRQ(ierr); 7191411c6eeSJed Brown ierr = PetscObjectReference((PetscObject)dm->ltogmapb);CHKERRQ(ierr); 7201411c6eeSJed Brown } 7211411c6eeSJed Brown } 7221411c6eeSJed Brown *ltog = dm->ltogmapb; 7231411c6eeSJed Brown PetscFunctionReturn(0); 7241411c6eeSJed Brown } 7251411c6eeSJed Brown 7261411c6eeSJed Brown #undef __FUNCT__ 7271411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize" 7281411c6eeSJed Brown /*@ 7291411c6eeSJed Brown DMGetBlockSize - Gets the inherent block size associated with a DM 7301411c6eeSJed Brown 7311411c6eeSJed Brown Not Collective 7321411c6eeSJed Brown 7331411c6eeSJed Brown Input Parameter: 7341411c6eeSJed Brown . dm - the DM with block structure 7351411c6eeSJed Brown 7361411c6eeSJed Brown Output Parameter: 7371411c6eeSJed Brown . bs - the block size, 1 implies no exploitable block structure 7381411c6eeSJed Brown 7391411c6eeSJed Brown Level: intermediate 7401411c6eeSJed Brown 7411411c6eeSJed Brown .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMappingBlock() 7421411c6eeSJed Brown @*/ 7437087cfbeSBarry Smith PetscErrorCode DMGetBlockSize(DM dm,PetscInt *bs) 7441411c6eeSJed Brown { 7451411c6eeSJed Brown PetscFunctionBegin; 7461411c6eeSJed Brown PetscValidHeaderSpecific(dm,DM_CLASSID,1); 7471411c6eeSJed Brown PetscValidPointer(bs,2); 7481411c6eeSJed Brown if (dm->bs < 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"DM does not have enough information to provide a block size yet"); 7491411c6eeSJed Brown *bs = dm->bs; 7501411c6eeSJed Brown PetscFunctionReturn(0); 7511411c6eeSJed Brown } 7521411c6eeSJed Brown 7531411c6eeSJed Brown #undef __FUNCT__ 754e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation" 75547c6ae99SBarry Smith /*@ 756e727c939SJed Brown DMCreateInterpolation - Gets interpolation matrix between two DMDA or DMComposite objects 75747c6ae99SBarry Smith 75847c6ae99SBarry Smith Collective on DM 75947c6ae99SBarry Smith 76047c6ae99SBarry Smith Input Parameter: 76147c6ae99SBarry Smith + dm1 - the DM object 76247c6ae99SBarry Smith - dm2 - the second, finer DM object 76347c6ae99SBarry Smith 76447c6ae99SBarry Smith Output Parameter: 76547c6ae99SBarry Smith + mat - the interpolation 76647c6ae99SBarry Smith - vec - the scaling (optional) 76747c6ae99SBarry Smith 76847c6ae99SBarry Smith Level: developer 76947c6ae99SBarry Smith 77085afcc9aSBarry Smith Notes: For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by 77185afcc9aSBarry Smith DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation. 772d52bd9f3SBarry Smith 7731f588964SMatthew G Knepley For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors 774d52bd9f3SBarry Smith EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic. 77585afcc9aSBarry Smith 77685afcc9aSBarry Smith 777e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen() 77847c6ae99SBarry Smith 77947c6ae99SBarry Smith @*/ 780e727c939SJed Brown PetscErrorCode DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec) 78147c6ae99SBarry Smith { 78247c6ae99SBarry Smith PetscErrorCode ierr; 78347c6ae99SBarry Smith 78447c6ae99SBarry Smith PetscFunctionBegin; 785171400e9SBarry Smith PetscValidHeaderSpecific(dm1,DM_CLASSID,1); 786171400e9SBarry Smith PetscValidHeaderSpecific(dm2,DM_CLASSID,2); 78725296bd5SBarry Smith ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr); 78847c6ae99SBarry Smith PetscFunctionReturn(0); 78947c6ae99SBarry Smith } 79047c6ae99SBarry Smith 79147c6ae99SBarry Smith #undef __FUNCT__ 792e727c939SJed Brown #define __FUNCT__ "DMCreateInjection" 79347c6ae99SBarry Smith /*@ 794e727c939SJed Brown DMCreateInjection - Gets injection matrix between two DMDA or DMComposite objects 79547c6ae99SBarry Smith 79647c6ae99SBarry Smith Collective on DM 79747c6ae99SBarry Smith 79847c6ae99SBarry Smith Input Parameter: 79947c6ae99SBarry Smith + dm1 - the DM object 80047c6ae99SBarry Smith - dm2 - the second, finer DM object 80147c6ae99SBarry Smith 80247c6ae99SBarry Smith Output Parameter: 80347c6ae99SBarry Smith . ctx - the injection 80447c6ae99SBarry Smith 80547c6ae99SBarry Smith Level: developer 80647c6ae99SBarry Smith 80785afcc9aSBarry Smith Notes: For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by 80885afcc9aSBarry Smith DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection. 80985afcc9aSBarry Smith 810e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation() 81147c6ae99SBarry Smith 81247c6ae99SBarry Smith @*/ 813e727c939SJed Brown PetscErrorCode DMCreateInjection(DM dm1,DM dm2,VecScatter *ctx) 81447c6ae99SBarry Smith { 81547c6ae99SBarry Smith PetscErrorCode ierr; 81647c6ae99SBarry Smith 81747c6ae99SBarry Smith PetscFunctionBegin; 818171400e9SBarry Smith PetscValidHeaderSpecific(dm1,DM_CLASSID,1); 819171400e9SBarry Smith PetscValidHeaderSpecific(dm2,DM_CLASSID,2); 82047c6ae99SBarry Smith ierr = (*dm1->ops->getinjection)(dm1,dm2,ctx);CHKERRQ(ierr); 82147c6ae99SBarry Smith PetscFunctionReturn(0); 82247c6ae99SBarry Smith } 82347c6ae99SBarry Smith 82447c6ae99SBarry Smith #undef __FUNCT__ 825e727c939SJed Brown #define __FUNCT__ "DMCreateColoring" 826d1e2c406SBarry Smith /*@C 827e727c939SJed Brown DMCreateColoring - Gets coloring for a DMDA or DMComposite 82847c6ae99SBarry Smith 82947c6ae99SBarry Smith Collective on DM 83047c6ae99SBarry Smith 83147c6ae99SBarry Smith Input Parameter: 83247c6ae99SBarry Smith + dm - the DM object 83347c6ae99SBarry Smith . ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL 83447c6ae99SBarry Smith - matype - either MATAIJ or MATBAIJ 83547c6ae99SBarry Smith 83647c6ae99SBarry Smith Output Parameter: 83747c6ae99SBarry Smith . coloring - the coloring 83847c6ae99SBarry Smith 83947c6ae99SBarry Smith Level: developer 84047c6ae99SBarry Smith 841e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix() 84247c6ae99SBarry Smith 843aab9d709SJed Brown @*/ 84419fd82e9SBarry Smith PetscErrorCode DMCreateColoring(DM dm,ISColoringType ctype,MatType mtype,ISColoring *coloring) 84547c6ae99SBarry Smith { 84647c6ae99SBarry Smith PetscErrorCode ierr; 84747c6ae99SBarry Smith 84847c6ae99SBarry Smith PetscFunctionBegin; 849171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 850ce94432eSBarry Smith if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet"); 85147c6ae99SBarry Smith ierr = (*dm->ops->getcoloring)(dm,ctype,mtype,coloring);CHKERRQ(ierr); 85247c6ae99SBarry Smith PetscFunctionReturn(0); 85347c6ae99SBarry Smith } 85447c6ae99SBarry Smith 85547c6ae99SBarry Smith #undef __FUNCT__ 856950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix" 85747c6ae99SBarry Smith /*@C 858950540a4SJed Brown DMCreateMatrix - Gets empty Jacobian for a DMDA or DMComposite 85947c6ae99SBarry Smith 86047c6ae99SBarry Smith Collective on DM 86147c6ae99SBarry Smith 86247c6ae99SBarry Smith Input Parameter: 86347c6ae99SBarry Smith + dm - the DM object 86447c6ae99SBarry Smith - mtype - Supported types are MATSEQAIJ, MATMPIAIJ, MATSEQBAIJ, MATMPIBAIJ, or 86594013140SBarry Smith any type which inherits from one of these (such as MATAIJ) 86647c6ae99SBarry Smith 86747c6ae99SBarry Smith Output Parameter: 86847c6ae99SBarry Smith . mat - the empty Jacobian 86947c6ae99SBarry Smith 870073dac72SJed Brown Level: beginner 87147c6ae99SBarry Smith 87294013140SBarry Smith Notes: This properly preallocates the number of nonzeros in the sparse matrix so you 87394013140SBarry Smith do not need to do it yourself. 87494013140SBarry Smith 87594013140SBarry Smith By default it also sets the nonzero structure and puts in the zero entries. To prevent setting 876aa219208SBarry Smith the nonzero pattern call DMDASetMatPreallocateOnly() 87794013140SBarry Smith 87894013140SBarry Smith For structured grid problems, when you call MatView() on this matrix it is displayed using the global natural ordering, NOT in the ordering used 87994013140SBarry Smith internally by PETSc. 88094013140SBarry Smith 88194013140SBarry Smith For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires 882aa219208SBarry Smith the indices for the global numbering for DMDAs which is complicated. 88394013140SBarry Smith 884e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation() 88547c6ae99SBarry Smith 886aab9d709SJed Brown @*/ 88719fd82e9SBarry Smith PetscErrorCode DMCreateMatrix(DM dm,MatType mtype,Mat *mat) 88847c6ae99SBarry Smith { 88947c6ae99SBarry Smith PetscErrorCode ierr; 89047c6ae99SBarry Smith 89147c6ae99SBarry Smith PetscFunctionBegin; 892171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 893519f805aSKarl Rupp #if !defined(PETSC_USE_DYNAMIC_LIBRARIES) 894607a6623SBarry Smith ierr = MatInitializePackage();CHKERRQ(ierr); 895235683edSBarry Smith #endif 896c7b7c8a4SJed Brown PetscValidHeaderSpecific(dm,DM_CLASSID,1); 897c7b7c8a4SJed Brown PetscValidPointer(mat,3); 898073dac72SJed Brown if (dm->mattype) { 89925296bd5SBarry Smith ierr = (*dm->ops->creatematrix)(dm,dm->mattype,mat);CHKERRQ(ierr); 900073dac72SJed Brown } else { 90125296bd5SBarry Smith ierr = (*dm->ops->creatematrix)(dm,mtype,mat);CHKERRQ(ierr); 902c7b7c8a4SJed Brown } 90347c6ae99SBarry Smith PetscFunctionReturn(0); 90447c6ae99SBarry Smith } 90547c6ae99SBarry Smith 90647c6ae99SBarry Smith #undef __FUNCT__ 907732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly" 908732e2eb9SMatthew G Knepley /*@ 909950540a4SJed Brown DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly 910732e2eb9SMatthew G Knepley preallocated but the nonzero structure and zero values will not be set. 911732e2eb9SMatthew G Knepley 912732e2eb9SMatthew G Knepley Logically Collective on DMDA 913732e2eb9SMatthew G Knepley 914732e2eb9SMatthew G Knepley Input Parameter: 915732e2eb9SMatthew G Knepley + dm - the DM 916732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation 917732e2eb9SMatthew G Knepley 918732e2eb9SMatthew G Knepley Level: developer 919950540a4SJed Brown .seealso DMCreateMatrix() 920732e2eb9SMatthew G Knepley @*/ 921732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only) 922732e2eb9SMatthew G Knepley { 923732e2eb9SMatthew G Knepley PetscFunctionBegin; 924732e2eb9SMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 925732e2eb9SMatthew G Knepley dm->prealloc_only = only; 926732e2eb9SMatthew G Knepley PetscFunctionReturn(0); 927732e2eb9SMatthew G Knepley } 928732e2eb9SMatthew G Knepley 929732e2eb9SMatthew G Knepley #undef __FUNCT__ 930a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray" 931a89ea682SMatthew G Knepley /*@C 932aa1993deSMatthew G Knepley DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray() 933a89ea682SMatthew G Knepley 934a89ea682SMatthew G Knepley Not Collective 935a89ea682SMatthew G Knepley 936a89ea682SMatthew G Knepley Input Parameters: 937a89ea682SMatthew G Knepley + dm - the DM object 938aa1993deSMatthew G Knepley . count - The minium size 939aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT) 940a89ea682SMatthew G Knepley 941a89ea682SMatthew G Knepley Output Parameter: 942a89ea682SMatthew G Knepley . array - the work array 943a89ea682SMatthew G Knepley 944a89ea682SMatthew G Knepley Level: developer 945a89ea682SMatthew G Knepley 946a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate() 947a89ea682SMatthew G Knepley @*/ 948aa1993deSMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem) 949a89ea682SMatthew G Knepley { 950a89ea682SMatthew G Knepley PetscErrorCode ierr; 951aa1993deSMatthew G Knepley DMWorkLink link; 952aa1993deSMatthew G Knepley size_t size; 953a89ea682SMatthew G Knepley 954a89ea682SMatthew G Knepley PetscFunctionBegin; 955a89ea682SMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 956aa1993deSMatthew G Knepley PetscValidPointer(mem,4); 957aa1993deSMatthew G Knepley if (dm->workin) { 958aa1993deSMatthew G Knepley link = dm->workin; 959aa1993deSMatthew G Knepley dm->workin = dm->workin->next; 960aa1993deSMatthew G Knepley } else { 961aa1993deSMatthew G Knepley ierr = PetscNewLog(dm,struct _DMWorkLink,&link);CHKERRQ(ierr); 962a89ea682SMatthew G Knepley } 963aa1993deSMatthew G Knepley ierr = PetscDataTypeGetSize(dtype,&size);CHKERRQ(ierr); 964aa1993deSMatthew G Knepley if (size*count > link->bytes) { 965aa1993deSMatthew G Knepley ierr = PetscFree(link->mem);CHKERRQ(ierr); 966aa1993deSMatthew G Knepley ierr = PetscMalloc(size*count,&link->mem);CHKERRQ(ierr); 967aa1993deSMatthew G Knepley link->bytes = size*count; 968aa1993deSMatthew G Knepley } 969aa1993deSMatthew G Knepley link->next = dm->workout; 970aa1993deSMatthew G Knepley dm->workout = link; 971aa1993deSMatthew G Knepley *(void**)mem = link->mem; 972a89ea682SMatthew G Knepley PetscFunctionReturn(0); 973a89ea682SMatthew G Knepley } 974a89ea682SMatthew G Knepley 975aa1993deSMatthew G Knepley #undef __FUNCT__ 976aa1993deSMatthew G Knepley #define __FUNCT__ "DMRestoreWorkArray" 977aa1993deSMatthew G Knepley /*@C 978aa1993deSMatthew G Knepley DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray() 979aa1993deSMatthew G Knepley 980aa1993deSMatthew G Knepley Not Collective 981aa1993deSMatthew G Knepley 982aa1993deSMatthew G Knepley Input Parameters: 983aa1993deSMatthew G Knepley + dm - the DM object 984aa1993deSMatthew G Knepley . count - The minium size 985aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT) 986aa1993deSMatthew G Knepley 987aa1993deSMatthew G Knepley Output Parameter: 988aa1993deSMatthew G Knepley . array - the work array 989aa1993deSMatthew G Knepley 990aa1993deSMatthew G Knepley Level: developer 991aa1993deSMatthew G Knepley 992aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate() 993aa1993deSMatthew G Knepley @*/ 994aa1993deSMatthew G Knepley PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem) 995aa1993deSMatthew G Knepley { 996aa1993deSMatthew G Knepley DMWorkLink *p,link; 997aa1993deSMatthew G Knepley 998aa1993deSMatthew G Knepley PetscFunctionBegin; 999aa1993deSMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 1000aa1993deSMatthew G Knepley PetscValidPointer(mem,4); 1001aa1993deSMatthew G Knepley for (p=&dm->workout; (link=*p); p=&link->next) { 1002aa1993deSMatthew G Knepley if (link->mem == *(void**)mem) { 1003aa1993deSMatthew G Knepley *p = link->next; 1004aa1993deSMatthew G Knepley link->next = dm->workin; 1005aa1993deSMatthew G Knepley dm->workin = link; 10060298fd71SBarry Smith *(void**)mem = NULL; 1007aa1993deSMatthew G Knepley PetscFunctionReturn(0); 1008aa1993deSMatthew G Knepley } 1009aa1993deSMatthew G Knepley } 1010aa1993deSMatthew G Knepley SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out"); 1011aa1993deSMatthew G Knepley PetscFunctionReturn(0); 1012aa1993deSMatthew G Knepley } 1013e7c4fc90SDmitry Karpeev 1014e7c4fc90SDmitry Karpeev #undef __FUNCT__ 1015435a35e8SMatthew G Knepley #define __FUNCT__ "DMSetNullSpaceConstructor" 1016435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace)) 1017435a35e8SMatthew G Knepley { 1018435a35e8SMatthew G Knepley PetscFunctionBegin; 1019435a35e8SMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 102082f516ccSBarry Smith if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field); 1021435a35e8SMatthew G Knepley dm->nullspaceConstructors[field] = nullsp; 1022435a35e8SMatthew G Knepley PetscFunctionReturn(0); 1023435a35e8SMatthew G Knepley } 1024435a35e8SMatthew G Knepley 1025435a35e8SMatthew G Knepley #undef __FUNCT__ 10264d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS" 10274f3b5142SJed Brown /*@C 10284d343eeaSMatthew G Knepley DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field 10294d343eeaSMatthew G Knepley 10304d343eeaSMatthew G Knepley Not collective 10314d343eeaSMatthew G Knepley 10324d343eeaSMatthew G Knepley Input Parameter: 10334d343eeaSMatthew G Knepley . dm - the DM object 10344d343eeaSMatthew G Knepley 10354d343eeaSMatthew G Knepley Output Parameters: 10360298fd71SBarry Smith + numFields - The number of fields (or NULL if not requested) 10370298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested) 10380298fd71SBarry Smith - fields - The global indices for each field (or NULL if not requested) 10394d343eeaSMatthew G Knepley 10404d343eeaSMatthew G Knepley Level: intermediate 10414d343eeaSMatthew G Knepley 104221c9b008SJed Brown Notes: 104321c9b008SJed Brown The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with 104421c9b008SJed Brown PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with 104521c9b008SJed Brown PetscFree(). 104621c9b008SJed Brown 10474d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix() 10484d343eeaSMatthew G Knepley @*/ 104937d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields) 10504d343eeaSMatthew G Knepley { 105137d0c07bSMatthew G Knepley PetscSection section, sectionGlobal; 10524d343eeaSMatthew G Knepley PetscErrorCode ierr; 10534d343eeaSMatthew G Knepley 10544d343eeaSMatthew G Knepley PetscFunctionBegin; 10554d343eeaSMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 105669ca1f37SDmitry Karpeev if (numFields) { 105769ca1f37SDmitry Karpeev PetscValidPointer(numFields,2); 105869ca1f37SDmitry Karpeev *numFields = 0; 105969ca1f37SDmitry Karpeev } 106037d0c07bSMatthew G Knepley if (fieldNames) { 106137d0c07bSMatthew G Knepley PetscValidPointer(fieldNames,3); 10620298fd71SBarry Smith *fieldNames = NULL; 106369ca1f37SDmitry Karpeev } 106469ca1f37SDmitry Karpeev if (fields) { 106569ca1f37SDmitry Karpeev PetscValidPointer(fields,4); 10660298fd71SBarry Smith *fields = NULL; 106769ca1f37SDmitry Karpeev } 106837d0c07bSMatthew G Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 106937d0c07bSMatthew G Knepley if (section) { 107037d0c07bSMatthew G Knepley PetscInt *fieldSizes, **fieldIndices; 107137d0c07bSMatthew G Knepley PetscInt nF, f, pStart, pEnd, p; 107237d0c07bSMatthew G Knepley 107337d0c07bSMatthew G Knepley ierr = DMGetDefaultGlobalSection(dm, §ionGlobal);CHKERRQ(ierr); 107437d0c07bSMatthew G Knepley ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr); 107537d0c07bSMatthew G Knepley ierr = PetscMalloc2(nF,PetscInt,&fieldSizes,nF,PetscInt*,&fieldIndices);CHKERRQ(ierr); 107637d0c07bSMatthew G Knepley ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr); 107737d0c07bSMatthew G Knepley for (f = 0; f < nF; ++f) { 107837d0c07bSMatthew G Knepley fieldSizes[f] = 0; 107937d0c07bSMatthew G Knepley } 108037d0c07bSMatthew G Knepley for (p = pStart; p < pEnd; ++p) { 108137d0c07bSMatthew G Knepley PetscInt gdof; 108237d0c07bSMatthew G Knepley 108337d0c07bSMatthew G Knepley ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr); 108437d0c07bSMatthew G Knepley if (gdof > 0) { 108537d0c07bSMatthew G Knepley for (f = 0; f < nF; ++f) { 108637d0c07bSMatthew G Knepley PetscInt fdof, fcdof; 108737d0c07bSMatthew G Knepley 108837d0c07bSMatthew G Knepley ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr); 108937d0c07bSMatthew G Knepley ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr); 109037d0c07bSMatthew G Knepley fieldSizes[f] += fdof-fcdof; 109137d0c07bSMatthew G Knepley } 109237d0c07bSMatthew G Knepley } 109337d0c07bSMatthew G Knepley } 109437d0c07bSMatthew G Knepley for (f = 0; f < nF; ++f) { 109537d0c07bSMatthew G Knepley ierr = PetscMalloc(fieldSizes[f] * sizeof(PetscInt), &fieldIndices[f]);CHKERRQ(ierr); 109637d0c07bSMatthew G Knepley fieldSizes[f] = 0; 109737d0c07bSMatthew G Knepley } 109837d0c07bSMatthew G Knepley for (p = pStart; p < pEnd; ++p) { 109937d0c07bSMatthew G Knepley PetscInt gdof, goff; 110037d0c07bSMatthew G Knepley 110137d0c07bSMatthew G Knepley ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr); 110237d0c07bSMatthew G Knepley if (gdof > 0) { 110337d0c07bSMatthew G Knepley ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr); 110437d0c07bSMatthew G Knepley for (f = 0; f < nF; ++f) { 110537d0c07bSMatthew G Knepley PetscInt fdof, fcdof, fc; 110637d0c07bSMatthew G Knepley 110737d0c07bSMatthew G Knepley ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr); 110837d0c07bSMatthew G Knepley ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr); 110937d0c07bSMatthew G Knepley for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) { 111037d0c07bSMatthew G Knepley fieldIndices[f][fieldSizes[f]] = goff++; 111137d0c07bSMatthew G Knepley } 111237d0c07bSMatthew G Knepley } 111337d0c07bSMatthew G Knepley } 111437d0c07bSMatthew G Knepley } 11158865f1eaSKarl Rupp if (numFields) *numFields = nF; 111637d0c07bSMatthew G Knepley if (fieldNames) { 111737d0c07bSMatthew G Knepley ierr = PetscMalloc(nF * sizeof(char*), fieldNames);CHKERRQ(ierr); 111837d0c07bSMatthew G Knepley for (f = 0; f < nF; ++f) { 111937d0c07bSMatthew G Knepley const char *fieldName; 112037d0c07bSMatthew G Knepley 112137d0c07bSMatthew G Knepley ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr); 112237d0c07bSMatthew G Knepley ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr); 112337d0c07bSMatthew G Knepley } 112437d0c07bSMatthew G Knepley } 112537d0c07bSMatthew G Knepley if (fields) { 112637d0c07bSMatthew G Knepley ierr = PetscMalloc(nF * sizeof(IS), fields);CHKERRQ(ierr); 112737d0c07bSMatthew G Knepley for (f = 0; f < nF; ++f) { 112882f516ccSBarry Smith ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr); 112937d0c07bSMatthew G Knepley } 113037d0c07bSMatthew G Knepley } 113137d0c07bSMatthew G Knepley ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr); 11328865f1eaSKarl Rupp } else if (dm->ops->createfieldis) { 11338865f1eaSKarl Rupp ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr); 113469ca1f37SDmitry Karpeev } 11354d343eeaSMatthew G Knepley PetscFunctionReturn(0); 11364d343eeaSMatthew G Knepley } 11374d343eeaSMatthew G Knepley 113816621825SDmitry Karpeev 113916621825SDmitry Karpeev #undef __FUNCT__ 114016621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecomposition" 114116621825SDmitry Karpeev /*@C 114216621825SDmitry Karpeev DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems 114316621825SDmitry Karpeev corresponding to different fields: each IS contains the global indices of the dofs of the 114416621825SDmitry Karpeev corresponding field. The optional list of DMs define the DM for each subproblem. 1145e7c4fc90SDmitry Karpeev Generalizes DMCreateFieldIS(). 1146e7c4fc90SDmitry Karpeev 1147e7c4fc90SDmitry Karpeev Not collective 1148e7c4fc90SDmitry Karpeev 1149e7c4fc90SDmitry Karpeev Input Parameter: 1150e7c4fc90SDmitry Karpeev . dm - the DM object 1151e7c4fc90SDmitry Karpeev 1152e7c4fc90SDmitry Karpeev Output Parameters: 11530298fd71SBarry Smith + len - The number of subproblems in the field decomposition (or NULL if not requested) 11540298fd71SBarry Smith . namelist - The name for each field (or NULL if not requested) 11550298fd71SBarry Smith . islist - The global indices for each field (or NULL if not requested) 11560298fd71SBarry Smith - dmlist - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined) 1157e7c4fc90SDmitry Karpeev 1158e7c4fc90SDmitry Karpeev Level: intermediate 1159e7c4fc90SDmitry Karpeev 1160e7c4fc90SDmitry Karpeev Notes: 1161e7c4fc90SDmitry Karpeev The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with 1162e7c4fc90SDmitry Karpeev PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(), 1163e7c4fc90SDmitry Karpeev and all of the arrays should be freed with PetscFree(). 1164e7c4fc90SDmitry Karpeev 1165e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS() 1166e7c4fc90SDmitry Karpeev @*/ 116716621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist) 1168e7c4fc90SDmitry Karpeev { 1169e7c4fc90SDmitry Karpeev PetscErrorCode ierr; 1170e7c4fc90SDmitry Karpeev 1171e7c4fc90SDmitry Karpeev PetscFunctionBegin; 1172e7c4fc90SDmitry Karpeev PetscValidHeaderSpecific(dm,DM_CLASSID,1); 11738865f1eaSKarl Rupp if (len) { 11748865f1eaSKarl Rupp PetscValidPointer(len,2); 11758865f1eaSKarl Rupp *len = 0; 11768865f1eaSKarl Rupp } 11778865f1eaSKarl Rupp if (namelist) { 11788865f1eaSKarl Rupp PetscValidPointer(namelist,3); 11798865f1eaSKarl Rupp *namelist = 0; 11808865f1eaSKarl Rupp } 11818865f1eaSKarl Rupp if (islist) { 11828865f1eaSKarl Rupp PetscValidPointer(islist,4); 11838865f1eaSKarl Rupp *islist = 0; 11848865f1eaSKarl Rupp } 11858865f1eaSKarl Rupp if (dmlist) { 11868865f1eaSKarl Rupp PetscValidPointer(dmlist,5); 11878865f1eaSKarl Rupp *dmlist = 0; 11888865f1eaSKarl Rupp } 1189f3f0edfdSDmitry Karpeev /* 1190f3f0edfdSDmitry Karpeev Is it a good idea to apply the following check across all impls? 1191f3f0edfdSDmitry Karpeev Perhaps some impls can have a well-defined decomposition before DMSetUp? 1192f3f0edfdSDmitry Karpeev This, however, follows the general principle that accessors are not well-behaved until the object is set up. 1193f3f0edfdSDmitry Karpeev */ 1194ce94432eSBarry Smith if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp"); 119516621825SDmitry Karpeev if (!dm->ops->createfielddecomposition) { 1196435a35e8SMatthew G Knepley PetscSection section; 1197435a35e8SMatthew G Knepley PetscInt numFields, f; 1198435a35e8SMatthew G Knepley 1199435a35e8SMatthew G Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 1200435a35e8SMatthew G Knepley if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);} 1201435a35e8SMatthew G Knepley if (section && numFields && dm->ops->createsubdm) { 1202435a35e8SMatthew G Knepley *len = numFields; 1203435a35e8SMatthew G Knepley ierr = PetscMalloc3(numFields,char*,namelist,numFields,IS,islist,numFields,DM,dmlist);CHKERRQ(ierr); 1204435a35e8SMatthew G Knepley for (f = 0; f < numFields; ++f) { 1205435a35e8SMatthew G Knepley const char *fieldName; 1206435a35e8SMatthew G Knepley 1207435a35e8SMatthew G Knepley ierr = DMCreateSubDM(dm, 1, &f, &(*islist)[f], &(*dmlist)[f]);CHKERRQ(ierr); 1208435a35e8SMatthew G Knepley ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr); 1209435a35e8SMatthew G Knepley ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr); 1210435a35e8SMatthew G Knepley } 1211435a35e8SMatthew G Knepley } else { 121269ca1f37SDmitry Karpeev ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr); 1213e7c4fc90SDmitry Karpeev /* By default there are no DMs associated with subproblems. */ 12140298fd71SBarry Smith if (dmlist) *dmlist = NULL; 1215e7c4fc90SDmitry Karpeev } 12168865f1eaSKarl Rupp } else { 121716621825SDmitry Karpeev ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr); 121816621825SDmitry Karpeev } 121916621825SDmitry Karpeev PetscFunctionReturn(0); 122016621825SDmitry Karpeev } 122116621825SDmitry Karpeev 122216621825SDmitry Karpeev #undef __FUNCT__ 1223435a35e8SMatthew G Knepley #define __FUNCT__ "DMCreateSubDM" 1224435a35e8SMatthew G Knepley /*@C 1225435a35e8SMatthew G Knepley DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in. 1226435a35e8SMatthew G Knepley The fields are defined by DMCreateFieldIS(). 1227435a35e8SMatthew G Knepley 1228435a35e8SMatthew G Knepley Not collective 1229435a35e8SMatthew G Knepley 1230435a35e8SMatthew G Knepley Input Parameters: 1231435a35e8SMatthew G Knepley + dm - the DM object 1232435a35e8SMatthew G Knepley . numFields - number of fields in this subproblem 12330298fd71SBarry Smith - len - The number of subproblems in the decomposition (or NULL if not requested) 1234435a35e8SMatthew G Knepley 1235435a35e8SMatthew G Knepley Output Parameters: 1236435a35e8SMatthew G Knepley . is - The global indices for the subproblem 1237435a35e8SMatthew G Knepley - dm - The DM for the subproblem 1238435a35e8SMatthew G Knepley 1239435a35e8SMatthew G Knepley Level: intermediate 1240435a35e8SMatthew G Knepley 1241435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS() 1242435a35e8SMatthew G Knepley @*/ 1243435a35e8SMatthew G Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm) 1244435a35e8SMatthew G Knepley { 1245435a35e8SMatthew G Knepley PetscErrorCode ierr; 1246435a35e8SMatthew G Knepley 1247435a35e8SMatthew G Knepley PetscFunctionBegin; 1248435a35e8SMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 1249435a35e8SMatthew G Knepley PetscValidPointer(fields,3); 12508865f1eaSKarl Rupp if (is) PetscValidPointer(is,4); 12518865f1eaSKarl Rupp if (subdm) PetscValidPointer(subdm,5); 1252435a35e8SMatthew G Knepley if (dm->ops->createsubdm) { 1253435a35e8SMatthew G Knepley ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr); 125482f516ccSBarry Smith } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined"); 1255435a35e8SMatthew G Knepley PetscFunctionReturn(0); 1256435a35e8SMatthew G Knepley } 1257435a35e8SMatthew G Knepley 125816621825SDmitry Karpeev 125916621825SDmitry Karpeev #undef __FUNCT__ 126016621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecomposition" 126116621825SDmitry Karpeev /*@C 12628d4ac253SDmitry Karpeev DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems 12638d4ac253SDmitry Karpeev corresponding to restrictions to pairs nested subdomains: each IS contains the global 12648d4ac253SDmitry Karpeev indices of the dofs of the corresponding subdomains. The inner subdomains conceptually 12658d4ac253SDmitry Karpeev define a nonoverlapping covering, while outer subdomains can overlap. 12668d4ac253SDmitry Karpeev The optional list of DMs define the DM for each subproblem. 126716621825SDmitry Karpeev 126816621825SDmitry Karpeev Not collective 126916621825SDmitry Karpeev 127016621825SDmitry Karpeev Input Parameter: 127116621825SDmitry Karpeev . dm - the DM object 127216621825SDmitry Karpeev 127316621825SDmitry Karpeev Output Parameters: 12740298fd71SBarry Smith + len - The number of subproblems in the domain decomposition (or NULL if not requested) 12750298fd71SBarry Smith . namelist - The name for each subdomain (or NULL if not requested) 12760298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested) 12770298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested) 12780298fd71SBarry Smith - dmlist - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined) 127916621825SDmitry Karpeev 128016621825SDmitry Karpeev Level: intermediate 128116621825SDmitry Karpeev 128216621825SDmitry Karpeev Notes: 128316621825SDmitry Karpeev The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with 128416621825SDmitry Karpeev PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(), 128516621825SDmitry Karpeev and all of the arrays should be freed with PetscFree(). 128616621825SDmitry Karpeev 12878d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition() 128816621825SDmitry Karpeev @*/ 12898d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist) 129016621825SDmitry Karpeev { 129116621825SDmitry Karpeev PetscErrorCode ierr; 1292be081cd6SPeter Brune DMSubDomainHookLink link; 1293be081cd6SPeter Brune PetscInt i,l; 129416621825SDmitry Karpeev 129516621825SDmitry Karpeev PetscFunctionBegin; 129616621825SDmitry Karpeev PetscValidHeaderSpecific(dm,DM_CLASSID,1); 129714a18fd3SPeter Brune if (len) {PetscValidPointer(len,2); *len = 0;} 12980298fd71SBarry Smith if (namelist) {PetscValidPointer(namelist,3); *namelist = NULL;} 12990298fd71SBarry Smith if (innerislist) {PetscValidPointer(innerislist,4); *innerislist = NULL;} 13000298fd71SBarry Smith if (outerislist) {PetscValidPointer(outerislist,5); *outerislist = NULL;} 13010298fd71SBarry Smith if (dmlist) {PetscValidPointer(dmlist,6); *dmlist = NULL;} 1302f3f0edfdSDmitry Karpeev /* 1303f3f0edfdSDmitry Karpeev Is it a good idea to apply the following check across all impls? 1304f3f0edfdSDmitry Karpeev Perhaps some impls can have a well-defined decomposition before DMSetUp? 1305f3f0edfdSDmitry Karpeev This, however, follows the general principle that accessors are not well-behaved until the object is set up. 1306f3f0edfdSDmitry Karpeev */ 1307ce94432eSBarry Smith if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp"); 130816621825SDmitry Karpeev if (dm->ops->createdomaindecomposition) { 1309be081cd6SPeter Brune ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr); 131014a18fd3SPeter Brune /* copy subdomain hooks and context over to the subdomain DMs */ 131114a18fd3SPeter Brune if (dmlist) { 1312be081cd6SPeter Brune for (i = 0; i < l; i++) { 1313be081cd6SPeter Brune for (link=dm->subdomainhook; link; link=link->next) { 1314be081cd6SPeter Brune if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);} 1315be081cd6SPeter Brune } 1316d425c654SPeter Brune (*dmlist)[i]->ctx = dm->ctx; 1317e7c4fc90SDmitry Karpeev } 131814a18fd3SPeter Brune } 131914a18fd3SPeter Brune if (len) *len = l; 132014a18fd3SPeter Brune } 1321e30e807fSPeter Brune PetscFunctionReturn(0); 1322e30e807fSPeter Brune } 1323e30e807fSPeter Brune 1324e30e807fSPeter Brune 1325e30e807fSPeter Brune #undef __FUNCT__ 1326e30e807fSPeter Brune #define __FUNCT__ "DMCreateDomainDecompositionScatters" 1327e30e807fSPeter Brune /*@C 1328e30e807fSPeter Brune DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector 1329e30e807fSPeter Brune 1330e30e807fSPeter Brune Not collective 1331e30e807fSPeter Brune 1332e30e807fSPeter Brune Input Parameters: 1333e30e807fSPeter Brune + dm - the DM object 1334e30e807fSPeter Brune . n - the number of subdomain scatters 1335e30e807fSPeter Brune - subdms - the local subdomains 1336e30e807fSPeter Brune 1337e30e807fSPeter Brune Output Parameters: 1338e30e807fSPeter Brune + n - the number of scatters returned 1339e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain 1340e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain 1341e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts) 1342e30e807fSPeter Brune 1343e30e807fSPeter Brune Notes: This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution 1344e30e807fSPeter Brune of general nonlinear problems with overlapping subdomain methods. While merely having index sets that enable subsets 1345e30e807fSPeter Brune of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of 1346e30e807fSPeter Brune solution and residual data. 1347e30e807fSPeter Brune 1348e30e807fSPeter Brune Level: developer 1349e30e807fSPeter Brune 1350e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS() 1351e30e807fSPeter Brune @*/ 1352e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat) 1353e30e807fSPeter Brune { 1354e30e807fSPeter Brune PetscErrorCode ierr; 1355e30e807fSPeter Brune 1356e30e807fSPeter Brune PetscFunctionBegin; 1357e30e807fSPeter Brune PetscValidHeaderSpecific(dm,DM_CLASSID,1); 1358e30e807fSPeter Brune PetscValidPointer(subdms,3); 1359e30e807fSPeter Brune PetscValidPointer(iscat,4); 1360e30e807fSPeter Brune PetscValidPointer(oscat,5); 1361e30e807fSPeter Brune PetscValidPointer(gscat,6); 1362e30e807fSPeter Brune if (dm->ops->createddscatters) { 1363e30e807fSPeter Brune ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr); 136482f516ccSBarry Smith } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionLocalScatter implementation defined"); 1365e7c4fc90SDmitry Karpeev PetscFunctionReturn(0); 1366e7c4fc90SDmitry Karpeev } 1367e7c4fc90SDmitry Karpeev 1368731c8d9eSDmitry Karpeev #undef __FUNCT__ 136947c6ae99SBarry Smith #define __FUNCT__ "DMRefine" 137047c6ae99SBarry Smith /*@ 137147c6ae99SBarry Smith DMRefine - Refines a DM object 137247c6ae99SBarry Smith 137347c6ae99SBarry Smith Collective on DM 137447c6ae99SBarry Smith 137547c6ae99SBarry Smith Input Parameter: 137647c6ae99SBarry Smith + dm - the DM object 137791d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL) 137847c6ae99SBarry Smith 137947c6ae99SBarry Smith Output Parameter: 13800298fd71SBarry Smith . dmf - the refined DM, or NULL 1381ae0a1c52SMatthew G Knepley 13820298fd71SBarry Smith Note: If no refinement was done, the return value is NULL 138347c6ae99SBarry Smith 138447c6ae99SBarry Smith Level: developer 138547c6ae99SBarry Smith 1386e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation() 138747c6ae99SBarry Smith @*/ 13887087cfbeSBarry Smith PetscErrorCode DMRefine(DM dm,MPI_Comm comm,DM *dmf) 138947c6ae99SBarry Smith { 139047c6ae99SBarry Smith PetscErrorCode ierr; 1391c833c3b5SJed Brown DMRefineHookLink link; 139247c6ae99SBarry Smith 139347c6ae99SBarry Smith PetscFunctionBegin; 1394732e2eb9SMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 139547c6ae99SBarry Smith ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr); 13964057135bSMatthew G Knepley if (*dmf) { 139743842a1eSJed Brown (*dmf)->ops->creatematrix = dm->ops->creatematrix; 13988865f1eaSKarl Rupp 13998cd211a4SJed Brown ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr); 14008865f1eaSKarl Rupp 1401644e2e5bSBarry Smith (*dmf)->ctx = dm->ctx; 14020598a293SJed Brown (*dmf)->leveldown = dm->leveldown; 1403656b349aSBarry Smith (*dmf)->levelup = dm->levelup + 1; 14048865f1eaSKarl Rupp 1405e4b4b23bSJed Brown ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr); 1406c833c3b5SJed Brown for (link=dm->refinehook; link; link=link->next) { 14078865f1eaSKarl Rupp if (link->refinehook) { 14088865f1eaSKarl Rupp ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr); 14098865f1eaSKarl Rupp } 1410c833c3b5SJed Brown } 1411c833c3b5SJed Brown } 1412c833c3b5SJed Brown PetscFunctionReturn(0); 1413c833c3b5SJed Brown } 1414c833c3b5SJed Brown 1415c833c3b5SJed Brown #undef __FUNCT__ 1416c833c3b5SJed Brown #define __FUNCT__ "DMRefineHookAdd" 1417bb9467b5SJed Brown /*@C 1418c833c3b5SJed Brown DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid 1419c833c3b5SJed Brown 1420c833c3b5SJed Brown Logically Collective 1421c833c3b5SJed Brown 1422c833c3b5SJed Brown Input Arguments: 1423c833c3b5SJed Brown + coarse - nonlinear solver context on which to run a hook when restricting to a coarser level 1424c833c3b5SJed Brown . refinehook - function to run when setting up a coarser level 1425c833c3b5SJed Brown . interphook - function to run to update data on finer levels (once per SNESSolve()) 14260298fd71SBarry Smith - ctx - [optional] user-defined context for provide data for the hooks (may be NULL) 1427c833c3b5SJed Brown 1428c833c3b5SJed Brown Calling sequence of refinehook: 1429c833c3b5SJed Brown $ refinehook(DM coarse,DM fine,void *ctx); 1430c833c3b5SJed Brown 1431c833c3b5SJed Brown + coarse - coarse level DM 1432c833c3b5SJed Brown . fine - fine level DM to interpolate problem to 1433c833c3b5SJed Brown - ctx - optional user-defined function context 1434c833c3b5SJed Brown 1435c833c3b5SJed Brown Calling sequence for interphook: 1436c833c3b5SJed Brown $ interphook(DM coarse,Mat interp,DM fine,void *ctx) 1437c833c3b5SJed Brown 1438c833c3b5SJed Brown + coarse - coarse level DM 1439c833c3b5SJed Brown . interp - matrix interpolating a coarse-level solution to the finer grid 1440c833c3b5SJed Brown . fine - fine level DM to update 1441c833c3b5SJed Brown - ctx - optional user-defined function context 1442c833c3b5SJed Brown 1443c833c3b5SJed Brown Level: advanced 1444c833c3b5SJed Brown 1445c833c3b5SJed Brown Notes: 1446c833c3b5SJed Brown This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing 1447c833c3b5SJed Brown 1448c833c3b5SJed Brown If this function is called multiple times, the hooks will be run in the order they are added. 1449c833c3b5SJed Brown 1450bb9467b5SJed Brown This function is currently not available from Fortran. 1451bb9467b5SJed Brown 1452c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate() 1453c833c3b5SJed Brown @*/ 1454c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx) 1455c833c3b5SJed Brown { 1456c833c3b5SJed Brown PetscErrorCode ierr; 1457c833c3b5SJed Brown DMRefineHookLink link,*p; 1458c833c3b5SJed Brown 1459c833c3b5SJed Brown PetscFunctionBegin; 1460c833c3b5SJed Brown PetscValidHeaderSpecific(coarse,DM_CLASSID,1); 1461c833c3b5SJed Brown for (p=&coarse->refinehook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */ 1462c833c3b5SJed Brown ierr = PetscMalloc(sizeof(struct _DMRefineHookLink),&link);CHKERRQ(ierr); 1463c833c3b5SJed Brown link->refinehook = refinehook; 1464c833c3b5SJed Brown link->interphook = interphook; 1465c833c3b5SJed Brown link->ctx = ctx; 14660298fd71SBarry Smith link->next = NULL; 1467c833c3b5SJed Brown *p = link; 1468c833c3b5SJed Brown PetscFunctionReturn(0); 1469c833c3b5SJed Brown } 1470c833c3b5SJed Brown 1471c833c3b5SJed Brown #undef __FUNCT__ 1472c833c3b5SJed Brown #define __FUNCT__ "DMInterpolate" 1473c833c3b5SJed Brown /*@ 1474c833c3b5SJed Brown DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd() 1475c833c3b5SJed Brown 1476c833c3b5SJed Brown Collective if any hooks are 1477c833c3b5SJed Brown 1478c833c3b5SJed Brown Input Arguments: 1479c833c3b5SJed Brown + coarse - coarser DM to use as a base 1480c833c3b5SJed Brown . restrct - interpolation matrix, apply using MatInterpolate() 1481c833c3b5SJed Brown - fine - finer DM to update 1482c833c3b5SJed Brown 1483c833c3b5SJed Brown Level: developer 1484c833c3b5SJed Brown 1485c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate() 1486c833c3b5SJed Brown @*/ 1487c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine) 1488c833c3b5SJed Brown { 1489c833c3b5SJed Brown PetscErrorCode ierr; 1490c833c3b5SJed Brown DMRefineHookLink link; 1491c833c3b5SJed Brown 1492c833c3b5SJed Brown PetscFunctionBegin; 1493c833c3b5SJed Brown for (link=fine->refinehook; link; link=link->next) { 14948865f1eaSKarl Rupp if (link->interphook) { 14958865f1eaSKarl Rupp ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr); 14968865f1eaSKarl Rupp } 14974057135bSMatthew G Knepley } 149847c6ae99SBarry Smith PetscFunctionReturn(0); 149947c6ae99SBarry Smith } 150047c6ae99SBarry Smith 150147c6ae99SBarry Smith #undef __FUNCT__ 1502eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel" 1503eb3f98d2SBarry Smith /*@ 1504eb3f98d2SBarry Smith DMGetRefineLevel - Get's the number of refinements that have generated this DM. 1505eb3f98d2SBarry Smith 1506eb3f98d2SBarry Smith Not Collective 1507eb3f98d2SBarry Smith 1508eb3f98d2SBarry Smith Input Parameter: 1509eb3f98d2SBarry Smith . dm - the DM object 1510eb3f98d2SBarry Smith 1511eb3f98d2SBarry Smith Output Parameter: 1512eb3f98d2SBarry Smith . level - number of refinements 1513eb3f98d2SBarry Smith 1514eb3f98d2SBarry Smith Level: developer 1515eb3f98d2SBarry Smith 15166a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation() 1517eb3f98d2SBarry Smith 1518eb3f98d2SBarry Smith @*/ 1519eb3f98d2SBarry Smith PetscErrorCode DMGetRefineLevel(DM dm,PetscInt *level) 1520eb3f98d2SBarry Smith { 1521eb3f98d2SBarry Smith PetscFunctionBegin; 1522eb3f98d2SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 1523eb3f98d2SBarry Smith *level = dm->levelup; 1524eb3f98d2SBarry Smith PetscFunctionReturn(0); 1525eb3f98d2SBarry Smith } 1526eb3f98d2SBarry Smith 1527eb3f98d2SBarry Smith #undef __FUNCT__ 1528baf369e7SPeter Brune #define __FUNCT__ "DMGlobalToLocalHookAdd" 1529bb9467b5SJed Brown /*@C 1530baf369e7SPeter Brune DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called 1531baf369e7SPeter Brune 1532baf369e7SPeter Brune Logically Collective 1533baf369e7SPeter Brune 1534baf369e7SPeter Brune Input Arguments: 1535baf369e7SPeter Brune + dm - the DM 1536baf369e7SPeter Brune . beginhook - function to run at the beginning of DMGlobalToLocalBegin() 1537baf369e7SPeter Brune . endhook - function to run after DMGlobalToLocalEnd() has completed 15380298fd71SBarry Smith - ctx - [optional] user-defined context for provide data for the hooks (may be NULL) 1539baf369e7SPeter Brune 1540baf369e7SPeter Brune Calling sequence for beginhook: 1541baf369e7SPeter Brune $ beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx) 1542baf369e7SPeter Brune 1543baf369e7SPeter Brune + dm - global DM 1544baf369e7SPeter Brune . g - global vector 1545baf369e7SPeter Brune . mode - mode 1546baf369e7SPeter Brune . l - local vector 1547baf369e7SPeter Brune - ctx - optional user-defined function context 1548baf369e7SPeter Brune 1549baf369e7SPeter Brune 1550baf369e7SPeter Brune Calling sequence for endhook: 1551baf369e7SPeter Brune $ beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx) 1552baf369e7SPeter Brune 1553baf369e7SPeter Brune + global - global DM 1554baf369e7SPeter Brune - ctx - optional user-defined function context 1555baf369e7SPeter Brune 1556baf369e7SPeter Brune Level: advanced 1557baf369e7SPeter Brune 1558baf369e7SPeter Brune Notes: 1559baf369e7SPeter Brune This function is only needed if auxiliary data needs to be set up on coarse grids. 1560baf369e7SPeter Brune 1561baf369e7SPeter Brune If this function is called multiple times, the hooks will be run in the order they are added. 1562baf369e7SPeter Brune 1563baf369e7SPeter Brune In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to 1564baf369e7SPeter Brune extract the finest level information from its context (instead of from the SNES). 1565baf369e7SPeter Brune 1566bb9467b5SJed Brown This function is currently not available from Fortran. 1567bb9467b5SJed Brown 1568baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate() 1569baf369e7SPeter Brune @*/ 1570baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx) 1571baf369e7SPeter Brune { 1572baf369e7SPeter Brune PetscErrorCode ierr; 1573baf369e7SPeter Brune DMGlobalToLocalHookLink link,*p; 1574baf369e7SPeter Brune 1575baf369e7SPeter Brune PetscFunctionBegin; 1576baf369e7SPeter Brune PetscValidHeaderSpecific(dm,DM_CLASSID,1); 1577baf369e7SPeter Brune for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */ 1578baf369e7SPeter Brune ierr = PetscMalloc(sizeof(struct _DMGlobalToLocalHookLink),&link);CHKERRQ(ierr); 1579baf369e7SPeter Brune link->beginhook = beginhook; 1580baf369e7SPeter Brune link->endhook = endhook; 1581baf369e7SPeter Brune link->ctx = ctx; 15820298fd71SBarry Smith link->next = NULL; 1583baf369e7SPeter Brune *p = link; 1584baf369e7SPeter Brune PetscFunctionReturn(0); 1585baf369e7SPeter Brune } 1586baf369e7SPeter Brune 1587baf369e7SPeter Brune #undef __FUNCT__ 158847c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin" 158947c6ae99SBarry Smith /*@ 159047c6ae99SBarry Smith DMGlobalToLocalBegin - Begins updating local vectors from global vector 159147c6ae99SBarry Smith 159247c6ae99SBarry Smith Neighbor-wise Collective on DM 159347c6ae99SBarry Smith 159447c6ae99SBarry Smith Input Parameters: 159547c6ae99SBarry Smith + dm - the DM object 159647c6ae99SBarry Smith . g - the global vector 159747c6ae99SBarry Smith . mode - INSERT_VALUES or ADD_VALUES 159847c6ae99SBarry Smith - l - the local vector 159947c6ae99SBarry Smith 160047c6ae99SBarry Smith 160147c6ae99SBarry Smith Level: beginner 160247c6ae99SBarry Smith 1603e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin() 160447c6ae99SBarry Smith 160547c6ae99SBarry Smith @*/ 16067087cfbeSBarry Smith PetscErrorCode DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l) 160747c6ae99SBarry Smith { 16087128ae9fSMatthew G Knepley PetscSF sf; 160947c6ae99SBarry Smith PetscErrorCode ierr; 1610baf369e7SPeter Brune DMGlobalToLocalHookLink link; 161147c6ae99SBarry Smith 161247c6ae99SBarry Smith PetscFunctionBegin; 1613171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 1614baf369e7SPeter Brune for (link=dm->gtolhook; link; link=link->next) { 16158865f1eaSKarl Rupp if (link->beginhook) { 16168865f1eaSKarl Rupp ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr); 16178865f1eaSKarl Rupp } 1618baf369e7SPeter Brune } 16197128ae9fSMatthew G Knepley ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr); 16207128ae9fSMatthew G Knepley if (sf) { 16217128ae9fSMatthew G Knepley PetscScalar *lArray, *gArray; 16227128ae9fSMatthew G Knepley 162382f516ccSBarry Smith if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode); 16247128ae9fSMatthew G Knepley ierr = VecGetArray(l, &lArray);CHKERRQ(ierr); 16257128ae9fSMatthew G Knepley ierr = VecGetArray(g, &gArray);CHKERRQ(ierr); 16267128ae9fSMatthew G Knepley ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr); 16277128ae9fSMatthew G Knepley ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr); 16287128ae9fSMatthew G Knepley ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr); 16297128ae9fSMatthew G Knepley } else { 1630843c4018SMatthew G Knepley ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr); 16317128ae9fSMatthew G Knepley } 163247c6ae99SBarry Smith PetscFunctionReturn(0); 163347c6ae99SBarry Smith } 163447c6ae99SBarry Smith 163547c6ae99SBarry Smith #undef __FUNCT__ 163647c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd" 163747c6ae99SBarry Smith /*@ 163847c6ae99SBarry Smith DMGlobalToLocalEnd - Ends updating local vectors from global vector 163947c6ae99SBarry Smith 164047c6ae99SBarry Smith Neighbor-wise Collective on DM 164147c6ae99SBarry Smith 164247c6ae99SBarry Smith Input Parameters: 164347c6ae99SBarry Smith + dm - the DM object 164447c6ae99SBarry Smith . g - the global vector 164547c6ae99SBarry Smith . mode - INSERT_VALUES or ADD_VALUES 164647c6ae99SBarry Smith - l - the local vector 164747c6ae99SBarry Smith 164847c6ae99SBarry Smith 164947c6ae99SBarry Smith Level: beginner 165047c6ae99SBarry Smith 1651e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin() 165247c6ae99SBarry Smith 165347c6ae99SBarry Smith @*/ 16547087cfbeSBarry Smith PetscErrorCode DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l) 165547c6ae99SBarry Smith { 16567128ae9fSMatthew G Knepley PetscSF sf; 165747c6ae99SBarry Smith PetscErrorCode ierr; 165861a3c1faSSatish Balay PetscScalar *lArray, *gArray; 1659baf369e7SPeter Brune DMGlobalToLocalHookLink link; 166047c6ae99SBarry Smith 166147c6ae99SBarry Smith PetscFunctionBegin; 1662171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 16637128ae9fSMatthew G Knepley ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr); 16647128ae9fSMatthew G Knepley if (sf) { 166582f516ccSBarry Smith if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode); 16667128ae9fSMatthew G Knepley 16677128ae9fSMatthew G Knepley ierr = VecGetArray(l, &lArray);CHKERRQ(ierr); 16687128ae9fSMatthew G Knepley ierr = VecGetArray(g, &gArray);CHKERRQ(ierr); 16697128ae9fSMatthew G Knepley ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr); 16707128ae9fSMatthew G Knepley ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr); 16717128ae9fSMatthew G Knepley ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr); 16727128ae9fSMatthew G Knepley } else { 1673843c4018SMatthew G Knepley ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr); 16747128ae9fSMatthew G Knepley } 1675baf369e7SPeter Brune for (link=dm->gtolhook; link; link=link->next) { 1676baf369e7SPeter Brune if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);} 1677baf369e7SPeter Brune } 167847c6ae99SBarry Smith PetscFunctionReturn(0); 167947c6ae99SBarry Smith } 168047c6ae99SBarry Smith 168147c6ae99SBarry Smith #undef __FUNCT__ 16829a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin" 168347c6ae99SBarry Smith /*@ 16849a42bb27SBarry Smith DMLocalToGlobalBegin - updates global vectors from local vectors 16859a42bb27SBarry Smith 16869a42bb27SBarry Smith Neighbor-wise Collective on DM 16879a42bb27SBarry Smith 16889a42bb27SBarry Smith Input Parameters: 16899a42bb27SBarry Smith + dm - the DM object 1690f6813fd5SJed Brown . l - the local vector 16919a42bb27SBarry Smith . mode - if INSERT_VALUES then no parallel communication is used, if ADD_VALUES then all ghost points from the same base point accumulate into that 16929a42bb27SBarry Smith base point. 1693f6813fd5SJed Brown - - the global vector 16949a42bb27SBarry Smith 16959a42bb27SBarry Smith Notes: In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation. If you would like to simply add the non-ghosted values in the local 16969a42bb27SBarry Smith array into the global array you need to either (1) zero the ghosted locations and use ADD_VALUES or (2) use INSERT_VALUES into a work global array and then add the work 16979a42bb27SBarry Smith global array to the final global array with VecAXPY(). 16989a42bb27SBarry Smith 16999a42bb27SBarry Smith Level: beginner 17009a42bb27SBarry Smith 1701e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin() 17029a42bb27SBarry Smith 17039a42bb27SBarry Smith @*/ 17047087cfbeSBarry Smith PetscErrorCode DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g) 17059a42bb27SBarry Smith { 17067128ae9fSMatthew G Knepley PetscSF sf; 17079a42bb27SBarry Smith PetscErrorCode ierr; 17089a42bb27SBarry Smith 17099a42bb27SBarry Smith PetscFunctionBegin; 1710171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 17117128ae9fSMatthew G Knepley ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr); 17127128ae9fSMatthew G Knepley if (sf) { 17137128ae9fSMatthew G Knepley MPI_Op op; 17147128ae9fSMatthew G Knepley PetscScalar *lArray, *gArray; 17157128ae9fSMatthew G Knepley 17167128ae9fSMatthew G Knepley switch (mode) { 17177128ae9fSMatthew G Knepley case INSERT_VALUES: 17187128ae9fSMatthew G Knepley case INSERT_ALL_VALUES: 17197128ae9fSMatthew G Knepley #if defined(PETSC_HAVE_MPI_REPLACE) 17207128ae9fSMatthew G Knepley op = MPI_REPLACE; break; 17217128ae9fSMatthew G Knepley #else 1722ce94432eSBarry Smith SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No support for INSERT_VALUES without an MPI-2 implementation"); 17237128ae9fSMatthew G Knepley #endif 17247128ae9fSMatthew G Knepley case ADD_VALUES: 17257128ae9fSMatthew G Knepley case ADD_ALL_VALUES: 17267128ae9fSMatthew G Knepley op = MPI_SUM; break; 17277128ae9fSMatthew G Knepley default: 172882f516ccSBarry Smith SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode); 17297128ae9fSMatthew G Knepley } 17307128ae9fSMatthew G Knepley ierr = VecGetArray(l, &lArray);CHKERRQ(ierr); 17317128ae9fSMatthew G Knepley ierr = VecGetArray(g, &gArray);CHKERRQ(ierr); 17327128ae9fSMatthew G Knepley ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr); 17337128ae9fSMatthew G Knepley ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr); 17347128ae9fSMatthew G Knepley ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr); 17357128ae9fSMatthew G Knepley } else { 1736843c4018SMatthew G Knepley ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr); 17377128ae9fSMatthew G Knepley } 17389a42bb27SBarry Smith PetscFunctionReturn(0); 17399a42bb27SBarry Smith } 17409a42bb27SBarry Smith 17419a42bb27SBarry Smith #undef __FUNCT__ 17429a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd" 17439a42bb27SBarry Smith /*@ 17449a42bb27SBarry Smith DMLocalToGlobalEnd - updates global vectors from local vectors 174547c6ae99SBarry Smith 174647c6ae99SBarry Smith Neighbor-wise Collective on DM 174747c6ae99SBarry Smith 174847c6ae99SBarry Smith Input Parameters: 174947c6ae99SBarry Smith + dm - the DM object 1750f6813fd5SJed Brown . l - the local vector 175147c6ae99SBarry Smith . mode - INSERT_VALUES or ADD_VALUES 1752f6813fd5SJed Brown - g - the global vector 175347c6ae99SBarry Smith 175447c6ae99SBarry Smith 175547c6ae99SBarry Smith Level: beginner 175647c6ae99SBarry Smith 1757e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd() 175847c6ae99SBarry Smith 175947c6ae99SBarry Smith @*/ 17607087cfbeSBarry Smith PetscErrorCode DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g) 176147c6ae99SBarry Smith { 17627128ae9fSMatthew G Knepley PetscSF sf; 176347c6ae99SBarry Smith PetscErrorCode ierr; 176447c6ae99SBarry Smith 176547c6ae99SBarry Smith PetscFunctionBegin; 1766171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 17677128ae9fSMatthew G Knepley ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr); 17687128ae9fSMatthew G Knepley if (sf) { 17697128ae9fSMatthew G Knepley MPI_Op op; 17707128ae9fSMatthew G Knepley PetscScalar *lArray, *gArray; 17717128ae9fSMatthew G Knepley 17727128ae9fSMatthew G Knepley switch (mode) { 17737128ae9fSMatthew G Knepley case INSERT_VALUES: 17747128ae9fSMatthew G Knepley case INSERT_ALL_VALUES: 17757128ae9fSMatthew G Knepley #if defined(PETSC_HAVE_MPI_REPLACE) 17767128ae9fSMatthew G Knepley op = MPI_REPLACE; break; 17777128ae9fSMatthew G Knepley #else 1778ce94432eSBarry Smith SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No support for INSERT_VALUES without an MPI-2 implementation"); 17797128ae9fSMatthew G Knepley #endif 17807128ae9fSMatthew G Knepley case ADD_VALUES: 17817128ae9fSMatthew G Knepley case ADD_ALL_VALUES: 17827128ae9fSMatthew G Knepley op = MPI_SUM; break; 17837128ae9fSMatthew G Knepley default: 178482f516ccSBarry Smith SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode); 17857128ae9fSMatthew G Knepley } 17867128ae9fSMatthew G Knepley ierr = VecGetArray(l, &lArray);CHKERRQ(ierr); 17877128ae9fSMatthew G Knepley ierr = VecGetArray(g, &gArray);CHKERRQ(ierr); 17887128ae9fSMatthew G Knepley ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr); 17897128ae9fSMatthew G Knepley ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr); 17907128ae9fSMatthew G Knepley ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr); 17917128ae9fSMatthew G Knepley } else { 1792843c4018SMatthew G Knepley ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr); 17937128ae9fSMatthew G Knepley } 179447c6ae99SBarry Smith PetscFunctionReturn(0); 179547c6ae99SBarry Smith } 179647c6ae99SBarry Smith 179747c6ae99SBarry Smith #undef __FUNCT__ 179847c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen" 179947c6ae99SBarry Smith /*@ 180047c6ae99SBarry Smith DMCoarsen - Coarsens a DM object 180147c6ae99SBarry Smith 180247c6ae99SBarry Smith Collective on DM 180347c6ae99SBarry Smith 180447c6ae99SBarry Smith Input Parameter: 180547c6ae99SBarry Smith + dm - the DM object 180691d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL) 180747c6ae99SBarry Smith 180847c6ae99SBarry Smith Output Parameter: 180947c6ae99SBarry Smith . dmc - the coarsened DM 181047c6ae99SBarry Smith 181147c6ae99SBarry Smith Level: developer 181247c6ae99SBarry Smith 1813e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation() 181447c6ae99SBarry Smith 181547c6ae99SBarry Smith @*/ 18167087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc) 181747c6ae99SBarry Smith { 181847c6ae99SBarry Smith PetscErrorCode ierr; 1819b17ce1afSJed Brown DMCoarsenHookLink link; 182047c6ae99SBarry Smith 182147c6ae99SBarry Smith PetscFunctionBegin; 1822171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 182347c6ae99SBarry Smith ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr); 182443842a1eSJed Brown (*dmc)->ops->creatematrix = dm->ops->creatematrix; 18258cd211a4SJed Brown ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr); 1826644e2e5bSBarry Smith (*dmc)->ctx = dm->ctx; 18270598a293SJed Brown (*dmc)->levelup = dm->levelup; 1828656b349aSBarry Smith (*dmc)->leveldown = dm->leveldown + 1; 1829e4b4b23bSJed Brown ierr = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr); 1830b17ce1afSJed Brown for (link=dm->coarsenhook; link; link=link->next) { 1831b17ce1afSJed Brown if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);} 1832b17ce1afSJed Brown } 1833b17ce1afSJed Brown PetscFunctionReturn(0); 1834b17ce1afSJed Brown } 1835b17ce1afSJed Brown 1836b17ce1afSJed Brown #undef __FUNCT__ 1837b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd" 1838bb9467b5SJed Brown /*@C 1839b17ce1afSJed Brown DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid 1840b17ce1afSJed Brown 1841b17ce1afSJed Brown Logically Collective 1842b17ce1afSJed Brown 1843b17ce1afSJed Brown Input Arguments: 1844b17ce1afSJed Brown + fine - nonlinear solver context on which to run a hook when restricting to a coarser level 1845b17ce1afSJed Brown . coarsenhook - function to run when setting up a coarser level 1846b17ce1afSJed Brown . restricthook - function to run to update data on coarser levels (once per SNESSolve()) 18470298fd71SBarry Smith - ctx - [optional] user-defined context for provide data for the hooks (may be NULL) 1848b17ce1afSJed Brown 1849b17ce1afSJed Brown Calling sequence of coarsenhook: 1850b17ce1afSJed Brown $ coarsenhook(DM fine,DM coarse,void *ctx); 1851b17ce1afSJed Brown 1852b17ce1afSJed Brown + fine - fine level DM 1853b17ce1afSJed Brown . coarse - coarse level DM to restrict problem to 1854b17ce1afSJed Brown - ctx - optional user-defined function context 1855b17ce1afSJed Brown 1856b17ce1afSJed Brown Calling sequence for restricthook: 1857c833c3b5SJed Brown $ restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx) 1858b17ce1afSJed Brown 1859b17ce1afSJed Brown + fine - fine level DM 1860b17ce1afSJed Brown . mrestrict - matrix restricting a fine-level solution to the coarse grid 1861c833c3b5SJed Brown . rscale - scaling vector for restriction 1862c833c3b5SJed Brown . inject - matrix restricting by injection 1863b17ce1afSJed Brown . coarse - coarse level DM to update 1864b17ce1afSJed Brown - ctx - optional user-defined function context 1865b17ce1afSJed Brown 1866b17ce1afSJed Brown Level: advanced 1867b17ce1afSJed Brown 1868b17ce1afSJed Brown Notes: 1869b17ce1afSJed Brown This function is only needed if auxiliary data needs to be set up on coarse grids. 1870b17ce1afSJed Brown 1871b17ce1afSJed Brown If this function is called multiple times, the hooks will be run in the order they are added. 1872b17ce1afSJed Brown 1873b17ce1afSJed Brown In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to 1874b17ce1afSJed Brown extract the finest level information from its context (instead of from the SNES). 1875b17ce1afSJed Brown 1876bb9467b5SJed Brown This function is currently not available from Fortran. 1877bb9467b5SJed Brown 1878c833c3b5SJed Brown .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate() 1879b17ce1afSJed Brown @*/ 1880b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx) 1881b17ce1afSJed Brown { 1882b17ce1afSJed Brown PetscErrorCode ierr; 1883b17ce1afSJed Brown DMCoarsenHookLink link,*p; 1884b17ce1afSJed Brown 1885b17ce1afSJed Brown PetscFunctionBegin; 1886b17ce1afSJed Brown PetscValidHeaderSpecific(fine,DM_CLASSID,1); 18876bfea28cSJed Brown for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */ 1888b17ce1afSJed Brown ierr = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr); 1889b17ce1afSJed Brown link->coarsenhook = coarsenhook; 1890b17ce1afSJed Brown link->restricthook = restricthook; 1891b17ce1afSJed Brown link->ctx = ctx; 18920298fd71SBarry Smith link->next = NULL; 1893b17ce1afSJed Brown *p = link; 1894b17ce1afSJed Brown PetscFunctionReturn(0); 1895b17ce1afSJed Brown } 1896b17ce1afSJed Brown 1897b17ce1afSJed Brown #undef __FUNCT__ 1898b17ce1afSJed Brown #define __FUNCT__ "DMRestrict" 1899b17ce1afSJed Brown /*@ 1900b17ce1afSJed Brown DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd() 1901b17ce1afSJed Brown 1902b17ce1afSJed Brown Collective if any hooks are 1903b17ce1afSJed Brown 1904b17ce1afSJed Brown Input Arguments: 1905b17ce1afSJed Brown + fine - finer DM to use as a base 1906b17ce1afSJed Brown . restrct - restriction matrix, apply using MatRestrict() 1907b17ce1afSJed Brown . inject - injection matrix, also use MatRestrict() 1908b17ce1afSJed Brown - coarse - coarer DM to update 1909b17ce1afSJed Brown 1910b17ce1afSJed Brown Level: developer 1911b17ce1afSJed Brown 1912b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict() 1913b17ce1afSJed Brown @*/ 1914b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse) 1915b17ce1afSJed Brown { 1916b17ce1afSJed Brown PetscErrorCode ierr; 1917b17ce1afSJed Brown DMCoarsenHookLink link; 1918b17ce1afSJed Brown 1919b17ce1afSJed Brown PetscFunctionBegin; 1920b17ce1afSJed Brown for (link=fine->coarsenhook; link; link=link->next) { 19218865f1eaSKarl Rupp if (link->restricthook) { 19228865f1eaSKarl Rupp ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr); 19238865f1eaSKarl Rupp } 1924b17ce1afSJed Brown } 192547c6ae99SBarry Smith PetscFunctionReturn(0); 192647c6ae99SBarry Smith } 192747c6ae99SBarry Smith 192847c6ae99SBarry Smith #undef __FUNCT__ 1929be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainHookAdd" 1930bb9467b5SJed Brown /*@C 1931be081cd6SPeter Brune DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid 19325dbd56e3SPeter Brune 19335dbd56e3SPeter Brune Logically Collective 19345dbd56e3SPeter Brune 19355dbd56e3SPeter Brune Input Arguments: 19365dbd56e3SPeter Brune + global - global DM 19375dbd56e3SPeter Brune . restricthook - function to run to update data on block solve (at the beginning of the block solve) 19380298fd71SBarry Smith - ctx - [optional] user-defined context for provide data for the hooks (may be NULL) 19395dbd56e3SPeter Brune 19405dbd56e3SPeter Brune Calling sequence for restricthook: 19415dbd56e3SPeter Brune $ restricthook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx) 19425dbd56e3SPeter Brune 19435dbd56e3SPeter Brune + global - global DM 19445dbd56e3SPeter Brune . out - scatter to the outer (with ghost and overlap points) block vector 19455dbd56e3SPeter Brune . in - scatter to block vector values only owned locally 19465dbd56e3SPeter Brune . block - block DM (may just be a shell if the global DM is passed in correctly) 19475dbd56e3SPeter Brune - ctx - optional user-defined function context 19485dbd56e3SPeter Brune 19495dbd56e3SPeter Brune Level: advanced 19505dbd56e3SPeter Brune 19515dbd56e3SPeter Brune Notes: 19525dbd56e3SPeter Brune This function is only needed if auxiliary data needs to be set up on coarse grids. 19535dbd56e3SPeter Brune 19545dbd56e3SPeter Brune If this function is called multiple times, the hooks will be run in the order they are added. 19555dbd56e3SPeter Brune 19565dbd56e3SPeter Brune In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to 19575dbd56e3SPeter Brune extract the finest level information from its context (instead of from the SNES). 19585dbd56e3SPeter Brune 1959bb9467b5SJed Brown This function is currently not available from Fortran. 1960bb9467b5SJed Brown 19615dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate() 19625dbd56e3SPeter Brune @*/ 1963be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx) 19645dbd56e3SPeter Brune { 19655dbd56e3SPeter Brune PetscErrorCode ierr; 1966be081cd6SPeter Brune DMSubDomainHookLink link,*p; 19675dbd56e3SPeter Brune 19685dbd56e3SPeter Brune PetscFunctionBegin; 19695dbd56e3SPeter Brune PetscValidHeaderSpecific(global,DM_CLASSID,1); 1970be081cd6SPeter Brune for (p=&global->subdomainhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */ 1971be081cd6SPeter Brune ierr = PetscMalloc(sizeof(struct _DMSubDomainHookLink),&link);CHKERRQ(ierr); 19725dbd56e3SPeter Brune link->restricthook = restricthook; 1973be081cd6SPeter Brune link->ddhook = ddhook; 19745dbd56e3SPeter Brune link->ctx = ctx; 19750298fd71SBarry Smith link->next = NULL; 19765dbd56e3SPeter Brune *p = link; 19775dbd56e3SPeter Brune PetscFunctionReturn(0); 19785dbd56e3SPeter Brune } 19795dbd56e3SPeter Brune 19805dbd56e3SPeter Brune #undef __FUNCT__ 1981be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainRestrict" 19825dbd56e3SPeter Brune /*@ 1983be081cd6SPeter Brune DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd() 19845dbd56e3SPeter Brune 19855dbd56e3SPeter Brune Collective if any hooks are 19865dbd56e3SPeter Brune 19875dbd56e3SPeter Brune Input Arguments: 19885dbd56e3SPeter Brune + fine - finer DM to use as a base 1989be081cd6SPeter Brune . oscatter - scatter from domain global vector filling subdomain global vector with overlap 1990be081cd6SPeter Brune . gscatter - scatter from domain global vector filling subdomain local vector with ghosts 19915dbd56e3SPeter Brune - coarse - coarer DM to update 19925dbd56e3SPeter Brune 19935dbd56e3SPeter Brune Level: developer 19945dbd56e3SPeter Brune 19955dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict() 19965dbd56e3SPeter Brune @*/ 1997be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm) 19985dbd56e3SPeter Brune { 19995dbd56e3SPeter Brune PetscErrorCode ierr; 2000be081cd6SPeter Brune DMSubDomainHookLink link; 20015dbd56e3SPeter Brune 20025dbd56e3SPeter Brune PetscFunctionBegin; 2003be081cd6SPeter Brune for (link=global->subdomainhook; link; link=link->next) { 20048865f1eaSKarl Rupp if (link->restricthook) { 20058865f1eaSKarl Rupp ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr); 20068865f1eaSKarl Rupp } 20075dbd56e3SPeter Brune } 20085dbd56e3SPeter Brune PetscFunctionReturn(0); 20095dbd56e3SPeter Brune } 20105dbd56e3SPeter Brune 20115dbd56e3SPeter Brune #undef __FUNCT__ 20125fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel" 20135fe1f584SPeter Brune /*@ 20146a7d9d85SPeter Brune DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM. 20155fe1f584SPeter Brune 20165fe1f584SPeter Brune Not Collective 20175fe1f584SPeter Brune 20185fe1f584SPeter Brune Input Parameter: 20195fe1f584SPeter Brune . dm - the DM object 20205fe1f584SPeter Brune 20215fe1f584SPeter Brune Output Parameter: 20226a7d9d85SPeter Brune . level - number of coarsenings 20235fe1f584SPeter Brune 20245fe1f584SPeter Brune Level: developer 20255fe1f584SPeter Brune 20266a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation() 20275fe1f584SPeter Brune 20285fe1f584SPeter Brune @*/ 20295fe1f584SPeter Brune PetscErrorCode DMGetCoarsenLevel(DM dm,PetscInt *level) 20305fe1f584SPeter Brune { 20315fe1f584SPeter Brune PetscFunctionBegin; 20325fe1f584SPeter Brune PetscValidHeaderSpecific(dm,DM_CLASSID,1); 20335fe1f584SPeter Brune *level = dm->leveldown; 20345fe1f584SPeter Brune PetscFunctionReturn(0); 20355fe1f584SPeter Brune } 20365fe1f584SPeter Brune 20375fe1f584SPeter Brune 20385fe1f584SPeter Brune 20395fe1f584SPeter Brune #undef __FUNCT__ 204047c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy" 204147c6ae99SBarry Smith /*@C 204247c6ae99SBarry Smith DMRefineHierarchy - Refines a DM object, all levels at once 204347c6ae99SBarry Smith 204447c6ae99SBarry Smith Collective on DM 204547c6ae99SBarry Smith 204647c6ae99SBarry Smith Input Parameter: 204747c6ae99SBarry Smith + dm - the DM object 204847c6ae99SBarry Smith - nlevels - the number of levels of refinement 204947c6ae99SBarry Smith 205047c6ae99SBarry Smith Output Parameter: 205147c6ae99SBarry Smith . dmf - the refined DM hierarchy 205247c6ae99SBarry Smith 205347c6ae99SBarry Smith Level: developer 205447c6ae99SBarry Smith 2055e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation() 205647c6ae99SBarry Smith 205747c6ae99SBarry Smith @*/ 20587087cfbeSBarry Smith PetscErrorCode DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[]) 205947c6ae99SBarry Smith { 206047c6ae99SBarry Smith PetscErrorCode ierr; 206147c6ae99SBarry Smith 206247c6ae99SBarry Smith PetscFunctionBegin; 2063171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 2064ce94432eSBarry Smith if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative"); 206547c6ae99SBarry Smith if (nlevels == 0) PetscFunctionReturn(0); 206647c6ae99SBarry Smith if (dm->ops->refinehierarchy) { 206747c6ae99SBarry Smith ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr); 206847c6ae99SBarry Smith } else if (dm->ops->refine) { 206947c6ae99SBarry Smith PetscInt i; 207047c6ae99SBarry Smith 2071ce94432eSBarry Smith ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr); 207247c6ae99SBarry Smith for (i=1; i<nlevels; i++) { 2073ce94432eSBarry Smith ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr); 207447c6ae99SBarry Smith } 2075ce94432eSBarry Smith } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet"); 207647c6ae99SBarry Smith PetscFunctionReturn(0); 207747c6ae99SBarry Smith } 207847c6ae99SBarry Smith 207947c6ae99SBarry Smith #undef __FUNCT__ 208047c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy" 208147c6ae99SBarry Smith /*@C 208247c6ae99SBarry Smith DMCoarsenHierarchy - Coarsens a DM object, all levels at once 208347c6ae99SBarry Smith 208447c6ae99SBarry Smith Collective on DM 208547c6ae99SBarry Smith 208647c6ae99SBarry Smith Input Parameter: 208747c6ae99SBarry Smith + dm - the DM object 208847c6ae99SBarry Smith - nlevels - the number of levels of coarsening 208947c6ae99SBarry Smith 209047c6ae99SBarry Smith Output Parameter: 209147c6ae99SBarry Smith . dmc - the coarsened DM hierarchy 209247c6ae99SBarry Smith 209347c6ae99SBarry Smith Level: developer 209447c6ae99SBarry Smith 2095e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation() 209647c6ae99SBarry Smith 209747c6ae99SBarry Smith @*/ 20987087cfbeSBarry Smith PetscErrorCode DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[]) 209947c6ae99SBarry Smith { 210047c6ae99SBarry Smith PetscErrorCode ierr; 210147c6ae99SBarry Smith 210247c6ae99SBarry Smith PetscFunctionBegin; 2103171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 2104ce94432eSBarry Smith if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative"); 210547c6ae99SBarry Smith if (nlevels == 0) PetscFunctionReturn(0); 210647c6ae99SBarry Smith PetscValidPointer(dmc,3); 210747c6ae99SBarry Smith if (dm->ops->coarsenhierarchy) { 210847c6ae99SBarry Smith ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr); 210947c6ae99SBarry Smith } else if (dm->ops->coarsen) { 211047c6ae99SBarry Smith PetscInt i; 211147c6ae99SBarry Smith 2112ce94432eSBarry Smith ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr); 211347c6ae99SBarry Smith for (i=1; i<nlevels; i++) { 2114ce94432eSBarry Smith ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr); 211547c6ae99SBarry Smith } 2116ce94432eSBarry Smith } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet"); 211747c6ae99SBarry Smith PetscFunctionReturn(0); 211847c6ae99SBarry Smith } 211947c6ae99SBarry Smith 212047c6ae99SBarry Smith #undef __FUNCT__ 2121e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates" 212247c6ae99SBarry Smith /*@ 2123e727c939SJed Brown DMCreateAggregates - Gets the aggregates that map between 212447c6ae99SBarry Smith grids associated with two DMs. 212547c6ae99SBarry Smith 212647c6ae99SBarry Smith Collective on DM 212747c6ae99SBarry Smith 212847c6ae99SBarry Smith Input Parameters: 212947c6ae99SBarry Smith + dmc - the coarse grid DM 213047c6ae99SBarry Smith - dmf - the fine grid DM 213147c6ae99SBarry Smith 213247c6ae99SBarry Smith Output Parameters: 213347c6ae99SBarry Smith . rest - the restriction matrix (transpose of the projection matrix) 213447c6ae99SBarry Smith 213547c6ae99SBarry Smith Level: intermediate 213647c6ae99SBarry Smith 213747c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid 213847c6ae99SBarry Smith 2139e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation() 214047c6ae99SBarry Smith @*/ 2141e727c939SJed Brown PetscErrorCode DMCreateAggregates(DM dmc, DM dmf, Mat *rest) 214247c6ae99SBarry Smith { 214347c6ae99SBarry Smith PetscErrorCode ierr; 214447c6ae99SBarry Smith 214547c6ae99SBarry Smith PetscFunctionBegin; 2146171400e9SBarry Smith PetscValidHeaderSpecific(dmc,DM_CLASSID,1); 2147171400e9SBarry Smith PetscValidHeaderSpecific(dmf,DM_CLASSID,2); 214847c6ae99SBarry Smith ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr); 214947c6ae99SBarry Smith PetscFunctionReturn(0); 215047c6ae99SBarry Smith } 215147c6ae99SBarry Smith 215247c6ae99SBarry Smith #undef __FUNCT__ 21531a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy" 21541a266240SBarry Smith /*@C 21551a266240SBarry Smith DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed 21561a266240SBarry Smith 21571a266240SBarry Smith Not Collective 21581a266240SBarry Smith 21591a266240SBarry Smith Input Parameters: 21601a266240SBarry Smith + dm - the DM object 21611a266240SBarry Smith - destroy - the destroy function 21621a266240SBarry Smith 21631a266240SBarry Smith Level: intermediate 21641a266240SBarry Smith 2165e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext() 21661a266240SBarry Smith 2167f07f9ceaSJed Brown @*/ 21681a266240SBarry Smith PetscErrorCode DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**)) 21691a266240SBarry Smith { 21701a266240SBarry Smith PetscFunctionBegin; 2171171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 21721a266240SBarry Smith dm->ctxdestroy = destroy; 21731a266240SBarry Smith PetscFunctionReturn(0); 21741a266240SBarry Smith } 21751a266240SBarry Smith 21761a266240SBarry Smith #undef __FUNCT__ 21771b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext" 2178b07ff414SBarry Smith /*@ 21791b2093e4SBarry Smith DMSetApplicationContext - Set a user context into a DM object 218047c6ae99SBarry Smith 218147c6ae99SBarry Smith Not Collective 218247c6ae99SBarry Smith 218347c6ae99SBarry Smith Input Parameters: 218447c6ae99SBarry Smith + dm - the DM object 218547c6ae99SBarry Smith - ctx - the user context 218647c6ae99SBarry Smith 218747c6ae99SBarry Smith Level: intermediate 218847c6ae99SBarry Smith 2189e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext() 219047c6ae99SBarry Smith 219147c6ae99SBarry Smith @*/ 21921b2093e4SBarry Smith PetscErrorCode DMSetApplicationContext(DM dm,void *ctx) 219347c6ae99SBarry Smith { 219447c6ae99SBarry Smith PetscFunctionBegin; 2195171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 219647c6ae99SBarry Smith dm->ctx = ctx; 219747c6ae99SBarry Smith PetscFunctionReturn(0); 219847c6ae99SBarry Smith } 219947c6ae99SBarry Smith 220047c6ae99SBarry Smith #undef __FUNCT__ 22011b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext" 220247c6ae99SBarry Smith /*@ 22031b2093e4SBarry Smith DMGetApplicationContext - Gets a user context from a DM object 220447c6ae99SBarry Smith 220547c6ae99SBarry Smith Not Collective 220647c6ae99SBarry Smith 220747c6ae99SBarry Smith Input Parameter: 220847c6ae99SBarry Smith . dm - the DM object 220947c6ae99SBarry Smith 221047c6ae99SBarry Smith Output Parameter: 221147c6ae99SBarry Smith . ctx - the user context 221247c6ae99SBarry Smith 221347c6ae99SBarry Smith Level: intermediate 221447c6ae99SBarry Smith 2215e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext() 221647c6ae99SBarry Smith 221747c6ae99SBarry Smith @*/ 22181b2093e4SBarry Smith PetscErrorCode DMGetApplicationContext(DM dm,void *ctx) 221947c6ae99SBarry Smith { 222047c6ae99SBarry Smith PetscFunctionBegin; 2221171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 22221b2093e4SBarry Smith *(void**)ctx = dm->ctx; 222347c6ae99SBarry Smith PetscFunctionReturn(0); 222447c6ae99SBarry Smith } 222547c6ae99SBarry Smith 222647c6ae99SBarry Smith #undef __FUNCT__ 222708da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds" 222808da532bSDmitry Karpeev /*@C 222908da532bSDmitry Karpeev DMSetVariableBounds - sets a function to compute the the lower and upper bound vectors for SNESVI. 223008da532bSDmitry Karpeev 223108da532bSDmitry Karpeev Logically Collective on DM 223208da532bSDmitry Karpeev 223308da532bSDmitry Karpeev Input Parameter: 223408da532bSDmitry Karpeev + dm - the DM object 22350298fd71SBarry Smith - f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set) 223608da532bSDmitry Karpeev 223708da532bSDmitry Karpeev Level: intermediate 223808da532bSDmitry Karpeev 2239835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), 224008da532bSDmitry Karpeev DMSetJacobian() 224108da532bSDmitry Karpeev 224208da532bSDmitry Karpeev @*/ 224308da532bSDmitry Karpeev PetscErrorCode DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec)) 224408da532bSDmitry Karpeev { 224508da532bSDmitry Karpeev PetscFunctionBegin; 224608da532bSDmitry Karpeev dm->ops->computevariablebounds = f; 224708da532bSDmitry Karpeev PetscFunctionReturn(0); 224808da532bSDmitry Karpeev } 224908da532bSDmitry Karpeev 225008da532bSDmitry Karpeev #undef __FUNCT__ 225108da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds" 225208da532bSDmitry Karpeev /*@ 225308da532bSDmitry Karpeev DMHasVariableBounds - does the DM object have a variable bounds function? 225408da532bSDmitry Karpeev 225508da532bSDmitry Karpeev Not Collective 225608da532bSDmitry Karpeev 225708da532bSDmitry Karpeev Input Parameter: 225808da532bSDmitry Karpeev . dm - the DM object to destroy 225908da532bSDmitry Karpeev 226008da532bSDmitry Karpeev Output Parameter: 226108da532bSDmitry Karpeev . flg - PETSC_TRUE if the variable bounds function exists 226208da532bSDmitry Karpeev 226308da532bSDmitry Karpeev Level: developer 226408da532bSDmitry Karpeev 226574e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext() 226608da532bSDmitry Karpeev 226708da532bSDmitry Karpeev @*/ 226808da532bSDmitry Karpeev PetscErrorCode DMHasVariableBounds(DM dm,PetscBool *flg) 226908da532bSDmitry Karpeev { 227008da532bSDmitry Karpeev PetscFunctionBegin; 227108da532bSDmitry Karpeev *flg = (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE; 227208da532bSDmitry Karpeev PetscFunctionReturn(0); 227308da532bSDmitry Karpeev } 227408da532bSDmitry Karpeev 227508da532bSDmitry Karpeev #undef __FUNCT__ 227608da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds" 227708da532bSDmitry Karpeev /*@C 227808da532bSDmitry Karpeev DMComputeVariableBounds - compute variable bounds used by SNESVI. 227908da532bSDmitry Karpeev 228008da532bSDmitry Karpeev Logically Collective on DM 228108da532bSDmitry Karpeev 228208da532bSDmitry Karpeev Input Parameters: 228308da532bSDmitry Karpeev + dm - the DM object to destroy 228408da532bSDmitry Karpeev - x - current solution at which the bounds are computed 228508da532bSDmitry Karpeev 228608da532bSDmitry Karpeev Output parameters: 228708da532bSDmitry Karpeev + xl - lower bound 228808da532bSDmitry Karpeev - xu - upper bound 228908da532bSDmitry Karpeev 229008da532bSDmitry Karpeev Level: intermediate 229108da532bSDmitry Karpeev 229274e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext() 229308da532bSDmitry Karpeev 229408da532bSDmitry Karpeev @*/ 229508da532bSDmitry Karpeev PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu) 229608da532bSDmitry Karpeev { 229708da532bSDmitry Karpeev PetscErrorCode ierr; 22985fd66863SKarl Rupp 229908da532bSDmitry Karpeev PetscFunctionBegin; 230008da532bSDmitry Karpeev PetscValidHeaderSpecific(xl,VEC_CLASSID,2); 230108da532bSDmitry Karpeev PetscValidHeaderSpecific(xu,VEC_CLASSID,2); 230208da532bSDmitry Karpeev if (dm->ops->computevariablebounds) { 230308da532bSDmitry Karpeev ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr); 23048865f1eaSKarl Rupp } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds."); 230508da532bSDmitry Karpeev PetscFunctionReturn(0); 230608da532bSDmitry Karpeev } 230708da532bSDmitry Karpeev 230808da532bSDmitry Karpeev #undef __FUNCT__ 2309b0ae01b7SPeter Brune #define __FUNCT__ "DMHasColoring" 2310b0ae01b7SPeter Brune /*@ 2311b0ae01b7SPeter Brune DMHasColoring - does the DM object have a method of providing a coloring? 2312b0ae01b7SPeter Brune 2313b0ae01b7SPeter Brune Not Collective 2314b0ae01b7SPeter Brune 2315b0ae01b7SPeter Brune Input Parameter: 2316b0ae01b7SPeter Brune . dm - the DM object 2317b0ae01b7SPeter Brune 2318b0ae01b7SPeter Brune Output Parameter: 2319b0ae01b7SPeter Brune . flg - PETSC_TRUE if the DM has facilities for DMCreateColoring(). 2320b0ae01b7SPeter Brune 2321b0ae01b7SPeter Brune Level: developer 2322b0ae01b7SPeter Brune 2323b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring() 2324b0ae01b7SPeter Brune 2325b0ae01b7SPeter Brune @*/ 2326b0ae01b7SPeter Brune PetscErrorCode DMHasColoring(DM dm,PetscBool *flg) 2327b0ae01b7SPeter Brune { 2328b0ae01b7SPeter Brune PetscFunctionBegin; 2329b0ae01b7SPeter Brune *flg = (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE; 2330b0ae01b7SPeter Brune PetscFunctionReturn(0); 2331b0ae01b7SPeter Brune } 2332b0ae01b7SPeter Brune 2333b0ae01b7SPeter Brune #undef __FUNCT__ 233408da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec" 2335748fac09SDmitry Karpeev /*@C 233608da532bSDmitry Karpeev DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear. 233708da532bSDmitry Karpeev 233808da532bSDmitry Karpeev Collective on DM 233908da532bSDmitry Karpeev 234008da532bSDmitry Karpeev Input Parameter: 234108da532bSDmitry Karpeev + dm - the DM object 23420298fd71SBarry Smith - x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems. 234308da532bSDmitry Karpeev 234408da532bSDmitry Karpeev Level: developer 234508da532bSDmitry Karpeev 234674e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext() 234708da532bSDmitry Karpeev 234808da532bSDmitry Karpeev @*/ 234908da532bSDmitry Karpeev PetscErrorCode DMSetVec(DM dm,Vec x) 235008da532bSDmitry Karpeev { 235108da532bSDmitry Karpeev PetscErrorCode ierr; 23525fd66863SKarl Rupp 235308da532bSDmitry Karpeev PetscFunctionBegin; 235408da532bSDmitry Karpeev if (x) { 235508da532bSDmitry Karpeev if (!dm->x) { 235608da532bSDmitry Karpeev ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr); 235708da532bSDmitry Karpeev } 235808da532bSDmitry Karpeev ierr = VecCopy(x,dm->x);CHKERRQ(ierr); 23598865f1eaSKarl Rupp } else if (dm->x) { 236008da532bSDmitry Karpeev ierr = VecDestroy(&dm->x);CHKERRQ(ierr); 236108da532bSDmitry Karpeev } 236208da532bSDmitry Karpeev PetscFunctionReturn(0); 236308da532bSDmitry Karpeev } 236408da532bSDmitry Karpeev 23650298fd71SBarry Smith PetscFunctionList DMList = NULL; 2366264ace61SBarry Smith PetscBool DMRegisterAllCalled = PETSC_FALSE; 2367264ace61SBarry Smith 2368264ace61SBarry Smith #undef __FUNCT__ 2369264ace61SBarry Smith #define __FUNCT__ "DMSetType" 2370264ace61SBarry Smith /*@C 2371264ace61SBarry Smith DMSetType - Builds a DM, for a particular DM implementation. 2372264ace61SBarry Smith 2373264ace61SBarry Smith Collective on DM 2374264ace61SBarry Smith 2375264ace61SBarry Smith Input Parameters: 2376264ace61SBarry Smith + dm - The DM object 2377264ace61SBarry Smith - method - The name of the DM type 2378264ace61SBarry Smith 2379264ace61SBarry Smith Options Database Key: 2380264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types 2381264ace61SBarry Smith 2382264ace61SBarry Smith Notes: 2383e1589f56SBarry Smith See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D). 2384264ace61SBarry Smith 2385264ace61SBarry Smith Level: intermediate 2386264ace61SBarry Smith 2387264ace61SBarry Smith .keywords: DM, set, type 2388264ace61SBarry Smith .seealso: DMGetType(), DMCreate() 2389264ace61SBarry Smith @*/ 239019fd82e9SBarry Smith PetscErrorCode DMSetType(DM dm, DMType method) 2391264ace61SBarry Smith { 2392264ace61SBarry Smith PetscErrorCode (*r)(DM); 2393264ace61SBarry Smith PetscBool match; 2394264ace61SBarry Smith PetscErrorCode ierr; 2395264ace61SBarry Smith 2396264ace61SBarry Smith PetscFunctionBegin; 2397264ace61SBarry Smith PetscValidHeaderSpecific(dm, DM_CLASSID,1); 2398251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr); 2399264ace61SBarry Smith if (match) PetscFunctionReturn(0); 2400264ace61SBarry Smith 2401607a6623SBarry Smith if (!DMRegisterAllCalled) {ierr = DMRegisterAll();CHKERRQ(ierr);} 24021c9cd337SJed Brown ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr); 2403ce94432eSBarry Smith if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method); 2404264ace61SBarry Smith 2405264ace61SBarry Smith if (dm->ops->destroy) { 2406264ace61SBarry Smith ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr); 24070298fd71SBarry Smith dm->ops->destroy = NULL; 2408264ace61SBarry Smith } 2409264ace61SBarry Smith ierr = (*r)(dm);CHKERRQ(ierr); 2410264ace61SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr); 2411264ace61SBarry Smith PetscFunctionReturn(0); 2412264ace61SBarry Smith } 2413264ace61SBarry Smith 2414264ace61SBarry Smith #undef __FUNCT__ 2415264ace61SBarry Smith #define __FUNCT__ "DMGetType" 2416264ace61SBarry Smith /*@C 2417264ace61SBarry Smith DMGetType - Gets the DM type name (as a string) from the DM. 2418264ace61SBarry Smith 2419264ace61SBarry Smith Not Collective 2420264ace61SBarry Smith 2421264ace61SBarry Smith Input Parameter: 2422264ace61SBarry Smith . dm - The DM 2423264ace61SBarry Smith 2424264ace61SBarry Smith Output Parameter: 2425264ace61SBarry Smith . type - The DM type name 2426264ace61SBarry Smith 2427264ace61SBarry Smith Level: intermediate 2428264ace61SBarry Smith 2429264ace61SBarry Smith .keywords: DM, get, type, name 2430264ace61SBarry Smith .seealso: DMSetType(), DMCreate() 2431264ace61SBarry Smith @*/ 243219fd82e9SBarry Smith PetscErrorCode DMGetType(DM dm, DMType *type) 2433264ace61SBarry Smith { 2434264ace61SBarry Smith PetscErrorCode ierr; 2435264ace61SBarry Smith 2436264ace61SBarry Smith PetscFunctionBegin; 2437264ace61SBarry Smith PetscValidHeaderSpecific(dm, DM_CLASSID,1); 2438264ace61SBarry Smith PetscValidCharPointer(type,2); 2439264ace61SBarry Smith if (!DMRegisterAllCalled) { 2440607a6623SBarry Smith ierr = DMRegisterAll();CHKERRQ(ierr); 2441264ace61SBarry Smith } 2442264ace61SBarry Smith *type = ((PetscObject)dm)->type_name; 2443264ace61SBarry Smith PetscFunctionReturn(0); 2444264ace61SBarry Smith } 2445264ace61SBarry Smith 244667a56275SMatthew G Knepley #undef __FUNCT__ 244767a56275SMatthew G Knepley #define __FUNCT__ "DMConvert" 244867a56275SMatthew G Knepley /*@C 244967a56275SMatthew G Knepley DMConvert - Converts a DM to another DM, either of the same or different type. 245067a56275SMatthew G Knepley 245167a56275SMatthew G Knepley Collective on DM 245267a56275SMatthew G Knepley 245367a56275SMatthew G Knepley Input Parameters: 245467a56275SMatthew G Knepley + dm - the DM 245567a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type) 245667a56275SMatthew G Knepley 245767a56275SMatthew G Knepley Output Parameter: 245867a56275SMatthew G Knepley . M - pointer to new DM 245967a56275SMatthew G Knepley 246067a56275SMatthew G Knepley Notes: 246167a56275SMatthew G Knepley Cannot be used to convert a sequential DM to parallel or parallel to sequential, 246267a56275SMatthew G Knepley the MPI communicator of the generated DM is always the same as the communicator 246367a56275SMatthew G Knepley of the input DM. 246467a56275SMatthew G Knepley 246567a56275SMatthew G Knepley Level: intermediate 246667a56275SMatthew G Knepley 246767a56275SMatthew G Knepley .seealso: DMCreate() 246867a56275SMatthew G Knepley @*/ 246919fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M) 247067a56275SMatthew G Knepley { 247167a56275SMatthew G Knepley DM B; 247267a56275SMatthew G Knepley char convname[256]; 247367a56275SMatthew G Knepley PetscBool sametype, issame; 247467a56275SMatthew G Knepley PetscErrorCode ierr; 247567a56275SMatthew G Knepley 247667a56275SMatthew G Knepley PetscFunctionBegin; 247767a56275SMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 247867a56275SMatthew G Knepley PetscValidType(dm,1); 247967a56275SMatthew G Knepley PetscValidPointer(M,3); 2480251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr); 248167a56275SMatthew G Knepley ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); 248267a56275SMatthew G Knepley { 24830298fd71SBarry Smith PetscErrorCode (*conv)(DM, DMType, DM*) = NULL; 248467a56275SMatthew G Knepley 248567a56275SMatthew G Knepley /* 248667a56275SMatthew G Knepley Order of precedence: 248767a56275SMatthew G Knepley 1) See if a specialized converter is known to the current DM. 248867a56275SMatthew G Knepley 2) See if a specialized converter is known to the desired DM class. 248967a56275SMatthew G Knepley 3) See if a good general converter is registered for the desired class 249067a56275SMatthew G Knepley 4) See if a good general converter is known for the current matrix. 249167a56275SMatthew G Knepley 5) Use a really basic converter. 249267a56275SMatthew G Knepley */ 249367a56275SMatthew G Knepley 249467a56275SMatthew G Knepley /* 1) See if a specialized converter is known to the current DM and the desired class */ 249567a56275SMatthew G Knepley ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr); 249667a56275SMatthew G Knepley ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr); 249767a56275SMatthew G Knepley ierr = PetscStrcat(convname,"_");CHKERRQ(ierr); 249867a56275SMatthew G Knepley ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr); 249967a56275SMatthew G Knepley ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr); 25000005d66cSJed Brown ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr); 250167a56275SMatthew G Knepley if (conv) goto foundconv; 250267a56275SMatthew G Knepley 250367a56275SMatthew G Knepley /* 2) See if a specialized converter is known to the desired DM class. */ 250482f516ccSBarry Smith ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr); 250567a56275SMatthew G Knepley ierr = DMSetType(B, newtype);CHKERRQ(ierr); 250667a56275SMatthew G Knepley ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr); 250767a56275SMatthew G Knepley ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr); 250867a56275SMatthew G Knepley ierr = PetscStrcat(convname,"_");CHKERRQ(ierr); 250967a56275SMatthew G Knepley ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr); 251067a56275SMatthew G Knepley ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr); 25110005d66cSJed Brown ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr); 251267a56275SMatthew G Knepley if (conv) { 2513fcfd50ebSBarry Smith ierr = DMDestroy(&B);CHKERRQ(ierr); 251467a56275SMatthew G Knepley goto foundconv; 251567a56275SMatthew G Knepley } 251667a56275SMatthew G Knepley 251767a56275SMatthew G Knepley #if 0 251867a56275SMatthew G Knepley /* 3) See if a good general converter is registered for the desired class */ 251967a56275SMatthew G Knepley conv = B->ops->convertfrom; 2520fcfd50ebSBarry Smith ierr = DMDestroy(&B);CHKERRQ(ierr); 252167a56275SMatthew G Knepley if (conv) goto foundconv; 252267a56275SMatthew G Knepley 252367a56275SMatthew G Knepley /* 4) See if a good general converter is known for the current matrix */ 252467a56275SMatthew G Knepley if (dm->ops->convert) { 252567a56275SMatthew G Knepley conv = dm->ops->convert; 252667a56275SMatthew G Knepley } 252767a56275SMatthew G Knepley if (conv) goto foundconv; 252867a56275SMatthew G Knepley #endif 252967a56275SMatthew G Knepley 253067a56275SMatthew G Knepley /* 5) Use a really basic converter. */ 253182f516ccSBarry Smith SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype); 253267a56275SMatthew G Knepley 253367a56275SMatthew G Knepley foundconv: 253467a56275SMatthew G Knepley ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr); 253567a56275SMatthew G Knepley ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr); 253667a56275SMatthew G Knepley ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr); 253767a56275SMatthew G Knepley } 253867a56275SMatthew G Knepley ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr); 253967a56275SMatthew G Knepley PetscFunctionReturn(0); 254067a56275SMatthew G Knepley } 2541264ace61SBarry Smith 2542264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/ 2543264ace61SBarry Smith 2544264ace61SBarry Smith #undef __FUNCT__ 2545264ace61SBarry Smith #define __FUNCT__ "DMRegister" 2546264ace61SBarry Smith /*@C 25471c84c290SBarry Smith DMRegister - Adds a new DM component implementation 25481c84c290SBarry Smith 25491c84c290SBarry Smith Not Collective 25501c84c290SBarry Smith 25511c84c290SBarry Smith Input Parameters: 25521c84c290SBarry Smith + name - The name of a new user-defined creation routine 25531c84c290SBarry Smith - create_func - The creation routine itself 25541c84c290SBarry Smith 25551c84c290SBarry Smith Notes: 25561c84c290SBarry Smith DMRegister() may be called multiple times to add several user-defined DMs 25571c84c290SBarry Smith 25581c84c290SBarry Smith 25591c84c290SBarry Smith Sample usage: 25601c84c290SBarry Smith .vb 2561bdf89e91SBarry Smith DMRegister("my_da", MyDMCreate); 25621c84c290SBarry Smith .ve 25631c84c290SBarry Smith 25641c84c290SBarry Smith Then, your DM type can be chosen with the procedural interface via 25651c84c290SBarry Smith .vb 25661c84c290SBarry Smith DMCreate(MPI_Comm, DM *); 25671c84c290SBarry Smith DMSetType(DM,"my_da"); 25681c84c290SBarry Smith .ve 25691c84c290SBarry Smith or at runtime via the option 25701c84c290SBarry Smith .vb 25711c84c290SBarry Smith -da_type my_da 25721c84c290SBarry Smith .ve 2573264ace61SBarry Smith 2574264ace61SBarry Smith Level: advanced 25751c84c290SBarry Smith 25761c84c290SBarry Smith .keywords: DM, register 2577bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy() 25781c84c290SBarry Smith 2579264ace61SBarry Smith @*/ 2580bdf89e91SBarry Smith PetscErrorCode DMRegister(const char sname[],PetscErrorCode (*function)(DM)) 2581264ace61SBarry Smith { 2582264ace61SBarry Smith PetscErrorCode ierr; 2583264ace61SBarry Smith 2584264ace61SBarry Smith PetscFunctionBegin; 2585a240a19fSJed Brown ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr); 2586264ace61SBarry Smith PetscFunctionReturn(0); 2587264ace61SBarry Smith } 2588264ace61SBarry Smith 2589264ace61SBarry Smith 2590264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/ 2591264ace61SBarry Smith #undef __FUNCT__ 2592264ace61SBarry Smith #define __FUNCT__ "DMRegisterDestroy" 2593264ace61SBarry Smith /*@C 2594607a6623SBarry Smith DMRegisterDestroy - Frees the list of DM methods that were registered by DMRegister(). 2595264ace61SBarry Smith 2596264ace61SBarry Smith Not Collective 2597264ace61SBarry Smith 2598264ace61SBarry Smith Level: advanced 2599264ace61SBarry Smith 2600264ace61SBarry Smith .keywords: DM, register, destroy 2601607a6623SBarry Smith .seealso: DMRegister(), DMRegisterAll() 2602264ace61SBarry Smith @*/ 26037087cfbeSBarry Smith PetscErrorCode DMRegisterDestroy(void) 2604264ace61SBarry Smith { 2605264ace61SBarry Smith PetscErrorCode ierr; 2606264ace61SBarry Smith 2607264ace61SBarry Smith PetscFunctionBegin; 2608140e18c1SBarry Smith ierr = PetscFunctionListDestroy(&DMList);CHKERRQ(ierr); 2609264ace61SBarry Smith DMRegisterAllCalled = PETSC_FALSE; 2610264ace61SBarry Smith PetscFunctionReturn(0); 2611264ace61SBarry Smith } 261223f975d1SBarry Smith 2613b859378eSBarry Smith #undef __FUNCT__ 2614b859378eSBarry Smith #define __FUNCT__ "DMLoad" 2615b859378eSBarry Smith /*@C 261655849f57SBarry Smith DMLoad - Loads a DM that has been stored in binary with DMView(). 2617b859378eSBarry Smith 2618b859378eSBarry Smith Collective on PetscViewer 2619b859378eSBarry Smith 2620b859378eSBarry Smith Input Parameters: 2621b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or 2622b859378eSBarry Smith some related function before a call to DMLoad(). 2623b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or 2624b859378eSBarry Smith HDF5 file viewer, obtained from PetscViewerHDF5Open() 2625b859378eSBarry Smith 2626b859378eSBarry Smith Level: intermediate 2627b859378eSBarry Smith 2628b859378eSBarry Smith Notes: 262955849f57SBarry Smith The type is determined by the data in the file, any type set into the DM before this call is ignored. 2630b859378eSBarry Smith 2631b859378eSBarry Smith Notes for advanced users: 2632b859378eSBarry Smith Most users should not need to know the details of the binary storage 2633b859378eSBarry Smith format, since DMLoad() and DMView() completely hide these details. 2634b859378eSBarry Smith But for anyone who's interested, the standard binary matrix storage 2635b859378eSBarry Smith format is 2636b859378eSBarry Smith .vb 2637b859378eSBarry Smith has not yet been determined 2638b859378eSBarry Smith .ve 2639b859378eSBarry Smith 2640b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad() 2641b859378eSBarry Smith @*/ 2642b859378eSBarry Smith PetscErrorCode DMLoad(DM newdm, PetscViewer viewer) 2643b859378eSBarry Smith { 2644b859378eSBarry Smith PetscErrorCode ierr; 264532c0f0efSBarry Smith PetscBool isbinary; 264655849f57SBarry Smith PetscInt classid; 264732c0f0efSBarry Smith char type[256]; 2648b859378eSBarry Smith 2649b859378eSBarry Smith PetscFunctionBegin; 2650b859378eSBarry Smith PetscValidHeaderSpecific(newdm,DM_CLASSID,1); 2651b859378eSBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 265232c0f0efSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 265332c0f0efSBarry Smith if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()"); 2654b859378eSBarry Smith 265532c0f0efSBarry Smith ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr); 2656ce94432eSBarry Smith if (classid != DM_FILE_CLASSID) SETERRQ(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file"); 265732c0f0efSBarry Smith ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr); 265832c0f0efSBarry Smith ierr = DMSetType(newdm, type);CHKERRQ(ierr); 26592d53ad75SBarry Smith if (newdm->ops->load) { 2660b859378eSBarry Smith ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr); 26612d53ad75SBarry Smith } 2662b859378eSBarry Smith PetscFunctionReturn(0); 2663b859378eSBarry Smith } 2664b859378eSBarry Smith 26657da65231SMatthew G Knepley /******************************** FEM Support **********************************/ 26667da65231SMatthew G Knepley 26677da65231SMatthew G Knepley #undef __FUNCT__ 26687da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector" 2669a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[]) 2670a6dfd86eSKarl Rupp { 26711d47ebbbSSatish Balay PetscInt f; 26721b30c384SMatthew G Knepley PetscErrorCode ierr; 26731b30c384SMatthew G Knepley 26747da65231SMatthew G Knepley PetscFunctionBegin; 267574778d6cSJed Brown ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr); 26761d47ebbbSSatish Balay for (f = 0; f < len; ++f) { 267774778d6cSJed Brown ierr = PetscPrintf(PETSC_COMM_SELF, " | %G |\n", PetscRealPart(x[f]));CHKERRQ(ierr); 26787da65231SMatthew G Knepley } 26797da65231SMatthew G Knepley PetscFunctionReturn(0); 26807da65231SMatthew G Knepley } 26817da65231SMatthew G Knepley 26827da65231SMatthew G Knepley #undef __FUNCT__ 26837da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix" 2684a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[]) 2685a6dfd86eSKarl Rupp { 26861b30c384SMatthew G Knepley PetscInt f, g; 26877da65231SMatthew G Knepley PetscErrorCode ierr; 26887da65231SMatthew G Knepley 26897da65231SMatthew G Knepley PetscFunctionBegin; 269074778d6cSJed Brown ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr); 26911d47ebbbSSatish Balay for (f = 0; f < rows; ++f) { 269274778d6cSJed Brown ierr = PetscPrintf(PETSC_COMM_SELF, " |");CHKERRQ(ierr); 26931d47ebbbSSatish Balay for (g = 0; g < cols; ++g) { 269474778d6cSJed Brown ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5G", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr); 26957da65231SMatthew G Knepley } 269674778d6cSJed Brown ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr); 26977da65231SMatthew G Knepley } 26987da65231SMatthew G Knepley PetscFunctionReturn(0); 26997da65231SMatthew G Knepley } 2700e7c4fc90SDmitry Karpeev 2701970e74d5SMatthew G Knepley #undef __FUNCT__ 270288ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection" 270388ed4aceSMatthew G Knepley /*@ 270488ed4aceSMatthew G Knepley DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM. 270588ed4aceSMatthew G Knepley 270688ed4aceSMatthew G Knepley Input Parameter: 270788ed4aceSMatthew G Knepley . dm - The DM 270888ed4aceSMatthew G Knepley 270988ed4aceSMatthew G Knepley Output Parameter: 271088ed4aceSMatthew G Knepley . section - The PetscSection 271188ed4aceSMatthew G Knepley 271288ed4aceSMatthew G Knepley Level: intermediate 271388ed4aceSMatthew G Knepley 271488ed4aceSMatthew G Knepley Note: This gets a borrowed reference, so the user should not destroy this PetscSection. 271588ed4aceSMatthew G Knepley 271688ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection() 271788ed4aceSMatthew G Knepley @*/ 27180adebc6cSBarry Smith PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section) 27190adebc6cSBarry Smith { 272088ed4aceSMatthew G Knepley PetscFunctionBegin; 272188ed4aceSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 272288ed4aceSMatthew G Knepley PetscValidPointer(section, 2); 272388ed4aceSMatthew G Knepley *section = dm->defaultSection; 272488ed4aceSMatthew G Knepley PetscFunctionReturn(0); 272588ed4aceSMatthew G Knepley } 272688ed4aceSMatthew G Knepley 272788ed4aceSMatthew G Knepley #undef __FUNCT__ 272888ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection" 272988ed4aceSMatthew G Knepley /*@ 273088ed4aceSMatthew G Knepley DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM. 273188ed4aceSMatthew G Knepley 273288ed4aceSMatthew G Knepley Input Parameters: 273388ed4aceSMatthew G Knepley + dm - The DM 273488ed4aceSMatthew G Knepley - section - The PetscSection 273588ed4aceSMatthew G Knepley 273688ed4aceSMatthew G Knepley Level: intermediate 273788ed4aceSMatthew G Knepley 273888ed4aceSMatthew G Knepley Note: Any existing Section will be destroyed 273988ed4aceSMatthew G Knepley 274088ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection() 274188ed4aceSMatthew G Knepley @*/ 27420adebc6cSBarry Smith PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section) 27430adebc6cSBarry Smith { 2744af122d2aSMatthew G Knepley PetscInt numFields; 2745af122d2aSMatthew G Knepley PetscInt f; 274688ed4aceSMatthew G Knepley PetscErrorCode ierr; 274788ed4aceSMatthew G Knepley 274888ed4aceSMatthew G Knepley PetscFunctionBegin; 274988ed4aceSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 27501d799100SJed Brown PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2); 27511d799100SJed Brown ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr); 275288ed4aceSMatthew G Knepley ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr); 275388ed4aceSMatthew G Knepley dm->defaultSection = section; 2754af122d2aSMatthew G Knepley ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr); 2755af122d2aSMatthew G Knepley if (numFields) { 2756af122d2aSMatthew G Knepley ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr); 2757af122d2aSMatthew G Knepley for (f = 0; f < numFields; ++f) { 2758af122d2aSMatthew G Knepley const char *name; 2759af122d2aSMatthew G Knepley 2760af122d2aSMatthew G Knepley ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr); 2761af122d2aSMatthew G Knepley ierr = PetscObjectSetName(dm->fields[f], name);CHKERRQ(ierr); 2762af122d2aSMatthew G Knepley } 2763af122d2aSMatthew G Knepley } 27641d799100SJed Brown /* The global section will be rebuilt in the next call to DMGetDefaultGlobalSection(). */ 27651d799100SJed Brown ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr); 276688ed4aceSMatthew G Knepley PetscFunctionReturn(0); 276788ed4aceSMatthew G Knepley } 276888ed4aceSMatthew G Knepley 276988ed4aceSMatthew G Knepley #undef __FUNCT__ 277088ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection" 277188ed4aceSMatthew G Knepley /*@ 277288ed4aceSMatthew G Knepley DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM. 277388ed4aceSMatthew G Knepley 27748b1ab98fSJed Brown Collective on DM 27758b1ab98fSJed Brown 277688ed4aceSMatthew G Knepley Input Parameter: 277788ed4aceSMatthew G Knepley . dm - The DM 277888ed4aceSMatthew G Knepley 277988ed4aceSMatthew G Knepley Output Parameter: 278088ed4aceSMatthew G Knepley . section - The PetscSection 278188ed4aceSMatthew G Knepley 278288ed4aceSMatthew G Knepley Level: intermediate 278388ed4aceSMatthew G Knepley 278488ed4aceSMatthew G Knepley Note: This gets a borrowed reference, so the user should not destroy this PetscSection. 278588ed4aceSMatthew G Knepley 278688ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection() 278788ed4aceSMatthew G Knepley @*/ 27880adebc6cSBarry Smith PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section) 27890adebc6cSBarry Smith { 279088ed4aceSMatthew G Knepley PetscErrorCode ierr; 279188ed4aceSMatthew G Knepley 279288ed4aceSMatthew G Knepley PetscFunctionBegin; 279388ed4aceSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 279488ed4aceSMatthew G Knepley PetscValidPointer(section, 2); 279588ed4aceSMatthew G Knepley if (!dm->defaultGlobalSection) { 279682f516ccSBarry Smith if (!dm->defaultSection || !dm->sf) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection and PetscSF in order to create a global PetscSection"); 2797b21d0597SMatthew G Knepley ierr = PetscSectionCreateGlobalSection(dm->defaultSection, dm->sf, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr); 2798ce94432eSBarry Smith ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm),dm->defaultGlobalSection,&dm->map);CHKERRQ(ierr); 279988ed4aceSMatthew G Knepley } 280088ed4aceSMatthew G Knepley *section = dm->defaultGlobalSection; 280188ed4aceSMatthew G Knepley PetscFunctionReturn(0); 280288ed4aceSMatthew G Knepley } 280388ed4aceSMatthew G Knepley 280488ed4aceSMatthew G Knepley #undef __FUNCT__ 2805b21d0597SMatthew G Knepley #define __FUNCT__ "DMSetDefaultGlobalSection" 2806b21d0597SMatthew G Knepley /*@ 2807b21d0597SMatthew G Knepley DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM. 2808b21d0597SMatthew G Knepley 2809b21d0597SMatthew G Knepley Input Parameters: 2810b21d0597SMatthew G Knepley + dm - The DM 28115080bbdbSMatthew G Knepley - section - The PetscSection, or NULL 2812b21d0597SMatthew G Knepley 2813b21d0597SMatthew G Knepley Level: intermediate 2814b21d0597SMatthew G Knepley 2815b21d0597SMatthew G Knepley Note: Any existing Section will be destroyed 2816b21d0597SMatthew G Knepley 2817b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection() 2818b21d0597SMatthew G Knepley @*/ 28190adebc6cSBarry Smith PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section) 28200adebc6cSBarry Smith { 2821b21d0597SMatthew G Knepley PetscErrorCode ierr; 2822b21d0597SMatthew G Knepley 2823b21d0597SMatthew G Knepley PetscFunctionBegin; 2824b21d0597SMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 28255080bbdbSMatthew G Knepley if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2); 28261d799100SJed Brown ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr); 2827b21d0597SMatthew G Knepley ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr); 2828b21d0597SMatthew G Knepley dm->defaultGlobalSection = section; 2829b21d0597SMatthew G Knepley PetscFunctionReturn(0); 2830b21d0597SMatthew G Knepley } 2831b21d0597SMatthew G Knepley 2832b21d0597SMatthew G Knepley #undef __FUNCT__ 283388ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF" 283488ed4aceSMatthew G Knepley /*@ 283588ed4aceSMatthew G Knepley DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set, 283688ed4aceSMatthew G Knepley it is created from the default PetscSection layouts in the DM. 283788ed4aceSMatthew G Knepley 283888ed4aceSMatthew G Knepley Input Parameter: 283988ed4aceSMatthew G Knepley . dm - The DM 284088ed4aceSMatthew G Knepley 284188ed4aceSMatthew G Knepley Output Parameter: 284288ed4aceSMatthew G Knepley . sf - The PetscSF 284388ed4aceSMatthew G Knepley 284488ed4aceSMatthew G Knepley Level: intermediate 284588ed4aceSMatthew G Knepley 284688ed4aceSMatthew G Knepley Note: This gets a borrowed reference, so the user should not destroy this PetscSF. 284788ed4aceSMatthew G Knepley 284888ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF() 284988ed4aceSMatthew G Knepley @*/ 28500adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf) 28510adebc6cSBarry Smith { 285288ed4aceSMatthew G Knepley PetscInt nroots; 285388ed4aceSMatthew G Knepley PetscErrorCode ierr; 285488ed4aceSMatthew G Knepley 285588ed4aceSMatthew G Knepley PetscFunctionBegin; 285688ed4aceSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 285788ed4aceSMatthew G Knepley PetscValidPointer(sf, 2); 28580298fd71SBarry Smith ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr); 285988ed4aceSMatthew G Knepley if (nroots < 0) { 286088ed4aceSMatthew G Knepley PetscSection section, gSection; 286188ed4aceSMatthew G Knepley 286288ed4aceSMatthew G Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 286331ea6d37SMatthew G Knepley if (section) { 286488ed4aceSMatthew G Knepley ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr); 286588ed4aceSMatthew G Knepley ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr); 286631ea6d37SMatthew G Knepley } else { 28670298fd71SBarry Smith *sf = NULL; 286831ea6d37SMatthew G Knepley PetscFunctionReturn(0); 286931ea6d37SMatthew G Knepley } 287088ed4aceSMatthew G Knepley } 287188ed4aceSMatthew G Knepley *sf = dm->defaultSF; 287288ed4aceSMatthew G Knepley PetscFunctionReturn(0); 287388ed4aceSMatthew G Knepley } 287488ed4aceSMatthew G Knepley 287588ed4aceSMatthew G Knepley #undef __FUNCT__ 287688ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF" 287788ed4aceSMatthew G Knepley /*@ 287888ed4aceSMatthew G Knepley DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM 287988ed4aceSMatthew G Knepley 288088ed4aceSMatthew G Knepley Input Parameters: 288188ed4aceSMatthew G Knepley + dm - The DM 288288ed4aceSMatthew G Knepley - sf - The PetscSF 288388ed4aceSMatthew G Knepley 288488ed4aceSMatthew G Knepley Level: intermediate 288588ed4aceSMatthew G Knepley 288688ed4aceSMatthew G Knepley Note: Any previous SF is destroyed 288788ed4aceSMatthew G Knepley 288888ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF() 288988ed4aceSMatthew G Knepley @*/ 28900adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf) 28910adebc6cSBarry Smith { 289288ed4aceSMatthew G Knepley PetscErrorCode ierr; 289388ed4aceSMatthew G Knepley 289488ed4aceSMatthew G Knepley PetscFunctionBegin; 289588ed4aceSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 289688ed4aceSMatthew G Knepley PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2); 289788ed4aceSMatthew G Knepley ierr = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr); 289888ed4aceSMatthew G Knepley dm->defaultSF = sf; 289988ed4aceSMatthew G Knepley PetscFunctionReturn(0); 290088ed4aceSMatthew G Knepley } 290188ed4aceSMatthew G Knepley 290288ed4aceSMatthew G Knepley #undef __FUNCT__ 290388ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF" 290488ed4aceSMatthew G Knepley /*@C 290588ed4aceSMatthew G Knepley DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections 290688ed4aceSMatthew G Knepley describing the data layout. 290788ed4aceSMatthew G Knepley 290888ed4aceSMatthew G Knepley Input Parameters: 290988ed4aceSMatthew G Knepley + dm - The DM 291088ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout 291188ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout 291288ed4aceSMatthew G Knepley 291388ed4aceSMatthew G Knepley Level: intermediate 291488ed4aceSMatthew G Knepley 291588ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF() 291688ed4aceSMatthew G Knepley @*/ 291788ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection) 291888ed4aceSMatthew G Knepley { 291982f516ccSBarry Smith MPI_Comm comm; 292088ed4aceSMatthew G Knepley PetscLayout layout; 292188ed4aceSMatthew G Knepley const PetscInt *ranges; 292288ed4aceSMatthew G Knepley PetscInt *local; 292388ed4aceSMatthew G Knepley PetscSFNode *remote; 292488ed4aceSMatthew G Knepley PetscInt pStart, pEnd, p, nroots, nleaves, l; 292588ed4aceSMatthew G Knepley PetscMPIInt size, rank; 292688ed4aceSMatthew G Knepley PetscErrorCode ierr; 292788ed4aceSMatthew G Knepley 292888ed4aceSMatthew G Knepley PetscFunctionBegin; 292982f516ccSBarry Smith ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr); 293088ed4aceSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 293188ed4aceSMatthew G Knepley ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr); 293288ed4aceSMatthew G Knepley ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr); 293388ed4aceSMatthew G Knepley ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr); 293488ed4aceSMatthew G Knepley ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr); 293588ed4aceSMatthew G Knepley ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr); 293688ed4aceSMatthew G Knepley ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr); 293788ed4aceSMatthew G Knepley ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr); 293888ed4aceSMatthew G Knepley ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr); 293988ed4aceSMatthew G Knepley ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr); 294088ed4aceSMatthew G Knepley for (p = pStart, nleaves = 0; p < pEnd; ++p) { 29416636e97aSMatthew G Knepley PetscInt gdof, gcdof; 294288ed4aceSMatthew G Knepley 29436636e97aSMatthew G Knepley ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr); 29446636e97aSMatthew G Knepley ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr); 29456636e97aSMatthew G Knepley nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof; 294688ed4aceSMatthew G Knepley } 294788ed4aceSMatthew G Knepley ierr = PetscMalloc(nleaves * sizeof(PetscInt), &local);CHKERRQ(ierr); 294888ed4aceSMatthew G Knepley ierr = PetscMalloc(nleaves * sizeof(PetscSFNode), &remote);CHKERRQ(ierr); 294988ed4aceSMatthew G Knepley for (p = pStart, l = 0; p < pEnd; ++p) { 29501f588964SMatthew G Knepley const PetscInt *cind; 29516636e97aSMatthew G Knepley PetscInt dof, cdof, off, gdof, gcdof, goff, gsize, d, c; 295288ed4aceSMatthew G Knepley 295388ed4aceSMatthew G Knepley ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr); 295488ed4aceSMatthew G Knepley ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr); 295588ed4aceSMatthew G Knepley ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr); 295688ed4aceSMatthew G Knepley ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr); 295788ed4aceSMatthew G Knepley ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr); 29586636e97aSMatthew G Knepley ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr); 295988ed4aceSMatthew G Knepley ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr); 29606636e97aSMatthew G Knepley if (!gdof) continue; /* Censored point */ 29616636e97aSMatthew G Knepley gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof; 29626636e97aSMatthew G Knepley if (gsize != dof-cdof) { 2963057b4bcdSMatthew G Knepley if (gsize != dof) SETERRQ4(comm, PETSC_ERR_ARG_WRONG, "Global dof %d for point %d is neither the constrained size %d, nor the unconstrained %d", gsize, p, dof-cdof, dof); 29646636e97aSMatthew G Knepley cdof = 0; /* Ignore constraints */ 29656636e97aSMatthew G Knepley } 296688ed4aceSMatthew G Knepley for (d = 0, c = 0; d < dof; ++d) { 296788ed4aceSMatthew G Knepley if ((c < cdof) && (cind[c] == d)) {++c; continue;} 296888ed4aceSMatthew G Knepley local[l+d-c] = off+d; 296988ed4aceSMatthew G Knepley } 297088ed4aceSMatthew G Knepley if (gdof < 0) { 29716636e97aSMatthew G Knepley for (d = 0; d < gsize; ++d, ++l) { 297288ed4aceSMatthew G Knepley PetscInt offset = -(goff+1) + d, r; 297388ed4aceSMatthew G Knepley 297431d3f06eSJed Brown ierr = PetscFindInt(offset,size,ranges,&r);CHKERRQ(ierr); 297531d3f06eSJed Brown if (r < 0) r = -(r+2); 297688ed4aceSMatthew G Knepley remote[l].rank = r; 297788ed4aceSMatthew G Knepley remote[l].index = offset - ranges[r]; 297888ed4aceSMatthew G Knepley } 297988ed4aceSMatthew G Knepley } else { 29806636e97aSMatthew G Knepley for (d = 0; d < gsize; ++d, ++l) { 298188ed4aceSMatthew G Knepley remote[l].rank = rank; 298288ed4aceSMatthew G Knepley remote[l].index = goff+d - ranges[rank]; 298388ed4aceSMatthew G Knepley } 298488ed4aceSMatthew G Knepley } 298588ed4aceSMatthew G Knepley } 29866636e97aSMatthew G Knepley if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves); 298788ed4aceSMatthew G Knepley ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr); 298888ed4aceSMatthew G Knepley ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr); 298988ed4aceSMatthew G Knepley PetscFunctionReturn(0); 299088ed4aceSMatthew G Knepley } 2991af122d2aSMatthew G Knepley 2992af122d2aSMatthew G Knepley #undef __FUNCT__ 2993b21d0597SMatthew G Knepley #define __FUNCT__ "DMGetPointSF" 2994b21d0597SMatthew G Knepley /*@ 2995b21d0597SMatthew G Knepley DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM. 2996b21d0597SMatthew G Knepley 2997b21d0597SMatthew G Knepley Input Parameter: 2998b21d0597SMatthew G Knepley . dm - The DM 2999b21d0597SMatthew G Knepley 3000b21d0597SMatthew G Knepley Output Parameter: 3001b21d0597SMatthew G Knepley . sf - The PetscSF 3002b21d0597SMatthew G Knepley 3003b21d0597SMatthew G Knepley Level: intermediate 3004b21d0597SMatthew G Knepley 3005b21d0597SMatthew G Knepley Note: This gets a borrowed reference, so the user should not destroy this PetscSF. 3006b21d0597SMatthew G Knepley 3007057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF() 3008b21d0597SMatthew G Knepley @*/ 30090adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf) 30100adebc6cSBarry Smith { 3011b21d0597SMatthew G Knepley PetscFunctionBegin; 3012b21d0597SMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 3013b21d0597SMatthew G Knepley PetscValidPointer(sf, 2); 3014b21d0597SMatthew G Knepley *sf = dm->sf; 3015b21d0597SMatthew G Knepley PetscFunctionReturn(0); 3016b21d0597SMatthew G Knepley } 3017b21d0597SMatthew G Knepley 3018b21d0597SMatthew G Knepley #undef __FUNCT__ 3019057b4bcdSMatthew G Knepley #define __FUNCT__ "DMSetPointSF" 3020057b4bcdSMatthew G Knepley /*@ 3021057b4bcdSMatthew G Knepley DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM. 3022057b4bcdSMatthew G Knepley 3023057b4bcdSMatthew G Knepley Input Parameters: 3024057b4bcdSMatthew G Knepley + dm - The DM 3025057b4bcdSMatthew G Knepley - sf - The PetscSF 3026057b4bcdSMatthew G Knepley 3027057b4bcdSMatthew G Knepley Level: intermediate 3028057b4bcdSMatthew G Knepley 3029057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF() 3030057b4bcdSMatthew G Knepley @*/ 30310adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf) 30320adebc6cSBarry Smith { 3033057b4bcdSMatthew G Knepley PetscErrorCode ierr; 3034057b4bcdSMatthew G Knepley 3035057b4bcdSMatthew G Knepley PetscFunctionBegin; 3036057b4bcdSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 3037057b4bcdSMatthew G Knepley PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1); 3038057b4bcdSMatthew G Knepley ierr = PetscSFDestroy(&dm->sf);CHKERRQ(ierr); 3039057b4bcdSMatthew G Knepley ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr); 3040057b4bcdSMatthew G Knepley dm->sf = sf; 3041057b4bcdSMatthew G Knepley PetscFunctionReturn(0); 3042057b4bcdSMatthew G Knepley } 3043057b4bcdSMatthew G Knepley 3044057b4bcdSMatthew G Knepley #undef __FUNCT__ 3045af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetNumFields" 3046af122d2aSMatthew G Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields) 3047af122d2aSMatthew G Knepley { 3048af122d2aSMatthew G Knepley PetscFunctionBegin; 3049af122d2aSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 3050af122d2aSMatthew G Knepley PetscValidPointer(numFields, 2); 3051af122d2aSMatthew G Knepley *numFields = dm->numFields; 3052af122d2aSMatthew G Knepley PetscFunctionReturn(0); 3053af122d2aSMatthew G Knepley } 3054af122d2aSMatthew G Knepley 3055af122d2aSMatthew G Knepley #undef __FUNCT__ 3056af122d2aSMatthew G Knepley #define __FUNCT__ "DMSetNumFields" 3057af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields) 3058af122d2aSMatthew G Knepley { 3059af122d2aSMatthew G Knepley PetscInt f; 3060af122d2aSMatthew G Knepley PetscErrorCode ierr; 3061af122d2aSMatthew G Knepley 3062af122d2aSMatthew G Knepley PetscFunctionBegin; 3063af122d2aSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 3064af122d2aSMatthew G Knepley for (f = 0; f < dm->numFields; ++f) { 3065af122d2aSMatthew G Knepley ierr = PetscObjectDestroy(&dm->fields[f]);CHKERRQ(ierr); 3066af122d2aSMatthew G Knepley } 3067af122d2aSMatthew G Knepley ierr = PetscFree(dm->fields);CHKERRQ(ierr); 3068af122d2aSMatthew G Knepley dm->numFields = numFields; 3069af122d2aSMatthew G Knepley ierr = PetscMalloc(dm->numFields * sizeof(PetscObject), &dm->fields);CHKERRQ(ierr); 3070af122d2aSMatthew G Knepley for (f = 0; f < dm->numFields; ++f) { 307182f516ccSBarry Smith ierr = PetscContainerCreate(PetscObjectComm((PetscObject)dm), (PetscContainer*) &dm->fields[f]);CHKERRQ(ierr); 3072af122d2aSMatthew G Knepley } 3073af122d2aSMatthew G Knepley PetscFunctionReturn(0); 3074af122d2aSMatthew G Knepley } 3075af122d2aSMatthew G Knepley 3076af122d2aSMatthew G Knepley #undef __FUNCT__ 3077af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetField" 3078af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field) 3079af122d2aSMatthew G Knepley { 3080af122d2aSMatthew G Knepley PetscFunctionBegin; 3081af122d2aSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 3082af122d2aSMatthew G Knepley PetscValidPointer(field, 2); 308382f516ccSBarry Smith if (!dm->fields) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Fields have not been setup in this DM. Call DMSetNumFields()"); 308482f516ccSBarry Smith if ((f < 0) || (f >= dm->numFields)) SETERRQ3(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Field %d should be in [%d,%d)", f, 0, dm->numFields); 3085af122d2aSMatthew G Knepley *field = dm->fields[f]; 3086af122d2aSMatthew G Knepley PetscFunctionReturn(0); 3087af122d2aSMatthew G Knepley } 30886636e97aSMatthew G Knepley 30896636e97aSMatthew G Knepley #undef __FUNCT__ 30906636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinates" 30916636e97aSMatthew G Knepley /*@ 30926636e97aSMatthew G Knepley DMSetCoordinates - Sets into the DM a global vector that holds the coordinates 30936636e97aSMatthew G Knepley 30946636e97aSMatthew G Knepley Collective on DM 30956636e97aSMatthew G Knepley 30966636e97aSMatthew G Knepley Input Parameters: 30976636e97aSMatthew G Knepley + dm - the DM 30986636e97aSMatthew G Knepley - c - coordinate vector 30996636e97aSMatthew G Knepley 31006636e97aSMatthew G Knepley Note: 31016636e97aSMatthew G Knepley The coordinates do include those for ghost points, which are in the local vector 31026636e97aSMatthew G Knepley 31036636e97aSMatthew G Knepley Level: intermediate 31046636e97aSMatthew G Knepley 31056636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates 31066636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM() 31076636e97aSMatthew G Knepley @*/ 31086636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c) 31096636e97aSMatthew G Knepley { 31106636e97aSMatthew G Knepley PetscErrorCode ierr; 31116636e97aSMatthew G Knepley 31126636e97aSMatthew G Knepley PetscFunctionBegin; 31136636e97aSMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 31146636e97aSMatthew G Knepley PetscValidHeaderSpecific(c,VEC_CLASSID,2); 31156636e97aSMatthew G Knepley ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr); 31166636e97aSMatthew G Knepley ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr); 31176636e97aSMatthew G Knepley dm->coordinates = c; 31186636e97aSMatthew G Knepley ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr); 31196636e97aSMatthew G Knepley PetscFunctionReturn(0); 31206636e97aSMatthew G Knepley } 31216636e97aSMatthew G Knepley 31226636e97aSMatthew G Knepley #undef __FUNCT__ 31236636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinatesLocal" 31246636e97aSMatthew G Knepley /*@ 31256636e97aSMatthew G Knepley DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates 31266636e97aSMatthew G Knepley 31276636e97aSMatthew G Knepley Collective on DM 31286636e97aSMatthew G Knepley 31296636e97aSMatthew G Knepley Input Parameters: 31306636e97aSMatthew G Knepley + dm - the DM 31316636e97aSMatthew G Knepley - c - coordinate vector 31326636e97aSMatthew G Knepley 31336636e97aSMatthew G Knepley Note: 31346636e97aSMatthew G Knepley The coordinates of ghost points can be set using DMSetCoordinates() 31356636e97aSMatthew G Knepley followed by DMGetCoordinatesLocal(). This is intended to enable the 31366636e97aSMatthew G Knepley setting of ghost coordinates outside of the domain. 31376636e97aSMatthew G Knepley 31386636e97aSMatthew G Knepley Level: intermediate 31396636e97aSMatthew G Knepley 31406636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates 31416636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM() 31426636e97aSMatthew G Knepley @*/ 31436636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c) 31446636e97aSMatthew G Knepley { 31456636e97aSMatthew G Knepley PetscErrorCode ierr; 31466636e97aSMatthew G Knepley 31476636e97aSMatthew G Knepley PetscFunctionBegin; 31486636e97aSMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 31496636e97aSMatthew G Knepley PetscValidHeaderSpecific(c,VEC_CLASSID,2); 31506636e97aSMatthew G Knepley ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr); 31516636e97aSMatthew G Knepley ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr); 31528865f1eaSKarl Rupp 31536636e97aSMatthew G Knepley dm->coordinatesLocal = c; 31548865f1eaSKarl Rupp 31556636e97aSMatthew G Knepley ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr); 31566636e97aSMatthew G Knepley PetscFunctionReturn(0); 31576636e97aSMatthew G Knepley } 31586636e97aSMatthew G Knepley 31596636e97aSMatthew G Knepley #undef __FUNCT__ 31606636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinates" 31616636e97aSMatthew G Knepley /*@ 31626636e97aSMatthew G Knepley DMGetCoordinates - Gets a global vector with the coordinates associated with the DM. 31636636e97aSMatthew G Knepley 31646636e97aSMatthew G Knepley Not Collective 31656636e97aSMatthew G Knepley 31666636e97aSMatthew G Knepley Input Parameter: 31676636e97aSMatthew G Knepley . dm - the DM 31686636e97aSMatthew G Knepley 31696636e97aSMatthew G Knepley Output Parameter: 31706636e97aSMatthew G Knepley . c - global coordinate vector 31716636e97aSMatthew G Knepley 31726636e97aSMatthew G Knepley Note: 31736636e97aSMatthew G Knepley This is a borrowed reference, so the user should NOT destroy this vector 31746636e97aSMatthew G Knepley 31756636e97aSMatthew G Knepley Each process has only the local coordinates (does NOT have the ghost coordinates). 31766636e97aSMatthew G Knepley 31776636e97aSMatthew G Knepley For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...) 31786636e97aSMatthew G Knepley and (x_0,y_0,z_0,x_1,y_1,z_1...) 31796636e97aSMatthew G Knepley 31806636e97aSMatthew G Knepley Level: intermediate 31816636e97aSMatthew G Knepley 31826636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates 31836636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM() 31846636e97aSMatthew G Knepley @*/ 31856636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c) 31866636e97aSMatthew G Knepley { 31876636e97aSMatthew G Knepley PetscErrorCode ierr; 31886636e97aSMatthew G Knepley 31896636e97aSMatthew G Knepley PetscFunctionBegin; 31906636e97aSMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 31916636e97aSMatthew G Knepley PetscValidPointer(c,2); 31921f588964SMatthew G Knepley if (!dm->coordinates && dm->coordinatesLocal) { 31930298fd71SBarry Smith DM cdm = NULL; 31946636e97aSMatthew G Knepley 31956636e97aSMatthew G Knepley ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr); 31966636e97aSMatthew G Knepley ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr); 31976636e97aSMatthew G Knepley ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr); 31986636e97aSMatthew G Knepley ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr); 31996636e97aSMatthew G Knepley ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr); 32006636e97aSMatthew G Knepley } 32016636e97aSMatthew G Knepley *c = dm->coordinates; 32026636e97aSMatthew G Knepley PetscFunctionReturn(0); 32036636e97aSMatthew G Knepley } 32046636e97aSMatthew G Knepley 32056636e97aSMatthew G Knepley #undef __FUNCT__ 32066636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinatesLocal" 32076636e97aSMatthew G Knepley /*@ 32086636e97aSMatthew G Knepley DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM. 32096636e97aSMatthew G Knepley 32106636e97aSMatthew G Knepley Collective on DM 32116636e97aSMatthew G Knepley 32126636e97aSMatthew G Knepley Input Parameter: 32136636e97aSMatthew G Knepley . dm - the DM 32146636e97aSMatthew G Knepley 32156636e97aSMatthew G Knepley Output Parameter: 32166636e97aSMatthew G Knepley . c - coordinate vector 32176636e97aSMatthew G Knepley 32186636e97aSMatthew G Knepley Note: 32196636e97aSMatthew G Knepley This is a borrowed reference, so the user should NOT destroy this vector 32206636e97aSMatthew G Knepley 32216636e97aSMatthew G Knepley Each process has the local and ghost coordinates 32226636e97aSMatthew G Knepley 32236636e97aSMatthew G Knepley For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...) 32246636e97aSMatthew G Knepley and (x_0,y_0,z_0,x_1,y_1,z_1...) 32256636e97aSMatthew G Knepley 32266636e97aSMatthew G Knepley Level: intermediate 32276636e97aSMatthew G Knepley 32286636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates 32296636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM() 32306636e97aSMatthew G Knepley @*/ 32316636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c) 32326636e97aSMatthew G Knepley { 32336636e97aSMatthew G Knepley PetscErrorCode ierr; 32346636e97aSMatthew G Knepley 32356636e97aSMatthew G Knepley PetscFunctionBegin; 32366636e97aSMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 32376636e97aSMatthew G Knepley PetscValidPointer(c,2); 32381f588964SMatthew G Knepley if (!dm->coordinatesLocal && dm->coordinates) { 32390298fd71SBarry Smith DM cdm = NULL; 32406636e97aSMatthew G Knepley 32416636e97aSMatthew G Knepley ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr); 32426636e97aSMatthew G Knepley ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr); 32436636e97aSMatthew G Knepley ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr); 32446636e97aSMatthew G Knepley ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr); 32456636e97aSMatthew G Knepley ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr); 32466636e97aSMatthew G Knepley } 32476636e97aSMatthew G Knepley *c = dm->coordinatesLocal; 32486636e97aSMatthew G Knepley PetscFunctionReturn(0); 32496636e97aSMatthew G Knepley } 32506636e97aSMatthew G Knepley 32516636e97aSMatthew G Knepley #undef __FUNCT__ 32526636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinateDM" 32536636e97aSMatthew G Knepley /*@ 32546636e97aSMatthew G Knepley DMGetCoordinateDM - Gets the DM that scatters between global and local coordinates 32556636e97aSMatthew G Knepley 32566636e97aSMatthew G Knepley Collective on DM 32576636e97aSMatthew G Knepley 32586636e97aSMatthew G Knepley Input Parameter: 32596636e97aSMatthew G Knepley . dm - the DM 32606636e97aSMatthew G Knepley 32616636e97aSMatthew G Knepley Output Parameter: 32626636e97aSMatthew G Knepley . cdm - coordinate DM 32636636e97aSMatthew G Knepley 32646636e97aSMatthew G Knepley Level: intermediate 32656636e97aSMatthew G Knepley 32666636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates 32676636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal() 32686636e97aSMatthew G Knepley @*/ 32696636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm) 32706636e97aSMatthew G Knepley { 32716636e97aSMatthew G Knepley PetscErrorCode ierr; 32726636e97aSMatthew G Knepley 32736636e97aSMatthew G Knepley PetscFunctionBegin; 32746636e97aSMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 32756636e97aSMatthew G Knepley PetscValidPointer(cdm,2); 32766636e97aSMatthew G Knepley if (!dm->coordinateDM) { 327782f516ccSBarry Smith if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM"); 32786636e97aSMatthew G Knepley ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr); 32796636e97aSMatthew G Knepley } 32806636e97aSMatthew G Knepley *cdm = dm->coordinateDM; 32816636e97aSMatthew G Knepley PetscFunctionReturn(0); 32826636e97aSMatthew G Knepley } 3283e87bb0d3SMatthew G Knepley 3284e87bb0d3SMatthew G Knepley #undef __FUNCT__ 3285e87bb0d3SMatthew G Knepley #define __FUNCT__ "DMLocatePoints" 3286e87bb0d3SMatthew G Knepley /*@ 3287e87bb0d3SMatthew G Knepley DMLocatePoints - Locate the points in v in the mesh and return an IS of the containing cells 3288e87bb0d3SMatthew G Knepley 3289e87bb0d3SMatthew G Knepley Not collective 3290e87bb0d3SMatthew G Knepley 3291e87bb0d3SMatthew G Knepley Input Parameters: 3292e87bb0d3SMatthew G Knepley + dm - The DM 3293e87bb0d3SMatthew G Knepley - v - The Vec of points 3294e87bb0d3SMatthew G Knepley 329561e3bb9bSMatthew G Knepley Output Parameter: 3296e87bb0d3SMatthew G Knepley . cells - The local cell numbers for cells which contain the points 3297e87bb0d3SMatthew G Knepley 3298e87bb0d3SMatthew G Knepley Level: developer 329961e3bb9bSMatthew G Knepley 330061e3bb9bSMatthew G Knepley .keywords: point location, mesh 330161e3bb9bSMatthew G Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal() 330261e3bb9bSMatthew G Knepley @*/ 3303e87bb0d3SMatthew G Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, IS *cells) 3304e87bb0d3SMatthew G Knepley { 3305735aa83eSMatthew G Knepley PetscErrorCode ierr; 3306735aa83eSMatthew G Knepley 3307e87bb0d3SMatthew G Knepley PetscFunctionBegin; 3308e87bb0d3SMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 3309e87bb0d3SMatthew G Knepley PetscValidHeaderSpecific(v,VEC_CLASSID,2); 3310e87bb0d3SMatthew G Knepley PetscValidPointer(cells,3); 3311735aa83eSMatthew G Knepley if (dm->ops->locatepoints) { 3312735aa83eSMatthew G Knepley ierr = (*dm->ops->locatepoints)(dm,v,cells);CHKERRQ(ierr); 331382f516ccSBarry Smith } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM"); 3314e87bb0d3SMatthew G Knepley PetscFunctionReturn(0); 3315e87bb0d3SMatthew G Knepley } 3316