| 1 |
// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root |
| 2 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
#include "config.h" |
| 4 |
|
| 5 |
#include <dune/common/exceptions.hh> |
| 6 |
|
| 7 |
#include "rm-write2file.h" |
| 8 |
|
| 9 |
#ifdef UG_DIM_3 |
| 10 |
static NS_DIM_PREFIX REFRULE Empty_Rule = |
| 11 |
{-1,-1,NS_DIM_PREFIX NO_CLASS,-1, |
| 12 |
{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}, |
| 13 |
-1, |
| 14 |
{{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}, |
| 15 |
{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}, |
| 16 |
{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1},{-1,-1}, |
| 17 |
{-1,-1}}, |
| 18 |
{{-1,{-1,-1,-1,-1,-1,-1,-1,-1},{-1,-1,-1,-1,-1,-1},-1}, |
| 19 |
{-1,{-1,-1,-1,-1,-1,-1,-1,-1},{-1,-1,-1,-1,-1,-1},-1}, |
| 20 |
{-1,{-1,-1,-1,-1,-1,-1,-1,-1},{-1,-1,-1,-1,-1,-1},-1}, |
| 21 |
{-1,{-1,-1,-1,-1,-1,-1,-1,-1},{-1,-1,-1,-1,-1,-1},-1}, |
| 22 |
{-1,{-1,-1,-1,-1,-1,-1,-1,-1},{-1,-1,-1,-1,-1,-1},-1}, |
| 23 |
{-1,{-1,-1,-1,-1,-1,-1,-1,-1},{-1,-1,-1,-1,-1,-1},-1}, |
| 24 |
{-1,{-1,-1,-1,-1,-1,-1,-1,-1},{-1,-1,-1,-1,-1,-1},-1}, |
| 25 |
{-1,{-1,-1,-1,-1,-1,-1,-1,-1},{-1,-1,-1,-1,-1,-1},-1}, |
| 26 |
{-1,{-1,-1,-1,-1,-1,-1,-1,-1},{-1,-1,-1,-1,-1,-1},-1}, |
| 27 |
{-1,{-1,-1,-1,-1,-1,-1,-1,-1},{-1,-1,-1,-1,-1,-1},-1}, |
| 28 |
{-1,{-1,-1,-1,-1,-1,-1,-1,-1},{-1,-1,-1,-1,-1,-1},-1}, |
| 29 |
{-1,{-1,-1,-1,-1,-1,-1,-1,-1},{-1,-1,-1,-1,-1,-1},-1}}}; |
| 30 |
|
| 31 |
|
| 32 |
template<class T> |
| 33 |
int writeArray(FILE* stream, const T* array, int const n) |
| 34 |
{ |
| 35 |
int num_chars = 0; |
| 36 |
for(int i=0; i < n;i++) |
| 37 |
{ |
| 38 |
num_chars += std::fprintf(stream,"%d,", static_cast<int>(array[i])); |
| 39 |
} |
| 40 |
return num_chars; |
| 41 |
} |
| 42 |
|
| 43 |
const char* tag2string(int const tag) |
| 44 |
{ |
| 45 |
switch(tag) |
| 46 |
{ |
| 47 |
case NS_DIM_PREFIX TETRAHEDRON: |
| 48 |
return "TETRAHEDRON"; |
| 49 |
case NS_DIM_PREFIX PYRAMID: |
| 50 |
return "PYRAMID"; |
| 51 |
case NS_DIM_PREFIX PRISM: |
| 52 |
return "PRISM"; |
| 53 |
case NS_DIM_PREFIX HEXAHEDRON: |
| 54 |
return "HEXAHEDRON"; |
| 55 |
default: |
| 56 |
DUNE_THROW(Dune::Exception, "tag2string: unknown tag " << tag); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
const char* class2string(int const rclass) |
| 61 |
{ |
| 62 |
switch(rclass) |
| 63 |
{ |
| 64 |
case NS_DIM_PREFIX NO_CLASS: |
| 65 |
return "NO_CLASS"; |
| 66 |
case NS_DIM_PREFIX YELLOW_CLASS: |
| 67 |
return "YELLOW_CLASS"; |
| 68 |
case NS_DIM_PREFIX GREEN_CLASS: |
| 69 |
return "GREEN_CLASS"; |
| 70 |
case NS_DIM_PREFIX RED_CLASS: |
| 71 |
return "RED_CLASS"; |
| 72 |
case NS_DIM_PREFIX SWITCH_CLASS: |
| 73 |
return "SWITCH_CLASS"; |
| 74 |
default: |
| 75 |
DUNE_THROW(Dune::Exception, "class2string: unknown class " << rclass); |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
int WriteSonData(std::FILE* const stream, NS_DIM_PREFIX sondata const& son) |
| 81 |
{ |
| 82 |
using std::fprintf; |
| 83 |
int num_chars = 0; |
| 84 |
|
| 85 |
// tag |
| 86 |
int tag = son.tag; |
| 87 |
const char* tag_s = tag2string(tag); |
| 88 |
num_chars += fprintf( stream,"{%s,{",tag_s); |
| 89 |
|
| 90 |
// corners |
| 91 |
num_chars += writeArray(stream, son.corners, MAX_CORNERS_OF_ELEM_DIM); |
| 92 |
num_chars += fprintf( stream,"},{"); |
| 93 |
|
| 94 |
// nb |
| 95 |
num_chars += writeArray(stream, son.nb, MAX_SIDES_OF_ELEM_DIM); |
| 96 |
num_chars += fprintf(stream ,"},%d}",son.path); |
| 97 |
|
| 98 |
return num_chars; |
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
void WriteRule2File(std::FILE* const stream, NS_DIM_PREFIX REFRULE const& theRule) |
| 103 |
{ |
| 104 |
using std::fprintf; |
| 105 |
|
| 106 |
// only used for alignment of comments |
| 107 |
int comment_row = 80; |
| 108 |
|
| 109 |
// tag, mark, rclass, nsons |
| 110 |
const char* tag_s = tag2string(theRule.tag); |
| 111 |
const char* rclass_s = class2string(theRule.rclass); |
| 112 |
int char0 = fprintf( stream," {%s,%d,%s,%d,",tag_s ,theRule.mark,rclass_s,theRule.nsons); |
| 113 |
fprintf( stream,"%*s// tag, mark, rclass, nsons\n", comment_row - char0," "); |
| 114 |
|
| 115 |
// pattern |
| 116 |
char0 = fprintf( stream," {"); |
| 117 |
char0 += writeArray(stream, theRule.pattern, MAX_NEW_CORNERS_DIM); |
| 118 |
fprintf( stream,"},%*s// pattern\n", comment_row - char0 -2, " "); |
| 119 |
|
| 120 |
// pat |
| 121 |
char0 = std::fprintf( stream," %d,",theRule.pat); |
| 122 |
fprintf( stream,"%*s// pat\n", comment_row - char0, " "); |
| 123 |
|
| 124 |
// sonandnode |
| 125 |
bool alreadyCommented = false; |
| 126 |
char0 = fprintf( stream," {"); |
| 127 |
for(int i=0; i < MAX_NEW_CORNERS_DIM; i++) |
| 128 |
{ |
| 129 |
char0 += fprintf( stream,"{%d,%d},",theRule.sonandnode[i][0],theRule.sonandnode[i][1]); |
| 130 |
// new line after 6 sons |
| 131 |
if( (i%6) ==0 && i !=0) |
| 132 |
{ |
| 133 |
// comment |
| 134 |
if (!alreadyCommented) |
| 135 |
{ |
| 136 |
fprintf( stream,"%*s// sonandnode", comment_row - char0, " "); |
| 137 |
alreadyCommented = !alreadyCommented; |
| 138 |
} |
| 139 |
fprintf( stream,"\n "); |
| 140 |
} |
| 141 |
} |
| 142 |
fprintf( stream,"},\n"); |
| 143 |
|
| 144 |
// sons |
| 145 |
alreadyCommented = false; |
| 146 |
char0 = fprintf( stream," {"); |
| 147 |
for(int i=0; i<MAX_SONS_DIM; i++) |
| 148 |
{ |
| 149 |
char0 += WriteSonData(stream, theRule.sons[i]); |
| 150 |
char0 += fprintf( stream,","); |
| 151 |
// comment |
| 152 |
if(!alreadyCommented) |
| 153 |
{ |
| 154 |
fprintf( stream,"%*s// sons", comment_row - char0, " "); |
| 155 |
alreadyCommented = !alreadyCommented; |
| 156 |
} |
| 157 |
fprintf( stream,"\n "); |
| 158 |
} |
| 159 |
|
| 160 |
fprintf( stream,"}}"); |
| 161 |
} |
| 162 |
|
| 163 |
void Write2File(std::FILE* stream, std::vector<NS_DIM_PREFIX REFRULE> const& rules, std::vector<NS_PREFIX SHORT> const& patterns) |
| 164 |
{ |
| 165 |
std::fprintf( stream, "// This file was generated by \"gm/rm3-writeRefRules2file\"\n\n"); |
| 166 |
|
| 167 |
std::fprintf( stream, "static const std::size_t nTetrahedronRefinementRules = %zd;\n", rules.size() ); |
| 168 |
std::fprintf( stream, "static REFRULE tetrahedronRefinementRules[] =\n{\n"); |
| 169 |
|
| 170 |
for (std::size_t i=0; i < rules.size(); i++) |
| 171 |
{ |
| 172 |
std::fprintf( stream, " // Rule %lu\n",i); |
| 173 |
WriteRule2File(stream, rules[i]); |
| 174 |
std::fprintf( stream,",\n\n"); |
| 175 |
} |
| 176 |
|
| 177 |
std::fprintf( stream, "};\n"); |
| 178 |
|
| 179 |
std::fprintf( stream, "static const NS_PREFIX SHORT pattern2RuleTetrahedron[%zd] = {", patterns.size()); |
| 180 |
writeArray(stream, patterns.data(), patterns.size()); |
| 181 |
std::fprintf( stream,"};\n"); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
START_UGDIM_NAMESPACE |
| 186 |
static void CorrectRule40 (refrule *theRule) |
| 187 |
{ |
| 188 |
for (int i : {1,4,8}) |
| 189 |
{ |
| 190 |
auto&& son = theRule->sons[i]; |
| 191 |
std::swap(son.corners[0],son.corners[1]); |
| 192 |
std::swap(son.nb[1],son.nb[2]); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
static void CorrectRule41 (refrule *theRule) |
| 197 |
{ |
| 198 |
for (int i : {1,4,7}) |
| 199 |
{ |
| 200 |
auto&& son = theRule->sons[i]; |
| 201 |
std::swap(son.corners[0],son.corners[1]); |
| 202 |
std::swap(son.nb[1],son.nb[2]); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
static void CorrectRule52 (refrule *theRule) |
| 207 |
{ |
| 208 |
for (int i : {2,8}) |
| 209 |
{ |
| 210 |
auto&& son = theRule->sons[i]; |
| 211 |
std::swap(son.corners[0],son.corners[1]); |
| 212 |
std::swap(son.nb[1],son.nb[2]); |
| 213 |
} |
| 214 |
} |
| 215 |
static void CorrectRule53 (refrule *theRule) |
| 216 |
{ |
| 217 |
for (int i : {2,7}) |
| 218 |
{ |
| 219 |
auto&& son = theRule->sons[i]; |
| 220 |
std::swap(son.corners[0],son.corners[1]); |
| 221 |
std::swap(son.nb[1],son.nb[2]); |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
static void CorrectRule85 (refrule *theRule) |
| 226 |
{ |
| 227 |
// i == 4 |
| 228 |
auto&& son = theRule->sons[4]; |
| 229 |
std::swap(son.corners[0],son.corners[1]); |
| 230 |
std::swap(son.nb[1],son.nb[2]); |
| 231 |
} |
| 232 |
|
| 233 |
static void CorrectRule86 (refrule *theRule) |
| 234 |
{ |
| 235 |
// i == 4 |
| 236 |
auto&& son = theRule->sons[4]; |
| 237 |
std::swap(son.corners[0],son.corners[1]); |
| 238 |
std::swap(son.nb[1],son.nb[2]); |
| 239 |
} |
| 240 |
|
| 241 |
static void CorrectRule111 (refrule *theRule) |
| 242 |
{ |
| 243 |
for (int i : {6,8}) |
| 244 |
{ |
| 245 |
auto&& son = theRule->sons[i]; |
| 246 |
std::swap(son.corners[0],son.corners[1]); |
| 247 |
std::swap(son.nb[1],son.nb[2]); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
static void CorrectRule112 (refrule *theRule) |
| 252 |
{ |
| 253 |
for (int i : {6,7}) |
| 254 |
{ |
| 255 |
auto&& son = theRule->sons[i]; |
| 256 |
std::swap(son.corners[0],son.corners[1]); |
| 257 |
std::swap(son.nb[1],son.nb[2]); |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
static void CorrectRule135 (refrule *theRule) |
| 262 |
{ |
| 263 |
for (int i : {1,3,11}) |
| 264 |
{ |
| 265 |
auto&& son = theRule->sons[i]; |
| 266 |
std::swap(son.corners[0],son.corners[1]); |
| 267 |
std::swap(son.nb[1],son.nb[2]); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
static void CorrectRule136 (refrule *theRule) |
| 272 |
{ |
| 273 |
for (int i : {2,3,11}) |
| 274 |
{ |
| 275 |
auto&& son = theRule->sons[i]; |
| 276 |
std::swap(son.corners[0],son.corners[1]); |
| 277 |
std::swap(son.nb[1],son.nb[2]); |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
static void CorrectRule155 (refrule *theRule) |
| 282 |
{ |
| 283 |
for (int i : {5,7,9}) |
| 284 |
{ |
| 285 |
auto&& son = theRule->sons[i]; |
| 286 |
std::swap(son.corners[0],son.corners[1]); |
| 287 |
std::swap(son.nb[1],son.nb[2]); |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
static void CorrectRule156 (refrule *theRule) |
| 292 |
{ |
| 293 |
for (int i : {5,8,9}) |
| 294 |
{ |
| 295 |
auto&& son = theRule->sons[i]; |
| 296 |
std::swap(son.corners[0],son.corners[1]); |
| 297 |
std::swap(son.nb[1],son.nb[2]); |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
static void CorrectRule183 (refrule *theRule) |
| 302 |
{ |
| 303 |
for (int i : {3,7,11}) |
| 304 |
{ |
| 305 |
auto&& son = theRule->sons[i]; |
| 306 |
std::swap(son.corners[0],son.corners[1]); |
| 307 |
std::swap(son.nb[1],son.nb[2]); |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
static void CorrectRule184 (refrule *theRule) |
| 312 |
{ |
| 313 |
for (int i : {2,8,11}) |
| 314 |
{ |
| 315 |
auto&& son = theRule->sons[i]; |
| 316 |
std::swap(son.corners[0],son.corners[1]); |
| 317 |
std::swap(son.nb[1],son.nb[2]); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
static int FReadRule (FILE *stream, REFRULE *theRule) |
| 322 |
{ |
| 323 |
int ns,p0,p1,p2,p3,p4,p5,pat; |
| 324 |
|
| 325 |
// init tag |
| 326 |
theRule->tag = TETRAHEDRON; |
| 327 |
|
| 328 |
if (fscanf(stream,"%d %d %d %d %d %d %d %d",&ns,&p0,&p1,&p2,&p3,&p4,&p5,&pat)!=8) return (1); |
| 329 |
|
| 330 |
theRule->nsons = ns; |
| 331 |
theRule->pattern[0] = p0; |
| 332 |
theRule->pattern[1] = p1; |
| 333 |
theRule->pattern[2] = p2; |
| 334 |
theRule->pattern[3] = p3; |
| 335 |
theRule->pattern[4] = p4; |
| 336 |
theRule->pattern[5] = p5; |
| 337 |
theRule->pat = pat; |
| 338 |
|
| 339 |
// MAXEDGES = 16 for tetrahedra |
| 340 |
int type,from,to,side; |
| 341 |
for (int i=0; i<16; i++) |
| 342 |
{ |
| 343 |
if (fscanf(stream," %d %d %d %d",&type,&from,&to,&side)!=4) return (1); |
| 344 |
} |
| 345 |
// MAX_SONS = 12 for tetrahedra |
| 346 |
for (int i=0; i<12; i++) |
| 347 |
{ |
| 348 |
int c0,c1,c2,c3,n0,n1,n2,n3,pa; |
| 349 |
if (fscanf(stream," %d %d %d %d %d %d %d %d %d",&c0,&c1,&c2,&c3,&n0,&n1,&n2,&n3,&pa)!=9) return (1); |
| 350 |
theRule->sons[i].tag = TETRAHEDRON; |
| 351 |
if (c0 == 10) c0 = 14; |
| 352 |
theRule->sons[i].corners[0] = c0; |
| 353 |
if (c1 == 10) c1 = 14; |
| 354 |
theRule->sons[i].corners[1] = c1; |
| 355 |
if (c2 == 10) c2 = 14; |
| 356 |
theRule->sons[i].corners[2] = c2; |
| 357 |
if (c3 == 10) c3 = 14; |
| 358 |
theRule->sons[i].corners[3] = c3; |
| 359 |
theRule->sons[i].nb[0] = n0; |
| 360 |
theRule->sons[i].nb[1] = n1; |
| 361 |
theRule->sons[i].nb[2] = n2; |
| 362 |
theRule->sons[i].nb[3] = n3; |
| 363 |
theRule->sons[i].path = pa; |
| 364 |
static const int nSidesOfTetrahedron = 4; /* == SIDES_OF_TAG(TETRAHEDRON) */ |
| 365 |
for (int j=0; j < nSidesOfTetrahedron; j++) |
| 366 |
if (theRule->sons[i].nb[j]>=TET_RULE_FATHER_SIDE_OFFSET) |
| 367 |
theRule->sons[i].nb[j] += FATHER_SIDE_OFFSET-TET_RULE_FATHER_SIDE_OFFSET; |
| 368 |
} |
| 369 |
|
| 370 |
// NEWCORNERS = 7 for tetrahedra |
| 371 |
// read edge node information |
| 372 |
int sn0,sn1; |
| 373 |
for (int i=0; i<6; i++) |
| 374 |
{ |
| 375 |
if (fscanf(stream," %d %d",&sn0,&sn1)!=2) return (1); |
| 376 |
theRule->sonandnode[i][0] = sn0; |
| 377 |
theRule->sonandnode[i][1] = sn1; |
| 378 |
} |
| 379 |
// read center node information |
| 380 |
if (fscanf(stream," %d %d",&sn0,&sn1)!=2) return (1); |
| 381 |
theRule->sonandnode[10][0] = sn0; |
| 382 |
theRule->sonandnode[10][1] = sn1; |
| 383 |
|
| 384 |
// init Pattern and pat for center node |
| 385 |
if (theRule->sonandnode[10][0] != NO_CENTER_NODE) |
| 386 |
{ |
| 387 |
theRule->pattern[10] = 1; |
| 388 |
theRule->pat |= (1<<10); |
| 389 |
} |
| 390 |
|
| 391 |
return (0); |
| 392 |
} |
| 393 |
|
| 394 |
|
| 395 |
void readTetrahedronRules(FILE* stream, std::vector<NS_DIM_PREFIX REFRULE>& rules, std::vector<NS_PREFIX SHORT>& patterns) |
| 396 |
{ |
| 397 |
USING_UGDIM_NAMESPACE |
| 398 |
USING_UG_NAMESPACE |
| 399 |
|
| 400 |
{ |
| 401 |
int nRules; |
| 402 |
int nPatterns; |
| 403 |
|
| 404 |
// read Rules and nPatterns from file |
| 405 |
if (std::fscanf(stream,"%d %d\n",&nRules,&nPatterns)!=2) |
| 406 |
DUNE_THROW(Dune::Exception, "failed to read nRules and nPatterns from file"); |
| 407 |
|
| 408 |
rules.assign(nRules, Empty_Rule); |
| 409 |
patterns.assign(nPatterns, -1); |
| 410 |
} |
| 411 |
|
| 412 |
// read Rules |
| 413 |
{ |
| 414 |
int i = 0; |
| 415 |
for (auto& rule : rules) { |
| 416 |
rule.mark = i++; |
| 417 |
rule.rclass = RED_CLASS|GREEN_CLASS; |
| 418 |
if (FReadRule(stream, &rule)) |
| 419 |
DUNE_THROW(Dune::Exception, "failed to read rule"); |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
for (auto& pattern : patterns) { |
| 424 |
if (std::fscanf(stream,"%hd",&pattern)!=1) |
| 425 |
DUNE_THROW(Dune::Exception, "failed to read pattern from file"); |
| 426 |
} |
| 427 |
|
| 428 |
// bug fix |
| 429 |
CorrectRule40(&rules[40]); |
| 430 |
CorrectRule41(&rules[41]); |
| 431 |
CorrectRule52(&rules[52]); |
| 432 |
CorrectRule53(&rules[53]); |
| 433 |
CorrectRule85(&rules[85]); |
| 434 |
CorrectRule86(&rules[86]); |
| 435 |
CorrectRule111(&rules[111]); |
| 436 |
CorrectRule112(&rules[112]); |
| 437 |
CorrectRule135(&rules[135]); |
| 438 |
CorrectRule136(&rules[136]); |
| 439 |
CorrectRule155(&rules[155]); |
| 440 |
CorrectRule156(&rules[156]); |
| 441 |
CorrectRule183(&rules[183]); |
| 442 |
CorrectRule184(&rules[184]); |
| 443 |
} |
| 444 |
|
| 445 |
END_UGDIM_NAMESPACE |
| 446 |
#endif //__THREEDIM |