xref: /libCEED/tests/t511-operator.c (revision ac421f396b3221f4b3f065b5ec198b4b0e1ed7a7)
1 /// @file
2 /// Test creation creation, action, and destruction for mass matrix operator
3 /// \test Test creation 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 
9 #include "t511-operator.h"
10 
11 int main(int argc, char **argv) {
12   Ceed ceed;
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, nelem, P, Ndofs, dim, CEED_MEM_HOST,
57                             CEED_USE_POINTER, indx, &Erestrictx);
58   CeedElemRestrictionCreateIdentity(ceed, nelem, P, nelem*P, dim, &Erestrictxi);
59 
60   CeedElemRestrictionCreate(ceed, nelem, P, Ndofs, 1, CEED_MEM_HOST,
61                             CEED_USE_POINTER, indx, &Erestrictu);
62   CeedElemRestrictionCreateIdentity(ceed, nelem, Q, Nqpts, 1, &Erestrictui);
63 
64 
65   // Bases
66   buildmats(qref, qweight, interp, grad);
67   CeedBasisCreateH1(ceed, CEED_TRIANGLE, dim, P, Q, interp, grad, qref,
68                     qweight, &bx);
69 
70   buildmats(qref, qweight, interp, grad);
71   CeedBasisCreateH1(ceed, CEED_TRIANGLE, 1, P, Q, interp, grad, qref,
72                     qweight, &bu);
73 
74   // QFunctions
75   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup);
76   CeedQFunctionAddInput(qf_setup, "_weight", 1, CEED_EVAL_WEIGHT);
77   CeedQFunctionAddInput(qf_setup, "x", dim*dim, CEED_EVAL_GRAD);
78   CeedQFunctionAddOutput(qf_setup, "rho", 1, CEED_EVAL_NONE);
79 
80   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass);
81   CeedQFunctionAddInput(qf_mass, "rho", 1, CEED_EVAL_NONE);
82   CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP);
83   CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP);
84 
85   // Operators
86   CeedOperatorCreate(ceed, qf_setup, NULL, NULL, &op_setup);
87 
88   CeedOperatorCreate(ceed, qf_mass, NULL, NULL, &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", Erestrictxi, CEED_NOTRANSPOSE,
95                        bx, CEED_VECTOR_NONE);
96   CeedOperatorSetField(op_setup, "x", Erestrictx, CEED_NOTRANSPOSE,
97                        bx, CEED_VECTOR_ACTIVE);
98   CeedOperatorSetField(op_setup, "rho", Erestrictui, CEED_NOTRANSPOSE,
99                        CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
100 
101   CeedOperatorSetField(op_mass, "rho", Erestrictui, CEED_NOTRANSPOSE,
102                        CEED_BASIS_COLLOCATED, qdata);
103   CeedOperatorSetField(op_mass, "u", Erestrictu, CEED_NOTRANSPOSE,
104                        bu, CEED_VECTOR_ACTIVE);
105   CeedOperatorSetField(op_mass, "v", Erestrictu, CEED_NOTRANSPOSE,
106                        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   CeedElemRestrictionDestroy(&Erestrictxi);
132   CeedBasisDestroy(&bu);
133   CeedBasisDestroy(&bx);
134   CeedVectorDestroy(&X);
135   CeedVectorDestroy(&U);
136   CeedVectorDestroy(&V);
137   CeedVectorDestroy(&qdata);
138   CeedDestroy(&ceed);
139   return 0;
140 }
141