173d26085Sjeremylt /// @file
2361afa9aSjeremylt /// Test creation, action, and destruction for mass matrix operator with multiple components
3361afa9aSjeremylt /// \test Test creation, action, and destruction for mass matrix operator with multiple components
44d537eeaSYohann #include "t502-operator.h"
573d26085Sjeremylt
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
main(int argc,char ** argv)1173d26085Sjeremylt int main(int argc, char **argv) {
1273d26085Sjeremylt Ceed ceed;
134fee36f0SJeremy L Thompson CeedElemRestriction elem_restriction_x, elem_restriction_u, elem_restriction_q_data;
14d1d35e2fSjeremylt CeedBasis basis_x, basis_u;
1573d26085Sjeremylt CeedQFunction qf_setup, qf_mass;
1673d26085Sjeremylt CeedOperator op_setup, op_mass;
174fee36f0SJeremy L Thompson CeedVector q_data, x, u, v;
184fee36f0SJeremy L Thompson CeedInt num_elem = 15, p = 5, q = 8;
194fee36f0SJeremy L Thompson CeedInt num_nodes_x = num_elem + 1, num_nodes_u = num_elem * (p - 1) + 1;
204fee36f0SJeremy L Thompson CeedInt ind_x[num_elem * 2], ind_u[num_elem * p];
2173d26085Sjeremylt
2273d26085Sjeremylt CeedInit(argv[1], &ceed);
23288c0443SJeremy L Thompson
244fee36f0SJeremy L Thompson CeedVectorCreate(ceed, num_nodes_x, &x);
254fee36f0SJeremy L Thompson {
264fee36f0SJeremy L Thompson CeedScalar x_array[num_nodes_x];
274fee36f0SJeremy L Thompson
284fee36f0SJeremy L Thompson for (CeedInt i = 0; i < num_nodes_x; i++) x_array[i] = (CeedScalar)i / (num_nodes_x - 1);
294fee36f0SJeremy L Thompson CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, x_array);
304fee36f0SJeremy L Thompson }
314fee36f0SJeremy L Thompson CeedVectorCreate(ceed, 2 * num_nodes_u, &u);
324fee36f0SJeremy L Thompson CeedVectorCreate(ceed, 2 * num_nodes_u, &v);
334fee36f0SJeremy L Thompson CeedVectorCreate(ceed, num_elem * q, &q_data);
344fee36f0SJeremy L Thompson
354fee36f0SJeremy L Thompson // Restrictions
36d1d35e2fSjeremylt for (CeedInt i = 0; i < num_elem; i++) {
37d1d35e2fSjeremylt ind_x[2 * i + 0] = i;
38d1d35e2fSjeremylt ind_x[2 * i + 1] = i + 1;
3973d26085Sjeremylt }
404fee36f0SJeremy L Thompson CeedElemRestrictionCreate(ceed, num_elem, 2, 1, 1, num_nodes_x, CEED_MEM_HOST, CEED_USE_POINTER, ind_x, &elem_restriction_x);
4173d26085Sjeremylt
42d1d35e2fSjeremylt for (CeedInt i = 0; i < num_elem; i++) {
434fee36f0SJeremy L Thompson for (CeedInt j = 0; j < p; j++) {
444fee36f0SJeremy L Thompson ind_u[p * i + j] = 2 * (i * (p - 1) + j);
4573d26085Sjeremylt }
4673d26085Sjeremylt }
474fee36f0SJeremy L Thompson CeedElemRestrictionCreate(ceed, num_elem, p, 2, 1, 2 * num_nodes_u, CEED_MEM_HOST, CEED_USE_POINTER, ind_u, &elem_restriction_u);
484fee36f0SJeremy L Thompson
494fee36f0SJeremy L Thompson CeedInt strides_q_data[3] = {1, q, q};
504fee36f0SJeremy L Thompson CeedElemRestrictionCreateStrided(ceed, num_elem, q, 1, q * num_elem, strides_q_data, &elem_restriction_q_data);
5173d26085Sjeremylt
5273d26085Sjeremylt // Bases
534fee36f0SJeremy L Thompson CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, 2, q, CEED_GAUSS, &basis_x);
544fee36f0SJeremy L Thompson CeedBasisCreateTensorH1Lagrange(ceed, 1, 2, p, q, CEED_GAUSS, &basis_u);
5573d26085Sjeremylt
5673d26085Sjeremylt // QFunctions
574d537eeaSYohann CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup);
58a61c78d6SJeremy L Thompson CeedQFunctionAddInput(qf_setup, "weight", 1, CEED_EVAL_WEIGHT);
594d537eeaSYohann CeedQFunctionAddInput(qf_setup, "dx", 1 * 1, CEED_EVAL_GRAD);
6073d26085Sjeremylt CeedQFunctionAddOutput(qf_setup, "rho", 1, CEED_EVAL_NONE);
6173d26085Sjeremylt
624d537eeaSYohann CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass);
6373d26085Sjeremylt CeedQFunctionAddInput(qf_mass, "rho", 1, CEED_EVAL_NONE);
6473d26085Sjeremylt CeedQFunctionAddInput(qf_mass, "u", 2, CEED_EVAL_INTERP);
6573d26085Sjeremylt CeedQFunctionAddOutput(qf_mass, "v", 2, CEED_EVAL_INTERP);
6673d26085Sjeremylt
6773d26085Sjeremylt // Operators
682b730f8bSJeremy L Thompson CeedOperatorCreate(ceed, qf_setup, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_setup);
694fee36f0SJeremy L Thompson CeedOperatorSetField(op_setup, "weight", CEED_ELEMRESTRICTION_NONE, basis_x, CEED_VECTOR_NONE);
704fee36f0SJeremy L Thompson CeedOperatorSetField(op_setup, "dx", elem_restriction_x, basis_x, CEED_VECTOR_ACTIVE);
71*356036faSJeremy L Thompson CeedOperatorSetField(op_setup, "rho", elem_restriction_q_data, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE);
7273d26085Sjeremylt
732b730f8bSJeremy L Thompson CeedOperatorCreate(ceed, qf_mass, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_mass);
74*356036faSJeremy L Thompson CeedOperatorSetField(op_mass, "rho", elem_restriction_q_data, CEED_BASIS_NONE, q_data);
754fee36f0SJeremy L Thompson CeedOperatorSetField(op_mass, "u", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
764fee36f0SJeremy L Thompson CeedOperatorSetField(op_mass, "v", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
7773d26085Sjeremylt
784fee36f0SJeremy L Thompson CeedOperatorApply(op_setup, x, q_data, CEED_REQUEST_IMMEDIATE);
7973d26085Sjeremylt
804fee36f0SJeremy L Thompson {
814fee36f0SJeremy L Thompson CeedScalar *u_array;
8273d26085Sjeremylt
834fee36f0SJeremy L Thompson CeedVectorGetArrayWrite(u, CEED_MEM_HOST, &u_array);
84d1d35e2fSjeremylt for (int i = 0; i < num_nodes_u; i++) {
854fee36f0SJeremy L Thompson u_array[2 * i] = 1.0;
864fee36f0SJeremy L Thompson u_array[2 * i + 1] = 2.0;
8773d26085Sjeremylt }
884fee36f0SJeremy L Thompson CeedVectorRestoreArray(u, &u_array);
894fee36f0SJeremy L Thompson }
904fee36f0SJeremy L Thompson CeedOperatorApply(op_mass, u, v, CEED_REQUEST_IMMEDIATE);
9173d26085Sjeremylt
9273d26085Sjeremylt // Check output
934fee36f0SJeremy L Thompson {
944fee36f0SJeremy L Thompson const CeedScalar *v_array;
954fee36f0SJeremy L Thompson CeedScalar sum_1 = 0., sum_2 = 0.;
964fee36f0SJeremy L Thompson
974fee36f0SJeremy L Thompson CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
98d1d35e2fSjeremylt for (CeedInt i = 0; i < num_nodes_u; i++) {
994fee36f0SJeremy L Thompson sum_1 += v_array[2 * i];
1004fee36f0SJeremy L Thompson sum_2 += v_array[2 * i + 1];
10173d26085Sjeremylt }
1024fee36f0SJeremy L Thompson CeedVectorRestoreArrayRead(v, &v_array);
1032b730f8bSJeremy L Thompson if (fabs(sum_1 - 1.) > 1000. * CEED_EPSILON) printf("Computed Area: %f != True Area: 1.0\n", sum_1);
1042b730f8bSJeremy L Thompson if (fabs(sum_2 - 2.) > 1000. * CEED_EPSILON) printf("Computed Area: %f != True Area: 2.0\n", sum_2);
1054fee36f0SJeremy L Thompson }
10673d26085Sjeremylt
1074fee36f0SJeremy L Thompson CeedVectorDestroy(&x);
1084fee36f0SJeremy L Thompson CeedVectorDestroy(&u);
1094fee36f0SJeremy L Thompson CeedVectorDestroy(&v);
1104fee36f0SJeremy L Thompson CeedVectorDestroy(&q_data);
1114fee36f0SJeremy L Thompson CeedElemRestrictionDestroy(&elem_restriction_u);
1124fee36f0SJeremy L Thompson CeedElemRestrictionDestroy(&elem_restriction_x);
1134fee36f0SJeremy L Thompson CeedElemRestrictionDestroy(&elem_restriction_q_data);
1144fee36f0SJeremy L Thompson CeedBasisDestroy(&basis_u);
1154fee36f0SJeremy L Thompson CeedBasisDestroy(&basis_x);
11673d26085Sjeremylt CeedQFunctionDestroy(&qf_setup);
11773d26085Sjeremylt CeedQFunctionDestroy(&qf_mass);
11873d26085Sjeremylt CeedOperatorDestroy(&op_setup);
11973d26085Sjeremylt CeedOperatorDestroy(&op_mass);
12073d26085Sjeremylt CeedDestroy(&ceed);
12173d26085Sjeremylt return 0;
12273d26085Sjeremylt }
123