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>
849aac155SJeremy L Thompson #include <stdio.h>
92b730f8bSJeremy L Thompson #include <stdlib.h>
102b730f8bSJeremy L Thompson
112b730f8bSJeremy L Thompson #include "t320-basis.h"
122b730f8bSJeremy L Thompson
main(int argc,char ** argv)13a8de75f0Sjeremylt int main(int argc, char **argv) {
14a8de75f0Sjeremylt Ceed ceed;
154fee36f0SJeremy L Thompson CeedElemRestriction elem_restriction_x, elem_restriction_u, elem_restriction_q_data;
16d1d35e2fSjeremylt CeedBasis basis_x, basis_u;
17a8de75f0Sjeremylt CeedQFunction qf_setup, qf_mass;
18a8de75f0Sjeremylt CeedOperator op_setup, op_mass;
194fee36f0SJeremy L Thompson CeedVector q_data, x, u, v;
204fee36f0SJeremy L Thompson CeedInt num_elem = 12, dim = 2, p = 6, q = 4;
21a8de75f0Sjeremylt CeedInt nx = 3, ny = 2;
22a8de75f0Sjeremylt CeedInt row, col, offset;
234fee36f0SJeremy L Thompson CeedInt num_dofs = (nx * 2 + 1) * (ny * 2 + 1), num_qpts = num_elem * q;
244fee36f0SJeremy L Thompson CeedInt ind_x[num_elem * p];
254fee36f0SJeremy L Thompson CeedScalar q_ref[dim * q], q_weight[q];
264fee36f0SJeremy L Thompson CeedScalar interp[p * q], grad[dim * p * q];
27a8de75f0Sjeremylt
28a8de75f0Sjeremylt CeedInit(argv[1], &ceed);
29a8de75f0Sjeremylt
304fee36f0SJeremy L Thompson CeedVectorCreate(ceed, dim * num_dofs, &x);
314fee36f0SJeremy L Thompson {
324fee36f0SJeremy L Thompson CeedScalar x_array[dim * num_dofs];
334fee36f0SJeremy L Thompson
34d1d35e2fSjeremylt for (CeedInt i = 0; i < num_dofs; i++) {
354fee36f0SJeremy L Thompson x_array[i] = (1. / (nx * 2)) * (CeedScalar)(i % (nx * 2 + 1));
364fee36f0SJeremy L Thompson x_array[i + num_dofs] = (1. / (ny * 2)) * (CeedScalar)(i / (nx * 2 + 1));
37a8de75f0Sjeremylt }
384fee36f0SJeremy L Thompson CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, x_array);
394fee36f0SJeremy L Thompson }
404fee36f0SJeremy L Thompson CeedVectorCreate(ceed, num_qpts, &q_data);
414fee36f0SJeremy L Thompson CeedVectorCreate(ceed, num_dofs, &u);
424fee36f0SJeremy L Thompson CeedVectorCreate(ceed, num_dofs, &v);
434fee36f0SJeremy L Thompson
444fee36f0SJeremy L Thompson // Restrictions
45d1d35e2fSjeremylt for (CeedInt i = 0; i < num_elem / 2; i++) {
46a8de75f0Sjeremylt col = i % nx;
47a8de75f0Sjeremylt row = i / nx;
48a8de75f0Sjeremylt offset = col * 2 + row * (nx * 2 + 1) * 2;
49a8de75f0Sjeremylt
504fee36f0SJeremy L Thompson ind_x[i * 2 * p + 0] = 2 + offset;
514fee36f0SJeremy L Thompson ind_x[i * 2 * p + 1] = 9 + offset;
524fee36f0SJeremy L Thompson ind_x[i * 2 * p + 2] = 16 + offset;
534fee36f0SJeremy L Thompson ind_x[i * 2 * p + 3] = 1 + offset;
544fee36f0SJeremy L Thompson ind_x[i * 2 * p + 4] = 8 + offset;
554fee36f0SJeremy L Thompson ind_x[i * 2 * p + 5] = 0 + offset;
56a8de75f0Sjeremylt
574fee36f0SJeremy L Thompson ind_x[i * 2 * p + 6] = 14 + offset;
584fee36f0SJeremy L Thompson ind_x[i * 2 * p + 7] = 7 + offset;
594fee36f0SJeremy L Thompson ind_x[i * 2 * p + 8] = 0 + offset;
604fee36f0SJeremy L Thompson ind_x[i * 2 * p + 9] = 15 + offset;
614fee36f0SJeremy L Thompson ind_x[i * 2 * p + 10] = 8 + offset;
624fee36f0SJeremy L Thompson ind_x[i * 2 * p + 11] = 16 + offset;
63a8de75f0Sjeremylt }
644fee36f0SJeremy L Thompson CeedElemRestrictionCreate(ceed, num_elem, p, dim, num_dofs, dim * num_dofs, CEED_MEM_HOST, CEED_USE_POINTER, ind_x, &elem_restriction_x);
654fee36f0SJeremy L Thompson CeedElemRestrictionCreate(ceed, num_elem, p, 1, 1, num_dofs, CEED_MEM_HOST, CEED_USE_POINTER, ind_x, &elem_restriction_u);
66a8de75f0Sjeremylt
674fee36f0SJeremy L Thompson CeedInt strides_q_data[3] = {1, q, q};
684fee36f0SJeremy L Thompson CeedElemRestrictionCreateStrided(ceed, num_elem, q, 1, num_qpts, strides_q_data, &elem_restriction_q_data);
69a8de75f0Sjeremylt
70a8de75f0Sjeremylt // Bases
714fee36f0SJeremy L Thompson Build2DSimplex(q_ref, q_weight, interp, grad);
724fee36f0SJeremy L Thompson CeedBasisCreateH1(ceed, CEED_TOPOLOGY_TRIANGLE, dim, p, q, interp, grad, q_ref, q_weight, &basis_x);
73a8de75f0Sjeremylt
744fee36f0SJeremy L Thompson Build2DSimplex(q_ref, q_weight, interp, grad);
754fee36f0SJeremy L Thompson CeedBasisCreateH1(ceed, CEED_TOPOLOGY_TRIANGLE, 1, p, q, interp, grad, q_ref, q_weight, &basis_u);
76a8de75f0Sjeremylt
77a8de75f0Sjeremylt // QFunctions
784d537eeaSYohann CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup);
79a61c78d6SJeremy L Thompson CeedQFunctionAddInput(qf_setup, "weight", 1, CEED_EVAL_WEIGHT);
804d537eeaSYohann CeedQFunctionAddInput(qf_setup, "dx", dim * dim, CEED_EVAL_GRAD);
81a8de75f0Sjeremylt CeedQFunctionAddOutput(qf_setup, "rho", 1, CEED_EVAL_NONE);
82a8de75f0Sjeremylt
834d537eeaSYohann CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass);
84a8de75f0Sjeremylt CeedQFunctionAddInput(qf_mass, "rho", 1, CEED_EVAL_NONE);
85a8de75f0Sjeremylt CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP);
86a8de75f0Sjeremylt CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP);
87a8de75f0Sjeremylt
88a8de75f0Sjeremylt // Operators
892b730f8bSJeremy L Thompson CeedOperatorCreate(ceed, qf_setup, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_setup);
904fee36f0SJeremy L Thompson CeedOperatorSetField(op_setup, "weight", CEED_ELEMRESTRICTION_NONE, basis_x, CEED_VECTOR_NONE);
914fee36f0SJeremy L Thompson CeedOperatorSetField(op_setup, "dx", elem_restriction_x, basis_x, CEED_VECTOR_ACTIVE);
92*356036faSJeremy L Thompson CeedOperatorSetField(op_setup, "rho", elem_restriction_q_data, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE);
93a8de75f0Sjeremylt
942b730f8bSJeremy L Thompson CeedOperatorCreate(ceed, qf_mass, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_mass);
95*356036faSJeremy L Thompson CeedOperatorSetField(op_mass, "rho", elem_restriction_q_data, CEED_BASIS_NONE, q_data);
964fee36f0SJeremy L Thompson CeedOperatorSetField(op_mass, "u", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
974fee36f0SJeremy L Thompson CeedOperatorSetField(op_mass, "v", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
98a8de75f0Sjeremylt
994fee36f0SJeremy L Thompson CeedOperatorApply(op_setup, x, q_data, CEED_REQUEST_IMMEDIATE);
100a8de75f0Sjeremylt
1014fee36f0SJeremy L Thompson CeedVectorSetValue(u, 0.0);
1024fee36f0SJeremy L Thompson CeedOperatorApply(op_mass, u, v, CEED_REQUEST_IMMEDIATE);
103a8de75f0Sjeremylt
104a8de75f0Sjeremylt // Check output
1054fee36f0SJeremy L Thompson {
1064fee36f0SJeremy L Thompson const CeedScalar *v_array;
107a8de75f0Sjeremylt
1084fee36f0SJeremy L Thompson CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
1094fee36f0SJeremy L Thompson for (CeedInt i = 0; i < num_dofs; i++) {
1104fee36f0SJeremy L Thompson if (fabs(v_array[i]) > 1e-14) printf("[%" CeedInt_FMT "] v %g != 0.0\n", i, v_array[i]);
1114fee36f0SJeremy L Thompson }
1124fee36f0SJeremy L Thompson CeedVectorRestoreArrayRead(v, &v_array);
1134fee36f0SJeremy L Thompson }
1144fee36f0SJeremy L Thompson
1154fee36f0SJeremy L Thompson CeedVectorDestroy(&x);
1164fee36f0SJeremy L Thompson CeedVectorDestroy(&u);
1174fee36f0SJeremy L Thompson CeedVectorDestroy(&v);
1184fee36f0SJeremy L Thompson CeedVectorDestroy(&q_data);
1194fee36f0SJeremy L Thompson CeedElemRestrictionDestroy(&elem_restriction_u);
1204fee36f0SJeremy L Thompson CeedElemRestrictionDestroy(&elem_restriction_x);
1214fee36f0SJeremy L Thompson CeedElemRestrictionDestroy(&elem_restriction_q_data);
1224fee36f0SJeremy L Thompson CeedBasisDestroy(&basis_u);
1234fee36f0SJeremy L Thompson CeedBasisDestroy(&basis_x);
124a8de75f0Sjeremylt CeedQFunctionDestroy(&qf_setup);
125a8de75f0Sjeremylt CeedQFunctionDestroy(&qf_mass);
126a8de75f0Sjeremylt CeedOperatorDestroy(&op_setup);
127a8de75f0Sjeremylt CeedOperatorDestroy(&op_mass);
128a8de75f0Sjeremylt CeedDestroy(&ceed);
129a8de75f0Sjeremylt return 0;
130a8de75f0Sjeremylt }
131