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

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

mise a jour des manipulations de faisceau

File size: 8.3 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  nameOfSoftware_ = nomDeLogiciel("generator");
17}
18
19bool softwareGenerator::createInputFile(particleBeam* beamBefore, unsigned int numeroDeb, unsigned int numeroFin, string workingDir)
20{
21  if ( !initComputationLimits(numeroDeb,numeroFin) ) return false;
22
23  if ( numeroDeb_ != numeroFin_ ) return false;
24
25  abstractElement* elPtr;
26  elPtr = dataManager_->getElementPointerFromNumero(numeroDeb_);
27
28  if ( elPtr->getNomdElement().getElementType() != RFgun ) {
29    dataManager_->consoleMessage(" softwareGenerator::createInputFile : the element must be rfgun " );
30    cerr << " softwareGenerator::createInputFile : the element must be rfgun" << endl;
31    return false;
32  }
33
34  ofstream outfile;
35  string name = workingDir + inputFileName_;
36  outfile.open(name.c_str(), ios::out);
37  if (!outfile) {
38    dataManager_->consoleMessage(" softwareGenerator::createInputFile : error opening output stream " );
39    cerr << " error opening output stream " << name << endl;
40    return false;
41  }
42  outfile << "&INPUT" << endl;
43  string fichier = "'" + workingDir + "faisceau.ini" + "'";
44  outfile << "   FNAME = " << fichier << endl;
45  outfile << " Add=FALSE, N_add=0" << endl;
46  outfile << " Species='electrons'" << endl;
47  outfile << elPtr->generatorOutputFlow() << endl;
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_ << " A L'ELEMENT " << numeroFin_ << 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() ) return false;
95
96  if ( numeroDeb_ != numeroFin_ ) {
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    } else {
226      passiveParticles.push_back(bareParticle(pos,betagamma));
227    }
228  }
229
230
231  return true;
232}
Note: See TracBrowser for help on using the repository browser.