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

Last change on this file since 455 was 455, checked in by garnier, 10 years ago

grosse modification pour intégrer les sections

File size: 11.1 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_ = nomDeLogiciel("generator");
12}
13
14softwareGenerator::softwareGenerator(string inputFileName, sectionToExecute* sect) : abstractSoftware(inputFileName, sect)
15{
16  cout << " softwareUsersprogram::softwareGenerator ENREGISTREMENT " << endl;
17  nameOfSoftware_ = 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 DE l'ELEMENT " << numeroDeb_deprecated_ << " A L'ELEMENT " << numeroFin_deprecated_ << 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  if ( !ComputationLimitsOk_deprecated() ) return false;
95
96  if ( numeroDeb_deprecated_ != numeroFin_deprecated_ ) {
97    dataManager_->consoleMessage(" softwareGenerator::buildBeamAfterElements : only one element (rfgun) must be calculated " );
98    return false;
99  }
100  // on initialise une nouvelle sortie diagnostic
101  particleBeam* newDiag = dataManager_->updateCurrentDiagnostic(true);
102
103  //  beams.push_back(particleBeam());
104  vector<double> centroid;
105  bareParticle refPart;
106  vector<bareParticle> particles;
107  vector<bareParticle> particlesPassives; // on ne fait rien de ces particules pour l'instant
108  if (beamFromGenerator(string("faisceau.ini"),workingDir, centroid, refPart,particles, particlesPassives )) {
109    newDiag->setWithParticles(centroid, refPart,particles); 
110  } else {
111    dataManager_->consoleMessage(" softwareGenerator::buildBeamAfterElements : error  " );
112    result = false;
113  }
114  return result;
115}
116
117bool softwareGenerator::beamFromGenerator(string beamFileName, string workingDir, vector<double>& centroid, bareParticle& refPart,vector<bareParticle>& particles, vector<bareParticle>& passiveParticles ) {
118  unsigned  k;
119  FILE* filefais;
120  string nomfilefais = workingDir + beamFileName;
121  cout << " nom fichier sortie generator : " << nomfilefais << endl;
122  filefais = fopen(nomfilefais.c_str(), "r");
123  if ( filefais == (FILE*)0 ) {
124    dataManager_->consoleMessage(" softwareGenerator::beamFromGenerator : error openig the file " + nomfilefais );
125    cerr << " beamFromGenerator() erreur a l'ouverture du fichier" << nomfilefais  << endl;; 
126    return false;
127  }
128  else cout << " beamFromGenerator() : ouverture du fichier " << nomfilefais << endl; 
129
130  generatorParticle partic;
131  std::vector<generatorParticle> faisceau;
132  int nbProbPart =0;
133  double timeRef = 0.0;
134  double betagammaZRef = 0.0;
135  // lecture part. de reference
136  if ( partic.readFromGeneratorFile(filefais) > 0 )
137    {
138         if ( partic.index != 1 ) {
139    dataManager_->consoleMessage(" softwareGenerator::beamFromGenerator : particles are not electrons, we have to reconsider this method beamFromGenerator" );
140           cout << " ERROR softwareGenerator::beamFromGenerator : particles are not electrons, we have to reconsider this method " << endl;
141        return false;
142      }
143   
144      if ( partic.flag != -1 ) {
145        cout << " ATTENTION softwareGenerator::beamFromGenerator : flag different de -1 " << endl;
146      }
147      if (fabs(partic.xx) > EPSILON || fabs(partic.yy) > EPSILON || fabs(partic.px) > EPSILON  || fabs(partic.py) > EPSILON) {
148        printf(" ATTENTION softwareGenerator::beamFromGenerator : part. reference douteuse  \n");
149        partic.imprim();
150      }
151      timeRef = partic.clock;
152      TRIDVECTOR  posRef(100.*partic.xx,100.*partic.yy,100.*partic.zz); // en cm
153      betagammaZRef = partic.pz/EREST_eV;
154      // l'impulsion donnee par generator est en eV/c
155      TRIDVECTOR betagammaRef(partic.px/EREST_eV , partic.py/EREST_eV, betagammaZRef);
156      refPart = bareParticle(posRef, betagammaRef);
157
158      // seule la part. de reference a un pz absolu (les autres pat. on un pz relatif a la ref)
159      // je mets ici a zero, pour homogeneiser
160      partic.pz = 0.0;
161      faisceau.push_back(partic);
162    }
163
164  while( partic.readFromGeneratorFile(filefais) > 0 ) {
165    faisceau.push_back(partic);
166    if ( partic.flag != -1 ) nbProbPart++;
167  }
168
169  if ( faisceau.size() == 0) 
170    {
171    dataManager_->consoleMessage(" softwareGenerator::beamFromGenerator : error no particle found" );
172      cerr << " softwareGenerator::beamFromGenerator echec lecture " << endl;
173      return false;
174    }
175  // pour l'instant on choisit un centroid nul;
176  centroid.clear();
177  centroid = vector<double>(6,0.0);
178
179  particles.clear();
180  passiveParticles.clear();
181  //  particles.resize(faisceau.size() - nbProbPart, bareParticle());
182  //  passiveParticles.resize(nbProbPart, bareParticle());
183  double x,y;
184  //  double deltaz;
185  double cdeltat;
186  TRIDVECTOR  pos;
187  TRIDVECTOR betagamma;
188  double pxPart;
189  double pyPart;
190  double pzPartRel;
191  double deltaPzPart;
192
193  for ( k=0; k < faisceau.size(); k++) {
194
195    pxPart = faisceau.at(k).px;
196    pyPart = faisceau.at(k).py;
197    pzPartRel = faisceau.at(k).pz;
198    deltaPzPart = faisceau.at(k).pz;
199    x=faisceau.at(k).xx;
200    y=faisceau.at(k).yy;
201
202    // tout ce qui suit sera a clarifier
203    double betaGammax = pxPart/EREST_eV;
204    double betaGammay = pyPart/EREST_eV;
205    double betaGammaz = betagammaZRef + pzPartRel/EREST_eV;
206    betagamma.setComponents(betaGammax, betaGammay, betaGammaz);
207    double gamma = sqrt(1.0 + betagamma.norm2());
208
209    // decalage temporel par rapport a la reference  ?
210    // on prend le faisceau sous la forme "a un instant donne"
211
212    //   deltat = faisceau.at(k).clock - timeRef; // nanoseondes
213    cdeltat = CLIGHT_m_per_ns * (faisceau.at(k).clock - timeRef); // metres
214    //    double ds = CLIGHT_m_per_ns * deltat /gamma;
215    //    x += betaGammax * ds;  // en metres
216    //    y += betaGammay * ds;
217
218    // ici on neglige la difference entre gamma de la part. et gamma de la ref.
219    //    deltaz = (pzPartRel/EREST_eV) * CLIGHT_m_per_ns * deltat; // en metres
220
221    //    pos.setComponents(100.*x,100.*y,100.*deltaz);  // en cm
222    pos.setComponents(100.*x,100.*y,100.*cdeltat);  // en cm
223    if ( faisceau.at(k).flag == -1 ) {
224      particles.push_back(bareParticle(pos,betagamma));
225      //      cout << " generator enregistre " << particles.back().FileOutputFlow() << endl;
226    } else {
227      passiveParticles.push_back(bareParticle(pos,betagamma));
228    }
229  }
230
231
232  return true;
233}
234
235
236string softwareGenerator::elementsData(const vector< pair<string, vector<string> > >& donnees) const {
237  unsigned k;
238  cout << " PASSAGE softwareGenerator::elementsData " << endl;
239  if ( donnees.at(0).first != "labelsGenericSpecific" ) {
240    cout << " softwareGenerator::elementsData ERROR : element badly defined " << endl;
241    return string();
242  }
243  string genericName = donnees.at(0).second.at(0);
244  if ( genericName == "rfgun" ) return rfgunData(donnees);
245  return string();
246}
247
248string softwareGenerator::rfgunData(const vector< pair<string, vector<string> > >& donnees) const {
249
250  cout << " PASSAGE softwareGenerator::rfgunData " << endl;
251  string nmacrop = "0";
252  string sigma_t = "0.0";
253  string sigma_r = "0.0";
254  string emit_x = "0.0";
255  string emit_y = "0.0";
256  string E_cin = "0.0";
257  string sigma_E = "0.0";
258  string phaseStep = "0.0";
259  string totalCharge = "0.0";
260
261  unsigned k;
262  for ( k=1; k < donnees.size(); k++) {
263    if ( donnees.at(k).first == "nbMacroparticles" ) { 
264      nmacrop = donnees.at(k).second.at(0);
265    } else if ( donnees.at(k).first == "sigmasTR" ) {
266      sigma_t = donnees.at(k).second.at(0);
267      sigma_r = donnees.at(k).second.at(1);
268    } else if ( donnees.at(k).first == "emittancesXY" ) {
269      emit_x = donnees.at(k).second.at(0);
270      emit_y = donnees.at(k).second.at(1);
271    } else if ( donnees.at(k).first == "kineticE" ) {
272      E_cin = donnees.at(k).second.at(0);
273      sigma_E = donnees.at(k).second.at(1);
274    } else if ( donnees.at(k).first == "phaseStep" ) {
275      phaseStep = donnees.at(k).second.at(0);
276    } else if ( donnees.at(k).first == "totalCharge" ) {
277      totalCharge = donnees.at(k).second.at(0);
278    }
279  }
280  ostringstream sortie;
281
282
283    sortie << "Ipart=" << nmacrop << endl;
284
285    sortie << "Probe=.True." << endl;
286    sortie << "Noise_reduc=.T" << endl;
287    sortie << "Cathode=.T." << endl;
288    sortie << "Q_total=" << totalCharge << endl;
289    sortie << "Ref_zpos=0.0" << endl;
290    sortie << "Ref_clock=0.0" << endl;
291    sortie << "Ref_Ekin=" << E_cin << endl; // tjs en MeV
292    sortie << "Dist_z='g'" << endl;
293    sortie << "sig_clock=" << 1.0e-3*atof(sigma_t.c_str()) << endl; // passage de ps en ns
294    sortie <<  "Dist_pz='g', sig_Ekin=" << 1000.*atof(sigma_E.c_str()) << ",  emit_z=0.0 ,    cor_Ekin=0.0 " << endl;  // passage en keV
295    sortie << "Dist_x='gauss',  sig_x=" <<  10.*atof(sigma_r.c_str()) << endl;    // passage en mm
296    sortie << "Dist_px='g', Nemit_x=" << emit_x << ",   cor_px=0.0" << endl;
297    sortie << "Dist_y='gauss',  sig_y=" <<  10.*atof(sigma_r.c_str()) << endl;    // passage en mm
298    sortie << "Dist_py='g', Nemit_y=" << emit_y << ",   cor_py=0.0" << endl;
299 
300  return sortie.str();
301}
Note: See TracBrowser for help on using the repository browser.