xref: /libCEED/tests/t511-operator.c (revision 69a53589b8c3c82ae8bb5be56f1100c7d3468573)
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   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   CeedInt stridesx[3] = {1, P, P*dim};
59   CeedElemRestrictionCreateStrided(ceed, nelem, P, nelem*P, dim, stridesx,
60                                    &Erestrictxi);
61 
62   CeedElemRestrictionCreate(ceed, imode, nelem, P, ndofs, 1, CEED_MEM_HOST,
63                             CEED_USE_POINTER, indx, &Erestrictu);
64   CeedInt stridesu[3] = {1, Q, Q};
65   CeedElemRestrictionCreateStrided(ceed, nelem, Q, nqpts, 1, stridesu,
66                                    &Erestrictui);
67 
68 
69   // Bases
70   buildmats(qref, qweight, interp, grad);
71   CeedBasisCreateH1(ceed, CEED_TRIANGLE, dim, P, Q, interp, grad, qref,
72                     qweight, &bx);
73 
74   buildmats(qref, qweight, interp, grad);
75   CeedBasisCreateH1(ceed, CEED_TRIANGLE, 1, P, Q, interp, grad, qref,
76                     qweight, &bu);
77 
78   // QFunctions
79   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup);
80   CeedQFunctionAddInput(qf_setup, "_weight", 1, CEED_EVAL_WEIGHT);
81   CeedQFunctionAddInput(qf_setup, "x", dim*dim, CEED_EVAL_GRAD);
82   CeedQFunctionAddOutput(qf_setup, "rho", 1, CEED_EVAL_NONE);
83 
84   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass);
85   CeedQFunctionAddInput(qf_mass, "rho", 1, CEED_EVAL_NONE);
86   CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP);
87   CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP);
88 
89   // Operators
90   CeedOperatorCreate(ceed, qf_setup, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE,
91                      &op_setup);
92 
93   CeedOperatorCreate(ceed, qf_mass, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE,
94                      &op_mass);
95 
96   CeedVectorCreate(ceed, dim*ndofs, &X);
97   CeedVectorSetArray(X, CEED_MEM_HOST, CEED_USE_POINTER, x);
98   CeedVectorCreate(ceed, nqpts, &qdata);
99 
100   CeedOperatorSetField(op_setup, "_weight", Erestrictxi, bx, CEED_VECTOR_NONE);
101   CeedOperatorSetField(op_setup, "x", Erestrictx, bx, CEED_VECTOR_ACTIVE);
102   CeedOperatorSetField(op_setup, "rho", Erestrictui, CEED_BASIS_COLLOCATED,
103                        CEED_VECTOR_ACTIVE);
104 
105   CeedOperatorSetField(op_mass, "rho", Erestrictui, CEED_BASIS_COLLOCATED,
106                        qdata);
107   CeedOperatorSetField(op_mass, "u", Erestrictu, bu, CEED_VECTOR_ACTIVE);
108   CeedOperatorSetField(op_mass, "v", Erestrictu, bu, CEED_VECTOR_ACTIVE);
109 
110   CeedOperatorApply(op_setup, X, qdata, CEED_REQUEST_IMMEDIATE);
111 
112   CeedVectorCreate(ceed, ndofs, &U);
113   CeedVectorSetValue(U, 1.0);
114   CeedVectorCreate(ceed, ndofs, &V);
115 
116   CeedOperatorApply(op_mass, U, V, CEED_REQUEST_IMMEDIATE);
117 
118   // Check output
119   CeedVectorGetArrayRead(V, CEED_MEM_HOST, &hv);
120   sum = 0.;
121   for (CeedInt i=0; i<ndofs; i++)
122     sum += hv[i];
123   if (fabs(sum-1.)>1e-10) printf("Computed Area: %f != True Area: 1.0\n", sum);
124   CeedVectorRestoreArrayRead(V, &hv);
125 
126   CeedQFunctionDestroy(&qf_setup);
127   CeedQFunctionDestroy(&qf_mass);
128   CeedOperatorDestroy(&op_setup);
129   CeedOperatorDestroy(&op_mass);
130   CeedElemRestrictionDestroy(&Erestrictu);
131   CeedElemRestrictionDestroy(&Erestrictx);
132   CeedElemRestrictionDestroy(&Erestrictui);
133   CeedElemRestrictionDestroy(&Erestrictxi);
134   CeedBasisDestroy(&bu);
135   CeedBasisDestroy(&bx);
136   CeedVectorDestroy(&X);
137   CeedVectorDestroy(&U);
138   CeedVectorDestroy(&V);
139   CeedVectorDestroy(&qdata);
140   CeedDestroy(&ceed);
141   return 0;
142 }
143