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

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

Désormais il est de nouveau possible d'ajouter des sections et dy affecter des softwares (ne marche pour le moment quavec la 1ere sectionToExecute. Autres améliorations et renommages

File size: 12.2 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]20currentBeam_(NULL),
21pspa_ (pspa)
[342]22{}
[305]23
[359]24dataManager::~dataManager()
[50]25{
[431]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_;
[50]31}
[38]32
[431]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();
[313]38}
[299]39
[455]40string dataManager::getLabelFromElementNumero_deprecated(int numero)
[118]41{
[455]42/*
[469]43 abstractElement* ptr = pspa_->getBeamLine_deprecated()->getAbstractElement(numero-1);
[431]44  if ( ptr == NULL ) return "";
45  return ptr->getLabel();
[455]46*/
47return "";
[118]48}
49
[455]50int dataManager::getNumeroFromElementLabel_deprecated(string label)
[342]51{
[455]52/*  int index = -1;
[431]53  for (int k = 0; k < getBeamLineSize() ; k++) {
[469]54    if (pspa_->getBeamLine_deprecated()->getAbstractElement(k) != NULL) {
55      if ( pspa_->getBeamLine_deprecated()->getAbstractElement(k)->getLabel() == label )
[431]56        {
57          index = (int)k + 1;
58          return index;
59        }
[305]60    }
[431]61  }
62  return index;
[455]63*/
64  return 0;
[305]65}
66
67
[302]68void dataManager::initializeExecution()
[102]69{
[371]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();
[386]83  indexElementToIndexDiag_.clear();
[371]84  currentBeam_ = NULL;
[455]85  for (unsigned int a=0; a< jobList_.size(); a++) {
86      jobList_[a]->clearSectionToExecute();
87  }
[105]88}
89
[305]90void dataManager::removeFile(string nameOfFile)
[105]91{
[431]92  ifstream fileExists;
93  fileExists.open(nameOfFile.c_str(), ios::in);
94  if (fileExists) {
95    fileExists.close();
96    remove(nameOfFile.c_str());
97  }
[112]98}
99
[313]100bool dataManager::executeAll()
[112]101{
[436]102#if BAVARD > 0
[431]103  cout << "***********************************" << endl;
104  cout << " dataManager::executeAll() " << endl << endl;
[436]105#endif
[431]106
[371]107  bool success = true;
108  string workingDir = pspa_->getWorkingDir();
[431]109 
[455]110
111  // Main loop !
[424]112  for(unsigned k = 0; k < jobList_.size(); k++) {
[455]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
[424]121    cout << " dataManager::executeAll je m'apprete a executer : " << (jobList_[k]->getSoftware()->getName()) << endl;
[436]122#endif
[455]123     
124      //cout << "k= " << k << ", de " << jobList_[k]->getFirstElement()->getLabel() << " à " << jobList_[k]->getLastElement()->getLabel() << " avec " << softw->getName() << endl;
125     
[424]126    if (softw == NULL) {
[431]127      success = false;
128      consoleMessage("dataManager::executeAll : unknown software");
129      break;
130    }
131   
[455]132    success = softw->createInputFile(currentBeam_,workingDir);
[431]133    if ( success ) {
134      success = softw->execute(workingDir);
[371]135      if ( success ) {
[431]136        success = softw->buildBeamAfterElements(workingDir);
[371]137      }
[431]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   
[455]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      }
[436]156#endif
[455]157    } //l
[424]158  } //k
[442]159  // if ( currentBeam_ ) {
160  // string aml_file = workingDir + "/" + "faisceau_final" + ".aml";
161  //   currentBeam_->writeToAMLFile(aml_file);
162  //   // TESTS
163  //   currentBeam_->readFromAMLFile(aml_file);
164  // }
[455]165  cout << " dataManager::executeAll() " << endl;
[436]166
[371]167  return success;
[102]168}
[50]169
[347]170void dataManager::saveConfiguration(string folder, string nameOfFile)
[112]171{
[431]172  ofstream outfile;
[436]173  //string name = pspa_->getWorkingDir()+ folder + "/" + nameOfFile + ".save";
[431]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();
[436]188
[455]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 
[431]195  abstractElement* elPtr;
[455]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      }
[50]204    }
[455]205  }
[431]206  outfile.close();
[112]207}
208
[424]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");
[431]215 
[424]216  // root node in the hierarchy
217  UAPNode* lab = rep->addChild("laboratory");
218  lab->addAttribute("name","PSPA lab");
[431]219 
[436]220  // general global parameters--docroot . --http-address 0.0.0.0 --http-port 8077
[424]221  globParam_.InputRep(lab);
[431]222 
[424]223  // accelerator or section of accelerator
224  //UAPNode* acc = lab->addChild("machine");
225  //acc->addAttribute("name",fileName);
226
[436]227  //sectorParam_.InputRep(lab);
[431]228
[424]229  // sequence of elements
230  UAPNode* sect = lab->addChild("sector");
231  sect->addAttribute("name","a_sect");
232
233  abstractElement* elPtr;
[455]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    }
[424]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
[431]250bool dataManager::restoreElements(string inputFileName)
[112]251{
[431]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 
[436]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
[431]265 
[469]266  pspa_->getBeamLine_deprecated()->clear();
[431]267  nomdElements::typedElement elementType;
268  string elementLabel;
269  while (infile >> elementLabel) {
[436]270    if (elementLabel == string("globals") ) {
271      globParam_.raz();
272      globParam_.FileInput(infile);
273    } else if (elementLabel == string("sectors") ) {
[455]274      // FIXME Francois !!!!!!!
275//      sectorParam_.raz();
276//      sectorParam_.FileInput(infile);
277      // END
[112]278    } else {
[431]279      elementType = nomdElements::getTypeFromLabel(elementLabel);
[469]280      GWt_abstractElement* nouveau = pspa_->getBeamLine_deprecated()->addElement(elementType);
[431]281      if ( nouveau == NULL ) {
282        cerr << " dataManager::restoreElements() : restore element " << elementLabel << " failed " << endl;
283        return false;
284      } else {
285        nouveau->FileInput(infile);
286      }
[112]287    }
[431]288  }// while
289 
[436]290#if BAVARD > 1
[431]291  unsigned k;
292  for(k = 0; k < getBeamLineSize(); k++) {
[469]293    abstractElement* ptr = pspa_->getBeamLine_deprecated()->getAbstractElement(k);
[431]294    cout << "recupere " << ptr->getLabel() << endl;
295  }
296#endif
297
298  infile.close();
299  return true;
[305]300}
[50]301
[305]302particleBeam* dataManager::getDiagnosticBeam(unsigned index)
[179]303{
[386]304  if (index >= indexElementToIndexDiag_.size() ) {
305    return NULL;
306  } else {
307    int indDiag = indexElementToIndexDiag_.at(index);
308    return &diagnosticBeam_.at(indDiag);
309  }
[179]310}
[149]311
[455]312particleBeam* dataManager::getDiagnosticBeam_deprecated(string elementLabel)
[386]313{
[455]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;
[386]331}
[236]332
[359]333
[342]334// on ne tient pas compte des elements "snapshot" presents dans la beamLine
[381]335void dataManager::donneesRmsEnveloppe(string type,vector<double>& xcor,vector<double>& ycor, string& titre, string& legendx, string& legendy)
[342]336{
337  double longueur = 0.0;
338  double valeur = 0.0;
339  xcor.clear();
340  ycor.clear();
[381]341  titre.clear();
342  titre = " rms enveloppe ";
343  legendx.clear();
344  legendx = " z (m)";
[342]345  if ( type == "x" ) {
346    unsigned i = 0;
[455]347    cout << " dataManager::donneesRmsEnveloppe " << endl;
[368]348    //    for (unsigned k = 0; k < getBeamLineSize(); k++) {
[455]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        }
[316]369      }
[342]370    }
[381]371    legendy.clear();
372    legendy = " x (cm) ";
[342]373  }
[316]374}
[342]375
[455]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;
[342]386}
387
[401]388
[449]389sector* dataManager::addNewSector() {
390  std::stringstream str;
391  str << getSectors().size()+1;
392  std::string result;
393  str >> result;
394 
[455]395  sector* sect = new sector(this, std::string("sector ") + result);
396  jobList_.push_back(sect);
[449]397  return sect;
398}
399
Note: See TracBrowser for help on using the repository browser.