source: PSPA/Interface_Web/branches/12_03_12-managerComboBox/pspaWT/sources/controler/src/dataManager.cc @ 385

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

debut des modifications pour le comboBox : changement de sectionToExecute

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