xref: /petsc/src/dm/interface/dmi.c (revision bdfec8b8a186e2cd4f335d45bbaf42e486bc9d79)
1af0996ceSBarry Smith #include <petsc/private/dmimpl.h>     /*I      "petscdm.h"     I*/
22764a2aaSMatthew G. Knepley #include <petscds.h>
311689aebSJed Brown 
411689aebSJed Brown #undef __FUNCT__
511689aebSJed Brown #define __FUNCT__ "DMCreateGlobalVector_Section_Private"
611689aebSJed Brown PetscErrorCode DMCreateGlobalVector_Section_Private(DM dm,Vec *vec)
711689aebSJed Brown {
811689aebSJed Brown   PetscSection   gSection;
9cc30b04dSMatthew G Knepley   PetscInt       localSize, bs, blockSize = -1, pStart, pEnd, p;
10fad22124SMatthew G Knepley   PetscErrorCode ierr;
115d1c0279SHong Zhang   PetscInt       in[2],out[2];
1211689aebSJed Brown 
1311689aebSJed Brown   PetscFunctionBegin;
1411689aebSJed Brown   ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
15fad22124SMatthew G Knepley   ierr = PetscSectionGetChart(gSection, &pStart, &pEnd);CHKERRQ(ierr);
1611689aebSJed Brown   for (p = pStart; p < pEnd; ++p) {
1711689aebSJed Brown     PetscInt dof, cdof;
1811689aebSJed Brown 
19fad22124SMatthew G Knepley     ierr = PetscSectionGetDof(gSection, p, &dof);CHKERRQ(ierr);
20fad22124SMatthew G Knepley     ierr = PetscSectionGetConstraintDof(gSection, p, &cdof);CHKERRQ(ierr);
210680ce0aSHong Zhang 
220680ce0aSHong Zhang     if (dof > 0) {
230680ce0aSHong Zhang       if (blockSize < 0 && dof-cdof > 0) {
240680ce0aSHong Zhang         /* set blockSize */
250680ce0aSHong Zhang         blockSize = dof-cdof;
260680ce0aSHong Zhang       } else if (dof-cdof != blockSize) {
270680ce0aSHong Zhang         /* non-identical blockSize, set it as 1 */
2811689aebSJed Brown         blockSize = 1;
2911689aebSJed Brown         break;
3011689aebSJed Brown       }
3111689aebSJed Brown     }
320680ce0aSHong Zhang   }
335d1c0279SHong Zhang 
345d1c0279SHong Zhang   in[0] = -blockSize;
355d1c0279SHong Zhang   in[1] = blockSize;
36*bdfec8b8SHong Zhang   ierr = MPIU_Allreduce(in,out,2,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
375d1c0279SHong Zhang   /* -out[0] = min(blockSize), out[1] = max(blockSize) */
385d1c0279SHong Zhang   if (-out[0] == out[1]) {
395d1c0279SHong Zhang     bs = out[1];
405d1c0279SHong Zhang   } else bs = 1;
415d1c0279SHong Zhang 
425d1c0279SHong Zhang   if (bs < 0) { /* Everyone was empty */
435d1c0279SHong Zhang     blockSize = 1;
445d1c0279SHong Zhang     bs        = 1;
455d1c0279SHong Zhang   }
465d1c0279SHong Zhang 
47fad22124SMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(gSection, &localSize);CHKERRQ(ierr);
4882f516ccSBarry Smith   if (localSize%blockSize) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Mismatch between blocksize %d and local storage size %d", blockSize, localSize);
4982f516ccSBarry Smith   ierr = VecCreate(PetscObjectComm((PetscObject)dm), vec);CHKERRQ(ierr);
5011689aebSJed Brown   ierr = VecSetSizes(*vec, localSize, PETSC_DETERMINE);CHKERRQ(ierr);
51cc30b04dSMatthew G Knepley   ierr = VecSetBlockSize(*vec, bs);CHKERRQ(ierr);
52c0dedaeaSBarry Smith   ierr = VecSetType(*vec,dm->vectype);CHKERRQ(ierr);
5311689aebSJed Brown   ierr = VecSetDM(*vec, dm);CHKERRQ(ierr);
5411689aebSJed Brown   /* ierr = VecSetLocalToGlobalMapping(*vec, dm->ltogmap);CHKERRQ(ierr); */
5511689aebSJed Brown   PetscFunctionReturn(0);
5611689aebSJed Brown }
5711689aebSJed Brown 
5811689aebSJed Brown #undef __FUNCT__
5911689aebSJed Brown #define __FUNCT__ "DMCreateLocalVector_Section_Private"
6011689aebSJed Brown PetscErrorCode DMCreateLocalVector_Section_Private(DM dm,Vec *vec)
6111689aebSJed Brown {
62fad22124SMatthew G Knepley   PetscSection   section;
6311689aebSJed Brown   PetscInt       localSize, blockSize = -1, pStart, pEnd, p;
64fad22124SMatthew G Knepley   PetscErrorCode ierr;
6511689aebSJed Brown 
6611689aebSJed Brown   PetscFunctionBegin;
67fad22124SMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
68fad22124SMatthew G Knepley   ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
6911689aebSJed Brown   for (p = pStart; p < pEnd; ++p) {
7011689aebSJed Brown     PetscInt dof;
7111689aebSJed Brown 
72fad22124SMatthew G Knepley     ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
7311689aebSJed Brown     if ((blockSize < 0) && (dof > 0)) blockSize = dof;
7411689aebSJed Brown     if ((dof > 0) && (dof != blockSize)) {
7511689aebSJed Brown       blockSize = 1;
7611689aebSJed Brown       break;
7711689aebSJed Brown     }
7811689aebSJed Brown   }
79fad22124SMatthew G Knepley   ierr = PetscSectionGetStorageSize(section, &localSize);CHKERRQ(ierr);
8011689aebSJed Brown   ierr = VecCreate(PETSC_COMM_SELF, vec);CHKERRQ(ierr);
8111689aebSJed Brown   ierr = VecSetSizes(*vec, localSize, localSize);CHKERRQ(ierr);
8211689aebSJed Brown   ierr = VecSetBlockSize(*vec, blockSize);CHKERRQ(ierr);
83c0dedaeaSBarry Smith   ierr = VecSetType(*vec,dm->vectype);CHKERRQ(ierr);
8411689aebSJed Brown   ierr = VecSetDM(*vec, dm);CHKERRQ(ierr);
8511689aebSJed Brown   PetscFunctionReturn(0);
8611689aebSJed Brown }
874d9407bcSMatthew G. Knepley 
884d9407bcSMatthew G. Knepley #undef __FUNCT__
894d9407bcSMatthew G. Knepley #define __FUNCT__ "DMCreateSubDM_Section_Private"
904d9407bcSMatthew G. Knepley /* This assumes that the DM has been cloned prior to the call */
914d9407bcSMatthew G. Knepley PetscErrorCode DMCreateSubDM_Section_Private(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
924d9407bcSMatthew G. Knepley {
934d9407bcSMatthew G. Knepley   PetscSection   section, sectionGlobal;
944d9407bcSMatthew G. Knepley   PetscInt      *subIndices;
954d9407bcSMatthew G. Knepley   PetscInt       subSize = 0, subOff = 0, nF, f, pStart, pEnd, p;
964d9407bcSMatthew G. Knepley   PetscErrorCode ierr;
974d9407bcSMatthew G. Knepley 
984d9407bcSMatthew G. Knepley   PetscFunctionBegin;
994d9407bcSMatthew G. Knepley   if (!numFields) PetscFunctionReturn(0);
1004d9407bcSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1014d9407bcSMatthew G. Knepley   ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
1024d9407bcSMatthew G. Knepley   if (!section) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Must set default section for DM before splitting fields");
1034d9407bcSMatthew G. Knepley   if (!sectionGlobal) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Must set default global section for DM before splitting fields");
1044d9407bcSMatthew G. Knepley   ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
1054d9407bcSMatthew G. Knepley   if (numFields > nF) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Number of requested fields %d greater than number of DM fields %d", numFields, nF);
1064d9407bcSMatthew G. Knepley   if (is) {
107b9eca265Ssarens     PetscInt bs = -1, bsLocal, bsMax, bsMin;
1084d9407bcSMatthew G. Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
1094d9407bcSMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
110b9eca265Ssarens       PetscInt gdof, pSubSize  = 0;
1114d9407bcSMatthew G. Knepley 
1124d9407bcSMatthew G. Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
1134d9407bcSMatthew G. Knepley       if (gdof > 0) {
1144d9407bcSMatthew G. Knepley         for (f = 0; f < numFields; ++f) {
1154d9407bcSMatthew G. Knepley           PetscInt fdof, fcdof;
1164d9407bcSMatthew G. Knepley 
1174d9407bcSMatthew G. Knepley           ierr     = PetscSectionGetFieldDof(section, p, fields[f], &fdof);CHKERRQ(ierr);
1184d9407bcSMatthew G. Knepley           ierr     = PetscSectionGetFieldConstraintDof(section, p, fields[f], &fcdof);CHKERRQ(ierr);
119b9eca265Ssarens           pSubSize += fdof-fcdof;
120b9eca265Ssarens         }
121b9eca265Ssarens         subSize += pSubSize;
122b9eca265Ssarens         if (pSubSize) {
123b9eca265Ssarens           if (bs < 0) {
124b9eca265Ssarens             bs = pSubSize;
125b9eca265Ssarens           } else if (bs != pSubSize) {
126b9eca265Ssarens             /* Layout does not admit a pointwise block size */
127b9eca265Ssarens             bs = 1;
1284d9407bcSMatthew G. Knepley           }
1294d9407bcSMatthew G. Knepley         }
1304d9407bcSMatthew G. Knepley       }
131b9eca265Ssarens     }
132b9eca265Ssarens     /* Must have same blocksize on all procs (some might have no points) */
133b9eca265Ssarens     bsLocal = bs;
134b9eca265Ssarens     ierr = MPIU_Allreduce(&bsLocal, &bsMax, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
135b9eca265Ssarens     bsLocal = bs < 0 ? bsMax : bs;
136b9eca265Ssarens     ierr = MPIU_Allreduce(&bsLocal, &bsMin, 1, MPIU_INT, MPI_MIN, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
137b9eca265Ssarens     if (bsMin != bsMax) {
138b9eca265Ssarens       bs = 1;
139b9eca265Ssarens     } else {
140b9eca265Ssarens       bs = bsMax;
141b9eca265Ssarens     }
142785e854fSJed Brown     ierr = PetscMalloc1(subSize, &subIndices);CHKERRQ(ierr);
1434d9407bcSMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
1444d9407bcSMatthew G. Knepley       PetscInt gdof, goff;
1454d9407bcSMatthew G. Knepley 
1464d9407bcSMatthew G. Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
1474d9407bcSMatthew G. Knepley       if (gdof > 0) {
1484d9407bcSMatthew G. Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
1494d9407bcSMatthew G. Knepley         for (f = 0; f < numFields; ++f) {
1504d9407bcSMatthew G. Knepley           PetscInt fdof, fcdof, fc, f2, poff = 0;
1514d9407bcSMatthew G. Knepley 
1524d9407bcSMatthew G. Knepley           /* Can get rid of this loop by storing field information in the global section */
1534d9407bcSMatthew G. Knepley           for (f2 = 0; f2 < fields[f]; ++f2) {
1544d9407bcSMatthew G. Knepley             ierr  = PetscSectionGetFieldDof(section, p, f2, &fdof);CHKERRQ(ierr);
1554d9407bcSMatthew G. Knepley             ierr  = PetscSectionGetFieldConstraintDof(section, p, f2, &fcdof);CHKERRQ(ierr);
1564d9407bcSMatthew G. Knepley             poff += fdof-fcdof;
1574d9407bcSMatthew G. Knepley           }
1584d9407bcSMatthew G. Knepley           ierr = PetscSectionGetFieldDof(section, p, fields[f], &fdof);CHKERRQ(ierr);
1594d9407bcSMatthew G. Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, fields[f], &fcdof);CHKERRQ(ierr);
1604d9407bcSMatthew G. Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++subOff) {
1614d9407bcSMatthew G. Knepley             subIndices[subOff] = goff+poff+fc;
1624d9407bcSMatthew G. Knepley           }
1634d9407bcSMatthew G. Knepley         }
1644d9407bcSMatthew G. Knepley       }
1654d9407bcSMatthew G. Knepley     }
1664d9407bcSMatthew G. Knepley     ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), subSize, subIndices, PETSC_OWN_POINTER, is);CHKERRQ(ierr);
167b9eca265Ssarens     ierr = ISSetBlockSize(*is, bs);CHKERRQ(ierr);
1684d9407bcSMatthew G. Knepley   }
1694d9407bcSMatthew G. Knepley   if (subdm) {
1704d9407bcSMatthew G. Knepley     PetscSection subsection;
1714d9407bcSMatthew G. Knepley     PetscBool    haveNull = PETSC_FALSE;
1724d9407bcSMatthew G. Knepley     PetscInt     f, nf = 0;
1734d9407bcSMatthew G. Knepley 
1744d9407bcSMatthew G. Knepley     ierr = PetscSectionCreateSubsection(section, numFields, fields, &subsection);CHKERRQ(ierr);
1754d9407bcSMatthew G. Knepley     ierr = DMSetDefaultSection(*subdm, subsection);CHKERRQ(ierr);
1764d9407bcSMatthew G. Knepley     ierr = PetscSectionDestroy(&subsection);CHKERRQ(ierr);
1774d9407bcSMatthew G. Knepley     for (f = 0; f < numFields; ++f) {
1784d9407bcSMatthew G. Knepley       (*subdm)->nullspaceConstructors[f] = dm->nullspaceConstructors[fields[f]];
1794d9407bcSMatthew G. Knepley       if ((*subdm)->nullspaceConstructors[f]) {
1804d9407bcSMatthew G. Knepley         haveNull = PETSC_TRUE;
1814d9407bcSMatthew G. Knepley         nf       = f;
1824d9407bcSMatthew G. Knepley       }
1834d9407bcSMatthew G. Knepley     }
184f646a522SMatthew G. Knepley     if (haveNull && is) {
1854d9407bcSMatthew G. Knepley       MatNullSpace nullSpace;
1864d9407bcSMatthew G. Knepley 
1874d9407bcSMatthew G. Knepley       ierr = (*(*subdm)->nullspaceConstructors[nf])(*subdm, nf, &nullSpace);CHKERRQ(ierr);
1884d9407bcSMatthew G. Knepley       ierr = PetscObjectCompose((PetscObject) *is, "nullspace", (PetscObject) nullSpace);CHKERRQ(ierr);
1894d9407bcSMatthew G. Knepley       ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1904d9407bcSMatthew G. Knepley     }
1910f21e855SMatthew G. Knepley     if (dm->prob) {
1920f21e855SMatthew G. Knepley       PetscInt Nf;
1930f21e855SMatthew G. Knepley 
1942764a2aaSMatthew G. Knepley       ierr = PetscDSGetNumFields(dm->prob, &Nf);CHKERRQ(ierr);
1950f21e855SMatthew G. Knepley       if (nF != Nf) SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "The number of DM fields %d does not match the number of Section fields %d", Nf, nF);
1964d9407bcSMatthew G. Knepley       ierr = DMSetNumFields(*subdm, numFields);CHKERRQ(ierr);
1974d9407bcSMatthew G. Knepley       for (f = 0; f < numFields; ++f) {
1980f21e855SMatthew G. Knepley         PetscObject disc;
1990f21e855SMatthew G. Knepley 
2000f21e855SMatthew G. Knepley         ierr = DMGetField(dm, fields[f], &disc);CHKERRQ(ierr);
2010f21e855SMatthew G. Knepley         ierr = DMSetField(*subdm, f, disc);CHKERRQ(ierr);
2024d9407bcSMatthew G. Knepley       }
203f646a522SMatthew G. Knepley       if (numFields == 1 && is) {
2040f21e855SMatthew G. Knepley         PetscObject disc, space, pmat;
2054d9407bcSMatthew G. Knepley 
2060f21e855SMatthew G. Knepley         ierr = DMGetField(*subdm, 0, &disc);CHKERRQ(ierr);
2070f21e855SMatthew G. Knepley         ierr = PetscObjectQuery(disc, "nullspace", &space);CHKERRQ(ierr);
2080f21e855SMatthew G. Knepley         if (space) {ierr = PetscObjectCompose((PetscObject) *is, "nullspace", space);CHKERRQ(ierr);}
2090f21e855SMatthew G. Knepley         ierr = PetscObjectQuery(disc, "nearnullspace", &space);CHKERRQ(ierr);
2100f21e855SMatthew G. Knepley         if (space) {ierr = PetscObjectCompose((PetscObject) *is, "nearnullspace", space);CHKERRQ(ierr);}
2110f21e855SMatthew G. Knepley         ierr = PetscObjectQuery(disc, "pmat", &pmat);CHKERRQ(ierr);
2120f21e855SMatthew G. Knepley         if (pmat) {ierr = PetscObjectCompose((PetscObject) *is, "pmat", pmat);CHKERRQ(ierr);}
2134d9407bcSMatthew G. Knepley       }
2144d9407bcSMatthew G. Knepley     }
2154d9407bcSMatthew G. Knepley   }
2168333ec13SMatthew G. Knepley #if 0
2178333ec13SMatthew G. Knepley   /* We need a way to filter the original SF for given fields:
2188333ec13SMatthew G. Knepley        - Keeping the original section around it too much I think
2198333ec13SMatthew G. Knepley        - We could keep the distributed section, and subset it
2208333ec13SMatthew G. Knepley    */
2218333ec13SMatthew G. Knepley   if (dm->sfNatural) {
2228333ec13SMatthew G. Knepley     PetscSF sfNatural;
2238333ec13SMatthew G. Knepley 
2248333ec13SMatthew G. Knepley     ierr = PetscSectionCreateSubsection(dm->originalSection, numFields, fields, &(*subdm)->originalSection);CHKERRQ(ierr);
2258333ec13SMatthew G. Knepley     ierr = DMPlexCreateGlobalToNaturalPetscSF(*subdm, &sfNatural);CHKERRQ(ierr);
2268333ec13SMatthew G. Knepley     ierr = DMPlexSetGlobalToNaturalPetscSF(*subdm, sfNatural);CHKERRQ(ierr);
2278333ec13SMatthew G. Knepley   }
2288333ec13SMatthew G. Knepley #endif
2294d9407bcSMatthew G. Knepley   PetscFunctionReturn(0);
2304d9407bcSMatthew G. Knepley }
231