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
Line 
1#include <stdio.h>
2#include <fstream>
3
4#include "dataManager.h"
5#include "mathematicalConstants.h"
6#include "PhysicalConstants.h"
7
8#include "GWt_pspaApplication.h"
9#include "GWt_console.h"
10#include "GWt_ligneFaisceau.h" // FIXME to be removed !
11
12#include <boost/filesystem.hpp>
13
14#include "UAP/UAPUtilities.hpp"
15#include "AML/AMLReader.hpp"
16
17#define BAVARD 0
18
19dataManager::dataManager(PspaApplication* pspa) :
20pspa_ (pspa)
21{}
22
23dataManager::~dataManager()
24{
25  unsigned k;
26  for (k=0; k < jobList_.size();k++) {
27    if ( jobList_[k] != NULL ) delete jobList_[k];
28  }
29}
30
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();
36}
37
38
39void dataManager::initializeExecution()
40{
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();
54  indexElementToIndexDiag_.clear();
55  for (unsigned int a=0; a< jobList_.size(); a++) {
56      jobList_[a]->clearSectionToExecute();
57  }
58}
59
60void dataManager::removeFile(string nameOfFile)
61{
62  ifstream fileExists;
63  fileExists.open(nameOfFile.c_str(), ios::in);
64  if (fileExists) {
65    fileExists.close();
66    remove(nameOfFile.c_str());
67  }
68}
69
70bool dataManager::executeAll()
71{
72#if BAVARD > 0
73  cout << "***********************************" << endl;
74  cout << " dataManager::executeAll() " << endl << endl;
75#endif
76
77  bool success = true;
78  string workingDir = pspa_->getWorkingDir();
79 
80  // Main loop !
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;
87     
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         
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          }
102   
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;
119   
120#if BAVARD > 0
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          }
127#endif
128        } //l
129    } //k
130 
131  // if ( currentBeam_deprecated_ ) {
132  // string aml_file = workingDir + "/" + "faisceau_final" + ".aml";
133  //   currentBeam_deprecated_->writeToAMLFile(aml_file);
134  //   // TESTS
135  //   currentBeam_deprecated_->readFromAMLFile(aml_file);
136  // }
137 
138  return success;
139}
140
141void dataManager::saveConfiguration(string folder, string nameOfFile)
142{
143  ofstream outfile;
144  //string name = pspa_->getWorkingDir()+ folder + "/" + nameOfFile + ".save";
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();
159
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 
166  abstractElement* elPtr;
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      }
175    }
176  }
177  outfile.close();
178}
179
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");
186 
187  // root node in the hierarchy
188  UAPNode* lab = rep->addChild("laboratory");
189  lab->addAttribute("name","PSPA lab");
190 
191  // general global parameters--docroot . --http-address 0.0.0.0 --http-port 8077
192  globParam_.InputRep(lab);
193 
194  // accelerator or section of accelerator
195  //UAPNode* acc = lab->addChild("machine");
196  //acc->addAttribute("name",fileName);
197
198  //sectorParam_.InputRep(lab);
199
200  // sequence of elements
201  UAPNode* sect = lab->addChild("sector");
202  sect->addAttribute("name","a_sect");
203
204  abstractElement* elPtr;
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    }
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
221bool dataManager::restoreElements(string inputFileName)
222{
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 
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
236 
237  // Clear the BeamLine
238  jobList_.clear();
239
240  // FIXME : For test purpose !!!
241  sector* mySect = addNewSector();
242 
243  nomdElements::typedElement elementType;
244  string elementLabel;
245  abstractElement* nouveau = NULL;
246  while (infile >> elementLabel) {
247    if (elementLabel == string("globals") ) {
248      globParam_.raz();
249      globParam_.FileInput(infile);
250    } else if (elementLabel == string("sectors") ) {
251      // FIXME Francois !!!!!!!
252//      sectorParam_.raz();
253//      sectorParam_.FileInput(infile);
254      // END
255    } else {
256      elementType = nomdElements::getTypeFromLabel(elementLabel);
257      nouveau = mySect->addElementAfter(elementType,nouveau);
258      if ( nouveau == NULL ) {
259        cerr << " dataManager::restoreElements() : restore element " << elementLabel << " failed " << endl;
260        return false;
261      } else {
262        nouveau->FileInput(infile);
263      }
264    }
265  }// while
266 
267#if BAVARD > 1
268/*  unsigned k;
269  for(k = 0; k < getBeamLineSize(); k++) {
270    abstractElement* ptr = pspa_->getBeamLine_deprecated()->getAbstractElement(k);
271    cout << "recupere " << ptr->getLabel() << endl;
272  }
273*/
274#endif
275
276  infile.close();
277  return true;
278}
279
280particleBeam* dataManager::getDiagnosticBeam(unsigned index)
281{
282  if (index >= indexElementToIndexDiag_.size() ) {
283    return NULL;
284  } else {
285    int indDiag = indexElementToIndexDiag_.at(index);
286    return &diagnosticBeam_.at(indDiag);
287  }
288}
289
290particleBeam* dataManager::getDiagnosticBeam_deprecated(string elementLabel)
291{
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;
309}
310
311
312// on ne tient pas compte des elements "snapshot" presents dans la beamLine
313void dataManager::donneesRmsEnveloppe(string type,vector<double>& xcor,vector<double>& ycor, string& titre, string& legendx, string& legendy)
314{
315  double longueur = 0.0;
316  double valeur = 0.0;
317  xcor.clear();
318  ycor.clear();
319  titre.clear();
320  titre = " rms enveloppe ";
321  legendx.clear();
322  legendx = " z (m)";
323  if ( type == "x" ) {
324    unsigned i = 0;
325    cout << " dataManager::donneesRmsEnveloppe " << endl;
326    //    for (unsigned k = 0; k < getBeamLineSize(); k++) {
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        }
347      }
348    }
349    legendy.clear();
350    legendy = " x (cm) ";
351  }
352}
353
354
355
356sector* dataManager::addNewSector() {
357  std::stringstream str;
358  str << getSectors().size()+1;
359  std::string result;
360  str >> result;
361 
362  sector* sect = new sector(this, std::string("sector ") + result);
363  jobList_.push_back(sect);
364  return sect;
365}
366
Note: See TracBrowser for help on using the repository browser.