COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
utest-vector-benchmark.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010-2011 von Karman Institute for Fluid Dynamics, Belgium
2 //
3 // This software is distributed under the terms of the
4 // GNU Lesser General Public License version 3 (LGPLv3).
5 // See doc/lgpl.txt and doc/gpl.txt for the license text.
6 
7 #define BOOST_TEST_DYN_LINK
8 #define BOOST_TEST_MODULE "Some benchmarkings for vector operations"
9 
10 #include <boost/test/unit_test.hpp>
11 #include <boost/numeric/ublas/vector.hpp>
12 
14 #include <Eigen/Dense>
15 
16 
18 #include "mesh/Elements.hpp"
19 #include "mesh/Region.hpp"
20 #include "mesh/Dictionary.hpp"
21 
24 
25 using namespace cf3;
26 using namespace cf3::common;
27 using namespace cf3::math;
28 using namespace cf3::mesh;
29 using namespace cf3::mesh::BlockMesh;
30 
32 {
35 };
36 
39 
41 template<typename VectorType>
42 void centroid_2d(const Table<Uint>::ArrayT& connectivity, const Table<Real>::ArrayT& coords, VectorType c0, VectorType c1, VectorType c2, VectorType c3, VectorType& result)
43 {
44  const Uint nb_elem = connectivity.size();
45 
46  result[XX] = 0.;
47  result[YY] = 0.;
48 
49  for(Uint elem = 0; elem != nb_elem; ++elem)
50  {
51  const Table<Uint>::ConstRow row = connectivity[elem];
52 
53  const Table<Real>::ConstRow crow0 = coords[row[0]];
54  c0[XX] = crow0[XX]; c0[YY] = crow0[YY];
55 
56  const Table<Real>::ConstRow crow1 = coords[row[1]];
57  c1[XX] = crow1[XX]; c1[YY] = crow1[YY];
58 
59  const Table<Real>::ConstRow crow2 = coords[row[2]];
60  c2[XX] = crow2[XX]; c2[YY] = crow2[YY];
61 
62  const Table<Real>::ConstRow crow3 = coords[row[3]];
63  c3[XX] = crow3[XX]; c3[YY] = crow3[YY];
64 
65  result += 0.25*(c0 + c1 + c2 + c3);
66  }
67 
68  result /= nb_elem;
69 }
70 
72 template<typename VectorType>
74  , VectorType c0
75  , VectorType c1
76  , VectorType c2
77  , VectorType c3
78  , VectorType c4
79  , VectorType c5
80  , VectorType c6
81  , VectorType c7
82  , VectorType& result)
83 {
84  const Uint nb_elem = connectivity.size();
85 
86  result[XX] = 0.;
87  result[YY] = 0.;
88  result[ZZ] = 0.;
89 
90  for(Uint elem = 0; elem != nb_elem; ++elem)
91  {
92  const Table<Uint>::ConstRow row = connectivity[elem];
93 
94  const Table<Real>::ConstRow crow0 = coords[row[0]];
95  c0[XX] = crow0[XX]; c0[YY] = crow0[YY]; c0[ZZ] = crow0[ZZ];
96 
97  const Table<Real>::ConstRow crow1 = coords[row[1]];
98  c1[XX] = crow1[XX]; c1[YY] = crow1[YY]; c1[ZZ] = crow1[ZZ];
99 
100  const Table<Real>::ConstRow crow2 = coords[row[2]];
101  c2[XX] = crow2[XX]; c2[YY] = crow2[YY]; c2[ZZ] = crow2[ZZ];
102 
103  const Table<Real>::ConstRow crow3 = coords[row[3]];
104  c3[XX] = crow3[XX]; c3[YY] = crow3[YY]; c3[ZZ] = crow3[ZZ];
105 
106  const Table<Real>::ConstRow crow4 = coords[row[4]];
107  c4[XX] = crow4[XX]; c4[YY] = crow4[YY]; c4[ZZ] = crow4[ZZ];
108 
109  const Table<Real>::ConstRow crow5 = coords[row[5]];
110  c5[XX] = crow5[XX]; c5[YY] = crow5[YY]; c5[ZZ] = crow5[ZZ];
111 
112  const Table<Real>::ConstRow crow6 = coords[row[6]];
113  c6[XX] = crow6[XX]; c6[YY] = crow6[YY]; c6[ZZ] = crow6[ZZ];
114 
115  const Table<Real>::ConstRow crow7 = coords[row[7]];
116  c7[XX] = crow7[XX]; c7[YY] = crow7[YY]; c7[ZZ] = crow7[ZZ];
117 
118  result += 0.125*(c0 + c1 + c2 + c3 + c4 + c5 + c6 + c7);
119  }
120 
121  result /= nb_elem;
122 }
123 
124 BOOST_AUTO_TEST_SUITE( VectorBenchmarkSuite )
125 
126 // Must be run before the next tests
128 {
129  grid_2d = allocate_component<Mesh>("grid_2d");
130  Tools::MeshGeneration::create_rectangle(*grid_2d, 1., 1., 2000, 2000);
131  channel_3d = allocate_component<Mesh>("channel_3d");
132  boost::shared_ptr<Component> root = boost::static_pointer_cast<Component>(allocate_component<Group>("root"));
133  BlockData& block_data = *root->create_component<BlockData>("block_data");
134  Tools::MeshGeneration::create_channel_3d(block_data, 10., 0.5, 5., 160, 80, 120, 0.1);
135  build_mesh(block_data, *channel_3d);
136 }
137 
139 {
140  RealVector c0(2);
141  RealVector c1(2);
142  RealVector c2(2);
143  RealVector c3(2);
144  RealVector result(2);
145 
146  centroid_2d( find_component_recursively_with_filter<Elements>( *grid_2d, IsElementsVolume() ).geometry_space().connectivity().array(), grid_2d->geometry_fields().coordinates().array(), c0, c1, c2, c3, result);
147 
148  BOOST_CHECK_CLOSE(result[XX], 0.5, 1e-6);
149  BOOST_CHECK_CLOSE(result[YY], 0.5, 1e-6);
150 }
151 
153 {
154  boost::numeric::ublas::c_vector<Real, 2> c0(2);
155  boost::numeric::ublas::c_vector<Real, 2> c1(2);
156  boost::numeric::ublas::c_vector<Real, 2> c2(2);
157  boost::numeric::ublas::c_vector<Real, 2> c3(2);
158  boost::numeric::ublas::c_vector<Real, 2> result(2);
159 
160  centroid_2d( find_component_recursively_with_filter<Elements>( *grid_2d, IsElementsVolume() ).geometry_space().connectivity().array(), grid_2d->geometry_fields().coordinates().array(), c0, c1, c2, c3, result);
161 
162  BOOST_CHECK_CLOSE(result[XX], 0.5, 1e-6);
163  BOOST_CHECK_CLOSE(result[YY], 0.5, 1e-6);
164 }
165 
167 {
168  boost::numeric::ublas::vector<Real> c0(2);
169  boost::numeric::ublas::vector<Real> c1(2);
170  boost::numeric::ublas::vector<Real> c2(2);
171  boost::numeric::ublas::vector<Real> c3(2);
172  boost::numeric::ublas::vector<Real> result(2);
173 
174  centroid_2d( find_component_recursively_with_filter<Elements>( *grid_2d, IsElementsVolume() ).geometry_space().connectivity().array(), grid_2d->geometry_fields().coordinates().array(), c0, c1, c2, c3, result);
175 
176  BOOST_CHECK_CLOSE(result[XX], 0.5, 1e-6);
177  BOOST_CHECK_CLOSE(result[YY], 0.5, 1e-6);
178 }
179 
181 {
182  RealVector c0(3);
183  RealVector c1(3);
184  RealVector c2(3);
185  RealVector c3(3);
186  RealVector c4(3);
187  RealVector c5(3);
188  RealVector c6(3);
189  RealVector c7(3);
190  RealVector result(3);
191 
192  const Elements& elems = find_component_recursively_with_name<Elements>(*channel_3d, "cf3.mesh.SF.LagrangeP1.Hexa3D");
193  const Table<Real>& coords = elems.geometry_fields().coordinates();
194 
195  centroid_3d(elems.geometry_space().connectivity().array(), coords.array(), c0, c1, c2, c3, c4, c5, c6, c7, result);
196 
197  BOOST_CHECK_CLOSE(result[XX], 5., 1e-6);
198  BOOST_CHECK_SMALL(result[YY], 1e-6);
199  BOOST_CHECK_CLOSE(result[ZZ], 2.5, 1e-6);
200 }
201 
203 {
204  boost::numeric::ublas::c_vector<Real, 3> c0(3);
205  boost::numeric::ublas::c_vector<Real, 3> c1(3);
206  boost::numeric::ublas::c_vector<Real, 3> c2(3);
207  boost::numeric::ublas::c_vector<Real, 3> c3(3);
208  boost::numeric::ublas::c_vector<Real, 3> c4(3);
209  boost::numeric::ublas::c_vector<Real, 3> c5(3);
210  boost::numeric::ublas::c_vector<Real, 3> c6(3);
211  boost::numeric::ublas::c_vector<Real, 3> c7(3);
212  boost::numeric::ublas::c_vector<Real, 3> result(3);
213 
214  const Elements& elems = find_component_recursively_with_name<Elements>(*channel_3d, "cf3.mesh.LagrangeP1.Hexa3D");
215  const Table<Real>& coords = elems.geometry_fields().coordinates();
216 
217  centroid_3d(elems.geometry_space().connectivity().array(), coords.array(), c0, c1, c2, c3, c4, c5, c6, c7, result);
218 
219  BOOST_CHECK_CLOSE(result[XX], 5., 1e-6);
220  BOOST_CHECK_SMALL(result[YY], 1e-6);
221  BOOST_CHECK_CLOSE(result[ZZ], 2.5, 1e-6);
222 }
223 
225 {
226  boost::numeric::ublas::vector<Real> c0(3);
227  boost::numeric::ublas::vector<Real> c1(3);
228  boost::numeric::ublas::vector<Real> c2(3);
229  boost::numeric::ublas::vector<Real> c3(3);
230  boost::numeric::ublas::vector<Real> c4(3);
231  boost::numeric::ublas::vector<Real> c5(3);
232  boost::numeric::ublas::vector<Real> c6(3);
233  boost::numeric::ublas::vector<Real> c7(3);
234  boost::numeric::ublas::vector<Real> result(3);
235 
236  const Elements& elems = find_component_recursively_with_name<Elements>(*channel_3d, "cf3.mesh.LagrangeP1.Hexa3D");
237  const Table<Real>& coords = elems.geometry_fields().coordinates();
238 
239  centroid_3d(elems.geometry_space().connectivity().array(), coords.array(), c0, c1, c2, c3, c4, c5, c6, c7, result);
240 
241  BOOST_CHECK_CLOSE(result[XX], 5., 1e-6);
242  BOOST_CHECK_SMALL(result[YY], 1e-6);
243  BOOST_CHECK_CLOSE(result[ZZ], 2.5, 1e-6);
244 }
245 
247 {
248  Eigen::Vector2d c0(2);
249  Eigen::Vector2d c1(2);
250  Eigen::Vector2d c2(2);
251  Eigen::Vector2d c3(2);
252  Eigen::Vector2d result(2);
253 
254  centroid_2d( find_component_recursively_with_filter<Elements>( *grid_2d, IsElementsVolume() ).geometry_space().connectivity().array(), grid_2d->geometry_fields().coordinates().array(), c0, c1, c2, c3, result);
255 
256  BOOST_CHECK_CLOSE(result[XX], 0.5, 1e-6);
257  BOOST_CHECK_CLOSE(result[YY], 0.5, 1e-6);
258 }
259 
261 {
262  Eigen::VectorXd c0(2);
263  Eigen::VectorXd c1(2);
264  Eigen::VectorXd c2(2);
265  Eigen::VectorXd c3(2);
266  Eigen::VectorXd result(2);
267 
268  centroid_2d( find_component_recursively_with_filter<Elements>( *grid_2d, IsElementsVolume() ).geometry_space().connectivity().array(), grid_2d->geometry_fields().coordinates().array(), c0, c1, c2, c3, result);
269 
270  BOOST_CHECK_CLOSE(result[XX], 0.5, 1e-6);
271  BOOST_CHECK_CLOSE(result[YY], 0.5, 1e-6);
272 }
273 
275 {
276  Eigen::Vector3d c0(3);
277  Eigen::Vector3d c1(3);
278  Eigen::Vector3d c2(3);
279  Eigen::Vector3d c3(3);
280  Eigen::Vector3d c4(3);
281  Eigen::Vector3d c5(3);
282  Eigen::Vector3d c6(3);
283  Eigen::Vector3d c7(3);
284  Eigen::Vector3d result(3);
285 
286  const Elements& elems = find_component_recursively_with_name<Elements>(*channel_3d, "cf3.mesh.LagrangeP1.Hexa3D");
287  const Table<Real>& coords = elems.geometry_fields().coordinates();
288 
289  centroid_3d(elems.geometry_space().connectivity().array(), coords.array(), c0, c1, c2, c3, c4, c5, c6, c7, result);
290 
291  BOOST_CHECK_CLOSE(result[XX], 5., 1e-6);
292  BOOST_CHECK_SMALL(result[YY], 1e-6);
293  BOOST_CHECK_CLOSE(result[ZZ], 2.5, 1e-6);
294 }
295 
297 {
298  Eigen::VectorXd c0(3);
299  Eigen::VectorXd c1(3);
300  Eigen::VectorXd c2(3);
301  Eigen::VectorXd c3(3);
302  Eigen::VectorXd c4(3);
303  Eigen::VectorXd c5(3);
304  Eigen::VectorXd c6(3);
305  Eigen::VectorXd c7(3);
306  Eigen::VectorXd result(3);
307 
308  const Elements& elems = find_component_recursively_with_name<Elements>(*channel_3d, "cf3.mesh.LagrangeP1.Hexa3D");
309  const Table<Real>& coords = elems.geometry_fields().coordinates();
310 
311  centroid_3d(elems.geometry_space().connectivity().array(), coords.array(), c0, c1, c2, c3, c4, c5, c6, c7, result);
312 
313  BOOST_CHECK_CLOSE(result[XX], 5., 1e-6);
314  BOOST_CHECK_SMALL(result[YY], 1e-6);
315  BOOST_CHECK_CLOSE(result[ZZ], 2.5, 1e-6);
316 }
317 
318 BOOST_AUTO_TEST_SUITE_END()
void centroid_2d(const Table< Uint >::ArrayT &connectivity, const Table< Real >::ArrayT &coords, VectorType c0, VectorType c1, VectorType c2, VectorType c3, VectorType &result)
Calculates the centroid of all centroids over a set of quads.
BOOST_FIXTURE_TEST_CASE(CreateMesh, VectorBenchmarkFixture)
ArrayT & array()
Definition: Table.hpp:92
Space & geometry_space() const
Definition: Entities.hpp:94
Basic Classes for Mathematical applications used by COOLFluiD.
static Handle< Mesh > channel_3d
const Field & coordinates() const
Definition: Dictionary.cpp:481
tuple root
Definition: coolfluid.py:24
void create_rectangle(Mesh &mesh, const Real x_len, const Real y_len, const Uint x_segments, const Uint y_segments)
Create a rectangular, 2D, quad-only mesh. No buffer for creation.
Any test using this fixture (or a derivative) will be timed.
Real e()
Definition of the Unit charge [C].
Definition: Consts.hpp:30
Dictionary & geometry_fields() const
Const access to the coordinates.
Definition: Entities.hpp:63
Definition: Defs.hpp:17
Definition: Defs.hpp:17
Uint size() const
Definition: Table.hpp:127
static Handle< Mesh > grid_2d
Basic Classes for Mesh applications used by COOLFluiD.
Library for I/O of the OpenFOAM BlockMesh dict files.
Definition: BlockData.cpp:47
Eigen::Matrix< Real, Eigen::Dynamic, 1 > RealVector
Dynamic sized column vector.
Definition: MatrixTypes.hpp:25
Top-level namespace for coolfluid.
Definition: Action.cpp:18
void centroid_3d(const Table< Uint >::ArrayT &connectivity, const Table< Real >::ArrayT &coords, VectorType c0, VectorType c1, VectorType c2, VectorType c3, VectorType c4, VectorType c5, VectorType c6, VectorType c7, VectorType &result)
Calculates the centroid of all centroids over a set of hexahedra.
void create_channel_3d(BlockArrays &blocks, const Real length, const Real half_height, const Real width, const Uint x_segs, const Uint y_segs_half, const Uint z_segs, const Real ratio)
Component holding a 2 dimensional array of a templated type.
Definition: Table.hpp:45
unsigned int Uint
typedef for unsigned int
Definition: CF.hpp:90
Definition: Defs.hpp:17
Base class for defining CF components.
Definition: Component.hpp:82
Most basic kernel library.
Definition: Action.cpp:19
Connectivity & connectivity()
connectivity table to dictionary entries
Definition: Space.hpp:110
Send comments to:
COOLFluiD Web Admin