xref: /libCEED/tests/t567-operator.c (revision 2b730f8b5a9c809740a0b3b302db43a719c636b1)
196461910SJeremy L Thompson /// @file
296461910SJeremy L Thompson /// Test assembly of non-symmetric Poisson operator (multi-component)
396461910SJeremy L Thompson /// \test Test assembly of non-symmetric Poisson operator (multi-component)
496461910SJeremy L Thompson #include "t567-operator.h"
596461910SJeremy L Thompson 
6*2b730f8bSJeremy L Thompson #include <ceed.h>
7*2b730f8bSJeremy L Thompson #include <math.h>
8*2b730f8bSJeremy L Thompson #include <stdlib.h>
9*2b730f8bSJeremy L Thompson 
1096461910SJeremy L Thompson int main(int argc, char **argv) {
1196461910SJeremy L Thompson   Ceed                ceed;
12*2b730f8bSJeremy L Thompson   CeedElemRestriction elem_restr_x, elem_restr_u, elem_restr_qd_i;
1396461910SJeremy L Thompson   CeedBasis           basis_x, basis_u;
1496461910SJeremy L Thompson   CeedQFunction       qf_setup, qf_diff;
1596461910SJeremy L Thompson   CeedOperator        op_setup, op_diff;
1696461910SJeremy L Thompson   CeedVector          q_data, X, U, V;
1796461910SJeremy L Thompson   CeedInt             P = 3, Q = 3, dim = 2, num_comp = 2;
1896461910SJeremy L Thompson   CeedInt             n_x = 1, n_y = 1;
1996461910SJeremy L Thompson   CeedInt             num_elem = n_x * n_y;
2096461910SJeremy L Thompson   CeedInt             num_dofs = (n_x * (P - 1) + 1) * (n_y * (P - 1) + 1), num_qpts = num_elem * Q * Q;
2196461910SJeremy L Thompson   CeedInt             ind_x[num_elem * P * P];
2296461910SJeremy L Thompson   CeedScalar          assembled[num_comp * num_comp * num_dofs * num_dofs];
2396461910SJeremy L Thompson   CeedScalar          x[dim * num_dofs], assembled_true[num_comp * num_comp * num_dofs * num_dofs];
2496461910SJeremy L Thompson   CeedScalar         *u;
2596461910SJeremy L Thompson   const CeedScalar   *v;
2696461910SJeremy L Thompson 
2796461910SJeremy L Thompson   CeedInit(argv[1], &ceed);
2896461910SJeremy L Thompson 
2996461910SJeremy L Thompson   // DoF Coordinates
30*2b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < n_x * (P - 1) + 1; i++) {
3196461910SJeremy L Thompson     for (CeedInt j = 0; j < n_y * (P - 1) + 1; j++) {
3296461910SJeremy L Thompson       x[i + j * (n_x * 2 + 1) + 0 * num_dofs] = (CeedScalar)i / (n_x * (P - 1));
3396461910SJeremy L Thompson       x[i + j * (n_x * 2 + 1) + 1 * num_dofs] = (CeedScalar)j / (n_y * (P - 1));
3496461910SJeremy L Thompson     }
35*2b730f8bSJeremy L Thompson   }
3696461910SJeremy L Thompson   CeedVectorCreate(ceed, dim * num_dofs, &X);
3796461910SJeremy L Thompson   CeedVectorSetArray(X, CEED_MEM_HOST, CEED_USE_POINTER, x);
3896461910SJeremy L Thompson 
3996461910SJeremy L Thompson   // Qdata Vector
4096461910SJeremy L Thompson   CeedVectorCreate(ceed, num_qpts * dim * (dim + 1) / 2, &q_data);
4196461910SJeremy L Thompson 
4296461910SJeremy L Thompson   // Element Setup
4396461910SJeremy L Thompson   for (CeedInt i = 0; i < num_elem; i++) {
4496461910SJeremy L Thompson     CeedInt col, row, offset;
4596461910SJeremy L Thompson     col    = i % n_x;
4696461910SJeremy L Thompson     row    = i / n_x;
4796461910SJeremy L Thompson     offset = col * (P - 1) + row * (n_x * (P - 1) + 1) * (P - 1);
48*2b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < P; j++) {
49*2b730f8bSJeremy L Thompson       for (CeedInt k = 0; k < P; k++) ind_x[P * (P * i + k) + j] = offset + k * P + j;
50*2b730f8bSJeremy L Thompson     }
5196461910SJeremy L Thompson   }
5296461910SJeremy L Thompson 
5396461910SJeremy L Thompson   // Restrictions
54*2b730f8bSJeremy L Thompson   CeedElemRestrictionCreate(ceed, num_elem, P * P, dim, num_dofs, dim * num_dofs, CEED_MEM_HOST, CEED_USE_POINTER, ind_x, &elem_restr_x);
55*2b730f8bSJeremy L Thompson   CeedElemRestrictionCreate(ceed, num_elem, P * P, num_comp, num_dofs, num_comp * num_dofs, CEED_MEM_HOST, CEED_USE_POINTER, ind_x, &elem_restr_u);
5696461910SJeremy L Thompson   CeedInt strides_qd[3] = {1, Q * Q * num_elem, Q * Q}; /* *NOPAD* */
57*2b730f8bSJeremy L Thompson   CeedElemRestrictionCreateStrided(ceed, num_elem, Q * Q, dim * (dim + 1) / 2, num_qpts * dim * (dim + 1) / 2, strides_qd, &elem_restr_qd_i);
5896461910SJeremy L Thompson 
5996461910SJeremy L Thompson   // Bases
6096461910SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, dim, dim, P, Q, CEED_GAUSS, &basis_x);
61*2b730f8bSJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, dim, num_comp, P, Q, CEED_GAUSS, &basis_u);
6296461910SJeremy L Thompson 
6396461910SJeremy L Thompson   // QFunctions
6496461910SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup);
6596461910SJeremy L Thompson   CeedQFunctionAddInput(qf_setup, "weight", 1, CEED_EVAL_WEIGHT);
6696461910SJeremy L Thompson   CeedQFunctionAddInput(qf_setup, "dx", dim * dim, CEED_EVAL_GRAD);
6796461910SJeremy L Thompson   CeedQFunctionAddOutput(qf_setup, "qdata", dim * (dim + 1) / 2, CEED_EVAL_NONE);
6896461910SJeremy L Thompson 
6996461910SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, diff, diff_loc, &qf_diff);
7096461910SJeremy L Thompson   CeedQFunctionAddInput(qf_diff, "qdata", dim * (dim + 1) / 2, CEED_EVAL_NONE);
7196461910SJeremy L Thompson   CeedQFunctionAddInput(qf_diff, "u", num_comp * dim, CEED_EVAL_GRAD);
7296461910SJeremy L Thompson   CeedQFunctionAddOutput(qf_diff, "v", num_comp * dim, CEED_EVAL_GRAD);
7396461910SJeremy L Thompson 
7496461910SJeremy L Thompson   // Operators
75*2b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_setup, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_setup);
76*2b730f8bSJeremy L Thompson   CeedOperatorSetField(op_setup, "weight", CEED_ELEMRESTRICTION_NONE, basis_x, CEED_VECTOR_NONE);
7796461910SJeremy L Thompson   CeedOperatorSetField(op_setup, "dx", elem_restr_x, basis_x, CEED_VECTOR_ACTIVE);
78*2b730f8bSJeremy L Thompson   CeedOperatorSetField(op_setup, "qdata", elem_restr_qd_i, CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
7996461910SJeremy L Thompson 
80*2b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_diff, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_diff);
81*2b730f8bSJeremy L Thompson   CeedOperatorSetField(op_diff, "qdata", elem_restr_qd_i, CEED_BASIS_COLLOCATED, q_data);
8296461910SJeremy L Thompson   CeedOperatorSetField(op_diff, "u", elem_restr_u, basis_u, CEED_VECTOR_ACTIVE);
8396461910SJeremy L Thompson   CeedOperatorSetField(op_diff, "v", elem_restr_u, basis_u, CEED_VECTOR_ACTIVE);
8496461910SJeremy L Thompson 
8596461910SJeremy L Thompson   // Apply Setup Operator
8696461910SJeremy L Thompson   CeedOperatorApply(op_setup, X, q_data, CEED_REQUEST_IMMEDIATE);
8796461910SJeremy L Thompson 
8896461910SJeremy L Thompson   // Fuly assemble operator
8996461910SJeremy L Thompson   for (CeedInt k = 0; k < num_comp * num_comp * num_dofs * num_dofs; k++) {
9096461910SJeremy L Thompson     assembled[k]      = 0.0;
9196461910SJeremy L Thompson     assembled_true[k] = 0.0;
9296461910SJeremy L Thompson   }
9396461910SJeremy L Thompson   CeedSize   nentries;
9496461910SJeremy L Thompson   CeedInt   *rows;
9596461910SJeremy L Thompson   CeedInt   *cols;
9696461910SJeremy L Thompson   CeedVector values;
9796461910SJeremy L Thompson   CeedOperatorLinearAssembleSymbolic(op_diff, &nentries, &rows, &cols);
9896461910SJeremy L Thompson   CeedVectorCreate(ceed, nentries, &values);
9996461910SJeremy L Thompson   CeedOperatorLinearAssemble(op_diff, values);
10096461910SJeremy L Thompson   const CeedScalar *vals;
10196461910SJeremy L Thompson   CeedVectorGetArrayRead(values, CEED_MEM_HOST, &vals);
102*2b730f8bSJeremy L Thompson   for (CeedInt k = 0; k < nentries; k++) assembled[rows[k] * num_comp * num_dofs + cols[k]] += vals[k];
10396461910SJeremy L Thompson   CeedVectorRestoreArrayRead(values, &vals);
10496461910SJeremy L Thompson 
10596461910SJeremy L Thompson   // Manually assemble operator
10696461910SJeremy L Thompson   CeedVectorCreate(ceed, num_comp * num_dofs, &U);
10796461910SJeremy L Thompson   CeedVectorSetValue(U, 0.0);
10896461910SJeremy L Thompson   CeedVectorCreate(ceed, num_comp * num_dofs, &V);
10996461910SJeremy L Thompson   CeedInt indOld = -1;
11096461910SJeremy L Thompson 
11196461910SJeremy L Thompson   for (CeedInt comp_in = 0; comp_in < num_comp; comp_in++) {
11296461910SJeremy L Thompson     for (CeedInt node_in = 0; node_in < num_dofs; node_in++) {
11396461910SJeremy L Thompson       // Set input
11496461910SJeremy L Thompson       CeedVectorGetArray(U, CEED_MEM_HOST, &u);
11596461910SJeremy L Thompson       CeedInt ind = node_in + comp_in * num_dofs;
11696461910SJeremy L Thompson       u[ind]      = 1.0;
117*2b730f8bSJeremy L Thompson       if (ind > 0) u[indOld] = 0.0;
11896461910SJeremy L Thompson       indOld = ind;
11996461910SJeremy L Thompson       CeedVectorRestoreArray(U, &u);
12096461910SJeremy L Thompson 
12196461910SJeremy L Thompson       // Compute effect of DoF j
12296461910SJeremy L Thompson       CeedOperatorApply(op_diff, U, V, CEED_REQUEST_IMMEDIATE);
12396461910SJeremy L Thompson 
12496461910SJeremy L Thompson       CeedVectorGetArrayRead(V, CEED_MEM_HOST, &v);
125*2b730f8bSJeremy L Thompson       for (CeedInt k = 0; k < num_dofs * num_comp; k++) assembled_true[k * num_dofs * num_comp + ind] = v[k];
12696461910SJeremy L Thompson       CeedVectorRestoreArrayRead(V, &v);
12796461910SJeremy L Thompson     }
12896461910SJeremy L Thompson   }
12996461910SJeremy L Thompson 
13096461910SJeremy L Thompson   // Check output
13196461910SJeremy L Thompson   for (CeedInt node_in = 0; node_in < num_dofs; node_in++) {
13296461910SJeremy L Thompson     for (CeedInt comp_in = 0; comp_in < num_comp; comp_in++) {
13396461910SJeremy L Thompson       for (CeedInt node_out = 0; node_out < num_dofs; node_out++) {
13496461910SJeremy L Thompson         for (CeedInt comp_out = 0; comp_out < num_comp; comp_out++) {
135*2b730f8bSJeremy L Thompson           const CeedInt    index                = (node_out + comp_out * num_dofs) * num_comp + node_in + comp_in * num_dofs;
13696461910SJeremy L Thompson           const CeedScalar assembled_value      = assembled[index];
13796461910SJeremy L Thompson           const CeedScalar assembled_true_value = assembled_true[index];
138*2b730f8bSJeremy L Thompson           if (fabs(assembled_value - assembled_true_value) > 100. * CEED_EPSILON) {
13996461910SJeremy L Thompson             // LCOV_EXCL_START
140*2b730f8bSJeremy L Thompson             printf("[(%" CeedInt_FMT ", %" CeedInt_FMT "), (%" CeedInt_FMT ", %" CeedInt_FMT ")] Error in assembly: %f != %f\n", node_out, comp_out,
141*2b730f8bSJeremy L Thompson                    node_in, comp_in, assembled_value, assembled_true_value);
14296461910SJeremy L Thompson             // LCOV_EXCL_STOP
14396461910SJeremy L Thompson           }
14496461910SJeremy L Thompson         }
14596461910SJeremy L Thompson       }
14696461910SJeremy L Thompson     }
147*2b730f8bSJeremy L Thompson   }
14896461910SJeremy L Thompson 
14996461910SJeremy L Thompson   // Cleanup
15096461910SJeremy L Thompson   free(rows);
15196461910SJeremy L Thompson   free(cols);
15296461910SJeremy L Thompson   CeedVectorDestroy(&values);
15396461910SJeremy L Thompson   CeedQFunctionDestroy(&qf_setup);
15496461910SJeremy L Thompson   CeedQFunctionDestroy(&qf_diff);
15596461910SJeremy L Thompson   CeedOperatorDestroy(&op_setup);
15696461910SJeremy L Thompson   CeedOperatorDestroy(&op_diff);
15796461910SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restr_u);
15896461910SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restr_x);
15996461910SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restr_qd_i);
16096461910SJeremy L Thompson   CeedBasisDestroy(&basis_u);
16196461910SJeremy L Thompson   CeedBasisDestroy(&basis_x);
16296461910SJeremy L Thompson   CeedVectorDestroy(&X);
16396461910SJeremy L Thompson   CeedVectorDestroy(&q_data);
16496461910SJeremy L Thompson   CeedVectorDestroy(&U);
16596461910SJeremy L Thompson   CeedVectorDestroy(&V);
16696461910SJeremy L Thompson   CeedDestroy(&ceed);
16796461910SJeremy L Thompson   return 0;
16896461910SJeremy L Thompson }
169