xref: /libCEED/tests/t500-operator.c (revision 783c99b3f03c04aed3140873345e25fc3bab2ee4)
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 
7 //! [QFunction User Code]
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 //! [QFunction User Code]
13 
14 static int setup(void *ctx, CeedInt Q, const CeedScalar *const *in,
15                  CeedScalar *const *out) {
16   const CeedScalar *weight = in[0], *dxdX = in[1];
17   CeedScalar *rho = out[0];
18   for (CeedInt i=0; i<Q; i++) {
19     rho[i] = weight[i] * dxdX[i];
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;  CeedOperator op_setup, op_mass;
39   CeedVector qdata, X, U, V;
40   CeedScalar *hu;
41   const CeedScalar *hv;
42   CeedInt nelem = 15, P = 5, Q = 8;
43   CeedInt Nx = nelem+1, Nu = nelem*(P-1)+1;
44   CeedInt indx[nelem*2], indu[nelem*P];
45   CeedScalar x[Nx];
46 
47 //! [Ceed Init]
48   CeedInit(argv[1], &ceed);
49 //! [Ceed Init]
50   for (CeedInt i=0; i<Nx; i++) x[i] = (CeedScalar) i / (Nx - 1);
51   for (CeedInt i=0; i<nelem; i++) {
52     indx[2*i+0] = i;
53     indx[2*i+1] = i+1;
54   }
55 //! [ElemRestr Create]
56   CeedElemRestrictionCreate(ceed, nelem, 2, Nx, 1, CEED_MEM_HOST,
57                             CEED_USE_POINTER,
58                             indx, &Erestrictx);
59   CeedElemRestrictionCreateIdentity(ceed, nelem, 2, nelem*2, 1, &Erestrictxi);
60 //! [ElemRestr Create]
61 
62   for (CeedInt i=0; i<nelem; i++) {
63     for (CeedInt j=0; j<P; j++) {
64       indu[P*i+j] = i*(P-1) + j;
65     }
66   }
67 //! [ElemRestrU Create]
68   CeedElemRestrictionCreate(ceed, nelem, P, Nu, 1, CEED_MEM_HOST,
69                             CEED_USE_POINTER,
70                             indu, &Erestrictu);
71   CeedElemRestrictionCreateIdentity(ceed, nelem, Q, Q*nelem, 1, &Erestrictui);
72 //! [ElemRestrU Create]
73 
74 //! [Basis Create]
75   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, 2, Q, CEED_GAUSS, &bx);
76   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, P, Q, CEED_GAUSS, &bu);
77 //! [Basis Create]
78 
79 //! [QFunction Create]
80   CeedQFunctionCreateInterior(ceed, 1, setup, __FILE__ ":setup", &qf_setup);
81   CeedQFunctionAddInput(qf_setup, "_weight", 1, CEED_EVAL_WEIGHT);
82   CeedQFunctionAddInput(qf_setup, "x", 1, CEED_EVAL_GRAD);
83   CeedQFunctionAddOutput(qf_setup, "rho", 1, CEED_EVAL_NONE);
84 
85   CeedQFunctionCreateInterior(ceed, 1, mass, __FILE__ ":mass", &qf_mass);
86   CeedQFunctionAddInput(qf_mass, "rho", 1, CEED_EVAL_NONE);
87   CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP);
88   CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP);
89 //! [QFunction Create]
90 
91 //! [Setup Create]
92   CeedOperatorCreate(ceed, qf_setup, NULL, NULL, &op_setup);
93 //! [Setup Create]
94 
95 //! [Operator Create]
96   CeedOperatorCreate(ceed, qf_mass, NULL, NULL, &op_mass);
97 //! [Operator Create]
98 
99   CeedVectorCreate(ceed, Nx, &X);
100   CeedVectorSetArray(X, CEED_MEM_HOST, CEED_USE_POINTER, x);
101   CeedVectorCreate(ceed, nelem*Q, &qdata);
102 
103 //! [Setup Set]
104   CeedOperatorSetField(op_setup, "_weight", Erestrictxi, bx,
105                        CEED_VECTOR_NONE);
106   CeedOperatorSetField(op_setup, "x", Erestrictx, bx, CEED_VECTOR_ACTIVE);
107   CeedOperatorSetField(op_setup, "rho", Erestrictui,
108                        CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
109 //! [Setup Set]
110 
111 //! [Operator Set]
112   CeedOperatorSetField(op_mass, "rho", Erestrictui,
113                        CEED_BASIS_COLLOCATED, qdata);
114   CeedOperatorSetField(op_mass, "u", Erestrictu, bu, CEED_VECTOR_ACTIVE);
115   CeedOperatorSetField(op_mass, "v", Erestrictu, bu, CEED_VECTOR_ACTIVE);
116 //! [Operator Set]
117 
118 //! [Setup Apply]
119   CeedOperatorApply(op_setup, X, qdata, CEED_REQUEST_IMMEDIATE);
120 //! [Setup Apply]
121 
122   CeedVectorCreate(ceed, Nu, &U);
123   CeedVectorGetArray(U, CEED_MEM_HOST, &hu);
124   for (CeedInt i=0; i<Nu; i++)
125     hu[i] = 0.0;
126   CeedVectorRestoreArray(U, &hu);
127   CeedVectorCreate(ceed, Nu, &V);
128 //! [Operator Apply]
129   CeedOperatorApply(op_mass, U, V, CEED_REQUEST_IMMEDIATE);
130 //! [Operator Apply]
131 
132   CeedVectorGetArrayRead(V, CEED_MEM_HOST, &hv);
133   for (CeedInt i=0; i<Nu; i++)
134     if (hv[i] != 0.0) printf("[%d] v %g != 0.0\n",i, hv[i]);
135   CeedVectorRestoreArrayRead(V, &hv);
136 
137   CeedQFunctionDestroy(&qf_setup);
138   CeedQFunctionDestroy(&qf_mass);
139   CeedOperatorDestroy(&op_setup);
140   CeedOperatorDestroy(&op_mass);
141   CeedElemRestrictionDestroy(&Erestrictu);
142   CeedElemRestrictionDestroy(&Erestrictx);
143   CeedElemRestrictionDestroy(&Erestrictui);
144   CeedElemRestrictionDestroy(&Erestrictxi);
145   CeedBasisDestroy(&bu);
146   CeedBasisDestroy(&bx);
147   CeedVectorDestroy(&X);
148   CeedVectorDestroy(&U);
149   CeedVectorDestroy(&V);
150   CeedVectorDestroy(&qdata);
151   CeedDestroy(&ceed);
152   return 0;
153 }
154