xref: /libCEED/tests/t537-operator.c (revision 4fee36f0a30516a0b5ad51bf7eb3b32d83efd623)
1d965c7a7SJeremy L Thompson /// @file
2d965c7a7SJeremy L Thompson /// Test assembly of mass matrix operator point block diagonal
3d965c7a7SJeremy L Thompson /// \test Test assembly of mass matrix operator point block diagonal
4d965c7a7SJeremy L Thompson #include "t537-operator.h"
5d965c7a7SJeremy L Thompson 
62b730f8bSJeremy L Thompson #include <ceed.h>
72b730f8bSJeremy L Thompson #include <math.h>
82b730f8bSJeremy L Thompson #include <stdlib.h>
92b730f8bSJeremy L Thompson 
10d965c7a7SJeremy L Thompson int main(int argc, char **argv) {
11d965c7a7SJeremy L Thompson   Ceed                ceed;
12*4fee36f0SJeremy L Thompson   CeedElemRestriction elem_restriction_x, elem_restriction_u, elem_restriction_q_data;
13d1d35e2fSjeremylt   CeedBasis           basis_x, basis_u;
14d965c7a7SJeremy L Thompson   CeedQFunction       qf_setup, qf_mass;
15d965c7a7SJeremy L Thompson   CeedOperator        op_setup, op_mass;
16*4fee36f0SJeremy L Thompson   CeedVector          q_data, x, assembled, u, v;
17*4fee36f0SJeremy L Thompson   CeedInt             num_elem = 6, p = 3, q = 4, dim = 2, num_comp = 2;
18d965c7a7SJeremy L Thompson   CeedInt             nx = 3, ny = 2;
19*4fee36f0SJeremy L Thompson   CeedInt             num_dofs = (nx * 2 + 1) * (ny * 2 + 1), num_qpts = num_elem * q * q;
20*4fee36f0SJeremy L Thompson   CeedInt             ind_x[num_elem * p * p];
21*4fee36f0SJeremy L Thompson   CeedScalar          assembled_true[num_comp * num_comp * num_dofs];
22d965c7a7SJeremy L Thompson 
23d965c7a7SJeremy L Thompson   CeedInit(argv[1], &ceed);
24d965c7a7SJeremy L Thompson 
25*4fee36f0SJeremy L Thompson   // Vectors
26*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, dim * num_dofs, &x);
27*4fee36f0SJeremy L Thompson   {
28*4fee36f0SJeremy L Thompson     CeedScalar x_array[dim * num_dofs];
29*4fee36f0SJeremy L Thompson 
302b730f8bSJeremy L Thompson     for (CeedInt i = 0; i < nx * 2 + 1; i++) {
31d965c7a7SJeremy L Thompson       for (CeedInt j = 0; j < ny * 2 + 1; j++) {
32*4fee36f0SJeremy L Thompson         x_array[i + j * (nx * 2 + 1) + 0 * num_dofs] = (CeedScalar)i / (2 * nx);
33*4fee36f0SJeremy L Thompson         x_array[i + j * (nx * 2 + 1) + 1 * num_dofs] = (CeedScalar)j / (2 * ny);
34d965c7a7SJeremy L Thompson       }
352b730f8bSJeremy L Thompson     }
36*4fee36f0SJeremy L Thompson     CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, x_array);
37*4fee36f0SJeremy L Thompson   }
38d1d35e2fSjeremylt   CeedVectorCreate(ceed, num_qpts, &q_data);
39d965c7a7SJeremy L Thompson 
40*4fee36f0SJeremy L Thompson   // Restrictions
41d1d35e2fSjeremylt   for (CeedInt i = 0; i < num_elem; i++) {
42d965c7a7SJeremy L Thompson     CeedInt col, row, offset;
43d965c7a7SJeremy L Thompson     col    = i % nx;
44d965c7a7SJeremy L Thompson     row    = i / nx;
45*4fee36f0SJeremy L Thompson     offset = col * (p - 1) + row * (nx * 2 + 1) * (p - 1);
46*4fee36f0SJeremy L Thompson     for (CeedInt j = 0; j < p; j++) {
47*4fee36f0SJeremy L Thompson       for (CeedInt k = 0; k < p; k++) ind_x[p * (p * i + k) + j] = offset + k * (nx * 2 + 1) + j;
482b730f8bSJeremy L Thompson     }
49d965c7a7SJeremy L Thompson   }
50*4fee36f0SJeremy L Thompson   CeedElemRestrictionCreate(ceed, num_elem, p * p, dim, num_dofs, dim * num_dofs, CEED_MEM_HOST, CEED_USE_POINTER, ind_x, &elem_restriction_x);
51*4fee36f0SJeremy L Thompson   CeedElemRestrictionCreate(ceed, num_elem, p * p, num_comp, num_dofs, num_comp * num_dofs, CEED_MEM_HOST, CEED_USE_POINTER, ind_x,
52*4fee36f0SJeremy L Thompson                             &elem_restriction_u);
53d965c7a7SJeremy L Thompson 
54*4fee36f0SJeremy L Thompson   CeedInt strides_q_data[3] = {1, q * q, q * q};
55*4fee36f0SJeremy L Thompson   CeedElemRestrictionCreateStrided(ceed, num_elem, q * q, 1, num_qpts, strides_q_data, &elem_restriction_q_data);
56d965c7a7SJeremy L Thompson 
57d965c7a7SJeremy L Thompson   // Bases
58*4fee36f0SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, dim, dim, p, q, CEED_GAUSS, &basis_x);
59*4fee36f0SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, dim, num_comp, p, q, CEED_GAUSS, &basis_u);
60d965c7a7SJeremy L Thompson 
61d965c7a7SJeremy L Thompson   // QFunctions
62d965c7a7SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup);
63a61c78d6SJeremy L Thompson   CeedQFunctionAddInput(qf_setup, "weight", 1, CEED_EVAL_WEIGHT);
64d965c7a7SJeremy L Thompson   CeedQFunctionAddInput(qf_setup, "dx", dim * dim, CEED_EVAL_GRAD);
65d965c7a7SJeremy L Thompson   CeedQFunctionAddOutput(qf_setup, "rho", 1, CEED_EVAL_NONE);
66d965c7a7SJeremy L Thompson 
67d965c7a7SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass);
68d965c7a7SJeremy L Thompson   CeedQFunctionAddInput(qf_mass, "rho", 1, CEED_EVAL_NONE);
69d1d35e2fSjeremylt   CeedQFunctionAddInput(qf_mass, "u", num_comp, CEED_EVAL_INTERP);
70d1d35e2fSjeremylt   CeedQFunctionAddOutput(qf_mass, "v", num_comp, CEED_EVAL_INTERP);
71d965c7a7SJeremy L Thompson 
72d965c7a7SJeremy L Thompson   // Operators
732b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_setup, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_setup);
742b730f8bSJeremy L Thompson   CeedOperatorSetField(op_setup, "weight", CEED_ELEMRESTRICTION_NONE, basis_x, CEED_VECTOR_NONE);
75*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_setup, "dx", elem_restriction_x, basis_x, CEED_VECTOR_ACTIVE);
76*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_setup, "rho", elem_restriction_q_data, CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
77d965c7a7SJeremy L Thompson 
782b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_mass, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_mass);
79*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass, "rho", elem_restriction_q_data, CEED_BASIS_COLLOCATED, q_data);
80*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass, "u", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
81*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass, "v", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
82d965c7a7SJeremy L Thompson 
83d965c7a7SJeremy L Thompson   // Apply Setup Operator
84*4fee36f0SJeremy L Thompson   CeedOperatorApply(op_setup, x, q_data, CEED_REQUEST_IMMEDIATE);
85d965c7a7SJeremy L Thompson 
86d965c7a7SJeremy L Thompson   // Assemble diagonal
87*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_comp * num_comp * num_dofs, &assembled);
88*4fee36f0SJeremy L Thompson   CeedOperatorLinearAssemblePointBlockDiagonal(op_mass, assembled, CEED_REQUEST_IMMEDIATE);
89d965c7a7SJeremy L Thompson 
90d965c7a7SJeremy L Thompson   // Manually assemble diagonal
91*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_comp * num_dofs, &u);
92*4fee36f0SJeremy L Thompson   CeedVectorSetValue(u, 0.0);
93*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_comp * num_dofs, &v);
942b730f8bSJeremy L Thompson   for (int i = 0; i < num_comp * num_comp * num_dofs; i++) assembled_true[i] = 0.0;
95d1d35e2fSjeremylt   CeedInt ind_old = -1;
96d1d35e2fSjeremylt   for (int i = 0; i < num_dofs; i++) {
97d1d35e2fSjeremylt     for (int j = 0; j < num_comp; j++) {
98*4fee36f0SJeremy L Thompson       CeedScalar       *u_array;
99*4fee36f0SJeremy L Thompson       const CeedScalar *v_array;
100*4fee36f0SJeremy L Thompson 
101d965c7a7SJeremy L Thompson       // Set input
102*4fee36f0SJeremy L Thompson       CeedVectorGetArray(u, CEED_MEM_HOST, &u_array);
103d1d35e2fSjeremylt       CeedInt ind  = i + j * num_dofs;
104*4fee36f0SJeremy L Thompson       u_array[ind] = 1.0;
105*4fee36f0SJeremy L Thompson       if (ind > 0) u_array[ind_old] = 0.0;
106d1d35e2fSjeremylt       ind_old = ind;
107*4fee36f0SJeremy L Thompson       CeedVectorRestoreArray(u, &u_array);
108d965c7a7SJeremy L Thompson 
109d965c7a7SJeremy L Thompson       // Compute effect of DoF i, comp j
110*4fee36f0SJeremy L Thompson       CeedOperatorApply(op_mass, u, v, CEED_REQUEST_IMMEDIATE);
111d965c7a7SJeremy L Thompson 
112d965c7a7SJeremy L Thompson       // Retrieve entry
113*4fee36f0SJeremy L Thompson       CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
114*4fee36f0SJeremy L Thompson       for (int k = 0; k < num_comp; k++) assembled_true[i * num_comp * num_comp + k * num_comp + j] += v_array[i + k * num_dofs];
115*4fee36f0SJeremy L Thompson       CeedVectorRestoreArrayRead(v, &v_array);
116d965c7a7SJeremy L Thompson     }
117d965c7a7SJeremy L Thompson   }
118d965c7a7SJeremy L Thompson 
119d965c7a7SJeremy L Thompson   // Check output
120*4fee36f0SJeremy L Thompson   {
121*4fee36f0SJeremy L Thompson     const CeedScalar *assembled_array;
122*4fee36f0SJeremy L Thompson 
123*4fee36f0SJeremy L Thompson     CeedVectorGetArrayRead(assembled, CEED_MEM_HOST, &assembled_array);
1242b730f8bSJeremy L Thompson     for (int i = 0; i < num_comp * num_comp * num_dofs; i++) {
125*4fee36f0SJeremy L Thompson       if (fabs(assembled_array[i] - assembled_true[i]) > 100. * CEED_EPSILON) {
126*4fee36f0SJeremy L Thompson         // LCOV_EXCL_START
127*4fee36f0SJeremy L Thompson         printf("[%" CeedInt_FMT "] Error in assembly: %f != %f\n", i, assembled_array[i], assembled_true[i]);
128*4fee36f0SJeremy L Thompson         // LCOV_EXCL_STOP
1292b730f8bSJeremy L Thompson       }
130*4fee36f0SJeremy L Thompson     }
131*4fee36f0SJeremy L Thompson     CeedVectorRestoreArrayRead(assembled, &assembled_array);
132*4fee36f0SJeremy L Thompson   }
133d965c7a7SJeremy L Thompson 
134d965c7a7SJeremy L Thompson   // Cleanup
135*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&x);
136*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&assembled);
137*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&q_data);
138*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&u);
139*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&v);
140*4fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_u);
141*4fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_x);
142*4fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_q_data);
143*4fee36f0SJeremy L Thompson   CeedBasisDestroy(&basis_u);
144*4fee36f0SJeremy L Thompson   CeedBasisDestroy(&basis_x);
145d965c7a7SJeremy L Thompson   CeedQFunctionDestroy(&qf_setup);
146d965c7a7SJeremy L Thompson   CeedQFunctionDestroy(&qf_mass);
147d965c7a7SJeremy L Thompson   CeedOperatorDestroy(&op_setup);
148d965c7a7SJeremy L Thompson   CeedOperatorDestroy(&op_mass);
149d965c7a7SJeremy L Thompson   CeedDestroy(&ceed);
150d965c7a7SJeremy L Thompson   return 0;
151d965c7a7SJeremy L Thompson }
152