1*9ba83ac0SJeremy 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 160b96b02dSJeremy L Thompson CEED_QFUNCTION(build_mass_diff)(void *ctx, const CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) { 170a242873SJeremy L Thompson // in[0] is Jacobians with shape [dim, dim, Q] 180b96b02dSJeremy L Thompson // in[1] is quadrature weights, size (Q) 190a242873SJeremy L Thompson const CeedScalar *w = in[1]; 200a242873SJeremy L Thompson CeedScalar(*q_data)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0]; 210a242873SJeremy L Thompson struct BuildContext *build_data = (struct BuildContext *)ctx; 220a242873SJeremy L Thompson 230b96b02dSJeremy L Thompson // At every quadrature point, compute w/det(J).adj(J).adj(J)^T and store 240b96b02dSJeremy L Thompson // the symmetric part of the result. 250b96b02dSJeremy L Thompson switch (build_data->dim + 10 * build_data->space_dim) { 260a242873SJeremy L Thompson case 11: { 270a242873SJeremy L Thompson const CeedScalar(*J)[1][CEED_Q_VLA] = (const CeedScalar(*)[1][CEED_Q_VLA])in[0]; 280a242873SJeremy L Thompson 290b96b02dSJeremy L Thompson CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { 300b96b02dSJeremy L Thompson // Mass 310a242873SJeremy L Thompson q_data[0][i] = w[i] * J[0][0][i]; 320a242873SJeremy L Thompson 330b96b02dSJeremy L Thompson // Diffusion 340a242873SJeremy L Thompson q_data[1][i] = w[i] / J[0][0][i]; 350b96b02dSJeremy L Thompson } // End of Quadrature Point Loop 360a242873SJeremy L Thompson } break; 370a242873SJeremy L Thompson case 22: { 380a242873SJeremy L Thompson const CeedScalar(*J)[2][CEED_Q_VLA] = (const CeedScalar(*)[2][CEED_Q_VLA])in[0]; 390a242873SJeremy L Thompson 400b96b02dSJeremy L Thompson CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { 410a242873SJeremy L Thompson // J: 0 2 q_data: 0 2 adj(J): J22 -J12 420a242873SJeremy L Thompson // 1 3 2 1 -J10 J00 430a242873SJeremy L Thompson const CeedScalar J00 = J[0][0][i]; 440a242873SJeremy L Thompson const CeedScalar J10 = J[0][1][i]; 450a242873SJeremy L Thompson const CeedScalar J01 = J[1][0][i]; 460a242873SJeremy L Thompson const CeedScalar J11 = J[1][1][i]; 470a242873SJeremy L Thompson const CeedScalar qw = w[i] / (J00 * J11 - J10 * J01); 480b96b02dSJeremy L Thompson 490b96b02dSJeremy L Thompson // Mass 500a242873SJeremy L Thompson q_data[0][i] = w[i] * (J00 * J11 - J10 * J01); 510a242873SJeremy L Thompson 520b96b02dSJeremy L Thompson // Diffusion 530a242873SJeremy L Thompson q_data[1][i] = qw * (J01 * J01 + J11 * J11); 540a242873SJeremy L Thompson q_data[2][i] = qw * (J00 * J00 + J10 * J10); 550a242873SJeremy L Thompson q_data[3][i] = -qw * (J00 * J01 + J10 * J11); 560b96b02dSJeremy L Thompson } // End of Quadrature Point Loop 570a242873SJeremy L Thompson } break; 580a242873SJeremy L Thompson case 33: { 590a242873SJeremy L Thompson const CeedScalar(*J)[3][CEED_Q_VLA] = (const CeedScalar(*)[3][CEED_Q_VLA])in[0]; 600a242873SJeremy L Thompson 610b96b02dSJeremy L Thompson CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { 620b96b02dSJeremy L Thompson // Compute the adjoint 630b96b02dSJeremy L Thompson CeedScalar A[3][3]; 640a242873SJeremy L Thompson 650b96b02dSJeremy L Thompson for (CeedInt j = 0; j < 3; j++) { 660b96b02dSJeremy L Thompson for (CeedInt k = 0; k < 3; k++) { 670b96b02dSJeremy L Thompson // Equivalent code with J as a VLA and no mod operations: 680b96b02dSJeremy 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] 690a242873SJeremy L Thompson A[k][j] = 700a242873SJeremy 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]; 710b96b02dSJeremy L Thompson } 720b96b02dSJeremy L Thompson } 730b96b02dSJeremy L Thompson 740b96b02dSJeremy L Thompson // Compute quadrature weight / det(J) 750a242873SJeremy 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]); 760b96b02dSJeremy L Thompson 770b96b02dSJeremy L Thompson // Mass 780a242873SJeremy 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]); 790a242873SJeremy L Thompson 800b96b02dSJeremy L Thompson // Diffusion 810b96b02dSJeremy L Thompson // Stored in Voigt convention 820b96b02dSJeremy L Thompson // 1 6 5 830b96b02dSJeremy L Thompson // 6 2 4 840b96b02dSJeremy L Thompson // 5 4 3 850a242873SJeremy 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]); 860a242873SJeremy 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]); 870a242873SJeremy 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]); 880a242873SJeremy 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]); 890a242873SJeremy 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]); 900a242873SJeremy 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]); 910b96b02dSJeremy L Thompson } // End of Quadrature Point Loop 920a242873SJeremy L Thompson } break; 930b96b02dSJeremy L Thompson } 940b96b02dSJeremy L Thompson return CEED_ERROR_SUCCESS; 950b96b02dSJeremy L Thompson } 960b96b02dSJeremy L Thompson 970b96b02dSJeremy L Thompson /// libCEED Q-function for applying a mass + diffusion operator 980b96b02dSJeremy L Thompson CEED_QFUNCTION(apply_mass_diff)(void *ctx, const CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) { 990b96b02dSJeremy L Thompson struct BuildContext *build_data = (struct BuildContext *)ctx; 1000a242873SJeremy L Thompson // in[1], out[1] solution values with shape [1, 1, Q] 1010a242873SJeremy L Thompson // in[1], out[1] solution gradients with shape [dim, 1, Q] 1020a242873SJeremy L Thompson // in[2] is quadrature data with shape [num_components, Q] 1030a242873SJeremy L Thompson const CeedScalar(*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2]; 1040b96b02dSJeremy L Thompson 1050b96b02dSJeremy L Thompson switch (build_data->dim) { 1060a242873SJeremy L Thompson case 1: { 1070a242873SJeremy L Thompson const CeedScalar *u = in[0], *ug = in[1]; 1080a242873SJeremy L Thompson CeedScalar *v = out[0], *vg = out[1]; 1090a242873SJeremy L Thompson 1100b96b02dSJeremy L Thompson CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { 1110b96b02dSJeremy L Thompson // Mass 1120a242873SJeremy L Thompson v[i] = q_data[0][i] * u[i]; 1130a242873SJeremy L Thompson 1140b96b02dSJeremy L Thompson // Diffusion 1150a242873SJeremy L Thompson vg[i] = q_data[1][i] * ug[i]; 1160b96b02dSJeremy L Thompson } // End of Quadrature Point Loop 1170a242873SJeremy L Thompson } break; 1180a242873SJeremy L Thompson case 2: { 1190a242873SJeremy L Thompson const CeedScalar *u = in[0]; 1200a242873SJeremy L Thompson const CeedScalar(*ug)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[1]; 1210a242873SJeremy L Thompson CeedScalar *v = out[0]; 1220a242873SJeremy L Thompson CeedScalar(*vg)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[1]; 1230a242873SJeremy L Thompson 1240b96b02dSJeremy L Thompson CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { 1250b96b02dSJeremy L Thompson // Mass 1260a242873SJeremy L Thompson v[i] = q_data[0][i] * u[i]; 1270b96b02dSJeremy L Thompson 1280b96b02dSJeremy L Thompson // Diffusion 1290b96b02dSJeremy L Thompson // Read q_data (dXdxdXdx_T symmetric matrix) 1300b96b02dSJeremy L Thompson // Stored in Voigt convention 1310b96b02dSJeremy L Thompson // 1 3 1320a242873SJeremy L Thompson // 23 2 1330b96b02dSJeremy L Thompson const CeedScalar dXdxdXdx_T[2][2] = { 1340a242873SJeremy L Thompson {q_data[1][i], q_data[3][i]}, 1350a242873SJeremy L Thompson {q_data[3][i], q_data[2][i]} 1360b96b02dSJeremy L Thompson }; 1370a242873SJeremy L Thompson 1380b96b02dSJeremy L Thompson // j = direction of vg 1390a242873SJeremy 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]); 1400b96b02dSJeremy L Thompson } // End of Quadrature Point Loop 1410a242873SJeremy L Thompson } break; 1420a242873SJeremy L Thompson case 3: { 1430a242873SJeremy L Thompson const CeedScalar *u = in[0]; 1440a242873SJeremy L Thompson const CeedScalar(*ug)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[1]; 1450a242873SJeremy L Thompson CeedScalar *v = out[0]; 1460a242873SJeremy L Thompson CeedScalar(*vg)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[1]; 1470a242873SJeremy L Thompson 1480b96b02dSJeremy L Thompson CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { 1490b96b02dSJeremy L Thompson // Mass 1500a242873SJeremy L Thompson v[i] = q_data[0][i] * u[i]; 1510b96b02dSJeremy L Thompson 1520b96b02dSJeremy L Thompson // Diffusion 1530b96b02dSJeremy L Thompson // Read q_data (dXdxdXdx_T symmetric matrix) 1540b96b02dSJeremy L Thompson // Stored in Voigt convention 1550a242873SJeremy L Thompson // 1 6 5 1560a242873SJeremy L Thompson // 6 2 4 1570a242873SJeremy L Thompson // 5 4 3 1580b96b02dSJeremy L Thompson const CeedScalar dXdxdXdx_T[3][3] = { 1590a242873SJeremy L Thompson {q_data[1][i], q_data[6][i], q_data[5][i]}, 1600a242873SJeremy L Thompson {q_data[6][i], q_data[2][i], q_data[4][i]}, 1610a242873SJeremy L Thompson {q_data[5][i], q_data[4][i], q_data[3][i]} 1620b96b02dSJeremy L Thompson }; 1630a242873SJeremy L Thompson 1640b96b02dSJeremy L Thompson // j = direction of vg 1650a242873SJeremy 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]); 1660b96b02dSJeremy L Thompson } // End of Quadrature Point Loop 1670a242873SJeremy L Thompson } break; 1680b96b02dSJeremy L Thompson } 1690b96b02dSJeremy L Thompson return CEED_ERROR_SUCCESS; 1700b96b02dSJeremy L Thompson } 171