xref: /libCEED/tests/t581-operator.c (revision 49a40c8a2d720db341b0b117b89656b473cbebfb)
1 /// @file
2 /// Test creation, action, and destruction for mass matrix operator using a trivial oriented element restriction (see t510)
3 /// \test Test creation, action, and destruction for mass matrix operator using a trivial oriented element restriction
4 #include "t510-operator.h"
5 
6 #include <ceed.h>
7 #include <math.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 
11 #include "t320-basis.h"
12 
13 int main(int argc, char **argv) {
14   Ceed                ceed;
15   CeedElemRestriction elem_restriction_x, elem_restriction_u, elem_restriction_q_data;
16   CeedBasis           basis_x, basis_u;
17   CeedQFunction       qf_setup, qf_mass;
18   CeedOperator        op_setup, op_mass;
19   CeedVector          q_data, x, u, v;
20   CeedInt             num_elem = 12, dim = 2, p = 6, q = 4;
21   CeedInt             nx = 3, ny = 2;
22   CeedInt             row, col, offset;
23   CeedInt             num_dofs = (nx * 2 + 1) * (ny * 2 + 1), num_qpts = num_elem * q;
24   CeedInt             ind_x[num_elem * p];
25   bool                orients_u[num_elem * p];
26   CeedScalar          q_ref[dim * q], q_weight[q];
27   CeedScalar          interp[p * q], grad[dim * p * q];
28 
29   CeedInit(argv[1], &ceed);
30 
31   CeedVectorCreate(ceed, dim * num_dofs, &x);
32   {
33     CeedScalar x_array[dim * num_dofs];
34 
35     for (CeedInt i = 0; i < num_dofs; i++) {
36       x_array[i]            = (1. / (nx * 2)) * (CeedScalar)(i % (nx * 2 + 1));
37       x_array[i + num_dofs] = (1. / (ny * 2)) * (CeedScalar)(i / (nx * 2 + 1));
38     }
39     CeedVectorSetArray(x, CEED_MEM_HOST, CEED_COPY_VALUES, x_array);
40   }
41   CeedVectorCreate(ceed, num_qpts, &q_data);
42   CeedVectorCreate(ceed, num_dofs, &u);
43   CeedVectorCreate(ceed, num_dofs, &v);
44 
45   // Restrictions
46   for (CeedInt i = 0; i < num_elem / 2; i++) {
47     col    = i % nx;
48     row    = i / nx;
49     offset = col * 2 + row * (nx * 2 + 1) * 2;
50 
51     ind_x[i * 2 * p + 0] = 2 + offset;
52     ind_x[i * 2 * p + 1] = 9 + offset;
53     ind_x[i * 2 * p + 2] = 16 + offset;
54     ind_x[i * 2 * p + 3] = 1 + offset;
55     ind_x[i * 2 * p + 4] = 8 + offset;
56     ind_x[i * 2 * p + 5] = 0 + offset;
57 
58     ind_x[i * 2 * p + 6]  = 14 + offset;
59     ind_x[i * 2 * p + 7]  = 7 + offset;
60     ind_x[i * 2 * p + 8]  = 0 + offset;
61     ind_x[i * 2 * p + 9]  = 15 + offset;
62     ind_x[i * 2 * p + 10] = 8 + offset;
63     ind_x[i * 2 * p + 11] = 16 + offset;
64 
65     for (CeedInt j = 0; j < 12; j++) {
66       orients_u[i * 2 * p + j] = false;
67     }
68   }
69   CeedElemRestrictionCreate(ceed, num_elem, p, dim, num_dofs, dim * num_dofs, CEED_MEM_HOST, CEED_USE_POINTER, ind_x, &elem_restriction_x);
70   CeedElemRestrictionCreateOriented(ceed, num_elem, p, 1, 1, num_dofs, CEED_MEM_HOST, CEED_USE_POINTER, ind_x, orients_u, &elem_restriction_u);
71 
72   CeedInt strides_q_data[3] = {1, q, q};
73   CeedElemRestrictionCreateStrided(ceed, num_elem, q, 1, num_qpts, strides_q_data, &elem_restriction_q_data);
74 
75   // Bases
76   Build2DSimplex(q_ref, q_weight, interp, grad);
77   CeedBasisCreateH1(ceed, CEED_TOPOLOGY_TRIANGLE, dim, p, q, interp, grad, q_ref, q_weight, &basis_x);
78 
79   Build2DSimplex(q_ref, q_weight, interp, grad);
80   CeedBasisCreateH1(ceed, CEED_TOPOLOGY_TRIANGLE, 1, p, q, interp, grad, q_ref, q_weight, &basis_u);
81 
82   // QFunctions
83   CeedQFunctionCreateInterior(ceed, 1, setup, setup_loc, &qf_setup);
84   CeedQFunctionAddInput(qf_setup, "weight", 1, CEED_EVAL_WEIGHT);
85   CeedQFunctionAddInput(qf_setup, "dx", dim * dim, CEED_EVAL_GRAD);
86   CeedQFunctionAddOutput(qf_setup, "rho", 1, CEED_EVAL_NONE);
87 
88   CeedQFunctionCreateInterior(ceed, 1, mass, mass_loc, &qf_mass);
89   CeedQFunctionAddInput(qf_mass, "rho", 1, CEED_EVAL_NONE);
90   CeedQFunctionAddInput(qf_mass, "u", 1, CEED_EVAL_INTERP);
91   CeedQFunctionAddOutput(qf_mass, "v", 1, CEED_EVAL_INTERP);
92 
93   // Operators
94   CeedOperatorCreate(ceed, qf_setup, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_setup);
95   CeedOperatorSetField(op_setup, "weight", CEED_ELEMRESTRICTION_NONE, basis_x, CEED_VECTOR_NONE);
96   CeedOperatorSetField(op_setup, "dx", elem_restriction_x, basis_x, CEED_VECTOR_ACTIVE);
97   CeedOperatorSetField(op_setup, "rho", elem_restriction_q_data, CEED_BASIS_NONE, CEED_VECTOR_ACTIVE);
98 
99   CeedOperatorCreate(ceed, qf_mass, CEED_QFUNCTION_NONE, CEED_QFUNCTION_NONE, &op_mass);
100   CeedOperatorSetField(op_mass, "rho", elem_restriction_q_data, CEED_BASIS_NONE, q_data);
101   CeedOperatorSetField(op_mass, "u", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
102   CeedOperatorSetField(op_mass, "v", elem_restriction_u, basis_u, CEED_VECTOR_ACTIVE);
103 
104   CeedOperatorApply(op_setup, x, q_data, CEED_REQUEST_IMMEDIATE);
105 
106   CeedVectorSetValue(u, 0.0);
107   CeedOperatorApply(op_mass, u, v, CEED_REQUEST_IMMEDIATE);
108 
109   // Check output
110   {
111     const CeedScalar *v_array;
112 
113     CeedVectorGetArrayRead(v, CEED_MEM_HOST, &v_array);
114     for (CeedInt i = 0; i < num_dofs; i++) {
115       if (fabs(v_array[i]) > 1e-14) printf("[%" CeedInt_FMT "] v %g != 0.0\n", i, v_array[i]);
116     }
117     CeedVectorRestoreArrayRead(v, &v_array);
118   }
119 
120   CeedVectorDestroy(&x);
121   CeedVectorDestroy(&u);
122   CeedVectorDestroy(&v);
123   CeedVectorDestroy(&q_data);
124   CeedElemRestrictionDestroy(&elem_restriction_u);
125   CeedElemRestrictionDestroy(&elem_restriction_x);
126   CeedElemRestrictionDestroy(&elem_restriction_q_data);
127   CeedBasisDestroy(&basis_u);
128   CeedBasisDestroy(&basis_x);
129   CeedQFunctionDestroy(&qf_setup);
130   CeedQFunctionDestroy(&qf_mass);
131   CeedOperatorDestroy(&op_setup);
132   CeedOperatorDestroy(&op_mass);
133   CeedDestroy(&ceed);
134   return 0;
135 }
136