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

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

Modification pour remettre en marche le Run. Desormais Transport passe, mais aucun test ne permet de dire si c est bon

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