xref: /libCEED/tests/t590-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 "t590-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, u, v;
1648acf710SJeremy L Thompson   CeedElemRestriction elem_restriction_x_points, elem_restriction_u;
1748acf710SJeremy L Thompson   CeedBasis           basis_u;
1848acf710SJeremy L Thompson   CeedQFunction       qf_mass;
1948acf710SJeremy L Thompson   CeedOperator        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   CeedVectorCreate(ceed, dim * num_points, &x_points);
2548acf710SJeremy L Thompson   {
2648acf710SJeremy L Thompson     CeedScalar x_array[dim * num_points];
2748acf710SJeremy L Thompson 
2848acf710SJeremy L Thompson     for (CeedInt e = 0; e < num_elem; e++) {
2948acf710SJeremy L Thompson       for (CeedInt d = 0; d < dim; d++) {
3048acf710SJeremy L Thompson         x_array[num_points_per_elem * (e * dim + d) + 0] = 0.25;
3148acf710SJeremy L Thompson         x_array[num_points_per_elem * (e * dim + d) + 1] = d == 0 ? -0.25 : 0.25;
3248acf710SJeremy L Thompson         x_array[num_points_per_elem * (e * dim + d) + 2] = d == 0 ? 0.25 : -0.25;
3348acf710SJeremy L Thompson         x_array[num_points_per_elem * (e * dim + d) + 3] = 0.25;
3448acf710SJeremy L Thompson       }
3548acf710SJeremy L Thompson     }
3648acf710SJeremy L Thompson     CeedVectorSetArray(x_points, CEED_MEM_HOST, CEED_COPY_VALUES, x_array);
3748acf710SJeremy L Thompson   }
3848acf710SJeremy L Thompson   {
3948acf710SJeremy L Thompson     CeedInt ind_x[num_elem + 1 + num_points];
4048acf710SJeremy L Thompson 
4148acf710SJeremy L Thompson     for (CeedInt i = 0; i <= num_elem; i++) ind_x[i] = num_elem + 1 + i * num_points_per_elem;
4248acf710SJeremy L Thompson     for (CeedInt i = 0; i < num_points; i++) ind_x[num_elem + 1 + i] = i;
4348acf710SJeremy L Thompson     CeedElemRestrictionCreateAtPoints(ceed, num_elem, num_points, dim, num_points * dim, CEED_MEM_HOST, CEED_COPY_VALUES, ind_x,
4448acf710SJeremy L Thompson                                       &elem_restriction_x_points);
4548acf710SJeremy L Thompson   }
4648acf710SJeremy L Thompson 
4748acf710SJeremy L Thompson   {
4848acf710SJeremy L Thompson     CeedInt ind_u[num_elem * p * p];
4948acf710SJeremy L Thompson 
5048acf710SJeremy L Thompson     for (CeedInt e = 0; e < num_elem; e++) {
5148acf710SJeremy L Thompson       CeedInt elem_xy[2] = {1, 1}, n_d[2] = {0, 0};
5248acf710SJeremy L Thompson 
5348acf710SJeremy L Thompson       for (CeedInt d = 0; d < dim; d++) n_d[d] = num_elem_1d * (p - 1) + 1;
5448acf710SJeremy L Thompson       {
5548acf710SJeremy L Thompson         CeedInt r_e = e;
5648acf710SJeremy L Thompson 
5748acf710SJeremy L Thompson         for (CeedInt d = 0; d < dim; d++) {
5848acf710SJeremy L Thompson           elem_xy[d] = r_e % num_elem_1d;
5948acf710SJeremy L Thompson           r_e /= num_elem_1d;
6048acf710SJeremy L Thompson         }
6148acf710SJeremy L Thompson       }
6248acf710SJeremy L Thompson       CeedInt num_nodes_in_elem = p * p, *elem_nodes = ind_u + e * num_nodes_in_elem;
6348acf710SJeremy L Thompson 
6448acf710SJeremy L Thompson       for (CeedInt n = 0; n < num_nodes_in_elem; n++) {
6548acf710SJeremy L Thompson         CeedInt g_node = 0, g_node_stride = 1, r_node = n;
6648acf710SJeremy L Thompson 
6748acf710SJeremy L Thompson         for (CeedInt d = 0; d < dim; d++) {
6848acf710SJeremy L Thompson           g_node += (elem_xy[d] * (p - 1) + r_node % p) * g_node_stride;
6948acf710SJeremy L Thompson           g_node_stride *= n_d[d];
7048acf710SJeremy L Thompson           r_node /= p;
7148acf710SJeremy L Thompson         }
7248acf710SJeremy L Thompson         elem_nodes[n] = g_node;
7348acf710SJeremy L Thompson       }
7448acf710SJeremy L Thompson     }
7548acf710SJeremy L Thompson     CeedElemRestrictionCreate(ceed, num_elem, p * p, 1, 1, num_nodes, CEED_MEM_HOST, CEED_COPY_VALUES, ind_u, &elem_restriction_u);
7648acf710SJeremy L Thompson   }
7748acf710SJeremy L Thompson   CeedBasisCreateTensorH1Lagrange(ceed, dim, 1, p, q, CEED_GAUSS, &basis_u);
7848acf710SJeremy L Thompson 
7948acf710SJeremy L Thompson   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass);
8048acf710SJeremy L Thompson   CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP);
8148acf710SJeremy L Thompson   CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP);
8248acf710SJeremy L Thompson 
8348acf710SJeremy L Thompson   CeedOperatorCreateAtPoints(ceed, qf_mass, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_mass);
8448acf710SJeremy L Thompson   CeedOperatorSetField(op_mass, "u", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
8548acf710SJeremy L Thompson   CeedOperatorSetField(op_mass, "v", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
8648acf710SJeremy L Thompson   CeedOperatorAtPointsSetPoints(op_mass, elem_restriction_x_points, x_points);
8748acf710SJeremy L Thompson 
88*b594f9faSZach Atkins   CeedOperatorIsAtPoints(op_mass, &is_at_points);
89*b594f9faSZach Atkins   if (!is_at_points) printf("Error: Operator should be at points\n");
90*b594f9faSZach Atkins 
9148acf710SJeremy L Thompson   CeedVectorCreate(ceed, num_nodes, &u);
9248acf710SJeremy L Thompson   CeedVectorSetValue(u, 1.0);
9348acf710SJeremy L Thompson   CeedVectorCreate(ceed, num_nodes, &v);
9448acf710SJeremy L Thompson   CeedOperatorApply(op_mass, u, v, CEED_REQUEST_IMMEDIATE);
9548acf710SJeremy L Thompson 
9648acf710SJeremy L Thompson   {
9748acf710SJeremy L Thompson     CeedScalar        sum = 0.0;
9848acf710SJeremy L Thompson     const CeedScalar *v_array;
9948acf710SJeremy L Thompson 
10048acf710SJeremy L Thompson     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
10148acf710SJeremy L Thompson     for (CeedInt i = 0; i < num_nodes; i++) sum += v_array[i];
10248acf710SJeremy L Thompson     CeedVectorRestoreArrayRead(v, &v_array);
10348acf710SJeremy L Thompson     // Summing 9 reference elements, each 2x2 => 36 sq units area
10448acf710SJeremy L Thompson     if (fabs(sum - 4.0 * num_elem) > CEED_EPSILON * 5e3) printf("Incorrect area computed, %f != %f\n", sum, 4.0 * num_elem);
10548acf710SJeremy L Thompson   }
10648acf710SJeremy L Thompson 
10748acf710SJeremy L Thompson   CeedVectorDestroy(&x_points);
10848acf710SJeremy L Thompson   CeedVectorDestroy(&u);
10948acf710SJeremy L Thompson   CeedVectorDestroy(&v);
11048acf710SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_x_points);
11148acf710SJeremy L Thompson   CeedElemRestrictionDestroy(&elem_restriction_u);
11248acf710SJeremy L Thompson   CeedBasisDestroy(&basis_u);
11348acf710SJeremy L Thompson   CeedQFunctionDestroy(&qf_mass);
11448acf710SJeremy L Thompson   CeedOperatorDestroy(&op_mass);
11548acf710SJeremy L Thompson   CeedDestroy(&ceed);
11648acf710SJeremy L Thompson   return 0;
11748acf710SJeremy L Thompson }
118