xref: /libCEED/examples/mfem/bp3.h (revision e5261ca07ee4f094ed332180400b18969f5abc8b)
1 // Copyright (c) 2017-2018, Lawrence Livermore National Security, LLC.
2 // Produced at the Lawrence Livermore National Laboratory. LLNL-CODE-734707.
3 // All Rights reserved. See files LICENSE and NOTICE for details.
4 //
5 // This file is part of CEED, a collection of benchmarks, miniapps, software
6 // libraries and APIs for efficient high-order finite element and spectral
7 // element discretizations for exascale applications. For more information and
8 // source code availability see http://github.com/ceed.
9 //
10 // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
11 // a collaborative effort of two U.S. Department of Energy organizations (Office
12 // of Science and the National Nuclear Security Administration) responsible for
13 // the planning and preparation of a capable exascale ecosystem, including
14 // software, applications, hardware, advanced system engineering and early
15 // testbed platforms, in support of the nation's exascale computing imperative.
16 
17 
18 /// A structure used to pass additional data to f_build_diff and f_apply_diff
19 struct BuildContext { CeedInt dim, space_dim; };
20 
21 /// libCEED Q-function for building quadrature data for a diffusion operator
22 CEED_QFUNCTION(f_build_diff)(void *ctx, const CeedInt Q,
23                              const CeedScalar *const *in, CeedScalar *const *out) {
24   BuildContext *bc = (BuildContext *)ctx;
25   // in[0] is Jacobians with shape [dim, nc=dim, Q]
26   // in[1] is quadrature weights, size (Q)
27   //
28   // At every quadrature point, compute w/det(J).adj(J).adj(J)^T and store
29   // the symmetric part of the result.
30   const CeedScalar *J = in[0], *w = in[1];
31   CeedScalar *qdata = out[0];
32 
33   switch (bc->dim + 10*bc->space_dim) {
34   case 11:
35     // Quadrature Point Loop
36     CeedPragmaSIMD
37     for (CeedInt i=0; i<Q; i++) {
38       qdata[i] = w[i] / J[i];
39     }
40     break;
41   case 22:
42     // Quadrature Point Loop
43     CeedPragmaSIMD
44     for (CeedInt i=0; i<Q; i++) {
45       // J: 0 2   qdata: 0 2   adj(J):  J22 -J12
46       //    1 3          2 1           -J21  J11
47       const CeedScalar J11 = J[i+Q*0];
48       const CeedScalar J21 = J[i+Q*1];
49       const CeedScalar J12 = J[i+Q*2];
50       const CeedScalar J22 = J[i+Q*3];
51       const CeedScalar qw = w[i] / (J11*J22 - J21*J12);
52       qdata[i+Q*0] =   qw * (J12*J12 + J22*J22);
53       qdata[i+Q*1] =   qw * (J11*J11 + J21*J21);
54       qdata[i+Q*2] = - qw * (J11*J12 + J21*J22);
55     }
56     break;
57   case 33:
58     // Quadrature Point Loop
59     CeedPragmaSIMD
60     for (CeedInt i=0; i<Q; i++) {
61       // J: 0 3 6   qdata: 0 5 4
62       //    1 4 7          5 1 3
63       //    2 5 8          4 3 2
64       const CeedScalar J11 = J[i+Q*0];
65       const CeedScalar J21 = J[i+Q*1];
66       const CeedScalar J31 = J[i+Q*2];
67       const CeedScalar J12 = J[i+Q*3];
68       const CeedScalar J22 = J[i+Q*4];
69       const CeedScalar J32 = J[i+Q*5];
70       const CeedScalar J13 = J[i+Q*6];
71       const CeedScalar J23 = J[i+Q*7];
72       const CeedScalar J33 = J[i+Q*8];
73       const CeedScalar A11 = J22*J33 - J23*J32;
74       const CeedScalar A12 = J13*J32 - J12*J33;
75       const CeedScalar A13 = J12*J23 - J13*J22;
76       const CeedScalar A21 = J23*J31 - J21*J33;
77       const CeedScalar A22 = J11*J33 - J13*J31;
78       const CeedScalar A23 = J13*J21 - J11*J23;
79       const CeedScalar A31 = J21*J32 - J22*J31;
80       const CeedScalar A32 = J12*J31 - J11*J32;
81       const CeedScalar A33 = J11*J22 - J12*J21;
82       const CeedScalar qw = w[i] / (J11*A11 + J21*A12 + J31*A13);
83       qdata[i+Q*0] = qw * (A11*A11 + A12*A12 + A13*A13);
84       qdata[i+Q*1] = qw * (A21*A21 + A22*A22 + A23*A23);
85       qdata[i+Q*2] = qw * (A31*A31 + A32*A32 + A33*A33);
86       qdata[i+Q*3] = qw * (A21*A31 + A22*A32 + A23*A33);
87       qdata[i+Q*4] = qw * (A11*A31 + A12*A32 + A13*A33);
88       qdata[i+Q*5] = qw * (A11*A21 + A12*A22 + A13*A23);
89     }
90     break;
91   }
92   return 0;
93 }
94 
95 /// libCEED Q-function for applying a diff operator
96 CEED_QFUNCTION(f_apply_diff)(void *ctx, const CeedInt Q,
97                              const CeedScalar *const *in, CeedScalar *const *out) {
98   BuildContext *bc = (BuildContext *)ctx;
99   // in[0], out[0] have shape [dim, nc=1, Q]
100   const CeedScalar *ug = in[0], *qdata = in[1];
101   CeedScalar *vg = out[0];
102 
103   switch (bc->dim) {
104   case 1:
105     // Quadrature Point Loop
106     CeedPragmaSIMD
107     for (CeedInt i=0; i<Q; i++) {
108       vg[i] = ug[i] * qdata[i];
109     }
110     break;
111   case 2:
112     // Quadrature Point Loop
113     CeedPragmaSIMD
114     for (CeedInt i=0; i<Q; i++) {
115       const CeedScalar ug0 = ug[i+Q*0];
116       const CeedScalar ug1 = ug[i+Q*1];
117       vg[i+Q*0] = qdata[i+Q*0]*ug0 + qdata[i+Q*2]*ug1;
118       vg[i+Q*1] = qdata[i+Q*2]*ug0 + qdata[i+Q*1]*ug1;
119     }
120     break;
121   case 3:
122     // Quadrature Point Loop
123     CeedPragmaSIMD
124     for (CeedInt i=0; i<Q; i++) {
125       const CeedScalar ug0 = ug[i+Q*0];
126       const CeedScalar ug1 = ug[i+Q*1];
127       const CeedScalar ug2 = ug[i+Q*2];
128       vg[i+Q*0] = qdata[i+Q*0]*ug0 + qdata[i+Q*5]*ug1 + qdata[i+Q*4]*ug2;
129       vg[i+Q*1] = qdata[i+Q*5]*ug0 + qdata[i+Q*1]*ug1 + qdata[i+Q*3]*ug2;
130       vg[i+Q*2] = qdata[i+Q*4]*ug0 + qdata[i+Q*3]*ug1 + qdata[i+Q*2]*ug2;
131     }
132     break;
133   }
134   return 0;
135 }
136 
137