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