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