xref: /libCEED/tests/t500-operator.c (revision a8de75f0ba69dfecd93c7d0093c041c0285ffd21)
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;
39   CeedOperator op_setup, op_mass;
40   CeedVector qdata, X, U, V;
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, indx, &Erestrictx);
58   CeedElemRestrictionCreateIdentity(ceed, nelem, 2, nelem*2, 1, &Erestrictxi);
59 //! [ElemRestr Create]
60 
61   for (CeedInt i=0; i<nelem; i++) {
62     for (CeedInt j=0; j<P; j++) {
63       indu[P*i+j] = i*(P-1) + j;
64     }
65   }
66 //! [ElemRestrU Create]
67   CeedElemRestrictionCreate(ceed, nelem, P, Nu, 1, CEED_MEM_HOST,
68                             CEED_USE_POINTER, indu, &Erestrictu);
69   CeedElemRestrictionCreateIdentity(ceed, nelem, Q, Q*nelem, 1, &Erestrictui);
70 //! [ElemRestrU Create]
71 
72 //! [Basis Create]
73   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, 2, Q, CEED_GAUSS, &bx);
74   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, P, Q, CEED_GAUSS, &bu);
75 //! [Basis Create]
76 
77 //! [QFunction Create]
78   CeedQFunctionCreateInterior(ceed, 1, setup, __FILE__ ":setup", &qf_setup);
79   CeedQFunctionAddInput(qf_setup, "_weight", 1, CEED_EVAL_WEIGHT);
80   CeedQFunctionAddInput(qf_setup, "x", 1, CEED_EVAL_GRAD);
81   CeedQFunctionAddOutput(qf_setup, "rho", 1, CEED_EVAL_NONE);
82 
83   CeedQFunctionCreateInterior(ceed, 1, mass, __FILE__ ":mass", &qf_mass);
84   CeedQFunctionAddInput(qf_mass, "rho", 1, CEED_EVAL_NONE);
85   CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP);
86   CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP);
87 //! [QFunction Create]
88 
89 //! [Setup Create]
90   CeedOperatorCreate(ceed, qf_setup, NULL, NULL, &op_setup);
91 //! [Setup Create]
92 
93 //! [Operator Create]
94   CeedOperatorCreate(ceed, qf_mass, NULL, NULL, &op_mass);
95 //! [Operator Create]
96 
97   CeedVectorCreate(ceed, Nx, &X);
98   CeedVectorSetArray(X, CEED_MEM_HOST, CEED_USE_POINTER, x);
99   CeedVectorCreate(ceed, nelem*Q, &qdata);
100 
101 //! [Setup Set]
102   CeedOperatorSetField(op_setup, "_weight", Erestrictxi, bx,
103                        CEED_VECTOR_NONE);
104   CeedOperatorSetField(op_setup, "x", Erestrictx, bx, CEED_VECTOR_ACTIVE);
105   CeedOperatorSetField(op_setup, "rho", Erestrictui,
106                        CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
107 //! [Setup Set]
108 
109 //! [Operator Set]
110   CeedOperatorSetField(op_mass, "rho", Erestrictui,
111                        CEED_BASIS_COLLOCATED, qdata);
112   CeedOperatorSetField(op_mass, "u", Erestrictu, bu, CEED_VECTOR_ACTIVE);
113   CeedOperatorSetField(op_mass, "v", Erestrictu, bu, CEED_VECTOR_ACTIVE);
114 //! [Operator Set]
115 
116 //! [Setup Apply]
117   CeedOperatorApply(op_setup, X, qdata, CEED_REQUEST_IMMEDIATE);
118 //! [Setup Apply]
119 
120   CeedVectorCreate(ceed, Nu, &U);
121   CeedVectorSetValue(U, 0.0);
122   CeedVectorCreate(ceed, Nu, &V);
123 //! [Operator Apply]
124   CeedOperatorApply(op_mass, U, V, CEED_REQUEST_IMMEDIATE);
125 //! [Operator Apply]
126 
127   CeedVectorGetArrayRead(V, CEED_MEM_HOST, &hv);
128   for (CeedInt i=0; i<Nu; i++)
129     if (hv[i] != 0.0) printf("[%d] v %g != 0.0\n",i, hv[i]);
130   CeedVectorRestoreArrayRead(V, &hv);
131 
132   CeedQFunctionDestroy(&qf_setup);
133   CeedQFunctionDestroy(&qf_mass);
134   CeedOperatorDestroy(&op_setup);
135   CeedOperatorDestroy(&op_mass);
136   CeedElemRestrictionDestroy(&Erestrictu);
137   CeedElemRestrictionDestroy(&Erestrictx);
138   CeedElemRestrictionDestroy(&Erestrictui);
139   CeedElemRestrictionDestroy(&Erestrictxi);
140   CeedBasisDestroy(&bu);
141   CeedBasisDestroy(&bx);
142   CeedVectorDestroy(&X);
143   CeedVectorDestroy(&U);
144   CeedVectorDestroy(&V);
145   CeedVectorDestroy(&qdata);
146   CeedDestroy(&ceed);
147   return 0;
148 }
149