xref: /libCEED/examples/ceed/ex3-volume.h (revision d4cc18453651bd0f94c1a2e078b2646a92dafdcc)
1 // Copyright (c) 2017-2026, Lawrence Livermore National Security, LLC and other CEED contributors.
2 // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3 //
4 // SPDX-License-Identifier: BSD-2-Clause
5 //
6 // This file is part of CEED:  http://github.com/ceed
7 
8 #include <ceed/types.h>
9 
10 /// A structure used to pass additional data to f_build_mass_diff
11 struct BuildContext {
12   CeedInt dim, space_dim;
13 };
14 
15 /// libCEED Q-function for building quadrature data for a mass + diffusion operator
16 CEED_QFUNCTION(build_mass_diff)(void *ctx, const CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
17   // in[0] is Jacobians with shape [dim, dim, Q]
18   // in[1] is quadrature weights, size (Q)
19   const CeedScalar *w             = in[1];
20   CeedScalar(*q_data)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
21   struct BuildContext *build_data = (struct BuildContext *)ctx;
22 
23   // At every quadrature point, compute w/det(J).adj(J).adj(J)^T and store
24   // the symmetric part of the result.
25   switch (build_data->dim + 10 * build_data->space_dim) {
26     case 11: {
27       const CeedScalar(*J)[1][CEED_Q_VLA] = (const CeedScalar(*)[1][CEED_Q_VLA])in[0];
28 
29       CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
30         // Mass
31         q_data[0][i] = w[i] * J[0][0][i];
32 
33         // Diffusion
34         q_data[1][i] = w[i] / J[0][0][i];
35       }  // End of Quadrature Point Loop
36     } break;
37     case 22: {
38       const CeedScalar(*J)[2][CEED_Q_VLA] = (const CeedScalar(*)[2][CEED_Q_VLA])in[0];
39 
40       CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
41         // J: 0 2   q_data: 0 2   adj(J):  J22 -J12
42         //    1 3           2 1           -J10  J00
43         const CeedScalar J00 = J[0][0][i];
44         const CeedScalar J10 = J[0][1][i];
45         const CeedScalar J01 = J[1][0][i];
46         const CeedScalar J11 = J[1][1][i];
47         const CeedScalar qw  = w[i] / (J00 * J11 - J10 * J01);
48 
49         // Mass
50         q_data[0][i] = w[i] * (J00 * J11 - J10 * J01);
51 
52         // Diffusion
53         q_data[1][i] = qw * (J01 * J01 + J11 * J11);
54         q_data[2][i] = qw * (J00 * J00 + J10 * J10);
55         q_data[3][i] = -qw * (J00 * J01 + J10 * J11);
56       }  // End of Quadrature Point Loop
57     } break;
58     case 33: {
59       const CeedScalar(*J)[3][CEED_Q_VLA] = (const CeedScalar(*)[3][CEED_Q_VLA])in[0];
60 
61       CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
62         // Compute the adjoint
63         CeedScalar A[3][3];
64 
65         for (CeedInt j = 0; j < 3; j++) {
66           for (CeedInt k = 0; k < 3; k++) {
67             // Equivalent code with J as a VLA and no mod operations:
68             // A[k][j] = J[j+1][k+1]*J[j+2][k+2] - J[j+1][k+2]*J[j+2][k+1]
69             A[k][j] =
70                 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];
71           }
72         }
73 
74         // Compute quadrature weight / det(J)
75         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]);
76 
77         // Mass
78         q_data[0][i] = w[i] * (J[0][0][i] * A[0][0] + J[0][1][i] * A[0][1] + J[0][2][i] * A[0][2]);
79 
80         // Diffusion
81         // Stored in Voigt convention
82         // 1 6 5
83         // 6 2 4
84         // 5 4 3
85         q_data[1][i] = qw * (A[0][0] * A[0][0] + A[0][1] * A[0][1] + A[0][2] * A[0][2]);
86         q_data[2][i] = qw * (A[1][0] * A[1][0] + A[1][1] * A[1][1] + A[1][2] * A[1][2]);
87         q_data[3][i] = qw * (A[2][0] * A[2][0] + A[2][1] * A[2][1] + A[2][2] * A[2][2]);
88         q_data[4][i] = qw * (A[1][0] * A[2][0] + A[1][1] * A[2][1] + A[1][2] * A[2][2]);
89         q_data[5][i] = qw * (A[0][0] * A[2][0] + A[0][1] * A[2][1] + A[0][2] * A[2][2]);
90         q_data[6][i] = qw * (A[0][0] * A[1][0] + A[0][1] * A[1][1] + A[0][2] * A[1][2]);
91       }  // End of Quadrature Point Loop
92     } break;
93   }
94   return CEED_ERROR_SUCCESS;
95 }
96 
97 /// libCEED Q-function for applying a mass + diffusion operator
98 CEED_QFUNCTION(apply_mass_diff)(void *ctx, const CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
99   struct BuildContext *build_data = (struct BuildContext *)ctx;
100   // in[1], out[1] solution values with shape [1, 1, Q]
101   // in[1], out[1] solution gradients with shape [dim, 1, Q]
102   // in[2] is quadrature data with shape [num_components, Q]
103   const CeedScalar(*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2];
104 
105   switch (build_data->dim) {
106     case 1: {
107       const CeedScalar *u = in[0], *ug = in[1];
108       CeedScalar       *v = out[0], *vg = out[1];
109 
110       CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
111         // Mass
112         v[i] = q_data[0][i] * u[i];
113 
114         // Diffusion
115         vg[i] = q_data[1][i] * ug[i];
116       }  // End of Quadrature Point Loop
117     } break;
118     case 2: {
119       const CeedScalar *u               = in[0];
120       const CeedScalar(*ug)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[1];
121       CeedScalar *v                     = out[0];
122       CeedScalar(*vg)[CEED_Q_VLA]       = (CeedScalar(*)[CEED_Q_VLA])out[1];
123 
124       CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
125         // Mass
126         v[i] = q_data[0][i] * u[i];
127 
128         // Diffusion
129         // Read q_data (dXdxdXdx_T symmetric matrix)
130         // Stored in Voigt convention
131         // 1 3
132         // 23 2
133         const CeedScalar dXdxdXdx_T[2][2] = {
134             {q_data[1][i], q_data[3][i]},
135             {q_data[3][i], q_data[2][i]}
136         };
137 
138         // j = direction of vg
139         for (int j = 0; j < 2; j++) vg[j][i] = (ug[0][i] * dXdxdXdx_T[0][j] + ug[1][i] * dXdxdXdx_T[1][j]);
140       }  // End of Quadrature Point Loop
141     } break;
142     case 3: {
143       const CeedScalar *u               = in[0];
144       const CeedScalar(*ug)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[1];
145       CeedScalar *v                     = out[0];
146       CeedScalar(*vg)[CEED_Q_VLA]       = (CeedScalar(*)[CEED_Q_VLA])out[1];
147 
148       CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
149         // Mass
150         v[i] = q_data[0][i] * u[i];
151 
152         // Diffusion
153         // Read q_data (dXdxdXdx_T symmetric matrix)
154         // Stored in Voigt convention
155         // 1 6 5
156         // 6 2 4
157         // 5 4 3
158         const CeedScalar dXdxdXdx_T[3][3] = {
159             {q_data[1][i], q_data[6][i], q_data[5][i]},
160             {q_data[6][i], q_data[2][i], q_data[4][i]},
161             {q_data[5][i], q_data[4][i], q_data[3][i]}
162         };
163 
164         // j = direction of vg
165         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]);
166       }  // End of Quadrature Point Loop
167     } break;
168   }
169   return CEED_ERROR_SUCCESS;
170 }
171