xref: /libCEED/examples/ceed/ex2-surface.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 ex2_surface_h
18 #define ex2_surface_h
19 
20 /// A structure used to pass additional data to f_build_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   struct BuildContext *bc = (struct 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     CeedPragmaSIMD
38     for (CeedInt i=0; i<Q; i++) {
39       qdata[i] = w[i] / J[i];
40     } // End of Quadrature Point Loop
41     break;
42   case 22:
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     } // End of Quadrature Point Loop
56     break;
57   case 33:
58     CeedPragmaSIMD
59     for (CeedInt i=0; i<Q; i++) {
60       // Compute the adjoint
61       CeedScalar A[3][3];
62       for (CeedInt j=0; j<3; j++)
63         for (CeedInt k=0; k<3; k++)
64           // Equivalent code with J as a VLA and no mod operations:
65           // A[k][j] = J[j+1][k+1]*J[j+2][k+2] - J[j+1][k+2]*J[j+2][k+1]
66           A[k][j] = J[i+Q*((j+1)%3+3*((k+1)%3))]*J[i+Q*((j+2)%3+3*((k+2)%3))] -
67                     J[i+Q*((j+1)%3+3*((k+2)%3))]*J[i+Q*((j+2)%3+3*((k+1)%3))];
68 
69       // Compute quadrature weight / det(J)
70       const CeedScalar qw = w[i] / (J[i+Q*0]*A[0][0] + J[i+Q*1]*A[1][1] +
71                                     J[i+Q*2]*A[2][2]);
72 
73       // Compute geometric factors
74       // Stored in Voigt convention
75       // 0 5 4
76       // 5 1 3
77       // 4 3 2
78       qdata[i+Q*0] = qw * (A[0][0]*A[0][0] + A[0][1]*A[0][1] + A[0][2]*A[0][2]);
79       qdata[i+Q*1] = qw * (A[1][0]*A[1][0] + A[1][1]*A[1][1] + A[1][2]*A[1][2]);
80       qdata[i+Q*2] = qw * (A[2][0]*A[2][0] + A[2][1]*A[2][1] + A[2][2]*A[2][2]);
81       qdata[i+Q*3] = qw * (A[1][0]*A[2][0] + A[1][1]*A[2][1] + A[1][2]*A[2][2]);
82       qdata[i+Q*4] = qw * (A[0][0]*A[2][0] + A[0][1]*A[2][1] + A[0][2]*A[2][2]);
83       qdata[i+Q*5] = qw * (A[0][0]*A[1][0] + A[0][1]*A[1][1] + A[0][2]*A[1][2]);
84     } // End of Quadrature Point Loop
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   struct BuildContext *bc = (struct 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     CeedPragmaSIMD
101     for (CeedInt i=0; i<Q; i++) {
102       vg[i] = ug[i] * qdata[i];
103     } // End of Quadrature Point Loop
104     break;
105   case 2:
106     CeedPragmaSIMD
107     for (CeedInt i=0; i<Q; i++) {
108       // Read spatial derivatives of u
109       const CeedScalar du[2]        =  {ug[i+Q*0],
110                                         ug[i+Q*1]
111                                        };
112 
113       // Read qdata (dXdxdXdxT symmetric matrix)
114       // Stored in Voigt convention
115       // 0 2
116       // 2 1
117       // *INDENT-OFF*
118       const CeedScalar dXdxdXdxT[2][2] = {{qdata[i+0*Q],
119                                            qdata[i+2*Q]},
120                                           {qdata[i+2*Q],
121                                            qdata[i+1*Q]}};
122       // *INDENT-ON*
123       // j = direction of vg
124       for (int j=0; j<2; j++)
125         vg[i+j*Q] = (du[0] * dXdxdXdxT[0][j] +
126                      du[1] * dXdxdXdxT[1][j]);
127     } // End of Quadrature Point Loop
128     break;
129   case 3:
130     CeedPragmaSIMD
131     for (CeedInt i=0; i<Q; i++) {
132       // Read spatial derivatives of u
133       const CeedScalar du[3]        =  {ug[i+Q*0],
134                                         ug[i+Q*1],
135                                         ug[i+Q*2]
136                                        };
137 
138       // Read qdata (dXdxdXdxT symmetric matrix)
139       // Stored in Voigt convention
140       // 0 5 4
141       // 5 1 3
142       // 4 3 2
143       // *INDENT-OFF*
144       const CeedScalar dXdxdXdxT[3][3] = {{qdata[i+0*Q],
145                                            qdata[i+5*Q],
146                                            qdata[i+4*Q]},
147                                           {qdata[i+5*Q],
148                                            qdata[i+1*Q],
149                                            qdata[i+3*Q]},
150                                           {qdata[i+4*Q],
151                                            qdata[i+3*Q],
152                                            qdata[i+2*Q]}
153                                          };
154       // *INDENT-ON*
155       // j = direction of vg
156       for (int j=0; j<3; j++)
157         vg[i+j*Q] = (du[0] * dXdxdXdxT[0][j] +
158                      du[1] * dXdxdXdxT[1][j] +
159                      du[2] * dXdxdXdxT[2][j]);
160     } // End of Quadrature Point Loop
161     break;
162   }
163   return 0;
164 }
165 
166 #endif // ex2_surface_h
167