Code_TYMPAN  4.2.0
Industrial site acoustic simulation
TYCalculManager.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 
22 #include <qcursor.h>
23 #include <qmessagebox.h>
24 #include <QTemporaryFile>
25 
26 #include "Tympan/core/config.h"
27 #include "Tympan/core/chrono.h"
28 #include "Tympan/core/defines.h"
32 #include "Tympan/gui/app/os.h"
40 #include "TYCalculManager.h"
41 
42 
43 #define TR(id) OLocalizator::getString("TYCalculManager", (id))
44 
45 using namespace tympan;
46 
48 {
49  _pCurrentCalcul = NULL;
50 }
51 
53 {
54  _pCurrentCalcul = NULL;
55 }
56 
58 {
59  _pCurrentCalcul = pCalcul;
60 }
61 
63 {
64  return launch(_pCurrentCalcul);
65 }
66 
68 {
69  if (!pCalcul)
70  {
71  return false;
72  }
73 
74  if (pCalcul->getState() == TYCalcul::Locked)
75  {
76  OMessageManager::get()->info("+++ UN RESULTAT MESURE NE PEUX FAIRE L'OBJET D'UN CALCUL +++");
77  return true; // Si le calcul est bloque, il ne peut etre execute
78  }
79 
80  TYProjet *pProject = pCalcul->getProjet();
81 
82  // Clear "calcul" result data before computing (avoid problems when removing some elements)
83  pCalcul->clearResult();
84 
86 
87  // Is the debug option "TYMPAN_DEBUG=keep_tmp_files" enabled?
88  bool keep_tmp_files = must_keep_tmp_files();
89  // Temporary XML files to give the current acoustic problem to the python
90  // script and get the results
91  QTemporaryFile problemfile;
92  problemfile.setFileTemplate(QDir::tempPath() + QString("/XXXXXX.xml"));
93  QTemporaryFile resultfile;
94  resultfile.setFileTemplate(QDir::tempPath() + QString("/XXXXXX.xml"));
95  QTemporaryFile meshfile;
96  meshfile.setFileTemplate(QDir::tempPath() + QString("/XXXXXX.ply"));
97  if(!init_tmp_file(problemfile, keep_tmp_files)
98  || !init_tmp_file(resultfile, keep_tmp_files)
99  || !init_tmp_file(meshfile, keep_tmp_files))
100  {
101  logger.error("Creation de fichier temporaire impossible. Veuillez verifier l'espace disque disponible.");
102  return false;
103  }
104  // Serialize current project
105  try
106  {
107  save_project(problemfile.fileName().toUtf8().data(), pProject);
108  }
109  catch(const tympan::invalid_data& exc)
110  {
111  ostringstream msg;
112  msg << boost::diagnostic_information(exc);
113  logger.error(
114  "Could not export current project. Computation won't be done");
115  logger.debug(msg.str().c_str());
116  return false;
117  }
118  if(keep_tmp_files)
119  {
120  logger.debug(
121  "Le calcul va s'executer en mode debug.\nLes fichiers temporaires ne seront pas supprimes une fois le calcul termine.\nProjet courant non calcule: %s. Projet avec les resultats du calcul: %s. Mesh de l'altimetrie au format ply: %s",
122  problemfile.fileName().toStdString().c_str(),
123  resultfile.fileName().toStdString().c_str(),
124  meshfile.fileName().toStdString().c_str());
125  }
126 
127  // Call python script "solve_tympan_project.py" with: the name of the file
128  // containing the problem, the name of the file where to record
129  // the result and the directory containing the solver plugin to use
130  // to solve the acoustic problem
131  QStringList args;
132  QString absolute_plugins_path (QCoreApplication::applicationDirPath());
133  absolute_plugins_path.append("/");
134  absolute_plugins_path.append(PLUGINS_PATH);
135  QString absolute_pyscript_path (QCoreApplication::applicationDirPath());
136  absolute_pyscript_path.append("/");
137  absolute_pyscript_path.append(SOLVE_PYSCRIPT);
138  args << absolute_pyscript_path << problemfile.fileName() << resultfile.fileName()
139  << meshfile.fileName() << absolute_plugins_path;
140 
141 
142  // Altimetry parameters
143  QString parameters = pCalcul->solverParams;
144  QRegExp altimetry_size_criterion_reg("(MeshElementSizeMax\\s?=\\s?)([0-9]+.[0-9]*)");
145  QRegExp altimetry_refine_mesh_reg("(RefineMesh\\s?=\\s?)(True|False)");
146  QRegExp altimetry_use_volumes_landtakes_reg("(UseVolumesLandtake\\s?=\\s?)(True|False)");
147  int pos_size = altimetry_size_criterion_reg.indexIn(parameters);
148  int pos_refi = altimetry_refine_mesh_reg.indexIn(parameters);
149  int pos_land = altimetry_use_volumes_landtakes_reg.indexIn(parameters);
150  if (pos_size > -1 && pos_refi > -1 && pos_land > -1 ){
151  QString altimetry_size_criterion = altimetry_size_criterion_reg.cap(2);
152  QString altimetry_refine_mesh = altimetry_refine_mesh_reg.cap(2);
153  QString altimetry_use_volumes_landtakes = altimetry_use_volumes_landtakes_reg.cap(2);
154  args << altimetry_size_criterion << altimetry_refine_mesh << altimetry_use_volumes_landtakes;
155  }
156 
157  logger.info(TR("id_msg_go_calcul"));
158 
159  if (!python_gui(args))
160  {
161  return false;
162  }
163  // Then read the result to update the internal model
164  LPTYProjet result;
165  try
166  {
167  result = load_project(resultfile.fileName().toUtf8().data());
168  }
169  catch(const tympan::invalid_data& exc)
170  {
171  ostringstream msg;
172  msg << boost::diagnostic_information(exc);
173  logger.error("Could not import computed project. No results available.");
174  logger.debug(msg.str().c_str());
175  QMessageBox msgBox;
176  msgBox.setText("Le fichier de resultats n'a pas pu etre lu.");
177  msgBox.exec();
178  return false;
179  }
180  // Update the current project with the results of the current acoustic
181  // problem
182  pProject = result.getRealPointer();
183  pCalcul = pProject->getCurrentCalcul();
184  getTYApp()->setCurProjet(result);
185  getTYMainWnd()->getProjetFrame()->setProjet(pProject);
186  // Update site altimetry with the mesh retrieved from the ply file
187  LPTYSiteNode pSite = pProject->getSite();
188  std::deque<OPoint3D> points;
189  std::deque<OTriangle> triangles;
190  std::deque<LPTYSol> materials;
191  pSite->readMesh(points, triangles, materials, meshfile.fileName());
192  pSite->getAltimetry()->plugBackTriangulation(points, triangles, materials);
193  pSite->updateAltiInfra();
194  pSite->updateAcoustique();
195  pProject->updateAltiRecepteurs();
196  // Update graphics
197  pCalcul->getParent()->updateGraphicTree();
198  pCalcul->updateGraphicTree();
199  TYSiteModelerFrame* psiteframe = dynamic_cast<TYSiteModelerFrame*>(getTYMainWnd()->getCurrentModeler());
200  if (psiteframe != nullptr)
201  {
202  psiteframe->getView()->getRenderer()->clearTabSelectedElement();
203  psiteframe->setSite(pSite);
204  psiteframe->getView()->getRenderer()->updateDisplayList();
205  psiteframe->updateView();
206  }
207  getTYMainWnd()->updateModelers(false, false);
209  // Update projet frame
211  // Computation achieved with success
212  logger.info(TR("id_msg_calcul_done"));
213  return true;
214 }
215 
217 {
218  bool ret = true;
219  TYCalcul* pCalcul = NULL;
220  TYProjet* pProjet = getTYApp()->getCurProjet().getRealPointer();
221  if (pProjet)
222  {
223  pCalcul = pProjet->getCurrentCalcul();
224  }
225  else
226  {
227  return true;
228  }
229 
230  if (pCalcul)
231  {
232  ret = true;
234 
235  /*// Question
236  int del = QMessageBox::Yes;
237  if (pCalcul->getIsAcousticModified())
238  {
239  del = QMessageBox::warning(getTYMainWnd(), getTYMainWnd()->windowTitle(), TR("id_msg_del_resultat"), QMessageBox::Yes, QMessageBox::No);
240  }
241  if (del == QMessageBox::Yes)
242  {
243  // Ok pour effacer les resultats (uniquement si c'est un resultat calcule);
244  if (pCalcul->getState() == TYCalcul::Actif)
245  {
246  // Reset du resultat
247  pCalcul->getResultat()->purge();
248  for (unsigned int i=0 ; i<pProjet->getPointsControl().size() ; i++)
249  {
250  pProjet->getPointsControl()[i]->purge(pCalcul);
251  }
252 
253  pCalcul->setIsAcousticModified(false); // La calcul est a jour
254  }
255 
256  ret = true;
257  }
258  else
259  {
260  ret = false;
261  }*/
262  }
263 
264  return ret;
265 }
266 
268 {
269  bool ret = false;
270  TYSiteNode* pSite = dynamic_cast<TYSiteNode*>(pElement);
271  if (pSite != nullptr)
272  {
273  pSite->updateAcoustique();
274  }
275  LPTYAcousticVolumeNode pAccVolNode = dynamic_cast<TYAcousticVolumeNode*>(pElement);
276  if (pAccVolNode._pObj != nullptr)
277  {
278  TYMessageManager::get()->info(TR("id_msg_go_updateacoustic"));
279 
280  TYApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
281 
282  // Calcul acoustique
283  ret = pAccVolNode->updateAcoustic();
284 
285  if (ret)
286  {
287  TYMessageManager::get()->info(TR("id_msg_updateacoustic_done"));
288  }
289  else
290  {
291  TYMessageManager::get()->info(TR("id_msg_updateacoustic_failed"));
292  }
293 
294  // MaJ graphique
295  pAccVolNode->getGraphicObject()->update(true);
296  getTYMainWnd()->updateModelers(false, false);
297 
298  TYApplication::restoreOverrideCursor();
299  }
300  else
301  {
302  LPTYAcousticLine pLine = dynamic_cast<TYAcousticLine*>(pElement);
303  if (pLine._pObj != nullptr)
304  {
305  TYMessageManager::get()->info(TR("id_msg_go_updateacoustic"));
306 
307  TYApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
308 
309  // Calcul acoustique
310  ret = pLine->updateAcoustic();
311 
312  if (ret)
313  {
314  TYMessageManager::get()->info(TR("id_msg_updateacoustic_done"));
315  }
316  else
317  {
318  TYMessageManager::get()->info(TR("id_msg_updateacoustic_failed"));
319  }
320 
321  // MaJ graphique
322  pLine->getGraphicObject()->update(true);
323  getTYMainWnd()->updateModelers(false, false);
324 
325  TYApplication::restoreOverrideCursor();
326  }
327  }
328 
329  return ret;
330 }
331 
#define TR(id)
The base exception class for errors due to invalid data.
Definition: exceptions.h:60
virtual ~TYCalculManager()
Destructeur.
bool python_gui(QStringList args)
Definition: os.cpp:30
virtual void debug(const char *message,...)
Definition: logging.cpp:153
void setCurrent(LPTYCalcul pCalcul)
Set du Calcul et Projet courant.
T * getRealPointer()
Definition: smartptr.h:275
QString solverParams
Definition: TYCalcul.h:367
bool updateAltiRecepteurs()
Definition: TYProjet.cpp:542
TYCalculManager()
Constructeur par defaut.
Frame pour la gestion de site (fichier header)
virtual void updateAcoustique(const bool &force=false)
Definition: TYSiteNode.cpp:928
void setSite(LPTYSiteNode pSite)
Fenetre principale de l&#39;application Tympan (fichier header)
classe de definition d&#39;un projet.
Definition: TYProjet.h:46
bool askForResetResultat()
Previent l&#39;utilisateur que le resultat va etre efface, si celui-ci est valide.
bool updateAcoustic(TYElement *pElement)
Appelle la methode de calcul acoustique du volume node passe.
void setCurProjet(LPTYProjet pProjet)
Set/Get du projet courant.
pour l&#39;application Tympan (fichier header)
void updateDisplayList(void)
bool init_tmp_file(QTemporaryFile &tmp_file, bool keep_file)
void updateModelers(bool clipping=true, bool axesAndGrid=true, bool displayList=true)
void save_project(const char *filename, const LPTYProjet &project)
save a project into an XML file
virtual void updateView(bool clipping=true, bool axesAndGrid=true)
Programme de calcul.
Definition: TYCalcul.h:49
Classe generique pour une fenetre de modeleur (fichier header)
virtual bool updateAcoustic(const bool &force=false)
void setProjet(LPTYProjet pProjet)
TYProjetFrame * getProjetFrame()
Definition: TYMainWindow.h:70
TYElement * getParent() const
Definition: TYElement.h:656
bool must_keep_tmp_files()
static OMessageManager * get()
Definition: logging.cpp:110
TYOpenGLRenderer * getRenderer()
Classe Modeler specialisee pour l&#39;edition des sites.
TYModelerFrame * getCurrentModeler()
Definition: TYMainWindow.h:68
Utilitaires pour les interactions entre l&#39;interface graphique et le système (headers) ...
LPTYProjet getCurProjet()
Set/Get du projet courant.
Definition: TYApplication.h:99
T * _pObj
The real pointer, must derived IRefCount.
Definition: smartptr.h:285
utilitaire pour la gestion des messages dans Tympan (fichier header)
virtual void info(const char *message,...)
Definition: logging.cpp:145
Gestionnaire des calculs acoustiques. Il fait l&#39;interface entre l&#39;IHM et le gestionnaire de donnees p...
LPTYCalcul getCurrentCalcul()
Set/Get du pointeur du Calcul courant.
Definition: TYProjet.h:361
Utilitaires pour les interactions entre l&#39;application tympan et des sous- processus python...
static void setIsSavedOk(const bool &toSave)
Definition: TYElement.h:853
TYRenderWindowInteractor * getView()
virtual void error(const char *message,...)
Definition: logging.cpp:129
int getState()
Get de l&#39;etat du calcul.
Definition: TYCalcul.h:333
void readMesh(std::deque< OPoint3D > &points, std::deque< OTriangle > &triangles, std::deque< LPTYSol > &materials, const QString &filename)
Definition: TYSiteNode.cpp:618
LPTYSiteNode getSite()
Get du site.
Definition: TYProjet.h:147
bool launchCurrent()
Execute le calcul courant.
virtual void updateAltiInfra()
Definition: TYSiteNode.cpp:659
Frame pour la gestion de projet (fichier header)
void setCalculDone(bool state)
LPTYProjet load_project(const char *filename)
load an XML project file
Classe Modeler specialisee pour l&#39;edition des sites (fichier header)
void clearResult()
Definition: TYCalcul.cpp:752
Utilities to load a project and a solver.
TYApplication * getTYApp()
Retourne le pointeur sur l&#39;application.
bool launch(LPTYCalcul pCalcul)
Execute un calcul.
LPTYAltimetrie getAltimetry() const
void plugBackTriangulation(const std::deque< OPoint3D > &points, std::deque< OTriangle > &triangles, const std::deque< LPTYSol > &materials)
plug back triangulation providfed by the TYTopographie
TYMainWindow * getTYMainWnd()
Retourne le pointeur sur la fenetre principale.
TYProjet * getProjet()
Definition: TYCalcul.cpp:73