xref: /petsc/src/dm/impls/moab/dmmoab.cxx (revision fd349b4114aa6c18db3d975e4ea5f0790da309c7)
1 #include <petsc-private/dmimpl.h> /*I  "petscdm.h"   I*/
2 #include "../src/vec/vec/impls/moab/vecmoabimpl.h" /*I  "petscvec.h"   I*/
3 
4 #include <petscdmmoab.h>
5 #include "MBTagConventions.hpp"
6 
7 typedef struct {
8   PetscInt bs; /* Number of degrees of freedom on each entity, aka tag size in moab */
9   PetscBool icreatedinstance; /* true if DM created moab instance internally, will destroy instance in DMDestroy */
10   moab::ParallelComm *pcomm;
11   moab::Interface *mbint;
12   moab::Tag ltog_tag; /* moab supports "global id" tags, which are usually local to global numbering */
13   moab::Range range;
14 } DM_Moab;
15 
16 #undef __FUNCT__
17 #define __FUNCT__ "DMCreateGlobalVector_Moab"
18 PetscErrorCode DMCreateGlobalVector_Moab(DM dm,Vec *gvec)
19 {
20   PetscErrorCode  ierr;
21   DM_Moab         *dmmoab = (DM_Moab*)dm->data;
22 
23   PetscFunctionBegin;
24   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
25   PetscValidPointer(gvec,2);
26   PetscInt block_size = ((DM_Moab*)dm->data)->bs;
27   ierr = VecMoabCreate(dmmoab->mbint,dmmoab->pcomm,block_size,dmmoab->ltog_tag,dmmoab->range,PETSC_FALSE,PETSC_TRUE,gvec);CHKERRQ(ierr);
28   PetscFunctionReturn(0);
29 }
30 
31 
32 #undef __FUNCT__
33 #define __FUNCT__ "DMCreateLocalVector_Moab"
34 PetscErrorCode DMCreateLocalVector_Moab(DM dm,Vec *gvec)
35 {
36   PetscErrorCode  ierr;
37   DM_Moab         *dmmoab = (DM_Moab*)dm->data;
38 
39   PetscFunctionBegin;
40   PetscInt bs = 1;
41   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42   PetscValidPointer(gvec,2);
43   ierr = VecMoabCreate(dmmoab->mbint,dmmoab->pcomm,bs,dmmoab->ltog_tag,dmmoab->range,PETSC_TRUE,PETSC_TRUE,gvec);CHKERRQ(ierr);
44   PetscFunctionReturn(0);
45 }
46 
47 #undef __FUNCT__
48 #define __FUNCT__ "DMDestroy_Moab"
49 PetscErrorCode DMDestroy_Moab(DM dm)
50 {
51   PetscFunctionBegin;
52   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
53 
54   // Delete the DM_Moab:
55   if(dm->data) {
56     if (((DM_Moab*)dm->data)->icreatedinstance) {
57       delete ((DM_Moab*)dm->data)->mbint;
58       ((DM_Moab*)dm->data)->mbint = NULL;
59       ((DM_Moab*)dm->data)->pcomm = NULL;
60     }
61     delete (DM_Moab*)dm->data;
62     dm->data = NULL;
63   }
64   PetscFunctionReturn(0);
65 }
66 
67 
68 #undef __FUNCT__
69 #define __FUNCT__ "DMInitialize_Moab"
70 PetscErrorCode DMInitialize_Moab(DM dm)
71 {
72   PetscFunctionBegin;
73 
74   // Create the DM_Moab and set dm->data
75   DM_Moab *dmmoab = new DM_Moab;
76   dmmoab->bs       = 0;
77   dmmoab->pcomm    = NULL;
78   dmmoab->mbint    = NULL;
79   dmmoab->ltog_tag = (moab::Tag)0;
80   dmmoab->icreatedinstance = PETSC_FALSE;
81   dm->data      = dmmoab;
82 
83     // initialize various functions
84   dm->ops->createglobalvector              = DMCreateGlobalVector_Moab;
85   dm->ops->createlocalvector               = DMCreateLocalVector_Moab;
86   dm->ops->destroy                         = DMDestroy_Moab;
87   PetscFunctionReturn(0);
88 }
89 
90 #undef __FUNCT__
91 #define __FUNCT__ "DMMoabCreate"
92 /*@
93   DMMoabCreate - Creates a DMMoab object, which encapsulates a moab instance
94 
95   Collective on MPI_Comm
96 
97   Input Parameter:
98 . comm - The communicator for the DMMoab object
99 
100   Output Parameter:
101 . moab  - The DMMoab object
102 
103   Level: beginner
104 
105 .keywords: DMMoab, create
106 @*/
107 PetscErrorCode DMMoabCreate(MPI_Comm comm, DM *moab)
108 {
109   PetscErrorCode ierr;
110 
111   PetscFunctionBegin;
112   PetscValidPointer(moab,2);
113   ierr = DMCreate(comm, moab);CHKERRQ(ierr);
114   ierr = DMSetType(*moab, DMMOAB);CHKERRQ(ierr);
115   PetscFunctionReturn(0);
116 }
117 
118 EXTERN_C_BEGIN
119 #undef __FUNCT__
120 #define __FUNCT__ "DMCreate_Moab"
121 PetscErrorCode DMCreate_Moab(DM dm)
122 {
123   DM_Moab        *moab;
124   PetscErrorCode ierr;
125 
126   PetscFunctionBegin;
127   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
128   ierr     = PetscNewLog(dm, DM_Moab, &moab);CHKERRQ(ierr);
129   dm->data = moab;
130 
131   PetscFunctionReturn(0);
132 }
133 EXTERN_C_END
134 
135 /*@
136   DMMoabCreateFromInstance - Creates a DMMoab object from an instance and (optionally) other data
137 
138   Collective on MPI_Comm
139 
140   Input Parameter:
141 . comm - The communicator for the DMMoab object
142 . moab - The MOAB Instance
143 . pcomm - A ParallelComm; if NULL, creates one internally for the whole communicator
144 . ltog_tag - A tag to use to retrieve global id for an entity; if 0, will use GLOBAL_ID_TAG_NAME/tag
145 . range - If non-NULL, contains range of entities to which DOFs will be assigned
146 
147   Output Parameter:
148 . moab  - The DMMoab object
149 
150   Level: beginner
151 
152 .keywords: DMMoab, create
153 @*/
154 #undef __FUNCT__
155 #define __FUNCT__ "DMMoabCreateFromInstance"
156 PetscErrorCode DMMoabCreateFromInstance(MPI_Comm comm, moab::Interface *iface, moab::ParallelComm *pcomm, moab::Tag ltog_tag, moab::Range *range, DM *moab)
157 {
158   PetscErrorCode ierr;
159 
160   PetscFunctionBegin;
161   PetscValidPointer(moab,2);
162   ierr = DMCreate(comm, moab);CHKERRQ(ierr);
163   ierr = DMSetType(*moab, DMMOAB);CHKERRQ(ierr);
164   ierr = DMInitialize_Moab(*moab);CHKERRQ(ierr);
165   ierr = DMMoabSetInterface(*moab, iface);CHKERRQ(ierr);
166   if (!pcomm) pcomm = new moab::ParallelComm(iface, comm);
167   ierr = DMMoabSetParallelComm(*moab, pcomm);CHKERRQ(ierr);
168   if (!ltog_tag) {
169     moab::ErrorCode merr = iface->tag_get_handle(GLOBAL_ID_TAG_NAME, ltog_tag);MBERRNM(merr);
170   }
171   ierr = DMMoabSetLocalToGlobalTag(*moab, ltog_tag);CHKERRQ(ierr);
172   if (range) {
173     ierr = DMMoabSetRange(*moab, *range);CHKERRQ(ierr);
174   }
175   PetscFunctionReturn(0);
176 }
177 
178 /*@
179   DMMoabCreateDMAndInstance - Creates a DMMoab object and a MOAB instance
180 
181   Collective on MPI_Comm
182 
183   Input Parameter:
184 . comm - The communicator for the DMMoab object
185 
186   Output Parameter:
187 . moab  - The DMMoab object
188 
189   Level: beginner
190 
191 .keywords: DMMoab, create
192 @*/
193 #undef __FUNCT__
194 #define __FUNCT__ "DMMoabCreateDMAndInstance"
195 PetscErrorCode DMMoabCreateDMAndInstance(MPI_Comm comm, DM *dm)
196 {
197   PetscErrorCode ierr;
198 
199   PetscFunctionBegin;
200   PetscValidPointer(dm,2);
201   PetscInt rank, nprocs;
202   MPI_Comm_rank(comm, &rank);
203   MPI_Comm_size(comm, &nprocs);
204   moab::Interface *iface = new moab::Core();
205   moab::ParallelComm *pcomm = new moab::ParallelComm(iface, comm);
206   ierr = DMMoabCreateFromInstance(comm, iface, pcomm, 0, NULL, dm);CHKERRQ(ierr);
207   ((DM_Moab*)(*dm)->data)->icreatedinstance = PETSC_TRUE;
208   PetscFunctionReturn(0);
209 }
210 
211 #undef __FUNCT__
212 #define __FUNCT__ "DMMoabSetParallelComm"
213 PetscErrorCode DMMoabSetParallelComm(DM dm,moab::ParallelComm *pcomm)
214 {
215   PetscFunctionBegin;
216   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
217   ((DM_Moab*)dm->data)->pcomm = pcomm;
218   ((DM_Moab*)dm->data)->mbint = pcomm->get_moab();
219   PetscFunctionReturn(0);
220 }
221 
222 
223 #undef __FUNCT__
224 #define __FUNCT__ "DMMoabGetParallelComm"
225 PetscErrorCode DMMoabGetParallelComm(DM dm,moab::ParallelComm **pcomm)
226 {
227   PetscFunctionBegin;
228   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
229   *pcomm = ((DM_Moab*)dm->data)->pcomm;
230   PetscFunctionReturn(0);
231 }
232 
233 
234 #undef __FUNCT__
235 #define __FUNCT__ "DMMoabSetInterface"
236 PetscErrorCode DMMoabSetInterface(DM dm,moab::Interface *iface)
237 {
238   PetscFunctionBegin;
239   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
240   ((DM_Moab*)dm->data)->pcomm = NULL;
241   ((DM_Moab*)dm->data)->mbint = iface;
242   PetscFunctionReturn(0);
243 }
244 
245 
246 #undef __FUNCT__
247 #define __FUNCT__ "DMMoabGetInterface"
248 PetscErrorCode DMMoabGetInterface(DM dm,moab::Interface **mbint)
249 {
250   PetscFunctionBegin;
251   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
252   *mbint = ((DM_Moab*)dm->data)->mbint;
253   PetscFunctionReturn(0);
254 }
255 
256 
257 #undef __FUNCT__
258 #define __FUNCT__ "DMMoabSetRange"
259 PetscErrorCode DMMoabSetRange(DM dm,moab::Range range)
260 {
261   PetscFunctionBegin;
262   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
263   ((DM_Moab*)dm->data)->range = range;
264   PetscFunctionReturn(0);
265 }
266 
267 
268 #undef __FUNCT__
269 #define __FUNCT__ "DMMoabGetRange"
270 PetscErrorCode DMMoabGetRange(DM dm,moab::Range *range)
271 {
272   PetscFunctionBegin;
273   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
274   *range = ((DM_Moab*)dm->data)->range;
275   PetscFunctionReturn(0);
276 }
277 
278 #undef __FUNCT__
279 #define __FUNCT__ "DMMoabSetLocalToGlobalTag"
280 PetscErrorCode DMMoabSetLocalToGlobalTag(DM dm,moab::Tag ltogtag)
281 {
282   PetscFunctionBegin;
283   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
284   ((DM_Moab*)dm->data)->ltog_tag = ltogtag;
285   PetscFunctionReturn(0);
286 }
287 
288 
289 #undef __FUNCT__
290 #define __FUNCT__ "DMMoabGetLocalToGlobalTag"
291 PetscErrorCode DMMoabGetLocalToGlobalTag(DM dm,moab::Tag *ltog_tag)
292 {
293   PetscFunctionBegin;
294   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
295   *ltog_tag = ((DM_Moab*)dm->data)->ltog_tag;
296   PetscFunctionReturn(0);
297 }
298 
299 
300 #undef __FUNCT__
301 #define __FUNCT__ "DMMoabSetBlockSize"
302 PetscErrorCode DMMoabSetBlockSize(DM dm,PetscInt bs)
303 {
304   PetscFunctionBegin;
305   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
306   ((DM_Moab*)dm->data)->bs = bs;
307   PetscFunctionReturn(0);
308 }
309 
310 
311 #undef __FUNCT__
312 #define __FUNCT__ "DMMoabGetBlockSize"
313 PetscErrorCode DMMoabGetBlockSize(DM dm,PetscInt *bs)
314 {
315   PetscFunctionBegin;
316   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
317   *bs = ((DM_Moab*)dm->data)->bs;
318   PetscFunctionReturn(0);
319 }
320 
321 
322 #undef __FUNCT__
323 #define __FUNCT__ "DMMoabCreateVectorFromTag"
324 PetscErrorCode DMMoabCreateVectorFromTag(DM dm,moab::Tag tag,moab::Range range,PetscBool serial, PetscBool destroy_tag,Vec *X)
325 {
326   PetscErrorCode ierr;
327   PetscFunctionBegin;
328   DM_Moab         *dmmoab = (DM_Moab*)dm->data;
329   ierr = VecMoabCreateFromTag(dmmoab->mbint, dmmoab->pcomm, tag, dmmoab->ltog_tag,range, serial, destroy_tag, X);CHKERRQ(ierr);
330   PetscFunctionReturn(0);
331 }
332 
333 #undef __FUNCT__
334 #define __FUNCT__ "DMMoabCreateVector"
335 PetscErrorCode DMMoabCreateVector(DM dm,PetscInt tag_size,moab::Range range,PetscBool serial,PetscBool destroy_tag,Vec *vec)
336 {
337   PetscErrorCode ierr;
338   PetscFunctionBegin;
339   DM_Moab         *dmmoab = (DM_Moab*)dm->data;
340   ierr = VecMoabCreate(dmmoab->mbint, dmmoab->pcomm, tag_size, dmmoab->ltog_tag, range, serial, destroy_tag, vec);CHKERRQ(ierr);
341   PetscFunctionReturn(0);
342 }
343 
344 
345 #undef __FUNCT__
346 #define __FUNCT__ "DMMoabGetVecTag"
347 PetscErrorCode DMMoabGetVecTag(Vec vec,moab::Tag *tag)
348 {
349   PetscErrorCode ierr;
350   PetscFunctionBegin;
351   ierr = VecMoabGetTag(vec,tag);CHKERRQ(ierr);
352   PetscFunctionReturn(0);
353 }
354 
355 
356 #undef __FUNCT__
357 #define __FUNCT__ "DMMoabGetVecRange"
358 PetscErrorCode DMMoabGetVecRange(Vec vec,moab::Range *range)
359 {
360   PetscErrorCode ierr;
361   PetscFunctionBegin;
362   ierr = VecMoabGetRange(vec,range);CHKERRQ(ierr);
363   PetscFunctionReturn(0);
364 }
365 
366 
367