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