Code_TYMPAN  4.4.0
Industrial site acoustic simulation
TYSolver.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012> <EDF-R&D> <FRANCE>
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  * See the GNU General Public License for more details.
11  * You should have received a copy of the GNU General Public License along
12  * with this program; if not, write to the Free Software Foundation, Inc.,
13  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
14  */
15 
16 #include "Tympan/core/logging.h"
25 #include "TYSolver.h"
26 #include "TYAcousticModel.h"
27 #include "TYAcousticPathFinder.h"
28 #include "TYFaceSelector.h"
29 #include "TYTask.h"
30 
32 {
33  // Creation du face selector
35 
36  // Creation du path finder
38 
39  // Creation du acoustic model
41 
42  _tabPolygon.clear();
43 
44  _scene = std::unique_ptr<Scene>(new Scene());
45 
46  // Creation de la collection de thread
47  _pool = NULL;
48 }
49 
51 {
52  if (_pool)
53  {
54  delete _pool;
55  }
56  _pool = NULL;
57 
58  if (_tabTrajets.size() > 0)
59  {
60  _tabTrajets.clear();
61  }
62 }
63 
65  tympan::LPSolverConfiguration configuration)
66 {
67  int nbTrajectsForOneSource = 0;
68  int nbTrajectsTotal = 0;
69  tympan::SolverConfiguration::set(configuration);
70  // Use grid accelerating structure instead of KDTree (default value)
72  "Overwriting Acccelerator solver parameter to 1 (grid accelerating structure)");
73  tympan::SolverConfiguration::get()->Accelerator = 1;
74  // Creation de la collection de thread
76 
77  // Creation du face selector
78  if (!_faceSelector)
79  {
81  }
82 
83  // Creation du path finder
85  {
87  }
88 
89  // Creation du acoustic model
90  if (!_acousticModel)
91  {
93  }
94 
95  // On calcule la structure
96  if (buildCalcStruct(aproblem))
97  {
99  }
100  else
101  {
102  return false;
103  }
104 
105  // Initialisation du path finder
106  _acousticPathFinder->init();
107 
108  // Initialisation du acoustic model
109  _acousticModel->init();
110 
111  tympan::SpectrumMatrix& matrix = aresult.get_data();
112  matrix.resize(aproblem.nreceptors(), aproblem.nsources());
113  tab_acoustic_path& tabRays = aresult.get_path_data();
114  tabRays.clear();
115 
116  // construction trajets et ajout des taches associees
117  for (unsigned int i = 0; i < aproblem.nsources(); i++)
118  {
119  // On reset la thread pool
120  _pool->begin(static_cast<unsigned int>(aproblem.nreceptors()));
121  // reset deque and liberate memory
122  _tabTrajets.clear();
123  nbTrajectsForOneSource = 0;
124 
125  for (unsigned int j = 0; j < aproblem.nreceptors(); j++)
126  {
127  TYTrajet* trajet = new TYTrajet(const_cast<tympan::AcousticProblemModel&>(aproblem).source(i),
128  const_cast<tympan::AcousticProblemModel&>(aproblem).receptor(j));
129  trajet->asrc_idx = i;
130  trajet->arcpt_idx = j;
131  _tabTrajets.push_back(trajet);
132 
133  _pool->push(new TYTask(*this, aproblem.nodes(), aproblem.triangles(), aproblem.materials(),
134  *_tabTrajets.at(nbTrajectsForOneSource), nbTrajectsTotal + 1));
135  nbTrajectsTotal++;
136  nbTrajectsForOneSource++;
137  }
138 
139  // launch threads
140  _pool->startPool();
141 
142  if (!_pool->end())
143  {
144  return false;
145  }
146 
147  // Displaying rays in the GUI
148  bool keepRays = tympan::SolverConfiguration::get()->KeepRays;
149  if (keepRays == true)
150  {
151  for (unsigned int i = 0; i < _tabTrajets.size(); i++)
152  {
153  for (size_t j = 0; j < _tabTrajets.at(i)->get_tab_rays().size(); j++)
154  {
155  tabRays.push_back(_tabTrajets.at(i)->get_tab_rays()[j]);
156  }
157  }
158  }
159 
160  for (int i = 0; i < nbTrajectsForOneSource; i++)
161  {
162  tympan::source_idx sidx = _tabTrajets.at(i)->asrc_idx;
163  tympan::receptor_idx ridx = _tabTrajets.at(i)->arcpt_idx;
164 
165  matrix(ridx, sidx) = _tabTrajets.at(i)->getSpectre();
166  }
167 
168  for (unsigned int cnt = 0; cnt < _tabTrajets.size(); cnt++)
169  {
170  delete _tabTrajets.at(cnt);
171  }
172  }
173 
174  return true;
175 }
176 
177 std::unique_ptr<TYFaceSelector> TYSolver::make_face_selector()
178 {
179  return std::unique_ptr<TYFaceSelector>(new TYFaceSelector(*this));
180 }
181 
182 std::unique_ptr<TYAcousticPathFinder> TYSolver::make_path_finder()
183 {
184  return std::unique_ptr<TYAcousticPathFinder>(new TYAcousticPathFinder(*this));
185 }
186 
187 std::unique_ptr<TYAcousticModel> TYSolver::make_acoustic_model()
188 {
189  return std::unique_ptr<TYAcousticModel>(new TYAcousticModel(*this));
190 }
191 
193 {
194  _tabPolygon.clear();
195  const tympan::nodes_pool_t& nodes = aproblem.nodes();
196  const tympan::triangle_pool_t& triangles = aproblem.triangles();
197 
198  _tabPolygon.reserve(triangles.size());
199 
200  OPoint3D pts[3];
201  for (unsigned int i = 0; i < triangles.size(); i++)
202  {
203  pts[0] = nodes[triangles[i].n[0]];
204  pts[1] = nodes[triangles[i].n[1]];
205  pts[2] = nodes[triangles[i].n[2]];
206 
208  OGeometrie::computeNormal(pts, 3, SI.normal);
209 
210  SI.volume_id = triangles[i].volume_id;
211  SI.tabPoint.push_back(pts[0]);
212  SI.tabPoint.push_back(pts[1]);
213  SI.tabPoint.push_back(pts[2]);
214  SI.material = triangles[i].made_of.get();
215 
216  _tabPolygon.push_back(SI);
217  }
218 
219  return true;
220 }
221 
223 {
224  if (_tabPolygon.empty())
225  {
226  return false;
227  }
228 
229  _scene->clean();
230 
231  Material* m = new Material(); // Only for compatibility, may be suppressed;
232 
233  vec3 pos;
234 
235  for (unsigned int i = 0; i < _tabPolygon.size(); i++)
236  {
237  // Recuperation et convertion de la normale de la surface
238 
239  unsigned int a, b, c;
240 
241  pos = OPoint3Dtovec3(_tabPolygon[i].tabPoint[0]);
242  _scene->addVertex(pos, a);
243 
244  pos = OPoint3Dtovec3(_tabPolygon[i].tabPoint[1]);
245  _scene->addVertex(pos, b);
246 
247  pos = OPoint3Dtovec3(_tabPolygon[i].tabPoint[2]);
248  _scene->addVertex(pos, c);
249 
250  if (dynamic_cast<tympan::AcousticGroundMaterial*>(_tabPolygon[i].material))
251  {
252  // Set last parameter true means triangle is part of the ground
253  (Triangle*)_scene->addTriangle(a, b, c, m, true);
254  }
255  else
256  {
257  (Triangle*)_scene->addTriangle(a, b, c, m);
258  }
259  }
260 
261  _scene->finish(); // Build accelerating structure
262 
263  return true;
264 }
NxReal c
Definition: NxVec3.cpp:317
std::vector< acoustic_path * > tab_acoustic_path
static void computeNormal(OPoint3D *pts, int nbPts, OVector3D &normal)
Computes the normal of the list of points.
Definition: 3d.cpp:1147
virtual void warning(const char *message,...)
Definition: logging.cpp:119
static OMessageManager * get()
Definition: logging.cpp:108
The 3D point class.
Definition: 3d.h:487
Slave threads collection.
Definition: threading.h:225
void begin(unsigned int count)
Begin solver.
Definition: threading.cpp:165
void startPool()
Definition: threading.cpp:173
bool end()
End solver.
Definition: threading.cpp:183
virtual void push(OTask *task)
Add a task to the queue.
Definition: threading.cpp:142
This class mainly define a mesh (list of Shape) used by the Simulation object.
Definition: Scene.h:51
Acoustic model for the default solver.
Build the acoustic path for the 9613 solver.
Building class of the faces list.
std::unique_ptr< TYAcousticModel > make_acoustic_model()
TYAcousticModel builder.
Definition: TYSolver.cpp:187
std::unique_ptr< TYAcousticPathFinder > make_path_finder()
TYAcousticPathFinder builder.
Definition: TYSolver.cpp:182
virtual ~TYSolver()
Destructor.
Definition: TYSolver.cpp:50
TYSolver()
Constructor.
Definition: TYSolver.cpp:31
bool appendTriangleToScene()
Convertion des triangles Tympan en primitives utilisables par ray tracer.
Definition: TYSolver.cpp:222
std::vector< TYTrajet * > _tabTrajets
Vector of TYTrajet.
Definition: TYSolver.h:100
std::unique_ptr< TYFaceSelector > _faceSelector
Pointer to the TYFaceSelector.
Definition: TYSolver.h:82
std::unique_ptr< TYAcousticPathFinder > _acousticPathFinder
Pointer to the TYAcousticPathFinder.
Definition: TYSolver.h:83
virtual bool solve(const tympan::AcousticProblemModel &aproblem, tympan::AcousticResultModel &aresult, tympan::LPSolverConfiguration configuration)
Launch the resolution and get the results.
Definition: TYSolver.cpp:64
OThreadPool * _pool
Definition: TYSolver.h:102
std::vector< TYStructSurfIntersect > _tabPolygon
Vector of TYStructSurfIntersect.
Definition: TYSolver.h:98
bool buildCalcStruct(const tympan::AcousticProblemModel &aproblem)
Definition: TYSolver.cpp:192
std::unique_ptr< TYFaceSelector > make_face_selector()
TYFaceSelector builder.
Definition: TYSolver.cpp:177
std::unique_ptr< Scene > _scene
Pointer to the Scene.
Definition: TYSolver.h:105
std::unique_ptr< TYAcousticModel > _acousticModel
Pointer to the TYAcousticModel.
Definition: TYSolver.h:84
Task of a thread collection for Tympan.
Definition: TYTask.h:32
This class TYTrajet (journey) links a couple Source-Receptor and a collection of paths,...
Definition: TYTrajet.h:35
tympan::source_idx asrc_idx
Definition: TYTrajet.h:226
tympan::receptor_idx arcpt_idx
Definition: TYTrajet.h:230
Triangle class.
Definition: Triangle.h:25
3D vector Vector defined with 3 float numbers
Definition: mathlib.h:108
Describes the ground material, a specific AcousticBuildingMaterial.
Definition: entities.hpp:76
Class to describe the acoustic problem.
const material_pool_t & materials() const
Return array of materials.
size_t nsources() const
Return the total number of sources.
size_t nreceptors() const
Return the total number of receptors.
const nodes_pool_t & nodes() const
Return array of nodes.
const triangle_pool_t & triangles() const
Return array of triangles.
Contains the results of the model solved.
SpectrumMatrix & get_data()
Return the results matrix.
tab_acoustic_path & get_path_data()
Return the array of the acoustic paths.
static void set(LPSolverConfiguration config)
Set a configuration.
Definition: config.cpp:102
static LPSolverConfiguration get()
Get the configuration.
Definition: config.cpp:93
Spectrum matrix N*M used to store results. N is the number of receptors. M is the number of sources.
void resize(size_t nb_receptors, size_t nb_sources)
Resize the matrix (data is cleared)
This file provides class for solver configuration.
Math library.
vec3 OPoint3Dtovec3(const OPoint3D &_p)
Converts a OPoint3D to vec3.
Definition: mathlib.h:440
size_t source_idx
Definition: entities.hpp:381
boost::shared_ptr< SolverConfiguration > LPSolverConfiguration
Definition: interfaces.h:25
std::deque< Point > nodes_pool_t
size_t receptor_idx
Definition: entities.hpp:398
std::deque< AcousticTriangle > triangle_pool_t
Array of AcousticTriangle.
Definition: entities.hpp:199
Describe surface intersections.
string volume_id
Volume id.
TabPoint3D tabPoint
Array of points used for the preselection.
tympan::AcousticMaterialBase * material
Reference to a material.
OVector3D normal
Normal vector to a face.