forked from UBCHREST/ablate
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlevelSetUtilities.cpp
More file actions
1917 lines (1388 loc) · 70.9 KB
/
levelSetUtilities.cpp
File metadata and controls
1917 lines (1388 loc) · 70.9 KB
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 "levelSetUtilities.hpp"
#include <petsc.h>
#include <memory>
#include "LS-VOF.hpp"
#include "cellGrad.hpp"
#include "geometry.hpp"
#include "domain/range.hpp"
#include "domain/reverseRange.hpp"
#include "mathFunctions/functionWrapper.hpp"
#include "utilities/constants.hpp"
#include "utilities/mathUtilities.hpp"
#include "utilities/mpiUtilities.hpp"
#include "utilities/petscSupport.hpp"
#include "utilities/petscUtilities.hpp"
void ablate::levelSet::Utilities::CellValGrad(DM dm, const PetscInt p, PetscReal *c, PetscReal *c0, PetscReal *g) {
DMPolytopeType ct;
PetscInt Nc;
PetscReal *coords = NULL;
const PetscScalar *array;
PetscBool isDG;
PetscReal x0[3];
// Coordinates of the cell vertices
DMPlexGetCellCoordinates(dm, p, &isDG, &Nc, &array, &coords) >> ablate::utilities::PetscUtilities::checkError;
// Center of the cell
DMPlexComputeCellGeometryFVM(dm, p, NULL, x0, NULL) >> ablate::utilities::PetscUtilities::checkError;
// Get the cell type and call appropriate VOF function
DMPlexGetCellType(dm, p, &ct) >> ablate::utilities::PetscUtilities::checkError;
switch (ct) {
case DM_POLYTOPE_SEGMENT:
Grad_1D(x0, coords, c, c0, g) >> ablate::utilities::PetscUtilities::checkError;
break;
case DM_POLYTOPE_TRIANGLE:
Grad_2D_Tri(x0, coords, c, c0, g) >> ablate::utilities::PetscUtilities::checkError;
break;
case DM_POLYTOPE_QUADRILATERAL:
Grad_2D_Quad(x0, coords, c, c0, g) >> ablate::utilities::PetscUtilities::checkError;
break;
case DM_POLYTOPE_TETRAHEDRON:
Grad_3D_Tetra(x0, coords, c, c0, g) >> ablate::utilities::PetscUtilities::checkError;
break;
case DM_POLYTOPE_HEXAHEDRON:
Grad_3D_Hex(x0, coords, c, c0, g) >> ablate::utilities::PetscUtilities::checkError;
break;
default:
throw std::invalid_argument("No element geometry for cell " + std::to_string(p) + " with type " + DMPolytopeTypes[ct]);
}
DMPlexRestoreCellCoordinates(dm, p, &isDG, &Nc, &array, &coords) >> ablate::utilities::PetscUtilities::checkError;
}
void ablate::levelSet::Utilities::CellValGrad(DM dm, const PetscInt fid, const PetscInt p, Vec f, PetscReal *c0, PetscReal *g) {
PetscInt nv, *verts;
const PetscScalar *fvals, *v;
PetscScalar *c;
DMPlexCellGetVertices(dm, p, &nv, &verts) >> ablate::utilities::PetscUtilities::checkError;
DMGetWorkArray(dm, nv, MPIU_SCALAR, &c) >> ablate::utilities::PetscUtilities::checkError;
VecGetArrayRead(f, &fvals) >> utilities::PetscUtilities::checkError;
for (PetscInt i = 0; i < nv; ++i) {
// DMPlexPointLocalFieldRead isn't behaving like I would expect. If I don't make f a pointer then it just returns zero.
// Additionally, it looks like it allows for the editing of the value.
if (fid >= 0) {
DMPlexPointLocalFieldRead(dm, verts[i], fid, fvals, &v) >> utilities::PetscUtilities::checkError;
} else {
DMPlexPointLocalRead(dm, verts[i], fvals, &v) >> utilities::PetscUtilities::checkError;
}
c[i] = *v;
}
DMPlexCellRestoreVertices(dm, p, &nv, &verts) >> ablate::utilities::PetscUtilities::checkError;
ablate::levelSet::Utilities::CellValGrad(dm, p, c, c0, g);
DMRestoreWorkArray(dm, nv, MPIU_SCALAR, &c) >> ablate::utilities::PetscUtilities::checkError;
}
void ablate::levelSet::Utilities::CellValGrad(std::shared_ptr<ablate::domain::SubDomain> subDomain, const ablate::domain::Field *field, const PetscInt p, PetscReal *c0, PetscReal *g) {
DM dm = subDomain->GetFieldDM(*field);
Vec f = subDomain->GetVec(*field);
ablate::levelSet::Utilities::CellValGrad(dm, field->id, p, f, c0, g);
}
void ablate::levelSet::Utilities::VertexToVertexGrad(std::shared_ptr<ablate::domain::SubDomain> subDomain, const ablate::domain::Field *field, const PetscInt p, PetscReal *g) {
// Given a field determine the gradient at a vertex
DM dm = subDomain->GetFieldDM(*field);
Vec vec = subDomain->GetVec(*field);
DMPlexVertexGradFromVertex(dm, p, vec, field->id, 0, g) >> ablate::utilities::PetscUtilities::checkError;
}
void DMPlexVertexDivFromCellUpwind(DM dm, const PetscInt v, Vec data, const PetscInt fID, const PetscReal s, const PetscReal g[], PetscReal *div) {
const PetscScalar *dataArray;
PetscInt cStart, cEnd;
PetscInt dim;
PetscInt nStar, *star = NULL;
DMGetDimension(dm, &dim) >> ablate::utilities::PetscUtilities::checkError;
PetscReal x0[dim];
DMPlexComputeCellGeometryFVM(dm, v, NULL, x0, NULL) >> ablate::utilities::PetscUtilities::checkError;
DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd) >> ablate::utilities::PetscUtilities::checkError;
VecGetArrayRead(data, &dataArray) >> ablate::utilities::PetscUtilities::checkError;
*div = 0.0;
PetscReal totalVol = 0.0;
// Everything using this vertex
DMPlexGetTransitiveClosure(dm, v, PETSC_FALSE, &nStar, &star) >> ablate::utilities::PetscUtilities::checkError;
for (PetscInt st = 0; st < nStar * 2; st += 2) {
if (star[st] >= cStart && star[st] < cEnd) { // It's a cell
PetscReal x[dim];
DMPlexComputeCellGeometryFVM(dm, star[st], NULL, x, NULL) >> ablate::utilities::PetscUtilities::checkError;
PetscReal dot = 0.0;
for (PetscInt d = 0; d < dim; ++d) {
dot += g[d]*(x0[d] - x[d]);
}
if (s*dot>=0.0) {
// Surface area normal
PetscScalar N[3];
DMPlexCornerSurfaceAreaNormal(dm, v, star[st], N) >> ablate::utilities::PetscUtilities::checkError;
const PetscScalar *val;
xDMPlexPointLocalRead(dm, star[st], fID, dataArray, &val) >> ablate::utilities::PetscUtilities::checkError;
for (PetscInt d = 0; d < dim; ++d) *div += val[d]*N[d];
totalVol += ablate::utilities::MathUtilities::MagVector(dim, N);
}
}
}
DMPlexRestoreTransitiveClosure(dm, v, PETSC_FALSE, &nStar, &star) >> ablate::utilities::PetscUtilities::checkError;
VecRestoreArrayRead(data, &dataArray) >> ablate::utilities::PetscUtilities::checkError;
if (totalVol > 0.0) *div /= totalVol;
}
/**
* Compute the upwind derivative
* @param dm - Domain of the gradient data.
* @param gradArray - Array containing the cell-centered gradient
* @param v - Vertex id
* @param direction - The direction to be considered upwind. +1 for standard upwind, -1 of downwind
* @param g - On input the gradient of the level-set field at a vertex. On output the upwind gradient at v
*/
static void VertexUpwindGrad(DM dm, PetscScalar *gradArray, const PetscInt gradID, const PetscInt v, const PetscReal direction, PetscReal *g) {
// The upwind direction is determined using the dot product between the vector u and the vector connecting the cell-center
// and the vertex
PetscInt dim;
PetscReal weightTotal = 0.0;
PetscScalar x0[3], n[3];
DMGetDimension(dm, &dim) >> ablate::utilities::PetscUtilities::checkError;
ablate::utilities::MathUtilities::NormVector(dim, g, n);
DMPlexComputeCellGeometryFVM(dm, v, NULL, x0, NULL) >> ablate::utilities::PetscUtilities::checkError;
//if(fabs(x0[0]-0.3125)<0.001 && fabs(x0[1]-0.0625)<0.00){
// printf("%ld\n", v);
// exit(0);
//}
for (PetscInt d = 0; d < dim; ++d) {
g[d] = 0.0;
}
// Obtain all cells which use this vertex
PetscInt nCells, *cells;
DMPlexVertexGetCells(dm, v, &nCells, &cells) >> ablate::utilities::PetscUtilities::checkError;
for (PetscInt c = 0; c < nCells; ++c) {
PetscReal x[3];
DMPlexComputeCellGeometryFVM(dm, cells[c], NULL, x, NULL) >> ablate::utilities::PetscUtilities::checkError;
ablate::utilities::MathUtilities::Subtract(dim, x0, x, x);
ablate::utilities::MathUtilities::NormVector(dim, x, x);
PetscReal dot = ablate::utilities::MathUtilities::DotVector(dim, n, x);
dot *= direction;
if (dot>=0.0) {
weightTotal += dot;
const PetscScalar *cellGrad = nullptr;
xDMPlexPointLocalRead(dm, cells[c], gradID, gradArray, &cellGrad) >> ablate::utilities::PetscUtilities::checkError;
// Weighted average of the surrounding cell-center gradients.
// Note that technically this is (in 2D) the area of the quadrilateral that is formed by connecting
// the vertex, center of the neighboring edges, and the center of the triangle. As the three quadrilaterals
// that are formed this way all have the same area, there is no need to take into account the 1/3. Something
// similar should hold in 3D and for other cell types that ABLATE uses.
for (PetscInt d = 0; d < dim; ++d) {
g[d] += dot*cellGrad[d];
}
}
}
//exit(0);
DMPlexVertexRestoreCells(dm, v, &nCells, &cells) >> ablate::utilities::PetscUtilities::checkError;
// Size of the communicator
MPI_Comm comm = PetscObjectComm((PetscObject)dm);
int size;
MPI_Comm_size(comm, &size) >> ablate::utilities::MpiUtilities::checkError;
// Error checking
if ( PetscAbs(weightTotal) < ablate::utilities::Constants::small ) {
// When running on a single processor all vertices should have an upwind cell. Throw an error if that's not the case.
// When running in parallel, ghost vertices at the edge of the local domain may not have any surrounding upwind cells, so
// ignore the error and simply set the upwind gradient to zero.
// if ( size==1 ) {
// throw std::runtime_error("ablate::levelSet::Utilities::VertexUpwindGrad encounted a situation where there are no upwind cells");
// }
// if ( size==1 ) {
// char err[255];
// sprintf(err, "ablate::levelSet::Utilities::VertexUpwindGrad encounted a situation where there are no upwind cells %f,%f", x0[0], x0[1]);
// throw std::runtime_error(err);
// }
for (PetscInt d = 0; d < dim; ++d) {
g[d] = 0.0;
}
}
else {
for (PetscInt d = 0; d < dim; ++d) {
g[d] /= weightTotal;
}
}
}
// Given a level set and normal at the cell center compute the level set values at the vertices assuming a straight interface
void ablate::levelSet::Utilities::VertexLevelSet_LS(DM dm, const PetscInt p, const PetscReal c0, const PetscReal *n, PetscReal **c) {
PetscInt dim, Nc, nVerts, i, j;
PetscReal x0[3] = {0.0, 0.0, 0.0};
PetscReal *coords = NULL;
const PetscScalar *array;
PetscBool isDG;
DMGetDimension(dm, &dim) >> ablate::utilities::PetscUtilities::checkError;
// The cell center
DMPlexComputeCellGeometryFVM(dm, p, NULL, x0, NULL) >> ablate::utilities::PetscUtilities::checkError;
// Coordinates of the cell vertices
DMPlexGetCellCoordinates(dm, p, &isDG, &Nc, &array, &coords) >> ablate::utilities::PetscUtilities::checkError;
// Number of vertices
nVerts = Nc / dim;
if (*c == NULL) {
PetscMalloc1(nVerts, c) >> ablate::utilities::PetscUtilities::checkError;
}
// The level set value of each vertex. This assumes that the interface is a line/plane
// with the given unit normal.
for (i = 0; i < nVerts; ++i) {
(*c)[i] = c0;
for (j = 0; j < dim; ++j) {
(*c)[i] += n[j] * (coords[i * dim + j] - x0[j]);
}
}
DMPlexRestoreCellCoordinates(dm, p, &isDG, &Nc, &array, &coords) >> ablate::utilities::PetscUtilities::checkError;
}
// Given a cell VOF and normal at the cell center compute the level set values at the vertices assuming a straight interface
void ablate::levelSet::Utilities::VertexLevelSet_VOF(DM dm, const PetscInt p, const PetscReal targetVOF, const PetscReal *n, PetscReal **c) {
PetscReal vof; // current VOF of the cell
PetscReal area; // length (2D) or area (3D) of the cell face
PetscReal cellVolume; // Area (2D) or volume (3D) of the cell
const PetscReal tol = 1e-8;
PetscInt i;
PetscReal offset;
PetscReal vofError;
PetscInt nv;
// Get the number of vertices for the cell
DMPlexCellGetNumVertices(dm, p, &nv) >> ablate::utilities::PetscUtilities::checkError;
// Get an initial guess at the vertex level set values assuming that the interface passes through the cell-center.
// Also allocates c if c==NULL on entry
ablate::levelSet::Utilities::VertexLevelSet_LS(dm, p, 0.0, n, c);
// Get the resulting VOF from the initial guess
ablate::levelSet::Utilities::VOF(dm, p, *c, &vof, &area, &cellVolume);
vofError = targetVOF - vof;
while (fabs(vofError) > tol) {
// The amount the center level set value needs to shift by.
offset = vofError * cellVolume / area;
// If this isn't damped then it will overshoot and there will be no interface in the cell
offset *= 0.5;
for (i = 0; i < nv; ++i) {
(*c)[i] -= offset;
}
ablate::levelSet::Utilities::VOF(dm, p, *c, &vof, &area, NULL);
vofError = targetVOF - vof;
};
}
// Returns the VOF for a given cell using the level-set values at the cell vertices.
// Refer to "Quadrature rules for triangular and tetrahedral elements with generalized functions"
// by Holdych, Noble, and Secor, Int. J. Numer. Meth. Engng 2008; 73:1310-1327.
void ablate::levelSet::Utilities::VOF(DM dm, const PetscInt p, PetscReal *c, PetscReal *vof, PetscReal *area, PetscReal *vol) {
DMPolytopeType ct;
PetscInt Nc;
PetscReal *coords = NULL;
const PetscScalar *array;
PetscBool isDG;
// Coordinates of the cell vertices
DMPlexGetCellCoordinates(dm, p, &isDG, &Nc, &array, &coords) >> ablate::utilities::PetscUtilities::checkError;
// Get the cell type and call appropriate VOF function
DMPlexGetCellType(dm, p, &ct) >> ablate::utilities::PetscUtilities::checkError;
switch (ct) {
case DM_POLYTOPE_SEGMENT:
VOF_1D(coords, c, vof, area, vol);
break;
case DM_POLYTOPE_TRIANGLE:
VOF_2D_Tri(coords, c, vof, area, vol);
break;
case DM_POLYTOPE_QUADRILATERAL:
VOF_2D_Quad(coords, c, vof, area, vol);
break;
case DM_POLYTOPE_TETRAHEDRON:
VOF_3D_Tetra(coords, c, vof, area, vol);
break;
case DM_POLYTOPE_HEXAHEDRON:
VOF_3D_Hex(coords, c, vof, area, vol);
break;
default:
throw std::invalid_argument("No element geometry for cell " + std::to_string(p) + " with type " + DMPolytopeTypes[ct]);
}
DMPlexRestoreCellCoordinates(dm, p, &isDG, &Nc, &array, &coords) >> ablate::utilities::PetscUtilities::checkError;
}
// Returns the VOF for a given cell with a known level set value (c0) and normal (nIn).
// This computes the level-set values at the vertices by approximating the interface as a straight-line with the same normal
// as provided
void ablate::levelSet::Utilities::VOF(DM dm, const PetscInt p, const PetscReal c0, const PetscReal *nIn, PetscReal *vof, PetscReal *area, PetscReal *vol) {
PetscReal *c = NULL;
ablate::levelSet::Utilities::VertexLevelSet_LS(dm, p, c0, nIn, &c);
ablate::levelSet::Utilities::VOF(dm, p, c, vof, area, vol); // Do the actual calculation.
PetscFree(c) >> ablate::utilities::PetscUtilities::checkError;
}
// Returns the VOF for a given cell using an analytic level set equation
// Refer to "Quadrature rules for triangular and tetrahedral elements with generalized functions"
void ablate::levelSet::Utilities::VOF(DM dm, PetscInt p, const std::shared_ptr<ablate::mathFunctions::MathFunction> &phi, PetscReal *vof, PetscReal *area, PetscReal *vol) {
PetscInt dim, Nc, nVerts, i;
PetscReal *c = NULL, *coords = NULL;
const PetscScalar *array;
PetscBool isDG;
DMGetDimension(dm, &dim) >> ablate::utilities::PetscUtilities::checkError;
// Coordinates of the cell vertices
DMPlexGetCellCoordinates(dm, p, &isDG, &Nc, &array, &coords) >> ablate::utilities::PetscUtilities::checkError;
// Number of vertices
nVerts = Nc / dim;
PetscMalloc1(nVerts, &c) >> ablate::utilities::PetscUtilities::checkError;
// The level set value of each vertex. This assumes that the interface is a line/plane
// with the given unit normal.
for (i = 0; i < nVerts; ++i) {
c[i] = phi->Eval(&coords[i * dim], dim, 0.0);
}
DMPlexRestoreCellCoordinates(dm, p, &isDG, &Nc, &array, &coords) >> ablate::utilities::PetscUtilities::checkError;
ablate::levelSet::Utilities::VOF(dm, p, c, vof, area, vol); // Do the actual calculation.
PetscFree(c) >> ablate::utilities::PetscUtilities::checkError;
}
// Return the VOF in a cell where the level set is defined at vertices
void ablate::levelSet::Utilities::VOF(std::shared_ptr<ablate::domain::SubDomain> subDomain, PetscInt cell, const ablate::domain::Field *lsField, PetscReal *vof, PetscReal *area, PetscReal *vol) {
DM dm = subDomain->GetFieldDM(*lsField);
Vec vec = subDomain->GetVec(*lsField);
const PetscScalar *array;
PetscReal *c;
PetscInt nv, *verts;
DMPlexCellGetVertices(dm, cell, &nv, &verts) >> ablate::utilities::PetscUtilities::checkError;
DMGetWorkArray(dm, nv, MPI_REAL, &c);
VecGetArrayRead(vec, &array) >> ablate::utilities::PetscUtilities::checkError;
for (PetscInt i = 0; i < nv; ++i) {
const PetscReal *val = nullptr;
xDMPlexPointLocalRead(dm, verts[i], lsField->id, array, &val) >> ablate::utilities::PetscUtilities::checkError;
c[i] = *val;
}
VecRestoreArrayRead(vec, &array) >> ablate::utilities::PetscUtilities::checkError;
ablate::levelSet::Utilities::VOF(dm, cell, c, vof, area, vol);
DMRestoreWorkArray(dm, nv, MPI_REAL, &c);
DMPlexCellRestoreVertices(dm, cell, &nv, &verts) >> ablate::utilities::PetscUtilities::checkError;
}
void SaveVertexData(DM dm, Vec vec, const char fname[255], const ablate::domain::Field *field, PetscInt Nc, std::shared_ptr<ablate::domain::SubDomain> subDomain) {
ablate::domain::Range range;
const PetscReal *array, *val;
PetscInt dim = subDomain->GetDimensions();
MPI_Comm comm = PetscObjectComm((PetscObject)dm);
int rank, size;
MPI_Comm_size(comm, &size) >> ablate::utilities::MpiUtilities::checkError;
MPI_Comm_rank(comm, &rank) >> ablate::utilities::MpiUtilities::checkError;
ablate::domain::GetRange(dm, nullptr, 0, range);
VecGetArrayRead(vec, &array) >> ablate::utilities::PetscUtilities::checkError;
for (PetscInt r = 0; r < size; ++r) {
if ( rank==r ) {
FILE *f1;
if ( rank==0 ) f1 = fopen(fname, "w");
else f1 = fopen(fname, "a");
for (PetscInt v = range.start; v < range.end; ++v) {
PetscInt vert = range.points ? range.points[v] : v;
PetscScalar *coords;
DMPlexPointLocalFieldRead(dm, vert, field->id, array, &val) >> ablate::utilities::PetscUtilities::checkError;
DMPlexVertexGetCoordinates(dm, 1, &vert, &coords);
for (PetscInt d = 0; d < dim; ++d) {
fprintf(f1, "%+.16e\t", coords[d]);
}
for (PetscInt i = 0; i < Nc; ++i) {
fprintf(f1, "%+.16e\t", val[i]);
}
fprintf(f1, "\n");
DMPlexVertexRestoreCoordinates(dm, 1, &vert, &coords);
}
fclose(f1);
}
MPI_Barrier(comm);
}
VecRestoreArrayRead(vec, &array) >> ablate::utilities::PetscUtilities::checkError;
ablate::domain::RestoreRange(range);
}
void SaveVertexData(DM dm, Vec vec, const char fname[255], const ablate::domain::Field *field, std::shared_ptr<ablate::domain::SubDomain> subDomain) {
SaveVertexData(dm, vec, fname, field, 1, subDomain);
}
void SaveVertexData(const char fname[255], const ablate::domain::Field *field, PetscInt Nc, std::shared_ptr<ablate::domain::SubDomain> subDomain) {
Vec vec = subDomain->GetVec(*field);
DM dm = subDomain->GetFieldDM(*field);
SaveVertexData(dm, vec, fname, field, Nc, subDomain);
}
void SaveCellData(DM dm, const Vec vec, const char fname[255], const PetscInt id, PetscInt Nc, std::shared_ptr<ablate::domain::SubDomain> subDomain) {
ablate::domain::Range range;
const PetscScalar *array;
PetscInt dim = subDomain->GetDimensions();
MPI_Comm comm = PetscObjectComm((PetscObject)dm);
int rank, size;
MPI_Comm_size(comm, &size) >> ablate::utilities::MpiUtilities::checkError;
MPI_Comm_rank(comm, &rank) >> ablate::utilities::MpiUtilities::checkError;
subDomain->GetCellRange(nullptr, range);
VecGetArrayRead(vec, &array) >> ablate::utilities::PetscUtilities::checkError;
for (PetscInt r = 0; r < size; ++r) {
if ( rank==r ) {
FILE *f1;
if ( rank==0 ) f1 = fopen(fname, "w");
else f1 = fopen(fname, "a");
for (PetscInt c = range.start; c < range.end; ++c) {
PetscInt cell = range.points ? range.points[c] : c;
if (ablate::levelSet::Utilities::ValidCell(dm, cell)) {
PetscReal x0[3];
DMPlexComputeCellGeometryFVM(dm, cell, NULL, x0, NULL) >> ablate::utilities::PetscUtilities::checkError;
for (PetscInt d = 0; d < dim; ++d) {
fprintf(f1, "%+e\t", x0[d]);
}
const PetscScalar *val;
DMPlexPointLocalFieldRead(dm, cell, id, array, &val) >> ablate::utilities::PetscUtilities::checkError;
for (PetscInt i = 0; i < Nc; ++i) {
fprintf(f1, "%+e\t", val[i]);
}
fprintf(f1, "\n");
}
}
fclose(f1);
}
MPI_Barrier(PETSC_COMM_WORLD);
}
VecRestoreArrayRead(vec, &array) >> ablate::utilities::PetscUtilities::checkError;
ablate::domain::RestoreRange(range);
}
void SaveCellData(DM dm, const Vec vec, const char fname[255], const ablate::domain::Field *field, PetscInt Nc, std::shared_ptr<ablate::domain::SubDomain> subDomain) {
SaveCellData(dm, vec, fname, field->id, Nc, subDomain);
}
// Inter-processor ghost cells are iterated over, so everything should work fine
static void CutCellLevelSetValues(std::shared_ptr<ablate::domain::SubDomain> subDomain, ablate::domain::Range cellRange, ablate::domain::Range vertRange, ablate::domain::ReverseRange reverseVertRange, const PetscInt *cellMask, DM vofDM, Vec vofVec, const PetscInt vofID, DM lsDM, Vec lsVec, const PetscInt normalID, const PetscInt lsID) {
const PetscScalar *vofArray = nullptr;
PetscScalar *lsArray = nullptr;
PetscInt *lsCount;
VecGetArrayRead(vofVec, &vofArray) >> ablate::utilities::PetscUtilities::checkError;
VecGetArray(lsVec, &lsArray) >> ablate::utilities::PetscUtilities::checkError;
DMGetWorkArray(lsDM, vertRange.end - vertRange.start, MPIU_INT, &lsCount) >> ablate::utilities::PetscUtilities::checkError;
PetscArrayzero(lsCount, vertRange.end - vertRange.start) >> ablate::utilities::PetscUtilities::checkError;
lsCount -= vertRange.start;
for (PetscInt v = vertRange.start; v < vertRange.end; ++v) {
PetscInt vert = vertRange.GetPoint(v);
PetscReal *lsVal = nullptr;
xDMPlexPointLocalRef(lsDM, vert, lsID, lsArray, &lsVal) >> ablate::utilities::PetscUtilities::checkError;
*lsVal = 0.0;
}
//int rank;
//MPI_Comm_rank(PETSC_COMM_WORLD, &rank) >> ablate::utilities::MpiUtilities::checkError;
for (PetscInt c = cellRange.start; c < cellRange.end; ++c) {
// Only worry about cut-cells
if ( cellMask[c]==1 ) {
PetscInt cell = cellRange.GetPoint(c);
// The VOF for the cell
const PetscScalar *vofVal = nullptr;
xDMPlexPointLocalRead(vofDM, cell, vofID, vofArray, &vofVal) >> ablate::utilities::PetscUtilities::checkError;
// The pre-computed cell-centered normal
const PetscScalar *n = nullptr;
xDMPlexPointLocalRead(lsDM, cell, normalID, lsArray, &n) >> ablate::utilities::PetscUtilities::checkError;
PetscInt nv, *verts;
DMPlexCellGetVertices(vofDM, cell, &nv, &verts) >> ablate::utilities::PetscUtilities::checkError;
PetscReal *lsVertVals = NULL;
DMGetWorkArray(lsDM, nv, MPIU_REAL, &lsVertVals) >> ablate::utilities::PetscUtilities::checkError;
// Level set values at the vertices
ablate::levelSet::Utilities::VertexLevelSet_VOF(lsDM, cell, *vofVal, n, &lsVertVals);
for (PetscInt v = 0; v < nv; ++v) {
PetscScalar *lsVal = nullptr;
xDMPlexPointLocalRef(lsDM, verts[v], lsID, lsArray, &lsVal) >> ablate::utilities::PetscUtilities::checkError;
*lsVal += lsVertVals[v];
PetscInt vert_i = reverseVertRange.GetIndex(verts[v]);
++lsCount[vert_i];
}
DMRestoreWorkArray(lsDM, nv, MPIU_REAL, &lsVertVals) >> ablate::utilities::PetscUtilities::checkError;
DMPlexCellRestoreVertices(vofDM, cell, &nv, &verts) >> ablate::utilities::PetscUtilities::checkError;
}
}
// This is no longer needed
VecRestoreArrayRead(vofVec, &vofArray) >> ablate::utilities::PetscUtilities::checkError;
for (PetscInt v = vertRange.start; v < vertRange.end; ++v) {
if ( lsCount[v] > 0 ) {
PetscInt vert = vertRange.GetPoint(v);
PetscReal *lsVal = nullptr;
xDMPlexPointLocalRef(lsDM, vert, lsID, lsArray, &lsVal) >> ablate::utilities::PetscUtilities::checkError;
*lsVal /= lsCount[v];
}
}
lsCount += vertRange.start;
DMRestoreWorkArray(lsDM, vertRange.end - vertRange.start, MPIU_INT, &lsCount) >> ablate::utilities::PetscUtilities::checkError;
VecRestoreArray(lsVec, &lsArray) >> ablate::utilities::PetscUtilities::checkError;
subDomain->UpdateAuxLocalVector();
}
//struct reinitCTX {
// std::shared_ptr<ablate::domain::rbf::RBF> rbf;
//};
//static Parameters parameters{};
#include "domain/RBF/ga.hpp"
#include "domain/RBF/hybrid.hpp"
#include "domain/RBF/imq.hpp"
#include "domain/RBF/mq.hpp"
#include "domain/RBF/phs.hpp"
#include "domain/RBF/rbf.hpp"
static std::shared_ptr<ablate::domain::rbf::RBF> vertRBF = nullptr;
static std::shared_ptr<ablate::domain::rbf::RBF> cellRBF = nullptr;
// Temporary for the review
//static PetscInt **cellNeighbors = nullptr, *numberNeighbors = nullptr;
// Make sure that the work is being done on valid cells and not ghost cells
bool ablate::levelSet::Utilities::ValidCell(DM dm, PetscInt p) {
DMPolytopeType ct;
DMPlexGetCellType(dm, p, &ct) >> ablate::utilities::PetscUtilities::checkError;
return (ct < DM_POLYTOPE_FV_GHOST);
}
PetscReal GaussianDerivativeFactor(const PetscInt dim, const PetscReal *x, const PetscReal s, const PetscInt dx, const PetscInt dy, const PetscInt dz) {
const PetscReal s2 = PetscSqr(s);
const PetscInt derHash = 100*dx + 10*dy + dz;
if (derHash > 0 && PetscAbsReal(s)<PETSC_SMALL) return (0.0);
switch (derHash) {
case 0: // Value
return (1.0);
case 100: // x
return (x[0]/s2);
case 10: // y
return (x[1]/s2);
case 1: // z
return (x[2]/s2);
case 200: // xx
return ((x[0]*x[0] - s2)/PetscSqr(s2));
case 20: // yy
return ((x[1]*x[1] - s2)/PetscSqr(s2));
case 2: // zz
return ((x[2]*x[2] - s2)/PetscSqr(s2));
case 110: // xy
return (x[0]*x[1]/PetscSqr(s2));
case 101: // xz
return (x[0]*x[2]/PetscSqr(s2));
case 11: // yz
return (x[1]*x[2]/PetscSqr(s2));
default:
throw std::runtime_error("Unknown derivative request");
}
}
static PetscInt FindCell(DM dm, const PetscReal x0[], const PetscInt nCells, const PetscInt cells[], PetscReal *distOut) {
// Return the cell with the cell-center that is the closest to a given point
PetscReal dist = PETSC_MAX_REAL;
PetscInt closestCell = -1;
PetscInt dim;
DMGetDimension(dm, &dim) >> ablate::utilities::PetscUtilities::checkError;
for (PetscInt c = 0; c < nCells; ++c) {
PetscReal x[dim];
DMPlexComputeCellGeometryFVM(dm, cells[c], NULL, x, NULL) >> ablate::utilities::PetscUtilities::checkError;
ablate::utilities::MathUtilities::Subtract(dim, x, x0, x);
PetscReal cellDist = ablate::utilities::MathUtilities::MagVector(dim, x);
if (cellDist < dist) {
closestCell = cells[c];
dist = cellDist;
}
}
if (distOut) *distOut = dist;
return (closestCell);
}
static PetscInt *interpCellList = nullptr;
// Hermite-Gauss quadrature points
static PetscInt nQuad = 4; // Size of the 1D quadrature
// The quadrature is actually sqrt(2) times the quadrature points. This is as we are integrating
// against the normal distribution, not exp(-x^2)
static PetscReal quad[4] = {-0.74196378430272585764851359672636022482952014750891895361147387899499975465000530,
0.74196378430272585764851359672636022482952014750891895361147387899499975465000530,
-2.3344142183389772393175122672103621944890707102161406718291603341725665622712306,
2.3344142183389772393175122672103621944890707102161406718291603341725665622712306};
// The weights are the true weights divided by sqrt(pi)
static PetscReal weights[4] = {0.45412414523193150818310700622549094933049562338805584403605771393758003145477625,
0.45412414523193150818310700622549094933049562338805584403605771393758003145477625,
0.045875854768068491816892993774509050669504376611944155963942286062419968545223748,
0.045875854768068491816892993774509050669504376611944155963942286062419968545223748};
static PetscReal sigmaFactor = 1.0;
void BuildInterpCellList(DM dm, const ablate::domain::Range cellRange) {
PetscReal h;
DMPlexGetMinRadius(dm, &h) >> ablate::utilities::PetscUtilities::checkError;
h *= 2.0; // Min radius returns the distance between a cell-center and a face. Double it to get the average cell size
const PetscReal sigma = sigmaFactor*h;
PetscInt dim;
DMGetDimension(dm, &dim) >> ablate::utilities::PetscUtilities::checkError;
PetscMalloc1(16*(cellRange.end - cellRange.start), &interpCellList);
for (PetscInt c = cellRange.start; c < cellRange.end; ++c) {
PetscInt cell = cellRange.GetPoint(c);
PetscReal x0[dim];
DMPlexComputeCellGeometryFVM(dm, cell, NULL, x0, NULL) >> ablate::utilities::PetscUtilities::checkError;
PetscInt nCells, *cellList;
DMPlexGetNeighbors(dm, cell, 2, -1.0, -1, PETSC_FALSE, PETSC_FALSE, &nCells, &cellList) >> ablate::utilities::PetscUtilities::checkError;
for (PetscInt i = 0; i < nQuad; ++i) {
for (PetscInt j = 0; j < nQuad; ++j) {
PetscReal x[2] = {x0[0] + sigma*quad[i], x0[1] + sigma*quad[j]};
const PetscInt interpCell = FindCell(dm, x, nCells, cellList, NULL);
if (interpCell < 0) {
PetscInt dim;
DMGetDimension(dm, &dim) >> ablate::utilities::PetscUtilities::checkError;
int rank;
MPI_Comm_rank(PETSC_COMM_WORLD, &rank);
throw std::runtime_error("BuildInterpCellList could not determine the location of (" + std::to_string(x[0]) + ", " + std::to_string(x[1]) + ") on rank " + std::to_string(rank) + ".");
}
interpCellList[(c - cellRange.start)*16 + i*4 + j] = interpCell;
}
}
DMPlexRestoreNeighbors(dm, cell, 2, -1.0, -1, PETSC_FALSE, PETSC_FALSE, &nCells, &cellList) >> ablate::utilities::PetscUtilities::checkError;
}
}
// Calculate the curvature from a vertex-based level set field using Gaussian convolution.
// Right now this is just 2D for testing purposes.
void CurvatureViaGaussian(DM dm, const PetscInt c, const PetscInt cell, const Vec vec, const ablate::domain::Field *lsField, double *H) {
PetscInt dim;
DMGetDimension(dm, &dim) >> ablate::utilities::PetscUtilities::checkError;
PetscReal h;
DMPlexGetMinRadius(dm, &h) >> ablate::utilities::PetscUtilities::checkError;
h *= 2.0; // Min radius returns the distance between a cell-center and a face. Double it to get the average cell size
// const PetscInt nQuad = 3; // Size of the 1D quadrature
// const PetscReal quad[] = {0.0, PetscSqrtReal(3.0), -PetscSqrtReal(3.0)};
// const PetscReal weights[] = {2.0/3.0, 1.0/6.0, 1.0/6.0};
PetscReal x0[dim], vol;
DMPlexComputeCellGeometryFVM(dm, cell, &vol, x0, NULL) >> ablate::utilities::PetscUtilities::checkError;
// Mabye relate this to PETSC_SQRT_MACHINE_EPSILON or similar?
// The would probably require that the derivative factor be re-done to account for round-off.
const PetscReal sigma = sigmaFactor*h;
PetscReal cx = 0.0, cy = 0.0, cxx = 0.0, cyy = 0.0, cxy = 0.0;
for (PetscInt i = 0; i < nQuad; ++i) {
for (PetscInt j = 0; j < nQuad; ++j) {
const PetscReal dist[2] = {sigma*quad[i], sigma*quad[j]};
PetscReal x[2] = {x0[0] + dist[0], x0[1] + dist[1]};
// const PetscInt interpCell = FindCell(dm, x, nCells, cellList, NULL);
const PetscInt interpCell = interpCellList[c*16 + i*4 + j];
const PetscReal lsVal = vertRBF->Interpolate(lsField, vec, interpCell, x);
const PetscReal wt = weights[i]*weights[j];
cx += wt*GaussianDerivativeFactor(dim, dist, sigma, 1, 0, 0)*lsVal;
cy += wt*GaussianDerivativeFactor(dim, dist, sigma, 0, 1, 0)*lsVal;
cxx += wt*GaussianDerivativeFactor(dim, dist, sigma, 2, 0, 0)*lsVal;
cyy += wt*GaussianDerivativeFactor(dim, dist, sigma, 0, 2, 0)*lsVal;
cxy += wt*GaussianDerivativeFactor(dim, dist, sigma, 1, 1, 0)*lsVal;
}
}
*H = (cxx*cy*cy + cyy*cx*cx - 2.0*cxy*cx*cy)/PetscPowReal(cx*cx + cy*cy, 1.5);
}
// Calculate the curvature from a vertex-based level set field using Gaussian convolution.
// Right now this is just 2D for testing purposes.
PetscReal LaplacianViaGaussian(DM dm, const PetscInt c, const PetscInt cell, const Vec vec, const ablate::domain::Field *lsField) {
PetscInt dim;
DMGetDimension(dm, &dim) >> ablate::utilities::PetscUtilities::checkError;
PetscReal h;
DMPlexGetMinRadius(dm, &h) >> ablate::utilities::PetscUtilities::checkError;
h *= 2.0; // Min radius returns the distance between a cell-center and a face. Double it to get the average cell size
// const PetscInt nQuad = 3; // Size of the 1D quadrature
// const PetscReal quad[] = {0.0, PetscSqrtReal(3.0), -PetscSqrtReal(3.0)};
// const PetscReal weights[] = {2.0/3.0, 1.0/6.0, 1.0/6.0};
PetscReal x0[dim], vol;
DMPlexComputeCellGeometryFVM(dm, cell, &vol, x0, NULL) >> ablate::utilities::PetscUtilities::checkError;
// Mabye relate this to PETSC_SQRT_MACHINE_EPSILON or similar?
// The would probably require that the derivative factor be re-done to account for round-off.
const PetscReal sigma = sigmaFactor*h;
PetscReal cxx = 0.0, cyy = 0.0;
for (PetscInt i = 0; i < nQuad; ++i) {
for (PetscInt j = 0; j < nQuad; ++j) {
const PetscReal dist[2] = {sigma*quad[i], sigma*quad[j]};
PetscReal x[2] = {x0[0] + dist[0], x0[1] + dist[1]};
// const PetscInt interpCell = FindCell(dm, x, nCells, cellList, NULL);
const PetscInt interpCell = interpCellList[c*16 + i*4 + j];
const PetscReal lsVal = vertRBF->Interpolate(lsField, vec, interpCell, x);
const PetscReal wt = weights[i]*weights[j];
cxx += wt*GaussianDerivativeFactor(dim, dist, sigma, 2, 0, 0)*lsVal;
cyy += wt*GaussianDerivativeFactor(dim, dist, sigma, 0, 2, 0)*lsVal;
}
}
return (cxx + cyy);
}
// Calculate the curvature from a vertex-based level set field using Gaussian convolution.
// Right now this is just 2D for testing purposes.
PetscReal SmoothingViaGaussian(DM dm, const PetscInt c, const PetscInt cell, const Vec vec, const ablate::domain::Field *field) {
PetscInt dim;
DMGetDimension(dm, &dim) >> ablate::utilities::PetscUtilities::checkError;
PetscReal h;
DMPlexGetMinRadius(dm, &h) >> ablate::utilities::PetscUtilities::checkError;
h *= 2.0; // Min radius returns the distance between a cell-center and a face. Double it to get the average cell size
// const PetscInt nQuad = 3; // Size of the 1D quadrature
// const PetscReal quad[] = {0.0, PetscSqrtReal(3.0), -PetscSqrtReal(3.0)};
// const PetscReal weights[] = {2.0/3.0, 1.0/6.0, 1.0/6.0};
PetscReal x0[dim], vol;
DMPlexComputeCellGeometryFVM(dm, cell, &vol, x0, NULL) >> ablate::utilities::PetscUtilities::checkError;
// Mabye relate this to PETSC_SQRT_MACHINE_EPSILON or similar?
// The would probably require that the derivative factor be re-done to account for round-off.
const PetscReal sigma = sigmaFactor*h;
PetscReal newVal = 0.0;
for (PetscInt i = 0; i < nQuad; ++i) {
for (PetscInt j = 0; j < nQuad; ++j) {
const PetscReal dist[2] = {sigma*quad[i], sigma*quad[j]};
PetscReal x[2] = {x0[0] + dist[0], x0[1] + dist[1]};
const PetscInt interpCell = interpCellList[c*16 + i*4 + j];
const PetscReal lsVal = vertRBF->Interpolate(field, vec, interpCell, x);
const PetscReal wt = weights[i]*weights[j];
newVal += wt*GaussianDerivativeFactor(dim, dist, sigma, 0, 0, 0)*lsVal;
}
}
return (newVal);
}
#define saveData
#ifdef saveData
static PetscInt saveIter = 0;
#endif
//vofField: cell-based field containing the target volume-of-fluid
//lsField: vertex-based field for level set values
//normalField: cell-based vector field containing normals
//curvField: cell-based vector field containing curvature
void ablate::levelSet::Utilities::Reinitialize(
const ablate::finiteVolume::FiniteVolumeSolver &flow,
std::shared_ptr<ablate::domain::SubDomain> subDomain,
const Vec solVec,
const ablate::domain::Field *vofField,
const PetscInt nLevels,
const ablate::domain::Field *lsField,
const ablate::domain::Field *vertexNormalField,
const ablate::domain::Field *cellNormalField,
const ablate::domain::Field *curvField
) {
#ifdef saveData
++saveIter;
#endif
// Note: Need to write a unit test where the vof and ls fields aren't in the same DM, e.g. one is a SOL vector and one is an AUX vector.
// Make sure that all of the fields are in the correct locations.
if ( vofField->location != ablate::domain::FieldLocation::SOL ){
throw std::runtime_error("VOF field must be in SOL");
}
if ( lsField->location != ablate::domain::FieldLocation::AUX ){
throw std::runtime_error("Level set field must be in AUX");
}
if ( vertexNormalField->location != ablate::domain::FieldLocation::AUX ){
throw std::runtime_error("Vertex Normal field must be in AUX");
}
if ( cellNormalField->location != ablate::domain::FieldLocation::AUX ){
throw std::runtime_error("Cell Normal field must be in AUX");
}
if ( curvField->location != ablate::domain::FieldLocation::AUX ){
throw std::runtime_error("Curvature Field field must be in AUX");