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

Last change on this file since 413 was 413, checked in by lemeur, 11 years ago

vérification (dataManager) de la consécution des sections

File size: 8.2 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_ = nomDeLogiciel("transport");
11}
12
13softwareTransport::softwareTransport(string inputFileName,globalParameters* globals, dataManager* dt) : abstractSoftware(inputFileName, globals, dt)
14{
15  nameOfSoftware_ = nomDeLogiciel("transport");
16}
17
18bool softwareTransport::createInputFile(particleBeam* beamBefore, unsigned int numeroDeb, unsigned int numeroFin, string workingDir)
19{ 
20
21
22
23  if ( !initComputationLimits(numeroDeb,numeroFin) ) return false;
24
25  abstractElement* premierEl = NULL;
26  premierEl = dataManager_->getElementPointerFromNumero(numeroDeb_);
27
28  // ceinture et bretelles
29  if ( premierEl == NULL ) {
30    cerr << " premier element inexistant ?? " << endl;
31    return false;
32  }
33
34  string name = workingDir + inputFileName_;
35  ofstream outfile;
36  outfile.open(name.c_str(), ios::out);
37  if (!outfile) {
38    dataManager_->consoleMessage(" softwareTransport::createInputFile : error opening output stream " );
39    cerr << " error opening output stream " << name << endl;
40    return false;
41  }
42
43  outfile << string("'titre provisoire'") << endl;
44  outfile << " 0 " << endl;
45  outfile << " UTRANS " << endl;
46
47
48  //  cout << "debug:: c'est quoi le premier element? " << premierEl->getNomdElement().getElementName() << endl;
49
50  // si le premier element a traiter est un 'beam', on ne fait rien pour l'instant
51  // il sera traite plus bas de maniere standard
52
53  // Dans le cas où le premier element n'est pas beam
54  if ( premierEl->getNomdElement().getElementType() != beam ) {
55    // s'il n'y a pas faisceau courant, erreur
56    if ( beamBefore == NULL ) {
57      dataManager_->consoleMessage(" softwareTransport::createInputFile : no input beam, input file not created");
58      cout << " pas de faisceau " << endl;
59      return false;
60    } else {
61      // s'il y a un faisceau courant
62      if ( !beamBefore->momentRepresentationOk() ) {
63        // s'il n'est pas au format transport (issu de parmela ?)
64        // on le met au format transport, s'il est deja au format 'particules' (par ex/ parmela)
65        if ( !beamBefore->particleRepresentationOk() ) {
66          dataManager_->consoleMessage(" softwareParmela::createInputFile : ca cafouille dans les representations de faisceau : ni particules, ni moments ? " );
67          cout << " ca cafouille dans les representations de faisceau : ni particules, ni moments ? " << endl;
68          return false;
69        } else {
70          beamBefore->buildMomentRepresentation();
71        }
72      }
73      // ici, en principe, on a un faisceau correct
74      // on ecrit sur le fichier input
75      const beam2Moments& moments = beamBefore->getTransportMoments();
76      double P0 = beamBefore->getP0Transport();
77      outfile << moments.writeToTransportInput(P0);
78    }
79  }
80
81  outfile << " PRINT, BEAM, ON; " << endl;
82
83  abstractElement* elPtr;
84  for (unsigned k = numeroDeb_; k <= numeroFin_; k++)
85    {
86      elPtr = dataManager_->getElementPointerFromNumero(k);
87      cout << "debug:: element [" << k << "] " << elPtr->getNomdElement().getElementName() << endl;
88      outfile << elPtr->transportOutputFlow();
89    }
90
91  outfile << " SENTINEL " << endl;
92  outfile << " SENTINEL " << endl;
93
94  outfile.close();
95  dataManager_->consoleMessage("fichier input termine pour TRANSPORT");
96  return true;
97}
98
99bool softwareTransport::execute(string workingDir)
100{
101  bool ExecuteStatus = true;
102
103  ostringstream sortie;
104  sortie << " EXECUTION DE TRANSPORT DE " << numeroDeb_ << " a " << numeroFin_ << endl;
105 
106  string transportJob = workingDir + "transport";
107  transportJob += string(" <  ");
108  transportJob += workingDir + inputFileName_;
109  cout << " job transport= " << transportJob << endl;
110 
111  string resultOfRun;
112  bool success = launchJob(transportJob,resultOfRun);
113  sortie << resultOfRun << endl;
114  if ( !success ) {
115    sortie << " launching of transport failed " << endl;
116    ExecuteStatus = false;
117  } else {
118    cout << " execution transport MARCHE " << endl;
119    string nameOut = workingDir + "transport.output";
120    ofstream outfile;
121    outfile.open(nameOut.c_str(), ios::out);
122    if (!outfile) {
123      sortie << " error first opening transport output stream " << nameOut << endl;
124      ExecuteStatus = false;
125    } else {
126      // on copie la sortie dans un fichier 'transport.out'
127      outfile << resultOfRun << endl;
128      outfile.close();
129    }
130  }
131
132  dataManager_->consoleMessage(sortie.str());
133  return ExecuteStatus;
134}
135
136bool softwareTransport::buildBeamAfterElements(string workingDir) 
137{
138
139  bool result = true;
140  if ( !ComputationLimitsOk() ) return false;
141
142  for (unsigned k = numeroDeb_; k <= numeroFin_; k++)
143    {
144      abstractElement* ptr = dataManager_->getElementPointerFromNumero(k);
145      if ( ptr == NULL ) {
146        dataManager_->consoleMessage(" softwareTransport::buildBeamAfterElements : null pointer on element " );
147        cout << " softwareTransport::buildBeamAfterElements : null pointer on element " << endl;
148        return false;         
149      }
150
151
152      if ( ptr->is_accepted_by_software(nameOfSoftware_) != TBoolOk ) {
153        // si l'element doit etre ignore, on renvoie sur le diag precedent
154        dataManager_->updateCurrentDiagnostic(false);
155      // // si l'element est un snapshot, recuperer la sortie precedente
156      //        if(ptr->getNomdElement().getElementType() == snapshot) {
157      //          string* param = ptr->getParametersString();
158      //          string cliche = workingDir + param[2].c_str();
159      //          cout<<"["<<k<<"] : ecrit sur fichier " << cliche << " le contenu de beam[ ]"<<endl;
160      //        }
161      } else {
162        // si le numero est reconnu de transport
163
164
165
166        string elemLabel = ptr->getLabel();
167        beam2Moments transpMoments;
168        // lecture sortie transport
169        if (!moments2FromTransport(workingDir,elemLabel,ptr->getNomdElement(),transpMoments) )
170          {
171            // si echec, on renvoie sur le diag precedent
172            dataManager_->updateCurrentDiagnostic(false);
173
174            dataManager_->consoleMessage(" softwareTransport::buildBeamAfterElements : element skipped in reading moments from transport output :  " + elemLabel);
175            //   cout << " softwareTransport::buildBeamAfterElements plantage dans lecture moments " << endl;
176            return true;
177          } else {
178          // si succes,
179        // on initialise une nouvelle sortie diagnostic
180          particleBeam* newDiag = dataManager_->updateCurrentDiagnostic(true);
181        // et on la complete
182          newDiag->set2Moments(transpMoments);
183        }
184      }
185    }
186  return result;
187}
188bool softwareTransport::moments2FromTransport(string workingDir,string elLab,const nomdElements elem,beam2Moments& moments) const 
189{
190  string elementLabel = elLab;
191  // transformer le label en majuscules ; je ne suis pas sur que ca
192  // marchera a tous les coups (glm)
193  std::transform(elementLabel.begin(),elementLabel.end(),elementLabel.begin(),(int (*)(int))std::toupper);
194
195  cout << "softwareTransport::moments2FromTransport on cherche element " << elementLabel << endl;
196 
197  string nameIn = workingDir + "transport.output";
198  ifstream infile;
199  infile.open(nameIn.c_str(), ios::in);
200  if (!infile) {
201    dataManager_->consoleMessage(" softwareTransport::moments2FromTransport : error re-opening transport output stream ");
202    cerr << " dataManager::moments2FromTransport : error re-opening transport output stream " << nameIn << endl;
203    return false;
204  }
205
206  string::size_type nn = string::npos;
207  string  fichier;
208  string buf;
209  unsigned compteur = 0;
210  while ( getline(infile, buf) ) {
211    fichier += buf+"\n";
212    nn = buf.find(elementLabel);
213    if ( nn != string::npos ) {
214      compteur++;
215    }
216  }
217  infile.close();
218
219  cout << " compteur= " << compteur << endl;
220  if ( compteur == 0 ) {
221    dataManager_->consoleMessage(" softwareTransport::moments2FromTransport : element"+elementLabel+"not found in the file "+nameIn);
222    cerr << " dataManager::moments2FromTransport : element " << elementLabel << " non trouve dans le fichier  " << nameIn << endl;
223    return false;
224  }
225
226  stringstream fichierStream(fichier);
227  buf.clear();
228  unsigned relu = 0;
229  while ( std::getline(fichierStream, buf) ) {
230    //    cout << " relecture buffer : " << buf << endl;
231    nn = buf.find(elementLabel);
232    if ( nn != string::npos ) {
233      relu++;
234      if ( relu == compteur ) {
235        cout << " TROUVE !" << endl;
236        break;
237      }
238    }
239  }
240
241  return moments.readFromTransportOutput(fichierStream);
242}
Note: See TracBrowser for help on using the repository browser.