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

Last change on this file since 455 was 455, checked in by garnier, 11 years ago

grosse modification pour intégrer les sections

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