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

Last change on this file since 342 was 342, checked in by touze, 11 years ago

nvx element snapshot

File size: 7.8 KB
Line 
1
2#include "softwareGenerator.h"
3#include "abstractElement.h"
4#include "generatorParticle.h"
5#include "mathematicalConstants.h"
6#include "PhysicalConstants.h"
7
8softwareGenerator::softwareGenerator() : abstractSoftware()
9{;}
10
11softwareGenerator::softwareGenerator(string inputFileName, globalParameters* globals, dataManager* dt) : abstractSoftware(inputFileName, globals, dt)
12{;}
13
14bool softwareGenerator::createInputFile(particleBeam* beamBefore, unsigned int numeroDeb, unsigned int numeroFin, string workingDir)
15{
16  if ( numeroDeb != numeroFin ) return false;
17
18  abstractElement* elPtr;
19  elPtr = dataManager_->getElementPointerFromNumero(numeroDeb);
20
21  if ( elPtr->getNomdElement().getElementType() != RFgun ) {
22    dataManager_->consoleMessage(" softwareGenerator::createInputFile : the element must be rfgun " );
23    cerr << " softwareGenerator::createInputFile : the element must be rfgun" << endl;
24    return false;
25  }
26
27  ofstream outfile;
28  string name = workingDir + inputFileName_;
29  outfile.open(name.c_str(), ios::out);
30  if (!outfile) {
31    dataManager_->consoleMessage(" softwareGenerator::createInputFile : error opening output stream " );
32    cerr << " error opening output stream " << name << endl;
33    return false;
34  }
35  outfile << "&INPUT" << endl;
36  string fichier = "'" + workingDir + "faisceau.ini" + "'";
37  outfile << "   FNAME = " << fichier << endl;
38  outfile << " Add=FALSE, N_add=0" << endl;
39  outfile << " Species='electrons'" << endl;
40  outfile << elPtr->generatorOutputFlow() << endl;
41  outfile << "/" << endl;
42  outfile.close();
43  dataManager_->consoleMessage("fichier input termine pour GENERATOR");
44  return true;
45}
46
47bool softwareGenerator::execute(unsigned int numeroDeb,unsigned int numeroFin,string workingDir) {
48 
49  bool ExecuteStatus = true;
50 
51  ostringstream sortie;
52  sortie << " EXECUTION DE GENERATOR DE l'ELEMENT " << numeroDeb << " A L'ELEMENT " << numeroFin << endl;
53
54  string generatorJob = workingDir + "generator";
55  generatorJob += string("   ");
56  generatorJob += workingDir + inputFileName_;
57
58  string resultOfRun;
59  bool success = launchJob(generatorJob,resultOfRun);
60  sortie << resultOfRun << endl;
61  if ( !success ) {
62    sortie << " launching of generator failed " << endl;
63    ExecuteStatus = false;
64  } else {
65    cout << " execution generator MARCHE " << endl;
66    sortie << resultOfRun;
67    string nameOut = workingDir + "generator.output";
68    ofstream outfile;
69    outfile.open(nameOut.c_str(), ios::out);
70    if (!outfile) {
71      sortie << " error first opening transport output stream " << nameOut << endl;
72      ExecuteStatus = false;
73    } else {
74      // on copie la sortie dans un fichier 'generator.out'
75      outfile << resultOfRun << endl;
76      outfile.close();
77    }
78  }
79 
80  dataManager_->consoleMessage(sortie.str());
81  return ExecuteStatus;
82}
83
84bool  softwareGenerator::buildBeamAfterElements(unsigned int numeroDeb,unsigned int numeroFin, vector<particleBeam>& beams, string workingDir) {
85
86  bool result = true;
87  if ( numeroDeb != numeroFin ) {
88    dataManager_->consoleMessage(" softwareGenerator::buildBeamAfterElements : only one element (rfgun) must be calculated " );
89    return false;
90  }
91
92  beams.push_back(particleBeam());
93  vector<double> centroid;
94  bareParticle refPart;
95  vector<bareParticle> particles;
96  vector<bareParticle> particlesPassives; // on ne fait rien de ces particules pour l'instant
97  if (beamFromGenerator(string("faisceau.ini"),workingDir, centroid, refPart,particles, particlesPassives )) {
98    beams.back().setWithParticles(centroid, refPart,particles); 
99  } else {
100    dataManager_->consoleMessage(" softwareGenerator::buildBeamAfterElements : error  " );
101    result = false;
102  }
103  return result;
104}
105
106bool softwareGenerator::beamFromGenerator(string beamFileName, string workingDir, vector<double>& centroid, bareParticle& refPart,vector<bareParticle>& particles, vector<bareParticle>& passiveParticles ) {
107  unsigned  k;
108  FILE* filefais;
109  string nomfilefais = workingDir + beamFileName;
110  cout << " nom fichier sortie generator : " << nomfilefais << endl;
111  filefais = fopen(nomfilefais.c_str(), "r");
112  if ( filefais == (FILE*)0 ) {
113    dataManager_->consoleMessage(" softwareGenerator::beamFromGenerator : error openig the file " + nomfilefais );
114    cerr << " beamFromGenerator() erreur a l'ouverture du fichier" << nomfilefais  << endl;; 
115    return false;
116  }
117  else cout << " beamFromGenerator() : ouverture du fichier " << nomfilefais << endl; 
118
119  generatorParticle partic;
120  std::vector<generatorParticle> faisceau;
121  int nbProbPart =0;
122  double timeRef = 0.0;
123  double betagammaZRef = 0.0;
124  // lecture part. de reference
125  if ( partic.readFromGeneratorFile(filefais) > 0 )
126    {
127         if ( partic.index != 1 ) {
128    dataManager_->consoleMessage(" softwareGenerator::beamFromGenerator : particles are not electrons, we have to reconsider this method beamFromGenerator" );
129           cout << " ERROR softwareGenerator::beamFromGenerator : particles are not electrons, we have to reconsider this method " << endl;
130        return false;
131      }
132   
133      if ( partic.flag != -1 ) {
134        cout << " ATTENTION softwareGenerator::beamFromGenerator : flag different de -1 " << endl;
135      }
136      if (fabs(partic.xx) > EPSILON || fabs(partic.yy) > EPSILON || fabs(partic.px) > EPSILON  || fabs(partic.py) > EPSILON) {
137        printf(" ATTENTION softwareGenerator::beamFromGenerator : part. reference douteuse  \n");
138        partic.imprim();
139      }
140      timeRef = partic.clock;
141      TRIDVECTOR  posRef(100.*partic.xx,100.*partic.yy,100.*partic.zz); // en cm
142      betagammaZRef = partic.pz/ERESTeV;
143      // l'impulsion donnee par generator est en eV/c
144      TRIDVECTOR betagammaRef(partic.px/ERESTeV , partic.py/ERESTeV, betagammaZRef);
145      refPart = bareParticle(posRef, betagammaRef);
146
147      // seule la part. de reference a un pz absolu (les autres pat. on un pz relatif a la ref)
148      // je mets ici a zero, pour homogeneiser
149      partic.pz = 0.0;
150      faisceau.push_back(partic);
151    }
152
153  while( partic.readFromGeneratorFile(filefais) > 0 ) {
154    faisceau.push_back(partic);
155    if ( partic.flag != -1 ) nbProbPart++;
156  }
157
158  if ( faisceau.size() == 0) 
159    {
160    dataManager_->consoleMessage(" softwareGenerator::beamFromGenerator : error no particle found" );
161      cerr << " softwareGenerator::beamFromGenerator echec lecture " << endl;
162      return false;
163    }
164  // pour l'instant on choisit un centroid nul;
165  centroid.clear();
166  centroid = vector<double>(6,0.0);
167
168  particles.clear();
169  passiveParticles.clear();
170  //  particles.resize(faisceau.size() - nbProbPart, bareParticle());
171  //  passiveParticles.resize(nbProbPart, bareParticle());
172  double x,y;
173  double deltaz;
174  double deltat;
175  TRIDVECTOR  pos;
176  TRIDVECTOR betagamma;
177  double pxPart;
178  double pyPart;
179  double pzPartRel;
180  double deltaPzPart;
181
182  for ( k=0; k < faisceau.size(); k++) {
183
184    pxPart = faisceau.at(k).px;
185    pyPart = faisceau.at(k).py;
186    pzPartRel = faisceau.at(k).pz;
187    deltaPzPart = faisceau.at(k).pz;
188    x=faisceau.at(k).xx;
189    y=faisceau.at(k).yy;
190
191    // tout ce qui suit sera a clarifier
192    double betaGammax = pxPart/ERESTeV;
193    double betaGammay = pyPart/ERESTeV;
194    double betaGammaz = betagammaZRef + pzPartRel/ERESTeV;
195    betagamma.setComponents(betaGammax, betaGammay, betaGammaz);
196    double gamma = sqrt(1.0 + betagamma.norm2());
197
198    // decalage temporel par rapport a la reference 
199    deltat = faisceau.at(k).clock - timeRef; // nanoseondes
200    double ds = CLIGHT_m_per_ns * deltat /gamma;
201    x += betaGammax * ds;  // en metres
202    y += betaGammay * ds;
203
204    // ici on neglige la difference entre gamma de la part. et gamma de la ref.
205    deltaz = (pzPartRel/ERESTeV) * CLIGHT_m_per_ns * deltat; // en metres
206
207    pos.setComponents(100.*x,100.*y,100.*deltaz);  // en cm
208    if ( faisceau.at(k).flag == -1 ) {
209      particles.push_back(bareParticle(pos,betagamma));
210    } else {
211      passiveParticles.push_back(bareParticle(pos,betagamma));
212    }
213  }
214
215
216  return true;
217}
Note: See TracBrowser for help on using the repository browser.