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