source: PSPA/Interface_Web/trunk/pspaWT/sources/controler/src/particleBeam.cc @ 341

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

fin dessins esp. phase + noms de fichiers

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