xref: /petsc/src/dm/impls/forest/forest.c (revision 9a81d01380cf46b6d30ed2f8abea6c9ba9e969dc)
1fbbfd472SToby Isaac #include <petsc/private/dmforestimpl.h> /*I petscdmforest.h I*/
2fbbfd472SToby Isaac #include <petsc/private/dmimpl.h>       /*I petscdm.h */
3ef19d27cSToby Isaac #include <petscsf.h>
4db4d5e8cSToby Isaac 
5db4d5e8cSToby Isaac #undef __FUNCT__
6a0452a8eSToby Isaac #define __FUNCT__ "DMForestTemplate"
7a0452a8eSToby Isaac PETSC_EXTERN PetscErrorCode DMForestTemplate(DM dm, DM tdm)
8a0452a8eSToby Isaac {
9a0452a8eSToby Isaac   DM_Forest        *forest = (DM_Forest *) dm->data;
10a0452a8eSToby Isaac   DM               base;
11a0452a8eSToby Isaac   DMForestTopology topology;
12a0452a8eSToby Isaac   PetscInt         dim, overlap, ref, factor;
13a0452a8eSToby Isaac   DMForestAdaptivityStrategy strat;
14a0452a8eSToby Isaac   PetscErrorCode   ierr;
15a0452a8eSToby Isaac 
16a0452a8eSToby Isaac   PetscFunctionBegin;
17a0452a8eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18a0452a8eSToby Isaac   PetscValidHeaderSpecific(tdm, DM_CLASSID, 2);
19a0452a8eSToby Isaac   ierr = DMForestGetBaseDM(dm,&base);CHKERRQ(ierr);
20a0452a8eSToby Isaac   ierr = DMForestSetBaseDM(tdm,base);CHKERRQ(ierr);
21a0452a8eSToby Isaac   ierr = DMForestGetTopology(dm,&topology);CHKERRQ(ierr);
22a0452a8eSToby Isaac   ierr = DMForestSetTopology(tdm,topology);CHKERRQ(ierr);
23a0452a8eSToby Isaac   ierr = DMForestGetAdjacencyDimension(dm,&dim);CHKERRQ(ierr);
24a0452a8eSToby Isaac   ierr = DMForestSetAdjacencyDimension(tdm,dim);CHKERRQ(ierr);
25a0452a8eSToby Isaac   ierr = DMForestGetPartitionOverlap(dm,&overlap);CHKERRQ(ierr);
26a0452a8eSToby Isaac   ierr = DMForestSetPartitionOverlap(tdm,overlap);CHKERRQ(ierr);
27a0452a8eSToby Isaac   ierr = DMForestGetMinimumRefinement(dm,&ref);CHKERRQ(ierr);
28a0452a8eSToby Isaac   ierr = DMForestSetMinimumRefinement(tdm,ref);CHKERRQ(ierr);
29a0452a8eSToby Isaac   ierr = DMForestGetMaximumRefinement(dm,&ref);CHKERRQ(ierr);
30a0452a8eSToby Isaac   ierr = DMForestSetMaximumRefinement(tdm,ref);CHKERRQ(ierr);
31a0452a8eSToby Isaac   ierr = DMForestGetAdaptivityStrategy(dm,&strat);CHKERRQ(ierr);
32a0452a8eSToby Isaac   ierr = DMForestSetAdaptivityStrategy(tdm,strat);CHKERRQ(ierr);
33a0452a8eSToby Isaac   ierr = DMForestGetGradeFactor(dm,&factor);CHKERRQ(ierr);
34a0452a8eSToby Isaac   ierr = DMForestSetGradeFactor(tdm,factor);CHKERRQ(ierr);
35a0452a8eSToby Isaac   if (forest->ftemplate) {
36a0452a8eSToby Isaac     ierr = (forest->ftemplate) (dm, tdm);CHKERRQ(ierr);
37a0452a8eSToby Isaac   }
38a0452a8eSToby Isaac   PetscFunctionReturn(0);
39a0452a8eSToby Isaac }
40a0452a8eSToby Isaac 
41a0452a8eSToby Isaac #undef __FUNCT__
42db4d5e8cSToby Isaac #define __FUNCT__ "DMClone_Forest"
43db4d5e8cSToby Isaac PETSC_EXTERN PetscErrorCode DMClone_Forest(DM dm, DM *newdm)
44db4d5e8cSToby Isaac {
45db4d5e8cSToby Isaac   DM_Forest        *forest = (DM_Forest *) dm->data;
46db4d5e8cSToby Isaac   const char       *type;
47db4d5e8cSToby Isaac   PetscErrorCode ierr;
48db4d5e8cSToby Isaac 
49db4d5e8cSToby Isaac   PetscFunctionBegin;
50db4d5e8cSToby Isaac   forest->refct++;
51db4d5e8cSToby Isaac   (*newdm)->data = forest;
52db4d5e8cSToby Isaac   ierr = PetscObjectGetType((PetscObject) dm, &type);CHKERRQ(ierr);
53db4d5e8cSToby Isaac   ierr = PetscObjectChangeTypeName((PetscObject) *newdm, type);CHKERRQ(ierr);
54db4d5e8cSToby Isaac   PetscFunctionReturn(0);
55db4d5e8cSToby Isaac }
56db4d5e8cSToby Isaac 
57db4d5e8cSToby Isaac #undef __FUNCT__
58db4d5e8cSToby Isaac #define __FUNCT__ "DMDestroy_Forest"
59d222f98bSToby Isaac static PetscErrorCode DMDestroy_Forest(DM dm)
60db4d5e8cSToby Isaac {
61db4d5e8cSToby Isaac   DM_Forest     *forest = (DM_Forest*) dm->data;
62db4d5e8cSToby Isaac   PetscErrorCode ierr;
63db4d5e8cSToby Isaac 
64db4d5e8cSToby Isaac   PetscFunctionBegin;
65db4d5e8cSToby Isaac   if (--forest->refct > 0) PetscFunctionReturn(0);
66d222f98bSToby Isaac   if (forest->destroy) {ierr = forest->destroy(dm);CHKERRQ(ierr);}
67db4d5e8cSToby Isaac   ierr = PetscSFDestroy(&forest->cellSF);CHKERRQ(ierr);
68db4d5e8cSToby Isaac   if (forest->adaptCopyMode == PETSC_OWN_POINTER) {
69db4d5e8cSToby Isaac     ierr = PetscFree(forest->adaptMarkers);CHKERRQ(ierr);
70db4d5e8cSToby Isaac   }
71db4d5e8cSToby Isaac   if (forest->cellWeightsCopyMode == PETSC_OWN_POINTER) {
72db4d5e8cSToby Isaac     ierr = PetscFree(forest->cellWeights);CHKERRQ(ierr);
73db4d5e8cSToby Isaac   }
74*9a81d013SToby Isaac   ierr = PetscFree(forest->adaptStrategy);CHKERRQ(ierr);
7556ba9f64SToby Isaac   ierr = DMDestroy(&forest->base);CHKERRQ(ierr);
7630f902e7SToby Isaac   ierr = PetscFree(forest->topology);CHKERRQ(ierr);
7730f902e7SToby Isaac   ierr = PetscFree(forest);CHKERRQ(ierr);
78db4d5e8cSToby Isaac   PetscFunctionReturn(0);
79db4d5e8cSToby Isaac }
80db4d5e8cSToby Isaac 
81db4d5e8cSToby Isaac #undef __FUNCT__
82dd8e54a2SToby Isaac #define __FUNCT__ "DMForestSetTopology"
83dd8e54a2SToby Isaac PetscErrorCode DMForestSetTopology(DM dm, DMForestTopology topology)
84db4d5e8cSToby Isaac {
85db4d5e8cSToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
86db4d5e8cSToby Isaac   PetscErrorCode ierr;
87db4d5e8cSToby Isaac 
88db4d5e8cSToby Isaac   PetscFunctionBegin;
89db4d5e8cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
90ef51cf95SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the topology after setup");
91dd8e54a2SToby Isaac   ierr = PetscFree(forest->topology);CHKERRQ(ierr);
92dd8e54a2SToby Isaac   ierr = PetscStrallocpy((const char *)topology,(char **) &forest->topology);CHKERRQ(ierr);
93db4d5e8cSToby Isaac   PetscFunctionReturn(0);
94db4d5e8cSToby Isaac }
95db4d5e8cSToby Isaac 
96db4d5e8cSToby Isaac #undef __FUNCT__
97dd8e54a2SToby Isaac #define __FUNCT__ "DMForestGetTopology"
98dd8e54a2SToby Isaac PetscErrorCode DMForestGetTopology(DM dm, DMForestTopology *topology)
99dd8e54a2SToby Isaac {
100dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
101dd8e54a2SToby Isaac 
102dd8e54a2SToby Isaac   PetscFunctionBegin;
103dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
104dd8e54a2SToby Isaac   PetscValidPointer(topology,2);
105dd8e54a2SToby Isaac   *topology = forest->topology;
106dd8e54a2SToby Isaac   PetscFunctionReturn(0);
107dd8e54a2SToby Isaac }
108dd8e54a2SToby Isaac 
109dd8e54a2SToby Isaac #undef __FUNCT__
110dd8e54a2SToby Isaac #define __FUNCT__ "DMForestSetBaseDM"
111dd8e54a2SToby Isaac PetscErrorCode DMForestSetBaseDM(DM dm, DM base)
112dd8e54a2SToby Isaac {
113dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
114dd8e54a2SToby Isaac   PetscInt       dim, dimEmbed;
115dd8e54a2SToby Isaac   PetscErrorCode ierr;
116dd8e54a2SToby Isaac 
117dd8e54a2SToby Isaac   PetscFunctionBegin;
118dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
119ef51cf95SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the base after setup");
120dd8e54a2SToby Isaac   ierr = PetscObjectReference((PetscObject)base);CHKERRQ(ierr);
121dd8e54a2SToby Isaac   ierr = DMDestroy(&forest->base);CHKERRQ(ierr);
122dd8e54a2SToby Isaac   forest->base = base;
123a0452a8eSToby Isaac   if (base) {
124a0452a8eSToby Isaac     PetscValidHeaderSpecific(base, DM_CLASSID, 2);
125dd8e54a2SToby Isaac     ierr = DMGetDimension(base,&dim);CHKERRQ(ierr);
126dd8e54a2SToby Isaac     ierr = DMSetDimension(dm,dim);CHKERRQ(ierr);
127dd8e54a2SToby Isaac     ierr = DMGetCoordinateDim(base,&dimEmbed);CHKERRQ(ierr);
128dd8e54a2SToby Isaac     ierr = DMSetCoordinateDim(dm,dimEmbed);CHKERRQ(ierr);
129a0452a8eSToby Isaac   }
130dd8e54a2SToby Isaac   PetscFunctionReturn(0);
131dd8e54a2SToby Isaac }
132dd8e54a2SToby Isaac 
133dd8e54a2SToby Isaac #undef __FUNCT__
134dd8e54a2SToby Isaac #define __FUNCT__ "DMForestGetBaseDM"
135dd8e54a2SToby Isaac PetscErrorCode DMForestGetBaseDM(DM dm, DM *base)
136dd8e54a2SToby Isaac {
137dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
138dd8e54a2SToby Isaac 
139dd8e54a2SToby Isaac   PetscFunctionBegin;
140dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
141dd8e54a2SToby Isaac   PetscValidPointer(base, 2);
142dd8e54a2SToby Isaac   *base = forest->base;
143dd8e54a2SToby Isaac   PetscFunctionReturn(0);
144dd8e54a2SToby Isaac }
145dd8e54a2SToby Isaac 
146dd8e54a2SToby Isaac #undef __FUNCT__
147dd8e54a2SToby Isaac #define __FUNCT__ "DMForestSetCoarseForest"
148dd8e54a2SToby Isaac PetscErrorCode DMForestSetCoarseForest(DM dm,DM coarse)
149dd8e54a2SToby Isaac {
150dd8e54a2SToby Isaac   PetscErrorCode   ierr;
151dd8e54a2SToby Isaac 
152dd8e54a2SToby Isaac   PetscFunctionBegin;
153dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
154ef51cf95SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the coarse forest after setup");
15556ba9f64SToby Isaac   ierr = DMSetCoarseDM(dm,coarse);CHKERRQ(ierr);
156a0452a8eSToby Isaac   if (coarse) {
157a0452a8eSToby Isaac     PetscValidHeaderSpecific(coarse, DM_CLASSID, 2);
158a0452a8eSToby Isaac     ierr = DMForestTemplate(coarse,dm);CHKERRQ(ierr);
159a0452a8eSToby Isaac   }
160dd8e54a2SToby Isaac   PetscFunctionReturn(0);
161dd8e54a2SToby Isaac }
162dd8e54a2SToby Isaac 
163dd8e54a2SToby Isaac #undef __FUNCT__
164dd8e54a2SToby Isaac #define __FUNCT__ "DMForestGetCoarseForest"
165dd8e54a2SToby Isaac PetscErrorCode DMForestGetCoarseForest(DM dm, DM *coarse)
166dd8e54a2SToby Isaac {
16756ba9f64SToby Isaac   PetscErrorCode ierr;
168dd8e54a2SToby Isaac 
169dd8e54a2SToby Isaac   PetscFunctionBegin;
17056ba9f64SToby Isaac   ierr = DMGetCoarseDM(dm,coarse);CHKERRQ(ierr);
171dd8e54a2SToby Isaac   PetscFunctionReturn(0);
172dd8e54a2SToby Isaac }
173dd8e54a2SToby Isaac 
174dd8e54a2SToby Isaac #undef __FUNCT__
175dd8e54a2SToby Isaac #define __FUNCT__ "DMForestSetFineForest"
176dd8e54a2SToby Isaac PetscErrorCode DMForestSetFineForest(DM dm,DM fine)
177dd8e54a2SToby Isaac {
178dd8e54a2SToby Isaac   PetscErrorCode   ierr;
179dd8e54a2SToby Isaac 
180dd8e54a2SToby Isaac   PetscFunctionBegin;
181dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
182ef51cf95SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the fine forest after setup");
18388bdff64SToby Isaac   ierr = DMSetFineDM(dm,fine);CHKERRQ(ierr);
184a0452a8eSToby Isaac   if (fine) {
185a0452a8eSToby Isaac     PetscValidHeaderSpecific(fine, DM_CLASSID, 2);
186a0452a8eSToby Isaac     ierr = DMForestTemplate(fine,dm);CHKERRQ(ierr);
187a0452a8eSToby Isaac   }
188dd8e54a2SToby Isaac   PetscFunctionReturn(0);
189dd8e54a2SToby Isaac }
190dd8e54a2SToby Isaac 
191dd8e54a2SToby Isaac #undef __FUNCT__
192ef51cf95SToby Isaac #define __FUNCT__ "DMForestGetFineForest"
193dd8e54a2SToby Isaac PetscErrorCode DMForestGetFineForest(DM dm, DM *fine)
194dd8e54a2SToby Isaac {
19588bdff64SToby Isaac   PetscErrorCode ierr;
196dd8e54a2SToby Isaac 
197dd8e54a2SToby Isaac   PetscFunctionBegin;
19888bdff64SToby Isaac   ierr = DMGetFineDM(dm,fine);CHKERRQ(ierr);
199dd8e54a2SToby Isaac   PetscFunctionReturn(0);
200dd8e54a2SToby Isaac }
201dd8e54a2SToby Isaac 
202dd8e54a2SToby Isaac #undef __FUNCT__
203dd8e54a2SToby Isaac #define __FUNCT__ "DMForestSetAdjacencyDimension"
204dd8e54a2SToby Isaac PetscErrorCode DMForestSetAdjacencyDimension(DM dm, PetscInt adjDim)
205dd8e54a2SToby Isaac {
206dd8e54a2SToby Isaac   PetscInt        dim;
207dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
208dd8e54a2SToby Isaac   PetscErrorCode  ierr;
209dd8e54a2SToby Isaac 
210dd8e54a2SToby Isaac   PetscFunctionBegin;
211dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
212ef51cf95SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the adjacency dimension after setup");
213dd8e54a2SToby Isaac   if (adjDim < 0) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"adjacency dim cannot be < 0: %d", adjDim);
214dd8e54a2SToby Isaac   ierr = DMGetDimension(dm,&dim);CHKERRQ(ierr);
215dd8e54a2SToby Isaac   if (adjDim > dim) SETERRQ2(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"adjacency dim cannot be > %d: %d", dim, adjDim);
216dd8e54a2SToby Isaac   forest->adjDim = adjDim;
217dd8e54a2SToby Isaac   PetscFunctionReturn(0);
218dd8e54a2SToby Isaac }
219dd8e54a2SToby Isaac 
220dd8e54a2SToby Isaac #undef __FUNCT__
221dd8e54a2SToby Isaac #define __FUNCT__ "DMForestSetAdjacencyCodimension"
222dd8e54a2SToby Isaac PetscErrorCode DMForestSetAdjacencyCodimension(DM dm, PetscInt adjCodim)
223dd8e54a2SToby Isaac {
224dd8e54a2SToby Isaac   PetscInt        dim;
225dd8e54a2SToby Isaac   PetscErrorCode  ierr;
226dd8e54a2SToby Isaac 
227dd8e54a2SToby Isaac   PetscFunctionBegin;
228dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
229dd8e54a2SToby Isaac   ierr = DMGetDimension(dm,&dim);CHKERRQ(ierr);
230dd8e54a2SToby Isaac   ierr = DMForestSetAdjacencyDimension(dm,dim-adjCodim);CHKERRQ(ierr);
231dd8e54a2SToby Isaac   PetscFunctionReturn(0);
232dd8e54a2SToby Isaac }
233dd8e54a2SToby Isaac 
234dd8e54a2SToby Isaac #undef __FUNCT__
235dd8e54a2SToby Isaac #define __FUNCT__ "DMForestGetAdjacencyDimension"
236dd8e54a2SToby Isaac PetscErrorCode DMForestGetAdjacencyDimension(DM dm, PetscInt *adjDim)
237dd8e54a2SToby Isaac {
238dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
239dd8e54a2SToby Isaac 
240dd8e54a2SToby Isaac   PetscFunctionBegin;
241dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
242dd8e54a2SToby Isaac   PetscValidIntPointer(adjDim,2);
243dd8e54a2SToby Isaac   *adjDim = forest->adjDim;
244dd8e54a2SToby Isaac   PetscFunctionReturn(0);
245dd8e54a2SToby Isaac }
246dd8e54a2SToby Isaac 
247dd8e54a2SToby Isaac #undef __FUNCT__
248dd8e54a2SToby Isaac #define __FUNCT__ "DMForestGetAdjacencyCodimension"
249dd8e54a2SToby Isaac PetscErrorCode DMForestGetAdjacencyCodimension(DM dm, PetscInt *adjCodim)
250dd8e54a2SToby Isaac {
251dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
252dd8e54a2SToby Isaac   PetscInt       dim;
253dd8e54a2SToby Isaac   PetscErrorCode ierr;
254dd8e54a2SToby Isaac 
255dd8e54a2SToby Isaac   PetscFunctionBegin;
256dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
257dd8e54a2SToby Isaac   PetscValidIntPointer(adjCodim,2);
258dd8e54a2SToby Isaac   ierr = DMGetDimension(dm,&dim);CHKERRQ(ierr);
259dd8e54a2SToby Isaac   *adjCodim = dim - forest->adjDim;
260dd8e54a2SToby Isaac   PetscFunctionReturn(0);
261dd8e54a2SToby Isaac }
262dd8e54a2SToby Isaac 
263dd8e54a2SToby Isaac #undef __FUNCT__
264ef51cf95SToby Isaac #define __FUNCT__ "DMForestSetPartitionOverlap"
265dd8e54a2SToby Isaac PetscErrorCode DMForestSetPartitionOverlap(DM dm, PetscInt overlap)
266dd8e54a2SToby Isaac {
267dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
268dd8e54a2SToby Isaac 
269dd8e54a2SToby Isaac   PetscFunctionBegin;
270dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
271ef51cf95SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the overlap after setup");
272dd8e54a2SToby Isaac   if (overlap < 0) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"overlap cannot be < 0: %d", overlap);
273dd8e54a2SToby Isaac   forest->overlap = overlap;
274dd8e54a2SToby Isaac   PetscFunctionReturn(0);
275dd8e54a2SToby Isaac }
276dd8e54a2SToby Isaac 
277dd8e54a2SToby Isaac #undef __FUNCT__
278dd8e54a2SToby Isaac #define __FUNCT__ "DMForestGetPartitionOverlap"
279dd8e54a2SToby Isaac PetscErrorCode DMForestGetPartitionOverlap(DM dm, PetscInt *overlap)
280dd8e54a2SToby Isaac {
281dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
282dd8e54a2SToby Isaac 
283dd8e54a2SToby Isaac   PetscFunctionBegin;
284dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
285dd8e54a2SToby Isaac   PetscValidIntPointer(overlap,2);
286dd8e54a2SToby Isaac   *overlap = forest->overlap;
287dd8e54a2SToby Isaac   PetscFunctionReturn(0);
288dd8e54a2SToby Isaac }
289dd8e54a2SToby Isaac 
290dd8e54a2SToby Isaac #undef __FUNCT__
291dd8e54a2SToby Isaac #define __FUNCT__ "DMForestSetMinimumRefinement"
292dd8e54a2SToby Isaac PetscErrorCode DMForestSetMinimumRefinement(DM dm, PetscInt minRefinement)
293dd8e54a2SToby Isaac {
294dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
295dd8e54a2SToby Isaac 
296dd8e54a2SToby Isaac   PetscFunctionBegin;
297dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
298ef51cf95SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the minimum refinement after setup");
299dd8e54a2SToby Isaac   forest->minRefinement = minRefinement;
300dd8e54a2SToby Isaac   PetscFunctionReturn(0);
301dd8e54a2SToby Isaac }
302dd8e54a2SToby Isaac 
303dd8e54a2SToby Isaac #undef __FUNCT__
304dd8e54a2SToby Isaac #define __FUNCT__ "DMForestGetMinimumRefinement"
305dd8e54a2SToby Isaac PetscErrorCode DMForestGetMinimumRefinement(DM dm, PetscInt *minRefinement)
306dd8e54a2SToby Isaac {
307dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
308dd8e54a2SToby Isaac 
309dd8e54a2SToby Isaac   PetscFunctionBegin;
310dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
311dd8e54a2SToby Isaac   PetscValidIntPointer(minRefinement,2);
312dd8e54a2SToby Isaac   *minRefinement = forest->minRefinement;
313dd8e54a2SToby Isaac   PetscFunctionReturn(0);
314dd8e54a2SToby Isaac }
315dd8e54a2SToby Isaac 
316dd8e54a2SToby Isaac #undef __FUNCT__
31756ba9f64SToby Isaac #define __FUNCT__ "DMForestSetInitialRefinement"
31856ba9f64SToby Isaac PetscErrorCode DMForestSetInitialRefinement(DM dm, PetscInt initRefinement)
31956ba9f64SToby Isaac {
32056ba9f64SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
32156ba9f64SToby Isaac 
32256ba9f64SToby Isaac   PetscFunctionBegin;
32356ba9f64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32456ba9f64SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the initial refinement after setup");
32556ba9f64SToby Isaac   forest->initRefinement = initRefinement;
32656ba9f64SToby Isaac   PetscFunctionReturn(0);
32756ba9f64SToby Isaac }
32856ba9f64SToby Isaac 
32956ba9f64SToby Isaac #undef __FUNCT__
33056ba9f64SToby Isaac #define __FUNCT__ "DMForestGetInitialRefinement"
33156ba9f64SToby Isaac PetscErrorCode DMForestGetInitialRefinement(DM dm, PetscInt *initRefinement)
33256ba9f64SToby Isaac {
33356ba9f64SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
33456ba9f64SToby Isaac 
33556ba9f64SToby Isaac   PetscFunctionBegin;
33656ba9f64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
33756ba9f64SToby Isaac   PetscValidIntPointer(initRefinement,2);
33856ba9f64SToby Isaac   *initRefinement = forest->initRefinement;
33956ba9f64SToby Isaac   PetscFunctionReturn(0);
34056ba9f64SToby Isaac }
34156ba9f64SToby Isaac 
34256ba9f64SToby Isaac #undef __FUNCT__
343c7eeac06SToby Isaac #define __FUNCT__ "DMForestSetMaximumRefinement"
344c7eeac06SToby Isaac PetscErrorCode DMForestSetMaximumRefinement(DM dm, PetscInt maxRefinement)
345dd8e54a2SToby Isaac {
346dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
347dd8e54a2SToby Isaac 
348dd8e54a2SToby Isaac   PetscFunctionBegin;
349dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
350ef51cf95SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the maximum refinement after setup");
351c7eeac06SToby Isaac   forest->maxRefinement = maxRefinement;
352dd8e54a2SToby Isaac   PetscFunctionReturn(0);
353dd8e54a2SToby Isaac }
354dd8e54a2SToby Isaac 
355dd8e54a2SToby Isaac #undef __FUNCT__
356c7eeac06SToby Isaac #define __FUNCT__ "DMForestGetMaximumRefinement"
357c7eeac06SToby Isaac PetscErrorCode DMForestGetMaximumRefinement(DM dm, PetscInt *maxRefinement)
358dd8e54a2SToby Isaac {
359dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
360dd8e54a2SToby Isaac 
361dd8e54a2SToby Isaac   PetscFunctionBegin;
362dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
363c7eeac06SToby Isaac   PetscValidIntPointer(maxRefinement,2);
364c7eeac06SToby Isaac   *maxRefinement = forest->maxRefinement;
365dd8e54a2SToby Isaac   PetscFunctionReturn(0);
366dd8e54a2SToby Isaac }
367c7eeac06SToby Isaac 
368c7eeac06SToby Isaac #undef __FUNCT__
369c7eeac06SToby Isaac #define __FUNCT__ "DMForestSetAdaptivityStrategy"
370c7eeac06SToby Isaac PetscErrorCode DMForestSetAdaptivityStrategy(DM dm, DMForestAdaptivityStrategy adaptStrategy)
371c7eeac06SToby Isaac {
372c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
373c7eeac06SToby Isaac   PetscErrorCode ierr;
374c7eeac06SToby Isaac 
375c7eeac06SToby Isaac   PetscFunctionBegin;
376c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
377c7eeac06SToby Isaac   ierr = PetscFree(forest->adaptStrategy);CHKERRQ(ierr);
378a73e2921SToby Isaac   ierr = PetscStrallocpy((const char *) adaptStrategy,(char **)&forest->adaptStrategy);CHKERRQ(ierr);
379c7eeac06SToby Isaac   PetscFunctionReturn(0);
380c7eeac06SToby Isaac }
381c7eeac06SToby Isaac 
382c7eeac06SToby Isaac #undef __FUNCT__
383c7eeac06SToby Isaac #define __FUNCT__ "DMForestGetAdaptivityStrategy"
384c7eeac06SToby Isaac PetscErrorCode DMForestGetAdaptivityStrategy(DM dm, DMForestAdaptivityStrategy *adaptStrategy)
385c7eeac06SToby Isaac {
386c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
387c7eeac06SToby Isaac 
388c7eeac06SToby Isaac   PetscFunctionBegin;
389c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
390c7eeac06SToby Isaac   PetscValidPointer(adaptStrategy,2);
391c7eeac06SToby Isaac   *adaptStrategy = forest->adaptStrategy;
392c7eeac06SToby Isaac   PetscFunctionReturn(0);
393c7eeac06SToby Isaac }
394c7eeac06SToby Isaac 
395c7eeac06SToby Isaac #undef __FUNCT__
396c7eeac06SToby Isaac #define __FUNCT__ "DMForestSetGradeFactor"
397c7eeac06SToby Isaac PetscErrorCode DMForestSetGradeFactor(DM dm, PetscInt grade)
398c7eeac06SToby Isaac {
399c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
400c7eeac06SToby Isaac 
401c7eeac06SToby Isaac   PetscFunctionBegin;
402c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
403ef51cf95SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the grade factor after setup");
404c7eeac06SToby Isaac   forest->gradeFactor = grade;
405c7eeac06SToby Isaac   PetscFunctionReturn(0);
406c7eeac06SToby Isaac }
407c7eeac06SToby Isaac 
408c7eeac06SToby Isaac #undef __FUNCT__
409c7eeac06SToby Isaac #define __FUNCT__ "DMForestGetGradeFactor"
410c7eeac06SToby Isaac PetscErrorCode DMForestGetGradeFactor(DM dm, PetscInt *grade)
411c7eeac06SToby Isaac {
412c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
413c7eeac06SToby Isaac 
414c7eeac06SToby Isaac   PetscFunctionBegin;
415c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
416c7eeac06SToby Isaac   PetscValidIntPointer(grade,2);
417c7eeac06SToby Isaac   *grade = forest->gradeFactor;
418c7eeac06SToby Isaac   PetscFunctionReturn(0);
419c7eeac06SToby Isaac }
420c7eeac06SToby Isaac 
421c7eeac06SToby Isaac #undef __FUNCT__
422ef51cf95SToby Isaac #define __FUNCT__ "DMForestSetCellWeightFactor"
423ef51cf95SToby Isaac PetscErrorCode DMForestSetCellWeightFactor(DM dm, PetscReal weightsFactor)
424c7eeac06SToby Isaac {
425c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
426c7eeac06SToby Isaac 
427c7eeac06SToby Isaac   PetscFunctionBegin;
428c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
429ef51cf95SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the weights factor after setup");
430c7eeac06SToby Isaac   forest->weightsFactor = weightsFactor;
431c7eeac06SToby Isaac   PetscFunctionReturn(0);
432c7eeac06SToby Isaac }
433c7eeac06SToby Isaac 
434c7eeac06SToby Isaac #undef __FUNCT__
435ef51cf95SToby Isaac #define __FUNCT__ "DMForestGetCellWeightFactor"
436ef51cf95SToby Isaac PetscErrorCode DMForestGetCellWeightFactor(DM dm, PetscReal *weightsFactor)
437c7eeac06SToby Isaac {
438c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
439c7eeac06SToby Isaac 
440c7eeac06SToby Isaac   PetscFunctionBegin;
441c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
442c7eeac06SToby Isaac   PetscValidRealPointer(weightsFactor,2);
443c7eeac06SToby Isaac   *weightsFactor = forest->weightsFactor;
444c7eeac06SToby Isaac   PetscFunctionReturn(0);
445c7eeac06SToby Isaac }
446c7eeac06SToby Isaac 
447c7eeac06SToby Isaac #undef __FUNCT__
448c7eeac06SToby Isaac #define __FUNCT__ "DMForestGetCellChart"
449c7eeac06SToby Isaac PetscErrorCode DMForestGetCellChart(DM dm, PetscInt *cStart, PetscInt *cEnd)
450c7eeac06SToby Isaac {
451c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
452c7eeac06SToby Isaac   PetscErrorCode ierr;
453c7eeac06SToby Isaac 
454c7eeac06SToby Isaac   PetscFunctionBegin;
455c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
456c7eeac06SToby Isaac   PetscValidIntPointer(cStart,2);
457c7eeac06SToby Isaac   PetscValidIntPointer(cEnd,2);
458c7eeac06SToby Isaac   if (((forest->cStart == PETSC_DETERMINE) || (forest->cEnd == PETSC_DETERMINE)) && forest->createcellchart) {
459c7eeac06SToby Isaac     ierr = forest->createcellchart(dm,&forest->cStart,&forest->cEnd);CHKERRQ(ierr);
460c7eeac06SToby Isaac   }
461c7eeac06SToby Isaac   *cStart =  forest->cStart;
462c7eeac06SToby Isaac   *cEnd   =  forest->cEnd;
463c7eeac06SToby Isaac   PetscFunctionReturn(0);
464c7eeac06SToby Isaac }
465c7eeac06SToby Isaac 
466c7eeac06SToby Isaac #undef __FUNCT__
467c7eeac06SToby Isaac #define __FUNCT__ "DMForestGetCellSF"
468c7eeac06SToby Isaac PetscErrorCode DMForestGetCellSF(DM dm, PetscSF *cellSF)
469c7eeac06SToby Isaac {
470c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
471c7eeac06SToby Isaac   PetscErrorCode ierr;
472c7eeac06SToby Isaac 
473c7eeac06SToby Isaac   PetscFunctionBegin;
474c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
475c7eeac06SToby Isaac   PetscValidPointer(cellSF,2);
476c7eeac06SToby Isaac   if ((!forest->cellSF) && forest->createcellsf) {
477c7eeac06SToby Isaac     ierr = forest->createcellsf(dm,&forest->cellSF);CHKERRQ(ierr);
478c7eeac06SToby Isaac   }
479c7eeac06SToby Isaac   *cellSF = forest->cellSF;
480c7eeac06SToby Isaac   PetscFunctionReturn(0);
481c7eeac06SToby Isaac }
482c7eeac06SToby Isaac 
483c7eeac06SToby Isaac #undef __FUNCT__
484c7eeac06SToby Isaac #define __FUNCT__ "DMForestSetAdaptivityMarkers"
485c7eeac06SToby Isaac PetscErrorCode DMForestSetAdaptivityMarkers(DM dm, PetscInt markers[], PetscCopyMode copyMode)
486c7eeac06SToby Isaac {
487c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
488c7eeac06SToby Isaac   PetscInt       cStart, cEnd;
489c7eeac06SToby Isaac   PetscErrorCode ierr;
490c7eeac06SToby Isaac 
491c7eeac06SToby Isaac   PetscFunctionBegin;
492c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
493c7eeac06SToby Isaac   ierr = DMForestGetCellChart(dm,&cStart,&cEnd);CHKERRQ(ierr);
494c7eeac06SToby Isaac   if (cEnd < cStart) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"cell chart [%d,%d) is not valid",cStart,cEnd);
495c7eeac06SToby Isaac   if (copyMode == PETSC_COPY_VALUES) {
496c7eeac06SToby Isaac     if (forest->adaptCopyMode != PETSC_OWN_POINTER || forest->adaptMarkers == markers) {
497c7eeac06SToby Isaac       ierr = PetscMalloc1(cEnd-cStart,&forest->adaptMarkers);CHKERRQ(ierr);
498c7eeac06SToby Isaac     }
499c7eeac06SToby Isaac     ierr = PetscMemcpy(forest->adaptMarkers,markers,(cEnd-cStart)*sizeof(*markers));CHKERRQ(ierr);
500c7eeac06SToby Isaac     forest->adaptCopyMode = PETSC_OWN_POINTER;
501c7eeac06SToby Isaac     PetscFunctionReturn(0);
502c7eeac06SToby Isaac   }
503c7eeac06SToby Isaac   if (forest->adaptCopyMode == PETSC_OWN_POINTER) {
504c7eeac06SToby Isaac     ierr = PetscFree(forest->adaptMarkers);CHKERRQ(ierr);
505c7eeac06SToby Isaac   }
506c7eeac06SToby Isaac   forest->adaptMarkers  = markers;
507c7eeac06SToby Isaac   forest->adaptCopyMode = copyMode;
508c7eeac06SToby Isaac   PetscFunctionReturn(0);
509c7eeac06SToby Isaac }
510c7eeac06SToby Isaac 
511c7eeac06SToby Isaac #undef __FUNCT__
512c7eeac06SToby Isaac #define __FUNCT__ "DMForestGetAdaptivityMarkers"
513c7eeac06SToby Isaac PetscErrorCode DMForestGetAdaptivityMarkers(DM dm, PetscInt **markers)
514c7eeac06SToby Isaac {
515c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
516c7eeac06SToby Isaac 
517c7eeac06SToby Isaac   PetscFunctionBegin;
518c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
519c7eeac06SToby Isaac   PetscValidPointer(markers,2);
520c7eeac06SToby Isaac   *markers = forest->adaptMarkers;
521c7eeac06SToby Isaac   PetscFunctionReturn(0);
522c7eeac06SToby Isaac }
523c7eeac06SToby Isaac 
524c7eeac06SToby Isaac #undef __FUNCT__
525c7eeac06SToby Isaac #define __FUNCT__ "DMForestSetCellWeights"
526c7eeac06SToby Isaac PetscErrorCode DMForestSetCellWeights(DM dm, PetscReal weights[], PetscCopyMode copyMode)
527c7eeac06SToby Isaac {
528c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
529c7eeac06SToby Isaac   PetscInt       cStart, cEnd;
530c7eeac06SToby Isaac   PetscErrorCode ierr;
531c7eeac06SToby Isaac 
532c7eeac06SToby Isaac   PetscFunctionBegin;
533c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
534c7eeac06SToby Isaac   ierr = DMForestGetCellChart(dm,&cStart,&cEnd);CHKERRQ(ierr);
535c7eeac06SToby Isaac   if (cEnd < cStart) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"cell chart [%d,%d) is not valid",cStart,cEnd);
536c7eeac06SToby Isaac   if (copyMode == PETSC_COPY_VALUES) {
537c7eeac06SToby Isaac     if (forest->cellWeightsCopyMode != PETSC_OWN_POINTER || forest->cellWeights == weights) {
538c7eeac06SToby Isaac       ierr = PetscMalloc1(cEnd-cStart,&forest->cellWeights);CHKERRQ(ierr);
539c7eeac06SToby Isaac     }
540c7eeac06SToby Isaac     ierr = PetscMemcpy(forest->cellWeights,weights,(cEnd-cStart)*sizeof(*weights));CHKERRQ(ierr);
541c7eeac06SToby Isaac     forest->cellWeightsCopyMode = PETSC_OWN_POINTER;
542c7eeac06SToby Isaac     PetscFunctionReturn(0);
543c7eeac06SToby Isaac   }
544c7eeac06SToby Isaac   if (forest->cellWeightsCopyMode == PETSC_OWN_POINTER) {
545c7eeac06SToby Isaac     ierr = PetscFree(forest->cellWeights);CHKERRQ(ierr);
546c7eeac06SToby Isaac   }
547c7eeac06SToby Isaac   forest->cellWeights  = weights;
548c7eeac06SToby Isaac   forest->cellWeightsCopyMode = copyMode;
549c7eeac06SToby Isaac   PetscFunctionReturn(0);
550c7eeac06SToby Isaac }
551c7eeac06SToby Isaac 
552c7eeac06SToby Isaac #undef __FUNCT__
553c7eeac06SToby Isaac #define __FUNCT__ "DMForestGetCellWeights"
554c7eeac06SToby Isaac PetscErrorCode DMForestGetCellWeights(DM dm, PetscReal **weights)
555c7eeac06SToby Isaac {
556c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
557c7eeac06SToby Isaac 
558c7eeac06SToby Isaac   PetscFunctionBegin;
559c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
560c7eeac06SToby Isaac   PetscValidPointer(weights,2);
561c7eeac06SToby Isaac   *weights = forest->cellWeights;
562c7eeac06SToby Isaac   PetscFunctionReturn(0);
563c7eeac06SToby Isaac }
564c7eeac06SToby Isaac 
565c7eeac06SToby Isaac #undef __FUNCT__
566c7eeac06SToby Isaac #define __FUNCT__ "DMForestSetWeightCapacity"
567c7eeac06SToby Isaac PetscErrorCode DMForestSetWeightCapacity(DM dm, PetscReal capacity)
568c7eeac06SToby Isaac {
569c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
570c7eeac06SToby Isaac 
571c7eeac06SToby Isaac   PetscFunctionBegin;
572c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
573ef51cf95SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the weight capacity after setup");
574c7eeac06SToby Isaac   if (capacity < 0.) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"Cannot have negative weight capacity; %f",capacity);
575c7eeac06SToby Isaac   forest->weightCapacity = capacity;
576c7eeac06SToby Isaac   PetscFunctionReturn(0);
577c7eeac06SToby Isaac }
578c7eeac06SToby Isaac 
579c7eeac06SToby Isaac #undef __FUNCT__
580c7eeac06SToby Isaac #define __FUNCT__ "DMForestGetWeightCapacity"
581c7eeac06SToby Isaac PetscErrorCode DMForestGetWeightCapacity(DM dm, PetscReal *capacity)
582c7eeac06SToby Isaac {
583c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
584c7eeac06SToby Isaac 
585c7eeac06SToby Isaac   PetscFunctionBegin;
586c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
587c7eeac06SToby Isaac   PetscValidRealPointer(capacity,2);
588c7eeac06SToby Isaac   *capacity = forest->weightCapacity;
589c7eeac06SToby Isaac   PetscFunctionReturn(0);
590c7eeac06SToby Isaac }
591c7eeac06SToby Isaac 
592dd8e54a2SToby Isaac #undef __FUNCT__
593db4d5e8cSToby Isaac #define __FUNCT__ "DMSetFromOptions_Forest"
5945c8434f9SToby Isaac PETSC_EXTERN PetscErrorCode DMSetFromOptions_Forest(PetscOptions *PetscOptionsObject,DM dm)
595db4d5e8cSToby Isaac {
596db4d5e8cSToby Isaac   DM_Forest                  *forest = (DM_Forest *) dm->data;
59756ba9f64SToby Isaac   PetscBool                  flg, flg1, flg2, flg3, flg4;
598dd8e54a2SToby Isaac   DMForestTopology           oldTopo;
599c7eeac06SToby Isaac   char                       stringBuffer[256];
600dd8e54a2SToby Isaac   PetscViewer                viewer;
601dd8e54a2SToby Isaac   PetscViewerFormat          format;
60256ba9f64SToby Isaac   PetscInt                   adjDim, adjCodim, overlap, minRefinement, initRefinement, maxRefinement, grade;
603c7eeac06SToby Isaac   PetscReal                  weightsFactor;
604c7eeac06SToby Isaac   DMForestAdaptivityStrategy adaptStrategy;
605db4d5e8cSToby Isaac   PetscErrorCode             ierr;
606db4d5e8cSToby Isaac 
607db4d5e8cSToby Isaac   PetscFunctionBegin;
608db4d5e8cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
609db4d5e8cSToby Isaac   forest->setFromOptions = PETSC_TRUE;
6105c8434f9SToby Isaac   ierr = PetscOptionsHead(PetscOptionsObject,"DMForest Options");CHKERRQ(ierr);
611dd8e54a2SToby Isaac   ierr = DMForestGetTopology(dm, &oldTopo);CHKERRQ(ierr);
61256ba9f64SToby Isaac   ierr = PetscOptionsString("-dm_forest_topology","the topology of the forest's base mesh","DMForestSetTopology",oldTopo,stringBuffer,256,&flg1);CHKERRQ(ierr);
61356ba9f64SToby Isaac   ierr = PetscOptionsViewer("-dm_forest_base_dm","load the base DM from a viewer specification","DMForestSetBaseDM",&viewer,&format,&flg2);CHKERRQ(ierr);
61456ba9f64SToby Isaac   ierr = PetscOptionsViewer("-dm_forest_coarse_forest","load the coarse forest from a viewer specification","DMForestSetCoarseForest",&viewer,&format,&flg3);CHKERRQ(ierr);
61556ba9f64SToby Isaac   ierr = PetscOptionsViewer("-dm_forest_fine_forest","load the fine forest from a viewer specification","DMForestSetFineForest",&viewer,&format,&flg4);CHKERRQ(ierr);
61656ba9f64SToby Isaac   if ((PetscInt) flg1 + (PetscInt) flg2 + (PetscInt) flg3 + (PetscInt) flg4 > 1) {
61756ba9f64SToby Isaac     SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_INCOMP,"Specify only one of -dm_forest_{topology,base_dm,coarse_forest,fine_forest}");
618dd8e54a2SToby Isaac   }
61956ba9f64SToby Isaac   if (flg1) {
62056ba9f64SToby Isaac     ierr = DMForestSetTopology(dm,(DMForestTopology)stringBuffer);CHKERRQ(ierr);
62156ba9f64SToby Isaac     ierr = DMForestSetBaseDM(dm,NULL);CHKERRQ(ierr);
62256ba9f64SToby Isaac     ierr = DMForestSetCoarseForest(dm,NULL);CHKERRQ(ierr);
62356ba9f64SToby Isaac     ierr = DMForestSetFineForest(dm,NULL);CHKERRQ(ierr);
62456ba9f64SToby Isaac   }
62556ba9f64SToby Isaac   if (flg2) {
626dd8e54a2SToby Isaac     DM         base;
627dd8e54a2SToby Isaac 
628dd8e54a2SToby Isaac     ierr = DMCreate(PetscObjectComm((PetscObject)dm),&base);CHKERRQ(ierr);
629dd8e54a2SToby Isaac     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
630dd8e54a2SToby Isaac     ierr = DMLoad(base,viewer);CHKERRQ(ierr);
631dd8e54a2SToby Isaac     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
632dd8e54a2SToby Isaac     ierr = DMForestSetBaseDM(dm,base);CHKERRQ(ierr);
633dd8e54a2SToby Isaac     ierr = DMDestroy(&base);CHKERRQ(ierr);
63456ba9f64SToby Isaac     ierr = DMForestSetTopology(dm,NULL);CHKERRQ(ierr);
63556ba9f64SToby Isaac     ierr = DMForestSetCoarseForest(dm,NULL);CHKERRQ(ierr);
63656ba9f64SToby Isaac     ierr = DMForestSetFineForest(dm,NULL);CHKERRQ(ierr);
637dd8e54a2SToby Isaac   }
63856ba9f64SToby Isaac   if (flg3) {
639dd8e54a2SToby Isaac     DM         coarse;
640dd8e54a2SToby Isaac 
641dd8e54a2SToby Isaac     ierr = DMCreate(PetscObjectComm((PetscObject)dm),&coarse);CHKERRQ(ierr);
642dd8e54a2SToby Isaac     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
643dd8e54a2SToby Isaac     ierr = DMLoad(coarse,viewer);CHKERRQ(ierr);
644dd8e54a2SToby Isaac     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
645dd8e54a2SToby Isaac     ierr = DMForestSetCoarseForest(dm,coarse);CHKERRQ(ierr);
646dd8e54a2SToby Isaac     ierr = DMDestroy(&coarse);CHKERRQ(ierr);
64756ba9f64SToby Isaac     ierr = DMForestSetTopology(dm,NULL);CHKERRQ(ierr);
64856ba9f64SToby Isaac     ierr = DMForestSetBaseDM(dm,NULL);CHKERRQ(ierr);
64956ba9f64SToby Isaac     ierr = DMForestSetFineForest(dm,NULL);CHKERRQ(ierr);
650dd8e54a2SToby Isaac   }
65156ba9f64SToby Isaac   if (flg4) {
652dd8e54a2SToby Isaac     DM         fine;
653dd8e54a2SToby Isaac 
654dd8e54a2SToby Isaac     ierr = DMCreate(PetscObjectComm((PetscObject)dm),&fine);CHKERRQ(ierr);
655dd8e54a2SToby Isaac     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
656dd8e54a2SToby Isaac     ierr = DMLoad(fine,viewer);CHKERRQ(ierr);
657dd8e54a2SToby Isaac     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
658dd8e54a2SToby Isaac     ierr = DMForestSetFineForest(dm,fine);CHKERRQ(ierr);
659dd8e54a2SToby Isaac     ierr = DMDestroy(&fine);CHKERRQ(ierr);
66056ba9f64SToby Isaac     ierr = DMForestSetTopology(dm,NULL);CHKERRQ(ierr);
66156ba9f64SToby Isaac     ierr = DMForestSetBaseDM(dm,NULL);CHKERRQ(ierr);
66256ba9f64SToby Isaac     ierr = DMForestSetCoarseForest(dm,NULL);CHKERRQ(ierr);
663dd8e54a2SToby Isaac   }
664dd8e54a2SToby Isaac   ierr = DMForestGetAdjacencyDimension(dm,&adjDim);CHKERRQ(ierr);
665dd8e54a2SToby Isaac   ierr = PetscOptionsInt("-dm_forest_adjacency_dimension","set the dimension of points that define adjacency in the forest","DMForestSetAdjacencyDimension",adjDim,&adjDim,&flg);CHKERRQ(ierr);
666dd8e54a2SToby Isaac   if (flg) {
667dd8e54a2SToby Isaac     ierr = DMForestSetAdjacencyDimension(dm,adjDim);CHKERRQ(ierr);
668dd8e54a2SToby Isaac   }
669dd8e54a2SToby Isaac   else {
670dd8e54a2SToby Isaac     ierr = DMForestGetAdjacencyCodimension(dm,&adjCodim);CHKERRQ(ierr);
671dd8e54a2SToby Isaac     ierr = PetscOptionsInt("-dm_forest_adjacency_codimension","set the codimension of points that define adjacency in the forest","DMForestSetAdjacencyCodimension",adjCodim,&adjCodim,&flg);CHKERRQ(ierr);
672dd8e54a2SToby Isaac     if (flg) {
673dd8e54a2SToby Isaac       ierr = DMForestSetAdjacencyCodimension(dm,adjCodim);CHKERRQ(ierr);
674dd8e54a2SToby Isaac     }
675dd8e54a2SToby Isaac   }
676dd8e54a2SToby Isaac   ierr = DMForestGetPartitionOverlap(dm,&overlap);CHKERRQ(ierr);
677dd8e54a2SToby Isaac   ierr = PetscOptionsInt("-dm_forest_partition_overlap","set the degree of partition overlap","DMForestSetPartitionOverlap",overlap,&overlap,&flg);CHKERRQ(ierr);
678dd8e54a2SToby Isaac   if (flg) {
679dd8e54a2SToby Isaac     ierr = DMForestSetPartitionOverlap(dm,overlap);CHKERRQ(ierr);
680dd8e54a2SToby Isaac   }
681dd8e54a2SToby Isaac   ierr = DMForestGetMinimumRefinement(dm,&minRefinement);CHKERRQ(ierr);
682dd8e54a2SToby Isaac   ierr = PetscOptionsInt("-dm_forest_minimum_refinement","set the minimum level of refinement in the forest","DMForestSetMinimumRefinement",minRefinement,&minRefinement,&flg);CHKERRQ(ierr);
683dd8e54a2SToby Isaac   if (flg) {
684dd8e54a2SToby Isaac     ierr = DMForestSetMinimumRefinement(dm,minRefinement);CHKERRQ(ierr);
685db4d5e8cSToby Isaac   }
68656ba9f64SToby Isaac   ierr = DMForestGetInitialRefinement(dm,&initRefinement);CHKERRQ(ierr);
68756ba9f64SToby Isaac   ierr = PetscOptionsInt("-dm_forest_initial_refinement","set the initial level of refinement in the forest","DMForestSetInitialRefinement",initRefinement,&initRefinement,&flg);CHKERRQ(ierr);
68856ba9f64SToby Isaac   if (flg) {
68956ba9f64SToby Isaac     ierr = DMForestSetInitialRefinement(dm,initRefinement);CHKERRQ(ierr);
69056ba9f64SToby Isaac   }
691c7eeac06SToby Isaac   ierr = DMForestGetMaximumRefinement(dm,&maxRefinement);CHKERRQ(ierr);
692c7eeac06SToby Isaac   ierr = PetscOptionsInt("-dm_forest_maximum_refinement","set the maximum level of refinement in the forest","DMForestSetMaximumRefinement",maxRefinement,&maxRefinement,&flg);CHKERRQ(ierr);
693c7eeac06SToby Isaac   if (flg) {
694c7eeac06SToby Isaac     ierr = DMForestSetMaximumRefinement(dm,maxRefinement);CHKERRQ(ierr);
695c7eeac06SToby Isaac   }
696c7eeac06SToby Isaac   ierr = DMForestGetAdaptivityStrategy(dm,&adaptStrategy);CHKERRQ(ierr);
697c7eeac06SToby Isaac   ierr = PetscOptionsString("-dm_forest_adaptivity_strategy","the forest's adaptivity-flag resolution strategy","DMForestSetAdaptivityStrategy",adaptStrategy,stringBuffer,256,&flg);CHKERRQ(ierr);
698c7eeac06SToby Isaac   if (flg) {
699c7eeac06SToby Isaac     ierr = DMForestSetAdaptivityStrategy(dm,(DMForestAdaptivityStrategy)stringBuffer);CHKERRQ(ierr);
700c7eeac06SToby Isaac   }
701c7eeac06SToby Isaac   ierr = DMForestGetGradeFactor(dm,&grade);CHKERRQ(ierr);
702c7eeac06SToby Isaac   ierr = PetscOptionsInt("-dm_forest_grade_factor","grade factor between neighboring cells","DMForestSetGradeFactor",grade,&grade,&flg);CHKERRQ(ierr);
703c7eeac06SToby Isaac   if (flg) {
704c7eeac06SToby Isaac     ierr = DMForestSetGradeFactor(dm,grade);CHKERRQ(ierr);
705c7eeac06SToby Isaac   }
706c7eeac06SToby Isaac   ierr = DMForestGetCellWeightFactor(dm,&weightsFactor);CHKERRQ(ierr);
707c7eeac06SToby Isaac   ierr = PetscOptionsReal("-dm_forest_cell_weight_factor","multiplying weight factor for cell refinement","DMForestSetCellWeightFactor",weightsFactor,&weightsFactor,&flg);CHKERRQ(ierr);
708c7eeac06SToby Isaac   if (flg) {
709c7eeac06SToby Isaac     ierr = DMForestSetCellWeightFactor(dm,weightsFactor);CHKERRQ(ierr);
710c7eeac06SToby Isaac   }
711db4d5e8cSToby Isaac   ierr = PetscOptionsTail();CHKERRQ(ierr);
712db4d5e8cSToby Isaac   PetscFunctionReturn(0);
713db4d5e8cSToby Isaac }
714db4d5e8cSToby Isaac 
715db4d5e8cSToby Isaac #undef __FUNCT__
716d222f98bSToby Isaac #define __FUNCT__ "DMInitialize_Forest"
717d222f98bSToby Isaac static PetscErrorCode DMInitialize_Forest(DM dm)
718d222f98bSToby Isaac {
719d222f98bSToby Isaac   PetscErrorCode ierr;
720d222f98bSToby Isaac 
721d222f98bSToby Isaac   PetscFunctionBegin;
722d222f98bSToby Isaac   ierr = PetscMemzero(dm->ops,sizeof(*(dm->ops)));CHKERRQ(ierr);
723d222f98bSToby Isaac 
724d222f98bSToby Isaac   dm->ops->clone          = DMClone_Forest;
725d222f98bSToby Isaac   dm->ops->setfromoptions = DMSetFromOptions_Forest;
726d222f98bSToby Isaac   dm->ops->destroy        = DMDestroy_Forest;
727d222f98bSToby Isaac   PetscFunctionReturn(0);
728d222f98bSToby Isaac }
729d222f98bSToby Isaac 
730d222f98bSToby Isaac #undef __FUNCT__
731db4d5e8cSToby Isaac #define __FUNCT__ "DMCreate_Forest"
732db4d5e8cSToby Isaac PETSC_EXTERN PetscErrorCode DMCreate_Forest(DM dm)
733db4d5e8cSToby Isaac {
734db4d5e8cSToby Isaac   DM_Forest      *forest;
735db4d5e8cSToby Isaac   PetscErrorCode ierr;
736db4d5e8cSToby Isaac 
737db4d5e8cSToby Isaac   PetscFunctionBegin;
738db4d5e8cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
739db4d5e8cSToby Isaac   ierr                        = PetscNewLog(dm,&forest);CHKERRQ(ierr);
740db4d5e8cSToby Isaac   dm->dim                     = 0;
741db4d5e8cSToby Isaac   dm->data                    = forest;
742db4d5e8cSToby Isaac   forest->refct               = 1;
743db4d5e8cSToby Isaac   forest->data                = NULL;
744dd8e54a2SToby Isaac   forest->setFromOptions      = PETSC_FALSE;
745db4d5e8cSToby Isaac   forest->topology            = NULL;
746db4d5e8cSToby Isaac   forest->base                = NULL;
747db4d5e8cSToby Isaac   forest->adjDim              = PETSC_DEFAULT;
748db4d5e8cSToby Isaac   forest->overlap             = PETSC_DEFAULT;
749db4d5e8cSToby Isaac   forest->minRefinement       = PETSC_DEFAULT;
750db4d5e8cSToby Isaac   forest->maxRefinement       = PETSC_DEFAULT;
75156ba9f64SToby Isaac   forest->initRefinement      = PETSC_DEFAULT;
752c7eeac06SToby Isaac   forest->cStart              = PETSC_DETERMINE;
753c7eeac06SToby Isaac   forest->cEnd                = PETSC_DETERMINE;
754db4d5e8cSToby Isaac   forest->cellSF              = 0;
755db4d5e8cSToby Isaac   forest->adaptMarkers        = NULL;
756db4d5e8cSToby Isaac   forest->adaptCopyMode       = PETSC_USE_POINTER;
757db4d5e8cSToby Isaac   forest->gradeFactor         = 2;
758db4d5e8cSToby Isaac   forest->cellWeights         = NULL;
759db4d5e8cSToby Isaac   forest->cellWeightsCopyMode = PETSC_USE_POINTER;
760db4d5e8cSToby Isaac   forest->weightsFactor       = 1.;
761db4d5e8cSToby Isaac   forest->weightCapacity      = 1.;
762a73e2921SToby Isaac   ierr = DMForestSetAdaptivityStrategy(dm,DMFORESTADAPTALL);CHKERRQ(ierr);
763d222f98bSToby Isaac   ierr = DMInitialize_Forest(dm);CHKERRQ(ierr);
764db4d5e8cSToby Isaac   PetscFunctionReturn(0);
765db4d5e8cSToby Isaac }
766db4d5e8cSToby Isaac 
767