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; 60519f805aSKarl Rupp #if !defined(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 } 3252348bcf4SPeter Brune for (nlink=(*dm)->namedlocal; nlink; nlink=nlink->next) cnt++; 3262348bcf4SPeter Brune if ((*dm)->x) { 3272348bcf4SPeter Brune DM obj; 3282348bcf4SPeter Brune ierr = VecGetDM((*dm)->x, &obj);CHKERRQ(ierr); 3292348bcf4SPeter Brune if (obj == *dm) cnt++; 3302348bcf4SPeter Brune } 331732e2eb9SMatthew G Knepley 3326bf464f9SBarry Smith if (--((PetscObject)(*dm))->refct - cnt > 0) {*dm = 0; PetscFunctionReturn(0);} 333732e2eb9SMatthew G Knepley /* 334732e2eb9SMatthew G Knepley Need this test because the dm references the vectors that 335732e2eb9SMatthew G Knepley reference the dm, so destroying the dm calls destroy on the 336732e2eb9SMatthew G Knepley vectors that cause another destroy on the dm 337732e2eb9SMatthew G Knepley */ 3386bf464f9SBarry Smith if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0); 3396bf464f9SBarry Smith ((PetscObject) (*dm))->refct = 0; 340732e2eb9SMatthew G Knepley for (i=0; i<DM_MAX_WORK_VECTORS; i++) { 3416bf464f9SBarry Smith if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()"); 3426bf464f9SBarry Smith ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr); 343732e2eb9SMatthew G Knepley } 344dfe15315SJed Brown for (nlink=(*dm)->namedglobal; nlink; nlink=nnext) { /* Destroy the named vectors */ 345dfe15315SJed Brown nnext = nlink->next; 346dfe15315SJed 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); 347dfe15315SJed Brown ierr = PetscFree(nlink->name);CHKERRQ(ierr); 348dfe15315SJed Brown ierr = VecDestroy(&nlink->X);CHKERRQ(ierr); 349dfe15315SJed Brown ierr = PetscFree(nlink);CHKERRQ(ierr); 350dfe15315SJed Brown } 351dfe15315SJed Brown (*dm)->namedglobal = PETSC_NULL; 3521a266240SBarry Smith 3532348bcf4SPeter Brune for (nlink=(*dm)->namedlocal; nlink; nlink=nnext) { /* Destroy the named local vectors */ 3542348bcf4SPeter Brune nnext = nlink->next; 3552348bcf4SPeter Brune if (nlink->status != DMVEC_STATUS_IN) SETERRQ1(((PetscObject)*dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"DM still has Vec named '%s' checked out",nlink->name); 3562348bcf4SPeter Brune ierr = PetscFree(nlink->name);CHKERRQ(ierr); 3572348bcf4SPeter Brune ierr = VecDestroy(&nlink->X);CHKERRQ(ierr); 3582348bcf4SPeter Brune ierr = PetscFree(nlink);CHKERRQ(ierr); 3592348bcf4SPeter Brune } 3602348bcf4SPeter Brune (*dm)->namedlocal = PETSC_NULL; 3612348bcf4SPeter Brune 362b17ce1afSJed Brown /* Destroy the list of hooks */ 363c833c3b5SJed Brown { 364c833c3b5SJed Brown DMCoarsenHookLink link,next; 365b17ce1afSJed Brown for (link=(*dm)->coarsenhook; link; link=next) { 366b17ce1afSJed Brown next = link->next; 367b17ce1afSJed Brown ierr = PetscFree(link);CHKERRQ(ierr); 368b17ce1afSJed Brown } 369b17ce1afSJed Brown (*dm)->coarsenhook = PETSC_NULL; 370c833c3b5SJed Brown } 371c833c3b5SJed Brown { 372c833c3b5SJed Brown DMRefineHookLink link,next; 373c833c3b5SJed Brown for (link=(*dm)->refinehook; link; link=next) { 374c833c3b5SJed Brown next = link->next; 375c833c3b5SJed Brown ierr = PetscFree(link);CHKERRQ(ierr); 376c833c3b5SJed Brown } 377c833c3b5SJed Brown (*dm)->refinehook = PETSC_NULL; 378c833c3b5SJed Brown } 379be081cd6SPeter Brune { 380be081cd6SPeter Brune DMSubDomainHookLink link,next; 381be081cd6SPeter Brune for (link=(*dm)->subdomainhook; link; link=next) { 382be081cd6SPeter Brune next = link->next; 383be081cd6SPeter Brune ierr = PetscFree(link);CHKERRQ(ierr); 384be081cd6SPeter Brune } 385be081cd6SPeter Brune (*dm)->subdomainhook = PETSC_NULL; 386be081cd6SPeter Brune } 387baf369e7SPeter Brune { 388baf369e7SPeter Brune DMGlobalToLocalHookLink link,next; 389baf369e7SPeter Brune for (link=(*dm)->gtolhook; link; link=next) { 390baf369e7SPeter Brune next = link->next; 391baf369e7SPeter Brune ierr = PetscFree(link);CHKERRQ(ierr); 392baf369e7SPeter Brune } 393baf369e7SPeter Brune (*dm)->gtolhook = PETSC_NULL; 394baf369e7SPeter Brune } 395aa1993deSMatthew G Knepley /* Destroy the work arrays */ 396aa1993deSMatthew G Knepley { 397aa1993deSMatthew G Knepley DMWorkLink link,next; 398aa1993deSMatthew G Knepley if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out"); 399aa1993deSMatthew G Knepley for (link=(*dm)->workin; link; link=next) { 400aa1993deSMatthew G Knepley next = link->next; 401aa1993deSMatthew G Knepley ierr = PetscFree(link->mem);CHKERRQ(ierr); 402aa1993deSMatthew G Knepley ierr = PetscFree(link);CHKERRQ(ierr); 403aa1993deSMatthew G Knepley } 404aa1993deSMatthew G Knepley (*dm)->workin = PETSC_NULL; 405aa1993deSMatthew G Knepley } 406b17ce1afSJed Brown 40752536dc3SBarry Smith ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr); 40852536dc3SBarry Smith ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr); 40952536dc3SBarry Smith ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr); 41052536dc3SBarry Smith 4111a266240SBarry Smith if ((*dm)->ctx && (*dm)->ctxdestroy) { 4121a266240SBarry Smith ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr); 4131a266240SBarry Smith } 41487e657c6SBarry Smith ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr); 41571cd77b2SBarry Smith ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr); 4164dcab191SBarry Smith ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr); 4176bf464f9SBarry Smith ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr); 4186bf464f9SBarry Smith ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmapb);CHKERRQ(ierr); 4196bf464f9SBarry Smith ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr); 420073dac72SJed Brown ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr); 42188ed4aceSMatthew G Knepley 42288ed4aceSMatthew G Knepley ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr); 42388ed4aceSMatthew G Knepley ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr); 4248b1ab98fSJed Brown ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr); 42588ed4aceSMatthew G Knepley ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr); 42688ed4aceSMatthew G Knepley ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr); 427af122d2aSMatthew G Knepley 4286636e97aSMatthew G Knepley ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr); 4296636e97aSMatthew G Knepley ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr); 4306636e97aSMatthew G Knepley ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr); 4316636e97aSMatthew G Knepley 432af122d2aSMatthew G Knepley for (f = 0; f < (*dm)->numFields; ++f) { 433af122d2aSMatthew G Knepley ierr = PetscObjectDestroy(&(*dm)->fields[f]);CHKERRQ(ierr); 434af122d2aSMatthew G Knepley } 435af122d2aSMatthew G Knepley ierr = PetscFree((*dm)->fields);CHKERRQ(ierr); 436732e2eb9SMatthew G Knepley /* if memory was published with AMS then destroy it */ 4376bf464f9SBarry Smith ierr = PetscObjectDepublish(*dm);CHKERRQ(ierr); 438732e2eb9SMatthew G Knepley 4396bf464f9SBarry Smith ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr); 440435a35e8SMatthew G Knepley /* We do not destroy (*dm)->data here so that we can reference count backend objects */ 441732e2eb9SMatthew G Knepley ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr); 44247c6ae99SBarry Smith PetscFunctionReturn(0); 44347c6ae99SBarry Smith } 44447c6ae99SBarry Smith 44547c6ae99SBarry Smith #undef __FUNCT__ 446d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp" 447d7bf68aeSBarry Smith /*@ 448d7bf68aeSBarry Smith DMSetUp - sets up the data structures inside a DM object 449d7bf68aeSBarry Smith 450d7bf68aeSBarry Smith Collective on DM 451d7bf68aeSBarry Smith 452d7bf68aeSBarry Smith Input Parameter: 453d7bf68aeSBarry Smith . dm - the DM object to setup 454d7bf68aeSBarry Smith 455d7bf68aeSBarry Smith Level: developer 456d7bf68aeSBarry Smith 457e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix() 458d7bf68aeSBarry Smith 459d7bf68aeSBarry Smith @*/ 4607087cfbeSBarry Smith PetscErrorCode DMSetUp(DM dm) 461d7bf68aeSBarry Smith { 462d7bf68aeSBarry Smith PetscErrorCode ierr; 463d7bf68aeSBarry Smith 464d7bf68aeSBarry Smith PetscFunctionBegin; 465171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 4668387afaaSJed Brown if (dm->setupcalled) PetscFunctionReturn(0); 467d7bf68aeSBarry Smith if (dm->ops->setup) { 468d7bf68aeSBarry Smith ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr); 469d7bf68aeSBarry Smith } 4708387afaaSJed Brown dm->setupcalled = PETSC_TRUE; 471d7bf68aeSBarry Smith PetscFunctionReturn(0); 472d7bf68aeSBarry Smith } 473d7bf68aeSBarry Smith 474d7bf68aeSBarry Smith #undef __FUNCT__ 475d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions" 476d7bf68aeSBarry Smith /*@ 477d7bf68aeSBarry Smith DMSetFromOptions - sets parameters in a DM from the options database 478d7bf68aeSBarry Smith 479d7bf68aeSBarry Smith Collective on DM 480d7bf68aeSBarry Smith 481d7bf68aeSBarry Smith Input Parameter: 482d7bf68aeSBarry Smith . dm - the DM object to set options for 483d7bf68aeSBarry Smith 484732e2eb9SMatthew G Knepley Options Database: 485dd85299cSBarry Smith + -dm_preallocate_only: Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros 486dd85299cSBarry Smith . -dm_vec_type <type> type of vector to create inside DM 487171400e9SBarry Smith . -dm_mat_type <type> type of matrix to create inside DM 488171400e9SBarry Smith - -dm_coloring_type <global or ghosted> 489732e2eb9SMatthew G Knepley 490d7bf68aeSBarry Smith Level: developer 491d7bf68aeSBarry Smith 492e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix() 493d7bf68aeSBarry Smith 494d7bf68aeSBarry Smith @*/ 4957087cfbeSBarry Smith PetscErrorCode DMSetFromOptions(DM dm) 496d7bf68aeSBarry Smith { 497f55f929bSMatthew G Knepley char typeName[256] = MATAIJ; 498ca266f36SBarry Smith PetscBool flg; 499d7bf68aeSBarry Smith PetscErrorCode ierr; 500d7bf68aeSBarry Smith 501d7bf68aeSBarry Smith PetscFunctionBegin; 502171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 5033194b578SJed Brown ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr); 504073dac72SJed 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); 505f9ba7244SBarry Smith ierr = PetscOptionsList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr); 506f9ba7244SBarry Smith if (flg) { 507f9ba7244SBarry Smith ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr); 508f9ba7244SBarry Smith } 5098caf3d72SBarry 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); 510073dac72SJed Brown if (flg) { 511521d9a4cSLisandro Dalcin ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr); 512073dac72SJed Brown } 5131b89239cSHong 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); 514f9ba7244SBarry Smith if (dm->ops->setfromoptions) { 515f9ba7244SBarry Smith ierr = (*dm->ops->setfromoptions)(dm);CHKERRQ(ierr); 516f9ba7244SBarry Smith } 517f9ba7244SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 518f9ba7244SBarry Smith ierr = PetscObjectProcessOptionsHandlers((PetscObject) dm);CHKERRQ(ierr); 51982fcb398SMatthew G Knepley ierr = PetscOptionsEnd();CHKERRQ(ierr); 520ca266f36SBarry Smith ierr = DMViewFromOptions(dm,"-dm_view");CHKERRQ(ierr); 521d7bf68aeSBarry Smith PetscFunctionReturn(0); 522d7bf68aeSBarry Smith } 523d7bf68aeSBarry Smith 524d7bf68aeSBarry Smith #undef __FUNCT__ 52547c6ae99SBarry Smith #define __FUNCT__ "DMView" 526fc9bc008SSatish Balay /*@C 527aa219208SBarry Smith DMView - Views a vector packer or DMDA. 52847c6ae99SBarry Smith 52947c6ae99SBarry Smith Collective on DM 53047c6ae99SBarry Smith 53147c6ae99SBarry Smith Input Parameter: 53247c6ae99SBarry Smith + dm - the DM object to view 53347c6ae99SBarry Smith - v - the viewer 53447c6ae99SBarry Smith 53547c6ae99SBarry Smith Level: developer 53647c6ae99SBarry Smith 537e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix() 53847c6ae99SBarry Smith 53947c6ae99SBarry Smith @*/ 5407087cfbeSBarry Smith PetscErrorCode DMView(DM dm,PetscViewer v) 54147c6ae99SBarry Smith { 54247c6ae99SBarry Smith PetscErrorCode ierr; 54332c0f0efSBarry Smith PetscBool isbinary; 54447c6ae99SBarry Smith 54547c6ae99SBarry Smith PetscFunctionBegin; 546171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 5473014e516SBarry Smith if (!v) { 5483014e516SBarry Smith ierr = PetscViewerASCIIGetStdout(((PetscObject)dm)->comm,&v);CHKERRQ(ierr); 5493014e516SBarry Smith } 55032c0f0efSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 55132c0f0efSBarry Smith if (isbinary) { 55255849f57SBarry Smith PetscInt classid = DM_FILE_CLASSID; 55332c0f0efSBarry Smith char type[256]; 55432c0f0efSBarry Smith 55532c0f0efSBarry Smith ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr); 55632c0f0efSBarry Smith ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr); 55732c0f0efSBarry Smith ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr); 55832c0f0efSBarry Smith } 5590c010503SBarry Smith if (dm->ops->view) { 5600c010503SBarry Smith ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr); 56147c6ae99SBarry Smith } 56247c6ae99SBarry Smith PetscFunctionReturn(0); 56347c6ae99SBarry Smith } 56447c6ae99SBarry Smith 56547c6ae99SBarry Smith #undef __FUNCT__ 56647c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector" 56747c6ae99SBarry Smith /*@ 568aa219208SBarry Smith DMCreateGlobalVector - Creates a global vector from a DMDA or DMComposite object 56947c6ae99SBarry Smith 57047c6ae99SBarry Smith Collective on DM 57147c6ae99SBarry Smith 57247c6ae99SBarry Smith Input Parameter: 57347c6ae99SBarry Smith . dm - the DM object 57447c6ae99SBarry Smith 57547c6ae99SBarry Smith Output Parameter: 57647c6ae99SBarry Smith . vec - the global vector 57747c6ae99SBarry Smith 578073dac72SJed Brown Level: beginner 57947c6ae99SBarry Smith 580e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix() 58147c6ae99SBarry Smith 58247c6ae99SBarry Smith @*/ 5837087cfbeSBarry Smith PetscErrorCode DMCreateGlobalVector(DM dm,Vec *vec) 58447c6ae99SBarry Smith { 58547c6ae99SBarry Smith PetscErrorCode ierr; 58647c6ae99SBarry Smith 58747c6ae99SBarry Smith PetscFunctionBegin; 588171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 58947c6ae99SBarry Smith ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr); 59047c6ae99SBarry Smith PetscFunctionReturn(0); 59147c6ae99SBarry Smith } 59247c6ae99SBarry Smith 59347c6ae99SBarry Smith #undef __FUNCT__ 59447c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector" 59547c6ae99SBarry Smith /*@ 596aa219208SBarry Smith DMCreateLocalVector - Creates a local vector from a DMDA or DMComposite object 59747c6ae99SBarry Smith 59847c6ae99SBarry Smith Not Collective 59947c6ae99SBarry Smith 60047c6ae99SBarry Smith Input Parameter: 60147c6ae99SBarry Smith . dm - the DM object 60247c6ae99SBarry Smith 60347c6ae99SBarry Smith Output Parameter: 60447c6ae99SBarry Smith . vec - the local vector 60547c6ae99SBarry Smith 606073dac72SJed Brown Level: beginner 60747c6ae99SBarry Smith 608e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix() 60947c6ae99SBarry Smith 61047c6ae99SBarry Smith @*/ 6117087cfbeSBarry Smith PetscErrorCode DMCreateLocalVector(DM dm,Vec *vec) 61247c6ae99SBarry Smith { 61347c6ae99SBarry Smith PetscErrorCode ierr; 61447c6ae99SBarry Smith 61547c6ae99SBarry Smith PetscFunctionBegin; 616171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 61747c6ae99SBarry Smith ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr); 61847c6ae99SBarry Smith PetscFunctionReturn(0); 61947c6ae99SBarry Smith } 62047c6ae99SBarry Smith 62147c6ae99SBarry Smith #undef __FUNCT__ 6221411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping" 6231411c6eeSJed Brown /*@ 6241411c6eeSJed Brown DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM. 6251411c6eeSJed Brown 6261411c6eeSJed Brown Collective on DM 6271411c6eeSJed Brown 6281411c6eeSJed Brown Input Parameter: 6291411c6eeSJed Brown . dm - the DM that provides the mapping 6301411c6eeSJed Brown 6311411c6eeSJed Brown Output Parameter: 6321411c6eeSJed Brown . ltog - the mapping 6331411c6eeSJed Brown 6341411c6eeSJed Brown Level: intermediate 6351411c6eeSJed Brown 6361411c6eeSJed Brown Notes: 6371411c6eeSJed Brown This mapping can then be used by VecSetLocalToGlobalMapping() or 6381411c6eeSJed Brown MatSetLocalToGlobalMapping(). 6391411c6eeSJed Brown 6401411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMappingBlock() 6411411c6eeSJed Brown @*/ 6427087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog) 6431411c6eeSJed Brown { 6441411c6eeSJed Brown PetscErrorCode ierr; 6451411c6eeSJed Brown 6461411c6eeSJed Brown PetscFunctionBegin; 6471411c6eeSJed Brown PetscValidHeaderSpecific(dm,DM_CLASSID,1); 6481411c6eeSJed Brown PetscValidPointer(ltog,2); 6491411c6eeSJed Brown if (!dm->ltogmap) { 65037d0c07bSMatthew G Knepley PetscSection section, sectionGlobal; 65137d0c07bSMatthew G Knepley 65237d0c07bSMatthew G Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 65337d0c07bSMatthew G Knepley if (section) { 65437d0c07bSMatthew G Knepley PetscInt *ltog; 65537d0c07bSMatthew G Knepley PetscInt pStart, pEnd, size, p, l; 65637d0c07bSMatthew G Knepley 65737d0c07bSMatthew G Knepley ierr = DMGetDefaultGlobalSection(dm, §ionGlobal);CHKERRQ(ierr); 65837d0c07bSMatthew G Knepley ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr); 65937d0c07bSMatthew G Knepley ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr); 66037d0c07bSMatthew G Knepley ierr = PetscMalloc(size * sizeof(PetscInt), <og);CHKERRQ(ierr); /* We want the local+overlap size */ 66137d0c07bSMatthew G Knepley for (p = pStart, l = 0; p < pEnd; ++p) { 66237d0c07bSMatthew G Knepley PetscInt dof, off, c; 66337d0c07bSMatthew G Knepley 66437d0c07bSMatthew G Knepley /* Should probably use constrained dofs */ 66537d0c07bSMatthew G Knepley ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr); 66637d0c07bSMatthew G Knepley ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr); 66737d0c07bSMatthew G Knepley for (c = 0; c < dof; ++c, ++l) { 66837d0c07bSMatthew G Knepley ltog[l] = off+c; 66937d0c07bSMatthew G Knepley } 67037d0c07bSMatthew G Knepley } 67137d0c07bSMatthew G Knepley ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_SELF, size, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr); 67237d0c07bSMatthew G Knepley ierr = PetscLogObjectParent(dm, dm->ltogmap);CHKERRQ(ierr); 67337d0c07bSMatthew G Knepley } else { 6741411c6eeSJed Brown if (!dm->ops->createlocaltoglobalmapping) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping"); 6751411c6eeSJed Brown ierr = (*dm->ops->createlocaltoglobalmapping)(dm);CHKERRQ(ierr); 6761411c6eeSJed Brown } 67737d0c07bSMatthew G Knepley } 6781411c6eeSJed Brown *ltog = dm->ltogmap; 6791411c6eeSJed Brown PetscFunctionReturn(0); 6801411c6eeSJed Brown } 6811411c6eeSJed Brown 6821411c6eeSJed Brown #undef __FUNCT__ 6831411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMappingBlock" 6841411c6eeSJed Brown /*@ 6851411c6eeSJed Brown DMGetLocalToGlobalMappingBlock - Accesses the blocked local-to-global mapping in a DM. 6861411c6eeSJed Brown 6871411c6eeSJed Brown Collective on DM 6881411c6eeSJed Brown 6891411c6eeSJed Brown Input Parameter: 6901411c6eeSJed Brown . da - the distributed array that provides the mapping 6911411c6eeSJed Brown 6921411c6eeSJed Brown Output Parameter: 6931411c6eeSJed Brown . ltog - the block mapping 6941411c6eeSJed Brown 6951411c6eeSJed Brown Level: intermediate 6961411c6eeSJed Brown 6971411c6eeSJed Brown Notes: 6981411c6eeSJed Brown This mapping can then be used by VecSetLocalToGlobalMappingBlock() or 6991411c6eeSJed Brown MatSetLocalToGlobalMappingBlock(). 7001411c6eeSJed Brown 7011411c6eeSJed Brown .seealso: DMCreateLocalVector(), DMGetLocalToGlobalMapping(), DMGetBlockSize(), VecSetBlockSize(), MatSetBlockSize() 7021411c6eeSJed Brown @*/ 7037087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMappingBlock(DM dm,ISLocalToGlobalMapping *ltog) 7041411c6eeSJed Brown { 7051411c6eeSJed Brown PetscErrorCode ierr; 7061411c6eeSJed Brown 7071411c6eeSJed Brown PetscFunctionBegin; 7081411c6eeSJed Brown PetscValidHeaderSpecific(dm,DM_CLASSID,1); 7091411c6eeSJed Brown PetscValidPointer(ltog,2); 7101411c6eeSJed Brown if (!dm->ltogmapb) { 7111411c6eeSJed Brown PetscInt bs; 7121411c6eeSJed Brown ierr = DMGetBlockSize(dm,&bs);CHKERRQ(ierr); 7131411c6eeSJed Brown if (bs > 1) { 7141411c6eeSJed Brown if (!dm->ops->createlocaltoglobalmappingblock) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"DM can not create LocalToGlobalMappingBlock"); 7151411c6eeSJed Brown ierr = (*dm->ops->createlocaltoglobalmappingblock)(dm);CHKERRQ(ierr); 7161411c6eeSJed Brown } else { 7171411c6eeSJed Brown ierr = DMGetLocalToGlobalMapping(dm,&dm->ltogmapb);CHKERRQ(ierr); 7181411c6eeSJed Brown ierr = PetscObjectReference((PetscObject)dm->ltogmapb);CHKERRQ(ierr); 7191411c6eeSJed Brown } 7201411c6eeSJed Brown } 7211411c6eeSJed Brown *ltog = dm->ltogmapb; 7221411c6eeSJed Brown PetscFunctionReturn(0); 7231411c6eeSJed Brown } 7241411c6eeSJed Brown 7251411c6eeSJed Brown #undef __FUNCT__ 7261411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize" 7271411c6eeSJed Brown /*@ 7281411c6eeSJed Brown DMGetBlockSize - Gets the inherent block size associated with a DM 7291411c6eeSJed Brown 7301411c6eeSJed Brown Not Collective 7311411c6eeSJed Brown 7321411c6eeSJed Brown Input Parameter: 7331411c6eeSJed Brown . dm - the DM with block structure 7341411c6eeSJed Brown 7351411c6eeSJed Brown Output Parameter: 7361411c6eeSJed Brown . bs - the block size, 1 implies no exploitable block structure 7371411c6eeSJed Brown 7381411c6eeSJed Brown Level: intermediate 7391411c6eeSJed Brown 7401411c6eeSJed Brown .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMappingBlock() 7411411c6eeSJed Brown @*/ 7427087cfbeSBarry Smith PetscErrorCode DMGetBlockSize(DM dm,PetscInt *bs) 7431411c6eeSJed Brown { 7441411c6eeSJed Brown PetscFunctionBegin; 7451411c6eeSJed Brown PetscValidHeaderSpecific(dm,DM_CLASSID,1); 7461411c6eeSJed Brown PetscValidPointer(bs,2); 7471411c6eeSJed 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"); 7481411c6eeSJed Brown *bs = dm->bs; 7491411c6eeSJed Brown PetscFunctionReturn(0); 7501411c6eeSJed Brown } 7511411c6eeSJed Brown 7521411c6eeSJed Brown #undef __FUNCT__ 753e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation" 75447c6ae99SBarry Smith /*@ 755e727c939SJed Brown DMCreateInterpolation - Gets interpolation matrix between two DMDA or DMComposite objects 75647c6ae99SBarry Smith 75747c6ae99SBarry Smith Collective on DM 75847c6ae99SBarry Smith 75947c6ae99SBarry Smith Input Parameter: 76047c6ae99SBarry Smith + dm1 - the DM object 76147c6ae99SBarry Smith - dm2 - the second, finer DM object 76247c6ae99SBarry Smith 76347c6ae99SBarry Smith Output Parameter: 76447c6ae99SBarry Smith + mat - the interpolation 76547c6ae99SBarry Smith - vec - the scaling (optional) 76647c6ae99SBarry Smith 76747c6ae99SBarry Smith Level: developer 76847c6ae99SBarry Smith 76985afcc9aSBarry 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 77085afcc9aSBarry Smith DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation. 771d52bd9f3SBarry Smith 7721f588964SMatthew G Knepley For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors 773d52bd9f3SBarry Smith EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic. 77485afcc9aSBarry Smith 77585afcc9aSBarry Smith 776e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen() 77747c6ae99SBarry Smith 77847c6ae99SBarry Smith @*/ 779e727c939SJed Brown PetscErrorCode DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec) 78047c6ae99SBarry Smith { 78147c6ae99SBarry Smith PetscErrorCode ierr; 78247c6ae99SBarry Smith 78347c6ae99SBarry Smith PetscFunctionBegin; 784171400e9SBarry Smith PetscValidHeaderSpecific(dm1,DM_CLASSID,1); 785171400e9SBarry Smith PetscValidHeaderSpecific(dm2,DM_CLASSID,2); 78625296bd5SBarry Smith ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr); 78747c6ae99SBarry Smith PetscFunctionReturn(0); 78847c6ae99SBarry Smith } 78947c6ae99SBarry Smith 79047c6ae99SBarry Smith #undef __FUNCT__ 791e727c939SJed Brown #define __FUNCT__ "DMCreateInjection" 79247c6ae99SBarry Smith /*@ 793e727c939SJed Brown DMCreateInjection - Gets injection matrix between two DMDA or DMComposite objects 79447c6ae99SBarry Smith 79547c6ae99SBarry Smith Collective on DM 79647c6ae99SBarry Smith 79747c6ae99SBarry Smith Input Parameter: 79847c6ae99SBarry Smith + dm1 - the DM object 79947c6ae99SBarry Smith - dm2 - the second, finer DM object 80047c6ae99SBarry Smith 80147c6ae99SBarry Smith Output Parameter: 80247c6ae99SBarry Smith . ctx - the injection 80347c6ae99SBarry Smith 80447c6ae99SBarry Smith Level: developer 80547c6ae99SBarry Smith 80685afcc9aSBarry 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 80785afcc9aSBarry Smith DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection. 80885afcc9aSBarry Smith 809e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation() 81047c6ae99SBarry Smith 81147c6ae99SBarry Smith @*/ 812e727c939SJed Brown PetscErrorCode DMCreateInjection(DM dm1,DM dm2,VecScatter *ctx) 81347c6ae99SBarry Smith { 81447c6ae99SBarry Smith PetscErrorCode ierr; 81547c6ae99SBarry Smith 81647c6ae99SBarry Smith PetscFunctionBegin; 817171400e9SBarry Smith PetscValidHeaderSpecific(dm1,DM_CLASSID,1); 818171400e9SBarry Smith PetscValidHeaderSpecific(dm2,DM_CLASSID,2); 81947c6ae99SBarry Smith ierr = (*dm1->ops->getinjection)(dm1,dm2,ctx);CHKERRQ(ierr); 82047c6ae99SBarry Smith PetscFunctionReturn(0); 82147c6ae99SBarry Smith } 82247c6ae99SBarry Smith 82347c6ae99SBarry Smith #undef __FUNCT__ 824e727c939SJed Brown #define __FUNCT__ "DMCreateColoring" 825d1e2c406SBarry Smith /*@C 826e727c939SJed Brown DMCreateColoring - Gets coloring for a DMDA or DMComposite 82747c6ae99SBarry Smith 82847c6ae99SBarry Smith Collective on DM 82947c6ae99SBarry Smith 83047c6ae99SBarry Smith Input Parameter: 83147c6ae99SBarry Smith + dm - the DM object 83247c6ae99SBarry Smith . ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL 83347c6ae99SBarry Smith - matype - either MATAIJ or MATBAIJ 83447c6ae99SBarry Smith 83547c6ae99SBarry Smith Output Parameter: 83647c6ae99SBarry Smith . coloring - the coloring 83747c6ae99SBarry Smith 83847c6ae99SBarry Smith Level: developer 83947c6ae99SBarry Smith 840e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix() 84147c6ae99SBarry Smith 842aab9d709SJed Brown @*/ 84319fd82e9SBarry Smith PetscErrorCode DMCreateColoring(DM dm,ISColoringType ctype,MatType mtype,ISColoring *coloring) 84447c6ae99SBarry Smith { 84547c6ae99SBarry Smith PetscErrorCode ierr; 84647c6ae99SBarry Smith 84747c6ae99SBarry Smith PetscFunctionBegin; 848171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 84947c6ae99SBarry Smith if (!dm->ops->getcoloring) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No coloring for this type of DM yet"); 85047c6ae99SBarry Smith ierr = (*dm->ops->getcoloring)(dm,ctype,mtype,coloring);CHKERRQ(ierr); 85147c6ae99SBarry Smith PetscFunctionReturn(0); 85247c6ae99SBarry Smith } 85347c6ae99SBarry Smith 85447c6ae99SBarry Smith #undef __FUNCT__ 855950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix" 85647c6ae99SBarry Smith /*@C 857950540a4SJed Brown DMCreateMatrix - Gets empty Jacobian for a DMDA or DMComposite 85847c6ae99SBarry Smith 85947c6ae99SBarry Smith Collective on DM 86047c6ae99SBarry Smith 86147c6ae99SBarry Smith Input Parameter: 86247c6ae99SBarry Smith + dm - the DM object 86347c6ae99SBarry Smith - mtype - Supported types are MATSEQAIJ, MATMPIAIJ, MATSEQBAIJ, MATMPIBAIJ, or 86494013140SBarry Smith any type which inherits from one of these (such as MATAIJ) 86547c6ae99SBarry Smith 86647c6ae99SBarry Smith Output Parameter: 86747c6ae99SBarry Smith . mat - the empty Jacobian 86847c6ae99SBarry Smith 869073dac72SJed Brown Level: beginner 87047c6ae99SBarry Smith 87194013140SBarry Smith Notes: This properly preallocates the number of nonzeros in the sparse matrix so you 87294013140SBarry Smith do not need to do it yourself. 87394013140SBarry Smith 87494013140SBarry Smith By default it also sets the nonzero structure and puts in the zero entries. To prevent setting 875aa219208SBarry Smith the nonzero pattern call DMDASetMatPreallocateOnly() 87694013140SBarry Smith 87794013140SBarry 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 87894013140SBarry Smith internally by PETSc. 87994013140SBarry Smith 88094013140SBarry Smith For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires 881aa219208SBarry Smith the indices for the global numbering for DMDAs which is complicated. 88294013140SBarry Smith 883e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation() 88447c6ae99SBarry Smith 885aab9d709SJed Brown @*/ 88619fd82e9SBarry Smith PetscErrorCode DMCreateMatrix(DM dm,MatType mtype,Mat *mat) 88747c6ae99SBarry Smith { 88847c6ae99SBarry Smith PetscErrorCode ierr; 88947c6ae99SBarry Smith 89047c6ae99SBarry Smith PetscFunctionBegin; 891171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 892519f805aSKarl Rupp #if !defined(PETSC_USE_DYNAMIC_LIBRARIES) 893235683edSBarry Smith ierr = MatInitializePackage(PETSC_NULL);CHKERRQ(ierr); 894235683edSBarry Smith #endif 895c7b7c8a4SJed Brown PetscValidHeaderSpecific(dm,DM_CLASSID,1); 896c7b7c8a4SJed Brown PetscValidPointer(mat,3); 897073dac72SJed Brown if (dm->mattype) { 89825296bd5SBarry Smith ierr = (*dm->ops->creatematrix)(dm,dm->mattype,mat);CHKERRQ(ierr); 899073dac72SJed Brown } else { 90025296bd5SBarry Smith ierr = (*dm->ops->creatematrix)(dm,mtype,mat);CHKERRQ(ierr); 901c7b7c8a4SJed Brown } 90247c6ae99SBarry Smith PetscFunctionReturn(0); 90347c6ae99SBarry Smith } 90447c6ae99SBarry Smith 90547c6ae99SBarry Smith #undef __FUNCT__ 906732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly" 907732e2eb9SMatthew G Knepley /*@ 908950540a4SJed Brown DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly 909732e2eb9SMatthew G Knepley preallocated but the nonzero structure and zero values will not be set. 910732e2eb9SMatthew G Knepley 911732e2eb9SMatthew G Knepley Logically Collective on DMDA 912732e2eb9SMatthew G Knepley 913732e2eb9SMatthew G Knepley Input Parameter: 914732e2eb9SMatthew G Knepley + dm - the DM 915732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation 916732e2eb9SMatthew G Knepley 917732e2eb9SMatthew G Knepley Level: developer 918950540a4SJed Brown .seealso DMCreateMatrix() 919732e2eb9SMatthew G Knepley @*/ 920732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only) 921732e2eb9SMatthew G Knepley { 922732e2eb9SMatthew G Knepley PetscFunctionBegin; 923732e2eb9SMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 924732e2eb9SMatthew G Knepley dm->prealloc_only = only; 925732e2eb9SMatthew G Knepley PetscFunctionReturn(0); 926732e2eb9SMatthew G Knepley } 927732e2eb9SMatthew G Knepley 928732e2eb9SMatthew G Knepley #undef __FUNCT__ 929a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray" 930a89ea682SMatthew G Knepley /*@C 931aa1993deSMatthew G Knepley DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray() 932a89ea682SMatthew G Knepley 933a89ea682SMatthew G Knepley Not Collective 934a89ea682SMatthew G Knepley 935a89ea682SMatthew G Knepley Input Parameters: 936a89ea682SMatthew G Knepley + dm - the DM object 937aa1993deSMatthew G Knepley . count - The minium size 938aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT) 939a89ea682SMatthew G Knepley 940a89ea682SMatthew G Knepley Output Parameter: 941a89ea682SMatthew G Knepley . array - the work array 942a89ea682SMatthew G Knepley 943a89ea682SMatthew G Knepley Level: developer 944a89ea682SMatthew G Knepley 945a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate() 946a89ea682SMatthew G Knepley @*/ 947aa1993deSMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem) 948a89ea682SMatthew G Knepley { 949a89ea682SMatthew G Knepley PetscErrorCode ierr; 950aa1993deSMatthew G Knepley DMWorkLink link; 951aa1993deSMatthew G Knepley size_t size; 952a89ea682SMatthew G Knepley 953a89ea682SMatthew G Knepley PetscFunctionBegin; 954a89ea682SMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 955aa1993deSMatthew G Knepley PetscValidPointer(mem,4); 956aa1993deSMatthew G Knepley if (dm->workin) { 957aa1993deSMatthew G Knepley link = dm->workin; 958aa1993deSMatthew G Knepley dm->workin = dm->workin->next; 959aa1993deSMatthew G Knepley } else { 960aa1993deSMatthew G Knepley ierr = PetscNewLog(dm,struct _DMWorkLink,&link);CHKERRQ(ierr); 961a89ea682SMatthew G Knepley } 962aa1993deSMatthew G Knepley ierr = PetscDataTypeGetSize(dtype,&size);CHKERRQ(ierr); 963aa1993deSMatthew G Knepley if (size*count > link->bytes) { 964aa1993deSMatthew G Knepley ierr = PetscFree(link->mem);CHKERRQ(ierr); 965aa1993deSMatthew G Knepley ierr = PetscMalloc(size*count,&link->mem);CHKERRQ(ierr); 966aa1993deSMatthew G Knepley link->bytes = size*count; 967aa1993deSMatthew G Knepley } 968aa1993deSMatthew G Knepley link->next = dm->workout; 969aa1993deSMatthew G Knepley dm->workout = link; 970aa1993deSMatthew G Knepley *(void**)mem = link->mem; 971a89ea682SMatthew G Knepley PetscFunctionReturn(0); 972a89ea682SMatthew G Knepley } 973a89ea682SMatthew G Knepley 974aa1993deSMatthew G Knepley #undef __FUNCT__ 975aa1993deSMatthew G Knepley #define __FUNCT__ "DMRestoreWorkArray" 976aa1993deSMatthew G Knepley /*@C 977aa1993deSMatthew G Knepley DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray() 978aa1993deSMatthew G Knepley 979aa1993deSMatthew G Knepley Not Collective 980aa1993deSMatthew G Knepley 981aa1993deSMatthew G Knepley Input Parameters: 982aa1993deSMatthew G Knepley + dm - the DM object 983aa1993deSMatthew G Knepley . count - The minium size 984aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT) 985aa1993deSMatthew G Knepley 986aa1993deSMatthew G Knepley Output Parameter: 987aa1993deSMatthew G Knepley . array - the work array 988aa1993deSMatthew G Knepley 989aa1993deSMatthew G Knepley Level: developer 990aa1993deSMatthew G Knepley 991aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate() 992aa1993deSMatthew G Knepley @*/ 993aa1993deSMatthew G Knepley PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem) 994aa1993deSMatthew G Knepley { 995aa1993deSMatthew G Knepley DMWorkLink *p,link; 996aa1993deSMatthew G Knepley 997aa1993deSMatthew G Knepley PetscFunctionBegin; 998aa1993deSMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 999aa1993deSMatthew G Knepley PetscValidPointer(mem,4); 1000aa1993deSMatthew G Knepley for (p=&dm->workout; (link=*p); p=&link->next) { 1001aa1993deSMatthew G Knepley if (link->mem == *(void**)mem) { 1002aa1993deSMatthew G Knepley *p = link->next; 1003aa1993deSMatthew G Knepley link->next = dm->workin; 1004aa1993deSMatthew G Knepley dm->workin = link; 1005aa1993deSMatthew G Knepley *(void**)mem = PETSC_NULL; 1006aa1993deSMatthew G Knepley PetscFunctionReturn(0); 1007aa1993deSMatthew G Knepley } 1008aa1993deSMatthew G Knepley } 1009aa1993deSMatthew G Knepley SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out"); 1010aa1993deSMatthew G Knepley PetscFunctionReturn(0); 1011aa1993deSMatthew G Knepley } 1012e7c4fc90SDmitry Karpeev 1013e7c4fc90SDmitry Karpeev #undef __FUNCT__ 1014435a35e8SMatthew G Knepley #define __FUNCT__ "DMSetNullSpaceConstructor" 1015435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace)) 1016435a35e8SMatthew G Knepley { 1017435a35e8SMatthew G Knepley PetscFunctionBegin; 1018435a35e8SMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 1019435a35e8SMatthew G Knepley if (field >= 10) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field); 1020435a35e8SMatthew G Knepley dm->nullspaceConstructors[field] = nullsp; 1021435a35e8SMatthew G Knepley PetscFunctionReturn(0); 1022435a35e8SMatthew G Knepley } 1023435a35e8SMatthew G Knepley 1024435a35e8SMatthew G Knepley #undef __FUNCT__ 10254d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS" 10264f3b5142SJed Brown /*@C 10274d343eeaSMatthew G Knepley DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field 10284d343eeaSMatthew G Knepley 10294d343eeaSMatthew G Knepley Not collective 10304d343eeaSMatthew G Knepley 10314d343eeaSMatthew G Knepley Input Parameter: 10324d343eeaSMatthew G Knepley . dm - the DM object 10334d343eeaSMatthew G Knepley 10344d343eeaSMatthew G Knepley Output Parameters: 103521c9b008SJed Brown + numFields - The number of fields (or PETSC_NULL if not requested) 103637d0c07bSMatthew G Knepley . fieldNames - The name for each field (or PETSC_NULL if not requested) 103721c9b008SJed Brown - fields - The global indices for each field (or PETSC_NULL if not requested) 10384d343eeaSMatthew G Knepley 10394d343eeaSMatthew G Knepley Level: intermediate 10404d343eeaSMatthew G Knepley 104121c9b008SJed Brown Notes: 104221c9b008SJed Brown The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with 104321c9b008SJed Brown PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with 104421c9b008SJed Brown PetscFree(). 104521c9b008SJed Brown 10464d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix() 10474d343eeaSMatthew G Knepley @*/ 104837d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields) 10494d343eeaSMatthew G Knepley { 105037d0c07bSMatthew G Knepley PetscSection section, sectionGlobal; 10514d343eeaSMatthew G Knepley PetscErrorCode ierr; 10524d343eeaSMatthew G Knepley 10534d343eeaSMatthew G Knepley PetscFunctionBegin; 10544d343eeaSMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 105569ca1f37SDmitry Karpeev if (numFields) { 105669ca1f37SDmitry Karpeev PetscValidPointer(numFields,2); 105769ca1f37SDmitry Karpeev *numFields = 0; 105869ca1f37SDmitry Karpeev } 105937d0c07bSMatthew G Knepley if (fieldNames) { 106037d0c07bSMatthew G Knepley PetscValidPointer(fieldNames,3); 106137d0c07bSMatthew G Knepley *fieldNames = PETSC_NULL; 106269ca1f37SDmitry Karpeev } 106369ca1f37SDmitry Karpeev if (fields) { 106469ca1f37SDmitry Karpeev PetscValidPointer(fields,4); 106569ca1f37SDmitry Karpeev *fields = PETSC_NULL; 106669ca1f37SDmitry Karpeev } 106737d0c07bSMatthew G Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 106837d0c07bSMatthew G Knepley if (section) { 106937d0c07bSMatthew G Knepley PetscInt *fieldSizes, **fieldIndices; 107037d0c07bSMatthew G Knepley PetscInt nF, f, pStart, pEnd, p; 107137d0c07bSMatthew G Knepley 107237d0c07bSMatthew G Knepley ierr = DMGetDefaultGlobalSection(dm, §ionGlobal);CHKERRQ(ierr); 107337d0c07bSMatthew G Knepley ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr); 107437d0c07bSMatthew G Knepley ierr = PetscMalloc2(nF,PetscInt,&fieldSizes,nF,PetscInt *,&fieldIndices);CHKERRQ(ierr); 107537d0c07bSMatthew G Knepley ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr); 107637d0c07bSMatthew G Knepley for (f = 0; f < nF; ++f) { 107737d0c07bSMatthew G Knepley fieldSizes[f] = 0; 107837d0c07bSMatthew G Knepley } 107937d0c07bSMatthew G Knepley for (p = pStart; p < pEnd; ++p) { 108037d0c07bSMatthew G Knepley PetscInt gdof; 108137d0c07bSMatthew G Knepley 108237d0c07bSMatthew G Knepley ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr); 108337d0c07bSMatthew G Knepley if (gdof > 0) { 108437d0c07bSMatthew G Knepley for (f = 0; f < nF; ++f) { 108537d0c07bSMatthew G Knepley PetscInt fdof, fcdof; 108637d0c07bSMatthew G Knepley 108737d0c07bSMatthew G Knepley ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr); 108837d0c07bSMatthew G Knepley ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr); 108937d0c07bSMatthew G Knepley fieldSizes[f] += fdof-fcdof; 109037d0c07bSMatthew G Knepley } 109137d0c07bSMatthew G Knepley } 109237d0c07bSMatthew G Knepley } 109337d0c07bSMatthew G Knepley for (f = 0; f < nF; ++f) { 109437d0c07bSMatthew G Knepley ierr = PetscMalloc(fieldSizes[f] * sizeof(PetscInt), &fieldIndices[f]);CHKERRQ(ierr); 109537d0c07bSMatthew G Knepley fieldSizes[f] = 0; 109637d0c07bSMatthew G Knepley } 109737d0c07bSMatthew G Knepley for (p = pStart; p < pEnd; ++p) { 109837d0c07bSMatthew G Knepley PetscInt gdof, goff; 109937d0c07bSMatthew G Knepley 110037d0c07bSMatthew G Knepley ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr); 110137d0c07bSMatthew G Knepley if (gdof > 0) { 110237d0c07bSMatthew G Knepley ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr); 110337d0c07bSMatthew G Knepley for (f = 0; f < nF; ++f) { 110437d0c07bSMatthew G Knepley PetscInt fdof, fcdof, fc; 110537d0c07bSMatthew G Knepley 110637d0c07bSMatthew G Knepley ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr); 110737d0c07bSMatthew G Knepley ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr); 110837d0c07bSMatthew G Knepley for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) { 110937d0c07bSMatthew G Knepley fieldIndices[f][fieldSizes[f]] = goff++; 111037d0c07bSMatthew G Knepley } 111137d0c07bSMatthew G Knepley } 111237d0c07bSMatthew G Knepley } 111337d0c07bSMatthew G Knepley } 111437d0c07bSMatthew G Knepley if (numFields) {*numFields = nF;} 111537d0c07bSMatthew G Knepley if (fieldNames) { 111637d0c07bSMatthew G Knepley ierr = PetscMalloc(nF * sizeof(char *), fieldNames);CHKERRQ(ierr); 111737d0c07bSMatthew G Knepley for (f = 0; f < nF; ++f) { 111837d0c07bSMatthew G Knepley const char *fieldName; 111937d0c07bSMatthew G Knepley 112037d0c07bSMatthew G Knepley ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr); 112137d0c07bSMatthew G Knepley ierr = PetscStrallocpy(fieldName, (char **) &(*fieldNames)[f]);CHKERRQ(ierr); 112237d0c07bSMatthew G Knepley } 112337d0c07bSMatthew G Knepley } 112437d0c07bSMatthew G Knepley if (fields) { 112537d0c07bSMatthew G Knepley ierr = PetscMalloc(nF * sizeof(IS), fields);CHKERRQ(ierr); 112637d0c07bSMatthew G Knepley for (f = 0; f < nF; ++f) { 112737d0c07bSMatthew G Knepley ierr = ISCreateGeneral(((PetscObject) dm)->comm, fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr); 112837d0c07bSMatthew G Knepley } 112937d0c07bSMatthew G Knepley } 113037d0c07bSMatthew G Knepley ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr); 113137d0c07bSMatthew G Knepley } else { 113237d0c07bSMatthew G Knepley if (dm->ops->createfieldis) {ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);} 113369ca1f37SDmitry Karpeev } 11344d343eeaSMatthew G Knepley PetscFunctionReturn(0); 11354d343eeaSMatthew G Knepley } 11364d343eeaSMatthew G Knepley 1137a89ea682SMatthew G Knepley #undef __FUNCT__ 113816621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecompositionDM" 1139e7c4fc90SDmitry Karpeev /*@C 114016621825SDmitry Karpeev DMCreateFieldDecompositionDM - creates a DM that encapsulates a decomposition of the original DM into fields. 114116621825SDmitry Karpeev 114216621825SDmitry Karpeev Not Collective 114316621825SDmitry Karpeev 114416621825SDmitry Karpeev Input Parameters: 114516621825SDmitry Karpeev + dm - the DM object 114616621825SDmitry Karpeev - name - the name of the field decomposition 114716621825SDmitry Karpeev 114816621825SDmitry Karpeev Output Parameter: 114916621825SDmitry Karpeev . ddm - the field decomposition DM (PETSC_NULL, if no such decomposition is known) 115016621825SDmitry Karpeev 115116621825SDmitry Karpeev Level: advanced 115216621825SDmitry Karpeev 115316621825SDmitry Karpeev .seealso DMDestroy(), DMCreate(), DMCreateFieldDecomposition(), DMCreateDomainDecompositionDM() 115416621825SDmitry Karpeev @*/ 115516621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecompositionDM(DM dm, const char* name, DM *ddm) 115616621825SDmitry Karpeev { 115716621825SDmitry Karpeev PetscErrorCode ierr; 115816621825SDmitry Karpeev 115916621825SDmitry Karpeev PetscFunctionBegin; 116016621825SDmitry Karpeev PetscValidHeaderSpecific(dm,DM_CLASSID,1); 116116621825SDmitry Karpeev PetscValidCharPointer(name,2); 116216621825SDmitry Karpeev PetscValidPointer(ddm,3); 116316621825SDmitry Karpeev *ddm = PETSC_NULL; 1164731c8d9eSDmitry Karpeev if (dm->ops->createfielddecompositiondm) { 116516621825SDmitry Karpeev ierr = (*dm->ops->createfielddecompositiondm)(dm,name,ddm);CHKERRQ(ierr); 116616621825SDmitry Karpeev } 116716621825SDmitry Karpeev PetscFunctionReturn(0); 116816621825SDmitry Karpeev } 116916621825SDmitry Karpeev 117016621825SDmitry Karpeev #undef __FUNCT__ 117116621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecomposition" 117216621825SDmitry Karpeev /*@C 117316621825SDmitry Karpeev DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems 117416621825SDmitry Karpeev corresponding to different fields: each IS contains the global indices of the dofs of the 117516621825SDmitry Karpeev corresponding field. The optional list of DMs define the DM for each subproblem. 1176e7c4fc90SDmitry Karpeev Generalizes DMCreateFieldIS(). 1177e7c4fc90SDmitry Karpeev 1178e7c4fc90SDmitry Karpeev Not collective 1179e7c4fc90SDmitry Karpeev 1180e7c4fc90SDmitry Karpeev Input Parameter: 1181e7c4fc90SDmitry Karpeev . dm - the DM object 1182e7c4fc90SDmitry Karpeev 1183e7c4fc90SDmitry Karpeev Output Parameters: 118416621825SDmitry Karpeev + len - The number of subproblems in the field decomposition (or PETSC_NULL if not requested) 118516621825SDmitry Karpeev . namelist - The name for each field (or PETSC_NULL if not requested) 118616621825SDmitry Karpeev . islist - The global indices for each field (or PETSC_NULL if not requested) 118716621825SDmitry Karpeev - dmlist - The DMs for each field subproblem (or PETSC_NULL, if not requested; if PETSC_NULL is returned, no DMs are defined) 1188e7c4fc90SDmitry Karpeev 1189e7c4fc90SDmitry Karpeev Level: intermediate 1190e7c4fc90SDmitry Karpeev 1191e7c4fc90SDmitry Karpeev Notes: 1192e7c4fc90SDmitry Karpeev The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with 1193e7c4fc90SDmitry Karpeev PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(), 1194e7c4fc90SDmitry Karpeev and all of the arrays should be freed with PetscFree(). 1195e7c4fc90SDmitry Karpeev 1196e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS() 1197e7c4fc90SDmitry Karpeev @*/ 119816621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist) 1199e7c4fc90SDmitry Karpeev { 1200e7c4fc90SDmitry Karpeev PetscErrorCode ierr; 1201e7c4fc90SDmitry Karpeev 1202e7c4fc90SDmitry Karpeev PetscFunctionBegin; 1203e7c4fc90SDmitry Karpeev PetscValidHeaderSpecific(dm,DM_CLASSID,1); 1204731c8d9eSDmitry Karpeev if (len) {PetscValidPointer(len,2); *len = 0;} 1205731c8d9eSDmitry Karpeev if (namelist) {PetscValidPointer(namelist,3); *namelist = 0;} 1206731c8d9eSDmitry Karpeev if (islist) {PetscValidPointer(islist,4); *islist = 0;} 1207731c8d9eSDmitry Karpeev if (dmlist) {PetscValidPointer(dmlist,5); *dmlist = 0;} 120816621825SDmitry Karpeev if (!dm->ops->createfielddecomposition) { 1209435a35e8SMatthew G Knepley PetscSection section; 1210435a35e8SMatthew G Knepley PetscInt numFields, f; 1211435a35e8SMatthew G Knepley 1212435a35e8SMatthew G Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 1213435a35e8SMatthew G Knepley if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);} 1214435a35e8SMatthew G Knepley if (section && numFields && dm->ops->createsubdm) { 1215435a35e8SMatthew G Knepley *len = numFields; 1216435a35e8SMatthew G Knepley ierr = PetscMalloc3(numFields,char*,namelist,numFields,IS,islist,numFields,DM,dmlist);CHKERRQ(ierr); 1217435a35e8SMatthew G Knepley for (f = 0; f < numFields; ++f) { 1218435a35e8SMatthew G Knepley const char *fieldName; 1219435a35e8SMatthew G Knepley 1220435a35e8SMatthew G Knepley ierr = DMCreateSubDM(dm, 1, &f, &(*islist)[f], &(*dmlist)[f]);CHKERRQ(ierr); 1221435a35e8SMatthew G Knepley ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr); 1222435a35e8SMatthew G Knepley ierr = PetscStrallocpy(fieldName, (char **) &(*namelist)[f]);CHKERRQ(ierr); 1223435a35e8SMatthew G Knepley } 1224435a35e8SMatthew G Knepley } else { 122569ca1f37SDmitry Karpeev ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr); 1226e7c4fc90SDmitry Karpeev /* By default there are no DMs associated with subproblems. */ 1227e7c4fc90SDmitry Karpeev if (dmlist) *dmlist = PETSC_NULL; 1228e7c4fc90SDmitry Karpeev } 1229435a35e8SMatthew G Knepley } 1230e7c4fc90SDmitry Karpeev else { 123116621825SDmitry Karpeev ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr); 123216621825SDmitry Karpeev } 123316621825SDmitry Karpeev PetscFunctionReturn(0); 123416621825SDmitry Karpeev } 123516621825SDmitry Karpeev 123616621825SDmitry Karpeev #undef __FUNCT__ 1237435a35e8SMatthew G Knepley #define __FUNCT__ "DMCreateSubDM" 1238435a35e8SMatthew G Knepley /*@C 1239435a35e8SMatthew G Knepley DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in. 1240435a35e8SMatthew G Knepley The fields are defined by DMCreateFieldIS(). 1241435a35e8SMatthew G Knepley 1242435a35e8SMatthew G Knepley Not collective 1243435a35e8SMatthew G Knepley 1244435a35e8SMatthew G Knepley Input Parameters: 1245435a35e8SMatthew G Knepley + dm - the DM object 1246435a35e8SMatthew G Knepley . numFields - number of fields in this subproblem 1247435a35e8SMatthew G Knepley - len - The number of subproblems in the decomposition (or PETSC_NULL if not requested) 1248435a35e8SMatthew G Knepley 1249435a35e8SMatthew G Knepley Output Parameters: 1250435a35e8SMatthew G Knepley . is - The global indices for the subproblem 1251435a35e8SMatthew G Knepley - dm - The DM for the subproblem 1252435a35e8SMatthew G Knepley 1253435a35e8SMatthew G Knepley Level: intermediate 1254435a35e8SMatthew G Knepley 1255435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS() 1256435a35e8SMatthew G Knepley @*/ 1257435a35e8SMatthew G Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm) 1258435a35e8SMatthew G Knepley { 1259435a35e8SMatthew G Knepley PetscErrorCode ierr; 1260435a35e8SMatthew G Knepley 1261435a35e8SMatthew G Knepley PetscFunctionBegin; 1262435a35e8SMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 1263435a35e8SMatthew G Knepley PetscValidPointer(fields,3); 1264435a35e8SMatthew G Knepley if (is) {PetscValidPointer(is,4);} 1265435a35e8SMatthew G Knepley if (subdm) {PetscValidPointer(subdm,5);} 1266435a35e8SMatthew G Knepley if (dm->ops->createsubdm) { 1267435a35e8SMatthew G Knepley ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr); 1268435a35e8SMatthew G Knepley } else SETERRQ(((PetscObject) dm)->comm, PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined"); 1269435a35e8SMatthew G Knepley PetscFunctionReturn(0); 1270435a35e8SMatthew G Knepley } 1271435a35e8SMatthew G Knepley 1272435a35e8SMatthew G Knepley #undef __FUNCT__ 127316621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecompositionDM" 127416621825SDmitry Karpeev /*@C 127516621825SDmitry Karpeev DMCreateDomainDecompositionDM - creates a DM that encapsulates a decomposition of the original DM into subdomains. 127616621825SDmitry Karpeev 127716621825SDmitry Karpeev Not Collective 127816621825SDmitry Karpeev 127916621825SDmitry Karpeev Input Parameters: 128016621825SDmitry Karpeev + dm - the DM object 128116621825SDmitry Karpeev - name - the name of the subdomain decomposition 128216621825SDmitry Karpeev 128316621825SDmitry Karpeev Output Parameter: 128416621825SDmitry Karpeev . ddm - the subdomain decomposition DM (PETSC_NULL, if no such decomposition is known) 128516621825SDmitry Karpeev 128616621825SDmitry Karpeev Level: advanced 128716621825SDmitry Karpeev 128816621825SDmitry Karpeev .seealso DMDestroy(), DMCreate(), DMCreateFieldDecomposition(), DMCreateDomainDecompositionDM() 128916621825SDmitry Karpeev @*/ 129016621825SDmitry Karpeev PetscErrorCode DMCreateDomainDecompositionDM(DM dm, const char* name, DM *ddm) 129116621825SDmitry Karpeev { 129216621825SDmitry Karpeev PetscErrorCode ierr; 129316621825SDmitry Karpeev 129416621825SDmitry Karpeev PetscFunctionBegin; 129516621825SDmitry Karpeev PetscValidHeaderSpecific(dm,DM_CLASSID,1); 129616621825SDmitry Karpeev PetscValidCharPointer(name,2); 129716621825SDmitry Karpeev PetscValidPointer(ddm,3); 129816621825SDmitry Karpeev *ddm = PETSC_NULL; 1299731c8d9eSDmitry Karpeev if (dm->ops->createdomaindecompositiondm) { 130016621825SDmitry Karpeev ierr = (*dm->ops->createdomaindecompositiondm)(dm,name,ddm);CHKERRQ(ierr); 130116621825SDmitry Karpeev } 130216621825SDmitry Karpeev PetscFunctionReturn(0); 130316621825SDmitry Karpeev } 130416621825SDmitry Karpeev 130516621825SDmitry Karpeev #undef __FUNCT__ 130616621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecomposition" 130716621825SDmitry Karpeev /*@C 13088d4ac253SDmitry Karpeev DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems 13098d4ac253SDmitry Karpeev corresponding to restrictions to pairs nested subdomains: each IS contains the global 13108d4ac253SDmitry Karpeev indices of the dofs of the corresponding subdomains. The inner subdomains conceptually 13118d4ac253SDmitry Karpeev define a nonoverlapping covering, while outer subdomains can overlap. 13128d4ac253SDmitry Karpeev The optional list of DMs define the DM for each subproblem. 131316621825SDmitry Karpeev 131416621825SDmitry Karpeev Not collective 131516621825SDmitry Karpeev 131616621825SDmitry Karpeev Input Parameter: 131716621825SDmitry Karpeev . dm - the DM object 131816621825SDmitry Karpeev 131916621825SDmitry Karpeev Output Parameters: 132016621825SDmitry Karpeev + len - The number of subproblems in the domain decomposition (or PETSC_NULL if not requested) 132116621825SDmitry Karpeev . namelist - The name for each subdomain (or PETSC_NULL if not requested) 13228d4ac253SDmitry Karpeev . innerislist - The global indices for each inner subdomain (or PETSC_NULL, if not requested) 13238d4ac253SDmitry Karpeev . outerislist - The global indices for each outer subdomain (or PETSC_NULL, if not requested) 132416621825SDmitry Karpeev - dmlist - The DMs for each subdomain subproblem (or PETSC_NULL, if not requested; if PETSC_NULL is returned, no DMs are defined) 132516621825SDmitry Karpeev 132616621825SDmitry Karpeev Level: intermediate 132716621825SDmitry Karpeev 132816621825SDmitry Karpeev Notes: 132916621825SDmitry Karpeev The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with 133016621825SDmitry Karpeev PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(), 133116621825SDmitry Karpeev and all of the arrays should be freed with PetscFree(). 133216621825SDmitry Karpeev 13338d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition() 133416621825SDmitry Karpeev @*/ 13358d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist) 133616621825SDmitry Karpeev { 133716621825SDmitry Karpeev PetscErrorCode ierr; 1338be081cd6SPeter Brune DMSubDomainHookLink link; 1339be081cd6SPeter Brune PetscInt i,l; 134016621825SDmitry Karpeev 134116621825SDmitry Karpeev PetscFunctionBegin; 134216621825SDmitry Karpeev PetscValidHeaderSpecific(dm,DM_CLASSID,1); 134314a18fd3SPeter Brune if (len) {PetscValidPointer(len,2); *len = 0;} 134416621825SDmitry Karpeev if (namelist) {PetscValidPointer(namelist,3); *namelist = PETSC_NULL;} 13458d4ac253SDmitry Karpeev if (innerislist) {PetscValidPointer(innerislist,4); *innerislist = PETSC_NULL;} 13468d4ac253SDmitry Karpeev if (outerislist) {PetscValidPointer(outerislist,5); *outerislist = PETSC_NULL;} 13478d4ac253SDmitry Karpeev if (dmlist) {PetscValidPointer(dmlist,6); *dmlist = PETSC_NULL;} 134816621825SDmitry Karpeev if (dm->ops->createdomaindecomposition) { 1349be081cd6SPeter Brune ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr); 135014a18fd3SPeter Brune /* copy subdomain hooks and context over to the subdomain DMs */ 135114a18fd3SPeter Brune if (dmlist) { 1352be081cd6SPeter Brune for (i = 0; i < l; i++) { 1353be081cd6SPeter Brune for (link=dm->subdomainhook; link; link=link->next) { 1354be081cd6SPeter Brune if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);} 1355be081cd6SPeter Brune } 1356d425c654SPeter Brune (*dmlist)[i]->ctx = dm->ctx; 1357e7c4fc90SDmitry Karpeev } 135814a18fd3SPeter Brune } 135914a18fd3SPeter Brune if (len) *len = l; 136014a18fd3SPeter Brune } 1361e30e807fSPeter Brune PetscFunctionReturn(0); 1362e30e807fSPeter Brune } 1363e30e807fSPeter Brune 1364e30e807fSPeter Brune 1365e30e807fSPeter Brune #undef __FUNCT__ 1366e30e807fSPeter Brune #define __FUNCT__ "DMCreateDomainDecompositionScatters" 1367e30e807fSPeter Brune /*@C 1368e30e807fSPeter Brune DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector 1369e30e807fSPeter Brune 1370e30e807fSPeter Brune Not collective 1371e30e807fSPeter Brune 1372e30e807fSPeter Brune Input Parameters: 1373e30e807fSPeter Brune + dm - the DM object 1374e30e807fSPeter Brune . n - the number of subdomain scatters 1375e30e807fSPeter Brune - subdms - the local subdomains 1376e30e807fSPeter Brune 1377e30e807fSPeter Brune Output Parameters: 1378e30e807fSPeter Brune + n - the number of scatters returned 1379e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain 1380e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain 1381e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts) 1382e30e807fSPeter Brune 1383e30e807fSPeter Brune Notes: This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution 1384e30e807fSPeter Brune of general nonlinear problems with overlapping subdomain methods. While merely having index sets that enable subsets 1385e30e807fSPeter Brune of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of 1386e30e807fSPeter Brune solution and residual data. 1387e30e807fSPeter Brune 1388e30e807fSPeter Brune Level: developer 1389e30e807fSPeter Brune 1390e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS() 1391e30e807fSPeter Brune @*/ 1392e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat) 1393e30e807fSPeter Brune { 1394e30e807fSPeter Brune PetscErrorCode ierr; 1395e30e807fSPeter Brune 1396e30e807fSPeter Brune PetscFunctionBegin; 1397e30e807fSPeter Brune PetscValidHeaderSpecific(dm,DM_CLASSID,1); 1398e30e807fSPeter Brune PetscValidPointer(subdms,3); 1399e30e807fSPeter Brune PetscValidPointer(iscat,4); 1400e30e807fSPeter Brune PetscValidPointer(oscat,5); 1401e30e807fSPeter Brune PetscValidPointer(gscat,6); 1402e30e807fSPeter Brune if (dm->ops->createddscatters) { 1403e30e807fSPeter Brune ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr); 1404e30e807fSPeter Brune } else SETERRQ(((PetscObject) dm)->comm, PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionLocalScatter implementation defined"); 1405e7c4fc90SDmitry Karpeev PetscFunctionReturn(0); 1406e7c4fc90SDmitry Karpeev } 1407e7c4fc90SDmitry Karpeev 1408731c8d9eSDmitry Karpeev #undef __FUNCT__ 140947c6ae99SBarry Smith #define __FUNCT__ "DMRefine" 141047c6ae99SBarry Smith /*@ 141147c6ae99SBarry Smith DMRefine - Refines a DM object 141247c6ae99SBarry Smith 141347c6ae99SBarry Smith Collective on DM 141447c6ae99SBarry Smith 141547c6ae99SBarry Smith Input Parameter: 141647c6ae99SBarry Smith + dm - the DM object 141791d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL) 141847c6ae99SBarry Smith 141947c6ae99SBarry Smith Output Parameter: 1420ae0a1c52SMatthew G Knepley . dmf - the refined DM, or PETSC_NULL 1421ae0a1c52SMatthew G Knepley 1422ae0a1c52SMatthew G Knepley Note: If no refinement was done, the return value is PETSC_NULL 142347c6ae99SBarry Smith 142447c6ae99SBarry Smith Level: developer 142547c6ae99SBarry Smith 1426e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation() 142747c6ae99SBarry Smith @*/ 14287087cfbeSBarry Smith PetscErrorCode DMRefine(DM dm,MPI_Comm comm,DM *dmf) 142947c6ae99SBarry Smith { 143047c6ae99SBarry Smith PetscErrorCode ierr; 1431c833c3b5SJed Brown DMRefineHookLink link; 143247c6ae99SBarry Smith 143347c6ae99SBarry Smith PetscFunctionBegin; 1434732e2eb9SMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 143547c6ae99SBarry Smith ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr); 14364057135bSMatthew G Knepley if (*dmf) { 143743842a1eSJed Brown (*dmf)->ops->creatematrix = dm->ops->creatematrix; 14388cd211a4SJed Brown ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr); 1439644e2e5bSBarry Smith (*dmf)->ctx = dm->ctx; 14400598a293SJed Brown (*dmf)->leveldown = dm->leveldown; 1441656b349aSBarry Smith (*dmf)->levelup = dm->levelup + 1; 1442e4b4b23bSJed Brown ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr); 1443c833c3b5SJed Brown for (link=dm->refinehook; link; link=link->next) { 1444c833c3b5SJed Brown if (link->refinehook) {ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);} 1445c833c3b5SJed Brown } 1446c833c3b5SJed Brown } 1447c833c3b5SJed Brown PetscFunctionReturn(0); 1448c833c3b5SJed Brown } 1449c833c3b5SJed Brown 1450c833c3b5SJed Brown #undef __FUNCT__ 1451c833c3b5SJed Brown #define __FUNCT__ "DMRefineHookAdd" 1452c833c3b5SJed Brown /*@ 1453c833c3b5SJed Brown DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid 1454c833c3b5SJed Brown 1455c833c3b5SJed Brown Logically Collective 1456c833c3b5SJed Brown 1457c833c3b5SJed Brown Input Arguments: 1458c833c3b5SJed Brown + coarse - nonlinear solver context on which to run a hook when restricting to a coarser level 1459c833c3b5SJed Brown . refinehook - function to run when setting up a coarser level 1460c833c3b5SJed Brown . interphook - function to run to update data on finer levels (once per SNESSolve()) 1461c833c3b5SJed Brown - ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL) 1462c833c3b5SJed Brown 1463c833c3b5SJed Brown Calling sequence of refinehook: 1464c833c3b5SJed Brown $ refinehook(DM coarse,DM fine,void *ctx); 1465c833c3b5SJed Brown 1466c833c3b5SJed Brown + coarse - coarse level DM 1467c833c3b5SJed Brown . fine - fine level DM to interpolate problem to 1468c833c3b5SJed Brown - ctx - optional user-defined function context 1469c833c3b5SJed Brown 1470c833c3b5SJed Brown Calling sequence for interphook: 1471c833c3b5SJed Brown $ interphook(DM coarse,Mat interp,DM fine,void *ctx) 1472c833c3b5SJed Brown 1473c833c3b5SJed Brown + coarse - coarse level DM 1474c833c3b5SJed Brown . interp - matrix interpolating a coarse-level solution to the finer grid 1475c833c3b5SJed Brown . fine - fine level DM to update 1476c833c3b5SJed Brown - ctx - optional user-defined function context 1477c833c3b5SJed Brown 1478c833c3b5SJed Brown Level: advanced 1479c833c3b5SJed Brown 1480c833c3b5SJed Brown Notes: 1481c833c3b5SJed Brown This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing 1482c833c3b5SJed Brown 1483c833c3b5SJed Brown If this function is called multiple times, the hooks will be run in the order they are added. 1484c833c3b5SJed Brown 1485c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate() 1486c833c3b5SJed Brown @*/ 1487c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx) 1488c833c3b5SJed Brown { 1489c833c3b5SJed Brown PetscErrorCode ierr; 1490c833c3b5SJed Brown DMRefineHookLink link,*p; 1491c833c3b5SJed Brown 1492c833c3b5SJed Brown PetscFunctionBegin; 1493c833c3b5SJed Brown PetscValidHeaderSpecific(coarse,DM_CLASSID,1); 1494c833c3b5SJed Brown for (p=&coarse->refinehook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */ 1495c833c3b5SJed Brown ierr = PetscMalloc(sizeof(struct _DMRefineHookLink),&link);CHKERRQ(ierr); 1496c833c3b5SJed Brown link->refinehook = refinehook; 1497c833c3b5SJed Brown link->interphook = interphook; 1498c833c3b5SJed Brown link->ctx = ctx; 1499c833c3b5SJed Brown link->next = PETSC_NULL; 1500c833c3b5SJed Brown *p = link; 1501c833c3b5SJed Brown PetscFunctionReturn(0); 1502c833c3b5SJed Brown } 1503c833c3b5SJed Brown 1504c833c3b5SJed Brown #undef __FUNCT__ 1505c833c3b5SJed Brown #define __FUNCT__ "DMInterpolate" 1506c833c3b5SJed Brown /*@ 1507c833c3b5SJed Brown DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd() 1508c833c3b5SJed Brown 1509c833c3b5SJed Brown Collective if any hooks are 1510c833c3b5SJed Brown 1511c833c3b5SJed Brown Input Arguments: 1512c833c3b5SJed Brown + coarse - coarser DM to use as a base 1513c833c3b5SJed Brown . restrct - interpolation matrix, apply using MatInterpolate() 1514c833c3b5SJed Brown - fine - finer DM to update 1515c833c3b5SJed Brown 1516c833c3b5SJed Brown Level: developer 1517c833c3b5SJed Brown 1518c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate() 1519c833c3b5SJed Brown @*/ 1520c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine) 1521c833c3b5SJed Brown { 1522c833c3b5SJed Brown PetscErrorCode ierr; 1523c833c3b5SJed Brown DMRefineHookLink link; 1524c833c3b5SJed Brown 1525c833c3b5SJed Brown PetscFunctionBegin; 1526c833c3b5SJed Brown for (link=fine->refinehook; link; link=link->next) { 1527c833c3b5SJed Brown if (link->interphook) {ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);} 15284057135bSMatthew G Knepley } 152947c6ae99SBarry Smith PetscFunctionReturn(0); 153047c6ae99SBarry Smith } 153147c6ae99SBarry Smith 153247c6ae99SBarry Smith #undef __FUNCT__ 1533eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel" 1534eb3f98d2SBarry Smith /*@ 1535eb3f98d2SBarry Smith DMGetRefineLevel - Get's the number of refinements that have generated this DM. 1536eb3f98d2SBarry Smith 1537eb3f98d2SBarry Smith Not Collective 1538eb3f98d2SBarry Smith 1539eb3f98d2SBarry Smith Input Parameter: 1540eb3f98d2SBarry Smith . dm - the DM object 1541eb3f98d2SBarry Smith 1542eb3f98d2SBarry Smith Output Parameter: 1543eb3f98d2SBarry Smith . level - number of refinements 1544eb3f98d2SBarry Smith 1545eb3f98d2SBarry Smith Level: developer 1546eb3f98d2SBarry Smith 15476a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation() 1548eb3f98d2SBarry Smith 1549eb3f98d2SBarry Smith @*/ 1550eb3f98d2SBarry Smith PetscErrorCode DMGetRefineLevel(DM dm,PetscInt *level) 1551eb3f98d2SBarry Smith { 1552eb3f98d2SBarry Smith PetscFunctionBegin; 1553eb3f98d2SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 1554eb3f98d2SBarry Smith *level = dm->levelup; 1555eb3f98d2SBarry Smith PetscFunctionReturn(0); 1556eb3f98d2SBarry Smith } 1557eb3f98d2SBarry Smith 1558eb3f98d2SBarry Smith #undef __FUNCT__ 1559baf369e7SPeter Brune #define __FUNCT__ "DMGlobalToLocalHookAdd" 1560baf369e7SPeter Brune /*@ 1561baf369e7SPeter Brune DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called 1562baf369e7SPeter Brune 1563baf369e7SPeter Brune Logically Collective 1564baf369e7SPeter Brune 1565baf369e7SPeter Brune Input Arguments: 1566baf369e7SPeter Brune + dm - the DM 1567baf369e7SPeter Brune . beginhook - function to run at the beginning of DMGlobalToLocalBegin() 1568baf369e7SPeter Brune . endhook - function to run after DMGlobalToLocalEnd() has completed 1569baf369e7SPeter Brune - ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL) 1570baf369e7SPeter Brune 1571baf369e7SPeter Brune Calling sequence for beginhook: 1572baf369e7SPeter Brune $ beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx) 1573baf369e7SPeter Brune 1574baf369e7SPeter Brune + dm - global DM 1575baf369e7SPeter Brune . g - global vector 1576baf369e7SPeter Brune . mode - mode 1577baf369e7SPeter Brune . l - local vector 1578baf369e7SPeter Brune - ctx - optional user-defined function context 1579baf369e7SPeter Brune 1580baf369e7SPeter Brune 1581baf369e7SPeter Brune Calling sequence for endhook: 1582baf369e7SPeter Brune $ beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx) 1583baf369e7SPeter Brune 1584baf369e7SPeter Brune + global - global DM 1585baf369e7SPeter Brune - ctx - optional user-defined function context 1586baf369e7SPeter Brune 1587baf369e7SPeter Brune Level: advanced 1588baf369e7SPeter Brune 1589baf369e7SPeter Brune Notes: 1590baf369e7SPeter Brune This function is only needed if auxiliary data needs to be set up on coarse grids. 1591baf369e7SPeter Brune 1592baf369e7SPeter Brune If this function is called multiple times, the hooks will be run in the order they are added. 1593baf369e7SPeter Brune 1594baf369e7SPeter Brune In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to 1595baf369e7SPeter Brune extract the finest level information from its context (instead of from the SNES). 1596baf369e7SPeter Brune 1597baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate() 1598baf369e7SPeter Brune @*/ 1599baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx) 1600baf369e7SPeter Brune { 1601baf369e7SPeter Brune PetscErrorCode ierr; 1602baf369e7SPeter Brune DMGlobalToLocalHookLink link,*p; 1603baf369e7SPeter Brune 1604baf369e7SPeter Brune PetscFunctionBegin; 1605baf369e7SPeter Brune PetscValidHeaderSpecific(dm,DM_CLASSID,1); 1606baf369e7SPeter Brune for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */ 1607baf369e7SPeter Brune ierr = PetscMalloc(sizeof(struct _DMGlobalToLocalHookLink),&link);CHKERRQ(ierr); 1608baf369e7SPeter Brune link->beginhook = beginhook; 1609baf369e7SPeter Brune link->endhook = endhook; 1610baf369e7SPeter Brune link->ctx = ctx; 1611baf369e7SPeter Brune link->next = PETSC_NULL; 1612baf369e7SPeter Brune *p = link; 1613baf369e7SPeter Brune PetscFunctionReturn(0); 1614baf369e7SPeter Brune } 1615baf369e7SPeter Brune 1616baf369e7SPeter Brune #undef __FUNCT__ 161747c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin" 161847c6ae99SBarry Smith /*@ 161947c6ae99SBarry Smith DMGlobalToLocalBegin - Begins updating local vectors from global vector 162047c6ae99SBarry Smith 162147c6ae99SBarry Smith Neighbor-wise Collective on DM 162247c6ae99SBarry Smith 162347c6ae99SBarry Smith Input Parameters: 162447c6ae99SBarry Smith + dm - the DM object 162547c6ae99SBarry Smith . g - the global vector 162647c6ae99SBarry Smith . mode - INSERT_VALUES or ADD_VALUES 162747c6ae99SBarry Smith - l - the local vector 162847c6ae99SBarry Smith 162947c6ae99SBarry Smith 163047c6ae99SBarry Smith Level: beginner 163147c6ae99SBarry Smith 1632e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin() 163347c6ae99SBarry Smith 163447c6ae99SBarry Smith @*/ 16357087cfbeSBarry Smith PetscErrorCode DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l) 163647c6ae99SBarry Smith { 16377128ae9fSMatthew G Knepley PetscSF sf; 163847c6ae99SBarry Smith PetscErrorCode ierr; 1639baf369e7SPeter Brune DMGlobalToLocalHookLink link; 164047c6ae99SBarry Smith 164147c6ae99SBarry Smith PetscFunctionBegin; 1642171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 1643baf369e7SPeter Brune for (link=dm->gtolhook; link; link=link->next) { 1644baf369e7SPeter Brune if (link->beginhook) {ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);} 1645baf369e7SPeter Brune } 16467128ae9fSMatthew G Knepley ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr); 16477128ae9fSMatthew G Knepley if (sf) { 16487128ae9fSMatthew G Knepley PetscScalar *lArray, *gArray; 16497128ae9fSMatthew G Knepley 16507128ae9fSMatthew G Knepley if (mode == ADD_VALUES) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode); 16517128ae9fSMatthew G Knepley ierr = VecGetArray(l, &lArray);CHKERRQ(ierr); 16527128ae9fSMatthew G Knepley ierr = VecGetArray(g, &gArray);CHKERRQ(ierr); 16537128ae9fSMatthew G Knepley ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr); 16547128ae9fSMatthew G Knepley ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr); 16557128ae9fSMatthew G Knepley ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr); 16567128ae9fSMatthew G Knepley } else { 1657843c4018SMatthew G Knepley ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr); 16587128ae9fSMatthew G Knepley } 165947c6ae99SBarry Smith PetscFunctionReturn(0); 166047c6ae99SBarry Smith } 166147c6ae99SBarry Smith 166247c6ae99SBarry Smith #undef __FUNCT__ 166347c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd" 166447c6ae99SBarry Smith /*@ 166547c6ae99SBarry Smith DMGlobalToLocalEnd - Ends updating local vectors from global vector 166647c6ae99SBarry Smith 166747c6ae99SBarry Smith Neighbor-wise Collective on DM 166847c6ae99SBarry Smith 166947c6ae99SBarry Smith Input Parameters: 167047c6ae99SBarry Smith + dm - the DM object 167147c6ae99SBarry Smith . g - the global vector 167247c6ae99SBarry Smith . mode - INSERT_VALUES or ADD_VALUES 167347c6ae99SBarry Smith - l - the local vector 167447c6ae99SBarry Smith 167547c6ae99SBarry Smith 167647c6ae99SBarry Smith Level: beginner 167747c6ae99SBarry Smith 1678e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin() 167947c6ae99SBarry Smith 168047c6ae99SBarry Smith @*/ 16817087cfbeSBarry Smith PetscErrorCode DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l) 168247c6ae99SBarry Smith { 16837128ae9fSMatthew G Knepley PetscSF sf; 168447c6ae99SBarry Smith PetscErrorCode ierr; 168561a3c1faSSatish Balay PetscScalar *lArray, *gArray; 1686baf369e7SPeter Brune DMGlobalToLocalHookLink link; 168747c6ae99SBarry Smith 168847c6ae99SBarry Smith PetscFunctionBegin; 1689171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 16907128ae9fSMatthew G Knepley ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr); 16917128ae9fSMatthew G Knepley if (sf) { 16927128ae9fSMatthew G Knepley if (mode == ADD_VALUES) SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode); 16937128ae9fSMatthew G Knepley 16947128ae9fSMatthew G Knepley ierr = VecGetArray(l, &lArray);CHKERRQ(ierr); 16957128ae9fSMatthew G Knepley ierr = VecGetArray(g, &gArray);CHKERRQ(ierr); 16967128ae9fSMatthew G Knepley ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr); 16977128ae9fSMatthew G Knepley ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr); 16987128ae9fSMatthew G Knepley ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr); 16997128ae9fSMatthew G Knepley } else { 1700843c4018SMatthew G Knepley ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr); 17017128ae9fSMatthew G Knepley } 1702baf369e7SPeter Brune for (link=dm->gtolhook; link; link=link->next) { 1703baf369e7SPeter Brune if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);} 1704baf369e7SPeter Brune } 170547c6ae99SBarry Smith PetscFunctionReturn(0); 170647c6ae99SBarry Smith } 170747c6ae99SBarry Smith 170847c6ae99SBarry Smith #undef __FUNCT__ 17099a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin" 171047c6ae99SBarry Smith /*@ 17119a42bb27SBarry Smith DMLocalToGlobalBegin - updates global vectors from local vectors 17129a42bb27SBarry Smith 17139a42bb27SBarry Smith Neighbor-wise Collective on DM 17149a42bb27SBarry Smith 17159a42bb27SBarry Smith Input Parameters: 17169a42bb27SBarry Smith + dm - the DM object 1717f6813fd5SJed Brown . l - the local vector 17189a42bb27SBarry 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 17199a42bb27SBarry Smith base point. 1720f6813fd5SJed Brown - - the global vector 17219a42bb27SBarry Smith 17229a42bb27SBarry 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 17239a42bb27SBarry 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 17249a42bb27SBarry Smith global array to the final global array with VecAXPY(). 17259a42bb27SBarry Smith 17269a42bb27SBarry Smith Level: beginner 17279a42bb27SBarry Smith 1728e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin() 17299a42bb27SBarry Smith 17309a42bb27SBarry Smith @*/ 17317087cfbeSBarry Smith PetscErrorCode DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g) 17329a42bb27SBarry Smith { 17337128ae9fSMatthew G Knepley PetscSF sf; 17349a42bb27SBarry Smith PetscErrorCode ierr; 17359a42bb27SBarry Smith 17369a42bb27SBarry Smith PetscFunctionBegin; 1737171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 17387128ae9fSMatthew G Knepley ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr); 17397128ae9fSMatthew G Knepley if (sf) { 17407128ae9fSMatthew G Knepley MPI_Op op; 17417128ae9fSMatthew G Knepley PetscScalar *lArray, *gArray; 17427128ae9fSMatthew G Knepley 17437128ae9fSMatthew G Knepley switch (mode) { 17447128ae9fSMatthew G Knepley case INSERT_VALUES: 17457128ae9fSMatthew G Knepley case INSERT_ALL_VALUES: 17467128ae9fSMatthew G Knepley #if defined(PETSC_HAVE_MPI_REPLACE) 17477128ae9fSMatthew G Knepley op = MPI_REPLACE; break; 17487128ae9fSMatthew G Knepley #else 17497128ae9fSMatthew G Knepley SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No support for INSERT_VALUES without an MPI-2 implementation"); 17507128ae9fSMatthew G Knepley #endif 17517128ae9fSMatthew G Knepley case ADD_VALUES: 17527128ae9fSMatthew G Knepley case ADD_ALL_VALUES: 17537128ae9fSMatthew G Knepley op = MPI_SUM; break; 17547128ae9fSMatthew G Knepley default: 17557128ae9fSMatthew G Knepley SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode); 17567128ae9fSMatthew G Knepley } 17577128ae9fSMatthew G Knepley ierr = VecGetArray(l, &lArray);CHKERRQ(ierr); 17587128ae9fSMatthew G Knepley ierr = VecGetArray(g, &gArray);CHKERRQ(ierr); 17597128ae9fSMatthew G Knepley ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr); 17607128ae9fSMatthew G Knepley ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr); 17617128ae9fSMatthew G Knepley ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr); 17627128ae9fSMatthew G Knepley } else { 1763843c4018SMatthew G Knepley ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr); 17647128ae9fSMatthew G Knepley } 17659a42bb27SBarry Smith PetscFunctionReturn(0); 17669a42bb27SBarry Smith } 17679a42bb27SBarry Smith 17689a42bb27SBarry Smith #undef __FUNCT__ 17699a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd" 17709a42bb27SBarry Smith /*@ 17719a42bb27SBarry Smith DMLocalToGlobalEnd - updates global vectors from local vectors 177247c6ae99SBarry Smith 177347c6ae99SBarry Smith Neighbor-wise Collective on DM 177447c6ae99SBarry Smith 177547c6ae99SBarry Smith Input Parameters: 177647c6ae99SBarry Smith + dm - the DM object 1777f6813fd5SJed Brown . l - the local vector 177847c6ae99SBarry Smith . mode - INSERT_VALUES or ADD_VALUES 1779f6813fd5SJed Brown - g - the global vector 178047c6ae99SBarry Smith 178147c6ae99SBarry Smith 178247c6ae99SBarry Smith Level: beginner 178347c6ae99SBarry Smith 1784e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd() 178547c6ae99SBarry Smith 178647c6ae99SBarry Smith @*/ 17877087cfbeSBarry Smith PetscErrorCode DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g) 178847c6ae99SBarry Smith { 17897128ae9fSMatthew G Knepley PetscSF sf; 179047c6ae99SBarry Smith PetscErrorCode ierr; 179147c6ae99SBarry Smith 179247c6ae99SBarry Smith PetscFunctionBegin; 1793171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 17947128ae9fSMatthew G Knepley ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr); 17957128ae9fSMatthew G Knepley if (sf) { 17967128ae9fSMatthew G Knepley MPI_Op op; 17977128ae9fSMatthew G Knepley PetscScalar *lArray, *gArray; 17987128ae9fSMatthew G Knepley 17997128ae9fSMatthew G Knepley switch (mode) { 18007128ae9fSMatthew G Knepley case INSERT_VALUES: 18017128ae9fSMatthew G Knepley case INSERT_ALL_VALUES: 18027128ae9fSMatthew G Knepley #if defined(PETSC_HAVE_MPI_REPLACE) 18037128ae9fSMatthew G Knepley op = MPI_REPLACE; break; 18047128ae9fSMatthew G Knepley #else 18057128ae9fSMatthew G Knepley SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No support for INSERT_VALUES without an MPI-2 implementation"); 18067128ae9fSMatthew G Knepley #endif 18077128ae9fSMatthew G Knepley case ADD_VALUES: 18087128ae9fSMatthew G Knepley case ADD_ALL_VALUES: 18097128ae9fSMatthew G Knepley op = MPI_SUM; break; 18107128ae9fSMatthew G Knepley default: 18117128ae9fSMatthew G Knepley SETERRQ1(((PetscObject) dm)->comm, PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode); 18127128ae9fSMatthew G Knepley } 18137128ae9fSMatthew G Knepley ierr = VecGetArray(l, &lArray);CHKERRQ(ierr); 18147128ae9fSMatthew G Knepley ierr = VecGetArray(g, &gArray);CHKERRQ(ierr); 18157128ae9fSMatthew G Knepley ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, op);CHKERRQ(ierr); 18167128ae9fSMatthew G Knepley ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr); 18177128ae9fSMatthew G Knepley ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr); 18187128ae9fSMatthew G Knepley } else { 1819843c4018SMatthew G Knepley ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr); 18207128ae9fSMatthew G Knepley } 182147c6ae99SBarry Smith PetscFunctionReturn(0); 182247c6ae99SBarry Smith } 182347c6ae99SBarry Smith 182447c6ae99SBarry Smith #undef __FUNCT__ 182547c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen" 182647c6ae99SBarry Smith /*@ 182747c6ae99SBarry Smith DMCoarsen - Coarsens a DM object 182847c6ae99SBarry Smith 182947c6ae99SBarry Smith Collective on DM 183047c6ae99SBarry Smith 183147c6ae99SBarry Smith Input Parameter: 183247c6ae99SBarry Smith + dm - the DM object 183391d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL) 183447c6ae99SBarry Smith 183547c6ae99SBarry Smith Output Parameter: 183647c6ae99SBarry Smith . dmc - the coarsened DM 183747c6ae99SBarry Smith 183847c6ae99SBarry Smith Level: developer 183947c6ae99SBarry Smith 1840e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation() 184147c6ae99SBarry Smith 184247c6ae99SBarry Smith @*/ 18437087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc) 184447c6ae99SBarry Smith { 184547c6ae99SBarry Smith PetscErrorCode ierr; 1846b17ce1afSJed Brown DMCoarsenHookLink link; 184747c6ae99SBarry Smith 184847c6ae99SBarry Smith PetscFunctionBegin; 1849171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 185047c6ae99SBarry Smith ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr); 185143842a1eSJed Brown (*dmc)->ops->creatematrix = dm->ops->creatematrix; 18528cd211a4SJed Brown ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr); 1853644e2e5bSBarry Smith (*dmc)->ctx = dm->ctx; 18540598a293SJed Brown (*dmc)->levelup = dm->levelup; 1855656b349aSBarry Smith (*dmc)->leveldown = dm->leveldown + 1; 1856e4b4b23bSJed Brown ierr = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr); 1857b17ce1afSJed Brown for (link=dm->coarsenhook; link; link=link->next) { 1858b17ce1afSJed Brown if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);} 1859b17ce1afSJed Brown } 1860b17ce1afSJed Brown PetscFunctionReturn(0); 1861b17ce1afSJed Brown } 1862b17ce1afSJed Brown 1863b17ce1afSJed Brown #undef __FUNCT__ 1864b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd" 1865b17ce1afSJed Brown /*@ 1866b17ce1afSJed Brown DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid 1867b17ce1afSJed Brown 1868b17ce1afSJed Brown Logically Collective 1869b17ce1afSJed Brown 1870b17ce1afSJed Brown Input Arguments: 1871b17ce1afSJed Brown + fine - nonlinear solver context on which to run a hook when restricting to a coarser level 1872b17ce1afSJed Brown . coarsenhook - function to run when setting up a coarser level 1873b17ce1afSJed Brown . restricthook - function to run to update data on coarser levels (once per SNESSolve()) 1874b17ce1afSJed Brown - ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL) 1875b17ce1afSJed Brown 1876b17ce1afSJed Brown Calling sequence of coarsenhook: 1877b17ce1afSJed Brown $ coarsenhook(DM fine,DM coarse,void *ctx); 1878b17ce1afSJed Brown 1879b17ce1afSJed Brown + fine - fine level DM 1880b17ce1afSJed Brown . coarse - coarse level DM to restrict problem to 1881b17ce1afSJed Brown - ctx - optional user-defined function context 1882b17ce1afSJed Brown 1883b17ce1afSJed Brown Calling sequence for restricthook: 1884c833c3b5SJed Brown $ restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx) 1885b17ce1afSJed Brown 1886b17ce1afSJed Brown + fine - fine level DM 1887b17ce1afSJed Brown . mrestrict - matrix restricting a fine-level solution to the coarse grid 1888c833c3b5SJed Brown . rscale - scaling vector for restriction 1889c833c3b5SJed Brown . inject - matrix restricting by injection 1890b17ce1afSJed Brown . coarse - coarse level DM to update 1891b17ce1afSJed Brown - ctx - optional user-defined function context 1892b17ce1afSJed Brown 1893b17ce1afSJed Brown Level: advanced 1894b17ce1afSJed Brown 1895b17ce1afSJed Brown Notes: 1896b17ce1afSJed Brown This function is only needed if auxiliary data needs to be set up on coarse grids. 1897b17ce1afSJed Brown 1898b17ce1afSJed Brown If this function is called multiple times, the hooks will be run in the order they are added. 1899b17ce1afSJed Brown 1900b17ce1afSJed Brown In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to 1901b17ce1afSJed Brown extract the finest level information from its context (instead of from the SNES). 1902b17ce1afSJed Brown 1903c833c3b5SJed Brown .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate() 1904b17ce1afSJed Brown @*/ 1905b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx) 1906b17ce1afSJed Brown { 1907b17ce1afSJed Brown PetscErrorCode ierr; 1908b17ce1afSJed Brown DMCoarsenHookLink link,*p; 1909b17ce1afSJed Brown 1910b17ce1afSJed Brown PetscFunctionBegin; 1911b17ce1afSJed Brown PetscValidHeaderSpecific(fine,DM_CLASSID,1); 19126bfea28cSJed Brown for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */ 1913b17ce1afSJed Brown ierr = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr); 1914b17ce1afSJed Brown link->coarsenhook = coarsenhook; 1915b17ce1afSJed Brown link->restricthook = restricthook; 1916b17ce1afSJed Brown link->ctx = ctx; 19176cab3a1bSJed Brown link->next = PETSC_NULL; 1918b17ce1afSJed Brown *p = link; 1919b17ce1afSJed Brown PetscFunctionReturn(0); 1920b17ce1afSJed Brown } 1921b17ce1afSJed Brown 1922b17ce1afSJed Brown #undef __FUNCT__ 1923b17ce1afSJed Brown #define __FUNCT__ "DMRestrict" 1924b17ce1afSJed Brown /*@ 1925b17ce1afSJed Brown DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd() 1926b17ce1afSJed Brown 1927b17ce1afSJed Brown Collective if any hooks are 1928b17ce1afSJed Brown 1929b17ce1afSJed Brown Input Arguments: 1930b17ce1afSJed Brown + fine - finer DM to use as a base 1931b17ce1afSJed Brown . restrct - restriction matrix, apply using MatRestrict() 1932b17ce1afSJed Brown . inject - injection matrix, also use MatRestrict() 1933b17ce1afSJed Brown - coarse - coarer DM to update 1934b17ce1afSJed Brown 1935b17ce1afSJed Brown Level: developer 1936b17ce1afSJed Brown 1937b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict() 1938b17ce1afSJed Brown @*/ 1939b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse) 1940b17ce1afSJed Brown { 1941b17ce1afSJed Brown PetscErrorCode ierr; 1942b17ce1afSJed Brown DMCoarsenHookLink link; 1943b17ce1afSJed Brown 1944b17ce1afSJed Brown PetscFunctionBegin; 1945b17ce1afSJed Brown for (link=fine->coarsenhook; link; link=link->next) { 1946b17ce1afSJed Brown if (link->restricthook) {ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);} 1947b17ce1afSJed Brown } 194847c6ae99SBarry Smith PetscFunctionReturn(0); 194947c6ae99SBarry Smith } 195047c6ae99SBarry Smith 195147c6ae99SBarry Smith #undef __FUNCT__ 1952be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainHookAdd" 19535dbd56e3SPeter Brune /*@ 1954be081cd6SPeter Brune DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid 19555dbd56e3SPeter Brune 19565dbd56e3SPeter Brune Logically Collective 19575dbd56e3SPeter Brune 19585dbd56e3SPeter Brune Input Arguments: 19595dbd56e3SPeter Brune + global - global DM 19605dbd56e3SPeter Brune . restricthook - function to run to update data on block solve (at the beginning of the block solve) 19615dbd56e3SPeter Brune - ctx - [optional] user-defined context for provide data for the hooks (may be PETSC_NULL) 19625dbd56e3SPeter Brune 19635dbd56e3SPeter Brune Calling sequence for restricthook: 19645dbd56e3SPeter Brune $ restricthook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx) 19655dbd56e3SPeter Brune 19665dbd56e3SPeter Brune + global - global DM 19675dbd56e3SPeter Brune . out - scatter to the outer (with ghost and overlap points) block vector 19685dbd56e3SPeter Brune . in - scatter to block vector values only owned locally 19695dbd56e3SPeter Brune . block - block DM (may just be a shell if the global DM is passed in correctly) 19705dbd56e3SPeter Brune - ctx - optional user-defined function context 19715dbd56e3SPeter Brune 19725dbd56e3SPeter Brune Level: advanced 19735dbd56e3SPeter Brune 19745dbd56e3SPeter Brune Notes: 19755dbd56e3SPeter Brune This function is only needed if auxiliary data needs to be set up on coarse grids. 19765dbd56e3SPeter Brune 19775dbd56e3SPeter Brune If this function is called multiple times, the hooks will be run in the order they are added. 19785dbd56e3SPeter Brune 19795dbd56e3SPeter Brune In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to 19805dbd56e3SPeter Brune extract the finest level information from its context (instead of from the SNES). 19815dbd56e3SPeter Brune 19825dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate() 19835dbd56e3SPeter Brune @*/ 1984be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx) 19855dbd56e3SPeter Brune { 19865dbd56e3SPeter Brune PetscErrorCode ierr; 1987be081cd6SPeter Brune DMSubDomainHookLink link,*p; 19885dbd56e3SPeter Brune 19895dbd56e3SPeter Brune PetscFunctionBegin; 19905dbd56e3SPeter Brune PetscValidHeaderSpecific(global,DM_CLASSID,1); 1991be081cd6SPeter Brune for (p=&global->subdomainhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */ 1992be081cd6SPeter Brune ierr = PetscMalloc(sizeof(struct _DMSubDomainHookLink),&link);CHKERRQ(ierr); 19935dbd56e3SPeter Brune link->restricthook = restricthook; 1994be081cd6SPeter Brune link->ddhook = ddhook; 19955dbd56e3SPeter Brune link->ctx = ctx; 19965dbd56e3SPeter Brune link->next = PETSC_NULL; 19975dbd56e3SPeter Brune *p = link; 19985dbd56e3SPeter Brune PetscFunctionReturn(0); 19995dbd56e3SPeter Brune } 20005dbd56e3SPeter Brune 20015dbd56e3SPeter Brune #undef __FUNCT__ 2002be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainRestrict" 20035dbd56e3SPeter Brune /*@ 2004be081cd6SPeter Brune DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd() 20055dbd56e3SPeter Brune 20065dbd56e3SPeter Brune Collective if any hooks are 20075dbd56e3SPeter Brune 20085dbd56e3SPeter Brune Input Arguments: 20095dbd56e3SPeter Brune + fine - finer DM to use as a base 2010be081cd6SPeter Brune . oscatter - scatter from domain global vector filling subdomain global vector with overlap 2011be081cd6SPeter Brune . gscatter - scatter from domain global vector filling subdomain local vector with ghosts 20125dbd56e3SPeter Brune - coarse - coarer DM to update 20135dbd56e3SPeter Brune 20145dbd56e3SPeter Brune Level: developer 20155dbd56e3SPeter Brune 20165dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict() 20175dbd56e3SPeter Brune @*/ 2018be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm) 20195dbd56e3SPeter Brune { 20205dbd56e3SPeter Brune PetscErrorCode ierr; 2021be081cd6SPeter Brune DMSubDomainHookLink link; 20225dbd56e3SPeter Brune 20235dbd56e3SPeter Brune PetscFunctionBegin; 2024be081cd6SPeter Brune for (link=global->subdomainhook; link; link=link->next) { 2025be081cd6SPeter Brune if (link->restricthook) {ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);} 20265dbd56e3SPeter Brune } 20275dbd56e3SPeter Brune PetscFunctionReturn(0); 20285dbd56e3SPeter Brune } 20295dbd56e3SPeter Brune 20305dbd56e3SPeter Brune #undef __FUNCT__ 20315fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel" 20325fe1f584SPeter Brune /*@ 20336a7d9d85SPeter Brune DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM. 20345fe1f584SPeter Brune 20355fe1f584SPeter Brune Not Collective 20365fe1f584SPeter Brune 20375fe1f584SPeter Brune Input Parameter: 20385fe1f584SPeter Brune . dm - the DM object 20395fe1f584SPeter Brune 20405fe1f584SPeter Brune Output Parameter: 20416a7d9d85SPeter Brune . level - number of coarsenings 20425fe1f584SPeter Brune 20435fe1f584SPeter Brune Level: developer 20445fe1f584SPeter Brune 20456a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation() 20465fe1f584SPeter Brune 20475fe1f584SPeter Brune @*/ 20485fe1f584SPeter Brune PetscErrorCode DMGetCoarsenLevel(DM dm,PetscInt *level) 20495fe1f584SPeter Brune { 20505fe1f584SPeter Brune PetscFunctionBegin; 20515fe1f584SPeter Brune PetscValidHeaderSpecific(dm,DM_CLASSID,1); 20525fe1f584SPeter Brune *level = dm->leveldown; 20535fe1f584SPeter Brune PetscFunctionReturn(0); 20545fe1f584SPeter Brune } 20555fe1f584SPeter Brune 20565fe1f584SPeter Brune 20575fe1f584SPeter Brune 20585fe1f584SPeter Brune #undef __FUNCT__ 205947c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy" 206047c6ae99SBarry Smith /*@C 206147c6ae99SBarry Smith DMRefineHierarchy - Refines a DM object, all levels at once 206247c6ae99SBarry Smith 206347c6ae99SBarry Smith Collective on DM 206447c6ae99SBarry Smith 206547c6ae99SBarry Smith Input Parameter: 206647c6ae99SBarry Smith + dm - the DM object 206747c6ae99SBarry Smith - nlevels - the number of levels of refinement 206847c6ae99SBarry Smith 206947c6ae99SBarry Smith Output Parameter: 207047c6ae99SBarry Smith . dmf - the refined DM hierarchy 207147c6ae99SBarry Smith 207247c6ae99SBarry Smith Level: developer 207347c6ae99SBarry Smith 2074e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation() 207547c6ae99SBarry Smith 207647c6ae99SBarry Smith @*/ 20777087cfbeSBarry Smith PetscErrorCode DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[]) 207847c6ae99SBarry Smith { 207947c6ae99SBarry Smith PetscErrorCode ierr; 208047c6ae99SBarry Smith 208147c6ae99SBarry Smith PetscFunctionBegin; 2082171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 208347c6ae99SBarry Smith if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative"); 208447c6ae99SBarry Smith if (nlevels == 0) PetscFunctionReturn(0); 208547c6ae99SBarry Smith if (dm->ops->refinehierarchy) { 208647c6ae99SBarry Smith ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr); 208747c6ae99SBarry Smith } else if (dm->ops->refine) { 208847c6ae99SBarry Smith PetscInt i; 208947c6ae99SBarry Smith 209047c6ae99SBarry Smith ierr = DMRefine(dm,((PetscObject)dm)->comm,&dmf[0]);CHKERRQ(ierr); 209147c6ae99SBarry Smith for (i=1; i<nlevels; i++) { 209247c6ae99SBarry Smith ierr = DMRefine(dmf[i-1],((PetscObject)dm)->comm,&dmf[i]);CHKERRQ(ierr); 209347c6ae99SBarry Smith } 20948cbdbec6SBarry Smith } else SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No RefineHierarchy for this DM yet"); 209547c6ae99SBarry Smith PetscFunctionReturn(0); 209647c6ae99SBarry Smith } 209747c6ae99SBarry Smith 209847c6ae99SBarry Smith #undef __FUNCT__ 209947c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy" 210047c6ae99SBarry Smith /*@C 210147c6ae99SBarry Smith DMCoarsenHierarchy - Coarsens a DM object, all levels at once 210247c6ae99SBarry Smith 210347c6ae99SBarry Smith Collective on DM 210447c6ae99SBarry Smith 210547c6ae99SBarry Smith Input Parameter: 210647c6ae99SBarry Smith + dm - the DM object 210747c6ae99SBarry Smith - nlevels - the number of levels of coarsening 210847c6ae99SBarry Smith 210947c6ae99SBarry Smith Output Parameter: 211047c6ae99SBarry Smith . dmc - the coarsened DM hierarchy 211147c6ae99SBarry Smith 211247c6ae99SBarry Smith Level: developer 211347c6ae99SBarry Smith 2114e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation() 211547c6ae99SBarry Smith 211647c6ae99SBarry Smith @*/ 21177087cfbeSBarry Smith PetscErrorCode DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[]) 211847c6ae99SBarry Smith { 211947c6ae99SBarry Smith PetscErrorCode ierr; 212047c6ae99SBarry Smith 212147c6ae99SBarry Smith PetscFunctionBegin; 2122171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 212347c6ae99SBarry Smith if (nlevels < 0) SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative"); 212447c6ae99SBarry Smith if (nlevels == 0) PetscFunctionReturn(0); 212547c6ae99SBarry Smith PetscValidPointer(dmc,3); 212647c6ae99SBarry Smith if (dm->ops->coarsenhierarchy) { 212747c6ae99SBarry Smith ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr); 212847c6ae99SBarry Smith } else if (dm->ops->coarsen) { 212947c6ae99SBarry Smith PetscInt i; 213047c6ae99SBarry Smith 213147c6ae99SBarry Smith ierr = DMCoarsen(dm,((PetscObject)dm)->comm,&dmc[0]);CHKERRQ(ierr); 213247c6ae99SBarry Smith for (i=1; i<nlevels; i++) { 213347c6ae99SBarry Smith ierr = DMCoarsen(dmc[i-1],((PetscObject)dm)->comm,&dmc[i]);CHKERRQ(ierr); 213447c6ae99SBarry Smith } 21358cbdbec6SBarry Smith } else SETERRQ(((PetscObject)dm)->comm,PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet"); 213647c6ae99SBarry Smith PetscFunctionReturn(0); 213747c6ae99SBarry Smith } 213847c6ae99SBarry Smith 213947c6ae99SBarry Smith #undef __FUNCT__ 2140e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates" 214147c6ae99SBarry Smith /*@ 2142e727c939SJed Brown DMCreateAggregates - Gets the aggregates that map between 214347c6ae99SBarry Smith grids associated with two DMs. 214447c6ae99SBarry Smith 214547c6ae99SBarry Smith Collective on DM 214647c6ae99SBarry Smith 214747c6ae99SBarry Smith Input Parameters: 214847c6ae99SBarry Smith + dmc - the coarse grid DM 214947c6ae99SBarry Smith - dmf - the fine grid DM 215047c6ae99SBarry Smith 215147c6ae99SBarry Smith Output Parameters: 215247c6ae99SBarry Smith . rest - the restriction matrix (transpose of the projection matrix) 215347c6ae99SBarry Smith 215447c6ae99SBarry Smith Level: intermediate 215547c6ae99SBarry Smith 215647c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid 215747c6ae99SBarry Smith 2158e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation() 215947c6ae99SBarry Smith @*/ 2160e727c939SJed Brown PetscErrorCode DMCreateAggregates(DM dmc, DM dmf, Mat *rest) 216147c6ae99SBarry Smith { 216247c6ae99SBarry Smith PetscErrorCode ierr; 216347c6ae99SBarry Smith 216447c6ae99SBarry Smith PetscFunctionBegin; 2165171400e9SBarry Smith PetscValidHeaderSpecific(dmc,DM_CLASSID,1); 2166171400e9SBarry Smith PetscValidHeaderSpecific(dmf,DM_CLASSID,2); 216747c6ae99SBarry Smith ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr); 216847c6ae99SBarry Smith PetscFunctionReturn(0); 216947c6ae99SBarry Smith } 217047c6ae99SBarry Smith 217147c6ae99SBarry Smith #undef __FUNCT__ 21721a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy" 21731a266240SBarry Smith /*@C 21741a266240SBarry Smith DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed 21751a266240SBarry Smith 21761a266240SBarry Smith Not Collective 21771a266240SBarry Smith 21781a266240SBarry Smith Input Parameters: 21791a266240SBarry Smith + dm - the DM object 21801a266240SBarry Smith - destroy - the destroy function 21811a266240SBarry Smith 21821a266240SBarry Smith Level: intermediate 21831a266240SBarry Smith 2184e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext() 21851a266240SBarry Smith 2186f07f9ceaSJed Brown @*/ 21871a266240SBarry Smith PetscErrorCode DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**)) 21881a266240SBarry Smith { 21891a266240SBarry Smith PetscFunctionBegin; 2190171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 21911a266240SBarry Smith dm->ctxdestroy = destroy; 21921a266240SBarry Smith PetscFunctionReturn(0); 21931a266240SBarry Smith } 21941a266240SBarry Smith 21951a266240SBarry Smith #undef __FUNCT__ 21961b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext" 2197b07ff414SBarry Smith /*@ 21981b2093e4SBarry Smith DMSetApplicationContext - Set a user context into a DM object 219947c6ae99SBarry Smith 220047c6ae99SBarry Smith Not Collective 220147c6ae99SBarry Smith 220247c6ae99SBarry Smith Input Parameters: 220347c6ae99SBarry Smith + dm - the DM object 220447c6ae99SBarry Smith - ctx - the user context 220547c6ae99SBarry Smith 220647c6ae99SBarry Smith Level: intermediate 220747c6ae99SBarry Smith 2208e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext() 220947c6ae99SBarry Smith 221047c6ae99SBarry Smith @*/ 22111b2093e4SBarry Smith PetscErrorCode DMSetApplicationContext(DM dm,void *ctx) 221247c6ae99SBarry Smith { 221347c6ae99SBarry Smith PetscFunctionBegin; 2214171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 221547c6ae99SBarry Smith dm->ctx = ctx; 221647c6ae99SBarry Smith PetscFunctionReturn(0); 221747c6ae99SBarry Smith } 221847c6ae99SBarry Smith 221947c6ae99SBarry Smith #undef __FUNCT__ 22201b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext" 222147c6ae99SBarry Smith /*@ 22221b2093e4SBarry Smith DMGetApplicationContext - Gets a user context from a DM object 222347c6ae99SBarry Smith 222447c6ae99SBarry Smith Not Collective 222547c6ae99SBarry Smith 222647c6ae99SBarry Smith Input Parameter: 222747c6ae99SBarry Smith . dm - the DM object 222847c6ae99SBarry Smith 222947c6ae99SBarry Smith Output Parameter: 223047c6ae99SBarry Smith . ctx - the user context 223147c6ae99SBarry Smith 223247c6ae99SBarry Smith Level: intermediate 223347c6ae99SBarry Smith 2234e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext() 223547c6ae99SBarry Smith 223647c6ae99SBarry Smith @*/ 22371b2093e4SBarry Smith PetscErrorCode DMGetApplicationContext(DM dm,void *ctx) 223847c6ae99SBarry Smith { 223947c6ae99SBarry Smith PetscFunctionBegin; 2240171400e9SBarry Smith PetscValidHeaderSpecific(dm,DM_CLASSID,1); 22411b2093e4SBarry Smith *(void**)ctx = dm->ctx; 224247c6ae99SBarry Smith PetscFunctionReturn(0); 224347c6ae99SBarry Smith } 224447c6ae99SBarry Smith 224547c6ae99SBarry Smith #undef __FUNCT__ 224608da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds" 224708da532bSDmitry Karpeev /*@C 224808da532bSDmitry Karpeev DMSetVariableBounds - sets a function to compute the the lower and upper bound vectors for SNESVI. 224908da532bSDmitry Karpeev 225008da532bSDmitry Karpeev Logically Collective on DM 225108da532bSDmitry Karpeev 225208da532bSDmitry Karpeev Input Parameter: 225308da532bSDmitry Karpeev + dm - the DM object 225408da532bSDmitry Karpeev - f - the function that computes variable bounds used by SNESVI (use PETSC_NULL to cancel a previous function that was set) 225508da532bSDmitry Karpeev 225608da532bSDmitry Karpeev Level: intermediate 225708da532bSDmitry Karpeev 2258835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(), 225908da532bSDmitry Karpeev DMSetJacobian() 226008da532bSDmitry Karpeev 226108da532bSDmitry Karpeev @*/ 226208da532bSDmitry Karpeev PetscErrorCode DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec)) 226308da532bSDmitry Karpeev { 226408da532bSDmitry Karpeev PetscFunctionBegin; 226508da532bSDmitry Karpeev dm->ops->computevariablebounds = f; 226608da532bSDmitry Karpeev PetscFunctionReturn(0); 226708da532bSDmitry Karpeev } 226808da532bSDmitry Karpeev 226908da532bSDmitry Karpeev #undef __FUNCT__ 227008da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds" 227108da532bSDmitry Karpeev /*@ 227208da532bSDmitry Karpeev DMHasVariableBounds - does the DM object have a variable bounds function? 227308da532bSDmitry Karpeev 227408da532bSDmitry Karpeev Not Collective 227508da532bSDmitry Karpeev 227608da532bSDmitry Karpeev Input Parameter: 227708da532bSDmitry Karpeev . dm - the DM object to destroy 227808da532bSDmitry Karpeev 227908da532bSDmitry Karpeev Output Parameter: 228008da532bSDmitry Karpeev . flg - PETSC_TRUE if the variable bounds function exists 228108da532bSDmitry Karpeev 228208da532bSDmitry Karpeev Level: developer 228308da532bSDmitry Karpeev 228474e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext() 228508da532bSDmitry Karpeev 228608da532bSDmitry Karpeev @*/ 228708da532bSDmitry Karpeev PetscErrorCode DMHasVariableBounds(DM dm,PetscBool *flg) 228808da532bSDmitry Karpeev { 228908da532bSDmitry Karpeev PetscFunctionBegin; 229008da532bSDmitry Karpeev *flg = (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE; 229108da532bSDmitry Karpeev PetscFunctionReturn(0); 229208da532bSDmitry Karpeev } 229308da532bSDmitry Karpeev 229408da532bSDmitry Karpeev #undef __FUNCT__ 229508da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds" 229608da532bSDmitry Karpeev /*@C 229708da532bSDmitry Karpeev DMComputeVariableBounds - compute variable bounds used by SNESVI. 229808da532bSDmitry Karpeev 229908da532bSDmitry Karpeev Logically Collective on DM 230008da532bSDmitry Karpeev 230108da532bSDmitry Karpeev Input Parameters: 230208da532bSDmitry Karpeev + dm - the DM object to destroy 230308da532bSDmitry Karpeev - x - current solution at which the bounds are computed 230408da532bSDmitry Karpeev 230508da532bSDmitry Karpeev Output parameters: 230608da532bSDmitry Karpeev + xl - lower bound 230708da532bSDmitry Karpeev - xu - upper bound 230808da532bSDmitry Karpeev 230908da532bSDmitry Karpeev Level: intermediate 231008da532bSDmitry Karpeev 231174e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext() 231208da532bSDmitry Karpeev 231308da532bSDmitry Karpeev @*/ 231408da532bSDmitry Karpeev PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu) 231508da532bSDmitry Karpeev { 231608da532bSDmitry Karpeev PetscErrorCode ierr; 2317*5fd66863SKarl Rupp 231808da532bSDmitry Karpeev PetscFunctionBegin; 231908da532bSDmitry Karpeev PetscValidHeaderSpecific(xl,VEC_CLASSID,2); 232008da532bSDmitry Karpeev PetscValidHeaderSpecific(xu,VEC_CLASSID,2); 232108da532bSDmitry Karpeev if (dm->ops->computevariablebounds) { 232208da532bSDmitry Karpeev ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr); 232308da532bSDmitry Karpeev } 2324a201590fSDmitry Karpeev else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds."); 232508da532bSDmitry Karpeev PetscFunctionReturn(0); 232608da532bSDmitry Karpeev } 232708da532bSDmitry Karpeev 232808da532bSDmitry Karpeev #undef __FUNCT__ 2329b0ae01b7SPeter Brune #define __FUNCT__ "DMHasColoring" 2330b0ae01b7SPeter Brune /*@ 2331b0ae01b7SPeter Brune DMHasColoring - does the DM object have a method of providing a coloring? 2332b0ae01b7SPeter Brune 2333b0ae01b7SPeter Brune Not Collective 2334b0ae01b7SPeter Brune 2335b0ae01b7SPeter Brune Input Parameter: 2336b0ae01b7SPeter Brune . dm - the DM object 2337b0ae01b7SPeter Brune 2338b0ae01b7SPeter Brune Output Parameter: 2339b0ae01b7SPeter Brune . flg - PETSC_TRUE if the DM has facilities for DMCreateColoring(). 2340b0ae01b7SPeter Brune 2341b0ae01b7SPeter Brune Level: developer 2342b0ae01b7SPeter Brune 2343b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring() 2344b0ae01b7SPeter Brune 2345b0ae01b7SPeter Brune @*/ 2346b0ae01b7SPeter Brune PetscErrorCode DMHasColoring(DM dm,PetscBool *flg) 2347b0ae01b7SPeter Brune { 2348b0ae01b7SPeter Brune PetscFunctionBegin; 2349b0ae01b7SPeter Brune *flg = (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE; 2350b0ae01b7SPeter Brune PetscFunctionReturn(0); 2351b0ae01b7SPeter Brune } 2352b0ae01b7SPeter Brune 2353b0ae01b7SPeter Brune #undef __FUNCT__ 235408da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec" 2355748fac09SDmitry Karpeev /*@C 235608da532bSDmitry Karpeev DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear. 235708da532bSDmitry Karpeev 235808da532bSDmitry Karpeev Collective on DM 235908da532bSDmitry Karpeev 236008da532bSDmitry Karpeev Input Parameter: 236108da532bSDmitry Karpeev + dm - the DM object 2362e88d7f4bSDmitry Karpeev - x - location to compute residual and Jacobian, if PETSC_NULL is passed to those routines; will be PETSC_NULL for linear problems. 236308da532bSDmitry Karpeev 236408da532bSDmitry Karpeev Level: developer 236508da532bSDmitry Karpeev 236674e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext() 236708da532bSDmitry Karpeev 236808da532bSDmitry Karpeev @*/ 236908da532bSDmitry Karpeev PetscErrorCode DMSetVec(DM dm,Vec x) 237008da532bSDmitry Karpeev { 237108da532bSDmitry Karpeev PetscErrorCode ierr; 2372*5fd66863SKarl Rupp 237308da532bSDmitry Karpeev PetscFunctionBegin; 237408da532bSDmitry Karpeev if (x) { 237508da532bSDmitry Karpeev if (!dm->x) { 237608da532bSDmitry Karpeev ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr); 237708da532bSDmitry Karpeev } 237808da532bSDmitry Karpeev ierr = VecCopy(x,dm->x);CHKERRQ(ierr); 237908da532bSDmitry Karpeev } 238008da532bSDmitry Karpeev else if (dm->x) { 238108da532bSDmitry Karpeev ierr = VecDestroy(&dm->x);CHKERRQ(ierr); 238208da532bSDmitry Karpeev } 238308da532bSDmitry Karpeev PetscFunctionReturn(0); 238408da532bSDmitry Karpeev } 238508da532bSDmitry Karpeev 2386140e18c1SBarry Smith PetscFunctionList DMList = PETSC_NULL; 2387264ace61SBarry Smith PetscBool DMRegisterAllCalled = PETSC_FALSE; 2388264ace61SBarry Smith 2389264ace61SBarry Smith #undef __FUNCT__ 2390264ace61SBarry Smith #define __FUNCT__ "DMSetType" 2391264ace61SBarry Smith /*@C 2392264ace61SBarry Smith DMSetType - Builds a DM, for a particular DM implementation. 2393264ace61SBarry Smith 2394264ace61SBarry Smith Collective on DM 2395264ace61SBarry Smith 2396264ace61SBarry Smith Input Parameters: 2397264ace61SBarry Smith + dm - The DM object 2398264ace61SBarry Smith - method - The name of the DM type 2399264ace61SBarry Smith 2400264ace61SBarry Smith Options Database Key: 2401264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types 2402264ace61SBarry Smith 2403264ace61SBarry Smith Notes: 2404e1589f56SBarry Smith See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D). 2405264ace61SBarry Smith 2406264ace61SBarry Smith Level: intermediate 2407264ace61SBarry Smith 2408264ace61SBarry Smith .keywords: DM, set, type 2409264ace61SBarry Smith .seealso: DMGetType(), DMCreate() 2410264ace61SBarry Smith @*/ 241119fd82e9SBarry Smith PetscErrorCode DMSetType(DM dm, DMType method) 2412264ace61SBarry Smith { 2413264ace61SBarry Smith PetscErrorCode (*r)(DM); 2414264ace61SBarry Smith PetscBool match; 2415264ace61SBarry Smith PetscErrorCode ierr; 2416264ace61SBarry Smith 2417264ace61SBarry Smith PetscFunctionBegin; 2418264ace61SBarry Smith PetscValidHeaderSpecific(dm, DM_CLASSID,1); 2419251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr); 2420264ace61SBarry Smith if (match) PetscFunctionReturn(0); 2421264ace61SBarry Smith 2422264ace61SBarry Smith if (!DMRegisterAllCalled) {ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr);} 2423140e18c1SBarry Smith ierr = PetscFunctionListFind(((PetscObject)dm)->comm, DMList, method,PETSC_TRUE,(void (**)(void)) &r);CHKERRQ(ierr); 242432c0f0efSBarry Smith if (!r) SETERRQ1(((PetscObject)dm)->comm,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method); 2425264ace61SBarry Smith 2426264ace61SBarry Smith if (dm->ops->destroy) { 2427264ace61SBarry Smith ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr); 2428b5c23020SJed Brown dm->ops->destroy = PETSC_NULL; 2429264ace61SBarry Smith } 2430264ace61SBarry Smith ierr = (*r)(dm);CHKERRQ(ierr); 2431264ace61SBarry Smith ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr); 2432264ace61SBarry Smith PetscFunctionReturn(0); 2433264ace61SBarry Smith } 2434264ace61SBarry Smith 2435264ace61SBarry Smith #undef __FUNCT__ 2436264ace61SBarry Smith #define __FUNCT__ "DMGetType" 2437264ace61SBarry Smith /*@C 2438264ace61SBarry Smith DMGetType - Gets the DM type name (as a string) from the DM. 2439264ace61SBarry Smith 2440264ace61SBarry Smith Not Collective 2441264ace61SBarry Smith 2442264ace61SBarry Smith Input Parameter: 2443264ace61SBarry Smith . dm - The DM 2444264ace61SBarry Smith 2445264ace61SBarry Smith Output Parameter: 2446264ace61SBarry Smith . type - The DM type name 2447264ace61SBarry Smith 2448264ace61SBarry Smith Level: intermediate 2449264ace61SBarry Smith 2450264ace61SBarry Smith .keywords: DM, get, type, name 2451264ace61SBarry Smith .seealso: DMSetType(), DMCreate() 2452264ace61SBarry Smith @*/ 245319fd82e9SBarry Smith PetscErrorCode DMGetType(DM dm, DMType *type) 2454264ace61SBarry Smith { 2455264ace61SBarry Smith PetscErrorCode ierr; 2456264ace61SBarry Smith 2457264ace61SBarry Smith PetscFunctionBegin; 2458264ace61SBarry Smith PetscValidHeaderSpecific(dm, DM_CLASSID,1); 2459264ace61SBarry Smith PetscValidCharPointer(type,2); 2460264ace61SBarry Smith if (!DMRegisterAllCalled) { 2461264ace61SBarry Smith ierr = DMRegisterAll(PETSC_NULL);CHKERRQ(ierr); 2462264ace61SBarry Smith } 2463264ace61SBarry Smith *type = ((PetscObject)dm)->type_name; 2464264ace61SBarry Smith PetscFunctionReturn(0); 2465264ace61SBarry Smith } 2466264ace61SBarry Smith 246767a56275SMatthew G Knepley #undef __FUNCT__ 246867a56275SMatthew G Knepley #define __FUNCT__ "DMConvert" 246967a56275SMatthew G Knepley /*@C 247067a56275SMatthew G Knepley DMConvert - Converts a DM to another DM, either of the same or different type. 247167a56275SMatthew G Knepley 247267a56275SMatthew G Knepley Collective on DM 247367a56275SMatthew G Knepley 247467a56275SMatthew G Knepley Input Parameters: 247567a56275SMatthew G Knepley + dm - the DM 247667a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type) 247767a56275SMatthew G Knepley 247867a56275SMatthew G Knepley Output Parameter: 247967a56275SMatthew G Knepley . M - pointer to new DM 248067a56275SMatthew G Knepley 248167a56275SMatthew G Knepley Notes: 248267a56275SMatthew G Knepley Cannot be used to convert a sequential DM to parallel or parallel to sequential, 248367a56275SMatthew G Knepley the MPI communicator of the generated DM is always the same as the communicator 248467a56275SMatthew G Knepley of the input DM. 248567a56275SMatthew G Knepley 248667a56275SMatthew G Knepley Level: intermediate 248767a56275SMatthew G Knepley 248867a56275SMatthew G Knepley .seealso: DMCreate() 248967a56275SMatthew G Knepley @*/ 249019fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M) 249167a56275SMatthew G Knepley { 249267a56275SMatthew G Knepley DM B; 249367a56275SMatthew G Knepley char convname[256]; 249467a56275SMatthew G Knepley PetscBool sametype, issame; 249567a56275SMatthew G Knepley PetscErrorCode ierr; 249667a56275SMatthew G Knepley 249767a56275SMatthew G Knepley PetscFunctionBegin; 249867a56275SMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 249967a56275SMatthew G Knepley PetscValidType(dm,1); 250067a56275SMatthew G Knepley PetscValidPointer(M,3); 2501251f4c67SDmitry Karpeev ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr); 250267a56275SMatthew G Knepley ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); 250367a56275SMatthew G Knepley { 250419fd82e9SBarry Smith PetscErrorCode (*conv)(DM, DMType, DM *) = PETSC_NULL; 250567a56275SMatthew G Knepley 250667a56275SMatthew G Knepley /* 250767a56275SMatthew G Knepley Order of precedence: 250867a56275SMatthew G Knepley 1) See if a specialized converter is known to the current DM. 250967a56275SMatthew G Knepley 2) See if a specialized converter is known to the desired DM class. 251067a56275SMatthew G Knepley 3) See if a good general converter is registered for the desired class 251167a56275SMatthew G Knepley 4) See if a good general converter is known for the current matrix. 251267a56275SMatthew G Knepley 5) Use a really basic converter. 251367a56275SMatthew G Knepley */ 251467a56275SMatthew G Knepley 251567a56275SMatthew G Knepley /* 1) See if a specialized converter is known to the current DM and the desired class */ 251667a56275SMatthew G Knepley ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr); 251767a56275SMatthew G Knepley ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr); 251867a56275SMatthew G Knepley ierr = PetscStrcat(convname,"_");CHKERRQ(ierr); 251967a56275SMatthew G Knepley ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr); 252067a56275SMatthew G Knepley ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr); 252167a56275SMatthew G Knepley ierr = PetscObjectQueryFunction((PetscObject)dm,convname,(void (**)(void))&conv);CHKERRQ(ierr); 252267a56275SMatthew G Knepley if (conv) goto foundconv; 252367a56275SMatthew G Knepley 252467a56275SMatthew G Knepley /* 2) See if a specialized converter is known to the desired DM class. */ 252567a56275SMatthew G Knepley ierr = DMCreate(((PetscObject) dm)->comm, &B);CHKERRQ(ierr); 252667a56275SMatthew G Knepley ierr = DMSetType(B, newtype);CHKERRQ(ierr); 252767a56275SMatthew G Knepley ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr); 252867a56275SMatthew G Knepley ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr); 252967a56275SMatthew G Knepley ierr = PetscStrcat(convname,"_");CHKERRQ(ierr); 253067a56275SMatthew G Knepley ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr); 253167a56275SMatthew G Knepley ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr); 253267a56275SMatthew G Knepley ierr = PetscObjectQueryFunction((PetscObject)B,convname,(void (**)(void))&conv);CHKERRQ(ierr); 253367a56275SMatthew G Knepley if (conv) { 2534fcfd50ebSBarry Smith ierr = DMDestroy(&B);CHKERRQ(ierr); 253567a56275SMatthew G Knepley goto foundconv; 253667a56275SMatthew G Knepley } 253767a56275SMatthew G Knepley 253867a56275SMatthew G Knepley #if 0 253967a56275SMatthew G Knepley /* 3) See if a good general converter is registered for the desired class */ 254067a56275SMatthew G Knepley conv = B->ops->convertfrom; 2541fcfd50ebSBarry Smith ierr = DMDestroy(&B);CHKERRQ(ierr); 254267a56275SMatthew G Knepley if (conv) goto foundconv; 254367a56275SMatthew G Knepley 254467a56275SMatthew G Knepley /* 4) See if a good general converter is known for the current matrix */ 254567a56275SMatthew G Knepley if (dm->ops->convert) { 254667a56275SMatthew G Knepley conv = dm->ops->convert; 254767a56275SMatthew G Knepley } 254867a56275SMatthew G Knepley if (conv) goto foundconv; 254967a56275SMatthew G Knepley #endif 255067a56275SMatthew G Knepley 255167a56275SMatthew G Knepley /* 5) Use a really basic converter. */ 255267a56275SMatthew G Knepley SETERRQ2(((PetscObject) dm)->comm, PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype); 255367a56275SMatthew G Knepley 255467a56275SMatthew G Knepley foundconv: 255567a56275SMatthew G Knepley ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr); 255667a56275SMatthew G Knepley ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr); 255767a56275SMatthew G Knepley ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr); 255867a56275SMatthew G Knepley } 255967a56275SMatthew G Knepley ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr); 256067a56275SMatthew G Knepley PetscFunctionReturn(0); 256167a56275SMatthew G Knepley } 2562264ace61SBarry Smith 2563264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/ 2564264ace61SBarry Smith 2565264ace61SBarry Smith #undef __FUNCT__ 2566264ace61SBarry Smith #define __FUNCT__ "DMRegister" 2567264ace61SBarry Smith /*@C 2568264ace61SBarry Smith DMRegister - See DMRegisterDynamic() 2569264ace61SBarry Smith 2570264ace61SBarry Smith Level: advanced 2571264ace61SBarry Smith @*/ 25727087cfbeSBarry Smith PetscErrorCode DMRegister(const char sname[], const char path[], const char name[], PetscErrorCode (*function)(DM)) 2573264ace61SBarry Smith { 2574264ace61SBarry Smith char fullname[PETSC_MAX_PATH_LEN]; 2575264ace61SBarry Smith PetscErrorCode ierr; 2576264ace61SBarry Smith 2577264ace61SBarry Smith PetscFunctionBegin; 2578264ace61SBarry Smith ierr = PetscStrcpy(fullname, path);CHKERRQ(ierr); 2579264ace61SBarry Smith ierr = PetscStrcat(fullname, ":");CHKERRQ(ierr); 2580264ace61SBarry Smith ierr = PetscStrcat(fullname, name);CHKERRQ(ierr); 2581140e18c1SBarry Smith ierr = PetscFunctionListAdd(PETSC_COMM_WORLD,&DMList, sname, fullname, (void (*)(void)) function);CHKERRQ(ierr); 2582264ace61SBarry Smith PetscFunctionReturn(0); 2583264ace61SBarry Smith } 2584264ace61SBarry Smith 2585264ace61SBarry Smith 2586264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/ 2587264ace61SBarry Smith #undef __FUNCT__ 2588264ace61SBarry Smith #define __FUNCT__ "DMRegisterDestroy" 2589264ace61SBarry Smith /*@C 2590264ace61SBarry Smith DMRegisterDestroy - Frees the list of DM methods that were registered by DMRegister()/DMRegisterDynamic(). 2591264ace61SBarry Smith 2592264ace61SBarry Smith Not Collective 2593264ace61SBarry Smith 2594264ace61SBarry Smith Level: advanced 2595264ace61SBarry Smith 2596264ace61SBarry Smith .keywords: DM, register, destroy 2597264ace61SBarry Smith .seealso: DMRegister(), DMRegisterAll(), DMRegisterDynamic() 2598264ace61SBarry Smith @*/ 25997087cfbeSBarry Smith PetscErrorCode DMRegisterDestroy(void) 2600264ace61SBarry Smith { 2601264ace61SBarry Smith PetscErrorCode ierr; 2602264ace61SBarry Smith 2603264ace61SBarry Smith PetscFunctionBegin; 2604140e18c1SBarry Smith ierr = PetscFunctionListDestroy(&DMList);CHKERRQ(ierr); 2605264ace61SBarry Smith DMRegisterAllCalled = PETSC_FALSE; 2606264ace61SBarry Smith PetscFunctionReturn(0); 2607264ace61SBarry Smith } 260823f975d1SBarry Smith 2609b859378eSBarry Smith #undef __FUNCT__ 2610b859378eSBarry Smith #define __FUNCT__ "DMLoad" 2611b859378eSBarry Smith /*@C 261255849f57SBarry Smith DMLoad - Loads a DM that has been stored in binary with DMView(). 2613b859378eSBarry Smith 2614b859378eSBarry Smith Collective on PetscViewer 2615b859378eSBarry Smith 2616b859378eSBarry Smith Input Parameters: 2617b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or 2618b859378eSBarry Smith some related function before a call to DMLoad(). 2619b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or 2620b859378eSBarry Smith HDF5 file viewer, obtained from PetscViewerHDF5Open() 2621b859378eSBarry Smith 2622b859378eSBarry Smith Level: intermediate 2623b859378eSBarry Smith 2624b859378eSBarry Smith Notes: 262555849f57SBarry Smith The type is determined by the data in the file, any type set into the DM before this call is ignored. 2626b859378eSBarry Smith 2627b859378eSBarry Smith Notes for advanced users: 2628b859378eSBarry Smith Most users should not need to know the details of the binary storage 2629b859378eSBarry Smith format, since DMLoad() and DMView() completely hide these details. 2630b859378eSBarry Smith But for anyone who's interested, the standard binary matrix storage 2631b859378eSBarry Smith format is 2632b859378eSBarry Smith .vb 2633b859378eSBarry Smith has not yet been determined 2634b859378eSBarry Smith .ve 2635b859378eSBarry Smith 2636b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad() 2637b859378eSBarry Smith @*/ 2638b859378eSBarry Smith PetscErrorCode DMLoad(DM newdm, PetscViewer viewer) 2639b859378eSBarry Smith { 2640b859378eSBarry Smith PetscErrorCode ierr; 264132c0f0efSBarry Smith PetscBool isbinary; 264255849f57SBarry Smith PetscInt classid; 264332c0f0efSBarry Smith char type[256]; 2644b859378eSBarry Smith 2645b859378eSBarry Smith PetscFunctionBegin; 2646b859378eSBarry Smith PetscValidHeaderSpecific(newdm,DM_CLASSID,1); 2647b859378eSBarry Smith PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); 264832c0f0efSBarry Smith ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr); 264932c0f0efSBarry Smith if (!isbinary) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen()"); 2650b859378eSBarry Smith 265132c0f0efSBarry Smith ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr); 265232c0f0efSBarry Smith if (classid != DM_FILE_CLASSID) SETERRQ(((PetscObject)newdm)->comm,PETSC_ERR_ARG_WRONG,"Not DM next in file"); 265332c0f0efSBarry Smith ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr); 265432c0f0efSBarry Smith ierr = DMSetType(newdm, type);CHKERRQ(ierr); 26552d53ad75SBarry Smith if (newdm->ops->load) { 2656b859378eSBarry Smith ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr); 26572d53ad75SBarry Smith } 2658b859378eSBarry Smith PetscFunctionReturn(0); 2659b859378eSBarry Smith } 2660b859378eSBarry Smith 26617da65231SMatthew G Knepley /******************************** FEM Support **********************************/ 26627da65231SMatthew G Knepley 26637da65231SMatthew G Knepley #undef __FUNCT__ 26647da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector" 2665a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[]) 2666a6dfd86eSKarl Rupp { 26671d47ebbbSSatish Balay PetscInt f; 26681b30c384SMatthew G Knepley PetscErrorCode ierr; 26691b30c384SMatthew G Knepley 26707da65231SMatthew G Knepley PetscFunctionBegin; 267174778d6cSJed Brown ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr); 26721d47ebbbSSatish Balay for (f = 0; f < len; ++f) { 267374778d6cSJed Brown ierr = PetscPrintf(PETSC_COMM_SELF, " | %G |\n", PetscRealPart(x[f]));CHKERRQ(ierr); 26747da65231SMatthew G Knepley } 26757da65231SMatthew G Knepley PetscFunctionReturn(0); 26767da65231SMatthew G Knepley } 26777da65231SMatthew G Knepley 26787da65231SMatthew G Knepley #undef __FUNCT__ 26797da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix" 2680a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[]) 2681a6dfd86eSKarl Rupp { 26821b30c384SMatthew G Knepley PetscInt f, g; 26837da65231SMatthew G Knepley PetscErrorCode ierr; 26847da65231SMatthew G Knepley 26857da65231SMatthew G Knepley PetscFunctionBegin; 268674778d6cSJed Brown ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr); 26871d47ebbbSSatish Balay for (f = 0; f < rows; ++f) { 268874778d6cSJed Brown ierr = PetscPrintf(PETSC_COMM_SELF, " |");CHKERRQ(ierr); 26891d47ebbbSSatish Balay for (g = 0; g < cols; ++g) { 269074778d6cSJed Brown ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5G", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr); 26917da65231SMatthew G Knepley } 269274778d6cSJed Brown ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr); 26937da65231SMatthew G Knepley } 26947da65231SMatthew G Knepley PetscFunctionReturn(0); 26957da65231SMatthew G Knepley } 2696e7c4fc90SDmitry Karpeev 2697970e74d5SMatthew G Knepley #undef __FUNCT__ 269888ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection" 269988ed4aceSMatthew G Knepley /*@ 270088ed4aceSMatthew G Knepley DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM. 270188ed4aceSMatthew G Knepley 270288ed4aceSMatthew G Knepley Input Parameter: 270388ed4aceSMatthew G Knepley . dm - The DM 270488ed4aceSMatthew G Knepley 270588ed4aceSMatthew G Knepley Output Parameter: 270688ed4aceSMatthew G Knepley . section - The PetscSection 270788ed4aceSMatthew G Knepley 270888ed4aceSMatthew G Knepley Level: intermediate 270988ed4aceSMatthew G Knepley 271088ed4aceSMatthew G Knepley Note: This gets a borrowed reference, so the user should not destroy this PetscSection. 271188ed4aceSMatthew G Knepley 271288ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection() 271388ed4aceSMatthew G Knepley @*/ 27140adebc6cSBarry Smith PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section) 27150adebc6cSBarry Smith { 271688ed4aceSMatthew G Knepley PetscFunctionBegin; 271788ed4aceSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 271888ed4aceSMatthew G Knepley PetscValidPointer(section, 2); 271988ed4aceSMatthew G Knepley *section = dm->defaultSection; 272088ed4aceSMatthew G Knepley PetscFunctionReturn(0); 272188ed4aceSMatthew G Knepley } 272288ed4aceSMatthew G Knepley 272388ed4aceSMatthew G Knepley #undef __FUNCT__ 272488ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection" 272588ed4aceSMatthew G Knepley /*@ 272688ed4aceSMatthew G Knepley DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM. 272788ed4aceSMatthew G Knepley 272888ed4aceSMatthew G Knepley Input Parameters: 272988ed4aceSMatthew G Knepley + dm - The DM 273088ed4aceSMatthew G Knepley - section - The PetscSection 273188ed4aceSMatthew G Knepley 273288ed4aceSMatthew G Knepley Level: intermediate 273388ed4aceSMatthew G Knepley 273488ed4aceSMatthew G Knepley Note: Any existing Section will be destroyed 273588ed4aceSMatthew G Knepley 273688ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection() 273788ed4aceSMatthew G Knepley @*/ 27380adebc6cSBarry Smith PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section) 27390adebc6cSBarry Smith { 2740af122d2aSMatthew G Knepley PetscInt numFields; 2741af122d2aSMatthew G Knepley PetscInt f; 274288ed4aceSMatthew G Knepley PetscErrorCode ierr; 274388ed4aceSMatthew G Knepley 274488ed4aceSMatthew G Knepley PetscFunctionBegin; 274588ed4aceSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 274688ed4aceSMatthew G Knepley ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr); 274788ed4aceSMatthew G Knepley ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr); 274888ed4aceSMatthew G Knepley dm->defaultSection = section; 2749af122d2aSMatthew G Knepley ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr); 2750af122d2aSMatthew G Knepley if (numFields) { 2751af122d2aSMatthew G Knepley ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr); 2752af122d2aSMatthew G Knepley for (f = 0; f < numFields; ++f) { 2753af122d2aSMatthew G Knepley const char *name; 2754af122d2aSMatthew G Knepley 2755af122d2aSMatthew G Knepley ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr); 2756af122d2aSMatthew G Knepley ierr = PetscObjectSetName(dm->fields[f], name);CHKERRQ(ierr); 2757af122d2aSMatthew G Knepley } 2758af122d2aSMatthew G Knepley } 275988ed4aceSMatthew G Knepley PetscFunctionReturn(0); 276088ed4aceSMatthew G Knepley } 276188ed4aceSMatthew G Knepley 276288ed4aceSMatthew G Knepley #undef __FUNCT__ 276388ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection" 276488ed4aceSMatthew G Knepley /*@ 276588ed4aceSMatthew G Knepley DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM. 276688ed4aceSMatthew G Knepley 27678b1ab98fSJed Brown Collective on DM 27688b1ab98fSJed Brown 276988ed4aceSMatthew G Knepley Input Parameter: 277088ed4aceSMatthew G Knepley . dm - The DM 277188ed4aceSMatthew G Knepley 277288ed4aceSMatthew G Knepley Output Parameter: 277388ed4aceSMatthew G Knepley . section - The PetscSection 277488ed4aceSMatthew G Knepley 277588ed4aceSMatthew G Knepley Level: intermediate 277688ed4aceSMatthew G Knepley 277788ed4aceSMatthew G Knepley Note: This gets a borrowed reference, so the user should not destroy this PetscSection. 277888ed4aceSMatthew G Knepley 277988ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection() 278088ed4aceSMatthew G Knepley @*/ 27810adebc6cSBarry Smith PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section) 27820adebc6cSBarry Smith { 278388ed4aceSMatthew G Knepley PetscErrorCode ierr; 278488ed4aceSMatthew G Knepley 278588ed4aceSMatthew G Knepley PetscFunctionBegin; 278688ed4aceSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 278788ed4aceSMatthew G Knepley PetscValidPointer(section, 2); 278888ed4aceSMatthew G Knepley if (!dm->defaultGlobalSection) { 278940e8a239SMatthew 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"); 2790b21d0597SMatthew G Knepley ierr = PetscSectionCreateGlobalSection(dm->defaultSection, dm->sf, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr); 27918b1ab98fSJed Brown ierr = PetscSectionGetValueLayout(((PetscObject)dm)->comm,dm->defaultGlobalSection,&dm->map);CHKERRQ(ierr); 279288ed4aceSMatthew G Knepley } 279388ed4aceSMatthew G Knepley *section = dm->defaultGlobalSection; 279488ed4aceSMatthew G Knepley PetscFunctionReturn(0); 279588ed4aceSMatthew G Knepley } 279688ed4aceSMatthew G Knepley 279788ed4aceSMatthew G Knepley #undef __FUNCT__ 2798b21d0597SMatthew G Knepley #define __FUNCT__ "DMSetDefaultGlobalSection" 2799b21d0597SMatthew G Knepley /*@ 2800b21d0597SMatthew G Knepley DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM. 2801b21d0597SMatthew G Knepley 2802b21d0597SMatthew G Knepley Input Parameters: 2803b21d0597SMatthew G Knepley + dm - The DM 2804b21d0597SMatthew G Knepley - section - The PetscSection 2805b21d0597SMatthew G Knepley 2806b21d0597SMatthew G Knepley Level: intermediate 2807b21d0597SMatthew G Knepley 2808b21d0597SMatthew G Knepley Note: Any existing Section will be destroyed 2809b21d0597SMatthew G Knepley 2810b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection() 2811b21d0597SMatthew G Knepley @*/ 28120adebc6cSBarry Smith PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section) 28130adebc6cSBarry Smith { 2814b21d0597SMatthew G Knepley PetscErrorCode ierr; 2815b21d0597SMatthew G Knepley 2816b21d0597SMatthew G Knepley PetscFunctionBegin; 2817b21d0597SMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 2818b21d0597SMatthew G Knepley ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr); 2819b21d0597SMatthew G Knepley dm->defaultGlobalSection = section; 2820b21d0597SMatthew G Knepley PetscFunctionReturn(0); 2821b21d0597SMatthew G Knepley } 2822b21d0597SMatthew G Knepley 2823b21d0597SMatthew G Knepley #undef __FUNCT__ 282488ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF" 282588ed4aceSMatthew G Knepley /*@ 282688ed4aceSMatthew G Knepley DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set, 282788ed4aceSMatthew G Knepley it is created from the default PetscSection layouts in the DM. 282888ed4aceSMatthew G Knepley 282988ed4aceSMatthew G Knepley Input Parameter: 283088ed4aceSMatthew G Knepley . dm - The DM 283188ed4aceSMatthew G Knepley 283288ed4aceSMatthew G Knepley Output Parameter: 283388ed4aceSMatthew G Knepley . sf - The PetscSF 283488ed4aceSMatthew G Knepley 283588ed4aceSMatthew G Knepley Level: intermediate 283688ed4aceSMatthew G Knepley 283788ed4aceSMatthew G Knepley Note: This gets a borrowed reference, so the user should not destroy this PetscSF. 283888ed4aceSMatthew G Knepley 283988ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF() 284088ed4aceSMatthew G Knepley @*/ 28410adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf) 28420adebc6cSBarry Smith { 284388ed4aceSMatthew G Knepley PetscInt nroots; 284488ed4aceSMatthew G Knepley PetscErrorCode ierr; 284588ed4aceSMatthew G Knepley 284688ed4aceSMatthew G Knepley PetscFunctionBegin; 284788ed4aceSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 284888ed4aceSMatthew G Knepley PetscValidPointer(sf, 2); 284988ed4aceSMatthew G Knepley ierr = PetscSFGetGraph(dm->defaultSF, &nroots, PETSC_NULL, PETSC_NULL, PETSC_NULL);CHKERRQ(ierr); 285088ed4aceSMatthew G Knepley if (nroots < 0) { 285188ed4aceSMatthew G Knepley PetscSection section, gSection; 285288ed4aceSMatthew G Knepley 285388ed4aceSMatthew G Knepley ierr = DMGetDefaultSection(dm, §ion);CHKERRQ(ierr); 285431ea6d37SMatthew G Knepley if (section) { 285588ed4aceSMatthew G Knepley ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr); 285688ed4aceSMatthew G Knepley ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr); 285731ea6d37SMatthew G Knepley } else { 285831ea6d37SMatthew G Knepley *sf = PETSC_NULL; 285931ea6d37SMatthew G Knepley PetscFunctionReturn(0); 286031ea6d37SMatthew G Knepley } 286188ed4aceSMatthew G Knepley } 286288ed4aceSMatthew G Knepley *sf = dm->defaultSF; 286388ed4aceSMatthew G Knepley PetscFunctionReturn(0); 286488ed4aceSMatthew G Knepley } 286588ed4aceSMatthew G Knepley 286688ed4aceSMatthew G Knepley #undef __FUNCT__ 286788ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF" 286888ed4aceSMatthew G Knepley /*@ 286988ed4aceSMatthew G Knepley DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM 287088ed4aceSMatthew G Knepley 287188ed4aceSMatthew G Knepley Input Parameters: 287288ed4aceSMatthew G Knepley + dm - The DM 287388ed4aceSMatthew G Knepley - sf - The PetscSF 287488ed4aceSMatthew G Knepley 287588ed4aceSMatthew G Knepley Level: intermediate 287688ed4aceSMatthew G Knepley 287788ed4aceSMatthew G Knepley Note: Any previous SF is destroyed 287888ed4aceSMatthew G Knepley 287988ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF() 288088ed4aceSMatthew G Knepley @*/ 28810adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf) 28820adebc6cSBarry Smith { 288388ed4aceSMatthew G Knepley PetscErrorCode ierr; 288488ed4aceSMatthew G Knepley 288588ed4aceSMatthew G Knepley PetscFunctionBegin; 288688ed4aceSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 288788ed4aceSMatthew G Knepley PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2); 288888ed4aceSMatthew G Knepley ierr = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr); 288988ed4aceSMatthew G Knepley dm->defaultSF = sf; 289088ed4aceSMatthew G Knepley PetscFunctionReturn(0); 289188ed4aceSMatthew G Knepley } 289288ed4aceSMatthew G Knepley 289388ed4aceSMatthew G Knepley #undef __FUNCT__ 289488ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF" 289588ed4aceSMatthew G Knepley /*@C 289688ed4aceSMatthew G Knepley DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections 289788ed4aceSMatthew G Knepley describing the data layout. 289888ed4aceSMatthew G Knepley 289988ed4aceSMatthew G Knepley Input Parameters: 290088ed4aceSMatthew G Knepley + dm - The DM 290188ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout 290288ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout 290388ed4aceSMatthew G Knepley 290488ed4aceSMatthew G Knepley Level: intermediate 290588ed4aceSMatthew G Knepley 290688ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF() 290788ed4aceSMatthew G Knepley @*/ 290888ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection) 290988ed4aceSMatthew G Knepley { 291088ed4aceSMatthew G Knepley MPI_Comm comm = ((PetscObject) dm)->comm; 291188ed4aceSMatthew G Knepley PetscLayout layout; 291288ed4aceSMatthew G Knepley const PetscInt *ranges; 291388ed4aceSMatthew G Knepley PetscInt *local; 291488ed4aceSMatthew G Knepley PetscSFNode *remote; 291588ed4aceSMatthew G Knepley PetscInt pStart, pEnd, p, nroots, nleaves, l; 291688ed4aceSMatthew G Knepley PetscMPIInt size, rank; 291788ed4aceSMatthew G Knepley PetscErrorCode ierr; 291888ed4aceSMatthew G Knepley 291988ed4aceSMatthew G Knepley PetscFunctionBegin; 292088ed4aceSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 292188ed4aceSMatthew G Knepley ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr); 292288ed4aceSMatthew G Knepley ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr); 292388ed4aceSMatthew G Knepley ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr); 292488ed4aceSMatthew G Knepley ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr); 292588ed4aceSMatthew G Knepley ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr); 292688ed4aceSMatthew G Knepley ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr); 292788ed4aceSMatthew G Knepley ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr); 292888ed4aceSMatthew G Knepley ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr); 292988ed4aceSMatthew G Knepley ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr); 293088ed4aceSMatthew G Knepley for (p = pStart, nleaves = 0; p < pEnd; ++p) { 29316636e97aSMatthew G Knepley PetscInt gdof, gcdof; 293288ed4aceSMatthew G Knepley 29336636e97aSMatthew G Knepley ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr); 29346636e97aSMatthew G Knepley ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr); 29356636e97aSMatthew G Knepley nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof; 293688ed4aceSMatthew G Knepley } 293788ed4aceSMatthew G Knepley ierr = PetscMalloc(nleaves * sizeof(PetscInt), &local);CHKERRQ(ierr); 293888ed4aceSMatthew G Knepley ierr = PetscMalloc(nleaves * sizeof(PetscSFNode), &remote);CHKERRQ(ierr); 293988ed4aceSMatthew G Knepley for (p = pStart, l = 0; p < pEnd; ++p) { 29401f588964SMatthew G Knepley const PetscInt *cind; 29416636e97aSMatthew G Knepley PetscInt dof, cdof, off, gdof, gcdof, goff, gsize, d, c; 294288ed4aceSMatthew G Knepley 294388ed4aceSMatthew G Knepley ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr); 294488ed4aceSMatthew G Knepley ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr); 294588ed4aceSMatthew G Knepley ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr); 294688ed4aceSMatthew G Knepley ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr); 294788ed4aceSMatthew G Knepley ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr); 29486636e97aSMatthew G Knepley ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr); 294988ed4aceSMatthew G Knepley ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr); 29506636e97aSMatthew G Knepley if (!gdof) continue; /* Censored point */ 29516636e97aSMatthew G Knepley gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof; 29526636e97aSMatthew G Knepley if (gsize != dof-cdof) { 2953057b4bcdSMatthew 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); 29546636e97aSMatthew G Knepley cdof = 0; /* Ignore constraints */ 29556636e97aSMatthew G Knepley } 295688ed4aceSMatthew G Knepley for (d = 0, c = 0; d < dof; ++d) { 295788ed4aceSMatthew G Knepley if ((c < cdof) && (cind[c] == d)) {++c; continue;} 295888ed4aceSMatthew G Knepley local[l+d-c] = off+d; 295988ed4aceSMatthew G Knepley } 296088ed4aceSMatthew G Knepley if (gdof < 0) { 29616636e97aSMatthew G Knepley for (d = 0; d < gsize; ++d, ++l) { 296288ed4aceSMatthew G Knepley PetscInt offset = -(goff+1) + d, r; 296388ed4aceSMatthew G Knepley 296431d3f06eSJed Brown ierr = PetscFindInt(offset,size,ranges,&r);CHKERRQ(ierr); 296531d3f06eSJed Brown if (r < 0) r = -(r+2); 296688ed4aceSMatthew G Knepley remote[l].rank = r; 296788ed4aceSMatthew G Knepley remote[l].index = offset - ranges[r]; 296888ed4aceSMatthew G Knepley } 296988ed4aceSMatthew G Knepley } else { 29706636e97aSMatthew G Knepley for (d = 0; d < gsize; ++d, ++l) { 297188ed4aceSMatthew G Knepley remote[l].rank = rank; 297288ed4aceSMatthew G Knepley remote[l].index = goff+d - ranges[rank]; 297388ed4aceSMatthew G Knepley } 297488ed4aceSMatthew G Knepley } 297588ed4aceSMatthew G Knepley } 29766636e97aSMatthew G Knepley if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves); 297788ed4aceSMatthew G Knepley ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr); 297888ed4aceSMatthew G Knepley ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr); 297988ed4aceSMatthew G Knepley PetscFunctionReturn(0); 298088ed4aceSMatthew G Knepley } 2981af122d2aSMatthew G Knepley 2982af122d2aSMatthew G Knepley #undef __FUNCT__ 2983b21d0597SMatthew G Knepley #define __FUNCT__ "DMGetPointSF" 2984b21d0597SMatthew G Knepley /*@ 2985b21d0597SMatthew G Knepley DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM. 2986b21d0597SMatthew G Knepley 2987b21d0597SMatthew G Knepley Input Parameter: 2988b21d0597SMatthew G Knepley . dm - The DM 2989b21d0597SMatthew G Knepley 2990b21d0597SMatthew G Knepley Output Parameter: 2991b21d0597SMatthew G Knepley . sf - The PetscSF 2992b21d0597SMatthew G Knepley 2993b21d0597SMatthew G Knepley Level: intermediate 2994b21d0597SMatthew G Knepley 2995b21d0597SMatthew G Knepley Note: This gets a borrowed reference, so the user should not destroy this PetscSF. 2996b21d0597SMatthew G Knepley 2997057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF() 2998b21d0597SMatthew G Knepley @*/ 29990adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf) 30000adebc6cSBarry Smith { 3001b21d0597SMatthew G Knepley PetscFunctionBegin; 3002b21d0597SMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 3003b21d0597SMatthew G Knepley PetscValidPointer(sf, 2); 3004b21d0597SMatthew G Knepley *sf = dm->sf; 3005b21d0597SMatthew G Knepley PetscFunctionReturn(0); 3006b21d0597SMatthew G Knepley } 3007b21d0597SMatthew G Knepley 3008b21d0597SMatthew G Knepley #undef __FUNCT__ 3009057b4bcdSMatthew G Knepley #define __FUNCT__ "DMSetPointSF" 3010057b4bcdSMatthew G Knepley /*@ 3011057b4bcdSMatthew G Knepley DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM. 3012057b4bcdSMatthew G Knepley 3013057b4bcdSMatthew G Knepley Input Parameters: 3014057b4bcdSMatthew G Knepley + dm - The DM 3015057b4bcdSMatthew G Knepley - sf - The PetscSF 3016057b4bcdSMatthew G Knepley 3017057b4bcdSMatthew G Knepley Level: intermediate 3018057b4bcdSMatthew G Knepley 3019057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF() 3020057b4bcdSMatthew G Knepley @*/ 30210adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf) 30220adebc6cSBarry Smith { 3023057b4bcdSMatthew G Knepley PetscErrorCode ierr; 3024057b4bcdSMatthew G Knepley 3025057b4bcdSMatthew G Knepley PetscFunctionBegin; 3026057b4bcdSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 3027057b4bcdSMatthew G Knepley PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1); 3028057b4bcdSMatthew G Knepley ierr = PetscSFDestroy(&dm->sf);CHKERRQ(ierr); 3029057b4bcdSMatthew G Knepley ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr); 3030057b4bcdSMatthew G Knepley dm->sf = sf; 3031057b4bcdSMatthew G Knepley PetscFunctionReturn(0); 3032057b4bcdSMatthew G Knepley } 3033057b4bcdSMatthew G Knepley 3034057b4bcdSMatthew G Knepley #undef __FUNCT__ 3035af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetNumFields" 3036af122d2aSMatthew G Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields) 3037af122d2aSMatthew G Knepley { 3038af122d2aSMatthew G Knepley PetscFunctionBegin; 3039af122d2aSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 3040af122d2aSMatthew G Knepley PetscValidPointer(numFields, 2); 3041af122d2aSMatthew G Knepley *numFields = dm->numFields; 3042af122d2aSMatthew G Knepley PetscFunctionReturn(0); 3043af122d2aSMatthew G Knepley } 3044af122d2aSMatthew G Knepley 3045af122d2aSMatthew G Knepley #undef __FUNCT__ 3046af122d2aSMatthew G Knepley #define __FUNCT__ "DMSetNumFields" 3047af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields) 3048af122d2aSMatthew G Knepley { 3049af122d2aSMatthew G Knepley PetscInt f; 3050af122d2aSMatthew G Knepley PetscErrorCode ierr; 3051af122d2aSMatthew G Knepley 3052af122d2aSMatthew G Knepley PetscFunctionBegin; 3053af122d2aSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 3054af122d2aSMatthew G Knepley for (f = 0; f < dm->numFields; ++f) { 3055af122d2aSMatthew G Knepley ierr = PetscObjectDestroy(&dm->fields[f]);CHKERRQ(ierr); 3056af122d2aSMatthew G Knepley } 3057af122d2aSMatthew G Knepley ierr = PetscFree(dm->fields);CHKERRQ(ierr); 3058af122d2aSMatthew G Knepley dm->numFields = numFields; 3059af122d2aSMatthew G Knepley ierr = PetscMalloc(dm->numFields * sizeof(PetscObject), &dm->fields);CHKERRQ(ierr); 3060af122d2aSMatthew G Knepley for (f = 0; f < dm->numFields; ++f) { 3061af122d2aSMatthew G Knepley ierr = PetscContainerCreate(((PetscObject) dm)->comm, (PetscContainer *) &dm->fields[f]);CHKERRQ(ierr); 3062af122d2aSMatthew G Knepley } 3063af122d2aSMatthew G Knepley PetscFunctionReturn(0); 3064af122d2aSMatthew G Knepley } 3065af122d2aSMatthew G Knepley 3066af122d2aSMatthew G Knepley #undef __FUNCT__ 3067af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetField" 3068af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field) 3069af122d2aSMatthew G Knepley { 3070af122d2aSMatthew G Knepley PetscFunctionBegin; 3071af122d2aSMatthew G Knepley PetscValidHeaderSpecific(dm, DM_CLASSID, 1); 3072af122d2aSMatthew G Knepley PetscValidPointer(field, 2); 3073af122d2aSMatthew G Knepley if (!dm->fields) SETERRQ(((PetscObject) dm)->comm, PETSC_ERR_ARG_WRONGSTATE, "Fields have not been setup in this DM. Call DMSetNumFields()"); 3074af122d2aSMatthew 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); 3075af122d2aSMatthew G Knepley *field = dm->fields[f]; 3076af122d2aSMatthew G Knepley PetscFunctionReturn(0); 3077af122d2aSMatthew G Knepley } 30786636e97aSMatthew G Knepley 30796636e97aSMatthew G Knepley #undef __FUNCT__ 30806636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinates" 30816636e97aSMatthew G Knepley /*@ 30826636e97aSMatthew G Knepley DMSetCoordinates - Sets into the DM a global vector that holds the coordinates 30836636e97aSMatthew G Knepley 30846636e97aSMatthew G Knepley Collective on DM 30856636e97aSMatthew G Knepley 30866636e97aSMatthew G Knepley Input Parameters: 30876636e97aSMatthew G Knepley + dm - the DM 30886636e97aSMatthew G Knepley - c - coordinate vector 30896636e97aSMatthew G Knepley 30906636e97aSMatthew G Knepley Note: 30916636e97aSMatthew G Knepley The coordinates do include those for ghost points, which are in the local vector 30926636e97aSMatthew G Knepley 30936636e97aSMatthew G Knepley Level: intermediate 30946636e97aSMatthew G Knepley 30956636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates 30966636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM() 30976636e97aSMatthew G Knepley @*/ 30986636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c) 30996636e97aSMatthew G Knepley { 31006636e97aSMatthew G Knepley PetscErrorCode ierr; 31016636e97aSMatthew G Knepley 31026636e97aSMatthew G Knepley PetscFunctionBegin; 31036636e97aSMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 31046636e97aSMatthew G Knepley PetscValidHeaderSpecific(c,VEC_CLASSID,2); 31056636e97aSMatthew G Knepley ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr); 31066636e97aSMatthew G Knepley ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr); 31076636e97aSMatthew G Knepley dm->coordinates = c; 31086636e97aSMatthew G Knepley ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr); 31096636e97aSMatthew G Knepley PetscFunctionReturn(0); 31106636e97aSMatthew G Knepley } 31116636e97aSMatthew G Knepley 31126636e97aSMatthew G Knepley #undef __FUNCT__ 31136636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinatesLocal" 31146636e97aSMatthew G Knepley /*@ 31156636e97aSMatthew G Knepley DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates 31166636e97aSMatthew G Knepley 31176636e97aSMatthew G Knepley Collective on DM 31186636e97aSMatthew G Knepley 31196636e97aSMatthew G Knepley Input Parameters: 31206636e97aSMatthew G Knepley + dm - the DM 31216636e97aSMatthew G Knepley - c - coordinate vector 31226636e97aSMatthew G Knepley 31236636e97aSMatthew G Knepley Note: 31246636e97aSMatthew G Knepley The coordinates of ghost points can be set using DMSetCoordinates() 31256636e97aSMatthew G Knepley followed by DMGetCoordinatesLocal(). This is intended to enable the 31266636e97aSMatthew G Knepley setting of ghost coordinates outside of the domain. 31276636e97aSMatthew G Knepley 31286636e97aSMatthew G Knepley Level: intermediate 31296636e97aSMatthew G Knepley 31306636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates 31316636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM() 31326636e97aSMatthew G Knepley @*/ 31336636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c) 31346636e97aSMatthew G Knepley { 31356636e97aSMatthew G Knepley PetscErrorCode ierr; 31366636e97aSMatthew G Knepley 31376636e97aSMatthew G Knepley PetscFunctionBegin; 31386636e97aSMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 31396636e97aSMatthew G Knepley PetscValidHeaderSpecific(c,VEC_CLASSID,2); 31406636e97aSMatthew G Knepley ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr); 31416636e97aSMatthew G Knepley ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr); 31426636e97aSMatthew G Knepley dm->coordinatesLocal = c; 31436636e97aSMatthew G Knepley ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr); 31446636e97aSMatthew G Knepley PetscFunctionReturn(0); 31456636e97aSMatthew G Knepley } 31466636e97aSMatthew G Knepley 31476636e97aSMatthew G Knepley #undef __FUNCT__ 31486636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinates" 31496636e97aSMatthew G Knepley /*@ 31506636e97aSMatthew G Knepley DMGetCoordinates - Gets a global vector with the coordinates associated with the DM. 31516636e97aSMatthew G Knepley 31526636e97aSMatthew G Knepley Not Collective 31536636e97aSMatthew G Knepley 31546636e97aSMatthew G Knepley Input Parameter: 31556636e97aSMatthew G Knepley . dm - the DM 31566636e97aSMatthew G Knepley 31576636e97aSMatthew G Knepley Output Parameter: 31586636e97aSMatthew G Knepley . c - global coordinate vector 31596636e97aSMatthew G Knepley 31606636e97aSMatthew G Knepley Note: 31616636e97aSMatthew G Knepley This is a borrowed reference, so the user should NOT destroy this vector 31626636e97aSMatthew G Knepley 31636636e97aSMatthew G Knepley Each process has only the local coordinates (does NOT have the ghost coordinates). 31646636e97aSMatthew G Knepley 31656636e97aSMatthew G Knepley For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...) 31666636e97aSMatthew G Knepley and (x_0,y_0,z_0,x_1,y_1,z_1...) 31676636e97aSMatthew G Knepley 31686636e97aSMatthew G Knepley Level: intermediate 31696636e97aSMatthew G Knepley 31706636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates 31716636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM() 31726636e97aSMatthew G Knepley @*/ 31736636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c) 31746636e97aSMatthew G Knepley { 31756636e97aSMatthew G Knepley PetscErrorCode ierr; 31766636e97aSMatthew G Knepley 31776636e97aSMatthew G Knepley PetscFunctionBegin; 31786636e97aSMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 31796636e97aSMatthew G Knepley PetscValidPointer(c,2); 31801f588964SMatthew G Knepley if (!dm->coordinates && dm->coordinatesLocal) { 318187743a01SMatthew G Knepley DM cdm = PETSC_NULL; 31826636e97aSMatthew G Knepley 31836636e97aSMatthew G Knepley ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr); 31846636e97aSMatthew G Knepley ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr); 31856636e97aSMatthew G Knepley ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr); 31866636e97aSMatthew G Knepley ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr); 31876636e97aSMatthew G Knepley ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr); 31886636e97aSMatthew G Knepley } 31896636e97aSMatthew G Knepley *c = dm->coordinates; 31906636e97aSMatthew G Knepley PetscFunctionReturn(0); 31916636e97aSMatthew G Knepley } 31926636e97aSMatthew G Knepley 31936636e97aSMatthew G Knepley #undef __FUNCT__ 31946636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinatesLocal" 31956636e97aSMatthew G Knepley /*@ 31966636e97aSMatthew G Knepley DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM. 31976636e97aSMatthew G Knepley 31986636e97aSMatthew G Knepley Collective on DM 31996636e97aSMatthew G Knepley 32006636e97aSMatthew G Knepley Input Parameter: 32016636e97aSMatthew G Knepley . dm - the DM 32026636e97aSMatthew G Knepley 32036636e97aSMatthew G Knepley Output Parameter: 32046636e97aSMatthew G Knepley . c - coordinate vector 32056636e97aSMatthew G Knepley 32066636e97aSMatthew G Knepley Note: 32076636e97aSMatthew G Knepley This is a borrowed reference, so the user should NOT destroy this vector 32086636e97aSMatthew G Knepley 32096636e97aSMatthew G Knepley Each process has the local and ghost coordinates 32106636e97aSMatthew G Knepley 32116636e97aSMatthew G Knepley For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...) 32126636e97aSMatthew G Knepley and (x_0,y_0,z_0,x_1,y_1,z_1...) 32136636e97aSMatthew G Knepley 32146636e97aSMatthew G Knepley Level: intermediate 32156636e97aSMatthew G Knepley 32166636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates 32176636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM() 32186636e97aSMatthew G Knepley @*/ 32196636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c) 32206636e97aSMatthew G Knepley { 32216636e97aSMatthew G Knepley PetscErrorCode ierr; 32226636e97aSMatthew G Knepley 32236636e97aSMatthew G Knepley PetscFunctionBegin; 32246636e97aSMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 32256636e97aSMatthew G Knepley PetscValidPointer(c,2); 32261f588964SMatthew G Knepley if (!dm->coordinatesLocal && dm->coordinates) { 322787743a01SMatthew G Knepley DM cdm = PETSC_NULL; 32286636e97aSMatthew G Knepley 32296636e97aSMatthew G Knepley ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr); 32306636e97aSMatthew G Knepley ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr); 32316636e97aSMatthew G Knepley ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr); 32326636e97aSMatthew G Knepley ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr); 32336636e97aSMatthew G Knepley ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr); 32346636e97aSMatthew G Knepley } 32356636e97aSMatthew G Knepley *c = dm->coordinatesLocal; 32366636e97aSMatthew G Knepley PetscFunctionReturn(0); 32376636e97aSMatthew G Knepley } 32386636e97aSMatthew G Knepley 32396636e97aSMatthew G Knepley #undef __FUNCT__ 32406636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinateDM" 32416636e97aSMatthew G Knepley /*@ 32426636e97aSMatthew G Knepley DMGetCoordinateDM - Gets the DM that scatters between global and local coordinates 32436636e97aSMatthew G Knepley 32446636e97aSMatthew G Knepley Collective on DM 32456636e97aSMatthew G Knepley 32466636e97aSMatthew G Knepley Input Parameter: 32476636e97aSMatthew G Knepley . dm - the DM 32486636e97aSMatthew G Knepley 32496636e97aSMatthew G Knepley Output Parameter: 32506636e97aSMatthew G Knepley . cdm - coordinate DM 32516636e97aSMatthew G Knepley 32526636e97aSMatthew G Knepley Level: intermediate 32536636e97aSMatthew G Knepley 32546636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates 32556636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal() 32566636e97aSMatthew G Knepley @*/ 32576636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm) 32586636e97aSMatthew G Knepley { 32596636e97aSMatthew G Knepley PetscErrorCode ierr; 32606636e97aSMatthew G Knepley 32616636e97aSMatthew G Knepley PetscFunctionBegin; 32626636e97aSMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 32636636e97aSMatthew G Knepley PetscValidPointer(cdm,2); 32646636e97aSMatthew G Knepley if (!dm->coordinateDM) { 32656636e97aSMatthew G Knepley if (!dm->ops->createcoordinatedm) SETERRQ(((PetscObject) dm)->comm, PETSC_ERR_SUP, "Unable to create coordinates for this DM"); 32666636e97aSMatthew G Knepley ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr); 32676636e97aSMatthew G Knepley } 32686636e97aSMatthew G Knepley *cdm = dm->coordinateDM; 32696636e97aSMatthew G Knepley PetscFunctionReturn(0); 32706636e97aSMatthew G Knepley } 3271e87bb0d3SMatthew G Knepley 3272e87bb0d3SMatthew G Knepley #undef __FUNCT__ 3273e87bb0d3SMatthew G Knepley #define __FUNCT__ "DMLocatePoints" 3274e87bb0d3SMatthew G Knepley /*@ 3275e87bb0d3SMatthew G Knepley DMLocatePoints - Locate the points in v in the mesh and return an IS of the containing cells 3276e87bb0d3SMatthew G Knepley 3277e87bb0d3SMatthew G Knepley Not collective 3278e87bb0d3SMatthew G Knepley 3279e87bb0d3SMatthew G Knepley Input Parameters: 3280e87bb0d3SMatthew G Knepley + dm - The DM 3281e87bb0d3SMatthew G Knepley - v - The Vec of points 3282e87bb0d3SMatthew G Knepley 328361e3bb9bSMatthew G Knepley Output Parameter: 3284e87bb0d3SMatthew G Knepley . cells - The local cell numbers for cells which contain the points 3285e87bb0d3SMatthew G Knepley 3286e87bb0d3SMatthew G Knepley Level: developer 328761e3bb9bSMatthew G Knepley 328861e3bb9bSMatthew G Knepley .keywords: point location, mesh 328961e3bb9bSMatthew G Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal() 329061e3bb9bSMatthew G Knepley @*/ 3291e87bb0d3SMatthew G Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, IS *cells) 3292e87bb0d3SMatthew G Knepley { 3293735aa83eSMatthew G Knepley PetscErrorCode ierr; 3294735aa83eSMatthew G Knepley 3295e87bb0d3SMatthew G Knepley PetscFunctionBegin; 3296e87bb0d3SMatthew G Knepley PetscValidHeaderSpecific(dm,DM_CLASSID,1); 3297e87bb0d3SMatthew G Knepley PetscValidHeaderSpecific(v,VEC_CLASSID,2); 3298e87bb0d3SMatthew G Knepley PetscValidPointer(cells,3); 3299735aa83eSMatthew G Knepley if (dm->ops->locatepoints) { 3300735aa83eSMatthew G Knepley ierr = (*dm->ops->locatepoints)(dm,v,cells);CHKERRQ(ierr); 3301f23aa3ddSBarry Smith } else SETERRQ(((PetscObject) dm)->comm, PETSC_ERR_SUP, "Point location not available for this DM"); 3302e87bb0d3SMatthew G Knepley PetscFunctionReturn(0); 3303e87bb0d3SMatthew G Knepley } 3304