xref: /libCEED/rust/libceed-sys/c-src/backends/ref/ceed-ref-basis.c (revision 5aed82e4fa97acf4ba24a7f10a35f5303a6798e0)
1*5aed82e4SJeremy L Thompson // Copyright (c) 2017-2024, 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.
321617c04Sjeremylt //
43d8e8822SJeremy L Thompson // SPDX-License-Identifier: BSD-2-Clause
521617c04Sjeremylt //
63d8e8822SJeremy L Thompson // This file is part of CEED:  http://github.com/ceed
721617c04Sjeremylt 
849aac155SJeremy L Thompson #include <ceed.h>
9ec3da8bcSJed Brown #include <ceed/backend.h>
103d576824SJeremy L Thompson #include <math.h>
113d576824SJeremy L Thompson #include <stdbool.h>
123d576824SJeremy L Thompson #include <string.h>
132b730f8bSJeremy L Thompson 
1421617c04Sjeremylt #include "ceed-ref.h"
1521617c04Sjeremylt 
16f10650afSjeremylt //------------------------------------------------------------------------------
17f10650afSjeremylt // Basis Apply
18f10650afSjeremylt //------------------------------------------------------------------------------
192b730f8bSJeremy L Thompson static int CeedBasisApply_Ref(CeedBasis basis, CeedInt num_elem, CeedTransposeMode t_mode, CeedEvalMode eval_mode, CeedVector U, CeedVector V) {
204ce2993fSjeremylt   Ceed               ceed;
21ad70ee2cSJeremy L Thompson   bool               is_tensor_basis;
22c4e3f59bSSebastian Grimberg   CeedInt            dim, num_comp, q_comp, num_nodes, num_qpts;
23ad70ee2cSJeremy L Thompson   const CeedInt      add = (t_mode == CEED_TRANSPOSE);
24ad70ee2cSJeremy L Thompson   const CeedScalar  *u;
25ad70ee2cSJeremy L Thompson   CeedScalar        *v;
26ad70ee2cSJeremy L Thompson   CeedTensorContract contract;
27ad70ee2cSJeremy L Thompson   CeedBasis_Ref     *impl;
28ad70ee2cSJeremy L Thompson 
29ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
30ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedBasisGetData(basis, &impl));
312b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetDimension(basis, &dim));
322b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetNumComponents(basis, &num_comp));
33c4e3f59bSSebastian Grimberg   CeedCallBackend(CeedBasisGetNumQuadratureComponents(basis, eval_mode, &q_comp));
342b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetNumNodes(basis, &num_nodes));
352b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetNumQuadraturePoints(basis, &num_qpts));
362b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisGetTensorContract(basis, &contract));
376574a04fSJeremy L Thompson   if (U != CEED_VECTOR_NONE) CeedCallBackend(CeedVectorGetArrayRead(U, CEED_MEM_HOST, &u));
386574a04fSJeremy L Thompson   else CeedCheck(eval_mode == CEED_EVAL_WEIGHT, ceed, CEED_ERROR_BACKEND, "An input vector is required for this CeedEvalMode");
392b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorGetArrayWrite(V, CEED_MEM_HOST, &v));
4021617c04Sjeremylt 
418d94b059Sjeremylt   // Clear v if operating in transpose
42d1d35e2fSjeremylt   if (t_mode == CEED_TRANSPOSE) {
43d1d35e2fSjeremylt     const CeedInt v_size = num_elem * num_comp * num_nodes;
44ad70ee2cSJeremy L Thompson 
452b730f8bSJeremy L Thompson     for (CeedInt i = 0; i < v_size; i++) v[i] = (CeedScalar)0.0;
4621617c04Sjeremylt   }
47ad70ee2cSJeremy L Thompson 
486402da51SJeremy L Thompson   CeedCallBackend(CeedBasisIsTensor(basis, &is_tensor_basis));
496402da51SJeremy L Thompson   if (is_tensor_basis) {
50c4e3f59bSSebastian Grimberg     // Tensor basis
51d1d35e2fSjeremylt     CeedInt P_1d, Q_1d;
52ad70ee2cSJeremy L Thompson 
532b730f8bSJeremy L Thompson     CeedCallBackend(CeedBasisGetNumNodes1D(basis, &P_1d));
542b730f8bSJeremy L Thompson     CeedCallBackend(CeedBasisGetNumQuadraturePoints1D(basis, &Q_1d));
55d1d35e2fSjeremylt     switch (eval_mode) {
568d94b059Sjeremylt       // Interpolate to/from quadrature points
57a2b73c81Sjeremylt       case CEED_EVAL_INTERP: {
588c1105f8SJeremy L Thompson         if (impl->has_collo_interp) {
59d1d35e2fSjeremylt           memcpy(v, u, num_elem * num_comp * num_nodes * sizeof(u[0]));
60dfe03796SJeremy L Thompson         } else {
61d1d35e2fSjeremylt           CeedInt P = P_1d, Q = Q_1d;
62ad70ee2cSJeremy L Thompson 
63d1d35e2fSjeremylt           if (t_mode == CEED_TRANSPOSE) {
642b730f8bSJeremy L Thompson             P = Q_1d;
652b730f8bSJeremy L Thompson             Q = P_1d;
6621617c04Sjeremylt           }
67d1d35e2fSjeremylt           CeedInt           pre = num_comp * CeedIntPow(P, dim - 1), post = num_elem;
68d1d35e2fSjeremylt           CeedScalar        tmp[2][num_elem * num_comp * Q * CeedIntPow(P > Q ? P : Q, dim - 1)];
69d1d35e2fSjeremylt           const CeedScalar *interp_1d;
70ad70ee2cSJeremy L Thompson 
712b730f8bSJeremy L Thompson           CeedCallBackend(CeedBasisGetInterp1D(basis, &interp_1d));
7221617c04Sjeremylt           for (CeedInt d = 0; d < dim; d++) {
732b730f8bSJeremy L Thompson             CeedCallBackend(CeedTensorContractApply(contract, pre, P, post, Q, interp_1d, t_mode, add && (d == dim - 1), d == 0 ? u : tmp[d % 2],
742b730f8bSJeremy L Thompson                                                     d == dim - 1 ? v : tmp[(d + 1) % 2]));
7521617c04Sjeremylt             pre /= P;
7621617c04Sjeremylt             post *= Q;
7721617c04Sjeremylt           }
78dfe03796SJeremy L Thompson         }
79a2b73c81Sjeremylt       } break;
808d94b059Sjeremylt       // Evaluate the gradient to/from quadrature points
81a2b73c81Sjeremylt       case CEED_EVAL_GRAD: {
8221617c04Sjeremylt         // In CEED_NOTRANSPOSE mode:
83d1d35e2fSjeremylt         // u has shape [dim, num_comp, P^dim, num_elem], row-major layout
84d1d35e2fSjeremylt         // v has shape [dim, num_comp, Q^dim, num_elem], row-major layout
8521617c04Sjeremylt         // In CEED_TRANSPOSE mode, the sizes of u and v are switched.
86d1d35e2fSjeremylt         CeedInt P = P_1d, Q = Q_1d;
87ad70ee2cSJeremy L Thompson 
88d1d35e2fSjeremylt         if (t_mode == CEED_TRANSPOSE) {
891c66c397SJeremy L Thompson           P = Q_1d;
901c66c397SJeremy L Thompson           Q = Q_1d;
9121617c04Sjeremylt         }
92d1d35e2fSjeremylt         CeedInt           pre = num_comp * CeedIntPow(P, dim - 1), post = num_elem;
93d1d35e2fSjeremylt         const CeedScalar *interp_1d;
94ad70ee2cSJeremy L Thompson 
952b730f8bSJeremy L Thompson         CeedCallBackend(CeedBasisGetInterp1D(basis, &interp_1d));
968c1105f8SJeremy L Thompson         if (impl->collo_grad_1d) {
97d1d35e2fSjeremylt           CeedScalar tmp[2][num_elem * num_comp * Q * CeedIntPow(P > Q ? P : Q, dim - 1)];
98d1d35e2fSjeremylt           CeedScalar interp[num_elem * num_comp * Q * CeedIntPow(P > Q ? P : Q, dim - 1)];
99ad70ee2cSJeremy L Thompson 
10084a01de5SJeremy L Thompson           // Interpolate to quadrature points (NoTranspose)
10184a01de5SJeremy L Thompson           //  or Grad to quadrature points (Transpose)
10221617c04Sjeremylt           for (CeedInt d = 0; d < dim; d++) {
1032b730f8bSJeremy L Thompson             CeedCallBackend(CeedTensorContractApply(contract, pre, P, post, Q, (t_mode == CEED_NOTRANSPOSE ? interp_1d : impl->collo_grad_1d), t_mode,
1042b730f8bSJeremy L Thompson                                                     add && (d > 0),
1052b730f8bSJeremy L Thompson                                                     (t_mode == CEED_NOTRANSPOSE ? (d == 0 ? u : tmp[d % 2]) : u + d * num_qpts * num_comp * num_elem),
1062b730f8bSJeremy L Thompson                                                     (t_mode == CEED_NOTRANSPOSE ? (d == dim - 1 ? interp : tmp[(d + 1) % 2]) : interp)));
10721617c04Sjeremylt             pre /= P;
10821617c04Sjeremylt             post *= Q;
10921617c04Sjeremylt           }
11084a01de5SJeremy L Thompson           // Grad to quadrature points (NoTranspose)
1118795c945Sjeremylt           //  or Interpolate to nodes (Transpose)
112d1d35e2fSjeremylt           P = Q_1d, Q = Q_1d;
113d1d35e2fSjeremylt           if (t_mode == CEED_TRANSPOSE) {
1141c66c397SJeremy L Thompson             P = Q_1d;
1151c66c397SJeremy L Thompson             Q = P_1d;
11684a01de5SJeremy L Thompson           }
117d1d35e2fSjeremylt           pre = num_comp * CeedIntPow(P, dim - 1), post = num_elem;
11884a01de5SJeremy L Thompson           for (CeedInt d = 0; d < dim; d++) {
1192b730f8bSJeremy L Thompson             CeedCallBackend(CeedTensorContractApply(
1202b730f8bSJeremy L Thompson                 contract, pre, P, post, Q, (t_mode == CEED_NOTRANSPOSE ? impl->collo_grad_1d : interp_1d), t_mode, add && (d == dim - 1),
1212b730f8bSJeremy L Thompson                 (t_mode == CEED_NOTRANSPOSE ? interp : (d == 0 ? interp : tmp[d % 2])),
1222b730f8bSJeremy L Thompson                 (t_mode == CEED_NOTRANSPOSE ? v + d * num_qpts * num_comp * num_elem : (d == dim - 1 ? v : tmp[(d + 1) % 2]))));
12384a01de5SJeremy L Thompson             pre /= P;
12484a01de5SJeremy L Thompson             post *= Q;
12521617c04Sjeremylt           }
1268c1105f8SJeremy L Thompson         } else if (impl->has_collo_interp) {  // Qpts collocated with nodes
127d1d35e2fSjeremylt           const CeedScalar *grad_1d;
128ad70ee2cSJeremy L Thompson 
1292b730f8bSJeremy L Thompson           CeedCallBackend(CeedBasisGetGrad1D(basis, &grad_1d));
130dfe03796SJeremy L Thompson 
131dfe03796SJeremy L Thompson           // Dim contractions, identity in other directions
132d1d35e2fSjeremylt           CeedInt pre = num_comp * CeedIntPow(P, dim - 1), post = num_elem;
133ad70ee2cSJeremy L Thompson 
134c6158135Sjeremylt           for (CeedInt d = 0; d < dim; d++) {
1352b730f8bSJeremy L Thompson             CeedCallBackend(CeedTensorContractApply(contract, pre, P, post, Q, grad_1d, t_mode, add && (d > 0),
1362b730f8bSJeremy L Thompson                                                     t_mode == CEED_NOTRANSPOSE ? u : u + d * num_comp * num_qpts * num_elem,
1372b730f8bSJeremy L Thompson                                                     t_mode == CEED_TRANSPOSE ? v : v + d * num_comp * num_qpts * num_elem));
138c6158135Sjeremylt             pre /= P;
139c6158135Sjeremylt             post *= Q;
140dfe03796SJeremy L Thompson           }
141a7bd39daSjeremylt         } else {  // Underintegration, P > Q
142d1d35e2fSjeremylt           const CeedScalar *grad_1d;
143ad70ee2cSJeremy L Thompson 
1442b730f8bSJeremy L Thompson           CeedCallBackend(CeedBasisGetGrad1D(basis, &grad_1d));
145a7bd39daSjeremylt 
146d1d35e2fSjeremylt           if (t_mode == CEED_TRANSPOSE) {
1471c66c397SJeremy L Thompson             P = Q_1d;
1481c66c397SJeremy L Thompson             Q = P_1d;
149a7bd39daSjeremylt           }
150d1d35e2fSjeremylt           CeedScalar tmp[2][num_elem * num_comp * Q * CeedIntPow(P > Q ? P : Q, dim - 1)];
151a7bd39daSjeremylt 
152a7bd39daSjeremylt           // Dim**2 contractions, apply grad when pass == dim
153a7bd39daSjeremylt           for (CeedInt p = 0; p < dim; p++) {
154d1d35e2fSjeremylt             CeedInt pre = num_comp * CeedIntPow(P, dim - 1), post = num_elem;
155ad70ee2cSJeremy L Thompson 
156a7bd39daSjeremylt             for (CeedInt d = 0; d < dim; d++) {
1572b730f8bSJeremy L Thompson               CeedCallBackend(CeedTensorContractApply(
1582b730f8bSJeremy L Thompson                   contract, pre, P, post, Q, (p == d) ? grad_1d : interp_1d, t_mode, add && (d == dim - 1),
1592b730f8bSJeremy L Thompson                   (d == 0 ? (t_mode == CEED_NOTRANSPOSE ? u : u + p * num_comp * num_qpts * num_elem) : tmp[d % 2]),
1602b730f8bSJeremy L Thompson                   (d == dim - 1 ? (t_mode == CEED_TRANSPOSE ? v : v + p * num_comp * num_qpts * num_elem) : tmp[(d + 1) % 2])));
161a7bd39daSjeremylt               pre /= P;
162a7bd39daSjeremylt               post *= Q;
163a7bd39daSjeremylt             }
164a7bd39daSjeremylt           }
165a7bd39daSjeremylt         }
166a2b73c81Sjeremylt       } break;
1678d94b059Sjeremylt       // Retrieve interpolation weights
168a2b73c81Sjeremylt       case CEED_EVAL_WEIGHT: {
169d1d35e2fSjeremylt         CeedInt           Q = Q_1d;
170d1d35e2fSjeremylt         const CeedScalar *q_weight_1d;
171ad70ee2cSJeremy L Thompson 
172ad70ee2cSJeremy L Thompson         CeedCheck(t_mode == CEED_NOTRANSPOSE, ceed, CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT incompatible with CEED_TRANSPOSE");
1732b730f8bSJeremy L Thompson         CeedCallBackend(CeedBasisGetQWeights(basis, &q_weight_1d));
17421617c04Sjeremylt         for (CeedInt d = 0; d < dim; d++) {
175b5cf12eeSjeremylt           CeedInt pre = CeedIntPow(Q, dim - d - 1), post = CeedIntPow(Q, d);
176ad70ee2cSJeremy L Thompson 
1772b730f8bSJeremy L Thompson           for (CeedInt i = 0; i < pre; i++) {
1782b730f8bSJeremy L Thompson             for (CeedInt j = 0; j < Q; j++) {
17984a01de5SJeremy L Thompson               for (CeedInt k = 0; k < post; k++) {
180ad70ee2cSJeremy L Thompson                 const CeedScalar w = q_weight_1d[j] * (d == 0 ? 1 : v[((i * Q + j) * post + k) * num_elem]);
181ad70ee2cSJeremy L Thompson 
1822b730f8bSJeremy L Thompson                 for (CeedInt e = 0; e < num_elem; e++) v[((i * Q + j) * post + k) * num_elem + e] = w;
1832b730f8bSJeremy L Thompson               }
1842b730f8bSJeremy L Thompson             }
18584a01de5SJeremy L Thompson           }
18621617c04Sjeremylt         }
187a2b73c81Sjeremylt       } break;
188c042f62fSJeremy L Thompson       // LCOV_EXCL_START
189a2b73c81Sjeremylt       case CEED_EVAL_DIV:
190a2b73c81Sjeremylt       case CEED_EVAL_CURL:
191bcbe1c99SJeremy L Thompson         return CeedError(ceed, CEED_ERROR_BACKEND, "%s not supported", CeedEvalModes[eval_mode]);
192a2b73c81Sjeremylt       case CEED_EVAL_NONE:
1932b730f8bSJeremy L Thompson         return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_NONE does not make sense in this context");
194c042f62fSJeremy L Thompson         // LCOV_EXCL_STOP
19521617c04Sjeremylt     }
196a8de75f0Sjeremylt   } else {
197a8de75f0Sjeremylt     // Non-tensor basis
198c4e3f59bSSebastian Grimberg     CeedInt P = num_nodes, Q = num_qpts;
199ad70ee2cSJeremy L Thompson 
200d1d35e2fSjeremylt     switch (eval_mode) {
20184a01de5SJeremy L Thompson       // Interpolate to/from quadrature points
202a8de75f0Sjeremylt       case CEED_EVAL_INTERP: {
2036c58de82SJeremy L Thompson         const CeedScalar *interp;
204ad70ee2cSJeremy L Thompson 
2052b730f8bSJeremy L Thompson         CeedCallBackend(CeedBasisGetInterp(basis, &interp));
206c4e3f59bSSebastian Grimberg         CeedCallBackend(CeedTensorContractStridedApply(contract, num_comp, P, num_elem, q_comp, Q, interp, t_mode, add, u, v));
2072b730f8bSJeremy L Thompson       } break;
20884a01de5SJeremy L Thompson       // Evaluate the gradient to/from quadrature points
209a8de75f0Sjeremylt       case CEED_EVAL_GRAD: {
2106c58de82SJeremy L Thompson         const CeedScalar *grad;
211ad70ee2cSJeremy L Thompson 
2122b730f8bSJeremy L Thompson         CeedCallBackend(CeedBasisGetGrad(basis, &grad));
213c4e3f59bSSebastian Grimberg         CeedCallBackend(CeedTensorContractStridedApply(contract, num_comp, P, num_elem, q_comp, Q, grad, t_mode, add, u, v));
214c4e3f59bSSebastian Grimberg       } break;
215c4e3f59bSSebastian Grimberg       // Evaluate the divergence to/from the quadrature points
216c4e3f59bSSebastian Grimberg       case CEED_EVAL_DIV: {
217c4e3f59bSSebastian Grimberg         const CeedScalar *div;
218ad70ee2cSJeremy L Thompson 
219c4e3f59bSSebastian Grimberg         CeedCallBackend(CeedBasisGetDiv(basis, &div));
220c4e3f59bSSebastian Grimberg         CeedCallBackend(CeedTensorContractStridedApply(contract, num_comp, P, num_elem, q_comp, Q, div, t_mode, add, u, v));
221c4e3f59bSSebastian Grimberg       } break;
222c4e3f59bSSebastian Grimberg       // Evaluate the curl to/from the quadrature points
223c4e3f59bSSebastian Grimberg       case CEED_EVAL_CURL: {
224c4e3f59bSSebastian Grimberg         const CeedScalar *curl;
225ad70ee2cSJeremy L Thompson 
226c4e3f59bSSebastian Grimberg         CeedCallBackend(CeedBasisGetCurl(basis, &curl));
227c4e3f59bSSebastian Grimberg         CeedCallBackend(CeedTensorContractStridedApply(contract, num_comp, P, num_elem, q_comp, Q, curl, t_mode, add, u, v));
2282b730f8bSJeremy L Thompson       } break;
22984a01de5SJeremy L Thompson       // Retrieve interpolation weights
230a8de75f0Sjeremylt       case CEED_EVAL_WEIGHT: {
231d1d35e2fSjeremylt         const CeedScalar *q_weight;
232ad70ee2cSJeremy L Thompson 
233ad70ee2cSJeremy L Thompson         CeedCheck(t_mode == CEED_NOTRANSPOSE, ceed, CEED_ERROR_BACKEND, "CEED_EVAL_WEIGHT incompatible with CEED_TRANSPOSE");
2342b730f8bSJeremy L Thompson         CeedCallBackend(CeedBasisGetQWeights(basis, &q_weight));
2352b730f8bSJeremy L Thompson         for (CeedInt i = 0; i < num_qpts; i++) {
2362b730f8bSJeremy L Thompson           for (CeedInt e = 0; e < num_elem; e++) v[i * num_elem + e] = q_weight[i];
2372b730f8bSJeremy L Thompson         }
238a8de75f0Sjeremylt       } break;
23950c301a5SRezgar Shakeri       // LCOV_EXCL_START
240a8de75f0Sjeremylt       case CEED_EVAL_NONE:
2412b730f8bSJeremy L Thompson         return CeedError(ceed, CEED_ERROR_BACKEND, "CEED_EVAL_NONE does not make sense in this context");
242c042f62fSJeremy L Thompson         // LCOV_EXCL_STOP
243a8de75f0Sjeremylt     }
244a8de75f0Sjeremylt   }
245a7b7f929Sjeremylt   if (U != CEED_VECTOR_NONE) {
2462b730f8bSJeremy L Thompson     CeedCallBackend(CeedVectorRestoreArrayRead(U, &u));
247aedaa0e5Sjeremylt   }
2482b730f8bSJeremy L Thompson   CeedCallBackend(CeedVectorRestoreArray(V, &v));
249e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
25021617c04Sjeremylt }
25121617c04Sjeremylt 
252f10650afSjeremylt //------------------------------------------------------------------------------
253b2165e7aSSebastian Grimberg // Basis Destroy Tensor
254b2165e7aSSebastian Grimberg //------------------------------------------------------------------------------
255b2165e7aSSebastian Grimberg static int CeedBasisDestroyTensor_Ref(CeedBasis basis) {
256b2165e7aSSebastian Grimberg   CeedBasis_Ref *impl;
257ad70ee2cSJeremy L Thompson 
258b2165e7aSSebastian Grimberg   CeedCallBackend(CeedBasisGetData(basis, &impl));
259b2165e7aSSebastian Grimberg   CeedCallBackend(CeedFree(&impl->collo_grad_1d));
260b2165e7aSSebastian Grimberg   CeedCallBackend(CeedFree(&impl));
261b2165e7aSSebastian Grimberg   return CEED_ERROR_SUCCESS;
262b2165e7aSSebastian Grimberg }
263b2165e7aSSebastian Grimberg 
264b2165e7aSSebastian Grimberg //------------------------------------------------------------------------------
265b2165e7aSSebastian Grimberg // Basis Create Tensor
266b2165e7aSSebastian Grimberg //------------------------------------------------------------------------------
267b2165e7aSSebastian Grimberg int CeedBasisCreateTensorH1_Ref(CeedInt dim, CeedInt P_1d, CeedInt Q_1d, const CeedScalar *interp_1d, const CeedScalar *grad_1d,
268b2165e7aSSebastian Grimberg                                 const CeedScalar *q_ref_1d, const CeedScalar *q_weight_1d, CeedBasis basis) {
269ca735530SJeremy L Thompson   Ceed               ceed, ceed_parent;
270b2165e7aSSebastian Grimberg   CeedBasis_Ref     *impl;
271ad70ee2cSJeremy L Thompson   CeedTensorContract contract;
272ad70ee2cSJeremy L Thompson 
273ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
274ca735530SJeremy L Thompson   CeedCallBackend(CeedGetParent(ceed, &ceed_parent));
275ad70ee2cSJeremy L Thompson 
276b2165e7aSSebastian Grimberg   CeedCallBackend(CeedCalloc(1, &impl));
277b2165e7aSSebastian Grimberg   // Check for collocated interp
278b2165e7aSSebastian Grimberg   if (Q_1d == P_1d) {
279ad70ee2cSJeremy L Thompson     bool has_collocated = true;
280ad70ee2cSJeremy L Thompson 
281b2165e7aSSebastian Grimberg     for (CeedInt i = 0; i < P_1d; i++) {
282ad70ee2cSJeremy L Thompson       has_collocated = has_collocated && (fabs(interp_1d[i + P_1d * i] - 1.0) < 1e-14);
283b2165e7aSSebastian Grimberg       for (CeedInt j = 0; j < P_1d; j++) {
284ad70ee2cSJeremy L Thompson         if (j != i) has_collocated = has_collocated && (fabs(interp_1d[j + P_1d * i]) < 1e-14);
285b2165e7aSSebastian Grimberg       }
286b2165e7aSSebastian Grimberg     }
287ad70ee2cSJeremy L Thompson     impl->has_collo_interp = has_collocated;
288b2165e7aSSebastian Grimberg   }
289b2165e7aSSebastian Grimberg   // Calculate collocated grad
290b2165e7aSSebastian Grimberg   if (Q_1d >= P_1d && !impl->has_collo_interp) {
291b2165e7aSSebastian Grimberg     CeedCallBackend(CeedMalloc(Q_1d * Q_1d, &impl->collo_grad_1d));
292b2165e7aSSebastian Grimberg     CeedCallBackend(CeedBasisGetCollocatedGrad(basis, impl->collo_grad_1d));
293b2165e7aSSebastian Grimberg   }
294b2165e7aSSebastian Grimberg   CeedCallBackend(CeedBasisSetData(basis, impl));
295b2165e7aSSebastian Grimberg 
296a71faab1SSebastian Grimberg   CeedCallBackend(CeedTensorContractCreate(ceed_parent, &contract));
297b2165e7aSSebastian Grimberg   CeedCallBackend(CeedBasisSetTensorContract(basis, contract));
298b2165e7aSSebastian Grimberg 
299b2165e7aSSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApply_Ref));
300b2165e7aSSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Destroy", CeedBasisDestroyTensor_Ref));
301b2165e7aSSebastian Grimberg   return CEED_ERROR_SUCCESS;
302b2165e7aSSebastian Grimberg }
303b2165e7aSSebastian Grimberg 
304b2165e7aSSebastian Grimberg //------------------------------------------------------------------------------
30550c301a5SRezgar Shakeri // Basis Create Non-Tensor H^1
306f10650afSjeremylt //------------------------------------------------------------------------------
3072b730f8bSJeremy L Thompson int CeedBasisCreateH1_Ref(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, const CeedScalar *grad,
3082b730f8bSJeremy L Thompson                           const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis) {
309ca735530SJeremy L Thompson   Ceed               ceed, ceed_parent;
310f10650afSjeremylt   CeedTensorContract contract;
311ad70ee2cSJeremy L Thompson 
312ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
313ca735530SJeremy L Thompson   CeedCallBackend(CeedGetParent(ceed, &ceed_parent));
314ad70ee2cSJeremy L Thompson 
315a71faab1SSebastian Grimberg   CeedCallBackend(CeedTensorContractCreate(ceed_parent, &contract));
3162b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisSetTensorContract(basis, contract));
317f10650afSjeremylt 
3182b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApply_Ref));
319e15f9bd0SJeremy L Thompson   return CEED_ERROR_SUCCESS;
320f10650afSjeremylt }
321f10650afSjeremylt 
322f10650afSjeremylt //------------------------------------------------------------------------------
32350c301a5SRezgar Shakeri // Basis Create Non-Tensor H(div)
32450c301a5SRezgar Shakeri //------------------------------------------------------------------------------
3252b730f8bSJeremy L Thompson int CeedBasisCreateHdiv_Ref(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp, const CeedScalar *div,
3262b730f8bSJeremy L Thompson                             const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis) {
327ca735530SJeremy L Thompson   Ceed               ceed, ceed_parent;
32850c301a5SRezgar Shakeri   CeedTensorContract contract;
329ad70ee2cSJeremy L Thompson 
330ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
331ca735530SJeremy L Thompson   CeedCallBackend(CeedGetParent(ceed, &ceed_parent));
332ad70ee2cSJeremy L Thompson 
333a71faab1SSebastian Grimberg   CeedCallBackend(CeedTensorContractCreate(ceed_parent, &contract));
3342b730f8bSJeremy L Thompson   CeedCallBackend(CeedBasisSetTensorContract(basis, contract));
33550c301a5SRezgar Shakeri 
3362b730f8bSJeremy L Thompson   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApply_Ref));
33750c301a5SRezgar Shakeri   return CEED_ERROR_SUCCESS;
33850c301a5SRezgar Shakeri }
33950c301a5SRezgar Shakeri 
34050c301a5SRezgar Shakeri //------------------------------------------------------------------------------
341c4e3f59bSSebastian Grimberg // Basis Create Non-Tensor H(curl)
342c4e3f59bSSebastian Grimberg //------------------------------------------------------------------------------
343c4e3f59bSSebastian Grimberg int CeedBasisCreateHcurl_Ref(CeedElemTopology topo, CeedInt dim, CeedInt num_nodes, CeedInt num_qpts, const CeedScalar *interp,
344c4e3f59bSSebastian Grimberg                              const CeedScalar *curl, const CeedScalar *q_ref, const CeedScalar *q_weight, CeedBasis basis) {
345ca735530SJeremy L Thompson   Ceed               ceed, ceed_parent;
346c4e3f59bSSebastian Grimberg   CeedTensorContract contract;
347ad70ee2cSJeremy L Thompson 
348ad70ee2cSJeremy L Thompson   CeedCallBackend(CeedBasisGetCeed(basis, &ceed));
349ca735530SJeremy L Thompson   CeedCallBackend(CeedGetParent(ceed, &ceed_parent));
350ad70ee2cSJeremy L Thompson 
351a71faab1SSebastian Grimberg   CeedCallBackend(CeedTensorContractCreate(ceed_parent, &contract));
352c4e3f59bSSebastian Grimberg   CeedCallBackend(CeedBasisSetTensorContract(basis, contract));
353c4e3f59bSSebastian Grimberg 
354c4e3f59bSSebastian Grimberg   CeedCallBackend(CeedSetBackendFunction(ceed, "Basis", basis, "Apply", CeedBasisApply_Ref));
355c4e3f59bSSebastian Grimberg   return CEED_ERROR_SUCCESS;
356c4e3f59bSSebastian Grimberg }
357c4e3f59bSSebastian Grimberg 
358c4e3f59bSSebastian Grimberg //------------------------------------------------------------------------------
359