xref: /petsc/src/dm/impls/forest/forest.c (revision 88bdff64cb7d92bd2b7e4dc3355b210b6b0de139)
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__
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   }
3856ba9f64SToby Isaac   ierr = DMDestroy(&forest->base);CHKERRQ(ierr);
3930f902e7SToby Isaac   ierr = PetscFree(forest->topology);CHKERRQ(ierr);
4030f902e7SToby Isaac   ierr = PetscFree(forest);CHKERRQ(ierr);
41db4d5e8cSToby Isaac   PetscFunctionReturn(0);
42db4d5e8cSToby Isaac }
43db4d5e8cSToby Isaac 
44db4d5e8cSToby Isaac #undef __FUNCT__
45dd8e54a2SToby Isaac #define __FUNCT__ "DMForestSetTopology"
46dd8e54a2SToby Isaac PetscErrorCode DMForestSetTopology(DM dm, DMForestTopology topology)
47db4d5e8cSToby Isaac {
48db4d5e8cSToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
49db4d5e8cSToby Isaac   PetscErrorCode ierr;
50db4d5e8cSToby Isaac 
51db4d5e8cSToby Isaac   PetscFunctionBegin;
52db4d5e8cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
53ef51cf95SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the topology after setup");
54dd8e54a2SToby Isaac   ierr = PetscFree(forest->topology);CHKERRQ(ierr);
55dd8e54a2SToby Isaac   ierr = PetscStrallocpy((const char *)topology,(char **) &forest->topology);CHKERRQ(ierr);
56db4d5e8cSToby Isaac   PetscFunctionReturn(0);
57db4d5e8cSToby Isaac }
58db4d5e8cSToby Isaac 
59db4d5e8cSToby Isaac #undef __FUNCT__
60dd8e54a2SToby Isaac #define __FUNCT__ "DMForestGetTopology"
61dd8e54a2SToby Isaac PetscErrorCode DMForestGetTopology(DM dm, DMForestTopology *topology)
62dd8e54a2SToby Isaac {
63dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
64dd8e54a2SToby Isaac 
65dd8e54a2SToby Isaac   PetscFunctionBegin;
66dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67dd8e54a2SToby Isaac   PetscValidPointer(topology,2);
68dd8e54a2SToby Isaac   *topology = forest->topology;
69dd8e54a2SToby Isaac   PetscFunctionReturn(0);
70dd8e54a2SToby Isaac }
71dd8e54a2SToby Isaac 
72dd8e54a2SToby Isaac #undef __FUNCT__
73dd8e54a2SToby Isaac #define __FUNCT__ "DMForestSetBaseDM"
74dd8e54a2SToby Isaac PetscErrorCode DMForestSetBaseDM(DM dm, DM base)
75dd8e54a2SToby Isaac {
76dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
77dd8e54a2SToby Isaac   PetscInt       dim, dimEmbed;
78dd8e54a2SToby Isaac   PetscErrorCode ierr;
79dd8e54a2SToby Isaac 
80dd8e54a2SToby Isaac   PetscFunctionBegin;
81dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8256ba9f64SToby Isaac   PetscValidHeaderSpecific(base, DM_CLASSID, 2);
83ef51cf95SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the base after setup");
84dd8e54a2SToby Isaac   ierr = PetscObjectReference((PetscObject)base);CHKERRQ(ierr);
85dd8e54a2SToby Isaac   ierr = DMDestroy(&forest->base);CHKERRQ(ierr);
86dd8e54a2SToby Isaac   forest->base = base;
87dd8e54a2SToby Isaac   ierr = DMGetDimension(base,&dim);CHKERRQ(ierr);
88dd8e54a2SToby Isaac   ierr = DMSetDimension(dm,dim);CHKERRQ(ierr);
89dd8e54a2SToby Isaac   ierr = DMGetCoordinateDim(base,&dimEmbed);CHKERRQ(ierr);
90dd8e54a2SToby Isaac   ierr = DMSetCoordinateDim(dm,dimEmbed);CHKERRQ(ierr);
91dd8e54a2SToby Isaac   PetscFunctionReturn(0);
92dd8e54a2SToby Isaac }
93dd8e54a2SToby Isaac 
94dd8e54a2SToby Isaac #undef __FUNCT__
95dd8e54a2SToby Isaac #define __FUNCT__ "DMForestGetBaseDM"
96dd8e54a2SToby Isaac PetscErrorCode DMForestGetBaseDM(DM dm, DM *base)
97dd8e54a2SToby Isaac {
98dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
99dd8e54a2SToby Isaac 
100dd8e54a2SToby Isaac   PetscFunctionBegin;
101dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
102dd8e54a2SToby Isaac   PetscValidPointer(base, 2);
103dd8e54a2SToby Isaac   *base = forest->base;
104dd8e54a2SToby Isaac   PetscFunctionReturn(0);
105dd8e54a2SToby Isaac }
106dd8e54a2SToby Isaac 
107dd8e54a2SToby Isaac #undef __FUNCT__
108dd8e54a2SToby Isaac #define __FUNCT__ "DMForestSetCoarseForest"
109dd8e54a2SToby Isaac PetscErrorCode DMForestSetCoarseForest(DM dm,DM coarse)
110dd8e54a2SToby Isaac {
111dd8e54a2SToby Isaac   DM               base;
112d222f98bSToby Isaac   DMForestTopology topology;
113dd8e54a2SToby Isaac   PetscErrorCode   ierr;
114dd8e54a2SToby Isaac 
115dd8e54a2SToby Isaac   PetscFunctionBegin;
116dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11756ba9f64SToby Isaac   PetscValidHeaderSpecific(coarse, 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");
12056ba9f64SToby Isaac   ierr = DMSetCoarseDM(dm,coarse);CHKERRQ(ierr);
121dd8e54a2SToby Isaac   ierr = DMForestGetBaseDM(coarse,&base);CHKERRQ(ierr);
122dd8e54a2SToby Isaac   ierr = DMForestSetBaseDM(dm,base);CHKERRQ(ierr);
123d222f98bSToby Isaac   ierr = DMForestGetTopology(coarse,&topology);CHKERRQ(ierr);
124d222f98bSToby Isaac   ierr = DMForestSetTopology(dm,topology);CHKERRQ(ierr);
125dd8e54a2SToby Isaac   PetscFunctionReturn(0);
126dd8e54a2SToby Isaac }
127dd8e54a2SToby Isaac 
128dd8e54a2SToby Isaac #undef __FUNCT__
129dd8e54a2SToby Isaac #define __FUNCT__ "DMForestGetCoarseForest"
130dd8e54a2SToby Isaac PetscErrorCode DMForestGetCoarseForest(DM dm, DM *coarse)
131dd8e54a2SToby Isaac {
13256ba9f64SToby Isaac   PetscErrorCode ierr;
133dd8e54a2SToby Isaac 
134dd8e54a2SToby Isaac   PetscFunctionBegin;
13556ba9f64SToby Isaac   ierr = DMGetCoarseDM(dm,coarse);CHKERRQ(ierr);
136dd8e54a2SToby Isaac   PetscFunctionReturn(0);
137dd8e54a2SToby Isaac }
138dd8e54a2SToby Isaac 
139dd8e54a2SToby Isaac #undef __FUNCT__
140dd8e54a2SToby Isaac #define __FUNCT__ "DMForestSetFineForest"
141dd8e54a2SToby Isaac PetscErrorCode DMForestSetFineForest(DM dm,DM fine)
142dd8e54a2SToby Isaac {
143dd8e54a2SToby Isaac   DM               base;
144d222f98bSToby Isaac   DMForestTopology topology;
145dd8e54a2SToby Isaac   PetscErrorCode   ierr;
146dd8e54a2SToby Isaac 
147dd8e54a2SToby Isaac   PetscFunctionBegin;
148dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14956ba9f64SToby Isaac   PetscValidHeaderSpecific(fine, DM_CLASSID, 2);
150ef51cf95SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the fine forest after setup");
151ef51cf95SToby Isaac   if (!fine->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot set a fine forest that is not set up");
152*88bdff64SToby Isaac   ierr = DMSetFineDM(dm,fine);CHKERRQ(ierr);
153dd8e54a2SToby Isaac   ierr = DMForestGetBaseDM(fine,&base);CHKERRQ(ierr);
154dd8e54a2SToby Isaac   ierr = DMForestSetBaseDM(dm,base);CHKERRQ(ierr);
155d222f98bSToby Isaac   ierr = DMForestGetTopology(fine,&topology);CHKERRQ(ierr);
156d222f98bSToby Isaac   ierr = DMForestSetTopology(dm,topology);CHKERRQ(ierr);
157dd8e54a2SToby Isaac   PetscFunctionReturn(0);
158dd8e54a2SToby Isaac }
159dd8e54a2SToby Isaac 
160dd8e54a2SToby Isaac #undef __FUNCT__
161ef51cf95SToby Isaac #define __FUNCT__ "DMForestGetFineForest"
162dd8e54a2SToby Isaac PetscErrorCode DMForestGetFineForest(DM dm, DM *fine)
163dd8e54a2SToby Isaac {
164*88bdff64SToby Isaac   PetscErrorCode ierr;
165dd8e54a2SToby Isaac 
166dd8e54a2SToby Isaac   PetscFunctionBegin;
167*88bdff64SToby Isaac   ierr = DMGetFineDM(dm,fine);CHKERRQ(ierr);
168dd8e54a2SToby Isaac   PetscFunctionReturn(0);
169dd8e54a2SToby Isaac }
170dd8e54a2SToby Isaac 
171dd8e54a2SToby Isaac #undef __FUNCT__
172dd8e54a2SToby Isaac #define __FUNCT__ "DMForestSetAdjacencyDimension"
173dd8e54a2SToby Isaac PetscErrorCode DMForestSetAdjacencyDimension(DM dm, PetscInt adjDim)
174dd8e54a2SToby Isaac {
175dd8e54a2SToby Isaac   PetscInt        dim;
176dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
177dd8e54a2SToby Isaac   PetscErrorCode  ierr;
178dd8e54a2SToby Isaac 
179dd8e54a2SToby Isaac   PetscFunctionBegin;
180dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
181ef51cf95SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the adjacency dimension after setup");
182dd8e54a2SToby Isaac   if (adjDim < 0) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"adjacency dim cannot be < 0: %d", adjDim);
183dd8e54a2SToby Isaac   ierr = DMGetDimension(dm,&dim);CHKERRQ(ierr);
184dd8e54a2SToby Isaac   if (adjDim > dim) SETERRQ2(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"adjacency dim cannot be > %d: %d", dim, adjDim);
185dd8e54a2SToby Isaac   forest->adjDim = adjDim;
186dd8e54a2SToby Isaac   PetscFunctionReturn(0);
187dd8e54a2SToby Isaac }
188dd8e54a2SToby Isaac 
189dd8e54a2SToby Isaac #undef __FUNCT__
190dd8e54a2SToby Isaac #define __FUNCT__ "DMForestSetAdjacencyCodimension"
191dd8e54a2SToby Isaac PetscErrorCode DMForestSetAdjacencyCodimension(DM dm, PetscInt adjCodim)
192dd8e54a2SToby Isaac {
193dd8e54a2SToby Isaac   PetscInt        dim;
194dd8e54a2SToby Isaac   PetscErrorCode  ierr;
195dd8e54a2SToby Isaac 
196dd8e54a2SToby Isaac   PetscFunctionBegin;
197dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
198dd8e54a2SToby Isaac   ierr = DMGetDimension(dm,&dim);CHKERRQ(ierr);
199dd8e54a2SToby Isaac   ierr = DMForestSetAdjacencyDimension(dm,dim-adjCodim);CHKERRQ(ierr);
200dd8e54a2SToby Isaac   PetscFunctionReturn(0);
201dd8e54a2SToby Isaac }
202dd8e54a2SToby Isaac 
203dd8e54a2SToby Isaac #undef __FUNCT__
204dd8e54a2SToby Isaac #define __FUNCT__ "DMForestGetAdjacencyDimension"
205dd8e54a2SToby Isaac PetscErrorCode DMForestGetAdjacencyDimension(DM dm, PetscInt *adjDim)
206dd8e54a2SToby Isaac {
207dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
208dd8e54a2SToby Isaac 
209dd8e54a2SToby Isaac   PetscFunctionBegin;
210dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
211dd8e54a2SToby Isaac   PetscValidIntPointer(adjDim,2);
212dd8e54a2SToby Isaac   *adjDim = forest->adjDim;
213dd8e54a2SToby Isaac   PetscFunctionReturn(0);
214dd8e54a2SToby Isaac }
215dd8e54a2SToby Isaac 
216dd8e54a2SToby Isaac #undef __FUNCT__
217dd8e54a2SToby Isaac #define __FUNCT__ "DMForestGetAdjacencyCodimension"
218dd8e54a2SToby Isaac PetscErrorCode DMForestGetAdjacencyCodimension(DM dm, PetscInt *adjCodim)
219dd8e54a2SToby Isaac {
220dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
221dd8e54a2SToby Isaac   PetscInt       dim;
222dd8e54a2SToby Isaac   PetscErrorCode ierr;
223dd8e54a2SToby Isaac 
224dd8e54a2SToby Isaac   PetscFunctionBegin;
225dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
226dd8e54a2SToby Isaac   PetscValidIntPointer(adjCodim,2);
227dd8e54a2SToby Isaac   ierr = DMGetDimension(dm,&dim);CHKERRQ(ierr);
228dd8e54a2SToby Isaac   *adjCodim = dim - forest->adjDim;
229dd8e54a2SToby Isaac   PetscFunctionReturn(0);
230dd8e54a2SToby Isaac }
231dd8e54a2SToby Isaac 
232dd8e54a2SToby Isaac #undef __FUNCT__
233ef51cf95SToby Isaac #define __FUNCT__ "DMForestSetPartitionOverlap"
234dd8e54a2SToby Isaac PetscErrorCode DMForestSetPartitionOverlap(DM dm, PetscInt overlap)
235dd8e54a2SToby Isaac {
236dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
237dd8e54a2SToby Isaac 
238dd8e54a2SToby Isaac   PetscFunctionBegin;
239dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
240ef51cf95SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the overlap after setup");
241dd8e54a2SToby Isaac   if (overlap < 0) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"overlap cannot be < 0: %d", overlap);
242dd8e54a2SToby Isaac   forest->overlap = overlap;
243dd8e54a2SToby Isaac   PetscFunctionReturn(0);
244dd8e54a2SToby Isaac }
245dd8e54a2SToby Isaac 
246dd8e54a2SToby Isaac #undef __FUNCT__
247dd8e54a2SToby Isaac #define __FUNCT__ "DMForestGetPartitionOverlap"
248dd8e54a2SToby Isaac PetscErrorCode DMForestGetPartitionOverlap(DM dm, PetscInt *overlap)
249dd8e54a2SToby Isaac {
250dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
251dd8e54a2SToby Isaac 
252dd8e54a2SToby Isaac   PetscFunctionBegin;
253dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
254dd8e54a2SToby Isaac   PetscValidIntPointer(overlap,2);
255dd8e54a2SToby Isaac   *overlap = forest->overlap;
256dd8e54a2SToby Isaac   PetscFunctionReturn(0);
257dd8e54a2SToby Isaac }
258dd8e54a2SToby Isaac 
259dd8e54a2SToby Isaac #undef __FUNCT__
260dd8e54a2SToby Isaac #define __FUNCT__ "DMForestSetMinimumRefinement"
261dd8e54a2SToby Isaac PetscErrorCode DMForestSetMinimumRefinement(DM dm, PetscInt minRefinement)
262dd8e54a2SToby Isaac {
263dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
264dd8e54a2SToby Isaac 
265dd8e54a2SToby Isaac   PetscFunctionBegin;
266dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
267ef51cf95SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the minimum refinement after setup");
268dd8e54a2SToby Isaac   forest->minRefinement = minRefinement;
269dd8e54a2SToby Isaac   PetscFunctionReturn(0);
270dd8e54a2SToby Isaac }
271dd8e54a2SToby Isaac 
272dd8e54a2SToby Isaac #undef __FUNCT__
273dd8e54a2SToby Isaac #define __FUNCT__ "DMForestGetMinimumRefinement"
274dd8e54a2SToby Isaac PetscErrorCode DMForestGetMinimumRefinement(DM dm, PetscInt *minRefinement)
275dd8e54a2SToby Isaac {
276dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
277dd8e54a2SToby Isaac 
278dd8e54a2SToby Isaac   PetscFunctionBegin;
279dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
280dd8e54a2SToby Isaac   PetscValidIntPointer(minRefinement,2);
281dd8e54a2SToby Isaac   *minRefinement = forest->minRefinement;
282dd8e54a2SToby Isaac   PetscFunctionReturn(0);
283dd8e54a2SToby Isaac }
284dd8e54a2SToby Isaac 
285dd8e54a2SToby Isaac #undef __FUNCT__
28656ba9f64SToby Isaac #define __FUNCT__ "DMForestSetInitialRefinement"
28756ba9f64SToby Isaac PetscErrorCode DMForestSetInitialRefinement(DM dm, PetscInt initRefinement)
28856ba9f64SToby Isaac {
28956ba9f64SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
29056ba9f64SToby Isaac 
29156ba9f64SToby Isaac   PetscFunctionBegin;
29256ba9f64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
29356ba9f64SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the initial refinement after setup");
29456ba9f64SToby Isaac   forest->initRefinement = initRefinement;
29556ba9f64SToby Isaac   PetscFunctionReturn(0);
29656ba9f64SToby Isaac }
29756ba9f64SToby Isaac 
29856ba9f64SToby Isaac #undef __FUNCT__
29956ba9f64SToby Isaac #define __FUNCT__ "DMForestGetInitialRefinement"
30056ba9f64SToby Isaac PetscErrorCode DMForestGetInitialRefinement(DM dm, PetscInt *initRefinement)
30156ba9f64SToby Isaac {
30256ba9f64SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
30356ba9f64SToby Isaac 
30456ba9f64SToby Isaac   PetscFunctionBegin;
30556ba9f64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
30656ba9f64SToby Isaac   PetscValidIntPointer(initRefinement,2);
30756ba9f64SToby Isaac   *initRefinement = forest->initRefinement;
30856ba9f64SToby Isaac   PetscFunctionReturn(0);
30956ba9f64SToby Isaac }
31056ba9f64SToby Isaac 
31156ba9f64SToby Isaac #undef __FUNCT__
312c7eeac06SToby Isaac #define __FUNCT__ "DMForestSetMaximumRefinement"
313c7eeac06SToby Isaac PetscErrorCode DMForestSetMaximumRefinement(DM dm, PetscInt maxRefinement)
314dd8e54a2SToby Isaac {
315dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
316dd8e54a2SToby Isaac 
317dd8e54a2SToby Isaac   PetscFunctionBegin;
318dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
319ef51cf95SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the maximum refinement after setup");
320c7eeac06SToby Isaac   forest->maxRefinement = maxRefinement;
321dd8e54a2SToby Isaac   PetscFunctionReturn(0);
322dd8e54a2SToby Isaac }
323dd8e54a2SToby Isaac 
324dd8e54a2SToby Isaac #undef __FUNCT__
325c7eeac06SToby Isaac #define __FUNCT__ "DMForestGetMaximumRefinement"
326c7eeac06SToby Isaac PetscErrorCode DMForestGetMaximumRefinement(DM dm, PetscInt *maxRefinement)
327dd8e54a2SToby Isaac {
328dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
329dd8e54a2SToby Isaac 
330dd8e54a2SToby Isaac   PetscFunctionBegin;
331dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
332c7eeac06SToby Isaac   PetscValidIntPointer(maxRefinement,2);
333c7eeac06SToby Isaac   *maxRefinement = forest->maxRefinement;
334dd8e54a2SToby Isaac   PetscFunctionReturn(0);
335dd8e54a2SToby Isaac }
336c7eeac06SToby Isaac 
337c7eeac06SToby Isaac #undef __FUNCT__
338c7eeac06SToby Isaac #define __FUNCT__ "DMForestSetAdaptivityStrategy"
339c7eeac06SToby Isaac PetscErrorCode DMForestSetAdaptivityStrategy(DM dm, DMForestAdaptivityStrategy adaptStrategy)
340c7eeac06SToby Isaac {
341c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
342c7eeac06SToby Isaac   PetscErrorCode ierr;
343c7eeac06SToby Isaac 
344c7eeac06SToby Isaac   PetscFunctionBegin;
345c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
346c7eeac06SToby Isaac   ierr = PetscFree(forest->adaptStrategy);CHKERRQ(ierr);
347c7eeac06SToby Isaac   ierr = PetscStrallocpy((const char *)adaptStrategy,(char **)adaptStrategy);CHKERRQ(ierr);
348c7eeac06SToby Isaac   PetscFunctionReturn(0);
349c7eeac06SToby Isaac }
350c7eeac06SToby Isaac 
351c7eeac06SToby Isaac #undef __FUNCT__
352c7eeac06SToby Isaac #define __FUNCT__ "DMForestGetAdaptivityStrategy"
353c7eeac06SToby Isaac PetscErrorCode DMForestGetAdaptivityStrategy(DM dm, DMForestAdaptivityStrategy *adaptStrategy)
354c7eeac06SToby Isaac {
355c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
356c7eeac06SToby Isaac 
357c7eeac06SToby Isaac   PetscFunctionBegin;
358c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
359c7eeac06SToby Isaac   PetscValidPointer(adaptStrategy,2);
360c7eeac06SToby Isaac   *adaptStrategy = forest->adaptStrategy;
361c7eeac06SToby Isaac   PetscFunctionReturn(0);
362c7eeac06SToby Isaac }
363c7eeac06SToby Isaac 
364c7eeac06SToby Isaac #undef __FUNCT__
365c7eeac06SToby Isaac #define __FUNCT__ "DMForestSetGradeFactor"
366c7eeac06SToby Isaac PetscErrorCode DMForestSetGradeFactor(DM dm, PetscInt grade)
367c7eeac06SToby Isaac {
368c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
369c7eeac06SToby Isaac 
370c7eeac06SToby Isaac   PetscFunctionBegin;
371c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
372ef51cf95SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the grade factor after setup");
373c7eeac06SToby Isaac   forest->gradeFactor = grade;
374c7eeac06SToby Isaac   PetscFunctionReturn(0);
375c7eeac06SToby Isaac }
376c7eeac06SToby Isaac 
377c7eeac06SToby Isaac #undef __FUNCT__
378c7eeac06SToby Isaac #define __FUNCT__ "DMForestGetGradeFactor"
379c7eeac06SToby Isaac PetscErrorCode DMForestGetGradeFactor(DM dm, PetscInt *grade)
380c7eeac06SToby Isaac {
381c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
382c7eeac06SToby Isaac 
383c7eeac06SToby Isaac   PetscFunctionBegin;
384c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
385c7eeac06SToby Isaac   PetscValidIntPointer(grade,2);
386c7eeac06SToby Isaac   *grade = forest->gradeFactor;
387c7eeac06SToby Isaac   PetscFunctionReturn(0);
388c7eeac06SToby Isaac }
389c7eeac06SToby Isaac 
390c7eeac06SToby Isaac #undef __FUNCT__
391ef51cf95SToby Isaac #define __FUNCT__ "DMForestSetCellWeightFactor"
392ef51cf95SToby Isaac PetscErrorCode DMForestSetCellWeightFactor(DM dm, PetscReal weightsFactor)
393c7eeac06SToby Isaac {
394c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
395c7eeac06SToby Isaac 
396c7eeac06SToby Isaac   PetscFunctionBegin;
397c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
398ef51cf95SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the weights factor after setup");
399c7eeac06SToby Isaac   forest->weightsFactor = weightsFactor;
400c7eeac06SToby Isaac   PetscFunctionReturn(0);
401c7eeac06SToby Isaac }
402c7eeac06SToby Isaac 
403c7eeac06SToby Isaac #undef __FUNCT__
404ef51cf95SToby Isaac #define __FUNCT__ "DMForestGetCellWeightFactor"
405ef51cf95SToby Isaac PetscErrorCode DMForestGetCellWeightFactor(DM dm, PetscReal *weightsFactor)
406c7eeac06SToby Isaac {
407c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
408c7eeac06SToby Isaac 
409c7eeac06SToby Isaac   PetscFunctionBegin;
410c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
411c7eeac06SToby Isaac   PetscValidRealPointer(weightsFactor,2);
412c7eeac06SToby Isaac   *weightsFactor = forest->weightsFactor;
413c7eeac06SToby Isaac   PetscFunctionReturn(0);
414c7eeac06SToby Isaac }
415c7eeac06SToby Isaac 
416c7eeac06SToby Isaac #undef __FUNCT__
417c7eeac06SToby Isaac #define __FUNCT__ "DMForestGetCellChart"
418c7eeac06SToby Isaac PetscErrorCode DMForestGetCellChart(DM dm, PetscInt *cStart, PetscInt *cEnd)
419c7eeac06SToby Isaac {
420c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
421c7eeac06SToby Isaac   PetscErrorCode ierr;
422c7eeac06SToby Isaac 
423c7eeac06SToby Isaac   PetscFunctionBegin;
424c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
425c7eeac06SToby Isaac   PetscValidIntPointer(cStart,2);
426c7eeac06SToby Isaac   PetscValidIntPointer(cEnd,2);
427c7eeac06SToby Isaac   if (((forest->cStart == PETSC_DETERMINE) || (forest->cEnd == PETSC_DETERMINE)) && forest->createcellchart) {
428c7eeac06SToby Isaac     ierr = forest->createcellchart(dm,&forest->cStart,&forest->cEnd);CHKERRQ(ierr);
429c7eeac06SToby Isaac   }
430c7eeac06SToby Isaac   *cStart =  forest->cStart;
431c7eeac06SToby Isaac   *cEnd   =  forest->cEnd;
432c7eeac06SToby Isaac   PetscFunctionReturn(0);
433c7eeac06SToby Isaac }
434c7eeac06SToby Isaac 
435c7eeac06SToby Isaac #undef __FUNCT__
436c7eeac06SToby Isaac #define __FUNCT__ "DMForestGetCellSF"
437c7eeac06SToby Isaac PetscErrorCode DMForestGetCellSF(DM dm, PetscSF *cellSF)
438c7eeac06SToby Isaac {
439c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
440c7eeac06SToby Isaac   PetscErrorCode ierr;
441c7eeac06SToby Isaac 
442c7eeac06SToby Isaac   PetscFunctionBegin;
443c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
444c7eeac06SToby Isaac   PetscValidPointer(cellSF,2);
445c7eeac06SToby Isaac   if ((!forest->cellSF) && forest->createcellsf) {
446c7eeac06SToby Isaac     ierr = forest->createcellsf(dm,&forest->cellSF);CHKERRQ(ierr);
447c7eeac06SToby Isaac   }
448c7eeac06SToby Isaac   *cellSF = forest->cellSF;
449c7eeac06SToby Isaac   PetscFunctionReturn(0);
450c7eeac06SToby Isaac }
451c7eeac06SToby Isaac 
452c7eeac06SToby Isaac #undef __FUNCT__
453c7eeac06SToby Isaac #define __FUNCT__ "DMForestSetAdaptivityMarkers"
454c7eeac06SToby Isaac PetscErrorCode DMForestSetAdaptivityMarkers(DM dm, PetscInt markers[], PetscCopyMode copyMode)
455c7eeac06SToby Isaac {
456c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
457c7eeac06SToby Isaac   PetscInt       cStart, cEnd;
458c7eeac06SToby Isaac   PetscErrorCode ierr;
459c7eeac06SToby Isaac 
460c7eeac06SToby Isaac   PetscFunctionBegin;
461c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
462c7eeac06SToby Isaac   ierr = DMForestGetCellChart(dm,&cStart,&cEnd);CHKERRQ(ierr);
463c7eeac06SToby Isaac   if (cEnd < cStart) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"cell chart [%d,%d) is not valid",cStart,cEnd);
464c7eeac06SToby Isaac   if (copyMode == PETSC_COPY_VALUES) {
465c7eeac06SToby Isaac     if (forest->adaptCopyMode != PETSC_OWN_POINTER || forest->adaptMarkers == markers) {
466c7eeac06SToby Isaac       ierr = PetscMalloc1(cEnd-cStart,&forest->adaptMarkers);CHKERRQ(ierr);
467c7eeac06SToby Isaac     }
468c7eeac06SToby Isaac     ierr = PetscMemcpy(forest->adaptMarkers,markers,(cEnd-cStart)*sizeof(*markers));CHKERRQ(ierr);
469c7eeac06SToby Isaac     forest->adaptCopyMode = PETSC_OWN_POINTER;
470c7eeac06SToby Isaac     PetscFunctionReturn(0);
471c7eeac06SToby Isaac   }
472c7eeac06SToby Isaac   if (forest->adaptCopyMode == PETSC_OWN_POINTER) {
473c7eeac06SToby Isaac     ierr = PetscFree(forest->adaptMarkers);CHKERRQ(ierr);
474c7eeac06SToby Isaac   }
475c7eeac06SToby Isaac   forest->adaptMarkers  = markers;
476c7eeac06SToby Isaac   forest->adaptCopyMode = copyMode;
477c7eeac06SToby Isaac   PetscFunctionReturn(0);
478c7eeac06SToby Isaac }
479c7eeac06SToby Isaac 
480c7eeac06SToby Isaac #undef __FUNCT__
481c7eeac06SToby Isaac #define __FUNCT__ "DMForestGetAdaptivityMarkers"
482c7eeac06SToby Isaac PetscErrorCode DMForestGetAdaptivityMarkers(DM dm, PetscInt **markers)
483c7eeac06SToby Isaac {
484c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
485c7eeac06SToby Isaac 
486c7eeac06SToby Isaac   PetscFunctionBegin;
487c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
488c7eeac06SToby Isaac   PetscValidPointer(markers,2);
489c7eeac06SToby Isaac   *markers = forest->adaptMarkers;
490c7eeac06SToby Isaac   PetscFunctionReturn(0);
491c7eeac06SToby Isaac }
492c7eeac06SToby Isaac 
493c7eeac06SToby Isaac #undef __FUNCT__
494c7eeac06SToby Isaac #define __FUNCT__ "DMForestSetCellWeights"
495c7eeac06SToby Isaac PetscErrorCode DMForestSetCellWeights(DM dm, PetscReal weights[], PetscCopyMode copyMode)
496c7eeac06SToby Isaac {
497c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
498c7eeac06SToby Isaac   PetscInt       cStart, cEnd;
499c7eeac06SToby Isaac   PetscErrorCode ierr;
500c7eeac06SToby Isaac 
501c7eeac06SToby Isaac   PetscFunctionBegin;
502c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
503c7eeac06SToby Isaac   ierr = DMForestGetCellChart(dm,&cStart,&cEnd);CHKERRQ(ierr);
504c7eeac06SToby Isaac   if (cEnd < cStart) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"cell chart [%d,%d) is not valid",cStart,cEnd);
505c7eeac06SToby Isaac   if (copyMode == PETSC_COPY_VALUES) {
506c7eeac06SToby Isaac     if (forest->cellWeightsCopyMode != PETSC_OWN_POINTER || forest->cellWeights == weights) {
507c7eeac06SToby Isaac       ierr = PetscMalloc1(cEnd-cStart,&forest->cellWeights);CHKERRQ(ierr);
508c7eeac06SToby Isaac     }
509c7eeac06SToby Isaac     ierr = PetscMemcpy(forest->cellWeights,weights,(cEnd-cStart)*sizeof(*weights));CHKERRQ(ierr);
510c7eeac06SToby Isaac     forest->cellWeightsCopyMode = PETSC_OWN_POINTER;
511c7eeac06SToby Isaac     PetscFunctionReturn(0);
512c7eeac06SToby Isaac   }
513c7eeac06SToby Isaac   if (forest->cellWeightsCopyMode == PETSC_OWN_POINTER) {
514c7eeac06SToby Isaac     ierr = PetscFree(forest->cellWeights);CHKERRQ(ierr);
515c7eeac06SToby Isaac   }
516c7eeac06SToby Isaac   forest->cellWeights  = weights;
517c7eeac06SToby Isaac   forest->cellWeightsCopyMode = copyMode;
518c7eeac06SToby Isaac   PetscFunctionReturn(0);
519c7eeac06SToby Isaac }
520c7eeac06SToby Isaac 
521c7eeac06SToby Isaac #undef __FUNCT__
522c7eeac06SToby Isaac #define __FUNCT__ "DMForestGetCellWeights"
523c7eeac06SToby Isaac PetscErrorCode DMForestGetCellWeights(DM dm, PetscReal **weights)
524c7eeac06SToby Isaac {
525c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
526c7eeac06SToby Isaac 
527c7eeac06SToby Isaac   PetscFunctionBegin;
528c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
529c7eeac06SToby Isaac   PetscValidPointer(weights,2);
530c7eeac06SToby Isaac   *weights = forest->cellWeights;
531c7eeac06SToby Isaac   PetscFunctionReturn(0);
532c7eeac06SToby Isaac }
533c7eeac06SToby Isaac 
534c7eeac06SToby Isaac #undef __FUNCT__
535c7eeac06SToby Isaac #define __FUNCT__ "DMForestSetWeightCapacity"
536c7eeac06SToby Isaac PetscErrorCode DMForestSetWeightCapacity(DM dm, PetscReal capacity)
537c7eeac06SToby Isaac {
538c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
539c7eeac06SToby Isaac 
540c7eeac06SToby Isaac   PetscFunctionBegin;
541c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
542ef51cf95SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the weight capacity after setup");
543c7eeac06SToby Isaac   if (capacity < 0.) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"Cannot have negative weight capacity; %f",capacity);
544c7eeac06SToby Isaac   forest->weightCapacity = capacity;
545c7eeac06SToby Isaac   PetscFunctionReturn(0);
546c7eeac06SToby Isaac }
547c7eeac06SToby Isaac 
548c7eeac06SToby Isaac #undef __FUNCT__
549c7eeac06SToby Isaac #define __FUNCT__ "DMForestGetWeightCapacity"
550c7eeac06SToby Isaac PetscErrorCode DMForestGetWeightCapacity(DM dm, PetscReal *capacity)
551c7eeac06SToby Isaac {
552c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
553c7eeac06SToby Isaac 
554c7eeac06SToby Isaac   PetscFunctionBegin;
555c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
556c7eeac06SToby Isaac   PetscValidRealPointer(capacity,2);
557c7eeac06SToby Isaac   *capacity = forest->weightCapacity;
558c7eeac06SToby Isaac   PetscFunctionReturn(0);
559c7eeac06SToby Isaac }
560c7eeac06SToby Isaac 
561dd8e54a2SToby Isaac #undef __FUNCT__
562db4d5e8cSToby Isaac #define __FUNCT__ "DMSetFromOptions_Forest"
5635c8434f9SToby Isaac PETSC_EXTERN PetscErrorCode DMSetFromOptions_Forest(PetscOptions *PetscOptionsObject,DM dm)
564db4d5e8cSToby Isaac {
565db4d5e8cSToby Isaac   DM_Forest                  *forest = (DM_Forest *) dm->data;
56656ba9f64SToby Isaac   PetscBool                  flg, flg1, flg2, flg3, flg4;
567dd8e54a2SToby Isaac   DMForestTopology           oldTopo;
568c7eeac06SToby Isaac   char                       stringBuffer[256];
569dd8e54a2SToby Isaac   PetscViewer                viewer;
570dd8e54a2SToby Isaac   PetscViewerFormat          format;
57156ba9f64SToby Isaac   PetscInt                   adjDim, adjCodim, overlap, minRefinement, initRefinement, maxRefinement, grade;
572c7eeac06SToby Isaac   PetscReal                  weightsFactor;
573c7eeac06SToby Isaac   DMForestAdaptivityStrategy adaptStrategy;
574db4d5e8cSToby Isaac   PetscErrorCode             ierr;
575db4d5e8cSToby Isaac 
576db4d5e8cSToby Isaac   PetscFunctionBegin;
577db4d5e8cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
578db4d5e8cSToby Isaac   forest->setFromOptions = PETSC_TRUE;
5795c8434f9SToby Isaac   ierr = PetscOptionsHead(PetscOptionsObject,"DMForest Options");CHKERRQ(ierr);
580dd8e54a2SToby Isaac   ierr = DMForestGetTopology(dm, &oldTopo);CHKERRQ(ierr);
58156ba9f64SToby Isaac   ierr = PetscOptionsString("-dm_forest_topology","the topology of the forest's base mesh","DMForestSetTopology",oldTopo,stringBuffer,256,&flg1);CHKERRQ(ierr);
58256ba9f64SToby Isaac   ierr = PetscOptionsViewer("-dm_forest_base_dm","load the base DM from a viewer specification","DMForestSetBaseDM",&viewer,&format,&flg2);CHKERRQ(ierr);
58356ba9f64SToby Isaac   ierr = PetscOptionsViewer("-dm_forest_coarse_forest","load the coarse forest from a viewer specification","DMForestSetCoarseForest",&viewer,&format,&flg3);CHKERRQ(ierr);
58456ba9f64SToby Isaac   ierr = PetscOptionsViewer("-dm_forest_fine_forest","load the fine forest from a viewer specification","DMForestSetFineForest",&viewer,&format,&flg4);CHKERRQ(ierr);
58556ba9f64SToby Isaac   if ((PetscInt) flg1 + (PetscInt) flg2 + (PetscInt) flg3 + (PetscInt) flg4 > 1) {
58656ba9f64SToby Isaac     SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_INCOMP,"Specify only one of -dm_forest_{topology,base_dm,coarse_forest,fine_forest}");
587dd8e54a2SToby Isaac   }
58856ba9f64SToby Isaac   if (flg1) {
58956ba9f64SToby Isaac     ierr = DMForestSetTopology(dm,(DMForestTopology)stringBuffer);CHKERRQ(ierr);
59056ba9f64SToby Isaac     ierr = DMForestSetBaseDM(dm,NULL);CHKERRQ(ierr);
59156ba9f64SToby Isaac     ierr = DMForestSetCoarseForest(dm,NULL);CHKERRQ(ierr);
59256ba9f64SToby Isaac     ierr = DMForestSetFineForest(dm,NULL);CHKERRQ(ierr);
59356ba9f64SToby Isaac   }
59456ba9f64SToby Isaac   if (flg2) {
595dd8e54a2SToby Isaac     DM         base;
596dd8e54a2SToby Isaac 
597dd8e54a2SToby Isaac     ierr = DMCreate(PetscObjectComm((PetscObject)dm),&base);CHKERRQ(ierr);
598dd8e54a2SToby Isaac     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
599dd8e54a2SToby Isaac     ierr = DMLoad(base,viewer);CHKERRQ(ierr);
600dd8e54a2SToby Isaac     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
601dd8e54a2SToby Isaac     ierr = DMForestSetBaseDM(dm,base);CHKERRQ(ierr);
602dd8e54a2SToby Isaac     ierr = DMDestroy(&base);CHKERRQ(ierr);
60356ba9f64SToby Isaac     ierr = DMForestSetTopology(dm,NULL);CHKERRQ(ierr);
60456ba9f64SToby Isaac     ierr = DMForestSetCoarseForest(dm,NULL);CHKERRQ(ierr);
60556ba9f64SToby Isaac     ierr = DMForestSetFineForest(dm,NULL);CHKERRQ(ierr);
606dd8e54a2SToby Isaac   }
60756ba9f64SToby Isaac   if (flg3) {
608dd8e54a2SToby Isaac     DM         coarse;
609dd8e54a2SToby Isaac 
610dd8e54a2SToby Isaac     ierr = DMCreate(PetscObjectComm((PetscObject)dm),&coarse);CHKERRQ(ierr);
611dd8e54a2SToby Isaac     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
612dd8e54a2SToby Isaac     ierr = DMLoad(coarse,viewer);CHKERRQ(ierr);
613dd8e54a2SToby Isaac     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
614dd8e54a2SToby Isaac     ierr = DMForestSetCoarseForest(dm,coarse);CHKERRQ(ierr);
615dd8e54a2SToby Isaac     ierr = DMDestroy(&coarse);CHKERRQ(ierr);
61656ba9f64SToby Isaac     ierr = DMForestSetTopology(dm,NULL);CHKERRQ(ierr);
61756ba9f64SToby Isaac     ierr = DMForestSetBaseDM(dm,NULL);CHKERRQ(ierr);
61856ba9f64SToby Isaac     ierr = DMForestSetFineForest(dm,NULL);CHKERRQ(ierr);
619dd8e54a2SToby Isaac   }
62056ba9f64SToby Isaac   if (flg4) {
621dd8e54a2SToby Isaac     DM         fine;
622dd8e54a2SToby Isaac 
623dd8e54a2SToby Isaac     ierr = DMCreate(PetscObjectComm((PetscObject)dm),&fine);CHKERRQ(ierr);
624dd8e54a2SToby Isaac     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
625dd8e54a2SToby Isaac     ierr = DMLoad(fine,viewer);CHKERRQ(ierr);
626dd8e54a2SToby Isaac     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
627dd8e54a2SToby Isaac     ierr = DMForestSetFineForest(dm,fine);CHKERRQ(ierr);
628dd8e54a2SToby Isaac     ierr = DMDestroy(&fine);CHKERRQ(ierr);
62956ba9f64SToby Isaac     ierr = DMForestSetTopology(dm,NULL);CHKERRQ(ierr);
63056ba9f64SToby Isaac     ierr = DMForestSetBaseDM(dm,NULL);CHKERRQ(ierr);
63156ba9f64SToby Isaac     ierr = DMForestSetCoarseForest(dm,NULL);CHKERRQ(ierr);
632dd8e54a2SToby Isaac   }
633dd8e54a2SToby Isaac   ierr = DMForestGetAdjacencyDimension(dm,&adjDim);CHKERRQ(ierr);
634dd8e54a2SToby Isaac   ierr = PetscOptionsInt("-dm_forest_adjacency_dimension","set the dimension of points that define adjacency in the forest","DMForestSetAdjacencyDimension",adjDim,&adjDim,&flg);CHKERRQ(ierr);
635dd8e54a2SToby Isaac   if (flg) {
636dd8e54a2SToby Isaac     ierr = DMForestSetAdjacencyDimension(dm,adjDim);CHKERRQ(ierr);
637dd8e54a2SToby Isaac   }
638dd8e54a2SToby Isaac   else {
639dd8e54a2SToby Isaac     ierr = DMForestGetAdjacencyCodimension(dm,&adjCodim);CHKERRQ(ierr);
640dd8e54a2SToby Isaac     ierr = PetscOptionsInt("-dm_forest_adjacency_codimension","set the codimension of points that define adjacency in the forest","DMForestSetAdjacencyCodimension",adjCodim,&adjCodim,&flg);CHKERRQ(ierr);
641dd8e54a2SToby Isaac     if (flg) {
642dd8e54a2SToby Isaac       ierr = DMForestSetAdjacencyCodimension(dm,adjCodim);CHKERRQ(ierr);
643dd8e54a2SToby Isaac     }
644dd8e54a2SToby Isaac   }
645dd8e54a2SToby Isaac   ierr = DMForestGetPartitionOverlap(dm,&overlap);CHKERRQ(ierr);
646dd8e54a2SToby Isaac   ierr = PetscOptionsInt("-dm_forest_partition_overlap","set the degree of partition overlap","DMForestSetPartitionOverlap",overlap,&overlap,&flg);CHKERRQ(ierr);
647dd8e54a2SToby Isaac   if (flg) {
648dd8e54a2SToby Isaac     ierr = DMForestSetPartitionOverlap(dm,overlap);CHKERRQ(ierr);
649dd8e54a2SToby Isaac   }
650dd8e54a2SToby Isaac   ierr = DMForestGetMinimumRefinement(dm,&minRefinement);CHKERRQ(ierr);
651dd8e54a2SToby Isaac   ierr = PetscOptionsInt("-dm_forest_minimum_refinement","set the minimum level of refinement in the forest","DMForestSetMinimumRefinement",minRefinement,&minRefinement,&flg);CHKERRQ(ierr);
652dd8e54a2SToby Isaac   if (flg) {
653dd8e54a2SToby Isaac     ierr = DMForestSetMinimumRefinement(dm,minRefinement);CHKERRQ(ierr);
654db4d5e8cSToby Isaac   }
65556ba9f64SToby Isaac   ierr = DMForestGetInitialRefinement(dm,&initRefinement);CHKERRQ(ierr);
65656ba9f64SToby Isaac   ierr = PetscOptionsInt("-dm_forest_initial_refinement","set the initial level of refinement in the forest","DMForestSetInitialRefinement",initRefinement,&initRefinement,&flg);CHKERRQ(ierr);
65756ba9f64SToby Isaac   if (flg) {
65856ba9f64SToby Isaac     ierr = DMForestSetInitialRefinement(dm,initRefinement);CHKERRQ(ierr);
65956ba9f64SToby Isaac   }
660c7eeac06SToby Isaac   ierr = DMForestGetMaximumRefinement(dm,&maxRefinement);CHKERRQ(ierr);
661c7eeac06SToby Isaac   ierr = PetscOptionsInt("-dm_forest_maximum_refinement","set the maximum level of refinement in the forest","DMForestSetMaximumRefinement",maxRefinement,&maxRefinement,&flg);CHKERRQ(ierr);
662c7eeac06SToby Isaac   if (flg) {
663c7eeac06SToby Isaac     ierr = DMForestSetMaximumRefinement(dm,maxRefinement);CHKERRQ(ierr);
664c7eeac06SToby Isaac   }
665c7eeac06SToby Isaac   ierr = DMForestGetAdaptivityStrategy(dm,&adaptStrategy);CHKERRQ(ierr);
666c7eeac06SToby Isaac   ierr = PetscOptionsString("-dm_forest_adaptivity_strategy","the forest's adaptivity-flag resolution strategy","DMForestSetAdaptivityStrategy",adaptStrategy,stringBuffer,256,&flg);CHKERRQ(ierr);
667c7eeac06SToby Isaac   if (flg) {
668c7eeac06SToby Isaac     ierr = DMForestSetAdaptivityStrategy(dm,(DMForestAdaptivityStrategy)stringBuffer);CHKERRQ(ierr);
669c7eeac06SToby Isaac   }
670c7eeac06SToby Isaac   ierr = DMForestGetGradeFactor(dm,&grade);CHKERRQ(ierr);
671c7eeac06SToby Isaac   ierr = PetscOptionsInt("-dm_forest_grade_factor","grade factor between neighboring cells","DMForestSetGradeFactor",grade,&grade,&flg);CHKERRQ(ierr);
672c7eeac06SToby Isaac   if (flg) {
673c7eeac06SToby Isaac     ierr = DMForestSetGradeFactor(dm,grade);CHKERRQ(ierr);
674c7eeac06SToby Isaac   }
675c7eeac06SToby Isaac   ierr = DMForestGetCellWeightFactor(dm,&weightsFactor);CHKERRQ(ierr);
676c7eeac06SToby Isaac   ierr = PetscOptionsReal("-dm_forest_cell_weight_factor","multiplying weight factor for cell refinement","DMForestSetCellWeightFactor",weightsFactor,&weightsFactor,&flg);CHKERRQ(ierr);
677c7eeac06SToby Isaac   if (flg) {
678c7eeac06SToby Isaac     ierr = DMForestSetCellWeightFactor(dm,weightsFactor);CHKERRQ(ierr);
679c7eeac06SToby Isaac   }
680db4d5e8cSToby Isaac   ierr = PetscOptionsTail();CHKERRQ(ierr);
681db4d5e8cSToby Isaac   PetscFunctionReturn(0);
682db4d5e8cSToby Isaac }
683db4d5e8cSToby Isaac 
684db4d5e8cSToby Isaac #undef __FUNCT__
685d222f98bSToby Isaac #define __FUNCT__ "DMInitialize_Forest"
686d222f98bSToby Isaac static PetscErrorCode DMInitialize_Forest(DM dm)
687d222f98bSToby Isaac {
688d222f98bSToby Isaac   PetscErrorCode ierr;
689d222f98bSToby Isaac 
690d222f98bSToby Isaac   PetscFunctionBegin;
691d222f98bSToby Isaac   ierr = PetscMemzero(dm->ops,sizeof(*(dm->ops)));CHKERRQ(ierr);
692d222f98bSToby Isaac 
693d222f98bSToby Isaac   dm->ops->clone          = DMClone_Forest;
694d222f98bSToby Isaac   dm->ops->setfromoptions = DMSetFromOptions_Forest;
695d222f98bSToby Isaac   dm->ops->destroy        = DMDestroy_Forest;
696d222f98bSToby Isaac   PetscFunctionReturn(0);
697d222f98bSToby Isaac }
698d222f98bSToby Isaac 
699d222f98bSToby Isaac #undef __FUNCT__
700db4d5e8cSToby Isaac #define __FUNCT__ "DMCreate_Forest"
701db4d5e8cSToby Isaac PETSC_EXTERN PetscErrorCode DMCreate_Forest(DM dm)
702db4d5e8cSToby Isaac {
703db4d5e8cSToby Isaac   DM_Forest      *forest;
704db4d5e8cSToby Isaac   PetscErrorCode ierr;
705db4d5e8cSToby Isaac 
706db4d5e8cSToby Isaac   PetscFunctionBegin;
707db4d5e8cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
708db4d5e8cSToby Isaac   ierr                        = PetscNewLog(dm,&forest);CHKERRQ(ierr);
709db4d5e8cSToby Isaac   dm->dim                     = 0;
710db4d5e8cSToby Isaac   dm->data                    = forest;
711db4d5e8cSToby Isaac   forest->refct               = 1;
712db4d5e8cSToby Isaac   forest->data                = NULL;
713dd8e54a2SToby Isaac   forest->setFromOptions      = PETSC_FALSE;
714db4d5e8cSToby Isaac   forest->topology            = NULL;
715db4d5e8cSToby Isaac   forest->base                = NULL;
716db4d5e8cSToby Isaac   forest->adjDim              = PETSC_DEFAULT;
717db4d5e8cSToby Isaac   forest->overlap             = PETSC_DEFAULT;
718db4d5e8cSToby Isaac   forest->minRefinement       = PETSC_DEFAULT;
719db4d5e8cSToby Isaac   forest->maxRefinement       = PETSC_DEFAULT;
72056ba9f64SToby Isaac   forest->initRefinement      = PETSC_DEFAULT;
721c7eeac06SToby Isaac   forest->cStart              = PETSC_DETERMINE;
722c7eeac06SToby Isaac   forest->cEnd                = PETSC_DETERMINE;
723db4d5e8cSToby Isaac   forest->cellSF              = 0;
724db4d5e8cSToby Isaac   forest->adaptMarkers        = NULL;
725db4d5e8cSToby Isaac   forest->adaptCopyMode       = PETSC_USE_POINTER;
726db4d5e8cSToby Isaac   forest->adaptStrategy       = DMFORESTADAPTALL;
727db4d5e8cSToby Isaac   forest->gradeFactor         = 2;
728db4d5e8cSToby Isaac   forest->cellWeights         = NULL;
729db4d5e8cSToby Isaac   forest->cellWeightsCopyMode = PETSC_USE_POINTER;
730db4d5e8cSToby Isaac   forest->weightsFactor       = 1.;
731db4d5e8cSToby Isaac   forest->weightCapacity      = 1.;
732d222f98bSToby Isaac   ierr = DMInitialize_Forest(dm);CHKERRQ(ierr);
733db4d5e8cSToby Isaac   PetscFunctionReturn(0);
734db4d5e8cSToby Isaac }
735db4d5e8cSToby Isaac 
736