xref: /libCEED/tests/t536-operator.c (revision b7ec98d8959851803d838dd4fd11ad111e0096dd)
1*b7ec98d8SJeremy L Thompson /// @file
2*b7ec98d8SJeremy L Thompson /// Test assembly of mass and Poisson operator QFunction
3*b7ec98d8SJeremy L Thompson /// \test Test assembly of mass and Poisson operator QFunction
4*b7ec98d8SJeremy L Thompson #include <ceed.h>
5*b7ec98d8SJeremy L Thompson #include <stdlib.h>
6*b7ec98d8SJeremy L Thompson #include <math.h>
7*b7ec98d8SJeremy L Thompson #include "t320-basis.h"
8*b7ec98d8SJeremy L Thompson #include "t535-operator.h"
9*b7ec98d8SJeremy L Thompson 
10*b7ec98d8SJeremy L Thompson int main(int argc, char **argv) {
11*b7ec98d8SJeremy L Thompson   Ceed ceed;
12*b7ec98d8SJeremy L Thompson   CeedElemRestriction Erestrictx, Erestrictu,
13*b7ec98d8SJeremy L Thompson                       Erestrictxi, Erestrictui,
14*b7ec98d8SJeremy L Thompson                       Erestrictqi;
15*b7ec98d8SJeremy L Thompson   CeedBasis bx, bu;
16*b7ec98d8SJeremy L Thompson   CeedQFunction qf_setup_mass, qf_setup_diff, qf_apply;
17*b7ec98d8SJeremy L Thompson   CeedOperator op_setup_mass, op_setup_diff, op_apply;
18*b7ec98d8SJeremy L Thompson   CeedVector qdata_mass, qdata_diff, X, A, U, V;
19*b7ec98d8SJeremy L Thompson   CeedInt nelem = 12, dim = 2, P = 6, Q = 4;
20*b7ec98d8SJeremy L Thompson   CeedInt nx = 3, ny = 2;
21*b7ec98d8SJeremy L Thompson   CeedInt row, col, offset;
22*b7ec98d8SJeremy L Thompson   CeedInt ndofs = (nx*2+1)*(ny*2+1), nqpts = nelem*Q*Q;
23*b7ec98d8SJeremy L Thompson   CeedInt indx[nelem*P*P];
24*b7ec98d8SJeremy L Thompson   CeedScalar x[dim*ndofs], assembledTrue[ndofs];
25*b7ec98d8SJeremy L Thompson   CeedScalar qref[dim*Q], qweight[Q];
26*b7ec98d8SJeremy L Thompson   CeedScalar interp[P*Q], grad[dim*P*Q];
27*b7ec98d8SJeremy L Thompson   CeedScalar *u;
28*b7ec98d8SJeremy L Thompson   const CeedScalar *a, *v;
29*b7ec98d8SJeremy L Thompson 
30*b7ec98d8SJeremy L Thompson   CeedInit(argv[1], &ceed);
31*b7ec98d8SJeremy L Thompson 
32*b7ec98d8SJeremy L Thompson   // DoF Coordinates
33*b7ec98d8SJeremy L Thompson   for (CeedInt i=0; i<ndofs; i++) {
34*b7ec98d8SJeremy L Thompson     x[i] = (1. / (nx*2)) * (CeedScalar) (i % (nx*2+1));
35*b7ec98d8SJeremy L Thompson     x[i+ndofs] = (1. / (ny*2)) * (CeedScalar) (i / (nx*2+1));
36*b7ec98d8SJeremy L Thompson   }
37*b7ec98d8SJeremy L Thompson   CeedVectorCreate(ceed, dim*ndofs, &X);
38*b7ec98d8SJeremy L Thompson   CeedVectorSetArray(X, CEED_MEM_HOST, CEED_USE_POINTER, x);
39*b7ec98d8SJeremy L Thompson 
40*b7ec98d8SJeremy L Thompson   // Qdata Vectors
41*b7ec98d8SJeremy L Thompson   CeedVectorCreate(ceed, nqpts, &qdata_mass);
42*b7ec98d8SJeremy L Thompson   CeedVectorCreate(ceed, nqpts*dim*(dim+1)/2, &qdata_diff);
43*b7ec98d8SJeremy L Thompson 
44*b7ec98d8SJeremy L Thompson   // Element Setup
45*b7ec98d8SJeremy L Thompson   for (CeedInt i=0; i<nelem/2; i++) {
46*b7ec98d8SJeremy L Thompson     col = i % nx;
47*b7ec98d8SJeremy L Thompson     row = i / nx;
48*b7ec98d8SJeremy L Thompson     offset = col*2 + row*(nx*2+1)*2;
49*b7ec98d8SJeremy L Thompson 
50*b7ec98d8SJeremy L Thompson     indx[i*2*P +  0] =  2 + offset;
51*b7ec98d8SJeremy L Thompson     indx[i*2*P +  1] =  9 + offset;
52*b7ec98d8SJeremy L Thompson     indx[i*2*P +  2] = 16 + offset;
53*b7ec98d8SJeremy L Thompson     indx[i*2*P +  3] =  1 + offset;
54*b7ec98d8SJeremy L Thompson     indx[i*2*P +  4] =  8 + offset;
55*b7ec98d8SJeremy L Thompson     indx[i*2*P +  5] =  0 + offset;
56*b7ec98d8SJeremy L Thompson 
57*b7ec98d8SJeremy L Thompson     indx[i*2*P +  6] = 14 + offset;
58*b7ec98d8SJeremy L Thompson     indx[i*2*P +  7] =  7 + offset;
59*b7ec98d8SJeremy L Thompson     indx[i*2*P +  8] =  0 + offset;
60*b7ec98d8SJeremy L Thompson     indx[i*2*P +  9] = 15 + offset;
61*b7ec98d8SJeremy L Thompson     indx[i*2*P + 10] =  8 + offset;
62*b7ec98d8SJeremy L Thompson     indx[i*2*P + 11] = 16 + offset;
63*b7ec98d8SJeremy L Thompson   }
64*b7ec98d8SJeremy L Thompson 
65*b7ec98d8SJeremy L Thompson   // Restrictions
66*b7ec98d8SJeremy L Thompson   CeedElemRestrictionCreate(ceed, nelem, P, ndofs, dim, CEED_MEM_HOST,
67*b7ec98d8SJeremy L Thompson                             CEED_USE_POINTER, indx, &Erestrictx);
68*b7ec98d8SJeremy L Thompson   CeedElemRestrictionCreateIdentity(ceed, nelem, P, nelem*P, dim, &Erestrictxi);
69*b7ec98d8SJeremy L Thompson 
70*b7ec98d8SJeremy L Thompson   CeedElemRestrictionCreate(ceed, nelem, P, ndofs, 1, CEED_MEM_HOST,
71*b7ec98d8SJeremy L Thompson                             CEED_USE_POINTER, indx, &Erestrictu);
72*b7ec98d8SJeremy L Thompson   CeedElemRestrictionCreateIdentity(ceed, nelem, Q, nqpts, 1, &Erestrictui);
73*b7ec98d8SJeremy L Thompson 
74*b7ec98d8SJeremy L Thompson 
75*b7ec98d8SJeremy L Thompson   CeedElemRestrictionCreateIdentity(ceed, nelem, Q, nqpts, dim*(dim+1)/2,
76*b7ec98d8SJeremy L Thompson                                     &Erestrictqi);
77*b7ec98d8SJeremy L Thompson 
78*b7ec98d8SJeremy L Thompson   // Bases
79*b7ec98d8SJeremy L Thompson   buildmats(qref, qweight, interp, grad);
80*b7ec98d8SJeremy L Thompson   CeedBasisCreateH1(ceed, CEED_TRIANGLE, dim, P, Q, interp, grad, qref,
81*b7ec98d8SJeremy L Thompson                     qweight, &bx);
82*b7ec98d8SJeremy L Thompson 
83*b7ec98d8SJeremy L Thompson   buildmats(qref, qweight, interp, grad);
84*b7ec98d8SJeremy L Thompson   CeedBasisCreateH1(ceed, CEED_TRIANGLE, 1, P, Q, interp, grad, qref,
85*b7ec98d8SJeremy L Thompson                     qweight, &bu);
86*b7ec98d8SJeremy L Thompson 
87*b7ec98d8SJeremy L Thompson   // QFunction - setup mass
88*b7ec98d8SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, setup_mass, setup_mass_loc,
89*b7ec98d8SJeremy L Thompson                               &qf_setup_mass);
90*b7ec98d8SJeremy L Thompson   CeedQFunctionAddInput(qf_setup_mass, "dx", dim*dim, CEED_EVAL_GRAD);
91*b7ec98d8SJeremy L Thompson   CeedQFunctionAddInput(qf_setup_mass, "_weight", 1, CEED_EVAL_WEIGHT);
92*b7ec98d8SJeremy L Thompson   CeedQFunctionAddOutput(qf_setup_mass, "qdata", 1, CEED_EVAL_NONE);
93*b7ec98d8SJeremy L Thompson 
94*b7ec98d8SJeremy L Thompson   // Operator - setup mass
95*b7ec98d8SJeremy L Thompson   CeedOperatorCreate(ceed, qf_setup_mass, NULL, NULL, &op_setup_mass);
96*b7ec98d8SJeremy L Thompson   CeedOperatorSetField(op_setup_mass, "dx", Erestrictx, CEED_NOTRANSPOSE, bx,
97*b7ec98d8SJeremy L Thompson                        CEED_VECTOR_ACTIVE);
98*b7ec98d8SJeremy L Thompson   CeedOperatorSetField(op_setup_mass, "_weight", Erestrictxi, CEED_NOTRANSPOSE,
99*b7ec98d8SJeremy L Thompson                        bx, CEED_VECTOR_NONE);
100*b7ec98d8SJeremy L Thompson   CeedOperatorSetField(op_setup_mass, "qdata", Erestrictui, CEED_NOTRANSPOSE,
101*b7ec98d8SJeremy L Thompson                        CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
102*b7ec98d8SJeremy L Thompson 
103*b7ec98d8SJeremy L Thompson   // QFunction - setup diff
104*b7ec98d8SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, setup_diff, setup_diff_loc,
105*b7ec98d8SJeremy L Thompson                               &qf_setup_diff);
106*b7ec98d8SJeremy L Thompson   CeedQFunctionAddInput(qf_setup_diff, "dx", dim*dim, CEED_EVAL_GRAD);
107*b7ec98d8SJeremy L Thompson   CeedQFunctionAddInput(qf_setup_diff, "_weight", 1, CEED_EVAL_WEIGHT);
108*b7ec98d8SJeremy L Thompson   CeedQFunctionAddOutput(qf_setup_diff, "qdata", dim*(dim+1)/2, CEED_EVAL_NONE);
109*b7ec98d8SJeremy L Thompson 
110*b7ec98d8SJeremy L Thompson   // Operator - setup diff
111*b7ec98d8SJeremy L Thompson   CeedOperatorCreate(ceed, qf_setup_diff, NULL, NULL, &op_setup_diff);
112*b7ec98d8SJeremy L Thompson   CeedOperatorSetField(op_setup_diff, "dx", Erestrictx, CEED_NOTRANSPOSE, bx,
113*b7ec98d8SJeremy L Thompson                        CEED_VECTOR_ACTIVE);
114*b7ec98d8SJeremy L Thompson   CeedOperatorSetField(op_setup_diff, "_weight", Erestrictxi, CEED_NOTRANSPOSE,
115*b7ec98d8SJeremy L Thompson                        bx, CEED_VECTOR_NONE);
116*b7ec98d8SJeremy L Thompson   CeedOperatorSetField(op_setup_diff, "qdata", Erestrictqi, CEED_NOTRANSPOSE,
117*b7ec98d8SJeremy L Thompson                        CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
118*b7ec98d8SJeremy L Thompson 
119*b7ec98d8SJeremy L Thompson   // Apply Setup Operators
120*b7ec98d8SJeremy L Thompson   CeedOperatorApply(op_setup_mass, X, qdata_mass, CEED_REQUEST_IMMEDIATE);
121*b7ec98d8SJeremy L Thompson   CeedOperatorApply(op_setup_diff, X, qdata_diff, CEED_REQUEST_IMMEDIATE);
122*b7ec98d8SJeremy L Thompson 
123*b7ec98d8SJeremy L Thompson   // QFunction - apply
124*b7ec98d8SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, apply, apply_loc, &qf_apply);
125*b7ec98d8SJeremy L Thompson   CeedQFunctionAddInput(qf_apply, "du", dim, CEED_EVAL_GRAD);
126*b7ec98d8SJeremy L Thompson   CeedQFunctionAddInput(qf_apply, "qdata_mass", 1, CEED_EVAL_NONE);
127*b7ec98d8SJeremy L Thompson   CeedQFunctionAddInput(qf_apply, "qdata_diff", dim*(dim+1)/2, CEED_EVAL_NONE);
128*b7ec98d8SJeremy L Thompson   CeedQFunctionAddInput(qf_apply, "u", 1, CEED_EVAL_INTERP);
129*b7ec98d8SJeremy L Thompson   CeedQFunctionAddOutput(qf_apply, "v", 1, CEED_EVAL_INTERP);
130*b7ec98d8SJeremy L Thompson   CeedQFunctionAddOutput(qf_apply, "dv", dim, CEED_EVAL_GRAD);
131*b7ec98d8SJeremy L Thompson 
132*b7ec98d8SJeremy L Thompson   // Operator - apply
133*b7ec98d8SJeremy L Thompson   CeedOperatorCreate(ceed, qf_apply, NULL, NULL, &op_apply);
134*b7ec98d8SJeremy L Thompson   CeedOperatorSetField(op_apply, "du", Erestrictu, CEED_NOTRANSPOSE, bu,
135*b7ec98d8SJeremy L Thompson                        CEED_VECTOR_ACTIVE);
136*b7ec98d8SJeremy L Thompson   CeedOperatorSetField(op_apply, "qdata_mass", Erestrictui, CEED_NOTRANSPOSE,
137*b7ec98d8SJeremy L Thompson                        CEED_BASIS_COLLOCATED, qdata_mass);
138*b7ec98d8SJeremy L Thompson   CeedOperatorSetField(op_apply, "qdata_diff", Erestrictqi, CEED_NOTRANSPOSE,
139*b7ec98d8SJeremy L Thompson                        CEED_BASIS_COLLOCATED, qdata_diff);
140*b7ec98d8SJeremy L Thompson   CeedOperatorSetField(op_apply, "u", Erestrictu, CEED_NOTRANSPOSE, bu,
141*b7ec98d8SJeremy L Thompson                        CEED_VECTOR_ACTIVE);
142*b7ec98d8SJeremy L Thompson   CeedOperatorSetField(op_apply, "v", Erestrictu, CEED_NOTRANSPOSE, bu,
143*b7ec98d8SJeremy L Thompson                        CEED_VECTOR_ACTIVE);
144*b7ec98d8SJeremy L Thompson   CeedOperatorSetField(op_apply, "dv", Erestrictu, CEED_NOTRANSPOSE, bu,
145*b7ec98d8SJeremy L Thompson                        CEED_VECTOR_ACTIVE);
146*b7ec98d8SJeremy L Thompson 
147*b7ec98d8SJeremy L Thompson   // Assemble diagonal
148*b7ec98d8SJeremy L Thompson   CeedOperatorAssembleLinearDiagonal(op_apply, &A, CEED_REQUEST_IMMEDIATE);
149*b7ec98d8SJeremy L Thompson 
150*b7ec98d8SJeremy L Thompson   // Manually assemble diagonal
151*b7ec98d8SJeremy L Thompson   CeedVectorCreate(ceed, ndofs, &U);
152*b7ec98d8SJeremy L Thompson   CeedVectorSetValue(U, 0.0);
153*b7ec98d8SJeremy L Thompson   CeedVectorCreate(ceed, ndofs, &V);
154*b7ec98d8SJeremy L Thompson   for (int i=0; i<ndofs; i++) {
155*b7ec98d8SJeremy L Thompson     // Set input
156*b7ec98d8SJeremy L Thompson     CeedVectorGetArray(U, CEED_MEM_HOST, &u);
157*b7ec98d8SJeremy L Thompson     u[i] = 1.0;
158*b7ec98d8SJeremy L Thompson     if (i)
159*b7ec98d8SJeremy L Thompson       u[i-1] = 0.0;
160*b7ec98d8SJeremy L Thompson     CeedVectorRestoreArray(U, &u);
161*b7ec98d8SJeremy L Thompson 
162*b7ec98d8SJeremy L Thompson     // Compute diag entry for DoF i
163*b7ec98d8SJeremy L Thompson     CeedOperatorApply(op_apply, U, V, CEED_REQUEST_IMMEDIATE);
164*b7ec98d8SJeremy L Thompson 
165*b7ec98d8SJeremy L Thompson     // Retrieve entry
166*b7ec98d8SJeremy L Thompson     CeedVectorGetArrayRead(V, CEED_MEM_HOST, &v);
167*b7ec98d8SJeremy L Thompson     assembledTrue[i] = v[i];
168*b7ec98d8SJeremy L Thompson     CeedVectorRestoreArrayRead(V, &v);
169*b7ec98d8SJeremy L Thompson   }
170*b7ec98d8SJeremy L Thompson 
171*b7ec98d8SJeremy L Thompson   // Check output
172*b7ec98d8SJeremy L Thompson   CeedVectorGetArrayRead(A, CEED_MEM_HOST, &a);
173*b7ec98d8SJeremy L Thompson   for (int i=0; i<ndofs; i++)
174*b7ec98d8SJeremy L Thompson     if (fabs(a[i] - assembledTrue[i]) > 1E-14)
175*b7ec98d8SJeremy L Thompson       // LCOV_EXCL_START
176*b7ec98d8SJeremy L Thompson       printf("[%d] Error in assembly: %f != %f\n", i, a[i], assembledTrue[i]);
177*b7ec98d8SJeremy L Thompson   // LCOV_EXCL_STOP
178*b7ec98d8SJeremy L Thompson 
179*b7ec98d8SJeremy L Thompson   // Cleanup
180*b7ec98d8SJeremy L Thompson   CeedQFunctionDestroy(&qf_setup_mass);
181*b7ec98d8SJeremy L Thompson   CeedQFunctionDestroy(&qf_setup_diff);
182*b7ec98d8SJeremy L Thompson   CeedQFunctionDestroy(&qf_apply);
183*b7ec98d8SJeremy L Thompson   CeedOperatorDestroy(&op_setup_mass);
184*b7ec98d8SJeremy L Thompson   CeedOperatorDestroy(&op_setup_diff);
185*b7ec98d8SJeremy L Thompson   CeedOperatorDestroy(&op_apply);
186*b7ec98d8SJeremy L Thompson   CeedElemRestrictionDestroy(&Erestrictu);
187*b7ec98d8SJeremy L Thompson   CeedElemRestrictionDestroy(&Erestrictx);
188*b7ec98d8SJeremy L Thompson   CeedElemRestrictionDestroy(&Erestrictui);
189*b7ec98d8SJeremy L Thompson   CeedElemRestrictionDestroy(&Erestrictxi);
190*b7ec98d8SJeremy L Thompson   CeedElemRestrictionDestroy(&Erestrictqi);
191*b7ec98d8SJeremy L Thompson   CeedBasisDestroy(&bu);
192*b7ec98d8SJeremy L Thompson   CeedBasisDestroy(&bx);
193*b7ec98d8SJeremy L Thompson   CeedVectorDestroy(&X);
194*b7ec98d8SJeremy L Thompson   CeedVectorDestroy(&A);
195*b7ec98d8SJeremy L Thompson   CeedVectorDestroy(&qdata_mass);
196*b7ec98d8SJeremy L Thompson   CeedVectorDestroy(&qdata_diff);
197*b7ec98d8SJeremy L Thompson   CeedVectorDestroy(&U);
198*b7ec98d8SJeremy L Thompson   CeedVectorDestroy(&V);
199*b7ec98d8SJeremy L Thompson   CeedDestroy(&ceed);
200*b7ec98d8SJeremy L Thompson   return 0;
201*b7ec98d8SJeremy L Thompson }
202