xref: /libCEED/tests/t503-operator.c (revision 4fee36f0a30516a0b5ad51bf7eb3b32d83efd623)
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>
62b730f8bSJeremy L Thompson #include <stdlib.h>
7361afa9aSjeremylt 
8a05f9790Sjeremylt #include "t500-operator.h"
9361afa9aSjeremylt 
10361afa9aSjeremylt int main(int argc, char **argv) {
11361afa9aSjeremylt   Ceed                ceed;
12*4fee36f0SJeremy L Thompson   CeedElemRestriction elem_restriction_x, elem_restriction_u, elem_restriction_q_data;
13d1d35e2fSjeremylt   CeedBasis           basis_x, basis_u;
14361afa9aSjeremylt   CeedQFunction       qf_setup, qf_mass;
15361afa9aSjeremylt   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];
20361afa9aSjeremylt 
21361afa9aSjeremylt   CeedInit(argv[1], &ceed);
22361afa9aSjeremylt 
23361afa9aSjeremylt   // Vectors
24*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_nodes_x, &x);
25*4fee36f0SJeremy L Thompson   {
26*4fee36f0SJeremy L Thompson     CeedScalar x_array[num_nodes_x];
27361afa9aSjeremylt 
28*4fee36f0SJeremy L Thompson     for (CeedInt i = 0; i < num_nodes_x; i++) x_array[i] = (CeedScalar)i / (num_nodes_x - 1);
29*4fee36f0SJeremy L Thompson     CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, x_array);
30*4fee36f0SJeremy L Thompson   }
31*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_nodes_u, &u);
32*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_nodes_u, &v);
33*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_elem * q, &q_data);
34361afa9aSjeremylt 
35361afa9aSjeremylt   // Restrictions
36d1d35e2fSjeremylt   for (CeedInt i = 0; i < num_elem; i++) {
37d1d35e2fSjeremylt     ind_x[2 * i + 0] = i;
38d1d35e2fSjeremylt     ind_x[2 * i + 1] = i + 1;
39361afa9aSjeremylt   }
40*4fee36f0SJeremy L Thompson   CeedElemRestrictionCreate(ceed, num_elem, 2, 1, 1, num_nodes_x, CEED_MEM_HOST, CEED_USE_POINTER, ind_x, &elem_restriction_x);
41361afa9aSjeremylt 
42d1d35e2fSjeremylt   for (CeedInt i = 0; i < num_elem; i++) {
43*4fee36f0SJeremy L Thompson     for (CeedInt j = 0; j < p; j++) {
44*4fee36f0SJeremy L Thompson       ind_u[p * i + j] = i * (p - 1) + j;
45361afa9aSjeremylt     }
46361afa9aSjeremylt   }
47*4fee36f0SJeremy L Thompson   CeedElemRestrictionCreate(ceed, num_elem, p, 1, 1, num_nodes_u, CEED_MEM_HOST, CEED_USE_POINTER, ind_u, &elem_restriction_u);
48*4fee36f0SJeremy L Thompson 
49*4fee36f0SJeremy L Thompson   CeedInt strides_q_data[3] = {1, q, q};
50*4fee36f0SJeremy L Thompson   CeedElemRestrictionCreateStrided(ceed, num_elem, q, 1, q * num_elem, strides_q_data, &elem_restriction_q_data);
51361afa9aSjeremylt 
52361afa9aSjeremylt   // Bases
53*4fee36f0SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, 2, q, CEED_GAUSS, &basis_x);
54*4fee36f0SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, p, q, CEED_GAUSS, &basis_u);
55361afa9aSjeremylt 
56361afa9aSjeremylt   // QFunctions
57361afa9aSjeremylt   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup);
58a61c78d6SJeremy L Thompson   CeedQFunctionAddInput(qf_setup, "weight", 1, CEED_EVAL_WEIGHT);
59361afa9aSjeremylt   CeedQFunctionAddInput(qf_setup, "dx", 1, CEED_EVAL_GRAD);
60361afa9aSjeremylt   CeedQFunctionAddOutput(qf_setup, "rho", 1, CEED_EVAL_NONE);
61361afa9aSjeremylt 
62361afa9aSjeremylt   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass);
63361afa9aSjeremylt   CeedQFunctionAddInput(qf_mass, "rho", 1, CEED_EVAL_NONE);
64361afa9aSjeremylt   CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP);
65361afa9aSjeremylt   CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP);
66361afa9aSjeremylt 
67361afa9aSjeremylt   // Operators
682b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_setup, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_setup);
69*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_setup, "weight", CEED_ELEMRESTRICTION_NONE, basis_x, CEED_VECTOR_NONE);
70*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_setup, "dx", elem_restriction_x, basis_x, x);
71*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_setup, "rho", elem_restriction_q_data, CEED_BASIS_COLLOCATED, q_data);
72361afa9aSjeremylt 
732b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_mass, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_mass);
74*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass, "rho", elem_restriction_q_data, CEED_BASIS_COLLOCATED, q_data);
75*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass, "u", elem_restriction_u, basis_u, u);
76*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass, "v", elem_restriction_u, basis_u, v);
77361afa9aSjeremylt 
78361afa9aSjeremylt   // Note - It is atypical to use only passive fields; this test is intended
79361afa9aSjeremylt   //   as a test for all passive input modes rather than as an example.
802b730f8bSJeremy L Thompson   CeedOperatorApply(op_setup, CEED_VECTOR_NONE, CEED_VECTOR_NONE, CEED_REQUEST_IMMEDIATE);
81*4fee36f0SJeremy L Thompson   CeedVectorSetValue(u, 1.0);
822b730f8bSJeremy L Thompson   CeedOperatorApply(op_mass, CEED_VECTOR_NONE, CEED_VECTOR_NONE, CEED_REQUEST_IMMEDIATE);
83361afa9aSjeremylt 
84361afa9aSjeremylt   // Check output
85*4fee36f0SJeremy L Thompson   {
86*4fee36f0SJeremy L Thompson     const CeedScalar *v_array;
87*4fee36f0SJeremy L Thompson     CeedScalar        sum = 0.;
88361afa9aSjeremylt 
89*4fee36f0SJeremy L Thompson     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
90*4fee36f0SJeremy L Thompson     for (CeedInt i = 0; i < num_nodes_u; i++) sum += v_array[i];
91*4fee36f0SJeremy L Thompson     CeedVectorRestoreArrayRead(v, &v_array);
92*4fee36f0SJeremy L Thompson     if (fabs(sum - 1.) > 1000. * CEED_EPSILON) printf("Computed Area: %f != True Area: 1.0\n", sum);
93*4fee36f0SJeremy L Thompson   }
94*4fee36f0SJeremy L Thompson 
95*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&x);
96*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&u);
97*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&v);
98*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&q_data);
99*4fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_u);
100*4fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_x);
101*4fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_q_data);
102*4fee36f0SJeremy L Thompson   CeedBasisDestroy(&basis_u);
103*4fee36f0SJeremy L Thompson   CeedBasisDestroy(&basis_x);
104361afa9aSjeremylt   CeedQFunctionDestroy(&qf_setup);
105361afa9aSjeremylt   CeedQFunctionDestroy(&qf_mass);
106361afa9aSjeremylt   CeedOperatorDestroy(&op_setup);
107361afa9aSjeremylt   CeedOperatorDestroy(&op_mass);
108361afa9aSjeremylt   CeedDestroy(&ceed);
109361afa9aSjeremylt   return 0;
110361afa9aSjeremylt }
111