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