xref: /libCEED/tests/t503-operator.c (revision e07206de23a3ad6f238e5da2d646d1a5a514f718)
1 /// @file
2 /// Test creation, action, and destruction for mass matrix operator with passive inputs and outputs
3 /// \test Test creation, action, and destruction for mass matrix operator with passive inputs and outputs
4 #include <ceed.h>
5 #include <stdlib.h>
6 #include <math.h>
7 
8 #include "t500-operator.h"
9 
10 int main(int argc, char **argv) {
11   Ceed ceed;
12   CeedElemRestriction Erestrictx, Erestrictu, Erestrictxi, Erestrictui;
13   CeedBasis bx, bu;
14   CeedQFunction qf_setup, qf_mass;
15   CeedOperator op_setup, op_mass;
16   CeedVector qdata, X, U, V;
17   const CeedScalar *hv;
18   CeedInt nelem = 15, P = 5, Q = 8;
19   CeedInt Nx = nelem+1, Nu = nelem*(P-1)+1;
20   CeedInt indx[nelem*2], indu[nelem*P];
21   CeedScalar x[Nx];
22   CeedScalar sum;
23 
24   CeedInit(argv[1], &ceed);
25 
26   // Vectors
27   CeedVectorCreate(ceed, Nu, &U);
28   CeedVectorSetValue(U, 1.0);
29 
30   CeedVectorCreate(ceed, Nu, &V);
31 
32   for (CeedInt i=0; i<Nx; i++)
33     x[i] = (CeedScalar) i / (Nx - 1);
34   CeedVectorCreate(ceed, Nx, &X);
35   CeedVectorSetArray(X, CEED_MEM_HOST, CEED_USE_POINTER, x);
36 
37   CeedVectorCreate(ceed, nelem*Q, &qdata);
38 
39   // Restrictions
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, indx, &Erestrictx);
46   CeedElemRestrictionCreateIdentity(ceed, nelem, 2, nelem*2, 1, &Erestrictxi);
47 
48   for (CeedInt i=0; i<nelem; i++) {
49     for (CeedInt j=0; j<P; j++) {
50       indu[P*i+j] = i*(P-1) + j;
51     }
52   }
53   CeedElemRestrictionCreate(ceed, nelem, P, Nu, 1, CEED_MEM_HOST,
54                             CEED_USE_POINTER, indu, &Erestrictu);
55   CeedElemRestrictionCreateIdentity(ceed, nelem, Q, Q*nelem, 1, &Erestrictui);
56 
57   // Bases
58   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, 2, Q, CEED_GAUSS, &bx);
59   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, P, Q, CEED_GAUSS, &bu);
60 
61   // QFunctions
62   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup);
63   CeedQFunctionAddInput(qf_setup, "_weight", 1, CEED_EVAL_WEIGHT);
64   CeedQFunctionAddInput(qf_setup, "dx", 1, CEED_EVAL_GRAD);
65   CeedQFunctionAddOutput(qf_setup, "rho", 1, CEED_EVAL_NONE);
66 
67   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &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   // Operators
73   CeedOperatorCreate(ceed, qf_setup, NULL, NULL, &op_setup);
74 
75   CeedOperatorCreate(ceed, qf_mass, NULL, NULL, &op_mass);
76 
77   CeedOperatorSetField(op_setup, "_weight", Erestrictxi, CEED_NOTRANSPOSE,
78                        bx, CEED_VECTOR_NONE);
79   CeedOperatorSetField(op_setup, "dx", Erestrictx, CEED_NOTRANSPOSE, bx, X);
80   CeedOperatorSetField(op_setup, "rho", Erestrictui, CEED_NOTRANSPOSE,
81                        CEED_BASIS_COLLOCATED, qdata);
82 
83   CeedOperatorSetField(op_mass, "rho", Erestrictui, CEED_NOTRANSPOSE,
84                        CEED_BASIS_COLLOCATED, qdata);
85   CeedOperatorSetField(op_mass, "u", Erestrictu, CEED_NOTRANSPOSE, bu, U);
86   CeedOperatorSetField(op_mass, "v", Erestrictu, CEED_NOTRANSPOSE, bu, V);
87 
88   // Note - It is atypical to use only passive fields; this test is intended
89   //   as a test for all passive input modes rather than as an example.
90   CeedOperatorApply(op_setup, NULL, NULL, CEED_REQUEST_IMMEDIATE);
91   CeedOperatorApply(op_mass, NULL, NULL, CEED_REQUEST_IMMEDIATE);
92 
93   // Check output
94   CeedVectorGetArrayRead(V, CEED_MEM_HOST, &hv);
95   sum = 0.;
96   for (CeedInt i=0; i<Nu; i++)
97     sum += hv[i];
98   if (fabs(sum-1.)>1e-10) printf("Computed Area: %f != True Area: 1.0\n", sum);
99   CeedVectorRestoreArrayRead(V, &hv);
100 
101   CeedQFunctionDestroy(&qf_setup);
102   CeedQFunctionDestroy(&qf_mass);
103   CeedOperatorDestroy(&op_setup);
104   CeedOperatorDestroy(&op_mass);
105   CeedElemRestrictionDestroy(&Erestrictu);
106   CeedElemRestrictionDestroy(&Erestrictx);
107   CeedElemRestrictionDestroy(&Erestrictui);
108   CeedElemRestrictionDestroy(&Erestrictxi);
109   CeedBasisDestroy(&bu);
110   CeedBasisDestroy(&bx);
111   CeedVectorDestroy(&X);
112   CeedVectorDestroy(&U);
113   CeedVectorDestroy(&V);
114   CeedVectorDestroy(&qdata);
115   CeedDestroy(&ceed);
116   return 0;
117 }
118