COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
utest-lss-vector.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 
8 #define BOOST_TEST_DYN_LINK
9 #define BOOST_TEST_MODULE "Test module for some LSS vector ops"
10 
12 
13 #include <fstream>
14 
15 #include <boost/test/unit_test.hpp>
16 #include <boost/assign/std/vector.hpp>
17 #include <boost/lexical_cast.hpp>
18 
19 #include <Teuchos_VerboseObject.hpp>
20 #include <Thyra_VectorStdOps.hpp>
21 
22 #include "common/Log.hpp"
23 #include "math/LSS/System.hpp"
28 
30 #include "common/PE/debug.hpp"
31 #include "common/Environment.hpp"
32 
34 
35 using namespace boost::assign;
36 
37 using namespace cf3;
38 using namespace cf3::math;
39 using namespace cf3::math::LSS;
40 
42 
43 struct LSSAtomicFixture
44 {
47  solvertype("Trilinos"),
48  gid(0),
49  irank(0),
50  nproc(1),
51  neq(1),
52  rank_updatable(0),
53  node_connectivity(0),
54  starting_indices(0)
55  {
56  if (common::PE::Comm::instance().is_initialized())
57  {
58  nproc=common::PE::Comm::instance().size();
59  irank=common::PE::Comm::instance().rank();
60  BOOST_CHECK_EQUAL(nproc,2);
61  }
62  m_argc = boost::unit_test::framework::master_test_suite().argc;
63  m_argv = boost::unit_test::framework::master_test_suite().argv;
64 
65  matrix_builder = "cf3.math.LSS.TrilinosFEVbrMatrix";
66  }
67 
70  {
71  }
72 
75  {
76  if (irank==0)
77  {
78  gid += 0,1,2,6;
79  rank_updatable += 0,0,0,1;
80  } else {
81  gid += 0,3,4,5,6;
82  rank_updatable += 0,1,1,1,1;
83  }
84  cp.insert("gid",gid,1,false);
85  cp.setup(Handle<common::PE::CommWrapper>(cp.get_child("gid")),rank_updatable);
86  }
87 
90  {
91  starting_indices.clear();
92  node_connectivity.clear();
93  if (irank==0)
94  {
95  node_connectivity += 0,3,1,3,2,3,3;
96  starting_indices += 0,2,4,6,7;
97  } else {
98  node_connectivity += 0,4,1,4,2,4,3,4,4;
99  starting_indices += 0,2,4,6,8,9;
100  }
101  sys.create(cp,neq,node_connectivity,starting_indices);
102  }
103 
105  std::string solvertype;
106  std::string matrix_builder;
107 
109  int irank;
110  int nproc;
111  int neq;
112  int m_argc;
113  char** m_argv;
114 
116  std::vector<Uint> gid;
117  std::vector<Uint> rank_updatable;
118 
120  std::vector<Uint> node_connectivity;
121  std::vector<Uint> starting_indices;
122 
123 };
124 
126 
127 BOOST_FIXTURE_TEST_SUITE( LSSAtomicSuite, LSSAtomicFixture )
128 
129 
132 {
133  common::PE::Comm::instance().init(m_argc,m_argv);
134  BOOST_CHECK_EQUAL(common::PE::Comm::instance().is_active(),true);
135  CFinfo.setFilterRankZero(false);
136  common::Core::instance().environment().options().set("log_level", 4u);
137  common::Core::instance().environment().options().set("exception_backtrace", false);
138  common::Core::instance().environment().options().set("exception_outputs", false);
139  //common::PE::wait_for_debugger(0);
140 }
141 
143 
144 BOOST_AUTO_TEST_CASE( test_matrix_apply )
145 {
146  boost::shared_ptr<common::PE::CommPattern> cp_ptr = common::allocate_component<common::PE::CommPattern>("commpattern");
147  common::PE::CommPattern& cp = *cp_ptr;
148  build_commpattern(cp);
149  boost::shared_ptr<LSS::System> sys(common::allocate_component<LSS::System>("sys"));
150  sys->options().option("matrix_builder").change_value(matrix_builder);
151  build_system(*sys,cp);
152  Handle<LSS::Matrix> mat=sys->matrix();
153 
154  mat->reset(1.);
155 
156 
157  Handle<LSS::TrilinosVector> sol(sys->solution());
158  sol->epetra_vector()->Random();
159  sys->rhs()->reset(1.);
160  if(common::PE::Comm::instance().rank() == 1)
161  sol->set_value(4, 2.);
162 
163  mat->apply(sys->rhs(), sys->solution(), 2., 1.);
164 
165  const Uint nb_blocks = sys->rhs()->blockrow_size();
166  const Uint neq = sys->rhs()->neq();
167 
168  for(Uint i = 0; i != nb_blocks; ++i)
169  {
170  for(Uint j = 0; j != neq; ++j)
171  {
172  if(!cp.isUpdatable()[i])
173  continue;
174  Real result, val;
175  sys->rhs()->get_value(i, j, result);
176  sys->solution()->get_value(i, j, val);
177  if(common::PE::Comm::instance().rank() == 1 && i == 4)
178  {
179  BOOST_CHECK_CLOSE(result, 1.+val*2., 1e-12);
180  }
181  else
182  {
183  BOOST_CHECK_CLOSE(result, 1.+(val+2.)*2., 1e-12);
184  }
185  }
186  }
187 }
188 
189 BOOST_AUTO_TEST_CASE( test_thyra_convert )
190 {
191  boost::shared_ptr<common::PE::CommPattern> cp_ptr = common::allocate_component<common::PE::CommPattern>("commpattern");
192  common::PE::CommPattern& cp = *cp_ptr;
193  build_commpattern(cp);
194  boost::shared_ptr<LSS::System> sys(common::allocate_component<LSS::System>("sys"));
195  sys->options().option("matrix_builder").change_value(matrix_builder);
196  build_system(*sys,cp);
197 
198  Handle<LSS::TrilinosVector> sol(sys->solution());
199  sol->reset(1.);
200  Teuchos::RCP< Thyra::VectorBase<Real> > thyra_vec = sol->thyra_vector();
201 
202  const Epetra_Vector& esol = *sol->epetra_vector();
203  const int edim = esol.Map().NumMyElements();
204  for(int i = 0; i != edim; ++i)
205  {
206  BOOST_CHECK_EQUAL(esol[i], 1.);
207  }
208 
209  Thyra::assign(thyra_vec.ptr(), 2.);
210  for(int i = 0; i != edim; ++i)
211  {
212  BOOST_CHECK_EQUAL(esol[i], 2.);
213  }
214 
215  sol->print_native(std::cout);
216 
217  sol->reset(3.);
218  BOOST_CHECK_EQUAL(Thyra::get_ele(*thyra_vec, 0), 3.);
219  BOOST_CHECK_EQUAL(Thyra::get_ele(*thyra_vec, 1), 3.);
220  BOOST_CHECK_EQUAL(Thyra::get_ele(*thyra_vec, 2), 3.);
221  thyra_vec->describe(*Teuchos::VerboseObjectBase::getDefaultOStream(), Teuchos::VERB_EXTREME);
222 }
223 
224 BOOST_AUTO_TEST_CASE( test_assign_update )
225 {
226  boost::shared_ptr<common::PE::CommPattern> cp_ptr = common::allocate_component<common::PE::CommPattern>("commpattern");
227  common::PE::CommPattern& cp = *cp_ptr;
228  build_commpattern(cp);
229  boost::shared_ptr<LSS::System> sys(common::allocate_component<LSS::System>("sys"));
230  sys->options().option("matrix_builder").change_value(matrix_builder);
231  build_system(*sys,cp);
232 
233  sys->solution()->reset(1.);
234  sys->rhs()->reset(2.);
235 
236  sys->solution()->assign(*sys->rhs());
237 
238  const Uint nb_blocks = sys->solution()->blockrow_size();
239 
240  for(Uint i = 0; i != nb_blocks; ++i)
241  {
242  for(Uint j = 0; j != neq; ++j)
243  {
244  Real val;
245  sys->solution()->get_value(i, j, val);
246  BOOST_CHECK_EQUAL(val, 2.);
247  }
248  }
249 
250  sys->solution()->update(*sys->rhs(), 2.);
251 
252  for(Uint i = 0; i != nb_blocks; ++i)
253  {
254  for(Uint j = 0; j != neq; ++j)
255  {
256  Real val;
257  sys->solution()->get_value(i, j, val);
258  BOOST_CHECK_EQUAL(val, 6.);
259  }
260  }
261 }
262 
264 {
265  boost::shared_ptr<common::PE::CommPattern> cp_ptr = common::allocate_component<common::PE::CommPattern>("commpattern");
266  common::PE::CommPattern& cp = *cp_ptr;
267  build_commpattern(cp);
268  boost::shared_ptr<LSS::System> sys(common::allocate_component<LSS::System>("sys"));
269  sys->options().option("matrix_builder").change_value(matrix_builder);
270  build_system(*sys,cp);
271 
272  Handle<LSS::TrilinosVector> sol(sys->solution());
273  sol->reset(1.);
274  Teuchos::RCP< Thyra::VectorBase<Real> > thyra_vec = sol->thyra_vector();
275 
276  Thyra::assign(thyra_vec.ptr(), 2.);
277  sol->sync();
278 
279  const Uint nb_blocks = sys->solution()->blockrow_size();
280  for(Uint i = 0; i != nb_blocks; ++i)
281  {
282  for(Uint j = 0; j != neq; ++j)
283  {
284  Real val;
285  sol->get_value(i, j, val);
286  BOOST_CHECK_EQUAL(val, 2.);
287  }
288  }
289 }
290 
291 BOOST_AUTO_TEST_CASE( test_scale )
292 {
293  boost::shared_ptr<common::PE::CommPattern> cp_ptr = common::allocate_component<common::PE::CommPattern>("commpattern");
294  common::PE::CommPattern& cp = *cp_ptr;
295  build_commpattern(cp);
296  boost::shared_ptr<LSS::System> sys(common::allocate_component<LSS::System>("sys"));
297  sys->options().option("matrix_builder").change_value(matrix_builder);
298  build_system(*sys,cp);
299 
300  Handle<LSS::TrilinosVector> sol(sys->solution());
301  sol->reset(1.);
302  Teuchos::RCP< Thyra::VectorBase<Real> > thyra_vec = sol->thyra_vector();
303 
304  sol->scale(2.);
305 
306  const Uint nb_blocks = sys->solution()->blockrow_size();
307  for(Uint i = 0; i != nb_blocks; ++i)
308  {
309  for(Uint j = 0; j != neq; ++j)
310  {
311  Real val;
312  sol->get_value(i, j, val);
313  BOOST_CHECK_EQUAL(val, 2.);
314  }
315  }
316 }
317 
319 
320 BOOST_AUTO_TEST_CASE( finalize_mpi )
321 {
322  CFinfo.setFilterRankZero(true);
323  common::PE::Comm::instance().finalize();
324  BOOST_CHECK_EQUAL(common::PE::Comm::instance().is_active(),false);
325 }
326 
328 
329 BOOST_AUTO_TEST_SUITE_END()
330 
331 
void build_system(LSS::System &sys, common::PE::CommPattern &cp)
build a test system
#define CFinfo
these are always defined
Definition: Log.hpp:104
void create(cf3::common::PE::CommPattern &cp, Uint neq, std::vector< Uint > &node_connectivity, std::vector< Uint > &starting_indices, const std::vector< Uint > &periodic_links_nodes=std::vector< Uint >(), const std::vector< bool > &periodic_links_active=std::vector< bool >())
Definition: System.cpp:82
void reset()
Set the handle to null.
Definition: Handle.hpp:81
virtual void reset(Real reset_to=0.)=0
Reset Matrix.
Safe pointer to an object. This is the supported method for referring to components.
Definition: Handle.hpp:39
This header collects all the headers needed for the linear system solver, also including configure-ti...
BOOST_AUTO_TEST_CASE(init_mpi)
Basic Classes for Mathematical applications used by COOLFluiD.
void setup(const Handle< CommWrapper > &gid, std::vector< Uint > &rank)
void build_commpattern(common::PE::CommPattern &cp)
create a test commpattern
void insert(const std::string &name, T *&data, const int size, const unsigned int stride=1, const bool needs_update=true)
Real e()
Definition of the Unit charge [C].
Definition: Consts.hpp:30
Handle< Component > get_child(const std::string &name)
Definition: Component.cpp:441
Top-level namespace for coolfluid.
Definition: Action.cpp:18
~LSSAtomicFixture()
common tear-down for each test case
unsigned int Uint
typedef for unsigned int
Definition: CF.hpp:90
LSSAtomicFixture()
common setup for each test case
virtual void apply(const Handle< Vector > &y, const Handle< Vector const > &x, const Real alpha=1., const Real beta=0.)=0
Compute y = alpha*A*x + beta*y.
Send comments to:
COOLFluiD Web Admin