xref: /libCEED/examples/mfem/bp3.h (revision 450bb777e488befcbd6c877f0c91ee3a4fe96487)
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 #ifndef bp3_h
18 #define bp3_h
19 #include <ceed.h>
20 
21 /// A structure used to pass additional data to f_build_diff and f_apply_diff
22 struct BuildContext { CeedInt dim, space_dim; };
23 
24 /// libCEED Q-function for building quadrature data for a diffusion operator
25 CEED_QFUNCTION(f_build_diff)(void *ctx, const CeedInt Q,
26                              const CeedScalar *const *in, CeedScalar *const *out) {
27   BuildContext *bc = (BuildContext *)ctx;
28   // in[0] is Jacobians with shape [dim, nc=dim, Q]
29   // in[1] is quadrature weights, size (Q)
30   //
31   // At every quadrature point, compute w/det(J).adj(J).adj(J)^T and store
32   // the symmetric part of the result.
33   const CeedScalar *J = in[0], *w = in[1];
34   CeedScalar *qdata = out[0];
35 
36   switch (bc->dim + 10*bc->space_dim) {
37   case 11:
38     // Quadrature Point Loop
39     CeedPragmaSIMD
40     for (CeedInt i=0; i<Q; i++) {
41       qdata[i] = w[i] / J[i];
42     }
43     break;
44   case 22:
45     // Quadrature Point Loop
46     CeedPragmaSIMD
47     for (CeedInt i=0; i<Q; i++) {
48       // J: 0 2   qdata: 0 2   adj(J):  J22 -J12
49       //    1 3          2 1           -J21  J11
50       const CeedScalar J11 = J[i+Q*0];
51       const CeedScalar J21 = J[i+Q*1];
52       const CeedScalar J12 = J[i+Q*2];
53       const CeedScalar J22 = J[i+Q*3];
54       const CeedScalar qw = w[i] / (J11*J22 - J21*J12);
55       qdata[i+Q*0] =   qw * (J12*J12 + J22*J22);
56       qdata[i+Q*1] =   qw * (J11*J11 + J21*J21);
57       qdata[i+Q*2] = - qw * (J11*J12 + J21*J22);
58     }
59     break;
60   case 33:
61     // Quadrature Point Loop
62     CeedPragmaSIMD
63     for (CeedInt i=0; i<Q; i++) {
64       // J: 0 3 6   qdata: 0 5 4
65       //    1 4 7          5 1 3
66       //    2 5 8          4 3 2
67       const CeedScalar J11 = J[i+Q*0];
68       const CeedScalar J21 = J[i+Q*1];
69       const CeedScalar J31 = J[i+Q*2];
70       const CeedScalar J12 = J[i+Q*3];
71       const CeedScalar J22 = J[i+Q*4];
72       const CeedScalar J32 = J[i+Q*5];
73       const CeedScalar J13 = J[i+Q*6];
74       const CeedScalar J23 = J[i+Q*7];
75       const CeedScalar J33 = J[i+Q*8];
76       const CeedScalar A11 = J22*J33 - J23*J32;
77       const CeedScalar A12 = J13*J32 - J12*J33;
78       const CeedScalar A13 = J12*J23 - J13*J22;
79       const CeedScalar A21 = J23*J31 - J21*J33;
80       const CeedScalar A22 = J11*J33 - J13*J31;
81       const CeedScalar A23 = J13*J21 - J11*J23;
82       const CeedScalar A31 = J21*J32 - J22*J31;
83       const CeedScalar A32 = J12*J31 - J11*J32;
84       const CeedScalar A33 = J11*J22 - J12*J21;
85       const CeedScalar qw = w[i] / (J11*A11 + J21*A12 + J31*A13);
86       qdata[i+Q*0] = qw * (A11*A11 + A12*A12 + A13*A13);
87       qdata[i+Q*1] = qw * (A21*A21 + A22*A22 + A23*A23);
88       qdata[i+Q*2] = qw * (A31*A31 + A32*A32 + A33*A33);
89       qdata[i+Q*3] = qw * (A21*A31 + A22*A32 + A23*A33);
90       qdata[i+Q*4] = qw * (A11*A31 + A12*A32 + A13*A33);
91       qdata[i+Q*5] = qw * (A11*A21 + A12*A22 + A13*A23);
92     }
93     break;
94   }
95   return 0;
96 }
97 
98 /// libCEED Q-function for applying a diff operator
99 CEED_QFUNCTION(f_apply_diff)(void *ctx, const CeedInt Q,
100                              const CeedScalar *const *in, CeedScalar *const *out) {
101   BuildContext *bc = (BuildContext *)ctx;
102   // in[0], out[0] have shape [dim, nc=1, Q]
103   const CeedScalar *ug = in[0], *qdata = in[1];
104   CeedScalar *vg = out[0];
105 
106   switch (bc->dim) {
107   case 1:
108     // Quadrature Point Loop
109     CeedPragmaSIMD
110     for (CeedInt i=0; i<Q; i++) {
111       vg[i] = ug[i] * qdata[i];
112     }
113     break;
114   case 2:
115     // Quadrature Point Loop
116     CeedPragmaSIMD
117     for (CeedInt i=0; i<Q; i++) {
118       const CeedScalar ug0 = ug[i+Q*0];
119       const CeedScalar ug1 = ug[i+Q*1];
120       vg[i+Q*0] = qdata[i+Q*0]*ug0 + qdata[i+Q*2]*ug1;
121       vg[i+Q*1] = qdata[i+Q*2]*ug0 + qdata[i+Q*1]*ug1;
122     }
123     break;
124   case 3:
125     // Quadrature Point Loop
126     CeedPragmaSIMD
127     for (CeedInt i=0; i<Q; i++) {
128       const CeedScalar ug0 = ug[i+Q*0];
129       const CeedScalar ug1 = ug[i+Q*1];
130       const CeedScalar ug2 = ug[i+Q*2];
131       vg[i+Q*0] = qdata[i+Q*0]*ug0 + qdata[i+Q*5]*ug1 + qdata[i+Q*4]*ug2;
132       vg[i+Q*1] = qdata[i+Q*5]*ug0 + qdata[i+Q*1]*ug1 + qdata[i+Q*3]*ug2;
133       vg[i+Q*2] = qdata[i+Q*4]*ug0 + qdata[i+Q*3]*ug1 + qdata[i+Q*2]*ug2;
134     }
135     break;
136   }
137   return 0;
138 }
139 
140 #endif // bp3_h
141