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