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