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

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

ajout traitement utilisateur

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