xref: /petsc/src/snes/utils/dm/dmadapt.c (revision 49abdd8a111d9c2ef7fc48ade253ef64e07f9b37)
1c6f61ee2SBarry Smith #include <petscdmadaptor.h> /*I "petscdmadaptor.h" I*/
2c6f61ee2SBarry Smith #include <petscdmplex.h>
3c6f61ee2SBarry Smith #include <petscdmforest.h>
4c6f61ee2SBarry Smith #include <petscds.h>
5c6f61ee2SBarry Smith #include <petscblaslapack.h>
63a336bb1SMatthew G. Knepley #include <petscsnes.h>
78b724c91SMatthew G. Knepley #include <petscdraw.h>
8c6f61ee2SBarry Smith 
9c6f61ee2SBarry Smith #include <petsc/private/dmadaptorimpl.h>
10c6f61ee2SBarry Smith #include <petsc/private/dmpleximpl.h>
11c6f61ee2SBarry Smith #include <petsc/private/petscfeimpl.h>
12c6f61ee2SBarry Smith 
133a336bb1SMatthew G. Knepley PetscClassId DMADAPTOR_CLASSID;
14c6f61ee2SBarry Smith 
153a336bb1SMatthew G. Knepley PetscFunctionList DMAdaptorList              = NULL;
163a336bb1SMatthew G. Knepley PetscBool         DMAdaptorRegisterAllCalled = PETSC_FALSE;
173a336bb1SMatthew G. Knepley 
188b724c91SMatthew G. Knepley PetscFunctionList DMAdaptorMonitorList              = NULL;
198b724c91SMatthew G. Knepley PetscFunctionList DMAdaptorMonitorCreateList        = NULL;
208b724c91SMatthew G. Knepley PetscFunctionList DMAdaptorMonitorDestroyList       = NULL;
218b724c91SMatthew G. Knepley PetscBool         DMAdaptorMonitorRegisterAllCalled = PETSC_FALSE;
228b724c91SMatthew G. Knepley 
238b724c91SMatthew G. Knepley const char *const DMAdaptationCriteria[] = {"NONE", "REFINE", "LABEL", "METRIC", "DMAdaptationCriterion", "DM_ADAPTATION_", NULL};
248b724c91SMatthew G. Knepley 
253a336bb1SMatthew G. Knepley /*@C
263a336bb1SMatthew G. Knepley   DMAdaptorRegister - Adds a new adaptor component implementation
273a336bb1SMatthew G. Knepley 
283a336bb1SMatthew G. Knepley   Not Collective
293a336bb1SMatthew G. Knepley 
303a336bb1SMatthew G. Knepley   Input Parameters:
313a336bb1SMatthew G. Knepley + name        - The name of a new user-defined creation routine
323a336bb1SMatthew G. Knepley - create_func - The creation routine
333a336bb1SMatthew G. Knepley 
343a336bb1SMatthew G. Knepley   Example Usage:
353a336bb1SMatthew G. Knepley .vb
363a336bb1SMatthew G. Knepley   DMAdaptorRegister("my_adaptor", MyAdaptorCreate);
373a336bb1SMatthew G. Knepley .ve
383a336bb1SMatthew G. Knepley 
393a336bb1SMatthew G. Knepley   Then, your adaptor type can be chosen with the procedural interface via
403a336bb1SMatthew G. Knepley .vb
413a336bb1SMatthew G. Knepley   DMAdaptorCreate(MPI_Comm, DMAdaptor *);
423a336bb1SMatthew G. Knepley   DMAdaptorSetType(DMAdaptor, "my_adaptor");
433a336bb1SMatthew G. Knepley .ve
443a336bb1SMatthew G. Knepley   or at runtime via the option
453a336bb1SMatthew G. Knepley .vb
463a336bb1SMatthew G. Knepley   -adaptor_type my_adaptor
473a336bb1SMatthew G. Knepley .ve
483a336bb1SMatthew G. Knepley 
493a336bb1SMatthew G. Knepley   Level: advanced
503a336bb1SMatthew G. Knepley 
513a336bb1SMatthew G. Knepley   Note:
523a336bb1SMatthew G. Knepley   `DMAdaptorRegister()` may be called multiple times to add several user-defined adaptors
533a336bb1SMatthew G. Knepley 
543a336bb1SMatthew G. Knepley .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMAdaptor`, `DMAdaptorRegisterAll()`, `DMAdaptorRegisterDestroy()`
553a336bb1SMatthew G. Knepley @*/
563a336bb1SMatthew G. Knepley PetscErrorCode DMAdaptorRegister(const char name[], PetscErrorCode (*create_func)(DMAdaptor))
57c6f61ee2SBarry Smith {
583a336bb1SMatthew G. Knepley   PetscFunctionBegin;
593a336bb1SMatthew G. Knepley   PetscCall(DMInitializePackage());
603a336bb1SMatthew G. Knepley   PetscCall(PetscFunctionListAdd(&DMAdaptorList, name, create_func));
613a336bb1SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
623a336bb1SMatthew G. Knepley }
633a336bb1SMatthew G. Knepley 
643a336bb1SMatthew G. Knepley PETSC_EXTERN PetscErrorCode DMAdaptorCreate_Gradient(DMAdaptor);
653a336bb1SMatthew G. Knepley PETSC_EXTERN PetscErrorCode DMAdaptorCreate_Flux(DMAdaptor);
663a336bb1SMatthew G. Knepley 
673a336bb1SMatthew G. Knepley /*@C
683a336bb1SMatthew G. Knepley   DMAdaptorRegisterAll - Registers all of the adaptor components in the `DM` package.
693a336bb1SMatthew G. Knepley 
703a336bb1SMatthew G. Knepley   Not Collective
713a336bb1SMatthew G. Knepley 
723a336bb1SMatthew G. Knepley   Level: advanced
733a336bb1SMatthew G. Knepley 
743a336bb1SMatthew G. Knepley .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMAdaptorType`, `DMRegisterAll()`, `DMAdaptorRegisterDestroy()`
753a336bb1SMatthew G. Knepley @*/
763a336bb1SMatthew G. Knepley PetscErrorCode DMAdaptorRegisterAll(void)
773a336bb1SMatthew G. Knepley {
783a336bb1SMatthew G. Knepley   PetscFunctionBegin;
793a336bb1SMatthew G. Knepley   if (DMAdaptorRegisterAllCalled) PetscFunctionReturn(PETSC_SUCCESS);
803a336bb1SMatthew G. Knepley   DMAdaptorRegisterAllCalled = PETSC_TRUE;
813a336bb1SMatthew G. Knepley 
823a336bb1SMatthew G. Knepley   PetscCall(DMAdaptorRegister(DMADAPTORGRADIENT, DMAdaptorCreate_Gradient));
833a336bb1SMatthew G. Knepley   PetscCall(DMAdaptorRegister(DMADAPTORFLUX, DMAdaptorCreate_Flux));
843a336bb1SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
853a336bb1SMatthew G. Knepley }
863a336bb1SMatthew G. Knepley 
873a336bb1SMatthew G. Knepley /*@C
883a336bb1SMatthew G. Knepley   DMAdaptorRegisterDestroy - This function destroys the registered `DMAdaptorType`. It is called from `PetscFinalize()`.
893a336bb1SMatthew G. Knepley 
903a336bb1SMatthew G. Knepley   Not collective
913a336bb1SMatthew G. Knepley 
923a336bb1SMatthew G. Knepley   Level: developer
933a336bb1SMatthew G. Knepley 
948b724c91SMatthew G. Knepley .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMAdaptorRegisterAll()`, `DMAdaptorType`, `PetscFinalize()`
953a336bb1SMatthew G. Knepley @*/
963a336bb1SMatthew G. Knepley PetscErrorCode DMAdaptorRegisterDestroy(void)
973a336bb1SMatthew G. Knepley {
983a336bb1SMatthew G. Knepley   PetscFunctionBegin;
993a336bb1SMatthew G. Knepley   PetscCall(PetscFunctionListDestroy(&DMAdaptorList));
1003a336bb1SMatthew G. Knepley   DMAdaptorRegisterAllCalled = PETSC_FALSE;
101c6f61ee2SBarry Smith   PetscFunctionReturn(PETSC_SUCCESS);
102c6f61ee2SBarry Smith }
103c6f61ee2SBarry Smith 
1048b724c91SMatthew G. Knepley static PetscErrorCode DMAdaptorMonitorMakeKey_Internal(const char name[], PetscViewerType vtype, PetscViewerFormat format, char key[])
1058b724c91SMatthew G. Knepley {
1068b724c91SMatthew G. Knepley   PetscFunctionBegin;
1078b724c91SMatthew G. Knepley   PetscCall(PetscStrncpy(key, name, PETSC_MAX_PATH_LEN));
1088b724c91SMatthew G. Knepley   PetscCall(PetscStrlcat(key, ":", PETSC_MAX_PATH_LEN));
1098b724c91SMatthew G. Knepley   PetscCall(PetscStrlcat(key, vtype, PETSC_MAX_PATH_LEN));
1108b724c91SMatthew G. Knepley   PetscCall(PetscStrlcat(key, ":", PETSC_MAX_PATH_LEN));
1118b724c91SMatthew G. Knepley   PetscCall(PetscStrlcat(key, PetscViewerFormats[format], PETSC_MAX_PATH_LEN));
1128b724c91SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
1138b724c91SMatthew G. Knepley }
1148b724c91SMatthew G. Knepley 
1158b724c91SMatthew G. Knepley /*@C
1168b724c91SMatthew G. Knepley   DMAdaptorMonitorRegister -  Registers a mesh adaptation monitor routine that may be accessed with `DMAdaptorMonitorSetFromOptions()`
1178b724c91SMatthew G. Knepley 
1188b724c91SMatthew G. Knepley   Not Collective
1198b724c91SMatthew G. Knepley 
1208b724c91SMatthew G. Knepley   Input Parameters:
1218b724c91SMatthew G. Knepley + name    - name of a new monitor routine
1228b724c91SMatthew G. Knepley . vtype   - A `PetscViewerType` for the output
1238b724c91SMatthew G. Knepley . format  - A `PetscViewerFormat` for the output
1248b724c91SMatthew G. Knepley . monitor - Monitor routine
1258b724c91SMatthew G. Knepley . create  - Creation routine, or `NULL`
1268b724c91SMatthew G. Knepley - destroy - Destruction routine, or `NULL`
1278b724c91SMatthew G. Knepley 
1288b724c91SMatthew G. Knepley   Level: advanced
1298b724c91SMatthew G. Knepley 
1308b724c91SMatthew G. Knepley   Note:
1318b724c91SMatthew G. Knepley   `DMAdaptorMonitorRegister()` may be called multiple times to add several user-defined monitors.
1328b724c91SMatthew G. Knepley 
1338b724c91SMatthew G. Knepley   Example Usage:
1348b724c91SMatthew G. Knepley .vb
1358b724c91SMatthew G. Knepley   DMAdaptorMonitorRegister("my_monitor", PETSCVIEWERASCII, PETSC_VIEWER_ASCII_INFO_DETAIL, MyMonitor, NULL, NULL);
1368b724c91SMatthew G. Knepley .ve
1378b724c91SMatthew G. Knepley 
1388b724c91SMatthew G. Knepley   Then, your monitor can be chosen with the procedural interface via
1398b724c91SMatthew G. Knepley .vb
1408b724c91SMatthew G. Knepley   DMAdaptorMonitorSetFromOptions(ksp, "-adaptor_monitor_my_monitor", "my_monitor", NULL)
1418b724c91SMatthew G. Knepley .ve
1428b724c91SMatthew G. Knepley   or at runtime via the option `-adaptor_monitor_my_monitor`
1438b724c91SMatthew G. Knepley 
1448b724c91SMatthew G. Knepley .seealso: [](ch_snes), `DMAdaptor`, `DMAdaptorMonitorSet()`, `DMAdaptorMonitorRegisterAll()`, `DMAdaptorMonitorSetFromOptions()`
1458b724c91SMatthew G. Knepley @*/
1468b724c91SMatthew G. Knepley PetscErrorCode DMAdaptorMonitorRegister(const char name[], PetscViewerType vtype, PetscViewerFormat format, PetscErrorCode (*monitor)(DMAdaptor, PetscInt, DM, DM, PetscInt, PetscReal[], Vec, PetscViewerAndFormat *), PetscErrorCode (*create)(PetscViewer, PetscViewerFormat, void *, PetscViewerAndFormat **), PetscErrorCode (*destroy)(PetscViewerAndFormat **))
1478b724c91SMatthew G. Knepley {
1488b724c91SMatthew G. Knepley   char key[PETSC_MAX_PATH_LEN];
1498b724c91SMatthew G. Knepley 
1508b724c91SMatthew G. Knepley   PetscFunctionBegin;
1518b724c91SMatthew G. Knepley   PetscCall(SNESInitializePackage());
1528b724c91SMatthew G. Knepley   PetscCall(DMAdaptorMonitorMakeKey_Internal(name, vtype, format, key));
1538b724c91SMatthew G. Knepley   PetscCall(PetscFunctionListAdd(&DMAdaptorMonitorList, key, monitor));
1548b724c91SMatthew G. Knepley   if (create) PetscCall(PetscFunctionListAdd(&DMAdaptorMonitorCreateList, key, create));
1558b724c91SMatthew G. Knepley   if (destroy) PetscCall(PetscFunctionListAdd(&DMAdaptorMonitorDestroyList, key, destroy));
1568b724c91SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
1578b724c91SMatthew G. Knepley }
1588b724c91SMatthew G. Knepley 
1598b724c91SMatthew G. Knepley /*@C
1608b724c91SMatthew G. Knepley   DMAdaptorMonitorRegisterDestroy - This function destroys the registered monitors for `DMAdaptor`. It is called from `PetscFinalize()`.
1618b724c91SMatthew G. Knepley 
1628b724c91SMatthew G. Knepley   Not collective
1638b724c91SMatthew G. Knepley 
1648b724c91SMatthew G. Knepley   Level: developer
1658b724c91SMatthew G. Knepley 
1668b724c91SMatthew G. Knepley .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMAdaptorMonitorRegisterAll()`, `DMAdaptor`, `PetscFinalize()`
1678b724c91SMatthew G. Knepley @*/
1688b724c91SMatthew G. Knepley PetscErrorCode DMAdaptorMonitorRegisterDestroy(void)
1698b724c91SMatthew G. Knepley {
1708b724c91SMatthew G. Knepley   PetscFunctionBegin;
1718b724c91SMatthew G. Knepley   PetscCall(PetscFunctionListDestroy(&DMAdaptorMonitorList));
1728b724c91SMatthew G. Knepley   PetscCall(PetscFunctionListDestroy(&DMAdaptorMonitorCreateList));
1738b724c91SMatthew G. Knepley   PetscCall(PetscFunctionListDestroy(&DMAdaptorMonitorDestroyList));
1748b724c91SMatthew G. Knepley   DMAdaptorMonitorRegisterAllCalled = PETSC_FALSE;
1758b724c91SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
1768b724c91SMatthew G. Knepley }
1778b724c91SMatthew G. Knepley 
178c6f61ee2SBarry Smith /*@
179c6f61ee2SBarry Smith   DMAdaptorCreate - Create a `DMAdaptor` object. Its purpose is to construct a adaptation `DMLabel` or metric `Vec` that can be used to modify the `DM`.
180c6f61ee2SBarry Smith 
181c6f61ee2SBarry Smith   Collective
182c6f61ee2SBarry Smith 
183c6f61ee2SBarry Smith   Input Parameter:
184c6f61ee2SBarry Smith . comm - The communicator for the `DMAdaptor` object
185c6f61ee2SBarry Smith 
186c6f61ee2SBarry Smith   Output Parameter:
187c6f61ee2SBarry Smith . adaptor - The `DMAdaptor` object
188c6f61ee2SBarry Smith 
189c6f61ee2SBarry Smith   Level: beginner
190c6f61ee2SBarry Smith 
191df514481SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAdaptor`, `DMAdaptorDestroy()`, `DMAdaptorAdapt()`, `PetscConvEst`, `PetscConvEstCreate()`
192c6f61ee2SBarry Smith @*/
193c6f61ee2SBarry Smith PetscErrorCode DMAdaptorCreate(MPI_Comm comm, DMAdaptor *adaptor)
194c6f61ee2SBarry Smith {
195c6f61ee2SBarry Smith   VecTaggerBox refineBox, coarsenBox;
196c6f61ee2SBarry Smith 
197c6f61ee2SBarry Smith   PetscFunctionBegin;
198c6f61ee2SBarry Smith   PetscAssertPointer(adaptor, 2);
199c6f61ee2SBarry Smith   PetscCall(PetscSysInitializePackage());
200c6f61ee2SBarry Smith 
2013a336bb1SMatthew G. Knepley   PetscCall(PetscHeaderCreate(*adaptor, DMADAPTOR_CLASSID, "DMAdaptor", "DM Adaptor", "DMAdaptor", comm, DMAdaptorDestroy, DMAdaptorView));
202c6f61ee2SBarry Smith   (*adaptor)->adaptCriterion   = DM_ADAPTATION_NONE;
203c6f61ee2SBarry Smith   (*adaptor)->numSeq           = 1;
204c6f61ee2SBarry Smith   (*adaptor)->Nadapt           = -1;
205c6f61ee2SBarry Smith   (*adaptor)->refinementFactor = 2.0;
206c6f61ee2SBarry Smith   refineBox.min = refineBox.max = PETSC_MAX_REAL;
207c6f61ee2SBarry Smith   PetscCall(VecTaggerCreate(PetscObjectComm((PetscObject)*adaptor), &(*adaptor)->refineTag));
208c6f61ee2SBarry Smith   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)(*adaptor)->refineTag, "refine_"));
209c6f61ee2SBarry Smith   PetscCall(VecTaggerSetType((*adaptor)->refineTag, VECTAGGERABSOLUTE));
210c6f61ee2SBarry Smith   PetscCall(VecTaggerAbsoluteSetBox((*adaptor)->refineTag, &refineBox));
211c6f61ee2SBarry Smith   coarsenBox.min = coarsenBox.max = PETSC_MAX_REAL;
212c6f61ee2SBarry Smith   PetscCall(VecTaggerCreate(PetscObjectComm((PetscObject)*adaptor), &(*adaptor)->coarsenTag));
213c6f61ee2SBarry Smith   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)(*adaptor)->coarsenTag, "coarsen_"));
214c6f61ee2SBarry Smith   PetscCall(VecTaggerSetType((*adaptor)->coarsenTag, VECTAGGERABSOLUTE));
215c6f61ee2SBarry Smith   PetscCall(VecTaggerAbsoluteSetBox((*adaptor)->coarsenTag, &coarsenBox));
216c6f61ee2SBarry Smith   PetscFunctionReturn(PETSC_SUCCESS);
217c6f61ee2SBarry Smith }
218c6f61ee2SBarry Smith 
219c6f61ee2SBarry Smith /*@
220c6f61ee2SBarry Smith   DMAdaptorDestroy - Destroys a `DMAdaptor` object
221c6f61ee2SBarry Smith 
222c6f61ee2SBarry Smith   Collective
223c6f61ee2SBarry Smith 
224c6f61ee2SBarry Smith   Input Parameter:
225c6f61ee2SBarry Smith . adaptor - The `DMAdaptor` object
226c6f61ee2SBarry Smith 
227c6f61ee2SBarry Smith   Level: beginner
228c6f61ee2SBarry Smith 
229df514481SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAdaptor`, `DMAdaptorCreate()`, `DMAdaptorAdapt()`
230c6f61ee2SBarry Smith @*/
231c6f61ee2SBarry Smith PetscErrorCode DMAdaptorDestroy(DMAdaptor *adaptor)
232c6f61ee2SBarry Smith {
233c6f61ee2SBarry Smith   PetscFunctionBegin;
234c6f61ee2SBarry Smith   if (!*adaptor) PetscFunctionReturn(PETSC_SUCCESS);
2353a336bb1SMatthew G. Knepley   PetscValidHeaderSpecific(*adaptor, DMADAPTOR_CLASSID, 1);
236f4f49eeaSPierre Jolivet   if (--((PetscObject)*adaptor)->refct > 0) {
237c6f61ee2SBarry Smith     *adaptor = NULL;
238c6f61ee2SBarry Smith     PetscFunctionReturn(PETSC_SUCCESS);
239c6f61ee2SBarry Smith   }
240c6f61ee2SBarry Smith   PetscCall(VecTaggerDestroy(&(*adaptor)->refineTag));
241c6f61ee2SBarry Smith   PetscCall(VecTaggerDestroy(&(*adaptor)->coarsenTag));
242c6f61ee2SBarry Smith   PetscCall(PetscFree2((*adaptor)->exactSol, (*adaptor)->exactCtx));
2438b724c91SMatthew G. Knepley   PetscCall(DMAdaptorMonitorCancel(*adaptor));
244c6f61ee2SBarry Smith   PetscCall(PetscHeaderDestroy(adaptor));
245c6f61ee2SBarry Smith   PetscFunctionReturn(PETSC_SUCCESS);
246c6f61ee2SBarry Smith }
247c6f61ee2SBarry Smith 
248c6f61ee2SBarry Smith /*@
2493a336bb1SMatthew G. Knepley   DMAdaptorSetType - Sets the particular implementation for a adaptor.
2503a336bb1SMatthew G. Knepley 
2513a336bb1SMatthew G. Knepley   Collective
2523a336bb1SMatthew G. Knepley 
2533a336bb1SMatthew G. Knepley   Input Parameters:
2543a336bb1SMatthew G. Knepley + adaptor - The `DMAdaptor`
2553a336bb1SMatthew G. Knepley - method  - The name of the adaptor type
2563a336bb1SMatthew G. Knepley 
2573a336bb1SMatthew G. Knepley   Options Database Key:
2583a336bb1SMatthew G. Knepley . -adaptor_type <type> - Sets the adaptor type; see `DMAdaptorType`
2593a336bb1SMatthew G. Knepley 
2603a336bb1SMatthew G. Knepley   Level: intermediate
2613a336bb1SMatthew G. Knepley 
2623a336bb1SMatthew G. Knepley .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMAdaptor`, `DMAdaptorType`, `DMAdaptorGetType()`, `DMAdaptorCreate()`
2633a336bb1SMatthew G. Knepley @*/
2643a336bb1SMatthew G. Knepley PetscErrorCode DMAdaptorSetType(DMAdaptor adaptor, DMAdaptorType method)
2653a336bb1SMatthew G. Knepley {
2663a336bb1SMatthew G. Knepley   PetscErrorCode (*r)(DMAdaptor);
2673a336bb1SMatthew G. Knepley   PetscBool match;
2683a336bb1SMatthew G. Knepley 
2693a336bb1SMatthew G. Knepley   PetscFunctionBegin;
2703a336bb1SMatthew G. Knepley   PetscValidHeaderSpecific(adaptor, DMADAPTOR_CLASSID, 1);
2713a336bb1SMatthew G. Knepley   PetscCall(PetscObjectTypeCompare((PetscObject)adaptor, method, &match));
2723a336bb1SMatthew G. Knepley   if (match) PetscFunctionReturn(PETSC_SUCCESS);
2733a336bb1SMatthew G. Knepley 
2743a336bb1SMatthew G. Knepley   PetscCall(DMAdaptorRegisterAll());
2753a336bb1SMatthew G. Knepley   PetscCall(PetscFunctionListFind(DMAdaptorList, method, &r));
2763a336bb1SMatthew G. Knepley   PetscCheck(r, PetscObjectComm((PetscObject)adaptor), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DMAdaptor type: %s", method);
2773a336bb1SMatthew G. Knepley 
2783a336bb1SMatthew G. Knepley   PetscTryTypeMethod(adaptor, destroy);
2793a336bb1SMatthew G. Knepley   PetscCall(PetscMemzero(adaptor->ops, sizeof(*adaptor->ops)));
2803a336bb1SMatthew G. Knepley   PetscCall(PetscObjectChangeTypeName((PetscObject)adaptor, method));
2813a336bb1SMatthew G. Knepley   PetscCall((*r)(adaptor));
2823a336bb1SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
2833a336bb1SMatthew G. Knepley }
2843a336bb1SMatthew G. Knepley 
2853a336bb1SMatthew G. Knepley /*@
2863a336bb1SMatthew G. Knepley   DMAdaptorGetType - Gets the type name (as a string) from the adaptor.
2873a336bb1SMatthew G. Knepley 
2883a336bb1SMatthew G. Knepley   Not Collective
2893a336bb1SMatthew G. Knepley 
2903a336bb1SMatthew G. Knepley   Input Parameter:
2913a336bb1SMatthew G. Knepley . adaptor - The `DMAdaptor`
2923a336bb1SMatthew G. Knepley 
2933a336bb1SMatthew G. Knepley   Output Parameter:
2943a336bb1SMatthew G. Knepley . type - The `DMAdaptorType` name
2953a336bb1SMatthew G. Knepley 
2963a336bb1SMatthew G. Knepley   Level: intermediate
2973a336bb1SMatthew G. Knepley 
2983a336bb1SMatthew G. Knepley .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMAdaptor`, `DMAdaptorType`, `DMAdaptorSetType()`, `DMAdaptorCreate()`
2993a336bb1SMatthew G. Knepley @*/
3003a336bb1SMatthew G. Knepley PetscErrorCode DMAdaptorGetType(DMAdaptor adaptor, DMAdaptorType *type)
3013a336bb1SMatthew G. Knepley {
3023a336bb1SMatthew G. Knepley   PetscFunctionBegin;
3033a336bb1SMatthew G. Knepley   PetscValidHeaderSpecific(adaptor, DMADAPTOR_CLASSID, 1);
3043a336bb1SMatthew G. Knepley   PetscAssertPointer(type, 2);
3053a336bb1SMatthew G. Knepley   PetscCall(DMAdaptorRegisterAll());
3063a336bb1SMatthew G. Knepley   *type = ((PetscObject)adaptor)->type_name;
3073a336bb1SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
3083a336bb1SMatthew G. Knepley }
3093a336bb1SMatthew G. Knepley 
3108b724c91SMatthew G. Knepley static PetscErrorCode PetscViewerAndFormatCreate_Internal(PetscViewer viewer, PetscViewerFormat format, void *ctx, PetscViewerAndFormat **vf)
3118b724c91SMatthew G. Knepley {
3128b724c91SMatthew G. Knepley   PetscFunctionBegin;
3138b724c91SMatthew G. Knepley   PetscCall(PetscViewerAndFormatCreate(viewer, format, vf));
3148b724c91SMatthew G. Knepley   (*vf)->data = ctx;
3158b724c91SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
3168b724c91SMatthew G. Knepley }
3178b724c91SMatthew G. Knepley 
3188b724c91SMatthew G. Knepley /*@C
3198b724c91SMatthew G. Knepley   DMAdaptorMonitorSet - Sets an ADDITIONAL function to be called at every iteration to monitor
3208b724c91SMatthew G. Knepley   the error etc.
3218b724c91SMatthew G. Knepley 
3228b724c91SMatthew G. Knepley   Logically Collective
3238b724c91SMatthew G. Knepley 
3248b724c91SMatthew G. Knepley   Input Parameters:
3258b724c91SMatthew G. Knepley + adaptor        - the `DMAdaptor`
3268b724c91SMatthew G. Knepley . monitor        - pointer to function (if this is `NULL`, it turns off monitoring
3278b724c91SMatthew G. Knepley . ctx            - [optional] context for private data for the monitor routine (use `NULL` if no context is needed)
328*49abdd8aSBarry Smith - monitordestroy - [optional] routine that frees monitor context (may be `NULL`), see `PetscCtxDestroyFn` for its calling sequence
3298b724c91SMatthew G. Knepley 
3308b724c91SMatthew G. Knepley   Calling sequence of `monitor`:
3318b724c91SMatthew G. Knepley + adaptor - the `DMAdaptor`
3328b724c91SMatthew G. Knepley . it      - iteration number
3338b724c91SMatthew G. Knepley . odm     - the original `DM`
3348b724c91SMatthew G. Knepley . adm     - the adapted `DM`
3358b724c91SMatthew G. Knepley . Nf      - number of fields
3368b724c91SMatthew G. Knepley . enorms  - (estimated) 2-norm of the error for each field
3378b724c91SMatthew G. Knepley . error   - `Vec` of cellwise errors
3388b724c91SMatthew G. Knepley - ctx     - optional monitoring context, as set by `DMAdaptorMonitorSet()`
3398b724c91SMatthew G. Knepley 
3408b724c91SMatthew G. Knepley   Options Database Keys:
3418b724c91SMatthew G. Knepley + -adaptor_monitor_size                - sets `DMAdaptorMonitorSize()`
3428b724c91SMatthew G. Knepley . -adaptor_monitor_error               - sets `DMAdaptorMonitorError()`
3438b724c91SMatthew G. Knepley . -adaptor_monitor_error draw          - sets `DMAdaptorMonitorErrorDraw()` and plots error
3448b724c91SMatthew G. Knepley . -adaptor_monitor_error draw::draw_lg - sets `DMAdaptorMonitorErrorDrawLG()` and plots error
3458b724c91SMatthew G. Knepley - -dm_adaptor_monitor_cancel           - Cancels all monitors that have been hardwired into a code by calls to `DMAdaptorMonitorSet()`, but does not cancel those set via the options database.
3468b724c91SMatthew G. Knepley 
3478b724c91SMatthew G. Knepley   Level: beginner
3488b724c91SMatthew G. Knepley 
349*49abdd8aSBarry Smith .seealso: [](ch_snes), `DMAdaptorMonitorError()`, `DMAdaptor`, `PetscCtxDestroyFn`
3508b724c91SMatthew G. Knepley @*/
351*49abdd8aSBarry Smith PetscErrorCode DMAdaptorMonitorSet(DMAdaptor adaptor, PetscErrorCode (*monitor)(DMAdaptor adaptor, PetscInt it, DM odm, DM adm, PetscInt Nf, PetscReal enorms[], Vec error, void *ctx), void *ctx, PetscCtxDestroyFn *monitordestroy)
3528b724c91SMatthew G. Knepley {
3538b724c91SMatthew G. Knepley   PetscBool identical;
3548b724c91SMatthew G. Knepley 
3558b724c91SMatthew G. Knepley   PetscFunctionBegin;
3568b724c91SMatthew G. Knepley   PetscValidHeaderSpecific(adaptor, DMADAPTOR_CLASSID, 1);
3578b724c91SMatthew G. Knepley   for (PetscInt i = 0; i < adaptor->numbermonitors; i++) {
3588b724c91SMatthew G. Knepley     PetscCall(PetscMonitorCompare((PetscErrorCode (*)(void))monitor, ctx, monitordestroy, (PetscErrorCode (*)(void))adaptor->monitor[i], adaptor->monitorcontext[i], adaptor->monitordestroy[i], &identical));
3598b724c91SMatthew G. Knepley     if (identical) PetscFunctionReturn(PETSC_SUCCESS);
3608b724c91SMatthew G. Knepley   }
3618b724c91SMatthew G. Knepley   PetscCheck(adaptor->numbermonitors < MAXDMADAPTORMONITORS, PetscObjectComm((PetscObject)adaptor), PETSC_ERR_ARG_OUTOFRANGE, "Too many DMAdaptor monitors set");
3628b724c91SMatthew G. Knepley   adaptor->monitor[adaptor->numbermonitors]          = monitor;
3638b724c91SMatthew G. Knepley   adaptor->monitordestroy[adaptor->numbermonitors]   = monitordestroy;
3648b724c91SMatthew G. Knepley   adaptor->monitorcontext[adaptor->numbermonitors++] = (void *)ctx;
3658b724c91SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
3668b724c91SMatthew G. Knepley }
3678b724c91SMatthew G. Knepley 
3688b724c91SMatthew G. Knepley /*@
3698b724c91SMatthew G. Knepley   DMAdaptorMonitorCancel - Clears all monitors for a `DMAdaptor` object.
3708b724c91SMatthew G. Knepley 
3718b724c91SMatthew G. Knepley   Logically Collective
3728b724c91SMatthew G. Knepley 
3738b724c91SMatthew G. Knepley   Input Parameter:
3748b724c91SMatthew G. Knepley . adaptor - the `DMAdaptor`
3758b724c91SMatthew G. Knepley 
3768b724c91SMatthew G. Knepley   Options Database Key:
3778b724c91SMatthew G. Knepley . -dm_adaptor_monitor_cancel - Cancels all monitors that have been hardwired into a code by calls to `DMAdaptorMonitorSet()`, but does not cancel those set via the options database.
3788b724c91SMatthew G. Knepley 
3798b724c91SMatthew G. Knepley   Level: intermediate
3808b724c91SMatthew G. Knepley 
3818b724c91SMatthew G. Knepley .seealso: [](ch_snes), `DMAdaptorMonitorError()`, `DMAdaptorMonitorSet()`, `DMAdaptor`
3828b724c91SMatthew G. Knepley @*/
3838b724c91SMatthew G. Knepley PetscErrorCode DMAdaptorMonitorCancel(DMAdaptor adaptor)
3848b724c91SMatthew G. Knepley {
3858b724c91SMatthew G. Knepley   PetscFunctionBegin;
3868b724c91SMatthew G. Knepley   PetscValidHeaderSpecific(adaptor, DMADAPTOR_CLASSID, 1);
3878b724c91SMatthew G. Knepley   for (PetscInt i = 0; i < adaptor->numbermonitors; ++i) {
3888b724c91SMatthew G. Knepley     if (adaptor->monitordestroy[i]) PetscCall((*adaptor->monitordestroy[i])(&adaptor->monitorcontext[i]));
3898b724c91SMatthew G. Knepley   }
3908b724c91SMatthew G. Knepley   adaptor->numbermonitors = 0;
3918b724c91SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
3928b724c91SMatthew G. Knepley }
3938b724c91SMatthew G. Knepley 
3948b724c91SMatthew G. Knepley /*@C
3958b724c91SMatthew G. Knepley   DMAdaptorMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user in the options database
3968b724c91SMatthew G. Knepley 
3978b724c91SMatthew G. Knepley   Collective
3988b724c91SMatthew G. Knepley 
3998b724c91SMatthew G. Knepley   Input Parameters:
4008b724c91SMatthew G. Knepley + adaptor - `DMadaptor` object you wish to monitor
4018b724c91SMatthew G. Knepley . opt     - the command line option for this monitor
4028b724c91SMatthew G. Knepley . name    - the monitor type one is seeking
4038b724c91SMatthew G. Knepley - ctx     - An optional user context for the monitor, or `NULL`
4048b724c91SMatthew G. Knepley 
4058b724c91SMatthew G. Knepley   Level: developer
4068b724c91SMatthew G. Knepley 
4078b724c91SMatthew G. Knepley .seealso: [](ch_snes), `DMAdaptorMonitorRegister()`, `DMAdaptorMonitorSet()`, `PetscOptionsGetViewer()`
4088b724c91SMatthew G. Knepley @*/
4098b724c91SMatthew G. Knepley PetscErrorCode DMAdaptorMonitorSetFromOptions(DMAdaptor adaptor, const char opt[], const char name[], void *ctx)
4108b724c91SMatthew G. Knepley {
4118b724c91SMatthew G. Knepley   PetscErrorCode (*mfunc)(DMAdaptor, PetscInt, DM, DM, PetscInt, PetscReal[], Vec, void *);
4128b724c91SMatthew G. Knepley   PetscErrorCode (*cfunc)(PetscViewer, PetscViewerFormat, void *, PetscViewerAndFormat **);
4138b724c91SMatthew G. Knepley   PetscErrorCode (*dfunc)(PetscViewerAndFormat **);
4148b724c91SMatthew G. Knepley   PetscViewerAndFormat *vf;
4158b724c91SMatthew G. Knepley   PetscViewer           viewer;
4168b724c91SMatthew G. Knepley   PetscViewerFormat     format;
4178b724c91SMatthew G. Knepley   PetscViewerType       vtype;
4188b724c91SMatthew G. Knepley   char                  key[PETSC_MAX_PATH_LEN];
4198b724c91SMatthew G. Knepley   PetscBool             flg;
4208b724c91SMatthew G. Knepley   const char           *prefix = NULL;
4218b724c91SMatthew G. Knepley 
4228b724c91SMatthew G. Knepley   PetscFunctionBegin;
4238b724c91SMatthew G. Knepley   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)adaptor, &prefix));
4248b724c91SMatthew G. Knepley   PetscCall(PetscOptionsCreateViewer(PetscObjectComm((PetscObject)adaptor), ((PetscObject)adaptor)->options, prefix, opt, &viewer, &format, &flg));
4258b724c91SMatthew G. Knepley   if (!flg) PetscFunctionReturn(PETSC_SUCCESS);
4268b724c91SMatthew G. Knepley 
4278b724c91SMatthew G. Knepley   PetscCall(PetscViewerGetType(viewer, &vtype));
4288b724c91SMatthew G. Knepley   PetscCall(DMAdaptorMonitorMakeKey_Internal(name, vtype, format, key));
4298b724c91SMatthew G. Knepley   PetscCall(PetscFunctionListFind(DMAdaptorMonitorList, key, &mfunc));
4308b724c91SMatthew G. Knepley   PetscCall(PetscFunctionListFind(DMAdaptorMonitorCreateList, key, &cfunc));
4318b724c91SMatthew G. Knepley   PetscCall(PetscFunctionListFind(DMAdaptorMonitorDestroyList, key, &dfunc));
4328b724c91SMatthew G. Knepley   if (!cfunc) cfunc = PetscViewerAndFormatCreate_Internal;
4338b724c91SMatthew G. Knepley   if (!dfunc) dfunc = PetscViewerAndFormatDestroy;
4348b724c91SMatthew G. Knepley 
4358b724c91SMatthew G. Knepley   PetscCall((*cfunc)(viewer, format, ctx, &vf));
4368b724c91SMatthew G. Knepley   PetscCall(PetscViewerDestroy(&viewer));
437*49abdd8aSBarry Smith   PetscCall(DMAdaptorMonitorSet(adaptor, mfunc, vf, (PetscCtxDestroyFn *)dfunc));
4388b724c91SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
4398b724c91SMatthew G. Knepley }
4408b724c91SMatthew G. Knepley 
4413a336bb1SMatthew G. Knepley /*@
442e03fd340SMatthew G. Knepley   DMAdaptorSetOptionsPrefix - Sets the prefix used for searching for all `DMAdaptor` options in the database.
443e03fd340SMatthew G. Knepley 
444e03fd340SMatthew G. Knepley   Logically Collective
445e03fd340SMatthew G. Knepley 
446e03fd340SMatthew G. Knepley   Input Parameters:
447e03fd340SMatthew G. Knepley + adaptor - the `DMAdaptor`
448e03fd340SMatthew G. Knepley - prefix  - the prefix to prepend to all option names
449e03fd340SMatthew G. Knepley 
450e03fd340SMatthew G. Knepley   Level: advanced
451e03fd340SMatthew G. Knepley 
452e03fd340SMatthew G. Knepley   Note:
453e03fd340SMatthew G. Knepley   A hyphen (-) must NOT be given at the beginning of the prefix name.
454e03fd340SMatthew G. Knepley   The first character of all runtime options is AUTOMATICALLY the hyphen.
455e03fd340SMatthew G. Knepley 
456e03fd340SMatthew G. Knepley .seealso: [](ch_snes), `DMAdaptor`, `SNESSetOptionsPrefix()`, `DMAdaptorSetFromOptions()`
457e03fd340SMatthew G. Knepley @*/
458e03fd340SMatthew G. Knepley PetscErrorCode DMAdaptorSetOptionsPrefix(DMAdaptor adaptor, const char prefix[])
459e03fd340SMatthew G. Knepley {
460e03fd340SMatthew G. Knepley   PetscFunctionBegin;
461e03fd340SMatthew G. Knepley   PetscValidHeaderSpecific(adaptor, DMADAPTOR_CLASSID, 1);
462e03fd340SMatthew G. Knepley   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)adaptor, prefix));
463e03fd340SMatthew G. Knepley   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)adaptor->refineTag, prefix));
464e03fd340SMatthew G. Knepley   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)adaptor->refineTag, "refine_"));
465e03fd340SMatthew G. Knepley   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)adaptor->coarsenTag, prefix));
466e03fd340SMatthew G. Knepley   PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)adaptor->coarsenTag, "coarsen_"));
467e03fd340SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
468e03fd340SMatthew G. Knepley }
469e03fd340SMatthew G. Knepley 
470e03fd340SMatthew G. Knepley /*@
471c6f61ee2SBarry Smith   DMAdaptorSetFromOptions - Sets properties of a `DMAdaptor` object from values in the options database
472c6f61ee2SBarry Smith 
473c6f61ee2SBarry Smith   Collective
474c6f61ee2SBarry Smith 
475c6f61ee2SBarry Smith   Input Parameter:
476c6f61ee2SBarry Smith . adaptor - The `DMAdaptor` object
477c6f61ee2SBarry Smith 
478c6f61ee2SBarry Smith   Options Database Keys:
4798b724c91SMatthew G. Knepley + -adaptor_monitor_size                - Monitor the mesh size
4808b724c91SMatthew G. Knepley . -adaptor_monitor_error               - Monitor the solution error
481c6f61ee2SBarry Smith . -adaptor_sequence_num <num>          - Number of adaptations to generate an optimal grid
482c6f61ee2SBarry Smith . -adaptor_target_num <num>            - Set the target number of vertices N_adapt, -1 for automatic determination
4833a336bb1SMatthew G. Knepley . -adaptor_refinement_factor <r>       - Set r such that N_adapt = r^dim N_orig
484d7c1f440SPierre Jolivet - -adaptor_mixed_setup_function <func> - Set the function func that sets up the mixed problem
485c6f61ee2SBarry Smith 
486c6f61ee2SBarry Smith   Level: beginner
487c6f61ee2SBarry Smith 
488df514481SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAdaptor`, `DMAdaptorCreate()`, `DMAdaptorAdapt()`
489c6f61ee2SBarry Smith @*/
490c6f61ee2SBarry Smith PetscErrorCode DMAdaptorSetFromOptions(DMAdaptor adaptor)
491c6f61ee2SBarry Smith {
4923a336bb1SMatthew G. Knepley   char                  typeName[PETSC_MAX_PATH_LEN];
4933a336bb1SMatthew G. Knepley   const char           *defName = DMADAPTORGRADIENT;
4943a336bb1SMatthew G. Knepley   char                  funcname[PETSC_MAX_PATH_LEN];
4958b724c91SMatthew G. Knepley   DMAdaptationCriterion criterion = DM_ADAPTATION_NONE;
4963a336bb1SMatthew G. Knepley   PetscBool             flg;
4973a336bb1SMatthew G. Knepley 
498c6f61ee2SBarry Smith   PetscFunctionBegin;
4993a336bb1SMatthew G. Knepley   PetscObjectOptionsBegin((PetscObject)adaptor);
5003a336bb1SMatthew G. Knepley   PetscCall(PetscOptionsFList("-adaptor_type", "DMAdaptor", "DMAdaptorSetType", DMAdaptorList, defName, typeName, 1024, &flg));
5013a336bb1SMatthew G. Knepley   if (flg) PetscCall(DMAdaptorSetType(adaptor, typeName));
5023a336bb1SMatthew G. Knepley   else if (!((PetscObject)adaptor)->type_name) PetscCall(DMAdaptorSetType(adaptor, defName));
5038b724c91SMatthew G. Knepley   PetscCall(PetscOptionsEnum("-adaptor_criterion", "Criterion used to drive adaptation", "", DMAdaptationCriteria, (PetscEnum)criterion, (PetscEnum *)&criterion, &flg));
5048b724c91SMatthew G. Knepley   if (flg) PetscCall(DMAdaptorSetCriterion(adaptor, criterion));
505c6f61ee2SBarry Smith   PetscCall(PetscOptionsInt("-adaptor_sequence_num", "Number of adaptations to generate an optimal grid", "DMAdaptorSetSequenceLength", adaptor->numSeq, &adaptor->numSeq, NULL));
506c6f61ee2SBarry Smith   PetscCall(PetscOptionsInt("-adaptor_target_num", "Set the target number of vertices N_adapt, -1 for automatic determination", "DMAdaptor", adaptor->Nadapt, &adaptor->Nadapt, NULL));
507c6f61ee2SBarry Smith   PetscCall(PetscOptionsReal("-adaptor_refinement_factor", "Set r such that N_adapt = r^dim N_orig", "DMAdaptor", adaptor->refinementFactor, &adaptor->refinementFactor, NULL));
5083a336bb1SMatthew G. Knepley   PetscCall(PetscOptionsString("-adaptor_mixed_setup_function", "Function to setup the mixed problem", "DMAdaptorSetMixedSetupFunction", funcname, funcname, sizeof(funcname), &flg));
5093a336bb1SMatthew G. Knepley   if (flg) {
5103a336bb1SMatthew G. Knepley     PetscErrorCode (*setupFunc)(DMAdaptor, DM);
5113a336bb1SMatthew G. Knepley 
5123a336bb1SMatthew G. Knepley     PetscCall(PetscDLSym(NULL, funcname, (void **)&setupFunc));
5133a336bb1SMatthew G. Knepley     PetscCheck(setupFunc, PetscObjectComm((PetscObject)adaptor), PETSC_ERR_ARG_WRONG, "Could not locate function %s", funcname);
5143a336bb1SMatthew G. Knepley     PetscCall(DMAdaptorSetMixedSetupFunction(adaptor, setupFunc));
5153a336bb1SMatthew G. Knepley   }
5168b724c91SMatthew G. Knepley   PetscCall(DMAdaptorMonitorSetFromOptions(adaptor, "-adaptor_monitor_size", "size", adaptor));
5178b724c91SMatthew G. Knepley   PetscCall(DMAdaptorMonitorSetFromOptions(adaptor, "-adaptor_monitor_error", "error", adaptor));
518c6f61ee2SBarry Smith   PetscOptionsEnd();
519c6f61ee2SBarry Smith   PetscCall(VecTaggerSetFromOptions(adaptor->refineTag));
520c6f61ee2SBarry Smith   PetscCall(VecTaggerSetFromOptions(adaptor->coarsenTag));
521c6f61ee2SBarry Smith   PetscFunctionReturn(PETSC_SUCCESS);
522c6f61ee2SBarry Smith }
523c6f61ee2SBarry Smith 
524c6f61ee2SBarry Smith /*@
525c6f61ee2SBarry Smith   DMAdaptorView - Views a `DMAdaptor` object
526c6f61ee2SBarry Smith 
527c6f61ee2SBarry Smith   Collective
528c6f61ee2SBarry Smith 
529c6f61ee2SBarry Smith   Input Parameters:
530c6f61ee2SBarry Smith + adaptor - The `DMAdaptor` object
531c6f61ee2SBarry Smith - viewer  - The `PetscViewer` object
532c6f61ee2SBarry Smith 
533c6f61ee2SBarry Smith   Level: beginner
534c6f61ee2SBarry Smith 
535df514481SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAdaptor`, `DMAdaptorCreate()`, `DMAdaptorAdapt()`
536c6f61ee2SBarry Smith @*/
537c6f61ee2SBarry Smith PetscErrorCode DMAdaptorView(DMAdaptor adaptor, PetscViewer viewer)
538c6f61ee2SBarry Smith {
539c6f61ee2SBarry Smith   PetscFunctionBegin;
540c6f61ee2SBarry Smith   PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)adaptor, viewer));
541c6f61ee2SBarry Smith   PetscCall(PetscViewerASCIIPrintf(viewer, "DM Adaptor\n"));
542c6f61ee2SBarry Smith   PetscCall(PetscViewerASCIIPrintf(viewer, "  sequence length: %" PetscInt_FMT "\n", adaptor->numSeq));
543c6f61ee2SBarry Smith   PetscCall(VecTaggerView(adaptor->refineTag, viewer));
544c6f61ee2SBarry Smith   PetscCall(VecTaggerView(adaptor->coarsenTag, viewer));
545c6f61ee2SBarry Smith   PetscFunctionReturn(PETSC_SUCCESS);
546c6f61ee2SBarry Smith }
547c6f61ee2SBarry Smith 
548c6f61ee2SBarry Smith /*@
549c6f61ee2SBarry Smith   DMAdaptorGetSolver - Gets the solver used to produce discrete solutions
550c6f61ee2SBarry Smith 
551c6f61ee2SBarry Smith   Not Collective
552c6f61ee2SBarry Smith 
553c6f61ee2SBarry Smith   Input Parameter:
554c6f61ee2SBarry Smith . adaptor - The `DMAdaptor` object
555c6f61ee2SBarry Smith 
556c6f61ee2SBarry Smith   Output Parameter:
557c6f61ee2SBarry Smith . snes - The solver
558c6f61ee2SBarry Smith 
559c6f61ee2SBarry Smith   Level: intermediate
560c6f61ee2SBarry Smith 
561df514481SBarry Smith .seealso: [](ch_dmbase), `DM`, `DMAdaptor`, `DMAdaptorSetSolver()`, `DMAdaptorCreate()`, `DMAdaptorAdapt()`
562c6f61ee2SBarry Smith @*/
563c6f61ee2SBarry Smith PetscErrorCode DMAdaptorGetSolver(DMAdaptor adaptor, SNES *snes)
564c6f61ee2SBarry Smith {
565c6f61ee2SBarry Smith   PetscFunctionBegin;
5663a336bb1SMatthew G. Knepley   PetscValidHeaderSpecific(adaptor, DMADAPTOR_CLASSID, 1);
567c6f61ee2SBarry Smith   PetscAssertPointer(snes, 2);
568c6f61ee2SBarry Smith   *snes = adaptor->snes;
569c6f61ee2SBarry Smith   PetscFunctionReturn(PETSC_SUCCESS);
570c6f61ee2SBarry Smith }
571c6f61ee2SBarry Smith 
572c6f61ee2SBarry Smith /*@
573c6f61ee2SBarry Smith   DMAdaptorSetSolver - Sets the solver used to produce discrete solutions
574c6f61ee2SBarry Smith 
575c6f61ee2SBarry Smith   Not Collective
576c6f61ee2SBarry Smith 
577c6f61ee2SBarry Smith   Input Parameters:
578c6f61ee2SBarry Smith + adaptor - The `DMAdaptor` object
579c6f61ee2SBarry Smith - snes    - The solver, this MUST have an attached `DM`/`PetscDS`, so that the exact solution can be computed
580c6f61ee2SBarry Smith 
581c6f61ee2SBarry Smith   Level: intermediate
582c6f61ee2SBarry Smith 
583df514481SBarry Smith .seealso: [](ch_dmbase), `DMAdaptor`, `DMAdaptorGetSolver()`, `DMAdaptorCreate()`, `DMAdaptorAdapt()`
584c6f61ee2SBarry Smith @*/
585c6f61ee2SBarry Smith PetscErrorCode DMAdaptorSetSolver(DMAdaptor adaptor, SNES snes)
586c6f61ee2SBarry Smith {
587c6f61ee2SBarry Smith   PetscFunctionBegin;
5883a336bb1SMatthew G. Knepley   PetscValidHeaderSpecific(adaptor, DMADAPTOR_CLASSID, 1);
589c6f61ee2SBarry Smith   PetscValidHeaderSpecific(snes, SNES_CLASSID, 2);
590c6f61ee2SBarry Smith   adaptor->snes = snes;
591c6f61ee2SBarry Smith   PetscCall(SNESGetDM(adaptor->snes, &adaptor->idm));
592c6f61ee2SBarry Smith   PetscFunctionReturn(PETSC_SUCCESS);
593c6f61ee2SBarry Smith }
594c6f61ee2SBarry Smith 
595c6f61ee2SBarry Smith /*@
596c6f61ee2SBarry Smith   DMAdaptorGetSequenceLength - Gets the number of sequential adaptations used by an adapter
597c6f61ee2SBarry Smith 
598c6f61ee2SBarry Smith   Not Collective
599c6f61ee2SBarry Smith 
600c6f61ee2SBarry Smith   Input Parameter:
601c6f61ee2SBarry Smith . adaptor - The `DMAdaptor` object
602c6f61ee2SBarry Smith 
603c6f61ee2SBarry Smith   Output Parameter:
604c6f61ee2SBarry Smith . num - The number of adaptations
605c6f61ee2SBarry Smith 
606c6f61ee2SBarry Smith   Level: intermediate
607c6f61ee2SBarry Smith 
608df514481SBarry Smith .seealso: [](ch_dmbase), `DMAdaptor`, `DMAdaptorSetSequenceLength()`, `DMAdaptorCreate()`, `DMAdaptorAdapt()`
609c6f61ee2SBarry Smith @*/
610c6f61ee2SBarry Smith PetscErrorCode DMAdaptorGetSequenceLength(DMAdaptor adaptor, PetscInt *num)
611c6f61ee2SBarry Smith {
612c6f61ee2SBarry Smith   PetscFunctionBegin;
6133a336bb1SMatthew G. Knepley   PetscValidHeaderSpecific(adaptor, DMADAPTOR_CLASSID, 1);
614c6f61ee2SBarry Smith   PetscAssertPointer(num, 2);
615c6f61ee2SBarry Smith   *num = adaptor->numSeq;
616c6f61ee2SBarry Smith   PetscFunctionReturn(PETSC_SUCCESS);
617c6f61ee2SBarry Smith }
618c6f61ee2SBarry Smith 
619c6f61ee2SBarry Smith /*@
620c6f61ee2SBarry Smith   DMAdaptorSetSequenceLength - Sets the number of sequential adaptations
621c6f61ee2SBarry Smith 
622c6f61ee2SBarry Smith   Not Collective
623c6f61ee2SBarry Smith 
624c6f61ee2SBarry Smith   Input Parameters:
625c6f61ee2SBarry Smith + adaptor - The `DMAdaptor` object
626c6f61ee2SBarry Smith - num     - The number of adaptations
627c6f61ee2SBarry Smith 
628c6f61ee2SBarry Smith   Level: intermediate
629c6f61ee2SBarry Smith 
630df514481SBarry Smith .seealso: [](ch_dmbase), `DMAdaptorGetSequenceLength()`, `DMAdaptorCreate()`, `DMAdaptorAdapt()`
631c6f61ee2SBarry Smith @*/
632c6f61ee2SBarry Smith PetscErrorCode DMAdaptorSetSequenceLength(DMAdaptor adaptor, PetscInt num)
633c6f61ee2SBarry Smith {
634c6f61ee2SBarry Smith   PetscFunctionBegin;
6353a336bb1SMatthew G. Knepley   PetscValidHeaderSpecific(adaptor, DMADAPTOR_CLASSID, 1);
636c6f61ee2SBarry Smith   adaptor->numSeq = num;
637c6f61ee2SBarry Smith   PetscFunctionReturn(PETSC_SUCCESS);
638c6f61ee2SBarry Smith }
639c6f61ee2SBarry Smith 
6403a336bb1SMatthew G. Knepley static PetscErrorCode DMAdaptorTransferSolution_Exact_Private(DMAdaptor adaptor, DM dm, Vec u, DM adm, Vec au, void *ctx)
6413a336bb1SMatthew G. Knepley {
6423a336bb1SMatthew G. Knepley   PetscFunctionBeginUser;
6433a336bb1SMatthew G. Knepley   PetscCall(DMProjectFunction(adm, 0.0, adaptor->exactSol, adaptor->exactCtx, INSERT_ALL_VALUES, au));
6443a336bb1SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
6453a336bb1SMatthew G. Knepley }
6463a336bb1SMatthew G. Knepley 
647c6f61ee2SBarry Smith /*@
648c6f61ee2SBarry Smith   DMAdaptorSetUp - After the solver is specified, creates data structures for controlling adaptivity
649c6f61ee2SBarry Smith 
650c6f61ee2SBarry Smith   Collective
651c6f61ee2SBarry Smith 
652c6f61ee2SBarry Smith   Input Parameter:
653c6f61ee2SBarry Smith . adaptor - The `DMAdaptor` object
654c6f61ee2SBarry Smith 
655c6f61ee2SBarry Smith   Level: beginner
656c6f61ee2SBarry Smith 
657df514481SBarry Smith .seealso: [](ch_dmbase), `DMAdaptor`, `DMAdaptorCreate()`, `DMAdaptorAdapt()`
658c6f61ee2SBarry Smith @*/
659c6f61ee2SBarry Smith PetscErrorCode DMAdaptorSetUp(DMAdaptor adaptor)
660c6f61ee2SBarry Smith {
661c6f61ee2SBarry Smith   PetscDS  prob;
662c6f61ee2SBarry Smith   PetscInt Nf, f;
663c6f61ee2SBarry Smith 
664c6f61ee2SBarry Smith   PetscFunctionBegin;
665c6f61ee2SBarry Smith   PetscCall(DMGetDS(adaptor->idm, &prob));
666c6f61ee2SBarry Smith   PetscCall(VecTaggerSetUp(adaptor->refineTag));
667c6f61ee2SBarry Smith   PetscCall(VecTaggerSetUp(adaptor->coarsenTag));
668c6f61ee2SBarry Smith   PetscCall(PetscDSGetNumFields(prob, &Nf));
669c6f61ee2SBarry Smith   PetscCall(PetscMalloc2(Nf, &adaptor->exactSol, Nf, &adaptor->exactCtx));
670c6f61ee2SBarry Smith   for (f = 0; f < Nf; ++f) {
671c6f61ee2SBarry Smith     PetscCall(PetscDSGetExactSolution(prob, f, &adaptor->exactSol[f], &adaptor->exactCtx[f]));
672c6f61ee2SBarry Smith     /* TODO Have a flag that forces projection rather than using the exact solution */
673c6f61ee2SBarry Smith     if (adaptor->exactSol[0]) PetscCall(DMAdaptorSetTransferFunction(adaptor, DMAdaptorTransferSolution_Exact_Private));
674c6f61ee2SBarry Smith   }
6753a336bb1SMatthew G. Knepley   PetscTryTypeMethod(adaptor, setup);
676c6f61ee2SBarry Smith   PetscFunctionReturn(PETSC_SUCCESS);
677c6f61ee2SBarry Smith }
678c6f61ee2SBarry Smith 
679c6f61ee2SBarry Smith PetscErrorCode DMAdaptorGetTransferFunction(DMAdaptor adaptor, PetscErrorCode (**tfunc)(DMAdaptor, DM, Vec, DM, Vec, void *))
680c6f61ee2SBarry Smith {
681c6f61ee2SBarry Smith   PetscFunctionBegin;
682c6f61ee2SBarry Smith   *tfunc = adaptor->ops->transfersolution;
683c6f61ee2SBarry Smith   PetscFunctionReturn(PETSC_SUCCESS);
684c6f61ee2SBarry Smith }
685c6f61ee2SBarry Smith 
686c6f61ee2SBarry Smith PetscErrorCode DMAdaptorSetTransferFunction(DMAdaptor adaptor, PetscErrorCode (*tfunc)(DMAdaptor, DM, Vec, DM, Vec, void *))
687c6f61ee2SBarry Smith {
688c6f61ee2SBarry Smith   PetscFunctionBegin;
689c6f61ee2SBarry Smith   adaptor->ops->transfersolution = tfunc;
690c6f61ee2SBarry Smith   PetscFunctionReturn(PETSC_SUCCESS);
691c6f61ee2SBarry Smith }
692c6f61ee2SBarry Smith 
693c6f61ee2SBarry Smith static PetscErrorCode DMAdaptorPreAdapt(DMAdaptor adaptor, Vec locX)
694c6f61ee2SBarry Smith {
695c6f61ee2SBarry Smith   DM           plex;
696c6f61ee2SBarry Smith   PetscDS      prob;
697c6f61ee2SBarry Smith   PetscObject  obj;
698c6f61ee2SBarry Smith   PetscClassId id;
699c6f61ee2SBarry Smith   PetscBool    isForest;
700c6f61ee2SBarry Smith 
701c6f61ee2SBarry Smith   PetscFunctionBegin;
702c6f61ee2SBarry Smith   PetscCall(DMConvert(adaptor->idm, DMPLEX, &plex));
703c6f61ee2SBarry Smith   PetscCall(DMGetDS(adaptor->idm, &prob));
704c6f61ee2SBarry Smith   PetscCall(PetscDSGetDiscretization(prob, 0, &obj));
705c6f61ee2SBarry Smith   PetscCall(PetscObjectGetClassId(obj, &id));
706c6f61ee2SBarry Smith   PetscCall(DMIsForest(adaptor->idm, &isForest));
707c6f61ee2SBarry Smith   if (adaptor->adaptCriterion == DM_ADAPTATION_NONE) {
708c6f61ee2SBarry Smith     if (isForest) {
709c6f61ee2SBarry Smith       adaptor->adaptCriterion = DM_ADAPTATION_LABEL;
710c6f61ee2SBarry Smith     }
711c6f61ee2SBarry Smith #if defined(PETSC_HAVE_PRAGMATIC)
712c6f61ee2SBarry Smith     else {
713c6f61ee2SBarry Smith       adaptor->adaptCriterion = DM_ADAPTATION_METRIC;
714c6f61ee2SBarry Smith     }
715c6f61ee2SBarry Smith #elif defined(PETSC_HAVE_MMG)
716c6f61ee2SBarry Smith     else {
717c6f61ee2SBarry Smith       adaptor->adaptCriterion = DM_ADAPTATION_METRIC;
718c6f61ee2SBarry Smith     }
719c6f61ee2SBarry Smith #elif defined(PETSC_HAVE_PARMMG)
720c6f61ee2SBarry Smith     else {
721c6f61ee2SBarry Smith       adaptor->adaptCriterion = DM_ADAPTATION_METRIC;
722c6f61ee2SBarry Smith     }
723c6f61ee2SBarry Smith #else
724c6f61ee2SBarry Smith     else {
7253a336bb1SMatthew G. Knepley       adaptor->adaptCriterion = DM_ADAPTATION_LABEL;
726c6f61ee2SBarry Smith     }
727c6f61ee2SBarry Smith #endif
728c6f61ee2SBarry Smith   }
729c6f61ee2SBarry Smith   if (id == PETSCFV_CLASSID) {
730c6f61ee2SBarry Smith     adaptor->femType = PETSC_FALSE;
731c6f61ee2SBarry Smith   } else {
732c6f61ee2SBarry Smith     adaptor->femType = PETSC_TRUE;
733c6f61ee2SBarry Smith   }
734c6f61ee2SBarry Smith   if (adaptor->femType) {
735c6f61ee2SBarry Smith     /* Compute local solution bc */
736c6f61ee2SBarry Smith     PetscCall(DMPlexInsertBoundaryValues(plex, PETSC_TRUE, locX, 0.0, adaptor->faceGeom, adaptor->cellGeom, NULL));
737c6f61ee2SBarry Smith   } else {
738c6f61ee2SBarry Smith     PetscFV      fvm = (PetscFV)obj;
739c6f61ee2SBarry Smith     PetscLimiter noneLimiter;
740c6f61ee2SBarry Smith     Vec          grad;
741c6f61ee2SBarry Smith 
742c6f61ee2SBarry Smith     PetscCall(PetscFVGetComputeGradients(fvm, &adaptor->computeGradient));
743c6f61ee2SBarry Smith     PetscCall(PetscFVSetComputeGradients(fvm, PETSC_TRUE));
744c6f61ee2SBarry Smith     /* Use no limiting when reconstructing gradients for adaptivity */
745c6f61ee2SBarry Smith     PetscCall(PetscFVGetLimiter(fvm, &adaptor->limiter));
746c6f61ee2SBarry Smith     PetscCall(PetscObjectReference((PetscObject)adaptor->limiter));
747c6f61ee2SBarry Smith     PetscCall(PetscLimiterCreate(PetscObjectComm((PetscObject)fvm), &noneLimiter));
748c6f61ee2SBarry Smith     PetscCall(PetscLimiterSetType(noneLimiter, PETSCLIMITERNONE));
749c6f61ee2SBarry Smith     PetscCall(PetscFVSetLimiter(fvm, noneLimiter));
750c6f61ee2SBarry Smith     /* Get FVM data */
751c6f61ee2SBarry Smith     PetscCall(DMPlexGetDataFVM(plex, fvm, &adaptor->cellGeom, &adaptor->faceGeom, &adaptor->gradDM));
752c6f61ee2SBarry Smith     PetscCall(VecGetDM(adaptor->cellGeom, &adaptor->cellDM));
753c6f61ee2SBarry Smith     PetscCall(VecGetArrayRead(adaptor->cellGeom, &adaptor->cellGeomArray));
754c6f61ee2SBarry Smith     /* Compute local solution bc */
755c6f61ee2SBarry Smith     PetscCall(DMPlexInsertBoundaryValues(plex, PETSC_TRUE, locX, 0.0, adaptor->faceGeom, adaptor->cellGeom, NULL));
756c6f61ee2SBarry Smith     /* Compute gradients */
757c6f61ee2SBarry Smith     PetscCall(DMCreateGlobalVector(adaptor->gradDM, &grad));
758c6f61ee2SBarry Smith     PetscCall(DMPlexReconstructGradientsFVM(plex, locX, grad));
759c6f61ee2SBarry Smith     PetscCall(DMGetLocalVector(adaptor->gradDM, &adaptor->cellGrad));
760c6f61ee2SBarry Smith     PetscCall(DMGlobalToLocalBegin(adaptor->gradDM, grad, INSERT_VALUES, adaptor->cellGrad));
761c6f61ee2SBarry Smith     PetscCall(DMGlobalToLocalEnd(adaptor->gradDM, grad, INSERT_VALUES, adaptor->cellGrad));
762c6f61ee2SBarry Smith     PetscCall(VecDestroy(&grad));
763c6f61ee2SBarry Smith     PetscCall(VecGetArrayRead(adaptor->cellGrad, &adaptor->cellGradArray));
764c6f61ee2SBarry Smith   }
765c6f61ee2SBarry Smith   PetscCall(DMDestroy(&plex));
766c6f61ee2SBarry Smith   PetscFunctionReturn(PETSC_SUCCESS);
767c6f61ee2SBarry Smith }
768c6f61ee2SBarry Smith 
769c6f61ee2SBarry Smith static PetscErrorCode DMAdaptorTransferSolution(DMAdaptor adaptor, DM dm, Vec x, DM adm, Vec ax)
770c6f61ee2SBarry Smith {
771c6f61ee2SBarry Smith   PetscReal time = 0.0;
772c6f61ee2SBarry Smith   Mat       interp;
773c6f61ee2SBarry Smith   void     *ctx;
774c6f61ee2SBarry Smith 
775c6f61ee2SBarry Smith   PetscFunctionBegin;
776c6f61ee2SBarry Smith   PetscCall(DMGetApplicationContext(dm, &ctx));
777c6f61ee2SBarry Smith   if (adaptor->ops->transfersolution) PetscUseTypeMethod(adaptor, transfersolution, dm, x, adm, ax, ctx);
778c6f61ee2SBarry Smith   else {
779c6f61ee2SBarry Smith     switch (adaptor->adaptCriterion) {
780c6f61ee2SBarry Smith     case DM_ADAPTATION_LABEL:
781c6f61ee2SBarry Smith       PetscCall(DMForestTransferVec(dm, x, adm, ax, PETSC_TRUE, time));
782c6f61ee2SBarry Smith       break;
783c6f61ee2SBarry Smith     case DM_ADAPTATION_REFINE:
784c6f61ee2SBarry Smith     case DM_ADAPTATION_METRIC:
785c6f61ee2SBarry Smith       PetscCall(DMCreateInterpolation(dm, adm, &interp, NULL));
786c6f61ee2SBarry Smith       PetscCall(MatInterpolate(interp, x, ax));
787c6f61ee2SBarry Smith       PetscCall(DMInterpolate(dm, interp, adm));
788c6f61ee2SBarry Smith       PetscCall(MatDestroy(&interp));
789c6f61ee2SBarry Smith       break;
790c6f61ee2SBarry Smith     default:
791c6f61ee2SBarry Smith       SETERRQ(PetscObjectComm((PetscObject)adaptor), PETSC_ERR_SUP, "No built-in projection for this adaptation criterion: %d", adaptor->adaptCriterion);
792c6f61ee2SBarry Smith     }
793c6f61ee2SBarry Smith   }
794c6f61ee2SBarry Smith   PetscFunctionReturn(PETSC_SUCCESS);
795c6f61ee2SBarry Smith }
796c6f61ee2SBarry Smith 
797c6f61ee2SBarry Smith static PetscErrorCode DMAdaptorPostAdapt(DMAdaptor adaptor)
798c6f61ee2SBarry Smith {
799c6f61ee2SBarry Smith   PetscDS      prob;
800c6f61ee2SBarry Smith   PetscObject  obj;
801c6f61ee2SBarry Smith   PetscClassId id;
802c6f61ee2SBarry Smith 
803c6f61ee2SBarry Smith   PetscFunctionBegin;
804c6f61ee2SBarry Smith   PetscCall(DMGetDS(adaptor->idm, &prob));
805c6f61ee2SBarry Smith   PetscCall(PetscDSGetDiscretization(prob, 0, &obj));
806c6f61ee2SBarry Smith   PetscCall(PetscObjectGetClassId(obj, &id));
807c6f61ee2SBarry Smith   if (id == PETSCFV_CLASSID) {
808c6f61ee2SBarry Smith     PetscFV fvm = (PetscFV)obj;
809c6f61ee2SBarry Smith 
810c6f61ee2SBarry Smith     PetscCall(PetscFVSetComputeGradients(fvm, adaptor->computeGradient));
811c6f61ee2SBarry Smith     /* Restore original limiter */
812c6f61ee2SBarry Smith     PetscCall(PetscFVSetLimiter(fvm, adaptor->limiter));
813c6f61ee2SBarry Smith 
814c6f61ee2SBarry Smith     PetscCall(VecRestoreArrayRead(adaptor->cellGeom, &adaptor->cellGeomArray));
815c6f61ee2SBarry Smith     PetscCall(VecRestoreArrayRead(adaptor->cellGrad, &adaptor->cellGradArray));
816c6f61ee2SBarry Smith     PetscCall(DMRestoreLocalVector(adaptor->gradDM, &adaptor->cellGrad));
817c6f61ee2SBarry Smith   }
818c6f61ee2SBarry Smith   PetscFunctionReturn(PETSC_SUCCESS);
819c6f61ee2SBarry Smith }
820c6f61ee2SBarry Smith 
821c6f61ee2SBarry Smith /*
8223a336bb1SMatthew G. Knepley   DMAdaptorComputeCellErrorIndicator_Gradient - Use the integrated gradient as an error indicator in the `DMAdaptor`
823c6f61ee2SBarry Smith 
824c6f61ee2SBarry Smith   Input Parameters:
825c6f61ee2SBarry Smith + adaptor  - The `DMAdaptor` object
826c6f61ee2SBarry Smith . dim      - The topological dimension
827c6f61ee2SBarry Smith . cell     - The cell
828c6f61ee2SBarry Smith . field    - The field integrated over the cell
829c6f61ee2SBarry Smith . gradient - The gradient integrated over the cell
830c6f61ee2SBarry Smith . cg       - A `PetscFVCellGeom` struct
831c6f61ee2SBarry Smith - ctx      - A user context
832c6f61ee2SBarry Smith 
833c6f61ee2SBarry Smith   Output Parameter:
834c6f61ee2SBarry Smith . errInd   - The error indicator
835c6f61ee2SBarry Smith 
836c6f61ee2SBarry Smith   Developer Note:
837c6f61ee2SBarry Smith   Some of the input arguments are absurdly specialized to special situations, it is not clear this is a good general API
838c6f61ee2SBarry Smith 
8393a336bb1SMatthew G. Knepley .seealso: [](ch_dmbase), `DMAdaptor`
840c6f61ee2SBarry Smith */
8413a336bb1SMatthew G. Knepley static PetscErrorCode DMAdaptorComputeCellErrorIndicator_Gradient(DMAdaptor adaptor, PetscInt dim, PetscInt Nc, const PetscScalar *field, const PetscScalar *gradient, const PetscFVCellGeom *cg, PetscReal *errInd, void *ctx)
842c6f61ee2SBarry Smith {
843c6f61ee2SBarry Smith   PetscReal err = 0.;
844c6f61ee2SBarry Smith   PetscInt  c, d;
845c6f61ee2SBarry Smith 
846c6f61ee2SBarry Smith   PetscFunctionBeginHot;
847c6f61ee2SBarry Smith   for (c = 0; c < Nc; c++) {
848c6f61ee2SBarry Smith     for (d = 0; d < dim; ++d) err += PetscSqr(PetscRealPart(gradient[c * dim + d]));
849c6f61ee2SBarry Smith   }
850c6f61ee2SBarry Smith   *errInd = cg->volume * err;
851c6f61ee2SBarry Smith   PetscFunctionReturn(PETSC_SUCCESS);
852c6f61ee2SBarry Smith }
853c6f61ee2SBarry Smith 
8543a336bb1SMatthew G. Knepley static PetscErrorCode DMAdaptorComputeErrorIndicator_Gradient(DMAdaptor adaptor, Vec locX, Vec errVec)
855c6f61ee2SBarry Smith {
8563a336bb1SMatthew G. Knepley   DM              dm, plex, edm, eplex;
8573a336bb1SMatthew G. Knepley   PetscDS         ds;
858c6f61ee2SBarry Smith   PetscObject     obj;
859c6f61ee2SBarry Smith   PetscClassId    id;
860c6f61ee2SBarry Smith   void           *ctx;
861c6f61ee2SBarry Smith   PetscQuadrature quad;
8623a336bb1SMatthew G. Knepley   PetscScalar    *earray;
8633a336bb1SMatthew G. Knepley   PetscReal       minMaxInd[2] = {PETSC_MAX_REAL, PETSC_MIN_REAL}, minMaxIndGlobal[2];
8643a336bb1SMatthew G. Knepley   PetscInt        dim, cdim, cStart, cEnd, Nf, Nc;
865c6f61ee2SBarry Smith 
866c6f61ee2SBarry Smith   PetscFunctionBegin;
8673a336bb1SMatthew G. Knepley   PetscCall(VecGetDM(locX, &dm));
8683a336bb1SMatthew G. Knepley   PetscCall(DMConvert(dm, DMPLEX, &plex));
8693a336bb1SMatthew G. Knepley   PetscCall(VecGetDM(errVec, &edm));
8703a336bb1SMatthew G. Knepley   PetscCall(DMConvert(edm, DMPLEX, &eplex));
871c6f61ee2SBarry Smith   PetscCall(DMGetDimension(plex, &dim));
872c6f61ee2SBarry Smith   PetscCall(DMGetCoordinateDim(plex, &cdim));
873c6f61ee2SBarry Smith   PetscCall(DMGetApplicationContext(plex, &ctx));
8743a336bb1SMatthew G. Knepley   PetscCall(DMGetDS(plex, &ds));
8753a336bb1SMatthew G. Knepley   PetscCall(PetscDSGetNumFields(ds, &Nf));
8763a336bb1SMatthew G. Knepley   PetscCall(PetscDSGetDiscretization(ds, 0, &obj));
877c6f61ee2SBarry Smith   PetscCall(PetscObjectGetClassId(obj, &id));
8783a336bb1SMatthew G. Knepley 
8793a336bb1SMatthew G. Knepley   PetscCall(VecGetArray(errVec, &earray));
8803a336bb1SMatthew G. Knepley   PetscCall(DMPlexGetSimplexOrBoxCells(plex, 0, &cStart, &cEnd));
8813a336bb1SMatthew G. Knepley   for (PetscInt cell = cStart; cell < cEnd; ++cell) {
8823a336bb1SMatthew G. Knepley     PetscScalar *eval;
8833a336bb1SMatthew G. Knepley     PetscReal    errInd = 0.;
8843a336bb1SMatthew G. Knepley 
885c6f61ee2SBarry Smith     if (id == PETSCFV_CLASSID) {
8863a336bb1SMatthew G. Knepley       PetscFV            fv = (PetscFV)obj;
887c6f61ee2SBarry Smith       const PetscScalar *pointSols;
888c6f61ee2SBarry Smith       const PetscScalar *pointSol;
889c6f61ee2SBarry Smith       const PetscScalar *pointGrad;
890c6f61ee2SBarry Smith       PetscFVCellGeom   *cg;
891c6f61ee2SBarry Smith 
8923a336bb1SMatthew G. Knepley       PetscCall(PetscFVGetNumComponents(fv, &Nc));
893c6f61ee2SBarry Smith       PetscCall(VecGetArrayRead(locX, &pointSols));
894c6f61ee2SBarry Smith       PetscCall(DMPlexPointLocalRead(plex, cell, pointSols, (void *)&pointSol));
895c6f61ee2SBarry Smith       PetscCall(DMPlexPointLocalRead(adaptor->gradDM, cell, adaptor->cellGradArray, (void *)&pointGrad));
896c6f61ee2SBarry Smith       PetscCall(DMPlexPointLocalRead(adaptor->cellDM, cell, adaptor->cellGeomArray, &cg));
8973a336bb1SMatthew G. Knepley       PetscUseTypeMethod(adaptor, computecellerrorindicator, dim, Nc, pointSol, pointGrad, cg, &errInd, ctx);
898c6f61ee2SBarry Smith       PetscCall(VecRestoreArrayRead(locX, &pointSols));
899c6f61ee2SBarry Smith     } else {
9003a336bb1SMatthew G. Knepley       PetscFE          fe = (PetscFE)obj;
901c6f61ee2SBarry Smith       PetscScalar     *x  = NULL, *field, *gradient, *interpolant, *interpolantGrad;
902c6f61ee2SBarry Smith       PetscFVCellGeom  cg;
903c6f61ee2SBarry Smith       PetscFEGeom      fegeom;
904c6f61ee2SBarry Smith       const PetscReal *quadWeights;
905c6f61ee2SBarry Smith       PetscReal       *coords;
9063a336bb1SMatthew G. Knepley       PetscInt         Nb, Nq, qNc;
907c6f61ee2SBarry Smith 
908c6f61ee2SBarry Smith       fegeom.dim      = dim;
909c6f61ee2SBarry Smith       fegeom.dimEmbed = cdim;
9103a336bb1SMatthew G. Knepley       PetscCall(PetscFEGetNumComponents(fe, &Nc));
9113a336bb1SMatthew G. Knepley       PetscCall(PetscFEGetQuadrature(fe, &quad));
9123a336bb1SMatthew G. Knepley       PetscCall(PetscFEGetDimension(fe, &Nb));
913c6f61ee2SBarry Smith       PetscCall(PetscQuadratureGetData(quad, NULL, &qNc, &Nq, NULL, &quadWeights));
914c6f61ee2SBarry Smith       PetscCall(PetscCalloc6(Nc, &field, cdim * Nc, &gradient, cdim * Nq, &coords, Nq, &fegeom.detJ, cdim * cdim * Nq, &fegeom.J, cdim * cdim * Nq, &fegeom.invJ));
915c6f61ee2SBarry Smith       PetscCall(PetscMalloc2(Nc, &interpolant, cdim * Nc, &interpolantGrad));
916c6f61ee2SBarry Smith       PetscCall(DMPlexComputeCellGeometryFEM(plex, cell, quad, coords, fegeom.J, fegeom.invJ, fegeom.detJ));
917c6f61ee2SBarry Smith       PetscCall(DMPlexComputeCellGeometryFVM(plex, cell, &cg.volume, NULL, NULL));
918c6f61ee2SBarry Smith       PetscCall(PetscArrayzero(gradient, cdim * Nc));
9193a336bb1SMatthew G. Knepley       PetscCall(DMPlexVecGetClosure(plex, NULL, locX, cell, NULL, &x));
9203a336bb1SMatthew G. Knepley       for (PetscInt f = 0; f < Nf; ++f) {
9213a336bb1SMatthew G. Knepley         PetscInt qc = 0;
922c6f61ee2SBarry Smith 
9233a336bb1SMatthew G. Knepley         PetscCall(PetscDSGetDiscretization(ds, f, &obj));
924c6f61ee2SBarry Smith         PetscCall(PetscArrayzero(interpolant, Nc));
925c6f61ee2SBarry Smith         PetscCall(PetscArrayzero(interpolantGrad, cdim * Nc));
9263a336bb1SMatthew G. Knepley         for (PetscInt q = 0; q < Nq; ++q) {
927c6f61ee2SBarry Smith           PetscCall(PetscFEInterpolateFieldAndGradient_Static((PetscFE)obj, 1, x, &fegeom, q, interpolant, interpolantGrad));
9283a336bb1SMatthew G. Knepley           for (PetscInt fc = 0; fc < Nc; ++fc) {
929c6f61ee2SBarry Smith             const PetscReal wt = quadWeights[q * qNc + qc + fc];
930c6f61ee2SBarry Smith 
931c6f61ee2SBarry Smith             field[fc] += interpolant[fc] * wt * fegeom.detJ[q];
9323a336bb1SMatthew G. Knepley             for (PetscInt d = 0; d < cdim; ++d) gradient[fc * cdim + d] += interpolantGrad[fc * dim + d] * wt * fegeom.detJ[q];
933c6f61ee2SBarry Smith           }
934c6f61ee2SBarry Smith         }
935c6f61ee2SBarry Smith         qc += Nc;
936c6f61ee2SBarry Smith       }
937c6f61ee2SBarry Smith       PetscCall(PetscFree2(interpolant, interpolantGrad));
938c6f61ee2SBarry Smith       PetscCall(DMPlexVecRestoreClosure(plex, NULL, locX, cell, NULL, &x));
9393a336bb1SMatthew G. Knepley       for (PetscInt fc = 0; fc < Nc; ++fc) {
940c6f61ee2SBarry Smith         field[fc] /= cg.volume;
9413a336bb1SMatthew G. Knepley         for (PetscInt d = 0; d < cdim; ++d) gradient[fc * cdim + d] /= cg.volume;
942c6f61ee2SBarry Smith       }
9433a336bb1SMatthew G. Knepley       PetscUseTypeMethod(adaptor, computecellerrorindicator, dim, Nc, field, gradient, &cg, &errInd, ctx);
944c6f61ee2SBarry Smith       PetscCall(PetscFree6(field, gradient, coords, fegeom.detJ, fegeom.J, fegeom.invJ));
945c6f61ee2SBarry Smith     }
9463a336bb1SMatthew G. Knepley     PetscCall(DMPlexPointGlobalRef(eplex, cell, earray, (void *)&eval));
9473a336bb1SMatthew G. Knepley     eval[0]      = errInd;
9483a336bb1SMatthew G. Knepley     minMaxInd[0] = PetscMin(minMaxInd[0], errInd);
9493a336bb1SMatthew G. Knepley     minMaxInd[1] = PetscMax(minMaxInd[1], errInd);
9503a336bb1SMatthew G. Knepley   }
9513a336bb1SMatthew G. Knepley   PetscCall(VecRestoreArray(errVec, &earray));
9523a336bb1SMatthew G. Knepley   PetscCall(DMDestroy(&plex));
9533a336bb1SMatthew G. Knepley   PetscCall(DMDestroy(&eplex));
9543a336bb1SMatthew G. Knepley   PetscCall(PetscGlobalMinMaxReal(PetscObjectComm((PetscObject)adaptor), minMaxInd, minMaxIndGlobal));
9553a336bb1SMatthew G. Knepley   PetscCall(PetscInfo(adaptor, "DMAdaptor: error indicator range (%g, %g)\n", (double)minMaxIndGlobal[0], (double)minMaxIndGlobal[1]));
9563a336bb1SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
9573a336bb1SMatthew G. Knepley }
9583a336bb1SMatthew G. Knepley 
9593a336bb1SMatthew G. Knepley static PetscErrorCode DMAdaptorComputeErrorIndicator_Flux(DMAdaptor adaptor, Vec lu, Vec errVec)
9603a336bb1SMatthew G. Knepley {
9613a336bb1SMatthew G. Knepley   DM          dm, mdm;
9623a336bb1SMatthew G. Knepley   SNES        msnes;
9633a336bb1SMatthew G. Knepley   Vec         mu, lmu;
9643a336bb1SMatthew G. Knepley   void       *ctx;
965e03fd340SMatthew G. Knepley   const char *prefix;
9663a336bb1SMatthew G. Knepley 
9673a336bb1SMatthew G. Knepley   PetscFunctionBegin;
9683a336bb1SMatthew G. Knepley   PetscCall(VecGetDM(lu, &dm));
9693a336bb1SMatthew G. Knepley 
9703a336bb1SMatthew G. Knepley   // Set up and solve mixed problem
9713a336bb1SMatthew G. Knepley   PetscCall(DMClone(dm, &mdm));
9723a336bb1SMatthew G. Knepley   PetscCall(SNESCreate(PetscObjectComm((PetscObject)mdm), &msnes));
9733a336bb1SMatthew G. Knepley   PetscCall(SNESSetDM(msnes, mdm));
974e03fd340SMatthew G. Knepley   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)adaptor, &prefix));
975e03fd340SMatthew G. Knepley   PetscCall(SNESSetOptionsPrefix(msnes, prefix));
9763a336bb1SMatthew G. Knepley   PetscCall(SNESAppendOptionsPrefix(msnes, "mixed_"));
9773a336bb1SMatthew G. Knepley 
9783a336bb1SMatthew G. Knepley   PetscTryTypeMethod(adaptor, mixedsetup, mdm);
9793a336bb1SMatthew G. Knepley   PetscCall(DMGetApplicationContext(dm, &ctx));
9803a336bb1SMatthew G. Knepley   PetscCall(DMPlexSetSNESLocalFEM(mdm, PETSC_FALSE, ctx));
9813a336bb1SMatthew G. Knepley   PetscCall(SNESSetFromOptions(msnes));
9823a336bb1SMatthew G. Knepley 
9833a336bb1SMatthew G. Knepley   PetscCall(DMCreateGlobalVector(mdm, &mu));
9843a336bb1SMatthew G. Knepley   PetscCall(PetscObjectSetName((PetscObject)mu, "Mixed Solution"));
9853a336bb1SMatthew G. Knepley   PetscCall(VecSet(mu, 0.0));
9863a336bb1SMatthew G. Knepley   PetscCall(SNESSolve(msnes, NULL, mu));
9873a336bb1SMatthew G. Knepley   PetscCall(VecViewFromOptions(mu, (PetscObject)adaptor, "-adapt_mixed_sol_vec_view"));
9883a336bb1SMatthew G. Knepley 
9893a336bb1SMatthew G. Knepley   PetscCall(DMGetLocalVector(mdm, &lmu));
9903a336bb1SMatthew G. Knepley   PetscCall(DMGlobalToLocal(mdm, mu, INSERT_VALUES, lmu));
9913a336bb1SMatthew G. Knepley   PetscCall(DMPlexInsertBoundaryValues(mdm, PETSC_TRUE, lmu, 0.0, NULL, NULL, NULL));
9923a336bb1SMatthew G. Knepley   PetscCall(DMPlexComputeL2FluxDiffVecLocal(lu, 0, lmu, 0, errVec));
9933a336bb1SMatthew G. Knepley   PetscCall(DMRestoreLocalVector(mdm, &lmu));
9943a336bb1SMatthew G. Knepley   PetscCall(VecDestroy(&mu));
9953a336bb1SMatthew G. Knepley   PetscCall(SNESDestroy(&msnes));
9963a336bb1SMatthew G. Knepley   PetscCall(DMDestroy(&mdm));
997c6f61ee2SBarry Smith   PetscFunctionReturn(PETSC_SUCCESS);
998c6f61ee2SBarry Smith }
999c6f61ee2SBarry Smith 
10008b724c91SMatthew G. Knepley /*@
10018b724c91SMatthew G. Knepley   DMAdaptorMonitor - runs the user provided monitor routines, if they exist
10028b724c91SMatthew G. Knepley 
10038b724c91SMatthew G. Knepley   Collective
10048b724c91SMatthew G. Knepley 
10058b724c91SMatthew G. Knepley   Input Parameters:
10068b724c91SMatthew G. Knepley + adaptor - the `DMAdaptor`
10078b724c91SMatthew G. Knepley . it      - iteration number
10088b724c91SMatthew G. Knepley . odm     - the original `DM`
10098b724c91SMatthew G. Knepley . adm     - the adapted `DM`
10108b724c91SMatthew G. Knepley . Nf      - the number of fields
10118b724c91SMatthew G. Knepley . enorms  - the 2-norm error values for each field
10128b724c91SMatthew G. Knepley - error   - `Vec` of cellwise errors
10138b724c91SMatthew G. Knepley 
10148b724c91SMatthew G. Knepley   Level: developer
10158b724c91SMatthew G. Knepley 
10168b724c91SMatthew G. Knepley   Note:
10178b724c91SMatthew G. Knepley   This routine is called by the `DMAdaptor` implementations.
10188b724c91SMatthew G. Knepley   It does not typically need to be called by the user.
10198b724c91SMatthew G. Knepley 
10208b724c91SMatthew G. Knepley .seealso: [](ch_snes), `DMAdaptorMonitorSet()`
10218b724c91SMatthew G. Knepley @*/
10228b724c91SMatthew G. Knepley PetscErrorCode DMAdaptorMonitor(DMAdaptor adaptor, PetscInt it, DM odm, DM adm, PetscInt Nf, PetscReal enorms[], Vec error)
10238b724c91SMatthew G. Knepley {
10248b724c91SMatthew G. Knepley   PetscFunctionBegin;
10258b724c91SMatthew G. Knepley   for (PetscInt i = 0; i < adaptor->numbermonitors; ++i) PetscCall((*adaptor->monitor[i])(adaptor, it, odm, adm, Nf, enorms, error, adaptor->monitorcontext[i]));
10268b724c91SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
10278b724c91SMatthew G. Knepley }
10288b724c91SMatthew G. Knepley 
10298b724c91SMatthew G. Knepley /*@C
10308b724c91SMatthew G. Knepley   DMAdaptorMonitorSize - Prints the mesh sizes at each iteration of an adaptation loop.
10318b724c91SMatthew G. Knepley 
10328b724c91SMatthew G. Knepley   Collective
10338b724c91SMatthew G. Knepley 
10348b724c91SMatthew G. Knepley   Input Parameters:
10358b724c91SMatthew G. Knepley + adaptor - the `DMAdaptor`
10368b724c91SMatthew G. Knepley . n       - iteration number
10378b724c91SMatthew G. Knepley . odm     - the original `DM`
10388b724c91SMatthew G. Knepley . adm     - the adapted `DM`
10398b724c91SMatthew G. Knepley . Nf      - number of fields
10408b724c91SMatthew G. Knepley . enorms  - 2-norm error values for each field (may be estimated).
10418b724c91SMatthew G. Knepley . error   - `Vec` of cellwise errors
10428b724c91SMatthew G. Knepley - vf      - The viewer context
10438b724c91SMatthew G. Knepley 
10448b724c91SMatthew G. Knepley   Options Database Key:
10458b724c91SMatthew G. Knepley . -adaptor_monitor_size - Activates `DMAdaptorMonitorSize()`
10468b724c91SMatthew G. Knepley 
10478b724c91SMatthew G. Knepley   Level: intermediate
10488b724c91SMatthew G. Knepley 
10498b724c91SMatthew G. Knepley   Note:
10508b724c91SMatthew G. Knepley   This is not called directly by users, rather one calls `DMAdaptorMonitorSet()`, with this function as an argument, to cause the monitor
10518b724c91SMatthew G. Knepley   to be used during the adaptation loop.
10528b724c91SMatthew G. Knepley 
10538b724c91SMatthew G. Knepley .seealso: [](ch_snes), `DMAdaptor`, `DMAdaptorMonitorSet()`, `DMAdaptorMonitorError()`, `DMAdaptorMonitorErrorDraw()`, `DMAdaptorMonitorErrorDrawLG()`
10548b724c91SMatthew G. Knepley @*/
10558b724c91SMatthew G. Knepley PetscErrorCode DMAdaptorMonitorSize(DMAdaptor adaptor, PetscInt n, DM odm, DM adm, PetscInt Nf, PetscReal enorms[], Vec error, PetscViewerAndFormat *vf)
10568b724c91SMatthew G. Knepley {
10578b724c91SMatthew G. Knepley   PetscViewer       viewer = vf->viewer;
10588b724c91SMatthew G. Knepley   PetscViewerFormat format = vf->format;
10598b724c91SMatthew G. Knepley   PetscInt          tablevel, cStart, cEnd, acStart, acEnd;
10608b724c91SMatthew G. Knepley   const char       *prefix;
10618b724c91SMatthew G. Knepley   PetscMPIInt       rank;
10628b724c91SMatthew G. Knepley 
10638b724c91SMatthew G. Knepley   PetscFunctionBegin;
10648b724c91SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 8);
10658b724c91SMatthew G. Knepley   PetscCall(PetscObjectGetTabLevel((PetscObject)adaptor, &tablevel));
10668b724c91SMatthew G. Knepley   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)adaptor, &prefix));
10678b724c91SMatthew G. Knepley   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)adaptor), &rank));
10688b724c91SMatthew G. Knepley   PetscCall(DMPlexGetHeightStratum(odm, 0, &cStart, &cEnd));
10698b724c91SMatthew G. Knepley   PetscCall(DMPlexGetHeightStratum(adm, 0, &acStart, &acEnd));
10708b724c91SMatthew G. Knepley 
10718b724c91SMatthew G. Knepley   PetscCall(PetscViewerPushFormat(viewer, format));
10728b724c91SMatthew G. Knepley   PetscCall(PetscViewerASCIIAddTab(viewer, tablevel));
10738b724c91SMatthew G. Knepley   if (n == 0 && prefix) PetscCall(PetscViewerASCIIPrintf(viewer, "  Sizes for %s adaptation.\n", prefix));
10748b724c91SMatthew G. Knepley   PetscCall(PetscViewerASCIIPrintf(viewer, "%3" PetscInt_FMT " DMAdaptor rank %d N_orig: %" PetscInt_FMT " N_adapt: %" PetscInt_FMT "\n", n, rank, cEnd - cStart, acEnd - acStart));
10758b724c91SMatthew G. Knepley   PetscCall(PetscViewerASCIISubtractTab(viewer, tablevel));
10768b724c91SMatthew G. Knepley   PetscCall(PetscViewerPopFormat(viewer));
10778b724c91SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
10788b724c91SMatthew G. Knepley }
10798b724c91SMatthew G. Knepley 
10808b724c91SMatthew G. Knepley /*@C
10818b724c91SMatthew G. Knepley   DMAdaptorMonitorError - Prints the error norm at each iteration of an adaptation loop.
10828b724c91SMatthew G. Knepley 
10838b724c91SMatthew G. Knepley   Collective
10848b724c91SMatthew G. Knepley 
10858b724c91SMatthew G. Knepley   Input Parameters:
10868b724c91SMatthew G. Knepley + adaptor - the `DMAdaptor`
10878b724c91SMatthew G. Knepley . n       - iteration number
10888b724c91SMatthew G. Knepley . odm     - the original `DM`
10898b724c91SMatthew G. Knepley . adm     - the adapted `DM`
10908b724c91SMatthew G. Knepley . Nf      - number of fields
10918b724c91SMatthew G. Knepley . enorms  - 2-norm error values for each field (may be estimated).
10928b724c91SMatthew G. Knepley . error   - `Vec` of cellwise errors
10938b724c91SMatthew G. Knepley - vf      - The viewer context
10948b724c91SMatthew G. Knepley 
10958b724c91SMatthew G. Knepley   Options Database Key:
10968b724c91SMatthew G. Knepley . -adaptor_monitor_error - Activates `DMAdaptorMonitorError()`
10978b724c91SMatthew G. Knepley 
10988b724c91SMatthew G. Knepley   Level: intermediate
10998b724c91SMatthew G. Knepley 
11008b724c91SMatthew G. Knepley   Note:
11018b724c91SMatthew G. Knepley   This is not called directly by users, rather one calls `DMAdaptorMonitorSet()`, with this function as an argument, to cause the monitor
11028b724c91SMatthew G. Knepley   to be used during the adaptation loop.
11038b724c91SMatthew G. Knepley 
11048b724c91SMatthew G. Knepley .seealso: [](ch_snes), `DMAdaptor`, `DMAdaptorMonitorSet()`, `DMAdaptorMonitorErrorDraw()`, `DMAdaptorMonitorErrorDrawLG()`
11058b724c91SMatthew G. Knepley @*/
11068b724c91SMatthew G. Knepley PetscErrorCode DMAdaptorMonitorError(DMAdaptor adaptor, PetscInt n, DM odm, DM adm, PetscInt Nf, PetscReal enorms[], Vec error, PetscViewerAndFormat *vf)
11078b724c91SMatthew G. Knepley {
11088b724c91SMatthew G. Knepley   PetscViewer       viewer = vf->viewer;
11098b724c91SMatthew G. Knepley   PetscViewerFormat format = vf->format;
11108b724c91SMatthew G. Knepley   PetscInt          tablevel, cStart, cEnd, acStart, acEnd;
11118b724c91SMatthew G. Knepley   const char       *prefix;
11128b724c91SMatthew G. Knepley 
11138b724c91SMatthew G. Knepley   PetscFunctionBegin;
11148b724c91SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 8);
11158b724c91SMatthew G. Knepley   PetscCall(PetscObjectGetTabLevel((PetscObject)adaptor, &tablevel));
11168b724c91SMatthew G. Knepley   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)adaptor, &prefix));
11178b724c91SMatthew G. Knepley 
11188b724c91SMatthew G. Knepley   PetscCall(PetscViewerPushFormat(viewer, format));
11198b724c91SMatthew G. Knepley   PetscCall(PetscViewerASCIIAddTab(viewer, tablevel));
11208b724c91SMatthew G. Knepley   if (n == 0 && prefix) PetscCall(PetscViewerASCIIPrintf(viewer, "  Error norms for %s adaptation.\n", prefix));
11218b724c91SMatthew G. Knepley   PetscCall(PetscViewerASCIIPrintf(viewer, "%3" PetscInt_FMT " DMAdaptor Error norm %s", n, Nf > 1 ? "[" : ""));
11228b724c91SMatthew G. Knepley   PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_FALSE));
11238b724c91SMatthew G. Knepley   for (PetscInt f = 0; f < Nf; ++f) {
11248b724c91SMatthew G. Knepley     if (f > 0) PetscCall(PetscViewerASCIIPrintf(viewer, ", "));
11258b724c91SMatthew G. Knepley     PetscCall(PetscViewerASCIIPrintf(viewer, "%14.12e", (double)enorms[f]));
11268b724c91SMatthew G. Knepley   }
11278b724c91SMatthew G. Knepley   PetscCall(DMPlexGetHeightStratum(odm, 0, &cStart, &cEnd));
11288b724c91SMatthew G. Knepley   PetscCall(DMPlexGetHeightStratum(adm, 0, &acStart, &acEnd));
11298b724c91SMatthew G. Knepley   PetscCall(PetscViewerASCIIPrintf(viewer, " N: %" PetscInt_FMT " Nadapt: %" PetscInt_FMT "\n", cEnd - cStart, acEnd - acStart));
11308b724c91SMatthew G. Knepley   PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_TRUE));
11318b724c91SMatthew G. Knepley   PetscCall(PetscViewerASCIISubtractTab(viewer, tablevel));
11328b724c91SMatthew G. Knepley   PetscCall(PetscViewerPopFormat(viewer));
11338b724c91SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
11348b724c91SMatthew G. Knepley }
11358b724c91SMatthew G. Knepley 
11368b724c91SMatthew G. Knepley /*@C
11378b724c91SMatthew G. Knepley   DMAdaptorMonitorErrorDraw - Plots the error at each iteration of an iterative solver.
11388b724c91SMatthew G. Knepley 
11398b724c91SMatthew G. Knepley   Collective
11408b724c91SMatthew G. Knepley 
11418b724c91SMatthew G. Knepley   Input Parameters:
11428b724c91SMatthew G. Knepley + adaptor - the `DMAdaptor`
11438b724c91SMatthew G. Knepley . n       - iteration number
11448b724c91SMatthew G. Knepley . odm     - the original `DM`
11458b724c91SMatthew G. Knepley . adm     - the adapted `DM`
11468b724c91SMatthew G. Knepley . Nf      - number of fields
11478b724c91SMatthew G. Knepley . enorms  - 2-norm error values for each field (may be estimated).
11488b724c91SMatthew G. Knepley . error   - `Vec` of cellwise errors
11498b724c91SMatthew G. Knepley - vf      - The viewer context
11508b724c91SMatthew G. Knepley 
11518b724c91SMatthew G. Knepley   Options Database Key:
11528b724c91SMatthew G. Knepley . -adaptor_monitor_error draw - Activates `DMAdaptorMonitorErrorDraw()`
11538b724c91SMatthew G. Knepley 
11548b724c91SMatthew G. Knepley   Level: intermediate
11558b724c91SMatthew G. Knepley 
11568b724c91SMatthew G. Knepley   Note:
11578b724c91SMatthew G. Knepley   This is not called directly by users, rather one calls `DMAdaptorMonitorSet()`, with this function as an argument, to cause the monitor
11588b724c91SMatthew G. Knepley   to be used during the adaptation loop.
11598b724c91SMatthew G. Knepley 
11608b724c91SMatthew G. Knepley .seealso: [](ch_snes), `PETSCVIEWERDRAW`, `DMAdaptor`, `DMAdaptorMonitorSet()`, `DMAdaptorMonitorErrorDrawLG()`
11618b724c91SMatthew G. Knepley @*/
11628b724c91SMatthew G. Knepley PetscErrorCode DMAdaptorMonitorErrorDraw(DMAdaptor adaptor, PetscInt n, DM odm, DM adm, PetscInt Nf, PetscReal enorms[], Vec error, PetscViewerAndFormat *vf)
11638b724c91SMatthew G. Knepley {
11648b724c91SMatthew G. Knepley   PetscViewer       viewer = vf->viewer;
11658b724c91SMatthew G. Knepley   PetscViewerFormat format = vf->format;
11668b724c91SMatthew G. Knepley 
11678b724c91SMatthew G. Knepley   PetscFunctionBegin;
11688b724c91SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 8);
11698b724c91SMatthew G. Knepley   PetscCall(PetscViewerPushFormat(viewer, format));
11708b724c91SMatthew G. Knepley   PetscCall(PetscObjectSetName((PetscObject)error, "Error Estimator"));
11718b724c91SMatthew G. Knepley   PetscCall(PetscObjectCompose((PetscObject)error, "__Vec_bc_zero__", (PetscObject)adaptor));
11728b724c91SMatthew G. Knepley   PetscCall(VecView(error, viewer));
11738b724c91SMatthew G. Knepley   PetscCall(PetscObjectCompose((PetscObject)error, "__Vec_bc_zero__", NULL));
11748b724c91SMatthew G. Knepley   PetscCall(PetscViewerPopFormat(viewer));
11758b724c91SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
11768b724c91SMatthew G. Knepley }
11778b724c91SMatthew G. Knepley 
11788b724c91SMatthew G. Knepley /*@C
1179d7c1f440SPierre Jolivet   DMAdaptorMonitorErrorDrawLGCreate - Creates the context for the error plotter `DMAdaptorMonitorErrorDrawLG()`
11808b724c91SMatthew G. Knepley 
11818b724c91SMatthew G. Knepley   Collective
11828b724c91SMatthew G. Knepley 
11838b724c91SMatthew G. Knepley   Input Parameters:
11848b724c91SMatthew G. Knepley + viewer - The `PetscViewer`
11858b724c91SMatthew G. Knepley . format - The viewer format
11868b724c91SMatthew G. Knepley - ctx    - An optional user context
11878b724c91SMatthew G. Knepley 
11888b724c91SMatthew G. Knepley   Output Parameter:
11898b724c91SMatthew G. Knepley . vf - The viewer context
11908b724c91SMatthew G. Knepley 
11918b724c91SMatthew G. Knepley   Level: intermediate
11928b724c91SMatthew G. Knepley 
11938b724c91SMatthew G. Knepley .seealso: [](ch_snes), `PETSCVIEWERDRAW`, `DMAdaptor`, `DMAdaptorMonitorSet()`, `DMAdaptorMonitorErrorDrawLG()`
11948b724c91SMatthew G. Knepley @*/
11958b724c91SMatthew G. Knepley PetscErrorCode DMAdaptorMonitorErrorDrawLGCreate(PetscViewer viewer, PetscViewerFormat format, void *ctx, PetscViewerAndFormat **vf)
11968b724c91SMatthew G. Knepley {
11978b724c91SMatthew G. Knepley   DMAdaptor adaptor = (DMAdaptor)ctx;
11988b724c91SMatthew G. Knepley   char    **names;
11998b724c91SMatthew G. Knepley   PetscInt  Nf;
12008b724c91SMatthew G. Knepley 
12018b724c91SMatthew G. Knepley   PetscFunctionBegin;
12028b724c91SMatthew G. Knepley   PetscCall(DMGetNumFields(adaptor->idm, &Nf));
12038b724c91SMatthew G. Knepley   PetscCall(PetscMalloc1(Nf + 1, &names));
12048b724c91SMatthew G. Knepley   for (PetscInt f = 0; f < Nf; ++f) {
12058b724c91SMatthew G. Knepley     PetscObject disc;
12068b724c91SMatthew G. Knepley     const char *fname;
12078b724c91SMatthew G. Knepley     char        lname[PETSC_MAX_PATH_LEN];
12088b724c91SMatthew G. Knepley 
12098b724c91SMatthew G. Knepley     PetscCall(DMGetField(adaptor->idm, f, NULL, &disc));
12108b724c91SMatthew G. Knepley     PetscCall(PetscObjectGetName(disc, &fname));
12118b724c91SMatthew G. Knepley     PetscCall(PetscStrncpy(lname, fname, PETSC_MAX_PATH_LEN));
12128b724c91SMatthew G. Knepley     PetscCall(PetscStrlcat(lname, " Error", PETSC_MAX_PATH_LEN));
12138b724c91SMatthew G. Knepley     PetscCall(PetscStrallocpy(lname, &names[f]));
12148b724c91SMatthew G. Knepley   }
12158b724c91SMatthew G. Knepley   PetscCall(PetscViewerAndFormatCreate(viewer, format, vf));
12168b724c91SMatthew G. Knepley   (*vf)->data = ctx;
12178b724c91SMatthew G. Knepley   PetscCall(KSPMonitorLGCreate(PetscObjectComm((PetscObject)viewer), NULL, NULL, "Log Error Norm", Nf, (const char **)names, PETSC_DECIDE, PETSC_DECIDE, 400, 300, &(*vf)->lg));
12188b724c91SMatthew G. Knepley   for (PetscInt f = 0; f < Nf; ++f) PetscCall(PetscFree(names[f]));
12198b724c91SMatthew G. Knepley   PetscCall(PetscFree(names));
12208b724c91SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
12218b724c91SMatthew G. Knepley }
12228b724c91SMatthew G. Knepley 
12238b724c91SMatthew G. Knepley /*@C
12248b724c91SMatthew G. Knepley   DMAdaptorMonitorErrorDrawLG - Plots the error norm at each iteration of an adaptive loop.
12258b724c91SMatthew G. Knepley 
12268b724c91SMatthew G. Knepley   Collective
12278b724c91SMatthew G. Knepley 
12288b724c91SMatthew G. Knepley   Input Parameters:
12298b724c91SMatthew G. Knepley + adaptor - the `DMAdaptor`
12308b724c91SMatthew G. Knepley . n       - iteration number
12318b724c91SMatthew G. Knepley . odm     - the original `DM`
12328b724c91SMatthew G. Knepley . adm     - the adapted `DM`
12338b724c91SMatthew G. Knepley . Nf      - number of fields
12348b724c91SMatthew G. Knepley . enorms  - 2-norm error values for each field (may be estimated).
12358b724c91SMatthew G. Knepley . error   - `Vec` of cellwise errors
12368b724c91SMatthew G. Knepley - vf      - The viewer context
12378b724c91SMatthew G. Knepley 
12388b724c91SMatthew G. Knepley   Options Database Key:
12398b724c91SMatthew G. Knepley . -adaptor_error draw::draw_lg - Activates `DMAdaptorMonitorErrorDrawLG()`
12408b724c91SMatthew G. Knepley 
12418b724c91SMatthew G. Knepley   Level: intermediate
12428b724c91SMatthew G. Knepley 
12438b724c91SMatthew G. Knepley   Notes:
12448b724c91SMatthew G. Knepley   This is not called directly by users, rather one calls `DMAdaptorMonitorSet()`, with this function as an argument, to cause the monitor
12458b724c91SMatthew G. Knepley   to be used during the adaptation loop.
12468b724c91SMatthew G. Knepley 
12478b724c91SMatthew G. Knepley   Call `DMAdaptorMonitorErrorDrawLGCreate()` to create the context needed for this monitor
12488b724c91SMatthew G. Knepley 
12498b724c91SMatthew G. Knepley .seealso: [](ch_snes), `PETSCVIEWERDRAW`, `DMAdaptor`, `DMAdaptorMonitorSet()`, `DMAdadptorMonitorErrorDraw()`, `DMAdadptorMonitorError()`,
12508b724c91SMatthew G. Knepley           `DMAdaptorMonitorTrueResidualDrawLGCreate()`
12518b724c91SMatthew G. Knepley @*/
12528b724c91SMatthew G. Knepley PetscErrorCode DMAdaptorMonitorErrorDrawLG(DMAdaptor adaptor, PetscInt n, DM odm, DM adm, PetscInt Nf, PetscReal enorms[], Vec error, PetscViewerAndFormat *vf)
12538b724c91SMatthew G. Knepley {
12548b724c91SMatthew G. Knepley   PetscViewer       viewer = vf->viewer;
12558b724c91SMatthew G. Knepley   PetscViewerFormat format = vf->format;
12568b724c91SMatthew G. Knepley   PetscDrawLG       lg     = vf->lg;
12578b724c91SMatthew G. Knepley   PetscReal        *x, *e;
12588b724c91SMatthew G. Knepley 
12598b724c91SMatthew G. Knepley   PetscFunctionBegin;
12608b724c91SMatthew G. Knepley   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 8);
12618b724c91SMatthew G. Knepley   PetscValidHeaderSpecific(lg, PETSC_DRAWLG_CLASSID, 8);
12628b724c91SMatthew G. Knepley   PetscCall(PetscCalloc2(Nf, &x, Nf, &e));
12638b724c91SMatthew G. Knepley   PetscCall(PetscViewerPushFormat(viewer, format));
12648b724c91SMatthew G. Knepley   if (!n) PetscCall(PetscDrawLGReset(lg));
12658b724c91SMatthew G. Knepley   for (PetscInt f = 0; f < Nf; ++f) {
12668b724c91SMatthew G. Knepley     x[f] = (PetscReal)n;
12678b724c91SMatthew G. Knepley     e[f] = enorms[f] > 0.0 ? PetscLog10Real(enorms[f]) : -15.;
12688b724c91SMatthew G. Knepley   }
12698b724c91SMatthew G. Knepley   PetscCall(PetscDrawLGAddPoint(lg, x, e));
12708b724c91SMatthew G. Knepley   PetscCall(PetscDrawLGDraw(lg));
12718b724c91SMatthew G. Knepley   PetscCall(PetscDrawLGSave(lg));
12728b724c91SMatthew G. Knepley   PetscCall(PetscViewerPopFormat(viewer));
12738b724c91SMatthew G. Knepley   PetscCall(PetscFree2(x, e));
12748b724c91SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
12758b724c91SMatthew G. Knepley }
12768b724c91SMatthew G. Knepley 
12778b724c91SMatthew G. Knepley /*@C
12788b724c91SMatthew G. Knepley   DMAdaptorMonitorRegisterAll - Registers all of the mesh adaptation monitors in the `SNES` package.
12798b724c91SMatthew G. Knepley 
12808b724c91SMatthew G. Knepley   Not Collective
12818b724c91SMatthew G. Knepley 
12828b724c91SMatthew G. Knepley   Level: advanced
12838b724c91SMatthew G. Knepley 
12848b724c91SMatthew G. Knepley .seealso: [](ch_snes), `SNES`, `DM`, `DMAdaptorMonitorRegister()`, `DMAdaptorRegister()`
12858b724c91SMatthew G. Knepley @*/
12868b724c91SMatthew G. Knepley PetscErrorCode DMAdaptorMonitorRegisterAll(void)
12878b724c91SMatthew G. Knepley {
12888b724c91SMatthew G. Knepley   PetscFunctionBegin;
12898b724c91SMatthew G. Knepley   if (DMAdaptorMonitorRegisterAllCalled) PetscFunctionReturn(PETSC_SUCCESS);
12908b724c91SMatthew G. Knepley   DMAdaptorMonitorRegisterAllCalled = PETSC_TRUE;
12918b724c91SMatthew G. Knepley 
12928b724c91SMatthew G. Knepley   PetscCall(DMAdaptorMonitorRegister("size", PETSCVIEWERASCII, PETSC_VIEWER_DEFAULT, DMAdaptorMonitorSize, NULL, NULL));
12938b724c91SMatthew G. Knepley   PetscCall(DMAdaptorMonitorRegister("error", PETSCVIEWERASCII, PETSC_VIEWER_DEFAULT, DMAdaptorMonitorError, NULL, NULL));
12948b724c91SMatthew G. Knepley   PetscCall(DMAdaptorMonitorRegister("error", PETSCVIEWERDRAW, PETSC_VIEWER_DEFAULT, DMAdaptorMonitorErrorDraw, NULL, NULL));
12958b724c91SMatthew G. Knepley   PetscCall(DMAdaptorMonitorRegister("error", PETSCVIEWERDRAW, PETSC_VIEWER_DRAW_LG, DMAdaptorMonitorErrorDrawLG, DMAdaptorMonitorErrorDrawLGCreate, NULL));
12968b724c91SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
12978b724c91SMatthew G. Knepley }
12988b724c91SMatthew G. Knepley 
12998b724c91SMatthew G. Knepley static void identity(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[])
13008b724c91SMatthew G. Knepley {
13018b724c91SMatthew G. Knepley   const PetscInt Nc = uOff[1] - uOff[0];
13028b724c91SMatthew G. Knepley 
13038b724c91SMatthew G. Knepley   for (PetscInt i = 0; i < Nc; ++i) f[i] = u[i];
13048b724c91SMatthew G. Knepley }
13058b724c91SMatthew G. Knepley 
1306c6f61ee2SBarry Smith static void identityFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[])
1307c6f61ee2SBarry Smith {
13088b724c91SMatthew G. Knepley   for (PetscInt i = 0; i < dim; ++i) {
13098b724c91SMatthew G. Knepley     for (PetscInt j = 0; j < dim; ++j) f[i + dim * j] = u[i + dim * j];
1310c6f61ee2SBarry Smith   }
1311c6f61ee2SBarry Smith }
1312c6f61ee2SBarry Smith 
1313c6f61ee2SBarry Smith static PetscErrorCode DMAdaptorAdapt_Sequence_Private(DMAdaptor adaptor, Vec inx, PetscBool doSolve, DM *adm, Vec *ax)
1314c6f61ee2SBarry Smith {
13153a336bb1SMatthew G. Knepley   PetscDS   ds;
13168b724c91SMatthew G. Knepley   PetscReal errorNorm = 0.;
1317c6f61ee2SBarry Smith   PetscInt  numAdapt  = adaptor->numSeq, adaptIter;
13183a336bb1SMatthew G. Knepley   PetscInt  dim, coordDim, Nf;
13198b724c91SMatthew G. Knepley   void     *ctx;
13208b724c91SMatthew G. Knepley   MPI_Comm  comm;
1321c6f61ee2SBarry Smith 
1322c6f61ee2SBarry Smith   PetscFunctionBegin;
1323c6f61ee2SBarry Smith   PetscCall(DMViewFromOptions(adaptor->idm, NULL, "-dm_adapt_pre_view"));
1324c6f61ee2SBarry Smith   PetscCall(VecViewFromOptions(inx, NULL, "-sol_adapt_pre_view"));
1325c6f61ee2SBarry Smith   PetscCall(PetscObjectGetComm((PetscObject)adaptor, &comm));
1326c6f61ee2SBarry Smith   PetscCall(DMGetDimension(adaptor->idm, &dim));
1327c6f61ee2SBarry Smith   PetscCall(DMGetCoordinateDim(adaptor->idm, &coordDim));
1328c6f61ee2SBarry Smith   PetscCall(DMGetApplicationContext(adaptor->idm, &ctx));
13293a336bb1SMatthew G. Knepley   PetscCall(DMGetDS(adaptor->idm, &ds));
13303a336bb1SMatthew G. Knepley   PetscCall(PetscDSGetNumFields(ds, &Nf));
13313a336bb1SMatthew G. Knepley   PetscCheck(Nf != 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot refine with no fields present!");
1332c6f61ee2SBarry Smith 
1333c6f61ee2SBarry Smith   /* Adapt until nothing changes */
1334c6f61ee2SBarry Smith   /* Adapt for a specified number of iterates */
1335c6f61ee2SBarry Smith   for (adaptIter = 0; adaptIter < numAdapt - 1; ++adaptIter) PetscCall(PetscViewerASCIIPushTab(PETSC_VIEWER_STDOUT_(comm)));
1336c6f61ee2SBarry Smith   for (adaptIter = 0; adaptIter < numAdapt; ++adaptIter) {
1337c6f61ee2SBarry Smith     PetscBool adapted = PETSC_FALSE;
1338c6f61ee2SBarry Smith     DM        dm      = adaptIter ? *adm : adaptor->idm, odm;
1339c6f61ee2SBarry Smith     Vec       x       = adaptIter ? *ax : inx, locX, ox;
13408b724c91SMatthew G. Knepley     Vec       error   = NULL;
1341c6f61ee2SBarry Smith 
1342c6f61ee2SBarry Smith     PetscCall(DMGetLocalVector(dm, &locX));
1343c6f61ee2SBarry Smith     PetscCall(DMAdaptorPreAdapt(adaptor, locX));
1344c6f61ee2SBarry Smith     if (doSolve) {
1345c6f61ee2SBarry Smith       SNES snes;
1346c6f61ee2SBarry Smith 
1347c6f61ee2SBarry Smith       PetscCall(DMAdaptorGetSolver(adaptor, &snes));
1348c6f61ee2SBarry Smith       PetscCall(SNESSolve(snes, NULL, adaptIter ? *ax : x));
1349c6f61ee2SBarry Smith     }
13503a336bb1SMatthew G. Knepley     PetscCall(DMGlobalToLocalBegin(dm, adaptIter ? *ax : x, INSERT_VALUES, locX));
13513a336bb1SMatthew G. Knepley     PetscCall(DMGlobalToLocalEnd(dm, adaptIter ? *ax : x, INSERT_VALUES, locX));
13523a336bb1SMatthew G. Knepley     PetscCall(VecViewFromOptions(adaptIter ? *ax : x, (PetscObject)adaptor, "-adapt_primal_sol_vec_view"));
1353c6f61ee2SBarry Smith     switch (adaptor->adaptCriterion) {
1354c6f61ee2SBarry Smith     case DM_ADAPTATION_REFINE:
1355c6f61ee2SBarry Smith       PetscCall(DMRefine(dm, comm, &odm));
1356c6f61ee2SBarry Smith       PetscCheck(odm, comm, PETSC_ERR_ARG_INCOMP, "DMRefine() did not perform any refinement, cannot continue grid sequencing");
1357c6f61ee2SBarry Smith       adapted = PETSC_TRUE;
13588b724c91SMatthew G. Knepley       PetscCall(DMAdaptorMonitor(adaptor, adaptIter, dm, dm, 1, &errorNorm, NULL));
1359c6f61ee2SBarry Smith       break;
1360c6f61ee2SBarry Smith     case DM_ADAPTATION_LABEL: {
1361c6f61ee2SBarry Smith       /* Adapt DM
1362c6f61ee2SBarry Smith            Create local solution
1363c6f61ee2SBarry Smith            Reconstruct gradients (FVM) or solve adjoint equation (FEM)
1364c6f61ee2SBarry Smith            Produce cellwise error indicator */
13653a336bb1SMatthew G. Knepley       DM             edm, plex;
13668b724c91SMatthew G. Knepley       PetscDS        ds;
13673a336bb1SMatthew G. Knepley       PetscFE        efe;
1368c6f61ee2SBarry Smith       DMLabel        adaptLabel;
1369c6f61ee2SBarry Smith       IS             refineIS, coarsenIS;
13703a336bb1SMatthew G. Knepley       DMPolytopeType ct;
13718b724c91SMatthew G. Knepley       PetscScalar    errorVal;
13723a336bb1SMatthew G. Knepley       PetscInt       nRefine, nCoarsen, cStart;
1373c6f61ee2SBarry Smith 
1374c6f61ee2SBarry Smith       PetscCall(DMLabelCreate(PETSC_COMM_SELF, "adapt", &adaptLabel));
1375c6f61ee2SBarry Smith 
13763a336bb1SMatthew G. Knepley       // TODO Move this creation to PreAdapt
13773a336bb1SMatthew G. Knepley       PetscCall(DMClone(dm, &edm));
13783a336bb1SMatthew G. Knepley       PetscCall(DMConvert(edm, DMPLEX, &plex));
13793a336bb1SMatthew G. Knepley       PetscCall(DMPlexGetHeightStratum(plex, 0, &cStart, NULL));
13803a336bb1SMatthew G. Knepley       PetscCall(DMPlexGetCellType(plex, cStart, &ct));
13813a336bb1SMatthew G. Knepley       PetscCall(DMDestroy(&plex));
13823a336bb1SMatthew G. Knepley       PetscCall(PetscFECreateLagrangeByCell(PETSC_COMM_SELF, dim, 1, ct, 0, PETSC_DEFAULT, &efe));
13833a336bb1SMatthew G. Knepley       PetscCall(PetscObjectSetName((PetscObject)efe, "Error"));
13843a336bb1SMatthew G. Knepley       PetscCall(DMSetField(edm, 0, NULL, (PetscObject)efe));
13853a336bb1SMatthew G. Knepley       PetscCall(PetscFEDestroy(&efe));
13863a336bb1SMatthew G. Knepley       PetscCall(DMCreateDS(edm));
13878b724c91SMatthew G. Knepley       PetscCall(DMGetGlobalVector(edm, &error));
13888b724c91SMatthew G. Knepley       PetscCall(PetscObjectSetName((PetscObject)error, "Error Estimator"));
1389c6f61ee2SBarry Smith 
13908b724c91SMatthew G. Knepley       PetscUseTypeMethod(adaptor, computeerrorindicator, locX, error);
13918b724c91SMatthew G. Knepley       PetscCall(VecViewFromOptions(error, (PetscObject)adaptor, "-adapt_error_vec_view"));
13928b724c91SMatthew G. Knepley       PetscCall(DMGetDS(edm, &ds));
13938b724c91SMatthew G. Knepley       PetscCall(PetscDSSetObjective(ds, 0, identity));
13948b724c91SMatthew G. Knepley       PetscCall(DMPlexComputeIntegralFEM(edm, error, &errorVal, NULL));
13958b724c91SMatthew G. Knepley       errorNorm = PetscRealPart(errorVal);
13963a336bb1SMatthew G. Knepley 
13973a336bb1SMatthew G. Knepley       // Compute IS from VecTagger
13988b724c91SMatthew G. Knepley       PetscCall(VecTaggerComputeIS(adaptor->refineTag, error, &refineIS, NULL));
13998b724c91SMatthew G. Knepley       PetscCall(VecTaggerComputeIS(adaptor->coarsenTag, error, &coarsenIS, NULL));
14003a336bb1SMatthew G. Knepley       PetscCall(ISViewFromOptions(refineIS, (PetscObject)adaptor->refineTag, "-is_view"));
14013a336bb1SMatthew G. Knepley       PetscCall(ISViewFromOptions(coarsenIS, (PetscObject)adaptor->coarsenTag, "-is_view"));
1402c6f61ee2SBarry Smith       PetscCall(ISGetSize(refineIS, &nRefine));
1403c6f61ee2SBarry Smith       PetscCall(ISGetSize(coarsenIS, &nCoarsen));
1404c6f61ee2SBarry Smith       PetscCall(PetscInfo(adaptor, "DMAdaptor: numRefine %" PetscInt_FMT ", numCoarsen %" PetscInt_FMT "\n", nRefine, nCoarsen));
1405c6f61ee2SBarry Smith       if (nRefine) PetscCall(DMLabelSetStratumIS(adaptLabel, DM_ADAPT_REFINE, refineIS));
1406c6f61ee2SBarry Smith       if (nCoarsen) PetscCall(DMLabelSetStratumIS(adaptLabel, DM_ADAPT_COARSEN, coarsenIS));
1407c6f61ee2SBarry Smith       PetscCall(ISDestroy(&coarsenIS));
1408c6f61ee2SBarry Smith       PetscCall(ISDestroy(&refineIS));
14093a336bb1SMatthew G. Knepley       // Adapt DM from label
1410c6f61ee2SBarry Smith       if (nRefine || nCoarsen) {
14113a336bb1SMatthew G. Knepley         char        oprefix[PETSC_MAX_PATH_LEN];
14123a336bb1SMatthew G. Knepley         const char *p;
14133a336bb1SMatthew G. Knepley         PetscBool   flg;
14143a336bb1SMatthew G. Knepley 
14153a336bb1SMatthew G. Knepley         PetscCall(PetscOptionsHasName(NULL, adaptor->hdr.prefix, "-adapt_vec_view", &flg));
14163a336bb1SMatthew G. Knepley         if (flg) {
14173a336bb1SMatthew G. Knepley           Vec ref;
14183a336bb1SMatthew G. Knepley 
14193a336bb1SMatthew G. Knepley           PetscCall(DMPlexCreateLabelField(dm, adaptLabel, &ref));
14203a336bb1SMatthew G. Knepley           PetscCall(VecViewFromOptions(ref, (PetscObject)adaptor, "-adapt_vec_view"));
14213a336bb1SMatthew G. Knepley           PetscCall(VecDestroy(&ref));
14223a336bb1SMatthew G. Knepley         }
14233a336bb1SMatthew G. Knepley 
14243a336bb1SMatthew G. Knepley         PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, &p));
14253a336bb1SMatthew G. Knepley         PetscCall(PetscStrncpy(oprefix, p, PETSC_MAX_PATH_LEN));
14263a336bb1SMatthew G. Knepley         PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)dm, "adapt_"));
1427c6f61ee2SBarry Smith         PetscCall(DMAdaptLabel(dm, adaptLabel, &odm));
14283a336bb1SMatthew G. Knepley         PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, oprefix));
14293a336bb1SMatthew G. Knepley         PetscCall(PetscObjectSetOptionsPrefix((PetscObject)odm, oprefix));
14308b724c91SMatthew G. Knepley         PetscCall(DMAdaptorMonitor(adaptor, adaptIter, dm, odm, 1, &errorNorm, error));
1431c6f61ee2SBarry Smith         adapted = PETSC_TRUE;
14328b724c91SMatthew G. Knepley       } else {
14338b724c91SMatthew G. Knepley         PetscCall(DMAdaptorMonitor(adaptor, adaptIter, dm, dm, 1, &errorNorm, error));
1434c6f61ee2SBarry Smith       }
1435c6f61ee2SBarry Smith       PetscCall(DMLabelDestroy(&adaptLabel));
14368b724c91SMatthew G. Knepley       PetscCall(DMRestoreGlobalVector(edm, &error));
14378b724c91SMatthew G. Knepley       PetscCall(DMDestroy(&edm));
1438c6f61ee2SBarry Smith     } break;
1439c6f61ee2SBarry Smith     case DM_ADAPTATION_METRIC: {
1440c6f61ee2SBarry Smith       DM        dmGrad, dmHess, dmMetric, dmDet;
1441c6f61ee2SBarry Smith       Vec       xGrad, xHess, metric, determinant;
1442c6f61ee2SBarry Smith       PetscReal N;
1443c6f61ee2SBarry Smith       DMLabel   bdLabel = NULL, rgLabel = NULL;
1444c6f61ee2SBarry Smith       PetscBool higherOrder = PETSC_FALSE;
1445c6f61ee2SBarry Smith       PetscInt  Nd          = coordDim * coordDim, f, vStart, vEnd;
1446c6f61ee2SBarry Smith       void (**funcs)(PetscInt, PetscInt, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[], PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]);
1447c6f61ee2SBarry Smith 
1448c6f61ee2SBarry Smith       PetscCall(PetscMalloc(1, &funcs));
1449c6f61ee2SBarry Smith       funcs[0] = identityFunc;
1450c6f61ee2SBarry Smith 
1451c6f61ee2SBarry Smith       /*     Setup finite element spaces */
1452c6f61ee2SBarry Smith       PetscCall(DMClone(dm, &dmGrad));
1453c6f61ee2SBarry Smith       PetscCall(DMClone(dm, &dmHess));
14543a336bb1SMatthew G. Knepley       PetscCheck(Nf <= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Adaptation with multiple fields not yet considered"); // TODO
14553a336bb1SMatthew G. Knepley       for (f = 0; f < Nf; ++f) {
1456c6f61ee2SBarry Smith         PetscFE         fe, feGrad, feHess;
1457c6f61ee2SBarry Smith         PetscDualSpace  Q;
1458c6f61ee2SBarry Smith         PetscSpace      space;
1459c6f61ee2SBarry Smith         DM              K;
1460c6f61ee2SBarry Smith         PetscQuadrature q;
1461c6f61ee2SBarry Smith         PetscInt        Nc, qorder, p;
1462c6f61ee2SBarry Smith         const char     *prefix;
1463c6f61ee2SBarry Smith 
14643a336bb1SMatthew G. Knepley         PetscCall(PetscDSGetDiscretization(ds, f, (PetscObject *)&fe));
1465c6f61ee2SBarry Smith         PetscCall(PetscFEGetNumComponents(fe, &Nc));
1466c6f61ee2SBarry Smith         PetscCheck(Nc <= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Adaptation with multiple components not yet considered"); // TODO
1467c6f61ee2SBarry Smith         PetscCall(PetscFEGetBasisSpace(fe, &space));
1468c6f61ee2SBarry Smith         PetscCall(PetscSpaceGetDegree(space, NULL, &p));
1469c6f61ee2SBarry Smith         if (p > 1) higherOrder = PETSC_TRUE;
1470c6f61ee2SBarry Smith         PetscCall(PetscFEGetDualSpace(fe, &Q));
1471c6f61ee2SBarry Smith         PetscCall(PetscDualSpaceGetDM(Q, &K));
1472c6f61ee2SBarry Smith         PetscCall(DMPlexGetDepthStratum(K, 0, &vStart, &vEnd));
1473c6f61ee2SBarry Smith         PetscCall(PetscFEGetQuadrature(fe, &q));
1474c6f61ee2SBarry Smith         PetscCall(PetscQuadratureGetOrder(q, &qorder));
1475c6f61ee2SBarry Smith         PetscCall(PetscObjectGetOptionsPrefix((PetscObject)fe, &prefix));
1476c6f61ee2SBarry Smith         PetscCall(PetscFECreateDefault(PetscObjectComm((PetscObject)dmGrad), dim, Nc * coordDim, PETSC_TRUE, prefix, qorder, &feGrad));
1477c6f61ee2SBarry Smith         PetscCall(PetscFECreateDefault(PetscObjectComm((PetscObject)dmHess), dim, Nc * Nd, PETSC_TRUE, prefix, qorder, &feHess));
1478c6f61ee2SBarry Smith         PetscCall(DMSetField(dmGrad, f, NULL, (PetscObject)feGrad));
1479c6f61ee2SBarry Smith         PetscCall(DMSetField(dmHess, f, NULL, (PetscObject)feHess));
1480c6f61ee2SBarry Smith         PetscCall(DMCreateDS(dmGrad));
1481c6f61ee2SBarry Smith         PetscCall(DMCreateDS(dmHess));
1482c6f61ee2SBarry Smith         PetscCall(PetscFEDestroy(&feGrad));
1483c6f61ee2SBarry Smith         PetscCall(PetscFEDestroy(&feHess));
1484c6f61ee2SBarry Smith       }
1485c6f61ee2SBarry Smith       /*     Compute vertexwise gradients from cellwise gradients */
1486c6f61ee2SBarry Smith       PetscCall(DMCreateLocalVector(dmGrad, &xGrad));
1487c6f61ee2SBarry Smith       PetscCall(VecViewFromOptions(locX, NULL, "-sol_adapt_loc_pre_view"));
1488c6f61ee2SBarry Smith       PetscCall(DMPlexComputeGradientClementInterpolant(dm, locX, xGrad));
1489c6f61ee2SBarry Smith       PetscCall(VecViewFromOptions(xGrad, NULL, "-adapt_gradient_view"));
1490c6f61ee2SBarry Smith       /*     Compute vertexwise Hessians from cellwise Hessians */
1491c6f61ee2SBarry Smith       PetscCall(DMCreateLocalVector(dmHess, &xHess));
1492c6f61ee2SBarry Smith       PetscCall(DMPlexComputeGradientClementInterpolant(dmGrad, xGrad, xHess));
1493c6f61ee2SBarry Smith       PetscCall(VecViewFromOptions(xHess, NULL, "-adapt_hessian_view"));
1494c6f61ee2SBarry Smith       PetscCall(VecDestroy(&xGrad));
1495c6f61ee2SBarry Smith       PetscCall(DMDestroy(&dmGrad));
1496c6f61ee2SBarry Smith       /*     Compute L-p normalized metric */
1497c6f61ee2SBarry Smith       PetscCall(DMClone(dm, &dmMetric));
1498c6f61ee2SBarry Smith       N = adaptor->Nadapt >= 0 ? adaptor->Nadapt : PetscPowRealInt(adaptor->refinementFactor, dim) * ((PetscReal)(vEnd - vStart));
14998b724c91SMatthew G. Knepley       // TODO This was where the old monitor was, figure out how to show metric and target N
1500c6f61ee2SBarry Smith       PetscCall(DMPlexMetricSetTargetComplexity(dmMetric, (PetscReal)N));
1501c6f61ee2SBarry Smith       if (higherOrder) {
1502c6f61ee2SBarry Smith         /*   Project Hessian into P1 space, if required */
1503c6f61ee2SBarry Smith         PetscCall(DMPlexMetricCreate(dmMetric, 0, &metric));
1504c6f61ee2SBarry Smith         PetscCall(DMProjectFieldLocal(dmMetric, 0.0, xHess, funcs, INSERT_ALL_VALUES, metric));
1505c6f61ee2SBarry Smith         PetscCall(VecDestroy(&xHess));
1506c6f61ee2SBarry Smith         xHess = metric;
1507c6f61ee2SBarry Smith       }
1508c6f61ee2SBarry Smith       PetscCall(PetscFree(funcs));
1509c6f61ee2SBarry Smith       PetscCall(DMPlexMetricCreate(dmMetric, 0, &metric));
1510c6f61ee2SBarry Smith       PetscCall(DMPlexMetricDeterminantCreate(dmMetric, 0, &determinant, &dmDet));
1511c6f61ee2SBarry Smith       PetscCall(DMPlexMetricNormalize(dmMetric, xHess, PETSC_TRUE, PETSC_TRUE, metric, determinant));
1512c6f61ee2SBarry Smith       PetscCall(VecDestroy(&determinant));
1513c6f61ee2SBarry Smith       PetscCall(DMDestroy(&dmDet));
1514c6f61ee2SBarry Smith       PetscCall(VecDestroy(&xHess));
1515c6f61ee2SBarry Smith       PetscCall(DMDestroy(&dmHess));
1516c6f61ee2SBarry Smith       /*     Adapt DM from metric */
1517c6f61ee2SBarry Smith       PetscCall(DMGetLabel(dm, "marker", &bdLabel));
1518c6f61ee2SBarry Smith       PetscCall(DMAdaptMetric(dm, metric, bdLabel, rgLabel, &odm));
15198b724c91SMatthew G. Knepley       PetscCall(DMAdaptorMonitor(adaptor, adaptIter, dm, odm, 1, &errorNorm, NULL));
1520c6f61ee2SBarry Smith       adapted = PETSC_TRUE;
1521c6f61ee2SBarry Smith       /*     Cleanup */
1522c6f61ee2SBarry Smith       PetscCall(VecDestroy(&metric));
1523c6f61ee2SBarry Smith       PetscCall(DMDestroy(&dmMetric));
1524c6f61ee2SBarry Smith     } break;
1525c6f61ee2SBarry Smith     default:
1526c6f61ee2SBarry Smith       SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Invalid adaptation type: %d", adaptor->adaptCriterion);
1527c6f61ee2SBarry Smith     }
1528c6f61ee2SBarry Smith     PetscCall(DMAdaptorPostAdapt(adaptor));
1529c6f61ee2SBarry Smith     PetscCall(DMRestoreLocalVector(dm, &locX));
1530c6f61ee2SBarry Smith     /* If DM was adapted, replace objects and recreate solution */
1531c6f61ee2SBarry Smith     if (adapted) {
1532c6f61ee2SBarry Smith       const char *name;
1533c6f61ee2SBarry Smith 
1534c6f61ee2SBarry Smith       PetscCall(PetscObjectGetName((PetscObject)dm, &name));
1535c6f61ee2SBarry Smith       PetscCall(PetscObjectSetName((PetscObject)odm, name));
1536c6f61ee2SBarry Smith       /* Reconfigure solver */
1537c6f61ee2SBarry Smith       PetscCall(SNESReset(adaptor->snes));
1538c6f61ee2SBarry Smith       PetscCall(SNESSetDM(adaptor->snes, odm));
1539c6f61ee2SBarry Smith       PetscCall(DMAdaptorSetSolver(adaptor, adaptor->snes));
1540c6f61ee2SBarry Smith       PetscCall(DMPlexSetSNESLocalFEM(odm, PETSC_FALSE, ctx));
1541c6f61ee2SBarry Smith       PetscCall(SNESSetFromOptions(adaptor->snes));
1542c6f61ee2SBarry Smith       /* Transfer system */
1543c6f61ee2SBarry Smith       PetscCall(DMCopyDisc(dm, odm));
1544c6f61ee2SBarry Smith       /* Transfer solution */
1545c6f61ee2SBarry Smith       PetscCall(DMCreateGlobalVector(odm, &ox));
1546c6f61ee2SBarry Smith       PetscCall(PetscObjectGetName((PetscObject)x, &name));
1547c6f61ee2SBarry Smith       PetscCall(PetscObjectSetName((PetscObject)ox, name));
1548c6f61ee2SBarry Smith       PetscCall(DMAdaptorTransferSolution(adaptor, dm, x, odm, ox));
1549c6f61ee2SBarry Smith       /* Cleanup adaptivity info */
1550c6f61ee2SBarry Smith       if (adaptIter > 0) PetscCall(PetscViewerASCIIPopTab(PETSC_VIEWER_STDOUT_(comm)));
1551c6f61ee2SBarry Smith       PetscCall(DMForestSetAdaptivityForest(dm, NULL)); /* clear internal references to the previous dm */
1552c6f61ee2SBarry Smith       PetscCall(DMDestroy(&dm));
1553c6f61ee2SBarry Smith       PetscCall(VecDestroy(&x));
1554c6f61ee2SBarry Smith       *adm = odm;
1555c6f61ee2SBarry Smith       *ax  = ox;
1556c6f61ee2SBarry Smith     } else {
1557c6f61ee2SBarry Smith       *adm      = dm;
1558c6f61ee2SBarry Smith       *ax       = x;
1559c6f61ee2SBarry Smith       adaptIter = numAdapt;
1560c6f61ee2SBarry Smith     }
1561c6f61ee2SBarry Smith     if (adaptIter < numAdapt - 1) {
1562c6f61ee2SBarry Smith       PetscCall(DMViewFromOptions(odm, NULL, "-dm_adapt_iter_view"));
1563c6f61ee2SBarry Smith       PetscCall(VecViewFromOptions(ox, NULL, "-sol_adapt_iter_view"));
1564c6f61ee2SBarry Smith     }
1565c6f61ee2SBarry Smith   }
1566c6f61ee2SBarry Smith   PetscCall(DMViewFromOptions(*adm, NULL, "-dm_adapt_view"));
1567c6f61ee2SBarry Smith   PetscCall(VecViewFromOptions(*ax, NULL, "-sol_adapt_view"));
1568c6f61ee2SBarry Smith   PetscFunctionReturn(PETSC_SUCCESS);
1569c6f61ee2SBarry Smith }
1570c6f61ee2SBarry Smith 
1571c6f61ee2SBarry Smith /*@
1572c6f61ee2SBarry Smith   DMAdaptorAdapt - Creates a new `DM` that is adapted to the problem
1573c6f61ee2SBarry Smith 
1574c6f61ee2SBarry Smith   Not Collective
1575c6f61ee2SBarry Smith 
1576c6f61ee2SBarry Smith   Input Parameters:
1577c6f61ee2SBarry Smith + adaptor  - The `DMAdaptor` object
1578c6f61ee2SBarry Smith . x        - The global approximate solution
1579c6f61ee2SBarry Smith - strategy - The adaptation strategy, see `DMAdaptationStrategy`
1580c6f61ee2SBarry Smith 
1581c6f61ee2SBarry Smith   Output Parameters:
1582c6f61ee2SBarry Smith + adm - The adapted `DM`
1583c6f61ee2SBarry Smith - ax  - The adapted solution
1584c6f61ee2SBarry Smith 
1585c6f61ee2SBarry Smith   Options Database Keys:
1586c6f61ee2SBarry Smith + -snes_adapt <strategy> - initial, sequential, multigrid
1587c6f61ee2SBarry Smith . -adapt_gradient_view   - View the Clement interpolant of the solution gradient
1588c6f61ee2SBarry Smith . -adapt_hessian_view    - View the Clement interpolant of the solution Hessian
1589c6f61ee2SBarry Smith - -adapt_metric_view     - View the metric tensor for adaptive mesh refinement
1590c6f61ee2SBarry Smith 
1591c6f61ee2SBarry Smith   Level: intermediate
1592c6f61ee2SBarry Smith 
1593df514481SBarry Smith .seealso: [](ch_dmbase), `DMAdaptor`, `DMAdaptationStrategy`, `DMAdaptorSetSolver()`, `DMAdaptorCreate()`
1594c6f61ee2SBarry Smith @*/
1595c6f61ee2SBarry Smith PetscErrorCode DMAdaptorAdapt(DMAdaptor adaptor, Vec x, DMAdaptationStrategy strategy, DM *adm, Vec *ax)
1596c6f61ee2SBarry Smith {
1597c6f61ee2SBarry Smith   PetscFunctionBegin;
1598c6f61ee2SBarry Smith   switch (strategy) {
1599c6f61ee2SBarry Smith   case DM_ADAPTATION_INITIAL:
1600c6f61ee2SBarry Smith     PetscCall(DMAdaptorAdapt_Sequence_Private(adaptor, x, PETSC_FALSE, adm, ax));
1601c6f61ee2SBarry Smith     break;
1602c6f61ee2SBarry Smith   case DM_ADAPTATION_SEQUENTIAL:
1603c6f61ee2SBarry Smith     PetscCall(DMAdaptorAdapt_Sequence_Private(adaptor, x, PETSC_TRUE, adm, ax));
1604c6f61ee2SBarry Smith     break;
1605c6f61ee2SBarry Smith   default:
1606c6f61ee2SBarry Smith     SETERRQ(PetscObjectComm((PetscObject)adaptor), PETSC_ERR_ARG_WRONG, "Unrecognized adaptation strategy %d", strategy);
1607c6f61ee2SBarry Smith   }
1608c6f61ee2SBarry Smith   PetscFunctionReturn(PETSC_SUCCESS);
1609c6f61ee2SBarry Smith }
16103a336bb1SMatthew G. Knepley 
16113a336bb1SMatthew G. Knepley /*@C
16123a336bb1SMatthew G. Knepley   DMAdaptorGetMixedSetupFunction - Get the function setting up the mixed problem, if it exists
16133a336bb1SMatthew G. Knepley 
16143a336bb1SMatthew G. Knepley   Not Collective
16153a336bb1SMatthew G. Knepley 
16163a336bb1SMatthew G. Knepley   Input Parameter:
16173a336bb1SMatthew G. Knepley . adaptor - the `DMAdaptor`
16183a336bb1SMatthew G. Knepley 
16193a336bb1SMatthew G. Knepley   Output Parameter:
16203a336bb1SMatthew G. Knepley . setupFunc - the function setting up the mixed problem, or `NULL`
16213a336bb1SMatthew G. Knepley 
16223a336bb1SMatthew G. Knepley   Level: advanced
16233a336bb1SMatthew G. Knepley 
16243a336bb1SMatthew G. Knepley .seealso: `DMAdaptor`, `DMAdaptorSetMixedSetupFunction()`, `DMAdaptorAdapt()`
16253a336bb1SMatthew G. Knepley @*/
16263a336bb1SMatthew G. Knepley PetscErrorCode DMAdaptorGetMixedSetupFunction(DMAdaptor adaptor, PetscErrorCode (**setupFunc)(DMAdaptor, DM))
16273a336bb1SMatthew G. Knepley {
16283a336bb1SMatthew G. Knepley   PetscFunctionBegin;
16293a336bb1SMatthew G. Knepley   PetscValidHeaderSpecific(adaptor, DMADAPTOR_CLASSID, 1);
16303a336bb1SMatthew G. Knepley   PetscAssertPointer(setupFunc, 2);
16313a336bb1SMatthew G. Knepley   *setupFunc = adaptor->ops->mixedsetup;
16323a336bb1SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
16333a336bb1SMatthew G. Knepley }
16343a336bb1SMatthew G. Knepley 
16353a336bb1SMatthew G. Knepley /*@C
16363a336bb1SMatthew G. Knepley   DMAdaptorSetMixedSetupFunction - Set the function setting up the mixed problem
16373a336bb1SMatthew G. Knepley 
16383a336bb1SMatthew G. Knepley   Not Collective
16393a336bb1SMatthew G. Knepley 
16403a336bb1SMatthew G. Knepley   Input Parameters:
16413a336bb1SMatthew G. Knepley + adaptor   - the `DMAdaptor`
16423a336bb1SMatthew G. Knepley - setupFunc - the function setting up the mixed problem
16433a336bb1SMatthew G. Knepley 
16443a336bb1SMatthew G. Knepley   Level: advanced
16453a336bb1SMatthew G. Knepley 
16463a336bb1SMatthew G. Knepley .seealso: `DMAdaptor`, `DMAdaptorGetMixedSetupFunction()`, `DMAdaptorAdapt()`
16473a336bb1SMatthew G. Knepley @*/
16483a336bb1SMatthew G. Knepley PetscErrorCode DMAdaptorSetMixedSetupFunction(DMAdaptor adaptor, PetscErrorCode (*setupFunc)(DMAdaptor, DM))
16493a336bb1SMatthew G. Knepley {
16503a336bb1SMatthew G. Knepley   PetscFunctionBegin;
16513a336bb1SMatthew G. Knepley   PetscValidHeaderSpecific(adaptor, DMADAPTOR_CLASSID, 1);
16523a336bb1SMatthew G. Knepley   PetscValidFunction(setupFunc, 2);
16533a336bb1SMatthew G. Knepley   adaptor->ops->mixedsetup = setupFunc;
16543a336bb1SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
16553a336bb1SMatthew G. Knepley }
16563a336bb1SMatthew G. Knepley 
16578b724c91SMatthew G. Knepley /*@C
16588b724c91SMatthew G. Knepley   DMAdaptorGetCriterion - Get the adaptation criterion
16598b724c91SMatthew G. Knepley 
16608b724c91SMatthew G. Knepley   Not Collective
16618b724c91SMatthew G. Knepley 
16628b724c91SMatthew G. Knepley   Input Parameter:
16638b724c91SMatthew G. Knepley . adaptor - the `DMAdaptor`
16648b724c91SMatthew G. Knepley 
16658b724c91SMatthew G. Knepley   Output Parameter:
16668b724c91SMatthew G. Knepley . criterion - the criterion for adaptation
16678b724c91SMatthew G. Knepley 
16688b724c91SMatthew G. Knepley   Level: advanced
16698b724c91SMatthew G. Knepley 
16708b724c91SMatthew G. Knepley .seealso: `DMAdaptor`, `DMAdaptorSetCrierion()`, `DMAdaptationCriterion`
16718b724c91SMatthew G. Knepley @*/
16728b724c91SMatthew G. Knepley PetscErrorCode DMAdaptorGetCriterion(DMAdaptor adaptor, DMAdaptationCriterion *criterion)
16738b724c91SMatthew G. Knepley {
16748b724c91SMatthew G. Knepley   PetscFunctionBegin;
16758b724c91SMatthew G. Knepley   PetscValidHeaderSpecific(adaptor, DMADAPTOR_CLASSID, 1);
16768b724c91SMatthew G. Knepley   PetscAssertPointer(criterion, 2);
16778b724c91SMatthew G. Knepley   *criterion = adaptor->adaptCriterion;
16788b724c91SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
16798b724c91SMatthew G. Knepley }
16808b724c91SMatthew G. Knepley 
16818b724c91SMatthew G. Knepley /*@C
16828b724c91SMatthew G. Knepley   DMAdaptorSetCriterion - Set the adaptation criterion
16838b724c91SMatthew G. Knepley 
16848b724c91SMatthew G. Knepley   Not Collective
16858b724c91SMatthew G. Knepley 
16868b724c91SMatthew G. Knepley   Input Parameters:
16878b724c91SMatthew G. Knepley + adaptor   - the `DMAdaptor`
16888b724c91SMatthew G. Knepley - criterion - the adaptation criterion
16898b724c91SMatthew G. Knepley 
16908b724c91SMatthew G. Knepley   Level: advanced
16918b724c91SMatthew G. Knepley 
16928b724c91SMatthew G. Knepley .seealso: `DMAdaptor`, `DMAdaptorGetCriterion()`, `DMAdaptationCriterion`
16938b724c91SMatthew G. Knepley @*/
16948b724c91SMatthew G. Knepley PetscErrorCode DMAdaptorSetCriterion(DMAdaptor adaptor, DMAdaptationCriterion criterion)
16958b724c91SMatthew G. Knepley {
16968b724c91SMatthew G. Knepley   PetscFunctionBegin;
16978b724c91SMatthew G. Knepley   PetscValidHeaderSpecific(adaptor, DMADAPTOR_CLASSID, 1);
16988b724c91SMatthew G. Knepley   adaptor->adaptCriterion = criterion;
16998b724c91SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
17008b724c91SMatthew G. Knepley }
17018b724c91SMatthew G. Knepley 
17023a336bb1SMatthew G. Knepley static PetscErrorCode DMAdaptorInitialize_Gradient(DMAdaptor adaptor)
17033a336bb1SMatthew G. Knepley {
17043a336bb1SMatthew G. Knepley   PetscFunctionBegin;
17053a336bb1SMatthew G. Knepley   adaptor->ops->computeerrorindicator     = DMAdaptorComputeErrorIndicator_Gradient;
17063a336bb1SMatthew G. Knepley   adaptor->ops->computecellerrorindicator = DMAdaptorComputeCellErrorIndicator_Gradient;
17073a336bb1SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
17083a336bb1SMatthew G. Knepley }
17093a336bb1SMatthew G. Knepley 
17103a336bb1SMatthew G. Knepley PETSC_EXTERN PetscErrorCode DMAdaptorCreate_Gradient(DMAdaptor adaptor)
17113a336bb1SMatthew G. Knepley {
17123a336bb1SMatthew G. Knepley   PetscFunctionBegin;
17133a336bb1SMatthew G. Knepley   PetscValidHeaderSpecific(adaptor, DMADAPTOR_CLASSID, 1);
17143a336bb1SMatthew G. Knepley   adaptor->data = NULL;
17153a336bb1SMatthew G. Knepley 
17163a336bb1SMatthew G. Knepley   PetscCall(DMAdaptorInitialize_Gradient(adaptor));
17173a336bb1SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
17183a336bb1SMatthew G. Knepley }
17193a336bb1SMatthew G. Knepley 
17203a336bb1SMatthew G. Knepley static PetscErrorCode DMAdaptorInitialize_Flux(DMAdaptor adaptor)
17213a336bb1SMatthew G. Knepley {
17223a336bb1SMatthew G. Knepley   PetscFunctionBegin;
17233a336bb1SMatthew G. Knepley   adaptor->ops->computeerrorindicator = DMAdaptorComputeErrorIndicator_Flux;
17243a336bb1SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
17253a336bb1SMatthew G. Knepley }
17263a336bb1SMatthew G. Knepley 
17273a336bb1SMatthew G. Knepley PETSC_EXTERN PetscErrorCode DMAdaptorCreate_Flux(DMAdaptor adaptor)
17283a336bb1SMatthew G. Knepley {
17293a336bb1SMatthew G. Knepley   PetscFunctionBegin;
17303a336bb1SMatthew G. Knepley   PetscValidHeaderSpecific(adaptor, DMADAPTOR_CLASSID, 1);
17313a336bb1SMatthew G. Knepley   adaptor->data = NULL;
17323a336bb1SMatthew G. Knepley 
17333a336bb1SMatthew G. Knepley   PetscCall(DMAdaptorInitialize_Flux(adaptor));
17343a336bb1SMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
17353a336bb1SMatthew G. Knepley }
1736