xref: /libCEED/tests/t510-operator.c (revision 0036de2c0ad807bd8ab213cac28d33186520da16)
1 /// @file
2 /// Test creation, action, and destruction for mass matrix operator
3 /// \test Test creation, action, and destruction for mass matrix operator
4 #include <ceed.h>
5 #include <stdlib.h>
6 #include <math.h>
7 #include "t320-basis.h"
8 #include "t510-operator.h"
9 
10 int main(int argc, char **argv) {
11   Ceed ceed;
12   CeedInterlaceMode imode = CEED_NONINTERLACED;
13   CeedElemRestriction Erestrictx, Erestrictu, Erestrictxi, Erestrictui;
14   CeedBasis bx, bu;
15   CeedQFunction qf_setup, qf_mass;
16   CeedOperator op_setup, op_mass;
17   CeedVector qdata, X, U, V;
18   const CeedScalar *hv;
19   CeedInt nelem = 12, dim = 2, P = 6, Q = 4;
20   CeedInt nx = 3, ny = 2;
21   CeedInt row, col, offset;
22   CeedInt Ndofs = (nx*2+1)*(ny*2+1), Nqpts = nelem*Q;
23   CeedInt indx[nelem*P];
24   CeedScalar x[dim*Ndofs];
25   CeedScalar qref[dim*Q], qweight[Q];
26   CeedScalar interp[P*Q], grad[dim*P*Q];
27 
28   CeedInit(argv[1], &ceed);
29 
30   for (CeedInt i=0; i<Ndofs; i++) {
31     x[i] = (1. / (nx*2)) * (CeedScalar) (i % (nx*2+1));
32     x[i+Ndofs] = (1. / (ny*2)) * (CeedScalar) (i / (nx*2+1));
33   }
34   for (CeedInt i=0; i<nelem/2; i++) {
35     col = i % nx;
36     row = i / nx;
37     offset = col*2 + row*(nx*2+1)*2;
38 
39     indx[i*2*P +  0] =  2 + offset;
40     indx[i*2*P +  1] =  9 + offset;
41     indx[i*2*P +  2] = 16 + offset;
42     indx[i*2*P +  3] =  1 + offset;
43     indx[i*2*P +  4] =  8 + offset;
44     indx[i*2*P +  5] =  0 + offset;
45 
46     indx[i*2*P +  6] = 14 + offset;
47     indx[i*2*P +  7] =  7 + offset;
48     indx[i*2*P +  8] =  0 + offset;
49     indx[i*2*P +  9] = 15 + offset;
50     indx[i*2*P + 10] =  8 + offset;
51     indx[i*2*P + 11] = 16 + offset;
52   }
53 
54   // Restrictions
55   CeedElemRestrictionCreate(ceed, imode, nelem, P, Ndofs, dim, CEED_MEM_HOST,
56                             CEED_USE_POINTER, indx, &Erestrictx);
57   CeedInt stridesx[3] = {1, P, P*dim};
58   CeedElemRestrictionCreateStrided(ceed, nelem, P, nelem*P, dim, stridesx,
59                                    &Erestrictxi);
60 
61   CeedElemRestrictionCreate(ceed, imode, nelem, P, Ndofs, 1, CEED_MEM_HOST,
62                             CEED_USE_POINTER, indx, &Erestrictu);
63   CeedInt stridesu[3] = {1, Q, Q};
64   CeedElemRestrictionCreateStrided(ceed, nelem, Q, Nqpts, 1, stridesu,
65                                    &Erestrictui);
66 
67 
68   // Bases
69   buildmats(qref, qweight, interp, grad);
70   CeedBasisCreateH1(ceed, CEED_TRIANGLE, dim, P, Q, interp, grad, qref,
71                     qweight, &bx);
72 
73   buildmats(qref, qweight, interp, grad);
74   CeedBasisCreateH1(ceed, CEED_TRIANGLE, 1, P, Q, interp, grad, qref,
75                     qweight, &bu);
76 
77   // QFunctions
78   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup);
79   CeedQFunctionAddInput(qf_setup, "_weight", 1, CEED_EVAL_WEIGHT);
80   CeedQFunctionAddInput(qf_setup, "dx", dim*dim, CEED_EVAL_GRAD);
81   CeedQFunctionAddOutput(qf_setup, "rho", 1, CEED_EVAL_NONE);
82 
83   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass);
84   CeedQFunctionAddInput(qf_mass, "rho", 1, CEED_EVAL_NONE);
85   CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP);
86   CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP);
87 
88   // Operators
89   CeedOperatorCreate(ceed, qf_setup, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE,
90                      &op_setup);
91 
92   CeedOperatorCreate(ceed, qf_mass, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE,
93                      &op_mass);
94 
95   CeedVectorCreate(ceed, dim*Ndofs, &X);
96   CeedVectorSetArray(X, CEED_MEM_HOST, CEED_USE_POINTER, x);
97   CeedVectorCreate(ceed, Nqpts, &qdata);
98 
99   CeedOperatorSetField(op_setup, "_weight", Erestrictxi, bx, CEED_VECTOR_NONE);
100   CeedOperatorSetField(op_setup, "dx", Erestrictx, bx, CEED_VECTOR_ACTIVE);
101   CeedOperatorSetField(op_setup, "rho", Erestrictui, CEED_BASIS_COLLOCATED,
102                        CEED_VECTOR_ACTIVE);
103 
104   CeedOperatorSetField(op_mass, "rho", Erestrictui, CEED_BASIS_COLLOCATED,
105                        qdata);
106   CeedOperatorSetField(op_mass, "u", Erestrictu, bu, CEED_VECTOR_ACTIVE);
107   CeedOperatorSetField(op_mass, "v", Erestrictu, bu, CEED_VECTOR_ACTIVE);
108 
109   CeedOperatorApply(op_setup, X, qdata, CEED_REQUEST_IMMEDIATE);
110 
111   CeedVectorCreate(ceed, Ndofs, &U);
112   CeedVectorSetValue(U, 0.0);
113   CeedVectorCreate(ceed, Ndofs, &V);
114 
115   CeedOperatorApply(op_mass, U, V, CEED_REQUEST_IMMEDIATE);
116 
117   // Check output
118   CeedVectorGetArrayRead(V, CEED_MEM_HOST, &hv);
119   for (CeedInt i=0; i<Ndofs; i++)
120     if (fabs(hv[i]) > 1e-14) printf("[%d] v %g != 0.0\n",i, hv[i]);
121   CeedVectorRestoreArrayRead(V, &hv);
122 
123   CeedQFunctionDestroy(&qf_setup);
124   CeedQFunctionDestroy(&qf_mass);
125   CeedOperatorDestroy(&op_setup);
126   CeedOperatorDestroy(&op_mass);
127   CeedElemRestrictionDestroy(&Erestrictu);
128   CeedElemRestrictionDestroy(&Erestrictx);
129   CeedElemRestrictionDestroy(&Erestrictui);
130   CeedElemRestrictionDestroy(&Erestrictxi);
131   CeedBasisDestroy(&bu);
132   CeedBasisDestroy(&bx);
133   CeedVectorDestroy(&X);
134   CeedVectorDestroy(&U);
135   CeedVectorDestroy(&V);
136   CeedVectorDestroy(&qdata);
137   CeedDestroy(&ceed);
138   return 0;
139 }
140