xref: /libCEED/rust/libceed-sys/c-src/backends/ref/ceed-ref-basis.c (revision c6158135e0c4dbb7edff94dbbd676bd7d2a17485)
121617c04Sjeremylt // Copyright (c) 2017-2018, Lawrence Livermore National Security, LLC.
221617c04Sjeremylt // Produced at the Lawrence Livermore National Laboratory. LLNL-CODE-734707.
321617c04Sjeremylt // All Rights reserved. See files LICENSE and NOTICE for details.
421617c04Sjeremylt //
521617c04Sjeremylt // This file is part of CEED, a collection of benchmarks, miniapps, software
621617c04Sjeremylt // libraries and APIs for efficient high-order finite element and spectral
721617c04Sjeremylt // element discretizations for exascale applications. For more information and
821617c04Sjeremylt // source code availability see http://github.com/ceed.
921617c04Sjeremylt //
1021617c04Sjeremylt // The CEED research is supported by the Exascale Computing Project 17-SC-20-SC,
1121617c04Sjeremylt // a collaborative effort of two U.S. Department of Energy organizations (Office
1221617c04Sjeremylt // of Science and the National Nuclear Security Administration) responsible for
1321617c04Sjeremylt // the planning and preparation of a capable exascale ecosystem, including
1421617c04Sjeremylt // software, applications, hardware, advanced system engineering and early
1521617c04Sjeremylt // testbed platforms, in support of the nation's exascale computing imperative.
1621617c04Sjeremylt 
1721617c04Sjeremylt #include "ceed-ref.h"
1821617c04Sjeremylt 
19d3181881Sjeremylt static int CeedBasisApply_Ref(CeedBasis basis, CeedInt nelem,
20d3181881Sjeremylt                               CeedTransposeMode tmode, CeedEvalMode emode,
21aedaa0e5Sjeremylt                               CeedVector U, CeedVector V) {
2221617c04Sjeremylt   int ierr;
234ce2993fSjeremylt   Ceed ceed;
244ce2993fSjeremylt   ierr = CeedBasisGetCeed(basis, &ceed); CeedChk(ierr);
258795c945Sjeremylt   CeedInt dim, ncomp, nnodes, nqpt;
264ce2993fSjeremylt   ierr = CeedBasisGetDimension(basis, &dim); CeedChk(ierr);
274ce2993fSjeremylt   ierr = CeedBasisGetNumComponents(basis, &ncomp); CeedChk(ierr);
288795c945Sjeremylt   ierr = CeedBasisGetNumNodes(basis, &nnodes); CeedChk(ierr);
294ce2993fSjeremylt   ierr = CeedBasisGetNumQuadraturePoints(basis, &nqpt); CeedChk(ierr);
302f86a920SJeremy L Thompson   CeedTensorContract contract;
312f86a920SJeremy L Thompson   ierr = CeedBasisGetTensorContract(basis, &contract); CeedChk(ierr);
3221617c04Sjeremylt   const CeedInt add = (tmode == CEED_TRANSPOSE);
33aedaa0e5Sjeremylt   const CeedScalar *u;
34aedaa0e5Sjeremylt   CeedScalar *v;
35aedaa0e5Sjeremylt   if (U) {
36aedaa0e5Sjeremylt     ierr = CeedVectorGetArrayRead(U, CEED_MEM_HOST, &u); CeedChk(ierr);
37aedaa0e5Sjeremylt   } else if (emode != CEED_EVAL_WEIGHT) {
38c042f62fSJeremy L Thompson     // LCOV_EXCL_START
39aedaa0e5Sjeremylt     return CeedError(ceed, 1,
40aedaa0e5Sjeremylt                      "An input vector is required for this CeedEvalMode");
41c042f62fSJeremy L Thompson     // LCOV_EXCL_STOP
42aedaa0e5Sjeremylt   }
43aedaa0e5Sjeremylt   ierr = CeedVectorGetArray(V, CEED_MEM_HOST, &v); CeedChk(ierr);
4421617c04Sjeremylt 
458d94b059Sjeremylt   // Clear v if operating in transpose
4621617c04Sjeremylt   if (tmode == CEED_TRANSPOSE) {
478795c945Sjeremylt     const CeedInt vsize = nelem*ncomp*nnodes;
4821617c04Sjeremylt     for (CeedInt i = 0; i < vsize; i++)
4984a01de5SJeremy L Thompson       v[i] = (CeedScalar) 0.0;
5021617c04Sjeremylt   }
514ce2993fSjeremylt   bool tensorbasis;
524ce2993fSjeremylt   ierr = CeedBasisGetTensorStatus(basis, &tensorbasis); CeedChk(ierr);
5384a01de5SJeremy L Thompson   // Tensor basis
544ce2993fSjeremylt   if (tensorbasis) {
554ce2993fSjeremylt     CeedInt P1d, Q1d;
564ce2993fSjeremylt     ierr = CeedBasisGetNumNodes1D(basis, &P1d); CeedChk(ierr);
574ce2993fSjeremylt     ierr = CeedBasisGetNumQuadraturePoints1D(basis, &Q1d); CeedChk(ierr);
58a2b73c81Sjeremylt     switch (emode) {
598d94b059Sjeremylt     // Interpolate to/from quadrature points
60a2b73c81Sjeremylt     case CEED_EVAL_INTERP: {
61dfe03796SJeremy L Thompson       CeedBasis_Ref *impl;
62dfe03796SJeremy L Thompson       ierr = CeedBasisGetData(basis, (void *)&impl); CeedChk(ierr);
63dfe03796SJeremy L Thompson       if (impl->collointerp) {
64dfe03796SJeremy L Thompson         memcpy(v, u, nelem*ncomp*nnodes*sizeof(u[0]));
65dfe03796SJeremy L Thompson       } else {
664ce2993fSjeremylt         CeedInt P = P1d, Q = Q1d;
6721617c04Sjeremylt         if (tmode == CEED_TRANSPOSE) {
684ce2993fSjeremylt           P = Q1d; Q = P1d;
6921617c04Sjeremylt         }
7084a01de5SJeremy L Thompson         CeedInt pre = ncomp*CeedIntPow(P, dim-1), post = nelem;
7184a01de5SJeremy L Thompson         CeedScalar tmp[2][nelem*ncomp*Q*CeedIntPow(P>Q?P:Q, dim-1)];
724ce2993fSjeremylt         CeedScalar *interp1d;
734ce2993fSjeremylt         ierr = CeedBasisGetInterp(basis, &interp1d); CeedChk(ierr);
7421617c04Sjeremylt         for (CeedInt d=0; d<dim; d++) {
752f86a920SJeremy L Thompson           ierr = CeedTensorContractApply(contract, pre, P, post, Q,
7684a01de5SJeremy L Thompson                                          interp1d, tmode, add&&(d==dim-1),
77dfe03796SJeremy L Thompson                                          d==0?u:tmp[d%2],
78dfe03796SJeremy L Thompson                                          d==dim-1?v:tmp[(d+1)%2]);
7921617c04Sjeremylt           CeedChk(ierr);
8021617c04Sjeremylt           pre /= P;
8121617c04Sjeremylt           post *= Q;
8221617c04Sjeremylt         }
83dfe03796SJeremy L Thompson       }
84a2b73c81Sjeremylt     } break;
858d94b059Sjeremylt     // Evaluate the gradient to/from quadrature points
86a2b73c81Sjeremylt     case CEED_EVAL_GRAD: {
8721617c04Sjeremylt       // In CEED_NOTRANSPOSE mode:
88ecf6354eSJed Brown       // u has shape [dim, ncomp, P^dim, nelem], row-major layout
89ecf6354eSJed Brown       // v has shape [dim, ncomp, Q^dim, nelem], row-major layout
9021617c04Sjeremylt       // In CEED_TRANSPOSE mode, the sizes of u and v are switched.
9184a01de5SJeremy L Thompson       CeedInt P = P1d, Q = Q1d;
9221617c04Sjeremylt       if (tmode == CEED_TRANSPOSE) {
9384a01de5SJeremy L Thompson         P = Q1d, Q = Q1d;
9421617c04Sjeremylt       }
9584a01de5SJeremy L Thompson       CeedBasis_Ref *impl;
9684a01de5SJeremy L Thompson       ierr = CeedBasisGetData(basis, (void *)&impl); CeedChk(ierr);
9784a01de5SJeremy L Thompson       CeedInt pre = ncomp*CeedIntPow(P, dim-1), post = nelem;
9884a01de5SJeremy L Thompson       CeedScalar *interp1d;
994ce2993fSjeremylt       ierr = CeedBasisGetInterp(basis, &interp1d); CeedChk(ierr);
100dfe03796SJeremy L Thompson       if (impl->collograd1d) {
101a7bd39daSjeremylt         CeedScalar tmp[2][nelem*ncomp*Q*CeedIntPow(P>Q?P:Q, dim-1)];
102a7bd39daSjeremylt         CeedScalar interp[nelem*ncomp*Q*CeedIntPow(P>Q?P:Q, dim-1)];
10384a01de5SJeremy L Thompson         // Interpolate to quadrature points (NoTranspose)
10484a01de5SJeremy L Thompson         //  or Grad to quadrature points (Transpose)
10521617c04Sjeremylt         for (CeedInt d=0; d<dim; d++) {
1062f86a920SJeremy L Thompson           ierr = CeedTensorContractApply(contract, pre, P, post, Q,
10784a01de5SJeremy L Thompson                                          (tmode == CEED_NOTRANSPOSE
10884a01de5SJeremy L Thompson                                           ? interp1d
109dfe03796SJeremy L Thompson                                           : impl->collograd1d),
11084a01de5SJeremy L Thompson                                          tmode, add&&(d>0),
11184a01de5SJeremy L Thompson                                          (tmode == CEED_NOTRANSPOSE
11284a01de5SJeremy L Thompson                                           ? (d==0?u:tmp[d%2])
11384a01de5SJeremy L Thompson                                           : u + d*nqpt*ncomp*nelem),
11484a01de5SJeremy L Thompson                                          (tmode == CEED_NOTRANSPOSE
11584a01de5SJeremy L Thompson                                           ? (d==dim-1?interp:tmp[(d+1)%2])
11684a01de5SJeremy L Thompson                                           : interp));
11721617c04Sjeremylt           CeedChk(ierr);
11821617c04Sjeremylt           pre /= P;
11921617c04Sjeremylt           post *= Q;
12021617c04Sjeremylt         }
12184a01de5SJeremy L Thompson         // Grad to quadrature points (NoTranspose)
1228795c945Sjeremylt         //  or Interpolate to nodes (Transpose)
12384a01de5SJeremy L Thompson         P = Q1d, Q = Q1d;
12484a01de5SJeremy L Thompson         if (tmode == CEED_TRANSPOSE) {
12584a01de5SJeremy L Thompson           P = Q1d, Q = P1d;
12684a01de5SJeremy L Thompson         }
12784a01de5SJeremy L Thompson         pre = ncomp*CeedIntPow(P, dim-1), post = nelem;
12884a01de5SJeremy L Thompson         for (CeedInt d=0; d<dim; d++) {
1292f86a920SJeremy L Thompson           ierr = CeedTensorContractApply(contract, pre, P, post, Q,
13084a01de5SJeremy L Thompson                                          (tmode == CEED_NOTRANSPOSE
131dfe03796SJeremy L Thompson                                           ? impl->collograd1d
13284a01de5SJeremy L Thompson                                           : interp1d),
13384a01de5SJeremy L Thompson                                          tmode, add&&(d==dim-1),
13484a01de5SJeremy L Thompson                                          (tmode == CEED_NOTRANSPOSE
13584a01de5SJeremy L Thompson                                           ? interp
13684a01de5SJeremy L Thompson                                           : (d==0?interp:tmp[d%2])),
13784a01de5SJeremy L Thompson                                          (tmode == CEED_NOTRANSPOSE
13884a01de5SJeremy L Thompson                                           ? v + d*nqpt*ncomp*nelem
13984a01de5SJeremy L Thompson                                           : (d==dim-1?v:tmp[(d+1)%2])));
14084a01de5SJeremy L Thompson           CeedChk(ierr);
14184a01de5SJeremy L Thompson           pre /= P;
14284a01de5SJeremy L Thompson           post *= Q;
14321617c04Sjeremylt         }
144dfe03796SJeremy L Thompson       } else if (impl->collointerp) { // Qpts collocated with nodes
145dfe03796SJeremy L Thompson         CeedScalar *grad1d;
146dfe03796SJeremy L Thompson         ierr = CeedBasisGetGrad(basis, &grad1d); CeedChk(ierr);
147dfe03796SJeremy L Thompson 
148dfe03796SJeremy L Thompson         // Dim contractions, identity in other directions
149dfe03796SJeremy L Thompson         CeedInt pre = ncomp*CeedIntPow(P, dim-1), post = nelem;
150*c6158135Sjeremylt         for (CeedInt d=0; d<dim; d++) {
151dfe03796SJeremy L Thompson           ierr = CeedTensorContractApply(contract, pre, P, post, Q,
152a1dbd226Sjeremylt                                          grad1d, tmode, add&&(d>0),
153dfe03796SJeremy L Thompson                                          tmode == CEED_NOTRANSPOSE
154dfe03796SJeremy L Thompson                                          ? u : u+d*ncomp*nqpt*nelem,
155dfe03796SJeremy L Thompson                                          tmode == CEED_TRANSPOSE
156dfe03796SJeremy L Thompson                                          ? v : v+d*ncomp*nqpt*nelem);
157dfe03796SJeremy L Thompson           CeedChk(ierr);
158*c6158135Sjeremylt           pre /= P;
159*c6158135Sjeremylt           post *= Q;
160dfe03796SJeremy L Thompson         }
161a7bd39daSjeremylt       } else { // Underintegration, P > Q
162a7bd39daSjeremylt         CeedScalar *grad1d;
163a7bd39daSjeremylt         ierr = CeedBasisGetGrad(basis, &grad1d); CeedChk(ierr);
164a7bd39daSjeremylt 
165a7bd39daSjeremylt         if (tmode == CEED_TRANSPOSE) {
166a7bd39daSjeremylt           P = Q1d, Q = P1d;
167a7bd39daSjeremylt         }
168a7bd39daSjeremylt         CeedScalar tmp[2][nelem*ncomp*Q*CeedIntPow(P>Q?P:Q, dim-1)];
169a7bd39daSjeremylt 
170a7bd39daSjeremylt         // Dim**2 contractions, apply grad when pass == dim
171a7bd39daSjeremylt         for (CeedInt p=0; p<dim; p++) {
172a7bd39daSjeremylt           CeedInt pre = ncomp*CeedIntPow(P, dim-1), post = nelem;
173a7bd39daSjeremylt           for (CeedInt d=0; d<dim; d++) {
174a7bd39daSjeremylt             ierr = CeedTensorContractApply(contract, pre, P, post, Q,
175a7bd39daSjeremylt                                            (p==d)? grad1d : interp1d,
176a7bd39daSjeremylt                                            tmode, add&&(d==dim-1),
177a7bd39daSjeremylt                                            (d == 0
178a7bd39daSjeremylt                                             ? (tmode == CEED_NOTRANSPOSE
179a7bd39daSjeremylt                                                ? u : u+p*ncomp*nqpt*nelem)
180a7bd39daSjeremylt                                             : tmp[d%2]),
181a7bd39daSjeremylt                                            (d == dim-1
182a7bd39daSjeremylt                                             ? (tmode == CEED_TRANSPOSE
183a7bd39daSjeremylt                                                ? v : v+p*ncomp*nqpt*nelem)
184a7bd39daSjeremylt                                             : tmp[(d+1)%2]));
185a7bd39daSjeremylt             CeedChk(ierr);
186a7bd39daSjeremylt             pre /= P;
187a7bd39daSjeremylt             post *= Q;
188a7bd39daSjeremylt           }
189a7bd39daSjeremylt         }
190a7bd39daSjeremylt       }
191a2b73c81Sjeremylt     } break;
1928d94b059Sjeremylt     // Retrieve interpolation weights
193a2b73c81Sjeremylt     case CEED_EVAL_WEIGHT: {
19421617c04Sjeremylt       if (tmode == CEED_TRANSPOSE)
195c042f62fSJeremy L Thompson         // LCOV_EXCL_START
1964ce2993fSjeremylt         return CeedError(ceed, 1,
19721617c04Sjeremylt                          "CEED_EVAL_WEIGHT incompatible with CEED_TRANSPOSE");
198c042f62fSJeremy L Thompson       // LCOV_EXCL_STOP
1994ce2993fSjeremylt       CeedInt Q = Q1d;
2004ce2993fSjeremylt       CeedScalar *qweight1d;
2014ce2993fSjeremylt       ierr = CeedBasisGetQWeights(basis, &qweight1d); CeedChk(ierr);
20221617c04Sjeremylt       for (CeedInt d=0; d<dim; d++) {
203b5cf12eeSjeremylt         CeedInt pre = CeedIntPow(Q, dim-d-1), post = CeedIntPow(Q, d);
204a2b73c81Sjeremylt         for (CeedInt i=0; i<pre; i++)
205a2b73c81Sjeremylt           for (CeedInt j=0; j<Q; j++)
20684a01de5SJeremy L Thompson             for (CeedInt k=0; k<post; k++) {
20784a01de5SJeremy L Thompson               CeedScalar w = qweight1d[j]
20884a01de5SJeremy L Thompson                              * (d == 0 ? 1 : v[((i*Q + j)*post + k)*nelem]);
20984a01de5SJeremy L Thompson               for (CeedInt e=0; e<nelem; e++)
21084a01de5SJeremy L Thompson                 v[((i*Q + j)*post + k)*nelem + e] = w;
21184a01de5SJeremy L Thompson             }
21221617c04Sjeremylt       }
213a2b73c81Sjeremylt     } break;
214c042f62fSJeremy L Thompson     // LCOV_EXCL_START
2158d94b059Sjeremylt     // Evaluate the divergence to/from the quadrature points
216a2b73c81Sjeremylt     case CEED_EVAL_DIV:
2174ce2993fSjeremylt       return CeedError(ceed, 1, "CEED_EVAL_DIV not supported");
2188d94b059Sjeremylt     // Evaluate the curl to/from the quadrature points
219a2b73c81Sjeremylt     case CEED_EVAL_CURL:
2204ce2993fSjeremylt       return CeedError(ceed, 1, "CEED_EVAL_CURL not supported");
2218d94b059Sjeremylt     // Take no action, BasisApply should not have been called
222a2b73c81Sjeremylt     case CEED_EVAL_NONE:
2234ce2993fSjeremylt       return CeedError(ceed, 1,
2244b8bea3bSJed Brown                        "CEED_EVAL_NONE does not make sense in this context");
225c042f62fSJeremy L Thompson       // LCOV_EXCL_STOP
22621617c04Sjeremylt     }
227a8de75f0Sjeremylt   } else {
228a8de75f0Sjeremylt     // Non-tensor basis
229a8de75f0Sjeremylt     switch (emode) {
23084a01de5SJeremy L Thompson     // Interpolate to/from quadrature points
231a8de75f0Sjeremylt     case CEED_EVAL_INTERP: {
2328795c945Sjeremylt       CeedInt P = nnodes, Q = nqpt;
2334ce2993fSjeremylt       CeedScalar *interp;
2344ce2993fSjeremylt       ierr = CeedBasisGetInterp(basis, &interp); CeedChk(ierr);
235a8de75f0Sjeremylt       if (tmode == CEED_TRANSPOSE) {
2368795c945Sjeremylt         P = nqpt; Q = nnodes;
237a8de75f0Sjeremylt       }
2382f86a920SJeremy L Thompson       ierr = CeedTensorContractApply(contract, ncomp, P, nelem, Q,
23984a01de5SJeremy L Thompson                                      interp, tmode, add, u, v);
240a8de75f0Sjeremylt       CeedChk(ierr);
241a8de75f0Sjeremylt     }
242a8de75f0Sjeremylt     break;
24384a01de5SJeremy L Thompson     // Evaluate the gradient to/from quadrature points
244a8de75f0Sjeremylt     case CEED_EVAL_GRAD: {
245475a782bSnbeams       CeedInt P = nnodes, Q = nqpt;
246475a782bSnbeams       CeedInt dimstride = nqpt * ncomp * nelem;
247475a782bSnbeams       CeedInt gradstride = nqpt * nnodes;
2484ce2993fSjeremylt       CeedScalar *grad;
2494ce2993fSjeremylt       ierr = CeedBasisGetGrad(basis, &grad); CeedChk(ierr);
250a8de75f0Sjeremylt       if (tmode == CEED_TRANSPOSE) {
251475a782bSnbeams         P = nqpt; Q = nnodes;
252475a782bSnbeams         for (CeedInt d = 0; d < dim; d++) {
2532f86a920SJeremy L Thompson           ierr = CeedTensorContractApply(contract, ncomp, P, nelem, Q,
254475a782bSnbeams                                          grad + d * gradstride, tmode, add,
255475a782bSnbeams                                          u + d * dimstride, v); CeedChk(ierr);
256475a782bSnbeams         }
257475a782bSnbeams       } else {
258475a782bSnbeams         for (CeedInt d = 0; d < dim; d++) {
259475a782bSnbeams           ierr = CeedTensorContractApply(contract, ncomp, P, nelem, Q,
260475a782bSnbeams                                          grad + d * gradstride, tmode, add,
261475a782bSnbeams                                          u, v + d * dimstride); CeedChk(ierr);
262475a782bSnbeams         }
263475a782bSnbeams       }
264a8de75f0Sjeremylt     }
265a8de75f0Sjeremylt     break;
26684a01de5SJeremy L Thompson     // Retrieve interpolation weights
267a8de75f0Sjeremylt     case CEED_EVAL_WEIGHT: {
268a8de75f0Sjeremylt       if (tmode == CEED_TRANSPOSE)
269c042f62fSJeremy L Thompson         // LCOV_EXCL_START
2704ce2993fSjeremylt         return CeedError(ceed, 1,
271a8de75f0Sjeremylt                          "CEED_EVAL_WEIGHT incompatible with CEED_TRANSPOSE");
272c042f62fSJeremy L Thompson       // LCOV_EXCL_STOP
2734ce2993fSjeremylt       CeedScalar *qweight;
2744ce2993fSjeremylt       ierr = CeedBasisGetQWeights(basis, &qweight); CeedChk(ierr);
275a8de75f0Sjeremylt       for (CeedInt i=0; i<nqpt; i++)
27684a01de5SJeremy L Thompson         for (CeedInt e=0; e<nelem; e++)
27784a01de5SJeremy L Thompson           v[i*nelem + e] = qweight[i];
278a8de75f0Sjeremylt     } break;
279c042f62fSJeremy L Thompson     // LCOV_EXCL_START
28084a01de5SJeremy L Thompson     // Evaluate the divergence to/from the quadrature points
281a8de75f0Sjeremylt     case CEED_EVAL_DIV:
2824ce2993fSjeremylt       return CeedError(ceed, 1, "CEED_EVAL_DIV not supported");
28384a01de5SJeremy L Thompson     // Evaluate the curl to/from the quadrature points
284a8de75f0Sjeremylt     case CEED_EVAL_CURL:
2854ce2993fSjeremylt       return CeedError(ceed, 1, "CEED_EVAL_CURL not supported");
28684a01de5SJeremy L Thompson     // Take no action, BasisApply should not have been called
287a8de75f0Sjeremylt     case CEED_EVAL_NONE:
2884ce2993fSjeremylt       return CeedError(ceed, 1,
289a8de75f0Sjeremylt                        "CEED_EVAL_NONE does not make sense in this context");
290c042f62fSJeremy L Thompson       // LCOV_EXCL_STOP
291a8de75f0Sjeremylt     }
292a8de75f0Sjeremylt   }
293aedaa0e5Sjeremylt   if (U) {
294aedaa0e5Sjeremylt     ierr = CeedVectorRestoreArrayRead(U, &u); CeedChk(ierr);
295aedaa0e5Sjeremylt   }
296aedaa0e5Sjeremylt   ierr = CeedVectorRestoreArray(V, &v); CeedChk(ierr);
29721617c04Sjeremylt   return 0;
29821617c04Sjeremylt }
29921617c04Sjeremylt 
30084a01de5SJeremy L Thompson static int CeedBasisDestroyNonTensor_Ref(CeedBasis basis) {
3012f86a920SJeremy L Thompson   int ierr;
3022f86a920SJeremy L Thompson   CeedTensorContract contract;
3032f86a920SJeremy L Thompson   ierr = CeedBasisGetTensorContract(basis, &contract); CeedChk(ierr);
3042f86a920SJeremy L Thompson   ierr = CeedTensorContractDestroy(&contract); CeedChk(ierr);
30584a01de5SJeremy L Thompson   return 0;
30684a01de5SJeremy L Thompson }
30784a01de5SJeremy L Thompson 
30884a01de5SJeremy L Thompson static int CeedBasisDestroyTensor_Ref(CeedBasis basis) {
30984a01de5SJeremy L Thompson   int ierr;
3102f86a920SJeremy L Thompson   CeedTensorContract contract;
3112f86a920SJeremy L Thompson   ierr = CeedBasisGetTensorContract(basis, &contract); CeedChk(ierr);
3122f86a920SJeremy L Thompson   ierr = CeedTensorContractDestroy(&contract); CeedChk(ierr);
3132f86a920SJeremy L Thompson 
31484a01de5SJeremy L Thompson   CeedBasis_Ref *impl;
31584a01de5SJeremy L Thompson   ierr = CeedBasisGetData(basis, (void *)&impl); CeedChk(ierr);
316dfe03796SJeremy L Thompson   ierr = CeedFree(&impl->collograd1d); CeedChk(ierr);
31784a01de5SJeremy L Thompson   ierr = CeedFree(&impl); CeedChk(ierr);
31884a01de5SJeremy L Thompson 
31921617c04Sjeremylt   return 0;
32021617c04Sjeremylt }
32121617c04Sjeremylt 
322667bc5fcSjeremylt int CeedBasisCreateTensorH1_Ref(CeedInt dim, CeedInt P1d,
32321617c04Sjeremylt                                 CeedInt Q1d, const CeedScalar *interp1d,
32421617c04Sjeremylt                                 const CeedScalar *grad1d,
32521617c04Sjeremylt                                 const CeedScalar *qref1d,
32621617c04Sjeremylt                                 const CeedScalar *qweight1d,
32721617c04Sjeremylt                                 CeedBasis basis) {
328fe2413ffSjeremylt   int ierr;
329fe2413ffSjeremylt   Ceed ceed;
330fe2413ffSjeremylt   ierr = CeedBasisGetCeed(basis, &ceed); CeedChk(ierr);
33184a01de5SJeremy L Thompson   CeedBasis_Ref *impl;
33284a01de5SJeremy L Thompson   ierr = CeedCalloc(1, &impl); CeedChk(ierr);
333dfe03796SJeremy L Thompson   // Check for collocated interp
334dfe03796SJeremy L Thompson   if (Q1d == P1d) {
335dfe03796SJeremy L Thompson     bool collocated = 1;
336dfe03796SJeremy L Thompson     for (CeedInt i=0; i<P1d; i++) {
337dfe03796SJeremy L Thompson       collocated = collocated && (fabs(interp1d[i+P1d*i] - 1.0) < 1e-14);
338dfe03796SJeremy L Thompson       for (CeedInt j=0; j<P1d; j++)
339dfe03796SJeremy L Thompson         if (j != i)
340dfe03796SJeremy L Thompson           collocated = collocated && (fabs(interp1d[j+P1d*i]) < 1e-14);
341dfe03796SJeremy L Thompson     }
342dfe03796SJeremy L Thompson     impl->collointerp = collocated;
343dfe03796SJeremy L Thompson   }
344dfe03796SJeremy L Thompson   // Calculate collocated grad
345dfe03796SJeremy L Thompson   if (Q1d >= P1d && !impl->collointerp) {
346dfe03796SJeremy L Thompson     ierr = CeedMalloc(Q1d*Q1d, &impl->collograd1d); CeedChk(ierr);
347dfe03796SJeremy L Thompson     ierr = CeedBasisGetCollocatedGrad(basis, impl->collograd1d); CeedChk(ierr);
348a7bd39daSjeremylt   }
34984a01de5SJeremy L Thompson   ierr = CeedBasisSetData(basis, (void *)&impl); CeedChk(ierr);
350fe2413ffSjeremylt 
3512f86a920SJeremy L Thompson   Ceed parent;
352de686571SJeremy L Thompson   ierr = CeedGetParent(ceed, &parent); CeedChk(ierr);
3532f86a920SJeremy L Thompson   CeedTensorContract contract;
354c71e1dcdSjeremylt   ierr = CeedTensorContractCreate(parent, basis, &contract); CeedChk(ierr);
3552f86a920SJeremy L Thompson   ierr = CeedBasisSetTensorContract(basis, &contract); CeedChk(ierr);
3562f86a920SJeremy L Thompson 
357fe2413ffSjeremylt   ierr = CeedSetBackendFunction(ceed, "Basis", basis, "Apply",
358fe2413ffSjeremylt                                 CeedBasisApply_Ref); CeedChk(ierr);
359fe2413ffSjeremylt   ierr = CeedSetBackendFunction(ceed, "Basis", basis, "Destroy",
36084a01de5SJeremy L Thompson                                 CeedBasisDestroyTensor_Ref); CeedChk(ierr);
36121617c04Sjeremylt   return 0;
36221617c04Sjeremylt }
363a8de75f0Sjeremylt 
36484a01de5SJeremy L Thompson 
36584a01de5SJeremy L Thompson 
366667bc5fcSjeremylt int CeedBasisCreateH1_Ref(CeedElemTopology topo, CeedInt dim,
3678795c945Sjeremylt                           CeedInt nnodes, CeedInt nqpts,
368a8de75f0Sjeremylt                           const CeedScalar *interp,
369a8de75f0Sjeremylt                           const CeedScalar *grad,
370a8de75f0Sjeremylt                           const CeedScalar *qref,
371a8de75f0Sjeremylt                           const CeedScalar *qweight,
372a8de75f0Sjeremylt                           CeedBasis basis) {
373fe2413ffSjeremylt   int ierr;
374fe2413ffSjeremylt   Ceed ceed;
375fe2413ffSjeremylt   ierr = CeedBasisGetCeed(basis, &ceed); CeedChk(ierr);
376fe2413ffSjeremylt 
377c71e1dcdSjeremylt   Ceed parent;
378c71e1dcdSjeremylt   ierr = CeedGetParent(ceed, &parent); CeedChk(ierr);
3792f86a920SJeremy L Thompson   CeedTensorContract contract;
380c71e1dcdSjeremylt   ierr = CeedTensorContractCreate(parent, basis, &contract); CeedChk(ierr);
3812f86a920SJeremy L Thompson   ierr = CeedBasisSetTensorContract(basis, &contract); CeedChk(ierr);
3822f86a920SJeremy L Thompson 
383fe2413ffSjeremylt   ierr = CeedSetBackendFunction(ceed, "Basis", basis, "Apply",
384fe2413ffSjeremylt                                 CeedBasisApply_Ref); CeedChk(ierr);
385fe2413ffSjeremylt   ierr = CeedSetBackendFunction(ceed, "Basis", basis, "Destroy",
38684a01de5SJeremy L Thompson                                 CeedBasisDestroyNonTensor_Ref); CeedChk(ierr);
38784a01de5SJeremy L Thompson 
388a8de75f0Sjeremylt   return 0;
389a8de75f0Sjeremylt }
390