source: PSPA/Interface_Web/trunk/pspaWT/src/dataManager.cc @ 149

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

trace d'enveloppe

File size: 13.9 KB
Line 
1#include "dataManager.h"
2#include "environmentVariables.h"
3
4#include <stdio.h>
5#include <fstream>
6
7abstractElement* dataManager::addElement(typedElement elemType)
8{
9  return elementsGallery_.addElement(elemType);
10}
11
12string dataManager::getLabelFromElementNumero(int numero)
13{
14  abstractElement* ptr = elementsGallery_.getElementPointerFromNumero(numero);
15  if ( ptr == NULL ) return "";
16  return ptr->getLabel();
17}
18
19
20void dataManager::addSectionToExecute(int debut, int fin, nomDeLogiciel prog)
21{
22  jobList_.push_back(new sectionToExecute);
23  jobList_.back()->firstElement = debut;
24  jobList_.back()->lastElement = fin;
25  jobList_.back()->software  = prog;
26}
27
28void dataManager::clearSectionToExecute()
29{
30  unsigned k;
31  for(k = 0; k < jobList_.size(); k++)
32    {
33      if ( jobList_[k] != NULL ) delete jobList_[k];
34    }
35  jobList_.clear();
36}
37
38string dataManager::checkExecute()
39{
40  cout << "dataManager::checkExecute()" << endl;
41 
42  int k,j;
43  string diagnostic;
44  int indexDeb, indexFin;
45  cout << "controle execution : " << endl;
46  for(k = 0; k < jobList_.size(); k++) {
47    indexDeb = jobList_[k]->firstElement;
48    indexFin = jobList_[k]->lastElement;
49    if ( indexFin  <= indexDeb )
50      {
51        diagnostic += " first ans last elements are the same \n";
52        break;
53      }
54    cout << " indexDeb= " << indexDeb << " indexFin= " << indexFin << endl;
55    indexDeb--;
56    indexFin--;
57    abstractElement* elPtr;
58    if(jobList_[k]->software == nomDeLogiciel::parmela ) 
59      {
60        for(j = indexDeb; j <= indexFin; j++) {
61          elPtr = elementsGallery_.getElementPointer(j);
62
63          cout << "elPtr parmela: " << elPtr->getLabel() << endl;
64
65          if(!elPtr->is_parmela_element()) {
66            diagnostic += " the element " + elPtr->getNameString() + " is not allowed for PARMELA \n";
67          }
68        }           
69      }
70    else if (jobList_[k]->software == nomDeLogiciel::transport) 
71      {
72        for(j = indexDeb; j <= indexFin; j++) {
73          elPtr = elementsGallery_.getElementPointer(j);
74
75          cout << "elPtr transp: " << elPtr->getLabel() << endl;
76
77          if (!elPtr->is_transport_element()) {
78            diagnostic += " the element " + elPtr->getNameString() + " is not allowed for TRANSPORT \n";
79          }
80        }           
81      }
82    else 
83      {
84        diagnostic += (jobList_[k]->software).getString() + " : unknown software \n "; 
85      }
86  }
87 
88  return diagnostic;
89}
90
91bool dataManager::executeTransport( unsigned int indexDeb, unsigned int indexFin, string& resul)
92{
93  ostringstream sortie;
94  bool success = true;
95  resul.clear();
96  sortie << " EXECUTION DE TRANSPORT DE " << indexDeb << " a " << indexFin << endl;
97  if ( !createInputFileTransport( indexDeb, indexFin) )
98    {
99          sortie << " error creating transport input file "  << endl;
100          resul = sortie.str();
101          success = false;
102          return success;
103    }
104
105  string bufString;
106  char buf[132];
107  string transportJob = WORKINGAREA + "transport";
108  transportJob += string(" <  ");
109  transportJob += WORKINGAREA + "transport.input";
110  //  transportJob += string(" >  ");
111  //  transportJob += WORKINGAREA + "transport.output";
112  cout << " job transport= " << transportJob << endl;
113
114      ofstream outfile;
115      string nameOut = WORKINGAREA + "transport.output";
116
117
118  FILE* pp = popen(transportJob.c_str(), "r");
119  if (pp != NULL)
120    {
121      cout << " execution transport MARCHE " << endl;
122
123      ////////////////////////
124      outfile.open(nameOut.c_str(), ios::out);
125      if (!outfile) 
126        {
127          sortie << " error first opening transport output stream " << nameOut << endl;
128          resul = sortie.str();
129          success = false;
130          return success;
131        }
132
133      unsigned nbl = 0;
134      while (fgets(buf, sizeof buf, pp))
135        {
136          //      cout << " buf= " << buf << endl;
137          sortie << buf;
138          outfile << buf;
139          nbl++;
140        }
141      outfile.close();
142      /////////////////////////////////
143
144      //      cout << " datamanager ecriture output terminee nbl = " << nbl << endl;
145      // on re-ouvre le fichier de sortie
146
147      ifstream infile;
148
149      infile.open(nameOut.c_str(), ios::in);
150     
151      //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
152
153      int k;
154      for ( k= indexDeb; k <= indexFin; k++)
155        {
156          diagnosticBeam_.push_back(particleBeam());
157
158          string elemLabel = getLabelFromElementNumero (k);
159          abstractElement* ptr = elementsGallery_.getElementPointerFromNumero(k);
160          bool camarche = diagnosticBeam_.back().setFromTransport(elemLabel, ptr->getName() );
161          if (!camarche  )
162            {
163              sortie << " reading transport output  failed " << endl;
164              success = false;
165              break;
166            }
167        }
168      currentBeam_ = &diagnosticBeam_.back();
169      //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
170
171      pclose(pp);
172      resul =  sortie.str();
173    }
174  else
175    {
176      sortie << " launching of transport failed \n";
177      resul =  sortie.str();
178      success =  false;
179    }
180  return success;
181}
182
183bool  dataManager::executeParmela(unsigned int indexDeb,unsigned int indexFin,string& resul)
184{
185  bool success = true;
186  resul.clear();
187  createInputFileParmela(indexDeb,indexFin);
188
189  ostringstream sortie;
190  sortie << " EXECUTION DE PARMELA DE l'ELEMENT " << indexDeb << " A L'ELEMENT " << indexFin << endl;
191
192  char buf[132];
193  string parmelaJob = WORKINGAREA + "parmela";
194  parmelaJob += string("   ");
195  parmelaJob += WORKINGAREA;
196  cout << " job parmela= " << parmelaJob << endl;
197  FILE* pp = popen(parmelaJob.c_str(), "r");
198  if (pp != NULL) {
199    cout << " execution parmela MARCHE " << endl;
200    while (fgets(buf, sizeof buf, pp))
201      {
202        sortie << buf;
203      }
204
205    //      cout << " sortie parmela " << sortie.str() << endl;
206    string::size_type nn = (sortie.str()).find("normal");
207    if ( nn == string::npos ) 
208      {
209        sortie << " abnormal exit of parmela " << endl;
210        resul =  sortie.str();
211        success = false;
212        return success;
213      }
214
215      int k;
216      for ( k= indexDeb; k <= indexFin; k++)
217        {
218          diagnosticBeam_.push_back(particleBeam());
219          if (! diagnosticBeam_.back().setFromParmela(k, globParam_.getFrequency()) )
220            {
221              sortie << " reading parmdesz  failed " << endl;
222              success = false;
223              break;
224            }
225        }
226      currentBeam_ = &diagnosticBeam_.back();
227
228      // if ( !currentBeam_->setFromParmela(indexFin, globParam_.getFrequency()) )
229      //        {
230      //          sortie << " reading parmdesz  failed " << endl;
231      //          success = false;
232      //        }
233      pclose(pp);
234      resul =  sortie.str();
235    }
236  else
237    {
238      sortie << " launching of parmela failed " << endl;
239      resul =  sortie.str();
240      success = false;
241    }
242 
243  return success;
244}
245
246bool dataManager::createInputFileTransport( unsigned int indexDeb, unsigned int indexFin)
247{
248  unsigned int k;
249  if ( !currentBeam_->momentRepresentationOk() )
250    {
251
252      cout << " dataManager::createInputFileTransport : creating moment representation " << endl;
253      if ( currentBeam_->particleRepresentationOk() )
254        {
255          currentBeam_->buildMomentRepresentation();
256        }
257      else 
258        {
259          cout << " ca cafouille dans les representations de faisceau " << endl;
260          return false;
261        }
262    }
263
264  indexDeb--;
265  indexFin--;
266
267  const vector< vector<double> >& moments = currentBeam_->getTransportMoments();
268  double P0 = currentBeam_->getP0Transport();
269  ofstream outfile;
270  string name = WORKINGAREA + "transport.input";
271  outfile.open(name.c_str(), ios::out);
272  if (!outfile) 
273    {
274      cout << " error opening output stream " << name << endl;
275      return false;
276    }
277
278  outfile << string("'titre provisoire'") << endl;
279  outfile << " 0 " << endl;
280  outfile << " UTRANS " << endl;
281
282  outfile << " BEAM, X=" << (moments.at(0)).at(0) << ", XP="<< (moments.at(1)).at(1) << ", Y=" << (moments.at(2)).at(2)<< ", YP=" << (moments.at(3)).at(3) << ", & " << endl;
283
284  outfile << " DL=" << (moments.at(4)).at(4) << ", DEL="<< (moments.at(5)).at(5) << ", P0=" << P0 << " ; " << endl;
285
286  outfile << " CORR, C21=" << (moments.at(1)).at(0) << ", C31="<< (moments.at(2)).at(0) << ", C32=" << (moments.at(2)).at(1) << ", & " << endl;
287
288  outfile << " C41=" << (moments.at(3)).at(0) << ", C42="<< (moments.at(3)).at(1) << ", C43=" << (moments.at(3)).at(2) << ", C51=" << (moments.at(4)).at(0) << ", & " << endl;
289
290  outfile << " C52=" << (moments.at(4)).at(1) << ", C53="<< (moments.at(4)).at(2) << ", C54=" << (moments.at(4)).at(3) << ", C61=" << (moments.at(5)).at(0) << ", & " << endl;
291
292  outfile << " C62=" << (moments.at(5)).at(1) << ", C63="<< (moments.at(5)).at(2) << ", C64=" << (moments.at(5)).at(3) << ", C65=" << (moments.at(5)).at(4) << " ; " << endl;
293  outfile << " PRINT, BEAM, ON; " << endl;
294
295
296  abstractElement* elPtr;
297  for ( k=indexDeb; k <= indexFin; k++)
298    {
299      elPtr = elementsGallery_.getElementPointer(k);
300      outfile << elPtr->transportOutputFlow() << endl;
301    }
302
303  outfile << " SENTINEL " << endl;
304  outfile << " SENTINEL " << endl;
305
306  outfile.close();
307  return true;
308}
309
310void dataManager::createInputFileParmela( unsigned int indexDeb, unsigned int indexFin)
311{
312  indexDeb--;
313  indexFin--;
314
315  ofstream outfile;
316  string name = WORKINGAREA + "parmin";
317  outfile.open(name.c_str(), ios::out);
318  if (!outfile) {
319      cerr << " error opening output stream " << name << endl;
320  }
321
322  abstractElement* elPtr;
323  elPtr = elementsGallery_.getElementPointer(indexDeb);
324  if ( elPtr->getName() != initialBeam ) {
325      cerr << " dataManager::createInputFileParmela : the first element should be initialBeam" << endl;
326    }
327 
328  unsigned int k;
329  double initalKineticEnergy = 0.0;
330  for ( k = indexDeb; k <= indexFin; k++)
331    {
332      if ( elPtr->getName() == initialBeam ) 
333        {
334          elPtr->setPhaseStep( globParam_.getIntegrationStep() );
335          elPtr = elementsGallery_.getElementPointer(k);
336          initalKineticEnergy = elPtr->getInitialKineticEnergy();
337          break;
338        }
339    }
340
341  outfile << "TITLE" << endl;
342  outfile << " titre provisoire " << endl;
343  outfile << "RUN /n0=1 /ip=999 /freq=" << globParam_.getFrequency() << "  /z0=0.0 /W0=" << initalKineticEnergy << "  /itype=1" << endl;
344  outfile << "OUTPUT 0" << endl;
345   
346  for ( k = indexDeb; k <= indexFin; k++)
347    {
348      elPtr = elementsGallery_.getElementPointer(k);
349      outfile << elPtr->parmelaOutputFlow() << endl;
350    }
351
352  outfile << "ZOUT" << endl;
353  outfile << "START /wt=0.0 /dwt=" << globParam_.getIntegrationStep() << "  /nsteps=" << globParam_.getNbSteps() << "  nsc=" << globParam_.getScPeriod() << "  /nout=10" << endl;
354  outfile << "END" << endl;
355  outfile.close();
356}
357
358void dataManager::initializeExecution()
359{
360  removeFile("parmdesz");
361  removeFile("parmin");
362  removeFile("transport.input");
363  removeFile("transport.output");
364  //  currentBeam_.clear();
365  currentBeam_ = NULL;
366  clearSectionToExecute();
367}
368
369void dataManager::removeFile(string nameOfFile) 
370{
371  ifstream fileExists;
372  string completeName = WORKINGAREA + nameOfFile;
373  fileExists.open(completeName.c_str(), ios::in);
374  if (fileExists) {
375    fileExists.close();
376    remove(completeName.c_str());
377  }
378}
379
380bool dataManager::executeAll(string &resul)
381{
382  bool success = true;
383  resul.clear();
384  string resultatPartiel;
385  unsigned k;
386  for(k = 0; k < jobList_.size(); k++)
387    {
388      int debut = jobList_[k]->firstElement;
389      int fin = jobList_[k]->lastElement;
390      resultatPartiel.clear();
391      cout << " dataManager::executeAll je m'apprete a executer : " << (jobList_[k]->software).getString() << endl;
392
393      if(jobList_[k]->software == nomDeLogiciel::parmela) {
394        success = executeParmela( debut, fin, resultatPartiel);
395      } else if (jobList_[k]->software == nomDeLogiciel::transport) {
396        success = executeTransport( debut, fin, resultatPartiel);
397      } else {
398        success = false;
399        resultatPartiel =  " unknown software -- STOP \n ";     
400        break;
401      }
402      resul += resultatPartiel;
403      if ( !success ) break;
404    }
405  return success;
406}
407
408void dataManager::saveAll()
409{
410  ofstream outfile;
411  string name = WORKINGAREA + "pspa.save";
412  outfile.open(name.c_str(), ios::out);
413  if (!outfile) {
414    cerr << " error opening output file for persistency " << name << endl;
415  }
416
417  outfile << globParam_.FileOutputFlow();
418  int k;
419  abstractElement* elPtr;
420  for ( k=0 ; k < elementsGallery_.size() ; k++)
421    {
422      elPtr = elementsGallery_.getElementPointer(k);
423      outfile << elPtr->FileOutputFlow();
424    }
425  outfile.close();   
426}
427
428void dataManager::restoreElements()
429{
430  cout << "dataManager::restoreElements()" << endl;
431     
432  ifstream infile;
433  string name = WORKINGAREA + "pspa.save";
434  infile.open(name.c_str(), ios::in);
435  if (!infile) {
436    cerr << " error opening input stream " << name << endl;
437  }
438 
439  int ielem;
440  string globalTitle;
441  if ( infile >> globalTitle ) {
442    if ( globalTitle == string("globals") ) {
443      globParam_.raz();
444      globParam_.FileInput(infile);
445    } else {
446      cerr << " dataManager::restoreAll ERROR : global parameters seems to be missing" << endl;
447    }
448  } else {
449    cerr << " dataManager::restoreAll ERROR : reading data save file" << endl;
450  }
451
452  elementsGallery_.raz();
453  typedElement elem;
454  while (infile >> ielem) {
455    elem = (typedElement)ielem;
456    abstractElement* nouveau = addElement(elem);
457    nouveau->FileInput(infile);
458  } 
459  infile.close();
460
461  // debug
462  int k;
463  for(k = 0; k <  elementsGallery_.size(); k++) {
464    abstractElement* ptr= elementsGallery_.getElementPointer(k);
465    cout << "reupere " << ptr->getLabel() << endl;
466  }
467}
468
469  particleBeam* dataManager::getDiagnosticBeam(int index) 
470  {
471    cout << " particleBeam* dataManager je vais dessiner " << getLabelFromElementNumero(index+1) << endl;
472    return &diagnosticBeam_.at(index);
473  }
474
475void dataManager::donneesRmsEnveloppe( string type, unsigned int numeroDeb, unsigned int numeroFin, vector<double>& xcor, vector<double>& ycor)
476{
477  unsigned k;
478  unsigned indexDeb = numeroDeb -1;
479  unsigned indexFin = numeroFin -1;
480  double longueur = 0.0;
481  double valeur = 0.0;
482  xcor.clear();
483  ycor.clear();
484  if ( type == "x" )
485    {
486      for (k= indexDeb; k <= indexFin; k++)
487        {
488          if ( !diagnosticBeam_.at(k).momentRepresentationOk() ) diagnosticBeam_.at(k).buildMomentRepresentation();
489          longueur += elementsGallery_.getElementPointer(k)->getLenghtOfElement();
490          valeur = diagnosticBeam_.at(k).getXmaxRms();
491          xcor.push_back(longueur);
492          ycor.push_back(valeur);
493        }
494    }
495  else
496    {
497      cerr << " dataManager::donneesRmsEnveloppe type " << type << " not programmed " << endl;
498      return;
499    }
500}
Note: See TracBrowser for help on using the repository browser.