1 | |
---|
2 | #include "particleBeam.h" |
---|
3 | #include "mathematicalConstants.h" |
---|
4 | #include "PhysicalConstants.h" |
---|
5 | #include "mathematicalTools.h" |
---|
6 | #include "mixedTools.h" |
---|
7 | |
---|
8 | #include "UAP/UAPUtilities.hpp" |
---|
9 | #include "AML/AMLReader.hpp" |
---|
10 | |
---|
11 | #include <stdio.h> |
---|
12 | #include <algorithm> |
---|
13 | #include <sstream> |
---|
14 | |
---|
15 | using namespace std; |
---|
16 | |
---|
17 | particleBeam::particleBeam() { |
---|
18 | P0Transport_ = 0.0; |
---|
19 | clear(); |
---|
20 | particleRepresentationOk_ = false; |
---|
21 | momentRepresentationOk_ = false; |
---|
22 | } |
---|
23 | |
---|
24 | void particleBeam::clear() { |
---|
25 | relativePartic_.clear(); |
---|
26 | rij_.raz(); |
---|
27 | P0Transport_ = 0.0; |
---|
28 | particleRepresentationOk_ = false; |
---|
29 | momentRepresentationOk_ = false; |
---|
30 | } |
---|
31 | |
---|
32 | unsigned long particleBeam::getNbParticles() const { |
---|
33 | return relativePartic_.size(); |
---|
34 | } |
---|
35 | |
---|
36 | const beam2Moments& particleBeam::getTransportMoments() const { |
---|
37 | return rij_; |
---|
38 | } |
---|
39 | |
---|
40 | double particleBeam::getSigmaTransportij(unsigned indexI, unsigned indexJ) { |
---|
41 | if ( indexI > 5 || indexJ > 5 ) { |
---|
42 | cerr << " particleBeam::getSigmaTransportij() indices out of range " << endl; |
---|
43 | return 0.0; |
---|
44 | } |
---|
45 | if ( !momentRepresentationOk_ ) { |
---|
46 | cerr << " particleBeam::getSigmaTransportij() beam is not in moment representation " << endl; |
---|
47 | return 0.0; |
---|
48 | } |
---|
49 | if ( indexI >= indexJ ) { |
---|
50 | return ( rij_.getMatrix().at(indexI) ).at(indexJ); |
---|
51 | } else { |
---|
52 | return ( rij_.getMatrix().at(indexJ) ).at(indexI); |
---|
53 | } |
---|
54 | |
---|
55 | } |
---|
56 | |
---|
57 | double particleBeam::getUnnormalizedEmittanceX() { |
---|
58 | double r = getSigmaTransportij(1,0); |
---|
59 | double rac = (1 - r*r); |
---|
60 | if ( rac <= 0.0 ) { |
---|
61 | return 0.0; |
---|
62 | } |
---|
63 | rac = sqrt(1 - r*r); |
---|
64 | return dimensionalFactorFromTransportToGraphics(0)*getSigmaTransportij(0,0) * getSigmaTransportij(1,1) * rac; // en pi.mm.mrad |
---|
65 | } |
---|
66 | |
---|
67 | double particleBeam::getUnnormalizedTranspPhaseSpaceArea(unsigned TranspIndexAbs, unsigned TranspIndexOrd) { |
---|
68 | if ( TranspIndexAbs == TranspIndexOrd ) return 0.0; |
---|
69 | double r = getSigmaTransportij(TranspIndexAbs,TranspIndexOrd); |
---|
70 | double rac = (1 - r*r); |
---|
71 | if ( rac <= 0.0 ) { |
---|
72 | return 0.0; |
---|
73 | } |
---|
74 | rac = sqrt(1 - r*r); |
---|
75 | return dimensionalFactorFromTransportToGraphics(TranspIndexAbs)*getSigmaTransportij(TranspIndexAbs,TranspIndexAbs) * |
---|
76 | dimensionalFactorFromTransportToGraphics(TranspIndexOrd)*getSigmaTransportij(TranspIndexOrd,TranspIndexOrd) * rac; // en pi.mm.mrad |
---|
77 | } |
---|
78 | |
---|
79 | |
---|
80 | |
---|
81 | double particleBeam::getP0Transport() const { |
---|
82 | return P0Transport_; |
---|
83 | } |
---|
84 | |
---|
85 | double particleBeam::referenceKineticEnergyMeV() const { |
---|
86 | if ( particleRepresentationOk_ ) { |
---|
87 | return (referenceParticle_.getGamma() -1.) * EREST_MeV; |
---|
88 | } else { |
---|
89 | double P0Norm = 1000.0 * P0Transport_ / EREST_MeV; |
---|
90 | double gamma = sqrt(1.0 + P0Norm * P0Norm); |
---|
91 | return (gamma - 1.0) * EREST_MeV; |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | void particleBeam::set2Moments(beam2Moments& moments) { |
---|
96 | rij_ = moments; |
---|
97 | momentRepresentationOk_ = true; |
---|
98 | } |
---|
99 | |
---|
100 | void particleBeam::setWithParticles(vector<double>& centroid, bareParticle& referencePart, vector<bareParticle>& particles) { |
---|
101 | cout << " particleBeam::setWithParticles taille vect. part. " << particles.size() << endl; |
---|
102 | centroid_.clear(); |
---|
103 | centroid_ = centroid; |
---|
104 | referenceParticle_ = referencePart; |
---|
105 | relativePartic_.clear(); |
---|
106 | relativePartic_ = particles; |
---|
107 | cout << " particleBeam::setWithParticles taille vect. part. ENREGISTRE " << relativePartic_.size() << endl; |
---|
108 | particleRepresentationOk_ = true; |
---|
109 | } |
---|
110 | |
---|
111 | bool particleBeam::particleRepresentationOk() const { |
---|
112 | return particleRepresentationOk_; |
---|
113 | } |
---|
114 | bool particleBeam::momentRepresentationOk() const { |
---|
115 | return momentRepresentationOk_; |
---|
116 | } |
---|
117 | |
---|
118 | void particleBeam::addParticle( bareParticle p) |
---|
119 | { |
---|
120 | relativePartic_.push_back(p); |
---|
121 | } |
---|
122 | |
---|
123 | const vector<bareParticle>& particleBeam::getParticleVector() const |
---|
124 | { |
---|
125 | return relativePartic_; |
---|
126 | } |
---|
127 | |
---|
128 | vector<bareParticle>& particleBeam::getParticleVector() |
---|
129 | { |
---|
130 | return relativePartic_; |
---|
131 | } |
---|
132 | |
---|
133 | // void particleBeam::getVariance(double& varx, double& vary, double& varz) const { |
---|
134 | // unsigned int k; |
---|
135 | // double x,y,z; |
---|
136 | // double xav = 0.; |
---|
137 | // double yav = 0.; |
---|
138 | // double zav = 0.; |
---|
139 | // double xavsq = 0.; |
---|
140 | // double yavsq = 0.; |
---|
141 | // double zavsq = 0.; |
---|
142 | |
---|
143 | // TRIDVECTOR pos; |
---|
144 | |
---|
145 | |
---|
146 | // for ( k = 0 ; k < goodPartic_.size(); k++) { |
---|
147 | // pos = goodPartic_.at(k).getPosition(); |
---|
148 | // pos.getComponents(x,y,z); |
---|
149 | // // partic_[k].getXYZ(x,y,z); |
---|
150 | // xav += x; |
---|
151 | // xavsq += x*x; |
---|
152 | // yav += y; |
---|
153 | // yavsq += y*y; |
---|
154 | // zav += z; |
---|
155 | // zavsq += z*z; |
---|
156 | // } |
---|
157 | |
---|
158 | // double aginv = double (goodPartic_.size()); |
---|
159 | // aginv = 1.0/aginv; |
---|
160 | |
---|
161 | // varx = aginv * ( xavsq - xav*xav*aginv ); |
---|
162 | // vary = aginv * ( yavsq - yav*yav*aginv ); |
---|
163 | // varz = aginv * ( zavsq - zav*zav*aginv ); |
---|
164 | // } |
---|
165 | |
---|
166 | |
---|
167 | void particleBeam::printAllXYZ() const { |
---|
168 | cout << " dump du faisceau : " << endl; |
---|
169 | cout << relativePartic_.size() << " particules " << endl; |
---|
170 | unsigned int k; |
---|
171 | for ( k = 0 ; k < relativePartic_.size(); k++) |
---|
172 | { |
---|
173 | double xx,yy,zz; |
---|
174 | relativePartic_.at(k).getPosition().getComponents(xx,yy,zz); |
---|
175 | double betgamx, betgamy, betgamz; |
---|
176 | relativePartic_.at(k).getBetaGamma().getComponents(betgamx, betgamy, betgamz); |
---|
177 | cout << " part. numero " << k << " x= " << xx << " y= " << yy << " dphas (c.dt, cm) = " << zz << " betgamx= " << betgamx << " betgamy= " << betgamy << " betgamz= " << betgamz << endl; |
---|
178 | } |
---|
179 | } |
---|
180 | |
---|
181 | |
---|
182 | // extension en phase (cm) |
---|
183 | void particleBeam::ZrangeCdt(double& cdtmin, double& cdtmax) const { |
---|
184 | double z; |
---|
185 | cdtmin = GRAND; |
---|
186 | cdtmax = -cdtmin; |
---|
187 | |
---|
188 | unsigned int k; |
---|
189 | for ( k = 0 ; k < relativePartic_.size(); k++) |
---|
190 | { |
---|
191 | z = relativePartic_.at(k).getZ(); // ce z est un dephasage, c.dt, en cm |
---|
192 | if ( z < cdtmin ) cdtmin = z; |
---|
193 | else if ( z > cdtmax) cdtmax = z; |
---|
194 | } |
---|
195 | } |
---|
196 | |
---|
197 | |
---|
198 | |
---|
199 | string particleBeam::fileOutputFlow() const { |
---|
200 | ostringstream sortie; |
---|
201 | unsigned int k; |
---|
202 | for ( k = 0 ; k < relativePartic_.size(); k++) |
---|
203 | { |
---|
204 | sortie << relativePartic_.at(k).FileOutputFlow() << endl; |
---|
205 | } |
---|
206 | sortie << endl; |
---|
207 | return sortie.str(); |
---|
208 | } |
---|
209 | |
---|
210 | |
---|
211 | void particleBeam::writeToAMLFile(string fileName) { |
---|
212 | UAPNode* uap_root = new UAPNode("UAP_root"); |
---|
213 | UAPNode* rep = uap_root->addChild("AML_representation"); |
---|
214 | |
---|
215 | // root node in the hierarchy |
---|
216 | UAPNode* lab = rep->addChild("laboratory"); |
---|
217 | lab->addAttribute("name","PSPA lab"); |
---|
218 | |
---|
219 | |
---|
220 | |
---|
221 | // root node in the hierarchy |
---|
222 | UAPNode* par = lab->addChild("particles"); |
---|
223 | par->addAttribute("name","PSPA particles"); |
---|
224 | |
---|
225 | unsigned int k; |
---|
226 | if ( particleRepresentationOk_ ) { |
---|
227 | for ( k = 0 ; k < relativePartic_.size(); k++) |
---|
228 | { |
---|
229 | TRIDVECTOR& position = relativePartic_.at(k).getReferenceToPosition(); |
---|
230 | TRIDVECTOR& betgam = relativePartic_.at(k).getReferenceToBetaGamma(); |
---|
231 | UAPNode* partic = par->addChild("particle"); |
---|
232 | string txt; |
---|
233 | txt = mixedTools::doubleToString(position.getComponent(0)); |
---|
234 | partic->addAttribute("x",txt); |
---|
235 | txt = mixedTools::doubleToString(position.getComponent(1)); |
---|
236 | partic->addAttribute("y",txt); |
---|
237 | txt = mixedTools::doubleToString(position.getComponent(2)); |
---|
238 | partic->addAttribute("cdeltat",txt); |
---|
239 | txt = mixedTools::doubleToString(betgam.getComponent(0)); |
---|
240 | partic->addAttribute("betagamma_x",txt); |
---|
241 | txt = mixedTools::doubleToString(betgam.getComponent(1)); |
---|
242 | partic->addAttribute("betagamma_y",txt); |
---|
243 | txt = mixedTools::doubleToString(betgam.getComponent(2)); |
---|
244 | partic->addAttribute("betagamma_z",txt); |
---|
245 | } |
---|
246 | |
---|
247 | } else { |
---|
248 | cout << " particleBeam::writeToAMLFile : representation 'particules' indisponible" << endl; |
---|
249 | } |
---|
250 | |
---|
251 | cout << "!Create the AML particle file ---------------------------" << endl; |
---|
252 | AMLReader reader; |
---|
253 | reader.AMLRepToAMLFile (uap_root, fileName); |
---|
254 | |
---|
255 | } |
---|
256 | |
---|
257 | bool particleBeam::readFromAMLFile(string fileName) { |
---|
258 | |
---|
259 | cout << "!read the AML particle file ---------------------------" << endl; |
---|
260 | AMLReader reader; |
---|
261 | UAPNode* uap_root = NULL; |
---|
262 | uap_root = reader.AMLFileToAMLRep (fileName); |
---|
263 | if ( !uap_root ) { |
---|
264 | cout << " particleBeam::readFromAMLFile ERREUR lecture fichier particules " << endl; |
---|
265 | return false; |
---|
266 | } |
---|
267 | |
---|
268 | |
---|
269 | NodeVec lesParticules = uap_root->getSubNodesByName(string("particles")); |
---|
270 | |
---|
271 | NodeVec& listPart = (*lesParticules.begin())->getChildren(); |
---|
272 | |
---|
273 | if ( !listPart.size() ) { |
---|
274 | cout << " particleBeam::readFromAMLFile ERREUR lecture fichier : pas de particules ? " << endl; |
---|
275 | return false; |
---|
276 | } |
---|
277 | |
---|
278 | vector<bareParticle> vecteurParticules; |
---|
279 | bareParticle reference; |
---|
280 | for (NodeVecIter iter = listPart.begin(); iter != listPart.end(); iter++) { |
---|
281 | TRIDVECTOR position, betagamma; |
---|
282 | UAPAttribute* att = NULL; |
---|
283 | att = (*iter)->getAttribute("x"); |
---|
284 | if ( att ) att->getDouble(position.component(0)); |
---|
285 | att = (*iter)->getAttribute("y"); |
---|
286 | if ( att ) att->getDouble(position.component(1)); |
---|
287 | att = (*iter)->getAttribute("cdeltat"); |
---|
288 | if ( att ) att->getDouble(position.component(2)); |
---|
289 | |
---|
290 | att = (*iter)->getAttribute("betagamma_x"); |
---|
291 | if ( att ) att->getDouble(betagamma.component(0)); |
---|
292 | att = (*iter)->getAttribute("betagamma_y"); |
---|
293 | if ( att ) att->getDouble(betagamma.component(1)); |
---|
294 | att = (*iter)->getAttribute("betagamma_z"); |
---|
295 | if ( att ) att->getDouble(betagamma.component(2)); |
---|
296 | |
---|
297 | if ( fabs(position.component(0)) < PETIT && fabs(position.component(1)) < PETIT && fabs(position.component(2)) < PETIT ) { |
---|
298 | reference = bareParticle(position, betagamma); |
---|
299 | } |
---|
300 | vecteurParticules.push_back(bareParticle(position, betagamma)); |
---|
301 | } |
---|
302 | vector<double> centroid(6,0.0); |
---|
303 | setWithParticles(centroid, reference, vecteurParticules); |
---|
304 | return true; |
---|
305 | } |
---|
306 | |
---|
307 | bool particleBeam::FileInput( ifstream& ifs) { |
---|
308 | bool test = true; |
---|
309 | string dum1, dum2; |
---|
310 | double dummy; |
---|
311 | if ( !( ifs >> dum1 >> dum2 >> dummy) ) return false; |
---|
312 | |
---|
313 | bareParticle pp; |
---|
314 | while ( pp.FileInput(ifs) ) |
---|
315 | { |
---|
316 | addParticle( pp); |
---|
317 | } |
---|
318 | return test; |
---|
319 | } |
---|
320 | |
---|
321 | void particleBeam::buildMomentRepresentation() { |
---|
322 | // le faisceau cense etre represente en un z donne (reference) sera |
---|
323 | // ici deploye spatialement (faisceau en un temps donne) |
---|
324 | unsigned k,j,m; |
---|
325 | double auxj, auxm; |
---|
326 | if ( !particleRepresentationOk_) |
---|
327 | { |
---|
328 | cerr << " particleBeam::buildMomentRepresentation() vecteur de particules invalide" << endl; |
---|
329 | return; |
---|
330 | } |
---|
331 | |
---|
332 | cout << " buildMomentRepresentation " << endl; |
---|
333 | // printAllXYZ(); |
---|
334 | |
---|
335 | double gref = referenceParticle_.getGamma() - 1.0; |
---|
336 | |
---|
337 | double P_reference_MeV_sur_c = gref*(gref+2); |
---|
338 | |
---|
339 | if ( P_reference_MeV_sur_c > 0.0 ) P_reference_MeV_sur_c = sqrt( P_reference_MeV_sur_c ); |
---|
340 | else P_reference_MeV_sur_c = 0.0; |
---|
341 | |
---|
342 | cout << " gref = " << gref << " P_reference_MeV_sur_c = " << P_reference_MeV_sur_c << endl; |
---|
343 | |
---|
344 | |
---|
345 | // initialisation des moments |
---|
346 | razDesMoments(); |
---|
347 | |
---|
348 | // accumulation |
---|
349 | TRIDVECTOR pos; |
---|
350 | TRIDVECTOR begam; |
---|
351 | double gamma; |
---|
352 | double begamz; |
---|
353 | double g; |
---|
354 | double PMeVsc; |
---|
355 | double del; |
---|
356 | // double xp,yp,dz,cdt; |
---|
357 | vector<double> part(6, 0.0); |
---|
358 | |
---|
359 | vector< vector<double> >& matrice = rij_.getMatrix(); |
---|
360 | |
---|
361 | TRIDVECTOR positionDeployee; |
---|
362 | |
---|
363 | for (k=0; k < relativePartic_.size(); k++) { |
---|
364 | |
---|
365 | positionDeployee = coordonneesDeployees(k); |
---|
366 | gamma = relativePartic_.at(k).getGamma(); |
---|
367 | |
---|
368 | // pos = relativePartic_.at(k).getPosition(); |
---|
369 | // cdt = pos.getComponent(2); |
---|
370 | begam= relativePartic_.at(k).getBetaGamma(); |
---|
371 | begamz = begam.getComponent(2); |
---|
372 | g = gamma -1.0; |
---|
373 | PMeVsc = g*(g+2); |
---|
374 | if ( PMeVsc > 0.) PMeVsc = sqrt( PMeVsc ); |
---|
375 | else PMeVsc = 0.0; |
---|
376 | |
---|
377 | |
---|
378 | |
---|
379 | |
---|
380 | // dz = begamz * cdt / gamma; |
---|
381 | // xp = begam.getComponent(0)/begamz; |
---|
382 | // yp = begam.getComponent(1)/begamz; |
---|
383 | |
---|
384 | part[0] = positionDeployee.getComponent(0); |
---|
385 | part[2] = positionDeployee.getComponent(1); |
---|
386 | part[4] = positionDeployee.getComponent(2); |
---|
387 | |
---|
388 | if ( begamz == 0.0 ) { |
---|
389 | part[1] = 0.0; |
---|
390 | part[3] = 0.0; |
---|
391 | } else { |
---|
392 | part[1] = begam.getComponent(0)/begamz; |
---|
393 | part[3] = begam.getComponent(1)/begamz; |
---|
394 | } |
---|
395 | |
---|
396 | if ( P_reference_MeV_sur_c > 0.0 ) { |
---|
397 | del = 100.0 * ( PMeVsc - P_reference_MeV_sur_c ) / P_reference_MeV_sur_c ; // en % |
---|
398 | part[5] = del; |
---|
399 | } else { |
---|
400 | if ( PMeVsc > 0.0 ) part[5] = 100.; |
---|
401 | else part[5] = 0.0; |
---|
402 | } |
---|
403 | |
---|
404 | for ( j = 0; j < 6; j++) { |
---|
405 | auxj = part.at(j) - centroid_.at(j); |
---|
406 | for (m=0; m <= j; m++) |
---|
407 | { |
---|
408 | auxm = part.at(m) - centroid_.at(m); |
---|
409 | ( matrice.at(j) ).at(m) += auxj*auxm; |
---|
410 | } |
---|
411 | } |
---|
412 | } |
---|
413 | |
---|
414 | |
---|
415 | // moyenne |
---|
416 | double facmoy = 1.0/double( relativePartic_.size() ); |
---|
417 | for ( j = 0; j < 6; j++) { |
---|
418 | double aux = ( matrice.at(j) ).at(j); |
---|
419 | if ( aux > 0.0 ) ( matrice.at(j) ).at(j) = sqrt(aux * facmoy ); |
---|
420 | else ( matrice.at(j) ).at(j) = 0.0; |
---|
421 | } |
---|
422 | |
---|
423 | for ( j = 0; j < 6; j++) { |
---|
424 | auxj = ( matrice.at(j) ).at(j); |
---|
425 | for (m=0; m < j; m++) { |
---|
426 | auxm = ( matrice.at(m) ).at(m); |
---|
427 | if ( auxm != 0.0 && auxj != 0.0 ) ( matrice.at(j) ).at(m) *= facmoy/(auxj * auxm); |
---|
428 | else ( matrice.at(j) ).at(m) = 0.0; |
---|
429 | } |
---|
430 | } |
---|
431 | |
---|
432 | ////////////////// si C21 = 1 , transport plante ! a voir ////////// |
---|
433 | cout << " valeur initiale de C21: " << ( matrice.at(1) ).at(0) << endl; |
---|
434 | if ( ( matrice.at(1) ).at(0) >0.999999 ) { |
---|
435 | ( matrice.at(1) ).at(0) = 0.999999; |
---|
436 | cout << " j'ai fait la correction C21: " << ( matrice.at(1) ).at(0) << endl; |
---|
437 | } |
---|
438 | |
---|
439 | |
---|
440 | // les longueurs sont en cm |
---|
441 | // les angles en radians, on passe en mrad; |
---|
442 | |
---|
443 | double uniteAngle = 1.0e+3; |
---|
444 | ( matrice.at(1) ).at(1) *= uniteAngle; |
---|
445 | ( matrice.at(3) ).at(3) *= uniteAngle; |
---|
446 | P0Transport_ = 1.0e-3*EREST_MeV*P_reference_MeV_sur_c; |
---|
447 | |
---|
448 | // cout << " buildmomentrepresentation impression des moments " << endl; |
---|
449 | // impressionDesMoments(); |
---|
450 | |
---|
451 | momentRepresentationOk_ = true; |
---|
452 | } |
---|
453 | |
---|
454 | void particleBeam::impressionDesMoments() const { |
---|
455 | rij_.impression(); |
---|
456 | } |
---|
457 | |
---|
458 | void particleBeam::razDesMoments() { |
---|
459 | rij_.raz(); |
---|
460 | } |
---|
461 | |
---|
462 | |
---|
463 | // void particleBeam::readTransportMoments(ifstream& inp) { |
---|
464 | // rij_.readFromTransportOutput(inp); |
---|
465 | // } |
---|
466 | |
---|
467 | // void particleBeam::readTransportMoments(stringstream& inp) { |
---|
468 | // rij_.readFromTransportOutput(inp); |
---|
469 | // } |
---|
470 | |
---|
471 | double particleBeam::getXmaxRms() { |
---|
472 | if ( !momentRepresentationOk_ ) buildMomentRepresentation(); |
---|
473 | return ( rij_.getMatrix().at(0) ).at(0); |
---|
474 | // return ( rij_transportMoments_.at(0) ).at(0); |
---|
475 | } |
---|
476 | |
---|
477 | |
---|
478 | void particleBeam::particlesPhaseSpaceData(vector<double>& xcor, vector<double>& ycor, vector<string>& legende, string namex, string namey) { |
---|
479 | |
---|
480 | unsigned indexAbs, indexOrd; |
---|
481 | indexAbs = pspaCoorIndexFromString(namex); |
---|
482 | indexOrd = pspaCoorIndexFromString(namey); |
---|
483 | |
---|
484 | particlesPhaseSpaceComponent(xcor, indexAbs); |
---|
485 | particlesPhaseSpaceComponent(ycor, indexOrd); |
---|
486 | legende.clear(); |
---|
487 | legende.push_back( "phase space " + namex + "," + namey); |
---|
488 | legende.push_back( "particle number : " + mixedTools::intToString(getNbParticles())); |
---|
489 | } |
---|
490 | |
---|
491 | // renvoie (dans le vecteur coord) la liste des coordonnées d'index 'index' : |
---|
492 | // index = 0 , 1, 2 -> x,y,z (en coordonnees deployees) |
---|
493 | // index = 3,4 -> x' = betax/betaz , y' = betay/betaz |
---|
494 | // index = 5 -> Ec : energie cinetique |
---|
495 | void particleBeam::particlesPhaseSpaceComponent(vector<double>& coord, unsigned index) |
---|
496 | { |
---|
497 | |
---|
498 | if ( !particleRepresentationOk_ ) return; |
---|
499 | |
---|
500 | coord.clear(); |
---|
501 | coord.resize(relativePartic_.size(), 0.0 ); |
---|
502 | // cout << " particleBeam::particlesPhaseSpaceComponent index = " << index << endl; |
---|
503 | |
---|
504 | if ( index <= 2 ) { |
---|
505 | |
---|
506 | for (unsigned i = 0; i < relativePartic_.size(); ++i) { |
---|
507 | |
---|
508 | coord.at(i) = 10.*coordonneesDeployees(i).getComponent(index); |
---|
509 | // coord.at(i) = 10.*relativePartic_.at(i).getPosition().getComponent(index); // en mm |
---|
510 | } |
---|
511 | return; |
---|
512 | } |
---|
513 | |
---|
514 | if ( index > 2 && index < 5 ) { |
---|
515 | // cout << " particleBeam::particlesPhaseSpaceComponent traitement vitesses " << endl; |
---|
516 | for (unsigned i = 0; i < relativePartic_.size(); ++i) { |
---|
517 | double begamz = relativePartic_.at(i).getBetaGamma().getComponent(2); |
---|
518 | if ( begamz != 0.0) { |
---|
519 | coord.at(i) = 1000.*relativePartic_.at(i).getBetaGamma().getComponent(index - 3)/begamz; // milliradians |
---|
520 | } else { |
---|
521 | coord.at(i) = 0.0; |
---|
522 | } |
---|
523 | } |
---|
524 | // cout << " particleBeam::particlesPhaseSpaceComponent traitement vitesses TERMINE " << endl; |
---|
525 | return; |
---|
526 | } |
---|
527 | |
---|
528 | if ( index == 5 ) { |
---|
529 | double gamma0 = referenceParticle_.getGamma(); |
---|
530 | if ( gamma0 == 0.0 ) return; |
---|
531 | for (unsigned i = 0; i < relativePartic_.size(); ++i) { |
---|
532 | coord.at(i) = 100.*(relativePartic_.at(i).getGamma() - gamma0)/gamma0; // en % |
---|
533 | } |
---|
534 | return; |
---|
535 | } |
---|
536 | } |
---|
537 | |
---|
538 | unsigned particleBeam::indexFromPspaToTransport(unsigned index) const { |
---|
539 | cout << " indexFromPspaToTransport entree : " << index << endl; |
---|
540 | switch ( index ) { |
---|
541 | case 0 : return 0; // x |
---|
542 | case 1 : return 2; // y |
---|
543 | case 2 : return 4; // z -> l |
---|
544 | case 3 : return 1; // xp |
---|
545 | case 4 : return 3; // yp |
---|
546 | case 5 : return 5; // de/E |
---|
547 | default : { |
---|
548 | cout << " particleBeam::indexFromPspaToTransport : coordinate index ERROR inital index : "<< index << endl; |
---|
549 | return 99; |
---|
550 | } |
---|
551 | } |
---|
552 | } |
---|
553 | |
---|
554 | unsigned particleBeam::pspaCoorIndexFromString(string nameCoor) const { |
---|
555 | if ( nameCoor == "x" ) { |
---|
556 | return 0; |
---|
557 | } else if ( nameCoor == "y" ) { |
---|
558 | return 1; |
---|
559 | } else if ( nameCoor == "dz" ) { |
---|
560 | return 2; |
---|
561 | } else if ( nameCoor == "xp" ) { |
---|
562 | return 3; |
---|
563 | } else if ( nameCoor == "yp" ) { |
---|
564 | return 4; |
---|
565 | } else if ( nameCoor == "dE/E" ) { |
---|
566 | return 5; |
---|
567 | } else { |
---|
568 | return 99; |
---|
569 | } |
---|
570 | } |
---|
571 | |
---|
572 | unsigned particleBeam::transportCoorIndexFromString(string nameCoor) const { |
---|
573 | if ( nameCoor == "x" ) { |
---|
574 | return 0; |
---|
575 | } else if ( nameCoor == "y" ) { |
---|
576 | return 2; |
---|
577 | } else if ( nameCoor == "dz" ) { |
---|
578 | return 4; |
---|
579 | } else if ( nameCoor == "xp" ) { |
---|
580 | return 1; |
---|
581 | } else if ( nameCoor == "yp" ) { |
---|
582 | return 3; |
---|
583 | } else if ( nameCoor == "dE/E" ) { |
---|
584 | return 5; |
---|
585 | } else { |
---|
586 | return 99; |
---|
587 | } |
---|
588 | } |
---|
589 | |
---|
590 | //void particleBeam::donneesDessinEllipse(vector<double>& xcor, vector<double>& ycor, vector<string>& legende, unsigned indexAbs, unsigned indexOrd) { |
---|
591 | |
---|
592 | void particleBeam::donneesDessinEllipse(vector<double>& xcor, vector<double>& ycor, vector<string>& legende, string namex, string namey) { |
---|
593 | int k; |
---|
594 | double x,y; |
---|
595 | if ( !momentRepresentationOk_ ) buildMomentRepresentation(); |
---|
596 | |
---|
597 | // if ( !momentRepresentationOk_ ) return; |
---|
598 | |
---|
599 | unsigned indexAbs, indexOrd; |
---|
600 | |
---|
601 | |
---|
602 | // index TRANSPORT |
---|
603 | indexAbs = transportCoorIndexFromString(namex); |
---|
604 | indexOrd = transportCoorIndexFromString(namey); |
---|
605 | cout << " namex= " << namex << " indexAbs= " << indexAbs << " namey= " << namey << " indexOrd= " << indexOrd << endl; |
---|
606 | if ( indexAbs > 5 || indexOrd > 5 ) return; |
---|
607 | |
---|
608 | xcor.clear(); |
---|
609 | ycor.clear(); |
---|
610 | |
---|
611 | |
---|
612 | |
---|
613 | double dimensionalFactorX, dimensionalFactorY; // to mm, if necessary |
---|
614 | dimensionalFactorX = dimensionalFactorFromTransportToGraphics(indexAbs); |
---|
615 | dimensionalFactorY = dimensionalFactorFromTransportToGraphics(indexOrd); |
---|
616 | |
---|
617 | |
---|
618 | |
---|
619 | legende.clear(); |
---|
620 | // if ( indexAbs == 0 && indexOrd == 1 ) { |
---|
621 | string namx = transportVariableName(indexAbs); |
---|
622 | string namy = transportVariableName(indexOrd); |
---|
623 | double em = getUnnormalizedTranspPhaseSpaceArea(indexAbs,indexOrd) ; |
---|
624 | string emitt = mixedTools::doubleToString(getUnnormalizedTranspPhaseSpaceArea(indexAbs,indexOrd)); |
---|
625 | string xmax = namx + "max= "; |
---|
626 | string valXmax = mixedTools::doubleToString(dimensionalFactorX*getSigmaTransportij(indexAbs,indexAbs)); |
---|
627 | string ymax = namy + "max= "; |
---|
628 | string valYmax = mixedTools::doubleToString(dimensionalFactorY*getSigmaTransportij(indexOrd,indexOrd)); // mm |
---|
629 | string correl = " correlation "; |
---|
630 | string valCorrel = mixedTools::doubleToString(getSigmaTransportij(1,0)); |
---|
631 | string xunit = graphicTransportUnitName(indexAbs); |
---|
632 | string yunit = graphicTransportUnitName(indexOrd); |
---|
633 | legende.push_back( "emittance" + namx + "," + namy + ": " + emitt + " pi." + xunit + "." + yunit); |
---|
634 | legende.push_back( xmax + valXmax + xunit); |
---|
635 | legende.push_back( ymax + valYmax + yunit); |
---|
636 | // } else { |
---|
637 | // legende.push_back(" text of legend not yet programmed "); |
---|
638 | // } |
---|
639 | |
---|
640 | cout << " index x" << indexAbs << " index y " << indexOrd << endl; |
---|
641 | double xm = dimensionalFactorX*( rij_.getMatrix().at(indexAbs) ).at(indexAbs); |
---|
642 | double ym = dimensionalFactorY*( rij_.getMatrix().at(indexOrd) ).at(indexOrd); |
---|
643 | double r; |
---|
644 | if ( indexOrd > indexAbs ) { |
---|
645 | r = ( rij_.getMatrix().at(indexOrd) ).at(indexAbs); |
---|
646 | } else { |
---|
647 | r = ( rij_.getMatrix().at(indexAbs) ).at(indexOrd); |
---|
648 | } |
---|
649 | |
---|
650 | cout << " racs11= " << xm << " racs22= " << ym << " r12= " << r << endl; |
---|
651 | |
---|
652 | |
---|
653 | int nbintv = 50; |
---|
654 | if ( xm == 0.0 ) return; |
---|
655 | double pas = 2.0 * xm / nbintv; |
---|
656 | |
---|
657 | // cout << " r= " << r << endl; |
---|
658 | double rac = (1 - r*r); |
---|
659 | if ( rac > 0.0 ) |
---|
660 | { |
---|
661 | cout << " cas rac > " << endl; |
---|
662 | rac = sqrt(1 - r*r); |
---|
663 | double alpha = -r / rac; |
---|
664 | double beta = xm / ( ym * rac); |
---|
665 | // double gamma = ym / ( xm * rac ); |
---|
666 | double epsil = xm * ym * rac; |
---|
667 | double fac1 = -1.0 / ( beta * beta); |
---|
668 | double fac2 = epsil/beta; |
---|
669 | double fac3 = -alpha/beta; |
---|
670 | double aux; |
---|
671 | for ( k=0; k < nbintv; k++) |
---|
672 | { |
---|
673 | x = -xm + k*pas; |
---|
674 | aux = fac1 * x * x + fac2; |
---|
675 | // cout << " aux2= " << aux << endl; |
---|
676 | if ( aux <= 0.0 ) |
---|
677 | { |
---|
678 | aux = 0.0; |
---|
679 | } |
---|
680 | else aux = sqrt(aux); |
---|
681 | |
---|
682 | // y = fac3*x; |
---|
683 | y = fac3*x + aux; |
---|
684 | xcor.push_back(x); |
---|
685 | ycor.push_back(y); |
---|
686 | } |
---|
687 | |
---|
688 | for ( k=0; k <= nbintv; k++) |
---|
689 | { |
---|
690 | x = xm - k*pas; |
---|
691 | aux = fac1 * x * x + fac2; |
---|
692 | if ( aux <= 0.0 ) |
---|
693 | { |
---|
694 | aux = 0.0; |
---|
695 | } |
---|
696 | else aux = sqrt(aux); |
---|
697 | // y = fac3*x; |
---|
698 | y = fac3*x - aux; |
---|
699 | xcor.push_back(x); |
---|
700 | ycor.push_back(y); |
---|
701 | } |
---|
702 | } |
---|
703 | else |
---|
704 | // cas degenere |
---|
705 | { |
---|
706 | cout << " cas degenere " << endl; |
---|
707 | double fac = ym/xm; |
---|
708 | for ( k=0; k < nbintv; k++) |
---|
709 | { |
---|
710 | x = -xm + k*pas; |
---|
711 | y = fac*x; |
---|
712 | xcor.push_back(x); |
---|
713 | ycor.push_back(y); |
---|
714 | } |
---|
715 | |
---|
716 | } |
---|
717 | } |
---|
718 | |
---|
719 | void particleBeam::histogramme(unsigned int iabs,vector<double>& xcor,vector<int>& hist, vector<string>& legende) |
---|
720 | { |
---|
721 | double out[3]; |
---|
722 | // out[0]= nbre de particules, out[1]= moyenne, out[2]= ecart-type |
---|
723 | vector<double> vshf; |
---|
724 | particlesPhaseSpaceComponent(vshf,iabs); |
---|
725 | histogramInitialize(iabs,vshf,out); |
---|
726 | histogramPacking(out[2],vshf,xcor,hist); |
---|
727 | |
---|
728 | if(iabs == 5) { |
---|
729 | out[1] *= EREST_MeV; // moyenne en MeV |
---|
730 | out[2] *= EREST_keV; // ecart-type en KeV |
---|
731 | } |
---|
732 | // legendes |
---|
733 | string unites[2]; |
---|
734 | if(iabs == 0 || iabs == 1 || iabs == 2) { |
---|
735 | unites[0]= unites[1]= " mm"; |
---|
736 | } |
---|
737 | if(iabs == 3 || iabs == 4) { |
---|
738 | unites[0]= unites[1]= " mrad"; |
---|
739 | } |
---|
740 | if(iabs == 5) { |
---|
741 | unites[0]= " MeV"; unites[1]= " KeV"; |
---|
742 | } |
---|
743 | |
---|
744 | legende.clear(); |
---|
745 | legende.push_back(" entries : "+ mixedTools::intToString((int)out[0]) ); |
---|
746 | legende.push_back(" mean : "+ mixedTools::doubleToString(out[1])+unites[0]); |
---|
747 | legende.push_back(" sigma rms : "+ mixedTools::doubleToString(out[2])+unites[1]); |
---|
748 | } |
---|
749 | |
---|
750 | void particleBeam::histogramInitialize(unsigned int index,vector<double>& vshf,double out[3]) |
---|
751 | { |
---|
752 | double vmin= GRAND; |
---|
753 | double vmax= -vmin; |
---|
754 | double vmoy= 0.0; |
---|
755 | double ecatyp= 0.0; |
---|
756 | |
---|
757 | for (unsigned int k = 0; k < relativePartic_.size(); k++) { |
---|
758 | if (vshf[k] < vmin) vmin = vshf[k]; |
---|
759 | else if (vshf[k] > vmax) vmax = vshf[k]; |
---|
760 | vmoy += vshf[k]; |
---|
761 | ecatyp += vshf[k]*vshf[k]; |
---|
762 | } |
---|
763 | |
---|
764 | double sum= (float)relativePartic_.size(); |
---|
765 | out[0]= sum; |
---|
766 | vmoy /= sum; |
---|
767 | out[1]= vmoy; |
---|
768 | ecatyp /= sum; |
---|
769 | ecatyp = sqrt(abs(ecatyp-vmoy*vmoy)); |
---|
770 | out[2]= ecatyp; |
---|
771 | |
---|
772 | if(index == 0) { |
---|
773 | cout << "position x -moyenne " << vmoy << " cm " << "-mini " << vmin << " cm " << "-maxi " << vmax << " cm " << "ecart type " << ecatyp << " cm" << endl; |
---|
774 | } |
---|
775 | if(index == 1) { |
---|
776 | cout << "position y -moyenne " << vmoy << " cm " << "-mini " << vmin << " cm " << "-maxi " << vmax << " cm " << "ecart type " << ecatyp << " cm" << endl; |
---|
777 | } |
---|
778 | if(index == 2) { |
---|
779 | cout << "position z -moyenne " << vmoy << " cm " << "-mini " << vmin << " cm " << "-maxi " << vmax << " cm " << "ecart type " << ecatyp << " cm" << endl; |
---|
780 | } |
---|
781 | if(index == 3) { |
---|
782 | cout << "divergence xp -moyenne " << vmoy << " mrad " << "-mini " << vmin << " mrad " << "-maxi " << vmax << " mrad " << "ecart type " << ecatyp << " mrad" << endl; |
---|
783 | } |
---|
784 | if(index == 4) { |
---|
785 | cout << "divergence yp -moyenne " << vmoy << " mrad " << "-mini " << vmin << " mrad " << "-maxi " << vmax << " mrad " << "ecart type " << ecatyp << " mrad" << endl; |
---|
786 | } |
---|
787 | if(index == 5) { |
---|
788 | double gmin = vmin-1.0; |
---|
789 | double gmax = vmax-1.0; |
---|
790 | cout << "energie cinetique -moyenne " << vmoy*EREST_MeV << " Mev " << "-mini " << gmin*EREST_MeV << " Mev " << "-maxi " << gmax*EREST_MeV << " Mev " << "ecart type " << ecatyp*EREST_MeV << " Kev" << endl; |
---|
791 | } |
---|
792 | |
---|
793 | for (unsigned int k = 0; k < relativePartic_.size(); k++) { |
---|
794 | vshf[k] -= vmoy; |
---|
795 | } |
---|
796 | } |
---|
797 | |
---|
798 | void particleBeam::histogramPacking(double ecatyp,vector<double>vshf,vector<double>&xcor,vector<int>& hist) |
---|
799 | { |
---|
800 | // demi fenetre hfene= max(5.*ecatyp-vmoy,vmoy-5.*ecatyp); |
---|
801 | double hfene= 5.*ecatyp; |
---|
802 | // et pas de l'histogramme |
---|
803 | double hpas = hfene/25.; |
---|
804 | |
---|
805 | cout << "demi fenetre " << hfene << ", hpas= " << hpas << endl; |
---|
806 | |
---|
807 | double vmin = -hfene; |
---|
808 | double dfen = 2.*hfene; |
---|
809 | int ihist = dfen/hpas; |
---|
810 | double phist = ihist*hpas; |
---|
811 | double dpas = hpas-(dfen-phist); |
---|
812 | if(dpas <= hpas*1.e-03) { |
---|
813 | ihist++; |
---|
814 | phist= ihist*hpas; |
---|
815 | } |
---|
816 | double vmax= vmin+hpas*ihist; |
---|
817 | |
---|
818 | cout << "xAxisNumberOfBins= " << ihist <<", xAxisMinimum= " << vmin << ", xAxisMaximum= " << vmax << ", NParticules= " << vshf.size() << ", phist " << phist << endl; |
---|
819 | |
---|
820 | if(!xcor.empty()) xcor.clear(); |
---|
821 | xcor.resize(ihist+1); |
---|
822 | for (int i = 0; i <= ihist; ++i) { |
---|
823 | xcor[i] = vmin+i*hpas; |
---|
824 | } |
---|
825 | |
---|
826 | ///////////////////////////////////// |
---|
827 | |
---|
828 | if(!hist.empty()) hist.clear(); |
---|
829 | hist.resize(ihist,0); |
---|
830 | for (unsigned int i = 0; i < vshf.size(); ++i) { |
---|
831 | double var= vshf[i]-vmin; |
---|
832 | if(var < 0 ) { |
---|
833 | cout<<"lesser that the minimum "<<var<<", ("<< i<<")"<< endl; |
---|
834 | hist.at(0)++; |
---|
835 | } else if(var >= phist) { |
---|
836 | cout<<"greater that the maximum "<<var<<", ("<< i<<")"<< endl; |
---|
837 | hist.at(ihist-1)++; |
---|
838 | } else { |
---|
839 | int kk= (int)floor(var/hpas); |
---|
840 | hist.at(kk)++; |
---|
841 | } |
---|
842 | } |
---|
843 | } |
---|
844 | |
---|
845 | // coordonnees d'une particule dans le faisceau deploye ( passage |
---|
846 | // d'une representation z=cte a une representation t=cte) |
---|
847 | TRIDVECTOR particleBeam::coordonneesDeployees(unsigned particleIndex, double cdtShift) { |
---|
848 | double gamma = relativePartic_.at(particleIndex).getGamma(); |
---|
849 | // TRIDVECTOR pos = relativePartic_.at(particleIndex).getPosition(); |
---|
850 | // cout << " ------------------ particleBeam::coordonneesDeployees ----- " << endl; |
---|
851 | // cout << " particleBeam::coordonneesDeployees " << relativePartic_.at(particleIndex).FileOutputFlow() << endl; |
---|
852 | // cout << " par reference x= " << relativePartic_.at(particleIndex).getReferenceToPosition().getComponent(0); |
---|
853 | // cout << " ----------------------------------------------------- " << endl; |
---|
854 | |
---|
855 | |
---|
856 | |
---|
857 | double xx = relativePartic_.at(particleIndex).getReferenceToPosition().getComponent(0); |
---|
858 | double yy = relativePartic_.at(particleIndex).getReferenceToPosition().getComponent(1); |
---|
859 | double cdt = relativePartic_.at(particleIndex).getReferenceToPosition().getComponent(2); |
---|
860 | cdt += cdtShift; |
---|
861 | // TRIDVECTOR begam= goodPartic_.at(particleIndex).getBetaGamma(); |
---|
862 | double begamz = relativePartic_.at(particleIndex).getReferenceToBetaGamma().getComponent(2); |
---|
863 | double fac = cdt / gamma; |
---|
864 | // double dz = begamz * fac; |
---|
865 | xx += relativePartic_.at(particleIndex).getReferenceToBetaGamma().getComponent(0) * fac; |
---|
866 | yy += relativePartic_.at(particleIndex).getReferenceToBetaGamma().getComponent(1) * fac; |
---|
867 | return TRIDVECTOR(xx, yy, begamz * fac); |
---|
868 | } |
---|