xref: /libCEED/tests/t511-operator.c (revision 52d6035f927efec34920a771ebaa7a03e4ffa966)
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 #include "t310-basis.h"
8 
9 static int setup(void *ctx, CeedInt Q, const CeedScalar *const *in,
10                  CeedScalar *const *out) {
11   const CeedScalar *weight = in[0], *J = in[1];
12   CeedScalar *rho = out[0];
13   for (CeedInt i=0; i<Q; i++) {
14     rho[i] = weight[i] * (J[i+Q*0]*J[i+Q*3] - J[i+Q*1]*J[i+Q*2]);
15   }
16   return 0;
17 }
18 
19 static int mass(void *ctx, CeedInt Q, const CeedScalar *const *in,
20                 CeedScalar *const *out) {
21   const CeedScalar *rho = in[0], *u = in[1];
22   CeedScalar *v = out[0];
23   for (CeedInt i=0; i<Q; i++) {
24     v[i] = rho[i] * u[i];
25   }
26   return 0;
27 }
28 
29 int main(int argc, char **argv) {
30   Ceed ceed;
31   CeedElemRestriction Erestrictx, Erestrictu, Erestrictxi, Erestrictui;
32   CeedBasis bx, bu;
33   CeedQFunction qf_setup, qf_mass;
34   CeedOperator op_setup, op_mass;
35   CeedVector qdata, X, U, V;
36   const CeedScalar *hv;
37   CeedInt nelem = 12, dim = 2, P = 6, Q = 4;
38   CeedInt nx = 3, ny = 2;
39   CeedInt row, col, offset;
40   CeedInt Ndofs = (nx*2+1)*(ny*2+1), Nqpts = nelem*Q;
41   CeedInt indx[nelem*P];
42   CeedScalar x[dim*Ndofs];
43   CeedScalar qref[dim*Q], qweight[Q];
44   CeedScalar interp[P*Q], grad[dim*P*Q];
45   CeedScalar sum;
46 
47   CeedInit(argv[1], &ceed);
48 
49   for (CeedInt i=0; i<Ndofs; i++) {
50     x[i] = (1. / (nx*2)) * (CeedScalar) (i % (nx*2+1));
51     x[i+Ndofs] = (1. / (ny*2)) * (CeedScalar) (i / (nx*2+1));
52   }
53   for (CeedInt i=0; i<nelem/2; i++) {
54     col = i % nx;
55     row = i / nx;
56     offset = col*2 + row*(nx*2+1)*2;
57 
58     indx[i*2*P +  0] =  2 + offset;
59     indx[i*2*P +  1] =  9 + offset;
60     indx[i*2*P +  2] = 16 + offset;
61     indx[i*2*P +  3] =  1 + offset;
62     indx[i*2*P +  4] =  8 + offset;
63     indx[i*2*P +  5] =  0 + offset;
64 
65     indx[i*2*P +  6] = 14 + offset;
66     indx[i*2*P +  7] =  7 + offset;
67     indx[i*2*P +  8] =  0 + offset;
68     indx[i*2*P +  9] = 15 + offset;
69     indx[i*2*P + 10] =  8 + offset;
70     indx[i*2*P + 11] = 16 + offset;
71   }
72 
73   // Restrictions
74   CeedElemRestrictionCreate(ceed, nelem, P, Ndofs, dim, CEED_MEM_HOST,
75                             CEED_USE_POINTER, indx, &Erestrictx);
76   CeedElemRestrictionCreateIdentity(ceed, nelem, P, nelem*P, dim, &Erestrictxi);
77 
78   CeedElemRestrictionCreate(ceed, nelem, P, Ndofs, 1, CEED_MEM_HOST,
79                             CEED_USE_POINTER, indx, &Erestrictu);
80   CeedElemRestrictionCreateIdentity(ceed, nelem, Q, Nqpts, 1, &Erestrictui);
81 
82 
83   // Bases
84   buildmats(qref, qweight, interp, grad);
85   CeedBasisCreateH1(ceed, CEED_TRIANGLE, dim, P, Q, interp, grad, qref,
86                     qweight, &bx);
87 
88   buildmats(qref, qweight, interp, grad);
89   CeedBasisCreateH1(ceed, CEED_TRIANGLE, 1, P, Q, interp, grad, qref,
90                     qweight, &bu);
91 
92   // QFunctions
93   CeedQFunctionCreateInterior(ceed, 1, setup, __FILE__ ":setup", &qf_setup);
94   CeedQFunctionAddInput(qf_setup, "_weight", 1, CEED_EVAL_WEIGHT);
95   CeedQFunctionAddInput(qf_setup, "x", dim, CEED_EVAL_GRAD);
96   CeedQFunctionAddOutput(qf_setup, "rho", 1, CEED_EVAL_NONE);
97 
98   CeedQFunctionCreateInterior(ceed, 1, mass, __FILE__ ":mass", &qf_mass);
99   CeedQFunctionAddInput(qf_mass, "rho", 1, CEED_EVAL_NONE);
100   CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP);
101   CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP);
102 
103   // Operators
104   CeedOperatorCreate(ceed, qf_setup, NULL, NULL, &op_setup);
105 
106   CeedOperatorCreate(ceed, qf_mass, NULL, NULL, &op_mass);
107 
108   CeedVectorCreate(ceed, dim*Ndofs, &X);
109   CeedVectorSetArray(X, CEED_MEM_HOST, CEED_USE_POINTER, x);
110   CeedVectorCreate(ceed, Nqpts, &qdata);
111 
112   CeedOperatorSetField(op_setup, "_weight", Erestrictxi, CEED_NOTRANSPOSE,
113                        bx, CEED_VECTOR_NONE);
114   CeedOperatorSetField(op_setup, "x", Erestrictx, CEED_NOTRANSPOSE,
115                        bx, CEED_VECTOR_ACTIVE);
116   CeedOperatorSetField(op_setup, "rho", Erestrictui, CEED_NOTRANSPOSE,
117                        CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
118 
119   CeedOperatorSetField(op_mass, "rho", Erestrictui, CEED_NOTRANSPOSE,
120                        CEED_BASIS_COLLOCATED, qdata);
121   CeedOperatorSetField(op_mass, "u", Erestrictu, CEED_NOTRANSPOSE,
122                        bu, CEED_VECTOR_ACTIVE);
123   CeedOperatorSetField(op_mass, "v", Erestrictu, CEED_NOTRANSPOSE,
124                        bu, CEED_VECTOR_ACTIVE);
125 
126   CeedOperatorApply(op_setup, X, qdata, CEED_REQUEST_IMMEDIATE);
127 
128   CeedVectorCreate(ceed, Ndofs, &U);
129   CeedVectorSetValue(U, 1.0);
130   CeedVectorCreate(ceed, Ndofs, &V);
131 
132   CeedOperatorApply(op_mass, U, V, CEED_REQUEST_IMMEDIATE);
133 
134   // Check output
135   CeedVectorGetArrayRead(V, CEED_MEM_HOST, &hv);
136   sum = 0.;
137   for (CeedInt i=0; i<Ndofs; i++)
138     sum += hv[i];
139   if (fabs(sum-1.)>1e-10) printf("Computed Area: %f != True Area: 1.0\n", sum);
140   CeedVectorRestoreArrayRead(V, &hv);
141 
142   CeedQFunctionDestroy(&qf_setup);
143   CeedQFunctionDestroy(&qf_mass);
144   CeedOperatorDestroy(&op_setup);
145   CeedOperatorDestroy(&op_mass);
146   CeedElemRestrictionDestroy(&Erestrictu);
147   CeedElemRestrictionDestroy(&Erestrictx);
148   CeedElemRestrictionDestroy(&Erestrictui);
149   CeedElemRestrictionDestroy(&Erestrictxi);
150   CeedBasisDestroy(&bu);
151   CeedBasisDestroy(&bx);
152   CeedVectorDestroy(&X);
153   CeedVectorDestroy(&U);
154   CeedVectorDestroy(&V);
155   CeedVectorDestroy(&qdata);
156   CeedDestroy(&ceed);
157   return 0;
158 }
159