xref: /libCEED/tests/t511-operator.c (revision 5c7b696c8582f667cb0fcf7d02b9ef3156803fee)
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   CeedScalar sum;
27 
28   CeedInit(argv[1], &ceed);
29 
30   for (CeedInt i=0; i<ndofs; i++) {
31     x[i] = (1. / (nx*2)) * (CeedScalar) (i % (nx*2+1));
32     x[i+ndofs] = (1. / (ny*2)) * (CeedScalar) (i / (nx*2+1));
33   }
34   for (CeedInt i=0; i<nelem/2; i++) {
35     col = i % nx;
36     row = i / nx;
37     offset = col*2 + row*(nx*2+1)*2;
38 
39     indx[i*2*P +  0] =  2 + offset;
40     indx[i*2*P +  1] =  9 + offset;
41     indx[i*2*P +  2] = 16 + offset;
42     indx[i*2*P +  3] =  1 + offset;
43     indx[i*2*P +  4] =  8 + offset;
44     indx[i*2*P +  5] =  0 + offset;
45 
46     indx[i*2*P +  6] = 14 + offset;
47     indx[i*2*P +  7] =  7 + offset;
48     indx[i*2*P +  8] =  0 + offset;
49     indx[i*2*P +  9] = 15 + offset;
50     indx[i*2*P + 10] =  8 + offset;
51     indx[i*2*P + 11] = 16 + offset;
52   }
53 
54   // Restrictions
55   CeedElemRestrictionCreate(ceed, nelem, P, dim, ndofs, dim*ndofs,
56                             CEED_MEM_HOST, CEED_USE_POINTER, indx, &Erestrictx);
57 
58   CeedElemRestrictionCreate(ceed, nelem, P, 1, 1, ndofs, CEED_MEM_HOST,
59                             CEED_USE_POINTER, indx, &Erestrictu);
60   CeedInt stridesu[3] = {1, Q, Q};
61   CeedElemRestrictionCreateStrided(ceed, nelem, Q, 1, nqpts, stridesu,
62                                    &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, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE,
87                      &op_setup);
88 
89   CeedOperatorCreate(ceed, qf_mass, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE,
90                      &op_mass);
91 
92   CeedVectorCreate(ceed, dim*ndofs, &X);
93   CeedVectorSetArray(X, CEED_MEM_HOST, CEED_USE_POINTER, x);
94   CeedVectorCreate(ceed, nqpts, &qdata);
95 
96   CeedOperatorSetField(op_setup, "_weight", CEED_ELEMRESTRICTION_NONE, bx,
97                        CEED_VECTOR_NONE);
98   CeedOperatorSetField(op_setup, "x", Erestrictx, bx, CEED_VECTOR_ACTIVE);
99   CeedOperatorSetField(op_setup, "rho", Erestrictui, CEED_BASIS_COLLOCATED,
100                        CEED_VECTOR_ACTIVE);
101 
102   CeedOperatorSetField(op_mass, "rho", Erestrictui, CEED_BASIS_COLLOCATED,
103                        qdata);
104   CeedOperatorSetField(op_mass, "u", Erestrictu, bu, CEED_VECTOR_ACTIVE);
105   CeedOperatorSetField(op_mass, "v", Erestrictu, bu, CEED_VECTOR_ACTIVE);
106 
107   CeedOperatorApply(op_setup, X, qdata, CEED_REQUEST_IMMEDIATE);
108 
109   CeedVectorCreate(ceed, ndofs, &U);
110   CeedVectorSetValue(U, 1.0);
111   CeedVectorCreate(ceed, ndofs, &V);
112 
113   CeedOperatorApply(op_mass, U, V, CEED_REQUEST_IMMEDIATE);
114 
115   // Check output
116   CeedVectorGetArrayRead(V, CEED_MEM_HOST, &hv);
117   sum = 0.;
118   for (CeedInt i=0; i<ndofs; i++)
119     sum += hv[i];
120   if (fabs(sum-1.)>1e-10) printf("Computed Area: %f != True Area: 1.0\n", sum);
121   CeedVectorRestoreArrayRead(V, &hv);
122 
123   CeedQFunctionDestroy(&qf_setup);
124   CeedQFunctionDestroy(&qf_mass);
125   CeedOperatorDestroy(&op_setup);
126   CeedOperatorDestroy(&op_mass);
127   CeedElemRestrictionDestroy(&Erestrictu);
128   CeedElemRestrictionDestroy(&Erestrictx);
129   CeedElemRestrictionDestroy(&Erestrictui);
130   CeedBasisDestroy(&bu);
131   CeedBasisDestroy(&bx);
132   CeedVectorDestroy(&X);
133   CeedVectorDestroy(&U);
134   CeedVectorDestroy(&V);
135   CeedVectorDestroy(&qdata);
136   CeedDestroy(&ceed);
137   return 0;
138 }
139