xref: /libCEED/tests/t510-operator.c (revision e07206de23a3ad6f238e5da2d646d1a5a514f718)
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 #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, NULL, NULL, &op_setup);
85 
86   CeedOperatorCreate(ceed, qf_mass, NULL, NULL, &op_mass);
87 
88   CeedVectorCreate(ceed, dim*Ndofs, &X);
89   CeedVectorSetArray(X, CEED_MEM_HOST, CEED_USE_POINTER, x);
90   CeedVectorCreate(ceed, Nqpts, &qdata);
91 
92   CeedOperatorSetField(op_setup, "_weight", Erestrictxi, CEED_NOTRANSPOSE,
93                        bx, CEED_VECTOR_NONE);
94   CeedOperatorSetField(op_setup, "dx", Erestrictx, CEED_NOTRANSPOSE,
95                        bx, CEED_VECTOR_ACTIVE);
96   CeedOperatorSetField(op_setup, "rho", Erestrictui, CEED_NOTRANSPOSE,
97                        CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
98 
99   CeedOperatorSetField(op_mass, "rho", Erestrictui, CEED_NOTRANSPOSE,
100                        CEED_BASIS_COLLOCATED, qdata);
101   CeedOperatorSetField(op_mass, "u", Erestrictu, CEED_NOTRANSPOSE,
102                        bu, CEED_VECTOR_ACTIVE);
103   CeedOperatorSetField(op_mass, "v", Erestrictu, CEED_NOTRANSPOSE,
104                        bu, CEED_VECTOR_ACTIVE);
105 
106   CeedOperatorApply(op_setup, X, qdata, CEED_REQUEST_IMMEDIATE);
107 
108   CeedVectorCreate(ceed, Ndofs, &U);
109   CeedVectorSetValue(U, 0.0);
110   CeedVectorCreate(ceed, Ndofs, &V);
111 
112   CeedOperatorApply(op_mass, U, V, CEED_REQUEST_IMMEDIATE);
113 
114   // Check output
115   CeedVectorGetArrayRead(V, CEED_MEM_HOST, &hv);
116   for (CeedInt i=0; i<Ndofs; i++)
117     if (fabs(hv[i]) > 1e-14) printf("[%d] v %g != 0.0\n",i, hv[i]);
118   CeedVectorRestoreArrayRead(V, &hv);
119 
120   CeedQFunctionDestroy(&qf_setup);
121   CeedQFunctionDestroy(&qf_mass);
122   CeedOperatorDestroy(&op_setup);
123   CeedOperatorDestroy(&op_mass);
124   CeedElemRestrictionDestroy(&Erestrictu);
125   CeedElemRestrictionDestroy(&Erestrictx);
126   CeedElemRestrictionDestroy(&Erestrictui);
127   CeedElemRestrictionDestroy(&Erestrictxi);
128   CeedBasisDestroy(&bu);
129   CeedBasisDestroy(&bx);
130   CeedVectorDestroy(&X);
131   CeedVectorDestroy(&U);
132   CeedVectorDestroy(&V);
133   CeedVectorDestroy(&qdata);
134   CeedDestroy(&ceed);
135   return 0;
136 }
137