forked from CEED/Laghos
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlaghost_tmop.cpp
1041 lines (955 loc) · 38.5 KB
/
laghost_tmop.cpp
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 <fstream>
#include <sys/time.h>
#include <sys/resource.h>
#include <cmath>
#include "laghost_tmop.hpp"
#include "mesh-optimizer.hpp"
#include "./common/mfem-common.hpp"
using std::cout;
using std::endl;
namespace mfem
{
void HR_adaptivity(ParMesh *pmesh, ParGridFunction &x_gf, const Array<int> &ess_tdofs, const int &myid, int &mesh_poly_deg, int &rs_levels, int &rp_levels, double &jitter, int &metric_id, int &target_id,\
double &lim_const, double &adapt_lim_const, int &quad_type, int &quad_order, int &solver_type, int &solver_iter, double &solver_rtol, \
int &solver_art_type, int &lin_solver, int &max_lin_iter, bool &move_bnd, int &combomet, bool &bal_expl_combo, bool &hradaptivity, int &h_metric_id, bool &normalization, int &verbosity_level, \
bool &fdscheme, int &adapt_eval, bool &exactaction, bool &pa, int &n_hr_iter, int &n_h_iter, int &mesh_node_ordering, int &barrier_type, \
int &worst_case_type)
{
const char *devopt = "cpu";
bool visualization = false;
int dim = pmesh->Dimension();
if(fdscheme){pa = false;}
// 4. Define a finite element space on the mesh. Here we use vector finite
// elements which are tensor products of quadratic finite elements. The
// number of components in the vector finite element space is specified by
// the last parameter of the FiniteElementSpace constructor.
FiniteElementCollection *fec;
if (mesh_poly_deg <= 0)
{
fec = new QuadraticPosFECollection;
mesh_poly_deg = 2;
}
else { fec = new H1_FECollection(mesh_poly_deg, dim); }
ParFiniteElementSpace *pfespace = new ParFiniteElementSpace(pmesh, fec, dim,
mesh_node_ordering);
// ParFiniteElementSpace *pfespace = new x_gf.FESpace();
// 5. Make the mesh curved based on the above finite element space. This
// means that we define the mesh elements through a fespace-based
// transformation of the reference element.
pmesh->SetNodalFESpace(pfespace);
// 7. Get the mesh nodes (vertices and other degrees of freedom in the finite
// element space) as a finite element grid function in fespace. Note that
// changing x automatically changes the shapes of the mesh elements.
ParGridFunction x(pfespace);
pmesh->SetNodalGridFunction(&x_gf);
// pmesh->SetNodalGridFunction(&x);
// 8. Define a vector representing the minimal local mesh size in the mesh
// nodes. We index the nodes using the scalar version of the degrees of
// freedom in pfespace. Note: this is partition-dependent.
//
// In addition, compute average mesh size and total volume.
Vector h0(pfespace->GetNDofs());
h0 = infinity();
double vol_loc = 0.0;
Array<int> dofs;
for (int i = 0; i < pmesh->GetNE(); i++)
{
// Get the local scalar element degrees of freedom in dofs.
pfespace->GetElementDofs(i, dofs);
// Adjust the value of h0 in dofs based on the local mesh size.
const double hi = pmesh->GetElementSize(i);
for (int j = 0; j < dofs.Size(); j++)
{
h0(dofs[j]) = min(h0(dofs[j]), hi);
}
vol_loc += pmesh->GetElementVolume(i);
}
double vol_glb;
MPI_Allreduce(&vol_loc, &vol_glb, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
const double small_phys_size = pow(vol_glb, 1.0 / dim) / 100.0;
// 9. Add a random perturbation to the nodes in the interior of the domain.
// We define a random grid function of fespace and make sure that it is
// zero on the boundary and its values are locally of the order of h0.
// The latter is based on the DofToVDof() method which maps the scalar to
// the vector degrees of freedom in pfespace.
ParGridFunction rdm(pfespace);
rdm.Randomize();
rdm -= 0.25; // Shift to random values in [-0.5,0.5].
rdm *= jitter;
rdm.HostReadWrite();
// Scale the random values to be of order of the local mesh size.
for (int i = 0; i < pfespace->GetNDofs(); i++)
{
for (int d = 0; d < dim; d++)
{
rdm(pfespace->DofToVDof(i,d)) *= h0(i);
}
}
Array<int> vdofs;
for (int i = 0; i < pfespace->GetNBE(); i++)
{
// Get the vector degrees of freedom in the boundary element.
pfespace->GetBdrElementVDofs(i, vdofs);
// Set the boundary values to zero.
for (int j = 0; j < vdofs.Size(); j++) { rdm(vdofs[j]) = 0.0; }
}
// x -= rdm;
// // Set the perturbation of all nodes from the true nodes.
// x.SetTrueVector();
// x.SetFromTrueVector();
x_gf -= rdm;
// Set the perturbation of all nodes from the true nodes.
x_gf.SetTrueVector();
x_gf.SetFromTrueVector();
// // 10. Save the starting (prior to the optimization) mesh to a file. This
// // output can be viewed later using GLVis: "glvis -m perturbed -np
// // num_mpi_tasks".
// {
// ostringstream mesh_name;
// mesh_name << "perturbed.mesh";
// ofstream mesh_ofs(mesh_name.str().c_str());
// mesh_ofs.precision(8);
// pmesh->PrintAsOne(mesh_ofs);
// }
// 11. Store the starting (prior to the optimization) positions.
ParGridFunction x0(pfespace);
// x0 = x;
x0 = x_gf;
// 12. Form the integrator that uses the chosen metric and target.
double min_detJ = -0.1;
TMOP_QualityMetric *metric = NULL;
switch (metric_id)
{
// T-metrics
case 1: metric = new TMOP_Metric_001; break;
case 2: metric = new TMOP_Metric_002; break;
case 4: metric = new TMOP_Metric_004; break;
case 7: metric = new TMOP_Metric_007; break;
case 9: metric = new TMOP_Metric_009; break;
case 14: metric = new TMOP_Metric_014; break;
case 22: metric = new TMOP_Metric_022(min_detJ); break;
case 50: metric = new TMOP_Metric_050; break;
case 55: metric = new TMOP_Metric_055; break;
case 56: metric = new TMOP_Metric_056; break;
case 58: metric = new TMOP_Metric_058; break;
case 66: metric = new TMOP_Metric_066(0.5); break;
case 77: metric = new TMOP_Metric_077; break;
case 80: metric = new TMOP_Metric_080(0.5); break;
case 85: metric = new TMOP_Metric_085; break;
case 90: metric = new TMOP_Metric_090; break;
case 94: metric = new TMOP_Metric_094; break;
case 98: metric = new TMOP_Metric_098; break;
// case 211: metric = new TMOP_Metric_211; break;
// case 252: metric = new TMOP_Metric_252(min_detJ); break;
case 301: metric = new TMOP_Metric_301; break;
case 302: metric = new TMOP_Metric_302; break;
case 303: metric = new TMOP_Metric_303; break;
case 304: metric = new TMOP_Metric_304; break;
// case 311: metric = new TMOP_Metric_311; break;
case 313: metric = new TMOP_Metric_313(min_detJ); break;
case 315: metric = new TMOP_Metric_315; break;
case 316: metric = new TMOP_Metric_316; break;
case 321: metric = new TMOP_Metric_321; break;
case 322: metric = new TMOP_Metric_322; break;
case 323: metric = new TMOP_Metric_323; break;
case 328: metric = new TMOP_Metric_328; break;
case 332: metric = new TMOP_Metric_332(0.5); break;
case 333: metric = new TMOP_Metric_333(0.5); break;
case 334: metric = new TMOP_Metric_334(0.5); break;
case 338: metric = new TMOP_Metric_338; break;
case 347: metric = new TMOP_Metric_347(0.5); break;
// case 352: metric = new TMOP_Metric_352(min_detJ); break;
case 360: metric = new TMOP_Metric_360; break;
// A-metrics
case 11: metric = new TMOP_AMetric_011; break;
case 36: metric = new TMOP_AMetric_036; break;
case 107: metric = new TMOP_AMetric_107a; break;
case 126: metric = new TMOP_AMetric_126(0.9); break;
default:
if (myid == 0) { cout << "Unknown metric_id: " << metric_id << endl; metric = new TMOP_Metric_001; break;}
}
TMOP_QualityMetric *h_metric = NULL;
if (hradaptivity)
{
switch (h_metric_id)
{
case 1: h_metric = new TMOP_Metric_001; break;
case 2: h_metric = new TMOP_Metric_002; break;
case 7: h_metric = new TMOP_Metric_007; break;
case 9: h_metric = new TMOP_Metric_009; break;
case 55: h_metric = new TMOP_Metric_055; break;
case 56: h_metric = new TMOP_Metric_056; break;
case 58: h_metric = new TMOP_Metric_058; break;
case 77: h_metric = new TMOP_Metric_077; break;
case 315: h_metric = new TMOP_Metric_315; break;
case 316: h_metric = new TMOP_Metric_316; break;
case 321: h_metric = new TMOP_Metric_321; break;
default:
if (myid == 0) { cout << "Metric_id not supported for h-adaptivity: " << h_metric_id << endl; h_metric = new TMOP_Metric_001; break;}
}
}
TMOP_WorstCaseUntangleOptimizer_Metric::BarrierType btype;
switch (barrier_type)
{
case 0: btype = TMOP_WorstCaseUntangleOptimizer_Metric::BarrierType::None;
break;
case 1: btype = TMOP_WorstCaseUntangleOptimizer_Metric::BarrierType::Shifted;
break;
case 2: btype = TMOP_WorstCaseUntangleOptimizer_Metric::BarrierType::Pseudo;
break;
default:
if (myid == 0) { cout << "barrier_type not supported: " << barrier_type << endl; btype = TMOP_WorstCaseUntangleOptimizer_Metric::BarrierType::None;}
}
TMOP_WorstCaseUntangleOptimizer_Metric::WorstCaseType wctype;
switch (worst_case_type)
{
case 0: wctype = TMOP_WorstCaseUntangleOptimizer_Metric::WorstCaseType::None;
break;
case 1: wctype = TMOP_WorstCaseUntangleOptimizer_Metric::WorstCaseType::Beta;
break;
case 2: wctype = TMOP_WorstCaseUntangleOptimizer_Metric::WorstCaseType::PMean;
break;
default:
if (myid == 0) {cout << "worst_case_type not supported: " << worst_case_type << endl; wctype = TMOP_WorstCaseUntangleOptimizer_Metric::WorstCaseType::None;}
}
TMOP_QualityMetric *untangler_metric = NULL;
if (barrier_type > 0 || worst_case_type > 0)
{
if (barrier_type > 0)
{
MFEM_VERIFY(metric_id == 4 || metric_id == 14 || metric_id == 66,
"Metric not supported for shifted/pseudo barriers.");
}
untangler_metric = new TMOP_WorstCaseUntangleOptimizer_Metric(*metric,
2,
1.5,
0.001,//0.01 for pseudo barrier
0.001,
btype,
wctype);
}
if (metric_id < 300 || h_metric_id < 300)
{
MFEM_VERIFY(dim == 2, "Incompatible metric for 3D meshes");
}
if (metric_id >= 300 || h_metric_id >= 300)
{
MFEM_VERIFY(dim == 3, "Incompatible metric for 2D meshes");
}
TargetConstructor::TargetType target_t;
TargetConstructor *target_c = NULL;
HessianCoefficient *adapt_coeff = NULL;
HRHessianCoefficient *hr_adapt_coeff = NULL;
H1_FECollection ind_fec(mesh_poly_deg, dim);
ParFiniteElementSpace ind_fes(pmesh, &ind_fec);
ParFiniteElementSpace ind_fesv(pmesh, &ind_fec, dim);
ParGridFunction size(&ind_fes), aspr(&ind_fes), ori(&ind_fes);
ParGridFunction aspr3d(&ind_fesv);
const AssemblyLevel al =
pa ? AssemblyLevel::PARTIAL : AssemblyLevel::LEGACY;
switch (target_id)
{
case 1: target_t = TargetConstructor::IDEAL_SHAPE_UNIT_SIZE; break;
case 2: target_t = TargetConstructor::IDEAL_SHAPE_EQUAL_SIZE; break;
case 3: target_t = TargetConstructor::IDEAL_SHAPE_GIVEN_SIZE; break;
case 4:
{
target_t = TargetConstructor::GIVEN_FULL;
AnalyticAdaptTC *tc = new AnalyticAdaptTC(target_t);
adapt_coeff = new HessianCoefficient(dim, metric_id);
tc->SetAnalyticTargetSpec(NULL, NULL, adapt_coeff);
target_c = tc;
break;
}
case 5: // Discrete size 2D or 3D
{
target_t = TargetConstructor::IDEAL_SHAPE_GIVEN_SIZE;
DiscreteAdaptTC *tc = new DiscreteAdaptTC(target_t);
if (adapt_eval == 0)
{
tc->SetAdaptivityEvaluator(new AdvectorCG(al));
}
else
{
#ifdef MFEM_USE_GSLIB
tc->SetAdaptivityEvaluator(new InterpolatorFP);
#else
MFEM_ABORT("MFEM is not built with GSLIB.");
#endif
}
ConstructSizeGF(size);
tc->SetParDiscreteTargetSize(size);
target_c = tc;
break;
}
case 6: // material indicator 2D
{
ParGridFunction d_x(&ind_fes), d_y(&ind_fes), disc(&ind_fes);
target_t = TargetConstructor::GIVEN_SHAPE_AND_SIZE;
DiscreteAdaptTC *tc = new DiscreteAdaptTC(target_t);
FunctionCoefficient mat_coeff(material_indicator_2d);
disc.ProjectCoefficient(mat_coeff);
if (adapt_eval == 0)
{
tc->SetAdaptivityEvaluator(new AdvectorCG(al));
}
else
{
#ifdef MFEM_USE_GSLIB
tc->SetAdaptivityEvaluator(new InterpolatorFP);
#else
MFEM_ABORT("MFEM is not built with GSLIB.");
#endif
}
// Diffuse the interface
DiffuseField(disc,2);
// Get partials with respect to x and y of the grid function
disc.GetDerivative(1,0,d_x);
disc.GetDerivative(1,1,d_y);
// Compute the squared magnitude of the gradient
for (int i = 0; i < size.Size(); i++)
{
size(i) = std::pow(d_x(i),2)+std::pow(d_y(i),2);
}
const double max = size.Max();
double max_all;
MPI_Allreduce(&max, &max_all, 1, MPI_DOUBLE, MPI_MAX, MPI_COMM_WORLD);
for (int i = 0; i < d_x.Size(); i++)
{
d_x(i) = std::abs(d_x(i));
d_y(i) = std::abs(d_y(i));
}
const double eps = 0.01;
const double aspr_ratio = 20.0;
const double size_ratio = 40.0;
for (int i = 0; i < size.Size(); i++)
{
size(i) = (size(i)/max_all);
aspr(i) = (d_x(i)+eps)/(d_y(i)+eps);
aspr(i) = 0.1 + 0.9*(1-size(i))*(1-size(i));
if (aspr(i) > aspr_ratio) {aspr(i) = aspr_ratio;}
if (aspr(i) < 1.0/aspr_ratio) {aspr(i) = 1.0/aspr_ratio;}
}
Vector vals;
const int NE = pmesh->GetNE();
double volume = 0.0, volume_ind = 0.0;
for (int i = 0; i < NE; i++)
{
ElementTransformation *Tr = pmesh->GetElementTransformation(i);
const IntegrationRule &ir =
IntRules.Get(pmesh->GetElementBaseGeometry(i), Tr->OrderJ());
size.GetValues(i, ir, vals);
for (int j = 0; j < ir.GetNPoints(); j++)
{
const IntegrationPoint &ip = ir.IntPoint(j);
Tr->SetIntPoint(&ip);
volume += ip.weight * Tr->Weight();
volume_ind += vals(j) * ip.weight * Tr->Weight();
}
}
double volume_all, volume_ind_all;
MPI_Allreduce(&volume, &volume_all, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD);
MPI_Allreduce(&volume_ind, &volume_ind_all, 1, MPI_DOUBLE, MPI_SUM,
MPI_COMM_WORLD);
const int NE_ALL = pmesh->GetGlobalNE();
const double avg_zone_size = volume_all / NE_ALL;
const double small_avg_ratio =
(volume_ind_all + (volume_all - volume_ind_all) / size_ratio)
/ volume_all;
const double small_zone_size = small_avg_ratio * avg_zone_size;
const double big_zone_size = size_ratio * small_zone_size;
for (int i = 0; i < size.Size(); i++)
{
const double val = size(i);
const double a = (big_zone_size - small_zone_size) / small_zone_size;
size(i) = big_zone_size / (1.0+a*val);
}
DiffuseField(size, 2);
DiffuseField(aspr, 2);
tc->SetParDiscreteTargetSize(size);
tc->SetParDiscreteTargetAspectRatio(aspr);
target_c = tc;
break;
}
case 7: // Discrete aspect ratio 3D
{
target_t = TargetConstructor::GIVEN_SHAPE_AND_SIZE;
DiscreteAdaptTC *tc = new DiscreteAdaptTC(target_t);
if (adapt_eval == 0)
{
tc->SetAdaptivityEvaluator(new AdvectorCG(al));
}
else
{
#ifdef MFEM_USE_GSLIB
tc->SetAdaptivityEvaluator(new InterpolatorFP);
#else
MFEM_ABORT("MFEM is not built with GSLIB.");
#endif
}
VectorFunctionCoefficient fd_aspr3d(dim, discrete_aspr_3d);
aspr3d.ProjectCoefficient(fd_aspr3d);
tc->SetParDiscreteTargetAspectRatio(aspr3d);
target_c = tc;
break;
}
case 8: // shape/size + orientation 2D
{
target_t = TargetConstructor::GIVEN_SHAPE_AND_SIZE;
DiscreteAdaptTC *tc = new DiscreteAdaptTC(target_t);
if (adapt_eval == 0)
{
tc->SetAdaptivityEvaluator(new AdvectorCG(al));
}
else
{
#ifdef MFEM_USE_GSLIB
tc->SetAdaptivityEvaluator(new InterpolatorFP);
#else
MFEM_ABORT("MFEM is not built with GSLIB.");
#endif
}
ConstantCoefficient size_coeff(0.1*0.1);
size.ProjectCoefficient(size_coeff);
tc->SetParDiscreteTargetSize(size);
FunctionCoefficient ori_coeff(discrete_ori_2d);
ori.ProjectCoefficient(ori_coeff);
tc->SetParDiscreteTargetOrientation(ori);
target_c = tc;
break;
}
// Targets used for hr-adaptivity tests.
case 9: // size target in an annular region.
case 10: // size+aspect-ratio in an annular region.
case 11: // size+aspect-ratio target for a rotate sine wave
{
target_t = TargetConstructor::GIVEN_FULL;
AnalyticAdaptTC *tc = new AnalyticAdaptTC(target_t);
hr_adapt_coeff = new HRHessianCoefficient(dim, target_id - 9);
tc->SetAnalyticTargetSpec(NULL, NULL, hr_adapt_coeff);
target_c = tc;
break;
}
default:
if (myid == 0) { cout << "Unknown target_id: " << target_id << endl; target_t = TargetConstructor::IDEAL_SHAPE_UNIT_SIZE; break;}
}
if (target_c == NULL)
{
target_c = new TargetConstructor(target_t, MPI_COMM_WORLD);
}
target_c->SetNodes(x0);
// Automatically balanced gamma in composite metrics.
auto metric_combo = dynamic_cast<TMOP_Combo_QualityMetric *>(metric);
if (metric_combo && bal_expl_combo)
{
Vector bal_weights;
// metric_combo->ComputeBalancedWeights(x, *target_c, bal_weights);
metric_combo->ComputeBalancedWeights(x_gf, *target_c, bal_weights);
metric_combo->SetWeights(bal_weights);
}
TMOP_QualityMetric *metric_to_use = barrier_type > 0 || worst_case_type > 0
? untangler_metric
: metric;
TMOP_Integrator *tmop_integ = new TMOP_Integrator(metric_to_use, target_c,
h_metric);
if (barrier_type > 0 || worst_case_type > 0)
{
tmop_integ->ComputeUntangleMetricQuantiles(x, *pfespace);
}
// Finite differences for computations of derivatives.
if (fdscheme)
{
MFEM_VERIFY(pa == false, "PA for finite differences is not implemented.");
tmop_integ->EnableFiniteDifferences(x);
tmop_integ->EnableFiniteDifferences(x_gf);
}
tmop_integ->SetExactActionFlag(exactaction);
// Setup the quadrature rules for the TMOP integrator.
IntegrationRules *irules = NULL;
switch (quad_type)
{
case 1: irules = &IntRulesLo; break;
case 2: irules = &IntRules; break;
case 3: irules = &IntRulesCU; break;
default:
if (myid == 0) { cout << "Unknown quad_type: " << quad_type << endl; irules = &IntRulesLo; break;}
}
tmop_integ->SetIntegrationRules(*irules, quad_order);
// if (myid == 0 && dim == 2)
// {
// cout << "Triangle quadrature points: "
// << irules->Get(Geometry::TRIANGLE, quad_order).GetNPoints()
// << "\nQuadrilateral quadrature points: "
// << irules->Get(Geometry::SQUARE, quad_order).GetNPoints() << endl;
// }
// if (myid == 0 && dim == 3)
// {
// cout << "Tetrahedron quadrature points: "
// << irules->Get(Geometry::TETRAHEDRON, quad_order).GetNPoints()
// << "\nHexahedron quadrature points: "
// << irules->Get(Geometry::CUBE, quad_order).GetNPoints()
// << "\nPrism quadrature points: "
// << irules->Get(Geometry::PRISM, quad_order).GetNPoints() << endl;
// }
// Limit the node movement.
// The limiting distances can be given by a general function of space.
ParFiniteElementSpace dist_pfespace(pmesh, fec); // scalar space
ParGridFunction dist(&dist_pfespace);
dist = 1.0;
// The small_phys_size is relevant only with proper normalization.
if (normalization) { dist = small_phys_size; }
ConstantCoefficient lim_coeff(lim_const);
if (lim_const != 0.0) { tmop_integ->EnableLimiting(x0, dist, lim_coeff); }
// Adaptive limiting.
ParGridFunction adapt_lim_gf0(&ind_fes);
ConstantCoefficient adapt_lim_coeff(adapt_lim_const);
AdaptivityEvaluator *adapt_lim_eval = NULL;
if (adapt_lim_const > 0.0)
{
MFEM_VERIFY(pa == false, "PA is not implemented for adaptive limiting");
FunctionCoefficient adapt_lim_gf0_coeff(adapt_lim_fun);
adapt_lim_gf0.ProjectCoefficient(adapt_lim_gf0_coeff);
if (adapt_eval == 0) { adapt_lim_eval = new AdvectorCG(al); }
else if (adapt_eval == 1)
{
#ifdef MFEM_USE_GSLIB
adapt_lim_eval = new InterpolatorFP;
#else
MFEM_ABORT("MFEM is not built with GSLIB support!");
#endif
}
else { MFEM_ABORT("Bad interpolation option."); }
tmop_integ->EnableAdaptiveLimiting(adapt_lim_gf0, adapt_lim_coeff,
*adapt_lim_eval);
// if (visualization)
// {
// socketstream vis1;
// common::VisualizeField(vis1, "localhost", 19916, adapt_lim_gf0, "Zeta 0",
// 300, 600, 300, 300);
// }
}
// Has to be after the enabling of the limiting / alignment, as it computes
// normalization factors for these terms as well.
if (normalization) { tmop_integ->ParEnableNormalization(x0); }
// 13. Setup the final NonlinearForm (which defines the integral of interest,
// its first and second derivatives). Here we can use a combination of
// metrics, i.e., optimize the sum of two integrals, where both are
// scaled by used-defined space-dependent weights. Note that there are
// no command-line options for the weights and the type of the second
// metric; one should update those in the code.
ParNonlinearForm a(pfespace);
if (pa) { a.SetAssemblyLevel(AssemblyLevel::PARTIAL); }
ConstantCoefficient *metric_coeff1 = NULL;
TMOP_QualityMetric *metric2 = NULL;
TargetConstructor *target_c2 = NULL;
FunctionCoefficient metric_coeff2(weight_fun);
// Explicit combination of metrics.
if (combomet > 0)
{
// First metric.
metric_coeff1 = new ConstantCoefficient(1.0);
tmop_integ->SetCoefficient(*metric_coeff1);
// Second metric.
if (dim == 2) { metric2 = new TMOP_Metric_077; }
else { metric2 = new TMOP_Metric_315; }
TMOP_Integrator *tmop_integ2 = NULL;
if (combomet == 1)
{
target_c2 = new TargetConstructor(
TargetConstructor::IDEAL_SHAPE_EQUAL_SIZE, MPI_COMM_WORLD);
target_c2->SetVolumeScale(0.01);
target_c2->SetNodes(x0);
tmop_integ2 = new TMOP_Integrator(metric2, target_c2, h_metric);
tmop_integ2->SetCoefficient(metric_coeff2);
}
else { tmop_integ2 = new TMOP_Integrator(metric2, target_c, h_metric); }
tmop_integ2->SetIntegrationRules(*irules, quad_order);
// if (fdscheme) { tmop_integ2->EnableFiniteDifferences(x); }
if (fdscheme) { tmop_integ2->EnableFiniteDifferences(x_gf); }
tmop_integ2->SetExactActionFlag(exactaction);
TMOPComboIntegrator *combo = new TMOPComboIntegrator;
combo->AddTMOPIntegrator(tmop_integ);
combo->AddTMOPIntegrator(tmop_integ2);
if (normalization) { combo->ParEnableNormalization(x0); }
if (lim_const != 0.0) { combo->EnableLimiting(x0, dist, lim_coeff); }
a.AddDomainIntegrator(combo);
}
else
{
a.AddDomainIntegrator(tmop_integ);
}
if (pa) { a.Setup(); }
// Compute the minimum det(J) of the starting mesh.
min_detJ = infinity();
const int NE = pmesh->GetNE();
for (int i = 0; i < NE; i++)
{
const IntegrationRule &ir =
irules->Get(pfespace->GetFE(i)->GetGeomType(), quad_order);
ElementTransformation *transf = pmesh->GetElementTransformation(i);
for (int j = 0; j < ir.GetNPoints(); j++)
{
transf->SetIntPoint(&ir.IntPoint(j));
min_detJ = min(min_detJ, transf->Jacobian().Det());
}
}
double minJ0;
MPI_Allreduce(&min_detJ, &minJ0, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD);
min_detJ = minJ0;
if (myid == 0)
{ cout << "Minimum det(J) of the original mesh is " << min_detJ << endl; }
// Force to run negative det J
// min_detJ = 1.0;
if (min_detJ < 0.0 && barrier_type == 0
&& metric_id != 22 && metric_id != 211 && metric_id != 252
&& metric_id != 311 && metric_id != 313 && metric_id != 352)
{
MFEM_ABORT("The input mesh is inverted! Try an untangling metric.");
}
if (min_detJ < 0.0)
{
MFEM_VERIFY(target_t == TargetConstructor::IDEAL_SHAPE_UNIT_SIZE,
"Untangling is supported only for ideal targets.");
const DenseMatrix &Wideal =
Geometries.GetGeomToPerfGeomJac(pfespace->GetFE(0)->GetGeomType());
min_detJ /= Wideal.Det();
double h0min = h0.Min(), h0min_all;
MPI_Allreduce(&h0min, &h0min_all, 1, MPI_DOUBLE, MPI_MIN, MPI_COMM_WORLD);
// Slightly below minJ0 to avoid div by 0.
min_detJ -= 0.01 * h0min_all;
}
// For HR tests, the energy is normalized by the number of elements.
// const double init_energy = a.GetParGridFunctionEnergy(x) /
const double init_energy = a.GetParGridFunctionEnergy(x_gf) /
(hradaptivity ? pmesh->GetGlobalNE() : 1);
double init_metric_energy = init_energy;
if (lim_const > 0.0 || adapt_lim_const > 0.0)
{
lim_coeff.constant = 0.0;
adapt_lim_coeff.constant = 0.0;
// init_metric_energy = a.GetParGridFunctionEnergy(x) /
init_metric_energy = a.GetParGridFunctionEnergy(x_gf) /
(hradaptivity ? pmesh->GetGlobalNE() : 1);
lim_coeff.constant = lim_const;
adapt_lim_coeff.constant = adapt_lim_const;
}
// Visualize the starting mesh and metric values.
// Note that for combinations of metrics, this only shows the first metric.
// if (visualization)
// {
// char title[] = "Initial metric values";
// vis_tmop_metric_p(mesh_poly_deg, *metric, *target_c, *pmesh, title, 0);
// }
// 14. Fix all boundary nodes, or fix only a given component depending on the
// boundary attributes of the given mesh. Attributes 1/2/3 correspond to
// fixed x/y/z components of the node. Attribute dim+1 corresponds to
// an entirely fixed node.
if (move_bnd == false)
{
Array<int> ess_bdr(pmesh->bdr_attributes.Max());
ess_bdr = 1;
a.SetEssentialBC(ess_bdr);
}
else
{
int n = 0;
for (int i = 0; i < pmesh->GetNBE(); i++)
{
const int nd = pfespace->GetBE(i)->GetDof();
const int attr = pmesh->GetBdrElement(i)->GetAttribute();
// MFEM_VERIFY(!(dim == 2 && attr == 3),
// "Boundary attribute 3 must be used only for 3D meshes. "
// "Adjust the attributes (1/2/3/4 for fixed x/y/z/all "
// "components, rest for free nodes), or use -fix-bnd.");
if (attr == 1 || attr == 2) { n += 1*nd; } // x
if (attr == 3 || attr == 4) { n += 1*nd; } // x and y
// if (attr == 1 || attr == 2) { n += 2*nd; } // x and y
// if (attr == 3 || attr == 4) { n += 1*nd; } // x and y
// if (attr == 1 || attr == 2 || attr == 3 || attr == 4 ) { n += nd; }
// if (attr == 4) { n += nd * dim; }
// std::cout << attr <<std::endl;
}
Array<int> ess_vdofs(n);
n = 0;
for (int i = 0; i < pmesh->GetNBE(); i++)
{
const int nd = pfespace->GetBE(i)->GetDof();
const int attr = pmesh->GetBdrElement(i)->GetAttribute();
pfespace->GetBdrElementVDofs(i, vdofs);
if (attr == 1) // Fix x components.
{
for (int j = 0; j < nd; j++)
{ ess_vdofs[n++] = vdofs[j];}
}
else if (attr == 2) // Fix x components.
{
for (int j = 0; j < nd; j++)
{ ess_vdofs[n++] = vdofs[j];}
}
else if (attr == 3) // Fix x and y components.
{
for (int j = 0; j < 1*nd; j++)
{ ess_vdofs[n++] = vdofs[j+nd]; }
}
else if (attr == 4) // Fix x and y components.
{
for (int j = 0; j < 1*nd; j++)
{ ess_vdofs[n++] = vdofs[j+nd]; }
}
// if (attr == 1) // Fix x and y components.
// {
// for (int j = 0; j < 2*nd; j++)
// { ess_vdofs[n++] = vdofs[j];}
// }
// else if (attr == 2) // Fix x and y components.
// {
// for (int j = 0; j < 2*nd; j++)
// { ess_vdofs[n++] = vdofs[j];}
// }
// else if (attr == 3) // Fix y components.
// {
// for (int j = 0; j < nd; j++)
// { ess_vdofs[n++] = vdofs[j+nd]; }
// }
// else if (attr == 4) // Fix y components.
// {
// for (int j = 0; j < nd; j++)
// { ess_vdofs[n++] = vdofs[j+nd]; }
// }
// else if (attr == 3) // Fix y components.
// {
// for (int j = 0; j < nd; j++)
// { ess_vdofs[n++] = vdofs[j+nd]; }
// }
// else if (attr == 4) // Fix y components.
// {
// for (int j = 0; j < nd; j++)
// { ess_vdofs[n++] = vdofs[j+nd]; }
// }
// else if (attr == 4) // Fix y components.
// {
// for (int j = 0; j < nd; j++)
// { ess_vdofs[n++] = vdofs[j+nd]; }
// }
// else if (attr == 3) // Fix z components.
// {
// for (int j = 0; j < nd; j++)
// { ess_vdofs[n++] = vdofs[j+2*nd]; }
// }
// else if (attr == 4) // Fix all components.
// {
// for (int j = 0; j < vdofs.Size(); j++)
// { ess_vdofs[n++] = vdofs[j]; }
// }
}
a.SetEssentialVDofs(ess_vdofs);
}
// Array<int> ess_tdofs;
// {
// Array<int> ess_bdr(pmesh->bdr_attributes.Max()), dofs_list;
// if(dim == 2)
// {
// ess_bdr = 0; ess_bdr[0] = 1; ess_bdr[1] = 1;
// // pfespace->GetEssentialTrueDofs(ess_bdr, dofs_list,0);
// pfespace->GetEssentialTrueDofs(ess_bdr, dofs_list);
// ess_tdofs.Append(dofs_list);
// ess_bdr = 0; ess_bdr[2] = 1; ess_bdr[3] = 1;
// // x_gf.FESpace()->GetEssentialTrueDofs(ess_bdr, dofs_list,1);
// pfespace->GetEssentialTrueDofs(ess_bdr, dofs_list);
// ess_tdofs.Append(dofs_list);
// }
// else
// {
// ess_bdr = 0; ess_bdr[0] = 1; ess_bdr[1] = 1;
// pfespace->GetEssentialTrueDofs(ess_bdr, dofs_list,0);
// ess_tdofs.Append(dofs_list);
// ess_bdr = 0; ess_bdr[2] = 1; ess_bdr[3] = 1;
// pfespace->GetEssentialTrueDofs(ess_bdr, dofs_list,1);
// ess_tdofs.Append(dofs_list);
// ess_bdr = 0; ess_bdr[4] = 1; ess_bdr[5] = 1;
// pfespace->GetEssentialTrueDofs(ess_bdr, dofs_list,2);
// ess_tdofs.Append(dofs_list);
// }
// }
// a.SetEssentialVDofs(ess_tdofs);
// As we use the inexact Newton method to solve the resulting nonlinear
// system, here we setup the linear solver for the system's Jacobian.
Solver *S = NULL, *S_prec = NULL;
const double linsol_rtol = 1e-12;
// Level of output.
IterativeSolver::PrintLevel linsolver_print;
if (verbosity_level == 2)
{ linsolver_print.Errors().Warnings().FirstAndLast(); }
if (verbosity_level > 2)
{ linsolver_print.Errors().Warnings().Iterations(); }
if (lin_solver == 0)
{
S = new DSmoother(1, 1.0, max_lin_iter);
}
else if (lin_solver == 1)
{
CGSolver *cg = new CGSolver(MPI_COMM_WORLD);
cg->SetMaxIter(max_lin_iter);
cg->SetRelTol(linsol_rtol);
cg->SetAbsTol(0.0);
cg->SetPrintLevel(linsolver_print);
S = cg;
}
else
{
MINRESSolver *minres = new MINRESSolver(MPI_COMM_WORLD);
minres->SetMaxIter(max_lin_iter);
minres->SetRelTol(linsol_rtol);
minres->SetAbsTol(0.0);
minres->SetPrintLevel(linsolver_print);
if (lin_solver == 3 || lin_solver == 4)
{
if (pa)
{
MFEM_VERIFY(lin_solver != 4, "PA l1-Jacobi is not implemented");
auto js = new OperatorJacobiSmoother;
js->SetPositiveDiagonal(true);
S_prec = js;
}
else
{
auto hs = new HypreSmoother;
hs->SetType((lin_solver == 3) ? HypreSmoother::Jacobi
/* */ : HypreSmoother::l1Jacobi, 1);
hs->SetPositiveDiagonal(true);
S_prec = hs;
}
minres->SetPreconditioner(*S_prec);
}
S = minres;
}
//
// Perform the nonlinear optimization.
//
const IntegrationRule &ir =
irules->Get(pfespace->GetFE(0)->GetGeomType(), quad_order);
TMOPNewtonSolver solver(pfespace->GetComm(), ir, solver_type);
// Provide all integration rules in case of a mixed mesh.
solver.SetIntegrationRules(*irules, quad_order);
// Specify linear solver when we use a Newton-based solver.
if (solver_type == 0) { solver.SetPreconditioner(*S); }
// For untangling, the solver will update the min det(T) values.
solver.SetMinDetPtr(&min_detJ);
solver.SetMaxIter(solver_iter);
solver.SetRelTol(solver_rtol);
solver.SetAbsTol(0.0);
if (solver_art_type > 0)
{
solver.SetAdaptiveLinRtol(solver_art_type, 0.5, 0.9);
}
// Level of output.
IterativeSolver::PrintLevel newton_print;
if (verbosity_level > 0)
{ newton_print.Errors().Warnings().Iterations(); }
solver.SetPrintLevel(newton_print);
// hr-adaptivity solver.
// If hr-adaptivity is disabled, r-adaptivity is done once using the
// TMOPNewtonSolver.
// Otherwise, "hr_iter" iterations of r-adaptivity are done followed by
// "h_per_r_iter" iterations of h-adaptivity after each r-adaptivity.
// The solver terminates if an h-adaptivity iteration does not modify
// any element in the mesh.
TMOPHRSolver hr_solver(*pmesh, a, solver,
x_gf, move_bnd, hradaptivity,
mesh_poly_deg, h_metric_id,
n_hr_iter, n_h_iter);
// TMOPHRSolver hr_solver(*pmesh, a, solver,
// x, move_bnd, hradaptivity,
// mesh_poly_deg, h_metric_id,
// n_hr_iter, n_h_iter);
hr_solver.AddGridFunctionForUpdate(&x0);
if (adapt_lim_const > 0.)
{
hr_solver.AddGridFunctionForUpdate(&adapt_lim_gf0);
hr_solver.AddFESpaceForUpdate(&ind_fes);
}
hr_solver.Mult();
// // 16. Save the optimized mesh to a file. This output can be viewed later
// // using GLVis: "glvis -m optimized -np num_mpi_tasks".
// {
// ostringstream mesh_name;
// mesh_name << "optimized.mesh";
// ofstream mesh_ofs(mesh_name.str().c_str());
// mesh_ofs.precision(8);
// pmesh->PrintAsOne(mesh_ofs);
// }
// Report the final energy of the functional.
// const double fin_energy = a.GetParGridFunctionEnergy(x) /
const double fin_energy = a.GetParGridFunctionEnergy(x_gf) /
(hradaptivity ? pmesh->GetGlobalNE() : 1);
double fin_metric_energy = fin_energy;
if (lim_const > 0.0 || adapt_lim_const > 0.0)
{
lim_coeff.constant = 0.0;
adapt_lim_coeff.constant = 0.0;
fin_metric_energy = a.GetParGridFunctionEnergy(x) /
(hradaptivity ? pmesh->GetGlobalNE() : 1);
lim_coeff.constant = lim_const;
adapt_lim_coeff.constant = adapt_lim_const;
}
if (myid == 0)
{
std::cout << std::scientific << std::setprecision(4);
// cout << "Initial strain energy: " << init_energy
// << " = metrics: " << init_metric_energy
// << " + extra terms: " << init_energy - init_metric_energy << endl;
// cout << " Final strain energy: " << fin_energy
// << " = metrics: " << fin_metric_energy
// << " + extra terms: " << fin_energy - fin_metric_energy << endl;
cout << "The strain energy decreased by: "
<< (init_energy - fin_energy) * 100.0 / init_energy << " %." << endl;
}
// Visualize the final mesh and metric values.
// if (visualization)
// {
// char title[] = "Final metric values";
// vis_tmop_metric_p(mesh_poly_deg, *metric, *target_c, *pmesh, title, 600);
// }
// if (adapt_lim_const > 0.0 && visualization)
// {
// socketstream vis0;
// common::VisualizeField(vis0, "localhost", 19916, adapt_lim_gf0, "Xi 0",
// 600, 600, 300, 300);