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