Debian Codegraph @

dune/uggrid/gm/algebra.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 /*! \file algebra.c
6 * \ingroup gm
7 */
8
9 /** \addtogroup gm
10 *
11 * @{
12 */
13
14 /****************************************************************************/
15 /* */
16 /* File: algebra.c */
17 /* */
18 /* Purpose: management for algebraic structures */
19 /* */
20 /* Author: Klaus Johannsen */
21 /* Interdisziplinaeres Zentrum fuer Wissenschaftliches Rechnen */
22 /* Universitaet Heidelberg */
23 /* Im Neuenheimer Feld 294 */
24 /* 6900 Heidelberg */
25 /* email: [email protected] */
26 /* */
27 /* blockvector data structure: */
28 /* Christian Wrobel */
29 /* Institut fuer Computeranwendungen III */
30 /* Universitaet Stuttgart */
31 /* Pfaffenwaldring 27 */
32 /* 70569 Stuttgart */
33 /* email: [email protected] */
34 /* */
35 /* History: 1.12.93 begin, ug 3d */
36 /* 26.10.94 begin combination 2D/3D version */
37 /* 28.09.95 blockvector implemented (Christian Wrobel) */
38 /* */
39 /* Remarks: */
40 /* */
41 /****************************************************************************/
42
43
44 /****************************************************************************/
45 /* */
46 /* defines to exclude functions */
47 /* */
48 /****************************************************************************/
49
50
51 /****************************************************************************/
52 /* */
53 /* include files */
54 /* system include files */
55 /* application include files */
56 /* */
57 /****************************************************************************/
58
59 #include <config.h>
60 #include <cassert>
61 #include <cmath>
62 #include <cstdio>
63 #include <cstdlib>
64 #include <cstring>
65
66 #include <dune/uggrid/low/architecture.h>
67 #include <dune/uggrid/low/debug.h>
68 #include <dune/uggrid/low/fifo.h>
69 #include <dune/uggrid/low/heaps.h>
70 #include <dune/uggrid/low/misc.h>
71 #include <dune/uggrid/low/namespace.h>
72 #include <dune/uggrid/low/ugenv.h>
73 #include <dune/uggrid/low/ugtypes.h>
74
75 #include <dune/uggrid/ugdevices.h>
76
77 #include "algebra.h"
78 #include "cw.h"
79 #include "dlmgr.h"
80 #include "gm.h"
81 #include "elements.h"
82 #include "refine.h"
83 #include "ugm.h"
84 #include "evm.h"
85 #include "dlmgr.h"
86
87 #ifdef ModelP
88 #include <dune/uggrid/parallel/dddif/parallel.h>
89 #endif
90
91
92 USING_UG_NAMESPACE
93 USING_UGDIM_NAMESPACE
94 using namespace PPIF;
95
96
97 /****************************************************************************/
98 /** \brief Return pointer to a new vector structure
99 *
100 * @param theGrid - grid where vector should be inserted
101 * @param side - one of the types defined in gm
102 * @param object - associate vector with this object
103 * @param vHandle - handle of new vector, i.e. a pointer to a pointer where
104 a pointer to the new vector is placed.
105
106 This function returns a pointer to a new side vector structure.
107 First the free list is checked for a free entry, if none
108 is available, a new structure is allocated from the heap.
109
110 * @return <ul>
111 * <li> 0 if ok </li>
112 * <li> 1 if error occurred. </li>
113 </ul>
114 */
115 /****************************************************************************/
116
117
118 INT NS_DIM_PREFIX CreateSideVector (GRID *theGrid, INT side, GEOM_OBJECT *object, VECTOR **vHandle)
119 {
120 *vHandle = nullptr;
121
122 #ifdef UG_DIM_2
123 return 0;
124 #endif
125
126 MULTIGRID *theMG = MYMG(theGrid);
127
128 VECTOR *pv = (VECTOR *)GetMemoryForObject(theMG,sizeof(VECTOR),VEOBJ);
129 if (pv==NULL)
130 REP_ERR_RETURN(1);
131
132 /* initialize data */
133 SETOBJT(pv,VEOBJ);
134 SETVDATATYPE(pv,BITWISE_TYPE(SIDEVEC));
135 SETVOTYPE(pv,SIDEVEC);
136 SETVCLASS(pv,3);
137 SETVNCLASS(pv,0);
138 SETVNEW(pv,1);
139 /* SETPRIO(dddContext, pv,PrioMaster); */
140
141 #ifndef ModelP
142 // Dune uses the id field for face indices in sequential grids
143 pv->id = (theGrid->mg->vectorIdCounter)++;
144 #endif
145
146 #ifdef ModelP
147 DDD_AttrSet(PARHDR(pv),GRID_ATTR(theGrid));
148 #endif
149
150 VOBJECT(pv) = object;
151 VINDEX(pv) = (long)NVEC(theGrid);
152 SUCCVC(pv) = FIRSTVECTOR(theGrid);
153
154 GRID_LINK_VECTOR(theGrid,pv,PrioMaster);
155
156 *vHandle = pv;
157
158 SETVECTORSIDE(*vHandle,side);
159 SETVCOUNT(*vHandle,1);
160
161 return (0);
162 }
163
164 /****************************************************************************/
165 /** \brief Remove vector from the data structure
166 *
167 * @param theGrid - grid level where theVector is in.
168 * @param theVector - VECTOR to be disposed.
169
170 This function removes vector from the data structure and places
171 it in the free list.
172
173 * @return <ul>
174 * <li> 0 if ok </li>
175 * <li> 1 if error occurred. </li>
176 </ul>
177 */
178 /****************************************************************************/
179
180 INT NS_DIM_PREFIX DisposeVector (GRID *theGrid, VECTOR *theVector)
181 {
182 INT Size;
183
184 if (theVector == NULL)
185 return(0);
186
187 /* now remove vector from vector list */
188 GRID_UNLINK_VECTOR(theGrid,theVector);
189
190 /* reset count flags */
191 SETVCOUNT(theVector,0);
192
193
194 /* delete the vector itself */
195 Size = sizeof(VECTOR);
196 if (PutFreeObject(theGrid->mg,theVector,Size,VEOBJ))
197 RETURN(1);
198
199 return(0);
200 }
201
202 /****************************************************************************/
203 /** \brief Dispose one of two vectors associated to a face
204 *
205 * @param theGrid - pointer to grid
206 * @param Elem0,side0 - first element and side
207 * @param Elem1,side1 - second element and side
208
209 After grid refinement it may happen that two side vector are associated to a face
210 in the grid. The elements on both sides of the face each have their own side
211 vector. This is an inconsistent state -- there should be only one vector per face.
212 This routine deletes one of the two side vectors and makes the data structure
213 consistent again. This is easier than only creating one unique side vector
214 to begin with.
215
216 * @return <ul>
217 * <li> 0 if ok </li>
218 * <li> 1 if error occurred. </li>
219 </ul>
220 */
221 /****************************************************************************/
222
223 #ifdef UG_DIM_3
224 INT NS_DIM_PREFIX DisposeDoubledSideVector (GRID *theGrid, ELEMENT *Elem0, INT Side0, ELEMENT *Elem1, INT Side1)
225 {
226 if (VEC_DEF_IN_OBJ_OF_GRID(theGrid,SIDEVEC))
227 {
228 assert(NBELEM(Elem0,Side0)==Elem1 && NBELEM(Elem1,Side1)==Elem0);
229 VECTOR *Vector0 = SVECTOR(Elem0,Side0);
230 VECTOR *Vector1 = SVECTOR(Elem1,Side1);
231 if (Vector0 == Vector1)
232 return (0);
233 if ((Vector0==NULL) || (Vector1==NULL))
234 /* this is the case at boundaries between different parts
235 the part not using vectors in SIDEs will not need a pointer
236 to the side vector */
237 return (0);
238 assert(VCOUNT(Vector0)==1 && VCOUNT(Vector1)==1);
239
240 SET_SVECTOR(Elem1,Side1,Vector0);
241 SETVCOUNT(Vector0,2);
242 if (DisposeVector (theGrid,Vector1))
243 RETURN (1);
244
245 return (0);
246
247 }
248
249 RETURN (1);
250 }
251 #endif
252
253
254 /****************************************************************************/
255 /** \brief Get a pointer list to all side data
256
257 * @param theElement - that element
258 * @param cnt - how many vectors
259 * @param vList - array to store vector list
260
261 This function gets a pointer array to all VECTORs in sides of the given element.
262
263 * @return <ul>
264 * <li> GM_OK if ok </li>
265 * <li> GM_ERROR if error occurred. </li>
266 </ul>
267 */
268 /****************************************************************************/
269
270 #ifdef UG_DIM_3
271 INT NS_DIM_PREFIX GetVectorsOfSides (const ELEMENT *theElement, INT *cnt, VECTOR **vList)
272 {
273 INT i;
274
275 *cnt = 0;
276
277 for (i=0; i<SIDES_OF_ELEM(theElement); i++)
278 if (SVECTOR(theElement,i) != NULL)
279 vList[(*cnt)++] = SVECTOR(theElement,i);
280
281 IFDEBUG(gm,0)
282 for (i=0; i<(*cnt); i++)
283 {
284 assert(vList[i] != NULL);
285 assert(VOTYPE(vList[i]) == SIDEVEC);
286 }
287 ENDDEBUG
288
289 return(GM_OK);
290 }
291 #endif
292
293 /****************************************************************************/
294 /** \brief Get pointers to elements having a common side
295
296 * @param theVector - given vector associated with a side of an element in 3D
297 * @param Elements - array to be filled with two pointers of elements
298 * @param Sides - array to be filled with side number within respective element
299
300 Given a VECTOR associated with the side of an element, this function
301 retrieves pointers to at most two elements having this side in common.
302 If the side is part of the exterior boundary of the domain, then
303 Elements[1] will be the NULL pointer.
304
305 * @return <ul>
306 * <li> 0 if ok
307 * <li> 1 if error occurred.
308 </ul>
309 */
310 /****************************************************************************/
311
312 #ifdef UG_DIM_3
313 INT NS_DIM_PREFIX GetElementInfoFromSideVector (const VECTOR *theVector, ELEMENT **Elements, INT *Sides)
314 {
315 INT i;
316 ELEMENT *theNeighbor;
317
318 if (VOTYPE(theVector) != SIDEVEC)
319 RETURN (1);
320 Elements[0] = (ELEMENT *)VOBJECT(theVector);
321 Sides[0] = VECTORSIDE(theVector);
322
323 /* find neighbor */
324 Elements[1] = theNeighbor = NBELEM(Elements[0],Sides[0]);
325 if (theNeighbor == NULL) return (0);
326
327 /* search the side */
328 for (i=0; i<SIDES_OF_ELEM(theNeighbor); i++)
329 if (NBELEM(theNeighbor,i) == Elements[0])
330 break;
331
332 /* found ? */
333 if (i<SIDES_OF_ELEM(theNeighbor))
334 {
335 Sides[1] = i;
336 return (0);
337 }
338 RETURN (1);
339 }
340 #endif
341
342 #ifdef ModelP
343 static int Gather_VectorVNew (DDD::DDDContext&, DDD_OBJ obj, void *data)
344 {
345 VECTOR *theVector = (VECTOR *)obj;
346
347 ((INT *)data)[0] = VNEW(theVector);
348
349 return(0);
350 }
351
352 static int Scatter_VectorVNew (DDD::DDDContext&, DDD_OBJ obj, void *data)
353 {
354 VECTOR *theVector = (VECTOR *)obj;
355
356 SETVNEW(theVector,std::max((INT)VNEW(theVector),((INT *)data)[0]));
357
358 return(0);
359 }
360
361 static int Scatter_GhostVectorVNew (DDD::DDDContext&, DDD_OBJ obj, void *data)
362 {
363 VECTOR *theVector = (VECTOR *)obj;
364
365 SETVNEW(theVector,((INT *)data)[0]);
366
367 return(0);
368 }
369 #endif
370
371 INT NS_DIM_PREFIX SetSurfaceClasses (MULTIGRID *theMG)
372 {
373 GRID *theGrid;
374 ELEMENT *theElement; VECTOR *v;
375 INT level,fullrefine;
376
377 level = TOPLEVEL(theMG);
378 if (level > 0) {
379 theGrid = GRID_ON_LEVEL(theMG,TOPLEVEL(theMG));
380 ClearVectorClasses(theGrid);
381 for (theElement=PFIRSTELEMENT(theGrid); theElement!=NULL;
382 theElement=SUCCE(theElement))
383 {
384 if (MinNodeClass(theElement)==3)
385 SeedVectorClasses(theGrid,theElement);
386 }
387 PropagateVectorClasses(theGrid);
388 theGrid = GRID_ON_LEVEL(theMG,0);
389 ClearNextVectorClasses(theGrid);
390 for (theElement=PFIRSTELEMENT(theGrid); theElement!=NULL;
391 theElement=SUCCE(theElement))
392 {
393 if (MinNextNodeClass(theElement)==3)
394 SeedNextVectorClasses(theGrid,theElement);
395 }
396 PropagateNextVectorClasses(theGrid);
397 }
398 for (level--; level>0; level--)
399 {
400 theGrid = GRID_ON_LEVEL(theMG,level);
401 ClearVectorClasses(theGrid);
402 ClearNextVectorClasses(theGrid);
403 for (theElement=PFIRSTELEMENT(theGrid); theElement!=NULL;
404 theElement=SUCCE(theElement)) {
405 if (MinNodeClass(theElement)==3)
406 SeedVectorClasses(theGrid,theElement);
407 if (MinNextNodeClass(theElement)==3)
408 SeedNextVectorClasses(theGrid,theElement);
409 }
410 PropagateVectorClasses(theGrid);
411 PropagateNextVectorClasses(theGrid);
412 }
413
414 fullrefine = TOPLEVEL(theMG);
415 for (level=TOPLEVEL(theMG); level>=0; level--)
416 {
417 theGrid = GRID_ON_LEVEL(theMG,level);
418 for (v=PFIRSTVECTOR(theGrid); v!= NULL; v=SUCCVC(v)) {
419 SETFINE_GRID_DOF(v,((VCLASS(v)>=2)&&(VNCLASS(v)<=1)));
420 if (FINE_GRID_DOF(v))
421 fullrefine = level;
422 }
423 }
424 #ifdef ModelP
425 fullrefine = UG_GlobalMinINT(theMG->ppifContext(), fullrefine);
426 #endif
427
428 FULLREFINELEVEL(theMG) = fullrefine;
429
430 return(0);
431 }
432
433 /****************************************************************************/
434 /** \brief Creates the algebra for a grid
435
436 * @param theGrid - pointer to grid
437
438 This function allocates VECTORs in all geometrical objects of the grid.
439
440 * @return <ul>
441 * <li> GM_OK if ok
442 </ul>
443 */
444 /****************************************************************************/
445
446 INT NS_DIM_PREFIX CreateAlgebra (MULTIGRID *theMG)
447 {
448 GRID *g = nullptr;
449 #ifdef UG_DIM_3
450 VECTOR *nbvec;
451 ELEMENT *nbelem;
452 INT j;
453 #endif
454 ELEMENT *elem;
455 INT i;
456
457
458 if (MG_COARSE_FIXED(theMG) == false) {
459 for (i=0; i<=TOPLEVEL(theMG); i++) {
460 g = GRID_ON_LEVEL(theMG,i);
461
462 if (NVEC(g)>0)
463 continue; /* skip this level */
464
465 /* loop elements and element sides */
466 for (elem=PFIRSTELEMENT(g); elem!=NULL; elem=SUCCE(elem)) {
467
468 /* side vectors */
469 #ifdef UG_DIM_3
470 for (INT side=0; side<SIDES_OF_ELEM(elem); side++)
471 if (SVECTOR(elem,side)==NULL) {
472 VECTOR *vec;
473 if (CreateSideVector (g,side,
474 (GEOM_OBJECT *)elem,&vec))
475 REP_ERR_RETURN (GM_ERROR);
476 SET_SVECTOR(elem,side,vec);
477 }
478 #endif
479 }
480 }
481 #ifdef UG_DIM_3
482 /* dispose doubled side vectors */
483 for (elem=PFIRSTELEMENT(g); elem!=NULL; elem=SUCCE(elem))
484 {
485 for(INT side=0; side<SIDES_OF_ELEM(elem); side++)
486 {
487 if(OBJT(elem)==BEOBJ)
488 {
489 if(INNER_SIDE(elem,side))
490 {
491 nbelem = NBELEM(elem,side);
492 ASSERT(nbelem!=NULL);
493 VECTOR *vec=SVECTOR(elem,side);
494 #ifdef Debug
495 INT n=0;
496 #endif
497 for(j=0; j<SIDES_OF_ELEM(nbelem); j++)
498 {
499 nbvec=SVECTOR(nbelem,j);
500
501 /* doubled sidevectors ? */
502 if(NBELEM(nbelem,j)==elem)
503 {
504 if(vec!=nbvec)
505 {
506 if(DisposeVector(g,nbvec))
507 REP_ERR_RETURN(GM_ERROR);
508 SET_SVECTOR(nbelem,j,vec);
509 SETVCOUNT(vec,2); /* PB, 25 Sep 2005: changed from 1 to 2 */
510 }
511 }
512 }
513 #ifdef Debug
514 n++;
515 #endif
516 ASSERT(n==1);
517 }
518 }else
519 {
520 nbelem = NBELEM(elem,side);
521 ASSERT(nbelem!=NULL);
522 VECTOR* vec=SVECTOR(elem,side);
523 #ifdef Debug
524 INT n=0;
525 #endif
526 for(j=0; j<SIDES_OF_ELEM(nbelem); j++)
527 {
528 nbvec=SVECTOR(nbelem,j);
529 ASSERT(nbvec!=NULL);
530
531 /* doubled sidevectors ? */
532 if(NBELEM(nbelem,j)==elem)
533 {
534 if(vec!=nbvec)
535 {
536 if(DisposeVector(g,nbvec))
537 REP_ERR_RETURN(GM_ERROR);
538 SET_SVECTOR(nbelem,j,vec);
539 SETVCOUNT(vec,2); /* PB, 25 Sep 2005: changed from 1 to 2 */
540 }
541 }
542 }
543 #ifdef Debug
544 n++;
545 #endif
546 ASSERT(n==1);
547 }
548 }
549 }
550 #endif
551 MG_COARSE_FIXED(theMG) = true;
552 }
553
554 /* now we should be safe to clear the InsertElement face map */
555 theMG->facemap.clear();
556
557 #ifdef ModelP
558 /* update VNEW-flags */
559 auto& context = theMG->dddContext();
560 const auto& dddctrl = ddd_ctrl(context);
561 DDD_IFExchange(context,
562 dddctrl.BorderVectorSymmIF,sizeof(INT),
563 Gather_VectorVNew,Scatter_VectorVNew);
564 DDD_IFOneway(context,
565 dddctrl.VectorIF,IF_FORWARD,sizeof(INT),
566 Gather_VectorVNew,Scatter_GhostVectorVNew);
567 #endif
568
569 SetSurfaceClasses(theMG);
570
571 return(GM_OK);
572 }
573
574
575
576 INT NS_DIM_PREFIX PrepareAlgebraModification (MULTIGRID *theMG)
577 {
578 int j,k;
579 ELEMENT *theElement;
580 VECTOR *theVector;
581
582 j = theMG->topLevel;
583 for (k=0; k<=j; k++)
584 {
585 for (theElement=PFIRSTELEMENT(GRID_ON_LEVEL(theMG,k)); theElement!=NULL; theElement=SUCCE(theElement))
586 {
587 SETUSED(theElement,0);
588 }
589 for (theVector=PFIRSTVECTOR(GRID_ON_LEVEL(theMG,k)); theVector!= NULL; theVector=SUCCVC(theVector))
590 {
591 SETVNEW(theVector,0);
592 }
593 }
594
595 return (0);
596 }
597
598 /****************************************************************************/
599 /** \brief Checks validity of geom_object and its vector
600
601 * @param fmt - FORMAT of associated multigrid
602 * @param theObject - the object which points to theVector
603 * @param ObjectString - for error message
604 * @param theVector - the vector of theObject
605 * @param VectorObjType - NODEVEC,...
606 * @param side - element side for SIDEVEC
607
608 This function checks the consistency between an geom_object and
609 its vector.
610
611 * @return <ul>
612 * <li> GM_OK if ok
613 * <li> GM_ERROR if error occurred.
614 </ul>
615 */
616 /****************************************************************************/
617
618 #ifdef UG_DIM_3
619 static INT CheckVector (GEOM_OBJECT *theObject, const char *ObjectString,
620 VECTOR *theVector, INT VectorObjType, INT side)
621 {
622 GEOM_OBJECT *VecObject;
623 INT errors = 0;
624
625 if (theVector == NULL)
626 {
627 errors++;
628 UserWriteF("%s ID=%ld has NO VECTOR", ObjectString,
629 ID(theObject));
630 UserWrite("\n");
631 }
632 else
633 {
634 VecObject = VOBJECT(theVector);
635 if (VecObject == NULL)
636 {
637 errors++;
638 UserWriteF("vector=" VINDEX_FMTX " %s GID=" GID_FMT " has NO BACKPTR\n",
639 VINDEX_PRTX(theVector), ObjectString,
640 (OBJT(theObject)==BEOBJ || OBJT(theObject)==IEOBJ) ?
641 EGID(&(theObject->el)) : (OBJT(theObject)==NDOBJ) ?
642 GID(&(theObject->nd)) :
643 GID(&(theObject->ed))
644 );
645 }
646 else
647 {
648 if (VecObject != theObject)
649 {
650 if (OBJT(VecObject) != OBJT(theObject))
651 {
652 int error = 1;
653
654 /* both objects may be elements */
655 if ((OBJT(VecObject)==BEOBJ || OBJT(VecObject)==IEOBJ) &&
656 (OBJT(theObject)==BEOBJ || OBJT(theObject)==IEOBJ) )
657 {
658 ELEMENT *theElement = (ELEMENT *)theObject;
659 ELEMENT *vecElement = (ELEMENT *)VecObject;
660 int i;
661
662 #ifdef ModelP
663 if (EMASTER(theElement) ||
664 EMASTER(vecElement) )
665 {
666 #endif
667 for (i=0; i<SIDES_OF_ELEM(theElement); i++)
668 if (NBELEM(theElement,i) == vecElement)
669 {
670 /* they are neighbors -> ok */
671 error = 0;
672 break;
673 }
674 #ifdef ModelP
675 }
676 #endif
677 if (error)
678 {
679 UserWriteF("vector=" VINDEX_FMTX " has type %s, but points "
680 "to wrong vecobj=" EID_FMTX " NO NB of obj=" EID_FMTX "\n",
681 VINDEX_PRTX(theVector),ObjectString,
682 EID_PRTX(vecElement),EID_PRTX(theElement));
683 }
684 }
685 else
686 {
687 errors++;
688 UserWriteF("vector=" VINDEX_FMTX " has type %s, but points "
689 "to wrong obj=%d type OBJT=%d\n",
690 VINDEX_PRTX(theVector),ObjectString,ID(VecObject),
691 OBJT(VecObject));
692 }
693 }
694 else
695 {
696 #ifdef UG_DIM_3
697 if (VectorObjType == SIDEVEC)
698 {
699 /* TODO: check side vector */
700 }
701 else
702 #endif
703 {
704 errors++;
705 UserWriteF("%s vector=" VINDEX_FMTX " is referenced by "
706 "obj0=%x, but points to wrong obj1=%x\n",
707 ObjectString, VINDEX_PRTX(theVector),
708 theObject, VecObject);
709 #ifdef ModelP
710 if (strcmp(ObjectString,"EDGE")==0)
711 UserWriteF("obj0: n0=%d n1=%d obj1: "
712 "n0=%d n1=%d\n",
713 GID(NBNODE(LINK0(&(theObject->ed)))),
714 GID(NBNODE(LINK1(&(theObject->ed)))),
715 GID(NBNODE(LINK0(&(VecObject->ed)))),
716 GID(NBNODE(LINK1(&(VecObject->ed)))) );
717 #endif
718 }
719 }
720 }
721 }
722 }
723
724 return(errors);
725 }
726 #endif
727
728 /****************************************************************************/
729 /** \brief Check the algebraic part of the data structure
730
731 * @param theGrid - grid level to check
732
733 This function checks the consistency of the algebraic data structures
734 including the interconnections between the geometric part.
735 This function assumes a correct geometric data structure.
736
737 * @return <ul>
738 * <li> GM_OK if ok </li>
739 * <li> GM_ERROR if error occurred. </li>
740 </ul>
741 */
742 /****************************************************************************/
743
744 INT NS_DIM_PREFIX CheckAlgebra (GRID *theGrid)
745 {
746 INT errors = 0;
747
748 if ((GLEVEL(theGrid)==0) && !MG_COARSE_FIXED(MYMG(theGrid)))
749 {
750 if (NVEC(theGrid)>0)
751 {
752 errors++;
753 UserWriteF("coarse grid not fixed but vectors allocated\n");
754 }
755 return(errors);
756 }
757
758 /* check pointers to element, side, edge vector */
759 for (ELEMENT *theElement = PFIRSTELEMENT(theGrid); theElement != nullptr;
760 theElement=SUCCE(theElement))
761 {
762 #ifdef UG_DIM_3
763 /* check side vectors */
764 if (VEC_DEF_IN_OBJ_OF_GRID(theGrid,SIDEVEC))
765 {
766 for (INT i=0; i<SIDES_OF_ELEM(theElement); i++)
767 {
768 VECTOR *theVector = SVECTOR(theElement,i);
769 errors += CheckVector((GEOM_OBJECT *) theElement, "ELEMSIDE",
770 theVector, SIDEVEC,i);
771 }
772 }
773 #endif
774 }
775
776 return(errors);
777 }
778
779 /****************************************************************************/
780 /** \brief Calc coordinate position of vector
781
782 * @param theVector - a given vector
783 * @param position - array to be filled
784
785 This function calcs physical position of vector. For edge vectors the
786 midpoint is returned, and for sides and elements the center of mass.
787
788 * @return <ul>
789 * <li> 0 if ok </li>
790 * <li> 1 if error occurred. </li>
791 </ul>
792 */
793 /****************************************************************************/
794
795 INT NS_DIM_PREFIX VectorPosition (const VECTOR *theVector, FieldVector<DOUBLE,DIM>& position)
796 {
797 #ifdef UG_DIM_3
798 ELEMENT *theElement;
799 INT theSide,j;
800 #endif
801
802 ASSERT(theVector != NULL);
803
804 switch (VOTYPE(theVector))
805 {
806 #ifdef UG_DIM_3
807 case SIDEVEC :
808 theElement = (ELEMENT *)VOBJECT(theVector);
809 theSide = VECTORSIDE(theVector);
810 for (INT i=0; i<DIM; i++)
811 {
812 position[i] = 0.0;
813 for(j=0; j<CORNERS_OF_SIDE(theElement,theSide); j++)
814 position[i] += CVECT(MYVERTEX(CORNER(theElement,CORNER_OF_SIDE(theElement,theSide,j))))[i];
815 position[i] /= CORNERS_OF_SIDE(theElement,theSide);
816 }
817 return (0);
818 #endif
819
820 default : PrintErrorMessage('E',"VectorPosition","unrecognized object type for vector");
821 assert(0);
822 }
823
824 RETURN (GM_ERROR);
825 }
826
827
828 /****************************************************************************/
829 /** \brief Initialize vector classes
830
831 * @param theGrid - given grid
832 * @param theElement - given element
833
834 Initialize vector class in all vectors associated with given element with 3.
835
836 * @return <ul>
837 * <li> 0 if ok </li>
838 * <li> 1 if error occurred. </li>
839 </ul>
840 */
841 /****************************************************************************/
842
843 INT NS_DIM_PREFIX SeedVectorClasses (GRID *theGrid, ELEMENT *theElement)
844 {
845 #ifdef UG_DIM_3
846 INT cnt;
847
848 VECTOR *vList[20];
849 if (VEC_DEF_IN_OBJ_OF_GRID(theGrid,SIDEVEC))
850 {
851 GetVectorsOfSides(theElement,&cnt,vList);
852 for (INT i=0; i<cnt; i++)
853 SETVCLASS(vList[i],3);
854 }
855 #endif
856 return (0);
857 }
858
859 /****************************************************************************/
860 /** \brief Reset vector classes
861
862 * @param theGrid - pointer to grid
863
864 Reset all vector classes in all vectors of given grid to 0.
865
866 * @return <ul>
867 * <li> 0 if ok </li>
868 * <li> 1 if error occurred. </li>
869 </ul>
870 */
871 /****************************************************************************/
872
873 INT NS_DIM_PREFIX ClearVectorClasses (GRID *theGrid)
874 {
875 VECTOR *theVector;
876
877 /* reset class of each vector to 0 */
878 for (theVector=PFIRSTVECTOR(theGrid); theVector!=NULL; theVector=SUCCVC(theVector))
879 SETVCLASS(theVector,0);
880
881 return(0);
882 }
883
884
885 #ifdef ModelP
886 static int Gather_VectorVClass (DDD::DDDContext&, DDD_OBJ obj, void *data)
887 {
888 VECTOR *theVector = (VECTOR *)obj;
889
890 PRINTDEBUG(gm,1,("Gather_VectorVClass(): v=" VINDEX_FMTX " vclass=%d\n",
891 VINDEX_PRTX(theVector),VCLASS(theVector)))
892
893 ((INT *)data)[0] = VCLASS(theVector);
894
895 return(0);
896 }
897
898 static int Scatter_VectorVClass (DDD::DDDContext&, DDD_OBJ obj, void *data)
899 {
900 VECTOR *theVector = (VECTOR *)obj;
901
902 SETVCLASS(theVector,std::max((INT)VCLASS(theVector),((INT *)data)[0]));
903
904 PRINTDEBUG(gm,2,("Scatter_VectorVClass(): v=" VINDEX_FMTX " vclass=%d\n",
905 VINDEX_PRTX(theVector),VCLASS(theVector)))
906
907 return(0);
908 }
909
910 static int Scatter_GhostVectorVClass (DDD::DDDContext&, DDD_OBJ obj, void *data)
911 {
912 VECTOR *theVector = (VECTOR *)obj;
913
914 SETVCLASS(theVector,((INT *)data)[0]);
915
916 PRINTDEBUG(gm,1,("Scatter_GhostVectorVClass(): v=" VINDEX_FMTX " vclass=%d\n",
917 VINDEX_PRTX(theVector),VCLASS(theVector)))
918
919 return(0);
920 }
921 #endif
922
923
924 /****************************************************************************/
925 /** \brief Compute vector classes after initialization
926
927 * @param theGrid - pointer to grid
928
929 After vector classes have been reset and initialized, this function
930 now computes the class 2 and class 1 vectors.
931
932 * @return <ul>
933 * <li> 0 if ok </li>
934 * <li> 1 if error occurred </li>
935 </ul>
936 */
937 /****************************************************************************/
938 INT NS_DIM_PREFIX PropagateVectorClasses (GRID *theGrid)
939 {
940 #ifdef ModelP
941 auto& context = theGrid->dddContext();
942 const auto& dddctrl = ddd_ctrl(context);
943
944 PRINTDEBUG(gm,1,("\nPropagateVectorClasses():"
945 " 1. communication on level %d\n",GLEVEL(theGrid)))
946 /* exchange VCLASS of vectors */
947 DDD_IFAExchange(context,
948 dddctrl.BorderVectorSymmIF,GRID_ATTR(theGrid),sizeof(INT),
949 Gather_VectorVClass, Scatter_VectorVClass);
950 #endif
951
952 #ifdef ModelP
953 PRINTDEBUG(gm,1,("\nPropagateVectorClasses(): 2. communication\n"))
954 /* exchange VCLASS of vectors */
955 DDD_IFAExchange(context,
956 dddctrl.BorderVectorSymmIF,GRID_ATTR(theGrid),sizeof(INT),
957 Gather_VectorVClass, Scatter_VectorVClass);
958 #endif
959
960 #ifdef ModelP
961 PRINTDEBUG(gm,1,("\nPropagateVectorClasses(): 3. communication\n"))
962 /* exchange VCLASS of vectors */
963 DDD_IFAExchange(context,
964 dddctrl.BorderVectorSymmIF,GRID_ATTR(theGrid),sizeof(INT),
965 Gather_VectorVClass, Scatter_VectorVClass);
966 #endif
967
968 #ifdef ModelP
969 /* send VCLASS to ghosts */
970 DDD_IFAOneway(context,
971 dddctrl.VectorIF,GRID_ATTR(theGrid),IF_FORWARD,sizeof(INT),
972 Gather_VectorVClass, Scatter_GhostVectorVClass);
973 #endif
974
975 return(0);
976 }
977
978 /****************************************************************************/
979 /** \brief Reset class of the vectors on the next level
980
981 * @param theGrid - pointer to grid
982
983 This function clears VNCLASS flag in all vectors. This is the first step to
984 compute the class of the dofs on the *NEXT* level, which
985 is also the basis for determining copies.
986
987 * @return <ul>
988 * <li> 0 if ok </li>
989 * <li> 1 if error occurred. </li>
990 </ul>
991 */
992 /****************************************************************************/
993
994 INT NS_DIM_PREFIX ClearNextVectorClasses (GRID *theGrid)
995 {
996 VECTOR *theVector;
997
998 /* reset class of each vector to 0 */
999 for (theVector=PFIRSTVECTOR(theGrid); theVector!=NULL; theVector=SUCCVC(theVector))
1000 SETVNCLASS(theVector,0);
1001
1002 /* now the refinement algorithm will initialize the class 3 vectors */
1003 /* on the *NEXT* level. */
1004 return(0);
1005 }
1006
1007 /****************************************************************************/
1008 /** \brief Set VNCLASS in all vectors associated with element
1009
1010 * @param theGrid - given grid
1011 * @param theElement - pointer to element
1012
1013 Set VNCLASS in all vectors associated with the element to 3.
1014
1015 * @return <ul>
1016 * <li> 0 if ok </li>
1017 * <li> 1 if error occurred. </li>
1018 </ul>
1019 */
1020 /****************************************************************************/
1021
1022 INT NS_DIM_PREFIX SeedNextVectorClasses (GRID *theGrid, ELEMENT *theElement)
1023 {
1024
1025 #ifdef UG_DIM_3
1026 if (VEC_DEF_IN_OBJ_OF_GRID(theGrid,SIDEVEC))
1027 {
1028 VECTOR *vList[20];
1029 INT cnt;
1030 GetVectorsOfSides(theElement,&cnt,vList);
1031 for (INT i=0; i<cnt; i++)
1032 SETVNCLASS(vList[i],3);
1033 }
1034 #endif
1035 return (0);
1036 }
1037
1038
1039
1040
1041 #ifdef ModelP
1042 static int Gather_VectorVNClass (DDD::DDDContext&, DDD_OBJ obj, void *data)
1043 {
1044 VECTOR *theVector = (VECTOR *)obj;
1045
1046 PRINTDEBUG(gm,2,("Gather_VectorVNClass(): v=" VINDEX_FMTX " vnclass=%d\n",
1047 VINDEX_PRTX(theVector),VNCLASS(theVector)))
1048
1049 ((INT *)data)[0] = VNCLASS(theVector);
1050
1051 return(GM_OK);
1052 }
1053
1054 static int Scatter_VectorVNClass (DDD::DDDContext&, DDD_OBJ obj, void *data)
1055 {
1056 VECTOR *theVector = (VECTOR *)obj;
1057
1058 SETVNCLASS(theVector,std::max((INT)VNCLASS(theVector),((INT *)data)[0]));
1059
1060 PRINTDEBUG(gm,2,("Scatter_VectorVNClass(): v=" VINDEX_FMTX " vnclass=%d\n",
1061 VINDEX_PRTX(theVector),VNCLASS(theVector)))
1062
1063 return(GM_OK);
1064 }
1065
1066 static int Scatter_GhostVectorVNClass (DDD::DDDContext&, DDD_OBJ obj, void *data)
1067 {
1068 VECTOR *theVector = (VECTOR *)obj;
1069
1070 SETVNCLASS(theVector,((INT *)data)[0]);
1071
1072 PRINTDEBUG(gm,2,("Scatter_GhostVectorVNClass(): v=" VINDEX_FMTX " vnclass=%d\n",
1073 VINDEX_PRTX(theVector),VNCLASS(theVector)))
1074
1075 return(GM_OK);
1076 }
1077 #endif
1078
1079 /****************************************************************************/
1080 /** \brief Compute VNCLASS in all vectors of a grid level
1081
1082 * @param theGrid - pointer to grid
1083
1084 Computes values of VNCLASS field in all vectors after seed.
1085
1086 * @return <ul>
1087 * <li> 0 if ok </li>
1088 * <li> 1 if error occurred </li>
1089 </ul>
1090 */
1091 /****************************************************************************/
1092 INT NS_DIM_PREFIX PropagateNextVectorClasses (GRID *theGrid)
1093 {
1094 #ifdef ModelP
1095 auto& context = theGrid->dddContext();
1096 const auto& dddctrl = ddd_ctrl(context);
1097
1098 PRINTDEBUG(gm,1,("\nPropagateNextVectorClasses(): 1. communication\n"))
1099 /* exchange VNCLASS of vectors */
1100 DDD_IFAExchange(context,
1101 dddctrl.BorderVectorSymmIF,GRID_ATTR(theGrid),sizeof(INT),
1102 Gather_VectorVNClass, Scatter_VectorVNClass);
1103 #endif
1104
1105 #ifdef ModelP
1106 PRINTDEBUG(gm,1,("\nPropagateNextVectorClasses(): 2. communication\n"))
1107 /* exchange VNCLASS of vectors */
1108 DDD_IFAExchange(context,
1109 dddctrl.BorderVectorSymmIF,GRID_ATTR(theGrid),sizeof(INT),
1110 Gather_VectorVNClass, Scatter_VectorVNClass);
1111 #endif
1112
1113 #ifdef ModelP
1114 PRINTDEBUG(gm,1,("\nPropagateNextVectorClasses(): 3. communication\n"))
1115 /* exchange VNCLASS of vectors */
1116 DDD_IFAExchange(context,
1117 dddctrl.BorderVectorSymmIF,GRID_ATTR(theGrid),sizeof(INT),
1118 Gather_VectorVNClass, Scatter_VectorVNClass);
1119
1120 /* send VCLASS to ghosts */
1121 DDD_IFAOneway(context,
1122 dddctrl.VectorIF,GRID_ATTR(theGrid),IF_FORWARD,sizeof(INT),
1123 Gather_VectorVNClass, Scatter_GhostVectorVNClass);
1124 #endif
1125
1126 return(0);
1127 }
1128
1129 /*@}*/