COOLFluiD  Release kernel
COOLFluiD is a Collaborative Simulation Environment (CSE) focused on complex MultiPhysics simulations.
utest-mesh-gmsh-parallel.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::gmsh::Reader parallel"
9 
10 #include <iostream>
11 #include <boost/test/unit_test.hpp>
12 
13 #include "common/Log.hpp"
14 #include "common/OptionList.hpp"
15 #include "common/PropertyList.hpp"
16 
17 #include "common/Core.hpp"
18 #include "common/Environment.hpp"
20 #include "common/List.hpp"
21 
22 #include "mesh/Mesh.hpp"
23 #include "mesh/Region.hpp"
24 #include "mesh/MeshReader.hpp"
25 #include "mesh/MeshWriter.hpp"
26 #include "mesh/MeshGenerator.hpp"
27 #include "mesh/MeshTransformer.hpp"
28 #include "mesh/Field.hpp"
29 #include "mesh/Entities.hpp"
30 #include "mesh/Space.hpp"
31 #include "mesh/Dictionary.hpp"
32 #include "mesh/MeshAdaptor.hpp"
33 
35 
36 using namespace std;
37 using namespace boost;
38 using namespace cf3;
39 using namespace cf3::mesh;
40 using namespace cf3::common;
41 
43 
45 {
48  {
49  m_argc = boost::unit_test::framework::master_test_suite().argc;
50  m_argv = boost::unit_test::framework::master_test_suite().argv;
51  }
52 
55  {
56  }
58 
59 
61  int m_argc;
62  char** m_argv;
63 
64 };
65 
67 
68 BOOST_FIXTURE_TEST_SUITE( gmshReaderMPITests_TestSuite, gmshReaderMPITests_Fixture )
69 
70 
73 {
74  Core::instance().initiate(m_argc,m_argv);
75  PE::Comm::instance().init(m_argc,m_argv);
76  Core::instance().environment().options().set("log_level",(Uint)INFO);
77  Core::instance().environment().options().set("regist_signal_handlers",true);
78 }
79 
81 
83 {
84  bool p2=true;
85  bool continuous=true;
86  Uint dim=2;
87  if (1){
88  // Generate a mesh
89  Handle<Mesh> generated_mesh = Core::instance().root().create_component<Mesh>("mesh");
90  boost::shared_ptr< MeshGenerator > generate_mesh = build_component_abstract_type<MeshGenerator>("cf3.mesh.SimpleMeshGenerator","meshgenerator");
91  generate_mesh->options().set("nb_cells",std::vector<Uint>(dim,25));
92  generate_mesh->options().set("lengths",std::vector<Real>(dim,2.));
93  generate_mesh->options().set("mesh",generated_mesh->uri());
94  generate_mesh->options().set("bdry",false);
95  generate_mesh->execute();
96 
97  PE::Comm::instance().barrier();
98 
99  if (p2)
100  {
101  // Create a P2 space and some fields
102  if (continuous)
103  generated_mesh->create_continuous_space("P2","cf3.mesh.LagrangeP2");
104  else
105  generated_mesh->create_discontinuous_space("P2","cf3.mesh.LagrangeP2");
106 
107  Dictionary& P2 = *generated_mesh->get_child("P2")->handle<Dictionary>();
108  CFinfo << "P2 space fully created" << CFendl;
109 
110  Field& glb_idx = P2.create_field("glb_node_idx");
111  for (Uint n=0; n<P2.size(); ++n)
112  glb_idx[n][0] = P2.glb_idx()[n];
113  }
114 
115  // Write a distributed mesh:
116  // - out-utest-mesh-gmsh-parallel_P0.msh
117  // - out-utest-mesh-gmsh-parallel_P1.msh
118  boost::shared_ptr< MeshWriter > write_mesh = build_component_abstract_type<MeshWriter>("cf3.mesh.gmsh.Writer","meshwriter");
119  write_mesh->options().set("mesh",generated_mesh);
120  if (p2)
121  write_mesh->options().set("fields",std::vector<URI>(1,generated_mesh->uri()/"P2/glb_node_idx"));
122  write_mesh->options().set("file",URI("out-utest-mesh-gmsh-parallel.msh"));
123  write_mesh->execute();
124  }
125 
126  PE::Comm::instance().barrier();
127 
128  // Read the two mesh-files created previously
129  boost::shared_ptr< MeshReader > read_mesh = build_component_abstract_type<MeshReader>("cf3.mesh.gmsh.Reader","meshreader");
130  read_mesh->options().set("read_fields",true);
131 
132  std::vector< Handle<Mesh> > meshes(PE::Comm::instance().size());
133  for (Uint p=0; p<PE::Comm::instance().size(); ++p)
134  {
135  meshes[p] = Core::instance().root().create_component<Mesh>("mesh"+to_str(p));
136  read_mesh->options().set("mesh",meshes[p]);
137  read_mesh->options().set("file",URI("out-utest-mesh-gmsh-parallel_P"+to_str(p)+".msh"));
138  read_mesh->execute();
139  CFinfo << "mesh["<<p<<"]: nb_cells = " << meshes[p]->properties().value_str("global_nb_cells") << CFendl;
140  CFinfo << "mesh["<<p<<"]: nb_nodes = " << meshes[p]->properties().value_str("global_nb_nodes") << CFendl;
141 
142  PE::Comm::instance().barrier();
143 
144  boost::shared_ptr< MeshWriter > write_mesh = build_component_abstract_type<MeshWriter>("cf3.mesh.gmsh.Writer","meshwriter");
145  write_mesh->options().set("mesh",meshes[p]);
146 
147  std::cout << meshes[p]->tree() << std::endl;
148  if (p2)
149  {
150  if (continuous)
151  write_mesh->options().set("fields",std::vector<URI>(1,meshes[p]->uri()/"geometry/glb_node_idx"));
152  else
153  write_mesh->options().set("fields",std::vector<URI>(1,meshes[p]->uri()/"discontinuous_geometry/glb_node_idx"));
154  }
155  write_mesh->options().set("file",URI("out-P"+to_str(p)+"-utest-mesh-gmsh-parallel.msh"));
156  write_mesh->execute();
157  }
158 
159  // Merge both meshes into one mesh, regions with same name are merged, otherwise added
160 
161 // boost::shared_ptr<mesh::actions::MergeMeshes> mesh_merger = allocate_component<mesh::actions::MergeMeshes>("merge_meshes");
162  Handle<Mesh> merged_mesh = Core::instance().root().create_component<Mesh>("merged_mesh");
163 
164  {
165  MeshAdaptor mesh_adaptor(*merged_mesh);
166 
167  boost_foreach( const Handle<Mesh>& mesh, meshes)
168  {
169  mesh_adaptor.combine_mesh(*mesh);
170  PE::Comm::instance().barrier();
171  }
172  cf3_assert(merged_mesh->elements().size());
173 // mesh_adaptor.assign_partition_agnostic_global_indices_to_dict(*merged_mesh->get_child("discontinuous_geometry")->handle<Dictionary>());
175  mesh_adaptor.fix_node_ranks();
176  mesh_adaptor.finish();
177  CFinfo << "merged_mesh: local_nb_cells = " << merged_mesh->properties().value_str("local_nb_cells") << CFendl;
178  CFinfo << "merged_mesh: local_nb_nodes = " << merged_mesh->properties().value_str("local_nb_nodes") << CFendl;
179  CFinfo << "merged_mesh: global_nb_cells = " << merged_mesh->properties().value_str("global_nb_cells") << CFendl;
180  CFinfo << "merged_mesh: global_nb_nodes = " << merged_mesh->properties().value_str("global_nb_nodes") << CFendl;
181 // merged_mesh->geometry_fields().update();
182  }
183  PE::Comm::instance().barrier();
184  {
185  // Write the merged mesh
186  CFinfo << "Write file \"out-merged-utest-mesh-gmsh-parallel.msh\" " << CFendl;
187  boost::shared_ptr< MeshWriter > write_mesh = build_component_abstract_type<MeshWriter>("cf3.mesh.gmsh.Writer","meshwriter");
188  write_mesh->options().set("mesh",merged_mesh);
189  if (p2)
190  {
191  if (continuous)
192  write_mesh->options().set("fields",std::vector<URI>(1,merged_mesh->uri()/"geometry/glb_node_idx"));
193  else
194  write_mesh->options().set("fields",std::vector<URI>(1,merged_mesh->uri()/"discontinuous_geometry/glb_node_idx"));
195  }
196  write_mesh->options().set("file",URI("out-merged-utest-mesh-gmsh-parallel.msh"));
197  write_mesh->execute();
198  }
199 
200  PE::Comm::instance().barrier();
201 
202  CFinfo << "Loadbalancing mesh with structure\n" << merged_mesh->tree(true);
203 
204  // Loadbalance the merged mesh
205  boost::shared_ptr<mesh::actions::LoadBalance> load_balancer = allocate_component<mesh::actions::LoadBalance>("load_balance");
206  load_balancer->transform(*merged_mesh);
207  CFinfo << "loadbalanced_mesh: nb_cells = " << merged_mesh->properties().value_str("global_nb_cells") << CFendl;
208  CFinfo << "loadbalanced_mesh: nb_nodes = " << merged_mesh->properties().value_str("global_nb_nodes") << CFendl;
209 
210  PE::Comm::instance().barrier();
211 
212  // Write the loadbalanced mesh
213  {
214  CFinfo << "Write file \"out-loadbalanced-utest-mesh-gmsh-parallel.msh\" " << CFendl;
215  boost::shared_ptr< MeshWriter > write_mesh = build_component_abstract_type<MeshWriter>("cf3.mesh.gmsh.Writer","meshwriter");
216  write_mesh->options().set("mesh",merged_mesh);
217  if (continuous)
218  write_mesh->options().set("fields",std::vector<URI>(1,merged_mesh->uri()/"geometry/glb_node_idx"));
219  else
220  write_mesh->options().set("fields",std::vector<URI>(1,merged_mesh->uri()/"discontinuous_geometry/glb_node_idx"));
221  write_mesh->options().set("file",URI("out-loadbalanced-utest-mesh-gmsh-parallel.msh"));
222  write_mesh->options().set("enable_overlap",false);
223  write_mesh->execute();
224  }
225 }
226 
228 
229 BOOST_AUTO_TEST_CASE( finalize_mpi )
230 {
231  PE::Comm::instance().finalize();
232  Core::instance().terminate();
233 }
234 
236 
237 BOOST_AUTO_TEST_SUITE_END()
238 
239 
#define CFinfo
these are always defined
Definition: Log.hpp:104
Field & create_field(const std::string &name, const Uint cols)
Create a new field in this group.
Definition: Dictionary.cpp:178
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
const std::vector< Handle< Entities > > & elements() const
Definition: Mesh.hpp:70
external boost library namespace
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
#define cf3_assert(a)
Definition: Assertions.hpp:93
URI uri() const
Construct the full path.
Definition: Component.cpp:248
STL namespace.
#define boost_foreach
lowercase version of BOOST_FOREACH
Definition: Foreach.hpp:16
common::URI uri(ComponentWrapper &self)
int m_argc
possibly common functions used on the tests below
#define CFendl
Definition: Log.hpp:109
Common_API std::string to_str(const T &v)
Converts to std::string.
void fix_node_ranks()
Correct ranks of nodes to be unique in all pid's.
PropertyList & properties()
Definition: Component.cpp:842
Basic Classes for Mesh applications used by COOLFluiD.
void remove_duplicate_elements_and_nodes()
Add another mesh to this mesh.
std::string value_str(const std::string &pname) const
gmshReaderMPITests_Fixture()
common setup for each test case
Handle< Component > get_child(const std::string &name)
Definition: Component.cpp:441
Class to adapt the mesh.
Definition: MeshAdaptor.hpp:45
Top-level namespace for coolfluid.
Definition: Action.cpp:18
common::List< Uint > & glb_idx()
Return the global index of every field row.
Definition: Dictionary.hpp:84
std::string tree(bool basic_mode=false, Uint depth=0, Uint recursion_level=0) const
Definition: Component.cpp:785
void finish()
Apply the changes the mesh adaptor for changes and fix inconsistent state.
unsigned int Uint
typedef for unsigned int
Definition: CF.hpp:90
~gmshReaderMPITests_Fixture()
common tear-down for each test case
OptionList & options()
Definition: Component.cpp:856
void set(const std::string &pname, const boost::any &val)
Definition: OptionList.cpp:132
Uint size() const
Number of rows of contained fields.
Definition: Dictionary.cpp:99
Most basic kernel library.
Definition: Action.cpp:19
void combine_mesh(const Mesh &other_mesh)
Add another mesh to this mesh.
BOOST_AUTO_TEST_CASE(init_mpi)
Send comments to:
COOLFluiD Web Admin