proteus 1.9.1
C/C++/Fortran libraries
Loading...
Searching...
No Matches
partitioning.cpp
Go to the documentation of this file.
1#include "partitioning.h"
3#include <cstdlib>
4#include <string>
5#include <sys/stat.h>
6#include <cstring>
7#include <cstdio>
8#include <cerrno>
9#include <unistd.h>
10
11namespace proteus
12{
13//todo add overlap for element based partitions
14int partitionElementsOriginal(const MPI_Comm& PROTEUS_COMM_WORLD, Mesh& mesh, int nElements_overlap)
15{
16 using namespace std;
17 int ierr,size,rank;
18
19 ierr = MPI_Comm_size(PROTEUS_COMM_WORLD,&size);
20 ierr = MPI_Comm_rank(PROTEUS_COMM_WORLD,&rank);
21
22 //Contents
23 //
24 //1. Partition the elements in the "default" partition (contiguous
25 //chunks in given ordering)
26 //
27 //2. Partition the elementNeighbors based on this partition
28 //
29 //3. Pass to Parmetis to build a better partition of the elements
30 //
31 //4. Tag a subset of the nodes on the subdomain elements as owned
32 //using a mark and pass approach.**
33 //
34 //5. Extract the nodes in the
35 //overlapping elements.**
36 //
37 //6. Build the subdomain mesh from the
38 //subdomain elements
39 //
40 //**To be more general we could get all the support (i.e. faces
41 //and edges) and partitiong them, but the main reason for
42 //partitioning is to keep track of a global numbering for degrees
43 //of freedom that live on each type of geometric entity. We only
44 //have node and element based DOFs so I just rebuild the other
45 //information once we have elements and nodes partitioned.
46 //
47 // \todo check that I restore all data that PETSc expects to have
48 // back, add PETSc error checking macros
49 //
50 //1. Build default partitioning
51 //
52 //get offsets so we can calculate the processor to global mapping
53 //for elements in the old (default) partitioning
54 valarray<int> elementOffsets_old(size+1);
55 elementOffsets_old[0] = 0;
56 for(int sdN=0;sdN<size;sdN++)
57 elementOffsets_old[sdN+1] = elementOffsets_old[sdN] +
58 int(mesh.nElements_global)/size +
59 (int(mesh.nElements_global)%size > sdN);
60
61 //2. Extract subdomain element adjacency information could read
62 //only the required portion from a file
63 int nElements_subdomain = (elementOffsets_old[rank+1] - elementOffsets_old[rank]);
64 PetscInt *elementNeighborsOffsets_subdomain,*elementNeighbors_subdomain,*weights_subdomain;
65 PetscMalloc(sizeof(PetscInt)*(nElements_subdomain+1),&elementNeighborsOffsets_subdomain);
66 PetscMalloc(sizeof(PetscInt)*(nElements_subdomain*mesh.nElementBoundaries_element),&elementNeighbors_subdomain);
67 PetscMalloc(sizeof(PetscInt)*(nElements_subdomain*mesh.nElementBoundaries_element),&weights_subdomain);
68 //this wastes a little space
69 elementNeighborsOffsets_subdomain[0] = 0;
70 for (int eN=0,offset=0; eN < nElements_subdomain; eN++)
71 {
72 int eN_global = elementOffsets_old[rank] + eN;
73 int offsetStart=offset;
74 for (int ebN=0; ebN< mesh.nElementBoundaries_element;ebN++)
75 {
76 int eN_neighbor_global = mesh.elementNeighborsArray[eN_global*mesh.nElementBoundaries_element + ebN];
77 if (eN_neighbor_global >= 0 )
78 elementNeighbors_subdomain[offset++] = eN_neighbor_global;
79 }
80 elementNeighborsOffsets_subdomain[eN+1]=offset;
81 sort(&elementNeighbors_subdomain[offsetStart],&elementNeighbors_subdomain[offset]);
82 int weight = (elementNeighborsOffsets_subdomain[eN+1] - elementNeighborsOffsets_subdomain[eN]);
83 for (int k=elementNeighborsOffsets_subdomain[eN];k < elementNeighborsOffsets_subdomain[eN+1];k++)
84 weights_subdomain[k] = weight;
85 // for (int o = offsetStart; o < offset; o++)
86 // {
87 // std::cout<<elementNeighbors_subdomain[o]<<'\t';
88 // }
89 // std::cout<<std::endl;
90 }
91 //3. Generate the new partitiong using PETSc, this is done in parallel using parmetis
92 Mat petscAdjacency;
93 // MatCreateMPIAdj(PROTEUS_COMM_WORLD,
94 // nElements_subdomain, mesh.nElements_global,
95 // &elementNeighborsOffsets_subdomain[0], &elementNeighbors_subdomain[0],
96 // &weights_subdomain[0],
97 // &petscAdjacency);
98 ierr = MatCreateMPIAdj(PROTEUS_COMM_WORLD,
99 nElements_subdomain,
100 mesh.nElements_global,
101 elementNeighborsOffsets_subdomain,
102 elementNeighbors_subdomain,
103 PETSC_NULL,//weights_subdomain,
104 &petscAdjacency);CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
105 MatPartitioning petscPartition;
106 MatPartitioningCreate(PROTEUS_COMM_WORLD,&petscPartition);
107 MatPartitioningSetAdjacency(petscPartition,petscAdjacency);
108 MatPartitioningSetFromOptions(petscPartition);
109
110 //get a petsc index set that has the new submdomain number for each element
111 IS elementPartitioningIS_new;
112 MatPartitioningApply(petscPartition,&elementPartitioningIS_new);
113 MatPartitioningDestroy(&petscPartition);
114 //MatDestroy(petscAdjacency);
115 //ISView(elementPartitioningIS_new,PETSC_VIEWER_STDOUT_WORLD);
116
117 //experiment with metis
118 //mwf set some defaults and not call if size == 1 since metis crashes
119 //cek commenting out for now
120 //int etype=1,edgecut=0,base=0;
121 //epart assign everything to processor zero by default
122 //valarray<int> epart(0,mesh.nElements_global),npart(mesh.nNodes_global);
123 //if (size > 1)
124 // METIS_PartMeshNodal(&mesh.nElements_global,&mesh.nNodes_global,mesh.elementNodesArray,&etype,&base,&size,&edgecut,&epart[0],&npart[0]);
125 //ISCreateGeneralWithArray(PETSC_COMM_SELF,mesh.nElements_global,&epart[0],&elementPartitioningIS_new);
126 //write mesh to view with showme
127 // std::ofstream nodeout("mesh.node"),eleout("mesh.ele"),partout("mesh.part");
128 // eleout<<mesh.nElements_global<<" 3 0"<<std::endl;
129 // partout<<mesh.nElements_global<<"\t"<<size<<std::endl;
130 // for (int eN=0;eN<mesh.nElements_global;eN++)
131 // {
132 // partout<<(eN+1)<<"\t"<<(1+epart[eN])<<std::endl;
133 // eleout<<(eN+1)<<"\t"<<(1+mesh.elementNodesArray[eN*3+0])
134 // <<"\t"<<(1+mesh.elementNodesArray[eN*3+1])
135 // <<"\t"<<(1+mesh.elementNodesArray[eN*3+2])
136 // <<std::endl;
137 // }
138 // nodeout<<mesh.nNodes_global<<" 2 0 0"<<std::endl;
139 // for (int nN=0;nN<mesh.nNodes_global;nN++)
140 // {
141 // nodeout<<(nN+1)<<"\t"<<mesh.nodeArray[nN*3+0]<<"\t"<<mesh.nodeArray[nN*3+1]<<std::endl;
142 // }
143 // eleout.close();
144 // partout.close();
145 //count the new number of elements on each subdomain
146 valarray<int> nElements_subdomain_new(size);
147 ISPartitioningCount(elementPartitioningIS_new,size,&nElements_subdomain_new[0]);
148
149 //get the new offsets for the subdomain to global numbering
150 valarray<int> elementOffsets_new(size+1);
151 elementOffsets_new[0] = 0;
152 for (int sdN=0;sdN<size;sdN++)
153 elementOffsets_new[sdN+1] = elementOffsets_new[sdN] + nElements_subdomain_new[sdN];
154
155 //get the new element numbers for the elements on this subdomain
156 IS elementNumberingIS_subdomain_old2new;
157 ISPartitioningToNumbering(elementPartitioningIS_new,&elementNumberingIS_subdomain_old2new);
158
159 //now get the new element numbers for the whole mesh so that we
160 //can just read this processors elements, reorder, and renumber**
161 //
162 //**We could do this in parallel by scattering all the element
163 //information
164 IS elementNumberingIS_global_old2new;
165 ISAllGather(elementNumberingIS_subdomain_old2new,&elementNumberingIS_global_old2new);
166 //ISView(elementNumberingIS_global_old2new,PETSC_VIEWER_STDOUT_SELF);
167 const PetscInt *elementNumbering_global_old2new;
168 ISGetIndices(elementNumberingIS_global_old2new,&elementNumbering_global_old2new);
169 valarray<int> elementNumbering_global_new2old(mesh.nElements_global);
170 for(int eN=0;eN<mesh.nElements_global;eN++)
171 elementNumbering_global_new2old[elementNumbering_global_old2new[eN]] = eN;
172
173 //Sort element based arrays, maybe I don't need to do this, maybe
174 //I just need to start writing into the subdomain mesh here and
175 //preserve subdomain2old and subdomain2global mappings
176 valarray<int> elementNodesArray_new(mesh.nElements_global*mesh.nNodes_element),
177 elementNeighborsArray_new(mesh.nElements_global*mesh.nElementBoundaries_element),
178 elementMaterialTypes_new(mesh.nElements_global),
179 elementBoundaryElementsArray_new(mesh.nElementBoundaries_global*2),
180 elementBoundaryNodesArray_new(mesh.nElementBoundaries_global*mesh.nNodes_elementBoundary),
181 edgeNodesArray_new(mesh.nEdges_global*2);
182 valarray<int> elementBoundaryMaterialTypes_new(mesh.nElementBoundaries_global);
183 for (int eN=0;eN<mesh.nElements_global;eN++)
184 {
185 for (int nN=0;nN<mesh.nNodes_element;nN++)
186 elementNodesArray_new[eN*mesh.nNodes_element + nN] =
187 mesh.elementNodesArray[elementNumbering_global_new2old[eN]*mesh.nNodes_element+nN];
188 for (int ebN=0;ebN<mesh.nElementBoundaries_element;ebN++)
189 elementNeighborsArray_new[eN*mesh.nElementBoundaries_element+ebN] =
190 mesh.elementNeighborsArray[elementNumbering_global_new2old[eN]*mesh.nElementBoundaries_element+ebN];
191 elementMaterialTypes_new[eN] = mesh.elementMaterialTypes[elementNumbering_global_new2old[eN]];
192 }
193 //renumber references to element numbers
194 for (int eN=0;eN<mesh.nElements_global;eN++)
195 {
196 for (int ebN=0;ebN<mesh.nElementBoundaries_element;ebN++)
197 {
198 int eN_ebN = elementNeighborsArray_new[eN*mesh.nElementBoundaries_element+ebN];
199 if (eN_ebN >= 0)
200 elementNeighborsArray_new[eN*mesh.nElementBoundaries_element+ebN] =
201 elementNumbering_global_old2new[eN_ebN];
202 }
203 }
204 for(int ebN=0;ebN<mesh.nElementBoundaries_global;ebN++)
205 {
206 int eN_L_old = mesh.elementBoundaryElementsArray[ebN*2+0],
207 eN_R_old = mesh.elementBoundaryElementsArray[ebN*2+1];
208 elementBoundaryElementsArray_new[ebN*2+0] = elementNumbering_global_old2new[eN_L_old];
209 if(eN_R_old >= 0)
210 elementBoundaryElementsArray_new[ebN*2+1] = elementNumbering_global_old2new[eN_R_old];
211 //mwf assume same numbering scheme for element boundaries for now?
212 elementBoundaryMaterialTypes_new[ebN] = mesh.elementBoundaryMaterialTypes[ebN];
213 }
214 //4. now we need to build a new node ordering with better data locality for C0 finite elements
215 //otherwise we could just grab the nodes on the subdomain and not worry about ownership
216 //in the long run it wouldn't be bad to do a global repartition of faces and edges for mixed hybrid
217 //and non-conforming finite elements
218 MPI_Status status;
219 PetscBT nodeMask;
220 PetscBTCreate(mesh.nNodes_global,&nodeMask);
221 if (rank > 0)
222 {
223 MPI_Recv(nodeMask,PetscBTLength(mesh.nNodes_global),MPI_CHAR,rank-1,0,PROTEUS_COMM_WORLD,&status);
224 }
225 //mark the unmarked nodes on this subdomain and store the node numbers
226 set<int> nodes_subdomain_owned;
227 for(int eN=elementOffsets_new[rank];eN<elementOffsets_new[rank+1];eN++)
228 for(int nN=0;nN<mesh.nNodes_element;nN++)
229 {
230 int nN_global = elementNodesArray_new[eN*mesh.nNodes_element+nN];
231 if (!PetscBTLookupSet(nodeMask,nN_global))
232 nodes_subdomain_owned.insert(nN_global);
233 }
234 //ship off the mask
235 if (rank < size-1)
236 MPI_Send(nodeMask,PetscBTLength(mesh.nNodes_global),MPI_CHAR,rank+1,0,PROTEUS_COMM_WORLD);
237 ierr = PetscBTDestroy(&nodeMask);
238 if (ierr)
239 cerr<<"Error in PetscBTDestroy"<<endl;
240 //get the number of nodes on each processor
241 valarray<int> nNodes_subdomain_new(size),
242 nodeOffsets_new(size+1);
243 for (int sdN=0;sdN<size;sdN++)
244 if (sdN == rank)
245 nNodes_subdomain_new[sdN] = nodes_subdomain_owned.size();
246 else
247 nNodes_subdomain_new[sdN] = 0;
248 valarray<int> nNodes_subdomain_new_send=nNodes_subdomain_new;
249 MPI_Allreduce(&nNodes_subdomain_new_send[0],&nNodes_subdomain_new[0],size,MPI_INT,MPI_SUM,PROTEUS_COMM_WORLD);
250 nodeOffsets_new[0] = 0;
251 for (int sdN=0;sdN<size;sdN++)
252 nodeOffsets_new[sdN+1] = nodeOffsets_new[sdN]+nNodes_subdomain_new[sdN];
253 //Now as with elements build a global node numbering, sort node
254 //based information, and renumber references to node numbers
255 valarray<int> nodeNumbering_new2old(nodes_subdomain_owned.size());
256 set<int>::iterator nN_ownedp=nodes_subdomain_owned.begin();
257 for (int nN=0;nN<int(nodes_subdomain_owned.size());nN++)
258 {
259 nodeNumbering_new2old[nN] = *nN_ownedp++;
260 }
261 IS nodeNumberingIS_new2old;
262 ISCreateGeneral(PROTEUS_COMM_WORLD,nodes_subdomain_owned.size(),&nodeNumbering_new2old[0],PETSC_COPY_VALUES,&nodeNumberingIS_new2old);
263 IS nodeNumberingIS_global_new2old;
264 ISAllGather(nodeNumberingIS_new2old,&nodeNumberingIS_global_new2old);
265 const PetscInt *nodeNumbering_global_new2old;
266 valarray<int> nodeNumbering_old2new_global(mesh.nNodes_global);
267 ISGetIndices(nodeNumberingIS_global_new2old,&nodeNumbering_global_new2old);
268 for (int nN=0;nN<mesh.nNodes_global;nN++)
269 {
270 nodeNumbering_old2new_global[nodeNumbering_global_new2old[nN]] = nN;
271 }
272 for (int eN=0;eN < mesh.nElements_global; eN++)
273 {
274 int nN_old;
275 for (int nN=0;nN < mesh.nNodes_element; nN++)
276 {
277 nN_old = elementNodesArray_new[eN*mesh.nNodes_element+nN];
278 elementNodesArray_new[eN*mesh.nNodes_element+nN] = nodeNumbering_old2new_global[nN_old];
279 }
280 }
281 for (int i=0;i<mesh.nElementBoundaries_global*mesh.nNodes_elementBoundary;i++)
282 {
283 int nN_old = mesh.elementBoundaryNodesArray[i];
284 elementBoundaryNodesArray_new[i] = nodeNumbering_old2new_global[nN_old];
285 }
286 for (int i=0;i<mesh.nEdges_global*2;i++)
287 {
288 int nN_old = mesh.edgeNodesArray[i];
289 edgeNodesArray_new[i] = nodeNumbering_old2new_global[nN_old];
290 }
291 valarray<int> nodeStarArray_new(mesh.nodeStarOffsets[mesh.nNodes_global]);
292 for (int i=0;i<mesh.nodeStarOffsets[mesh.nNodes_global];i++)
293 {
294 int nN_old = mesh.nodeStarArray[i];
295 nodeStarArray_new[i] = nodeNumbering_old2new_global[nN_old];
296 }
297 valarray<double> nodeArray_new(mesh.nNodes_global*3);
298 valarray<int> nodeMaterialTypes_new(mesh.nNodes_global);
299 for (int nN=0;nN<mesh.nNodes_global;nN++)
300 {
301 int nN_new = nodeNumbering_old2new_global[nN];
302 nodeArray_new[nN_new*3+0] = mesh.nodeArray[nN*3+0];
303 nodeArray_new[nN_new*3+1] = mesh.nodeArray[nN*3+1];
304 nodeArray_new[nN_new*3+2] = mesh.nodeArray[nN*3+2];
305 nodeMaterialTypes_new[nN_new] = mesh.nodeMaterialTypes[nN];
306 }
307 //write partitioned mesh to view with "showme"
308 // std::ofstream nodeout("mesh.node"),eleout("mesh.ele"),partout("mesh.part");
309 // eleout<<mesh.nElements_global<<" 3 0"<<std::endl;
310 // partout<<mesh.nElements_global<<"\t"<<size<<std::endl;
311 // for (int eN=0;eN<mesh.nElements_global;eN++)
312 // {
313 // partout<<(eN+1)<<"\t"<<(1+epart[elementNumbering_global_new2old[eN]])<<std::endl;
314 // //partout<<(eN+1)<<"\t"<<(1+epart[eN])<<std::endl;
315 // eleout<<(eN+1)<<"\t"<<(1+elementNodesArray_new[eN*3+0])
316 // <<"\t"<<(1+elementNodesArray_new[eN*3+1])
317 // <<"\t"<<(1+elementNodesArray_new[eN*3+2])
318 // <<std::endl;
319 // }
320 // nodeout<<mesh.nNodes_global<<" 2 0 0"<<std::endl;
321 // for (int nN=0;nN<mesh.nNodes_global;nN++)
322 // {
323 // nodeout<<(nN+1)<<"\t"<<nodeArray_new[nN*3+0]<<"\t"<<nodeArray_new[nN*3+1]<<std::endl;
324 // }
325 // eleout.close();
326 // partout.close();
327 //5. At this point we have new, renumbered and sorted the global element and node based information, and
328 //we have it all on each processor so we can add overlap
329 set<int> elements_overlap,nodes_overlap;
330 for(int eN=elementOffsets_new[rank];eN<elementOffsets_new[rank+1];eN++)
331 for (int nN=0;nN<mesh.nNodes_element;nN++)
332 {
333 int nN_global = elementNodesArray_new[eN*mesh.nNodes_element+nN];
334 if (nN_global < nodeOffsets_new[rank] || nN_global >= nodeOffsets_new[rank+1])
335 nodes_overlap.insert(nN_global);
336 }
337
338
339 if (nElements_overlap > 0)
340 {
341 //get all elements in the node stars and their nodes
342 // for (int nN_new=nodeOffsets_new[rank]; nN_new < nodeOffsets_new[rank+1]; nN_new++)
343 // {
344 // int nN = nodeNumbering_global_new2old[nN_new];
345 // for (int offset =mesh.nodeElementOffsets[nN];offset<mesh.nodeElementOffsets[nN+1];offset++)
346 // {
347 // int eN = mesh.nodeElementsArray[offset];
348 // int eN_new = elementNumbering_global_old2new[eN];
349 // if (eN_new < elementOffsets_new[rank] or eN_new >= elementOffsets_new[rank+1])
350 // {
351 // elements_overlap.insert(eN_new);
352 // for (int nN_element=0;nN_element<mesh.nNodes_element;nN_element++)
353 // {
354 // int nN_global = elementNodesArray_new[eN_new*mesh.nNodes_element+nN_element];
355 // if (nN_global < nodeOffsets_new[rank] or nN_global >= nodeOffsets_new[rank+1])
356 // nodes_overlap.insert(nN_global);
357 // }
358 // }
359 // }
360 // }
361 //get all elements in the node stars of owned nodes and those elements' nodes
362 for (set<int>::iterator nN=nodes_subdomain_owned.begin();nN != nodes_subdomain_owned.end();nN++)
363 {
364 for (int offset =mesh.nodeElementOffsets[*nN];offset<mesh.nodeElementOffsets[(*nN)+1];offset++)
365 {
366 int eN = mesh.nodeElementsArray[offset];
367 int eN_new = elementNumbering_global_old2new[eN];
368 if (eN_new < elementOffsets_new[rank] or eN_new >= elementOffsets_new[rank+1])
369 {
370 elements_overlap.insert(eN_new);
371 for (int nN_element=0;nN_element<mesh.nNodes_element;nN_element++)
372 {
373 int nN_global = elementNodesArray_new[eN_new*mesh.nNodes_element+nN_element];
374 if (nN_global < nodeOffsets_new[rank] or nN_global >= nodeOffsets_new[rank+1])
375 nodes_overlap.insert(nN_global);
376 }
377 }
378 }
379 }
380 //get all the element neighbors
381 for(int eN=elementOffsets_new[rank];eN<elementOffsets_new[rank+1];eN++)
382 for(int ebN=0;ebN<mesh.nElementBoundaries_element;ebN++)
383 {
384 int eN_ebN = elementNeighborsArray_new[eN*mesh.nElementBoundaries_element+ebN];
385 if (eN_ebN >= 0 &&
386 (eN_ebN < elementOffsets_new[rank] || eN_ebN >= elementOffsets_new[rank+1]))
387 {
388 elements_overlap.insert(eN_ebN);
389 for (int nN=0;nN<mesh.nNodes_element;nN++)
390 {
391 int nN_global = elementNodesArray_new[eN_ebN*mesh.nNodes_element+nN];
392 if (nN_global < nodeOffsets_new[rank] || nN_global >= nodeOffsets_new[rank+1])
393 nodes_overlap.insert(nN_global);
394 }
395 }
396 }
397 }
398 for (int layer=1;layer<nElements_overlap;layer++)
399 {
400 for (set<int>::iterator eN_p=elements_overlap.begin();eN_p != elements_overlap.end();eN_p++)
401 {
402 int eN_global = *eN_p;
403 for(int ebN=0;ebN<mesh.nElementBoundaries_element;ebN++)
404 {
405 int eN_ebN = elementNeighborsArray_new[eN_global*mesh.nElementBoundaries_element+ebN];
406 if (eN_ebN >= 0 &&
407 (eN_ebN < elementOffsets_new[rank] || eN_ebN >= elementOffsets_new[rank+1]))
408 {
409 elements_overlap.insert(eN_ebN);
410 for (int nN=0;nN<mesh.nNodes_element;nN++)
411 {
412 int nN_global = elementNodesArray_new[eN_ebN*mesh.nNodes_element+nN];
413 if (nN_global < nodeOffsets_new[rank] || nN_global >= nodeOffsets_new[rank+1])
414 nodes_overlap.insert(nN_global);
415 }
416 }
417 }
418 }
419 }
420 //6. Now build subdomain mesh
421 //
422 //set what we know
423 if (mesh.subdomainp == NULL)
424 mesh.subdomainp = new Mesh();
425 mesh.subdomainp->usePlex = mesh.usePlex;
426 mesh.subdomainp->nElements_global = nElements_subdomain_new[rank] + elements_overlap.size();
427 mesh.subdomainp->nNodes_global = nNodes_subdomain_new[rank] + nodes_overlap.size();
431 //load the elements and nodes
432 valarray<int> nodeNumbering_subdomain2global(mesh.subdomainp->nNodes_global);
433 map<int,int> nodeNumbering_global2subdomain;
434 mesh.subdomainp->nodeArray = new double[mesh.subdomainp->nNodes_global*3];
436 for(int nN=0;nN<nNodes_subdomain_new[rank];nN++)
437 {
438 int nN_global = nN + nodeOffsets_new[rank];
439 nodeNumbering_subdomain2global[nN] = nN_global;
440 nodeNumbering_global2subdomain[nN_global] = nN;
441 mesh.subdomainp->nodeArray[nN*3+0] = nodeArray_new[nN_global*3+0];
442 mesh.subdomainp->nodeArray[nN*3+1] = nodeArray_new[nN_global*3+1];
443 mesh.subdomainp->nodeArray[nN*3+2] = nodeArray_new[nN_global*3+2];
444 mesh.subdomainp->nodeMaterialTypes[nN]= nodeMaterialTypes_new[nN_global];
445 }
446 //note: sets in C++ are sorted so the overlap is laid out in
447 //contiguous chunks corresponding to the partitions
448 set<int>::iterator nN_p=nodes_overlap.begin();
449 for(int nN=nNodes_subdomain_new[rank];nN < nNodes_subdomain_new[rank] + int(nodes_overlap.size()); nN++)
450 {
451 int nN_global = *nN_p++;
452 nodeNumbering_subdomain2global[nN] = nN_global;
453 nodeNumbering_global2subdomain[nN_global] = nN;
454 mesh.subdomainp->nodeArray[nN*3+0] = nodeArray_new[nN_global*3+0];
455 mesh.subdomainp->nodeArray[nN*3+1] = nodeArray_new[nN_global*3+1];
456 mesh.subdomainp->nodeArray[nN*3+2] = nodeArray_new[nN_global*3+2];
457 mesh.subdomainp->nodeMaterialTypes[nN]= nodeMaterialTypes_new[nN_global];
458 }
461 valarray<int> elementNumbering_subdomain2global(mesh.subdomainp->nElements_global);
462 for (int eN=0;eN<nElements_subdomain_new[rank];eN++)
463 {
464 int eN_global = eN+elementOffsets_new[rank];
465 elementNumbering_subdomain2global[eN] = eN_global;
466 mesh.subdomainp->elementMaterialTypes[eN] = elementMaterialTypes_new[eN_global];
467 for (int nN=0;nN<mesh.subdomainp->nNodes_element;nN++)
469 nodeNumbering_global2subdomain[elementNodesArray_new[eN_global*mesh.nNodes_element + nN]];
470 }
471 set<int>::iterator eN_p=elements_overlap.begin();
472 for(int eN=nElements_subdomain_new[rank];eN < nElements_subdomain_new[rank]+int(elements_overlap.size());eN++)
473 {
474 int eN_global = *eN_p++;
475 mesh.subdomainp->elementMaterialTypes[eN] = elementMaterialTypes_new[eN_global];
476 elementNumbering_subdomain2global[eN] = eN_global;
477 for (int nN=0;nN<mesh.subdomainp->nNodes_element;nN++)
479 nodeNumbering_global2subdomain[elementNodesArray_new[eN_global*mesh.nNodes_element + nN]];
480 }
481
482 if (mesh.subdomainp->nNodes_element == 2)
483 {
487 }
488 else if (mesh.subdomainp->nNodes_element == 3)
489 {
493 }
494 else if (mesh.subdomainp->nNodes_element == 4)
495 {
499 }
500 if (mesh.elementBoundaryMaterialTypes != NULL)
501 {
502 assert(mesh.elementBoundariesArray != NULL);
503 //mwftodo need to copy over elementBoundaryMaterialTypes now
504 for (int eN=0;eN<mesh.subdomainp->nElements_global;eN++)
505 {
506 int eN_global_new = elementNumbering_subdomain2global[eN];
507 int eN_global_old = elementNumbering_global_new2old[eN_global_new];
508 for (int ebN_element = 0; ebN_element < mesh.nElementBoundaries_element; ebN_element++)
509 {
510 int ebN_global_old = mesh.elementBoundariesArray[eN_global_old*mesh.nElementBoundaries_element+ebN_element];
511 int ebN_subdomain = mesh.subdomainp->elementBoundariesArray[eN*mesh.nElementBoundaries_element+ebN_element];
512 mesh.subdomainp->elementBoundaryMaterialTypes[ebN_subdomain] = mesh.elementBoundaryMaterialTypes[ebN_global_old];
513 }
514 }
515 }
516 //now we've got the old mesh in the old ordering and the subdomain mesh in the new ordering
517 //the first chunk of nodes and elements are the owned elements so we need to know how many of those there are
518 //and the offset of the first one so we can compute subdomain2global
519 mesh.nodeOffsets_subdomain_owned = new int[size+1];
520 mesh.elementOffsets_subdomain_owned = new int[size+1];
521 for (int sdN=0;sdN<size+1;sdN++)
522 {
523 mesh.nodeOffsets_subdomain_owned[sdN] = nodeOffsets_new[sdN];
524 mesh.elementOffsets_subdomain_owned[sdN] = elementOffsets_new[sdN];
525 }
526 //we also need the subdomain 2 new global mappings
528 for (int nN=0;nN<mesh.subdomainp->nNodes_global;nN++)
529 mesh.nodeNumbering_subdomain2global[nN] = nodeNumbering_subdomain2global[nN];
531 for (int eN=0;eN<mesh.subdomainp->nElements_global;eN++)
532 mesh.elementNumbering_subdomain2global[eN] = elementNumbering_subdomain2global[eN];
533
534 ISRestoreIndices(elementNumberingIS_global_old2new,&elementNumbering_global_old2new);
535
536 ISDestroy(&elementPartitioningIS_new);
537 ISDestroy(&elementNumberingIS_subdomain_old2new);
538 ISDestroy(&elementNumberingIS_global_old2new);
539
540 ISRestoreIndices(nodeNumberingIS_global_new2old,&nodeNumbering_global_new2old);
541
542 ISDestroy(&nodeNumberingIS_new2old);
543 ISDestroy(&nodeNumberingIS_global_new2old);
544
545 return 0;
546}
547
548int partitionNodes(const MPI_Comm& PROTEUS_COMM_WORLD, Mesh& mesh, int nNodes_overlap)
549{
550 using namespace std;
551 int ierr,size,rank;
552
553 ierr = MPI_Comm_size(PROTEUS_COMM_WORLD,&size);
554 ierr = MPI_Comm_rank(PROTEUS_COMM_WORLD,&rank);
555 /***********************************************************************
556 partition domain based on nodes rather than elements, basically repeats
557 partitionElements with this one modification
558
559 right now generates equivalent of 1 layer of overlap regardless of input
560
561 1. Partition nodes in the default partition of contiguous chunks with
562 input ordering
563
564 2. Determine nodal connectivity on local processor
565
566 3. Generate new nodal partition using PETSc interface
567
568 4. Collect elements containing locally owned nodes and assign ownership
569
570 5. Generate global element numbering for new subdomain ownership
571
572 6. Create overlap (ghost) information for nodes and elements
573
574 7. March through additional layers of overlap if requested,
575
576 8. Build subdomain meshes in new numbering
577 ***********************************************************************/
578 //
579 //1. Build default nodal partition
580 //
581 //compute offsets to build processor (local) to global ordering for nodes
582 //in default partitioning
583 // printf("rank %d partitionNodes nNodes_global %d\n", rank, mesh.nNodes_global);
584 valarray<int> nodeOffsets_old(size+1);
585 nodeOffsets_old[0] = 0;
586 for (int sdN=0; sdN < size; sdN++)
587 {
588 nodeOffsets_old[sdN+1] = nodeOffsets_old[sdN] +
589 int(mesh.nNodes_global)/size + (int(mesh.nNodes_global)%size > sdN);
590 // printf("rank %d sdN %d nNodes_global %d nodeOffsets_old[%d] = %d\n", rank, sdN, mesh.nNodes_global, sdN+1, nodeOffsets_old[sdN+1]);
591 }
592 //
593 //2. Determine nodal connectivity on local processor, (local node star array)
594 //
595 int nNodes_subdomain = (nodeOffsets_old[rank+1] - nodeOffsets_old[rank]);
596 // printf("rank %d nNodes_subdomain %d\n", rank, nNodes_subdomain);
597 PetscInt *nodeNeighborsOffsets_subdomain,*nodeNeighbors_subdomain,*weights_subdomain;
598 PetscMalloc(sizeof(PetscInt)*(nNodes_subdomain+1),&nodeNeighborsOffsets_subdomain);
599 PetscMalloc(sizeof(PetscInt)*(nNodes_subdomain*mesh.max_nNodeNeighbors_node),&nodeNeighbors_subdomain);
600 PetscMalloc(sizeof(PetscInt)*(nNodes_subdomain*mesh.max_nNodeNeighbors_node),&weights_subdomain);
601 nodeNeighborsOffsets_subdomain[0] = 0;
602 for (int nN = 0,offset=0; nN < nNodes_subdomain; nN++)
603 {
604 int nN_global = nodeOffsets_old[rank] + nN;
605 // printf("rank %d nN %d nodeOffsets_old[rank] %d nNodes_subdomain %d nN_global = %d nodeStarOffset = %d\n", rank, nN, nodeOffsets_old[rank], nNodes_subdomain, nN_global, mesh.nodeStarOffsets[nN_global]);
606 for (int offset_global = mesh.nodeStarOffsets[nN_global];
607 offset_global < mesh.nodeStarOffsets[nN_global+1]; offset_global++)
608 {
609 nodeNeighbors_subdomain[offset++] = mesh.nodeStarArray[offset_global];
610 // printf("rank %d nN %d offset_global %d mesh.nodeStarOffsets[nN_global+1] %d nodeNeighbors_subdomain[offset++] = %d mesh.nodeStarArray[offset_global]\n", rank, nN, offset_global, mesh.nodeStarOffsets[nN_global+1], nodeNeighbors_subdomain[offset++], mesh.nodeStarArray[offset_global]);
611 }
612 nodeNeighborsOffsets_subdomain[nN+1]=offset;
613 sort(&nodeNeighbors_subdomain[nodeNeighborsOffsets_subdomain[nN]],&nodeNeighbors_subdomain[nodeNeighborsOffsets_subdomain[nN+1]]);
614 int weight= (nodeNeighborsOffsets_subdomain[nN+1] - nodeNeighborsOffsets_subdomain[nN]);
615 for (int k=nodeNeighborsOffsets_subdomain[nN];k<nodeNeighborsOffsets_subdomain[nN+1];k++)
616 weights_subdomain[k] = weight;
617 // printf("rank %d nodeNeighborsOffsets_subdomain[0] = %d\n", rank, nodeNeighborsOffsets_subdomain[0]);
618 }
619 //
620 //3. Generate new nodal partition using PETSc interface
621 //
622 Mat petscAdjacency;
623 // MatCreateMPIAdj(PROTEUS_COMM_WORLD,
624 // nNodes_subdomain, mesh.nNodes_global,
625 // &nodeNeighborsOffsets_subdomain[0], &nodeNeighbors_subdomain[0],
626 // &weights_subdomain[0],//PETSC_NULL,//ignore weighting for now
627 // &petscAdjacency);
628 ierr = MatCreateMPIAdj(PROTEUS_COMM_WORLD,
629 nNodes_subdomain,
630 mesh.nNodes_global,
631 nodeNeighborsOffsets_subdomain,
632 nodeNeighbors_subdomain,
633 PETSC_NULL,//weights_subdomain,
634 &petscAdjacency);CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
635 MatPartitioning petscPartition;
636 MatPartitioningCreate(PROTEUS_COMM_WORLD,&petscPartition);
637 MatPartitioningSetAdjacency(petscPartition,petscAdjacency);
638 MatPartitioningSetFromOptions(petscPartition);
639
640 //get petsc index set that has the new subdomain number for each node
641 IS nodePartitioningIS_new;
642 MatPartitioningApply(petscPartition,&nodePartitioningIS_new);
643 MatPartitioningDestroy(&petscPartition); //gets petscAdjacency too I believe
644
645 //determine the number of nodes per subdomain in new partitioning
646 valarray<int> nNodes_subdomain_new(size);
647 ISPartitioningCount(nodePartitioningIS_new,size,&nNodes_subdomain_new[0]);
648
649 //need new offsets for subdomain to global numbering
650 valarray<int> nodeOffsets_new(size+1);
651 nodeOffsets_new[0] = 0;
652 for (int sdN = 0; sdN < size; sdN++)
653 nodeOffsets_new[sdN+1] = nodeOffsets_new[sdN] + nNodes_subdomain_new[sdN];
654
655 //get the new node numbers for nodes on this subdomain
656 IS nodeNumberingIS_subdomain_old2new;
657 ISPartitioningToNumbering(nodePartitioningIS_new,&nodeNumberingIS_subdomain_old2new);
658
659 //collect new node numbers for whole mesh so that subdomain reordering and renumbering
660 //can be done easily
661
662 IS nodeNumberingIS_global_old2new;
663 ISAllGather(nodeNumberingIS_subdomain_old2new,&nodeNumberingIS_global_old2new);
664 //mwf original and correct I believe
665 const PetscInt * nodeNumbering_global_old2new;//needs restore call
666 ISGetIndices(nodeNumberingIS_global_old2new,&nodeNumbering_global_old2new);
667
668 //reverse mapping for node numbers as well
669 valarray<int> nodeNumbering_global_new2old(mesh.nNodes_global);
670 for (int nN = 0; nN < mesh.nNodes_global; nN++)
671 nodeNumbering_global_new2old[nodeNumbering_global_old2new[nN]] = nN;
672
673 // //mwf debug
674 // if (rank == 0)
675 // {
676 // for (int sdN = 0; sdN < size+1; sdN++)
677 // std::cout<<"partitionNodes rank= "<<rank<<" nodeOffset["<<sdN<<"]= "<<nodeOffsets_new[sdN]<<std::endl;
678 // for (int nN = 0; nN < mesh.nNodes_global; nN++)
679 // {
680 // std::cout<<"partitionNodes rank= "<<rank<<" nN= "<<nN<<" old2new= "<<nodeNumbering_global_old2new[nN]<<" new2old= "<<nodeNumbering_global_new2old[nN]<<std::endl;
681 // }
682 // for (int sdN = 0; sdN < size; sdN++)
683 // {
684 // std::cout<<"============"<<std::endl;
685 // std::cout<<"partitionNodes rank= "<<sdN<<" nNodes_owned= "<<nodeOffsets_new[sdN+1]-nodeOffsets_new[sdN]<<" = "<<std::endl;
686 // for (int nN = nodeOffsets_new[sdN]; nN < nodeOffsets_new[sdN+1]; nN++)
687 // {
688 // std::cout<<"new number= "<<nN<<" <--> old number "<<nodeNumbering_global_new2old[nN]<<" x,y,z= "
689 // <<mesh.nodeArray[nodeNumbering_global_new2old[nN]*3+0]<<" , "
690 // <<mesh.nodeArray[nodeNumbering_global_new2old[nN]*3+1]<<" , "
691 // <<mesh.nodeArray[nodeNumbering_global_new2old[nN]*3+2]<<std::endl;
692 // }
693 // }
694
695 // }
696 //
697 //4. To build subdomain meshes, go through and collect elements containing
698 // the locally owned nodes. Assign processor ownership of elements
699 //
700 MPI_Status status;
701 PetscBT elementMask;
702 PetscBTCreate(mesh.nElements_global,&elementMask);
703 //get the owned element information
704 if (rank > 0)
705 {
706 MPI_Recv(elementMask,PetscBTLength(mesh.nElements_global),MPI_CHAR,rank-1,0,PROTEUS_COMM_WORLD,&status);
707 }
708 //mark the unmarked elements on this subdomain and store element numbers (in old numbering)
709 set<int> elements_subdomain_owned;
710 for (int nN = nodeOffsets_new[rank]; nN < nodeOffsets_new[rank+1]; nN++)
711 {
712 int nN_global_old = nodeNumbering_global_new2old[nN];
713 for (int eN_star_offset = mesh.nodeElementOffsets[nN_global_old];
714 eN_star_offset < mesh.nodeElementOffsets[nN_global_old+1]; eN_star_offset++)
715 {
716 int eN_star_old = mesh.nodeElementsArray[eN_star_offset];
717 if (!PetscBTLookupSet(elementMask,eN_star_old))
718 {
719 elements_subdomain_owned.insert(eN_star_old);
720 }
721 }
722 }
723 //mwf debug
724 // for (int nN = nodeOffsets_new[rank]; nN < nodeOffsets_new[rank+1]; nN++)
725 // {
726 // int nN_global_old = nodeNumbering_global_new2old[nN];
727 // int nElements_owned_nN =0;
728 // for (int eN_star_offset = mesh.nodeElementOffsets[nN_global_old];
729 // eN_star_offset < mesh.nodeElementOffsets[nN_global_old+1]; eN_star_offset++)
730 // {
731 // int eN_star_old = mesh.nodeElementsArray[eN_star_offset];
732 // if (elements_subdomain_owned.find(eN_star_old) != elements_subdomain_owned.end())
733 // nElements_owned_nN++;
734 // }
735
736 // if (nElements_owned_nN <= 0)
737 // {
738 // std::cout<<"Problem? proc "<<rank<<" nN_new = "<<nN<<" nN_old= "<<nN_global_old<<" nElements_owned_for_nN = "<<nElements_owned_nN<<std::endl;
739 // //find out processor owners for node neighbors
740 // for (int offset = mesh.nodeStarOffsets[nN_global_old]; offset < mesh.nodeStarOffsets[nN_global_old+1]; offset++)
741 // {
742 // int nN_neig_old = mesh.nodeStarArray[offset];
743 // std::cout<<"\t neig node old "<<nN_neig_old<<" neig node new "<<nodeNumbering_global_old2new[nN_neig_old]<<" this proc offsets= ["
744 // <<nodeOffsets_new[rank]<<","<<nodeOffsets_new[rank+1]<<"];"<<std::endl;
745 // }
746 // }
747 // }
748 //pass off newly marked info
749 if (rank < size-1)
750 MPI_Send(elementMask,PetscBTLength(mesh.nElements_global),MPI_CHAR,rank+1,0,PROTEUS_COMM_WORLD);
751 ierr = PetscBTDestroy(&elementMask);
752 if (ierr)
753 cerr<<"Error in PetscBTDestroy"<<endl;
754
755 //
756 //5. Generate global element numbering corresponding to new subdomain ownership
757 //
758 valarray<int> nElements_subdomain_new(size),
759 elementOffsets_new(size+1);
760 for (int sdN = 0; sdN < size; sdN++)
761 {
762 if (sdN == rank)
763 nElements_subdomain_new[sdN] = int(elements_subdomain_owned.size());
764 else
765 nElements_subdomain_new[sdN] = 0;
766 }
767 valarray<int> nElements_subdomain_new_send = nElements_subdomain_new;
768 MPI_Allreduce(&nElements_subdomain_new_send[0],&nElements_subdomain_new[0],size,MPI_INT,MPI_SUM,PROTEUS_COMM_WORLD);
769 //new size info
770 elementOffsets_new[0] = 0;
771 for (int sdN = 0; sdN < size; sdN++)
772 elementOffsets_new[sdN+1] = elementOffsets_new[sdN] + nElements_subdomain_new[sdN];
773
774 //map to old element numbering
775 valarray<int> elementNumbering_subdomain_new2old(elements_subdomain_owned.size());
776 set<int>::iterator eN_ownedp = elements_subdomain_owned.begin();
777 for (int eN = 0; eN < int(elements_subdomain_owned.size()); eN++)
778 {
779 elementNumbering_subdomain_new2old[eN] = *eN_ownedp++;
780 }
781 //use Petsc IS to get global new2old numbering
782 IS elementNumberingIS_subdomain_new2old;
783 ISCreateGeneral(PROTEUS_COMM_WORLD,elements_subdomain_owned.size(),&elementNumbering_subdomain_new2old[0],PETSC_COPY_VALUES,
784 &elementNumberingIS_subdomain_new2old);
785 IS elementNumberingIS_global_new2old;
786 ISAllGather(elementNumberingIS_subdomain_new2old,&elementNumberingIS_global_new2old);
787
788 const PetscInt *elementNumbering_global_new2old;//needs to be restored
789 ISGetIndices(elementNumberingIS_global_new2old,&elementNumbering_global_new2old);
790 //reverse mapping
791 valarray<int> elementNumbering_global_old2new(mesh.nElements_global);
792 for (int eN = 0; eN < mesh.nElements_global; eN++)
793 {
794 elementNumbering_global_old2new[elementNumbering_global_new2old[eN]] = eN;
795 }
796
797
798
799 //4b,5b. repeat process to build global face numbering
800 //first get element --> element boundaries array for new element but old element boundary numbering
801 valarray<int> elementBoundariesArray_new(mesh.nElements_global*mesh.nElementBoundaries_element);
802 for (int eN=0; eN < mesh.nElements_global; eN++)
803 for (int ebN=0; ebN < mesh.nElementBoundaries_element; ebN++)
804 {
805 elementBoundariesArray_new[eN*mesh.nElementBoundaries_element+ebN] =
806 mesh.elementBoundariesArray[elementNumbering_global_new2old[eN]*mesh.nElementBoundaries_element+ebN];
807 // printf("rank %d eN %d ebN %d elementBoundariesArray_new[%d] = %d\n", rank, eN, ebN, eN*mesh.nElementBoundaries_element+ebN, elementBoundariesArray_new[eN*mesh.nElementBoundaries_element+ebN]);
808 }
809 MPI_Status status_elementBoundaries;
810 PetscBT elementBoundaryMask;
811 PetscBTCreate(mesh.nElementBoundaries_global,&elementBoundaryMask);
812 if (rank > 0)
813 {
814 MPI_Recv(elementBoundaryMask,PetscBTLength(mesh.nElementBoundaries_global),MPI_CHAR,rank-1,0,PROTEUS_COMM_WORLD,&status_elementBoundaries);
815 }
816 //mark the unmarked faces on this subdomain and store the global face numbers
817 //going through owned elements can pick up owned elementBoundaries on "outside" of owned nodes nodeStars
818 set<int> elementBoundaries_subdomain_owned;
819 if (mesh.nNodes_element == 8)
820 {
821
822 int lface[6][4] = {{0,1,2,3},
823 {0,1,5,4},
824 {1,2,6,5},
825 {2,3,7,6},
826 {3,0,4,7},
827 {4,5,6,7}};
828
829 for (int nN = nodeOffsets_new[rank]; nN < nodeOffsets_new[rank+1]; nN++)
830 {
831 int nN_global_old = nodeNumbering_global_new2old[nN];
832 //now get elements in node star
833 for (int offset_old = mesh.nodeElementOffsets[nN_global_old];
834 offset_old < mesh.nodeElementOffsets[nN_global_old+1]; offset_old++)
835 {
836 int eN_star_old = mesh.nodeElementsArray[offset_old];
837 int eN_star_new = elementNumbering_global_old2new[eN_star_old];
838
839 //loop through element boundaries on each element, want but want to skip
840 //the element boundary across from the owned node
841 for (int ebN=0; ebN<mesh.nElementBoundaries_element;ebN++)
842 {
843 bool foundNode = false;
844 for (int nNl=0; nNl<mesh.nNodes_elementBoundary ;nNl++)
845 {
846 int nN_global_old_across = mesh.elementNodesArray[eN_star_old*mesh.nNodes_element + lface[ebN][nNl]];
847 if (nN_global_old_across == nN_global_old) foundNode = true;
848 }
849 if (foundNode)
850 {
851 int ebN_global=elementBoundariesArray_new[eN_star_new*mesh.nElementBoundaries_element+ebN];
852 if (!PetscBTLookupSet(elementBoundaryMask,ebN_global))
853 elementBoundaries_subdomain_owned.insert(ebN_global);
854 }
855 }
856
857 }
858 }
859
860 }
861 else
862 {
863 for (int nN = nodeOffsets_new[rank]; nN < nodeOffsets_new[rank+1]; nN++)
864 {
865 int nN_global_old = nodeNumbering_global_new2old[nN];
866 //now get elements in node star
867 for (int offset_old = mesh.nodeElementOffsets[nN_global_old];
868 offset_old < mesh.nodeElementOffsets[nN_global_old+1]; offset_old++)
869 {
870 int eN_star_old = mesh.nodeElementsArray[offset_old];
871 int eN_star_new = elementNumbering_global_old2new[eN_star_old];
872
873 //loop through element boundaries on each element, want but want to skip
874 //the element boundary across from the owned node
875 for (int ebN=0; ebN<mesh.nElementBoundaries_element;ebN++)
876 {
877 int nN_global_old_across = mesh.elementNodesArray[eN_star_old*mesh.nNodes_element+ebN];
878 if (nN_global_old_across != nN_global_old)
879 {
880 int ebN_global=elementBoundariesArray_new[eN_star_new*mesh.nElementBoundaries_element+ebN];
881 if (!PetscBTLookupSet(elementBoundaryMask,ebN_global))
882 elementBoundaries_subdomain_owned.insert(ebN_global);
883 }
884 }
885 }
886 }
887 }
888
889 //ship off the mask
890 if (rank < size-1)
891 MPI_Send(elementBoundaryMask,PetscBTLength(mesh.nElementBoundaries_global),MPI_CHAR,rank+1,0,PROTEUS_COMM_WORLD);
892 ierr = PetscBTDestroy(&elementBoundaryMask);
893 if (ierr)
894 cerr<<"Error in PetscBTDestroy for elementBoundaries"<<endl;
895 //get the number of elementBoundaries on each processor
896 valarray<int> nElementBoundaries_subdomain_new(size),
897 elementBoundaryOffsets_new(size+1);
898 for (int sdN=0;sdN<size;sdN++)
899 if (sdN == rank)
900 nElementBoundaries_subdomain_new[sdN] = elementBoundaries_subdomain_owned.size();
901 else
902 nElementBoundaries_subdomain_new[sdN] = 0;
903 valarray<int> nElementBoundaries_subdomain_new_send=nElementBoundaries_subdomain_new;
904 MPI_Allreduce(&nElementBoundaries_subdomain_new_send[0],&nElementBoundaries_subdomain_new[0],size,MPI_INT,MPI_SUM,PROTEUS_COMM_WORLD);
905 elementBoundaryOffsets_new[0] = 0;
906 for (int sdN=0;sdN<size;sdN++)
907 elementBoundaryOffsets_new[sdN+1] = elementBoundaryOffsets_new[sdN]+nElementBoundaries_subdomain_new[sdN];
908 //Now as with elements and nodes build a global face numbering
909 //resetting the face based information is a little different since much of this is currently built below based
910 //on the element and node information
911 //
912 valarray<int> elementBoundaryNumbering_new2old(elementBoundaries_subdomain_owned.size());
913 set<int>::iterator ebN_ownedp=elementBoundaries_subdomain_owned.begin();
914 for (int ebN=0;ebN<int(elementBoundaries_subdomain_owned.size());ebN++)
915 {
916 elementBoundaryNumbering_new2old[ebN] = *ebN_ownedp++;
917 }
918 IS elementBoundaryNumberingIS_subdomain_new2old;
919 ISCreateGeneral(PROTEUS_COMM_WORLD,elementBoundaries_subdomain_owned.size(),&elementBoundaryNumbering_new2old[0],PETSC_COPY_VALUES,&elementBoundaryNumberingIS_subdomain_new2old);
920 IS elementBoundaryNumberingIS_global_new2old;
921 ISAllGather(elementBoundaryNumberingIS_subdomain_new2old,&elementBoundaryNumberingIS_global_new2old);
922 const PetscInt *elementBoundaryNumbering_global_new2old;
923 valarray<int> elementBoundaryNumbering_old2new_global(mesh.nElementBoundaries_global);
924 ISGetIndices(elementBoundaryNumberingIS_global_new2old,&elementBoundaryNumbering_global_new2old);
925 for (int ebN=0;ebN<mesh.nElementBoundaries_global;ebN++)
926 {
927 elementBoundaryNumbering_old2new_global[elementBoundaryNumbering_global_new2old[ebN]] = ebN;
928 // printf("rank %d ebN %d old ebN %d new ebN %d \n", rank, ebN, elementBoundaryNumbering_global_new2old[ebN], elementBoundaryNumbering_old2new_global[ebN]);
929 }
930 for (int eN=0;eN < mesh.nElements_global; eN++)
931 {
932 // printf("rank %d eN %d nElementBoundaries_element %d\n", rank, eN, mesh.nElementBoundaries_element);
933 int ebN_old;
934 for (int ebN=0;ebN < mesh.nElementBoundaries_element; ebN++)
935 {
936 ebN_old = elementBoundariesArray_new[eN*mesh.nElementBoundaries_element+ebN];
937 // printf("rank %d eN %d ebN %d old ebN %d elementBoundariesArray_new %d \n", rank, eN, ebN, ebN_old, elementBoundaryNumbering_old2new_global[ebN_old]);
938 elementBoundariesArray_new[eN*mesh.nElementBoundaries_element+ebN] = elementBoundaryNumbering_old2new_global[ebN_old];
939 }
940 }
941
942 //4c,5c. Build global edge numbering as well ownership is determined by who owns the left (0) node
943 // of the edge
944 map<NodeTuple<2>, int> nodesEdgeMap_global; //new global node numbers --> original edge numbering
945 set<int> edges_subdomain_owned;
946 for (int ig = 0; ig < mesh.nEdges_global; ig++)
947 {
948 const int nN0_global_old = mesh.edgeNodesArray[2*ig];
949 const int nN1_global_old = mesh.edgeNodesArray[2*ig+1];
950 const int nN0_global = nodeNumbering_global_old2new[nN0_global_old];
951 const int nN1_global = nodeNumbering_global_old2new[nN1_global_old];
952 int nodes[2];
953 nodes[0] = nN0_global;
954 nodes[1] = nN1_global;
955 NodeTuple<2> et(nodes);
956 nodesEdgeMap_global[et] = ig;
957 if (nodeOffsets_new[rank] <= et.nodes[0] && et.nodes[0] < nodeOffsets_new[rank+1])
958 edges_subdomain_owned.insert(ig);
959 }
960 valarray<int> nEdges_subdomain_new(size),
961 edgeOffsets_new(size+1);
962
963 for (int sdN=0; sdN < size; sdN++)
964 if (sdN == rank)
965 nEdges_subdomain_new[sdN] = edges_subdomain_owned.size();
966 else
967 nEdges_subdomain_new[sdN] = 0;
968 //collect ownership info
969 valarray<int> nEdges_subdomain_new_send=nEdges_subdomain_new;
970 MPI_Allreduce(&nEdges_subdomain_new_send[0],&nEdges_subdomain_new[0],size,MPI_INT,MPI_SUM,PROTEUS_COMM_WORLD);
971 edgeOffsets_new[0] = 0;
972 for (int sdN=0;sdN<size;sdN++)
973 edgeOffsets_new[sdN+1] = edgeOffsets_new[sdN]+nEdges_subdomain_new[sdN];
974
975 //build new petsc numbering and global maps from old2new and new2old
976 valarray<int> edgeNumbering_new2old(edges_subdomain_owned.size());
977 set<int>::iterator edges_ownedp = edges_subdomain_owned.begin();
978 for (int i=0; i < int(edges_subdomain_owned.size());i++)
979 edgeNumbering_new2old[i] = *edges_ownedp++;
980
981 IS edgeNumberingIS_subdomain_new2old;
982 ISCreateGeneral(PROTEUS_COMM_WORLD,edges_subdomain_owned.size(),&edgeNumbering_new2old[0],PETSC_COPY_VALUES,&edgeNumberingIS_subdomain_new2old);
983 IS edgeNumberingIS_global_new2old;
984 ISAllGather(edgeNumberingIS_subdomain_new2old,&edgeNumberingIS_global_new2old);
985 const PetscInt *edgeNumbering_global_new2old;
986 valarray<int> edgeNumbering_old2new_global(mesh.nEdges_global);
987 ISGetIndices(edgeNumberingIS_global_new2old,&edgeNumbering_global_new2old);
988 for (int ig=0;ig<mesh.nEdges_global;ig++)
989 {
990 edgeNumbering_old2new_global[edgeNumbering_global_new2old[ig]] = ig;
991 }
992
993 //create array with (new edge) --> (new node 0, new node 1)
994 //and map from (new node 0, new node 1) --> (new global edge)
995 valarray<int> edgeNodesArray_newNodesAndEdges(2*mesh.nEdges_global);
996 map<NodeTuple<2>, int > nodesEdgeMap_global_new;
997 for (int ig = 0; ig < mesh.nEdges_global; ig++)
998 {
999 const int nN0_global_old = mesh.edgeNodesArray[2*ig];
1000 const int nN1_global_old = mesh.edgeNodesArray[2*ig+1];
1001 const int nN0_global = nodeNumbering_global_old2new[nN0_global_old];
1002 const int nN1_global = nodeNumbering_global_old2new[nN1_global_old];
1003
1004 const int edge_new = edgeNumbering_old2new_global[ig];
1005 edgeNodesArray_newNodesAndEdges[edge_new*2+0] = nN0_global;
1006 edgeNodesArray_newNodesAndEdges[edge_new*2+1] = nN1_global;
1007 int nodes[2];
1008 nodes[0] = nN0_global;
1009 nodes[1] = nN1_global;
1010 NodeTuple<2> et(nodes);
1011 nodesEdgeMap_global_new[et] = edge_new;
1012 }
1013
1014
1015
1016
1017
1018
1019 //
1020 //6. Figure out which elements are in node stars but are not locally owned, create ghost information
1021 // for these, do the same for elements
1022 set<int> elements_overlap,nodes_overlap,elementBoundaries_overlap,edges_overlap;
1023 for (int nN = nodeOffsets_new[rank]; nN < nodeOffsets_new[rank+1]; nN++)
1024 {
1025 int nN_global_old = nodeNumbering_global_new2old[nN];
1026 //nodes in node star
1027 for (int offset_old = mesh.nodeStarOffsets[nN_global_old];
1028 offset_old < mesh.nodeStarOffsets[nN_global_old+1]; offset_old++)
1029 {
1030 int nN_neig_old = mesh.nodeStarArray[offset_old];
1031 int nN_neig_new = nodeNumbering_global_old2new[nN_neig_old];
1032
1033 bool offproc = nN_neig_new >= nodeOffsets_new[rank+1] || nN_neig_new < nodeOffsets_new[rank];
1034 if (offproc)
1035 nodes_overlap.insert(nN_neig_new);
1036 }
1037 //now get elements, elementBoundaries, and edges in node star
1038 for (int offset_old = mesh.nodeElementOffsets[nN_global_old];
1039 offset_old < mesh.nodeElementOffsets[nN_global_old+1]; offset_old++)
1040 {
1041 int eN_star_old = mesh.nodeElementsArray[offset_old];
1042 int eN_star_new = elementNumbering_global_old2new[eN_star_old];
1043 bool offproc = eN_star_new >= elementOffsets_new[rank+1] || eN_star_new < elementOffsets_new[rank];
1044 if (offproc)
1045 elements_overlap.insert(eN_star_new);
1046 for (int ebN=0; ebN<mesh.nElementBoundaries_element;ebN++)
1047 {
1048 int ebN_global=elementBoundariesArray_new[eN_star_new*mesh.nElementBoundaries_element+ebN];
1049 // //mwf debug
1050 // std::cout<<"partitionNode default overlap rank= "<<rank<<" nN_new= "<<nN<<" eN_star_new= "<<eN_star_new<<" ebN= "<<ebN
1051 // <<" ebN_global= "<<ebN_global<<" ghost= "<<(ebN_global < elementBoundaryOffsets_new[rank] || ebN_global >= elementBoundaryOffsets_new[rank+1])
1052 // <<" offsets= ["<<elementBoundaryOffsets_new[rank]<<","<<elementBoundaryOffsets_new[rank+1]<<"]"<<std::endl;
1053 if (ebN_global < elementBoundaryOffsets_new[rank] || ebN_global >= elementBoundaryOffsets_new[rank+1])
1054 {
1055 elementBoundaries_overlap.insert(ebN_global);
1056 }
1057 }
1058 for (int nN0=0;nN0<mesh.nNodes_element;nN0++)
1059 for (int nN1=nN0+1; nN1<mesh.nNodes_element;nN1++)
1060 {
1061 const int nN0_global_old = mesh.elementNodesArray[eN_star_old*mesh.nNodes_element+nN0];
1062 const int nN1_global_old = mesh.elementNodesArray[eN_star_old*mesh.nNodes_element+nN1];
1063 const int nN0_global = nodeNumbering_global_old2new[nN0_global_old];
1064 const int nN1_global = nodeNumbering_global_old2new[nN1_global_old];
1065 bool foundEdge = false;
1066 int nodes[2];
1067 nodes[0] = nN0_global;
1068 nodes[1] = nN1_global;
1069 NodeTuple<2> et(nodes);
1070 const int edge_global = nodesEdgeMap_global_new[et];
1071 if (edge_global < edgeOffsets_new[rank] || edge_global >= edgeOffsets_new[rank+1])
1072 edges_overlap.insert(edge_global);
1073 }//edges
1074 }//elements in node star
1075 }//nodes on this processor
1076
1077 //
1078 //7. If we want more layers of overlap, do we have to build connectivity info in new numbering then march out
1079 // or can we just march through nodes in node_overlap and grab all of their elements that aren't owned?
1080 //
1081 int overlap_remaining = nNodes_overlap -1; //default gives 1 layer overlap
1082 //last set of overlap nodes added
1083 set<int> last_nodes_added2overlap = nodes_overlap;
1084 while (overlap_remaining > 0)
1085 {
1086 set<int> new_nodes_overlap,new_elements_overlap,new_elementBoundaries_overlap,new_edges_overlap;
1087 set<int>::iterator nN_p = last_nodes_added2overlap.begin();
1088 while (nN_p != last_nodes_added2overlap.end())
1089 {
1090 int nN_global_new = *nN_p;
1091 int nN_global_old = nodeNumbering_global_new2old[nN_global_new];//need old numbering for connectivity
1092 for (int offset_old = mesh.nodeStarOffsets[nN_global_old];
1093 offset_old < mesh.nodeStarOffsets[nN_global_old+1]; offset_old++)
1094 {
1095 int nN_neig_old = mesh.nodeStarArray[offset_old];
1096 int nN_neig_new = nodeNumbering_global_old2new[nN_neig_old];
1097 //just need to check if neighbor is offprocessor, may already be in overlap
1098 //but set merge will take care of that
1099 bool offproc = nN_neig_new >= nodeOffsets_new[rank+1] || nN_neig_new < nodeOffsets_new[rank];
1100 if (offproc)
1101 new_nodes_overlap.insert(nN_neig_new);
1102 }//node neighbor loop
1103 nN_p++;
1104 }//loop adding new nodes
1105
1106 //loop through added nodes, grab elements only check if not on processor
1107 set<int>::iterator nN_newp = last_nodes_added2overlap.begin();
1108 while (nN_newp != last_nodes_added2overlap.end())
1109 {
1110 int nN_global_new = *nN_newp;
1111 int nN_global_old = nodeNumbering_global_new2old[nN_global_new];//need old numbering for connectivity
1112 for (int offset_old = mesh.nodeElementOffsets[nN_global_old];
1113 offset_old < mesh.nodeElementOffsets[nN_global_old+1]; offset_old++)
1114 {
1115 int eN_star_old = mesh.nodeElementsArray[offset_old];
1116 int eN_star_new = elementNumbering_global_old2new[eN_star_old];
1117 bool offproc = eN_star_new >= elementOffsets_new[rank+1] || eN_star_new < elementOffsets_new[rank];
1118 if (offproc)
1119 new_elements_overlap.insert(eN_star_new);
1120 //element boundaries too
1121 for (int ebN=0; ebN<mesh.nElementBoundaries_element;ebN++)
1122 {
1123 int ebN_global=elementBoundariesArray_new[eN_star_new*mesh.nElementBoundaries_element+ebN];
1124 // //mwf debug
1125 // std::cout<<"partitionNode overlap_remaining= "<<overlap_remaining<<" rank= "<<rank<<" nN_global_new= "<<nN_global_new<<" eN_star_new= "<<eN_star_new<<" ebN= "<<ebN
1126 // <<" ebN_global= "<<ebN_global<<" ghost= "<<(ebN_global < elementBoundaryOffsets_new[rank] || ebN_global >= elementBoundaryOffsets_new[rank+1])
1127 // <<" offsets= ["<<elementBoundaryOffsets_new[rank]<<","<<elementBoundaryOffsets_new[rank+1]<<std::endl;
1128 if (ebN_global < elementBoundaryOffsets_new[rank] || ebN_global >= elementBoundaryOffsets_new[rank+1])
1129 new_elementBoundaries_overlap.insert(ebN_global);
1130 }//element boundaries
1131 //edges
1132 for (int nN0=0;nN0<mesh.nNodes_element;nN0++)
1133 for (int nN1=nN0+1; nN1<mesh.nNodes_element;nN1++)
1134 {
1135 const int nN0_global_old = mesh.elementNodesArray[eN_star_old*mesh.nNodes_element+nN0];
1136 const int nN1_global_old = mesh.elementNodesArray[eN_star_old*mesh.nNodes_element+nN1];
1137 const int nN0_global = nodeNumbering_global_old2new[nN0_global_old];
1138 const int nN1_global = nodeNumbering_global_old2new[nN1_global_old];
1139 bool foundEdge = false;
1140 int nodes[2];
1141 nodes[0] = nN0_global;
1142 nodes[1] = nN1_global;
1143 NodeTuple<2> et(nodes);
1144 const int edge_global = nodesEdgeMap_global_new[et];
1145 if (edge_global < edgeOffsets_new[rank] || edge_global >= edgeOffsets_new[rank+1])
1146 new_edges_overlap.insert(edge_global);
1147 }//edges
1148 }//elements in node star
1149 nN_newp++;
1150 }//new nodes
1151 last_nodes_added2overlap.clear();
1152 set_difference(new_nodes_overlap.begin(),new_nodes_overlap.end(),
1153 nodes_overlap.begin(),nodes_overlap.end(),
1154 std::inserter(last_nodes_added2overlap,last_nodes_added2overlap.begin()));
1155
1156 //could do a set_merge
1157 for (set<int>::iterator nN_addedp = new_nodes_overlap.begin();
1158 nN_addedp != new_nodes_overlap.end();
1159 nN_addedp++)
1160 {
1161 nodes_overlap.insert(*nN_addedp);
1162 }
1163 for (set<int>::iterator eN_addedp = new_elements_overlap.begin();
1164 eN_addedp != new_elements_overlap.end();
1165 eN_addedp++)
1166 {
1167 elements_overlap.insert(*eN_addedp);
1168 }
1169 for (set<int>::iterator ebN_addedp = new_elementBoundaries_overlap.begin();
1170 ebN_addedp != new_elementBoundaries_overlap.end();
1171 ebN_addedp++)
1172 {
1173 elementBoundaries_overlap.insert(*ebN_addedp);
1174 }
1175 for (set<int>::iterator edge_addedp = new_edges_overlap.begin();
1176 edge_addedp != new_edges_overlap.end();
1177 edge_addedp++)
1178 {
1179 edges_overlap.insert(*edge_addedp);
1180 }
1181 //example calls
1182
1183 overlap_remaining--;
1184 }//ovelap loop
1185
1186 //
1187 //8. Build subdomain meshes in new numbering, assumes memory not allocated in subdomain mesh
1188 //
1189 if (mesh.subdomainp == NULL)
1190 mesh.subdomainp = new Mesh();
1191 mesh.subdomainp->nElements_global = nElements_subdomain_new[rank] + elements_overlap.size();
1192 mesh.subdomainp->nNodes_global = nNodes_subdomain_new[rank] + nodes_overlap.size();
1193 mesh.subdomainp->nElementBoundaries_global = nElementBoundaries_subdomain_new[rank]+elementBoundaries_overlap.size();
1194 mesh.subdomainp->nEdges_global = nEdges_subdomain_new[rank]+edges_overlap.size();
1198 //subdomain 2 global mappings (including ghost info)
1199 valarray<int> nodeNumbering_subdomain2global(mesh.subdomainp->nNodes_global);
1200 valarray<int> elementNumbering_subdomain2global(mesh.subdomainp->nElements_global);
1201 valarray<int> elementBoundaryNumbering_subdomain2global(mesh.subdomainp->nElementBoundaries_global);
1202 valarray<int> edgeNumbering_subdomain2global(mesh.subdomainp->nEdges_global);
1203 map<int,int> nodeNumbering_global2subdomain;
1204 map<int,int> elementBoundaryNumbering_global2subdomain;
1205 mesh.subdomainp->nodeArray = new double[mesh.subdomainp->nNodes_global*3];
1207 //locally owned
1208 // printf("rank %d nNodes_subdomain_new = %d\n", rank, nNodes_subdomain_new[rank]);
1209 for (int nN = 0; nN < nNodes_subdomain_new[rank]; nN++)
1210 {
1211 int nN_global_new = nN + nodeOffsets_new[rank];
1212 int nN_global_old = nodeNumbering_global_new2old[nN_global_new];
1213 nodeNumbering_subdomain2global[nN] = nN_global_new;
1214 nodeNumbering_global2subdomain[nN_global_new] = nN;
1215 mesh.subdomainp->nodeArray[nN*3+0] = mesh.nodeArray[nN_global_old*3+0];
1216 mesh.subdomainp->nodeArray[nN*3+1] = mesh.nodeArray[nN_global_old*3+1];
1217 mesh.subdomainp->nodeArray[nN*3+2] = mesh.nodeArray[nN_global_old*3+2];
1218 // printf("rank %d nN %d nN_global_new %d nN_global_old %d nodeArray[%d] = (%f,%f,%f) nodeMaterialTypes[%d] = %d\n",
1219 // rank, nN, nN_global_new, nN_global_old, nN*3,
1220 // mesh.subdomainp->nodeArray[nN*3+0],
1221 // mesh.subdomainp->nodeArray[nN*3+1],
1222 // mesh.subdomainp->nodeArray[nN*3+2],
1223 // mesh.subdomainp->nodeMaterialTypes[nN]);
1224
1225 mesh.subdomainp->nodeMaterialTypes[nN] = mesh.nodeMaterialTypes[nN_global_old];
1226 }
1227 //printf("Hello 10\n");
1228 //ghost
1229 //note: sets in C++ are sorted so the overlap is laid out in
1230 //contiguous chunks corresponding to the partitions
1231 set<int>::iterator nN_p = nodes_overlap.begin();
1232 for (int nN = nNodes_subdomain_new[rank]; nN < nNodes_subdomain_new[rank] + int(nodes_overlap.size()); nN++)
1233 {
1234 int nN_global_new = *nN_p++;
1235 int nN_global_old = nodeNumbering_global_new2old[nN_global_new];
1236 nodeNumbering_subdomain2global[nN] = nN_global_new;
1237 nodeNumbering_global2subdomain[nN_global_new] = nN;
1238 mesh.subdomainp->nodeArray[nN*3+0] = mesh.nodeArray[nN_global_old*3+0];
1239 mesh.subdomainp->nodeArray[nN*3+1] = mesh.nodeArray[nN_global_old*3+1];
1240 mesh.subdomainp->nodeArray[nN*3+2] = mesh.nodeArray[nN_global_old*3+2];
1241 mesh.subdomainp->nodeMaterialTypes[nN] = mesh.nodeMaterialTypes[nN_global_old];
1242 }
1245 //locally owned
1246 for (int eN = 0; eN < nElements_subdomain_new[rank]; eN++)
1247 {
1248 int eN_global_new = elementOffsets_new[rank] + eN;
1249 int eN_global_old = elementNumbering_global_new2old[eN_global_new];
1250 elementNumbering_subdomain2global[eN] = eN_global_new;
1251 mesh.subdomainp->elementMaterialTypes[eN] = mesh.elementMaterialTypes[eN_global_old];
1252 for (int nN = 0; nN < mesh.subdomainp->nNodes_element; nN++)
1253 {
1254 int nN_global_old = mesh.elementNodesArray[eN_global_old*mesh.nNodes_element + nN];
1255 int nN_global_new = nodeNumbering_global_old2new[nN_global_old];
1256 int nN_subdomain = nodeNumbering_global2subdomain[nN_global_new];
1257 mesh.subdomainp->elementNodesArray[eN*mesh.subdomainp->nNodes_element + nN]= nN_subdomain;
1258 }
1259
1260 }
1261 //ghost
1262 set<int>::iterator eN_p = elements_overlap.begin();
1263 for (int eN = nElements_subdomain_new[rank]; eN < nElements_subdomain_new[rank] + int(elements_overlap.size()); eN++)
1264 {
1265 int eN_global_new = *eN_p++;
1266 int eN_global_old = elementNumbering_global_new2old[eN_global_new];
1267 elementNumbering_subdomain2global[eN] = eN_global_new;
1268 mesh.subdomainp->elementMaterialTypes[eN] = mesh.elementMaterialTypes[eN_global_old];
1269 for (int nN = 0; nN < mesh.subdomainp->nNodes_element; nN++)
1270 {
1271 int nN_global_old = mesh.elementNodesArray[eN_global_old*mesh.nNodes_element + nN];
1272 int nN_global_new = nodeNumbering_global_old2new[nN_global_old];
1273 int nN_subdomain = nodeNumbering_global2subdomain[nN_global_new];
1274 mesh.subdomainp->elementNodesArray[eN*mesh.subdomainp->nNodes_element + nN]= nN_subdomain;
1275 }
1276 }
1277 //element boundaries
1278 //locally owned
1279 //printf("Hello 11\n");
1280 for (int ebN=0; ebN < nElementBoundaries_subdomain_new[rank]; ebN++)
1281 {
1282 int ebN_global = ebN + elementBoundaryOffsets_new[rank];
1283 elementBoundaryNumbering_subdomain2global[ebN]=ebN_global;
1284 elementBoundaryNumbering_global2subdomain[ebN_global] = ebN;
1285 }
1286 //ghost
1287 set<int>::iterator ebN_p = elementBoundaries_overlap.begin();
1288 for(int ebN=nElementBoundaries_subdomain_new[rank];ebN < nElementBoundaries_subdomain_new[rank] + int(elementBoundaries_overlap.size()); ebN++)
1289 {
1290 int ebN_global = *ebN_p++;
1291 elementBoundaryNumbering_subdomain2global[ebN] = ebN_global;
1292 elementBoundaryNumbering_global2subdomain[ebN_global] = ebN;
1293 }
1294 //need elementBoundariesArray to assign consistent numbering on subdomain
1297 for (int eN=0;eN<nElements_subdomain_new[rank];eN++)
1298 {
1299 int eN_global = eN+elementOffsets_new[rank];
1300 for (int ebN=0;ebN<mesh.subdomainp->nElementBoundaries_element;ebN++)
1302 elementBoundaryNumbering_global2subdomain[elementBoundariesArray_new[eN_global*mesh.nElementBoundaries_element + ebN]];
1303}
1304//ghost elements
1305set<int>::iterator eN_p2 = elements_overlap.begin();
1306for (int eN = nElements_subdomain_new[rank]; eN < nElements_subdomain_new[rank] + int(elements_overlap.size()); eN++)
1307{
1308 int eN_global_new = *eN_p2++;
1309 for (int ebN=0;ebN<mesh.subdomainp->nElementBoundaries_element;ebN++)
1310 {
1311 // printf("rank %d eN %d ebN %d eN_global_new %d ebN_global %d\n", rank, eN, ebN, eN_global_new, elementBoundariesArray_new[eN_global_new*mesh.nElementBoundaries_element + ebN]);
1313 elementBoundaryNumbering_global2subdomain[elementBoundariesArray_new[eN_global_new*mesh.nElementBoundaries_element + ebN]];
1314 }
1315
1316}
1317
1318//printf("Hello 12\n");
1319//edges
1320mesh.subdomainp->edgeNodesArray = new int[mesh.subdomainp->nEdges_global*2];
1321//locally owned
1322for (int i=0; i < nEdges_subdomain_new[rank]; i++)
1323{
1324 const int ig = i+edgeOffsets_new[rank];
1325 const int nN0_global = edgeNodesArray_newNodesAndEdges[ig*2+0];
1326 const int nN1_global = edgeNodesArray_newNodesAndEdges[ig*2+1];
1327 //mwf todo double check can always count on having nodes on this processor
1328 const int nN0_subdomain = nodeNumbering_global2subdomain[nN0_global];
1329 const int nN1_subdomain = nodeNumbering_global2subdomain[nN1_global];
1330 mesh.subdomainp->edgeNodesArray[2*i+0]=nN0_subdomain;
1331 mesh.subdomainp->edgeNodesArray[2*i+1]=nN1_subdomain;
1332 edgeNumbering_subdomain2global[i] = ig;
1333}
1334//ghost
1335set<int>::iterator edge_p = edges_overlap.begin();
1336for (int i=nEdges_subdomain_new[rank]; i < nEdges_subdomain_new[rank] + int(edges_overlap.size()); i++)
1337{
1338 const int ig =*edge_p++;
1339 const int nN0_global = edgeNodesArray_newNodesAndEdges[ig*2+0];
1340 const int nN1_global = edgeNodesArray_newNodesAndEdges[ig*2+1];
1341 //mwf todo make sure always have nodes for the edge on this processor
1342 const int nN0_subdomain = nodeNumbering_global2subdomain[nN0_global];
1343 const int nN1_subdomain = nodeNumbering_global2subdomain[nN1_global];
1344 mesh.subdomainp->edgeNodesArray[2*i+0]=nN0_subdomain;
1345 mesh.subdomainp->edgeNodesArray[2*i+1]=nN1_subdomain;
1346 edgeNumbering_subdomain2global[i] = ig;
1347
1348}
1349
1350//now build rest of subdomain mesh connectivity information etc
1351mesh.subdomainp->px = mesh.px;
1352mesh.subdomainp->py = mesh.py;
1353mesh.subdomainp->pz = mesh.pz;
1354
1355if (mesh.subdomainp->px != 0)
1356{
1357 //constructElementBoundaryElementsArray_tetrahedron(*mesh.subdomainp);
1358 //constructElementBoundaryElementsArrayWithGivenElementBoundaryNumbers_tetrahedron(*mesh.subdomainp);
1362}
1363else if (mesh.subdomainp->nNodes_element == 2)
1364{
1365 //constructElementBoundaryElementsArray_edge(*mesh.subdomainp);
1366 //constructElementBoundaryElementsArrayWithGivenElementBoundaryNumbers_edge(*mesh.subdomainp);
1370}
1371else if (mesh.subdomainp->nNodes_element == 3)
1372{
1373 //constructElementBoundaryElementsArray_triangle(*mesh.subdomainp);
1374 //constructElementBoundaryElementsArrayWithGivenElementBoundaryNumbers_triangle(*mesh.subdomainp);
1378}
1379else if (mesh.subdomainp->nNodes_element == 4 && mesh.subdomainp->nNodes_elementBoundary == 2)
1380{
1381 //constructElementBoundaryElementsArray_tetrahedron(*mesh.subdomainp);
1382 //constructElementBoundaryElementsArrayWithGivenElementBoundaryNumbers_tetrahedron(*mesh.subdomainp);
1386}
1387else if (mesh.subdomainp->nNodes_element == 4 && mesh.subdomainp->nNodes_elementBoundary == 3)
1388{
1389 //constructElementBoundaryElementsArray_tetrahedron(*mesh.subdomainp);
1390 //constructElementBoundaryElementsArrayWithGivenElementBoundaryNumbers_tetrahedron(*mesh.subdomainp);
1394}
1395else if (mesh.subdomainp->nNodes_element == 8)
1396{
1400}
1401else
1402{
1403 assert(false);
1404}
1405
1406if (mesh.elementBoundaryMaterialTypes != NULL)
1407{
1408 assert(mesh.elementBoundariesArray != NULL);
1409 assert(mesh.subdomainp->elementBoundariesArray != NULL);
1410 for (int eN=0;eN<mesh.subdomainp->nElements_global;eN++)
1411 {
1412 int eN_global_new = elementNumbering_subdomain2global[eN];
1413 int eN_global_old = elementNumbering_global_new2old[eN_global_new];
1414 for (int ebN_element = 0; ebN_element < mesh.nElementBoundaries_element; ebN_element++)
1415 {
1416 int ebN_global_old = mesh.elementBoundariesArray[eN_global_old*mesh.nElementBoundaries_element+ebN_element];
1417 int ebN_subdomain = mesh.subdomainp->elementBoundariesArray[eN*mesh.nElementBoundaries_element+ebN_element];
1418 // printf("ebN_global_old = %d \t ebN_subdomain = %d \t elementBoundaryMaterialType = %d mesh.subdomainp->elementBoundaryMaterialTypes = %d\n",
1419 // ebN_global_old, ebN_subdomain, mesh.elementBoundaryMaterialTypes[ebN_global_old], mesh.subdomainp->elementBoundaryMaterialTypes[ebN_subdomain]);
1420 // printf("en = %d eN_global_new %d eN_global_old %d ebN_element %d ebN_global_old %d elementBoundariesArray = %d \n",eN,eN_global_new,eN_global_old,ebN_element,ebN_global_old, mesh.elementBoundariesArray[eN_global_old*mesh.nElementBoundaries_element+ebN_element]);
1421 // printf("rank %d eN %d eN_global_new %d eN_global_old %d ebN_element %d ebN_global_old %d ebN_subdomain %d elementBoundaryMaterialType = %d\n",
1422 // rank,eN,eN_global_new,eN_global_old,ebN_element,ebN_global_old,ebN_subdomain, mesh.elementBoundaryMaterialTypes[ebN_global_old]);
1423 mesh.subdomainp->elementBoundaryMaterialTypes[ebN_subdomain] = mesh.elementBoundaryMaterialTypes[ebN_global_old];
1424 }
1425 }
1426}
1427//transfer information about owned nodes and elements to mesh
1429delete [] mesh.nodeOffsets_subdomain_owned;
1431delete [] mesh.elementOffsets_subdomain_owned;
1435 delete [] mesh.edgeOffsets_subdomain_owned;
1436 mesh.nodeOffsets_subdomain_owned = new int[size+1];
1437 mesh.elementOffsets_subdomain_owned = new int[size+1];
1438 mesh.elementBoundaryOffsets_subdomain_owned = new int[size+1];
1439 mesh.edgeOffsets_subdomain_owned = new int[size+1];
1440 for (int sdN = 0; sdN < size+1; sdN++)
1441 {
1442 mesh.nodeOffsets_subdomain_owned[sdN] = nodeOffsets_new[sdN];
1443 mesh.elementOffsets_subdomain_owned[sdN] = elementOffsets_new[sdN];
1444 mesh.elementBoundaryOffsets_subdomain_owned[sdN] = elementBoundaryOffsets_new[sdN];
1445 mesh.edgeOffsets_subdomain_owned[sdN] = edgeOffsets_new[sdN];
1446 }
1448 delete [] mesh.nodeNumbering_subdomain2global;
1450 for (int nN = 0; nN < mesh.subdomainp->nNodes_global; nN++)
1451 mesh.nodeNumbering_subdomain2global[nN] = nodeNumbering_subdomain2global[nN];
1453 delete [] mesh.elementNumbering_subdomain2global;
1455 for (int eN = 0; eN < mesh.subdomainp->nElements_global; eN++)
1456 mesh.elementNumbering_subdomain2global[eN] = elementNumbering_subdomain2global[eN];
1457 //
1461 for (int ebN = 0; ebN < mesh.subdomainp->nElementBoundaries_global; ebN++)
1462 mesh.elementBoundaryNumbering_subdomain2global[ebN] = elementBoundaryNumbering_subdomain2global[ebN];
1463 //
1465 delete [] mesh.edgeNumbering_subdomain2global;
1467 for (int i=0; i< mesh.subdomainp->nEdges_global; i++)
1468 mesh.edgeNumbering_subdomain2global[i] = edgeNumbering_subdomain2global[i];
1469
1470 //cleanup
1471 ISRestoreIndices(nodeNumberingIS_global_old2new,&nodeNumbering_global_old2new);
1472
1473 ISDestroy(&nodePartitioningIS_new);
1474 ISDestroy(&nodeNumberingIS_subdomain_old2new);
1475 ISDestroy(&nodeNumberingIS_global_old2new);
1476
1477 ISRestoreIndices(elementNumberingIS_global_new2old,&elementNumbering_global_new2old);
1478
1479 ISDestroy(&elementNumberingIS_subdomain_new2old);
1480 ISDestroy(&elementNumberingIS_global_new2old);
1481
1482 ISRestoreIndices(elementBoundaryNumberingIS_global_new2old,&elementBoundaryNumbering_global_new2old);
1483
1484 ISDestroy(&elementBoundaryNumberingIS_subdomain_new2old);
1485 ISDestroy(&elementBoundaryNumberingIS_global_new2old);
1486
1487 ISRestoreIndices(edgeNumberingIS_global_new2old,&edgeNumbering_global_new2old);
1488
1489 ISDestroy(&edgeNumberingIS_subdomain_new2old);
1490 ISDestroy(&edgeNumberingIS_global_new2old);
1491
1492 return 0;
1493}
1494
1495namespace {
1496 //Reopening the mappings file read-only.
1497 //
1498 //The numbering datasets are written collectively through the MPI-IO driver
1499 //and then reopened read-only by every rank. Nothing used to check the
1500 //reopen: on Carpenter at >=384 ranks some ranks saw
1501 //
1502 // H5F__super_read(): truncated file:
1503 // eof = 115343360, sblock->base_addr = 0, stored_eof = 116070216
1504 //
1505 //(93 of 1536 in one run), H5Dopen1 returned -1, H5Dread failed on an invalid
1506 //id, and the ZERO-initialised destination valarray became the numbering --
1507 //every old id mapping to new id 0. That is the origin of the degenerate
1508 //face numbering and of edgeNodesArray left at its -1 initialiser.
1509 //
1510 //Closing the writer before every reopen (beb3ba81) is necessary but NOT
1511 //sufficient. A standalone reproduction on Carpenter (HDF5 1.14.6, Lustre,
1512 //7680 ranks on 40 nodes, production-sized datasets; torino_flume
1513 //debug/stale_eof) showed what the failures depend on:
1514 // - NOT time: waiting 5, 50 or 500 ms after the barrier fails as often as
1515 // not waiting, and a stat() right after a failed open always shows the
1516 // correct size;
1517 // - concurrency on one node: a node's first opener never failed, while
1518 // ranks opening at the same moment as many others on their node did
1519 // (up to ~100 of 7680 per reopen);
1520 // - one real H5Fopen per node first fixes it: with the node leader opening
1521 // before the rest, 4 failures in 61,440 opens. A leader that only
1522 // fstat()s the file and sees the right size does not help.
1523 //The likely cause is the Lustre client handing some of many concurrent
1524 //openers its stale cached size for a file another client just changed.
1525 //
1526 //So the reopen is LOCAL and node-leader-first: each rank opens the file
1527 //with HDF5's default (sec2) driver, one rank per node before the others,
1528 //and a failed open is retried with a short jittered backoff. Reads are
1529 //ordinary per-rank POSIX reads whose sieve buffer coalesces the point
1530 //selections, and the close is not collective, so one slow rank no longer
1531 //holds every other rank in an MPI_File_close barrier. Same build, mesh and
1532 //ranks (7680, 14.7M nodes): "Collecting elements" 704 s vs 1215 s with the
1533 //world MPI-IO reopen, partitioning 882 s vs 1426 s; one MPI-IO run took
1534 //8981 s when a single straggler rank held the other 7679 in that barrier.
1535 //
1536 //PROTEUS_MAPPINGS_READ_DRIVER=mpio restores the world MPI-IO reopen
1537 //(collective, not retried) as a fallback.
1538 bool mappingsReadUsesMPIIO()
1539 {
1540 static const bool use_mpio = []() {
1541 const char* driver = std::getenv("PROTEUS_MAPPINGS_READ_DRIVER");
1542 return driver != NULL && std::string(driver) == "mpio";
1543 }();
1544 return use_mpio;
1545 }
1546
1547 //Transfer mode for a read from a file opened by openMappingsReadOnly. HDF5
1548 //rejects collective transfer on a non-MPI driver ("collective access for
1549 //MPI-based drivers only"), so collective is only requested on the MPI-IO
1550 //path; on the local path the read is an ordinary independent read.
1551 herr_t setMappingsReadXfer(hid_t dxpl)
1552 {
1553 if (mappingsReadUsesMPIIO())
1554 return H5Pset_dxpl_mpio(dxpl, H5FD_MPIO_COLLECTIVE);
1555 return 0;
1556 }
1557
1558 //Ranks sharing this rank's node (and its Lustre client), split once.
1559 MPI_Comm mappingsNodeComm(const MPI_Comm& comm)
1560 {
1561 static MPI_Comm node = MPI_COMM_NULL;
1562 static MPI_Comm split_from = MPI_COMM_NULL;
1563 if (node == MPI_COMM_NULL || split_from != comm)
1564 {
1565 int rank = 0;
1566 MPI_Comm_rank(comm, &rank);
1567 MPI_Comm_split_type(comm, MPI_COMM_TYPE_SHARED, rank, MPI_INFO_NULL, &node);
1568 split_from = comm;
1569 }
1570 return node;
1571 }
1572
1573 //A local read-only open, retried: 10 quick attempts with 10-60 ms of
1574 //jitter (the failure is a collision with the node's other openers, not a
1575 //window that closes with time), then up to 5 more 1 s apart. Returns the
1576 //file id or -1, and the number of attempts used.
1577 hid_t openLocalWithRetry(const char* name, hid_t fapl, int rank, int& attempts)
1578 {
1579 unsigned int seed = 2654435761u * (unsigned int)(rank + 1);
1580 const int quick = 10, slow = 5;
1581 hid_t fid = -1;
1582 attempts = 0;
1583 while (attempts < quick + slow)
1584 {
1585 H5E_BEGIN_TRY {
1586 fid = H5Fopen(name, H5F_ACC_RDONLY, fapl);
1587 } H5E_END_TRY;
1588 attempts++;
1589 if (fid >= 0 || attempts == quick + slow)
1590 break;
1591 if (attempts < quick)
1592 usleep(10000 + rand_r(&seed) % 50000);
1593 else
1594 sleep(1);
1595 }
1596 return fid;
1597 }
1598
1599 //A rank that exhausts its retries writes what it saw to
1600 //mappings_open_fail.<rank>.txt before the caller aborts: the abort can
1601 //drop that rank's stderr (it did on Carpenter, job 3180113).
1602 void reportMappingsOpenFailure(int rank, const char* name, int attempts)
1603 {
1604 char path[64];
1605 snprintf(path, sizeof(path), "mappings_open_fail.%d.txt", rank);
1606 FILE* f = fopen(path, "w");
1607 if (f == NULL)
1608 f = stderr;
1609 struct stat st;
1610 int sr = stat(name, &st);
1611 fprintf(f, "rank %d: H5Fopen(%s, RDONLY) failed %d times\n", rank, name, attempts);
1612 if (sr == 0)
1613 fprintf(f, "stat: st_size %lld\n", (long long)st.st_size);
1614 else
1615 fprintf(f, "stat failed: %s\n", strerror(errno));
1616 H5Eprint2(H5E_DEFAULT, f);
1617 if (f != stderr)
1618 fclose(f);
1619 }
1620
1621 //Collective: every rank must call this (barrier and the retry count reduction).
1622 hid_t openMappingsReadOnly(const MPI_Comm& comm, const char* name)
1623 {
1624 const bool use_mpio = mappingsReadUsesMPIIO();
1625 int rank = 0;
1626 MPI_Comm_rank(comm, &rank);
1627 static bool reported = false;
1628 if (!reported)
1629 {
1630 if (use_mpio)
1631 logEvent("mappings file read-only reopen: MPI-IO driver (PROTEUS_MAPPINGS_READ_DRIVER=mpio)",3);
1632 else
1633 logEvent("mappings file read-only reopen: local sec2 driver",3);
1634 reported = true;
1635 }
1636 MPI_Barrier(comm);
1637 hid_t fapl = H5Pcreate(H5P_FILE_ACCESS);
1638 if (fapl < 0) return fapl;
1639 if (use_mpio)
1640 H5Pset_fapl_mpio(fapl, comm, MPI_INFO_NULL);
1641 hid_t fid = -1;
1642 int attempts = 1;
1643 int node_rank = 0;
1644 if (use_mpio)
1645 {
1646 //the world MPI-IO open is collective and must not be retried per rank
1647 H5E_BEGIN_TRY {
1648 fid = H5Fopen(name, H5F_ACC_RDONLY, fapl);
1649 } H5E_END_TRY;
1650 }
1651 else
1652 {
1653 //node leader first, then the rest of the node
1654 MPI_Comm node = mappingsNodeComm(comm);
1655 MPI_Comm_rank(node, &node_rank);
1656 if (node_rank == 0)
1657 fid = openLocalWithRetry(name, fapl, rank, attempts);
1658 MPI_Barrier(node);
1659 if (node_rank != 0)
1660 fid = openLocalWithRetry(name, fapl, rank, attempts);
1661 }
1662 if (fid < 0)
1663 {
1664 //reopen once more with error reporting on, to capture the HDF5 stack
1665 fid = H5Fopen(name, H5F_ACC_RDONLY, fapl);
1666 if (fid < 0)
1667 reportMappingsOpenFailure(rank, name, attempts + 1);
1668 }
1669 H5Pclose(fapl);
1670 int counts[4] = {(attempts > 1 && node_rank == 0) ? 1 : 0,
1671 (attempts > 1 && node_rank != 0) ? 1 : 0,
1672 (fid < 0) ? 1 : 0,
1673 attempts};
1674 int totals[3] = {0, 0, 0}, maxAttempts = 0;
1675 MPI_Allreduce(counts, totals, 3, MPI_INT, MPI_SUM, comm);
1676 MPI_Allreduce(&counts[3], &maxAttempts, 1, MPI_INT, MPI_MAX, comm);
1677 if (totals[0] + totals[1] + totals[2] > 0)
1678 {
1679 char msg[256];
1680 snprintf(msg, sizeof(msg), "mappings file reopen: %d node leader(s) and %d other rank(s) needed a retry (max %d attempts), %d failed (stale size on a parallel filesystem)",
1681 totals[0], totals[1], maxAttempts, totals[2]);
1682 logEvent(msg,3);
1683 }
1684 return fid;
1685 }
1686}
1687
1688int partitionNodesFromTetgenFiles(const MPI_Comm& PROTEUS_COMM_WORLD, const char* filebase, int indexBase,
1689 Mesh& newMesh, int nNodes_overlap, double memHardLimit)
1690{
1691 using namespace std;
1692 PetscErrorCode ierr;
1693 PetscMPIInt size,rank;
1694
1695 ierr = MPI_Comm_size(PROTEUS_COMM_WORLD,&size);CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
1696 ierr = MPI_Comm_rank(PROTEUS_COMM_WORLD,&rank);CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
1697 //cek todo: decide whether to remove PETSc logging and just use proteus logging
1698 PetscLogStage partitioning_stage;
1699 PetscLogStageRegister("Mesh Partition",&partitioning_stage);
1700 PetscLogStagePush(partitioning_stage);
1701
1702 /***********************************************************************
1703 partition domain based on the nodes without reading in the global mesh.
1704
1705 1. Partition nodes in the default partition of contiguous chunks with
1706 input ordering
1707
1708 2. Determine nodal connectivity on local processor
1709
1710 3. Generate new nodal partition using PETSc interface
1711
1712 4. Collect subdomain elements: any that contain owned nodes; tag ownership
1713
1714 4a,b. Collect element boundaries and edges on the subdomain elements and tag ownership
1715
1716 5. Generate global element, element boundary, and edge numbering for new subdomain ownership
1717
1718 6. Create overlap (ghost) information for nodes and elements
1719
1720 7. March through additional layers of overlap if requested,
1721
1722 8. Build subdomain meshes in new numbering
1723 ***********************************************************************/
1724 //
1725 //0. Set up tetgen files. Note, tetgen should have been run with -feen to get alll the faces and elements
1726 //
1727 bool failed = false;
1728 const int simplexDim = 4;
1729 const int vertexDim = 3;
1730 using namespace IOutils;
1731 std::string vertexFileName = std::string(filebase) + ".node" ;
1732 std::string elementFileName = std::string(filebase) + ".ele" ;
1733 std::string elementBoundaryFileName = std::string(filebase) + ".face" ;
1734 std::string edgeFileName = std::string(filebase) + ".edge" ;
1735 //
1736 //1. Build default nodal partition
1737 //
1738 //compute offsets to build subdomain2global ordering for nodes
1739 //in default partitioning of the domain into subdomains. All
1740 //we need is the number of nodes in the global mesh
1741 //
1742 //read nodes for tetgen format
1743 //first just read the number of nodes and whether or not there are node tags
1744 logEvent("Building default partitioning",5);
1745 int read_elements_event;
1746 PetscLogEventRegister("Read eles",0,&read_elements_event);
1747 PetscLogEventBegin(read_elements_event,0,0,0,0);
1748 std::ifstream vertexFile(vertexFileName.c_str());
1749 if (!vertexFile.good())
1750 {
1751 std::cerr<<"cannot open Tetgen node file "
1752 <<vertexFileName<<std::endl;
1753 failed = true;
1754 return failed;
1755 }
1756 int hasVertexMarkers(0),hasVertexAttributes(0),nSpace(3),nNodes_global;
1757 //read number of vertices and whether node flags are provided
1758 vertexFile >> eatcomments >> nNodes_global >> nSpace >> hasVertexAttributes >> hasVertexMarkers >> eatline ;
1759 assert(nNodes_global > 0);
1760 assert(nSpace == 3);
1761 newMesh.nNodes_global = nNodes_global;
1762 newMesh.nNodes_element = simplexDim;
1763 newMesh.nNodes_elementBoundary = simplexDim-1;
1764 newMesh.nElementBoundaries_element = simplexDim;
1765 //cek todo: review the situation with tetgen and triangle atribs and markers
1766 if (hasVertexAttributes > 0 && rank==0)
1767 {
1768 std::cerr<<"WARNING Tetgen nodes hasAttributes= "<<hasVertexAttributes
1769 <<" > 0 will treat first value as integer id for boundary!!"<<std::endl;
1770 hasVertexMarkers = 1;
1771 }
1772 //don't need to read anymore from .nodes for now
1773 //leaving file open because we'll pick up here to read the node coordinates
1774 //after partioning, which just requires topology information
1775 //
1776 //offsets provide the lower and upper bounds for the global numbering
1777 //first we just partition the nodes among the MPI ranks, approximately equally, ignoring connectivity
1778 valarray<int> nodeOffsets_old(size+1);
1779 nodeOffsets_old[0] = 0;
1780 for (int sdN=0; sdN < size; sdN++)
1781 {
1782 nodeOffsets_old[sdN+1] = nodeOffsets_old[sdN] +
1783 int(nNodes_global)/size + (int(nNodes_global)%size > sdN);
1784 }
1785 int nNodes_subdomain_old = nodeOffsets_old[rank+1] - nodeOffsets_old[rank];
1786 int nNodes_subdomain_max = int(nNodes_global)/size + (int(nNodes_global)%size > 0);
1787 char log_buffer[100];
1788 sprintf(log_buffer,"First partitioning: Max nNodes_subdomain %d nNodes_global %d",nNodes_subdomain_max,nNodes_global);
1789 logEvent(log_buffer,5);
1790 //
1791 //2. Determine nodal connectivity (nodeStarArray) for nodes on subdomain
1792 //
1793 //connectivty commes from the topology (elements) file. We just grab the elements
1794 //that contain currently owned nodes
1795 logEvent("Determining nodal connectivity to construct new partitioning",5);
1796 std::ifstream elementFile(elementFileName.c_str());
1797 if (!elementFile.good())
1798 {
1799 std::cerr<<"cannot open Tetgen element file "
1800 <<elementFileName<<std::endl;
1801 failed = true;
1802 return failed;
1803 }
1804 //read elements
1805 int nNodesPerSimplex(simplexDim),hasElementMarkers = 0,nElements_global;
1806 elementFile >> eatcomments >> nElements_global >> nNodesPerSimplex >> hasElementMarkers >> eatline;
1807 assert(nElements_global > 0);
1808 assert(nNodesPerSimplex == simplexDim);
1809 newMesh.nElements_global = nElements_global;
1810 valarray<int> element_nodes_old(4);
1811 vector<set<int>> nodeStar(nNodes_subdomain_old);
1812 map<int,valarray<int>> elements_old;
1813 for (int ie = 0; ie < nElements_global; ie++)
1814 {
1815 int ne, nv;
1816 elementFile >> eatcomments >> ne;
1817 ne -= indexBase;
1818 assert(0 <= ne && ne < nElements_global && elementFile.good());
1819 for (int iv = 0; iv < simplexDim; iv++)
1820 {
1821 elementFile >> nv;
1822 nv -= indexBase;
1823 assert(0 <= nv && nv < nNodes_global);
1824 element_nodes_old[iv] = nv;
1825 }
1826 //for each node on the element
1827 for (int iv = 0; iv < simplexDim; iv++)
1828 {
1829 //check if the node is owned by the subdomain
1830 int nN_star = element_nodes_old[iv];
1831 bool inSubdomain=false;
1832 if (nN_star >= nodeOffsets_old[rank] && nN_star < nodeOffsets_old[rank+1])
1833 {
1834 inSubdomain = true;
1835 for (int jv = 0; jv < simplexDim; jv++)
1836 {
1837 if (iv != jv)
1838 {
1839 int nN_star_subdomain = nN_star-nodeOffsets_old[rank];
1840 nodeStar[nN_star_subdomain].insert(element_nodes_old[jv]);
1841 }
1842 }
1843 }
1844 if (inSubdomain)
1845 elements_old[ie] = element_nodes_old;
1846 }
1847 elementFile >> eatline;
1848 }//end ie
1849 elements_old.clear();
1850 elementFile.close();
1851 //cek todo: track c++ containers and put inside scope to reduce dynamic memory usage
1852 PetscLogEventEnd(read_elements_event,0,0,0,0);
1853 int repartition_nodes_event;
1854 PetscLogEventRegister("Repart nodes",0,&repartition_nodes_event);
1855 PetscLogEventBegin(repartition_nodes_event,0,0,0,0);
1856 //done reading element file for first time; will need to read again after node partitioning
1857 //build compact data structure for nodeStar, and
1858 //find maximum number of nodes in any star
1859 int max_nNodeNeighbors_node=0;
1860 for (int nN=0;nN<nNodes_subdomain_old;nN++)
1861 max_nNodeNeighbors_node=max(max_nNodeNeighbors_node,static_cast<int>(nodeStar[nN].size()));
1862 //build connectivity data structures for PETSc
1863 PetscBool isInitialized;
1864 PetscInitialized(&isInitialized);
1865 PetscInt *nodeNeighborsOffsets_subdomain,*nodeNeighbors_subdomain,*weights_subdomain,*vertex_weights_subdomain;
1866 PetscReal *partition_weights;
1867 PetscMalloc(sizeof(PetscInt)*(nNodes_subdomain_old+1),&nodeNeighborsOffsets_subdomain);
1868 PetscMalloc(sizeof(PetscInt)*(nNodes_subdomain_old*max_nNodeNeighbors_node),&nodeNeighbors_subdomain);
1869 PetscMalloc(sizeof(PetscInt)*(nNodes_subdomain_old*max_nNodeNeighbors_node),&weights_subdomain);
1870 PetscMalloc(sizeof(PetscInt)*(nNodes_subdomain_old),&vertex_weights_subdomain);
1871 PetscMalloc(sizeof(PetscReal)*(size),&partition_weights);
1872 for (int sd=0;sd<size;sd++)
1873 partition_weights[sd] = 1.0/double(size);
1874 nodeNeighborsOffsets_subdomain[0] = 0;
1875 bool test=false;
1876 for (int nN = 0,offset=0; nN < nNodes_subdomain_old; nN++)
1877 {
1878 for (auto nN_star=nodeStar[nN].begin(); nN_star!=nodeStar[nN].end(); nN_star++,offset++)
1879 {
1880 nodeNeighbors_subdomain[offset] = *nN_star;
1881 }
1882 nodeNeighborsOffsets_subdomain[nN+1]=offset;
1883 if (test)//make sure nodeNeighbors are sorted in ascending order ()
1884 {
1885 valarray<int> neighbors(nodeNeighborsOffsets_subdomain[nN+1]-nodeNeighborsOffsets_subdomain[nN]);
1886 for (int i=0;i<neighbors.size();i++)
1887 neighbors[i] = nodeNeighbors_subdomain[nodeNeighborsOffsets_subdomain[nN]+i];
1888 sort(&nodeNeighbors_subdomain[nodeNeighborsOffsets_subdomain[nN]],&nodeNeighbors_subdomain[nodeNeighborsOffsets_subdomain[nN+1]]);
1889 for (int i=0;i<neighbors.size();i++)
1890 assert(neighbors[i] == nodeNeighbors_subdomain[nodeNeighborsOffsets_subdomain[nN]+i]);
1891 }
1892 //weight nodes by size of star
1893 int weight= (nodeNeighborsOffsets_subdomain[nN+1] - nodeNeighborsOffsets_subdomain[nN]);
1894 vertex_weights_subdomain[nN] = weight;
1895 for (int k=nodeNeighborsOffsets_subdomain[nN];k<nodeNeighborsOffsets_subdomain[nN+1];k++)
1896 weights_subdomain[k] = weight;
1897 }
1898 nodeStar.clear();
1899 //
1900 //3. Generate new nodal partition using PETSc interface to graph partitioners
1901 //
1902 logEvent("Constructing new nodal partition",5);
1903 Mat petscAdjacency;
1904 ierr = MatCreateMPIAdj(PROTEUS_COMM_WORLD,
1905 nNodes_subdomain_old,
1906 nNodes_global,
1907 nodeNeighborsOffsets_subdomain,
1908 nodeNeighbors_subdomain,
1909 weights_subdomain,
1910 &petscAdjacency);CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
1911 const double max_rss_gb(memHardLimit/1024.0);
1912 ierr = enforceMemoryLimit(PROTEUS_COMM_WORLD, rank, max_rss_gb,"Done allocating MPIAdj");CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
1913 MatPartitioning petscPartition;
1914 ierr = MatPartitioningCreate(PROTEUS_COMM_WORLD,&petscPartition);CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
1915 ierr = MatPartitioningSetAdjacency(petscPartition,petscAdjacency);CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
1916 ierr = MatPartitioningSetFromOptions(petscPartition);CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
1917 ierr = MatPartitioningSetVertexWeights(petscPartition,vertex_weights_subdomain);CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
1918 ierr = MatPartitioningSetPartitionWeights(petscPartition,partition_weights);CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
1919 //get petsc index set that has the new subdomain number for each node
1920 IS nodePartitioningIS_new;
1921 ierr = MatPartitioningApply(petscPartition,&nodePartitioningIS_new);CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
1922 ierr = MatPartitioningDestroy(&petscPartition);CHKERRABORT(PROTEUS_COMM_WORLD, ierr); //gets petscAdjacency too I believe
1923 ierr = enforceMemoryLimit(PROTEUS_COMM_WORLD, rank, max_rss_gb,"Done applying partition");CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
1924
1925 //determine the number of nodes per subdomain in new partitioning
1926 valarray<int> nNodes_subdomain_new(size);
1927 ISPartitioningCount(nodePartitioningIS_new,size,&nNodes_subdomain_new[0]);
1928 //need new offsets for subdomain to global numbering
1929 valarray<int> nodeOffsets_new(size+1);
1930 nodeOffsets_new[0] = 0;
1931 nNodes_subdomain_max = 0;
1932 for (int sdN = 0; sdN < size; sdN++)
1933 {
1934 nodeOffsets_new[sdN+1] = nodeOffsets_new[sdN] + nNodes_subdomain_new[sdN];
1935 nNodes_subdomain_max=max(nNodes_subdomain_max, nNodes_subdomain_new[sdN]);
1936 }
1937 sprintf(log_buffer,"Final partitioning: Max nNodes_subdomain %d nNodes_global %d",nNodes_subdomain_max,nNodes_global);
1938 logEvent(log_buffer,5);
1939 //get the new node numbers for nodes on this subdomain
1940 IS nodeNumberingIS_subdomain_old2new;
1941 ISPartitioningToNumbering(nodePartitioningIS_new,&nodeNumberingIS_subdomain_old2new);
1942 ierr=ISDestroy(&nodePartitioningIS_new);CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
1943 const PetscInt* nodes_subdomain_old2new_array;
1944 ISGetIndices(nodeNumberingIS_subdomain_old2new, &nodes_subdomain_old2new_array);
1945 //
1946 // out of core creation of global old2new mapping
1947 //
1948 //create the mappings file
1949 logEvent("Writing/reading node numberings to hdf5",5);
1950 hid_t mappings_plist_id = H5Pcreate(H5P_FILE_ACCESS);
1951 H5Pset_fapl_mpio(mappings_plist_id, PROTEUS_COMM_WORLD, MPI_INFO_NULL);
1952 const char* H5FILE_NAME("mappings.h5");
1953 hid_t file_id = H5Fcreate(H5FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT, mappings_plist_id);
1954 assert(file_id != H5I_INVALID_HID);
1955 H5Pclose(mappings_plist_id);
1956 //
1957 //write old2new
1958 //
1959 hsize_t nodes_global_count[]={static_cast<hsize_t>(nNodes_global)};
1960 const hsize_t ARRAY_RANK(1);
1961 hid_t nodes_old2new_filespace_id = H5Screate_simple(ARRAY_RANK, nodes_global_count, NULL);
1962 hid_t nodes_old2new_dataset_id = H5Dcreate(file_id, "nodeNumbering_old2new", H5T_NATIVE_INT,
1963 nodes_old2new_filespace_id,
1964 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
1965 hsize_t nodes_subdomain_count[]={static_cast<hsize_t>(nNodes_subdomain_old)};
1966 hsize_t nodes_subdomain_offset[]={static_cast<hsize_t>(nodeOffsets_old[rank])};
1967 hid_t nodes_old2new_memspace_id = H5Screate_simple(ARRAY_RANK, nodes_subdomain_count, NULL);
1968 H5Sselect_hyperslab(nodes_old2new_filespace_id, H5S_SELECT_SET,
1969 nodes_subdomain_offset, NULL,
1970 nodes_subdomain_count, NULL);
1971 hid_t nodes_old2new_plist_id = H5Pcreate(H5P_DATASET_XFER);
1972 herr_t status = H5Pset_dxpl_mpio(nodes_old2new_plist_id, H5FD_MPIO_COLLECTIVE);
1973 status = H5Dwrite(nodes_old2new_dataset_id, H5T_NATIVE_INT,
1974 nodes_old2new_memspace_id, nodes_old2new_filespace_id,
1975 nodes_old2new_plist_id, nodes_subdomain_old2new_array);
1976 H5Pclose(nodes_old2new_plist_id);
1977 H5Dclose(nodes_old2new_dataset_id);
1978 H5Sclose(nodes_old2new_memspace_id);
1979 H5Sclose(nodes_old2new_filespace_id);
1980 //
1981 //write new2old
1982 //
1983 hid_t nodes_new2old_filespace_id = H5Screate_simple(ARRAY_RANK, nodes_global_count, NULL);
1984 hid_t nodes_new2old_dataset_id = H5Dcreate(file_id, "nodeNumbering_new2old", H5T_NATIVE_INT,
1985 nodes_new2old_filespace_id,
1986 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
1987 hid_t nodes_new2old_memspace_id = H5Screate_simple(ARRAY_RANK, nodes_subdomain_count, NULL);
1988 //new2old is not a hyperslab, unlike old2new, so we have to use H5Sselect_elements
1989 //create a node list to write to for the global new2old mapping
1990 valarray<hsize_t> new_node_indices(nNodes_subdomain_old);
1991 valarray<int> old_node_indices(nNodes_subdomain_old);
1992 for (int i=0;i<nNodes_subdomain_old;i++)
1993 {
1994 new_node_indices[i] = static_cast<hsize_t>(nodes_subdomain_old2new_array[i]);//new node indices where we are writing to
1995 old_node_indices[i] = nodeOffsets_old[rank]+i;//old node numbers at those locations
1996 }
1997 status = H5Sselect_elements(nodes_new2old_filespace_id, H5S_SELECT_SET,
1998 nNodes_subdomain_old, &new_node_indices[0]);
1999 hid_t nodes_new2old_plist_id = H5Pcreate(H5P_DATASET_XFER);
2000 status = H5Pset_dxpl_mpio(nodes_new2old_plist_id, H5FD_MPIO_COLLECTIVE);
2001 status = H5Dwrite(nodes_new2old_dataset_id, H5T_NATIVE_INT,
2002 nodes_new2old_memspace_id, nodes_new2old_filespace_id,
2003 nodes_new2old_plist_id, &old_node_indices[0]);
2004 H5Pclose(nodes_new2old_plist_id);
2005 H5Dclose(nodes_new2old_dataset_id);
2006 H5Sclose(nodes_new2old_memspace_id);
2007 H5Sclose(nodes_new2old_filespace_id);
2008 H5Fclose(file_id);
2009 //
2010 //test out of core
2011 //
2012 IS nodeNumberingIS_global_old2new;
2013 const PetscInt *nodeNumbering_global_old2new;
2014 valarray<int> nodeNumbering_global_new2old;
2015 if (test)
2016 {
2017 ISAllGather(nodeNumberingIS_subdomain_old2new,&nodeNumberingIS_global_old2new);
2018 ISGetIndices(nodeNumberingIS_global_old2new,&nodeNumbering_global_old2new);
2019 nodeNumbering_global_new2old.resize(nNodes_global);
2020 for (int nN = 0; nN < nNodes_global; nN++)
2021 nodeNumbering_global_new2old[nodeNumbering_global_old2new[nN]] = nN;
2022 herr_t status;
2023 valarray<int> nodeNumbering_old2new_read(nNodes_global);
2024 file_id = openMappingsReadOnly(PROTEUS_COMM_WORLD, H5FILE_NAME);
2025 assert(file_id != H5I_INVALID_HID);
2026 hid_t nodeNumbering_old2new_dataset_id = H5Dopen1(file_id, "/nodeNumbering_old2new");
2027 status = H5Dread(nodeNumbering_old2new_dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT,
2028 &nodeNumbering_old2new_read[0]);
2029 status = H5Dclose(nodeNumbering_old2new_dataset_id);
2030 for (int i=0;i<nNodes_global;i++)
2031 {
2032 assert(nodeNumbering_global_old2new[i] == nodeNumbering_old2new_read[i]);
2033 }
2034 std::cout<<"==================out of core old2new nodes is correct!===================="<<std::endl;
2035 hid_t nodeNumbering_new2old_dataset_id = H5Dopen2(file_id, "/nodeNumbering_new2old", H5P_DEFAULT);
2036 valarray<int> nodeNumbering_new2old_read(nNodes_global);
2037 status = H5Dread(nodeNumbering_new2old_dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT,
2038 &nodeNumbering_new2old_read[0]);
2039 status = H5Dclose(nodeNumbering_new2old_dataset_id);
2040 H5Fclose(file_id);
2041 for (int i=0;i<nNodes_global;i++)
2042 {
2043 assert(nodeNumbering_global_new2old[i] == nodeNumbering_new2old_read[i]);
2044 }
2045 std::cout<<"==================out of core new2old nodes is correct!===================="<<std::endl;
2046 nodeNumbering_global_new2old.resize(0);
2047 }
2048 //cek todo: put this in the right place and use integers not doubles
2049 Vec nodeNumbering_old2new_petsc;
2050 ierr = VecCreateMPI(PROTEUS_COMM_WORLD, nNodes_subdomain_old, nNodes_global, &nodeNumbering_old2new_petsc);
2051 CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
2052 valarray<PetscInt> indices(nNodes_subdomain_old);
2053 valarray<PetscInt> ix(nNodes_subdomain_old);
2054 valarray<PetscScalar> y(nNodes_subdomain_old);
2055 for (int i=0;i<nNodes_subdomain_old;i++)
2056 {
2057 indices[i] = i+nodeOffsets_old[rank];
2058 ix[i]=i;
2059 y[i]=static_cast<PetscScalar>(nodes_subdomain_old2new_array[i]);
2060 }
2061 ISLocalToGlobalMapping mapping;
2062 PetscInt bs(1);
2063 ierr = ISLocalToGlobalMappingCreate(PROTEUS_COMM_WORLD, bs, nNodes_subdomain_old, &indices[0], PETSC_USE_POINTER, &mapping);
2064 CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
2065 ierr = VecSetLocalToGlobalMapping(nodeNumbering_old2new_petsc, mapping);
2066 CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
2067 ierr = VecSetValuesLocal(nodeNumbering_old2new_petsc, nNodes_subdomain_old, &ix[0], &y[0], INSERT_VALUES);
2068 CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
2069 ierr = VecAssemblyBegin(nodeNumbering_old2new_petsc);
2070 CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
2071 ierr = VecAssemblyEnd(nodeNumbering_old2new_petsc);
2072 CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
2073 if (test)
2074 {
2075 PetscScalar* test_local;
2076 VecGetArray(nodeNumbering_old2new_petsc,&test_local);
2077 for (int i=0;i<nNodes_subdomain_old;i++)
2078 {
2079 assert(test_local[i] == static_cast<PetscScalar>(nodes_subdomain_old2new_array[i]));
2080 }
2081 VecRestoreArray(nodeNumbering_old2new_petsc,&test_local);
2082 }
2083 //ISRestoreIndices(nodeNumberingIS_subdomain_old2new, &nodes_subdomain_old2new_array);
2084 //ierr=ISDestroy(&nodeNumberingIS_subdomain_old2new);CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
2085
2086 PetscLogEventEnd(repartition_nodes_event,0,0,0,0);
2087 int receive_element_mask_event;
2088 PetscLogEventRegister("Recv. ele mask",0,&receive_element_mask_event);
2089 PetscLogEventBegin(receive_element_mask_event,0,0,0,0);
2090 //
2091 //4. To build subdomain meshes, go through and collect elements containing
2092 // the locally owned nodes. Assign processor ownership of elements
2093 //
2094 logEvent("Collecting elements containing locally owned nodes (element support of the subdomain nodes)",5);
2095 PetscLogEventEnd(receive_element_mask_event,0,0,0,0);
2096 ierr = enforceMemoryLimit(PROTEUS_COMM_WORLD, rank, max_rss_gb,"Done with masks");CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
2097 int build_subdomains_reread_elements_event;
2098 PetscLogEventRegister("Reread eles",0,&build_subdomains_reread_elements_event);
2099 PetscLogEventBegin(build_subdomains_reread_elements_event,0,0,0,0);
2100 //
2101 //mark the unmarked elements on this subdomain and store element numbers (in old numbering)
2102 //
2103 //We need to find the elements in the support of nodes in the new
2104 //partitioning, so we'll need to re-read the elements file to get
2105 //the the elements for the nodes in the new partitioning. We will be collecting OLD element numbers.
2106 std::ifstream elementFile2(elementFileName.c_str());
2107 if (!elementFile2.good())
2108 {
2109 std::cerr<<"cannot open Tetgen elements file"
2110 <<elementFileName<<std::endl;
2111 failed = true;
2112 return failed;
2113 }
2114 elementFile2 >> eatcomments >> nElements_global >> nNodesPerSimplex >> hasElementMarkers >> eatline;
2115 assert(nElements_global > 0);
2116 assert(nNodesPerSimplex == simplexDim);
2117 //cek todo: check that these are all necessary
2118 set<int> elements_subdomain_owned;
2119 valarray<int> element_nodes_new(4);
2120 vector<set<int>> nodeElementsStar(nNodes_subdomain_new[rank]);
2121 vector<set<int>> nodeStarNew(nNodes_subdomain_new[rank]);
2122 map<int,valarray<int>> elementNodesArrayMap;
2123 map<int,long int> elementMaterialTypesMap;
2124 map<NodeTuple<3>,ElementNeighbors> elementBoundaryElementsMap;
2125 map<NodeTuple<2>,set<pair<int,int>>> edgeElementsMap;
2126 set<int> node_collection;
2127 vector<valarray<int>> element_old_nodes_collection;
2128 vector<int> elements_collection;
2129 vector<double> elementId_collection;
2130 map<int, int> nodes_old2new_subdomain_map;
2131 //note any element index containers are in the old element numbering
2132 int eN_c_start = 0;//start of the collection of elements we are currently processing
2133 //Chunk this re-read by a FIXED node-collection size. Sizing the chunk as
2134 //nElements_global/size makes the NUMBER of chunks equal the number of ranks,
2135 //so every rank added costs another full read cycle, and each cycle has more
2136 //participants. Restored from 77c99396^.
2137 const int nNodes_collection_max=10000;
2138 //Hold the mappings file open across the whole loop. Opening and closing it
2139 //once per chunk had every rank open the same Lustre file on every chunk.
2140 file_id = openMappingsReadOnly(PROTEUS_COMM_WORLD, H5FILE_NAME);
2141 if (file_id < 0)
2142 {
2143 H5Eprint2(H5E_DEFAULT, stderr);
2144 SETERRABORT(PROTEUS_COMM_WORLD, PETSC_ERR_FILE_OPEN,
2145 "could not reopen the mappings file; see the HDF5 stack above");
2146 }
2147 assert(file_id != H5I_INVALID_HID);
2148 for (int ie = 0; ie < nElements_global; ie++)
2149 {
2150 //
2151 //Accumulate a collection of elements in the old numbering of sufficient length,
2152 //then get the new node numbers for those old nodes, renumber, take ownership of elements,
2153 //and then release the memory for the collection and continue
2154 //basic idea is to use only a part of old2new at a time.
2155 //doing with hdf5 for now, but could do with scatter
2156 //
2157 int ne, nv, elementId(0);
2158 long double elementId_double;
2159 elementFile2 >> eatcomments >> ne;
2160 ne -= indexBase;
2161 assert(0 <= ne && ne < nElements_global && elementFile.good());
2162 elements_collection.push_back(ne);
2163 for (int iv = 0; iv < simplexDim; iv++)
2164 {
2165 elementFile2 >> nv ;
2166 nv -= indexBase;
2167 assert(0 <= nv && nv < nNodes_global);
2168 node_collection.insert(nv);
2169 element_nodes_old[iv] = nv;
2170 }
2171 if (hasElementMarkers > 0)
2172 {
2173 elementFile2 >> elementId_double;
2174 elementId_collection.push_back(elementId_double);
2175 }
2176 element_old_nodes_collection.push_back(element_nodes_old);
2177 if (node_collection.size() >= nNodes_collection_max || ie == nElements_global-1)
2178 {
2179 //create a node list to read from old2new mapping
2180 valarray<int> nodes_old2new_subset(node_collection.size());
2181 map<int,int> nodes_old2new_subset_map;
2182 bool test_scatter=false;
2183 if (test_scatter)
2184 {
2185 Vec node_old2new_collection_petsc;
2186 ierr = VecCreateMPI(PROTEUS_COMM_WORLD, node_collection.size(), PETSC_DETERMINE,&node_old2new_collection_petsc);
2187 CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
2188 PetscInt collection_low, collection_high;
2189 ierr = VecGetOwnershipRange(node_old2new_collection_petsc, &collection_low, &collection_high);
2190 CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
2191 valarray<PetscInt> node_collection_array(node_collection.size());
2192 valarray<PetscInt> iy_collection(node_collection.size());
2193 int i_nc=0;
2194 for (auto nv=node_collection.begin();nv!=node_collection.end();nv++,i_nc++)
2195 {
2196 node_collection_array[i_nc] = static_cast<PetscInt>(*nv);
2197 iy_collection[i_nc] = i_nc + collection_low;
2198 }
2199 IS old_ix,iy;
2200 ierr = ISCreateGeneral(PROTEUS_COMM_WORLD, node_collection.size(), &node_collection_array[0],
2201 PETSC_USE_POINTER, &old_ix);
2202 CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
2203 ierr = ISCreateGeneral(PROTEUS_COMM_WORLD, node_collection.size(), &iy_collection[0],
2204 PETSC_USE_POINTER, &iy);
2205 CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
2206 VecScatter old2new_collection_scatter;
2207 ierr = VecScatterCreate(nodeNumbering_old2new_petsc, old_ix,
2208 node_old2new_collection_petsc, iy,
2209 &old2new_collection_scatter);
2210 CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
2211 ierr = VecScatterBegin(old2new_collection_scatter,
2212 nodeNumbering_old2new_petsc, node_old2new_collection_petsc,
2213 INSERT_VALUES, SCATTER_FORWARD);
2214 CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
2215
2216 ierr = VecScatterEnd(old2new_collection_scatter,
2217 nodeNumbering_old2new_petsc, node_old2new_collection_petsc,
2218 INSERT_VALUES, SCATTER_FORWARD);
2219 CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
2220 PetscScalar* nodes_old2new_subset_array;
2221 ierr = VecGetArray(node_old2new_collection_petsc, &nodes_old2new_subset_array);
2222 CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
2223 for (int i=0;i<node_collection.size();i++)
2224 {
2225 nodes_old2new_subset[i] = nodes_old2new_subset_array[i];
2226 }
2227 ierr = VecRestoreArray(node_old2new_collection_petsc, &nodes_old2new_subset_array);
2228 CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
2229 ierr = VecDestroy(&node_old2new_collection_petsc);
2230 CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
2231 ierr = VecScatterDestroy(&old2new_collection_scatter);
2232 CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
2233 ierr = ISDestroy(&old_ix);
2234 CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
2235 ierr = ISDestroy(&iy);
2236 CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
2237 for (int i=0;i<node_collection.size();i++)
2238 {
2239 nodes_old2new_subset_map[node_collection_array[i]] = nodes_old2new_subset[i];
2240 }
2241 if (test)
2242 {
2243 for (int i=0;i<node_collection.size();i++)
2244 {
2245 assert(nodes_old2new_subset_map[node_collection_array[i]] == nodeNumbering_global_old2new[node_collection_array[i]]);
2246 }
2247 }
2248 }
2249 else
2250 {
2251 herr_t status;
2252 valarray<hsize_t> node_collection_array(node_collection.size());
2253 int i_nc=0;
2254 for (auto nv=node_collection.begin();nv!=node_collection.end();nv++,i_nc++)
2255 {
2256 node_collection_array[i_nc] = static_cast<hsize_t>(*nv);
2257 }
2258 nodes_old2new_dataset_id = H5Dopen1(file_id,"/nodeNumbering_old2new");
2259 nodes_old2new_filespace_id = H5Dget_space(nodes_old2new_dataset_id);
2260 status = H5Sselect_elements(nodes_old2new_filespace_id, H5S_SELECT_SET,
2261 node_collection.size(), &node_collection_array[0]);
2262 hsize_t dims[] = {static_cast<hsize_t>(node_collection.size())};
2263 hid_t nodes_old2new_subset_memspace_id = H5Screate_simple(1, dims, NULL);
2264 nodes_old2new_plist_id = H5Pcreate(H5P_DATASET_XFER);
2265#ifdef H5_HAVE_PARALLEL
2266 status = H5Pset_dxpl_mpio(nodes_old2new_plist_id, H5FD_MPIO_INDEPENDENT);
2267#endif
2268 status = H5Dread(nodes_old2new_dataset_id, H5T_NATIVE_INT,
2269 nodes_old2new_subset_memspace_id, nodes_old2new_filespace_id,
2270 H5P_DEFAULT, &nodes_old2new_subset[0]);
2271 H5Pclose(nodes_old2new_plist_id);
2272 H5Sclose(nodes_old2new_subset_memspace_id);
2273 H5Sclose(nodes_old2new_filespace_id);
2274 H5Dclose(nodes_old2new_dataset_id);
2275 for (int i=0;i<node_collection.size();i++)
2276 {
2277 nodes_old2new_subset_map[node_collection_array[i]] = nodes_old2new_subset[i];
2278 }
2279 if (test)
2280 {
2281 for (int i=0;i<node_collection.size();i++)
2282 {
2283 assert(nodes_old2new_subset_map[node_collection_array[i]] == nodeNumbering_global_old2new[node_collection_array[i]]);
2284 }
2285 }
2286 }
2287 for (int eN_c = eN_c_start; eN_c < ie+1;eN_c++)
2288 {
2289 ne = elements_collection[eN_c-eN_c_start];
2290 for (int iv = 0; iv < simplexDim; iv++)
2291 {
2292 element_nodes_old[iv] = element_old_nodes_collection[eN_c-eN_c_start][iv];
2293 element_nodes_new[iv] = nodes_old2new_subset_map[element_nodes_old[iv]];
2294 }
2295 NodeTuple<4> nodeTuple(&element_nodes_new[0]);
2296 for (int iv = 0; iv < simplexDim; iv++)
2297 {
2298 int nN_star_new = element_nodes_new[iv];
2299 bool inSubdomain=false;
2300 if (nN_star_new >= nodeOffsets_new[rank] && nN_star_new < nodeOffsets_new[rank+1])
2301 {
2302 inSubdomain = true;
2303 for (int iv=0; iv < simplexDim; iv++)
2304 nodes_old2new_subdomain_map[element_nodes_old[iv]] = element_nodes_new[iv];
2305 //add all the element boundaries of this element
2306 for (int ebN=0;ebN < 4 ; ebN++)
2307 {
2308 int nodes[3] = { element_nodes_new[(ebN+1) % 4],
2309 element_nodes_new[(ebN+2) % 4],
2310 element_nodes_new[(ebN+3) % 4]};
2311 NodeTuple<3> nodeTuple(nodes);
2312 if(elementBoundaryElementsMap.find(nodeTuple) != elementBoundaryElementsMap.end())
2313 {
2314 if (elementBoundaryElementsMap[nodeTuple].right == -1 && ne != elementBoundaryElementsMap[nodeTuple].left)
2315 {
2316 elementBoundaryElementsMap[nodeTuple].right=ne;
2317 elementBoundaryElementsMap[nodeTuple].right_ebN_element=ebN;
2318 }
2319 }
2320 else
2321 {
2322 elementBoundaryElementsMap[nodeTuple] = ElementNeighbors(ne,ebN);
2323 }
2324 }
2325 //add all the edges of this element
2326 for (int nNL=0,edN=0;nNL < 4 ; nNL++)
2327 for(int nNR=nNL+1;nNR < 4;nNR++,edN++)
2328 {
2329 int nodes[2] = { element_nodes_new[nNL],
2330 element_nodes_new[nNR]};
2331 NodeTuple<2> nodeTuple(nodes);
2332 edgeElementsMap[nodeTuple].insert(pair<int,int>(ne,edN));
2333 }
2334 //add all the nodes to the node star
2335 int nN_star_new_subdomain = nN_star_new - nodeOffsets_new[rank];
2336 nodeElementsStar[nN_star_new_subdomain].insert(ne);
2337 for (int jv = 0; jv < simplexDim; jv++)
2338 {
2339 if (iv != jv)
2340 {
2341 int nN_point_new = element_nodes_new[jv];
2342 nodeStarNew[nN_star_new_subdomain].insert(nN_point_new);
2343 }
2344 }
2345 }
2346 if (inSubdomain)
2347 {
2348 elementNodesArrayMap[ne] = element_nodes_new;
2349 }
2350 }
2351 //if this element contains any node owned by this subdomain, check node 1
2352 if (elementNodesArrayMap.find(ne) != elementNodesArrayMap.end())
2353 {
2354 if (nodeTuple.nodes[1] >= nodeOffsets_new[rank] && nodeTuple.nodes[1] < nodeOffsets_new[rank+1])
2355 elements_subdomain_owned.insert(ne);
2356 if (hasElementMarkers > 0)
2357 {
2358 elementId = static_cast<long int>(elementId_collection[eN_c-eN_c_start]);
2359 elementMaterialTypesMap[ne] = elementId;
2360 }
2361 }
2362 }
2363 eN_c_start = ie+1;
2364 node_collection.clear();
2365 elements_collection.clear();
2366 elementId_collection.clear();
2367 element_old_nodes_collection.clear();
2368 }
2369 elementFile2 >> eatline;
2370 }
2371 elementFile2.close();
2372 H5Fclose(file_id);
2373 int nElements_owned_subdomain(elements_subdomain_owned.size()),
2374 nElements_owned_new=0;
2375 MPI_Allreduce(&nElements_owned_subdomain,&nElements_owned_new,1,MPI_INT,MPI_SUM,PROTEUS_COMM_WORLD);
2376 assert(nElements_owned_new == nElements_global);
2377 PetscLogEventEnd(build_subdomains_reread_elements_event,0,0,0,0);
2378 int build_subdomains_send_marked_elements_event;
2379 PetscLogEventRegister("Mark/send eles",0,&build_subdomains_send_marked_elements_event);
2380 PetscLogEventBegin(build_subdomains_send_marked_elements_event,0,0,0,0);
2381 ierr = enforceMemoryLimit(PROTEUS_COMM_WORLD, rank, max_rss_gb,"Done marking elements");CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
2382 //cek todo: fix PETsc and memory log messages
2383 //
2384 //done with the element file
2385 //
2386 //construct compact nodeElementsArray
2387 valarray<int> nodeElementOffsets(nNodes_subdomain_new[rank]+1);
2388 nodeElementOffsets[0] = 0;
2389 for (int nN = 0; nN < nNodes_subdomain_new[rank]; nN++)
2390 nodeElementOffsets[nN+1] = nodeElementOffsets[nN]+nodeElementsStar[nN].size();
2391 valarray<int> nodeElementsArray(nodeElementOffsets[nNodes_subdomain_new[rank]]);
2392 for (int nN=0,offset=0; nN < nNodes_subdomain_new[rank]; nN++)
2393 {
2394 for (set<int>::iterator eN_star = nodeElementsStar[nN].begin(); eN_star != nodeElementsStar[nN].end();
2395 eN_star++,offset++)
2396 {
2397 nodeElementsArray[offset] = *eN_star;
2398 }
2399 }
2400 nodeElementsStar.clear();
2401 //construct compact nodeStarArray
2402 valarray<int> nodeStarOffsetsNew(nNodes_subdomain_new[rank]+1);
2403 nodeStarOffsetsNew[0] = 0;
2404 for (int nN=1;nN<nNodes_subdomain_new[rank]+1;nN++)
2405 nodeStarOffsetsNew[nN] = nodeStarOffsetsNew[nN-1] + nodeStarNew[nN-1].size();
2406 valarray<int> nodeStarArrayNew(nodeStarOffsetsNew[nNodes_subdomain_new[rank]]);
2407 for (int nN=0,offset=0;nN<nNodes_subdomain_new[rank];nN++)
2408 {
2409 for (set<int>::iterator nN_star=nodeStarNew[nN].begin();nN_star!=nodeStarNew[nN].end();nN_star++,offset++)
2410 {
2411 nodeStarArrayNew[offset] = *nN_star;
2412 }
2413 }
2414 nodeStarNew.clear();
2415 PetscLogEventEnd(build_subdomains_send_marked_elements_event,0,0,0,0);
2416 int build_subdomains_global_numbering_elements_event;
2417 PetscLogEventRegister("Global ele nmbr",0,&build_subdomains_global_numbering_elements_event);
2418 PetscLogEventBegin(build_subdomains_global_numbering_elements_event,0,0,0,0);
2419 //
2420 //5. Generate global element numbering corresponding to new subdomain ownership
2421 //
2422 logEvent("Generating global element numbering corresponding to new subdomain ownership",5);
2423 valarray<int> nElements_subdomain_new(size),
2424 elementOffsets_new(size+1);
2425 for (int sdN = 0; sdN < size; sdN++)
2426 {
2427 if (sdN == rank)
2428 nElements_subdomain_new[sdN] = int(elements_subdomain_owned.size());
2429 else
2430 nElements_subdomain_new[sdN] = 0;
2431 }
2432 valarray<int> nElements_subdomain_new_send = nElements_subdomain_new;
2433 MPI_Allreduce(&nElements_subdomain_new_send[0],&nElements_subdomain_new[0],size,MPI_INT,MPI_SUM,PROTEUS_COMM_WORLD);
2434 sprintf(log_buffer,"Final partitioning: Max nElements_subdomain %d nElements_global %d",nElements_subdomain_new.max(),nElements_global);
2435 logEvent(log_buffer,5);
2436
2437 //construct new offsets for elements
2438 elementOffsets_new[0] = 0;
2439 for (int sdN = 0; sdN < size; sdN++)
2440 elementOffsets_new[sdN+1] = elementOffsets_new[sdN] + nElements_subdomain_new[sdN];
2441 //map to old element numbering
2442 valarray<int> elementNumbering_subdomain_new2old(elements_subdomain_owned.size());
2443 set<int>::iterator eN_ownedp = elements_subdomain_owned.begin();
2444 map<int, int> elementNumbering_old2new_map;
2445 for (int eN = 0; eN < int(elements_subdomain_owned.size()); eN++,eN_ownedp++)
2446 {
2447 //only for owned elements on the subdomain
2448 elementNumbering_subdomain_new2old[eN] = *eN_ownedp;
2449 elementNumbering_old2new_map[*eN_ownedp] = eN;
2450 }
2451 //
2452 //write this subdomain to the mapping file
2453 //
2454 ierr = enforceMemoryLimit(PROTEUS_COMM_WORLD, rank, max_rss_gb,"Writing element numberings to hdf5");CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
2455 logEvent("Writing element numberings to hdf5",5);
2456 mappings_plist_id = H5Pcreate(H5P_FILE_ACCESS);
2457 status = H5Pset_fapl_mpio(mappings_plist_id, PROTEUS_COMM_WORLD, MPI_INFO_NULL);
2458 assert(status >= 0);
2459 file_id = H5Fopen(H5FILE_NAME, H5F_ACC_RDWR, mappings_plist_id);
2460 assert(file_id != H5I_INVALID_HID);
2461 H5Pclose(mappings_plist_id);
2462 hsize_t e_dims[]={static_cast<hsize_t>(nElements_global)};
2463 hid_t e_new2old_filespace_id = H5Screate_simple(ARRAY_RANK, e_dims, NULL);
2464 hid_t e_new2old_dataset_id = H5Dcreate(file_id, "elementNumbering_new2old", H5T_NATIVE_INT,
2465 e_new2old_filespace_id,
2466 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
2467 hsize_t e_count[]={static_cast<hsize_t>(nElements_subdomain_new[rank])};
2468 hsize_t e_offset[]={static_cast<hsize_t>(elementOffsets_new[rank])};
2469 hid_t e_new2old_memspace_id = H5Screate_simple(ARRAY_RANK, e_count, NULL);
2470 H5Sselect_hyperslab(e_new2old_filespace_id, H5S_SELECT_SET, e_offset, NULL, e_count, NULL);
2471 hid_t e_new2old_plist_id = H5Pcreate(H5P_DATASET_XFER);
2472 H5Pset_dxpl_mpio(e_new2old_plist_id, H5FD_MPIO_COLLECTIVE);
2473 status = H5Dwrite(e_new2old_dataset_id, H5T_NATIVE_INT,
2474 e_new2old_memspace_id, e_new2old_filespace_id,
2475 e_new2old_plist_id, &elementNumbering_subdomain_new2old[0]);
2476 H5Dflush(e_new2old_dataset_id);
2477 H5Pclose(e_new2old_plist_id);
2478 H5Dclose(e_new2old_dataset_id);
2479 H5Sclose(e_new2old_memspace_id);
2480 H5Sclose(e_new2old_filespace_id);
2481 valarray<hsize_t> old_element_indices(static_cast<hsize_t>(nElements_subdomain_new[rank]));
2482 valarray<int> new_element_indices(nElements_subdomain_new[rank]);
2483 ierr = enforceMemoryLimit(PROTEUS_COMM_WORLD, rank, max_rss_gb,"new2old done; now old2new");CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
2484 //do old2new map too, can't use hyperslab
2485 ierr = enforceMemoryLimit(PROTEUS_COMM_WORLD, rank, max_rss_gb,"old2new index valarrays done");CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
2486 for (int i=0;i<nElements_subdomain_new[rank];i++)
2487 {
2488 old_element_indices[i] = static_cast<hsize_t>(elementNumbering_subdomain_new2old[i]);
2489 new_element_indices[i] = elementOffsets_new[rank]+i;
2490 }
2491 hid_t e_old2new_filespace_id = H5Screate_simple(ARRAY_RANK, e_dims, NULL);
2492 hid_t e_old2new_dataset_id = H5Dcreate(file_id, "elementNumbering_old2new", H5T_NATIVE_INT,
2493 e_old2new_filespace_id,
2494 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
2495 hid_t e_old2new_memspace_id = H5Screate_simple(ARRAY_RANK, e_count, NULL);
2496 status = H5Sselect_elements(e_old2new_filespace_id, H5S_SELECT_SET,
2497 nElements_subdomain_new[rank], &old_element_indices[0]);
2498 hid_t e_old2new_plist_id = H5Pcreate(H5P_DATASET_XFER);
2499 H5Pset_dxpl_mpio(e_old2new_plist_id, H5FD_MPIO_COLLECTIVE);
2500 status = H5Dwrite(e_old2new_dataset_id, H5T_NATIVE_INT,
2501 e_old2new_memspace_id, e_old2new_filespace_id,
2502 e_old2new_plist_id, &new_element_indices[0]);
2503 H5Pclose(e_old2new_plist_id);
2504 H5Sclose(e_old2new_memspace_id);
2505 H5Dclose(e_old2new_dataset_id);
2506 H5Sclose(e_old2new_filespace_id);
2507 H5Fclose(file_id);
2508 //
2509 //now get maps for all the elements on the subdomain, not just owned elements
2510 //
2511 ierr = enforceMemoryLimit(PROTEUS_COMM_WORLD, rank, max_rss_gb,"Reading numberings from hdf5 to get subdomain map");CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
2512 logEvent("Reading element numberings from hdf5 to get subdomain map",5);
2513
2514 valarray<hsize_t> old_element_indices_subdomain(static_cast<hsize_t>(elementNodesArrayMap.size()));
2515 valarray<int> new_element_indices_subdomain(elementNodesArrayMap.size());
2516 int eN_old_subdomain = 0;
2517 for (auto it = elementNodesArrayMap.begin(); it != elementNodesArrayMap.end(); it++, eN_old_subdomain++)
2518 {
2519 old_element_indices_subdomain[eN_old_subdomain] = it->first;
2520 }
2521 file_id = openMappingsReadOnly(PROTEUS_COMM_WORLD, H5FILE_NAME);
2522 if (file_id < 0)
2523 {
2524 H5Eprint2(H5E_DEFAULT, stderr);
2525 SETERRABORT(PROTEUS_COMM_WORLD, PETSC_ERR_FILE_OPEN,
2526 "could not reopen the mappings file; see the HDF5 stack above");
2527 }
2528 e_old2new_dataset_id = H5Dopen1(file_id, "/elementNumbering_old2new");
2529 e_old2new_filespace_id = H5Dget_space(e_old2new_dataset_id);
2530 status = H5Sselect_elements(e_old2new_filespace_id, H5S_SELECT_SET,
2531 elementNodesArrayMap.size(), &old_element_indices_subdomain[0]);
2532 hsize_t e_subdomain_count[]={static_cast<hsize_t>(elementNodesArrayMap.size())};
2533 hid_t e_old2new_subdomain_memspace_id = H5Screate_simple(ARRAY_RANK, e_subdomain_count, NULL);
2534 hid_t e_old2new_subdomain_plist_id = H5Pcreate(H5P_DATASET_XFER);
2535 status = setMappingsReadXfer(e_old2new_subdomain_plist_id);
2536 status = H5Dread(e_old2new_dataset_id, H5T_NATIVE_INT,
2537 e_old2new_subdomain_memspace_id, e_old2new_filespace_id,
2538 H5P_DEFAULT, &new_element_indices_subdomain[0]);
2539 H5Pclose(e_old2new_subdomain_plist_id);
2540 H5Sclose(e_old2new_subdomain_memspace_id);
2541 H5Dclose(e_old2new_dataset_id);
2542 H5Sclose(e_old2new_filespace_id);
2543 H5Fclose(file_id);
2544 ierr = enforceMemoryLimit(PROTEUS_COMM_WORLD, rank, max_rss_gb,"Constructing element subdomain maps");CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
2545 map<int,int> elementNumbering_old2new_subdomain_map;
2546 map<int,int> elementNumbering_new2old_subdomain_map;
2547 for (int i=0;i<elementNodesArrayMap.size();i++)
2548 {
2549 elementNumbering_old2new_subdomain_map[old_element_indices_subdomain[i]] = new_element_indices_subdomain[i];
2550 elementNumbering_new2old_subdomain_map[new_element_indices_subdomain[i]] = old_element_indices_subdomain[i];
2551 }
2552 if (test)
2553 {
2554 //these were needed inside the last test section, which was in a loop, so release now
2555 ISRestoreIndices(nodeNumberingIS_global_old2new,&nodeNumbering_global_old2new);
2556 ierr=ISDestroy(&nodeNumberingIS_global_old2new);CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
2557 //set up global element numbering
2558 IS elementNumberingIS_subdomain_new2old;
2559 const PetscInt *elementNumbering_global_new2old;//needs to be restored
2560 valarray<int> elementNumbering_global_old2new(nElements_global);
2561 ISCreateGeneral(PROTEUS_COMM_WORLD,elements_subdomain_owned.size(),
2562 &elementNumbering_subdomain_new2old[0],PETSC_COPY_VALUES,
2563 &elementNumberingIS_subdomain_new2old);
2564 IS elementNumberingIS_global_new2old;
2565 ISAllGather(elementNumberingIS_subdomain_new2old,&elementNumberingIS_global_new2old);
2566 ISGetIndices(elementNumberingIS_global_new2old,&elementNumbering_global_new2old);
2567 //construct reverse mapping
2568 for (int eN = 0; eN < nElements_global; eN++)
2569 {
2570 elementNumbering_global_old2new[elementNumbering_global_new2old[eN]] = eN;
2571 }
2572 //check old2new
2573 valarray<int> elementNumbering_global_old2new_read(nElements_global);
2574 file_id = openMappingsReadOnly(PROTEUS_COMM_WORLD, H5FILE_NAME);
2575 hid_t e_old2new_dataset_id = H5Dopen1(file_id, "/elementNumbering_old2new");
2576 status = H5Dread(e_old2new_dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT,
2577 &elementNumbering_global_old2new_read[0]);
2578 status = H5Dclose(e_old2new_dataset_id);
2579 for (int i=0;i<nElements_global;i++)
2580 {
2581 assert(elementNumbering_global_old2new[i] == elementNumbering_global_old2new_read[i]);
2582 }
2583 std::cout<<"==================out of core elements old2new is correct!===================="<<std::endl;
2584 for (auto it = elementNodesArrayMap.begin(); it != elementNodesArrayMap.end(); it++)
2585 assert(elementNumbering_old2new_subdomain_map[it->first] ==
2586 elementNumbering_global_old2new_read[it->first]);
2587 std::cout<<"==================out of core elements old2new map is correct!===================="<<std::endl;
2588 //check new2old
2589 valarray<int> elementNumbering_global_new2old_read(nElements_global);
2590 hid_t e_new2old_dataset_id = H5Dopen1(file_id, "/elementNumbering_new2old");
2591 status = H5Dread(e_new2old_dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT,
2592 &elementNumbering_global_new2old_read[0]);
2593 status = H5Dclose(e_new2old_dataset_id);
2594 status = H5Fclose(file_id);
2595 //assert() is compiled out by -DNDEBUG, so this verification has to
2596 //count and report rather than assert, or the whole block is cost
2597 //with no signal.
2598 {
2599 long bad_=0, first_=-1;
2600 for (int i=0;i<nElements_global;i++)
2601 if (elementNumbering_global_new2old[i] != elementNumbering_global_new2old_read[i]) { if (first_<0) first_=i; bad_++; }
2602 if (bad_)
2603 {
2604 std::cerr<<"PROTEUS numbering check failed: elementNumbering_global_new2old vs elementNumbering_global_new2old_read, "
2605 <<bad_<<" of "<<nElements_global<<" differ, first at "<<first_<<std::endl;
2606 SETERRABORT(PROTEUS_COMM_WORLD, PETSC_ERR_PLIB,
2607 "out-of-core numbering does not match in-core");
2608 }
2609 }
2610 std::cout<<"==================out of core elements new2old is correct!===================="<<std::endl;
2611 for (size_t i=0;i< new_element_indices_subdomain.size(); i++)
2612 {
2613 int eN = new_element_indices_subdomain[i];
2614 assert(elementNumbering_new2old_subdomain_map[eN] == elementNumbering_global_new2old_read[eN]);
2615 }
2616 std::cout<<"==================out of core elements new2old map is correct!===================="<<std::endl;
2617 ISRestoreIndices(elementNumberingIS_global_new2old,&elementNumbering_global_new2old);
2618 ierr=ISDestroy(&elementNumberingIS_global_new2old);CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
2619 ierr=ISDestroy(&elementNumberingIS_subdomain_new2old);CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
2620 }
2621 PetscLogEventEnd(build_subdomains_global_numbering_elements_event,0,0,0,0);
2622 ierr = enforceMemoryLimit(PROTEUS_COMM_WORLD, rank, max_rss_gb,"Done allocating element numbering new2old/old2new");CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
2623 int build_subdomains_faces_event;
2624 PetscLogEventRegister("Subd faces",0,&build_subdomains_faces_event);
2625 PetscLogEventBegin(build_subdomains_faces_event,0,0,0,0);
2626 //
2627 //4b,5b. repeat process to build global face (elementBoundary) numbering
2628 //
2629 //first read element boundaries to create nodeElementBoundariesArray
2630 //for all element boundaries on this subdomain, which we'll use to
2631 //grab element boundaries from the bit array
2632 logEvent("Generating global face numbering and ownership corresponding to new subdomain ownership",5);
2633 std::ifstream elementBoundaryFile(elementBoundaryFileName.c_str());
2634
2635 if (!elementBoundaryFile.good())
2636 {
2637 std::cerr<<"cannot open Tetgen face file "
2638 <<elementBoundaryFileName<<std::endl;
2639 failed = true;
2640 return failed;
2641 }
2642
2643 bool hasElementBoundaryMarkers = false;
2644 int nElementBoundaries_global;
2645 int ihasElementBoundaryMarkers(0);
2646 elementBoundaryFile >> eatcomments >> nElementBoundaries_global >> ihasElementBoundaryMarkers >> eatline ;
2647 assert(nElementBoundaries_global > 0);
2648 if (ihasElementBoundaryMarkers > 0)
2649 {
2650 hasElementBoundaryMarkers = true;
2651 }
2652 newMesh.nElementBoundaries_global = nElementBoundaries_global;
2653 //note, these will be in the new element numbering
2654 set<int> elementBoundaries_subdomain_owned;
2655 vector<set<int>> nodeElementBoundariesStar(nNodes_subdomain_new[rank]);
2656 map<int,int> elementBoundaryMaterialTypesMap;
2657 map<int,valarray<int>> elementBoundariesMap;
2658 set<int> supportedElementBoundaries;
2659 set<int> elementBoundaries_subdomain;
2660 for (int ieb = 0; ieb < nElementBoundaries_global; ieb++)
2661 {
2662 int neb,nn0,nn1,nn2; int ebId(0);
2663 elementBoundaryFile >> eatcomments >> neb >> nn0 >> nn1 >> nn2;
2664 if (ihasElementBoundaryMarkers > 0)
2665 elementBoundaryFile >> ebId;
2666 neb -= indexBase;
2667 nn0 -= indexBase;
2668 nn1 -= indexBase;
2669 nn2 -= indexBase;
2670 assert(0 <= neb && neb < nElementBoundaries_global && elementBoundaryFile.good());
2671 //grab the element boundaries for the node if the node is owned by the subdomain
2672 //this will miss the element boundaries on the "outside boundary" of the star, which we'll grab later
2673 //
2674 int nn0_new = -1;
2675 if (nodes_old2new_subdomain_map.find(nn0) != nodes_old2new_subdomain_map.end())
2676 nn0_new = nodes_old2new_subdomain_map[nn0];
2677 if (nn0_new >= nodeOffsets_new[rank] && nn0_new < nodeOffsets_new[rank+1])
2678 {
2679 nodeElementBoundariesStar[nn0_new-nodeOffsets_new[rank]].insert(neb);
2680 supportedElementBoundaries.insert(neb);
2681 }
2682 int nn1_new = -2;
2683 if (nodes_old2new_subdomain_map.find(nn1) != nodes_old2new_subdomain_map.end())
2684 nn1_new = nodes_old2new_subdomain_map[nn1];
2685 if (nn1_new >= nodeOffsets_new[rank] && nn1_new < nodeOffsets_new[rank+1])
2686 {
2687 nodeElementBoundariesStar[nn1_new-nodeOffsets_new[rank]].insert(neb);
2688 supportedElementBoundaries.insert(neb);
2689 }
2690 int nn2_new = -3;
2691 if (nodes_old2new_subdomain_map.find(nn2) != nodes_old2new_subdomain_map.end())
2692 nn2_new = nodes_old2new_subdomain_map[nn2];
2693 if (nn2_new >= nodeOffsets_new[rank] && nn2_new < nodeOffsets_new[rank+1])
2694 {
2695 nodeElementBoundariesStar[nn2_new-nodeOffsets_new[rank]].insert(neb);
2696 supportedElementBoundaries.insert(neb);
2697 }
2698 //if any of the new nodes is negative, then this face does not belong to an element in the subdomain
2699 //so the check on nodes[1] will always be false
2700 const int nodes[3] = {nn0_new,nn1_new,nn2_new};
2701 NodeTuple<3> nodeTuple(nodes);
2702 elementBoundaryFile >> eatline;
2703 if (elementBoundaryElementsMap.find(nodeTuple) != elementBoundaryElementsMap.end())//this element boundary is on an element in the subdomain
2704 {
2705 elementBoundaries_subdomain.insert(neb);
2706 assert(nn0_new >= 0 && nn1_new >= 0 && nn2_new >= 0);
2707 if (nodeTuple.nodes[1] >= nodeOffsets_new[rank] && nodeTuple.nodes[1] < nodeOffsets_new[rank+1])
2708 elementBoundaries_subdomain_owned.insert(neb);
2709 if (ihasElementBoundaryMarkers > 0)
2710 elementBoundaryMaterialTypesMap[neb]=ebId;
2711 int eN_left = elementNumbering_old2new_subdomain_map[elementBoundaryElementsMap[nodeTuple].left];
2712 if (elementBoundariesMap.find(eN_left) != elementBoundariesMap.end())
2713 {
2714 elementBoundariesMap[eN_left][elementBoundaryElementsMap[nodeTuple].left_ebN_element] = neb;
2715 }
2716 else
2717 {
2718 //initialize
2719 valarray<int> elementBoundaries_element(-1,4);
2720 elementBoundariesMap[eN_left] = elementBoundaries_element;
2721 //assign
2722 elementBoundariesMap[eN_left][elementBoundaryElementsMap[nodeTuple].left_ebN_element] = neb;
2723 }
2724 if (elementBoundaryElementsMap[nodeTuple].right >= 0)
2725 {
2726 int eN_right = elementNumbering_old2new_subdomain_map[elementBoundaryElementsMap[nodeTuple].right];
2727 if (elementBoundariesMap.find(eN_right) != elementBoundariesMap.end())
2728 {
2729 elementBoundariesMap[eN_right][elementBoundaryElementsMap[nodeTuple].right_ebN_element] = neb;
2730 }
2731 else
2732 {
2733 //initialize
2734 valarray<int> elementBoundaries_element(-1, 4);
2735 elementBoundariesMap[eN_right] = elementBoundaries_element;
2736 //assign
2737 elementBoundariesMap[eN_right][elementBoundaryElementsMap[nodeTuple].right_ebN_element] = neb;
2738 }
2739 }
2740 }
2741 }
2742 //done reading element boundaries
2743 elementBoundaryFile.close();
2744 ierr = enforceMemoryLimit(PROTEUS_COMM_WORLD, rank, max_rss_gb,"Done reading element boundaries");CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
2745 int nElementBoundaries_owned_subdomain=elementBoundaries_subdomain_owned.size(),
2746 nElementBoundaries_owned_new=0;
2747 MPI_Allreduce(&nElementBoundaries_owned_subdomain,&nElementBoundaries_owned_new,1,MPI_INT,MPI_SUM,PROTEUS_COMM_WORLD);
2748 assert(nElementBoundaries_owned_new == nElementBoundaries_global);
2749 //now get the element boundaries on the outside of the star
2750 for (auto elementBoundariesp=elementBoundariesMap.begin();
2751 elementBoundariesp!=elementBoundariesMap.end();
2752 elementBoundariesp++)
2753 {
2754 //loop over the nodes of this element for the owned nodes
2755 for (int iv=0;iv<4;iv++)
2756 {
2757 //the elementNodesArrayMap is in the old element numbering while the elementBoundariesMap is in the new element numbering
2758 int nN_global = elementNodesArrayMap[elementNumbering_new2old_subdomain_map[elementBoundariesp->first]][iv];
2759 if (nN_global >= nodeOffsets_new[rank] && nN_global < nodeOffsets_new[rank+1])
2760 {
2761 //add all the faces to this node star
2762 for(int eb=0;eb<4;eb++)
2763 {
2764 nodeElementBoundariesStar[nN_global-nodeOffsets_new[rank]].insert(elementBoundariesp->second[eb]);
2765 }
2766 }
2767 }
2768 }
2769 //build compact structures for nodeElementBoundariesArray
2770 valarray<int> nodeElementBoundaryOffsets(nNodes_subdomain_new[rank]+1);
2771 nodeElementBoundaryOffsets[0] = 0;
2772 for (int nN = 0; nN < nNodes_subdomain_new[rank]; nN++)
2773 nodeElementBoundaryOffsets[nN+1] = nodeElementBoundaryOffsets[nN]+nodeElementBoundariesStar[nN].size();
2774 valarray<int> nodeElementBoundariesArray(nodeElementBoundaryOffsets[nNodes_subdomain_new[rank]]);
2775 for (int nN=0,offset=0; nN < nNodes_subdomain_new[rank]; nN++)
2776 {
2777 for (set<int>::iterator ebN_star = nodeElementBoundariesStar[nN].begin(); ebN_star != nodeElementBoundariesStar[nN].end();
2778 ebN_star++,offset++)
2779 {
2780 nodeElementBoundariesArray[offset] = *ebN_star;
2781 }
2782 }
2783
2784 //get the number of elementBoundaries owned on each processor
2785 valarray<int> nElementBoundaries_subdomain_new(size),
2786 elementBoundaryOffsets_new(size+1);
2787 for (int sdN=0;sdN<size;sdN++)
2788 if (sdN == rank)
2789 nElementBoundaries_subdomain_new[sdN] = elementBoundaries_subdomain_owned.size();
2790 else
2791 nElementBoundaries_subdomain_new[sdN] = 0;
2792 valarray<int> nElementBoundaries_subdomain_new_send=nElementBoundaries_subdomain_new;
2793 MPI_Allreduce(&nElementBoundaries_subdomain_new_send[0],&nElementBoundaries_subdomain_new[0],
2794 size,MPI_INT,MPI_SUM,PROTEUS_COMM_WORLD);
2795 elementBoundaryOffsets_new[0] = 0;
2796 for (int sdN=0;sdN<size;sdN++)
2797 elementBoundaryOffsets_new[sdN+1] = elementBoundaryOffsets_new[sdN]+nElementBoundaries_subdomain_new[sdN];
2798 //
2799 //Now as with elements and nodes build a global face numbering
2800 //
2801 valarray<int> elementBoundaryNumbering_new2old(elementBoundaries_subdomain_owned.size());
2802 map<int,int> elementBoundaryNumbering_old2new_map;
2803 set<int>::iterator ebN_ownedp=elementBoundaries_subdomain_owned.begin();
2804 for (int ebN=0;ebN<int(elementBoundaries_subdomain_owned.size());ebN++, ebN_ownedp++)
2805 {
2806 elementBoundaryNumbering_new2old[ebN] = *ebN_ownedp;
2807 elementBoundaryNumbering_old2new_map[*ebN_ownedp] = ebN+elementBoundaryOffsets_new[rank];
2808 }
2809 //
2810 //write this subdomain to the mapping file
2811 //
2812 logEvent("Writing/reading elementBoundary (face) numberings to hdf5",5);
2813 hsize_t eb_dims[]={static_cast<hsize_t>(nElementBoundaries_global)};
2814 hid_t eb_new2old_filespace_id = H5Screate_simple(ARRAY_RANK, eb_dims, NULL);
2815 mappings_plist_id = H5Pcreate(H5P_FILE_ACCESS);
2816 H5Pset_fapl_mpio(mappings_plist_id, PROTEUS_COMM_WORLD, MPI_INFO_NULL);
2817 file_id = H5Fopen(H5FILE_NAME, H5F_ACC_RDWR, mappings_plist_id);
2818 H5Pclose(mappings_plist_id);
2819 hid_t eb_new2old_dataset_id = H5Dcreate(file_id, "elementBoundaryNumbering_new2old", H5T_NATIVE_INT,
2820 eb_new2old_filespace_id,
2821 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
2822 hsize_t eb_count[] = {static_cast<hsize_t>(nElementBoundaries_subdomain_new[rank])};
2823 hsize_t eb_offset[] = {static_cast<hsize_t>(elementBoundaryOffsets_new[rank])};
2824 hid_t eb_new2old_memspace_id = H5Screate_simple(ARRAY_RANK, eb_count, NULL);
2825 H5Sselect_hyperslab(eb_new2old_filespace_id, H5S_SELECT_SET, eb_offset, NULL, eb_count, NULL);
2826 hid_t eb_new2old_plist_id = H5Pcreate(H5P_DATASET_XFER);
2827 H5Pset_dxpl_mpio(eb_new2old_plist_id, H5FD_MPIO_COLLECTIVE);
2828 status = H5Dwrite(eb_new2old_dataset_id, H5T_NATIVE_INT, eb_new2old_memspace_id, eb_new2old_filespace_id,
2829 eb_new2old_plist_id, &elementBoundaryNumbering_new2old[0]);
2830 H5Pclose(eb_new2old_plist_id);
2831 H5Sclose(eb_new2old_memspace_id);
2832 H5Sclose(eb_new2old_filespace_id);
2833 H5Dclose(eb_new2old_dataset_id);
2834 //do old2new map too, can't use hyperslab
2835 valarray<hsize_t> old_elementBoundary_indices(nElementBoundaries_subdomain_new[rank]);
2836 valarray<int> new_elementBoundary_indices(nElementBoundaries_subdomain_new[rank]);
2837 for (int i=0;i<nElementBoundaries_subdomain_new[rank];i++)
2838 {
2839 old_elementBoundary_indices[i] = elementBoundaryNumbering_new2old[i];
2840 new_elementBoundary_indices[i] = elementBoundaryOffsets_new[rank]+i;
2841 }
2842 hid_t eb_old2new_filespace_id = H5Screate_simple(ARRAY_RANK, eb_dims, NULL);
2843 hid_t eb_old2new_dataset_id = H5Dcreate(file_id, "elementBoundaryNumbering_old2new", H5T_NATIVE_INT,
2844 eb_old2new_filespace_id,
2845 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
2846 hid_t eb_old2new_memspace_id = H5Screate_simple(ARRAY_RANK, eb_count, NULL);
2847 status = H5Sselect_elements(eb_old2new_filespace_id, H5S_SELECT_SET,
2848 nElementBoundaries_subdomain_new[rank], &old_elementBoundary_indices[0]);
2849 hid_t eb_old2new_plist_id = H5Pcreate(H5P_DATASET_XFER);
2850 H5Pset_dxpl_mpio(eb_old2new_plist_id, H5FD_MPIO_COLLECTIVE);
2851 status = H5Dwrite(eb_old2new_dataset_id, H5T_NATIVE_INT,
2852 eb_old2new_memspace_id, eb_old2new_filespace_id,
2853 eb_old2new_plist_id, &new_elementBoundary_indices[0]);
2854 H5Pclose(eb_new2old_plist_id);
2855 H5Sclose(eb_old2new_memspace_id);
2856 H5Dclose(eb_old2new_dataset_id);
2857 H5Sclose(eb_old2new_filespace_id);
2858 H5Fclose(file_id);
2859 //now get maps for all the elements on the subdomain, not just owned
2860 //
2861 valarray<hsize_t> old_elementBoundary_indices_subdomain(elementBoundaries_subdomain.size());
2862 valarray<int> new_elementBoundary_indices_subdomain(elementBoundaries_subdomain.size());
2863 int ebN_subdomain = 0;
2864 for (auto it = elementBoundaries_subdomain.begin();
2865 it != elementBoundaries_subdomain.end();
2866 it++, ebN_subdomain++)
2867 {
2868 old_elementBoundary_indices_subdomain[ebN_subdomain] = static_cast<hsize_t>(*it);
2869 }
2870 file_id = openMappingsReadOnly(PROTEUS_COMM_WORLD, H5FILE_NAME);
2871 if (file_id < 0)
2872 {
2873 H5Eprint2(H5E_DEFAULT, stderr);
2874 SETERRABORT(PROTEUS_COMM_WORLD, PETSC_ERR_FILE_OPEN,
2875 "could not reopen the mappings file; see the HDF5 stack above");
2876 }
2877 eb_old2new_filespace_id = H5Screate_simple(ARRAY_RANK, eb_dims, NULL);
2878 eb_old2new_dataset_id = H5Dopen1(file_id, "/elementBoundaryNumbering_old2new");
2879 status = H5Sselect_elements(eb_old2new_filespace_id, H5S_SELECT_SET,
2880 elementBoundaries_subdomain.size(),
2881 &old_elementBoundary_indices_subdomain[0]);
2882 hsize_t eb_subdomain_count[]={static_cast<hsize_t>(elementBoundaries_subdomain.size())};
2883 hid_t eb_old2new_subdomain_memspace_id = H5Screate_simple(ARRAY_RANK, eb_subdomain_count, NULL);
2884 hid_t eb_old2new_subdomain_plist_id = H5Pcreate(H5P_DATASET_XFER);
2885 setMappingsReadXfer(eb_old2new_subdomain_plist_id);
2886 status = H5Dread(eb_old2new_dataset_id, H5T_NATIVE_INT,
2887 eb_old2new_subdomain_memspace_id, eb_old2new_filespace_id,
2888 H5P_DEFAULT, &new_elementBoundary_indices_subdomain[0]);
2889 H5Pclose(eb_old2new_subdomain_plist_id);
2890 H5Sclose(eb_old2new_subdomain_memspace_id);
2891 H5Sclose(eb_old2new_filespace_id);
2892 H5Dclose(eb_old2new_dataset_id);
2893 H5Fclose(file_id);
2894 map<int,int> elementBoundaryNumbering_old2new_subdomain_map;
2895 map<int,int> elementBoundaryNumbering_new2old_subdomain_map;
2896 for (int i=0;i<elementBoundaries_subdomain.size();i++)
2897 {
2898 elementBoundaryNumbering_old2new_subdomain_map[old_elementBoundary_indices_subdomain[i]] = new_elementBoundary_indices_subdomain[i];
2899 elementBoundaryNumbering_new2old_subdomain_map[new_elementBoundary_indices_subdomain[i]] = old_elementBoundary_indices_subdomain[i];
2900 }
2901 IS elementBoundaryNumberingIS_subdomain_new2old;
2902 ISCreateGeneral(PROTEUS_COMM_WORLD,elementBoundaries_subdomain_owned.size(),&elementBoundaryNumbering_new2old[0],PETSC_COPY_VALUES,&elementBoundaryNumberingIS_subdomain_new2old);
2903 if (test)
2904 {
2905 IS elementBoundaryNumberingIS_global_new2old;
2906 const PetscInt *elementBoundaryNumbering_global_new2old;
2907 valarray<int> elementBoundaryNumbering_global_old2new(nElementBoundaries_global);
2908 ISAllGather(elementBoundaryNumberingIS_subdomain_new2old,&elementBoundaryNumberingIS_global_new2old);
2909 elementBoundaryNumbering_global_old2new.resize(newMesh.nElementBoundaries_global);
2910 ierr = enforceMemoryLimit(PROTEUS_COMM_WORLD, rank, max_rss_gb,"Allocating elementBoudnary old2new/new2old");CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
2911 ISGetIndices(elementBoundaryNumberingIS_global_new2old,&elementBoundaryNumbering_global_new2old);
2912 for (int i=0;i<nElementBoundaries_global;i++)
2913 {
2914 elementBoundaryNumbering_global_old2new[elementBoundaryNumbering_global_new2old[i]]=i;
2915 }
2916
2917 herr_t status;
2918 valarray<int> elementBoundaryNumbering_old2new_read(nElementBoundaries_global);
2919 file_id = openMappingsReadOnly(PROTEUS_COMM_WORLD, H5FILE_NAME);
2920 eb_old2new_dataset_id = H5Dopen1(file_id, "/elementBoundaryNumbering_old2new");
2921 status = H5Dread(eb_old2new_dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT,
2922 &elementBoundaryNumbering_old2new_read[0]);
2923 status = H5Dclose(eb_old2new_dataset_id);
2924 //assert() is compiled out by -DNDEBUG, so this verification has to
2925 //count and report rather than assert, or the whole block is cost
2926 //with no signal.
2927 {
2928 long bad_=0, first_=-1;
2929 for (int i=0;i<nElementBoundaries_global;i++)
2930 if (elementBoundaryNumbering_global_old2new[i] != elementBoundaryNumbering_old2new_read[i]) { if (first_<0) first_=i; bad_++; }
2931 if (bad_)
2932 {
2933 std::cerr<<"PROTEUS numbering check failed: elementBoundaryNumbering_global_old2new vs elementBoundaryNumbering_old2new_read, "
2934 <<bad_<<" of "<<nElementBoundaries_global<<" differ, first at "<<first_<<std::endl;
2935 SETERRABORT(PROTEUS_COMM_WORLD, PETSC_ERR_PLIB,
2936 "out-of-core numbering does not match in-core");
2937 }
2938 }
2939 std::cout<<"==================out of core old2new elementBoundaries is correct!===================="<<std::endl;
2940 hid_t eb_new2old_dataset_id = H5Dopen1(file_id, "/elementBoundaryNumbering_new2old");
2941 valarray<int> elementBoundaryNumbering_new2old_read(nElementBoundaries_global);
2942 status = H5Dread(eb_new2old_dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT,
2943 &elementBoundaryNumbering_new2old_read[0]);
2944 status = H5Dclose(eb_new2old_dataset_id);
2945 status = H5Fclose(file_id);
2946 //assert() is compiled out by -DNDEBUG, so this verification has to
2947 //count and report rather than assert, or the whole block is cost
2948 //with no signal.
2949 {
2950 long bad_=0, first_=-1;
2951 for (int i=0;i<nElementBoundaries_global;i++)
2952 if (elementBoundaryNumbering_global_new2old[i] != elementBoundaryNumbering_new2old_read[i]) { if (first_<0) first_=i; bad_++; }
2953 if (bad_)
2954 {
2955 std::cerr<<"PROTEUS numbering check failed: elementBoundaryNumbering_global_new2old vs elementBoundaryNumbering_new2old_read, "
2956 <<bad_<<" of "<<nElementBoundaries_global<<" differ, first at "<<first_<<std::endl;
2957 SETERRABORT(PROTEUS_COMM_WORLD, PETSC_ERR_PLIB,
2958 "out-of-core numbering does not match in-core");
2959 }
2960 }
2961 std::cout<<"==================out of core new2old elementBoundaries is correct!===================="<<std::endl;
2962 for (int i=0;i<elementBoundaries_subdomain.size();i++)
2963 {
2964 int ebN = old_elementBoundary_indices_subdomain[i];
2965 assert(elementBoundaryNumbering_global_old2new[ebN] == elementBoundaryNumbering_old2new_subdomain_map[ebN]);
2966 }
2967 std::cout<<"==================out of core elementBoundaries old2new subdomain_map is correct!===================="<<std::endl;
2968 for (int i=0;i<elementBoundaries_subdomain.size();i++)
2969 {
2970 int ebN = new_elementBoundary_indices_subdomain[i];
2971 assert(elementBoundaryNumbering_global_new2old[ebN] == elementBoundaryNumbering_new2old_subdomain_map[ebN]);
2972 }
2973 std::cout<<"==================out of core elementBoundaries new2old subdomain_map is correct!===================="<<std::endl;
2974 ISRestoreIndices(elementBoundaryNumberingIS_global_new2old,&elementBoundaryNumbering_global_new2old);
2975 ierr=ISDestroy(&elementBoundaryNumberingIS_subdomain_new2old);CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
2976 ierr=ISDestroy(&elementBoundaryNumberingIS_global_new2old);CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
2977 }
2978 PetscLogEventEnd(build_subdomains_faces_event,0,0,0,0);
2979 ierr = enforceMemoryLimit(PROTEUS_COMM_WORLD, rank, max_rss_gb,"Done allocating elementBoudnary old2new/new2old");CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
2980 int build_subdomains_edges_event;
2981 PetscLogEventRegister("Subd edges",0,&build_subdomains_edges_event);
2982 PetscLogEventBegin(build_subdomains_edges_event,0,0,0,0);
2983
2984 //
2985 //4c,5c. Repeate the process for edges
2986 //
2987 logEvent("Generating global edge numbering and ownership corresponding to new subdomain ownership",5);
2988 std::ifstream edgeFile(edgeFileName.c_str());
2989
2990 if (!edgeFile.good())
2991 {
2992 std::cerr<<"cannot open Tetgen edge file"
2993 <<edgeFileName<<std::endl;
2994 failed = true;
2995 return failed;
2996 }
2997
2998 bool hasEdgeMarkers = false;
2999 int nEdges_global;
3000 int ihasEdgeMarkers(0);
3001 edgeFile >> eatcomments >> nEdges_global >> eatline;// edge file doesn currently contain markers >> ihasEdgeMarkers >> eatline ;
3002 assert(nEdges_global > 0);
3003 if (ihasEdgeMarkers > 0)
3004 {
3005 hasEdgeMarkers = true;
3006 }
3007 newMesh.nEdges_global = nEdges_global;
3008 set<int> edges_subdomain_owned;
3009 vector<set<int> > nodeEdgesStar(nNodes_subdomain_new[rank]);
3010 map<int,int> edgeMaterialTypesMap;
3011 map<int,valarray<int> > elementEdgesMap;
3012 map<int,pair<int,int> > edgeNodesMap;
3013 set<int> supportedEdges;
3014 for (int ied = 0; ied < nEdges_global; ied++)
3015 {
3016 int ned,nn0,nn1; int edId(0);
3017 edgeFile >> eatcomments >> ned >> nn0 >> nn1;
3018 if (ihasEdgeMarkers > 0)
3019 edgeFile >> edId;
3020 ned -= indexBase;
3021 nn0 -= indexBase;
3022 nn1 -= indexBase;
3023 assert(0 <= ned && ned < nEdges_global && edgeFile.good());
3024 int nn0_new = -1;
3025 if (nodes_old2new_subdomain_map.find(nn0) != nodes_old2new_subdomain_map.end())
3026 nn0_new = nodes_old2new_subdomain_map[nn0];
3027 if (nn0_new >= nodeOffsets_new[rank] && nn0_new < nodeOffsets_new[rank+1])
3028 {
3029 nodeEdgesStar.at(nn0_new-nodeOffsets_new[rank]).insert(ned);
3030 supportedEdges.insert(ned);
3031 }
3032 int nn1_new = -2;
3033 if(nodes_old2new_subdomain_map.find(nn1) != nodes_old2new_subdomain_map.end())
3034 nn1_new = nodes_old2new_subdomain_map[nn1];
3035 if (nn1_new >= nodeOffsets_new[rank] && nn1_new < nodeOffsets_new[rank+1])
3036 {
3037 nodeEdgesStar.at(nn1_new-nodeOffsets_new[rank]).insert(ned);
3038 supportedEdges.insert(ned);
3039 }
3040 int nodes[2] = {nn0_new,nn1_new};
3041 NodeTuple<2> nodeTuple(nodes);
3042 edgeFile >> eatline;
3043 if (edgeElementsMap.find(nodeTuple) != edgeElementsMap.end())//this edge is on an element in the subdomain
3044 {
3045 if (nodeTuple.nodes[0] >= nodeOffsets_new[rank] && nodeTuple.nodes[0] < nodeOffsets_new[rank+1])
3046 edges_subdomain_owned.insert(ned);
3047 //pick up all the edges on the subdomain and store their nodes
3048 edgeNodesMap[ned].first = nodeTuple.nodes[0];
3049 edgeNodesMap[ned].second = nodeTuple.nodes[1];
3050 if (ihasEdgeMarkers > 0)
3051 edgeMaterialTypesMap[ned]=edId;
3052 for (auto elementp=edgeElementsMap[nodeTuple].begin();
3053 elementp != edgeElementsMap[nodeTuple].end();
3054 elementp++)
3055 {
3056 int eN = elementNumbering_old2new_subdomain_map[elementp->first];
3057 if (elementEdgesMap.find(eN) != elementEdgesMap.end())
3058 {
3059 elementEdgesMap[eN][elementp->second] = ned;
3060 }
3061 else
3062 {
3063 std::valarray<int> init(-1,6);
3064 elementEdgesMap[eN] = init;
3065 elementEdgesMap[eN][elementp->second] = ned;
3066 }
3067 }
3068 }
3069 }//end iv
3070 edgeFile.close();
3071 int nEdges_owned_subdomain=edges_subdomain_owned.size(),
3072 nEdges_owned_new=0;
3073 MPI_Allreduce(&nEdges_owned_subdomain,&nEdges_owned_new,1,MPI_INT,MPI_SUM,PROTEUS_COMM_WORLD);
3074 assert(nEdges_owned_new == nEdges_global);
3075 ierr = enforceMemoryLimit(PROTEUS_COMM_WORLD, rank, max_rss_gb,"Done reading edges");CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
3076 //done with edge file
3077 //
3078 //just as with faces, we need to add edges along outer boundaries of star
3079 //not sure if we need to collect nodeEdges star above anymore, since we're doing this
3080 //
3081 for (auto edgesp=elementEdgesMap.begin();
3082 edgesp!=elementEdgesMap.end();
3083 edgesp++)
3084 {
3085 //loop over the nodes of this element for the owned nodes
3086 for (int iv=0;iv<4;iv++)
3087 {
3088 //the elementNodesArrayMap is in the old element numbering while the elementEdgesMap is in the new element numbering
3089 int nN_global = -1;
3090 if (elementNumbering_new2old_subdomain_map.find(edgesp->first) != elementNumbering_new2old_subdomain_map.end())
3091 nN_global = elementNodesArrayMap[elementNumbering_new2old_subdomain_map[edgesp->first]][iv];
3092 if (nN_global >= nodeOffsets_new[rank] && nN_global < nodeOffsets_new[rank+1])
3093 {
3094 //add all the edges to this node star
3095 for(int ed=0;ed<6;ed++)
3096 {
3097 nodeEdgesStar.at(nN_global-nodeOffsets_new[rank]).insert(edgesp->second[ed]);
3098 }
3099 }
3100 }
3101 }
3102 //build compact data structures for nodeEdgesArray
3103 valarray<int> nodeEdgeOffsets(nNodes_subdomain_new[rank]+1);
3104 nodeEdgeOffsets[0] = 0;
3105 for (int nN = 0; nN < nNodes_subdomain_new[rank]; nN++)
3106 nodeEdgeOffsets[nN+1] = nodeEdgeOffsets[nN]+nodeEdgesStar.at(nN).size();
3107 valarray<int> nodeEdgesArray(nodeEdgeOffsets[nNodes_subdomain_new[rank]]);
3108 for (int nN=0,offset=0; nN < nNodes_subdomain_new[rank]; nN++)
3109 {
3110 for (auto edN_star = nodeEdgesStar.at(nN).begin();
3111 edN_star != nodeEdgesStar.at(nN).end();
3112 edN_star++,offset++)
3113 {
3114 nodeEdgesArray[offset] = *edN_star;
3115 }
3116 }
3117 newMesh.subdomainp->nEdges_global = edgeNodesMap.size();
3118 //get the number of edges on each processor
3119 valarray<int> nEdges_subdomain_new(size),
3120 edgeOffsets_new(size+1);
3121 for (int sdN=0;sdN<size;sdN++)
3122 if (sdN == rank)
3123 nEdges_subdomain_new[sdN] = edges_subdomain_owned.size();
3124 else
3125 nEdges_subdomain_new[sdN] = 0;
3126 valarray<int> nEdges_subdomain_new_send=nEdges_subdomain_new;
3127 MPI_Allreduce(&nEdges_subdomain_new_send[0],&nEdges_subdomain_new[0],size,MPI_INT,MPI_SUM,PROTEUS_COMM_WORLD);
3128 //
3129 //construct new offsets for owned edges
3130 //
3131 edgeOffsets_new[0] = 0;
3132 for (int sdN=0;sdN<size;sdN++)
3133 edgeOffsets_new[sdN+1] = edgeOffsets_new[sdN]+nEdges_subdomain_new[sdN];
3134 //
3135 //Now as with elementBoundaries, build a global face numbering
3136 //resetting the edge based information is a little different since much of this is currently built below based
3137 //on the element and node information
3138 //
3139 valarray<int> edgeNumbering_new2old(edges_subdomain_owned.size());
3140 map<int,int> edgeNumbering_old2new_map;
3141 set<int>::iterator edN_ownedp=edges_subdomain_owned.begin();
3142 for (int edN=0;edN<int(edges_subdomain_owned.size());edN++,edN_ownedp++)
3143 {
3144 edgeNumbering_new2old[edN] = *edN_ownedp;
3145 edgeNumbering_old2new_map[*edN_ownedp] = edN+edgeOffsets_new[rank];
3146 }
3147 //
3148 //write this subdomain to the mapping file
3149 //
3150 logEvent("Writing/reading edge numberings to hdf5",5);
3151 hsize_t ed_dims[]={static_cast<hsize_t>(nEdges_global)};
3152 hid_t ed_new2old_filespace_id = H5Screate_simple(ARRAY_RANK, ed_dims, NULL);
3153 mappings_plist_id = H5Pcreate(H5P_FILE_ACCESS);
3154 H5Pset_fapl_mpio(mappings_plist_id, PROTEUS_COMM_WORLD, MPI_INFO_NULL);
3155 file_id = H5Fopen(H5FILE_NAME, H5F_ACC_RDWR, mappings_plist_id);
3156 H5Pclose(mappings_plist_id);
3157 hid_t ed_new2old_dataset_id = H5Dcreate(file_id, "edgeNumbering_new2old", H5T_NATIVE_INT,
3158 ed_new2old_filespace_id,
3159 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
3160 hsize_t ed_count[]={static_cast<hsize_t>(nEdges_subdomain_new[rank])};
3161 hsize_t ed_offset[]={static_cast<hsize_t>(edgeOffsets_new[rank])};
3162 hid_t ed_new2old_memspace_id = H5Screate_simple(ARRAY_RANK, ed_count, NULL);
3163 H5Sselect_hyperslab(ed_new2old_filespace_id, H5S_SELECT_SET, ed_offset, NULL, ed_count, NULL);
3164 hid_t ed_new2old_plist_id = H5Pcreate(H5P_DATASET_XFER);
3165 H5Pset_dxpl_mpio(ed_new2old_plist_id, H5FD_MPIO_COLLECTIVE);
3166 status = H5Dwrite(ed_new2old_dataset_id, H5T_NATIVE_INT,
3167 ed_new2old_memspace_id, ed_new2old_filespace_id,
3168 ed_new2old_plist_id, &edgeNumbering_new2old[0]);
3169 H5Pclose(ed_new2old_plist_id);
3170 H5Sclose(ed_new2old_memspace_id);
3171 H5Sclose(ed_new2old_filespace_id);
3172 H5Dclose(ed_new2old_dataset_id);
3173 //do old2new map too, can't use hyperslab
3174 valarray<hsize_t> old_edge_indices(static_cast<hsize_t>(nEdges_subdomain_new[rank]));
3175 valarray<int> new_edge_indices(nEdges_subdomain_new[rank]);
3176 for (int i=0;i<nEdges_subdomain_new[rank];i++)
3177 {
3178 old_edge_indices[i] = static_cast<hsize_t>(edgeNumbering_new2old[i]);
3179 new_edge_indices[i] = edgeOffsets_new[rank]+i;
3180 }
3181 hid_t ed_old2new_filespace_id = H5Screate_simple(ARRAY_RANK, ed_dims, NULL);
3182 hid_t ed_old2new_dataspace_id = H5Dcreate(file_id, "edgeNumbering_old2new", H5T_NATIVE_INT,
3183 ed_old2new_filespace_id,
3184 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
3185 hid_t ed_old2new_memspace_id = H5Screate_simple(ARRAY_RANK, ed_count, NULL);
3186 ed_old2new_filespace_id = H5Dget_space(ed_old2new_dataspace_id);
3187 status = H5Sselect_elements(ed_old2new_filespace_id, H5S_SELECT_SET,
3188 nEdges_subdomain_new[rank], &old_edge_indices[0]);
3189 hid_t ed_old2new_plist_id = H5Pcreate(H5P_DATASET_XFER);
3190 H5Pset_dxpl_mpio(ed_old2new_plist_id, H5FD_MPIO_COLLECTIVE);
3191 status = H5Dwrite(ed_old2new_dataspace_id, H5T_NATIVE_INT,
3192 ed_old2new_memspace_id, ed_old2new_filespace_id,
3193 ed_old2new_plist_id, &new_edge_indices[0]);
3194 H5Pclose(ed_old2new_plist_id);
3195 H5Sclose(ed_old2new_memspace_id);
3196 H5Dclose(ed_old2new_dataspace_id);
3197 H5Sclose(ed_old2new_filespace_id);
3198 H5Fclose(file_id);
3199 //
3200 //now get maps for all the edges on the subdomain, not just owned
3201 valarray<hsize_t> old_edge_indices_subdomain(edgeNodesMap.size());
3202 valarray<int> new_edge_indices_subdomain(edgeNodesMap.size());
3203 int edN_old_subdomain = 0;
3204 for (auto it = edgeNodesMap.begin(); it != edgeNodesMap.end(); it++, edN_old_subdomain++)
3205 {
3206 old_edge_indices_subdomain[edN_old_subdomain] = static_cast<hsize_t>(it->first);
3207 }
3208 file_id = openMappingsReadOnly(PROTEUS_COMM_WORLD, H5FILE_NAME);
3209 if (file_id < 0)
3210 {
3211 H5Eprint2(H5E_DEFAULT, stderr);
3212 SETERRABORT(PROTEUS_COMM_WORLD, PETSC_ERR_FILE_OPEN,
3213 "could not reopen the mappings file; see the HDF5 stack above");
3214 }
3215 ed_old2new_dataspace_id = H5Dopen1(file_id, "/edgeNumbering_old2new");
3216 ed_old2new_filespace_id = H5Screate_simple(ARRAY_RANK, ed_dims, NULL);
3217 status = H5Sselect_elements(ed_old2new_filespace_id, H5S_SELECT_SET,
3218 edgeNodesMap.size(), &old_edge_indices_subdomain[0]);
3219 hsize_t ed_subdomain_count[]={static_cast<hsize_t>(edgeNodesMap.size())};
3220 hid_t ed_old2new_subdomain_memspace_id = H5Screate_simple(ARRAY_RANK, ed_subdomain_count, NULL);
3221 hid_t ed_old2new_subdomain_plist_id = H5Pcreate(H5P_DATASET_XFER);
3222 setMappingsReadXfer(ed_old2new_subdomain_plist_id);
3223 status = H5Dread(ed_old2new_dataspace_id, H5T_NATIVE_INT, ed_old2new_subdomain_memspace_id,
3224 ed_old2new_filespace_id,
3225 H5P_DEFAULT, &new_edge_indices_subdomain[0]);
3226 H5Pclose(ed_old2new_subdomain_plist_id);
3227 H5Sclose(ed_old2new_subdomain_memspace_id);
3228 H5Sclose(ed_old2new_filespace_id);
3229 H5Dclose(ed_old2new_dataspace_id);
3230 H5Fclose(file_id);
3231 map<int,int> edgeNumbering_old2new_subdomain_map;
3232 map<int,int> edgeNumbering_new2old_subdomain_map;
3233 for (int i=0;i<edgeNodesMap.size();i++)
3234 {
3235 edgeNumbering_old2new_subdomain_map[old_edge_indices_subdomain[i]] = new_edge_indices_subdomain[i];
3236 edgeNumbering_new2old_subdomain_map[new_edge_indices_subdomain[i]] = old_edge_indices_subdomain[i];
3237 }
3238 if (test)
3239 {
3240 IS edgeNumberingIS_subdomain_new2old;
3241 ISCreateGeneral(PROTEUS_COMM_WORLD,edges_subdomain_owned.size(),&edgeNumbering_new2old[0],PETSC_COPY_VALUES,&edgeNumberingIS_subdomain_new2old);
3242 IS edgeNumberingIS_global_new2old;
3243 ISAllGather(edgeNumberingIS_subdomain_new2old,&edgeNumberingIS_global_new2old);
3244 const PetscInt *edgeNumbering_global_new2old;
3245 ISGetIndices(edgeNumberingIS_global_new2old,&edgeNumbering_global_new2old);
3246 valarray<int> edgeNumbering_global_old2new(newMesh.nEdges_global);
3247 ierr = enforceMemoryLimit(PROTEUS_COMM_WORLD, rank, max_rss_gb,"Setting edgeNumering old2new/new2old");CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
3248 for (int edN=0;edN<newMesh.nEdges_global;edN++)
3249 {
3250 edgeNumbering_global_old2new[edgeNumbering_global_new2old[edN]] = edN;
3251 }
3252
3253 herr_t status;
3254 valarray<int> edgeNumbering_old2new_read(nEdges_global);
3255 file_id = openMappingsReadOnly(PROTEUS_COMM_WORLD, H5FILE_NAME);
3256 hid_t ed_old2new_dataset_id = H5Dopen1(file_id, "/edgeNumbering_old2new");
3257 status = H5Dread(ed_old2new_dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT,
3258 &edgeNumbering_old2new_read[0]);
3259 status = H5Dclose(ed_old2new_dataset_id);
3260 //assert() is compiled out by -DNDEBUG, so this verification has to
3261 //count and report rather than assert, or the whole block is cost
3262 //with no signal.
3263 {
3264 long bad_=0, first_=-1;
3265 for (int i=0;i<nEdges_global;i++)
3266 if (edgeNumbering_global_old2new[i] != edgeNumbering_old2new_read[i]) { if (first_<0) first_=i; bad_++; }
3267 if (bad_)
3268 {
3269 std::cerr<<"PROTEUS numbering check failed: edgeNumbering_global_old2new vs edgeNumbering_old2new_read, "
3270 <<bad_<<" of "<<nEdges_global<<" differ, first at "<<first_<<std::endl;
3271 SETERRABORT(PROTEUS_COMM_WORLD, PETSC_ERR_PLIB,
3272 "out-of-core numbering does not match in-core");
3273 }
3274 }
3275 std::cout<<"==================out of core old2new edges is correct!===================="<<std::endl;
3276 hid_t ed_new2old_dataset_id = H5Dopen1(file_id, "/edgeNumbering_new2old");
3277 valarray<int> edgeNumbering_new2old_read(nEdges_global);
3278 status = H5Dread(ed_new2old_dataset_id, H5T_NATIVE_INT, H5S_ALL, H5S_ALL, H5P_DEFAULT,
3279 &edgeNumbering_new2old_read[0]);
3280 status = H5Dclose(ed_new2old_dataset_id);
3281 status = H5Fclose(file_id);
3282 //assert() is compiled out by -DNDEBUG, so this verification has to
3283 //count and report rather than assert, or the whole block is cost
3284 //with no signal.
3285 {
3286 long bad_=0, first_=-1;
3287 for (int i=0;i<nEdges_global;i++)
3288 if (edgeNumbering_global_new2old[i] != edgeNumbering_new2old_read[i]) { if (first_<0) first_=i; bad_++; }
3289 if (bad_)
3290 {
3291 std::cerr<<"PROTEUS numbering check failed: edgeNumbering_global_new2old vs edgeNumbering_new2old_read, "
3292 <<bad_<<" of "<<nEdges_global<<" differ, first at "<<first_<<std::endl;
3293 SETERRABORT(PROTEUS_COMM_WORLD, PETSC_ERR_PLIB,
3294 "out-of-core numbering does not match in-core");
3295 }
3296 }
3297 std::cout<<"==================out of core new2old edges is correct!===================="<<std::endl;
3298
3299 for (auto it = edgeNodesMap.begin(); it != edgeNodesMap.end(); ++it)
3300 {
3301 assert(edgeNumbering_global_old2new[it->first] == edgeNumbering_old2new_subdomain_map[it->first]);
3302 }
3303 std::cout<<"==================out of core edgeNumbering old2new subdomain is correct!===================="<<std::endl;
3304 for (int i = 0; i < edgeNodesMap.size(); i++)
3305 {
3306 int edN = new_edge_indices_subdomain[i];
3307 assert(edgeNumbering_global_new2old[edN] == edgeNumbering_new2old_subdomain_map[edN]);
3308 }
3309 std::cout<<"==================out of core edgeNumbering new2old subdomain is correct!===================="<<std::endl;
3310 ISRestoreIndices(edgeNumberingIS_global_new2old,&edgeNumbering_global_new2old);
3311 ierr=ISDestroy(&edgeNumberingIS_subdomain_new2old);CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
3312 ierr=ISDestroy(&edgeNumberingIS_global_new2old);CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
3313 }
3314 ierr = enforceMemoryLimit(PROTEUS_COMM_WORLD, rank, max_rss_gb,"Done allocating edgeNumering old2new/new2old");CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
3315 //
3316 //6. Figure out what is in the node stars but not locally owned, create ghost information
3317 //
3318 logEvent("Creating ghost information",5);
3319 set<int> elements_overlap,nodes_overlap,elementBoundaries_overlap,edges_overlap;
3320 for (int nN = 0; nN < nNodes_subdomain_new[rank]; nN++)
3321 {
3322 //nodes
3323 for (int offset = nodeStarOffsetsNew[nN];offset<nodeStarOffsetsNew[nN+1];offset++)
3324 {
3325 int nN_point_global = nodeStarArrayNew[offset];
3326 bool offproc = nN_point_global < nodeOffsets_new[rank] || nN_point_global >= nodeOffsets_new[rank+1];
3327 if (offproc)
3328 nodes_overlap.insert(nN_point_global);
3329 }
3330 //elements
3331 for (int eN_star_offset = nodeElementOffsets[nN];
3332 eN_star_offset < nodeElementOffsets[nN+1]; eN_star_offset++)
3333 {
3334 int eN_star_old = nodeElementsArray[eN_star_offset];
3335 int eN_star_new = elementNumbering_old2new_subdomain_map[eN_star_old];
3336 bool offproc = eN_star_new >= elementOffsets_new[rank+1] || eN_star_new < elementOffsets_new[rank];
3337 if (offproc)
3338 elements_overlap.insert(eN_star_new);
3339 }
3340 //element boundaries
3341 for (int ebN_star_offset = nodeElementBoundaryOffsets[nN];
3342 ebN_star_offset < nodeElementBoundaryOffsets[nN+1]; ebN_star_offset++)
3343 {
3344 int ebN_star_old = nodeElementBoundariesArray[ebN_star_offset];
3345 int ebN_star_new = elementBoundaryNumbering_old2new_subdomain_map[ebN_star_old];
3346 bool offproc = ebN_star_new >= elementBoundaryOffsets_new[rank+1] || ebN_star_new < elementBoundaryOffsets_new[rank];
3347 if (offproc)
3348 elementBoundaries_overlap.insert(ebN_star_new);
3349 }
3350 //edges in overlap
3351 for (int edN_star_offset = nodeEdgeOffsets[nN];
3352 edN_star_offset < nodeEdgeOffsets[nN+1]; edN_star_offset++)
3353 {
3354 int edN_star_old = nodeEdgesArray[edN_star_offset];
3355 int edN_star_new = edgeNumbering_old2new_subdomain_map[edN_star_old];
3356 bool offproc = edN_star_new >= edgeOffsets_new[rank+1] || edN_star_new < edgeOffsets_new[rank];
3357 if (offproc)
3358 edges_overlap.insert(edN_star_new);
3359 }
3360 }//nodes on this processor
3361 //cek debugging, edge overlap seems to be messed up. Check global node tuples of edges vs global edge numbers
3362 assert(edges_overlap.size() + nEdges_subdomain_new[rank] == edgeNodesMap.size());
3363 //
3364 //enumerate the overlap
3365 //
3366 int nN_subdomain = nNodes_subdomain_new[rank];
3367 map<int,int> nodes_overlap_global2subdomainMap;
3368 for (auto nN_globalp=nodes_overlap.begin();nN_globalp != nodes_overlap.end(); nN_globalp++,nN_subdomain++)
3369 nodes_overlap_global2subdomainMap[*nN_globalp] = nN_subdomain;
3370 //
3371 //7. add any addtional overlap, skip for now
3372 //
3373 //cek todo: re-enable additional overlap with out of core global maps
3374 assert(nNodes_overlap<2);
3375 logEvent("Skipping additiona of addtional overlap--only minimal overlap supported by this partioning function for now",5);
3376 //
3377 PetscLogEventEnd(build_subdomains_edges_event,0,0,0,0);
3378 int build_subdomains_renumber_event;
3379 PetscLogEventRegister("Subd's renumber",0,&build_subdomains_renumber_event);
3380 PetscLogEventBegin(build_subdomains_renumber_event,0,0,0,0);
3381 //
3382 //8. Build subdomain meshes in new numbering, assumes memory not allocated in subdomain mesh
3383 //
3384 logEvent("USER WARNING: You are partioning in parallel directly from Tetgen files (parun -F ...). ",5);
3385 logEvent("USER WARNING: You must use the 'f' and 'ee' flags in the triangleOptions input to avoid errors.",5);
3386 logEvent("USER WARNING: You may have done so, but the tetgen options are not stored in the Tetgen files, so we can't know.",5);
3387 logEvent("Creating final subdomain mesh data structures",5);
3388 if (newMesh.subdomainp == NULL)
3389 newMesh.subdomainp = new Mesh();
3390 newMesh.subdomainp->nElements_global = nElements_subdomain_new[rank] + elements_overlap.size();
3391 newMesh.subdomainp->nNodes_global = nNodes_subdomain_new[rank] + nodes_overlap.size();
3392 newMesh.subdomainp->nElementBoundaries_global = nElementBoundaries_subdomain_new[rank]+elementBoundaries_overlap.size();
3393 assert(int(edges_subdomain_owned.size()+edges_overlap.size()) == newMesh.subdomainp->nEdges_global);
3394 //newMesh.subdomainp->nEdges_global = edges_subdomain_owned.size()+edges_overlap.size();
3395 newMesh.subdomainp->nNodes_element = newMesh.nNodes_element;
3398 //subdomain 2 global mappings (including ghost info)
3399 valarray<int> nodeNumbering_subdomain2global(newMesh.subdomainp->nNodes_global);
3400 valarray<int> elementNumbering_subdomain2global(newMesh.subdomainp->nElements_global);
3401 valarray<int> elementBoundaryNumbering_subdomain2global(newMesh.subdomainp->nElementBoundaries_global);
3402 valarray<int> edgeNumbering_subdomain2global(newMesh.subdomainp->nEdges_global);
3403 map<int,int> nodeNumbering_global2subdomainMap;
3404 map<int,int> elementBoundaryNumbering_global2subdomainMap;
3405 map<int,int> edgeNumbering_global2subdomainMap;
3406 newMesh.subdomainp->nodeArray = new double[newMesh.subdomainp->nNodes_global*3];
3407 newMesh.subdomainp->nodeMaterialTypes = new int[newMesh.subdomainp->nNodes_global];
3408 //
3409 //now finally finish reading node coordinates and node flags
3410 //
3411 for (int iv = 0; iv < nNodes_global; iv++)
3412 {
3413 int nv; double x,y,z; int nodeId(0);
3414 vertexFile >> eatcomments >> nv >> x >> y >> z;
3415 if (hasVertexMarkers > 0)
3416 vertexFile >> nodeId;
3417 nv -= indexBase;
3418 assert(0 <= nv && nv < nNodes_global && vertexFile.good());
3419 int nN_global_new = -1;
3420 if (nodes_old2new_subdomain_map.find(nv) != nodes_old2new_subdomain_map.end())
3421 nN_global_new = nodes_old2new_subdomain_map[nv];
3422 //local
3423 if (nN_global_new >= nodeOffsets_new[rank] && nN_global_new < nodeOffsets_new[rank+1])
3424 {
3425 int nv_subdomain_new = nN_global_new - nodeOffsets_new[rank];
3426 nodeNumbering_subdomain2global[nv_subdomain_new] = nN_global_new;
3427 nodeNumbering_global2subdomainMap[nN_global_new] = nv_subdomain_new;
3428 newMesh.subdomainp->nodeArray[vertexDim*nv_subdomain_new + 0] = x;
3429 newMesh.subdomainp->nodeArray[vertexDim*nv_subdomain_new + 1] = y;
3430 newMesh.subdomainp->nodeArray[vertexDim*nv_subdomain_new + 2] = z;
3431 if (hasVertexMarkers > 0)
3432 newMesh.subdomainp->nodeMaterialTypes[nv_subdomain_new] = nodeId;
3433 }
3434 //overlap
3435 if (nodes_overlap.count(nN_global_new) == 1)
3436 {
3437 int nv_subdomain_new = nodes_overlap_global2subdomainMap[nN_global_new];
3438 nodeNumbering_subdomain2global[nv_subdomain_new] = nN_global_new;
3439 nodeNumbering_global2subdomainMap[nN_global_new] = nv_subdomain_new;
3440 newMesh.subdomainp->nodeArray[vertexDim*nv_subdomain_new + 0] = x;
3441 newMesh.subdomainp->nodeArray[vertexDim*nv_subdomain_new + 1] = y;
3442 newMesh.subdomainp->nodeArray[vertexDim*nv_subdomain_new + 2] = z;
3443 if (hasVertexMarkers > 0)
3444 newMesh.subdomainp->nodeMaterialTypes[nv_subdomain_new] = nodeId;
3445 }
3446 vertexFile >> eatline;
3447 }//end iv
3448 vertexFile.close();
3449 //done with vertex file (and all file reads at this point)
3450 ierr = enforceMemoryLimit(PROTEUS_COMM_WORLD, rank, max_rss_gb,"Done reading vertices");CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
3451
3453 newMesh.subdomainp->elementMaterialTypes = new int[newMesh.subdomainp->nElements_global];
3454 //
3455 //elements
3456 //
3457 //locally owned
3458 //
3459 for (int eN = 0; eN < nElements_subdomain_new[rank]; eN++)
3460 {
3461 int eN_global_new = elementOffsets_new[rank] + eN;
3462 int eN_global_old = elementNumbering_subdomain_new2old[eN];
3463 elementNumbering_subdomain2global[eN] = eN_global_new;
3464 newMesh.subdomainp->elementMaterialTypes[eN] = elementMaterialTypesMap[eN_global_old];
3465 for (int nN = 0; nN < newMesh.subdomainp->nNodes_element; nN++)
3466 {
3467 int nN_global_new = elementNodesArrayMap[eN_global_old][nN];
3468 int nN_subdomain = nodeNumbering_global2subdomainMap[nN_global_new];
3469 newMesh.subdomainp->elementNodesArray[eN*newMesh.subdomainp->nNodes_element + nN]= nN_subdomain;
3470 }
3471 }
3472 //
3473 //ghost
3474 //
3475 set<int>::iterator eN_p = elements_overlap.begin();
3476 for (int eN = nElements_subdomain_new[rank]; eN < nElements_subdomain_new[rank] + int(elements_overlap.size()); eN++,eN_p++)
3477 {
3478 int eN_global_new = *eN_p;
3479 int eN_global_old = elementNumbering_new2old_subdomain_map[eN_global_new];
3480 elementNumbering_subdomain2global[eN] = eN_global_new;
3481 newMesh.subdomainp->elementMaterialTypes[eN] = elementMaterialTypesMap[eN_global_old];
3482 for (int nN = 0; nN < newMesh.subdomainp->nNodes_element; nN++)
3483 {
3484 int nN_global_new = elementNodesArrayMap[eN_global_old][nN];
3485 int nN_subdomain = nodeNumbering_global2subdomainMap[nN_global_new];
3486 newMesh.subdomainp->elementNodesArray[eN*newMesh.subdomainp->nNodes_element + nN]= nN_subdomain;
3487 }
3488 }
3489 //
3490 //element boundaries
3491 //
3492 //owned
3493 //
3494 for (int ebN=0; ebN < nElementBoundaries_subdomain_new[rank]; ebN++)
3495 {
3496 int ebN_global = ebN + elementBoundaryOffsets_new[rank];
3497 elementBoundaryNumbering_subdomain2global[ebN]=ebN_global;
3498 elementBoundaryNumbering_global2subdomainMap[ebN_global] = ebN;
3499 }
3500 //
3501 //ghost
3502 //
3503 set<int>::iterator ebN_p = elementBoundaries_overlap.begin();
3504 for(int ebN=nElementBoundaries_subdomain_new[rank];ebN < nElementBoundaries_subdomain_new[rank] + int(elementBoundaries_overlap.size()); ebN++,ebN_p++)
3505 {
3506 int ebN_global = *ebN_p;
3507 elementBoundaryNumbering_subdomain2global[ebN] = ebN_global;
3508 elementBoundaryNumbering_global2subdomainMap[ebN_global] = ebN;
3509 }
3510 //
3511 //need elementBoundariesArray to assign consistent numbering on subdomain
3512 //
3513 //local
3514 //
3517 for (int eN=0;eN<nElements_subdomain_new[rank];eN++)
3518 {
3519 int eN_global = eN+elementOffsets_new[rank];
3520 for (int ebN=0;ebN<newMesh.subdomainp->nElementBoundaries_element;ebN++)
3521 {
3523 elementBoundaryNumbering_global2subdomainMap[elementBoundaryNumbering_old2new_subdomain_map[elementBoundariesMap[eN_global][ebN]]];
3524 }
3525 }
3526 //
3527 //ghost elements
3528 //
3529 eN_p = elements_overlap.begin();
3530 for (int eN = nElements_subdomain_new[rank]; eN < nElements_subdomain_new[rank] + int(elements_overlap.size()); eN++,eN_p++)
3531 {
3532 int eN_global_new = *eN_p;
3533 for (int ebN=0;ebN<newMesh.subdomainp->nElementBoundaries_element;ebN++)
3534 {
3536 elementBoundaryNumbering_global2subdomainMap[elementBoundaryNumbering_old2new_subdomain_map[elementBoundariesMap[eN_global_new][ebN]]];
3537 }
3538 }
3539 //
3540 //edges
3541 //
3542 //local
3543 //
3544 for (int edN=0; edN < nEdges_subdomain_new[rank]; edN++)
3545 {
3546 int edN_global = edN + edgeOffsets_new[rank];
3547 edgeNumbering_subdomain2global[edN]=edN_global;
3548 edgeNumbering_global2subdomainMap[edN_global] = edN;
3549 }
3550 //
3551 //ghost
3552 //
3553 set<int>::iterator edN_p = edges_overlap.begin();
3554 for(int edN=nEdges_subdomain_new[rank];edN < nEdges_subdomain_new[rank] + int(edges_overlap.size()); edN++,edN_p++)
3555 {
3556 int edN_global = *edN_p;
3557 edgeNumbering_subdomain2global[edN] = edN_global;
3558 edgeNumbering_global2subdomainMap[edN_global] = edN;
3559 }
3560 //
3561 //now build edgeNodes array in new numberings
3562 //
3563 newMesh.subdomainp->edgeNodesArray = new int[newMesh.subdomainp->nEdges_global*2];
3564 for (int i=0;i<newMesh.subdomainp->nEdges_global*2;i++)
3565 newMesh.subdomainp->edgeNodesArray[i] = -1;
3566 for (map<int,pair<int,int> >::iterator edgep=edgeNodesMap.begin();
3567 edgep!=edgeNodesMap.end();
3568 edgep++)
3569 {
3570 int edN_global_old = edgep->first;
3571 int edN_global_new = edgeNumbering_old2new_subdomain_map[edN_global_old];
3572 assert(edgeNumbering_global2subdomainMap.find(edN_global_new) != edgeNumbering_global2subdomainMap.end());
3573 int edN_subdomain = edgeNumbering_global2subdomainMap[edN_global_new];
3574 newMesh.subdomainp->edgeNodesArray[edN_subdomain*2+0] = nodeNumbering_global2subdomainMap[edgep->second.first];
3575 newMesh.subdomainp->edgeNodesArray[edN_subdomain*2+1] = nodeNumbering_global2subdomainMap[edgep->second.second];
3576 }
3577 //
3578 //end edges
3579 //
3580
3581 //now build rest of subdomain mesh connectivity information etc
3583 //build local geometric info
3586 if (hasElementBoundaryMarkers)
3587 {
3588 assert(newMesh.subdomainp->elementBoundariesArray != NULL);
3589 for (auto ebmp = elementBoundaryMaterialTypesMap.begin(); ebmp != elementBoundaryMaterialTypesMap.end();ebmp++)
3590 {
3591 int ebN_global_new = elementBoundaryNumbering_old2new_subdomain_map[ebmp->first];
3592 assert(elementBoundaryNumbering_global2subdomainMap.find(ebN_global_new) != elementBoundaryNumbering_global2subdomainMap.end());
3593 int ebN_subdomain = elementBoundaryNumbering_global2subdomainMap[ebN_global_new];
3594 newMesh.subdomainp->elementBoundaryMaterialTypes[ebN_subdomain] = ebmp->second;
3595 }
3596 if (!hasVertexMarkers)
3597 for (int ebNE = 0; ebNE < newMesh.subdomainp->nExteriorElementBoundaries_global; ebNE++)
3598 {
3599 int ebN = newMesh.subdomainp->exteriorElementBoundariesArray[ebNE];
3600 for (int nN_local = 0; nN_local < newMesh.subdomainp->nNodes_elementBoundary; nN_local++)
3601 {
3602 int nN = newMesh.subdomainp->elementBoundaryNodesArray[ebN*newMesh.subdomainp->nNodes_elementBoundary+nN_local];
3604 }
3605 }
3606 }
3607 ierr = enforceMemoryLimit(PROTEUS_COMM_WORLD, rank, max_rss_gb,"Done with material types");CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
3608 PetscLogEventEnd(build_subdomains_renumber_event,0,0,0,0);
3609 int build_subdomains_cleanup_event;
3610 PetscLogEventRegister("Cleanup",0,&build_subdomains_cleanup_event);
3611 PetscLogEventBegin(build_subdomains_cleanup_event,0,0,0,0);
3612 //transfer information about owned nodes and elements to mesh
3613 logEvent("Cleaning up after partitioning",5);
3614 if (newMesh.nodeOffsets_subdomain_owned)
3615 delete [] newMesh.nodeOffsets_subdomain_owned;
3617 delete [] newMesh.elementOffsets_subdomain_owned;
3620 if (newMesh.edgeOffsets_subdomain_owned)
3621 delete [] newMesh.edgeOffsets_subdomain_owned;
3622 newMesh.nodeOffsets_subdomain_owned = new int[size+1];
3623 newMesh.elementOffsets_subdomain_owned = new int[size+1];
3624 newMesh.elementBoundaryOffsets_subdomain_owned = new int[size+1];
3625 newMesh.edgeOffsets_subdomain_owned = new int[size+1];
3626 for (int sdN = 0; sdN < size+1; sdN++)
3627 {
3628 newMesh.nodeOffsets_subdomain_owned[sdN] = nodeOffsets_new[sdN];
3629 newMesh.elementOffsets_subdomain_owned[sdN] = elementOffsets_new[sdN];
3630 newMesh.elementBoundaryOffsets_subdomain_owned[sdN] = elementBoundaryOffsets_new[sdN];
3631 newMesh.edgeOffsets_subdomain_owned[sdN] = edgeOffsets_new[sdN];
3632 }
3634 delete [] newMesh.nodeNumbering_subdomain2global;
3635 newMesh.nodeNumbering_subdomain2global = new int[newMesh.subdomainp->nNodes_global];
3636 for (int nN = 0; nN < newMesh.subdomainp->nNodes_global; nN++)
3637 newMesh.nodeNumbering_subdomain2global[nN] = nodeNumbering_subdomain2global[nN];
3639 delete [] newMesh.elementNumbering_subdomain2global;
3641 for (int eN = 0; eN < newMesh.subdomainp->nElements_global; eN++)
3642 newMesh.elementNumbering_subdomain2global[eN] = elementNumbering_subdomain2global[eN];
3643 //
3647 for (int ebN = 0; ebN < newMesh.subdomainp->nElementBoundaries_global; ebN++)
3648 newMesh.elementBoundaryNumbering_subdomain2global[ebN] = elementBoundaryNumbering_subdomain2global[ebN];
3649 //
3651 delete [] newMesh.edgeNumbering_subdomain2global;
3652 newMesh.edgeNumbering_subdomain2global = new int[newMesh.subdomainp->nEdges_global];
3653 for (int i=0; i< newMesh.subdomainp->nEdges_global; i++)
3654 newMesh.edgeNumbering_subdomain2global[i] = edgeNumbering_subdomain2global[i];
3655 //cleanup
3656 PetscLogEventEnd(build_subdomains_cleanup_event,0,0,0,0);
3657 PetscLogStagePop();
3658 PetscLogView(PETSC_VIEWER_STDOUT_WORLD);
3659 ierr = enforceMemoryLimit(PROTEUS_COMM_WORLD, rank, max_rss_gb,"Done with partitioning!");CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
3660 return 0;
3661}
3662int partitionNodesFromTriangleFiles(const MPI_Comm& PROTEUS_COMM_WORLD, const char* filebase, int indexBase, Mesh& newMesh, int nNodes_overlap)
3663{
3664 using namespace std;
3665 PetscErrorCode ierr;
3666 PetscMPIInt size,rank;
3667
3668 ierr = MPI_Comm_size(PROTEUS_COMM_WORLD,&size);CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
3669 ierr = MPI_Comm_rank(PROTEUS_COMM_WORLD,&rank);CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
3670 PetscLogStage partitioning_stage;
3671 PetscLogStageRegister("Mesh Partition",&partitioning_stage);
3672 PetscLogStagePush(partitioning_stage);
3673
3674 /***********************************************************************
3675 partition domain based on the nodes without reading in the global mesh.
3676
3677 1. Partition nodes in the default partition of contiguous chunks with
3678 input ordering
3679
3680 2. Determine nodal connectivity on local processor
3681
3682 3. Generate new nodal partition using PETSc interface
3683
3684 4. Collect subdomain elements: any that contain owned nodes; tag ownership
3685
3686 4a,b. Collect element boundaries and edges on the subdomain elements and tag ownership
3687
3688 5. Generate global element and edge numbering for new subdomain ownership
3689
3690 6. Create overlap (ghost) information for nodes and elements
3691
3692 7. March through additional layers of overlap if requested,
3693
3694 8. Build subdomain meshes in new numbering
3695 ***********************************************************************/
3696 //
3697 //0. Set up triangle files. Note, triangle should have been run with -en to get all edges and neighbors
3698 //
3699 bool failed = false;
3700 const int simplexDim = 3;
3701 const int vertexDim = 3;
3702 using namespace IOutils;
3703 std::string vertexFileName = std::string(filebase) + ".node" ;
3704 std::string elementFileName = std::string(filebase) + ".ele" ;
3705 std::string elementBoundaryFileName = std::string(filebase) + ".edge" ;
3706 std::string edgeFileName = std::string(filebase) + ".edge" ;
3707
3708 //
3709 //1. Build default nodal partition
3710 //
3711 //compute offsets to build subdomain2global ordering for nodes
3712 //in default partitioning of the domain into subdomains. All
3713 //we need is the number of nodes in the global mesh
3714 //
3715 //read nodes for triangle format
3716 //first just read the number of nodes and whether or not there are node tags
3717 int read_elements_event;
3718 PetscLogEventRegister("Read eles",0,&read_elements_event);
3719 PetscLogEventBegin(read_elements_event,0,0,0,0);
3720 std::ifstream vertexFile(vertexFileName.c_str());
3721 if (!vertexFile.good())
3722 {
3723 std::cerr<<"cannot open triangle node file "
3724 <<vertexFileName<<std::endl;
3725 failed = true;
3726 return failed;
3727 }
3728 int hasVertexMarkers(0),hasVertexAttributes(0),nSpace(2),nNodes_global;
3729 //read number of vertices and whether node flags are provided
3730 vertexFile >> eatcomments >> nNodes_global >> nSpace >> hasVertexAttributes >> hasVertexMarkers >> eatline ;
3731 assert(nNodes_global > 0);
3732 assert(nSpace == 2);
3733 newMesh.nNodes_global = nNodes_global;
3734 newMesh.nNodes_element = simplexDim;
3735 newMesh.nNodes_elementBoundary = simplexDim-1;
3736 newMesh.nElementBoundaries_element = simplexDim;
3737 if (hasVertexAttributes > 0)
3738 {
3739 std::cerr<<"WARNING triangle nodes hasAttributes= "<<hasVertexAttributes
3740 <<" > 0 will treat first value as integer id for boundary!!"<<std::endl;
3741 hasVertexMarkers = 1;
3742 }
3743 //don't need to read anymore from nodes for now
3744
3745 //offsets provide the lower and upper bounds for the global numbering
3746 //first we just partition the nodes approximately equally ignoring connectivity
3747 valarray<int> nodeOffsets_old(size+1);
3748 nodeOffsets_old[0] = 0;
3749 for (int sdN=0; sdN < size; sdN++)
3750 {
3751 nodeOffsets_old[sdN+1] = nodeOffsets_old[sdN] +
3752 int(nNodes_global)/size + (int(nNodes_global)%size > sdN);
3753 }
3754 int nNodes_subdomain_old = nodeOffsets_old[rank+1] - nodeOffsets_old[rank];
3755
3756 //
3757 //2. Determine nodal connectivity (nodeStarArray) for nodes on subdomain
3758 //
3759 //connectivty commes from the topology (elements) file. We just grab the elements
3760 //that contain currently owned nodes
3761 std::ifstream elementFile(elementFileName.c_str());
3762 if (!elementFile.good())
3763 {
3764 std::cerr<<"cannot open triangle element file "
3765 <<elementFileName<<std::endl;
3766 failed = true;
3767 return failed;
3768 }
3769 //read elements
3770 int nNodesPerSimplex(simplexDim),hasElementMarkers = 0,nElements_global;
3771 elementFile >> eatcomments >> nElements_global >> nNodesPerSimplex >> hasElementMarkers >> eatline;
3772 assert(nElements_global > 0);
3773 assert(nNodesPerSimplex == simplexDim);
3774 newMesh.nElements_global = nElements_global;
3775 vector<int> element_nodes_old(3);
3776 vector<set<int> > nodeStar(nNodes_subdomain_old);
3777 map<int,vector<int> > elements_old;//elementNodesMap_old
3778 for (int ie = 0; ie < nElements_global; ie++)
3779 {
3780 int ne, nv;
3781 elementFile >> eatcomments >> ne;
3782 ne -= indexBase;
3783 assert(0 <= ne && ne < nElements_global && elementFile.good());
3784 for (int iv = 0; iv < simplexDim; iv++)
3785 {
3786 elementFile >> nv;
3787 nv -= indexBase;
3788 assert(0 <= nv && nv < nNodes_global);
3789 element_nodes_old[iv] = nv;
3790 }
3791 //for each node on the element
3792 for (int iv = 0; iv < simplexDim; iv++)
3793 {
3794 //check if the node is owned by the subdomain
3795 int nN_star = element_nodes_old[iv];
3796 bool inSubdomain=false;
3797 if (nN_star >= nodeOffsets_old[rank] && nN_star < nodeOffsets_old[rank+1])
3798 {
3799 //this node is owned by the subdomain so
3800 inSubdomain = true;
3801 for (int jv = 0; jv < simplexDim; jv++)
3802 {
3803 if (iv != jv)
3804 {
3805 int nN_star_subdomain = nN_star-nodeOffsets_old[rank];
3806 nodeStar[nN_star_subdomain].insert(element_nodes_old[jv]);
3807 }
3808 }
3809 }
3810 if (inSubdomain)
3811 elements_old[ie] = element_nodes_old;
3812 }
3813 elementFile >> eatline;
3814 }//end ie
3815 elementFile.close();
3816 PetscLogEventEnd(read_elements_event,0,0,0,0);
3817 int repartition_nodes_event;
3818 PetscLogEventRegister("Repart nodes",0,&repartition_nodes_event);
3819 PetscLogEventBegin(repartition_nodes_event,0,0,0,0);
3820 //done reading element file for first time; will need to read again after node partitioning
3821 //build compact data structure for nodeStar
3822 valarray<int> nodeStarOffsets(nNodes_subdomain_old+1);
3823 nodeStarOffsets[0] = 0;
3824 for (int nN=1;nN<nNodes_subdomain_old+1;nN++)
3825 nodeStarOffsets[nN] = nodeStarOffsets[nN-1] + nodeStar[nN-1].size();
3826 valarray<int> nodeStarArray(nodeStarOffsets[nNodes_subdomain_old]);
3827 for (int nN=0,offset=0;nN<nNodes_subdomain_old;nN++)
3828 for (set<int>::iterator nN_star=nodeStar[nN].begin();nN_star!=nodeStar[nN].end();nN_star++,offset++)
3829 nodeStarArray[offset] = *nN_star;
3830 //find maximum number of nodes in any star
3831 int max_nNodeNeighbors_node=0;
3832 for (int nN=0;nN<nNodes_subdomain_old;nN++)
3833 max_nNodeNeighbors_node=max(max_nNodeNeighbors_node,nodeStarOffsets[nN+1]-nodeStarOffsets[nN]);
3834 //build connectivity data structures for PETSc
3835 PetscBool isInitialized;
3836 PetscInitialized(&isInitialized);
3837 PetscInt *nodeNeighborsOffsets_subdomain,*nodeNeighbors_subdomain,*weights_subdomain,*vertex_weights_subdomain;
3838 PetscReal *partition_weights;
3839 PetscMalloc(sizeof(PetscInt)*(nNodes_subdomain_old+1),&nodeNeighborsOffsets_subdomain);
3840 PetscMalloc(sizeof(PetscInt)*(nNodes_subdomain_old*max_nNodeNeighbors_node),&nodeNeighbors_subdomain);
3841 PetscMalloc(sizeof(PetscInt)*(nNodes_subdomain_old*max_nNodeNeighbors_node),&weights_subdomain);
3842 PetscMalloc(sizeof(PetscInt)*(nNodes_subdomain_old),&vertex_weights_subdomain);
3843 PetscMalloc(sizeof(PetscReal)*(size),&partition_weights);
3844 for (int sd=0;sd<size;sd++)
3845 partition_weights[sd] = 1.0/double(size);
3846 nodeNeighborsOffsets_subdomain[0] = 0;
3847 //I think we can simplify this now that nodeStarArray is local to the subdomain, could just use nodeStar instead of nodeStarArray
3848 for (int nN = 0,offset=0; nN < nNodes_subdomain_old; nN++)
3849 {
3850 for (int offset_subdomain = nodeStarOffsets[nN];
3851 offset_subdomain < nodeStarOffsets[nN+1];
3852 offset_subdomain++)
3853 {
3854 nodeNeighbors_subdomain[offset++] = nodeStarArray[offset_subdomain];
3855 }
3856 nodeNeighborsOffsets_subdomain[nN+1]=offset;
3857 sort(&nodeNeighbors_subdomain[nodeNeighborsOffsets_subdomain[nN]],&nodeNeighbors_subdomain[nodeNeighborsOffsets_subdomain[nN+1]]);
3858 //weight nodes by size of star
3859 int weight= (nodeNeighborsOffsets_subdomain[nN+1] - nodeNeighborsOffsets_subdomain[nN]);
3860 vertex_weights_subdomain[nN] = weight;
3861 for (int k=nodeNeighborsOffsets_subdomain[nN];k<nodeNeighborsOffsets_subdomain[nN+1];k++)
3862 weights_subdomain[k] = weight;
3863 }
3864 //
3865 //3. Generate new nodal partition using PETSc interface
3866 //
3867 Mat petscAdjacency;
3868 int nNodes_subdomain_max=0;
3869 MPI_Allreduce(&nNodes_subdomain_old,
3870 &nNodes_subdomain_max,
3871 1,
3872 MPI_INT,
3873 MPI_MAX,
3874 PROTEUS_COMM_WORLD);
3875 if (rank == 0)
3876 std::cout<<"Max nNodes_subdomain "<<nNodes_subdomain_max<<" nNodes_global "<<nNodes_global<<std::endl;
3877 ierr = MatCreateMPIAdj(PROTEUS_COMM_WORLD,
3878 nNodes_subdomain_old,
3879 nNodes_global,
3880 nodeNeighborsOffsets_subdomain,
3881 nodeNeighbors_subdomain,
3882 weights_subdomain,
3883 &petscAdjacency);CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
3884 //const double max_rss_gb(0.75*3.25);//half max mem per core on topaz
3885 const double max_rss_gb(10.0);
3886 ierr = enforceMemoryLimit(PROTEUS_COMM_WORLD, rank, max_rss_gb,"Done allocating MPIAdj");CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
3887 MatPartitioning petscPartition;
3888 ierr = MatPartitioningCreate(PROTEUS_COMM_WORLD,&petscPartition);CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
3889 ierr = MatPartitioningSetAdjacency(petscPartition,petscAdjacency);CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
3890 ierr = MatPartitioningSetFromOptions(petscPartition);CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
3891 ierr = MatPartitioningSetVertexWeights(petscPartition,vertex_weights_subdomain);CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
3892 ierr = MatPartitioningSetPartitionWeights(petscPartition,partition_weights);CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
3893 //get petsc index set that has the new subdomain number for each node
3894 IS nodePartitioningIS_new;
3895 ierr = MatPartitioningApply(petscPartition,&nodePartitioningIS_new);CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
3896 ierr = MatPartitioningDestroy(&petscPartition);CHKERRABORT(PROTEUS_COMM_WORLD, ierr); //gets petscAdjacency too I believe
3897 //ierr = enforceMemoryLimit(PROTEUS_COMM_WORLD, rank, max_rss_gb,"Done applying partition");CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
3898
3899 //determine the number of nodes per subdomain in new partitioning
3900 valarray<int> nNodes_subdomain_new(size);
3901 ISPartitioningCount(nodePartitioningIS_new,size,&nNodes_subdomain_new[0]);
3902
3903 //need new offsets for subdomain to global numbering
3904 valarray<int> nodeOffsets_new(size+1);
3905 nodeOffsets_new[0] = 0;
3906 for (int sdN = 0; sdN < size; sdN++)
3907 {
3908 nodeOffsets_new[sdN+1] = nodeOffsets_new[sdN] + nNodes_subdomain_new[sdN];
3909 }
3910
3911 //get the new node numbers for nodes on this subdomain
3912 IS nodeNumberingIS_subdomain_old2new;
3913 ISPartitioningToNumbering(nodePartitioningIS_new,&nodeNumberingIS_subdomain_old2new);
3914 //
3915 //try out of core
3916 //
3917 /*
3918 * Set up file access property list with parallel I/O access
3919 */
3920 hid_t plist_id = H5Pcreate(H5P_FILE_ACCESS);
3921#ifdef H5_HAVE_PARALLEL
3922 H5Pset_fapl_mpio(plist_id, PROTEUS_COMM_WORLD, MPI_INFO_NULL);
3923#endif
3924
3925 /*
3926 * Create a new file collectively and release property list identifier.
3927 */
3928 const char* H5FILE_NAME("mappings.h5");
3929 hid_t file_id = H5Fcreate(H5FILE_NAME, H5F_ACC_TRUNC, H5P_DEFAULT, plist_id);
3930 H5Pclose(plist_id);
3931
3932
3933 /*
3934 * Create the dataspace for the dataset.
3935 */
3936 hsize_t dimsf[1];
3937 dimsf[0] = nNodes_global;
3938#define RANK 1
3939 hid_t filespace = H5Screate_simple(RANK, dimsf, NULL);
3940
3941 /*
3942 * Create the dataset with default properties and close filespace.
3943 */
3944 hid_t dset_id = H5Dcreate(file_id, "nodeNumbering_old2new", H5T_NATIVE_INT, filespace,
3945 H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
3946 H5Sclose(filespace);
3947
3948 /*
3949 * Each process defines dataset in memory and writes it to the hyperslab
3950 * in the file.
3951 */
3952 hsize_t count[1]; /* hyperslab selection parameters */
3953 hsize_t offset[1];
3954 count[0] = nNodes_subdomain_old;
3955 offset[0] = nodeOffsets_old[rank];
3956 hid_t memspace = H5Screate_simple(RANK, count, NULL);
3957
3958 /*
3959 * Select hyperslab in the file.
3960 */
3961 filespace = H5Dget_space(dset_id);
3962 H5Sselect_hyperslab(filespace, H5S_SELECT_SET, offset, NULL, count, NULL);
3963
3964 /*
3965 * Initialize data buffer
3966 */
3967 // data = (int *) malloc(sizeof(int)*count[0]*count[1]);
3968 // for (i=0; i < count[0]*count[1]; i++) {
3969 // data[i] = mpi_rank + 10;
3970 // }
3971 const PetscInt* data;
3972 ISGetIndices(nodeNumberingIS_subdomain_old2new, &data);
3973
3974 /*
3975 * Create property list for collective dataset write.
3976 */
3977 plist_id = H5Pcreate(H5P_DATASET_XFER);
3978#ifdef H5_HAVE_PARALLEL
3979 H5Pset_dxpl_mpio(plist_id, H5FD_MPIO_COLLECTIVE);
3980#endif
3981
3982 herr_t status = H5Dwrite(dset_id, H5T_NATIVE_INT, memspace, filespace,
3983 plist_id, data);
3984 //free(data);
3985 ISRestoreIndices(nodeNumberingIS_subdomain_old2new, &data);
3986 /*
3987 * Close/release resources.
3988 */
3989 H5Dclose(dset_id);
3990 //Close the write handle here. It used to stay open until the end of the
3991 //function, so the read-only reopen below met a file that was still open
3992 //for write and H5Fopen refused it -- PETSC_ERR_FILE_OPEN, MPI_Abort(65) on
3993 //every rank within 20 s. Closing also flushes, which the read needs.
3994 H5Fclose(file_id);
3995 //
3996 //end try out of core
3997 //
3998 //collect new node numbers for whole mesh so that subdomain reordering and renumbering
3999 //can be done easily
4000
4001 //The ISAllGather that used to build nodeNumbering_global_old2new here is
4002 //gone: it put the whole global node numbering on every rank. The element
4003 //loop below now reads old->new in chunks and keeps only
4004 //nodes_old2new_subdomain_map, the nodes this rank's elements actually touch.
4005 //
4006 //test out of core
4007 //
4008 //(the scratch out-of-core comparison block is gone with the gather it
4009 //compared against)
4010 //
4011 //end test out of core
4012 //
4013 //reverse mapping for node numbers too
4014 //cek hack, not needed
4015 /*
4016 valarray<int> nodeNumbering_global_new2old(nNodes_global);
4017 for (int nN = 0; nN < nNodes_global; nN++)
4018 nodeNumbering_global_new2old[nodeNumbering_global_old2new[nN]] = nN;
4019 */
4020 PetscLogEventEnd(repartition_nodes_event,0,0,0,0);
4021 int receive_element_mask_event;
4022 PetscLogEventRegister("Recv. ele mask",0,&receive_element_mask_event);
4023 PetscLogEventBegin(receive_element_mask_event,0,0,0,0);
4024 //
4025 //4. To build subdomain meshes, go through and collect elements containing
4026 // the locally owned nodes. Assign processor ownership of elements
4027 //
4028 PetscLogEventEnd(receive_element_mask_event,0,0,0,0);
4029 //ierr = enforceMemoryLimit(PROTEUS_COMM_WORLD, rank, max_rss_gb,"Done with masks");CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
4030 int build_subdomains_reread_elements_event;
4031 PetscLogEventRegister("Reread eles",0,&build_subdomains_reread_elements_event);
4032 PetscLogEventBegin(build_subdomains_reread_elements_event,0,0,0,0);
4033 //
4034 //mark the unmarked elements on this subdomain and store element numbers (in old numbering)
4035 //
4036 //We need to find the elements in the support of nodes in the new
4037 //partitioning, so we'll need to re-read the elements file to get
4038 //the the elements for the nodes in the new partitioning. We will be collecting OLD element numbers.
4039 std::ifstream elementFile2(elementFileName.c_str());
4040
4041 if (!elementFile2.good())
4042 {
4043 std::cerr<<"cannot open triangle elements file"
4044 <<elementFileName<<std::endl;
4045 failed = true;
4046 return failed;
4047 }
4048 elementFile2 >> eatcomments >> nElements_global >> nNodesPerSimplex >> hasElementMarkers >> eatline;
4049 assert(nElements_global > 0);
4050 assert(nNodesPerSimplex == simplexDim);
4051 set<int> elements_subdomain_owned;
4052 vector<int> element_nodes_new(3);
4053 int element_nodes_new_array[3];
4054 vector<set<int> > nodeElementsStar(nNodes_subdomain_new[rank]);
4055 vector<set<int> > nodeStarNew(nNodes_subdomain_new[rank]);
4056 map<int,vector<int> > elementNodesArrayMap;
4057 map<int,long int> elementMaterialTypesMap;
4058 map<NodeTuple<2>,ElementNeighbors> elementBoundaryElementsMap;
4059 map<NodeTuple<2>,set<pair<int,int> > > edgeElementsMap;
4060
4061 //note any element index containers are in the old element numbering
4062 //
4063 //Out-of-core node numbering, mirroring partitionNodesFromTetgenFiles.
4064 //
4065 //This loop used to index nodeNumbering_global_old2new, a global array that
4066 //ISAllGather had put on EVERY rank. For a 2D mesh with N nodes that is N
4067 //ints per rank here, and the element, elementBoundary and edge numberings
4068 //add roughly 13N more -- so per-rank memory did not fall as ranks were
4069 //added. Measured on damBreak (569508 nodes): peak per core was FLAT at
4070 //0.26 GB from 384 to 768 ranks while the subdomain halved.
4071 //
4072 //Instead, accumulate a chunk of elements, read old->new for just the nodes
4073 //that chunk touches, process it, and release. The chunk is capped by NODE
4074 //COUNT, not by nElements_global/size, so the number of chunks does not grow
4075 //with the rank count -- the mistake 77c99396 made in the tetgen path.
4076 const int nNodes_collection_max=10000;
4077 set<int> node_collection;
4078 vector<int> elements_collection;
4079 vector<valarray<int> > element_old_nodes_collection;
4080 vector<double> elementId_collection;
4081 map<int,int> nodes_old2new_subdomain_map;
4082 int eN_c_start = 0;
4083 file_id = openMappingsReadOnly(PROTEUS_COMM_WORLD, H5FILE_NAME);
4084 if (file_id < 0)
4085 {
4086 H5Eprint2(H5E_DEFAULT, stderr);
4087 SETERRABORT(PROTEUS_COMM_WORLD, PETSC_ERR_FILE_OPEN,
4088 "could not reopen the mappings file; see the HDF5 stack above");
4089 }
4090 for (int ie = 0; ie < nElements_global; ie++)
4091 {
4092 int ne, nv;
4093 long double elementId_double;
4094 elementFile2 >> eatcomments >> ne;
4095 ne -= indexBase;
4096 assert(0 <= ne && ne < nElements_global && elementFile2.good());
4097 elements_collection.push_back(ne);
4098 valarray<int> enodes_PROBE(simplexDim);
4099 for (int iv = 0; iv < simplexDim; iv++)
4100 {
4101 elementFile2 >> nv;
4102 nv -= indexBase;
4103 assert(0 <= nv && nv < nNodes_global);
4104 node_collection.insert(nv);
4105 enodes_PROBE[iv] = nv;
4106 }
4107 element_old_nodes_collection.push_back(enodes_PROBE);
4108 //read the marker unconditionally: the stream position must not depend on
4109 //whether this element turns out to be in the subdomain
4110 if (hasElementMarkers > 0)
4111 {
4112 elementFile2 >> elementId_double;
4113 elementId_collection.push_back(static_cast<double>(elementId_double));
4114 }
4115 elementFile2 >> eatline;
4116
4117 if (node_collection.size() >= (size_t)nNodes_collection_max || ie == nElements_global-1)
4118 {
4119 //one point-selection read for the whole chunk
4120 valarray<hsize_t> node_collection_array(node_collection.size());
4121 valarray<int> nodes_old2new_subset(node_collection.size());
4122 int i_nc=0;
4123 for (auto nvp=node_collection.begin();nvp!=node_collection.end();nvp++,i_nc++)
4124 node_collection_array[i_nc] = static_cast<hsize_t>(*nvp);
4125 hid_t dset_id_c = H5Dopen1(file_id,"/nodeNumbering_old2new");
4126 if (dset_id_c < 0)
4127 {
4128 H5Eprint2(H5E_DEFAULT, stderr);
4129 SETERRABORT(PROTEUS_COMM_WORLD, PETSC_ERR_FILE_READ,
4130 "could not open /nodeNumbering_old2new");
4131 }
4132 hid_t fspace_c = H5Dget_space(dset_id_c);
4133 herr_t st_c = H5Sselect_elements(fspace_c, H5S_SELECT_SET,
4134 node_collection.size(), &node_collection_array[0]);
4135 hsize_t dims_c[] = {static_cast<hsize_t>(node_collection.size())};
4136 hid_t mspace_c = H5Screate_simple(1, dims_c, NULL);
4137 herr_t rd_c = H5Dread(dset_id_c, H5T_NATIVE_INT, mspace_c, fspace_c,
4138 H5P_DEFAULT, &nodes_old2new_subset[0]);
4139 if (st_c < 0 || rd_c < 0)
4140 {
4141 H5Eprint2(H5E_DEFAULT, stderr);
4142 SETERRABORT(PROTEUS_COMM_WORLD, PETSC_ERR_FILE_READ,
4143 "could not read /nodeNumbering_old2new for this chunk");
4144 }
4145 H5Sclose(mspace_c); H5Sclose(fspace_c); H5Dclose(dset_id_c);
4146 map<int,int> nodes_old2new_subset_map;
4147 for (size_t i=0;i<node_collection.size();i++)
4148 nodes_old2new_subset_map[node_collection_array[i]] = nodes_old2new_subset[i];
4149
4150 for (int eN_c = eN_c_start; eN_c <= ie; eN_c++)
4151 {
4152 const int ne = elements_collection[eN_c-eN_c_start];
4153 long int elementId(0);
4154 for (int iv = 0; iv < simplexDim; iv++)
4155 {
4156 element_nodes_old[iv] = element_old_nodes_collection[eN_c-eN_c_start][iv];
4157 element_nodes_new[iv] = nodes_old2new_subset_map[element_nodes_old[iv]];
4158 element_nodes_new_array[iv] = element_nodes_new[iv];
4159 }
4160 NodeTuple<simplexDim> nodeTuple(element_nodes_new_array);
4161 for (int iv = 0; iv < simplexDim; iv++)
4162 {
4163 int nN_star_new = element_nodes_new[iv];
4164 bool inSubdomain=false;
4165 if (nN_star_new >= nodeOffsets_new[rank] && nN_star_new < nodeOffsets_new[rank+1])
4166 {
4167 inSubdomain = true;
4168 //add all the element boundaries of this element
4169 for (int ebN=0;ebN < simplexDim ; ebN++)
4170 {
4171 int nodes[simplexDim-1] = { element_nodes_new[(ebN+1) % simplexDim],
4172 element_nodes_new[(ebN+2) % simplexDim]};
4173 NodeTuple<simplexDim-1> nodeTuple(nodes);
4174 if(elementBoundaryElementsMap.find(nodeTuple) != elementBoundaryElementsMap.end())
4175 {
4176 if (elementBoundaryElementsMap[nodeTuple].right == -1 && ne != elementBoundaryElementsMap[nodeTuple].left)
4177 {
4178 elementBoundaryElementsMap[nodeTuple].right=ne;
4179 elementBoundaryElementsMap[nodeTuple].right_ebN_element=ebN;
4180 }
4181 }
4182 else
4183 {
4184 elementBoundaryElementsMap[nodeTuple] = ElementNeighbors(ne,ebN);
4185 }
4186 }
4187 //add all the edges of this element
4188 for (int nNL=0,edN=0;nNL < simplexDim ; nNL++)
4189 for(int nNR=nNL+1;nNR < simplexDim;nNR++,edN++)
4190 {
4191 int nodes[2] = { element_nodes_new[nNL],
4192 element_nodes_new[nNR]};
4193 NodeTuple<2> nodeTuple(nodes);
4194 edgeElementsMap[nodeTuple].insert(pair<int,int>(ne,edN));
4195 }
4196 //add all the nodes to the node star
4197 int nN_star_new_subdomain = nN_star_new - nodeOffsets_new[rank];
4198 nodeElementsStar[nN_star_new_subdomain].insert(ne);
4199 for (int jv = 0; jv < simplexDim; jv++)
4200 {
4201 if (iv != jv)
4202 {
4203 int nN_point_new = element_nodes_new[jv];
4204 nodeStarNew[nN_star_new_subdomain].insert(nN_point_new);
4205 }
4206 }
4207 }
4208 if (inSubdomain)
4209 {
4210 elementNodesArrayMap[ne] = element_nodes_new;
4211 //the later .poly/.edge readers need old->new for exactly these
4212 //nodes, which is what replaces the global ISAllGather
4213 for (int jv = 0; jv < simplexDim; jv++)
4214 nodes_old2new_subdomain_map[element_nodes_old[jv]] = element_nodes_new[jv];
4215 }
4216 }
4217 if (elementNodesArrayMap.find(ne) != elementNodesArrayMap.end())//this element contains a node owned by this subdomain
4218 {
4219 if (nodeTuple.nodes[1] >= nodeOffsets_new[rank] && nodeTuple.nodes[1] < nodeOffsets_new[rank+1])
4220 elements_subdomain_owned.insert(ne);
4221 if (hasElementMarkers > 0)
4222 {
4223 elementId = static_cast<long int>(elementId_collection[eN_c-eN_c_start]);
4224 elementMaterialTypesMap[ne] = elementId;
4225 }
4226 }
4227 }
4228 eN_c_start = ie+1;
4229 node_collection.clear();
4230 elements_collection.clear();
4231 elementId_collection.clear();
4232 element_old_nodes_collection.clear();
4233 }
4234 }
4235 H5Fclose(file_id);
4236 elementFile2.close();
4237 int nElements_owned_subdomain(elements_subdomain_owned.size()),
4238 nElements_owned_new=0;
4239 MPI_Allreduce(&nElements_owned_subdomain,&nElements_owned_new,1,MPI_INT,MPI_SUM,PROTEUS_COMM_WORLD);
4240 assert(nElements_owned_new == nElements_global);
4241 PetscLogEventEnd(build_subdomains_reread_elements_event,0,0,0,0);
4242 int build_subdomains_send_marked_elements_event;
4243 PetscLogEventRegister("Mark/send eles",0,&build_subdomains_send_marked_elements_event);
4244 PetscLogEventBegin(build_subdomains_send_marked_elements_event,0,0,0,0);
4245 //ierr = enforceMemoryLimit(PROTEUS_COMM_WORLD, rank, max_rss_gb,"Done marking elements");CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
4246 //
4247 //done with the element file
4248 //
4249 //construct compact nodeElementsArray
4250 valarray<int> nodeElementOffsets(nNodes_subdomain_new[rank]+1);
4251 nodeElementOffsets[0] = 0;
4252 for (int nN = 0; nN < nNodes_subdomain_new[rank]; nN++)
4253 nodeElementOffsets[nN+1] = nodeElementOffsets[nN]+nodeElementsStar[nN].size();
4254 valarray<int> nodeElementsArray(nodeElementOffsets[nNodes_subdomain_new[rank]]);
4255 for (int nN=0,offset=0; nN < nNodes_subdomain_new[rank]; nN++)
4256 {
4257 for (set<int>::iterator eN_star = nodeElementsStar[nN].begin(); eN_star != nodeElementsStar[nN].end();
4258 eN_star++,offset++)
4259 {
4260 nodeElementsArray[offset] = *eN_star;
4261 }
4262 }
4263 //construct compact nodeStarArray
4264 valarray<int> nodeStarOffsetsNew(nNodes_subdomain_new[rank]+1);
4265 nodeStarOffsetsNew[0] = 0;
4266 for (int nN=1;nN<nNodes_subdomain_new[rank]+1;nN++)
4267 nodeStarOffsetsNew[nN] = nodeStarOffsetsNew[nN-1] + nodeStarNew[nN-1].size();
4268 valarray<int> nodeStarArrayNew(nodeStarOffsetsNew[nNodes_subdomain_new[rank]]);
4269 for (int nN=0,offset=0;nN<nNodes_subdomain_new[rank];nN++)
4270 {
4271 for (set<int>::iterator nN_star=nodeStarNew[nN].begin();nN_star!=nodeStarNew[nN].end();nN_star++,offset++)
4272 {
4273 nodeStarArrayNew[offset] = *nN_star;
4274 }
4275 }
4276 PetscLogEventEnd(build_subdomains_send_marked_elements_event,0,0,0,0);
4277 int build_subdomains_global_numbering_elements_event;
4278 PetscLogEventRegister("Global ele nmbr",0,&build_subdomains_global_numbering_elements_event);
4279 PetscLogEventBegin(build_subdomains_global_numbering_elements_event,0,0,0,0);
4280 //
4281 //5. Generate global element numbering corresponding to new subdomain ownership
4282 //
4283 valarray<int> nElements_subdomain_new(size),
4284 elementOffsets_new(size+1);
4285 for (int sdN = 0; sdN < size; sdN++)
4286 {
4287 if (sdN == rank)
4288 nElements_subdomain_new[sdN] = int(elements_subdomain_owned.size());
4289 else
4290 nElements_subdomain_new[sdN] = 0;
4291 }
4292 valarray<int> nElements_subdomain_new_send = nElements_subdomain_new;
4293 MPI_Allreduce(&nElements_subdomain_new_send[0],&nElements_subdomain_new[0],size,MPI_INT,MPI_SUM,PROTEUS_COMM_WORLD);
4294 //construct new offsets for elements
4295 elementOffsets_new[0] = 0;
4296 for (int sdN = 0; sdN < size; sdN++)
4297 elementOffsets_new[sdN+1] = elementOffsets_new[sdN] + nElements_subdomain_new[sdN];
4298 //map to old element numbering
4299 valarray<int> elementNumbering_subdomain_new2old(elements_subdomain_owned.size());
4300 set<int>::iterator eN_ownedp = elements_subdomain_owned.begin();
4301 for (int eN = 0; eN < int(elements_subdomain_owned.size()); eN++,eN_ownedp++)
4302 {
4303 elementNumbering_subdomain_new2old[eN] = *eN_ownedp;
4304 }
4305 //use Petsc IS to get global new2old numbering
4306 IS elementNumberingIS_subdomain_new2old;
4307 ISCreateGeneral(PROTEUS_COMM_WORLD,elements_subdomain_owned.size(),&elementNumbering_subdomain_new2old[0],PETSC_COPY_VALUES,
4308 &elementNumberingIS_subdomain_new2old);
4309 IS elementNumberingIS_global_new2old;
4310 ISAllGather(elementNumberingIS_subdomain_new2old,&elementNumberingIS_global_new2old);
4311
4312 const PetscInt *elementNumbering_global_new2old;//needs to be restored
4313 ISGetIndices(elementNumberingIS_global_new2old,&elementNumbering_global_new2old);
4314 //construct reverse mapping
4315 valarray<int> elementNumbering_global_old2new(nElements_global);
4316 for (int eN = 0; eN < nElements_global; eN++)
4317 {
4318 elementNumbering_global_old2new[elementNumbering_global_new2old[eN]] = eN;
4319 }
4320 PetscLogEventEnd(build_subdomains_global_numbering_elements_event,0,0,0,0);
4321 //ierr = enforceMemoryLimit(PROTEUS_COMM_WORLD, rank, max_rss_gb,"Done allocating element numbering new2old/old2new");CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
4322 int build_subdomains_faces_event;
4323 PetscLogEventRegister("Subd faces",0,&build_subdomains_faces_event);
4324 PetscLogEventBegin(build_subdomains_faces_event,0,0,0,0);
4325 //
4326 //4b,5b. repeat process to build global face (elementBoundary) numbering
4327 //
4328 //first read element boundaries to create nodeElementBoundariesArray
4329 //for all element boundaries on this subdomain, which we'll use to
4330 //grab element boundaries from the bit array
4331
4332
4333 //In 2D, the element boundaries are edges and not faces.
4334 //nodeElementBoundariesArray maps an element
4335
4336 std::ifstream elementBoundaryFile(elementBoundaryFileName.c_str());
4337
4338 if (!elementBoundaryFile.good())
4339 {
4340 std::cerr<<"cannot open triangle edge file "
4341 <<elementBoundaryFileName<<std::endl;
4342 failed = true;
4343 return failed;
4344 }
4345
4346 bool hasElementBoundaryMarkers = false;
4347 int nElementBoundaries_global;
4348 int ihasElementBoundaryMarkers(0);
4349
4350 elementBoundaryFile >> eatcomments >> nElementBoundaries_global >>ihasElementBoundaryMarkers >> eatline ;
4351 assert(nElementBoundaries_global > 0);
4352 if (ihasElementBoundaryMarkers > 0)
4353 {
4354 hasElementBoundaryMarkers = true;
4355 }
4356 newMesh.nElementBoundaries_global = nElementBoundaries_global;
4357 //note, these will be in the new element numbering
4358 set<int> elementBoundaries_subdomain_owned;
4359 vector<set<int> > nodeElementBoundariesStar(nNodes_subdomain_new[rank]);
4360 map<int,int> elementBoundaryMaterialTypesMap;
4361 map<int,vector<int> > elementBoundariesMap;
4362 set<int> supportedElementBoundaries;
4363 for (int ieb = 0; ieb < nElementBoundaries_global; ieb++)
4364 {
4365 int neb,nn0,nn1; int ebId(0);
4366 elementBoundaryFile >> eatcomments >> neb >> nn0 >> nn1;
4367 if (ihasElementBoundaryMarkers > 0)
4368 elementBoundaryFile >> ebId;
4369 neb -= indexBase;
4370 nn0 -= indexBase;
4371 nn1 -= indexBase;
4372 assert(0 <= neb && neb < nElementBoundaries_global && elementBoundaryFile.good());
4373 //grab the element boundaries for the node if the node is owned by the subdomain
4374 //this will miss the element boundaries on the "outside boundary" of the star, which will grab later
4375 int nn0_new = -1;
4376 if (nodes_old2new_subdomain_map.find(nn0) != nodes_old2new_subdomain_map.end())
4377 nn0_new = nodes_old2new_subdomain_map[nn0];
4378 if (nn0_new >= nodeOffsets_new[rank] && nn0_new < nodeOffsets_new[rank+1])
4379 {
4380 nodeElementBoundariesStar[nn0_new-nodeOffsets_new[rank]].insert(neb);
4381 supportedElementBoundaries.insert(neb);
4382 }
4383 int nn1_new = -1;
4384 if (nodes_old2new_subdomain_map.find(nn1) != nodes_old2new_subdomain_map.end())
4385 nn1_new = nodes_old2new_subdomain_map[nn1];
4386 if (nn1_new >= nodeOffsets_new[rank] && nn1_new < nodeOffsets_new[rank+1])
4387 {
4388 nodeElementBoundariesStar[nn1_new-nodeOffsets_new[rank]].insert(neb);
4389 supportedElementBoundaries.insert(neb);
4390 }
4391 int nodes[2] = {nn0_new,nn1_new};
4392 NodeTuple<2> nodeTuple(nodes);
4393 elementBoundaryFile >> eatline;
4394 if (elementBoundaryElementsMap.find(nodeTuple) != elementBoundaryElementsMap.end())//this element boundary is on an element in the subdomain
4395 {
4396 if (nodeTuple.nodes[1] >= nodeOffsets_new[rank] && nodeTuple.nodes[1] < nodeOffsets_new[rank+1])
4397 elementBoundaries_subdomain_owned.insert(neb);
4398 if (ihasElementBoundaryMarkers > 0)
4399 elementBoundaryMaterialTypesMap[neb]=ebId;
4400 int eN_left = elementNumbering_global_old2new[elementBoundaryElementsMap[nodeTuple].left];
4401 if (elementBoundariesMap.find(eN_left) != elementBoundariesMap.end())
4402 {
4403 elementBoundariesMap[eN_left][elementBoundaryElementsMap[nodeTuple].left_ebN_element] = neb;
4404 }
4405 else
4406 {
4407 //initialize
4408 vector<int> elementBoundaries_element(3,-1);
4409 elementBoundariesMap[eN_left] = elementBoundaries_element;
4410 //assign
4411 elementBoundariesMap[eN_left][elementBoundaryElementsMap[nodeTuple].left_ebN_element] = neb;
4412 }
4413 if (elementBoundaryElementsMap[nodeTuple].right >= 0)
4414 {
4415 int eN_right = elementNumbering_global_old2new[elementBoundaryElementsMap[nodeTuple].right];
4416 if (elementBoundariesMap.find(eN_right) != elementBoundariesMap.end())
4417 {
4418 elementBoundariesMap[eN_right][elementBoundaryElementsMap[nodeTuple].right_ebN_element] = neb;
4419 }
4420 else
4421 {
4422 //initialize
4423 vector<int> elementBoundaries_element(3,-1);
4424 elementBoundariesMap[eN_right] = elementBoundaries_element;
4425 //assign
4426 elementBoundariesMap[eN_right][elementBoundaryElementsMap[nodeTuple].right_ebN_element] = neb;
4427 }
4428 }
4429 }
4430 }
4431 elementBoundaryElementsMap.clear();
4432 //done reading element boundaries
4433 elementBoundaryFile.close();
4434 //ierr = enforceMemoryLimit(PROTEUS_COMM_WORLD, rank, max_rss_gb,"Done reading element boundaries");CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
4435 int nElementBoundaries_owned_subdomain=elementBoundaries_subdomain_owned.size(),
4436 nElementBoundaries_owned_new=0;
4437 MPI_Allreduce(&nElementBoundaries_owned_subdomain,&nElementBoundaries_owned_new,1,MPI_INT,MPI_SUM,PROTEUS_COMM_WORLD);
4438 assert(nElementBoundaries_owned_new == nElementBoundaries_global);
4439
4440 //now get the element boundaries on the outside of the star
4441 for (map<int,vector<int> >::iterator elementBoundariesp=elementBoundariesMap.begin();
4442 elementBoundariesp!=elementBoundariesMap.end();
4443 elementBoundariesp++)
4444 {
4445 //loop over the nodes of this element for the owned nodes
4446 for (int iv=0;iv<simplexDim;iv++)
4447 {
4448 //the elementNodesArrayMap is in the old element numbering while the elementBoundariesMap is in the new element numbering
4449 int nN_global = elementNodesArrayMap[elementNumbering_global_new2old[elementBoundariesp->first]][iv];
4450 if (nN_global >= nodeOffsets_new[rank] && nN_global < nodeOffsets_new[rank+1])
4451 {
4452 //add all the faces to this node star
4453 for(int eb=0;eb<simplexDim;eb++)
4454 {
4455 nodeElementBoundariesStar[nN_global-nodeOffsets_new[rank]].insert(elementBoundariesp->second[eb]);
4456 }
4457 }
4458 }
4459 }
4460 //build compact structures for nodeElementBoundariesArray
4461 valarray<int> nodeElementBoundaryOffsets(nNodes_subdomain_new[rank]+1);
4462 nodeElementBoundaryOffsets[0] = 0;
4463 for (int nN = 0; nN < nNodes_subdomain_new[rank]; nN++)
4464 nodeElementBoundaryOffsets[nN+1] = nodeElementBoundaryOffsets[nN]+nodeElementBoundariesStar[nN].size();
4465 valarray<int> nodeElementBoundariesArray(nodeElementBoundaryOffsets[nNodes_subdomain_new[rank]]);
4466 for (int nN=0,offset=0; nN < nNodes_subdomain_new[rank]; nN++)
4467 {
4468 for (set<int>::iterator ebN_star = nodeElementBoundariesStar[nN].begin(); ebN_star != nodeElementBoundariesStar[nN].end();
4469 ebN_star++,offset++)
4470 {
4471 nodeElementBoundariesArray[offset] = *ebN_star;
4472 }
4473 }
4474
4475 //get the number of elementBoundaries owned on each processor
4476 valarray<int> nElementBoundaries_subdomain_new(size),
4477 elementBoundaryOffsets_new(size+1);
4478 for (int sdN=0;sdN<size;sdN++)
4479 if (sdN == rank)
4480 nElementBoundaries_subdomain_new[sdN] = elementBoundaries_subdomain_owned.size();
4481 else
4482 nElementBoundaries_subdomain_new[sdN] = 0;
4483 valarray<int> nElementBoundaries_subdomain_new_send=nElementBoundaries_subdomain_new;
4484 MPI_Allreduce(&nElementBoundaries_subdomain_new_send[0],&nElementBoundaries_subdomain_new[0],size,MPI_INT,MPI_SUM,PROTEUS_COMM_WORLD);
4485 elementBoundaryOffsets_new[0] = 0;
4486 for (int sdN=0;sdN<size;sdN++)
4487 elementBoundaryOffsets_new[sdN+1] = elementBoundaryOffsets_new[sdN]+nElementBoundaries_subdomain_new[sdN];
4488 //
4489 //Now as with elements and nodes build a global face numbering
4490 //resetting the face-based information is a little different since much of this is currently built below based
4491 //on the element and node information
4492 //
4493 valarray<int> elementBoundaryNumbering_new2old(elementBoundaries_subdomain_owned.size());
4494 set<int>::iterator ebN_ownedp=elementBoundaries_subdomain_owned.begin();
4495 for (int ebN=0;ebN<int(elementBoundaries_subdomain_owned.size());ebN++)
4496 {
4497 elementBoundaryNumbering_new2old[ebN] = *ebN_ownedp++;
4498 }
4499 IS elementBoundaryNumberingIS_subdomain_new2old;
4500 ISCreateGeneral(PROTEUS_COMM_WORLD,elementBoundaries_subdomain_owned.size(),&elementBoundaryNumbering_new2old[0],PETSC_COPY_VALUES,&elementBoundaryNumberingIS_subdomain_new2old);
4501 IS elementBoundaryNumberingIS_global_new2old;
4502 ISAllGather(elementBoundaryNumberingIS_subdomain_new2old,&elementBoundaryNumberingIS_global_new2old);
4503 const PetscInt *elementBoundaryNumbering_global_new2old;
4504 valarray<int> elementBoundaryNumbering_global_old2new(newMesh.nElementBoundaries_global);
4505 ISGetIndices(elementBoundaryNumberingIS_global_new2old,&elementBoundaryNumbering_global_new2old);
4506 //ierr = enforceMemoryLimit(PROTEUS_COMM_WORLD, rank, max_rss_gb,"Allocating elementBoudnary old2new/new2old");CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
4507 for (int ebN=0;ebN<newMesh.nElementBoundaries_global;ebN++)
4508 {
4509 elementBoundaryNumbering_global_old2new[elementBoundaryNumbering_global_new2old[ebN]] = ebN;
4510 }
4511 ISRestoreIndices(elementBoundaryNumberingIS_global_new2old,&elementBoundaryNumbering_global_new2old);
4512 ISDestroy(&elementBoundaryNumberingIS_subdomain_new2old);
4513 ISDestroy(&elementBoundaryNumberingIS_global_new2old);
4514 PetscLogEventEnd(build_subdomains_faces_event,0,0,0,0);
4515 //ierr = enforceMemoryLimit(PROTEUS_COMM_WORLD, rank, max_rss_gb,"Done allocating elementBoudnary old2new/new2old");CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
4516 int build_subdomains_edges_event;
4517 PetscLogEventRegister("Subd edges",0,&build_subdomains_edges_event);
4518 PetscLogEventBegin(build_subdomains_edges_event,0,0,0,0);
4519
4520 //
4521 //4c,5c. Repeate the process for edges
4522 //
4523 std::ifstream edgeFile(edgeFileName.c_str());
4524
4525 if (!edgeFile.good())
4526 {
4527 std::cerr<<"cannot open Triangle edge file"
4528 <<edgeFileName<<std::endl;
4529 failed = true;
4530 return failed;
4531 }
4532
4533 bool hasEdgeMarkers = false;
4534 int nEdges_global;
4535 int ihasEdgeMarkers(0);
4536 edgeFile >> eatcomments >> nEdges_global >> eatline;// edge file doesn currently contain markers >> ihasEdgeMarkers >> eatline ;
4537 assert(nEdges_global > 0);
4538 if (ihasEdgeMarkers > 0)
4539 {
4540 hasEdgeMarkers = true;
4541 }
4542
4543 newMesh.nEdges_global = nEdges_global;
4544 set<int> edges_subdomain_owned;
4545 vector<set<int> > nodeEdgesStar(nNodes_subdomain_new[rank]);
4546 map<int,int> edgeMaterialTypesMap;
4547 map<int,vector<int> > elementEdgesMap;
4548 map<int,pair<int,int> > edgeNodesMap;
4549 set<int> supportedEdges;
4550 for (int ied = 0; ied < nEdges_global; ied++)
4551 {
4552 int ned,nn0,nn1; int edId(0);
4553 edgeFile >> eatcomments >> ned >> nn0 >> nn1;
4554 if (ihasEdgeMarkers > 0)
4555 edgeFile >> edId;
4556 ned -= indexBase;
4557 nn0 -= indexBase;
4558 nn1 -= indexBase;
4559 assert(0 <= ned && ned < nEdges_global && edgeFile.good());
4560 int nn0_new = -1;
4561 if (nodes_old2new_subdomain_map.find(nn0) != nodes_old2new_subdomain_map.end())
4562 nn0_new = nodes_old2new_subdomain_map[nn0];
4563 if (nn0_new >= nodeOffsets_new[rank] && nn0_new < nodeOffsets_new[rank+1])
4564 {
4565 nodeEdgesStar.at(nn0_new-nodeOffsets_new[rank]).insert(ned);
4566 supportedEdges.insert(ned);
4567 }
4568 int nn1_new = -1;
4569 if (nodes_old2new_subdomain_map.find(nn1) != nodes_old2new_subdomain_map.end())
4570 nn1_new = nodes_old2new_subdomain_map[nn1];
4571 if (nn1_new >= nodeOffsets_new[rank] && nn1_new < nodeOffsets_new[rank+1])
4572 {
4573 nodeEdgesStar.at(nn1_new-nodeOffsets_new[rank]).insert(ned);
4574 supportedEdges.insert(ned);
4575 }
4576 int nodes[2] = {nn0_new,nn1_new};
4577 NodeTuple<2> nodeTuple(nodes);
4578 edgeFile >> eatline;
4579 if (edgeElementsMap.find(nodeTuple) != edgeElementsMap.end())//this edge is on an element in the subdomain
4580 {
4581 if (nodeTuple.nodes[0] >= nodeOffsets_new[rank] && nodeTuple.nodes[0] < nodeOffsets_new[rank+1])
4582 edges_subdomain_owned.insert(ned);
4583 //pick up all the edges on the subdomain and store their nodes
4584 edgeNodesMap[ned].first = nodeTuple.nodes[0];
4585 edgeNodesMap[ned].second = nodeTuple.nodes[1];
4586 if (ihasEdgeMarkers > 0)
4587 edgeMaterialTypesMap[ned]=edId;
4588 for (set<pair<int,int> >::iterator elementp=edgeElementsMap[nodeTuple].begin();
4589 elementp != edgeElementsMap[nodeTuple].end();
4590 elementp++)
4591 {
4592 int eN = elementNumbering_global_old2new[elementp->first];
4593 if (elementEdgesMap.find(eN) != elementEdgesMap.end())
4594 {
4595 elementEdgesMap[eN][elementp->second] = ned;
4596 }
4597 else
4598 {
4599 std::vector<int> init(3,-1);
4600 elementEdgesMap[eN] = init;
4601 elementEdgesMap[eN][elementp->second] = ned;
4602 }
4603 }
4604 }
4605 }//end iv
4606 edgeElementsMap.clear();
4607 edgeFile.close();
4608 int nEdges_owned_subdomain=edges_subdomain_owned.size(),
4609 nEdges_owned_new=0;
4610 MPI_Allreduce(&nEdges_owned_subdomain,&nEdges_owned_new,1,MPI_INT,MPI_SUM,PROTEUS_COMM_WORLD);
4611 assert(nEdges_owned_new == nEdges_global);
4612 //ierr = enforceMemoryLimit(PROTEUS_COMM_WORLD, rank, max_rss_gb,"Done reading edges");CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
4613 //done with edge file
4614 //
4615 //just as with faces, we need to add edges along outer boundaries of star
4616 //not sure if we need to collect nodeEdges star above anymore, since we're doing this
4617 //
4618 for (map<int,vector<int> >::iterator edgesp=elementEdgesMap.begin();
4619 edgesp!=elementEdgesMap.end();
4620 edgesp++)
4621 {
4622 //loop over the nodes of this element for the owned nodes
4623 for (int iv=0;iv<simplexDim;iv++)
4624 {
4625 //the elementNodesArrayMap is in the old elemetn numbering while the elementEdgesMap is in the new element numbering
4626 int nN_global = elementNodesArrayMap[elementNumbering_global_new2old[edgesp->first]][iv];
4627 if (nN_global >= nodeOffsets_new[rank] && nN_global < nodeOffsets_new[rank+1])
4628 {
4629 //add all the edges to this node star
4630 for(int ed=0;ed<3;ed++)
4631 {
4632 nodeEdgesStar.at(nN_global-nodeOffsets_new[rank]).insert(edgesp->second[ed]);
4633 }
4634 }
4635 }
4636 }
4637 //build compact data structures for nodeEdgesArray
4638 valarray<int> nodeEdgeOffsets(nNodes_subdomain_new[rank]+1);
4639 nodeEdgeOffsets[0] = 0;
4640 for (int nN = 0; nN < nNodes_subdomain_new[rank]; nN++)
4641 nodeEdgeOffsets[nN+1] = nodeEdgeOffsets[nN]+nodeEdgesStar.at(nN).size();
4642 valarray<int> nodeEdgesArray(nodeEdgeOffsets[nNodes_subdomain_new[rank]]);
4643 for (int nN=0,offset=0; nN < nNodes_subdomain_new[rank]; nN++)
4644 {
4645 for (set<int>::iterator edN_star = nodeEdgesStar.at(nN).begin();
4646 edN_star != nodeEdgesStar.at(nN).end();
4647 edN_star++,offset++)
4648 {
4649 nodeEdgesArray[offset] = *edN_star;
4650 }
4651 }
4652 newMesh.subdomainp->nEdges_global = edgeNodesMap.size();
4653 //get the number of edges on each processor
4654 valarray<int> nEdges_subdomain_new(size),
4655 edgeOffsets_new(size+1);
4656 for (int sdN=0;sdN<size;sdN++)
4657 if (sdN == rank)
4658 nEdges_subdomain_new[sdN] = edges_subdomain_owned.size();
4659 else
4660 nEdges_subdomain_new[sdN] = 0;
4661 valarray<int> nEdges_subdomain_new_send=nEdges_subdomain_new;
4662 MPI_Allreduce(&nEdges_subdomain_new_send[0],&nEdges_subdomain_new[0],size,MPI_INT,MPI_SUM,PROTEUS_COMM_WORLD);
4663 //
4664 //construct new offsets for owned edges
4665 //
4666 edgeOffsets_new[0] = 0;
4667 for (int sdN=0;sdN<size;sdN++)
4668 edgeOffsets_new[sdN+1] = edgeOffsets_new[sdN]+nEdges_subdomain_new[sdN];
4669 //
4670 //Now as with elementBoundaries, build a global face numbering
4671 //resetting the edge based information is a little different since much of this is currently built below based
4672 //on the element and node information
4673 //
4674 valarray<int> edgeNumbering_new2old(edges_subdomain_owned.size());
4675 set<int>::iterator edN_ownedp=edges_subdomain_owned.begin();
4676 for (int edN=0;edN<int(edges_subdomain_owned.size());edN++,edN_ownedp++)
4677 {
4678 edgeNumbering_new2old[edN] = *edN_ownedp;
4679 }
4680 IS edgeNumberingIS_subdomain_new2old;
4681 ISCreateGeneral(PROTEUS_COMM_WORLD,edges_subdomain_owned.size(),&edgeNumbering_new2old[0],PETSC_COPY_VALUES,&edgeNumberingIS_subdomain_new2old);
4682 IS edgeNumberingIS_global_new2old;
4683 ISAllGather(edgeNumberingIS_subdomain_new2old,&edgeNumberingIS_global_new2old);
4684 const PetscInt *edgeNumbering_global_new2old;
4685 valarray<int> edgeNumbering_global_old2new(newMesh.nEdges_global);
4686 ISGetIndices(edgeNumberingIS_global_new2old,&edgeNumbering_global_new2old);
4687 //ierr = enforceMemoryLimit(PROTEUS_COMM_WORLD, rank, max_rss_gb,"Setting edgeNumering old2new/new2old");CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
4688 for (int edN=0;edN<newMesh.nEdges_global;edN++)
4689 {
4690 edgeNumbering_global_old2new[edgeNumbering_global_new2old[edN]] = edN;
4691 }
4692 ISRestoreIndices(edgeNumberingIS_global_new2old,&edgeNumbering_global_new2old);
4693 ISDestroy(&edgeNumberingIS_subdomain_new2old);
4694 ISDestroy(&edgeNumberingIS_global_new2old);
4695 //ierr = enforceMemoryLimit(PROTEUS_COMM_WORLD, rank, max_rss_gb,"Done allocating edgeNumering old2new/new2old");CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
4696 //
4697 //6. Figure out what is in the node stars but not locally owned, create ghost information
4698 //
4699
4700 set<int> elements_overlap,nodes_overlap,elementBoundaries_overlap,edges_overlap;
4701 for (int nN = 0; nN < nNodes_subdomain_new[rank]; nN++)
4702 {
4703 //nodes
4704 for (int offset = nodeStarOffsetsNew[nN];offset<nodeStarOffsetsNew[nN+1];offset++)
4705 {
4706 int nN_point_global = nodeStarArrayNew[offset];
4707 bool offproc = nN_point_global < nodeOffsets_new[rank] || nN_point_global >= nodeOffsets_new[rank+1];
4708 if (offproc)
4709 nodes_overlap.insert(nN_point_global);
4710 }
4711 //elements
4712 for (int eN_star_offset = nodeElementOffsets[nN];
4713 eN_star_offset < nodeElementOffsets[nN+1]; eN_star_offset++)
4714 {
4715 int eN_star_old = nodeElementsArray[eN_star_offset];
4716 int eN_star_new = elementNumbering_global_old2new[eN_star_old];
4717 bool offproc = eN_star_new >= elementOffsets_new[rank+1] || eN_star_new < elementOffsets_new[rank];
4718 if (offproc)
4719 elements_overlap.insert(eN_star_new);
4720 }
4721 //element boundaries
4722 for (int ebN_star_offset = nodeElementBoundaryOffsets[nN];
4723 ebN_star_offset < nodeElementBoundaryOffsets[nN+1]; ebN_star_offset++)
4724 {
4725 int ebN_star_old = nodeElementBoundariesArray[ebN_star_offset];
4726 int ebN_star_new = elementBoundaryNumbering_global_old2new[ebN_star_old];
4727 bool offproc = ebN_star_new >= elementBoundaryOffsets_new[rank+1] || ebN_star_new < elementBoundaryOffsets_new[rank];
4728 if (offproc)
4729 elementBoundaries_overlap.insert(ebN_star_new);
4730 }
4731 //edges in overlap
4732 for (int edN_star_offset = nodeEdgeOffsets[nN];
4733 edN_star_offset < nodeEdgeOffsets[nN+1]; edN_star_offset++)
4734 {
4735 int edN_star_old = nodeEdgesArray[edN_star_offset];
4736 int edN_star_new = edgeNumbering_global_old2new[edN_star_old];
4737 bool offproc = edN_star_new >= edgeOffsets_new[rank+1] || edN_star_new < edgeOffsets_new[rank];
4738 if (offproc)
4739 edges_overlap.insert(edN_star_new);
4740 }
4741 }//nodes on this processor
4742 elementNumbering_global_old2new.resize(0);
4743 MPI_Barrier(PROTEUS_COMM_WORLD);
4744 assert(edges_overlap.size() + nEdges_subdomain_new[rank] == edgeNodesMap.size());
4745 //
4746 //enumerate the overlap
4747 //
4748 int nN_subdomain = nNodes_subdomain_new[rank];
4749 map<int,int> nodes_overlap_global2subdomainMap;
4750 for (set<int>::iterator nN_globalp=nodes_overlap.begin();nN_globalp != nodes_overlap.end(); nN_globalp++,nN_subdomain++)
4751 nodes_overlap_global2subdomainMap[*nN_globalp] = nN_subdomain;
4752
4753 // map<int,int> elements_overlap_global2subdomainMap;
4754 // for (set<int>::iterator eN_globalp=elements_overlap.begin();eN_globalp != elements_overlap.end(); eN_globalp++,eN_subdomain++)
4755 // elements_overlap_global2subdomainMap[*eN_globalp] = eN_subdomain;
4756
4757 // int ebN_subdomain = nElementBoundaries_subdomain_new[rank];
4758 // map<int,int> elementBoundaries_overlap_global2subdomainMap;
4759 // for (set<int>::iterator ebN_globalp=elementBoundaries_overlap.begin();ebN_globalp != elementBoundaries_overlap.end(); ebN_globalp++,ebN_subdomain++)
4760 // elementBoundaries_overlap_global2subdomainMap[*ebN_globalp] = ebN_subdomain;
4761
4762 // int edN_subdomain = nEdges_subdomain_new[rank];
4763 // map<int,int> edges_overlap_global2subdomainMap;
4764 // for (set<int>::iterator edN_globalp=edges_overlap.begin();edN_globalp != edges_overlap.end(); edN_globalp++,edN_subdomain++)
4765 // edges_overlap_global2subdomainMap[*edN_globalp] = edN_subdomain;
4766 //
4767 //7. add any addtional overlap, skip for now
4768 //
4769
4770 PetscLogEventEnd(build_subdomains_edges_event,0,0,0,0);
4771 int build_subdomains_renumber_event;
4772 PetscLogEventRegister("Subd's renumber",0,&build_subdomains_renumber_event);
4773 PetscLogEventBegin(build_subdomains_renumber_event,0,0,0,0);
4774 //
4775 //8. Build subdomain meshes in new numbering, assumes memory not allocated in subdomain mesh
4776 //
4777 if(rank==0){
4778 std::cerr<<"USER WARNING: In order to avoid a segmentation fault, you need to have supplied the 'f' flag to the triangleOptions input."<<std::endl;
4779 std::cerr<<"USER WARNING: In order to avoid an edge assertion error, you need to have supplied the 'ee' flag to the triangleOptions input."<<std::endl;
4780 }
4781
4782 if (newMesh.subdomainp == NULL)
4783 newMesh.subdomainp = new Mesh();
4784 newMesh.subdomainp->nElements_global = nElements_subdomain_new[rank] + elements_overlap.size();
4785 newMesh.subdomainp->nNodes_global = nNodes_subdomain_new[rank] + nodes_overlap.size();
4786 newMesh.subdomainp->nElementBoundaries_global = nElementBoundaries_subdomain_new[rank]+elementBoundaries_overlap.size();
4787 assert(int(edges_subdomain_owned.size()+edges_overlap.size()) == newMesh.subdomainp->nEdges_global);
4788 //newMesh.subdomainp->nEdges_global = edges_subdomain_owned.size()+edges_overlap.size();
4789 newMesh.subdomainp->nNodes_element = newMesh.nNodes_element;
4792 //subdomain 2 global mappings (including ghost info)
4793 valarray<int> nodeNumbering_subdomain2global(newMesh.subdomainp->nNodes_global);
4794 valarray<int> elementNumbering_subdomain2global(newMesh.subdomainp->nElements_global);
4795 valarray<int> elementBoundaryNumbering_subdomain2global(newMesh.subdomainp->nElementBoundaries_global);
4796 valarray<int> edgeNumbering_subdomain2global(newMesh.subdomainp->nEdges_global);
4797 map<int,int> nodeNumbering_global2subdomainMap;
4798 map<int,int> elementBoundaryNumbering_global2subdomainMap;
4799 map<int,int> edgeNumbering_global2subdomainMap;
4800 newMesh.subdomainp->nodeArray = new double[newMesh.subdomainp->nNodes_global*3];
4801 newMesh.subdomainp->nodeMaterialTypes = new int[newMesh.subdomainp->nNodes_global];
4802 //
4803 //now finally finish reading node coordinates and node flags
4804 //
4805 for (int iv = 0; iv < nNodes_global; iv++)
4806 {
4807 int nv; double x,y; int nodeId(0);
4808 vertexFile >> eatcomments >> nv >> x >> y;
4809 if (hasVertexMarkers > 0)
4810 vertexFile >> nodeId;
4811 nv -= indexBase;
4812 assert(0 <= nv && nv < nNodes_global && vertexFile.good());
4813 int nN_global_new = -1;
4814 if (nodes_old2new_subdomain_map.find(nv) != nodes_old2new_subdomain_map.end())
4815 nN_global_new = nodes_old2new_subdomain_map[nv];
4816 //local
4817 if (nN_global_new >= nodeOffsets_new[rank] && nN_global_new < nodeOffsets_new[rank+1])
4818 {
4819 int nv_subdomain_new = nN_global_new - nodeOffsets_new[rank];
4820 nodeNumbering_subdomain2global[nv_subdomain_new] = nN_global_new;
4821 nodeNumbering_global2subdomainMap[nN_global_new] = nv_subdomain_new;
4822 newMesh.subdomainp->nodeArray[vertexDim*nv_subdomain_new + 0] = x;
4823 newMesh.subdomainp->nodeArray[vertexDim*nv_subdomain_new + 1] = y;
4824 newMesh.subdomainp->nodeArray[vertexDim*nv_subdomain_new + 2] = 0.0;
4825 if (hasVertexMarkers > 0)
4826 newMesh.subdomainp->nodeMaterialTypes[nv_subdomain_new] = nodeId;
4827 }
4828 //overlap
4829 if (nodes_overlap.count(nN_global_new) == 1)
4830 {
4831 int nv_subdomain_new = nodes_overlap_global2subdomainMap[nN_global_new];
4832 nodeNumbering_subdomain2global[nv_subdomain_new] = nN_global_new;
4833 nodeNumbering_global2subdomainMap[nN_global_new] = nv_subdomain_new;
4834 newMesh.subdomainp->nodeArray[vertexDim*nv_subdomain_new + 0] = x;
4835 newMesh.subdomainp->nodeArray[vertexDim*nv_subdomain_new + 1] = y;
4836 newMesh.subdomainp->nodeArray[vertexDim*nv_subdomain_new + 2] = 0.0;
4837 if (hasVertexMarkers > 0)
4838 newMesh.subdomainp->nodeMaterialTypes[nv_subdomain_new] = nodeId;
4839 }
4840 vertexFile >> eatline;
4841 }//end iv
4842 vertexFile.close();
4843 ISDestroy(&nodePartitioningIS_new);
4844 ISDestroy(&nodeNumberingIS_subdomain_old2new);
4845 //done with vertex file (and all file reads at this point)
4846 //ierr = enforceMemoryLimit(PROTEUS_COMM_WORLD, rank, max_rss_gb,"Done reading vertices");CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
4847
4849 newMesh.subdomainp->elementMaterialTypes = new int[newMesh.subdomainp->nElements_global];
4850 //
4851 //elements
4852 //
4853 //locally owned
4854 //
4855 for (int eN = 0; eN < nElements_subdomain_new[rank]; eN++)
4856 {
4857 int eN_global_new = elementOffsets_new[rank] + eN;
4858 int eN_global_old = elementNumbering_global_new2old[eN_global_new];
4859 elementNumbering_subdomain2global[eN] = eN_global_new;
4860 newMesh.subdomainp->elementMaterialTypes[eN] = elementMaterialTypesMap[eN_global_old];
4861 for (int nN = 0; nN < newMesh.subdomainp->nNodes_element; nN++)
4862 {
4863 int nN_global_new = elementNodesArrayMap[eN_global_old][nN];
4864 int nN_subdomain = nodeNumbering_global2subdomainMap[nN_global_new];
4865 newMesh.subdomainp->elementNodesArray[eN*newMesh.subdomainp->nNodes_element + nN]= nN_subdomain;
4866 }
4867 }
4868 //
4869 //ghost
4870 //
4871 set<int>::iterator eN_p = elements_overlap.begin();
4872 for (int eN = nElements_subdomain_new[rank]; eN < nElements_subdomain_new[rank] + int(elements_overlap.size()); eN++,eN_p++)
4873 {
4874 int eN_global_new = *eN_p;
4875 int eN_global_old = elementNumbering_global_new2old[eN_global_new];
4876 elementNumbering_subdomain2global[eN] = eN_global_new;
4877 newMesh.subdomainp->elementMaterialTypes[eN] = elementMaterialTypesMap[eN_global_old];
4878 for (int nN = 0; nN < newMesh.subdomainp->nNodes_element; nN++)
4879 {
4880 int nN_global_new = elementNodesArrayMap[eN_global_old][nN];
4881 int nN_subdomain = nodeNumbering_global2subdomainMap[nN_global_new];
4882 newMesh.subdomainp->elementNodesArray[eN*newMesh.subdomainp->nNodes_element + nN]= nN_subdomain;
4883 }
4884 }
4885 elementNodesArrayMap.clear();
4886 elementMaterialTypesMap.clear();
4887 ISRestoreIndices(elementNumberingIS_global_new2old,&elementNumbering_global_new2old);
4888 ISDestroy(&elementNumberingIS_subdomain_new2old);
4889 ISDestroy(&elementNumberingIS_global_new2old);
4890 //
4891 //element boundaries
4892 //
4893 //owned
4894 //
4895 for (int ebN=0; ebN < nElementBoundaries_subdomain_new[rank]; ebN++)
4896 {
4897 int ebN_global = ebN + elementBoundaryOffsets_new[rank];
4898 elementBoundaryNumbering_subdomain2global[ebN]=ebN_global;
4899 elementBoundaryNumbering_global2subdomainMap[ebN_global] = ebN;
4900 }
4901 //
4902 //ghost
4903 //
4904 set<int>::iterator ebN_p = elementBoundaries_overlap.begin();
4905 for(int ebN=nElementBoundaries_subdomain_new[rank];ebN < nElementBoundaries_subdomain_new[rank] + int(elementBoundaries_overlap.size()); ebN++,ebN_p++)
4906 {
4907 int ebN_global = *ebN_p;
4908 elementBoundaryNumbering_subdomain2global[ebN] = ebN_global;
4909 elementBoundaryNumbering_global2subdomainMap[ebN_global] = ebN;
4910 }
4911 //
4912 //need elementBoundariesArray to assign consistent numbering on subdomain
4913 //
4914 //local
4915 //
4918 for (int eN=0;eN<nElements_subdomain_new[rank];eN++)
4919 {
4920 int eN_global = eN+elementOffsets_new[rank];
4921 for (int ebN=0;ebN<newMesh.subdomainp->nElementBoundaries_element;ebN++)
4922 {
4924 elementBoundaryNumbering_global2subdomainMap[elementBoundaryNumbering_global_old2new[elementBoundariesMap[eN_global][ebN]]];
4925 }
4926 }
4927 //
4928 //ghost elements
4929 //
4930 eN_p = elements_overlap.begin();
4931 for (int eN = nElements_subdomain_new[rank]; eN < nElements_subdomain_new[rank] + int(elements_overlap.size()); eN++,eN_p++)
4932 {
4933 int eN_global_new = *eN_p;
4934 for (int ebN=0;ebN<newMesh.subdomainp->nElementBoundaries_element;ebN++)
4935 {
4937 elementBoundaryNumbering_global2subdomainMap[elementBoundaryNumbering_global_old2new[elementBoundariesMap[eN_global_new][ebN]]];
4938 }
4939 }
4940 //
4941 //edges
4942 //
4943 //local
4944 //
4945 for (int edN=0; edN < nEdges_subdomain_new[rank]; edN++)
4946 {
4947 int edN_global = edN + edgeOffsets_new[rank];
4948 edgeNumbering_subdomain2global[edN]=edN_global;
4949 edgeNumbering_global2subdomainMap[edN_global] = edN;
4950 }
4951 //
4952 //ghost
4953 //
4954 set<int>::iterator edN_p = edges_overlap.begin();
4955 for(int edN=nEdges_subdomain_new[rank];edN < nEdges_subdomain_new[rank] + int(edges_overlap.size()); edN++,edN_p++)
4956 {
4957 int edN_global = *edN_p;
4958 edgeNumbering_subdomain2global[edN] = edN_global;
4959 edgeNumbering_global2subdomainMap[edN_global] = edN;
4960 }
4961 //
4962 //now build edgeNodes array in new numberings
4963 //
4964 newMesh.subdomainp->edgeNodesArray = new int[newMesh.subdomainp->nEdges_global*2];
4965 for (int i=0;i<newMesh.subdomainp->nEdges_global*2;i++)
4966 newMesh.subdomainp->edgeNodesArray[i] = -1;
4967 for (map<int,pair<int,int> >::iterator edgep=edgeNodesMap.begin();
4968 edgep!=edgeNodesMap.end();
4969 edgep++)
4970 {
4971 int edN_global_old = edgep->first;
4972 int edN_global_new = edgeNumbering_global_old2new[edN_global_old];
4973 assert(edgeNumbering_global2subdomainMap.find(edN_global_new) != edgeNumbering_global2subdomainMap.end());
4974 int edN_subdomain = edgeNumbering_global2subdomainMap[edN_global_new];
4975 newMesh.subdomainp->edgeNodesArray[edN_subdomain*2+0] = nodeNumbering_global2subdomainMap[edgep->second.first];
4976 newMesh.subdomainp->edgeNodesArray[edN_subdomain*2+1] = nodeNumbering_global2subdomainMap[edgep->second.second];
4977 }
4978 edgeNumbering_global_old2new.resize(0);
4979 //
4980 //end edges
4981 //
4982
4983 //now build rest of subdomain mesh connectivity information etc
4984 bool callOld=true;
4985 if(callOld)
4987 else
4988 {
4989 //const int DEFAULT_ELEMENT_MATERIAL=0;
4990 const int DEFAULT_NODE_MATERIAL=-1;
4991 const int INTERIOR_NODE_MATERIAL=0;
4992 const int EXTERIOR_NODE_MATERIAL=1;
4995
4998 assert(newMesh.subdomainp->elementBoundariesArray);
4999 using namespace std;
5000 //double start,stop;
5001 map<NodeTuple<3>,
5002 ElementNeighbors> elementBoundaryElements;
5003 map<NodeTuple<3>,
5004 int> elementBoundaryIds;
5005 //start=CurrentTime();
5006 //cout<<"Extracting boundary elements"<<endl;
5007 for(int eN=0;eN<newMesh.subdomainp->nElements_global;eN++)
5008 for(int ebN=0;ebN<newMesh.subdomainp->nElementBoundaries_element;ebN++)
5009 {
5010 int ebN_global = newMesh.subdomainp->elementBoundariesArray[eN*newMesh.subdomainp->nElementBoundaries_element+ebN];
5011 int nodes[3];
5012 nodes[0] = newMesh.subdomainp->elementNodesArray[eN*4+((ebN+1)%4)];
5013 nodes[1] = newMesh.subdomainp->elementNodesArray[eN*4+((ebN+2)%4)];
5014 nodes[2] = newMesh.subdomainp->elementNodesArray[eN*4+((ebN+3)%4)];
5015 NodeTuple<3> ebt(nodes);
5016 if(elementBoundaryElements.find(ebt) != elementBoundaryElements.end())
5017 {
5018 elementBoundaryElements[ebt].right=eN;
5019 elementBoundaryElements[ebt].right_ebN_element=ebN;
5020 assert(elementBoundaryIds[ebt] == ebN_global);
5021 }
5022 else
5023 {
5024 elementBoundaryElements.insert(elementBoundaryElements.end(),make_pair(ebt,ElementNeighbors(eN,ebN)));
5025 elementBoundaryIds.insert(elementBoundaryIds.end(),make_pair(ebt,ebN_global));
5026 }
5027 }
5028 //stop = CurrentTime();
5029 //cout<<"Elapsed time for building element boundary elements map= "<<(stop-start)<<"s"<<endl;
5030 newMesh.subdomainp->nElementBoundaries_global = elementBoundaryElements.size();
5031 //cout<<"nElementBoundaries_global = "<<newMesh.subdomainp->nElementBoundaries_global<<endl;
5032
5033 //cout<<"Allocating Arrays"<<endl;
5034 //start = CurrentTime();
5035 set<int> interiorElementBoundaries,exteriorElementBoundaries;
5040 //stop = CurrentTime();
5041 //cout<<"Elapsed time for allocating arrays = "<<(stop-start)<<"s"<<endl;
5042
5043 //cout<<"Generating elementBoundaryElementsArray and elementBoundaryNodesArray"<<endl;
5044 //start = CurrentTime();
5045 for(map<NodeTuple<3>,ElementNeighbors>::iterator eb=elementBoundaryElements.begin();
5046 eb != elementBoundaryElements.end();
5047 eb++)
5048 {
5049 int ebN = elementBoundaryIds[eb->first];
5050 newMesh.subdomainp->elementBoundaryNodesArray[ebN*3 + 0] = eb->first.nodes[0];
5051 newMesh.subdomainp->elementBoundaryNodesArray[ebN*3 + 1] = eb->first.nodes[1];
5052 newMesh.subdomainp->elementBoundaryNodesArray[ebN*3 + 2] = eb->first.nodes[2];
5053
5054 newMesh.subdomainp->elementBoundaryElementsArray[ebN*2 + 0] = eb->second.left;
5055 newMesh.subdomainp->elementBoundaryLocalElementBoundariesArray[ebN*2 + 0] = eb->second.left_ebN_element;
5056 newMesh.subdomainp->elementBoundaryElementsArray[ebN*2 + 1] = eb->second.right;
5057 newMesh.subdomainp->elementBoundaryLocalElementBoundariesArray[ebN*2 + 1] = eb->second.right_ebN_element;
5058 newMesh.subdomainp->elementNeighborsArray[eb->second.left*newMesh.subdomainp->nElementBoundaries_element + eb->second.left_ebN_element] = eb->second.right;
5059 if(eb->second.right != -1)
5060 {
5061 interiorElementBoundaries.insert(ebN);
5062 newMesh.subdomainp->elementNeighborsArray[eb->second.right*newMesh.subdomainp->nElementBoundaries_element + eb->second.right_ebN_element] = eb->second.left;
5063 }
5064 else
5065 exteriorElementBoundaries.insert(ebN);
5066 assert(newMesh.subdomainp->elementBoundariesArray[eb->second.left*newMesh.subdomainp->nElementBoundaries_element + eb->second.left_ebN_element] == ebN);
5067 if (eb->second.right != -1)
5068 {
5069 assert(newMesh.subdomainp->elementBoundariesArray[eb->second.right*newMesh.subdomainp->nElementBoundaries_element + eb->second.right_ebN_element] == ebN);
5070 }
5071 }
5072 newMesh.subdomainp->nInteriorElementBoundaries_global = interiorElementBoundaries.size();
5074 newMesh.subdomainp->nExteriorElementBoundaries_global = exteriorElementBoundaries.size();
5076 int ebNI=0,ebNE=0;
5077 for (set<int>::iterator ebN=interiorElementBoundaries.begin();ebN != interiorElementBoundaries.end(); ebN++,ebNI++)
5078 newMesh.subdomainp->interiorElementBoundariesArray[ebNI] = *ebN;
5079 for (set<int>::iterator ebN=exteriorElementBoundaries.begin();ebN != exteriorElementBoundaries.end(); ebN++,ebNE++)
5080 newMesh.subdomainp->exteriorElementBoundariesArray[ebNE] = *ebN;
5081 set<NodeTuple<2> > edges;
5082 for (int eN=0;eN<newMesh.subdomainp->nElements_global;eN++)
5083 {
5084 int nodes[2];
5085 for (int nN_L=0;nN_L<newMesh.subdomainp->nNodes_element;nN_L++)
5086 for (int nN_R=nN_L+1;nN_R<newMesh.subdomainp->nNodes_element;nN_R++)
5087 {
5088 nodes[0] = newMesh.subdomainp->elementNodesArray[eN*4+nN_L];
5089 nodes[1] = newMesh.subdomainp->elementNodesArray[eN*4+nN_R];
5090 edges.insert(NodeTuple<2>(nodes));
5091 }
5092 }
5093 assert(newMesh.subdomainp->nEdges_global == int(edges.size()));
5094 vector<set<int> > nodeStar(newMesh.subdomainp->nNodes_global);
5095 for (int edgeN=0;edgeN<newMesh.subdomainp->nEdges_global;edgeN++)
5096 {
5097 nodeStar[newMesh.subdomainp->edgeNodesArray[edgeN*2+0]].insert(newMesh.subdomainp->edgeNodesArray[edgeN*2+1]);
5098 nodeStar[newMesh.subdomainp->edgeNodesArray[edgeN*2+1]].insert(newMesh.subdomainp->edgeNodesArray[edgeN*2+0]);
5099 }
5100 newMesh.subdomainp->nodeStarOffsets = new int[newMesh.subdomainp->nNodes_global+1];
5101 newMesh.subdomainp->nodeStarOffsets[0] = 0;
5102 for (int nN=1;nN<newMesh.subdomainp->nNodes_global+1;nN++)
5103 newMesh.subdomainp->nodeStarOffsets[nN] = newMesh.subdomainp->nodeStarOffsets[nN-1] + nodeStar[nN-1].size();
5104 newMesh.subdomainp->nodeStarArray = new int[newMesh.subdomainp->nodeStarOffsets[newMesh.subdomainp->nNodes_global]];
5105 for (int nN=0,offset=0;nN<newMesh.subdomainp->nNodes_global;nN++)
5106 for (set<int>::iterator nN_star=nodeStar[nN].begin();nN_star!=nodeStar[nN].end();nN_star++,offset++)
5107 newMesh.subdomainp->nodeStarArray[offset] = *nN_star;
5108 //stop = CurrentTime();
5110 for (int nN=0;nN<newMesh.subdomainp->nNodes_global;nN++)
5112 //mwf repeat for node-->elements arrays
5113 vector<set<int> > nodeElementsStar(newMesh.subdomainp->nNodes_global);
5114 for (int eN = 0; eN < newMesh.subdomainp->nElements_global; eN++)
5115 {
5116 for (int nN = 0; nN < newMesh.subdomainp->nNodes_element; nN++)
5117 nodeElementsStar[newMesh.subdomainp->elementNodesArray[eN*newMesh.subdomainp->nNodes_element+nN]].insert(eN);
5118 }
5119 newMesh.subdomainp->nodeElementOffsets = new int[newMesh.subdomainp->nNodes_global+1];
5120 newMesh.subdomainp->nodeElementOffsets[0] = 0;
5121 for (int nN = 0; nN < newMesh.subdomainp->nNodes_global; nN++)
5122 newMesh.subdomainp->nodeElementOffsets[nN+1] = newMesh.subdomainp->nodeElementOffsets[nN]+nodeElementsStar[nN].size();
5124 for (int nN=0,offset=0; nN < newMesh.subdomainp->nNodes_global; nN++)
5125 {
5126 for (set<int>::iterator eN_star = nodeElementsStar[nN].begin(); eN_star != nodeElementsStar[nN].end();
5127 eN_star++,offset++)
5128 {
5129 newMesh.subdomainp->nodeElementsArray[offset] = *eN_star;
5130 }
5131 }
5132 //mwf end node-->elements construction
5134 //if nodeMaterial is DEFAULT, go ahead and set to interior or exterior
5135 //depending on which boundary node belongs to.
5136 //If node on at least one exterior boundary then it's exterior
5137 for (int ebNE = 0; ebNE < newMesh.subdomainp->nExteriorElementBoundaries_global; ebNE++)
5138 {
5139 int ebN = newMesh.subdomainp->exteriorElementBoundariesArray[ebNE];
5141 for (int nN_local = 0; nN_local < newMesh.subdomainp->nNodes_elementBoundary; nN_local++)
5142 {
5143 int nN = newMesh.subdomainp->elementBoundaryNodesArray[ebN*newMesh.subdomainp->nNodes_elementBoundary+nN_local];
5146 }
5147 }
5148 for (int ebNI = 0; ebNI < newMesh.subdomainp->nInteriorElementBoundaries_global; ebNI++)
5149 {
5150 int ebN = newMesh.subdomainp->interiorElementBoundariesArray[ebNI];
5152 for (int nN_local = 0; nN_local < newMesh.subdomainp->nNodes_elementBoundary; nN_local++)
5153 {
5154 int nN = newMesh.subdomainp->elementBoundaryNodesArray[ebN*newMesh.subdomainp->nNodes_elementBoundary+nN_local];
5157 }
5158 }
5159 //cout<<"Elapsed time for populating arrays = "<<(stop-start)<<"s"<<endl;
5160 }
5161 //build local geometric info
5164
5165 if (hasElementBoundaryMarkers)
5166 {
5167 assert(newMesh.subdomainp->elementBoundariesArray != NULL);
5168 for (map<int,int>::iterator ebmp = elementBoundaryMaterialTypesMap.begin(); ebmp != elementBoundaryMaterialTypesMap.end();ebmp++)
5169 {
5170 int ebN_global_new = elementBoundaryNumbering_global_old2new[ebmp->first];
5171 assert(elementBoundaryNumbering_global2subdomainMap.find(ebN_global_new) != elementBoundaryNumbering_global2subdomainMap.end());
5172 int ebN_subdomain = elementBoundaryNumbering_global2subdomainMap[ebN_global_new];
5173 newMesh.subdomainp->elementBoundaryMaterialTypes[ebN_subdomain] = ebmp->second;
5174 }
5175 if (!hasVertexMarkers)
5176 for (int ebNE = 0; ebNE < newMesh.subdomainp->nExteriorElementBoundaries_global; ebNE++)
5177 {
5178 int ebN = newMesh.subdomainp->exteriorElementBoundariesArray[ebNE];
5179 for (int nN_local = 0; nN_local < newMesh.subdomainp->nNodes_elementBoundary; nN_local++)
5180 {
5181 int nN = newMesh.subdomainp->elementBoundaryNodesArray[ebN*newMesh.subdomainp->nNodes_elementBoundary+nN_local];
5183 }
5184 }
5185 }
5186 elementBoundaryNumbering_global_old2new.resize(0);
5187 //ierr = enforceMemoryLimit(PROTEUS_COMM_WORLD, rank, max_rss_gb,"Done with material types");CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
5188 PetscLogEventEnd(build_subdomains_renumber_event,0,0,0,0);
5189 int build_subdomains_cleanup_event;
5190 PetscLogEventRegister("Cleanup",0,&build_subdomains_cleanup_event);
5191 PetscLogEventBegin(build_subdomains_cleanup_event,0,0,0,0);
5192 //transfer information about owned nodes and elements to mesh
5193 if (newMesh.nodeOffsets_subdomain_owned)
5194 delete [] newMesh.nodeOffsets_subdomain_owned;
5196 delete [] newMesh.elementOffsets_subdomain_owned;
5199 if (newMesh.edgeOffsets_subdomain_owned)
5200 delete [] newMesh.edgeOffsets_subdomain_owned;
5201 newMesh.nodeOffsets_subdomain_owned = new int[size+1];
5202 newMesh.elementOffsets_subdomain_owned = new int[size+1];
5203 newMesh.elementBoundaryOffsets_subdomain_owned = new int[size+1];
5204 newMesh.edgeOffsets_subdomain_owned = new int[size+1];
5205 for (int sdN = 0; sdN < size+1; sdN++)
5206 {
5207 newMesh.nodeOffsets_subdomain_owned[sdN] = nodeOffsets_new[sdN];
5208 newMesh.elementOffsets_subdomain_owned[sdN] = elementOffsets_new[sdN];
5209 newMesh.elementBoundaryOffsets_subdomain_owned[sdN] = elementBoundaryOffsets_new[sdN];
5210 newMesh.edgeOffsets_subdomain_owned[sdN] = edgeOffsets_new[sdN];
5211 }
5213 delete [] newMesh.nodeNumbering_subdomain2global;
5214 newMesh.nodeNumbering_subdomain2global = new int[newMesh.subdomainp->nNodes_global];
5215 for (int nN = 0; nN < newMesh.subdomainp->nNodes_global; nN++)
5216 newMesh.nodeNumbering_subdomain2global[nN] = nodeNumbering_subdomain2global[nN];
5218 delete [] newMesh.elementNumbering_subdomain2global;
5220 for (int eN = 0; eN < newMesh.subdomainp->nElements_global; eN++)
5221 newMesh.elementNumbering_subdomain2global[eN] = elementNumbering_subdomain2global[eN];
5222 //
5226 for (int ebN = 0; ebN < newMesh.subdomainp->nElementBoundaries_global; ebN++)
5227 newMesh.elementBoundaryNumbering_subdomain2global[ebN] = elementBoundaryNumbering_subdomain2global[ebN];
5228 //
5230 delete [] newMesh.edgeNumbering_subdomain2global;
5231 newMesh.edgeNumbering_subdomain2global = new int[newMesh.subdomainp->nEdges_global];
5232 for (int i=0; i< newMesh.subdomainp->nEdges_global; i++)
5233 newMesh.edgeNumbering_subdomain2global[i] = edgeNumbering_subdomain2global[i];
5234 //cleanup
5235 /* out of core*/
5236 H5Sclose(filespace);
5237 H5Sclose(memspace);
5238 H5Pclose(plist_id);
5239 //(the mappings file is closed right after the numbering write and again
5240 //after the chunked read loop; nothing is open here any more)
5241 /* out of core */
5242 PetscLogEventEnd(build_subdomains_cleanup_event,0,0,0,0);
5243 PetscLogStagePop();
5244 PetscLogView(PETSC_VIEWER_STDOUT_WORLD);
5245 //ierr = enforceMemoryLimit(PROTEUS_COMM_WORLD, rank, max_rss_gb,"Done with partitioning!");CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
5246 return 0;
5247}
5248//todo add overlap for element based partitions
5249int partitionElements(const MPI_Comm& PROTEUS_COMM_WORLD, Mesh& mesh, int nElements_overlap)
5250{
5251 using namespace std;
5252 int ierr,size,rank;
5253
5254 ierr = MPI_Comm_size(PROTEUS_COMM_WORLD,&size);
5255 ierr = MPI_Comm_rank(PROTEUS_COMM_WORLD,&rank);
5256
5257 //Contents
5258 //
5259 //1. Partition the elements in the "default" partition (contiguous
5260 //chunks in given ordering)
5261 //
5262 //2. Partition the elementNeighbors based on this partition
5263 //
5264 //3. Pass to Parmetis to build a better partition of the elements
5265 //
5266 //4. Tag a subset of the nodes and faces on the subdomain elements as owned
5267 //using a mark and pass approach.**
5268 //
5269 //5. Extract the nodes and faces in the
5270 //overlapping elements.**
5271 //
5272 //6. Build the subdomain mesh from the
5273 //subdomain elements
5274 //
5275 //**To be more general we could get all the support (i.e. faces
5276 //and edges) and partitiong them, but the main reason for
5277 //partitioning is to keep track of a global numbering for degrees
5278 //of freedom that live on each type of geometric entity. We only
5279 //have node and element based DOFs so I just rebuild the other
5280 //information once we have elements and nodes partitioned.
5281 //
5282 // \todo check that I restore all data that PETSc expects to have
5283 // back, add PETSc error checking macros
5284 //
5285 //1. Build default partitioning
5286 //
5287 //get offsets so we can calculate the processor to global mapping
5288 //for elements in the old (default) partitioning
5289 valarray<int> elementOffsets_old(size+1);
5290 elementOffsets_old[0] = 0;
5291 for(int sdN=0;sdN<size;sdN++)
5292 elementOffsets_old[sdN+1] = elementOffsets_old[sdN] +
5293 int(mesh.nElements_global)/size +
5294 (int(mesh.nElements_global)%size > sdN);
5295
5296 //2. Extract subdomain element adjacency information could read
5297 //only the required portion from a file
5298 int nElements_subdomain = (elementOffsets_old[rank+1] - elementOffsets_old[rank]);
5299 PetscInt *elementNeighborsOffsets_subdomain,*elementNeighbors_subdomain,*weights_subdomain;
5300 PetscMalloc(sizeof(PetscInt)*(nElements_subdomain+1),&elementNeighborsOffsets_subdomain);
5301 PetscMalloc(sizeof(PetscInt)*(nElements_subdomain*mesh.nElementBoundaries_element),&elementNeighbors_subdomain);
5302 PetscMalloc(sizeof(PetscInt)*(nElements_subdomain*mesh.nElementBoundaries_element),&weights_subdomain);
5303 //this wastes a little space
5304 elementNeighborsOffsets_subdomain[0] = 0;
5305 for (int eN=0,offset=0; eN < nElements_subdomain; eN++)
5306 {
5307 int eN_global = elementOffsets_old[rank] + eN;
5308 int offsetStart=offset;
5309 for (int ebN=0; ebN< mesh.nElementBoundaries_element;ebN++)
5310 {
5311 int eN_neighbor_global = mesh.elementNeighborsArray[eN_global*mesh.nElementBoundaries_element + ebN];
5312 if (eN_neighbor_global >= 0 )
5313 elementNeighbors_subdomain[offset++] = eN_neighbor_global;
5314 }
5315 elementNeighborsOffsets_subdomain[eN+1]=offset;
5316 sort(&elementNeighbors_subdomain[offsetStart],&elementNeighbors_subdomain[offset]);
5317 int weight = (elementNeighborsOffsets_subdomain[eN+1] - elementNeighborsOffsets_subdomain[eN]);
5318 for (int k=elementNeighborsOffsets_subdomain[eN];k<elementNeighborsOffsets_subdomain[eN+1];k++)
5319 weights_subdomain[k] = weight;
5320 }
5321 //3. Generate the new partitiong using PETSc, this is done in parallel using parmetis
5322 Mat petscAdjacency;
5323 // MatCreateMPIAdj(PROTEUS_COMM_WORLD,
5324 // nElements_subdomain, mesh.nElements_global,
5325 // &elementNeighborsOffsets_subdomain[0], &elementNeighbors_subdomain[0],
5326 // &weights_subdomain[0],//PETSC_NULL,
5327 // &petscAdjacency);
5328 ierr = MatCreateMPIAdj(PROTEUS_COMM_WORLD,
5329 nElements_subdomain,
5330 mesh.nElements_global,
5331 elementNeighborsOffsets_subdomain,
5332 elementNeighbors_subdomain,
5333 PETSC_NULL,//weights_subdomain,
5334 &petscAdjacency);CHKERRABORT(PROTEUS_COMM_WORLD, ierr);
5335 PetscFree(weights_subdomain);
5336 MatPartitioning petscPartition;
5337 MatPartitioningCreate(PROTEUS_COMM_WORLD,&petscPartition);
5338 MatPartitioningSetAdjacency(petscPartition,petscAdjacency);
5339 MatPartitioningSetFromOptions(petscPartition);
5340
5341 //get a petsc index set that has the new submdomain number for each element
5342 IS elementPartitioningIS_new;
5343 MatPartitioningApply(petscPartition,&elementPartitioningIS_new);
5344 MatPartitioningDestroy(&petscPartition);
5345 MatDestroy(&petscAdjacency);
5346 //ISView(elementPartitioningIS_new,PETSC_VIEWER_STDOUT_WORLD);
5347
5348 //experiment with metis
5349 //mwf set some defaults and not call if size == 1 since metis crashes
5350 //cek commenting out for now
5351 //int etype=1,edgecut=0,base=0;
5352 //epart assign everything to processor zero by default
5353 //valarray<int> epart(0,mesh.nElements_global),npart(mesh.nNodes_global);
5354 //if (size > 1)
5355 // METIS_PartMeshNodal(&mesh.nElements_global,&mesh.nNodes_global,mesh.elementNodesArray,&etype,&base,&size,&edgecut,&epart[0],&npart[0]);
5356 //ISCreateGeneralWithArray(PETSC_COMM_SELF,mesh.nElements_global,&epart[0],&elementPartitioningIS_new);
5357 //write mesh to view with showme
5358 // std::ofstream nodeout("mesh.node"),eleout("mesh.ele"),partout("mesh.part");
5359 // eleout<<mesh.nElements_global<<" 3 0"<<std::endl;
5360 // partout<<mesh.nElements_global<<"\t"<<size<<std::endl;
5361 // for (int eN=0;eN<mesh.nElements_global;eN++)
5362 // {
5363 // partout<<(eN+1)<<"\t"<<(1+epart[eN])<<std::endl;
5364 // eleout<<(eN+1)<<"\t"<<(1+mesh.elementNodesArray[eN*3+0])
5365 // <<"\t"<<(1+mesh.elementNodesArray[eN*3+1])
5366 // <<"\t"<<(1+mesh.elementNodesArray[eN*3+2])
5367 // <<std::endl;
5368 // }
5369 // nodeout<<mesh.nNodes_global<<" 2 0 0"<<std::endl;
5370 // for (int nN=0;nN<mesh.nNodes_global;nN++)
5371 // {
5372 // nodeout<<(nN+1)<<"\t"<<mesh.nodeArray[nN*3+0]<<"\t"<<mesh.nodeArray[nN*3+1]<<std::endl;
5373 // }
5374 // eleout.close();
5375 // partout.close();
5376 //count the new number of elements on each subdomain
5377 valarray<int> nElements_subdomain_new(size);
5378 ISPartitioningCount(elementPartitioningIS_new,size,&nElements_subdomain_new[0]);
5379
5380 //get the new offsets for the subdomain to global numbering
5381 valarray<int> elementOffsets_new(size+1);
5382 elementOffsets_new[0] = 0;
5383 for (int sdN=0;sdN<size;sdN++)
5384 elementOffsets_new[sdN+1] = elementOffsets_new[sdN] + nElements_subdomain_new[sdN];
5385
5386 //get the new element numbers for the elements on this subdomain
5387 IS elementNumberingIS_subdomain_old2new;
5388 ISPartitioningToNumbering(elementPartitioningIS_new,&elementNumberingIS_subdomain_old2new);
5389
5390 //now get the new element numbers for the whole mesh so that we
5391 //can just read this processors elements, reorder, and renumber**
5392 //
5393 //**We could do this in parallel by scattering all the element
5394 //information
5395 IS elementNumberingIS_global_old2new;
5396 ISAllGather(elementNumberingIS_subdomain_old2new,&elementNumberingIS_global_old2new);
5397 const PetscInt *elementNumbering_global_old2new;
5398 ISGetIndices(elementNumberingIS_global_old2new,&elementNumbering_global_old2new);
5399 valarray<int> elementNumbering_global_new2old(mesh.nElements_global);
5400 for(int eN=0;eN<mesh.nElements_global;eN++)
5401 elementNumbering_global_new2old[elementNumbering_global_old2new[eN]] = eN;
5402
5403 //Sort element based arrays, maybe I don't need to do this, maybe
5404 //I just need to start writing into the subdomain mesh here and
5405 //preserve subdomain2old and subdomain2global mappings
5406 valarray<int> elementNodesArray_new(mesh.nElements_global*mesh.nNodes_element),
5407 elementNeighborsArray_new(mesh.nElements_global*mesh.nElementBoundaries_element),
5408 elementMaterialTypes_new(mesh.nElements_global),
5409 elementBoundariesArray_new(mesh.nElements_global*mesh.nElementBoundaries_element),
5410 elementBoundaryElementsArray_new(mesh.nElementBoundaries_global*2),
5411 elementBoundaryLocalElementBoundariesArray_new(mesh.nElementBoundaries_global*2),
5412 elementBoundaryNodesArray_new(mesh.nElementBoundaries_global*mesh.nNodes_elementBoundary),
5413 edgeNodesArray_new(mesh.nEdges_global*2);
5414 valarray<int> elementBoundaryMaterialTypes_new(mesh.nElementBoundaries_global);
5415 for (int eN=0;eN<mesh.nElements_global;eN++)
5416 {
5417 for (int nN=0;nN<mesh.nNodes_element;nN++)
5418 elementNodesArray_new[eN*mesh.nNodes_element + nN] =
5419 mesh.elementNodesArray[elementNumbering_global_new2old[eN]*mesh.nNodes_element+nN];
5420 for (int ebN=0;ebN<mesh.nElementBoundaries_element;ebN++)
5421 {
5422 elementNeighborsArray_new[eN*mesh.nElementBoundaries_element+ebN] =
5423 mesh.elementNeighborsArray[elementNumbering_global_new2old[eN]*mesh.nElementBoundaries_element+ebN];
5424 //need new elements --> old element boundary numbers for now
5425 elementBoundariesArray_new[eN*mesh.nElementBoundaries_element+ebN] =
5426 mesh.elementBoundariesArray[elementNumbering_global_new2old[eN]*mesh.nElementBoundaries_element+ebN];
5427 }
5428 elementMaterialTypes_new[eN] = mesh.elementMaterialTypes[elementNumbering_global_new2old[eN]];
5429
5430 }
5431 //renumber references to element numbers
5432 for (int eN=0;eN<mesh.nElements_global;eN++)
5433 {
5434 for (int ebN=0;ebN<mesh.nElementBoundaries_element;ebN++)
5435 {
5436 int eN_ebN = elementNeighborsArray_new[eN*mesh.nElementBoundaries_element+ebN];
5437 if (eN_ebN >= 0)
5438 elementNeighborsArray_new[eN*mesh.nElementBoundaries_element+ebN] =
5439 elementNumbering_global_old2new[eN_ebN];
5440 }
5441 }
5442 //4. now we need to build a new node ordering with better data locality for C0 finite elements
5443 //otherwise we could just grab the nodes on the subdomain and not worry about ownership
5444 //in the long run it wouldn't be bad to do a global repartition of faces and edges for mixed hybrid
5445 //and non-conforming finite elements
5446 MPI_Status status;
5447 PetscBT nodeMask;
5448 PetscBTCreate(mesh.nNodes_global,&nodeMask);
5449 if (rank > 0)
5450 {
5451 MPI_Recv(nodeMask,PetscBTLength(mesh.nNodes_global),MPI_CHAR,rank-1,0,PROTEUS_COMM_WORLD,&status);
5452 }
5453 //mark the unmarked nodes on this subdomain and store the node numbers
5454 set<int> nodes_subdomain_owned;
5455 for(int eN=elementOffsets_new[rank];eN<elementOffsets_new[rank+1];eN++)
5456 for(int nN=0;nN<mesh.nNodes_element;nN++)
5457 {
5458 int nN_global = elementNodesArray_new[eN*mesh.nNodes_element+nN];
5459 if (!PetscBTLookupSet(nodeMask,nN_global))
5460 nodes_subdomain_owned.insert(nN_global);
5461 }
5462 //ship off the mask
5463 if (rank < size-1)
5464 MPI_Send(nodeMask,PetscBTLength(mesh.nNodes_global),MPI_CHAR,rank+1,0,PROTEUS_COMM_WORLD);
5465 ierr = PetscBTDestroy(&nodeMask);
5466 if (ierr)
5467 cerr<<"Error in PetscBTDestroy"<<endl;
5468 //get the number of nodes on each processor
5469 valarray<int> nNodes_subdomain_new(size),
5470 nodeOffsets_new(size+1);
5471 for (int sdN=0;sdN<size;sdN++)
5472 if (sdN == rank)
5473 nNodes_subdomain_new[sdN] = nodes_subdomain_owned.size();
5474 else
5475 nNodes_subdomain_new[sdN] = 0;
5476 valarray<int> nNodes_subdomain_new_send=nNodes_subdomain_new;
5477 MPI_Allreduce(&nNodes_subdomain_new_send[0],&nNodes_subdomain_new[0],size,MPI_INT,MPI_SUM,PROTEUS_COMM_WORLD);
5478 nodeOffsets_new[0] = 0;
5479 for (int sdN=0;sdN<size;sdN++)
5480 nodeOffsets_new[sdN+1] = nodeOffsets_new[sdN]+nNodes_subdomain_new[sdN];
5481
5482 assert(nodeOffsets_new[size]==mesh.nNodes_global);
5483
5484 //Now as with elements build a global node numbering, sort node
5485 //based information, and renumber references to node numbers
5486 valarray<int> nodeNumbering_new2old(nodes_subdomain_owned.size());
5487 set<int>::iterator nN_ownedp=nodes_subdomain_owned.begin();
5488 for (int nN=0;nN<int(nodes_subdomain_owned.size());nN++)
5489 {
5490 nodeNumbering_new2old[nN] = *nN_ownedp++;
5491 }
5492 IS nodeNumberingIS_new2old;
5493 ISCreateGeneral(PROTEUS_COMM_WORLD,nodes_subdomain_owned.size(),&nodeNumbering_new2old[0],PETSC_COPY_VALUES,&nodeNumberingIS_new2old);
5494 IS nodeNumberingIS_global_new2old;
5495 ISAllGather(nodeNumberingIS_new2old,&nodeNumberingIS_global_new2old);
5496 const PetscInt *nodeNumbering_global_new2old;
5497 valarray<int> nodeNumbering_old2new_global(mesh.nNodes_global);
5498 ISGetIndices(nodeNumberingIS_global_new2old,&nodeNumbering_global_new2old);
5499 for (int nN=0;nN<mesh.nNodes_global;nN++)
5500 {
5501 nodeNumbering_old2new_global[nodeNumbering_global_new2old[nN]] = nN;
5502 }
5503 for (int eN=0;eN < mesh.nElements_global; eN++)
5504 {
5505 int nN_old;
5506 for (int nN=0;nN < mesh.nNodes_element; nN++)
5507 {
5508 nN_old = elementNodesArray_new[eN*mesh.nNodes_element+nN];
5509 elementNodesArray_new[eN*mesh.nNodes_element+nN] = nodeNumbering_old2new_global[nN_old];
5510 }
5511 }
5512 //mwf postpone until after element boundary renumbering
5513 // for (int i=0;i<mesh.nElementBoundaries_global*mesh.nNodes_elementBoundary;i++)
5514 // {
5515 // int nN_old = mesh.elementBoundaryNodesArray[i];
5516 // elementBoundaryNodesArray_new[i] = nodeNumbering_old2new_global[nN_old];
5517 // }
5518 for (int i=0;i<mesh.nEdges_global*2;i++)
5519 {
5520 int nN_old = mesh.edgeNodesArray[i];
5521 edgeNodesArray_new[i] = nodeNumbering_old2new_global[nN_old];
5522 }
5523 valarray<int> nodeStarArray_new(mesh.nodeStarOffsets[mesh.nNodes_global]);
5524 for (int i=0;i<mesh.nodeStarOffsets[mesh.nNodes_global];i++)
5525 {
5526 int nN_old = mesh.nodeStarArray[i];
5527 nodeStarArray_new[i] = nodeNumbering_old2new_global[nN_old];
5528 }
5529 valarray<double> nodeArray_new(mesh.nNodes_global*3);
5530 valarray<int> nodeMaterialTypes_new(mesh.nNodes_global);
5531 for (int nN=0;nN<mesh.nNodes_global;nN++)
5532 {
5533 int nN_new = nodeNumbering_old2new_global[nN];
5534 nodeArray_new[nN_new*3+0] = mesh.nodeArray[nN*3+0];
5535 nodeArray_new[nN_new*3+1] = mesh.nodeArray[nN*3+1];
5536 nodeArray_new[nN_new*3+2] = mesh.nodeArray[nN*3+2];
5537 nodeMaterialTypes_new[nN_new] = mesh.nodeMaterialTypes[nN];
5538 }
5539
5540 //4b. repeat process to build global face numbering
5541 MPI_Status status_elementBoundaries;
5542 PetscBT elementBoundaryMask;
5543 PetscBTCreate(mesh.nElementBoundaries_global,&elementBoundaryMask);
5544 if (rank > 0)
5545 {
5546 MPI_Recv(elementBoundaryMask,PetscBTLength(mesh.nElementBoundaries_global),MPI_CHAR,rank-1,0,PROTEUS_COMM_WORLD,&status_elementBoundaries);
5547 }
5548 //mark the unmarked faces on this subdomain and store the global face numbers
5549 set<int> elementBoundaries_subdomain_owned;
5550 for(int eN=elementOffsets_new[rank];eN<elementOffsets_new[rank+1];eN++)
5551 for(int ebN=0;ebN<mesh.nElementBoundaries_element;ebN++)
5552 {
5553 int ebN_global = elementBoundariesArray_new[eN*mesh.nElementBoundaries_element+ebN];
5554 if(!PetscBTLookup(elementBoundaryMask,ebN_global))
5555 {
5556 bool notFound=true;
5557 for(int nN=0;nN<mesh.nNodes_elementBoundary;nN++)
5558 {
5559 int nN_global_old = mesh.elementBoundaryNodesArray[ebN_global*mesh.nNodes_elementBoundary+nN];
5560 if (nodes_subdomain_owned.count(nN_global_old) > 0)
5561 {
5562 notFound=false;
5563 }
5564 }
5565 if (notFound)
5566 {
5567 //std::cout<<"=========================Face has no owned nodes"<<std::endl;
5568 PetscBTSet(elementBoundaryMask,ebN_global);
5569 elementBoundaries_subdomain_owned.insert(ebN_global);
5570 }
5571 else
5572 {
5573 PetscBTSet(elementBoundaryMask,ebN_global);
5574 elementBoundaries_subdomain_owned.insert(ebN_global);
5575 }
5576 }
5577 }
5578 //std::cout<<"Done marking element boundares "<<elementBoundaries_subdomain_owned.size()<<std::endl;
5579 //ship off the mask
5580 if (rank < size-1)
5581 MPI_Send(elementBoundaryMask,PetscBTLength(mesh.nElementBoundaries_global),MPI_CHAR,rank+1,0,PROTEUS_COMM_WORLD);
5582 ierr = PetscBTDestroy(&elementBoundaryMask);
5583 if (ierr)
5584 cerr<<"Error in PetscBTDestroy for elementBoundaries"<<endl;
5585 //get the number of elementBoundaries on each processor
5586 valarray<int> nElementBoundaries_subdomain_new(size),
5587 elementBoundaryOffsets_new(size+1);
5588 for (int sdN=0;sdN<size;sdN++)
5589 if (sdN == rank)
5590 nElementBoundaries_subdomain_new[sdN] = elementBoundaries_subdomain_owned.size();
5591 else
5592 nElementBoundaries_subdomain_new[sdN] = 0;
5593 valarray<int> nElementBoundaries_subdomain_new_send=nElementBoundaries_subdomain_new;
5594 MPI_Allreduce(&nElementBoundaries_subdomain_new_send[0],&nElementBoundaries_subdomain_new[0],size,MPI_INT,MPI_SUM,PROTEUS_COMM_WORLD);
5595 elementBoundaryOffsets_new[0] = 0;
5596 for (int sdN=0;sdN<size;sdN++)
5597 elementBoundaryOffsets_new[sdN+1] = elementBoundaryOffsets_new[sdN]+nElementBoundaries_subdomain_new[sdN];
5598 //Now as with elements and nodes build a global face numbering
5599 //resetting the face based information is a little different since much of this is currently built below based
5600 //on the element and node information
5601 //
5602 valarray<int> elementBoundaryNumbering_new2old(elementBoundaries_subdomain_owned.size());
5603 set<int>::iterator ebN_ownedp=elementBoundaries_subdomain_owned.begin();
5604 for (int ebN=0;ebN<int(elementBoundaries_subdomain_owned.size());ebN++)
5605 {
5606 elementBoundaryNumbering_new2old[ebN] = *ebN_ownedp++;
5607 }
5608 IS elementBoundaryNumberingIS_new2old;
5609 ISCreateGeneral(PROTEUS_COMM_WORLD,elementBoundaries_subdomain_owned.size(),&elementBoundaryNumbering_new2old[0],PETSC_COPY_VALUES,&elementBoundaryNumberingIS_new2old);
5610 IS elementBoundaryNumberingIS_global_new2old;
5611 ISAllGather(elementBoundaryNumberingIS_new2old,&elementBoundaryNumberingIS_global_new2old);
5612 const PetscInt *elementBoundaryNumbering_global_new2old;
5613 valarray<int> elementBoundaryNumbering_old2new_global(mesh.nElementBoundaries_global);
5614 ISGetIndices(elementBoundaryNumberingIS_global_new2old,&elementBoundaryNumbering_global_new2old);
5615 for (int ebN=0;ebN<mesh.nElementBoundaries_global;ebN++)
5616 {
5617 elementBoundaryNumbering_old2new_global[elementBoundaryNumbering_global_new2old[ebN]] = ebN;
5618 }
5619 for (int eN=0;eN < mesh.nElements_global; eN++)
5620 {
5621 int ebN_old;
5622 for (int ebN=0;ebN < mesh.nElementBoundaries_element; ebN++)
5623 {
5624 ebN_old = elementBoundariesArray_new[eN*mesh.nElementBoundaries_element+ebN];
5625 elementBoundariesArray_new[eN*mesh.nElementBoundaries_element+ebN] = elementBoundaryNumbering_old2new_global[ebN_old];
5626 }
5627 }
5628 //redo
5629 for (int ebN=0;ebN<mesh.nElementBoundaries_global;ebN++)
5630 {
5631 int ebN_old=elementBoundaryNumbering_global_new2old[ebN];
5632 for (int nN=0; nN<mesh.nNodes_elementBoundary; nN++)
5633 {
5634 int nN_old = mesh.elementBoundaryNodesArray[ebN_old*mesh.nNodes_elementBoundary+nN];
5635 elementBoundaryNodesArray_new[ebN*mesh.nNodes_elementBoundary+nN]=nodeNumbering_old2new_global[nN_old];
5636 }
5637 int eN_L_old = mesh.elementBoundaryElementsArray[ebN_old*2+0],
5638 eN_R_old = mesh.elementBoundaryElementsArray[ebN_old*2+1];
5639 elementBoundaryElementsArray_new[ebN*2+0] = elementNumbering_global_old2new[eN_L_old];
5640 if(eN_R_old >= 0)
5641 elementBoundaryElementsArray_new[ebN*2+1] = elementNumbering_global_old2new[eN_R_old];
5642
5643 elementBoundaryMaterialTypes_new[ebN] = mesh.elementBoundaryMaterialTypes[ebN_old];
5644 }
5645 // //mwf debug check constistency
5646 // for (int ebN=0; ebN<mesh.nElementBoundaries_global;ebN++)
5647 // {
5648 // int eN_left=elementBoundaryElementsArray_new[ebN*2+0];
5649 // int eN_right=elementBoundaryElementsArray_new[ebN*2+1];
5650 // assert(eN_left>=0);
5651 // bool found_ebN_left=false;
5652 // for (int ebN_element=0; ebN_element<mesh.nElementBoundaries_element; ebN_element++)
5653 // {
5654 // if (ebN == elementBoundariesArray_new[eN_left*mesh.nElementBoundaries_element+ebN_element])
5655 // {
5656 // assert(ebN_element==elementBoundaryLocalElementBoundariesArray_new[ebN*2+0]);
5657 // found_ebN_left=true;
5658 // }
5659 // }
5660 // assert(found_ebN_left);
5661 // if (eN_right>=0)
5662 // {
5663 // bool found_ebN_right=false;
5664 // for (int ebN_element=0; ebN_element<mesh.nElementBoundaries_element; ebN_element++)
5665 // {
5666 // if (ebN == elementBoundariesArray_new[eN_right*mesh.nElementBoundaries_element+ebN_element])
5667 // {
5668 // assert(ebN_element==elementBoundaryLocalElementBoundariesArray_new[ebN*2+1]);
5669 // found_ebN_right=true;
5670 // }
5671 // }
5672 // assert(found_ebN_right);
5673 // }
5674 // }
5675
5676 //do not renumber interior and exterior element boundary arrays yet
5677
5678 //write partitioned mesh to view with "showme"
5679 // std::ofstream nodeout("mesh.node"),eleout("mesh.ele"),partout("mesh.part");
5680 // eleout<<mesh.nElements_global<<" 3 0"<<std::endl;
5681 // partout<<mesh.nElements_global<<"\t"<<size<<std::endl;
5682 // for (int eN=0;eN<mesh.nElements_global;eN++)
5683 // {
5684 // partout<<(eN+1)<<"\t"<<(1+epart[elementNumbering_global_new2old[eN]])<<std::endl;
5685 // //partout<<(eN+1)<<"\t"<<(1+epart[eN])<<std::endl;
5686 // eleout<<(eN+1)<<"\t"<<(1+elementNodesArray_new[eN*3+0])
5687 // <<"\t"<<(1+elementNodesArray_new[eN*3+1])
5688 // <<"\t"<<(1+elementNodesArray_new[eN*3+2])
5689 // <<std::endl;
5690 // }
5691 // nodeout<<mesh.nNodes_global<<" 2 0 0"<<std::endl;
5692 // for (int nN=0;nN<mesh.nNodes_global;nN++)
5693 // {
5694 // nodeout<<(nN+1)<<"\t"<<nodeArray_new[nN*3+0]<<"\t"<<nodeArray_new[nN*3+1]<<std::endl;
5695 // }
5696 // eleout.close();
5697 // partout.close();
5698
5699 //4c. Build global edge numbering as well
5700 // ownership is determined by the edges on owned elements, then
5701 // who owns the left (0) node of the edge
5702 MPI_Status status_edges;
5703 PetscBT edgesMask;
5704 PetscBTCreate(mesh.nEdges_global,&edgesMask);
5705 if (rank > 0)
5706 {
5707 MPI_Recv(edgesMask,PetscBTLength(mesh.nEdges_global),MPI_CHAR,rank-1,0,PROTEUS_COMM_WORLD,&status_edges);
5708 }
5709 //mark the unmarked faces on this subdomain and store the global face numbers
5710 map<NodeTuple<2>, int> nodesEdgeMap_global; //new global node numbers --> original edge numbering
5711 set<int> edges_subdomain_owned;
5712 for (int ig = 0; ig < mesh.nEdges_global; ig++)
5713 {
5714 int nodes[2];
5715 nodes[0] = edgeNodesArray_new[2*ig];
5716 nodes[1] = edgeNodesArray_new[2*ig+1];
5717 NodeTuple<2> et(nodes);
5718 nodesEdgeMap_global[et] = ig;
5719 }
5720 for(int eN=elementOffsets_new[rank];eN<elementOffsets_new[rank+1];eN++)
5721 {
5722 for (int nN0=0;nN0<mesh.nNodes_element;nN0++)//assume all nodes in element may be edges
5723 for (int nN1=nN0+1; nN1<mesh.nNodes_element;nN1++)
5724 {
5725 int nodes[2];
5726 nodes[0] = elementNodesArray_new[eN*mesh.nNodes_element+nN0];
5727 nodes[1] = elementNodesArray_new[eN*mesh.nNodes_element+nN1];
5728 NodeTuple<2> et(nodes);
5729 if (nodesEdgeMap_global.find(et) != nodesEdgeMap_global.end())
5730 {
5731 int edge_global = nodesEdgeMap_global[et];
5732 if(!PetscBTLookup(edgesMask,edge_global))
5733 {
5734 PetscBTSet(edgesMask,edge_global);
5735 edges_subdomain_owned.insert(nodesEdgeMap_global[et]);
5736 }
5737 }
5738 }
5739 }
5740 if (rank < size-1)
5741 MPI_Send(edgesMask,PetscBTLength(mesh.nEdges_global),MPI_CHAR,rank+1,0,PROTEUS_COMM_WORLD);
5742 ierr = PetscBTDestroy(&edgesMask);
5743 if (ierr)
5744 cerr<<"Error in PetscBTDestroy for edges"<<endl;
5745
5746 valarray<int> nEdges_subdomain_new(size),
5747 edgeOffsets_new(size+1);
5748
5749 for (int sdN=0; sdN < size; sdN++)
5750 {
5751 if (sdN == rank)
5752 nEdges_subdomain_new[sdN] = edges_subdomain_owned.size();
5753 else
5754 nEdges_subdomain_new[sdN] = 0;
5755 }
5756 //collect ownership info
5757 valarray<int> nEdges_subdomain_new_send=nEdges_subdomain_new;
5758 MPI_Allreduce(&nEdges_subdomain_new_send[0],&nEdges_subdomain_new[0],size,MPI_INT,MPI_SUM,PROTEUS_COMM_WORLD);
5759 edgeOffsets_new[0] = 0;
5760 for (int sdN=0;sdN<size;sdN++)
5761 edgeOffsets_new[sdN+1] = edgeOffsets_new[sdN]+nEdges_subdomain_new[sdN];
5762
5763 //build new petsc numbering and global maps from old2new and new2old
5764 valarray<int> edgeNumbering_new2old(edges_subdomain_owned.size());
5765 set<int>::iterator edges_ownedp = edges_subdomain_owned.begin();
5766 for (int i=0; i < int(edges_subdomain_owned.size());i++)
5767 edgeNumbering_new2old[i] = *edges_ownedp++;
5768
5769 IS edgeNumberingIS_new2old;
5770 ISCreateGeneral(PROTEUS_COMM_WORLD,edges_subdomain_owned.size(),&edgeNumbering_new2old[0],PETSC_COPY_VALUES,&edgeNumberingIS_new2old);
5771 IS edgeNumberingIS_global_new2old;
5772 ISAllGather(edgeNumberingIS_new2old,&edgeNumberingIS_global_new2old);
5773 const PetscInt *edgeNumbering_global_new2old;
5774
5775
5776 valarray<int> edgeNumbering_old2new_global(mesh.nEdges_global);
5777 ISGetIndices(edgeNumberingIS_global_new2old,&edgeNumbering_global_new2old);
5778 for (int ig=0;ig<mesh.nEdges_global;ig++)
5779 {
5780 edgeNumbering_old2new_global[edgeNumbering_global_new2old[ig]] = ig;
5781 }
5782
5783 //create array with (new edge) --> (new node 0, new node 1)
5784 //and map from (new node 0, new node 1) --> (new global edge)
5785 valarray<int> edgeNodesArray_newNodesAndEdges(2*mesh.nEdges_global);
5786 map<NodeTuple<2>,int > nodesEdgeMap_global_new;
5787 for (int ig = 0; ig < mesh.nEdges_global; ig++)
5788 {
5789 int nodes[2];
5790 const int edge_old = edgeNumbering_global_new2old[ig];
5791 nodes[0] = edgeNodesArray_new[edge_old*2+0];
5792 nodes[1] = edgeNodesArray_new[edge_old*2+1];
5793 NodeTuple<2> et(nodes);
5794 edgeNodesArray_newNodesAndEdges[ig*2+0] = edgeNodesArray_new[edge_old*2+0];
5795 edgeNodesArray_newNodesAndEdges[ig*2+1] = edgeNodesArray_new[edge_old*2+1];
5796 assert(nodesEdgeMap_global_new.find(et) == nodesEdgeMap_global_new.end());
5797 nodesEdgeMap_global_new[et] = ig;
5798 }
5799
5800 //5. At this point we have new, renumbered and sorted the global element, and node based information, and
5801 //we have it all on each processor so we can add overlap
5802 set<int> elements_overlap,nodes_overlap,elementBoundaries_overlap,edges_overlap;
5803 for(int eN=elementOffsets_new[rank];eN<elementOffsets_new[rank+1];eN++)
5804 {
5805 for (int nN=0;nN<mesh.nNodes_element;nN++)
5806 {
5807 int nN_global = elementNodesArray_new[eN*mesh.nNodes_element+nN];
5808 if (nN_global < nodeOffsets_new[rank] || nN_global >= nodeOffsets_new[rank+1])
5809 nodes_overlap.insert(nN_global);
5810 }
5811 for (int ebN=0; ebN<mesh.nElementBoundaries_element;ebN++)
5812 {
5813 int ebN_global=elementBoundariesArray_new[eN*mesh.nElementBoundaries_element+ebN];
5814 if (ebN_global < elementBoundaryOffsets_new[rank] || ebN_global >= elementBoundaryOffsets_new[rank+1])
5815 elementBoundaries_overlap.insert(ebN_global);
5816 }
5817 for (int nN0=0;nN0<mesh.nNodes_element;nN0++)//assume all nodes in element may be edges
5818 for (int nN1=nN0+1; nN1<mesh.nNodes_element;nN1++)
5819 {
5820 int nodes[2];
5821 nodes[0] = elementNodesArray_new[eN*mesh.nNodes_element+nN0];
5822 nodes[1] = elementNodesArray_new[eN*mesh.nNodes_element+nN1];
5823 NodeTuple<2> et(nodes);
5824 const int nN0_global = elementNodesArray_new[eN*mesh.nNodes_element+nN0];
5825 const int nN1_global = elementNodesArray_new[eN*mesh.nNodes_element+nN1];
5826 if (nN0_global < nodeOffsets_new[rank] || nN0_global >= nodeOffsets_new[rank+1])
5827 assert(nodes_overlap.find(nN0_global) != nodes_overlap.end());
5828 if (nN1_global < nodeOffsets_new[rank] || nN1_global >= nodeOffsets_new[rank+1])
5829 assert(nodes_overlap.find(nN1_global) != nodes_overlap.end());
5830 if (nodesEdgeMap_global_new.find(et) != nodesEdgeMap_global_new.end())
5831 {
5832 const int edge_global = nodesEdgeMap_global_new[et];
5833 if (edge_global < edgeOffsets_new[rank] || edge_global >= edgeOffsets_new[rank+1])
5834 edges_overlap.insert(edge_global);
5835 }
5836 }//edges
5837 }
5838
5839 if (nElements_overlap > 0)
5840 {
5841 //get all elements in the node stars and their nodes
5842 // for (int nN_new=nodeOffsets_new[rank]; nN_new < nodeOffsets_new[rank+1]; nN_new++)
5843 // {
5844 // int nN = nodeNumbering_global_new2old[nN_new];
5845 // for (int offset =mesh.nodeElementOffsets[nN];offset<mesh.nodeElementOffsets[nN+1];offset++)
5846 // {
5847 // int eN = mesh.nodeElementsArray[offset];
5848 // int eN_new = elementNumbering_global_old2new[eN];
5849 // if (eN_new < elementOffsets_new[rank] or eN_new >= elementOffsets_new[rank+1])
5850 // {
5851 // elements_overlap.insert(eN_new);
5852 // for (int nN_element=0;nN_element<mesh.nNodes_element;nN_element++)
5853 // {
5854 // int nN_global = elementNodesArray_new[eN_new*mesh.nNodes_element+nN_element];
5855 // if (nN_global < nodeOffsets_new[rank] or nN_global >= nodeOffsets_new[rank+1])
5856 // nodes_overlap.insert(nN_global);
5857 // }
5858 // }
5859 // }
5860 // }
5861 //get all elements in the node stars of owned nodes and those elements' nodes and faces and edges
5862 for (set<int>::iterator nN=nodes_subdomain_owned.begin();nN != nodes_subdomain_owned.end();nN++)
5863 {
5864 for (int offset =mesh.nodeElementOffsets[*nN];offset<mesh.nodeElementOffsets[(*nN)+1];offset++)
5865 {
5866 int eN = mesh.nodeElementsArray[offset];
5867 int eN_new = elementNumbering_global_old2new[eN];
5868 if (eN_new < elementOffsets_new[rank] or eN_new >= elementOffsets_new[rank+1])
5869 {
5870 elements_overlap.insert(eN_new);
5871 for (int nN_element=0;nN_element<mesh.nNodes_element;nN_element++)
5872 {
5873 int nN_global = elementNodesArray_new[eN_new*mesh.nNodes_element+nN_element];
5874 if (nN_global < nodeOffsets_new[rank] or nN_global >= nodeOffsets_new[rank+1])
5875 nodes_overlap.insert(nN_global);
5876 }
5877 for (int ebN=0; ebN<mesh.nElementBoundaries_element;ebN++)
5878 {
5879 int ebN_global=elementBoundariesArray_new[eN_new*mesh.nElementBoundaries_element+ebN];
5880 if (ebN_global < elementBoundaryOffsets_new[rank] || ebN_global >= elementBoundaryOffsets_new[rank+1])
5881 elementBoundaries_overlap.insert(ebN_global);
5882 }
5883 //edges too
5884 for (int nN0=0;nN0<mesh.nNodes_element;nN0++)
5885 for (int nN1=nN0+1; nN1<mesh.nNodes_element;nN1++)
5886 {
5887 int nodes[2];
5888 nodes[0] = elementNodesArray_new[eN_new*mesh.nNodes_element+nN0];
5889 nodes[1] = elementNodesArray_new[eN_new*mesh.nNodes_element+nN1];
5890 NodeTuple<2> et(nodes);
5891 const int nN0_global = elementNodesArray_new[eN_new*mesh.nNodes_element+nN0];
5892 const int nN1_global = elementNodesArray_new[eN_new*mesh.nNodes_element+nN1];
5893 if (nodesEdgeMap_global_new.find(et) != nodesEdgeMap_global_new.end())
5894 {
5895 const int edge_global = nodesEdgeMap_global_new[et];
5896 if (edge_global < edgeOffsets_new[rank] || edge_global >= edgeOffsets_new[rank+1])
5897 edges_overlap.insert(edge_global);
5898 }
5899 }//edges
5900 }
5901 }
5902 }
5903 //get all the element neighbors
5904 for(int eN=elementOffsets_new[rank];eN<elementOffsets_new[rank+1];eN++)
5905 for(int ebN=0;ebN<mesh.nElementBoundaries_element;ebN++)
5906 {
5907 int eN_ebN = elementNeighborsArray_new[eN*mesh.nElementBoundaries_element+ebN];
5908 if (eN_ebN >= 0 &&
5909 (eN_ebN < elementOffsets_new[rank] || eN_ebN >= elementOffsets_new[rank+1]))
5910 {
5911 elements_overlap.insert(eN_ebN);
5912 for (int nN=0;nN<mesh.nNodes_element;nN++)
5913 {
5914 int nN_global = elementNodesArray_new[eN_ebN*mesh.nNodes_element+nN];
5915 if (nN_global < nodeOffsets_new[rank] || nN_global >= nodeOffsets_new[rank+1])
5916 nodes_overlap.insert(nN_global);
5917 }
5918 for (int ebN_element=0; ebN_element<mesh.nElementBoundaries_element;ebN_element++)
5919 {
5920 int ebN_global=elementBoundariesArray_new[eN_ebN*mesh.nElementBoundaries_element+ebN_element];
5921 if (ebN_global < elementBoundaryOffsets_new[rank] || ebN_global >= elementBoundaryOffsets_new[rank+1])
5922 elementBoundaries_overlap.insert(ebN_global);
5923 }
5924 //edges too
5925 for (int nN0=0;nN0<mesh.nNodes_element;nN0++)
5926 for (int nN1=nN0+1; nN1<mesh.nNodes_element;nN1++)
5927 {
5928 int nodes[2];
5929 nodes[0] = elementNodesArray_new[eN_ebN*mesh.nNodes_element+nN0];
5930 nodes[1] = elementNodesArray_new[eN_ebN*mesh.nNodes_element+nN1];
5931 NodeTuple<2> et(nodes);
5932 const int nN0_global = elementNodesArray_new[eN_ebN*mesh.nNodes_element+nN0];
5933 const int nN1_global = elementNodesArray_new[eN_ebN*mesh.nNodes_element+nN1];
5934 bool foundEdge = false;
5935 const int edge_global = nodesEdgeMap_global_new[et];
5936 if (edge_global < edgeOffsets_new[rank] || edge_global >= edgeOffsets_new[rank+1])
5937 edges_overlap.insert(edge_global);
5938 }//edges
5939 }
5940 }
5941 }
5942 for (int layer=1;layer<nElements_overlap;layer++)
5943 {
5944 for (set<int>::iterator eN_p=elements_overlap.begin();eN_p != elements_overlap.end();eN_p++)
5945 {
5946 int eN_global = *eN_p;
5947 for(int ebN=0;ebN<mesh.nElementBoundaries_element;ebN++)
5948 {
5949 int eN_ebN = elementNeighborsArray_new[eN_global*mesh.nElementBoundaries_element+ebN];
5950 if (eN_ebN >= 0 &&
5951 (eN_ebN < elementOffsets_new[rank] || eN_ebN >= elementOffsets_new[rank+1]))
5952 {
5953 elements_overlap.insert(eN_ebN);
5954 for (int nN=0;nN<mesh.nNodes_element;nN++)
5955 {
5956 int nN_global = elementNodesArray_new[eN_ebN*mesh.nNodes_element+nN];
5957 if (nN_global < nodeOffsets_new[rank] || nN_global >= nodeOffsets_new[rank+1])
5958 nodes_overlap.insert(nN_global);
5959 }
5960 for (int ebN_element=0; ebN_element<mesh.nElementBoundaries_element;ebN_element++)
5961 {
5962 int ebN_global=elementBoundariesArray_new[eN_ebN*mesh.nElementBoundaries_element+ebN_element];
5963 if (ebN_global < elementBoundaryOffsets_new[rank] || ebN_global >= elementBoundaryOffsets_new[rank+1])
5964 elementBoundaries_overlap.insert(ebN_global);
5965 }
5966 //edges too
5967 for (int nN0=0;nN0<mesh.nNodes_element;nN0++)
5968 for (int nN1=nN0+1; nN1<mesh.nNodes_element;nN1++)
5969 {
5970 int nodes[2];
5971 nodes[0] = elementNodesArray_new[eN_ebN*mesh.nNodes_element+nN0];
5972 nodes[1] = elementNodesArray_new[eN_ebN*mesh.nNodes_element+nN1];
5973 NodeTuple<2> et(nodes);
5974 const int nN0_global = elementNodesArray_new[eN_ebN*mesh.nNodes_element+nN0];
5975 const int nN1_global = elementNodesArray_new[eN_ebN*mesh.nNodes_element+nN1];
5976 bool foundEdge = false;
5977 if (nodesEdgeMap_global_new.find(et) != nodesEdgeMap_global_new.end())
5978 {
5979 const int edge_global = nodesEdgeMap_global_new[et];
5980 if (edge_global < edgeOffsets_new[rank] || edge_global >= edgeOffsets_new[rank+1])
5981 edges_overlap.insert(edge_global);
5982 }
5983 }//edges
5984 }
5985 }
5986 }
5987 }
5988 //
5989 //6. Now build subdomain mesh
5990 //
5991 //set what we know
5992 if (mesh.subdomainp == NULL)
5993 mesh.subdomainp = new Mesh();
5994 mesh.subdomainp->nElements_global = nElements_subdomain_new[rank] + elements_overlap.size();
5995 mesh.subdomainp->nNodes_global = nNodes_subdomain_new[rank] + nodes_overlap.size();
5996 //better be true ... check below
5997 mesh.subdomainp->nElementBoundaries_global = nElementBoundaries_subdomain_new[rank]+elementBoundaries_overlap.size();
5998 mesh.subdomainp->nEdges_global = nEdges_subdomain_new[rank] + edges_overlap.size();
5999
6003 //load the elements, nodes. and elementBoundaries
6004 valarray<int> nodeNumbering_subdomain2global(mesh.subdomainp->nNodes_global);
6005 map<int,int> nodeNumbering_global2subdomain;
6006 mesh.subdomainp->nodeArray = new double[mesh.subdomainp->nNodes_global*3];
6008 for(int nN=0;nN<nNodes_subdomain_new[rank];nN++)
6009 {
6010 int nN_global = nN + nodeOffsets_new[rank];
6011 nodeNumbering_subdomain2global[nN] = nN_global;
6012 nodeNumbering_global2subdomain[nN_global] = nN;
6013 mesh.subdomainp->nodeArray[nN*3+0] = nodeArray_new[nN_global*3+0];
6014 mesh.subdomainp->nodeArray[nN*3+1] = nodeArray_new[nN_global*3+1];
6015 mesh.subdomainp->nodeArray[nN*3+2] = nodeArray_new[nN_global*3+2];
6016 mesh.subdomainp->nodeMaterialTypes[nN]= nodeMaterialTypes_new[nN_global];
6017 }
6018 //note: sets in C++ are sorted so the overlap is laid out in
6019 //contiguous chunks corresponding to the partitions
6020 set<int>::iterator nN_p=nodes_overlap.begin();
6021 for(int nN=nNodes_subdomain_new[rank];nN < nNodes_subdomain_new[rank] + int(nodes_overlap.size()); nN++)
6022 {
6023 int nN_global = *nN_p++;
6024 nodeNumbering_subdomain2global[nN] = nN_global;
6025 nodeNumbering_global2subdomain[nN_global] = nN;
6026 mesh.subdomainp->nodeArray[nN*3+0] = nodeArray_new[nN_global*3+0];
6027 mesh.subdomainp->nodeArray[nN*3+1] = nodeArray_new[nN_global*3+1];
6028 mesh.subdomainp->nodeArray[nN*3+2] = nodeArray_new[nN_global*3+2];
6029 mesh.subdomainp->nodeMaterialTypes[nN]= nodeMaterialTypes_new[nN_global];
6030 }
6031
6032 //see what we must/can set for element boundaries here
6033 valarray<int> elementBoundaryNumbering_subdomain2global(mesh.subdomainp->nElementBoundaries_global);
6034 map<int,int> elementBoundaryNumbering_global2subdomain;
6035 for (int ebN=0; ebN < nElementBoundaries_subdomain_new[rank]; ebN++)
6036 {
6037 int ebN_global = ebN + elementBoundaryOffsets_new[rank];
6038 elementBoundaryNumbering_subdomain2global[ebN]=ebN_global;
6039 elementBoundaryNumbering_global2subdomain[ebN_global] = ebN;
6040 }
6041 set<int>::iterator ebN_p = elementBoundaries_overlap.begin();
6042 for(int ebN=nElementBoundaries_subdomain_new[rank];ebN < nElementBoundaries_subdomain_new[rank] + int(elementBoundaries_overlap.size()); ebN++)
6043 {
6044 int ebN_global = *ebN_p++;
6045 elementBoundaryNumbering_subdomain2global[ebN] = ebN_global;
6046 elementBoundaryNumbering_global2subdomain[ebN_global] = ebN;
6047 }
6048
6049
6052 //try to use elementBoundariesArray to set unique element boundary id below
6055 valarray<int> elementNumbering_subdomain2global(mesh.subdomainp->nElements_global);
6056 for (int eN=0;eN<nElements_subdomain_new[rank];eN++)
6057 {
6058 int eN_global = eN+elementOffsets_new[rank];
6059 elementNumbering_subdomain2global[eN] = eN_global;
6060 mesh.subdomainp->elementMaterialTypes[eN] = elementMaterialTypes_new[eN_global];
6061 for (int nN=0;nN<mesh.subdomainp->nNodes_element;nN++)
6062 {
6064 nodeNumbering_global2subdomain[elementNodesArray_new[eN_global*mesh.nNodes_element + nN]];
6065 }
6066 for (int ebN=0;ebN<mesh.subdomainp->nElementBoundaries_element;ebN++)
6068 elementBoundaryNumbering_global2subdomain[elementBoundariesArray_new[eN_global*mesh.nElementBoundaries_element + ebN]];
6069 }
6070 set<int>::iterator eN_p=elements_overlap.begin();
6071 for(int eN=nElements_subdomain_new[rank];eN < nElements_subdomain_new[rank]+int(elements_overlap.size());eN++)
6072 {
6073 int eN_global = *eN_p++;
6074 mesh.subdomainp->elementMaterialTypes[eN] = elementMaterialTypes_new[eN_global];
6075 elementNumbering_subdomain2global[eN] = eN_global;
6076 for (int nN=0;nN<mesh.subdomainp->nNodes_element;nN++)
6077 {
6079 nodeNumbering_global2subdomain[elementNodesArray_new[eN_global*mesh.nNodes_element + nN]];
6080 }
6081 for (int ebN=0;ebN<mesh.subdomainp->nElementBoundaries_element;ebN++)
6083 elementBoundaryNumbering_global2subdomain[elementBoundariesArray_new[eN_global*mesh.nElementBoundaries_element + ebN]];
6084 }
6085
6086 //set edge information, main piece necessary for setting subdomain data structures consistently is a map
6087 //from (nN0,nN1) --> edge_subdomain
6088 valarray<int> edgeNumbering_subdomain2global(mesh.subdomainp->nEdges_global);
6089 mesh.subdomainp->edgeNodesArray = new int[mesh.subdomainp->nEdges_global*2];
6090 for (int i=0; i < nEdges_subdomain_new[rank]; i++)
6091 {
6092 const int ig = i+edgeOffsets_new[rank];
6093 const int nN0_global = edgeNodesArray_newNodesAndEdges[ig*2+0];
6094 const int nN1_global = edgeNodesArray_newNodesAndEdges[ig*2+1];
6095 //mwf todo double check can always count on having nodes on this processor
6096 assert(nodeNumbering_global2subdomain.find(nN0_global) != nodeNumbering_global2subdomain.end());
6097 assert(nodeNumbering_global2subdomain.find(nN1_global) != nodeNumbering_global2subdomain.end());
6098 const int nN0_subdomain = nodeNumbering_global2subdomain[nN0_global];
6099 const int nN1_subdomain = nodeNumbering_global2subdomain[nN1_global];
6100 mesh.subdomainp->edgeNodesArray[2*i+0]=nN0_subdomain;
6101 mesh.subdomainp->edgeNodesArray[2*i+1]=nN1_subdomain;
6102 edgeNumbering_subdomain2global[i] = ig;
6103 }
6104 set<int>::iterator edge_p = edges_overlap.begin();
6105 for (int i=nEdges_subdomain_new[rank]; i < nEdges_subdomain_new[rank] + int(edges_overlap.size()); i++)
6106 {
6107 const int ig =*edge_p++;
6108 const int nN0_global = edgeNodesArray_newNodesAndEdges[ig*2+0];
6109 const int nN1_global = edgeNodesArray_newNodesAndEdges[ig*2+1];
6110 //mwf todo make sure always have nodes for the edge on this processor
6111 const int nN0_subdomain = nodeNumbering_global2subdomain[nN0_global];
6112 const int nN1_subdomain = nodeNumbering_global2subdomain[nN1_global];
6113 mesh.subdomainp->edgeNodesArray[2*i+0]=nN0_subdomain;
6114 mesh.subdomainp->edgeNodesArray[2*i+1]=nN1_subdomain;
6115 edgeNumbering_subdomain2global[i] = ig;
6116 }
6117 //now fill in rest of boundary information, etc
6118 mesh.subdomainp->px = mesh.px;
6119 mesh.subdomainp->py = mesh.py;
6120 mesh.subdomainp->pz = mesh.pz;
6121
6122 if (mesh.px != 0)
6123 {
6124 //constructElementBoundaryElementsArrayWithGivenElementBoundaryNumbers_tetrahedron(*mesh.subdomainp);
6128
6129 }
6130 else if (mesh.subdomainp->nNodes_element == 2)
6131 {
6132 //constructElementBoundaryElementsArrayWithGivenElementBoundaryNumbers_edge(*mesh.subdomainp);
6136 }
6137
6138 else if (mesh.subdomainp->nNodes_element == 3)
6139 {
6140 //constructElementBoundaryElementsArrayWithGivenElementBoundaryNumbers_triangle(*mesh.subdomainp);
6144 }
6145 else if (mesh.subdomainp->nNodes_element == 4 && mesh.subdomainp->nNodes_elementBoundary == 2)
6146 {
6147 //constructElementBoundaryElementsArrayWithGivenElementBoundaryNumbers_tetrahedron(*mesh.subdomainp);
6151 }
6152 else if (mesh.subdomainp->nNodes_element == 4)
6153 {
6154 //constructElementBoundaryElementsArrayWithGivenElementBoundaryNumbers_tetrahedron(*mesh.subdomainp);
6158 }
6159 else if (mesh.subdomainp->nNodes_element == 8)
6160 {
6164 }
6165 else
6166 {
6167 assert(false);
6168 }
6169 if (mesh.elementBoundaryMaterialTypes != NULL)
6170 {
6171 assert(mesh.elementBoundariesArray != NULL);
6172 //todo need to check that local element boundary numbering for
6173 //element boundaries stays the same
6174 for (int eN=0;eN<mesh.subdomainp->nElements_global;eN++)
6175 {
6176 int eN_global_new = elementNumbering_subdomain2global[eN];
6177 int eN_global_old = elementNumbering_global_new2old[eN_global_new];
6178 for (int ebN_element = 0; ebN_element < mesh.nElementBoundaries_element; ebN_element++)
6179 {
6180 int ebN_global_old = mesh.elementBoundariesArray[eN_global_old*mesh.nElementBoundaries_element+ebN_element];
6181 int ebN_subdomain = mesh.subdomainp->elementBoundariesArray[eN*mesh.nElementBoundaries_element+ebN_element];
6182 mesh.subdomainp->elementBoundaryMaterialTypes[ebN_subdomain] = mesh.elementBoundaryMaterialTypes[ebN_global_old];
6183 }
6184 }
6185 }
6186 //now we've got the old mesh in the old ordering and the subdomain mesh in the new ordering
6187 //the first chunk of nodes and elements are the owned elements so we need to know how many of those there are
6188 //and the offset of the first one so we can compute subdomain2global
6189 mesh.nodeOffsets_subdomain_owned = new int[size+1];
6190 mesh.elementOffsets_subdomain_owned = new int[size+1];
6191 mesh.elementBoundaryOffsets_subdomain_owned = new int[size+1];
6192 mesh.edgeOffsets_subdomain_owned = new int[size+1];
6193 for (int sdN=0;sdN<size+1;sdN++)
6194 {
6195 mesh.nodeOffsets_subdomain_owned[sdN] = nodeOffsets_new[sdN];
6196 mesh.elementOffsets_subdomain_owned[sdN] = elementOffsets_new[sdN];
6197 mesh.elementBoundaryOffsets_subdomain_owned[sdN] = elementBoundaryOffsets_new[sdN];
6198 mesh.edgeOffsets_subdomain_owned[sdN] = edgeOffsets_new[sdN];
6199 }
6200 //we also need the subdomain 2 new global mappings
6202 for (int nN=0;nN<mesh.subdomainp->nNodes_global;nN++)
6203 mesh.nodeNumbering_subdomain2global[nN] = nodeNumbering_subdomain2global[nN];
6205 for (int eN=0;eN<mesh.subdomainp->nElements_global;eN++)
6206 mesh.elementNumbering_subdomain2global[eN] = elementNumbering_subdomain2global[eN];
6208 for (int ebN=0;ebN<mesh.subdomainp->nElementBoundaries_global;ebN++)
6209 mesh.elementBoundaryNumbering_subdomain2global[ebN] = elementBoundaryNumbering_subdomain2global[ebN];
6211 for (int i=0; i< mesh.subdomainp->nEdges_global; i++)
6212 mesh.edgeNumbering_subdomain2global[i] = edgeNumbering_subdomain2global[i];
6213
6214 //
6215 //go ahead and renumber global mesh
6216 //
6217 //std::cout<<"nodeArray"<<std::endl;
6218 for (int i=0;i<mesh.nNodes_global*3;i++)
6219 mesh.nodeArray[i] = nodeArray_new[i];
6220 //std::cout<<"elementNodesArray"<<std::endl;
6221 for (int i=0;i<mesh.nElements_global*mesh.nNodes_element;i++)
6222 mesh.elementNodesArray[i] = elementNodesArray_new[i];
6223 //std::cout<<"elementBoundaryNodesArray"<<std::endl;
6224 for (int i=0;i<mesh.nElementBoundaries_global*mesh.nNodes_elementBoundary;i++)
6225 mesh.elementBoundaryNodesArray[i] = elementBoundaryNodesArray_new[i];
6226 //std::cout<<"edgeNodesArray"<<std::endl;
6227 for (int i=0;i<mesh.nEdges_global*2;i++)
6228 mesh.edgeNodesArray[i] = edgeNodesArray_newNodesAndEdges[i];
6229 //std::cout<<"nodeStarArray"<<std::endl;
6230 for (int i=0;i<mesh.nodeStarOffsets[mesh.nNodes_global];i++)
6231 mesh.nodeStarArray[i] = nodeStarArray_new[i];
6232 //std::cout<<"elementNeighborsArray"<<std::endl;
6233 for (int i=0; i<mesh.nElements_global*mesh.nElementBoundaries_element; i++)
6234 mesh.elementNeighborsArray[i] = elementNeighborsArray_new[i];
6235 //std::cout<<"elementBoundariesArray"<<std::endl;
6236 for (int i=0; i<mesh.nElements_global*mesh.nElementBoundaries_element; i++)
6237 mesh.elementBoundariesArray[i] = elementBoundariesArray_new[i];
6238 //std::cout<<"elementBoundaryElementsArray"<<std::endl;
6239 for (int i=0; i<mesh.nElementBoundaries_global*2; i++)
6240 mesh.elementBoundaryElementsArray[i] = elementBoundaryElementsArray_new[i];
6241 //std::cout<<"elementBoundaryLocalElementBoundariesArray"<<std::endl;
6242 for (int i=0; i<mesh.nElementBoundaries_global*2; i++)
6243 mesh.elementBoundaryLocalElementBoundariesArray[i] = elementBoundaryLocalElementBoundariesArray_new[i];
6244 //
6245 //need material properties too
6246 //
6247 if (mesh.elementMaterialTypes != NULL)
6248 {
6249 //std::cout<<"elementMaterialTypes"<<std::endl;
6250 for (int i=0; i < mesh.nElements_global; i++)
6251 mesh.elementMaterialTypes[i] = elementMaterialTypes_new[i];
6252 }
6253 if (mesh.elementBoundaryMaterialTypes != NULL)
6254 {
6255 //std::cout<<"elementBoundaryMaterialTypes"<<std::endl;
6256 for (int i=0; i < mesh.nElementBoundaries_global; i++)
6257 mesh.elementBoundaryMaterialTypes[i] = elementBoundaryMaterialTypes_new[i];
6258 }
6259
6260 ISRestoreIndices(elementNumberingIS_global_old2new,&elementNumbering_global_old2new);
6261
6262 ISDestroy(&elementPartitioningIS_new);
6263 ISDestroy(&elementNumberingIS_subdomain_old2new);
6264 ISDestroy(&elementNumberingIS_global_old2new);
6265
6266 ISRestoreIndices(nodeNumberingIS_global_new2old,&nodeNumbering_global_new2old);
6267
6268 ISDestroy(&nodeNumberingIS_new2old);
6269 ISDestroy(&nodeNumberingIS_global_new2old);
6270
6271 ISRestoreIndices(elementBoundaryNumberingIS_global_new2old,&elementBoundaryNumbering_global_new2old);
6272
6273 ISDestroy(&elementBoundaryNumberingIS_new2old);
6274 ISDestroy(&elementBoundaryNumberingIS_global_new2old);
6275
6276 ISRestoreIndices(edgeNumberingIS_global_new2old,&edgeNumbering_global_new2old);
6277
6278 ISDestroy(&edgeNumberingIS_new2old);
6279 ISDestroy(&edgeNumberingIS_global_new2old);
6280
6281 return 0;
6282}
6283
6284int buildQuadraticSubdomain2GlobalMappings_1d(const MPI_Comm& PROTEUS_COMM_WORLD, Mesh& mesh,
6285 const int *elementOffsets_subdomain_owned,
6286 const int *nodeOffsets_subdomain_owned,
6287 const int *elementNumbering_subdomain2global,
6288 const int *nodeNumbering_subdomain2global,
6289 int& nDOF_all_processes,//total number of dofs in whole domain
6290 int& nDOF_subdomain,//total number of dofs in sub-domain
6291 int& max_dof_neighbors,//maximum number of neighbors for connectivity of dofs
6292 int *offsets_subdomain_owned, //starting point of local dofs on each processor (nProcs+1)
6293 int * subdomain_l2g, //local to global dof mapping on subdomain
6294 int* subdomain2global,//subdomain dof to global (parallel) numbering
6295 double * lagrangeNodesArray)//location of nodes corresponding to dofs
6296{
6297 using namespace std;
6298 int ierr,size,rank;
6299
6300 ierr = MPI_Comm_size(PROTEUS_COMM_WORLD,&size);
6301 ierr = MPI_Comm_rank(PROTEUS_COMM_WORLD,&rank);
6302
6303
6304 //In 1d the quadratic dofs can be associated with nodes and elements
6305 //assuming have ownership info and consistent local/global mappings for nodes, elements
6306 //build a mapping from local quadratic dofs to global quadratic dofs for petsc
6307 //assume a processor owns a dof if it owns that node or element
6308 assert(elementOffsets_subdomain_owned);
6309 assert(nodeOffsets_subdomain_owned);
6310 assert(elementNumbering_subdomain2global);
6311 assert(nodeNumbering_subdomain2global);
6312 assert(mesh.subdomainp);
6313
6314 const int nNodes_owned = nodeOffsets_subdomain_owned[rank+1]-nodeOffsets_subdomain_owned[rank];
6315 const int nElements_owned = elementOffsets_subdomain_owned[rank+1]-elementOffsets_subdomain_owned[rank];
6316
6317 //start with a logical global ordering of dofs as
6318 //[global nodes, global elements]
6319 //want to create global numbering
6320 //[nodes proc 0,elements proc 0,nodes proc 1, elements proc 1,...]
6321 valarray<int> quadraticNumbering_new2old(nNodes_owned + nElements_owned);
6322 for (int nN = 0; nN < nNodes_owned; nN++)
6323 {
6324 int dof_global = nodeNumbering_subdomain2global[nN];
6325 quadraticNumbering_new2old[nN] = dof_global;
6326 }
6327 for (int eN = 0; eN < nElements_owned; eN++)
6328 {
6329 int dof_global = mesh.nNodes_global + elementNumbering_subdomain2global[eN];
6330 quadraticNumbering_new2old[nNodes_owned + eN] = dof_global;
6331 }
6332
6333 //build an index set for new numbering
6334 IS quadraticNumberingIS_new2old;
6335 ISCreateGeneral(PROTEUS_COMM_WORLD,nNodes_owned + nElements_owned,&quadraticNumbering_new2old[0],PETSC_COPY_VALUES,&quadraticNumberingIS_new2old);
6336 IS quadraticNumberingIS_global_new2old;
6337 ISAllGather(quadraticNumberingIS_new2old,&quadraticNumberingIS_global_new2old);
6338 //get old 2 new mapping for dofs
6339 const PetscInt *quadraticNumbering_global_new2old;
6340 valarray<int> quadraticNumbering_old2new_global(mesh.nNodes_global + mesh.nElements_global);
6341 ISGetIndices(quadraticNumberingIS_global_new2old,&quadraticNumbering_global_new2old);
6342 for (int id = 0; id < mesh.nNodes_global + mesh.nElements_global; id++)
6343 quadraticNumbering_old2new_global[quadraticNumbering_global_new2old[id]] = id;
6344 // //mwf debug
6345 // for (int id = 0; id < mesh.nNodes_global + mesh.nElements_global; id++)
6346 // {
6347 // std::cout<<" rank= "<<rank<<" build c0p2 mappings new2old["<<id<<"]= "<<quadraticNumbering_global_new2old[id]
6348 // <<" old2new["<<quadraticNumbering_global_new2old[id]<<"]= "<<quadraticNumbering_old2new_global[quadraticNumbering_global_new2old[id]]
6349 // <<std::endl;
6350 // }
6351 assert(offsets_subdomain_owned);
6352 assert(subdomain2global);
6353 assert(subdomain_l2g);
6354 assert(lagrangeNodesArray);
6355 nDOF_all_processes = mesh.nNodes_global+mesh.nElements_global;
6356 nDOF_subdomain = mesh.subdomainp->nNodes_global+mesh.subdomainp->nElements_global;
6357 max_dof_neighbors = 2*mesh.max_nNodeNeighbors_node;
6358
6359 for (int sdN=0; sdN < size+1; sdN++)
6360 offsets_subdomain_owned[sdN] = nodeOffsets_subdomain_owned[sdN]+elementOffsets_subdomain_owned[sdN];
6361
6362 //loop through owned and ghost dofs build subdomain mapping by
6363 //going from old --> new
6364 int localOffset = 0;
6365 for (int nN = 0; nN < nNodes_owned; nN++)
6366 {
6367 int dof_global_old = nodeNumbering_subdomain2global[nN];
6368 int dof_global_new = quadraticNumbering_old2new_global[dof_global_old];
6369 subdomain2global[localOffset + nN] = dof_global_new;
6370 //mwf debug
6371 //std::cout<<" rank= "<<rank<<" nN= "<<nN<<" local dof= "<<localOffset+nN<<" nNodes_owned= "<<nNodes_owned<<" dof_old= "<<dof_global_old<<" new= "<<dof_global_new<<std::endl;
6372 }
6373 localOffset += nNodes_owned;
6374 for (int eN = 0; eN < nElements_owned; eN++)
6375 {
6376 int dof_global_old = mesh.nNodes_global + elementNumbering_subdomain2global[eN];
6377 int dof_global_new = quadraticNumbering_old2new_global[dof_global_old];
6378 subdomain2global[localOffset + eN] = dof_global_new;
6379 //mwf debug
6380 //std::cout<<" rank= "<<rank<<" eN= "<<eN<<" local dof= "<<localOffset+eN<<" nElements_owned= "<<nElements_owned<<" dof_old= "<<dof_global_old<<" new= "<<dof_global_new<<std::endl;
6381 }
6382 localOffset += nElements_owned;
6383 for (int nN = nNodes_owned; nN < mesh.subdomainp->nNodes_global; nN++)
6384 {
6385 int dof_global_old = nodeNumbering_subdomain2global[nN];
6386 int dof_global_new = quadraticNumbering_old2new_global[dof_global_old];
6387 subdomain2global[localOffset + nN -nNodes_owned] = dof_global_new;
6388 //mwf debug
6389 //std::cout<<" rank= "<<rank<<" nN= "<<nN<<" local dof= "<<localOffset+nN-nNodes_owned<<" nNodes_owned= "<<nNodes_owned<<" dof_old= "<<dof_global_old<<" new= "<<dof_global_new<<std::endl;
6390 }
6391 localOffset += mesh.subdomainp->nNodes_global - nNodes_owned;
6392 for (int eN = nElements_owned; eN < mesh.subdomainp->nElements_global; eN++)
6393 {
6394 int dof_global_old = mesh.nNodes_global + elementNumbering_subdomain2global[eN];
6395 int dof_global_new = quadraticNumbering_old2new_global[dof_global_old];
6396 subdomain2global[localOffset + eN-nElements_owned] = dof_global_new;
6397 //mwf debug
6398 //std::cout<<" rank= "<<rank<<" eN= "<<eN<<" local dof= "<<localOffset+eN-nElements_owned<<" nElements_owned= "<<nElements_owned<<" dof_old= "<<dof_global_old<<" new= "<<dof_global_new<<std::endl;
6399 }
6400 //setup local to global mapping on the subdomain for finite elements
6401 const int nDOF_element = 3;
6402 const int ghostNodeOffset = nNodes_owned + nElements_owned;
6403 const int ghostElementOffset = ghostNodeOffset + mesh.subdomainp->nNodes_global-nNodes_owned;
6404
6405 //for lagrange nodes
6406 int nN_global_subdomain[2];
6407 for (int eN=0; eN < mesh.subdomainp->nElements_global; eN++)
6408 {
6409 for (int nN=0; nN < mesh.subdomainp->nNodes_element; nN++)
6410 {
6411 nN_global_subdomain[nN] = mesh.subdomainp->elementNodesArray[eN*mesh.subdomainp->nNodes_element + nN];
6412 if (nN_global_subdomain[nN] < nNodes_owned)
6413 subdomain_l2g[eN*nDOF_element+nN] = nN_global_subdomain[nN];
6414 else
6415 subdomain_l2g[eN*nDOF_element+nN] = nN_global_subdomain[nN]-nNodes_owned + ghostNodeOffset;
6416 //vertex dof
6417 for (int eI=0; eI < 3; eI++)
6418 lagrangeNodesArray[subdomain_l2g[eN*nDOF_element+nN]*3+eI] = mesh.subdomainp->nodeArray[3*nN_global_subdomain[nN]+eI];
6419 }
6420 if (eN < nElements_owned)
6421 subdomain_l2g[eN*nDOF_element+2] = nNodes_owned + eN;
6422 else
6423 subdomain_l2g[eN*nDOF_element+2] = eN - nElements_owned + ghostElementOffset;
6424 //vertex dof
6425 for (int eI=0; eI < 3; eI++)
6426 lagrangeNodesArray[subdomain_l2g[eN*nDOF_element+2]*3+eI] = 0.5*(mesh.subdomainp->nodeArray[3*nN_global_subdomain[0]+eI]+mesh.subdomainp->nodeArray[3*nN_global_subdomain[1]+eI]);
6427 }
6428
6429 ISRestoreIndices(quadraticNumberingIS_global_new2old,&quadraticNumbering_global_new2old);
6430 ISDestroy(&quadraticNumberingIS_new2old);
6431 ISDestroy(&quadraticNumberingIS_global_new2old);
6432
6433 return 0;
6434}
6435
6436int buildQuadraticSubdomain2GlobalMappings_2d(const MPI_Comm& PROTEUS_COMM_WORLD, Mesh& mesh,
6437 const int *elementBoundaryOffsets_subdomain_owned,
6438 const int *nodeOffsets_subdomain_owned,
6439 const int *elementBoundaryNumbering_subdomain2global,
6440 const int *nodeNumbering_subdomain2global,
6441 int& nDOF_all_processes,//total number of dofs in whole domain
6442 int& nDOF_subdomain,//total number of dofs in sub-domain
6443 int& max_dof_neighbors,//maximum number of neighbors for connectivity of dofs
6444 int *offsets_subdomain_owned, //starting point of local dofs on each processor (nProcs+1)
6445 int *subdomain_l2g, //local to global dof mapping on subdomain
6446 int *subdomain2global,//subdomain dof to global (parallel) numbering
6447 double * lagrangeNodesArray)//location of nodes corresponding to dofs
6448{
6449 using namespace std;
6450 int ierr,size,rank;
6451
6452 ierr = MPI_Comm_size(PROTEUS_COMM_WORLD,&size);
6453 ierr = MPI_Comm_rank(PROTEUS_COMM_WORLD,&rank);
6454
6455 //In 2d the quadratic dofs can be associated with nodes and element Boundaries
6456 //assuming have ownership info and consistent local/global mappings for nodes, elementBoundaries
6457 //build a mapping from local quadratic dofs to global quadratic dofs for petsc
6458 //assume a processor owns a dof if it owns that node or element
6459 assert(elementBoundaryOffsets_subdomain_owned);
6460 assert(nodeOffsets_subdomain_owned);
6461 assert(elementBoundaryNumbering_subdomain2global);
6462 assert(nodeNumbering_subdomain2global);
6463 assert(mesh.subdomainp);
6464
6465 const int nNodes_owned = nodeOffsets_subdomain_owned[rank+1]-nodeOffsets_subdomain_owned[rank];
6466 const int nElementBoundaries_owned = elementBoundaryOffsets_subdomain_owned[rank+1]-elementBoundaryOffsets_subdomain_owned[rank];
6467 const int nDOFs_owned = nNodes_owned + nElementBoundaries_owned;
6468 const int nDOFs_global= mesh.nNodes_global + mesh.nElementBoundaries_global;
6469 //start with a logical global ordering of dofs as
6470 //[global nodes, global elementBoundaries]
6471 //want to create global numbering
6472 //[nodes proc 0,elementBoundaries proc 0,nodes proc 1, elementBoundaries proc 1,...]
6473 valarray<int> quadraticNumbering_new2old(nDOFs_owned);
6474 for (int nN = 0; nN < nNodes_owned; nN++)
6475 {
6476 int dof_global = nodeNumbering_subdomain2global[nN];
6477 quadraticNumbering_new2old[nN] = dof_global;
6478 }
6479 for (int ebN = 0; ebN < nElementBoundaries_owned; ebN++)
6480 {
6481 int dof_global = mesh.nNodes_global + elementBoundaryNumbering_subdomain2global[ebN];
6482 quadraticNumbering_new2old[nNodes_owned + ebN] = dof_global;
6483 }
6484
6485 //build an index set for new numbering
6486 IS quadraticNumberingIS_new2old;
6487 ISCreateGeneral(PROTEUS_COMM_WORLD,nDOFs_owned,&quadraticNumbering_new2old[0],PETSC_COPY_VALUES,&quadraticNumberingIS_new2old);
6488 IS quadraticNumberingIS_global_new2old;
6489 ISAllGather(quadraticNumberingIS_new2old,&quadraticNumberingIS_global_new2old);
6490 //get old 2 new mapping for dofs
6491 const PetscInt *quadraticNumbering_global_new2old;
6492 valarray<int> quadraticNumbering_old2new_global(nDOFs_global);
6493 ISGetIndices(quadraticNumberingIS_global_new2old,&quadraticNumbering_global_new2old);
6494 for (int id = 0; id < nDOFs_global; id++)
6495 quadraticNumbering_old2new_global[quadraticNumbering_global_new2old[id]] = id;
6496 //mwf debug
6497 //for (int id = 0; id < nDOFs_global; id++)
6498 // {
6499 // std::cout<<" rank= "<<rank<<" build 2d c0p2 mappings new2old["<<id<<"]= "<<quadraticNumbering_global_new2old[id]
6500 // <<" old2new["<<quadraticNumbering_global_new2old[id]<<"]= "<<quadraticNumbering_old2new_global[quadraticNumbering_global_new2old[id]]
6501 // <<std::endl;
6502 // }
6503 assert(offsets_subdomain_owned);
6504 assert(subdomain2global);
6505 assert(subdomain_l2g);
6506 assert(lagrangeNodesArray);
6507 for (int sdN=0; sdN < size+1; sdN++)
6508 offsets_subdomain_owned[sdN] = nodeOffsets_subdomain_owned[sdN]+elementBoundaryOffsets_subdomain_owned[sdN];
6509 nDOF_all_processes = mesh.nNodes_global+mesh.nElementBoundaries_global;
6510 nDOF_subdomain = mesh.subdomainp->nNodes_global+mesh.subdomainp->nElementBoundaries_global;
6511 max_dof_neighbors = 2*mesh.max_nNodeNeighbors_node;
6512
6513 //loop through owned and ghost dofs build subdomain mapping by
6514 //going from old --> new
6515 int localOffset = 0;
6516 for (int nN = 0; nN < nNodes_owned; nN++)
6517 {
6518 int dof_global_old = nodeNumbering_subdomain2global[nN];
6519 int dof_global_new = quadraticNumbering_old2new_global[dof_global_old];
6520 subdomain2global[localOffset + nN] = dof_global_new;
6521 //mwf debug
6522 //std::cout<<" rank= "<<rank<<" nN= "<<nN<<" local dof= "<<localOffset+nN<<" nNodes_owned= "<<nNodes_owned<<" dof_old= "<<dof_global_old<<" new= "<<dof_global_new<<std::endl;
6523 }
6524 localOffset += nNodes_owned;
6525 for (int ebN = 0; ebN < nElementBoundaries_owned; ebN++)
6526 {
6527 int dof_global_old = mesh.nNodes_global + elementBoundaryNumbering_subdomain2global[ebN];
6528 int dof_global_new = quadraticNumbering_old2new_global[dof_global_old];
6529 subdomain2global[localOffset + ebN] = dof_global_new;
6530 //mwf debug
6531 //std::cout<<" rank= "<<rank<<" ebN= "<<ebN<<" local dof= "<<localOffset+ebN<<" nElementBoundaries_owned= "<<nElementBoundaries_owned
6532 // <<" dof_old= "<<dof_global_old<<" new= "<<dof_global_new<<std::endl;
6533 }
6534 localOffset += nElementBoundaries_owned;
6535 for (int nN = nNodes_owned; nN < mesh.subdomainp->nNodes_global; nN++)
6536 {
6537 int dof_global_old = nodeNumbering_subdomain2global[nN];
6538 int dof_global_new = quadraticNumbering_old2new_global[dof_global_old];
6539 subdomain2global[localOffset + nN -nNodes_owned] = dof_global_new;
6540 //mwf debug
6541 //std::cout<<" rank= "<<rank<<" nN= "<<nN<<" local dof= "<<localOffset+nN-nNodes_owned<<" nNodes_owned= "<<nNodes_owned<<" dof_old= "<<dof_global_old<<" new= "<<dof_global_new<<std::endl;
6542 }
6543 localOffset += mesh.subdomainp->nNodes_global - nNodes_owned;
6544 for (int ebN = nElementBoundaries_owned; ebN < mesh.subdomainp->nElementBoundaries_global; ebN++)
6545 {
6546 int dof_global_old = mesh.nNodes_global + elementBoundaryNumbering_subdomain2global[ebN];
6547 int dof_global_new = quadraticNumbering_old2new_global[dof_global_old];
6548 subdomain2global[localOffset + ebN-nElementBoundaries_owned] = dof_global_new;
6549 //mwf debug
6550 //std::cout<<" rank= "<<rank<<" ebN= "<<ebN<<" local dof= "<<localOffset+ebN-nElementBoundaries_owned<<" nElementBoundaries_owned= "<<nElementBoundaries_owned<<" dof_old= "<<dof_global_old<<" new= "<<dof_global_new<<std::endl;
6551 }
6552 //setup local to global mapping on the subdomain for finite elements
6553 const int nDOF_element = 6;
6554 const int ghostNodeOffset = nNodes_owned + nElementBoundaries_owned;
6555 const int ghostElementBoundaryOffset = ghostNodeOffset + mesh.subdomainp->nNodes_global-nNodes_owned;
6556
6557 //for lagrange nodes
6558 int nN_global_subdomain[3];
6559 for (int eN=0; eN < mesh.subdomainp->nElements_global; eN++)
6560 {
6561 for (int nN=0; nN < mesh.subdomainp->nNodes_element; nN++)
6562 {
6563 nN_global_subdomain[nN] = mesh.subdomainp->elementNodesArray[eN*mesh.subdomainp->nNodes_element + nN];
6564 if (nN_global_subdomain[nN] < nNodes_owned)
6565 subdomain_l2g[eN*nDOF_element+nN] = nN_global_subdomain[nN];
6566 else
6567 subdomain_l2g[eN*nDOF_element+nN] = nN_global_subdomain[nN]-nNodes_owned + ghostNodeOffset;
6568 //vertex dof
6569 for (int eI=0; eI < 3; eI++)
6570 lagrangeNodesArray[subdomain_l2g[eN*nDOF_element+nN]*3+eI] = mesh.subdomainp->nodeArray[3*nN_global_subdomain[nN]+eI];
6571 }
6572 for (int ebN=0; ebN < mesh.subdomainp->nElementBoundaries_element; ebN++)
6573 {
6574 //take into account numbering of edges according to
6575 //vertex they are across from
6576 int ebN_global = mesh.subdomainp->elementBoundariesArray[eN*mesh.subdomainp->nElementBoundaries_element+((ebN+2)%3)];
6577 if (ebN_global < nElementBoundaries_owned)
6578 subdomain_l2g[eN*nDOF_element+3+ebN] = nNodes_owned + ebN_global;
6579 else
6580 subdomain_l2g[eN*nDOF_element+3+ebN] = ebN_global - nElementBoundaries_owned + ghostElementBoundaryOffset;
6581 //center of edge dof
6582 for (int eI=0; eI < 3; eI++)
6583 lagrangeNodesArray[subdomain_l2g[eN*nDOF_element+3+ebN]*3+eI] = 0.5*(mesh.subdomainp->nodeArray[3*nN_global_subdomain[(ebN+0)%3]+eI]+mesh.subdomainp->nodeArray[3*nN_global_subdomain[(ebN+1)%3]+eI]);
6584 }
6585 }//eN
6586
6587 ISRestoreIndices(quadraticNumberingIS_global_new2old,&quadraticNumbering_global_new2old);
6588 ISDestroy(&quadraticNumberingIS_new2old);
6589 ISDestroy(&quadraticNumberingIS_global_new2old);
6590
6591 return 0;
6592}
6593
6594
6595int buildQuadraticSubdomain2GlobalMappings_3d(const MPI_Comm& PROTEUS_COMM_WORLD, Mesh& mesh,
6596 const int *edgeOffsets_subdomain_owned,
6597 const int *nodeOffsets_subdomain_owned,
6598 const int *edgeNumbering_subdomain2global,
6599 const int *nodeNumbering_subdomain2global,
6600 int& nDOF_all_processes,//total number of dofs in whole domain
6601 int& nDOF_subdomain,//total number of dofs in sub-domain
6602 int& max_dof_neighbors,//maximum number of neighbors for connectivity of dofs
6603 int *offsets_subdomain_owned, //starting point of local dofs on each processor (nProcs+1)
6604 int *subdomain_l2g, //local to global dof mapping on subdomain
6605 int *subdomain2global,//subdomain dof to global (parallel) numbering
6606 double * lagrangeNodesArray)//location of nodes corresponding to dofs
6607{
6608 using namespace std;
6609 int ierr,size,rank;
6610
6611 ierr = MPI_Comm_size(PROTEUS_COMM_WORLD,&size);
6612 ierr = MPI_Comm_rank(PROTEUS_COMM_WORLD,&rank);
6613
6614 //In 2d the quadratic dofs can be associated with nodes and element Boundaries
6615 //assuming have ownership info and consistent local/global mappings for nodes, elementBoundaries
6616 //build a mapping from local quadratic dofs to global quadratic dofs for petsc
6617 //assume a processor owns a dof if it owns that node or element
6618 assert(edgeOffsets_subdomain_owned);
6619 assert(nodeOffsets_subdomain_owned);
6620 assert(edgeNumbering_subdomain2global);
6621 assert(nodeNumbering_subdomain2global);
6622 assert(mesh.subdomainp);
6623
6624 const int nNodes_owned = nodeOffsets_subdomain_owned[rank+1]-nodeOffsets_subdomain_owned[rank];
6625 const int nEdges_owned = edgeOffsets_subdomain_owned[rank+1]-edgeOffsets_subdomain_owned[rank];
6626 const int nDOFs_owned = nNodes_owned + nEdges_owned;
6627 const int nDOFs_global= mesh.nNodes_global + mesh.nEdges_global;
6628 //start with a logical global ordering of dofs as
6629 //[global nodes, global edges]
6630 //want to create global numbering
6631 //[nodes proc 0,edges proc 0,nodes proc 1, edges proc 1,...]
6632 valarray<int> quadraticNumbering_new2old(nDOFs_owned);
6633 for (int nN = 0; nN < nNodes_owned; nN++)
6634 {
6635 int dof_global = nodeNumbering_subdomain2global[nN];
6636 quadraticNumbering_new2old[nN] = dof_global;
6637 }
6638 for (int i = 0; i < nEdges_owned; i++)
6639 {
6640 int dof_global = mesh.nNodes_global + edgeNumbering_subdomain2global[i];
6641 quadraticNumbering_new2old[nNodes_owned + i] = dof_global;
6642 }
6643
6644 //build an index set for new numbering
6645 IS quadraticNumberingIS_new2old;
6646 ISCreateGeneral(PROTEUS_COMM_WORLD,nDOFs_owned,&quadraticNumbering_new2old[0],PETSC_COPY_VALUES,&quadraticNumberingIS_new2old);
6647 IS quadraticNumberingIS_global_new2old;
6648 ISAllGather(quadraticNumberingIS_new2old,&quadraticNumberingIS_global_new2old);
6649 //get old 2 new mapping for dofs
6650 const PetscInt *quadraticNumbering_global_new2old;
6651 valarray<int> quadraticNumbering_old2new_global(nDOFs_global);
6652 ISGetIndices(quadraticNumberingIS_global_new2old,&quadraticNumbering_global_new2old);
6653 for (int id = 0; id < nDOFs_global; id++)
6654 quadraticNumbering_old2new_global[quadraticNumbering_global_new2old[id]] = id;
6655 //mwf debug
6656 // if (rank == 0)
6657 // {
6658 // for (int id = 0; id < nDOFs_global; id++)
6659 // {
6660 // std::cout<<" rank= "<<rank<<" build 2d c0p2 mappings new2old["<<id<<"]= "<<quadraticNumbering_global_new2old[id]
6661 // <<" old2new["<<quadraticNumbering_global_new2old[id]<<"]= "<<quadraticNumbering_old2new_global[quadraticNumbering_global_new2old[id]]
6662 // <<std::endl;
6663 // }
6664 // }
6665 assert(offsets_subdomain_owned);
6666 assert(subdomain2global);
6667 assert(subdomain_l2g);
6668 assert(lagrangeNodesArray);
6669 for (int sdN=0; sdN < size+1; sdN++)
6670 offsets_subdomain_owned[sdN] = nodeOffsets_subdomain_owned[sdN]+edgeOffsets_subdomain_owned[sdN];
6671 nDOF_all_processes = mesh.nNodes_global+mesh.nEdges_global;
6672 nDOF_subdomain = mesh.subdomainp->nNodes_global+mesh.subdomainp->nEdges_global;
6673 max_dof_neighbors = 2*mesh.max_nNodeNeighbors_node;
6674
6675 //loop through owned and ghost dofs build subdomain mapping by
6676 //going from old --> new
6677 int localOffset = 0;
6678 for (int nN = 0; nN < nNodes_owned; nN++)
6679 {
6680 int dof_global_old = nodeNumbering_subdomain2global[nN];
6681 int dof_global_new = quadraticNumbering_old2new_global[dof_global_old];
6682 subdomain2global[localOffset + nN] = dof_global_new;
6683 //mwf debug
6684 // if (rank == 0)
6685 // std::cout<<" rank= "<<rank<<" nN= "<<nN<<" local dof= "<<localOffset+nN<<" nNodes_owned= "<<nNodes_owned<<" dof_old= "<<dof_global_old<<" new= "<<dof_global_new<<std::endl;
6686 }
6687 localOffset += nNodes_owned;
6688 for (int i = 0; i < nEdges_owned; i++)
6689 {
6690 int dof_global_old = mesh.nNodes_global + edgeNumbering_subdomain2global[i];
6691 int dof_global_new = quadraticNumbering_old2new_global[dof_global_old];
6692 subdomain2global[localOffset + i] = dof_global_new;
6693 //mwf debug
6694 // if (rank == 0)
6695 // std::cout<<" rank= "<<rank<<" i= "<<i<<" local dof= "<<localOffset+i<<" nEdges_owned= "<<nEdges_owned
6696 // <<" dof_old= "<<dof_global_old<<" new= "<<dof_global_new<<std::endl;
6697 }
6698 localOffset += nEdges_owned;
6699 for (int nN = nNodes_owned; nN < mesh.subdomainp->nNodes_global; nN++)
6700 {
6701 int dof_global_old = nodeNumbering_subdomain2global[nN];
6702 int dof_global_new = quadraticNumbering_old2new_global[dof_global_old];
6703 subdomain2global[localOffset + nN -nNodes_owned] = dof_global_new;
6704 //mwf debug
6705 // if (rank == 0)
6706 // std::cout<<" rank= "<<rank<<" nN= "<<nN<<" local dof= "<<localOffset+nN-nNodes_owned<<" nNodes_owned= "<<nNodes_owned<<" dof_old= "<<dof_global_old<<" new= "<<dof_global_new<<std::endl;
6707 }
6708 localOffset += mesh.subdomainp->nNodes_global - nNodes_owned;
6709 for (int i = nEdges_owned; i < mesh.subdomainp->nEdges_global; i++)
6710 {
6711 int dof_global_old = mesh.nNodes_global + edgeNumbering_subdomain2global[i];
6712 int dof_global_new = quadraticNumbering_old2new_global[dof_global_old];
6713 subdomain2global[localOffset + i-nEdges_owned] = dof_global_new;
6714 // //mwf debug
6715 // if (rank == 0)
6716 // std::cout<<" rank= "<<rank<<" i= "<<i<<" local dof= "<<localOffset+i-nEdges_owned<<" nEdges_owned= "<<nEdges_owned<<" dof_old= "<<dof_global_old<<" new= "<<dof_global_new<<std::endl;
6717 }
6718 //setup local to global mapping on the subdomain for finite elements
6719 const int nDOF_element = 10;
6720 const int ghostNodeOffset = nNodes_owned + nEdges_owned;
6721 const int ghostEdgeOffset = ghostNodeOffset + mesh.subdomainp->nNodes_global-nNodes_owned;
6722 //need mapping from nodes to edge to setup element based relationship
6723 map<NodeTuple<2>, int> nodesEdgeMap_subdomain;
6724 for (int i=0; i < mesh.subdomainp->nEdges_global; i++)
6725 {
6726 int nodes[2];
6727 nodes[0] = mesh.subdomainp->edgeNodesArray[2*i];
6728 nodes[1] = mesh.subdomainp->edgeNodesArray[2*i+1];
6729 NodeTuple<2> et(nodes);
6730 const int nN0 = mesh.subdomainp->edgeNodesArray[2*i];
6731 const int nN1 = mesh.subdomainp->edgeNodesArray[2*i+1];
6732 nodesEdgeMap_subdomain[et] = i;
6733 }
6734 for (int eN=0; eN < mesh.subdomainp->nElements_global; eN++)
6735 {
6736 int local_offset = 0;
6737 for (int nN=0; nN < mesh.subdomainp->nNodes_element; nN++)
6738 {
6739 //node_i --> unique subdomain node 0 <= i <= 3
6740 const int nN_global_subdomain = mesh.subdomainp->elementNodesArray[eN*mesh.subdomainp->nNodes_element + nN];
6741 //assign unique subdomain id based on |owned nodes|owned edges|ghost nodes|ghost edges|
6742 if (nN_global_subdomain < nNodes_owned)
6743 {
6744 subdomain_l2g[eN*nDOF_element + local_offset + nN] = nN_global_subdomain;
6745 }
6746 else
6747 {
6748 subdomain_l2g[eN*nDOF_element + local_offset + nN] = ghostNodeOffset + nN_global_subdomain - nNodes_owned;
6749 }
6750 //unique subdomain id --> unique cross processor id
6751 //subdomain2global[subdomain_l2g[eN*nDOF_element + local_offset + nN]] = nodeNumbering_subdomain2global[nN_global_subdomain];
6752 //mwf debug
6753 // if (rank == 0)
6754 // {
6755 // std::cout<<"build loc2glob c0p2 3d eN= "<<eN<<" node= "<<nN<<" sub_dof= "<< subdomain_l2g[eN*nDOF_element + local_offset + nN]
6756 // <<" glob_dof= "<<subdomain2global[subdomain_l2g[eN*nDOF_element + local_offset + nN]]<<std::endl;
6757 // }
6758 for (int eI=0; eI < 3; eI++)
6759 lagrangeNodesArray[subdomain_l2g[eN*nDOF_element + local_offset +nN]*3+eI] = mesh.subdomainp->nodeArray[nN_global_subdomain*3+eI];
6760 }
6761 local_offset += mesh.subdomainp->nNodes_element;
6762 //(node_i,node_{i+1}) --> unique subdomain edge, 0 <= i < 3
6763 for (int nN = 0; nN < mesh.subdomainp->nNodes_element-1; nN++)
6764 {
6765 const int nN_neig = (nN+1)%mesh.subdomainp->nNodes_element;
6766 const int nN_global_subdomain = mesh.subdomainp->elementNodesArray[eN*mesh.subdomainp->nNodes_element + nN];
6767 const int nN_neig_global_subdomain= mesh.subdomainp->elementNodesArray[eN*mesh.subdomainp->nNodes_element + nN_neig];
6768 //unique edge subdomain id and global id
6769 int edge_subdomain = -1,edge_global=-1;
6770 //see if edge is (nN,nN_neig) or vice versa
6771 int nodes[2];
6772 nodes[0] = nN_global_subdomain;
6773 nodes[1] = nN_neig_global_subdomain;
6774 NodeTuple<2> et(nodes);
6775 edge_subdomain = nodesEdgeMap_subdomain[et];
6776 edge_global = edgeNumbering_subdomain2global[edge_subdomain];
6777 assert(edge_subdomain >= 0 && edge_global >= 0);
6778
6779 //assign unique subdomain id based on |owned nodes|owned edges|ghost nodes|ghost edges|
6780 if (edge_subdomain < nEdges_owned)
6781 subdomain_l2g[eN*nDOF_element + local_offset + nN] = nNodes_owned + edge_subdomain;
6782 else
6783 subdomain_l2g[eN*nDOF_element + local_offset + nN] = ghostEdgeOffset + edge_subdomain - nEdges_owned;
6784 //unique subdomain id --> unique cross processor id
6785 //set above could do here subdomain2global[subdomain_l2g[eN*nDOF_element + local_offset + nN]] = mesh.nNodes_global + edge_global;
6786 //mwf debug
6787 // if (rank == 0)
6788 // {
6789 // std::cout<<"build loc2glob c0p2 3d eN= "<<eN<<" edge("<<nN<<","<<nN_neig<<") sub_dof= "<< subdomain_l2g[eN*nDOF_element + local_offset + nN]
6790 // <<" glob_dof= "<<subdomain2global[subdomain_l2g[eN*nDOF_element + local_offset + nN]]<<std::endl;
6791 // }
6792 for (int eI=0; eI < 3; eI++)
6793 lagrangeNodesArray[subdomain_l2g[eN*nDOF_element+local_offset+nN]*3+eI] = 0.5*(mesh.subdomainp->nodeArray[nN_global_subdomain*3+eI]+
6794 mesh.subdomainp->nodeArray[nN_neig_global_subdomain*3+eI]);
6795 }
6796 local_offset += mesh.subdomainp->nNodes_element-1;
6797 //(node_i,node_{i+2}) --> unique subdomain edge, 0 <= i < 2
6798 for (int nN = 0; nN < mesh.subdomainp->nNodes_element-2; nN++)
6799 {
6800 const int nN_neig = (nN+2)%mesh.subdomainp->nNodes_element;
6801 const int nN_global_subdomain = mesh.subdomainp->elementNodesArray[eN*mesh.subdomainp->nNodes_element + nN];
6802 const int nN_neig_global_subdomain= mesh.subdomainp->elementNodesArray[eN*mesh.subdomainp->nNodes_element + nN_neig];
6803 //unique edge subdomain id and global id
6804 int edge_subdomain = -1, edge_global = -1;
6805 //see if edge is (nN,nN_neig) or vice versa
6806 int nodes[2];
6807 nodes[0] = nN_global_subdomain;
6808 nodes[1] = nN_neig_global_subdomain;
6809 NodeTuple<2> et(nodes);
6810 edge_subdomain = nodesEdgeMap_subdomain[et];
6811 edge_global = edgeNumbering_subdomain2global[edge_subdomain];
6812 assert(edge_subdomain >= 0 && edge_global >= 0);
6813 //assign unique subdomain id based on |owned nodes|owned edges|ghost nodes|ghost edges|
6814 if (edge_subdomain < nEdges_owned)
6815 subdomain_l2g[eN*nDOF_element + local_offset + nN] = nNodes_owned + edge_subdomain;
6816 else
6817 subdomain_l2g[eN*nDOF_element + local_offset + nN] = ghostEdgeOffset + edge_subdomain - nEdges_owned;
6818 //unique subdomain id --> unique cross processor id
6819 //subdomain2global[subdomain_l2g[eN*nDOF_element + local_offset + nN]] = mesh.nNodes_global + edge_global;
6820 //mwf debug
6821 // if (rank == 0)
6822 // {
6823 // std::cout<<"build loc2glob c0p2 3d eN= "<<eN<<" edge("<<nN<<","<<nN_neig<<") sub_dof= "<< subdomain_l2g[eN*nDOF_element + local_offset + nN]
6824 // <<" glob_dof= "<<subdomain2global[subdomain_l2g[eN*nDOF_element + local_offset + nN]]<<std::endl;
6825 // }
6826 for (int eI=0; eI < 3; eI++)
6827 lagrangeNodesArray[subdomain_l2g[eN*nDOF_element+local_offset+nN]*3+eI] = 0.5*(mesh.subdomainp->nodeArray[nN_global_subdomain*3+eI]+
6828 mesh.subdomainp->nodeArray[nN_neig_global_subdomain*3+eI]);
6829 }
6830 local_offset += mesh.subdomainp->nNodes_element-2;
6831 //(node_i,node_{i+3}) --> unique subdomain edge, 0 = i
6832 for (int nN = 0; nN < mesh.subdomainp->nNodes_element-3; nN++)
6833 {
6834 const int nN_neig = (nN+3)%mesh.subdomainp->nNodes_element;
6835 const int nN_global_subdomain = mesh.subdomainp->elementNodesArray[eN*mesh.subdomainp->nNodes_element + nN];
6836 const int nN_neig_global_subdomain= mesh.subdomainp->elementNodesArray[eN*mesh.subdomainp->nNodes_element + nN_neig];
6837 //unique edge subdomain id and global id
6838 int edge_subdomain = -1, edge_global = -1;
6839 //see if edge is (nN,nN_neig) or vice versa
6840 int nodes[2];
6841 nodes[0] = nN_global_subdomain;
6842 nodes[1] = nN_neig_global_subdomain;
6843 NodeTuple<2> et(nodes);
6844 edge_subdomain = nodesEdgeMap_subdomain[et];
6845 edge_global = edgeNumbering_subdomain2global[edge_subdomain];
6846 assert(edge_subdomain >= 0 && edge_global >= 0);
6847 //assign unique subdomain id based on |owned nodes|owned edges|ghost nodes|ghost edges|
6848 if (edge_subdomain < nEdges_owned)
6849 subdomain_l2g[eN*nDOF_element + local_offset + nN] = nNodes_owned + edge_subdomain;
6850 else
6851 subdomain_l2g[eN*nDOF_element + local_offset + nN] = ghostEdgeOffset + edge_subdomain - nEdges_owned;
6852 //unique subdomain id --> unique cross processor id
6853 //subdomain2global[subdomain_l2g[eN*nDOF_element + local_offset + nN]] = mesh.nNodes_global + edge_global;
6854 //mwf debug
6855 // if (rank == 0)
6856 // {
6857 // std::cout<<"build loc2glob c0p2 3d eN= "<<eN<<" edge("<<nN<<","<<nN_neig<<") sub_dof= "<< subdomain_l2g[eN*nDOF_element + local_offset + nN]
6858 // <<" glob_dof= "<<subdomain2global[subdomain_l2g[eN*nDOF_element + local_offset + nN]]<<std::endl;
6859 // }
6860 for (int eI=0; eI < 3; eI++)
6861 lagrangeNodesArray[subdomain_l2g[eN*nDOF_element+local_offset+nN]*3+eI] = 0.5*(mesh.subdomainp->nodeArray[nN_global_subdomain*3+eI]+
6862 mesh.subdomainp->nodeArray[nN_neig_global_subdomain*3+eI]);
6863 }
6864
6865 }//eN
6866
6867 ISRestoreIndices(quadraticNumberingIS_global_new2old,&quadraticNumbering_global_new2old);
6868 ISDestroy(&quadraticNumberingIS_new2old);
6869 ISDestroy(&quadraticNumberingIS_global_new2old);
6870
6871 return 0;
6872}
6873
6874int buildQuadraticCubeSubdomain2GlobalMappings_3d(const MPI_Comm& PROTEUS_COMM_WORLD, Mesh& mesh,
6875 const int *edgeOffsets_subdomain_owned,
6876 const int *nodeOffsets_subdomain_owned,
6877 const int *edgeNumbering_subdomain2global,
6878 const int *nodeNumbering_subdomain2global,
6879 int& nDOF_all_processes,//total number of dofs in whole domain
6880 int& nDOF_subdomain,//total number of dofs in sub-domain
6881 int& max_dof_neighbors,//maximum number of neighbors for connectivity of dofs
6882 int *offsets_subdomain_owned, //starting point of local dofs on each processor (nProcs+1)
6883 int *subdomain_l2g, //local to global dof mapping on subdomain
6884 int *subdomain2global,//subdomain dof to global (parallel) numbering
6885 double * lagrangeNodesArray)//location of nodes corresponding to dofs
6886{
6887 using namespace std;
6888 int ierr,size,rank;
6889
6890 ierr = MPI_Comm_size(PROTEUS_COMM_WORLD,&size);
6891 ierr = MPI_Comm_rank(PROTEUS_COMM_WORLD,&rank);
6892
6893 //In 2d the quadratic dofs can be associated with nodes and element Boundaries
6894 //assuming have ownership info and consistent local/global mappings for nodes, elementBoundaries
6895 //build a mapping from local quadratic dofs to global quadratic dofs for petsc
6896 //assume a processor owns a dof if it owns that node or element
6897 assert(edgeOffsets_subdomain_owned);
6898 assert(nodeOffsets_subdomain_owned);
6899 assert(edgeNumbering_subdomain2global);
6900 assert(nodeNumbering_subdomain2global);
6901 assert(mesh.subdomainp);
6902
6903 const int *elementBoundaryOffsets_subdomain_owned=mesh.elementBoundaryOffsets_subdomain_owned;
6904 const int *elementOffsets_subdomain_owned=mesh.elementOffsets_subdomain_owned;
6905 const int *elementBoundaryNumbering_subdomain2global=mesh.elementBoundaryNumbering_subdomain2global;
6906 const int *elementNumbering_subdomain2global=mesh.elementNumbering_subdomain2global;
6907
6908 const int nNodes_owned = nodeOffsets_subdomain_owned[rank+1]-nodeOffsets_subdomain_owned[rank];
6909 const int nEdges_owned = edgeOffsets_subdomain_owned[rank+1]-edgeOffsets_subdomain_owned[rank];
6910 const int nBoundaries_owned = elementBoundaryOffsets_subdomain_owned[rank+1]-elementBoundaryOffsets_subdomain_owned[rank];
6911 const int nElements_owned = elementOffsets_subdomain_owned[rank+1]-elementOffsets_subdomain_owned[rank];
6912
6913 const int nDOFs_owned = nNodes_owned + nEdges_owned + nBoundaries_owned + nElements_owned;
6914 const int nDOFs_global= mesh.nNodes_global + mesh.nEdges_global + mesh.nElementBoundaries_global + mesh.nElements_global;
6915 //start with a logical global ordering of dofs as
6916 //[global nodes, global edges]
6917 //want to create global numbering
6918 //[nodes proc 0,edges proc 0,nodes proc 1, edges proc 1,...]
6919 valarray<int> quadraticNumbering_new2old(nDOFs_owned);
6920 for (int nN = 0; nN < nNodes_owned; nN++)
6921 {
6922 int dof_global = nodeNumbering_subdomain2global[nN];
6923 quadraticNumbering_new2old[nN] = dof_global;
6924 }
6925 for (int i = 0; i < nEdges_owned; i++)
6926 {
6927 int dof_global = mesh.nNodes_global + edgeNumbering_subdomain2global[i];
6928 quadraticNumbering_new2old[nNodes_owned + i] = dof_global;
6929 }
6930
6931 //-------------------
6932 for (int i = 0; i < nBoundaries_owned; i++)
6933 {
6934 int dof_global = mesh.nNodes_global + mesh.nEdges_global + elementBoundaryNumbering_subdomain2global[i];
6935 quadraticNumbering_new2old[nNodes_owned + nEdges_owned + i] = dof_global;
6936 }
6937 for (int i = 0; i < nElements_owned; i++)
6938 {
6939 int dof_global = mesh.nNodes_global + mesh.nEdges_global + mesh.nElementBoundaries_global + elementNumbering_subdomain2global[i];
6940 quadraticNumbering_new2old[nNodes_owned + nEdges_owned + nBoundaries_owned + i] = dof_global;
6941 }
6942 //-------------------
6943
6944
6945 //build an index set for new numbering
6946 IS quadraticNumberingIS_new2old;
6947 ISCreateGeneral(PROTEUS_COMM_WORLD,nDOFs_owned,&quadraticNumbering_new2old[0],PETSC_COPY_VALUES,&quadraticNumberingIS_new2old);
6948 IS quadraticNumberingIS_global_new2old;
6949 ISAllGather(quadraticNumberingIS_new2old,&quadraticNumberingIS_global_new2old);
6950 //get old 2 new mapping for dofs
6951 const PetscInt *quadraticNumbering_global_new2old;
6952 valarray<int> quadraticNumbering_old2new_global(nDOFs_global);
6953 ISGetIndices(quadraticNumberingIS_global_new2old,&quadraticNumbering_global_new2old);
6954 for (int id = 0; id < nDOFs_global; id++)
6955 quadraticNumbering_old2new_global[quadraticNumbering_global_new2old[id]] = id;
6956 //mwf debug
6957 // if (rank == 0)
6958 // {
6959 // for (int id = 0; id < nDOFs_global; id++)
6960 // {
6961 // std::cout<<" rank= "<<rank<<" build 2d c0p2 mappings new2old["<<id<<"]= "<<quadraticNumbering_global_new2old[id]
6962 // <<" old2new["<<quadraticNumbering_global_new2old[id]<<"]= "<<quadraticNumbering_old2new_global[quadraticNumbering_global_new2old[id]]
6963 // <<std::endl;
6964 // }
6965 // }
6966 assert(offsets_subdomain_owned);
6967 assert(subdomain2global);
6968 assert(subdomain_l2g);
6969 assert(lagrangeNodesArray);
6970 for (int sdN=0; sdN < size+1; sdN++)
6971 offsets_subdomain_owned[sdN] = nodeOffsets_subdomain_owned[sdN]+edgeOffsets_subdomain_owned[sdN]+elementBoundaryOffsets_subdomain_owned[sdN]+elementOffsets_subdomain_owned[sdN];
6972 nDOF_all_processes = mesh.nNodes_global+mesh.nEdges_global+ mesh.nElementBoundaries_global+mesh.nElements_global;
6974 max_dof_neighbors = 2*mesh.max_nNodeNeighbors_node;//8*27?
6975
6976 //loop through owned and ghost dofs build subdomain mapping by
6977 //going from old --> new
6978 int localOffset = 0;
6979 for (int nN = 0; nN < nNodes_owned; nN++)
6980 {
6981 int dof_global_old = nodeNumbering_subdomain2global[nN];
6982 int dof_global_new = quadraticNumbering_old2new_global[dof_global_old];
6983 subdomain2global[localOffset + nN] = dof_global_new;
6984 //mwf debug
6985 // if (rank == 0)
6986 // std::cout<<" rank= "<<rank<<" nN= "<<nN<<" local dof= "<<localOffset+nN<<" nNodes_owned= "<<nNodes_owned<<" dof_old= "<<dof_global_old<<" new= "<<dof_global_new<<std::endl;
6987 }
6988
6989 localOffset += nNodes_owned;
6990 for (int i = 0; i < nEdges_owned; i++)
6991 {
6992 int dof_global_old = mesh.nNodes_global + edgeNumbering_subdomain2global[i];
6993 int dof_global_new = quadraticNumbering_old2new_global[dof_global_old];
6994 subdomain2global[localOffset + i] = dof_global_new;
6995 //mwf debug
6996 // if (rank == 0)
6997 // std::cout<<" rank= "<<rank<<" i= "<<i<<" local dof= "<<localOffset+i<<" nEdges_owned= "<<nEdges_owned
6998 // <<" dof_old= "<<dof_global_old<<" new= "<<dof_global_new<<std::endl;
6999 }
7000
7001
7002 localOffset += nEdges_owned;
7003 for (int i = 0; i < nBoundaries_owned; i++)
7004 {
7005 int dof_global_old = mesh.nNodes_global + mesh.nEdges_global + elementBoundaryNumbering_subdomain2global[i];
7006 int dof_global_new = quadraticNumbering_old2new_global[dof_global_old];
7007 subdomain2global[localOffset + i] = dof_global_new;
7008 //mwf debug
7009 // if (rank == 0)
7010 // std::cout<<" rank= "<<rank<<" i= "<<i<<" local dof= "<<localOffset+i<<" nEdges_owned= "<<nEdges_owned
7011 // <<" dof_old= "<<dof_global_old<<" new= "<<dof_global_new<<std::endl;
7012 }
7013
7014 localOffset += nBoundaries_owned;
7015 for (int i = 0; i < nElements_owned; i++)
7016 {
7017 int dof_global_old = mesh.nNodes_global + mesh.nEdges_global + mesh.nElementBoundaries_global + elementNumbering_subdomain2global[i];
7018 int dof_global_new = quadraticNumbering_old2new_global[dof_global_old];
7019 subdomain2global[localOffset + i] = dof_global_new;
7020 //mwf debug
7021 // if (rank == 0)
7022 // std::cout<<" rank= "<<rank<<" i= "<<i<<" local dof= "<<localOffset+i<<" nEdges_owned= "<<nEdges_owned
7023 // <<" dof_old= "<<dof_global_old<<" new= "<<dof_global_new<<std::endl;
7024 }
7025
7026 localOffset += nElements_owned;
7027 for (int nN = nNodes_owned; nN < mesh.subdomainp->nNodes_global; nN++)
7028 {
7029 int dof_global_old = nodeNumbering_subdomain2global[nN];
7030 int dof_global_new = quadraticNumbering_old2new_global[dof_global_old];
7031 subdomain2global[localOffset + nN - nNodes_owned] = dof_global_new;
7032 //mwf debug
7033 // if (rank == 0)
7034 // std::cout<<" rank= "<<rank<<" nN= "<<nN<<" local dof= "<<localOffset+nN-nNodes_owned<<" nNodes_owned= "<<nNodes_owned<<" dof_old= "<<dof_global_old<<" new= "<<dof_global_new<<std::endl;
7035 }
7036
7037 localOffset += mesh.subdomainp->nNodes_global - nNodes_owned;
7038 for (int i = nEdges_owned; i < mesh.subdomainp->nEdges_global; i++)
7039 {
7040 int dof_global_old = mesh.nNodes_global + edgeNumbering_subdomain2global[i];
7041 int dof_global_new = quadraticNumbering_old2new_global[dof_global_old];
7042 subdomain2global[localOffset + i - nEdges_owned] = dof_global_new;
7043 // //mwf debug
7044 // if (rank == 0)
7045 // std::cout<<" rank= "<<rank<<" i= "<<i<<" local dof= "<<localOffset+i-nEdges_owned<<" nEdges_owned= "<<nEdges_owned<<" dof_old= "<<dof_global_old<<" new= "<<dof_global_new<<std::endl;
7046 }
7047
7048 localOffset += mesh.subdomainp->nEdges_global - nEdges_owned;
7049 for (int i = nBoundaries_owned; i < mesh.subdomainp->nElementBoundaries_global; i++)
7050 {
7051 int dof_global_old = mesh.nNodes_global + mesh.nEdges_global + elementBoundaryNumbering_subdomain2global[i];
7052 int dof_global_new = quadraticNumbering_old2new_global[dof_global_old];
7053 subdomain2global[localOffset + i - nBoundaries_owned] = dof_global_new;
7054 // //mwf debug
7055 // if (rank == 0)
7056 // std::cout<<" rank= "<<rank<<" i= "<<i<<" local dof= "<<localOffset+i-nEdges_owned<<" nEdges_owned= "<<nEdges_owned<<" dof_old= "<<dof_global_old<<" new= "<<dof_global_new<<std::endl;
7057 }
7058
7059 localOffset += mesh.subdomainp->nElementBoundaries_global - nBoundaries_owned;
7060 for (int i = nElements_owned; i < mesh.subdomainp->nElements_global; i++)
7061 {
7062 int dof_global_old = mesh.nNodes_global + mesh.nEdges_global + mesh.nElementBoundaries_global + elementNumbering_subdomain2global[i];
7063 int dof_global_new = quadraticNumbering_old2new_global[dof_global_old];
7064 subdomain2global[localOffset + i - nElements_owned] = dof_global_new;
7065 // //mwf debug
7066 // if (rank == 0)
7067 // std::cout<<" rank= "<<rank<<" i= "<<i<<" local dof= "<<localOffset+i-nEdges_owned<<" nEdges_owned= "<<nEdges_owned<<" dof_old= "<<dof_global_old<<" new= "<<dof_global_new<<std::endl;
7068 }
7069
7070 //setup local to global mapping on the subdomain for finite elements
7071 const int nDOF_element = 27;
7072 const int ghostNodeOffset = nNodes_owned + nEdges_owned + nBoundaries_owned + nElements_owned;
7073 const int ghostEdgeOffset = ghostNodeOffset + mesh.subdomainp->nNodes_global-nNodes_owned;
7074 const int ghostBoundaryOffset = ghostEdgeOffset + mesh.subdomainp->nEdges_global-nEdges_owned;
7075 const int ghostElementOffset = ghostBoundaryOffset + mesh.subdomainp->nElementBoundaries_global-nBoundaries_owned;
7076
7077 int ledge[12][2] = {{0,1},{1,2},{2,3},{3,0},
7078 {0,4},{1,5},{2,6},{3,7},
7079 {4,5},{5,6},{6,7},{7,4}};
7080
7081 int nEdges_element = 12;
7082
7083 int lface[6][4] = {{0,1,2,3},
7084 {0,1,5,4},
7085 {1,2,6,5},
7086 {2,3,7,6},
7087 {3,0,4,7},
7088 {4,5,6,7}};
7089
7090 assert(mesh.subdomainp->nElementBoundaries_element == 6);
7091 //need mapping from nodes to edge to setup element based relationship
7092 map<NodeTuple<2>, int> nodesEdgeMap_subdomain;
7093 for (int i=0; i < mesh.subdomainp->nEdges_global; i++)
7094 {
7095 int nodes[2];
7096 nodes[0] = mesh.subdomainp->edgeNodesArray[2*i+0];
7097 nodes[1] = mesh.subdomainp->edgeNodesArray[2*i+1];
7098 NodeTuple<2> nt(nodes);
7099 nodesEdgeMap_subdomain[nt] = i;
7100 }
7101
7102 map<NodeTuple<4>, int> nodesBoundaryMap_subdomain;
7103 for (int i=0; i < mesh.subdomainp->nElementBoundaries_global; i++)
7104 {
7105
7106 int nodes[4];
7107 nodes[0] = mesh.subdomainp->elementBoundaryNodesArray[i*4+0];
7108 nodes[1] = mesh.subdomainp->elementBoundaryNodesArray[i*4+1];
7109 nodes[2] = mesh.subdomainp->elementBoundaryNodesArray[i*4+2];
7110 nodes[3] = mesh.subdomainp->elementBoundaryNodesArray[i*4+3];
7111 NodeTuple<4> ebt(nodes);
7112 assert(nodesBoundaryMap_subdomain.find(ebt) == nodesBoundaryMap_subdomain.end());
7113 nodesBoundaryMap_subdomain[ebt] = i;
7114 }
7115
7116 for (int eN=0; eN < mesh.subdomainp->nElements_global; eN++)
7117 {
7118 int local_offset = 0;
7119 for (int nN=0; nN < mesh.subdomainp->nNodes_element; nN++)
7120 {
7121 //node_i --> unique subdomain node 0 <= i <= 3
7122 const int nN_global_subdomain = mesh.subdomainp->elementNodesArray[eN*mesh.subdomainp->nNodes_element + nN];
7123 //assign unique subdomain id based on |owned nodes|owned edges|ghost nodes|ghost edges|
7124 if (nN_global_subdomain < nNodes_owned)
7125 {
7126 subdomain_l2g[eN*nDOF_element + local_offset + nN] = nN_global_subdomain;
7127 }
7128 else
7129 {
7130 subdomain_l2g[eN*nDOF_element + local_offset + nN] = ghostNodeOffset + nN_global_subdomain - nNodes_owned;
7131 }
7132 //unique subdomain id --> unique cross processor id
7133 //subdomain2global[subdomain_l2g[eN*nDOF_element + local_offset + nN]] = nodeNumbering_subdomain2global[nN_global_subdomain];
7134 //mwf debug
7135 // if (rank == 0)
7136 // {
7137 // std::cout<<"build loc2glob c0p2 3d eN= "<<eN<<" node= "<<nN<<" sub_dof= "<< subdomain_l2g[eN*nDOF_element + local_offset + nN]
7138 // <<" glob_dof= "<<subdomain2global[subdomain_l2g[eN*nDOF_element + local_offset + nN]]<<std::endl;
7139 // }
7140 for (int eI=0; eI < 3; eI++)
7141 lagrangeNodesArray[subdomain_l2g[eN*nDOF_element + local_offset +nN]*3+eI] = mesh.subdomainp->nodeArray[nN_global_subdomain*3+eI];
7142 }
7143 local_offset += mesh.subdomainp->nNodes_element;
7144 for (int nN = 0; nN < nEdges_element; nN++)
7145 {
7146 int nodes[2];
7147 nodes[0] = mesh.subdomainp->elementNodesArray[eN*mesh.subdomainp->nNodes_element + ledge[nN][0]];
7148 nodes[1] = mesh.subdomainp->elementNodesArray[eN*mesh.subdomainp->nNodes_element + ledge[nN][1]];
7149 NodeTuple<2> nt(nodes);
7150 int edge_subdomain = nodesEdgeMap_subdomain[nt];
7151 int edge_global = edgeNumbering_subdomain2global[edge_subdomain];
7152 assert(edge_subdomain >= 0 && edge_global >= 0);
7153 //assign unique subdomain id based on |owned nodes|owned edges|ghost nodes|ghost edges|
7154 if (edge_subdomain < nEdges_owned)
7155 subdomain_l2g[eN*nDOF_element + local_offset + nN] = nNodes_owned + edge_subdomain;
7156 else
7157 subdomain_l2g[eN*nDOF_element + local_offset + nN] = ghostEdgeOffset + edge_subdomain - nEdges_owned;
7158 //unique subdomain id --> unique cross processor id
7159 //set above could do here subdomain2global[subdomain_l2g[eN*nDOF_element + local_offset + nN]] = mesh.nNodes_global + edge_global;
7160 //mwf debug
7161 // if (rank == 0)
7162 // {
7163 // std::cout<<"build loc2glob c0p2 3d eN= "<<eN<<" edge("<<nN<<","<<nN_neig<<") sub_dof= "<< subdomain_l2g[eN*nDOF_element + local_offset + nN]
7164 // <<" glob_dof= "<<subdomain2global[subdomain_l2g[eN*nDOF_element + local_offset + nN]]<<std::endl;
7165 // }
7166 for (int eI=0; eI < 3; eI++)
7167 lagrangeNodesArray[subdomain_l2g[eN*nDOF_element+local_offset+nN]*3+eI] = 0.5*(mesh.subdomainp->nodeArray[nodes[0]*3+eI]+
7168 mesh.subdomainp->nodeArray[nodes[1]*3+eI]);
7169 }
7170
7171 local_offset += nEdges_element;
7172 for (int nN = 0; nN < mesh.subdomainp->nElementBoundaries_element; nN++)
7173 {
7174
7175 int nodes[4];
7176 nodes[0] = mesh.subdomainp->elementNodesArray[eN*mesh.subdomainp->nNodes_element+lface[nN][0]];
7177 nodes[1] = mesh.subdomainp->elementNodesArray[eN*mesh.subdomainp->nNodes_element+lface[nN][1]];
7178 nodes[2] = mesh.subdomainp->elementNodesArray[eN*mesh.subdomainp->nNodes_element+lface[nN][2]];
7179 nodes[3] = mesh.subdomainp->elementNodesArray[eN*mesh.subdomainp->nNodes_element+lface[nN][3]];
7180 NodeTuple<4> ebt(nodes);
7181
7182
7183 int boundary_subdomain = nodesBoundaryMap_subdomain[ebt];
7184 int boundary_global = elementBoundaryNumbering_subdomain2global[boundary_subdomain];
7185
7186 assert(boundary_subdomain >= 0 && boundary_global >= 0);
7187
7188 //assign unique subdomain id based on |owned nodes|owned edges|ghost nodes|ghost edges|
7189 if (boundary_subdomain < nBoundaries_owned)
7190 subdomain_l2g[eN*nDOF_element + local_offset + nN] = nNodes_owned + nEdges_owned + boundary_subdomain;
7191 else
7192 subdomain_l2g[eN*nDOF_element + local_offset + nN] = ghostBoundaryOffset + boundary_subdomain - nBoundaries_owned;
7193 //unique subdomain id --> unique cross processor id
7194 //set above could do here subdomain2global[subdomain_l2g[eN*nDOF_element + local_offset + nN]] = mesh.nNodes_global + edge_global;
7195 //mwf debug
7196 // if (rank == 0)
7197 // {
7198 // std::cout<<"build loc2glob c0p2 3d eN= "<<eN<<" edge("<<nN<<","<<nN_neig<<") sub_dof= "<< subdomain_l2g[eN*nDOF_element + local_offset + nN]
7199 // <<" glob_dof= "<<subdomain2global[subdomain_l2g[eN*nDOF_element + local_offset + nN]]<<std::endl;
7200 // }
7201 for (int eI=0; eI < 3; eI++)
7202 lagrangeNodesArray[subdomain_l2g[eN*nDOF_element+local_offset+nN]*3+eI] = 0.25*(mesh.subdomainp->nodeArray[nodes[0]*3+eI]+
7203 mesh.subdomainp->nodeArray[nodes[1]*3+eI]+
7204 mesh.subdomainp->nodeArray[nodes[2]*3+eI]+
7205 mesh.subdomainp->nodeArray[nodes[3]*3+eI]);
7206 }
7207
7208 local_offset += mesh.subdomainp->nElementBoundaries_element;
7209
7210 // Interior node!!!
7211
7212 if (eN < nElements_owned)
7213 subdomain_l2g[eN*nDOF_element + local_offset] = nNodes_owned + nEdges_owned + nBoundaries_owned + eN;
7214 else
7215 subdomain_l2g[eN*nDOF_element + local_offset] = ghostElementOffset + eN - nElements_owned;
7216 //unique subdomain id --> unique cross processor id
7217 //set above could do here subdomain2global[subdomain_l2g[eN*nDOF_element + local_offset + nN]] = mesh.nNodes_global + edge_global;
7218 //mwf debug
7219 // if (rank == 0)
7220 // {
7221 // std::cout<<"build loc2glob c0p2 3d eN= "<<eN<<" edge("<<nN<<","<<nN_neig<<") sub_dof= "<< subdomain_l2g[eN*nDOF_element + local_offset + nN]
7222 // <<" glob_dof= "<<subdomain2global[subdomain_l2g[eN*nDOF_element + local_offset + nN]]<<std::endl;
7223 // }
7224 for (int eI=0; eI < 3; eI++)
7225 lagrangeNodesArray[subdomain_l2g[eN*nDOF_element+local_offset]*3+eI] = 0.125*(mesh.subdomainp->nodeArray[mesh.subdomainp->elementNodesArray[eN*mesh.subdomainp->nNodes_element + 0]*3+eI]+
7233
7234
7235 }//eN
7236
7237 ISRestoreIndices(quadraticNumberingIS_global_new2old,&quadraticNumbering_global_new2old);
7238 ISDestroy(&quadraticNumberingIS_new2old);
7239 ISDestroy(&quadraticNumberingIS_global_new2old);
7240
7241 return 0;
7242}
7243
7244
7245
7246
7247
7248int buildDiscontinuousGalerkinSubdomain2GlobalMappings(const MPI_Comm& PROTEUS_COMM_WORLD, Mesh& mesh,
7249 const int *elementOffsets_subdomain_owned,
7250 const int *elementNumbering_subdomain2global,
7251 int nDOF_element,
7252 int& nDOF_all_processes,//total number of dofs in whole domain
7253 int& nDOF_subdomain,//total number of dofs in sub-domain
7254 int& max_dof_neighbors,//maximum number of neighbors for connectivity of dofs
7255 int *offsets_subdomain_owned, //starting point of local dofs on each processor (nProcs+1)
7256 int * subdomain_l2g, //local to global dof mapping on subdomain
7257 int* subdomain2global)//subdomain dof to global (parallel) numbering
7258
7259{
7260 using namespace std;
7261 int ierr,size,rank;
7262 ierr = MPI_Comm_size(PROTEUS_COMM_WORLD,&size);
7263 ierr = MPI_Comm_rank(PROTEUS_COMM_WORLD,&rank);
7264
7265 //DG dofs stored element wise
7266 //[...,eN_0,eN_1,..,eN_ndof_local,...]
7267 //assume a processor owns a dof if it owns that element
7268 assert(elementOffsets_subdomain_owned);
7269 assert(elementNumbering_subdomain2global);
7270 assert(mesh.subdomainp);
7271
7272 const int nElements_owned = elementOffsets_subdomain_owned[rank+1]-elementOffsets_subdomain_owned[rank];
7273
7274 assert(offsets_subdomain_owned);
7275 assert(subdomain2global);
7276 assert(subdomain_l2g);
7277 nDOF_all_processes = mesh.nElements_global*nDOF_element;
7278 nDOF_subdomain = mesh.subdomainp->nElements_global*nDOF_element;
7279 max_dof_neighbors = mesh.nElementBoundaries_element*nDOF_element;
7280
7281 for (int sdN=0; sdN < size+1; sdN++)
7282 offsets_subdomain_owned[sdN] = elementOffsets_subdomain_owned[sdN]*nDOF_element;
7283
7284 //loop through owned and ghost dofs build subdomain mapping
7285 for (int eN = 0; eN < mesh.subdomainp->nElements_global; eN++)
7286 {
7287 for (int i = 0; i < nDOF_element; i++)
7288 {
7289 subdomain2global[eN*nDOF_element + i] = elementNumbering_subdomain2global[eN]*nDOF_element + i;
7290 //mwf debug
7291 //std::cout<<" rank= "<<rank<<" eN= "<<eN<<" local dof= "<<localOffset+eN<<" nElements_owned= "<<nElements_owned<<" dof_old= "<<dof_global_old<<" new= "<<dof_global_new<<std::endl;
7292 subdomain_l2g[eN*nDOF_element+i] = eN*nDOF_element + i;
7293 }
7294 }
7295
7296 return 0;
7297}
7298}
float * vector(long nl, long nh)
void init(void)
Double * z
Definition Headers.h:49
int nodes[nNodes]
Definition mesh.h:440
#define max(a, b)
int constructElementBoundaryElementsArray_edge(Mesh &mesh)
Definition mesh.cpp:622
int allocateGeometricInfo_hexahedron(Mesh &mesh)
Definition mesh.cpp:3651
int computeGeometricInfo_NURBS(Mesh &mesh)
Definition mesh.cpp:3749
int constructElementBoundaryElementsArrayWithGivenElementBoundaryAndEdgeNumbers_hexahedron(Mesh &mesh)
Definition mesh.cpp:3305
int allocateGeometricInfo_triangle(Mesh &mesh)
Definition mesh.cpp:3756
int constructElementBoundaryElementsArrayWithGivenElementBoundaryAndEdgeNumbers_edge(Mesh &mesh)
Definition mesh.cpp:2632
int allocateGeometricInfo_quadrilateral(Mesh &mesh)
Definition mesh.cpp:3856
int constructElementBoundaryElementsArray_tetrahedron(Mesh &mesh)
Definition mesh.cpp:1118
int computeGeometricInfo_edge(Mesh &mesh)
Definition mesh.cpp:3938
int allocateGeometricInfo_edge(Mesh &mesh)
Definition mesh.cpp:3926
int constructElementBoundaryElementsArrayWithGivenElementBoundaryAndEdgeNumbers_triangle(Mesh &mesh)
Definition mesh.cpp:2787
int constructElementBoundaryElementsArrayWithGivenElementBoundaryAndEdgeNumbers_NURBS(Mesh &mesh)
Definition mesh.cpp:1738
int constructElementBoundaryElementsArray_triangle(Mesh &mesh)
Definition mesh.cpp:780
int constructElementBoundaryElementsArrayWithGivenElementBoundaryAndEdgeNumbers_quadrilateral(Mesh &mesh)
Definition mesh.cpp:2962
int computeGeometricInfo_quadrilateral(Mesh &mesh)
Definition mesh.cpp:3866
int computeGeometricInfo_triangle(Mesh &mesh)
Definition mesh.cpp:3768
int allocateGeometricInfo_NURBS(Mesh &mesh)
Definition mesh.cpp:3743
int allocateGeometricInfo_tetrahedron(Mesh &mesh)
Definition mesh.cpp:3535
int constructElementBoundaryElementsArrayWithGivenElementBoundaryAndEdgeNumbers_tetrahedron(Mesh &mesh)
Definition mesh.cpp:3117
int computeGeometricInfo_hexahedron(Mesh &mesh)
Definition mesh.cpp:3663
int computeGeometricInfo_tetrahedron(Mesh &mesh)
Definition mesh.cpp:3547
const int INTERIOR_ELEMENT_BOUNDARY_MATERIAL
Definition mesh.cpp:52
const int EXTERIOR_NODE_MATERIAL
Definition mesh.cpp:51
const int DEFAULT_NODE_MATERIAL
Definition mesh.cpp:49
const int INTERIOR_NODE_MATERIAL
Definition mesh.cpp:50
const int EXTERIOR_ELEMENT_BOUNDARY_MATERIAL
Definition mesh.cpp:53
std::istream & eatcomments(std::istream &s)
Definition meshio.cpp:21
std::istream & eatline(std::istream &s)
Definition meshio.cpp:11
Definition ADR.h:21
int partitionNodesFromTetgenFiles(const MPI_Comm &PROTEUS_COMM_WORLD, const char *filebase, int indexBase, Mesh &newMesh, int nNodes_overlap, double memHardLimit)
int buildQuadraticSubdomain2GlobalMappings_1d(const MPI_Comm &PROTEUS_COMM_WORLD, Mesh &mesh, const int *elementOffsets_subdomain_owned, const int *nodeOffsets_subdomain_owned, const int *elementNumbering_subdomain2global, const int *nodeNumbering_subdomain2global, int &nDOF_all_processes, int &nDOF_subdomain, int &max_dof_neighbors, int *offsets_subdomain_owned, int *subdomain_l2g, int *subdomain2global, double *lagrangeNodesArray)
int buildQuadraticCubeSubdomain2GlobalMappings_3d(const MPI_Comm &PROTEUS_COMM_WORLD, Mesh &mesh, const int *edgeOffsets_subdomain_owned, const int *nodeOffsets_subdomain_owned, const int *edgeNumbering_subdomain2global, const int *nodeNumbering_subdomain2global, int &nDOF_all_processes, int &nDOF_subdomain, int &max_dof_neighbors, int *offsets_subdomain_owned, int *subdomain_l2g, int *subdomain2global, double *lagrangeNodesArray)
int partitionNodesFromTriangleFiles(const MPI_Comm &PROTEUS_COMM_WORLD, const char *filebase, int indexBase, Mesh &newMesh, int nNodes_overlap)
int buildDiscontinuousGalerkinSubdomain2GlobalMappings(const MPI_Comm &PROTEUS_COMM_WORLD, Mesh &mesh, const int *elementOffsets_subdomain_owned, const int *elementNumbering_subdomain2global, int nDOF_element, int &nDOF_all_processes, int &nDOF_subdomain, int &max_dof_neighbors, int *offsets_subdomain_owned, int *subdomain_l2g, int *subdomain2global)
int buildQuadraticSubdomain2GlobalMappings_3d(const MPI_Comm &PROTEUS_COMM_WORLD, Mesh &mesh, const int *edgeOffsets_subdomain_owned, const int *nodeOffsets_subdomain_owned, const int *edgeNumbering_subdomain2global, const int *nodeNumbering_subdomain2global, int &nDOF_all_processes, int &nDOF_subdomain, int &max_dof_neighbors, int *offsets_subdomain_owned, int *subdomain_l2g, int *subdomain2global, double *lagrangeNodesArray)
int partitionElements(const MPI_Comm &PROTEUS_COMM_WORLD, Mesh &mesh, int nElements_overlap)
int partitionNodes(const MPI_Comm &PROTEUS_COMM_WORLD, Mesh &mesh, int nNodes_overlap)
int partitionElementsOriginal(const MPI_Comm &PROTEUS_COMM_WORLD, Mesh &mesh, int nElements_overlap)
int buildQuadraticSubdomain2GlobalMappings_2d(const MPI_Comm &PROTEUS_COMM_WORLD, Mesh &mesh, const int *elementBoundaryOffsets_subdomain_owned, const int *nodeOffsets_subdomain_owned, const int *elementBoundaryNumbering_subdomain2global, const int *nodeNumbering_subdomain2global, int &nDOF_all_processes, int &nDOF_subdomain, int &max_dof_neighbors, int *offsets_subdomain_owned, int *subdomain_l2g, int *subdomain2global, double *lagrangeNodesArray)
double f(const double &g, const double &h, const double &hZ)
Definition SW2DCV.h:58
int enforceMemoryLimit(const MPI_Comm &PROTEUS_COMM_WORLD, int rank, double max_rss_gb, const char *msg)
#define RANK
Definition mesh.h:30
int * elementBoundaryNodesArray
Definition mesh.h:49
int * edgeNumbering_subdomain2global
Definition mesh.h:85
int * nodeStarOffsets
Definition mesh.h:56
int * elementBoundaryNumbering_subdomain2global
Definition mesh.h:83
int nEdges_global
Definition mesh.h:41
double * nodeArray
Definition mesh.h:69
int nNodes_elementBoundary
Definition mesh.h:35
int * nodeNumbering_subdomain2global
Definition mesh.h:81
int nNodes_element
Definition mesh.h:34
int * elementBoundaryElementsArray
Definition mesh.h:50
int * nodeElementOffsets
Definition mesh.h:46
int * elementBoundaryOffsets_subdomain_owned
Definition mesh.h:82
int * nodeElementsArray
Definition mesh.h:45
int nElements_global
Definition mesh.h:32
int * elementOffsets_subdomain_owned
Definition mesh.h:78
int nNodes_global
Definition mesh.h:33
int * nodeStarArray
Definition mesh.h:55
int * elementNodesArray
Definition mesh.h:44
int * elementNeighborsArray
Definition mesh.h:47
int py
Definition mesh.h:66
int * exteriorElementBoundariesArray
Definition mesh.h:53
int * elementBoundaryLocalElementBoundariesArray
Definition mesh.h:51
int nExteriorElementBoundaries_global
Definition mesh.h:39
int pz
Definition mesh.h:66
int nElementBoundaries_element
Definition mesh.h:36
int * elementNumbering_subdomain2global
Definition mesh.h:79
int * nodeOffsets_subdomain_owned
Definition mesh.h:80
int * elementBoundaryMaterialTypes
Definition mesh.h:58
int max_nNodeNeighbors_node
Definition mesh.h:42
int * elementMaterialTypes
Definition mesh.h:57
int * edgeNodesArray
Definition mesh.h:54
int * edgeOffsets_subdomain_owned
Definition mesh.h:84
Mesh * subdomainp
Definition mesh.h:90
int px
Definition mesh.h:66
int nInteriorElementBoundaries_global
Definition mesh.h:38
int * elementBoundariesArray
Definition mesh.h:48
int * interiorElementBoundariesArray
Definition mesh.h:52
int nElementBoundaries_global
Definition mesh.h:37
int * nodeMaterialTypes
Definition mesh.h:59
bool usePlex
Definition mesh.h:88