1 | #include "particleBeam.h" |
---|
2 | #include "mathematicalConstants.h" |
---|
3 | #include "PhysicalConstants.h" |
---|
4 | #include "mathematicalTools.h" |
---|
5 | //#include <string> |
---|
6 | #include <stdio.h> |
---|
7 | #include <algorithm> |
---|
8 | #include <sstream> |
---|
9 | //#include "environmentVariables.h" |
---|
10 | #include <Wt/WApplication> |
---|
11 | |
---|
12 | using namespace std; |
---|
13 | |
---|
14 | particleBeam::particleBeam() { |
---|
15 | // rij_transportMoments_.resize(6); |
---|
16 | // unsigned dim=0; |
---|
17 | // unsigned k; |
---|
18 | // for ( k=0; k < 6; k++){ |
---|
19 | // rij_transportMoments_.at(k).resize(++dim); |
---|
20 | // } |
---|
21 | P0Transport_ = 0.0; |
---|
22 | particleRepresentationOk_ = false; |
---|
23 | momentRepresentationOk_ = false; |
---|
24 | } |
---|
25 | |
---|
26 | void particleBeam::clear() { |
---|
27 | goodPartic_.clear(); |
---|
28 | rij_.raz(); |
---|
29 | P0Transport_ = 0.0; |
---|
30 | particleRepresentationOk_ = false; |
---|
31 | momentRepresentationOk_ = false; |
---|
32 | } |
---|
33 | |
---|
34 | int particleBeam::getNbParticles() const { |
---|
35 | return goodPartic_.size(); |
---|
36 | } |
---|
37 | |
---|
38 | const beam2Moments& particleBeam::getTransportMoments() const { |
---|
39 | return rij_; |
---|
40 | } |
---|
41 | |
---|
42 | double particleBeam::getSigmaTransportij(unsigned i, unsigned j) { |
---|
43 | if ( i < 1 || i > 6 || j < 1 || j > 6 ) { |
---|
44 | cerr << " particleBeam::getSigmaTransportij() indices out of range " << endl; |
---|
45 | return 0.0; |
---|
46 | } |
---|
47 | if ( !momentRepresentationOk_ ) { |
---|
48 | cerr << " particleBeam::getSigmaTransportij() beam is not in moment representation " << endl; |
---|
49 | return 0.0; |
---|
50 | } |
---|
51 | |
---|
52 | i--; |
---|
53 | j--; |
---|
54 | if ( j > i ) { |
---|
55 | unsigned aux = i; |
---|
56 | i = j; |
---|
57 | j = aux; |
---|
58 | } |
---|
59 | return ( rij_.getMatrix().at(i) ).at(j); |
---|
60 | } |
---|
61 | |
---|
62 | |
---|
63 | double particleBeam::getUnnormalizedEmittanceX() { |
---|
64 | double r = getSigmaTransportij(2,1); |
---|
65 | double rac = (1 - r*r); |
---|
66 | if ( rac <= 0.0 ) { |
---|
67 | return 0.0; |
---|
68 | } |
---|
69 | rac = sqrt(1 - r*r); |
---|
70 | return 10.*getSigmaTransportij(1,1) * getSigmaTransportij(2,2) * rac; // en pi.mm.mrad |
---|
71 | } |
---|
72 | |
---|
73 | double particleBeam::getP0Transport() const { |
---|
74 | return P0Transport_; |
---|
75 | } |
---|
76 | |
---|
77 | void particleBeam::set2Moments(beam2Moments& moments) { |
---|
78 | rij_ = moments; |
---|
79 | momentRepresentationOk_ = true; |
---|
80 | } |
---|
81 | |
---|
82 | void particleBeam::setWithParticles(vector<double>& centroid, bareParticle& referencePart, vector<bareParticle>& particles) { |
---|
83 | cout << " particleBeam::setWithParticles taille vect. part. " << particles.size() << endl; |
---|
84 | centroid_.clear(); |
---|
85 | centroid_ = centroid; |
---|
86 | referenceParticle_ = referencePart; |
---|
87 | goodPartic_.clear(); |
---|
88 | goodPartic_ = particles; |
---|
89 | cout << " particleBeam::setWithParticles taille vect. part. ENREGISTRE " << goodPartic_.size() << endl; |
---|
90 | // printAllXYZ(); |
---|
91 | particleRepresentationOk_ = true; |
---|
92 | } |
---|
93 | |
---|
94 | |
---|
95 | bool particleBeam::particleRepresentationOk() const { |
---|
96 | return particleRepresentationOk_; |
---|
97 | } |
---|
98 | bool particleBeam::momentRepresentationOk() const { |
---|
99 | return momentRepresentationOk_; |
---|
100 | } |
---|
101 | |
---|
102 | void particleBeam::addParticle( bareParticle p) |
---|
103 | { |
---|
104 | goodPartic_.push_back(p); |
---|
105 | } |
---|
106 | |
---|
107 | const vector<bareParticle>& particleBeam::getParticleVector() const |
---|
108 | { |
---|
109 | return goodPartic_; |
---|
110 | } |
---|
111 | vector<bareParticle>& particleBeam::getParticleVector() |
---|
112 | { |
---|
113 | return goodPartic_; |
---|
114 | } |
---|
115 | |
---|
116 | |
---|
117 | |
---|
118 | |
---|
119 | void particleBeam::getVariance(double& varx, double& vary, double& varz) const { |
---|
120 | unsigned int k; |
---|
121 | double x,y,z; |
---|
122 | double xav = 0.; |
---|
123 | double yav = 0.; |
---|
124 | double zav = 0.; |
---|
125 | double xavsq = 0.; |
---|
126 | double yavsq = 0.; |
---|
127 | double zavsq = 0.; |
---|
128 | |
---|
129 | TRIDVECTOR pos; |
---|
130 | |
---|
131 | |
---|
132 | for ( k = 0 ; k < goodPartic_.size(); k++) { |
---|
133 | pos = goodPartic_.at(k).getPosition(); |
---|
134 | pos.getComponents(x,y,z); |
---|
135 | // partic_[k].getXYZ(x,y,z); |
---|
136 | xav += x; |
---|
137 | xavsq += x*x; |
---|
138 | yav += y; |
---|
139 | yavsq += y*y; |
---|
140 | zav += z; |
---|
141 | zavsq += z*z; |
---|
142 | } |
---|
143 | |
---|
144 | double aginv = double (goodPartic_.size()); |
---|
145 | aginv = 1.0/aginv; |
---|
146 | |
---|
147 | varx = aginv * ( xavsq - xav*xav*aginv ); |
---|
148 | vary = aginv * ( yavsq - yav*yav*aginv ); |
---|
149 | varz = aginv * ( zavsq - zav*zav*aginv ); |
---|
150 | } |
---|
151 | |
---|
152 | |
---|
153 | void particleBeam::printAllXYZ() const { |
---|
154 | cout << " dump du faisceau : " << endl; |
---|
155 | cout << goodPartic_.size() << " particules " << endl; |
---|
156 | unsigned int k; |
---|
157 | for ( k = 0 ; k < goodPartic_.size(); k++) |
---|
158 | { |
---|
159 | double xx,yy,zz; |
---|
160 | goodPartic_.at(k).getPosition().getComponents(xx,yy,zz); |
---|
161 | double betgamx, betgamy, betgamz; |
---|
162 | goodPartic_.at(k).getBetaGamma().getComponents(betgamx, betgamy, betgamz); |
---|
163 | cout << " part. numero " << k << " x= " << xx << " y= " << yy << " z= " << zz << " betgamx= " << betgamx << " betgamy= " << betgamy << " betgamz= " << betgamz << endl; |
---|
164 | } |
---|
165 | } |
---|
166 | |
---|
167 | |
---|
168 | |
---|
169 | void particleBeam::Zrange(double& zmin, double& zmax) const { |
---|
170 | double z; |
---|
171 | zmin = GRAND; |
---|
172 | zmax = -zmin; |
---|
173 | |
---|
174 | unsigned int k; |
---|
175 | for ( k = 0 ; k < goodPartic_.size(); k++) |
---|
176 | { |
---|
177 | z = goodPartic_.at(k).getZ(); |
---|
178 | if ( z < zmin ) zmin = z; |
---|
179 | else if ( z > zmax) zmax = z; |
---|
180 | } |
---|
181 | } |
---|
182 | |
---|
183 | |
---|
184 | |
---|
185 | string particleBeam::FileOutputFlow() const { |
---|
186 | ostringstream sortie; |
---|
187 | unsigned int k; |
---|
188 | for ( k = 0 ; k < goodPartic_.size(); k++) |
---|
189 | { |
---|
190 | sortie << goodPartic_.at(k).FileOutputFlow() << endl; |
---|
191 | } |
---|
192 | sortie << endl; |
---|
193 | return sortie.str(); |
---|
194 | } |
---|
195 | |
---|
196 | bool particleBeam::FileInput( ifstream& ifs) { |
---|
197 | bool test = true; |
---|
198 | string dum1, dum2; |
---|
199 | double dummy; |
---|
200 | if ( !( ifs >> dum1 >> dum2 >> dummy) ) return false; |
---|
201 | |
---|
202 | bareParticle pp; |
---|
203 | while ( pp.FileInput(ifs) ) |
---|
204 | { |
---|
205 | addParticle( pp); |
---|
206 | } |
---|
207 | return test; |
---|
208 | } |
---|
209 | |
---|
210 | void particleBeam::buildMomentRepresentation() { |
---|
211 | |
---|
212 | unsigned k,j,m; |
---|
213 | double auxj, auxm; |
---|
214 | if ( !particleRepresentationOk_) |
---|
215 | { |
---|
216 | cerr << " particleBeam::buildMomentRepresentation() vecteur de particules invalide" << endl; |
---|
217 | return; |
---|
218 | } |
---|
219 | |
---|
220 | cout << " buildMomentRepresentation " << endl; |
---|
221 | // printAllXYZ(); |
---|
222 | |
---|
223 | double gref = referenceParticle_.getGamma() - 1.0; |
---|
224 | double P_reference_MeV_sur_c = sqrt( gref*(gref+2) ); |
---|
225 | |
---|
226 | cout << " gref = " << gref << " P_reference_MeV_sur_c = " << P_reference_MeV_sur_c << endl; |
---|
227 | |
---|
228 | |
---|
229 | // initialisation des moments |
---|
230 | razDesMoments(); |
---|
231 | |
---|
232 | // accumulation |
---|
233 | TRIDVECTOR pos; |
---|
234 | TRIDVECTOR begam; |
---|
235 | double gamma; |
---|
236 | double begamz; |
---|
237 | double g; |
---|
238 | double PMeVsc; |
---|
239 | double del; |
---|
240 | vector<double> part(6, 0.0); |
---|
241 | |
---|
242 | vector< vector<double> >& matrice = rij_.getMatrix(); |
---|
243 | |
---|
244 | |
---|
245 | for (k=0; k < goodPartic_.size(); k++) { |
---|
246 | gamma = goodPartic_.at(k).getGamma(); |
---|
247 | pos = goodPartic_.at(k).getPosition(); |
---|
248 | begam= goodPartic_.at(k).getBetaGamma(); |
---|
249 | begamz = begam.getComponent(2); |
---|
250 | g = gamma -1.0; |
---|
251 | PMeVsc = sqrt( g*(g+2) ); |
---|
252 | del = 100.0 * ( PMeVsc - P_reference_MeV_sur_c ) / P_reference_MeV_sur_c ; // en % |
---|
253 | |
---|
254 | part[0] = pos.getComponent(0); |
---|
255 | part[1] = begam.getComponent(0)/begamz; |
---|
256 | part[2] = pos.getComponent(1); |
---|
257 | part[3] = begam.getComponent(1)/begamz; |
---|
258 | part[4] = pos.getComponent(2); |
---|
259 | part[5] = del; |
---|
260 | |
---|
261 | for ( j = 0; j < 6; j++) { |
---|
262 | auxj = part.at(j) - centroid_.at(j); |
---|
263 | for (m=0; m <= j; m++) |
---|
264 | { |
---|
265 | auxm = part.at(m) - centroid_.at(m); |
---|
266 | |
---|
267 | ( matrice.at(j) ).at(m) += auxj*auxm; |
---|
268 | // ( rij_transportMoments_.at(j) ).at(m) += auxj*auxm; |
---|
269 | |
---|
270 | |
---|
271 | // cout << " j= " << j << " m= " << m << " rjm= " << ( rij_transportMoments_.at(j) ).at(m) << endl; |
---|
272 | } |
---|
273 | } |
---|
274 | } |
---|
275 | |
---|
276 | |
---|
277 | // moyenne |
---|
278 | double facmoy = 1.0/double( goodPartic_.size() ); |
---|
279 | for ( j = 0; j < 6; j++) { |
---|
280 | ( matrice.at(j) ).at(j) = sqrt(( matrice.at(j) ).at(j) * facmoy ); |
---|
281 | } |
---|
282 | |
---|
283 | for ( j = 0; j < 6; j++) { |
---|
284 | auxj = ( matrice.at(j) ).at(j); |
---|
285 | for (m=0; m < j; m++) { |
---|
286 | auxm = ( matrice.at(m) ).at(m); |
---|
287 | ( matrice.at(j) ).at(m) *= facmoy/(auxj * auxm); |
---|
288 | } |
---|
289 | } |
---|
290 | |
---|
291 | ////////////////// si C21 = 1 , transport plante ! a voir ////////// |
---|
292 | cout << " valeur initiale de C21: " << ( matrice.at(1) ).at(0) << endl; |
---|
293 | if ( ( matrice.at(1) ).at(0) >0.999999 ) { |
---|
294 | ( matrice.at(1) ).at(0) = 0.999999; |
---|
295 | cout << " j'ai fait la correction C21: " << ( matrice.at(1) ).at(0) << endl; |
---|
296 | } |
---|
297 | |
---|
298 | |
---|
299 | // les longueurs sont en cm |
---|
300 | // les angles en radians, on passe en mrad; |
---|
301 | |
---|
302 | double uniteAngle = 1.0e+3; |
---|
303 | ( matrice.at(1) ).at(1) *= uniteAngle; |
---|
304 | ( matrice.at(3) ).at(3) *= uniteAngle; |
---|
305 | P0Transport_ = 1.0e-3*ERESTMeV*P_reference_MeV_sur_c; |
---|
306 | |
---|
307 | // cout << " buildmomentrepresentation impression des moments " << endl; |
---|
308 | // impressionDesMoments(); |
---|
309 | |
---|
310 | momentRepresentationOk_ = true; |
---|
311 | } |
---|
312 | |
---|
313 | void particleBeam::impressionDesMoments() const { |
---|
314 | rij_.impression(); |
---|
315 | } |
---|
316 | |
---|
317 | void particleBeam::razDesMoments() { |
---|
318 | rij_.raz(); |
---|
319 | } |
---|
320 | |
---|
321 | |
---|
322 | // void particleBeam::readTransportMoments(ifstream& inp) { |
---|
323 | // rij_.readFromTransportOutput(inp); |
---|
324 | // } |
---|
325 | |
---|
326 | // void particleBeam::readTransportMoments(stringstream& inp) { |
---|
327 | // rij_.readFromTransportOutput(inp); |
---|
328 | // } |
---|
329 | |
---|
330 | double particleBeam::getXmaxRms() { |
---|
331 | if ( !momentRepresentationOk_ ) buildMomentRepresentation(); |
---|
332 | return ( rij_.getMatrix().at(0) ).at(0); |
---|
333 | // return ( rij_transportMoments_.at(0) ).at(0); |
---|
334 | } |
---|
335 | |
---|
336 | void particleBeam::donneesDessinEllipseXxp(vector<double>& xcor, vector<double>& ycor) { |
---|
337 | int k; |
---|
338 | double x,y; |
---|
339 | |
---|
340 | if ( !momentRepresentationOk_ ) return; |
---|
341 | |
---|
342 | xcor.clear(); |
---|
343 | ycor.clear(); |
---|
344 | |
---|
345 | double xm = ( rij_.getMatrix().at(0) ).at(0); |
---|
346 | double ym = ( rij_.getMatrix().at(1) ).at(1); |
---|
347 | double r = ( rij_.getMatrix().at(1) ).at(0); |
---|
348 | |
---|
349 | cout << " racs11= " << xm << " racs22= " << ym << " r12= " << r << endl; |
---|
350 | |
---|
351 | |
---|
352 | int nbintv = 50; |
---|
353 | if ( xm == 0.0 ) return; |
---|
354 | double pas = 2.0 * xm / nbintv; |
---|
355 | |
---|
356 | // cout << " r= " << r << endl; |
---|
357 | double rac = (1 - r*r); |
---|
358 | if ( rac > 0.0 ) |
---|
359 | { |
---|
360 | cout << " cas rac > " << endl; |
---|
361 | rac = sqrt(1 - r*r); |
---|
362 | double alpha = -r / rac; |
---|
363 | double beta = xm / ( ym * rac); |
---|
364 | // double gamma = ym / ( xm * rac ); |
---|
365 | double epsil = xm * ym * rac; |
---|
366 | double fac1 = -1.0 / ( beta * beta); |
---|
367 | double fac2 = epsil/beta; |
---|
368 | double fac3 = -alpha/beta; |
---|
369 | double aux; |
---|
370 | for ( k=0; k < nbintv; k++) |
---|
371 | { |
---|
372 | x = -xm + k*pas; |
---|
373 | aux = fac1 * x * x + fac2; |
---|
374 | // cout << " aux2= " << aux << endl; |
---|
375 | if ( aux <= 0.0 ) |
---|
376 | { |
---|
377 | aux = 0.0; |
---|
378 | } |
---|
379 | else aux = sqrt(aux); |
---|
380 | |
---|
381 | // y = fac3*x; |
---|
382 | y = fac3*x + aux; |
---|
383 | xcor.push_back(x); |
---|
384 | ycor.push_back(y); |
---|
385 | } |
---|
386 | |
---|
387 | for ( k=0; k <= nbintv; k++) |
---|
388 | { |
---|
389 | x = xm - k*pas; |
---|
390 | aux = fac1 * x * x + fac2; |
---|
391 | if ( aux <= 0.0 ) |
---|
392 | { |
---|
393 | aux = 0.0; |
---|
394 | } |
---|
395 | else aux = sqrt(aux); |
---|
396 | // y = fac3*x; |
---|
397 | y = fac3*x - aux; |
---|
398 | xcor.push_back(x); |
---|
399 | ycor.push_back(y); |
---|
400 | } |
---|
401 | } |
---|
402 | else |
---|
403 | // cas degenere |
---|
404 | { |
---|
405 | cout << " cas degenere " << endl; |
---|
406 | double fac = ym/xm; |
---|
407 | for ( k=0; k < nbintv; k++) |
---|
408 | { |
---|
409 | x = -xm + k*pas; |
---|
410 | y = fac*x; |
---|
411 | xcor.push_back(x); |
---|
412 | ycor.push_back(y); |
---|
413 | } |
---|
414 | |
---|
415 | } |
---|
416 | } |
---|
417 | |
---|
418 | void particleBeam::histogramme(vector<double>&xcor,vector<int>& hist,int& cnts) |
---|
419 | { |
---|
420 | double gammin= GRAND; |
---|
421 | double gammax= -gammin; |
---|
422 | double Emoy= 0.0; |
---|
423 | double ecatyp= 0.0; |
---|
424 | |
---|
425 | for (unsigned int k = 0; k < goodPartic_.size(); k++) |
---|
426 | { |
---|
427 | double gamma = goodPartic_.at(k).getGamma(); |
---|
428 | double EMev = (gamma-1.0)*ERESTMeV; |
---|
429 | if (gamma < gammin) gammin = gamma; |
---|
430 | else if (gamma > gammax) gammax = gamma; |
---|
431 | Emoy += EMev; |
---|
432 | ecatyp += EMev*EMev; |
---|
433 | } |
---|
434 | |
---|
435 | double sum= (float)goodPartic_.size(); |
---|
436 | Emoy /= sum; |
---|
437 | ecatyp /= sum; |
---|
438 | ecatyp = sqrt(abs(ecatyp-Emoy*Emoy)); |
---|
439 | |
---|
440 | double Emin = (gammin-1.0)*ERESTMeV; |
---|
441 | double Emax = (gammax-1.0)*ERESTMeV; |
---|
442 | cout << "energie cinetique -moyenne " << Emoy << " Mev " << "-mini " << Emin << " Mev " << "-maxi " << Emax << " Mev " << "ecart type " << ecatyp*1000.0 << " Kev" << endl; |
---|
443 | |
---|
444 | vector<double> Eshf; |
---|
445 | for (unsigned int k = 0; k < goodPartic_.size(); k++) |
---|
446 | { |
---|
447 | double gamma = goodPartic_.at(k).getGamma(); |
---|
448 | double EMev = (gamma-1.0)*ERESTMeV; |
---|
449 | Eshf.push_back(EMev-Emoy); |
---|
450 | } |
---|
451 | |
---|
452 | ////////////////////////////////////////////////////////////////////////////// |
---|
453 | |
---|
454 | // demi fenetre en energie, et pas de l'histogramme |
---|
455 | // double hfene= max(3.*ecatyp-Emoy,Emoy-3.*ecatyp); |
---|
456 | double hfene= 3.*ecatyp; |
---|
457 | double hpas = hfene/25.; |
---|
458 | |
---|
459 | cout << "demi fenetre " << hfene << ", hpas= " << hpas << endl; |
---|
460 | |
---|
461 | double vmin = -hfene; |
---|
462 | double dfen = 2.*hfene; |
---|
463 | int ihist = dfen/hpas; |
---|
464 | double phist = ihist*hpas; |
---|
465 | double dpas = hpas-(dfen-phist); |
---|
466 | if(dpas <= hpas*1.e-03) { |
---|
467 | ihist++; |
---|
468 | phist= ihist*hpas; |
---|
469 | } |
---|
470 | double vmax= vmin+hpas*ihist; |
---|
471 | |
---|
472 | cout << "'xAxisNumberOfBins= " << ihist <<", xAxisMinimum= " << vmin << ", xAxisMaximum= " << vmax << ", NParticules= " << Eshf.size() << ", phist " << phist << endl; |
---|
473 | |
---|
474 | xcor= vector<double>(ihist); |
---|
475 | for (int i = 0; i < ihist; ++i) { |
---|
476 | |
---|
477 | // on gradue l'abcisse en pourcents |
---|
478 | xcor[i]= 100.*( vmin+i*hpas ); |
---|
479 | } |
---|
480 | |
---|
481 | hist= vector<int>(ihist,0); |
---|
482 | for (unsigned i = 0; i < Eshf.size(); ++i) { |
---|
483 | double var= Eshf[i]-vmin; |
---|
484 | if(var < 0 || var >= phist) cout<<"out of range "<<var<<", ("<< i<<")"<< endl; |
---|
485 | int k= var/hpas; |
---|
486 | int kk= (int)floor(var/hpas); |
---|
487 | if(i%20 == 0) cout<<"v("<<i<<")= " <<var<<" ["<<k<<"-"<<kk<<"], "<<endl; |
---|
488 | hist[kk]++; |
---|
489 | } |
---|
490 | |
---|
491 | cnts= 0; |
---|
492 | for (int i = 0; i < ihist; ++i) { |
---|
493 | if(hist.at(i) > 0) cnts++; |
---|
494 | cout<<"("<<xcor.at(i)<<","<<hist.at(i)<<") "; |
---|
495 | } |
---|
496 | cout<< " ... cnts= " << cnts << endl; |
---|
497 | } |
---|