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