xref: /libCEED/tests/t594-operator.c (revision fb133d4b0da93cd2b1f52e39cd1305dc4c4450c3)
1*fb133d4bSJeremy L Thompson /// @file
2*fb133d4bSJeremy L Thompson /// Test diagonal assembly of mass matrix operator at points
3*fb133d4bSJeremy L Thompson /// \test Test diagonal assembly of mass matrix operator at points
4*fb133d4bSJeremy L Thompson #include <ceed.h>
5*fb133d4bSJeremy L Thompson #include <math.h>
6*fb133d4bSJeremy L Thompson #include <stdio.h>
7*fb133d4bSJeremy L Thompson #include <stdlib.h>
8*fb133d4bSJeremy L Thompson 
9*fb133d4bSJeremy L Thompson #include "t500-operator.h"
10*fb133d4bSJeremy L Thompson 
11*fb133d4bSJeremy L Thompson int main(int argc, char **argv) {
12*fb133d4bSJeremy L Thompson   Ceed       ceed;
13*fb133d4bSJeremy L Thompson   CeedInt    num_elem = 3, dim = 1, p = 3, q = 5;
14*fb133d4bSJeremy L Thompson   CeedInt    num_nodes_x = num_elem + 1, num_nodes_u = num_elem * (p - 1) + 1, num_points_per_elem = 4, num_points = num_elem * num_points_per_elem;
15*fb133d4bSJeremy L Thompson   CeedInt    ind_x[num_elem * 2], ind_u[num_elem * p], ind_x_points[num_elem + 1 + num_points];
16*fb133d4bSJeremy L Thompson   CeedScalar x_array_mesh[num_nodes_x], x_array_points[num_points], assembled_true[num_nodes_u];
17*fb133d4bSJeremy L Thompson   CeedVector x_points = NULL, x_elem = NULL, q_data = NULL, u = NULL, v = NULL, assembled = NULL;
18*fb133d4bSJeremy L Thompson   CeedElemRestriction elem_restriction_x_points, elem_restriction_q_data, elem_restriction_x, elem_restriction_u;
19*fb133d4bSJeremy L Thompson   CeedBasis           basis_x, basis_u;
20*fb133d4bSJeremy L Thompson   CeedQFunction       qf_setup, qf_mass;
21*fb133d4bSJeremy L Thompson   CeedOperator        op_setup, op_mass;
22*fb133d4bSJeremy L Thompson   bool                is_at_points;
23*fb133d4bSJeremy L Thompson 
24*fb133d4bSJeremy L Thompson   CeedInit(argv[1], &ceed);
25*fb133d4bSJeremy L Thompson 
26*fb133d4bSJeremy L Thompson   // Mesh coordinates
27*fb133d4bSJeremy L Thompson   for (CeedInt i = 0; i < num_nodes_x; i++) x_array_mesh[i] = (CeedScalar)i / (num_nodes_x - 1);
28*fb133d4bSJeremy L Thompson   for (CeedInt i = 0; i < num_elem; i++) {
29*fb133d4bSJeremy L Thompson     ind_x[2 * i + 0] = i;
30*fb133d4bSJeremy L Thompson     ind_x[2 * i + 1] = i + 1;
31*fb133d4bSJeremy L Thompson   }
32*fb133d4bSJeremy L Thompson   CeedElemRestrictionCreate(ceed, num_elem, 2, 1, 1, num_nodes_x, CEED_MEM_HOST, CEED_USE_POINTER, ind_x, &elem_restriction_x);
33*fb133d4bSJeremy L Thompson   CeedVectorCreate(ceed, num_nodes_x, &x_elem);
34*fb133d4bSJeremy L Thompson   CeedVectorSetArray(x_elem, CEED_MEM_HOST, CEED_USE_POINTER, x_array_mesh);
35*fb133d4bSJeremy L Thompson 
36*fb133d4bSJeremy L Thompson   // U mesh
37*fb133d4bSJeremy L Thompson   for (CeedInt i = 0; i < num_elem; i++) {
38*fb133d4bSJeremy L Thompson     for (CeedInt j = 0; j < p; j++) {
39*fb133d4bSJeremy L Thompson       ind_u[p * i + j] = i * (p - 1) + j;
40*fb133d4bSJeremy L Thompson     }
41*fb133d4bSJeremy L Thompson   }
42*fb133d4bSJeremy L Thompson   CeedElemRestrictionCreate(ceed, num_elem, p, 1, 1, num_nodes_u, CEED_MEM_HOST, CEED_USE_POINTER, ind_u, &elem_restriction_u);
43*fb133d4bSJeremy L Thompson 
44*fb133d4bSJeremy L Thompson   // Point reference coordinates
45*fb133d4bSJeremy L Thompson   {
46*fb133d4bSJeremy L Thompson     CeedScalar weight_tmp[num_points_per_elem + 1];
47*fb133d4bSJeremy L Thompson     CeedInt    current_index = 0;
48*fb133d4bSJeremy L Thompson 
49*fb133d4bSJeremy L Thompson     // Use num_points_per_elem + 1 to test non-uniform quadrature
50*fb133d4bSJeremy L Thompson     CeedGaussQuadrature(num_points_per_elem + 1, x_array_points, weight_tmp);
51*fb133d4bSJeremy L Thompson     ind_x_points[0] = num_elem + 1;
52*fb133d4bSJeremy L Thompson     for (CeedInt p = 0; p < num_points_per_elem + 1; p++, current_index++) {
53*fb133d4bSJeremy L Thompson       ind_x_points[num_elem + 1 + current_index] = current_index;
54*fb133d4bSJeremy L Thompson     }
55*fb133d4bSJeremy L Thompson     // Use num_points_per_elem for middle elements
56*fb133d4bSJeremy L Thompson     for (CeedInt e = 1; e < num_elem - 1; e++) {
57*fb133d4bSJeremy L Thompson       CeedGaussQuadrature(num_points_per_elem, &x_array_points[current_index], weight_tmp);
58*fb133d4bSJeremy L Thompson       ind_x_points[e] = num_elem + 1 + current_index;
59*fb133d4bSJeremy L Thompson       for (CeedInt p = 0; p < num_points_per_elem; p++, current_index++) {
60*fb133d4bSJeremy L Thompson         ind_x_points[num_elem + 1 + current_index] = current_index;
61*fb133d4bSJeremy L Thompson       }
62*fb133d4bSJeremy L Thompson     }
63*fb133d4bSJeremy L Thompson     // Use num_points_per_elem - 1 to test non-uniform quadrature
64*fb133d4bSJeremy L Thompson     CeedGaussQuadrature(num_points_per_elem - 1, &x_array_points[current_index], weight_tmp);
65*fb133d4bSJeremy L Thompson     ind_x_points[num_elem - 1] = num_elem + 1 + current_index;
66*fb133d4bSJeremy L Thompson     for (CeedInt p = 0; p < num_points_per_elem - 1; p++, current_index++) {
67*fb133d4bSJeremy L Thompson       ind_x_points[num_elem + 1 + current_index] = current_index;
68*fb133d4bSJeremy L Thompson     }
69*fb133d4bSJeremy L Thompson     ind_x_points[num_elem] = num_elem + 1 + current_index;
70*fb133d4bSJeremy L Thompson 
71*fb133d4bSJeremy L Thompson     CeedVectorCreate(ceed, num_elem * num_points_per_elem, &x_points);
72*fb133d4bSJeremy L Thompson     CeedVectorSetArray(x_points, CEED_MEM_HOST, CEED_USE_POINTER, x_array_points);
73*fb133d4bSJeremy L Thompson     CeedElemRestrictionCreateAtPoints(ceed, num_elem, num_points, 1, num_points, CEED_MEM_HOST, CEED_COPY_VALUES, ind_x_points,
74*fb133d4bSJeremy L Thompson                                       &elem_restriction_x_points);
75*fb133d4bSJeremy L Thompson     CeedElemRestrictionCreateAtPoints(ceed, num_elem, num_points, 1, num_points, CEED_MEM_HOST, CEED_COPY_VALUES, ind_x_points,
76*fb133d4bSJeremy L Thompson                                       &elem_restriction_q_data);
77*fb133d4bSJeremy L Thompson 
78*fb133d4bSJeremy L Thompson     // Q data
79*fb133d4bSJeremy L Thompson     CeedVectorCreate(ceed, num_points, &q_data);
80*fb133d4bSJeremy L Thompson   }
81*fb133d4bSJeremy L Thompson 
82*fb133d4bSJeremy L Thompson   // Basis creation
83*fb133d4bSJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, dim, dim, 2, q, CEED_GAUSS, &basis_x);
84*fb133d4bSJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, dim, 1, p, q, CEED_GAUSS, &basis_u);
85*fb133d4bSJeremy L Thompson 
86*fb133d4bSJeremy L Thompson   // Setup geometric scaling
87*fb133d4bSJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup);
88*fb133d4bSJeremy L Thompson   CeedQFunctionAddInput(qf_setup, "x", dim * dim, CEED_EVAL_GRAD);
89*fb133d4bSJeremy L Thompson   CeedQFunctionAddInput(qf_setup, "weight", 1, CEED_EVAL_WEIGHT);
90*fb133d4bSJeremy L Thompson   CeedQFunctionAddOutput(qf_setup, "rho", 1, CEED_EVAL_NONE);
91*fb133d4bSJeremy L Thompson 
92*fb133d4bSJeremy L Thompson   CeedOperatorCreateAtPoints(ceed, qf_setup, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_setup);
93*fb133d4bSJeremy L Thompson   CeedOperatorSetField(op_setup, "x", elem_restriction_x, basis_x, CEED_VECTOR_ACTIVE);
94*fb133d4bSJeremy L Thompson   CeedOperatorSetField(op_setup, "weight", CEED_ELEMRESTRICTION_NONE, basis_x, CEED_VECTOR_NONE);
95*fb133d4bSJeremy L Thompson   CeedOperatorSetField(op_setup, "rho", elem_restriction_q_data, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE);
96*fb133d4bSJeremy L Thompson   CeedOperatorAtPointsSetPoints(op_setup, elem_restriction_x_points, x_points);
97*fb133d4bSJeremy L Thompson 
98*fb133d4bSJeremy L Thompson   CeedOperatorApply(op_setup, x_elem, q_data, CEED_REQUEST_IMMEDIATE);
99*fb133d4bSJeremy L Thompson 
100*fb133d4bSJeremy L Thompson   // Mass operator
101*fb133d4bSJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass);
102*fb133d4bSJeremy L Thompson   CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP);
103*fb133d4bSJeremy L Thompson   CeedQFunctionAddInput(qf_mass, "rho", 1, CEED_EVAL_NONE);
104*fb133d4bSJeremy L Thompson   CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP);
105*fb133d4bSJeremy L Thompson 
106*fb133d4bSJeremy L Thompson   CeedOperatorCreateAtPoints(ceed, qf_mass, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_mass);
107*fb133d4bSJeremy L Thompson   CeedOperatorSetField(op_mass, "u", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
108*fb133d4bSJeremy L Thompson   CeedOperatorSetField(op_mass, "rho", elem_restriction_q_data, CEED_BASIS_NONE, q_data);
109*fb133d4bSJeremy L Thompson   CeedOperatorSetField(op_mass, "v", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
110*fb133d4bSJeremy L Thompson   CeedOperatorAtPointsSetPoints(op_mass, elem_restriction_x_points, x_points);
111*fb133d4bSJeremy L Thompson 
112*fb133d4bSJeremy L Thompson   CeedOperatorIsAtPoints(op_mass, &is_at_points);
113*fb133d4bSJeremy L Thompson   if (!is_at_points) printf("Error: Operator should be at points\n");
114*fb133d4bSJeremy L Thompson 
115*fb133d4bSJeremy L Thompson   CeedVectorCreate(ceed, num_nodes_u, &u);
116*fb133d4bSJeremy L Thompson   CeedVectorSetValue(u, 0.0);
117*fb133d4bSJeremy L Thompson   CeedVectorCreate(ceed, num_nodes_u, &v);
118*fb133d4bSJeremy L Thompson 
119*fb133d4bSJeremy L Thompson   // Assemble diagonal
120*fb133d4bSJeremy L Thompson   CeedVectorCreate(ceed, num_nodes_u, &assembled);
121*fb133d4bSJeremy L Thompson   CeedOperatorLinearAssembleDiagonal(op_mass, assembled, CEED_REQUEST_IMMEDIATE);
122*fb133d4bSJeremy L Thompson 
123*fb133d4bSJeremy L Thompson   // Manually assemble diagonal
124*fb133d4bSJeremy L Thompson   CeedVectorSetValue(u, 0.0);
125*fb133d4bSJeremy L Thompson   for (CeedInt i = 0; i < num_nodes_u; i++) {
126*fb133d4bSJeremy L Thompson     CeedScalar       *u_array;
127*fb133d4bSJeremy L Thompson     const CeedScalar *v_array;
128*fb133d4bSJeremy L Thompson 
129*fb133d4bSJeremy L Thompson     // Set input
130*fb133d4bSJeremy L Thompson     CeedVectorGetArray(u, CEED_MEM_HOST, &u_array);
131*fb133d4bSJeremy L Thompson     u_array[i] = 1.0;
132*fb133d4bSJeremy L Thompson     if (i) u_array[i - 1] = 0.0;
133*fb133d4bSJeremy L Thompson     CeedVectorRestoreArray(u, &u_array);
134*fb133d4bSJeremy L Thompson 
135*fb133d4bSJeremy L Thompson     // Compute diag entry for DoF i
136*fb133d4bSJeremy L Thompson     CeedOperatorApply(op_mass, u, v, CEED_REQUEST_IMMEDIATE);
137*fb133d4bSJeremy L Thompson 
138*fb133d4bSJeremy L Thompson     // Retrieve entry
139*fb133d4bSJeremy L Thompson     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
140*fb133d4bSJeremy L Thompson     assembled_true[i] = v_array[i];
141*fb133d4bSJeremy L Thompson     CeedVectorRestoreArrayRead(v, &v_array);
142*fb133d4bSJeremy L Thompson   }
143*fb133d4bSJeremy L Thompson 
144*fb133d4bSJeremy L Thompson   // Check output
145*fb133d4bSJeremy L Thompson   {
146*fb133d4bSJeremy L Thompson     const CeedScalar *assembled_array;
147*fb133d4bSJeremy L Thompson 
148*fb133d4bSJeremy L Thompson     CeedVectorGetArrayRead(assembled, CEED_MEM_HOST, &assembled_array);
149*fb133d4bSJeremy L Thompson     for (CeedInt i = 0; i < num_nodes_u; i++) {
150*fb133d4bSJeremy L Thompson       if (fabs(assembled_array[i] - assembled_true[i]) > 100. * CEED_EPSILON) {
151*fb133d4bSJeremy L Thompson         // LCOV_EXCL_START
152*fb133d4bSJeremy L Thompson         printf("[%" CeedInt_FMT "] Error in assembly: %f != %f\n", i, assembled_array[i], assembled_true[i]);
153*fb133d4bSJeremy L Thompson         // LCOV_EXCL_STOP
154*fb133d4bSJeremy L Thompson       }
155*fb133d4bSJeremy L Thompson     }
156*fb133d4bSJeremy L Thompson     CeedVectorRestoreArrayRead(assembled, &assembled_array);
157*fb133d4bSJeremy L Thompson   }
158*fb133d4bSJeremy L Thompson 
159*fb133d4bSJeremy L Thompson   // Cleanup
160*fb133d4bSJeremy L Thompson   CeedVectorDestroy(&q_data);
161*fb133d4bSJeremy L Thompson   CeedVectorDestroy(&u);
162*fb133d4bSJeremy L Thompson   CeedVectorDestroy(&v);
163*fb133d4bSJeremy L Thompson   CeedVectorDestroy(&x_points);
164*fb133d4bSJeremy L Thompson   CeedVectorDestroy(&q_data);
165*fb133d4bSJeremy L Thompson   CeedVectorDestroy(&x_elem);
166*fb133d4bSJeremy L Thompson   CeedVectorDestroy(&assembled);
167*fb133d4bSJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_q_data);
168*fb133d4bSJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_x);
169*fb133d4bSJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_x_points);
170*fb133d4bSJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_u);
171*fb133d4bSJeremy L Thompson   CeedBasisDestroy(&basis_x);
172*fb133d4bSJeremy L Thompson   CeedBasisDestroy(&basis_u);
173*fb133d4bSJeremy L Thompson   CeedQFunctionDestroy(&qf_setup);
174*fb133d4bSJeremy L Thompson   CeedQFunctionDestroy(&qf_mass);
175*fb133d4bSJeremy L Thompson   CeedOperatorDestroy(&op_setup);
176*fb133d4bSJeremy L Thompson   CeedOperatorDestroy(&op_mass);
177*fb133d4bSJeremy L Thompson   CeedDestroy(&ceed);
178*fb133d4bSJeremy L Thompson   return 0;
179*fb133d4bSJeremy L Thompson }
180