xref: /libCEED/tests/t511-operator.c (revision a8d322087fa8f150327cdc2bf14a171452b711ec)
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   CeedTransposeMode lmode = CEED_NOTRANSPOSE;
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, lmode, nelem, P, ndofs, dim, CEED_MEM_HOST,
57                             CEED_USE_POINTER, indx, &Erestrictx);
58   CeedElemRestrictionCreateIdentity(ceed, lmode, nelem, P, nelem*P, dim,
59                                     &Erestrictxi);
60 
61   CeedElemRestrictionCreate(ceed, lmode, nelem, P, ndofs, 1, CEED_MEM_HOST,
62                             CEED_USE_POINTER, indx, &Erestrictu);
63   CeedElemRestrictionCreateIdentity(ceed, lmode, nelem, Q, nqpts, 1,
64                                     &Erestrictui);
65 
66 
67   // Bases
68   buildmats(qref, qweight, interp, grad);
69   CeedBasisCreateH1(ceed, CEED_TRIANGLE, dim, P, Q, interp, grad, qref,
70                     qweight, &bx);
71 
72   buildmats(qref, qweight, interp, grad);
73   CeedBasisCreateH1(ceed, CEED_TRIANGLE, 1, P, Q, interp, grad, qref,
74                     qweight, &bu);
75 
76   // QFunctions
77   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup);
78   CeedQFunctionAddInput(qf_setup, "_weight", 1, CEED_EVAL_WEIGHT);
79   CeedQFunctionAddInput(qf_setup, "x", dim*dim, CEED_EVAL_GRAD);
80   CeedQFunctionAddOutput(qf_setup, "rho", 1, CEED_EVAL_NONE);
81 
82   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass);
83   CeedQFunctionAddInput(qf_mass, "rho", 1, CEED_EVAL_NONE);
84   CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP);
85   CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP);
86 
87   // Operators
88   CeedOperatorCreate(ceed, qf_setup, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE,
89                      &op_setup);
90 
91   CeedOperatorCreate(ceed, qf_mass, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE,
92                      &op_mass);
93 
94   CeedVectorCreate(ceed, dim*ndofs, &X);
95   CeedVectorSetArray(X, CEED_MEM_HOST, CEED_USE_POINTER, x);
96   CeedVectorCreate(ceed, nqpts, &qdata);
97 
98   CeedOperatorSetField(op_setup, "_weight", Erestrictxi, bx, 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   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