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

Last change on this file since 308 was 308, checked in by garnier, 12 years ago

LigneFaisceau: Correction d un bug qui ne remplissqit pas les parametres des elements

File size: 9.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 "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::getElementPointerFromIndex(int k){
58    return pspa_->getBeamLine()->getAbstractElement(k);
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 indexDeb, indexFin;
88  cout << "controle execution : " << endl;
89  for(k = 0; k < jobList_.size(); k++) {
90    indexDeb = jobList_[k]->firstElement;
91    indexFin = jobList_[k]->lastElement;
92    if ( indexFin  < indexDeb )
93      {
94        diagnostic += " last element before first  for job " + (jobList_[k]->software).getString() + " \n";
95        resul = error;
96        break;
97      }
98    cout << " indexDeb= " << indexDeb << " indexFin= " << indexFin << endl;
99    indexDeb--;
100    indexFin--;
101   
102    abstractElement* elPtr;
103    string checkCompatibility;
104    resul = checkElementsForExec(jobList_[k]->software, indexDeb, indexFin, 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 indexDeb, unsigned indexFin, string& diagnostic)
113{
114    trivaluedBool resul = ok;
115    diagnostic.clear();
116    trivaluedBool accepted;
117    abstractElement* elPtr;
118    for(unsigned j = indexDeb; j <= indexFin; j++) {
119        elPtr = pspa_->getBeamLine()->getAbstractElement(j);
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(debut,fin,workingDir_,resultatPartiel);
195        if ( success ) {
196          success = softw->buildBeamAfterElements(debut, fin, diagnosticBeam_, workingDir_);
197          if ( !success ) {
198            resultatPartiel += " readingt results  failed \n";
199          }
200        }
201      }
202      delete softw;
203      if ( success ) currentBeam_ = &diagnosticBeam_.back();
204      else currentBeam_ = NULL;
205      resul += resultatPartiel;
206      if ( !success ) break;
207    }
208    return success;
209}
210
211
212void dataManager::saveConfiguration( string nameOfFile)
213{
214    ofstream outfile;
215    string name = workingDir_ + nameOfFile + ".save";
216    outfile.open(name.c_str(), ios::out);
217    if (!outfile) {
218        cerr << " error opening output file for persistency " << name << endl;
219    }
220   
221    outfile << globParam_.FileOutputFlow();
222    unsigned k;
223    abstractElement* elPtr;
224    for ( k=0 ; k < getBeamLineSize() ; k++)
225    {
226        elPtr = pspa_->getBeamLine()->getAbstractElement(k);
227        outfile << elPtr->FileOutputFlow();
228    }
229    outfile.close();
230}
231
232
233bool dataManager::restoreElements( string inputFileName)
234{
235    cout << "dataManager::restoreElements() fichier " << inputFileName << endl;
236    ifstream infile;
237    string name = workingDir_ + inputFileName + ".save";
238    infile.open(name.c_str(), ios::in);
239    if (!infile) {
240        cerr << " error opening input stream " << name << endl;
241        return false;
242    }
243    else cout << " successful opening input stream " << name << endl;
244   
245    string globalTitle;
246    if ( infile >> globalTitle ) {
247        if ( globalTitle == string("globals") ) {
248            globParam_.raz();
249            globParam_.FileInput(infile);
250        } else {
251            cerr << " dataManager::restoreElements ERROR : global parameters seems to be missing" << endl;
252            return false;
253        }
254    } else {
255        cerr << " dataManager::restoreElements ERROR : reading data save file" << endl;
256        return false;
257    }
258   
259    pspa_->getBeamLine()->clear();
260   
261    typedElement elementType;
262    string elementLabel;
263    while (infile >> elementLabel) {
264        elementType = nomdElements::getTypeFromLabel(elementLabel);
265       
266        GWt_abstractElement* nouveau = pspa_->getBeamLine()->addElement(elementType);
267       
268        if ( nouveau == NULL ) {
269            cerr << " dataManager::restoreElements ERROR : restoring element " << elementLabel << " failed " << endl;
270            return false;
271        }
272        nouveau->FileInput(infile);
273    }
274    infile.close();
275
276    // debug
277    unsigned k;
278    for(k = 0; k < getBeamLineSize(); k++) {
279        abstractElement* ptr = pspa_->getBeamLine()->getAbstractElement(k);
280       
281        cout << "reupere " << ptr->getLabel() << endl;
282    }
283    return true;
284}
285
286
287particleBeam* dataManager::getDiagnosticBeam(unsigned index)
288{
289   
290   
291    if (  index >= diagnosticBeam_.size() ) {
292        return NULL;
293    }
294    else {
295        return &diagnosticBeam_.at(index);
296    }
297}
298
299
300void dataManager::donneesRmsEnveloppe( string type, unsigned int numeroDeb, unsigned int numeroFin, vector<double>& xcor, vector<double>& ycor)
301{
302    unsigned k;
303    if ( numeroDeb < 1 ) numeroDeb = 1;
304    if ( numeroFin > diagnosticBeam_.size() ) numeroFin = diagnosticBeam_.size();
305   
306    unsigned indexDeb = numeroDeb -1;
307    unsigned indexFin = numeroFin -1;
308   
309    double longueur = 0.0;
310    double valeur = 0.0;
311    xcor.clear();
312    ycor.clear();
313    if ( type == "x" )
314    {
315        for (k= indexDeb; k <= indexFin; k++)
316        {
317            if ( !diagnosticBeam_.at(k).momentRepresentationOk() ) diagnosticBeam_.at(k).buildMomentRepresentation();
318            longueur += pspa_->getBeamLine()->getAbstractElement(k)->getLenghtOfElement();
319            valeur = diagnosticBeam_.at(k).getXmaxRms();
320            xcor.push_back(longueur);
321            ycor.push_back(valeur);
322        }
323    }
324    else
325    {
326        cerr << " dataManager::donneesRmsEnveloppe type " << type << " not programmed " << endl;
327        return;
328    }
329}
330
331int dataManager::getBeamLineSize() {
332    return pspa_->getBeamLine()->getBeamLineSize();
333}
Note: See TracBrowser for help on using the repository browser.