-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex12.cpp
2451 lines (2161 loc) · 119 KB
/
ex12.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
static char help[] = "Poisson Problem in 2d and 3d with simplicial finite elements.\n\
We solve the Poisson problem in a rectangular\n\
domain, using a parallel unstructured mesh (DMPLEX) to discretize it.\n\
This example supports discretized auxiliary fields (conductivity) as well as\n\
multilevel nonlinear solvers.\n\n\n";
/*
A visualization of the adaptation can be accomplished using:
-dm_adapt_view hdf5:$PWD/adapt.h5 -sol_adapt_view hdf5:$PWD/adapt.h5::append -dm_adapt_pre_view hdf5:$PWD/orig.h5 -sol_adapt_pre_view hdf5:$PWD/orig.h5::append
Information on refinement:
-info -info_exclude null,sys,vec,is,mat,ksp,snes,ts
*/
#include <petscdmplex.h>
#include <petscdmadaptor.h>
#include <petscsnes.h>
#include <petscds.h>
#include <petscviewerhdf5.h>
#include <petscsf.h>
#include <petscviewer.h>
#include <Omega_h_file.hpp>
#include <Omega_h_library.hpp>
#include <Omega_h_mesh.hpp>
#include <Omega_h_comm.hpp>
#include <Omega_h_build.hpp>
#include <Omega_h_for.hpp>
#include <Omega_h_int_scan.hpp>
#include <Omega_h_array_ops.hpp>
#include <Omega_h_print.hpp>
#include <algorithm>
#include <vector>
#include <set>
#include <iostream>
#include <unordered_set>
typedef enum {NEUMANN, DIRICHLET, NONE} BCType;
typedef enum {RUN_FULL, RUN_EXACT, RUN_TEST, RUN_PERF} RunType;
typedef enum {COEFF_NONE, COEFF_ANALYTIC, COEFF_FIELD, COEFF_NONLINEAR, COEFF_CIRCLE, COEFF_CROSS} CoeffType;
typedef struct {
PetscInt debug; /* The debugging level */
RunType runType; /* Whether to run tests, or solve the full problem */
PetscBool jacobianMF; /* Whether to calculate the Jacobian action on the fly */
PetscLogEvent createMeshEvent;
PetscBool showInitial, showSolution, restart, quiet, nonzInit;
/* Domain and mesh definition */
PetscInt dim; /* The topological mesh dimension */
DMBoundaryType periodicity[3]; /* The domain periodicity */
PetscInt cells[3]; /* The initial domain division */
char filename[2048]; /* The optional mesh file */
PetscBool interpolate; /* Generate intermediate mesh elements */
PetscReal refinementLimit; /* The largest allowable cell volume */
PetscBool viewHierarchy; /* Whether to view the hierarchy */
PetscBool simplex; /* Simplicial mesh */
/* Problem definition */
BCType bcType;
CoeffType variableCoefficient;
PetscErrorCode (**exactFuncs)(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *ctx);
PetscBool fieldBC;
void (**exactFields)(PetscInt, PetscInt, PetscInt,
const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]);
PetscBool bdIntegral; /* Compute the integral of the solution on the boundary */
/* Solver */
PC pcmg; /* This is needed for error monitoring */
PetscBool checkksp; /* Whether to check the KSPSolve for runType == RUN_TEST */
char mesh_type[512] = "box";
char picpart_path[512] = "picpart.osh";
} AppCtx;
static PetscErrorCode zero(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *ctx)
{
u[0] = 0.0;
return 0;
}
static PetscErrorCode ecks(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *ctx)
{
u[0] = x[0];
return 0;
}
/*
In 2D for Dirichlet conditions, we use exact solution:
u = x^2 + y^2
f = 4
so that
-\Delta u + f = -4 + 4 = 0
For Neumann conditions, we have
-\nabla u \cdot -\hat y |_{y=0} = (2y)|_{y=0} = 0 (bottom)
-\nabla u \cdot \hat y |_{y=1} = -(2y)|_{y=1} = -2 (top)
-\nabla u \cdot -\hat x |_{x=0} = (2x)|_{x=0} = 0 (left)
-\nabla u \cdot \hat x |_{x=1} = -(2x)|_{x=1} = -2 (right)
Which we can express as
\nabla u \cdot \hat n|_\Gamma = {2 x, 2 y} \cdot \hat n = 2 (x + y)
The boundary integral of this solution is (assuming we are not orienting the edges)
\int^1_0 x^2 dx + \int^1_0 (1 + y^2) dy + \int^1_0 (x^2 + 1) dx + \int^1_0 y^2 dy = 1/3 + 4/3 + 4/3 + 1/3 = 3 1/3
*/
static PetscErrorCode quadratic_u_2d(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *ctx)
{
*u = x[0]*x[0] + x[1]*x[1];
return 0;
}
static void quadratic_u_field_2d(PetscInt dim, PetscInt Nf, PetscInt NfAux,
const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar uexact[])
{
uexact[0] = a[0];
}
static PetscErrorCode circle_u_2d(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *ctx)
{
const PetscReal alpha = 500.;
const PetscReal radius2 = PetscSqr(0.15);
const PetscReal r2 = PetscSqr(x[0] - 0.5) + PetscSqr(x[1] - 0.5);
const PetscReal xi = alpha*(radius2 - r2);
*u = PetscTanhScalar(xi) + 1.0;
return 0;
}
static PetscErrorCode cross_u_2d(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *ctx)
{
const PetscReal alpha = 50*4;
const PetscReal xy = (x[0]-0.5)*(x[1]-0.5);
*u = PetscSinReal(alpha*xy) * (alpha*PetscAbsReal(xy) < 2*PETSC_PI ? 1 : 0.01);
return 0;
}
static void f0_u(PetscInt dim, PetscInt Nf, PetscInt NfAux,
const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
{
f0[0] = 4.0;
}
static void f0_circle_u(PetscInt dim, PetscInt Nf, PetscInt NfAux,
const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
{
const PetscReal alpha = 500.;
const PetscReal radius2 = PetscSqr(0.15);
const PetscReal r2 = PetscSqr(x[0] - 0.5) + PetscSqr(x[1] - 0.5);
const PetscReal xi = alpha*(radius2 - r2);
f0[0] = (-4.0*alpha - 8.0*PetscSqr(alpha)*r2*PetscTanhReal(xi)) * PetscSqr(1.0/PetscCoshReal(xi));
}
static void f0_cross_u(PetscInt dim, PetscInt Nf, PetscInt NfAux,
const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
{
const PetscReal alpha = 50*4;
const PetscReal xy = (x[0]-0.5)*(x[1]-0.5);
f0[0] = PetscSinReal(alpha*xy) * (alpha*PetscAbsReal(xy) < 2*PETSC_PI ? 1 : 0.01);
}
static void f0_bd_u(PetscInt dim, PetscInt Nf, PetscInt NfAux,
const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
PetscReal t, const PetscReal x[], const PetscReal n[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
{
PetscInt d;
for (d = 0, f0[0] = 0.0; d < dim; ++d) f0[0] += -n[d]*2.0*x[d];
}
static void f1_bd_zero(PetscInt dim, PetscInt Nf, PetscInt NfAux,
const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
PetscReal t, const PetscReal x[], const PetscReal n[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f1[])
{
PetscInt comp;
for (comp = 0; comp < dim; ++comp) f1[comp] = 0.0;
}
/* gradU[comp*dim+d] = {u_x, u_y} or {u_x, u_y, u_z} */
static void f1_u(PetscInt dim, PetscInt Nf, PetscInt NfAux,
const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f1[])
{
PetscInt d;
for (d = 0; d < dim; ++d) f1[d] = u_x[d];
}
/* < \nabla v, \nabla u + {\nabla u}^T >
This just gives \nabla u, give the perdiagonal for the transpose */
static void g3_uu(PetscInt dim, PetscInt Nf, PetscInt NfAux,
const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g3[])
{
PetscInt d;
for (d = 0; d < dim; ++d) g3[d*dim+d] = 1.0;
}
/*
In 2D for x periodicity and y Dirichlet conditions, we use exact solution:
u = sin(2 pi x)
f = -4 pi^2 sin(2 pi x)
so that
-\Delta u + f = 4 pi^2 sin(2 pi x) - 4 pi^2 sin(2 pi x) = 0
*/
static PetscErrorCode xtrig_u_2d(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *ctx)
{
*u = PetscSinReal(2.0*PETSC_PI*x[0]);
return 0;
}
static void f0_xtrig_u(PetscInt dim, PetscInt Nf, PetscInt NfAux,
const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
{
f0[0] = -4.0*PetscSqr(PETSC_PI)*PetscSinReal(2.0*PETSC_PI*x[0]);
}
/*
In 2D for x-y periodicity, we use exact solution:
u = sin(2 pi x) sin(2 pi y)
f = -8 pi^2 sin(2 pi x)
so that
-\Delta u + f = 4 pi^2 sin(2 pi x) sin(2 pi y) + 4 pi^2 sin(2 pi x) sin(2 pi y) - 8 pi^2 sin(2 pi x) = 0
*/
static PetscErrorCode xytrig_u_2d(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *ctx)
{
*u = PetscSinReal(2.0*PETSC_PI*x[0])*PetscSinReal(2.0*PETSC_PI*x[1]);
return 0;
}
static void f0_xytrig_u(PetscInt dim, PetscInt Nf, PetscInt NfAux,
const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
{
f0[0] = -8.0*PetscSqr(PETSC_PI)*PetscSinReal(2.0*PETSC_PI*x[0]);
}
/*
In 2D for Dirichlet conditions with a variable coefficient, we use exact solution:
u = x^2 + y^2
f = 6 (x + y)
nu = (x + y)
so that
-\div \nu \grad u + f = -6 (x + y) + 6 (x + y) = 0
*/
static PetscErrorCode nu_2d(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *ctx)
{
*u = x[0] + x[1];
return 0;
}
void f0_analytic_u(PetscInt dim, PetscInt Nf, PetscInt NfAux,
const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
{
f0[0] = 6.0*(x[0] + x[1]);
}
/* gradU[comp*dim+d] = {u_x, u_y} or {u_x, u_y, u_z} */
void f1_analytic_u(PetscInt dim, PetscInt Nf, PetscInt NfAux,
const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f1[])
{
PetscInt d;
for (d = 0; d < dim; ++d) f1[d] = (x[0] + x[1])*u_x[d];
}
void f1_field_u(PetscInt dim, PetscInt Nf, PetscInt NfAux,
const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f1[])
{
PetscInt d;
for (d = 0; d < dim; ++d) f1[d] = a[0]*u_x[d];
}
/* < \nabla v, \nabla u + {\nabla u}^T >
This just gives \nabla u, give the perdiagonal for the transpose */
void g3_analytic_uu(PetscInt dim, PetscInt Nf, PetscInt NfAux,
const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g3[])
{
PetscInt d;
for (d = 0; d < dim; ++d) g3[d*dim+d] = x[0] + x[1];
}
void g3_field_uu(PetscInt dim, PetscInt Nf, PetscInt NfAux,
const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g3[])
{
PetscInt d;
for (d = 0; d < dim; ++d) g3[d*dim+d] = a[0];
}
/*
In 2D for Dirichlet conditions with a nonlinear coefficient (p-Laplacian with p = 4), we use exact solution:
u = x^2 + y^2
f = 16 (x^2 + y^2)
nu = 1/2 |grad u|^2
so that
-\div \nu \grad u + f = -16 (x^2 + y^2) + 16 (x^2 + y^2) = 0
*/
void f0_analytic_nonlinear_u(PetscInt dim, PetscInt Nf, PetscInt NfAux,
const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
{
f0[0] = 16.0*(x[0]*x[0] + x[1]*x[1]);
}
/* gradU[comp*dim+d] = {u_x, u_y} or {u_x, u_y, u_z} */
void f1_analytic_nonlinear_u(PetscInt dim, PetscInt Nf, PetscInt NfAux,
const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f1[])
{
PetscScalar nu = 0.0;
PetscInt d;
for (d = 0; d < dim; ++d) nu += u_x[d]*u_x[d];
for (d = 0; d < dim; ++d) f1[d] = 0.5*nu*u_x[d];
}
/*
grad (u + eps w) - grad u = eps grad w
1/2 |grad (u + eps w)|^2 grad (u + eps w) - 1/2 |grad u|^2 grad u
= 1/2 (|grad u|^2 + 2 eps <grad u,grad w>) (grad u + eps grad w) - 1/2 |grad u|^2 grad u
= 1/2 (eps |grad u|^2 grad w + 2 eps <grad u,grad w> grad u)
= eps (1/2 |grad u|^2 grad w + grad u <grad u,grad w>)
*/
void g3_analytic_nonlinear_uu(PetscInt dim, PetscInt Nf, PetscInt NfAux,
const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g3[])
{
PetscScalar nu = 0.0;
PetscInt d, e;
for (d = 0; d < dim; ++d) nu += u_x[d]*u_x[d];
for (d = 0; d < dim; ++d) {
g3[d*dim+d] = 0.5*nu;
for (e = 0; e < dim; ++e) {
g3[d*dim+e] += u_x[d]*u_x[e];
}
}
}
/*
In 3D for Dirichlet conditions we use exact solution:
u = 2/3 (x^2 + y^2 + z^2)
f = 4
so that
-\Delta u + f = -2/3 * 6 + 4 = 0
For Neumann conditions, we have
-\nabla u \cdot -\hat z |_{z=0} = (2z)|_{z=0} = 0 (bottom)
-\nabla u \cdot \hat z |_{z=1} = -(2z)|_{z=1} = -2 (top)
-\nabla u \cdot -\hat y |_{y=0} = (2y)|_{y=0} = 0 (front)
-\nabla u \cdot \hat y |_{y=1} = -(2y)|_{y=1} = -2 (back)
-\nabla u \cdot -\hat x |_{x=0} = (2x)|_{x=0} = 0 (left)
-\nabla u \cdot \hat x |_{x=1} = -(2x)|_{x=1} = -2 (right)
Which we can express as
\nabla u \cdot \hat n|_\Gamma = {2 x, 2 y, 2z} \cdot \hat n = 2 (x + y + z)
*/
static PetscErrorCode quadratic_u_3d(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *ctx)
{
*u = 2.0*(x[0]*x[0] + x[1]*x[1] + x[2]*x[2])/3.0;
return 0;
}
static void quadratic_u_field_3d(PetscInt dim, PetscInt Nf, PetscInt NfAux,
const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar uexact[])
{
uexact[0] = a[0];
}
static void bd_integral_2d(PetscInt dim, PetscInt Nf, PetscInt NfAux,
const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
PetscReal t, const PetscReal x[], const PetscReal n[], PetscInt numConstants, const PetscScalar constants[], PetscScalar *uint)
{
uint[0] = u[0];
}
static PetscErrorCode ProcessOptions(MPI_Comm comm, AppCtx *options)
{
const char *bcTypes[3] = {"neumann", "dirichlet", "none"};
const char *runTypes[4] = {"full", "exact", "test", "perf"};
const char *coeffTypes[6] = {"none", "analytic", "field", "nonlinear", "circle", "cross"};
PetscInt bd, bc, run, coeff, n;
PetscBool flg;
PetscErrorCode ierr;
PetscFunctionBeginUser;
options->debug = 0;
options->runType = RUN_FULL;
options->dim = 2;
options->periodicity[0] = DM_BOUNDARY_NONE;
options->periodicity[1] = DM_BOUNDARY_NONE;
options->periodicity[2] = DM_BOUNDARY_NONE;
options->cells[0] = 2;
options->cells[1] = 2;
options->cells[2] = 2;
options->filename[0] = '\0';
options->interpolate = PETSC_TRUE;
options->refinementLimit = 0.0;
options->bcType = DIRICHLET;
options->variableCoefficient = COEFF_NONE;
options->fieldBC = PETSC_FALSE;
options->jacobianMF = PETSC_FALSE;
options->showInitial = PETSC_FALSE;
options->showSolution = PETSC_FALSE;
options->restart = PETSC_FALSE;
options->viewHierarchy = PETSC_FALSE;
options->simplex = PETSC_TRUE;
options->quiet = PETSC_FALSE;
options->nonzInit = PETSC_FALSE;
options->bdIntegral = PETSC_FALSE;
options->checkksp = PETSC_FALSE;
ierr = PetscOptionsBegin(comm, "", "Poisson Problem Options", "DMPLEX");CHKERRQ(ierr);
ierr = PetscOptionsInt("-debug", "The debugging level", "ex12.c", options->debug, &options->debug, NULL);CHKERRQ(ierr);
run = options->runType;
ierr = PetscOptionsEList("-run_type", "The run type", "ex12.c", runTypes, 4, runTypes[options->runType], &run, NULL);CHKERRQ(ierr);
options->runType = (RunType) run;
ierr = PetscOptionsInt("-dim", "The topological mesh dimension", "ex12.c", options->dim, &options->dim, NULL);CHKERRQ(ierr);
bd = options->periodicity[0];
ierr = PetscOptionsEList("-x_periodicity", "The x-boundary periodicity", "ex12.c", DMBoundaryTypes, 5, DMBoundaryTypes[options->periodicity[0]], &bd, NULL);CHKERRQ(ierr);
options->periodicity[0] = (DMBoundaryType) bd;
bd = options->periodicity[1];
ierr = PetscOptionsEList("-y_periodicity", "The y-boundary periodicity", "ex12.c", DMBoundaryTypes, 5, DMBoundaryTypes[options->periodicity[1]], &bd, NULL);CHKERRQ(ierr);
options->periodicity[1] = (DMBoundaryType) bd;
bd = options->periodicity[2];
ierr = PetscOptionsEList("-z_periodicity", "The z-boundary periodicity", "ex12.c", DMBoundaryTypes, 5, DMBoundaryTypes[options->periodicity[2]], &bd, NULL);CHKERRQ(ierr);
options->periodicity[2] = (DMBoundaryType) bd;
n = 3;
ierr = PetscOptionsIntArray("-cells", "The initial mesh division", "ex12.c", options->cells, &n, NULL);CHKERRQ(ierr);
ierr = PetscOptionsString("-f", "Mesh filename to read", "ex12.c", options->filename, options->filename, sizeof(options->filename), &flg);CHKERRQ(ierr);
ierr = PetscOptionsBool("-interpolate", "Generate intermediate mesh elements", "ex12.c", options->interpolate, &options->interpolate, NULL);CHKERRQ(ierr);
ierr = PetscOptionsReal("-refinement_limit", "The largest allowable cell volume", "ex12.c", options->refinementLimit, &options->refinementLimit, NULL);CHKERRQ(ierr);
bc = options->bcType;
ierr = PetscOptionsEList("-bc_type","Type of boundary condition","ex12.c",bcTypes,3,bcTypes[options->bcType],&bc,NULL);CHKERRQ(ierr);
options->bcType = (BCType) bc;
coeff = options->variableCoefficient;
ierr = PetscOptionsEList("-variable_coefficient","Type of variable coefficent","ex12.c",coeffTypes,6,coeffTypes[options->variableCoefficient],&coeff,NULL);CHKERRQ(ierr);
options->variableCoefficient = (CoeffType) coeff;
ierr = PetscOptionsBool("-field_bc", "Use a field representation for the BC", "ex12.c", options->fieldBC, &options->fieldBC, NULL);CHKERRQ(ierr);
ierr = PetscOptionsBool("-jacobian_mf", "Calculate the action of the Jacobian on the fly", "ex12.c", options->jacobianMF, &options->jacobianMF, NULL);CHKERRQ(ierr);
ierr = PetscOptionsBool("-show_initial", "Output the initial guess for verification", "ex12.c", options->showInitial, &options->showInitial, NULL);CHKERRQ(ierr);
ierr = PetscOptionsBool("-show_solution", "Output the solution for verification", "ex12.c", options->showSolution, &options->showSolution, NULL);CHKERRQ(ierr);
ierr = PetscOptionsBool("-restart", "Read in the mesh and solution from a file", "ex12.c", options->restart, &options->restart, NULL);CHKERRQ(ierr);
ierr = PetscOptionsBool("-dm_view_hierarchy", "View the coarsened hierarchy", "ex12.c", options->viewHierarchy, &options->viewHierarchy, NULL);CHKERRQ(ierr);
ierr = PetscOptionsBool("-simplex", "Simplicial (true) or tensor (false) mesh", "ex12.c", options->simplex, &options->simplex, NULL);CHKERRQ(ierr);
ierr = PetscOptionsBool("-quiet", "Don't print any vecs", "ex12.c", options->quiet, &options->quiet, NULL);CHKERRQ(ierr);
ierr = PetscOptionsBool("-nonzero_initial_guess", "nonzero intial guess", "ex12.c", options->nonzInit, &options->nonzInit, NULL);CHKERRQ(ierr);
ierr = PetscOptionsBool("-bd_integral", "Compute the integral of the solution on the boundary", "ex12.c", options->bdIntegral, &options->bdIntegral, NULL);CHKERRQ(ierr);
if (options->runType == RUN_TEST) {
ierr = PetscOptionsBool("-run_test_check_ksp", "Check solution of KSP", "ex12.c", options->checkksp, &options->checkksp, NULL);CHKERRQ(ierr);
}
ierr = PetscOptionsString("-mesh", "Use box or xgc mesh", "ex12.c", options->mesh_type, options->mesh_type, sizeof(options->mesh_type), &flg);CHKERRQ(ierr);
ierr = PetscOptionsString("-picpart_path", "Specify picpart file path", "ex12.c", options->picpart_path, options->picpart_path, sizeof(options->picpart_path), &flg);CHKERRQ(ierr);
ierr = PetscOptionsEnd();
ierr = PetscLogEventRegister("CreateMesh", DM_CLASSID, &options->createMeshEvent);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
static PetscErrorCode CreateBCLabel(DM dm, const char name[])
{
DMLabel label;
PetscErrorCode ierr;
PetscFunctionBeginUser;
ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
ierr = DMPlexMarkBoundaryFaces(dm, 1, label);CHKERRQ(ierr);
ierr = DMPlexLabelComplete(dm, label);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
void getNeighborElmCounts(Omega_h::Mesh m, Omega_h::HostRead<Omega_h::LO>& nborRanks,
Omega_h::HostRead<Omega_h::LO>& nborElmCnts, bool debug=false) {
auto comm = m.comm();
const auto rank = comm->rank();
Omega_h::Dist d = m.ask_dist(0);
Omega_h::HostRead<Omega_h::LO> fRanks = d.items2ranks();
//get list of unique neighbors
std::unordered_set<int> uniqNborRanks;
//forward neibhors - defined by unowned boundary ents
for(int i=0; i<fRanks.size(); i++)
if( fRanks[i] != rank )
uniqNborRanks.insert(fRanks[i]);
auto dinv = d.invert();
Omega_h::HostRead<Omega_h::LO> rRanks = dinv.items2ranks();
//reverse neibhors - defined by owned boundary ents
for(int i=0; i<rRanks.size(); i++)
if( rRanks[i] != rank )
uniqNborRanks.insert(rRanks[i]);
//create an array of the neighbors
Omega_h::HostWrite<Omega_h::LO> destRanks(uniqNborRanks.size());
int i = 0;
for(const auto& nbor : uniqNborRanks)
destRanks[i++] = nbor;
Omega_h::Dist nbors;
nbors.set_parent_comm(m.comm());
//where we are sending
Omega_h::Read<int> destRanks_r(destRanks);
Omega_h::Read<int> destIdx_r(destRanks.size(), 0);
//what we are sending
Omega_h::Read<int> elmCnt(destRanks.size(), m.nelems());
nbors.set_dest_ranks(destRanks_r);
nbors.set_dest_idxs(destIdx_r,1);
nborElmCnts = nbors.exch(elmCnt,1);
nborRanks = nbors.msgs2ranks();
assert(nborRanks.size() == nborElmCnts.size());
if(debug) {
for (int r = 0; r < comm->size(); r++) {
if(rank == r) {
fprintf(stderr, "------%d------\n", rank);
for(auto i = 0; i < nborElmCnts.size(); i++)
fprintf(stderr, "%d:%d ", nborRanks[i], nborElmCnts[i]);
fprintf(stderr, "\n");
}
MPI_Barrier(MPI_COMM_WORLD);
}
MPI_Barrier(MPI_COMM_WORLD);
}
}
const int numVertsPerTri = 3;
//the vertex ids are local, but need to be mapped to a range of 0:numCoreVertices
//Should this use an adjacency based reordering? the xgc vertex
// numbering appears to have a specific pattern we may need to preserve
void getPicPartCoreElmToVtxArray(Omega_h::Mesh &mesh, const int rank, int& numCells,
Omega_h::LOs& partvtx2corevtx, Omega_h::HostRead<Omega_h::LO>& corecells2verts_hr) {
const auto ownership_elem_d = mesh.get_array<Omega_h::LO>(mesh.dim(), "ownership");
const auto isOwned = Omega_h::each_eq_to(ownership_elem_d,rank);
const auto partElm2CoreElm = Omega_h::offset_scan(isOwned);
numCells = partElm2CoreElm.last();
// Get the vertices to cell adjacency
auto partcells2verts = mesh.ask_elem_verts();
// Get the core of the picpart
const Omega_h::Write<Omega_h::LO> corecells2verts(numCells*numVertsPerTri,0);
const auto pv2cvSz = partvtx2corevtx.size();
const auto getCoreCells2Verts = OMEGA_H_LAMBDA(Omega_h::LO i) {
if( isOwned[i] ) {
for(int j=0; j<numVertsPerTri; j++) {
// Create local ids for the vertices in the core.
// The petsc documentation says that DMPlexBuildFromCellList requires vertices on
// the process to use 'global ids'.
// We normally define 'global ids' as a unique number for each vertex that is
// consistent across all processes.
// Petsc requires the vertices to be numbered from 0 to numVertices-1,
// where numVertices is the number of vertices owned by this process.
// Change the picpart local vertex ids to core local vertex ids.
const auto partVtxId = partcells2verts[i*3+j];
assert(partVtxId<pv2cvSz);
const auto coreVtxId = partvtx2corevtx[partVtxId];
assert(coreVtxId != -1);
const auto coreElmIdx = partElm2CoreElm[i];
assert(coreElmIdx < numCells);
corecells2verts[coreElmIdx*3+j] = coreVtxId;
}
}
};
Omega_h::parallel_for(ownership_elem_d.size(), getCoreCells2Verts);
Omega_h::HostRead<Omega_h::LO> cc2v_hr(corecells2verts);
corecells2verts_hr = cc2v_hr;
}
//petsc needs an array of vertex coordinates for all vertices in the core on
//this process
void getPicPartCoreVtxCoords(Omega_h::Mesh &mesh, const int rank,
int& numCoreVerts, int& numOwnedCoreVerts,
Omega_h::HostRead<Omega_h::Real>& coreVertexCoords,
Omega_h::LOs& partvtx2corevtx_rd) {
//read tag placed by pumipic that defines which process owns each vertex
const auto vtxOwnership_d = mesh.get_array<Omega_h::LO>(0, "ownership");
auto isOwned = Omega_h::each_eq_to(vtxOwnership_d,rank);
numOwnedCoreVerts = Omega_h::get_sum(isOwned);
assert(numOwnedCoreVerts <= mesh.nverts());
//for each element owned by this rank mark the vertices bound by it as owned
const Omega_h::Write<Omega_h::LO> isCoreVtx(mesh.nverts(),0);
const auto elms2verts_d = mesh.ask_elem_verts();
const auto elmOwnership_d = mesh.get_array<Omega_h::LO>(mesh.dim(), "ownership");
const auto markCoreVerts = OMEGA_H_LAMBDA(Omega_h::LO elm) {
if ( elmOwnership_d[elm] == rank ) {
for(int i=0; i<numVertsPerTri; i++) {
const auto vtxIdx = elms2verts_d[(elm*numVertsPerTri)+i];
isCoreVtx[vtxIdx] = 1;
}
}
};
Omega_h::parallel_for(mesh.nelems(), markCoreVerts);
Omega_h::LOs isCoreVtx_r(isCoreVtx);
const auto prefixSum = Omega_h::offset_scan(isCoreVtx_r);
numCoreVerts = prefixSum.last();
//create an array for the coordinates of vertices on this rank
const auto numPartVerts = mesh.nverts();
auto coords = mesh.coords();
Omega_h::Write<Omega_h::Real> coreVtxCoords_d(numCoreVerts*2);
Omega_h::Write<Omega_h::LO> vtxMap_wd(numPartVerts,-1);
Omega_h::Write<Omega_h::LO> vtxIdx(1,0);
const auto getCoordinatesAndMap = OMEGA_H_LAMBDA(Omega_h::LO vtx) {
if ( isCoreVtx[vtx] ) {
const auto idx = prefixSum[vtx];
if(vtx>=numPartVerts) printf("vtx %d numCoreVerts %d\n", vtx, numPartVerts);
assert(vtx<numPartVerts);
vtxMap_wd[vtx] = idx;
coreVtxCoords_d[idx*2] = coords[vtx*2];
coreVtxCoords_d[idx*2+1] = coords[vtx*2+1];
}
};
Omega_h::parallel_for(mesh.nverts(), getCoordinatesAndMap);
partvtx2corevtx_rd = vtxMap_wd;
//copy to host, set input arg references
Omega_h::HostRead<Omega_h::Real> hr(coreVtxCoords_d);
coreVertexCoords = hr;
}
template <class T>
void print(const int rank, std::string key, T arr_d) {
std::cerr << rank << " " << key << " " << arr_d << "\n";
}
// The boundary of the core needs to have links between the
// owner vertices and the non-owner vertices.
// These links are defined by the index of the owner vtx on the owning process
// being sent to all processes containing non-owner copies of that vtx.
// Note, the local index must be in the array that petsc gets for local
// vertices.
// rank (in) this process id in mpi_comm_world
// numCoreVerts (in) number of vertices in the core on this process/picpart
// numCoreOwnedVerts (in) number of owned vertices in the core on this process/picpart
// numCoreElems (in) number of elements in the core on this process/picpart
// partvtx2corevtx_rh (in) map from vertices in the picpart to their indices
// within the core,
// vtxMap[i] >= 0 if vtx i is in the core, -1 otherwise
// ghostOwnerRank_rh (inOut) owning rank for each core vertex
// ghostOwnerIdx_rh (inOut) index on the owning rank for each core vertex
void getPicPartCoreVtxOwnerIdx(Omega_h::Mesh &mesh, const int rank,
const int numCoreVerts, const int numCoreOwnedVerts, const int numCoreElms,
Omega_h::LOs partvtx2corevtx_rd,
Omega_h::HostRead<Omega_h::LO>& ghostOwnerLocIdx_rh,
Omega_h::HostRead<Omega_h::LO>& ghostOwnerRank_rh,
Omega_h::HostRead<Omega_h::LO>& ghostOwnerIdx_rh) {
const int numCoreGhostVerts = numCoreVerts - numCoreOwnedVerts;
assert(numCoreGhostVerts >=0);
const auto vtxOwner_d = mesh.get_array<Omega_h::LO>(0, "ownership");
const auto vtxGids_d = mesh.get_array<Omega_h::GO>(0, "gids");
Omega_h::Write<Omega_h::LO> ghostVtx2coreVtx_d(numCoreGhostVerts);
Omega_h::Write<Omega_h::GO> ghostVtxGid_d(numCoreGhostVerts);
Omega_h::Write<Omega_h::I32> ghostVtxOwner_d(numCoreGhostVerts);
Omega_h::Write<Omega_h::LO> cgMask(mesh.nverts(),0);
const auto createCoreGhostMask = OMEGA_H_LAMBDA(Omega_h::LO i) {
const auto isInCore = (partvtx2corevtx_rd[i] >= 0);
const auto isNotOwned = (vtxOwner_d[i] != rank);
cgMask[i] = (isInCore && isNotOwned);
};
Omega_h::parallel_for(mesh.nverts(), createCoreGhostMask);
Omega_h::LOs cgMask_r(cgMask);
const auto prefixSum = Omega_h::offset_scan(cgMask_r);
Omega_h::Write<Omega_h::LO> coreVtx2ghostVtx_d(numCoreVerts, -1);
const auto getGhostVtxInfo = OMEGA_H_LAMBDA(Omega_h::LO i) {
const auto coreIdx = partvtx2corevtx_rd[i];
const auto idx = prefixSum[i];
if ( cgMask_r[i] ) { // in the core and not owned
coreVtx2ghostVtx_d[coreIdx] = idx;
ghostVtx2coreVtx_d[idx] = coreIdx;
ghostVtxGid_d[idx] = vtxGids_d[i];
ghostVtxOwner_d[idx] = vtxOwner_d[i];
}
};
Omega_h::parallel_for(mesh.nverts(), getGhostVtxInfo);
Omega_h::Dist dist;
const auto worldComm = mesh.library()->world();
dist.set_parent_comm(worldComm);
Omega_h::GOs ghostVtxGid_rd(ghostVtxGid_d);
Omega_h::Read<Omega_h::I32> ghostVtxOwner_rd(ghostVtxOwner_d);
dist.set_dest_ranks(ghostVtxOwner_rd);
dist.set_dest_globals(ghostVtxGid_rd);
//non-owners send GID (and local idx) to owners - owners don't know
//which ranks have ghosts.
const auto inGid = dist.exch(ghostVtxGid_rd,1); //global id of vtx
const auto distInv = dist.invert();
const auto inRmts = distInv.items2dests();
const auto inRank = inRmts.ranks; //source rank of vtx info
//find the vertex with global id 'inGid' in the local core vertex array
//and send its local index to the remote process
Omega_h::Write<Omega_h::LO> ownerIdx_d(inGid.size());
const auto numGhostsReceived = inGid.size();
const auto findIdxOfGid = OMEGA_H_LAMBDA(Omega_h::LO i) {
const auto coreIdx = partvtx2corevtx_rd[i];
const auto vtxGid = vtxGids_d[i];
//look for ghosts with matching Gids, there is no race condition
//since each vtx can appear exactly once in the local picpart mesh
for(int j=0; j<numGhostsReceived; j++) {
//TODO move conditional outside the loop to minimize divergence
if ( vtxGid == inGid[j] ) {
assert(coreIdx>=0);
ownerIdx_d[j] = numCoreElms + coreIdx; //PETSC_NEEDS_4A
}
}
};
Omega_h::parallel_for(mesh.nverts(), findIdxOfGid);
Omega_h::LOs ownerIdx_rd(ownerIdx_d);
//send local index to remote process
const auto ghostOwnerIdx_rd = distInv.exch(ownerIdx_rd,1);
//move arrays to host
{
Omega_h::HostRead<Omega_h::LO> rh(ghostOwnerIdx_rd);
ghostOwnerIdx_rh = rh;
}
{
Omega_h::HostRead<Omega_h::LO> rh(ghostVtxOwner_d);
ghostOwnerRank_rh = rh;
}
{
Omega_h::HostRead<Omega_h::LO> rh(ghostVtx2coreVtx_d);
ghostOwnerLocIdx_rh = rh;
}
}
void getPtnMeshElmToVtxArray(Omega_h::Mesh &mesh, std::vector<int>& global_cell) {
const int numCells = mesh.nelems();
// Get the vertices to cell adjacency
Omega_h::HostRead<Omega_h::LO> cell(mesh.ask_elem_verts());
assert(cell.size() == numVertsPerTri*numCells);
// Change the local to global vertex id for adjacency
for (int i = 0; i < cell.size(); i++)
{
global_cell.push_back(cell[i]);
}
}
static PetscErrorCode CreateQuadMesh(MPI_Comm comm, DM *dm, AppCtx *options, Omega_h::Library lib)
{
assert(options->dim == 2);
auto mesh = Omega_h::Mesh(&lib);
int rank, commSize;
MPI_Comm_rank(comm, &rank);
MPI_Comm_size(comm, &commSize);
if (strcmp(options->mesh_type, "box") == 0)
{
mesh = Omega_h::build_box(lib.world(), OMEGA_H_SIMPLEX, 1., 1., 0,
options->cells[0], options->cells[1], 0);
}
else if (strcmp(options->mesh_type, "picpart") == 0)
{
Omega_h::filesystem::path file_path = options->picpart_path;
file_path += std::to_string(rank);
file_path += ".osh";
Omega_h::binary::read(file_path, lib.self(), &mesh);
MPI_Barrier(MPI_COMM_WORLD);
}
else
{
Omega_h::binary::read(options->mesh_type, lib.world(), &mesh, false);
mesh.balance();
}
PetscErrorCode ierr;
const int dim = mesh.dim();
int numVertices; //TODO 'ptscNumVerts'
int numOwnedVertices; //TODO 'ptscNumOwnedVerts'
int numVerticesGhost; //TODO 'ptscNumRmtVerts'
int numGlobalVerts; //TODO 'ptscNumGlobVerts'
int numCells; //TODO 'ptscNumCells'
Omega_h::HostRead<Omega_h::Real> vertexCoords; //TODO 'ptscVtxCoords'
if (strcmp(options->mesh_type, "picpart") == 0)
{
//PETSC_NEEDS_1 - 'vertexCoords'
int numCoreVerts;
int numOwnedCoreVerts;
Omega_h::LOs partvtx2corevtx_rd;
getPicPartCoreVtxCoords(mesh, rank, numCoreVerts, numOwnedCoreVerts, vertexCoords, partvtx2corevtx_rd);
int numCoreElms;
Omega_h::HostRead<Omega_h::LO> cells2verts;
getPicPartCoreElmToVtxArray(mesh, rank, numCoreElms, partvtx2corevtx_rd, cells2verts);
assert(numCoreElms);
numCells = numCoreElms;
numVertices = numCoreVerts;
numOwnedVertices = numOwnedCoreVerts;
MPI_Allreduce(&numOwnedVertices, &numGlobalVerts, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
//PETSC_NEEDS_4A - 'vtxRemoteIdx'
Omega_h::HostRead<Omega_h::LO> ghostOwnerLocIdx_rh;
Omega_h::HostRead<Omega_h::LO> ghostOwnerRank_rh;
Omega_h::HostRead<Omega_h::LO> ghostOwnerIdx_rh;
const int numCoreRmtVtx = numCoreVerts - numOwnedCoreVerts;
numVerticesGhost = numCoreRmtVtx;
getPicPartCoreVtxOwnerIdx(mesh, rank, numCoreVerts, numOwnedCoreVerts, numCoreElms,
partvtx2corevtx_rd, ghostOwnerLocIdx_rh, ghostOwnerRank_rh, ghostOwnerIdx_rh);
if(numCoreRmtVtx > 0) {
assert(ghostOwnerRank_rh.size());
assert(ghostOwnerIdx_rh.size());
}
//PETSC_NEEDS_4B - 'vtxRemoteRank'
//create a plex object on each process from local info
ierr = DMPlexCreateFromCellListPetsc(comm, dim, numCoreElms,
numCoreVerts, numVertsPerTri, PETSC_FALSE, cells2verts.data(),
dim, vertexCoords.data(), dm); CHKERRQ(ierr);
PetscInt *localVertex;
ierr = PetscMalloc1(numCoreRmtVtx, &localVertex);CHKERRQ(ierr);
PetscSFNode *remoteVertex;
ierr = PetscMalloc1(numCoreRmtVtx, &remoteVertex);CHKERRQ(ierr);
for(int i=0; i<numCoreRmtVtx; i++) {
localVertex[i] = numCoreElms+ghostOwnerLocIdx_rh[i];
remoteVertex[i].rank = ghostOwnerRank_rh[i];
remoteVertex[i].index = ghostOwnerIdx_rh[i];
}
PetscSF pointSF;
ierr = DMGetPointSF(*dm, &pointSF);CHKERRQ(ierr);
ierr = PetscSFSetGraph(pointSF, numCoreElms+numCoreVerts, numCoreRmtVtx,
localVertex, PETSC_OWN_POINTER, remoteVertex, PETSC_OWN_POINTER);CHKERRQ(ierr);
ierr = DMSetFromOptions(*dm);CHKERRQ(ierr); //perform mesh checks
ierr = DMViewFromOptions(*dm, NULL, "-dm_view");CHKERRQ(ierr);
if(false) {
PetscSFView(pointSF, PETSC_VIEWER_STDOUT_WORLD);
}
}
else { //partitioned omegah mesh
Omega_h::HostRead<int> nborRanks;
Omega_h::HostRead<int> nborElmCnts;
std::vector<int> cells2verts;
getPtnMeshElmToVtxArray(mesh, cells2verts);
Omega_h::HostRead<Omega_h::LO> ownership_vert = mesh.ask_owners(0).ranks;
numOwnedVertices = std::count(ownership_vert.data(), ownership_vert.data()+ownership_vert.size(), rank);
numGlobalVerts = mesh.nglobal_ents(0);
numVertices = mesh.nverts();
numCells = mesh.nelems();
vertexCoords = mesh.coords();
getNeighborElmCounts(mesh, nborRanks, nborElmCnts, false);
assert( nborRanks.size() > 0 &&
(nborRanks.size() == nborElmCnts.size()) );
Omega_h::HostRead<Omega_h::I32> vtxRemoteRank; //TODO 'ptscVtxRmtRank'
vtxRemoteRank = mesh.ask_owners(0).ranks;
Omega_h::HostRead<Omega_h::LO> vtxRemoteIdx; //TODO 'ptscVtxRmtIdx'
vtxRemoteIdx = mesh.ask_owners(0).idxs;
//PETSC_NEEDS_2 - 'numVerticesGhost'
int numVerticesGhost = 0; //vertices that are not owned
for (int i = 0; i < vtxRemoteRank.size(); i++) {
if (rank != vtxRemoteRank[i])
numVerticesGhost++;
}
typedef std::map<int,int> Mi2i;
Mi2i nbor2ElmCnt;
for(int i = 0; i < nborRanks.size(); i++)
nbor2ElmCnt[nborRanks[i]] = nborElmCnts[i];
//PETSC_NEEDS_3 - 'localVertex'
int *localVertex;
PetscSFNode *remoteVertex;
ierr = PetscMalloc1(numVerticesGhost, &localVertex);CHKERRQ(ierr);
ierr = PetscMalloc1(numVerticesGhost, &remoteVertex);CHKERRQ(ierr);
for (int i = 0, j = 0; i < vtxRemoteRank.size(); i++)
{
const auto nborRank= vtxRemoteRank[i];
if (rank != nborRank)
{
localVertex[j] = numCells+i;
const auto nborElmCnt = nbor2ElmCnt[nborRank];
remoteVertex[j].index = vtxRemoteIdx[i]+nborElmCnt;
remoteVertex[j].rank = nborRank;
j++;
}
}
//create a plex object on each process from local info
ierr = DMPlexCreateFromCellListPetsc(comm, dim, numCells,
numVertices, numVertsPerTri, PETSC_FALSE, cells2verts.data(),
dim, vertexCoords.data(), dm); CHKERRQ(ierr);
PetscSF pointSF;
ierr = DMGetPointSF(*dm, &pointSF);CHKERRQ(ierr);
ierr = PetscSFSetGraph(pointSF, numCells+numVertices, numVerticesGhost, localVertex, PETSC_OWN_POINTER, remoteVertex, PETSC_OWN_POINTER);CHKERRQ(ierr);
if(false) {
PetscSFView(pointSF, PETSC_VIEWER_STDOUT_WORLD);
}
} //end partitioned omegah mesh
assert(vertexCoords.size() == dim*numVertices);
const auto debug = options->debug;
if(debug) {
for (int i = 0; i < commSize; i++) {
if(rank == i) {
std::cerr << rank << " numCells: " << numCells << " numVertices: " <<
numVertices << " numVerticesNotOwned: " << numVerticesGhost << "\n";
}
MPI_Barrier(comm);
}
}
DM dm_int;
ierr = DMPlexInterpolate(*dm, &dm_int);
ierr = DMDestroy(dm);CHKERRQ(ierr);
*dm = dm_int;
ierr = DMSetFromOptions(*dm);CHKERRQ(ierr); //perform mesh checks
ierr = DMViewFromOptions(*dm, NULL, "-dm_view");CHKERRQ(ierr);
// Get the starting and ending index for the topology
PetscInt cStart, cEnd, vStart, vEnd, eStart, eEnd;
ierr = DMPlexGetHeightStratum(*dm, 0, &cStart, &cEnd); /* cells */
ierr = DMPlexGetHeightStratum(*dm, 1, &eStart, &eEnd); /* edges */
ierr = DMPlexGetHeightStratum(*dm, 2, &vStart, &vEnd); /* vertices */
if(debug) {
for (int i = 0; i < commSize; i++) {
if(rank == i) {
if(!rank)
std::cerr << "<rank> <quant> <petsc count:omega count>\n";
std::cerr << rank << " numCells " << cEnd-cStart << ":" << numCells << " numVerts "
<< vEnd-vStart << ":" << numVertices << "\n";
}
MPI_Barrier(comm);
}
}
/*
Iterate through all the edges and then check if each edge is shared by two different cells by
using DMPlexGetSupportSize. It is a boundary edge if the edge exist in only one cell.
*/
std::vector<int> boundary_edge;
for (int i = eStart; i < eEnd; i++)
{
int support_size;
ierr = DMPlexGetSupportSize(*dm, i, &support_size);CHKERRQ(ierr);
if (support_size == 1)
{
boundary_edge.push_back(i);
}
}
// By using a set, the vertices for all the boundary edges would not repeat
// Can be replaced with an unordered_set