xref: /libCEED/tests/t595-operator.c (revision e9c76bddc0f2a44f522e0176ed6b7e0c0aa1df73)
1 /// @file
2 /// Test FLOP estimation for mass matrix operator at points
3 /// \test Test FLOP estimation for mass matrix operator at points
4 #include "t595-operator.h"
5 
6 #include <ceed.h>
7 #include <math.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 
11 int main(int argc, char **argv) {
12   Ceed    ceed;
13   CeedInt num_elem_1d = 3, num_elem = num_elem_1d * num_elem_1d, dim = 2, p = 3, q = 5;
14   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   CeedSize            flop_estimate = 0;
16   CeedVector          x_points, q_data;
17   CeedElemRestriction elem_restriction_x_points, elem_restriction_q_data, elem_restriction_u;
18   CeedBasis           basis_x, basis_u;
19   CeedQFunction       qf_mass;
20   CeedOperator        op_mass;
21   bool                is_at_points;
22 
23   CeedInit(argv[1], &ceed);
24 
25   // Point reference coordinates
26   CeedVectorCreate(ceed, dim * num_points, &x_points);
27   {
28     CeedScalar x_array[dim * num_points];
29 
30     for (CeedInt e = 0; e < num_elem; e++) {
31       for (CeedInt d = 0; d < dim; d++) {
32         x_array[num_points_per_elem * (e * dim + d) + 0] = 0.25;
33         x_array[num_points_per_elem * (e * dim + d) + 1] = d == 0 ? -0.25 : 0.25;
34         x_array[num_points_per_elem * (e * dim + d) + 2] = d == 0 ? 0.25 : -0.25;
35         x_array[num_points_per_elem * (e * dim + d) + 3] = 0.25;
36       }
37     }
38     CeedVectorSetArray(x_points, CEED_MEM_HOST, CEED_COPY_VALUES, x_array);
39   }
40   {
41     CeedInt ind_x[num_elem + 1 + num_points];
42 
43     for (CeedInt i = 0; i <= num_elem; i++) ind_x[i] = num_elem + 1 + i * num_points_per_elem;
44     for (CeedInt i = 0; i < num_points; i++) ind_x[num_elem + 1 + i] = i;
45     CeedElemRestrictionCreateAtPoints(ceed, num_elem, num_points, dim, num_points * dim, CEED_MEM_HOST, CEED_COPY_VALUES, ind_x,
46                                       &elem_restriction_x_points);
47     CeedElemRestrictionCreateAtPoints(ceed, num_elem, num_points, 1, num_points, CEED_MEM_HOST, CEED_COPY_VALUES, ind_x, &elem_restriction_q_data);
48   }
49 
50   // Q data
51   CeedVectorCreate(ceed, num_points, &q_data);
52 
53   CeedBasisCreateTensorH1Lagrange(ceed, dim, dim, 2, q, CEED_GAUSS, &basis_x);
54 
55   // Cell solution
56   {
57     CeedInt ind_u[num_elem * p * p];
58 
59     for (CeedInt e = 0; e < num_elem; e++) {
60       CeedInt elem_xy[2] = {1, 1}, n_d[2] = {0, 0};
61 
62       for (CeedInt d = 0; d < dim; d++) n_d[d] = num_elem_1d * (p - 1) + 1;
63       {
64         CeedInt r_e = e;
65 
66         for (CeedInt d = 0; d < dim; d++) {
67           elem_xy[d] = r_e % num_elem_1d;
68           r_e /= num_elem_1d;
69         }
70       }
71       CeedInt num_nodes_in_elem = p * p, *elem_nodes = ind_u + e * num_nodes_in_elem;
72 
73       for (CeedInt n = 0; n < num_nodes_in_elem; n++) {
74         CeedInt g_node = 0, g_node_stride = 1, r_node = n;
75 
76         for (CeedInt d = 0; d < dim; d++) {
77           g_node += (elem_xy[d] * (p - 1) + r_node % p) * g_node_stride;
78           g_node_stride *= n_d[d];
79           r_node /= p;
80         }
81         elem_nodes[n] = g_node;
82       }
83     }
84     CeedElemRestrictionCreate(ceed, num_elem, p * p, 1, 1, num_nodes, CEED_MEM_HOST, CEED_COPY_VALUES, ind_u, &elem_restriction_u);
85   }
86   CeedBasisCreateTensorH1Lagrange(ceed, dim, 1, p, q, CEED_GAUSS, &basis_u);
87 
88   // Mass operator
89   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass);
90   CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP);
91   CeedQFunctionAddInput(qf_mass, "rho", 1, CEED_EVAL_NONE);
92   CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP);
93 
94   CeedOperatorCreateAtPoints(ceed, qf_mass, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_mass);
95   CeedOperatorSetField(op_mass, "u", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
96   CeedOperatorSetField(op_mass, "rho", elem_restriction_q_data, CEED_BASIS_NONE, q_data);
97   CeedOperatorSetField(op_mass, "v", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
98   CeedOperatorAtPointsSetPoints(op_mass, elem_restriction_x_points, x_points);
99 
100   CeedOperatorIsAtPoints(op_mass, &is_at_points);
101   if (!is_at_points) printf("Error: Operator should be at points\n");
102 
103   // Estimate FLOPs
104   CeedQFunctionSetUserFlopsEstimate(qf_mass, 1);
105   CeedOperatorGetFlopsEstimate(op_mass, &flop_estimate);
106 
107   // Check output
108   if (flop_estimate != 16317) {
109     // LCOV_EXCL_START
110     printf("Incorrect FLOP estimate computed, %ld != 16317\n", flop_estimate);
111     // LCOV_EXCL_STOP
112   }
113 
114   CeedVectorDestroy(&x_points);
115   CeedVectorDestroy(&q_data);
116   CeedElemRestrictionDestroy(&elem_restriction_x_points);
117   CeedElemRestrictionDestroy(&elem_restriction_q_data);
118   CeedElemRestrictionDestroy(&elem_restriction_u);
119   CeedBasisDestroy(&basis_x);
120   CeedBasisDestroy(&basis_u);
121   CeedQFunctionDestroy(&qf_mass);
122   CeedOperatorDestroy(&op_mass);
123   CeedDestroy(&ceed);
124   return 0;
125 }
126