@@ -52,38 +52,38 @@ SETTING PARAMETERS (E0=0, dE=0, lambda0=0,dlambda=0, phase=0, randomphase=1, Ee=
5252
5353SHARE
5454%{
55- // %include "read_table-lib"
55+ // %include "read_table-lib"
5656
57- #ifndef MCCODE_BESSELKNU
58- #define MCCODE_BESSELKNU 1
57+ #ifndef MCCODE_BESSELKNU
58+ #define MCCODE_BESSELKNU 1
5959
60- #pragma acc routine seq
61- double besselKnu(double nu, double x){
62- const double h=0.5;
63- double KK=0,dK;
64- int r=0;
65- const int maxiter=1000;
66- KK=exp(-x)/2.0;
67- dK=1;
68- while (dK>DBL_EPSILON && r<maxiter){
60+ #pragma acc routine seq
61+ double
62+ besselKnu (double nu, double x) {
63+ const double h = 0.5;
64+ double KK = 0, dK;
65+ int r = 0;
66+ const int maxiter = 1000;
67+ KK = exp (-x) / 2.0;
68+ dK = 1;
69+ while (dK > DBL_EPSILON && r < maxiter) {
6970 r++;
70- dK= exp(-x* cosh(r* h))* cosh(nu*r* h);
71- KK+= dK;
71+ dK = exp (-x * cosh (r * h)) * cosh (nu * r * h);
72+ KK += dK;
7273 }
73- #ifndef OPENACC
74- if (r>= maxiter) {
75- fprintf(stderr,"Warning: Maximum number of iterations exceeded in besselKnu(%g,%g).\n",nu,x);
74+ #ifndef OPENACC
75+ if (r >= maxiter) {
76+ fprintf (stderr, "Warning: Maximum number of iterations exceeded in besselKnu(%g,%g).\n", nu, x);
7677 }
77- #endif
78- KK*= h;
78+ #endif
79+ KK *= h;
7980 return KK;
8081 }
81- #endif /*MCCODE_BESSELKNU*/
82-
83- #ifndef M_SQRT1_2
84- #define M_SQRT1_2 0.70710678118654752440
85- #endif
82+ #endif /*MCCODE_BESSELKNU*/
8683
84+ #ifndef M_SQRT1_2
85+ #define M_SQRT1_2 0.70710678118654752440
86+ #endif
8787%}
8888
8989DECLARE
@@ -103,163 +103,165 @@ INITIALIZE
103103
104104 // fprintf(stderr,"Warning (%s): Bending_magnet is an experimental component - testing is ongoing\n",NAME_CURRENT_COMP);
105105
106- if(B<= 0 || Ee<= 0 || Ie<=0 ) {
107- fprintf(stderr, "Error (%s): B, Ee, and Ie must all be >= 0. Found (%g %g %g). Aborting.\n",NAME_CURRENT_COMP,B, Ee,Ie);
108- exit(1);
106+ if (B <= 0 || Ee <= 0 || Ie <= 0) {
107+ fprintf (stderr, "Error (%s): B, Ee, and Ie must all be >= 0. Found (%g %g %g). Aborting.\n", NAME_CURRENT_COMP, B, Ee, Ie);
108+ exit (1);
109109 }
110110
111- if (sigex <0 || sigey<0) {
112- fprintf(stderr, "Error (%s): sigex and sigey must be > 0. Negative beam size isn't meaningful. Aborting.\n",NAME_CURRENT_COMP);
113- exit(1);
111+ if (sigex < 0 || sigey < 0) {
112+ fprintf (stderr, "Error (%s): sigex and sigey must be > 0. Negative beam size isn't meaningful. Aborting.\n", NAME_CURRENT_COMP);
113+ exit (1);
114114 }
115- if (dist<=0) {
116- fprintf(stderr,"Error (%s): Target undefined.\n",NAME_CURRENT_COMP);
117- exit(1);
115+ if (dist <= 0) {
116+ fprintf (stderr, "Error (%s): Target undefined.\n", NAME_CURRENT_COMP);
117+ exit (1);
118118 }
119119
120120 /*compute gamma*/
121- gamma= (Ee* 1e9)/ (MELECTRON/ CELE* M_C* M_C);/*the extra CELE is to convert to eV*/
122- gamma2= gamma* gamma;
123- igamma= 1.0/ gamma;
121+ gamma = (Ee * 1e9) / (MELECTRON / CELE * M_C * M_C); /*the extra CELE is to convert to eV*/
122+ gamma2 = gamma * gamma;
123+ igamma = 1.0 / gamma;
124124
125- //printf("Bending_magnet (%s): gamma=%g, divergence is 1/gamma=%g rad.\n",NAME_CURRENT_COMP,gamma,igamma);
125+ // printf("Bending_magnet (%s): gamma=%g, divergence is 1/gamma=%g rad.\n",NAME_CURRENT_COMP,gamma,igamma);
126126 /*compute characteristic energy in keV*/
127- double Ec= 0.665*Ee*Ee* B;
128- //double Ec=1.5*gamma2*HBAR*CELE*B/MELECTRON *1e-3; /*check units on this one. The 1e-3 factor is because energy is assumed to be in keV*/
127+ double Ec = 0.665 * Ee * Ee * B;
128+ // double Ec=1.5*gamma2*HBAR*CELE*B/MELECTRON *1e-3; /*check units on this one. The 1e-3 factor is because energy is assumed to be in keV*/
129129 /*We normally do computations in k so use that for transfer*/
130- kc= E2K* Ec;
130+ kc = E2K * Ec;
131131
132- s1x= sqrt(sigex* sigex + igamma* igamma* dist* dist);
133- s1y= sqrt(sigey* sigey + igamma* igamma* dist* dist);
134- p0= 1.0/ mcget_ncount();
132+ s1x = sqrt (sigex * sigex + igamma * igamma * dist * dist);
133+ s1y = sqrt (sigey * sigey + igamma * igamma * dist * dist);
134+ p0 = 1.0 / mcget_ncount ();
135135%}
136136
137137
138138TRACE
139139%{
140140
141- double xx,yy,x1,y1,z1;
142- double k,e, l;
143- double F1= 1.0;
144- double dx,dy,dz;
145-
141+ double xx, yy, x1, y1, z1;
142+ double k, e, l;
143+ double F1 = 1.0;
144+ double dx, dy, dz;
145+
146146 // initial source area
147- xx= randnorm();
148- yy= randnorm();
149- x=xx* sigex;
150- y=yy* sigey;
151- z= 0;
147+ xx = randnorm ();
148+ yy = randnorm ();
149+ x = xx * sigex;
150+ y = yy * sigey;
151+ z = 0;
152152
153153 // Gaussian distribution at origin
154- p= p0;/*initial weight is p0*/
155- if (E0){
156- if(!dE){
157- e= E0;
158- }else {
159- e= randpm1()* dE + E0;
154+ p = p0; /*initial weight is p0*/
155+ if (E0) {
156+ if (!dE) {
157+ e = E0;
158+ } else {
159+ e = randpm1 () * dE + E0;
160160 }
161- k= E2K* e;
162- }else if (lambda0){
163- if (!dlambda){
164- l= lambda0;
165- }else{
166- l= randpm1()* dlambda + lambda0;
161+ k = E2K * e;
162+ } else if (lambda0) {
163+ if (!dlambda) {
164+ l = lambda0;
165+ } else {
166+ l = randpm1 () * dlambda + lambda0;
167167 }
168- k=(2* M_PI/ l);
168+ k = (2 * M_PI / l);
169169 }
170170 // targeted area calculation
171- if (focus_xw){
172- if (!gauss_t){
171+ if (focus_xw) {
172+ if (!gauss_t) {
173173 /*sample uniformly but adjust weight*/
174- x1= randpm1()* focus_xw/ 2.0;
175- p*= exp(-(x1* x1)/ (2.0* s1x* s1x));
176- }else {
174+ x1 = randpm1 () * focus_xw / 2.0;
175+ p *= exp (-(x1 * x1) / (2.0 * s1x * s1x));
176+ } else {
177177 do {
178- x1= randnorm()* s1x;
179- }while (focus_xw!= 0 && fabs(x1)> focus_xw/ 2.0);
178+ x1 = randnorm () * s1x;
179+ } while (focus_xw != 0 && fabs (x1) > focus_xw / 2.0);
180180 /*adjust for restricted sampling window*/
181- p*= erf(focus_xw* 0.5* M_SQRT1_2/ s1x);
181+ p *= erf (focus_xw * 0.5 * M_SQRT1_2 / s1x);
182182 }
183- }else{
184- x1= randnorm()* igamma;
183+ } else {
184+ x1 = randnorm () * igamma;
185185 }
186- if (focus_yh){
187- if (!gauss_t){
186+ if (focus_yh) {
187+ if (!gauss_t) {
188188 /*sample uniformly but adjust weight*/
189- y1= randpm1()* focus_yh/ 2.0;
190- p*= exp(-(y1* y1)/ (2.0* s1y* s1y));
191- }else {
189+ y1 = randpm1 () * focus_yh / 2.0;
190+ p *= exp (-(y1 * y1) / (2.0 * s1y * s1y));
191+ } else {
192192 do {
193- y1= randnorm()* s1y;
194- }while (fabs(y1)> focus_yh/ 2.0);
193+ y1 = randnorm () * s1y;
194+ } while (fabs (y1) > focus_yh / 2.0);
195195 /*adjust for restricted sampling window*/
196- p*= erf(focus_yh* 0.5* M_SQRT1_2/ s1y);
196+ p *= erf (focus_yh * 0.5 * M_SQRT1_2 / s1y);
197197 }
198- }else{
199- y1= randnorm()* igamma;
198+ } else {
199+ y1 = randnorm () * igamma;
200200 }
201- z1=dist;
202- dx=x1-x;
203- dy=y1-y;
204- dz=sqrt(dx*dx+dy*dy+dist*dist);
201+ z1 = dist;
202+ dx = x1 - x;
203+ dy = y1 - y;
204+ dz = sqrt (dx * dx + dy * dy + dist * dist);
205+
206+ kx = (k * dx) / dz;
207+ ky = (k * dy) / dz;
208+ kz = (k * dist) / dz;
205209
206- kx=(k*dx)/dz;
207- ky=(k*dy)/dz;
208- kz=(k*dist)/dz;
209-
210- /*spectral strength of radiation is given by Patterson*/
211- double k_kc=k/kc;
212- double K2_3=besselKnu(0.666666666666666666666666667,k_kc*0.5);
213- p*=1.33e13*Ee*Ee*Ie* k_kc*k_kc*K2_3*K2_3;
214- //p*=ALPHA/(M_PI*M_PI)*gamma2*Ie/CELE* 1e-4 * 0.75 *k_kc*k_kc*K2_3*K2_3;
215-
210+ /*spectral strength of radiation is given by Patterson*/
211+ double k_kc = k / kc;
212+ double K2_3 = besselKnu (0.666666666666666666666666667, k_kc * 0.5);
213+ p *= 1.33e13 * Ee * Ee * Ie * k_kc * k_kc * K2_3 * K2_3;
214+ // p*=ALPHA/(M_PI*M_PI)*gamma2*Ie/CELE* 1e-4 * 0.75 *k_kc*k_kc*K2_3*K2_3;
216215
217216 /*randomly pick phase*/
218- if (randomphase){
219- phi= rand01()*2* M_PI;
220- }else{
221- phi= phase;
217+ if (randomphase) {
218+ phi = rand01 () * 2 * M_PI;
219+ } else {
220+ phi = phase;
222221 }
223222
224223 /*set polarization vector*/
225- Ex=0;Ey=0;Ez=0;
224+ Ex = 0;
225+ Ey = 0;
226+ Ez = 0;
226227%}
227228
228229MCDISPLAY
229230%{
230-
231231
232- double radius,D;
233- radius=3.3*Ee/B;;
234- D=0.1;
235- double x0,x1,z0,z1;
236- const double phimin=-2.0*DEG2RAD, phimax=2.0*DEG2RAD;
237- double phi=phimin,dphi;
238- dphi=(phimax-phimin)/32;
239- while (phi<phimax){
240- x0=radius*(1-cos(phi));
241- x1=radius*(1-cos(phi+dphi));
242- z0=radius*sin(phi);
243- z1=radius*sin(phi+dphi);
244- line(x0,0.0,z0,x1,0.0,z1);
245- phi+=dphi;
232+
233+ double radius, D;
234+ radius = 3.3 * Ee / B;
235+ ;
236+ D = 0.1;
237+ double x0, x1, z0, z1;
238+ const double phimin = -2.0 * DEG2RAD, phimax = 2.0 * DEG2RAD;
239+ double phi = phimin, dphi;
240+ dphi = (phimax - phimin) / 32;
241+ while (phi < phimax) {
242+ x0 = radius * (1 - cos (phi));
243+ x1 = radius * (1 - cos (phi + dphi));
244+ z0 = radius * sin (phi);
245+ z1 = radius * sin (phi + dphi);
246+ line (x0, 0.0, z0, x1, 0.0, z1);
247+ phi += dphi;
246248 }
247249
248- line(0.0,0.0,0.0, D*sin(igamma), 0.0, D);
249- line(0.0,0.0,0.0,-D*sin(igamma), 0.0, D);
250- line(0.0,0.0,0.0, 0.0, D*sin(igamma), D);
251- line(0.0,0.0,0.0, 0.0,-D*sin(igamma), D);
252-
253- phi =-igamma;
254- dphi= 2.0* igamma/ 32;
255- while(phi< igamma){
256- x0=D* sin(phi);
257- x1=D* sin(phi+ dphi);
258- z0=D* cos(phi);
259- z1=D* cos(phi+ dphi);
260- line(x0,0.0,z0,x1,0.0,z1);
261- line(0.0,x0,z0,0.0,x1,z1);
262- phi+= dphi;
250+ line (0.0, 0.0, 0.0, D* sin (igamma), 0.0, D);
251+ line (0.0, 0.0, 0.0, -D* sin (igamma), 0.0, D);
252+ line (0.0, 0.0, 0.0, 0.0, D* sin (igamma), D);
253+ line (0.0, 0.0, 0.0, 0.0, -D* sin (igamma), D);
254+
255+ phi = -igamma;
256+ dphi = 2.0 * igamma / 32;
257+ while (phi < igamma) {
258+ x0 = D * sin (phi);
259+ x1 = D * sin (phi + dphi);
260+ z0 = D * cos (phi);
261+ z1 = D * cos (phi + dphi);
262+ line (x0, 0.0, z0, x1, 0.0, z1);
263+ line (0.0, x0, z0, 0.0, x1, z1);
264+ phi += dphi;
263265 }
264266%}
265267
0 commit comments