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