xref: /libCEED/tests/t502-operator.c (revision 2f4d9adb86426a8397f9d81f8b0a0e6244dbfdbd)
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     v[Q+i] = rho[i] * u[Q+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   CeedScalar *hu;
42   const CeedScalar *hv;
43   CeedInt nelem = 15, P = 5, Q = 8;
44   CeedInt Nx = nelem+1, Nu = nelem*(P-1)+1;
45   CeedInt indx[nelem*2], indu[nelem*P];
46   CeedScalar x[Nx];
47   CeedScalar sum1, sum2;
48 
49   CeedInit(argv[1], &ceed);
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   // Restrictions
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 
60   for (CeedInt i=0; i<nelem; i++) {
61     for (CeedInt j=0; j<P; j++) {
62       indu[P*i+j] = i*(P-1) + j;
63     }
64   }
65   CeedElemRestrictionCreate(ceed, nelem, P, Nu, 2, CEED_MEM_HOST,
66                             CEED_USE_POINTER, indu, &Erestrictu);
67   CeedElemRestrictionCreateIdentity(ceed, nelem, Q, Q*nelem, 1, &Erestrictui);
68 
69   // Bases
70   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, 2, Q, CEED_GAUSS, &bx);
71   CeedBasisCreateTensorH1Lagrange(ceed, 1, 2, P, Q, CEED_GAUSS, &bu);
72 
73   // QFunctions
74   CeedQFunctionCreateInterior(ceed, 1, setup, __FILE__ ":setup", &qf_setup);
75   CeedQFunctionAddInput(qf_setup, "_weight", 1, CEED_EVAL_WEIGHT);
76   CeedQFunctionAddInput(qf_setup, "x", 1, CEED_EVAL_GRAD);
77   CeedQFunctionAddOutput(qf_setup, "rho", 1, CEED_EVAL_NONE);
78 
79   CeedQFunctionCreateInterior(ceed, 1, mass, __FILE__ ":mass", &qf_mass);
80   CeedQFunctionAddInput(qf_mass, "rho", 1, CEED_EVAL_NONE);
81   CeedQFunctionAddInput(qf_mass, "u", 2, CEED_EVAL_INTERP);
82   CeedQFunctionAddOutput(qf_mass, "v", 2, CEED_EVAL_INTERP);
83 
84   // Operators
85   CeedOperatorCreate(ceed, qf_setup, NULL, NULL, &op_setup);
86 
87   CeedOperatorCreate(ceed, qf_mass, NULL, NULL, &op_mass);
88 
89   CeedVectorCreate(ceed, Nx, &X);
90   CeedVectorSetArray(X, CEED_MEM_HOST, CEED_USE_POINTER, x);
91   CeedVectorCreate(ceed, nelem*Q, &qdata);
92 
93   CeedOperatorSetField(op_setup, "_weight", Erestrictxi, CEED_NOTRANSPOSE,
94                        bx, CEED_VECTOR_NONE);
95   CeedOperatorSetField(op_setup, "x", Erestrictx, CEED_NOTRANSPOSE,
96                        bx, CEED_VECTOR_ACTIVE);
97   CeedOperatorSetField(op_setup, "rho", Erestrictui, CEED_NOTRANSPOSE,
98                        CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
99 
100   CeedOperatorSetField(op_mass, "rho", Erestrictui, CEED_NOTRANSPOSE,
101                        CEED_BASIS_COLLOCATED, qdata);
102   CeedOperatorSetField(op_mass, "u", Erestrictu, CEED_TRANSPOSE,
103                        bu, CEED_VECTOR_ACTIVE);
104   CeedOperatorSetField(op_mass, "v", Erestrictu, CEED_TRANSPOSE,
105                        bu, CEED_VECTOR_ACTIVE);
106 
107   CeedOperatorApply(op_setup, X, qdata, CEED_REQUEST_IMMEDIATE);
108 
109   CeedVectorCreate(ceed, 2*Nu, &U);
110   CeedVectorGetArray(U, CEED_MEM_HOST, &hu);
111   for (int i = 0; i < Nu; i++) {
112     hu[2*i] = 1.0;
113     hu[2*i+1] = 2.0;
114   }
115   CeedVectorRestoreArray(U, &hu);
116   CeedVectorCreate(ceed, 2*Nu, &V);
117   CeedOperatorApply(op_mass, U, V, CEED_REQUEST_IMMEDIATE);
118 
119   // Check output
120   CeedVectorGetArrayRead(V, CEED_MEM_HOST, &hv);
121   sum1 = 0.; sum2 = 0.;
122   for (CeedInt i=0; i<Nu; i++) {
123     sum1 += hv[2*i];
124     sum2 += hv[2*i+1];
125   }
126   if (fabs(sum1-1.)>1e-10) printf("Computed Area: %f != True Area: 1.0\n", sum1);
127   if (fabs(sum2-2.)>1e-10) printf("Computed Area: %f != True Area: 2.0\n", sum2);
128   CeedVectorRestoreArrayRead(V, &hv);
129 
130   CeedQFunctionDestroy(&qf_setup);
131   CeedQFunctionDestroy(&qf_mass);
132   CeedOperatorDestroy(&op_setup);
133   CeedOperatorDestroy(&op_mass);
134   CeedElemRestrictionDestroy(&Erestrictu);
135   CeedElemRestrictionDestroy(&Erestrictx);
136   CeedElemRestrictionDestroy(&Erestrictui);
137   CeedElemRestrictionDestroy(&Erestrictxi);
138   CeedBasisDestroy(&bu);
139   CeedBasisDestroy(&bx);
140   CeedVectorDestroy(&X);
141   CeedVectorDestroy(&U);
142   CeedVectorDestroy(&V);
143   CeedVectorDestroy(&qdata);
144   CeedDestroy(&ceed);
145   return 0;
146 }
147