xref: /libCEED/tests/t510-operator.c (revision ed768a3cf06a2a740bbfe20134286531e89dc9b1)
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, Erestrictxi, 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, Ndofs, dim, CEED_MEM_HOST,
55                             CEED_USE_POINTER, indx, &Erestrictx);
56   CeedElemRestrictionCreateIdentity(ceed, nelem, P, nelem*P, dim, &Erestrictxi);
57 
58   CeedElemRestrictionCreate(ceed, nelem, P, Ndofs, 1, CEED_MEM_HOST,
59                             CEED_USE_POINTER, indx, &Erestrictu);
60   CeedElemRestrictionCreateIdentity(ceed, nelem, Q, Nqpts, 1, &Erestrictui);
61 
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", Erestrictxi, CEED_NOTRANSPOSE,
95                        bx, CEED_VECTOR_NONE);
96   CeedOperatorSetField(op_setup, "dx", 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, 0.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   for (CeedInt i=0; i<Ndofs; i++)
119     if (fabs(hv[i]) > 1e-14) printf("[%d] v %g != 0.0\n",i, hv[i]);
120   CeedVectorRestoreArrayRead(V, &hv);
121 
122   CeedQFunctionDestroy(&qf_setup);
123   CeedQFunctionDestroy(&qf_mass);
124   CeedOperatorDestroy(&op_setup);
125   CeedOperatorDestroy(&op_mass);
126   CeedElemRestrictionDestroy(&Erestrictu);
127   CeedElemRestrictionDestroy(&Erestrictx);
128   CeedElemRestrictionDestroy(&Erestrictui);
129   CeedElemRestrictionDestroy(&Erestrictxi);
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