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

Last change on this file since 312 was 312, checked in by lemeur, 12 years ago

suppression designation elements par index + fin generator

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