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

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

definition des compatibilites des elements dans les software

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