xref: /libCEED/tests/t591-operator.c (revision 48acf7109003d10be9ba6e2ea1703bbbf207ad3b)
1*48acf710SJeremy L Thompson /// @file
2*48acf710SJeremy L Thompson /// Test creation, action, and destruction for mass matrix operator at points
3*48acf710SJeremy L Thompson /// \test Test creation, action, and destruction for mass matrix operator at points
4*48acf710SJeremy L Thompson #include "t591-operator.h"
5*48acf710SJeremy L Thompson 
6*48acf710SJeremy L Thompson #include <ceed.h>
7*48acf710SJeremy L Thompson #include <math.h>
8*48acf710SJeremy L Thompson #include <stdio.h>
9*48acf710SJeremy L Thompson #include <stdlib.h>
10*48acf710SJeremy L Thompson 
11*48acf710SJeremy L Thompson int main(int argc, char **argv) {
12*48acf710SJeremy L Thompson   Ceed    ceed;
13*48acf710SJeremy L Thompson   CeedInt num_elem_1d = 3, num_elem = num_elem_1d * num_elem_1d, dim = 2, p = 3, q = 5;
14*48acf710SJeremy L Thompson   CeedInt num_nodes = (num_elem_1d * (p - 1) + 1) * (num_elem_1d * (p - 1) + 1), num_points_per_elem = 4, num_points = num_elem * num_points_per_elem;
15*48acf710SJeremy L Thompson   CeedVector          x_points, x_elem, q_data, u, v;
16*48acf710SJeremy L Thompson   CeedElemRestriction elem_restriction_x_points, elem_restriction_q_data, elem_restriction_x, elem_restriction_u;
17*48acf710SJeremy L Thompson   CeedBasis           basis_x, basis_u;
18*48acf710SJeremy L Thompson   CeedQFunction       qf_setup, qf_mass;
19*48acf710SJeremy L Thompson   CeedOperator        op_setup, op_mass;
20*48acf710SJeremy L Thompson 
21*48acf710SJeremy L Thompson   CeedInit(argv[1], &ceed);
22*48acf710SJeremy L Thompson 
23*48acf710SJeremy L Thompson   // Point reference coordinates
24*48acf710SJeremy L Thompson   CeedVectorCreate(ceed, dim * num_points, &x_points);
25*48acf710SJeremy L Thompson   {
26*48acf710SJeremy L Thompson     CeedScalar x_array[dim * num_points];
27*48acf710SJeremy L Thompson 
28*48acf710SJeremy L Thompson     for (CeedInt e = 0; e < num_elem; e++) {
29*48acf710SJeremy L Thompson       for (CeedInt d = 0; d < dim; d++) {
30*48acf710SJeremy L Thompson         x_array[num_points_per_elem * (e * dim + d) + 0] = 0.25;
31*48acf710SJeremy L Thompson         x_array[num_points_per_elem * (e * dim + d) + 1] = d == 0 ? -0.25 : 0.25;
32*48acf710SJeremy L Thompson         x_array[num_points_per_elem * (e * dim + d) + 2] = d == 0 ? 0.25 : -0.25;
33*48acf710SJeremy L Thompson         x_array[num_points_per_elem * (e * dim + d) + 3] = 0.25;
34*48acf710SJeremy L Thompson       }
35*48acf710SJeremy L Thompson     }
36*48acf710SJeremy L Thompson     CeedVectorSetArray(x_points, CEED_MEM_HOST, CEED_COPY_VALUES, x_array);
37*48acf710SJeremy L Thompson   }
38*48acf710SJeremy L Thompson   {
39*48acf710SJeremy L Thompson     CeedInt ind_x[num_elem + 1 + num_points];
40*48acf710SJeremy L Thompson 
41*48acf710SJeremy L Thompson     for (CeedInt i = 0; i <= num_elem; i++) ind_x[i] = num_elem + 1 + i * num_points_per_elem;
42*48acf710SJeremy L Thompson     for (CeedInt i = 0; i < num_points; i++) ind_x[num_elem + 1 + i] = i;
43*48acf710SJeremy L Thompson     CeedElemRestrictionCreateAtPoints(ceed, num_elem, num_points, dim, num_points * dim, CEED_MEM_HOST, CEED_COPY_VALUES, ind_x,
44*48acf710SJeremy L Thompson                                       &elem_restriction_x_points);
45*48acf710SJeremy L Thompson     CeedElemRestrictionCreateAtPoints(ceed, num_elem, num_points, 1, num_points, CEED_MEM_HOST, CEED_COPY_VALUES, ind_x, &elem_restriction_q_data);
46*48acf710SJeremy L Thompson   }
47*48acf710SJeremy L Thompson 
48*48acf710SJeremy L Thompson   // Q data
49*48acf710SJeremy L Thompson   CeedVectorCreate(ceed, num_points, &q_data);
50*48acf710SJeremy L Thompson 
51*48acf710SJeremy L Thompson   // Cell coordinates
52*48acf710SJeremy L Thompson   {
53*48acf710SJeremy L Thompson     CeedInt p = 2, num_nodes = (num_elem_1d * (p - 1) + 1) * (num_elem_1d * (p - 1) + 1);
54*48acf710SJeremy L Thompson     CeedInt ind_x[num_elem * p * p];
55*48acf710SJeremy L Thompson 
56*48acf710SJeremy L Thompson     for (CeedInt e = 0; e < num_elem; e++) {
57*48acf710SJeremy L Thompson       CeedInt elem_xy[2] = {1, 1}, n_d[2] = {0, 0};
58*48acf710SJeremy L Thompson 
59*48acf710SJeremy L Thompson       for (CeedInt d = 0; d < dim; d++) n_d[d] = num_elem_1d * (p - 1) + 1;
60*48acf710SJeremy L Thompson       {
61*48acf710SJeremy L Thompson         CeedInt r_e = e;
62*48acf710SJeremy L Thompson 
63*48acf710SJeremy L Thompson         for (CeedInt d = 0; d < dim; d++) {
64*48acf710SJeremy L Thompson           elem_xy[d] = r_e % num_elem_1d;
65*48acf710SJeremy L Thompson           r_e /= num_elem_1d;
66*48acf710SJeremy L Thompson         }
67*48acf710SJeremy L Thompson       }
68*48acf710SJeremy L Thompson       CeedInt num_nodes_in_elem = p * p, *elem_nodes = ind_x + e * num_nodes_in_elem;
69*48acf710SJeremy L Thompson 
70*48acf710SJeremy L Thompson       for (CeedInt n = 0; n < num_nodes_in_elem; n++) {
71*48acf710SJeremy L Thompson         CeedInt g_node = 0, g_node_stride = 1, r_node = n;
72*48acf710SJeremy L Thompson 
73*48acf710SJeremy L Thompson         for (CeedInt d = 0; d < dim; d++) {
74*48acf710SJeremy L Thompson           g_node += (elem_xy[d] * (p - 1) + r_node % p) * g_node_stride;
75*48acf710SJeremy L Thompson           g_node_stride *= n_d[d];
76*48acf710SJeremy L Thompson           r_node /= p;
77*48acf710SJeremy L Thompson         }
78*48acf710SJeremy L Thompson         elem_nodes[n] = p * g_node;
79*48acf710SJeremy L Thompson       }
80*48acf710SJeremy L Thompson     }
81*48acf710SJeremy L Thompson     CeedElemRestrictionCreate(ceed, num_elem, p * p, dim, 1, dim * num_nodes, CEED_MEM_HOST, CEED_COPY_VALUES, ind_x, &elem_restriction_x);
82*48acf710SJeremy L Thompson     CeedVectorCreate(ceed, dim * num_nodes, &x_elem);
83*48acf710SJeremy L Thompson     {
84*48acf710SJeremy L Thompson       CeedScalar x_array[dim * num_nodes];
85*48acf710SJeremy L Thompson 
86*48acf710SJeremy L Thompson       for (CeedInt i = 0; i <= num_elem_1d; i++) {
87*48acf710SJeremy L Thompson         for (CeedInt j = 0; j <= num_elem_1d; j++) {
88*48acf710SJeremy L Thompson           x_array[(i * (num_elem_1d + 1) + j) * dim + 0] = j;
89*48acf710SJeremy L Thompson           x_array[(i * (num_elem_1d + 1) + j) * dim + 1] = i;
90*48acf710SJeremy L Thompson         }
91*48acf710SJeremy L Thompson       }
92*48acf710SJeremy L Thompson       CeedVectorSetArray(x_elem, CEED_MEM_HOST, CEED_COPY_VALUES, x_array);
93*48acf710SJeremy L Thompson     }
94*48acf710SJeremy L Thompson   }
95*48acf710SJeremy L Thompson 
96*48acf710SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, dim, dim, 2, q, CEED_GAUSS, &basis_x);
97*48acf710SJeremy L Thompson 
98*48acf710SJeremy L Thompson   // Cell solution
99*48acf710SJeremy L Thompson   {
100*48acf710SJeremy L Thompson     CeedInt ind_u[num_elem * p * p];
101*48acf710SJeremy L Thompson 
102*48acf710SJeremy L Thompson     for (CeedInt e = 0; e < num_elem; e++) {
103*48acf710SJeremy L Thompson       CeedInt elem_xy[2] = {1, 1}, n_d[2] = {0, 0};
104*48acf710SJeremy L Thompson 
105*48acf710SJeremy L Thompson       for (CeedInt d = 0; d < dim; d++) n_d[d] = num_elem_1d * (p - 1) + 1;
106*48acf710SJeremy L Thompson       {
107*48acf710SJeremy L Thompson         CeedInt r_e = e;
108*48acf710SJeremy L Thompson 
109*48acf710SJeremy L Thompson         for (CeedInt d = 0; d < dim; d++) {
110*48acf710SJeremy L Thompson           elem_xy[d] = r_e % num_elem_1d;
111*48acf710SJeremy L Thompson           r_e /= num_elem_1d;
112*48acf710SJeremy L Thompson         }
113*48acf710SJeremy L Thompson       }
114*48acf710SJeremy L Thompson       CeedInt num_nodes_in_elem = p * p, *elem_nodes = ind_u + e * num_nodes_in_elem;
115*48acf710SJeremy L Thompson 
116*48acf710SJeremy L Thompson       for (CeedInt n = 0; n < num_nodes_in_elem; n++) {
117*48acf710SJeremy L Thompson         CeedInt g_node = 0, g_node_stride = 1, r_node = n;
118*48acf710SJeremy L Thompson 
119*48acf710SJeremy L Thompson         for (CeedInt d = 0; d < dim; d++) {
120*48acf710SJeremy L Thompson           g_node += (elem_xy[d] * (p - 1) + r_node % p) * g_node_stride;
121*48acf710SJeremy L Thompson           g_node_stride *= n_d[d];
122*48acf710SJeremy L Thompson           r_node /= p;
123*48acf710SJeremy L Thompson         }
124*48acf710SJeremy L Thompson         elem_nodes[n] = g_node;
125*48acf710SJeremy L Thompson       }
126*48acf710SJeremy L Thompson     }
127*48acf710SJeremy L Thompson     CeedElemRestrictionCreate(ceed, num_elem, p * p, 1, 1, num_nodes, CEED_MEM_HOST, CEED_COPY_VALUES, ind_u, &elem_restriction_u);
128*48acf710SJeremy L Thompson   }
129*48acf710SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, dim, 1, p, q, CEED_GAUSS, &basis_u);
130*48acf710SJeremy L Thompson 
131*48acf710SJeremy L Thompson   // Setup geometric scaling
132*48acf710SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup);
133*48acf710SJeremy L Thompson   CeedQFunctionAddInput(qf_setup, "x", dim * dim, CEED_EVAL_GRAD);
134*48acf710SJeremy L Thompson   CeedQFunctionAddInput(qf_setup, "weight", 1, CEED_EVAL_WEIGHT);
135*48acf710SJeremy L Thompson   CeedQFunctionAddOutput(qf_setup, "rho", 1, CEED_EVAL_NONE);
136*48acf710SJeremy L Thompson 
137*48acf710SJeremy L Thompson   CeedOperatorCreateAtPoints(ceed, qf_setup, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_setup);
138*48acf710SJeremy L Thompson   CeedOperatorSetField(op_setup, "x", elem_restriction_x, basis_x, CEED_VECTOR_ACTIVE);
139*48acf710SJeremy L Thompson   CeedOperatorSetField(op_setup, "weight", CEED_ELEMRESTRICTION_NONE, basis_x, CEED_VECTOR_NONE);
140*48acf710SJeremy L Thompson   CeedOperatorSetField(op_setup, "rho", elem_restriction_q_data, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE);
141*48acf710SJeremy L Thompson   CeedOperatorAtPointsSetPoints(op_setup, elem_restriction_x_points, x_points);
142*48acf710SJeremy L Thompson 
143*48acf710SJeremy L Thompson   CeedOperatorApply(op_setup, x_elem, q_data, CEED_REQUEST_IMMEDIATE);
144*48acf710SJeremy L Thompson 
145*48acf710SJeremy L Thompson   // Mass operator
146*48acf710SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass);
147*48acf710SJeremy L Thompson   CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP);
148*48acf710SJeremy L Thompson   CeedQFunctionAddInput(qf_mass, "rho", 1, CEED_EVAL_NONE);
149*48acf710SJeremy L Thompson   CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP);
150*48acf710SJeremy L Thompson 
151*48acf710SJeremy L Thompson   CeedOperatorCreateAtPoints(ceed, qf_mass, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_mass);
152*48acf710SJeremy L Thompson   CeedOperatorSetField(op_mass, "u", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
153*48acf710SJeremy L Thompson   CeedOperatorSetField(op_mass, "rho", elem_restriction_q_data, CEED_BASIS_NONE, q_data);
154*48acf710SJeremy L Thompson   CeedOperatorSetField(op_mass, "v", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
155*48acf710SJeremy L Thompson   CeedOperatorAtPointsSetPoints(op_mass, elem_restriction_x_points, x_points);
156*48acf710SJeremy L Thompson 
157*48acf710SJeremy L Thompson   CeedVectorCreate(ceed, num_nodes, &u);
158*48acf710SJeremy L Thompson   CeedVectorSetValue(u, 1.0);
159*48acf710SJeremy L Thompson   CeedVectorCreate(ceed, num_nodes, &v);
160*48acf710SJeremy L Thompson   CeedOperatorApply(op_mass, u, v, CEED_REQUEST_IMMEDIATE);
161*48acf710SJeremy L Thompson 
162*48acf710SJeremy L Thompson   {
163*48acf710SJeremy L Thompson     CeedScalar        sum = 0.0;
164*48acf710SJeremy L Thompson     const CeedScalar *v_array;
165*48acf710SJeremy L Thompson 
166*48acf710SJeremy L Thompson     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
167*48acf710SJeremy L Thompson     for (CeedInt i = 0; i < num_nodes; i++) sum += v_array[i];
168*48acf710SJeremy L Thompson     CeedVectorRestoreArrayRead(v, &v_array);
169*48acf710SJeremy L Thompson     // Summing 9 reference elements
170*48acf710SJeremy L Thompson     if (fabs(sum - 1.0 * num_elem) > CEED_EPSILON * 5e3) printf("Incorrect area computed, %f != %f\n", sum, 1.0 * num_elem);
171*48acf710SJeremy L Thompson   }
172*48acf710SJeremy L Thompson 
173*48acf710SJeremy L Thompson   CeedVectorDestroy(&x_points);
174*48acf710SJeremy L Thompson   CeedVectorDestroy(&q_data);
175*48acf710SJeremy L Thompson   CeedVectorDestroy(&x_elem);
176*48acf710SJeremy L Thompson   CeedVectorDestroy(&u);
177*48acf710SJeremy L Thompson   CeedVectorDestroy(&v);
178*48acf710SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_x_points);
179*48acf710SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_q_data);
180*48acf710SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_x);
181*48acf710SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_u);
182*48acf710SJeremy L Thompson   CeedBasisDestroy(&basis_x);
183*48acf710SJeremy L Thompson   CeedBasisDestroy(&basis_u);
184*48acf710SJeremy L Thompson   CeedQFunctionDestroy(&qf_setup);
185*48acf710SJeremy L Thompson   CeedQFunctionDestroy(&qf_mass);
186*48acf710SJeremy L Thompson   CeedOperatorDestroy(&op_setup);
187*48acf710SJeremy L Thompson   CeedOperatorDestroy(&op_mass);
188*48acf710SJeremy L Thompson   CeedDestroy(&ceed);
189*48acf710SJeremy L Thompson   return 0;
190*48acf710SJeremy L Thompson }
191