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

Last change on this file since 381 was 381, checked in by lemeur, 11 years ago

complements graphiques, legendes et unification unites

File size: 9.7 KB
Line 
1#include "dataManager.h"
2#include "mathematicalConstants.h"
3#include "PhysicalConstants.h"
4#include "softwareParmela.h"
5#include "softwareTransport.h"
6#include "GWt_pspaApplication.h"
7#include "GWt_console.h"Ò
8#include "softwareGenerator.h"
9#include "softwareTest.h"
10
11#include <boost/filesystem.hpp>
12#include <stdio.h>
13#include <fstream>
14
15dataManager::dataManager(PspaApplication* pspa) :
16currentBeam_(NULL),
17pspa_ (pspa)
18{}
19
20dataManager::~dataManager()
21{
22    unsigned k;
23    for (k=0; k < jobList_.size();k++) {
24        if ( jobList_[k] != NULL ) delete jobList_[k];
25    }
26    if ( currentBeam_ == NULL ) delete currentBeam_;
27}
28
29void dataManager::consoleMessage(string message) {
30    GWt_console* console = static_cast<GWt_console*> (wApp->findWidget ("console"));
31    if (console) console->addConsoleMessage(message);
32    pspa_->processEvents();
33}
34
35string dataManager::getLabelFromElementNumero(int numero)
36{
37    abstractElement* ptr = pspa_->getBeamLine()->getAbstractElement(numero-1);
38    if ( ptr == NULL ) return "";
39    return ptr->getLabel();
40}
41
42
43int dataManager::getNumeroFromElementLabel(string label)
44{
45    int index = -1;
46   
47    for (int k = 0; k < getBeamLineSize() ; k++)
48    {
49        if (pspa_->getBeamLine()->getAbstractElement(k) != NULL){
50            if ( pspa_->getBeamLine()->getAbstractElement(k)->getLabel() == label )
51            {
52                index = (int)k + 1;
53                return index;
54            }
55        }
56    }
57    return index;
58}
59
60
61abstractElement* dataManager::getElementPointerFromNumero(int k)
62{
63  return pspa_->getBeamLine()->getAbstractElement(k-1);
64}
65
66void dataManager::addSectionToExecute(int debut, int fin, nomDeLogiciel prog)
67{
68    jobList_.push_back(new sectionToExecute);
69    jobList_.back()->firstElement = debut;
70    jobList_.back()->lastElement = fin;
71    jobList_.back()->software  = prog;
72}
73
74void dataManager::clearSectionToExecute()
75{
76    unsigned k;
77    for(k = 0; k < jobList_.size(); k++)
78    {
79        if ( jobList_[k] != NULL ) delete jobList_[k];
80    }
81    jobList_.clear();
82}
83
84
85void dataManager::initializeExecution()
86{
87  string workingDir = pspa_->getWorkingDir();
88  if (workingDir == "") {
89    return;
90  }
91  removeFile(workingDir + "parmdesz");
92  removeFile(workingDir + "parmin");
93  removeFile(workingDir + "parin.input0");
94  removeFile(workingDir + "transport.input");
95  removeFile(workingDir + "transport.output");
96  removeFile(workingDir + "generator.in");
97  removeFile(workingDir + "faisceau.ini");
98  removeFile(workingDir + "generator.output");
99  diagnosticBeam_.clear();
100  currentBeam_ = NULL;
101  firstComputedElemNumero_ = getBeamLineSize();
102  lastComputedElemNumero_ = 1;
103  clearSectionToExecute();
104}
105
106void dataManager::removeFile(string nameOfFile)
107{
108    ifstream fileExists;
109    fileExists.open(nameOfFile.c_str(), ios::in);
110    if (fileExists) {
111        fileExists.close();
112        remove(nameOfFile.c_str());
113    }
114}
115
116bool dataManager::executeAll()
117{
118  bool success = true;
119  abstractSoftware* softw = NULL;
120  string workingDir = pspa_->getWorkingDir();
121 
122  for(unsigned k = 0; k < jobList_.size(); k++)
123    {
124      cout << " dataManager::executeAll je m'apprete a executer : " << (jobList_[k]->software).getString() << endl;
125     
126      int debut = jobList_[k]->firstElement;
127      int fin = jobList_[k]->lastElement;
128      softw = createSoftwareConnexion(jobList_[k]->software);
129     
130      if (softw == NULL) {
131        success = false;
132        consoleMessage("dataManager::executeAll : unknown software");
133        break;
134      }
135       
136      success = softw->createInputFile(currentBeam_,debut,fin,workingDir);
137      if ( success ) {
138        success = softw->execute(debut,fin,workingDir);
139        if ( success ) {
140          success = softw->buildBeamAfterElements(debut,fin,diagnosticBeam_,workingDir);
141        }
142      }
143       
144      delete softw;
145      if ( success ) {
146        currentBeam_ = &diagnosticBeam_.back();
147        cout << " execute termine avec succes " << endl;
148      } else {
149        currentBeam_ = NULL;
150        cout << " execute termine en ECHEC " << endl;
151      }
152      if ( !success ) break;
153     
154     
155      if ( debut < firstComputedElemNumero_ ) firstComputedElemNumero_ = debut;
156      if ( fin > lastComputedElemNumero_ ) lastComputedElemNumero_ = fin;
157     
158      //debug
159      cout << "dataManager::executeAll #diagnosticBeam= " << diagnosticBeam_.size() << endl;
160      cout << "dataManager::executeAll #getBeamLineSize()= " << getBeamLineSize() << endl;
161      for (int j = debut; j <= fin; j++) {
162        abstractElement* elPtr= getElementPointerFromNumero(j);
163        cout << "[" << j << "] " << elPtr->getNomdElement().getElementName() << endl;
164      }
165    } //k
166 
167  cout << " dataManager::executeAll premier element : " << firstComputedElemNumero_ << " dernier : " << lastComputedElemNumero_ << endl;
168 
169  return success;
170}
171
172void dataManager::saveConfiguration(string folder, string nameOfFile)
173{
174    ofstream outfile;
175    //    string name = pspa_->getWorkingDir()+ folder + "/" + nameOfFile + ".save";
176    cout << " dataManager::saveConfiguration : suppression du folder dans le nom de " << endl;
177    cout << " sauvegarde, en attendant que ce soit autre chose que le sessionId" << endl;
178    cout << " et qu'on puisse restaurer normalement" << endl;
179    string name = pspa_->getWorkingDir() + "/" + nameOfFile + ".save";
180   
181    // make dir if not exist
182    boost::filesystem::create_directories(pspa_->getWorkingDir() + folder + "/");
183   
184    outfile.open(name.c_str(), ios::out);
185    if (!outfile) {
186        cerr << " error opening output file for persistency " << name << endl;
187    }
188   
189    outfile << globParam_.FileOutputFlow();
190    abstractElement* elPtr;
191    for(unsigned k = 0; k < getBeamLineSize() ; k++)
192    {
193        elPtr = pspa_->getBeamLine()->getAbstractElement(k);
194        outfile << elPtr->FileOutputFlow();
195    }
196    outfile.close();
197}
198
199bool dataManager::restoreElements( string inputFileName)
200{
201    cout << "dataManager::restoreElements() fichier " << inputFileName << endl;
202    ifstream infile;
203    string name = inputFileName;
204    infile.open(name.c_str(), ios::in);
205    if (!infile) {
206        cerr << " error opening input stream " << name << endl;
207        return false;
208    }
209    else cout << " successful opening input stream " << name << endl;
210   
211    string globalTitle;
212    if ( infile >> globalTitle ) {
213        if ( globalTitle == string("globals") ) {
214            globParam_.raz();
215            globParam_.FileInput(infile);
216        } else {
217            cerr << " dataManager::restoreElements ERROR : global parameters seems to be missing" << endl;
218            return false;
219        }
220    } else {
221        cerr << " dataManager::restoreElements ERROR : reading data save file" << endl;
222        return false;
223    }
224   
225    pspa_->getBeamLine()->clear();
226   
227    typedElement elementType;
228    string elementLabel;
229    while (infile >> elementLabel) {
230        elementType = nomdElements::getTypeFromLabel(elementLabel);
231       
232        GWt_abstractElement* nouveau = pspa_->getBeamLine()->addElement(elementType);
233       
234        if ( nouveau == NULL ) {
235            cerr << " dataManager::restoreElements ERROR : restoring element " << elementLabel << " failed " << endl;
236            return false;
237        }
238        nouveau->FileInput(infile);
239    }
240    infile.close();
241   
242    // debug
243    // unsigned k;
244    // for(k = 0; k < getBeamLineSize(); k++) {
245    //     abstractElement* ptr = pspa_->getBeamLine()->getAbstractElement(k);
246    //     cout << "reupere " << ptr->getLabel() << endl;
247    // }
248    return true;
249}
250
251particleBeam* dataManager::getDiagnosticBeam(unsigned index)
252{
253    if (index >= diagnosticBeam_.size()) {
254        return NULL;
255    } else {
256        return &diagnosticBeam_.at(index);
257    }
258}
259
260
261
262// on ne tient pas compte des elements "snapshot" presents dans la beamLine
263void dataManager::donneesRmsEnveloppe(string type,vector<double>& xcor,vector<double>& ycor, string& titre, string& legendx, string& legendy)
264{
265  double longueur = 0.0;
266  double valeur = 0.0;
267  xcor.clear();
268  ycor.clear();
269  titre.clear();
270  titre = " rms enveloppe ";
271  legendx.clear();
272  legendx = " z (m)";
273  if ( type == "x" ) {
274    unsigned i = 0;
275    cout << " dataManager::donneesRmsEnveloppe BeamLineSize = " << getBeamLineSize() << endl;
276    //    for (unsigned k = 0; k < getBeamLineSize(); k++) {
277    for (unsigned k = firstComputedElemNumero_ -1 ; k < lastComputedElemNumero_ ; k++) {
278      abstractElement* elPtr = pspa_->getBeamLine()->getAbstractElement(k);
279           if(elPtr->getNomdElement().getElementType() == snapshot) continue;
280      //      if(elPtr->getNomdElement().getElementType() == fit) continue;
281
282      if ( !diagnosticBeam_.at(i).momentRepresentationOk() ) {
283        diagnosticBeam_.at(i).buildMomentRepresentation();
284      }
285
286      longueur += elPtr->getLenghtOfElement();
287      valeur = diagnosticBeam_.at(i).getXmaxRms();
288      cout << " dataManager::donneesRmsEnveloppe index = " << k <<  " longueur = " << longueur << " enveloppe : " << valeur << endl;
289      xcor.push_back(0.01*longueur);  // on passe en metres
290      ycor.push_back(valeur);
291      i++;
292    }
293    legendy.clear();
294    legendy = " x (cm) ";
295  }
296}
297
298int dataManager::getBeamLineSize() {
299    return pspa_->getBeamLine()->getBeamLineSize();
300}
301
302abstractSoftware* dataManager::createSoftwareConnexion(nomDeLogiciel logi)
303{
304    string inputFileName;
305    if(logi == nomDeLogiciel::parmela) {
306        inputFileName = "parmin";
307        return new softwareParmela(inputFileName, &globParam_, this);
308    } else if (logi == nomDeLogiciel::transport) {
309        inputFileName = "transport.input";
310        return new softwareTransport(inputFileName, &globParam_, this);
311    } else if (logi == nomDeLogiciel::generator) {
312        inputFileName = "generator.in";
313        return new softwareGenerator(inputFileName, &globParam_, this);
314    } else if (logi == nomDeLogiciel::test) {
315        return new softwareTest(inputFileName, &globParam_, this);
316    } else {
317        return NULL;
318    }
319}
Note: See TracBrowser for help on using the repository browser.