xref: /libCEED/tests/t534-operator.c (revision 7509a596beda7c1d002d2274a17375b09541fdb6)
1b7ec98d8SJeremy L Thompson /// @file
2*7509a596Sjeremylt /// Test assembly of Poisson operator diagonal
3*7509a596Sjeremylt /// \test Test assembly of Poisson operator diagonal
4b7ec98d8SJeremy L Thompson #include <ceed.h>
5b7ec98d8SJeremy L Thompson #include <stdlib.h>
6b7ec98d8SJeremy L Thompson #include <math.h>
7b7ec98d8SJeremy L Thompson #include "t534-operator.h"
8b7ec98d8SJeremy L Thompson 
9b7ec98d8SJeremy L Thompson int main(int argc, char **argv) {
10b7ec98d8SJeremy L Thompson   Ceed ceed;
1161dbc9d2Sjeremylt   CeedInterlaceMode imode = CEED_NONINTERLACED;
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, qf_diff;
17b7ec98d8SJeremy L Thompson   CeedOperator op_setup, op_diff;
18b7ec98d8SJeremy L Thompson   CeedVector qdata, X, A, U, V;
19b7ec98d8SJeremy L Thompson   CeedInt nelem = 6, P = 3, Q = 4, dim = 2;
20b7ec98d8SJeremy L Thompson   CeedInt nx = 3, ny = 2;
21b7ec98d8SJeremy L Thompson   CeedInt ndofs = (nx*2+1)*(ny*2+1), nqpts = nelem*Q*Q;
22b7ec98d8SJeremy L Thompson   CeedInt indx[nelem*P*P];
23b7ec98d8SJeremy L Thompson   CeedScalar x[dim*ndofs], assembledTrue[ndofs];
24b7ec98d8SJeremy L Thompson   CeedScalar *u;
25b7ec98d8SJeremy L Thompson   const CeedScalar *a, *v;
26b7ec98d8SJeremy L Thompson 
27b7ec98d8SJeremy L Thompson   CeedInit(argv[1], &ceed);
28b7ec98d8SJeremy L Thompson 
29b7ec98d8SJeremy L Thompson   // DoF Coordinates
30b7ec98d8SJeremy L Thompson   for (CeedInt i=0; i<nx*2+1; i++)
31b7ec98d8SJeremy L Thompson     for (CeedInt j=0; j<ny*2+1; j++) {
32b7ec98d8SJeremy L Thompson       x[i+j*(nx*2+1)+0*ndofs] = (CeedScalar) i / (2*nx);
33b7ec98d8SJeremy L Thompson       x[i+j*(nx*2+1)+1*ndofs] = (CeedScalar) j / (2*ny);
34b7ec98d8SJeremy L Thompson     }
35b7ec98d8SJeremy L Thompson   CeedVectorCreate(ceed, dim*ndofs, &X);
36b7ec98d8SJeremy L Thompson   CeedVectorSetArray(X, CEED_MEM_HOST, CEED_USE_POINTER, x);
37b7ec98d8SJeremy L Thompson 
38b7ec98d8SJeremy L Thompson   // Qdata Vector
39b7ec98d8SJeremy L Thompson   CeedVectorCreate(ceed, nqpts*dim*(dim+1)/2, &qdata);
40b7ec98d8SJeremy L Thompson 
41b7ec98d8SJeremy L Thompson   // Element Setup
42b7ec98d8SJeremy L Thompson   for (CeedInt i=0; i<nelem; i++) {
43b7ec98d8SJeremy L Thompson     CeedInt col, row, offset;
44b7ec98d8SJeremy L Thompson     col = i % nx;
45b7ec98d8SJeremy L Thompson     row = i / nx;
46b7ec98d8SJeremy L Thompson     offset = col*(P-1) + row*(nx*2+1)*(P-1);
47b7ec98d8SJeremy L Thompson     for (CeedInt j=0; j<P; j++)
48b7ec98d8SJeremy L Thompson       for (CeedInt k=0; k<P; k++)
49b7ec98d8SJeremy L Thompson         indx[P*(P*i+k)+j] = offset + k*(nx*2+1) + j;
50b7ec98d8SJeremy L Thompson   }
51b7ec98d8SJeremy L Thompson 
52b7ec98d8SJeremy L Thompson   // Restrictions
5361dbc9d2Sjeremylt   CeedElemRestrictionCreate(ceed, imode, nelem, P*P, ndofs, dim, CEED_MEM_HOST,
54b7ec98d8SJeremy L Thompson                             CEED_USE_POINTER, indx, &Erestrictx);
55*7509a596Sjeremylt   CeedInt stridesx[3] = {1, P*P, P *P*dim};
56*7509a596Sjeremylt   CeedElemRestrictionCreateStrided(ceed,  nelem, P*P, nelem*P*P, dim, stridesx,
57b7ec98d8SJeremy L Thompson                                    &Erestrictxi);
58b7ec98d8SJeremy L Thompson 
5961dbc9d2Sjeremylt   CeedElemRestrictionCreate(ceed, imode, nelem, P*P, ndofs, 1, CEED_MEM_HOST,
60b7ec98d8SJeremy L Thompson                             CEED_USE_POINTER, indx, &Erestrictu);
61*7509a596Sjeremylt   CeedInt stridesu[3] = {1, Q*Q, Q*Q};
62*7509a596Sjeremylt   CeedElemRestrictionCreateStrided(ceed, nelem, Q*Q, nqpts, 1, stridesu,
63a8d32208Sjeremylt                                    &Erestrictui);
64b7ec98d8SJeremy L Thompson 
65*7509a596Sjeremylt   CeedInt stridesqd[3] = {1, Q*Q, Q *Q *dim *(dim+1)/2};
66*7509a596Sjeremylt   CeedElemRestrictionCreateStrided(ceed, nelem, Q*Q, nqpts, dim*(dim+1)/2,
67*7509a596Sjeremylt                                    stridesqd, &Erestrictqi);
68b7ec98d8SJeremy L Thompson 
69b7ec98d8SJeremy L Thompson   // Bases
70b7ec98d8SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, dim, dim, P, Q, CEED_GAUSS, &bx);
71b7ec98d8SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, dim, 1, P, Q, CEED_GAUSS, &bu);
72b7ec98d8SJeremy L Thompson 
73b7ec98d8SJeremy L Thompson   // QFunction - setup
74b7ec98d8SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup);
75b7ec98d8SJeremy L Thompson   CeedQFunctionAddInput(qf_setup, "dx", dim*dim, CEED_EVAL_GRAD);
76b7ec98d8SJeremy L Thompson   CeedQFunctionAddInput(qf_setup, "_weight", 1, CEED_EVAL_WEIGHT);
77b7ec98d8SJeremy L Thompson   CeedQFunctionAddOutput(qf_setup, "qdata", dim*(dim+1)/2, CEED_EVAL_NONE);
78b7ec98d8SJeremy L Thompson 
79b7ec98d8SJeremy L Thompson   // Operator - setup
80442e7f0bSjeremylt   CeedOperatorCreate(ceed, qf_setup, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE,
81442e7f0bSjeremylt                      &op_setup);
82a8d32208Sjeremylt   CeedOperatorSetField(op_setup, "dx", Erestrictx, bx, CEED_VECTOR_ACTIVE);
83a8d32208Sjeremylt   CeedOperatorSetField(op_setup, "_weight", Erestrictxi, bx, CEED_VECTOR_NONE);
84a8d32208Sjeremylt   CeedOperatorSetField(op_setup, "qdata", Erestrictqi, CEED_BASIS_COLLOCATED,
85b7ec98d8SJeremy L Thompson                        CEED_VECTOR_ACTIVE);
86b7ec98d8SJeremy L Thompson 
87b7ec98d8SJeremy L Thompson   // Apply Setup Operator
88b7ec98d8SJeremy L Thompson   CeedOperatorApply(op_setup, X, qdata, CEED_REQUEST_IMMEDIATE);
89b7ec98d8SJeremy L Thompson 
90b7ec98d8SJeremy L Thompson   // QFunction - apply
91b7ec98d8SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, diff, diff_loc, &qf_diff);
92b7ec98d8SJeremy L Thompson   CeedQFunctionAddInput(qf_diff, "du", dim, CEED_EVAL_GRAD);
93b7ec98d8SJeremy L Thompson   CeedQFunctionAddInput(qf_diff, "qdata", dim*(dim+1)/2, CEED_EVAL_NONE);
94b7ec98d8SJeremy L Thompson   CeedQFunctionAddOutput(qf_diff, "dv", dim, CEED_EVAL_GRAD);
95b7ec98d8SJeremy L Thompson 
96b7ec98d8SJeremy L Thompson   // Operator - apply
97442e7f0bSjeremylt   CeedOperatorCreate(ceed, qf_diff, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE,
98442e7f0bSjeremylt                      &op_diff);
99a8d32208Sjeremylt   CeedOperatorSetField(op_diff, "du", Erestrictu, bu, CEED_VECTOR_ACTIVE);
100a8d32208Sjeremylt   CeedOperatorSetField(op_diff, "qdata", Erestrictqi, CEED_BASIS_COLLOCATED,
101a8d32208Sjeremylt                        qdata);
102a8d32208Sjeremylt   CeedOperatorSetField(op_diff, "dv", Erestrictu, bu, CEED_VECTOR_ACTIVE);
103b7ec98d8SJeremy L Thompson 
104b7ec98d8SJeremy L Thompson   // Assemble diagonal
105b7ec98d8SJeremy L Thompson   CeedOperatorAssembleLinearDiagonal(op_diff, &A, CEED_REQUEST_IMMEDIATE);
106b7ec98d8SJeremy L Thompson 
107b7ec98d8SJeremy L Thompson   // Manually assemble diagonal
108b7ec98d8SJeremy L Thompson   CeedVectorCreate(ceed, ndofs, &U);
109b7ec98d8SJeremy L Thompson   CeedVectorSetValue(U, 0.0);
110b7ec98d8SJeremy L Thompson   CeedVectorCreate(ceed, ndofs, &V);
111b7ec98d8SJeremy L Thompson   for (int i=0; i<ndofs; i++) {
112b7ec98d8SJeremy L Thompson     // Set input
113b7ec98d8SJeremy L Thompson     CeedVectorGetArray(U, CEED_MEM_HOST, &u);
114b7ec98d8SJeremy L Thompson     u[i] = 1.0;
115b7ec98d8SJeremy L Thompson     if (i)
116b7ec98d8SJeremy L Thompson       u[i-1] = 0.0;
117b7ec98d8SJeremy L Thompson     CeedVectorRestoreArray(U, &u);
118b7ec98d8SJeremy L Thompson 
119b7ec98d8SJeremy L Thompson     // Compute diag entry for DoF i
120b7ec98d8SJeremy L Thompson     CeedOperatorApply(op_diff, U, V, CEED_REQUEST_IMMEDIATE);
121b7ec98d8SJeremy L Thompson 
122b7ec98d8SJeremy L Thompson     // Retrieve entry
123b7ec98d8SJeremy L Thompson     CeedVectorGetArrayRead(V, CEED_MEM_HOST, &v);
124b7ec98d8SJeremy L Thompson     assembledTrue[i] = v[i];
125b7ec98d8SJeremy L Thompson     CeedVectorRestoreArrayRead(V, &v);
126b7ec98d8SJeremy L Thompson   }
127b7ec98d8SJeremy L Thompson 
128b7ec98d8SJeremy L Thompson   // Check output
129b7ec98d8SJeremy L Thompson   CeedVectorGetArrayRead(A, CEED_MEM_HOST, &a);
130b7ec98d8SJeremy L Thompson   for (int i=0; i<ndofs; i++)
1314f694d2fSjeremylt     if (fabs(a[i] - assembledTrue[i]) > 1e-13)
132b7ec98d8SJeremy L Thompson       // LCOV_EXCL_START
133b7ec98d8SJeremy L Thompson       printf("[%d] Error in assembly: %f != %f\n", i, a[i], assembledTrue[i]);
134b7ec98d8SJeremy L Thompson   // LCOV_EXCL_STOP
135b7ec98d8SJeremy L Thompson 
136b7ec98d8SJeremy L Thompson   // Cleanup
137b7ec98d8SJeremy L Thompson   CeedQFunctionDestroy(&qf_setup);
138b7ec98d8SJeremy L Thompson   CeedQFunctionDestroy(&qf_diff);
139b7ec98d8SJeremy L Thompson   CeedOperatorDestroy(&op_setup);
140b7ec98d8SJeremy L Thompson   CeedOperatorDestroy(&op_diff);
141b7ec98d8SJeremy L Thompson   CeedElemRestrictionDestroy(&Erestrictu);
142b7ec98d8SJeremy L Thompson   CeedElemRestrictionDestroy(&Erestrictx);
143b7ec98d8SJeremy L Thompson   CeedElemRestrictionDestroy(&Erestrictui);
144b7ec98d8SJeremy L Thompson   CeedElemRestrictionDestroy(&Erestrictxi);
145b7ec98d8SJeremy L Thompson   CeedElemRestrictionDestroy(&Erestrictqi);
146b7ec98d8SJeremy L Thompson   CeedBasisDestroy(&bu);
147b7ec98d8SJeremy L Thompson   CeedBasisDestroy(&bx);
148b7ec98d8SJeremy L Thompson   CeedVectorDestroy(&X);
149b7ec98d8SJeremy L Thompson   CeedVectorDestroy(&A);
150b7ec98d8SJeremy L Thompson   CeedVectorDestroy(&qdata);
151b7ec98d8SJeremy L Thompson   CeedVectorDestroy(&U);
152b7ec98d8SJeremy L Thompson   CeedVectorDestroy(&V);
153b7ec98d8SJeremy L Thompson   CeedDestroy(&ceed);
154b7ec98d8SJeremy L Thompson   return 0;
155b7ec98d8SJeremy L Thompson }
156