xref: /libCEED/rust/libceed-sys/c-src/backends/ref/ceed-ref-basis.c (revision dfe037969ae081f7a6317a1495a1b0d10c389fbf)
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) {
38aedaa0e5Sjeremylt     return CeedError(ceed, 1,
39aedaa0e5Sjeremylt                      "An input vector is required for this CeedEvalMode");
40aedaa0e5Sjeremylt   }
41aedaa0e5Sjeremylt   ierr = CeedVectorGetArray(V, CEED_MEM_HOST, &v); CeedChk(ierr);
4221617c04Sjeremylt 
438d94b059Sjeremylt   // Clear v if operating in transpose
4421617c04Sjeremylt   if (tmode == CEED_TRANSPOSE) {
458795c945Sjeremylt     const CeedInt vsize = nelem*ncomp*nnodes;
4621617c04Sjeremylt     for (CeedInt i = 0; i < vsize; i++)
4784a01de5SJeremy L Thompson       v[i] = (CeedScalar) 0.0;
4821617c04Sjeremylt   }
494ce2993fSjeremylt   bool tensorbasis;
504ce2993fSjeremylt   ierr = CeedBasisGetTensorStatus(basis, &tensorbasis); CeedChk(ierr);
5184a01de5SJeremy L Thompson   // Tensor basis
524ce2993fSjeremylt   if (tensorbasis) {
534ce2993fSjeremylt     CeedInt P1d, Q1d;
544ce2993fSjeremylt     ierr = CeedBasisGetNumNodes1D(basis, &P1d); CeedChk(ierr);
554ce2993fSjeremylt     ierr = CeedBasisGetNumQuadraturePoints1D(basis, &Q1d); CeedChk(ierr);
56a2b73c81Sjeremylt     switch (emode) {
578d94b059Sjeremylt     // Interpolate to/from quadrature points
58a2b73c81Sjeremylt     case CEED_EVAL_INTERP: {
59*dfe03796SJeremy L Thompson       CeedBasis_Ref *impl;
60*dfe03796SJeremy L Thompson       ierr = CeedBasisGetData(basis, (void *)&impl); CeedChk(ierr);
61*dfe03796SJeremy L Thompson       if (impl->collointerp) {
62*dfe03796SJeremy L Thompson         memcpy(v, u, nelem*ncomp*nnodes*sizeof(u[0]));
63*dfe03796SJeremy L Thompson       } else {
644ce2993fSjeremylt         CeedInt P = P1d, Q = Q1d;
6521617c04Sjeremylt         if (tmode == CEED_TRANSPOSE) {
664ce2993fSjeremylt           P = Q1d; Q = P1d;
6721617c04Sjeremylt         }
6884a01de5SJeremy L Thompson         CeedInt pre = ncomp*CeedIntPow(P, dim-1), post = nelem;
6984a01de5SJeremy L Thompson         CeedScalar tmp[2][nelem*ncomp*Q*CeedIntPow(P>Q?P:Q, dim-1)];
704ce2993fSjeremylt         CeedScalar *interp1d;
714ce2993fSjeremylt         ierr = CeedBasisGetInterp(basis, &interp1d); CeedChk(ierr);
7221617c04Sjeremylt         for (CeedInt d=0; d<dim; d++) {
732f86a920SJeremy L Thompson           ierr = CeedTensorContractApply(contract, pre, P, post, Q,
7484a01de5SJeremy L Thompson                                          interp1d, tmode, add&&(d==dim-1),
75*dfe03796SJeremy L Thompson                                          d==0?u:tmp[d%2],
76*dfe03796SJeremy L Thompson                                          d==dim-1?v:tmp[(d+1)%2]);
7721617c04Sjeremylt           CeedChk(ierr);
7821617c04Sjeremylt           pre /= P;
7921617c04Sjeremylt           post *= Q;
8021617c04Sjeremylt         }
81*dfe03796SJeremy L Thompson       }
82a2b73c81Sjeremylt     } break;
838d94b059Sjeremylt     // Evaluate the gradient to/from quadrature points
84a2b73c81Sjeremylt     case CEED_EVAL_GRAD: {
8521617c04Sjeremylt       // In CEED_NOTRANSPOSE mode:
86ecf6354eSJed Brown       // u has shape [dim, ncomp, P^dim, nelem], row-major layout
87ecf6354eSJed Brown       // v has shape [dim, ncomp, Q^dim, nelem], row-major layout
8821617c04Sjeremylt       // In CEED_TRANSPOSE mode, the sizes of u and v are switched.
8984a01de5SJeremy L Thompson       CeedInt P = P1d, Q = Q1d;
9021617c04Sjeremylt       if (tmode == CEED_TRANSPOSE) {
9184a01de5SJeremy L Thompson         P = Q1d, Q = Q1d;
9221617c04Sjeremylt       }
9384a01de5SJeremy L Thompson       CeedBasis_Ref *impl;
9484a01de5SJeremy L Thompson       ierr = CeedBasisGetData(basis, (void *)&impl); CeedChk(ierr);
9584a01de5SJeremy L Thompson       CeedInt pre = ncomp*CeedIntPow(P, dim-1), post = nelem;
9684a01de5SJeremy L Thompson       CeedScalar *interp1d;
974ce2993fSjeremylt       ierr = CeedBasisGetInterp(basis, &interp1d); CeedChk(ierr);
98*dfe03796SJeremy L Thompson       if (impl->collograd1d) {
99a7bd39daSjeremylt         CeedScalar tmp[2][nelem*ncomp*Q*CeedIntPow(P>Q?P:Q, dim-1)];
100a7bd39daSjeremylt         CeedScalar interp[nelem*ncomp*Q*CeedIntPow(P>Q?P:Q, dim-1)];
10184a01de5SJeremy L Thompson         // Interpolate to quadrature points (NoTranspose)
10284a01de5SJeremy L Thompson         //  or Grad to quadrature points (Transpose)
10321617c04Sjeremylt         for (CeedInt d=0; d<dim; d++) {
1042f86a920SJeremy L Thompson           ierr = CeedTensorContractApply(contract, pre, P, post, Q,
10584a01de5SJeremy L Thompson                                          (tmode == CEED_NOTRANSPOSE
10684a01de5SJeremy L Thompson                                           ? interp1d
107*dfe03796SJeremy L Thompson                                           : impl->collograd1d),
10884a01de5SJeremy L Thompson                                          tmode, add&&(d>0),
10984a01de5SJeremy L Thompson                                          (tmode == CEED_NOTRANSPOSE
11084a01de5SJeremy L Thompson                                           ? (d==0?u:tmp[d%2])
11184a01de5SJeremy L Thompson                                           : u + d*nqpt*ncomp*nelem),
11284a01de5SJeremy L Thompson                                          (tmode == CEED_NOTRANSPOSE
11384a01de5SJeremy L Thompson                                           ? (d==dim-1?interp:tmp[(d+1)%2])
11484a01de5SJeremy L Thompson                                           : interp));
11521617c04Sjeremylt           CeedChk(ierr);
11621617c04Sjeremylt           pre /= P;
11721617c04Sjeremylt           post *= Q;
11821617c04Sjeremylt         }
11984a01de5SJeremy L Thompson         // Grad to quadrature points (NoTranspose)
1208795c945Sjeremylt         //  or Interpolate to nodes (Transpose)
12184a01de5SJeremy L Thompson         P = Q1d, Q = Q1d;
12284a01de5SJeremy L Thompson         if (tmode == CEED_TRANSPOSE) {
12384a01de5SJeremy L Thompson           P = Q1d, Q = P1d;
12484a01de5SJeremy L Thompson         }
12584a01de5SJeremy L Thompson         pre = ncomp*CeedIntPow(P, dim-1), post = nelem;
12684a01de5SJeremy L Thompson         for (CeedInt d=0; d<dim; d++) {
1272f86a920SJeremy L Thompson           ierr = CeedTensorContractApply(contract, pre, P, post, Q,
12884a01de5SJeremy L Thompson                                          (tmode == CEED_NOTRANSPOSE
129*dfe03796SJeremy L Thompson                                           ? impl->collograd1d
13084a01de5SJeremy L Thompson                                           : interp1d),
13184a01de5SJeremy L Thompson                                          tmode, add&&(d==dim-1),
13284a01de5SJeremy L Thompson                                          (tmode == CEED_NOTRANSPOSE
13384a01de5SJeremy L Thompson                                           ? interp
13484a01de5SJeremy L Thompson                                           : (d==0?interp:tmp[d%2])),
13584a01de5SJeremy L Thompson                                          (tmode == CEED_NOTRANSPOSE
13684a01de5SJeremy L Thompson                                           ? v + d*nqpt*ncomp*nelem
13784a01de5SJeremy L Thompson                                           : (d==dim-1?v:tmp[(d+1)%2])));
13884a01de5SJeremy L Thompson           CeedChk(ierr);
13984a01de5SJeremy L Thompson           pre /= P;
14084a01de5SJeremy L Thompson           post *= Q;
14121617c04Sjeremylt         }
142*dfe03796SJeremy L Thompson       } else if (impl->collointerp) { // Qpts collocated with nodes
143*dfe03796SJeremy L Thompson         CeedScalar *grad1d;
144*dfe03796SJeremy L Thompson         ierr = CeedBasisGetGrad(basis, &grad1d); CeedChk(ierr);
145*dfe03796SJeremy L Thompson 
146*dfe03796SJeremy L Thompson         // Dim contractions, identity in other directions
147*dfe03796SJeremy L Thompson         for (CeedInt d=0; d<dim; d++) {
148*dfe03796SJeremy L Thompson           CeedInt pre = ncomp*CeedIntPow(P, dim-1), post = nelem;
149*dfe03796SJeremy L Thompson             ierr = CeedTensorContractApply(contract, pre, P, post, Q,
150*dfe03796SJeremy L Thompson                                            grad1d,
151*dfe03796SJeremy L Thompson                                            tmode, add&&(d>0),
152*dfe03796SJeremy L Thompson                                            tmode == CEED_NOTRANSPOSE
153*dfe03796SJeremy L Thompson                                              ? u : u+d*ncomp*nqpt*nelem,
154*dfe03796SJeremy L Thompson                                            tmode == CEED_TRANSPOSE
155*dfe03796SJeremy L Thompson                                              ? v : v+d*ncomp*nqpt*nelem);
156*dfe03796SJeremy L Thompson             CeedChk(ierr);
157*dfe03796SJeremy L Thompson             pre /= P;
158*dfe03796SJeremy L Thompson             post *= Q;
159*dfe03796SJeremy L Thompson         }
160a7bd39daSjeremylt       } else { // Underintegration, P > Q
161a7bd39daSjeremylt         CeedScalar *grad1d;
162a7bd39daSjeremylt         ierr = CeedBasisGetGrad(basis, &grad1d); CeedChk(ierr);
163a7bd39daSjeremylt 
164a7bd39daSjeremylt         if (tmode == CEED_TRANSPOSE) {
165a7bd39daSjeremylt           P = Q1d, Q = P1d;
166a7bd39daSjeremylt         }
167a7bd39daSjeremylt         CeedScalar tmp[2][nelem*ncomp*Q*CeedIntPow(P>Q?P:Q, dim-1)];
168a7bd39daSjeremylt 
169a7bd39daSjeremylt         // Dim**2 contractions, apply grad when pass == dim
170a7bd39daSjeremylt         for (CeedInt p=0; p<dim; p++) {
171a7bd39daSjeremylt           CeedInt pre = ncomp*CeedIntPow(P, dim-1), post = nelem;
172a7bd39daSjeremylt           for (CeedInt d=0; d<dim; d++) {
173a7bd39daSjeremylt             ierr = CeedTensorContractApply(contract, pre, P, post, Q,
174a7bd39daSjeremylt                                            (p==d)? grad1d : interp1d,
175a7bd39daSjeremylt                                            tmode, add&&(d==dim-1),
176a7bd39daSjeremylt                                            (d == 0
177a7bd39daSjeremylt                                             ? (tmode == CEED_NOTRANSPOSE
178a7bd39daSjeremylt                                                ? u : u+p*ncomp*nqpt*nelem)
179a7bd39daSjeremylt                                             : tmp[d%2]),
180a7bd39daSjeremylt                                            (d == dim-1
181a7bd39daSjeremylt                                             ? (tmode == CEED_TRANSPOSE
182a7bd39daSjeremylt                                                ? v : v+p*ncomp*nqpt*nelem)
183a7bd39daSjeremylt                                             : tmp[(d+1)%2]));
184a7bd39daSjeremylt             CeedChk(ierr);
185a7bd39daSjeremylt             pre /= P;
186a7bd39daSjeremylt             post *= Q;
187a7bd39daSjeremylt           }
188a7bd39daSjeremylt         }
189a7bd39daSjeremylt       }
190a2b73c81Sjeremylt     } break;
1918d94b059Sjeremylt     // Retrieve interpolation weights
192a2b73c81Sjeremylt     case CEED_EVAL_WEIGHT: {
19321617c04Sjeremylt       if (tmode == CEED_TRANSPOSE)
1944ce2993fSjeremylt         return CeedError(ceed, 1,
19521617c04Sjeremylt                          "CEED_EVAL_WEIGHT incompatible with CEED_TRANSPOSE");
1964ce2993fSjeremylt       CeedInt Q = Q1d;
1974ce2993fSjeremylt       CeedScalar *qweight1d;
1984ce2993fSjeremylt       ierr = CeedBasisGetQWeights(basis, &qweight1d); CeedChk(ierr);
19921617c04Sjeremylt       for (CeedInt d=0; d<dim; d++) {
200b5cf12eeSjeremylt         CeedInt pre = CeedIntPow(Q, dim-d-1), post = CeedIntPow(Q, d);
201a2b73c81Sjeremylt         for (CeedInt i=0; i<pre; i++)
202a2b73c81Sjeremylt           for (CeedInt j=0; j<Q; j++)
20384a01de5SJeremy L Thompson             for (CeedInt k=0; k<post; k++) {
20484a01de5SJeremy L Thompson               CeedScalar w = qweight1d[j]
20584a01de5SJeremy L Thompson                              * (d == 0 ? 1 : v[((i*Q + j)*post + k)*nelem]);
20684a01de5SJeremy L Thompson               for (CeedInt e=0; e<nelem; e++)
20784a01de5SJeremy L Thompson                 v[((i*Q + j)*post + k)*nelem + e] = w;
20884a01de5SJeremy L Thompson             }
20921617c04Sjeremylt       }
210a2b73c81Sjeremylt     } break;
2118d94b059Sjeremylt     // Evaluate the divergence to/from the quadrature points
212a2b73c81Sjeremylt     case CEED_EVAL_DIV:
2134ce2993fSjeremylt       return CeedError(ceed, 1, "CEED_EVAL_DIV not supported");
2148d94b059Sjeremylt     // Evaluate the curl to/from the quadrature points
215a2b73c81Sjeremylt     case CEED_EVAL_CURL:
2164ce2993fSjeremylt       return CeedError(ceed, 1, "CEED_EVAL_CURL not supported");
2178d94b059Sjeremylt     // Take no action, BasisApply should not have been called
218a2b73c81Sjeremylt     case CEED_EVAL_NONE:
2194ce2993fSjeremylt       return CeedError(ceed, 1,
2204b8bea3bSJed Brown                        "CEED_EVAL_NONE does not make sense in this context");
22121617c04Sjeremylt     }
222a8de75f0Sjeremylt   } else {
223a8de75f0Sjeremylt     // Non-tensor basis
224a8de75f0Sjeremylt     switch (emode) {
22584a01de5SJeremy L Thompson     // Interpolate to/from quadrature points
226a8de75f0Sjeremylt     case CEED_EVAL_INTERP: {
2278795c945Sjeremylt       CeedInt P = nnodes, Q = nqpt;
2284ce2993fSjeremylt       CeedScalar *interp;
2294ce2993fSjeremylt       ierr = CeedBasisGetInterp(basis, &interp); CeedChk(ierr);
230a8de75f0Sjeremylt       if (tmode == CEED_TRANSPOSE) {
2318795c945Sjeremylt         P = nqpt; Q = nnodes;
232a8de75f0Sjeremylt       }
2332f86a920SJeremy L Thompson       ierr = CeedTensorContractApply(contract, ncomp, P, nelem, Q,
23484a01de5SJeremy L Thompson                                      interp, tmode, add, u, v);
235a8de75f0Sjeremylt       CeedChk(ierr);
236a8de75f0Sjeremylt     }
237a8de75f0Sjeremylt     break;
23884a01de5SJeremy L Thompson     // Evaluate the gradient to/from quadrature points
239a8de75f0Sjeremylt     case CEED_EVAL_GRAD: {
2408795c945Sjeremylt       CeedInt P = nnodes, Q = dim*nqpt;
2414ce2993fSjeremylt       CeedScalar *grad;
2424ce2993fSjeremylt       ierr = CeedBasisGetGrad(basis, &grad); CeedChk(ierr);
243a8de75f0Sjeremylt       if (tmode == CEED_TRANSPOSE) {
2448795c945Sjeremylt         P = dim*nqpt; Q = nnodes;
245a8de75f0Sjeremylt       }
2462f86a920SJeremy L Thompson       ierr = CeedTensorContractApply(contract, ncomp, P, nelem, Q,
24784a01de5SJeremy L Thompson                                      grad, tmode, add, u, v);
248a8de75f0Sjeremylt       CeedChk(ierr);
249a8de75f0Sjeremylt     }
250a8de75f0Sjeremylt     break;
25184a01de5SJeremy L Thompson     // Retrieve interpolation weights
252a8de75f0Sjeremylt     case CEED_EVAL_WEIGHT: {
253a8de75f0Sjeremylt       if (tmode == CEED_TRANSPOSE)
2544ce2993fSjeremylt         return CeedError(ceed, 1,
255a8de75f0Sjeremylt                          "CEED_EVAL_WEIGHT incompatible with CEED_TRANSPOSE");
2564ce2993fSjeremylt       CeedScalar *qweight;
2574ce2993fSjeremylt       ierr = CeedBasisGetQWeights(basis, &qweight); CeedChk(ierr);
258a8de75f0Sjeremylt       for (CeedInt i=0; i<nqpt; i++)
25984a01de5SJeremy L Thompson         for (CeedInt e=0; e<nelem; e++)
26084a01de5SJeremy L Thompson           v[i*nelem + e] = qweight[i];
261a8de75f0Sjeremylt     } break;
26284a01de5SJeremy L Thompson     // Evaluate the divergence to/from the quadrature points
263a8de75f0Sjeremylt     case CEED_EVAL_DIV:
2644ce2993fSjeremylt       return CeedError(ceed, 1, "CEED_EVAL_DIV not supported");
26584a01de5SJeremy L Thompson     // Evaluate the curl to/from the quadrature points
266a8de75f0Sjeremylt     case CEED_EVAL_CURL:
2674ce2993fSjeremylt       return CeedError(ceed, 1, "CEED_EVAL_CURL not supported");
26884a01de5SJeremy L Thompson     // Take no action, BasisApply should not have been called
269a8de75f0Sjeremylt     case CEED_EVAL_NONE:
2704ce2993fSjeremylt       return CeedError(ceed, 1,
271a8de75f0Sjeremylt                        "CEED_EVAL_NONE does not make sense in this context");
272a8de75f0Sjeremylt     }
273a8de75f0Sjeremylt   }
274aedaa0e5Sjeremylt   if (U) {
275aedaa0e5Sjeremylt     ierr = CeedVectorRestoreArrayRead(U, &u); CeedChk(ierr);
276aedaa0e5Sjeremylt   }
277aedaa0e5Sjeremylt   ierr = CeedVectorRestoreArray(V, &v); CeedChk(ierr);
27821617c04Sjeremylt   return 0;
27921617c04Sjeremylt }
28021617c04Sjeremylt 
28184a01de5SJeremy L Thompson static int CeedBasisDestroyNonTensor_Ref(CeedBasis basis) {
2822f86a920SJeremy L Thompson   int ierr;
2832f86a920SJeremy L Thompson   CeedTensorContract contract;
2842f86a920SJeremy L Thompson   ierr = CeedBasisGetTensorContract(basis, &contract); CeedChk(ierr);
2852f86a920SJeremy L Thompson   ierr = CeedTensorContractDestroy(&contract); CeedChk(ierr);
28684a01de5SJeremy L Thompson   return 0;
28784a01de5SJeremy L Thompson }
28884a01de5SJeremy L Thompson 
28984a01de5SJeremy L Thompson static int CeedBasisDestroyTensor_Ref(CeedBasis basis) {
29084a01de5SJeremy L Thompson   int ierr;
2912f86a920SJeremy L Thompson   CeedTensorContract contract;
2922f86a920SJeremy L Thompson   ierr = CeedBasisGetTensorContract(basis, &contract); CeedChk(ierr);
2932f86a920SJeremy L Thompson   ierr = CeedTensorContractDestroy(&contract); CeedChk(ierr);
2942f86a920SJeremy L Thompson 
29584a01de5SJeremy L Thompson   CeedBasis_Ref *impl;
29684a01de5SJeremy L Thompson   ierr = CeedBasisGetData(basis, (void *)&impl); CeedChk(ierr);
297*dfe03796SJeremy L Thompson   ierr = CeedFree(&impl->collograd1d); CeedChk(ierr);
29884a01de5SJeremy L Thompson   ierr = CeedFree(&impl); CeedChk(ierr);
29984a01de5SJeremy L Thompson 
30021617c04Sjeremylt   return 0;
30121617c04Sjeremylt }
30221617c04Sjeremylt 
303667bc5fcSjeremylt int CeedBasisCreateTensorH1_Ref(CeedInt dim, CeedInt P1d,
30421617c04Sjeremylt                                 CeedInt Q1d, const CeedScalar *interp1d,
30521617c04Sjeremylt                                 const CeedScalar *grad1d,
30621617c04Sjeremylt                                 const CeedScalar *qref1d,
30721617c04Sjeremylt                                 const CeedScalar *qweight1d,
30821617c04Sjeremylt                                 CeedBasis basis) {
309fe2413ffSjeremylt   int ierr;
310fe2413ffSjeremylt   Ceed ceed;
311fe2413ffSjeremylt   ierr = CeedBasisGetCeed(basis, &ceed); CeedChk(ierr);
31284a01de5SJeremy L Thompson   CeedBasis_Ref *impl;
31384a01de5SJeremy L Thompson   ierr = CeedCalloc(1, &impl); CeedChk(ierr);
314*dfe03796SJeremy L Thompson   // Check for collocated interp
315*dfe03796SJeremy L Thompson   if (Q1d == P1d) {
316*dfe03796SJeremy L Thompson     bool collocated = 1;
317*dfe03796SJeremy L Thompson     for (CeedInt i=0; i<P1d; i++) {
318*dfe03796SJeremy L Thompson       collocated = collocated && (fabs(interp1d[i+P1d*i] - 1.0) < 1e-14);
319*dfe03796SJeremy L Thompson       for (CeedInt j=0; j<P1d; j++)
320*dfe03796SJeremy L Thompson         if (j != i)
321*dfe03796SJeremy L Thompson           collocated = collocated && (fabs(interp1d[j+P1d*i]) < 1e-14);
322*dfe03796SJeremy L Thompson     }
323*dfe03796SJeremy L Thompson     impl->collointerp = collocated;
324*dfe03796SJeremy L Thompson   }
325*dfe03796SJeremy L Thompson   // Calculate collocated grad
326*dfe03796SJeremy L Thompson   if (Q1d >= P1d && !impl->collointerp) {
327*dfe03796SJeremy L Thompson     ierr = CeedMalloc(Q1d*Q1d, &impl->collograd1d); CeedChk(ierr);
328*dfe03796SJeremy L Thompson     ierr = CeedBasisGetCollocatedGrad(basis, impl->collograd1d); CeedChk(ierr);
329a7bd39daSjeremylt   }
33084a01de5SJeremy L Thompson   ierr = CeedBasisSetData(basis, (void *)&impl); CeedChk(ierr);
331fe2413ffSjeremylt 
3322f86a920SJeremy L Thompson   Ceed parent;
333de686571SJeremy L Thompson   ierr = CeedGetParent(ceed, &parent); CeedChk(ierr);
3342f86a920SJeremy L Thompson   CeedTensorContract contract;
335c71e1dcdSjeremylt   ierr = CeedTensorContractCreate(parent, basis, &contract); CeedChk(ierr);
3362f86a920SJeremy L Thompson   ierr = CeedBasisSetTensorContract(basis, &contract); CeedChk(ierr);
3372f86a920SJeremy L Thompson 
338fe2413ffSjeremylt   ierr = CeedSetBackendFunction(ceed, "Basis", basis, "Apply",
339fe2413ffSjeremylt                                 CeedBasisApply_Ref); CeedChk(ierr);
340fe2413ffSjeremylt   ierr = CeedSetBackendFunction(ceed, "Basis", basis, "Destroy",
34184a01de5SJeremy L Thompson                                 CeedBasisDestroyTensor_Ref); CeedChk(ierr);
34221617c04Sjeremylt   return 0;
34321617c04Sjeremylt }
344a8de75f0Sjeremylt 
34584a01de5SJeremy L Thompson 
34684a01de5SJeremy L Thompson 
347667bc5fcSjeremylt int CeedBasisCreateH1_Ref(CeedElemTopology topo, CeedInt dim,
3488795c945Sjeremylt                           CeedInt nnodes, CeedInt nqpts,
349a8de75f0Sjeremylt                           const CeedScalar *interp,
350a8de75f0Sjeremylt                           const CeedScalar *grad,
351a8de75f0Sjeremylt                           const CeedScalar *qref,
352a8de75f0Sjeremylt                           const CeedScalar *qweight,
353a8de75f0Sjeremylt                           CeedBasis basis) {
354fe2413ffSjeremylt   int ierr;
355fe2413ffSjeremylt   Ceed ceed;
356fe2413ffSjeremylt   ierr = CeedBasisGetCeed(basis, &ceed); CeedChk(ierr);
357fe2413ffSjeremylt 
358c71e1dcdSjeremylt   Ceed parent;
359c71e1dcdSjeremylt   ierr = CeedGetParent(ceed, &parent); CeedChk(ierr);
3602f86a920SJeremy L Thompson   CeedTensorContract contract;
361c71e1dcdSjeremylt   ierr = CeedTensorContractCreate(parent, basis, &contract); CeedChk(ierr);
3622f86a920SJeremy L Thompson   ierr = CeedBasisSetTensorContract(basis, &contract); CeedChk(ierr);
3632f86a920SJeremy L Thompson 
364fe2413ffSjeremylt   ierr = CeedSetBackendFunction(ceed, "Basis", basis, "Apply",
365fe2413ffSjeremylt                                 CeedBasisApply_Ref); CeedChk(ierr);
366fe2413ffSjeremylt   ierr = CeedSetBackendFunction(ceed, "Basis", basis, "Destroy",
36784a01de5SJeremy L Thompson                                 CeedBasisDestroyNonTensor_Ref); CeedChk(ierr);
36884a01de5SJeremy L Thompson 
369a8de75f0Sjeremylt   return 0;
370a8de75f0Sjeremylt }
371