xref: /libCEED/tests/t537-operator.c (revision 2b730f8b5a9c809740a0b3b302db43a719c636b1)
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 
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 
10d965c7a7SJeremy L Thompson int main(int argc, char **argv) {
11d965c7a7SJeremy L Thompson   Ceed                ceed;
12*2b730f8bSJeremy L Thompson   CeedElemRestriction elem_restr_x, elem_restr_u, elem_restr_qd_i;
13d1d35e2fSjeremylt   CeedBasis           basis_x, basis_u;
14d965c7a7SJeremy L Thompson   CeedQFunction       qf_setup, qf_mass;
15d965c7a7SJeremy L Thompson   CeedOperator        op_setup, op_mass;
16d1d35e2fSjeremylt   CeedVector          q_data, X, A, U, V;
17d1d35e2fSjeremylt   CeedInt             num_elem = 6, P = 3, Q = 4, dim = 2, num_comp = 2;
18d965c7a7SJeremy L Thompson   CeedInt             nx = 3, ny = 2;
19d1d35e2fSjeremylt   CeedInt             num_dofs = (nx * 2 + 1) * (ny * 2 + 1), num_qpts = num_elem * Q * Q;
20d1d35e2fSjeremylt   CeedInt             ind_x[num_elem * P * P];
21d1d35e2fSjeremylt   CeedScalar          x[dim * num_dofs], assembled_true[num_comp * num_comp * num_dofs];
22d965c7a7SJeremy L Thompson   CeedScalar         *u;
23d965c7a7SJeremy L Thompson   const CeedScalar   *a, *v;
24d965c7a7SJeremy L Thompson 
25d965c7a7SJeremy L Thompson   CeedInit(argv[1], &ceed);
26d965c7a7SJeremy L Thompson 
27d965c7a7SJeremy L Thompson   // DoF Coordinates
28*2b730f8bSJeremy L Thompson   for (CeedInt i = 0; i < nx * 2 + 1; i++) {
29d965c7a7SJeremy L Thompson     for (CeedInt j = 0; j < ny * 2 + 1; j++) {
30d1d35e2fSjeremylt       x[i + j * (nx * 2 + 1) + 0 * num_dofs] = (CeedScalar)i / (2 * nx);
31d1d35e2fSjeremylt       x[i + j * (nx * 2 + 1) + 1 * num_dofs] = (CeedScalar)j / (2 * ny);
32d965c7a7SJeremy L Thompson     }
33*2b730f8bSJeremy L Thompson   }
34d1d35e2fSjeremylt   CeedVectorCreate(ceed, dim * num_dofs, &X);
35d965c7a7SJeremy L Thompson   CeedVectorSetArray(X, CEED_MEM_HOST, CEED_USE_POINTER, x);
36d965c7a7SJeremy L Thompson 
37d965c7a7SJeremy L Thompson   // Qdata Vector
38d1d35e2fSjeremylt   CeedVectorCreate(ceed, num_qpts, &q_data);
39d965c7a7SJeremy L Thompson 
40d965c7a7SJeremy L Thompson   // Element Setup
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;
45d965c7a7SJeremy L Thompson     offset = col * (P - 1) + row * (nx * 2 + 1) * (P - 1);
46*2b730f8bSJeremy L Thompson     for (CeedInt j = 0; j < P; j++) {
47*2b730f8bSJeremy L Thompson       for (CeedInt k = 0; k < P; k++) ind_x[P * (P * i + k) + j] = offset + k * (nx * 2 + 1) + j;
48*2b730f8bSJeremy L Thompson     }
49d965c7a7SJeremy L Thompson   }
50d965c7a7SJeremy L Thompson 
51d965c7a7SJeremy L Thompson   // Restrictions
52*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);
53*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);
54d1d35e2fSjeremylt   CeedInt strides_qd[3] = {1, Q * Q, Q * Q};
55*2b730f8bSJeremy L Thompson   CeedElemRestrictionCreateStrided(ceed, num_elem, Q * Q, 1, num_qpts, strides_qd, &elem_restr_qd_i);
56d965c7a7SJeremy L Thompson 
57d965c7a7SJeremy L Thompson   // Bases
58d1d35e2fSjeremylt   CeedBasisCreateTensorH1Lagrange(ceed, dim, dim, P, Q, CEED_GAUSS, &basis_x);
59*2b730f8bSJeremy 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
73*2b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_setup, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_setup);
74*2b730f8bSJeremy L Thompson   CeedOperatorSetField(op_setup, "weight", CEED_ELEMRESTRICTION_NONE, basis_x, CEED_VECTOR_NONE);
75d1d35e2fSjeremylt   CeedOperatorSetField(op_setup, "dx", elem_restr_x, basis_x, CEED_VECTOR_ACTIVE);
76*2b730f8bSJeremy L Thompson   CeedOperatorSetField(op_setup, "rho", elem_restr_qd_i, CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
77d965c7a7SJeremy L Thompson 
78*2b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_mass, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_mass);
79*2b730f8bSJeremy L Thompson   CeedOperatorSetField(op_mass, "rho", elem_restr_qd_i, CEED_BASIS_COLLOCATED, q_data);
80d1d35e2fSjeremylt   CeedOperatorSetField(op_mass, "u", elem_restr_u, basis_u, CEED_VECTOR_ACTIVE);
81d1d35e2fSjeremylt   CeedOperatorSetField(op_mass, "v", elem_restr_u, basis_u, CEED_VECTOR_ACTIVE);
82d965c7a7SJeremy L Thompson 
83d965c7a7SJeremy L Thompson   // Apply Setup Operator
84d1d35e2fSjeremylt   CeedOperatorApply(op_setup, X, q_data, CEED_REQUEST_IMMEDIATE);
85d965c7a7SJeremy L Thompson 
86d965c7a7SJeremy L Thompson   // Assemble diagonal
87d1d35e2fSjeremylt   CeedVectorCreate(ceed, num_comp * num_comp * num_dofs, &A);
88*2b730f8bSJeremy L Thompson   CeedOperatorLinearAssemblePointBlockDiagonal(op_mass, A, CEED_REQUEST_IMMEDIATE);
89d965c7a7SJeremy L Thompson 
90d965c7a7SJeremy L Thompson   // Manually assemble diagonal
91d1d35e2fSjeremylt   CeedVectorCreate(ceed, num_comp * num_dofs, &U);
92d965c7a7SJeremy L Thompson   CeedVectorSetValue(U, 0.0);
93d1d35e2fSjeremylt   CeedVectorCreate(ceed, num_comp * num_dofs, &V);
94*2b730f8bSJeremy 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++) {
98d965c7a7SJeremy L Thompson       // Set input
99d965c7a7SJeremy L Thompson       CeedVectorGetArray(U, CEED_MEM_HOST, &u);
100d1d35e2fSjeremylt       CeedInt ind = i + j * num_dofs;
101d965c7a7SJeremy L Thompson       u[ind]      = 1.0;
102*2b730f8bSJeremy L Thompson       if (ind > 0) u[ind_old] = 0.0;
103d1d35e2fSjeremylt       ind_old = ind;
104d965c7a7SJeremy L Thompson       CeedVectorRestoreArray(U, &u);
105d965c7a7SJeremy L Thompson 
106d965c7a7SJeremy L Thompson       // Compute effect of DoF i, comp j
107d965c7a7SJeremy L Thompson       CeedOperatorApply(op_mass, U, V, CEED_REQUEST_IMMEDIATE);
108d965c7a7SJeremy L Thompson 
109d965c7a7SJeremy L Thompson       // Retrieve entry
110d965c7a7SJeremy L Thompson       CeedVectorGetArrayRead(V, CEED_MEM_HOST, &v);
111*2b730f8bSJeremy L Thompson       for (int k = 0; k < num_comp; k++) assembled_true[i * num_comp * num_comp + k * num_comp + j] += v[i + k * num_dofs];
112d965c7a7SJeremy L Thompson       CeedVectorRestoreArrayRead(V, &v);
113d965c7a7SJeremy L Thompson     }
114d965c7a7SJeremy L Thompson   }
115d965c7a7SJeremy L Thompson 
116d965c7a7SJeremy L Thompson   // Check output
117d965c7a7SJeremy L Thompson   CeedVectorGetArrayRead(A, CEED_MEM_HOST, &a);
118*2b730f8bSJeremy L Thompson   for (int i = 0; i < num_comp * num_comp * num_dofs; i++) {
119*2b730f8bSJeremy L Thompson     if (fabs(a[i] - assembled_true[i]) > 100. * CEED_EPSILON) printf("[%" CeedInt_FMT "] Error in assembly: %f != %f\n", i, a[i], assembled_true[i]);
120*2b730f8bSJeremy L Thompson   }
121d965c7a7SJeremy L Thompson   CeedVectorRestoreArrayRead(A, &a);
122d965c7a7SJeremy L Thompson 
123d965c7a7SJeremy L Thompson   // Cleanup
124d965c7a7SJeremy L Thompson   CeedQFunctionDestroy(&qf_setup);
125d965c7a7SJeremy L Thompson   CeedQFunctionDestroy(&qf_mass);
126d965c7a7SJeremy L Thompson   CeedOperatorDestroy(&op_setup);
127d965c7a7SJeremy L Thompson   CeedOperatorDestroy(&op_mass);
128d1d35e2fSjeremylt   CeedElemRestrictionDestroy(&elem_restr_u);
129d1d35e2fSjeremylt   CeedElemRestrictionDestroy(&elem_restr_x);
130d1d35e2fSjeremylt   CeedElemRestrictionDestroy(&elem_restr_qd_i);
131d1d35e2fSjeremylt   CeedBasisDestroy(&basis_u);
132d1d35e2fSjeremylt   CeedBasisDestroy(&basis_x);
133d965c7a7SJeremy L Thompson   CeedVectorDestroy(&X);
134d965c7a7SJeremy L Thompson   CeedVectorDestroy(&A);
135d1d35e2fSjeremylt   CeedVectorDestroy(&q_data);
136d965c7a7SJeremy L Thompson   CeedVectorDestroy(&U);
137d965c7a7SJeremy L Thompson   CeedVectorDestroy(&V);
138d965c7a7SJeremy L Thompson   CeedDestroy(&ceed);
139d965c7a7SJeremy L Thompson   return 0;
140d965c7a7SJeremy L Thompson }
141