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