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

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

Changement de la couleur des sections selectionnes + plein dautres choses

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