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