1 /*******************************************************************************
2 *
3 * McXtrace, X-ray tracing package
4 *         Copyright (C), All rights reserved
5 *         Risoe National Laboratory, Roskilde, Denmark
6 *         Institut Laue Langevin, Grenoble, France
7 *
8 * Library: share/shadow-lib.c
9 *
10 * %Identification
11 * Written by: Andrea Prodi
12 * Date:   November 21, 2011
13 * Origin: Risoe/ILL
14 * Release: McXtrace 0.1
15 * Version: $Revision$
16 *
17 * It handles the way Shadow parses parameters.
18 * Functions are used by the Shadow_input and Shadow_output
19 * components. Embedded within instrument if MC_EMBEDDED_RUNTIME is defined.
20 *
21 * Usage: within SHARE
22 * %include "shadow-lib"
23 *
24 * $Id$
25 *
26 * $Log: shadow-lib.c,v $
27 *******************************************************************************/
28 
29 #ifndef SHADOW_LIB_H
30 #error McStas : please import this library with %include "shadow-lib"
31 #endif
32 
33 /********************************************************************************************/
34 #ifdef WIN32
35 # include <fcntl.h>
36 # include <io.h>
37 # define cSlash '\\'
38 #else
39 # define cSlash '/'
40 #endif
41 
42 #define KM2S 1.0e+08     /* Convert k[1/AA](McXtrace) to k[1/cm](Shadow) */
43 #define KS2M 1.0e-08     /* Convert k[1/cm](Shadow) to k[1/AA](McXtrace) */
44 
45 long     BufferSize;       /* size of the neutron input and output buffer */
46 char*    ParDirectory;     /* parameter directory */
47 int      ParDirectoryLength;
48 
49 /* Convert McXtrace state parameters to SHADOW x-ray structure. */
50 
mcxtrace2shadow(double x,double y,double z,double kx,double ky,double kz,double phi,double t,double Ex,double Ey,double Ez,double p,int rayindex)51 Ray mcxtrace2shadow(double x, double y, double z,
52           double kx, double ky, double kz,
53           double phi,
54           double t,
55           double Ex, double Ey, double Ez,
56 	  double p, int rayindex)
57 {
58   double k;     /* ray's momentum */
59   Ray ray;        /* Shadow ray structure */
60 
61 /* Convert spatial coordinates */
62   ray.Position[0] = -x;
63   ray.Position[1] =  z;
64   ray.Position[2] =  y;
65 
66   k = sqrt(kx*kx + ky*ky + kz*kz);
67   if(k == 0.0)
68   {
69     fprintf(stderr, "Error: zero momentum! (mcxtrace2shadow)\n");
70     exit(1);
71   }
72 /* Convert momentum to unit direction vector */
73   ray.Kvector[0] = -kx/k;
74   ray.Kvector[1] =  kz/k;
75   ray.Kvector[2] =  ky/k;
76 /* Convert EM vector (s-) */
77   ray.EMvector[0] =  p;       /* Asx */
78   ray.EMvector[1] =  0.0;       /* Asy */
79   ray.EMvector[2] =  0.0;       /* Asz */
80 /* Convert aux data */
81   ray.Flag =  1;       /* Flag=1 ray passed;=0 ray absorbed */
82   ray.Wavenumber = KM2S * k;       /*   q [cm-1] */
83   ray.Rayindex = rayindex;       /* kk ray number */
84 /* Convert phases */
85   ray.Phase[0] = 0.00;       /* opd */
86   ray.Phase[1] =  phi;       /* Fs  */
87   ray.Phase[2] = 0.00;       /* Fp  */
88 /* Convert EM vector (p-), not used in McXtrace */
89   ray.APvector[0] = 0.00;       /* Apx */
90   ray.APvector[1] = 0.00;       /* Apy */
91   ray.APvector[2] = 0.00;       /* Apz */
92 
93   return ray;
94 }
95 
96 
97 /* Convert SHADOW state parameters to McXtrace x-ray structure. */
shadow2mcxtrace(Ray ray,double * x,double * y,double * z,double * kx,double * ky,double * kz,double * Ex,double * Ey,double * Ez,double * phi,double * t,double * p)98 void shadow2mcxtrace(Ray ray,
99        double *x, double *y, double *z,
100        double *kx, double *ky, double *kz,
101        double *Ex, double *Ey, double *Ez,
102        double *phi, double *t, double *p)
103 {
104 
105 /* Convert to McXtrace data structure  */
106 
107 /* Convert spatial coordinates */
108   *x = -ray.Position[0];
109   *y = ray.Position[2];
110   *z = ray.Position[1];
111 
112   if(ray.Wavenumber == 0.0)
113   {
114     fprintf(stderr, "Error: zero wavelength! (shadow2mcxtrace: )\n");
115     exit(1);
116   }
117 /* Convert unit direction vector to momentum components*/
118   *kx = -KS2M * ray.Wavenumber*ray.Kvector[0];
119   *ky =  KS2M * ray.Wavenumber*ray.Kvector[2];
120   *kz =  KS2M * ray.Wavenumber*ray.Kvector[1];
121 /* Convert EM vector (s-) */
122   *Ex=ray.EMvector[0];         /* Asx */
123   *Ey=ray.EMvector[1];         /* Asy */
124   *Ez=ray.EMvector[2];         /* Asz */
125 
126 /* if (ncol == 13) { */
127 /* 		ray.Phase[0] = ray_vec[12]; */
128 /* 		*phi=ray.Phase[1]; */
129 /* 	    } else if (ncol == 18) { */
130 /* 		ray.Phase[0] = ray_vec[12]; */
131 /* 		ray.Phase[1] = ray_vec[13]; */
132 /* 		ray.Phase[2] = ray_vec[14]; */
133 
134   *phi=ray.Phase[1];
135 
136 /* 		/\* Read EM vector (p) *\/ */
137 /* 		ray.APvector[0] = ray_vec[15]; */
138 /* 		ray.APvector[1] = ray_vec[16]; */
139 /* 		ray.APvector[2] = ray_vec[17]; */
140 /* 	    } */
141 
142   *t = 0.0;               /* no time info under Shadow */
143   *p = ray.EMvector[0];   /* x-ray  weight */
144 }
145 
setParDirectory(char * a)146 void setParDirectory (char *a) {
147   int len;
148   if ((len = strlen(a))) {
149     /* last character should be a slash */
150     if (a[len-1] == cSlash) {
151       memcpy ((ParDirectory = (char *) malloc(len+1)), a, len);
152     } else {
153       memcpy ((ParDirectory = (char *) malloc(len+2)), a, len);
154       ParDirectory[len++] = cSlash;
155       ParDirectory[len] = 0;
156     }
157     ParDirectoryLength = len;
158   }
159 }
160 
161 /* Adding path of parameter directory to file name */
FullParName(char * filename)162 char* FullParName(char* filename)
163 {
164   int sel=0;
165   char *res, *a=NULL;
166   int alen, blen;
167 
168   if (filename == 0)
169      return 0;
170 
171   /* Do not change an absolute path. */
172  #ifdef WIN32
173   /* we consider a filename with : as absolute */
174   if (strstr(filename, ":")) sel = -1;
175  #else
176   if (filename[0] == '/') sel = -1;
177  #endif
178   if (sel == -1) {
179     alen = 0;
180   } else if (sel == 0) {
181     a = ParDirectory;
182     alen = ParDirectoryLength;
183   }
184   blen = strlen(filename);
185   if ((res = (char *) malloc(alen+blen+1)))
186   { if (alen)
187       strcpy(res, a);
188     else
189       strcpy(res,"");
190     strcat(res, filename);
191   }
192   return res;
193 }
194 
195 /* end of shadow-lib.c */