xref: /libCEED/tests/t511-operator.c (revision 2f4d9adb86426a8397f9d81f8b0a0e6244dbfdbd)
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 "t310-basis.h"
8 
9 static int setup(void *ctx, CeedInt Q, const CeedScalar *const *in,
10                  CeedScalar *const *out);
11 static int mass(void *ctx, CeedInt Q, const CeedScalar *const *in,
12                 CeedScalar *const *out);
13 
14 static int setup(void *ctx, CeedInt Q, const CeedScalar *const *in,
15                  CeedScalar *const *out) {
16   const CeedScalar *weight = in[0], *J = in[1];
17   CeedScalar *rho = out[0];
18   for (CeedInt i=0; i<Q; i++) {
19     rho[i] = weight[i] * (J[i+Q*0]*J[i+Q*3] - J[i+Q*1]*J[i+Q*2]);
20   }
21   return 0;
22 }
23 
24 static int mass(void *ctx, CeedInt Q, const CeedScalar *const *in,
25                 CeedScalar *const *out) {
26   const CeedScalar *rho = in[0], *u = in[1];
27   CeedScalar *v = out[0];
28   for (CeedInt i=0; i<Q; i++) {
29     v[i] = rho[i] * u[i];
30   }
31   return 0;
32 }
33 
34 int main(int argc, char **argv) {
35   Ceed ceed;
36   CeedElemRestriction Erestrictx, Erestrictu, Erestrictxi, Erestrictui;
37   CeedBasis bx, bu;
38   CeedQFunction qf_setup, qf_mass;
39   CeedOperator op_setup, op_mass;
40   CeedVector qdata, X, U, V;
41   const CeedScalar *hv;
42   CeedInt nelem = 12, dim = 2, P = 6, Q = 4;
43   CeedInt nx = 3, ny = 2;
44   CeedInt row, col, offset;
45   CeedInt Ndofs = (nx*2+1)*(ny*2+1), Nqpts = nelem*Q;
46   CeedInt indx[nelem*P];
47   CeedScalar x[dim*Ndofs];
48   CeedScalar qref[dim*Q], qweight[Q];
49   CeedScalar interp[P*Q], grad[dim*P*Q];
50   CeedScalar sum;
51 
52   CeedInit(argv[1], &ceed);
53 
54   for (CeedInt i=0; i<Ndofs; i++) {
55     x[i] = (1. / (nx*2)) * (CeedScalar) (i % (nx*2+1));
56     x[i+Ndofs] = (1. / (ny*2)) * (CeedScalar) (i / (nx*2+1));
57   }
58   for (CeedInt i=0; i<nelem/2; i++) {
59     col = i % nx;
60     row = i / nx;
61     offset = col*2 + row*(nx*2+1)*2;
62 
63     indx[i*2*P +  0] =  2 + offset;
64     indx[i*2*P +  1] =  9 + offset;
65     indx[i*2*P +  2] = 16 + offset;
66     indx[i*2*P +  3] =  1 + offset;
67     indx[i*2*P +  4] =  8 + offset;
68     indx[i*2*P +  5] =  0 + offset;
69 
70     indx[i*2*P +  6] = 14 + offset;
71     indx[i*2*P +  7] =  7 + offset;
72     indx[i*2*P +  8] =  0 + offset;
73     indx[i*2*P +  9] = 15 + offset;
74     indx[i*2*P + 10] =  8 + offset;
75     indx[i*2*P + 11] = 16 + offset;
76   }
77 
78   // Restrictions
79   CeedElemRestrictionCreate(ceed, nelem, P, Ndofs, dim, CEED_MEM_HOST,
80                             CEED_USE_POINTER, indx, &Erestrictx);
81   CeedElemRestrictionCreateIdentity(ceed, nelem, P, nelem*P, dim, &Erestrictxi);
82 
83   CeedElemRestrictionCreate(ceed, nelem, P, Ndofs, 1, CEED_MEM_HOST,
84                             CEED_USE_POINTER, indx, &Erestrictu);
85   CeedElemRestrictionCreateIdentity(ceed, nelem, Q, Nqpts, 1, &Erestrictui);
86 
87 
88   // Bases
89   buildmats(qref, qweight, interp, grad);
90   CeedBasisCreateH1(ceed, CEED_TRIANGLE, dim, P, Q, interp, grad, qref,
91                     qweight, &bx);
92 
93   buildmats(qref, qweight, interp, grad);
94   CeedBasisCreateH1(ceed, CEED_TRIANGLE, 1, P, Q, interp, grad, qref,
95                     qweight, &bu);
96 
97   // QFunctions
98   CeedQFunctionCreateInterior(ceed, 1, setup, __FILE__ ":setup", &qf_setup);
99   CeedQFunctionAddInput(qf_setup, "_weight", 1, CEED_EVAL_WEIGHT);
100   CeedQFunctionAddInput(qf_setup, "x", dim, CEED_EVAL_GRAD);
101   CeedQFunctionAddOutput(qf_setup, "rho", 1, CEED_EVAL_NONE);
102 
103   CeedQFunctionCreateInterior(ceed, 1, mass, __FILE__ ":mass", &qf_mass);
104   CeedQFunctionAddInput(qf_mass, "rho", 1, CEED_EVAL_NONE);
105   CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP);
106   CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP);
107 
108   // Operators
109   CeedOperatorCreate(ceed, qf_setup, NULL, NULL, &op_setup);
110 
111   CeedOperatorCreate(ceed, qf_mass, NULL, NULL, &op_mass);
112 
113   CeedVectorCreate(ceed, dim*Ndofs, &X);
114   CeedVectorSetArray(X, CEED_MEM_HOST, CEED_USE_POINTER, x);
115   CeedVectorCreate(ceed, Nqpts, &qdata);
116 
117   CeedOperatorSetField(op_setup, "_weight", Erestrictxi, CEED_NOTRANSPOSE,
118                        bx, CEED_VECTOR_NONE);
119   CeedOperatorSetField(op_setup, "x", Erestrictx, CEED_NOTRANSPOSE,
120                        bx, CEED_VECTOR_ACTIVE);
121   CeedOperatorSetField(op_setup, "rho", Erestrictui, CEED_NOTRANSPOSE,
122                        CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
123 
124   CeedOperatorSetField(op_mass, "rho", Erestrictui, CEED_NOTRANSPOSE,
125                        CEED_BASIS_COLLOCATED, qdata);
126   CeedOperatorSetField(op_mass, "u", Erestrictu, CEED_NOTRANSPOSE,
127                        bu, CEED_VECTOR_ACTIVE);
128   CeedOperatorSetField(op_mass, "v", Erestrictu, CEED_NOTRANSPOSE,
129                        bu, CEED_VECTOR_ACTIVE);
130 
131   CeedOperatorApply(op_setup, X, qdata, CEED_REQUEST_IMMEDIATE);
132 
133   CeedVectorCreate(ceed, Ndofs, &U);
134   CeedVectorSetValue(U, 1.0);
135   CeedVectorCreate(ceed, Ndofs, &V);
136 
137   CeedOperatorApply(op_mass, U, V, CEED_REQUEST_IMMEDIATE);
138 
139   // Check output
140   CeedVectorGetArrayRead(V, CEED_MEM_HOST, &hv);
141   sum = 0.;
142   for (CeedInt i=0; i<Ndofs; i++)
143     sum += hv[i];
144   if (fabs(sum-1.)>1e-10) printf("Computed Area: %f != True Area: 1.0\n", sum);
145   CeedVectorRestoreArrayRead(V, &hv);
146 
147   CeedQFunctionDestroy(&qf_setup);
148   CeedQFunctionDestroy(&qf_mass);
149   CeedOperatorDestroy(&op_setup);
150   CeedOperatorDestroy(&op_mass);
151   CeedElemRestrictionDestroy(&Erestrictu);
152   CeedElemRestrictionDestroy(&Erestrictx);
153   CeedElemRestrictionDestroy(&Erestrictui);
154   CeedElemRestrictionDestroy(&Erestrictxi);
155   CeedBasisDestroy(&bu);
156   CeedBasisDestroy(&bx);
157   CeedVectorDestroy(&X);
158   CeedVectorDestroy(&U);
159   CeedVectorDestroy(&V);
160   CeedVectorDestroy(&qdata);
161   CeedDestroy(&ceed);
162   return 0;
163 }
164