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

Last change on this file since 436 was 436, checked in by touze, 11 years ago

ajout de secteurs

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