source: PSPA/Interface_Web/trunk/pspaWT/src/particleBeam.cc @ 124

Last change on this file since 124 was 124, checked in by lemeur, 12 years ago

normalisation des includes mathematicalConstants.h

File size: 11.6 KB
Line 
1#include "particleBeam.h"
2#include "mathematicalConstants.h"
3#include "PhysicalConstants.h"
4//#include <string>
5#include <stdio.h>
6
7#include "environmentVariables.h"
8
9
10bool particleBeam::setFromParmela(unsigned numeroElement, double referencefrequency)
11{
12  unsigned int k;
13  FILE* filefais;
14  string nomfilefais = WORKINGAREA + "parmdesz";
15  cout << " nom fichier desz : " << nomfilefais << endl;
16  filefais = fopen(nomfilefais.c_str(), "r");
17
18  if ( filefais == (FILE*)0 ) 
19    {
20      cerr << " particleBeam::setFromParmela() erreur a l'ouverture du fichier" << nomfilefais  << endl;; 
21      return false;
22    }
23  else cout << " particleBeam::setFromParmela() : ouverture du fichier " << nomfilefais << endl; 
24
25  struct particle partic;
26  struct particle* partRefPtr=NULL;
27
28  std::vector<struct particle> faisceau;
29
30  //  int numElem=-1;
31  partRefPtr=NULL;
32  cout << " particleBeam::setFromParmela : numeroElement = " << numeroElement << endl;
33  numeroElement--;
34  while( partic.readFromParmelaFile(filefais) > 0 )
35
36    {
37      // // on ne va conserver que les resultats a la sortie du dernier element
38      // if ( partic.ne != numElem )
39      //        {
40      //          faisceau.clear();
41      //          partRefPtr=NULL;
42      //          numElem = partic.ne;
43      //        }
44      //      cout << " partic.ne = " << partic.ne << endl;
45      if ( partic.ne == numeroElement )
46        {
47          //      cout << " trouve particule " << endl;
48          faisceau.push_back(partic);
49          if ( partic.np == 1 )
50            {
51              if ( fabs(partic.xx) > EPSILON || fabs(partic.yy) > EPSILON || fabs(partic.xxp) > EPSILON  || fabs(partic.yyp) > EPSILON) 
52                {
53                  printf(" ATTENTION part. reference douteuse  \n");
54                  partic.imprim();
55                }
56              partRefPtr=&faisceau.back();
57          //      cout << " indice de la part. ref. " << faisceau.size()-1 << endl;
58          //          printf("part. reference \n");
59          //          partRefPtr->imprim();
60            }
61        }
62    }
63
64  if ( faisceau.size() == 0) return false;
65
66  //  printf("dernier element %d \n", numElem);
67  // facteur  c/ 360. pour calculer (c dphi) / (360.freq)
68  // avec freq en Mhz et dphi en degres et résultat en cm:
69  double FACTEUR =  83.3333;  // ameliorer la precision
70  double x,xp,y,yp;
71  double betagammaz;
72  // contrairement a ce qu'indique la notice PARMELA, dans parmdesz, les xp et yp
73  // sont donnes en radians
74  // particule de reference
75 
76  x=partRefPtr->xx;
77  xp=partRefPtr->xxp;
78  y=partRefPtr->yy;
79  yp=partRefPtr->yyp;
80  betagammaz = partRefPtr->begamz;
81  TRIDVECTOR  posRef(x,y,0.0);
82  TRIDVECTOR betagammaRef(xp*betagammaz, yp*betagammaz, betagammaz);
83
84  referenceParticle_ = bareParticle(posRef, betagammaRef);
85
86
87  // pour l'instant on choisit un centroid nul;
88  centroid_ = vector<double>(6,0.0);
89
90  goodPartic_.clear();
91  for ( k=0; k < faisceau.size(); k++)
92    //  for (int k=0; k < 10; k++)
93    {
94      x=faisceau.at(k).xx;
95      xp=faisceau.at(k).xxp;
96      y=faisceau.at(k).yy;
97      yp=faisceau.at(k).yyp;
98      // dephasage par rapport a la reference 
99      double dephas = faisceau.at(k).phi - partRefPtr->phi; // degrés
100      double g = faisceau.at(k).wz/ERESTMeV;
101      betagammaz = faisceau.at(k).begamz;
102      double betaz = betagammaz/(g+1.0);
103      double deltaz = FACTEUR * betaz * dephas / referencefrequency;
104      x += xp * deltaz;
105      y += yp * deltaz;
106      TRIDVECTOR  pos(x,y,deltaz);
107      TRIDVECTOR betagamma(xp*betagammaz, yp*betagammaz, betagammaz);
108      bareParticle bp(pos,betagamma);
109      goodPartic_.push_back(bp);
110    }
111  particleRepresentationOk_ = true;
112  //  buildMomentRepresentation();
113  return true;
114}
115
116
117void particleBeam::getVariance(double& varx, double& vary, double& varz) const
118{
119
120  double x,y,z;
121  double xav = 0.;
122  double yav = 0.;
123  double zav = 0.;
124  double xavsq = 0.;
125  double yavsq = 0.;
126  double zavsq = 0.;
127
128
129  TRIDVECTOR pos;
130
131  unsigned int k;
132
133  for ( k = 0 ; k < goodPartic_.size(); k++)
134    {
135      pos = goodPartic_.at(k).getPosition();
136      pos.getComponents(x,y,z);
137      //      partic_[k].getXYZ(x,y,z);
138      xav += x;
139      xavsq += x*x;
140      yav += y;
141      yavsq += y*y;
142      zav += z;
143      zavsq += z*z;
144    }
145
146  double aginv = double (goodPartic_.size());
147  aginv = 1.0/aginv;
148
149  varx =  aginv * ( xavsq - xav*xav*aginv );
150  vary =  aginv * ( yavsq - yav*yav*aginv );
151  varz =  aginv * ( zavsq - zav*zav*aginv );
152
153}
154
155
156void particleBeam::printAllXYZ() const
157{
158  cout << " dump du faisceau : " << endl;
159
160  cout <<  goodPartic_.size() << " particules " << endl;
161  unsigned int k;
162  for ( k = 0 ; k < goodPartic_.size(); k++)
163    {
164      double xx,yy,zz;
165      goodPartic_.at(k).getPosition().getComponents(xx,yy,zz);
166      cout << " part. numero " << k << "  x= " << xx << " y= " << yy  << " z= " << zz << endl;
167    }
168}
169
170
171
172void particleBeam::Zrange(double& zmin, double& zmax) const
173{
174  double z;
175  zmin = GRAND;
176  zmax = -zmin;
177
178  unsigned int k;
179  for ( k = 0 ; k < goodPartic_.size(); k++)
180    {
181      z = goodPartic_.at(k).getZ();
182      if ( z < zmin ) zmin = z;
183      else if ( z > zmax) zmax = z;         
184    }
185}
186
187
188
189string particleBeam::FileOutputFlow() const
190{
191  ostringstream sortie;
192  unsigned int k;
193  for ( k = 0 ; k < goodPartic_.size(); k++)
194    {
195      sortie << goodPartic_.at(k).FileOutputFlow() << endl;
196    }
197  sortie << endl;
198  return sortie.str();
199}
200
201bool particleBeam::FileInput( ifstream& ifs)
202{
203  bool test = true;
204  string dum1, dum2;
205  double dummy;
206  if ( !( ifs >> dum1 >> dum2 >> dummy) ) return false;
207 
208  bareParticle pp;
209  while ( pp.FileInput(ifs) )
210    {
211      addParticle( pp);
212    }
213  return test;
214}
215
216void particleBeam::buildMomentRepresentation()
217{
218
219  unsigned k,j,m;
220  double auxj, auxm;
221
222  if ( !particleRepresentationOk_)
223    {
224      cerr << " particleBeam::buildMomentRepresentation() vecteur de particules invalide" << endl;
225    }
226
227  cout << " buildMomentRepresentation " << endl;
228  //  printAllXYZ();
229
230  double gref = referenceParticle_.getGamma() - 1.0;
231  double P_reference_MeV_sur_c = sqrt( gref*(gref+2) );
232
233  // initialisation des moments
234  for ( j = 0; j < 6; j++)
235    {
236      for (m=0; m <= j; m++) 
237        {
238          ( rij_transportMoments_.at(j) ).at(m) = 0.0; // element r_jm
239        }
240    }
241
242  // accumulation
243  for (k=0; k < goodPartic_.size(); k++)
244    {
245      bareParticle bp = goodPartic_.at(k);
246      double gamma = bp.getGamma();
247      TRIDVECTOR pos = bp.getPosition();
248      TRIDVECTOR begam= bp.getBetaGamma();
249      double begamz = begam.getComponent(2);
250      double g = gamma -1.0;
251      double PMeVsc = sqrt( g*(g+2) );
252      double del = 100.0 * ( PMeVsc -  P_reference_MeV_sur_c ) / P_reference_MeV_sur_c ; // en %
253
254      vector<double> part(6);
255      part[0] = pos.getComponent(0);
256      part[1] = begam.getComponent(0)/begamz;
257      part[2] = pos.getComponent(1);
258      part[3] = begam.getComponent(1)/begamz;
259
260      part[4] = pos.getComponent(2);
261
262      part[5] = del;
263
264      //      cout << " buildMomentRepresentation part. " << k << " x= " << part[0] << " xp= " << part[1] << " y= " << part[2] << " yp= " << part[3] << endl;
265
266      for ( j = 0; j < 6; j++)
267        {
268          auxj = part.at(j) - centroid_.at(j);
269          for (m=0; m <= j; m++) 
270            {
271              auxm = part.at(m) - centroid_.at(m);
272              ( rij_transportMoments_.at(j) ).at(m) += auxj*auxm;
273              //              cout << " j= " << j << " m= " << m << " rjm= " << ( rij_transportMoments_.at(j) ).at(m) << endl;
274            }
275        }
276    }
277
278
279  // moyenne
280  double facmoy = 1.0/double( goodPartic_.size() );
281  for ( j = 0; j < 6; j++)
282    {
283      ( rij_transportMoments_.at(j) ).at(j) = sqrt(( rij_transportMoments_.at(j) ).at(j) * facmoy );
284    }
285
286  for ( j = 0; j < 6; j++)
287    {
288      auxj =  ( rij_transportMoments_.at(j) ).at(j);
289      for (m=0; m < j; m++) 
290        {
291          auxm = ( rij_transportMoments_.at(m) ).at(m);
292          ( rij_transportMoments_.at(j) ).at(m) *= facmoy/(auxj * auxm);
293        }
294    }
295   
296  // les longueurs sont en cm
297  // les angles en radians, on passe en mrad;
298
299  double uniteAngle = 1.0e+3;
300  ( rij_transportMoments_.at(1) ).at(1)  *= uniteAngle;
301
302  ( rij_transportMoments_.at(3) ).at(3)  *= uniteAngle;
303   
304  P0Transport_ = 1.0e-3*ERESTMeV*P_reference_MeV_sur_c;
305
306  // cout << " impression des moments " << endl;
307  // for ( j = 0; j < 6; j++)
308  //   {
309  //     for (m=0; m <= j; m++)
310  //    {
311  //          cout  << ( rij_transportMoments_.at(j) ).at(m) << " ";
312  //    }
313  //     cout << endl;
314  //   }
315
316  momentRepresentationOk_ = true;
317}
318
319bool  particleBeam::setFromTransport(ifstream& inp, unsigned nblignes)
320  {
321    unsigned k;
322       unsigned j,m;
323    unsigned  nl0;
324    //    cout << " particleBeam : lecture resultats transport nblignes= " << nblignes << endl;
325      string buf;
326       nl0 = nblignes - 7;
327       //       cout << " setFromTransport nblignes= " << nblignes << endl;
328       for (k=0; k < nl0; k++) 
329         {
330           getline(inp, buf);
331           // cout  << " ligne " << k+1 << " buf= " << buf << endl;
332         }
333
334       readTransportMoments(inp);
335
336  cout << " impression des moments " << endl;
337  for ( j = 0; j < 6; j++)
338    {
339      for (m=0; m <= j; m++) 
340        {
341              cout  << ( rij_transportMoments_.at(j) ).at(m) << " ";
342        }
343      cout << endl;
344    }
345
346     momentRepresentationOk_ = true;
347     return true;
348  }
349
350
351
352void particleBeam::readTransportMoments(ifstream& inp)
353{
354        unsigned j,m;
355      string  bidString;
356       double bidon;
357
358  // initialisation des moments
359  for ( j = 0; j < 6; j++)
360    {
361      for (m=0; m <= j; m++) 
362        {
363          ( rij_transportMoments_.at(j) ).at(m) = 0.0; // element r_jm
364        }
365    }
366
367        inp >> bidon >>  bidString >>  bidon >>  ( rij_transportMoments_.at(0) ).at(0) >> bidString;
368        inp >> bidon >> ( rij_transportMoments_.at(1) ).at(1) >>  bidString >> ( rij_transportMoments_.at(1) ).at(0);
369        inp >> bidon >> ( rij_transportMoments_.at(2) ).at(2) >>  bidString >> ( rij_transportMoments_.at(2) ).at(0)  >> ( rij_transportMoments_.at(2) ).at(1);
370        inp >> bidon >> ( rij_transportMoments_.at(3) ).at(3) >>  bidString >> ( rij_transportMoments_.at(3) ).at(0)  >> ( rij_transportMoments_.at(3) ).at(1) >> ( rij_transportMoments_.at(3) ).at(2);
371
372        inp >> bidon >> ( rij_transportMoments_.at(4) ).at(4) >>  bidString >> ( rij_transportMoments_.at(4) ).at(0)  >> ( rij_transportMoments_.at(4) ).at(1) >> ( rij_transportMoments_.at(4) ).at(2) >> ( rij_transportMoments_.at(4) ).at(3);
373
374        inp >> bidon >> ( rij_transportMoments_.at(5) ).at(5) >>  bidString >> ( rij_transportMoments_.at(5) ).at(0)  >> ( rij_transportMoments_.at(5) ).at(1) >> ( rij_transportMoments_.at(5) ).at(2) >> ( rij_transportMoments_.at(5) ).at(3) >> ( rij_transportMoments_.at(5) ).at(4);
375
376}
377
378
379
380void particleBeam::donneesDessinEllipseXxp(vector<double>& xcor, vector<double>& ycor)
381{
382  if ( !momentRepresentationOk_ ) return;
383
384  xcor.clear();
385  ycor.clear();
386
387  double xm = ( rij_transportMoments_.at(0) ).at(0);
388  double ym = ( rij_transportMoments_.at(1) ).at(1);
389  double r  = ( rij_transportMoments_.at(1) ).at(0);
390
391  //  cout << " r= " << r << endl;
392
393  double rac = sqrt(1 - r*r);
394  double alpha = -r / rac;
395  double beta = xm / ( ym * rac);
396  //  double gamma = ym / ( xm * rac );
397  double epsil = xm * ym * rac;
398
399
400  int nbintv = 50;
401  double pas = 2.0 * xm / nbintv;
402  double fac1 = -1.0 / ( beta * beta);
403  double fac2 = epsil/beta;
404  double fac3 = -alpha/beta;
405  int k;
406  double x,y;
407  double aux;
408  for ( k=0; k < nbintv; k++)
409    {
410      x = -xm + k*pas;
411      aux = fac1 * x * x + fac2;
412      //     cout << " aux2= " << aux << endl;
413      if ( aux <= 0.0 )
414        {
415          aux = 0.0;
416        }
417      else aux = sqrt(aux);
418     
419      //        y = fac3*x;
420            y = fac3*x + aux;
421      xcor.push_back(x);
422      ycor.push_back(y);
423    }
424
425  for ( k=0; k <= nbintv; k++)
426    {
427      x = xm - k*pas;
428      aux =  fac1 * x * x + fac2;
429      if ( aux <= 0.0 ) 
430        {
431          aux = 0.0;
432        }
433      else aux = sqrt(aux);
434      //   y = fac3*x;
435            y = fac3*x - aux;
436      xcor.push_back(x);
437      ycor.push_back(y);
438    }
439}
Note: See TracBrowser for help on using the repository browser.