COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
utest-mesh-dictionary.cpp
Go to the documentation of this file.
1 // Copyright (C) 2010-2013 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 "Test module for cf3::mesh::Dictionary"
9 
10 #include <iostream>
11 #include <boost/test/unit_test.hpp>
12 #include <boost/assign/list_of.hpp>
13 #include <boost/foreach.hpp>
14 
15 #include "common/Log.hpp"
16 #include "common/OptionList.hpp"
17 #include "common/Core.hpp"
18 
19 #include "math/MatrixTypes.hpp"
21 
22 #include "mesh/Mesh.hpp"
23 #include "mesh/Region.hpp"
24 #include "mesh/Elements.hpp"
25 #include "mesh/Connectivity.hpp"
26 #include "mesh/Dictionary.hpp"
28 #include "mesh/Field.hpp"
29 #include "mesh/Space.hpp"
30 #include "mesh/Faces.hpp"
31 #include "mesh/Cells.hpp"
32 
33 using namespace boost;
34 using namespace cf3;
35 using namespace cf3::mesh;
36 using namespace cf3::common;
37 using namespace boost::assign;
38 
40 
42 {
45  {
46  // uncomment if you want to use arguments to the test executable
47  //int* argc = &boost::unit_test::framework::master_test_suite().argc;
48  //char*** argv = &boost::unit_test::framework::master_test_suite().argv;
49  }
50 
53  {
54  }
55 
58 };
59 
60 Handle< Mesh > DictionaryTests_Fixture::m_mesh = Core::instance().root().create_component<Mesh>("mesh");
61 
62 template <typename Derived>
63 void map(Field& field, Eigen::Map<Derived>& v, const Uint row_idx, const Uint var_idx)
64 {
65  new (&v) typename Derived::MapType( &field.array()[row_idx][field.descriptor().offset(var_idx)],field.descriptor().var_length(var_idx) );
66 }
67 
68 
69 template <typename Derived>
70 typename Derived::MapType map(Field& field, const Uint row_idx, const Uint var_idx)
71 {
72  return typename Derived::MapType( &field.array()[row_idx][field.descriptor().offset(var_idx)],field.descriptor().var_length(var_idx) );
73 }
74 
75 
77 
78 BOOST_FIXTURE_TEST_SUITE( DictionaryTests_TestSuite, DictionaryTests_Fixture )
79 
80 
82 BOOST_AUTO_TEST_CASE( test_MeshCreation )
83 {
84  SimpleMeshGenerator& mesh_gen = *Core::instance().root().create_component<SimpleMeshGenerator>("mesh_gen");
85  mesh_gen.options().set("mesh",m_mesh->uri());
86  mesh_gen.options().set("lengths",std::vector<Real>(2,5.));
87  mesh_gen.options().set("nb_cells",std::vector<Uint>(2,5u));
88  mesh_gen.execute();
89 }
91 
92 BOOST_AUTO_TEST_CASE( test_Dictionary )
93 {
94  Mesh& mesh = *m_mesh;
95 
96  // Check if nodes dict is sane
97  BOOST_CHECK_NO_THROW(mesh.geometry_fields().check_sanity());
98 
99  // Check if connectivity returns expected results
100  boost_foreach(const Handle<Entities>& elements_handle, mesh.geometry_fields().entities_range())
101  {
102  Entities& elements = *elements_handle;
103  for (Uint e=0; e<elements.size(); ++e)
104  {
105  BOOST_CHECK( mesh.geometry_fields().space(elements).connectivity()[e] == elements.geometry_space().connectivity()[e] );
106  }
107  }
108  BOOST_CHECK_EQUAL( mesh.geometry_fields().entities_range().size() , 5u);
109 
110 
111  // ----------------------------------------------------------------------------------------------
112  // CHECK element-based field group building
113 
114  // Create space and dict for Lagrange P0 elements
115  Dictionary& elem_fields = mesh.create_discontinuous_space("elems_P0","cf3.mesh.LagrangeP0");
116 
117  BOOST_CHECK_EQUAL( elem_fields.size() , 45);
118  BOOST_CHECK_EQUAL( elem_fields.entities_range().size() , 5u);
119 
120  // CHECK connectivity access for nodes
121  Uint cell_idx=0;
122  boost_foreach(const Handle<Entities>& elements_handle, elem_fields.entities_range())
123  {
124  const Entities& elements = *elements_handle;
125  const Space& space = elem_fields.space(elements);
126  const Connectivity& field_connectivity = space.connectivity();
127  for (Uint e=0; e<elements.size(); ++e)
128  {
129  boost_foreach( const Uint point, field_connectivity[e] )
130  {
131  BOOST_CHECK_EQUAL( point, cell_idx ); // same because P0 field
132  ++cell_idx;
133  }
134  }
135  }
136 
137  // ----------------------------------------------------------------------------------------------
138  // CHECK field building inside field groups
139 
140  Field& solution = elem_fields.create_field("solution","rho[s],V[v],p[s]");
141  Field& volume = elem_fields.create_field("volume");
142 
143  BOOST_CHECK_EQUAL(solution.size() , elem_fields.size());
144  BOOST_CHECK_EQUAL(solution.row_size() , 4);
145  BOOST_CHECK_EQUAL(solution.array().num_elements() , solution.size()*solution.row_size());
146  BOOST_CHECK_EQUAL(solution.dict().uri().string() , elem_fields.uri().string() );
147  BOOST_CHECK_EQUAL(volume.size() , elem_fields.size());
148  BOOST_CHECK_EQUAL(volume.dict().uri().string() , elem_fields.uri().string() );
149 
150  // There should be 1 coordinates field, 1 volume field, 1 solution field
151  BOOST_CHECK_EQUAL(find_components<Field>(elem_fields).size() , 3u);
152  boost_foreach(Field& field, find_components<Field>(elem_fields))
153  BOOST_CHECK_EQUAL( field.dict().uri().string() , elem_fields.uri().string());
154 
155  BOOST_CHECK_EQUAL(elem_fields.field("volume").uri().string() , volume.uri().string());
156 
157  // ----------------------------------------------------------------------------------------------
158  // CHECK P1 point-based field group building
159 
160  // Create field group for the space "points_P1" fields
161  Dictionary& point_P1_fields = mesh.create_continuous_space("points_P1","cf3.mesh.LagrangeP1");
162 
163  BOOST_CHECK_EQUAL ( point_P1_fields.size() , mesh.geometry_fields().size() );
164 
165  // ----------------------------------------------------------------------------------------------
166  // CHECK P2 point-based field group building
167 
168  // Create field group for the space "P2"
169  Dictionary& point_P2_fields = mesh.create_continuous_space("points_P2","cf3.mesh.LagrangeP2");
170  BOOST_CHECK_EQUAL ( point_P2_fields.size() , 121u );
171 
172 
173 // std::cout << mesh.tree() << std::endl;
174 }
175 
177 
178 BOOST_AUTO_TEST_CASE( test_Field )
179 {
180  Handle<Dictionary> elems_P0(m_mesh->get_child("elems_P0"));
181  Field& volume = elems_P0->field("volume");
182  boost_foreach(const Handle<Entities>& elements_handle, volume.entities_range())
183  {
184  Entities& elements = *elements_handle;
185  const Space& space = volume.space(elements);
186  for (Uint e=0; e<elements.size(); ++e)
187  {
188  boost_foreach( const Uint state, space.connectivity()[e])
189  {
190  volume[state][0] = elements.element_type().volume(elements.geometry_space().get_coordinates(e));
191  }
192 
193 
194  }
195  }
196 
197  Handle<Dictionary> points_P2(m_mesh->get_child("points_P2"));
198  Field& point_field = points_P2->create_field("point_field");
199 
200 
201  boost_foreach(const Handle<Entities>& elements_handle, point_field.entities_range())
202  {
203  Entities& elements = *elements_handle;
204  const Space& space = point_field.space(elements);
205  for (Uint e=0; e<elements.size(); ++e)
206  {
207  boost_foreach( const Uint point, space.connectivity()[e] )
208  point_field[point][0] = 1.;
209  }
210  }
211 }
212 
214 
215 BOOST_AUTO_TEST_CASE( FieldOperators )
216 {
217  Handle<Dictionary> elems_P0(m_mesh->get_child("elems_P0"));
218  Field& solution = elems_P0->create_field("solution","sol[1]");
219  Field& solution_copy = elems_P0->create_field("solution_copy",solution.descriptor().description());
220  solution_copy.descriptor().prefix_variable_names("copy_");
221 
222  solution[0][0] = 25.;
223  solution_copy = solution;
224  BOOST_CHECK_EQUAL ( solution_copy[0][0] , 25. );
225  solution_copy += solution_copy;
226  BOOST_CHECK_EQUAL ( solution_copy[0][0] , 50. );
227  solution_copy *= 2;
228  BOOST_CHECK_EQUAL ( solution_copy[0][0] , 100. );
229  solution_copy /= 2;
230  BOOST_CHECK_EQUAL ( solution_copy[0][0] , 50. );
231  solution_copy *= solution_copy;
232  BOOST_CHECK_EQUAL ( solution_copy[0][0] , 2500. );
233  solution_copy /= solution_copy;
234  BOOST_CHECK_EQUAL ( solution_copy[0][0] , 1. );
235  solution_copy -= solution_copy;
236  BOOST_CHECK_EQUAL ( solution_copy[0][0] , 0. );
237 }
238 
240 
241 BOOST_AUTO_TEST_CASE( FieldVariables )
242 {
243  Handle<Dictionary> elems_P0(m_mesh->get_child("elems_P0"));
244  Field& solution = elems_P0->create_field("solution",4);
245 
246  Field& velocity = elems_P0->create_field("U",VECTOR_2D);
247  Field& pressure = elems_P0->create_field("p",SCALAR);
248  Field& temperature = elems_P0->create_field("T",SCALAR);
249  Field::Ref U = velocity.ref();
250  Field::Ref p = pressure.ref();
251  Field::Ref T = temperature.ref();
252 
253  Field::RefCol u = U.col(XX);
254  Field::RefCol v = U.col(YY);
255 
256  p.setConstant(101000);
257  u.setConstant(30.);
258  U.col(YY).setZero();
259  T.setConstant(298.15);
260 
261  Real gamma = 1.4;
262  Real R = 287.05;
263 
264  Field::Ref rho = solution.col(0);
265  Field::Ref rhoUx = solution.col(1);
266  Field::Ref rhoUy = solution.col(2);
267  Field::Ref rhoE = solution.col(3);
268  rho = p/(R*T);
269  rhoUx = rho*u;
270  rhoUy = rho*v;
271  rhoE = p/(gamma-1) + 0.5 * rho * (u.pow(2) + v.pow(2));
272 
273  std::cout << U << std::endl;
274  std::cout << solution << std::endl;
275 
276  BOOST_CHECK_EQUAL( U.rows() , solution.size() );
277  BOOST_CHECK_EQUAL( U.cols() , 2u );
278  BOOST_CHECK_EQUAL( p.rows() , solution.size() );
279  BOOST_CHECK_EQUAL( p.cols() , 1u );
280 
281  for (int i=0; i<solution.size(); ++i)
282  {
283  BOOST_CHECK_EQUAL( solution[i][0] , 101000./(R*298.15) );
284  BOOST_CHECK_EQUAL( solution[i][1] , 30. * solution[i][0] );
285  BOOST_CHECK_EQUAL( solution[i][2] , 0. );
286  BOOST_CHECK_EQUAL( solution[i][3] , 101000./(gamma-1.) + 0.5*solution[i][0]*30*30 );
287  }
288 
289  for (int i=0; i<solution.size(); ++i)
290  {
291  solution[i][0] = pressure.scalar(i)/(R*temperature.scalar(i));
292  solution[i][1] = velocity[i][XX] * solution[i][0];
293  solution[i][2] = velocity[i][YY] * solution[i][0];
294  solution[i][3] = pressure.scalar(i)/(gamma-1.) + 0.5 * solution[i][0] * velocity.vector(i).squaredNorm();
295  }
296 
297  for (int i=0; i<solution.size(); ++i)
298  {
299  BOOST_CHECK_EQUAL( solution[i][0] , 101000./(R*298.15) );
300  BOOST_CHECK_EQUAL( solution[i][1] , 30. * solution[i][0] );
301  BOOST_CHECK_EQUAL( solution[i][2] , 0. );
302  BOOST_CHECK_EQUAL( solution[i][3] , 101000./(gamma-1.) + 0.5*solution[i][0]*30*30 );
303  }
304 
305 
306  std::cout << "type of var = " << common::demangle( typeid(p).name() ) << std::endl;
307  std::cout << "type of varcol = " << common::demangle( typeid(u).name() ) << std::endl;
308 }
309 
311 
312 BOOST_AUTO_TEST_SUITE_END()
313 
314 
Real & scalar(const Uint r)
Definition: Field.cpp:290
std::string name(ComponentWrapper &self)
const std::vector< Handle< Entities > > & entities_range() const
Definition: Dictionary.cpp:353
ArrayT & array()
Definition: Table.hpp:92
Dictionary & create_discontinuous_space(const std::string &space_name, const std::string &space_lib_name, const std::vector< Handle< Entities > > &entities)
Definition: Mesh.cpp:316
external boost library namespace
Real R()
Definition of the ideal gas constant [J/mol K].
Definition: Consts.hpp:36
Space & geometry_space() const
Definition: Entities.hpp:94
Dictionary & create_continuous_space(const std::string &space_name, const std::string &space_lib_name, const std::vector< Handle< Entities > > &entities)
Definition: Mesh.cpp:269
RowVectorRef vector(const Uint r)
Definition: Field.cpp:297
boost::proto::terminal< SFOp< VolumeOp > >::type const volume
Static terminals that can be used in proto expressions.
void prefix_variable_names(const std::string &prefix)
ElementType & element_type() const
return the elementType
Definition: Entities.cpp:116
URI uri() const
Construct the full path.
Definition: Component.cpp:248
Eigen::Map< ArrayStorage, Eigen::Unaligned, Eigen::Stride< Eigen::Dynamic, Eigen::Dynamic > > Ref
Definition: Field.hpp:42
static Handle< Mesh > m_mesh
common mesh accessed by all tests
bool check_sanity(std::vector< std::string > &messages) const
Check if all fields are compatible.
Definition: Dictionary.cpp:272
#define boost_foreach
lowercase version of BOOST_FOREACH
Definition: Foreach.hpp:16
Real e()
Definition of the Unit charge [C].
Definition: Consts.hpp:30
Dictionary & dict() const
Definition: Field.cpp:118
Handle< Component const > root() const
Definition: Component.cpp:266
Definition: Defs.hpp:17
Uint size() const
Definition: Table.hpp:127
Generate a simple carthesian P1 mesh without grading.
DictionaryTests_Fixture()
common setup for each test case
Uint size() const
return the number of elements
Definition: Entities.cpp:161
~DictionaryTests_Fixture()
common tear-down for each test case
Basic Classes for Mesh applications used by COOLFluiD.
virtual Real volume(const RealMatrix &nodes) const =0
Top-level namespace for coolfluid.
Definition: Action.cpp:18
Real gamma()
Definition of the Euler gamma.
Definition: Consts.hpp:38
Uint offset(const std::string &name) const
RealMatrix get_coordinates(const Uint elem_idx) const
Lookup element coordinates.
Definition: Space.cpp:179
const Space & space(const Entities &entities) const
Return the space of given entities.
Definition: Dictionary.cpp:161
Uint row_size(Uint i=0) const
Definition: Table.hpp:133
BOOST_AUTO_TEST_CASE(test_MeshCreation)
Eigen::Block< Ref, Eigen::Dynamic, 1 > RefCol
Definition: Field.hpp:44
unsigned int Uint
typedef for unsigned int
Definition: CF.hpp:90
Uint var_length(const std::string &name) const
math::VariablesDescriptor & descriptor() const
Definition: Field.hpp:127
Definition: Defs.hpp:17
OptionList & options()
Definition: Component.cpp:856
Dictionary & geometry_fields() const
Definition: Mesh.cpp:339
Space component class.
Definition: Space.hpp:59
void set(const std::string &pname, const boost::any &val)
Definition: OptionList.cpp:132
void map(Field &field, Eigen::Map< Derived > &v, const Uint row_idx, const Uint var_idx)
Uint size() const
Number of rows of contained fields.
Definition: Dictionary.cpp:99
Most basic kernel library.
Definition: Action.cpp:19
std::string string() const
Definition: URI.cpp:258
Connectivity & connectivity()
connectivity table to dictionary entries
Definition: Space.hpp:110
std::string demangle(const char *type)
Function to demangle the return of typeid()
Definition: TypeInfo.cpp:22
Send comments to:
COOLFluiD Web Admin