Changeset 3280 for branches

Show
Ignore:
Timestamp:
01/26/12 21:07:06 (4 months ago)
Author:
erkn
Message:

now can handle an input spectrum file

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/mcxtrace-1.0/xlib/sources/Source_gaussian.comp

    r3256 r3280  
    3535 
    3636DEFINE COMPONENT Source_gaussian 
    37 DEFINITION PARAMETERS () 
    38 SETTING PARAMETERS (sig_x=1,sig_y=0,sigPr_x=0,sigPr_y=0,flux=1,dist=1,gamma=0,E0=0, dE=0, lambda0=0,dlambda=-1,phase=-1) 
     37DEFINITION PARAMETERS (string spectrum_file=NULL) 
     38SETTING PARAMETERS (sig_x=1,sig_y=0,sigPr_x=0,sigPr_y=0,flux=1,dist=1,gauss=0,gamma=0,E0=0, dE=0, lambda0=0,dlambda=-1,phase=-1) 
    3939OUTPUT PARAMETERS () 
    4040/* X-ray parameters: (x,y,z,kx,ky,kz,phi,t,Ex,Ey,Ez,p) */  
     
    4242SHARE 
    4343%{ 
     44  %include "read_table-lib" 
    4445  double Gauss2D(double sigmaX, double sigmaY, double x, double y, double A){ 
    4546    double F; 
     
    5152DECLARE 
    5253%{ 
    53  double l, pmul; 
     54  struct { 
     55    double l0,dl; 
     56    double pmul,pint; 
     57    t_Table T; 
     58  } prms; 
    5459%} 
    5560 
     
    6368    exit(0); 
    6469  } 
    65          
    66   if (E0){ 
    67     lambda0=2*M_PI/(E0*E2K); 
     70 
     71 if (spectrum_file){ 
     72    /*read spectrum from file*/ 
     73    int status=0; 
     74    if ( (status=Table_Read(&(prms.T),spectrum_file,0))==-1){ 
     75      fprintf(stderr,"Source_gaussian(%s) Error: Could not parse file \"%s\"\n",NAME_CURRENT_COMP,spectrum_file); 
     76      exit(-1); 
     77    } 
     78    /*data is now in table t*/ 
     79    /*integrate to get total flux, assuming numbers have been corrected for measuring aperture*/ 
     80    int i; 
     81    prms.pint=0; 
     82    t_Table *T=&(prms.T); 
     83    for (i=0;i<prms.T.rows-1;i++){ 
     84      prms.pint+=((T->data[i*T->columns+1]+T->data[(i+1)*T->columns+1])/2.0)*(T->data[(i+1)*T->columns]-T->data[i*T->columns]);  
     85    } 
     86    printf("Source_gaussian(%s) Integrated intensity radiated is %g pht/s\n",NAME_CURRENT_COMP,prms.pint); 
     87    if(E0) printf("Source_gaussian(%s) E0!=0 -> assuming intensity spectrum is parametrized by energy [keV]\n",NAME_CURRENT_COMP); 
     88  } else if (E0){ 
     89    lambda0=2*M_PI/E2K * (E0/(E0*E0-dE*dE)); 
    6890    if (dE) { 
    69       dlambda=2*M_PI/(E2K*E0*E0)*dE; 
     91      dlambda=2*M_PI/E2K * (dE/(E0*E0-dE*dE)); 
    7092    } else { 
    7193      dlambda=0; 
     
    7799  /*calculate the X-ray weight from the flux*/ 
    78100  if (flux){//pmul=flux; 
    79     pmul=flux*1.0/((double) mcget_ncount());  
     101    prms.pmul=flux*1.0/(double)mcget_ncount();  
    80102  }else{ 
    81     pmul=1.0/((double) mcget_ncount()); 
     103    prms.pmul=1.0/(double)mcget_ncount(); 
    82104  } 
    83  
    84    
    85    
    86105%} 
    87106 
     
    90109%{ 
    91110  double xx,yy,spX,spY,x1,y1,z1; 
    92   double k; 
     111  double k,e,l; 
    93112  double F1,F2; 
    94113  double dx,dy,dz; 
     
    102121  
    103122  // Gaussian distribution at origin 
    104   F1=Gauss2D(sig_x,sig_y,x,y,pmul); 
    105    
    106   if (dlambda){ 
    107     l=lambda0+dlambda*randnorm(); 
     123  F1=Gauss2D(sig_x,sig_y,x,y,prms.pmul); 
     124 
     125  if (spectrum_file){ 
     126    double pp=0; 
     127    //while (pp<=0){  
     128    l=prms.T.data[0]+ (prms.T.data[(prms.T.rows-1)*prms.T.columns] -prms.T.data[0])*rand01(); 
     129    pp=Table_Value(prms.T,l,2); 
     130    //} 
     131    p*=pp; 
     132    /*if E0!=0 convert the tabled value to wavelength*/ 
     133    if (E0) { 
     134      l=2*M_PI/(l*1e-3*E2K); 
     135    } 
     136  }else if (dlambda){ 
     137    if (gauss){ 
     138      l=lambda0+dlambda*randnorm(); 
     139    }else{ 
     140      l=randpm1()*dlambda*0.5 + lambda0; 
     141    } 
    108142  }else{ 
    109143    l=lambda0; 
     
    111145 
    112146  k=(2*M_PI/l);  
    113    
     147 
    114148  // Beam's footprint at a dist calculation 
    115149  spX=sqrt(sig_x*sig_x+sigPr_x*sigPr_x*dist*dist); 
     
    142176  Ex=0;Ey=0;Ez=0; 
    143177   
    144   p*=erf(F2)*pmul;   
     178  p*=erf(F2)*prms.pmul;   
    145179   
    146180%}