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

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

suppression methodes redondantes (suite)

File size: 9.3 KB
Line 
1#include "dataManager.h"
2#include "environmentVariables.h"
3
4#include <stdio.h>
5#include <fstream>
6
7//using namespace std;
8
9abstractElement* dataManager::addElement(nomdElement elemType)
10{
11  return elementsGallery_.addElement(elemType);
12}
13
14
15
16
17  // void dataManager::addSectionToExecute(int debut, int fin, nomDeLogiciel prog)
18  // {
19  //   jobList_.push_back(new sectionToExecute);
20  //   jobList_.back()->firstElement = debut;
21  //   jobList_.back()->lastElement = fin;
22  //   jobList_.back()->software  = prog;
23  // }
24
25// void dataManager::clearSectionToExecute()
26// {
27//   unsigned k;
28//   for (k=0; k < jobList_.size();k++)
29//     {
30//       if ( jobList_[k] != NULL ) delete jobList_[k];
31//     }
32//   jobList_.clear();
33// }
34
35
36string dataManager::executeTransport( unsigned int indexDeb, unsigned int indexFin)
37{
38    ostringstream sortie;
39
40  sortie << " EXECUTION DE TRANSPORT DE " << indexDeb << " a " << indexFin << endl;
41  //  sortie << " (programmation en cours) " << endl;
42  createInputFileTransport( indexDeb, indexFin);
43
44  string buf;
45  string transportJob = WORKINGAREA + "/transport";
46  transportJob += string(" <  ");
47  transportJob += WORKINGAREA + "/transport.input";
48  transportJob += string(" >  ");
49  transportJob += WORKINGAREA + "/transport.output";
50  cout << " job transport= " << transportJob << endl;
51
52  FILE* pp = popen(transportJob.c_str(), "r");
53  if (pp == NULL)
54    {
55      cerr << " execution transport foire " << endl;
56      return string("");
57    }
58  else
59    {
60      cout << " execution transport MARCHE " << endl;
61
62     ifstream infile;
63     string name = WORKINGAREA + "/transport.output";
64      infile.open(name.c_str(), ios::in);
65      if (!infile) 
66        {
67          cerr << " error opening transport output stream " << name << endl;
68        }
69
70
71
72      unsigned nbl = 0;
73      while (getline(infile, buf) )
74        {
75          sortie << buf << endl;
76          //      cout << " buf: " << buf << endl;
77          nbl++;
78        }
79      infile.close();
80      infile.clear();
81      infile.open(name.c_str(), ios::in);
82      if (!infile) 
83        {
84          cerr << " error re-opening transport output stream " << name << endl;
85        }
86      else
87        {
88          cout << " successfull re-opening transport output stream " << name << endl;
89        }
90
91
92      //         cout << " sortie transport " << sortie.str() << endl;
93
94      // nbl = 0;
95      // //      while (getline(infile, buf) )
96      // for ( int ibid= 0; ibid < 80; ibid++)
97      //        {
98      //          getline(infile, buf);
99      //          cout << " buf deux fois : " << buf << endl;
100      //          nbl++;
101      //        }
102
103
104      currentBeam_.setFromTransport(infile, nbl);
105
106      infile.close();
107      pclose(pp);
108      return sortie.str();
109    }
110      return sortie.str();
111}
112
113
114string dataManager::executeParmela( unsigned int indexDeb, unsigned int indexFin)
115{
116
117  createInputFileParmela(indexDeb, indexFin);
118  ostringstream sortie;
119  sortie << " EXECUTION DE PARMELA DE l'ELEMENT " << indexDeb << " A L'ELEMENT " << indexFin << endl;
120
121  char buf[132];
122  string parmelaJob = WORKINGAREA + "/parmela";
123  parmelaJob += string("   ");
124  parmelaJob += WORKINGAREA;
125  cout << " job parmela= " << parmelaJob << endl;
126  FILE* pp = popen(parmelaJob.c_str(), "r");
127  if (pp == NULL)
128    {
129      cerr << " execution parmela foire " << endl;
130      return string("");
131    }
132  else
133    {
134      cout << " execution parmela MARCHE " << endl;
135      while (fgets(buf, sizeof buf, pp))
136        {
137          sortie << buf;
138        }
139
140      //      cout << " sortie parmela " << sortie.str() << endl;
141
142      currentBeam_.setFromParmela(globParam_.getFrequency());
143      pclose(pp);
144      return sortie.str();
145    }
146 
147}
148
149
150void dataManager::createInputFileTransport( unsigned int indexDeb, unsigned int indexFin)
151{
152  unsigned int k;
153  if ( !currentBeam_.momentRepresentationOk() )
154    {
155
156      // if ( currentBeam_.particleRepresentationOk() )
157      //        {
158      //          currentBeam_.buildMomentRepresentation();
159      //        }
160      // else
161      //        {
162          cerr << " dataManager::createInputFileTransport : the beam is not OK " << endl;
163          return;
164          //    }
165    }
166  indexDeb--;
167  indexFin--;
168
169  const vector< vector<double> >& moments = currentBeam_.getTransportMoments();
170  double P0 = currentBeam_.getP0Transport();
171  ofstream outfile;
172  string name = WORKINGAREA + "/transport.input";
173  outfile.open(name.c_str(), ios::out);
174  if (!outfile) 
175    {
176      cerr << " error opening output stream " << name << endl;
177    }
178
179  outfile << string("'titre provisoire'") << endl;
180  outfile << " 0 " << endl;
181  outfile << " UTRANS " << endl;
182
183  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;
184
185  outfile << " DL=" << (moments.at(4)).at(4) << ", DEL="<< (moments.at(5)).at(5) << ", P0=" << P0 << " ; " << endl;
186
187  outfile << " CORR, C21=" << (moments.at(1)).at(0) << ", C31="<< (moments.at(2)).at(0) << ", C32=" << (moments.at(2)).at(1) << ", & " << endl;
188
189  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;
190
191  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;
192
193  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;
194  outfile << " PRINT, BEAM, ON; " << endl;
195
196
197  abstractElement* elPtr;
198  for ( k=indexDeb; k <= indexFin; k++)
199    {
200      elPtr = elementsGallery_.getElementPointer(k);
201      outfile << elPtr->transportOutputFlow() << endl;
202    }
203
204  outfile << " SENTINEL " << endl;
205  outfile << " SENTINEL " << endl;
206
207  outfile.close();
208
209}
210
211void dataManager::createInputFileParmela( unsigned int indexDeb, unsigned int indexFin)
212{
213  indexDeb--;
214  indexFin--;
215  ofstream outfile;
216  string name = WORKINGAREA + "/parmin";
217  outfile.open(name.c_str(), ios::out);
218      if (!outfile) 
219        {
220          cerr << " error opening output stream " << name << endl;
221        }
222  unsigned int k;
223  abstractElement* elPtr;
224
225  elPtr = elementsGallery_.getElementPointer(indexDeb);
226  if ( elPtr->getName() != initialBeam ) 
227    {
228      cerr << " dataManager::createInputFileParmela : the first element should be initialBeam" << endl;
229    }
230 
231
232  double initalKineticEnergy = 0.0;
233  for ( k=indexDeb; k <= indexFin; k++)
234    {
235      if ( elPtr->getName() == initialBeam ) 
236        {
237          elPtr->setPhaseStep( globParam_.getIntegrationStep() );
238          elPtr = elementsGallery_.getElementPointer(k);
239          initalKineticEnergy = elPtr->getInitialKineticEnergy();
240          break;
241        }
242    }
243
244  outfile << "TITLE" << endl;
245  outfile << " titre provisoire " << endl;
246  outfile << "RUN /n0=1 /ip=999 /freq=" << globParam_.getFrequency() << "  /z0=0.0 /W0=" << initalKineticEnergy << "  /itype=1" << endl;
247  outfile << "OUTPUT 0" << endl;
248   
249  for ( k=indexDeb; k <= indexFin; k++)
250    {
251      elPtr = elementsGallery_.getElementPointer(k);
252      outfile << elPtr->parmelaOutputFlow() << endl;
253    }
254  outfile << "ZOUT" << endl;
255  outfile << "START /wt=0.0 /dwt=" << globParam_.getIntegrationStep() << "  /nsteps=" << globParam_.getNbSteps() << "  nsc=" << globParam_.getScPeriod() << "  /nout=10" << endl;
256  outfile << "END" << endl;
257  outfile.close();
258}
259
260  void dataManager::initializeExecution()
261  {
262    currentBeam_.clear();
263  }
264
265  // string dataManager::executeAll()
266  // {
267  //   //    return executeParmela( 0, elementsGallery_.size()-1);
268  //   unsigned k;
269  //   int debut, fin;
270  //   currentBeam_.clear();
271  //   string resul("");
272  //   for ( k=0; k< jobList_.size(); k++)
273  //     {
274  //    debut = jobList_[k]->firstElement;
275  //    fin = jobList_[k]->lastElement;
276  //    if ( jobList_[k]->software == nomDeLogiciel::parmela ) resul += executeParmela( debut, fin);
277  //    else if ( jobList_[k]->software == nomDeLogiciel::transport ) resul += executeTransport( debut, fin);
278  //    else
279  //      {
280  //        resul +=  " unknown software -- STOP \n "; 
281  //        break;
282  //      }
283  //     }
284  //   //    cout << " resultat : " << resul << endl;
285  //   return resul;
286  // }
287
288
289
290  void dataManager::saveAll()
291  {
292     ofstream outfile;
293     string name = WORKINGAREA + "/pspa.save";
294  outfile.open(name.c_str(), ios::out);
295      if (!outfile) 
296        {
297          cerr << " error opening output file for persistency " << name << endl;
298        }
299  outfile << globParam_.FileOutputFlow();
300  int k;
301  abstractElement* elPtr;
302  for ( k=0 ; k < elementsGallery_.size() ; k++)
303    {
304      elPtr = elementsGallery_.getElementPointer(k);
305      outfile << elPtr->FileOutputFlow();
306    }
307    outfile.close();   
308  }
309
310  void dataManager::restoreAll()
311  {
312     ifstream infile;
313     string name = WORKINGAREA + "/pspa.save";
314      infile.open(name.c_str(), ios::in);
315      if (!infile) 
316        {
317          cerr << " error opening input stream " << name << endl;
318        }
319      int ielem;
320
321      string globalTitle;
322      if ( infile >> globalTitle )
323        {
324          if ( globalTitle == string("globals") ) 
325            {
326              globParam_.raz();
327              globParam_.FileInput(infile);
328            }
329          else
330            {
331              cerr << " dataManager::restoreAll ERROR : global parameters seems to be missing" << endl;
332            }
333        }
334      else
335        {
336          cerr << " dataManager::restoreAll ERROR : reading data save file" << endl;
337        }
338      elementsGallery_.raz();
339      nomdElement elem;
340       while (infile >> ielem)
341        {
342          elem = (nomdElement)ielem;
343          abstractElement* nouveau = addElement(elem);
344          nouveau->FileInput(infile);
345        }
346
347
348      infile.close();
349  }
Note: See TracBrowser for help on using the repository browser.