xref: /petsc/src/dm/impls/forest/forest.c (revision 30f902e79a0cc6523323dfd84cf2b133f28f8f5a) !
1ef19d27cSToby Isaac #include <petsc-private/dmforestimpl.h> /*I petscdmforest.h I*/
2ef19d27cSToby Isaac #include <petsc-private/dmimpl.h>       /*I petscdm.h */
3ef19d27cSToby Isaac #include <petscsf.h>
4db4d5e8cSToby Isaac 
5db4d5e8cSToby Isaac #undef __FUNCT__
6db4d5e8cSToby Isaac #define __FUNCT__ "DMClone_Forest"
7db4d5e8cSToby Isaac PETSC_EXTERN PetscErrorCode DMClone_Forest(DM dm, DM *newdm)
8db4d5e8cSToby Isaac {
9db4d5e8cSToby Isaac   DM_Forest        *forest = (DM_Forest *) dm->data;
10db4d5e8cSToby Isaac   const char       *type;
11db4d5e8cSToby Isaac   PetscErrorCode ierr;
12db4d5e8cSToby Isaac 
13db4d5e8cSToby Isaac   PetscFunctionBegin;
14db4d5e8cSToby Isaac   forest->refct++;
15db4d5e8cSToby Isaac   (*newdm)->data = forest;
16db4d5e8cSToby Isaac   ierr = PetscObjectGetType((PetscObject) dm, &type);CHKERRQ(ierr);
17db4d5e8cSToby Isaac   ierr = PetscObjectChangeTypeName((PetscObject) *newdm, type);CHKERRQ(ierr);
18db4d5e8cSToby Isaac   PetscFunctionReturn(0);
19db4d5e8cSToby Isaac }
20db4d5e8cSToby Isaac 
21db4d5e8cSToby Isaac #undef __FUNCT__
22db4d5e8cSToby Isaac #define __FUNCT__ "DMDestroy_Forest"
23d222f98bSToby Isaac static PetscErrorCode DMDestroy_Forest(DM dm)
24db4d5e8cSToby Isaac {
25db4d5e8cSToby Isaac   DM_Forest     *forest = (DM_Forest*) dm->data;
26db4d5e8cSToby Isaac   PetscErrorCode ierr;
27db4d5e8cSToby Isaac 
28db4d5e8cSToby Isaac   PetscFunctionBegin;
29db4d5e8cSToby Isaac   if (--forest->refct > 0) PetscFunctionReturn(0);
30d222f98bSToby Isaac   if (forest->destroy) {ierr = forest->destroy(dm);CHKERRQ(ierr);}
31db4d5e8cSToby Isaac   ierr = PetscSFDestroy(&forest->cellSF);CHKERRQ(ierr);
32db4d5e8cSToby Isaac   if (forest->adaptCopyMode == PETSC_OWN_POINTER) {
33db4d5e8cSToby Isaac     ierr = PetscFree(forest->adaptMarkers);CHKERRQ(ierr);
34db4d5e8cSToby Isaac   }
35db4d5e8cSToby Isaac   if (forest->cellWeightsCopyMode == PETSC_OWN_POINTER) {
36db4d5e8cSToby Isaac     ierr = PetscFree(forest->cellWeights);CHKERRQ(ierr);
37db4d5e8cSToby Isaac   }
38*30f902e7SToby Isaac   ierr = PetscFree(forest->topology);CHKERRQ(ierr);
39*30f902e7SToby Isaac   ierr = PetscFree(forest);CHKERRQ(ierr);
40db4d5e8cSToby Isaac   PetscFunctionReturn(0);
41db4d5e8cSToby Isaac }
42db4d5e8cSToby Isaac 
43db4d5e8cSToby Isaac #undef __FUNCT__
44dd8e54a2SToby Isaac #define __FUNCT__ "DMForestSetTopology"
45dd8e54a2SToby Isaac PetscErrorCode DMForestSetTopology(DM dm, DMForestTopology topology)
46db4d5e8cSToby Isaac {
47db4d5e8cSToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
48db4d5e8cSToby Isaac   PetscErrorCode ierr;
49db4d5e8cSToby Isaac 
50db4d5e8cSToby Isaac   PetscFunctionBegin;
51db4d5e8cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52ef51cf95SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the topology after setup");
53dd8e54a2SToby Isaac   ierr = PetscFree(forest->topology);CHKERRQ(ierr);
54dd8e54a2SToby Isaac   ierr = PetscStrallocpy((const char *)topology,(char **) &forest->topology);CHKERRQ(ierr);
55db4d5e8cSToby Isaac   PetscFunctionReturn(0);
56db4d5e8cSToby Isaac }
57db4d5e8cSToby Isaac 
58db4d5e8cSToby Isaac #undef __FUNCT__
59dd8e54a2SToby Isaac #define __FUNCT__ "DMForestGetTopology"
60dd8e54a2SToby Isaac PetscErrorCode DMForestGetTopology(DM dm, DMForestTopology *topology)
61dd8e54a2SToby Isaac {
62dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
63dd8e54a2SToby Isaac 
64dd8e54a2SToby Isaac   PetscFunctionBegin;
65dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66dd8e54a2SToby Isaac   PetscValidPointer(topology,2);
67dd8e54a2SToby Isaac   *topology = forest->topology;
68dd8e54a2SToby Isaac   PetscFunctionReturn(0);
69dd8e54a2SToby Isaac }
70dd8e54a2SToby Isaac 
71dd8e54a2SToby Isaac #undef __FUNCT__
72dd8e54a2SToby Isaac #define __FUNCT__ "DMForestSetBaseDM"
73dd8e54a2SToby Isaac PetscErrorCode DMForestSetBaseDM(DM dm, DM base)
74dd8e54a2SToby Isaac {
75dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
76dd8e54a2SToby Isaac   PetscInt       dim, dimEmbed;
77dd8e54a2SToby Isaac   PetscErrorCode ierr;
78dd8e54a2SToby Isaac 
79dd8e54a2SToby Isaac   PetscFunctionBegin;
80dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
81dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
82ef51cf95SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the base after setup");
83dd8e54a2SToby Isaac   ierr = PetscObjectReference((PetscObject)base);CHKERRQ(ierr);
84dd8e54a2SToby Isaac   ierr = DMDestroy(&forest->base);CHKERRQ(ierr);
85dd8e54a2SToby Isaac   forest->base = base;
86dd8e54a2SToby Isaac   ierr = DMGetDimension(base,&dim);CHKERRQ(ierr);
87dd8e54a2SToby Isaac   ierr = DMSetDimension(dm,dim);CHKERRQ(ierr);
88dd8e54a2SToby Isaac   ierr = DMGetCoordinateDim(base,&dimEmbed);CHKERRQ(ierr);
89dd8e54a2SToby Isaac   ierr = DMSetCoordinateDim(dm,dimEmbed);CHKERRQ(ierr);
90dd8e54a2SToby Isaac   PetscFunctionReturn(0);
91dd8e54a2SToby Isaac }
92dd8e54a2SToby Isaac 
93dd8e54a2SToby Isaac #undef __FUNCT__
94dd8e54a2SToby Isaac #define __FUNCT__ "DMForestGetBaseDM"
95dd8e54a2SToby Isaac PetscErrorCode DMForestGetBaseDM(DM dm, DM *base)
96dd8e54a2SToby Isaac {
97dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
98dd8e54a2SToby Isaac 
99dd8e54a2SToby Isaac   PetscFunctionBegin;
100dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
101dd8e54a2SToby Isaac   PetscValidPointer(base, 2);
102dd8e54a2SToby Isaac   *base = forest->base;
103dd8e54a2SToby Isaac   PetscFunctionReturn(0);
104dd8e54a2SToby Isaac }
105dd8e54a2SToby Isaac 
106dd8e54a2SToby Isaac #undef __FUNCT__
107dd8e54a2SToby Isaac #define __FUNCT__ "DMForestSetCoarseForest"
108dd8e54a2SToby Isaac PetscErrorCode DMForestSetCoarseForest(DM dm,DM coarse)
109dd8e54a2SToby Isaac {
110dd8e54a2SToby Isaac   DM_Forest        *forest       = (DM_Forest *) dm->data;
111dd8e54a2SToby Isaac   DM               base;
112d222f98bSToby Isaac   DMForestTopology topology;
113dd8e54a2SToby Isaac   PetscErrorCode   ierr;
114dd8e54a2SToby Isaac 
115dd8e54a2SToby Isaac   PetscFunctionBegin;
116dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
117dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
118ef51cf95SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the coarse forest after setup");
119ef51cf95SToby Isaac   if (!coarse->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot set a coarse forest that is not set up");
120dd8e54a2SToby Isaac   ierr = PetscObjectReference((PetscObject)coarse);CHKERRQ(ierr);
121dd8e54a2SToby Isaac   ierr = DMDestroy(&forest->coarse);CHKERRQ(ierr);
122dd8e54a2SToby Isaac   ierr = DMForestGetBaseDM(coarse,&base);CHKERRQ(ierr);
123dd8e54a2SToby Isaac   ierr = DMForestSetBaseDM(dm,base);CHKERRQ(ierr);
124d222f98bSToby Isaac   ierr = DMForestGetTopology(coarse,&topology);CHKERRQ(ierr);
125d222f98bSToby Isaac   ierr = DMForestSetTopology(dm,topology);CHKERRQ(ierr);
126dd8e54a2SToby Isaac   forest->coarse = coarse;
127dd8e54a2SToby Isaac   PetscFunctionReturn(0);
128dd8e54a2SToby Isaac }
129dd8e54a2SToby Isaac 
130dd8e54a2SToby Isaac #undef __FUNCT__
131dd8e54a2SToby Isaac #define __FUNCT__ "DMForestGetCoarseForest"
132dd8e54a2SToby Isaac PetscErrorCode DMForestGetCoarseForest(DM dm, DM *coarse)
133dd8e54a2SToby Isaac {
134dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
135dd8e54a2SToby Isaac 
136dd8e54a2SToby Isaac   PetscFunctionBegin;
137dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
138dd8e54a2SToby Isaac   PetscValidPointer(coarse, 2);
139dd8e54a2SToby Isaac   *coarse = forest->coarse;
140dd8e54a2SToby Isaac   PetscFunctionReturn(0);
141dd8e54a2SToby Isaac }
142dd8e54a2SToby Isaac 
143dd8e54a2SToby Isaac #undef __FUNCT__
144dd8e54a2SToby Isaac #define __FUNCT__ "DMForestSetFineForest"
145dd8e54a2SToby Isaac PetscErrorCode DMForestSetFineForest(DM dm,DM fine)
146dd8e54a2SToby Isaac {
147dd8e54a2SToby Isaac   DM_Forest        *forest = (DM_Forest *) dm->data;
148dd8e54a2SToby Isaac   DM               base;
149d222f98bSToby Isaac   DMForestTopology topology;
150dd8e54a2SToby Isaac   PetscErrorCode   ierr;
151dd8e54a2SToby Isaac 
152dd8e54a2SToby Isaac   PetscFunctionBegin;
153dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
154dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
155ef51cf95SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the fine forest after setup");
156ef51cf95SToby Isaac   if (!fine->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot set a fine forest that is not set up");
157dd8e54a2SToby Isaac   ierr = PetscObjectReference((PetscObject)fine);CHKERRQ(ierr);
158dd8e54a2SToby Isaac   ierr = DMDestroy(&forest->fine);CHKERRQ(ierr);
159dd8e54a2SToby Isaac   ierr = DMForestGetBaseDM(fine,&base);CHKERRQ(ierr);
160dd8e54a2SToby Isaac   ierr = DMForestSetBaseDM(dm,base);CHKERRQ(ierr);
161d222f98bSToby Isaac   ierr = DMForestGetTopology(fine,&topology);CHKERRQ(ierr);
162d222f98bSToby Isaac   ierr = DMForestSetTopology(dm,topology);CHKERRQ(ierr);
163dd8e54a2SToby Isaac   forest->fine = fine;
164dd8e54a2SToby Isaac   PetscFunctionReturn(0);
165dd8e54a2SToby Isaac }
166dd8e54a2SToby Isaac 
167dd8e54a2SToby Isaac #undef __FUNCT__
168ef51cf95SToby Isaac #define __FUNCT__ "DMForestGetFineForest"
169dd8e54a2SToby Isaac PetscErrorCode DMForestGetFineForest(DM dm, DM *fine)
170dd8e54a2SToby Isaac {
171dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
172dd8e54a2SToby Isaac 
173dd8e54a2SToby Isaac   PetscFunctionBegin;
174dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
175dd8e54a2SToby Isaac   PetscValidPointer(fine, 2);
176dd8e54a2SToby Isaac   *fine = forest->fine;
177dd8e54a2SToby Isaac   PetscFunctionReturn(0);
178dd8e54a2SToby Isaac }
179dd8e54a2SToby Isaac 
180dd8e54a2SToby Isaac #undef __FUNCT__
181dd8e54a2SToby Isaac #define __FUNCT__ "DMForestSetAdjacencyDimension"
182dd8e54a2SToby Isaac PetscErrorCode DMForestSetAdjacencyDimension(DM dm, PetscInt adjDim)
183dd8e54a2SToby Isaac {
184dd8e54a2SToby Isaac   PetscInt        dim;
185dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
186dd8e54a2SToby Isaac   PetscErrorCode  ierr;
187dd8e54a2SToby Isaac 
188dd8e54a2SToby Isaac   PetscFunctionBegin;
189dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
190ef51cf95SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the adjacency dimension after setup");
191dd8e54a2SToby Isaac   if (adjDim < 0) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"adjacency dim cannot be < 0: %d", adjDim);
192dd8e54a2SToby Isaac   ierr = DMGetDimension(dm,&dim);CHKERRQ(ierr);
193dd8e54a2SToby Isaac   if (adjDim > dim) SETERRQ2(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"adjacency dim cannot be > %d: %d", dim, adjDim);
194dd8e54a2SToby Isaac   forest->adjDim = adjDim;
195dd8e54a2SToby Isaac   PetscFunctionReturn(0);
196dd8e54a2SToby Isaac }
197dd8e54a2SToby Isaac 
198dd8e54a2SToby Isaac #undef __FUNCT__
199dd8e54a2SToby Isaac #define __FUNCT__ "DMForestSetAdjacencyCodimension"
200dd8e54a2SToby Isaac PetscErrorCode DMForestSetAdjacencyCodimension(DM dm, PetscInt adjCodim)
201dd8e54a2SToby Isaac {
202dd8e54a2SToby Isaac   PetscInt        dim;
203dd8e54a2SToby Isaac   PetscErrorCode  ierr;
204dd8e54a2SToby Isaac 
205dd8e54a2SToby Isaac   PetscFunctionBegin;
206dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
207dd8e54a2SToby Isaac   ierr = DMGetDimension(dm,&dim);CHKERRQ(ierr);
208dd8e54a2SToby Isaac   ierr = DMForestSetAdjacencyDimension(dm,dim-adjCodim);CHKERRQ(ierr);
209dd8e54a2SToby Isaac   PetscFunctionReturn(0);
210dd8e54a2SToby Isaac }
211dd8e54a2SToby Isaac 
212dd8e54a2SToby Isaac #undef __FUNCT__
213dd8e54a2SToby Isaac #define __FUNCT__ "DMForestGetAdjacencyDimension"
214dd8e54a2SToby Isaac PetscErrorCode DMForestGetAdjacencyDimension(DM dm, PetscInt *adjDim)
215dd8e54a2SToby Isaac {
216dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
217dd8e54a2SToby Isaac 
218dd8e54a2SToby Isaac   PetscFunctionBegin;
219dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
220dd8e54a2SToby Isaac   PetscValidIntPointer(adjDim,2);
221dd8e54a2SToby Isaac   *adjDim = forest->adjDim;
222dd8e54a2SToby Isaac   PetscFunctionReturn(0);
223dd8e54a2SToby Isaac }
224dd8e54a2SToby Isaac 
225dd8e54a2SToby Isaac #undef __FUNCT__
226dd8e54a2SToby Isaac #define __FUNCT__ "DMForestGetAdjacencyCodimension"
227dd8e54a2SToby Isaac PetscErrorCode DMForestGetAdjacencyCodimension(DM dm, PetscInt *adjCodim)
228dd8e54a2SToby Isaac {
229dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
230dd8e54a2SToby Isaac   PetscInt       dim;
231dd8e54a2SToby Isaac   PetscErrorCode ierr;
232dd8e54a2SToby Isaac 
233dd8e54a2SToby Isaac   PetscFunctionBegin;
234dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
235dd8e54a2SToby Isaac   PetscValidIntPointer(adjCodim,2);
236dd8e54a2SToby Isaac   ierr = DMGetDimension(dm,&dim);CHKERRQ(ierr);
237dd8e54a2SToby Isaac   *adjCodim = dim - forest->adjDim;
238dd8e54a2SToby Isaac   PetscFunctionReturn(0);
239dd8e54a2SToby Isaac }
240dd8e54a2SToby Isaac 
241dd8e54a2SToby Isaac #undef __FUNCT__
242ef51cf95SToby Isaac #define __FUNCT__ "DMForestSetPartitionOverlap"
243dd8e54a2SToby Isaac PetscErrorCode DMForestSetPartitionOverlap(DM dm, PetscInt overlap)
244dd8e54a2SToby Isaac {
245dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
246dd8e54a2SToby Isaac 
247dd8e54a2SToby Isaac   PetscFunctionBegin;
248dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
249ef51cf95SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the overlap after setup");
250dd8e54a2SToby Isaac   if (overlap < 0) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"overlap cannot be < 0: %d", overlap);
251dd8e54a2SToby Isaac   forest->overlap = overlap;
252dd8e54a2SToby Isaac   PetscFunctionReturn(0);
253dd8e54a2SToby Isaac }
254dd8e54a2SToby Isaac 
255dd8e54a2SToby Isaac #undef __FUNCT__
256dd8e54a2SToby Isaac #define __FUNCT__ "DMForestGetPartitionOverlap"
257dd8e54a2SToby Isaac PetscErrorCode DMForestGetPartitionOverlap(DM dm, PetscInt *overlap)
258dd8e54a2SToby Isaac {
259dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
260dd8e54a2SToby Isaac 
261dd8e54a2SToby Isaac   PetscFunctionBegin;
262dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
263dd8e54a2SToby Isaac   PetscValidIntPointer(overlap,2);
264dd8e54a2SToby Isaac   *overlap = forest->overlap;
265dd8e54a2SToby Isaac   PetscFunctionReturn(0);
266dd8e54a2SToby Isaac }
267dd8e54a2SToby Isaac 
268dd8e54a2SToby Isaac #undef __FUNCT__
269dd8e54a2SToby Isaac #define __FUNCT__ "DMForestSetMinimumRefinement"
270dd8e54a2SToby Isaac PetscErrorCode DMForestSetMinimumRefinement(DM dm, PetscInt minRefinement)
271dd8e54a2SToby Isaac {
272dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
273dd8e54a2SToby Isaac 
274dd8e54a2SToby Isaac   PetscFunctionBegin;
275dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
276ef51cf95SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the minimum refinement after setup");
277dd8e54a2SToby Isaac   forest->minRefinement = minRefinement;
278dd8e54a2SToby Isaac   PetscFunctionReturn(0);
279dd8e54a2SToby Isaac }
280dd8e54a2SToby Isaac 
281dd8e54a2SToby Isaac #undef __FUNCT__
282dd8e54a2SToby Isaac #define __FUNCT__ "DMForestGetMinimumRefinement"
283dd8e54a2SToby Isaac PetscErrorCode DMForestGetMinimumRefinement(DM dm, PetscInt *minRefinement)
284dd8e54a2SToby Isaac {
285dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
286dd8e54a2SToby Isaac 
287dd8e54a2SToby Isaac   PetscFunctionBegin;
288dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
289dd8e54a2SToby Isaac   PetscValidIntPointer(minRefinement,2);
290dd8e54a2SToby Isaac   *minRefinement = forest->minRefinement;
291dd8e54a2SToby Isaac   PetscFunctionReturn(0);
292dd8e54a2SToby Isaac }
293dd8e54a2SToby Isaac 
294dd8e54a2SToby Isaac #undef __FUNCT__
295c7eeac06SToby Isaac #define __FUNCT__ "DMForestSetMaximumRefinement"
296c7eeac06SToby Isaac PetscErrorCode DMForestSetMaximumRefinement(DM dm, PetscInt maxRefinement)
297dd8e54a2SToby Isaac {
298dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
299dd8e54a2SToby Isaac 
300dd8e54a2SToby Isaac   PetscFunctionBegin;
301dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
302ef51cf95SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the maximum refinement after setup");
303c7eeac06SToby Isaac   forest->maxRefinement = maxRefinement;
304dd8e54a2SToby Isaac   PetscFunctionReturn(0);
305dd8e54a2SToby Isaac }
306dd8e54a2SToby Isaac 
307dd8e54a2SToby Isaac #undef __FUNCT__
308c7eeac06SToby Isaac #define __FUNCT__ "DMForestGetMaximumRefinement"
309c7eeac06SToby Isaac PetscErrorCode DMForestGetMaximumRefinement(DM dm, PetscInt *maxRefinement)
310dd8e54a2SToby Isaac {
311dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
312dd8e54a2SToby Isaac 
313dd8e54a2SToby Isaac   PetscFunctionBegin;
314dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
315c7eeac06SToby Isaac   PetscValidIntPointer(maxRefinement,2);
316c7eeac06SToby Isaac   *maxRefinement = forest->maxRefinement;
317dd8e54a2SToby Isaac   PetscFunctionReturn(0);
318dd8e54a2SToby Isaac }
319c7eeac06SToby Isaac 
320c7eeac06SToby Isaac #undef __FUNCT__
321c7eeac06SToby Isaac #define __FUNCT__ "DMForestSetAdaptivityStrategy"
322c7eeac06SToby Isaac PetscErrorCode DMForestSetAdaptivityStrategy(DM dm, DMForestAdaptivityStrategy adaptStrategy)
323c7eeac06SToby Isaac {
324c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
325c7eeac06SToby Isaac   PetscErrorCode ierr;
326c7eeac06SToby Isaac 
327c7eeac06SToby Isaac   PetscFunctionBegin;
328c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
329c7eeac06SToby Isaac   ierr = PetscFree(forest->adaptStrategy);CHKERRQ(ierr);
330c7eeac06SToby Isaac   ierr = PetscStrallocpy((const char *)adaptStrategy,(char **)adaptStrategy);CHKERRQ(ierr);
331c7eeac06SToby Isaac   PetscFunctionReturn(0);
332c7eeac06SToby Isaac }
333c7eeac06SToby Isaac 
334c7eeac06SToby Isaac #undef __FUNCT__
335c7eeac06SToby Isaac #define __FUNCT__ "DMForestGetAdaptivityStrategy"
336c7eeac06SToby Isaac PetscErrorCode DMForestGetAdaptivityStrategy(DM dm, DMForestAdaptivityStrategy *adaptStrategy)
337c7eeac06SToby Isaac {
338c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
339c7eeac06SToby Isaac 
340c7eeac06SToby Isaac   PetscFunctionBegin;
341c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
342c7eeac06SToby Isaac   PetscValidPointer(adaptStrategy,2);
343c7eeac06SToby Isaac   *adaptStrategy = forest->adaptStrategy;
344c7eeac06SToby Isaac   PetscFunctionReturn(0);
345c7eeac06SToby Isaac }
346c7eeac06SToby Isaac 
347c7eeac06SToby Isaac #undef __FUNCT__
348c7eeac06SToby Isaac #define __FUNCT__ "DMForestSetGradeFactor"
349c7eeac06SToby Isaac PetscErrorCode DMForestSetGradeFactor(DM dm, PetscInt grade)
350c7eeac06SToby Isaac {
351c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
352c7eeac06SToby Isaac 
353c7eeac06SToby Isaac   PetscFunctionBegin;
354c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
355ef51cf95SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the grade factor after setup");
356c7eeac06SToby Isaac   forest->gradeFactor = grade;
357c7eeac06SToby Isaac   PetscFunctionReturn(0);
358c7eeac06SToby Isaac }
359c7eeac06SToby Isaac 
360c7eeac06SToby Isaac #undef __FUNCT__
361c7eeac06SToby Isaac #define __FUNCT__ "DMForestGetGradeFactor"
362c7eeac06SToby Isaac PetscErrorCode DMForestGetGradeFactor(DM dm, PetscInt *grade)
363c7eeac06SToby Isaac {
364c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
365c7eeac06SToby Isaac 
366c7eeac06SToby Isaac   PetscFunctionBegin;
367c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
368c7eeac06SToby Isaac   PetscValidIntPointer(grade,2);
369c7eeac06SToby Isaac   *grade = forest->gradeFactor;
370c7eeac06SToby Isaac   PetscFunctionReturn(0);
371c7eeac06SToby Isaac }
372c7eeac06SToby Isaac 
373c7eeac06SToby Isaac #undef __FUNCT__
374ef51cf95SToby Isaac #define __FUNCT__ "DMForestSetCellWeightFactor"
375ef51cf95SToby Isaac PetscErrorCode DMForestSetCellWeightFactor(DM dm, PetscReal weightsFactor)
376c7eeac06SToby Isaac {
377c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
378c7eeac06SToby Isaac 
379c7eeac06SToby Isaac   PetscFunctionBegin;
380c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
381ef51cf95SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the weights factor after setup");
382c7eeac06SToby Isaac   forest->weightsFactor = weightsFactor;
383c7eeac06SToby Isaac   PetscFunctionReturn(0);
384c7eeac06SToby Isaac }
385c7eeac06SToby Isaac 
386c7eeac06SToby Isaac #undef __FUNCT__
387ef51cf95SToby Isaac #define __FUNCT__ "DMForestGetCellWeightFactor"
388ef51cf95SToby Isaac PetscErrorCode DMForestGetCellWeightFactor(DM dm, PetscReal *weightsFactor)
389c7eeac06SToby Isaac {
390c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
391c7eeac06SToby Isaac 
392c7eeac06SToby Isaac   PetscFunctionBegin;
393c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
394c7eeac06SToby Isaac   PetscValidRealPointer(weightsFactor,2);
395c7eeac06SToby Isaac   *weightsFactor = forest->weightsFactor;
396c7eeac06SToby Isaac   PetscFunctionReturn(0);
397c7eeac06SToby Isaac }
398c7eeac06SToby Isaac 
399c7eeac06SToby Isaac #undef __FUNCT__
400c7eeac06SToby Isaac #define __FUNCT__ "DMForestGetCellChart"
401c7eeac06SToby Isaac PetscErrorCode DMForestGetCellChart(DM dm, PetscInt *cStart, PetscInt *cEnd)
402c7eeac06SToby Isaac {
403c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
404c7eeac06SToby Isaac   PetscErrorCode ierr;
405c7eeac06SToby Isaac 
406c7eeac06SToby Isaac   PetscFunctionBegin;
407c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
408c7eeac06SToby Isaac   PetscValidIntPointer(cStart,2);
409c7eeac06SToby Isaac   PetscValidIntPointer(cEnd,2);
410c7eeac06SToby Isaac   if (((forest->cStart == PETSC_DETERMINE) || (forest->cEnd == PETSC_DETERMINE)) && forest->createcellchart) {
411c7eeac06SToby Isaac     ierr = forest->createcellchart(dm,&forest->cStart,&forest->cEnd);CHKERRQ(ierr);
412c7eeac06SToby Isaac   }
413c7eeac06SToby Isaac   *cStart =  forest->cStart;
414c7eeac06SToby Isaac   *cEnd   =  forest->cEnd;
415c7eeac06SToby Isaac   PetscFunctionReturn(0);
416c7eeac06SToby Isaac }
417c7eeac06SToby Isaac 
418c7eeac06SToby Isaac #undef __FUNCT__
419c7eeac06SToby Isaac #define __FUNCT__ "DMForestGetCellSF"
420c7eeac06SToby Isaac PetscErrorCode DMForestGetCellSF(DM dm, PetscSF *cellSF)
421c7eeac06SToby Isaac {
422c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
423c7eeac06SToby Isaac   PetscErrorCode ierr;
424c7eeac06SToby Isaac 
425c7eeac06SToby Isaac   PetscFunctionBegin;
426c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
427c7eeac06SToby Isaac   PetscValidPointer(cellSF,2);
428c7eeac06SToby Isaac   if ((!forest->cellSF) && forest->createcellsf) {
429c7eeac06SToby Isaac     ierr = forest->createcellsf(dm,&forest->cellSF);CHKERRQ(ierr);
430c7eeac06SToby Isaac   }
431c7eeac06SToby Isaac   *cellSF = forest->cellSF;
432c7eeac06SToby Isaac   PetscFunctionReturn(0);
433c7eeac06SToby Isaac }
434c7eeac06SToby Isaac 
435c7eeac06SToby Isaac #undef __FUNCT__
436c7eeac06SToby Isaac #define __FUNCT__ "DMForestSetAdaptivityMarkers"
437c7eeac06SToby Isaac PetscErrorCode DMForestSetAdaptivityMarkers(DM dm, PetscInt markers[], PetscCopyMode copyMode)
438c7eeac06SToby Isaac {
439c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
440c7eeac06SToby Isaac   PetscInt       cStart, cEnd;
441c7eeac06SToby Isaac   PetscErrorCode ierr;
442c7eeac06SToby Isaac 
443c7eeac06SToby Isaac   PetscFunctionBegin;
444c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
445c7eeac06SToby Isaac   ierr = DMForestGetCellChart(dm,&cStart,&cEnd);CHKERRQ(ierr);
446c7eeac06SToby Isaac   if (cEnd < cStart) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"cell chart [%d,%d) is not valid",cStart,cEnd);
447c7eeac06SToby Isaac   if (copyMode == PETSC_COPY_VALUES) {
448c7eeac06SToby Isaac     if (forest->adaptCopyMode != PETSC_OWN_POINTER || forest->adaptMarkers == markers) {
449c7eeac06SToby Isaac       ierr = PetscMalloc1(cEnd-cStart,&forest->adaptMarkers);CHKERRQ(ierr);
450c7eeac06SToby Isaac     }
451c7eeac06SToby Isaac     ierr = PetscMemcpy(forest->adaptMarkers,markers,(cEnd-cStart)*sizeof(*markers));CHKERRQ(ierr);
452c7eeac06SToby Isaac     forest->adaptCopyMode = PETSC_OWN_POINTER;
453c7eeac06SToby Isaac     PetscFunctionReturn(0);
454c7eeac06SToby Isaac   }
455c7eeac06SToby Isaac   if (forest->adaptCopyMode == PETSC_OWN_POINTER) {
456c7eeac06SToby Isaac     ierr = PetscFree(forest->adaptMarkers);CHKERRQ(ierr);
457c7eeac06SToby Isaac   }
458c7eeac06SToby Isaac   forest->adaptMarkers  = markers;
459c7eeac06SToby Isaac   forest->adaptCopyMode = copyMode;
460c7eeac06SToby Isaac   PetscFunctionReturn(0);
461c7eeac06SToby Isaac }
462c7eeac06SToby Isaac 
463c7eeac06SToby Isaac #undef __FUNCT__
464c7eeac06SToby Isaac #define __FUNCT__ "DMForestGetAdaptivityMarkers"
465c7eeac06SToby Isaac PetscErrorCode DMForestGetAdaptivityMarkers(DM dm, PetscInt **markers)
466c7eeac06SToby Isaac {
467c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
468c7eeac06SToby Isaac 
469c7eeac06SToby Isaac   PetscFunctionBegin;
470c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
471c7eeac06SToby Isaac   PetscValidPointer(markers,2);
472c7eeac06SToby Isaac   *markers = forest->adaptMarkers;
473c7eeac06SToby Isaac   PetscFunctionReturn(0);
474c7eeac06SToby Isaac }
475c7eeac06SToby Isaac 
476c7eeac06SToby Isaac #undef __FUNCT__
477c7eeac06SToby Isaac #define __FUNCT__ "DMForestSetCellWeights"
478c7eeac06SToby Isaac PetscErrorCode DMForestSetCellWeights(DM dm, PetscReal weights[], PetscCopyMode copyMode)
479c7eeac06SToby Isaac {
480c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
481c7eeac06SToby Isaac   PetscInt       cStart, cEnd;
482c7eeac06SToby Isaac   PetscErrorCode ierr;
483c7eeac06SToby Isaac 
484c7eeac06SToby Isaac   PetscFunctionBegin;
485c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
486c7eeac06SToby Isaac   ierr = DMForestGetCellChart(dm,&cStart,&cEnd);CHKERRQ(ierr);
487c7eeac06SToby Isaac   if (cEnd < cStart) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"cell chart [%d,%d) is not valid",cStart,cEnd);
488c7eeac06SToby Isaac   if (copyMode == PETSC_COPY_VALUES) {
489c7eeac06SToby Isaac     if (forest->cellWeightsCopyMode != PETSC_OWN_POINTER || forest->cellWeights == weights) {
490c7eeac06SToby Isaac       ierr = PetscMalloc1(cEnd-cStart,&forest->cellWeights);CHKERRQ(ierr);
491c7eeac06SToby Isaac     }
492c7eeac06SToby Isaac     ierr = PetscMemcpy(forest->cellWeights,weights,(cEnd-cStart)*sizeof(*weights));CHKERRQ(ierr);
493c7eeac06SToby Isaac     forest->cellWeightsCopyMode = PETSC_OWN_POINTER;
494c7eeac06SToby Isaac     PetscFunctionReturn(0);
495c7eeac06SToby Isaac   }
496c7eeac06SToby Isaac   if (forest->cellWeightsCopyMode == PETSC_OWN_POINTER) {
497c7eeac06SToby Isaac     ierr = PetscFree(forest->cellWeights);CHKERRQ(ierr);
498c7eeac06SToby Isaac   }
499c7eeac06SToby Isaac   forest->cellWeights  = weights;
500c7eeac06SToby Isaac   forest->cellWeightsCopyMode = copyMode;
501c7eeac06SToby Isaac   PetscFunctionReturn(0);
502c7eeac06SToby Isaac }
503c7eeac06SToby Isaac 
504c7eeac06SToby Isaac #undef __FUNCT__
505c7eeac06SToby Isaac #define __FUNCT__ "DMForestGetCellWeights"
506c7eeac06SToby Isaac PetscErrorCode DMForestGetCellWeights(DM dm, PetscReal **weights)
507c7eeac06SToby Isaac {
508c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
509c7eeac06SToby Isaac 
510c7eeac06SToby Isaac   PetscFunctionBegin;
511c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
512c7eeac06SToby Isaac   PetscValidPointer(weights,2);
513c7eeac06SToby Isaac   *weights = forest->cellWeights;
514c7eeac06SToby Isaac   PetscFunctionReturn(0);
515c7eeac06SToby Isaac }
516c7eeac06SToby Isaac 
517c7eeac06SToby Isaac #undef __FUNCT__
518c7eeac06SToby Isaac #define __FUNCT__ "DMForestSetWeightCapacity"
519c7eeac06SToby Isaac PetscErrorCode DMForestSetWeightCapacity(DM dm, PetscReal capacity)
520c7eeac06SToby Isaac {
521c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
522c7eeac06SToby Isaac 
523c7eeac06SToby Isaac   PetscFunctionBegin;
524c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
525ef51cf95SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the weight capacity after setup");
526c7eeac06SToby Isaac   if (capacity < 0.) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"Cannot have negative weight capacity; %f",capacity);
527c7eeac06SToby Isaac   forest->weightCapacity = capacity;
528c7eeac06SToby Isaac   PetscFunctionReturn(0);
529c7eeac06SToby Isaac }
530c7eeac06SToby Isaac 
531c7eeac06SToby Isaac #undef __FUNCT__
532c7eeac06SToby Isaac #define __FUNCT__ "DMForestGetWeightCapacity"
533c7eeac06SToby Isaac PetscErrorCode DMForestGetWeightCapacity(DM dm, PetscReal *capacity)
534c7eeac06SToby Isaac {
535c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
536c7eeac06SToby Isaac 
537c7eeac06SToby Isaac   PetscFunctionBegin;
538c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
539c7eeac06SToby Isaac   PetscValidRealPointer(capacity,2);
540c7eeac06SToby Isaac   *capacity = forest->weightCapacity;
541c7eeac06SToby Isaac   PetscFunctionReturn(0);
542c7eeac06SToby Isaac }
543c7eeac06SToby Isaac 
544dd8e54a2SToby Isaac #undef __FUNCT__
545db4d5e8cSToby Isaac #define __FUNCT__ "DMSetFromOptions_Forest"
5465c8434f9SToby Isaac PETSC_EXTERN PetscErrorCode DMSetFromOptions_Forest(PetscOptions *PetscOptionsObject,DM dm)
547db4d5e8cSToby Isaac {
548db4d5e8cSToby Isaac   DM_Forest                  *forest = (DM_Forest *) dm->data;
549dd8e54a2SToby Isaac   PetscBool                  flg;
550dd8e54a2SToby Isaac   DMForestTopology           oldTopo;
551c7eeac06SToby Isaac   char                       stringBuffer[256];
552dd8e54a2SToby Isaac   PetscViewer                viewer;
553dd8e54a2SToby Isaac   PetscViewerFormat          format;
554c7eeac06SToby Isaac   PetscInt                   adjDim, adjCodim, overlap, minRefinement, maxRefinement, grade;
555c7eeac06SToby Isaac   PetscReal                  weightsFactor;
556c7eeac06SToby Isaac   DMForestAdaptivityStrategy adaptStrategy;
557db4d5e8cSToby Isaac   PetscErrorCode             ierr;
558db4d5e8cSToby Isaac 
559db4d5e8cSToby Isaac   PetscFunctionBegin;
560db4d5e8cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
561db4d5e8cSToby Isaac   forest->setFromOptions = PETSC_TRUE;
5625c8434f9SToby Isaac   ierr = PetscOptionsHead(PetscOptionsObject,"DMForest Options");CHKERRQ(ierr);
563dd8e54a2SToby Isaac   ierr = DMForestGetTopology(dm, &oldTopo);CHKERRQ(ierr);
564c7eeac06SToby Isaac   ierr = PetscOptionsString("-dm_forest_topology","the topology of the forest's base mesh","DMForestSetTopology",oldTopo,stringBuffer,256,&flg);CHKERRQ(ierr);
565db4d5e8cSToby Isaac   if (flg) {
566c7eeac06SToby Isaac     ierr = DMForestSetTopology(dm,(DMForestTopology)stringBuffer);CHKERRQ(ierr);
567dd8e54a2SToby Isaac   }
568dd8e54a2SToby Isaac   ierr = PetscOptionsViewer("-dm_forest_base_dm","load the base DM from a viewer specification","DMForestSetBaseDM",&viewer,&format,&flg);CHKERRQ(ierr);
569dd8e54a2SToby Isaac   if (flg) {
570dd8e54a2SToby Isaac     DM         base;
571dd8e54a2SToby Isaac 
572dd8e54a2SToby Isaac     ierr = DMCreate(PetscObjectComm((PetscObject)dm),&base);CHKERRQ(ierr);
573dd8e54a2SToby Isaac     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
574dd8e54a2SToby Isaac     ierr = DMLoad(base,viewer);CHKERRQ(ierr);
575dd8e54a2SToby Isaac     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
576dd8e54a2SToby Isaac     ierr = DMForestSetBaseDM(dm,base);CHKERRQ(ierr);
577dd8e54a2SToby Isaac     ierr = DMDestroy(&base);CHKERRQ(ierr);
578dd8e54a2SToby Isaac   }
579dd8e54a2SToby Isaac   ierr = PetscOptionsViewer("-dm_forest_coarse_forest","load the coarse forest from a viewer specification","DMForestSetCoarseForest",&viewer,&format,&flg);CHKERRQ(ierr);
580dd8e54a2SToby Isaac   if (flg) {
581dd8e54a2SToby Isaac     DM         coarse;
582dd8e54a2SToby Isaac 
583dd8e54a2SToby Isaac     ierr = DMCreate(PetscObjectComm((PetscObject)dm),&coarse);CHKERRQ(ierr);
584dd8e54a2SToby Isaac     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
585dd8e54a2SToby Isaac     ierr = DMLoad(coarse,viewer);CHKERRQ(ierr);
586dd8e54a2SToby Isaac     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
587dd8e54a2SToby Isaac     ierr = DMForestSetCoarseForest(dm,coarse);CHKERRQ(ierr);
588dd8e54a2SToby Isaac     ierr = DMDestroy(&coarse);CHKERRQ(ierr);
589dd8e54a2SToby Isaac   }
590dd8e54a2SToby Isaac   ierr = PetscOptionsViewer("-dm_forest_fine_forest","load the fine forest from a viewer specification","DMForestSetFineForest",&viewer,&format,&flg);CHKERRQ(ierr);
591dd8e54a2SToby Isaac   if (flg) {
592dd8e54a2SToby Isaac     DM         fine;
593dd8e54a2SToby Isaac 
594dd8e54a2SToby Isaac     ierr = DMCreate(PetscObjectComm((PetscObject)dm),&fine);CHKERRQ(ierr);
595dd8e54a2SToby Isaac     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
596dd8e54a2SToby Isaac     ierr = DMLoad(fine,viewer);CHKERRQ(ierr);
597dd8e54a2SToby Isaac     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
598dd8e54a2SToby Isaac     ierr = DMForestSetFineForest(dm,fine);CHKERRQ(ierr);
599dd8e54a2SToby Isaac     ierr = DMDestroy(&fine);CHKERRQ(ierr);
600dd8e54a2SToby Isaac   }
601dd8e54a2SToby Isaac   ierr = DMForestGetAdjacencyDimension(dm,&adjDim);CHKERRQ(ierr);
602dd8e54a2SToby Isaac   ierr = PetscOptionsInt("-dm_forest_adjacency_dimension","set the dimension of points that define adjacency in the forest","DMForestSetAdjacencyDimension",adjDim,&adjDim,&flg);CHKERRQ(ierr);
603dd8e54a2SToby Isaac   if (flg) {
604dd8e54a2SToby Isaac     ierr = DMForestSetAdjacencyDimension(dm,adjDim);CHKERRQ(ierr);
605dd8e54a2SToby Isaac   }
606dd8e54a2SToby Isaac   else {
607dd8e54a2SToby Isaac     ierr = DMForestGetAdjacencyCodimension(dm,&adjCodim);CHKERRQ(ierr);
608dd8e54a2SToby Isaac     ierr = PetscOptionsInt("-dm_forest_adjacency_codimension","set the codimension of points that define adjacency in the forest","DMForestSetAdjacencyCodimension",adjCodim,&adjCodim,&flg);CHKERRQ(ierr);
609dd8e54a2SToby Isaac     if (flg) {
610dd8e54a2SToby Isaac       ierr = DMForestSetAdjacencyCodimension(dm,adjCodim);CHKERRQ(ierr);
611dd8e54a2SToby Isaac     }
612dd8e54a2SToby Isaac   }
613dd8e54a2SToby Isaac   ierr = DMForestGetPartitionOverlap(dm,&overlap);CHKERRQ(ierr);
614dd8e54a2SToby Isaac   ierr = PetscOptionsInt("-dm_forest_partition_overlap","set the degree of partition overlap","DMForestSetPartitionOverlap",overlap,&overlap,&flg);CHKERRQ(ierr);
615dd8e54a2SToby Isaac   if (flg) {
616dd8e54a2SToby Isaac     ierr = DMForestSetPartitionOverlap(dm,overlap);CHKERRQ(ierr);
617dd8e54a2SToby Isaac   }
618dd8e54a2SToby Isaac   ierr = DMForestGetMinimumRefinement(dm,&minRefinement);CHKERRQ(ierr);
619dd8e54a2SToby Isaac   ierr = PetscOptionsInt("-dm_forest_minimum_refinement","set the minimum level of refinement in the forest","DMForestSetMinimumRefinement",minRefinement,&minRefinement,&flg);CHKERRQ(ierr);
620dd8e54a2SToby Isaac   if (flg) {
621dd8e54a2SToby Isaac     ierr = DMForestSetMinimumRefinement(dm,minRefinement);CHKERRQ(ierr);
622db4d5e8cSToby Isaac   }
623c7eeac06SToby Isaac   ierr = DMForestGetMaximumRefinement(dm,&maxRefinement);CHKERRQ(ierr);
624c7eeac06SToby Isaac   ierr = PetscOptionsInt("-dm_forest_maximum_refinement","set the maximum level of refinement in the forest","DMForestSetMaximumRefinement",maxRefinement,&maxRefinement,&flg);CHKERRQ(ierr);
625c7eeac06SToby Isaac   if (flg) {
626c7eeac06SToby Isaac     ierr = DMForestSetMaximumRefinement(dm,maxRefinement);CHKERRQ(ierr);
627c7eeac06SToby Isaac   }
628c7eeac06SToby Isaac   ierr = DMForestGetAdaptivityStrategy(dm,&adaptStrategy);CHKERRQ(ierr);
629c7eeac06SToby Isaac   ierr = PetscOptionsString("-dm_forest_adaptivity_strategy","the forest's adaptivity-flag resolution strategy","DMForestSetAdaptivityStrategy",adaptStrategy,stringBuffer,256,&flg);CHKERRQ(ierr);
630c7eeac06SToby Isaac   if (flg) {
631c7eeac06SToby Isaac     ierr = DMForestSetAdaptivityStrategy(dm,(DMForestAdaptivityStrategy)stringBuffer);CHKERRQ(ierr);
632c7eeac06SToby Isaac   }
633c7eeac06SToby Isaac   ierr = DMForestGetGradeFactor(dm,&grade);CHKERRQ(ierr);
634c7eeac06SToby Isaac   ierr = PetscOptionsInt("-dm_forest_grade_factor","grade factor between neighboring cells","DMForestSetGradeFactor",grade,&grade,&flg);CHKERRQ(ierr);
635c7eeac06SToby Isaac   if (flg) {
636c7eeac06SToby Isaac     ierr = DMForestSetGradeFactor(dm,grade);CHKERRQ(ierr);
637c7eeac06SToby Isaac   }
638c7eeac06SToby Isaac   ierr = DMForestGetCellWeightFactor(dm,&weightsFactor);CHKERRQ(ierr);
639c7eeac06SToby Isaac   ierr = PetscOptionsReal("-dm_forest_cell_weight_factor","multiplying weight factor for cell refinement","DMForestSetCellWeightFactor",weightsFactor,&weightsFactor,&flg);CHKERRQ(ierr);
640c7eeac06SToby Isaac   if (flg) {
641c7eeac06SToby Isaac     ierr = DMForestSetCellWeightFactor(dm,weightsFactor);CHKERRQ(ierr);
642c7eeac06SToby Isaac   }
643db4d5e8cSToby Isaac   ierr = PetscOptionsTail();CHKERRQ(ierr);
644db4d5e8cSToby Isaac   PetscFunctionReturn(0);
645db4d5e8cSToby Isaac }
646db4d5e8cSToby Isaac 
647db4d5e8cSToby Isaac #undef __FUNCT__
648d222f98bSToby Isaac #define __FUNCT__ "DMInitialize_Forest"
649d222f98bSToby Isaac static PetscErrorCode DMInitialize_Forest(DM dm)
650d222f98bSToby Isaac {
651d222f98bSToby Isaac   PetscErrorCode ierr;
652d222f98bSToby Isaac 
653d222f98bSToby Isaac   PetscFunctionBegin;
654d222f98bSToby Isaac   ierr = PetscMemzero(dm->ops,sizeof(*(dm->ops)));CHKERRQ(ierr);
655d222f98bSToby Isaac 
656d222f98bSToby Isaac   dm->ops->clone          = DMClone_Forest;
657d222f98bSToby Isaac   dm->ops->setfromoptions = DMSetFromOptions_Forest;
658d222f98bSToby Isaac   dm->ops->destroy        = DMDestroy_Forest;
659d222f98bSToby Isaac   PetscFunctionReturn(0);
660d222f98bSToby Isaac }
661d222f98bSToby Isaac 
662d222f98bSToby Isaac #undef __FUNCT__
663db4d5e8cSToby Isaac #define __FUNCT__ "DMCreate_Forest"
664db4d5e8cSToby Isaac PETSC_EXTERN PetscErrorCode DMCreate_Forest(DM dm)
665db4d5e8cSToby Isaac {
666db4d5e8cSToby Isaac   DM_Forest      *forest;
667db4d5e8cSToby Isaac   PetscErrorCode ierr;
668db4d5e8cSToby Isaac 
669db4d5e8cSToby Isaac   PetscFunctionBegin;
670db4d5e8cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
671db4d5e8cSToby Isaac   ierr                        = PetscNewLog(dm,&forest);CHKERRQ(ierr);
672db4d5e8cSToby Isaac   dm->dim                     = 0;
673db4d5e8cSToby Isaac   dm->data                    = forest;
674db4d5e8cSToby Isaac   forest->refct               = 1;
675db4d5e8cSToby Isaac   forest->data                = NULL;
676dd8e54a2SToby Isaac   forest->setFromOptions      = PETSC_FALSE;
677db4d5e8cSToby Isaac   forest->topology            = NULL;
678db4d5e8cSToby Isaac   forest->base                = NULL;
679db4d5e8cSToby Isaac   forest->coarse              = NULL;
680db4d5e8cSToby Isaac   forest->fine                = NULL;
681db4d5e8cSToby Isaac   forest->adjDim              = PETSC_DEFAULT;
682db4d5e8cSToby Isaac   forest->overlap             = PETSC_DEFAULT;
683db4d5e8cSToby Isaac   forest->minRefinement       = PETSC_DEFAULT;
684db4d5e8cSToby Isaac   forest->maxRefinement       = PETSC_DEFAULT;
685c7eeac06SToby Isaac   forest->cStart              = PETSC_DETERMINE;
686c7eeac06SToby Isaac   forest->cEnd                = PETSC_DETERMINE;
687db4d5e8cSToby Isaac   forest->cellSF              = 0;
688db4d5e8cSToby Isaac   forest->adaptMarkers        = NULL;
689db4d5e8cSToby Isaac   forest->adaptCopyMode       = PETSC_USE_POINTER;
690db4d5e8cSToby Isaac   forest->adaptStrategy       = DMFORESTADAPTALL;
691db4d5e8cSToby Isaac   forest->gradeFactor         = 2;
692db4d5e8cSToby Isaac   forest->cellWeights         = NULL;
693db4d5e8cSToby Isaac   forest->cellWeightsCopyMode = PETSC_USE_POINTER;
694db4d5e8cSToby Isaac   forest->weightsFactor       = 1.;
695db4d5e8cSToby Isaac   forest->weightCapacity      = 1.;
696d222f98bSToby Isaac   ierr = DMInitialize_Forest(dm);CHKERRQ(ierr);
697db4d5e8cSToby Isaac   PetscFunctionReturn(0);
698db4d5e8cSToby Isaac }
699db4d5e8cSToby Isaac 
700