175f0d5a4SJeremy L Thompson /// @file
275f0d5a4SJeremy L Thompson /// Test creation, action, and destruction for mass matrix composite operator with multigrid level, tensor basis and interpolation basis generation
375f0d5a4SJeremy L Thompson /// \test Test creation, action, and destruction for mass matrix composite operator with multigrid level, tensor basis and interpolation basis
475f0d5a4SJeremy L Thompson /// generation
575f0d5a4SJeremy L Thompson #include <ceed.h>
675f0d5a4SJeremy L Thompson #include <math.h>
749aac155SJeremy L Thompson #include <stdio.h>
875f0d5a4SJeremy L Thompson #include <stdlib.h>
975f0d5a4SJeremy L Thompson
1075f0d5a4SJeremy L Thompson #include "t502-operator.h"
1175f0d5a4SJeremy L Thompson
main(int argc,char ** argv)1275f0d5a4SJeremy L Thompson int main(int argc, char **argv) {
1375f0d5a4SJeremy L Thompson Ceed ceed;
144fee36f0SJeremy L Thompson CeedOperator op_mass_coarse, op_mass_fine, op_prolong, op_restrict;
154fee36f0SJeremy L Thompson CeedVector x, u_coarse, u_fine, v_coarse, v_fine, p_mult_fine;
164fee36f0SJeremy L Thompson CeedInt num_elem = 15, num_elem_sub = 5, num_sub_ops = 3, p_coarse = 3, p_fine = 5, q = 8, num_comp = 2;
174fee36f0SJeremy L Thompson CeedInt num_dofs_x = num_elem + 1, num_dofs_u_coarse = num_elem * (p_coarse - 1) + 1, num_dofs_u_fine = num_elem * (p_fine - 1) + 1;
184fee36f0SJeremy L Thompson CeedInt ind_u_coarse[num_elem_sub * p_coarse], ind_u_fine[num_elem_sub * p_fine], ind_x[num_elem_sub * 2];
1975f0d5a4SJeremy L Thompson
2075f0d5a4SJeremy L Thompson CeedInit(argv[1], &ceed);
2175f0d5a4SJeremy L Thompson
224fee36f0SJeremy L Thompson // Vectors
234fee36f0SJeremy L Thompson CeedVectorCreate(ceed, num_dofs_x, &x);
244fee36f0SJeremy L Thompson {
254fee36f0SJeremy L Thompson CeedScalar x_array[num_dofs_x];
264fee36f0SJeremy L Thompson
274fee36f0SJeremy L Thompson for (CeedInt i = 0; i < num_dofs_x; i++) x_array[i] = (CeedScalar)i / (num_dofs_x - 1);
284fee36f0SJeremy L Thompson CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, x_array);
294fee36f0SJeremy L Thompson }
304fee36f0SJeremy L Thompson CeedVectorCreate(ceed, num_comp * num_dofs_u_coarse, &u_coarse);
314fee36f0SJeremy L Thompson CeedVectorCreate(ceed, num_comp * num_dofs_u_coarse, &v_coarse);
324fee36f0SJeremy L Thompson CeedVectorCreate(ceed, num_comp * num_dofs_u_fine, &u_fine);
334fee36f0SJeremy L Thompson CeedVectorCreate(ceed, num_comp * num_dofs_u_fine, &v_fine);
344fee36f0SJeremy L Thompson
3575f0d5a4SJeremy L Thompson // Composite operators
36*ed094490SJeremy L Thompson CeedOperatorCreateComposite(ceed, &op_mass_coarse);
37*ed094490SJeremy L Thompson CeedOperatorCreateComposite(ceed, &op_mass_fine);
38*ed094490SJeremy L Thompson CeedOperatorCreateComposite(ceed, &op_prolong);
39*ed094490SJeremy L Thompson CeedOperatorCreateComposite(ceed, &op_restrict);
4075f0d5a4SJeremy L Thompson
4175f0d5a4SJeremy L Thompson // Setup fine suboperators
4275f0d5a4SJeremy L Thompson for (CeedInt i = 0; i < num_sub_ops; i++) {
4375f0d5a4SJeremy L Thompson CeedVector q_data;
444fee36f0SJeremy L Thompson CeedElemRestriction elem_restriction_x, elem_restriction_q_data, elem_restriction_u_fine;
454fee36f0SJeremy L Thompson CeedBasis basis_x, basis_u_fine;
4675f0d5a4SJeremy L Thompson CeedQFunction qf_setup, qf_mass;
474fee36f0SJeremy L Thompson CeedOperator sub_op_setup, sub_op_mass_fine;
4875f0d5a4SJeremy L Thompson
4975f0d5a4SJeremy L Thompson // -- QData
504fee36f0SJeremy L Thompson CeedVectorCreate(ceed, num_elem * q, &q_data);
5175f0d5a4SJeremy L Thompson
5275f0d5a4SJeremy L Thompson // -- Restrictions
5375f0d5a4SJeremy L Thompson CeedInt offset = num_elem_sub * i;
5475f0d5a4SJeremy L Thompson for (CeedInt j = 0; j < num_elem_sub; j++) {
5575f0d5a4SJeremy L Thompson ind_x[2 * j + 0] = offset + j;
5675f0d5a4SJeremy L Thompson ind_x[2 * j + 1] = offset + j + 1;
5775f0d5a4SJeremy L Thompson }
584fee36f0SJeremy L Thompson CeedElemRestrictionCreate(ceed, num_elem_sub, 2, 1, 1, num_dofs_x, CEED_MEM_HOST, CEED_COPY_VALUES, ind_x, &elem_restriction_x);
5975f0d5a4SJeremy L Thompson
604fee36f0SJeremy L Thompson offset = num_elem_sub * i * (p_fine - 1);
6175f0d5a4SJeremy L Thompson for (CeedInt j = 0; j < num_elem_sub; j++) {
624fee36f0SJeremy L Thompson for (CeedInt k = 0; k < p_fine; k++) {
634fee36f0SJeremy L Thompson ind_u_fine[p_fine * j + k] = offset + j * (p_fine - 1) + k;
6475f0d5a4SJeremy L Thompson }
6575f0d5a4SJeremy L Thompson }
664fee36f0SJeremy L Thompson CeedElemRestrictionCreate(ceed, num_elem_sub, p_fine, num_comp, num_dofs_u_fine, num_comp * num_dofs_u_fine, CEED_MEM_HOST, CEED_COPY_VALUES,
674fee36f0SJeremy L Thompson ind_u_fine, &elem_restriction_u_fine);
6875f0d5a4SJeremy L Thompson
694fee36f0SJeremy L Thompson CeedInt strides_q_data[3] = {1, q, q};
704fee36f0SJeremy L Thompson CeedElemRestrictionCreateStrided(ceed, num_elem_sub, q, 1, q * num_elem, strides_q_data, &elem_restriction_q_data);
7175f0d5a4SJeremy L Thompson
7275f0d5a4SJeremy L Thompson // -- Bases
734fee36f0SJeremy L Thompson CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, 2, q, CEED_GAUSS, &basis_x);
744fee36f0SJeremy L Thompson CeedBasisCreateTensorH1Lagrange(ceed, 1, num_comp, p_fine, q, CEED_GAUSS, &basis_u_fine);
7575f0d5a4SJeremy L Thompson
7675f0d5a4SJeremy L Thompson // -- QFunctions
7775f0d5a4SJeremy L Thompson CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup);
7875f0d5a4SJeremy L Thompson CeedQFunctionAddInput(qf_setup, "weight", 1, CEED_EVAL_WEIGHT);
7975f0d5a4SJeremy L Thompson CeedQFunctionAddInput(qf_setup, "dx", 1 * 1, CEED_EVAL_GRAD);
8075f0d5a4SJeremy L Thompson CeedQFunctionAddOutput(qf_setup, "q data", 1, CEED_EVAL_NONE);
8175f0d5a4SJeremy L Thompson
8275f0d5a4SJeremy L Thompson CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass);
8375f0d5a4SJeremy L Thompson CeedQFunctionAddInput(qf_mass, "q data", 1, CEED_EVAL_NONE);
8475f0d5a4SJeremy L Thompson CeedQFunctionAddInput(qf_mass, "u", num_comp, CEED_EVAL_INTERP);
8575f0d5a4SJeremy L Thompson CeedQFunctionAddOutput(qf_mass, "v", num_comp, CEED_EVAL_INTERP);
8675f0d5a4SJeremy L Thompson
8775f0d5a4SJeremy L Thompson // -- Operators
8875f0d5a4SJeremy L Thompson CeedOperatorCreate(ceed, qf_setup, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &sub_op_setup);
8975f0d5a4SJeremy L Thompson CeedOperatorSetField(sub_op_setup, "weight", CEED_ELEMRESTRICTION_NONE, basis_x, CEED_VECTOR_NONE);
904fee36f0SJeremy L Thompson CeedOperatorSetField(sub_op_setup, "dx", elem_restriction_x, basis_x, CEED_VECTOR_ACTIVE);
91356036faSJeremy L Thompson CeedOperatorSetField(sub_op_setup, "q data", elem_restriction_q_data, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE);
9275f0d5a4SJeremy L Thompson
934fee36f0SJeremy L Thompson CeedOperatorCreate(ceed, qf_mass, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &sub_op_mass_fine);
94356036faSJeremy L Thompson CeedOperatorSetField(sub_op_mass_fine, "q data", elem_restriction_q_data, CEED_BASIS_NONE, q_data);
954fee36f0SJeremy L Thompson CeedOperatorSetField(sub_op_mass_fine, "u", elem_restriction_u_fine, basis_u_fine, CEED_VECTOR_ACTIVE);
964fee36f0SJeremy L Thompson CeedOperatorSetField(sub_op_mass_fine, "v", elem_restriction_u_fine, basis_u_fine, CEED_VECTOR_ACTIVE);
9775f0d5a4SJeremy L Thompson
9875f0d5a4SJeremy L Thompson // -- Create qdata
994fee36f0SJeremy L Thompson CeedOperatorApply(sub_op_setup, x, q_data, CEED_REQUEST_IMMEDIATE);
10075f0d5a4SJeremy L Thompson
10175f0d5a4SJeremy L Thompson // -- Composite operators
102*ed094490SJeremy L Thompson CeedOperatorCompositeAddSub(op_mass_fine, sub_op_mass_fine);
10375f0d5a4SJeremy L Thompson
10475f0d5a4SJeremy L Thompson // -- Cleanup
10575f0d5a4SJeremy L Thompson CeedVectorDestroy(&q_data);
1064fee36f0SJeremy L Thompson CeedElemRestrictionDestroy(&elem_restriction_u_fine);
1074fee36f0SJeremy L Thompson CeedElemRestrictionDestroy(&elem_restriction_x);
1084fee36f0SJeremy L Thompson CeedElemRestrictionDestroy(&elem_restriction_q_data);
1094fee36f0SJeremy L Thompson CeedBasisDestroy(&basis_u_fine);
11075f0d5a4SJeremy L Thompson CeedBasisDestroy(&basis_x);
11175f0d5a4SJeremy L Thompson CeedQFunctionDestroy(&qf_setup);
11275f0d5a4SJeremy L Thompson CeedQFunctionDestroy(&qf_mass);
11375f0d5a4SJeremy L Thompson CeedOperatorDestroy(&sub_op_setup);
1144fee36f0SJeremy L Thompson CeedOperatorDestroy(&sub_op_mass_fine);
11575f0d5a4SJeremy L Thompson }
11675f0d5a4SJeremy L Thompson
11775f0d5a4SJeremy L Thompson // Scale for suboperator multiplicity
1184fee36f0SJeremy L Thompson CeedVectorCreate(ceed, num_comp * num_dofs_u_fine, &p_mult_fine);
119*ed094490SJeremy L Thompson CeedOperatorCompositeGetMultiplicity(op_mass_fine, 0, NULL, p_mult_fine);
12075f0d5a4SJeremy L Thompson
12175f0d5a4SJeremy L Thompson // Setup coarse and prolong/restriction suboperators
12275f0d5a4SJeremy L Thompson for (CeedInt i = 0; i < num_sub_ops; i++) {
1234fee36f0SJeremy L Thompson CeedElemRestriction elem_restriction_u_coarse;
1244fee36f0SJeremy L Thompson CeedBasis basis_u_coarse;
1254fee36f0SJeremy L Thompson CeedOperator *sub_ops_mass_fine, sub_op_mass_coarse, sub_op_prolong, sub_op_restrict;
12675f0d5a4SJeremy L Thompson
12775f0d5a4SJeremy L Thompson // -- Fine grid operator
128*ed094490SJeremy L Thompson CeedOperatorCompositeGetSubList(op_mass_fine, &sub_ops_mass_fine);
12975f0d5a4SJeremy L Thompson
13075f0d5a4SJeremy L Thompson // -- Restrictions
1314fee36f0SJeremy L Thompson CeedInt offset = num_elem_sub * i * (p_coarse - 1);
13275f0d5a4SJeremy L Thompson for (CeedInt j = 0; j < num_elem_sub; j++) {
1334fee36f0SJeremy L Thompson for (CeedInt k = 0; k < p_coarse; k++) {
1344fee36f0SJeremy L Thompson ind_u_coarse[p_coarse * j + k] = offset + j * (p_coarse - 1) + k;
13575f0d5a4SJeremy L Thompson }
13675f0d5a4SJeremy L Thompson }
1374fee36f0SJeremy L Thompson CeedElemRestrictionCreate(ceed, num_elem_sub, p_coarse, num_comp, num_dofs_u_coarse, num_comp * num_dofs_u_coarse, CEED_MEM_HOST,
1384fee36f0SJeremy L Thompson CEED_COPY_VALUES, ind_u_coarse, &elem_restriction_u_coarse);
13975f0d5a4SJeremy L Thompson
14075f0d5a4SJeremy L Thompson // -- Bases
1414fee36f0SJeremy L Thompson CeedBasisCreateTensorH1Lagrange(ceed, 1, num_comp, p_coarse, q, CEED_GAUSS, &basis_u_coarse);
14275f0d5a4SJeremy L Thompson
14375f0d5a4SJeremy L Thompson // -- Create multigrid level
1444fee36f0SJeremy L Thompson CeedOperatorMultigridLevelCreate(sub_ops_mass_fine[i], p_mult_fine, elem_restriction_u_coarse, basis_u_coarse, &sub_op_mass_coarse,
1454fee36f0SJeremy L Thompson &sub_op_prolong, &sub_op_restrict);
14675f0d5a4SJeremy L Thompson
14775f0d5a4SJeremy L Thompson // -- Composite operators
148*ed094490SJeremy L Thompson CeedOperatorCompositeAddSub(op_mass_coarse, sub_op_mass_coarse);
149*ed094490SJeremy L Thompson CeedOperatorCompositeAddSub(op_prolong, sub_op_prolong);
150*ed094490SJeremy L Thompson CeedOperatorCompositeAddSub(op_restrict, sub_op_restrict);
15175f0d5a4SJeremy L Thompson
15275f0d5a4SJeremy L Thompson // -- Cleanup
1534fee36f0SJeremy L Thompson CeedElemRestrictionDestroy(&elem_restriction_u_coarse);
1544fee36f0SJeremy L Thompson CeedBasisDestroy(&basis_u_coarse);
1554fee36f0SJeremy L Thompson CeedOperatorDestroy(&sub_op_mass_coarse);
15675f0d5a4SJeremy L Thompson CeedOperatorDestroy(&sub_op_prolong);
15775f0d5a4SJeremy L Thompson CeedOperatorDestroy(&sub_op_restrict);
15875f0d5a4SJeremy L Thompson }
15975f0d5a4SJeremy L Thompson
16075f0d5a4SJeremy L Thompson // Coarse problem
1614fee36f0SJeremy L Thompson CeedVectorSetValue(u_coarse, 1.0);
1624fee36f0SJeremy L Thompson CeedOperatorApply(op_mass_coarse, u_coarse, v_coarse, CEED_REQUEST_IMMEDIATE);
16375f0d5a4SJeremy L Thompson
16475f0d5a4SJeremy L Thompson // Check output
1654fee36f0SJeremy L Thompson {
1664fee36f0SJeremy L Thompson const CeedScalar *v_array;
1674fee36f0SJeremy L Thompson CeedScalar sum = 0.;
1684fee36f0SJeremy L Thompson
1694fee36f0SJeremy L Thompson CeedVectorGetArrayRead(v_coarse, CEED_MEM_HOST, &v_array);
1704fee36f0SJeremy L Thompson for (CeedInt i = 0; i < num_comp * num_dofs_u_coarse; i++) {
1714fee36f0SJeremy L Thompson sum += v_array[i];
17275f0d5a4SJeremy L Thompson }
1734fee36f0SJeremy L Thompson CeedVectorRestoreArrayRead(v_coarse, &v_array);
17475f0d5a4SJeremy L Thompson if (fabs(sum - 2.) > 1000. * CEED_EPSILON) printf("Computed Area Coarse Grid: %f != True Area: 2.0\n", sum);
1754fee36f0SJeremy L Thompson }
17675f0d5a4SJeremy L Thompson
17775f0d5a4SJeremy L Thompson // Prolong coarse u
1784fee36f0SJeremy L Thompson CeedOperatorApply(op_prolong, u_coarse, u_fine, CEED_REQUEST_IMMEDIATE);
17975f0d5a4SJeremy L Thompson
18075f0d5a4SJeremy L Thompson // Fine problem
1814fee36f0SJeremy L Thompson CeedOperatorApply(op_mass_fine, u_fine, v_fine, CEED_REQUEST_IMMEDIATE);
18275f0d5a4SJeremy L Thompson
18375f0d5a4SJeremy L Thompson // Check output
1844fee36f0SJeremy L Thompson {
1854fee36f0SJeremy L Thompson const CeedScalar *v_array;
1864fee36f0SJeremy L Thompson CeedScalar sum = 0.;
1874fee36f0SJeremy L Thompson CeedVectorGetArrayRead(v_fine, CEED_MEM_HOST, &v_array);
1884fee36f0SJeremy L Thompson for (CeedInt i = 0; i < num_comp * num_dofs_u_fine; i++) {
1894fee36f0SJeremy L Thompson sum += v_array[i];
19075f0d5a4SJeremy L Thompson }
1914fee36f0SJeremy L Thompson CeedVectorRestoreArrayRead(v_fine, &v_array);
1924fee36f0SJeremy L Thompson
19375f0d5a4SJeremy L Thompson if (fabs(sum - 2.) > 1000. * CEED_EPSILON) printf("Computed Area Fine Grid: %f != True Area: 2.0\n", sum);
1944fee36f0SJeremy L Thompson }
19575f0d5a4SJeremy L Thompson // Restrict state to coarse grid
1964fee36f0SJeremy L Thompson CeedOperatorApply(op_restrict, v_fine, v_coarse, CEED_REQUEST_IMMEDIATE);
19775f0d5a4SJeremy L Thompson
19875f0d5a4SJeremy L Thompson // Check output
1994fee36f0SJeremy L Thompson {
2004fee36f0SJeremy L Thompson const CeedScalar *v_array;
2014fee36f0SJeremy L Thompson CeedScalar sum = 0.;
2024fee36f0SJeremy L Thompson
2034fee36f0SJeremy L Thompson CeedVectorGetArrayRead(v_coarse, CEED_MEM_HOST, &v_array);
2044fee36f0SJeremy L Thompson for (CeedInt i = 0; i < num_comp * num_dofs_u_coarse; i++) {
2054fee36f0SJeremy L Thompson sum += v_array[i];
20675f0d5a4SJeremy L Thompson }
2074fee36f0SJeremy L Thompson CeedVectorRestoreArrayRead(v_coarse, &v_array);
20875f0d5a4SJeremy L Thompson if (fabs(sum - 2.) > 1000. * CEED_EPSILON) printf("Computed Area Coarse Grid: %f != True Area: 2.0\n", sum);
2094fee36f0SJeremy L Thompson }
21075f0d5a4SJeremy L Thompson
21175f0d5a4SJeremy L Thompson // Cleanup
2124fee36f0SJeremy L Thompson CeedVectorDestroy(&x);
2134fee36f0SJeremy L Thompson CeedVectorDestroy(&u_coarse);
2144fee36f0SJeremy L Thompson CeedVectorDestroy(&u_fine);
2154fee36f0SJeremy L Thompson CeedVectorDestroy(&v_coarse);
2164fee36f0SJeremy L Thompson CeedVectorDestroy(&v_fine);
2174fee36f0SJeremy L Thompson CeedVectorDestroy(&p_mult_fine);
2184fee36f0SJeremy L Thompson CeedOperatorDestroy(&op_mass_coarse);
2194fee36f0SJeremy L Thompson CeedOperatorDestroy(&op_mass_fine);
22075f0d5a4SJeremy L Thompson CeedOperatorDestroy(&op_prolong);
22175f0d5a4SJeremy L Thompson CeedOperatorDestroy(&op_restrict);
22275f0d5a4SJeremy L Thompson CeedDestroy(&ceed);
22375f0d5a4SJeremy L Thompson return 0;
22475f0d5a4SJeremy L Thompson }
225