xref: /libCEED/examples/python/qfunctions/ex3-volume.h (revision d4cc18453651bd0f94c1a2e078b2646a92dafdcc)
1*9ba83ac0SJeremy L Thompson // Copyright (c) 2017-2026, Lawrence Livermore National Security, LLC and other CEED contributors.
27b3ff069SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
37b3ff069SJeremy L Thompson //
47b3ff069SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
57b3ff069SJeremy L Thompson //
67b3ff069SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
77b3ff069SJeremy L Thompson #pragma once
87b3ff069SJeremy L Thompson 
97b3ff069SJeremy L Thompson #include <ceed.h>
107b3ff069SJeremy L Thompson #include "ex-common.h"
117b3ff069SJeremy L Thompson 
127b3ff069SJeremy 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)137b3ff069SJeremy L Thompson CEED_QFUNCTION(build_mass_diff)(void *ctx, const CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
147b3ff069SJeremy L Thompson   // in[0] is Jacobians with shape [dim, dim, Q]
157b3ff069SJeremy L Thompson   // in[1] is quadrature weights, size (Q)
167b3ff069SJeremy L Thompson   const CeedScalar *w             = in[1];
177b3ff069SJeremy L Thompson   CeedScalar(*q_data)[CEED_Q_VLA] = (CeedScalar(*)[CEED_Q_VLA])out[0];
187b3ff069SJeremy L Thompson   struct BuildContext *build_data = (struct BuildContext *)ctx;
197b3ff069SJeremy L Thompson 
207b3ff069SJeremy L Thompson   // At every quadrature point, compute w/det(J).adj(J).adj(J)^T and store
217b3ff069SJeremy L Thompson   // the symmetric part of the result.
227b3ff069SJeremy L Thompson   switch (build_data->dim + 10 * build_data->space_dim) {
237b3ff069SJeremy L Thompson     case 11: {  // dim = 1, space_dim = 1
247b3ff069SJeremy L Thompson       const CeedScalar(*J)[1][CEED_Q_VLA] = (const CeedScalar(*)[1][CEED_Q_VLA])in[0];
257b3ff069SJeremy L Thompson 
267b3ff069SJeremy L Thompson       CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
277b3ff069SJeremy L Thompson         // Mass
287b3ff069SJeremy L Thompson         q_data[0][i] = w[i] * J[0][0][i];
297b3ff069SJeremy L Thompson 
307b3ff069SJeremy L Thompson         // Diffusion
317b3ff069SJeremy L Thompson         q_data[1][i] = w[i] / J[0][0][i];
327b3ff069SJeremy L Thompson       }
337b3ff069SJeremy L Thompson     } break;
347b3ff069SJeremy L Thompson     case 22: {  // dim = 2, space_dim = 2
357b3ff069SJeremy L Thompson       const CeedScalar(*J)[2][CEED_Q_VLA] = (const CeedScalar(*)[2][CEED_Q_VLA])in[0];
367b3ff069SJeremy L Thompson 
377b3ff069SJeremy L Thompson       CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
387b3ff069SJeremy L Thompson         // J: 0 2   q_data: 0 2   adj(J):  J22 -J12
397b3ff069SJeremy L Thompson         //    1 3           2 1           -J10  J00
407b3ff069SJeremy L Thompson         const CeedScalar J00 = J[0][0][i];
417b3ff069SJeremy L Thompson         const CeedScalar J10 = J[0][1][i];
427b3ff069SJeremy L Thompson         const CeedScalar J01 = J[1][0][i];
437b3ff069SJeremy L Thompson         const CeedScalar J11 = J[1][1][i];
447b3ff069SJeremy L Thompson         const CeedScalar qw  = w[i] / (J00 * J11 - J10 * J01);
457b3ff069SJeremy L Thompson 
467b3ff069SJeremy L Thompson         // Mass
477b3ff069SJeremy L Thompson         q_data[0][i] = w[i] * (J00 * J11 - J10 * J01);
487b3ff069SJeremy L Thompson 
497b3ff069SJeremy L Thompson         // Diffusion
507b3ff069SJeremy L Thompson         q_data[1][i] = qw * (J01 * J01 + J11 * J11);
517b3ff069SJeremy L Thompson         q_data[2][i] = qw * (J00 * J00 + J10 * J10);
527b3ff069SJeremy L Thompson         q_data[3][i] = -qw * (J00 * J01 + J10 * J11);
537b3ff069SJeremy L Thompson       }
547b3ff069SJeremy L Thompson     } break;
557b3ff069SJeremy L Thompson     case 33: {  // dim = 3, space_dim = 3
567b3ff069SJeremy L Thompson       const CeedScalar(*J)[3][CEED_Q_VLA] = (const CeedScalar(*)[3][CEED_Q_VLA])in[0];
577b3ff069SJeremy L Thompson 
587b3ff069SJeremy L Thompson       CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
597b3ff069SJeremy L Thompson         // Compute the adjoint
607b3ff069SJeremy L Thompson         CeedScalar A[3][3];
617b3ff069SJeremy L Thompson         for (CeedInt j = 0; j < 3; j++) {
627b3ff069SJeremy L Thompson           for (CeedInt k = 0; k < 3; k++) {
637b3ff069SJeremy L Thompson             A[k][j] =
647b3ff069SJeremy 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];
657b3ff069SJeremy L Thompson           }
667b3ff069SJeremy L Thompson         }
677b3ff069SJeremy L Thompson 
687b3ff069SJeremy L Thompson         // Compute quadrature weight / det(J)
697b3ff069SJeremy 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]);
707b3ff069SJeremy L Thompson 
717b3ff069SJeremy L Thompson         // Mass
727b3ff069SJeremy 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]);
737b3ff069SJeremy L Thompson 
747b3ff069SJeremy L Thompson         // Diffusion
757b3ff069SJeremy L Thompson         // Stored in Voigt convention
767b3ff069SJeremy L Thompson         // 1 6 5
777b3ff069SJeremy L Thompson         // 6 2 4
787b3ff069SJeremy L Thompson         // 5 4 3
797b3ff069SJeremy 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]);
807b3ff069SJeremy 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]);
817b3ff069SJeremy 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]);
827b3ff069SJeremy 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]);
837b3ff069SJeremy 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]);
847b3ff069SJeremy 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]);
857b3ff069SJeremy L Thompson       }
867b3ff069SJeremy L Thompson     } break;
877b3ff069SJeremy L Thompson   }
887b3ff069SJeremy L Thompson   return CEED_ERROR_SUCCESS;
897b3ff069SJeremy L Thompson }
907b3ff069SJeremy L Thompson 
917b3ff069SJeremy 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)927b3ff069SJeremy L Thompson CEED_QFUNCTION(apply_mass_diff)(void *ctx, const CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
937b3ff069SJeremy L Thompson   struct BuildContext *build_data = (struct BuildContext *)ctx;
947b3ff069SJeremy L Thompson   // in[0], out[0] solution values with shape [1, 1, Q]
957b3ff069SJeremy L Thompson   // in[1], out[1] solution gradients with shape [dim, 1, Q]
967b3ff069SJeremy L Thompson   // in[2] is quadrature data with shape [num_components, Q]
977b3ff069SJeremy L Thompson   const CeedScalar(*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[2];
987b3ff069SJeremy L Thompson 
997b3ff069SJeremy L Thompson   switch (build_data->dim) {
1007b3ff069SJeremy L Thompson     case 1: {
1017b3ff069SJeremy L Thompson       const CeedScalar *u = in[0], *ug = in[1];
1027b3ff069SJeremy L Thompson       CeedScalar       *v = out[0], *vg = out[1];
1037b3ff069SJeremy L Thompson 
1047b3ff069SJeremy L Thompson       CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
1057b3ff069SJeremy L Thompson         // Mass
1067b3ff069SJeremy L Thompson         v[i] = q_data[0][i] * u[i];
1077b3ff069SJeremy L Thompson 
1087b3ff069SJeremy L Thompson         // Diffusion
1097b3ff069SJeremy L Thompson         vg[i] = q_data[1][i] * ug[i];
1107b3ff069SJeremy L Thompson       }
1117b3ff069SJeremy L Thompson     } break;
1127b3ff069SJeremy L Thompson     case 2: {
1137b3ff069SJeremy L Thompson       const CeedScalar *u               = in[0];
1147b3ff069SJeremy L Thompson       const CeedScalar(*ug)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[1];
1157b3ff069SJeremy L Thompson       CeedScalar *v                     = out[0];
1167b3ff069SJeremy L Thompson       CeedScalar(*vg)[CEED_Q_VLA]       = (CeedScalar(*)[CEED_Q_VLA])out[1];
1177b3ff069SJeremy L Thompson 
1187b3ff069SJeremy L Thompson       CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
1197b3ff069SJeremy L Thompson         // Mass
1207b3ff069SJeremy L Thompson         v[i] = q_data[0][i] * u[i];
1217b3ff069SJeremy L Thompson 
1227b3ff069SJeremy L Thompson         // Diffusion
1237b3ff069SJeremy L Thompson         // Read q_data (dXdxdXdx_T symmetric matrix)
1247b3ff069SJeremy L Thompson         // Stored in Voigt convention
1257b3ff069SJeremy L Thompson         // 1 3
1267b3ff069SJeremy L Thompson         // 3 2
1277b3ff069SJeremy L Thompson         const CeedScalar dXdxdXdx_T[2][2] = {
1287b3ff069SJeremy L Thompson             {q_data[1][i], q_data[3][i]},
1297b3ff069SJeremy L Thompson             {q_data[3][i], q_data[2][i]}
1307b3ff069SJeremy L Thompson         };
1317b3ff069SJeremy L Thompson 
1327b3ff069SJeremy L Thompson         // j = direction of vg
1337b3ff069SJeremy L Thompson         for (int j = 0; j < 2; j++) {
1347b3ff069SJeremy L Thompson           vg[j][i] = (ug[0][i] * dXdxdXdx_T[0][j] + ug[1][i] * dXdxdXdx_T[1][j]);
1357b3ff069SJeremy L Thompson         }
1367b3ff069SJeremy L Thompson       }
1377b3ff069SJeremy L Thompson     } break;
1387b3ff069SJeremy L Thompson     case 3: {
1397b3ff069SJeremy L Thompson       const CeedScalar *u               = in[0];
1407b3ff069SJeremy L Thompson       const CeedScalar(*ug)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[1];
1417b3ff069SJeremy L Thompson       CeedScalar *v                     = out[0];
1427b3ff069SJeremy L Thompson       CeedScalar(*vg)[CEED_Q_VLA]       = (CeedScalar(*)[CEED_Q_VLA])out[1];
1437b3ff069SJeremy L Thompson 
1447b3ff069SJeremy L Thompson       CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
1457b3ff069SJeremy L Thompson         // Mass
1467b3ff069SJeremy L Thompson         v[i] = q_data[0][i] * u[i];
1477b3ff069SJeremy L Thompson 
1487b3ff069SJeremy L Thompson         // Diffusion
1497b3ff069SJeremy L Thompson         // Read q_data (dXdxdXdx_T symmetric matrix)
1507b3ff069SJeremy L Thompson         // Stored in Voigt convention
1517b3ff069SJeremy L Thompson         // 1 6 5
1527b3ff069SJeremy L Thompson         // 6 2 4
1537b3ff069SJeremy L Thompson         // 5 4 3
1547b3ff069SJeremy L Thompson         const CeedScalar dXdxdXdx_T[3][3] = {
1557b3ff069SJeremy L Thompson             {q_data[1][i], q_data[6][i], q_data[5][i]},
1567b3ff069SJeremy L Thompson             {q_data[6][i], q_data[2][i], q_data[4][i]},
1577b3ff069SJeremy L Thompson             {q_data[5][i], q_data[4][i], q_data[3][i]}
1587b3ff069SJeremy L Thompson         };
1597b3ff069SJeremy L Thompson 
1607b3ff069SJeremy L Thompson         // j = direction of vg
1617b3ff069SJeremy L Thompson         for (int j = 0; j < 3; j++) {
1627b3ff069SJeremy 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]);
1637b3ff069SJeremy L Thompson         }
1647b3ff069SJeremy L Thompson       }
1657b3ff069SJeremy L Thompson     } break;
1667b3ff069SJeremy L Thompson   }
1677b3ff069SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1687b3ff069SJeremy L Thompson }
169