xref: /libCEED/tests/t510-operator.c (revision 4fee36f0a30516a0b5ad51bf7eb3b32d83efd623)
1a8de75f0Sjeremylt /// @file
231e5b035Sjeremylt /// Test creation, action, and destruction for mass matrix operator
331e5b035Sjeremylt /// \test Test creation, action, and destruction for mass matrix operator
44d537eeaSYohann #include "t510-operator.h"
5a8de75f0Sjeremylt 
62b730f8bSJeremy L Thompson #include <ceed.h>
72b730f8bSJeremy L Thompson #include <math.h>
82b730f8bSJeremy L Thompson #include <stdlib.h>
92b730f8bSJeremy L Thompson 
102b730f8bSJeremy L Thompson #include "t320-basis.h"
112b730f8bSJeremy L Thompson 
12a8de75f0Sjeremylt int main(int argc, char **argv) {
13a8de75f0Sjeremylt   Ceed                ceed;
14*4fee36f0SJeremy L Thompson   CeedElemRestriction elem_restriction_x, elem_restriction_u, elem_restriction_q_data;
15d1d35e2fSjeremylt   CeedBasis           basis_x, basis_u;
16a8de75f0Sjeremylt   CeedQFunction       qf_setup, qf_mass;
17a8de75f0Sjeremylt   CeedOperator        op_setup, op_mass;
18*4fee36f0SJeremy L Thompson   CeedVector          q_data, x, u, v;
19*4fee36f0SJeremy L Thompson   CeedInt             num_elem = 12, dim = 2, p = 6, q = 4;
20a8de75f0Sjeremylt   CeedInt             nx = 3, ny = 2;
21a8de75f0Sjeremylt   CeedInt             row, col, offset;
22*4fee36f0SJeremy L Thompson   CeedInt             num_dofs = (nx * 2 + 1) * (ny * 2 + 1), num_qpts = num_elem * q;
23*4fee36f0SJeremy L Thompson   CeedInt             ind_x[num_elem * p];
24*4fee36f0SJeremy L Thompson   CeedScalar          q_ref[dim * q], q_weight[q];
25*4fee36f0SJeremy L Thompson   CeedScalar          interp[p * q], grad[dim * p * q];
26a8de75f0Sjeremylt 
27a8de75f0Sjeremylt   CeedInit(argv[1], &ceed);
28a8de75f0Sjeremylt 
29*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, dim * num_dofs, &x);
30*4fee36f0SJeremy L Thompson   {
31*4fee36f0SJeremy L Thompson     CeedScalar x_array[dim * num_dofs];
32*4fee36f0SJeremy L Thompson 
33d1d35e2fSjeremylt     for (CeedInt i = 0; i < num_dofs; i++) {
34*4fee36f0SJeremy L Thompson       x_array[i]            = (1. / (nx * 2)) * (CeedScalar)(i % (nx * 2 + 1));
35*4fee36f0SJeremy L Thompson       x_array[i + num_dofs] = (1. / (ny * 2)) * (CeedScalar)(i / (nx * 2 + 1));
36a8de75f0Sjeremylt     }
37*4fee36f0SJeremy L Thompson     CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, x_array);
38*4fee36f0SJeremy L Thompson   }
39*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_qpts, &q_data);
40*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_dofs, &u);
41*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_dofs, &v);
42*4fee36f0SJeremy L Thompson 
43*4fee36f0SJeremy L Thompson   // Restrictions
44d1d35e2fSjeremylt   for (CeedInt i = 0; i < num_elem / 2; i++) {
45a8de75f0Sjeremylt     col    = i % nx;
46a8de75f0Sjeremylt     row    = i / nx;
47a8de75f0Sjeremylt     offset = col * 2 + row * (nx * 2 + 1) * 2;
48a8de75f0Sjeremylt 
49*4fee36f0SJeremy L Thompson     ind_x[i * 2 * p + 0] = 2 + offset;
50*4fee36f0SJeremy L Thompson     ind_x[i * 2 * p + 1] = 9 + offset;
51*4fee36f0SJeremy L Thompson     ind_x[i * 2 * p + 2] = 16 + offset;
52*4fee36f0SJeremy L Thompson     ind_x[i * 2 * p + 3] = 1 + offset;
53*4fee36f0SJeremy L Thompson     ind_x[i * 2 * p + 4] = 8 + offset;
54*4fee36f0SJeremy L Thompson     ind_x[i * 2 * p + 5] = 0 + offset;
55a8de75f0Sjeremylt 
56*4fee36f0SJeremy L Thompson     ind_x[i * 2 * p + 6]  = 14 + offset;
57*4fee36f0SJeremy L Thompson     ind_x[i * 2 * p + 7]  = 7 + offset;
58*4fee36f0SJeremy L Thompson     ind_x[i * 2 * p + 8]  = 0 + offset;
59*4fee36f0SJeremy L Thompson     ind_x[i * 2 * p + 9]  = 15 + offset;
60*4fee36f0SJeremy L Thompson     ind_x[i * 2 * p + 10] = 8 + offset;
61*4fee36f0SJeremy L Thompson     ind_x[i * 2 * p + 11] = 16 + offset;
62a8de75f0Sjeremylt   }
63*4fee36f0SJeremy L Thompson   CeedElemRestrictionCreate(ceed, num_elem, p, dim, num_dofs, dim * num_dofs, CEED_MEM_HOST, CEED_USE_POINTER, ind_x, &elem_restriction_x);
64*4fee36f0SJeremy L Thompson   CeedElemRestrictionCreate(ceed, num_elem, p, 1, 1, num_dofs, CEED_MEM_HOST, CEED_USE_POINTER, ind_x, &elem_restriction_u);
65a8de75f0Sjeremylt 
66*4fee36f0SJeremy L Thompson   CeedInt strides_q_data[3] = {1, q, q};
67*4fee36f0SJeremy L Thompson   CeedElemRestrictionCreateStrided(ceed, num_elem, q, 1, num_qpts, strides_q_data, &elem_restriction_q_data);
68a8de75f0Sjeremylt 
69a8de75f0Sjeremylt   // Bases
70*4fee36f0SJeremy L Thompson   Build2DSimplex(q_ref, q_weight, interp, grad);
71*4fee36f0SJeremy L Thompson   CeedBasisCreateH1(ceed, CEED_TOPOLOGY_TRIANGLE, dim, p, q, interp, grad, q_ref, q_weight, &basis_x);
72a8de75f0Sjeremylt 
73*4fee36f0SJeremy L Thompson   Build2DSimplex(q_ref, q_weight, interp, grad);
74*4fee36f0SJeremy L Thompson   CeedBasisCreateH1(ceed, CEED_TOPOLOGY_TRIANGLE, 1, p, q, interp, grad, q_ref, q_weight, &basis_u);
75a8de75f0Sjeremylt 
76a8de75f0Sjeremylt   // QFunctions
774d537eeaSYohann   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup);
78a61c78d6SJeremy L Thompson   CeedQFunctionAddInput(qf_setup, "weight", 1, CEED_EVAL_WEIGHT);
794d537eeaSYohann   CeedQFunctionAddInput(qf_setup, "dx", dim * dim, CEED_EVAL_GRAD);
80a8de75f0Sjeremylt   CeedQFunctionAddOutput(qf_setup, "rho", 1, CEED_EVAL_NONE);
81a8de75f0Sjeremylt 
824d537eeaSYohann   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass);
83a8de75f0Sjeremylt   CeedQFunctionAddInput(qf_mass, "rho", 1, CEED_EVAL_NONE);
84a8de75f0Sjeremylt   CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP);
85a8de75f0Sjeremylt   CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP);
86a8de75f0Sjeremylt 
87a8de75f0Sjeremylt   // Operators
882b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_setup, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_setup);
89*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_setup, "weight", CEED_ELEMRESTRICTION_NONE, basis_x, CEED_VECTOR_NONE);
90*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_setup, "dx", elem_restriction_x, basis_x, CEED_VECTOR_ACTIVE);
91*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_setup, "rho", elem_restriction_q_data, CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
92a8de75f0Sjeremylt 
932b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_mass, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_mass);
94*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass, "rho", elem_restriction_q_data, CEED_BASIS_COLLOCATED, q_data);
95*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass, "u", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
96*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass, "v", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
97a8de75f0Sjeremylt 
98*4fee36f0SJeremy L Thompson   CeedOperatorApply(op_setup, x, q_data, CEED_REQUEST_IMMEDIATE);
99a8de75f0Sjeremylt 
100*4fee36f0SJeremy L Thompson   CeedVectorSetValue(u, 0.0);
101*4fee36f0SJeremy L Thompson   CeedOperatorApply(op_mass, u, v, CEED_REQUEST_IMMEDIATE);
102a8de75f0Sjeremylt 
103a8de75f0Sjeremylt   // Check output
104*4fee36f0SJeremy L Thompson   {
105*4fee36f0SJeremy L Thompson     const CeedScalar *v_array;
106a8de75f0Sjeremylt 
107*4fee36f0SJeremy L Thompson     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
108*4fee36f0SJeremy L Thompson     for (CeedInt i = 0; i < num_dofs; i++) {
109*4fee36f0SJeremy L Thompson       if (fabs(v_array[i]) > 1e-14) printf("[%" CeedInt_FMT "] v %g != 0.0\n", i, v_array[i]);
110*4fee36f0SJeremy L Thompson     }
111*4fee36f0SJeremy L Thompson     CeedVectorRestoreArrayRead(v, &v_array);
112*4fee36f0SJeremy L Thompson   }
113*4fee36f0SJeremy L Thompson 
114*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&x);
115*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&u);
116*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&v);
117*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&q_data);
118*4fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_u);
119*4fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_x);
120*4fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_q_data);
121*4fee36f0SJeremy L Thompson   CeedBasisDestroy(&basis_u);
122*4fee36f0SJeremy L Thompson   CeedBasisDestroy(&basis_x);
123a8de75f0Sjeremylt   CeedQFunctionDestroy(&qf_setup);
124a8de75f0Sjeremylt   CeedQFunctionDestroy(&qf_mass);
125a8de75f0Sjeremylt   CeedOperatorDestroy(&op_setup);
126a8de75f0Sjeremylt   CeedOperatorDestroy(&op_mass);
127a8de75f0Sjeremylt   CeedDestroy(&ceed);
128a8de75f0Sjeremylt   return 0;
129a8de75f0Sjeremylt }
130