xref: /libCEED/tests/t591-operator.c (revision ef7f882e93b9bc65eaaf386e2d001a2e8e95cfc8)
148acf710SJeremy L Thompson /// @file
248acf710SJeremy L Thompson /// Test creation, action, and destruction for mass matrix operator at points
348acf710SJeremy L Thompson /// \test Test creation, action, and destruction for mass matrix operator at points
448acf710SJeremy L Thompson #include "t591-operator.h"
548acf710SJeremy L Thompson 
648acf710SJeremy L Thompson #include <ceed.h>
748acf710SJeremy L Thompson #include <math.h>
848acf710SJeremy L Thompson #include <stdio.h>
948acf710SJeremy L Thompson #include <stdlib.h>
1048acf710SJeremy L Thompson 
main(int argc,char ** argv)1148acf710SJeremy L Thompson int main(int argc, char **argv) {
1248acf710SJeremy L Thompson   Ceed    ceed;
1348acf710SJeremy L Thompson   CeedInt num_elem_1d = 3, num_elem = num_elem_1d * num_elem_1d, dim = 2, p = 3, q = 5;
1448acf710SJeremy 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;
1548acf710SJeremy L Thompson   CeedVector          x_points, x_elem, q_data, u, v;
1648acf710SJeremy L Thompson   CeedElemRestriction elem_restriction_x_points, elem_restriction_q_data, elem_restriction_x, elem_restriction_u;
1748acf710SJeremy L Thompson   CeedBasis           basis_x, basis_u;
1848acf710SJeremy L Thompson   CeedQFunction       qf_setup, qf_mass;
1948acf710SJeremy L Thompson   CeedOperator        op_setup, op_mass;
20*b594f9faSZach Atkins   bool                is_at_points;
2148acf710SJeremy L Thompson 
2248acf710SJeremy L Thompson   CeedInit(argv[1], &ceed);
2348acf710SJeremy L Thompson 
2448acf710SJeremy L Thompson   // Point reference coordinates
2548acf710SJeremy L Thompson   CeedVectorCreate(ceed, dim * num_points, &x_points);
2648acf710SJeremy L Thompson   {
2748acf710SJeremy L Thompson     CeedScalar x_array[dim * num_points];
2848acf710SJeremy L Thompson 
2948acf710SJeremy L Thompson     for (CeedInt e = 0; e < num_elem; e++) {
3048acf710SJeremy L Thompson       for (CeedInt d = 0; d < dim; d++) {
3148acf710SJeremy L Thompson         x_array[num_points_per_elem * (e * dim + d) + 0] = 0.25;
3248acf710SJeremy L Thompson         x_array[num_points_per_elem * (e * dim + d) + 1] = d == 0 ? -0.25 : 0.25;
3348acf710SJeremy L Thompson         x_array[num_points_per_elem * (e * dim + d) + 2] = d == 0 ? 0.25 : -0.25;
3448acf710SJeremy L Thompson         x_array[num_points_per_elem * (e * dim + d) + 3] = 0.25;
3548acf710SJeremy L Thompson       }
3648acf710SJeremy L Thompson     }
3748acf710SJeremy L Thompson     CeedVectorSetArray(x_points, CEED_MEM_HOST, CEED_COPY_VALUES, x_array);
3848acf710SJeremy L Thompson   }
3948acf710SJeremy L Thompson   {
4048acf710SJeremy L Thompson     CeedInt ind_x[num_elem + 1 + num_points];
4148acf710SJeremy L Thompson 
4248acf710SJeremy L Thompson     for (CeedInt i = 0; i <= num_elem; i++) ind_x[i] = num_elem + 1 + i * num_points_per_elem;
4348acf710SJeremy L Thompson     for (CeedInt i = 0; i < num_points; i++) ind_x[num_elem + 1 + i] = i;
4448acf710SJeremy L Thompson     CeedElemRestrictionCreateAtPoints(ceed, num_elem, num_points, dim, num_points * dim, CEED_MEM_HOST, CEED_COPY_VALUES, ind_x,
4548acf710SJeremy L Thompson                                       &elem_restriction_x_points);
4648acf710SJeremy L Thompson     CeedElemRestrictionCreateAtPoints(ceed, num_elem, num_points, 1, num_points, CEED_MEM_HOST, CEED_COPY_VALUES, ind_x, &elem_restriction_q_data);
4748acf710SJeremy L Thompson   }
4848acf710SJeremy L Thompson 
4948acf710SJeremy L Thompson   // Q data
5048acf710SJeremy L Thompson   CeedVectorCreate(ceed, num_points, &q_data);
5148acf710SJeremy L Thompson 
5248acf710SJeremy L Thompson   // Cell coordinates
5348acf710SJeremy L Thompson   {
5448acf710SJeremy L Thompson     CeedInt p = 2, num_nodes = (num_elem_1d * (p - 1) + 1) * (num_elem_1d * (p - 1) + 1);
5548acf710SJeremy L Thompson     CeedInt ind_x[num_elem * p * p];
5648acf710SJeremy L Thompson 
5748acf710SJeremy L Thompson     for (CeedInt e = 0; e < num_elem; e++) {
5848acf710SJeremy L Thompson       CeedInt elem_xy[2] = {1, 1}, n_d[2] = {0, 0};
5948acf710SJeremy L Thompson 
6048acf710SJeremy L Thompson       for (CeedInt d = 0; d < dim; d++) n_d[d] = num_elem_1d * (p - 1) + 1;
6148acf710SJeremy L Thompson       {
6248acf710SJeremy L Thompson         CeedInt r_e = e;
6348acf710SJeremy L Thompson 
6448acf710SJeremy L Thompson         for (CeedInt d = 0; d < dim; d++) {
6548acf710SJeremy L Thompson           elem_xy[d] = r_e % num_elem_1d;
6648acf710SJeremy L Thompson           r_e /= num_elem_1d;
6748acf710SJeremy L Thompson         }
6848acf710SJeremy L Thompson       }
6948acf710SJeremy L Thompson       CeedInt num_nodes_in_elem = p * p, *elem_nodes = ind_x + e * num_nodes_in_elem;
7048acf710SJeremy L Thompson 
7148acf710SJeremy L Thompson       for (CeedInt n = 0; n < num_nodes_in_elem; n++) {
7248acf710SJeremy L Thompson         CeedInt g_node = 0, g_node_stride = 1, r_node = n;
7348acf710SJeremy L Thompson 
7448acf710SJeremy L Thompson         for (CeedInt d = 0; d < dim; d++) {
7548acf710SJeremy L Thompson           g_node += (elem_xy[d] * (p - 1) + r_node % p) * g_node_stride;
7648acf710SJeremy L Thompson           g_node_stride *= n_d[d];
7748acf710SJeremy L Thompson           r_node /= p;
7848acf710SJeremy L Thompson         }
7948acf710SJeremy L Thompson         elem_nodes[n] = p * g_node;
8048acf710SJeremy L Thompson       }
8148acf710SJeremy L Thompson     }
8248acf710SJeremy L Thompson     CeedElemRestrictionCreate(ceed, num_elem, p * p, dim, 1, dim * num_nodes, CEED_MEM_HOST, CEED_COPY_VALUES, ind_x, &elem_restriction_x);
8348acf710SJeremy L Thompson     CeedVectorCreate(ceed, dim * num_nodes, &x_elem);
8448acf710SJeremy L Thompson     {
8548acf710SJeremy L Thompson       CeedScalar x_array[dim * num_nodes];
8648acf710SJeremy L Thompson 
8748acf710SJeremy L Thompson       for (CeedInt i = 0; i <= num_elem_1d; i++) {
8848acf710SJeremy L Thompson         for (CeedInt j = 0; j <= num_elem_1d; j++) {
8948acf710SJeremy L Thompson           x_array[(i * (num_elem_1d + 1) + j) * dim + 0] = j;
9048acf710SJeremy L Thompson           x_array[(i * (num_elem_1d + 1) + j) * dim + 1] = i;
9148acf710SJeremy L Thompson         }
9248acf710SJeremy L Thompson       }
9348acf710SJeremy L Thompson       CeedVectorSetArray(x_elem, CEED_MEM_HOST, CEED_COPY_VALUES, x_array);
9448acf710SJeremy L Thompson     }
9548acf710SJeremy L Thompson   }
9648acf710SJeremy L Thompson 
9748acf710SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, dim, dim, 2, q, CEED_GAUSS, &basis_x);
9848acf710SJeremy L Thompson 
9948acf710SJeremy L Thompson   // Cell solution
10048acf710SJeremy L Thompson   {
10148acf710SJeremy L Thompson     CeedInt ind_u[num_elem * p * p];
10248acf710SJeremy L Thompson 
10348acf710SJeremy L Thompson     for (CeedInt e = 0; e < num_elem; e++) {
10448acf710SJeremy L Thompson       CeedInt elem_xy[2] = {1, 1}, n_d[2] = {0, 0};
10548acf710SJeremy L Thompson 
10648acf710SJeremy L Thompson       for (CeedInt d = 0; d < dim; d++) n_d[d] = num_elem_1d * (p - 1) + 1;
10748acf710SJeremy L Thompson       {
10848acf710SJeremy L Thompson         CeedInt r_e = e;
10948acf710SJeremy L Thompson 
11048acf710SJeremy L Thompson         for (CeedInt d = 0; d < dim; d++) {
11148acf710SJeremy L Thompson           elem_xy[d] = r_e % num_elem_1d;
11248acf710SJeremy L Thompson           r_e /= num_elem_1d;
11348acf710SJeremy L Thompson         }
11448acf710SJeremy L Thompson       }
11548acf710SJeremy L Thompson       CeedInt num_nodes_in_elem = p * p, *elem_nodes = ind_u + e * num_nodes_in_elem;
11648acf710SJeremy L Thompson 
11748acf710SJeremy L Thompson       for (CeedInt n = 0; n < num_nodes_in_elem; n++) {
11848acf710SJeremy L Thompson         CeedInt g_node = 0, g_node_stride = 1, r_node = n;
11948acf710SJeremy L Thompson 
12048acf710SJeremy L Thompson         for (CeedInt d = 0; d < dim; d++) {
12148acf710SJeremy L Thompson           g_node += (elem_xy[d] * (p - 1) + r_node % p) * g_node_stride;
12248acf710SJeremy L Thompson           g_node_stride *= n_d[d];
12348acf710SJeremy L Thompson           r_node /= p;
12448acf710SJeremy L Thompson         }
12548acf710SJeremy L Thompson         elem_nodes[n] = g_node;
12648acf710SJeremy L Thompson       }
12748acf710SJeremy L Thompson     }
12848acf710SJeremy L Thompson     CeedElemRestrictionCreate(ceed, num_elem, p * p, 1, 1, num_nodes, CEED_MEM_HOST, CEED_COPY_VALUES, ind_u, &elem_restriction_u);
12948acf710SJeremy L Thompson   }
13048acf710SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, dim, 1, p, q, CEED_GAUSS, &basis_u);
13148acf710SJeremy L Thompson 
13248acf710SJeremy L Thompson   // Setup geometric scaling
13348acf710SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup);
13448acf710SJeremy L Thompson   CeedQFunctionAddInput(qf_setup, "x", dim * dim, CEED_EVAL_GRAD);
13548acf710SJeremy L Thompson   CeedQFunctionAddInput(qf_setup, "weight", 1, CEED_EVAL_WEIGHT);
13648acf710SJeremy L Thompson   CeedQFunctionAddOutput(qf_setup, "rho", 1, CEED_EVAL_NONE);
13748acf710SJeremy L Thompson 
13848acf710SJeremy L Thompson   CeedOperatorCreateAtPoints(ceed, qf_setup, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_setup);
13948acf710SJeremy L Thompson   CeedOperatorSetField(op_setup, "x", elem_restriction_x, basis_x, CEED_VECTOR_ACTIVE);
14048acf710SJeremy L Thompson   CeedOperatorSetField(op_setup, "weight", CEED_ELEMRESTRICTION_NONE, basis_x, CEED_VECTOR_NONE);
14148acf710SJeremy L Thompson   CeedOperatorSetField(op_setup, "rho", elem_restriction_q_data, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE);
14248acf710SJeremy L Thompson   CeedOperatorAtPointsSetPoints(op_setup, elem_restriction_x_points, x_points);
14348acf710SJeremy L Thompson 
14448acf710SJeremy L Thompson   CeedOperatorApply(op_setup, x_elem, q_data, CEED_REQUEST_IMMEDIATE);
14548acf710SJeremy L Thompson 
14648acf710SJeremy L Thompson   // Mass operator
14748acf710SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass);
14848acf710SJeremy L Thompson   CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP);
14948acf710SJeremy L Thompson   CeedQFunctionAddInput(qf_mass, "rho", 1, CEED_EVAL_NONE);
15048acf710SJeremy L Thompson   CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP);
15148acf710SJeremy L Thompson 
15248acf710SJeremy L Thompson   CeedOperatorCreateAtPoints(ceed, qf_mass, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_mass);
15348acf710SJeremy L Thompson   CeedOperatorSetField(op_mass, "u", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
15448acf710SJeremy L Thompson   CeedOperatorSetField(op_mass, "rho", elem_restriction_q_data, CEED_BASIS_NONE, q_data);
15548acf710SJeremy L Thompson   CeedOperatorSetField(op_mass, "v", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
15648acf710SJeremy L Thompson   CeedOperatorAtPointsSetPoints(op_mass, elem_restriction_x_points, x_points);
15748acf710SJeremy L Thompson 
158*b594f9faSZach Atkins   CeedOperatorIsAtPoints(op_mass, &is_at_points);
159*b594f9faSZach Atkins   if (!is_at_points) printf("Error: Operator should be at points\n");
160*b594f9faSZach Atkins 
16148acf710SJeremy L Thompson   CeedVectorCreate(ceed, num_nodes, &u);
16248acf710SJeremy L Thompson   CeedVectorSetValue(u, 1.0);
16348acf710SJeremy L Thompson   CeedVectorCreate(ceed, num_nodes, &v);
16448acf710SJeremy L Thompson   CeedOperatorApply(op_mass, u, v, CEED_REQUEST_IMMEDIATE);
16548acf710SJeremy L Thompson 
16648acf710SJeremy L Thompson   {
16748acf710SJeremy L Thompson     CeedScalar        sum = 0.0;
16848acf710SJeremy L Thompson     const CeedScalar *v_array;
16948acf710SJeremy L Thompson 
17048acf710SJeremy L Thompson     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
17148acf710SJeremy L Thompson     for (CeedInt i = 0; i < num_nodes; i++) sum += v_array[i];
17248acf710SJeremy L Thompson     CeedVectorRestoreArrayRead(v, &v_array);
17348acf710SJeremy L Thompson     // Summing 9 reference elements
17448acf710SJeremy L Thompson     if (fabs(sum - 1.0 * num_elem) > CEED_EPSILON * 5e3) printf("Incorrect area computed, %f != %f\n", sum, 1.0 * num_elem);
17548acf710SJeremy L Thompson   }
17648acf710SJeremy L Thompson 
17748acf710SJeremy L Thompson   CeedVectorDestroy(&x_points);
17848acf710SJeremy L Thompson   CeedVectorDestroy(&q_data);
17948acf710SJeremy L Thompson   CeedVectorDestroy(&x_elem);
18048acf710SJeremy L Thompson   CeedVectorDestroy(&u);
18148acf710SJeremy L Thompson   CeedVectorDestroy(&v);
18248acf710SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_x_points);
18348acf710SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_q_data);
18448acf710SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_x);
18548acf710SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_u);
18648acf710SJeremy L Thompson   CeedBasisDestroy(&basis_x);
18748acf710SJeremy L Thompson   CeedBasisDestroy(&basis_u);
18848acf710SJeremy L Thompson   CeedQFunctionDestroy(&qf_setup);
18948acf710SJeremy L Thompson   CeedQFunctionDestroy(&qf_mass);
19048acf710SJeremy L Thompson   CeedOperatorDestroy(&op_setup);
19148acf710SJeremy L Thompson   CeedOperatorDestroy(&op_mass);
19248acf710SJeremy L Thompson   CeedDestroy(&ceed);
19348acf710SJeremy L Thompson   return 0;
19448acf710SJeremy L Thompson }
195