19ba83ac0SJeremy L Thompson // Copyright (c) 2017-2026, Lawrence Livermore National Security, LLC and other CEED contributors.
20b96b02dSJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
30b96b02dSJeremy L Thompson //
40b96b02dSJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
50b96b02dSJeremy L Thompson //
60b96b02dSJeremy L Thompson // This file is part of CEED: http://github.com/ceed
70b96b02dSJeremy L Thompson
80b96b02dSJeremy L Thompson #include <ceed/types.h>
90b96b02dSJeremy L Thompson
100b96b02dSJeremy L Thompson /// A structure used to pass additional data to f_build_mass_diff
110b96b02dSJeremy L Thompson struct BuildContext {
120b96b02dSJeremy L Thompson CeedInt dim, space_dim;
130b96b02dSJeremy L Thompson };
140b96b02dSJeremy L Thompson
150b96b02dSJeremy L Thompson /// 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)160b96b02dSJeremy L Thompson CEED_QFUNCTION(build_mass_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]
200b96b02dSJeremy L Thompson // 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
240b96b02dSJeremy L Thompson // At every quadrature point, compute w/det(J).adj(J).adj(J)^T and store
250b96b02dSJeremy L Thompson // the symmetric part of the result.
260b96b02dSJeremy 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];
290a242873SJeremy L Thompson
300b96b02dSJeremy L Thompson CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
310b96b02dSJeremy L Thompson // Mass
320a242873SJeremy L Thompson q_data[0][i] = w[i] * J[0][0][i];
330a242873SJeremy L Thompson
340b96b02dSJeremy L Thompson // Diffusion
350a242873SJeremy L Thompson q_data[1][i] = w[i] / J[0][0][i];
360b96b02dSJeremy L Thompson } // End of Quadrature Point Loop
370a242873SJeremy L Thompson } break;
380a242873SJeremy L Thompson case 22: {
390a242873SJeremy L Thompson const CeedScalar(*J)[2][CEED_Q_VLA] = (const CeedScalar(*)[2][CEED_Q_VLA])in[0];
400a242873SJeremy L Thompson
410b96b02dSJeremy L Thompson CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
420a242873SJeremy L Thompson // J: 0 2 q_data: 0 2 adj(J): J22 -J12
430a242873SJeremy L Thompson // 1 3 2 1 -J10 J00
440a242873SJeremy L Thompson const CeedScalar J00 = J[0][0][i];
450a242873SJeremy L Thompson const CeedScalar J10 = J[0][1][i];
460a242873SJeremy L Thompson const CeedScalar J01 = J[1][0][i];
470a242873SJeremy L Thompson const CeedScalar J11 = J[1][1][i];
480a242873SJeremy L Thompson const CeedScalar qw = w[i] / (J00 * J11 - J10 * J01);
490b96b02dSJeremy L Thompson
500b96b02dSJeremy L Thompson // Mass
510a242873SJeremy L Thompson q_data[0][i] = w[i] * (J00 * J11 - J10 * J01);
520a242873SJeremy L Thompson
530b96b02dSJeremy L Thompson // Diffusion
540a242873SJeremy L Thompson q_data[1][i] = qw * (J01 * J01 + J11 * J11);
550a242873SJeremy L Thompson q_data[2][i] = qw * (J00 * J00 + J10 * J10);
560a242873SJeremy L Thompson q_data[3][i] = -qw * (J00 * J01 + J10 * J11);
570b96b02dSJeremy L Thompson } // End of Quadrature Point Loop
580a242873SJeremy L Thompson } break;
590a242873SJeremy L Thompson case 33: {
600a242873SJeremy L Thompson const CeedScalar(*J)[3][CEED_Q_VLA] = (const CeedScalar(*)[3][CEED_Q_VLA])in[0];
610a242873SJeremy L Thompson
620b96b02dSJeremy L Thompson CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
630b96b02dSJeremy L Thompson // Compute the adjoint
640b96b02dSJeremy L Thompson CeedScalar A[3][3];
650a242873SJeremy L Thompson
660b96b02dSJeremy L Thompson for (CeedInt j = 0; j < 3; j++) {
670b96b02dSJeremy L Thompson for (CeedInt k = 0; k < 3; k++) {
680b96b02dSJeremy L Thompson // Equivalent code with J as a VLA and no mod operations:
690b96b02dSJeremy 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]
700a242873SJeremy L Thompson A[k][j] =
710a242873SJeremy 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];
720b96b02dSJeremy L Thompson }
730b96b02dSJeremy L Thompson }
740b96b02dSJeremy L Thompson
750b96b02dSJeremy L Thompson // Compute quadrature weight / det(J)
760a242873SJeremy 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]);
770b96b02dSJeremy L Thompson
780b96b02dSJeremy L Thompson // Mass
790a242873SJeremy L Thompson 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]);
800a242873SJeremy L Thompson
810b96b02dSJeremy L Thompson // Diffusion
820b96b02dSJeremy L Thompson // Stored in Voigt convention
830b96b02dSJeremy L Thompson // 1 6 5
840b96b02dSJeremy L Thompson // 6 2 4
850b96b02dSJeremy L Thompson // 5 4 3
860a242873SJeremy L Thompson q_data[1][i] = qw * (A[0][0] * A[0][0] + A[0][1] * A[0][1] + A[0][2] * A[0][2]);
870a242873SJeremy L Thompson q_data[2][i] = qw * (A[1][0] * A[1][0] + A[1][1] * A[1][1] + A[1][2] * A[1][2]);
880a242873SJeremy L Thompson q_data[3][i] = qw * (A[2][0] * A[2][0] + A[2][1] * A[2][1] + A[2][2] * A[2][2]);
890a242873SJeremy L Thompson q_data[4][i] = qw * (A[1][0] * A[2][0] + A[1][1] * A[2][1] + A[1][2] * A[2][2]);
900a242873SJeremy L Thompson q_data[5][i] = qw * (A[0][0] * A[2][0] + A[0][1] * A[2][1] + A[0][2] * A[2][2]);
910a242873SJeremy L Thompson q_data[6][i] = qw * (A[0][0] * A[1][0] + A[0][1] * A[1][1] + A[0][2] * A[1][2]);
920b96b02dSJeremy L Thompson } // End of Quadrature Point Loop
930a242873SJeremy L Thompson } break;
940b96b02dSJeremy L Thompson }
950b96b02dSJeremy L Thompson return CEED_ERROR_SUCCESS;
960b96b02dSJeremy L Thompson }
970b96b02dSJeremy L Thompson
980b96b02dSJeremy L Thompson /// libCEED Q-function for applying a mass + diffusion operator
apply_mass_diff(void * ctx,const CeedInt Q,const CeedScalar * const * in,CeedScalar * const * out)990b96b02dSJeremy L Thompson CEED_QFUNCTION(apply_mass_diff)(void *ctx, const CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
1000b96b02dSJeremy L Thompson struct BuildContext *build_data = (struct BuildContext *)ctx;
101*860dc821SJeremy L Thompson
1020a242873SJeremy L Thompson // in[1], out[1] solution values with shape [1, 1, Q]
1030a242873SJeremy L Thompson // in[1], out[1] solution gradients with shape [dim, 1, Q]
1040a242873SJeremy L Thompson // in[2] is quadrature data with shape [num_components, Q]
1050a242873SJeremy L Thompson const CeedScalar(*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2];
1060b96b02dSJeremy L Thompson
1070b96b02dSJeremy L Thompson switch (build_data->dim) {
1080a242873SJeremy L Thompson case 1: {
1090a242873SJeremy L Thompson const CeedScalar *u = in[0], *ug = in[1];
1100a242873SJeremy L Thompson CeedScalar *v = out[0], *vg = out[1];
1110a242873SJeremy L Thompson
1120b96b02dSJeremy L Thompson CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
1130b96b02dSJeremy L Thompson // Mass
1140a242873SJeremy L Thompson v[i] = q_data[0][i] * u[i];
1150a242873SJeremy L Thompson
1160b96b02dSJeremy L Thompson // Diffusion
1170a242873SJeremy L Thompson vg[i] = q_data[1][i] * ug[i];
1180b96b02dSJeremy L Thompson } // End of Quadrature Point Loop
1190a242873SJeremy L Thompson } break;
1200a242873SJeremy L Thompson case 2: {
1210a242873SJeremy L Thompson const CeedScalar *u = in[0];
1220a242873SJeremy L Thompson const CeedScalar(*ug)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[1];
1230a242873SJeremy L Thompson CeedScalar *v = out[0];
1240a242873SJeremy L Thompson CeedScalar(*vg)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[1];
1250a242873SJeremy L Thompson
1260b96b02dSJeremy L Thompson CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
1270b96b02dSJeremy L Thompson // Mass
1280a242873SJeremy L Thompson v[i] = q_data[0][i] * u[i];
1290b96b02dSJeremy L Thompson
1300b96b02dSJeremy L Thompson // Diffusion
1310b96b02dSJeremy L Thompson // Read q_data (dXdxdXdx_T symmetric matrix)
1320b96b02dSJeremy L Thompson // Stored in Voigt convention
1330b96b02dSJeremy L Thompson // 1 3
1340a242873SJeremy L Thompson // 23 2
1350b96b02dSJeremy L Thompson const CeedScalar dXdxdXdx_T[2][2] = {
1360a242873SJeremy L Thompson {q_data[1][i], q_data[3][i]},
1370a242873SJeremy L Thompson {q_data[3][i], q_data[2][i]}
1380b96b02dSJeremy L Thompson };
1390a242873SJeremy L Thompson
1400b96b02dSJeremy L Thompson // j = direction of vg
1410a242873SJeremy 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]);
1420b96b02dSJeremy L Thompson } // End of Quadrature Point Loop
1430a242873SJeremy L Thompson } break;
1440a242873SJeremy L Thompson case 3: {
1450a242873SJeremy L Thompson const CeedScalar *u = in[0];
1460a242873SJeremy L Thompson const CeedScalar(*ug)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[1];
1470a242873SJeremy L Thompson CeedScalar *v = out[0];
1480a242873SJeremy L Thompson CeedScalar(*vg)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[1];
1490a242873SJeremy L Thompson
1500b96b02dSJeremy L Thompson CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
1510b96b02dSJeremy L Thompson // Mass
1520a242873SJeremy L Thompson v[i] = q_data[0][i] * u[i];
1530b96b02dSJeremy L Thompson
1540b96b02dSJeremy L Thompson // Diffusion
1550b96b02dSJeremy L Thompson // Read q_data (dXdxdXdx_T symmetric matrix)
1560b96b02dSJeremy L Thompson // Stored in Voigt convention
1570a242873SJeremy L Thompson // 1 6 5
1580a242873SJeremy L Thompson // 6 2 4
1590a242873SJeremy L Thompson // 5 4 3
1600b96b02dSJeremy L Thompson const CeedScalar dXdxdXdx_T[3][3] = {
1610a242873SJeremy L Thompson {q_data[1][i], q_data[6][i], q_data[5][i]},
1620a242873SJeremy L Thompson {q_data[6][i], q_data[2][i], q_data[4][i]},
1630a242873SJeremy L Thompson {q_data[5][i], q_data[4][i], q_data[3][i]}
1640b96b02dSJeremy L Thompson };
1650a242873SJeremy L Thompson
1660b96b02dSJeremy L Thompson // j = direction of vg
1670a242873SJeremy 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]);
1680b96b02dSJeremy L Thompson } // End of Quadrature Point Loop
1690a242873SJeremy L Thompson } break;
1700b96b02dSJeremy L Thompson }
1710b96b02dSJeremy L Thompson return CEED_ERROR_SUCCESS;
1720b96b02dSJeremy L Thompson }
173