Debian Codegraph @

dune/uggrid/gm/ugm.cc

raw
1 // SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root
2 // SPDX-License-Identifier: LGPL-2.1-or-later
3 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4 // vi: set et ts=4 sw=2 sts=2:
5 /****************************************************************************/
6 /* */
7 /* File: ugm.c */
8 /* */
9 /* Purpose: unstructured grid manager */
10 /* */
11 /* Author: Peter Bastian */
12 /* Interdisziplinaeres Zentrum fuer Wissenschaftliches Rechnen */
13 /* Universitaet Heidelberg */
14 /* Im Neuenheimer Feld 368 */
15 /* 6900 Heidelberg */
16 /* email: [email protected] */
17 /* */
18 /* History: 09.03.92 begin, ug version 2.0 */
19 /* Aug 28 1996, ug version 3.4 */
20 /* */
21 /* Remarks: */
22 /* */
23 /****************************************************************************/
24
25 /****************************************************************************/
26 /* */
27 /* defines to exclude functions */
28 /* */
29 /****************************************************************************/
30
31 /****************************************************************************/
32 /* */
33 /* include files */
34 /* system include files */
35 /* application include files */
36 /* */
37 /****************************************************************************/
38
39 #include <config.h>
40 #include <cstdlib>
41 #include <cstddef>
42 #include <cstdio>
43 #include <cstring>
44 #include <cmath>
45 #include <cassert>
46 #include <algorithm>
47
48 #include <errno.h>
49 #include <vector>
50
51 #include <dune/uggrid/low/architecture.h>
52 #include <dune/uggrid/low/heaps.h>
53 #include <dune/uggrid/low/debug.h>
54 #include <dune/uggrid/low/fifo.h>
55 #include <dune/uggrid/low/misc.h>
56 #include <dune/uggrid/low/ugenv.h>
57 #include <dune/uggrid/low/ugtypes.h>
58
59 #include <dune/uggrid/ugdevices.h>
60
61 #include "cw.h"
62 #include "evm.h"
63 #include "gm.h"
64 #include "rm.h"
65 #include "dlmgr.h"
66 #include "algebra.h"
67 #include "ugm.h"
68 #include "shapes.h"
69 #include "refine.h"
70 #include <dune/uggrid/domain/std_domain.h>
71 #include "pargm.h"
72
73 #ifdef ModelP
74 #include <dune/uggrid/parallel/dddif/identify.h>
75 #include <dune/uggrid/parallel/ppif/ppif.h>
76 #endif
77
78 #include <dune/uggrid/parallel/ppif/ppifcontext.hh>
79
80 USING_UG_NAMESPACE
81 USING_UGDIM_NAMESPACE
82 using namespace PPIF;
83
84 /****************************************************************************/
85 /* */
86 /* defines in the following order */
87 /* */
88 /* compile time constants defining static data size (i.e. arrays) */
89 /* other constants */
90 /* macros */
91 /* */
92 /****************************************************************************/
93
94 #define RESOLUTION 20 /* resolution for creating boundary midnode */
95 #define SMALL1 0.001
96
97 #define LINKTABLESIZE 32 /* max number of inks per node for ordering */
98
99 /** \brief macro for controlling debugging output by conditions on objects */
100 #define UGM_CDBG(x,y)
101
102 /****************************************************************************/
103 /* */
104 /* data structures used in this source file (exported data structures are */
105 /* in the corresponding include file!) */
106 /* */
107 /****************************************************************************/
108
109 /****************************************************************************/
110 /* */
111 /* definition of variables global to this source file only (static!) */
112 /* */
113 /****************************************************************************/
114
115 /** \brief General purpose text buffer */
116 static char buffer[4*256];
117
118 static INT theMGDirID; /* env var ID for the multigrids */
119 static INT theMGRootDirID; /* env dir ID for the multigrids */
120
121 static UINT UsedOBJT; /* for the dynamic OBJECT management */
122
123 /****************************************************************************/
124 /* */
125 /* forward declarations of functions used before they are defined */
126 /* */
127 /****************************************************************************/
128
129 static INT DisposeVertex (GRID *theGrid, VERTEX *theVertex);
130 static INT DisposeEdge (GRID *theGrid, EDGE *theEdge);
131
132
133 /****************************************************************************/
134 /** \brief Get an object type id not occupied in theMG
135 *
136 * This function gets an object type id not occupied in theMG.
137 *
138 * @return <ul>
139 * <li> id of object type if ok </li>
140 * <li> -1 when error occurred </li>
141 * </ul>
142 */
143 /****************************************************************************/
144
145 GM_OBJECTS NS_DIM_PREFIX GetFreeOBJT ()
146 {
147 INT i;
148
149 /* skip predefined object types, they cannot be re-allocated */
150 for (i=NPREDEFOBJ; i<MAXOBJECTS; i++)
151 if (!READ_FLAG(UsedOBJT,1<<i))
152 break;
153
154 if (i<MAXOBJECTS)
155 {
156 SET_FLAG(UsedOBJT,1<<i);
157 return (GM_OBJECTS)i;
158 }
159 else
160 return NOOBJ;
161 }
162
163 /****************************************************************************/
164 /** \brief Get an object from free list if possible
165 \fn GetMemoryForObject
166
167 * @param theMG - pointer to multigrid
168 * @param size - size of the object
169 * @param type - type of the requested object
170
171 This function gets an object of type `type` from free list if possible,
172 otherwise it allocates memory from the multigrid heap using 'GetMem'.
173
174 @return <ul>
175 <li> pointer to an object of the requested type </li>
176 <li> NULL if object of requested type is not available </li>
177 * </ul>
178 */
179 /****************************************************************************/
180
181 #ifdef ModelP
182 static void ConstructDDDObject (DDD::DDDContext& context, void *obj, INT size, INT type)
183 {
184 if (obj!=NULL && type!=NOOBJ)
185 {
186 memset(obj,0,size);
187 /* link this object to DDD management */
188 if (HAS_DDDHDR(context, type))
189 {
190 DDD_TYPE dddtype = DDDTYPE(context, type);
191 DDD_HDR dddhdr = (DDD_HDR)(((char *)obj) + DDD_InfoHdrOffset(context, dddtype));
192 DDD_HdrConstructor(context, dddhdr, dddtype, PrioMaster, 0);
193 }
194 }
195 }
196 #endif
197
198 void * NS_DIM_PREFIX GetMemoryForObject (MULTIGRID *theMG, INT size, INT type)
199 {
200 void * obj = GetMem(MGHEAP(theMG),size);
201 if (obj != NULL)
202 memset(obj,0,size);
203
204 #ifdef ModelP
205 ConstructDDDObject(theMG->dddContext(), obj,size,type);
206 #endif
207
208 return obj;
209 }
210
211 /****************************************************************************/
212 /** \brief Put an object in the free list
213 \fn PutFreeObject
214
215 * @param theMG - pointer to multigrid
216 * @param object - object to insert in free list
217 * @param size - size of the object
218 * @param type - type of the requested object
219
220 This function puts an object in the free list.
221
222 @return <ul>
223 <li> 0 if ok </li>
224 <li> 1 when error occurred. </li>
225 * </ul> */
226 /****************************************************************************/
227
228 #ifdef ModelP
229 static void DestructDDDObject(DDD::DDDContext& context, void *object, INT type)
230 {
231 if (type!=NOOBJ)
232 {
233 /* unlink object from DDD management */
234 if (HAS_DDDHDR(context, type))
235 {
236 DDD_HDR dddhdr = (DDD_HDR)
237 (((char *)object)+DDD_InfoHdrOffset(context, DDDTYPE(context, type)));
238 DDD_HdrDestructor(context, dddhdr);
239 }
240 }
241 }
242 #endif
243
244 INT NS_DIM_PREFIX PutFreeObject (MULTIGRID *theMG, void *object, INT size, GM_OBJECTS type)
245 {
246 #ifdef ModelP
247 DestructDDDObject(theMG->dddContext(), object,type);
248 #endif
249
250 DisposeMem(MGHEAP(theMG), object);
251 return 0;
252 }
253
254 /****************************************************************************/
255 /** \brief Return pointer to a new boundary vertex structure
256 *
257 * @param theGrid grid where vertex should be inserted
258 *
259 * This function creates and initializes a new boundary vertex structure
260 * and returns a pointer to it.
261 *
262 * @return <ul>
263 * <li> pointer to requested object </li>
264 * <li> NULL if out of memory </li>
265 * </ul>
266 */
267 /****************************************************************************/
268
269 static VERTEX *CreateBoundaryVertex (GRID *theGrid)
270 {
271 VERTEX *pv;
272 INT i;
273
274 pv = (VERTEX*)GetMemoryForObject(MYMG(theGrid),sizeof(struct bvertex),BVOBJ);
275 if (pv==NULL) return(NULL);
276 VDATA(pv) = NULL;
277
278 /* initialize data */
279 CTRL(pv) = 0;
280 SETOBJT(pv,BVOBJ);
281 SETNOOFNODE(pv,0);
282 SETLEVEL(pv,theGrid->level);
283 ID(pv) = (theGrid->mg->vertIdCounter)++;
284 VFATHER(pv) = NULL;
285 #ifdef TOPNODE
286 TOPNODE(pv) = NULL;
287 #endif
288 for (i=0; i<DIM; i++) LCVECT(pv)[i] = 0.0;
289 SETONEDGE(pv,0);
290 SETMOVE(pv,DIM_OF_BND);
291 #ifdef ModelP
292 DDD_AttrSet(PARHDRV(pv),GRID_ATTR(theGrid));
293 /* SETVXPRIO(pv,PrioMaster); */
294 #endif
295
296 /* insert in vertex list */
297 GRID_LINK_VERTEX(theGrid,pv,PrioMaster);
298
299 return(pv);
300 }
301
302 /****************************************************************************/
303 /** \brief Return pointer to a new inner vertex structure
304 *
305 * @param theGrid grid where vertex should be inserted
306 *
307 * This function creates and initializes a new inner vertex structure
308 * and returns a pointer to it.
309 *
310 * @return <ul>
311 <li> pointer to requested object </li>
312 <li> NULL if out of memory </li>
313 </ul> */
314 /****************************************************************************/
315
316 static VERTEX *CreateInnerVertex (GRID *theGrid)
317 {
318 VERTEX *pv;
319 INT i;
320
321 pv = (VERTEX*)GetMemoryForObject(MYMG(theGrid),sizeof(struct ivertex),IVOBJ);
322 if (pv==NULL) return(NULL);
323 VDATA(pv) = NULL;
324
325 /* initialize data */
326 CTRL(pv) = 0;
327 SETOBJT(pv,IVOBJ);
328 SETNOOFNODE(pv,0);
329 SETLEVEL(pv,theGrid->level);
330 ID(pv) = (theGrid->mg->vertIdCounter)++;
331 VFATHER(pv) = NULL;
332 #ifdef TOPNODE
333 TOPNODE(pv) = NULL;
334 #endif
335 SETMOVE(pv,DIM);
336 #ifdef ModelP
337 DDD_AttrSet(PARHDRV(pv),GRID_ATTR(theGrid));
338 /* SETVXPRIO(pv,PrioMaster); */
339 #endif
340 for (i=0; i<DIM; i++) LCVECT(pv)[i] = 0.0;
341
342 /* insert in vertex list */
343 GRID_LINK_VERTEX(theGrid,pv,PrioMaster);
344
345 return(pv);
346 }
347
348 /****************************************************************************/
349 /** \brief Return pointer to a new node structure
350
351 * @param theGrid - grid where vertex should be inserted
352 * @param vertex - vertex of the node
353 * @param FatherNode - father node (may be NULL)
354 * @param NodeType - node type (CORNER_NODE..., cf. gm.h)
355
356 This function creates and initializes a new node structure
357 and returns a pointer to it.
358
359 @return <ul>
360 <li> pointer to requested object </li>
361 <li> NULL if out of memory </li>
362 </ul> */
363 /****************************************************************************/
364
365 static NODE *CreateNode (GRID *theGrid, VERTEX *vertex,
366 GEOM_OBJECT *Father, INT NodeType)
367 {
368 NODE *pn;
369
370 pn = (NODE *)GetMemoryForObject(MYMG(theGrid),sizeof(NODE),NDOBJ);
371 if (pn==NULL) return(NULL);
372
373 /* initialize data */
374 SETOBJT(pn,NDOBJ);
375 SETLEVEL(pn,theGrid->level);
376 #ifdef ModelP
377 DDD_AttrSet(PARHDR(pn),GRID_ATTR(theGrid));
378 /* SETPRIO(pn,PrioMaster); */
379 pn->message_buffer_ = nullptr;
380 pn->message_buffer_size_ = 0;
381 #endif
382 ID(pn) = (theGrid->mg->nodeIdCounter)++;
383 START(pn) = NULL;
384 SONNODE(pn) = NULL;
385 MYVERTEX(pn) = vertex;
386 if (NOOFNODE(vertex)<NOOFNODEMAX)
387 INCNOOFNODE(vertex);
388 else
389 ASSERT(0);
390 /* priliminary */
391 if (Father != NULL)
392 if ((OBJT(Father) == IEOBJ) || (OBJT(Father) == BEOBJ))
393 Father = NULL;
394 SETNFATHER(pn,Father);
395 SETNTYPE(pn,NodeType);
396 SETNCLASS(pn,3),
397 SETNNCLASS(pn,0);
398 if (OBJT(vertex) == BVOBJ)
399 SETNSUBDOM(pn,0);
400 else if (VFATHER(vertex) != NULL)
401 SETNSUBDOM(pn,SUBDOMAIN(VFATHER(vertex)));
402 else if (Father != NULL) {
403 if (OBJT(Father) == NDOBJ)
404 SETNSUBDOM(pn,NSUBDOM((NODE *)Father));
405 else if (OBJT(Father) == EDOBJ)
406 SETNSUBDOM(pn,EDSUBDOM((EDGE *)Father));
407 }
408 else
409 SETNSUBDOM(pn,0);
410
411 theGrid->status |= 1; /* recalculate stiffness matrix */
412
413 /* insert in vertex list */
414 GRID_LINK_NODE(theGrid,pn,PrioMaster);
415
416 return(pn);
417 }
418
419 /****************************************************************************/
420 /** \brief Return pointer to a new node structure on an edge
421
422 * @param theGrid - grid where vertex should be inserted
423 * @param FatherNode - node father
424
425 This function creates and initializes a new node structure
426 at the midpoint of an element edge and returns a pointer to it.
427
428 @return <ul>
429 <li> pointer to requested object </li>
430 <li> NULL if out of memory </li>
431 </ul> */
432 /****************************************************************************/
433
434 NODE *NS_DIM_PREFIX CreateSonNode (GRID *theGrid, NODE *FatherNode)
435 {
436 NODE *pn;
437 VERTEX *theVertex;
438
439 theVertex = MYVERTEX(FatherNode);
440
441 pn = CreateNode(theGrid,theVertex,(GEOM_OBJECT *)FatherNode,CORNER_NODE);
442 if (pn == NULL)
443 return(NULL);
444 SONNODE(FatherNode) = pn;
445
446 #ifdef TOPNODE
447 TOPNODE(theVertex) = pn;
448 #endif
449
450 return(pn);
451 }
452
453 /****************************************************************************/
454 /** \brief Return pointer to a new node structure on an edge
455
456 * @param theGrid - grid where node should be inserted
457 * @param theElement - pointer to an element
458 * @param theVertex - pointer to vertex if already existing
459 * @param edge - id of an element edge
460
461 This function creates and initializes a new node structure
462 at the midpoint of an element edge and returns a pointer to it.
463
464 @return <ul>
465 <li> pointer to requested object </li>
466 <li> NULL if out of memory </li>
467 </ul> */
468 /****************************************************************************/
469
470 NODE *NS_DIM_PREFIX CreateMidNode (GRID *theGrid, ELEMENT *theElement, VERTEX *theVertex, INT edge)
471 {
472 BNDP *bndp;
473 DOUBLE *x[MAX_CORNERS_OF_ELEM];
474 DOUBLE_VECTOR bnd_global,global;
475 DOUBLE diff;
476 INT n,move;
477
478 const INT co0 = CORNER_OF_EDGE(theElement,edge,0);
479 const INT co1 = CORNER_OF_EDGE(theElement,edge,1);
480 VERTEX* v0 = MYVERTEX(CORNER(theElement,co0));
481 VERTEX* v1 = MYVERTEX(CORNER(theElement,co1));
482 V_DIM_LINCOMB(0.5, CVECT(v0), 0.5, CVECT(v1), global);
483
484 /* set MIDNODE pointer */
485 EDGE* theEdge = GetEdge(CORNER(theElement,co0),CORNER(theElement,co1));
486 ASSERT(theEdge!=NULL);
487
488 /* allocate vertex */
489 const bool vertex_null = (theVertex==NULL);
490 if (theVertex==NULL)
491 {
492 if ((OBJT(v0) == BVOBJ) && (OBJT(v1) == BVOBJ))
493 #ifdef UG_DIM_2
494 if (OBJT(theElement) == BEOBJ)
495 if (SIDE_ON_BND(theElement,edge))
496 #endif
497 #ifdef UG_DIM_3
498 if (EDSUBDOM(theEdge) == 0)
499 #endif
500 {
501 bndp = BNDP_CreateBndP(MGHEAP(MYMG(theGrid)),V_BNDP(v0),V_BNDP(v1),0.5);
502 if (bndp != NULL)
503 {
504 theVertex = CreateBoundaryVertex(theGrid);
505 if (theVertex == NULL)
506 return(NULL);
507 if (BNDP_Global(bndp,bnd_global))
508 return(NULL);
509 if (BNDP_BndPDesc(bndp,&move))
510 return(NULL);
511 SETMOVE(theVertex,move);
512 V_BNDP(theVertex) = bndp;
513 V_DIM_COPY(bnd_global,CVECT(theVertex));
514 FieldVector<DOUBLE,DIM>& local = LCVECT(theVertex);
515 V_DIM_EUKLIDNORM_OF_DIFF(bnd_global,global,diff);
516 if (diff > MAX_PAR_DIST)
517 {
518 SETMOVED(theVertex,1);
519 CORNER_COORDINATES(theElement,n,x);
520 UG_GlobalToLocal(n,(const DOUBLE **)x,bnd_global,local);
521 }
522 else
523 V_DIM_LINCOMB(0.5, LOCAL_COORD_OF_ELEM(theElement,co0),
524 0.5, LOCAL_COORD_OF_ELEM(theElement,co1),local);
525 PRINTDEBUG(gm,1,("local = %f %f %f\n",local[0],local[1],local[2]));
526 }
527 }
528 if (theVertex == NULL)
529 {
530 /* we need an inner vertex */
531 theVertex = CreateInnerVertex(theGrid);
532 if (theVertex==NULL) return(NULL);
533 V_DIM_COPY(global,CVECT(theVertex));
534 V_DIM_LINCOMB(0.5, LOCAL_COORD_OF_ELEM(theElement,co0),
535 0.5, LOCAL_COORD_OF_ELEM(theElement,co1),
536 LCVECT(theVertex));
537 }
538 VFATHER(theVertex) = theElement;
539 SETONEDGE(theVertex,edge);
540 }
541 /* allocate node */
542 NODE* theNode = CreateNode(theGrid,theVertex,(GEOM_OBJECT *)theEdge,MID_NODE);
543 if (theNode==NULL && vertex_null)
544 {
545 DisposeVertex(theGrid,theVertex);
546 return(NULL);
547 }
548
549 MIDNODE(theEdge) = theNode;
550 #ifdef TOPNODE
551 if (TOPNODE(theVertex)==NULL || LEVEL(TOPNODE(theVertex))<LEVEL(theNode))
552 TOPNODE(theVertex) = theNode;
553 #endif
554
555 if (OBJT(theVertex) == BVOBJ)
556 PRINTDEBUG(dom,1,(" MidPoint %d %f %f %f\n",ID(theNode),
557 bnd_global[0],
558 bnd_global[1],
559 bnd_global[2]));
560
561 PRINTDEBUG(dddif,1,(PFMT " CreateMidNode(): n=" ID_FMTX
562 " NTYPE=%d OBJT=%d father " ID_FMTX " \n",
563 theGrid->ppifContext().me(),ID_PRTX(theNode),NTYPE(theNode),
564 OBJT(NFATHER(theNode)),NFATHER(theNode)));
565
566 return(theNode);
567 }
568
569
570 NODE * NS_DIM_PREFIX GetMidNode (const ELEMENT *theElement, INT edge)
571 {
572 EDGE *theEdge;
573 NODE *theNode;
574 VERTEX *theVertex;
575
576 theEdge = GetEdge(CORNER(theElement,CORNER_OF_EDGE(theElement,edge,0)),
577 CORNER(theElement,CORNER_OF_EDGE(theElement,edge,1)));
578 if (theEdge == NULL) return(NULL);
579 theNode = MIDNODE(theEdge);
580 if (theNode == NULL) return(NULL);
581
582 /** \todo This is a bad place for the following code (s.l. 981015) */
583 theVertex = MYVERTEX(theNode);
584 if (theVertex!=NULL && VFATHER(theVertex) == NULL) {
585 /** \todo Strange that this cast has to be here. O.S. 060902 */
586 VFATHER(theVertex) = (ELEMENT*)theElement;
587 SETONEDGE(theVertex,edge);
588 V_DIM_LINCOMB(0.5,
589 LOCAL_COORD_OF_ELEM(theElement,
590 CORNER_OF_EDGE(theElement,edge,0)),
591 0.5,
592 LOCAL_COORD_OF_ELEM(theElement,
593 CORNER_OF_EDGE(theElement,edge,1)),
594 LCVECT(theVertex));
595 }
596 return(theNode);
597 }
598
599
600 /****************************************************************************/
601 /** \brief ???
602 */
603 /****************************************************************************/
604
605 static INT SideOfNbElement(const ELEMENT *theElement, INT side)
606 {
607 ELEMENT *nb;
608 NODE *nd[MAX_CORNERS_OF_SIDE];
609 INT i,j,m,n,num;
610
611 nb = NBELEM(theElement,side);
612 if (nb == NULL) return(MAX_SIDES_OF_ELEM);
613
614 for (j=0; j<SIDES_OF_ELEM(nb); j++)
615 if (NBELEM(nb,j) == theElement) return(j);
616
617 n = CORNERS_OF_SIDE(theElement,side);
618 for (i=0; i<n; i++)
619 nd[i] = CORNER(theElement,CORNER_OF_SIDE(theElement,side,i));
620
621 for (j=0; j<SIDES_OF_ELEM(nb); j++) {
622 num = 0;
623 for (i=0; i<n; i++)
624 for (m=0; m<CORNERS_OF_SIDE(nb,j); m++)
625 if (nd[i] == CORNER(nb,CORNER_OF_SIDE(nb,j,m))) num++;
626 if (num == n) return(j);
627 }
628
629 return(MAX_SIDES_OF_ELEM);
630 }
631
632 #ifdef UG_DIM_3
633
634
635 /****************************************************************************/
636 /** \brief Return pointer to a new node structure on a side (3d)
637
638 * @param theGrid - grid where vertex should be inserted
639 * @param theElement - pointer to an element
640 * @param theVertex - pointer vertex
641 * @param side - id of an element side
642
643 This function creates and initializes a new node structure
644 at the midpoint of an element side and returns a pointer to it.
645
646 @return <ul>
647 <li> pointer to requested object </li>
648 <li> NULL if out of memory </li>
649 </ul> */
650 /****************************************************************************/
651
652 NODE *NS_DIM_PREFIX CreateSideNode (GRID *theGrid, ELEMENT *theElement, VERTEX *theVertex, INT side)
653 {
654 DOUBLE_VECTOR bnd_global,global;
655 FieldVector<DOUBLE,DIM> local;
656 DOUBLE *x[MAX_CORNERS_OF_ELEM];
657 NODE *theNode;
658 BNDP *bndp;
659 BNDS *bnds;
660 DOUBLE fac, diff;
661 INT n,j,k,move,vertex_null;
662
663 n = CORNERS_OF_SIDE(theElement,side);
664 fac = 1.0 / n;
665 V_DIM_CLEAR(local);
666 V_DIM_CLEAR(global);
667 for (j=0; j<n; j++)
668 {
669 k = CORNER_OF_SIDE(theElement,side,j);
670 V_DIM_LINCOMB(1.0,local,1.0,
671 LOCAL_COORD_OF_ELEM(theElement,k),local);
672 V_DIM_LINCOMB(1.0,global,1.0,
673 CVECT(MYVERTEX(CORNER(theElement,k))),global);
674 }
675 V_DIM_SCALE(fac,local);
676 V_DIM_SCALE(fac,global);
677
678 /* check if boundary vertex */
679 vertex_null = (theVertex==NULL);
680 if (theVertex==NULL)
681 {
682 if (OBJT(theElement) == BEOBJ) {
683 bnds = ELEM_BNDS(theElement,side);
684 if (bnds != NULL) {
685 FieldVector<DOUBLE,DIM_OF_BND> bnd_local;
686 if (n == 3)
687 bnd_local[0] = bnd_local[1] = 0.33333333333333;
688 else if (n == 4)
689 bnd_local[0] = bnd_local[1] = 0.5;
690 bndp = BNDS_CreateBndP(MGHEAP(MYMG(theGrid)),bnds,bnd_local);
691 if (bndp != NULL) {
692 theVertex = CreateBoundaryVertex(theGrid);
693 if (theVertex == NULL)
694 return(NULL);
695 if (BNDP_BndPDesc(bndp,&move))
696 return(NULL);
697 SETMOVE(theVertex,move);
698 if (BNDP_Global(bndp,bnd_global))
699 return(NULL);
700 V_BNDP(theVertex) = bndp;
701 V_DIM_COPY(bnd_global,CVECT(theVertex));
702 V_DIM_EUKLIDNORM_OF_DIFF(bnd_global,global,diff);
703 if (diff > MAX_PAR_DIST) {
704 SETMOVED(theVertex,1);
705 CORNER_COORDINATES(theElement,k,x);
706 UG_GlobalToLocal(k,(const DOUBLE **)x,bnd_global,local);
707 PRINTDEBUG(gm,1,("local = %f %f %f\n",local[0],local[1],local[2]));
708 }
709 }
710 }
711 }
712
713 if (theVertex == NULL)
714 {
715 theVertex = CreateInnerVertex(theGrid);
716 if (theVertex == NULL) return(NULL);
717 V_DIM_COPY(global,CVECT(theVertex));
718 }
719 VFATHER(theVertex) = theElement;
720 SETONSIDE(theVertex,side);
721 SETONNBSIDE(theVertex,SideOfNbElement(theElement,side));
722 V_DIM_COPY(local,LCVECT(theVertex));
723 }
724 /* create node */
725 theNode = CreateNode(theGrid,theVertex,
726 (GEOM_OBJECT *)theElement,SIDE_NODE);
727 if (theNode==NULL && vertex_null)
728 {
729 DisposeVertex(theGrid,theVertex);
730 return(NULL);
731 }
732 #ifdef TOPNODE
733 if (TOPNODE(theVertex) == NULL || LEVEL(TOPNODE(theVertex))<LEVEL(theNode))
734 TOPNODE(theVertex) = theNode;
735 #endif
736 theGrid->status |= 1;
737
738 return(theNode);
739 }
740
741 static NODE *GetSideNodeX (const ELEMENT *theElement, INT side, INT n,
742 NODE **MidNodes)
743 {
744 ELEMENT *theFather;
745 NODE *theNode;
746 VERTEX *theVertex;
747 LINK *theLink0,*theLink1,*theLink2,*theLink3;
748 DOUBLE fac;
749 INT i;
750
751 if (n == 4) {
752 for (theLink0=START(MidNodes[0]); theLink0!=NULL;
753 theLink0=NEXT(theLink0)) {
754 theNode = NBNODE(theLink0);
755 if (NTYPE(theNode) != SIDE_NODE)
756 continue;
757 for (theLink1=START(MidNodes[1]); theLink1!=NULL;
758 theLink1=NEXT(theLink1)) {
759 if (theNode != NBNODE(theLink1))
760 continue;
761 for (theLink2=START(MidNodes[2]); theLink2!=NULL;
762 theLink2=NEXT(theLink2)) {
763 if (theNode != NBNODE(theLink2))
764 continue;
765 for (theLink3=START(MidNodes[3]); theLink3!=NULL;
766 theLink3=NEXT(theLink3)) {
767 if (theNode != NBNODE(theLink3))
768 continue;
769 theVertex = MYVERTEX(theNode);
770 theFather = VFATHER(theVertex);
771 if (theFather == theElement) {
772 #ifndef ModelP
773 /* HEAPFAULT in theFather possible,
774 if in a previous call of DisposeElement
775 some son is not reached by GetAllSons */
776 assert(ONSIDE(theVertex) == side);
777 #endif
778 SETONSIDE(theVertex,side);
779 return(theNode);
780 }
781 else if (theFather == NBELEM(theElement,side)) {
782 SETONNBSIDE(theVertex,side);
783 return(theNode);
784 }
785 else if (theFather == NULL) {
786 /** \todo Strange that this cast has to be here */
787 VFATHER(theVertex) = (ELEMENT*)theElement;
788 SETONSIDE(theVertex,side);
789 SETONNBSIDE(theVertex,
790 SideOfNbElement(theElement,side));
791 fac = 1.0 / n;
792 Dune::FieldVector<DOUBLE,DIM>& local = LCVECT(theVertex);
793 V_DIM_CLEAR(local);
794 for (i=0; i<n; i++) {
795 V_DIM_LINCOMB(1.0,local,fac,
796 LOCAL_COORD_OF_ELEM(theElement,
797 CORNER_OF_SIDE(theElement,side,i)),
798 local);
799 }
800 return(theNode);
801 }
802 #ifndef ModelP
803 /* HEAPFAULT in theFather possible,
804 if in a previous call of DisposeElement
805 some son is not reached by GetAllSons */
806 else
807 assert(0);
808 #endif
809 return(theNode);
810 }
811 }
812 }
813 }
814 }
815 else if (n == 3) {
816 for (theLink0=START(MidNodes[0]); theLink0!=NULL;
817 theLink0=NEXT(theLink0)) {
818 theNode = NBNODE(theLink0);
819 if (NTYPE(theNode) != SIDE_NODE)
820 continue;
821 for (theLink1=START(MidNodes[1]); theLink1!=NULL;
822 theLink1=NEXT(theLink1)) {
823 if (theNode != NBNODE(theLink1))
824 continue;
825 for (theLink2=START(MidNodes[2]); theLink2!=NULL;
826 theLink2=NEXT(theLink2)) {
827 if (theNode != NBNODE(theLink2))
828 continue;
829 theVertex = MYVERTEX(theNode);
830 theFather = VFATHER(theVertex);
831 if (theFather == theElement) {
832 if (ONSIDE(theVertex) == side)
833 return(theNode);
834 #ifdef ModelP
835 SETONSIDE(theVertex,side);
836 return(theNode);
837 #endif
838 }
839 else if (theFather == NBELEM(theElement,side))
840 {
841 INT nbside = SideOfNbElement(theElement,side);
842 if (nbside==ONSIDE(theVertex))
843 {
844 SETONNBSIDE(theVertex,side);
845 return(theNode);
846 }
847 #ifdef ModelP
848 /** \todo Strange that this cast has to be here */
849 VFATHER(theVertex) = (ELEMENT*)theElement;
850 SETONSIDE(theVertex,side);
851 SETONNBSIDE(theVertex,nbside);
852 return(theNode);
853 #endif
854 }
855 else if (theFather == NULL) {
856 /** \todo Strange that this cast has to be here */
857 VFATHER(theVertex) = (ELEMENT*)theElement;
858 SETONSIDE(theVertex,side);
859 SETONNBSIDE(theVertex,
860 SideOfNbElement(theElement,side));
861 fac = 1.0 / n;
862 Dune::FieldVector<DOUBLE,DIM>& local = LCVECT(theVertex);
863 V_DIM_CLEAR(local);
864 for (i=0; i<n; i++) {
865 V_DIM_LINCOMB(1.0,local,fac,
866 LOCAL_COORD_OF_ELEM(theElement,
867 CORNER_OF_SIDE(theElement,side,i)),
868 local);
869 }
870 return(theNode);
871 }
872 #ifdef ModelP
873 else {
874 return(theNode);
875 }
876 #endif
877 }
878 }
879 }
880 }
881 #ifdef ModelP
882 else if (n == 2) {
883 for (theLink0=START(MidNodes[0]); theLink0!=NULL;
884 theLink0=NEXT(theLink0)) {
885 theNode = NBNODE(theLink0);
886 if (NTYPE(theNode) != SIDE_NODE)
887 continue;
888 for (theLink1=START(MidNodes[1]); theLink1!=NULL;
889 theLink1=NEXT(theLink1)) {
890 if (theNode != NBNODE(theLink1))
891 continue;
892 theVertex = MYVERTEX(theNode);
893 theFather = VFATHER(theVertex);
894 if (theFather == theElement) {
895 if (ONSIDE(theVertex) == side)
896 return(theNode);
897 SETONSIDE(theVertex,side);
898 return(theNode);
899 }
900 else if (theFather == NBELEM(theElement,side)) {
901 SETONNBSIDE(theVertex,side);
902 return(theNode);
903 }
904 return(theNode);
905 }
906 }
907 }
908 #endif
909
910 return(NULL);
911 }
912
913 NODE * NS_DIM_PREFIX GetSideNode (const ELEMENT *theElement, INT side)
914 {
915 NODE *theNode;
916 NODE *MidNodes[MAX_EDGES_OF_SIDE];
917 INT i,n;
918 #ifdef ModelP
919 INT k;
920 #endif
921
922 n = 0;
923 for (i=0; i<EDGES_OF_SIDE(theElement,side); i++) {
924 theNode = GetMidNode(theElement,EDGE_OF_SIDE(theElement,side,i));
925 if (theNode != NULL)
926 MidNodes[n++] = theNode;
927 #ifndef ModelP
928 else return(NULL);
929 #endif
930 }
931 PRINTDEBUG(gm,2,("GetSideNode(): elem=" EID_FMTX
932 " side=%d nb. of midnodes=%d\n",
933 EID_PRTX(theElement),side,n));
934 #ifdef ModelP
935 if (TAG(theElement)==PYRAMID && side!=0) return(NULL);
936 #endif
937 theNode = GetSideNodeX(theElement,side,n,MidNodes);
938 #ifdef ModelP
939 if (theNode != NULL)
940 return(theNode);
941 if (n < 3)
942 return(NULL);
943 for (i=0; i<n; i++)
944 {
945 NODE *MidNodes1[MAX_EDGES_OF_SIDE-1];
946 INT j,m;
947
948 m = 0;
949 for (j=0; j<n; j++) {
950 if (i == j) continue;
951 MidNodes1[m++] = MidNodes[j];
952 }
953 theNode = GetSideNodeX(theElement,side,n-1,MidNodes1);
954 if (theNode != NULL)
955 return(theNode);
956 }
957 if (n < 4)
958 return(NULL);
959 for (i=1; i<n; i++)
960 for (k=0; k<i; k++)
961 {
962 NODE *MidNodes1[MAX_EDGES_OF_SIDE-2];
963 INT j,m;
964
965 m = 0;
966 for (j=0; j<n; j++) {
967 if (i == j) continue;
968 if (k == j) continue;
969 MidNodes1[m++] = MidNodes[j];
970 }
971 theNode = GetSideNodeX(theElement,side,n-2,MidNodes1);
972 if (theNode != NULL)
973 return(theNode);
974 }
975 #endif
976
977 return(theNode);
978 }
979
980 static int CountSideNodes (ELEMENT *e)
981 {
982 int side = 0;
983
984 for (int i = 0; i < CORNERS_OF_ELEM(e); i++)
985 {
986 const NODE *n = CORNER(e,i);
987 if (SIDETYPE(n)) side++;
988 }
989 return(side);
990 }
991
992 int GetSideIDFromScratchSpecialRule17Pyr (ELEMENT *theElement, NODE *theNode)
993 {
994 ELEMENT *f = EFATHER(theElement);
995 int side = SIDES_OF_ELEM(f);
996
997 #ifdef Debug
998 assert(TAG(theElement)==PYRAMID);
999 INT snodes = 0;
1000 INT cnodes = 0;
1001 for (int l = 0; l < CORNERS_OF_ELEM(theElement); l++)
1002 {
1003 const NODE *enode = CORNER(theElement,l);
1004 if (CORNERTYPE(enode)) cnodes++;
1005 if (SIDETYPE(enode)) snodes++;
1006 }
1007 assert(snodes == 1);
1008 assert(cnodes == 4);
1009 #endif
1010
1011 for (int i = 0; i < SIDES_OF_ELEM(f); i++)
1012 {
1013 int nodes = 0;
1014 for (int k = 0; k < CORNERS_OF_SIDE(f,i); k++)
1015 {
1016 const NODE *fnode = CORNER(f, CORNER_OF_SIDE(f, i, k));
1017 for (int l = 0; l < CORNERS_OF_ELEM(theElement); l++)
1018 {
1019 const NODE *enode = CORNER(theElement,l);
1020 if (enode == SONNODE(fnode)) nodes++;
1021 }
1022 }
1023 assert(nodes==0 || nodes==2 || nodes==4);
1024 #ifdef Debug
1025 if (nodes == 0) side = i;
1026 #else
1027 if (nodes == 0) return(i);
1028 #endif
1029 }
1030
1031 assert(side<SIDES_OF_ELEM(f));
1032 return(side);
1033 }
1034
1035
1036 int GetSideIDFromScratchSpecialRule22Tet (ELEMENT *theElement, NODE *theNode)
1037 {
1038 int i,k,l,nodes,midnodes;
1039 #ifdef Debug
1040 INT cnodes, snodes, mnodes;
1041 #endif
1042 ELEMENT *f = EFATHER(theElement);
1043 int side = SIDES_OF_ELEM(f);
1044
1045 #ifdef Debug
1046 assert(TAG(theElement)==TETRAHEDRON);
1047 snodes = cnodes = mnodes = 0;
1048 for (l=0; l<CORNERS_OF_ELEM(theElement); l++)
1049 {
1050 const NODE *enode = CORNER(theElement,l);
1051 if (CORNERTYPE(enode)) cnodes++;
1052 if (MIDTYPE(enode)) mnodes++;
1053 if (SIDETYPE(enode)) snodes++;
1054 }
1055 assert(cnodes == 2);
1056 assert(mnodes == 1);
1057 assert(snodes == 1);
1058 #endif
1059
1060 for (i=0; i<SIDES_OF_ELEM(f); i++)
1061 {
1062 nodes = 0;
1063 midnodes = 0;
1064 for (k=0; k<CORNERS_OF_SIDE(f,i); k++)
1065 {
1066 const NODE *fnode = CORNER(f,CORNER_OF_SIDE(f,i,k));
1067
1068 const EDGE *edge = GetEdge(CORNER_OF_SIDE_PTR(f,i,k),
1069 CORNER_OF_SIDE_PTR(f,i,(k+1)%CORNERS_OF_SIDE(f,i)));
1070 assert(edge != NULL);
1071
1072 for (l=0; l<CORNERS_OF_ELEM(theElement); l++)
1073 {
1074 const NODE *enode = CORNER(theElement,l);
1075 if (enode == SONNODE(fnode)) nodes++;
1076 if (enode == MIDNODE(edge)) midnodes++;
1077 }
1078 }
1079 assert(nodes==0 || nodes==1 || nodes==2 || nodes==4);
1080 #ifdef Debug
1081 if (nodes==0 && midnodes==1) side = i;
1082 #else
1083 if (nodes==0 && midnodes==1) return(i);
1084 #endif
1085 }
1086
1087 assert(side<SIDES_OF_ELEM(f));
1088 return(side);
1089 }
1090
1091
1092 INT GetSideIDFromScratchSpecialRule (ELEMENT *theElement, NODE *theNode)
1093 {
1094 int j,l;
1095 #ifndef NDEBUG
1096 ELEMENT *f = EFATHER(theElement);
1097 #endif
1098
1099 assert(TAG(f)==HEXAHEDRON);
1100 assert(ECLASS(theElement)==GREEN_CLASS);
1101 assert(NSONS(f)==9 || NSONS(f)==11 || EHGHOST(theElement));
1102
1103 if (TAG(theElement)==PYRAMID)
1104 {
1105 return(GetSideIDFromScratchSpecialRule17Pyr(theElement,theNode));
1106 }
1107
1108 assert(TAG(theElement)==TETRAHEDRON);
1109 /* centroid tetrahedron of special rule 22 */
1110 if (CountSideNodes(theElement) == 2)
1111 {
1112 /* if side not found search over neighbor */
1113 for (j=0; j<SIDES_OF_ELEM(theElement); j++)
1114 {
1115 ELEMENT *nb = NBELEM(theElement,j);
1116
1117 if (nb == NULL) continue;
1118
1119 for (l=0; l<CORNERS_OF_ELEM(nb); l++)
1120 if (theNode == CORNER(nb,l))
1121 return(GetSideIDFromScratch(nb,theNode));
1122 }
1123 }
1124
1125 assert(CountSideNodes(theElement)==1);
1126
1127 return(GetSideIDFromScratchSpecialRule22Tet(theElement,theNode));
1128 }
1129
1130 INT NS_DIM_PREFIX GetSideIDFromScratch (ELEMENT *theElement, NODE *theNode)
1131 {
1132 ELEMENT *theFather;
1133 NODE *nd[MAX_EDGES_OF_ELEM];
1134 EDGE *edge;
1135 INT i,j,k,l,cnt;
1136
1137 ASSERT(NTYPE(theNode) == SIDE_NODE);
1138
1139 theFather = EFATHER(theElement);
1140
1141 /* determine midnodes of father */
1142 for (i=0; i<EDGES_OF_ELEM(theFather); i++)
1143 {
1144 edge = GetEdge(CORNER_OF_EDGE_PTR(theFather,i,0),
1145 CORNER_OF_EDGE_PTR(theFather,i,1));
1146 nd[i] = MIDNODE(edge);
1147 }
1148
1149 for (j=0; j<SIDES_OF_ELEM(theElement); j++)
1150 {
1151 if (3 == CORNERS_OF_SIDE(theElement,j)) continue;
1152
1153 for (l=0; l<CORNERS_OF_SIDE(theElement,j); l++)
1154 if (theNode == CORNER(theElement,CORNER_OF_SIDE(theElement,j,l)))
1155 break;
1156 if (l == CORNERS_OF_SIDE(theElement,j)) continue;
1157
1158 for (i=0; i<SIDES_OF_ELEM(theFather); i++)
1159 {
1160 #ifdef DUNE_UGGRID_DUNE_UGGRID_TET_RULESET
1161 if (3 == CORNERS_OF_SIDE(theFather,i)) continue;
1162 #endif
1163
1164 cnt = 0;
1165 for (k=0; k<EDGES_OF_SIDE(theFather,i); k++)
1166 for (l=0; l<CORNERS_OF_SIDE(theElement,j); l++)
1167 {
1168 if (nd[EDGE_OF_SIDE(theFather,i,k)] ==
1169 CORNER(theElement,CORNER_OF_SIDE(theElement,j,l)))
1170 cnt++;
1171 if (cnt == 2)
1172 return(i);
1173 }
1174 }
1175 }
1176
1177
1178 /* if side not found search over neighbor */
1179 for (j=0; j<SIDES_OF_ELEM(theElement); j++)
1180 {
1181 ELEMENT *nb = NBELEM(theElement,j);
1182
1183 if (3 == CORNERS_OF_SIDE(theElement,j))
1184 continue;
1185
1186 if (nb == NULL) continue;
1187
1188 for (l=0; l<CORNERS_OF_ELEM(nb); l++)
1189 if (theNode == CORNER(nb,l))
1190 return(GetSideIDFromScratch(nb,theNode));
1191 }
1192
1193
1194 for (j=0; j<SIDES_OF_ELEM(theElement); j++)
1195 {
1196 if (4 != CORNERS_OF_SIDE(theElement,j)) continue;
1197 for (l=0; l<4; l++)
1198 if (theNode == CORNER(theElement,CORNER_OF_SIDE(theElement,j,l)))
1199 break;
1200 if (l < 4)
1201 {
1202 INT l1 = (l+1) % 4;
1203
1204 for (i=0; i<SIDES_OF_ELEM(theFather); i++) {
1205 if (3 == CORNERS_OF_SIDE(theFather,i)) continue;
1206 for (k=0; k<EDGES_OF_SIDE(theFather,i); k++) {
1207 if (nd[EDGE_OF_SIDE(theFather,i,k)] ==
1208 CORNER(theElement,CORNER_OF_SIDE(theElement,j,l1)))
1209 return(i);
1210 if (nd[EDGE_OF_SIDE(theFather,i,k)] ==
1211 CORNER(theElement,CORNER_OF_SIDE(theElement,j,l1)))
1212 return(i);
1213 }
1214 }
1215 }
1216 }
1217
1218 return(GetSideIDFromScratchSpecialRule(theElement,theNode));
1219
1220 return(SIDES_OF_ELEM(theFather));
1221 }
1222
1223 INT GetSideIDFromScratchOld (ELEMENT *theElement, NODE *theNode)
1224 {
1225 ELEMENT *theFather = EFATHER(theElement);
1226 NODE *nd[MAX_EDGES_OF_ELEM];
1227
1228 ASSERT(NTYPE(theNode) == SIDE_NODE);
1229
1230 /* determine midnodes of father */
1231 for (INT i = 0; i < EDGES_OF_ELEM(theFather); i++)
1232 {
1233 EDGE *edge = GetEdge(CORNER_OF_EDGE_PTR(theFather,i,0),
1234 CORNER_OF_EDGE_PTR(theFather,i,1));
1235 nd[i] = MIDNODE(edge);
1236 }
1237
1238 for (INT j = 0; j < SIDES_OF_ELEM(theElement); j++)
1239 {
1240 if (3 == CORNERS_OF_SIDE(theElement,j)) continue;
1241
1242 INT l;
1243 for (l = 0; l < CORNERS_OF_SIDE(theElement,j); l++)
1244 if (theNode == CORNER(theElement,CORNER_OF_SIDE(theElement,j,l)))
1245 break;
1246 if (l == CORNERS_OF_SIDE(theElement,j)) continue;
1247
1248 for (INT i = 0; i < SIDES_OF_ELEM(theFather); i++)
1249 {
1250 if (3 == CORNERS_OF_SIDE(theFather,i)) continue;
1251
1252 INT cnt = 0;
1253 for (INT k = 0; k < EDGES_OF_SIDE(theFather,i); k++)
1254 for (INT n = 0; n < CORNERS_OF_SIDE(theElement,j); n++)
1255 {
1256 if (nd[EDGE_OF_SIDE(theFather,i,k)] ==
1257 CORNER(theElement,CORNER_OF_SIDE(theElement,j,n)))
1258 cnt++;
1259 if (cnt == 2)
1260 return(i);
1261 }
1262 }
1263 }
1264
1265
1266 /* if side not found search over neighbor */
1267 for (INT j = 0; j < SIDES_OF_ELEM(theElement); j++)
1268 {
1269 ELEMENT *nb = NBELEM(theElement,j);
1270
1271 if (3 == CORNERS_OF_SIDE(theElement,j))
1272 {
1273
1274 /* treatment of special green rule 17 and 22 */
1275 if (((TAG(theElement)==PYRAMID && NSONS(theFather)==9) ||
1276 (TAG(theElement)==TETRAHEDRON && NSONS(theFather)==11))
1277 && 2==CountSideNodes(theElement) &&
1278 TAG(theFather)==HEXAHEDRON &&
1279 ECLASS(theElement)==GREEN_CLASS)
1280 /* not continue */;
1281 else
1282 continue;
1283 }
1284
1285 if (nb == NULL) continue;
1286
1287 for (INT l = 0; l < CORNERS_OF_ELEM(nb); l++)
1288 if (theNode == CORNER(nb,l))
1289 return(GetSideIDFromScratch(nb,theNode));
1290 }
1291
1292
1293 for (INT j = 0; j < SIDES_OF_ELEM(theElement); j++)
1294 {
1295 if (4 != CORNERS_OF_SIDE(theElement,j)) continue;
1296 INT l;
1297 for (l = 0; l < 4; l++)
1298 if (theNode == CORNER(theElement,CORNER_OF_SIDE(theElement,j,l)))
1299 break;
1300 if (l < 4)
1301 {
1302 INT l1 = (l+1) % 4;
1303
1304 for (INT i = 0; i < SIDES_OF_ELEM(theFather); i++) {
1305 if (3 == CORNERS_OF_SIDE(theFather,i)) continue;
1306 for (INT k = 0; k < EDGES_OF_SIDE(theFather,i); k++) {
1307 if (nd[EDGE_OF_SIDE(theFather,i,k)] ==
1308 CORNER(theElement,CORNER_OF_SIDE(theElement,j,l1)))
1309 return(i);
1310 if (nd[EDGE_OF_SIDE(theFather,i,k)] ==
1311 CORNER(theElement,CORNER_OF_SIDE(theElement,j,l1)))
1312 return(i);
1313 }
1314 }
1315 }
1316 }
1317
1318 /* treatment of special green rule 17 and 22 */
1319 for (INT j = 0; j < SIDES_OF_ELEM(theElement); j++)
1320 {
1321 INT l;
1322 for (l=0; l<CORNERS_OF_SIDE(theElement,j); l++)
1323 if (theNode == CORNER(theElement,CORNER_OF_SIDE(theElement,j,l)))
1324 break;
1325 if (l == CORNERS_OF_SIDE(theElement,j)) continue;
1326
1327 for (INT i = 0; i < SIDES_OF_ELEM(theFather); i++)
1328 {
1329 if (3 == CORNERS_OF_SIDE(theFather,i)) continue;
1330
1331 INT cnt = 0;
1332 for (INT k = 0; k < EDGES_OF_SIDE(theFather,i); k++)
1333 for (INT n = 0; n < CORNERS_OF_SIDE(theElement,j); n++)
1334 {
1335 if (nd[EDGE_OF_SIDE(theFather,i,k)] ==
1336 CORNER(theElement,CORNER_OF_SIDE(theElement,j,n)))
1337 cnt++;
1338 if (cnt==1 && ECLASS(theElement)==GREEN_CLASS &&
1339 TAG(theElement)==TETRAHEDRON &&
1340 TAG(theFather)==HEXAHEDRON &&
1341 (NSONS(theFather)==9 || NSONS(theFather)==11))
1342 {
1343 return(i);
1344 }
1345
1346 }
1347 }
1348 }
1349
1350 UserWriteF("GetSideIDFromScratch(): e=" EID_FMTX " f=" EID_FMTX "\n",
1351 EID_PRTX(theElement),EID_PRTX(theFather));
1352 return(0);
1353 }
1354
1355 #endif /* UG_DIM_3 */
1356
1357
1358 /****************************************************************************/
1359 /** \brief Get the center node of an element of next finer level
1360 *
1361 * This function gets the center node of an element of next finer level
1362
1363 @return <ul>
1364 <li> pointer to center node </li>
1365 <li> NULL no node found </li>
1366 </ul> */
1367 /****************************************************************************/
1368
1369 NODE * NS_DIM_PREFIX GetCenterNode (const ELEMENT *theElement)
1370 {
1371 INT i,j;
1372 NODE *theNode;
1373 ELEMENT *SonList[MAX_SONS],*theSon;
1374
1375 #ifdef __CENTERNODE__
1376 return(CENTERNODE(theElement));
1377 #endif
1378
1379 theNode = NULL;
1380 if (GetAllSons(theElement,SonList) != GM_OK) assert(0);
1381
1382 for (i=0; i<MAX_SONS && SonList[i]!=NULL; i++)
1383 {
1384 theSon = SonList[i];
1385 for (j=0; j<CORNERS_OF_ELEM(theSon); j++)
1386 {
1387 theNode = CORNER(theSon,j);
1388 if (NTYPE(theNode) == CENTER_NODE)
1389 {
1390 if (EMASTER(theElement))
1391 assert(VFATHER(MYVERTEX(theNode)) == theElement);
1392 return (theNode);
1393 }
1394 }
1395 }
1396 return (NULL);
1397 }
1398
1399 /****************************************************************************/
1400 /** \brief Allocate a new node on a side of an element
1401 *
1402 * Includes vertex
1403 * best fit boundary coordinates and local coordinates.
1404 *
1405 * @return <ul>
1406 * <li> pointer to new node </li>
1407 * <li> NULL: could not allocate </li>
1408 </ul> */
1409 /****************************************************************************/
1410 /* #define MOVE_MIDNODE */
1411 NODE * NS_DIM_PREFIX CreateCenterNode (GRID *theGrid, ELEMENT *theElement, VERTEX *theVertex)
1412 {
1413 DOUBLE_VECTOR diff;
1414 INT n,j,moved,vertex_null;
1415 VERTEX *VertexOnEdge[MAX_EDGES_OF_ELEM];
1416 NODE *theNode;
1417 EDGE *theEdge;
1418 DOUBLE fac;
1419 DOUBLE *x[MAX_CORNERS_OF_ELEM];
1420 #ifdef MOVE_MIDNODE
1421 #ifndef ModelP
1422 DOUBLE len_opp,len_bnd;
1423 #endif
1424 #endif
1425
1426 /* check if moved side nodes exist */
1427 CORNER_COORDINATES(theElement,n,x);
1428 moved = 0;
1429 vertex_null = (theVertex==NULL);
1430 if (theVertex==NULL && OBJT(theElement) == BEOBJ) {
1431 for (j=0; j<EDGES_OF_ELEM(theElement); j++) {
1432 theEdge=GetEdge(CORNER(theElement,CORNER_OF_EDGE(theElement,j,0)),
1433 CORNER(theElement,CORNER_OF_EDGE(theElement,j,1)));
1434 ASSERT(theEdge != NULL);
1435 theNode = MIDNODE(theEdge);
1436 if (theNode == NULL)
1437 VertexOnEdge[j] = NULL;
1438 else {
1439 VertexOnEdge[j] = MYVERTEX(theNode);
1440 moved += MOVED(VertexOnEdge[j]);
1441 }
1442 }
1443 #ifdef MOVE_MIDNODE
1444 #ifndef ModelP
1445 if (moved == 1) {
1446 for (j=0; j<EDGES_OF_ELEM(theElement); j++)
1447 if (VertexOnEdge[j] != NULL)
1448 if (MOVED(VertexOnEdge[j])) break;
1449 theVertex = VertexOnEdge[OPPOSITE_EDGE(theElement,j)];
1450 if (theVertex != NULL) {
1451 V_DIM_LINCOMB(0.5,CVECT(MYVERTEX(CORNER(theElement,CORNER_OF_EDGE(theElement,j,0)))),
1452 0.5,CVECT(MYVERTEX(CORNER(theElement,CORNER_OF_EDGE(theElement,j,1)))),
1453 diff);
1454 V_DIM_LINCOMB(1.0,CVECT(VertexOnEdge[j]),-1.0,diff,diff);
1455 /* scale diff according to length of edges */
1456 V_DIM_EUKLIDNORM_OF_DIFF(
1457 CVECT(MYVERTEX(CORNER(theElement,CORNER_OF_EDGE(theElement,j,0)))),
1458 CVECT(MYVERTEX(CORNER(theElement,CORNER_OF_EDGE(theElement,j,1)))),len_bnd);
1459 V_DIM_EUKLIDNORM_OF_DIFF(
1460 CVECT(MYVERTEX(CORNER(theElement,
1461 CORNER_OF_EDGE(theElement,OPPOSITE_EDGE(theElement,j),0)))),
1462 CVECT(MYVERTEX(CORNER(theElement,
1463 CORNER_OF_EDGE(theElement,OPPOSITE_EDGE(theElement,j),1)))),
1464 len_opp);
1465 V_DIM_SCALE(len_opp/len_bnd,diff);
1466 DOUBLE* global = CVECT(theVertex);
1467 PRINTDEBUG(gm,1,("CreateCenterNode: global_orig = %f %f %f\n",global[0],global[1],global[2]));
1468 PRINTDEBUG(gm,1,("CreateCenterNode: diff = %f %f %f\n",diff[0],diff[1],diff[2]));
1469 V_DIM_LINCOMB(1.0,global,0.5,diff,global);
1470 PRINTDEBUG(gm,1,("CreateCenterNode: global_mod = %f %f %f\n",global[0],global[1],global[2]));
1471 SETMOVED(VertexOnEdge[OPPOSITE_EDGE(theElement,j)],1);
1472 UG_GlobalToLocal(n,(const DOUBLE **)x,global,LCVECT(theVertex));
1473 PRINTDEBUG(gm,1,("CreateCenterNode: local = %f %f %f\n",LCVECT(theVertex)[0],LCVECT(theVertex)[1],LCVECT(theVertex)[2]));
1474 SETONEDGE(theVertex,OPPOSITE_EDGE(theElement,j));
1475 VFATHER(theVertex) = theElement;
1476 }
1477 }
1478 #endif
1479 #endif
1480 }
1481
1482 if (vertex_null)
1483 {
1484 theVertex = CreateInnerVertex(theGrid);
1485 if (theVertex==NULL)
1486 return(NULL);
1487 VFATHER(theVertex) = theElement;
1488 }
1489
1490 theNode = CreateNode(theGrid,theVertex,(GEOM_OBJECT *)theElement,CENTER_NODE);
1491 if (theNode==NULL && vertex_null)
1492 {
1493 DisposeVertex(theGrid,theVertex);
1494 return(NULL);
1495 }
1496
1497 #ifdef TOPNODE
1498 if (TOPNODE(theVertex) == NULL || LEVEL(TOPNODE(theVertex))<LEVEL(theNode))
1499 TOPNODE(theVertex) = theNode;
1500 #endif
1501 theGrid->status |= 1;
1502
1503 if (!vertex_null) return(theNode);
1504
1505 FieldVector<DOUBLE,DIM>& global = CVECT(theVertex);
1506 FieldVector<DOUBLE,DIM>& local = LCVECT(theVertex);
1507 V_DIM_CLEAR(local);
1508 fac = 1.0 / n;
1509 for (j=0; j<n; j++)
1510 V_DIM_LINCOMB(1.0,local,
1511 fac,LOCAL_COORD_OF_ELEM(theElement,j),local);
1512 LOCAL_TO_GLOBAL(n,x,local,global);
1513 if (moved) {
1514 V_DIM_CLEAR(diff);
1515 for (j=0; j<EDGES_OF_ELEM(theElement); j++)
1516 if (VertexOnEdge[j] != NULL) {
1517 V_DIM_COPY(CVECT(VertexOnEdge[j]),diff);
1518 V_DIM_LINCOMB(1.0,diff,-0.5,CVECT(MYVERTEX(CORNER(theElement,CORNER_OF_EDGE(theElement,j,0)))),diff);
1519 V_DIM_LINCOMB(1.0,diff,-0.5,CVECT(MYVERTEX(CORNER(theElement,CORNER_OF_EDGE(theElement,j,1)))),diff);
1520 V_DIM_LINCOMB(0.5,diff,1.0,global,global);
1521 }
1522 UG_GlobalToLocal(n,(const DOUBLE **)x,global,local);
1523 LOCAL_TO_GLOBAL(n,x,local,diff);
1524 SETMOVED(theVertex,1);
1525 }
1526 return(theNode);
1527 }
1528
1529
1530 /****************************************************************************/
1531 /** \brief Get all nodes related to this element on next level
1532
1533 * @param theElement - element for context
1534 * @param theElementContext - node context of this element
1535
1536 This function returns the nodes related to the element on the next
1537 finer level. The ordering is according to the reference numbering.
1538
1539 @return <ul>
1540 <li> GM_OK if ok </li>
1541 <li> != GM_OK if not ok </li>
1542 </ul> */
1543 /****************************************************************************/
1544
1545 INT NS_DIM_PREFIX GetNodeContext (const ELEMENT *theElement, NODE **theElementContext)
1546 {
1547 NODE *theNode, **MidNodes, **CenterNode;
1548 EDGE *theEdge;
1549 INT i,Corner0, Corner1;
1550 #ifdef UG_DIM_3
1551 NODE **SideNodes;
1552 #endif
1553
1554 /* reset context */
1555 for(i=0; i<MAX_CORNERS_OF_ELEM+MAX_NEW_CORNERS_DIM; i++)
1556 theElementContext[i] = NULL;
1557
1558 /* is element to refine */
1559 if (!IS_REFINED(theElement)) return(GM_OK);
1560
1561 /* get corner nodes */
1562 for (i=0; i<CORNERS_OF_ELEM(theElement); i++)
1563 {
1564 theNode = CORNER(theElement,i);
1565 theElementContext[i] = SONNODE(theNode);
1566 }
1567
1568 /* check for midpoint nodes */
1569 MidNodes = theElementContext+CORNERS_OF_ELEM(theElement);
1570 for (i=0; i<EDGES_OF_ELEM(theElement); i++)
1571 {
1572 Corner0 = CORNER_OF_EDGE(theElement,i,0);
1573 Corner1 = CORNER_OF_EDGE(theElement,i,1);
1574
1575 theEdge = GetEdge(CORNER(theElement,Corner0),
1576 CORNER(theElement,Corner1));
1577 ASSERT(theEdge != NULL);
1578
1579 MidNodes[i] = MIDNODE(theEdge);
1580 }
1581
1582 #ifdef UG_DIM_3
1583 SideNodes = theElementContext+CORNERS_OF_ELEM(theElement)+
1584 EDGES_OF_ELEM(theElement);
1585 for (i=0; i<SIDES_OF_ELEM(theElement); i++)
1586 {
1587 #ifdef DUNE_UGGRID_DUNE_UGGRID_TET_RULESET
1588 /* no side nodes for triangular sides yet */
1589 if (CORNERS_OF_SIDE(theElement,i) == 3) continue;
1590 #endif
1591 /* check for side node */
1592 SideNodes[i] = GetSideNode(theElement,i);
1593 }
1594 #endif
1595
1596 /* check for center node */
1597 CenterNode = MidNodes+CENTER_NODE_INDEX(theElement);
1598 CenterNode[0] = GetCenterNode(theElement);
1599
1600 return(GM_OK);
1601 }
1602
1603 /****************************************************************************/
1604 /** \brief Return matching side of the neighboring element
1605
1606 * @param theNeighbor - element to test for matching side
1607 * @param nbside - the matching side
1608 * @param theElement - element with side to match
1609 * @param side - side of element to match
1610
1611 This function computes the matching side of neighboring element.
1612
1613 */
1614 /****************************************************************************/
1615
1616 void NS_DIM_PREFIX GetNbSideByNodes (ELEMENT *theNeighbor, INT *nbside, ELEMENT *theElement, INT side)
1617 {
1618 INT i,k,l,ec,nc;
1619
1620 ec = CORNERS_OF_SIDE(theElement,side);
1621
1622 for (i=0; i<SIDES_OF_ELEM(theNeighbor); i++)
1623 {
1624 nc = CORNERS_OF_SIDE(theNeighbor,i);
1625 if (ec != nc) continue;
1626
1627 for (k=0; k<nc; k++)
1628 if (CORNER_OF_SIDE_PTR(theElement,side,0) ==
1629 CORNER_OF_SIDE_PTR(theNeighbor,i,k))
1630 break;
1631 if (k == nc) continue;
1632
1633 for (l=1; l<ec; l++)
1634 {
1635 if (CORNER_OF_SIDE_PTR(theElement,side,l) !=
1636 CORNER_OF_SIDE_PTR(theNeighbor,i,(nc+k-l)%nc))
1637 break;
1638 }
1639 if (l == ec)
1640 {
1641 *nbside = i;
1642 return;
1643 }
1644 }
1645
1646 /* no side of the neighbor matches */
1647 assert(0);
1648 }
1649
1650 /****************************************************************************/
1651 /** \brief Return pointer to son edge if it exists
1652 *
1653 * @param theEdge edge for which son is searched
1654 *
1655 * This function returns the pointer to the son edge if it exists.
1656 *
1657 * @return <ul>
1658 <li> pointer to specified object </li>
1659 <li> NULL if not found </li>
1660 </ul> */
1661 /****************************************************************************/
1662
1663 EDGE * NS_DIM_PREFIX GetSonEdge (const EDGE *theEdge)
1664 {
1665 EDGE *SonEdge=NULL;
1666 NODE *Node0,*Node1,*SonNode0,*SonNode1;
1667
1668 Node0 = NBNODE(LINK0(theEdge));
1669 Node1 = NBNODE(LINK1(theEdge));
1670
1671 SonNode0 = SONNODE(Node0);
1672 SonNode1 = SONNODE(Node1);
1673
1674 if (SonNode0!=NULL && SonNode1!=NULL)
1675 SonEdge = GetEdge(SonNode0,SonNode1);
1676
1677 return(SonEdge);
1678 }
1679
1680
1681 /****************************************************************************/
1682 /** \brief Return pointer to son edges if it exists
1683
1684 * @param theEdge - edge for which son is searched
1685 * @param SonEdges - array of pointers will be filled with son edges
1686
1687 This function returns the pointer to the son edges if existing.
1688
1689 @return <ul>
1690 <li> number of found edges (0,1,2) </li>
1691 </ul> */
1692 /****************************************************************************/
1693
1694 INT NS_DIM_PREFIX GetSonEdges (const EDGE *theEdge, EDGE *SonEdges[MAX_SON_EDGES])
1695 {
1696 INT nedges;
1697 NODE *Node0,*Node1,*SonNode0,*SonNode1,*MidNode;
1698
1699 nedges = 0;
1700 SonEdges[0] = NULL;
1701 SonEdges[1] = NULL;
1702
1703 Node0 = NBNODE(LINK0(theEdge));
1704 Node1 = NBNODE(LINK1(theEdge));
1705
1706 if (GID(Node0)<GID(Node1))
1707 {
1708 SonNode0 = SONNODE(Node0);
1709 SonNode1 = SONNODE(Node1);
1710 }
1711 else
1712 {
1713 SonNode0 = SONNODE(Node1);
1714 SonNode1 = SONNODE(Node0);
1715 }
1716 MidNode = MIDNODE(theEdge);
1717
1718 /* parallel note: */
1719 /* since existence of MidNode decides whether for one SonEdge or */
1720 /* two half SonEdges is searched, the data structure must be */
1721 /* consistent in a way that if the MidNode exists also the */
1722 /* MIDNODE pointer is set to MidNode. (s.l. 980227) */
1723 if (MidNode == NULL)
1724 {
1725 if (SonNode0!=NULL && SonNode1!=NULL)
1726 SonEdges[0] = GetEdge(SonNode0,SonNode1);
1727 }
1728 else
1729 {
1730 if (SonNode0!=NULL)
1731 SonEdges[0] = GetEdge(SonNode0,MidNode);
1732
1733 if (SonNode1!=NULL)
1734 SonEdges[1] = GetEdge(MidNode,SonNode1);
1735 }
1736
1737 if (SonEdges[0] != NULL) nedges++;
1738 if (SonEdges[1] != NULL) nedges++;
1739
1740 return(nedges);
1741 }
1742
1743 /****************************************************************************/
1744 /** \brief Return pointer to father edge if it exists
1745
1746 * @param theEdge - edge for which father is searched
1747
1748 This function returns the pointer to the father edge if it exists.
1749
1750 @return <ul>
1751 <li> pointer to specified object </li>
1752 <li> NULL if not found </li>
1753 </ul> */
1754 /****************************************************************************/
1755
1756 EDGE * NS_DIM_PREFIX GetFatherEdge (const EDGE *theEdge)
1757 {
1758 NODE *theNode0 = NBNODE(LINK0(theEdge));
1759 NODE *theNode1 = NBNODE(LINK1(theEdge));
1760 EDGE *FatherEdge = NULL;
1761
1762 /* one node is center node -> no father edge */
1763 if (CENTERTYPE(theNode0) || CENTERTYPE(theNode1)) return(NULL);
1764
1765 #ifdef UG_DIM_3
1766 /* one node is side node -> no father edge */
1767 if (SIDETYPE(theNode0) || SIDETYPE(theNode1)) return(NULL);
1768 #endif
1769
1770 /* both nodes are mid nodes -> no father edge */
1771 if (MIDTYPE(theNode0) && MIDTYPE(theNode1)) return(NULL);
1772
1773 /* one node is mid node -> no father edge */
1774 if (MIDTYPE(theNode0) || MIDTYPE(theNode1))
1775 {
1776 NODE *FatherNode0,*FatherNode1, *theNode;
1777
1778 if (MIDTYPE(theNode1))
1779 {
1780 theNode = theNode0; theNode0 = theNode1; theNode1 = theNode;
1781 }
1782 FatherEdge = (EDGE *) NFATHER(theNode0);
1783 if (FatherEdge == NULL) return(NULL);
1784
1785 FatherNode0 = NBNODE(LINK0(FatherEdge));
1786 FatherNode1 = NBNODE(LINK1(FatherEdge));
1787 if (SONNODE(FatherNode0)==theNode1 || SONNODE(FatherNode1)==theNode1)
1788 return(FatherEdge);
1789 else
1790 return(NULL);
1791 }
1792
1793 /* both nodes are corner nodes -> try to get the edge */
1794 if (CORNERTYPE(theNode0) && CORNERTYPE(theNode1))
1795 {
1796 if (NFATHER(theNode0)!=NULL && NFATHER(theNode1)!=NULL)
1797 return(GetEdge(NFATHER(theNode0),NFATHER(theNode1)));
1798 else
1799 return(NULL);
1800 }
1801
1802 /* No father available */
1803 return NULL;
1804 }
1805
1806 #ifdef UG_DIM_3
1807
1808 /****************************************************************************/
1809 /** \brief Return pointer to father edge if it exists
1810
1811 * @param SideNodes - nodes of the side
1812 * @param ncorners - number of sidenodes
1813 * @param Nodes - corners of edge for which father is searched
1814 * @param theEdge - edge for which father is searched
1815
1816 This function returns the pointer to the father edge if it exists.
1817
1818 @return <ul>
1819 <li> pointer to specified object </li>
1820 <li> NULL if not found </li>
1821 </ul> */
1822 /****************************************************************************/
1823
1824 EDGE * NS_DIM_PREFIX FatherEdge (NODE **SideNodes, INT ncorners, NODE **Nodes, EDGE *theEdge)
1825 {
1826 INT pos0,pos1;
1827 EDGE *fatherEdge = NULL;
1828
1829 ASSERT(Nodes[0]!=NULL);
1830 ASSERT(Nodes[1]!=NULL);
1831
1832 /* one node is side node -> no father edge */
1833 if (NTYPE(Nodes[0])==SIDE_NODE || NTYPE(Nodes[1])==SIDE_NODE) return(NULL);
1834
1835 /* both nodes are side nodes -> no father edge */
1836 if (NTYPE(Nodes[0])==MID_NODE && NTYPE(Nodes[1])==MID_NODE) return(NULL);
1837
1838 for (pos0=0; pos0<MAX_SIDE_NODES; pos0++) {
1839 if (SideNodes[pos0] == Nodes[0])
1840 break;
1841 }
1842 ASSERT(pos0<MAX_SIDE_NODES);
1843
1844 for (pos1=0; pos1<MAX_SIDE_NODES; pos1++)
1845 if (SideNodes[pos1] == Nodes[1])
1846 break;
1847 ASSERT(pos1<MAX_SIDE_NODES);
1848
1849 switch (NTYPE(Nodes[0]))
1850 {
1851 case (CORNER_NODE) :
1852
1853 ASSERT(pos0<ncorners);
1854 if ( ((pos0+1)%ncorners == pos1) ||
1855 (pos0+ncorners == pos1) )
1856 {
1857 ASSERT(OBJT(NFATHER(SideNodes[(pos0+1)%ncorners])) == NDOBJ);
1858 fatherEdge = GetEdge((NODE *)NFATHER(Nodes[0]),
1859 (NODE *)NFATHER(SideNodes[(pos0+1)%ncorners]));
1860 ASSERT(fatherEdge!=NULL);
1861 }
1862
1863 if ( ((pos0-1+ncorners)%ncorners == pos1) ||
1864 ((pos0-1+ncorners)%ncorners+ncorners == pos1) )
1865 {
1866 ASSERT(OBJT(NFATHER(SideNodes[(pos0-1+ncorners)%ncorners])) == NDOBJ);
1867 fatherEdge = GetEdge((NODE *)NFATHER(Nodes[0]),
1868 (NODE *)NFATHER(SideNodes[(pos0-1+ncorners)%ncorners]));
1869 ASSERT(fatherEdge!=NULL);
1870 }
1871
1872 break;
1873
1874 case (MID_NODE) :
1875
1876 ASSERT(pos0>=ncorners);
1877 ASSERT(pos0<2*ncorners);
1878
1879 if ((pos0+1)%ncorners == pos1)
1880 {
1881 ASSERT(OBJT(NFATHER(SideNodes[pos0%ncorners])) == NDOBJ);
1882 fatherEdge = GetEdge((NODE *)NFATHER(SideNodes[pos0%ncorners]),
1883 (NODE *)NFATHER(Nodes[1]));
1884 ASSERT(fatherEdge!=NULL);
1885 }
1886
1887 if (pos0%ncorners == pos1)
1888 {
1889 ASSERT(OBJT(NFATHER(SideNodes[(pos0+1)%ncorners])) == NDOBJ);
1890 fatherEdge = GetEdge((NODE *)NFATHER(SideNodes[(pos0+1)%ncorners]),
1891 (NODE *)NFATHER(Nodes[1]));
1892 ASSERT(fatherEdge!=NULL);
1893 }
1894
1895 break;
1896
1897 case (SIDE_NODE) :
1898
1899 /* this edge has no father edge */
1900 fatherEdge = NULL;
1901 break;
1902
1903 default :
1904 assert(0);
1905 break;
1906 }
1907
1908 IFDEBUG(dddif,1)
1909 INT i;
1910 EDGE* edge0, *edge1;
1911
1912 edge0 = edge1 = NULL;
1913
1914 /* test whether theEdge lies above fatherEdge */
1915 if (fatherEdge!=NULL)
1916 {
1917 if (MIDNODE(fatherEdge)!=NULL)
1918 {
1919 edge0 = GetEdge(MIDNODE(fatherEdge),SONNODE(NBNODE(LINK0(fatherEdge))));
1920 edge1 = GetEdge(MIDNODE(fatherEdge),SONNODE(NBNODE(LINK1(fatherEdge))));
1921 }
1922 else
1923 edge0 = GetEdge(SONNODE(NBNODE(LINK0(fatherEdge))),
1924 SONNODE(NBNODE(LINK1(fatherEdge))));
1925
1926 IFDEBUG(dddif,5)
1927 UserWriteF("fatherEdge=%x theEdge=%x edge0=%x edge1=%x\n",
1928 fatherEdge,theEdge,edge0,edge1);
1929 UserWriteF("Nodes[0]=%d Nodes[1]=%d\n",ID(Nodes[0]),ID(Nodes[1]));
1930
1931 UserWriteF("SideNodes\n");
1932 for (i=0; i<MAX_SIDE_NODES; i++) UserWriteF(" %5d",i);
1933 UserWriteF("\n");
1934 for (i=0; i<MAX_SIDE_NODES; i++)
1935 if (SideNodes[i]!=NULL) UserWriteF(" %5d",ID(SideNodes[i]));
1936 UserWriteF("\n");
1937 ENDDEBUG
1938
1939 assert(edge0==theEdge || edge1==theEdge);
1940 }
1941 ENDDEBUG
1942
1943 return(fatherEdge);
1944 }
1945 #endif
1946
1947 /****************************************************************************/
1948 /** \brief Return pointer to edge if it exists
1949
1950 * @param from - starting node of edge
1951 * @param to - end node of edge
1952
1953 This function returns the pointer to the specified edge if it exists.
1954
1955 @return <ul>
1956 <li> pointer to specified object </li>
1957 <li> NULL if not found </li>
1958 </ul> */
1959 /****************************************************************************/
1960
1961 EDGE * NS_DIM_PREFIX GetEdge (const NODE *from, const NODE *to)
1962 {
1963 LINK *pl;
1964
1965 /* run through neighbor list */
1966 for (pl=START(from); pl!=NULL; pl = NEXT(pl))
1967 if (NBNODE(pl)==to)
1968 return(MYEDGE(pl));
1969
1970 /* return not found */
1971 return(NULL);
1972 }
1973
1974 /****************************************************************************/
1975 /** \brief Return pointer to a new edge structure
1976
1977 * @param theGrid - grid where vertex should be inserted
1978 * @param theElement - pointer to element
1979 * @param edge - number of edge
1980 * @param with_vector - also create vector for edge (true/false)
1981
1982 This function returns a pointer to a new edge structure.
1983
1984 @return <ul>
1985 <li> pointer to requested object </li>
1986 <li> NULL if out of memory </li>
1987 </ul> */
1988 /****************************************************************************/
1989
1990 #ifndef ModelP
1991 static
1992 #endif
1993 EDGE *
1994 #ifdef ModelP
1995 NS_DIM_PREFIX
1996 #endif
1997 CreateEdge (GRID *theGrid, ELEMENT *theElement, INT edge, bool with_vector)
1998 {
1999 ELEMENT *theFather;
2000 EDGE *pe,*father_edge;
2001 NODE *from,*to,*n1,*n2;
2002 LINK *link0,*link1;
2003 #ifdef UG_DIM_3
2004 VERTEX *theVertex;
2005 NODE *nbn1,*nbn2,*nbn3,*nbn4;
2006 INT sc,found,side,k,j;
2007 #endif
2008
2009 from = CORNER(theElement,CORNER_OF_EDGE(theElement,edge,0));
2010 to = CORNER(theElement,CORNER_OF_EDGE(theElement,edge,1));
2011
2012 /* check if edge exists already */
2013 if( (pe = GetEdge(from, to)) != NULL ) {
2014 if (NO_OF_ELEM(pe)<NO_OF_ELEM_MAX-1)
2015 INC_NO_OF_ELEM(pe);
2016 else
2017 ASSERT(0);
2018
2019 return(pe);
2020 }
2021
2022 pe = (EDGE*)GetMemoryForObject(theGrid->mg,sizeof(EDGE)-sizeof(VECTOR*),EDOBJ);
2023 if (pe==NULL) return(NULL);
2024
2025 /* initialize data */
2026 link0 = LINK0(pe);
2027 link1 = LINK1(pe);
2028 SETOBJT(pe,EDOBJ);
2029 SETLOFFSET(link0,0);
2030 SETLOFFSET(link1,1);
2031
2032 pe->id = (theGrid->mg->edgeIdCounter)++;
2033
2034 SETLEVEL(pe,GLEVEL(theGrid));
2035 #ifdef ModelP
2036 DDD_AttrSet(PARHDR(pe), GRID_ATTR(theGrid));
2037 /* SETPRIO(pe,PrioMaster); */
2038 #endif
2039 #ifdef IDENT_ONLY_NEW
2040 if (GET_IDENT_MODE() == IDENT_ON)
2041 SETNEW_EDIDENT(pe,1);
2042 #endif
2043
2044 UGM_CDBG(pe,
2045 UserWriteF(PFMT "create edge=" EDID_FMTX " from=" ID_FMTX "tf=%d to=" ID_FMTX "tt=%d"
2046 "elem=" EID_FMTX "edge=%d\n",
2047 theGrid->ppifContext().me(),EDID_PRTX(pe),ID_PRTX(from),NTYPE(from),ID_PRTX(to),NTYPE(to),
2048 EID_PRTX(theElement),edge);
2049 if (0)
2050 UserWriteF(PFMT "nfatherf=" ID_FMTX "nfathert=" ID_FMTX " fatheredge=" EDID_FMTX "\n",
2051 theGrid->ppifContext().me(),ID_PRTX((NODE*)NFATHER(from)),ID_PRTX((NODE*)NFATHER(to)),
2052 EDID_PRTX(GetEdge((NODE*)NFATHER(from),(NODE*)NFATHER(to))));)
2053
2054 NBNODE(link0) = to;
2055 NBNODE(link1) = from;
2056 SET_NO_OF_ELEM(pe,1);
2057 SETEDGENEW(pe,1);
2058
2059 /* set edge-subdomain from topological information with respect to father-element */
2060 SETEDSUBDOM(pe,SUBDOMAIN(theElement));
2061 theFather = EFATHER(theElement);
2062 if (theFather!=NULL)
2063 {
2064 SETEDSUBDOM(pe,SUBDOMAIN(theFather));
2065 if (NTYPE(from)<NTYPE(to))
2066 {
2067 n1 = from;
2068 n2 = to;
2069 }
2070 else
2071 {
2072 n1 = to;
2073 n2 = from;
2074 }
2075 switch(NTYPE(n1)|(NTYPE(n2)<<4))
2076 {
2077 #ifdef UG_DIM_2
2078 case (CORNER_NODE | (CORNER_NODE<<4)) :
2079 father_edge = GetEdge(NFATHER(n1),NFATHER(n2));
2080 if (father_edge!=NULL) SETEDSUBDOM(pe,EDSUBDOM(father_edge));
2081 break;
2082 case (CORNER_NODE | (MID_NODE<<4)) :
2083 father_edge = NFATHEREDGE(n2);
2084 #ifdef ModelP
2085 if (father_edge==NULL)
2086 {
2087 /* TODO: check this after priority set:
2088 assert( GHOST(n1) || GHOST(n2) ); */
2089 break;
2090 }
2091 #endif
2092 assert(father_edge!=NULL);
2093 if (NBNODE(LINK0(father_edge))==NFATHER(n1) || NBNODE(LINK1(father_edge))==NFATHER(n1)) SETEDSUBDOM(pe,EDSUBDOM(father_edge));
2094 break;
2095 #endif
2096 #ifdef UG_DIM_3
2097 case (CORNER_NODE | (CORNER_NODE<<4)) :
2098 father_edge = GetEdge(NFATHER(n1),NFATHER(n2));
2099 if (father_edge!=NULL) SETEDSUBDOM(pe,EDSUBDOM(father_edge));
2100 else
2101 {
2102 /* do fathers of n1, n2 lies on a side (of the father) which has BNDS? */
2103 for (j=0; j<SIDES_OF_ELEM(theFather); j++)
2104 {
2105 found=0;
2106 for (k=0; k<CORNERS_OF_SIDE(theFather,j); k++)
2107 {
2108 sc = CORNER_OF_SIDE(theFather,j,k);
2109 if (CORNER(theFather,sc)==NFATHER(n1) || CORNER(theFather,sc)==NFATHER(n2)) found++;
2110 }
2111 if (found==2 && (OBJT(theFather)==BEOBJ) && SIDE_ON_BND(theFather,j))
2112 {
2113 SETEDSUBDOM(pe,0);
2114 break;
2115 }
2116 }
2117 }
2118 break;
2119
2120 case (CORNER_NODE | (MID_NODE<<4)) :
2121 father_edge = NFATHEREDGE(n2);
2122 assert(father_edge!=NULL);
2123 nbn1 = NBNODE(LINK0(father_edge));
2124 nbn2 = NBNODE(LINK1(father_edge));
2125 if (nbn1==NFATHER(n1) || nbn2==NFATHER(n1)) SETEDSUBDOM(pe,EDSUBDOM(father_edge));
2126 else
2127 {
2128 /* do all nodes n1, nbn1, nbn2 ly on the same side of father? */
2129 side=-1;
2130 for (j=0; j<SIDES_OF_ELEM(theFather); j++)
2131 {
2132 found=0;
2133 for (k=0; k<CORNERS_OF_SIDE(theFather,j); k++)
2134 {
2135 sc = CORNER_OF_SIDE(theFather,j,k);
2136 if (CORNER(theFather,sc)==NFATHER(n1) || CORNER(theFather,sc)==nbn1 || CORNER(theFather,sc)==nbn2) found++;
2137 }
2138 if (found==3)
2139 {
2140 side = j;
2141 break;
2142 }
2143 }
2144 if (side>=0 && (OBJT(theFather)==BEOBJ) && SIDE_ON_BND(theFather,side)) SETEDSUBDOM(pe,0);
2145 }
2146 break;
2147
2148 case (MID_NODE | (MID_NODE<<4)) :
2149 father_edge = NFATHEREDGE(n1);
2150 assert(father_edge!=NULL);
2151 nbn1 = NBNODE(LINK0(father_edge));
2152 nbn2 = NBNODE(LINK1(father_edge));
2153 father_edge = NFATHEREDGE(n2);
2154 assert(father_edge!=NULL);
2155 nbn3 = NBNODE(LINK0(father_edge));
2156 nbn4 = NBNODE(LINK1(father_edge));
2157
2158 /* do all nodes nbn1, nbn2, nbn3, nbn4 ly on the same side of father? */
2159 side=-1;
2160 for (j=0; j<SIDES_OF_ELEM(theFather); j++)
2161 {
2162 found=0;
2163 for (k=0; k<CORNERS_OF_SIDE(theFather,j); k++)
2164 {
2165 sc = CORNER_OF_SIDE(theFather,j,k);
2166 if (CORNER(theFather,sc)==nbn1) found++;
2167 if (CORNER(theFather,sc)==nbn2) found++;
2168 if (CORNER(theFather,sc)==nbn3) found++;
2169 if (CORNER(theFather,sc)==nbn4) found++;
2170 }
2171 if (found==4)
2172 {
2173 side = j;
2174 break;
2175 }
2176 }
2177 if (side>=0 && (OBJT(theFather)==BEOBJ) && SIDE_ON_BND(theFather,side)) SETEDSUBDOM(pe,0);
2178 break;
2179
2180 case (CORNER_NODE | (SIDE_NODE<<4)) :
2181 theVertex = MYVERTEX(n2);
2182 if (VFATHER(theVertex) == theFather)
2183 side = ONSIDE(theVertex);
2184 else
2185 side = ONNBSIDE(theVertex);
2186 if ((OBJT(theFather)==BEOBJ) && SIDE_ON_BND(theFather,side))
2187 for (k=0; k<CORNERS_OF_SIDE(theFather,side); k++)
2188 if (CORNER(theFather,CORNER_OF_SIDE(theFather,side,k))==NFATHER(n1))
2189 {
2190 SETEDSUBDOM(pe,0);
2191 break;
2192 }
2193 break;
2194
2195 case (MID_NODE | (SIDE_NODE<<4)) :
2196 theVertex = MYVERTEX(n2);
2197 if (VFATHER(theVertex) == theFather)
2198 side = ONSIDE(theVertex);
2199 else
2200 side = ONNBSIDE(theVertex);
2201 if ((OBJT(theFather)==BEOBJ) && SIDE_ON_BND(theFather,side))
2202 {
2203 found=0;
2204 father_edge = NFATHEREDGE(n1);
2205 assert(father_edge!=NULL);
2206 nbn1 = NBNODE(LINK0(father_edge));
2207 nbn2 = NBNODE(LINK1(father_edge));
2208 for (k=0; k<CORNERS_OF_SIDE(theFather,side); k++)
2209 {
2210 if (CORNER(theFather,CORNER_OF_SIDE(theFather,side,k))==nbn1 || CORNER(theFather,CORNER_OF_SIDE(theFather,side,k))==nbn2)
2211 found++;
2212 }
2213 if (found==2) SETEDSUBDOM(pe,0);
2214 }
2215 break;
2216 #endif
2217 } /* end switch */
2218 } /* end (theFather!=NULL) */
2219
2220 /* put in neighbor lists */
2221 NEXT(link0) = START(from);
2222 START(from) = link0;
2223 NEXT(link1) = START(to);
2224 START(to) = link1;
2225
2226 /* counters */
2227 NE(theGrid)++;
2228
2229 /* return ok */
2230 return(pe);
2231 }
2232
2233 /****************************************************************************/
2234 /** \brief Return pointer to link if it exists
2235
2236 * @param from - starting node of link
2237 * @param to - end node of link
2238
2239 This function returns the pointer to the specified link if it exists.
2240
2241 @return <ul>
2242 <li> pointer to specified link </li>
2243 <li> NULL if not found. </li>
2244 </ul> */
2245 /****************************************************************************/
2246
2247 LINK *GetLink (const NODE *from, const NODE *to)
2248 {
2249 LINK *pl;
2250
2251 /* run through neighbor list */
2252 for (pl=START(from); pl!=NULL; pl = NEXT(pl))
2253 if (NBNODE(pl)==to)
2254 return(pl);
2255
2256 /* return not found */
2257 return(NULL);
2258 }
2259
2260 /****************************************************************************/
2261 /** \brief Return a pointer to a new element structure
2262
2263 * @param theGrid - grid structure to extend
2264 * @param tag - the element type
2265 * @param objtype - inner element (IEOBJ) or boundary element (BEOBJ)
2266 * @param nodes - list of corner nodes in reference numbering
2267 * @param Father - pointer to father element (NULL on base level)
2268 * @param with_vector -
2269
2270 This function creates and initializes a new element and returns a pointer to it.
2271
2272 @return <ul>
2273 <li> pointer to requested object </li>
2274 <li> NULL if out of memory </li>
2275 </ul> */
2276 /****************************************************************************/
2277
2278 ELEMENT * NS_DIM_PREFIX CreateElement (GRID *theGrid, INT tag, INT objtype, NODE **nodes,
2279 ELEMENT *Father, bool with_vector)
2280 {
2281 ELEMENT *pe;
2282 INT i,s_id;
2283
2284 if (objtype == IEOBJ)
2285 pe = (ELEMENT*)GetMemoryForObject(MYMG(theGrid),INNER_SIZE_TAG(tag),
2286 MAPPED_INNER_OBJT_TAG(tag));
2287 else if (objtype == BEOBJ)
2288 pe = (ELEMENT*)GetMemoryForObject(MYMG(theGrid),BND_SIZE_TAG(tag),
2289 MAPPED_BND_OBJT_TAG(tag));
2290 else
2291 std::abort();
2292
2293 if (pe==NULL) return(NULL);
2294
2295 /* initialize data */
2296 SETNEWEL(pe,1);
2297 SETOBJT(pe,objtype);
2298 SETTAG(pe,tag);
2299 SETLEVEL(pe,theGrid->level);
2300 #ifdef ModelP
2301 DDD_AttrSet(PARHDRE(pe),GRID_ATTR(theGrid));
2302 /* SETEPRIO(theGrid->dddContext(), pe,PrioMaster); */
2303 PARTITION(pe) = theGrid->ppifContext().me();
2304 #endif
2305 ID(pe) = (theGrid->mg->elemIdCounter)++;
2306
2307 /* subdomain id */
2308 s_id = (Father != NULL) ? SUBDOMAIN(Father) : 0;
2309 SETSUBDOMAIN(pe,s_id);
2310
2311 #ifdef __CENTERNODE__
2312 SET_CENTERNODE(pe,NULL);
2313 #endif
2314
2315 SET_EFATHER(pe,Father);
2316
2317 /* set corner nodes */
2318 for (i=0; i<CORNERS_OF_ELEM(pe); i++)
2319 SET_CORNER(pe,i,nodes[i]);
2320
2321 /* create edges */
2322 for (i=0; i<EDGES_OF_ELEM(pe); i++)
2323 if (CreateEdge (theGrid,pe,i,with_vector) == NULL) {
2324 DisposeElement(theGrid,pe);
2325 return(NULL);
2326 }
2327
2328 UGM_CDBG(pe,
2329 UserWriteF(PFMT "create elem=" EID_FMTX,
2330 theGrid->ppifContext().me(),EID_PRTX(pe));
2331 for (i=0; i<CORNERS_OF_ELEM(pe); i++)
2332 UserWriteF(" n%d=" ID_FMTX, i,ID_PRTX(CORNER(pe,i)));
2333 UserWriteF("\n");
2334 for (i=0; i<EDGES_OF_ELEM(pe); i++)
2335 {
2336 EDGE *theEdge;
2337
2338 theEdge = GetEdge(CORNER_OF_EDGE_PTR(pe,i,0),
2339 CORNER_OF_EDGE_PTR(pe,i,1));
2340 UserWriteF(" e%d=" EDID_FMTX, i,EDID_PRTX(theEdge));
2341 }
2342 UserWriteF("\n");)
2343
2344
2345 /* create side vectors if */
2346 #ifdef UG_DIM_3
2347 for (i=0; i<SIDES_OF_ELEM(pe); i++)
2348 if (with_vector)
2349 {
2350 VECTOR *pv;
2351 if (CreateSideVector (theGrid,i,(GEOM_OBJECT *)pe,&pv))
2352 {
2353 DisposeElement(theGrid,pe);
2354 return (NULL);
2355 }
2356 SET_SVECTOR(pe,i,pv);
2357 }
2358 else
2359 SET_SVECTOR(pe,i,NULL);
2360 #endif
2361
2362 /* insert in element list */
2363 GRID_LINK_ELEMENT(theGrid,pe,PrioMaster);
2364
2365 if (theGrid->level>0)
2366 {
2367 INT where = PRIO2INDEX(PrioMaster);
2368
2369 #ifndef ModelP
2370 ASSERT(Father != NULL);
2371 #endif
2372 if (Father != NULL)
2373 {
2374 if (SON(Father,where) == NULL)
2375 SET_SON(Father,where,pe);
2376 SETNSONS(Father,NSONS(Father)+1);
2377 }
2378 }
2379
2380 /* return ok */
2381 return(pe);
2382 }
2383
2384 /****************************************************************************/
2385 /** \brief Creates the element sides of son elements
2386
2387 * @param theGrid - grid for which to create
2388 * @param theElement - pointer to a boundary element
2389 * @param side - side id of a side of the element
2390 * @param theSon - pointer to a son element
2391 * @param son_side - side id of a side of the son
2392
2393 This function creates and initializes an element side of a son element.
2394 Here also the side vector (iff at all) is inspected in 'ReinspectSonSideVector'.
2395 The latter function eventually reallocates the vector if its size has changed.
2396
2397 @return <ul>
2398 <li> GM_OK if ok </li>
2399 <li> GM_ERROR when error occurred. </li>
2400 </ul> */
2401 /****************************************************************************/
2402
2403 INT NS_DIM_PREFIX CreateSonElementSide (GRID *theGrid, ELEMENT *theElement, INT side,
2404 ELEMENT *theSon, INT son_side)
2405 {
2406 INT n,i;
2407 BNDS *bnds;
2408 BNDP *bndp[MAX_CORNERS_OF_ELEM];
2409
2410 ASSERT (OBJT(theElement) == BEOBJ);
2411
2412 ASSERT (ELEM_BNDS(theElement,side) != NULL);
2413
2414 /* check if Edges of theElement, which are on the side 'side' have EDSUBDOM 0 */
2415 n = CORNERS_OF_SIDE(theElement,side);
2416 for (i=0; i<n; i++)
2417 {
2418 const EDGE* theEdge = GetEdge(CORNER(theElement,CORNER_OF_SIDE(theElement,side,i)),CORNER(theElement,CORNER_OF_SIDE(theElement,side,(i+1)%n)));
2419 assert(EDSUBDOM(theEdge)==0);
2420 }
2421
2422 n = CORNERS_OF_SIDE(theSon,son_side);
2423 for (i=0; i<n; i++)
2424 {
2425 /* check if vertices of Son lie on boundary */
2426 if (OBJT(MYVERTEX(CORNER(theSon,CORNER_OF_SIDE(theSon,son_side,i))))!=BVOBJ)
2427 {
2428 NODE *theNode;
2429 EDGE *theFatherEdge;
2430 INT t1,t2;
2431
2432 theNode = CORNER(theSon,CORNER_OF_SIDE(theSon,son_side,i));
2433 printf("ID=%d\n",(int)ID(theNode));
2434 switch (NTYPE(theNode))
2435 {
2436 case CORNER_NODE :
2437 printf("NTYPE = CORNER_NODE");
2438 break;
2439 case MID_NODE :
2440 printf(PFMT "el " EID_FMTX " son " EID_FMTX " vertex " VID_FMTX "\n",theGrid->ppifContext().me(),EID_PRTX(theElement),EID_PRTX(theSon),VID_PRTX(MYVERTEX(CORNER(theSon,CORNER_OF_SIDE(theSon,son_side,i)))));
2441 printf(PFMT "NTYPE = MID_NODE\n",theGrid->ppifContext().me());
2442 theFatherEdge = NFATHEREDGE(theNode);
2443 printf(PFMT "EDSUBDOM = %d\n",theGrid->ppifContext().me(),(int)EDSUBDOM(theFatherEdge));
2444 t1 = (OBJT(MYVERTEX(NBNODE(LINK0(theFatherEdge))))==BVOBJ);
2445 t2 = (OBJT(MYVERTEX(NBNODE(LINK1(theFatherEdge))))==BVOBJ);
2446 printf(PFMT "BVOBJ(theFatherEdge): %d %d\n",theGrid->ppifContext().me(),(int)t1,(int)t2);
2447 break;
2448 case SIDE_NODE :
2449 printf("NTYPE = SIDE_NODE");
2450 break;
2451 case CENTER_NODE :
2452 printf("NTYPE = CENTER_NODE");
2453 break;
2454 }
2455 ASSERT(0);
2456 }
2457 bndp[i] = V_BNDP(MYVERTEX(CORNER(theSon,CORNER_OF_SIDE(theSon,son_side,i))));
2458 }
2459 bnds = BNDP_CreateBndS(MGHEAP(MYMG(theGrid)),bndp,n);
2460 if (bnds == NULL)
2461 RETURN(GM_ERROR);
2462 SET_BNDS(theSon,son_side,bnds);
2463
2464 #ifdef UG_DIM_2
2465 const EDGE* theEdge = GetEdge(CORNER(theSon,CORNER_OF_EDGE(theSon,son_side,0)),
2466 CORNER(theSon,CORNER_OF_EDGE(theSon,son_side,1)));
2467 ASSERT(theEdge != NULL);
2468 SETEDSUBDOM(theEdge,0);
2469 #endif
2470
2471 #ifdef UG_DIM_3
2472 /** \todo is this necessary?
2473 for (i=0; i<EDGES_OF_SIDE(theSon,son_side); i++) {
2474 int k = EDGE_OF_SIDE(theSon,son_side,i);
2475 theEdge = GetEdge(CORNER(theSon,CORNER_OF_EDGE(theSon,k,0)),
2476 CORNER(theSon,CORNER_OF_EDGE(theSon,k,1)));
2477 ASSERT(theEdge != NULL);
2478 SETEDSUBDOM(theEdge,0);
2479 } */
2480 #endif
2481
2482 return(GM_OK);
2483 }
2484
2485 /****************************************************************************/
2486 /** \brief Return pointer to new grid structure
2487
2488 * @param theMG - multigrid structure
2489
2490 This function creates and initialized a new grid structure for top level + 1
2491 and returns a pointer to it.
2492
2493 @return <ul>
2494 <li> pointer to requested object </li>
2495 <li> NULL if out of memory </li>
2496 </ul> */
2497 /****************************************************************************/
2498
2499 GRID * NS_DIM_PREFIX CreateNewLevel (MULTIGRID *theMG)
2500 {
2501 GRID *theGrid;
2502
2503 if (TOPLEVEL(theMG)+1>=MAXLEVEL) return(NULL);
2504 INT l = TOPLEVEL(theMG)+1;
2505
2506 /* allocate grid object */
2507 theGrid = (GRID*)GetMemoryForObject(theMG,sizeof(GRID),GROBJ);
2508 if (theGrid==NULL) return(NULL);
2509
2510 /* fill in data */
2511 CTRL(theGrid) = 0;
2512 SETOBJT(theGrid,GROBJ);
2513 GLEVEL(theGrid) = l;
2514 NE(theGrid) = 0;
2515 /* other counters are init in INIT fcts below */
2516
2517 GSTATUS(theGrid,0);
2518 GRID_INIT_ELEMENT_LIST(theGrid);
2519 GRID_INIT_NODE_LIST(theGrid);
2520 GRID_INIT_VERTEX_LIST(theGrid);
2521 GRID_INIT_VECTOR_LIST(theGrid);
2522 if (l>0)
2523 {
2524 DOWNGRID(theGrid) = GRID_ON_LEVEL(theMG,l-1);
2525 UPGRID(GRID_ON_LEVEL(theMG,l-1)) = theGrid;
2526 UPGRID(theGrid) = NULL;
2527 }
2528 else if (l==0)
2529 {
2530 DOWNGRID(theGrid) = NULL;
2531 UPGRID(theGrid) = NULL;
2532 }
2533 else
2534 {
2535 UPGRID(theGrid) = GRID_ON_LEVEL(theMG,l+1);
2536 DOWNGRID(theGrid) = NULL;
2537 DOWNGRID(GRID_ON_LEVEL(theMG,l+1)) = theGrid;
2538 }
2539 MYMG(theGrid) = theMG;
2540 GRID_ON_LEVEL(theMG,l) = theGrid;
2541 TOPLEVEL(theMG) = l;
2542
2543 return(theGrid);
2544 }
2545
2546
2547 /****************************************************************************/
2548 /** \brief Create a multigrid environment item
2549
2550 * @param name - name of the multigrid
2551
2552 This function creates a multigrid environment directory.
2553
2554 @return <ul>
2555 <li> pointer to new MULTIGRID </li>
2556 <li> NULL if error occurred </li>
2557 </ul> */
2558 /****************************************************************************/
2559
2560 MULTIGRID * NS_DIM_PREFIX MakeMGItem (const char *name, std::shared_ptr<PPIF::PPIFContext> ppifContext)
2561 {
2562 MULTIGRID *theMG;
2563
2564 if (ChangeEnvDir("/Multigrids") == NULL) return (NULL);
2565 if (strlen(name)>=NAMESIZE || strlen(name)<=1) return (NULL);
2566 theMG = (MULTIGRID *) MakeEnvItem(name,theMGDirID,sizeof(MULTIGRID));
2567 if (theMG == NULL) return(NULL);
2568
2569 new(theMG) multigrid;
2570
2571 #if ModelP
2572 theMG->ppifContext_ = ppifContext;
2573 theMG->dddContext_ = std::make_shared<DDD::DDDContext>(
2574 theMG->ppifContext_,
2575 std::make_shared<DDD_CTRL>()
2576 );
2577
2578 InitDDD(theMG->dddContext());
2579
2580 globalDDDContext(theMG->dddContext_);
2581 #else
2582 theMG->ppifContext_ = std::make_shared<PPIF::PPIFContext>();
2583 #endif
2584
2585 return (theMG);
2586 }
2587
2588 /****************************************************************************/
2589 /** \todo Please doc me!
2590
2591 * @param theMG
2592 * @param FromLevel
2593 * @param ToLevel
2594 * @param mask
2595
2596 DESCRIPTION:
2597
2598 @return
2599 INT
2600 */
2601 /****************************************************************************/
2602
2603 INT NS_DIM_PREFIX ClearMultiGridUsedFlags (MULTIGRID *theMG, INT FromLevel, INT ToLevel, INT mask)
2604 {
2605 int i,level,elem,node,edge,vertex,vector;
2606 GRID *theGrid;
2607 ELEMENT *theElement;
2608 NODE *theNode;
2609 EDGE *theEdge;
2610 VECTOR *theVector;
2611
2612 elem = mask & MG_ELEMUSED;
2613 node = mask & MG_NODEUSED;
2614 edge = mask & MG_EDGEUSED;
2615 vertex = mask & MG_VERTEXUSED;
2616 vector = mask & MG_VECTORUSED;
2617
2618 for (level=FromLevel; level<=ToLevel; level++)
2619 {
2620 theGrid = GRID_ON_LEVEL(theMG,level);
2621 if (elem || edge)
2622 for (theElement=PFIRSTELEMENT(theGrid); theElement!=NULL; theElement=SUCCE(theElement))
2623 {
2624 if (elem) SETUSED(theElement,0);
2625 if (edge)
2626 {
2627 for (i=0; i<EDGES_OF_ELEM(theElement); i++)
2628 {
2629 theEdge = GetEdge(CORNER_OF_EDGE_PTR(theElement,i,0),
2630 CORNER_OF_EDGE_PTR(theElement,i,1));
2631 SETUSED(theEdge,0);
2632 }
2633 }
2634 }
2635 if (node || vertex)
2636 {
2637 for (theNode=PFIRSTNODE(theGrid); theNode!=NULL; theNode=SUCCN(theNode))
2638 {
2639 if (node) SETUSED(theNode,0);
2640 if (vertex) SETUSED(MYVERTEX(theNode),0);
2641 }
2642 }
2643 if (vector)
2644 {
2645 for (theVector=PFIRSTVECTOR(theGrid); theVector!=NULL; theVector=SUCCVC(theVector))
2646 {
2647 if (vector) SETUSED(theVector,0);
2648 }
2649 }
2650
2651 }
2652
2653 return(GM_OK);
2654 }
2655
2656
2657 /****************************************************************************/
2658 /** \brief Find the multigrid environment item with name
2659
2660 * @param name - name of the multigrid to find
2661
2662 This function find the multigrid environment item with `name` and
2663 returns a pointer to the multigrid structure.
2664
2665 @return <ul>
2666 <li> pointer to MULTIGRID </li>
2667 <li> NULL if not found. </li>
2668 </ul> */
2669 /****************************************************************************/
2670
2671 MULTIGRID * NS_DIM_PREFIX GetMultigrid (const char *name)
2672 {
2673 return ((MULTIGRID *) SearchEnv(name,"/Multigrids",
2674 theMGDirID,theMGRootDirID));
2675 }
2676
2677 /****************************************************************************/
2678 /** \brief Return a pointer to the first multigrid
2679
2680 This function returns a pointer to the first multigrid in the /Multigrids
2681 directory.
2682
2683 @return <ul>
2684 <li> pointer to MULTIGRID </li>
2685 <li> NULL if not found. </li>
2686 </ul> */
2687 /****************************************************************************/
2688
2689 MULTIGRID * NS_DIM_PREFIX GetFirstMultigrid ()
2690 {
2691 ENVDIR *theMGRootDir;
2692 MULTIGRID *theMG;
2693
2694 theMGRootDir = ChangeEnvDir("/Multigrids");
2695
2696 assert (theMGRootDir!=NULL);
2697
2698 theMG = (MULTIGRID *) ENVDIR_DOWN(theMGRootDir);
2699
2700 if (theMG != NULL)
2701 {
2702 #ifdef ModelP
2703 InitCurrMG(theMG);
2704 #endif
2705 }
2706
2707 return (theMG);
2708 }
2709
2710 /****************************************************************************/
2711 /** \brief Return a pointer to the next multigrid
2712
2713 * @param theMG - multigrid structure
2714
2715 This function returns a pointer to the next multigrid in the /Multigrids
2716 directory.
2717
2718 @return <ul>
2719 <li> pointer to MULTIGRID </li>
2720 <li> NULL if not found. </li>
2721 </ul> */
2722 /****************************************************************************/
2723
2724 MULTIGRID * NS_DIM_PREFIX GetNextMultigrid (const MULTIGRID *theMG)
2725 {
2726 MULTIGRID *MG;
2727
2728 MG = (MULTIGRID *) NEXT_ENVITEM(theMG);
2729
2730 if (MG != NULL)
2731 {
2732 #ifdef ModelP
2733 InitCurrMG(MG);
2734 #endif
2735 }
2736
2737 return (MG);
2738 }
2739
2740 /****************************************************************************/
2741 /** \brief Return a pointer to new multigrid structure
2742
2743 * @param MultigridName - name of multigrid
2744 * @param domain - name of domain description from environment
2745 * @param problem - name of problem description from environment
2746 * @param format - name of format description from environment
2747 * @param optimizedIE - allocate NodeElementList
2748
2749 This function creates and initializes a new multigrid structure including
2750 allocation of heap, combining the domain and the boundary conditions
2751 and creation of the fixed corners of the domain.
2752
2753 @return <ul>
2754 <li> pointer to new object </li>
2755 <li> NULL if an error occurred. </li>
2756 </ul> */
2757 /****************************************************************************/
2758
2759 MULTIGRID * NS_DIM_PREFIX CreateMultiGrid (char *MultigridName, STD_BVP *theBVP,
2760 const char *format, INT optimizedIE, INT insertMesh,
2761 std::shared_ptr<PPIF::PPIFContext> ppifContext)
2762 {
2763 HEAP *theHeap;
2764 MULTIGRID *theMG;
2765 INT i;
2766 MESH mesh;
2767 INT MarkKey;
2768
2769 if (not ppifContext)
2770 ppifContext = std::make_shared<PPIF::PPIFContext>();
2771
2772 /* allocate multigrid envitem */
2773 theMG = MakeMGItem(MultigridName, ppifContext);
2774 if (theMG==NULL) return(NULL);
2775
2776 #ifdef ModelP
2777 InitCurrMG(theMG);
2778 #endif
2779
2780 /* allocate the heap */
2781 /* When using the system heap: allocate just enough memory for the actual bookkeeping data structure */
2782 theHeap = NewHeap(SIMPLE_HEAP, sizeof(HEAP), malloc(sizeof(HEAP)));
2783 if (theHeap==NULL)
2784 {
2785 UserWriteF("CreateMultiGrid: cannot allocate %ld bytes\n", sizeof(HEAP));
2786 PrintErrorMessage('E', "CreateMultiGrid","Cannot allocate heap!");
2787
2788 DisposeMultiGrid(theMG);
2789 return(NULL);
2790 }
2791
2792 /* mark temp memory here, release it after coarse grid construction in FixCoarseGrid */
2793 MarkTmpMem(theHeap,&MarkKey);
2794 MG_MARK_KEY(theMG) = MarkKey;
2795
2796 if (insertMesh)
2797 BVP_Init(theBVP,theHeap,&mesh,MarkKey);
2798 else
2799 BVP_Init(theBVP,theHeap,NULL,MarkKey);
2800 if (theBVP==NULL)
2801 {
2802 PrintErrorMessage('E',"CreateMultiGrid","BVP not found");
2803 return(NULL);
2804 }
2805 theMG->BVP_Name = "dummy BVP name";
2806
2807 /* 1: general user data space */
2808 // As we are using this version only with DUNE, we will never have UG user data
2809 /* 2: user heap */
2810 // As we are using this version only with DUNE, we will never need the user heap
2811
2812 /* fill multigrid structure */
2813 theMG->status = 0;
2814 MG_COARSE_FIXED(theMG) = 0;
2815 theMG->vertIdCounter = 0;
2816 theMG->nodeIdCounter = 0;
2817 theMG->elemIdCounter = 0;
2818 theMG->edgeIdCounter = 0;
2819 #ifndef ModelP
2820 theMG->vectorIdCounter = 0;
2821 #endif
2822 theMG->topLevel = -1;
2823 MG_BVP(theMG) = theBVP;
2824 RESETMGSTATUS(theMG);
2825
2826 theMG->theHeap = theHeap;
2827 for (i=0; i<MAXLEVEL; i++)
2828 GRID_ON_LEVEL(theMG,i) = NULL;
2829
2830 /* allocate level 0 grid */
2831 if (CreateNewLevel(theMG)==NULL)
2832 {
2833 DisposeMultiGrid(theMG);
2834 return(NULL);
2835 }
2836
2837 /* allocate predefined mesh, e. g. corner vertices pointers */
2838 if (insertMesh)
2839 {
2840 #ifdef ModelP
2841 if (theMG->ppifContext().isMaster())
2842 {
2843 #endif
2844 if (InsertMesh(theMG,&mesh))
2845 {
2846 DisposeMultiGrid(theMG);
2847 return(NULL);
2848 }
2849 #ifdef ModelP
2850 }
2851 #endif
2852
2853 ASSERT(mesh.mesh_status!=MESHSTAT_NOTINIT);
2854 if (mesh.mesh_status==MESHSTAT_MESH)
2855 if (FixCoarseGrid(theMG))
2856 {
2857 DisposeMultiGrid(theMG);
2858 return(NULL);
2859 }
2860 }
2861 /* return ok */
2862 return(theMG);
2863 }
2864
2865 /****************************************************************************/
2866 /** \brief Remove edge from the data structure
2867
2868 * @param theGrid - grid to remove from
2869 * @param theEdge - edge to remove
2870
2871 This function remove an edge from the data structure including its
2872 vector (if one) and inserts them into the free list.
2873
2874 @return <ul>
2875 <li> 0 if ok </li>
2876 <li> 1 if an error occurred. </li>
2877 </ul> */
2878 /****************************************************************************/
2879
2880 static INT DisposeEdge (GRID *theGrid, EDGE *theEdge)
2881 {
2882 LINK *link0,*link1,*pl;
2883 NODE *from,*to;
2884 INT found;
2885
2886 /* reconstruct data */
2887 link0 = LINK0(theEdge);
2888 link1 = LINK1(theEdge);
2889 from = NBNODE(link1);
2890 to = NBNODE(link0);
2891 found = 0;
2892
2893 /* delete link0 in from vertex */
2894 if (START(from)==link0)
2895 {
2896 START(from) = NEXT(link0);
2897 found++;
2898 }
2899 else
2900 {
2901 for (pl=START(from); pl!=NULL; pl = NEXT(pl))
2902 {
2903 if (NEXT(pl)==link0)
2904 {
2905 NEXT(pl) = NEXT(link0);
2906 found++;
2907 break;
2908 }
2909 }
2910 }
2911
2912 /* delete link1 in to vertex */
2913 if (START(to)==link1)
2914 {
2915 START(to) = NEXT(link1);
2916 found++;
2917 }
2918 else
2919 {
2920 for (pl=START(to); pl!=NULL; pl = NEXT(pl))
2921 {
2922 if (NEXT(pl)==link1)
2923 {
2924 NEXT(pl) = NEXT(link1);
2925 found++;
2926 break;
2927 }
2928 }
2929 }
2930
2931 /* reset pointer of midnode to edge */
2932 if (MIDNODE(theEdge) != NULL)
2933 SETNFATHER(MIDNODE(theEdge),NULL);
2934
2935 PutFreeObject(theGrid->mg,theEdge,sizeof(EDGE)-sizeof(VECTOR*),EDOBJ);
2936
2937 /* check error condition */
2938 if (found!=2) RETURN(1);
2939
2940 /* return ok */
2941 NE(theGrid)--;
2942 return(0);
2943 }
2944
2945 /****************************************************************************/
2946 /** \brief Remove node including its edges from the data structure
2947
2948 * @param theGrid - grid to remove from
2949 * @param theNode - node to remove
2950
2951 This function removes node including its edges and vector (if one)
2952 from the data structure and inserts all objects into the free list.
2953
2954 @return <ul>
2955 <li> 0 if ok </li>
2956 <li> 1 when error occurred. </li>
2957 </ul> */
2958 /****************************************************************************/
2959
2960 INT NS_DIM_PREFIX DisposeNode (GRID *theGrid, NODE *theNode)
2961 {
2962 VERTEX *theVertex;
2963 GEOM_OBJECT *father;
2964
2965 /* call DisposeElement first! */
2966 assert(START(theNode) == NULL);
2967 #ifdef ModelP
2968 if (SONNODE(theNode) != NULL)
2969 {
2970 SETNFATHER(SONNODE(theNode),NULL);
2971 }
2972 #else
2973 assert(SONNODE(theNode) == NULL);
2974 #endif
2975
2976 /* remove node from node list */
2977 GRID_UNLINK_NODE(theGrid,theNode);
2978
2979 theVertex = MYVERTEX(theNode);
2980 father = (GEOM_OBJECT *)NFATHER(theNode);
2981 if (father != NULL)
2982 {
2983 switch (NTYPE(theNode))
2984 {
2985
2986 case (CORNER_NODE) :
2987 ASSERT(OBJT(father) == NDOBJ);
2988 SONNODE((NODE *)father) = NULL;
2989 #ifdef TOPNODE
2990 if (theVertex != NULL)
2991 TOPNODE(theVertex) = (NODE *)father;
2992 #endif
2993 break;
2994
2995 case (MID_NODE) :
2996 ASSERT(OBJT(father) == EDOBJ);
2997 MIDNODE((EDGE *)father) = NULL;
2998 break;
2999
3000 #ifdef __CENTERNODE__
3001 case (CENTER_NODE) :
3002 ASSERT(OBJT(father)==IEOBJ || OBJT(father)==BEOBJ);
3003 SET_CENTERNODE((ELEMENT *)father,NULL);
3004 break;
3005 #endif
3006
3007 default :
3008 ASSERT(0);
3009 break;
3010 }
3011 }
3012
3013 /** \todo delete old vertex handling */
3014 if (0)
3015 if (theVertex != NULL)
3016 {
3017 #ifdef ModelP
3018 /* vertices have to be linked and unlinked */
3019 /* relative to the level they are created for */
3020 INT levelofvertex = LEVEL(theVertex);
3021 MULTIGRID *MG = MYMG(theGrid);
3022 GRID *GridOfVertex = GRID_ON_LEVEL(MG,levelofvertex);
3023
3024 if (SONNODE(theNode) == NULL)
3025 DisposeVertex(GridOfVertex,theVertex);
3026 #else
3027 DisposeVertex(theGrid,theVertex);
3028 #endif
3029 }
3030
3031 if (NOOFNODE(theVertex)<1)
3032 RETURN(GM_ERROR);
3033 if (NOOFNODE(theVertex)==1)
3034 DisposeVertex(theGrid,theVertex);
3035 else
3036 DECNOOFNODE(theVertex);
3037
3038 #ifdef ModelP
3039 /* free message buffer */
3040 theNode->message_buffer_free();
3041 #endif
3042
3043 PutFreeObject(theGrid->mg,theNode,sizeof(NODE),NDOBJ);
3044
3045 /* return ok */
3046 return(0);
3047 }
3048
3049 /****************************************************************************/
3050 /** \brief Remove vertex from the data structure
3051
3052 * @param theGrid - grid to remove from
3053 * @param theVertex - vertex to remove
3054
3055 This function removes a vertex from the data structure
3056 and puts it into the free list.
3057
3058 @return <ul>
3059 <li> 0 if ok </li>
3060 <li> 1 no valid object number </li>
3061 </ul> */
3062 /****************************************************************************/
3063
3064 static INT DisposeVertex (GRID *theGrid, VERTEX *theVertex)
3065 {
3066 // The following call to HEAPFAULT triggers a failing assertion in some
3067 // distributed settings. I don't know whether this is the sign of a hidden bug
3068 // somewhere, or whether HEAPFAULT is an obsolete left-over of UG3's
3069 // hand-written manual memory heap.
3070 //HEAPFAULT(theVertex);
3071
3072 PRINTDEBUG(gm,1,(PFMT "DisposeVertex(): Gridlevel=%d theVertex=" VID_FMTX "\n",
3073 theGrid->ppifContext().me(),GLEVEL(theGrid),VID_PRTX(theVertex)));
3074
3075 theGrid = GRID_ON_LEVEL(MYMG(theGrid),LEVEL(theVertex));
3076
3077 /* remove vertex from vertex list */
3078 GRID_UNLINK_VERTEX(theGrid,theVertex);
3079
3080 if( OBJT(theVertex) == BVOBJ )
3081 {
3082 BNDP_Dispose(MGHEAP(MYMG(theGrid)),V_BNDP(theVertex));
3083 PutFreeObject(MYMG(theGrid),theVertex,sizeof(struct bvertex),BVOBJ);
3084 }
3085 else
3086 PutFreeObject(MYMG(theGrid),theVertex,sizeof(struct ivertex),IVOBJ);
3087
3088 return(0);
3089 }
3090
3091 /****************************************************************************/
3092 /** \brief Remove element from the data structure
3093
3094 * @param theGrid - grid to remove from
3095 * @param theElement - element to remove
3096
3097 This function removes an element from the data structure and inserts it
3098 into the free list. This includes all elementsides, sidevectors and the
3099 elementvector if they exist.
3100
3101 @return <ul>
3102 <li> 0 if ok </li>
3103 <li> 1 no valid object number. </li>
3104 </ul> */
3105 /****************************************************************************/
3106
3107 INT NS_DIM_PREFIX DisposeElement (GRID *theGrid, ELEMENT *theElement)
3108 {
3109 INT j,tag;
3110 NODE *theNode;
3111 VERTEX *theVertex;
3112 EDGE *theEdge;
3113 BNDS *bnds;
3114 ELEMENT *theFather;
3115 ELEMENT *succe = SUCCE(theElement);
3116 #ifdef UG_DIM_3
3117 VECTOR *theVector;
3118 DOUBLE fac;
3119 INT k,m,o,l;
3120 #endif
3121
3122 GRID_UNLINK_ELEMENT(theGrid,theElement);
3123
3124 #ifdef __CENTERNODE__
3125 {
3126 theNode = CENTERNODE(theElement);
3127
3128 if (theNode != NULL) SETNFATHER(theNode,NULL);
3129 }
3130 #endif
3131
3132 theFather = EFATHER(theElement);
3133
3134 if (LEVEL(theElement)>0)
3135 {
3136 #ifndef ModelP
3137 ASSERT(theFather != NULL);
3138 #endif
3139
3140 /* check intergrid pointer from father */
3141 if (theFather != NULL)
3142 {
3143 #ifdef ModelP
3144 int index = PRIO2INDEX(EPRIO(theElement));
3145 #else
3146 int index = 0;
3147 #endif
3148 ELEMENT *Next = NULL;
3149 ASSERT(index!=-1 && index<2);
3150
3151 if (SON(theFather,index) == theElement)
3152 {
3153 if (succe != NULL)
3154 {
3155 if (EFATHER(succe)==theFather)
3156 #ifdef ModelP
3157 if (PRIO2INDEX(EPRIO(succe)) == PRIO2INDEX(EPRIO(theElement)))
3158 #endif
3159 {
3160 Next = succe;
3161 }
3162 }
3163 SET_SON(theFather,index,Next);
3164 }
3165
3166 SETNSONS(theFather,NSONS(theFather)-1);
3167
3168 PRINTDEBUG(gm,2,(PFMT "DisposeElement(): elem=" EID_FMTX
3169 " father=" EID_FMTX " son0=%x son1=%x\n",
3170 theGrid->ppifContext().me(),EID_PRTX(theElement),EID_PRTX(theFather),
3171 SON(theFather,0),SON(theFather,1)));
3172 }
3173 }
3174
3175 #ifdef ModelP
3176 /* reset father pointers of sons */
3177 /** \todo possibly some son cannot be reached by GetAllSons, */
3178 /* because their father has not been on this proc and */
3179 /* they lost their father pointers */
3180 if (NSONS(theElement)>0)
3181 {
3182 ELEMENT *SonList[MAX_SONS];
3183
3184 if (GetAllSons(theElement,SonList)) RETURN(GM_FATAL);
3185
3186 INT i = 0;
3187 while (i<MAX_SONS && SonList[i] != NULL)
3188 {
3189 PRINTDEBUG(gm,2,(PFMT "DisposeElement(): elem=" EID_FMTX
3190 " deleting fatherpointer of son=" EID_FMTX "\n",
3191 theGrid->ppifContext().me(),EID_PRTX(theElement),EID_PRTX(SonList[i])));
3192 SET_EFATHER(SonList[i],NULL);
3193
3194 /* reset VFATHER of centernode vertex */
3195 for (j=0; j<CORNERS_OF_ELEM(SonList[i]); j++)
3196 {
3197 theNode = CORNER(SonList[i],j);
3198 #ifndef __CENTERNODE__
3199 if (CENTERTYPE(theNode) && NFATHER(theNode)!=NULL)
3200 SETNFATHER(theNode,NULL);
3201 #endif
3202 theVertex = MYVERTEX(theNode);
3203 if (VFATHER(theVertex) != NULL && VFATHER(theVertex) == theElement)
3204 VFATHER(theVertex) = NULL;
3205 }
3206 i++;
3207 }
3208 }
3209 #endif
3210
3211 /* remove element sides if it's a boundary element */
3212 if (OBJT(theElement)==BEOBJ)
3213 for (INT i = 0; i < SIDES_OF_ELEM(theElement); i++)
3214 {
3215 bnds = ELEM_BNDS(theElement,i);
3216 if (bnds != NULL)
3217 BNDS_Dispose(MGHEAP(MYMG(theGrid)),bnds);
3218 }
3219
3220 #ifdef UG_DIM_3
3221 /* reset VFATHER of sidenodes */
3222 for (j=0; j<SIDES_OF_ELEM(theElement); j++) {
3223 theNode = GetSideNode(theElement,j);
3224 if (theNode == NULL) continue;
3225 theVertex = MYVERTEX(theNode);
3226 if (VFATHER(MYVERTEX(theNode)) == theElement) {
3227 ELEMENT *theNb = NBELEM(theElement,j);
3228
3229 VFATHER(theVertex) = theNb;
3230 if (theNb != NULL) {
3231 /* calculate new local coords */
3232 k = ONNBSIDE(theVertex);
3233 SETONSIDE(theVertex,k);
3234 m = CORNERS_OF_SIDE(theNb,k);
3235 FieldVector<DOUBLE,DIM>& local = LCVECT(theVertex);
3236 fac = 1.0 / m;
3237 V_DIM_CLEAR(local);
3238 for (o=0; o<m; o++) {
3239 l = CORNER_OF_SIDE(theNb,k,o);
3240 V_DIM_LINCOMB(1.0,local,1.0,
3241 LOCAL_COORD_OF_ELEM(theNb,l),local);
3242 }
3243 V_DIM_SCALE(fac,local);
3244 }
3245 }
3246 SETONNBSIDE(theVertex,MAX_SIDES_OF_ELEM);
3247 }
3248 #endif
3249
3250 for (j=0; j<EDGES_OF_ELEM(theElement); j++)
3251 {
3252 theEdge=GetEdge(CORNER(theElement,CORNER_OF_EDGE(theElement,j,0)),
3253 CORNER(theElement,CORNER_OF_EDGE(theElement,j,1)));
3254 ASSERT(theEdge!=NULL);
3255
3256 if (NO_OF_ELEM(theEdge)<1)
3257 RETURN(GM_ERROR);
3258
3259 /* edit VFATHER of midnode */
3260 if (MIDNODE(theEdge) != NULL)
3261 {
3262 theVertex = MYVERTEX(MIDNODE(theEdge));
3263 if (VFATHER(theVertex) == theElement) {
3264 #ifdef UG_DIM_2
3265 theFather = NBELEM(theElement,j);
3266 VFATHER(theVertex) = theFather;
3267 if (theFather != NULL)
3268 {
3269 /* calculate new local coords */
3270 int co0,co1;
3271
3272 /* reconstruct local coordinates of vertex */
3273 co0 = CORNER_OF_EDGE(theFather,j,0);
3274 co1 = CORNER_OF_EDGE(theFather,j,1);
3275
3276 /* local coordinates have to be local towards pe */
3277 V_DIM_LINCOMB(0.5, LOCAL_COORD_OF_ELEM(theFather,co0),
3278 0.5, LOCAL_COORD_OF_ELEM(theFather,co1),
3279 LCVECT(theVertex));
3280 SETONEDGE(theVertex,j);
3281 }
3282 #endif
3283 #ifdef UG_DIM_3
3284 VFATHER(theVertex) = NULL;
3285 #endif
3286 }
3287 }
3288
3289 if (NO_OF_ELEM(theEdge)==1)
3290 DisposeEdge(theGrid,theEdge);
3291 else
3292 DEC_NO_OF_ELEM(theEdge);
3293 }
3294
3295 for (j=0; j<CORNERS_OF_ELEM(theElement); j++)
3296 {
3297 theNode = CORNER(theElement,j);
3298
3299 if (START(theNode) == NULL)
3300 {
3301 if (NTYPE(theNode)==MID_NODE)
3302 {
3303 if (NFATHER(theNode)!=NULL)
3304 {
3305 MIDNODE((EDGE *)NFATHER(theNode)) = NULL;
3306 }
3307 #ifndef ModelP
3308 /* HEAPFAULT in theFather possible, if in a previous call
3309 some son is not reached by GetAllSons */
3310 else
3311 {
3312 theVertex = MYVERTEX(theNode);
3313 theFather = VFATHER(theVertex);
3314 if (theFather != NULL)
3315 {
3316 INT edge = ONEDGE(theVertex);
3317 theEdge = GetEdge(CORNER(theFather,
3318 CORNER_OF_EDGE(theFather,edge,0)),
3319 CORNER(theFather,
3320 CORNER_OF_EDGE(theFather,edge,1)));
3321 ASSERT(theEdge!=NULL);
3322 MIDNODE(theEdge) = NULL;
3323 }
3324 }
3325 #endif
3326 }
3327 DisposeNode(theGrid,theNode);
3328 }
3329 }
3330
3331 /* reset neighbor pointers referencing element and dispose vectors in sides if */
3332 for (INT i = 0; i < SIDES_OF_ELEM(theElement); i++)
3333 {
3334 ELEMENT *theNeighbor = NBELEM(theElement,i);
3335
3336 #ifdef UG_DIM_3
3337 if (VEC_DEF_IN_OBJ_OF_GRID(theGrid,SIDEVEC))
3338 {
3339 theVector = SVECTOR(theElement,i);
3340 if (theVector!=NULL)
3341 {
3342 assert (VCOUNT(theVector) != 0);
3343 assert (VCOUNT(theVector) != 3);
3344 if (VCOUNT(theVector) == 1)
3345 {
3346 if (DisposeVector (theGrid,theVector))
3347 RETURN (1);
3348 }
3349 else
3350 {
3351 if (!FindNeighborElement (theElement,i,&theNeighbor,&j))
3352 RETURN (1);
3353 VOBJECT(theVector) = (GEOM_OBJECT *)theNeighbor;
3354 SETVECTORSIDE(theVector,j);
3355 SETVCOUNT(SVECTOR(theElement,i),1);
3356 }
3357 }
3358 }
3359 #endif
3360 if (theNeighbor!=NULL)
3361 {
3362 for (j=0; j<SIDES_OF_ELEM(theNeighbor); j++)
3363 if (NBELEM(theNeighbor,j)==theElement)
3364 {
3365 SET_NBELEM(theNeighbor,j,NULL);
3366 break;
3367 }
3368 #ifdef ModelP
3369 ASSERT(j<SIDES_OF_ELEM(theNeighbor) || EGHOST(theElement));
3370 #else
3371 ASSERT(j<SIDES_OF_ELEM(theNeighbor));
3372 #endif
3373 }
3374 }
3375
3376 #ifdef ModelP
3377 /* free message buffer */
3378 theElement->message_buffer_free();
3379 #endif
3380
3381 /* dispose element */
3382 /* give it a new tag ! (I know this is somewhat ugly) */
3383 tag = TAG(theElement);
3384 if (OBJT(theElement)==BEOBJ)
3385 {
3386 SETOBJT(theElement,MAPPED_BND_OBJT_TAG(tag));
3387 PutFreeObject(theGrid->mg,theElement,
3388 BND_SIZE_TAG(tag),MAPPED_BND_OBJT_TAG(tag));
3389 }
3390 else
3391 {
3392 SETOBJT(theElement,MAPPED_INNER_OBJT_TAG(tag));
3393 PutFreeObject(theGrid->mg,theElement,INNER_SIZE_TAG(tag),
3394 MAPPED_INNER_OBJT_TAG(tag));
3395 }
3396
3397 return(0);
3398 }
3399
3400 #ifndef ModelP
3401 #define DO_NOT_DISPOSE return (2)
3402 #else
3403 #define DO_NOT_DISPOSE dispose=0
3404 #endif
3405
3406 /****************************************************************************/
3407 /** \brief Construct coarse grid from surface
3408
3409 * @param theMG - multigrid to collapse
3410
3411 This function constructs coarse grid from surface. ATTENTION: Use refine $g
3412 to cover always the whole domain with the grid on each level.
3413
3414 @return <ul>
3415 <li> 0 if ok </li>
3416 <li> 1 no valid object number </li>
3417 <li> 2 grid structure not empty or level 0 </li>
3418 </ul> */
3419 /****************************************************************************/
3420
3421 INT NS_DIM_PREFIX Collapse (MULTIGRID *theMG)
3422 {
3423 GRID *theGrid;
3424 ELEMENT *theElement;
3425 NODE *theNode;
3426 EDGE *theEdge;
3427 VERTEX *theVertex;
3428 #ifdef ModelP
3429 VECTOR *vec;
3430 #endif
3431 INT tl = TOPLEVEL(theMG);
3432 INT l,i;
3433
3434 #ifdef ModelP
3435 DDD_XferBegin(theMG->dddContext());
3436 #ifdef DDDOBJMGR
3437 DDD_ObjMgrBegin();
3438 #endif
3439 #endif
3440 for (l=tl-1; l>=0; l--) {
3441 theGrid = GRID_ON_LEVEL(theMG,l);
3442 for (theNode=PFIRSTNODE(theGrid); theNode != NULL;
3443 theNode = SUCCN(theNode)) {
3444 SONNODE(theNode) = NULL;
3445 SETNFATHER(theNode,NULL);
3446 }
3447 for (theElement=PFIRSTELEMENT(theGrid); theElement != NULL;
3448 theElement = SUCCE(theElement)) {
3449 SETNSONS(theElement,0);
3450 SET_SON(theElement,0,NULL);
3451 #ifdef ModelP
3452 SET_SON(theElement,1,NULL);
3453 #endif
3454 for (i=0; i<EDGES_OF_ELEM(theElement); i++) {
3455 theEdge = GetEdge(CORNER(theElement,
3456 CORNER_OF_EDGE(theElement,i,0)),
3457 CORNER(theElement,
3458 CORNER_OF_EDGE(theElement,i,1)));
3459 MIDNODE(theEdge) = NULL;
3460 }
3461 }
3462 while (PFIRSTELEMENT(theGrid)!=NULL)
3463 if (DisposeElement(theGrid,PFIRSTELEMENT(theGrid)))
3464 return(1);
3465 while (PFIRSTNODE(theGrid)!=NULL)
3466 {
3467 if (DisposeNode(theGrid,PFIRSTNODE(theGrid)))
3468 return(1);
3469 }
3470 while (PFIRSTVERTEX(theGrid)!=NULL) {
3471 theVertex = PFIRSTVERTEX(theGrid);
3472 GRID_UNLINK_VERTEX(theGrid,theVertex);
3473 GRID_LINK_VERTEX(GRID_ON_LEVEL(theMG,tl),
3474 theVertex,VXPRIO(theVertex));
3475 }
3476 GRID_ON_LEVEL(theMG,l) = NULL;
3477 }
3478
3479 #ifdef ModelP
3480 #ifdef DDDOBJMGR
3481 DDD_ObjMgrEnd();
3482 #endif
3483 DDD_XferEnd(theMG->dddContext());
3484 #endif
3485
3486 /* move top level grid to bottom (level 0) */
3487 theGrid = GRID_ON_LEVEL(theMG,tl);
3488 theGrid->finer = NULL;
3489 theGrid->coarser = NULL;
3490 theGrid->level = 0;
3491 GRID_ON_LEVEL(theMG,tl) = NULL;
3492 GRID_ON_LEVEL(theMG,0) = theGrid;
3493 theMG->topLevel = 0;
3494 theMG->fullrefineLevel = 0;
3495
3496 for (theNode=PFIRSTNODE(theGrid); theNode != NULL;
3497 theNode = SUCCN(theNode)) {
3498 SETNFATHER(theNode,NULL);
3499 SETNTYPE(theNode,LEVEL_0_NODE);
3500 SETNCLASS(theNode,3);
3501 SETNNCLASS(theNode,0);
3502 SETLEVEL(theNode,0);
3503 VFATHER(MYVERTEX(theNode)) = NULL;
3504 #ifdef ModelP
3505 DDD_AttrSet(PARHDR(theNode),GRID_ATTR(theGrid));
3506 #endif
3507 }
3508 for (theElement=PFIRSTELEMENT(theGrid); theElement != NULL;
3509 theElement = SUCCE(theElement)) {
3510 SETECLASS(theElement,RED_CLASS);
3511 SET_EFATHER(theElement,NULL);
3512 SETLEVEL(theElement,0);
3513 #ifdef ModelP
3514 DDD_AttrSet(PARHDRE(theElement),GRID_ATTR(theGrid));
3515 #endif
3516 for (i=0; i<EDGES_OF_ELEM(theElement); i++) {
3517 theEdge = GetEdge(CORNER(theElement,
3518 CORNER_OF_EDGE(theElement,i,0)),
3519 CORNER(theElement,
3520 CORNER_OF_EDGE(theElement,i,1)));
3521 SETLEVEL(theEdge,0);
3522 #if (defined ModelP) && (defined UG_DIM_3)
3523 DDD_AttrSet(PARHDR(theEdge),GRID_ATTR(theGrid));
3524 #endif
3525 }
3526 }
3527 for (theVertex=PFIRSTVERTEX(theGrid); theVertex != NULL;
3528 theVertex = SUCCV(theVertex)) {
3529 SETLEVEL(theVertex,0);
3530 #ifdef ModelP
3531 DDD_AttrSet(PARHDRV(theVertex),GRID_ATTR(theGrid));
3532 #endif
3533 ASSERT(NOOFNODE(theVertex)==1);
3534 }
3535
3536 #ifdef ModelP
3537 for (vec=PFIRSTVECTOR(theGrid); vec != NULL; vec = SUCCVC(vec))
3538 DDD_AttrSet(PARHDR(vec),GRID_ATTR(theGrid));
3539
3540 /* rebiuld all DDD interfaces due to removed objects and changed attributes */
3541 DDD_IFRefreshAll(theGrid->dddContext());
3542 #endif
3543
3544 if (MG_COARSE_FIXED(theMG))
3545 if (CreateAlgebra(theMG))
3546 REP_ERR_RETURN(1);
3547
3548 return(0);
3549 }
3550
3551 /****************************************************************************/
3552 /** \brief Remove top level grid from multigrid structure
3553
3554 * @param theMG - multigrid to remove from
3555
3556 This function removes the top level grid from multigrid structure.
3557
3558 @return <ul>
3559 <li> 0 if ok </li>
3560 <li> 1 no valid object number </li>
3561 <li> 2 grid structure not empty or level 0 </li>
3562 </ul> */
3563 /****************************************************************************/
3564
3565 INT NS_DIM_PREFIX DisposeTopLevel (MULTIGRID *theMG)
3566 {
3567 int l;
3568 GRID *theGrid;
3569 #ifdef ModelP
3570 int dispose = 1;
3571 #endif
3572
3573 /* level 0 can not be deleted */
3574 l = theMG->topLevel;
3575 if (l<=0) DO_NOT_DISPOSE;
3576 theGrid = GRID_ON_LEVEL(theMG,l);
3577
3578 /* is level empty */
3579 if (PFIRSTELEMENT(theGrid)!=NULL) DO_NOT_DISPOSE;
3580 if (PFIRSTVERTEX(theGrid)!=NULL) DO_NOT_DISPOSE;
3581 if (PFIRSTNODE(theGrid)!=NULL) DO_NOT_DISPOSE;
3582
3583 #ifdef ModelP
3584 dispose = UG_GlobalMinINT(theMG->ppifContext(), dispose);
3585 if (!dispose) return(2);
3586 #endif
3587
3588 /* remove from grids array */
3589 GRID_ON_LEVEL(theMG,l) = NULL;
3590 GRID_ON_LEVEL(theMG,l-1)->finer = NULL;
3591 (theMG->topLevel)--;
3592
3593 PutFreeObject(theMG,theGrid,sizeof(GRID),GROBJ);
3594
3595 return(0);
3596 }
3597
3598 /****************************************************************************/
3599 /** \brief Dispose top level grid
3600
3601 * @param theGrid - grid to be removed
3602
3603 This function removes the top level grid from multigrid structure.
3604
3605 @return <ul>
3606 <li> 0 if ok </li>
3607 <li> 1 no valid object number </li>
3608 <li> 2 grid structure not empty or level 0 </li>
3609 </ul> */
3610 /****************************************************************************/
3611
3612 INT NS_DIM_PREFIX DisposeGrid (GRID *theGrid)
3613 {
3614 MULTIGRID *theMG;
3615
3616 if (theGrid == NULL)
3617 return(0);
3618
3619 theMG = MYMG(theGrid);
3620
3621 if (GLEVEL(theGrid)<0)
3622 return (1);
3623
3624 if (theGrid->finer != NULL)
3625 return(1);
3626
3627 /* clear level */
3628 while (PFIRSTELEMENT(theGrid)!=NULL)
3629 if (DisposeElement(theGrid,PFIRSTELEMENT(theGrid)))
3630 return(2);
3631
3632 while (PFIRSTNODE(theGrid)!=NULL)
3633 if (DisposeNode(theGrid,PFIRSTNODE(theGrid)))
3634 return(2);
3635
3636 while (PFIRSTVERTEX(theGrid)!=NULL)
3637 if (DisposeVertex(theGrid,PFIRSTVERTEX(theGrid)))
3638 return(4);
3639
3640 /* level 0 can not be deleted */
3641 if (GLEVEL(theGrid) > 0)
3642 return(DisposeTopLevel(theMG));
3643
3644 /* remove from grids array */
3645 GRID_ON_LEVEL(theMG,0) = NULL;
3646 theMG->topLevel = -1;
3647 theMG->nodeIdCounter = 0;
3648 theMG->vertIdCounter = 0;
3649 theMG->elemIdCounter = 0;
3650
3651 PutFreeObject(theMG,theGrid,sizeof(GRID),GROBJ);
3652
3653 return(0);
3654 }
3655
3656
3657 /****************************************************************************/
3658 /** \brief Release memory for the whole multigrid structure
3659
3660 * @param theMG - multigrid to remove
3661
3662 This function releases the memory for the whole multigrid structure.
3663
3664 @return <ul>
3665 <li> GM_OK if ok </li>
3666 <li> GM_ERROR when error occurred. </li>
3667 </ul> */
3668 /****************************************************************************/
3669
3670 INT NS_DIM_PREFIX DisposeMultiGrid (MULTIGRID *theMG)
3671 {
3672 INT level;
3673
3674 #ifdef ModelP
3675 /* tell DDD that we will 'inconsistently' delete objects.
3676 this is a dangerous mode as it switches DDD warnings off. */
3677 DDD_SetOption(theMG->dddContext(), OPT_WARNING_DESTRUCT_HDR, OPT_OFF);
3678 #endif
3679
3680 for (level = TOPLEVEL(theMG); level >= 0; level --)
3681 if (DisposeGrid(GRID_ON_LEVEL(theMG,level)))
3682 RETURN(1);
3683
3684 #ifdef ModelP
3685 /* stop dangerous mode. from now on DDD will issue warnings again. */
3686 DDD_SetOption(theMG->dddContext(), OPT_WARNING_DESTRUCT_HDR, OPT_ON);
3687
3688 /* rebuild DDD-interfaces because distributed vectors have been
3689 deleted without communication */
3690 DDD_IFRefreshAll(theMG->dddContext());
3691 #endif
3692
3693 /** \todo Normally the MG-heap should be cleaned-up before freeing.
3694 DDD depends on storage in the heap, even if no DDD objects
3695 are allocated!! (due to free-lists, DDD type definitions
3696 etc.) therefore, repeated new/close commands are inhibited
3697 explicitly in dune/uggrid/parallel/dddif/initddd.c(InitCurrMG()). */
3698 DisposeHeap(MGHEAP(theMG));
3699
3700 /* dispose BVP */
3701 delete MG_BVP(theMG);
3702
3703 /* first unlock the mg */
3704 ((ENVITEM*) theMG)->v.locked = false;
3705
3706 #ifdef ModelP
3707 ExitDDD(theMG->dddContext());
3708 globalDDDContext(nullptr);
3709 #endif
3710 theMG->~multigrid();
3711
3712 /* delete mg */
3713 if (ChangeEnvDir("/Multigrids")==NULL) RETURN (GM_ERROR);
3714 if (RemoveEnvDir ((ENVITEM *)theMG)) RETURN (GM_ERROR);
3715
3716 return(GM_OK);
3717 }
3718
3719 /****************************************************************************/
3720 /** \brief Determine neighbor and side of neighbor that goes back to element
3721 *
3722 * @param theElement - considered element
3723 * @param Side - side of that element
3724 * @param theNeighbor - handle to neighbor
3725 * @param NeighborSide - number of side of neighbor that goes back to elem
3726 *
3727 This function determines the neighbor and side of the neighbor that goes back to elem.
3728
3729 @return <ul>
3730 <li> 0 if ok </li>
3731 <li> 1 when error occurred. </li>
3732 </ul> */
3733 /****************************************************************************/
3734
3735 INT NS_DIM_PREFIX FindNeighborElement (const ELEMENT *theElement, INT Side, ELEMENT **theNeighbor, INT *NeighborSide)
3736 {
3737 INT i;
3738
3739 /* find neighbor */
3740 *theNeighbor = NBELEM(theElement,Side);
3741 if (*theNeighbor == NULL) return (0);
3742
3743 /* search the side */
3744 for (i=0; i<SIDES_OF_ELEM(*theNeighbor); i++)
3745 if (NBELEM(*theNeighbor,i) == theElement)
3746 break;
3747
3748 /* found ? */
3749 if (i<SIDES_OF_ELEM(*theNeighbor))
3750 {
3751 *NeighborSide = i;
3752 return (1);
3753 }
3754 return (0);
3755 }
3756
3757
3758 /****************************************************************************/
3759 /** \brief Insert an inner node
3760 *
3761 * @param theGrid grid structure
3762 * @param pos array containing position
3763 *
3764 * This function inserts a inner node into level 0.
3765 *
3766 * @return <ul>
3767 * <li> pointer to new node if ok </li>
3768 * <li> NULL when error occurred </li>
3769 * </ul>
3770 */
3771 /****************************************************************************/
3772
3773 NODE * NS_DIM_PREFIX InsertInnerNode (GRID *theGrid, const DOUBLE *pos)
3774 {
3775 VERTEX *theVertex;
3776 NODE *theNode;
3777 INT i;
3778
3779 /* create objects */
3780 theVertex = CreateInnerVertex(theGrid);
3781 if (theVertex==NULL)
3782 {
3783 PrintErrorMessage('E',"InsertInnerNode","cannot create vertex");
3784 return(NULL);
3785 }
3786 theNode = CreateNode(theGrid,theVertex,NULL,LEVEL_0_NODE);
3787 if (theNode==NULL)
3788 {
3789 DisposeVertex(theGrid,theVertex);
3790 PrintErrorMessage('E',"InsertInnerNode","cannot create node");
3791 return(NULL);
3792 }
3793
3794 /* fill data */
3795 for (i=0; i<DIM; i++) CVECT(theVertex)[i] = pos[i];
3796 SETMOVE(theVertex,DIM);
3797
3798 return(theNode);
3799 }
3800
3801 /****************************************************************************/
3802 /** \brief Insert a boundary node
3803
3804 * @param theGrid - grid structure
3805 * @param bndp - boundary point descriptor
3806
3807 This function inserts a boundary node into level 0.
3808
3809 @return <ul>
3810 <li> GM_OK if ok </li>
3811 <li> GM_ERROR when error occurred. </li>
3812 </ul> */
3813 /****************************************************************************/
3814
3815 NODE * NS_DIM_PREFIX InsertBoundaryNode (GRID *theGrid, BNDP *bndp)
3816 {
3817 NODE *theNode;
3818 VERTEX *theVertex;
3819 INT move;
3820
3821 /* create objects */
3822 theVertex = CreateBoundaryVertex(theGrid);
3823 if (theVertex==NULL)
3824 {
3825 BNDP_Dispose(MGHEAP(MYMG(theGrid)),bndp);
3826 PrintErrorMessage('E',"InsertBoundaryNode","cannot create vertex");
3827 REP_ERR_RETURN(NULL);
3828 }
3829 if (BNDP_Global(bndp,CVECT(theVertex)))
3830 {
3831 DisposeVertex(theGrid,theVertex);
3832 return(NULL);
3833 }
3834
3835 if (BNDP_BndPDesc(bndp,&move))
3836 {
3837 DisposeVertex(theGrid,theVertex);
3838 return(NULL);
3839 }
3840 SETMOVE(theVertex,move);
3841 V_BNDP(theVertex) = bndp;
3842
3843 theNode = CreateNode(theGrid,theVertex,NULL,LEVEL_0_NODE);
3844 if (theNode==NULL)
3845 {
3846 DisposeVertex(theGrid,theVertex);
3847 PrintErrorMessage('E',"InsertBoundaryNode","cannot create node");
3848 REP_ERR_RETURN(NULL);
3849 }
3850 #ifdef TOPNODE
3851 TOPNODE(theVertex) = theNode;
3852 #endif
3853
3854 PRINTDEBUG(dom,1,(" ipn %ld nd %x bndp %x \n",
3855 ID(theNode),theNode,V_BNDP(theVertex)));
3856
3857 return(theNode);
3858 }
3859
3860 /****************************************************************************/
3861 /** \brief Delete a node
3862
3863 * @param theGrid - grid structure
3864 * @param theNode - node to delete
3865
3866 This function deletes a node from level 0.
3867
3868 @return <ul>
3869 <li> GM_OK if ok </li>
3870 <li> GM_ERROR when error occurred. </li>
3871 </ul> */
3872 /****************************************************************************/
3873
3874 INT NS_DIM_PREFIX DeleteNode (GRID *theGrid, NODE *theNode)
3875 {
3876 VERTEX *theVertex;
3877 ELEMENT *theElement;
3878 INT i;
3879
3880 if (theNode==NULL)
3881 {
3882 PrintErrorMessage('E',"DeleteNode","node not found");
3883 RETURN(GM_ERROR);
3884 }
3885
3886 /* check corner */
3887 theVertex = MYVERTEX(theNode);
3888 if (MOVE(theVertex)==0)
3889 {
3890 PrintErrorMessage('E',"DeleteNode","corners cannot be deleted");
3891 RETURN(GM_ERROR);
3892 }
3893
3894 /* check if some element needs that node */
3895 for (theElement=FIRSTELEMENT(theGrid); theElement!=NULL; theElement=SUCCE(theElement))
3896 for (i=0; i<CORNERS_OF_ELEM(theElement); i++)
3897 if (CORNER(theElement,i)==theNode)
3898 {
3899 PrintErrorMessage('E',"DeleteNode","there is an element needing that node");
3900 RETURN(GM_ERROR);
3901 }
3902
3903 /* now allowed to delete */
3904 DisposeNode(theGrid,theNode);
3905
3906 return(GM_OK);
3907 }
3908
3909 #ifdef UG_DIM_2
3910
3911
3912 /****************************************************************************/
3913 /** \todo Please doc me!
3914
3915 CheckOrientation -
3916
3917 SYNOPSIS:
3918 INT CheckOrientation (INT n, VERTEX **vertices);
3919
3920
3921 * @param n
3922 * @param vertices
3923
3924 DESCRIPTION:
3925
3926 @return
3927 INT
3928 */
3929 /****************************************************************************/
3930
3931 INT NS_DIM_PREFIX CheckOrientation (INT n, VERTEX **vertices)
3932 {
3933 int i;
3934 DOUBLE x1,x2,y1,y2;
3935
3936 for (i=0; i<n; i++)
3937 {
3938 x1 = CVECT(vertices[(i+1)%n])[0] - CVECT(vertices[i])[0];
3939 x2 = CVECT(vertices[(i+n-1)%n])[0] - CVECT(vertices[i])[0];
3940 y1 = CVECT(vertices[(i+1)%n])[1] - CVECT(vertices[i])[1];
3941 y2 = CVECT(vertices[(i+n-1)%n])[1] - CVECT(vertices[i])[1];
3942 if (vp(x1,y1,x2,y2)<SMALL_C)
3943 {
3944 return(0);
3945 }
3946 }
3947 return(1);
3948 }
3949
3950 #endif
3951
3952 #ifdef UG_DIM_3
3953
3954
3955 /****************************************************************************/
3956 /** \todo Please doc me!
3957 CheckOrientation -
3958
3959 SYNOPSIS:
3960 INT CheckOrientation (INT n, VERTEX **vertices);
3961
3962
3963 * @param n
3964 * @param vertices
3965
3966 DESCRIPTION:
3967
3968 @return
3969 INT
3970 */
3971 /****************************************************************************/
3972
3973 INT NS_DIM_PREFIX CheckOrientation (INT n, VERTEX **vertices)
3974 {
3975 DOUBLE_VECTOR diff[3],rot;
3976 DOUBLE det;
3977 INT i;
3978
3979 /* TODO: this case */
3980 if (n == 8 || n==6 || n==5)
3981 return(1);
3982
3983 for (i=1; i<n; i++)
3984 V3_SUBTRACT(CVECT(vertices[i]),CVECT(vertices[0]),diff[i-1]);
3985 V3_VECTOR_PRODUCT(diff[0],diff[1],rot);
3986 V3_SCALAR_PRODUCT(rot,diff[2],det);
3987
3988 if (det < 0.0)
3989 return(0);
3990
3991 return(1);
3992 }
3993 #endif
3994
3995
3996 /****************************************************************************/
3997 /** \todo Please doc me!
3998 CheckOrientationInGrid -
3999
4000 SYNOPSIS:
4001 INT CheckOrientationInGrid (GRID *theGrid);
4002
4003
4004 * @param theGrid
4005
4006 DESCRIPTION:
4007
4008 @return
4009 INT
4010 */
4011 /****************************************************************************/
4012
4013 INT NS_DIM_PREFIX CheckOrientationInGrid (GRID *theGrid)
4014 {
4015 ELEMENT *theElement;
4016 NODE *theNode;
4017 VERTEX *vertices[MAX_CORNERS_OF_ELEM];
4018 INT i;
4019
4020 for (theElement=PFIRSTELEMENT(theGrid); theElement!=NULL; theElement=SUCCE(theElement))
4021 {
4022 for (i=0; i<CORNERS_OF_ELEM(theElement); i++)
4023 {
4024 theNode = CORNER(theElement,i);
4025 if (theNode==NULL) return (1);
4026 vertices[i] = MYVERTEX(theNode);
4027 if (vertices[i]==NULL) return (1);
4028 }
4029 if (!CheckOrientation (CORNERS_OF_ELEM(theElement),vertices)) return (1);
4030 }
4031
4032 return (0);
4033 }
4034
4035
4036 /****************************************************************************/
4037 /** \todo Please doc me!
4038
4039 * @param[in] tag Element type
4040 * @param[in] Node
4041 * @param[in] theMG
4042 * @param[out] NbrS
4043 * @param[out] Nbr
4044
4045 @return 0 if all went well, 1 if an error occurred
4046 */
4047 /****************************************************************************/
4048
4049 static INT NeighborSearch_O_n(INT tag, ELEMENT *theElement, NODE **Node, MULTIGRID *theMG, INT *NbrS, ELEMENT **Nbr)
4050 {
4051
4052 #if 0
4053 // this is (as a reference) the O(n^2) version
4054 INT i,jj,k,m,num;
4055 NODE *sideNode[MAX_CORNERS_OF_SIDE];
4056 ELEMENT *theOther;
4057 NODE *NeighborNode;
4058 MULTIGRID *theMG = MYMG(theGrid);
4059
4060 /*O(n*n)InsertElement ...*/
4061 /* for all sides of the element to be created */
4062 for (i=0; i<SIDES_OF_REF(n); i++)
4063 {
4064 for(jj=0; jj<CORNERS_OF_SIDE_REF(n,i); jj++ )
4065 sideNode[jj] = Node[CORNER_OF_SIDE_REF(n,i,jj)];
4066
4067 /* for all neighbouring elements already inserted */
4068 for (theOther=FIRSTELEMENT(theGrid); theOther!=NULL;
4069 theOther=SUCCE(theOther))
4070 {
4071 if (theOther == theElement)
4072 continue;
4073 /* for all sides of the neighbour element */
4074 for (jj=0; jj<SIDES_OF_ELEM(theOther); jj++)
4075 {
4076 num = 0;
4077 /* for all corners of the side of the neighbour */
4078 for (m=0; m<CORNERS_OF_SIDE(theOther,jj); m++)
4079 {
4080 NeighborNode = CORNER(theOther,
4081 CORNER_OF_SIDE(theOther,jj,m));
4082 /* for all corners of the side of the
4083 element to be created */
4084 for (k=0; k<CORNERS_OF_SIDE_REF(n,i); k++)
4085 if(NeighborNode==sideNode[k])
4086 {
4087 num++;
4088 break;
4089 }
4090 }
4091 if(num==CORNERS_OF_SIDE_REF(n,i))
4092 {
4093 if (NBELEM(theOther,jj)!=NULL)
4094 {
4095 PrintErrorMessage('E',"InsertElement -> NeighborSearch_O_nn",
4096 "neighbor relation inconsistent");
4097 return(1);
4098 }
4099 Nbr[i] = theOther;
4100 NbrS[i] = jj;
4101 }
4102 }
4103 }
4104 }
4105 /* ... O(n*n)InsertElement */
4106 #else
4107 /*O(n*n)InsertElement ...*/
4108 /* for all sides of the element to be created */
4109 MULTIGRID::FaceNodes faceNodes;
4110 for (int i=0; i<SIDES_OF_TAG(tag); i++)
4111 {
4112 int j = 0;
4113 for(j=0; j<CORNERS_OF_SIDE_TAG(tag,i); j++)
4114 faceNodes[j] = Node[CORNER_OF_SIDE_TAG(tag,i,j)];
4115 for(; j<MAX_CORNERS_OF_SIDE; j++)
4116 faceNodes[j] = 0;
4117 std::sort(faceNodes.begin(), faceNodes.begin()+CORNERS_OF_SIDE_TAG(tag,i));
4118
4119 // try to write myself...
4120 auto result = theMG->facemap.emplace(faceNodes,std::make_pair(theElement,i));
4121 // if this failed (i.e. result.second == false) an entry already exists
4122 if (! result.second)
4123 {
4124 // update neighbor list the other entry, stored in result.first->second
4125 auto & data = result.first->second;
4126 ELEMENT* theOther = data.first;
4127 int idx = data.second;
4128 Nbr[i] = theOther;
4129 NbrS[i] = idx;
4130 theMG->facemap.erase(faceNodes);
4131 }
4132
4133 }
4134
4135 #endif
4136
4137 return(0);
4138 /*... O(n)InsertElement ...*/
4139 } /*of static INT NeighborSearch_O_n()*/
4140
4141
4142
4143 /****************************************************************************/
4144 /** \todo Please doc me!
4145 Neighbor_Direct_Insert -
4146
4147 SYNOPSIS:
4148 static INT Neighbor_Direct_Insert(INT n, ELEMENT **ElemList, INT *NbgSdList, INT* NbrS, ELEMENT **Nbr);
4149
4150
4151 * @param tag Element type
4152 * @param ElemList
4153 * @param NbgSdList
4154 * @param NbrS
4155 * @param Nbr
4156
4157 DESCRIPTION:
4158
4159 @return
4160 INT
4161 */
4162 /****************************************************************************/
4163
4164 static INT Neighbor_Direct_Insert(INT tag, ELEMENT **ElemList, const INT *NbgSdList, INT* NbrS, ELEMENT **Nbr)
4165 {
4166 INT i;
4167
4168 for (i=0; i<SIDES_OF_TAG(tag); i++)
4169 Nbr[i] = ElemList[i];
4170 if (NbgSdList != NULL)
4171 for (i=0; i<SIDES_OF_TAG(tag); i++)
4172 NbrS[i] = NbgSdList[i];
4173
4174 return(0);
4175 }
4176
4177
4178 /****************************************************************************/
4179 /** \brief Insert an element
4180
4181 * @param theGrid - grid structure
4182 * @param[in] n Number of vertices of the element to be inserted
4183 * @param Node
4184 * @param ElemList
4185 * @param NbgSdList
4186 * @param bnds_flag
4187
4188 This function inserts an element
4189
4190 \return Pointer to the newly created element, NULL if an error occurred
4191
4192 */
4193 /****************************************************************************/
4194
4195 ELEMENT * NS_DIM_PREFIX InsertElement (GRID *theGrid, INT n, NODE **Node, ELEMENT **ElemList, INT *NbgSdList, INT *bnds_flag)
4196 {
4197 MULTIGRID *theMG;
4198 INT i,j,k,m,rv,tag,ElementType;
4199 INT NeighborSide[MAX_SIDES_OF_ELEM];
4200 [[maybe_unused]] NODE *sideNode[MAX_CORNERS_OF_SIDE];
4201 VERTEX *Vertex[MAX_CORNERS_OF_ELEM],*sideVertex[MAX_CORNERS_OF_SIDE];
4202 ELEMENT *theElement,*Neighbor[MAX_SIDES_OF_ELEM];
4203 BNDS *bnds[MAX_SIDES_OF_ELEM];
4204 BNDP *bndp[MAX_CORNERS_OF_ELEM];
4205
4206 theMG = MYMG(theGrid);
4207
4208 // nodes are already inserted, so we know how many there are...
4209 if (theMG->facemap.bucket_count() <= 1)
4210 {
4211 // try to allocate the right size a-priori to avoid rehashing
4212 theMG->facemap.rehash(theMG->nodeIdCounter);
4213 // theMG->facemap.max_load_factor(1000);
4214 }
4215
4216 /* check parameters */
4217 #ifdef UG_DIM_2
4218 switch (n)
4219 {
4220 case 3 :
4221 tag = TRIANGLE;
4222 break;
4223 case 4 :
4224 tag = QUADRILATERAL;
4225 break;
4226 default :
4227 PrintErrorMessage('E',"InsertElement","only triangles and quadrilaterals allowed in 2D");
4228 return(NULL);
4229 }
4230 #endif
4231
4232 #ifdef UG_DIM_3
4233 switch (n)
4234 {
4235 case 4 :
4236 tag = TETRAHEDRON;
4237 break;
4238 case 5 :
4239 tag = PYRAMID;
4240 break;
4241 case 6 :
4242 tag = PRISM;
4243 break;
4244 case 8 :
4245 tag = HEXAHEDRON;
4246 break;
4247 default :
4248 PrintErrorMessage('E',"InsertElement","only tetrahedra, prisms, pyramids, and hexahedra are allowed in the 3D coarse grid");
4249 return(NULL);
4250 }
4251 #endif
4252
4253 /* init vertices */
4254 for (i=0; i<n; i++)
4255 {
4256 PRINTDEBUG(gm,1,("InsertElement(): node[%d]=" ID_FMTX "vertex[%d]=" VID_FMTX "\n",
4257 i,ID_PRTX(Node[i]),i,VID_PRTX(MYVERTEX(Node[i]))))
4258 Vertex[i] = MYVERTEX(Node[i]);
4259 }
4260
4261 #ifdef UG_DIM_2
4262 /* find orientation */
4263 if (!CheckOrientation(n,Vertex))
4264 {
4265 /* flip order */
4266 std::swap(Node[0], Node[n/2]);
4267 std::swap(Vertex[0], Vertex[n/2]);
4268
4269 if (!CheckOrientation(n,Vertex))
4270 {
4271 /* this was the only possibility for a triangle: so is a nonconvex quadrilateral */
4272 /* interchange first two nodes and try again */
4273 std::swap(Node[0], Node[1]);
4274 std::swap(Vertex[0], Vertex[1]);
4275 if (!CheckOrientation(n,Vertex))
4276 {
4277 /* flip order */
4278 std::swap(Node[0], Node[n/2]);
4279 std::swap(Vertex[0], Vertex[n/2]);
4280 if (!CheckOrientation(n,Vertex))
4281 {
4282 /* flip order back */
4283 std::swap(Node[0], Node[n/2]);
4284 std::swap(Vertex[0], Vertex[n/2]);
4285 /* interchange second two nodes and try again */
4286 std::swap(Node[1], Node[2]);
4287 std::swap(Vertex[1], Vertex[2]);
4288 if (!CheckOrientation(n,Vertex))
4289 {
4290 /* flip order */
4291 std::swap(Node[0], Node[n/2]);
4292 std::swap(Vertex[0], Vertex[n/2]);
4293 if (!CheckOrientation(n,Vertex))
4294 {
4295 PrintErrorMessage('E',"InsertElement",
4296 "cannot find orientation");
4297 return(NULL);
4298 }
4299 }
4300 }
4301 }
4302 }
4303 }
4304 #endif
4305
4306 #ifdef UG_DIM_3
4307 if (!CheckOrientation (n,Vertex))
4308 {
4309 sideNode[0] = Node[0];
4310 sideVertex[0] = Vertex[0];
4311 Node[0] = Node[1];
4312 Vertex[0] = Vertex[1];
4313 Node[1] = sideNode[0];
4314 Vertex[1] = sideVertex[0];
4315 }
4316 #endif
4317
4318 /* init pointers */
4319 for (i=0; i<SIDES_OF_TAG(tag); i++)
4320 {
4321 Neighbor[i] = NULL;
4322 bnds[i] = NULL;
4323 }
4324
4325 /* compute side information (theSeg[i]==NULL) means inner side */
4326 ElementType = IEOBJ;
4327 for (i=0; i<SIDES_OF_TAG(tag); i++)
4328 {
4329 m = CORNERS_OF_SIDE_TAG(tag,i);
4330 for(j=0; j<m; j++ )
4331 {
4332 k = CORNER_OF_SIDE_TAG(tag,i,j);
4333 sideNode[j] = Node[k];
4334 sideVertex[j] = Vertex[k];
4335 }
4336 bool found = false;
4337 for(j=0; j<m; j++ )
4338 {
4339 if( OBJT(sideVertex[j]) == IVOBJ ) found = true;
4340 }
4341 if( found ) continue;
4342
4343 /* all vertices of side[i] are on the boundary now */
4344
4345 /* We now assume, that: */
4346 /* if bnds_flag!=NULL && bnds_flag[i]!=0 there has to be a bnds */
4347 /* so, if not -->error */
4348 /* or: if bnds_flag==NULL, the domain decides whether there */
4349 /* should be a bnds or not (never an error) */
4350
4351 for (j=0; j<m; j++)
4352 bndp[j] = V_BNDP(sideVertex[j]);
4353
4354 if (bnds_flag==NULL)
4355 {
4356 bnds[i] = BNDP_CreateBndS(MGHEAP(theMG),bndp,m);
4357 if (bnds[i] != NULL) ElementType = BEOBJ;
4358 }
4359 else if (bnds_flag[i]!=0)
4360 {
4361 bnds[i] = BNDP_CreateBndS(MGHEAP(theMG),bndp,m);
4362 assert(bnds[i]!=NULL);
4363 ElementType = BEOBJ;
4364 }
4365 }
4366
4367 /* create the new Element */
4368 theElement = CreateElement(theGrid,tag,ElementType,Node,NULL,0);
4369 if (theElement==NULL)
4370 {
4371 PrintErrorMessage('E',"InsertElement","cannot allocate element");
4372 return(NULL);
4373 }
4374
4375 if (ElemList == NULL)
4376 {
4377 /* using the fast O(n) algorithm */
4378 NeighborSearch_O_n(tag, theElement, Node, theMG, NeighborSide, Neighbor);
4379 }
4380 else
4381 {
4382 /* use given neighboring elements */
4383 if ( (rv = Neighbor_Direct_Insert(tag, ElemList, NbgSdList, NeighborSide, Neighbor)) == 1)
4384 {
4385 PrintErrorMessage('E',"InsertElement"," ERROR by calling Neighbor_Direct_Insert()");
4386 return(NULL);
4387 }
4388 }
4389
4390 /* create element sides if necessary */
4391 if (OBJT(theElement)==BEOBJ)
4392 for (i=0; i<SIDES_OF_ELEM(theElement); i++)
4393 SET_BNDS(theElement,i,bnds[i]);
4394
4395 /* fill element data */
4396 for (i=0; i<SIDES_OF_ELEM(theElement); i++)
4397 {
4398 SET_NBELEM(theElement,i,Neighbor[i]);
4399 if (Neighbor[i]!=NULL)
4400 {
4401 if (NbgSdList == NULL)
4402 NeighborSide[i] = SideOfNbElement(theElement,i);
4403 if (NeighborSide[i] >= MAX_SIDES_OF_ELEM) {
4404 PrintErrorMessage('E',"InsertElement",
4405 "neighbor relation inconsistent");
4406 return(NULL);
4407 }
4408 SET_NBELEM(Neighbor[i],NeighborSide[i],theElement);
4409 #ifdef UG_DIM_3
4410 if (VEC_DEF_IN_OBJ_OF_GRID(theGrid,SIDEVEC))
4411 if (DisposeDoubledSideVector(theGrid,Neighbor[i],
4412 NeighborSide[i],theElement,i))
4413 return(NULL);
4414 #endif
4415 }
4416 }
4417
4418 SET_EFATHER(theElement,NULL);
4419 SETECLASS(theElement,RED_CLASS);
4420
4421 return(theElement);
4422 }
4423
4424 /****************************************************************************/
4425 /** \brief Delete an element
4426
4427 * @param theMG - multigrid structure
4428 * @param theElement - element to delete
4429
4430 This function deletes an element from level 0.
4431
4432 @return <ul>
4433 <li> GM_OK if ok </li>
4434 <li> GM_ERROR when error occurred. </li>
4435 </ul> */
4436 /****************************************************************************/
4437
4438 INT NS_DIM_PREFIX DeleteElement (MULTIGRID *theMG, ELEMENT *theElement) /* 3D VERSION */
4439 {
4440 GRID *theGrid;
4441 INT i,j,found;
4442
4443 /* check level */
4444 if (TOPLEVEL(theMG)!=0)
4445 {
4446 PrintErrorMessage('E',"DeleteElement",
4447 "only a multigrid with exactly one level can be edited");
4448 RETURN(GM_ERROR);
4449 }
4450 theGrid = GRID_ON_LEVEL(theMG,0);
4451
4452 /* delete pointers in neighbors */
4453 for (i=0; i<SIDES_OF_ELEM(theElement); i++)
4454 {
4455 ELEMENT *theNeighbor = NBELEM(theElement,i);
4456 if (theNeighbor!=NULL)
4457 {
4458 found = 0;
4459 for (j=0; j<SIDES_OF_ELEM(theNeighbor); j++)
4460 if (NBELEM(theNeighbor,j)==theElement)
4461 {
4462 found++;
4463 SET_NBELEM(theNeighbor,j,NULL);
4464 }
4465 if (found!=1) RETURN(GM_ERROR);
4466 }
4467 }
4468
4469 /* delete element now */
4470 DisposeElement(theGrid,theElement);
4471
4472 return(GM_OK);
4473 }
4474
4475
4476 /****************************************************************************/
4477 /** \brief Insert a mesh described by the domain
4478
4479 * @param theMG - multigrid structure
4480 * @param theMesh - mesh structure
4481
4482 This function inserts all nodes and elements given by the mesh.
4483
4484 @return <ul>
4485 <li> GM_OK if ok </li>
4486 <li> GM_ERROR when error occurred. </li>
4487 </ul> */
4488 /****************************************************************************/
4489
4490 INT NS_DIM_PREFIX InsertMesh (MULTIGRID *theMG, MESH *theMesh)
4491 {
4492 GRID *theGrid;
4493 ELEMENT *theElement;
4494 NODE **NList,*Nodes[MAX_CORNERS_OF_ELEM];
4495 VERTEX **VList;
4496 INT i,k,n,nv,j,maxlevel,l,move;
4497 INT ElemSideOnBnd[MAX_SIDES_OF_ELEM];
4498 INT MarkKey = MG_MARK_KEY(theMG);
4499
4500 if (theMesh == NULL) return(GM_OK);
4501 if (theMesh->nElements == NULL)
4502 {
4503 assert(theMesh->VertexLevel==NULL);
4504 theGrid = GRID_ON_LEVEL(theMG,0);
4505 for (i=0; i<theMesh->nBndP; i++)
4506 if (InsertBoundaryNode(theGrid,theMesh->theBndPs[i]) == NULL)
4507 REP_ERR_RETURN(GM_ERROR);
4508
4509 for (i=0; i<theMesh->nInnP; i++)
4510 if (InsertInnerNode(theGrid,theMesh->Position[i]) == NULL)
4511 REP_ERR_RETURN(GM_ERROR);
4512 return(GM_OK);
4513 }
4514
4515 /* prepare */
4516 nv = theMesh->nBndP + theMesh->nInnP;
4517 VList = (VERTEX **) GetTmpMem(MGHEAP(theMG),nv*sizeof(VERTEX *),MarkKey);
4518 if (VList == NULL) return(GM_ERROR);
4519 NList = (NODE **) GetTmpMem(MGHEAP(theMG),nv*sizeof(NODE *),MarkKey);
4520 if (NList == NULL) return(GM_ERROR);
4521 for (j=0; j<nv; j++) NList[j] = NULL;
4522
4523 maxlevel = 0;
4524 if (theMesh->VertexLevel!=NULL)
4525 {
4526 for (i=0; i<theMesh->nBndP; i++)
4527 {
4528 theGrid = GRID_ON_LEVEL(theMG,theMesh->VertexLevel[i]);
4529 VList[i] = CreateBoundaryVertex(theGrid);
4530 assert(VList[i]!=NULL);
4531 if (BNDP_Global(theMesh->theBndPs[i],CVECT(VList[i]))) assert(0);
4532 if (BNDP_BndPDesc(theMesh->theBndPs[i],&move))
4533 return(GM_OK);
4534 SETMOVE(VList[i],move);
4535 V_BNDP(VList[i]) = theMesh->theBndPs[i];
4536 maxlevel = std::max(maxlevel,(INT)theMesh->VertexLevel[i]);
4537 }
4538 for (i=theMesh->nBndP; i<nv; i++)
4539 {
4540 theGrid = GRID_ON_LEVEL(theMG,theMesh->VertexLevel[i]);
4541 VList[i] = CreateInnerVertex(theGrid);
4542 V_DIM_COPY(theMesh->Position[i-theMesh->nBndP],CVECT(VList[i]));
4543 maxlevel = std::max(maxlevel,(INT)theMesh->VertexLevel[i]);
4544 }
4545 }
4546 else
4547 {
4548 theGrid = GRID_ON_LEVEL(theMG,0);
4549 for (i=0; i<theMesh->nBndP; i++)
4550 {
4551 VList[i] = CreateBoundaryVertex(theGrid);
4552 assert(VList[i]!=NULL);
4553 if (BNDP_Global(theMesh->theBndPs[i],CVECT(VList[i]))) assert(0);
4554 if (BNDP_BndPDesc(theMesh->theBndPs[i],&move))
4555 return(GM_OK);
4556 SETMOVE(VList[i],move);
4557 V_BNDP(VList[i]) = theMesh->theBndPs[i];
4558 }
4559 for (i=theMesh->nBndP; i<nv; i++)
4560 {
4561 VList[i] = CreateInnerVertex(theGrid);
4562 V_DIM_COPY(theMesh->Position[i-theMesh->nBndP],CVECT(VList[i]));
4563 }
4564 }
4565 if (theMesh->nElements == NULL)
4566 return(GM_OK);
4567 for (j=1; j<=1; j++)
4568 for (k=0; k<theMesh->nElements[j]; k++)
4569 {
4570 if (theMesh->ElementLevel!=NULL) i = theMesh->ElementLevel[j][k];
4571 else i=0;
4572 theGrid = GRID_ON_LEVEL(theMG,i);
4573 n = theMesh->Element_corners[j][k];
4574 for (l=0; l<n; l++)
4575 {
4576 NODE *curListNode = NList[theMesh->Element_corner_ids[j][k][l]];
4577 if (curListNode == NULL || LEVEL(curListNode) < i)
4578 {
4579 Nodes[l] = CreateNode(theGrid,VList[theMesh->Element_corner_ids[j][k][l]],NULL,LEVEL_0_NODE);
4580 if (Nodes[l]==NULL) assert(0);
4581 NList[theMesh->Element_corner_ids[j][k][l]] = Nodes[l];
4582 if (curListNode == NULL || LEVEL(curListNode) < i-1)
4583 {
4584 SETNFATHER(Nodes[l],NULL);
4585 }
4586 else
4587 {
4588 SETNFATHER(Nodes[l],(GEOM_OBJECT *)curListNode);
4589 SONNODE(curListNode) = Nodes[l];
4590 }
4591 }
4592 else
4593 {
4594 Nodes[l] = curListNode;
4595 }
4596 }
4597 if (theMesh->ElemSideOnBnd==NULL)
4598 theElement = InsertElement (theGrid,n,Nodes,NULL,NULL,NULL);
4599 else
4600 {
4601 for (l=0; l<SIDES_OF_TAG(REF2TAG(n)); l++) ElemSideOnBnd[l] = (theMesh->ElemSideOnBnd[j][k]&(1<<l));
4602 theElement = InsertElement (theGrid,n,Nodes,NULL,NULL,ElemSideOnBnd);
4603 }
4604 SETSUBDOMAIN(theElement,j);
4605 }
4606
4607 return(GM_OK);
4608 }
4609
4610 /****************************************************************************/
4611 /** \todo Please doc me!
4612 InnerBoundary -
4613
4614 SYNOPSIS:
4615 INT InnerBoundary (ELEMENT *t, INT side);
4616
4617
4618 * @param t
4619 * @param side
4620
4621 DESCRIPTION:
4622
4623 @return
4624 INT
4625 */
4626 /****************************************************************************/
4627
4628 INT NS_DIM_PREFIX InnerBoundary (ELEMENT *t, INT side)
4629 {
4630 INT left,right;
4631
4632 ASSERT(OBJT(t) == BEOBJ);
4633 ASSERT(SIDE_ON_BND(t,side));
4634
4635 BNDS_BndSDesc(ELEM_BNDS(t,side),&left,&right);
4636
4637 return((left != 0) && (right != 0));
4638 }
4639
4640
4641 /****************************************************************************/
4642 /** \brief Calculate the center of mass for an element
4643 *
4644 * @param theElement the element
4645 * @param center_of_mass center of mass as the result
4646 *
4647 * This function calculates the center of mass for an arbitrary element.
4648 * DOUBLE_VECTOR is an array for a 2D resp. 3D coordinate.
4649 *
4650 * \sa DOUBLE_VECTOR, ELEMENT
4651 */
4652 /****************************************************************************/
4653
4654 void NS_DIM_PREFIX CalculateCenterOfMass(ELEMENT *theElement, DOUBLE_VECTOR center_of_mass)
4655 {
4656 INT i, nr_corners;
4657
4658 nr_corners = CORNERS_OF_ELEM(theElement);
4659 V_DIM_CLEAR(center_of_mass);
4660
4661 for (i=0; i<nr_corners; i++)
4662 {
4663 const FieldVector<DOUBLE,DIM>& corner = CVECT(MYVERTEX(CORNER(theElement,i)));
4664 V_DIM_ADD(center_of_mass,corner,center_of_mass);
4665 }
4666
4667 V_DIM_SCALE(1.0/nr_corners,center_of_mass);
4668 }
4669
4670 /****************************************************************************/
4671 /** \brief Calculate an (hopefully) unique key for the geometric object
4672
4673 * @param obj - geometric object which from the key is needed (can be one of VERTEX, ELEMENT, NODE or VECTOR)
4674
4675 This function calculates an (hopefully) unique key for VERTEX,
4676 ELEMENT, NODE, EDGE and VECTOR typed objects.
4677
4678 The heuristic is: calculate a 2D/3D position for the geometric object and
4679 transform this position to a single number by a weighted summation of the
4680 leading digits of the 2 resp. 3 coordinates and taking from this again
4681 the sigificant digits and adding the level number.
4682
4683 APPLICATION:
4684 Use always an explicit cast to avoid compiler warnings, e.g.
4685 NODE *theNode;
4686 KeyForObject((KEY_OBJECT *)theNode);
4687
4688 * \sa VERTEX, ELEMENT, NODE, EDGE, VECTOR
4689
4690 @return
4691 * the resulting key
4692 */
4693 /****************************************************************************/
4694
4695 INT NS_DIM_PREFIX KeyForObject( KEY_OBJECT *obj )
4696 {
4697 int dummy,i; /* dummy variable */
4698 DOUBLE_VECTOR coord;
4699
4700 if (obj==NULL) return (-1);
4701 switch( OBJT(obj) )
4702 {
4703 /* vertex */
4704 case BVOBJ :
4705 case IVOBJ : /* both together cover all vertex types */
4706 return LEVEL(obj)+COORDINATE_TO_KEY(CVECT((VERTEX*)obj),&dummy);
4707
4708 /* element */
4709 case BEOBJ :
4710 case IEOBJ : for (i=0; i<CORNERS_OF_ELEM((ELEMENT*)obj); i++)
4711 {
4712 if(CORNER((ELEMENT*)obj,i)==NULL)
4713 return (-1);
4714 if(MYVERTEX(CORNER((ELEMENT*)obj,i))==NULL)
4715 return (-1);
4716 }
4717 /* both together cover all element types */
4718 CalculateCenterOfMass( (ELEMENT*)obj, coord );
4719 return LEVEL(obj)+COORDINATE_TO_KEY(coord,&dummy);
4720
4721 /* node */
4722 case NDOBJ : if( MYVERTEX((NODE*)obj) == NULL )
4723 return (-1);
4724 return LEVEL(obj)+COORDINATE_TO_KEY(CVECT(MYVERTEX((NODE*)obj)),&dummy);
4725
4726 /* vector */
4727 case VEOBJ : if( VOBJECT((VECTOR*)obj) == NULL )
4728 return (-1);
4729 VectorPosition( (VECTOR*)obj, coord );
4730 return LEVEL(obj)+COORDINATE_TO_KEY(coord,&dummy);
4731
4732 /* edge */
4733 case EDOBJ : if( NBNODE(LINK0((EDGE*)obj)) == NULL )
4734 return (-1);
4735 if( MYVERTEX(NBNODE(LINK0((EDGE*)obj))) == NULL )
4736 return (-1);
4737 if( NBNODE(LINK1((EDGE*)obj)) == NULL )
4738 return (-1);
4739 if( MYVERTEX(NBNODE(LINK1((EDGE*)obj))) == NULL )
4740 return (-1);
4741 V_DIM_CLEAR(coord);
4742 /* sum of the coordinates of the 2 edge corners */
4743 V_DIM_ADD(coord,CVECT(MYVERTEX(NBNODE(LINK0((EDGE*)obj)))),coord);
4744 V_DIM_ADD(coord,CVECT(MYVERTEX(NBNODE(LINK1((EDGE*)obj)))),coord);
4745 /* the midpoint of the line is half of the sum */
4746 V_DIM_SCALE(0.5,coord);
4747 /* return the key of the midpoint as the key for the edge */
4748 return LEVEL(obj)+COORDINATE_TO_KEY(coord,&dummy);
4749
4750 default : snprintf( buffer, 4*256, "unrecognized object type %d", OBJT(obj) );
4751 PrintErrorMessage('E',"KeyForObject",buffer);
4752 return(0);
4753 }
4754 return (GM_ERROR);
4755 }
4756
4757 void NS_DIM_PREFIX ListMultiGridHeader (const INT longformat)
4758 {
4759 if (longformat)
4760 snprintf(buffer, 4*256," %-20.20s %-20.20s %-20.20s %10.10s %10.10s\n","mg name","domain name","problem name","heap size","heap used");
4761 else
4762 snprintf(buffer, 4*256," %-20.20s\n","mg name");
4763 }
4764
4765 /****************************************************************************/
4766 /** \brief List general information about multigrid structure
4767
4768 * @param theMG - structure to list
4769 * @param isCurrent - is `theMG` current multigrid
4770 * @param longformat - print all information or only name of `theMG`
4771
4772 This function lists general information about a multigrid structure.
4773
4774 */
4775 /****************************************************************************/
4776
4777 void NS_DIM_PREFIX ListMultiGrid (const MULTIGRID *theMG, const INT isCurrent, const INT longformat)
4778 {
4779 char c;
4780
4781 c = isCurrent ? '*' : ' ';
4782
4783 if (longformat)
4784 UserWriteF(" %c %-20.20s %-20.20s\n",c,ENVITEM_NAME(theMG),
4785 theMG->BVP_Name.c_str());
4786 else
4787 UserWriteF(" %c %-20.20s\n",c,ENVITEM_NAME(theMG));
4788 }
4789
4790 /****************************************************************************/
4791 /** \brief List information about refinement type distribution
4792
4793 * @param theMG - structure to list
4794 * @param gridflag -
4795 * @param greenflag
4796 * @param lbflag
4797 * @param verbose
4798
4799 This function lists information about multigrids element types.
4800
4801 * \todo Please return value!
4802 */
4803 /****************************************************************************/
4804
4805 INT NS_DIM_PREFIX MultiGridStatus (const MULTIGRID *theMG, INT gridflag, INT greenflag, INT lbflag, INT verbose)
4806 {
4807 INT i,j,sons,maxsons;
4808 INT red, green, yellow;
4809 INT mg_red,mg_green,mg_yellow;
4810 INT mg_greenrulesons[MAXLEVEL+1][MAX_SONS+1],mg_greenrules[MAXLEVEL+1];
4811 INT markcount[MAXLEVEL+1],closuresides[MAXLEVEL+1];
4812 FLOAT sum,sum_div_red,redplusgreen_div_red;
4813 FLOAT mg_sum,mg_sum_div_red,mg_redplusgreen_div_red;
4814 ELEMENT *theElement;
4815 GRID *theGrid;
4816 #ifdef ModelP
4817 INT MarkKey;
4818 INT total_elements,sum_elements;
4819 INT master_elements,hghost_elements,vghost_elements,vhghost_elements;
4820 #endif
4821
4822 #ifdef ModelP
4823 const auto& ppifContext = theMG->ppifContext();
4824 const int me = ppifContext.me();
4825 const int procs = ppifContext.procs();
4826 #endif
4827
4828 mg_red = mg_green = mg_yellow = mg_sum = 0;
4829 mg_sum_div_red = mg_redplusgreen_div_red = 0.0;
4830
4831 for (i=0; i<MAXLEVEL+1; i++)
4832 {
4833 /* for greenflag */
4834 mg_greenrules[i] = 0;
4835 for (j=0; j<MAX_SONS+1; j++) mg_greenrulesons[i][j] = 0;
4836 maxsons = 0;
4837
4838 /* for gridflag */
4839 markcount[i] = 0;
4840 closuresides[i] = 0;
4841 }
4842
4843 #ifdef ModelP
4844 MarkTmpMem(MGHEAP(theMG),&MarkKey);
4845 std::vector<int> infobuffer((procs+1)*(MAXLEVEL+1)*ELEMENT_PRIOS, 0);
4846 std::vector<int*> lbinfo(procs+1);
4847 for (i=0; i<procs+1; i++)
4848 lbinfo[i] = &infobuffer[i*(MAXLEVEL+1)*ELEMENT_PRIOS];
4849
4850 total_elements = sum_elements = 0;
4851 master_elements = hghost_elements = vghost_elements = vhghost_elements = 0;
4852 #endif
4853
4854 if (verbose && gridflag)
4855 {
4856 UserWriteF("\nMULTIGRID STATISTICS:\n");
4857 UserWriteF("LEVEL RED GREEN YELLOW SUM SUM/RED (RED+GREEN)/RED\n");
4858 }
4859
4860 /* compute multi grid infos */
4861 for (i=0; i<=TOPLEVEL(theMG); i++)
4862 {
4863 theGrid = GRID_ON_LEVEL(theMG,i);
4864 red = green = yellow = 0;
4865 sum = sum_div_red = redplusgreen_div_red = 0.0;
4866
4867 for (theElement=PFIRSTELEMENT(theGrid); theElement!=NULL;
4868 theElement=SUCCE(theElement))
4869 {
4870 SETUSED(theElement,0);
4871 /* count eclasses */
4872 switch (ECLASS(theElement))
4873 {
4874 case RED_CLASS : red++; break;
4875 case GREEN_CLASS : green++; break;
4876 case YELLOW_CLASS : yellow++; break;
4877 default : assert(0);
4878 }
4879 /* count marks and closuresides */
4880 if (EstimateHere(theElement))
4881 {
4882 ELEMENT *MarkElement = ELEMENT_TO_MARK(theElement);
4883 INT marktype = GetRefinementMarkType(theElement);
4884
4885 if (marktype==1 &&
4886 USED(MarkElement)==0)
4887 {
4888 markcount[LEVEL(MarkElement)]++;
4889 markcount[MAXLEVEL]++;
4890 for (j=0; j<SIDES_OF_ELEM(MarkElement); j++)
4891 {
4892 ELEMENT *NbElement = NBELEM(MarkElement,j);
4893 if (NbElement!=NULL && MARKCLASS(NbElement)==RED_CLASS)
4894 {
4895 closuresides[LEVEL(MarkElement)]++;
4896 closuresides[MAXLEVEL]++;
4897 }
4898 }
4899 SETUSED(MarkElement,1);
4900 }
4901 }
4902 /* green refinement statistics */
4903 switch (REFINECLASS(theElement))
4904 {
4905 case GREEN_CLASS :
4906 sons = NSONS(theElement);
4907 mg_greenrulesons[i][sons]++;
4908 mg_greenrulesons[i][MAX_SONS]+=sons;
4909 mg_greenrules[i]++;
4910 mg_greenrulesons[MAXLEVEL][sons]++;
4911 mg_greenrulesons[MAXLEVEL][MAX_SONS]+=sons;
4912 mg_greenrules[MAXLEVEL]++;
4913 if (maxsons < sons) maxsons = sons;
4914 break;
4915 default :
4916 break;
4917 }
4918 #ifdef ModelP
4919 /* count master, hghost, vghost and vhghost elements */
4920 switch (EPRIO(theElement))
4921 {
4922 case PrioMaster :
4923 lbinfo[me][ELEMENT_PRIOS*i]++;
4924 lbinfo[me][ELEMENT_PRIOS*MAXLEVEL]++;
4925 break;
4926 case PrioHGhost :
4927 lbinfo[me][ELEMENT_PRIOS*i+1]++;
4928 lbinfo[me][ELEMENT_PRIOS*MAXLEVEL+1]++;
4929 break;
4930 case PrioVGhost :
4931 lbinfo[me][ELEMENT_PRIOS*i+2]++;
4932 lbinfo[me][ELEMENT_PRIOS*MAXLEVEL+2]++;
4933 break;
4934 case PrioVHGhost :
4935 lbinfo[me][ELEMENT_PRIOS*i+3]++;
4936 lbinfo[me][ELEMENT_PRIOS*MAXLEVEL+3]++;
4937 break;
4938 default :
4939 printf( PFMT "MultiGridStatus: wrong element prio %d\n",me,EPRIO(theElement));
4940 assert(0);
4941 break;
4942 }
4943 #endif
4944 }
4945 sum = red + green + yellow;
4946 if (red > 0)
4947 {
4948 sum_div_red = sum / red;
4949 redplusgreen_div_red = ((float)(red+green)) / red;
4950 }
4951 else
4952 {
4953 sum_div_red = 0.0;
4954 redplusgreen_div_red = 0.0;
4955 }
4956
4957 if (verbose && gridflag)
4958 UserWriteF(" %2d %9d %9d %9d %9.0f %2.3f %2.3f\n",
4959 i,red,green,yellow,sum,sum_div_red,redplusgreen_div_red);
4960
4961 mg_red += red;
4962 mg_green += green;
4963 mg_yellow += yellow;
4964 mg_sum += sum;
4965 }
4966 if (mg_red > 0)
4967 {
4968 mg_sum_div_red = mg_sum / mg_red;
4969 mg_redplusgreen_div_red = ((float)(mg_red + mg_green)) / mg_red;
4970 }
4971 else
4972 {
4973 mg_sum_div_red = 0.0;
4974 mg_redplusgreen_div_red = 0.0;
4975 }
4976
4977 if (verbose && gridflag)
4978 UserWriteF(" ALL %9d %9d %9d %9.0f %2.3f %2.3f\n",
4979 mg_red,mg_green,mg_yellow,mg_sum,mg_sum_div_red,mg_redplusgreen_div_red);
4980
4981 /* set heap info in refine info */
4982 if (gridflag)
4983 {
4984 float New;
4985 float newpergreen;
4986
4987 SETMARKCOUNT(REFINEINFO(theMG),markcount[MAXLEVEL]);
4988
4989 New = markcount[MAXLEVEL]*(2<<(DIM-1))*mg_sum_div_red;
4990 SETPREDNEW0(REFINEINFO(theMG),New);
4991
4992 if (mg_greenrules[MAXLEVEL] > 0)
4993 newpergreen = ((float)mg_greenrulesons[MAXLEVEL][MAX_SONS])/mg_greenrules[MAXLEVEL];
4994 else
4995 newpergreen = 0;
4996 New = markcount[MAXLEVEL]*(2<<(DIM-1))+newpergreen*closuresides[MAXLEVEL];
4997 SETPREDNEW1(REFINEINFO(theMG),New);
4998
4999 SETREAL(REFINEINFO(theMG),mg_sum);
5000 }
5001
5002 /* list heap info */
5003 if (verbose && gridflag)
5004 {
5005 UserWriteF(" EST %2d ELEMS=%9.0f MARKCOUNT=%9.0f PRED_NEW0=%9.0f PRED_NEW1=%9.0f\n",
5006 REFINESTEP(REFINEINFO(theMG)),REAL(REFINEINFO(theMG)),MARKCOUNT(REFINEINFO(theMG)),
5007 PREDNEW0(REFINEINFO(theMG)),PREDNEW1(REFINEINFO(theMG)));
5008 UserWriteF(" EST TRACE step=%d\n",refine_info.step);
5009 for (i=0; i<refine_info.step; i++)
5010 UserWriteF(" EST %2d ELEMS=%9.0f MARKS=%9.0f REAL=%9.0f PRED0=%9.0f PRED1=%9.0f\n",
5011 i,refine_info.real[i],refine_info.markcount[i],
5012 ((i<refine_info.step) ? refine_info.real[i+1]-refine_info.real[i] : 0),
5013 refine_info.predicted_new[i][0],
5014 refine_info.predicted_new[i][1]);
5015 }
5016
5017 /* compute and list green rule info */
5018 if (verbose && greenflag)
5019 {
5020 UserWriteF("\nGREEN RULE STATISTICS:\n");
5021 UserWriteF(" LEVEL GREENSONS RULES GREENSONS/RUL");
5022 for (j=0; j<8 && j<maxsons; j++) UserWriteF(" %1d/%2d/...",j,j+8);
5023 UserWriteF("\n");
5024
5025 for (i=0; i<=TOPLEVEL(theMG); i++)
5026 {
5027 UserWriteF(" %2d %9d %9d %2.3f",i,mg_greenrulesons[i][MAX_SONS],
5028 mg_greenrules[i],
5029 (mg_greenrules[i]!=0) ? ((float)mg_greenrulesons[i][MAX_SONS])/mg_greenrules[i] : 0);
5030 for (j=0; j<maxsons; j++)
5031 {
5032 UserWriteF(" %9d",mg_greenrulesons[i][j]);
5033 if ((j+1)%8 == 0) UserWriteF("\n%41s"," ");
5034 }
5035 UserWriteF("\n");
5036 }
5037 UserWriteF(" ALL %9d %9d %2.3f",mg_greenrulesons[MAXLEVEL][MAX_SONS],
5038 mg_greenrules[MAXLEVEL],
5039 (mg_greenrules[MAXLEVEL]) ? ((float)mg_greenrulesons[MAXLEVEL][MAX_SONS])/
5040 mg_greenrules[MAXLEVEL] : 0.0);
5041 for (j=0; j<maxsons; j++)
5042 {
5043 UserWriteF(" %9d",mg_greenrulesons[MAXLEVEL][j]);
5044 if ((j+1)%8 == 0) UserWriteF("\n%41s"," ");
5045 }
5046 UserWriteF("\n");
5047 }
5048
5049 #ifdef ModelP
5050 /* compute and list loadbalancing info */
5051 if (verbose && lbflag)
5052 {
5053 UserWriteF("\nLB INFO:\n");
5054 /* now collect lb info on master */
5055 if (ppifContext.isMaster())
5056 {
5057 std::vector<VChannelPtr> mych(procs, nullptr);
5058
5059 for (i=1; i<procs; i++)
5060 {
5061 mych[i] =ConnSync(ppifContext, i,3917);
5062 RecvSync(ppifContext, mych[i],(void *)lbinfo[i],(MAXLEVEL+1)*ELEMENT_PRIOS*sizeof(INT));
5063 }
5064 Synchronize(ppifContext);
5065 for (i=1; i<procs; i++)
5066 {
5067 DiscSync(ppifContext, mych[i]);
5068 }
5069 }
5070 else
5071 {
5072 VChannelPtr mych;
5073
5074 mych = ConnSync(ppifContext, ppifContext.master(), 3917);
5075 SendSync(ppifContext, mych,(void *)lbinfo[me],(MAXLEVEL+1)*ELEMENT_PRIOS*sizeof(INT));
5076 Synchronize(ppifContext);
5077 DiscSync(ppifContext, mych);
5078 ReleaseTmpMem(MGHEAP(theMG),MarkKey);
5079 return(GM_OK);
5080 }
5081
5082 /* sum levels over procs */
5083 for (i=0; i<procs; i++)
5084 {
5085 for (j=0; j<TOPLEVEL(theMG)+1; j++)
5086 {
5087 lbinfo[procs][ELEMENT_PRIOS*j] += lbinfo[i][ELEMENT_PRIOS*j];
5088 lbinfo[procs][ELEMENT_PRIOS*j+1] += lbinfo[i][ELEMENT_PRIOS*j+1];
5089 lbinfo[procs][ELEMENT_PRIOS*j+2] += lbinfo[i][ELEMENT_PRIOS*j+2];
5090 lbinfo[procs][ELEMENT_PRIOS*j+3] += lbinfo[i][ELEMENT_PRIOS*j+3];
5091 }
5092 }
5093
5094 /* only master */
5095 if (lbflag >= 3)
5096 {
5097 UserWriteF(" LEVEL");
5098 for (i=0; i<ELEMENT_PRIOS*(TOPLEVEL(theMG)+1); i++)
5099 {
5100 UserWriteF(" %9d",i/ELEMENT_PRIOS);
5101 }
5102 UserWrite("\n");
5103 UserWriteF("PROC ");
5104 for (i=0; i<ELEMENT_PRIOS*(TOPLEVEL(theMG)+1); i++)
5105 {
5106 UserWriteF(" %9s",(i%ELEMENT_PRIOS==0) ? "MASTER" : (i%ELEMENT_PRIOS==1) ? "HGHOST" :
5107 (i%ELEMENT_PRIOS==2) ? "VGHOST" : "VHGHOST");
5108 }
5109 UserWrite("\n");
5110 for (i=0; i<procs; i++)
5111 {
5112 UserWriteF("%4d ",i);
5113 for (j=0; j<ELEMENT_PRIOS*(TOPLEVEL(theMG)+1); j++)
5114 {
5115 UserWriteF(" %9d",lbinfo[i][j]);
5116 }
5117 UserWrite("\n");
5118 }
5119 UserWriteF("\n");
5120 }
5121
5122 if (lbflag >= 2)
5123 {
5124 float memeff;
5125
5126 UserWriteF("%5s %9s %9s %9s %9s %9s %6s\n",
5127 "LEVEL","SUM","MASTER","HGHOST","VGHOST","VHGHOST","MEMEFF");
5128 for (i=0; i<=TOPLEVEL(theMG); i++)
5129 {
5130 sum_elements = lbinfo[procs][ELEMENT_PRIOS*i]+lbinfo[procs][ELEMENT_PRIOS*i+1]+
5131 lbinfo[procs][ELEMENT_PRIOS*i+2]+lbinfo[procs][ELEMENT_PRIOS*i+3];
5132 if (sum_elements > 0)
5133 memeff = ((float)lbinfo[procs][ELEMENT_PRIOS*i])/sum_elements*100;
5134 else
5135 memeff = 0.0;
5136 UserWriteF("%4d %9d %9d %9d %9d %9d %3.2f\n",i,sum_elements,
5137 lbinfo[procs][ELEMENT_PRIOS*i],lbinfo[procs][ELEMENT_PRIOS*i+1],
5138 lbinfo[procs][ELEMENT_PRIOS*i+2],lbinfo[procs][ELEMENT_PRIOS*i+3],memeff);
5139 }
5140 UserWrite("\n");
5141
5142 UserWriteF("%4s %9s %9s %9s %9s %9s %6s\n",
5143 "PROC","SUM","MASTER","HGHOST","VGHOST","VHGHOST","MEMEFF");
5144 for (i=0; i<procs; i++)
5145 {
5146 sum_elements = lbinfo[i][ELEMENT_PRIOS*MAXLEVEL]+lbinfo[i][ELEMENT_PRIOS*MAXLEVEL+1]+
5147 lbinfo[i][ELEMENT_PRIOS*MAXLEVEL+2]+lbinfo[i][ELEMENT_PRIOS*MAXLEVEL+3];
5148 if (sum_elements > 0)
5149 memeff = ((float)lbinfo[i][ELEMENT_PRIOS*MAXLEVEL])/sum_elements*100;
5150 else
5151 memeff = 0.0;
5152 UserWriteF("%4d %9d %9d %9d %9d %9d %3.2f\n",i,sum_elements,
5153 lbinfo[i][ELEMENT_PRIOS*MAXLEVEL],lbinfo[i][ELEMENT_PRIOS*MAXLEVEL+1],
5154 lbinfo[i][ELEMENT_PRIOS*MAXLEVEL+2],lbinfo[i][ELEMENT_PRIOS*MAXLEVEL+3],memeff);
5155 }
5156 UserWrite("\n");
5157 }
5158
5159 if (lbflag >= 1)
5160 {
5161 float memeff;
5162
5163 for (i=0; i<procs; i++)
5164 {
5165 master_elements += lbinfo[i][ELEMENT_PRIOS*MAXLEVEL];
5166 hghost_elements += lbinfo[i][ELEMENT_PRIOS*MAXLEVEL+1];
5167 vghost_elements += lbinfo[i][ELEMENT_PRIOS*MAXLEVEL+2];
5168 vhghost_elements += lbinfo[i][ELEMENT_PRIOS*MAXLEVEL+3];
5169 }
5170 total_elements = master_elements + hghost_elements + vghost_elements;
5171 if (total_elements > 0)
5172 memeff = ((float)master_elements)/total_elements*100;
5173 else
5174 memeff = 0.0;
5175 UserWriteF("%9s %9s %9s %9s %9s %6s\n","TOTAL","MASTER","HGHOST","VGHOST","VHGHOST","MEMEFF");
5176 UserWriteF("%9d %9d %9d %9d %9d %3.2f\n",total_elements,master_elements,hghost_elements,
5177 vghost_elements,vhghost_elements,memeff);
5178 }
5179
5180 }
5181 ReleaseTmpMem(MGHEAP(theMG),MarkKey);
5182 #endif
5183
5184 return (GM_OK);
5185 }
5186
5187 /****************************************************************************/
5188 /** \brief List general information about grids of multigrid
5189
5190 * @param theMG - multigrid structure
5191
5192 This function lists general information about the grids of a multigrid.
5193
5194 */
5195 /****************************************************************************/
5196
5197 void NS_DIM_PREFIX ListGrids (const MULTIGRID *theMG)
5198 {
5199 GRID *theGrid;
5200 ELEMENT *theElement,*NBElem;
5201 VERTEX *myVertex,*nbVertex,*v0,*v1;
5202 NODE *theNode,*n0,*n1;
5203 EDGE *theEdge;
5204 LINK *theLink;
5205 VECTOR *vec;
5206 DOUBLE hmin,hmax,h;
5207 INT l,minl,i,soe,eos,coe,side,e;
5208 INT nn,ne,nt,ns,nvec,nc;
5209
5210 UserWriteF("grids of '%s':\n",ENVITEM_NAME(theMG));
5211
5212 UserWrite("level maxlevel #vert #node #edge #elem #side #vect #conn");
5213 UserWrite(" minedge maxedge\n");
5214
5215 for (l=0; l<=TOPLEVEL(theMG); l++)
5216 {
5217 theGrid = GRID_ON_LEVEL(theMG,l);
5218
5219 /* calculate minimal and maximal edge */
5220 hmin = MAX_C;
5221 hmax = 0.0;
5222 for (theNode=FIRSTNODE(theGrid); theNode!=NULL; theNode=SUCCN(theNode))
5223 {
5224 myVertex = MYVERTEX(theNode);
5225 for (theLink=START(theNode); theLink!=NULL; theLink=NEXT(theLink))
5226 {
5227 nbVertex = MYVERTEX(NBNODE(theLink));
5228 V_DIM_EUKLIDNORM_OF_DIFF(CVECT(myVertex),CVECT(nbVertex),h);
5229 hmin = std::min(hmin,h);
5230 hmax = std::max(hmax,h);
5231 }
5232 }
5233 ns = 0;
5234 for (theElement=PFIRSTELEMENT(theGrid); theElement!=NULL;
5235 theElement=SUCCE(theElement))
5236 if (OBJT(theElement) == BEOBJ)
5237 for (i=0; i<SIDES_OF_ELEM(theElement); i++)
5238 if (SIDE_ON_BND(theElement,i))
5239 ns++;
5240
5241 UserWriteF(" %3d %8d %8ld %8ld %8ld %8ld %8ld %8ld %9.3e %9.3e\n",l,TOPLEVEL(theMG),
5242 (long)NV(theGrid),(long)NN(theGrid),(long)NE(theGrid),(long)NT(theGrid),
5243 (long)ns,(long)NVEC(theGrid),(float)hmin,(float)hmax);
5244 }
5245
5246 /* surface grid */
5247 minl = TOPLEVEL(theMG);
5248 hmin = MAX_C;
5249 hmax = 0.0;
5250 nn = ne = nt = ns = nvec = nc = 0;
5251 for (l=0; l<=TOPLEVEL(theMG); l++)
5252 {
5253 theGrid = GRID_ON_LEVEL(theMG,l);
5254
5255 /* reset USED flags in all objects to be counted */
5256 for (theNode=FIRSTNODE(theGrid); theNode!=NULL; theNode=SUCCN(theNode))
5257 {
5258 SETUSED(theNode,0);
5259 for (theLink=START(theNode); theLink!=NULL; theLink=NEXT(theLink))
5260 SETUSED(MYEDGE(theLink),0);
5261 }
5262
5263 /* count vectors and connections */
5264 for (vec=FIRSTVECTOR(theGrid); vec!=NULL; vec=SUCCVC(vec))
5265 if ((l==TOPLEVEL(theMG)) || (VNCLASS(vec)<1))
5266 {
5267 nvec++;
5268 }
5269
5270 /* count other objects */
5271 for (theElement=PFIRSTELEMENT(theGrid); theElement!=NULL; theElement=SUCCE(theElement))
5272 if ((NSONS(theElement)==0) || (l==TOPLEVEL(theMG)))
5273 {
5274 nt++;
5275
5276 minl = std::min(minl,l);
5277
5278 coe = CORNERS_OF_ELEM(theElement);
5279 for (i=0; i<coe; i++)
5280 {
5281 theNode = CORNER(theElement,i);
5282 if (USED(theNode)) continue;
5283 SETUSED(theNode,1);
5284
5285 if ((SONNODE(theNode)==NULL) || (l==TOPLEVEL(theMG)))
5286 nn++;
5287 }
5288
5289 soe = SIDES_OF_ELEM(theElement);
5290 for (side=0; side<soe; side++)
5291 {
5292 if (OBJT(theElement)==BEOBJ)
5293 if (ELEM_BNDS(theElement,side)!=NULL) ns++;
5294
5295 /* check neighbour element */
5296 if (l<TOPLEVEL(theMG))
5297 if ((NBElem=NBELEM(theElement,side))!=NULL)
5298 if (NSONS(NBElem)>0)
5299 continue; /* objects of this side will be counted by the neighbour */
5300
5301 eos = EDGES_OF_SIDE(theElement,side);
5302 for (i=0; i<eos; i++)
5303 {
5304 e = EDGE_OF_SIDE(theElement,side,i);
5305 n0 = CORNER(theElement,CORNER_OF_EDGE(theElement,e,0));
5306 v0 = MYVERTEX(n0);
5307 n1 = CORNER(theElement,CORNER_OF_EDGE(theElement,e,1));
5308 v1 = MYVERTEX(n1);
5309
5310 if ((theEdge=GetEdge(n0,n1))==NULL) continue;
5311 if (USED(theEdge)) continue;
5312 SETUSED(theEdge,1);
5313
5314 /* any sons ? */
5315 if (SONNODE(n0)!=NULL && SONNODE(n1)!=NULL)
5316 if (GetEdge(SONNODE(n0),SONNODE(n1))!=NULL) continue;
5317 if (MIDNODE(theEdge) != NULL)
5318 {
5319 if (SONNODE(n0)!=NULL)
5320 if (GetEdge(MIDNODE(theEdge),SONNODE(n0))!=NULL) continue;
5321 if (SONNODE(n1)!=NULL)
5322 if (GetEdge(MIDNODE(theEdge),SONNODE(n1))!=NULL) continue;
5323 }
5324 ne++;
5325
5326 V_DIM_EUKLIDNORM_OF_DIFF(CVECT(v0),CVECT(v1),h);
5327 hmin = std::min(hmin,h);
5328 hmax = std::max(hmax,h);
5329 }
5330 }
5331 }
5332 }
5333
5334 UserWrite("\nsurface grid up to top level:\n");
5335 UserWriteF("%c %3d %8d %8s %8ld %8ld %8ld %8ld %8ld %8ld %9.3e %9.3e\n",' ',minl,(int)TOPLEVEL(theMG),
5336 "---",(long)nn,(long)ne,(long)nt,
5337 (long)ns,(long)nvec,(long)nc,(float)hmin,(float)hmax);
5338
5339 #ifdef ModelP
5340 /* surface grid up to current level */
5341 minl = TOPLEVEL(theMG);
5342 hmin = MAX_C;
5343 hmax = 0.0;
5344 nn = ne = nt = ns = nvec = nc = 0;
5345 for (l=0; l<=TOPLEVEL(theMG); l++)
5346 {
5347 theGrid = GRID_ON_LEVEL(theMG,l);
5348
5349 /* reset USED flags in all objects to be counted */
5350 for (theNode=FIRSTNODE(theGrid); theNode!=NULL; theNode=SUCCN(theNode))
5351 {
5352 SETUSED(theNode,0);
5353 for (theLink=START(theNode); theLink!=NULL; theLink=NEXT(theLink))
5354 SETUSED(MYEDGE(theLink),0);
5355 }
5356 /* count vectors and connections */
5357 for (vec=FIRSTVECTOR(theGrid); vec!=NULL; vec=SUCCVC(vec))
5358 if ((l==TOPLEVEL(theMG)) || (VNCLASS(vec)<1))
5359 if (PRIO(vec) == PrioMaster) nvec++;
5360
5361 /* count other objects */
5362 for (theElement=FIRSTELEMENT(theGrid);
5363 theElement!=NULL; theElement=SUCCE(theElement))
5364 if (EstimateHere(theElement))
5365 {
5366 nt++;
5367
5368 minl = std::min(minl,l);
5369
5370 coe = CORNERS_OF_ELEM(theElement);
5371 for (i=0; i<coe; i++)
5372 {
5373 theNode = CORNER(theElement,i);
5374 if (USED(theNode)) continue;
5375 SETUSED(theNode,1);
5376
5377 if ((SONNODE(theNode)==NULL) || (l==TOPLEVEL(theMG)))
5378 if (PRIO(theNode) == PrioMaster) nn++;
5379 }
5380
5381 soe = SIDES_OF_ELEM(theElement);
5382 for (side=0; side<soe; side++)
5383 {
5384 if (OBJT(theElement)==BEOBJ)
5385 if (ELEM_BNDS(theElement,side)!=NULL) ns++;
5386
5387 /* check neighbour element */
5388 if (l<TOPLEVEL(theMG))
5389 if ((NBElem=NBELEM(theElement,side))!=NULL)
5390 if (NSONS(NBElem)>0)
5391 continue; /* objects of this side will be counted by the neighbour */
5392
5393 eos = EDGES_OF_SIDE(theElement,side);
5394 for (i=0; i<eos; i++)
5395 {
5396 e = EDGE_OF_SIDE(theElement,side,i);
5397 n0 = CORNER(theElement,CORNER_OF_EDGE(theElement,e,0));
5398 v0 = MYVERTEX(n0);
5399 n1 = CORNER(theElement,CORNER_OF_EDGE(theElement,e,1));
5400 v1 = MYVERTEX(n1);
5401 V_DIM_EUKLIDNORM_OF_DIFF(CVECT(v0),CVECT(v1),h);
5402 hmin = std::min(hmin,h);
5403 hmax = std::max(hmax,h);
5404 }
5405 }
5406 }
5407 }
5408 nn = UG_GlobalSumINT(theMG->ppifContext(), nn);
5409 ne = UG_GlobalSumINT(theMG->ppifContext(), ne);
5410 nt = UG_GlobalSumINT(theMG->ppifContext(), nt);
5411 ns = UG_GlobalSumINT(theMG->ppifContext(), ns);
5412 nvec = UG_GlobalSumINT(theMG->ppifContext(), nvec);
5413 nc = UG_GlobalSumINT(theMG->ppifContext(), nc);
5414 hmin = UG_GlobalMinDOUBLE(theMG->ppifContext(), hmin);
5415 hmax = UG_GlobalMaxDOUBLE(theMG->ppifContext(), hmax);
5416 UserWrite("\nsurface of all processors up to top level:\n");
5417 UserWriteF("%c %3d %8d %8s %8ld %8s %8ld %8ld %8ld %8s %9.3e %9.3e\n",
5418 ' ',minl,TOPLEVEL(theMG),
5419 "---",(long)nn," ",(long)nt,
5420 (long)ns,(long)nvec," ",(float)hmin,(float)hmax);
5421 #endif
5422 }
5423
5424 /****************************************************************************/
5425 /** \brief List information about node in multigrid
5426
5427 * @param theMG - structure containing the node
5428 * @param theNode - node to list
5429 * @param dataopt - list user data if true
5430 * @param bopt - list boundary info if true
5431 * @param nbopt - list info about neighbors if true
5432 * @param vopt - list more information
5433
5434 This function lists information about a node in a multigrid.
5435
5436 */
5437 /****************************************************************************/
5438
5439 void NS_DIM_PREFIX ListNode (const MULTIGRID *theMG, const NODE *theNode, INT dataopt, INT bopt, INT nbopt, INT vopt)
5440 {
5441 VERTEX *theVertex;
5442 LINK *theLink;
5443 INT i;
5444
5445 theVertex = MYVERTEX(theNode);
5446
5447 /******************************/
5448 /* print standard information */
5449 /******************************/
5450 /* line 1 */ UserWriteF("NODEID=" ID_FFMTE " CTRL=%8lx VEID="
5451 VID_FMTX " LEVEL=%2d",
5452 ID_PRTE(theNode),(long)CTRL(theNode),
5453 VID_PRTX(theVertex),LEVEL(theNode));
5454
5455 /* print coordinates of that node */
5456 for(i=0; i<DIM; i++)
5457 {
5458 UserWriteF(" x%1d=%11.4E",i, (float)(CVECT(theVertex)[i]) );
5459 }
5460 UserWrite("\n");
5461
5462 if (vopt) /* verbose: print all information */
5463 {
5464 /* print nfather information */
5465 if (NFATHER(theNode)!=NULL)
5466 {
5467 switch (NTYPE(theNode))
5468 {
5469 case (CORNER_NODE) :
5470 UserWriteF(" NFATHER(Node)=" ID_FMTX "\n",
5471 ID_PRTX((NODE *)NFATHER(theNode)));
5472 break;
5473 case (MID_NODE) :
5474 UserWriteF(" NFATHER(Edge)=" EDID_FMTX "\n",
5475 EDID_PRTX((EDGE *)NFATHER(theNode)));
5476 break;
5477 default :
5478 break;
5479 }
5480 }
5481 /* print nfather information */
5482 if (SONNODE(theNode)!=NULL)
5483 {
5484 UserWriteF(" SONNODE=" ID_FMTX "\n",ID_PRTX(SONNODE(theNode)));
5485 }
5486
5487 /* line 3 */ /* print vertex father information */
5488 if (VFATHER(theVertex)!=NULL)
5489 {
5490 UserWriteF(" VERTEXFATHER=" EID_FMTX " ",
5491 EID_PRTX(VFATHER(theVertex)));
5492 for(i=0; i<DIM; i++)
5493 {
5494 UserWriteF("XI[%d]=%11.4E ",i, (float)(LCVECT(theVertex)[i]) );
5495 }
5496 }
5497
5498 UserWriteF(" key=%d\n", KeyForObject((KEY_OBJECT *)theNode) );
5499
5500 UserWriteF(" classes: NCLASS = %d NNCLASS = %d\n",NCLASS(theNode),NNCLASS(theNode));
5501 }
5502
5503 /******************************/
5504 /* print boundary information */
5505 /******************************/
5506 if (bopt)
5507 {
5508 if (OBJT(theVertex) == BVOBJ)
5509 {
5510 if (BNDP_BndPDesc(V_BNDP(theVertex),&i))
5511 UserWrite("Error in boundary point\n");
5512 else
5513 UserWriteF("boundary point: move %d moved %d\n",i,
5514 MOVED(theVertex));
5515 }
5516 }
5517
5518 if (nbopt)
5519 {
5520 for (theLink=START(theNode); theLink!=NULL; theLink=NEXT(theLink))
5521 {
5522 #if defined UG_DIM_3 && defined ModelP
5523 UserWriteF(" EDGE=%x/%08x ",MYEDGE(theLink),
5524 DDD_InfoGlobalId(PARHDR(MYEDGE(theLink))));
5525 #else
5526 UserWrite(" ");
5527 #endif
5528 UserWriteF("NB=" ID_FMTX " CTRL=%8lx NO_OF_ELEM=%3d",
5529 ID_PRTX(NBNODE(theLink)),(long)CTRL(theLink),NO_OF_ELEM(MYEDGE(theLink)));
5530 if (MIDNODE(MYEDGE(theLink))!=NULL)
5531 UserWriteF(" MIDNODE=" ID_FMTX, ID_PRTX(MIDNODE(MYEDGE(theLink))));
5532 theVertex=MYVERTEX(NBNODE(theLink));
5533 for(i=0; i<DIM; i++)
5534 {
5535 UserWriteF(" x%1d=%11.4E",i, (float)(CVECT(theVertex)[i]) );
5536 }
5537 UserWrite("\n");
5538 }
5539 }
5540 }
5541
5542
5543 /****************************************************************************/
5544 /** \brief
5545 ListElement - List information about element
5546
5547 * @param theMG - structure to list
5548 * @param theElement - element to list
5549 * @param dataopt - list user data if true
5550 * @param vopt - list more information
5551
5552 This function lists information about an element
5553
5554 */
5555 /****************************************************************************/
5556
5557 void NS_DIM_PREFIX ListElement (const MULTIGRID *theMG, const ELEMENT *theElement, INT dataopt, INT bopt, INT nbopt, INT vopt)
5558 {
5559 char etype[10];
5560 char ekind[8];
5561 int i,j;
5562 ELEMENT *SonList[MAX_SONS];
5563
5564 if (DIM==2)
5565 switch (TAG(theElement))
5566 {
5567 case TRIANGLE : strcpy(etype,"TRI"); break;
5568 case QUADRILATERAL : strcpy(etype,"QUA"); break;
5569 default : strcpy(etype,"???"); break;
5570 }
5571 else
5572 switch (TAG(theElement))
5573 {
5574 case TETRAHEDRON : strcpy(etype,"TET"); break;
5575 case PYRAMID : strcpy(etype,"PYR"); break;
5576 case PRISM : strcpy(etype,"PRI"); break;
5577 case HEXAHEDRON : strcpy(etype,"HEX"); break;
5578 default : strcpy(etype,"???"); break;
5579 }
5580 switch (ECLASS(theElement))
5581 {
5582 case YELLOW_CLASS : strcpy(ekind,"YELLOW "); break;
5583 case GREEN_CLASS : strcpy(ekind,"GREEN "); break;
5584 case RED_CLASS : strcpy(ekind,"RED "); break;
5585 default : strcpy(ekind,"??? "); break;
5586 }
5587 UserWriteF("ELEMID=" EID_FFMTE " %5s %5s CTRL=%8lx FLAG=%8lx REFINE=%2d MARK=%2d LEVEL=%2d",
5588 EID_PRTE(theElement),ekind,etype,
5589 (long)CTRL(theElement),(long)FLAG(theElement),REFINE(theElement),MARK(theElement),LEVEL(theElement));
5590 if (COARSEN(theElement)) UserWrite(" COARSEN");
5591 UserWrite("\n");
5592
5593 if (vopt)
5594 {
5595 UserWriteF("subdomain=%d \n", SUBDOMAIN(theElement));
5596 for (i=0; i<CORNERS_OF_ELEM(theElement); i++)
5597 {
5598 UserWriteF(" N%d=" ID_FMTX,i,ID_PRTX(CORNER(theElement,i)));
5599 }
5600 UserWriteF("\n");
5601 if (EFATHER(theElement))
5602 UserWriteF(" FA=" EID_FMTX ,EID_PRTX(EFATHER(theElement)));
5603 else
5604 UserWriteF(" FA=NULL");
5605
5606 UserWriteF(" NSONS=%d\n",NSONS(theElement));
5607
5608 if (GetAllSons(theElement,SonList)!=0) return;
5609 for (i=0; i<MAX_SONS && SonList[i] != NULL; i++)
5610 {
5611 UserWriteF(" S%d=" EID_FMTX ,i,EID_PRTX(SonList[i]));
5612 if ((i+1)%4 == 0) UserWrite("\n");
5613 }
5614
5615 }
5616 if (nbopt)
5617 {
5618 for (i=0; i<SIDES_OF_ELEM(theElement); i++)
5619 if (NBELEM(theElement,i)!=NULL)
5620 {
5621 UserWriteF(" NB%d=" EID_FMTX ,
5622 i,EID_PRTX(NBELEM(theElement,i)));
5623 }
5624 UserWrite("\n");
5625 }
5626 if (bopt)
5627 {
5628 UserWrite(" ");
5629 if (OBJT(theElement)==BEOBJ)
5630 {
5631 for (i=0; i<SIDES_OF_ELEM(theElement); i++)
5632 {
5633 for(j=0; j<CORNERS_OF_SIDE(theElement,i); j++)
5634 {
5635 #if defined(ModelP) && defined(UG_DIM_3)
5636 UserWriteF(" NODE[ID=%ld]: ",
5637 (long)(ID(CORNER(theElement,
5638 CORNER_OF_SIDE(theElement,i,j)))));
5639 #endif
5640 UserWrite("\n");
5641 }
5642 }
5643 }
5644 UserWrite("\n");
5645 }
5646 }
5647
5648
5649 /****************************************************************************/
5650 /** \brief List information about vector
5651
5652 * @param theMG - multigrid structure to list
5653 * @param theVector - vector to list
5654 * @param dataopt - list user data if true
5655 * @param modifiers - flags modifying output style and verbose level
5656
5657 This function lists information about a vector.
5658
5659 */
5660 /****************************************************************************/
5661
5662 void NS_DIM_PREFIX ListVector (const MULTIGRID *theMG, const VECTOR *theVector, INT dataopt, INT modifiers)
5663 {
5664 DOUBLE_VECTOR pos;
5665
5666 /* print index and type of vector */
5667 UserWriteF("IND=" VINDEX_FFMTE " VTYPE=%d(SIDEVECTOR) ",
5668 VINDEX_PRTE(theVector));
5669
5670 if (READ_FLAG(modifiers,LV_POS))
5671 {
5672 if (VectorPosition(theVector,pos))
5673 return;
5674 #ifdef UG_DIM_2
5675 UserWriteF("POS=(%10.2e,%10.2e)",pos[0],pos[1]);
5676 #endif
5677 #ifdef UG_DIM_3
5678 UserWriteF("POS=(%10.2e,%10.2e,%10.2e)",pos[0],pos[1],pos[2]);
5679 #endif
5680 }
5681
5682 /* print object type of vector */
5683 if (READ_FLAG(modifiers,LV_VO_INFO))
5684 switch (VOTYPE(theVector))
5685 {
5686 #ifdef UG_DIM_3
5687 case SIDEVEC :
5688 {
5689 ELEMENT *theElement = (ELEMENT*)VOBJECT(theVector);
5690 UserWriteF("SIDE-V elemID=" EID_FFMT
5691 " ",
5692 EID_PRT(theElement));
5693 }
5694 break;
5695 #endif
5696
5697 default : PrintErrorMessage( 'E', "ListVector", "unrecognized VECTOR type" );
5698 assert(0);
5699 }
5700
5701 UserWriteF("VCLASS=%1d VNCLASS=%1d",VCLASS(theVector),VNCLASS(theVector));
5702 UserWriteF(" key=%d\n", KeyForObject((KEY_OBJECT *)theVector) );
5703 }
5704
5705 /****************************************************************************/
5706 /** \brief Returns highest Node class of a dof on next level
5707
5708 * @param theElement - pointer to a element
5709
5710 This function returns highest 'NCLASS' of a Node associated with the
5711 element.
5712
5713 @return <ul>
5714 <li> 0 if ok </li>
5715 <li> 1 if error occurred. </li>
5716 </ul> */
5717 /****************************************************************************/
5718
5719 static INT MaxNodeClass (const ELEMENT *theElement)
5720 {
5721 INT m = 0;
5722 INT i;
5723
5724 for (i=0; i<CORNERS_OF_ELEM(theElement); i++) {
5725 INT c = NCLASS(CORNER(theElement,i));
5726
5727 m = std::max(m,c);
5728 }
5729
5730 return (m);
5731 }
5732
5733 /****************************************************************************/
5734 /** \brief Returns highest Node class of a dof on next level
5735
5736 * @param theElement - pointer to a element
5737
5738 This function returns highest 'NNCLASS' of a Node associated with the
5739 element.
5740
5741 @return <ul>
5742 <li> 0 if ok </li>
5743 <li> 1 if error occurred. </li>
5744 </ul> */
5745 /****************************************************************************/
5746
5747 INT NS_DIM_PREFIX MaxNextNodeClass (const ELEMENT *theElement)
5748 {
5749 INT m = 0;
5750 INT i;
5751
5752 for (i=0; i<CORNERS_OF_ELEM(theElement); i++) {
5753 INT c = NNCLASS(CORNER(theElement,i));
5754
5755 m = std::max(m,c);
5756 }
5757
5758 return (m);
5759 }
5760
5761 /****************************************************************************/
5762 /** \brief Returns minimal Node class of a dof on next level
5763
5764 * @param theElement - pointer to a element
5765
5766 This function returns highest 'NNCLASS' of a Node associated with the
5767 element.
5768
5769 @return <ul>
5770 <li> 0 if ok </li>
5771 <li> 1 if error occurred. </li>
5772 </ul> */
5773 /****************************************************************************/
5774
5775 INT NS_DIM_PREFIX MinNodeClass (const ELEMENT *theElement)
5776 {
5777 INT m = 3;
5778 INT i;
5779
5780 for (i=0; i<CORNERS_OF_ELEM(theElement); i++) {
5781 INT c = NCLASS(CORNER(theElement,i));
5782
5783 m = std::min(m,c);
5784 }
5785
5786 return (m);
5787 }
5788
5789 /****************************************************************************/
5790 /** \brief Returns minimal Node class of a dof on next level
5791
5792 * @param theElement - pointer to a element
5793
5794 This function returns highest 'NNCLASS' of a Node associated with the
5795 element.
5796
5797 @return <ul>
5798 <li> 0 if ok </li>
5799 <li> 1 if error occurred. </li>
5800 </ul> */
5801 /****************************************************************************/
5802
5803 INT NS_DIM_PREFIX MinNextNodeClass (const ELEMENT *theElement)
5804 {
5805 INT m = 3;
5806 INT i;
5807
5808 for (i=0; i<CORNERS_OF_ELEM(theElement); i++) {
5809 INT c = NNCLASS(CORNER(theElement,i));
5810
5811 m = std::min(m,c);
5812 }
5813
5814 return (m);
5815 }
5816
5817 /****************************************************************************/
5818 /** \brief Initialize node classes
5819
5820 * @param theGrid - given grid
5821 * @param theElement - given element
5822
5823 Initialize Node class in all nodes associated with given element with 3.
5824
5825 @return <ul>
5826 <li> 0 if ok </li>
5827 <li> 1 if error occurred. </li>
5828 </ul> */
5829 /****************************************************************************/
5830
5831 INT NS_DIM_PREFIX SeedNodeClasses (ELEMENT *theElement)
5832 {
5833 INT i;
5834
5835 for (i=0; i<CORNERS_OF_ELEM(theElement); i++)
5836 SETNCLASS(CORNER(theElement,i),3);
5837
5838 return (0);
5839 }
5840
5841 /****************************************************************************/
5842 /** \brief Reset node classes
5843
5844 * @param theGrid - pointer to grid
5845
5846 Reset all node classes in all nodes of given grid to 0.
5847
5848 @return <ul>
5849 <li> 0 if ok </li>
5850 <li> 1 if error occurred. </li>
5851 </ul> */
5852 /****************************************************************************/
5853
5854 INT NS_DIM_PREFIX ClearNodeClasses (GRID *theGrid)
5855 {
5856 NODE *theNode;
5857
5858 /* reset class of each Node to 0 */
5859 for (theNode=PFIRSTNODE(theGrid); theNode!=NULL; theNode=SUCCN(theNode))
5860 SETNCLASS(theNode,0);
5861
5862 return(0);
5863 }
5864
5865 #ifdef ModelP
5866 static int Gather_NodeClass (DDD::DDDContext&, DDD_OBJ obj, void *data)
5867 {
5868 NODE *theNode = (NODE *)obj;
5869
5870 ((INT *)data)[0] = NCLASS(theNode);
5871
5872 return(0);
5873 }
5874
5875 static int Scatter_NodeClass (DDD::DDDContext&, DDD_OBJ obj, void *data)
5876 {
5877 NODE *theNode = (NODE *)obj;
5878
5879 SETNCLASS(theNode,std::max((INT)NCLASS(theNode),((INT *)data)[0]));
5880
5881 return(0);
5882 }
5883
5884 static int Scatter_GhostNodeClass (DDD::DDDContext&, DDD_OBJ obj, void *data)
5885 {
5886 NODE *theNode = (NODE *)obj;
5887
5888 SETNCLASS(theNode,((INT *)data)[0]);
5889
5890 return(0);
5891 }
5892 #endif
5893
5894 /****************************************************************************/
5895 /** \brief Compute Node classes after initialization
5896
5897 * @param theGrid - pointer to grid
5898
5899 After Node classes have been reset and initialized, this function
5900 now computes the class 2 and class 1 Nodes.
5901
5902 @return <ul>
5903 <li> 0 if ok </li>
5904 <li> 1 if error occurred </li>
5905 </ul> */
5906 /****************************************************************************/
5907 static INT PropagateNodeClass (GRID *theGrid, INT nclass)
5908 {
5909 ELEMENT *theElement;
5910 INT i;
5911
5912 for (theElement=FIRSTELEMENT(theGrid);
5913 theElement!= NULL; theElement = SUCCE(theElement))
5914 if (MaxNodeClass(theElement) == nclass)
5915 for (i=0; i<CORNERS_OF_ELEM(theElement); i++)
5916 {
5917 NODE *theNode = CORNER(theElement,i);
5918
5919 if (NCLASS(theNode) < nclass)
5920 SETNCLASS(theNode,nclass-1);
5921 }
5922
5923 /* only for this values valid */
5924 ASSERT(nclass==3 || nclass==2);
5925
5926 return(0);
5927 }
5928
5929 INT NS_DIM_PREFIX PropagateNodeClasses (GRID *theGrid)
5930 {
5931 #ifdef ModelP
5932 auto& context = theGrid->dddContext();
5933 const auto& dddctrl = ddd_ctrl(context);
5934
5935 PRINTDEBUG(gm,1,("\n" PFMT "PropagateNodeClasses():"
5936 " 1. communication on level %d\n",theGrid->ppifContext().me(),GLEVEL(theGrid)))
5937 /* exchange NCLASS of Nodes */
5938 DDD_IFAExchange(context,
5939 dddctrl.BorderNodeSymmIF,GRID_ATTR(theGrid),sizeof(INT),
5940 Gather_NodeClass, Scatter_NodeClass);
5941 #endif
5942
5943 /* set Node classes in the algebraic neighborhood to 2 */
5944 if (PropagateNodeClass(theGrid,3)) REP_ERR_RETURN(1);
5945
5946 #ifdef ModelP
5947 PRINTDEBUG(gm,1,("\n" PFMT "PropagateNodeClasses(): 2. communication\n",
5948 theGrid->ppifContext().me()))
5949 /* exchange NCLASS of Nodes */
5950 DDD_IFAExchange(context,
5951 dddctrl.BorderNodeSymmIF,GRID_ATTR(theGrid),sizeof(INT),
5952 Gather_NodeClass, Scatter_NodeClass);
5953 #endif
5954
5955 /* set Node classes in the algebraic neighborhood to 1 */
5956 if (PropagateNodeClass(theGrid,2)) REP_ERR_RETURN(1);
5957
5958 #ifdef ModelP
5959 PRINTDEBUG(gm,1,("\n" PFMT "PropagateNodeClasses(): 3. communication\n",
5960 theGrid->ppifContext().me()))
5961 /* exchange NCLASS of Nodes */
5962 DDD_IFAExchange(context,
5963 dddctrl.BorderNodeSymmIF,GRID_ATTR(theGrid),sizeof(INT),
5964 Gather_NodeClass, Scatter_NodeClass);
5965
5966 /* send NCLASS to ghosts */
5967 DDD_IFAOneway(context,
5968 dddctrl.NodeIF,GRID_ATTR(theGrid),IF_FORWARD,sizeof(INT),
5969 Gather_NodeClass, Scatter_GhostNodeClass);
5970 #endif
5971
5972 return(0);
5973 }
5974
5975 /****************************************************************************/
5976 /** \brief Reset class of the Nodes on the next level
5977
5978 * @param theGrid - pointer to grid
5979
5980 This function clears NNCLASS flag in all Nodes. This is the first step to
5981 compute the class of the dofs on the *NEXT* level, which
5982 is also the basis for determining copies.
5983
5984 @return <ul>
5985 <li> 0 if ok </li>
5986 <li> 1 if error occurred. </li>
5987 </ul> */
5988 /****************************************************************************/
5989
5990 INT NS_DIM_PREFIX ClearNextNodeClasses (GRID *theGrid)
5991 {
5992 NODE *theNode;
5993
5994 /* reset class of each Node to 0 */
5995 for (theNode=PFIRSTNODE(theGrid); theNode!=NULL; theNode=SUCCN(theNode))
5996 SETNNCLASS(theNode,0);
5997
5998 /* now the refinement algorithm will initialize the class 3 Nodes */
5999 /* on the *NEXT* level. */
6000 return(0);
6001 }
6002
6003 /****************************************************************************/
6004 /** \brief Set 'NNCLASS' in all Nodes associated with element
6005
6006 * @param theElement - pointer to element
6007
6008 Set 'NNCLASS' in all nodes associated with the element to 3.
6009
6010 @return <ul>
6011 <li> 0 if ok </li>
6012 <li> 1 if error occurred. </li>
6013 </ul> */
6014 /****************************************************************************/
6015
6016 INT NS_DIM_PREFIX SeedNextNodeClasses (ELEMENT *theElement)
6017 {
6018 INT i;
6019
6020 for (i=0; i<CORNERS_OF_ELEM(theElement); i++)
6021 SETNNCLASS(CORNER(theElement,i),3);
6022
6023 return (0);
6024 }
6025
6026 /****************************************************************************/
6027 /** \brief
6028 PropagateNextNodeClasses - Compute 'NNCLASS' in all Nodes of a grid level
6029
6030 * @param theGrid - pointer to grid
6031
6032 Computes values of 'NNCLASS' field in all nodes after seed.
6033
6034 @return <ul>
6035 <li> 0 if ok </li>
6036 <li> 1 if error occurred </li>
6037 </ul> */
6038 /****************************************************************************/
6039
6040 #ifdef ModelP
6041 static int Gather_NextNodeClass (DDD::DDDContext&, DDD_OBJ obj, void *data)
6042 {
6043 NODE *theNode = (NODE *)obj;
6044
6045 ((INT *)data)[0] = NNCLASS(theNode);
6046
6047 return(GM_OK);
6048 }
6049
6050 static int Scatter_NextNodeClass (DDD::DDDContext&, DDD_OBJ obj, void *data)
6051 {
6052 NODE *theNode = (NODE *)obj;
6053
6054 SETNNCLASS(theNode,std::max((INT)NNCLASS(theNode),((INT *)data)[0]));
6055
6056 return(GM_OK);
6057 }
6058
6059 static int Scatter_GhostNextNodeClass (DDD::DDDContext&, DDD_OBJ obj, void *data)
6060 {
6061 NODE *theNode = (NODE *)obj;
6062
6063 SETNNCLASS(theNode,((INT *)data)[0]);
6064
6065 return(GM_OK);
6066 }
6067 #endif
6068
6069 static INT PropagateNextNodeClass (GRID *theGrid, INT nnclass)
6070 {
6071 ELEMENT *theElement;
6072 INT i;
6073
6074 for (theElement=FIRSTELEMENT(theGrid);
6075 theElement!= NULL; theElement = SUCCE(theElement))
6076 if (MaxNextNodeClass(theElement) == nnclass)
6077 for (i=0; i<CORNERS_OF_ELEM(theElement); i++)
6078 {
6079 NODE *theNode = CORNER(theElement,i);
6080
6081 if (NNCLASS(theNode) < nnclass)
6082 SETNNCLASS(theNode,nnclass-1);
6083 }
6084
6085 /* only for this values valid */
6086 ASSERT(nnclass==3 || nnclass==2);
6087
6088 return(0);
6089 }
6090
6091 INT NS_DIM_PREFIX PropagateNextNodeClasses (GRID *theGrid)
6092 {
6093 #ifdef ModelP
6094 auto& context = theGrid->dddContext();
6095 const auto& dddctrl = ddd_ctrl(context);
6096
6097 PRINTDEBUG(gm,1,("\n" PFMT "PropagateNextNodeClasses(): 1. communication\n",theGrid->ppifContext().me()))
6098 /* exchange NNCLASS of Nodes */
6099 DDD_IFAExchange(context,
6100 dddctrl.BorderNodeSymmIF,GRID_ATTR(theGrid),sizeof(INT),
6101 Gather_NextNodeClass, Scatter_NextNodeClass);
6102 #endif
6103
6104 if (PropagateNextNodeClass(theGrid,3)) REP_ERR_RETURN(1);
6105
6106 #ifdef ModelP
6107 PRINTDEBUG(gm,1,("\n" PFMT "PropagateNextNodeClasses(): 2. communication\n",theGrid->ppifContext().me()))
6108 /* exchange NNCLASS of Nodes */
6109 DDD_IFAExchange(context,
6110 dddctrl.BorderNodeSymmIF,GRID_ATTR(theGrid),sizeof(INT),
6111 Gather_NextNodeClass, Scatter_NextNodeClass);
6112 #endif
6113
6114 if (PropagateNextNodeClass(theGrid,2)) REP_ERR_RETURN(1);
6115
6116 #ifdef ModelP
6117 PRINTDEBUG(gm,1,("\n" PFMT "PropagateNextNodeClasses(): 3. communication\n",theGrid->ppifContext().me()))
6118 /* exchange NNCLASS of Nodes */
6119 DDD_IFAExchange(context,
6120 dddctrl.BorderNodeSymmIF,GRID_ATTR(theGrid),sizeof(INT),
6121 Gather_NextNodeClass, Scatter_NextNodeClass);
6122
6123 /* send NNCLASSn to ghosts */
6124 DDD_IFAOneway(context,
6125 dddctrl.NodeIF,GRID_ATTR(theGrid),IF_FORWARD,sizeof(INT),
6126 Gather_NextNodeClass, Scatter_GhostNextNodeClass);
6127 #endif
6128
6129 return(0);
6130 }
6131
6132 /****************************************************************************/
6133 /** \brief Set subdomain id on level 0 edges
6134
6135 * @param id - the id of the block to be allocated
6136
6137 This function sets the subdomain id taken from the elements for level 0 edges.
6138
6139 @return <ul>
6140 <li> GM_OK if ok </li>
6141 <li> GM_ERROR if error occurred </li>
6142 </ul> */
6143 /****************************************************************************/
6144
6145 static INT SetEdgeAndNodeSubdomainFromElements (GRID *theGrid)
6146 {
6147 ELEMENT *theElement;
6148 NODE *n0,*n1;
6149 EDGE *ed;
6150 INT s_id,s,i,k;
6151
6152 /* first set subdomain id for all edges */
6153 for (theElement=PFIRSTELEMENT(theGrid); theElement!=NULL;
6154 theElement=SUCCE(theElement))
6155 {
6156 /* all edges of the element acquire the subdomain id of the element */
6157 s_id = SUBDOMAIN(theElement);
6158 for (k=0; k<EDGES_OF_ELEM(theElement); k++)
6159 {
6160 n0 = CORNER(theElement,CORNER_OF_EDGE(theElement,k,0));
6161 n1 = CORNER(theElement,CORNER_OF_EDGE(theElement,k,1));
6162 ed = GetEdge(n0,n1);
6163 ASSERT(ed!=NULL);
6164 SETEDSUBDOM(ed,s_id);
6165 }
6166 for (i=0; i<CORNERS_OF_ELEM(theElement); i++)
6167 SETNSUBDOM(CORNER(theElement,i),s_id);
6168 }
6169
6170 /* now change subdomain id for boundary edges and nodes to 0 */
6171 for (theElement=PFIRSTELEMENT(theGrid); theElement!=NULL;
6172 theElement=SUCCE(theElement))
6173 if (OBJT(theElement)==BEOBJ)
6174 for (s=0; s<SIDES_OF_ELEM(theElement); s++)
6175 {
6176 if (ELEM_BNDS(theElement,s)==NULL)
6177 continue;
6178
6179 for (i=0; i<EDGES_OF_SIDE(theElement,s); i++)
6180 {
6181 k = EDGE_OF_SIDE(theElement,s,i);
6182 n0 = CORNER(theElement,CORNER_OF_EDGE(theElement,k,0));
6183 n1 = CORNER(theElement,CORNER_OF_EDGE(theElement,k,1));
6184 SETNSUBDOM(n0,0);
6185 ASSERT(OBJT(MYVERTEX(n0)) == BVOBJ);
6186 SETNSUBDOM(n1,0);
6187 ASSERT(OBJT(MYVERTEX(n1)) == BVOBJ);
6188 ed = GetEdge(n0,n1);
6189 ASSERT(ed!=NULL);
6190 SETEDSUBDOM(ed,0);
6191 }
6192 }
6193 IFDEBUG(gm,1)
6194 for (theElement=PFIRSTELEMENT(theGrid); theElement!=NULL;
6195 theElement=SUCCE(theElement))
6196 {
6197 PRINTDEBUG(gm,1,("el(%d)-sd=%d\n",ID(theElement),
6198 SUBDOMAIN(theElement)));
6199 for (k=0; k<EDGES_OF_ELEM(theElement); k++)
6200 {
6201 n0 = CORNER(theElement,CORNER_OF_EDGE(theElement,k,0));
6202 n1 = CORNER(theElement,CORNER_OF_EDGE(theElement,k,1));
6203 ed = GetEdge(n0,n1);
6204 PRINTDEBUG(gm,1,(" ed(%d,%d)-sd=%d nsub %d %d\n",ID(n0),ID(n1),
6205 EDSUBDOM(ed),NSUBDOM(n0),NSUBDOM(n1)));
6206 }
6207 }
6208 ENDDEBUG
6209
6210 return (GM_OK);
6211 }
6212
6213 /****************************************************************************/
6214 /** \brief
6215 RemoveSpuriousBoundarySides - remove boundary side of element and neighbour
6216
6217 * @param elem - element
6218 * @param side - boundary side to remove
6219
6220 This function removes the boundary side of element and neighbour.
6221
6222 @return <ul>
6223 <li> GM_OK if ok </li>
6224 <li> GM_ERROR if error occurred </li>
6225 </ul> */
6226 /****************************************************************************/
6227
6228 static INT RemoveSpuriousBoundarySides (HEAP *heap, ELEMENT *elem, INT side)
6229 {
6230 ELEMENT *nb=NBELEM(elem,side);
6231 BNDS *nbbside,*bside=ELEM_BNDS(elem,side);
6232 INT nbside;
6233
6234 ASSERT(bside!=NULL);
6235 ASSERT(OBJT(elem)==BEOBJ);
6236 ASSERT(nb!=NULL);
6237 ASSERT(OBJT(nb)==BEOBJ);
6238
6239 /* search nbside */
6240 for (nbside=0; nbside<SIDES_OF_ELEM(nb); nbside++)
6241 if (NBELEM(nb,nbside)==elem)
6242 break;
6243 ASSERT(nbside<SIDES_OF_ELEM(nb));
6244 nbbside = ELEM_BNDS(nb,nbside);
6245 ASSERT(nbbside!=NULL);
6246
6247 PRINTDEBUG(gm,1,("spurious bsides between elem %ld and elem %ld removed",(long)ID(elem),(long)ID(nb)));
6248
6249 if (BNDS_Dispose(heap,bside))
6250 REP_ERR_RETURN(1);
6251 SET_BNDS(elem,side,NULL);
6252
6253 if (BNDS_Dispose(heap,nbbside))
6254 REP_ERR_RETURN(2);
6255 SET_BNDS(nb,nbside,NULL);
6256
6257 return (0);
6258 }
6259
6260 /****************************************************************************/
6261 /** \brief Replace boundary by inner element
6262 *
6263 * @param grid grid where elem resides
6264 * @param elemH handle to element to be replaced: will point to new element
6265 *
6266 * This function replaces a boundary by an inner element.
6267 *
6268 * @return <ul>
6269 * <li> GM_OK if ok </li>
6270 * <li> GM_ERROR if error occurred </li>
6271 **</ul>
6272 */
6273 /****************************************************************************/
6274
6275 static INT BElem2IElem (GRID *grid, ELEMENT **elemH)
6276 {
6277 ELEMENT *nb[MAX_SIDES_OF_ELEM],*elem=*elemH,*ielem;
6278 NODE *nodes[MAX_CORNERS_OF_ELEM];
6279 INT i,j,nbside[MAX_SIDES_OF_ELEM],s_id;
6280
6281 ASSERT(GLEVEL(grid)==0);
6282
6283 /* save context */
6284 for (i=0; i<CORNERS_OF_ELEM(elem); i++)
6285 nodes[i] = CORNER(elem,i);
6286
6287 for (i=0; i<SIDES_OF_ELEM(elem); i++)
6288 {
6289 nb[i] = NBELEM(elem,i);
6290 for (j=0; j<SIDES_OF_ELEM(nb[i]); j++)
6291 if (NBELEM(nb[i],j)==elem)
6292 break;
6293 ASSERT(j<SIDES_OF_ELEM(nb[i]));
6294 nbside[i] = j;
6295 }
6296
6297 s_id = SUBDOMAIN(elem);
6298
6299 /* create/dispose */
6300 ielem = CreateElement(grid,TAG(elem),IEOBJ,nodes,EFATHER(elem),false);
6301 if (ielem==NULL)
6302 REP_ERR_RETURN(1);
6303
6304 if (DisposeElement(grid,elem))
6305 REP_ERR_RETURN(1);
6306
6307 *elemH = ielem;
6308
6309 /* set context */
6310 for (i=0; i<SIDES_OF_ELEM(ielem); i++)
6311 {
6312 SET_NBELEM(ielem,i,nb[i]);
6313
6314 SET_NBELEM(nb[i],nbside[i],ielem);
6315 }
6316 SETSUBDOMAIN(ielem,s_id);
6317 SETECLASS(ielem,RED_CLASS);
6318
6319 return (0);
6320 }
6321
6322 /****************************************************************************/
6323 /** \brief
6324 FinishGrid - remove erroneously introduced bsides and propagate sub domain IDs
6325
6326 * @param mg - multigrid
6327
6328 This function removes erroneously introduced bsides and propagates sub domain IDs.
6329
6330 @return <ul>
6331 <li> GM_OK if ok </li>
6332 <li> GM_ERROR if error occurred </li>
6333 </ul> */
6334 /****************************************************************************/
6335
6336 static INT FinishGrid (MULTIGRID *mg)
6337 {
6338 GRID *grid;
6339 ELEMENT *elem,*nb,*succ;
6340 HEAP *heap=MGHEAP(mg);
6341 FIFO unused,shell;
6342 INT MarkKey = MG_MARK_KEY(mg);
6343 INT side,nbid,s_id;
6344 INT *sd_table;
6345 void *fifoBuffer;
6346
6347 /* prepare */
6348 if (TOPLEVEL(mg)<0)
6349 REP_ERR_RETURN (GM_ERROR);
6350 grid = GRID_ON_LEVEL(mg,0);
6351 if (!NT(grid))
6352 return (GM_OK);
6353
6354 for (elem=PFIRSTELEMENT(grid); elem!=NULL; elem=SUCCE(elem))
6355 {
6356 SETUSED(elem,false);
6357 SETTHEFLAG(elem,false);
6358 }
6359
6360 /* table for subdomain ids */
6361 const INT nsd = 2;
6362 sd_table = (INT*)GetTmpMem(heap,nsd*sizeof(INT),MarkKey);
6363 if (sd_table==NULL)
6364 REP_ERR_RETURN (GM_ERROR);
6365
6366 /* init two fifos */
6367 fifoBuffer = (void *)GetTmpMem(heap, sizeof(ELEMENT*)*NT(grid), MarkKey);
6368 if (fifoBuffer == NULL)
6369 REP_ERR_RETURN (GM_ERROR);
6370 fifo_init(&unused, fifoBuffer, sizeof(ELEMENT*)*NT(grid));
6371 fifoBuffer = (void *)GetTmpMem(heap, sizeof(ELEMENT*)*NT(grid), MarkKey);
6372 if (fifoBuffer == NULL)
6373 REP_ERR_RETURN (GM_ERROR);
6374 fifo_init(&shell, fifoBuffer, sizeof(ELEMENT*)*NT(grid));
6375
6376 /* outermost loop handles nonconnected domains */
6377 while (true)
6378 {
6379 for (elem=PFIRSTELEMENT(grid); elem!=NULL; elem=SUCCE(elem))
6380 if (!USED(elem))
6381 break;
6382 if (elem!=NULL)
6383 fifo_in(&unused,elem);
6384 else
6385 break;
6386
6387 while (!fifo_empty(&unused))
6388 {
6389 /* grab next !USED element */
6390 do
6391 elem = (ELEMENT*) fifo_out(&unused);
6392 while (USED(elem) && !fifo_empty(&unused));
6393 if (USED(elem))
6394 /* we are done */
6395 break;
6396
6397 /* shell algo (using FLAG): neighbours, but not across bside */
6398 fifo_clear(&shell);
6399 fifo_in(&shell,elem);
6400 SETTHEFLAG(elem,true);
6401 for (INT i=0; i<=nsd; i++) sd_table[i] = 0;
6402 INT found = false;
6403 while (!fifo_empty(&shell))
6404 {
6405 elem = (ELEMENT*) fifo_out(&shell);
6406
6407 if (OBJT(elem)==BEOBJ)
6408 for (side=0; side<SIDES_OF_ELEM(elem); side++)
6409 if (SIDE_ON_BND(elem,side))
6410 {
6411 INT id;
6412 if (BNDS_BndSDesc(ELEM_BNDS(elem,side),&id,&nbid))
6413 REP_ERR_RETURN (GM_ERROR);
6414
6415 if ((nb=NBELEM(elem,side))==NULL)
6416 {
6417 /* this bside must be ok (outer boundary) */
6418 /* TODO (HRR 971012): parallel? */
6419 ASSERT(nbid==0);
6420 s_id = id;
6421 found = true;
6422 break;
6423 }
6424 else
6425 if (USED(nb))
6426 {
6427 /* he must know! */
6428 if (nbid==SUBDOMAIN(nb))
6429 s_id = id;
6430 else if (id==SUBDOMAIN(nb))
6431 s_id = nbid;
6432 else
6433 ASSERT(false);
6434 }
6435
6436 /* handle outer boundary cases */
6437 if (id==0)
6438 {
6439 ASSERT(nbid>0);
6440 s_id = nbid;
6441 found = true;
6442 break;
6443 }
6444 if (nbid==0)
6445 {
6446 ASSERT(id>0);
6447 s_id = id;
6448 found = true;
6449 break;
6450 }
6451
6452 ++sd_table[id];
6453 if (sd_table[id]>1)
6454 {
6455 s_id = id;
6456 found = true;
6457 break;
6458 }
6459 }
6460 if (found)
6461 break;
6462
6463 /* push neighbours not across boundary */
6464 if (OBJT(elem)==BEOBJ)
6465 {
6466 for (side=0; side<SIDES_OF_ELEM(elem); side++)
6467 if (!SIDE_ON_BND(elem,side))
6468 if ((nb=NBELEM(elem,side))!=NULL)
6469 if (!USED(nb) && !THEFLAG(nb))
6470 {
6471 fifo_in(&shell,nb);
6472 SETTHEFLAG(nb,true);
6473 }
6474 }
6475 else
6476 {
6477 for (side=0; side<SIDES_OF_ELEM(elem); side++)
6478 if ((nb=NBELEM(elem,side))!=NULL)
6479 if (!USED(nb) && !THEFLAG(nb))
6480 {
6481 fifo_in(&shell,nb);
6482 SETTHEFLAG(nb,true);
6483 }
6484 }
6485 }
6486
6487 /* count occurrences of subdom ids (max 2 different) */
6488 found=0;
6489 for (INT i=0; i<=nsd; i++)
6490 if (sd_table[i])
6491 found++;
6492 if (found>2)
6493 /* FATAL: algorithm relies on assumptions obviously not fulfilled! */
6494 ASSERT(false);
6495
6496 /* again shell algo starting from last element */
6497 /* set USED, propagate subdomain ids and remove spurious bsides (has to have NB!) */
6498 /* use PRINTDEBUG */
6499 fifo_clear(&shell);
6500 fifo_in(&shell,elem);
6501 SETUSED(elem,true);
6502 SETSUBDOMAIN(elem,s_id);
6503 while (!fifo_empty(&shell))
6504 {
6505 elem = (ELEMENT*) fifo_out(&shell);
6506
6507 if (OBJT(elem)==BEOBJ)
6508 {
6509 for (side=0; side<SIDES_OF_ELEM(elem); side++)
6510 if (SIDE_ON_BND(elem,side))
6511 {
6512 if ((nb=NBELEM(elem,side))==NULL)
6513 continue;
6514 if (!USED(nb))
6515 /* push unused neighbour across boundary to unused fifo */
6516 fifo_in(&unused,nb);
6517
6518 INT id;
6519 if (BNDS_BndSDesc(ELEM_BNDS(elem,side),&id,&nbid))
6520 REP_ERR_RETURN (GM_ERROR);
6521
6522 if (id!=s_id || nbid==0)
6523 {
6524 /* remove spurious bside of both elements */
6525 if (RemoveSpuriousBoundarySides(heap,elem,side))
6526 REP_ERR_RETURN(1);
6527 }
6528 }
6529 }
6530
6531 /* push neighbours not across boundary */
6532 if (OBJT(elem)==BEOBJ)
6533 {
6534 for (side=0; side<SIDES_OF_ELEM(elem); side++)
6535 if (!SIDE_ON_BND(elem,side))
6536 {
6537 if ((nb=NBELEM(elem,side))!=NULL)
6538 {
6539 if (!USED(nb))
6540 {
6541 fifo_in(&shell,nb);
6542 SETUSED(nb,true);
6543 SETSUBDOMAIN(nb,s_id);
6544 }
6545 }
6546 else
6547 /* TODO (HRR 971012): ModelP: no error if EGHOST? */
6548 /* grid not closed */
6549 REP_ERR_RETURN(1);
6550 }
6551 }
6552 else
6553 {
6554 for (side=0; side<SIDES_OF_ELEM(elem); side++)
6555 if ((nb=NBELEM(elem,side))!=NULL)
6556 {
6557 if (!USED(nb))
6558 {
6559 fifo_in(&shell,nb);
6560 SETUSED(nb,true);
6561 SETSUBDOMAIN(nb,s_id);
6562 }
6563 }
6564 else
6565 /* TODO (HRR 971012): ModelP: no error if EGHOST? */
6566 /* grid not closed */
6567 REP_ERR_RETURN(1);
6568 }
6569 }
6570 }
6571 }
6572
6573 for (elem=PFIRSTELEMENT(grid); elem!=NULL; elem=succ)
6574 {
6575 succ = SUCCE(elem);
6576
6577 if (OBJT(elem)!=BEOBJ) continue;
6578
6579 /* check whether element still has bsides */
6580 for (side=0; side<SIDES_OF_ELEM(elem); side++)
6581 if (ELEM_BNDS(elem,side)!=NULL)
6582 break;
6583 if (side>=SIDES_OF_ELEM(elem))
6584 if (BElem2IElem(grid,&elem))
6585 REP_ERR_RETURN(1);
6586 }
6587
6588 if (SetEdgeAndNodeSubdomainFromElements(grid))
6589 REP_ERR_RETURN (GM_ERROR);
6590
6591 return (GM_OK);
6592 }
6593
6594 /****************************************************************************/
6595 /** \brief
6596 SetSubdomainIDfromBndInfo - set subdomain id on level 0 elements and edges
6597
6598 * @param id - the id of the block to be allocated
6599
6600 This function sets the subdomain for level 0 elements and edges.
6601
6602 @return <ul>
6603 <li> GM_OK if ok </li>
6604 <li> GM_ERROR if error occurred </li>
6605 </ul> */
6606 /****************************************************************************/
6607
6608 INT NS_DIM_PREFIX SetSubdomainIDfromBndInfo (MULTIGRID *theMG)
6609 {
6610 HEAP *theHeap;
6611 GRID *theGrid;
6612 ELEMENT *theElement, *theNeighbor;
6613 NODE *theNode;
6614 void *fifoBuffer;
6615 INT i,n,id,nbid,j;
6616 FIFO myfifo;
6617 INT MarkKey = MG_MARK_KEY(theMG);
6618
6619 /* prepare */
6620 if (TOPLEVEL(theMG)<0) REP_ERR_RETURN (GM_ERROR);
6621 theGrid = GRID_ON_LEVEL(theMG,0);
6622 n = NT(theGrid); if (n==0) return(0);
6623
6624 /* allocate fifo and init */
6625 theHeap = MYMG(theGrid)->theHeap;
6626 fifoBuffer = (void *)GetTmpMem(theHeap, sizeof(ELEMENT*)*n, MarkKey);
6627 fifo_init(&myfifo, fifoBuffer, sizeof(ELEMENT*)*n);
6628 for (theElement=PFIRSTELEMENT(theGrid); theElement!=NULL;
6629 theElement=SUCCE(theElement))
6630 SETUSED(theElement,0);
6631
6632 /* insert all boundary elements */
6633 for (theElement=PFIRSTELEMENT(theGrid); theElement!=NULL;
6634 theElement=SUCCE(theElement))
6635 if (OBJT(theElement)==BEOBJ && !USED(theElement))
6636 {
6637 for (i=0; i<SIDES_OF_ELEM(theElement); i++)
6638 if (ELEM_BNDS(theElement,i)!=NULL)
6639 break;
6640 assert(i<SIDES_OF_ELEM(theElement));
6641
6642 /* set id from BNDS */
6643 if (BNDS_BndSDesc(ELEM_BNDS(theElement,i),&id,&nbid))
6644 REP_ERR_RETURN (GM_ERROR);
6645 assert(id>0);
6646 SETSUBDOMAIN(theElement,id);
6647 SETUSED(theElement,1);
6648 fifo_in(&myfifo,(void *)theElement);
6649 PRINTDEBUG(gm,1,("elem %3d sid %d\n",ID(theElement),SUBDOMAIN(theElement)));
6650 for (i=0; i<CORNERS_OF_ELEM(theElement); i++)
6651 {
6652 theNode = CORNER(theElement,i);
6653 if (OBJT(MYVERTEX(theNode))==IVOBJ)
6654 SETNSUBDOM(theNode,id);
6655 }
6656 for (i=0; i<SIDES_OF_ELEM(theElement); i++)
6657 {
6658 if (NBELEM(theElement,i)==NULL || SIDE_ON_BND(theElement,i)) continue;
6659 theNeighbor = NBELEM(theElement,i);
6660 if (USED(theNeighbor))
6661 assert(SUBDOMAIN(theElement)==SUBDOMAIN(theNeighbor));
6662 }
6663 }
6664
6665 /* set subdomain id for all elements */
6666 while(!fifo_empty(&myfifo))
6667 {
6668 theElement = (ELEMENT*)fifo_out(&myfifo);
6669 for (i=0; i<SIDES_OF_ELEM(theElement); i++)
6670 {
6671 if (NBELEM(theElement,i)==NULL) continue;
6672 theNeighbor = NBELEM(theElement,i);
6673 if (USED(theNeighbor))
6674 {
6675 if (INNER_SIDE(theElement,i))
6676 assert(SUBDOMAIN(theElement)==SUBDOMAIN(theNeighbor));
6677 continue;
6678 }
6679 SETSUBDOMAIN(theNeighbor,SUBDOMAIN(theElement));
6680 SETUSED(theNeighbor,1);
6681 for (j=0; j<CORNERS_OF_ELEM(theElement); j++)
6682 {
6683 theNode = CORNER(theElement,j);
6684 if (OBJT(MYVERTEX(theNode))==IVOBJ)
6685 SETNSUBDOM(theNode,SUBDOMAIN(theElement));
6686 }
6687 fifo_in(&myfifo,(void *)theNeighbor);
6688 }
6689 }
6690
6691 IFDEBUG(gm,1)
6692 for (theElement=PFIRSTELEMENT(theGrid); theElement!=NULL; theElement=SUCCE(theElement))
6693 assert(USED(theElement));
6694 ENDDEBUG
6695
6696 if (SetEdgeAndNodeSubdomainFromElements(theGrid))
6697 REP_ERR_RETURN (GM_ERROR);
6698
6699 return (GM_OK);
6700 }
6701
6702 /****************************************************************************/
6703 /** \brief Do all that is necessary to complete the coarse grid
6704
6705 * @param id - the id of the block to be allocated
6706
6707 This function does all that is necessary to complete the coarse grid.
6708 Finally the MG_COARSE_FIXED flag is set.
6709
6710 @return <ul>
6711 <li> GM_OK if ok </li>
6712 <li> GM_ERROR if error occurred </li>
6713 * </ul>
6714 */
6715 /****************************************************************************/
6716
6717 INT NS_DIM_PREFIX FixCoarseGrid (MULTIGRID *theMG)
6718 {
6719 if (MG_COARSE_FIXED(theMG)) return (GM_OK);
6720
6721 /** \todo (HRR 971031): check that before check-in!
6722 if (FinishGrid(theMG))
6723 REP_ERR_RETURN (GM_ERROR);*/
6724
6725 /** \todo (HRR 971031): remove if above works */
6726 if (SetSubdomainIDfromBndInfo(theMG))
6727 REP_ERR_RETURN (GM_ERROR);
6728
6729 /* set this flag here because it is checked by CreateAlgebra */
6730 if (CreateAlgebra(theMG) != GM_OK)
6731 REP_ERR_RETURN (GM_ERROR);
6732
6733 /* here all temp memory since CreateMultiGrid is released */
6734 ReleaseTmpMem(MGHEAP(theMG),MG_MARK_KEY(theMG));
6735 MG_MARK_KEY(theMG) = 0;
6736
6737 return (GM_OK);
6738 }
6739
6740 /****************************************************************************/
6741 /** \brief Init what is necessary
6742 *
6743 * This function initializes the grid manager.
6744 *
6745 * @return <ul>
6746 * <li> GM_OK if ok </li>
6747 * <li> > 0 line in which error occurred </li>
6748 * </ul>
6749 */
6750 /****************************************************************************/
6751
6752 INT NS_DIM_PREFIX InitUGManager ()
6753 {
6754 INT i;
6755
6756 /* install the /Multigrids directory */
6757 if (ChangeEnvDir("/")==NULL)
6758 {
6759 PrintErrorMessage('F',"InitUGManager","could not changedir to root");
6760 return(__LINE__);
6761 }
6762 theMGRootDirID = GetNewEnvDirID();
6763 if (MakeEnvItem("Multigrids",theMGRootDirID,sizeof(ENVDIR))==NULL)
6764 {
6765 PrintErrorMessage('F',"InitUGManager","could not install /Multigrids dir");
6766 return(__LINE__);
6767 }
6768 theMGDirID = GetNewEnvDirID();
6769
6770 /* init the OBJT management */
6771 UsedOBJT = 0;
6772 for (i=0; i<NPREDEFOBJ; i++)
6773 SET_FLAG(UsedOBJT,1<<i);
6774
6775 return (GM_OK);
6776 }
6777
6778 INT NS_DIM_PREFIX ExitUGManager ()
6779 {
6780 return 0;
6781 }
6782
6783 /* nur temporaer zum Debuggen drin (Christian Wrobel): */
6784 /* TODO: entfernen nach Debuggphase */
6785 char *PrintElementInfo (ELEMENT *theElement,INT full)
6786 {
6787 static char out[2000];
6788 char tmp[200];
6789 char etype[10];
6790 char ekind[8];
6791 int i,j;
6792 ELEMENT *SonList[MAX_SONS];
6793
6794 if (theElement==NULL)
6795 {
6796 printf( "PrintElementInfo: element == NULL\n");
6797 return (NULL);
6798 }
6799
6800 if (DIM==2)
6801 switch (TAG(theElement))
6802 {
6803 case TRIANGLE : strcpy(etype,"TRI"); break;
6804 case QUADRILATERAL : strcpy(etype,"QUA"); break;
6805 default : strcpy(etype,"???"); break;
6806 }
6807 else
6808 switch (TAG(theElement))
6809 {
6810 case TETRAHEDRON : strcpy(etype,"TET"); break;
6811 case PYRAMID : strcpy(etype,"PYR"); break;
6812 case PRISM : strcpy(etype,"PRI"); break;
6813 case HEXAHEDRON : strcpy(etype,"HEX"); break;
6814 default : strcpy(etype,"???"); break;
6815 }
6816 switch (ECLASS(theElement))
6817 {
6818 case YELLOW_CLASS : strcpy(ekind,"YELLOW "); break;
6819 case GREEN_CLASS : strcpy(ekind,"GREEN "); break;
6820 case RED_CLASS : strcpy(ekind,"RED "); break;
6821 default : strcpy(ekind,"??? "); break;
6822 }
6823 if(full)
6824 snprintf(out, 2000,"ELEMID=" EID_FFMTE " %5s %5s CTRL=%8lx FLAG=%8lx REFINE=%2d MARK=%2d LEVEL=%2d",
6825 EID_PRTE(theElement),ekind,etype,
6826 (long)CTRL(theElement),(long)FLAG(theElement),REFINE(theElement),MARK(theElement),LEVEL(theElement));
6827 else
6828 snprintf(out, 2000,"ELEMID=" EID_FFMTE, EID_PRTE(theElement));
6829
6830 if (COARSEN(theElement)) strcat(out," COARSEN");
6831 strcat(out,"\n");
6832 for (i=0; i<CORNERS_OF_ELEM(theElement); i++)
6833 {
6834 #ifdef UG_DIM_2
6835 snprintf(tmp, 200," N%d=" ID_FMTX " x=%g y=%g\n",
6836 i,
6837 ID_PRTX(CORNER(theElement,i)),
6838 CVECT(MYVERTEX(CORNER(theElement,i)))[0],
6839 CVECT(MYVERTEX(CORNER(theElement,i)))[1]
6840 );
6841 #endif
6842 #ifdef UG_DIM_3
6843 snprintf(tmp, 200," N%d=" ID_FMTX " x=%g y=%g z=%g\n",
6844 i,
6845 ID_PRTX(CORNER(theElement,i)),
6846 CVECT(MYVERTEX(CORNER(theElement,i)))[0],
6847 CVECT(MYVERTEX(CORNER(theElement,i)))[1],
6848 CVECT(MYVERTEX(CORNER(theElement,i)))[2]
6849 );
6850 #endif
6851 strcat( out, tmp );
6852 }
6853
6854 if (EFATHER(theElement))
6855 {
6856 snprintf(tmp, 200," FA=" EID_FMTX "\n" ,EID_PRTX(EFATHER(theElement)));
6857 strcat( out, tmp );
6858 }
6859 else
6860 strcat( out," FA=NULL\n");
6861
6862 if( full)
6863 {
6864 UserWriteF(" NSONS=%d\n",NSONS(theElement));
6865 if (GetAllSons(theElement,SonList)==0)
6866 {
6867 for (i=0; i<MAX_SONS && SonList[i] != NULL; i++)
6868 {
6869 snprintf(tmp, 200," SON%d " EID_FMTX "\n" ,i,EID_PRTX(SonList[i]));
6870 strcat( out, tmp );
6871
6872 for (j=0; j<CORNERS_OF_ELEM(SonList[i]); j++)
6873 {
6874 #ifdef UG_DIM_2
6875 snprintf(tmp, 200," N%d= " ID_FMTX " x=%g y=%g\n",
6876 j,
6877 ID_PRTX(CORNER(SonList[i],j)),
6878 CVECT(MYVERTEX(CORNER(SonList[i],j)))[0],
6879 CVECT(MYVERTEX(CORNER(SonList[i],j)))[1]
6880 );
6881 #endif
6882 #ifdef UG_DIM_3
6883 snprintf(tmp, 200," N%d= " ID_FMTX " x=%g y=%g z=%g\n",
6884 j,
6885 ID_PRTX(CORNER(SonList[i],j)),
6886 CVECT(MYVERTEX(CORNER(SonList[i],j)))[0],
6887 CVECT(MYVERTEX(CORNER(SonList[i],j)))[1],
6888 CVECT(MYVERTEX(CORNER(SonList[i],j)))[2]
6889 );
6890 #endif
6891 strcat( out, tmp );
6892 }
6893 }
6894 }
6895 }
6896 snprintf(tmp, 200," key=%d\n", KeyForObject((KEY_OBJECT *)theElement) );
6897 strcat( out, tmp );
6898
6899 if(full)
6900 {
6901 if (OBJT(theElement)==BEOBJ)
6902 strcat( out," boundary element\n" );
6903 else
6904 strcat( out," no boundary element\n" );
6905
6906 for (i=0; i<SIDES_OF_ELEM(theElement); i++)
6907 {
6908 for(j=0; j<CORNERS_OF_SIDE(theElement,i); j++)
6909 {
6910 #ifdef UG_DIM_2
6911 snprintf(tmp, 200," NODE[ID=%ld]: x=%g y=%g",
6912 (long)(ID(CORNER(theElement,CORNER_OF_SIDE(theElement,i,j)))),
6913 CVECT(MYVERTEX(CORNER(theElement,CORNER_OF_SIDE(theElement,i,j))))[0],
6914 CVECT(MYVERTEX(CORNER(theElement,CORNER_OF_SIDE(theElement,i,j))))[1]);
6915 #endif
6916 #ifdef UG_DIM_3
6917 snprintf(tmp, 200," NODE[ID=%ld]: x=%g y=%g z=%g",
6918 (long)(ID(CORNER(theElement,CORNER_OF_SIDE(theElement,i,j)))),
6919 CVECT(MYVERTEX(CORNER(theElement,CORNER_OF_SIDE(theElement,i,j))))[0],
6920 CVECT(MYVERTEX(CORNER(theElement,CORNER_OF_SIDE(theElement,i,j))))[1],
6921 CVECT(MYVERTEX(CORNER(theElement,CORNER_OF_SIDE(theElement,i,j))))[2]);
6922 #endif
6923 strcat( out, tmp );
6924 }
6925 strcat( out,"\n");
6926 }
6927 }
6928 #ifdef ModelP
6929 /*UserWriteF(PFMT"%s", me,out );*/
6930 printf("%s",out);
6931 #else
6932 UserWrite(out);
6933 #endif
6934
6935 return out;
6936 }