Actual source code: dmgenerate.c

  1: #include <petsc/private/dmimpl.h>

  3: PETSC_EXTERN PetscErrorCode DMIsForest(DM, PetscBool *);

  5: DMGeneratorFunctionList DMGenerateList              = NULL;
  6: PetscBool               DMGenerateRegisterAllCalled = PETSC_FALSE;

  8: #if PetscDefined(HAVE_TRIANGLE)
  9: PETSC_EXTERN PetscErrorCode DMPlexGenerate_Triangle(DM, PetscBool, DM *);
 10: PETSC_EXTERN PetscErrorCode DMPlexRefine_Triangle(DM, PetscReal *, DM *);
 11: #endif
 12: #if PetscDefined(HAVE_TETGEN)
 13: PETSC_EXTERN PetscErrorCode DMPlexGenerate_Tetgen(DM, PetscBool, DM *);
 14: PETSC_EXTERN PetscErrorCode DMPlexRefine_Tetgen(DM, double *, DM *);
 15: #endif
 16: #if PetscDefined(HAVE_CTETGEN)
 17: PETSC_EXTERN PetscErrorCode DMPlexGenerate_CTetgen(DM, PetscBool, DM *);
 18: PETSC_EXTERN PetscErrorCode DMPlexRefine_CTetgen(DM, double *, DM *);
 19: #endif
 20: #if PetscDefined(HAVE_PRAGMATIC)
 21: PETSC_EXTERN PetscErrorCode DMAdaptMetric_Pragmatic_Plex(DM, Vec, DMLabel, DMLabel, DM *);
 22: #endif
 23: #if PetscDefined(HAVE_MMG)
 24: PETSC_EXTERN PetscErrorCode DMAdaptMetric_Mmg_Plex(DM, Vec, DMLabel, DMLabel, DM *);
 25: #endif
 26: #if PetscDefined(HAVE_PARMMG)
 27: PETSC_EXTERN PetscErrorCode DMAdaptMetric_ParMmg_Plex(DM, Vec, DMLabel, DMLabel, DM *);
 28: #endif
 29: PETSC_EXTERN PetscErrorCode DMPlexTransformAdaptLabel(DM, Vec, DMLabel, DMLabel, DM *);
 30: PETSC_EXTERN PetscErrorCode DMAdaptLabel_Forest(DM, Vec, DMLabel, DMLabel, DM *);

 32: /*@C
 33:   DMGenerateRegisterAll - Registers all of the mesh generation methods in the `DM` package.

 35:   Not Collective

 37:   Level: advanced

 39: .seealso: `DM`, `DMGenerateRegisterDestroy()`
 40: @*/
 41: PetscErrorCode DMGenerateRegisterAll(void)
 42: {
 43:   PetscFunctionBegin;
 44:   if (DMGenerateRegisterAllCalled) PetscFunctionReturn(PETSC_SUCCESS);
 45:   DMGenerateRegisterAllCalled = PETSC_TRUE;
 46: #if PetscDefined(HAVE_TRIANGLE)
 47:   PetscCall(DMGenerateRegister("triangle", DMPlexGenerate_Triangle, DMPlexRefine_Triangle, NULL, 1));
 48: #endif
 49: #if PetscDefined(HAVE_CTETGEN)
 50:   PetscCall(DMGenerateRegister("ctetgen", DMPlexGenerate_CTetgen, DMPlexRefine_CTetgen, NULL, 2));
 51: #endif
 52: #if PetscDefined(HAVE_TETGEN)
 53:   PetscCall(DMGenerateRegister("tetgen", DMPlexGenerate_Tetgen, DMPlexRefine_Tetgen, NULL, 2));
 54: #endif
 55: #if PetscDefined(HAVE_PRAGMATIC)
 56:   PetscCall(DMGenerateRegister("pragmatic", NULL, NULL, DMAdaptMetric_Pragmatic_Plex, -1));
 57: #endif
 58: #if PetscDefined(HAVE_MMG)
 59:   PetscCall(DMGenerateRegister("mmg", NULL, NULL, DMAdaptMetric_Mmg_Plex, -1));
 60: #endif
 61: #if PetscDefined(HAVE_PARMMG)
 62:   PetscCall(DMGenerateRegister("parmmg", NULL, NULL, DMAdaptMetric_ParMmg_Plex, -1));
 63: #endif
 64:   PetscCall(DMGenerateRegister("cellrefiner", NULL, NULL, DMPlexTransformAdaptLabel, -1));
 65:   PetscCall(DMGenerateRegister("forest", NULL, NULL, DMAdaptLabel_Forest, -1));
 66:   PetscFunctionReturn(PETSC_SUCCESS);
 67: }

 69: /*@C
 70:   DMGenerateRegister -  Adds a grid generator to `DM`

 72:   Not Collective, No Fortran Support

 74:   Input Parameters:
 75: + sname - name of a new user-defined grid generator
 76: . fnc   - generator function
 77: . rfnc  - refinement function
 78: . alfnc - adapt by label function
 79: - dim   - dimension of boundary of domain

 81:   Example Usage:
 82: .vb
 83:    DMGenerateRegister("my_generator", MyGeneratorCreate, MyGeneratorRefiner, MyGeneratorAdaptor, dim);
 84: .ve

 86:   Then, your generator can be chosen at runtime via the option
 87: .vb
 88:   -dm_generator my_generator
 89: .ve

 91:   Level: advanced

 93:   Note:
 94:   `DMGenerateRegister()` may be called multiple times to add several user-defined generators

 96: .seealso: `DM`, `DMGenerateRegisterAll()`, `DMPlexGenerate()`, `DMGenerateRegisterDestroy()`
 97: @*/
 98: PetscErrorCode DMGenerateRegister(const char sname[], PetscErrorCode (*fnc)(DM, PetscBool, DM *), PetscErrorCode (*rfnc)(DM, PetscReal *, DM *), PetscErrorCode (*alfnc)(DM, Vec, DMLabel, DMLabel, DM *), PetscInt dim)
 99: {
100:   DMGeneratorFunctionList entry;

102:   PetscFunctionBegin;
103:   PetscCall(PetscNew(&entry));
104:   PetscCall(PetscStrallocpy(sname, &entry->name));
105:   entry->generate = fnc;
106:   entry->refine   = rfnc;
107:   entry->adapt    = alfnc;
108:   entry->dim      = dim;
109:   entry->next     = NULL;
110:   if (!DMGenerateList) DMGenerateList = entry;
111:   else {
112:     DMGeneratorFunctionList fl = DMGenerateList;
113:     while (fl->next) fl = fl->next;
114:     fl->next = entry;
115:   }
116:   PetscFunctionReturn(PETSC_SUCCESS);
117: }

119: /*@
120:   DMGenerateRegisterDestroy - Frees the list of `DM` mesh generators that were registered by `DMGenerateRegister()` or `DMGenerateRegisterAll()`.

122:   Not Collective

124:   Level: advanced

126: .seealso: `DM`, `DMGenerateRegister()`, `DMGenerateRegisterAll()`
127: @*/
128: PetscErrorCode DMGenerateRegisterDestroy(void)
129: {
130:   DMGeneratorFunctionList next, fl;

132:   PetscFunctionBegin;
133:   next = fl = DMGenerateList;
134:   while (next) {
135:     next = fl ? fl->next : NULL;
136:     if (fl) PetscCall(PetscFree(fl->name));
137:     PetscCall(PetscFree(fl));
138:     fl = next;
139:   }
140:   DMGenerateList              = NULL;
141:   DMGenerateRegisterAllCalled = PETSC_FALSE;
142:   PetscFunctionReturn(PETSC_SUCCESS);
143: }

145: /*@
146:   DMAdaptLabel - Adapt a `DM` based on a `DMLabel` with values interpreted as coarsening and refining flags.  Specific implementations of `DM` maybe have
147:   specialized flags, but all implementations should accept flag values `DM_ADAPT_DETERMINE`, `DM_ADAPT_KEEP`, `DM_ADAPT_REFINE`, and,
148:   `DM_ADAPT_COARSEN`.

150:   Collective

152:   Input Parameters:
153: + dm    - the pre-adaptation `DM` object
154: - label - label with the flags

156:   Output Parameters:
157: . dmAdapt - the adapted `DM` object: may be `NULL` if an adapted `DM` could not be produced.

159:   Level: intermediate

161: .seealso: `DM`, `DMAdaptMetric()`, `DMCoarsen()`, `DMRefine()`
162: @*/
163: PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
164: {
165:   DMGeneratorFunctionList fl;
166:   char                    adaptname[PETSC_MAX_PATH_LEN];
167:   const char             *name;
168:   PetscInt                dim;
169:   PetscBool               flg, isForest, found = PETSC_FALSE;

171:   PetscFunctionBegin;
174:   PetscAssertPointer(dmAdapt, 3);
175:   *dmAdapt = NULL;
176:   PetscCall(DMGetDimension(dm, &dim));
177:   PetscCall(DMIsForest(dm, &isForest));
178:   name = isForest ? "forest" : "cellrefiner";
179:   PetscCall(PetscOptionsGetString(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_adaptor", adaptname, sizeof(adaptname), &flg));
180:   if (flg) name = adaptname;

182:   fl = DMGenerateList;
183:   while (fl) {
184:     PetscCall(PetscStrcmp(fl->name, name, &flg));
185:     if (flg) {
186:       PetscCall((*fl->adapt)(dm, NULL, label, NULL, dmAdapt));
187:       found = PETSC_TRUE;
188:     }
189:     fl = fl->next;
190:   }
191:   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Grid adaptor %s not registered; you may need to add --download-%s to your ./configure options", name, name);
192:   if (*dmAdapt) {
193:     PetscCtx ctx;

195:     (*dmAdapt)->prealloc_only = dm->prealloc_only; /* maybe this should go .... */
196:     PetscCall(PetscFree((*dmAdapt)->vectype));
197:     PetscCall(PetscStrallocpy(dm->vectype, (char **)&(*dmAdapt)->vectype));
198:     PetscCall(PetscFree((*dmAdapt)->mattype));
199:     PetscCall(PetscStrallocpy(dm->mattype, (char **)&(*dmAdapt)->mattype));
200:     PetscCall(DMGetApplicationContext(dm, &ctx));
201:     PetscCall(DMSetApplicationContext(*dmAdapt, ctx));
202:   }
203:   PetscFunctionReturn(PETSC_SUCCESS);
204: }

206: /*@
207:   DMAdaptMetric - Generates a mesh adapted to the specified metric field.

209:   Input Parameters:
210: + dm      - The DM object
211: . metric  - The metric to which the mesh is adapted, defined vertex-wise.
212: . bdLabel - Label for boundary tags, which will be preserved in the output mesh. `bdLabel` should be `NULL` if there is no such label, and should be different from "_boundary_".
213: - rgLabel - Label for cell tags, which will be preserved in the output mesh. `rgLabel` should be `NULL` if there is no such label, and should be different from "_regions_".

215:   Output Parameter:
216: . dmAdapt - Pointer to the `DM` object containing the adapted mesh

218:   Note:
219:   The label in the adapted mesh will be registered under the name of the input `DMLabel` object

221:   Level: advanced

223: .seealso: `DMAdaptLabel()`, `DMCoarsen()`, `DMRefine()`
224: @*/
225: PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DMLabel rgLabel, DM *dmAdapt)
226: {
227:   DMGeneratorFunctionList fl;
228:   char                    adaptname[PETSC_MAX_PATH_LEN];
229:   const char             *name;
230:   const char *const       adaptors[3] = {"pragmatic", "mmg", "parmmg"};
231:   PetscInt                dim;
232:   PetscBool               flg, found = PETSC_FALSE;

234:   PetscFunctionBegin;
239:   PetscAssertPointer(dmAdapt, 5);
240:   *dmAdapt = NULL;
241:   PetscCall(DMGetDimension(dm, &dim));
242:   PetscCall(PetscOptionsGetString(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_adaptor", adaptname, sizeof(adaptname), &flg));

244:   /* Default to Mmg in serial and ParMmg in parallel */
245:   if (flg) name = adaptname;
246:   else {
247:     MPI_Comm    comm;
248:     PetscMPIInt size;

250:     PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
251:     PetscCallMPI(MPI_Comm_size(comm, &size));
252:     if (size == 1) name = adaptors[1];
253:     else name = adaptors[2];
254:   }

256:   fl = DMGenerateList;
257:   while (fl) {
258:     PetscCall(PetscStrcmp(fl->name, name, &flg));
259:     if (flg) {
260:       PetscCall((*fl->adapt)(dm, metric, bdLabel, rgLabel, dmAdapt));
261:       found = PETSC_TRUE;
262:     }
263:     fl = fl->next;
264:   }
265:   PetscCheck(found, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Grid adaptor %s not registered; you may need to add --download-%s to your ./configure options", name, name);
266:   if (*dmAdapt) {
267:     (*dmAdapt)->prealloc_only = dm->prealloc_only; /* maybe this should go .... */
268:     PetscCall(PetscFree((*dmAdapt)->vectype));
269:     PetscCall(PetscStrallocpy(dm->vectype, (char **)&(*dmAdapt)->vectype));
270:     PetscCall(PetscFree((*dmAdapt)->mattype));
271:     PetscCall(PetscStrallocpy(dm->mattype, (char **)&(*dmAdapt)->mattype));
272:   }
273:   PetscFunctionReturn(PETSC_SUCCESS);
274: }