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

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

correction de correction de bug (beam2Moments::readFromTransportOutput)

File size: 11.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) :
16  currentBeam_(NULL),
17  pspa_ (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
85trivaluedBool dataManager::checkExecute(string& diagnostic)
86{
87  cout << "dataManager::checkExecute()" << endl;
88  consoleMessage(" VERIFICATION AVANT EXECUTION ");
89 
90  trivaluedBool resul = ok;
91  diagnostic.clear();
92 
93  for(unsigned k = 0; k < jobList_.size(); k++) {
94   
95    unsigned numeroDeb = jobList_[k]->firstElement;
96    unsigned numeroFin = jobList_[k]->lastElement;
97   
98    if ( numeroFin  < numeroDeb ) {
99      diagnostic += " last element before first for job " + (jobList_[k]->software).getString() + " \n";
100      resul = error;
101      break;
102    }
103
104    string checkCompatibility;
105    resul = checkElementsForExec(jobList_[k]->software,numeroDeb,numeroFin,checkCompatibility);
106    diagnostic += checkCompatibility;
107  } //k
108
109  return resul;
110}
111
112trivaluedBool dataManager::checkElementsForExec(nomDeLogiciel logiciel,unsigned numeroDeb,unsigned numeroFin,string& diagnostic)
113{
114  trivaluedBool resul = ok;
115  trivaluedBool accepted;
116
117  diagnostic.clear(); 
118  abstractElement* elPtr;
119
120  for(unsigned j = numeroDeb; j <= numeroFin; j++) {
121    elPtr = pspa_->getBeamLine()->getAbstractElement(j-1);
122    accepted = elPtr->is_accepted_by_software(logiciel);
123
124    if(accepted == error) {
125      diagnostic += " the element " + elPtr->getNomdElement().getElementName() + " is not allowed with " + logiciel.getString() + " \n";
126      resul = error;
127    } else if (accepted  == warning) {
128      diagnostic += " the element " + elPtr->getNomdElement().getElementName() + " will be ignored by " + logiciel.getString() + " \n";
129      if ( resul != error ) resul = warning;
130    }
131  }
132
133  return resul;
134}
135
136void dataManager::initializeExecution()
137{
138  string workingDir = pspa_->getWorkingDir();
139    if (workingDir == "") {
140        return;
141    }
142    removeFile(workingDir + "parmdesz");
143    removeFile(workingDir + "parmin");
144    removeFile(workingDir + "parin.input0");
145    removeFile(workingDir + "transport.input");
146    removeFile(workingDir + "transport.output");
147    removeFile(workingDir + "generator.in");
148    removeFile(workingDir + "faisceau.ini");
149    removeFile(workingDir + "generator.output");
150    diagnosticBeam_.clear();
151    currentBeam_ = NULL;
152    clearSectionToExecute();
153}
154
155void dataManager::removeFile(string nameOfFile)
156{
157    ifstream fileExists;
158    fileExists.open(nameOfFile.c_str(), ios::in);
159    if (fileExists) {
160        fileExists.close();
161        remove(nameOfFile.c_str());
162    }
163}
164
165bool dataManager::executeAll()
166{
167  bool success = true;
168  abstractSoftware* softw = NULL;
169  string workingDir = pspa_->getWorkingDir();
170
171  for(unsigned k = 0; k < jobList_.size(); k++)
172    { 
173      cout << " dataManager::executeAll je m'apprete a executer : " << (jobList_[k]->software).getString() << endl;
174     
175      int debut = jobList_[k]->firstElement;
176      int fin = jobList_[k]->lastElement;
177      softw = createSoftwareConnexion(jobList_[k]->software);
178     
179      if (softw == NULL) {
180        success = false;
181        consoleMessage("dataManager::executeAll : unknown software");
182        break;
183      }
184
185      success = softw->createInputFile(currentBeam_,debut,fin,workingDir);
186      if ( success ) {
187        success = softw->execute(debut,fin,workingDir);
188        if ( success ) {
189          success = softw->buildBeamAfterElements(debut,fin,diagnosticBeam_,workingDir);
190        }
191      }
192     
193      delete softw;
194      if ( success ) {
195        currentBeam_ = &diagnosticBeam_.back();
196        cout << " execute termine avec succes " << endl;
197      } else {
198        currentBeam_ = NULL;
199        cout << " execute termine en ECHEC " << endl;
200      }
201      if ( !success ) break;
202 
203      //debug
204      cout << "dataManager::executeAll #diagnosticBeam= " << diagnosticBeam_.size() << endl;
205      cout << "dataManager::executeAll #getBeamLineSize()= " << getBeamLineSize() << endl;
206      for (int j = debut; j <= fin; j++) {
207        abstractElement* elPtr= getElementPointerFromNumero(j);
208        cout << "[" << j << "] " << elPtr->getNomdElement().getElementName() << endl;
209      } 
210
211    } //k
212 
213  return success;
214}
215
216void dataManager::saveConfiguration(string folder, string nameOfFile)
217{
218    ofstream outfile;
219    string name = pspa_->getWorkingDir()+ folder + "/" + nameOfFile + ".save";
220
221    // make dir if not exist
222    boost::filesystem::create_directories(pspa_->getWorkingDir() + folder + "/");
223   
224    outfile.open(name.c_str(), ios::out);
225    if (!outfile) {
226        cerr << " error opening output file for persistency " << name << endl;
227    }
228 
229  outfile << globParam_.FileOutputFlow();
230  abstractElement* elPtr;
231  for(unsigned k = 0; k < getBeamLineSize() ; k++)
232    {
233      elPtr = pspa_->getBeamLine()->getAbstractElement(k);
234      outfile << elPtr->FileOutputFlow();
235    }
236  outfile.close();
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    //     cout << "reupere " << ptr->getLabel() << endl;
287    // }
288    return true;
289}
290
291particleBeam* dataManager::getDiagnosticBeam(unsigned index)
292{
293  if (index >= diagnosticBeam_.size()) {
294    return NULL;
295  } else {
296    return &diagnosticBeam_.at(index);
297  }
298}
299
300// void 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//   double longueur = 0.0;
307//   double valeur = 0.0;
308//   xcor.clear();
309//   ycor.clear();
310//   if ( type == "x" )
311//     {
312//       for (k= numeroDeb; k <= numeroFin; k++)
313//         {
314//        if ( !diagnosticBeam_.at(k-1).momentRepresentationOk() ) diagnosticBeam_.at(k-1).buildMomentRepresentation();
315//        longueur += pspa_->getBeamLine()->getAbstractElement(k-1)->getLenghtOfElement();
316//        valeur = diagnosticBeam_.at(k-1).getXmaxRms();
317//        cout << " dataManager::donneesRmsEnveloppe longueur = " << longueur << " enveleloppe : " << valeur << endl;
318//        xcor.push_back(longueur);
319//        ycor.push_back(valeur);
320//         }
321//     }
322//   else
323//     {
324//       cerr << " dataManager::donneesRmsEnveloppe type " << type << " not programmed " << endl;
325//       return;
326//     }
327// }
328
329// on ne tient pas compte des elements "snapshot" presents dans la beamLine
330void dataManager::donneesRmsEnveloppe(string type,vector<double>& xcor,vector<double>& ycor)
331{
332  double longueur = 0.0;
333  double valeur = 0.0;
334  xcor.clear();
335  ycor.clear();
336
337  if ( type == "x" ) {
338    unsigned i = 0;
339    cout << " dataManager::donneesRmsEnveloppe BeamLineSize = " << getBeamLineSize() << endl;
340    for (unsigned k = 0; k < getBeamLineSize(); k++) {
341      abstractElement* elPtr = pspa_->getBeamLine()->getAbstractElement(k);
342      if(elPtr->getNomdElement().getElementType() == snapshot) continue;
343      if(elPtr->getNomdElement().getElementType() == fit) continue;
344
345      if ( !diagnosticBeam_.at(i).momentRepresentationOk() ) {
346        diagnosticBeam_.at(i).buildMomentRepresentation();
347      }
348
349      longueur += elPtr->getLenghtOfElement();
350      valeur = diagnosticBeam_.at(i).getXmaxRms();
351      cout << " dataManager::donneesRmsEnveloppe index = " << k <<  " longueur = " << longueur << " enveleloppe : " << valeur << endl;
352      xcor.push_back(longueur);
353      ycor.push_back(valeur);
354      i++;
355    }
356  } else {
357    cerr << " dataManager::donneesRmsEnveloppe type " << type << " not programmed " << endl;
358    return;
359  }
360}
361
362int dataManager::getBeamLineSize() {
363  return pspa_->getBeamLine()->getBeamLineSize();
364}
365
366abstractSoftware* dataManager::createSoftwareConnexion(nomDeLogiciel logi) 
367{
368  string inputFileName;
369  if(logi == nomDeLogiciel::parmela) {
370    inputFileName = "parmin";
371    return new softwareParmela(inputFileName, &globParam_, this);
372  } else if (logi == nomDeLogiciel::transport) {
373    inputFileName = "transport.input";
374    return new softwareTransport(inputFileName, &globParam_, this);
375  } else if (logi == nomDeLogiciel::generator) {
376    inputFileName = "generator.in";
377    return new softwareGenerator(inputFileName, &globParam_, this);
378  } else if (logi == nomDeLogiciel::test) {
379    return new softwareTest(inputFileName, &globParam_, this);
380  } else {
381    return NULL;
382  } 
383}
Note: See TracBrowser for help on using the repository browser.