xref: /libCEED/tests/t590-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 "t590-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, u, v;
16*48acf710SJeremy L Thompson   CeedElemRestriction elem_restriction_x_points, elem_restriction_u;
17*48acf710SJeremy L Thompson   CeedBasis           basis_u;
18*48acf710SJeremy L Thompson   CeedQFunction       qf_mass;
19*48acf710SJeremy L Thompson   CeedOperator        op_mass;
20*48acf710SJeremy L Thompson 
21*48acf710SJeremy L Thompson   CeedInit(argv[1], &ceed);
22*48acf710SJeremy L Thompson 
23*48acf710SJeremy L Thompson   CeedVectorCreate(ceed, dim * num_points, &x_points);
24*48acf710SJeremy L Thompson   {
25*48acf710SJeremy L Thompson     CeedScalar x_array[dim * num_points];
26*48acf710SJeremy L Thompson 
27*48acf710SJeremy L Thompson     for (CeedInt e = 0; e < num_elem; e++) {
28*48acf710SJeremy L Thompson       for (CeedInt d = 0; d < dim; d++) {
29*48acf710SJeremy L Thompson         x_array[num_points_per_elem * (e * dim + d) + 0] = 0.25;
30*48acf710SJeremy L Thompson         x_array[num_points_per_elem * (e * dim + d) + 1] = d == 0 ? -0.25 : 0.25;
31*48acf710SJeremy L Thompson         x_array[num_points_per_elem * (e * dim + d) + 2] = d == 0 ? 0.25 : -0.25;
32*48acf710SJeremy L Thompson         x_array[num_points_per_elem * (e * dim + d) + 3] = 0.25;
33*48acf710SJeremy L Thompson       }
34*48acf710SJeremy L Thompson     }
35*48acf710SJeremy L Thompson     CeedVectorSetArray(x_points, CEED_MEM_HOST, CEED_COPY_VALUES, x_array);
36*48acf710SJeremy L Thompson   }
37*48acf710SJeremy L Thompson   {
38*48acf710SJeremy L Thompson     CeedInt ind_x[num_elem + 1 + num_points];
39*48acf710SJeremy L Thompson 
40*48acf710SJeremy L Thompson     for (CeedInt i = 0; i <= num_elem; i++) ind_x[i] = num_elem + 1 + i * num_points_per_elem;
41*48acf710SJeremy L Thompson     for (CeedInt i = 0; i < num_points; i++) ind_x[num_elem + 1 + i] = i;
42*48acf710SJeremy L Thompson     CeedElemRestrictionCreateAtPoints(ceed, num_elem, num_points, dim, num_points * dim, CEED_MEM_HOST, CEED_COPY_VALUES, ind_x,
43*48acf710SJeremy L Thompson                                       &elem_restriction_x_points);
44*48acf710SJeremy L Thompson   }
45*48acf710SJeremy L Thompson 
46*48acf710SJeremy L Thompson   {
47*48acf710SJeremy L Thompson     CeedInt ind_u[num_elem * p * p];
48*48acf710SJeremy L Thompson 
49*48acf710SJeremy L Thompson     for (CeedInt e = 0; e < num_elem; e++) {
50*48acf710SJeremy L Thompson       CeedInt elem_xy[2] = {1, 1}, n_d[2] = {0, 0};
51*48acf710SJeremy L Thompson 
52*48acf710SJeremy L Thompson       for (CeedInt d = 0; d < dim; d++) n_d[d] = num_elem_1d * (p - 1) + 1;
53*48acf710SJeremy L Thompson       {
54*48acf710SJeremy L Thompson         CeedInt r_e = e;
55*48acf710SJeremy L Thompson 
56*48acf710SJeremy L Thompson         for (CeedInt d = 0; d < dim; d++) {
57*48acf710SJeremy L Thompson           elem_xy[d] = r_e % num_elem_1d;
58*48acf710SJeremy L Thompson           r_e /= num_elem_1d;
59*48acf710SJeremy L Thompson         }
60*48acf710SJeremy L Thompson       }
61*48acf710SJeremy L Thompson       CeedInt num_nodes_in_elem = p * p, *elem_nodes = ind_u + e * num_nodes_in_elem;
62*48acf710SJeremy L Thompson 
63*48acf710SJeremy L Thompson       for (CeedInt n = 0; n < num_nodes_in_elem; n++) {
64*48acf710SJeremy L Thompson         CeedInt g_node = 0, g_node_stride = 1, r_node = n;
65*48acf710SJeremy L Thompson 
66*48acf710SJeremy L Thompson         for (CeedInt d = 0; d < dim; d++) {
67*48acf710SJeremy L Thompson           g_node += (elem_xy[d] * (p - 1) + r_node % p) * g_node_stride;
68*48acf710SJeremy L Thompson           g_node_stride *= n_d[d];
69*48acf710SJeremy L Thompson           r_node /= p;
70*48acf710SJeremy L Thompson         }
71*48acf710SJeremy L Thompson         elem_nodes[n] = g_node;
72*48acf710SJeremy L Thompson       }
73*48acf710SJeremy L Thompson     }
74*48acf710SJeremy L Thompson     CeedElemRestrictionCreate(ceed, num_elem, p * p, 1, 1, num_nodes, CEED_MEM_HOST, CEED_COPY_VALUES, ind_u, &elem_restriction_u);
75*48acf710SJeremy L Thompson   }
76*48acf710SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, dim, 1, p, q, CEED_GAUSS, &basis_u);
77*48acf710SJeremy L Thompson 
78*48acf710SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass);
79*48acf710SJeremy L Thompson   CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP);
80*48acf710SJeremy L Thompson   CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP);
81*48acf710SJeremy L Thompson 
82*48acf710SJeremy L Thompson   CeedOperatorCreateAtPoints(ceed, qf_mass, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_mass);
83*48acf710SJeremy L Thompson   CeedOperatorSetField(op_mass, "u", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
84*48acf710SJeremy L Thompson   CeedOperatorSetField(op_mass, "v", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
85*48acf710SJeremy L Thompson   CeedOperatorAtPointsSetPoints(op_mass, elem_restriction_x_points, x_points);
86*48acf710SJeremy L Thompson 
87*48acf710SJeremy L Thompson   CeedVectorCreate(ceed, num_nodes, &u);
88*48acf710SJeremy L Thompson   CeedVectorSetValue(u, 1.0);
89*48acf710SJeremy L Thompson   CeedVectorCreate(ceed, num_nodes, &v);
90*48acf710SJeremy L Thompson   CeedOperatorApply(op_mass, u, v, CEED_REQUEST_IMMEDIATE);
91*48acf710SJeremy L Thompson 
92*48acf710SJeremy L Thompson   {
93*48acf710SJeremy L Thompson     CeedScalar        sum = 0.0;
94*48acf710SJeremy L Thompson     const CeedScalar *v_array;
95*48acf710SJeremy L Thompson 
96*48acf710SJeremy L Thompson     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
97*48acf710SJeremy L Thompson     for (CeedInt i = 0; i < num_nodes; i++) sum += v_array[i];
98*48acf710SJeremy L Thompson     CeedVectorRestoreArrayRead(v, &v_array);
99*48acf710SJeremy L Thompson     // Summing 9 reference elements, each 2x2 => 36 sq units area
100*48acf710SJeremy L Thompson     if (fabs(sum - 4.0 * num_elem) > CEED_EPSILON * 5e3) printf("Incorrect area computed, %f != %f\n", sum, 4.0 * num_elem);
101*48acf710SJeremy L Thompson   }
102*48acf710SJeremy L Thompson 
103*48acf710SJeremy L Thompson   CeedVectorDestroy(&x_points);
104*48acf710SJeremy L Thompson   CeedVectorDestroy(&u);
105*48acf710SJeremy L Thompson   CeedVectorDestroy(&v);
106*48acf710SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_x_points);
107*48acf710SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_u);
108*48acf710SJeremy L Thompson   CeedBasisDestroy(&basis_u);
109*48acf710SJeremy L Thompson   CeedQFunctionDestroy(&qf_mass);
110*48acf710SJeremy L Thompson   CeedOperatorDestroy(&op_mass);
111*48acf710SJeremy L Thompson   CeedDestroy(&ceed);
112*48acf710SJeremy L Thompson   return 0;
113*48acf710SJeremy L Thompson }
114