xref: /libCEED/examples/python/qfunctions/ex2-surface.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/types.h>
107b3ff069SJeremy L Thompson #include "ex-common.h"
117b3ff069SJeremy L Thompson 
127b3ff069SJeremy L Thompson /// libCEED Q-function for building quadrature data for a diffusion operator
build_diff(void * ctx,const CeedInt Q,const CeedScalar * const * in,CeedScalar * const * out)137b3ff069SJeremy L Thompson CEED_QFUNCTION(build_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: {
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++) { q_data[0][i] = w[i] / J[0][0][i]; }  // End of Quadrature Point Loop
277b3ff069SJeremy L Thompson     } break;
287b3ff069SJeremy L Thompson     case 22: {
297b3ff069SJeremy L Thompson       const CeedScalar(*J)[2][CEED_Q_VLA] = (const CeedScalar(*)[2][CEED_Q_VLA])in[0];
307b3ff069SJeremy L Thompson 
317b3ff069SJeremy L Thompson       CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
327b3ff069SJeremy L Thompson         // J: 0 2   q_data: 0 2   adj(J):  J11 -J01
337b3ff069SJeremy L Thompson         //    1 3           2 1           -J10  J00
347b3ff069SJeremy L Thompson         const CeedScalar J00 = J[0][0][i];
357b3ff069SJeremy L Thompson         const CeedScalar J10 = J[0][1][i];
367b3ff069SJeremy L Thompson         const CeedScalar J01 = J[1][0][i];
377b3ff069SJeremy L Thompson         const CeedScalar J11 = J[1][1][i];
387b3ff069SJeremy L Thompson         const CeedScalar qw  = w[i] / (J00 * J11 - J10 * J01);
397b3ff069SJeremy L Thompson 
407b3ff069SJeremy L Thompson         q_data[0][i] = qw * (J01 * J01 + J11 * J11);
417b3ff069SJeremy L Thompson         q_data[1][i] = qw * (J00 * J00 + J10 * J10);
427b3ff069SJeremy L Thompson         q_data[2][i] = -qw * (J00 * J01 + J10 * J11);
437b3ff069SJeremy L Thompson       }  // End of Quadrature Point Loop
447b3ff069SJeremy L Thompson     } break;
457b3ff069SJeremy L Thompson     case 33: {
467b3ff069SJeremy L Thompson       const CeedScalar(*J)[3][CEED_Q_VLA] = (const CeedScalar(*)[3][CEED_Q_VLA])in[0];
477b3ff069SJeremy L Thompson 
487b3ff069SJeremy L Thompson       CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
497b3ff069SJeremy L Thompson         // Compute the adjoint
507b3ff069SJeremy L Thompson         CeedScalar A[3][3];
517b3ff069SJeremy L Thompson 
527b3ff069SJeremy L Thompson         for (CeedInt j = 0; j < 3; j++) {
537b3ff069SJeremy L Thompson           for (CeedInt k = 0; k < 3; k++) {
547b3ff069SJeremy L Thompson             // Equivalent code with J as a VLA and no mod operations:
557b3ff069SJeremy 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]
567b3ff069SJeremy L Thompson             A[k][j] =
577b3ff069SJeremy 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];
587b3ff069SJeremy L Thompson           }
597b3ff069SJeremy L Thompson         }
607b3ff069SJeremy L Thompson 
617b3ff069SJeremy L Thompson         // Compute quadrature weight / det(J)
627b3ff069SJeremy 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]);
637b3ff069SJeremy L Thompson 
647b3ff069SJeremy L Thompson         // Compute geometric factors
657b3ff069SJeremy L Thompson         // Stored in Voigt convention
667b3ff069SJeremy L Thompson         // 0 5 4
677b3ff069SJeremy L Thompson         // 5 1 3
687b3ff069SJeremy L Thompson         // 4 3 2
697b3ff069SJeremy L Thompson         q_data[0][i] = qw * (A[0][0] * A[0][0] + A[0][1] * A[0][1] + A[0][2] * A[0][2]);
707b3ff069SJeremy L Thompson         q_data[1][i] = qw * (A[1][0] * A[1][0] + A[1][1] * A[1][1] + A[1][2] * A[1][2]);
717b3ff069SJeremy L Thompson         q_data[2][i] = qw * (A[2][0] * A[2][0] + A[2][1] * A[2][1] + A[2][2] * A[2][2]);
727b3ff069SJeremy L Thompson         q_data[3][i] = qw * (A[1][0] * A[2][0] + A[1][1] * A[2][1] + A[1][2] * A[2][2]);
737b3ff069SJeremy L Thompson         q_data[4][i] = qw * (A[0][0] * A[2][0] + A[0][1] * A[2][1] + A[0][2] * A[2][2]);
747b3ff069SJeremy L Thompson         q_data[5][i] = qw * (A[0][0] * A[1][0] + A[0][1] * A[1][1] + A[0][2] * A[1][2]);
757b3ff069SJeremy L Thompson       }  // End of Quadrature Point Loop
767b3ff069SJeremy L Thompson     } break;
777b3ff069SJeremy L Thompson   }
787b3ff069SJeremy L Thompson   return CEED_ERROR_SUCCESS;
797b3ff069SJeremy L Thompson }
807b3ff069SJeremy L Thompson 
817b3ff069SJeremy L Thompson /// libCEED Q-function for applying a diff operator
apply_diff(void * ctx,const CeedInt Q,const CeedScalar * const * in,CeedScalar * const * out)827b3ff069SJeremy L Thompson CEED_QFUNCTION(apply_diff)(void *ctx, const CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
837b3ff069SJeremy L Thompson   struct BuildContext *build_data = (struct BuildContext *)ctx;
847b3ff069SJeremy L Thompson   // in[0], out[0] solution gradients with shape [dim, 1, Q]
857b3ff069SJeremy L Thompson   // in[1] is quadrature data with shape [num_components, Q]
867b3ff069SJeremy L Thompson   const CeedScalar(*q_data)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[1];
877b3ff069SJeremy L Thompson 
887b3ff069SJeremy L Thompson   switch (build_data->dim) {
897b3ff069SJeremy L Thompson     case 1: {
907b3ff069SJeremy L Thompson       const CeedScalar *ug = in[0];
917b3ff069SJeremy L Thompson       CeedScalar       *vg = out[0];
927b3ff069SJeremy L Thompson 
937b3ff069SJeremy L Thompson       CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) { vg[i] = ug[i] * q_data[0][i]; }  // End of Quadrature Point Loop
947b3ff069SJeremy L Thompson     } break;
957b3ff069SJeremy L Thompson     case 2: {
967b3ff069SJeremy L Thompson       const CeedScalar(*ug)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
977b3ff069SJeremy L Thompson       CeedScalar(*vg)[CEED_Q_VLA]       = (CeedScalar(*)[CEED_Q_VLA])out[0];
987b3ff069SJeremy L Thompson 
997b3ff069SJeremy L Thompson       CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
1007b3ff069SJeremy L Thompson         // Read q_data (dXdxdXdx_T symmetric matrix)
1017b3ff069SJeremy L Thompson         // Stored in Voigt convention
1027b3ff069SJeremy L Thompson         // 0 2
1037b3ff069SJeremy L Thompson         // 2 1
1047b3ff069SJeremy L Thompson         const CeedScalar dXdxdXdx_T[2][2] = {
1057b3ff069SJeremy L Thompson             {q_data[0][i], q_data[2][i]},
1067b3ff069SJeremy L Thompson             {q_data[2][i], q_data[1][i]}
1077b3ff069SJeremy L Thompson         };
1087b3ff069SJeremy L Thompson 
1097b3ff069SJeremy L Thompson         // j = direction of vg
1107b3ff069SJeremy 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]);
1117b3ff069SJeremy L Thompson       }  // End of Quadrature Point Loop
1127b3ff069SJeremy L Thompson     } break;
1137b3ff069SJeremy L Thompson     case 3: {
1147b3ff069SJeremy L Thompson       const CeedScalar(*ug)[CEED_Q_VLA] = (const CeedScalar(*)[CEED_Q_VLA])in[0];
1157b3ff069SJeremy L Thompson       CeedScalar(*vg)[CEED_Q_VLA]       = (CeedScalar(*)[CEED_Q_VLA])out[0];
1167b3ff069SJeremy L Thompson 
1177b3ff069SJeremy L Thompson       CeedPragmaSIMD for (CeedInt i = 0; i < Q; i++) {
1187b3ff069SJeremy L Thompson         // Read q_data (dXdxdXdx_T symmetric matrix)
1197b3ff069SJeremy L Thompson         // Stored in Voigt convention
1207b3ff069SJeremy L Thompson         // 0 5 4
1217b3ff069SJeremy L Thompson         // 5 1 3
1227b3ff069SJeremy L Thompson         // 4 3 2
1237b3ff069SJeremy L Thompson         const CeedScalar dXdxdXdx_T[3][3] = {
1247b3ff069SJeremy L Thompson             {q_data[0][i], q_data[5][i], q_data[4][i]},
1257b3ff069SJeremy L Thompson             {q_data[5][i], q_data[1][i], q_data[3][i]},
1267b3ff069SJeremy L Thompson             {q_data[4][i], q_data[3][i], q_data[2][i]}
1277b3ff069SJeremy L Thompson         };
1287b3ff069SJeremy L Thompson 
1297b3ff069SJeremy L Thompson         // j = direction of vg
1307b3ff069SJeremy 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]);
1317b3ff069SJeremy L Thompson       }  // End of Quadrature Point Loop
1327b3ff069SJeremy L Thompson     } break;
1337b3ff069SJeremy L Thompson   }
1347b3ff069SJeremy L Thompson   return CEED_ERROR_SUCCESS;
1357b3ff069SJeremy L Thompson }
136