COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
utest-common-arraydiff.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 
18 #include "common/Action.hpp"
19 #include "common/Core.hpp"
20 #include "common/List.hpp"
21 #include "common/OptionList.hpp"
22 #include "common/PropertyList.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 
32 struct BinaryDataFixture
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(0, 1);
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(0, 1);
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 
73  {
74  const Uint nb_rows = list.size();
75  for(Uint i = 0; i != nb_rows; ++i)
76  {
77  list[i] = (i % 2 == 0);
78  }
79  }
80 
81  const Uint rank;
82 
83  boost::random::mt19937 gen;
84 
85  const Uint int_table_size;
86  static const Uint int_table_cols = 3;
87  const Uint real_table_size;
88  static const Uint real_table_cols = 8;
89 
90  const Uint int_list_size;
91  const Uint real_list_size;
92 };
93 
94 BOOST_FIXTURE_TEST_SUITE( BinaryDataSuite, BinaryDataFixture )
95 
96 
99 {
100  common::Core::instance().environment().options().set("log_level", 4);
101  common::Core::instance().environment().options().set("only_cpu0_writes", false);
102  common::PE::Comm::instance().init(boost::unit_test::framework::master_test_suite().argc,boost::unit_test::framework::master_test_suite().argv);
103  BOOST_CHECK_EQUAL(common::PE::Comm::instance().is_active(),true);
104 }
105 
106 BOOST_AUTO_TEST_CASE( CompareArrays )
107 {
108  common::Component& group = *common::Core::instance().root().create_component("Group", "cf3.common.Group");
109  Handle<common::Action> array_differ(group.create_component("ArrayDiff", "cf3.common.ArrayDiff"));
110 
111  common::Table<Uint>& int_table1 = *group.create_component< common::Table<Uint> >("IntTable1");
112  common::Table<Uint>& int_table2 = *group.create_component< common::Table<Uint> >("IntTable2");
113  int_table1.set_row_size(int_table_cols);
114  int_table1.resize(int_table_size);
115  fill_table(int_table1);
116  int_table2.set_row_size(int_table_cols);
117  int_table2.resize(int_table_size);
118  int_table2.array() = int_table1.array();
119  array_differ->options().set("left", int_table1.handle());
120  array_differ->options().set("right", int_table2.handle());
121  array_differ->execute();
122  BOOST_CHECK(array_differ->properties().value<bool>("arrays_equal"));
123  int_table2[rank][rank] += rank+1;
124  array_differ->execute();
125  BOOST_CHECK(!array_differ->properties().value<bool>("arrays_equal"));
126 
127  common::Table<Real>& real_table1 = *group.create_component< common::Table<Real> >("RealTable1");
128  common::Table<Real>& real_table2 = *group.create_component< common::Table<Real> >("RealTable2");
129  real_table1.set_row_size(real_table_cols);
130  real_table1.resize(real_table_size);
131  real_table2.set_row_size(real_table_cols);
132  real_table2.resize(real_table_size);
133  fill_table(real_table1);
134  real_table2.array() = real_table1.array();
135  array_differ->options().set("left", real_table1.handle());
136  array_differ->options().set("right", real_table2.handle());
137  array_differ->execute();
138  BOOST_CHECK(array_differ->properties().value<bool>("arrays_equal"));
139  real_table2[rank][rank] += static_cast<Real>(rank+1)*1e-18;
140  array_differ->execute();
141  BOOST_CHECK(array_differ->properties().value<bool>("arrays_equal"));
142  real_table2[rank][rank] += static_cast<Real>(rank+1)*1e-15;
143  array_differ->execute();
144  BOOST_CHECK(!array_differ->properties().value<bool>("arrays_equal"));
145 
146  common::List<Uint>& int_list1 = *group.create_component< common::List<Uint> >("IntList1");
147  common::List<Uint>& int_list2 = *group.create_component< common::List<Uint> >("IntList2");
148  int_list1.resize(int_list_size);
149  int_list2.resize(int_list_size);
150  fill_list( int_list1 );
151  int_list2.array() = int_list1.array();
152  array_differ->options().set("left", int_list1.handle());
153  array_differ->options().set("right", int_list2.handle());
154  array_differ->execute();
155  BOOST_CHECK(array_differ->properties().value<bool>("arrays_equal"));
156  int_list1[rank] += rank+1;
157  array_differ->execute();
158  BOOST_CHECK(!array_differ->properties().value<bool>("arrays_equal"));
159 
160  common::List<Real>& real_list1 = *group.create_component< common::List<Real> >("RealList");
161  common::List<Real>& real_list2 = *group.create_component< common::List<Real> >("RealList");
162  real_list1.resize(real_list_size);
163  real_list2.resize(real_list_size);
164  fill_list( real_list1 );
165  real_list2.array() = real_list1.array();
166  array_differ->options().set("left", real_list1.handle());
167  array_differ->options().set("right", real_list2.handle());
168  array_differ->execute();
169  BOOST_CHECK(array_differ->properties().value<bool>("arrays_equal"));
170  real_list2[rank] += static_cast<Real>(rank+1)*1e-18;
171  array_differ->execute();
172  BOOST_CHECK(array_differ->properties().value<bool>("arrays_equal"));
173  real_list2[rank] += static_cast<Real>(rank+1)*1e-14;
174  array_differ->execute();
175  BOOST_CHECK(!array_differ->properties().value<bool>("arrays_equal"));
176 
177  common::List<bool>& bool_list1 = *group.create_component< common::List<bool> >("BoolList1");
178  common::List<bool>& bool_list2 = *group.create_component< common::List<bool> >("BoolList2");
179  bool_list1.resize(int_list_size);
180  bool_list2.resize(int_list_size);
181  fill_list( bool_list1 );
182  bool_list2.array() = bool_list1.array();
183  array_differ->options().set("left", bool_list1.handle());
184  array_differ->options().set("right", bool_list2.handle());
185  array_differ->execute();
186  BOOST_CHECK(array_differ->properties().value<bool>("arrays_equal"));
187  bool_list1[rank] = !bool_list2[rank];
188  array_differ->execute();
189  BOOST_CHECK(!array_differ->properties().value<bool>("arrays_equal"));
190 }
191 
193 
194 BOOST_AUTO_TEST_SUITE_END()
195 
196 
ListT & array()
Definition: List.hpp:76
Safe pointer to an object. This is the supported method for referring to components.
Definition: Handle.hpp:39
ArrayT & array()
Definition: Table.hpp:92
void resize(const Uint new_size)
Definition: List.hpp:69
void fill_list(common::List< bool > &list)
BOOST_AUTO_TEST_CASE(InitMPI)
Real e()
Definition of the Unit charge [C].
Definition: Consts.hpp:30
Uint size() const
Definition: Table.hpp:127
void fill_table(common::Table< T > &table)
void fill_list(common::List< T > &list)
void init(int argc=0, char **args=0)
Definition: Comm.cpp:80
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
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
Handle< Component > handle()
Get a handle to the component.
Definition: Component.hpp:179
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
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
Send comments to:
COOLFluiD Web Admin