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

Last change on this file since 359 was 359, checked in by garnier, 11 years ago

Changement de la couleur des sections selectionnes + plein dautres choses

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