10b96b02dSJeremy L Thompson // Copyright (c) 2017-2024, 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) { 17*0a242873SJeremy L Thompson // in[0] is Jacobians with shape [dim, dim, Q] 180b96b02dSJeremy L Thompson // in[1] is quadrature weights, size (Q) 19*0a242873SJeremy L Thompson const CeedScalar *w = in[1]; 20*0a242873SJeremy L Thompson CeedScalar(*q_data)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0]; 21*0a242873SJeremy L Thompson struct BuildContext *build_data = (struct BuildContext *)ctx; 22*0a242873SJeremy 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) { 26*0a242873SJeremy L Thompson case 11: { 27*0a242873SJeremy L Thompson const CeedScalar(*J)[1][CEED_Q_VLA] = (const CeedScalar(*)[1][CEED_Q_VLA])in[0]; 28*0a242873SJeremy L Thompson 290b96b02dSJeremy L Thompson CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { 300b96b02dSJeremy L Thompson // Mass 31*0a242873SJeremy L Thompson q_data[0][i] = w[i] * J[0][0][i]; 32*0a242873SJeremy L Thompson 330b96b02dSJeremy L Thompson // Diffusion 34*0a242873SJeremy L Thompson q_data[1][i] = w[i] / J[0][0][i]; 350b96b02dSJeremy L Thompson } // End of Quadrature Point Loop 36*0a242873SJeremy L Thompson } break; 37*0a242873SJeremy L Thompson case 22: { 38*0a242873SJeremy L Thompson const CeedScalar(*J)[2][CEED_Q_VLA] = (const CeedScalar(*)[2][CEED_Q_VLA])in[0]; 39*0a242873SJeremy L Thompson 400b96b02dSJeremy L Thompson CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { 41*0a242873SJeremy L Thompson // J: 0 2 q_data: 0 2 adj(J): J22 -J12 42*0a242873SJeremy L Thompson // 1 3 2 1 -J10 J00 43*0a242873SJeremy L Thompson const CeedScalar J00 = J[0][0][i]; 44*0a242873SJeremy L Thompson const CeedScalar J10 = J[0][1][i]; 45*0a242873SJeremy L Thompson const CeedScalar J01 = J[1][0][i]; 46*0a242873SJeremy L Thompson const CeedScalar J11 = J[1][1][i]; 47*0a242873SJeremy L Thompson const CeedScalar qw = w[i] / (J00 * J11 - J10 * J01); 480b96b02dSJeremy L Thompson 490b96b02dSJeremy L Thompson // Mass 50*0a242873SJeremy L Thompson q_data[0][i] = w[i] * (J00 * J11 - J10 * J01); 51*0a242873SJeremy L Thompson 520b96b02dSJeremy L Thompson // Diffusion 53*0a242873SJeremy L Thompson q_data[1][i] = qw * (J01 * J01 + J11 * J11); 54*0a242873SJeremy L Thompson q_data[2][i] = qw * (J00 * J00 + J10 * J10); 55*0a242873SJeremy L Thompson q_data[3][i] = -qw * (J00 * J01 + J10 * J11); 560b96b02dSJeremy L Thompson } // End of Quadrature Point Loop 57*0a242873SJeremy L Thompson } break; 58*0a242873SJeremy L Thompson case 33: { 59*0a242873SJeremy L Thompson const CeedScalar(*J)[3][CEED_Q_VLA] = (const CeedScalar(*)[3][CEED_Q_VLA])in[0]; 60*0a242873SJeremy 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]; 64*0a242873SJeremy 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] 69*0a242873SJeremy L Thompson A[k][j] = 70*0a242873SJeremy 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) 75*0a242873SJeremy 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 78*0a242873SJeremy 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]); 79*0a242873SJeremy 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 85*0a242873SJeremy 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]); 86*0a242873SJeremy 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]); 87*0a242873SJeremy 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]); 88*0a242873SJeremy 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]); 89*0a242873SJeremy 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]); 90*0a242873SJeremy 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 92*0a242873SJeremy 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; 100*0a242873SJeremy L Thompson // in[1], out[1] solution values with shape [1, 1, Q] 101*0a242873SJeremy L Thompson // in[1], out[1] solution gradients with shape [dim, 1, Q] 102*0a242873SJeremy L Thompson // in[2] is quadrature data with shape [num_components, Q] 103*0a242873SJeremy 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) { 106*0a242873SJeremy L Thompson case 1: { 107*0a242873SJeremy L Thompson const CeedScalar *u = in[0], *ug = in[1]; 108*0a242873SJeremy L Thompson CeedScalar *v = out[0], *vg = out[1]; 109*0a242873SJeremy L Thompson 1100b96b02dSJeremy L Thompson CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { 1110b96b02dSJeremy L Thompson // Mass 112*0a242873SJeremy L Thompson v[i] = q_data[0][i] * u[i]; 113*0a242873SJeremy L Thompson 1140b96b02dSJeremy L Thompson // Diffusion 115*0a242873SJeremy L Thompson vg[i] = q_data[1][i] * ug[i]; 1160b96b02dSJeremy L Thompson } // End of Quadrature Point Loop 117*0a242873SJeremy L Thompson } break; 118*0a242873SJeremy L Thompson case 2: { 119*0a242873SJeremy L Thompson const CeedScalar *u = in[0]; 120*0a242873SJeremy L Thompson const CeedScalar(*ug)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[1]; 121*0a242873SJeremy L Thompson CeedScalar *v = out[0]; 122*0a242873SJeremy L Thompson CeedScalar(*vg)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[1]; 123*0a242873SJeremy L Thompson 1240b96b02dSJeremy L Thompson CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { 1250b96b02dSJeremy L Thompson // Mass 126*0a242873SJeremy 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 132*0a242873SJeremy L Thompson // 23 2 1330b96b02dSJeremy L Thompson const CeedScalar dXdxdXdx_T[2][2] = { 134*0a242873SJeremy L Thompson {q_data[1][i], q_data[3][i]}, 135*0a242873SJeremy L Thompson {q_data[3][i], q_data[2][i]} 1360b96b02dSJeremy L Thompson }; 137*0a242873SJeremy L Thompson 1380b96b02dSJeremy L Thompson // j = direction of vg 139*0a242873SJeremy 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 141*0a242873SJeremy L Thompson } break; 142*0a242873SJeremy L Thompson case 3: { 143*0a242873SJeremy L Thompson const CeedScalar *u = in[0]; 144*0a242873SJeremy L Thompson const CeedScalar(*ug)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[1]; 145*0a242873SJeremy L Thompson CeedScalar *v = out[0]; 146*0a242873SJeremy L Thompson CeedScalar(*vg)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[1]; 147*0a242873SJeremy L Thompson 1480b96b02dSJeremy L Thompson CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { 1490b96b02dSJeremy L Thompson // Mass 150*0a242873SJeremy 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 155*0a242873SJeremy L Thompson // 1 6 5 156*0a242873SJeremy L Thompson // 6 2 4 157*0a242873SJeremy L Thompson // 5 4 3 1580b96b02dSJeremy L Thompson const CeedScalar dXdxdXdx_T[3][3] = { 159*0a242873SJeremy L Thompson {q_data[1][i], q_data[6][i], q_data[5][i]}, 160*0a242873SJeremy L Thompson {q_data[6][i], q_data[2][i], q_data[4][i]}, 161*0a242873SJeremy L Thompson {q_data[5][i], q_data[4][i], q_data[3][i]} 1620b96b02dSJeremy L Thompson }; 163*0a242873SJeremy L Thompson 1640b96b02dSJeremy L Thompson // j = direction of vg 165*0a242873SJeremy 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 167*0a242873SJeremy L Thompson } break; 1680b96b02dSJeremy L Thompson } 1690b96b02dSJeremy L Thompson return CEED_ERROR_SUCCESS; 1700b96b02dSJeremy L Thompson } 171