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

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

bug #43 fixed

File size: 12.6 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
428void particleBeam::histogramme(vector<double>&xcor,vector<int>& hist,int& cnts,double out[3])
429{
430  // sortie pour la legende: out[0]= entries, out[1]= mean, out[2]= rms
431
432  double gammin= GRAND;
433  double gammax= -gammin;
434  double Emoy= 0.0;
435  double ecatyp= 0.0;
436
437  for (unsigned int k = 0; k < goodPartic_.size(); k++)
438    {
439      double gamma = goodPartic_.at(k).getGamma();
440      double EMev = (gamma-1.0)*ERESTMeV;
441      if (gamma < gammin) gammin = gamma;
442      else if (gamma > gammax) gammax = gamma;
443      Emoy += EMev;
444      ecatyp += EMev*EMev;
445    }
446
447  double sum= (float)goodPartic_.size();
448  out[0]= sum;
449  Emoy /= sum;
450  out[1]= Emoy; //MeV
451  ecatyp /= sum;
452  out[2]= 1000.0*sqrt(ecatyp); //KeV
453  ecatyp = sqrt(abs(ecatyp-Emoy*Emoy));
454
455  double Emin = (gammin-1.0)*ERESTMeV;
456  double Emax = (gammax-1.0)*ERESTMeV;
457  cout << "energie cinetique -moyenne " << Emoy << " Mev " << "-mini " << Emin << " Mev " << "-maxi " << Emax << " Mev " << "ecart type " << ecatyp*1000.0 << " Kev" << endl;
458
459  vector<double> Eshf;
460  for (unsigned int k = 0; k < goodPartic_.size(); k++)
461    {
462      double gamma = goodPartic_.at(k).getGamma();
463      double EMev = (gamma-1.0)*ERESTMeV;
464      Eshf.push_back(EMev-Emoy);
465    }
466
467  //////////////////////////////////////////////////////////////////////////////
468 
469  // demi fenetre en energie, et pas de l'histogramme
470  //  double hfene= max(3.*ecatyp-Emoy,Emoy-3.*ecatyp);
471  double hfene= 3.*ecatyp;
472  double hpas = hfene/25.;
473
474  cout << "demi fenetre " << hfene << ", hpas= " << hpas << endl;
475
476  double vmin = -hfene;
477  double dfen = 2.*hfene;
478  int ihist = dfen/hpas;
479  double phist = ihist*hpas;
480  double dpas = hpas-(dfen-phist);
481  if(dpas <= hpas*1.e-03) {
482    ihist++;
483    phist= ihist*hpas;
484  }
485  double vmax= vmin+hpas*ihist;
486 
487  cout << "'xAxisNumberOfBins= " << ihist <<", xAxisMinimum= " << vmin << ", xAxisMaximum= " << vmax << ", NParticules= " << Eshf.size() << ", phist " << phist << endl;
488
489  xcor= vector<double>(ihist);
490  for (int i = 0; i < ihist; ++i) {
491
492    // on gradue l'abscisse en pourcents
493    xcor[i]= 100.*( vmin+i*hpas );
494  }
495
496  hist= vector<int>(ihist,0);
497  for (unsigned i = 0; i < Eshf.size(); ++i) {
498    double var= Eshf[i]-vmin;
499    if(var < 0 || var >= phist) cout<<"out of range "<<var<<", ("<< i<<")"<< endl;
500    int k= var/hpas;
501    int kk= (int)floor(var/hpas);
502    //if(i%20 == 0) cout<<"v("<<i<<")= " <<var<<" ["<<k<<"-"<<kk<<"], "<<endl;
503    hist[kk]++;
504  }
505
506  cnts= 0;
507  for (int i = 0; i < ihist; ++i) {
508    if(hist.at(i) > 0) cnts++;
509    //cout<<"("<<xcor.at(i)<<","<<hist.at(i)<<") ";
510  }
511  cout<< " ... cnts=  " << cnts << endl;
512}
Note: See TracBrowser for help on using the repository browser.