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

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

creation d'un diagnostic de coherence

File size: 9.4 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
119  ostringstream sortie;
120  sortie << " EXECUTION DE PARMELA DE l'ELEMENT " << indexDeb << " A L'ELEMENT " << indexFin << endl;
121
122  char buf[132];
123  string parmelaJob = WORKINGAREA + "/parmela";
124  parmelaJob += string("   ");
125  parmelaJob += WORKINGAREA;
126  cout << " job parmela= " << parmelaJob << endl;
127  FILE* pp = popen(parmelaJob.c_str(), "r");
128  if (pp == NULL)
129    {
130      cerr << " execution parmela foire " << endl;
131      return string("");
132    }
133  else
134    {
135      cout << " execution parmela MARCHE " << endl;
136      while (fgets(buf, sizeof buf, pp))
137        {
138          sortie << buf;
139        }
140
141      //      cout << " sortie parmela " << sortie.str() << endl;
142
143      currentBeam_.setFromParmela(globParam_.getFrequency());
144      pclose(pp);
145      return sortie.str();
146    }
147 
148}
149
150
151void dataManager::createInputFileTransport( unsigned int indexDeb, unsigned int indexFin)
152{
153  unsigned int k;
154  if ( !currentBeam_.momentRepresentationOk() )
155    {
156
157          cerr << " dataManager::createInputFileTransport : the beam is not OK " << endl;
158    }
159  indexDeb--;
160  indexFin--;
161
162  const vector< vector<double> >& moments = currentBeam_.getTransportMoments();
163  double P0 = currentBeam_.getP0Transport();
164  ofstream outfile;
165  string name = WORKINGAREA + "/transport.input";
166  outfile.open(name.c_str(), ios::out);
167  if (!outfile) 
168    {
169      cerr << " error opening output stream " << name << endl;
170    }
171
172  outfile << string("'titre provisoire'") << endl;
173  outfile << " 0 " << endl;
174  outfile << " UTRANS " << endl;
175
176  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;
177
178  outfile << " DL=" << (moments.at(4)).at(4) << ", DEL="<< (moments.at(5)).at(5) << ", P0=" << P0 << " ; " << endl;
179
180  outfile << " CORR, C21=" << (moments.at(1)).at(0) << ", C31="<< (moments.at(2)).at(0) << ", C32=" << (moments.at(2)).at(1) << ", & " << endl;
181
182  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;
183
184  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;
185
186  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;
187  outfile << " PRINT, BEAM, ON; " << endl;
188
189
190  abstractElement* elPtr;
191  for ( k=indexDeb; k <= indexFin; k++)
192    {
193      elPtr = elementsGallery_.getElementPointer(k);
194      outfile << elPtr->transportOutputFlow() << endl;
195    }
196
197  outfile << " SENTINEL " << endl;
198  outfile << " SENTINEL " << endl;
199
200  outfile.close();
201
202}
203
204void dataManager::createInputFileParmela( unsigned int indexDeb, unsigned int indexFin)
205{
206  indexDeb--;
207  indexFin--;
208
209 
210
211
212  ofstream outfile;
213  string name = WORKINGAREA + "/parmin";
214  outfile.open(name.c_str(), ios::out);
215      if (!outfile) 
216        {
217          cerr << " error opening output stream " << name << endl;
218        }
219  unsigned int k;
220  abstractElement* elPtr;
221
222  elPtr = elementsGallery_.getElementPointer(indexDeb);
223  if ( elPtr->getName() != initialBeam ) 
224    {
225      cerr << " dataManager::createInputFileParmela : the first element should be initialBeam" << endl;
226    }
227 
228
229  double initalKineticEnergy = 0.0;
230  for ( k=indexDeb; k <= indexFin; k++)
231    {
232      if ( elPtr->getName() == initialBeam ) 
233        {
234          elPtr->setPhaseStep( globParam_.getIntegrationStep() );
235          elPtr = elementsGallery_.getElementPointer(k);
236          initalKineticEnergy = elPtr->getInitialKineticEnergy();
237          break;
238        }
239    }
240
241  outfile << "TITLE" << endl;
242  outfile << " titre provisoire " << endl;
243  outfile << "RUN /n0=1 /ip=999 /freq=" << globParam_.getFrequency() << "  /z0=0.0 /W0=" << initalKineticEnergy << "  /itype=1" << endl;
244  outfile << "OUTPUT 0" << endl;
245   
246  for ( k=indexDeb; k <= indexFin; k++)
247    {
248      elPtr = elementsGallery_.getElementPointer(k);
249      outfile << elPtr->parmelaOutputFlow() << endl;
250    }
251  outfile << "ZOUT" << endl;
252  outfile << "START /wt=0.0 /dwt=" << globParam_.getIntegrationStep() << "  /nsteps=" << globParam_.getNbSteps() << "  nsc=" << globParam_.getScPeriod() << "  /nout=10" << endl;
253  outfile << "END" << endl;
254  outfile.close();
255}
256
257void dataManager::initializeExecution()
258{
259  ifstream testParmdesz;
260  string parmdesz = WORKINGAREA + "/parmdesz";
261  testParmdesz.open(parmdesz.c_str(), ios::in);
262  if (testParmdesz)
263    {
264      testParmdesz.close();
265      remove(parmdesz.c_str());
266    }
267  currentBeam_.clear();
268}
269
270  // string dataManager::executeAll()
271  // {
272  //   //    return executeParmela( 0, elementsGallery_.size()-1);
273  //   unsigned k;
274  //   int debut, fin;
275  //   currentBeam_.clear();
276  //   string resul("");
277  //   for ( k=0; k< jobList_.size(); k++)
278  //     {
279  //    debut = jobList_[k]->firstElement;
280  //    fin = jobList_[k]->lastElement;
281  //    if ( jobList_[k]->software == nomDeLogiciel::parmela ) resul += executeParmela( debut, fin);
282  //    else if ( jobList_[k]->software == nomDeLogiciel::transport ) resul += executeTransport( debut, fin);
283  //    else
284  //      {
285  //        resul +=  " unknown software -- STOP \n "; 
286  //        break;
287  //      }
288  //     }
289  //   //    cout << " resultat : " << resul << endl;
290  //   return resul;
291  // }
292
293
294
295  void dataManager::saveAll()
296  {
297     ofstream outfile;
298     string name = WORKINGAREA + "/pspa.save";
299  outfile.open(name.c_str(), ios::out);
300      if (!outfile) 
301        {
302          cerr << " error opening output file for persistency " << name << endl;
303        }
304  outfile << globParam_.FileOutputFlow();
305  int k;
306  abstractElement* elPtr;
307  for ( k=0 ; k < elementsGallery_.size() ; k++)
308    {
309      elPtr = elementsGallery_.getElementPointer(k);
310      outfile << elPtr->FileOutputFlow();
311    }
312    outfile.close();   
313  }
314
315  void dataManager::restoreAll()
316  {
317     ifstream infile;
318     string name = WORKINGAREA + "/pspa.save";
319      infile.open(name.c_str(), ios::in);
320      if (!infile) 
321        {
322          cerr << " error opening input stream " << name << endl;
323        }
324      int ielem;
325
326      string globalTitle;
327      if ( infile >> globalTitle )
328        {
329          if ( globalTitle == string("globals") ) 
330            {
331              globParam_.raz();
332              globParam_.FileInput(infile);
333            }
334          else
335            {
336              cerr << " dataManager::restoreAll ERROR : global parameters seems to be missing" << endl;
337            }
338        }
339      else
340        {
341          cerr << " dataManager::restoreAll ERROR : reading data save file" << endl;
342        }
343      elementsGallery_.raz();
344      nomdElement elem;
345       while (infile >> ielem)
346        {
347          elem = (nomdElement)ielem;
348          abstractElement* nouveau = addElement(elem);
349          nouveau->FileInput(infile);
350        }
351
352
353      infile.close();
354  }
Note: See TracBrowser for help on using the repository browser.