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

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

sauvgarde de la configuration sur fichier format AML

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