xref: /libCEED/tests/t597-operator.h (revision 1b95d8c6326a50bc83b21741e59f087f026c971e)
1*1b95d8c6SJeremy L Thompson // Copyright (c) 2017-2025, Lawrence Livermore National Security, LLC and other CEED contributors.
2*1b95d8c6SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
3*1b95d8c6SJeremy L Thompson //
4*1b95d8c6SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
5*1b95d8c6SJeremy L Thompson //
6*1b95d8c6SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
7*1b95d8c6SJeremy L Thompson 
8*1b95d8c6SJeremy L Thompson #include <ceed/types.h>
9*1b95d8c6SJeremy L Thompson 
10*1b95d8c6SJeremy L Thompson CEED_QFUNCTION(setup)(void *ctx, const CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
11*1b95d8c6SJeremy L Thompson   // At every quadrature point, compute qw/det(J).adj(J).adj(J)^T and store
12*1b95d8c6SJeremy L Thompson   // the symmetric part of the result.
13*1b95d8c6SJeremy L Thompson 
14*1b95d8c6SJeremy L Thompson   // in[0] is Jacobians with shape [2, nc=2, Q]
15*1b95d8c6SJeremy L Thompson   // in[1] is quadrature weights, size (Q)
16*1b95d8c6SJeremy L Thompson   const CeedScalar *J = in[0], *qw = in[1];
17*1b95d8c6SJeremy L Thompson 
18*1b95d8c6SJeremy L Thompson   // out[0] is qdata, size (Q)
19*1b95d8c6SJeremy L Thompson   CeedScalar *qd = out[0];
20*1b95d8c6SJeremy L Thompson 
21*1b95d8c6SJeremy L Thompson   // Quadrature point loop
22*1b95d8c6SJeremy L Thompson   for (CeedInt i = 0; i < Q; i++) {
23*1b95d8c6SJeremy L Thompson     // J: 0 2   qd: 0 2   adj(J):  J22 -J12
24*1b95d8c6SJeremy L Thompson     //    1 3       2 1           -J21  J11
25*1b95d8c6SJeremy L Thompson     const CeedScalar J11 = J[i + Q * 0];
26*1b95d8c6SJeremy L Thompson     const CeedScalar J21 = J[i + Q * 1];
27*1b95d8c6SJeremy L Thompson     const CeedScalar J12 = J[i + Q * 2];
28*1b95d8c6SJeremy L Thompson     const CeedScalar J22 = J[i + Q * 3];
29*1b95d8c6SJeremy L Thompson     const CeedScalar w   = qw[i] / (J11 * J22 - J21 * J12);
30*1b95d8c6SJeremy L Thompson     qd[i + Q * 0]        = w * (J12 * J12 + J22 * J22);
31*1b95d8c6SJeremy L Thompson     qd[i + Q * 2]        = w * (J11 * J11 + J21 * J21);
32*1b95d8c6SJeremy L Thompson     qd[i + Q * 1]        = -w * (J11 * J12 + J21 * J22);
33*1b95d8c6SJeremy L Thompson   }
34*1b95d8c6SJeremy L Thompson 
35*1b95d8c6SJeremy L Thompson   return 0;
36*1b95d8c6SJeremy L Thompson }
37*1b95d8c6SJeremy L Thompson 
38*1b95d8c6SJeremy L Thompson CEED_QFUNCTION(diff)(void *ctx, const CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
39*1b95d8c6SJeremy L Thompson   CeedInt num_comp = *(CeedInt *)ctx;
40*1b95d8c6SJeremy L Thompson   // in[0] is gradient u, shape [2, nc=1, Q]
41*1b95d8c6SJeremy L Thompson   // in[1] is quadrature data, size (3*Q)
42*1b95d8c6SJeremy L Thompson   const CeedScalar *du = in[0], *qd = in[1];
43*1b95d8c6SJeremy L Thompson 
44*1b95d8c6SJeremy L Thompson   // out[0] is output to multiply against gradient v, shape [2, nc=1, Q]
45*1b95d8c6SJeremy L Thompson   CeedScalar *dv = out[0];
46*1b95d8c6SJeremy L Thompson 
47*1b95d8c6SJeremy L Thompson   // Quadrature point loop
48*1b95d8c6SJeremy L Thompson   for (CeedInt i = 0; i < Q; i++) {
49*1b95d8c6SJeremy L Thompson     for (CeedInt c = 0; c < num_comp; c++) {
50*1b95d8c6SJeremy L Thompson       const CeedScalar du0 = du[i + Q * (2 * c + 0)];
51*1b95d8c6SJeremy L Thompson       const CeedScalar du1 = du[i + Q * (2 * c + 1)];
52*1b95d8c6SJeremy L Thompson 
53*1b95d8c6SJeremy L Thompson       dv[i + Q * (2 * c + 0)] = qd[i + Q * 0] * du0 + qd[i + Q * 2] * du1;
54*1b95d8c6SJeremy L Thompson       dv[i + Q * (2 * c + 1)] = qd[i + Q * 2] * du0 + qd[i + Q * 1] * du1;
55*1b95d8c6SJeremy L Thompson     }
56*1b95d8c6SJeremy L Thompson   }
57*1b95d8c6SJeremy L Thompson 
58*1b95d8c6SJeremy L Thompson   return 0;
59*1b95d8c6SJeremy L Thompson }
60