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