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