xref: /libCEED/tests/t501-operator.c (revision 4fee36f0a30516a0b5ad51bf7eb3b32d83efd623)
1a8de75f0Sjeremylt /// @file
2361afa9aSjeremylt /// Test creation, action, and destruction for mass matrix operator
3361afa9aSjeremylt /// \test Test creation, action, and destruction for mass matrix operator
4a8de75f0Sjeremylt #include <ceed.h>
5a8de75f0Sjeremylt #include <math.h>
62b730f8bSJeremy L Thompson #include <stdlib.h>
7a8de75f0Sjeremylt 
8a05f9790Sjeremylt #include "t500-operator.h"
9a8de75f0Sjeremylt 
10a8de75f0Sjeremylt int main(int argc, char **argv) {
11a8de75f0Sjeremylt   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];
20a8de75f0Sjeremylt 
21a8de75f0Sjeremylt   CeedInit(argv[1], &ceed);
22288c0443SJeremy L Thompson 
23*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_nodes_x, &x);
24*4fee36f0SJeremy L Thompson   {
25*4fee36f0SJeremy L Thompson     CeedScalar x_array[num_nodes_x];
26*4fee36f0SJeremy L Thompson 
27*4fee36f0SJeremy L Thompson     for (CeedInt i = 0; i < num_nodes_x; i++) x_array[i] = (CeedScalar)i / (num_nodes_x - 1);
28*4fee36f0SJeremy L Thompson     CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, x_array);
29*4fee36f0SJeremy L Thompson   }
30*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_nodes_u, &u);
31*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_nodes_u, &v);
32*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_elem * q, &q_data);
33*4fee36f0SJeremy L Thompson 
34*4fee36f0SJeremy L Thompson   // Restrictions
35d1d35e2fSjeremylt   for (CeedInt i = 0; i < num_elem; i++) {
36d1d35e2fSjeremylt     ind_x[2 * i + 0] = i;
37d1d35e2fSjeremylt     ind_x[2 * i + 1] = i + 1;
38a8de75f0Sjeremylt   }
39*4fee36f0SJeremy L Thompson   CeedElemRestrictionCreate(ceed, num_elem, 2, 1, 1, num_nodes_x, CEED_MEM_HOST, CEED_USE_POINTER, ind_x, &elem_restriction_x);
40a8de75f0Sjeremylt 
41d1d35e2fSjeremylt   for (CeedInt i = 0; i < num_elem; i++) {
42*4fee36f0SJeremy L Thompson     for (CeedInt j = 0; j < p; j++) {
43*4fee36f0SJeremy L Thompson       ind_u[p * i + j] = i * (p - 1) + j;
44a8de75f0Sjeremylt     }
45a8de75f0Sjeremylt   }
46*4fee36f0SJeremy L Thompson   CeedElemRestrictionCreate(ceed, num_elem, p, 1, 1, num_nodes_u, CEED_MEM_HOST, CEED_USE_POINTER, ind_u, &elem_restriction_u);
47*4fee36f0SJeremy L Thompson 
48*4fee36f0SJeremy L Thompson   CeedInt strides_q_data[3] = {1, q, q};
49*4fee36f0SJeremy L Thompson   CeedElemRestrictionCreateStrided(ceed, num_elem, q, 1, q * num_elem, strides_q_data, &elem_restriction_q_data);
50a8de75f0Sjeremylt 
51a8de75f0Sjeremylt   // Bases
52*4fee36f0SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, 2, q, CEED_GAUSS, &basis_x);
53*4fee36f0SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, p, q, CEED_GAUSS, &basis_u);
54a8de75f0Sjeremylt 
55a8de75f0Sjeremylt   // QFunctions
564d537eeaSYohann   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup);
57a61c78d6SJeremy L Thompson   CeedQFunctionAddInput(qf_setup, "weight", 1, CEED_EVAL_WEIGHT);
584d537eeaSYohann   CeedQFunctionAddInput(qf_setup, "dx", 1, CEED_EVAL_GRAD);
59a8de75f0Sjeremylt   CeedQFunctionAddOutput(qf_setup, "rho", 1, CEED_EVAL_NONE);
60a8de75f0Sjeremylt 
614d537eeaSYohann   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass);
62a8de75f0Sjeremylt   CeedQFunctionAddInput(qf_mass, "rho", 1, CEED_EVAL_NONE);
63a8de75f0Sjeremylt   CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP);
64a8de75f0Sjeremylt   CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP);
65a8de75f0Sjeremylt 
66a8de75f0Sjeremylt   // Operators
672b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_setup, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_setup);
68*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_setup, "weight", CEED_ELEMRESTRICTION_NONE, basis_x, CEED_VECTOR_NONE);
69*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_setup, "dx", elem_restriction_x, basis_x, CEED_VECTOR_ACTIVE);
70*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_setup, "rho", elem_restriction_q_data, CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
71a8de75f0Sjeremylt 
722b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_mass, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_mass);
73*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass, "rho", elem_restriction_q_data, CEED_BASIS_COLLOCATED, q_data);
74*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass, "u", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
75*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass, "v", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
76a8de75f0Sjeremylt 
77*4fee36f0SJeremy L Thompson   CeedOperatorApply(op_setup, x, q_data, CEED_REQUEST_IMMEDIATE);
78a8de75f0Sjeremylt 
79*4fee36f0SJeremy L Thompson   CeedVectorSetValue(u, 1.0);
80*4fee36f0SJeremy L Thompson   CeedOperatorApply(op_mass, u, v, CEED_REQUEST_IMMEDIATE);
81a8de75f0Sjeremylt 
82a8de75f0Sjeremylt   // Check output
83*4fee36f0SJeremy L Thompson   {
84*4fee36f0SJeremy L Thompson     const CeedScalar *v_array;
85*4fee36f0SJeremy L Thompson     CeedScalar        sum = 0.;
86a8de75f0Sjeremylt 
87*4fee36f0SJeremy L Thompson     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
88*4fee36f0SJeremy L Thompson     for (CeedInt i = 0; i < num_nodes_u; i++) sum += v_array[i];
89*4fee36f0SJeremy L Thompson     CeedVectorRestoreArrayRead(v, &v_array);
90*4fee36f0SJeremy L Thompson     if (fabs(sum - 1.) > 1000. * CEED_EPSILON) printf("Computed Area: %f != True Area: 1.0\n", sum);
91*4fee36f0SJeremy L Thompson   }
92*4fee36f0SJeremy L Thompson 
93*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&x);
94*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&u);
95*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&v);
96*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&q_data);
97*4fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_u);
98*4fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_x);
99*4fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_q_data);
100*4fee36f0SJeremy L Thompson   CeedBasisDestroy(&basis_u);
101*4fee36f0SJeremy L Thompson   CeedBasisDestroy(&basis_x);
102a8de75f0Sjeremylt   CeedQFunctionDestroy(&qf_setup);
103a8de75f0Sjeremylt   CeedQFunctionDestroy(&qf_mass);
104a8de75f0Sjeremylt   CeedOperatorDestroy(&op_setup);
105a8de75f0Sjeremylt   CeedOperatorDestroy(&op_mass);
106a8de75f0Sjeremylt   CeedDestroy(&ceed);
107a8de75f0Sjeremylt   return 0;
108a8de75f0Sjeremylt }
109