source: PSPA/Interface_Web/trunk/pspaWT/sources/controler/src/softwareGenerator.cc @ 485

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

liste des logiciels compatibles dans les tooltip des elements

File size: 11.0 KB
Line 
1
2#include "softwareGenerator.h"
3#include "abstractElement.h"
4#include "generatorParticle.h"
5#include "mathematicalConstants.h"
6#include "PhysicalConstants.h"
7#include "dataManager.h"
8
9softwareGenerator::softwareGenerator() : abstractSoftware()
10{
11  nameOfSoftware_ = new nomDeLogiciel("generator");
12}
13
14softwareGenerator::softwareGenerator(string inputFileName, sectionToExecute* sect,dataManager* data) : abstractSoftware(inputFileName, sect,data)
15{
16  cout << " softwareUsersprogram::softwareGenerator ENREGISTREMENT " << endl;
17  nameOfSoftware_ = new  nomDeLogiciel("generator");
18  //  registerElement(nomdElements::RFgun,TBoolOk);
19}
20
21bool softwareGenerator::createInputFile(particleBeam* beamBefore, string workingDir)
22{
23  abstractElement* firstElement = getSectionToExecute()->getElements().front();
24 
25  if ( firstElement->getNomdElement().getElementType() != nomdElements::RFgun ) {
26    dataManager_->consoleMessage(" softwareGenerator::createInputFile : the element must be rfgun " );
27    cerr << " softwareGenerator::createInputFile : the element must be rfgun" << endl;
28    return false;
29  }
30
31  ofstream outfile;
32  string name = workingDir + inputFileName_;
33  outfile.open(name.c_str(), ios::out);
34  if (!outfile) {
35    dataManager_->consoleMessage(" softwareGenerator::createInputFile : error opening output stream " );
36    cerr << " error opening output stream " << name << endl;
37    return false;
38  }
39  outfile << "&INPUT" << endl;
40  string fichier = "'" + workingDir + "faisceau.ini" + "'";
41  outfile << "   FNAME = " << fichier << endl;
42  outfile << " Add=FALSE, N_add=0" << endl;
43  outfile << " Species='electrons'" << endl;
44  //  outfile << firstElement->generatorOutputFlow() << endl;
45
46  outfile << elementsData(firstElement->parametersToSoftware()) << endl;
47
48  outfile << "/" << endl;
49  outfile.close();
50  dataManager_->consoleMessage("fichier input termine pour GENERATOR");
51  return true;
52}
53
54bool softwareGenerator::execute(string workingDir) {
55 
56  bool ExecuteStatus = true;
57 
58  ostringstream sortie;
59  sortie << " EXECUTION DE GENERATOR  " << endl;
60
61  string generatorJob = workingDir + "generator";
62  generatorJob += string("   ");
63  generatorJob += workingDir + inputFileName_;
64
65  string resultOfRun;
66  bool success = launchJob(generatorJob,resultOfRun);
67  sortie << resultOfRun << endl;
68  if ( !success ) {
69    sortie << " launching of generator failed " << endl;
70    ExecuteStatus = false;
71  } else {
72    cout << " execution generator MARCHE " << endl;
73    sortie << resultOfRun;
74    string nameOut = workingDir + "generator.output";
75    ofstream outfile;
76    outfile.open(nameOut.c_str(), ios::out);
77    if (!outfile) {
78      sortie << " error first opening transport output stream " << nameOut << endl;
79      ExecuteStatus = false;
80    } else {
81      // on copie la sortie dans un fichier 'generator.out'
82      outfile << resultOfRun << endl;
83      outfile.close();
84    }
85  }
86 
87  dataManager_->consoleMessage(sortie.str());
88  return ExecuteStatus;
89}
90
91bool  softwareGenerator::buildBeamAfterElements( string workingDir) {
92
93  bool result = true;
94
95  if ( getSectionToExecute()->getElements().size() != 1 ) {
96    dataManager_->consoleMessage(" softwareGenerator::buildBeamAfterElements : only one element (rfgun) must be calculated " );
97    return false;
98  }
99  // on initialise une nouvelle sortie diagnostic
100  particleBeam* newDiag = dataManager_->updateCurrentDiagnostic(true);
101
102  //  beams.push_back(particleBeam());
103  vector<double> centroid;
104  bareParticle refPart;
105  vector<bareParticle> particles;
106  vector<bareParticle> particlesPassives; // on ne fait rien de ces particules pour l'instant
107  if (beamFromGenerator(string("faisceau.ini"),workingDir, centroid, refPart,particles, particlesPassives )) {
108    newDiag->setWithParticles(centroid, refPart,particles); 
109  } else {
110    dataManager_->consoleMessage(" softwareGenerator::buildBeamAfterElements : error  " );
111    result = false;
112  }
113  return result;
114}
115
116bool softwareGenerator::beamFromGenerator(string beamFileName, string workingDir, vector<double>& centroid, bareParticle& refPart,vector<bareParticle>& particles, vector<bareParticle>& passiveParticles ) {
117  unsigned  k;
118  FILE* filefais;
119  string nomfilefais = workingDir + beamFileName;
120  cout << " nom fichier sortie generator : " << nomfilefais << endl;
121  filefais = fopen(nomfilefais.c_str(), "r");
122  if ( filefais == (FILE*)0 ) {
123    dataManager_->consoleMessage(" softwareGenerator::beamFromGenerator : error openig the file " + nomfilefais );
124    cerr << " beamFromGenerator() erreur a l'ouverture du fichier" << nomfilefais  << endl;; 
125    return false;
126  }
127  else cout << " beamFromGenerator() : ouverture du fichier " << nomfilefais << endl; 
128
129  generatorParticle partic;
130  std::vector<generatorParticle> faisceau;
131  int nbProbPart =0;
132  double timeRef = 0.0;
133  double betagammaZRef = 0.0;
134  // lecture part. de reference
135  if ( partic.readFromGeneratorFile(filefais) > 0 )
136    {
137         if ( partic.index != 1 ) {
138    dataManager_->consoleMessage(" softwareGenerator::beamFromGenerator : particles are not electrons, we have to reconsider this method beamFromGenerator" );
139           cout << " ERROR softwareGenerator::beamFromGenerator : particles are not electrons, we have to reconsider this method " << endl;
140        return false;
141      }
142   
143      if ( partic.flag != -1 ) {
144        cout << " ATTENTION softwareGenerator::beamFromGenerator : flag different de -1 " << endl;
145      }
146      if (fabs(partic.xx) > EPSILON || fabs(partic.yy) > EPSILON || fabs(partic.px) > EPSILON  || fabs(partic.py) > EPSILON) {
147        printf(" ATTENTION softwareGenerator::beamFromGenerator : part. reference douteuse  \n");
148        partic.imprim();
149      }
150      timeRef = partic.clock;
151      TRIDVECTOR  posRef(100.*partic.xx,100.*partic.yy,100.*partic.zz); // en cm
152      betagammaZRef = partic.pz/EREST_eV;
153      // l'impulsion donnee par generator est en eV/c
154      TRIDVECTOR betagammaRef(partic.px/EREST_eV , partic.py/EREST_eV, betagammaZRef);
155      refPart = bareParticle(posRef, betagammaRef);
156
157      // seule la part. de reference a un pz absolu (les autres pat. on un pz relatif a la ref)
158      // je mets ici a zero, pour homogeneiser
159      partic.pz = 0.0;
160      faisceau.push_back(partic);
161    }
162
163  while( partic.readFromGeneratorFile(filefais) > 0 ) {
164    faisceau.push_back(partic);
165    if ( partic.flag != -1 ) nbProbPart++;
166  }
167
168  if ( faisceau.size() == 0) 
169    {
170    dataManager_->consoleMessage(" softwareGenerator::beamFromGenerator : error no particle found" );
171      cerr << " softwareGenerator::beamFromGenerator echec lecture " << endl;
172      return false;
173    }
174  // pour l'instant on choisit un centroid nul;
175  centroid.clear();
176  centroid = vector<double>(6,0.0);
177
178  particles.clear();
179  passiveParticles.clear();
180  //  particles.resize(faisceau.size() - nbProbPart, bareParticle());
181  //  passiveParticles.resize(nbProbPart, bareParticle());
182  double x,y;
183  //  double deltaz;
184  double cdeltat;
185  TRIDVECTOR  pos;
186  TRIDVECTOR betagamma;
187  double pxPart;
188  double pyPart;
189  double pzPartRel;
190  double deltaPzPart;
191
192  for ( k=0; k < faisceau.size(); k++) {
193
194    pxPart = faisceau.at(k).px;
195    pyPart = faisceau.at(k).py;
196    pzPartRel = faisceau.at(k).pz;
197    deltaPzPart = faisceau.at(k).pz;
198    x=faisceau.at(k).xx;
199    y=faisceau.at(k).yy;
200
201    // tout ce qui suit sera a clarifier
202    double betaGammax = pxPart/EREST_eV;
203    double betaGammay = pyPart/EREST_eV;
204    double betaGammaz = betagammaZRef + pzPartRel/EREST_eV;
205    betagamma.setComponents(betaGammax, betaGammay, betaGammaz);
206    double gamma = sqrt(1.0 + betagamma.norm2());
207
208    // decalage temporel par rapport a la reference  ?
209    // on prend le faisceau sous la forme "a un instant donne"
210
211    //   deltat = faisceau.at(k).clock - timeRef; // nanoseondes
212    cdeltat = CLIGHT_m_per_ns * (faisceau.at(k).clock - timeRef); // metres
213    //    double ds = CLIGHT_m_per_ns * deltat /gamma;
214    //    x += betaGammax * ds;  // en metres
215    //    y += betaGammay * ds;
216
217    // ici on neglige la difference entre gamma de la part. et gamma de la ref.
218    //    deltaz = (pzPartRel/EREST_eV) * CLIGHT_m_per_ns * deltat; // en metres
219
220    //    pos.setComponents(100.*x,100.*y,100.*deltaz);  // en cm
221    pos.setComponents(100.*x,100.*y,100.*cdeltat);  // en cm
222    if ( faisceau.at(k).flag == -1 ) {
223      particles.push_back(bareParticle(pos,betagamma));
224      //      cout << " generator enregistre " << particles.back().FileOutputFlow() << endl;
225    } else {
226      passiveParticles.push_back(bareParticle(pos,betagamma));
227    }
228  }
229
230
231  return true;
232}
233
234
235string softwareGenerator::elementsData(const vector< pair<string, vector<string> > >& donnees) const {
236  unsigned k;
237  cout << " PASSAGE softwareGenerator::elementsData " << endl;
238  if ( donnees.at(0).first != "labelsGenericSpecific" ) {
239    cout << " softwareGenerator::elementsData ERROR : element badly defined " << endl;
240    return string();
241  }
242  string genericName = donnees.at(0).second.at(0);
243  if ( genericName == "rfgun" ) return rfgunData(donnees);
244  return string();
245}
246
247string softwareGenerator::rfgunData(const vector< pair<string, vector<string> > >& donnees) const {
248
249  cout << " PASSAGE softwareGenerator::rfgunData " << endl;
250  string nmacrop = "0";
251  string sigma_t = "0.0";
252  string sigma_r = "0.0";
253  string emit_x = "0.0";
254  string emit_y = "0.0";
255  string E_cin = "0.0";
256  string sigma_E = "0.0";
257  string phaseStep = "0.0";
258  string totalCharge = "0.0";
259
260  unsigned k;
261  for ( k=1; k < donnees.size(); k++) {
262    if ( donnees.at(k).first == "nbMacroparticles" ) { 
263      nmacrop = donnees.at(k).second.at(0);
264    } else if ( donnees.at(k).first == "sigmasTR" ) {
265      sigma_t = donnees.at(k).second.at(0);
266      sigma_r = donnees.at(k).second.at(1);
267    } else if ( donnees.at(k).first == "emittancesXY" ) {
268      emit_x = donnees.at(k).second.at(0);
269      emit_y = donnees.at(k).second.at(1);
270    } else if ( donnees.at(k).first == "kineticE" ) {
271      E_cin = donnees.at(k).second.at(0);
272      sigma_E = donnees.at(k).second.at(1);
273    } else if ( donnees.at(k).first == "phaseStep" ) {
274      phaseStep = donnees.at(k).second.at(0);
275    } else if ( donnees.at(k).first == "totalCharge" ) {
276      totalCharge = donnees.at(k).second.at(0);
277    }
278  }
279  ostringstream sortie;
280
281
282    sortie << "Ipart=" << nmacrop << endl;
283
284    sortie << "Probe=.True." << endl;
285    sortie << "Noise_reduc=.T" << endl;
286    sortie << "Cathode=.T." << endl;
287    sortie << "Q_total=" << totalCharge << endl;
288    sortie << "Ref_zpos=0.0" << endl;
289    sortie << "Ref_clock=0.0" << endl;
290    sortie << "Ref_Ekin=" << E_cin << endl; // tjs en MeV
291    sortie << "Dist_z='g'" << endl;
292    sortie << "sig_clock=" << 1.0e-3*atof(sigma_t.c_str()) << endl; // passage de ps en ns
293    sortie <<  "Dist_pz='g', sig_Ekin=" << 1000.*atof(sigma_E.c_str()) << ",  emit_z=0.0 ,    cor_Ekin=0.0 " << endl;  // passage en keV
294    sortie << "Dist_x='gauss',  sig_x=" <<  10.*atof(sigma_r.c_str()) << endl;    // passage en mm
295    sortie << "Dist_px='g', Nemit_x=" << emit_x << ",   cor_px=0.0" << endl;
296    sortie << "Dist_y='gauss',  sig_y=" <<  10.*atof(sigma_r.c_str()) << endl;    // passage en mm
297    sortie << "Dist_py='g', Nemit_y=" << emit_y << ",   cor_py=0.0" << endl;
298 
299  return sortie.str();
300}
Note: See TracBrowser for help on using the repository browser.