1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 /*! \file rm.h
4  * \ingroup gm
5  */
6 
7 
8 /****************************************************************************/
9 /*                                                                                                                                                      */
10 /* File:          rule manager header file                                                                              */
11 /*                                                                                                                                                      */
12 /* Purpose:   defines data structures for refinement rules                                      */
13 /*                                                                                                                                                      */
14 /* Author:        Stefan Lang                                                                           */
15 /*                        Institut fuer Computeranwendungen III                                                 */
16 /*                        Universitaet Stuttgart                                                                                */
17 /*                        Pfaffenwaldring 27                                                                                    */
18 /*                        70550 Stuttgart                                                                                               */
19 /*                                                                                                                                                      */
20 /* History:   20.11.95 begin, ugp version 3.0                                                           */
21 /*                                                                                                                                                      */
22 /* Remarks:                                                                                                                             */
23 /*                                                                                                                                                      */
24 /****************************************************************************/
25 
26 
27 /****************************************************************************/
28 /*                                                                                                                                                      */
29 /* auto include mechanism and other include files                                                       */
30 /*                                                                                                                                                      */
31 /****************************************************************************/
32 
33 #ifndef __RULEMANAGER__
34 #define __RULEMANAGER__
35 
36 #include <memory>
37 
38 #include "gm.h"
39 #include "refine.h"
40 
41 #include <dune/uggrid/low/namespace.h>
42 
43 START_UGDIM_NAMESPACE
44 
45 /****************************************************************************/
46 /*                                                                                                                                                      */
47 /* defines in the following order                                                                                       */
48 /*                                                                                                                                                      */
49 /*                compile time constants defining static data size (i.e. arrays)        */
50 /*                other constants                                                                                                       */
51 /*                macros                                                                                                                        */
52 /*                                                                                                                                                      */
53 /****************************************************************************/
54 
55 /* Declaration of DUNE_UGGRID_TET_RULESET has been moved to config.h */
56 /* uncomment this if you want to use the full rule set for tetrahedra */
57 
58 /** \brief Defines for edge types */
59 enum {INNER_EDGE        = 1,
60       SIDE_EDGE         = 2,
61       HALF_FATHER_EDGE  = 3,
62       FATHER_EDGE       = 4};
63 
64 #define NEXTSIDEMASKHEX         0x00000007
65 #define NEXTSIDEHEX(i,n)        (((i) & (NEXTSIDEMASKHEX<<(3*(n))))>>(3*(n)))
66 
67 #define FATHER_SIDE_OFFSET 100 /* greater values indicate outside faces */
68 
69 #define MAX_NEW_CORNERS(tag) MaxNewCorners[(tag)]       /* midpoints on edges and sides plus center           */
70 #define MAX_NEW_EDGES(tag)   MaxNewEdges[(tag)]         /* maximal new edges of type 1/2                      */
71 #define MAX_RULES(tag)       MaxRules[(tag)]            /* number of rules for one element type               */
72 #define CENTER_NODE_INDEX(e) CenterNodeIndex[TAG(e)]  /* index of centernode in sonandnode and elemcontext */
73 #define CENTER_NODE_INDEX_TAG(t) CenterNodeIndex[(t)]     /* index of centernode in sonandnode and elemcontext */
74 
75 #define MARK2RULE(e,m)          (m)
76 #define MARK2RULEADR(e,m)       (&(RefRules[TAG(e)][(m)]))
77 #define RULE2PATTERN(e,r)       (RefRules[TAG(e)][(r)].pattern)
78 #define RULE2PAT(e,r)           (RefRules[TAG(e)][(r)].pat)
79 #define MARK2PAT(e,r)           (RefRules[TAG(e)][(r)].pat)
80 #define MARK2PATTERN(e,m)       (RefRules[TAG(e)][(m)].pattern)
81 
82 #define PATTERN2RULE(e,p)       (Patterns2Rules((e),(p)))
83 #define RULE2MARK(e,r)          (RefRules[TAG(e)][(r)].mark)
84 #define PATTERN2MARK(e,p)       (((PATTERN2RULE((e),(p)))>=0) ? (RefRules[TAG(e)][PATTERN2RULE((e),(p))].mark) : -1)
85 
86 #ifdef __SR2201__
87 #define NODE_OF_RULE(e,m,i) ((*MARK2RULEADR((e),(m))).sonandnode[(i)][0]!=-1)
88 #else
89 #define NODE_OF_RULE(e,m,i)     (MARK2RULEADR((e),(m))->sonandnode[(i)][0]!=-1)
90 #endif
91 
92 #define CONCAT(a,b,c)            CONCAT_AUX(a,b,c)
93 #define CONCAT_AUX(a,b,c)        a ## b ## c
94 
95 /* dimension dependent MAX_CORNERS_OF_ELEM */
96 #define MAX_CORNERS_OF_ELEM_2D  4
97 #define MAX_CORNERS_OF_ELEM_3D  8
98 #define MAX_CORNERS_OF_ELEM_DIM CONCAT(MAX_CORNERS_OF_ELEM_,DIM,D)
99 
100 /* dimension dependent MAX_EDGES_OF_ELEM */
101 #define MAX_EDGES_OF_ELEM_2D    4
102 #define MAX_EDGES_OF_ELEM_3D    12
103 #define MAX_EDGES_OF_ELEM_DIM CONCAT(MAX_EDGES_OF_ELEM_,DIM,D)
104 
105 /* dimension dependent MAX_NEW_CORNERS */
106 #define MAX_NEW_CORNERS_2D    5
107 #define MAX_NEW_CORNERS_3D   19
108 #define MAX_NEW_CORNERS_DIM CONCAT(MAX_NEW_CORNERS_,DIM,D)
109 
110 /* dimension dependent MAX_NEW_EDGES */
111 #define MAX_NEW_EDGES_2D   12
112 #define MAX_NEW_EDGES_3D   54
113 #define MAX_NEW_EDGES_DIM CONCAT(MAX_NEW_EDGES_,DIM,D)
114 
115 /* dimension dependent MAX_SIDES_OF_ELEM */
116 #define MAX_SIDES_OF_ELEM_2D    4
117 #define MAX_SIDES_OF_ELEM_3D    6
118 #define MAX_SIDES_OF_ELEM_DIM CONCAT(MAX_SIDES_OF_ELEM_,DIM,D)
119 
120 /* dimension dependent MAX_SONS */
121 #define MAX_SONS_2D    4
122 #define MAX_SONS_3D    12
123 #define MAX_SONS_DIM CONCAT(MAX_SONS_,DIM,D)
124 
125 #if FATHER_SIDE_OFFSET<MAX_SONS
126 #error  /* increase FATHER_SIDE_OFFSET correspondigly */
127 #endif
128 
129 /* max fine grid nodes of an element */
130 #define MAX_REFINED_CORNERS_DIM (MAX_CORNERS_OF_ELEM+MAX_NEW_CORNERS_DIM)
131 
132 #define IS_REFINED(e)           (REFINE(e)!=NO_REFINEMENT)
133 #define MARKED(e)               (MARK(e)!=NO_REFINEMENT)
134 #define LEAFELEM(e)                     (!IS_REFINED(e))
135 
136 /** \brief Indices of rules in rule array */
137 enum {T_NOREF               = 0,
138       T_COPY                = 1,
139       T_RED                 = 2,
140       T_BISECT_1_0          = 3,
141       T_BISECT_1_1          = 4,
142       T_BISECT_1_2          = 5,
143       T_BISECT_2_T1_0       = 7,
144       T_BISECT_2_T1_1       = 8,
145       T_BISECT_2_T1_2       = 6,
146       T_BISECT_2_T2_0       = 11,
147       T_BISECT_2_T2_1       = 9,
148       T_BISECT_2_T2_2       = 10,
149       T_BISECT_2_Q_0        = 12,
150       T_BISECT_2_Q_1        = 13,
151       T_BISECT_2_Q_2        = 14,
152       T_BISECT_3_0          = 15,
153       T_BISECT_3_1          = 16,
154       T_BISECT_3_2          = 17};
155 
156 enum {Q_NOREF,
157       Q_COPY,
158       Q_RED,
159       Q_CLOSE_1_0,
160       Q_CLOSE_1_1,
161       Q_CLOSE_1_2,
162       Q_CLOSE_1_3,
163       Q_BLUE_0,
164       Q_BLUE_1,
165       Q_CLOSE_2_0,
166       Q_CLOSE_2_1,
167       Q_CLOSE_2_2,
168       Q_CLOSE_2_3,
169       Q_CLOSE_3_0,
170       Q_CLOSE_3_1,
171       Q_CLOSE_3_2,
172       Q_CLOSE_3_3};
173 
174 #define TET_COPY                        1
175 #ifdef DUNE_UGGRID_TET_RULESET
176 #define FULL_REFRULE        Pattern2Rule[TETRAHEDRON][0x3F]
177 #define FULL_REFRULE_0_5    (Pattern2Rule[TETRAHEDRON][0x3F]+1)
178 #define FULL_REFRULE_1_3    (Pattern2Rule[TETRAHEDRON][0x3F]+2)
179 #define FULL_REFRULE_2_4    (Pattern2Rule[TETRAHEDRON][0x3F]+0)
180 #define TET_RED                         FULL_REFRULE
181 #define TET_RED_0_5                     FULL_REFRULE_0_5
182 #define TET_RED_1_3                     FULL_REFRULE_1_3
183 #define TET_RED_2_4                     FULL_REFRULE_2_4
184 #else
185 #define TET_RED                         2
186 #define TET_RED_0_5                     3
187 #define TET_RED_1_3                     4
188 #define TET_RED_2_4                     2
189 #define FULL_REFRULE        TET_RED
190 #define FULL_REFRULE_0_5    TET_RED_0_5
191 #define FULL_REFRULE_1_3    TET_RED_1_3
192 #define FULL_REFRULE_2_4    TET_RED_2_4
193 #define TET_RED_HEX                     5
194 #endif
195 
196 enum {PYR_COPY       = 1,
197       PYR_RED        = 2,
198       PYR_BISECT_0_1 = 3,
199       PYR_BISECT_0_2 = 4};
200 
201 enum {PRI_COPY             = 1,
202       PRI_RED              = 2,
203       PRI_QUADSECT         = 3,
204       PRI_BISECT_0_1       = 4,
205       PRI_BISECT_0_2       = 5,
206       PRI_BISECT_0_3       = 6,
207       PRI_BISECT_1_2       = 7,
208       PRI_BISECT_HEX0      = 8,
209       PRI_BISECT_HEX1      = 9,
210       PRI_BISECT_HEX2      = 10,
211       PRI_RED_HEX          = 11,
212       PRI_ROT_L            = 12,
213       PRI_ROT_R            = 13,
214       PRI_QUADSECT_HEXPRI0 = 14};
215 
216 enum {HEXA_COPY           = 1,
217       HEXA_RED            = 2,
218       HEXA_BISECT_0_1     = 3,
219       HEXA_BISECT_0_2     = 4,
220       HEXA_BISECT_0_3     = 5,
221       HEXA_QUADSECT_0     = 6,
222       HEXA_QUADSECT_1     = 7,
223       HEXA_QUADSECT_2     = 8,
224       HEXA_TRISECT_0      = 9,
225       HEXA_TRISECT_5      = 10,
226       HEXA_BISECT_HEXPRI0 = 11,
227       HEXA_BISECT_HEXPRI1 = 12};
228 
229 
230 /****************************************************************************/
231 /*                                                                                                                                                      */
232 /* macros for rules                                                                     */
233 /*                                                                                                                                                      */
234 /****************************************************************************/
235 
236 #define TAG_OF_RULE(r)              ((r)->tag)
237 #define MARK_OF_RULE(r)             ((r)->mark)
238 #define CLASS_OF_RULE(r)            ((r)->rclass)
239 #define NSONS_OF_RULE(r)            ((r)->nsons)
240 #define SON_OF_RULE(r,s)            (&((r)->sons[(s)]))
241 #define PATTERN_OF_RULE(r,i)            ((r)->pattern[(i)])
242 #define PAT_OF_RULE(r)                          ((r)->pat)
243 #define SON_OF_NODE_OF_RULE(r,n)        ((r)->sonandnode[(n)][0])
244 #define SONNODE_OF_NODE_OF_RULE(r,n)((r)->sonandnode[(n)][0])
245 #define SON_TAG_OF_RULE(r,s)            ((r)->sons[(s)].tag)
246 #define SON_TAG(s)                                      ((s)->tag)
247 #define SON_CORNER_OF_RULE(r,s,n)       ((r)->sons[(s)].corners[(n)])
248 #define SON_CORNER(s,n)                         ((s)->corners[(n)])
249 #define SON_NB_OF_RULE(r,s,n)           ((r)->sons[(s)].nb[(n)])
250 #define SON_NB(s,n)                                     ((s)->nb[(n)])
251 #define SON_PATH_OF_RULE(r,s)           ((r)->sons[(s)].path)
252 #define SON_PATH(s)                                     ((s)->path)
253 
254 /* macros for referencing of sons paths */
255 /* 4 high bits for no of neighbours to be passed */
256 #define PATHDEPTHMASK 0xF0000000
257 #define PATHDEPTHSHIFT 28
258 #define PATHDEPTH(i)                            (((i) & PATHDEPTHMASK)>>PATHDEPTHSHIFT)
259 #define SETPATHDEPTH(i,val)             (i) = ((i)&(~PATHDEPTHMASK))|(((val)<<PATHDEPTHSHIFT)&PATHDEPTHMASK)
260 
261 /* 3 bits at position n for element side */
262 #define NEXTSIDEMASK 0x00000007
263 #define NEXTSIDE(i,n)                           (((i) & (NEXTSIDEMASK<<(3*(n))))>>(3*(n)))
264 #define SETNEXTSIDE(i,n,val)            (i) = ((i)&(~(NEXTSIDEMASK<<(3*(n)))))|(((val)&NEXTSIDEMASK)<<(3*(n)))
265 
266 #define MAX_PATH_DEPTH                          (PATHDEPTHSHIFT/3)
267 
268 #define NOCLASS(c)                                      ((c) == 0)
269 #define YELLOWCLASS(c)                          ((c) & 1)
270 #define GREENCLASS(c)                           ((c) & 2)
271 #define REDCLASS(c)                                     ((c) & 3)
272 #define SWITCHCLASS(c)                          ((c) & 4)
273 
274 /****************************************************************************/
275 /*                                                                          */
276 /* data structures exported by the corresponding source file                */
277 /*                                                                          */
278 /****************************************************************************/
279 
280 typedef INT (*FULLREFRULEPTR)(ELEMENT *);
281 
282 typedef struct {
283   /** \brief Fields for environment list variable */
284   NS_PREFIX ENVVAR v;
285 
286   /* full ref rule specific stuff */
287   FULLREFRULEPTR theFullRefRule;               /* the best full refrule                    */
288 
289 } FULLREFRULE;
290 
291 struct sondata {
292   SHORT tag;                                     /* which element type is the son                           */
293   SHORT corners[MAX_CORNERS_OF_ELEM_DIM];        /* corners of the son                                  */
294   SHORT nb[MAX_SIDES_OF_ELEM_DIM];               /* neighbors of this son                               */
295   /* < FATHER_SIDE_OFFSET if nb has same father  */
296   /* >= FATHER_SIDE_OFFSET if nb has other father*/
297   INT path;                                      /* path used in GetSons() for tetras                   */
298 };
299 
300 struct refrule {
301   SHORT tag;                                                        /* which element type this rule can refine */
302   SHORT mark;                                                       /* the mark of this rule                   */
303   SHORT rclass;                                                     /* class of rule:3bits for COPY, IREG, REG */
304   SHORT nsons;                                              /* number of sons rule creates             */
305   SHORT pattern[MAX_NEW_CORNERS_DIM];                       /* stores which edges are refined          */
306   INT pat;                                                      /* bitwise format of pattern               */
307   SHORT sonandnode[MAX_NEW_CORNERS_DIM][2];                 /* for each new node the number of the son */
308   /* and the local node number of the node   */
309   struct sondata sons[MAX_SONS_DIM];
310 };
311 
312 typedef struct sondata SONDATA;
313 typedef struct refrule REFRULE;
314 
315 
316 /****************************************************************************/
317 /*                                                                                                                                                      */
318 /* definition of exported global variables                                                                      */
319 /*                                                                                                                                                      */
320 /****************************************************************************/
321 
322 extern INT MaxRules[TAGS];
323 extern INT MaxNewCorners[TAGS];
324 extern INT MaxNewEdges[TAGS];
325 extern INT CenterNodeIndex[TAGS];
326 extern REFRULE                  *RefRules[TAGS];
327 extern const SHORT* Pattern2Rule[TAGS];
328 extern FULLREFRULEPTR theFullRefRule;
329 
330 
331 /****************************************************************************/
332 /*                                                                                                                                                      */
333 /* function declarations                                                                                                        */
334 /*                                                                                                                                                      */
335 /****************************************************************************/
336 
337 INT ShowRefRule         (INT tag, INT nb);
338 INT ShowRefRuleX        (INT tag, INT nb, PrintfProcPtr Printf);
339 
340 INT                     InitRuleManager                 (void);
341 INT                     Patterns2Rules                  (ELEMENT *theElement,INT pattern);
342 ELEMENT         *ELEMENT_TO_MARK                (ELEMENT *theElement);
343 
344 #ifdef __THREEDIM__
345 INT             GetRule_AnisotropicRed  (ELEMENT *theElement, INT *Rule);
346 #endif
347 
348 END_UGDIM_NAMESPACE
349 
350 #endif
351