xref: /libCEED/tests/t500-operator.c (revision 4fee36f0a30516a0b5ad51bf7eb3b32d83efd623)
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>
82b730f8bSJeremy L Thompson #include <stdlib.h>
92b730f8bSJeremy L Thompson 
1057c64913Sjeremylt int main(int argc, char **argv) {
1157c64913Sjeremylt   Ceed                ceed;
12*4fee36f0SJeremy L Thompson   CeedElemRestriction elem_restriction_x, elem_restriction_u, elem_restriction_q_data;
13d1d35e2fSjeremylt   CeedBasis           basis_x, basis_u;
14a8de75f0Sjeremylt   CeedQFunction       qf_setup, qf_mass;
15a8de75f0Sjeremylt   CeedOperator        op_setup, op_mass;
16*4fee36f0SJeremy L Thompson   CeedVector          q_data, x, u, v;
17*4fee36f0SJeremy L Thompson   CeedInt             num_elem = 15, p = 5, q = 8;
18*4fee36f0SJeremy L Thompson   CeedInt             num_nodes_x = num_elem + 1, num_nodes_u = num_elem * (p - 1) + 1;
19*4fee36f0SJeremy L Thompson   CeedInt             ind_x[num_elem * 2], ind_u[num_elem * p];
20*4fee36f0SJeremy L Thompson   CeedScalar          x_array[num_nodes_x];
2157c64913Sjeremylt 
229ddbf157Sjeremylt   //! [Ceed Init]
2357c64913Sjeremylt   CeedInit(argv[1], &ceed);
249ddbf157Sjeremylt   //! [Ceed Init]
25*4fee36f0SJeremy L Thompson   for (CeedInt i = 0; i < num_nodes_x; i++) x_array[i] = (CeedScalar)i / (num_nodes_x - 1);
26d1d35e2fSjeremylt   for (CeedInt i = 0; i < num_elem; i++) {
27d1d35e2fSjeremylt     ind_x[2 * i + 0] = i;
28d1d35e2fSjeremylt     ind_x[2 * i + 1] = i + 1;
2957c64913Sjeremylt   }
309ddbf157Sjeremylt   //! [ElemRestr Create]
31*4fee36f0SJeremy L Thompson   CeedElemRestrictionCreate(ceed, num_elem, 2, 1, 1, num_nodes_x, CEED_MEM_HOST, CEED_USE_POINTER, ind_x, &elem_restriction_x);
329ddbf157Sjeremylt   //! [ElemRestr Create]
3357c64913Sjeremylt 
34d1d35e2fSjeremylt   for (CeedInt i = 0; i < num_elem; i++) {
35*4fee36f0SJeremy L Thompson     for (CeedInt j = 0; j < p; j++) {
36*4fee36f0SJeremy L Thompson       ind_u[p * i + j] = i * (p - 1) + j;
3757c64913Sjeremylt     }
3857c64913Sjeremylt   }
399ddbf157Sjeremylt   //! [ElemRestrU Create]
40*4fee36f0SJeremy L Thompson   CeedElemRestrictionCreate(ceed, num_elem, p, 1, 1, num_nodes_u, CEED_MEM_HOST, CEED_USE_POINTER, ind_u, &elem_restriction_u);
41*4fee36f0SJeremy L Thompson   CeedInt strides_q_data[3] = {1, q, q};
42*4fee36f0SJeremy L Thompson   CeedElemRestrictionCreateStrided(ceed, num_elem, q, 1, q * num_elem, strides_q_data, &elem_restriction_q_data);
439ddbf157Sjeremylt   //! [ElemRestrU Create]
4457c64913Sjeremylt 
459ddbf157Sjeremylt   //! [Basis Create]
46*4fee36f0SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, 2, q, CEED_GAUSS, &basis_x);
47*4fee36f0SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, p, q, CEED_GAUSS, &basis_u);
489ddbf157Sjeremylt   //! [Basis Create]
4957c64913Sjeremylt 
509ddbf157Sjeremylt   //! [QFunction Create]
514d537eeaSYohann   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup);
52a61c78d6SJeremy L Thompson   CeedQFunctionAddInput(qf_setup, "weight", 1, CEED_EVAL_WEIGHT);
534d537eeaSYohann   CeedQFunctionAddInput(qf_setup, "dx", 1, CEED_EVAL_GRAD);
5457c64913Sjeremylt   CeedQFunctionAddOutput(qf_setup, "rho", 1, CEED_EVAL_NONE);
5557c64913Sjeremylt 
564d537eeaSYohann   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass);
5757c64913Sjeremylt   CeedQFunctionAddInput(qf_mass, "rho", 1, CEED_EVAL_NONE);
5857c64913Sjeremylt   CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP);
5957c64913Sjeremylt   CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP);
609ddbf157Sjeremylt   //! [QFunction Create]
6157c64913Sjeremylt 
629ddbf157Sjeremylt   //! [Setup Create]
632b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_setup, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_setup);
649ddbf157Sjeremylt   //! [Setup Create]
659ddbf157Sjeremylt 
669ddbf157Sjeremylt   //! [Operator Create]
672b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_mass, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_mass);
689ddbf157Sjeremylt   //! [Operator Create]
6957c64913Sjeremylt 
70*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_nodes_x, &x);
71*4fee36f0SJeremy L Thompson   CeedVectorSetArray(x, CEED_MEM_HOST, CEED_USE_POINTER, x_array);
72*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_elem * q, &q_data);
7357c64913Sjeremylt 
749ddbf157Sjeremylt   //! [Setup Set]
752b730f8bSJeremy L Thompson   CeedOperatorSetField(op_setup, "weight", CEED_ELEMRESTRICTION_NONE, basis_x, CEED_VECTOR_NONE);
76*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_setup, "dx", elem_restriction_x, basis_x, CEED_VECTOR_ACTIVE);
77*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_setup, "rho", elem_restriction_q_data, CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
789ddbf157Sjeremylt   //! [Setup Set]
7957c64913Sjeremylt 
809ddbf157Sjeremylt   //! [Operator Set]
81*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass, "rho", elem_restriction_q_data, CEED_BASIS_COLLOCATED, q_data);
82*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass, "u", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
83*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass, "v", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
849ddbf157Sjeremylt   //! [Operator Set]
8557c64913Sjeremylt 
869ddbf157Sjeremylt   //! [Setup Apply]
87*4fee36f0SJeremy L Thompson   CeedOperatorApply(op_setup, x, q_data, CEED_REQUEST_IMMEDIATE);
889ddbf157Sjeremylt   //! [Setup Apply]
8957c64913Sjeremylt 
90*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_nodes_u, &u);
91*4fee36f0SJeremy L Thompson   CeedVectorSetValue(u, 0.0);
92*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_nodes_u, &v);
939ddbf157Sjeremylt   //! [Operator Apply]
94*4fee36f0SJeremy L Thompson   CeedOperatorApply(op_mass, u, v, CEED_REQUEST_IMMEDIATE);
959ddbf157Sjeremylt   //! [Operator Apply]
9657c64913Sjeremylt 
97*4fee36f0SJeremy L Thompson   {
98*4fee36f0SJeremy L Thompson     const CeedScalar *v_array;
99*4fee36f0SJeremy L Thompson 
100*4fee36f0SJeremy L Thompson     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
1012b730f8bSJeremy L Thompson     for (CeedInt i = 0; i < num_nodes_u; i++) {
102*4fee36f0SJeremy L Thompson       if (fabs(v_array[i]) > 1e-14) printf("[%" CeedInt_FMT "] v %g != 0.0\n", i, v_array[i]);
1032b730f8bSJeremy L Thompson     }
104*4fee36f0SJeremy L Thompson     CeedVectorRestoreArrayRead(v, &v_array);
105*4fee36f0SJeremy L Thompson   }
10657c64913Sjeremylt 
107*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&x);
108*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&u);
109*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&v);
110*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&q_data);
111*4fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_x);
112*4fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_u);
113*4fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_q_data);
114*4fee36f0SJeremy L Thompson   CeedBasisDestroy(&basis_x);
115*4fee36f0SJeremy L Thompson   CeedBasisDestroy(&basis_u);
11657c64913Sjeremylt   CeedQFunctionDestroy(&qf_setup);
11757c64913Sjeremylt   CeedQFunctionDestroy(&qf_mass);
11857c64913Sjeremylt   CeedOperatorDestroy(&op_setup);
11957c64913Sjeremylt   CeedOperatorDestroy(&op_mass);
12057c64913Sjeremylt   CeedDestroy(&ceed);
12157c64913Sjeremylt   return 0;
12257c64913Sjeremylt }
123