xref: /libCEED/tests/t597-operator.h (revision d4cc18453651bd0f94c1a2e078b2646a92dafdcc)
1*9ba83ac0SJeremy L Thompson // Copyright (c) 2017-2026, Lawrence Livermore National Security, LLC and other CEED contributors.
21b95d8c6SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
31b95d8c6SJeremy L Thompson //
41b95d8c6SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
51b95d8c6SJeremy L Thompson //
61b95d8c6SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
71b95d8c6SJeremy L Thompson 
81b95d8c6SJeremy L Thompson #include <ceed/types.h>
91b95d8c6SJeremy L Thompson 
setup(void * ctx,const CeedInt Q,const CeedScalar * const * in,CeedScalar * const * out)101b95d8c6SJeremy L Thompson CEED_QFUNCTION(setup)(void *ctx, const CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
111b95d8c6SJeremy L Thompson   // At every quadrature point, compute qw/det(J).adj(J).adj(J)^T and store
121b95d8c6SJeremy L Thompson   // the symmetric part of the result.
131b95d8c6SJeremy L Thompson 
141b95d8c6SJeremy L Thompson   // in[0] is Jacobians with shape [2, nc=2, Q]
151b95d8c6SJeremy L Thompson   // in[1] is quadrature weights, size (Q)
161b95d8c6SJeremy L Thompson   const CeedScalar *J = in[0], *qw = in[1];
171b95d8c6SJeremy L Thompson 
181b95d8c6SJeremy L Thompson   // out[0] is qdata, size (Q)
191b95d8c6SJeremy L Thompson   CeedScalar *qd = out[0];
201b95d8c6SJeremy L Thompson 
211b95d8c6SJeremy L Thompson   // Quadrature point loop
221b95d8c6SJeremy L Thompson   for (CeedInt i = 0; i < Q; i++) {
231b95d8c6SJeremy L Thompson     // J: 0 2   qd: 0 2   adj(J):  J22 -J12
241b95d8c6SJeremy L Thompson     //    1 3       2 1           -J21  J11
251b95d8c6SJeremy L Thompson     const CeedScalar J11 = J[i + Q * 0];
261b95d8c6SJeremy L Thompson     const CeedScalar J21 = J[i + Q * 1];
271b95d8c6SJeremy L Thompson     const CeedScalar J12 = J[i + Q * 2];
281b95d8c6SJeremy L Thompson     const CeedScalar J22 = J[i + Q * 3];
291b95d8c6SJeremy L Thompson     const CeedScalar w   = qw[i] / (J11 * J22 - J21 * J12);
301b95d8c6SJeremy L Thompson     qd[i + Q * 0]        = w * (J12 * J12 + J22 * J22);
311b95d8c6SJeremy L Thompson     qd[i + Q * 2]        = w * (J11 * J11 + J21 * J21);
321b95d8c6SJeremy L Thompson     qd[i + Q * 1]        = -w * (J11 * J12 + J21 * J22);
331b95d8c6SJeremy L Thompson   }
341b95d8c6SJeremy L Thompson 
351b95d8c6SJeremy L Thompson   return 0;
361b95d8c6SJeremy L Thompson }
371b95d8c6SJeremy L Thompson 
diff(void * ctx,const CeedInt Q,const CeedScalar * const * in,CeedScalar * const * out)381b95d8c6SJeremy L Thompson CEED_QFUNCTION(diff)(void *ctx, const CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
391b95d8c6SJeremy L Thompson   CeedInt num_comp = *(CeedInt *)ctx;
401b95d8c6SJeremy L Thompson   // in[0] is gradient u, shape [2, nc=1, Q]
411b95d8c6SJeremy L Thompson   // in[1] is quadrature data, size (3*Q)
421b95d8c6SJeremy L Thompson   const CeedScalar *du = in[0], *qd = in[1];
431b95d8c6SJeremy L Thompson 
441b95d8c6SJeremy L Thompson   // out[0] is output to multiply against gradient v, shape [2, nc=1, Q]
451b95d8c6SJeremy L Thompson   CeedScalar *dv = out[0];
461b95d8c6SJeremy L Thompson 
471b95d8c6SJeremy L Thompson   // Quadrature point loop
481b95d8c6SJeremy L Thompson   for (CeedInt i = 0; i < Q; i++) {
491b95d8c6SJeremy L Thompson     for (CeedInt c = 0; c < num_comp; c++) {
501b95d8c6SJeremy L Thompson       const CeedScalar du0 = du[i + Q * (2 * c + 0)];
511b95d8c6SJeremy L Thompson       const CeedScalar du1 = du[i + Q * (2 * c + 1)];
521b95d8c6SJeremy L Thompson 
531b95d8c6SJeremy L Thompson       dv[i + Q * (2 * c + 0)] = qd[i + Q * 0] * du0 + qd[i + Q * 2] * du1;
541b95d8c6SJeremy L Thompson       dv[i + Q * (2 * c + 1)] = qd[i + Q * 2] * du0 + qd[i + Q * 1] * du1;
551b95d8c6SJeremy L Thompson     }
561b95d8c6SJeremy L Thompson   }
571b95d8c6SJeremy L Thompson 
581b95d8c6SJeremy L Thompson   return 0;
591b95d8c6SJeremy L Thompson }
60