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

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

introduction softwareTest

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