Code_TYMPAN  4.2.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 
64 
65 
68 {
69  int nbTrajectsForOneSource = 0;
70  int nbTrajectsTotal = 0;
71  tympan::SolverConfiguration::set(configuration);
72  // Use grid accelerating structure instead of KDTree (default value)
74  "Overwriting Acccelerator solver parameter to 1 (grid accelerating structure)");
75  tympan::SolverConfiguration::get()->Accelerator = 1;
76  // Creation de la collection de thread
78 
79  // Creation du face selector
81 
82  // Creation du path finder
84 
85  // Creation du acoustic model
87 
88  // On calcule la structure
89  if (buildCalcStruct(aproblem))
90  {
92  }
93  else
94  {
95  return false;
96  }
97 
98  // Initialisation du path finder
99  _acousticPathFinder->init();
100 
101  // Initialisation du acoustic model
102  _acousticModel->init();
103 
104  tympan::SpectrumMatrix& matrix = aresult.get_data();
105  matrix.resize(aproblem.nreceptors(), aproblem.nsources());
106  tab_acoustic_path& tabRays = aresult.get_path_data();
107  tabRays.clear();
108 
109  //construction trajets et ajout des taches associees
110  for(unsigned int i=0; i< aproblem.nsources(); i++)
111  {
112  // On reset la thread pool
113  _pool->begin(aproblem.nreceptors());
114  //reset deque and liberate memory
115  _tabTrajets.clear();
116  nbTrajectsForOneSource =0;
117 
118  for (unsigned int j = 0; j<aproblem.nreceptors(); j++)
119  {
120  TYTrajet *trajet = new TYTrajet(const_cast<tympan::AcousticProblemModel&>(aproblem).source(i), const_cast<tympan::AcousticProblemModel&>(aproblem).receptor(j));
121  trajet->asrc_idx = i;
122  trajet->arcpt_idx = j;
123  _tabTrajets.push_back(trajet);
124 
125  _pool->push(new TYTask(*this, aproblem.nodes(), aproblem.triangles(), aproblem.materials(),*_tabTrajets.at(nbTrajectsForOneSource),nbTrajectsTotal + 1));
126  nbTrajectsTotal++;
127  nbTrajectsForOneSource++;
128  }
129 
130  //launch threads
131  _pool->startPool();
132 
133  if (!_pool->end())
134  {
135  return false;
136  }
137 
138  // Displaying rays in the GUI
139  bool keepRays = tympan::SolverConfiguration::get()->Anime3DKeepRays;
140  if (keepRays == true)
141  {
142  for (unsigned int i=0; i<_tabTrajets.size(); i++)
143  {
144  for (size_t j=0; j<_tabTrajets.at(i)->get_tab_rays().size(); j++)
145  {
146  tabRays.push_back(_tabTrajets.at(i)->get_tab_rays()[j]);
147  }
148  }
149  }
150 
151  for (int i=0; i<nbTrajectsForOneSource; i++)
152  {
153  tympan::source_idx sidx = _tabTrajets.at(i)->asrc_idx;
154  tympan::receptor_idx ridx = _tabTrajets.at(i)->arcpt_idx;
155 
156  matrix(ridx, sidx) = _tabTrajets.at(i)->getSpectre();
157  }
158 
159  for(unsigned int cnt = 0 ; cnt < _tabTrajets.size();cnt++)
160  {
161  delete _tabTrajets.at(cnt);
162  }
163  }
164 
165  return true;
166 }
167 
168 std::unique_ptr<TYFaceSelector> TYSolver::make_face_selector()
169 {
170  return std::unique_ptr<TYFaceSelector>( new TYFaceSelector(*this) );
171 }
172 
173 std::unique_ptr<TYAcousticPathFinder> TYSolver::make_path_finder()
174 {
175  return std::unique_ptr<TYAcousticPathFinder>( new TYAcousticPathFinder(*this) );
176 }
177 
178 std::unique_ptr<TYAcousticModel> TYSolver::make_acoustic_model()
179 {
180  return std::unique_ptr<TYAcousticModel>( new TYAcousticModel(*this) );
181 }
182 
184 {
185  _tabPolygon.clear();
186  const tympan::nodes_pool_t& nodes = aproblem.nodes();
187  const tympan::triangle_pool_t& triangles = aproblem.triangles();
188 
189  _tabPolygon.reserve( triangles.size() );
190 
191  OPoint3D pts[3];
192  for (unsigned int i=0; i<triangles.size(); i++)
193  {
194  pts[0] = nodes[triangles[i].n[0]];
195  pts[1] = nodes[triangles[i].n[1]];
196  pts[2] = nodes[triangles[i].n[2]];
197 
199  OGeometrie::computeNormal(pts, 3, SI.normal);
200 
201  SI.volume_id = triangles[i].volume_id;
202  SI.tabPoint.push_back(pts[0]);
203  SI.tabPoint.push_back(pts[1]);
204  SI.tabPoint.push_back(pts[2]);
205  SI.material = triangles[i].made_of.get();
206 
207  _tabPolygon.push_back(SI);
208  }
209 
210  return true;
211 }
212 
214 {
215  if ( _tabPolygon.empty() )
216  {
217  return false;
218  }
219 
220  _scene->clean();
221 
222  Material *m = new Material(); // Only for compatibility, may be suppressed;
223 
224  vec3 pos;
225 
226  for (unsigned int i = 0; i < _tabPolygon.size(); i++)
227  {
228  //Recuperation et convertion de la normale de la surface
229 
230  unsigned int a, b, c;
231 
232  pos = OPoint3Dtovec3(_tabPolygon[i].tabPoint[0]);
233  _scene->addVertex(pos, a);
234 
235  pos = OPoint3Dtovec3(_tabPolygon[i].tabPoint[1]);
236  _scene->addVertex(pos, b);
237 
238  pos = OPoint3Dtovec3(_tabPolygon[i].tabPoint[2]);
239  _scene->addVertex(pos, c);
240 
241  if ( dynamic_cast<tympan::AcousticGroundMaterial*>(_tabPolygon[i].material) )
242  {
243  // Set last parameter true means triangle is part of the ground
244  (Triangle*)_scene->addTriangle(a, b, c, m, true);
245  }
246  else
247  {
248  (Triangle*)_scene->addTriangle(a, b, c, m);
249  }
250  }
251 
252  _scene->finish(); // Build accelerating structure
253 
254  return true;
255 }
virtual void warning(const char *message,...)
Definition: logging.cpp:121
void startPool()
Definition: threading.cpp:182
std::unique_ptr< TYAcousticModel > _acousticModel
Pointer to the TYAcousticModel.
Definition: TYSolver.h:71
size_t nreceptors() const
Return the total number of receptors.
virtual void push(OTask *task)
Add a task to the queue.
Definition: threading.cpp:151
size_t source_idx
Definition: entities.hpp:335
TYSolver()
Constructor.
Definition: TYSolver.cpp:31
const material_pool_t & materials() const
Return array of materials.
Building class of the faces list.
void resize(size_t nb_receptors, size_t nb_sources)
Resize the matrix (data is cleared)
tympan::receptor_idx arcpt_idx
Definition: TYTrajet.h:187
std::unique_ptr< TYFaceSelector > _faceSelector
Pointer to the TYFaceSelector.
Definition: TYSolver.h:69
This file provides class for solver configuration.
std::unique_ptr< Scene > _scene
Pointer to the Scene.
Definition: TYSolver.h:92
SpectrumMatrix & get_data()
Return the results matrix.
virtual ~TYSolver()
Destructor.
Definition: TYSolver.cpp:50
size_t receptor_idx
Definition: entities.hpp:353
void begin(unsigned int count)
Begin solver.
Definition: threading.cpp:174
std::vector< TYTrajet * > _tabTrajets
Vector of TYTrajet.
Definition: TYSolver.h:87
Task of a thread collection for Tympan.
Definition: TYTask.h:31
tympan::AcousticMaterialBase * material
Triangle material.
bool buildCalcStruct(const tympan::AcousticProblemModel &aproblem)
Definition: TYSolver.cpp:183
NxReal c
Definition: NxVec3.cpp:345
std::unique_ptr< TYAcousticPathFinder > _acousticPathFinder
Pointer to the TYAcousticPathFinder.
Definition: TYSolver.h:70
const triangle_pool_t & triangles() const
Return array of triangles.
3D vector Vector defined with 3 float numbers
Definition: mathlib.h:107
static OMessageManager * get()
Definition: logging.cpp:110
This class TYTrajet (journey) links a couple Source-Receptor and a collection of paths, in addition to the direct path.
Definition: TYTrajet.h:35
string volume_id
Volume id.
TabPoint3D tabPoint
Points array used during the pre-selection.
Spectrum matrix N*M used to store results. N is the number of receptors. M is the number of sources...
std::deque< AcousticTriangle > triangle_pool_t
Array of AcousticTriangle.
Definition: entities.hpp:156
OVector3D normal
Surface normal vector.
bool appendTriangleToScene()
Convertion des triangles Tympan en primitives utilisables par ray tracer.
Definition: TYSolver.cpp:213
Slave threads collection.
Definition: threading.h:254
Triangle class.
Definition: Triangle.h:24
static void set(LPSolverConfiguration config)
Set a configuration.
Definition: config.cpp:108
Build the acoustic path for the default solver.
vec3 OPoint3Dtovec3(const OPoint3D &_p)
Converts a OPoint3D to vec3.
Definition: mathlib.h:313
Class to describe the acoustic problem.
std::unique_ptr< TYFaceSelector > make_face_selector()
TYFaceSelector builder.
Definition: TYSolver.cpp:168
const nodes_pool_t & nodes() const
Return array of nodes.
Math library.
Describe surface intersections.
tympan::source_idx asrc_idx
Definition: TYTrajet.h:183
std::deque< Point > nodes_pool_t
boost::shared_ptr< SolverConfiguration > LPSolverConfiguration
Definition: interfaces.h:24
size_t nsources() const
Return the total number of sources.
This class mainly define a mesh (list of Shape) used by the Simulation object.
Definition: Scene.h:50
tab_acoustic_path & get_path_data()
Return the array of the acoustic paths.
std::vector< TYStructSurfIntersect > _tabPolygon
Vector of TYStructSurfIntersect.
Definition: TYSolver.h:85
Contains the results of the model solved.
The 3D point class.
Definition: 3d.h:484
Acoustic model for the default solver.
OThreadPool * _pool
Definition: TYSolver.h:89
std::unique_ptr< TYAcousticPathFinder > make_path_finder()
TYAcousticPathFinder builder.
Definition: TYSolver.cpp:173
virtual bool solve(const tympan::AcousticProblemModel &aproblem, tympan::AcousticResultModel &aresult, tympan::LPSolverConfiguration configuration)
Launch the resolution and get the results.
Definition: TYSolver.cpp:66
static void computeNormal(OPoint3D *pts, int nbPts, OVector3D &normal)
Computes the normal of the list of points.
Definition: 3d.cpp:1127
std::vector< acoustic_path * > tab_acoustic_path
std::unique_ptr< TYAcousticModel > make_acoustic_model()
TYAcousticModel builder.
Definition: TYSolver.cpp:178
bool end()
End solver.
Definition: threading.cpp:193
static LPSolverConfiguration get()
Get the configuration.
Definition: config.cpp:99