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

Last change on this file since 342 was 342, checked in by touze, 11 years ago

nvx element snapshot

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