1b45d2f2cSJed Brown #include <petsc-private/dmimpl.h> /*I "petscdm.h" I*/ 247c6ae99SBarry Smith 3732e2eb9SMatthew G Knepley PetscClassId DM_CLASSID; 467a56275SMatthew G Knepley PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal; 567a56275SMatthew G Knepley 647c6ae99SBarry Smith #undef __FUNCT__ 7ca266f36SBarry Smith #define __FUNCT__ "DMViewFromOptions" 8ca266f36SBarry Smith /* 9ca266f36SBarry Smith Processes command line options to determine if/how a dm 10ca266f36SBarry Smith is to be viewed. 11ca266f36SBarry Smith 12ca266f36SBarry Smith */ 13ca266f36SBarry Smith PetscErrorCode DMViewFromOptions(DM dm,const char optionname[]) 14ca266f36SBarry Smith { 15ca266f36SBarry Smith PetscErrorCode ierr; 16ca266f36SBarry Smith PetscBool flg; 17ca266f36SBarry Smith PetscViewer viewer; 18cffb1e40SBarry Smith PetscViewerFormat format; 19ca266f36SBarry Smith 20ca266f36SBarry Smith PetscFunctionBegin; 21cffb1e40SBarry Smith ierr = PetscOptionsGetViewer(((PetscObject)dm)->comm,((PetscObject)dm)->prefix,optionname,&viewer,&format,&flg);CHKERRQ(ierr); 22ca266f36SBarry Smith if (flg) { 23cffb1e40SBarry Smith ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr); 24ca266f36SBarry Smith ierr = DMView(dm,viewer);CHKERRQ(ierr); 25cffb1e40SBarry Smith ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); 26cffb1e40SBarry Smith ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); 27ca266f36SBarry Smith } 28ca266f36SBarry Smith PetscFunctionReturn(0); 29ca266f36SBarry Smith } 30ca266f36SBarry Smith 31ca266f36SBarry Smith 32ca266f36SBarry Smith #undef __FUNCT__ 33a4121054SBarry Smith #define __FUNCT__ "DMCreate" 34a4121054SBarry Smith /*@ 35de043629SMatthew G Knepley DMCreate - Creates an empty DM object. The type can then be set with DMSetType(). 36a4121054SBarry Smith 37a4121054SBarry Smith If you never call DMSetType() it will generate an 38a4121054SBarry Smith error when you try to use the vector. 39a4121054SBarry Smith 40a4121054SBarry Smith Collective on MPI_Comm 41a4121054SBarry Smith 42a4121054SBarry Smith Input Parameter: 43a4121054SBarry Smith . comm - The communicator for the DM object 44a4121054SBarry Smith 45a4121054SBarry Smith Output Parameter: 46a4121054SBarry Smith . dm - The DM object 47a4121054SBarry Smith 48a4121054SBarry Smith Level: beginner 49a4121054SBarry Smith 50a4121054SBarry Smith .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE 51a4121054SBarry Smith @*/ 527087cfbeSBarry Smith PetscErrorCode DMCreate(MPI_Comm comm,DM *dm) 53a4121054SBarry Smith { 54a4121054SBarry Smith DM v; 55a4121054SBarry Smith PetscErrorCode ierr; 56a4121054SBarry Smith 57a4121054SBarry Smith PetscFunctionBegin; 581411c6eeSJed Brown PetscValidPointer(dm,2); 591411c6eeSJed Brown *dm = PETSC_NULL; 60a4121054SBarry Smith #ifndef PETSC_USE_DYNAMIC_LIBRARIES 61b84caa0eSBarry Smith ierr = VecInitializePackage(PETSC_NULL);CHKERRQ(ierr); 62b84caa0eSBarry Smith ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr); 63a4121054SBarry Smith ierr = DMInitializePackage(PETSC_NULL);CHKERRQ(ierr); 64a4121054SBarry Smith #endif 65a4121054SBarry Smith 663194b578SJed Brown ierr = PetscHeaderCreate(v, _p_DM, struct _DMOps, DM_CLASSID, -1, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr); 67a4121054SBarry Smith ierr = PetscMemzero(v->ops, sizeof(struct _DMOps));CHKERRQ(ierr); 681411c6eeSJed Brown 69e7c4fc90SDmitry Karpeev 701411c6eeSJed Brown v->ltogmap = PETSC_NULL; 711411c6eeSJed Brown v->ltogmapb = PETSC_NULL; 721411c6eeSJed Brown v->bs = 1; 73171400e9SBarry Smith v->coloringtype = IS_COLORING_GLOBAL; 7488ed4aceSMatthew G Knepley ierr = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr); 7588ed4aceSMatthew G Knepley ierr = PetscSFCreate(comm, &v->defaultSF);CHKERRQ(ierr); 7688ed4aceSMatthew G Knepley v->defaultSection = PETSC_NULL; 7788ed4aceSMatthew G Knepley v->defaultGlobalSection = PETSC_NULL; 78435a35e8SMatthew G Knepley { 79435a35e8SMatthew G Knepley PetscInt i; 80435a35e8SMatthew G Knepley for (i = 0; i < 10; ++i) { 81435a35e8SMatthew G Knepley v->nullspaceConstructors[i] = PETSC_NULL; 82435a35e8SMatthew G Knepley } 83435a35e8SMatthew G Knepley } 84af122d2aSMatthew G Knepley v->numFields = 0; 85af122d2aSMatthew G Knepley v->fields = PETSC_NULL; 861411c6eeSJed Brown 871411c6eeSJed Brown *dm = v; 88a4121054SBarry Smith PetscFunctionReturn(0); 89a4121054SBarry Smith } 90a4121054SBarry Smith 91a4121054SBarry Smith #undef __FUNCT__ 929a42bb27SBarry Smith #define __FUNCT__ "DMSetVecType" 939a42bb27SBarry Smith /*@C 94564755cdSBarry Smith DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector() 959a42bb27SBarry Smith 96aa219208SBarry Smith Logically Collective on DMDA 979a42bb27SBarry Smith 989a42bb27SBarry Smith Input Parameter: 999a42bb27SBarry Smith + da - initial distributed array 1008154be41SBarry Smith . ctype - the vector type, currently either VECSTANDARD or VECCUSP 1019a42bb27SBarry Smith 1029a42bb27SBarry Smith Options Database: 103dd85299cSBarry Smith . -dm_vec_type ctype 1049a42bb27SBarry Smith 1059a42bb27SBarry Smith Level: intermediate 1069a42bb27SBarry Smith 107aa219208SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMDestroy(), DMDA, DMDAInterpolationType, VecType 1089a42bb27SBarry Smith @*/ 10919fd82e9SBarry Smith PetscErrorCode DMSetVecType(DM da,VecType ctype) 1109a42bb27SBarry Smith { 1119a42bb27SBarry Smith PetscErrorCode ierr; 1129a42bb27SBarry Smith 1139a42bb27SBarry Smith PetscFunctionBegin; 1149a42bb27SBarry Smith PetscValidHeaderSpecific(da,DM_CLASSID,1); 1159a42bb27SBarry Smith ierr = PetscFree(da->vectype);CHKERRQ(ierr); 11619fd82e9SBarry Smith ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr); 1179a42bb27SBarry Smith PetscFunctionReturn(0); 1189a42bb27SBarry Smith } 1199a42bb27SBarry Smith 1209a42bb27SBarry Smith #undef __FUNCT__ 1215f1ad066SMatthew G Knepley #define __FUNCT__ "VecGetDM" 1225f1ad066SMatthew G Knepley /*@ 12334f98d34SBarry Smith VecGetDM - Gets the DM defining the data layout of the vector 1245f1ad066SMatthew G Knepley 1255f1ad066SMatthew G Knepley Not collective 1265f1ad066SMatthew G Knepley 1275f1ad066SMatthew G Knepley Input Parameter: 1285f1ad066SMatthew G Knepley . v - The Vec 1295f1ad066SMatthew G Knepley 1305f1ad066SMatthew G Knepley Output Parameter: 1315f1ad066SMatthew G Knepley . dm - The DM 1325f1ad066SMatthew G Knepley 1335f1ad066SMatthew G Knepley Level: intermediate 1345f1ad066SMatthew G Knepley 1355f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType() 1365f1ad066SMatthew G Knepley @*/ 1375f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm) 1385f1ad066SMatthew G Knepley { 1395f1ad066SMatthew G Knepley PetscErrorCode ierr; 1405f1ad066SMatthew G Knepley 1415f1ad066SMatthew G Knepley PetscFunctionBegin; 1425f1ad066SMatthew G Knepley PetscValidHeaderSpecific(v,VEC_CLASSID,1); 1435f1ad066SMatthew G Knepley PetscValidPointer(dm,2); 1445f1ad066SMatthew G Knepley ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject *) dm);CHKERRQ(ierr); 1455f1ad066SMatthew G Knepley PetscFunctionReturn(0); 1465f1ad066SMatthew G Knepley } 1475f1ad066SMatthew G Knepley 1485f1ad066SMatthew G Knepley #undef __FUNCT__ 1495f1ad066SMatthew G Knepley #define __FUNCT__ "VecSetDM" 1505f1ad066SMatthew G Knepley /*@ 1515f1ad066SMatthew G Knepley VecSetDM - Sets the DM defining the data layout of the vector 1525f1ad066SMatthew G Knepley 1535f1ad066SMatthew G Knepley Not collective 1545f1ad066SMatthew G Knepley 1555f1ad066SMatthew G Knepley Input Parameters: 1565f1ad066SMatthew G Knepley + v - The Vec 1575f1ad066SMatthew G Knepley - dm - The DM 1585f1ad066SMatthew G Knepley 1595f1ad066SMatthew G Knepley Level: intermediate 1605f1ad066SMatthew G Knepley 1615f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType() 1625f1ad066SMatthew G Knepley @*/ 1635f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm) 1645f1ad066SMatthew G Knepley { 1655f1ad066SMatthew G Knepley PetscErrorCode ierr; 1665f1ad066SMatthew G Knepley 1675f1ad066SMatthew G Knepley PetscFunctionBegin; 1685f1ad066SMatthew G Knepley PetscValidHeaderSpecific(v,VEC_CLASSID,1); 1695f1ad066SMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,2); 1705f1ad066SMatthew G Knepley ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr); 1715f1ad066SMatthew G Knepley PetscFunctionReturn(0); 1725f1ad066SMatthew G Knepley } 1735f1ad066SMatthew G Knepley 1745f1ad066SMatthew G Knepley #undef __FUNCT__ 175521d9a4cSLisandro Dalcin #define __FUNCT__ "DMSetMatType" 176521d9a4cSLisandro Dalcin /*@C 177521d9a4cSLisandro Dalcin DMSetMatType - Sets the type of matrix created with DMCreateMatrix() 178521d9a4cSLisandro Dalcin 179521d9a4cSLisandro Dalcin Logically Collective on DM 180521d9a4cSLisandro Dalcin 181521d9a4cSLisandro Dalcin Input Parameter: 182521d9a4cSLisandro Dalcin + dm - the DM context 183521d9a4cSLisandro Dalcin . ctype - the matrix type 184521d9a4cSLisandro Dalcin 185521d9a4cSLisandro Dalcin Options Database: 186521d9a4cSLisandro Dalcin . -dm_mat_type ctype 187521d9a4cSLisandro Dalcin 188521d9a4cSLisandro Dalcin Level: intermediate 189521d9a4cSLisandro Dalcin 190521d9a4cSLisandro Dalcin .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType 191521d9a4cSLisandro Dalcin @*/ 19219fd82e9SBarry Smith PetscErrorCode DMSetMatType(DM dm,MatType ctype) 193521d9a4cSLisandro Dalcin { 194521d9a4cSLisandro Dalcin PetscErrorCode ierr; 19588f0584fSBarry Smith 196521d9a4cSLisandro Dalcin PetscFunctionBegin; 197521d9a4cSLisandro Dalcin PetscValidHeaderSpecific(dm,DM_CLASSID,1); 198521d9a4cSLisandro Dalcin ierr = PetscFree(dm->mattype);CHKERRQ(ierr); 19919fd82e9SBarry Smith ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr); 200521d9a4cSLisandro Dalcin PetscFunctionReturn(0); 201521d9a4cSLisandro Dalcin } 202521d9a4cSLisandro Dalcin 203521d9a4cSLisandro Dalcin #undef __FUNCT__ 204c688c046SMatthew G Knepley #define __FUNCT__ "MatGetDM" 205c688c046SMatthew G Knepley /*@ 20634f98d34SBarry Smith MatGetDM - Gets the DM defining the data layout of the matrix 207c688c046SMatthew G Knepley 208c688c046SMatthew G Knepley Not collective 209c688c046SMatthew G Knepley 210c688c046SMatthew G Knepley Input Parameter: 211c688c046SMatthew G Knepley . A - The Mat 212c688c046SMatthew G Knepley 213c688c046SMatthew G Knepley Output Parameter: 214c688c046SMatthew G Knepley . dm - The DM 215c688c046SMatthew G Knepley 216c688c046SMatthew G Knepley Level: intermediate 217c688c046SMatthew G Knepley 218c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType() 219c688c046SMatthew G Knepley @*/ 220c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm) 221c688c046SMatthew G Knepley { 222c688c046SMatthew G Knepley PetscErrorCode ierr; 223c688c046SMatthew G Knepley 224c688c046SMatthew G Knepley PetscFunctionBegin; 225c688c046SMatthew G Knepley PetscValidHeaderSpecific(A,MAT_CLASSID,1); 226c688c046SMatthew G Knepley PetscValidPointer(dm,2); 227c688c046SMatthew G Knepley ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject *) dm);CHKERRQ(ierr); 228c688c046SMatthew G Knepley PetscFunctionReturn(0); 229c688c046SMatthew G Knepley } 230c688c046SMatthew G Knepley 231c688c046SMatthew G Knepley #undef __FUNCT__ 232c688c046SMatthew G Knepley #define __FUNCT__ "MatSetDM" 233c688c046SMatthew G Knepley /*@ 234c688c046SMatthew G Knepley MatSetDM - Sets the DM defining the data layout of the matrix 235c688c046SMatthew G Knepley 236c688c046SMatthew G Knepley Not collective 237c688c046SMatthew G Knepley 238c688c046SMatthew G Knepley Input Parameters: 239c688c046SMatthew G Knepley + A - The Mat 240c688c046SMatthew G Knepley - dm - The DM 241c688c046SMatthew G Knepley 242c688c046SMatthew G Knepley Level: intermediate 243c688c046SMatthew G Knepley 244c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType() 245c688c046SMatthew G Knepley @*/ 246c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm) 247c688c046SMatthew G Knepley { 248c688c046SMatthew G Knepley PetscErrorCode ierr; 249c688c046SMatthew G Knepley 250c688c046SMatthew G Knepley PetscFunctionBegin; 251c688c046SMatthew G Knepley PetscValidHeaderSpecific(A,MAT_CLASSID,1); 252a85f731bSMatthew G Knepley if (dm) {PetscValidHeaderSpecific(dm,DM_CLASSID,2);} 253c688c046SMatthew G Knepley ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr); 254c688c046SMatthew G Knepley PetscFunctionReturn(0); 255c688c046SMatthew G Knepley } 256c688c046SMatthew G Knepley 257c688c046SMatthew G Knepley #undef __FUNCT__ 2589a42bb27SBarry Smith #define __FUNCT__ "DMSetOptionsPrefix" 2599a42bb27SBarry Smith /*@C 2609a42bb27SBarry Smith DMSetOptionsPrefix - Sets the prefix used for searching for all 261aa219208SBarry Smith DMDA options in the database. 2629a42bb27SBarry Smith 263aa219208SBarry Smith Logically Collective on DMDA 2649a42bb27SBarry Smith 2659a42bb27SBarry Smith Input Parameter: 266aa219208SBarry Smith + da - the DMDA context 2679a42bb27SBarry Smith - prefix - the prefix to prepend to all option names 2689a42bb27SBarry Smith 2699a42bb27SBarry Smith Notes: 2709a42bb27SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 2719a42bb27SBarry Smith The first character of all runtime options is AUTOMATICALLY the hyphen. 2729a42bb27SBarry Smith 2739a42bb27SBarry Smith Level: advanced 2749a42bb27SBarry Smith 275aa219208SBarry Smith .keywords: DMDA, set, options, prefix, database 2769a42bb27SBarry Smith 2779a42bb27SBarry Smith .seealso: DMSetFromOptions() 2789a42bb27SBarry Smith @*/ 2797087cfbeSBarry Smith PetscErrorCode DMSetOptionsPrefix(DM dm,const char prefix[]) 2809a42bb27SBarry Smith { 2819a42bb27SBarry Smith PetscErrorCode ierr; 2829a42bb27SBarry Smith 2839a42bb27SBarry Smith PetscFunctionBegin; 2849a42bb27SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 2859a42bb27SBarry Smith ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr); 2869a42bb27SBarry Smith PetscFunctionReturn(0); 2879a42bb27SBarry Smith } 2889a42bb27SBarry Smith 2899a42bb27SBarry Smith #undef __FUNCT__ 29047c6ae99SBarry Smith #define __FUNCT__ "DMDestroy" 29147c6ae99SBarry Smith /*@ 292aa219208SBarry Smith DMDestroy - Destroys a vector packer or DMDA. 29347c6ae99SBarry Smith 29447c6ae99SBarry Smith Collective on DM 29547c6ae99SBarry Smith 29647c6ae99SBarry Smith Input Parameter: 29747c6ae99SBarry Smith . dm - the DM object to destroy 29847c6ae99SBarry Smith 29947c6ae99SBarry Smith Level: developer 30047c6ae99SBarry Smith 301e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix() 30247c6ae99SBarry Smith 30347c6ae99SBarry Smith @*/ 304fcfd50ebSBarry Smith PetscErrorCode DMDestroy(DM *dm) 30547c6ae99SBarry Smith { 306af122d2aSMatthew G Knepley PetscInt i, cnt = 0, f; 307dfe15315SJed Brown DMNamedVecLink nlink,nnext; 30847c6ae99SBarry Smith PetscErrorCode ierr; 30947c6ae99SBarry Smith 31047c6ae99SBarry Smith PetscFunctionBegin; 3116bf464f9SBarry Smith if (!*dm) PetscFunctionReturn(0); 3126bf464f9SBarry Smith PetscValidHeaderSpecific((*dm),DM_CLASSID,1); 31387e657c6SBarry Smith 31487e657c6SBarry Smith /* count all the circular references of DM and its contained Vecs */ 315732e2eb9SMatthew G Knepley for (i=0; i<DM_MAX_WORK_VECTORS; i++) { 3166bf464f9SBarry Smith if ((*dm)->localin[i]) {cnt++;} 3176bf464f9SBarry Smith if ((*dm)->globalin[i]) {cnt++;} 318732e2eb9SMatthew G Knepley } 319dfe15315SJed Brown for (nlink=(*dm)->namedglobal; nlink; nlink=nlink->next) cnt++; 32071cd77b2SBarry Smith if ((*dm)->x) { 321c688c046SMatthew G Knepley DM obj; 322c688c046SMatthew G Knepley ierr = VecGetDM((*dm)->x, &obj);CHKERRQ(ierr); 323c688c046SMatthew G Knepley if (obj == *dm) cnt++; 32471cd77b2SBarry Smith } 325732e2eb9SMatthew G Knepley 3266bf464f9SBarry Smith if (--((PetscObject)(*dm))->refct - cnt > 0) {*dm = 0; PetscFunctionReturn(0);} 327732e2eb9SMatthew G Knepley /* 328732e2eb9SMatthew G Knepley Need this test because the dm references the vectors that 329732e2eb9SMatthew G Knepley reference the dm, so destroying the dm calls destroy on the 330732e2eb9SMatthew G Knepley vectors that cause another destroy on the dm 331732e2eb9SMatthew G Knepley */ 3326bf464f9SBarry Smith if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0); 3336bf464f9SBarry Smith ((PetscObject) (*dm))->refct = 0; 334732e2eb9SMatthew G Knepley for (i=0; i<DM_MAX_WORK_VECTORS; i++) { 3356bf464f9SBarry Smith if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()"); 3366bf464f9SBarry Smith ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr); 337732e2eb9SMatthew G Knepley } 338dfe15315SJed Brown for (nlink=(*dm)->namedglobal; nlink; nlink=nnext) { /* Destroy the named vectors */ 339dfe15315SJed Brown nnext = nlink->next; 340dfe15315SJed 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); 341dfe15315SJed Brown ierr = PetscFree(nlink->name);CHKERRQ(ierr); 342dfe15315SJed Brown ierr = VecDestroy(&nlink->X);CHKERRQ(ierr); 343dfe15315SJed Brown ierr = PetscFree(nlink);CHKERRQ(ierr); 344dfe15315SJed Brown } 345dfe15315SJed Brown (*dm)->namedglobal = PETSC_NULL; 3461a266240SBarry Smith 347b17ce1afSJed Brown /* Destroy the list of hooks */ 348c833c3b5SJed Brown { 349c833c3b5SJed Brown DMCoarsenHookLink link,next; 350b17ce1afSJed Brown for (link=(*dm)->coarsenhook; link; link=next) { 351b17ce1afSJed Brown next = link->next; 352b17ce1afSJed Brown ierr = PetscFree(link);CHKERRQ(ierr); 353b17ce1afSJed Brown } 354b17ce1afSJed Brown (*dm)->coarsenhook = PETSC_NULL; 355c833c3b5SJed Brown } 356c833c3b5SJed Brown { 357c833c3b5SJed Brown DMRefineHookLink link,next; 358c833c3b5SJed Brown for (link=(*dm)->refinehook; link; link=next) { 359c833c3b5SJed Brown next = link->next; 360c833c3b5SJed Brown ierr = PetscFree(link);CHKERRQ(ierr); 361c833c3b5SJed Brown } 362c833c3b5SJed Brown (*dm)->refinehook = PETSC_NULL; 363c833c3b5SJed Brown } 364be081cd6SPeter Brune { 365be081cd6SPeter Brune DMSubDomainHookLink link,next; 366be081cd6SPeter Brune for (link=(*dm)->subdomainhook; link; link=next) { 367be081cd6SPeter Brune next = link->next; 368be081cd6SPeter Brune ierr = PetscFree(link);CHKERRQ(ierr); 369be081cd6SPeter Brune } 370be081cd6SPeter Brune (*dm)->subdomainhook = PETSC_NULL; 371be081cd6SPeter Brune } 372baf369e7SPeter Brune { 373baf369e7SPeter Brune DMGlobalToLocalHookLink link,next; 374baf369e7SPeter Brune for (link=(*dm)->gtolhook; link; link=next) { 375baf369e7SPeter Brune next = link->next; 376baf369e7SPeter Brune ierr = PetscFree(link);CHKERRQ(ierr); 377baf369e7SPeter Brune } 378baf369e7SPeter Brune (*dm)->gtolhook = PETSC_NULL; 379baf369e7SPeter Brune } 380aa1993deSMatthew G Knepley /* Destroy the work arrays */ 381aa1993deSMatthew G Knepley { 382aa1993deSMatthew G Knepley DMWorkLink link,next; 383aa1993deSMatthew G Knepley if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out"); 384aa1993deSMatthew G Knepley for (link=(*dm)->workin; link; link=next) { 385aa1993deSMatthew G Knepley next = link->next; 386aa1993deSMatthew G Knepley ierr = PetscFree(link->mem);CHKERRQ(ierr); 387aa1993deSMatthew G Knepley ierr = PetscFree(link);CHKERRQ(ierr); 388aa1993deSMatthew G Knepley } 389aa1993deSMatthew G Knepley (*dm)->workin = PETSC_NULL; 390aa1993deSMatthew G Knepley } 391b17ce1afSJed Brown 39252536dc3SBarry Smith ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr); 39352536dc3SBarry Smith ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr); 39452536dc3SBarry Smith ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr); 39552536dc3SBarry Smith 3961a266240SBarry Smith if ((*dm)->ctx && (*dm)->ctxdestroy) { 3971a266240SBarry Smith ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr); 3981a266240SBarry Smith } 39987e657c6SBarry Smith ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr); 40071cd77b2SBarry Smith ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr); 4014dcab191SBarry Smith ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr); 4026bf464f9SBarry Smith ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr); 4036bf464f9SBarry Smith ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmapb);CHKERRQ(ierr); 4046bf464f9SBarry Smith ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr); 405073dac72SJed Brown ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr); 40688ed4aceSMatthew G Knepley 40788ed4aceSMatthew G Knepley ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr); 40888ed4aceSMatthew G Knepley ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr); 4098b1ab98fSJed Brown ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr); 41088ed4aceSMatthew G Knepley ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr); 41188ed4aceSMatthew G Knepley ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr); 412af122d2aSMatthew G Knepley 4136636e97aSMatthew G Knepley ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr); 4146636e97aSMatthew G Knepley ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr); 4156636e97aSMatthew G Knepley ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr); 4166636e97aSMatthew G Knepley 417af122d2aSMatthew G Knepley for (f = 0; f < (*dm)->numFields; ++f) { 418af122d2aSMatthew G Knepley ierr = PetscObjectDestroy(&(*dm)->fields[f]);CHKERRQ(ierr); 419af122d2aSMatthew G Knepley } 420af122d2aSMatthew G Knepley ierr = PetscFree((*dm)->fields);CHKERRQ(ierr); 421732e2eb9SMatthew G Knepley /* if memory was published with AMS then destroy it */ 4226bf464f9SBarry Smith ierr = PetscObjectDepublish(*dm);CHKERRQ(ierr); 423732e2eb9SMatthew G Knepley 4246bf464f9SBarry Smith ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr); 425435a35e8SMatthew G Knepley /* We do not destroy (*dm)->data here so that we can reference count backend objects */ 426732e2eb9SMatthew G Knepley ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr); 42747c6ae99SBarry Smith PetscFunctionReturn(0); 42847c6ae99SBarry Smith } 42947c6ae99SBarry Smith 43047c6ae99SBarry Smith #undef __FUNCT__ 431d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp" 432d7bf68aeSBarry Smith /*@ 433d7bf68aeSBarry Smith DMSetUp - sets up the data structures inside a DM object 434d7bf68aeSBarry Smith 435d7bf68aeSBarry Smith Collective on DM 436d7bf68aeSBarry Smith 437d7bf68aeSBarry Smith Input Parameter: 438d7bf68aeSBarry Smith . dm - the DM object to setup 439d7bf68aeSBarry Smith 440d7bf68aeSBarry Smith Level: developer 441d7bf68aeSBarry Smith 442e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix() 443d7bf68aeSBarry Smith 444d7bf68aeSBarry Smith @*/ 4457087cfbeSBarry Smith PetscErrorCode DMSetUp(DM dm) 446d7bf68aeSBarry Smith { 447d7bf68aeSBarry Smith PetscErrorCode ierr; 448d7bf68aeSBarry Smith 449d7bf68aeSBarry Smith PetscFunctionBegin; 450171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 4518387afaaSJed Brown if (dm->setupcalled) PetscFunctionReturn(0); 452d7bf68aeSBarry Smith if (dm->ops->setup) { 453d7bf68aeSBarry Smith ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr); 454d7bf68aeSBarry Smith } 4558387afaaSJed Brown dm->setupcalled = PETSC_TRUE; 456d7bf68aeSBarry Smith PetscFunctionReturn(0); 457d7bf68aeSBarry Smith } 458d7bf68aeSBarry Smith 459d7bf68aeSBarry Smith #undef __FUNCT__ 460d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions" 461d7bf68aeSBarry Smith /*@ 462d7bf68aeSBarry Smith DMSetFromOptions - sets parameters in a DM from the options database 463d7bf68aeSBarry Smith 464d7bf68aeSBarry Smith Collective on DM 465d7bf68aeSBarry Smith 466d7bf68aeSBarry Smith Input Parameter: 467d7bf68aeSBarry Smith . dm - the DM object to set options for 468d7bf68aeSBarry Smith 469732e2eb9SMatthew G Knepley Options Database: 470dd85299cSBarry Smith + -dm_preallocate_only: Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros 471dd85299cSBarry Smith . -dm_vec_type <type> type of vector to create inside DM 472171400e9SBarry Smith . -dm_mat_type <type> type of matrix to create inside DM 473171400e9SBarry Smith - -dm_coloring_type <global or ghosted> 474732e2eb9SMatthew G Knepley 475d7bf68aeSBarry Smith Level: developer 476d7bf68aeSBarry Smith 477e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix() 478d7bf68aeSBarry Smith 479d7bf68aeSBarry Smith @*/ 4807087cfbeSBarry Smith PetscErrorCode DMSetFromOptions(DM dm) 481d7bf68aeSBarry Smith { 482f55f929bSMatthew G Knepley char typeName[256] = MATAIJ; 483ca266f36SBarry Smith PetscBool flg; 484d7bf68aeSBarry Smith PetscErrorCode ierr; 485d7bf68aeSBarry Smith 486d7bf68aeSBarry Smith PetscFunctionBegin; 487171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 4883194b578SJed Brown ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr); 489073dac72SJed Brown ierr = PetscOptionsBool("-dm_preallocate_only","only preallocate matrix, but do not set column indices","DMSetMatrixPreallocateOnly",dm->prealloc_only,&dm->prealloc_only,PETSC_NULL);CHKERRQ(ierr); 490f9ba7244SBarry Smith ierr = PetscOptionsList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr); 491f9ba7244SBarry Smith if (flg) { 492f9ba7244SBarry Smith ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr); 493f9ba7244SBarry Smith } 4948caf3d72SBarry Smith ierr = PetscOptionsList("-dm_mat_type","Matrix type used for created matrices","DMSetMatType",MatList,dm->mattype?dm->mattype:typeName,typeName,sizeof(typeName),&flg);CHKERRQ(ierr); 495073dac72SJed Brown if (flg) { 496521d9a4cSLisandro Dalcin ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr); 497073dac72SJed Brown } 4981b89239cSHong Zhang ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","ISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,PETSC_NULL);CHKERRQ(ierr); 499f9ba7244SBarry Smith if (dm->ops->setfromoptions) { 500f9ba7244SBarry Smith ierr = (*dm->ops->setfromoptions)(dm);CHKERRQ(ierr); 501f9ba7244SBarry Smith } 502f9ba7244SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 503f9ba7244SBarry Smith ierr = PetscObjectProcessOptionsHandlers((PetscObject) dm);CHKERRQ(ierr); 50482fcb398SMatthew G Knepley ierr = PetscOptionsEnd();CHKERRQ(ierr); 505ca266f36SBarry Smith ierr = DMViewFromOptions(dm,"-dm_view");CHKERRQ(ierr); 506d7bf68aeSBarry Smith PetscFunctionReturn(0); 507d7bf68aeSBarry Smith } 508d7bf68aeSBarry Smith 509d7bf68aeSBarry Smith #undef __FUNCT__ 51047c6ae99SBarry Smith #define __FUNCT__ "DMView" 511fc9bc008SSatish Balay /*@C 512aa219208SBarry Smith DMView - Views a vector packer or DMDA. 51347c6ae99SBarry Smith 51447c6ae99SBarry Smith Collective on DM 51547c6ae99SBarry Smith 51647c6ae99SBarry Smith Input Parameter: 51747c6ae99SBarry Smith + dm - the DM object to view 51847c6ae99SBarry Smith - v - the viewer 51947c6ae99SBarry Smith 52047c6ae99SBarry Smith Level: developer 52147c6ae99SBarry Smith 522e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix() 52347c6ae99SBarry Smith 52447c6ae99SBarry Smith @*/ 5257087cfbeSBarry Smith PetscErrorCode DMView(DM dm,PetscViewer v) 52647c6ae99SBarry Smith { 52747c6ae99SBarry Smith PetscErrorCode ierr; 52832c0f0efSBarry Smith PetscBool isbinary; 52947c6ae99SBarry Smith 53047c6ae99SBarry Smith PetscFunctionBegin; 531171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 5323014e516SBarry Smith if (!v) { 5333014e516SBarry Smith ierr = PetscViewerASCIIGetStdout(((PetscObject)dm)->comm,&v);CHKERRQ(ierr); 5343014e516SBarry Smith } 53532c0f0efSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 53632c0f0efSBarry Smith if (isbinary) { 53755849f57SBarry Smith PetscInt classid = DM_FILE_CLASSID; 53832c0f0efSBarry Smith char type[256]; 53932c0f0efSBarry Smith 54032c0f0efSBarry Smith ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 54132c0f0efSBarry Smith ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr); 54232c0f0efSBarry Smith ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr); 54332c0f0efSBarry Smith } 5440c010503SBarry Smith if (dm->ops->view) { 5450c010503SBarry Smith ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr); 54647c6ae99SBarry Smith } 54747c6ae99SBarry Smith PetscFunctionReturn(0); 54847c6ae99SBarry Smith } 54947c6ae99SBarry Smith 55047c6ae99SBarry Smith #undef __FUNCT__ 55147c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector" 55247c6ae99SBarry Smith /*@ 553aa219208SBarry Smith DMCreateGlobalVector - Creates a global vector from a DMDA or DMComposite object 55447c6ae99SBarry Smith 55547c6ae99SBarry Smith Collective on DM 55647c6ae99SBarry Smith 55747c6ae99SBarry Smith Input Parameter: 55847c6ae99SBarry Smith . dm - the DM object 55947c6ae99SBarry Smith 56047c6ae99SBarry Smith Output Parameter: 56147c6ae99SBarry Smith . vec - the global vector 56247c6ae99SBarry Smith 563073dac72SJed Brown Level: beginner 56447c6ae99SBarry Smith 565e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix() 56647c6ae99SBarry Smith 56747c6ae99SBarry Smith @*/ 5687087cfbeSBarry Smith PetscErrorCode DMCreateGlobalVector(DM dm,Vec *vec) 56947c6ae99SBarry Smith { 57047c6ae99SBarry Smith PetscErrorCode ierr; 57147c6ae99SBarry Smith 57247c6ae99SBarry Smith PetscFunctionBegin; 573171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 57447c6ae99SBarry Smith ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr); 57547c6ae99SBarry Smith PetscFunctionReturn(0); 57647c6ae99SBarry Smith } 57747c6ae99SBarry Smith 57847c6ae99SBarry Smith #undef __FUNCT__ 57947c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector" 58047c6ae99SBarry Smith /*@ 581aa219208SBarry Smith DMCreateLocalVector - Creates a local vector from a DMDA or DMComposite object 58247c6ae99SBarry Smith 58347c6ae99SBarry Smith Not Collective 58447c6ae99SBarry Smith 58547c6ae99SBarry Smith Input Parameter: 58647c6ae99SBarry Smith . dm - the DM object 58747c6ae99SBarry Smith 58847c6ae99SBarry Smith Output Parameter: 58947c6ae99SBarry Smith . vec - the local vector 59047c6ae99SBarry Smith 591073dac72SJed Brown Level: beginner 59247c6ae99SBarry Smith 593e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix() 59447c6ae99SBarry Smith 59547c6ae99SBarry Smith @*/ 5967087cfbeSBarry Smith PetscErrorCode DMCreateLocalVector(DM dm,Vec *vec) 59747c6ae99SBarry Smith { 59847c6ae99SBarry Smith PetscErrorCode ierr; 59947c6ae99SBarry Smith 60047c6ae99SBarry Smith PetscFunctionBegin; 601171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 60247c6ae99SBarry Smith ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr); 60347c6ae99SBarry Smith PetscFunctionReturn(0); 60447c6ae99SBarry Smith } 60547c6ae99SBarry Smith 60647c6ae99SBarry Smith #undef __FUNCT__ 6071411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping" 6081411c6eeSJed Brown /*@ 6091411c6eeSJed Brown DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM. 6101411c6eeSJed Brown 6111411c6eeSJed Brown Collective on DM 6121411c6eeSJed Brown 6131411c6eeSJed Brown Input Parameter: 6141411c6eeSJed Brown . dm - the DM that provides the mapping 6151411c6eeSJed Brown 6161411c6eeSJed Brown Output Parameter: 6171411c6eeSJed Brown . ltog - the mapping 6181411c6eeSJed Brown 6191411c6eeSJed Brown Level: intermediate 6201411c6eeSJed Brown 6211411c6eeSJed Brown Notes: 6221411c6eeSJed Brown This mapping can then be used by VecSetLocalToGlobalMapping() or 6231411c6eeSJed Brown MatSetLocalToGlobalMapping(). 6241411c6eeSJed Brown 6251411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMappingBlock() 6261411c6eeSJed Brown @*/ 6277087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog) 6281411c6eeSJed Brown { 6291411c6eeSJed Brown PetscErrorCode ierr; 6301411c6eeSJed Brown 6311411c6eeSJed Brown PetscFunctionBegin; 6321411c6eeSJed Brown PetscValidHeaderSpecific(dm,DM_CLASSID,1); 6331411c6eeSJed Brown PetscValidPointer(ltog,2); 6341411c6eeSJed Brown if (!dm->ltogmap) { 63537d0c07bSMatthew G Knepley PetscSection section, sectionGlobal; 63637d0c07bSMatthew G Knepley 63737d0c07bSMatthew G Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 63837d0c07bSMatthew G Knepley if (section) { 63937d0c07bSMatthew G Knepley PetscInt *ltog; 64037d0c07bSMatthew G Knepley PetscInt pStart, pEnd, size, p, l; 64137d0c07bSMatthew G Knepley 64237d0c07bSMatthew G Knepley ierr = DMGetDefaultGlobalSection(dm, §ionGlobal);CHKERRQ(ierr); 64337d0c07bSMatthew G Knepley ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr); 64437d0c07bSMatthew G Knepley ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr); 64537d0c07bSMatthew G Knepley ierr = PetscMalloc(size * sizeof(PetscInt), <og);CHKERRQ(ierr); /* We want the local+overlap size */ 64637d0c07bSMatthew G Knepley for (p = pStart, l = 0; p < pEnd; ++p) { 64737d0c07bSMatthew G Knepley PetscInt dof, off, c; 64837d0c07bSMatthew G Knepley 64937d0c07bSMatthew G Knepley /* Should probably use constrained dofs */ 65037d0c07bSMatthew G Knepley ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr); 65137d0c07bSMatthew G Knepley ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr); 65237d0c07bSMatthew G Knepley for (c = 0; c < dof; ++c, ++l) { 65337d0c07bSMatthew G Knepley ltog[l] = off+c; 65437d0c07bSMatthew G Knepley } 65537d0c07bSMatthew G Knepley } 65637d0c07bSMatthew G Knepley ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_SELF, size, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr); 65737d0c07bSMatthew G Knepley ierr = PetscLogObjectParent(dm, dm->ltogmap);CHKERRQ(ierr); 65837d0c07bSMatthew G Knepley } else { 6591411c6eeSJed Brown if (!dm->ops->createlocaltoglobalmapping) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping"); 6601411c6eeSJed Brown ierr = (*dm->ops->createlocaltoglobalmapping)(dm);CHKERRQ(ierr); 6611411c6eeSJed Brown } 66237d0c07bSMatthew G Knepley } 6631411c6eeSJed Brown *ltog = dm->ltogmap; 6641411c6eeSJed Brown PetscFunctionReturn(0); 6651411c6eeSJed Brown } 6661411c6eeSJed Brown 6671411c6eeSJed Brown #undef __FUNCT__ 6681411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMappingBlock" 6691411c6eeSJed Brown /*@ 6701411c6eeSJed Brown DMGetLocalToGlobalMappingBlock - Accesses the blocked local-to-global mapping in a DM. 6711411c6eeSJed Brown 6721411c6eeSJed Brown Collective on DM 6731411c6eeSJed Brown 6741411c6eeSJed Brown Input Parameter: 6751411c6eeSJed Brown . da - the distributed array that provides the mapping 6761411c6eeSJed Brown 6771411c6eeSJed Brown Output Parameter: 6781411c6eeSJed Brown . ltog - the block mapping 6791411c6eeSJed Brown 6801411c6eeSJed Brown Level: intermediate 6811411c6eeSJed Brown 6821411c6eeSJed Brown Notes: 6831411c6eeSJed Brown This mapping can then be used by VecSetLocalToGlobalMappingBlock() or 6841411c6eeSJed Brown MatSetLocalToGlobalMappingBlock(). 6851411c6eeSJed Brown 6861411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMapping(), DMGetBlockSize(), VecSetBlockSize(), MatSetBlockSize() 6871411c6eeSJed Brown @*/ 6887087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMappingBlock(DM dm,ISLocalToGlobalMapping *ltog) 6891411c6eeSJed Brown { 6901411c6eeSJed Brown PetscErrorCode ierr; 6911411c6eeSJed Brown 6921411c6eeSJed Brown PetscFunctionBegin; 6931411c6eeSJed Brown PetscValidHeaderSpecific(dm,DM_CLASSID,1); 6941411c6eeSJed Brown PetscValidPointer(ltog,2); 6951411c6eeSJed Brown if (!dm->ltogmapb) { 6961411c6eeSJed Brown PetscInt bs; 6971411c6eeSJed Brown ierr = DMGetBlockSize(dm,&bs);CHKERRQ(ierr); 6981411c6eeSJed Brown if (bs > 1) { 6991411c6eeSJed Brown if (!dm->ops->createlocaltoglobalmappingblock) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMappingBlock"); 7001411c6eeSJed Brown ierr = (*dm->ops->createlocaltoglobalmappingblock)(dm);CHKERRQ(ierr); 7011411c6eeSJed Brown } else { 7021411c6eeSJed Brown ierr = DMGetLocalToGlobalMapping(dm,&dm->ltogmapb);CHKERRQ(ierr); 7031411c6eeSJed Brown ierr = PetscObjectReference((PetscObject)dm->ltogmapb);CHKERRQ(ierr); 7041411c6eeSJed Brown } 7051411c6eeSJed Brown } 7061411c6eeSJed Brown *ltog = dm->ltogmapb; 7071411c6eeSJed Brown PetscFunctionReturn(0); 7081411c6eeSJed Brown } 7091411c6eeSJed Brown 7101411c6eeSJed Brown #undef __FUNCT__ 7111411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize" 7121411c6eeSJed Brown /*@ 7131411c6eeSJed Brown DMGetBlockSize - Gets the inherent block size associated with a DM 7141411c6eeSJed Brown 7151411c6eeSJed Brown Not Collective 7161411c6eeSJed Brown 7171411c6eeSJed Brown Input Parameter: 7181411c6eeSJed Brown . dm - the DM with block structure 7191411c6eeSJed Brown 7201411c6eeSJed Brown Output Parameter: 7211411c6eeSJed Brown . bs - the block size, 1 implies no exploitable block structure 7221411c6eeSJed Brown 7231411c6eeSJed Brown Level: intermediate 7241411c6eeSJed Brown 7251411c6eeSJed Brown .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMappingBlock() 7261411c6eeSJed Brown @*/ 7277087cfbeSBarry Smith PetscErrorCode DMGetBlockSize(DM dm,PetscInt *bs) 7281411c6eeSJed Brown { 7291411c6eeSJed Brown PetscFunctionBegin; 7301411c6eeSJed Brown PetscValidHeaderSpecific(dm,DM_CLASSID,1); 7311411c6eeSJed Brown PetscValidPointer(bs,2); 7321411c6eeSJed 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"); 7331411c6eeSJed Brown *bs = dm->bs; 7341411c6eeSJed Brown PetscFunctionReturn(0); 7351411c6eeSJed Brown } 7361411c6eeSJed Brown 7371411c6eeSJed Brown #undef __FUNCT__ 738e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation" 73947c6ae99SBarry Smith /*@ 740e727c939SJed Brown DMCreateInterpolation - Gets interpolation matrix between two DMDA or DMComposite objects 74147c6ae99SBarry Smith 74247c6ae99SBarry Smith Collective on DM 74347c6ae99SBarry Smith 74447c6ae99SBarry Smith Input Parameter: 74547c6ae99SBarry Smith + dm1 - the DM object 74647c6ae99SBarry Smith - dm2 - the second, finer DM object 74747c6ae99SBarry Smith 74847c6ae99SBarry Smith Output Parameter: 74947c6ae99SBarry Smith + mat - the interpolation 75047c6ae99SBarry Smith - vec - the scaling (optional) 75147c6ae99SBarry Smith 75247c6ae99SBarry Smith Level: developer 75347c6ae99SBarry Smith 75485afcc9aSBarry 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 75585afcc9aSBarry Smith DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation. 756d52bd9f3SBarry Smith 7571f588964SMatthew G Knepley For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors 758d52bd9f3SBarry Smith EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic. 75985afcc9aSBarry Smith 76085afcc9aSBarry Smith 761e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen() 76247c6ae99SBarry Smith 76347c6ae99SBarry Smith @*/ 764e727c939SJed Brown PetscErrorCode DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec) 76547c6ae99SBarry Smith { 76647c6ae99SBarry Smith PetscErrorCode ierr; 76747c6ae99SBarry Smith 76847c6ae99SBarry Smith PetscFunctionBegin; 769171400e9SBarry Smith PetscValidHeaderSpecific(dm1,DM_CLASSID,1); 770171400e9SBarry Smith PetscValidHeaderSpecific(dm2,DM_CLASSID,2); 77125296bd5SBarry Smith ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr); 77247c6ae99SBarry Smith PetscFunctionReturn(0); 77347c6ae99SBarry Smith } 77447c6ae99SBarry Smith 77547c6ae99SBarry Smith #undef __FUNCT__ 776e727c939SJed Brown #define __FUNCT__ "DMCreateInjection" 77747c6ae99SBarry Smith /*@ 778e727c939SJed Brown DMCreateInjection - Gets injection matrix between two DMDA or DMComposite objects 77947c6ae99SBarry Smith 78047c6ae99SBarry Smith Collective on DM 78147c6ae99SBarry Smith 78247c6ae99SBarry Smith Input Parameter: 78347c6ae99SBarry Smith + dm1 - the DM object 78447c6ae99SBarry Smith - dm2 - the second, finer DM object 78547c6ae99SBarry Smith 78647c6ae99SBarry Smith Output Parameter: 78747c6ae99SBarry Smith . ctx - the injection 78847c6ae99SBarry Smith 78947c6ae99SBarry Smith Level: developer 79047c6ae99SBarry Smith 79185afcc9aSBarry 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 79285afcc9aSBarry Smith DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection. 79385afcc9aSBarry Smith 794e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation() 79547c6ae99SBarry Smith 79647c6ae99SBarry Smith @*/ 797e727c939SJed Brown PetscErrorCode DMCreateInjection(DM dm1,DM dm2,VecScatter *ctx) 79847c6ae99SBarry Smith { 79947c6ae99SBarry Smith PetscErrorCode ierr; 80047c6ae99SBarry Smith 80147c6ae99SBarry Smith PetscFunctionBegin; 802171400e9SBarry Smith PetscValidHeaderSpecific(dm1,DM_CLASSID,1); 803171400e9SBarry Smith PetscValidHeaderSpecific(dm2,DM_CLASSID,2); 80447c6ae99SBarry Smith ierr = (*dm1->ops->getinjection)(dm1,dm2,ctx);CHKERRQ(ierr); 80547c6ae99SBarry Smith PetscFunctionReturn(0); 80647c6ae99SBarry Smith } 80747c6ae99SBarry Smith 80847c6ae99SBarry Smith #undef __FUNCT__ 809e727c939SJed Brown #define __FUNCT__ "DMCreateColoring" 810d1e2c406SBarry Smith /*@C 811e727c939SJed Brown DMCreateColoring - Gets coloring for a DMDA or DMComposite 81247c6ae99SBarry Smith 81347c6ae99SBarry Smith Collective on DM 81447c6ae99SBarry Smith 81547c6ae99SBarry Smith Input Parameter: 81647c6ae99SBarry Smith + dm - the DM object 81747c6ae99SBarry Smith . ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL 81847c6ae99SBarry Smith - matype - either MATAIJ or MATBAIJ 81947c6ae99SBarry Smith 82047c6ae99SBarry Smith Output Parameter: 82147c6ae99SBarry Smith . coloring - the coloring 82247c6ae99SBarry Smith 82347c6ae99SBarry Smith Level: developer 82447c6ae99SBarry Smith 825e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix() 82647c6ae99SBarry Smith 827aab9d709SJed Brown @*/ 82819fd82e9SBarry Smith PetscErrorCode DMCreateColoring(DM dm,ISColoringType ctype,MatType mtype,ISColoring *coloring) 82947c6ae99SBarry Smith { 83047c6ae99SBarry Smith PetscErrorCode ierr; 83147c6ae99SBarry Smith 83247c6ae99SBarry Smith PetscFunctionBegin; 833171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 83447c6ae99SBarry Smith if (!dm->ops->getcoloring) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No coloring for this type of DM yet"); 83547c6ae99SBarry Smith ierr = (*dm->ops->getcoloring)(dm,ctype,mtype,coloring);CHKERRQ(ierr); 83647c6ae99SBarry Smith PetscFunctionReturn(0); 83747c6ae99SBarry Smith } 83847c6ae99SBarry Smith 83947c6ae99SBarry Smith #undef __FUNCT__ 840950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix" 84147c6ae99SBarry Smith /*@C 842950540a4SJed Brown DMCreateMatrix - Gets empty Jacobian for a DMDA or DMComposite 84347c6ae99SBarry Smith 84447c6ae99SBarry Smith Collective on DM 84547c6ae99SBarry Smith 84647c6ae99SBarry Smith Input Parameter: 84747c6ae99SBarry Smith + dm - the DM object 84847c6ae99SBarry Smith - mtype - Supported types are MATSEQAIJ, MATMPIAIJ, MATSEQBAIJ, MATMPIBAIJ, or 84994013140SBarry Smith any type which inherits from one of these (such as MATAIJ) 85047c6ae99SBarry Smith 85147c6ae99SBarry Smith Output Parameter: 85247c6ae99SBarry Smith . mat - the empty Jacobian 85347c6ae99SBarry Smith 854073dac72SJed Brown Level: beginner 85547c6ae99SBarry Smith 85694013140SBarry Smith Notes: This properly preallocates the number of nonzeros in the sparse matrix so you 85794013140SBarry Smith do not need to do it yourself. 85894013140SBarry Smith 85994013140SBarry Smith By default it also sets the nonzero structure and puts in the zero entries. To prevent setting 860aa219208SBarry Smith the nonzero pattern call DMDASetMatPreallocateOnly() 86194013140SBarry Smith 86294013140SBarry 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 86394013140SBarry Smith internally by PETSc. 86494013140SBarry Smith 86594013140SBarry Smith For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires 866aa219208SBarry Smith the indices for the global numbering for DMDAs which is complicated. 86794013140SBarry Smith 868e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation() 86947c6ae99SBarry Smith 870aab9d709SJed Brown @*/ 87119fd82e9SBarry Smith PetscErrorCode DMCreateMatrix(DM dm,MatType mtype,Mat *mat) 87247c6ae99SBarry Smith { 87347c6ae99SBarry Smith PetscErrorCode ierr; 87447c6ae99SBarry Smith 87547c6ae99SBarry Smith PetscFunctionBegin; 876171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 877235683edSBarry Smith #ifndef PETSC_USE_DYNAMIC_LIBRARIES 878235683edSBarry Smith ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr); 879235683edSBarry Smith #endif 880c7b7c8a4SJed Brown PetscValidHeaderSpecific(dm,DM_CLASSID,1); 881c7b7c8a4SJed Brown PetscValidPointer(mat,3); 882073dac72SJed Brown if (dm->mattype) { 88325296bd5SBarry Smith ierr = (*dm->ops->creatematrix)(dm,dm->mattype,mat);CHKERRQ(ierr); 884073dac72SJed Brown } else { 88525296bd5SBarry Smith ierr = (*dm->ops->creatematrix)(dm,mtype,mat);CHKERRQ(ierr); 886c7b7c8a4SJed Brown } 88747c6ae99SBarry Smith PetscFunctionReturn(0); 88847c6ae99SBarry Smith } 88947c6ae99SBarry Smith 89047c6ae99SBarry Smith #undef __FUNCT__ 891732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly" 892732e2eb9SMatthew G Knepley /*@ 893950540a4SJed Brown DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly 894732e2eb9SMatthew G Knepley preallocated but the nonzero structure and zero values will not be set. 895732e2eb9SMatthew G Knepley 896732e2eb9SMatthew G Knepley Logically Collective on DMDA 897732e2eb9SMatthew G Knepley 898732e2eb9SMatthew G Knepley Input Parameter: 899732e2eb9SMatthew G Knepley + dm - the DM 900732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation 901732e2eb9SMatthew G Knepley 902732e2eb9SMatthew G Knepley Level: developer 903950540a4SJed Brown .seealso DMCreateMatrix() 904732e2eb9SMatthew G Knepley @*/ 905732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only) 906732e2eb9SMatthew G Knepley { 907732e2eb9SMatthew G Knepley PetscFunctionBegin; 908732e2eb9SMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 909732e2eb9SMatthew G Knepley dm->prealloc_only = only; 910732e2eb9SMatthew G Knepley PetscFunctionReturn(0); 911732e2eb9SMatthew G Knepley } 912732e2eb9SMatthew G Knepley 913732e2eb9SMatthew G Knepley #undef __FUNCT__ 914a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray" 915a89ea682SMatthew G Knepley /*@C 916aa1993deSMatthew G Knepley DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray() 917a89ea682SMatthew G Knepley 918a89ea682SMatthew G Knepley Not Collective 919a89ea682SMatthew G Knepley 920a89ea682SMatthew G Knepley Input Parameters: 921a89ea682SMatthew G Knepley + dm - the DM object 922aa1993deSMatthew G Knepley . count - The minium size 923aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT) 924a89ea682SMatthew G Knepley 925a89ea682SMatthew G Knepley Output Parameter: 926a89ea682SMatthew G Knepley . array - the work array 927a89ea682SMatthew G Knepley 928a89ea682SMatthew G Knepley Level: developer 929a89ea682SMatthew G Knepley 930a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate() 931a89ea682SMatthew G Knepley @*/ 932aa1993deSMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem) 933a89ea682SMatthew G Knepley { 934a89ea682SMatthew G Knepley PetscErrorCode ierr; 935aa1993deSMatthew G Knepley DMWorkLink link; 936aa1993deSMatthew G Knepley size_t size; 937a89ea682SMatthew G Knepley 938a89ea682SMatthew G Knepley PetscFunctionBegin; 939a89ea682SMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 940aa1993deSMatthew G Knepley PetscValidPointer(mem,4); 941aa1993deSMatthew G Knepley if (dm->workin) { 942aa1993deSMatthew G Knepley link = dm->workin; 943aa1993deSMatthew G Knepley dm->workin = dm->workin->next; 944aa1993deSMatthew G Knepley } else { 945aa1993deSMatthew G Knepley ierr = PetscNewLog(dm,struct _DMWorkLink,&link);CHKERRQ(ierr); 946a89ea682SMatthew G Knepley } 947aa1993deSMatthew G Knepley ierr = PetscDataTypeGetSize(dtype,&size);CHKERRQ(ierr); 948aa1993deSMatthew G Knepley if (size*count > link->bytes) { 949aa1993deSMatthew G Knepley ierr = PetscFree(link->mem);CHKERRQ(ierr); 950aa1993deSMatthew G Knepley ierr = PetscMalloc(size*count,&link->mem);CHKERRQ(ierr); 951aa1993deSMatthew G Knepley link->bytes = size*count; 952aa1993deSMatthew G Knepley } 953aa1993deSMatthew G Knepley link->next = dm->workout; 954aa1993deSMatthew G Knepley dm->workout = link; 955aa1993deSMatthew G Knepley *(void**)mem = link->mem; 956a89ea682SMatthew G Knepley PetscFunctionReturn(0); 957a89ea682SMatthew G Knepley } 958a89ea682SMatthew G Knepley 959aa1993deSMatthew G Knepley #undef __FUNCT__ 960aa1993deSMatthew G Knepley #define __FUNCT__ "DMRestoreWorkArray" 961aa1993deSMatthew G Knepley /*@C 962aa1993deSMatthew G Knepley DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray() 963aa1993deSMatthew G Knepley 964aa1993deSMatthew G Knepley Not Collective 965aa1993deSMatthew G Knepley 966aa1993deSMatthew G Knepley Input Parameters: 967aa1993deSMatthew G Knepley + dm - the DM object 968aa1993deSMatthew G Knepley . count - The minium size 969aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT) 970aa1993deSMatthew G Knepley 971aa1993deSMatthew G Knepley Output Parameter: 972aa1993deSMatthew G Knepley . array - the work array 973aa1993deSMatthew G Knepley 974aa1993deSMatthew G Knepley Level: developer 975aa1993deSMatthew G Knepley 976aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate() 977aa1993deSMatthew G Knepley @*/ 978aa1993deSMatthew G Knepley PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem) 979aa1993deSMatthew G Knepley { 980aa1993deSMatthew G Knepley DMWorkLink *p,link; 981aa1993deSMatthew G Knepley 982aa1993deSMatthew G Knepley PetscFunctionBegin; 983aa1993deSMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 984aa1993deSMatthew G Knepley PetscValidPointer(mem,4); 985aa1993deSMatthew G Knepley for (p=&dm->workout; (link=*p); p=&link->next) { 986aa1993deSMatthew G Knepley if (link->mem == *(void**)mem) { 987aa1993deSMatthew G Knepley *p = link->next; 988aa1993deSMatthew G Knepley link->next = dm->workin; 989aa1993deSMatthew G Knepley dm->workin = link; 990aa1993deSMatthew G Knepley *(void**)mem = PETSC_NULL; 991aa1993deSMatthew G Knepley PetscFunctionReturn(0); 992aa1993deSMatthew G Knepley } 993aa1993deSMatthew G Knepley } 994aa1993deSMatthew G Knepley SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out"); 995aa1993deSMatthew G Knepley PetscFunctionReturn(0); 996aa1993deSMatthew G Knepley } 997e7c4fc90SDmitry Karpeev 998e7c4fc90SDmitry Karpeev #undef __FUNCT__ 999435a35e8SMatthew G Knepley #define __FUNCT__ "DMSetNullSpaceConstructor" 1000435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace)) 1001435a35e8SMatthew G Knepley { 1002435a35e8SMatthew G Knepley PetscFunctionBegin; 1003435a35e8SMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 1004435a35e8SMatthew G Knepley if (field >= 10) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field); 1005435a35e8SMatthew G Knepley dm->nullspaceConstructors[field] = nullsp; 1006435a35e8SMatthew G Knepley PetscFunctionReturn(0); 1007435a35e8SMatthew G Knepley } 1008435a35e8SMatthew G Knepley 1009435a35e8SMatthew G Knepley #undef __FUNCT__ 10104d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS" 10114f3b5142SJed Brown /*@C 10124d343eeaSMatthew G Knepley DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field 10134d343eeaSMatthew G Knepley 10144d343eeaSMatthew G Knepley Not collective 10154d343eeaSMatthew G Knepley 10164d343eeaSMatthew G Knepley Input Parameter: 10174d343eeaSMatthew G Knepley . dm - the DM object 10184d343eeaSMatthew G Knepley 10194d343eeaSMatthew G Knepley Output Parameters: 102021c9b008SJed Brown + numFields - The number of fields (or PETSC_NULL if not requested) 102137d0c07bSMatthew G Knepley . fieldNames - The name for each field (or PETSC_NULL if not requested) 102221c9b008SJed Brown - fields - The global indices for each field (or PETSC_NULL if not requested) 10234d343eeaSMatthew G Knepley 10244d343eeaSMatthew G Knepley Level: intermediate 10254d343eeaSMatthew G Knepley 102621c9b008SJed Brown Notes: 102721c9b008SJed Brown The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with 102821c9b008SJed Brown PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with 102921c9b008SJed Brown PetscFree(). 103021c9b008SJed Brown 10314d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix() 10324d343eeaSMatthew G Knepley @*/ 103337d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields) 10344d343eeaSMatthew G Knepley { 103537d0c07bSMatthew G Knepley PetscSection section, sectionGlobal; 10364d343eeaSMatthew G Knepley PetscErrorCode ierr; 10374d343eeaSMatthew G Knepley 10384d343eeaSMatthew G Knepley PetscFunctionBegin; 10394d343eeaSMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 104069ca1f37SDmitry Karpeev if (numFields) { 104169ca1f37SDmitry Karpeev PetscValidPointer(numFields,2); 104269ca1f37SDmitry Karpeev *numFields = 0; 104369ca1f37SDmitry Karpeev } 104437d0c07bSMatthew G Knepley if (fieldNames) { 104537d0c07bSMatthew G Knepley PetscValidPointer(fieldNames,3); 104637d0c07bSMatthew G Knepley *fieldNames = PETSC_NULL; 104769ca1f37SDmitry Karpeev } 104869ca1f37SDmitry Karpeev if (fields) { 104969ca1f37SDmitry Karpeev PetscValidPointer(fields,4); 105069ca1f37SDmitry Karpeev *fields = PETSC_NULL; 105169ca1f37SDmitry Karpeev } 105237d0c07bSMatthew G Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 105337d0c07bSMatthew G Knepley if (section) { 105437d0c07bSMatthew G Knepley PetscInt *fieldSizes, **fieldIndices; 105537d0c07bSMatthew G Knepley PetscInt nF, f, pStart, pEnd, p; 105637d0c07bSMatthew G Knepley 105737d0c07bSMatthew G Knepley ierr = DMGetDefaultGlobalSection(dm, §ionGlobal);CHKERRQ(ierr); 105837d0c07bSMatthew G Knepley ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr); 105937d0c07bSMatthew G Knepley ierr = PetscMalloc2(nF,PetscInt,&fieldSizes,nF,PetscInt *,&fieldIndices);CHKERRQ(ierr); 106037d0c07bSMatthew G Knepley ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr); 106137d0c07bSMatthew G Knepley for (f = 0; f < nF; ++f) { 106237d0c07bSMatthew G Knepley fieldSizes[f] = 0; 106337d0c07bSMatthew G Knepley } 106437d0c07bSMatthew G Knepley for (p = pStart; p < pEnd; ++p) { 106537d0c07bSMatthew G Knepley PetscInt gdof; 106637d0c07bSMatthew G Knepley 106737d0c07bSMatthew G Knepley ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr); 106837d0c07bSMatthew G Knepley if (gdof > 0) { 106937d0c07bSMatthew G Knepley for (f = 0; f < nF; ++f) { 107037d0c07bSMatthew G Knepley PetscInt fdof, fcdof; 107137d0c07bSMatthew G Knepley 107237d0c07bSMatthew G Knepley ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr); 107337d0c07bSMatthew G Knepley ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr); 107437d0c07bSMatthew G Knepley fieldSizes[f] += fdof-fcdof; 107537d0c07bSMatthew G Knepley } 107637d0c07bSMatthew G Knepley } 107737d0c07bSMatthew G Knepley } 107837d0c07bSMatthew G Knepley for (f = 0; f < nF; ++f) { 107937d0c07bSMatthew G Knepley ierr = PetscMalloc(fieldSizes[f] * sizeof(PetscInt), &fieldIndices[f]);CHKERRQ(ierr); 108037d0c07bSMatthew G Knepley fieldSizes[f] = 0; 108137d0c07bSMatthew G Knepley } 108237d0c07bSMatthew G Knepley for (p = pStart; p < pEnd; ++p) { 108337d0c07bSMatthew G Knepley PetscInt gdof, goff; 108437d0c07bSMatthew G Knepley 108537d0c07bSMatthew G Knepley ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr); 108637d0c07bSMatthew G Knepley if (gdof > 0) { 108737d0c07bSMatthew G Knepley ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr); 108837d0c07bSMatthew G Knepley for (f = 0; f < nF; ++f) { 108937d0c07bSMatthew G Knepley PetscInt fdof, fcdof, fc; 109037d0c07bSMatthew G Knepley 109137d0c07bSMatthew G Knepley ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr); 109237d0c07bSMatthew G Knepley ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr); 109337d0c07bSMatthew G Knepley for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) { 109437d0c07bSMatthew G Knepley fieldIndices[f][fieldSizes[f]] = goff++; 109537d0c07bSMatthew G Knepley } 109637d0c07bSMatthew G Knepley } 109737d0c07bSMatthew G Knepley } 109837d0c07bSMatthew G Knepley } 109937d0c07bSMatthew G Knepley if (numFields) {*numFields = nF;} 110037d0c07bSMatthew G Knepley if (fieldNames) { 110137d0c07bSMatthew G Knepley ierr = PetscMalloc(nF * sizeof(char *), fieldNames);CHKERRQ(ierr); 110237d0c07bSMatthew G Knepley for (f = 0; f < nF; ++f) { 110337d0c07bSMatthew G Knepley const char *fieldName; 110437d0c07bSMatthew G Knepley 110537d0c07bSMatthew G Knepley ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr); 110637d0c07bSMatthew G Knepley ierr = PetscStrallocpy(fieldName, (char **) &(*fieldNames)[f]);CHKERRQ(ierr); 110737d0c07bSMatthew G Knepley } 110837d0c07bSMatthew G Knepley } 110937d0c07bSMatthew G Knepley if (fields) { 111037d0c07bSMatthew G Knepley ierr = PetscMalloc(nF * sizeof(IS), fields);CHKERRQ(ierr); 111137d0c07bSMatthew G Knepley for (f = 0; f < nF; ++f) { 111237d0c07bSMatthew G Knepley ierr = ISCreateGeneral(((PetscObject) dm)->comm, fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr); 111337d0c07bSMatthew G Knepley } 111437d0c07bSMatthew G Knepley } 111537d0c07bSMatthew G Knepley ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr); 111637d0c07bSMatthew G Knepley } else { 111737d0c07bSMatthew G Knepley if (dm->ops->createfieldis) {ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);} 111869ca1f37SDmitry Karpeev } 11194d343eeaSMatthew G Knepley PetscFunctionReturn(0); 11204d343eeaSMatthew G Knepley } 11214d343eeaSMatthew G Knepley 1122a89ea682SMatthew G Knepley #undef __FUNCT__ 112316621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecompositionDM" 1124e7c4fc90SDmitry Karpeev /*@C 112516621825SDmitry Karpeev DMCreateFieldDecompositionDM - creates a DM that encapsulates a decomposition of the original DM into fields. 112616621825SDmitry Karpeev 112716621825SDmitry Karpeev Not Collective 112816621825SDmitry Karpeev 112916621825SDmitry Karpeev Input Parameters: 113016621825SDmitry Karpeev + dm - the DM object 113116621825SDmitry Karpeev - name - the name of the field decomposition 113216621825SDmitry Karpeev 113316621825SDmitry Karpeev Output Parameter: 113416621825SDmitry Karpeev . ddm - the field decomposition DM (PETSC_NULL, if no such decomposition is known) 113516621825SDmitry Karpeev 113616621825SDmitry Karpeev Level: advanced 113716621825SDmitry Karpeev 113816621825SDmitry Karpeev .seealso DMDestroy(), DMCreate(), DMCreateFieldDecomposition(), DMCreateDomainDecompositionDM() 113916621825SDmitry Karpeev @*/ 114016621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecompositionDM(DM dm, const char* name, DM *ddm) 114116621825SDmitry Karpeev { 114216621825SDmitry Karpeev PetscErrorCode ierr; 114316621825SDmitry Karpeev 114416621825SDmitry Karpeev PetscFunctionBegin; 114516621825SDmitry Karpeev PetscValidHeaderSpecific(dm,DM_CLASSID,1); 114616621825SDmitry Karpeev PetscValidCharPointer(name,2); 114716621825SDmitry Karpeev PetscValidPointer(ddm,3); 114816621825SDmitry Karpeev *ddm = PETSC_NULL; 1149731c8d9eSDmitry Karpeev if (dm->ops->createfielddecompositiondm) { 115016621825SDmitry Karpeev ierr = (*dm->ops->createfielddecompositiondm)(dm,name,ddm);CHKERRQ(ierr); 115116621825SDmitry Karpeev } 115216621825SDmitry Karpeev PetscFunctionReturn(0); 115316621825SDmitry Karpeev } 115416621825SDmitry Karpeev 115516621825SDmitry Karpeev #undef __FUNCT__ 115616621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecomposition" 115716621825SDmitry Karpeev /*@C 115816621825SDmitry Karpeev DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems 115916621825SDmitry Karpeev corresponding to different fields: each IS contains the global indices of the dofs of the 116016621825SDmitry Karpeev corresponding field. The optional list of DMs define the DM for each subproblem. 1161e7c4fc90SDmitry Karpeev Generalizes DMCreateFieldIS(). 1162e7c4fc90SDmitry Karpeev 1163e7c4fc90SDmitry Karpeev Not collective 1164e7c4fc90SDmitry Karpeev 1165e7c4fc90SDmitry Karpeev Input Parameter: 1166e7c4fc90SDmitry Karpeev . dm - the DM object 1167e7c4fc90SDmitry Karpeev 1168e7c4fc90SDmitry Karpeev Output Parameters: 116916621825SDmitry Karpeev + len - The number of subproblems in the field decomposition (or PETSC_NULL if not requested) 117016621825SDmitry Karpeev . namelist - The name for each field (or PETSC_NULL if not requested) 117116621825SDmitry Karpeev . islist - The global indices for each field (or PETSC_NULL if not requested) 117216621825SDmitry Karpeev - dmlist - The DMs for each field subproblem (or PETSC_NULL, if not requested; if PETSC_NULL is returned, no DMs are defined) 1173e7c4fc90SDmitry Karpeev 1174e7c4fc90SDmitry Karpeev Level: intermediate 1175e7c4fc90SDmitry Karpeev 1176e7c4fc90SDmitry Karpeev Notes: 1177e7c4fc90SDmitry Karpeev The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with 1178e7c4fc90SDmitry Karpeev PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(), 1179e7c4fc90SDmitry Karpeev and all of the arrays should be freed with PetscFree(). 1180e7c4fc90SDmitry Karpeev 1181e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS() 1182e7c4fc90SDmitry Karpeev @*/ 118316621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist) 1184e7c4fc90SDmitry Karpeev { 1185e7c4fc90SDmitry Karpeev PetscErrorCode ierr; 1186e7c4fc90SDmitry Karpeev 1187e7c4fc90SDmitry Karpeev PetscFunctionBegin; 1188e7c4fc90SDmitry Karpeev PetscValidHeaderSpecific(dm,DM_CLASSID,1); 1189731c8d9eSDmitry Karpeev if (len) {PetscValidPointer(len,2); *len = 0;} 1190731c8d9eSDmitry Karpeev if (namelist) {PetscValidPointer(namelist,3); *namelist = 0;} 1191731c8d9eSDmitry Karpeev if (islist) {PetscValidPointer(islist,4); *islist = 0;} 1192731c8d9eSDmitry Karpeev if (dmlist) {PetscValidPointer(dmlist,5); *dmlist = 0;} 119316621825SDmitry Karpeev if (!dm->ops->createfielddecomposition) { 1194435a35e8SMatthew G Knepley PetscSection section; 1195435a35e8SMatthew G Knepley PetscInt numFields, f; 1196435a35e8SMatthew G Knepley 1197435a35e8SMatthew G Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 1198435a35e8SMatthew G Knepley if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);} 1199435a35e8SMatthew G Knepley if (section && numFields && dm->ops->createsubdm) { 1200435a35e8SMatthew G Knepley *len = numFields; 1201435a35e8SMatthew G Knepley ierr = PetscMalloc3(numFields,char*,namelist,numFields,IS,islist,numFields,DM,dmlist);CHKERRQ(ierr); 1202435a35e8SMatthew G Knepley for (f = 0; f < numFields; ++f) { 1203435a35e8SMatthew G Knepley const char *fieldName; 1204435a35e8SMatthew G Knepley 1205435a35e8SMatthew G Knepley ierr = DMCreateSubDM(dm, 1, &f, &(*islist)[f], &(*dmlist)[f]);CHKERRQ(ierr); 1206435a35e8SMatthew G Knepley ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr); 1207435a35e8SMatthew G Knepley ierr = PetscStrallocpy(fieldName, (char **) &(*namelist)[f]);CHKERRQ(ierr); 1208435a35e8SMatthew G Knepley } 1209435a35e8SMatthew G Knepley } else { 121069ca1f37SDmitry Karpeev ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr); 1211e7c4fc90SDmitry Karpeev /* By default there are no DMs associated with subproblems. */ 1212e7c4fc90SDmitry Karpeev if (dmlist) *dmlist = PETSC_NULL; 1213e7c4fc90SDmitry Karpeev } 1214435a35e8SMatthew G Knepley } 1215e7c4fc90SDmitry Karpeev else { 121616621825SDmitry Karpeev ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr); 121716621825SDmitry Karpeev } 121816621825SDmitry Karpeev PetscFunctionReturn(0); 121916621825SDmitry Karpeev } 122016621825SDmitry Karpeev 122116621825SDmitry Karpeev #undef __FUNCT__ 1222435a35e8SMatthew G Knepley #define __FUNCT__ "DMCreateSubDM" 1223435a35e8SMatthew G Knepley /*@C 1224435a35e8SMatthew G Knepley DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in. 1225435a35e8SMatthew G Knepley The fields are defined by DMCreateFieldIS(). 1226435a35e8SMatthew G Knepley 1227435a35e8SMatthew G Knepley Not collective 1228435a35e8SMatthew G Knepley 1229435a35e8SMatthew G Knepley Input Parameters: 1230435a35e8SMatthew G Knepley + dm - the DM object 1231435a35e8SMatthew G Knepley . numFields - number of fields in this subproblem 1232435a35e8SMatthew G Knepley - len - The number of subproblems in the decomposition (or PETSC_NULL if not requested) 1233435a35e8SMatthew G Knepley 1234435a35e8SMatthew G Knepley Output Parameters: 1235435a35e8SMatthew G Knepley . is - The global indices for the subproblem 1236435a35e8SMatthew G Knepley - dm - The DM for the subproblem 1237435a35e8SMatthew G Knepley 1238435a35e8SMatthew G Knepley Level: intermediate 1239435a35e8SMatthew G Knepley 1240435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS() 1241435a35e8SMatthew G Knepley @*/ 1242435a35e8SMatthew G Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm) 1243435a35e8SMatthew G Knepley { 1244435a35e8SMatthew G Knepley PetscErrorCode ierr; 1245435a35e8SMatthew G Knepley 1246435a35e8SMatthew G Knepley PetscFunctionBegin; 1247435a35e8SMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 1248435a35e8SMatthew G Knepley PetscValidPointer(fields,3); 1249435a35e8SMatthew G Knepley if (is) {PetscValidPointer(is,4);} 1250435a35e8SMatthew G Knepley if (subdm) {PetscValidPointer(subdm,5);} 1251435a35e8SMatthew G Knepley if (dm->ops->createsubdm) { 1252435a35e8SMatthew G Knepley ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr); 1253435a35e8SMatthew G Knepley } else SETERRQ(((PetscObject) dm)->comm, PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined"); 1254435a35e8SMatthew G Knepley PetscFunctionReturn(0); 1255435a35e8SMatthew G Knepley } 1256435a35e8SMatthew G Knepley 1257435a35e8SMatthew G Knepley #undef __FUNCT__ 125816621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecompositionDM" 125916621825SDmitry Karpeev /*@C 126016621825SDmitry Karpeev DMCreateDomainDecompositionDM - creates a DM that encapsulates a decomposition of the original DM into subdomains. 126116621825SDmitry Karpeev 126216621825SDmitry Karpeev Not Collective 126316621825SDmitry Karpeev 126416621825SDmitry Karpeev Input Parameters: 126516621825SDmitry Karpeev + dm - the DM object 126616621825SDmitry Karpeev - name - the name of the subdomain decomposition 126716621825SDmitry Karpeev 126816621825SDmitry Karpeev Output Parameter: 126916621825SDmitry Karpeev . ddm - the subdomain decomposition DM (PETSC_NULL, if no such decomposition is known) 127016621825SDmitry Karpeev 127116621825SDmitry Karpeev Level: advanced 127216621825SDmitry Karpeev 127316621825SDmitry Karpeev .seealso DMDestroy(), DMCreate(), DMCreateFieldDecomposition(), DMCreateDomainDecompositionDM() 127416621825SDmitry Karpeev @*/ 127516621825SDmitry Karpeev PetscErrorCode DMCreateDomainDecompositionDM(DM dm, const char* name, DM *ddm) 127616621825SDmitry Karpeev { 127716621825SDmitry Karpeev PetscErrorCode ierr; 127816621825SDmitry Karpeev 127916621825SDmitry Karpeev PetscFunctionBegin; 128016621825SDmitry Karpeev PetscValidHeaderSpecific(dm,DM_CLASSID,1); 128116621825SDmitry Karpeev PetscValidCharPointer(name,2); 128216621825SDmitry Karpeev PetscValidPointer(ddm,3); 128316621825SDmitry Karpeev *ddm = PETSC_NULL; 1284731c8d9eSDmitry Karpeev if (dm->ops->createdomaindecompositiondm) { 128516621825SDmitry Karpeev ierr = (*dm->ops->createdomaindecompositiondm)(dm,name,ddm);CHKERRQ(ierr); 128616621825SDmitry Karpeev } 128716621825SDmitry Karpeev PetscFunctionReturn(0); 128816621825SDmitry Karpeev } 128916621825SDmitry Karpeev 129016621825SDmitry Karpeev #undef __FUNCT__ 129116621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecomposition" 129216621825SDmitry Karpeev /*@C 12938d4ac253SDmitry Karpeev DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems 12948d4ac253SDmitry Karpeev corresponding to restrictions to pairs nested subdomains: each IS contains the global 12958d4ac253SDmitry Karpeev indices of the dofs of the corresponding subdomains. The inner subdomains conceptually 12968d4ac253SDmitry Karpeev define a nonoverlapping covering, while outer subdomains can overlap. 12978d4ac253SDmitry Karpeev The optional list of DMs define the DM for each subproblem. 129816621825SDmitry Karpeev 129916621825SDmitry Karpeev Not collective 130016621825SDmitry Karpeev 130116621825SDmitry Karpeev Input Parameter: 130216621825SDmitry Karpeev . dm - the DM object 130316621825SDmitry Karpeev 130416621825SDmitry Karpeev Output Parameters: 130516621825SDmitry Karpeev + len - The number of subproblems in the domain decomposition (or PETSC_NULL if not requested) 130616621825SDmitry Karpeev . namelist - The name for each subdomain (or PETSC_NULL if not requested) 13078d4ac253SDmitry Karpeev . innerislist - The global indices for each inner subdomain (or PETSC_NULL, if not requested) 13088d4ac253SDmitry Karpeev . outerislist - The global indices for each outer subdomain (or PETSC_NULL, if not requested) 130916621825SDmitry Karpeev - dmlist - The DMs for each subdomain subproblem (or PETSC_NULL, if not requested; if PETSC_NULL is returned, no DMs are defined) 131016621825SDmitry Karpeev 131116621825SDmitry Karpeev Level: intermediate 131216621825SDmitry Karpeev 131316621825SDmitry Karpeev Notes: 131416621825SDmitry Karpeev The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with 131516621825SDmitry Karpeev PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(), 131616621825SDmitry Karpeev and all of the arrays should be freed with PetscFree(). 131716621825SDmitry Karpeev 13188d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition() 131916621825SDmitry Karpeev @*/ 13208d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist) 132116621825SDmitry Karpeev { 132216621825SDmitry Karpeev PetscErrorCode ierr; 1323be081cd6SPeter Brune DMSubDomainHookLink link; 1324be081cd6SPeter Brune PetscInt i,l; 132516621825SDmitry Karpeev 132616621825SDmitry Karpeev PetscFunctionBegin; 132716621825SDmitry Karpeev PetscValidHeaderSpecific(dm,DM_CLASSID,1); 132814a18fd3SPeter Brune if (len) {PetscValidPointer(len,2); *len = 0;} 132916621825SDmitry Karpeev if (namelist) {PetscValidPointer(namelist,3); *namelist = PETSC_NULL;} 13308d4ac253SDmitry Karpeev if (innerislist) {PetscValidPointer(innerislist,4); *innerislist = PETSC_NULL;} 13318d4ac253SDmitry Karpeev if (outerislist) {PetscValidPointer(outerislist,5); *outerislist = PETSC_NULL;} 13328d4ac253SDmitry Karpeev if (dmlist) {PetscValidPointer(dmlist,6); *dmlist = PETSC_NULL;} 133316621825SDmitry Karpeev if (dm->ops->createdomaindecomposition) { 1334be081cd6SPeter Brune ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr); 133514a18fd3SPeter Brune /* copy subdomain hooks and context over to the subdomain DMs */ 133614a18fd3SPeter Brune if (dmlist) { 1337be081cd6SPeter Brune for (i = 0; i < l; i++) { 1338be081cd6SPeter Brune for (link=dm->subdomainhook; link; link=link->next) { 1339be081cd6SPeter Brune if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);} 1340be081cd6SPeter Brune } 1341d425c654SPeter Brune (*dmlist)[i]->ctx = dm->ctx; 1342e7c4fc90SDmitry Karpeev } 134314a18fd3SPeter Brune } 134414a18fd3SPeter Brune if (len) *len = l; 134514a18fd3SPeter Brune } 1346e30e807fSPeter Brune PetscFunctionReturn(0); 1347e30e807fSPeter Brune } 1348e30e807fSPeter Brune 1349e30e807fSPeter Brune 1350e30e807fSPeter Brune #undef __FUNCT__ 1351e30e807fSPeter Brune #define __FUNCT__ "DMCreateDomainDecompositionScatters" 1352e30e807fSPeter Brune /*@C 1353e30e807fSPeter Brune DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector 1354e30e807fSPeter Brune 1355e30e807fSPeter Brune Not collective 1356e30e807fSPeter Brune 1357e30e807fSPeter Brune Input Parameters: 1358e30e807fSPeter Brune + dm - the DM object 1359e30e807fSPeter Brune . n - the number of subdomain scatters 1360e30e807fSPeter Brune - subdms - the local subdomains 1361e30e807fSPeter Brune 1362e30e807fSPeter Brune Output Parameters: 1363e30e807fSPeter Brune + n - the number of scatters returned 1364e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain 1365e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain 1366e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts) 1367e30e807fSPeter Brune 1368e30e807fSPeter Brune Notes: This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution 1369e30e807fSPeter Brune of general nonlinear problems with overlapping subdomain methods. While merely having index sets that enable subsets 1370e30e807fSPeter Brune of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of 1371e30e807fSPeter Brune solution and residual data. 1372e30e807fSPeter Brune 1373e30e807fSPeter Brune Level: developer 1374e30e807fSPeter Brune 1375e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS() 1376e30e807fSPeter Brune @*/ 1377e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat) 1378e30e807fSPeter Brune { 1379e30e807fSPeter Brune PetscErrorCode ierr; 1380e30e807fSPeter Brune 1381e30e807fSPeter Brune PetscFunctionBegin; 1382e30e807fSPeter Brune PetscValidHeaderSpecific(dm,DM_CLASSID,1); 1383e30e807fSPeter Brune PetscValidPointer(subdms,3); 1384e30e807fSPeter Brune PetscValidPointer(iscat,4); 1385e30e807fSPeter Brune PetscValidPointer(oscat,5); 1386e30e807fSPeter Brune PetscValidPointer(gscat,6); 1387e30e807fSPeter Brune if (dm->ops->createddscatters) { 1388e30e807fSPeter Brune ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr); 1389e30e807fSPeter Brune } else SETERRQ(((PetscObject) dm)->comm, PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionLocalScatter implementation defined"); 1390e7c4fc90SDmitry Karpeev PetscFunctionReturn(0); 1391e7c4fc90SDmitry Karpeev } 1392e7c4fc90SDmitry Karpeev 1393731c8d9eSDmitry Karpeev #undef __FUNCT__ 139447c6ae99SBarry Smith #define __FUNCT__ "DMRefine" 139547c6ae99SBarry Smith /*@ 139647c6ae99SBarry Smith DMRefine - Refines a DM object 139747c6ae99SBarry Smith 139847c6ae99SBarry Smith Collective on DM 139947c6ae99SBarry Smith 140047c6ae99SBarry Smith Input Parameter: 140147c6ae99SBarry Smith + dm - the DM object 140291d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL) 140347c6ae99SBarry Smith 140447c6ae99SBarry Smith Output Parameter: 1405ae0a1c52SMatthew G Knepley . dmf - the refined DM, or PETSC_NULL 1406ae0a1c52SMatthew G Knepley 1407ae0a1c52SMatthew G Knepley Note: If no refinement was done, the return value is PETSC_NULL 140847c6ae99SBarry Smith 140947c6ae99SBarry Smith Level: developer 141047c6ae99SBarry Smith 1411e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation() 141247c6ae99SBarry Smith @*/ 14137087cfbeSBarry Smith PetscErrorCode DMRefine(DM dm,MPI_Comm comm,DM *dmf) 141447c6ae99SBarry Smith { 141547c6ae99SBarry Smith PetscErrorCode ierr; 1416c833c3b5SJed Brown DMRefineHookLink link; 141747c6ae99SBarry Smith 141847c6ae99SBarry Smith PetscFunctionBegin; 1419732e2eb9SMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 142047c6ae99SBarry Smith ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr); 14214057135bSMatthew G Knepley if (*dmf) { 142243842a1eSJed Brown (*dmf)->ops->creatematrix = dm->ops->creatematrix; 14238cd211a4SJed Brown ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr); 1424644e2e5bSBarry Smith (*dmf)->ctx = dm->ctx; 14250598a293SJed Brown (*dmf)->leveldown = dm->leveldown; 1426656b349aSBarry Smith (*dmf)->levelup = dm->levelup + 1; 1427e4b4b23bSJed Brown ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr); 1428c833c3b5SJed Brown for (link=dm->refinehook; link; link=link->next) { 1429c833c3b5SJed Brown if (link->refinehook) {ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);} 1430c833c3b5SJed Brown } 1431c833c3b5SJed Brown } 1432c833c3b5SJed Brown PetscFunctionReturn(0); 1433c833c3b5SJed Brown } 1434c833c3b5SJed Brown 1435c833c3b5SJed Brown #undef __FUNCT__ 1436c833c3b5SJed Brown #define __FUNCT__ "DMRefineHookAdd" 1437c833c3b5SJed Brown /*@ 1438c833c3b5SJed Brown DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid 1439c833c3b5SJed Brown 1440c833c3b5SJed Brown Logically Collective 1441c833c3b5SJed Brown 1442c833c3b5SJed Brown Input Arguments: 1443c833c3b5SJed Brown + coarse - nonlinear solver context on which to run a hook when restricting to a coarser level 1444c833c3b5SJed Brown . refinehook - function to run when setting up a coarser level 1445c833c3b5SJed Brown . interphook - function to run to update data on finer levels (once per SNESSolve()) 1446c833c3b5SJed Brown - ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL) 1447c833c3b5SJed Brown 1448c833c3b5SJed Brown Calling sequence of refinehook: 1449c833c3b5SJed Brown $ refinehook(DM coarse,DM fine,void *ctx); 1450c833c3b5SJed Brown 1451c833c3b5SJed Brown + coarse - coarse level DM 1452c833c3b5SJed Brown . fine - fine level DM to interpolate problem to 1453c833c3b5SJed Brown - ctx - optional user-defined function context 1454c833c3b5SJed Brown 1455c833c3b5SJed Brown Calling sequence for interphook: 1456c833c3b5SJed Brown $ interphook(DM coarse,Mat interp,DM fine,void *ctx) 1457c833c3b5SJed Brown 1458c833c3b5SJed Brown + coarse - coarse level DM 1459c833c3b5SJed Brown . interp - matrix interpolating a coarse-level solution to the finer grid 1460c833c3b5SJed Brown . fine - fine level DM to update 1461c833c3b5SJed Brown - ctx - optional user-defined function context 1462c833c3b5SJed Brown 1463c833c3b5SJed Brown Level: advanced 1464c833c3b5SJed Brown 1465c833c3b5SJed Brown Notes: 1466c833c3b5SJed Brown This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing 1467c833c3b5SJed Brown 1468c833c3b5SJed Brown If this function is called multiple times, the hooks will be run in the order they are added. 1469c833c3b5SJed Brown 1470c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate() 1471c833c3b5SJed Brown @*/ 1472c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx) 1473c833c3b5SJed Brown { 1474c833c3b5SJed Brown PetscErrorCode ierr; 1475c833c3b5SJed Brown DMRefineHookLink link,*p; 1476c833c3b5SJed Brown 1477c833c3b5SJed Brown PetscFunctionBegin; 1478c833c3b5SJed Brown PetscValidHeaderSpecific(coarse,DM_CLASSID,1); 1479c833c3b5SJed Brown for (p=&coarse->refinehook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */ 1480c833c3b5SJed Brown ierr = PetscMalloc(sizeof(struct _DMRefineHookLink),&link);CHKERRQ(ierr); 1481c833c3b5SJed Brown link->refinehook = refinehook; 1482c833c3b5SJed Brown link->interphook = interphook; 1483c833c3b5SJed Brown link->ctx = ctx; 1484c833c3b5SJed Brown link->next = PETSC_NULL; 1485c833c3b5SJed Brown *p = link; 1486c833c3b5SJed Brown PetscFunctionReturn(0); 1487c833c3b5SJed Brown } 1488c833c3b5SJed Brown 1489c833c3b5SJed Brown #undef __FUNCT__ 1490c833c3b5SJed Brown #define __FUNCT__ "DMInterpolate" 1491c833c3b5SJed Brown /*@ 1492c833c3b5SJed Brown DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd() 1493c833c3b5SJed Brown 1494c833c3b5SJed Brown Collective if any hooks are 1495c833c3b5SJed Brown 1496c833c3b5SJed Brown Input Arguments: 1497c833c3b5SJed Brown + coarse - coarser DM to use as a base 1498c833c3b5SJed Brown . restrct - interpolation matrix, apply using MatInterpolate() 1499c833c3b5SJed Brown - fine - finer DM to update 1500c833c3b5SJed Brown 1501c833c3b5SJed Brown Level: developer 1502c833c3b5SJed Brown 1503c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate() 1504c833c3b5SJed Brown @*/ 1505c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine) 1506c833c3b5SJed Brown { 1507c833c3b5SJed Brown PetscErrorCode ierr; 1508c833c3b5SJed Brown DMRefineHookLink link; 1509c833c3b5SJed Brown 1510c833c3b5SJed Brown PetscFunctionBegin; 1511c833c3b5SJed Brown for (link=fine->refinehook; link; link=link->next) { 1512c833c3b5SJed Brown if (link->interphook) {ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);} 15134057135bSMatthew G Knepley } 151447c6ae99SBarry Smith PetscFunctionReturn(0); 151547c6ae99SBarry Smith } 151647c6ae99SBarry Smith 151747c6ae99SBarry Smith #undef __FUNCT__ 1518eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel" 1519eb3f98d2SBarry Smith /*@ 1520eb3f98d2SBarry Smith DMGetRefineLevel - Get's the number of refinements that have generated this DM. 1521eb3f98d2SBarry Smith 1522eb3f98d2SBarry Smith Not Collective 1523eb3f98d2SBarry Smith 1524eb3f98d2SBarry Smith Input Parameter: 1525eb3f98d2SBarry Smith . dm - the DM object 1526eb3f98d2SBarry Smith 1527eb3f98d2SBarry Smith Output Parameter: 1528eb3f98d2SBarry Smith . level - number of refinements 1529eb3f98d2SBarry Smith 1530eb3f98d2SBarry Smith Level: developer 1531eb3f98d2SBarry Smith 15326a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation() 1533eb3f98d2SBarry Smith 1534eb3f98d2SBarry Smith @*/ 1535eb3f98d2SBarry Smith PetscErrorCode DMGetRefineLevel(DM dm,PetscInt *level) 1536eb3f98d2SBarry Smith { 1537eb3f98d2SBarry Smith PetscFunctionBegin; 1538eb3f98d2SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 1539eb3f98d2SBarry Smith *level = dm->levelup; 1540eb3f98d2SBarry Smith PetscFunctionReturn(0); 1541eb3f98d2SBarry Smith } 1542eb3f98d2SBarry Smith 1543eb3f98d2SBarry Smith #undef __FUNCT__ 1544baf369e7SPeter Brune #define __FUNCT__ "DMGlobalToLocalHookAdd" 1545baf369e7SPeter Brune /*@ 1546baf369e7SPeter Brune DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called 1547baf369e7SPeter Brune 1548baf369e7SPeter Brune Logically Collective 1549baf369e7SPeter Brune 1550baf369e7SPeter Brune Input Arguments: 1551baf369e7SPeter Brune + dm - the DM 1552baf369e7SPeter Brune . beginhook - function to run at the beginning of DMGlobalToLocalBegin() 1553baf369e7SPeter Brune . endhook - function to run after DMGlobalToLocalEnd() has completed 1554baf369e7SPeter Brune - ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL) 1555baf369e7SPeter Brune 1556baf369e7SPeter Brune Calling sequence for beginhook: 1557baf369e7SPeter Brune $ beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx) 1558baf369e7SPeter Brune 1559baf369e7SPeter Brune + dm - global DM 1560baf369e7SPeter Brune . g - global vector 1561baf369e7SPeter Brune . mode - mode 1562baf369e7SPeter Brune . l - local vector 1563baf369e7SPeter Brune - ctx - optional user-defined function context 1564baf369e7SPeter Brune 1565baf369e7SPeter Brune 1566baf369e7SPeter Brune Calling sequence for endhook: 1567baf369e7SPeter Brune $ beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx) 1568baf369e7SPeter Brune 1569baf369e7SPeter Brune + global - global DM 1570baf369e7SPeter Brune - ctx - optional user-defined function context 1571baf369e7SPeter Brune 1572baf369e7SPeter Brune Level: advanced 1573baf369e7SPeter Brune 1574baf369e7SPeter Brune Notes: 1575baf369e7SPeter Brune This function is only needed if auxiliary data needs to be set up on coarse grids. 1576baf369e7SPeter Brune 1577baf369e7SPeter Brune If this function is called multiple times, the hooks will be run in the order they are added. 1578baf369e7SPeter Brune 1579baf369e7SPeter Brune In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to 1580baf369e7SPeter Brune extract the finest level information from its context (instead of from the SNES). 1581baf369e7SPeter Brune 1582baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate() 1583baf369e7SPeter Brune @*/ 1584baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx) 1585baf369e7SPeter Brune { 1586baf369e7SPeter Brune PetscErrorCode ierr; 1587baf369e7SPeter Brune DMGlobalToLocalHookLink link,*p; 1588baf369e7SPeter Brune 1589baf369e7SPeter Brune PetscFunctionBegin; 1590baf369e7SPeter Brune PetscValidHeaderSpecific(dm,DM_CLASSID,1); 1591baf369e7SPeter Brune for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */ 1592baf369e7SPeter Brune ierr = PetscMalloc(sizeof(struct _DMGlobalToLocalHookLink),&link);CHKERRQ(ierr); 1593baf369e7SPeter Brune link->beginhook = beginhook; 1594baf369e7SPeter Brune link->endhook = endhook; 1595baf369e7SPeter Brune link->ctx = ctx; 1596baf369e7SPeter Brune link->next = PETSC_NULL; 1597baf369e7SPeter Brune *p = link; 1598baf369e7SPeter Brune PetscFunctionReturn(0); 1599baf369e7SPeter Brune } 1600baf369e7SPeter Brune 1601baf369e7SPeter Brune #undef __FUNCT__ 160247c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin" 160347c6ae99SBarry Smith /*@ 160447c6ae99SBarry Smith DMGlobalToLocalBegin - Begins updating local vectors from global vector 160547c6ae99SBarry Smith 160647c6ae99SBarry Smith Neighbor-wise Collective on DM 160747c6ae99SBarry Smith 160847c6ae99SBarry Smith Input Parameters: 160947c6ae99SBarry Smith + dm - the DM object 161047c6ae99SBarry Smith . g - the global vector 161147c6ae99SBarry Smith . mode - INSERT_VALUES or ADD_VALUES 161247c6ae99SBarry Smith - l - the local vector 161347c6ae99SBarry Smith 161447c6ae99SBarry Smith 161547c6ae99SBarry Smith Level: beginner 161647c6ae99SBarry Smith 1617e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin() 161847c6ae99SBarry Smith 161947c6ae99SBarry Smith @*/ 16207087cfbeSBarry Smith PetscErrorCode DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l) 162147c6ae99SBarry Smith { 16227128ae9fSMatthew G Knepley PetscSF sf; 162347c6ae99SBarry Smith PetscErrorCode ierr; 1624baf369e7SPeter Brune DMGlobalToLocalHookLink link; 162547c6ae99SBarry Smith 162647c6ae99SBarry Smith PetscFunctionBegin; 1627171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 1628baf369e7SPeter Brune for (link=dm->gtolhook; link; link=link->next) { 1629baf369e7SPeter Brune if (link->beginhook) {ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);} 1630baf369e7SPeter Brune } 16317128ae9fSMatthew G Knepley ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr); 16327128ae9fSMatthew G Knepley if (sf) { 16337128ae9fSMatthew G Knepley PetscScalar *lArray, *gArray; 16347128ae9fSMatthew G Knepley 16357128ae9fSMatthew G Knepley if (mode == ADD_VALUES) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode); 16367128ae9fSMatthew G Knepley ierr = VecGetArray(l, &lArray);CHKERRQ(ierr); 16377128ae9fSMatthew G Knepley ierr = VecGetArray(g, &gArray);CHKERRQ(ierr); 16387128ae9fSMatthew G Knepley ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr); 16397128ae9fSMatthew G Knepley ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr); 16407128ae9fSMatthew G Knepley ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr); 16417128ae9fSMatthew G Knepley } else { 1642843c4018SMatthew G Knepley ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr); 16437128ae9fSMatthew G Knepley } 164447c6ae99SBarry Smith PetscFunctionReturn(0); 164547c6ae99SBarry Smith } 164647c6ae99SBarry Smith 164747c6ae99SBarry Smith #undef __FUNCT__ 164847c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd" 164947c6ae99SBarry Smith /*@ 165047c6ae99SBarry Smith DMGlobalToLocalEnd - Ends updating local vectors from global vector 165147c6ae99SBarry Smith 165247c6ae99SBarry Smith Neighbor-wise Collective on DM 165347c6ae99SBarry Smith 165447c6ae99SBarry Smith Input Parameters: 165547c6ae99SBarry Smith + dm - the DM object 165647c6ae99SBarry Smith . g - the global vector 165747c6ae99SBarry Smith . mode - INSERT_VALUES or ADD_VALUES 165847c6ae99SBarry Smith - l - the local vector 165947c6ae99SBarry Smith 166047c6ae99SBarry Smith 166147c6ae99SBarry Smith Level: beginner 166247c6ae99SBarry Smith 1663e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin() 166447c6ae99SBarry Smith 166547c6ae99SBarry Smith @*/ 16667087cfbeSBarry Smith PetscErrorCode DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l) 166747c6ae99SBarry Smith { 16687128ae9fSMatthew G Knepley PetscSF sf; 166947c6ae99SBarry Smith PetscErrorCode ierr; 167061a3c1faSSatish Balay PetscScalar *lArray, *gArray; 1671baf369e7SPeter Brune DMGlobalToLocalHookLink link; 167247c6ae99SBarry Smith 167347c6ae99SBarry Smith PetscFunctionBegin; 1674171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 16757128ae9fSMatthew G Knepley ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr); 16767128ae9fSMatthew G Knepley if (sf) { 16777128ae9fSMatthew G Knepley if (mode == ADD_VALUES) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode); 16787128ae9fSMatthew G Knepley 16797128ae9fSMatthew G Knepley ierr = VecGetArray(l, &lArray);CHKERRQ(ierr); 16807128ae9fSMatthew G Knepley ierr = VecGetArray(g, &gArray);CHKERRQ(ierr); 16817128ae9fSMatthew G Knepley ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr); 16827128ae9fSMatthew G Knepley ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr); 16837128ae9fSMatthew G Knepley ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr); 16847128ae9fSMatthew G Knepley } else { 1685843c4018SMatthew G Knepley ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr); 16867128ae9fSMatthew G Knepley } 1687baf369e7SPeter Brune for (link=dm->gtolhook; link; link=link->next) { 1688baf369e7SPeter Brune if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);} 1689baf369e7SPeter Brune } 169047c6ae99SBarry Smith PetscFunctionReturn(0); 169147c6ae99SBarry Smith } 169247c6ae99SBarry Smith 169347c6ae99SBarry Smith #undef __FUNCT__ 16949a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin" 169547c6ae99SBarry Smith /*@ 16969a42bb27SBarry Smith DMLocalToGlobalBegin - updates global vectors from local vectors 16979a42bb27SBarry Smith 16989a42bb27SBarry Smith Neighbor-wise Collective on DM 16999a42bb27SBarry Smith 17009a42bb27SBarry Smith Input Parameters: 17019a42bb27SBarry Smith + dm - the DM object 1702f6813fd5SJed Brown . l - the local vector 17039a42bb27SBarry 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 17049a42bb27SBarry Smith base point. 1705f6813fd5SJed Brown - - the global vector 17069a42bb27SBarry Smith 17079a42bb27SBarry 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 17089a42bb27SBarry 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 17099a42bb27SBarry Smith global array to the final global array with VecAXPY(). 17109a42bb27SBarry Smith 17119a42bb27SBarry Smith Level: beginner 17129a42bb27SBarry Smith 1713e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin() 17149a42bb27SBarry Smith 17159a42bb27SBarry Smith @*/ 17167087cfbeSBarry Smith PetscErrorCode DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g) 17179a42bb27SBarry Smith { 17187128ae9fSMatthew G Knepley PetscSF sf; 17199a42bb27SBarry Smith PetscErrorCode ierr; 17209a42bb27SBarry Smith 17219a42bb27SBarry Smith PetscFunctionBegin; 1722171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 17237128ae9fSMatthew G Knepley ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr); 17247128ae9fSMatthew G Knepley if (sf) { 17257128ae9fSMatthew G Knepley MPI_Op op; 17267128ae9fSMatthew G Knepley PetscScalar *lArray, *gArray; 17277128ae9fSMatthew G Knepley 17287128ae9fSMatthew G Knepley switch(mode) { 17297128ae9fSMatthew G Knepley case INSERT_VALUES: 17307128ae9fSMatthew G Knepley case INSERT_ALL_VALUES: 17317128ae9fSMatthew G Knepley #if defined(PETSC_HAVE_MPI_REPLACE) 17327128ae9fSMatthew G Knepley op = MPI_REPLACE; break; 17337128ae9fSMatthew G Knepley #else 17347128ae9fSMatthew G Knepley SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No support for INSERT_VALUES without an MPI-2 implementation"); 17357128ae9fSMatthew G Knepley #endif 17367128ae9fSMatthew G Knepley case ADD_VALUES: 17377128ae9fSMatthew G Knepley case ADD_ALL_VALUES: 17387128ae9fSMatthew G Knepley op = MPI_SUM; break; 17397128ae9fSMatthew G Knepley default: 17407128ae9fSMatthew G Knepley SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode); 17417128ae9fSMatthew G Knepley } 17427128ae9fSMatthew G Knepley ierr = VecGetArray(l, &lArray);CHKERRQ(ierr); 17437128ae9fSMatthew G Knepley ierr = VecGetArray(g, &gArray);CHKERRQ(ierr); 17447128ae9fSMatthew G Knepley ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr); 17457128ae9fSMatthew G Knepley ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr); 17467128ae9fSMatthew G Knepley ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr); 17477128ae9fSMatthew G Knepley } else { 1748843c4018SMatthew G Knepley ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr); 17497128ae9fSMatthew G Knepley } 17509a42bb27SBarry Smith PetscFunctionReturn(0); 17519a42bb27SBarry Smith } 17529a42bb27SBarry Smith 17539a42bb27SBarry Smith #undef __FUNCT__ 17549a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd" 17559a42bb27SBarry Smith /*@ 17569a42bb27SBarry Smith DMLocalToGlobalEnd - updates global vectors from local vectors 175747c6ae99SBarry Smith 175847c6ae99SBarry Smith Neighbor-wise Collective on DM 175947c6ae99SBarry Smith 176047c6ae99SBarry Smith Input Parameters: 176147c6ae99SBarry Smith + dm - the DM object 1762f6813fd5SJed Brown . l - the local vector 176347c6ae99SBarry Smith . mode - INSERT_VALUES or ADD_VALUES 1764f6813fd5SJed Brown - g - the global vector 176547c6ae99SBarry Smith 176647c6ae99SBarry Smith 176747c6ae99SBarry Smith Level: beginner 176847c6ae99SBarry Smith 1769e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd() 177047c6ae99SBarry Smith 177147c6ae99SBarry Smith @*/ 17727087cfbeSBarry Smith PetscErrorCode DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g) 177347c6ae99SBarry Smith { 17747128ae9fSMatthew G Knepley PetscSF sf; 177547c6ae99SBarry Smith PetscErrorCode ierr; 177647c6ae99SBarry Smith 177747c6ae99SBarry Smith PetscFunctionBegin; 1778171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 17797128ae9fSMatthew G Knepley ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr); 17807128ae9fSMatthew G Knepley if (sf) { 17817128ae9fSMatthew G Knepley MPI_Op op; 17827128ae9fSMatthew G Knepley PetscScalar *lArray, *gArray; 17837128ae9fSMatthew G Knepley 17847128ae9fSMatthew G Knepley switch(mode) { 17857128ae9fSMatthew G Knepley case INSERT_VALUES: 17867128ae9fSMatthew G Knepley case INSERT_ALL_VALUES: 17877128ae9fSMatthew G Knepley #if defined(PETSC_HAVE_MPI_REPLACE) 17887128ae9fSMatthew G Knepley op = MPI_REPLACE; break; 17897128ae9fSMatthew G Knepley #else 17907128ae9fSMatthew G Knepley SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No support for INSERT_VALUES without an MPI-2 implementation"); 17917128ae9fSMatthew G Knepley #endif 17927128ae9fSMatthew G Knepley case ADD_VALUES: 17937128ae9fSMatthew G Knepley case ADD_ALL_VALUES: 17947128ae9fSMatthew G Knepley op = MPI_SUM; break; 17957128ae9fSMatthew G Knepley default: 17967128ae9fSMatthew G Knepley SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode); 17977128ae9fSMatthew G Knepley } 17987128ae9fSMatthew G Knepley ierr = VecGetArray(l, &lArray);CHKERRQ(ierr); 17997128ae9fSMatthew G Knepley ierr = VecGetArray(g, &gArray);CHKERRQ(ierr); 18007128ae9fSMatthew G Knepley ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr); 18017128ae9fSMatthew G Knepley ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr); 18027128ae9fSMatthew G Knepley ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr); 18037128ae9fSMatthew G Knepley } else { 1804843c4018SMatthew G Knepley ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr); 18057128ae9fSMatthew G Knepley } 180647c6ae99SBarry Smith PetscFunctionReturn(0); 180747c6ae99SBarry Smith } 180847c6ae99SBarry Smith 180947c6ae99SBarry Smith #undef __FUNCT__ 181047c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen" 181147c6ae99SBarry Smith /*@ 181247c6ae99SBarry Smith DMCoarsen - Coarsens a DM object 181347c6ae99SBarry Smith 181447c6ae99SBarry Smith Collective on DM 181547c6ae99SBarry Smith 181647c6ae99SBarry Smith Input Parameter: 181747c6ae99SBarry Smith + dm - the DM object 181891d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL) 181947c6ae99SBarry Smith 182047c6ae99SBarry Smith Output Parameter: 182147c6ae99SBarry Smith . dmc - the coarsened DM 182247c6ae99SBarry Smith 182347c6ae99SBarry Smith Level: developer 182447c6ae99SBarry Smith 1825e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation() 182647c6ae99SBarry Smith 182747c6ae99SBarry Smith @*/ 18287087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc) 182947c6ae99SBarry Smith { 183047c6ae99SBarry Smith PetscErrorCode ierr; 1831b17ce1afSJed Brown DMCoarsenHookLink link; 183247c6ae99SBarry Smith 183347c6ae99SBarry Smith PetscFunctionBegin; 1834171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 183547c6ae99SBarry Smith ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr); 183643842a1eSJed Brown (*dmc)->ops->creatematrix = dm->ops->creatematrix; 18378cd211a4SJed Brown ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr); 1838644e2e5bSBarry Smith (*dmc)->ctx = dm->ctx; 18390598a293SJed Brown (*dmc)->levelup = dm->levelup; 1840656b349aSBarry Smith (*dmc)->leveldown = dm->leveldown + 1; 1841e4b4b23bSJed Brown ierr = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr); 1842b17ce1afSJed Brown for (link=dm->coarsenhook; link; link=link->next) { 1843b17ce1afSJed Brown if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);} 1844b17ce1afSJed Brown } 1845b17ce1afSJed Brown PetscFunctionReturn(0); 1846b17ce1afSJed Brown } 1847b17ce1afSJed Brown 1848b17ce1afSJed Brown #undef __FUNCT__ 1849b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd" 1850b17ce1afSJed Brown /*@ 1851b17ce1afSJed Brown DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid 1852b17ce1afSJed Brown 1853b17ce1afSJed Brown Logically Collective 1854b17ce1afSJed Brown 1855b17ce1afSJed Brown Input Arguments: 1856b17ce1afSJed Brown + fine - nonlinear solver context on which to run a hook when restricting to a coarser level 1857b17ce1afSJed Brown . coarsenhook - function to run when setting up a coarser level 1858b17ce1afSJed Brown . restricthook - function to run to update data on coarser levels (once per SNESSolve()) 1859b17ce1afSJed Brown - ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL) 1860b17ce1afSJed Brown 1861b17ce1afSJed Brown Calling sequence of coarsenhook: 1862b17ce1afSJed Brown $ coarsenhook(DM fine,DM coarse,void *ctx); 1863b17ce1afSJed Brown 1864b17ce1afSJed Brown + fine - fine level DM 1865b17ce1afSJed Brown . coarse - coarse level DM to restrict problem to 1866b17ce1afSJed Brown - ctx - optional user-defined function context 1867b17ce1afSJed Brown 1868b17ce1afSJed Brown Calling sequence for restricthook: 1869c833c3b5SJed Brown $ restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx) 1870b17ce1afSJed Brown 1871b17ce1afSJed Brown + fine - fine level DM 1872b17ce1afSJed Brown . mrestrict - matrix restricting a fine-level solution to the coarse grid 1873c833c3b5SJed Brown . rscale - scaling vector for restriction 1874c833c3b5SJed Brown . inject - matrix restricting by injection 1875b17ce1afSJed Brown . coarse - coarse level DM to update 1876b17ce1afSJed Brown - ctx - optional user-defined function context 1877b17ce1afSJed Brown 1878b17ce1afSJed Brown Level: advanced 1879b17ce1afSJed Brown 1880b17ce1afSJed Brown Notes: 1881b17ce1afSJed Brown This function is only needed if auxiliary data needs to be set up on coarse grids. 1882b17ce1afSJed Brown 1883b17ce1afSJed Brown If this function is called multiple times, the hooks will be run in the order they are added. 1884b17ce1afSJed Brown 1885b17ce1afSJed Brown In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to 1886b17ce1afSJed Brown extract the finest level information from its context (instead of from the SNES). 1887b17ce1afSJed Brown 1888c833c3b5SJed Brown .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate() 1889b17ce1afSJed Brown @*/ 1890b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx) 1891b17ce1afSJed Brown { 1892b17ce1afSJed Brown PetscErrorCode ierr; 1893b17ce1afSJed Brown DMCoarsenHookLink link,*p; 1894b17ce1afSJed Brown 1895b17ce1afSJed Brown PetscFunctionBegin; 1896b17ce1afSJed Brown PetscValidHeaderSpecific(fine,DM_CLASSID,1); 18976bfea28cSJed Brown for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */ 1898b17ce1afSJed Brown ierr = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr); 1899b17ce1afSJed Brown link->coarsenhook = coarsenhook; 1900b17ce1afSJed Brown link->restricthook = restricthook; 1901b17ce1afSJed Brown link->ctx = ctx; 19026cab3a1bSJed Brown link->next = PETSC_NULL; 1903b17ce1afSJed Brown *p = link; 1904b17ce1afSJed Brown PetscFunctionReturn(0); 1905b17ce1afSJed Brown } 1906b17ce1afSJed Brown 1907b17ce1afSJed Brown #undef __FUNCT__ 1908b17ce1afSJed Brown #define __FUNCT__ "DMRestrict" 1909b17ce1afSJed Brown /*@ 1910b17ce1afSJed Brown DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd() 1911b17ce1afSJed Brown 1912b17ce1afSJed Brown Collective if any hooks are 1913b17ce1afSJed Brown 1914b17ce1afSJed Brown Input Arguments: 1915b17ce1afSJed Brown + fine - finer DM to use as a base 1916b17ce1afSJed Brown . restrct - restriction matrix, apply using MatRestrict() 1917b17ce1afSJed Brown . inject - injection matrix, also use MatRestrict() 1918b17ce1afSJed Brown - coarse - coarer DM to update 1919b17ce1afSJed Brown 1920b17ce1afSJed Brown Level: developer 1921b17ce1afSJed Brown 1922b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict() 1923b17ce1afSJed Brown @*/ 1924b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse) 1925b17ce1afSJed Brown { 1926b17ce1afSJed Brown PetscErrorCode ierr; 1927b17ce1afSJed Brown DMCoarsenHookLink link; 1928b17ce1afSJed Brown 1929b17ce1afSJed Brown PetscFunctionBegin; 1930b17ce1afSJed Brown for (link=fine->coarsenhook; link; link=link->next) { 1931b17ce1afSJed Brown if (link->restricthook) {ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);} 1932b17ce1afSJed Brown } 193347c6ae99SBarry Smith PetscFunctionReturn(0); 193447c6ae99SBarry Smith } 193547c6ae99SBarry Smith 193647c6ae99SBarry Smith #undef __FUNCT__ 1937be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainHookAdd" 19385dbd56e3SPeter Brune /*@ 1939be081cd6SPeter Brune DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid 19405dbd56e3SPeter Brune 19415dbd56e3SPeter Brune Logically Collective 19425dbd56e3SPeter Brune 19435dbd56e3SPeter Brune Input Arguments: 19445dbd56e3SPeter Brune + global - global DM 19455dbd56e3SPeter Brune . restricthook - function to run to update data on block solve (at the beginning of the block solve) 19465dbd56e3SPeter Brune - ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL) 19475dbd56e3SPeter Brune 19485dbd56e3SPeter Brune Calling sequence for restricthook: 19495dbd56e3SPeter Brune $ restricthook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx) 19505dbd56e3SPeter Brune 19515dbd56e3SPeter Brune + global - global DM 19525dbd56e3SPeter Brune . out - scatter to the outer (with ghost and overlap points) block vector 19535dbd56e3SPeter Brune . in - scatter to block vector values only owned locally 19545dbd56e3SPeter Brune . block - block DM (may just be a shell if the global DM is passed in correctly) 19555dbd56e3SPeter Brune - ctx - optional user-defined function context 19565dbd56e3SPeter Brune 19575dbd56e3SPeter Brune Level: advanced 19585dbd56e3SPeter Brune 19595dbd56e3SPeter Brune Notes: 19605dbd56e3SPeter Brune This function is only needed if auxiliary data needs to be set up on coarse grids. 19615dbd56e3SPeter Brune 19625dbd56e3SPeter Brune If this function is called multiple times, the hooks will be run in the order they are added. 19635dbd56e3SPeter Brune 19645dbd56e3SPeter Brune In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to 19655dbd56e3SPeter Brune extract the finest level information from its context (instead of from the SNES). 19665dbd56e3SPeter Brune 19675dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate() 19685dbd56e3SPeter Brune @*/ 1969be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx) 19705dbd56e3SPeter Brune { 19715dbd56e3SPeter Brune PetscErrorCode ierr; 1972be081cd6SPeter Brune DMSubDomainHookLink link,*p; 19735dbd56e3SPeter Brune 19745dbd56e3SPeter Brune PetscFunctionBegin; 19755dbd56e3SPeter Brune PetscValidHeaderSpecific(global,DM_CLASSID,1); 1976be081cd6SPeter Brune for (p=&global->subdomainhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */ 1977be081cd6SPeter Brune ierr = PetscMalloc(sizeof(struct _DMSubDomainHookLink),&link);CHKERRQ(ierr); 19785dbd56e3SPeter Brune link->restricthook = restricthook; 1979be081cd6SPeter Brune link->ddhook = ddhook; 19805dbd56e3SPeter Brune link->ctx = ctx; 19815dbd56e3SPeter Brune link->next = PETSC_NULL; 19825dbd56e3SPeter Brune *p = link; 19835dbd56e3SPeter Brune PetscFunctionReturn(0); 19845dbd56e3SPeter Brune } 19855dbd56e3SPeter Brune 19865dbd56e3SPeter Brune #undef __FUNCT__ 1987be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainRestrict" 19885dbd56e3SPeter Brune /*@ 1989be081cd6SPeter Brune DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd() 19905dbd56e3SPeter Brune 19915dbd56e3SPeter Brune Collective if any hooks are 19925dbd56e3SPeter Brune 19935dbd56e3SPeter Brune Input Arguments: 19945dbd56e3SPeter Brune + fine - finer DM to use as a base 1995be081cd6SPeter Brune . oscatter - scatter from domain global vector filling subdomain global vector with overlap 1996be081cd6SPeter Brune . gscatter - scatter from domain global vector filling subdomain local vector with ghosts 19975dbd56e3SPeter Brune - coarse - coarer DM to update 19985dbd56e3SPeter Brune 19995dbd56e3SPeter Brune Level: developer 20005dbd56e3SPeter Brune 20015dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict() 20025dbd56e3SPeter Brune @*/ 2003be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm) 20045dbd56e3SPeter Brune { 20055dbd56e3SPeter Brune PetscErrorCode ierr; 2006be081cd6SPeter Brune DMSubDomainHookLink link; 20075dbd56e3SPeter Brune 20085dbd56e3SPeter Brune PetscFunctionBegin; 2009be081cd6SPeter Brune for (link=global->subdomainhook; link; link=link->next) { 2010be081cd6SPeter Brune if (link->restricthook) {ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);} 20115dbd56e3SPeter Brune } 20125dbd56e3SPeter Brune PetscFunctionReturn(0); 20135dbd56e3SPeter Brune } 20145dbd56e3SPeter Brune 20155dbd56e3SPeter Brune #undef __FUNCT__ 20165fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel" 20175fe1f584SPeter Brune /*@ 20186a7d9d85SPeter Brune DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM. 20195fe1f584SPeter Brune 20205fe1f584SPeter Brune Not Collective 20215fe1f584SPeter Brune 20225fe1f584SPeter Brune Input Parameter: 20235fe1f584SPeter Brune . dm - the DM object 20245fe1f584SPeter Brune 20255fe1f584SPeter Brune Output Parameter: 20266a7d9d85SPeter Brune . level - number of coarsenings 20275fe1f584SPeter Brune 20285fe1f584SPeter Brune Level: developer 20295fe1f584SPeter Brune 20306a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation() 20315fe1f584SPeter Brune 20325fe1f584SPeter Brune @*/ 20335fe1f584SPeter Brune PetscErrorCode DMGetCoarsenLevel(DM dm,PetscInt *level) 20345fe1f584SPeter Brune { 20355fe1f584SPeter Brune PetscFunctionBegin; 20365fe1f584SPeter Brune PetscValidHeaderSpecific(dm,DM_CLASSID,1); 20375fe1f584SPeter Brune *level = dm->leveldown; 20385fe1f584SPeter Brune PetscFunctionReturn(0); 20395fe1f584SPeter Brune } 20405fe1f584SPeter Brune 20415fe1f584SPeter Brune 20425fe1f584SPeter Brune 20435fe1f584SPeter Brune #undef __FUNCT__ 204447c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy" 204547c6ae99SBarry Smith /*@C 204647c6ae99SBarry Smith DMRefineHierarchy - Refines a DM object, all levels at once 204747c6ae99SBarry Smith 204847c6ae99SBarry Smith Collective on DM 204947c6ae99SBarry Smith 205047c6ae99SBarry Smith Input Parameter: 205147c6ae99SBarry Smith + dm - the DM object 205247c6ae99SBarry Smith - nlevels - the number of levels of refinement 205347c6ae99SBarry Smith 205447c6ae99SBarry Smith Output Parameter: 205547c6ae99SBarry Smith . dmf - the refined DM hierarchy 205647c6ae99SBarry Smith 205747c6ae99SBarry Smith Level: developer 205847c6ae99SBarry Smith 2059e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation() 206047c6ae99SBarry Smith 206147c6ae99SBarry Smith @*/ 20627087cfbeSBarry Smith PetscErrorCode DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[]) 206347c6ae99SBarry Smith { 206447c6ae99SBarry Smith PetscErrorCode ierr; 206547c6ae99SBarry Smith 206647c6ae99SBarry Smith PetscFunctionBegin; 2067171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 206847c6ae99SBarry Smith if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative"); 206947c6ae99SBarry Smith if (nlevels == 0) PetscFunctionReturn(0); 207047c6ae99SBarry Smith if (dm->ops->refinehierarchy) { 207147c6ae99SBarry Smith ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr); 207247c6ae99SBarry Smith } else if (dm->ops->refine) { 207347c6ae99SBarry Smith PetscInt i; 207447c6ae99SBarry Smith 207547c6ae99SBarry Smith ierr = DMRefine(dm,((PetscObject)dm)->comm,&dmf[0]);CHKERRQ(ierr); 207647c6ae99SBarry Smith for (i=1; i<nlevels; i++) { 207747c6ae99SBarry Smith ierr = DMRefine(dmf[i-1],((PetscObject)dm)->comm,&dmf[i]);CHKERRQ(ierr); 207847c6ae99SBarry Smith } 20798cbdbec6SBarry Smith } else SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No RefineHierarchy for this DM yet"); 208047c6ae99SBarry Smith PetscFunctionReturn(0); 208147c6ae99SBarry Smith } 208247c6ae99SBarry Smith 208347c6ae99SBarry Smith #undef __FUNCT__ 208447c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy" 208547c6ae99SBarry Smith /*@C 208647c6ae99SBarry Smith DMCoarsenHierarchy - Coarsens a DM object, all levels at once 208747c6ae99SBarry Smith 208847c6ae99SBarry Smith Collective on DM 208947c6ae99SBarry Smith 209047c6ae99SBarry Smith Input Parameter: 209147c6ae99SBarry Smith + dm - the DM object 209247c6ae99SBarry Smith - nlevels - the number of levels of coarsening 209347c6ae99SBarry Smith 209447c6ae99SBarry Smith Output Parameter: 209547c6ae99SBarry Smith . dmc - the coarsened DM hierarchy 209647c6ae99SBarry Smith 209747c6ae99SBarry Smith Level: developer 209847c6ae99SBarry Smith 2099e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation() 210047c6ae99SBarry Smith 210147c6ae99SBarry Smith @*/ 21027087cfbeSBarry Smith PetscErrorCode DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[]) 210347c6ae99SBarry Smith { 210447c6ae99SBarry Smith PetscErrorCode ierr; 210547c6ae99SBarry Smith 210647c6ae99SBarry Smith PetscFunctionBegin; 2107171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 210847c6ae99SBarry Smith if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative"); 210947c6ae99SBarry Smith if (nlevels == 0) PetscFunctionReturn(0); 211047c6ae99SBarry Smith PetscValidPointer(dmc,3); 211147c6ae99SBarry Smith if (dm->ops->coarsenhierarchy) { 211247c6ae99SBarry Smith ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr); 211347c6ae99SBarry Smith } else if (dm->ops->coarsen) { 211447c6ae99SBarry Smith PetscInt i; 211547c6ae99SBarry Smith 211647c6ae99SBarry Smith ierr = DMCoarsen(dm,((PetscObject)dm)->comm,&dmc[0]);CHKERRQ(ierr); 211747c6ae99SBarry Smith for (i=1; i<nlevels; i++) { 211847c6ae99SBarry Smith ierr = DMCoarsen(dmc[i-1],((PetscObject)dm)->comm,&dmc[i]);CHKERRQ(ierr); 211947c6ae99SBarry Smith } 21208cbdbec6SBarry Smith } else SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet"); 212147c6ae99SBarry Smith PetscFunctionReturn(0); 212247c6ae99SBarry Smith } 212347c6ae99SBarry Smith 212447c6ae99SBarry Smith #undef __FUNCT__ 2125e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates" 212647c6ae99SBarry Smith /*@ 2127e727c939SJed Brown DMCreateAggregates - Gets the aggregates that map between 212847c6ae99SBarry Smith grids associated with two DMs. 212947c6ae99SBarry Smith 213047c6ae99SBarry Smith Collective on DM 213147c6ae99SBarry Smith 213247c6ae99SBarry Smith Input Parameters: 213347c6ae99SBarry Smith + dmc - the coarse grid DM 213447c6ae99SBarry Smith - dmf - the fine grid DM 213547c6ae99SBarry Smith 213647c6ae99SBarry Smith Output Parameters: 213747c6ae99SBarry Smith . rest - the restriction matrix (transpose of the projection matrix) 213847c6ae99SBarry Smith 213947c6ae99SBarry Smith Level: intermediate 214047c6ae99SBarry Smith 214147c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid 214247c6ae99SBarry Smith 2143e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation() 214447c6ae99SBarry Smith @*/ 2145e727c939SJed Brown PetscErrorCode DMCreateAggregates(DM dmc, DM dmf, Mat *rest) 214647c6ae99SBarry Smith { 214747c6ae99SBarry Smith PetscErrorCode ierr; 214847c6ae99SBarry Smith 214947c6ae99SBarry Smith PetscFunctionBegin; 2150171400e9SBarry Smith PetscValidHeaderSpecific(dmc,DM_CLASSID,1); 2151171400e9SBarry Smith PetscValidHeaderSpecific(dmf,DM_CLASSID,2); 215247c6ae99SBarry Smith ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr); 215347c6ae99SBarry Smith PetscFunctionReturn(0); 215447c6ae99SBarry Smith } 215547c6ae99SBarry Smith 215647c6ae99SBarry Smith #undef __FUNCT__ 21571a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy" 21581a266240SBarry Smith /*@C 21591a266240SBarry Smith DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed 21601a266240SBarry Smith 21611a266240SBarry Smith Not Collective 21621a266240SBarry Smith 21631a266240SBarry Smith Input Parameters: 21641a266240SBarry Smith + dm - the DM object 21651a266240SBarry Smith - destroy - the destroy function 21661a266240SBarry Smith 21671a266240SBarry Smith Level: intermediate 21681a266240SBarry Smith 2169e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext() 21701a266240SBarry Smith 2171f07f9ceaSJed Brown @*/ 21721a266240SBarry Smith PetscErrorCode DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**)) 21731a266240SBarry Smith { 21741a266240SBarry Smith PetscFunctionBegin; 2175171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 21761a266240SBarry Smith dm->ctxdestroy = destroy; 21771a266240SBarry Smith PetscFunctionReturn(0); 21781a266240SBarry Smith } 21791a266240SBarry Smith 21801a266240SBarry Smith #undef __FUNCT__ 21811b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext" 2182b07ff414SBarry Smith /*@ 21831b2093e4SBarry Smith DMSetApplicationContext - Set a user context into a DM object 218447c6ae99SBarry Smith 218547c6ae99SBarry Smith Not Collective 218647c6ae99SBarry Smith 218747c6ae99SBarry Smith Input Parameters: 218847c6ae99SBarry Smith + dm - the DM object 218947c6ae99SBarry Smith - ctx - the user context 219047c6ae99SBarry Smith 219147c6ae99SBarry Smith Level: intermediate 219247c6ae99SBarry Smith 2193e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext() 219447c6ae99SBarry Smith 219547c6ae99SBarry Smith @*/ 21961b2093e4SBarry Smith PetscErrorCode DMSetApplicationContext(DM dm,void *ctx) 219747c6ae99SBarry Smith { 219847c6ae99SBarry Smith PetscFunctionBegin; 2199171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 220047c6ae99SBarry Smith dm->ctx = ctx; 220147c6ae99SBarry Smith PetscFunctionReturn(0); 220247c6ae99SBarry Smith } 220347c6ae99SBarry Smith 220447c6ae99SBarry Smith #undef __FUNCT__ 22051b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext" 220647c6ae99SBarry Smith /*@ 22071b2093e4SBarry Smith DMGetApplicationContext - Gets a user context from a DM object 220847c6ae99SBarry Smith 220947c6ae99SBarry Smith Not Collective 221047c6ae99SBarry Smith 221147c6ae99SBarry Smith Input Parameter: 221247c6ae99SBarry Smith . dm - the DM object 221347c6ae99SBarry Smith 221447c6ae99SBarry Smith Output Parameter: 221547c6ae99SBarry Smith . ctx - the user context 221647c6ae99SBarry Smith 221747c6ae99SBarry Smith Level: intermediate 221847c6ae99SBarry Smith 2219e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext() 222047c6ae99SBarry Smith 222147c6ae99SBarry Smith @*/ 22221b2093e4SBarry Smith PetscErrorCode DMGetApplicationContext(DM dm,void *ctx) 222347c6ae99SBarry Smith { 222447c6ae99SBarry Smith PetscFunctionBegin; 2225171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 22261b2093e4SBarry Smith *(void**)ctx = dm->ctx; 222747c6ae99SBarry Smith PetscFunctionReturn(0); 222847c6ae99SBarry Smith } 222947c6ae99SBarry Smith 223047c6ae99SBarry Smith #undef __FUNCT__ 223108da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds" 223208da532bSDmitry Karpeev /*@C 223308da532bSDmitry Karpeev DMSetVariableBounds - sets a function to compute the the lower and upper bound vectors for SNESVI. 223408da532bSDmitry Karpeev 223508da532bSDmitry Karpeev Logically Collective on DM 223608da532bSDmitry Karpeev 223708da532bSDmitry Karpeev Input Parameter: 223808da532bSDmitry Karpeev + dm - the DM object 223908da532bSDmitry Karpeev - f - the function that computes variable bounds used by SNESVI (use PETSC_NULL to cancel a previous function that was set) 224008da532bSDmitry Karpeev 224108da532bSDmitry Karpeev Level: intermediate 224208da532bSDmitry Karpeev 2243835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), 224408da532bSDmitry Karpeev DMSetJacobian() 224508da532bSDmitry Karpeev 224608da532bSDmitry Karpeev @*/ 224708da532bSDmitry Karpeev PetscErrorCode DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec)) 224808da532bSDmitry Karpeev { 224908da532bSDmitry Karpeev PetscFunctionBegin; 225008da532bSDmitry Karpeev dm->ops->computevariablebounds = f; 225108da532bSDmitry Karpeev PetscFunctionReturn(0); 225208da532bSDmitry Karpeev } 225308da532bSDmitry Karpeev 225408da532bSDmitry Karpeev #undef __FUNCT__ 225508da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds" 225608da532bSDmitry Karpeev /*@ 225708da532bSDmitry Karpeev DMHasVariableBounds - does the DM object have a variable bounds function? 225808da532bSDmitry Karpeev 225908da532bSDmitry Karpeev Not Collective 226008da532bSDmitry Karpeev 226108da532bSDmitry Karpeev Input Parameter: 226208da532bSDmitry Karpeev . dm - the DM object to destroy 226308da532bSDmitry Karpeev 226408da532bSDmitry Karpeev Output Parameter: 226508da532bSDmitry Karpeev . flg - PETSC_TRUE if the variable bounds function exists 226608da532bSDmitry Karpeev 226708da532bSDmitry Karpeev Level: developer 226808da532bSDmitry Karpeev 226974e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext() 227008da532bSDmitry Karpeev 227108da532bSDmitry Karpeev @*/ 227208da532bSDmitry Karpeev PetscErrorCode DMHasVariableBounds(DM dm,PetscBool *flg) 227308da532bSDmitry Karpeev { 227408da532bSDmitry Karpeev PetscFunctionBegin; 227508da532bSDmitry Karpeev *flg = (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE; 227608da532bSDmitry Karpeev PetscFunctionReturn(0); 227708da532bSDmitry Karpeev } 227808da532bSDmitry Karpeev 227908da532bSDmitry Karpeev #undef __FUNCT__ 228008da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds" 228108da532bSDmitry Karpeev /*@C 228208da532bSDmitry Karpeev DMComputeVariableBounds - compute variable bounds used by SNESVI. 228308da532bSDmitry Karpeev 228408da532bSDmitry Karpeev Logically Collective on DM 228508da532bSDmitry Karpeev 228608da532bSDmitry Karpeev Input Parameters: 228708da532bSDmitry Karpeev + dm - the DM object to destroy 228808da532bSDmitry Karpeev - x - current solution at which the bounds are computed 228908da532bSDmitry Karpeev 229008da532bSDmitry Karpeev Output parameters: 229108da532bSDmitry Karpeev + xl - lower bound 229208da532bSDmitry Karpeev - xu - upper bound 229308da532bSDmitry Karpeev 229408da532bSDmitry Karpeev Level: intermediate 229508da532bSDmitry Karpeev 229674e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext() 229708da532bSDmitry Karpeev 229808da532bSDmitry Karpeev @*/ 229908da532bSDmitry Karpeev PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu) 230008da532bSDmitry Karpeev { 230108da532bSDmitry Karpeev PetscErrorCode ierr; 230208da532bSDmitry Karpeev PetscFunctionBegin; 230308da532bSDmitry Karpeev PetscValidHeaderSpecific(xl,VEC_CLASSID,2); 230408da532bSDmitry Karpeev PetscValidHeaderSpecific(xu,VEC_CLASSID,2); 230508da532bSDmitry Karpeev if (dm->ops->computevariablebounds) { 230608da532bSDmitry Karpeev ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr); 230708da532bSDmitry Karpeev } 2308a201590fSDmitry Karpeev else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds."); 230908da532bSDmitry Karpeev PetscFunctionReturn(0); 231008da532bSDmitry Karpeev } 231108da532bSDmitry Karpeev 231208da532bSDmitry Karpeev #undef __FUNCT__ 2313b0ae01b7SPeter Brune #define __FUNCT__ "DMHasColoring" 2314b0ae01b7SPeter Brune /*@ 2315b0ae01b7SPeter Brune DMHasColoring - does the DM object have a method of providing a coloring? 2316b0ae01b7SPeter Brune 2317b0ae01b7SPeter Brune Not Collective 2318b0ae01b7SPeter Brune 2319b0ae01b7SPeter Brune Input Parameter: 2320b0ae01b7SPeter Brune . dm - the DM object 2321b0ae01b7SPeter Brune 2322b0ae01b7SPeter Brune Output Parameter: 2323b0ae01b7SPeter Brune . flg - PETSC_TRUE if the DM has facilities for DMCreateColoring(). 2324b0ae01b7SPeter Brune 2325b0ae01b7SPeter Brune Level: developer 2326b0ae01b7SPeter Brune 2327b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring() 2328b0ae01b7SPeter Brune 2329b0ae01b7SPeter Brune @*/ 2330b0ae01b7SPeter Brune PetscErrorCode DMHasColoring(DM dm,PetscBool *flg) 2331b0ae01b7SPeter Brune { 2332b0ae01b7SPeter Brune PetscFunctionBegin; 2333b0ae01b7SPeter Brune *flg = (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE; 2334b0ae01b7SPeter Brune PetscFunctionReturn(0); 2335b0ae01b7SPeter Brune } 2336b0ae01b7SPeter Brune 2337b0ae01b7SPeter Brune #undef __FUNCT__ 233808da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec" 2339748fac09SDmitry Karpeev /*@C 234008da532bSDmitry Karpeev DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear. 234108da532bSDmitry Karpeev 234208da532bSDmitry Karpeev Collective on DM 234308da532bSDmitry Karpeev 234408da532bSDmitry Karpeev Input Parameter: 234508da532bSDmitry Karpeev + dm - the DM object 2346e88d7f4bSDmitry Karpeev - x - location to compute residual and Jacobian, if PETSC_NULL is passed to those routines; will be PETSC_NULL for linear problems. 234708da532bSDmitry Karpeev 234808da532bSDmitry Karpeev Level: developer 234908da532bSDmitry Karpeev 235074e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext() 235108da532bSDmitry Karpeev 235208da532bSDmitry Karpeev @*/ 235308da532bSDmitry Karpeev PetscErrorCode DMSetVec(DM dm,Vec x) 235408da532bSDmitry Karpeev { 235508da532bSDmitry Karpeev PetscErrorCode ierr; 235608da532bSDmitry Karpeev PetscFunctionBegin; 235708da532bSDmitry Karpeev if (x) { 235808da532bSDmitry Karpeev if (!dm->x) { 235908da532bSDmitry Karpeev ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr); 236008da532bSDmitry Karpeev } 236108da532bSDmitry Karpeev ierr = VecCopy(x,dm->x);CHKERRQ(ierr); 236208da532bSDmitry Karpeev } 236308da532bSDmitry Karpeev else if (dm->x) { 236408da532bSDmitry Karpeev ierr = VecDestroy(&dm->x);CHKERRQ(ierr); 236508da532bSDmitry Karpeev } 236608da532bSDmitry Karpeev PetscFunctionReturn(0); 236708da532bSDmitry Karpeev } 236808da532bSDmitry Karpeev 2369140e18c1SBarry Smith PetscFunctionList DMList = PETSC_NULL; 2370264ace61SBarry Smith PetscBool DMRegisterAllCalled = PETSC_FALSE; 2371264ace61SBarry Smith 2372264ace61SBarry Smith #undef __FUNCT__ 2373264ace61SBarry Smith #define __FUNCT__ "DMSetType" 2374264ace61SBarry Smith /*@C 2375264ace61SBarry Smith DMSetType - Builds a DM, for a particular DM implementation. 2376264ace61SBarry Smith 2377264ace61SBarry Smith Collective on DM 2378264ace61SBarry Smith 2379264ace61SBarry Smith Input Parameters: 2380264ace61SBarry Smith + dm - The DM object 2381264ace61SBarry Smith - method - The name of the DM type 2382264ace61SBarry Smith 2383264ace61SBarry Smith Options Database Key: 2384264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types 2385264ace61SBarry Smith 2386264ace61SBarry Smith Notes: 2387e1589f56SBarry Smith See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D). 2388264ace61SBarry Smith 2389264ace61SBarry Smith Level: intermediate 2390264ace61SBarry Smith 2391264ace61SBarry Smith .keywords: DM, set, type 2392264ace61SBarry Smith .seealso: DMGetType(), DMCreate() 2393264ace61SBarry Smith @*/ 239419fd82e9SBarry Smith PetscErrorCode DMSetType(DM dm, DMType method) 2395264ace61SBarry Smith { 2396264ace61SBarry Smith PetscErrorCode (*r)(DM); 2397264ace61SBarry Smith PetscBool match; 2398264ace61SBarry Smith PetscErrorCode ierr; 2399264ace61SBarry Smith 2400264ace61SBarry Smith PetscFunctionBegin; 2401264ace61SBarry Smith PetscValidHeaderSpecific(dm, DM_CLASSID,1); 2402251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr); 2403264ace61SBarry Smith if (match) PetscFunctionReturn(0); 2404264ace61SBarry Smith 2405264ace61SBarry Smith if (!DMRegisterAllCalled) {ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 2406140e18c1SBarry Smith ierr = PetscFunctionListFind(((PetscObject)dm)->comm, DMList, method,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr); 240732c0f0efSBarry Smith if (!r) SETERRQ1(((PetscObject)dm)->comm,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method); 2408264ace61SBarry Smith 2409264ace61SBarry Smith if (dm->ops->destroy) { 2410264ace61SBarry Smith ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr); 2411b5c23020SJed Brown dm->ops->destroy = PETSC_NULL; 2412264ace61SBarry Smith } 2413264ace61SBarry Smith ierr = (*r)(dm);CHKERRQ(ierr); 2414264ace61SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr); 2415264ace61SBarry Smith PetscFunctionReturn(0); 2416264ace61SBarry Smith } 2417264ace61SBarry Smith 2418264ace61SBarry Smith #undef __FUNCT__ 2419264ace61SBarry Smith #define __FUNCT__ "DMGetType" 2420264ace61SBarry Smith /*@C 2421264ace61SBarry Smith DMGetType - Gets the DM type name (as a string) from the DM. 2422264ace61SBarry Smith 2423264ace61SBarry Smith Not Collective 2424264ace61SBarry Smith 2425264ace61SBarry Smith Input Parameter: 2426264ace61SBarry Smith . dm - The DM 2427264ace61SBarry Smith 2428264ace61SBarry Smith Output Parameter: 2429264ace61SBarry Smith . type - The DM type name 2430264ace61SBarry Smith 2431264ace61SBarry Smith Level: intermediate 2432264ace61SBarry Smith 2433264ace61SBarry Smith .keywords: DM, get, type, name 2434264ace61SBarry Smith .seealso: DMSetType(), DMCreate() 2435264ace61SBarry Smith @*/ 243619fd82e9SBarry Smith PetscErrorCode DMGetType(DM dm, DMType *type) 2437264ace61SBarry Smith { 2438264ace61SBarry Smith PetscErrorCode ierr; 2439264ace61SBarry Smith 2440264ace61SBarry Smith PetscFunctionBegin; 2441264ace61SBarry Smith PetscValidHeaderSpecific(dm, DM_CLASSID,1); 2442264ace61SBarry Smith PetscValidCharPointer(type,2); 2443264ace61SBarry Smith if (!DMRegisterAllCalled) { 2444264ace61SBarry Smith ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr); 2445264ace61SBarry Smith } 2446264ace61SBarry Smith *type = ((PetscObject)dm)->type_name; 2447264ace61SBarry Smith PetscFunctionReturn(0); 2448264ace61SBarry Smith } 2449264ace61SBarry Smith 245067a56275SMatthew G Knepley #undef __FUNCT__ 245167a56275SMatthew G Knepley #define __FUNCT__ "DMConvert" 245267a56275SMatthew G Knepley /*@C 245367a56275SMatthew G Knepley DMConvert - Converts a DM to another DM, either of the same or different type. 245467a56275SMatthew G Knepley 245567a56275SMatthew G Knepley Collective on DM 245667a56275SMatthew G Knepley 245767a56275SMatthew G Knepley Input Parameters: 245867a56275SMatthew G Knepley + dm - the DM 245967a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type) 246067a56275SMatthew G Knepley 246167a56275SMatthew G Knepley Output Parameter: 246267a56275SMatthew G Knepley . M - pointer to new DM 246367a56275SMatthew G Knepley 246467a56275SMatthew G Knepley Notes: 246567a56275SMatthew G Knepley Cannot be used to convert a sequential DM to parallel or parallel to sequential, 246667a56275SMatthew G Knepley the MPI communicator of the generated DM is always the same as the communicator 246767a56275SMatthew G Knepley of the input DM. 246867a56275SMatthew G Knepley 246967a56275SMatthew G Knepley Level: intermediate 247067a56275SMatthew G Knepley 247167a56275SMatthew G Knepley .seealso: DMCreate() 247267a56275SMatthew G Knepley @*/ 247319fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M) 247467a56275SMatthew G Knepley { 247567a56275SMatthew G Knepley DM B; 247667a56275SMatthew G Knepley char convname[256]; 247767a56275SMatthew G Knepley PetscBool sametype, issame; 247867a56275SMatthew G Knepley PetscErrorCode ierr; 247967a56275SMatthew G Knepley 248067a56275SMatthew G Knepley PetscFunctionBegin; 248167a56275SMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 248267a56275SMatthew G Knepley PetscValidType(dm,1); 248367a56275SMatthew G Knepley PetscValidPointer(M,3); 2484251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr); 248567a56275SMatthew G Knepley ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); 248667a56275SMatthew G Knepley { 248719fd82e9SBarry Smith PetscErrorCode (*conv)(DM, DMType, DM *) = PETSC_NULL; 248867a56275SMatthew G Knepley 248967a56275SMatthew G Knepley /* 249067a56275SMatthew G Knepley Order of precedence: 249167a56275SMatthew G Knepley 1) See if a specialized converter is known to the current DM. 249267a56275SMatthew G Knepley 2) See if a specialized converter is known to the desired DM class. 249367a56275SMatthew G Knepley 3) See if a good general converter is registered for the desired class 249467a56275SMatthew G Knepley 4) See if a good general converter is known for the current matrix. 249567a56275SMatthew G Knepley 5) Use a really basic converter. 249667a56275SMatthew G Knepley */ 249767a56275SMatthew G Knepley 249867a56275SMatthew G Knepley /* 1) See if a specialized converter is known to the current DM and the desired class */ 249967a56275SMatthew G Knepley ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr); 250067a56275SMatthew G Knepley ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr); 250167a56275SMatthew G Knepley ierr = PetscStrcat(convname,"_");CHKERRQ(ierr); 250267a56275SMatthew G Knepley ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr); 250367a56275SMatthew G Knepley ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr); 250467a56275SMatthew G Knepley ierr = PetscObjectQueryFunction((PetscObject)dm,convname,(void (**)(void))&conv);CHKERRQ(ierr); 250567a56275SMatthew G Knepley if (conv) goto foundconv; 250667a56275SMatthew G Knepley 250767a56275SMatthew G Knepley /* 2) See if a specialized converter is known to the desired DM class. */ 250867a56275SMatthew G Knepley ierr = DMCreate(((PetscObject) dm)->comm, &B);CHKERRQ(ierr); 250967a56275SMatthew G Knepley ierr = DMSetType(B, newtype);CHKERRQ(ierr); 251067a56275SMatthew G Knepley ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr); 251167a56275SMatthew G Knepley ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr); 251267a56275SMatthew G Knepley ierr = PetscStrcat(convname,"_");CHKERRQ(ierr); 251367a56275SMatthew G Knepley ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr); 251467a56275SMatthew G Knepley ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr); 251567a56275SMatthew G Knepley ierr = PetscObjectQueryFunction((PetscObject)B,convname,(void (**)(void))&conv);CHKERRQ(ierr); 251667a56275SMatthew G Knepley if (conv) { 2517fcfd50ebSBarry Smith ierr = DMDestroy(&B);CHKERRQ(ierr); 251867a56275SMatthew G Knepley goto foundconv; 251967a56275SMatthew G Knepley } 252067a56275SMatthew G Knepley 252167a56275SMatthew G Knepley #if 0 252267a56275SMatthew G Knepley /* 3) See if a good general converter is registered for the desired class */ 252367a56275SMatthew G Knepley conv = B->ops->convertfrom; 2524fcfd50ebSBarry Smith ierr = DMDestroy(&B);CHKERRQ(ierr); 252567a56275SMatthew G Knepley if (conv) goto foundconv; 252667a56275SMatthew G Knepley 252767a56275SMatthew G Knepley /* 4) See if a good general converter is known for the current matrix */ 252867a56275SMatthew G Knepley if (dm->ops->convert) { 252967a56275SMatthew G Knepley conv = dm->ops->convert; 253067a56275SMatthew G Knepley } 253167a56275SMatthew G Knepley if (conv) goto foundconv; 253267a56275SMatthew G Knepley #endif 253367a56275SMatthew G Knepley 253467a56275SMatthew G Knepley /* 5) Use a really basic converter. */ 253567a56275SMatthew G Knepley SETERRQ2(((PetscObject) dm)->comm, PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype); 253667a56275SMatthew G Knepley 253767a56275SMatthew G Knepley foundconv: 253867a56275SMatthew G Knepley ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr); 253967a56275SMatthew G Knepley ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr); 254067a56275SMatthew G Knepley ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr); 254167a56275SMatthew G Knepley } 254267a56275SMatthew G Knepley ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr); 254367a56275SMatthew G Knepley PetscFunctionReturn(0); 254467a56275SMatthew G Knepley } 2545264ace61SBarry Smith 2546264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/ 2547264ace61SBarry Smith 2548264ace61SBarry Smith #undef __FUNCT__ 2549264ace61SBarry Smith #define __FUNCT__ "DMRegister" 2550264ace61SBarry Smith /*@C 2551264ace61SBarry Smith DMRegister - See DMRegisterDynamic() 2552264ace61SBarry Smith 2553264ace61SBarry Smith Level: advanced 2554264ace61SBarry Smith @*/ 25557087cfbeSBarry Smith PetscErrorCode DMRegister(const char sname[], const char path[], const char name[], PetscErrorCode (*function)(DM)) 2556264ace61SBarry Smith { 2557264ace61SBarry Smith char fullname[PETSC_MAX_PATH_LEN]; 2558264ace61SBarry Smith PetscErrorCode ierr; 2559264ace61SBarry Smith 2560264ace61SBarry Smith PetscFunctionBegin; 2561264ace61SBarry Smith ierr = PetscStrcpy(fullname, path);CHKERRQ(ierr); 2562264ace61SBarry Smith ierr = PetscStrcat(fullname, ":");CHKERRQ(ierr); 2563264ace61SBarry Smith ierr = PetscStrcat(fullname, name);CHKERRQ(ierr); 2564140e18c1SBarry Smith ierr = PetscFunctionListAdd(PETSC_COMM_WORLD,&DMList, sname, fullname, (void (*)(void)) function);CHKERRQ(ierr); 2565264ace61SBarry Smith PetscFunctionReturn(0); 2566264ace61SBarry Smith } 2567264ace61SBarry Smith 2568264ace61SBarry Smith 2569264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/ 2570264ace61SBarry Smith #undef __FUNCT__ 2571264ace61SBarry Smith #define __FUNCT__ "DMRegisterDestroy" 2572264ace61SBarry Smith /*@C 2573264ace61SBarry Smith DMRegisterDestroy - Frees the list of DM methods that were registered by DMRegister()/DMRegisterDynamic(). 2574264ace61SBarry Smith 2575264ace61SBarry Smith Not Collective 2576264ace61SBarry Smith 2577264ace61SBarry Smith Level: advanced 2578264ace61SBarry Smith 2579264ace61SBarry Smith .keywords: DM, register, destroy 2580264ace61SBarry Smith .seealso: DMRegister(), DMRegisterAll(), DMRegisterDynamic() 2581264ace61SBarry Smith @*/ 25827087cfbeSBarry Smith PetscErrorCode DMRegisterDestroy(void) 2583264ace61SBarry Smith { 2584264ace61SBarry Smith PetscErrorCode ierr; 2585264ace61SBarry Smith 2586264ace61SBarry Smith PetscFunctionBegin; 2587140e18c1SBarry Smith ierr = PetscFunctionListDestroy(&DMList);CHKERRQ(ierr); 2588264ace61SBarry Smith DMRegisterAllCalled = PETSC_FALSE; 2589264ace61SBarry Smith PetscFunctionReturn(0); 2590264ace61SBarry Smith } 259123f975d1SBarry Smith 2592b859378eSBarry Smith #undef __FUNCT__ 2593b859378eSBarry Smith #define __FUNCT__ "DMLoad" 2594b859378eSBarry Smith /*@C 259555849f57SBarry Smith DMLoad - Loads a DM that has been stored in binary with DMView(). 2596b859378eSBarry Smith 2597b859378eSBarry Smith Collective on PetscViewer 2598b859378eSBarry Smith 2599b859378eSBarry Smith Input Parameters: 2600b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or 2601b859378eSBarry Smith some related function before a call to DMLoad(). 2602b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or 2603b859378eSBarry Smith HDF5 file viewer, obtained from PetscViewerHDF5Open() 2604b859378eSBarry Smith 2605b859378eSBarry Smith Level: intermediate 2606b859378eSBarry Smith 2607b859378eSBarry Smith Notes: 260855849f57SBarry Smith The type is determined by the data in the file, any type set into the DM before this call is ignored. 2609b859378eSBarry Smith 2610b859378eSBarry Smith Notes for advanced users: 2611b859378eSBarry Smith Most users should not need to know the details of the binary storage 2612b859378eSBarry Smith format, since DMLoad() and DMView() completely hide these details. 2613b859378eSBarry Smith But for anyone who's interested, the standard binary matrix storage 2614b859378eSBarry Smith format is 2615b859378eSBarry Smith .vb 2616b859378eSBarry Smith has not yet been determined 2617b859378eSBarry Smith .ve 2618b859378eSBarry Smith 2619b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad() 2620b859378eSBarry Smith @*/ 2621b859378eSBarry Smith PetscErrorCode DMLoad(DM newdm, PetscViewer viewer) 2622b859378eSBarry Smith { 2623b859378eSBarry Smith PetscErrorCode ierr; 262432c0f0efSBarry Smith PetscBool isbinary; 262555849f57SBarry Smith PetscInt classid; 262632c0f0efSBarry Smith char type[256]; 2627b859378eSBarry Smith 2628b859378eSBarry Smith PetscFunctionBegin; 2629b859378eSBarry Smith PetscValidHeaderSpecific(newdm,DM_CLASSID,1); 2630b859378eSBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 263132c0f0efSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 263232c0f0efSBarry Smith if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()"); 2633b859378eSBarry Smith 263432c0f0efSBarry Smith ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr); 263532c0f0efSBarry Smith if (classid != DM_FILE_CLASSID) SETERRQ(((PetscObject)newdm)->comm,PETSC_ERR_ARG_WRONG,"Not DM next in file"); 263632c0f0efSBarry Smith ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr); 263732c0f0efSBarry Smith ierr = DMSetType(newdm, type);CHKERRQ(ierr); 26382d53ad75SBarry Smith if (newdm->ops->load) { 2639b859378eSBarry Smith ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr); 26402d53ad75SBarry Smith } 2641b859378eSBarry Smith PetscFunctionReturn(0); 2642b859378eSBarry Smith } 2643b859378eSBarry Smith 26447da65231SMatthew G Knepley /******************************** FEM Support **********************************/ 26457da65231SMatthew G Knepley 26467da65231SMatthew G Knepley #undef __FUNCT__ 26477da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector" 2648*a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[]) 2649*a6dfd86eSKarl Rupp { 26501d47ebbbSSatish Balay PetscInt f; 26511b30c384SMatthew G Knepley PetscErrorCode ierr; 26521b30c384SMatthew G Knepley 26537da65231SMatthew G Knepley PetscFunctionBegin; 265474778d6cSJed Brown ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr); 26551d47ebbbSSatish Balay for (f = 0; f < len; ++f) { 265674778d6cSJed Brown ierr = PetscPrintf(PETSC_COMM_SELF, " | %G |\n", PetscRealPart(x[f]));CHKERRQ(ierr); 26577da65231SMatthew G Knepley } 26587da65231SMatthew G Knepley PetscFunctionReturn(0); 26597da65231SMatthew G Knepley } 26607da65231SMatthew G Knepley 26617da65231SMatthew G Knepley #undef __FUNCT__ 26627da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix" 2663*a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[]) 2664*a6dfd86eSKarl Rupp { 26651b30c384SMatthew G Knepley PetscInt f, g; 26667da65231SMatthew G Knepley PetscErrorCode ierr; 26677da65231SMatthew G Knepley 26687da65231SMatthew G Knepley PetscFunctionBegin; 266974778d6cSJed Brown ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr); 26701d47ebbbSSatish Balay for (f = 0; f < rows; ++f) { 267174778d6cSJed Brown ierr = PetscPrintf(PETSC_COMM_SELF, " |");CHKERRQ(ierr); 26721d47ebbbSSatish Balay for (g = 0; g < cols; ++g) { 267374778d6cSJed Brown ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5G", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr); 26747da65231SMatthew G Knepley } 267574778d6cSJed Brown ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr); 26767da65231SMatthew G Knepley } 26777da65231SMatthew G Knepley PetscFunctionReturn(0); 26787da65231SMatthew G Knepley } 2679e7c4fc90SDmitry Karpeev 2680970e74d5SMatthew G Knepley #undef __FUNCT__ 268188ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection" 268288ed4aceSMatthew G Knepley /*@ 268388ed4aceSMatthew G Knepley DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM. 268488ed4aceSMatthew G Knepley 268588ed4aceSMatthew G Knepley Input Parameter: 268688ed4aceSMatthew G Knepley . dm - The DM 268788ed4aceSMatthew G Knepley 268888ed4aceSMatthew G Knepley Output Parameter: 268988ed4aceSMatthew G Knepley . section - The PetscSection 269088ed4aceSMatthew G Knepley 269188ed4aceSMatthew G Knepley Level: intermediate 269288ed4aceSMatthew G Knepley 269388ed4aceSMatthew G Knepley Note: This gets a borrowed reference, so the user should not destroy this PetscSection. 269488ed4aceSMatthew G Knepley 269588ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection() 269688ed4aceSMatthew G Knepley @*/ 26970adebc6cSBarry Smith PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section) 26980adebc6cSBarry Smith { 269988ed4aceSMatthew G Knepley PetscFunctionBegin; 270088ed4aceSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 270188ed4aceSMatthew G Knepley PetscValidPointer(section, 2); 270288ed4aceSMatthew G Knepley *section = dm->defaultSection; 270388ed4aceSMatthew G Knepley PetscFunctionReturn(0); 270488ed4aceSMatthew G Knepley } 270588ed4aceSMatthew G Knepley 270688ed4aceSMatthew G Knepley #undef __FUNCT__ 270788ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection" 270888ed4aceSMatthew G Knepley /*@ 270988ed4aceSMatthew G Knepley DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM. 271088ed4aceSMatthew G Knepley 271188ed4aceSMatthew G Knepley Input Parameters: 271288ed4aceSMatthew G Knepley + dm - The DM 271388ed4aceSMatthew G Knepley - section - The PetscSection 271488ed4aceSMatthew G Knepley 271588ed4aceSMatthew G Knepley Level: intermediate 271688ed4aceSMatthew G Knepley 271788ed4aceSMatthew G Knepley Note: Any existing Section will be destroyed 271888ed4aceSMatthew G Knepley 271988ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection() 272088ed4aceSMatthew G Knepley @*/ 27210adebc6cSBarry Smith PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section) 27220adebc6cSBarry Smith { 2723af122d2aSMatthew G Knepley PetscInt numFields; 2724af122d2aSMatthew G Knepley PetscInt f; 272588ed4aceSMatthew G Knepley PetscErrorCode ierr; 272688ed4aceSMatthew G Knepley 272788ed4aceSMatthew G Knepley PetscFunctionBegin; 272888ed4aceSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 272988ed4aceSMatthew G Knepley ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr); 273088ed4aceSMatthew G Knepley ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr); 273188ed4aceSMatthew G Knepley dm->defaultSection = section; 2732af122d2aSMatthew G Knepley ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr); 2733af122d2aSMatthew G Knepley if (numFields) { 2734af122d2aSMatthew G Knepley ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr); 2735af122d2aSMatthew G Knepley for (f = 0; f < numFields; ++f) { 2736af122d2aSMatthew G Knepley const char *name; 2737af122d2aSMatthew G Knepley 2738af122d2aSMatthew G Knepley ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr); 2739af122d2aSMatthew G Knepley ierr = PetscObjectSetName(dm->fields[f], name);CHKERRQ(ierr); 2740af122d2aSMatthew G Knepley } 2741af122d2aSMatthew G Knepley } 274288ed4aceSMatthew G Knepley PetscFunctionReturn(0); 274388ed4aceSMatthew G Knepley } 274488ed4aceSMatthew G Knepley 274588ed4aceSMatthew G Knepley #undef __FUNCT__ 274688ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection" 274788ed4aceSMatthew G Knepley /*@ 274888ed4aceSMatthew G Knepley DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM. 274988ed4aceSMatthew G Knepley 27508b1ab98fSJed Brown Collective on DM 27518b1ab98fSJed Brown 275288ed4aceSMatthew G Knepley Input Parameter: 275388ed4aceSMatthew G Knepley . dm - The DM 275488ed4aceSMatthew G Knepley 275588ed4aceSMatthew G Knepley Output Parameter: 275688ed4aceSMatthew G Knepley . section - The PetscSection 275788ed4aceSMatthew G Knepley 275888ed4aceSMatthew G Knepley Level: intermediate 275988ed4aceSMatthew G Knepley 276088ed4aceSMatthew G Knepley Note: This gets a borrowed reference, so the user should not destroy this PetscSection. 276188ed4aceSMatthew G Knepley 276288ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection() 276388ed4aceSMatthew G Knepley @*/ 27640adebc6cSBarry Smith PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section) 27650adebc6cSBarry Smith { 276688ed4aceSMatthew G Knepley PetscErrorCode ierr; 276788ed4aceSMatthew G Knepley 276888ed4aceSMatthew G Knepley PetscFunctionBegin; 276988ed4aceSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 277088ed4aceSMatthew G Knepley PetscValidPointer(section, 2); 277188ed4aceSMatthew G Knepley if (!dm->defaultGlobalSection) { 277240e8a239SMatthew G Knepley if (!dm->defaultSection || !dm->sf) SETERRQ(((PetscObject) dm)->comm, PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection and PetscSF in order to create a global PetscSection"); 2773b21d0597SMatthew G Knepley ierr = PetscSectionCreateGlobalSection(dm->defaultSection, dm->sf, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr); 27748b1ab98fSJed Brown ierr = PetscSectionGetValueLayout(((PetscObject)dm)->comm,dm->defaultGlobalSection,&dm->map);CHKERRQ(ierr); 277588ed4aceSMatthew G Knepley } 277688ed4aceSMatthew G Knepley *section = dm->defaultGlobalSection; 277788ed4aceSMatthew G Knepley PetscFunctionReturn(0); 277888ed4aceSMatthew G Knepley } 277988ed4aceSMatthew G Knepley 278088ed4aceSMatthew G Knepley #undef __FUNCT__ 2781b21d0597SMatthew G Knepley #define __FUNCT__ "DMSetDefaultGlobalSection" 2782b21d0597SMatthew G Knepley /*@ 2783b21d0597SMatthew G Knepley DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM. 2784b21d0597SMatthew G Knepley 2785b21d0597SMatthew G Knepley Input Parameters: 2786b21d0597SMatthew G Knepley + dm - The DM 2787b21d0597SMatthew G Knepley - section - The PetscSection 2788b21d0597SMatthew G Knepley 2789b21d0597SMatthew G Knepley Level: intermediate 2790b21d0597SMatthew G Knepley 2791b21d0597SMatthew G Knepley Note: Any existing Section will be destroyed 2792b21d0597SMatthew G Knepley 2793b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection() 2794b21d0597SMatthew G Knepley @*/ 27950adebc6cSBarry Smith PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section) 27960adebc6cSBarry Smith { 2797b21d0597SMatthew G Knepley PetscErrorCode ierr; 2798b21d0597SMatthew G Knepley 2799b21d0597SMatthew G Knepley PetscFunctionBegin; 2800b21d0597SMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 2801b21d0597SMatthew G Knepley ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr); 2802b21d0597SMatthew G Knepley dm->defaultGlobalSection = section; 2803b21d0597SMatthew G Knepley PetscFunctionReturn(0); 2804b21d0597SMatthew G Knepley } 2805b21d0597SMatthew G Knepley 2806b21d0597SMatthew G Knepley #undef __FUNCT__ 280788ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF" 280888ed4aceSMatthew G Knepley /*@ 280988ed4aceSMatthew G Knepley DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set, 281088ed4aceSMatthew G Knepley it is created from the default PetscSection layouts in the DM. 281188ed4aceSMatthew G Knepley 281288ed4aceSMatthew G Knepley Input Parameter: 281388ed4aceSMatthew G Knepley . dm - The DM 281488ed4aceSMatthew G Knepley 281588ed4aceSMatthew G Knepley Output Parameter: 281688ed4aceSMatthew G Knepley . sf - The PetscSF 281788ed4aceSMatthew G Knepley 281888ed4aceSMatthew G Knepley Level: intermediate 281988ed4aceSMatthew G Knepley 282088ed4aceSMatthew G Knepley Note: This gets a borrowed reference, so the user should not destroy this PetscSF. 282188ed4aceSMatthew G Knepley 282288ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF() 282388ed4aceSMatthew G Knepley @*/ 28240adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf) 28250adebc6cSBarry Smith { 282688ed4aceSMatthew G Knepley PetscInt nroots; 282788ed4aceSMatthew G Knepley PetscErrorCode ierr; 282888ed4aceSMatthew G Knepley 282988ed4aceSMatthew G Knepley PetscFunctionBegin; 283088ed4aceSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 283188ed4aceSMatthew G Knepley PetscValidPointer(sf, 2); 283288ed4aceSMatthew G Knepley ierr = PetscSFGetGraph(dm->defaultSF, &nroots, PETSC_NULL, PETSC_NULL, PETSC_NULL);CHKERRQ(ierr); 283388ed4aceSMatthew G Knepley if (nroots < 0) { 283488ed4aceSMatthew G Knepley PetscSection section, gSection; 283588ed4aceSMatthew G Knepley 283688ed4aceSMatthew G Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 283731ea6d37SMatthew G Knepley if (section) { 283888ed4aceSMatthew G Knepley ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr); 283988ed4aceSMatthew G Knepley ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr); 284031ea6d37SMatthew G Knepley } else { 284131ea6d37SMatthew G Knepley *sf = PETSC_NULL; 284231ea6d37SMatthew G Knepley PetscFunctionReturn(0); 284331ea6d37SMatthew G Knepley } 284488ed4aceSMatthew G Knepley } 284588ed4aceSMatthew G Knepley *sf = dm->defaultSF; 284688ed4aceSMatthew G Knepley PetscFunctionReturn(0); 284788ed4aceSMatthew G Knepley } 284888ed4aceSMatthew G Knepley 284988ed4aceSMatthew G Knepley #undef __FUNCT__ 285088ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF" 285188ed4aceSMatthew G Knepley /*@ 285288ed4aceSMatthew G Knepley DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM 285388ed4aceSMatthew G Knepley 285488ed4aceSMatthew G Knepley Input Parameters: 285588ed4aceSMatthew G Knepley + dm - The DM 285688ed4aceSMatthew G Knepley - sf - The PetscSF 285788ed4aceSMatthew G Knepley 285888ed4aceSMatthew G Knepley Level: intermediate 285988ed4aceSMatthew G Knepley 286088ed4aceSMatthew G Knepley Note: Any previous SF is destroyed 286188ed4aceSMatthew G Knepley 286288ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF() 286388ed4aceSMatthew G Knepley @*/ 28640adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf) 28650adebc6cSBarry Smith { 286688ed4aceSMatthew G Knepley PetscErrorCode ierr; 286788ed4aceSMatthew G Knepley 286888ed4aceSMatthew G Knepley PetscFunctionBegin; 286988ed4aceSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 287088ed4aceSMatthew G Knepley PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2); 287188ed4aceSMatthew G Knepley ierr = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr); 287288ed4aceSMatthew G Knepley dm->defaultSF = sf; 287388ed4aceSMatthew G Knepley PetscFunctionReturn(0); 287488ed4aceSMatthew G Knepley } 287588ed4aceSMatthew G Knepley 287688ed4aceSMatthew G Knepley #undef __FUNCT__ 287788ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF" 287888ed4aceSMatthew G Knepley /*@C 287988ed4aceSMatthew G Knepley DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections 288088ed4aceSMatthew G Knepley describing the data layout. 288188ed4aceSMatthew G Knepley 288288ed4aceSMatthew G Knepley Input Parameters: 288388ed4aceSMatthew G Knepley + dm - The DM 288488ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout 288588ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout 288688ed4aceSMatthew G Knepley 288788ed4aceSMatthew G Knepley Level: intermediate 288888ed4aceSMatthew G Knepley 288988ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF() 289088ed4aceSMatthew G Knepley @*/ 289188ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection) 289288ed4aceSMatthew G Knepley { 289388ed4aceSMatthew G Knepley MPI_Comm comm = ((PetscObject) dm)->comm; 289488ed4aceSMatthew G Knepley PetscLayout layout; 289588ed4aceSMatthew G Knepley const PetscInt *ranges; 289688ed4aceSMatthew G Knepley PetscInt *local; 289788ed4aceSMatthew G Knepley PetscSFNode *remote; 289888ed4aceSMatthew G Knepley PetscInt pStart, pEnd, p, nroots, nleaves, l; 289988ed4aceSMatthew G Knepley PetscMPIInt size, rank; 290088ed4aceSMatthew G Knepley PetscErrorCode ierr; 290188ed4aceSMatthew G Knepley 290288ed4aceSMatthew G Knepley PetscFunctionBegin; 290388ed4aceSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 290488ed4aceSMatthew G Knepley ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr); 290588ed4aceSMatthew G Knepley ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr); 290688ed4aceSMatthew G Knepley ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr); 290788ed4aceSMatthew G Knepley ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr); 290888ed4aceSMatthew G Knepley ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr); 290988ed4aceSMatthew G Knepley ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr); 291088ed4aceSMatthew G Knepley ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr); 291188ed4aceSMatthew G Knepley ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr); 291288ed4aceSMatthew G Knepley ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr); 291388ed4aceSMatthew G Knepley for (p = pStart, nleaves = 0; p < pEnd; ++p) { 29146636e97aSMatthew G Knepley PetscInt gdof, gcdof; 291588ed4aceSMatthew G Knepley 29166636e97aSMatthew G Knepley ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr); 29176636e97aSMatthew G Knepley ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr); 29186636e97aSMatthew G Knepley nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof; 291988ed4aceSMatthew G Knepley } 292088ed4aceSMatthew G Knepley ierr = PetscMalloc(nleaves * sizeof(PetscInt), &local);CHKERRQ(ierr); 292188ed4aceSMatthew G Knepley ierr = PetscMalloc(nleaves * sizeof(PetscSFNode), &remote);CHKERRQ(ierr); 292288ed4aceSMatthew G Knepley for (p = pStart, l = 0; p < pEnd; ++p) { 29231f588964SMatthew G Knepley const PetscInt *cind; 29246636e97aSMatthew G Knepley PetscInt dof, cdof, off, gdof, gcdof, goff, gsize, d, c; 292588ed4aceSMatthew G Knepley 292688ed4aceSMatthew G Knepley ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr); 292788ed4aceSMatthew G Knepley ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr); 292888ed4aceSMatthew G Knepley ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr); 292988ed4aceSMatthew G Knepley ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr); 293088ed4aceSMatthew G Knepley ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr); 29316636e97aSMatthew G Knepley ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr); 293288ed4aceSMatthew G Knepley ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr); 29336636e97aSMatthew G Knepley if (!gdof) continue; /* Censored point */ 29346636e97aSMatthew G Knepley gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof; 29356636e97aSMatthew G Knepley if (gsize != dof-cdof) { 2936057b4bcdSMatthew 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); 29376636e97aSMatthew G Knepley cdof = 0; /* Ignore constraints */ 29386636e97aSMatthew G Knepley } 293988ed4aceSMatthew G Knepley for (d = 0, c = 0; d < dof; ++d) { 294088ed4aceSMatthew G Knepley if ((c < cdof) && (cind[c] == d)) {++c; continue;} 294188ed4aceSMatthew G Knepley local[l+d-c] = off+d; 294288ed4aceSMatthew G Knepley } 294388ed4aceSMatthew G Knepley if (gdof < 0) { 29446636e97aSMatthew G Knepley for(d = 0; d < gsize; ++d, ++l) { 294588ed4aceSMatthew G Knepley PetscInt offset = -(goff+1) + d, r; 294688ed4aceSMatthew G Knepley 294731d3f06eSJed Brown ierr = PetscFindInt(offset,size,ranges,&r);CHKERRQ(ierr); 294831d3f06eSJed Brown if (r < 0) r = -(r+2); 294988ed4aceSMatthew G Knepley remote[l].rank = r; 295088ed4aceSMatthew G Knepley remote[l].index = offset - ranges[r]; 295188ed4aceSMatthew G Knepley } 295288ed4aceSMatthew G Knepley } else { 29536636e97aSMatthew G Knepley for(d = 0; d < gsize; ++d, ++l) { 295488ed4aceSMatthew G Knepley remote[l].rank = rank; 295588ed4aceSMatthew G Knepley remote[l].index = goff+d - ranges[rank]; 295688ed4aceSMatthew G Knepley } 295788ed4aceSMatthew G Knepley } 295888ed4aceSMatthew G Knepley } 29596636e97aSMatthew G Knepley if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves); 296088ed4aceSMatthew G Knepley ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr); 296188ed4aceSMatthew G Knepley ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr); 296288ed4aceSMatthew G Knepley PetscFunctionReturn(0); 296388ed4aceSMatthew G Knepley } 2964af122d2aSMatthew G Knepley 2965af122d2aSMatthew G Knepley #undef __FUNCT__ 2966b21d0597SMatthew G Knepley #define __FUNCT__ "DMGetPointSF" 2967b21d0597SMatthew G Knepley /*@ 2968b21d0597SMatthew G Knepley DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM. 2969b21d0597SMatthew G Knepley 2970b21d0597SMatthew G Knepley Input Parameter: 2971b21d0597SMatthew G Knepley . dm - The DM 2972b21d0597SMatthew G Knepley 2973b21d0597SMatthew G Knepley Output Parameter: 2974b21d0597SMatthew G Knepley . sf - The PetscSF 2975b21d0597SMatthew G Knepley 2976b21d0597SMatthew G Knepley Level: intermediate 2977b21d0597SMatthew G Knepley 2978b21d0597SMatthew G Knepley Note: This gets a borrowed reference, so the user should not destroy this PetscSF. 2979b21d0597SMatthew G Knepley 2980057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF() 2981b21d0597SMatthew G Knepley @*/ 29820adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf) 29830adebc6cSBarry Smith { 2984b21d0597SMatthew G Knepley PetscFunctionBegin; 2985b21d0597SMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 2986b21d0597SMatthew G Knepley PetscValidPointer(sf, 2); 2987b21d0597SMatthew G Knepley *sf = dm->sf; 2988b21d0597SMatthew G Knepley PetscFunctionReturn(0); 2989b21d0597SMatthew G Knepley } 2990b21d0597SMatthew G Knepley 2991b21d0597SMatthew G Knepley #undef __FUNCT__ 2992057b4bcdSMatthew G Knepley #define __FUNCT__ "DMSetPointSF" 2993057b4bcdSMatthew G Knepley /*@ 2994057b4bcdSMatthew G Knepley DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM. 2995057b4bcdSMatthew G Knepley 2996057b4bcdSMatthew G Knepley Input Parameters: 2997057b4bcdSMatthew G Knepley + dm - The DM 2998057b4bcdSMatthew G Knepley - sf - The PetscSF 2999057b4bcdSMatthew G Knepley 3000057b4bcdSMatthew G Knepley Level: intermediate 3001057b4bcdSMatthew G Knepley 3002057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF() 3003057b4bcdSMatthew G Knepley @*/ 30040adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf) 30050adebc6cSBarry Smith { 3006057b4bcdSMatthew G Knepley PetscErrorCode ierr; 3007057b4bcdSMatthew G Knepley 3008057b4bcdSMatthew G Knepley PetscFunctionBegin; 3009057b4bcdSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 3010057b4bcdSMatthew G Knepley PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1); 3011057b4bcdSMatthew G Knepley ierr = PetscSFDestroy(&dm->sf);CHKERRQ(ierr); 3012057b4bcdSMatthew G Knepley ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr); 3013057b4bcdSMatthew G Knepley dm->sf = sf; 3014057b4bcdSMatthew G Knepley PetscFunctionReturn(0); 3015057b4bcdSMatthew G Knepley } 3016057b4bcdSMatthew G Knepley 3017057b4bcdSMatthew G Knepley #undef __FUNCT__ 3018af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetNumFields" 3019af122d2aSMatthew G Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields) 3020af122d2aSMatthew G Knepley { 3021af122d2aSMatthew G Knepley PetscFunctionBegin; 3022af122d2aSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 3023af122d2aSMatthew G Knepley PetscValidPointer(numFields, 2); 3024af122d2aSMatthew G Knepley *numFields = dm->numFields; 3025af122d2aSMatthew G Knepley PetscFunctionReturn(0); 3026af122d2aSMatthew G Knepley } 3027af122d2aSMatthew G Knepley 3028af122d2aSMatthew G Knepley #undef __FUNCT__ 3029af122d2aSMatthew G Knepley #define __FUNCT__ "DMSetNumFields" 3030af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields) 3031af122d2aSMatthew G Knepley { 3032af122d2aSMatthew G Knepley PetscInt f; 3033af122d2aSMatthew G Knepley PetscErrorCode ierr; 3034af122d2aSMatthew G Knepley 3035af122d2aSMatthew G Knepley PetscFunctionBegin; 3036af122d2aSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 3037af122d2aSMatthew G Knepley for (f = 0; f < dm->numFields; ++f) { 3038af122d2aSMatthew G Knepley ierr = PetscObjectDestroy(&dm->fields[f]);CHKERRQ(ierr); 3039af122d2aSMatthew G Knepley } 3040af122d2aSMatthew G Knepley ierr = PetscFree(dm->fields);CHKERRQ(ierr); 3041af122d2aSMatthew G Knepley dm->numFields = numFields; 3042af122d2aSMatthew G Knepley ierr = PetscMalloc(dm->numFields * sizeof(PetscObject), &dm->fields);CHKERRQ(ierr); 3043af122d2aSMatthew G Knepley for (f = 0; f < dm->numFields; ++f) { 3044af122d2aSMatthew G Knepley ierr = PetscContainerCreate(((PetscObject) dm)->comm, (PetscContainer *) &dm->fields[f]);CHKERRQ(ierr); 3045af122d2aSMatthew G Knepley } 3046af122d2aSMatthew G Knepley PetscFunctionReturn(0); 3047af122d2aSMatthew G Knepley } 3048af122d2aSMatthew G Knepley 3049af122d2aSMatthew G Knepley #undef __FUNCT__ 3050af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetField" 3051af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field) 3052af122d2aSMatthew G Knepley { 3053af122d2aSMatthew G Knepley PetscFunctionBegin; 3054af122d2aSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 3055af122d2aSMatthew G Knepley PetscValidPointer(field, 2); 3056af122d2aSMatthew G Knepley if (!dm->fields) SETERRQ(((PetscObject) dm)->comm, PETSC_ERR_ARG_WRONGSTATE, "Fields have not been setup in this DM. Call DMSetNumFields()"); 3057af122d2aSMatthew G Knepley if ((f < 0) || (f >= dm->numFields)) SETERRQ3(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Field %d should be in [%d,%d)", f, 0, dm->numFields); 3058af122d2aSMatthew G Knepley *field = dm->fields[f]; 3059af122d2aSMatthew G Knepley PetscFunctionReturn(0); 3060af122d2aSMatthew G Knepley } 30616636e97aSMatthew G Knepley 30626636e97aSMatthew G Knepley #undef __FUNCT__ 30636636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinates" 30646636e97aSMatthew G Knepley /*@ 30656636e97aSMatthew G Knepley DMSetCoordinates - Sets into the DM a global vector that holds the coordinates 30666636e97aSMatthew G Knepley 30676636e97aSMatthew G Knepley Collective on DM 30686636e97aSMatthew G Knepley 30696636e97aSMatthew G Knepley Input Parameters: 30706636e97aSMatthew G Knepley + dm - the DM 30716636e97aSMatthew G Knepley - c - coordinate vector 30726636e97aSMatthew G Knepley 30736636e97aSMatthew G Knepley Note: 30746636e97aSMatthew G Knepley The coordinates do include those for ghost points, which are in the local vector 30756636e97aSMatthew G Knepley 30766636e97aSMatthew G Knepley Level: intermediate 30776636e97aSMatthew G Knepley 30786636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates 30796636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM() 30806636e97aSMatthew G Knepley @*/ 30816636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c) 30826636e97aSMatthew G Knepley { 30836636e97aSMatthew G Knepley PetscErrorCode ierr; 30846636e97aSMatthew G Knepley 30856636e97aSMatthew G Knepley PetscFunctionBegin; 30866636e97aSMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 30876636e97aSMatthew G Knepley PetscValidHeaderSpecific(c,VEC_CLASSID,2); 30886636e97aSMatthew G Knepley ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr); 30896636e97aSMatthew G Knepley ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr); 30906636e97aSMatthew G Knepley dm->coordinates = c; 30916636e97aSMatthew G Knepley ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr); 30926636e97aSMatthew G Knepley PetscFunctionReturn(0); 30936636e97aSMatthew G Knepley } 30946636e97aSMatthew G Knepley 30956636e97aSMatthew G Knepley #undef __FUNCT__ 30966636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinatesLocal" 30976636e97aSMatthew G Knepley /*@ 30986636e97aSMatthew G Knepley DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates 30996636e97aSMatthew G Knepley 31006636e97aSMatthew G Knepley Collective on DM 31016636e97aSMatthew G Knepley 31026636e97aSMatthew G Knepley Input Parameters: 31036636e97aSMatthew G Knepley + dm - the DM 31046636e97aSMatthew G Knepley - c - coordinate vector 31056636e97aSMatthew G Knepley 31066636e97aSMatthew G Knepley Note: 31076636e97aSMatthew G Knepley The coordinates of ghost points can be set using DMSetCoordinates() 31086636e97aSMatthew G Knepley followed by DMGetCoordinatesLocal(). This is intended to enable the 31096636e97aSMatthew G Knepley setting of ghost coordinates outside of the domain. 31106636e97aSMatthew G Knepley 31116636e97aSMatthew G Knepley Level: intermediate 31126636e97aSMatthew G Knepley 31136636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates 31146636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM() 31156636e97aSMatthew G Knepley @*/ 31166636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c) 31176636e97aSMatthew G Knepley { 31186636e97aSMatthew G Knepley PetscErrorCode ierr; 31196636e97aSMatthew G Knepley 31206636e97aSMatthew G Knepley PetscFunctionBegin; 31216636e97aSMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 31226636e97aSMatthew G Knepley PetscValidHeaderSpecific(c,VEC_CLASSID,2); 31236636e97aSMatthew G Knepley ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr); 31246636e97aSMatthew G Knepley ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr); 31256636e97aSMatthew G Knepley dm->coordinatesLocal = c; 31266636e97aSMatthew G Knepley ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr); 31276636e97aSMatthew G Knepley PetscFunctionReturn(0); 31286636e97aSMatthew G Knepley } 31296636e97aSMatthew G Knepley 31306636e97aSMatthew G Knepley #undef __FUNCT__ 31316636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinates" 31326636e97aSMatthew G Knepley /*@ 31336636e97aSMatthew G Knepley DMGetCoordinates - Gets a global vector with the coordinates associated with the DM. 31346636e97aSMatthew G Knepley 31356636e97aSMatthew G Knepley Not Collective 31366636e97aSMatthew G Knepley 31376636e97aSMatthew G Knepley Input Parameter: 31386636e97aSMatthew G Knepley . dm - the DM 31396636e97aSMatthew G Knepley 31406636e97aSMatthew G Knepley Output Parameter: 31416636e97aSMatthew G Knepley . c - global coordinate vector 31426636e97aSMatthew G Knepley 31436636e97aSMatthew G Knepley Note: 31446636e97aSMatthew G Knepley This is a borrowed reference, so the user should NOT destroy this vector 31456636e97aSMatthew G Knepley 31466636e97aSMatthew G Knepley Each process has only the local coordinates (does NOT have the ghost coordinates). 31476636e97aSMatthew G Knepley 31486636e97aSMatthew G Knepley For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...) 31496636e97aSMatthew G Knepley and (x_0,y_0,z_0,x_1,y_1,z_1...) 31506636e97aSMatthew G Knepley 31516636e97aSMatthew G Knepley Level: intermediate 31526636e97aSMatthew G Knepley 31536636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates 31546636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM() 31556636e97aSMatthew G Knepley @*/ 31566636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c) 31576636e97aSMatthew G Knepley { 31586636e97aSMatthew G Knepley PetscErrorCode ierr; 31596636e97aSMatthew G Knepley 31606636e97aSMatthew G Knepley PetscFunctionBegin; 31616636e97aSMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 31626636e97aSMatthew G Knepley PetscValidPointer(c,2); 31631f588964SMatthew G Knepley if (!dm->coordinates && dm->coordinatesLocal) { 316487743a01SMatthew G Knepley DM cdm = PETSC_NULL; 31656636e97aSMatthew G Knepley 31666636e97aSMatthew G Knepley ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr); 31676636e97aSMatthew G Knepley ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr); 31686636e97aSMatthew G Knepley ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr); 31696636e97aSMatthew G Knepley ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr); 31706636e97aSMatthew G Knepley ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr); 31716636e97aSMatthew G Knepley } 31726636e97aSMatthew G Knepley *c = dm->coordinates; 31736636e97aSMatthew G Knepley PetscFunctionReturn(0); 31746636e97aSMatthew G Knepley } 31756636e97aSMatthew G Knepley 31766636e97aSMatthew G Knepley #undef __FUNCT__ 31776636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinatesLocal" 31786636e97aSMatthew G Knepley /*@ 31796636e97aSMatthew G Knepley DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM. 31806636e97aSMatthew G Knepley 31816636e97aSMatthew G Knepley Collective on DM 31826636e97aSMatthew G Knepley 31836636e97aSMatthew G Knepley Input Parameter: 31846636e97aSMatthew G Knepley . dm - the DM 31856636e97aSMatthew G Knepley 31866636e97aSMatthew G Knepley Output Parameter: 31876636e97aSMatthew G Knepley . c - coordinate vector 31886636e97aSMatthew G Knepley 31896636e97aSMatthew G Knepley Note: 31906636e97aSMatthew G Knepley This is a borrowed reference, so the user should NOT destroy this vector 31916636e97aSMatthew G Knepley 31926636e97aSMatthew G Knepley Each process has the local and ghost coordinates 31936636e97aSMatthew G Knepley 31946636e97aSMatthew G Knepley For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...) 31956636e97aSMatthew G Knepley and (x_0,y_0,z_0,x_1,y_1,z_1...) 31966636e97aSMatthew G Knepley 31976636e97aSMatthew G Knepley Level: intermediate 31986636e97aSMatthew G Knepley 31996636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates 32006636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM() 32016636e97aSMatthew G Knepley @*/ 32026636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c) 32036636e97aSMatthew G Knepley { 32046636e97aSMatthew G Knepley PetscErrorCode ierr; 32056636e97aSMatthew G Knepley 32066636e97aSMatthew G Knepley PetscFunctionBegin; 32076636e97aSMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 32086636e97aSMatthew G Knepley PetscValidPointer(c,2); 32091f588964SMatthew G Knepley if (!dm->coordinatesLocal && dm->coordinates) { 321087743a01SMatthew G Knepley DM cdm = PETSC_NULL; 32116636e97aSMatthew G Knepley 32126636e97aSMatthew G Knepley ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr); 32136636e97aSMatthew G Knepley ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr); 32146636e97aSMatthew G Knepley ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr); 32156636e97aSMatthew G Knepley ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr); 32166636e97aSMatthew G Knepley ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr); 32176636e97aSMatthew G Knepley } 32186636e97aSMatthew G Knepley *c = dm->coordinatesLocal; 32196636e97aSMatthew G Knepley PetscFunctionReturn(0); 32206636e97aSMatthew G Knepley } 32216636e97aSMatthew G Knepley 32226636e97aSMatthew G Knepley #undef __FUNCT__ 32236636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinateDM" 32246636e97aSMatthew G Knepley /*@ 32256636e97aSMatthew G Knepley DMGetCoordinateDM - Gets the DM that scatters between global and local coordinates 32266636e97aSMatthew G Knepley 32276636e97aSMatthew G Knepley Collective on DM 32286636e97aSMatthew G Knepley 32296636e97aSMatthew G Knepley Input Parameter: 32306636e97aSMatthew G Knepley . dm - the DM 32316636e97aSMatthew G Knepley 32326636e97aSMatthew G Knepley Output Parameter: 32336636e97aSMatthew G Knepley . cdm - coordinate DM 32346636e97aSMatthew G Knepley 32356636e97aSMatthew G Knepley Level: intermediate 32366636e97aSMatthew G Knepley 32376636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates 32386636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal() 32396636e97aSMatthew G Knepley @*/ 32406636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm) 32416636e97aSMatthew G Knepley { 32426636e97aSMatthew G Knepley PetscErrorCode ierr; 32436636e97aSMatthew G Knepley 32446636e97aSMatthew G Knepley PetscFunctionBegin; 32456636e97aSMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 32466636e97aSMatthew G Knepley PetscValidPointer(cdm,2); 32476636e97aSMatthew G Knepley if (!dm->coordinateDM) { 32486636e97aSMatthew G Knepley if (!dm->ops->createcoordinatedm) SETERRQ(((PetscObject) dm)->comm, PETSC_ERR_SUP, "Unable to create coordinates for this DM"); 32496636e97aSMatthew G Knepley ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr); 32506636e97aSMatthew G Knepley } 32516636e97aSMatthew G Knepley *cdm = dm->coordinateDM; 32526636e97aSMatthew G Knepley PetscFunctionReturn(0); 32536636e97aSMatthew G Knepley } 3254e87bb0d3SMatthew G Knepley 3255e87bb0d3SMatthew G Knepley #undef __FUNCT__ 3256e87bb0d3SMatthew G Knepley #define __FUNCT__ "DMLocatePoints" 3257e87bb0d3SMatthew G Knepley /*@ 3258e87bb0d3SMatthew G Knepley DMLocatePoints - Locate the points in v in the mesh and return an IS of the containing cells 3259e87bb0d3SMatthew G Knepley 3260e87bb0d3SMatthew G Knepley Not collective 3261e87bb0d3SMatthew G Knepley 3262e87bb0d3SMatthew G Knepley Input Parameters: 3263e87bb0d3SMatthew G Knepley + dm - The DM 3264e87bb0d3SMatthew G Knepley - v - The Vec of points 3265e87bb0d3SMatthew G Knepley 326661e3bb9bSMatthew G Knepley Output Parameter: 3267e87bb0d3SMatthew G Knepley . cells - The local cell numbers for cells which contain the points 3268e87bb0d3SMatthew G Knepley 3269e87bb0d3SMatthew G Knepley Level: developer 327061e3bb9bSMatthew G Knepley 327161e3bb9bSMatthew G Knepley .keywords: point location, mesh 327261e3bb9bSMatthew G Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal() 327361e3bb9bSMatthew G Knepley @*/ 3274e87bb0d3SMatthew G Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, IS *cells) 3275e87bb0d3SMatthew G Knepley { 3276735aa83eSMatthew G Knepley PetscErrorCode ierr; 3277735aa83eSMatthew G Knepley 3278e87bb0d3SMatthew G Knepley PetscFunctionBegin; 3279e87bb0d3SMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 3280e87bb0d3SMatthew G Knepley PetscValidHeaderSpecific(v,VEC_CLASSID,2); 3281e87bb0d3SMatthew G Knepley PetscValidPointer(cells,3); 3282735aa83eSMatthew G Knepley if (dm->ops->locatepoints) { 3283735aa83eSMatthew G Knepley ierr = (*dm->ops->locatepoints)(dm,v,cells);CHKERRQ(ierr); 3284735aa83eSMatthew G Knepley } else { 3285735aa83eSMatthew G Knepley SETERRQ(((PetscObject) dm)->comm, PETSC_ERR_SUP, "Point location not available for this DM"); 3286735aa83eSMatthew G Knepley } 3287e87bb0d3SMatthew G Knepley PetscFunctionReturn(0); 3288e87bb0d3SMatthew G Knepley } 3289