xref: /libCEED/examples/ceed/ex2-surface.h (revision 0a242873a29183ba8fb2a56a57a36b831ce52f87)
15aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
366087c08SValeria Barra //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
566087c08SValeria Barra //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
766087c08SValeria Barra 
8c0b5abf0SJeremy L Thompson #include <ceed/types.h>
9c9c2c079SJeremy L Thompson 
1066087c08SValeria Barra /// A structure used to pass additional data to f_build_diff
112b730f8bSJeremy L Thompson struct BuildContext {
122b730f8bSJeremy L Thompson   CeedInt dim, space_dim;
132b730f8bSJeremy L Thompson };
1466087c08SValeria Barra 
1566087c08SValeria Barra /// libCEED Q-function for building quadrature data for a diffusion operator
16d37d859eSJeremy L Thompson CEED_QFUNCTION(build_diff)(void *ctx, const CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
17*0a242873SJeremy L Thompson   // in[0] is Jacobians with shape [dim, dim, Q]
1866087c08SValeria Barra   // in[1] is quadrature weights, size (Q)
19*0a242873SJeremy L Thompson   const CeedScalar *w             = in[1];
20*0a242873SJeremy L Thompson   CeedScalar(*q_data)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
21*0a242873SJeremy L Thompson   struct BuildContext *build_data = (struct BuildContext *)ctx;
22*0a242873SJeremy L Thompson 
2366087c08SValeria Barra   // At every quadrature point, compute w/det(J).adj(J).adj(J)^T and store
2466087c08SValeria Barra   // the symmetric part of the result.
25d37d859eSJeremy L Thompson   switch (build_data->dim + 10 * build_data->space_dim) {
26*0a242873SJeremy L Thompson     case 11: {
27*0a242873SJeremy L Thompson       const CeedScalar(*J)[1][CEED_Q_VLA] = (const CeedScalar(*)[1][CEED_Q_VLA])in[0];
280b96b02dSJeremy L Thompson 
29*0a242873SJeremy L Thompson       CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { q_data[0][i] = w[i] / J[0][0][i]; }  // End of Quadrature Point Loop
30*0a242873SJeremy L Thompson     } break;
31*0a242873SJeremy L Thompson     case 22: {
32*0a242873SJeremy L Thompson       const CeedScalar(*J)[2][CEED_Q_VLA] = (const CeedScalar(*)[2][CEED_Q_VLA])in[0];
33*0a242873SJeremy L Thompson 
34*0a242873SJeremy L Thompson       CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
35*0a242873SJeremy L Thompson         // J: 0 2   q_data: 0 2   adj(J):  J11 -J01
36*0a242873SJeremy L Thompson         //    1 3           2 1           -J10  J00
37*0a242873SJeremy L Thompson         const CeedScalar J00 = J[0][0][i];
38*0a242873SJeremy L Thompson         const CeedScalar J10 = J[0][1][i];
39*0a242873SJeremy L Thompson         const CeedScalar J01 = J[1][0][i];
40*0a242873SJeremy L Thompson         const CeedScalar J11 = J[1][1][i];
41*0a242873SJeremy L Thompson         const CeedScalar qw  = w[i] / (J00 * J11 - J10 * J01);
42*0a242873SJeremy L Thompson 
43*0a242873SJeremy L Thompson         q_data[0][i] = qw * (J01 * J01 + J11 * J11);
44*0a242873SJeremy L Thompson         q_data[1][i] = qw * (J00 * J00 + J10 * J10);
45*0a242873SJeremy L Thompson         q_data[2][i] = -qw * (J00 * J01 + J10 * J11);
4666087c08SValeria Barra       }  // End of Quadrature Point Loop
47*0a242873SJeremy L Thompson     } break;
48*0a242873SJeremy L Thompson     case 33: {
49*0a242873SJeremy L Thompson       const CeedScalar(*J)[3][CEED_Q_VLA] = (const CeedScalar(*)[3][CEED_Q_VLA])in[0];
50*0a242873SJeremy L Thompson 
512b730f8bSJeremy L Thompson       CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
5266087c08SValeria Barra         // Compute the adjoint
5366087c08SValeria Barra         CeedScalar A[3][3];
54*0a242873SJeremy L Thompson 
550b96b02dSJeremy L Thompson         for (CeedInt j = 0; j < 3; j++) {
560b96b02dSJeremy L Thompson           for (CeedInt k = 0; k < 3; k++) {
5766087c08SValeria Barra             // Equivalent code with J as a VLA and no mod operations:
5866087c08SValeria Barra             // A[k][j] = J[j+1][k+1]*J[j+2][k+2] - J[j+1][k+2]*J[j+2][k+1]
59*0a242873SJeremy L Thompson             A[k][j] =
60*0a242873SJeremy L Thompson                 J[(k + 1) % 3][(j + 1) % 3][i] * J[(k + 2) % 3][(j + 2) % 3][i] - J[(k + 2) % 3][(j + 1) % 3][i] * J[(k + 1) % 3][(j + 2) % 3][i];
610b96b02dSJeremy L Thompson           }
620b96b02dSJeremy L Thompson         }
6366087c08SValeria Barra 
6466087c08SValeria Barra         // Compute quadrature weight / det(J)
65*0a242873SJeremy L Thompson         const CeedScalar qw = w[i] / (J[0][0][i] * A[0][0] + J[0][1][i] * A[0][1] + J[0][2][i] * A[0][2]);
6666087c08SValeria Barra 
6766087c08SValeria Barra         // Compute geometric factors
6866087c08SValeria Barra         // Stored in Voigt convention
6966087c08SValeria Barra         // 0 5 4
7066087c08SValeria Barra         // 5 1 3
7166087c08SValeria Barra         // 4 3 2
72*0a242873SJeremy L Thompson         q_data[0][i] = qw * (A[0][0] * A[0][0] + A[0][1] * A[0][1] + A[0][2] * A[0][2]);
73*0a242873SJeremy L Thompson         q_data[1][i] = qw * (A[1][0] * A[1][0] + A[1][1] * A[1][1] + A[1][2] * A[1][2]);
74*0a242873SJeremy L Thompson         q_data[2][i] = qw * (A[2][0] * A[2][0] + A[2][1] * A[2][1] + A[2][2] * A[2][2]);
75*0a242873SJeremy L Thompson         q_data[3][i] = qw * (A[1][0] * A[2][0] + A[1][1] * A[2][1] + A[1][2] * A[2][2]);
76*0a242873SJeremy L Thompson         q_data[4][i] = qw * (A[0][0] * A[2][0] + A[0][1] * A[2][1] + A[0][2] * A[2][2]);
77*0a242873SJeremy L Thompson         q_data[5][i] = qw * (A[0][0] * A[1][0] + A[0][1] * A[1][1] + A[0][2] * A[1][2]);
7866087c08SValeria Barra       }  // End of Quadrature Point Loop
79*0a242873SJeremy L Thompson     } break;
8066087c08SValeria Barra   }
810b96b02dSJeremy L Thompson   return CEED_ERROR_SUCCESS;
8266087c08SValeria Barra }
8366087c08SValeria Barra 
8466087c08SValeria Barra /// libCEED Q-function for applying a diff operator
85d37d859eSJeremy L Thompson CEED_QFUNCTION(apply_diff)(void *ctx, const CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
86d37d859eSJeremy L Thompson   struct BuildContext *build_data = (struct BuildContext *)ctx;
87*0a242873SJeremy L Thompson   // in[0], out[0] solution gradients with shape [dim, 1, Q]
88*0a242873SJeremy L Thompson   // in[1] is quadrature data with shape [num_components, Q]
89*0a242873SJeremy L Thompson   const CeedScalar(*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[1];
9066087c08SValeria Barra 
91d37d859eSJeremy L Thompson   switch (build_data->dim) {
92*0a242873SJeremy L Thompson     case 1: {
93*0a242873SJeremy L Thompson       const CeedScalar *ug = in[0];
94*0a242873SJeremy L Thompson       CeedScalar       *vg = out[0];
9566087c08SValeria Barra 
96*0a242873SJeremy L Thompson       CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { vg[i] = ug[i] * q_data[0][i]; }  // End of Quadrature Point Loop
97*0a242873SJeremy L Thompson     } break;
98*0a242873SJeremy L Thompson     case 2: {
99*0a242873SJeremy L Thompson       const CeedScalar(*ug)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
100*0a242873SJeremy L Thompson       CeedScalar(*vg)[CEED_Q_VLA]       = (CeedScalar(*)[CEED_Q_VLA])out[0];
101*0a242873SJeremy L Thompson 
102*0a242873SJeremy L Thompson       CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
103d1d35e2fSjeremylt         // Read q_data (dXdxdXdx_T symmetric matrix)
10466087c08SValeria Barra         // Stored in Voigt convention
10566087c08SValeria Barra         // 0 2
10666087c08SValeria Barra         // 2 1
1072b730f8bSJeremy L Thompson         const CeedScalar dXdxdXdx_T[2][2] = {
108*0a242873SJeremy L Thompson             {q_data[0][i], q_data[2][i]},
109*0a242873SJeremy L Thompson             {q_data[2][i], q_data[1][i]}
1102b730f8bSJeremy L Thompson         };
11166087c08SValeria Barra 
112*0a242873SJeremy L Thompson         // j = direction of vg
113*0a242873SJeremy L Thompson         for (int j = 0; j < 2; j++) vg[j][i] = (ug[0][i] * dXdxdXdx_T[0][j] + ug[1][i] * dXdxdXdx_T[1][j]);
114*0a242873SJeremy L Thompson       }  // End of Quadrature Point Loop
115*0a242873SJeremy L Thompson     } break;
116*0a242873SJeremy L Thompson     case 3: {
117*0a242873SJeremy L Thompson       const CeedScalar(*ug)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
118*0a242873SJeremy L Thompson       CeedScalar(*vg)[CEED_Q_VLA]       = (CeedScalar(*)[CEED_Q_VLA])out[0];
119*0a242873SJeremy L Thompson 
120*0a242873SJeremy L Thompson       CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
121d1d35e2fSjeremylt         // Read q_data (dXdxdXdx_T symmetric matrix)
12266087c08SValeria Barra         // Stored in Voigt convention
12366087c08SValeria Barra         // 0 5 4
12466087c08SValeria Barra         // 5 1 3
12566087c08SValeria Barra         // 4 3 2
1262b730f8bSJeremy L Thompson         const CeedScalar dXdxdXdx_T[3][3] = {
127*0a242873SJeremy L Thompson             {q_data[0][i], q_data[5][i], q_data[4][i]},
128*0a242873SJeremy L Thompson             {q_data[5][i], q_data[1][i], q_data[3][i]},
129*0a242873SJeremy L Thompson             {q_data[4][i], q_data[3][i], q_data[2][i]}
13066087c08SValeria Barra         };
131*0a242873SJeremy L Thompson 
13266087c08SValeria Barra         // j = direction of vg
133*0a242873SJeremy L Thompson         for (int j = 0; j < 3; j++) vg[j][i] = (ug[0][i] * dXdxdXdx_T[0][j] + ug[1][i] * dXdxdXdx_T[1][j] + ug[2][i] * dXdxdXdx_T[2][j]);
13466087c08SValeria Barra       }  // End of Quadrature Point Loop
135*0a242873SJeremy L Thompson     } break;
13666087c08SValeria Barra   }
1370b96b02dSJeremy L Thompson   return CEED_ERROR_SUCCESS;
13866087c08SValeria Barra }
139