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