xref: /libCEED/tests/t503-operator.c (revision 5d1e906964b04cb5161d672bdc4191311737c811)
1361afa9aSjeremylt /// @file
2361afa9aSjeremylt /// Test creation, action, and destruction for mass matrix operator with passive inputs and outputs
3361afa9aSjeremylt /// \test Test creation, action, and destruction for mass matrix operator with passive inputs and outputs
4361afa9aSjeremylt #include <ceed.h>
5361afa9aSjeremylt #include <math.h>
649aac155SJeremy L Thompson #include <stdio.h>
72b730f8bSJeremy L Thompson #include <stdlib.h>
8361afa9aSjeremylt 
9a05f9790Sjeremylt #include "t500-operator.h"
10361afa9aSjeremylt 
main(int argc,char ** argv)11361afa9aSjeremylt int main(int argc, char **argv) {
12361afa9aSjeremylt   Ceed                ceed;
134fee36f0SJeremy L Thompson   CeedElemRestriction elem_restriction_x, elem_restriction_u, elem_restriction_q_data;
14d1d35e2fSjeremylt   CeedBasis           basis_x, basis_u;
15361afa9aSjeremylt   CeedQFunction       qf_setup, qf_mass;
16361afa9aSjeremylt   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];
21361afa9aSjeremylt 
22361afa9aSjeremylt   CeedInit(argv[1], &ceed);
23361afa9aSjeremylt 
24361afa9aSjeremylt   // Vectors
254fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_nodes_x, &x);
264fee36f0SJeremy L Thompson   {
274fee36f0SJeremy L Thompson     CeedScalar x_array[num_nodes_x];
28361afa9aSjeremylt 
294fee36f0SJeremy L Thompson     for (CeedInt i = 0; i < num_nodes_x; i++) x_array[i] = (CeedScalar)i / (num_nodes_x - 1);
304fee36f0SJeremy L Thompson     CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, x_array);
314fee36f0SJeremy L Thompson   }
324fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_nodes_u, &u);
334fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_nodes_u, &v);
344fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_elem * q, &q_data);
35361afa9aSjeremylt 
36361afa9aSjeremylt   // Restrictions
37d1d35e2fSjeremylt   for (CeedInt i = 0; i < num_elem; i++) {
38d1d35e2fSjeremylt     ind_x[2 * i + 0] = i;
39d1d35e2fSjeremylt     ind_x[2 * i + 1] = i + 1;
40361afa9aSjeremylt   }
414fee36f0SJeremy L Thompson   CeedElemRestrictionCreate(ceed, num_elem, 2, 1, 1, num_nodes_x, CEED_MEM_HOST, CEED_USE_POINTER, ind_x, &elem_restriction_x);
42361afa9aSjeremylt 
43d1d35e2fSjeremylt   for (CeedInt i = 0; i < num_elem; i++) {
444fee36f0SJeremy L Thompson     for (CeedInt j = 0; j < p; j++) {
454fee36f0SJeremy L Thompson       ind_u[p * i + j] = i * (p - 1) + j;
46361afa9aSjeremylt     }
47361afa9aSjeremylt   }
484fee36f0SJeremy L Thompson   CeedElemRestrictionCreate(ceed, num_elem, p, 1, 1, num_nodes_u, CEED_MEM_HOST, CEED_USE_POINTER, ind_u, &elem_restriction_u);
494fee36f0SJeremy L Thompson 
504fee36f0SJeremy L Thompson   CeedInt strides_q_data[3] = {1, q, q};
514fee36f0SJeremy L Thompson   CeedElemRestrictionCreateStrided(ceed, num_elem, q, 1, q * num_elem, strides_q_data, &elem_restriction_q_data);
52361afa9aSjeremylt 
53361afa9aSjeremylt   // Bases
544fee36f0SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, 2, q, CEED_GAUSS, &basis_x);
554fee36f0SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, p, q, CEED_GAUSS, &basis_u);
56361afa9aSjeremylt 
57361afa9aSjeremylt   // QFunctions
58361afa9aSjeremylt   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup);
59a61c78d6SJeremy L Thompson   CeedQFunctionAddInput(qf_setup, "weight", 1, CEED_EVAL_WEIGHT);
60361afa9aSjeremylt   CeedQFunctionAddInput(qf_setup, "dx", 1, CEED_EVAL_GRAD);
61361afa9aSjeremylt   CeedQFunctionAddOutput(qf_setup, "rho", 1, CEED_EVAL_NONE);
62361afa9aSjeremylt 
63361afa9aSjeremylt   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass);
64361afa9aSjeremylt   CeedQFunctionAddInput(qf_mass, "rho", 1, CEED_EVAL_NONE);
65361afa9aSjeremylt   CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP);
66361afa9aSjeremylt   CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP);
67361afa9aSjeremylt 
68361afa9aSjeremylt   // Operators
692b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_setup, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_setup);
704fee36f0SJeremy L Thompson   CeedOperatorSetField(op_setup, "weight", CEED_ELEMRESTRICTION_NONE, basis_x, CEED_VECTOR_NONE);
714fee36f0SJeremy L Thompson   CeedOperatorSetField(op_setup, "dx", elem_restriction_x, basis_x, x);
72*356036faSJeremy L Thompson   CeedOperatorSetField(op_setup, "rho", elem_restriction_q_data, CEED_BASIS_NONE, q_data);
73361afa9aSjeremylt 
742b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_mass, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_mass);
75*356036faSJeremy L Thompson   CeedOperatorSetField(op_mass, "rho", elem_restriction_q_data, CEED_BASIS_NONE, q_data);
764fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass, "u", elem_restriction_u, basis_u, u);
774fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass, "v", elem_restriction_u, basis_u, v);
78361afa9aSjeremylt 
79361afa9aSjeremylt   // Note - It is atypical to use only passive fields; this test is intended
80361afa9aSjeremylt   //   as a test for all passive input modes rather than as an example.
812b730f8bSJeremy L Thompson   CeedOperatorApply(op_setup, CEED_VECTOR_NONE, CEED_VECTOR_NONE, CEED_REQUEST_IMMEDIATE);
824fee36f0SJeremy L Thompson   CeedVectorSetValue(u, 1.0);
832b730f8bSJeremy L Thompson   CeedOperatorApply(op_mass, CEED_VECTOR_NONE, CEED_VECTOR_NONE, CEED_REQUEST_IMMEDIATE);
84361afa9aSjeremylt 
85361afa9aSjeremylt   // Check output
864fee36f0SJeremy L Thompson   {
874fee36f0SJeremy L Thompson     const CeedScalar *v_array;
884fee36f0SJeremy L Thompson     CeedScalar        sum = 0.;
89361afa9aSjeremylt 
904fee36f0SJeremy L Thompson     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
914fee36f0SJeremy L Thompson     for (CeedInt i = 0; i < num_nodes_u; i++) sum += v_array[i];
924fee36f0SJeremy L Thompson     CeedVectorRestoreArrayRead(v, &v_array);
934fee36f0SJeremy L Thompson     if (fabs(sum - 1.) > 1000. * CEED_EPSILON) printf("Computed Area: %f != True Area: 1.0\n", sum);
944fee36f0SJeremy L Thompson   }
954fee36f0SJeremy L Thompson 
964fee36f0SJeremy L Thompson   CeedVectorDestroy(&x);
974fee36f0SJeremy L Thompson   CeedVectorDestroy(&u);
984fee36f0SJeremy L Thompson   CeedVectorDestroy(&v);
994fee36f0SJeremy L Thompson   CeedVectorDestroy(&q_data);
1004fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_u);
1014fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_x);
1024fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_q_data);
1034fee36f0SJeremy L Thompson   CeedBasisDestroy(&basis_u);
1044fee36f0SJeremy L Thompson   CeedBasisDestroy(&basis_x);
105361afa9aSjeremylt   CeedQFunctionDestroy(&qf_setup);
106361afa9aSjeremylt   CeedQFunctionDestroy(&qf_mass);
107361afa9aSjeremylt   CeedOperatorDestroy(&op_setup);
108361afa9aSjeremylt   CeedOperatorDestroy(&op_mass);
109361afa9aSjeremylt   CeedDestroy(&ceed);
110361afa9aSjeremylt   return 0;
111361afa9aSjeremylt }
112