xref: /libCEED/tests/t511-operator.c (revision 56d8cfc241e0df885b47de7ad2d12edb4b8ae247)
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, 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   CeedScalar sum;
28 
29   CeedInit(argv[1], &ceed);
30 
31   for (CeedInt i=0; i<ndofs; i++) {
32     x[i] = (1. / (nx*2)) * (CeedScalar) (i % (nx*2+1));
33     x[i+ndofs] = (1. / (ny*2)) * (CeedScalar) (i / (nx*2+1));
34   }
35   for (CeedInt i=0; i<nelem/2; i++) {
36     col = i % nx;
37     row = i / nx;
38     offset = col*2 + row*(nx*2+1)*2;
39 
40     indx[i*2*P +  0] =  2 + offset;
41     indx[i*2*P +  1] =  9 + offset;
42     indx[i*2*P +  2] = 16 + offset;
43     indx[i*2*P +  3] =  1 + offset;
44     indx[i*2*P +  4] =  8 + offset;
45     indx[i*2*P +  5] =  0 + offset;
46 
47     indx[i*2*P +  6] = 14 + offset;
48     indx[i*2*P +  7] =  7 + offset;
49     indx[i*2*P +  8] =  0 + offset;
50     indx[i*2*P +  9] = 15 + offset;
51     indx[i*2*P + 10] =  8 + offset;
52     indx[i*2*P + 11] = 16 + offset;
53   }
54 
55   // Restrictions
56   CeedElemRestrictionCreate(ceed, imode, nelem, P, ndofs, dim, CEED_MEM_HOST,
57                             CEED_USE_POINTER, indx, &Erestrictx);
58 
59   CeedElemRestrictionCreate(ceed, imode, nelem, P, ndofs, 1, CEED_MEM_HOST,
60                             CEED_USE_POINTER, indx, &Erestrictu);
61   CeedInt stridesu[3] = {1, Q, Q};
62   CeedElemRestrictionCreateStrided(ceed, nelem, Q, nqpts, 1, stridesu,
63                                    &Erestrictui);
64 
65 
66   // Bases
67   buildmats(qref, qweight, interp, grad);
68   CeedBasisCreateH1(ceed, CEED_TRIANGLE, dim, P, Q, interp, grad, qref,
69                     qweight, &bx);
70 
71   buildmats(qref, qweight, interp, grad);
72   CeedBasisCreateH1(ceed, CEED_TRIANGLE, 1, P, Q, interp, grad, qref,
73                     qweight, &bu);
74 
75   // QFunctions
76   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup);
77   CeedQFunctionAddInput(qf_setup, "_weight", 1, CEED_EVAL_WEIGHT);
78   CeedQFunctionAddInput(qf_setup, "x", dim*dim, CEED_EVAL_GRAD);
79   CeedQFunctionAddOutput(qf_setup, "rho", 1, CEED_EVAL_NONE);
80 
81   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass);
82   CeedQFunctionAddInput(qf_mass, "rho", 1, CEED_EVAL_NONE);
83   CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP);
84   CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP);
85 
86   // Operators
87   CeedOperatorCreate(ceed, qf_setup, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE,
88                      &op_setup);
89 
90   CeedOperatorCreate(ceed, qf_mass, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE,
91                      &op_mass);
92 
93   CeedVectorCreate(ceed, dim*ndofs, &X);
94   CeedVectorSetArray(X, CEED_MEM_HOST, CEED_USE_POINTER, x);
95   CeedVectorCreate(ceed, nqpts, &qdata);
96 
97   CeedOperatorSetField(op_setup, "_weight", CEED_ELEMRESTRICTION_NONE, bx,
98                        CEED_VECTOR_NONE);
99   CeedOperatorSetField(op_setup, "x", Erestrictx, bx, CEED_VECTOR_ACTIVE);
100   CeedOperatorSetField(op_setup, "rho", Erestrictui, CEED_BASIS_COLLOCATED,
101                        CEED_VECTOR_ACTIVE);
102 
103   CeedOperatorSetField(op_mass, "rho", Erestrictui, CEED_BASIS_COLLOCATED,
104                        qdata);
105   CeedOperatorSetField(op_mass, "u", Erestrictu, bu, CEED_VECTOR_ACTIVE);
106   CeedOperatorSetField(op_mass, "v", Erestrictu, bu, CEED_VECTOR_ACTIVE);
107 
108   CeedOperatorApply(op_setup, X, qdata, CEED_REQUEST_IMMEDIATE);
109 
110   CeedVectorCreate(ceed, ndofs, &U);
111   CeedVectorSetValue(U, 1.0);
112   CeedVectorCreate(ceed, ndofs, &V);
113 
114   CeedOperatorApply(op_mass, U, V, CEED_REQUEST_IMMEDIATE);
115 
116   // Check output
117   CeedVectorGetArrayRead(V, CEED_MEM_HOST, &hv);
118   sum = 0.;
119   for (CeedInt i=0; i<ndofs; i++)
120     sum += hv[i];
121   if (fabs(sum-1.)>1e-10) printf("Computed Area: %f != True Area: 1.0\n", sum);
122   CeedVectorRestoreArrayRead(V, &hv);
123 
124   CeedQFunctionDestroy(&qf_setup);
125   CeedQFunctionDestroy(&qf_mass);
126   CeedOperatorDestroy(&op_setup);
127   CeedOperatorDestroy(&op_mass);
128   CeedElemRestrictionDestroy(&Erestrictu);
129   CeedElemRestrictionDestroy(&Erestrictx);
130   CeedElemRestrictionDestroy(&Erestrictui);
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