xref: /libCEED/examples/ceed/ex2-surface.h (revision d6ed5abffa3823f6603d4dc65272439584c38270)
19ba83ac0SJeremy 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
build_diff(void * ctx,const CeedInt Q,const CeedScalar * const * in,CeedScalar * const * out)16d37d859eSJeremy L Thompson CEED_QFUNCTION(build_diff)(void *ctx, const CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
17*860dc821SJeremy L Thompson   struct BuildContext *build_data = (struct BuildContext *)ctx;
18*860dc821SJeremy L Thompson 
190a242873SJeremy L Thompson   // in[0] is Jacobians with shape [dim, dim, Q]
2066087c08SValeria Barra   // in[1] is quadrature weights, size (Q)
210a242873SJeremy L Thompson   const CeedScalar *w             = in[1];
220a242873SJeremy L Thompson   CeedScalar(*q_data)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
230a242873SJeremy L Thompson 
2466087c08SValeria Barra   // At every quadrature point, compute w/det(J).adj(J).adj(J)^T and store
2566087c08SValeria Barra   // the symmetric part of the result.
26d37d859eSJeremy L Thompson   switch (build_data->dim + 10 * build_data->space_dim) {
270a242873SJeremy L Thompson     case 11: {
280a242873SJeremy L Thompson       const CeedScalar(*J)[1][CEED_Q_VLA] = (const CeedScalar(*)[1][CEED_Q_VLA])in[0];
290b96b02dSJeremy L Thompson 
300a242873SJeremy 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
310a242873SJeremy L Thompson     } break;
320a242873SJeremy L Thompson     case 22: {
330a242873SJeremy L Thompson       const CeedScalar(*J)[2][CEED_Q_VLA] = (const CeedScalar(*)[2][CEED_Q_VLA])in[0];
340a242873SJeremy L Thompson 
350a242873SJeremy L Thompson       CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
360a242873SJeremy L Thompson         // J: 0 2   q_data: 0 2   adj(J):  J11 -J01
370a242873SJeremy L Thompson         //    1 3           2 1           -J10  J00
380a242873SJeremy L Thompson         const CeedScalar J00 = J[0][0][i];
390a242873SJeremy L Thompson         const CeedScalar J10 = J[0][1][i];
400a242873SJeremy L Thompson         const CeedScalar J01 = J[1][0][i];
410a242873SJeremy L Thompson         const CeedScalar J11 = J[1][1][i];
420a242873SJeremy L Thompson         const CeedScalar qw  = w[i] / (J00 * J11 - J10 * J01);
430a242873SJeremy L Thompson 
440a242873SJeremy L Thompson         q_data[0][i] = qw * (J01 * J01 + J11 * J11);
450a242873SJeremy L Thompson         q_data[1][i] = qw * (J00 * J00 + J10 * J10);
460a242873SJeremy L Thompson         q_data[2][i] = -qw * (J00 * J01 + J10 * J11);
4766087c08SValeria Barra       }  // End of Quadrature Point Loop
480a242873SJeremy L Thompson     } break;
490a242873SJeremy L Thompson     case 33: {
500a242873SJeremy L Thompson       const CeedScalar(*J)[3][CEED_Q_VLA] = (const CeedScalar(*)[3][CEED_Q_VLA])in[0];
510a242873SJeremy L Thompson 
522b730f8bSJeremy L Thompson       CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
5366087c08SValeria Barra         // Compute the adjoint
5466087c08SValeria Barra         CeedScalar A[3][3];
550a242873SJeremy L Thompson 
560b96b02dSJeremy L Thompson         for (CeedInt j = 0; j < 3; j++) {
570b96b02dSJeremy L Thompson           for (CeedInt k = 0; k < 3; k++) {
5866087c08SValeria Barra             // Equivalent code with J as a VLA and no mod operations:
5966087c08SValeria Barra             // A[k][j] = J[j+1][k+1]*J[j+2][k+2] - J[j+1][k+2]*J[j+2][k+1]
600a242873SJeremy L Thompson             A[k][j] =
610a242873SJeremy 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];
620b96b02dSJeremy L Thompson           }
630b96b02dSJeremy L Thompson         }
6466087c08SValeria Barra 
6566087c08SValeria Barra         // Compute quadrature weight / det(J)
660a242873SJeremy 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]);
6766087c08SValeria Barra 
6866087c08SValeria Barra         // Compute geometric factors
6966087c08SValeria Barra         // Stored in Voigt convention
7066087c08SValeria Barra         // 0 5 4
7166087c08SValeria Barra         // 5 1 3
7266087c08SValeria Barra         // 4 3 2
730a242873SJeremy 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]);
740a242873SJeremy 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]);
750a242873SJeremy 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]);
760a242873SJeremy 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]);
770a242873SJeremy 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]);
780a242873SJeremy 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]);
7966087c08SValeria Barra       }  // End of Quadrature Point Loop
800a242873SJeremy L Thompson     } break;
8166087c08SValeria Barra   }
820b96b02dSJeremy L Thompson   return CEED_ERROR_SUCCESS;
8366087c08SValeria Barra }
8466087c08SValeria Barra 
8566087c08SValeria Barra /// libCEED Q-function for applying a diff operator
apply_diff(void * ctx,const CeedInt Q,const CeedScalar * const * in,CeedScalar * const * out)86d37d859eSJeremy L Thompson CEED_QFUNCTION(apply_diff)(void *ctx, const CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
87d37d859eSJeremy L Thompson   struct BuildContext *build_data = (struct BuildContext *)ctx;
88*860dc821SJeremy L Thompson 
890a242873SJeremy L Thompson   // in[0], out[0] solution gradients with shape [dim, 1, Q]
900a242873SJeremy L Thompson   // in[1] is quadrature data with shape [num_components, Q]
910a242873SJeremy L Thompson   const CeedScalar(*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[1];
9266087c08SValeria Barra 
93d37d859eSJeremy L Thompson   switch (build_data->dim) {
940a242873SJeremy L Thompson     case 1: {
950a242873SJeremy L Thompson       const CeedScalar *ug = in[0];
960a242873SJeremy L Thompson       CeedScalar       *vg = out[0];
9766087c08SValeria Barra 
980a242873SJeremy L Thompson       CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { vg[i] = ug[i] * q_data[0][i]; }  // End of Quadrature Point Loop
990a242873SJeremy L Thompson     } break;
1000a242873SJeremy L Thompson     case 2: {
1010a242873SJeremy L Thompson       const CeedScalar(*ug)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
1020a242873SJeremy L Thompson       CeedScalar(*vg)[CEED_Q_VLA]       = (CeedScalar(*)[CEED_Q_VLA])out[0];
1030a242873SJeremy L Thompson 
1040a242873SJeremy L Thompson       CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
105d1d35e2fSjeremylt         // Read q_data (dXdxdXdx_T symmetric matrix)
10666087c08SValeria Barra         // Stored in Voigt convention
10766087c08SValeria Barra         // 0 2
10866087c08SValeria Barra         // 2 1
1092b730f8bSJeremy L Thompson         const CeedScalar dXdxdXdx_T[2][2] = {
1100a242873SJeremy L Thompson             {q_data[0][i], q_data[2][i]},
1110a242873SJeremy L Thompson             {q_data[2][i], q_data[1][i]}
1122b730f8bSJeremy L Thompson         };
11366087c08SValeria Barra 
1140a242873SJeremy L Thompson         // j = direction of vg
1150a242873SJeremy 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]);
1160a242873SJeremy L Thompson       }  // End of Quadrature Point Loop
1170a242873SJeremy L Thompson     } break;
1180a242873SJeremy L Thompson     case 3: {
1190a242873SJeremy L Thompson       const CeedScalar(*ug)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
1200a242873SJeremy L Thompson       CeedScalar(*vg)[CEED_Q_VLA]       = (CeedScalar(*)[CEED_Q_VLA])out[0];
1210a242873SJeremy L Thompson 
1220a242873SJeremy L Thompson       CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
123d1d35e2fSjeremylt         // Read q_data (dXdxdXdx_T symmetric matrix)
12466087c08SValeria Barra         // Stored in Voigt convention
12566087c08SValeria Barra         // 0 5 4
12666087c08SValeria Barra         // 5 1 3
12766087c08SValeria Barra         // 4 3 2
1282b730f8bSJeremy L Thompson         const CeedScalar dXdxdXdx_T[3][3] = {
1290a242873SJeremy L Thompson             {q_data[0][i], q_data[5][i], q_data[4][i]},
1300a242873SJeremy L Thompson             {q_data[5][i], q_data[1][i], q_data[3][i]},
1310a242873SJeremy L Thompson             {q_data[4][i], q_data[3][i], q_data[2][i]}
13266087c08SValeria Barra         };
1330a242873SJeremy L Thompson 
13466087c08SValeria Barra         // j = direction of vg
1350a242873SJeremy 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]);
13666087c08SValeria Barra       }  // End of Quadrature Point Loop
1370a242873SJeremy L Thompson     } break;
13866087c08SValeria Barra   }
1390b96b02dSJeremy L Thompson   return CEED_ERROR_SUCCESS;
14066087c08SValeria Barra }
141