source: PSPA/Interface_Web/trunk/pspaWT/sources/controler/src/softwareTransport.cc @ 493

Last change on this file since 493 was 493, checked in by lemeur, 10 years ago

refection generale des secteurs et applications de softwares (suite)

File size: 14.1 KB
Line 
1#include "softwareTransport.h"
2#include "abstractElement.h"
3#include "beam2Moments.h"
4#include "dataManager.h"
5
6#include <algorithm>
7
8softwareTransport::softwareTransport() : abstractSoftware()
9{
10  nameOfSoftware_ = new nomDeLogiciel("transport");
11}
12
13// softwareTransport::softwareTransport(string inputFileName,sectionToExecute* sect, dataManager* data) : abstractSoftware(inputFileName, sect,data)
14// {
15//   nameOfSoftware_ = new nomDeLogiciel("transport");
16// }
17
18softwareTransport::softwareTransport(string inputFileName,computingBlock* cmpb, dataManager* data) : abstractSoftware(inputFileName, cmpb,data)
19{
20  nameOfSoftware_ = new nomDeLogiciel("transport");
21}
22
23bool softwareTransport::createInputFile(particleBeam* beamBefore, string workingDir)
24{ 
25
26
27  abstractElement* premierEl = getComputingBlock()->getFirstElement();
28
29  // ceinture et bretelles
30  if ( premierEl == NULL ) {
31    cerr << " premier element inexistant ?? " << endl;
32    return false;
33  }
34
35  string name = workingDir + inputFileName_;
36  ofstream outfile;
37  outfile.open(name.c_str(), ios::out);
38  if (!outfile) {
39    dataManager_->consoleMessage(" softwareTransport::createInputFile : error opening output stream " );
40    cerr << " error opening output stream " << name << endl;
41    return false;
42  }
43
44  outfile << string("'titre provisoire'") << endl;
45  outfile << " 0 " << endl;
46  outfile << " UTRANS " << endl;
47
48
49  //  cout << "debug:: c'est quoi le premier element? " << premierEl->getNomdElement().getElementName() << endl;
50
51  // si le premier element a traiter est un 'beam', on ne fait rien pour l'instant
52  // il sera traite plus bas de maniere standard
53
54  // Dans le cas où le premier element n'est pas beam
55  if ( premierEl->getNomdElement().getElementType() != nomdElements::beam ) {
56    // s'il n'y a pas faisceau courant, erreur
57    if ( beamBefore == NULL ) {
58      dataManager_->consoleMessage(" softwareTransport::createInputFile : no input beam, input file not created");
59      cout << " pas de faisceau " << endl;
60      return false;
61    } else {
62      // s'il y a un faisceau courant
63      if ( !beamBefore->momentRepresentationOk() ) {
64        // s'il n'est pas au format transport (issu de parmela ?)
65        // on le met au format transport, s'il est deja au format 'particules' (par ex/ parmela)
66        if ( !beamBefore->particleRepresentationOk() ) {
67          dataManager_->consoleMessage(" softwareParmela::createInputFile : ca cafouille dans les representations de faisceau : ni particules, ni moments ? " );
68          cout << " ca cafouille dans les representations de faisceau : ni particules, ni moments ? " << endl;
69          return false;
70        } else {
71          beamBefore->buildMomentRepresentation();
72        }
73      }
74      // ici, en principe, on a un faisceau correct
75      // on ecrit sur le fichier input
76      const beam2Moments& moments = beamBefore->getTransportMoments();
77      double P0 = beamBefore->getP0Transport();
78      outfile << moments.writeToTransportInput(P0);
79    }
80  }
81
82  outfile << " PRINT, BEAM, ON; " << endl;
83
84  abstractElement* elPtr;
85  for (unsigned k = 0; k < getComputingBlock()->getNumberOfElements(); k++)
86    {
87      elPtr = getComputingBlock()->getElement(k);
88      cout << "debug:: element [" << k << "] " << elPtr->getNomdElement().getExpandedName() << endl;
89      //      outfile << elPtr->transportOutputFlow();
90      outfile << elementsData(elPtr->parametersToSoftware());
91    }
92
93  outfile << " SENTINEL " << endl;
94  outfile << " SENTINEL " << endl;
95
96  outfile.close();
97  dataManager_->consoleMessage("fichier input termine pour TRANSPORT");
98  return true;
99}
100
101bool softwareTransport::execute(string workingDir)
102{
103  bool ExecuteStatus = true;
104
105  ostringstream sortie;
106  sortie << " EXECUTION DE TRANSPORT " << endl;
107 
108  string transportJob = workingDir + "transport";
109  transportJob += string(" <  ");
110  transportJob += workingDir + inputFileName_;
111  cout << " job transport= " << transportJob << endl;
112 
113  string resultOfRun;
114  bool success = launchJob(transportJob,resultOfRun);
115  sortie << resultOfRun << endl;
116  if ( !success ) {
117    sortie << " launching of transport failed " << endl;
118    ExecuteStatus = false;
119  } else {
120    cout << " execution transport MARCHE " << endl;
121    string nameOut = workingDir + "transport.output";
122    ofstream outfile;
123    outfile.open(nameOut.c_str(), ios::out);
124    if (!outfile) {
125      sortie << " error first opening transport output stream " << nameOut << endl;
126      ExecuteStatus = false;
127    } else {
128      // on copie la sortie dans un fichier 'transport.out'
129      outfile << resultOfRun << endl;
130      outfile.close();
131    }
132  }
133
134  dataManager_->consoleMessage(sortie.str());
135  return ExecuteStatus;
136}
137
138bool softwareTransport::buildBeamAfterElements(string workingDir) 
139{
140
141  bool result = true;
142
143  for (unsigned k = 0; k < getComputingBlock()->getNumberOfElements(); k++)
144    {
145      abstractElement* ptr = getComputingBlock()->getElement(k);
146      if ( ptr == NULL ) {
147        dataManager_->consoleMessage(" softwareTransport::buildBeamAfterElements : null pointer on element " );
148        cout << " softwareTransport::buildBeamAfterElements : null pointer on element " << endl;
149        return false;         
150      }
151
152      if ( nameOfSoftware_->doAcceptElement(ptr->getNomdElement().getElementType())  != TBoolOk ) {
153      //      if ( doAcceptElement(ptr->getNomdElement().getElementType())  != TBoolOk ) {
154
155        // si l'element doit etre ignore, on renvoie sur le diag precedent
156        dataManager_->updateCurrentDiagnostic(false);
157      // // si l'element est un snapshot, recuperer la sortie precedente
158      //        if(ptr->getNomdElement().getElementType() == snapshot) {
159      //          string* param = ptr->getParametersString();
160      //          string cliche = workingDir + param[2].c_str();
161      //          cout<<"["<<k<<"] : ecrit sur fichier " << cliche << " le contenu de beam[ ]"<<endl;
162      //        }
163      } else {
164        // si le numero est reconnu de transport
165
166
167
168        string elemLabel = ptr->getLabel();
169        beam2Moments transpMoments;
170        // lecture sortie transport
171        if (!moments2FromTransport(workingDir,elemLabel,ptr->getNomdElement(),transpMoments) )
172          {
173            // si echec, on renvoie sur le diag precedent
174            dataManager_->updateCurrentDiagnostic(false);
175
176            dataManager_->consoleMessage(" softwareTransport::buildBeamAfterElements : element skipped in reading moments from transport output :  " + elemLabel);
177            //   cout << " softwareTransport::buildBeamAfterElements plantage dans lecture moments " << endl;
178            return true;
179          } else {
180          // si succes,
181        // on initialise une nouvelle sortie diagnostic
182          particleBeam* newDiag = dataManager_->updateCurrentDiagnostic(true);
183        // et on la complete
184          newDiag->set2Moments(transpMoments);
185        }
186      }
187    }
188  return result;
189}
190bool softwareTransport::moments2FromTransport(string workingDir,string elLab,const nomdElements elem,beam2Moments& moments) const 
191{
192  string elementLabel = elLab;
193  // transformer le label en majuscules ; je ne suis pas sur que ca
194  // marchera a tous les coups (glm)
195  std::transform(elementLabel.begin(),elementLabel.end(),elementLabel.begin(),(int (*)(int))std::toupper);
196
197  cout << "softwareTransport::moments2FromTransport on cherche element " << elementLabel << endl;
198 
199  string nameIn = workingDir + "transport.output";
200  ifstream infile;
201  infile.open(nameIn.c_str(), ios::in);
202  if (!infile) {
203    dataManager_->consoleMessage(" softwareTransport::moments2FromTransport : error re-opening transport output stream ");
204    cerr << " dataManager::moments2FromTransport : error re-opening transport output stream " << nameIn << endl;
205    return false;
206  }
207
208  string::size_type nn = string::npos;
209  string  fichier;
210  string buf;
211  unsigned compteur = 0;
212  while ( getline(infile, buf) ) {
213    fichier += buf+"\n";
214    nn = buf.find(elementLabel);
215    if ( nn != string::npos ) {
216      compteur++;
217    }
218  }
219  infile.close();
220
221  cout << " compteur= " << compteur << endl;
222  if ( compteur == 0 ) {
223    dataManager_->consoleMessage(" softwareTransport::moments2FromTransport : element"+elementLabel+"not found in the file "+nameIn);
224    cerr << " dataManager::moments2FromTransport : element " << elementLabel << " non trouve dans le fichier  " << nameIn << endl;
225    return false;
226  }
227
228  stringstream fichierStream(fichier);
229  buf.clear();
230  unsigned relu = 0;
231  while ( std::getline(fichierStream, buf) ) {
232    //    cout << " relecture buffer : " << buf << endl;
233    nn = buf.find(elementLabel);
234    if ( nn != string::npos ) {
235      relu++;
236      if ( relu == compteur ) {
237        cout << " TROUVE !" << endl;
238        break;
239      }
240    }
241  }
242
243  return moments.readFromTransportOutput(fichierStream);
244}
245
246string softwareTransport::elementsData(const vector< pair<string, vector<string> > >& donnees) const {
247  unsigned k;
248  cout << " PASSAGE softwareTransport::elementsData " << endl;
249  if ( donnees.at(0).first != "labelsGenericSpecific" ) {
250    cout << " softwareTransport::elementsData ERROR : element badly defined " << endl;
251    return string();
252  }
253  string genericName = donnees.at(0).second.at(0);
254  if ( genericName == "beam" ) return beamData(donnees);
255  if ( genericName == "drift" ) return driftData(donnees);
256  if ( genericName == "solnd" ) return solenoData(donnees);
257  if ( genericName == "bend" ) return bendData(donnees);
258  if ( genericName == "fit" ) return fitData(donnees);
259  return string();
260}
261
262string softwareTransport::beamData(const vector< pair<string, vector<string> > >& donnees) const {
263
264  cout << " PASSAGE softwareTransport::beamData " << endl;
265  double x = 0.0;
266  double xp = 0.0;
267  double y = 0.0;
268  double yp = 0.0;
269  double dl = 0.0;
270  double del = 0.0;
271  double p0 = 0.0;
272  string specificName = "";
273
274  unsigned k;
275  for ( k=0; k < donnees.size(); k++) {
276    if ( donnees.at(k).first == "transverseCoordinates" ) { 
277      x = atof( donnees.at(k).second.at(0).c_str() );
278      xp = atof( donnees.at(k).second.at(1).c_str() );
279      y = atof( donnees.at(k).second.at(2).c_str() );
280      yp = atof( donnees.at(k).second.at(3).c_str() );
281    } else if ( donnees.at(k).first == "longitudinalCoordinates" ) {
282      dl = atof( donnees.at(k).second.at(0).c_str() );
283      del = atof( donnees.at(k).second.at(1).c_str() );
284    } else if ( donnees.at(k).first == "momentum" ) {
285      p0 = atof( donnees.at(k).second.at(0).c_str() );
286    } else if ( donnees.at(k).first == "labelsGenericSpecific" ) {
287      specificName = donnees.at(k).second.at(1);
288    }
289  }
290  ostringstream sortie;
291    beam2Moments moments(x, xp, y, yp, dl, del);
292    sortie << specificName << ":" << moments.writeToTransportInput(p0) << endl;
293    return sortie.str(); 
294  return sortie.str();
295}
296
297string softwareTransport::driftData(const vector< pair<string, vector<string> > >& donnees) const {
298  cout << " PASSAGE softwareTransport::driftData " << endl;
299
300  string lenght = "0.0";
301  string specificName = "";
302
303  unsigned k;
304  for ( k=0; k < donnees.size(); k++) {
305    if ( donnees.at(k).first == "lenghtAperture" ) { 
306      lenght = donnees.at(k).second.at(0);
307    } else if ( donnees.at(k).first == "labelsGenericSpecific" ) {
308      specificName = donnees.at(k).second.at(1);
309    }
310  }
311  ostringstream sortie;
312    // longueur en m
313  sortie << specificName << ":" << "  DRIFT, L=" << 0.01*atof(lenght.c_str()) << ";" << endl;
314  return sortie.str();
315}
316
317
318string softwareTransport::solenoData(const vector< pair<string, vector<string> > >& donnees) const {
319  cout << " softwareTransport::solenoData " << endl;
320  string lenght = "0.0";
321  string B0 = "0.0";
322  string varyB0 = "0";
323  string specificName = "";
324
325
326  unsigned k;
327  for ( k=0; k < donnees.size(); k++) {
328    if ( donnees.at(k).first == "lenghtAperture" ) { 
329      lenght = donnees.at(k).second.at(0);
330    } else if ( donnees.at(k).first == "field" ) {
331      B0 = donnees.at(k).second.at(0);
332    } else if ( donnees.at(k).first == "isBvariable" ) {
333      varyB0 = donnees.at(k).second.at(0);
334    } else if ( donnees.at(k).first == "labelsGenericSpecific" ) {
335      specificName = donnees.at(k).second.at(1);
336    }
337  }
338    ostringstream sortie;
339    // on passe la longueur en metres
340    sortie << specificName << ":" << "  SOLENOID, L=" << 0.01*atof (lenght.c_str()) << ", B=" << B0 << ";" << endl;
341    if ( atoi(varyB0.c_str()) ) {
342      sortie <<  "  VARY, " << specificName+"[B]"  << ";" << endl;
343    }
344    return sortie.str();
345
346}
347
348
349string softwareTransport::bendData(const vector< pair<string, vector<string> > >& donnees) const {
350
351  cout << " softwareTransport::bendData " << endl;
352
353  string lenght = "0.0";
354  string radius = "0.0";
355  string angleDeg = "0.0";
356  string beta1 = "0.0";
357  string beta2 = "0.0";
358
359
360
361  string specificName = "";
362
363  unsigned k;
364  for ( k=0; k < donnees.size(); k++) {
365    if ( donnees.at(k).first == "lenghtAperture" ) { 
366      lenght = donnees.at(k).second.at(0);
367    } else if ( donnees.at(k).first == "angleDegre" ) {
368      angleDeg = donnees.at(k).second.at(0);
369    } else if ( donnees.at(k).first == "radius" ) {
370      radius = donnees.at(k).second.at(0);
371     } else if ( donnees.at(k).first == "rotatedFaces" ) {
372      beta1 = donnees.at(k).second.at(0);
373      beta2 = donnees.at(k).second.at(1);
374    } else if ( donnees.at(k).first == "labelsGenericSpecific" ) {
375      specificName = donnees.at(k).second.at(1);
376    }   
377  }
378
379    ostringstream sortie;
380    sortie << specificName << ":" << "  SBEND, RADIUS=" << radius << ", ANGLE=" << angleDeg << ", E1=" << beta1 << ", E2=" << beta2 << ";" << endl;
381    return sortie.str();
382
383}
384
385
386string softwareTransport::fitData(const vector< pair<string, vector<string> > >& donnees) const {
387  cout << " softwareTransport::fitData " << endl;
388  string variableName = "";
389  string variableValue = "0.0";
390  string tolerance = "0.0";
391  string specificName = "";
392
393  unsigned k;
394  for ( k=0; k < donnees.size(); k++) {
395    if ( donnees.at(k).first == "variable") { 
396      variableName = donnees.at(k).second.at(0);
397      variableValue = donnees.at(k).second.at(1);
398      tolerance = donnees.at(k).second.at(2);
399    } else if ( donnees.at(k).first == "labelsGenericSpecific" ) {
400      specificName = donnees.at(k).second.at(1);
401    }   
402  }
403    ostringstream sortie;
404    sortie << specificName << ":" << "  FIT," << variableName << " = " << variableValue << ", TOLER = " << tolerance << " ;" << endl;
405    if ( variableName == "R11" || variableName == "R22" || variableName == "R12" || variableName == "R21" ) {
406        sortie << " PRINT, TRANS;" << endl;
407    }
408    return sortie.str();
409}
Note: See TracBrowser for help on using the repository browser.