1b45d2f2cSJed Brown #include <petsc-private/dmimpl.h> /*I "petscdm.h" I*/ 20c312b8eSJed Brown #include <petscsf.h> 347c6ae99SBarry Smith 4732e2eb9SMatthew G Knepley PetscClassId DM_CLASSID; 5f089877aSRichard Tran Mills PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal, DM_LocalToLocal; 667a56275SMatthew G Knepley 747c6ae99SBarry Smith #undef __FUNCT__ 8a4121054SBarry Smith #define __FUNCT__ "DMCreate" 9a4121054SBarry Smith /*@ 10de043629SMatthew G Knepley DMCreate - Creates an empty DM object. The type can then be set with DMSetType(). 11a4121054SBarry Smith 12a4121054SBarry Smith If you never call DMSetType() it will generate an 13a4121054SBarry Smith error when you try to use the vector. 14a4121054SBarry Smith 15a4121054SBarry Smith Collective on MPI_Comm 16a4121054SBarry Smith 17a4121054SBarry Smith Input Parameter: 18a4121054SBarry Smith . comm - The communicator for the DM object 19a4121054SBarry Smith 20a4121054SBarry Smith Output Parameter: 21a4121054SBarry Smith . dm - The DM object 22a4121054SBarry Smith 23a4121054SBarry Smith Level: beginner 24a4121054SBarry Smith 25a4121054SBarry Smith .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE 26a4121054SBarry Smith @*/ 277087cfbeSBarry Smith PetscErrorCode DMCreate(MPI_Comm comm,DM *dm) 28a4121054SBarry Smith { 29a4121054SBarry Smith DM v; 30a4121054SBarry Smith PetscErrorCode ierr; 31a4121054SBarry Smith 32a4121054SBarry Smith PetscFunctionBegin; 331411c6eeSJed Brown PetscValidPointer(dm,2); 340298fd71SBarry Smith *dm = NULL; 358b984314SMatthew G. Knepley ierr = PetscSysInitializePackage();CHKERRQ(ierr); 36607a6623SBarry Smith ierr = VecInitializePackage();CHKERRQ(ierr); 37607a6623SBarry Smith ierr = MatInitializePackage();CHKERRQ(ierr); 38607a6623SBarry Smith ierr = DMInitializePackage();CHKERRQ(ierr); 39a4121054SBarry Smith 4067c2884eSBarry Smith ierr = PetscHeaderCreate(v, _p_DM, struct _DMOps, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr); 41a4121054SBarry Smith ierr = PetscMemzero(v->ops, sizeof(struct _DMOps));CHKERRQ(ierr); 421411c6eeSJed Brown 43e7c4fc90SDmitry Karpeev 440298fd71SBarry Smith v->ltogmap = NULL; 450298fd71SBarry Smith v->ltogmapb = NULL; 461411c6eeSJed Brown v->bs = 1; 47171400e9SBarry Smith v->coloringtype = IS_COLORING_GLOBAL; 4888ed4aceSMatthew G Knepley ierr = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr); 4988ed4aceSMatthew G Knepley ierr = PetscSFCreate(comm, &v->defaultSF);CHKERRQ(ierr); 500298fd71SBarry Smith v->defaultSection = NULL; 510298fd71SBarry Smith v->defaultGlobalSection = NULL; 52435a35e8SMatthew G Knepley { 53435a35e8SMatthew G Knepley PetscInt i; 54435a35e8SMatthew G Knepley for (i = 0; i < 10; ++i) { 550298fd71SBarry Smith v->nullspaceConstructors[i] = NULL; 56435a35e8SMatthew G Knepley } 57435a35e8SMatthew G Knepley } 58af122d2aSMatthew G Knepley v->numFields = 0; 590298fd71SBarry Smith v->fields = NULL; 60c0dedaeaSBarry Smith ierr = DMSetVecType(v,VECSTANDARD);CHKERRQ(ierr); 61b412c318SBarry Smith ierr = DMSetMatType(v,MATAIJ);CHKERRQ(ierr); 621411c6eeSJed Brown *dm = v; 63a4121054SBarry Smith PetscFunctionReturn(0); 64a4121054SBarry Smith } 65a4121054SBarry Smith 66a4121054SBarry Smith #undef __FUNCT__ 6738221697SMatthew G. Knepley #define __FUNCT__ "DMClone" 6838221697SMatthew G. Knepley /*@ 6938221697SMatthew G. Knepley DMClone - Creates a DM object with the same topology as the original. 7038221697SMatthew G. Knepley 7138221697SMatthew G. Knepley Collective on MPI_Comm 7238221697SMatthew G. Knepley 7338221697SMatthew G. Knepley Input Parameter: 7438221697SMatthew G. Knepley . dm - The original DM object 7538221697SMatthew G. Knepley 7638221697SMatthew G. Knepley Output Parameter: 7738221697SMatthew G. Knepley . newdm - The new DM object 7838221697SMatthew G. Knepley 7938221697SMatthew G. Knepley Level: beginner 8038221697SMatthew G. Knepley 8138221697SMatthew G. Knepley .keywords: DM, topology, create 8238221697SMatthew G. Knepley @*/ 8338221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm) 8438221697SMatthew G. Knepley { 8538221697SMatthew G. Knepley PetscSF sf; 8638221697SMatthew G. Knepley Vec coords; 8738221697SMatthew G. Knepley void *ctx; 8838221697SMatthew G. Knepley PetscErrorCode ierr; 8938221697SMatthew G. Knepley 9038221697SMatthew G. Knepley PetscFunctionBegin; 9138221697SMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 9238221697SMatthew G. Knepley PetscValidPointer(newdm,2); 9338221697SMatthew G. Knepley ierr = DMCreate(PetscObjectComm((PetscObject)dm), newdm);CHKERRQ(ierr); 9438221697SMatthew G. Knepley if (dm->ops->clone) { 9538221697SMatthew G. Knepley ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr); 9638221697SMatthew G. Knepley } 97cd27c7c9SMatthew G. Knepley (*newdm)->setupcalled = PETSC_TRUE; 9838221697SMatthew G. Knepley ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr); 9938221697SMatthew G. Knepley ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr); 10038221697SMatthew G. Knepley ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr); 10138221697SMatthew G. Knepley ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr); 10238221697SMatthew G. Knepley ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr); 10338221697SMatthew G. Knepley if (coords) { 10438221697SMatthew G. Knepley ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr); 10538221697SMatthew G. Knepley } else { 10638221697SMatthew G. Knepley ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr); 10738221697SMatthew G. Knepley if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);} 10838221697SMatthew G. Knepley } 10938221697SMatthew G. Knepley PetscFunctionReturn(0); 11038221697SMatthew G. Knepley } 11138221697SMatthew G. Knepley 11238221697SMatthew G. Knepley #undef __FUNCT__ 1139a42bb27SBarry Smith #define __FUNCT__ "DMSetVecType" 1149a42bb27SBarry Smith /*@C 115564755cdSBarry Smith DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector() 1169a42bb27SBarry Smith 117aa219208SBarry Smith Logically Collective on DMDA 1189a42bb27SBarry Smith 1199a42bb27SBarry Smith Input Parameter: 1209a42bb27SBarry Smith + da - initial distributed array 1218154be41SBarry Smith . ctype - the vector type, currently either VECSTANDARD or VECCUSP 1229a42bb27SBarry Smith 1239a42bb27SBarry Smith Options Database: 124dd85299cSBarry Smith . -dm_vec_type ctype 1259a42bb27SBarry Smith 1269a42bb27SBarry Smith Level: intermediate 1279a42bb27SBarry Smith 128c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMDestroy(), DMDA, DMDAInterpolationType, VecType, DMGetVecType() 1299a42bb27SBarry Smith @*/ 13019fd82e9SBarry Smith PetscErrorCode DMSetVecType(DM da,VecType ctype) 1319a42bb27SBarry Smith { 1329a42bb27SBarry Smith PetscErrorCode ierr; 1339a42bb27SBarry Smith 1349a42bb27SBarry Smith PetscFunctionBegin; 1359a42bb27SBarry Smith PetscValidHeaderSpecific(da,DM_CLASSID,1); 1369a42bb27SBarry Smith ierr = PetscFree(da->vectype);CHKERRQ(ierr); 13719fd82e9SBarry Smith ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr); 1389a42bb27SBarry Smith PetscFunctionReturn(0); 1399a42bb27SBarry Smith } 1409a42bb27SBarry Smith 1419a42bb27SBarry Smith #undef __FUNCT__ 142c0dedaeaSBarry Smith #define __FUNCT__ "DMGetVecType" 143c0dedaeaSBarry Smith /*@C 144c0dedaeaSBarry Smith DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector() 145c0dedaeaSBarry Smith 146c0dedaeaSBarry Smith Logically Collective on DMDA 147c0dedaeaSBarry Smith 148c0dedaeaSBarry Smith Input Parameter: 149c0dedaeaSBarry Smith . da - initial distributed array 150c0dedaeaSBarry Smith 151c0dedaeaSBarry Smith Output Parameter: 152c0dedaeaSBarry Smith . ctype - the vector type 153c0dedaeaSBarry Smith 154c0dedaeaSBarry Smith Level: intermediate 155c0dedaeaSBarry Smith 156c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMDestroy(), DMDA, DMDAInterpolationType, VecType 157c0dedaeaSBarry Smith @*/ 158c0dedaeaSBarry Smith PetscErrorCode DMGetVecType(DM da,VecType *ctype) 159c0dedaeaSBarry Smith { 160c0dedaeaSBarry Smith PetscFunctionBegin; 161c0dedaeaSBarry Smith PetscValidHeaderSpecific(da,DM_CLASSID,1); 162c0dedaeaSBarry Smith *ctype = da->vectype; 163c0dedaeaSBarry Smith PetscFunctionReturn(0); 164c0dedaeaSBarry Smith } 165c0dedaeaSBarry Smith 166c0dedaeaSBarry Smith #undef __FUNCT__ 1675f1ad066SMatthew G Knepley #define __FUNCT__ "VecGetDM" 1685f1ad066SMatthew G Knepley /*@ 16934f98d34SBarry Smith VecGetDM - Gets the DM defining the data layout of the vector 1705f1ad066SMatthew G Knepley 1715f1ad066SMatthew G Knepley Not collective 1725f1ad066SMatthew G Knepley 1735f1ad066SMatthew G Knepley Input Parameter: 1745f1ad066SMatthew G Knepley . v - The Vec 1755f1ad066SMatthew G Knepley 1765f1ad066SMatthew G Knepley Output Parameter: 1775f1ad066SMatthew G Knepley . dm - The DM 1785f1ad066SMatthew G Knepley 1795f1ad066SMatthew G Knepley Level: intermediate 1805f1ad066SMatthew G Knepley 1815f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType() 1825f1ad066SMatthew G Knepley @*/ 1835f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm) 1845f1ad066SMatthew G Knepley { 1855f1ad066SMatthew G Knepley PetscErrorCode ierr; 1865f1ad066SMatthew G Knepley 1875f1ad066SMatthew G Knepley PetscFunctionBegin; 1885f1ad066SMatthew G Knepley PetscValidHeaderSpecific(v,VEC_CLASSID,1); 1895f1ad066SMatthew G Knepley PetscValidPointer(dm,2); 1905f1ad066SMatthew G Knepley ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr); 1915f1ad066SMatthew G Knepley PetscFunctionReturn(0); 1925f1ad066SMatthew G Knepley } 1935f1ad066SMatthew G Knepley 1945f1ad066SMatthew G Knepley #undef __FUNCT__ 1955f1ad066SMatthew G Knepley #define __FUNCT__ "VecSetDM" 1965f1ad066SMatthew G Knepley /*@ 1975f1ad066SMatthew G Knepley VecSetDM - Sets the DM defining the data layout of the vector 1985f1ad066SMatthew G Knepley 1995f1ad066SMatthew G Knepley Not collective 2005f1ad066SMatthew G Knepley 2015f1ad066SMatthew G Knepley Input Parameters: 2025f1ad066SMatthew G Knepley + v - The Vec 2035f1ad066SMatthew G Knepley - dm - The DM 2045f1ad066SMatthew G Knepley 2055f1ad066SMatthew G Knepley Level: intermediate 2065f1ad066SMatthew G Knepley 2075f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType() 2085f1ad066SMatthew G Knepley @*/ 2095f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm) 2105f1ad066SMatthew G Knepley { 2115f1ad066SMatthew G Knepley PetscErrorCode ierr; 2125f1ad066SMatthew G Knepley 2135f1ad066SMatthew G Knepley PetscFunctionBegin; 2145f1ad066SMatthew G Knepley PetscValidHeaderSpecific(v,VEC_CLASSID,1); 215d7f50e27SLisandro Dalcin if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2); 2165f1ad066SMatthew G Knepley ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr); 2175f1ad066SMatthew G Knepley PetscFunctionReturn(0); 2185f1ad066SMatthew G Knepley } 2195f1ad066SMatthew G Knepley 2205f1ad066SMatthew G Knepley #undef __FUNCT__ 221521d9a4cSLisandro Dalcin #define __FUNCT__ "DMSetMatType" 222521d9a4cSLisandro Dalcin /*@C 223521d9a4cSLisandro Dalcin DMSetMatType - Sets the type of matrix created with DMCreateMatrix() 224521d9a4cSLisandro Dalcin 225521d9a4cSLisandro Dalcin Logically Collective on DM 226521d9a4cSLisandro Dalcin 227521d9a4cSLisandro Dalcin Input Parameter: 228521d9a4cSLisandro Dalcin + dm - the DM context 229521d9a4cSLisandro Dalcin . ctype - the matrix type 230521d9a4cSLisandro Dalcin 231521d9a4cSLisandro Dalcin Options Database: 232521d9a4cSLisandro Dalcin . -dm_mat_type ctype 233521d9a4cSLisandro Dalcin 234521d9a4cSLisandro Dalcin Level: intermediate 235521d9a4cSLisandro Dalcin 236c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType() 237521d9a4cSLisandro Dalcin @*/ 23819fd82e9SBarry Smith PetscErrorCode DMSetMatType(DM dm,MatType ctype) 239521d9a4cSLisandro Dalcin { 240521d9a4cSLisandro Dalcin PetscErrorCode ierr; 24188f0584fSBarry Smith 242521d9a4cSLisandro Dalcin PetscFunctionBegin; 243521d9a4cSLisandro Dalcin PetscValidHeaderSpecific(dm,DM_CLASSID,1); 244521d9a4cSLisandro Dalcin ierr = PetscFree(dm->mattype);CHKERRQ(ierr); 24519fd82e9SBarry Smith ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr); 246521d9a4cSLisandro Dalcin PetscFunctionReturn(0); 247521d9a4cSLisandro Dalcin } 248521d9a4cSLisandro Dalcin 249521d9a4cSLisandro Dalcin #undef __FUNCT__ 250c0dedaeaSBarry Smith #define __FUNCT__ "DMGetMatType" 251c0dedaeaSBarry Smith /*@C 252c0dedaeaSBarry Smith DMGetMatType - Gets the type of matrix created with DMCreateMatrix() 253c0dedaeaSBarry Smith 254c0dedaeaSBarry Smith Logically Collective on DM 255c0dedaeaSBarry Smith 256c0dedaeaSBarry Smith Input Parameter: 257c0dedaeaSBarry Smith . dm - the DM context 258c0dedaeaSBarry Smith 259c0dedaeaSBarry Smith Output Parameter: 260c0dedaeaSBarry Smith . ctype - the matrix type 261c0dedaeaSBarry Smith 262c0dedaeaSBarry Smith Options Database: 263c0dedaeaSBarry Smith . -dm_mat_type ctype 264c0dedaeaSBarry Smith 265c0dedaeaSBarry Smith Level: intermediate 266c0dedaeaSBarry Smith 267c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType() 268c0dedaeaSBarry Smith @*/ 269c0dedaeaSBarry Smith PetscErrorCode DMGetMatType(DM dm,MatType *ctype) 270c0dedaeaSBarry Smith { 271c0dedaeaSBarry Smith PetscFunctionBegin; 272c0dedaeaSBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 273c0dedaeaSBarry Smith *ctype = dm->mattype; 274c0dedaeaSBarry Smith PetscFunctionReturn(0); 275c0dedaeaSBarry Smith } 276c0dedaeaSBarry Smith 277c0dedaeaSBarry Smith #undef __FUNCT__ 278c688c046SMatthew G Knepley #define __FUNCT__ "MatGetDM" 279c688c046SMatthew G Knepley /*@ 28034f98d34SBarry Smith MatGetDM - Gets the DM defining the data layout of the matrix 281c688c046SMatthew G Knepley 282c688c046SMatthew G Knepley Not collective 283c688c046SMatthew G Knepley 284c688c046SMatthew G Knepley Input Parameter: 285c688c046SMatthew G Knepley . A - The Mat 286c688c046SMatthew G Knepley 287c688c046SMatthew G Knepley Output Parameter: 288c688c046SMatthew G Knepley . dm - The DM 289c688c046SMatthew G Knepley 290c688c046SMatthew G Knepley Level: intermediate 291c688c046SMatthew G Knepley 292c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType() 293c688c046SMatthew G Knepley @*/ 294c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm) 295c688c046SMatthew G Knepley { 296c688c046SMatthew G Knepley PetscErrorCode ierr; 297c688c046SMatthew G Knepley 298c688c046SMatthew G Knepley PetscFunctionBegin; 299c688c046SMatthew G Knepley PetscValidHeaderSpecific(A,MAT_CLASSID,1); 300c688c046SMatthew G Knepley PetscValidPointer(dm,2); 301c688c046SMatthew G Knepley ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr); 302c688c046SMatthew G Knepley PetscFunctionReturn(0); 303c688c046SMatthew G Knepley } 304c688c046SMatthew G Knepley 305c688c046SMatthew G Knepley #undef __FUNCT__ 306c688c046SMatthew G Knepley #define __FUNCT__ "MatSetDM" 307c688c046SMatthew G Knepley /*@ 308c688c046SMatthew G Knepley MatSetDM - Sets the DM defining the data layout of the matrix 309c688c046SMatthew G Knepley 310c688c046SMatthew G Knepley Not collective 311c688c046SMatthew G Knepley 312c688c046SMatthew G Knepley Input Parameters: 313c688c046SMatthew G Knepley + A - The Mat 314c688c046SMatthew G Knepley - dm - The DM 315c688c046SMatthew G Knepley 316c688c046SMatthew G Knepley Level: intermediate 317c688c046SMatthew G Knepley 318c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType() 319c688c046SMatthew G Knepley @*/ 320c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm) 321c688c046SMatthew G Knepley { 322c688c046SMatthew G Knepley PetscErrorCode ierr; 323c688c046SMatthew G Knepley 324c688c046SMatthew G Knepley PetscFunctionBegin; 325c688c046SMatthew G Knepley PetscValidHeaderSpecific(A,MAT_CLASSID,1); 3268865f1eaSKarl Rupp if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2); 327c688c046SMatthew G Knepley ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr); 328c688c046SMatthew G Knepley PetscFunctionReturn(0); 329c688c046SMatthew G Knepley } 330c688c046SMatthew G Knepley 331c688c046SMatthew G Knepley #undef __FUNCT__ 3329a42bb27SBarry Smith #define __FUNCT__ "DMSetOptionsPrefix" 3339a42bb27SBarry Smith /*@C 3349a42bb27SBarry Smith DMSetOptionsPrefix - Sets the prefix used for searching for all 335aa219208SBarry Smith DMDA options in the database. 3369a42bb27SBarry Smith 337aa219208SBarry Smith Logically Collective on DMDA 3389a42bb27SBarry Smith 3399a42bb27SBarry Smith Input Parameter: 340aa219208SBarry Smith + da - the DMDA context 3419a42bb27SBarry Smith - prefix - the prefix to prepend to all option names 3429a42bb27SBarry Smith 3439a42bb27SBarry Smith Notes: 3449a42bb27SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 3459a42bb27SBarry Smith The first character of all runtime options is AUTOMATICALLY the hyphen. 3469a42bb27SBarry Smith 3479a42bb27SBarry Smith Level: advanced 3489a42bb27SBarry Smith 349aa219208SBarry Smith .keywords: DMDA, set, options, prefix, database 3509a42bb27SBarry Smith 3519a42bb27SBarry Smith .seealso: DMSetFromOptions() 3529a42bb27SBarry Smith @*/ 3537087cfbeSBarry Smith PetscErrorCode DMSetOptionsPrefix(DM dm,const char prefix[]) 3549a42bb27SBarry Smith { 3559a42bb27SBarry Smith PetscErrorCode ierr; 3569a42bb27SBarry Smith 3579a42bb27SBarry Smith PetscFunctionBegin; 3589a42bb27SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 3599a42bb27SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr); 3609a42bb27SBarry Smith PetscFunctionReturn(0); 3619a42bb27SBarry Smith } 3629a42bb27SBarry Smith 3639a42bb27SBarry Smith #undef __FUNCT__ 36447c6ae99SBarry Smith #define __FUNCT__ "DMDestroy" 36547c6ae99SBarry Smith /*@ 366aa219208SBarry Smith DMDestroy - Destroys a vector packer or DMDA. 36747c6ae99SBarry Smith 36847c6ae99SBarry Smith Collective on DM 36947c6ae99SBarry Smith 37047c6ae99SBarry Smith Input Parameter: 37147c6ae99SBarry Smith . dm - the DM object to destroy 37247c6ae99SBarry Smith 37347c6ae99SBarry Smith Level: developer 37447c6ae99SBarry Smith 375e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix() 37647c6ae99SBarry Smith 37747c6ae99SBarry Smith @*/ 378fcfd50ebSBarry Smith PetscErrorCode DMDestroy(DM *dm) 37947c6ae99SBarry Smith { 380af122d2aSMatthew G Knepley PetscInt i, cnt = 0, f; 381dfe15315SJed Brown DMNamedVecLink nlink,nnext; 38247c6ae99SBarry Smith PetscErrorCode ierr; 38347c6ae99SBarry Smith 38447c6ae99SBarry Smith PetscFunctionBegin; 3856bf464f9SBarry Smith if (!*dm) PetscFunctionReturn(0); 3866bf464f9SBarry Smith PetscValidHeaderSpecific((*dm),DM_CLASSID,1); 38787e657c6SBarry Smith 388a546ed68SMatthew G. Knepley /* I think it makes sense to dump all attached things when you are destroyed, which also eliminates circular references */ 389a546ed68SMatthew G. Knepley for (f = 0; f < (*dm)->numFields; ++f) { 390a546ed68SMatthew G. Knepley ierr = PetscObjectCompose((*dm)->fields[f], "pmat", NULL);CHKERRQ(ierr); 391a546ed68SMatthew G. Knepley ierr = PetscObjectCompose((*dm)->fields[f], "nullspace", NULL);CHKERRQ(ierr); 392a546ed68SMatthew G. Knepley ierr = PetscObjectCompose((*dm)->fields[f], "nearnullspace", NULL);CHKERRQ(ierr); 393a546ed68SMatthew G. Knepley } 39487e657c6SBarry Smith /* count all the circular references of DM and its contained Vecs */ 395732e2eb9SMatthew G Knepley for (i=0; i<DM_MAX_WORK_VECTORS; i++) { 3968865f1eaSKarl Rupp if ((*dm)->localin[i]) cnt++; 3978865f1eaSKarl Rupp if ((*dm)->globalin[i]) cnt++; 398732e2eb9SMatthew G Knepley } 399dfe15315SJed Brown for (nlink=(*dm)->namedglobal; nlink; nlink=nlink->next) cnt++; 4002348bcf4SPeter Brune for (nlink=(*dm)->namedlocal; nlink; nlink=nlink->next) cnt++; 4012348bcf4SPeter Brune if ((*dm)->x) { 4022348bcf4SPeter Brune DM obj; 4032348bcf4SPeter Brune ierr = VecGetDM((*dm)->x, &obj);CHKERRQ(ierr); 4042348bcf4SPeter Brune if (obj == *dm) cnt++; 4052348bcf4SPeter Brune } 406732e2eb9SMatthew G Knepley 4076bf464f9SBarry Smith if (--((PetscObject)(*dm))->refct - cnt > 0) {*dm = 0; PetscFunctionReturn(0);} 408732e2eb9SMatthew G Knepley /* 409732e2eb9SMatthew G Knepley Need this test because the dm references the vectors that 410732e2eb9SMatthew G Knepley reference the dm, so destroying the dm calls destroy on the 411732e2eb9SMatthew G Knepley vectors that cause another destroy on the dm 412732e2eb9SMatthew G Knepley */ 4136bf464f9SBarry Smith if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0); 4146bf464f9SBarry Smith ((PetscObject) (*dm))->refct = 0; 415732e2eb9SMatthew G Knepley for (i=0; i<DM_MAX_WORK_VECTORS; i++) { 4166bf464f9SBarry Smith if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()"); 4176bf464f9SBarry Smith ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr); 418732e2eb9SMatthew G Knepley } 419dfe15315SJed Brown for (nlink=(*dm)->namedglobal; nlink; nlink=nnext) { /* Destroy the named vectors */ 420dfe15315SJed Brown nnext = nlink->next; 421dfe15315SJed 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); 422dfe15315SJed Brown ierr = PetscFree(nlink->name);CHKERRQ(ierr); 423dfe15315SJed Brown ierr = VecDestroy(&nlink->X);CHKERRQ(ierr); 424dfe15315SJed Brown ierr = PetscFree(nlink);CHKERRQ(ierr); 425dfe15315SJed Brown } 4260298fd71SBarry Smith (*dm)->namedglobal = NULL; 4271a266240SBarry Smith 4282348bcf4SPeter Brune for (nlink=(*dm)->namedlocal; nlink; nlink=nnext) { /* Destroy the named local vectors */ 4292348bcf4SPeter Brune nnext = nlink->next; 4302348bcf4SPeter 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); 4312348bcf4SPeter Brune ierr = PetscFree(nlink->name);CHKERRQ(ierr); 4322348bcf4SPeter Brune ierr = VecDestroy(&nlink->X);CHKERRQ(ierr); 4332348bcf4SPeter Brune ierr = PetscFree(nlink);CHKERRQ(ierr); 4342348bcf4SPeter Brune } 4350298fd71SBarry Smith (*dm)->namedlocal = NULL; 4362348bcf4SPeter Brune 437b17ce1afSJed Brown /* Destroy the list of hooks */ 438c833c3b5SJed Brown { 439c833c3b5SJed Brown DMCoarsenHookLink link,next; 440b17ce1afSJed Brown for (link=(*dm)->coarsenhook; link; link=next) { 441b17ce1afSJed Brown next = link->next; 442b17ce1afSJed Brown ierr = PetscFree(link);CHKERRQ(ierr); 443b17ce1afSJed Brown } 4440298fd71SBarry Smith (*dm)->coarsenhook = NULL; 445c833c3b5SJed Brown } 446c833c3b5SJed Brown { 447c833c3b5SJed Brown DMRefineHookLink link,next; 448c833c3b5SJed Brown for (link=(*dm)->refinehook; link; link=next) { 449c833c3b5SJed Brown next = link->next; 450c833c3b5SJed Brown ierr = PetscFree(link);CHKERRQ(ierr); 451c833c3b5SJed Brown } 4520298fd71SBarry Smith (*dm)->refinehook = NULL; 453c833c3b5SJed Brown } 454be081cd6SPeter Brune { 455be081cd6SPeter Brune DMSubDomainHookLink link,next; 456be081cd6SPeter Brune for (link=(*dm)->subdomainhook; link; link=next) { 457be081cd6SPeter Brune next = link->next; 458be081cd6SPeter Brune ierr = PetscFree(link);CHKERRQ(ierr); 459be081cd6SPeter Brune } 4600298fd71SBarry Smith (*dm)->subdomainhook = NULL; 461be081cd6SPeter Brune } 462baf369e7SPeter Brune { 463baf369e7SPeter Brune DMGlobalToLocalHookLink link,next; 464baf369e7SPeter Brune for (link=(*dm)->gtolhook; link; link=next) { 465baf369e7SPeter Brune next = link->next; 466baf369e7SPeter Brune ierr = PetscFree(link);CHKERRQ(ierr); 467baf369e7SPeter Brune } 4680298fd71SBarry Smith (*dm)->gtolhook = NULL; 469baf369e7SPeter Brune } 470aa1993deSMatthew G Knepley /* Destroy the work arrays */ 471aa1993deSMatthew G Knepley { 472aa1993deSMatthew G Knepley DMWorkLink link,next; 473aa1993deSMatthew G Knepley if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out"); 474aa1993deSMatthew G Knepley for (link=(*dm)->workin; link; link=next) { 475aa1993deSMatthew G Knepley next = link->next; 476aa1993deSMatthew G Knepley ierr = PetscFree(link->mem);CHKERRQ(ierr); 477aa1993deSMatthew G Knepley ierr = PetscFree(link);CHKERRQ(ierr); 478aa1993deSMatthew G Knepley } 4790298fd71SBarry Smith (*dm)->workin = NULL; 480aa1993deSMatthew G Knepley } 481b17ce1afSJed Brown 48252536dc3SBarry Smith ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr); 48352536dc3SBarry Smith ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr); 48452536dc3SBarry Smith ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr); 48552536dc3SBarry Smith 4861a266240SBarry Smith if ((*dm)->ctx && (*dm)->ctxdestroy) { 4871a266240SBarry Smith ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr); 4881a266240SBarry Smith } 48987e657c6SBarry Smith ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr); 49071cd77b2SBarry Smith ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr); 4914dcab191SBarry Smith ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr); 4926bf464f9SBarry Smith ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr); 4936bf464f9SBarry Smith ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmapb);CHKERRQ(ierr); 4946bf464f9SBarry Smith ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr); 495073dac72SJed Brown ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr); 49688ed4aceSMatthew G Knepley 49788ed4aceSMatthew G Knepley ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr); 49888ed4aceSMatthew G Knepley ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr); 4998b1ab98fSJed Brown ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr); 50088ed4aceSMatthew G Knepley ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr); 50188ed4aceSMatthew G Knepley ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr); 502af122d2aSMatthew G Knepley 5036636e97aSMatthew G Knepley ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr); 5046636e97aSMatthew G Knepley ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr); 5056636e97aSMatthew G Knepley ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr); 5066636e97aSMatthew G Knepley 507af122d2aSMatthew G Knepley for (f = 0; f < (*dm)->numFields; ++f) { 508af122d2aSMatthew G Knepley ierr = PetscObjectDestroy(&(*dm)->fields[f]);CHKERRQ(ierr); 509af122d2aSMatthew G Knepley } 510af122d2aSMatthew G Knepley ierr = PetscFree((*dm)->fields);CHKERRQ(ierr); 511e04113cfSBarry Smith /* if memory was published with SAWs then destroy it */ 512e04113cfSBarry Smith ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr); 513732e2eb9SMatthew G Knepley 5146bf464f9SBarry Smith ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr); 515435a35e8SMatthew G Knepley /* We do not destroy (*dm)->data here so that we can reference count backend objects */ 516732e2eb9SMatthew G Knepley ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr); 51747c6ae99SBarry Smith PetscFunctionReturn(0); 51847c6ae99SBarry Smith } 51947c6ae99SBarry Smith 52047c6ae99SBarry Smith #undef __FUNCT__ 521d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp" 522d7bf68aeSBarry Smith /*@ 523d7bf68aeSBarry Smith DMSetUp - sets up the data structures inside a DM object 524d7bf68aeSBarry Smith 525d7bf68aeSBarry Smith Collective on DM 526d7bf68aeSBarry Smith 527d7bf68aeSBarry Smith Input Parameter: 528d7bf68aeSBarry Smith . dm - the DM object to setup 529d7bf68aeSBarry Smith 530d7bf68aeSBarry Smith Level: developer 531d7bf68aeSBarry Smith 532e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix() 533d7bf68aeSBarry Smith 534d7bf68aeSBarry Smith @*/ 5357087cfbeSBarry Smith PetscErrorCode DMSetUp(DM dm) 536d7bf68aeSBarry Smith { 537d7bf68aeSBarry Smith PetscErrorCode ierr; 538d7bf68aeSBarry Smith 539d7bf68aeSBarry Smith PetscFunctionBegin; 540171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 5418387afaaSJed Brown if (dm->setupcalled) PetscFunctionReturn(0); 542d7bf68aeSBarry Smith if (dm->ops->setup) { 543d7bf68aeSBarry Smith ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr); 544d7bf68aeSBarry Smith } 5458387afaaSJed Brown dm->setupcalled = PETSC_TRUE; 546d7bf68aeSBarry Smith PetscFunctionReturn(0); 547d7bf68aeSBarry Smith } 548d7bf68aeSBarry Smith 549d7bf68aeSBarry Smith #undef __FUNCT__ 550d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions" 551d7bf68aeSBarry Smith /*@ 552d7bf68aeSBarry Smith DMSetFromOptions - sets parameters in a DM from the options database 553d7bf68aeSBarry Smith 554d7bf68aeSBarry Smith Collective on DM 555d7bf68aeSBarry Smith 556d7bf68aeSBarry Smith Input Parameter: 557d7bf68aeSBarry Smith . dm - the DM object to set options for 558d7bf68aeSBarry Smith 559732e2eb9SMatthew G Knepley Options Database: 560dd85299cSBarry Smith + -dm_preallocate_only: Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros 561dd85299cSBarry Smith . -dm_vec_type <type> type of vector to create inside DM 562171400e9SBarry Smith . -dm_mat_type <type> type of matrix to create inside DM 563171400e9SBarry Smith - -dm_coloring_type <global or ghosted> 564732e2eb9SMatthew G Knepley 565d7bf68aeSBarry Smith Level: developer 566d7bf68aeSBarry Smith 567e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix() 568d7bf68aeSBarry Smith 569d7bf68aeSBarry Smith @*/ 5707087cfbeSBarry Smith PetscErrorCode DMSetFromOptions(DM dm) 571d7bf68aeSBarry Smith { 5727781c08eSBarry Smith char typeName[256]; 573ca266f36SBarry Smith PetscBool flg; 574d7bf68aeSBarry Smith PetscErrorCode ierr; 575d7bf68aeSBarry Smith 576d7bf68aeSBarry Smith PetscFunctionBegin; 577171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 5783194b578SJed Brown ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr); 5790298fd71SBarry 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); 580a264d7a6SBarry Smith ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr); 581f9ba7244SBarry Smith if (flg) { 582f9ba7244SBarry Smith ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr); 583f9ba7244SBarry Smith } 584a264d7a6SBarry Smith ierr = PetscOptionsFList("-dm_mat_type","Matrix type used for created matrices","DMSetMatType",MatList,dm->mattype ? dm->mattype : typeName,typeName,sizeof(typeName),&flg);CHKERRQ(ierr); 585073dac72SJed Brown if (flg) { 586521d9a4cSLisandro Dalcin ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr); 587073dac72SJed Brown } 5880298fd71SBarry Smith ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","ISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr); 589f9ba7244SBarry Smith if (dm->ops->setfromoptions) { 590f9ba7244SBarry Smith ierr = (*dm->ops->setfromoptions)(dm);CHKERRQ(ierr); 591f9ba7244SBarry Smith } 592f9ba7244SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 593f9ba7244SBarry Smith ierr = PetscObjectProcessOptionsHandlers((PetscObject) dm);CHKERRQ(ierr); 59482fcb398SMatthew G Knepley ierr = PetscOptionsEnd();CHKERRQ(ierr); 595d7bf68aeSBarry Smith PetscFunctionReturn(0); 596d7bf68aeSBarry Smith } 597d7bf68aeSBarry Smith 598d7bf68aeSBarry Smith #undef __FUNCT__ 59947c6ae99SBarry Smith #define __FUNCT__ "DMView" 600fc9bc008SSatish Balay /*@C 601aa219208SBarry Smith DMView - Views a vector packer or DMDA. 60247c6ae99SBarry Smith 60347c6ae99SBarry Smith Collective on DM 60447c6ae99SBarry Smith 60547c6ae99SBarry Smith Input Parameter: 60647c6ae99SBarry Smith + dm - the DM object to view 60747c6ae99SBarry Smith - v - the viewer 60847c6ae99SBarry Smith 60947c6ae99SBarry Smith Level: developer 61047c6ae99SBarry Smith 611e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix() 61247c6ae99SBarry Smith 61347c6ae99SBarry Smith @*/ 6147087cfbeSBarry Smith PetscErrorCode DMView(DM dm,PetscViewer v) 61547c6ae99SBarry Smith { 61647c6ae99SBarry Smith PetscErrorCode ierr; 61732c0f0efSBarry Smith PetscBool isbinary; 61847c6ae99SBarry Smith 61947c6ae99SBarry Smith PetscFunctionBegin; 620171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 6213014e516SBarry Smith if (!v) { 622ce94432eSBarry Smith ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr); 6233014e516SBarry Smith } 62432c0f0efSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 62532c0f0efSBarry Smith if (isbinary) { 62655849f57SBarry Smith PetscInt classid = DM_FILE_CLASSID; 62732c0f0efSBarry Smith char type[256]; 62832c0f0efSBarry Smith 62932c0f0efSBarry Smith ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 63032c0f0efSBarry Smith ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr); 63132c0f0efSBarry Smith ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr); 63232c0f0efSBarry Smith } 6330c010503SBarry Smith if (dm->ops->view) { 6340c010503SBarry Smith ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr); 63547c6ae99SBarry Smith } 63647c6ae99SBarry Smith PetscFunctionReturn(0); 63747c6ae99SBarry Smith } 63847c6ae99SBarry Smith 63947c6ae99SBarry Smith #undef __FUNCT__ 64047c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector" 64147c6ae99SBarry Smith /*@ 642aa219208SBarry Smith DMCreateGlobalVector - Creates a global vector from a DMDA or DMComposite object 64347c6ae99SBarry Smith 64447c6ae99SBarry Smith Collective on DM 64547c6ae99SBarry Smith 64647c6ae99SBarry Smith Input Parameter: 64747c6ae99SBarry Smith . dm - the DM object 64847c6ae99SBarry Smith 64947c6ae99SBarry Smith Output Parameter: 65047c6ae99SBarry Smith . vec - the global vector 65147c6ae99SBarry Smith 652073dac72SJed Brown Level: beginner 65347c6ae99SBarry Smith 654e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix() 65547c6ae99SBarry Smith 65647c6ae99SBarry Smith @*/ 6577087cfbeSBarry Smith PetscErrorCode DMCreateGlobalVector(DM dm,Vec *vec) 65847c6ae99SBarry Smith { 65947c6ae99SBarry Smith PetscErrorCode ierr; 66047c6ae99SBarry Smith 66147c6ae99SBarry Smith PetscFunctionBegin; 662171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 66347c6ae99SBarry Smith ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr); 66447c6ae99SBarry Smith PetscFunctionReturn(0); 66547c6ae99SBarry Smith } 66647c6ae99SBarry Smith 66747c6ae99SBarry Smith #undef __FUNCT__ 66847c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector" 66947c6ae99SBarry Smith /*@ 670aa219208SBarry Smith DMCreateLocalVector - Creates a local vector from a DMDA or DMComposite object 67147c6ae99SBarry Smith 67247c6ae99SBarry Smith Not Collective 67347c6ae99SBarry Smith 67447c6ae99SBarry Smith Input Parameter: 67547c6ae99SBarry Smith . dm - the DM object 67647c6ae99SBarry Smith 67747c6ae99SBarry Smith Output Parameter: 67847c6ae99SBarry Smith . vec - the local vector 67947c6ae99SBarry Smith 680073dac72SJed Brown Level: beginner 68147c6ae99SBarry Smith 682e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix() 68347c6ae99SBarry Smith 68447c6ae99SBarry Smith @*/ 6857087cfbeSBarry Smith PetscErrorCode DMCreateLocalVector(DM dm,Vec *vec) 68647c6ae99SBarry Smith { 68747c6ae99SBarry Smith PetscErrorCode ierr; 68847c6ae99SBarry Smith 68947c6ae99SBarry Smith PetscFunctionBegin; 690171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 69147c6ae99SBarry Smith ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr); 69247c6ae99SBarry Smith PetscFunctionReturn(0); 69347c6ae99SBarry Smith } 69447c6ae99SBarry Smith 69547c6ae99SBarry Smith #undef __FUNCT__ 6961411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping" 6971411c6eeSJed Brown /*@ 6981411c6eeSJed Brown DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM. 6991411c6eeSJed Brown 7001411c6eeSJed Brown Collective on DM 7011411c6eeSJed Brown 7021411c6eeSJed Brown Input Parameter: 7031411c6eeSJed Brown . dm - the DM that provides the mapping 7041411c6eeSJed Brown 7051411c6eeSJed Brown Output Parameter: 7061411c6eeSJed Brown . ltog - the mapping 7071411c6eeSJed Brown 7081411c6eeSJed Brown Level: intermediate 7091411c6eeSJed Brown 7101411c6eeSJed Brown Notes: 7111411c6eeSJed Brown This mapping can then be used by VecSetLocalToGlobalMapping() or 7121411c6eeSJed Brown MatSetLocalToGlobalMapping(). 7131411c6eeSJed Brown 7141411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMappingBlock() 7151411c6eeSJed Brown @*/ 7167087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog) 7171411c6eeSJed Brown { 7181411c6eeSJed Brown PetscErrorCode ierr; 7191411c6eeSJed Brown 7201411c6eeSJed Brown PetscFunctionBegin; 7211411c6eeSJed Brown PetscValidHeaderSpecific(dm,DM_CLASSID,1); 7221411c6eeSJed Brown PetscValidPointer(ltog,2); 7231411c6eeSJed Brown if (!dm->ltogmap) { 72437d0c07bSMatthew G Knepley PetscSection section, sectionGlobal; 72537d0c07bSMatthew G Knepley 72637d0c07bSMatthew G Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 72737d0c07bSMatthew G Knepley if (section) { 72837d0c07bSMatthew G Knepley PetscInt *ltog; 72937d0c07bSMatthew G Knepley PetscInt pStart, pEnd, size, p, l; 73037d0c07bSMatthew G Knepley 73137d0c07bSMatthew G Knepley ierr = DMGetDefaultGlobalSection(dm, §ionGlobal);CHKERRQ(ierr); 73237d0c07bSMatthew G Knepley ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr); 73337d0c07bSMatthew G Knepley ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr); 734785e854fSJed Brown ierr = PetscMalloc1(size, <og);CHKERRQ(ierr); /* We want the local+overlap size */ 73537d0c07bSMatthew G Knepley for (p = pStart, l = 0; p < pEnd; ++p) { 73637d0c07bSMatthew G Knepley PetscInt dof, off, c; 73737d0c07bSMatthew G Knepley 73837d0c07bSMatthew G Knepley /* Should probably use constrained dofs */ 73937d0c07bSMatthew G Knepley ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr); 74037d0c07bSMatthew G Knepley ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr); 74137d0c07bSMatthew G Knepley for (c = 0; c < dof; ++c, ++l) { 74237d0c07bSMatthew G Knepley ltog[l] = off+c; 74337d0c07bSMatthew G Knepley } 74437d0c07bSMatthew G Knepley } 74537d0c07bSMatthew G Knepley ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_SELF, size, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr); 7463bb1ff40SBarry Smith ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr); 74737d0c07bSMatthew G Knepley } else { 748184d77edSJed Brown if (!dm->ops->getlocaltoglobalmapping) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping"); 749184d77edSJed Brown ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr); 7501411c6eeSJed Brown } 75137d0c07bSMatthew G Knepley } 7521411c6eeSJed Brown *ltog = dm->ltogmap; 7531411c6eeSJed Brown PetscFunctionReturn(0); 7541411c6eeSJed Brown } 7551411c6eeSJed Brown 7561411c6eeSJed Brown #undef __FUNCT__ 7571411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMappingBlock" 7581411c6eeSJed Brown /*@ 7591411c6eeSJed Brown DMGetLocalToGlobalMappingBlock - Accesses the blocked local-to-global mapping in a DM. 7601411c6eeSJed Brown 7611411c6eeSJed Brown Collective on DM 7621411c6eeSJed Brown 7631411c6eeSJed Brown Input Parameter: 7641411c6eeSJed Brown . da - the distributed array that provides the mapping 7651411c6eeSJed Brown 7661411c6eeSJed Brown Output Parameter: 7671411c6eeSJed Brown . ltog - the block mapping 7681411c6eeSJed Brown 7691411c6eeSJed Brown Level: intermediate 7701411c6eeSJed Brown 7711411c6eeSJed Brown Notes: 7721411c6eeSJed Brown This mapping can then be used by VecSetLocalToGlobalMappingBlock() or 7731411c6eeSJed Brown MatSetLocalToGlobalMappingBlock(). 7741411c6eeSJed Brown 7751411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMapping(), DMGetBlockSize(), VecSetBlockSize(), MatSetBlockSize() 7761411c6eeSJed Brown @*/ 7777087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMappingBlock(DM dm,ISLocalToGlobalMapping *ltog) 7781411c6eeSJed Brown { 7791411c6eeSJed Brown PetscErrorCode ierr; 7801411c6eeSJed Brown 7811411c6eeSJed Brown PetscFunctionBegin; 7821411c6eeSJed Brown PetscValidHeaderSpecific(dm,DM_CLASSID,1); 7831411c6eeSJed Brown PetscValidPointer(ltog,2); 7841411c6eeSJed Brown if (!dm->ltogmapb) { 7851411c6eeSJed Brown PetscInt bs; 7861411c6eeSJed Brown ierr = DMGetBlockSize(dm,&bs);CHKERRQ(ierr); 7871411c6eeSJed Brown if (bs > 1) { 788184d77edSJed Brown if (!dm->ops->getlocaltoglobalmappingblock) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMappingBlock"); 789184d77edSJed Brown ierr = (*dm->ops->getlocaltoglobalmappingblock)(dm);CHKERRQ(ierr); 7901411c6eeSJed Brown } else { 7911411c6eeSJed Brown ierr = DMGetLocalToGlobalMapping(dm,&dm->ltogmapb);CHKERRQ(ierr); 7921411c6eeSJed Brown ierr = PetscObjectReference((PetscObject)dm->ltogmapb);CHKERRQ(ierr); 7931411c6eeSJed Brown } 7941411c6eeSJed Brown } 7951411c6eeSJed Brown *ltog = dm->ltogmapb; 7961411c6eeSJed Brown PetscFunctionReturn(0); 7971411c6eeSJed Brown } 7981411c6eeSJed Brown 7991411c6eeSJed Brown #undef __FUNCT__ 8001411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize" 8011411c6eeSJed Brown /*@ 8021411c6eeSJed Brown DMGetBlockSize - Gets the inherent block size associated with a DM 8031411c6eeSJed Brown 8041411c6eeSJed Brown Not Collective 8051411c6eeSJed Brown 8061411c6eeSJed Brown Input Parameter: 8071411c6eeSJed Brown . dm - the DM with block structure 8081411c6eeSJed Brown 8091411c6eeSJed Brown Output Parameter: 8101411c6eeSJed Brown . bs - the block size, 1 implies no exploitable block structure 8111411c6eeSJed Brown 8121411c6eeSJed Brown Level: intermediate 8131411c6eeSJed Brown 8141411c6eeSJed Brown .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMappingBlock() 8151411c6eeSJed Brown @*/ 8167087cfbeSBarry Smith PetscErrorCode DMGetBlockSize(DM dm,PetscInt *bs) 8171411c6eeSJed Brown { 8181411c6eeSJed Brown PetscFunctionBegin; 8191411c6eeSJed Brown PetscValidHeaderSpecific(dm,DM_CLASSID,1); 8201411c6eeSJed Brown PetscValidPointer(bs,2); 8211411c6eeSJed 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"); 8221411c6eeSJed Brown *bs = dm->bs; 8231411c6eeSJed Brown PetscFunctionReturn(0); 8241411c6eeSJed Brown } 8251411c6eeSJed Brown 8261411c6eeSJed Brown #undef __FUNCT__ 827e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation" 82847c6ae99SBarry Smith /*@ 829e727c939SJed Brown DMCreateInterpolation - Gets interpolation matrix between two DMDA or DMComposite objects 83047c6ae99SBarry Smith 83147c6ae99SBarry Smith Collective on DM 83247c6ae99SBarry Smith 83347c6ae99SBarry Smith Input Parameter: 83447c6ae99SBarry Smith + dm1 - the DM object 83547c6ae99SBarry Smith - dm2 - the second, finer DM object 83647c6ae99SBarry Smith 83747c6ae99SBarry Smith Output Parameter: 83847c6ae99SBarry Smith + mat - the interpolation 83947c6ae99SBarry Smith - vec - the scaling (optional) 84047c6ae99SBarry Smith 84147c6ae99SBarry Smith Level: developer 84247c6ae99SBarry Smith 84385afcc9aSBarry 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 84485afcc9aSBarry Smith DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation. 845d52bd9f3SBarry Smith 8461f588964SMatthew G Knepley For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors 847d52bd9f3SBarry Smith EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic. 84885afcc9aSBarry Smith 84985afcc9aSBarry Smith 850e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen() 85147c6ae99SBarry Smith 85247c6ae99SBarry Smith @*/ 853e727c939SJed Brown PetscErrorCode DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec) 85447c6ae99SBarry Smith { 85547c6ae99SBarry Smith PetscErrorCode ierr; 85647c6ae99SBarry Smith 85747c6ae99SBarry Smith PetscFunctionBegin; 858171400e9SBarry Smith PetscValidHeaderSpecific(dm1,DM_CLASSID,1); 859171400e9SBarry Smith PetscValidHeaderSpecific(dm2,DM_CLASSID,2); 86025296bd5SBarry Smith ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr); 86147c6ae99SBarry Smith PetscFunctionReturn(0); 86247c6ae99SBarry Smith } 86347c6ae99SBarry Smith 86447c6ae99SBarry Smith #undef __FUNCT__ 865e727c939SJed Brown #define __FUNCT__ "DMCreateInjection" 86647c6ae99SBarry Smith /*@ 867e727c939SJed Brown DMCreateInjection - Gets injection matrix between two DMDA or DMComposite objects 86847c6ae99SBarry Smith 86947c6ae99SBarry Smith Collective on DM 87047c6ae99SBarry Smith 87147c6ae99SBarry Smith Input Parameter: 87247c6ae99SBarry Smith + dm1 - the DM object 87347c6ae99SBarry Smith - dm2 - the second, finer DM object 87447c6ae99SBarry Smith 87547c6ae99SBarry Smith Output Parameter: 87647c6ae99SBarry Smith . ctx - the injection 87747c6ae99SBarry Smith 87847c6ae99SBarry Smith Level: developer 87947c6ae99SBarry Smith 88085afcc9aSBarry 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 88185afcc9aSBarry Smith DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection. 88285afcc9aSBarry Smith 883e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation() 88447c6ae99SBarry Smith 88547c6ae99SBarry Smith @*/ 886e727c939SJed Brown PetscErrorCode DMCreateInjection(DM dm1,DM dm2,VecScatter *ctx) 88747c6ae99SBarry Smith { 88847c6ae99SBarry Smith PetscErrorCode ierr; 88947c6ae99SBarry Smith 89047c6ae99SBarry Smith PetscFunctionBegin; 891171400e9SBarry Smith PetscValidHeaderSpecific(dm1,DM_CLASSID,1); 892171400e9SBarry Smith PetscValidHeaderSpecific(dm2,DM_CLASSID,2); 89347c6ae99SBarry Smith ierr = (*dm1->ops->getinjection)(dm1,dm2,ctx);CHKERRQ(ierr); 89447c6ae99SBarry Smith PetscFunctionReturn(0); 89547c6ae99SBarry Smith } 89647c6ae99SBarry Smith 89747c6ae99SBarry Smith #undef __FUNCT__ 898e727c939SJed Brown #define __FUNCT__ "DMCreateColoring" 899b412c318SBarry Smith /*@ 900b412c318SBarry Smith DMCreateColoring - Gets coloring for a DM 90147c6ae99SBarry Smith 90247c6ae99SBarry Smith Collective on DM 90347c6ae99SBarry Smith 90447c6ae99SBarry Smith Input Parameter: 90547c6ae99SBarry Smith + dm - the DM object 906b412c318SBarry Smith - ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL 90747c6ae99SBarry Smith 90847c6ae99SBarry Smith Output Parameter: 90947c6ae99SBarry Smith . coloring - the coloring 91047c6ae99SBarry Smith 91147c6ae99SBarry Smith Level: developer 91247c6ae99SBarry Smith 913b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType() 91447c6ae99SBarry Smith 915aab9d709SJed Brown @*/ 916b412c318SBarry Smith PetscErrorCode DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring) 91747c6ae99SBarry Smith { 91847c6ae99SBarry Smith PetscErrorCode ierr; 91947c6ae99SBarry Smith 92047c6ae99SBarry Smith PetscFunctionBegin; 921171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 922ce94432eSBarry Smith if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet"); 923b412c318SBarry Smith ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr); 92447c6ae99SBarry Smith PetscFunctionReturn(0); 92547c6ae99SBarry Smith } 92647c6ae99SBarry Smith 92747c6ae99SBarry Smith #undef __FUNCT__ 928950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix" 929b412c318SBarry Smith /*@ 930950540a4SJed Brown DMCreateMatrix - Gets empty Jacobian for a DMDA or DMComposite 93147c6ae99SBarry Smith 93247c6ae99SBarry Smith Collective on DM 93347c6ae99SBarry Smith 93447c6ae99SBarry Smith Input Parameter: 935b412c318SBarry Smith . dm - the DM object 93647c6ae99SBarry Smith 93747c6ae99SBarry Smith Output Parameter: 93847c6ae99SBarry Smith . mat - the empty Jacobian 93947c6ae99SBarry Smith 940073dac72SJed Brown Level: beginner 94147c6ae99SBarry Smith 94294013140SBarry Smith Notes: This properly preallocates the number of nonzeros in the sparse matrix so you 94394013140SBarry Smith do not need to do it yourself. 94494013140SBarry Smith 94594013140SBarry Smith By default it also sets the nonzero structure and puts in the zero entries. To prevent setting 946aa219208SBarry Smith the nonzero pattern call DMDASetMatPreallocateOnly() 94794013140SBarry Smith 94894013140SBarry 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 94994013140SBarry Smith internally by PETSc. 95094013140SBarry Smith 95194013140SBarry Smith For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires 952aa219208SBarry Smith the indices for the global numbering for DMDAs which is complicated. 95394013140SBarry Smith 954b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType() 95547c6ae99SBarry Smith 956aab9d709SJed Brown @*/ 957b412c318SBarry Smith PetscErrorCode DMCreateMatrix(DM dm,Mat *mat) 95847c6ae99SBarry Smith { 95947c6ae99SBarry Smith PetscErrorCode ierr; 96047c6ae99SBarry Smith 96147c6ae99SBarry Smith PetscFunctionBegin; 962171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 963607a6623SBarry Smith ierr = MatInitializePackage();CHKERRQ(ierr); 964c7b7c8a4SJed Brown PetscValidHeaderSpecific(dm,DM_CLASSID,1); 965c7b7c8a4SJed Brown PetscValidPointer(mat,3); 966b412c318SBarry Smith ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr); 96747c6ae99SBarry Smith PetscFunctionReturn(0); 96847c6ae99SBarry Smith } 96947c6ae99SBarry Smith 97047c6ae99SBarry Smith #undef __FUNCT__ 971732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly" 972732e2eb9SMatthew G Knepley /*@ 973950540a4SJed Brown DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly 974732e2eb9SMatthew G Knepley preallocated but the nonzero structure and zero values will not be set. 975732e2eb9SMatthew G Knepley 976732e2eb9SMatthew G Knepley Logically Collective on DMDA 977732e2eb9SMatthew G Knepley 978732e2eb9SMatthew G Knepley Input Parameter: 979732e2eb9SMatthew G Knepley + dm - the DM 980732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation 981732e2eb9SMatthew G Knepley 982732e2eb9SMatthew G Knepley Level: developer 983950540a4SJed Brown .seealso DMCreateMatrix() 984732e2eb9SMatthew G Knepley @*/ 985732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only) 986732e2eb9SMatthew G Knepley { 987732e2eb9SMatthew G Knepley PetscFunctionBegin; 988732e2eb9SMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 989732e2eb9SMatthew G Knepley dm->prealloc_only = only; 990732e2eb9SMatthew G Knepley PetscFunctionReturn(0); 991732e2eb9SMatthew G Knepley } 992732e2eb9SMatthew G Knepley 993732e2eb9SMatthew G Knepley #undef __FUNCT__ 994a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray" 995a89ea682SMatthew G Knepley /*@C 996aa1993deSMatthew G Knepley DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray() 997a89ea682SMatthew G Knepley 998a89ea682SMatthew G Knepley Not Collective 999a89ea682SMatthew G Knepley 1000a89ea682SMatthew G Knepley Input Parameters: 1001a89ea682SMatthew G Knepley + dm - the DM object 1002aa1993deSMatthew G Knepley . count - The minium size 1003aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT) 1004a89ea682SMatthew G Knepley 1005a89ea682SMatthew G Knepley Output Parameter: 1006a89ea682SMatthew G Knepley . array - the work array 1007a89ea682SMatthew G Knepley 1008a89ea682SMatthew G Knepley Level: developer 1009a89ea682SMatthew G Knepley 1010a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate() 1011a89ea682SMatthew G Knepley @*/ 1012aa1993deSMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem) 1013a89ea682SMatthew G Knepley { 1014a89ea682SMatthew G Knepley PetscErrorCode ierr; 1015aa1993deSMatthew G Knepley DMWorkLink link; 1016aa1993deSMatthew G Knepley size_t size; 1017a89ea682SMatthew G Knepley 1018a89ea682SMatthew G Knepley PetscFunctionBegin; 1019a89ea682SMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 1020aa1993deSMatthew G Knepley PetscValidPointer(mem,4); 1021aa1993deSMatthew G Knepley if (dm->workin) { 1022aa1993deSMatthew G Knepley link = dm->workin; 1023aa1993deSMatthew G Knepley dm->workin = dm->workin->next; 1024aa1993deSMatthew G Knepley } else { 1025b00a9115SJed Brown ierr = PetscNewLog(dm,&link);CHKERRQ(ierr); 1026a89ea682SMatthew G Knepley } 1027aa1993deSMatthew G Knepley ierr = PetscDataTypeGetSize(dtype,&size);CHKERRQ(ierr); 1028aa1993deSMatthew G Knepley if (size*count > link->bytes) { 1029aa1993deSMatthew G Knepley ierr = PetscFree(link->mem);CHKERRQ(ierr); 1030aa1993deSMatthew G Knepley ierr = PetscMalloc(size*count,&link->mem);CHKERRQ(ierr); 1031aa1993deSMatthew G Knepley link->bytes = size*count; 1032aa1993deSMatthew G Knepley } 1033aa1993deSMatthew G Knepley link->next = dm->workout; 1034aa1993deSMatthew G Knepley dm->workout = link; 1035aa1993deSMatthew G Knepley *(void**)mem = link->mem; 1036a89ea682SMatthew G Knepley PetscFunctionReturn(0); 1037a89ea682SMatthew G Knepley } 1038a89ea682SMatthew G Knepley 1039aa1993deSMatthew G Knepley #undef __FUNCT__ 1040aa1993deSMatthew G Knepley #define __FUNCT__ "DMRestoreWorkArray" 1041aa1993deSMatthew G Knepley /*@C 1042aa1993deSMatthew G Knepley DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray() 1043aa1993deSMatthew G Knepley 1044aa1993deSMatthew G Knepley Not Collective 1045aa1993deSMatthew G Knepley 1046aa1993deSMatthew G Knepley Input Parameters: 1047aa1993deSMatthew G Knepley + dm - the DM object 1048aa1993deSMatthew G Knepley . count - The minium size 1049aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT) 1050aa1993deSMatthew G Knepley 1051aa1993deSMatthew G Knepley Output Parameter: 1052aa1993deSMatthew G Knepley . array - the work array 1053aa1993deSMatthew G Knepley 1054aa1993deSMatthew G Knepley Level: developer 1055aa1993deSMatthew G Knepley 1056aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate() 1057aa1993deSMatthew G Knepley @*/ 1058aa1993deSMatthew G Knepley PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem) 1059aa1993deSMatthew G Knepley { 1060aa1993deSMatthew G Knepley DMWorkLink *p,link; 1061aa1993deSMatthew G Knepley 1062aa1993deSMatthew G Knepley PetscFunctionBegin; 1063aa1993deSMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 1064aa1993deSMatthew G Knepley PetscValidPointer(mem,4); 1065aa1993deSMatthew G Knepley for (p=&dm->workout; (link=*p); p=&link->next) { 1066aa1993deSMatthew G Knepley if (link->mem == *(void**)mem) { 1067aa1993deSMatthew G Knepley *p = link->next; 1068aa1993deSMatthew G Knepley link->next = dm->workin; 1069aa1993deSMatthew G Knepley dm->workin = link; 10700298fd71SBarry Smith *(void**)mem = NULL; 1071aa1993deSMatthew G Knepley PetscFunctionReturn(0); 1072aa1993deSMatthew G Knepley } 1073aa1993deSMatthew G Knepley } 1074aa1993deSMatthew G Knepley SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out"); 1075aa1993deSMatthew G Knepley PetscFunctionReturn(0); 1076aa1993deSMatthew G Knepley } 1077e7c4fc90SDmitry Karpeev 1078e7c4fc90SDmitry Karpeev #undef __FUNCT__ 1079435a35e8SMatthew G Knepley #define __FUNCT__ "DMSetNullSpaceConstructor" 1080435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace)) 1081435a35e8SMatthew G Knepley { 1082435a35e8SMatthew G Knepley PetscFunctionBegin; 1083435a35e8SMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 108482f516ccSBarry Smith if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field); 1085435a35e8SMatthew G Knepley dm->nullspaceConstructors[field] = nullsp; 1086435a35e8SMatthew G Knepley PetscFunctionReturn(0); 1087435a35e8SMatthew G Knepley } 1088435a35e8SMatthew G Knepley 1089435a35e8SMatthew G Knepley #undef __FUNCT__ 10904d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS" 10914f3b5142SJed Brown /*@C 10924d343eeaSMatthew G Knepley DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field 10934d343eeaSMatthew G Knepley 10944d343eeaSMatthew G Knepley Not collective 10954d343eeaSMatthew G Knepley 10964d343eeaSMatthew G Knepley Input Parameter: 10974d343eeaSMatthew G Knepley . dm - the DM object 10984d343eeaSMatthew G Knepley 10994d343eeaSMatthew G Knepley Output Parameters: 11000298fd71SBarry Smith + numFields - The number of fields (or NULL if not requested) 11010298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested) 11020298fd71SBarry Smith - fields - The global indices for each field (or NULL if not requested) 11034d343eeaSMatthew G Knepley 11044d343eeaSMatthew G Knepley Level: intermediate 11054d343eeaSMatthew G Knepley 110621c9b008SJed Brown Notes: 110721c9b008SJed Brown The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with 110821c9b008SJed Brown PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with 110921c9b008SJed Brown PetscFree(). 111021c9b008SJed Brown 11114d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix() 11124d343eeaSMatthew G Knepley @*/ 111337d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields) 11144d343eeaSMatthew G Knepley { 111537d0c07bSMatthew G Knepley PetscSection section, sectionGlobal; 11164d343eeaSMatthew G Knepley PetscErrorCode ierr; 11174d343eeaSMatthew G Knepley 11184d343eeaSMatthew G Knepley PetscFunctionBegin; 11194d343eeaSMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 112069ca1f37SDmitry Karpeev if (numFields) { 112169ca1f37SDmitry Karpeev PetscValidPointer(numFields,2); 112269ca1f37SDmitry Karpeev *numFields = 0; 112369ca1f37SDmitry Karpeev } 112437d0c07bSMatthew G Knepley if (fieldNames) { 112537d0c07bSMatthew G Knepley PetscValidPointer(fieldNames,3); 11260298fd71SBarry Smith *fieldNames = NULL; 112769ca1f37SDmitry Karpeev } 112869ca1f37SDmitry Karpeev if (fields) { 112969ca1f37SDmitry Karpeev PetscValidPointer(fields,4); 11300298fd71SBarry Smith *fields = NULL; 113169ca1f37SDmitry Karpeev } 113237d0c07bSMatthew G Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 113337d0c07bSMatthew G Knepley if (section) { 113437d0c07bSMatthew G Knepley PetscInt *fieldSizes, **fieldIndices; 113537d0c07bSMatthew G Knepley PetscInt nF, f, pStart, pEnd, p; 113637d0c07bSMatthew G Knepley 113737d0c07bSMatthew G Knepley ierr = DMGetDefaultGlobalSection(dm, §ionGlobal);CHKERRQ(ierr); 113837d0c07bSMatthew G Knepley ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr); 1139dcca6d9dSJed Brown ierr = PetscMalloc2(nF,&fieldSizes,nF,&fieldIndices);CHKERRQ(ierr); 114037d0c07bSMatthew G Knepley ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr); 114137d0c07bSMatthew G Knepley for (f = 0; f < nF; ++f) { 114237d0c07bSMatthew G Knepley fieldSizes[f] = 0; 114337d0c07bSMatthew G Knepley } 114437d0c07bSMatthew G Knepley for (p = pStart; p < pEnd; ++p) { 114537d0c07bSMatthew G Knepley PetscInt gdof; 114637d0c07bSMatthew G Knepley 114737d0c07bSMatthew G Knepley ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr); 114837d0c07bSMatthew G Knepley if (gdof > 0) { 114937d0c07bSMatthew G Knepley for (f = 0; f < nF; ++f) { 115037d0c07bSMatthew G Knepley PetscInt fdof, fcdof; 115137d0c07bSMatthew G Knepley 115237d0c07bSMatthew G Knepley ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr); 115337d0c07bSMatthew G Knepley ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr); 115437d0c07bSMatthew G Knepley fieldSizes[f] += fdof-fcdof; 115537d0c07bSMatthew G Knepley } 115637d0c07bSMatthew G Knepley } 115737d0c07bSMatthew G Knepley } 115837d0c07bSMatthew G Knepley for (f = 0; f < nF; ++f) { 1159785e854fSJed Brown ierr = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr); 116037d0c07bSMatthew G Knepley fieldSizes[f] = 0; 116137d0c07bSMatthew G Knepley } 116237d0c07bSMatthew G Knepley for (p = pStart; p < pEnd; ++p) { 116337d0c07bSMatthew G Knepley PetscInt gdof, goff; 116437d0c07bSMatthew G Knepley 116537d0c07bSMatthew G Knepley ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr); 116637d0c07bSMatthew G Knepley if (gdof > 0) { 116737d0c07bSMatthew G Knepley ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr); 116837d0c07bSMatthew G Knepley for (f = 0; f < nF; ++f) { 116937d0c07bSMatthew G Knepley PetscInt fdof, fcdof, fc; 117037d0c07bSMatthew G Knepley 117137d0c07bSMatthew G Knepley ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr); 117237d0c07bSMatthew G Knepley ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr); 117337d0c07bSMatthew G Knepley for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) { 117437d0c07bSMatthew G Knepley fieldIndices[f][fieldSizes[f]] = goff++; 117537d0c07bSMatthew G Knepley } 117637d0c07bSMatthew G Knepley } 117737d0c07bSMatthew G Knepley } 117837d0c07bSMatthew G Knepley } 11798865f1eaSKarl Rupp if (numFields) *numFields = nF; 118037d0c07bSMatthew G Knepley if (fieldNames) { 1181785e854fSJed Brown ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr); 118237d0c07bSMatthew G Knepley for (f = 0; f < nF; ++f) { 118337d0c07bSMatthew G Knepley const char *fieldName; 118437d0c07bSMatthew G Knepley 118537d0c07bSMatthew G Knepley ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr); 118637d0c07bSMatthew G Knepley ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr); 118737d0c07bSMatthew G Knepley } 118837d0c07bSMatthew G Knepley } 118937d0c07bSMatthew G Knepley if (fields) { 1190785e854fSJed Brown ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr); 119137d0c07bSMatthew G Knepley for (f = 0; f < nF; ++f) { 119282f516ccSBarry Smith ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr); 119337d0c07bSMatthew G Knepley } 119437d0c07bSMatthew G Knepley } 119537d0c07bSMatthew G Knepley ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr); 11968865f1eaSKarl Rupp } else if (dm->ops->createfieldis) { 11978865f1eaSKarl Rupp ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr); 119869ca1f37SDmitry Karpeev } 11994d343eeaSMatthew G Knepley PetscFunctionReturn(0); 12004d343eeaSMatthew G Knepley } 12014d343eeaSMatthew G Knepley 120216621825SDmitry Karpeev 120316621825SDmitry Karpeev #undef __FUNCT__ 120416621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecomposition" 120516621825SDmitry Karpeev /*@C 120616621825SDmitry Karpeev DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems 120716621825SDmitry Karpeev corresponding to different fields: each IS contains the global indices of the dofs of the 120816621825SDmitry Karpeev corresponding field. The optional list of DMs define the DM for each subproblem. 1209e7c4fc90SDmitry Karpeev Generalizes DMCreateFieldIS(). 1210e7c4fc90SDmitry Karpeev 1211e7c4fc90SDmitry Karpeev Not collective 1212e7c4fc90SDmitry Karpeev 1213e7c4fc90SDmitry Karpeev Input Parameter: 1214e7c4fc90SDmitry Karpeev . dm - the DM object 1215e7c4fc90SDmitry Karpeev 1216e7c4fc90SDmitry Karpeev Output Parameters: 12170298fd71SBarry Smith + len - The number of subproblems in the field decomposition (or NULL if not requested) 12180298fd71SBarry Smith . namelist - The name for each field (or NULL if not requested) 12190298fd71SBarry Smith . islist - The global indices for each field (or NULL if not requested) 12200298fd71SBarry Smith - dmlist - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined) 1221e7c4fc90SDmitry Karpeev 1222e7c4fc90SDmitry Karpeev Level: intermediate 1223e7c4fc90SDmitry Karpeev 1224e7c4fc90SDmitry Karpeev Notes: 1225e7c4fc90SDmitry Karpeev The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with 1226e7c4fc90SDmitry Karpeev PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(), 1227e7c4fc90SDmitry Karpeev and all of the arrays should be freed with PetscFree(). 1228e7c4fc90SDmitry Karpeev 1229e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS() 1230e7c4fc90SDmitry Karpeev @*/ 123116621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist) 1232e7c4fc90SDmitry Karpeev { 1233e7c4fc90SDmitry Karpeev PetscErrorCode ierr; 1234e7c4fc90SDmitry Karpeev 1235e7c4fc90SDmitry Karpeev PetscFunctionBegin; 1236e7c4fc90SDmitry Karpeev PetscValidHeaderSpecific(dm,DM_CLASSID,1); 12378865f1eaSKarl Rupp if (len) { 12388865f1eaSKarl Rupp PetscValidPointer(len,2); 12398865f1eaSKarl Rupp *len = 0; 12408865f1eaSKarl Rupp } 12418865f1eaSKarl Rupp if (namelist) { 12428865f1eaSKarl Rupp PetscValidPointer(namelist,3); 12438865f1eaSKarl Rupp *namelist = 0; 12448865f1eaSKarl Rupp } 12458865f1eaSKarl Rupp if (islist) { 12468865f1eaSKarl Rupp PetscValidPointer(islist,4); 12478865f1eaSKarl Rupp *islist = 0; 12488865f1eaSKarl Rupp } 12498865f1eaSKarl Rupp if (dmlist) { 12508865f1eaSKarl Rupp PetscValidPointer(dmlist,5); 12518865f1eaSKarl Rupp *dmlist = 0; 12528865f1eaSKarl Rupp } 1253f3f0edfdSDmitry Karpeev /* 1254f3f0edfdSDmitry Karpeev Is it a good idea to apply the following check across all impls? 1255f3f0edfdSDmitry Karpeev Perhaps some impls can have a well-defined decomposition before DMSetUp? 1256f3f0edfdSDmitry Karpeev This, however, follows the general principle that accessors are not well-behaved until the object is set up. 1257f3f0edfdSDmitry Karpeev */ 1258ce94432eSBarry Smith if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp"); 125916621825SDmitry Karpeev if (!dm->ops->createfielddecomposition) { 1260435a35e8SMatthew G Knepley PetscSection section; 1261435a35e8SMatthew G Knepley PetscInt numFields, f; 1262435a35e8SMatthew G Knepley 1263435a35e8SMatthew G Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 1264435a35e8SMatthew G Knepley if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);} 1265435a35e8SMatthew G Knepley if (section && numFields && dm->ops->createsubdm) { 1266435a35e8SMatthew G Knepley *len = numFields; 1267dcca6d9dSJed Brown ierr = PetscMalloc3(numFields,namelist,numFields,islist,numFields,dmlist);CHKERRQ(ierr); 1268435a35e8SMatthew G Knepley for (f = 0; f < numFields; ++f) { 1269435a35e8SMatthew G Knepley const char *fieldName; 1270435a35e8SMatthew G Knepley 1271435a35e8SMatthew G Knepley ierr = DMCreateSubDM(dm, 1, &f, &(*islist)[f], &(*dmlist)[f]);CHKERRQ(ierr); 1272435a35e8SMatthew G Knepley ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr); 1273435a35e8SMatthew G Knepley ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr); 1274435a35e8SMatthew G Knepley } 1275435a35e8SMatthew G Knepley } else { 127669ca1f37SDmitry Karpeev ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr); 1277e7c4fc90SDmitry Karpeev /* By default there are no DMs associated with subproblems. */ 12780298fd71SBarry Smith if (dmlist) *dmlist = NULL; 1279e7c4fc90SDmitry Karpeev } 12808865f1eaSKarl Rupp } else { 128116621825SDmitry Karpeev ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr); 128216621825SDmitry Karpeev } 128316621825SDmitry Karpeev PetscFunctionReturn(0); 128416621825SDmitry Karpeev } 128516621825SDmitry Karpeev 128616621825SDmitry Karpeev #undef __FUNCT__ 1287435a35e8SMatthew G Knepley #define __FUNCT__ "DMCreateSubDM" 1288435a35e8SMatthew G Knepley /*@C 1289435a35e8SMatthew G Knepley DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in. 1290435a35e8SMatthew G Knepley The fields are defined by DMCreateFieldIS(). 1291435a35e8SMatthew G Knepley 1292435a35e8SMatthew G Knepley Not collective 1293435a35e8SMatthew G Knepley 1294435a35e8SMatthew G Knepley Input Parameters: 1295435a35e8SMatthew G Knepley + dm - the DM object 1296435a35e8SMatthew G Knepley . numFields - number of fields in this subproblem 12970298fd71SBarry Smith - len - The number of subproblems in the decomposition (or NULL if not requested) 1298435a35e8SMatthew G Knepley 1299435a35e8SMatthew G Knepley Output Parameters: 1300435a35e8SMatthew G Knepley . is - The global indices for the subproblem 1301435a35e8SMatthew G Knepley - dm - The DM for the subproblem 1302435a35e8SMatthew G Knepley 1303435a35e8SMatthew G Knepley Level: intermediate 1304435a35e8SMatthew G Knepley 1305435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS() 1306435a35e8SMatthew G Knepley @*/ 1307435a35e8SMatthew G Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm) 1308435a35e8SMatthew G Knepley { 1309435a35e8SMatthew G Knepley PetscErrorCode ierr; 1310435a35e8SMatthew G Knepley 1311435a35e8SMatthew G Knepley PetscFunctionBegin; 1312435a35e8SMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 1313435a35e8SMatthew G Knepley PetscValidPointer(fields,3); 13148865f1eaSKarl Rupp if (is) PetscValidPointer(is,4); 13158865f1eaSKarl Rupp if (subdm) PetscValidPointer(subdm,5); 1316435a35e8SMatthew G Knepley if (dm->ops->createsubdm) { 1317435a35e8SMatthew G Knepley ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr); 131882f516ccSBarry Smith } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined"); 1319435a35e8SMatthew G Knepley PetscFunctionReturn(0); 1320435a35e8SMatthew G Knepley } 1321435a35e8SMatthew G Knepley 132216621825SDmitry Karpeev 132316621825SDmitry Karpeev #undef __FUNCT__ 132416621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecomposition" 132516621825SDmitry Karpeev /*@C 13268d4ac253SDmitry Karpeev DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems 13278d4ac253SDmitry Karpeev corresponding to restrictions to pairs nested subdomains: each IS contains the global 13288d4ac253SDmitry Karpeev indices of the dofs of the corresponding subdomains. The inner subdomains conceptually 13298d4ac253SDmitry Karpeev define a nonoverlapping covering, while outer subdomains can overlap. 13308d4ac253SDmitry Karpeev The optional list of DMs define the DM for each subproblem. 133116621825SDmitry Karpeev 133216621825SDmitry Karpeev Not collective 133316621825SDmitry Karpeev 133416621825SDmitry Karpeev Input Parameter: 133516621825SDmitry Karpeev . dm - the DM object 133616621825SDmitry Karpeev 133716621825SDmitry Karpeev Output Parameters: 13380298fd71SBarry Smith + len - The number of subproblems in the domain decomposition (or NULL if not requested) 13390298fd71SBarry Smith . namelist - The name for each subdomain (or NULL if not requested) 13400298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested) 13410298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested) 13420298fd71SBarry Smith - dmlist - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined) 134316621825SDmitry Karpeev 134416621825SDmitry Karpeev Level: intermediate 134516621825SDmitry Karpeev 134616621825SDmitry Karpeev Notes: 134716621825SDmitry Karpeev The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with 134816621825SDmitry Karpeev PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(), 134916621825SDmitry Karpeev and all of the arrays should be freed with PetscFree(). 135016621825SDmitry Karpeev 13518d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition() 135216621825SDmitry Karpeev @*/ 13538d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist) 135416621825SDmitry Karpeev { 135516621825SDmitry Karpeev PetscErrorCode ierr; 1356be081cd6SPeter Brune DMSubDomainHookLink link; 1357be081cd6SPeter Brune PetscInt i,l; 135816621825SDmitry Karpeev 135916621825SDmitry Karpeev PetscFunctionBegin; 136016621825SDmitry Karpeev PetscValidHeaderSpecific(dm,DM_CLASSID,1); 136114a18fd3SPeter Brune if (len) {PetscValidPointer(len,2); *len = 0;} 13620298fd71SBarry Smith if (namelist) {PetscValidPointer(namelist,3); *namelist = NULL;} 13630298fd71SBarry Smith if (innerislist) {PetscValidPointer(innerislist,4); *innerislist = NULL;} 13640298fd71SBarry Smith if (outerislist) {PetscValidPointer(outerislist,5); *outerislist = NULL;} 13650298fd71SBarry Smith if (dmlist) {PetscValidPointer(dmlist,6); *dmlist = NULL;} 1366f3f0edfdSDmitry Karpeev /* 1367f3f0edfdSDmitry Karpeev Is it a good idea to apply the following check across all impls? 1368f3f0edfdSDmitry Karpeev Perhaps some impls can have a well-defined decomposition before DMSetUp? 1369f3f0edfdSDmitry Karpeev This, however, follows the general principle that accessors are not well-behaved until the object is set up. 1370f3f0edfdSDmitry Karpeev */ 1371ce94432eSBarry Smith if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp"); 137216621825SDmitry Karpeev if (dm->ops->createdomaindecomposition) { 1373be081cd6SPeter Brune ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr); 137414a18fd3SPeter Brune /* copy subdomain hooks and context over to the subdomain DMs */ 137514a18fd3SPeter Brune if (dmlist) { 1376be081cd6SPeter Brune for (i = 0; i < l; i++) { 1377be081cd6SPeter Brune for (link=dm->subdomainhook; link; link=link->next) { 1378be081cd6SPeter Brune if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);} 1379be081cd6SPeter Brune } 1380d425c654SPeter Brune (*dmlist)[i]->ctx = dm->ctx; 1381e7c4fc90SDmitry Karpeev } 138214a18fd3SPeter Brune } 138314a18fd3SPeter Brune if (len) *len = l; 138414a18fd3SPeter Brune } 1385e30e807fSPeter Brune PetscFunctionReturn(0); 1386e30e807fSPeter Brune } 1387e30e807fSPeter Brune 1388e30e807fSPeter Brune 1389e30e807fSPeter Brune #undef __FUNCT__ 1390e30e807fSPeter Brune #define __FUNCT__ "DMCreateDomainDecompositionScatters" 1391e30e807fSPeter Brune /*@C 1392e30e807fSPeter Brune DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector 1393e30e807fSPeter Brune 1394e30e807fSPeter Brune Not collective 1395e30e807fSPeter Brune 1396e30e807fSPeter Brune Input Parameters: 1397e30e807fSPeter Brune + dm - the DM object 1398e30e807fSPeter Brune . n - the number of subdomain scatters 1399e30e807fSPeter Brune - subdms - the local subdomains 1400e30e807fSPeter Brune 1401e30e807fSPeter Brune Output Parameters: 1402e30e807fSPeter Brune + n - the number of scatters returned 1403e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain 1404e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain 1405e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts) 1406e30e807fSPeter Brune 1407e30e807fSPeter Brune Notes: This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution 1408e30e807fSPeter Brune of general nonlinear problems with overlapping subdomain methods. While merely having index sets that enable subsets 1409e30e807fSPeter Brune of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of 1410e30e807fSPeter Brune solution and residual data. 1411e30e807fSPeter Brune 1412e30e807fSPeter Brune Level: developer 1413e30e807fSPeter Brune 1414e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS() 1415e30e807fSPeter Brune @*/ 1416e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat) 1417e30e807fSPeter Brune { 1418e30e807fSPeter Brune PetscErrorCode ierr; 1419e30e807fSPeter Brune 1420e30e807fSPeter Brune PetscFunctionBegin; 1421e30e807fSPeter Brune PetscValidHeaderSpecific(dm,DM_CLASSID,1); 1422e30e807fSPeter Brune PetscValidPointer(subdms,3); 1423e30e807fSPeter Brune if (dm->ops->createddscatters) { 1424e30e807fSPeter Brune ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr); 142582f516ccSBarry Smith } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionLocalScatter implementation defined"); 1426e7c4fc90SDmitry Karpeev PetscFunctionReturn(0); 1427e7c4fc90SDmitry Karpeev } 1428e7c4fc90SDmitry Karpeev 1429731c8d9eSDmitry Karpeev #undef __FUNCT__ 143047c6ae99SBarry Smith #define __FUNCT__ "DMRefine" 143147c6ae99SBarry Smith /*@ 143247c6ae99SBarry Smith DMRefine - Refines a DM object 143347c6ae99SBarry Smith 143447c6ae99SBarry Smith Collective on DM 143547c6ae99SBarry Smith 143647c6ae99SBarry Smith Input Parameter: 143747c6ae99SBarry Smith + dm - the DM object 143891d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL) 143947c6ae99SBarry Smith 144047c6ae99SBarry Smith Output Parameter: 14410298fd71SBarry Smith . dmf - the refined DM, or NULL 1442ae0a1c52SMatthew G Knepley 14430298fd71SBarry Smith Note: If no refinement was done, the return value is NULL 144447c6ae99SBarry Smith 144547c6ae99SBarry Smith Level: developer 144647c6ae99SBarry Smith 1447e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation() 144847c6ae99SBarry Smith @*/ 14497087cfbeSBarry Smith PetscErrorCode DMRefine(DM dm,MPI_Comm comm,DM *dmf) 145047c6ae99SBarry Smith { 145147c6ae99SBarry Smith PetscErrorCode ierr; 1452c833c3b5SJed Brown DMRefineHookLink link; 145347c6ae99SBarry Smith 145447c6ae99SBarry Smith PetscFunctionBegin; 1455732e2eb9SMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 145647c6ae99SBarry Smith ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr); 14574057135bSMatthew G Knepley if (*dmf) { 145843842a1eSJed Brown (*dmf)->ops->creatematrix = dm->ops->creatematrix; 14598865f1eaSKarl Rupp 14608cd211a4SJed Brown ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr); 14618865f1eaSKarl Rupp 1462644e2e5bSBarry Smith (*dmf)->ctx = dm->ctx; 14630598a293SJed Brown (*dmf)->leveldown = dm->leveldown; 1464656b349aSBarry Smith (*dmf)->levelup = dm->levelup + 1; 14658865f1eaSKarl Rupp 1466e4b4b23bSJed Brown ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr); 1467c833c3b5SJed Brown for (link=dm->refinehook; link; link=link->next) { 14688865f1eaSKarl Rupp if (link->refinehook) { 14698865f1eaSKarl Rupp ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr); 14708865f1eaSKarl Rupp } 1471c833c3b5SJed Brown } 1472c833c3b5SJed Brown } 1473c833c3b5SJed Brown PetscFunctionReturn(0); 1474c833c3b5SJed Brown } 1475c833c3b5SJed Brown 1476c833c3b5SJed Brown #undef __FUNCT__ 1477c833c3b5SJed Brown #define __FUNCT__ "DMRefineHookAdd" 1478bb9467b5SJed Brown /*@C 1479c833c3b5SJed Brown DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid 1480c833c3b5SJed Brown 1481c833c3b5SJed Brown Logically Collective 1482c833c3b5SJed Brown 1483c833c3b5SJed Brown Input Arguments: 1484c833c3b5SJed Brown + coarse - nonlinear solver context on which to run a hook when restricting to a coarser level 1485c833c3b5SJed Brown . refinehook - function to run when setting up a coarser level 1486c833c3b5SJed Brown . interphook - function to run to update data on finer levels (once per SNESSolve()) 14870298fd71SBarry Smith - ctx - [optional] user-defined context for provide data for the hooks (may be NULL) 1488c833c3b5SJed Brown 1489c833c3b5SJed Brown Calling sequence of refinehook: 1490c833c3b5SJed Brown $ refinehook(DM coarse,DM fine,void *ctx); 1491c833c3b5SJed Brown 1492c833c3b5SJed Brown + coarse - coarse level DM 1493c833c3b5SJed Brown . fine - fine level DM to interpolate problem to 1494c833c3b5SJed Brown - ctx - optional user-defined function context 1495c833c3b5SJed Brown 1496c833c3b5SJed Brown Calling sequence for interphook: 1497c833c3b5SJed Brown $ interphook(DM coarse,Mat interp,DM fine,void *ctx) 1498c833c3b5SJed Brown 1499c833c3b5SJed Brown + coarse - coarse level DM 1500c833c3b5SJed Brown . interp - matrix interpolating a coarse-level solution to the finer grid 1501c833c3b5SJed Brown . fine - fine level DM to update 1502c833c3b5SJed Brown - ctx - optional user-defined function context 1503c833c3b5SJed Brown 1504c833c3b5SJed Brown Level: advanced 1505c833c3b5SJed Brown 1506c833c3b5SJed Brown Notes: 1507c833c3b5SJed Brown This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing 1508c833c3b5SJed Brown 1509c833c3b5SJed Brown If this function is called multiple times, the hooks will be run in the order they are added. 1510c833c3b5SJed Brown 1511bb9467b5SJed Brown This function is currently not available from Fortran. 1512bb9467b5SJed Brown 1513c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate() 1514c833c3b5SJed Brown @*/ 1515c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx) 1516c833c3b5SJed Brown { 1517c833c3b5SJed Brown PetscErrorCode ierr; 1518c833c3b5SJed Brown DMRefineHookLink link,*p; 1519c833c3b5SJed Brown 1520c833c3b5SJed Brown PetscFunctionBegin; 1521c833c3b5SJed Brown PetscValidHeaderSpecific(coarse,DM_CLASSID,1); 1522c833c3b5SJed Brown for (p=&coarse->refinehook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */ 1523c833c3b5SJed Brown ierr = PetscMalloc(sizeof(struct _DMRefineHookLink),&link);CHKERRQ(ierr); 1524c833c3b5SJed Brown link->refinehook = refinehook; 1525c833c3b5SJed Brown link->interphook = interphook; 1526c833c3b5SJed Brown link->ctx = ctx; 15270298fd71SBarry Smith link->next = NULL; 1528c833c3b5SJed Brown *p = link; 1529c833c3b5SJed Brown PetscFunctionReturn(0); 1530c833c3b5SJed Brown } 1531c833c3b5SJed Brown 1532c833c3b5SJed Brown #undef __FUNCT__ 1533c833c3b5SJed Brown #define __FUNCT__ "DMInterpolate" 1534c833c3b5SJed Brown /*@ 1535c833c3b5SJed Brown DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd() 1536c833c3b5SJed Brown 1537c833c3b5SJed Brown Collective if any hooks are 1538c833c3b5SJed Brown 1539c833c3b5SJed Brown Input Arguments: 1540c833c3b5SJed Brown + coarse - coarser DM to use as a base 1541c833c3b5SJed Brown . restrct - interpolation matrix, apply using MatInterpolate() 1542c833c3b5SJed Brown - fine - finer DM to update 1543c833c3b5SJed Brown 1544c833c3b5SJed Brown Level: developer 1545c833c3b5SJed Brown 1546c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate() 1547c833c3b5SJed Brown @*/ 1548c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine) 1549c833c3b5SJed Brown { 1550c833c3b5SJed Brown PetscErrorCode ierr; 1551c833c3b5SJed Brown DMRefineHookLink link; 1552c833c3b5SJed Brown 1553c833c3b5SJed Brown PetscFunctionBegin; 1554c833c3b5SJed Brown for (link=fine->refinehook; link; link=link->next) { 15558865f1eaSKarl Rupp if (link->interphook) { 15568865f1eaSKarl Rupp ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr); 15578865f1eaSKarl Rupp } 15584057135bSMatthew G Knepley } 155947c6ae99SBarry Smith PetscFunctionReturn(0); 156047c6ae99SBarry Smith } 156147c6ae99SBarry Smith 156247c6ae99SBarry Smith #undef __FUNCT__ 1563eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel" 1564eb3f98d2SBarry Smith /*@ 1565eb3f98d2SBarry Smith DMGetRefineLevel - Get's the number of refinements that have generated this DM. 1566eb3f98d2SBarry Smith 1567eb3f98d2SBarry Smith Not Collective 1568eb3f98d2SBarry Smith 1569eb3f98d2SBarry Smith Input Parameter: 1570eb3f98d2SBarry Smith . dm - the DM object 1571eb3f98d2SBarry Smith 1572eb3f98d2SBarry Smith Output Parameter: 1573eb3f98d2SBarry Smith . level - number of refinements 1574eb3f98d2SBarry Smith 1575eb3f98d2SBarry Smith Level: developer 1576eb3f98d2SBarry Smith 15776a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation() 1578eb3f98d2SBarry Smith 1579eb3f98d2SBarry Smith @*/ 1580eb3f98d2SBarry Smith PetscErrorCode DMGetRefineLevel(DM dm,PetscInt *level) 1581eb3f98d2SBarry Smith { 1582eb3f98d2SBarry Smith PetscFunctionBegin; 1583eb3f98d2SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 1584eb3f98d2SBarry Smith *level = dm->levelup; 1585eb3f98d2SBarry Smith PetscFunctionReturn(0); 1586eb3f98d2SBarry Smith } 1587eb3f98d2SBarry Smith 1588eb3f98d2SBarry Smith #undef __FUNCT__ 1589baf369e7SPeter Brune #define __FUNCT__ "DMGlobalToLocalHookAdd" 1590bb9467b5SJed Brown /*@C 1591baf369e7SPeter Brune DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called 1592baf369e7SPeter Brune 1593baf369e7SPeter Brune Logically Collective 1594baf369e7SPeter Brune 1595baf369e7SPeter Brune Input Arguments: 1596baf369e7SPeter Brune + dm - the DM 1597baf369e7SPeter Brune . beginhook - function to run at the beginning of DMGlobalToLocalBegin() 1598baf369e7SPeter Brune . endhook - function to run after DMGlobalToLocalEnd() has completed 15990298fd71SBarry Smith - ctx - [optional] user-defined context for provide data for the hooks (may be NULL) 1600baf369e7SPeter Brune 1601baf369e7SPeter Brune Calling sequence for beginhook: 1602baf369e7SPeter Brune $ beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx) 1603baf369e7SPeter Brune 1604baf369e7SPeter Brune + dm - global DM 1605baf369e7SPeter Brune . g - global vector 1606baf369e7SPeter Brune . mode - mode 1607baf369e7SPeter Brune . l - local vector 1608baf369e7SPeter Brune - ctx - optional user-defined function context 1609baf369e7SPeter Brune 1610baf369e7SPeter Brune 1611baf369e7SPeter Brune Calling sequence for endhook: 1612ec4806b8SPeter Brune $ endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx) 1613baf369e7SPeter Brune 1614baf369e7SPeter Brune + global - global DM 1615baf369e7SPeter Brune - ctx - optional user-defined function context 1616baf369e7SPeter Brune 1617baf369e7SPeter Brune Level: advanced 1618baf369e7SPeter Brune 1619baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate() 1620baf369e7SPeter Brune @*/ 1621baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx) 1622baf369e7SPeter Brune { 1623baf369e7SPeter Brune PetscErrorCode ierr; 1624baf369e7SPeter Brune DMGlobalToLocalHookLink link,*p; 1625baf369e7SPeter Brune 1626baf369e7SPeter Brune PetscFunctionBegin; 1627baf369e7SPeter Brune PetscValidHeaderSpecific(dm,DM_CLASSID,1); 1628baf369e7SPeter Brune for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */ 1629baf369e7SPeter Brune ierr = PetscMalloc(sizeof(struct _DMGlobalToLocalHookLink),&link);CHKERRQ(ierr); 1630baf369e7SPeter Brune link->beginhook = beginhook; 1631baf369e7SPeter Brune link->endhook = endhook; 1632baf369e7SPeter Brune link->ctx = ctx; 16330298fd71SBarry Smith link->next = NULL; 1634baf369e7SPeter Brune *p = link; 1635baf369e7SPeter Brune PetscFunctionReturn(0); 1636baf369e7SPeter Brune } 1637baf369e7SPeter Brune 1638baf369e7SPeter Brune #undef __FUNCT__ 163947c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin" 164047c6ae99SBarry Smith /*@ 164147c6ae99SBarry Smith DMGlobalToLocalBegin - Begins updating local vectors from global vector 164247c6ae99SBarry Smith 164347c6ae99SBarry Smith Neighbor-wise Collective on DM 164447c6ae99SBarry Smith 164547c6ae99SBarry Smith Input Parameters: 164647c6ae99SBarry Smith + dm - the DM object 164747c6ae99SBarry Smith . g - the global vector 164847c6ae99SBarry Smith . mode - INSERT_VALUES or ADD_VALUES 164947c6ae99SBarry Smith - l - the local vector 165047c6ae99SBarry Smith 165147c6ae99SBarry Smith 165247c6ae99SBarry Smith Level: beginner 165347c6ae99SBarry Smith 1654e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin() 165547c6ae99SBarry Smith 165647c6ae99SBarry Smith @*/ 16577087cfbeSBarry Smith PetscErrorCode DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l) 165847c6ae99SBarry Smith { 16597128ae9fSMatthew G Knepley PetscSF sf; 166047c6ae99SBarry Smith PetscErrorCode ierr; 1661baf369e7SPeter Brune DMGlobalToLocalHookLink link; 166247c6ae99SBarry Smith 166347c6ae99SBarry Smith PetscFunctionBegin; 1664171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 1665baf369e7SPeter Brune for (link=dm->gtolhook; link; link=link->next) { 16668865f1eaSKarl Rupp if (link->beginhook) { 16678865f1eaSKarl Rupp ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr); 16688865f1eaSKarl Rupp } 1669baf369e7SPeter Brune } 16707128ae9fSMatthew G Knepley ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr); 16717128ae9fSMatthew G Knepley if (sf) { 16727128ae9fSMatthew G Knepley PetscScalar *lArray, *gArray; 16737128ae9fSMatthew G Knepley 167482f516ccSBarry Smith if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode); 16757128ae9fSMatthew G Knepley ierr = VecGetArray(l, &lArray);CHKERRQ(ierr); 16767128ae9fSMatthew G Knepley ierr = VecGetArray(g, &gArray);CHKERRQ(ierr); 16777128ae9fSMatthew G Knepley ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr); 16787128ae9fSMatthew G Knepley ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr); 16797128ae9fSMatthew G Knepley ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr); 16807128ae9fSMatthew G Knepley } else { 1681843c4018SMatthew G Knepley ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr); 16827128ae9fSMatthew G Knepley } 168347c6ae99SBarry Smith PetscFunctionReturn(0); 168447c6ae99SBarry Smith } 168547c6ae99SBarry Smith 168647c6ae99SBarry Smith #undef __FUNCT__ 168747c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd" 168847c6ae99SBarry Smith /*@ 168947c6ae99SBarry Smith DMGlobalToLocalEnd - Ends updating local vectors from global vector 169047c6ae99SBarry Smith 169147c6ae99SBarry Smith Neighbor-wise Collective on DM 169247c6ae99SBarry Smith 169347c6ae99SBarry Smith Input Parameters: 169447c6ae99SBarry Smith + dm - the DM object 169547c6ae99SBarry Smith . g - the global vector 169647c6ae99SBarry Smith . mode - INSERT_VALUES or ADD_VALUES 169747c6ae99SBarry Smith - l - the local vector 169847c6ae99SBarry Smith 169947c6ae99SBarry Smith 170047c6ae99SBarry Smith Level: beginner 170147c6ae99SBarry Smith 1702e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin() 170347c6ae99SBarry Smith 170447c6ae99SBarry Smith @*/ 17057087cfbeSBarry Smith PetscErrorCode DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l) 170647c6ae99SBarry Smith { 17077128ae9fSMatthew G Knepley PetscSF sf; 170847c6ae99SBarry Smith PetscErrorCode ierr; 170961a3c1faSSatish Balay PetscScalar *lArray, *gArray; 1710baf369e7SPeter Brune DMGlobalToLocalHookLink link; 171147c6ae99SBarry Smith 171247c6ae99SBarry Smith PetscFunctionBegin; 1713171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 17147128ae9fSMatthew G Knepley ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr); 17157128ae9fSMatthew G Knepley if (sf) { 171682f516ccSBarry Smith if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode); 17177128ae9fSMatthew G Knepley 17187128ae9fSMatthew G Knepley ierr = VecGetArray(l, &lArray);CHKERRQ(ierr); 17197128ae9fSMatthew G Knepley ierr = VecGetArray(g, &gArray);CHKERRQ(ierr); 17207128ae9fSMatthew G Knepley ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr); 17217128ae9fSMatthew G Knepley ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr); 17227128ae9fSMatthew G Knepley ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr); 17237128ae9fSMatthew G Knepley } else { 1724843c4018SMatthew G Knepley ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr); 17257128ae9fSMatthew G Knepley } 1726baf369e7SPeter Brune for (link=dm->gtolhook; link; link=link->next) { 1727baf369e7SPeter Brune if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);} 1728baf369e7SPeter Brune } 172947c6ae99SBarry Smith PetscFunctionReturn(0); 173047c6ae99SBarry Smith } 173147c6ae99SBarry Smith 173247c6ae99SBarry Smith #undef __FUNCT__ 17339a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin" 173447c6ae99SBarry Smith /*@ 17359a42bb27SBarry Smith DMLocalToGlobalBegin - updates global vectors from local vectors 17369a42bb27SBarry Smith 17379a42bb27SBarry Smith Neighbor-wise Collective on DM 17389a42bb27SBarry Smith 17399a42bb27SBarry Smith Input Parameters: 17409a42bb27SBarry Smith + dm - the DM object 1741f6813fd5SJed Brown . l - the local vector 17429a42bb27SBarry 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 17439a42bb27SBarry Smith base point. 1744f6813fd5SJed Brown - - the global vector 17459a42bb27SBarry Smith 17469a42bb27SBarry 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 17479a42bb27SBarry 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 17489a42bb27SBarry Smith global array to the final global array with VecAXPY(). 17499a42bb27SBarry Smith 17509a42bb27SBarry Smith Level: beginner 17519a42bb27SBarry Smith 1752e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin() 17539a42bb27SBarry Smith 17549a42bb27SBarry Smith @*/ 17557087cfbeSBarry Smith PetscErrorCode DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g) 17569a42bb27SBarry Smith { 17577128ae9fSMatthew G Knepley PetscSF sf; 17589a42bb27SBarry Smith PetscErrorCode ierr; 17599a42bb27SBarry Smith 17609a42bb27SBarry Smith PetscFunctionBegin; 1761171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 17627128ae9fSMatthew G Knepley ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr); 17637128ae9fSMatthew G Knepley if (sf) { 17647128ae9fSMatthew G Knepley MPI_Op op; 17657128ae9fSMatthew G Knepley PetscScalar *lArray, *gArray; 17667128ae9fSMatthew G Knepley 17677128ae9fSMatthew G Knepley switch (mode) { 17687128ae9fSMatthew G Knepley case INSERT_VALUES: 17697128ae9fSMatthew G Knepley case INSERT_ALL_VALUES: 17708bfbc91cSJed Brown op = MPIU_REPLACE; break; 17717128ae9fSMatthew G Knepley case ADD_VALUES: 17727128ae9fSMatthew G Knepley case ADD_ALL_VALUES: 17737128ae9fSMatthew G Knepley op = MPI_SUM; break; 17747128ae9fSMatthew G Knepley default: 177582f516ccSBarry Smith SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode); 17767128ae9fSMatthew G Knepley } 17777128ae9fSMatthew G Knepley ierr = VecGetArray(l, &lArray);CHKERRQ(ierr); 17787128ae9fSMatthew G Knepley ierr = VecGetArray(g, &gArray);CHKERRQ(ierr); 17797128ae9fSMatthew G Knepley ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr); 17807128ae9fSMatthew G Knepley ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr); 17817128ae9fSMatthew G Knepley ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr); 17827128ae9fSMatthew G Knepley } else { 1783843c4018SMatthew G Knepley ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr); 17847128ae9fSMatthew G Knepley } 17859a42bb27SBarry Smith PetscFunctionReturn(0); 17869a42bb27SBarry Smith } 17879a42bb27SBarry Smith 17889a42bb27SBarry Smith #undef __FUNCT__ 17899a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd" 17909a42bb27SBarry Smith /*@ 17919a42bb27SBarry Smith DMLocalToGlobalEnd - updates global vectors from local vectors 179247c6ae99SBarry Smith 179347c6ae99SBarry Smith Neighbor-wise Collective on DM 179447c6ae99SBarry Smith 179547c6ae99SBarry Smith Input Parameters: 179647c6ae99SBarry Smith + dm - the DM object 1797f6813fd5SJed Brown . l - the local vector 179847c6ae99SBarry Smith . mode - INSERT_VALUES or ADD_VALUES 1799f6813fd5SJed Brown - g - the global vector 180047c6ae99SBarry Smith 180147c6ae99SBarry Smith 180247c6ae99SBarry Smith Level: beginner 180347c6ae99SBarry Smith 1804e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd() 180547c6ae99SBarry Smith 180647c6ae99SBarry Smith @*/ 18077087cfbeSBarry Smith PetscErrorCode DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g) 180847c6ae99SBarry Smith { 18097128ae9fSMatthew G Knepley PetscSF sf; 181047c6ae99SBarry Smith PetscErrorCode ierr; 181147c6ae99SBarry Smith 181247c6ae99SBarry Smith PetscFunctionBegin; 1813171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 18147128ae9fSMatthew G Knepley ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr); 18157128ae9fSMatthew G Knepley if (sf) { 18167128ae9fSMatthew G Knepley MPI_Op op; 18177128ae9fSMatthew G Knepley PetscScalar *lArray, *gArray; 18187128ae9fSMatthew G Knepley 18197128ae9fSMatthew G Knepley switch (mode) { 18207128ae9fSMatthew G Knepley case INSERT_VALUES: 18217128ae9fSMatthew G Knepley case INSERT_ALL_VALUES: 18228bfbc91cSJed Brown op = MPIU_REPLACE; break; 18237128ae9fSMatthew G Knepley case ADD_VALUES: 18247128ae9fSMatthew G Knepley case ADD_ALL_VALUES: 18257128ae9fSMatthew G Knepley op = MPI_SUM; break; 18267128ae9fSMatthew G Knepley default: 182782f516ccSBarry Smith SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode); 18287128ae9fSMatthew G Knepley } 18297128ae9fSMatthew G Knepley ierr = VecGetArray(l, &lArray);CHKERRQ(ierr); 18307128ae9fSMatthew G Knepley ierr = VecGetArray(g, &gArray);CHKERRQ(ierr); 18317128ae9fSMatthew G Knepley ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr); 18327128ae9fSMatthew G Knepley ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr); 18337128ae9fSMatthew G Knepley ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr); 18347128ae9fSMatthew G Knepley } else { 1835843c4018SMatthew G Knepley ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr); 18367128ae9fSMatthew G Knepley } 183747c6ae99SBarry Smith PetscFunctionReturn(0); 183847c6ae99SBarry Smith } 183947c6ae99SBarry Smith 184047c6ae99SBarry Smith #undef __FUNCT__ 1841f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalBegin" 1842f089877aSRichard Tran Mills /*@ 1843bc0a1609SRichard Tran Mills DMLocalToLocalBegin - Maps from a local vector (including ghost points 1844bc0a1609SRichard Tran Mills that contain irrelevant values) to another local vector where the ghost 1845d78e899eSRichard Tran Mills points in the second are set correctly. Must be followed by DMLocalToLocalEnd(). 1846f089877aSRichard Tran Mills 1847bc0a1609SRichard Tran Mills Neighbor-wise Collective on DM and Vec 1848f089877aSRichard Tran Mills 1849f089877aSRichard Tran Mills Input Parameters: 1850f089877aSRichard Tran Mills + dm - the DM object 1851bc0a1609SRichard Tran Mills . g - the original local vector 1852bc0a1609SRichard Tran Mills - mode - one of INSERT_VALUES or ADD_VALUES 1853f089877aSRichard Tran Mills 1854bc0a1609SRichard Tran Mills Output Parameter: 1855bc0a1609SRichard Tran Mills . l - the local vector with correct ghost values 1856f089877aSRichard Tran Mills 1857f089877aSRichard Tran Mills Level: intermediate 1858f089877aSRichard Tran Mills 1859bc0a1609SRichard Tran Mills Notes: 1860bc0a1609SRichard Tran Mills The local vectors used here need not be the same as those 1861bc0a1609SRichard Tran Mills obtained from DMCreateLocalVector(), BUT they 1862bc0a1609SRichard Tran Mills must have the same parallel data layout; they could, for example, be 1863bc0a1609SRichard Tran Mills obtained with VecDuplicate() from the DM originating vectors. 1864bc0a1609SRichard Tran Mills 1865bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, begin 1866bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin() 1867f089877aSRichard Tran Mills 1868f089877aSRichard Tran Mills @*/ 1869f089877aSRichard Tran Mills PetscErrorCode DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l) 1870f089877aSRichard Tran Mills { 1871f089877aSRichard Tran Mills PetscErrorCode ierr; 1872f089877aSRichard Tran Mills 1873f089877aSRichard Tran Mills PetscFunctionBegin; 1874f089877aSRichard Tran Mills PetscValidHeaderSpecific(dm,DM_CLASSID,1); 1875f089877aSRichard Tran Mills ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr); 1876f089877aSRichard Tran Mills PetscFunctionReturn(0); 1877f089877aSRichard Tran Mills } 1878f089877aSRichard Tran Mills 1879f089877aSRichard Tran Mills #undef __FUNCT__ 1880f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalEnd" 1881f089877aSRichard Tran Mills /*@ 1882bc0a1609SRichard Tran Mills DMLocalToLocalEnd - Maps from a local vector (including ghost points 1883bc0a1609SRichard Tran Mills that contain irrelevant values) to another local vector where the ghost 1884d78e899eSRichard Tran Mills points in the second are set correctly. Must be preceded by DMLocalToLocalBegin(). 1885f089877aSRichard Tran Mills 1886bc0a1609SRichard Tran Mills Neighbor-wise Collective on DM and Vec 1887f089877aSRichard Tran Mills 1888f089877aSRichard Tran Mills Input Parameters: 1889bc0a1609SRichard Tran Mills + da - the DM object 1890bc0a1609SRichard Tran Mills . g - the original local vector 1891bc0a1609SRichard Tran Mills - mode - one of INSERT_VALUES or ADD_VALUES 1892f089877aSRichard Tran Mills 1893bc0a1609SRichard Tran Mills Output Parameter: 1894bc0a1609SRichard Tran Mills . l - the local vector with correct ghost values 1895f089877aSRichard Tran Mills 1896f089877aSRichard Tran Mills Level: intermediate 1897f089877aSRichard Tran Mills 1898bc0a1609SRichard Tran Mills Notes: 1899bc0a1609SRichard Tran Mills The local vectors used here need not be the same as those 1900bc0a1609SRichard Tran Mills obtained from DMCreateLocalVector(), BUT they 1901bc0a1609SRichard Tran Mills must have the same parallel data layout; they could, for example, be 1902bc0a1609SRichard Tran Mills obtained with VecDuplicate() from the DM originating vectors. 1903bc0a1609SRichard Tran Mills 1904bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, end 1905bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin() 1906f089877aSRichard Tran Mills 1907f089877aSRichard Tran Mills @*/ 1908f089877aSRichard Tran Mills PetscErrorCode DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l) 1909f089877aSRichard Tran Mills { 1910f089877aSRichard Tran Mills PetscErrorCode ierr; 1911f089877aSRichard Tran Mills 1912f089877aSRichard Tran Mills PetscFunctionBegin; 1913f089877aSRichard Tran Mills PetscValidHeaderSpecific(dm,DM_CLASSID,1); 1914f089877aSRichard Tran Mills ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr); 1915f089877aSRichard Tran Mills PetscFunctionReturn(0); 1916f089877aSRichard Tran Mills } 1917f089877aSRichard Tran Mills 1918f089877aSRichard Tran Mills 1919f089877aSRichard Tran Mills #undef __FUNCT__ 192047c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen" 192147c6ae99SBarry Smith /*@ 192247c6ae99SBarry Smith DMCoarsen - Coarsens a DM object 192347c6ae99SBarry Smith 192447c6ae99SBarry Smith Collective on DM 192547c6ae99SBarry Smith 192647c6ae99SBarry Smith Input Parameter: 192747c6ae99SBarry Smith + dm - the DM object 192891d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL) 192947c6ae99SBarry Smith 193047c6ae99SBarry Smith Output Parameter: 193147c6ae99SBarry Smith . dmc - the coarsened DM 193247c6ae99SBarry Smith 193347c6ae99SBarry Smith Level: developer 193447c6ae99SBarry Smith 1935e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation() 193647c6ae99SBarry Smith 193747c6ae99SBarry Smith @*/ 19387087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc) 193947c6ae99SBarry Smith { 194047c6ae99SBarry Smith PetscErrorCode ierr; 1941b17ce1afSJed Brown DMCoarsenHookLink link; 194247c6ae99SBarry Smith 194347c6ae99SBarry Smith PetscFunctionBegin; 1944171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 194547c6ae99SBarry Smith ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr); 194643842a1eSJed Brown (*dmc)->ops->creatematrix = dm->ops->creatematrix; 19478cd211a4SJed Brown ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr); 1948644e2e5bSBarry Smith (*dmc)->ctx = dm->ctx; 19490598a293SJed Brown (*dmc)->levelup = dm->levelup; 1950656b349aSBarry Smith (*dmc)->leveldown = dm->leveldown + 1; 1951e4b4b23bSJed Brown ierr = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr); 1952b17ce1afSJed Brown for (link=dm->coarsenhook; link; link=link->next) { 1953b17ce1afSJed Brown if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);} 1954b17ce1afSJed Brown } 1955b17ce1afSJed Brown PetscFunctionReturn(0); 1956b17ce1afSJed Brown } 1957b17ce1afSJed Brown 1958b17ce1afSJed Brown #undef __FUNCT__ 1959b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd" 1960bb9467b5SJed Brown /*@C 1961b17ce1afSJed Brown DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid 1962b17ce1afSJed Brown 1963b17ce1afSJed Brown Logically Collective 1964b17ce1afSJed Brown 1965b17ce1afSJed Brown Input Arguments: 1966b17ce1afSJed Brown + fine - nonlinear solver context on which to run a hook when restricting to a coarser level 1967b17ce1afSJed Brown . coarsenhook - function to run when setting up a coarser level 1968b17ce1afSJed Brown . restricthook - function to run to update data on coarser levels (once per SNESSolve()) 19690298fd71SBarry Smith - ctx - [optional] user-defined context for provide data for the hooks (may be NULL) 1970b17ce1afSJed Brown 1971b17ce1afSJed Brown Calling sequence of coarsenhook: 1972b17ce1afSJed Brown $ coarsenhook(DM fine,DM coarse,void *ctx); 1973b17ce1afSJed Brown 1974b17ce1afSJed Brown + fine - fine level DM 1975b17ce1afSJed Brown . coarse - coarse level DM to restrict problem to 1976b17ce1afSJed Brown - ctx - optional user-defined function context 1977b17ce1afSJed Brown 1978b17ce1afSJed Brown Calling sequence for restricthook: 1979c833c3b5SJed Brown $ restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx) 1980b17ce1afSJed Brown 1981b17ce1afSJed Brown + fine - fine level DM 1982b17ce1afSJed Brown . mrestrict - matrix restricting a fine-level solution to the coarse grid 1983c833c3b5SJed Brown . rscale - scaling vector for restriction 1984c833c3b5SJed Brown . inject - matrix restricting by injection 1985b17ce1afSJed Brown . coarse - coarse level DM to update 1986b17ce1afSJed Brown - ctx - optional user-defined function context 1987b17ce1afSJed Brown 1988b17ce1afSJed Brown Level: advanced 1989b17ce1afSJed Brown 1990b17ce1afSJed Brown Notes: 1991b17ce1afSJed Brown This function is only needed if auxiliary data needs to be set up on coarse grids. 1992b17ce1afSJed Brown 1993b17ce1afSJed Brown If this function is called multiple times, the hooks will be run in the order they are added. 1994b17ce1afSJed Brown 1995b17ce1afSJed Brown In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to 1996b17ce1afSJed Brown extract the finest level information from its context (instead of from the SNES). 1997b17ce1afSJed Brown 1998bb9467b5SJed Brown This function is currently not available from Fortran. 1999bb9467b5SJed Brown 2000c833c3b5SJed Brown .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate() 2001b17ce1afSJed Brown @*/ 2002b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx) 2003b17ce1afSJed Brown { 2004b17ce1afSJed Brown PetscErrorCode ierr; 2005b17ce1afSJed Brown DMCoarsenHookLink link,*p; 2006b17ce1afSJed Brown 2007b17ce1afSJed Brown PetscFunctionBegin; 2008b17ce1afSJed Brown PetscValidHeaderSpecific(fine,DM_CLASSID,1); 20096bfea28cSJed Brown for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */ 2010b17ce1afSJed Brown ierr = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr); 2011b17ce1afSJed Brown link->coarsenhook = coarsenhook; 2012b17ce1afSJed Brown link->restricthook = restricthook; 2013b17ce1afSJed Brown link->ctx = ctx; 20140298fd71SBarry Smith link->next = NULL; 2015b17ce1afSJed Brown *p = link; 2016b17ce1afSJed Brown PetscFunctionReturn(0); 2017b17ce1afSJed Brown } 2018b17ce1afSJed Brown 2019b17ce1afSJed Brown #undef __FUNCT__ 2020b17ce1afSJed Brown #define __FUNCT__ "DMRestrict" 2021b17ce1afSJed Brown /*@ 2022b17ce1afSJed Brown DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd() 2023b17ce1afSJed Brown 2024b17ce1afSJed Brown Collective if any hooks are 2025b17ce1afSJed Brown 2026b17ce1afSJed Brown Input Arguments: 2027b17ce1afSJed Brown + fine - finer DM to use as a base 2028b17ce1afSJed Brown . restrct - restriction matrix, apply using MatRestrict() 2029b17ce1afSJed Brown . inject - injection matrix, also use MatRestrict() 2030b17ce1afSJed Brown - coarse - coarer DM to update 2031b17ce1afSJed Brown 2032b17ce1afSJed Brown Level: developer 2033b17ce1afSJed Brown 2034b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict() 2035b17ce1afSJed Brown @*/ 2036b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse) 2037b17ce1afSJed Brown { 2038b17ce1afSJed Brown PetscErrorCode ierr; 2039b17ce1afSJed Brown DMCoarsenHookLink link; 2040b17ce1afSJed Brown 2041b17ce1afSJed Brown PetscFunctionBegin; 2042b17ce1afSJed Brown for (link=fine->coarsenhook; link; link=link->next) { 20438865f1eaSKarl Rupp if (link->restricthook) { 20448865f1eaSKarl Rupp ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr); 20458865f1eaSKarl Rupp } 2046b17ce1afSJed Brown } 204747c6ae99SBarry Smith PetscFunctionReturn(0); 204847c6ae99SBarry Smith } 204947c6ae99SBarry Smith 205047c6ae99SBarry Smith #undef __FUNCT__ 2051be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainHookAdd" 2052bb9467b5SJed Brown /*@C 2053be081cd6SPeter Brune DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid 20545dbd56e3SPeter Brune 20555dbd56e3SPeter Brune Logically Collective 20565dbd56e3SPeter Brune 20575dbd56e3SPeter Brune Input Arguments: 20585dbd56e3SPeter Brune + global - global DM 2059ec4806b8SPeter Brune . ddhook - function to run to pass data to the decomposition DM upon its creation 20605dbd56e3SPeter Brune . restricthook - function to run to update data on block solve (at the beginning of the block solve) 20610298fd71SBarry Smith - ctx - [optional] user-defined context for provide data for the hooks (may be NULL) 20625dbd56e3SPeter Brune 2063ec4806b8SPeter Brune 2064ec4806b8SPeter Brune Calling sequence for ddhook: 2065ec4806b8SPeter Brune $ ddhook(DM global,DM block,void *ctx) 2066ec4806b8SPeter Brune 2067ec4806b8SPeter Brune + global - global DM 2068ec4806b8SPeter Brune . block - block DM 2069ec4806b8SPeter Brune - ctx - optional user-defined function context 2070ec4806b8SPeter Brune 20715dbd56e3SPeter Brune Calling sequence for restricthook: 2072ec4806b8SPeter Brune $ restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx) 20735dbd56e3SPeter Brune 20745dbd56e3SPeter Brune + global - global DM 20755dbd56e3SPeter Brune . out - scatter to the outer (with ghost and overlap points) block vector 20765dbd56e3SPeter Brune . in - scatter to block vector values only owned locally 2077ec4806b8SPeter Brune . block - block DM 20785dbd56e3SPeter Brune - ctx - optional user-defined function context 20795dbd56e3SPeter Brune 20805dbd56e3SPeter Brune Level: advanced 20815dbd56e3SPeter Brune 20825dbd56e3SPeter Brune Notes: 2083ec4806b8SPeter Brune This function is only needed if auxiliary data needs to be set up on subdomain DMs. 20845dbd56e3SPeter Brune 20855dbd56e3SPeter Brune If this function is called multiple times, the hooks will be run in the order they are added. 20865dbd56e3SPeter Brune 20875dbd56e3SPeter Brune In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to 2088ec4806b8SPeter Brune extract the global information from its context (instead of from the SNES). 20895dbd56e3SPeter Brune 2090bb9467b5SJed Brown This function is currently not available from Fortran. 2091bb9467b5SJed Brown 20925dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate() 20935dbd56e3SPeter Brune @*/ 2094be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx) 20955dbd56e3SPeter Brune { 20965dbd56e3SPeter Brune PetscErrorCode ierr; 2097be081cd6SPeter Brune DMSubDomainHookLink link,*p; 20985dbd56e3SPeter Brune 20995dbd56e3SPeter Brune PetscFunctionBegin; 21005dbd56e3SPeter Brune PetscValidHeaderSpecific(global,DM_CLASSID,1); 2101be081cd6SPeter Brune for (p=&global->subdomainhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */ 2102be081cd6SPeter Brune ierr = PetscMalloc(sizeof(struct _DMSubDomainHookLink),&link);CHKERRQ(ierr); 21035dbd56e3SPeter Brune link->restricthook = restricthook; 2104be081cd6SPeter Brune link->ddhook = ddhook; 21055dbd56e3SPeter Brune link->ctx = ctx; 21060298fd71SBarry Smith link->next = NULL; 21075dbd56e3SPeter Brune *p = link; 21085dbd56e3SPeter Brune PetscFunctionReturn(0); 21095dbd56e3SPeter Brune } 21105dbd56e3SPeter Brune 21115dbd56e3SPeter Brune #undef __FUNCT__ 2112be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainRestrict" 21135dbd56e3SPeter Brune /*@ 2114be081cd6SPeter Brune DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd() 21155dbd56e3SPeter Brune 21165dbd56e3SPeter Brune Collective if any hooks are 21175dbd56e3SPeter Brune 21185dbd56e3SPeter Brune Input Arguments: 21195dbd56e3SPeter Brune + fine - finer DM to use as a base 2120be081cd6SPeter Brune . oscatter - scatter from domain global vector filling subdomain global vector with overlap 2121be081cd6SPeter Brune . gscatter - scatter from domain global vector filling subdomain local vector with ghosts 21225dbd56e3SPeter Brune - coarse - coarer DM to update 21235dbd56e3SPeter Brune 21245dbd56e3SPeter Brune Level: developer 21255dbd56e3SPeter Brune 21265dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict() 21275dbd56e3SPeter Brune @*/ 2128be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm) 21295dbd56e3SPeter Brune { 21305dbd56e3SPeter Brune PetscErrorCode ierr; 2131be081cd6SPeter Brune DMSubDomainHookLink link; 21325dbd56e3SPeter Brune 21335dbd56e3SPeter Brune PetscFunctionBegin; 2134be081cd6SPeter Brune for (link=global->subdomainhook; link; link=link->next) { 21358865f1eaSKarl Rupp if (link->restricthook) { 21368865f1eaSKarl Rupp ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr); 21378865f1eaSKarl Rupp } 21385dbd56e3SPeter Brune } 21395dbd56e3SPeter Brune PetscFunctionReturn(0); 21405dbd56e3SPeter Brune } 21415dbd56e3SPeter Brune 21425dbd56e3SPeter Brune #undef __FUNCT__ 21435fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel" 21445fe1f584SPeter Brune /*@ 21456a7d9d85SPeter Brune DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM. 21465fe1f584SPeter Brune 21475fe1f584SPeter Brune Not Collective 21485fe1f584SPeter Brune 21495fe1f584SPeter Brune Input Parameter: 21505fe1f584SPeter Brune . dm - the DM object 21515fe1f584SPeter Brune 21525fe1f584SPeter Brune Output Parameter: 21536a7d9d85SPeter Brune . level - number of coarsenings 21545fe1f584SPeter Brune 21555fe1f584SPeter Brune Level: developer 21565fe1f584SPeter Brune 21576a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation() 21585fe1f584SPeter Brune 21595fe1f584SPeter Brune @*/ 21605fe1f584SPeter Brune PetscErrorCode DMGetCoarsenLevel(DM dm,PetscInt *level) 21615fe1f584SPeter Brune { 21625fe1f584SPeter Brune PetscFunctionBegin; 21635fe1f584SPeter Brune PetscValidHeaderSpecific(dm,DM_CLASSID,1); 21645fe1f584SPeter Brune *level = dm->leveldown; 21655fe1f584SPeter Brune PetscFunctionReturn(0); 21665fe1f584SPeter Brune } 21675fe1f584SPeter Brune 21685fe1f584SPeter Brune 21695fe1f584SPeter Brune 21705fe1f584SPeter Brune #undef __FUNCT__ 217147c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy" 217247c6ae99SBarry Smith /*@C 217347c6ae99SBarry Smith DMRefineHierarchy - Refines a DM object, all levels at once 217447c6ae99SBarry Smith 217547c6ae99SBarry Smith Collective on DM 217647c6ae99SBarry Smith 217747c6ae99SBarry Smith Input Parameter: 217847c6ae99SBarry Smith + dm - the DM object 217947c6ae99SBarry Smith - nlevels - the number of levels of refinement 218047c6ae99SBarry Smith 218147c6ae99SBarry Smith Output Parameter: 218247c6ae99SBarry Smith . dmf - the refined DM hierarchy 218347c6ae99SBarry Smith 218447c6ae99SBarry Smith Level: developer 218547c6ae99SBarry Smith 2186e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation() 218747c6ae99SBarry Smith 218847c6ae99SBarry Smith @*/ 21897087cfbeSBarry Smith PetscErrorCode DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[]) 219047c6ae99SBarry Smith { 219147c6ae99SBarry Smith PetscErrorCode ierr; 219247c6ae99SBarry Smith 219347c6ae99SBarry Smith PetscFunctionBegin; 2194171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 2195ce94432eSBarry Smith if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative"); 219647c6ae99SBarry Smith if (nlevels == 0) PetscFunctionReturn(0); 219747c6ae99SBarry Smith if (dm->ops->refinehierarchy) { 219847c6ae99SBarry Smith ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr); 219947c6ae99SBarry Smith } else if (dm->ops->refine) { 220047c6ae99SBarry Smith PetscInt i; 220147c6ae99SBarry Smith 2202ce94432eSBarry Smith ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr); 220347c6ae99SBarry Smith for (i=1; i<nlevels; i++) { 2204ce94432eSBarry Smith ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr); 220547c6ae99SBarry Smith } 2206ce94432eSBarry Smith } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet"); 220747c6ae99SBarry Smith PetscFunctionReturn(0); 220847c6ae99SBarry Smith } 220947c6ae99SBarry Smith 221047c6ae99SBarry Smith #undef __FUNCT__ 221147c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy" 221247c6ae99SBarry Smith /*@C 221347c6ae99SBarry Smith DMCoarsenHierarchy - Coarsens a DM object, all levels at once 221447c6ae99SBarry Smith 221547c6ae99SBarry Smith Collective on DM 221647c6ae99SBarry Smith 221747c6ae99SBarry Smith Input Parameter: 221847c6ae99SBarry Smith + dm - the DM object 221947c6ae99SBarry Smith - nlevels - the number of levels of coarsening 222047c6ae99SBarry Smith 222147c6ae99SBarry Smith Output Parameter: 222247c6ae99SBarry Smith . dmc - the coarsened DM hierarchy 222347c6ae99SBarry Smith 222447c6ae99SBarry Smith Level: developer 222547c6ae99SBarry Smith 2226e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation() 222747c6ae99SBarry Smith 222847c6ae99SBarry Smith @*/ 22297087cfbeSBarry Smith PetscErrorCode DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[]) 223047c6ae99SBarry Smith { 223147c6ae99SBarry Smith PetscErrorCode ierr; 223247c6ae99SBarry Smith 223347c6ae99SBarry Smith PetscFunctionBegin; 2234171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 2235ce94432eSBarry Smith if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative"); 223647c6ae99SBarry Smith if (nlevels == 0) PetscFunctionReturn(0); 223747c6ae99SBarry Smith PetscValidPointer(dmc,3); 223847c6ae99SBarry Smith if (dm->ops->coarsenhierarchy) { 223947c6ae99SBarry Smith ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr); 224047c6ae99SBarry Smith } else if (dm->ops->coarsen) { 224147c6ae99SBarry Smith PetscInt i; 224247c6ae99SBarry Smith 2243ce94432eSBarry Smith ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr); 224447c6ae99SBarry Smith for (i=1; i<nlevels; i++) { 2245ce94432eSBarry Smith ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr); 224647c6ae99SBarry Smith } 2247ce94432eSBarry Smith } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet"); 224847c6ae99SBarry Smith PetscFunctionReturn(0); 224947c6ae99SBarry Smith } 225047c6ae99SBarry Smith 225147c6ae99SBarry Smith #undef __FUNCT__ 2252e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates" 225347c6ae99SBarry Smith /*@ 2254e727c939SJed Brown DMCreateAggregates - Gets the aggregates that map between 225547c6ae99SBarry Smith grids associated with two DMs. 225647c6ae99SBarry Smith 225747c6ae99SBarry Smith Collective on DM 225847c6ae99SBarry Smith 225947c6ae99SBarry Smith Input Parameters: 226047c6ae99SBarry Smith + dmc - the coarse grid DM 226147c6ae99SBarry Smith - dmf - the fine grid DM 226247c6ae99SBarry Smith 226347c6ae99SBarry Smith Output Parameters: 226447c6ae99SBarry Smith . rest - the restriction matrix (transpose of the projection matrix) 226547c6ae99SBarry Smith 226647c6ae99SBarry Smith Level: intermediate 226747c6ae99SBarry Smith 226847c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid 226947c6ae99SBarry Smith 2270e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation() 227147c6ae99SBarry Smith @*/ 2272e727c939SJed Brown PetscErrorCode DMCreateAggregates(DM dmc, DM dmf, Mat *rest) 227347c6ae99SBarry Smith { 227447c6ae99SBarry Smith PetscErrorCode ierr; 227547c6ae99SBarry Smith 227647c6ae99SBarry Smith PetscFunctionBegin; 2277171400e9SBarry Smith PetscValidHeaderSpecific(dmc,DM_CLASSID,1); 2278171400e9SBarry Smith PetscValidHeaderSpecific(dmf,DM_CLASSID,2); 227947c6ae99SBarry Smith ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr); 228047c6ae99SBarry Smith PetscFunctionReturn(0); 228147c6ae99SBarry Smith } 228247c6ae99SBarry Smith 228347c6ae99SBarry Smith #undef __FUNCT__ 22841a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy" 22851a266240SBarry Smith /*@C 22861a266240SBarry Smith DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed 22871a266240SBarry Smith 22881a266240SBarry Smith Not Collective 22891a266240SBarry Smith 22901a266240SBarry Smith Input Parameters: 22911a266240SBarry Smith + dm - the DM object 22921a266240SBarry Smith - destroy - the destroy function 22931a266240SBarry Smith 22941a266240SBarry Smith Level: intermediate 22951a266240SBarry Smith 2296e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext() 22971a266240SBarry Smith 2298f07f9ceaSJed Brown @*/ 22991a266240SBarry Smith PetscErrorCode DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**)) 23001a266240SBarry Smith { 23011a266240SBarry Smith PetscFunctionBegin; 2302171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 23031a266240SBarry Smith dm->ctxdestroy = destroy; 23041a266240SBarry Smith PetscFunctionReturn(0); 23051a266240SBarry Smith } 23061a266240SBarry Smith 23071a266240SBarry Smith #undef __FUNCT__ 23081b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext" 2309b07ff414SBarry Smith /*@ 23101b2093e4SBarry Smith DMSetApplicationContext - Set a user context into a DM object 231147c6ae99SBarry Smith 231247c6ae99SBarry Smith Not Collective 231347c6ae99SBarry Smith 231447c6ae99SBarry Smith Input Parameters: 231547c6ae99SBarry Smith + dm - the DM object 231647c6ae99SBarry Smith - ctx - the user context 231747c6ae99SBarry Smith 231847c6ae99SBarry Smith Level: intermediate 231947c6ae99SBarry Smith 2320e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext() 232147c6ae99SBarry Smith 232247c6ae99SBarry Smith @*/ 23231b2093e4SBarry Smith PetscErrorCode DMSetApplicationContext(DM dm,void *ctx) 232447c6ae99SBarry Smith { 232547c6ae99SBarry Smith PetscFunctionBegin; 2326171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 232747c6ae99SBarry Smith dm->ctx = ctx; 232847c6ae99SBarry Smith PetscFunctionReturn(0); 232947c6ae99SBarry Smith } 233047c6ae99SBarry Smith 233147c6ae99SBarry Smith #undef __FUNCT__ 23321b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext" 233347c6ae99SBarry Smith /*@ 23341b2093e4SBarry Smith DMGetApplicationContext - Gets a user context from a DM object 233547c6ae99SBarry Smith 233647c6ae99SBarry Smith Not Collective 233747c6ae99SBarry Smith 233847c6ae99SBarry Smith Input Parameter: 233947c6ae99SBarry Smith . dm - the DM object 234047c6ae99SBarry Smith 234147c6ae99SBarry Smith Output Parameter: 234247c6ae99SBarry Smith . ctx - the user context 234347c6ae99SBarry Smith 234447c6ae99SBarry Smith Level: intermediate 234547c6ae99SBarry Smith 2346e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext() 234747c6ae99SBarry Smith 234847c6ae99SBarry Smith @*/ 23491b2093e4SBarry Smith PetscErrorCode DMGetApplicationContext(DM dm,void *ctx) 235047c6ae99SBarry Smith { 235147c6ae99SBarry Smith PetscFunctionBegin; 2352171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 23531b2093e4SBarry Smith *(void**)ctx = dm->ctx; 235447c6ae99SBarry Smith PetscFunctionReturn(0); 235547c6ae99SBarry Smith } 235647c6ae99SBarry Smith 235747c6ae99SBarry Smith #undef __FUNCT__ 235808da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds" 235908da532bSDmitry Karpeev /*@C 236008da532bSDmitry Karpeev DMSetVariableBounds - sets a function to compute the the lower and upper bound vectors for SNESVI. 236108da532bSDmitry Karpeev 236208da532bSDmitry Karpeev Logically Collective on DM 236308da532bSDmitry Karpeev 236408da532bSDmitry Karpeev Input Parameter: 236508da532bSDmitry Karpeev + dm - the DM object 23660298fd71SBarry Smith - f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set) 236708da532bSDmitry Karpeev 236808da532bSDmitry Karpeev Level: intermediate 236908da532bSDmitry Karpeev 2370835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), 237108da532bSDmitry Karpeev DMSetJacobian() 237208da532bSDmitry Karpeev 237308da532bSDmitry Karpeev @*/ 237408da532bSDmitry Karpeev PetscErrorCode DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec)) 237508da532bSDmitry Karpeev { 237608da532bSDmitry Karpeev PetscFunctionBegin; 237708da532bSDmitry Karpeev dm->ops->computevariablebounds = f; 237808da532bSDmitry Karpeev PetscFunctionReturn(0); 237908da532bSDmitry Karpeev } 238008da532bSDmitry Karpeev 238108da532bSDmitry Karpeev #undef __FUNCT__ 238208da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds" 238308da532bSDmitry Karpeev /*@ 238408da532bSDmitry Karpeev DMHasVariableBounds - does the DM object have a variable bounds function? 238508da532bSDmitry Karpeev 238608da532bSDmitry Karpeev Not Collective 238708da532bSDmitry Karpeev 238808da532bSDmitry Karpeev Input Parameter: 238908da532bSDmitry Karpeev . dm - the DM object to destroy 239008da532bSDmitry Karpeev 239108da532bSDmitry Karpeev Output Parameter: 239208da532bSDmitry Karpeev . flg - PETSC_TRUE if the variable bounds function exists 239308da532bSDmitry Karpeev 239408da532bSDmitry Karpeev Level: developer 239508da532bSDmitry Karpeev 239674e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext() 239708da532bSDmitry Karpeev 239808da532bSDmitry Karpeev @*/ 239908da532bSDmitry Karpeev PetscErrorCode DMHasVariableBounds(DM dm,PetscBool *flg) 240008da532bSDmitry Karpeev { 240108da532bSDmitry Karpeev PetscFunctionBegin; 240208da532bSDmitry Karpeev *flg = (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE; 240308da532bSDmitry Karpeev PetscFunctionReturn(0); 240408da532bSDmitry Karpeev } 240508da532bSDmitry Karpeev 240608da532bSDmitry Karpeev #undef __FUNCT__ 240708da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds" 240808da532bSDmitry Karpeev /*@C 240908da532bSDmitry Karpeev DMComputeVariableBounds - compute variable bounds used by SNESVI. 241008da532bSDmitry Karpeev 241108da532bSDmitry Karpeev Logically Collective on DM 241208da532bSDmitry Karpeev 241308da532bSDmitry Karpeev Input Parameters: 241408da532bSDmitry Karpeev + dm - the DM object to destroy 241508da532bSDmitry Karpeev - x - current solution at which the bounds are computed 241608da532bSDmitry Karpeev 241708da532bSDmitry Karpeev Output parameters: 241808da532bSDmitry Karpeev + xl - lower bound 241908da532bSDmitry Karpeev - xu - upper bound 242008da532bSDmitry Karpeev 242108da532bSDmitry Karpeev Level: intermediate 242208da532bSDmitry Karpeev 242374e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext() 242408da532bSDmitry Karpeev 242508da532bSDmitry Karpeev @*/ 242608da532bSDmitry Karpeev PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu) 242708da532bSDmitry Karpeev { 242808da532bSDmitry Karpeev PetscErrorCode ierr; 24295fd66863SKarl Rupp 243008da532bSDmitry Karpeev PetscFunctionBegin; 243108da532bSDmitry Karpeev PetscValidHeaderSpecific(xl,VEC_CLASSID,2); 243208da532bSDmitry Karpeev PetscValidHeaderSpecific(xu,VEC_CLASSID,2); 243308da532bSDmitry Karpeev if (dm->ops->computevariablebounds) { 243408da532bSDmitry Karpeev ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr); 24358865f1eaSKarl Rupp } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds."); 243608da532bSDmitry Karpeev PetscFunctionReturn(0); 243708da532bSDmitry Karpeev } 243808da532bSDmitry Karpeev 243908da532bSDmitry Karpeev #undef __FUNCT__ 2440b0ae01b7SPeter Brune #define __FUNCT__ "DMHasColoring" 2441b0ae01b7SPeter Brune /*@ 2442b0ae01b7SPeter Brune DMHasColoring - does the DM object have a method of providing a coloring? 2443b0ae01b7SPeter Brune 2444b0ae01b7SPeter Brune Not Collective 2445b0ae01b7SPeter Brune 2446b0ae01b7SPeter Brune Input Parameter: 2447b0ae01b7SPeter Brune . dm - the DM object 2448b0ae01b7SPeter Brune 2449b0ae01b7SPeter Brune Output Parameter: 2450b0ae01b7SPeter Brune . flg - PETSC_TRUE if the DM has facilities for DMCreateColoring(). 2451b0ae01b7SPeter Brune 2452b0ae01b7SPeter Brune Level: developer 2453b0ae01b7SPeter Brune 2454b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring() 2455b0ae01b7SPeter Brune 2456b0ae01b7SPeter Brune @*/ 2457b0ae01b7SPeter Brune PetscErrorCode DMHasColoring(DM dm,PetscBool *flg) 2458b0ae01b7SPeter Brune { 2459b0ae01b7SPeter Brune PetscFunctionBegin; 2460b0ae01b7SPeter Brune *flg = (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE; 2461b0ae01b7SPeter Brune PetscFunctionReturn(0); 2462b0ae01b7SPeter Brune } 2463b0ae01b7SPeter Brune 2464b0ae01b7SPeter Brune #undef __FUNCT__ 246508da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec" 2466748fac09SDmitry Karpeev /*@C 246708da532bSDmitry Karpeev DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear. 246808da532bSDmitry Karpeev 246908da532bSDmitry Karpeev Collective on DM 247008da532bSDmitry Karpeev 247108da532bSDmitry Karpeev Input Parameter: 247208da532bSDmitry Karpeev + dm - the DM object 24730298fd71SBarry Smith - x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems. 247408da532bSDmitry Karpeev 247508da532bSDmitry Karpeev Level: developer 247608da532bSDmitry Karpeev 247774e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext() 247808da532bSDmitry Karpeev 247908da532bSDmitry Karpeev @*/ 248008da532bSDmitry Karpeev PetscErrorCode DMSetVec(DM dm,Vec x) 248108da532bSDmitry Karpeev { 248208da532bSDmitry Karpeev PetscErrorCode ierr; 24835fd66863SKarl Rupp 248408da532bSDmitry Karpeev PetscFunctionBegin; 248508da532bSDmitry Karpeev if (x) { 248608da532bSDmitry Karpeev if (!dm->x) { 248708da532bSDmitry Karpeev ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr); 248808da532bSDmitry Karpeev } 248908da532bSDmitry Karpeev ierr = VecCopy(x,dm->x);CHKERRQ(ierr); 24908865f1eaSKarl Rupp } else if (dm->x) { 249108da532bSDmitry Karpeev ierr = VecDestroy(&dm->x);CHKERRQ(ierr); 249208da532bSDmitry Karpeev } 249308da532bSDmitry Karpeev PetscFunctionReturn(0); 249408da532bSDmitry Karpeev } 249508da532bSDmitry Karpeev 24960298fd71SBarry Smith PetscFunctionList DMList = NULL; 2497264ace61SBarry Smith PetscBool DMRegisterAllCalled = PETSC_FALSE; 2498264ace61SBarry Smith 2499264ace61SBarry Smith #undef __FUNCT__ 2500264ace61SBarry Smith #define __FUNCT__ "DMSetType" 2501264ace61SBarry Smith /*@C 2502264ace61SBarry Smith DMSetType - Builds a DM, for a particular DM implementation. 2503264ace61SBarry Smith 2504264ace61SBarry Smith Collective on DM 2505264ace61SBarry Smith 2506264ace61SBarry Smith Input Parameters: 2507264ace61SBarry Smith + dm - The DM object 2508264ace61SBarry Smith - method - The name of the DM type 2509264ace61SBarry Smith 2510264ace61SBarry Smith Options Database Key: 2511264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types 2512264ace61SBarry Smith 2513264ace61SBarry Smith Notes: 2514e1589f56SBarry Smith See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D). 2515264ace61SBarry Smith 2516264ace61SBarry Smith Level: intermediate 2517264ace61SBarry Smith 2518264ace61SBarry Smith .keywords: DM, set, type 2519264ace61SBarry Smith .seealso: DMGetType(), DMCreate() 2520264ace61SBarry Smith @*/ 252119fd82e9SBarry Smith PetscErrorCode DMSetType(DM dm, DMType method) 2522264ace61SBarry Smith { 2523264ace61SBarry Smith PetscErrorCode (*r)(DM); 2524264ace61SBarry Smith PetscBool match; 2525264ace61SBarry Smith PetscErrorCode ierr; 2526264ace61SBarry Smith 2527264ace61SBarry Smith PetscFunctionBegin; 2528264ace61SBarry Smith PetscValidHeaderSpecific(dm, DM_CLASSID,1); 2529251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr); 2530264ace61SBarry Smith if (match) PetscFunctionReturn(0); 2531264ace61SBarry Smith 2532607a6623SBarry Smith if (!DMRegisterAllCalled) {ierr = DMRegisterAll();CHKERRQ(ierr);} 25331c9cd337SJed Brown ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr); 2534ce94432eSBarry Smith if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method); 2535264ace61SBarry Smith 2536264ace61SBarry Smith if (dm->ops->destroy) { 2537264ace61SBarry Smith ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr); 25380298fd71SBarry Smith dm->ops->destroy = NULL; 2539264ace61SBarry Smith } 2540264ace61SBarry Smith ierr = (*r)(dm);CHKERRQ(ierr); 2541264ace61SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr); 2542264ace61SBarry Smith PetscFunctionReturn(0); 2543264ace61SBarry Smith } 2544264ace61SBarry Smith 2545264ace61SBarry Smith #undef __FUNCT__ 2546264ace61SBarry Smith #define __FUNCT__ "DMGetType" 2547264ace61SBarry Smith /*@C 2548264ace61SBarry Smith DMGetType - Gets the DM type name (as a string) from the DM. 2549264ace61SBarry Smith 2550264ace61SBarry Smith Not Collective 2551264ace61SBarry Smith 2552264ace61SBarry Smith Input Parameter: 2553264ace61SBarry Smith . dm - The DM 2554264ace61SBarry Smith 2555264ace61SBarry Smith Output Parameter: 2556264ace61SBarry Smith . type - The DM type name 2557264ace61SBarry Smith 2558264ace61SBarry Smith Level: intermediate 2559264ace61SBarry Smith 2560264ace61SBarry Smith .keywords: DM, get, type, name 2561264ace61SBarry Smith .seealso: DMSetType(), DMCreate() 2562264ace61SBarry Smith @*/ 256319fd82e9SBarry Smith PetscErrorCode DMGetType(DM dm, DMType *type) 2564264ace61SBarry Smith { 2565264ace61SBarry Smith PetscErrorCode ierr; 2566264ace61SBarry Smith 2567264ace61SBarry Smith PetscFunctionBegin; 2568264ace61SBarry Smith PetscValidHeaderSpecific(dm, DM_CLASSID,1); 2569264ace61SBarry Smith PetscValidCharPointer(type,2); 2570264ace61SBarry Smith if (!DMRegisterAllCalled) { 2571607a6623SBarry Smith ierr = DMRegisterAll();CHKERRQ(ierr); 2572264ace61SBarry Smith } 2573264ace61SBarry Smith *type = ((PetscObject)dm)->type_name; 2574264ace61SBarry Smith PetscFunctionReturn(0); 2575264ace61SBarry Smith } 2576264ace61SBarry Smith 257767a56275SMatthew G Knepley #undef __FUNCT__ 257867a56275SMatthew G Knepley #define __FUNCT__ "DMConvert" 257967a56275SMatthew G Knepley /*@C 258067a56275SMatthew G Knepley DMConvert - Converts a DM to another DM, either of the same or different type. 258167a56275SMatthew G Knepley 258267a56275SMatthew G Knepley Collective on DM 258367a56275SMatthew G Knepley 258467a56275SMatthew G Knepley Input Parameters: 258567a56275SMatthew G Knepley + dm - the DM 258667a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type) 258767a56275SMatthew G Knepley 258867a56275SMatthew G Knepley Output Parameter: 258967a56275SMatthew G Knepley . M - pointer to new DM 259067a56275SMatthew G Knepley 259167a56275SMatthew G Knepley Notes: 259267a56275SMatthew G Knepley Cannot be used to convert a sequential DM to parallel or parallel to sequential, 259367a56275SMatthew G Knepley the MPI communicator of the generated DM is always the same as the communicator 259467a56275SMatthew G Knepley of the input DM. 259567a56275SMatthew G Knepley 259667a56275SMatthew G Knepley Level: intermediate 259767a56275SMatthew G Knepley 259867a56275SMatthew G Knepley .seealso: DMCreate() 259967a56275SMatthew G Knepley @*/ 260019fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M) 260167a56275SMatthew G Knepley { 260267a56275SMatthew G Knepley DM B; 260367a56275SMatthew G Knepley char convname[256]; 260467a56275SMatthew G Knepley PetscBool sametype, issame; 260567a56275SMatthew G Knepley PetscErrorCode ierr; 260667a56275SMatthew G Knepley 260767a56275SMatthew G Knepley PetscFunctionBegin; 260867a56275SMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 260967a56275SMatthew G Knepley PetscValidType(dm,1); 261067a56275SMatthew G Knepley PetscValidPointer(M,3); 2611251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr); 261267a56275SMatthew G Knepley ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); 261367a56275SMatthew G Knepley { 26140298fd71SBarry Smith PetscErrorCode (*conv)(DM, DMType, DM*) = NULL; 261567a56275SMatthew G Knepley 261667a56275SMatthew G Knepley /* 261767a56275SMatthew G Knepley Order of precedence: 261867a56275SMatthew G Knepley 1) See if a specialized converter is known to the current DM. 261967a56275SMatthew G Knepley 2) See if a specialized converter is known to the desired DM class. 262067a56275SMatthew G Knepley 3) See if a good general converter is registered for the desired class 262167a56275SMatthew G Knepley 4) See if a good general converter is known for the current matrix. 262267a56275SMatthew G Knepley 5) Use a really basic converter. 262367a56275SMatthew G Knepley */ 262467a56275SMatthew G Knepley 262567a56275SMatthew G Knepley /* 1) See if a specialized converter is known to the current DM and the desired class */ 262667a56275SMatthew G Knepley ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr); 262767a56275SMatthew G Knepley ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr); 262867a56275SMatthew G Knepley ierr = PetscStrcat(convname,"_");CHKERRQ(ierr); 262967a56275SMatthew G Knepley ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr); 263067a56275SMatthew G Knepley ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr); 26310005d66cSJed Brown ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr); 263267a56275SMatthew G Knepley if (conv) goto foundconv; 263367a56275SMatthew G Knepley 263467a56275SMatthew G Knepley /* 2) See if a specialized converter is known to the desired DM class. */ 263582f516ccSBarry Smith ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr); 263667a56275SMatthew G Knepley ierr = DMSetType(B, newtype);CHKERRQ(ierr); 263767a56275SMatthew G Knepley ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr); 263867a56275SMatthew G Knepley ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr); 263967a56275SMatthew G Knepley ierr = PetscStrcat(convname,"_");CHKERRQ(ierr); 264067a56275SMatthew G Knepley ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr); 264167a56275SMatthew G Knepley ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr); 26420005d66cSJed Brown ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr); 264367a56275SMatthew G Knepley if (conv) { 2644fcfd50ebSBarry Smith ierr = DMDestroy(&B);CHKERRQ(ierr); 264567a56275SMatthew G Knepley goto foundconv; 264667a56275SMatthew G Knepley } 264767a56275SMatthew G Knepley 264867a56275SMatthew G Knepley #if 0 264967a56275SMatthew G Knepley /* 3) See if a good general converter is registered for the desired class */ 265067a56275SMatthew G Knepley conv = B->ops->convertfrom; 2651fcfd50ebSBarry Smith ierr = DMDestroy(&B);CHKERRQ(ierr); 265267a56275SMatthew G Knepley if (conv) goto foundconv; 265367a56275SMatthew G Knepley 265467a56275SMatthew G Knepley /* 4) See if a good general converter is known for the current matrix */ 265567a56275SMatthew G Knepley if (dm->ops->convert) { 265667a56275SMatthew G Knepley conv = dm->ops->convert; 265767a56275SMatthew G Knepley } 265867a56275SMatthew G Knepley if (conv) goto foundconv; 265967a56275SMatthew G Knepley #endif 266067a56275SMatthew G Knepley 266167a56275SMatthew G Knepley /* 5) Use a really basic converter. */ 266282f516ccSBarry Smith SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype); 266367a56275SMatthew G Knepley 266467a56275SMatthew G Knepley foundconv: 266567a56275SMatthew G Knepley ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr); 266667a56275SMatthew G Knepley ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr); 266767a56275SMatthew G Knepley ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr); 266867a56275SMatthew G Knepley } 266967a56275SMatthew G Knepley ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr); 267067a56275SMatthew G Knepley PetscFunctionReturn(0); 267167a56275SMatthew G Knepley } 2672264ace61SBarry Smith 2673264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/ 2674264ace61SBarry Smith 2675264ace61SBarry Smith #undef __FUNCT__ 2676264ace61SBarry Smith #define __FUNCT__ "DMRegister" 2677264ace61SBarry Smith /*@C 26781c84c290SBarry Smith DMRegister - Adds a new DM component implementation 26791c84c290SBarry Smith 26801c84c290SBarry Smith Not Collective 26811c84c290SBarry Smith 26821c84c290SBarry Smith Input Parameters: 26831c84c290SBarry Smith + name - The name of a new user-defined creation routine 26841c84c290SBarry Smith - create_func - The creation routine itself 26851c84c290SBarry Smith 26861c84c290SBarry Smith Notes: 26871c84c290SBarry Smith DMRegister() may be called multiple times to add several user-defined DMs 26881c84c290SBarry Smith 26891c84c290SBarry Smith 26901c84c290SBarry Smith Sample usage: 26911c84c290SBarry Smith .vb 2692bdf89e91SBarry Smith DMRegister("my_da", MyDMCreate); 26931c84c290SBarry Smith .ve 26941c84c290SBarry Smith 26951c84c290SBarry Smith Then, your DM type can be chosen with the procedural interface via 26961c84c290SBarry Smith .vb 26971c84c290SBarry Smith DMCreate(MPI_Comm, DM *); 26981c84c290SBarry Smith DMSetType(DM,"my_da"); 26991c84c290SBarry Smith .ve 27001c84c290SBarry Smith or at runtime via the option 27011c84c290SBarry Smith .vb 27021c84c290SBarry Smith -da_type my_da 27031c84c290SBarry Smith .ve 2704264ace61SBarry Smith 2705264ace61SBarry Smith Level: advanced 27061c84c290SBarry Smith 27071c84c290SBarry Smith .keywords: DM, register 2708bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy() 27091c84c290SBarry Smith 2710264ace61SBarry Smith @*/ 2711bdf89e91SBarry Smith PetscErrorCode DMRegister(const char sname[],PetscErrorCode (*function)(DM)) 2712264ace61SBarry Smith { 2713264ace61SBarry Smith PetscErrorCode ierr; 2714264ace61SBarry Smith 2715264ace61SBarry Smith PetscFunctionBegin; 2716a240a19fSJed Brown ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr); 2717264ace61SBarry Smith PetscFunctionReturn(0); 2718264ace61SBarry Smith } 2719264ace61SBarry Smith 2720b859378eSBarry Smith #undef __FUNCT__ 2721b859378eSBarry Smith #define __FUNCT__ "DMLoad" 2722b859378eSBarry Smith /*@C 272355849f57SBarry Smith DMLoad - Loads a DM that has been stored in binary with DMView(). 2724b859378eSBarry Smith 2725b859378eSBarry Smith Collective on PetscViewer 2726b859378eSBarry Smith 2727b859378eSBarry Smith Input Parameters: 2728b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or 2729b859378eSBarry Smith some related function before a call to DMLoad(). 2730b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or 2731b859378eSBarry Smith HDF5 file viewer, obtained from PetscViewerHDF5Open() 2732b859378eSBarry Smith 2733b859378eSBarry Smith Level: intermediate 2734b859378eSBarry Smith 2735b859378eSBarry Smith Notes: 273655849f57SBarry Smith The type is determined by the data in the file, any type set into the DM before this call is ignored. 2737b859378eSBarry Smith 2738b859378eSBarry Smith Notes for advanced users: 2739b859378eSBarry Smith Most users should not need to know the details of the binary storage 2740b859378eSBarry Smith format, since DMLoad() and DMView() completely hide these details. 2741b859378eSBarry Smith But for anyone who's interested, the standard binary matrix storage 2742b859378eSBarry Smith format is 2743b859378eSBarry Smith .vb 2744b859378eSBarry Smith has not yet been determined 2745b859378eSBarry Smith .ve 2746b859378eSBarry Smith 2747b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad() 2748b859378eSBarry Smith @*/ 2749b859378eSBarry Smith PetscErrorCode DMLoad(DM newdm, PetscViewer viewer) 2750b859378eSBarry Smith { 2751b859378eSBarry Smith PetscErrorCode ierr; 275232c0f0efSBarry Smith PetscBool isbinary; 275355849f57SBarry Smith PetscInt classid; 275432c0f0efSBarry Smith char type[256]; 2755b859378eSBarry Smith 2756b859378eSBarry Smith PetscFunctionBegin; 2757b859378eSBarry Smith PetscValidHeaderSpecific(newdm,DM_CLASSID,1); 2758b859378eSBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 275932c0f0efSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 276032c0f0efSBarry Smith if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()"); 2761b859378eSBarry Smith 276232c0f0efSBarry Smith ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr); 27639200755eSBarry Smith if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid); 276432c0f0efSBarry Smith ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr); 276532c0f0efSBarry Smith ierr = DMSetType(newdm, type);CHKERRQ(ierr); 27662d53ad75SBarry Smith if (newdm->ops->load) { 2767b859378eSBarry Smith ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr); 27682d53ad75SBarry Smith } 2769b859378eSBarry Smith PetscFunctionReturn(0); 2770b859378eSBarry Smith } 2771b859378eSBarry Smith 27727da65231SMatthew G Knepley /******************************** FEM Support **********************************/ 27737da65231SMatthew G Knepley 27747da65231SMatthew G Knepley #undef __FUNCT__ 27757da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector" 2776a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[]) 2777a6dfd86eSKarl Rupp { 27781d47ebbbSSatish Balay PetscInt f; 27791b30c384SMatthew G Knepley PetscErrorCode ierr; 27801b30c384SMatthew G Knepley 27817da65231SMatthew G Knepley PetscFunctionBegin; 278274778d6cSJed Brown ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr); 27831d47ebbbSSatish Balay for (f = 0; f < len; ++f) { 2784*57622a8eSBarry Smith ierr = PetscPrintf(PETSC_COMM_SELF, " | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr); 27857da65231SMatthew G Knepley } 27867da65231SMatthew G Knepley PetscFunctionReturn(0); 27877da65231SMatthew G Knepley } 27887da65231SMatthew G Knepley 27897da65231SMatthew G Knepley #undef __FUNCT__ 27907da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix" 2791a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[]) 2792a6dfd86eSKarl Rupp { 27931b30c384SMatthew G Knepley PetscInt f, g; 27947da65231SMatthew G Knepley PetscErrorCode ierr; 27957da65231SMatthew G Knepley 27967da65231SMatthew G Knepley PetscFunctionBegin; 279774778d6cSJed Brown ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr); 27981d47ebbbSSatish Balay for (f = 0; f < rows; ++f) { 279974778d6cSJed Brown ierr = PetscPrintf(PETSC_COMM_SELF, " |");CHKERRQ(ierr); 28001d47ebbbSSatish Balay for (g = 0; g < cols; ++g) { 280174778d6cSJed Brown ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5G", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr); 28027da65231SMatthew G Knepley } 280374778d6cSJed Brown ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr); 28047da65231SMatthew G Knepley } 28057da65231SMatthew G Knepley PetscFunctionReturn(0); 28067da65231SMatthew G Knepley } 2807e7c4fc90SDmitry Karpeev 2808970e74d5SMatthew G Knepley #undef __FUNCT__ 2809e759306cSMatthew G. Knepley #define __FUNCT__ "DMPrintLocalVec" 28106113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X) 2811e759306cSMatthew G. Knepley { 2812e759306cSMatthew G. Knepley PetscMPIInt rank, numProcs; 2813e759306cSMatthew G. Knepley PetscInt p; 2814e759306cSMatthew G. Knepley PetscErrorCode ierr; 2815e759306cSMatthew G. Knepley 2816e759306cSMatthew G. Knepley PetscFunctionBegin; 2817e759306cSMatthew G. Knepley ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr); 2818e759306cSMatthew G. Knepley ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm), &numProcs);CHKERRQ(ierr); 2819e759306cSMatthew G. Knepley ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "%s:\n", name);CHKERRQ(ierr); 2820e759306cSMatthew G. Knepley for (p = 0; p < numProcs; ++p) { 2821e759306cSMatthew G. Knepley if (p == rank) { 2822e759306cSMatthew G. Knepley Vec x; 2823e759306cSMatthew G. Knepley 2824e759306cSMatthew G. Knepley ierr = VecDuplicate(X, &x);CHKERRQ(ierr); 2825e759306cSMatthew G. Knepley ierr = VecCopy(X, x);CHKERRQ(ierr); 28266113b454SMatthew G. Knepley ierr = VecChop(x, tol);CHKERRQ(ierr); 2827e759306cSMatthew G. Knepley ierr = VecView(x, PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr); 2828e759306cSMatthew G. Knepley ierr = VecDestroy(&x);CHKERRQ(ierr); 2829e759306cSMatthew G. Knepley ierr = PetscViewerFlush(PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr); 2830e759306cSMatthew G. Knepley } 2831e759306cSMatthew G. Knepley ierr = PetscBarrier((PetscObject) dm);CHKERRQ(ierr); 2832e759306cSMatthew G. Knepley } 2833e759306cSMatthew G. Knepley PetscFunctionReturn(0); 2834e759306cSMatthew G. Knepley } 2835e759306cSMatthew G. Knepley 2836e759306cSMatthew G. Knepley #undef __FUNCT__ 283788ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection" 283888ed4aceSMatthew G Knepley /*@ 283988ed4aceSMatthew G Knepley DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM. 284088ed4aceSMatthew G Knepley 284188ed4aceSMatthew G Knepley Input Parameter: 284288ed4aceSMatthew G Knepley . dm - The DM 284388ed4aceSMatthew G Knepley 284488ed4aceSMatthew G Knepley Output Parameter: 284588ed4aceSMatthew G Knepley . section - The PetscSection 284688ed4aceSMatthew G Knepley 284788ed4aceSMatthew G Knepley Level: intermediate 284888ed4aceSMatthew G Knepley 284988ed4aceSMatthew G Knepley Note: This gets a borrowed reference, so the user should not destroy this PetscSection. 285088ed4aceSMatthew G Knepley 285188ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection() 285288ed4aceSMatthew G Knepley @*/ 28530adebc6cSBarry Smith PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section) 28540adebc6cSBarry Smith { 285588ed4aceSMatthew G Knepley PetscFunctionBegin; 285688ed4aceSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 285788ed4aceSMatthew G Knepley PetscValidPointer(section, 2); 285888ed4aceSMatthew G Knepley *section = dm->defaultSection; 285988ed4aceSMatthew G Knepley PetscFunctionReturn(0); 286088ed4aceSMatthew G Knepley } 286188ed4aceSMatthew G Knepley 286288ed4aceSMatthew G Knepley #undef __FUNCT__ 286388ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection" 286488ed4aceSMatthew G Knepley /*@ 286588ed4aceSMatthew G Knepley DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM. 286688ed4aceSMatthew G Knepley 286788ed4aceSMatthew G Knepley Input Parameters: 286888ed4aceSMatthew G Knepley + dm - The DM 286988ed4aceSMatthew G Knepley - section - The PetscSection 287088ed4aceSMatthew G Knepley 287188ed4aceSMatthew G Knepley Level: intermediate 287288ed4aceSMatthew G Knepley 287388ed4aceSMatthew G Knepley Note: Any existing Section will be destroyed 287488ed4aceSMatthew G Knepley 287588ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection() 287688ed4aceSMatthew G Knepley @*/ 28770adebc6cSBarry Smith PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section) 28780adebc6cSBarry Smith { 2879af122d2aSMatthew G Knepley PetscInt numFields; 2880af122d2aSMatthew G Knepley PetscInt f; 288188ed4aceSMatthew G Knepley PetscErrorCode ierr; 288288ed4aceSMatthew G Knepley 288388ed4aceSMatthew G Knepley PetscFunctionBegin; 288488ed4aceSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 28851d799100SJed Brown PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2); 28861d799100SJed Brown ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr); 288788ed4aceSMatthew G Knepley ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr); 288888ed4aceSMatthew G Knepley dm->defaultSection = section; 2889af122d2aSMatthew G Knepley ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr); 2890af122d2aSMatthew G Knepley if (numFields) { 2891af122d2aSMatthew G Knepley ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr); 2892af122d2aSMatthew G Knepley for (f = 0; f < numFields; ++f) { 2893af122d2aSMatthew G Knepley const char *name; 2894af122d2aSMatthew G Knepley 2895af122d2aSMatthew G Knepley ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr); 2896af122d2aSMatthew G Knepley ierr = PetscObjectSetName(dm->fields[f], name);CHKERRQ(ierr); 2897af122d2aSMatthew G Knepley } 2898af122d2aSMatthew G Knepley } 28991d799100SJed Brown /* The global section will be rebuilt in the next call to DMGetDefaultGlobalSection(). */ 29001d799100SJed Brown ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr); 290188ed4aceSMatthew G Knepley PetscFunctionReturn(0); 290288ed4aceSMatthew G Knepley } 290388ed4aceSMatthew G Knepley 290488ed4aceSMatthew G Knepley #undef __FUNCT__ 290588ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection" 290688ed4aceSMatthew G Knepley /*@ 290788ed4aceSMatthew G Knepley DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM. 290888ed4aceSMatthew G Knepley 29098b1ab98fSJed Brown Collective on DM 29108b1ab98fSJed Brown 291188ed4aceSMatthew G Knepley Input Parameter: 291288ed4aceSMatthew G Knepley . dm - The DM 291388ed4aceSMatthew G Knepley 291488ed4aceSMatthew G Knepley Output Parameter: 291588ed4aceSMatthew G Knepley . section - The PetscSection 291688ed4aceSMatthew G Knepley 291788ed4aceSMatthew G Knepley Level: intermediate 291888ed4aceSMatthew G Knepley 291988ed4aceSMatthew G Knepley Note: This gets a borrowed reference, so the user should not destroy this PetscSection. 292088ed4aceSMatthew G Knepley 292188ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection() 292288ed4aceSMatthew G Knepley @*/ 29230adebc6cSBarry Smith PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section) 29240adebc6cSBarry Smith { 292588ed4aceSMatthew G Knepley PetscErrorCode ierr; 292688ed4aceSMatthew G Knepley 292788ed4aceSMatthew G Knepley PetscFunctionBegin; 292888ed4aceSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 292988ed4aceSMatthew G Knepley PetscValidPointer(section, 2); 293088ed4aceSMatthew G Knepley if (!dm->defaultGlobalSection) { 293182f516ccSBarry 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"); 2932b21d0597SMatthew G Knepley ierr = PetscSectionCreateGlobalSection(dm->defaultSection, dm->sf, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr); 2933cf06b437SMatthew G. Knepley ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr); 2934ce94432eSBarry Smith ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm),dm->defaultGlobalSection,&dm->map);CHKERRQ(ierr); 293588ed4aceSMatthew G Knepley } 293688ed4aceSMatthew G Knepley *section = dm->defaultGlobalSection; 293788ed4aceSMatthew G Knepley PetscFunctionReturn(0); 293888ed4aceSMatthew G Knepley } 293988ed4aceSMatthew G Knepley 294088ed4aceSMatthew G Knepley #undef __FUNCT__ 2941b21d0597SMatthew G Knepley #define __FUNCT__ "DMSetDefaultGlobalSection" 2942b21d0597SMatthew G Knepley /*@ 2943b21d0597SMatthew G Knepley DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM. 2944b21d0597SMatthew G Knepley 2945b21d0597SMatthew G Knepley Input Parameters: 2946b21d0597SMatthew G Knepley + dm - The DM 29475080bbdbSMatthew G Knepley - section - The PetscSection, or NULL 2948b21d0597SMatthew G Knepley 2949b21d0597SMatthew G Knepley Level: intermediate 2950b21d0597SMatthew G Knepley 2951b21d0597SMatthew G Knepley Note: Any existing Section will be destroyed 2952b21d0597SMatthew G Knepley 2953b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection() 2954b21d0597SMatthew G Knepley @*/ 29550adebc6cSBarry Smith PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section) 29560adebc6cSBarry Smith { 2957b21d0597SMatthew G Knepley PetscErrorCode ierr; 2958b21d0597SMatthew G Knepley 2959b21d0597SMatthew G Knepley PetscFunctionBegin; 2960b21d0597SMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 29615080bbdbSMatthew G Knepley if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2); 29621d799100SJed Brown ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr); 2963b21d0597SMatthew G Knepley ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr); 2964b21d0597SMatthew G Knepley dm->defaultGlobalSection = section; 2965b21d0597SMatthew G Knepley PetscFunctionReturn(0); 2966b21d0597SMatthew G Knepley } 2967b21d0597SMatthew G Knepley 2968b21d0597SMatthew G Knepley #undef __FUNCT__ 296988ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF" 297088ed4aceSMatthew G Knepley /*@ 297188ed4aceSMatthew G Knepley DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set, 297288ed4aceSMatthew G Knepley it is created from the default PetscSection layouts in the DM. 297388ed4aceSMatthew G Knepley 297488ed4aceSMatthew G Knepley Input Parameter: 297588ed4aceSMatthew G Knepley . dm - The DM 297688ed4aceSMatthew G Knepley 297788ed4aceSMatthew G Knepley Output Parameter: 297888ed4aceSMatthew G Knepley . sf - The PetscSF 297988ed4aceSMatthew G Knepley 298088ed4aceSMatthew G Knepley Level: intermediate 298188ed4aceSMatthew G Knepley 298288ed4aceSMatthew G Knepley Note: This gets a borrowed reference, so the user should not destroy this PetscSF. 298388ed4aceSMatthew G Knepley 298488ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF() 298588ed4aceSMatthew G Knepley @*/ 29860adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf) 29870adebc6cSBarry Smith { 298888ed4aceSMatthew G Knepley PetscInt nroots; 298988ed4aceSMatthew G Knepley PetscErrorCode ierr; 299088ed4aceSMatthew G Knepley 299188ed4aceSMatthew G Knepley PetscFunctionBegin; 299288ed4aceSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 299388ed4aceSMatthew G Knepley PetscValidPointer(sf, 2); 29940298fd71SBarry Smith ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr); 299588ed4aceSMatthew G Knepley if (nroots < 0) { 299688ed4aceSMatthew G Knepley PetscSection section, gSection; 299788ed4aceSMatthew G Knepley 299888ed4aceSMatthew G Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 299931ea6d37SMatthew G Knepley if (section) { 300088ed4aceSMatthew G Knepley ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr); 300188ed4aceSMatthew G Knepley ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr); 300231ea6d37SMatthew G Knepley } else { 30030298fd71SBarry Smith *sf = NULL; 300431ea6d37SMatthew G Knepley PetscFunctionReturn(0); 300531ea6d37SMatthew G Knepley } 300688ed4aceSMatthew G Knepley } 300788ed4aceSMatthew G Knepley *sf = dm->defaultSF; 300888ed4aceSMatthew G Knepley PetscFunctionReturn(0); 300988ed4aceSMatthew G Knepley } 301088ed4aceSMatthew G Knepley 301188ed4aceSMatthew G Knepley #undef __FUNCT__ 301288ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF" 301388ed4aceSMatthew G Knepley /*@ 301488ed4aceSMatthew G Knepley DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM 301588ed4aceSMatthew G Knepley 301688ed4aceSMatthew G Knepley Input Parameters: 301788ed4aceSMatthew G Knepley + dm - The DM 301888ed4aceSMatthew G Knepley - sf - The PetscSF 301988ed4aceSMatthew G Knepley 302088ed4aceSMatthew G Knepley Level: intermediate 302188ed4aceSMatthew G Knepley 302288ed4aceSMatthew G Knepley Note: Any previous SF is destroyed 302388ed4aceSMatthew G Knepley 302488ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF() 302588ed4aceSMatthew G Knepley @*/ 30260adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf) 30270adebc6cSBarry Smith { 302888ed4aceSMatthew G Knepley PetscErrorCode ierr; 302988ed4aceSMatthew G Knepley 303088ed4aceSMatthew G Knepley PetscFunctionBegin; 303188ed4aceSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 303288ed4aceSMatthew G Knepley PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2); 303388ed4aceSMatthew G Knepley ierr = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr); 303488ed4aceSMatthew G Knepley dm->defaultSF = sf; 303588ed4aceSMatthew G Knepley PetscFunctionReturn(0); 303688ed4aceSMatthew G Knepley } 303788ed4aceSMatthew G Knepley 303888ed4aceSMatthew G Knepley #undef __FUNCT__ 303988ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF" 304088ed4aceSMatthew G Knepley /*@C 304188ed4aceSMatthew G Knepley DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections 304288ed4aceSMatthew G Knepley describing the data layout. 304388ed4aceSMatthew G Knepley 304488ed4aceSMatthew G Knepley Input Parameters: 304588ed4aceSMatthew G Knepley + dm - The DM 304688ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout 304788ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout 304888ed4aceSMatthew G Knepley 304988ed4aceSMatthew G Knepley Level: intermediate 305088ed4aceSMatthew G Knepley 305188ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF() 305288ed4aceSMatthew G Knepley @*/ 305388ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection) 305488ed4aceSMatthew G Knepley { 305582f516ccSBarry Smith MPI_Comm comm; 305688ed4aceSMatthew G Knepley PetscLayout layout; 305788ed4aceSMatthew G Knepley const PetscInt *ranges; 305888ed4aceSMatthew G Knepley PetscInt *local; 305988ed4aceSMatthew G Knepley PetscSFNode *remote; 3060ecd73843SMatthew G. Knepley PetscInt pStart, pEnd, p, nroots, nleaves = 0, l; 306188ed4aceSMatthew G Knepley PetscMPIInt size, rank; 306288ed4aceSMatthew G Knepley PetscErrorCode ierr; 306388ed4aceSMatthew G Knepley 306488ed4aceSMatthew G Knepley PetscFunctionBegin; 306582f516ccSBarry Smith ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr); 306688ed4aceSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 306788ed4aceSMatthew G Knepley ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr); 306888ed4aceSMatthew G Knepley ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr); 306988ed4aceSMatthew G Knepley ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr); 307088ed4aceSMatthew G Knepley ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr); 307188ed4aceSMatthew G Knepley ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr); 307288ed4aceSMatthew G Knepley ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr); 307388ed4aceSMatthew G Knepley ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr); 307488ed4aceSMatthew G Knepley ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr); 307588ed4aceSMatthew G Knepley ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr); 3076ecd73843SMatthew G. Knepley for (p = pStart; p < pEnd; ++p) { 30776636e97aSMatthew G Knepley PetscInt gdof, gcdof; 307888ed4aceSMatthew G Knepley 30796636e97aSMatthew G Knepley ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr); 30806636e97aSMatthew G Knepley ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr); 30816636e97aSMatthew G Knepley nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof; 308288ed4aceSMatthew G Knepley } 3083785e854fSJed Brown ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr); 3084785e854fSJed Brown ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr); 308588ed4aceSMatthew G Knepley for (p = pStart, l = 0; p < pEnd; ++p) { 30861f588964SMatthew G Knepley const PetscInt *cind; 30876636e97aSMatthew G Knepley PetscInt dof, cdof, off, gdof, gcdof, goff, gsize, d, c; 308888ed4aceSMatthew G Knepley 308988ed4aceSMatthew G Knepley ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr); 309088ed4aceSMatthew G Knepley ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr); 309188ed4aceSMatthew G Knepley ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr); 309288ed4aceSMatthew G Knepley ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr); 309388ed4aceSMatthew G Knepley ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr); 30946636e97aSMatthew G Knepley ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr); 309588ed4aceSMatthew G Knepley ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr); 30966636e97aSMatthew G Knepley if (!gdof) continue; /* Censored point */ 30976636e97aSMatthew G Knepley gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof; 30986636e97aSMatthew G Knepley if (gsize != dof-cdof) { 3099057b4bcdSMatthew 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); 31006636e97aSMatthew G Knepley cdof = 0; /* Ignore constraints */ 31016636e97aSMatthew G Knepley } 310288ed4aceSMatthew G Knepley for (d = 0, c = 0; d < dof; ++d) { 310388ed4aceSMatthew G Knepley if ((c < cdof) && (cind[c] == d)) {++c; continue;} 310488ed4aceSMatthew G Knepley local[l+d-c] = off+d; 310588ed4aceSMatthew G Knepley } 310688ed4aceSMatthew G Knepley if (gdof < 0) { 31076636e97aSMatthew G Knepley for (d = 0; d < gsize; ++d, ++l) { 310888ed4aceSMatthew G Knepley PetscInt offset = -(goff+1) + d, r; 310988ed4aceSMatthew G Knepley 311005376888SMatthew G. Knepley ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr); 311131d3f06eSJed Brown if (r < 0) r = -(r+2); 311205376888SMatthew G. Knepley if ((r < 0) || (r >= size)) SETERRQ4(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %d mapped to invalid process %d (%d, %d)", p, r, gdof, goff); 311388ed4aceSMatthew G Knepley remote[l].rank = r; 311488ed4aceSMatthew G Knepley remote[l].index = offset - ranges[r]; 311588ed4aceSMatthew G Knepley } 311688ed4aceSMatthew G Knepley } else { 31176636e97aSMatthew G Knepley for (d = 0; d < gsize; ++d, ++l) { 311888ed4aceSMatthew G Knepley remote[l].rank = rank; 311988ed4aceSMatthew G Knepley remote[l].index = goff+d - ranges[rank]; 312088ed4aceSMatthew G Knepley } 312188ed4aceSMatthew G Knepley } 312288ed4aceSMatthew G Knepley } 31236636e97aSMatthew G Knepley if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves); 312488ed4aceSMatthew G Knepley ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr); 312588ed4aceSMatthew G Knepley ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr); 312688ed4aceSMatthew G Knepley PetscFunctionReturn(0); 312788ed4aceSMatthew G Knepley } 3128af122d2aSMatthew G Knepley 3129af122d2aSMatthew G Knepley #undef __FUNCT__ 3130b21d0597SMatthew G Knepley #define __FUNCT__ "DMGetPointSF" 3131b21d0597SMatthew G Knepley /*@ 3132b21d0597SMatthew G Knepley DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM. 3133b21d0597SMatthew G Knepley 3134b21d0597SMatthew G Knepley Input Parameter: 3135b21d0597SMatthew G Knepley . dm - The DM 3136b21d0597SMatthew G Knepley 3137b21d0597SMatthew G Knepley Output Parameter: 3138b21d0597SMatthew G Knepley . sf - The PetscSF 3139b21d0597SMatthew G Knepley 3140b21d0597SMatthew G Knepley Level: intermediate 3141b21d0597SMatthew G Knepley 3142b21d0597SMatthew G Knepley Note: This gets a borrowed reference, so the user should not destroy this PetscSF. 3143b21d0597SMatthew G Knepley 3144057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF() 3145b21d0597SMatthew G Knepley @*/ 31460adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf) 31470adebc6cSBarry Smith { 3148b21d0597SMatthew G Knepley PetscFunctionBegin; 3149b21d0597SMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 3150b21d0597SMatthew G Knepley PetscValidPointer(sf, 2); 3151b21d0597SMatthew G Knepley *sf = dm->sf; 3152b21d0597SMatthew G Knepley PetscFunctionReturn(0); 3153b21d0597SMatthew G Knepley } 3154b21d0597SMatthew G Knepley 3155b21d0597SMatthew G Knepley #undef __FUNCT__ 3156057b4bcdSMatthew G Knepley #define __FUNCT__ "DMSetPointSF" 3157057b4bcdSMatthew G Knepley /*@ 3158057b4bcdSMatthew G Knepley DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM. 3159057b4bcdSMatthew G Knepley 3160057b4bcdSMatthew G Knepley Input Parameters: 3161057b4bcdSMatthew G Knepley + dm - The DM 3162057b4bcdSMatthew G Knepley - sf - The PetscSF 3163057b4bcdSMatthew G Knepley 3164057b4bcdSMatthew G Knepley Level: intermediate 3165057b4bcdSMatthew G Knepley 3166057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF() 3167057b4bcdSMatthew G Knepley @*/ 31680adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf) 31690adebc6cSBarry Smith { 3170057b4bcdSMatthew G Knepley PetscErrorCode ierr; 3171057b4bcdSMatthew G Knepley 3172057b4bcdSMatthew G Knepley PetscFunctionBegin; 3173057b4bcdSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 3174057b4bcdSMatthew G Knepley PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1); 3175057b4bcdSMatthew G Knepley ierr = PetscSFDestroy(&dm->sf);CHKERRQ(ierr); 3176057b4bcdSMatthew G Knepley ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr); 3177057b4bcdSMatthew G Knepley dm->sf = sf; 3178057b4bcdSMatthew G Knepley PetscFunctionReturn(0); 3179057b4bcdSMatthew G Knepley } 3180057b4bcdSMatthew G Knepley 3181057b4bcdSMatthew G Knepley #undef __FUNCT__ 3182af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetNumFields" 3183af122d2aSMatthew G Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields) 3184af122d2aSMatthew G Knepley { 3185af122d2aSMatthew G Knepley PetscFunctionBegin; 3186af122d2aSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 3187af122d2aSMatthew G Knepley PetscValidPointer(numFields, 2); 3188af122d2aSMatthew G Knepley *numFields = dm->numFields; 3189af122d2aSMatthew G Knepley PetscFunctionReturn(0); 3190af122d2aSMatthew G Knepley } 3191af122d2aSMatthew G Knepley 3192af122d2aSMatthew G Knepley #undef __FUNCT__ 3193af122d2aSMatthew G Knepley #define __FUNCT__ "DMSetNumFields" 3194af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields) 3195af122d2aSMatthew G Knepley { 3196af122d2aSMatthew G Knepley PetscInt f; 3197af122d2aSMatthew G Knepley PetscErrorCode ierr; 3198af122d2aSMatthew G Knepley 3199af122d2aSMatthew G Knepley PetscFunctionBegin; 3200af122d2aSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 3201af122d2aSMatthew G Knepley for (f = 0; f < dm->numFields; ++f) { 3202af122d2aSMatthew G Knepley ierr = PetscObjectDestroy(&dm->fields[f]);CHKERRQ(ierr); 3203af122d2aSMatthew G Knepley } 3204af122d2aSMatthew G Knepley ierr = PetscFree(dm->fields);CHKERRQ(ierr); 3205af122d2aSMatthew G Knepley dm->numFields = numFields; 3206785e854fSJed Brown ierr = PetscMalloc1(dm->numFields, &dm->fields);CHKERRQ(ierr); 3207af122d2aSMatthew G Knepley for (f = 0; f < dm->numFields; ++f) { 320882f516ccSBarry Smith ierr = PetscContainerCreate(PetscObjectComm((PetscObject)dm), (PetscContainer*) &dm->fields[f]);CHKERRQ(ierr); 3209af122d2aSMatthew G Knepley } 3210af122d2aSMatthew G Knepley PetscFunctionReturn(0); 3211af122d2aSMatthew G Knepley } 3212af122d2aSMatthew G Knepley 3213af122d2aSMatthew G Knepley #undef __FUNCT__ 3214af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetField" 3215af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field) 3216af122d2aSMatthew G Knepley { 3217af122d2aSMatthew G Knepley PetscFunctionBegin; 3218af122d2aSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 3219af122d2aSMatthew G Knepley PetscValidPointer(field, 2); 322082f516ccSBarry Smith if (!dm->fields) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "Fields have not been setup in this DM. Call DMSetNumFields()"); 322182f516ccSBarry 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); 3222af122d2aSMatthew G Knepley *field = dm->fields[f]; 3223af122d2aSMatthew G Knepley PetscFunctionReturn(0); 3224af122d2aSMatthew G Knepley } 32256636e97aSMatthew G Knepley 32266636e97aSMatthew G Knepley #undef __FUNCT__ 3227b64e0483SPeter Brune #define __FUNCT__ "DMRestrictHook_Coordinates" 3228b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx) 3229b64e0483SPeter Brune { 3230b64e0483SPeter Brune DM dm_coord,dmc_coord; 3231b64e0483SPeter Brune PetscErrorCode ierr; 3232b64e0483SPeter Brune Vec coords,ccoords; 3233b64e0483SPeter Brune VecScatter scat; 3234b64e0483SPeter Brune PetscFunctionBegin; 3235b64e0483SPeter Brune ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr); 3236b64e0483SPeter Brune ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr); 3237b64e0483SPeter Brune ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr); 3238b64e0483SPeter Brune ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr); 3239b64e0483SPeter Brune if (coords && !ccoords) { 3240b64e0483SPeter Brune ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr); 3241b64e0483SPeter Brune ierr = DMCreateInjection(dmc_coord,dm_coord,&scat);CHKERRQ(ierr); 3242b64e0483SPeter Brune ierr = VecScatterBegin(scat,coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 3243b64e0483SPeter Brune ierr = VecScatterEnd(scat,coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 3244b64e0483SPeter Brune ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr); 3245b64e0483SPeter Brune ierr = VecScatterDestroy(&scat);CHKERRQ(ierr); 3246b64e0483SPeter Brune ierr = VecDestroy(&ccoords);CHKERRQ(ierr); 3247b64e0483SPeter Brune } 3248b64e0483SPeter Brune PetscFunctionReturn(0); 3249b64e0483SPeter Brune } 3250b64e0483SPeter Brune 3251b64e0483SPeter Brune #undef __FUNCT__ 325203dadc2fSPeter Brune #define __FUNCT__ "DMSubDomainHook_Coordinates" 325303dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx) 325403dadc2fSPeter Brune { 325503dadc2fSPeter Brune DM dm_coord,subdm_coord; 325603dadc2fSPeter Brune PetscErrorCode ierr; 325703dadc2fSPeter Brune Vec coords,ccoords,clcoords; 325803dadc2fSPeter Brune VecScatter *scat_i,*scat_g; 325903dadc2fSPeter Brune PetscFunctionBegin; 326003dadc2fSPeter Brune ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr); 326103dadc2fSPeter Brune ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr); 326203dadc2fSPeter Brune ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr); 326303dadc2fSPeter Brune ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr); 326403dadc2fSPeter Brune if (coords && !ccoords) { 326503dadc2fSPeter Brune ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr); 326603dadc2fSPeter Brune ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr); 326703dadc2fSPeter Brune ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr); 326803dadc2fSPeter Brune ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 326903dadc2fSPeter Brune ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 327003dadc2fSPeter Brune ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 327103dadc2fSPeter Brune ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); 327203dadc2fSPeter Brune ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr); 327303dadc2fSPeter Brune ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr); 327403dadc2fSPeter Brune ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr); 327503dadc2fSPeter Brune ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr); 327603dadc2fSPeter Brune ierr = VecDestroy(&ccoords);CHKERRQ(ierr); 327703dadc2fSPeter Brune ierr = VecDestroy(&clcoords);CHKERRQ(ierr); 327803dadc2fSPeter Brune ierr = PetscFree(scat_i);CHKERRQ(ierr); 327903dadc2fSPeter Brune ierr = PetscFree(scat_g);CHKERRQ(ierr); 328003dadc2fSPeter Brune } 328103dadc2fSPeter Brune PetscFunctionReturn(0); 328203dadc2fSPeter Brune } 328303dadc2fSPeter Brune 328403dadc2fSPeter Brune #undef __FUNCT__ 32856636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinates" 32866636e97aSMatthew G Knepley /*@ 32876636e97aSMatthew G Knepley DMSetCoordinates - Sets into the DM a global vector that holds the coordinates 32886636e97aSMatthew G Knepley 32896636e97aSMatthew G Knepley Collective on DM 32906636e97aSMatthew G Knepley 32916636e97aSMatthew G Knepley Input Parameters: 32926636e97aSMatthew G Knepley + dm - the DM 32936636e97aSMatthew G Knepley - c - coordinate vector 32946636e97aSMatthew G Knepley 32956636e97aSMatthew G Knepley Note: 32966636e97aSMatthew G Knepley The coordinates do include those for ghost points, which are in the local vector 32976636e97aSMatthew G Knepley 32986636e97aSMatthew G Knepley Level: intermediate 32996636e97aSMatthew G Knepley 33006636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates 33016636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM() 33026636e97aSMatthew G Knepley @*/ 33036636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c) 33046636e97aSMatthew G Knepley { 33056636e97aSMatthew G Knepley PetscErrorCode ierr; 33066636e97aSMatthew G Knepley 33076636e97aSMatthew G Knepley PetscFunctionBegin; 33086636e97aSMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 33096636e97aSMatthew G Knepley PetscValidHeaderSpecific(c,VEC_CLASSID,2); 33106636e97aSMatthew G Knepley ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr); 33116636e97aSMatthew G Knepley ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr); 33126636e97aSMatthew G Knepley dm->coordinates = c; 33136636e97aSMatthew G Knepley ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr); 3314b64e0483SPeter Brune ierr = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr); 331503dadc2fSPeter Brune ierr = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr); 33166636e97aSMatthew G Knepley PetscFunctionReturn(0); 33176636e97aSMatthew G Knepley } 33186636e97aSMatthew G Knepley 33196636e97aSMatthew G Knepley #undef __FUNCT__ 33206636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinatesLocal" 33216636e97aSMatthew G Knepley /*@ 33226636e97aSMatthew G Knepley DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates 33236636e97aSMatthew G Knepley 33246636e97aSMatthew G Knepley Collective on DM 33256636e97aSMatthew G Knepley 33266636e97aSMatthew G Knepley Input Parameters: 33276636e97aSMatthew G Knepley + dm - the DM 33286636e97aSMatthew G Knepley - c - coordinate vector 33296636e97aSMatthew G Knepley 33306636e97aSMatthew G Knepley Note: 33316636e97aSMatthew G Knepley The coordinates of ghost points can be set using DMSetCoordinates() 33326636e97aSMatthew G Knepley followed by DMGetCoordinatesLocal(). This is intended to enable the 33336636e97aSMatthew G Knepley setting of ghost coordinates outside of the domain. 33346636e97aSMatthew G Knepley 33356636e97aSMatthew G Knepley Level: intermediate 33366636e97aSMatthew G Knepley 33376636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates 33386636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM() 33396636e97aSMatthew G Knepley @*/ 33406636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c) 33416636e97aSMatthew G Knepley { 33426636e97aSMatthew G Knepley PetscErrorCode ierr; 33436636e97aSMatthew G Knepley 33446636e97aSMatthew G Knepley PetscFunctionBegin; 33456636e97aSMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 33466636e97aSMatthew G Knepley PetscValidHeaderSpecific(c,VEC_CLASSID,2); 33476636e97aSMatthew G Knepley ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr); 33486636e97aSMatthew G Knepley ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr); 33498865f1eaSKarl Rupp 33506636e97aSMatthew G Knepley dm->coordinatesLocal = c; 33518865f1eaSKarl Rupp 33526636e97aSMatthew G Knepley ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr); 33536636e97aSMatthew G Knepley PetscFunctionReturn(0); 33546636e97aSMatthew G Knepley } 33556636e97aSMatthew G Knepley 33566636e97aSMatthew G Knepley #undef __FUNCT__ 33576636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinates" 33586636e97aSMatthew G Knepley /*@ 33596636e97aSMatthew G Knepley DMGetCoordinates - Gets a global vector with the coordinates associated with the DM. 33606636e97aSMatthew G Knepley 33616636e97aSMatthew G Knepley Not Collective 33626636e97aSMatthew G Knepley 33636636e97aSMatthew G Knepley Input Parameter: 33646636e97aSMatthew G Knepley . dm - the DM 33656636e97aSMatthew G Knepley 33666636e97aSMatthew G Knepley Output Parameter: 33676636e97aSMatthew G Knepley . c - global coordinate vector 33686636e97aSMatthew G Knepley 33696636e97aSMatthew G Knepley Note: 33706636e97aSMatthew G Knepley This is a borrowed reference, so the user should NOT destroy this vector 33716636e97aSMatthew G Knepley 33726636e97aSMatthew G Knepley Each process has only the local coordinates (does NOT have the ghost coordinates). 33736636e97aSMatthew G Knepley 33746636e97aSMatthew G Knepley For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...) 33756636e97aSMatthew G Knepley and (x_0,y_0,z_0,x_1,y_1,z_1...) 33766636e97aSMatthew G Knepley 33776636e97aSMatthew G Knepley Level: intermediate 33786636e97aSMatthew G Knepley 33796636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates 33806636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM() 33816636e97aSMatthew G Knepley @*/ 33826636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c) 33836636e97aSMatthew G Knepley { 33846636e97aSMatthew G Knepley PetscErrorCode ierr; 33856636e97aSMatthew G Knepley 33866636e97aSMatthew G Knepley PetscFunctionBegin; 33876636e97aSMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 33886636e97aSMatthew G Knepley PetscValidPointer(c,2); 33891f588964SMatthew G Knepley if (!dm->coordinates && dm->coordinatesLocal) { 33900298fd71SBarry Smith DM cdm = NULL; 33916636e97aSMatthew G Knepley 33926636e97aSMatthew G Knepley ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr); 33936636e97aSMatthew G Knepley ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr); 33946636e97aSMatthew G Knepley ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr); 33956636e97aSMatthew G Knepley ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr); 33966636e97aSMatthew G Knepley ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr); 33976636e97aSMatthew G Knepley } 33986636e97aSMatthew G Knepley *c = dm->coordinates; 33996636e97aSMatthew G Knepley PetscFunctionReturn(0); 34006636e97aSMatthew G Knepley } 34016636e97aSMatthew G Knepley 34026636e97aSMatthew G Knepley #undef __FUNCT__ 34036636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinatesLocal" 34046636e97aSMatthew G Knepley /*@ 34056636e97aSMatthew G Knepley DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM. 34066636e97aSMatthew G Knepley 34076636e97aSMatthew G Knepley Collective on DM 34086636e97aSMatthew G Knepley 34096636e97aSMatthew G Knepley Input Parameter: 34106636e97aSMatthew G Knepley . dm - the DM 34116636e97aSMatthew G Knepley 34126636e97aSMatthew G Knepley Output Parameter: 34136636e97aSMatthew G Knepley . c - coordinate vector 34146636e97aSMatthew G Knepley 34156636e97aSMatthew G Knepley Note: 34166636e97aSMatthew G Knepley This is a borrowed reference, so the user should NOT destroy this vector 34176636e97aSMatthew G Knepley 34186636e97aSMatthew G Knepley Each process has the local and ghost coordinates 34196636e97aSMatthew G Knepley 34206636e97aSMatthew G Knepley For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...) 34216636e97aSMatthew G Knepley and (x_0,y_0,z_0,x_1,y_1,z_1...) 34226636e97aSMatthew G Knepley 34236636e97aSMatthew G Knepley Level: intermediate 34246636e97aSMatthew G Knepley 34256636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates 34266636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM() 34276636e97aSMatthew G Knepley @*/ 34286636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c) 34296636e97aSMatthew G Knepley { 34306636e97aSMatthew G Knepley PetscErrorCode ierr; 34316636e97aSMatthew G Knepley 34326636e97aSMatthew G Knepley PetscFunctionBegin; 34336636e97aSMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 34346636e97aSMatthew G Knepley PetscValidPointer(c,2); 34351f588964SMatthew G Knepley if (!dm->coordinatesLocal && dm->coordinates) { 34360298fd71SBarry Smith DM cdm = NULL; 34376636e97aSMatthew G Knepley 34386636e97aSMatthew G Knepley ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr); 34396636e97aSMatthew G Knepley ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr); 34406636e97aSMatthew G Knepley ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr); 34416636e97aSMatthew G Knepley ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr); 34426636e97aSMatthew G Knepley ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr); 34436636e97aSMatthew G Knepley } 34446636e97aSMatthew G Knepley *c = dm->coordinatesLocal; 34456636e97aSMatthew G Knepley PetscFunctionReturn(0); 34466636e97aSMatthew G Knepley } 34476636e97aSMatthew G Knepley 34486636e97aSMatthew G Knepley #undef __FUNCT__ 34496636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinateDM" 34506636e97aSMatthew G Knepley /*@ 34511cfe2091SMatthew G. Knepley DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates 34526636e97aSMatthew G Knepley 34536636e97aSMatthew G Knepley Collective on DM 34546636e97aSMatthew G Knepley 34556636e97aSMatthew G Knepley Input Parameter: 34566636e97aSMatthew G Knepley . dm - the DM 34576636e97aSMatthew G Knepley 34586636e97aSMatthew G Knepley Output Parameter: 34596636e97aSMatthew G Knepley . cdm - coordinate DM 34606636e97aSMatthew G Knepley 34616636e97aSMatthew G Knepley Level: intermediate 34626636e97aSMatthew G Knepley 34636636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates 34641cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal() 34656636e97aSMatthew G Knepley @*/ 34666636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm) 34676636e97aSMatthew G Knepley { 34686636e97aSMatthew G Knepley PetscErrorCode ierr; 34696636e97aSMatthew G Knepley 34706636e97aSMatthew G Knepley PetscFunctionBegin; 34716636e97aSMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 34726636e97aSMatthew G Knepley PetscValidPointer(cdm,2); 34736636e97aSMatthew G Knepley if (!dm->coordinateDM) { 347482f516ccSBarry Smith if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM"); 34756636e97aSMatthew G Knepley ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr); 34766636e97aSMatthew G Knepley } 34776636e97aSMatthew G Knepley *cdm = dm->coordinateDM; 34786636e97aSMatthew G Knepley PetscFunctionReturn(0); 34796636e97aSMatthew G Knepley } 3480e87bb0d3SMatthew G Knepley 3481e87bb0d3SMatthew G Knepley #undef __FUNCT__ 34821cfe2091SMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateDM" 34831cfe2091SMatthew G. Knepley /*@ 34841cfe2091SMatthew G. Knepley DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates 34851cfe2091SMatthew G. Knepley 34861cfe2091SMatthew G. Knepley Logically Collective on DM 34871cfe2091SMatthew G. Knepley 34881cfe2091SMatthew G. Knepley Input Parameters: 34891cfe2091SMatthew G. Knepley + dm - the DM 34901cfe2091SMatthew G. Knepley - cdm - coordinate DM 34911cfe2091SMatthew G. Knepley 34921cfe2091SMatthew G. Knepley Level: intermediate 34931cfe2091SMatthew G. Knepley 34941cfe2091SMatthew G. Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates 34951cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal() 34961cfe2091SMatthew G. Knepley @*/ 34971cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm) 34981cfe2091SMatthew G. Knepley { 34991cfe2091SMatthew G. Knepley PetscErrorCode ierr; 35001cfe2091SMatthew G. Knepley 35011cfe2091SMatthew G. Knepley PetscFunctionBegin; 35021cfe2091SMatthew G. Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 35031cfe2091SMatthew G. Knepley PetscValidHeaderSpecific(cdm,DM_CLASSID,2); 35041cfe2091SMatthew G. Knepley ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr); 35051cfe2091SMatthew G. Knepley dm->coordinateDM = cdm; 35061cfe2091SMatthew G. Knepley ierr = PetscObjectReference((PetscObject) dm->coordinateDM);CHKERRQ(ierr); 35071cfe2091SMatthew G. Knepley PetscFunctionReturn(0); 35081cfe2091SMatthew G. Knepley } 35091cfe2091SMatthew G. Knepley 35101cfe2091SMatthew G. Knepley #undef __FUNCT__ 3511e8abe2deSMatthew G. Knepley #define __FUNCT__ "DMGetCoordinateSection" 3512e8abe2deSMatthew G. Knepley /*@ 3513e8abe2deSMatthew G. Knepley DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh. 3514e8abe2deSMatthew G. Knepley 3515e8abe2deSMatthew G. Knepley Not Collective 3516e8abe2deSMatthew G. Knepley 3517e8abe2deSMatthew G. Knepley Input Parameter: 3518e8abe2deSMatthew G. Knepley . dm - The DM object 3519e8abe2deSMatthew G. Knepley 3520e8abe2deSMatthew G. Knepley Output Parameter: 3521e8abe2deSMatthew G. Knepley . section - The PetscSection object 3522e8abe2deSMatthew G. Knepley 3523e8abe2deSMatthew G. Knepley Level: intermediate 3524e8abe2deSMatthew G. Knepley 3525e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates 3526e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection() 3527e8abe2deSMatthew G. Knepley @*/ 3528e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section) 3529e8abe2deSMatthew G. Knepley { 3530e8abe2deSMatthew G. Knepley DM cdm; 3531e8abe2deSMatthew G. Knepley PetscErrorCode ierr; 3532e8abe2deSMatthew G. Knepley 3533e8abe2deSMatthew G. Knepley PetscFunctionBegin; 3534e8abe2deSMatthew G. Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 3535e8abe2deSMatthew G. Knepley PetscValidPointer(section, 2); 3536e8abe2deSMatthew G. Knepley ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr); 3537e8abe2deSMatthew G. Knepley ierr = DMGetDefaultSection(cdm, section);CHKERRQ(ierr); 3538e8abe2deSMatthew G. Knepley PetscFunctionReturn(0); 3539e8abe2deSMatthew G. Knepley } 3540e8abe2deSMatthew G. Knepley 3541e8abe2deSMatthew G. Knepley #undef __FUNCT__ 3542e8abe2deSMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateSection" 3543e8abe2deSMatthew G. Knepley /*@ 3544e8abe2deSMatthew G. Knepley DMSetCoordinateSection - Set the layout of coordinate values over the mesh. 3545e8abe2deSMatthew G. Knepley 3546e8abe2deSMatthew G. Knepley Not Collective 3547e8abe2deSMatthew G. Knepley 3548e8abe2deSMatthew G. Knepley Input Parameters: 3549e8abe2deSMatthew G. Knepley + dm - The DM object 3550e8abe2deSMatthew G. Knepley - section - The PetscSection object 3551e8abe2deSMatthew G. Knepley 3552e8abe2deSMatthew G. Knepley Level: intermediate 3553e8abe2deSMatthew G. Knepley 3554e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates 3555e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection() 3556e8abe2deSMatthew G. Knepley @*/ 3557e8abe2deSMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscSection section) 3558e8abe2deSMatthew G. Knepley { 3559e8abe2deSMatthew G. Knepley DM cdm; 3560e8abe2deSMatthew G. Knepley PetscErrorCode ierr; 3561e8abe2deSMatthew G. Knepley 3562e8abe2deSMatthew G. Knepley PetscFunctionBegin; 3563e8abe2deSMatthew G. Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 3564e8abe2deSMatthew G. Knepley PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2); 3565e8abe2deSMatthew G. Knepley ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr); 3566e8abe2deSMatthew G. Knepley ierr = DMSetDefaultSection(cdm, section);CHKERRQ(ierr); 3567e8abe2deSMatthew G. Knepley PetscFunctionReturn(0); 3568e8abe2deSMatthew G. Knepley } 3569e8abe2deSMatthew G. Knepley 3570e8abe2deSMatthew G. Knepley #undef __FUNCT__ 3571e87bb0d3SMatthew G Knepley #define __FUNCT__ "DMLocatePoints" 3572e87bb0d3SMatthew G Knepley /*@ 3573e87bb0d3SMatthew G Knepley DMLocatePoints - Locate the points in v in the mesh and return an IS of the containing cells 3574e87bb0d3SMatthew G Knepley 3575e87bb0d3SMatthew G Knepley Not collective 3576e87bb0d3SMatthew G Knepley 3577e87bb0d3SMatthew G Knepley Input Parameters: 3578e87bb0d3SMatthew G Knepley + dm - The DM 3579e87bb0d3SMatthew G Knepley - v - The Vec of points 3580e87bb0d3SMatthew G Knepley 358161e3bb9bSMatthew G Knepley Output Parameter: 3582e87bb0d3SMatthew G Knepley . cells - The local cell numbers for cells which contain the points 3583e87bb0d3SMatthew G Knepley 3584e87bb0d3SMatthew G Knepley Level: developer 358561e3bb9bSMatthew G Knepley 358661e3bb9bSMatthew G Knepley .keywords: point location, mesh 358761e3bb9bSMatthew G Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal() 358861e3bb9bSMatthew G Knepley @*/ 3589e87bb0d3SMatthew G Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, IS *cells) 3590e87bb0d3SMatthew G Knepley { 3591735aa83eSMatthew G Knepley PetscErrorCode ierr; 3592735aa83eSMatthew G Knepley 3593e87bb0d3SMatthew G Knepley PetscFunctionBegin; 3594e87bb0d3SMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 3595e87bb0d3SMatthew G Knepley PetscValidHeaderSpecific(v,VEC_CLASSID,2); 3596e87bb0d3SMatthew G Knepley PetscValidPointer(cells,3); 3597735aa83eSMatthew G Knepley if (dm->ops->locatepoints) { 3598735aa83eSMatthew G Knepley ierr = (*dm->ops->locatepoints)(dm,v,cells);CHKERRQ(ierr); 359982f516ccSBarry Smith } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM"); 3600e87bb0d3SMatthew G Knepley PetscFunctionReturn(0); 3601e87bb0d3SMatthew G Knepley } 3602