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

Last change on this file since 481 was 481, checked in by garnier, 10 years ago

Suppression de methodes deprecated et de quelques warning de compilation

File size: 11.3 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
81  // Main loop !
82  for(unsigned k = 0; k < jobList_.size(); k++) {
83    sector* sector = jobList_[k];
84   
85    // for the moment, we put everything in one line without loops
86    for (unsigned int l=0; l <sector->getSectionsToExecute().size(); l++) {
87      sectionToExecute* section = sector->getSectionsToExecute()[l];
88      abstractSoftware* softw = section->getSoftware();
89     
90#if BAVARD > 0
91    cout << " dataManager::executeAll je m'apprete a executer : " << (jobList_[k]->getSoftware()->getName()) << endl;
92#endif
93     
94      //cout << "k= " << k << ", de " << jobList_[k]->getFirstElement()->getLabel() << " à " << jobList_[k]->getLastElement()->getLabel() << " avec " << softw->getName() << endl;
95     
96    if (softw == NULL) {
97      success = false;
98      consoleMessage("dataManager::executeAll : unknown software");
99      break;
100    }
101      // END OF FIXME
102      success = softw->createInputFile(sector->getParticleBeam(),workingDir);
103    if ( success ) {
104      success = softw->execute(workingDir);
105      if ( success ) {
106        success = softw->buildBeamAfterElements(workingDir);
107      }
108    }
109   
110    if ( success  && (diagnosticBeam_.size() > 0)) {
111      sector->setParticleBeam(&diagnosticBeam_.at(indexElementToIndexDiag_.back()));
112      cout << " execute termine avec succes " << endl;
113    } else {
114      sector->setParticleBeam(NULL);
115      cout << " execute termine en ECHEC " << endl;
116    }
117    if ( !success ) break;
118   
119#if BAVARD > 0
120      cout << "dataManager::executeAll #diagnosticBeam= " << diagnosticBeam_.size() << endl;
121      cout << "dataManager::executeAll #getBeamLineSize()= " << getBeamLineSize() << endl;
122      std::vector< abstractElement* > elements = section->getElements();
123      for (unsigned j = 0; j < elements.size(); j++) {
124        cout << "[" << j << "] " << elements[j]->getNomdElement().getExpandedName() << endl;
125      }
126#endif
127    } //l
128  } //k
129  // if ( currentBeam_deprecated_ ) {
130  // string aml_file = workingDir + "/" + "faisceau_final" + ".aml";
131  //   currentBeam_deprecated_->writeToAMLFile(aml_file);
132  //   // TESTS
133  //   currentBeam_deprecated_->readFromAMLFile(aml_file);
134  // }
135  cout << " dataManager::executeAll() " << endl;
136
137  return success;
138}
139
140void dataManager::saveConfiguration(string folder, string nameOfFile)
141{
142  ofstream outfile;
143  //string name = pspa_->getWorkingDir()+ folder + "/" + nameOfFile + ".save";
144  cout << " dataManager::saveConfiguration : suppression du folder dans le nom de " << endl;
145  cout << " sauvegarde, en attendant que ce soit autre chose que le sessionId" << endl;
146  cout << " et qu'on puisse restaurer normalement" << endl;
147
148  string name = pspa_->getWorkingDir() + "/" + nameOfFile + ".save"; 
149  // make dir if not exist
150  boost::filesystem::create_directories(pspa_->getWorkingDir() + folder + "/");
151 
152  outfile.open(name.c_str(), ios::out);
153  if (!outfile) {
154    cerr<<" ERROR opening output file for persistency "<<name<<endl;
155  }
156 
157  outfile << globParam_.FileOutputFlow();
158
159  // FIXME Francois: Comment sauver la configuration des section ?
160  for (unsigned int a=0; a< jobList_.size(); a++) {
161    outfile << jobList_[a]->getSectorParameters().FileOutputFlow();
162  }
163  // END
164 
165  abstractElement* elPtr;
166  for (unsigned k = 0; k < jobList_.size(); k++) {
167    sector* sector = jobList_[k];
168    for (unsigned l = 0; l < sector->getSectionsToExecute().size(); l++) {
169      sectionToExecute* section = sector->getSectionsToExecute()[l];
170      for (unsigned m = 0; m < section->getElements().size(); m++) {
171        elPtr = section->getElements()[m];
172        outfile << elPtr->FileOutputFlow();
173      }
174    }
175  }
176  outfile.close();
177}
178
179// ecriture sur fichier AML
180void dataManager::writeToAMLFile(string fileName)
181{
182  UAPNode* uap_root = NULL;
183  uap_root = new UAPNode("UAP_root");
184  UAPNode* rep = uap_root->addChild("AML_representation");
185 
186  // root node in the hierarchy
187  UAPNode* lab = rep->addChild("laboratory");
188  lab->addAttribute("name","PSPA lab");
189 
190  // general global parameters--docroot . --http-address 0.0.0.0 --http-port 8077
191  globParam_.InputRep(lab);
192 
193  // accelerator or section of accelerator
194  //UAPNode* acc = lab->addChild("machine");
195  //acc->addAttribute("name",fileName);
196
197  //sectorParam_.InputRep(lab);
198
199  // sequence of elements
200  UAPNode* sect = lab->addChild("sector");
201  sect->addAttribute("name","a_sect");
202
203  abstractElement* elPtr;
204  for (unsigned k = 0; k < jobList_.size(); k++) {
205    sector* sector = jobList_[k];
206    for (unsigned l = 0; l < sector->getSectionsToExecute().size(); l++) {
207      sectionToExecute* section = sector->getSectionsToExecute()[l];
208      for (unsigned m = 0; m < section->getElements().size(); m++) {
209        elPtr = section->getElements()[m];
210        elPtr->InputRep(sect);
211      }
212    }
213  }
214  cout << "!Create the AML file ---------------------------" << endl;
215  AMLReader reader;
216  string aml_file = pspa_->getWorkingDir() + "/" + fileName + ".aml";
217  reader.AMLRepToAMLFile (uap_root, aml_file);
218}
219
220bool dataManager::restoreElements(string inputFileName)
221{
222  cout << "***********************************" << endl;
223  cout << " dataManager::restoreElements() fichier :" << inputFileName << endl << endl;
224
225  ifstream infile;
226  string name = inputFileName;
227  infile.open(name.c_str(), ios::in);
228  if (!infile) {
229    cerr << " error opening input stream " << name << endl;
230    return false;
231  }
232  else cout << " successful opening input stream : " << name << endl;
233 
234  //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
235 
236  // Clear the BeamLine
237  jobList_.clear();
238
239  // FIXME : For test purpose !!!
240  sector* mySect = addNewSector();
241 
242  nomdElements::typedElement elementType;
243  string elementLabel;
244  abstractElement* nouveau = NULL;
245  while (infile >> elementLabel) {
246    if (elementLabel == string("globals") ) {
247      globParam_.raz();
248      globParam_.FileInput(infile);
249    } else if (elementLabel == string("sectors") ) {
250      // FIXME Francois !!!!!!!
251//      sectorParam_.raz();
252//      sectorParam_.FileInput(infile);
253      // END
254    } else {
255      elementType = nomdElements::getTypeFromLabel(elementLabel);
256      nouveau = mySect->addElementAfter(elementType,nouveau);
257      if ( nouveau == NULL ) {
258        cerr << " dataManager::restoreElements() : restore element " << elementLabel << " failed " << endl;
259        return false;
260      } else {
261        nouveau->FileInput(infile);
262      }
263    }
264  }// while
265 
266#if BAVARD > 1
267/*  unsigned k;
268  for(k = 0; k < getBeamLineSize(); k++) {
269    abstractElement* ptr = pspa_->getBeamLine_deprecated()->getAbstractElement(k);
270    cout << "recupere " << ptr->getLabel() << endl;
271  }
272*/
273#endif
274
275  infile.close();
276  return true;
277}
278
279particleBeam* dataManager::getDiagnosticBeam(unsigned index)
280{
281  if (index >= indexElementToIndexDiag_.size() ) {
282    return NULL;
283  } else {
284    int indDiag = indexElementToIndexDiag_.at(index);
285    return &diagnosticBeam_.at(indDiag);
286  }
287}
288
289particleBeam* dataManager::getDiagnosticBeam_deprecated(string elementLabel)
290{
291  // FIXME : Devra etre changé par une récupération par "abstractElement" et non pas par label
292  unsigned int number = 0;
293  abstractElement* elPtr;
294  for (unsigned k = 0; k < jobList_.size(); k++) {
295    sector* sector = jobList_[k];
296    for (unsigned l = 0; l < sector->getSectionsToExecute().size(); l++) {
297      sectionToExecute* section = sector->getSectionsToExecute()[l];
298      for (unsigned m = 0; m < section->getElements().size(); m++) {
299        elPtr = section->getElements()[m];
300        if (elPtr->getLabel() == elementLabel) {
301          return getDiagnosticBeam(number);
302        }
303        number ++;
304      }
305    }
306  }
307  return NULL;
308}
309
310
311// on ne tient pas compte des elements "snapshot" presents dans la beamLine
312void dataManager::donneesRmsEnveloppe(string type,vector<double>& xcor,vector<double>& ycor, string& titre, string& legendx, string& legendy)
313{
314  double longueur = 0.0;
315  double valeur = 0.0;
316  xcor.clear();
317  ycor.clear();
318  titre.clear();
319  titre = " rms enveloppe ";
320  legendx.clear();
321  legendx = " z (m)";
322  if ( type == "x" ) {
323    unsigned i = 0;
324    cout << " dataManager::donneesRmsEnveloppe " << endl;
325    //    for (unsigned k = 0; k < getBeamLineSize(); k++) {
326    for (unsigned k = 0; k < jobList_.size(); k++) {
327      sector* sector = jobList_[k];
328      for (unsigned l = 0; l < sector->getSectionsToExecute().size(); l++) {
329        sectionToExecute* section = sector->getSectionsToExecute()[l];
330        for (unsigned m = 0; m < section->getElements().size(); m++) {
331          abstractElement* elPtr = section->getElements()[m];
332          //     if(elPtr->getNomdElement().getElementType() == snapshot) continue;
333          //      if(elPtr->getNomdElement().getElementType() == fit) continue;
334          particleBeam* beamToDraw = getDiagnosticBeam(i);
335          if ( !beamToDraw->momentRepresentationOk() ) {
336            beamToDraw->buildMomentRepresentation();
337          }
338         
339          longueur += elPtr->getLenghtOfElement();
340          valeur = beamToDraw->getXmaxRms();
341          cout << " dataManager::donneesRmsEnveloppe index = " << k <<  " longueur = " << longueur << " enveloppe : " << valeur << endl;
342          xcor.push_back(0.01*longueur);  // on passe en metres
343          ycor.push_back(valeur);
344          i++;
345        }
346      }
347    }
348    legendy.clear();
349    legendy = " x (cm) ";
350  }
351}
352
353
354
355sector* dataManager::addNewSector() {
356  std::stringstream str;
357  str << getSectors().size()+1;
358  std::string result;
359  str >> result;
360 
361  sector* sect = new sector(this, std::string("sector ") + result);
362  jobList_.push_back(sect);
363  return sect;
364}
365
Note: See TracBrowser for help on using the repository browser.