source: PSPA/Interface_Web/trunk/pspaWT/sources/controler/src/dataManager.cc @ 485

Last change on this file since 485 was 485, checked in by lemeur, 10 years ago

liste des logiciels compatibles dans les tooltip des elements

File size: 11.5 KB
RevLine 
[431]1#include <stdio.h>
2#include <fstream>
3
[27]4#include "dataManager.h"
[243]5#include "mathematicalConstants.h"
6#include "PhysicalConstants.h"
[431]7
8#include "GWt_pspaApplication.h"
9#include "GWt_console.h"
[455]10#include "GWt_ligneFaisceau.h" // FIXME to be removed !
[431]11
[347]12#include <boost/filesystem.hpp>
[27]13
[424]14#include "UAP/UAPUtilities.hpp"
15#include "AML/AMLReader.hpp"
16
[431]17#define BAVARD 0
18
[342]19dataManager::dataManager(PspaApplication* pspa) :
[359]20pspa_ (pspa)
[342]21{}
[305]22
[359]23dataManager::~dataManager()
[50]24{
[431]25  unsigned k;
26  for (k=0; k < jobList_.size();k++) {
27    if ( jobList_[k] != NULL ) delete jobList_[k];
28  }
[50]29}
[38]30
[431]31void dataManager::consoleMessage(string message)
32{
33  GWt_console* console = static_cast<GWt_console*> (wApp->findWidget ("console"));
34  if (console) console->addConsoleMessage(message + "\n");
35  pspa_->processEvents();
[313]36}
[299]37
[118]38
[302]39void dataManager::initializeExecution()
[102]40{
[371]41  string workingDir = pspa_->getWorkingDir();
42  if (workingDir == "") {
43    return;
44  }
45  removeFile(workingDir + "parmdesz");
46  removeFile(workingDir + "parmin");
47  removeFile(workingDir + "parin.input0");
48  removeFile(workingDir + "transport.input");
49  removeFile(workingDir + "transport.output");
50  removeFile(workingDir + "generator.in");
51  removeFile(workingDir + "faisceau.ini");
52  removeFile(workingDir + "generator.output");
53  diagnosticBeam_.clear();
[386]54  indexElementToIndexDiag_.clear();
[455]55  for (unsigned int a=0; a< jobList_.size(); a++) {
56      jobList_[a]->clearSectionToExecute();
57  }
[105]58}
59
[305]60void dataManager::removeFile(string nameOfFile)
[105]61{
[431]62  ifstream fileExists;
63  fileExists.open(nameOfFile.c_str(), ios::in);
64  if (fileExists) {
65    fileExists.close();
66    remove(nameOfFile.c_str());
67  }
[112]68}
69
[313]70bool dataManager::executeAll()
[112]71{
[436]72#if BAVARD > 0
[431]73  cout << "***********************************" << endl;
74  cout << " dataManager::executeAll() " << endl << endl;
[436]75#endif
[431]76
[371]77  bool success = true;
78  string workingDir = pspa_->getWorkingDir();
[431]79 
[455]80  // Main loop !
[482]81  cout << " dataManager::executeAll #jobList_= " << jobList_.size() << endl;
82  for(unsigned k = 0; k < jobList_.size(); k++) 
83    {
84      sector* sector = jobList_[k];
85      cout << " dataManager::executeAll sector name= " << sector->getName() << endl;
86      cout << " dataManager::executeAll #sectionsToExecute= " << sector->getSectionsToExecute().size() << endl;
[455]87     
[482]88      // for the moment, we put everything in one line without loops
89      for(unsigned int l = 0; l < sector->getSectionsToExecute().size(); l++) 
90        {
91          sectionToExecute* section = sector->getSectionsToExecute()[l];
92          abstractSoftware* softw = section->getSoftware();
93         
[485]94          cout << " execution du SOFWARE " << softw->getName() << endl;
95          cout << " adresse section " << section << endl;
96          cout << " adresse faisceau " << sector->getParticleBeam() << endl;
97          if (softw == NULL) {
98            success = false;
99            consoleMessage("dataManager::executeAll : unknown software");
100            break;
101          }
[482]102   
[485]103          success = softw->createInputFile(sector->getParticleBeam(),workingDir);
104          if ( success ) {
105            success = softw->execute(workingDir);
106            if ( success ) {
107              success = softw->buildBeamAfterElements(workingDir);
108            }
109          }
110          cout << " dataManager::executeAll success = " << success << " y-a-ty du faisceau : " << diagnosticBeam_.size() << endl;
111          if ( success  && (diagnosticBeam_.size() > 0)) {
112            sector->setParticleBeam(&diagnosticBeam_.at(indexElementToIndexDiag_.back()));
113            cout << " dataManager::executeAll termine avec succes " << endl;
114          } else {
115            sector->setParticleBeam(NULL);
116            cout << " dataManager::executeAll termine en ECHEC " << endl;
117          }
118          if ( !success ) break;
[431]119   
[455]120#if BAVARD > 0
[482]121          cout << "dataManager::executeAll #diagnosticBeam= " << diagnosticBeam_.size() << endl;
122          cout << "dataManager::executeAll #getBeamLineSize()= " << getBeamLineSize() << endl;
123          std::vector< abstractElement* > elements = section->getElements();
124          for (unsigned j = 0; j < elements.size(); j++) {
125            cout << "[" << j << "] " << elements[j]->getNomdElement().getExpandedName() << endl;
126          }
[436]127#endif
[482]128        } //l
129    } //k
130 
[472]131  // if ( currentBeam_deprecated_ ) {
[442]132  // string aml_file = workingDir + "/" + "faisceau_final" + ".aml";
[472]133  //   currentBeam_deprecated_->writeToAMLFile(aml_file);
[442]134  //   // TESTS
[472]135  //   currentBeam_deprecated_->readFromAMLFile(aml_file);
[442]136  // }
[482]137 
[371]138  return success;
[102]139}
[50]140
[347]141void dataManager::saveConfiguration(string folder, string nameOfFile)
[112]142{
[431]143  ofstream outfile;
[436]144  //string name = pspa_->getWorkingDir()+ folder + "/" + nameOfFile + ".save";
[431]145  cout << " dataManager::saveConfiguration : suppression du folder dans le nom de " << endl;
146  cout << " sauvegarde, en attendant que ce soit autre chose que le sessionId" << endl;
147  cout << " et qu'on puisse restaurer normalement" << endl;
148
149  string name = pspa_->getWorkingDir() + "/" + nameOfFile + ".save"; 
150  // make dir if not exist
151  boost::filesystem::create_directories(pspa_->getWorkingDir() + folder + "/");
152 
153  outfile.open(name.c_str(), ios::out);
154  if (!outfile) {
155    cerr<<" ERROR opening output file for persistency "<<name<<endl;
156  }
157 
158  outfile << globParam_.FileOutputFlow();
[436]159
[455]160  // FIXME Francois: Comment sauver la configuration des section ?
161  for (unsigned int a=0; a< jobList_.size(); a++) {
162    outfile << jobList_[a]->getSectorParameters().FileOutputFlow();
163  }
164  // END
165 
[431]166  abstractElement* elPtr;
[455]167  for (unsigned k = 0; k < jobList_.size(); k++) {
168    sector* sector = jobList_[k];
169    for (unsigned l = 0; l < sector->getSectionsToExecute().size(); l++) {
170      sectionToExecute* section = sector->getSectionsToExecute()[l];
171      for (unsigned m = 0; m < section->getElements().size(); m++) {
172        elPtr = section->getElements()[m];
173        outfile << elPtr->FileOutputFlow();
174      }
[50]175    }
[455]176  }
[431]177  outfile.close();
[112]178}
179
[424]180// ecriture sur fichier AML
181void dataManager::writeToAMLFile(string fileName)
182{
183  UAPNode* uap_root = NULL;
184  uap_root = new UAPNode("UAP_root");
185  UAPNode* rep = uap_root->addChild("AML_representation");
[431]186 
[424]187  // root node in the hierarchy
188  UAPNode* lab = rep->addChild("laboratory");
189  lab->addAttribute("name","PSPA lab");
[431]190 
[436]191  // general global parameters--docroot . --http-address 0.0.0.0 --http-port 8077
[424]192  globParam_.InputRep(lab);
[431]193 
[424]194  // accelerator or section of accelerator
195  //UAPNode* acc = lab->addChild("machine");
196  //acc->addAttribute("name",fileName);
197
[436]198  //sectorParam_.InputRep(lab);
[431]199
[424]200  // sequence of elements
201  UAPNode* sect = lab->addChild("sector");
202  sect->addAttribute("name","a_sect");
203
204  abstractElement* elPtr;
[455]205  for (unsigned k = 0; k < jobList_.size(); k++) {
206    sector* sector = jobList_[k];
207    for (unsigned l = 0; l < sector->getSectionsToExecute().size(); l++) {
208      sectionToExecute* section = sector->getSectionsToExecute()[l];
209      for (unsigned m = 0; m < section->getElements().size(); m++) {
210        elPtr = section->getElements()[m];
211        elPtr->InputRep(sect);
212      }
213    }
[424]214  }
215  cout << "!Create the AML file ---------------------------" << endl;
216  AMLReader reader;
217  string aml_file = pspa_->getWorkingDir() + "/" + fileName + ".aml";
218  reader.AMLRepToAMLFile (uap_root, aml_file);
219}
220
[431]221bool dataManager::restoreElements(string inputFileName)
[112]222{
[431]223  cout << "***********************************" << endl;
224  cout << " dataManager::restoreElements() fichier :" << inputFileName << endl << endl;
225
226  ifstream infile;
227  string name = inputFileName;
228  infile.open(name.c_str(), ios::in);
229  if (!infile) {
230    cerr << " error opening input stream " << name << endl;
231    return false;
232  }
233  else cout << " successful opening input stream : " << name << endl;
234 
[436]235  //NOTE:: je ne sais pas si les "secteurs" seront conservés, aussi pour ne pas trop changer les fichiers je lis les données aprÚs "globals" dans la boucle
[431]236 
[474]237  // Clear the BeamLine
238  jobList_.clear();
239
240  // FIXME : For test purpose !!!
241  sector* mySect = addNewSector();
242 
[431]243  nomdElements::typedElement elementType;
244  string elementLabel;
[479]245  abstractElement* nouveau = NULL;
[431]246  while (infile >> elementLabel) {
[436]247    if (elementLabel == string("globals") ) {
248      globParam_.raz();
249      globParam_.FileInput(infile);
250    } else if (elementLabel == string("sectors") ) {
[455]251      // FIXME Francois !!!!!!!
252//      sectorParam_.raz();
253//      sectorParam_.FileInput(infile);
254      // END
[112]255    } else {
[431]256      elementType = nomdElements::getTypeFromLabel(elementLabel);
[479]257      nouveau = mySect->addElementAfter(elementType,nouveau);
[431]258      if ( nouveau == NULL ) {
259        cerr << " dataManager::restoreElements() : restore element " << elementLabel << " failed " << endl;
260        return false;
261      } else {
262        nouveau->FileInput(infile);
263      }
[112]264    }
[431]265  }// while
266 
[436]267#if BAVARD > 1
[474]268/*  unsigned k;
[431]269  for(k = 0; k < getBeamLineSize(); k++) {
[469]270    abstractElement* ptr = pspa_->getBeamLine_deprecated()->getAbstractElement(k);
[431]271    cout << "recupere " << ptr->getLabel() << endl;
272  }
[474]273*/
[431]274#endif
275
276  infile.close();
277  return true;
[305]278}
[50]279
[305]280particleBeam* dataManager::getDiagnosticBeam(unsigned index)
[179]281{
[386]282  if (index >= indexElementToIndexDiag_.size() ) {
283    return NULL;
284  } else {
285    int indDiag = indexElementToIndexDiag_.at(index);
286    return &diagnosticBeam_.at(indDiag);
287  }
[179]288}
[149]289
[455]290particleBeam* dataManager::getDiagnosticBeam_deprecated(string elementLabel)
[386]291{
[455]292  // FIXME : Devra etre changé par une récupération par "abstractElement" et non pas par label
293  unsigned int number = 0;
294  abstractElement* elPtr;
295  for (unsigned k = 0; k < jobList_.size(); k++) {
296    sector* sector = jobList_[k];
297    for (unsigned l = 0; l < sector->getSectionsToExecute().size(); l++) {
298      sectionToExecute* section = sector->getSectionsToExecute()[l];
299      for (unsigned m = 0; m < section->getElements().size(); m++) {
300        elPtr = section->getElements()[m];
301        if (elPtr->getLabel() == elementLabel) {
302          return getDiagnosticBeam(number);
303        }
304        number ++;
305      }
306    }
307  }
308  return NULL;
[386]309}
[236]310
[359]311
[342]312// on ne tient pas compte des elements "snapshot" presents dans la beamLine
[381]313void dataManager::donneesRmsEnveloppe(string type,vector<double>& xcor,vector<double>& ycor, string& titre, string& legendx, string& legendy)
[342]314{
315  double longueur = 0.0;
316  double valeur = 0.0;
317  xcor.clear();
318  ycor.clear();
[381]319  titre.clear();
320  titre = " rms enveloppe ";
321  legendx.clear();
322  legendx = " z (m)";
[342]323  if ( type == "x" ) {
324    unsigned i = 0;
[455]325    cout << " dataManager::donneesRmsEnveloppe " << endl;
[368]326    //    for (unsigned k = 0; k < getBeamLineSize(); k++) {
[455]327    for (unsigned k = 0; k < jobList_.size(); k++) {
328      sector* sector = jobList_[k];
329      for (unsigned l = 0; l < sector->getSectionsToExecute().size(); l++) {
330        sectionToExecute* section = sector->getSectionsToExecute()[l];
331        for (unsigned m = 0; m < section->getElements().size(); m++) {
332          abstractElement* elPtr = section->getElements()[m];
333          //     if(elPtr->getNomdElement().getElementType() == snapshot) continue;
334          //      if(elPtr->getNomdElement().getElementType() == fit) continue;
335          particleBeam* beamToDraw = getDiagnosticBeam(i);
336          if ( !beamToDraw->momentRepresentationOk() ) {
337            beamToDraw->buildMomentRepresentation();
338          }
339         
340          longueur += elPtr->getLenghtOfElement();
341          valeur = beamToDraw->getXmaxRms();
342          cout << " dataManager::donneesRmsEnveloppe index = " << k <<  " longueur = " << longueur << " enveloppe : " << valeur << endl;
343          xcor.push_back(0.01*longueur);  // on passe en metres
344          ycor.push_back(valeur);
345          i++;
346        }
[316]347      }
[342]348    }
[381]349    legendy.clear();
350    legendy = " x (cm) ";
[342]351  }
[316]352}
[342]353
354
[401]355
[449]356sector* dataManager::addNewSector() {
357  std::stringstream str;
358  str << getSectors().size()+1;
359  std::string result;
360  str >> result;
361 
[455]362  sector* sect = new sector(this, std::string("sector ") + result);
363  jobList_.push_back(sect);
[449]364  return sect;
365}
366
Note: See TracBrowser for help on using the repository browser.