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