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

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

fin dessins esp. phase + noms de fichiers

File size: 10.2 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 <stdio.h>
12#include <fstream>
13//#include <algorithm>
14
15
16dataManager::dataManager(PspaApplication* pspa) :
17currentBeam_(NULL)
18,pspa_ ( pspa)
19{
20  //  workingDir_ = pspa_->getWorkingDir();
21}
22
23
24dataManager::~dataManager() {
25    unsigned k;
26    for (k=0; k < jobList_.size();k++) {
27        if ( jobList_[k] != NULL ) delete jobList_[k];
28    }
29    if ( currentBeam_ == NULL ) delete currentBeam_;
30}
31
32void dataManager::consoleMessage(string message) {
33   GWt_console* console = static_cast<GWt_console*> (wApp->findWidget ("console"));
34   if (console) console->addConsoleMessage(message);
35   pspa_->processEvents();
36}
37
38string dataManager::getLabelFromElementNumero(int numero)
39{
40    abstractElement* ptr = pspa_->getBeamLine()->getAbstractElement(numero-1);
41    if ( ptr == NULL ) return "";
42    return ptr->getLabel();
43}
44
45
46int dataManager::getNumeroFromElementLabel(string label){
47    int index = -1;
48    unsigned int k;
49   
50    for (k = 0; k < getBeamLineSize() ; k++)
51    {
52        if (pspa_->getBeamLine()->getAbstractElement(k) != NULL){
53            if ( pspa_->getBeamLine()->getAbstractElement(k)->getLabel() == label )
54            {
55                index = (int)k + 1;
56                return index;
57            }
58        }
59    }
60    return index;
61}
62
63
64abstractElement* dataManager::getElementPointerFromNumero(int k){
65    return pspa_->getBeamLine()->getAbstractElement(k-1);
66}
67
68
69void dataManager::addSectionToExecute(int debut, int fin, nomDeLogiciel prog)
70{
71    jobList_.push_back(new sectionToExecute);
72    jobList_.back()->firstElement = debut;
73    jobList_.back()->lastElement = fin;
74    jobList_.back()->software  = prog;
75}
76
77void dataManager::clearSectionToExecute()
78{
79    unsigned k;
80    for(k = 0; k < jobList_.size(); k++)
81    {
82        if ( jobList_[k] != NULL ) delete jobList_[k];
83    }
84    jobList_.clear();
85}
86
87trivaluedBool dataManager::checkExecute(string& diagnostic)
88{
89   cout << "dataManager::checkExecute()" << endl;
90   consoleMessage(" VERIFICATION AVANT EXECUTION ");
91   //   console_ = static_cast<GWt_console*> (wApp->findWidget ("console"));
92   //   if (console_) console_->addConsoleMessage(" VERIFICATION AVANT EXECUTION ");
93
94  trivaluedBool resul = ok;
95  unsigned k,j;
96  diagnostic.clear();
97  unsigned numeroDeb, numeroFin;
98  cout << "controle execution : " << endl;
99  for(k = 0; k < jobList_.size(); k++) {
100    numeroDeb = jobList_[k]->firstElement;
101    numeroFin = jobList_[k]->lastElement;
102    if ( numeroFin  < numeroDeb )
103      {
104        diagnostic += " last element before first  for job " + (jobList_[k]->software).getString() + " \n";
105        resul = error;
106        break;
107      }
108    cout << " numeroDeb= " << numeroDeb << " numeroFin= " << numeroFin << endl;
109    // indexDeb--;
110    // indexFin--;
111   
112    abstractElement* elPtr;
113    string checkCompatibility;
114    resul = checkElementsForExec(jobList_[k]->software, numeroDeb, numeroFin, checkCompatibility);
115    diagnostic += checkCompatibility;
116  }
117  cout << " data manager renvoie resul = " << resul << endl;
118  return resul;
119}
120
121
122trivaluedBool dataManager::checkElementsForExec(nomDeLogiciel logiciel, unsigned numeroDeb, unsigned numeroFin, string& diagnostic)
123{
124    trivaluedBool resul = ok;
125    diagnostic.clear();
126    trivaluedBool accepted;
127    abstractElement* elPtr;
128    for(unsigned j = numeroDeb; j <= numeroFin; j++) {
129        elPtr = pspa_->getBeamLine()->getAbstractElement(j-1);
130        accepted = elPtr->is_accepted_by_software(logiciel);
131        if(accepted  == error  ) {
132            diagnostic += " the element " + elPtr->getNomdElement().getElementName() + " is not allowed with " + logiciel.getString() + " \n";
133            resul = error;
134        } else if ( accepted  == warning ) {
135            diagnostic += " the element " + elPtr->getNomdElement().getElementName() + " will be ignored by " + logiciel.getString() + " \n";
136            if ( resul != error ) resul = warning;
137        }
138    }
139    return resul;
140}
141
142
143
144
145void dataManager::initializeExecution()
146{
147  string workingDir = pspa_->getWorkingDir();
148    if (workingDir == "") {
149        return;
150    }
151    removeFile(workingDir + "parmdesz");
152    removeFile(workingDir + "parmin");
153    removeFile(workingDir + "parin.input0");
154    removeFile(workingDir + "transport.input");
155    removeFile(workingDir + "transport.output");
156    removeFile(workingDir + "generator.in");
157    removeFile(workingDir + "faisceau.ini");
158    removeFile(workingDir + "generator.output");
159    diagnosticBeam_.clear();
160    currentBeam_ = NULL;
161    clearSectionToExecute();
162}
163
164void dataManager::removeFile(string nameOfFile)
165{
166    ifstream fileExists;
167    fileExists.open(nameOfFile.c_str(), ios::in);
168    if (fileExists) {
169        fileExists.close();
170        remove(nameOfFile.c_str());
171    }
172}
173
174bool dataManager::executeAll()
175{
176    bool success = true;
177    //    resul.clear();
178    //    string resultatPartiel;
179    unsigned k;
180    abstractSoftware* softw =NULL;
181    string workingDir = pspa_->getWorkingDir();
182    for(k = 0; k < jobList_.size(); k++)
183    {
184      int debut = jobList_[k]->firstElement;
185      int fin = jobList_[k]->lastElement;
186      cout << " dataManager::executeAll je m'apprete a executer : " << (jobList_[k]->software).getString() << endl;
187
188      softw = createSoftwareConnexion(jobList_[k]->software );
189
190      if ( softw == NULL ) {
191        success = false;
192        consoleMessage("dataManager::executeAll : unknown software");
193        //      resultatPartiel =  " unknown software -- STOP \n ";     
194        break;
195      }
196
197      success = softw->createInputFile(currentBeam_, debut, fin, workingDir);
198      if ( success ) {
199        success = softw->execute(debut,fin,workingDir);
200        if ( success ) {
201          success = softw->buildBeamAfterElements(debut, fin, diagnosticBeam_, workingDir);
202        }
203      }
204      delete softw;
205      if ( success ) {
206        currentBeam_ = &diagnosticBeam_.back();
207        cout << " execute termine avec succes " << endl;
208      } else {
209        currentBeam_ = NULL;
210        cout << " execute termine en ECHEC " << endl;
211      }
212      if ( !success ) break;
213    }
214    return success;
215}
216
217
218void dataManager::saveConfiguration( string nameOfCase)
219{
220    ofstream outfile;
221    string name = pspa_->getWorkingDir() + nameOfCase + ".save";
222    outfile.open(name.c_str(), ios::out);
223    if (!outfile) {
224        cerr << " error opening output file for persistency " << name << endl;
225    }
226   
227    outfile << globParam_.FileOutputFlow();
228    unsigned k;
229    abstractElement* elPtr;
230    for ( k=0 ; k < getBeamLineSize() ; k++)
231    {
232        elPtr = pspa_->getBeamLine()->getAbstractElement(k);
233        outfile << elPtr->FileOutputFlow();
234    }
235    outfile.close();
236}
237
238
239bool dataManager::restoreElements( string inputFileName)
240{
241    cout << "dataManager::restoreElements() fichier " << inputFileName << endl;
242    ifstream infile;
243    string name = inputFileName;
244    infile.open(name.c_str(), ios::in);
245    if (!infile) {
246        cerr << " error opening input stream " << name << endl;
247        return false;
248    }
249    else cout << " successful opening input stream " << name << endl;
250   
251    string globalTitle;
252    if ( infile >> globalTitle ) {
253        if ( globalTitle == string("globals") ) {
254            globParam_.raz();
255            globParam_.FileInput(infile);
256        } else {
257            cerr << " dataManager::restoreElements ERROR : global parameters seems to be missing" << endl;
258            return false;
259        }
260    } else {
261        cerr << " dataManager::restoreElements ERROR : reading data save file" << endl;
262        return false;
263    }
264   
265    pspa_->getBeamLine()->clear();
266   
267    typedElement elementType;
268    string elementLabel;
269    while (infile >> elementLabel) {
270        elementType = nomdElements::getTypeFromLabel(elementLabel);
271       
272        GWt_abstractElement* nouveau = pspa_->getBeamLine()->addElement(elementType);
273       
274        if ( nouveau == NULL ) {
275            cerr << " dataManager::restoreElements ERROR : restoring element " << elementLabel << " failed " << endl;
276            return false;
277        }
278        nouveau->FileInput(infile);
279    }
280    infile.close();
281
282    // debug
283    unsigned k;
284    for(k = 0; k < getBeamLineSize(); k++) {
285        abstractElement* ptr = pspa_->getBeamLine()->getAbstractElement(k);
286       
287        cout << "reupere " << ptr->getLabel() << endl;
288    }
289    return true;
290}
291
292
293particleBeam* dataManager::getDiagnosticBeam(unsigned index)
294{
295   
296   
297    if (  index >= diagnosticBeam_.size() ) {
298        return NULL;
299    }
300    else {
301        return &diagnosticBeam_.at(index);
302    }
303}
304
305
306void dataManager::donneesRmsEnveloppe( string type, unsigned int numeroDeb, unsigned int numeroFin, vector<double>& xcor, vector<double>& ycor)
307{
308    unsigned k;
309    if ( numeroDeb < 1 ) numeroDeb = 1;
310    if ( numeroFin > diagnosticBeam_.size() ) numeroFin = diagnosticBeam_.size();
311       
312    double longueur = 0.0;
313    double valeur = 0.0;
314    xcor.clear();
315    ycor.clear();
316    if ( type == "x" )
317    {
318        for (k= numeroDeb; k <= numeroFin; k++)
319        {
320            if ( !diagnosticBeam_.at(k-1).momentRepresentationOk() ) diagnosticBeam_.at(k-1).buildMomentRepresentation();
321            longueur += pspa_->getBeamLine()->getAbstractElement(k-1)->getLenghtOfElement();
322            valeur = diagnosticBeam_.at(k-1).getXmaxRms();
323            xcor.push_back(longueur);
324            ycor.push_back(valeur);
325        }
326    }
327    else
328    {
329        cerr << " dataManager::donneesRmsEnveloppe type " << type << " not programmed " << endl;
330        return;
331    }
332}
333
334int dataManager::getBeamLineSize() {
335    return pspa_->getBeamLine()->getBeamLineSize();
336}
337
338abstractSoftware* dataManager::createSoftwareConnexion(nomDeLogiciel logi) {
339
340      string inputFileName;
341      if(logi == nomDeLogiciel::parmela) {
342        inputFileName = "parmin";
343        return new softwareParmela(inputFileName, &globParam_, this);
344      } else if (logi == nomDeLogiciel::transport) {
345        inputFileName = "transport.input";
346        return new softwareTransport(inputFileName, &globParam_, this);
347      } else if (logi == nomDeLogiciel::generator) {
348        inputFileName = "generator.in";
349        return new softwareGenerator(inputFileName, &globParam_, this);
350      } else if (logi == nomDeLogiciel::test) {
351        return new softwareTest(inputFileName, &globParam_, this);
352      } else {
353        return NULL;
354      }
355
356}
Note: See TracBrowser for help on using the repository browser.