xref: /libCEED/tests/t530-operator.c (revision 01ecf5b4a3895d33c3d6fd6ee44ca505a2fd4980)
11d102b48SJeremy L Thompson /// @file
21d102b48SJeremy L Thompson /// Test assembly of mass matrix operator QFunction
31d102b48SJeremy L Thompson /// \test Test assembly of mass matrix operator QFunction
41d102b48SJeremy L Thompson #include <ceed.h>
51d102b48SJeremy L Thompson #include <math.h>
649aac155SJeremy L Thompson #include <stdio.h>
72b730f8bSJeremy L Thompson #include <stdlib.h>
82b730f8bSJeremy L Thompson 
9a05f9790Sjeremylt #include "t510-operator.h"
101d102b48SJeremy L Thompson 
main(int argc,char ** argv)111d102b48SJeremy L Thompson int main(int argc, char **argv) {
121d102b48SJeremy L Thompson   Ceed                ceed;
134fee36f0SJeremy L Thompson   CeedElemRestriction elem_restriction_x, elem_restriction_u, elem_restriction_q_data, elem_restriction_assembled;
14d1d35e2fSjeremylt   CeedBasis           basis_x, basis_u;
151d102b48SJeremy L Thompson   CeedQFunction       qf_setup, qf_mass;
161d102b48SJeremy L Thompson   CeedOperator        op_setup, op_mass;
174fee36f0SJeremy L Thompson   CeedVector          q_data, x, qf_assembled, u, v;
184fee36f0SJeremy L Thompson   CeedInt             num_elem = 6, p = 3, q = 4, dim = 2;
191d102b48SJeremy L Thompson   CeedInt             nx = 3, ny = 2;
204fee36f0SJeremy L Thompson   CeedInt             num_dofs = (nx * 2 + 1) * (ny * 2 + 1), num_qpts = num_elem * q * q;
214fee36f0SJeremy L Thompson   CeedInt             ind_x[num_elem * p * p];
221d102b48SJeremy L Thompson 
231d102b48SJeremy L Thompson   CeedInit(argv[1], &ceed);
241d102b48SJeremy L Thompson 
254fee36f0SJeremy L Thompson   // Vectors
264fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, dim * num_dofs, &x);
274fee36f0SJeremy L Thompson   {
284fee36f0SJeremy L Thompson     CeedScalar x_array[dim * num_dofs];
292b730f8bSJeremy L Thompson     for (CeedInt i = 0; i < nx * 2 + 1; i++) {
301d102b48SJeremy L Thompson       for (CeedInt j = 0; j < ny * 2 + 1; j++) {
314fee36f0SJeremy L Thompson         x_array[i + j * (nx * 2 + 1) + 0 * num_dofs] = (CeedScalar)i / (2 * nx);
324fee36f0SJeremy L Thompson         x_array[i + j * (nx * 2 + 1) + 1 * num_dofs] = (CeedScalar)j / (2 * ny);
331d102b48SJeremy L Thompson       }
342b730f8bSJeremy L Thompson     }
354fee36f0SJeremy L Thompson     CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, x_array);
364fee36f0SJeremy L Thompson   }
374fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_dofs, &u);
384fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_dofs, &v);
39d1d35e2fSjeremylt   CeedVectorCreate(ceed, num_qpts, &q_data);
401d102b48SJeremy L Thompson 
414fee36f0SJeremy L Thompson   // Restrictions
42d1d35e2fSjeremylt   for (CeedInt i = 0; i < num_elem; i++) {
431d102b48SJeremy L Thompson     CeedInt col, row, offset;
441d102b48SJeremy L Thompson     col    = i % nx;
451d102b48SJeremy L Thompson     row    = i / nx;
464fee36f0SJeremy L Thompson     offset = col * (p - 1) + row * (nx * 2 + 1) * (p - 1);
474fee36f0SJeremy L Thompson     for (CeedInt j = 0; j < p; j++) {
484fee36f0SJeremy L Thompson       for (CeedInt k = 0; k < p; k++) ind_x[p * (p * i + k) + j] = offset + k * (nx * 2 + 1) + j;
492b730f8bSJeremy L Thompson     }
501d102b48SJeremy L Thompson   }
514fee36f0SJeremy L Thompson   CeedElemRestrictionCreate(ceed, num_elem, p * p, dim, num_dofs, dim * num_dofs, CEED_MEM_HOST, CEED_USE_POINTER, ind_x, &elem_restriction_x);
524fee36f0SJeremy L Thompson   CeedElemRestrictionCreate(ceed, num_elem, p * p, 1, 1, num_dofs, CEED_MEM_HOST, CEED_USE_POINTER, ind_x, &elem_restriction_u);
531d102b48SJeremy L Thompson 
544fee36f0SJeremy L Thompson   CeedInt strides_q_data[3] = {1, q * q, q * q};
554fee36f0SJeremy L Thompson   CeedElemRestrictionCreateStrided(ceed, num_elem, q * q, 1, num_qpts, strides_q_data, &elem_restriction_q_data);
561d102b48SJeremy L Thompson 
571d102b48SJeremy L Thompson   // Bases
584fee36f0SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, dim, dim, p, q, CEED_GAUSS, &basis_x);
594fee36f0SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, dim, 1, p, q, CEED_GAUSS, &basis_u);
601d102b48SJeremy L Thompson 
611d102b48SJeremy L Thompson   // QFunctions
621d102b48SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup);
63a61c78d6SJeremy L Thompson   CeedQFunctionAddInput(qf_setup, "weight", 1, CEED_EVAL_WEIGHT);
641d102b48SJeremy L Thompson   CeedQFunctionAddInput(qf_setup, "dx", dim * dim, CEED_EVAL_GRAD);
651d102b48SJeremy L Thompson   CeedQFunctionAddOutput(qf_setup, "rho", 1, CEED_EVAL_NONE);
661d102b48SJeremy L Thompson 
671d102b48SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass);
681d102b48SJeremy L Thompson   CeedQFunctionAddInput(qf_mass, "rho", 1, CEED_EVAL_NONE);
691d102b48SJeremy L Thompson   CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP);
701d102b48SJeremy L Thompson   CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP);
711d102b48SJeremy L Thompson 
721d102b48SJeremy L Thompson   // Operators
732b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_setup, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_setup);
742b730f8bSJeremy L Thompson   CeedOperatorSetField(op_setup, "weight", CEED_ELEMRESTRICTION_NONE, basis_x, CEED_VECTOR_NONE);
754fee36f0SJeremy L Thompson   CeedOperatorSetField(op_setup, "dx", elem_restriction_x, basis_x, CEED_VECTOR_ACTIVE);
76356036faSJeremy L Thompson   CeedOperatorSetField(op_setup, "rho", elem_restriction_q_data, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE);
771d102b48SJeremy L Thompson 
782b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_mass, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_mass);
79356036faSJeremy L Thompson   CeedOperatorSetField(op_mass, "rho", elem_restriction_q_data, CEED_BASIS_NONE, q_data);
804fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass, "u", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
814fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass, "v", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
821d102b48SJeremy L Thompson 
831d102b48SJeremy L Thompson   // Apply Setup Operator
844fee36f0SJeremy L Thompson   CeedOperatorApply(op_setup, x, q_data, CEED_REQUEST_IMMEDIATE);
851d102b48SJeremy L Thompson 
861d102b48SJeremy L Thompson   // Assemble QFunction
87beecbf24SJeremy L Thompson   CeedOperatorSetQFunctionAssemblyReuse(op_mass, true);
88beecbf24SJeremy L Thompson   CeedOperatorSetQFunctionAssemblyDataUpdateNeeded(op_mass, true);
894fee36f0SJeremy L Thompson   CeedOperatorLinearAssembleQFunction(op_mass, &qf_assembled, &elem_restriction_assembled, CEED_REQUEST_IMMEDIATE);
901d102b48SJeremy L Thompson 
911d102b48SJeremy L Thompson   // Check output
924fee36f0SJeremy L Thompson   {
934fee36f0SJeremy L Thompson     const CeedScalar *assembled_array, *q_data_array;
944fee36f0SJeremy L Thompson 
954fee36f0SJeremy L Thompson     CeedVectorGetArrayRead(qf_assembled, CEED_MEM_HOST, &assembled_array);
964fee36f0SJeremy L Thompson     CeedVectorGetArrayRead(q_data, CEED_MEM_HOST, &q_data_array);
97*6c10af5dSJeremy L Thompson     for (CeedInt i = 0; i < num_qpts; i++) {
984fee36f0SJeremy L Thompson       if (fabs(q_data_array[i] - assembled_array[i]) > 1e-9) {
994fee36f0SJeremy L Thompson         // LCOV_EXCL_START
1004fee36f0SJeremy L Thompson         printf("Error: qf_assembled[%" CeedInt_FMT "] = %f != %f\n", i, assembled_array[i], q_data_array[i]);
1014fee36f0SJeremy L Thompson         // LCOV_EXCL_STOP
1024fee36f0SJeremy L Thompson       }
103*6c10af5dSJeremy L Thompson     }
1044fee36f0SJeremy L Thompson     CeedVectorRestoreArrayRead(qf_assembled, &assembled_array);
1054fee36f0SJeremy L Thompson     CeedVectorRestoreArrayRead(q_data, &q_data_array);
1064fee36f0SJeremy L Thompson   }
1071d102b48SJeremy L Thompson 
1081d102b48SJeremy L Thompson   // Apply original Mass Operator
1091d102b48SJeremy L Thompson   CeedVectorSetValue(u, 1.0);
1101d102b48SJeremy L Thompson   CeedOperatorApply(op_mass, u, v, CEED_REQUEST_IMMEDIATE);
1111d102b48SJeremy L Thompson 
1121d102b48SJeremy L Thompson   // Check output
1134fee36f0SJeremy L Thompson   {
1144fee36f0SJeremy L Thompson     const CeedScalar *v_array;
1151d102b48SJeremy L Thompson     CeedScalar        area = 0.0;
1164fee36f0SJeremy L Thompson 
1174fee36f0SJeremy L Thompson     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
1184fee36f0SJeremy L Thompson     for (CeedInt i = 0; i < num_dofs; i++) area += v_array[i];
1192b730f8bSJeremy L Thompson     if (fabs(area - 1.0) > 100. * CEED_EPSILON) printf("Error: True operator computed area = %f != 1.0\n", area);
1204fee36f0SJeremy L Thompson     CeedVectorRestoreArrayRead(v, &v_array);
1214fee36f0SJeremy L Thompson   }
1221d102b48SJeremy L Thompson 
123d1d35e2fSjeremylt   // Switch to new q_data
1244fee36f0SJeremy L Thompson   {
1254fee36f0SJeremy L Thompson     const CeedScalar *assembled_array;
1264fee36f0SJeremy L Thompson 
1274fee36f0SJeremy L Thompson     CeedVectorGetArrayRead(qf_assembled, CEED_MEM_HOST, &assembled_array);
1284fee36f0SJeremy L Thompson     CeedVectorSetArray(q_data, CEED_MEM_HOST, CEED_COPY_VALUES, (CeedScalar *)assembled_array);
1294fee36f0SJeremy L Thompson     CeedVectorRestoreArrayRead(qf_assembled, &assembled_array);
1304fee36f0SJeremy L Thompson   }
1311d102b48SJeremy L Thompson 
1321d102b48SJeremy L Thompson   // Apply new Mass Operator
1331d102b48SJeremy L Thompson   CeedOperatorApply(op_mass, u, v, CEED_REQUEST_IMMEDIATE);
1341d102b48SJeremy L Thompson 
1351d102b48SJeremy L Thompson   // Check output
1364fee36f0SJeremy L Thompson   {
1374fee36f0SJeremy L Thompson     const CeedScalar *v_array;
1384fee36f0SJeremy L Thompson     CeedScalar        area = 0.0;
1394fee36f0SJeremy L Thompson 
1404fee36f0SJeremy L Thompson     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
1414fee36f0SJeremy L Thompson     for (CeedInt i = 0; i < num_dofs; i++) area += v_array[i];
1424fee36f0SJeremy L Thompson     CeedVectorRestoreArrayRead(v, &v_array);
1432b730f8bSJeremy L Thompson     if (fabs(area - 1.0) > 1000. * CEED_EPSILON) printf("Error: Linearized operator computed area = %f != 1.0\n", area);
1444fee36f0SJeremy L Thompson   }
1451d102b48SJeremy L Thompson 
1461d102b48SJeremy L Thompson   // Cleanup
1474fee36f0SJeremy L Thompson   CeedVectorDestroy(&x);
1484fee36f0SJeremy L Thompson   CeedVectorDestroy(&qf_assembled);
1494fee36f0SJeremy L Thompson   CeedVectorDestroy(&q_data);
1504fee36f0SJeremy L Thompson   CeedVectorDestroy(&u);
1514fee36f0SJeremy L Thompson   CeedVectorDestroy(&v);
1524fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_u);
1534fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_x);
1544fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_q_data);
1554fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_assembled);
1564fee36f0SJeremy L Thompson   CeedBasisDestroy(&basis_u);
1574fee36f0SJeremy L Thompson   CeedBasisDestroy(&basis_x);
1581d102b48SJeremy L Thompson   CeedQFunctionDestroy(&qf_setup);
1591d102b48SJeremy L Thompson   CeedQFunctionDestroy(&qf_mass);
1601d102b48SJeremy L Thompson   CeedOperatorDestroy(&op_setup);
1611d102b48SJeremy L Thompson   CeedOperatorDestroy(&op_mass);
1621d102b48SJeremy L Thompson   CeedDestroy(&ceed);
1631d102b48SJeremy L Thompson   return 0;
1641d102b48SJeremy L Thompson }
165