-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmesh.cxx
1804 lines (1585 loc) · 58.9 KB
/
mesh.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <limits>
#include <string>
#include <sstream>
#ifdef THREED
#define TETLIBRARY
#include "tetgen/tetgen.h"
#undef TETLIBRARY
#endif // THREED
#ifdef USEEXODUS
#include "netcdf.h"
#include "exodusII.h"
#endif // USEEXODUS
#define REAL double
#define VOID void
#define ANSI_DECLARATORS
#include "triangle/triangle.h"
#undef REAL
#undef VOID
#undef ANSI_DECLARATORS
#include "constants.hpp"
#include "parameters.hpp"
#include "sortindex.hpp"
#include "utils.hpp"
#include "mesh.hpp"
#include "markerset.hpp"
#ifdef WIN32
#ifndef _MSC_VER
namespace std {
static std::string to_string(long double t)
{
char temp[32];
sprintf(temp,"%f",double(t));
return std::string(temp);
}
}
#endif //_MSC_VER
#endif // WIN32
namespace { // anonymous namespace
void set_verbosity_str(std::string &verbosity, int meshing_verbosity)
{
switch (meshing_verbosity) {
case -1:
verbosity = "Q";
break;
case 0:
verbosity = "";
break;
case 1:
verbosity = "V";
break;
case 2:
verbosity = "VV";
break;
case 3:
verbosity = "VVV";
break;
default:
verbosity = "";
break;
}
}
void set_volume_str(std::string &vol, double max_volume)
{
vol.clear();
if (max_volume > 0) {
vol += 'a';
vol += std::to_string((long double)max_volume);
}
else if (max_volume == 0) {
// max_volume is set inside regional attributes (usu. read from poly files)
vol += 'a';
}
}
void set_2d_quality_str(std::string &quality, double min_angle)
{
quality.clear();
if (min_angle > 0) {
quality += 'q';
quality += std::to_string((long double)min_angle);
}
}
void triangulate_polygon
(double min_angle, double max_area,
int meshing_verbosity,
int npoints, int nsegments,
const double *points, const int *segments, const int *segflags,
const int nregions, const double *regionattributes,
int *noutpoints, int *ntriangles, int *noutsegments,
double **outpoints, int **triangles,
int **outsegments, int **outsegflags, double **outregattr)
{
char options[255];
triangulateio in, out;
std::string verbosity, vol, quality;
set_verbosity_str(verbosity, meshing_verbosity);
set_volume_str(vol, max_area);
set_2d_quality_str(quality, min_angle);
if( nregions > 0 )
std::sprintf(options, "%s%spjz%sA", verbosity.c_str(), quality.c_str(), vol.c_str());
else
std::sprintf(options, "%s%spjz%s", verbosity.c_str(), quality.c_str(), vol.c_str());
if( meshing_verbosity >= 0 )
std::cout << "The meshing option is: " << options << '\n';
in.pointlist = const_cast<double*>(points);
in.pointattributelist = NULL;
in.pointmarkerlist = NULL;
in.numberofpoints = npoints;
in.numberofpointattributes = 0;
in.trianglelist = NULL;
in.triangleattributelist = NULL;
in.trianglearealist = NULL;
in.numberoftriangles = 0;
in.numberofcorners = 3;
in.numberoftriangleattributes = 0;
in.segmentlist = const_cast<int*>(segments);
in.segmentmarkerlist = const_cast<int*>(segflags);
in.numberofsegments = nsegments;
in.holelist = NULL;
in.numberofholes = 0;
in.numberofregions = nregions;
if( nregions > 0 )
in.regionlist = const_cast<double*>(regionattributes);
else
in.regionlist = NULL;
out.pointlist = NULL;
out.pointattributelist = NULL;
out.pointmarkerlist = NULL;
out.trianglelist = NULL;
out.triangleattributelist = NULL;
out.neighborlist = NULL;
out.segmentlist = NULL;
out.segmentmarkerlist = NULL;
out.edgelist = NULL;
out.edgemarkerlist = NULL;
/*******************************/
triangulate(options, &in, &out, NULL);
/*******************************/
*noutpoints = out.numberofpoints;
*outpoints = out.pointlist;
*ntriangles = out.numberoftriangles;
*triangles = out.trianglelist;
*noutsegments = out.numberofsegments;
*outsegments = out.segmentlist;
*outsegflags = out.segmentmarkerlist;
*outregattr = out.triangleattributelist;
trifree(out.pointmarkerlist);
}
void set_3d_quality_str(std::string &quality, double max_ratio,
double min_dihedral_angle, double max_dihedral_angle)
{
quality.clear();
if (max_ratio > 0) {
quality += 'q';
quality += std::to_string((long double)max_ratio);
quality += "qq";
quality += std::to_string((long double)min_dihedral_angle);
quality += "qqq";
quality += std::to_string((long double)max_dihedral_angle);
}
}
#ifdef THREED
void tetrahedralize_polyhedron
(double max_ratio, double min_dihedral_angle, double max_volume,
int vertex_per_polygon, int meshing_verbosity, int optlevel,
int npoints, int nsegments,
const double *points, const int *segments, const int *segflags,
const tetgenio::facet *facets,
const int nregions, const double *regionattributes,
int *noutpoints, int *ntriangles, int *noutsegments,
double **outpoints, int **triangles,
int **outsegments, int **outsegflags, double **outregattr)
{
//
// Setting Tetgen options.
//
char options[255];
double max_dihedral_angle = 180 - 3 * min_dihedral_angle;
std::string verbosity, vol, quality;
set_verbosity_str(verbosity, meshing_verbosity);
set_volume_str(vol, max_volume);
set_3d_quality_str(quality, max_ratio, min_dihedral_angle, max_dihedral_angle);
if( nregions > 0 )
std::sprintf(options, "%s%s%spzs%dA", verbosity.c_str(), quality.c_str(), vol.c_str(), optlevel);
else
std::sprintf(options, "%s%s%spzs%d", verbosity.c_str(), quality.c_str(), vol.c_str(), optlevel);
if( meshing_verbosity >= 0 )
std::cout << "The meshing option is: " << options << '\n';
//
// Setting input arrays to tetgen
//
// NOTE: all tetgenio pointers will need to be
// reset to NULL to prevent double-free error
tetgenio in;
in.pointlist = const_cast<double*>(points);
in.numberofpoints = npoints;
tetgenio::polygon *polys;
tetgenio::facet *fl;
if (facets == NULL) {
polys = new tetgenio::polygon[nsegments];
for (int i=0; i<nsegments; ++i) {
polys[i].vertexlist = const_cast<int*>(&segments[i*vertex_per_polygon]);
polys[i].numberofvertices = vertex_per_polygon;
}
fl = new tetgenio::facet[nsegments];
for (int i=0; i<nsegments; ++i) {
fl[i].polygonlist = &polys[i];
fl[i].numberofpolygons = 1;
fl[i].holelist = NULL;
fl[i].numberofholes = 0;
}
}
else {
fl = const_cast<tetgenio::facet*>(facets);
}
in.facetlist = fl;
in.facetmarkerlist = const_cast<int*>(segflags);
in.numberoffacets = nsegments;
in.holelist = NULL;
in.numberofholes = 0;
in.numberofregions = nregions;
if( nregions > 0 )
in.regionlist = const_cast<double*>(regionattributes);
else
in.regionlist = NULL;
tetgenio out;
/*******************************/
tetrahedralize(options, &in, &out, NULL, NULL);
/*******************************/
// the destructor of tetgenio will free any non-NULL pointer
// set in.pointers to NULL to prevent double-free
in.pointlist = NULL;
in.facetmarkerlist = NULL;
in.facetlist = NULL;
in.regionlist = NULL;
if (facets == NULL) {
delete [] polys;
delete [] fl;
}
*noutpoints = out.numberofpoints;
*outpoints = out.pointlist;
out.pointlist = NULL;
*ntriangles = out.numberoftetrahedra;
*triangles = out.tetrahedronlist;
out.tetrahedronlist = NULL;
*noutsegments = out.numberoftrifaces;
*outsegments = out.trifacelist;
*outsegflags = out.trifacemarkerlist;
*outregattr = out.tetrahedronattributelist;
out.trifacelist = NULL;
out.trifacemarkerlist = NULL;
out.tetrahedronattributelist = NULL;
}
#endif
void points_to_mesh(const Param ¶m, Variables &var,
int npoints, const double *points,
int n_init_segments, const int *init_segments, const int *init_segflags,
int nregions, const double *regattr,
double max_elem_size, int vertex_per_polygon)
{
double *pcoord, *pregattr;
int *pconnectivity, *psegment, *psegflag;
points_to_new_mesh(param.mesh, npoints, points,
n_init_segments, init_segments, init_segflags,
nregions, regattr,
max_elem_size, vertex_per_polygon,
var.nnode, var.nelem, var.nseg,
pcoord, pconnectivity, psegment, psegflag, pregattr);
var.coord = new array_t(pcoord, var.nnode);
var.connectivity = new conn_t(pconnectivity, var.nelem);
var.segment = new segment_t(psegment, var.nseg);
var.segflag = new segflag_t(psegflag, var.nseg);
var.regattr = new regattr_t(pregattr, var.nelem);
}
void new_mesh_uniform_resolution(const Param& param, Variables& var)
{
int npoints = 4 * (NDIMS - 1); // 2D:4; 3D:8
double *points = new double[npoints*NDIMS];
int n_init_segments = 2 * NDIMS; // 2D:4; 3D:6
int n_segment_nodes = 2 * (NDIMS - 1); // 2D:2; 3D:4
int *init_segments = new int[n_init_segments*n_segment_nodes];
int *init_segflags = new int[n_init_segments];
const int attr_ndata = NDIMS+2;
const int nregions = (param.ic.mattype_option == 0) ? param.mat.nmat : 1;
double *regattr = new double[nregions*attr_ndata]; // each region has (NDIMS+2) data fields: x, (y,) z, region marker (mat type), and volume.
double elem_size; // size of a typical element
int vertex_per_polygon = 4;
#ifndef THREED
{
/* Define 4 corner points of the rectangle, with this order:
* BOUNDZ1
* 0 ------- 3
* BOUNDX0 | | BOUNDX1
* 1 ------- 2
* BOUNDZ0
*/
// corner 0
points[0] = 0;
points[1] = 0;
// corner 1
points[2] = 0;
points[3] = -param.mesh.zlength;
// corner 2
points[4] = param.mesh.xlength;
points[5] = -param.mesh.zlength;
// corner 3
points[6] = param.mesh.xlength;
points[7] = 0;
for (int i=0; i<n_init_segments; ++i) {
// 0th node of the i-th segment
init_segments[2*i] = i;
// 1st node of the i-th segment
init_segments[2*i+1] = i+1;
}
// the 1st node of the last segment is connected back to the 0th node
init_segments[2*n_init_segments-1] = 0;
// boundary flags (see definition in constants.hpp)
init_segflags[0] = BOUNDX0;
init_segflags[1] = BOUNDZ0;
init_segflags[2] = BOUNDX1;
init_segflags[3] = BOUNDZ1;
elem_size = 1.5 * param.mesh.resolution * param.mesh.resolution;
// Need to modify when nregions > 1.
for (int i = 0; i < nregions; i++) {
regattr[i * attr_ndata] = 0.5*param.mesh.xlength;
regattr[i * attr_ndata + 1] = -0.5*param.mesh.zlength;
regattr[i * attr_ndata + 2] = 0;
regattr[i * attr_ndata + 3] = -1;
}
}
#else
{
/* Define 8 corner points of the box, with this order:
* 4 ------- 7
* / /|
* / / 6
* 0 ------- 3 /
* | |/
* 1 ------- 2
*
* Cut-out diagram with boundary flag:
* 4 ------- 7
* | BOUNDZ1 |
* 4 ------- 0 ------- 3 ------- 7 ------- 4
* | BOUNDX0 | BOUNDY0 | BOUNDX1 | BOUNDY1 |
* 5 ------- 1 ------- 2 ------- 6 ------- 5
* | BOUNDZ0 |
* 5 ------- 6
*/
// corner 0
points[0] = 0;
points[1] = 0;
points[2] = 0;
// corner 1
points[3] = 0;
points[4] = 0;
points[5] = -param.mesh.zlength;
// corner 2
points[6] = param.mesh.xlength;
points[7] = 0;
points[8] = -param.mesh.zlength;
// corner 3
points[9] = param.mesh.xlength;
points[10] = 0;
points[11] = 0;
// corner 4
points[12] = 0;
points[13] = param.mesh.ylength;
points[14] = 0;
// corner 5
points[15] = 0;
points[16] = param.mesh.ylength;
points[17] = -param.mesh.zlength;
// corner 6
points[18] = param.mesh.xlength;
points[19] = param.mesh.ylength;
points[20] = -param.mesh.zlength;
// corner 7
points[21] = param.mesh.xlength;
points[22] = param.mesh.ylength;
points[23] = 0;
// BOUNDX0
init_segments[0] = 0;
init_segments[1] = 1;
init_segments[2] = 5;
init_segments[3] = 4;
// BOUNDY0
init_segments[4] = 0;
init_segments[5] = 3;
init_segments[6] = 2;
init_segments[7] = 1;
// BOUNDZ0
init_segments[8] = 1;
init_segments[9] = 2;
init_segments[10] = 6;
init_segments[11] = 5;
// BOUNDX1
init_segments[12] = 3;
init_segments[13] = 7;
init_segments[14] = 6;
init_segments[15] = 2;
// BOUNDY1
init_segments[16] = 7;
init_segments[17] = 4;
init_segments[18] = 5;
init_segments[19] = 6;
// BOUNDZ1
init_segments[20] = 0;
init_segments[21] = 4;
init_segments[22] = 7;
init_segments[23] = 3;
// boundary flags (see definition in constants.hpp)
init_segflags[0] = BOUNDX0;
init_segflags[1] = BOUNDY0;
init_segflags[2] = BOUNDZ0;
init_segflags[3] = BOUNDX1;
init_segflags[4] = BOUNDY1;
init_segflags[5] = BOUNDZ1;
elem_size = 0.7 * param.mesh.resolution
* param.mesh.resolution * param.mesh.resolution;
// Need to modify when nregions > 1. Using .poly file might be better.
for (int i = 0; i < nregions; i++) {
regattr[i * attr_ndata] = 0.5*param.mesh.xlength;
regattr[i * attr_ndata + 1] = 0.5*param.mesh.ylength;
regattr[i * attr_ndata + 2] = -0.5*param.mesh.zlength;
regattr[i * attr_ndata + 3] = 0;
regattr[i * attr_ndata + 4] = -1;
}
}
#endif
points_to_mesh(param, var, npoints, points,
n_init_segments, init_segments, init_segflags, nregions, regattr,
elem_size, vertex_per_polygon);
delete [] points;
delete [] init_segments;
delete [] init_segflags;
delete [] regattr;
}
void new_mesh_refined_zone(const Param& param, Variables& var)
{
const Mesh& m = param.mesh;
// bounding box
const double Lx = m.xlength;
#ifdef THREED
const double Ly = m.ylength;
#endif
const double Lz = m.zlength;
// typical distance between nodes in the refined zone
const double d = m.resolution;
// adjust the bounds of the refined zone so that nodes are not on the boundary
const double x0 = std::max(m.refined_zonex.first, d / Lx);
const double x1 = std::min(m.refined_zonex.second, 1 - d / Lx);
#ifdef THREED
const double y0 = std::max(m.refined_zoney.first, d / Ly);
const double y1 = std::min(m.refined_zoney.second, 1 - d / Ly);
#endif
const double z0 = std::max(m.refined_zonez.first, d / Lz);
const double z1 = std::min(m.refined_zonez.second, 1 - d / Lz);
const int npoints = 2 * 4 * (NDIMS - 1);
double *points = new double[npoints*NDIMS];
const int nsegments = 2 * (2 * NDIMS); // 2 x 2D:4; 3D:6
const int nnodes = 2 * (NDIMS - 1); // 2D:2; 3D:4
const int vertex_per_polygon = 4;
int *segments = new int[nsegments*nnodes];
int *segflags = new int[nsegments];
#ifndef THREED
{
/* Define 4 corner points of the rectangle, with this order:
* BOUNDZ1
* 0 ------- 3
* BOUNDX0 | | BOUNDX1
* 1 ------- 2
* BOUNDZ0
*/
// outer box // inner box
// corner 0 // corner 0
points[0] = 0.0; points[8+0] = +x0*Lx;
points[1] = 0.0; points[8+1] = -z0*Lz;
// corner 1 // corner 1
points[2] = 0.0; points[8+2] = +x0*Lx;
points[3] = -Lz; points[8+3] = -z1*Lz;
// corner 2 // corner 2
points[4] = +Lx; points[8+4] = +x1*Lx;
points[5] = -Lz; points[8+5] = -z1*Lz;
// corner 3 // corner 3
points[6] = +Lx; points[8+6] = +x1*Lx;
points[7] = 0.0; points[8+7] = -z0*Lz;
// outer segments // inner segments
// BOUNDX0 // BOUNDX0
segments[0] = 0; segments[8+0] = 4+0;
segments[1] = 1; segments[8+1] = 4+1;
// BOUNDZ0 // BOUNDZ0
segments[2] = 1; segments[8+2] = 4+1;
segments[3] = 2; segments[8+3] = 4+2;
// BOUNDX1 // BOUNDX1
segments[4] = 2; segments[8+4] = 4+2;
segments[5] = 3; segments[8+5] = 4+3;
// BOUNDZ1 // BOUNDZ1
segments[6] = 3; segments[8+6] = 4+3;
segments[7] = 0; segments[8+7] = 4+0;
// outer boundary // inner boundary
segflags[0] = BOUNDX0; segflags[4+0] = 0;
segflags[1] = BOUNDZ0; segflags[4+1] = 0;
segflags[2] = BOUNDX1; segflags[4+2] = 0;
segflags[3] = BOUNDZ1; segflags[4+3] = 0;
}
#else
{
/* Define 8 corner points of the box, with this order:
* 4 ------- 7
* / /|
* / / 6
* 0 ------- 3 /
* | |/
* 1 ------- 2
*
* Cut-out diagram with boundary flag:
* 4 ------- 7
* | BOUNDZ1 |
* 4 ------- 0 ------- 3 ------- 7 ------- 4
* | BOUNDX0 | BOUNDY0 | BOUNDX1 | BOUNDY1 |
* 5 ------- 1 ------- 2 ------- 6 ------- 5
* | BOUNDZ0 |
* 5 ------- 6
*/
// outer box // inner box
// corner 0 // corner 0
points[ 0] = 0.0; points[24+ 0] = +x0*Lx;
points[ 1] = 0.0; points[24+ 1] = +y0*Ly;
points[ 2] = 0.0; points[24+ 2] = -z0*Lz;
// corner 1 // corner 1
points[ 3] = 0.0; points[24+ 3] = +x0*Lx;
points[ 4] = 0.0; points[24+ 4] = +y0*Ly;
points[ 5] = -Lz; points[24+ 5] = -z1*Lz;
// corner 2 // corner 2
points[ 6] = +Lx; points[24+ 6] = +x1*Lx;
points[ 7] = 0.0; points[24+ 7] = +y0*Ly;
points[ 8] = -Lz; points[24+ 8] = -z1*Lz;
// corner 3 // corner 3
points[ 9] = +Lx; points[24+ 9] = +x1*Lx;
points[10] = 0.0; points[24+10] = +y0*Ly;
points[11] = 0.0; points[24+11] = -z0*Lz;
// corner 4 // corner 4
points[12] = 0.0; points[24+12] = +x0*Lx;
points[13] = +Ly; points[24+13] = +y1*Ly;
points[14] = 0.0; points[24+14] = -z0*Lz;
// corner 5 // corner 5
points[15] = 0.0; points[24+15] = +x0*Lx;
points[16] = +Ly; points[24+16] = +y1*Ly;
points[17] = -Lz; points[24+17] = -z1*Lz;
// corner 6 // corner 6
points[18] = +Lx; points[24+18] = +x1*Lx;
points[19] = +Ly; points[24+19] = +y1*Ly;
points[20] = -Lz; points[24+20] = -z1*Lz;
// corner 7 // corner 7
points[21] = +Lx; points[24+21] = +x1*Lx;
points[22] = +Ly; points[24+22] = +y1*Ly;
points[23] = 0.0; points[24+23] = -z0*Lz;
// outer segments // inner segments
// BOUNDX0 // BOUNDX0
segments[ 0] = 0; segments[24+ 0] = 8+0;
segments[ 1] = 1; segments[24+ 1] = 8+1;
segments[ 2] = 5; segments[24+ 2] = 8+5;
segments[ 3] = 4; segments[24+ 3] = 8+4;
// BOUNDY0 // BOUNDY0
segments[ 4] = 0; segments[24+ 4] = 8+0;
segments[ 5] = 3; segments[24+ 5] = 8+3;
segments[ 6] = 2; segments[24+ 6] = 8+2;
segments[ 7] = 1; segments[24+ 7] = 8+1;
// BOUNDZ0 // BOUNDZ0
segments[ 8] = 1; segments[24+ 8] = 8+1;
segments[ 9] = 2; segments[24+ 9] = 8+2;
segments[10] = 6; segments[24+10] = 8+6;
segments[11] = 5; segments[24+11] = 8+5;
// BOUNDX1 // BOUNDX1
segments[12] = 3; segments[24+12] = 8+3;
segments[13] = 7; segments[24+13] = 8+7;
segments[14] = 6; segments[24+14] = 8+6;
segments[15] = 2; segments[24+15] = 8+2;
// BOUNDY1 // BOUNDY1
segments[16] = 7; segments[24+16] = 8+7;
segments[17] = 4; segments[24+17] = 8+4;
segments[18] = 5; segments[24+18] = 8+5;
segments[19] = 6; segments[24+19] = 8+6;
// BOUNDZ1 // BOUNDZ1
segments[20] = 0; segments[24+20] = 8+0;
segments[21] = 4; segments[24+21] = 8+4;
segments[22] = 7; segments[24+22] = 8+7;
segments[23] = 3; segments[24+23] = 8+3;
// outer boundary // inner boundary
segflags[0] = BOUNDX0; segflags[6+0] = 0;
segflags[1] = BOUNDY0; segflags[6+1] = 0;
segflags[2] = BOUNDZ0; segflags[6+2] = 0;
segflags[3] = BOUNDX1; segflags[6+3] = 0;
segflags[4] = BOUNDY1; segflags[6+4] = 0;
segflags[5] = BOUNDZ1; segflags[6+5] = 0;
}
#endif
const int nregions = 2;
const int ndata = NDIMS+2; // each region has (NDIMS+2) data fields: x, (y,) z, region marker (mat type), area/volume.
double *regattr = new double[nregions*ndata];
double *region0 = regattr;
double *region1 = regattr + ndata;
#ifndef THREED
const double area1 = 1.5 * (d*d);
#else
const double area1 = 0.7 * (d*d*d);
#endif
const double area0 = area1 * m.largest_size;
int pos = 0;
region0[pos] = +d/2; region1[pos++] = +x0*Lx+d/2; // x
#if THREED
region0[pos] = +d/2; region1[pos++] = +y0*Ly+d/2; // y
#endif
region0[pos] = -d/2; region1[pos++] = -z0*Lz-d/2; // z
region0[pos] = 0.0; region1[pos++] = 0.0; // attribute
region0[pos] = area0; region1[pos++] = area1; // area/volume
const double max_elem_size = 0;
points_to_mesh(param, var, npoints, points,
nsegments, segments, segflags, nregions, regattr,
max_elem_size, vertex_per_polygon);
delete [] points;
delete [] segments;
delete [] segflags;
delete [] regattr;
}
void my_fgets(char *buffer, std::size_t size, std::FILE *fp,
int &lineno, const std::string &filename)
{
char *s;
while (1) {
++ lineno;
s = std::fgets(buffer, size, fp);
if (! s) {
std::cerr << "Error: reading line " << lineno
<< " of '" << filename << "'\n";
std::exit(2);
}
if (std::strlen(buffer) == size-1 && buffer[size-2] != '\n') {
std::cerr << "Error: reading line " << lineno
<< " of '" << filename << "', line is too long.\n";
std::exit(2);
}
// check for blank lines and comments
if (buffer[0] != '\n' && buffer[0] != '#') break;
}
}
void new_mesh_from_polyfile(const Param& param, Variables& var)
{
/* The format specifiction for the poly file can be found in:
* 2D: http://www.cs.cmu.edu/~quake/triangle.poly.html
* 3D: http://wias-berlin.de/software/tetgen/fformats.poly.html
*
* Note:
* 1. the poly file in 3D has a more complicated format.
* 2. The last row in the regional attributes is the regional constraint
* on the maximum triangle area or tetrahedra volume, which is the max
* size of an element.
* when meshing_option=90, the unit is meter^dim.
* when meshing_option=91, the unit is mesh.resolution^dim.
*/
#ifdef THREED
const double std_elem_size = 0.7 * param.mesh.resolution
* param.mesh.resolution * param.mesh.resolution;
#else
const double std_elem_size = 1.5 * param.mesh.resolution * param.mesh.resolution;
#endif
std::FILE *fp = std::fopen(param.mesh.poly_filename.c_str(), "r");
if (! fp) {
std::cerr << "Error: Cannot open poly_filename '" << param.mesh.poly_filename << "'\n";
std::exit(2);
}
int lineno = 0;
int n;
char buffer[2550];
// get header of node list
int npoints;
{
my_fgets(buffer, 2550, fp, lineno, param.mesh.poly_filename);
int dim, nattr, nbdrym;
n = std::sscanf(buffer, "%d %d %d %d", &npoints, &dim, &nattr, &nbdrym);
if (n != 4) {
std::cerr << "Error: parsing line " << lineno << " of '"
<< param.mesh.poly_filename << "'\n";
std::exit(1);
}
if (dim != NDIMS ||
nattr != 0 ||
nbdrym != 0) {
std::cerr << "Error: unsupported value in line " << lineno
<< " of '" << param.mesh.poly_filename << "'\n";
std::exit(1);
}
}
// get node list
double *points = new double[npoints * NDIMS];
for (int i=0; i<npoints; i++) {
my_fgets(buffer, 2550, fp, lineno, param.mesh.poly_filename);
int k;
double *x = &points[i*NDIMS];
#ifdef THREED
n = std::sscanf(buffer, "%d %lf %lf %lf", &k, x, x+1, x+2);
#else
n = std::sscanf(buffer, "%d %lf %lf", &k, x, x+1);
#endif
if (n != NDIMS+1) {
std::cerr << "Error: parsing line " << lineno << " of '"
<< param.mesh.poly_filename << "'\n";
std::exit(1);
}
if (k != i) {
std::cerr << "Error: node number is continuous from 0 at line " << lineno << " of '"
<< param.mesh.poly_filename << "'\n";
std::exit(1);
}
}
// get header of segment (facet) list
int n_init_segments;
{
my_fgets(buffer, 2550, fp, lineno, param.mesh.poly_filename);
int has_bdryflag;
n = std::sscanf(buffer, "%d %d", &n_init_segments, &has_bdryflag);
if (n != 2) {
std::cerr << "Error: parsing line " << lineno << " of '"
<< param.mesh.poly_filename << "'\n";
std::exit(1);
}
if (has_bdryflag != 1) {
std::cerr << "Error: unsupported value in line " << lineno
<< " of '" << param.mesh.poly_filename << "'\n";
std::exit(1);
}
}
// get segment (facet) list
#ifdef THREED
auto facets = new tetgenio::facet[n_init_segments];
int *init_segflags = new int[n_init_segments];
for (int i=0; i<n_init_segments; i++) {
my_fgets(buffer, 2550, fp, lineno, param.mesh.poly_filename);
auto &f = facets[i];
int npolygons, nholes, bdryflag;
n = std::sscanf(buffer, "%d %d %d", &npolygons, &nholes, &bdryflag);
if (n != 3) {
std::cerr << "Error: parsing line " << lineno << " of '"
<< param.mesh.poly_filename << "'\n";
std::exit(1);
}
if (npolygons <= 0 ||
nholes != 0) {
std::cerr << "Error: unsupported value in line " << lineno
<< " of '" << param.mesh.poly_filename << "'\n";
std::exit(1);
}
if (bdryflag == 0) goto flag_ok;
for (int j=0; j<nbdrytypes; j++) {
if (bdryflag == 1 << j) goto flag_ok;
}
std::cerr << "Error: bdry_flag has multiple bits set in line " << lineno
<< " of '" << param.mesh.poly_filename << "'\n";
std::exit(1);
flag_ok:
init_segflags[i] = bdryflag;
f.polygonlist = new tetgenio::polygon[npolygons];
f.numberofpolygons = npolygons;
f.holelist = NULL;
f.numberofholes = 0;
for (int j=0; j<npolygons; j++) {
my_fgets(buffer, 4096, fp, lineno, param.mesh.poly_filename);
std::istringstream inbuf(std::string(buffer, 4096));
int nvertex;
inbuf >> nvertex;
if (nvertex < NODES_PER_FACET || nvertex > 9999) {
std::cerr << "Error: unsupported number of polygon points in line " << lineno
<< " of '" << param.mesh.poly_filename << "'\n";
std::exit(1);
}
f.polygonlist[j].vertexlist = new int[nvertex];
f.polygonlist[j].numberofvertices = nvertex;
for (int k=0; k<nvertex; k++) {
inbuf >> f.polygonlist[j].vertexlist[k];
if (f.polygonlist[j].vertexlist[k] < 0 ||
f.polygonlist[j].vertexlist[k] >= npoints) {
std::cerr << "Error: segment contains out-of-range node # [0-" << npoints
<<"] in line " << lineno << " of '"
<< param.mesh.poly_filename << "'\n";
std::exit(1);
}
}
}
}
#else
int *init_segments = new int[n_init_segments * NODES_PER_FACET];
int *init_segflags = new int[n_init_segments];
for (int i=0; i<n_init_segments; i++) {
my_fgets(buffer, 255, fp, lineno, param.mesh.poly_filename);
int *x = &init_segments[i*NODES_PER_FACET];
int junk, bdryflag;
n = std::sscanf(buffer, "%d %d %d %d", &junk, x, x+1, &bdryflag);
if (n != NODES_PER_FACET+2) {
std::cerr << "Error: parsing line " << lineno << " of '"
<< param.mesh.poly_filename << "'\n";
std::exit(1);
}
if (bdryflag == 0) goto flag_ok;
for (int j=0; j<nbdrytypes; j++) {
if (bdryflag == 1 << j) goto flag_ok;
}
std::cerr << "Error: bdry_flag has multiple bits set in line " << lineno
<< " of '" << param.mesh.poly_filename << "'\n";
std::exit(1);
flag_ok:
init_segflags[i] = bdryflag;
}
for (int i=0; i<n_init_segments; i++) {
int *x = &init_segments[i*NODES_PER_FACET];
for (int j=0; j<NODES_PER_FACET; j++) {
if (x[j] < 0 || x[j] >= npoints) {
std::cerr << "Error: segment contains out-of-range node # [0-" << npoints
<<"] in line " << lineno << " of '"
<< param.mesh.poly_filename << "'\n";
std::exit(1);
}
}
}
#endif
// get header of hole list
{
my_fgets(buffer, 255, fp, lineno, param.mesh.poly_filename);
int nholes;
n = std::sscanf(buffer, "%d", &nholes);
if (n != 1) {
std::cerr << "Error: parsing line " << lineno << " of '"
<< param.mesh.poly_filename << "'\n";
std::exit(1);
}
if (nholes != 0) {
std::cerr << "Error: unsupported value in line " << lineno
<< " of '" << param.mesh.poly_filename << "'\n";
std::exit(1);
}
}
// get header of region list
int nregions;
{
my_fgets(buffer, 255, fp, lineno, param.mesh.poly_filename);
n = std::sscanf(buffer, "%d", &nregions);
if (n != 1) {
std::cerr << "Error: parsing line " << lineno << " of '"
<< param.mesh.poly_filename << "'\n";
std::exit(1);
}
if (nregions <= 0) {
std::cerr << "Error: nregions <= 0, at line " << lineno << " of '"
<< param.mesh.poly_filename << "'\n";
std::exit(1);
}
}
// get region list
double *regattr = new double[nregions * (NDIMS+2)]; // each region has these data fields: x, (y,) z, region marker (mattype), and volume.
bool has_max_size = false;
for (int i=0; i<nregions; i++) {
my_fgets(buffer, 255, fp, lineno, param.mesh.poly_filename);
int junk;
double *x = ®attr[i*(NDIMS+2)];
#ifdef THREED
n = std::sscanf(buffer, "%d %lf %lf %lf %lf %lf", &junk, x, x+1, x+2, x+3, x+4);
#else
n = std::sscanf(buffer, "%d %lf %lf %lf %lf", &junk, x, x+1, x+2, x+3);
#endif
if (n != NDIMS+3) {
std::cerr << "Error: parsing line " << lineno << " of '"
<< param.mesh.poly_filename << "'. "<<NDIMS+3<<" values should be given but only "<<n<<" found.\n";
std::exit(1);
}
if ( x[NDIMS] < 0 || x[NDIMS] >= param.mat.nmat ) {
std::cerr << "Error: "<<NDIMS+2<<"-th value in line "<<lineno<<" should be >=0 and < "<<param.mat.nmat<<" (=mat.num_materials) but is "<<x[NDIMS]<<"\n";
std::cerr << "Note that this parameter is directly used as the index of mat. prop. arrays.\n";