COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
utest-binarydata.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::Component"
9 
10 #include <iostream>
11 
12 #include <boost/mpl/if.hpp>
13 #include <boost/test/unit_test.hpp>
14 #include <boost/random/mersenne_twister.hpp>
15 #include <boost/random/uniform_int_distribution.hpp>
16 #include <boost/random/uniform_real_distribution.hpp>
17 
20 #include "common/Core.hpp"
21 #include "common/List.hpp"
22 #include "common/OptionList.hpp"
23 #include "common/Table.hpp"
24 
25 #include "common/PE/Comm.hpp"
26 #include <common/Environment.hpp>
27 
28 using namespace cf3;
29 
31 
33 {
35  rank(common::PE::Comm::instance().rank()),
36  gen(rank),
37  int_table_size(10000+1000*rank),
38  real_table_size(20000+2000*rank),
39  int_list_size(30000-3000*rank),
40  real_list_size(40000+4000*rank)
41  {
42  }
43 
44  template<typename T>
46  {
47  typedef typename boost::mpl::if_< typename boost::is_integral<T>::type, boost::random::uniform_int_distribution<T>, boost::random::uniform_real_distribution<T> >::type distribution_type;
48  distribution_type dist(std::numeric_limits<T>::min(), std::numeric_limits<T>::max());
49  const Uint nb_rows = table.size();
50  const Uint nb_cols = table.row_size();
51  for(Uint i = 0; i != nb_rows; ++i)
52  {
53  for(Uint j = 0; j != nb_cols; ++j)
54  {
55  table[i][j] = dist(gen);
56  }
57  }
58  }
59 
60  template<typename T>
62  {
63  typedef typename boost::mpl::if_< typename boost::is_integral<T>::type, boost::random::uniform_int_distribution<T>, boost::random::uniform_real_distribution<T> >::type distribution_type;
64  distribution_type dist(std::numeric_limits<T>::min(), std::numeric_limits<T>::max());
65  const Uint nb_rows = list.size();
66  for(Uint i = 0; i != nb_rows; ++i)
67  {
68  list[i] = dist(gen);
69  }
70  }
71 
72  const Uint rank;
73 
74  boost::random::mt19937 gen;
75 
77  static const Uint int_table_cols = 3;
79  static const Uint real_table_cols = 8;
80 
83 };
84 
85 BOOST_FIXTURE_TEST_SUITE( BinaryDataSuite, BinaryDataFixture )
86 
87 
90 {
91  common::Core::instance().environment().options().set("log_level", 4);
92  common::PE::Comm::instance().init(boost::unit_test::framework::master_test_suite().argc,boost::unit_test::framework::master_test_suite().argv);
93  BOOST_CHECK_EQUAL(common::PE::Comm::instance().is_active(),true);
94 }
95 
96 BOOST_AUTO_TEST_CASE( WriteBinaryData )
97 {
98  common::Component& group = *common::Core::instance().root().create_component("WriteGroup", "cf3.common.Group");
99 
100  common::Table<Uint>& int_table = *group.create_component< common::Table<Uint> >("IntTable");
101  int_table.set_row_size(int_table_cols);
102  int_table.resize(int_table_size);
103  fill_table(int_table);
104 
105  common::Table<Real>& real_table = *group.create_component< common::Table<Real> >("RealTable");
106  real_table.set_row_size(real_table_cols);
107  real_table.resize(real_table_size);
108  fill_table(real_table);
109 
110  common::List<Uint>& int_list = *group.create_component< common::List<Uint> >("IntList");
111  int_list.resize(int_list_size);
112  fill_list(int_list);
113 
114  common::List<Real>& real_list = *group.create_component< common::List<Real> >("RealList");
115  real_list.resize(real_list_size);
116  fill_list(real_list);
117 
119  writer.options().set("file", common::URI("binary_data.cfbinxml"));
120 
121  writer.append_data(int_table);
122  writer.append_data(real_table);
123  writer.append_data(real_list);
124  writer.append_data(int_list);
125 
126  common::List<Real>& empty_real_list = *group.create_component< common::List<Real> >("EmptyRealList");
127  common::Table<Real>& empty_real_table = *group.create_component< common::Table<Real> >("EmptyRealTable");
128  empty_real_table.set_row_size(real_table_cols);
129 
130  writer.append_data(empty_real_list);
131  writer.append_data(empty_real_table);
132 
133  writer.close();
134 }
135 
136 BOOST_AUTO_TEST_CASE( ReadBinaryData )
137 {
138  common::Component& read_group = *common::Core::instance().root().create_component("ReadGroup", "cf3.common.Group");
140  reader.options().set("file", common::URI("binary_data.cfbinxml"));
141 
142  common::Table<Uint>& read_int_table = *read_group.create_component< common::Table<Uint> >("IntTable");
143  common::Table<Real>& read_real_table = *read_group.create_component< common::Table<Real> >("RealTable");
144  common::List<Uint>& read_int_list = *read_group.create_component< common::List<Uint> >("IntList");
145  common::List<Real>& read_real_list = *read_group.create_component< common::List<Real> >("RealList");
146 
147  Handle<common::Component> write_group = common::Core::instance().root().get_child("WriteGroup");
148  Handle< common::Table<Uint> > write_int_table(write_group->get_child("IntTable"));
149  Handle< common::Table<Real> > write_real_table(write_group->get_child("RealTable"));
150  Handle< common::List<Uint> > write_int_list(write_group->get_child("IntList"));
151  Handle< common::List<Real> > write_real_list(write_group->get_child("RealList"));
152 
153  reader.read_table(read_int_table, 0);
154  reader.read_table(read_real_table, 1);
155  reader.read_list(read_real_list, 2);
156  reader.read_list(read_int_list, 3);
157 
158  BOOST_CHECK_EQUAL(read_int_table.row_size(), write_int_table->row_size());
159  BOOST_CHECK_EQUAL(read_int_table.size(), write_int_table->size());
160  BOOST_CHECK(read_int_table.array() == write_int_table->array());
161  BOOST_CHECK(read_real_table.array() == write_real_table->array());
162  BOOST_CHECK(read_real_list.array() == write_real_list->array());
163  BOOST_CHECK(read_int_list.array() == write_int_list->array());
164 
165  common::List<Real>& empty_real_list = *read_group.create_component< common::List<Real> >("EmptyRealList");
166  common::Table<Real>& empty_real_table = *read_group.create_component< common::Table<Real> >("EmptyRealTable");
167 
168  reader.read_list(empty_real_list, 4);
169  reader.read_table(empty_real_table, 5);
170 
171  BOOST_CHECK_EQUAL(empty_real_list.size(), 0);
172  BOOST_CHECK_EQUAL(empty_real_table.size(), 0);
173  BOOST_CHECK_EQUAL(empty_real_table.row_size(), 8);
174 }
175 
177 
178 BOOST_AUTO_TEST_SUITE_END()
179 
180 
ListT & array()
Definition: List.hpp:76
ArrayT & array()
Definition: Table.hpp:92
void resize(const Uint new_size)
Definition: List.hpp:69
void close()
Close the current file.
boost::random::mt19937 gen
Uint append_data(const Table< T > &table)
Append a new data block containing data from the supplied table. An index into the current file is re...
Real max(const Real a, const Real b)
Maximum between two scalars.
Definition: Terminals.hpp:228
void read_list(List< T > &list, const Uint block_idx)
Read the given block into the supplied list. The list is resized as needed.
Uint size() const
Definition: Table.hpp:127
void fill_table(common::Table< T > &table)
Real min(const Real a, const Real b)
Minimum between two scalars.
Definition: Terminals.hpp:234
void fill_list(common::List< T > &list)
Handle< Component > get_child(const std::string &name)
Definition: Component.cpp:441
void init(int argc=0, char **args=0)
Definition: Comm.cpp:80
void read_table(Table< T > &table, const Uint block_idx)
Read the given block into the supplied table. The table is resized as needed.
Top-level namespace for coolfluid.
Definition: Action.cpp:18
Component holding a 1 dimensional array of a templated type.
Definition: List.hpp:40
Component holding a 2 dimensional array of a templated type.
Definition: Table.hpp:45
Uint row_size(Uint i=0) const
Definition: Table.hpp:133
const Uint real_table_size
void set_row_size(const Uint nb_cols)
Definition: Table.hpp:78
common::Component & root() const
Gives the default root component.
Definition: Core.cpp:145
Uint size() const
Definition: List.hpp:111
static boost::proto::terminal< ExpressionGroupTag >::type group
Use group(expr1, expr2, ..., exprN) to evaluate a group of expressions.
common::Environment & environment() const
Definition: Core.cpp:168
unsigned int Uint
typedef for unsigned int
Definition: CF.hpp:90
static Core & instance()
Definition: Core.cpp:37
BOOST_AUTO_TEST_CASE(InitMPI)
OptionList & options()
Definition: Component.cpp:856
static Comm & instance()
Return a reference to the current PE.
Definition: Comm.cpp:44
Base class for defining CF components.
Definition: Component.hpp:82
Component for writing binary data collected into a single file.
void set(const std::string &pname, const boost::any &val)
Definition: OptionList.cpp:132
Handle< Component > create_component(const std::string &name, const std::string &builder)
Build a (sub)component of this component using the extended type_name of the component.
Definition: Component.cpp:568
virtual void resize(const Uint nb_rows)
Definition: Table.hpp:85
Component for writing binary data collected into a single file.
Send comments to:
COOLFluiD Web Admin