xref: /libCEED/tests/t505-operator.c (revision 4fee36f0a30516a0b5ad51bf7eb3b32d83efd623)
1cae8b89aSjeremylt /// @file
2cae8b89aSjeremylt /// Test CeedOperatorApplyAdd
3cae8b89aSjeremylt /// \test Test CeedOperatorApplyAdd
4cae8b89aSjeremylt #include <ceed.h>
5cae8b89aSjeremylt #include <math.h>
62b730f8bSJeremy L Thompson #include <stdlib.h>
7cae8b89aSjeremylt 
8cae8b89aSjeremylt #include "t500-operator.h"
9cae8b89aSjeremylt 
10cae8b89aSjeremylt int main(int argc, char **argv) {
11cae8b89aSjeremylt   Ceed                ceed;
12*4fee36f0SJeremy L Thompson   CeedElemRestriction elem_restriction_x, elem_restriction_u, elem_restriction_q_data;
13d1d35e2fSjeremylt   CeedBasis           basis_x, basis_u;
14cae8b89aSjeremylt   CeedQFunction       qf_setup, qf_mass;
15cae8b89aSjeremylt   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];
20cae8b89aSjeremylt 
21cae8b89aSjeremylt   CeedInit(argv[1], &ceed);
22cae8b89aSjeremylt 
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     for (CeedInt i = 0; i < num_nodes_x; i++) x_array[i] = (CeedScalar)i / (num_nodes_x - 1);
27*4fee36f0SJeremy L Thompson     CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, x_array);
28*4fee36f0SJeremy L Thompson   }
29*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_nodes_u, &u);
30*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_nodes_u, &v);
31*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, num_elem * q, &q_data);
32*4fee36f0SJeremy L Thompson 
33*4fee36f0SJeremy L Thompson   // Restrictions
34d1d35e2fSjeremylt   for (CeedInt i = 0; i < num_elem; i++) {
35d1d35e2fSjeremylt     ind_x[2 * i + 0] = i;
36d1d35e2fSjeremylt     ind_x[2 * i + 1] = i + 1;
37cae8b89aSjeremylt   }
38*4fee36f0SJeremy L Thompson   CeedElemRestrictionCreate(ceed, num_elem, 2, 1, 1, num_nodes_x, CEED_MEM_HOST, CEED_USE_POINTER, ind_x, &elem_restriction_x);
3915910d16Sjeremylt 
40d1d35e2fSjeremylt   for (CeedInt i = 0; i < num_elem; i++) {
41*4fee36f0SJeremy L Thompson     for (CeedInt j = 0; j < p; j++) {
42*4fee36f0SJeremy L Thompson       ind_u[p * i + j] = i * (p - 1) + j;
43cae8b89aSjeremylt     }
44cae8b89aSjeremylt   }
45*4fee36f0SJeremy L Thompson   CeedElemRestrictionCreate(ceed, num_elem, p, 1, 1, num_nodes_u, CEED_MEM_HOST, CEED_USE_POINTER, ind_u, &elem_restriction_u);
46*4fee36f0SJeremy L Thompson 
47*4fee36f0SJeremy L Thompson   CeedInt strides_q_data[3] = {1, q, q};
48*4fee36f0SJeremy L Thompson   CeedElemRestrictionCreateStrided(ceed, num_elem, q, 1, q * num_elem, strides_q_data, &elem_restriction_q_data);
49cae8b89aSjeremylt 
50cae8b89aSjeremylt   // Bases
51*4fee36f0SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, 2, q, CEED_GAUSS, &basis_x);
52*4fee36f0SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, p, q, CEED_GAUSS, &basis_u);
53cae8b89aSjeremylt 
54cae8b89aSjeremylt   // QFunctions
55cae8b89aSjeremylt   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup);
56a61c78d6SJeremy L Thompson   CeedQFunctionAddInput(qf_setup, "weight", 1, CEED_EVAL_WEIGHT);
57cae8b89aSjeremylt   CeedQFunctionAddInput(qf_setup, "dx", 1, CEED_EVAL_GRAD);
58cae8b89aSjeremylt   CeedQFunctionAddOutput(qf_setup, "rho", 1, CEED_EVAL_NONE);
59cae8b89aSjeremylt 
60cae8b89aSjeremylt   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass);
61cae8b89aSjeremylt   CeedQFunctionAddInput(qf_mass, "rho", 1, CEED_EVAL_NONE);
62cae8b89aSjeremylt   CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP);
63cae8b89aSjeremylt   CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP);
64cae8b89aSjeremylt 
65cae8b89aSjeremylt   // Operators
662b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_setup, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_setup);
67*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_setup, "weight", CEED_ELEMRESTRICTION_NONE, basis_x, CEED_VECTOR_NONE);
68*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_setup, "dx", elem_restriction_x, basis_x, CEED_VECTOR_ACTIVE);
69*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_setup, "rho", elem_restriction_q_data, CEED_BASIS_COLLOCATED, CEED_VECTOR_ACTIVE);
70cae8b89aSjeremylt 
712b730f8bSJeremy L Thompson   CeedOperatorCreate(ceed, qf_mass, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_mass);
72*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass, "rho", elem_restriction_q_data, CEED_BASIS_COLLOCATED, q_data);
73*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass, "u", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
74*4fee36f0SJeremy L Thompson   CeedOperatorSetField(op_mass, "v", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
75cae8b89aSjeremylt 
76*4fee36f0SJeremy L Thompson   CeedOperatorApply(op_setup, x, q_data, CEED_REQUEST_IMMEDIATE);
77cae8b89aSjeremylt 
78cae8b89aSjeremylt   // Apply with V = 0
79*4fee36f0SJeremy L Thompson   CeedVectorSetValue(u, 1.0);
80*4fee36f0SJeremy L Thompson   CeedVectorSetValue(v, 0.0);
81*4fee36f0SJeremy L Thompson   CeedOperatorApplyAdd(op_mass, u, v, CEED_REQUEST_IMMEDIATE);
82cae8b89aSjeremylt 
83cae8b89aSjeremylt   // Check output
84*4fee36f0SJeremy L Thompson   {
85*4fee36f0SJeremy L Thompson     const CeedScalar *v_array;
86*4fee36f0SJeremy L Thompson     CeedScalar        sum = 0.;
87*4fee36f0SJeremy L Thompson 
88*4fee36f0SJeremy L Thompson     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
89*4fee36f0SJeremy L Thompson     for (CeedInt i = 0; i < num_nodes_u; i++) sum += v_array[i];
90*4fee36f0SJeremy L Thompson     CeedVectorRestoreArrayRead(v, &v_array);
912b730f8bSJeremy L Thompson     if (fabs(sum - 1.) > 1000. * CEED_EPSILON) printf("Computed Area: %f != True Area: 1.0\n", sum);
92*4fee36f0SJeremy L Thompson   }
93cae8b89aSjeremylt 
94cae8b89aSjeremylt   // Apply with V = 1
95*4fee36f0SJeremy L Thompson   CeedVectorSetValue(v, 1.0);
96*4fee36f0SJeremy L Thompson   CeedOperatorApplyAdd(op_mass, u, v, CEED_REQUEST_IMMEDIATE);
97cae8b89aSjeremylt 
98cae8b89aSjeremylt   // Check output
99*4fee36f0SJeremy L Thompson   {
100*4fee36f0SJeremy L Thompson     const CeedScalar *v_array;
101*4fee36f0SJeremy L Thompson     CeedScalar        sum = -num_nodes_u;
102cae8b89aSjeremylt 
103*4fee36f0SJeremy L Thompson     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
104*4fee36f0SJeremy L Thompson     for (CeedInt i = 0; i < num_nodes_u; i++) sum += v_array[i];
105*4fee36f0SJeremy L Thompson     CeedVectorRestoreArrayRead(v, &v_array);
106*4fee36f0SJeremy L Thompson     if (fabs(sum - 1.) > 1000. * CEED_EPSILON) printf("Computed Area: %f != True Area: 1.0\n", sum);
107*4fee36f0SJeremy L Thompson   }
108*4fee36f0SJeremy L Thompson 
109*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&x);
110*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&u);
111*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&v);
112*4fee36f0SJeremy L Thompson   CeedVectorDestroy(&q_data);
113*4fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_u);
114*4fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_x);
115*4fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_q_data);
116*4fee36f0SJeremy L Thompson   CeedBasisDestroy(&basis_u);
117*4fee36f0SJeremy L Thompson   CeedBasisDestroy(&basis_x);
118cae8b89aSjeremylt   CeedQFunctionDestroy(&qf_setup);
119cae8b89aSjeremylt   CeedQFunctionDestroy(&qf_mass);
120cae8b89aSjeremylt   CeedOperatorDestroy(&op_setup);
121cae8b89aSjeremylt   CeedOperatorDestroy(&op_mass);
122cae8b89aSjeremylt   CeedDestroy(&ceed);
123cae8b89aSjeremylt   return 0;
124cae8b89aSjeremylt }
125