xref: /libCEED/examples/python/qfunctions/ex2-surface.h (revision 7b3ff0698626cc2e5ce463afc10290072fd55c90)
1*7b3ff069SJeremy L Thompson // Copyright (c) 2017-2025, Lawrence Livermore National Security, LLC and other CEED contributors.
2*7b3ff069SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3*7b3ff069SJeremy L Thompson //
4*7b3ff069SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5*7b3ff069SJeremy L Thompson //
6*7b3ff069SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7*7b3ff069SJeremy L Thompson #pragma once
8*7b3ff069SJeremy L Thompson 
9*7b3ff069SJeremy L Thompson #include <ceed/types.h>
10*7b3ff069SJeremy L Thompson #include "ex-common.h"
11*7b3ff069SJeremy L Thompson 
12*7b3ff069SJeremy L Thompson /// libCEED Q-function for building quadrature data for a diffusion operator
13*7b3ff069SJeremy L Thompson CEED_QFUNCTION(build_diff)(void *ctx, const CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
14*7b3ff069SJeremy L Thompson   // in[0] is Jacobians with shape [dim, dim, Q]
15*7b3ff069SJeremy L Thompson   // in[1] is quadrature weights, size (Q)
16*7b3ff069SJeremy L Thompson   const CeedScalar *w             = in[1];
17*7b3ff069SJeremy L Thompson   CeedScalar(*q_data)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
18*7b3ff069SJeremy L Thompson   struct BuildContext *build_data = (struct BuildContext *)ctx;
19*7b3ff069SJeremy L Thompson 
20*7b3ff069SJeremy L Thompson   // At every quadrature point, compute w/det(J).adj(J).adj(J)^T and store
21*7b3ff069SJeremy L Thompson   // the symmetric part of the result.
22*7b3ff069SJeremy L Thompson   switch (build_data->dim + 10 * build_data->space_dim) {
23*7b3ff069SJeremy L Thompson     case 11: {
24*7b3ff069SJeremy L Thompson       const CeedScalar(*J)[1][CEED_Q_VLA] = (const CeedScalar(*)[1][CEED_Q_VLA])in[0];
25*7b3ff069SJeremy L Thompson 
26*7b3ff069SJeremy 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
27*7b3ff069SJeremy L Thompson     } break;
28*7b3ff069SJeremy L Thompson     case 22: {
29*7b3ff069SJeremy L Thompson       const CeedScalar(*J)[2][CEED_Q_VLA] = (const CeedScalar(*)[2][CEED_Q_VLA])in[0];
30*7b3ff069SJeremy L Thompson 
31*7b3ff069SJeremy L Thompson       CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
32*7b3ff069SJeremy L Thompson         // J: 0 2   q_data: 0 2   adj(J):  J11 -J01
33*7b3ff069SJeremy L Thompson         //    1 3           2 1           -J10  J00
34*7b3ff069SJeremy L Thompson         const CeedScalar J00 = J[0][0][i];
35*7b3ff069SJeremy L Thompson         const CeedScalar J10 = J[0][1][i];
36*7b3ff069SJeremy L Thompson         const CeedScalar J01 = J[1][0][i];
37*7b3ff069SJeremy L Thompson         const CeedScalar J11 = J[1][1][i];
38*7b3ff069SJeremy L Thompson         const CeedScalar qw  = w[i] / (J00 * J11 - J10 * J01);
39*7b3ff069SJeremy L Thompson 
40*7b3ff069SJeremy L Thompson         q_data[0][i] = qw * (J01 * J01 + J11 * J11);
41*7b3ff069SJeremy L Thompson         q_data[1][i] = qw * (J00 * J00 + J10 * J10);
42*7b3ff069SJeremy L Thompson         q_data[2][i] = -qw * (J00 * J01 + J10 * J11);
43*7b3ff069SJeremy L Thompson       }  // End of Quadrature Point Loop
44*7b3ff069SJeremy L Thompson     } break;
45*7b3ff069SJeremy L Thompson     case 33: {
46*7b3ff069SJeremy L Thompson       const CeedScalar(*J)[3][CEED_Q_VLA] = (const CeedScalar(*)[3][CEED_Q_VLA])in[0];
47*7b3ff069SJeremy L Thompson 
48*7b3ff069SJeremy L Thompson       CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
49*7b3ff069SJeremy L Thompson         // Compute the adjoint
50*7b3ff069SJeremy L Thompson         CeedScalar A[3][3];
51*7b3ff069SJeremy L Thompson 
52*7b3ff069SJeremy L Thompson         for (CeedInt j = 0; j < 3; j++) {
53*7b3ff069SJeremy L Thompson           for (CeedInt k = 0; k < 3; k++) {
54*7b3ff069SJeremy L Thompson             // Equivalent code with J as a VLA and no mod operations:
55*7b3ff069SJeremy L Thompson             // A[k][j] = J[j+1][k+1]*J[j+2][k+2] - J[j+1][k+2]*J[j+2][k+1]
56*7b3ff069SJeremy L Thompson             A[k][j] =
57*7b3ff069SJeremy 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];
58*7b3ff069SJeremy L Thompson           }
59*7b3ff069SJeremy L Thompson         }
60*7b3ff069SJeremy L Thompson 
61*7b3ff069SJeremy L Thompson         // Compute quadrature weight / det(J)
62*7b3ff069SJeremy 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]);
63*7b3ff069SJeremy L Thompson 
64*7b3ff069SJeremy L Thompson         // Compute geometric factors
65*7b3ff069SJeremy L Thompson         // Stored in Voigt convention
66*7b3ff069SJeremy L Thompson         // 0 5 4
67*7b3ff069SJeremy L Thompson         // 5 1 3
68*7b3ff069SJeremy L Thompson         // 4 3 2
69*7b3ff069SJeremy 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]);
70*7b3ff069SJeremy 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]);
71*7b3ff069SJeremy 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]);
72*7b3ff069SJeremy 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]);
73*7b3ff069SJeremy 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]);
74*7b3ff069SJeremy 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]);
75*7b3ff069SJeremy L Thompson       }  // End of Quadrature Point Loop
76*7b3ff069SJeremy L Thompson     } break;
77*7b3ff069SJeremy L Thompson   }
78*7b3ff069SJeremy L Thompson   return CEED_ERROR_SUCCESS;
79*7b3ff069SJeremy L Thompson }
80*7b3ff069SJeremy L Thompson 
81*7b3ff069SJeremy L Thompson /// libCEED Q-function for applying a diff operator
82*7b3ff069SJeremy L Thompson CEED_QFUNCTION(apply_diff)(void *ctx, const CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
83*7b3ff069SJeremy L Thompson   struct BuildContext *build_data = (struct BuildContext *)ctx;
84*7b3ff069SJeremy L Thompson   // in[0], out[0] solution gradients with shape [dim, 1, Q]
85*7b3ff069SJeremy L Thompson   // in[1] is quadrature data with shape [num_components, Q]
86*7b3ff069SJeremy L Thompson   const CeedScalar(*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[1];
87*7b3ff069SJeremy L Thompson 
88*7b3ff069SJeremy L Thompson   switch (build_data->dim) {
89*7b3ff069SJeremy L Thompson     case 1: {
90*7b3ff069SJeremy L Thompson       const CeedScalar *ug = in[0];
91*7b3ff069SJeremy L Thompson       CeedScalar       *vg = out[0];
92*7b3ff069SJeremy L Thompson 
93*7b3ff069SJeremy L Thompson       CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { vg[i] = ug[i] * q_data[0][i]; }  // End of Quadrature Point Loop
94*7b3ff069SJeremy L Thompson     } break;
95*7b3ff069SJeremy L Thompson     case 2: {
96*7b3ff069SJeremy L Thompson       const CeedScalar(*ug)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
97*7b3ff069SJeremy L Thompson       CeedScalar(*vg)[CEED_Q_VLA]       = (CeedScalar(*)[CEED_Q_VLA])out[0];
98*7b3ff069SJeremy L Thompson 
99*7b3ff069SJeremy L Thompson       CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
100*7b3ff069SJeremy L Thompson         // Read q_data (dXdxdXdx_T symmetric matrix)
101*7b3ff069SJeremy L Thompson         // Stored in Voigt convention
102*7b3ff069SJeremy L Thompson         // 0 2
103*7b3ff069SJeremy L Thompson         // 2 1
104*7b3ff069SJeremy L Thompson         const CeedScalar dXdxdXdx_T[2][2] = {
105*7b3ff069SJeremy L Thompson             {q_data[0][i], q_data[2][i]},
106*7b3ff069SJeremy L Thompson             {q_data[2][i], q_data[1][i]}
107*7b3ff069SJeremy L Thompson         };
108*7b3ff069SJeremy L Thompson 
109*7b3ff069SJeremy L Thompson         // j = direction of vg
110*7b3ff069SJeremy 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]);
111*7b3ff069SJeremy L Thompson       }  // End of Quadrature Point Loop
112*7b3ff069SJeremy L Thompson     } break;
113*7b3ff069SJeremy L Thompson     case 3: {
114*7b3ff069SJeremy L Thompson       const CeedScalar(*ug)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
115*7b3ff069SJeremy L Thompson       CeedScalar(*vg)[CEED_Q_VLA]       = (CeedScalar(*)[CEED_Q_VLA])out[0];
116*7b3ff069SJeremy L Thompson 
117*7b3ff069SJeremy L Thompson       CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
118*7b3ff069SJeremy L Thompson         // Read q_data (dXdxdXdx_T symmetric matrix)
119*7b3ff069SJeremy L Thompson         // Stored in Voigt convention
120*7b3ff069SJeremy L Thompson         // 0 5 4
121*7b3ff069SJeremy L Thompson         // 5 1 3
122*7b3ff069SJeremy L Thompson         // 4 3 2
123*7b3ff069SJeremy L Thompson         const CeedScalar dXdxdXdx_T[3][3] = {
124*7b3ff069SJeremy L Thompson             {q_data[0][i], q_data[5][i], q_data[4][i]},
125*7b3ff069SJeremy L Thompson             {q_data[5][i], q_data[1][i], q_data[3][i]},
126*7b3ff069SJeremy L Thompson             {q_data[4][i], q_data[3][i], q_data[2][i]}
127*7b3ff069SJeremy L Thompson         };
128*7b3ff069SJeremy L Thompson 
129*7b3ff069SJeremy L Thompson         // j = direction of vg
130*7b3ff069SJeremy 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]);
131*7b3ff069SJeremy L Thompson       }  // End of Quadrature Point Loop
132*7b3ff069SJeremy L Thompson     } break;
133*7b3ff069SJeremy L Thompson   }
134*7b3ff069SJeremy L Thompson   return CEED_ERROR_SUCCESS;
135*7b3ff069SJeremy L Thompson }
136