xref: /libCEED/tests/t500-operator.c (revision 5d1e906964b04cb5161d672bdc4191311737c811)
14411cf47Sjeremylt /// @file
231e5b035Sjeremylt /// Test creation, action, and destruction for mass matrix operator
331e5b035Sjeremylt /// \test Test creation, action, and destruction for mass matrix operator
44d537eeaSYohann #include "t500-operator.h"
557c64913Sjeremylt 
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)1157c64913Sjeremylt int main(int argc, char **argv) {
1257c64913Sjeremylt   Ceed                ceed;
134fee36f0SJeremy 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;
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];
214fee36f0SJeremy L Thompson   CeedScalar          x_array[num_nodes_x];
2257c64913Sjeremylt 
239ddbf157Sjeremylt   //! [Ceed Init]
2457c64913Sjeremylt   CeedInit(argv[1], &ceed);
259ddbf157Sjeremylt   //! [Ceed Init]
264fee36f0SJeremy L Thompson   for (CeedInt i = 0; i < num_nodes_x; i++) x_array[i] = (CeedScalar)i / (num_nodes_x - 1);
27d1d35e2fSjeremylt   for (CeedInt i = 0; i < num_elem; i++) {
28d1d35e2fSjeremylt     ind_x[2 * i + 0] = i;
29d1d35e2fSjeremylt     ind_x[2 * i + 1] = i + 1;
3057c64913Sjeremylt   }
319ddbf157Sjeremylt   //! [ElemRestr Create]
324fee36f0SJeremy L Thompson   CeedElemRestrictionCreate(ceed, num_elem, 2, 1, 1, num_nodes_x, CEED_MEM_HOST, CEED_USE_POINTER, ind_x, &elem_restriction_x);
339ddbf157Sjeremylt   //! [ElemRestr Create]
3457c64913Sjeremylt 
35d1d35e2fSjeremylt   for (CeedInt i = 0; i < num_elem; i++) {
364fee36f0SJeremy L Thompson     for (CeedInt j = 0; j < p; j++) {
374fee36f0SJeremy L Thompson       ind_u[p * i + j] = i * (p - 1) + j;
3857c64913Sjeremylt     }
3957c64913Sjeremylt   }
409ddbf157Sjeremylt   //! [ElemRestrU Create]
414fee36f0SJeremy L Thompson   CeedElemRestrictionCreate(ceed, num_elem, p, 1, 1, num_nodes_u, CEED_MEM_HOST, CEED_USE_POINTER, ind_u, &elem_restriction_u);
424fee36f0SJeremy L Thompson   CeedInt strides_q_data[3] = {1, q, q};
434fee36f0SJeremy L Thompson   CeedElemRestrictionCreateStrided(ceed, num_elem, q, 1, q * num_elem, strides_q_data, &elem_restriction_q_data);
449ddbf157Sjeremylt   //! [ElemRestrU Create]
4557c64913Sjeremylt 
469ddbf157Sjeremylt   //! [Basis Create]
474fee36f0SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, 2, q, CEED_GAUSS, &basis_x);
484fee36f0SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, p, q, CEED_GAUSS, &basis_u);
499ddbf157Sjeremylt   //! [Basis Create]
5057c64913Sjeremylt 
519ddbf157Sjeremylt   //! [QFunction Create]
524d537eeaSYohann   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup);
53a61c78d6SJeremy L Thompson   CeedQFunctionAddInput(qf_setup, "weight", 1, CEED_EVAL_WEIGHT);
544d537eeaSYohann   CeedQFunctionAddInput(qf_setup, "dx", 1, CEED_EVAL_GRAD);
5557c64913Sjeremylt   CeedQFunctionAddOutput(qf_setup, "rho", 1, CEED_EVAL_NONE);
5657c64913Sjeremylt 
574d537eeaSYohann   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass);
5857c64913Sjeremylt   CeedQFunctionAddInput(qf_mass, "rho", 1, CEED_EVAL_NONE);
5957c64913Sjeremylt   CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP);
6057c64913Sjeremylt   CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP);
619ddbf157Sjeremylt   //! [QFunction Create]
6257c64913Sjeremylt 
639ddbf157Sjeremylt   //! [Setup Create]
642b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_setup, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_setup);
659ddbf157Sjeremylt   //! [Setup Create]
669ddbf157Sjeremylt 
679ddbf157Sjeremylt   //! [Operator Create]
682b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_mass, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_mass);
699ddbf157Sjeremylt   //! [Operator Create]
7057c64913Sjeremylt 
714fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_nodes_x, &x);
724fee36f0SJeremy L Thompson   CeedVectorSetArray(x, CEED_MEM_HOST, CEED_USE_POINTER, x_array);
734fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_elem * q, &q_data);
7457c64913Sjeremylt 
759ddbf157Sjeremylt   //! [Setup Set]
762b730f8bSJeremy L Thompson   CeedOperatorSetField(op_setup, "weight", CEED_ELEMRESTRICTION_NONE, basis_x, CEED_VECTOR_NONE);
774fee36f0SJeremy L Thompson   CeedOperatorSetField(op_setup, "dx", elem_restriction_x, basis_x, CEED_VECTOR_ACTIVE);
78*356036faSJeremy L Thompson   CeedOperatorSetField(op_setup, "rho", elem_restriction_q_data, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE);
799ddbf157Sjeremylt   //! [Setup Set]
8057c64913Sjeremylt 
819ddbf157Sjeremylt   //! [Operator Set]
82*356036faSJeremy L Thompson   CeedOperatorSetField(op_mass, "rho", elem_restriction_q_data, CEED_BASIS_NONE, q_data);
834fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass, "u", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
844fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass, "v", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
859ddbf157Sjeremylt   //! [Operator Set]
8657c64913Sjeremylt 
879ddbf157Sjeremylt   //! [Setup Apply]
884fee36f0SJeremy L Thompson   CeedOperatorApply(op_setup, x, q_data, CEED_REQUEST_IMMEDIATE);
899ddbf157Sjeremylt   //! [Setup Apply]
9057c64913Sjeremylt 
914fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_nodes_u, &u);
924fee36f0SJeremy L Thompson   CeedVectorSetValue(u, 0.0);
934fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_nodes_u, &v);
949ddbf157Sjeremylt   //! [Operator Apply]
954fee36f0SJeremy L Thompson   CeedOperatorApply(op_mass, u, v, CEED_REQUEST_IMMEDIATE);
969ddbf157Sjeremylt   //! [Operator Apply]
9757c64913Sjeremylt 
984fee36f0SJeremy L Thompson   {
994fee36f0SJeremy L Thompson     const CeedScalar *v_array;
1004fee36f0SJeremy L Thompson 
1014fee36f0SJeremy L Thompson     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
1022b730f8bSJeremy L Thompson     for (CeedInt i = 0; i < num_nodes_u; i++) {
1034fee36f0SJeremy L Thompson       if (fabs(v_array[i]) > 1e-14) printf("[%" CeedInt_FMT "] v %g != 0.0\n", i, v_array[i]);
1042b730f8bSJeremy L Thompson     }
1054fee36f0SJeremy L Thompson     CeedVectorRestoreArrayRead(v, &v_array);
1064fee36f0SJeremy L Thompson   }
10757c64913Sjeremylt 
1084fee36f0SJeremy L Thompson   CeedVectorDestroy(&x);
1094fee36f0SJeremy L Thompson   CeedVectorDestroy(&u);
1104fee36f0SJeremy L Thompson   CeedVectorDestroy(&v);
1114fee36f0SJeremy L Thompson   CeedVectorDestroy(&q_data);
1124fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_x);
1134fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_u);
1144fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_q_data);
1154fee36f0SJeremy L Thompson   CeedBasisDestroy(&basis_x);
1164fee36f0SJeremy L Thompson   CeedBasisDestroy(&basis_u);
11757c64913Sjeremylt   CeedQFunctionDestroy(&qf_setup);
11857c64913Sjeremylt   CeedQFunctionDestroy(&qf_mass);
11957c64913Sjeremylt   CeedOperatorDestroy(&op_setup);
12057c64913Sjeremylt   CeedOperatorDestroy(&op_mass);
12157c64913Sjeremylt   CeedDestroy(&ceed);
12257c64913Sjeremylt   return 0;
12357c64913Sjeremylt }
124