xref: /libCEED/tests/t531-operator.h (revision d4cc18453651bd0f94c1a2e078b2646a92dafdcc)
1*9ba83ac0SJeremy L Thompson // Copyright (c) 2017-2026, Lawrence Livermore National Security, LLC and other CEED contributors.
23d8e8822SJeremy L Thompson // All Rights Reserved. See the top-level LICENSE and NOTICE files for details.
31d102b48SJeremy L Thompson //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
51d102b48SJeremy L Thompson //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
71d102b48SJeremy L Thompson 
8c0b5abf0SJeremy L Thompson #include <ceed/types.h>
9c9c2c079SJeremy L Thompson 
setup(void * ctx,const CeedInt Q,const CeedScalar * const * in,CeedScalar * const * out)102b730f8bSJeremy L Thompson CEED_QFUNCTION(setup)(void *ctx, const CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
111d102b48SJeremy L Thompson   // At every quadrature point, compute qw/det(J).adj(J).adj(J)^T and store
121d102b48SJeremy L Thompson   // the symmetric part of the result.
131d102b48SJeremy L Thompson 
141d102b48SJeremy L Thompson   // in[0] is Jacobians with shape [2, nc=2, Q]
151d102b48SJeremy L Thompson   // in[1] is quadrature weights, size (Q)
161d102b48SJeremy L Thompson   const CeedScalar *J = in[0], *qw = in[1];
171d102b48SJeremy L Thompson 
181d102b48SJeremy L Thompson   // out[0] is qdata, size (Q)
191d102b48SJeremy L Thompson   CeedScalar *qd = out[0];
201d102b48SJeremy L Thompson 
211d102b48SJeremy L Thompson   // Quadrature point loop
221d102b48SJeremy L Thompson   for (CeedInt i = 0; i < Q; i++) {
231d102b48SJeremy L Thompson     // J: 0 2   qd: 0 2   adj(J):  J22 -J12
241d102b48SJeremy L Thompson     //    1 3       2 1           -J21  J11
251d102b48SJeremy L Thompson     const CeedScalar J11 = J[i + Q * 0];
261d102b48SJeremy L Thompson     const CeedScalar J21 = J[i + Q * 1];
271d102b48SJeremy L Thompson     const CeedScalar J12 = J[i + Q * 2];
281d102b48SJeremy L Thompson     const CeedScalar J22 = J[i + Q * 3];
291d102b48SJeremy L Thompson     const CeedScalar w   = qw[i] / (J11 * J22 - J21 * J12);
301d102b48SJeremy L Thompson     qd[i + Q * 0]        = w * (J12 * J12 + J22 * J22);
3168ebe796SJeremy L Thompson     qd[i + Q * 1]        = w * (J11 * J11 + J21 * J21);
3268ebe796SJeremy L Thompson     qd[i + Q * 2]        = -w * (J11 * J12 + J21 * J22);
331d102b48SJeremy L Thompson   }
341d102b48SJeremy L Thompson 
351d102b48SJeremy L Thompson   return 0;
361d102b48SJeremy L Thompson }
371d102b48SJeremy L Thompson 
diff(void * ctx,const CeedInt Q,const CeedScalar * const * in,CeedScalar * const * out)382b730f8bSJeremy L Thompson CEED_QFUNCTION(diff)(void *ctx, const CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
391d102b48SJeremy L Thompson   // in[0] is gradient u, shape [2, nc=1, Q]
401d102b48SJeremy L Thompson   // in[1] is quadrature data, size (3*Q)
411d102b48SJeremy L Thompson   const CeedScalar *du = in[0], *qd = in[1];
421d102b48SJeremy L Thompson 
431d102b48SJeremy L Thompson   // out[0] is output to multiply against gradient v, shape [2, nc=1, Q]
441d102b48SJeremy L Thompson   CeedScalar *dv = out[0];
451d102b48SJeremy L Thompson 
461d102b48SJeremy L Thompson   // Quadrature point loop
471d102b48SJeremy L Thompson   for (CeedInt i = 0; i < Q; i++) {
481d102b48SJeremy L Thompson     const CeedScalar du0 = du[i + Q * 0];
491d102b48SJeremy L Thompson     const CeedScalar du1 = du[i + Q * 1];
501d102b48SJeremy L Thompson     dv[i + Q * 0]        = qd[i + Q * 0] * du0 + qd[i + Q * 2] * du1;
511d102b48SJeremy L Thompson     dv[i + Q * 1]        = qd[i + Q * 2] * du0 + qd[i + Q * 1] * du1;
521d102b48SJeremy L Thompson   }
531d102b48SJeremy L Thompson   return 0;
541d102b48SJeremy L Thompson }
551d102b48SJeremy L Thompson 
diff_lin(void * ctx,const CeedInt Q,const CeedScalar * const * in,CeedScalar * const * out)562b730f8bSJeremy L Thompson CEED_QFUNCTION(diff_lin)(void *ctx, const CeedInt Q, const CeedScalar *const *in, CeedScalar *const *out) {
571d102b48SJeremy L Thompson   // in[0] is gradient u, shape [2, nc=1, Q]
581d102b48SJeremy L Thompson   // in[1] is quadrature data, size (4*Q)
591d102b48SJeremy L Thompson   const CeedScalar *du = in[0], *qd = in[1];
601d102b48SJeremy L Thompson 
611d102b48SJeremy L Thompson   // out[0] is output to multiply against gradient v, shape [2, nc=1, Q]
621d102b48SJeremy L Thompson   CeedScalar *dv = out[0];
631d102b48SJeremy L Thompson 
641d102b48SJeremy L Thompson   // Quadrature point loop
651d102b48SJeremy L Thompson   for (CeedInt i = 0; i < Q; i++) {
661d102b48SJeremy L Thompson     const CeedScalar du0 = du[i + Q * 0];
671d102b48SJeremy L Thompson     const CeedScalar du1 = du[i + Q * 1];
681d102b48SJeremy L Thompson     // Linearized Qdata is provided column-major
691d102b48SJeremy L Thompson     //  0 2
701d102b48SJeremy L Thompson     //  1 3
711d102b48SJeremy L Thompson     dv[i + Q * 0] = qd[i + Q * 0] * du0 + qd[i + Q * 2] * du1;
721d102b48SJeremy L Thompson     dv[i + Q * 1] = qd[i + Q * 1] * du0 + qd[i + Q * 3] * du1;
731d102b48SJeremy L Thompson   }
741d102b48SJeremy L Thompson 
751d102b48SJeremy L Thompson   return 0;
761d102b48SJeremy L Thompson }
77