xref: /libCEED/tests/t507-operator.c (revision 4fee36f0a30516a0b5ad51bf7eb3b32d83efd623)
11da99368SJeremy L Thompson /// @file
21da99368SJeremy L Thompson /// Test VLA macro for operator
31da99368SJeremy L Thompson /// \test VLA marco for operator
41da99368SJeremy L Thompson #include "t507-operator.h"
51da99368SJeremy L Thompson 
62b730f8bSJeremy L Thompson #include <ceed.h>
72b730f8bSJeremy L Thompson #include <math.h>
82b730f8bSJeremy L Thompson #include <stdlib.h>
92b730f8bSJeremy L Thompson 
101da99368SJeremy L Thompson int main(int argc, char **argv) {
111da99368SJeremy L Thompson   Ceed                ceed;
12*4fee36f0SJeremy L Thompson   CeedElemRestriction elem_restriction_x, elem_restriction_u, elem_restriction_q_data;
13d1d35e2fSjeremylt   CeedBasis           basis_x, basis_u;
141da99368SJeremy L Thompson   CeedQFunction       qf_setup, qf_mass;
151da99368SJeremy L Thompson   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];
201da99368SJeremy L Thompson 
211da99368SJeremy L Thompson   CeedInit(argv[1], &ceed);
221da99368SJeremy 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, 2 * num_nodes_u, &u);
31*4fee36f0SJeremy L Thompson   CeedVectorCreate(ceed, 2 * 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;
381da99368SJeremy L Thompson   }
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);
401da99368SJeremy L Thompson 
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] = 2 * (i * (p - 1) + j);
441da99368SJeremy L Thompson     }
451da99368SJeremy L Thompson   }
46*4fee36f0SJeremy L Thompson   CeedElemRestrictionCreate(ceed, num_elem, p, 2, 1, 2 * 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);
501da99368SJeremy L Thompson 
511da99368SJeremy L Thompson   // Bases
52*4fee36f0SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, 1, 1, 2, q, CEED_GAUSS, &basis_x);
53*4fee36f0SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, 1, 2, p, q, CEED_GAUSS, &basis_u);
541da99368SJeremy L Thompson 
551da99368SJeremy L Thompson   // QFunctions
561da99368SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup);
57a61c78d6SJeremy L Thompson   CeedQFunctionAddInput(qf_setup, "weight", 1, CEED_EVAL_WEIGHT);
581da99368SJeremy L Thompson   CeedQFunctionAddInput(qf_setup, "dx", 1 * 1, CEED_EVAL_GRAD);
591da99368SJeremy L Thompson   CeedQFunctionAddOutput(qf_setup, "rho", 1, CEED_EVAL_NONE);
601da99368SJeremy L Thompson 
611da99368SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass);
621da99368SJeremy L Thompson   CeedQFunctionAddInput(qf_mass, "rho", 1, CEED_EVAL_NONE);
631da99368SJeremy L Thompson   CeedQFunctionAddInput(qf_mass, "u", 2, CEED_EVAL_INTERP);
641da99368SJeremy L Thompson   CeedQFunctionAddOutput(qf_mass, "v", 2, CEED_EVAL_INTERP);
651da99368SJeremy L Thompson 
661da99368SJeremy L Thompson   // 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);
711da99368SJeremy L Thompson 
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);
761da99368SJeremy L Thompson 
77*4fee36f0SJeremy L Thompson   CeedOperatorApply(op_setup, x, q_data, CEED_REQUEST_IMMEDIATE);
781da99368SJeremy L Thompson 
79*4fee36f0SJeremy L Thompson   {
80*4fee36f0SJeremy L Thompson     CeedScalar *u_array;
811da99368SJeremy L Thompson 
82*4fee36f0SJeremy L Thompson     CeedVectorGetArrayWrite(u, CEED_MEM_HOST, &u_array);
83d1d35e2fSjeremylt     for (int i = 0; i < num_nodes_u; i++) {
84*4fee36f0SJeremy L Thompson       u_array[2 * i]     = 1.0;
85*4fee36f0SJeremy L Thompson       u_array[2 * i + 1] = 2.0;
861da99368SJeremy L Thompson     }
87*4fee36f0SJeremy L Thompson     CeedVectorRestoreArray(u, &u_array);
88*4fee36f0SJeremy L Thompson   }
89*4fee36f0SJeremy L Thompson 
90*4fee36f0SJeremy L Thompson   CeedOperatorApply(op_mass, u, v, CEED_REQUEST_IMMEDIATE);
911da99368SJeremy L Thompson 
921da99368SJeremy L Thompson   // Check output
93*4fee36f0SJeremy L Thompson   {
94*4fee36f0SJeremy L Thompson     const CeedScalar *v_array;
95*4fee36f0SJeremy L Thompson     CeedScalar        sum_1 = 0., sum_2 = 0.;
96*4fee36f0SJeremy L Thompson 
97*4fee36f0SJeremy L Thompson     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
98d1d35e2fSjeremylt     for (CeedInt i = 0; i < num_nodes_u; i++) {
99*4fee36f0SJeremy L Thompson       sum_1 += v_array[2 * i];
100*4fee36f0SJeremy L Thompson       sum_2 += v_array[2 * i + 1];
1011da99368SJeremy L Thompson     }
102*4fee36f0SJeremy L Thompson     CeedVectorRestoreArrayRead(v, &v_array);
1032b730f8bSJeremy L Thompson     if (fabs(sum_1 - 1.) > 1000. * CEED_EPSILON) printf("Computed Area: %f != True Area: 1.0\n", sum_1);
1042b730f8bSJeremy L Thompson     if (fabs(sum_2 - 2.) > 1000. * CEED_EPSILON) printf("Computed Area: %f != True Area: 2.0\n", sum_2);
105*4fee36f0SJeremy L Thompson   }
1061da99368SJeremy L Thompson 
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_u);
112*4fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_x);
113*4fee36f0SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_q_data);
114*4fee36f0SJeremy L Thompson   CeedBasisDestroy(&basis_u);
115*4fee36f0SJeremy L Thompson   CeedBasisDestroy(&basis_x);
1161da99368SJeremy L Thompson   CeedQFunctionDestroy(&qf_setup);
1171da99368SJeremy L Thompson   CeedQFunctionDestroy(&qf_mass);
1181da99368SJeremy L Thompson   CeedOperatorDestroy(&op_setup);
1191da99368SJeremy L Thompson   CeedOperatorDestroy(&op_mass);
1201da99368SJeremy L Thompson   CeedDestroy(&ceed);
1211da99368SJeremy L Thompson   return 0;
1221da99368SJeremy L Thompson }
123