xref: /libCEED/tests/t507-operator.c (revision 1da993686108ca99c277bcc17f7a3ba064c97b46)
1*1da99368SJeremy L Thompson /// @file
2*1da99368SJeremy L Thompson /// Test VLA macro for operator
3*1da99368SJeremy L Thompson /// \test VLA marco for operator
4*1da99368SJeremy L Thompson #include <ceed.h>
5*1da99368SJeremy L Thompson #include <stdlib.h>
6*1da99368SJeremy L Thompson #include <math.h>
7*1da99368SJeremy L Thompson 
8*1da99368SJeremy L Thompson #include "t507-operator.h"
9*1da99368SJeremy L Thompson 
10*1da99368SJeremy L Thompson int main(int argc, char **argv) {
11*1da99368SJeremy L Thompson   Ceed ceed;
12*1da99368SJeremy L Thompson   CeedInterlaceMode imode = CEED_NONINTERLACED,
13*1da99368SJeremy L Thompson                     imodeu = CEED_INTERLACED;
14*1da99368SJeremy L Thompson   CeedElemRestriction Erestrictx, Erestrictu, Erestrictui;
15*1da99368SJeremy L Thompson   CeedBasis bx, bu;
16*1da99368SJeremy L Thompson   CeedQFunction qf_setup, qf_mass;
17*1da99368SJeremy L Thompson   CeedOperator op_setup, op_mass;
18*1da99368SJeremy L Thompson   CeedVector qdata, X, U, V;
19*1da99368SJeremy L Thompson   CeedScalar *hu;
20*1da99368SJeremy L Thompson   const CeedScalar *hv;
21*1da99368SJeremy L Thompson   CeedInt nelem = 15, P = 5, Q = 8;
22*1da99368SJeremy L Thompson   CeedInt Nx = nelem+1, Nu = nelem*(P-1)+1;
23*1da99368SJeremy L Thompson   CeedInt indx[nelem*2], indu[nelem*P];
24*1da99368SJeremy L Thompson   CeedScalar x[Nx];
25*1da99368SJeremy L Thompson   CeedScalar sum1, sum2;
26*1da99368SJeremy L Thompson 
27*1da99368SJeremy L Thompson   CeedInit(argv[1], &ceed);
28*1da99368SJeremy L Thompson 
29*1da99368SJeremy L Thompson   for (CeedInt i=0; i<Nx; i++)
30*1da99368SJeremy L Thompson     x[i] = (CeedScalar) i / (Nx - 1);
31*1da99368SJeremy L Thompson   for (CeedInt i=0; i<nelem; i++) {
32*1da99368SJeremy L Thompson     indx[2*i+0] = i;
33*1da99368SJeremy L Thompson     indx[2*i+1] = i+1;
34*1da99368SJeremy L Thompson   }
35*1da99368SJeremy L Thompson   // Restrictions
36*1da99368SJeremy L Thompson   CeedElemRestrictionCreate(ceed, imode, nelem, 2, Nx, 1, CEED_MEM_HOST,
37*1da99368SJeremy L Thompson                             CEED_USE_POINTER, indx, &Erestrictx);
38*1da99368SJeremy L Thompson 
39*1da99368SJeremy L Thompson   for (CeedInt i=0; i<nelem; i++) {
40*1da99368SJeremy L Thompson     for (CeedInt j=0; j<P; j++) {
41*1da99368SJeremy L Thompson       indu[P*i+j] = i*(P-1) + j;
42*1da99368SJeremy L Thompson     }
43*1da99368SJeremy L Thompson   }
44*1da99368SJeremy L Thompson   CeedElemRestrictionCreate(ceed, imodeu, nelem, P, Nu, 2, CEED_MEM_HOST,
45*1da99368SJeremy L Thompson                             CEED_USE_POINTER, indu, &Erestrictu);
46*1da99368SJeremy L Thompson   CeedInt stridesu[3] = {1, Q, Q};
47*1da99368SJeremy L Thompson   CeedElemRestrictionCreateStrided(ceed, nelem, Q, Q*nelem, 1, stridesu,
48*1da99368SJeremy L Thompson                                    &Erestrictui);
49*1da99368SJeremy L Thompson 
50*1da99368SJeremy L Thompson   // Bases
51*1da99368SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, 2, Q, CEED_GAUSS, &bx);
52*1da99368SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, 1, 2, P, Q, CEED_GAUSS, &bu);
53*1da99368SJeremy L Thompson 
54*1da99368SJeremy L Thompson   // QFunctions
55*1da99368SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup);
56*1da99368SJeremy L Thompson   CeedQFunctionAddInput(qf_setup, "_weight", 1, CEED_EVAL_WEIGHT);
57*1da99368SJeremy L Thompson   CeedQFunctionAddInput(qf_setup, "dx", 1*1, CEED_EVAL_GRAD);
58*1da99368SJeremy L Thompson   CeedQFunctionAddOutput(qf_setup, "rho", 1, CEED_EVAL_NONE);
59*1da99368SJeremy L Thompson 
60*1da99368SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass);
61*1da99368SJeremy L Thompson   CeedQFunctionAddInput(qf_mass, "rho", 1, CEED_EVAL_NONE);
62*1da99368SJeremy L Thompson   CeedQFunctionAddInput(qf_mass, "u", 2, CEED_EVAL_INTERP);
63*1da99368SJeremy L Thompson   CeedQFunctionAddOutput(qf_mass, "v", 2, CEED_EVAL_INTERP);
64*1da99368SJeremy L Thompson 
65*1da99368SJeremy L Thompson   // Operators
66*1da99368SJeremy L Thompson   CeedOperatorCreate(ceed, qf_setup, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE,
67*1da99368SJeremy L Thompson                      &op_setup);
68*1da99368SJeremy L Thompson 
69*1da99368SJeremy L Thompson   CeedOperatorCreate(ceed, qf_mass, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE,
70*1da99368SJeremy L Thompson                      &op_mass);
71*1da99368SJeremy L Thompson 
72*1da99368SJeremy L Thompson   CeedVectorCreate(ceed, Nx, &X);
73*1da99368SJeremy L Thompson   CeedVectorSetArray(X, CEED_MEM_HOST, CEED_USE_POINTER, x);
74*1da99368SJeremy L Thompson   CeedVectorCreate(ceed, nelem*Q, &qdata);
75*1da99368SJeremy L Thompson 
76*1da99368SJeremy L Thompson   CeedOperatorSetField(op_setup, "_weight", CEED_ELEMRESTRICTION_NONE, bx,
77*1da99368SJeremy L Thompson                        CEED_VECTOR_NONE);
78*1da99368SJeremy L Thompson   CeedOperatorSetField(op_setup, "dx", Erestrictx, bx, CEED_VECTOR_ACTIVE);
79*1da99368SJeremy L Thompson   CeedOperatorSetField(op_setup, "rho", Erestrictui, CEED_BASIS_COLLOCATED,
80*1da99368SJeremy L Thompson                        CEED_VECTOR_ACTIVE);
81*1da99368SJeremy L Thompson 
82*1da99368SJeremy L Thompson   CeedOperatorSetField(op_mass, "rho", Erestrictui, CEED_BASIS_COLLOCATED,
83*1da99368SJeremy L Thompson                        qdata);
84*1da99368SJeremy L Thompson   CeedOperatorSetField(op_mass, "u", Erestrictu, bu, CEED_VECTOR_ACTIVE);
85*1da99368SJeremy L Thompson   CeedOperatorSetField(op_mass, "v", Erestrictu, bu, CEED_VECTOR_ACTIVE);
86*1da99368SJeremy L Thompson 
87*1da99368SJeremy L Thompson   CeedOperatorApply(op_setup, X, qdata, CEED_REQUEST_IMMEDIATE);
88*1da99368SJeremy L Thompson 
89*1da99368SJeremy L Thompson   CeedVectorCreate(ceed, 2*Nu, &U);
90*1da99368SJeremy L Thompson   CeedVectorGetArray(U, CEED_MEM_HOST, &hu);
91*1da99368SJeremy L Thompson   for (int i = 0; i < Nu; i++) {
92*1da99368SJeremy L Thompson     hu[2*i] = 1.0;
93*1da99368SJeremy L Thompson     hu[2*i+1] = 2.0;
94*1da99368SJeremy L Thompson   }
95*1da99368SJeremy L Thompson   CeedVectorRestoreArray(U, &hu);
96*1da99368SJeremy L Thompson   CeedVectorCreate(ceed, 2*Nu, &V);
97*1da99368SJeremy L Thompson   CeedOperatorApply(op_mass, U, V, CEED_REQUEST_IMMEDIATE);
98*1da99368SJeremy L Thompson 
99*1da99368SJeremy L Thompson   // Check output
100*1da99368SJeremy L Thompson   CeedVectorGetArrayRead(V, CEED_MEM_HOST, &hv);
101*1da99368SJeremy L Thompson   sum1 = 0.; sum2 = 0.;
102*1da99368SJeremy L Thompson   for (CeedInt i=0; i<Nu; i++) {
103*1da99368SJeremy L Thompson     sum1 += hv[2*i];
104*1da99368SJeremy L Thompson     sum2 += hv[2*i+1];
105*1da99368SJeremy L Thompson   }
106*1da99368SJeremy L Thompson   if (fabs(sum1-1.)>1e-10) printf("Computed Area: %f != True Area: 1.0\n", sum1);
107*1da99368SJeremy L Thompson   if (fabs(sum2-2.)>1e-10) printf("Computed Area: %f != True Area: 2.0\n", sum2);
108*1da99368SJeremy L Thompson   CeedVectorRestoreArrayRead(V, &hv);
109*1da99368SJeremy L Thompson 
110*1da99368SJeremy L Thompson   CeedQFunctionDestroy(&qf_setup);
111*1da99368SJeremy L Thompson   CeedQFunctionDestroy(&qf_mass);
112*1da99368SJeremy L Thompson   CeedOperatorDestroy(&op_setup);
113*1da99368SJeremy L Thompson   CeedOperatorDestroy(&op_mass);
114*1da99368SJeremy L Thompson   CeedElemRestrictionDestroy(&Erestrictu);
115*1da99368SJeremy L Thompson   CeedElemRestrictionDestroy(&Erestrictx);
116*1da99368SJeremy L Thompson   CeedElemRestrictionDestroy(&Erestrictui);
117*1da99368SJeremy L Thompson   CeedBasisDestroy(&bu);
118*1da99368SJeremy L Thompson   CeedBasisDestroy(&bx);
119*1da99368SJeremy L Thompson   CeedVectorDestroy(&X);
120*1da99368SJeremy L Thompson   CeedVectorDestroy(&U);
121*1da99368SJeremy L Thompson   CeedVectorDestroy(&V);
122*1da99368SJeremy L Thompson   CeedVectorDestroy(&qdata);
123*1da99368SJeremy L Thompson   CeedDestroy(&ceed);
124*1da99368SJeremy L Thompson   return 0;
125*1da99368SJeremy L Thompson }
126