-
Notifications
You must be signed in to change notification settings - Fork 55
/
TestAbstractFunctionalCalculator.hpp
239 lines (202 loc) · 8.62 KB
/
TestAbstractFunctionalCalculator.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
Copyright (c) 2005-2024, University of Oxford.
All rights reserved.
University of Oxford means the Chancellor, Masters and Scholars of the
University of Oxford, having an administrative office at Wellington
Square, Oxford OX1 2JD, UK.
This file is part of Chaste.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the University of Oxford nor the names of its
contributors may be used to endorse or promote products derived from this
software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef TESTABSTRACTFUNCTIONALCALCULATOR_HPP_
#define TESTABSTRACTFUNCTIONALCALCULATOR_HPP_
#include <cxxtest/TestSuite.h>
#include "AbstractFunctionalCalculator.hpp"
#include "TetrahedralMesh.hpp"
#include "DistributedTetrahedralMesh.hpp"
#include "TrianglesMeshReader.hpp"
#include "PetscTools.hpp"
#include "PetscSetupAndFinalize.hpp"
#include "DistributedVector.hpp"
/* HOW_TO_TAG PDE
* Evaluate integrals (using a solution from a PDE solve say) over a finite element mesh
*/
/*
* Returns 1.0 everywhere so that the total integral over the mesh of
* this integrand is just the volume of the mesh. For testing.
*/
template<unsigned DIM>
class VolumeCalculator : public AbstractFunctionalCalculator<DIM,DIM,1>
{
double GetIntegrand(ChastePoint<DIM>& rX,
c_vector<double,1>& rU,
c_matrix<double,1,DIM>& rGradU)
{
return 1.0;
}
};
// Check x and u are interpolated correctly
class ExampleFunctionalOne : public AbstractFunctionalCalculator<2,2,2>
{
double GetIntegrand(ChastePoint<2>& rX,
c_vector<double,2>& rU,
c_matrix<double,2,2>& rGradU)
{
return rX[0]*rU[0] + rX[1]*rU[1];
}
};
// Check grad_u is interpolated correctly
class ExampleFunctionalTwo : public AbstractFunctionalCalculator<2,2,2>
{
double GetIntegrand(ChastePoint<2>& rX,
c_vector<double,2>& rU,
c_matrix<double,2,2>& rGradU)
{
return rX[0]*rU[0] + rX[1]*rU[1] + 0.5*(rGradU(0,0)+rGradU(0,1)+rGradU(1,0)+rGradU(1,1));
}
};
// Check higher order polynomial are integrated correctly
class ExampleFunctionalThree : public AbstractFunctionalCalculator<2,2,2>
{
double GetIntegrand(ChastePoint<2>& rX,
c_vector<double,2>& rU,
c_matrix<double,2,2>& rGradU)
{
return rX[0]*rX[1]*rU[1];
}
};
class TestAbstractFunctionalCalculator : public CxxTest::TestSuite
{
public:
void TestWithVolumeCalculator()
{
TrianglesMeshReader<2,2> reader("mesh/test/data/square_128_elements");
TetrahedralMesh<2,2> mesh;
mesh.ConstructFromMeshReader(reader);
DistributedTetrahedralMesh<2,2> distributed_mesh;
distributed_mesh.ConstructFromMeshReader(reader);
VolumeCalculator<2> volume_calculator;
Vec vec = PetscTools::CreateAndSetVec(mesh.GetNumNodes(), 0.0);
double result = volume_calculator.Calculate(mesh,vec);
TS_ASSERT_DELTA(result, mesh.GetVolume(), 1e-12);
double distributed_result = volume_calculator.Calculate(distributed_mesh,vec);
TS_ASSERT_DELTA(result, distributed_result, 1e-12);
Vec bad_vec = PetscTools::CreateAndSetVec(mesh.GetNumNodes()+1, 0.0);
TS_ASSERT_THROWS_THIS(volume_calculator.Calculate(mesh,bad_vec),"The solution size does not match the mesh");
PetscTools::Destroy(vec);
PetscTools::Destroy(bad_vec);
}
void TestWithExampleFunctionals()
{
TrianglesMeshReader<2,2> reader("mesh/test/data/square_128_elements");
DistributedTetrahedralMesh<2,2> mesh;
mesh.ConstructFromMeshReader(reader);
// Test interpolation of x and u
// Integrate x^2 + 2y over the unit square
// = 4/3
ExampleFunctionalOne calculator;
DistributedVectorFactory* p_factory = mesh.GetDistributedVectorFactory();
Vec petsc_vec = p_factory->CreateVec(2);
DistributedVector vec1 = p_factory->CreateDistributedVector(petsc_vec);
DistributedVector::Stripe u1(vec1, 0);
DistributedVector::Stripe v1(vec1, 1);
for (DistributedVector::Iterator index = vec1.Begin();
index != vec1.End();
++index)
{
Node<2>* p_node = mesh.GetNode(index.Global);
u1[index] = p_node->rGetLocation()[0];
v1[index] = 2.0;
}
vec1.Restore();
double result = calculator.Calculate(mesh, petsc_vec);
TS_ASSERT_DELTA(result, 4.0/3.0, 1e-14);
// Test interpolation of grad_u
// Integrate x^2 + y^2 + 1 over the unit square
// = 5/3
ExampleFunctionalTwo other_calculator;
DistributedVector vec2 = p_factory->CreateDistributedVector(petsc_vec);
DistributedVector::Stripe u2(vec2, 0);
DistributedVector::Stripe v2(vec2, 1);
for (DistributedVector::Iterator index = vec2.Begin();
index != vec2.End();
++index)
{
Node<2>* p_node = mesh.GetNode(index.Global);
u2[index] = p_node->rGetLocation()[0];
v2[index] = p_node->rGetLocation()[1];
}
vec2.Restore();
result = other_calculator.Calculate(mesh, petsc_vec);
TS_ASSERT_DELTA(result, 1 + 2.0/3.0, 1e-14);
// Test cubic integration
// Integrate x*y^2 over unit square = 1/6
ExampleFunctionalThree higher_order_calculator;
TS_ASSERT_DELTA(higher_order_calculator.Calculate(mesh, petsc_vec), 1.0/6.0, 1e-14);
PetscTools::Destroy(petsc_vec);
}
void TestWithExampleFunctionalsNonDistributed()
{
TrianglesMeshReader<2,2> reader("mesh/test/data/square_128_elements");
TetrahedralMesh<2,2> mesh;
mesh.ConstructFromMeshReader(reader);
// Test interpolation of x and u
// Integrate x^2 + 2y over the unit square
// = 4/3
ExampleFunctionalOne calculator;
DistributedVectorFactory* p_factory = mesh.GetDistributedVectorFactory();
Vec petsc_vec = p_factory->CreateVec(2);
DistributedVector vec1 = p_factory->CreateDistributedVector(petsc_vec);
DistributedVector::Stripe u1(vec1, 0);
DistributedVector::Stripe v1(vec1, 1);
for (DistributedVector::Iterator index = vec1.Begin();
index != vec1.End();
++index)
{
Node<2>* p_node = mesh.GetNode(index.Global);
u1[index] = p_node->rGetLocation()[0];
v1[index] = 2.0;
}
vec1.Restore();
double result = calculator.Calculate(mesh, petsc_vec);
TS_ASSERT_DELTA(result, 4.0/3.0, 1e-14);
// Test interpolation of grad_u
// Integrate x^2 + y^2 + 1 over the unit square
// = 5/3
ExampleFunctionalTwo other_calculator;
DistributedVector vec2 = p_factory->CreateDistributedVector(petsc_vec);
DistributedVector::Stripe u2(vec2, 0);
DistributedVector::Stripe v2(vec2, 1);
for (DistributedVector::Iterator index = vec2.Begin();
index != vec2.End();
++index)
{
Node<2>* p_node = mesh.GetNode(index.Global);
u2[index] = p_node->rGetLocation()[0];
v2[index] = p_node->rGetLocation()[1];
}
vec2.Restore();
result = other_calculator.Calculate(mesh, petsc_vec);
TS_ASSERT_DELTA(result, 1 + 2.0/3.0, 1e-14);
PetscTools::Destroy(petsc_vec);
}
};
#endif /*TESTABSTRACTFUNCTIONALCALCULATOR_HPP_*/