-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathsetup.c
841 lines (749 loc) · 33.6 KB
/
setup.c
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
// Copyright (c) 2017, Lawrence Livermore National Security, LLC. Produced at
// the Lawrence Livermore National Laboratory. LLNL-CODE-734707. All Rights
// reserved. See files LICENSE and NOTICE for details.
//
// This file is part of CEED, a collection of benchmarks, miniapps, software
// libraries and APIs for efficient high-order finite element and spectral
// element discretizations for exascale applications. For more information and
// source code availability see http://github.com/ceed.
//
// The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
// a collaborative effort of two U.S. Department of Energy organizations (Office
// of Science and the National Nuclear Security Administration) responsible for
// the planning and preparation of a capable exascale ecosystem, including
// software, applications, hardware, advanced system engineering and early
// testbed platforms, in support of the nation's exascale computing imperative.
/// @file
/// Helper setup functions for shallow-water example using PETSc
#include <stdbool.h>
#include <string.h>
#include <petsc.h>
#include <petscsys.h>
#include <petscdmplex.h>
#include <petscfe.h>
#include <ceed.h>
#include "../sw_headers.h" // Function prototytes
#include "../qfunctions/setup_geo.h" // Geometric factors
#include "../qfunctions/advection.h" // Physics point-wise functions
#include "../qfunctions/geostrophic.h" // Physics point-wise functions
#if PETSC_VERSION_LT(3,14,0)
# define DMPlexGetClosureIndices(a,b,c,d,e,f,g,h,i) DMPlexGetClosureIndices(a,b,c,d,f,g,i)
# define DMPlexRestoreClosureIndices(a,b,c,d,e,f,g,h,i) DMPlexRestoreClosureIndices(a,b,c,d,f,g,i)
#endif
#if PETSC_VERSION_LT(3,14,0)
# define DMPlexCreateSphereMesh(a,b,c,d,e) DMPlexCreateSphereMesh(a,b,c,e)
#endif
problemData problemOptions[] = {
[SWE_ADVECTION] = {
.topodim = 2,
.qdatasize = 11,
.setup = SetupGeo,
.setup_loc = SetupGeo_loc,
.ics = ICsSW_Advection,
.ics_loc = ICsSW_Advection_loc,
.apply_explfunction = SWExplicit_Advection,
.apply_explfunction_loc = SWExplicit_Advection_loc,
.apply_implfunction = SWImplicit_Advection,
.apply_implfunction_loc = SWImplicit_Advection_loc,
.apply_jacobian = SWJacobian_Advection,
.apply_jacobian_loc = SWJacobian_Advection_loc,
.non_zero_time = PETSC_TRUE
},
[SWE_GEOSTROPHIC] = {
.topodim = 2,
.qdatasize = 11,
.setup = SetupGeo,
.setup_loc = SetupGeo_loc,
.ics = ICsSW,
.ics_loc = ICsSW_loc,
.apply_explfunction = SWExplicit,
.apply_explfunction_loc = SWExplicit_loc,
.apply_implfunction = SWImplicit,
.apply_implfunction_loc = SWImplicit_loc,
.apply_jacobian = SWJacobian,
.apply_jacobian_loc = SWJacobian_loc,
.non_zero_time = PETSC_FALSE
}
};
// -----------------------------------------------------------------------------
// Auxiliary function to determine if nodes belong to cube faces (panels)
// -----------------------------------------------------------------------------
PetscErrorCode FindPanelEdgeNodes(DM dm, PhysicsContext phys_ctx,
PetscInt ncomp, PetscInt degree,
PetscInt topodim, PetscInt *edgenodecnt,
EdgeNode *edgenodes, Mat *T) {
PetscInt ierr;
MPI_Comm comm;
PetscInt cstart, cend, gdofs;
PetscSection section, sectionloc;
PetscFunctionBeginUser;
// Get Nelem
ierr = DMGetGlobalSection(dm, §ion); CHKERRQ(ierr);
ierr = DMGetLocalSection(dm, §ionloc); CHKERRQ(ierr);
ierr = DMPlexGetHeightStratum(dm, 0, &cstart, &cend); CHKERRQ(ierr);
PetscSF sf;
Vec bitmapVec;
ierr = DMGetSectionSF(dm, &sf); CHKERRQ(ierr);
ierr = DMCreateGlobalVector(dm, &bitmapVec); CHKERRQ(ierr);
ierr = VecGetSize(bitmapVec, &gdofs); CHKERRQ(ierr);
ierr = VecDestroy(&bitmapVec);
// Arrays for local and global bitmaps
unsigned int *bitmaploc;
unsigned int *bitmap;
ierr = PetscCalloc2(gdofs, &bitmaploc, gdofs, &bitmap);
CHKERRQ(ierr);
// Get indices
for (PetscInt c = cstart; c < cend; c++) { // Traverse elements
PetscInt numindices, *indices, n, panel;
// Query element panel
ierr = DMGetLabelValue(dm, "panel", c, &panel); CHKERRQ(ierr);
ierr = DMPlexGetClosureIndices(dm, sectionloc, section, c, PETSC_TRUE,
&numindices, &indices, NULL, NULL);
CHKERRQ(ierr);
for (n = 0; n < numindices; n += ncomp) { // Traverse nodes per element
PetscInt bitmapidx = indices[n];
bitmaploc[bitmapidx] |= (1 << panel);
}
ierr = DMPlexRestoreClosureIndices(dm, sectionloc, section, c, PETSC_TRUE,
&numindices, &indices, NULL, NULL);
CHKERRQ(ierr);
}
// Reduce from all ranks
PetscObjectGetComm((PetscObject)dm, &comm); CHKERRQ(ierr);
MPI_Reduce(bitmaploc, bitmap, gdofs, MPI_UNSIGNED, MPI_BOR, 0, comm);
// Read the resulting bitmap and extract edge nodes only
ierr = PetscMalloc1(gdofs + 24*ncomp, edgenodes); CHKERRQ(ierr);
for (PetscInt i = 0; i < gdofs; i += ncomp) {
PetscInt ones = 0, panels[3];
for (PetscInt p = 0; p < 6; p++) {
if (bitmap[i] & 1)
panels[ones++] = p;
bitmap[i] >>= 1;
}
if (ones == 2) {
(*edgenodes)[*edgenodecnt].idx = i;
(*edgenodes)[*edgenodecnt].panelA = panels[0];
(*edgenodes)[*edgenodecnt].panelB = panels[1];
(*edgenodecnt)++;
}
else if (ones == 3) {
(*edgenodes)[*edgenodecnt].idx = i;
(*edgenodes)[*edgenodecnt].panelA = panels[0];
(*edgenodes)[*edgenodecnt].panelB = panels[1];
(*edgenodecnt)++;
(*edgenodes)[*edgenodecnt].idx = i;
(*edgenodes)[*edgenodecnt].panelA = panels[0];
(*edgenodes)[*edgenodecnt].panelB = panels[2];
(*edgenodecnt)++;
(*edgenodes)[*edgenodecnt].idx = i;
(*edgenodes)[*edgenodecnt].panelA = panels[1];
(*edgenodes)[*edgenodecnt].panelB = panels[2];
(*edgenodecnt)++;
}
}
ierr = SetupRestrictionMatrix(dm, phys_ctx, degree, ncomp, *edgenodes,
*edgenodecnt, T); CHKERRQ(ierr);
// Free heap
ierr = PetscFree2(bitmaploc, bitmap); CHKERRQ(ierr);
PetscFunctionReturn(0);
}
// -----------------------------------------------------------------------------
// Auxiliary function that sets up all coordinate transformations between panels
// -----------------------------------------------------------------------------
PetscErrorCode SetupRestrictionMatrix(DM dm, PhysicsContext phys_ctx,
PetscInt degree, PetscInt ncomp,
EdgeNode edgenodes, PetscInt nedgenodes,
Mat *T) {
PetscInt ierr, nnodes, c, cStart, cEnd, nelem, numP, gdofs;
MPI_Comm comm;
Vec X;
PetscSection section;
const PetscScalar *xarray;
PetscFunctionBeginUser;
ierr = PetscObjectGetComm((PetscObject)dm, &comm); CHKERRQ(ierr);
ierr = DMGetSection(dm, §ion); CHKERRQ(ierr);
ierr = DMGetCoordinates(dm, &X); CHKERRQ(ierr);
ierr = VecGetSize(X, &gdofs); CHKERRQ(ierr);
nnodes = gdofs/ncomp;
ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd); CHKERRQ(ierr);
nelem = cEnd - cStart;
numP = degree + 1;
// Preallocate sparse matrix
ierr = MatCreateAIJ(comm, nelem*numP*numP*ncomp, nnodes*ncomp,
PETSC_DETERMINE, PETSC_DETERMINE, 2, NULL,
nnodes*ncomp, NULL, T); CHKERRQ(ierr);
// Loop over elements
for (c = cStart; c < cEnd; c++) {
PetscInt numindices, *indices, i;
ierr = DMPlexGetClosureIndices(dm, section, section, c, PETSC_TRUE,
&numindices, &indices, NULL, NULL);
CHKERRQ(ierr);
for (i = 0; i < numindices; i += ncomp) {
for (PetscInt j = 0; j < ncomp; j++) {
if (indices[i+j] != indices[i] + (PetscInt)(copysign(j, indices[i])))
SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP,
"Cell %D closure indices not interlaced", c);
}
// NO BC on closed surfaces
PetscInt loc = indices[i];
// Query element panel
PetscInt panel;
ierr = DMGetLabelValue(dm, "panel", c, &panel); CHKERRQ(ierr);
// Query if edge node
PetscBool isEdgeNode = PETSC_FALSE;
for (PetscInt e = 0; e < nedgenodes; e++) {
if (edgenodes[e].idx == loc) {
isEdgeNode = PETSC_TRUE;
break;
}
}
PetscScalar entries[9];
if (isEdgeNode) {
ierr = VecGetArrayRead(X, &xarray); CHKERRQ(ierr);
PetscScalar R = phys_ctx->R;
// Read global Cartesian coordinates
PetscScalar x[3] = {xarray[loc + 0],
xarray[loc + 1],
xarray[loc + 2]
};
// Restore array read
ierr = VecRestoreArrayRead(X, &xarray); CHKERRQ(ierr);
// Normalize quadrature point coordinates to sphere
PetscScalar rad = sqrt(x[0]*x[0] + x[1]*x[1] + x[2]*x[2]);
x[0] *= R / rad;
x[1] *= R / rad;
x[2] *= R / rad;
// Compute latitude and longitude
const PetscScalar theta = asin(x[2] / R); // latitude
const PetscScalar lambda = atan2(x[1], x[0]); // longitude
PetscScalar T00, T01, T10, T11;
switch (panel) {
case 0:
case 1:
case 2:
case 3:
// For P_0 (front), P_1 (east), P_2 (back), P_3 (west):
T00 = cos(theta)*cos(lambda) * cos(lambda);
T01 = cos(theta)*cos(lambda) * 0.;
T10 = cos(theta)*cos(lambda) * -sin(theta)*sin(lambda);
T11 = cos(theta)*cos(lambda) * cos(theta);
break;
case 4:
// For P4 (north):
T00 = sin(theta) * cos(lambda);
T01 = sin(theta) * sin(lambda);
T10 = sin(theta) * -sin(theta)*sin(lambda);
T11 = sin(theta) * sin(theta)*cos(lambda);
break;
case 5:
// For P5 (south):
T00 = sin(theta) * (-cos(theta));
T01 = sin(theta) * sin(lambda);
T10 = sin(theta) * sin(theta)*sin(lambda);
T11 = sin(theta) * sin(theta)*cos(lambda);
break;
}
entries[0] = T00;
entries[1] = T01;
entries[2] = 0.;
entries[3] = T10;
entries[4] = T11;
entries[5] = 0.;
entries[6] = 0.;
entries[7] = 0.;
entries[8] = 1.;
} else {
entries[0] = 1.;
entries[1] = 0.;
entries[2] = 0.;
entries[3] = 0.;
entries[4] = 1.;
entries[5] = 0.;
entries[6] = 0.;
entries[7] = 0.;
entries[8] = 1.;
}
PetscInt elem = c - cStart;
PetscInt idxrow[3] = {elem*loc + 0, elem*loc + 1, elem*loc + 2};
PetscInt idxcol[3] = {loc + 0, loc + 1, loc + 2};
ierr = MatSetValues(*T, ncomp, idxrow, ncomp, idxcol, entries,
INSERT_VALUES); CHKERRQ(ierr);
}
ierr = DMPlexRestoreClosureIndices(dm, section, section, c, PETSC_TRUE,
&numindices, &indices, NULL, NULL);
CHKERRQ(ierr);
}
// Assemble matrix for all node transformations
ierr = MatAssemblyBegin(*T, MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
ierr = MatAssemblyEnd(*T, MAT_FINAL_ASSEMBLY); CHKERRQ(ierr);
PetscFunctionReturn(0);
}
// -----------------------------------------------------------------------------
// Auxiliary function that converts global 3D coors into local panel coords
// -----------------------------------------------------------------------------
PetscErrorCode TransformCoords(DM dm, Vec Xloc, const PetscInt ncompx,
EdgeNode edgenodes, const PetscInt nedgenodes,
PhysicsContext phys_ctx, Vec *Xpanelsloc) {
PetscInt ierr;
PetscInt lsize, depth, nstart, nend;
const PetscScalar *xlocarray;
PetscScalar *xpanelslocarray;
PetscFunctionBeginUser;
ierr = VecGetArrayRead(Xloc, &xlocarray); CHKERRQ(ierr);
ierr = VecGetSize(Xloc, &lsize); CHKERRQ(ierr);
ierr = VecGetArray(*Xpanelsloc, &xpanelslocarray); CHKERRQ(ierr);
ierr = DMPlexGetDepth(dm, &depth); CHKERRQ(ierr);
ierr = DMPlexGetHeightStratum(dm, depth, &nstart, &nend); CHKERRQ(ierr);
for (PetscInt n = 0; n < (nend - nstart); n++) { // Traverse nodes
// Query element panel
PetscInt panel;
PetscSection sectionloc;
ierr = DMGetLocalSection(dm, §ionloc); CHKERRQ(ierr);
ierr = DMGetLabelValue(dm, "panel", n + nstart, &panel); CHKERRQ(ierr);
PetscScalar X = xlocarray[n*ncompx+0];
PetscScalar Y = xlocarray[n*ncompx+1];
PetscScalar Z = xlocarray[n*ncompx+2];
// Normalize quadrature point coordinates to sphere
CeedScalar rad = sqrt(X*X + Y*Y + Z*Z);
X *= phys_ctx->R / rad;
Y *= phys_ctx->R / rad;
Z *= phys_ctx->R / rad;
PetscScalar x, y;
PetscScalar l = phys_ctx->R/sqrt(3.);
PetscBool isedgenode = PETSC_FALSE;
// Determine if this node is an edge node
for (PetscInt e = 0; e < nedgenodes; e++) {
if (n * ncompx == edgenodes[e].idx) {
isedgenode = PETSC_TRUE; break;
}
}
if (isedgenode) {
// Compute latitude and longitude
const CeedScalar theta = asin(Z / phys_ctx->R); // latitude
const CeedScalar lambda = atan2(Y, X); // longitude
xpanelslocarray[n*ncompx+0] = theta;
xpanelslocarray[n*ncompx+1] = lambda;
xpanelslocarray[n*ncompx+2] = -1;
}
else {
switch (panel) {
case 0:
x = l * (Y / X);
y = l * (Z / X);
break;
case 1:
x = l * (-X / Y);
y = l * (Z / Y);
break;
case 2:
x = l * (Y / X);
y = l * (-Z / X);
break;
case 3:
x = l * (-X / Y);
y = l * (-Z / Y);
break;
case 4:
x = l * (Y / Z);
y = l * (-X / Z);
break;
case 5:
x = l * (-Y / Z);
y = l * (-X / Z);
break;
}
xpanelslocarray[n*ncompx+0] = x;
xpanelslocarray[n*ncompx+1] = y;
xpanelslocarray[n*ncompx+2] = panel;
}
} // End of nodes for loop
ierr = VecRestoreArrayRead(Xloc, &xlocarray); CHKERRQ(ierr);
ierr = VecRestoreArray(*Xpanelsloc, &xpanelslocarray); CHKERRQ(ierr);
PetscFunctionReturn(0);
}
// -----------------------------------------------------------------------------
// Auxiliary function to create PETSc FE space for a given degree
// -----------------------------------------------------------------------------
PetscErrorCode PetscFECreateByDegree(DM dm, PetscInt dim, PetscInt Nc,
PetscBool isSimplex,
const char prefix[],
PetscInt order, PetscFE *fem) {
PetscQuadrature q, fq;
DM K;
PetscSpace P;
PetscDualSpace Q;
PetscInt quadPointsPerEdge;
PetscBool tensor = isSimplex ? PETSC_FALSE : PETSC_TRUE;
PetscErrorCode ierr;
PetscFunctionBeginUser;
/* Create space */
ierr = PetscSpaceCreate(PetscObjectComm((PetscObject) dm), &P); CHKERRQ(ierr);
ierr = PetscObjectSetOptionsPrefix((PetscObject) P, prefix); CHKERRQ(ierr);
ierr = PetscSpacePolynomialSetTensor(P, tensor); CHKERRQ(ierr);
ierr = PetscSpaceSetFromOptions(P); CHKERRQ(ierr);
ierr = PetscSpaceSetNumComponents(P, Nc); CHKERRQ(ierr);
ierr = PetscSpaceSetNumVariables(P, dim); CHKERRQ(ierr);
ierr = PetscSpaceSetDegree(P, order, order); CHKERRQ(ierr);
ierr = PetscSpaceSetUp(P); CHKERRQ(ierr);
ierr = PetscSpacePolynomialGetTensor(P, &tensor); CHKERRQ(ierr);
/* Create dual space */
ierr = PetscDualSpaceCreate(PetscObjectComm((PetscObject) dm), &Q);
CHKERRQ(ierr);
ierr = PetscDualSpaceSetType(Q,PETSCDUALSPACELAGRANGE); CHKERRQ(ierr);
ierr = PetscObjectSetOptionsPrefix((PetscObject) Q, prefix); CHKERRQ(ierr);
ierr = PetscDualSpaceCreateReferenceCell(Q, dim, isSimplex, &K); CHKERRQ(ierr);
ierr = PetscDualSpaceSetDM(Q, K); CHKERRQ(ierr);
ierr = DMDestroy(&K); CHKERRQ(ierr);
ierr = PetscDualSpaceSetNumComponents(Q, Nc); CHKERRQ(ierr);
ierr = PetscDualSpaceSetOrder(Q, order); CHKERRQ(ierr);
ierr = PetscDualSpaceLagrangeSetTensor(Q, tensor); CHKERRQ(ierr);
ierr = PetscDualSpaceSetFromOptions(Q); CHKERRQ(ierr);
ierr = PetscDualSpaceSetUp(Q); CHKERRQ(ierr);
/* Create element */
ierr = PetscFECreate(PetscObjectComm((PetscObject) dm), fem); CHKERRQ(ierr);
ierr = PetscObjectSetOptionsPrefix((PetscObject) *fem, prefix); CHKERRQ(ierr);
ierr = PetscFESetFromOptions(*fem); CHKERRQ(ierr);
ierr = PetscFESetBasisSpace(*fem, P); CHKERRQ(ierr);
ierr = PetscFESetDualSpace(*fem, Q); CHKERRQ(ierr);
ierr = PetscFESetNumComponents(*fem, Nc); CHKERRQ(ierr);
ierr = PetscFESetUp(*fem); CHKERRQ(ierr);
ierr = PetscSpaceDestroy(&P); CHKERRQ(ierr);
ierr = PetscDualSpaceDestroy(&Q); CHKERRQ(ierr);
/* Create quadrature */
quadPointsPerEdge = PetscMax(order + 1,1);
if (isSimplex) {
ierr = PetscDTStroudConicalQuadrature(dim, 1, quadPointsPerEdge, -1.0, 1.0,
&q); CHKERRQ(ierr);
ierr = PetscDTStroudConicalQuadrature(dim-1, 1, quadPointsPerEdge, -1.0, 1.0,
&fq); CHKERRQ(ierr);
} else {
ierr = PetscDTGaussTensorQuadrature(dim, 1, quadPointsPerEdge, -1.0, 1.0,
&q); CHKERRQ(ierr);
ierr = PetscDTGaussTensorQuadrature(dim-1, 1, quadPointsPerEdge, -1.0, 1.0,
&fq); CHKERRQ(ierr);
}
ierr = PetscFESetQuadrature(*fem, q); CHKERRQ(ierr);
ierr = PetscFESetFaceQuadrature(*fem, fq); CHKERRQ(ierr);
ierr = PetscQuadratureDestroy(&q); CHKERRQ(ierr);
ierr = PetscQuadratureDestroy(&fq); CHKERRQ(ierr);
PetscFunctionReturn(0);
}
// -----------------------------------------------------------------------------
// Auxiliary function to setup DM FE space and info
// -----------------------------------------------------------------------------
PetscErrorCode SetupDMByDegree(DM dm, PetscInt degree, PetscInt ncomp,
PetscInt dim) {
PetscErrorCode ierr;
PetscFunctionBeginUser;
{
// Configure the finite element space
PetscFE fe;
ierr = PetscFECreateByDegree(dm, dim, ncomp, PETSC_FALSE, NULL, degree,
&fe);
ierr = PetscObjectSetName((PetscObject)fe, "Q"); CHKERRQ(ierr);
ierr = DMAddField(dm, NULL, (PetscObject)fe); CHKERRQ(ierr);
ierr = DMCreateDS(dm); CHKERRQ(ierr);
ierr = DMPlexSetClosurePermutationTensor(dm, PETSC_DETERMINE, NULL);
CHKERRQ(ierr);
ierr = PetscFEDestroy(&fe); CHKERRQ(ierr);
}
{
// Empty name for conserved field (because there is only one field)
PetscSection section;
ierr = DMGetLocalSection(dm, §ion); CHKERRQ(ierr);
ierr = PetscSectionSetFieldName(section, 0, ""); CHKERRQ(ierr);
ierr = PetscSectionSetComponentName(section, 0, 0, "u_lambda");
CHKERRQ(ierr);
ierr = PetscSectionSetComponentName(section, 0, 1, "u_theta");
CHKERRQ(ierr);
ierr = PetscSectionSetComponentName(section, 0, 2, "h");
CHKERRQ(ierr);
}
PetscFunctionReturn(0);
}
// -----------------------------------------------------------------------------
// PETSc sphere auxiliary function
// -----------------------------------------------------------------------------
// Utility function taken from petsc/src/dm/impls/plex/tutorials/ex7.c
PetscErrorCode ProjectToUnitSphere(DM dm) {
Vec coordinates;
PetscScalar *coords;
PetscInt Nv, v, dim, d;
PetscErrorCode ierr;
PetscFunctionBeginUser;
ierr = DMGetCoordinatesLocal(dm, &coordinates); CHKERRQ(ierr);
ierr = VecGetLocalSize(coordinates, &Nv); CHKERRQ(ierr);
ierr = VecGetBlockSize(coordinates, &dim); CHKERRQ(ierr);
Nv /= dim;
ierr = VecGetArray(coordinates, &coords); CHKERRQ(ierr);
for (v = 0; v < Nv; ++v) {
PetscReal r = 0.0;
for (d = 0; d < dim; ++d) r += PetscSqr(PetscRealPart(coords[v*dim+d]));
r = PetscSqrtReal(r);
for (d = 0; d < dim; ++d) coords[v*dim+d] /= r;
}
ierr = VecRestoreArray(coordinates, &coords); CHKERRQ(ierr);
PetscFunctionReturn(0);
}
// -----------------------------------------------------------------------------
// Auxiliary function to define CEED restrictions from DMPlex data
// -----------------------------------------------------------------------------
PetscErrorCode CreateRestrictionPlex(Ceed ceed, DM dm, CeedInt P,
CeedInt ncomp,
CeedElemRestriction *Erestrict) {
PetscInt ierr;
PetscInt c, cStart, cEnd, nelem, nnodes, *erestrict, eoffset;
PetscSection section;
Vec Uloc;
PetscFunctionBegin;
// Get Nelem
ierr = DMGetSection(dm, §ion); CHKERRQ(ierr);
ierr = DMPlexGetHeightStratum(dm, 0, &cStart,& cEnd); CHKERRQ(ierr);
nelem = cEnd - cStart;
// Get indices
ierr = PetscMalloc1(nelem*P*P, &erestrict); CHKERRQ(ierr);
for (c = cStart, eoffset = 0; c < cEnd; c++) {
PetscInt numindices, *indices, i;
ierr = DMPlexGetClosureIndices(dm, section, section, c, PETSC_TRUE,
&numindices, &indices, NULL, NULL);
CHKERRQ(ierr);
for (i = 0; i < numindices; i += ncomp) {
for (PetscInt j = 0; j < ncomp; j++) {
if (indices[i+j] != indices[i] + (PetscInt)(copysign(j, indices[i])))
SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP,
"Cell %D closure indices not interlaced", c);
}
// NO BC on closed surfaces
PetscInt loc = indices[i];
erestrict[eoffset++] = loc;
}
ierr = DMPlexRestoreClosureIndices(dm, section, section, c, PETSC_TRUE,
&numindices, &indices, NULL, NULL);
CHKERRQ(ierr);
}
if (eoffset != nelem*P*P) SETERRQ3(PETSC_COMM_SELF,
PETSC_ERR_LIB, "ElemRestriction of size (%D,%D) initialized %D nodes",
nelem, P*P, eoffset);
// Setup CEED restriction
ierr = DMGetLocalVector(dm, &Uloc); CHKERRQ(ierr);
ierr = VecGetLocalSize(Uloc, &nnodes); CHKERRQ(ierr);
ierr = DMRestoreLocalVector(dm, &Uloc); CHKERRQ(ierr);
CeedElemRestrictionCreate(ceed, nelem, P*P, ncomp, 1, nnodes,
CEED_MEM_HOST, CEED_COPY_VALUES, erestrict,
Erestrict);
ierr = PetscFree(erestrict); CHKERRQ(ierr);
PetscFunctionReturn(0);
}
// -----------------------------------------------------------------------------
// Auxiliary function to create a CeedVector from PetscVec of same size
// -----------------------------------------------------------------------------
PetscErrorCode CreateVectorFromPetscVec(Ceed ceed, Vec p,
CeedVector *v) {
PetscErrorCode ierr;
PetscInt m;
PetscFunctionBeginUser;
ierr = VecGetLocalSize(p, &m); CHKERRQ(ierr);
ierr = CeedVectorCreate(ceed, m, v); CHKERRQ(ierr);
PetscFunctionReturn(0);
}
// -----------------------------------------------------------------------------
// Auxiliary function to place a PetscVec into a CeedVector of same size
// -----------------------------------------------------------------------------
PetscErrorCode VectorPlacePetscVec(CeedVector c, Vec p) {
PetscErrorCode ierr;
PetscInt mceed,mpetsc;
PetscScalar *a;
PetscFunctionBeginUser;
ierr = CeedVectorGetLength(c, &mceed); CHKERRQ(ierr);
ierr = VecGetLocalSize(p, &mpetsc); CHKERRQ(ierr);
if (mceed != mpetsc) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,
"Cannot place PETSc Vec of length %D in CeedVector of length %D",
mpetsc, mceed);
ierr = VecGetArray(p, &a); CHKERRQ(ierr);
CeedVectorSetArray(c, CEED_MEM_HOST, CEED_USE_POINTER, a);
PetscFunctionReturn(0);
}
// -----------------------------------------------------------------------------
// Auxiliary function to set up libCEED objects for a given degree
// -----------------------------------------------------------------------------
PetscErrorCode SetupLibceed(DM dm, Ceed ceed, CeedInt degree, CeedInt qextra,
PetscInt ncompx, PetscInt ncompq, User user,
CeedData data, problemData *problem) {
int ierr;
DM dmcoord;
Vec Xloc;
CeedBasis basisx, basisxc, basisq;
CeedElemRestriction Erestrictx, Erestrictq, Erestrictqdi;
CeedQFunction qf_setup, qf_ics, qf_explicit, qf_implicit,
qf_jacobian;
CeedOperator op_setup, op_ics, op_explicit, op_implicit,
op_jacobian;
CeedVector xcorners, qdata, q0ceed;
CeedInt numP, numQ, cStart, cEnd, nelem, qdatasize = problem->qdatasize,
topodim = problem->topodim;;
// CEED bases
numP = degree + 1;
numQ = numP + qextra;
CeedBasisCreateTensorH1Lagrange(ceed, topodim, ncompq, numP, numQ,
CEED_GAUSS, &basisq);
CeedBasisCreateTensorH1Lagrange(ceed, topodim, ncompx, 2, numQ,
CEED_GAUSS, &basisx);
CeedBasisCreateTensorH1Lagrange(ceed, topodim, ncompx, 2, numP,
CEED_GAUSS_LOBATTO, &basisxc);
ierr = DMGetCoordinateDM(dm, &dmcoord); CHKERRQ(ierr);
ierr = DMPlexSetClosurePermutationTensor(dmcoord, PETSC_DETERMINE, NULL);
CHKERRQ(ierr);
// CEED restrictions
ierr = CreateRestrictionPlex(ceed, dmcoord, 2, ncompx, &Erestrictx);
CHKERRQ(ierr);
ierr = DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd); CHKERRQ(ierr);
nelem = cEnd - cStart;
CeedElemRestrictionCreateStrided(ceed, nelem, numQ*numQ, qdatasize,
qdatasize*nelem*numQ*numQ,
CEED_STRIDES_BACKEND, &Erestrictqdi);
// Solution vec restriction is a strided (identity) because we use a user
// mat mult before and after operator apply
CeedElemRestrictionCreateStrided(ceed, nelem, numP*numP, ncompq,
ncompq*nelem*numP*numP,
CEED_STRIDES_BACKEND, &Erestrictq);
// Element coordinates
ierr = DMGetCoordinatesLocal(dm, &Xloc); CHKERRQ(ierr);
ierr = CreateVectorFromPetscVec(ceed, Xloc, &xcorners); CHKERRQ(ierr);
// Create the persistent vectors that will be needed in setup and apply
CeedInt nqpts;
CeedBasisGetNumQuadraturePoints(basisq, &nqpts);
CeedVectorCreate(ceed, qdatasize*nelem*nqpts, &qdata);
CeedElemRestrictionCreateVector(Erestrictq, &q0ceed, NULL);
user->q0ceed = q0ceed;
// Create the Q-Function that builds the quadrature data
CeedQFunctionCreateInterior(ceed, 1, problem->setup, problem->setup_loc,
&qf_setup);
CeedQFunctionAddInput(qf_setup, "x", ncompx, CEED_EVAL_INTERP);
CeedQFunctionAddInput(qf_setup, "dx", ncompx*topodim, CEED_EVAL_GRAD);
CeedQFunctionAddInput(qf_setup, "weight", 1, CEED_EVAL_WEIGHT);
CeedQFunctionAddOutput(qf_setup, "qdata", qdatasize, CEED_EVAL_NONE);
// Create the Q-Function that sets the ICs of the operator
CeedQFunctionCreateInterior(ceed, 1, problem->ics, problem->ics_loc, &qf_ics);
CeedQFunctionAddInput(qf_ics, "x", ncompx, CEED_EVAL_INTERP);
CeedQFunctionAddOutput(qf_ics, "q0", ncompq, CEED_EVAL_NONE);
// Create the Q-Function that defines the explicit part of the PDE operator
CeedQFunctionCreateInterior(ceed, 1, problem->apply_explfunction,
problem->apply_explfunction_loc, &qf_explicit);
CeedQFunctionAddInput(qf_explicit, "x", ncompx, CEED_EVAL_INTERP);
CeedQFunctionAddInput(qf_explicit, "q", ncompq, CEED_EVAL_INTERP);
CeedQFunctionAddInput(qf_explicit, "dq", ncompq*topodim, CEED_EVAL_GRAD);
CeedQFunctionAddInput(qf_explicit, "qdata", qdatasize, CEED_EVAL_NONE);
CeedQFunctionAddOutput(qf_explicit, "v", ncompq, CEED_EVAL_INTERP);
CeedQFunctionAddOutput(qf_explicit, "dv", ncompq*topodim, CEED_EVAL_GRAD);
// Create the Q-Function that defines the implicit part of the PDE operator
CeedQFunctionCreateInterior(ceed, 1, problem->apply_implfunction,
problem->apply_implfunction_loc, &qf_implicit);
CeedQFunctionAddInput(qf_implicit, "q", ncompq, CEED_EVAL_INTERP);
CeedQFunctionAddInput(qf_implicit, "dq", ncompq*topodim, CEED_EVAL_GRAD);
CeedQFunctionAddInput(qf_implicit, "qdot", ncompq, CEED_EVAL_INTERP);
CeedQFunctionAddInput(qf_implicit, "qdata", qdatasize, CEED_EVAL_NONE);
CeedQFunctionAddInput(qf_implicit, "x", ncompx, CEED_EVAL_INTERP);
CeedQFunctionAddOutput(qf_implicit, "v", ncompq, CEED_EVAL_INTERP);
CeedQFunctionAddOutput(qf_implicit, "dv", ncompq*topodim, CEED_EVAL_GRAD);
// Create the Q-Function that defines the action of the Jacobian operator
CeedQFunctionCreateInterior(ceed, 1, problem->apply_jacobian,
problem->apply_jacobian_loc, &qf_jacobian);
CeedQFunctionAddInput(qf_jacobian, "q", ncompq, CEED_EVAL_INTERP);
CeedQFunctionAddInput(qf_jacobian, "dq", ncompq*topodim, CEED_EVAL_GRAD);
CeedQFunctionAddInput(qf_jacobian, "deltaq", ncompq, CEED_EVAL_INTERP);
CeedQFunctionAddInput(qf_jacobian, "deltadq", ncompq*topodim, CEED_EVAL_GRAD);
CeedQFunctionAddInput(qf_jacobian, "qdata", qdatasize, CEED_EVAL_NONE);
CeedQFunctionAddOutput(qf_jacobian, "deltav", ncompq, CEED_EVAL_INTERP);
CeedQFunctionAddOutput(qf_jacobian, "deltadv", ncompq*topodim,
CEED_EVAL_GRAD);
// Create the operator that builds the quadrature data for the operator
CeedOperatorCreate(ceed, qf_setup, NULL, NULL, &op_setup);
CeedOperatorSetField(op_setup, "x", Erestrictx, basisx,
CEED_VECTOR_ACTIVE);
CeedOperatorSetField(op_setup, "dx", Erestrictx, basisx,
CEED_VECTOR_ACTIVE);
CeedOperatorSetField(op_setup, "weight", CEED_ELEMRESTRICTION_NONE, basisx,
CEED_VECTOR_NONE);
CeedOperatorSetField(op_setup, "qdata", Erestrictqdi,
CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
// Create the operator that sets the ICs
CeedOperatorCreate(ceed, qf_ics, NULL, NULL, &op_ics);
CeedOperatorSetField(op_ics, "x", Erestrictx, basisxc, CEED_VECTOR_ACTIVE);
CeedOperatorSetField(op_ics, "q0", Erestrictq,
CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
CeedElemRestrictionCreateVector(Erestrictq, &user->qceed, NULL);
CeedElemRestrictionCreateVector(Erestrictq, &user->qdotceed, NULL);
CeedElemRestrictionCreateVector(Erestrictq, &user->gceed, NULL);
CeedElemRestrictionCreateVector(Erestrictq, &user->fceed, NULL);
CeedElemRestrictionCreateVector(Erestrictq, &user->jceed, NULL);
// Create the explicit part of the PDE operator
CeedOperatorCreate(ceed, qf_explicit, NULL, NULL, &op_explicit);
CeedOperatorSetField(op_explicit, "x", Erestrictx, basisx, xcorners);
CeedOperatorSetField(op_explicit, "q", Erestrictq, basisq,
CEED_VECTOR_ACTIVE);
CeedOperatorSetField(op_explicit, "dq", Erestrictq, basisq,
CEED_VECTOR_ACTIVE);
CeedOperatorSetField(op_explicit, "qdata", Erestrictqdi,
CEED_BASIS_COLLOCATED, qdata);
CeedOperatorSetField(op_explicit, "v", Erestrictq, basisq,
CEED_VECTOR_ACTIVE);
CeedOperatorSetField(op_explicit, "dv", Erestrictq, basisq,
CEED_VECTOR_ACTIVE);
user->op_explicit = op_explicit;
// Create the implicit part of the PDE operator
CeedOperatorCreate(ceed, qf_implicit, NULL, NULL, &op_implicit);
CeedOperatorSetField(op_implicit, "q", Erestrictq, basisq,
CEED_VECTOR_ACTIVE);
CeedOperatorSetField(op_implicit, "dq", Erestrictq, basisq,
CEED_VECTOR_ACTIVE);
CeedOperatorSetField(op_implicit, "qdot", Erestrictq, basisq, user->qdotceed);
CeedOperatorSetField(op_implicit, "qdata", Erestrictqdi,
CEED_BASIS_COLLOCATED, qdata);
CeedOperatorSetField(op_implicit, "x", Erestrictx, basisx, xcorners);
CeedOperatorSetField(op_implicit, "v", Erestrictq, basisq,
CEED_VECTOR_ACTIVE);
CeedOperatorSetField(op_implicit, "dv", Erestrictq, basisq,
CEED_VECTOR_ACTIVE);
user->op_implicit = op_implicit;
// Create the Jacobian of the PDE operator
CeedOperatorCreate(ceed, qf_jacobian, NULL, NULL, &op_jacobian);
CeedOperatorSetField(op_jacobian, "q", Erestrictq, basisq,
CEED_VECTOR_ACTIVE);
CeedOperatorSetField(op_jacobian, "dq", Erestrictq, basisq,
CEED_VECTOR_ACTIVE);
CeedOperatorSetField(op_jacobian, "deltaq", Erestrictq, basisq,
CEED_VECTOR_ACTIVE);
CeedOperatorSetField(op_jacobian, "deltadq", Erestrictq, basisq,
CEED_VECTOR_ACTIVE);
CeedOperatorSetField(op_jacobian, "qdata", Erestrictqdi,
CEED_BASIS_COLLOCATED, qdata);
CeedOperatorSetField(op_jacobian, "deltav", Erestrictq, basisq,
CEED_VECTOR_ACTIVE);
CeedOperatorSetField(op_jacobian, "deltadv", Erestrictq, basisq,
CEED_VECTOR_ACTIVE);
user->op_jacobian = op_jacobian;
// Set up the libCEED context
CeedQFunctionSetContext(qf_ics, data->physCtx);
CeedQFunctionSetContext(qf_explicit, data->problCtx);
CeedQFunctionSetContext(qf_implicit, data->problCtx);
CeedQFunctionSetContext(qf_jacobian, data->problCtx);
// Save libCEED data required for level // TODO: check how many of these are really needed outside
data->basisx = basisx;
data->basisq = basisq;
data->Erestrictx = Erestrictx;
data->Erestrictq = Erestrictq;
data->Erestrictqdi = Erestrictqdi;
data->qf_setup = qf_setup;
data->qf_ics = qf_ics;
data->qf_explicit = qf_explicit;
data->qf_implicit = qf_implicit;
data->qf_jacobian = qf_jacobian;
data->op_setup = op_setup;
data->op_ics = op_ics;
data->op_explicit = op_explicit;
data->op_implicit = op_implicit;
data->op_jacobian = op_jacobian;
data->qdata = qdata;
data->xcorners = xcorners;
PetscFunctionReturn(0);
}