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

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

Correction dans les comboBox

File size: 10.3 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(abstractElement* debut,int debutIndex, abstractElement* fin, int finIndex, abstractSoftware* prog)
67{
68    jobList_.push_back(new sectionToExecute(debut, debutIndex, fin, finIndex, prog));
69}
70
71void dataManager::clearSectionToExecute()
72{
73    unsigned k;
74    for(k = 0; k < jobList_.size(); k++)
75    {
76        if ( jobList_[k] != NULL ) clearSectionToExecute(k);
77    }
78    jobList_.clear();
79}
80
81
82void dataManager::clearSectionToExecute(int a) {
83  if (a<0) return;
84  if (a>= jobList_.size()) return;
85  jobList_.erase (jobList_.begin()+a);
86}
87
88
89void dataManager::initializeExecution()
90{
91  string workingDir = pspa_->getWorkingDir();
92  if (workingDir == "") {
93    return;
94  }
95  removeFile(workingDir + "parmdesz");
96  removeFile(workingDir + "parmin");
97  removeFile(workingDir + "parin.input0");
98  removeFile(workingDir + "transport.input");
99  removeFile(workingDir + "transport.output");
100  removeFile(workingDir + "generator.in");
101  removeFile(workingDir + "faisceau.ini");
102  removeFile(workingDir + "generator.output");
103  diagnosticBeam_.clear();
104  indexElementToIndexDiag_.clear();
105  currentBeam_ = NULL;
106  firstComputedElemNumero_ = getBeamLineSize();
107  lastComputedElemNumero_ = 1;
108  clearSectionToExecute();
109}
110
111void dataManager::removeFile(string nameOfFile)
112{
113    ifstream fileExists;
114    fileExists.open(nameOfFile.c_str(), ios::in);
115    if (fileExists) {
116        fileExists.close();
117        remove(nameOfFile.c_str());
118    }
119}
120
121bool dataManager::executeAll()
122{
123  bool success = true;
124  abstractSoftware* softw = NULL;
125  string workingDir = pspa_->getWorkingDir();
126 
127  for(unsigned k = 0; k < jobList_.size(); k++)
128    {
129      cout << " dataManager::executeAll je m'apprete a executer : " << (jobList_[k]->getSoftware()->getName()) << endl;
130     
131      int debut = jobList_[k]->getElementNumberInSection();
132      int fin = jobList_[k]->getLastElementNumberInSection();
133      softw = jobList_[k]->getSoftware();
134     
135      if (softw == NULL) {
136        success = false;
137        consoleMessage("dataManager::executeAll : unknown software");
138        break;
139      }
140     
141      success = softw->createInputFile(currentBeam_,debut,fin,workingDir);
142      if ( success ) {
143        success = softw->execute(workingDir);
144        if ( success ) {
145          success = softw->buildBeamAfterElements(workingDir);
146        }
147//=======
148//        success = softw->execute(debut,fin,workingDir);
149//        if ( success ) {
150//          success = softw->buildBeamAfterElements(debut,fin,diagnosticBeam_,workingDir);
151//        }
152//>>>>>>> .merge-right.r400
153      }
154     
155      delete softw;
156      if ( success ) {
157        currentBeam_ = &diagnosticBeam_.at(indexElementToIndexDiag_.back());
158        cout << " execute termine avec succes " << endl;
159      } else {
160        currentBeam_ = NULL;
161        cout << " execute termine en ECHEC " << endl;
162      }
163      if ( !success ) break;
164     
165     
166      if ( debut < firstComputedElemNumero_ ) firstComputedElemNumero_ = debut;
167      if ( fin > lastComputedElemNumero_ ) lastComputedElemNumero_ = fin;
168     
169      //debug
170      cout << "dataManager::executeAll #diagnosticBeam= " << diagnosticBeam_.size() << endl;
171      cout << "dataManager::executeAll #getBeamLineSize()= " << getBeamLineSize() << endl;
172      for (int j = debut; j <= fin; j++) {
173        abstractElement* elPtr= getElementPointerFromNumero(j);
174        cout << "[" << j << "] " << elPtr->getNomdElement().getElementName() << endl;
175      }
176    } //k
177 
178  cout << " dataManager::executeAll premier element : " << firstComputedElemNumero_ << " dernier : " << lastComputedElemNumero_ << endl;
179 
180  return success;
181}
182
183void dataManager::saveConfiguration(string folder, string nameOfFile)
184{
185    ofstream outfile;
186    //    string name = pspa_->getWorkingDir()+ folder + "/" + nameOfFile + ".save";
187    cout << " dataManager::saveConfiguration : suppression du folder dans le nom de " << endl;
188    cout << " sauvegarde, en attendant que ce soit autre chose que le sessionId" << endl;
189    cout << " et qu'on puisse restaurer normalement" << endl;
190    string name = pspa_->getWorkingDir() + "/" + nameOfFile + ".save";
191   
192    // make dir if not exist
193    boost::filesystem::create_directories(pspa_->getWorkingDir() + folder + "/");
194   
195    outfile.open(name.c_str(), ios::out);
196    if (!outfile) {
197        cerr << " error opening output file for persistency " << name << endl;
198    }
199   
200    outfile << globParam_.FileOutputFlow();
201    abstractElement* elPtr;
202    for(unsigned k = 0; k < getBeamLineSize() ; k++)
203    {
204        elPtr = pspa_->getBeamLine()->getAbstractElement(k);
205        outfile << elPtr->FileOutputFlow();
206    }
207    outfile.close();
208}
209
210bool dataManager::restoreElements( string inputFileName)
211{
212    cout << "dataManager::restoreElements() fichier " << inputFileName << endl;
213    ifstream infile;
214    string name = inputFileName;
215    infile.open(name.c_str(), ios::in);
216    if (!infile) {
217        cerr << " error opening input stream " << name << endl;
218        return false;
219    }
220    else cout << " successful opening input stream " << name << endl;
221   
222    string globalTitle;
223    if ( infile >> globalTitle ) {
224        if ( globalTitle == string("globals") ) {
225            globParam_.raz();
226            globParam_.FileInput(infile);
227        } else {
228            cerr << " dataManager::restoreElements ERROR : global parameters seems to be missing" << endl;
229            return false;
230        }
231    } else {
232        cerr << " dataManager::restoreElements ERROR : reading data save file" << endl;
233        return false;
234    }
235   
236    pspa_->getBeamLine()->clear();
237   
238    typedElement elementType;
239    string elementLabel;
240    while (infile >> elementLabel) {
241        elementType = nomdElements::getTypeFromLabel(elementLabel);
242       
243        GWt_abstractElement* nouveau = pspa_->getBeamLine()->addElement(elementType);
244       
245        if ( nouveau == NULL ) {
246            cerr << " dataManager::restoreElements ERROR : restoring element " << elementLabel << " failed " << endl;
247            return false;
248        }
249        nouveau->FileInput(infile);
250    }
251    infile.close();
252   
253    // debug
254    // unsigned k;
255    // for(k = 0; k < getBeamLineSize(); k++) {
256    //     abstractElement* ptr = pspa_->getBeamLine()->getAbstractElement(k);
257    //     cout << "reupere " << ptr->getLabel() << endl;
258    // }
259    return true;
260}
261
262particleBeam* dataManager::getDiagnosticBeam(unsigned index)
263{
264  if (index >= indexElementToIndexDiag_.size() ) {
265    return NULL;
266  } else {
267    int indDiag = indexElementToIndexDiag_.at(index);
268    return &diagnosticBeam_.at(indDiag);
269  }
270}
271
272particleBeam* dataManager::getDiagnosticBeam(string elementLabel)
273{
274  return getDiagnosticBeam( getNumeroFromElementLabel(elementLabel) -1);
275}
276
277
278// on ne tient pas compte des elements "snapshot" presents dans la beamLine
279void dataManager::donneesRmsEnveloppe(string type,vector<double>& xcor,vector<double>& ycor, string& titre, string& legendx, string& legendy)
280{
281  double longueur = 0.0;
282  double valeur = 0.0;
283  xcor.clear();
284  ycor.clear();
285  titre.clear();
286  titre = " rms enveloppe ";
287  legendx.clear();
288  legendx = " z (m)";
289  if ( type == "x" ) {
290    unsigned i = 0;
291    cout << " dataManager::donneesRmsEnveloppe BeamLineSize = " << getBeamLineSize() << endl;
292    //    for (unsigned k = 0; k < getBeamLineSize(); k++) {
293    for (unsigned k = firstComputedElemNumero_ -1 ; k < lastComputedElemNumero_ ; k++) {
294      abstractElement* elPtr = pspa_->getBeamLine()->getAbstractElement(k);
295      //     if(elPtr->getNomdElement().getElementType() == snapshot) continue;
296      //      if(elPtr->getNomdElement().getElementType() == fit) continue;
297      particleBeam* beamToDraw = getDiagnosticBeam(i);
298      if ( !beamToDraw->momentRepresentationOk() ) {
299        beamToDraw->buildMomentRepresentation();
300      }
301
302      longueur += elPtr->getLenghtOfElement();
303      valeur = beamToDraw->getXmaxRms();
304      cout << " dataManager::donneesRmsEnveloppe index = " << k <<  " longueur = " << longueur << " enveloppe : " << valeur << endl;
305      xcor.push_back(0.01*longueur);  // on passe en metres
306      ycor.push_back(valeur);
307      i++;
308    }
309    legendy.clear();
310    legendy = " x (cm) ";
311  }
312}
313
314int dataManager::getBeamLineSize() {
315    return pspa_->getBeamLine()->getBeamLineSize();
316}
317
318
319abstractSoftware* dataManager::createSoftwareConnexion(nomDeLogiciel logi)
320{
321    string inputFileName;
322    if(logi == nomDeLogiciel::parmela) {
323        inputFileName = "parmin";
324        return new softwareParmela(inputFileName, &globParam_, this);
325    } else if (logi == nomDeLogiciel::transport) {
326        inputFileName = "transport.input";
327        return new softwareTransport(inputFileName, &globParam_, this);
328    } else if (logi == nomDeLogiciel::generator) {
329        inputFileName = "generator.in";
330        return new softwareGenerator(inputFileName, &globParam_, this);
331    } else if (logi == nomDeLogiciel::test) {
332        return new softwareTest(inputFileName, &globParam_, this);
333    } else {
334        return NULL;
335    }
336}
Note: See TracBrowser for help on using the repository browser.