xref: /libCEED/examples/ceed/ex2-surface.h (revision 9ba83ac0e4b1fca39d6fa6737a318a9f0cbc172d)
1*9ba83ac0SJeremy L Thompson // Copyright (c) 2017-2026, 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) {
170a242873SJeremy L Thompson   // in[0] is Jacobians with shape [dim, dim, Q]
1866087c08SValeria Barra   // in[1] is quadrature weights, size (Q)
190a242873SJeremy L Thompson   const CeedScalar *w             = in[1];
200a242873SJeremy L Thompson   CeedScalar(*q_data)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
210a242873SJeremy L Thompson   struct BuildContext *build_data = (struct BuildContext *)ctx;
220a242873SJeremy 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) {
260a242873SJeremy L Thompson     case 11: {
270a242873SJeremy L Thompson       const CeedScalar(*J)[1][CEED_Q_VLA] = (const CeedScalar(*)[1][CEED_Q_VLA])in[0];
280b96b02dSJeremy L Thompson 
290a242873SJeremy 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
300a242873SJeremy L Thompson     } break;
310a242873SJeremy L Thompson     case 22: {
320a242873SJeremy L Thompson       const CeedScalar(*J)[2][CEED_Q_VLA] = (const CeedScalar(*)[2][CEED_Q_VLA])in[0];
330a242873SJeremy L Thompson 
340a242873SJeremy L Thompson       CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
350a242873SJeremy L Thompson         // J: 0 2   q_data: 0 2   adj(J):  J11 -J01
360a242873SJeremy L Thompson         //    1 3           2 1           -J10  J00
370a242873SJeremy L Thompson         const CeedScalar J00 = J[0][0][i];
380a242873SJeremy L Thompson         const CeedScalar J10 = J[0][1][i];
390a242873SJeremy L Thompson         const CeedScalar J01 = J[1][0][i];
400a242873SJeremy L Thompson         const CeedScalar J11 = J[1][1][i];
410a242873SJeremy L Thompson         const CeedScalar qw  = w[i] / (J00 * J11 - J10 * J01);
420a242873SJeremy L Thompson 
430a242873SJeremy L Thompson         q_data[0][i] = qw * (J01 * J01 + J11 * J11);
440a242873SJeremy L Thompson         q_data[1][i] = qw * (J00 * J00 + J10 * J10);
450a242873SJeremy L Thompson         q_data[2][i] = -qw * (J00 * J01 + J10 * J11);
4666087c08SValeria Barra       }  // End of Quadrature Point Loop
470a242873SJeremy L Thompson     } break;
480a242873SJeremy L Thompson     case 33: {
490a242873SJeremy L Thompson       const CeedScalar(*J)[3][CEED_Q_VLA] = (const CeedScalar(*)[3][CEED_Q_VLA])in[0];
500a242873SJeremy L Thompson 
512b730f8bSJeremy L Thompson       CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
5266087c08SValeria Barra         // Compute the adjoint
5366087c08SValeria Barra         CeedScalar A[3][3];
540a242873SJeremy 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]
590a242873SJeremy L Thompson             A[k][j] =
600a242873SJeremy 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)
650a242873SJeremy 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
720a242873SJeremy 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]);
730a242873SJeremy 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]);
740a242873SJeremy 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]);
750a242873SJeremy 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]);
760a242873SJeremy 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]);
770a242873SJeremy 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
790a242873SJeremy 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;
870a242873SJeremy L Thompson   // in[0], out[0] solution gradients with shape [dim, 1, Q]
880a242873SJeremy L Thompson   // in[1] is quadrature data with shape [num_components, Q]
890a242873SJeremy 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) {
920a242873SJeremy L Thompson     case 1: {
930a242873SJeremy L Thompson       const CeedScalar *ug = in[0];
940a242873SJeremy L Thompson       CeedScalar       *vg = out[0];
9566087c08SValeria Barra 
960a242873SJeremy L Thompson       CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { vg[i] = ug[i] * q_data[0][i]; }  // End of Quadrature Point Loop
970a242873SJeremy L Thompson     } break;
980a242873SJeremy L Thompson     case 2: {
990a242873SJeremy L Thompson       const CeedScalar(*ug)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
1000a242873SJeremy L Thompson       CeedScalar(*vg)[CEED_Q_VLA]       = (CeedScalar(*)[CEED_Q_VLA])out[0];
1010a242873SJeremy L Thompson 
1020a242873SJeremy 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] = {
1080a242873SJeremy L Thompson             {q_data[0][i], q_data[2][i]},
1090a242873SJeremy L Thompson             {q_data[2][i], q_data[1][i]}
1102b730f8bSJeremy L Thompson         };
11166087c08SValeria Barra 
1120a242873SJeremy L Thompson         // j = direction of vg
1130a242873SJeremy 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]);
1140a242873SJeremy L Thompson       }  // End of Quadrature Point Loop
1150a242873SJeremy L Thompson     } break;
1160a242873SJeremy L Thompson     case 3: {
1170a242873SJeremy L Thompson       const CeedScalar(*ug)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
1180a242873SJeremy L Thompson       CeedScalar(*vg)[CEED_Q_VLA]       = (CeedScalar(*)[CEED_Q_VLA])out[0];
1190a242873SJeremy L Thompson 
1200a242873SJeremy 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] = {
1270a242873SJeremy L Thompson             {q_data[0][i], q_data[5][i], q_data[4][i]},
1280a242873SJeremy L Thompson             {q_data[5][i], q_data[1][i], q_data[3][i]},
1290a242873SJeremy L Thompson             {q_data[4][i], q_data[3][i], q_data[2][i]}
13066087c08SValeria Barra         };
1310a242873SJeremy L Thompson 
13266087c08SValeria Barra         // j = direction of vg
1330a242873SJeremy 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
1350a242873SJeremy L Thompson     } break;
13666087c08SValeria Barra   }
1370b96b02dSJeremy L Thompson   return CEED_ERROR_SUCCESS;
13866087c08SValeria Barra }
139