1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
/*******************************************************************************
*
* McXtrace, x-ray tracing package
* Copyright, All rights reserved
* DTU Physics, Kgs. Lyngby, Denmark
* Synchrotron SOLEIL, Saint-Aubin, France
*
* Component: PSD_monitor_coh
*
* %Identification
* Written by: Erik Knudsen
* Date: March 13, 2010
* Origin: Risoe
*
* Position-sensitive monitor with phase integration.
*
* %Description
* An (n times m) pixel PSD monitor taking phase into account.
* As the i:th ray hits a pixel (j,k) in the monitor the intensity in that
* pixel will be updated as a complex sum, i.e. <math>P_i = P_{i-1} + p_i exp{-\phi_i}</math>.
*
* By setting ratio<1 the effective pixel area becomes a
* fraction of the ideal (which is to divide the xwidth and yheight intervals into nx and ny abutting
* subintervals). This reduces the monitor effective area by ratio^2.
* If the centering flag is set - the monitor will treat all rays as if they hit a pixel
* center. This behaves as if ratio -> 0, but at no cost in statistics.
*
* Example: PSD_monitor_coh(xwidth=0.1, yheight=0.1,
* nx=90, ny=90, filename="Output.psd")
*
* %Parameters
* INPUT PARAMETERS:
*
* xwidth: [m] Width of detector.
* yheight: [m] Height of detector.
* nx: [ ] Number of pixel columns.
* ny: [ ] Number of pixel rows.
* filename: [m] Name of file in which to store the detector images - the suffixes .abs and .arg will be added.
* restore_xray: [ ] If set, the monitor does not influence the xray state.
* ratio: [ ] ratio between pixel area and effective pixel area.
* centering: [ ] Treat all rays as if they hit the center of the pixel.
* nowritefile: [1] If set, monitor will skip writing to disk
*
* CALCULATED PARAMETERS:
*
* PSD_N: Array of xray event counts.
* PSD_phi: Array of xray complex weight counts (the argument consituttes the phase).
* PSD_p2: Array of second ms.
*
* %End
*******************************************************************************/
DEFINE COMPONENT PSD_monitor_coh
SETTING PARAMETERS (int nx=90, int ny=90, string filename=0, restore_xray=0,
xwidth=0.05, yheight=0.05, ratio=1, int centering=1, int nowritefile=0)
/*STATE PARAMETERS (x,y,z,kx,ky,kz,phi,Ex,Ey,Ez,p)*/
SHARE
%{
#include <complex.h>
%}
DECLARE
%{
DArray2d PSD_N;
DArray2d PSD_p2;
DArray2d PSD_phi_real;
DArray2d PSD_phi_imag;
double xmin;
double xmax;
double ymin;
double ymax;
%}
INITIALIZE
%{
int i,j;
xmax = xwidth/2; xmin = -xmax;
ymax = yheight/2; ymin = -ymax;
if ((xmin >= xmax) || (ymin >= ymax)) {
fprintf(stderr,"ERROR (%s): Null detection area! Aborting.\n",NAME_CURRENT_COMP);
exit(-1);
}
PSD_N = create_darr2d(nx,ny);
PSD_p2 = create_darr2d(nx,ny);
PSD_phi_real = create_darr2d(nx,ny);
PSD_phi_imag = create_darr2d(nx,ny);
// Use instance name for monitor output if no input was given
if (!strcmp(filename,"\0")) sprintf(filename,"%s",NAME_CURRENT_COMP);
%}
TRACE
%{
int i,j;
double k,x_o,y_o,z_o;
double p2;
double complex dphi;
double dphi_r,dphi_i;
x_o=x;y_o=y;z_o=z;
k=sqrt(kx*kx+ky*ky+kz*kz);
PROP_Z0;
if (x>xmin && x<xmax && y>ymin && y<ymax)
{
double dl,dlc,dnu,cx,cy;
i = floor((x - xmin)*nx/(xmax - xmin));
j = floor((y - ymin)*ny/(ymax - ymin));
/*center coordinates of the pixel*/
if(centering){
dl=sqrt( (x-x_o)*(x-x_o) + (y-y_o)*(y-y_o) + z_o*z_o);
cx=xwidth/nx*(i+0.5)-xwidth/2.0;
cy=yheight/ny*(j+0.5)-yheight/2.0;
/*what would the phase be if we transported it to the center of the pixel?*/
dlc=sqrt((cx-x_o)*(cx-x_o) + (cy-y_o)*(cy-y_o) + z_o*z_o);
dnu=(dl-dlc)*k*1e10;
}else{
dnu=0;
}
/*check if within the ratio of the pixel*/
if ( fabs(xwidth/nx*(i+0.5)-xwidth/2.0-x)<xwidth/nx*ratio && fabs(yheight/ny*(j+0.5)-yheight/2.0-y)<yheight/ny*ratio ){
dphi=p*cexp(I*(phi-dnu));
dphi_r=creal(dphi);
dphi_i=cimag(dphi);
p2 = p*p;
#pragma acc atomic
PSD_N[i][j]+=1;
#pragma acc atomic
PSD_phi_real[i][j]+= dphi_r;
#pragma acc atomic
PSD_phi_imag[i][j]+= dphi_i;
#pragma acc atomic
PSD_p2[i][j] += p2;
SCATTER;
}
}
if (restore_xray) {
RESTORE_XRAY(INDEX_CURRENT_COMP, x, y, z, kx, ky, kz, phi, t, Ex, Ey, Ez, p);
}
%}
SAVE
%{
if(!nowritefile){
char filename_abs[255],filename_arg[255];
double *PSD_p_abs, *PSD_p_arg;
int i,j;
snprintf(filename_abs,255,"%s.abs",filename);
snprintf(filename_arg,255,"%s.arg",filename);
/*This should be done differently if MPI*/
PSD_p_abs=calloc(nx*ny,sizeof(double));
PSD_p_arg=calloc(nx*ny,sizeof(double));
if(PSD_p_abs==NULL || PSD_p_arg==NULL){
fprintf(stderr,"Error (%s): Buffer memory allocation error\nAborting\n.",NAME_CURRENT_COMP);exit(-1);
exit(-1);
}
for (i=0;i<nx;i++){
for (j=0;j<ny;j++){
PSD_p_abs[i*ny +j]=cabs(PSD_phi_real[i][j] + I*PSD_phi_imag[i][j]);
PSD_p_arg[i*ny +j]=carg(PSD_phi_real[i][j] + I*PSD_phi_imag[i][j]);
}
}
#ifdef USE_MPI
double *PSD_p_absbuf,*PSD_p_argbuf;
double *N_buf;
if (mpi_node_rank==0){
PSD_p_absbuf=calloc(mpi_node_count*ny,sizeof(double));
PSD_p_argbuf=calloc(mpi_node_count*ny, sizeof(double));
N_buf=calloc(mpi_node_count*ny,sizeof(double));
}else{
PSD_p_absbuf=NULL;
PSD_p_absbuf=NULL;
N_buf=NULL;
}
for (i=0;i<nx;i++){
MPI_Gather(&(PSD_p_abs[i*ny]), ny, MPI_DOUBLE, PSD_p_absbuf, ny, MPI_DOUBLE, 0,MPI_COMM_WORLD);
MPI_Gather(&(PSD_p_arg[i*ny]), ny, MPI_DOUBLE, PSD_p_argbuf, ny, MPI_DOUBLE, 0,MPI_COMM_WORLD);
MPI_Gather(&(PSD_N[i][0]),ny, MPI_DOUBLE, N_buf, ny, MPI_DOUBLE, 0,MPI_COMM_WORLD);
if (mpi_node_rank==0){
for (j=0;j<ny;j++){
double complex tmp_phi=0;
unsigned long tmp_N=0;
int k;
for (k=0;k<mpi_node_count;k++){
tmp_phi+=PSD_p_absbuf[k*ny+j]*cexp(I*PSD_p_argbuf[k*ny+j]);
tmp_N+=(unsigned long)N_buf[k*ny+j];
}
PSD_p_abs[i*ny+j]=cabs(tmp_phi);
PSD_p_arg[i*ny+j]=carg(tmp_phi);
PSD_N[i][j]= tmp_N;
}
}else{
/*set all the other nodes' data to zero. The DETECTOR_OUT macros will do an unnecessary gather,
and so we don't want to add things twice.*/
for (j=0;j<ny;j++){
PSD_p_abs[i*ny+j]=0;
PSD_p_arg[i*ny+j]=0;
PSD_N[i][j]=0;
}
}
}
/*doing it this way is inefficient since we send a lot of zeros across the network - but it should work*/
#endif
char fname1[256];
snprintf(fname1, 256, "%s_arg", NAME_CURRENT_COMP);
// we use mcdetector_out_nD in order to update the [comp_name]_I symbol for the 2nd output
if(nx==1 && ny==1){
DETECTOR_OUT_0D("Coh. Intensity monitor (magnitude)",
(double) PSD_N[0][0], PSD_p_abs[0], PSD_p2[0][0]);
mcdetector_out_0D("Coh. Phase monitor (argument)",
(double) PSD_N[0][0], PSD_p_arg[0], PSD_p2[0][0], fname1,POS_A_CURRENT_COMP,ROT_A_CURRENT_COMP);
}else if(nx==1){
DETECTOR_OUT_1D(
"Coh. PSD_monitor (magnitude)","Y Position[m]", "Intensity", "Y",
ymin,ymax,ny,PSD_N[0],PSD_p_abs,PSD_p2[0],filename_abs);
mcdetector_out_1D(
"Coh. PSD_monitor (argument)","Y Position[m]", "Phase", "Y",
ymin,ymax,ny,PSD_N[0],PSD_p_arg,PSD_p2[0],filename_arg,
fname1,POS_A_CURRENT_COMP,ROT_A_CURRENT_COMP);
}else if (ny==1){
DETECTOR_OUT_1D(
"Coh. PSD_monitor (magnitude)","X Position[m]", "Intensity", "X",
xmin,xmax,nx,PSD_N[0],PSD_p_abs,PSD_p2[0],filename_abs);
mcdetector_out_1D(
"Coh. PSD_monitor (argument)","X Position[m]", "Phase", "X",
xmin,xmax,nx,PSD_N[0],PSD_p_arg,PSD_p2[0],filename_arg,
fname1,POS_A_CURRENT_COMP,ROT_A_CURRENT_COMP);
}else{
/*don't do the internal gather in DETECTOR_OUT_2D*/
//MPI_NOGATHER;
DETECTOR_OUT_2D(
"Coh. PSD_monitor (magnitude)",
"X position [m]","Y position [m]",
xmin, xmax, ymin, ymax, nx, ny,
PSD_N[0],PSD_p_abs,PSD_p2[0],
filename_abs);
/*don't do the internal gather in DETECTOR_OUT_2D*/
//MPI_NOGATHER;
mcdetector_out_2D(
"Coh. PSD_monitor (argument)",
"X position [m]","Y position [m]",
xmin, xmax, ymin, ymax, nx, ny,
(PSD_N[0]),PSD_p_arg,PSD_p2[0],
filename_arg,
fname1,POS_A_CURRENT_COMP,ROT_A_CURRENT_COMP);
}
}
%}
FINALLY
%{
destroy_darr2d(PSD_N);
destroy_darr2d(PSD_p2);
destroy_darr2d(PSD_phi_real);
destroy_darr2d(PSD_phi_imag);
%}
MCDISPLAY
%{
multiline(5, (double)xmin, (double)ymin, 0.0,
(double)xmax, (double)ymin, 0.0,
(double)xmax, (double)ymax, 0.0,
(double)xmin, (double)ymax, 0.0,
(double)xmin, (double)ymin, 0.0);
%}
END
|