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