xref: /petsc/src/dm/impls/forest/forest.c (revision d8984e3bdf0f026b480e7f0cf5caa02e9e5a5cd9)
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 
4101d9d024SToby Isaac static PetscErrorCode DMInitialize_Forest(DM dm);
4201d9d024SToby Isaac 
43a0452a8eSToby Isaac #undef __FUNCT__
44db4d5e8cSToby Isaac #define __FUNCT__ "DMClone_Forest"
45db4d5e8cSToby Isaac PETSC_EXTERN PetscErrorCode DMClone_Forest(DM dm, DM *newdm)
46db4d5e8cSToby Isaac {
47db4d5e8cSToby Isaac   DM_Forest        *forest = (DM_Forest *) dm->data;
48db4d5e8cSToby Isaac   const char       *type;
49db4d5e8cSToby Isaac   PetscErrorCode ierr;
50db4d5e8cSToby Isaac 
51db4d5e8cSToby Isaac   PetscFunctionBegin;
52db4d5e8cSToby Isaac   forest->refct++;
53db4d5e8cSToby Isaac   (*newdm)->data = forest;
54db4d5e8cSToby Isaac   ierr = PetscObjectGetType((PetscObject) dm, &type);CHKERRQ(ierr);
55db4d5e8cSToby Isaac   ierr = PetscObjectChangeTypeName((PetscObject) *newdm, type);CHKERRQ(ierr);
5601d9d024SToby Isaac   ierr = DMInitialize_Forest(*newdm);CHKERRQ(ierr);
57db4d5e8cSToby Isaac   PetscFunctionReturn(0);
58db4d5e8cSToby Isaac }
59db4d5e8cSToby Isaac 
60db4d5e8cSToby Isaac #undef __FUNCT__
61db4d5e8cSToby Isaac #define __FUNCT__ "DMDestroy_Forest"
62d222f98bSToby Isaac static PetscErrorCode DMDestroy_Forest(DM dm)
63db4d5e8cSToby Isaac {
64db4d5e8cSToby Isaac   DM_Forest     *forest = (DM_Forest*) dm->data;
65db4d5e8cSToby Isaac   PetscErrorCode ierr;
66db4d5e8cSToby Isaac 
67db4d5e8cSToby Isaac   PetscFunctionBegin;
68db4d5e8cSToby Isaac   if (--forest->refct > 0) PetscFunctionReturn(0);
69d222f98bSToby Isaac   if (forest->destroy) {ierr = forest->destroy(dm);CHKERRQ(ierr);}
70db4d5e8cSToby Isaac   ierr = PetscSFDestroy(&forest->cellSF);CHKERRQ(ierr);
71db4d5e8cSToby Isaac   if (forest->adaptCopyMode == PETSC_OWN_POINTER) {
72db4d5e8cSToby Isaac     ierr = PetscFree(forest->adaptMarkers);CHKERRQ(ierr);
73db4d5e8cSToby Isaac   }
74db4d5e8cSToby Isaac   if (forest->cellWeightsCopyMode == PETSC_OWN_POINTER) {
75db4d5e8cSToby Isaac     ierr = PetscFree(forest->cellWeights);CHKERRQ(ierr);
76db4d5e8cSToby Isaac   }
779a81d013SToby Isaac   ierr = PetscFree(forest->adaptStrategy);CHKERRQ(ierr);
7856ba9f64SToby Isaac   ierr = DMDestroy(&forest->base);CHKERRQ(ierr);
7930f902e7SToby Isaac   ierr = PetscFree(forest->topology);CHKERRQ(ierr);
8030f902e7SToby Isaac   ierr = PetscFree(forest);CHKERRQ(ierr);
81db4d5e8cSToby Isaac   PetscFunctionReturn(0);
82db4d5e8cSToby Isaac }
83db4d5e8cSToby Isaac 
84db4d5e8cSToby Isaac #undef __FUNCT__
85dd8e54a2SToby Isaac #define __FUNCT__ "DMForestSetTopology"
86dd8e54a2SToby Isaac PetscErrorCode DMForestSetTopology(DM dm, DMForestTopology topology)
87db4d5e8cSToby Isaac {
88db4d5e8cSToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
89db4d5e8cSToby Isaac   PetscErrorCode ierr;
90db4d5e8cSToby Isaac 
91db4d5e8cSToby Isaac   PetscFunctionBegin;
92db4d5e8cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
93ef51cf95SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the topology after setup");
94dd8e54a2SToby Isaac   ierr = PetscFree(forest->topology);CHKERRQ(ierr);
95dd8e54a2SToby Isaac   ierr = PetscStrallocpy((const char *)topology,(char **) &forest->topology);CHKERRQ(ierr);
96db4d5e8cSToby Isaac   PetscFunctionReturn(0);
97db4d5e8cSToby Isaac }
98db4d5e8cSToby Isaac 
99db4d5e8cSToby Isaac #undef __FUNCT__
100dd8e54a2SToby Isaac #define __FUNCT__ "DMForestGetTopology"
101dd8e54a2SToby Isaac PetscErrorCode DMForestGetTopology(DM dm, DMForestTopology *topology)
102dd8e54a2SToby Isaac {
103dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
104dd8e54a2SToby Isaac 
105dd8e54a2SToby Isaac   PetscFunctionBegin;
106dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
107dd8e54a2SToby Isaac   PetscValidPointer(topology,2);
108dd8e54a2SToby Isaac   *topology = forest->topology;
109dd8e54a2SToby Isaac   PetscFunctionReturn(0);
110dd8e54a2SToby Isaac }
111dd8e54a2SToby Isaac 
112dd8e54a2SToby Isaac #undef __FUNCT__
113dd8e54a2SToby Isaac #define __FUNCT__ "DMForestSetBaseDM"
114dd8e54a2SToby Isaac PetscErrorCode DMForestSetBaseDM(DM dm, DM base)
115dd8e54a2SToby Isaac {
116dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
117dd8e54a2SToby Isaac   PetscInt       dim, dimEmbed;
118dd8e54a2SToby Isaac   PetscErrorCode ierr;
119dd8e54a2SToby Isaac 
120dd8e54a2SToby Isaac   PetscFunctionBegin;
121dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
122ef51cf95SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the base after setup");
123dd8e54a2SToby Isaac   ierr = PetscObjectReference((PetscObject)base);CHKERRQ(ierr);
124dd8e54a2SToby Isaac   ierr = DMDestroy(&forest->base);CHKERRQ(ierr);
125dd8e54a2SToby Isaac   forest->base = base;
126a0452a8eSToby Isaac   if (base) {
127a0452a8eSToby Isaac     PetscValidHeaderSpecific(base, DM_CLASSID, 2);
128dd8e54a2SToby Isaac     ierr = DMGetDimension(base,&dim);CHKERRQ(ierr);
129dd8e54a2SToby Isaac     ierr = DMSetDimension(dm,dim);CHKERRQ(ierr);
130dd8e54a2SToby Isaac     ierr = DMGetCoordinateDim(base,&dimEmbed);CHKERRQ(ierr);
131dd8e54a2SToby Isaac     ierr = DMSetCoordinateDim(dm,dimEmbed);CHKERRQ(ierr);
132a0452a8eSToby Isaac   }
133dd8e54a2SToby Isaac   PetscFunctionReturn(0);
134dd8e54a2SToby Isaac }
135dd8e54a2SToby Isaac 
136dd8e54a2SToby Isaac #undef __FUNCT__
137dd8e54a2SToby Isaac #define __FUNCT__ "DMForestGetBaseDM"
138dd8e54a2SToby Isaac PetscErrorCode DMForestGetBaseDM(DM dm, DM *base)
139dd8e54a2SToby Isaac {
140dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
141dd8e54a2SToby Isaac 
142dd8e54a2SToby Isaac   PetscFunctionBegin;
143dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
144dd8e54a2SToby Isaac   PetscValidPointer(base, 2);
145dd8e54a2SToby Isaac   *base = forest->base;
146dd8e54a2SToby Isaac   PetscFunctionReturn(0);
147dd8e54a2SToby Isaac }
148dd8e54a2SToby Isaac 
149dd8e54a2SToby Isaac #undef __FUNCT__
150dd8e54a2SToby Isaac #define __FUNCT__ "DMForestSetCoarseForest"
151dd8e54a2SToby Isaac PetscErrorCode DMForestSetCoarseForest(DM dm,DM coarse)
152dd8e54a2SToby Isaac {
153dd8e54a2SToby Isaac   PetscErrorCode   ierr;
154dd8e54a2SToby Isaac 
155dd8e54a2SToby Isaac   PetscFunctionBegin;
156dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
157ef51cf95SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the coarse forest after setup");
15856ba9f64SToby Isaac   ierr = DMSetCoarseDM(dm,coarse);CHKERRQ(ierr);
159a0452a8eSToby Isaac   if (coarse) {
160a0452a8eSToby Isaac     PetscValidHeaderSpecific(coarse, DM_CLASSID, 2);
161a0452a8eSToby Isaac     ierr = DMForestTemplate(coarse,dm);CHKERRQ(ierr);
162a0452a8eSToby Isaac   }
163dd8e54a2SToby Isaac   PetscFunctionReturn(0);
164dd8e54a2SToby Isaac }
165dd8e54a2SToby Isaac 
166dd8e54a2SToby Isaac #undef __FUNCT__
167dd8e54a2SToby Isaac #define __FUNCT__ "DMForestGetCoarseForest"
168dd8e54a2SToby Isaac PetscErrorCode DMForestGetCoarseForest(DM dm, DM *coarse)
169dd8e54a2SToby Isaac {
17056ba9f64SToby Isaac   PetscErrorCode ierr;
171dd8e54a2SToby Isaac 
172dd8e54a2SToby Isaac   PetscFunctionBegin;
17356ba9f64SToby Isaac   ierr = DMGetCoarseDM(dm,coarse);CHKERRQ(ierr);
174dd8e54a2SToby Isaac   PetscFunctionReturn(0);
175dd8e54a2SToby Isaac }
176dd8e54a2SToby Isaac 
177dd8e54a2SToby Isaac #undef __FUNCT__
178dd8e54a2SToby Isaac #define __FUNCT__ "DMForestSetFineForest"
179dd8e54a2SToby Isaac PetscErrorCode DMForestSetFineForest(DM dm,DM fine)
180dd8e54a2SToby Isaac {
181dd8e54a2SToby Isaac   PetscErrorCode   ierr;
182dd8e54a2SToby Isaac 
183dd8e54a2SToby Isaac   PetscFunctionBegin;
184dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
185ef51cf95SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the fine forest after setup");
18688bdff64SToby Isaac   ierr = DMSetFineDM(dm,fine);CHKERRQ(ierr);
187a0452a8eSToby Isaac   if (fine) {
188a0452a8eSToby Isaac     PetscValidHeaderSpecific(fine, DM_CLASSID, 2);
189a0452a8eSToby Isaac     ierr = DMForestTemplate(fine,dm);CHKERRQ(ierr);
190a0452a8eSToby Isaac   }
191dd8e54a2SToby Isaac   PetscFunctionReturn(0);
192dd8e54a2SToby Isaac }
193dd8e54a2SToby Isaac 
194dd8e54a2SToby Isaac #undef __FUNCT__
195ef51cf95SToby Isaac #define __FUNCT__ "DMForestGetFineForest"
196dd8e54a2SToby Isaac PetscErrorCode DMForestGetFineForest(DM dm, DM *fine)
197dd8e54a2SToby Isaac {
19888bdff64SToby Isaac   PetscErrorCode ierr;
199dd8e54a2SToby Isaac 
200dd8e54a2SToby Isaac   PetscFunctionBegin;
20188bdff64SToby Isaac   ierr = DMGetFineDM(dm,fine);CHKERRQ(ierr);
202dd8e54a2SToby Isaac   PetscFunctionReturn(0);
203dd8e54a2SToby Isaac }
204dd8e54a2SToby Isaac 
205dd8e54a2SToby Isaac #undef __FUNCT__
206dd8e54a2SToby Isaac #define __FUNCT__ "DMForestSetAdjacencyDimension"
207dd8e54a2SToby Isaac PetscErrorCode DMForestSetAdjacencyDimension(DM dm, PetscInt adjDim)
208dd8e54a2SToby Isaac {
209dd8e54a2SToby Isaac   PetscInt        dim;
210dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
211dd8e54a2SToby Isaac   PetscErrorCode  ierr;
212dd8e54a2SToby Isaac 
213dd8e54a2SToby Isaac   PetscFunctionBegin;
214dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
215ef51cf95SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the adjacency dimension after setup");
216dd8e54a2SToby Isaac   if (adjDim < 0) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"adjacency dim cannot be < 0: %d", adjDim);
217dd8e54a2SToby Isaac   ierr = DMGetDimension(dm,&dim);CHKERRQ(ierr);
218dd8e54a2SToby Isaac   if (adjDim > dim) SETERRQ2(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"adjacency dim cannot be > %d: %d", dim, adjDim);
219dd8e54a2SToby Isaac   forest->adjDim = adjDim;
220dd8e54a2SToby Isaac   PetscFunctionReturn(0);
221dd8e54a2SToby Isaac }
222dd8e54a2SToby Isaac 
223dd8e54a2SToby Isaac #undef __FUNCT__
224dd8e54a2SToby Isaac #define __FUNCT__ "DMForestSetAdjacencyCodimension"
225dd8e54a2SToby Isaac PetscErrorCode DMForestSetAdjacencyCodimension(DM dm, PetscInt adjCodim)
226dd8e54a2SToby Isaac {
227dd8e54a2SToby Isaac   PetscInt        dim;
228dd8e54a2SToby Isaac   PetscErrorCode  ierr;
229dd8e54a2SToby Isaac 
230dd8e54a2SToby Isaac   PetscFunctionBegin;
231dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
232dd8e54a2SToby Isaac   ierr = DMGetDimension(dm,&dim);CHKERRQ(ierr);
233dd8e54a2SToby Isaac   ierr = DMForestSetAdjacencyDimension(dm,dim-adjCodim);CHKERRQ(ierr);
234dd8e54a2SToby Isaac   PetscFunctionReturn(0);
235dd8e54a2SToby Isaac }
236dd8e54a2SToby Isaac 
237dd8e54a2SToby Isaac #undef __FUNCT__
238dd8e54a2SToby Isaac #define __FUNCT__ "DMForestGetAdjacencyDimension"
239dd8e54a2SToby Isaac PetscErrorCode DMForestGetAdjacencyDimension(DM dm, PetscInt *adjDim)
240dd8e54a2SToby Isaac {
241dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
242dd8e54a2SToby Isaac 
243dd8e54a2SToby Isaac   PetscFunctionBegin;
244dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
245dd8e54a2SToby Isaac   PetscValidIntPointer(adjDim,2);
246dd8e54a2SToby Isaac   *adjDim = forest->adjDim;
247dd8e54a2SToby Isaac   PetscFunctionReturn(0);
248dd8e54a2SToby Isaac }
249dd8e54a2SToby Isaac 
250dd8e54a2SToby Isaac #undef __FUNCT__
251dd8e54a2SToby Isaac #define __FUNCT__ "DMForestGetAdjacencyCodimension"
252dd8e54a2SToby Isaac PetscErrorCode DMForestGetAdjacencyCodimension(DM dm, PetscInt *adjCodim)
253dd8e54a2SToby Isaac {
254dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
255dd8e54a2SToby Isaac   PetscInt       dim;
256dd8e54a2SToby Isaac   PetscErrorCode ierr;
257dd8e54a2SToby Isaac 
258dd8e54a2SToby Isaac   PetscFunctionBegin;
259dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
260dd8e54a2SToby Isaac   PetscValidIntPointer(adjCodim,2);
261dd8e54a2SToby Isaac   ierr = DMGetDimension(dm,&dim);CHKERRQ(ierr);
262dd8e54a2SToby Isaac   *adjCodim = dim - forest->adjDim;
263dd8e54a2SToby Isaac   PetscFunctionReturn(0);
264dd8e54a2SToby Isaac }
265dd8e54a2SToby Isaac 
266dd8e54a2SToby Isaac #undef __FUNCT__
267ef51cf95SToby Isaac #define __FUNCT__ "DMForestSetPartitionOverlap"
268dd8e54a2SToby Isaac PetscErrorCode DMForestSetPartitionOverlap(DM dm, PetscInt overlap)
269dd8e54a2SToby Isaac {
270dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
271dd8e54a2SToby Isaac 
272dd8e54a2SToby Isaac   PetscFunctionBegin;
273dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
274ef51cf95SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the overlap after setup");
275dd8e54a2SToby Isaac   if (overlap < 0) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"overlap cannot be < 0: %d", overlap);
276dd8e54a2SToby Isaac   forest->overlap = overlap;
277dd8e54a2SToby Isaac   PetscFunctionReturn(0);
278dd8e54a2SToby Isaac }
279dd8e54a2SToby Isaac 
280dd8e54a2SToby Isaac #undef __FUNCT__
281dd8e54a2SToby Isaac #define __FUNCT__ "DMForestGetPartitionOverlap"
282dd8e54a2SToby Isaac PetscErrorCode DMForestGetPartitionOverlap(DM dm, PetscInt *overlap)
283dd8e54a2SToby Isaac {
284dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
285dd8e54a2SToby Isaac 
286dd8e54a2SToby Isaac   PetscFunctionBegin;
287dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
288dd8e54a2SToby Isaac   PetscValidIntPointer(overlap,2);
289dd8e54a2SToby Isaac   *overlap = forest->overlap;
290dd8e54a2SToby Isaac   PetscFunctionReturn(0);
291dd8e54a2SToby Isaac }
292dd8e54a2SToby Isaac 
293dd8e54a2SToby Isaac #undef __FUNCT__
294dd8e54a2SToby Isaac #define __FUNCT__ "DMForestSetMinimumRefinement"
295dd8e54a2SToby Isaac PetscErrorCode DMForestSetMinimumRefinement(DM dm, PetscInt minRefinement)
296dd8e54a2SToby Isaac {
297dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
298dd8e54a2SToby Isaac 
299dd8e54a2SToby Isaac   PetscFunctionBegin;
300dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
301ef51cf95SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the minimum refinement after setup");
302dd8e54a2SToby Isaac   forest->minRefinement = minRefinement;
303dd8e54a2SToby Isaac   PetscFunctionReturn(0);
304dd8e54a2SToby Isaac }
305dd8e54a2SToby Isaac 
306dd8e54a2SToby Isaac #undef __FUNCT__
307dd8e54a2SToby Isaac #define __FUNCT__ "DMForestGetMinimumRefinement"
308dd8e54a2SToby Isaac PetscErrorCode DMForestGetMinimumRefinement(DM dm, PetscInt *minRefinement)
309dd8e54a2SToby Isaac {
310dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
311dd8e54a2SToby Isaac 
312dd8e54a2SToby Isaac   PetscFunctionBegin;
313dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
314dd8e54a2SToby Isaac   PetscValidIntPointer(minRefinement,2);
315dd8e54a2SToby Isaac   *minRefinement = forest->minRefinement;
316dd8e54a2SToby Isaac   PetscFunctionReturn(0);
317dd8e54a2SToby Isaac }
318dd8e54a2SToby Isaac 
319dd8e54a2SToby Isaac #undef __FUNCT__
32056ba9f64SToby Isaac #define __FUNCT__ "DMForestSetInitialRefinement"
32156ba9f64SToby Isaac PetscErrorCode DMForestSetInitialRefinement(DM dm, PetscInt initRefinement)
32256ba9f64SToby Isaac {
32356ba9f64SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
32456ba9f64SToby Isaac 
32556ba9f64SToby Isaac   PetscFunctionBegin;
32656ba9f64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32756ba9f64SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the initial refinement after setup");
32856ba9f64SToby Isaac   forest->initRefinement = initRefinement;
32956ba9f64SToby Isaac   PetscFunctionReturn(0);
33056ba9f64SToby Isaac }
33156ba9f64SToby Isaac 
33256ba9f64SToby Isaac #undef __FUNCT__
33356ba9f64SToby Isaac #define __FUNCT__ "DMForestGetInitialRefinement"
33456ba9f64SToby Isaac PetscErrorCode DMForestGetInitialRefinement(DM dm, PetscInt *initRefinement)
33556ba9f64SToby Isaac {
33656ba9f64SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
33756ba9f64SToby Isaac 
33856ba9f64SToby Isaac   PetscFunctionBegin;
33956ba9f64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
34056ba9f64SToby Isaac   PetscValidIntPointer(initRefinement,2);
34156ba9f64SToby Isaac   *initRefinement = forest->initRefinement;
34256ba9f64SToby Isaac   PetscFunctionReturn(0);
34356ba9f64SToby Isaac }
34456ba9f64SToby Isaac 
34556ba9f64SToby Isaac #undef __FUNCT__
346c7eeac06SToby Isaac #define __FUNCT__ "DMForestSetMaximumRefinement"
347c7eeac06SToby Isaac PetscErrorCode DMForestSetMaximumRefinement(DM dm, PetscInt maxRefinement)
348dd8e54a2SToby Isaac {
349dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
350dd8e54a2SToby Isaac 
351dd8e54a2SToby Isaac   PetscFunctionBegin;
352dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
353ef51cf95SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the maximum refinement after setup");
354c7eeac06SToby Isaac   forest->maxRefinement = maxRefinement;
355dd8e54a2SToby Isaac   PetscFunctionReturn(0);
356dd8e54a2SToby Isaac }
357dd8e54a2SToby Isaac 
358dd8e54a2SToby Isaac #undef __FUNCT__
359c7eeac06SToby Isaac #define __FUNCT__ "DMForestGetMaximumRefinement"
360c7eeac06SToby Isaac PetscErrorCode DMForestGetMaximumRefinement(DM dm, PetscInt *maxRefinement)
361dd8e54a2SToby Isaac {
362dd8e54a2SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
363dd8e54a2SToby Isaac 
364dd8e54a2SToby Isaac   PetscFunctionBegin;
365dd8e54a2SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
366c7eeac06SToby Isaac   PetscValidIntPointer(maxRefinement,2);
367c7eeac06SToby Isaac   *maxRefinement = forest->maxRefinement;
368dd8e54a2SToby Isaac   PetscFunctionReturn(0);
369dd8e54a2SToby Isaac }
370c7eeac06SToby Isaac 
371c7eeac06SToby Isaac #undef __FUNCT__
372c7eeac06SToby Isaac #define __FUNCT__ "DMForestSetAdaptivityStrategy"
373c7eeac06SToby Isaac PetscErrorCode DMForestSetAdaptivityStrategy(DM dm, DMForestAdaptivityStrategy adaptStrategy)
374c7eeac06SToby Isaac {
375c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
376c7eeac06SToby Isaac   PetscErrorCode ierr;
377c7eeac06SToby Isaac 
378c7eeac06SToby Isaac   PetscFunctionBegin;
379c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
380c7eeac06SToby Isaac   ierr = PetscFree(forest->adaptStrategy);CHKERRQ(ierr);
381a73e2921SToby Isaac   ierr = PetscStrallocpy((const char *) adaptStrategy,(char **)&forest->adaptStrategy);CHKERRQ(ierr);
382c7eeac06SToby Isaac   PetscFunctionReturn(0);
383c7eeac06SToby Isaac }
384c7eeac06SToby Isaac 
385c7eeac06SToby Isaac #undef __FUNCT__
386c7eeac06SToby Isaac #define __FUNCT__ "DMForestGetAdaptivityStrategy"
387c7eeac06SToby Isaac PetscErrorCode DMForestGetAdaptivityStrategy(DM dm, DMForestAdaptivityStrategy *adaptStrategy)
388c7eeac06SToby Isaac {
389c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
390c7eeac06SToby Isaac 
391c7eeac06SToby Isaac   PetscFunctionBegin;
392c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
393c7eeac06SToby Isaac   PetscValidPointer(adaptStrategy,2);
394c7eeac06SToby Isaac   *adaptStrategy = forest->adaptStrategy;
395c7eeac06SToby Isaac   PetscFunctionReturn(0);
396c7eeac06SToby Isaac }
397c7eeac06SToby Isaac 
398c7eeac06SToby Isaac #undef __FUNCT__
399c7eeac06SToby Isaac #define __FUNCT__ "DMForestSetGradeFactor"
400c7eeac06SToby Isaac PetscErrorCode DMForestSetGradeFactor(DM dm, PetscInt grade)
401c7eeac06SToby Isaac {
402c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
403c7eeac06SToby Isaac 
404c7eeac06SToby Isaac   PetscFunctionBegin;
405c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
406ef51cf95SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the grade factor after setup");
407c7eeac06SToby Isaac   forest->gradeFactor = grade;
408c7eeac06SToby Isaac   PetscFunctionReturn(0);
409c7eeac06SToby Isaac }
410c7eeac06SToby Isaac 
411c7eeac06SToby Isaac #undef __FUNCT__
412c7eeac06SToby Isaac #define __FUNCT__ "DMForestGetGradeFactor"
413c7eeac06SToby Isaac PetscErrorCode DMForestGetGradeFactor(DM dm, PetscInt *grade)
414c7eeac06SToby Isaac {
415c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
416c7eeac06SToby Isaac 
417c7eeac06SToby Isaac   PetscFunctionBegin;
418c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
419c7eeac06SToby Isaac   PetscValidIntPointer(grade,2);
420c7eeac06SToby Isaac   *grade = forest->gradeFactor;
421c7eeac06SToby Isaac   PetscFunctionReturn(0);
422c7eeac06SToby Isaac }
423c7eeac06SToby Isaac 
424c7eeac06SToby Isaac #undef __FUNCT__
425ef51cf95SToby Isaac #define __FUNCT__ "DMForestSetCellWeightFactor"
426ef51cf95SToby Isaac PetscErrorCode DMForestSetCellWeightFactor(DM dm, PetscReal weightsFactor)
427c7eeac06SToby Isaac {
428c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
429c7eeac06SToby Isaac 
430c7eeac06SToby Isaac   PetscFunctionBegin;
431c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
432ef51cf95SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the weights factor after setup");
433c7eeac06SToby Isaac   forest->weightsFactor = weightsFactor;
434c7eeac06SToby Isaac   PetscFunctionReturn(0);
435c7eeac06SToby Isaac }
436c7eeac06SToby Isaac 
437c7eeac06SToby Isaac #undef __FUNCT__
438ef51cf95SToby Isaac #define __FUNCT__ "DMForestGetCellWeightFactor"
439ef51cf95SToby Isaac PetscErrorCode DMForestGetCellWeightFactor(DM dm, PetscReal *weightsFactor)
440c7eeac06SToby Isaac {
441c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
442c7eeac06SToby Isaac 
443c7eeac06SToby Isaac   PetscFunctionBegin;
444c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
445c7eeac06SToby Isaac   PetscValidRealPointer(weightsFactor,2);
446c7eeac06SToby Isaac   *weightsFactor = forest->weightsFactor;
447c7eeac06SToby Isaac   PetscFunctionReturn(0);
448c7eeac06SToby Isaac }
449c7eeac06SToby Isaac 
450c7eeac06SToby Isaac #undef __FUNCT__
451c7eeac06SToby Isaac #define __FUNCT__ "DMForestGetCellChart"
452c7eeac06SToby Isaac PetscErrorCode DMForestGetCellChart(DM dm, PetscInt *cStart, PetscInt *cEnd)
453c7eeac06SToby Isaac {
454c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
455c7eeac06SToby Isaac   PetscErrorCode ierr;
456c7eeac06SToby Isaac 
457c7eeac06SToby Isaac   PetscFunctionBegin;
458c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
459c7eeac06SToby Isaac   PetscValidIntPointer(cStart,2);
460c7eeac06SToby Isaac   PetscValidIntPointer(cEnd,2);
461c7eeac06SToby Isaac   if (((forest->cStart == PETSC_DETERMINE) || (forest->cEnd == PETSC_DETERMINE)) && forest->createcellchart) {
462c7eeac06SToby Isaac     ierr = forest->createcellchart(dm,&forest->cStart,&forest->cEnd);CHKERRQ(ierr);
463c7eeac06SToby Isaac   }
464c7eeac06SToby Isaac   *cStart =  forest->cStart;
465c7eeac06SToby Isaac   *cEnd   =  forest->cEnd;
466c7eeac06SToby Isaac   PetscFunctionReturn(0);
467c7eeac06SToby Isaac }
468c7eeac06SToby Isaac 
469c7eeac06SToby Isaac #undef __FUNCT__
470c7eeac06SToby Isaac #define __FUNCT__ "DMForestGetCellSF"
471c7eeac06SToby Isaac PetscErrorCode DMForestGetCellSF(DM dm, PetscSF *cellSF)
472c7eeac06SToby Isaac {
473c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
474c7eeac06SToby Isaac   PetscErrorCode ierr;
475c7eeac06SToby Isaac 
476c7eeac06SToby Isaac   PetscFunctionBegin;
477c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
478c7eeac06SToby Isaac   PetscValidPointer(cellSF,2);
479c7eeac06SToby Isaac   if ((!forest->cellSF) && forest->createcellsf) {
480c7eeac06SToby Isaac     ierr = forest->createcellsf(dm,&forest->cellSF);CHKERRQ(ierr);
481c7eeac06SToby Isaac   }
482c7eeac06SToby Isaac   *cellSF = forest->cellSF;
483c7eeac06SToby Isaac   PetscFunctionReturn(0);
484c7eeac06SToby Isaac }
485c7eeac06SToby Isaac 
486c7eeac06SToby Isaac #undef __FUNCT__
487c7eeac06SToby Isaac #define __FUNCT__ "DMForestSetAdaptivityMarkers"
488c7eeac06SToby Isaac PetscErrorCode DMForestSetAdaptivityMarkers(DM dm, PetscInt markers[], PetscCopyMode copyMode)
489c7eeac06SToby Isaac {
490c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
491c7eeac06SToby Isaac   PetscInt       cStart, cEnd;
492c7eeac06SToby Isaac   PetscErrorCode ierr;
493c7eeac06SToby Isaac 
494c7eeac06SToby Isaac   PetscFunctionBegin;
495c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
496c7eeac06SToby Isaac   ierr = DMForestGetCellChart(dm,&cStart,&cEnd);CHKERRQ(ierr);
497c7eeac06SToby Isaac   if (cEnd < cStart) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"cell chart [%d,%d) is not valid",cStart,cEnd);
498c7eeac06SToby Isaac   if (copyMode == PETSC_COPY_VALUES) {
499c7eeac06SToby Isaac     if (forest->adaptCopyMode != PETSC_OWN_POINTER || forest->adaptMarkers == markers) {
500c7eeac06SToby Isaac       ierr = PetscMalloc1(cEnd-cStart,&forest->adaptMarkers);CHKERRQ(ierr);
501c7eeac06SToby Isaac     }
502c7eeac06SToby Isaac     ierr = PetscMemcpy(forest->adaptMarkers,markers,(cEnd-cStart)*sizeof(*markers));CHKERRQ(ierr);
503c7eeac06SToby Isaac     forest->adaptCopyMode = PETSC_OWN_POINTER;
504c7eeac06SToby Isaac     PetscFunctionReturn(0);
505c7eeac06SToby Isaac   }
506c7eeac06SToby Isaac   if (forest->adaptCopyMode == PETSC_OWN_POINTER) {
507c7eeac06SToby Isaac     ierr = PetscFree(forest->adaptMarkers);CHKERRQ(ierr);
508c7eeac06SToby Isaac   }
509c7eeac06SToby Isaac   forest->adaptMarkers  = markers;
510c7eeac06SToby Isaac   forest->adaptCopyMode = copyMode;
511c7eeac06SToby Isaac   PetscFunctionReturn(0);
512c7eeac06SToby Isaac }
513c7eeac06SToby Isaac 
514c7eeac06SToby Isaac #undef __FUNCT__
515c7eeac06SToby Isaac #define __FUNCT__ "DMForestGetAdaptivityMarkers"
516c7eeac06SToby Isaac PetscErrorCode DMForestGetAdaptivityMarkers(DM dm, PetscInt **markers)
517c7eeac06SToby Isaac {
518c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
519c7eeac06SToby Isaac 
520c7eeac06SToby Isaac   PetscFunctionBegin;
521c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
522c7eeac06SToby Isaac   PetscValidPointer(markers,2);
523c7eeac06SToby Isaac   *markers = forest->adaptMarkers;
524c7eeac06SToby Isaac   PetscFunctionReturn(0);
525c7eeac06SToby Isaac }
526c7eeac06SToby Isaac 
527c7eeac06SToby Isaac #undef __FUNCT__
528c7eeac06SToby Isaac #define __FUNCT__ "DMForestSetCellWeights"
529c7eeac06SToby Isaac PetscErrorCode DMForestSetCellWeights(DM dm, PetscReal weights[], PetscCopyMode copyMode)
530c7eeac06SToby Isaac {
531c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
532c7eeac06SToby Isaac   PetscInt       cStart, cEnd;
533c7eeac06SToby Isaac   PetscErrorCode ierr;
534c7eeac06SToby Isaac 
535c7eeac06SToby Isaac   PetscFunctionBegin;
536c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
537c7eeac06SToby Isaac   ierr = DMForestGetCellChart(dm,&cStart,&cEnd);CHKERRQ(ierr);
538c7eeac06SToby Isaac   if (cEnd < cStart) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"cell chart [%d,%d) is not valid",cStart,cEnd);
539c7eeac06SToby Isaac   if (copyMode == PETSC_COPY_VALUES) {
540c7eeac06SToby Isaac     if (forest->cellWeightsCopyMode != PETSC_OWN_POINTER || forest->cellWeights == weights) {
541c7eeac06SToby Isaac       ierr = PetscMalloc1(cEnd-cStart,&forest->cellWeights);CHKERRQ(ierr);
542c7eeac06SToby Isaac     }
543c7eeac06SToby Isaac     ierr = PetscMemcpy(forest->cellWeights,weights,(cEnd-cStart)*sizeof(*weights));CHKERRQ(ierr);
544c7eeac06SToby Isaac     forest->cellWeightsCopyMode = PETSC_OWN_POINTER;
545c7eeac06SToby Isaac     PetscFunctionReturn(0);
546c7eeac06SToby Isaac   }
547c7eeac06SToby Isaac   if (forest->cellWeightsCopyMode == PETSC_OWN_POINTER) {
548c7eeac06SToby Isaac     ierr = PetscFree(forest->cellWeights);CHKERRQ(ierr);
549c7eeac06SToby Isaac   }
550c7eeac06SToby Isaac   forest->cellWeights  = weights;
551c7eeac06SToby Isaac   forest->cellWeightsCopyMode = copyMode;
552c7eeac06SToby Isaac   PetscFunctionReturn(0);
553c7eeac06SToby Isaac }
554c7eeac06SToby Isaac 
555c7eeac06SToby Isaac #undef __FUNCT__
556c7eeac06SToby Isaac #define __FUNCT__ "DMForestGetCellWeights"
557c7eeac06SToby Isaac PetscErrorCode DMForestGetCellWeights(DM dm, PetscReal **weights)
558c7eeac06SToby Isaac {
559c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
560c7eeac06SToby Isaac 
561c7eeac06SToby Isaac   PetscFunctionBegin;
562c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
563c7eeac06SToby Isaac   PetscValidPointer(weights,2);
564c7eeac06SToby Isaac   *weights = forest->cellWeights;
565c7eeac06SToby Isaac   PetscFunctionReturn(0);
566c7eeac06SToby Isaac }
567c7eeac06SToby Isaac 
568c7eeac06SToby Isaac #undef __FUNCT__
569c7eeac06SToby Isaac #define __FUNCT__ "DMForestSetWeightCapacity"
570c7eeac06SToby Isaac PetscErrorCode DMForestSetWeightCapacity(DM dm, PetscReal capacity)
571c7eeac06SToby Isaac {
572c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
573c7eeac06SToby Isaac 
574c7eeac06SToby Isaac   PetscFunctionBegin;
575c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
576ef51cf95SToby Isaac   if (dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE,"Cannot change the weight capacity after setup");
577c7eeac06SToby Isaac   if (capacity < 0.) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"Cannot have negative weight capacity; %f",capacity);
578c7eeac06SToby Isaac   forest->weightCapacity = capacity;
579c7eeac06SToby Isaac   PetscFunctionReturn(0);
580c7eeac06SToby Isaac }
581c7eeac06SToby Isaac 
582c7eeac06SToby Isaac #undef __FUNCT__
583c7eeac06SToby Isaac #define __FUNCT__ "DMForestGetWeightCapacity"
584c7eeac06SToby Isaac PetscErrorCode DMForestGetWeightCapacity(DM dm, PetscReal *capacity)
585c7eeac06SToby Isaac {
586c7eeac06SToby Isaac   DM_Forest      *forest = (DM_Forest *) dm->data;
587c7eeac06SToby Isaac 
588c7eeac06SToby Isaac   PetscFunctionBegin;
589c7eeac06SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
590c7eeac06SToby Isaac   PetscValidRealPointer(capacity,2);
591c7eeac06SToby Isaac   *capacity = forest->weightCapacity;
592c7eeac06SToby Isaac   PetscFunctionReturn(0);
593c7eeac06SToby Isaac }
594c7eeac06SToby Isaac 
595dd8e54a2SToby Isaac #undef __FUNCT__
596db4d5e8cSToby Isaac #define __FUNCT__ "DMSetFromOptions_Forest"
5970709b2feSToby Isaac PETSC_EXTERN PetscErrorCode DMSetFromOptions_Forest(PetscOptionItems *PetscOptionsObject,DM dm)
598db4d5e8cSToby Isaac {
599db4d5e8cSToby Isaac   DM_Forest                  *forest = (DM_Forest *) dm->data;
60056ba9f64SToby Isaac   PetscBool                  flg, flg1, flg2, flg3, flg4;
601dd8e54a2SToby Isaac   DMForestTopology           oldTopo;
602c7eeac06SToby Isaac   char                       stringBuffer[256];
603dd8e54a2SToby Isaac   PetscViewer                viewer;
604dd8e54a2SToby Isaac   PetscViewerFormat          format;
60556ba9f64SToby Isaac   PetscInt                   adjDim, adjCodim, overlap, minRefinement, initRefinement, maxRefinement, grade;
606c7eeac06SToby Isaac   PetscReal                  weightsFactor;
607c7eeac06SToby Isaac   DMForestAdaptivityStrategy adaptStrategy;
608db4d5e8cSToby Isaac   PetscErrorCode             ierr;
609db4d5e8cSToby Isaac 
610db4d5e8cSToby Isaac   PetscFunctionBegin;
611db4d5e8cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
612db4d5e8cSToby Isaac   forest->setFromOptions = PETSC_TRUE;
613dd8e54a2SToby Isaac   ierr = DMForestGetTopology(dm, &oldTopo);CHKERRQ(ierr);
614a3eda1eaSToby Isaac   ierr = PetscOptionsHead(PetscOptionsObject,"DMForest Options");CHKERRQ(ierr);
61556ba9f64SToby Isaac   ierr = PetscOptionsString("-dm_forest_topology","the topology of the forest's base mesh","DMForestSetTopology",oldTopo,stringBuffer,256,&flg1);CHKERRQ(ierr);
61656ba9f64SToby Isaac   ierr = PetscOptionsViewer("-dm_forest_base_dm","load the base DM from a viewer specification","DMForestSetBaseDM",&viewer,&format,&flg2);CHKERRQ(ierr);
61756ba9f64SToby Isaac   ierr = PetscOptionsViewer("-dm_forest_coarse_forest","load the coarse forest from a viewer specification","DMForestSetCoarseForest",&viewer,&format,&flg3);CHKERRQ(ierr);
61856ba9f64SToby Isaac   ierr = PetscOptionsViewer("-dm_forest_fine_forest","load the fine forest from a viewer specification","DMForestSetFineForest",&viewer,&format,&flg4);CHKERRQ(ierr);
61956ba9f64SToby Isaac   if ((PetscInt) flg1 + (PetscInt) flg2 + (PetscInt) flg3 + (PetscInt) flg4 > 1) {
62056ba9f64SToby Isaac     SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_INCOMP,"Specify only one of -dm_forest_{topology,base_dm,coarse_forest,fine_forest}");
621dd8e54a2SToby Isaac   }
62256ba9f64SToby Isaac   if (flg1) {
62356ba9f64SToby Isaac     ierr = DMForestSetTopology(dm,(DMForestTopology)stringBuffer);CHKERRQ(ierr);
62456ba9f64SToby Isaac     ierr = DMForestSetBaseDM(dm,NULL);CHKERRQ(ierr);
62556ba9f64SToby Isaac     ierr = DMForestSetCoarseForest(dm,NULL);CHKERRQ(ierr);
62656ba9f64SToby Isaac     ierr = DMForestSetFineForest(dm,NULL);CHKERRQ(ierr);
62756ba9f64SToby Isaac   }
62856ba9f64SToby Isaac   if (flg2) {
629dd8e54a2SToby Isaac     DM         base;
630dd8e54a2SToby Isaac 
631dd8e54a2SToby Isaac     ierr = DMCreate(PetscObjectComm((PetscObject)dm),&base);CHKERRQ(ierr);
632dd8e54a2SToby Isaac     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
633dd8e54a2SToby Isaac     ierr = DMLoad(base,viewer);CHKERRQ(ierr);
634dd8e54a2SToby Isaac     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
635dd8e54a2SToby Isaac     ierr = DMForestSetBaseDM(dm,base);CHKERRQ(ierr);
636dd8e54a2SToby Isaac     ierr = DMDestroy(&base);CHKERRQ(ierr);
63756ba9f64SToby Isaac     ierr = DMForestSetTopology(dm,NULL);CHKERRQ(ierr);
63856ba9f64SToby Isaac     ierr = DMForestSetCoarseForest(dm,NULL);CHKERRQ(ierr);
63956ba9f64SToby Isaac     ierr = DMForestSetFineForest(dm,NULL);CHKERRQ(ierr);
640dd8e54a2SToby Isaac   }
64156ba9f64SToby Isaac   if (flg3) {
642dd8e54a2SToby Isaac     DM         coarse;
643dd8e54a2SToby Isaac 
644dd8e54a2SToby Isaac     ierr = DMCreate(PetscObjectComm((PetscObject)dm),&coarse);CHKERRQ(ierr);
645dd8e54a2SToby Isaac     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
646dd8e54a2SToby Isaac     ierr = DMLoad(coarse,viewer);CHKERRQ(ierr);
647dd8e54a2SToby Isaac     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
648dd8e54a2SToby Isaac     ierr = DMForestSetCoarseForest(dm,coarse);CHKERRQ(ierr);
649dd8e54a2SToby Isaac     ierr = DMDestroy(&coarse);CHKERRQ(ierr);
65056ba9f64SToby Isaac     ierr = DMForestSetTopology(dm,NULL);CHKERRQ(ierr);
65156ba9f64SToby Isaac     ierr = DMForestSetBaseDM(dm,NULL);CHKERRQ(ierr);
65256ba9f64SToby Isaac     ierr = DMForestSetFineForest(dm,NULL);CHKERRQ(ierr);
653dd8e54a2SToby Isaac   }
65456ba9f64SToby Isaac   if (flg4) {
655dd8e54a2SToby Isaac     DM         fine;
656dd8e54a2SToby Isaac 
657dd8e54a2SToby Isaac     ierr = DMCreate(PetscObjectComm((PetscObject)dm),&fine);CHKERRQ(ierr);
658dd8e54a2SToby Isaac     ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr);
659dd8e54a2SToby Isaac     ierr = DMLoad(fine,viewer);CHKERRQ(ierr);
660dd8e54a2SToby Isaac     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
661dd8e54a2SToby Isaac     ierr = DMForestSetFineForest(dm,fine);CHKERRQ(ierr);
662dd8e54a2SToby Isaac     ierr = DMDestroy(&fine);CHKERRQ(ierr);
66356ba9f64SToby Isaac     ierr = DMForestSetTopology(dm,NULL);CHKERRQ(ierr);
66456ba9f64SToby Isaac     ierr = DMForestSetBaseDM(dm,NULL);CHKERRQ(ierr);
66556ba9f64SToby Isaac     ierr = DMForestSetCoarseForest(dm,NULL);CHKERRQ(ierr);
666dd8e54a2SToby Isaac   }
667dd8e54a2SToby Isaac   ierr = DMForestGetAdjacencyDimension(dm,&adjDim);CHKERRQ(ierr);
668dd8e54a2SToby Isaac   ierr = PetscOptionsInt("-dm_forest_adjacency_dimension","set the dimension of points that define adjacency in the forest","DMForestSetAdjacencyDimension",adjDim,&adjDim,&flg);CHKERRQ(ierr);
669dd8e54a2SToby Isaac   if (flg) {
670dd8e54a2SToby Isaac     ierr = DMForestSetAdjacencyDimension(dm,adjDim);CHKERRQ(ierr);
671dd8e54a2SToby Isaac   }
672dd8e54a2SToby Isaac   else {
673dd8e54a2SToby Isaac     ierr = DMForestGetAdjacencyCodimension(dm,&adjCodim);CHKERRQ(ierr);
674dd8e54a2SToby Isaac     ierr = PetscOptionsInt("-dm_forest_adjacency_codimension","set the codimension of points that define adjacency in the forest","DMForestSetAdjacencyCodimension",adjCodim,&adjCodim,&flg);CHKERRQ(ierr);
675dd8e54a2SToby Isaac     if (flg) {
676dd8e54a2SToby Isaac       ierr = DMForestSetAdjacencyCodimension(dm,adjCodim);CHKERRQ(ierr);
677dd8e54a2SToby Isaac     }
678dd8e54a2SToby Isaac   }
679dd8e54a2SToby Isaac   ierr = DMForestGetPartitionOverlap(dm,&overlap);CHKERRQ(ierr);
680dd8e54a2SToby Isaac   ierr = PetscOptionsInt("-dm_forest_partition_overlap","set the degree of partition overlap","DMForestSetPartitionOverlap",overlap,&overlap,&flg);CHKERRQ(ierr);
681dd8e54a2SToby Isaac   if (flg) {
682dd8e54a2SToby Isaac     ierr = DMForestSetPartitionOverlap(dm,overlap);CHKERRQ(ierr);
683dd8e54a2SToby Isaac   }
684dd8e54a2SToby Isaac   ierr = DMForestGetMinimumRefinement(dm,&minRefinement);CHKERRQ(ierr);
685dd8e54a2SToby Isaac   ierr = PetscOptionsInt("-dm_forest_minimum_refinement","set the minimum level of refinement in the forest","DMForestSetMinimumRefinement",minRefinement,&minRefinement,&flg);CHKERRQ(ierr);
686dd8e54a2SToby Isaac   if (flg) {
687dd8e54a2SToby Isaac     ierr = DMForestSetMinimumRefinement(dm,minRefinement);CHKERRQ(ierr);
688db4d5e8cSToby Isaac   }
68956ba9f64SToby Isaac   ierr = DMForestGetInitialRefinement(dm,&initRefinement);CHKERRQ(ierr);
69056ba9f64SToby Isaac   ierr = PetscOptionsInt("-dm_forest_initial_refinement","set the initial level of refinement in the forest","DMForestSetInitialRefinement",initRefinement,&initRefinement,&flg);CHKERRQ(ierr);
69156ba9f64SToby Isaac   if (flg) {
69256ba9f64SToby Isaac     ierr = DMForestSetInitialRefinement(dm,initRefinement);CHKERRQ(ierr);
69356ba9f64SToby Isaac   }
694c7eeac06SToby Isaac   ierr = DMForestGetMaximumRefinement(dm,&maxRefinement);CHKERRQ(ierr);
695c7eeac06SToby Isaac   ierr = PetscOptionsInt("-dm_forest_maximum_refinement","set the maximum level of refinement in the forest","DMForestSetMaximumRefinement",maxRefinement,&maxRefinement,&flg);CHKERRQ(ierr);
696c7eeac06SToby Isaac   if (flg) {
697c7eeac06SToby Isaac     ierr = DMForestSetMaximumRefinement(dm,maxRefinement);CHKERRQ(ierr);
698c7eeac06SToby Isaac   }
699c7eeac06SToby Isaac   ierr = DMForestGetAdaptivityStrategy(dm,&adaptStrategy);CHKERRQ(ierr);
700c7eeac06SToby Isaac   ierr = PetscOptionsString("-dm_forest_adaptivity_strategy","the forest's adaptivity-flag resolution strategy","DMForestSetAdaptivityStrategy",adaptStrategy,stringBuffer,256,&flg);CHKERRQ(ierr);
701c7eeac06SToby Isaac   if (flg) {
702c7eeac06SToby Isaac     ierr = DMForestSetAdaptivityStrategy(dm,(DMForestAdaptivityStrategy)stringBuffer);CHKERRQ(ierr);
703c7eeac06SToby Isaac   }
704c7eeac06SToby Isaac   ierr = DMForestGetGradeFactor(dm,&grade);CHKERRQ(ierr);
705c7eeac06SToby Isaac   ierr = PetscOptionsInt("-dm_forest_grade_factor","grade factor between neighboring cells","DMForestSetGradeFactor",grade,&grade,&flg);CHKERRQ(ierr);
706c7eeac06SToby Isaac   if (flg) {
707c7eeac06SToby Isaac     ierr = DMForestSetGradeFactor(dm,grade);CHKERRQ(ierr);
708c7eeac06SToby Isaac   }
709c7eeac06SToby Isaac   ierr = DMForestGetCellWeightFactor(dm,&weightsFactor);CHKERRQ(ierr);
710c7eeac06SToby Isaac   ierr = PetscOptionsReal("-dm_forest_cell_weight_factor","multiplying weight factor for cell refinement","DMForestSetCellWeightFactor",weightsFactor,&weightsFactor,&flg);CHKERRQ(ierr);
711c7eeac06SToby Isaac   if (flg) {
712c7eeac06SToby Isaac     ierr = DMForestSetCellWeightFactor(dm,weightsFactor);CHKERRQ(ierr);
713c7eeac06SToby Isaac   }
7146b847ec6SToby Isaac   ierr = PetscOptionsInt("-dm_refine","equivalent to -dm_forest_set_minimum_refinement and -dm_forest_set_initial_refinement with the same value",NULL,minRefinement,&minRefinement,&flg);CHKERRQ(ierr);
7156b847ec6SToby Isaac   if (flg) {
7166b847ec6SToby Isaac     ierr = DMForestSetMinimumRefinement(dm,minRefinement);CHKERRQ(ierr);
7176b847ec6SToby Isaac     ierr = DMForestSetInitialRefinement(dm,minRefinement);CHKERRQ(ierr);
7186b847ec6SToby Isaac   }
7196b847ec6SToby Isaac   ierr = PetscOptionsInt("-dm_refine_hierarchy","equivalent to -dm_forest_set_minimum_refinement 0 and -dm_forest_set_initial_refinement",NULL,initRefinement,&initRefinement,&flg);CHKERRQ(ierr);
7206b847ec6SToby Isaac   if (flg) {
7216b847ec6SToby Isaac     ierr = DMForestSetMinimumRefinement(dm,0);CHKERRQ(ierr);
7226b847ec6SToby Isaac     ierr = DMForestSetInitialRefinement(dm,initRefinement);CHKERRQ(ierr);
7236b847ec6SToby Isaac   }
724db4d5e8cSToby Isaac   ierr = PetscOptionsTail();CHKERRQ(ierr);
725db4d5e8cSToby Isaac   PetscFunctionReturn(0);
726db4d5e8cSToby Isaac }
727db4d5e8cSToby Isaac 
728db4d5e8cSToby Isaac #undef __FUNCT__
729*d8984e3bSMatthew G. Knepley #define __FUNCT__ "DMCreateSubDM_Forest"
730*d8984e3bSMatthew G. Knepley PetscErrorCode DMCreateSubDM_Forest(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
731*d8984e3bSMatthew G. Knepley {
732*d8984e3bSMatthew G. Knepley   PetscErrorCode ierr;
733*d8984e3bSMatthew G. Knepley 
734*d8984e3bSMatthew G. Knepley   PetscFunctionBegin;
735*d8984e3bSMatthew G. Knepley   if (subdm) {ierr = DMClone(dm, subdm);CHKERRQ(ierr);}
736*d8984e3bSMatthew G. Knepley   ierr = DMCreateSubDM_Section_Private(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
737*d8984e3bSMatthew G. Knepley   PetscFunctionReturn(0);
738*d8984e3bSMatthew G. Knepley }
739*d8984e3bSMatthew G. Knepley 
740*d8984e3bSMatthew G. Knepley #undef __FUNCT__
741d222f98bSToby Isaac #define __FUNCT__ "DMInitialize_Forest"
742d222f98bSToby Isaac static PetscErrorCode DMInitialize_Forest(DM dm)
743d222f98bSToby Isaac {
744d222f98bSToby Isaac   PetscErrorCode ierr;
745d222f98bSToby Isaac 
746d222f98bSToby Isaac   PetscFunctionBegin;
747d222f98bSToby Isaac   ierr = PetscMemzero(dm->ops,sizeof(*(dm->ops)));CHKERRQ(ierr);
748d222f98bSToby Isaac 
749d222f98bSToby Isaac   dm->ops->clone          = DMClone_Forest;
750d222f98bSToby Isaac   dm->ops->setfromoptions = DMSetFromOptions_Forest;
751d222f98bSToby Isaac   dm->ops->destroy        = DMDestroy_Forest;
752*d8984e3bSMatthew G. Knepley   dm->ops->createsubdm    = DMCreateSubDM_Forest;
753d222f98bSToby Isaac   PetscFunctionReturn(0);
754d222f98bSToby Isaac }
755d222f98bSToby Isaac 
756d222f98bSToby Isaac #undef __FUNCT__
757db4d5e8cSToby Isaac #define __FUNCT__ "DMCreate_Forest"
758db4d5e8cSToby Isaac PETSC_EXTERN PetscErrorCode DMCreate_Forest(DM dm)
759db4d5e8cSToby Isaac {
760db4d5e8cSToby Isaac   DM_Forest      *forest;
761db4d5e8cSToby Isaac   PetscErrorCode ierr;
762db4d5e8cSToby Isaac 
763db4d5e8cSToby Isaac   PetscFunctionBegin;
764db4d5e8cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
765db4d5e8cSToby Isaac   ierr                        = PetscNewLog(dm,&forest);CHKERRQ(ierr);
766db4d5e8cSToby Isaac   dm->dim                     = 0;
767db4d5e8cSToby Isaac   dm->data                    = forest;
768db4d5e8cSToby Isaac   forest->refct               = 1;
769db4d5e8cSToby Isaac   forest->data                = NULL;
770dd8e54a2SToby Isaac   forest->setFromOptions      = PETSC_FALSE;
771db4d5e8cSToby Isaac   forest->topology            = NULL;
772db4d5e8cSToby Isaac   forest->base                = NULL;
773db4d5e8cSToby Isaac   forest->adjDim              = PETSC_DEFAULT;
774db4d5e8cSToby Isaac   forest->overlap             = PETSC_DEFAULT;
775db4d5e8cSToby Isaac   forest->minRefinement       = PETSC_DEFAULT;
776db4d5e8cSToby Isaac   forest->maxRefinement       = PETSC_DEFAULT;
77756ba9f64SToby Isaac   forest->initRefinement      = PETSC_DEFAULT;
778c7eeac06SToby Isaac   forest->cStart              = PETSC_DETERMINE;
779c7eeac06SToby Isaac   forest->cEnd                = PETSC_DETERMINE;
780db4d5e8cSToby Isaac   forest->cellSF              = 0;
781db4d5e8cSToby Isaac   forest->adaptMarkers        = NULL;
782db4d5e8cSToby Isaac   forest->adaptCopyMode       = PETSC_USE_POINTER;
783db4d5e8cSToby Isaac   forest->gradeFactor         = 2;
784db4d5e8cSToby Isaac   forest->cellWeights         = NULL;
785db4d5e8cSToby Isaac   forest->cellWeightsCopyMode = PETSC_USE_POINTER;
786db4d5e8cSToby Isaac   forest->weightsFactor       = 1.;
787db4d5e8cSToby Isaac   forest->weightCapacity      = 1.;
788a73e2921SToby Isaac   ierr = DMForestSetAdaptivityStrategy(dm,DMFORESTADAPTALL);CHKERRQ(ierr);
789d222f98bSToby Isaac   ierr = DMInitialize_Forest(dm);CHKERRQ(ierr);
790db4d5e8cSToby Isaac   PetscFunctionReturn(0);
791db4d5e8cSToby Isaac }
792db4d5e8cSToby Isaac 
793