1 | /** |
---|
2 | * model for NR |
---|
3 | */ |
---|
4 | // The original code, of which work was not DANSE funded, |
---|
5 | // was provided by J. Cho. |
---|
6 | #include <math.h> |
---|
7 | #include "refl.h" |
---|
8 | #include "libmultifunc/librefl.h" |
---|
9 | #include <stdio.h> |
---|
10 | #include <stdlib.h> |
---|
11 | |
---|
12 | #define lamda 4.62 |
---|
13 | |
---|
14 | |
---|
15 | double re_kernel(double dp[], double q) { |
---|
16 | int n = dp[0]; |
---|
17 | int i,j,fun_type[n+2]; |
---|
18 | |
---|
19 | double scale = dp[1]; |
---|
20 | double thick_inter_sub = dp[2]; |
---|
21 | double sld_sub = dp[4]; |
---|
22 | double sld_super = dp[5]; |
---|
23 | double background = dp[6]; |
---|
24 | |
---|
25 | double sld[n+2],thick_inter[n+2],thick[n+2],total_thick; |
---|
26 | double nsl=21.0; //nsl = Num_sub_layer: |
---|
27 | int n_s; |
---|
28 | double sld_i,sldim_i,dz,phi,R,ko2; |
---|
29 | double sign,erfunc, fun; |
---|
30 | double pi; |
---|
31 | |
---|
32 | fun_type[0] = dp[3]; |
---|
33 | for (i =1; i<=n; i++){ |
---|
34 | sld[i] = dp[i+6]; |
---|
35 | thick_inter[i]= dp[i+16]; |
---|
36 | thick[i] = dp[i+26]; |
---|
37 | fun_type[i] = dp[i+36]; |
---|
38 | total_thick += thick[i] + thick_inter[i]; |
---|
39 | } |
---|
40 | sld[0] = sld_sub; |
---|
41 | sld[n+1] = sld_super; |
---|
42 | |
---|
43 | thick[0] = total_thick/5.0; |
---|
44 | thick[n+1] = total_thick/5.0; |
---|
45 | thick_inter[0] = thick_inter_sub; |
---|
46 | thick_inter[n+1] = 0.0; |
---|
47 | |
---|
48 | complex inv_n,phi1,alpha,alpha2,kn,fnm,fnp,rn,Xn,nn,nn2,an,nnp1,one,zero,two,n_sub,n_sup,knp1,Xnp1; |
---|
49 | pi = 4.0*atan(1.0); |
---|
50 | one = cassign(1.0,0.0); |
---|
51 | //zero = cassign(0.0,0.0); |
---|
52 | two= cassign(0.0,-2.0); |
---|
53 | |
---|
54 | //Checking if floor is available. |
---|
55 | //no imaginary sld inputs in this function yet |
---|
56 | n_sub=cassign(1.0-sld_sub*pow(lamda,2.0)/(2.0*pi),0.0); |
---|
57 | n_sup=cassign(1.0-sld_super*pow(lamda,2.0)/(2.0*pi),0.0); |
---|
58 | ko2 = pow(2.0*pi/lamda,2.0); |
---|
59 | |
---|
60 | phi = asin(lamda*q/(4.0*pi)); |
---|
61 | phi1 = cdiv(rcmult(phi,one),n_sup); |
---|
62 | alpha = cmult(n_sup,ccos(phi1)); |
---|
63 | alpha2 = cmult(alpha,alpha); |
---|
64 | |
---|
65 | nnp1=n_sub; |
---|
66 | knp1=csqrt(rcmult(ko2,csub(cmult(nnp1,nnp1),alpha2))); //nnp1*ko*sin(phinp1) |
---|
67 | Xnp1=cassign(0.0,0.0); |
---|
68 | dz = 0.0; |
---|
69 | // iteration for # of layers +sub from the top |
---|
70 | for (i=1;i<=n+1; i++){ |
---|
71 | if (fun_type[i-1]==1) |
---|
72 | fun = 5; |
---|
73 | else |
---|
74 | fun = 0; |
---|
75 | //iteration for 9 sub-layers |
---|
76 | for (j=0;j<2;j++){ |
---|
77 | for (n_s=0;n_s<nsl; n_s++){ |
---|
78 | if (j==1){ |
---|
79 | if (i==n+1) |
---|
80 | break; |
---|
81 | dz = thick[i]; |
---|
82 | sld_i = sld[i]; |
---|
83 | } |
---|
84 | else{ |
---|
85 | dz = thick_inter[i-1]/nsl;//nsl; |
---|
86 | if (sld[i-1] == sld[i]){ |
---|
87 | sld_i = sld[i]; |
---|
88 | } |
---|
89 | else{ |
---|
90 | sld_i = intersldfunc(fun,nsl, n_s+0.5, 2.5, sld[i-1], sld[i]); |
---|
91 | } |
---|
92 | } |
---|
93 | nn = cassign(1.0-sld_i*pow(lamda,2.0)/(2.0*pi),0.0); |
---|
94 | nn2=cmult(nn,nn); |
---|
95 | |
---|
96 | kn=csqrt(rcmult(ko2,csub(nn2,alpha2))); //nn*ko*sin(phin) |
---|
97 | an=cexp(rcmult(dz,cmult(two,kn))); |
---|
98 | |
---|
99 | fnm=csub(kn,knp1); |
---|
100 | fnp=cadd(kn,knp1); |
---|
101 | rn=cdiv(fnm,fnp); |
---|
102 | Xn=cmult(an,cdiv(cadd(rn,Xnp1),cadd(one,cmult(rn,Xnp1)))); //Xn=an*((rn+Xnp1*anp1)/(1+rn*Xnp1*anp1)) |
---|
103 | |
---|
104 | Xnp1=Xn; |
---|
105 | knp1=kn; |
---|
106 | |
---|
107 | if (j==1) |
---|
108 | break; |
---|
109 | } |
---|
110 | } |
---|
111 | } |
---|
112 | R=pow(Xn.re,2.0)+pow(Xn.im,2.0); |
---|
113 | // This temperarily fixes the total reflection for Rfunction and linear. |
---|
114 | // ToDo: Show why it happens that Xn.re=0 and Xn.im >1! |
---|
115 | if (Xn.im == 0.0){ |
---|
116 | R=1.0; |
---|
117 | } |
---|
118 | R *= scale; |
---|
119 | R += background; |
---|
120 | |
---|
121 | return R; |
---|
122 | |
---|
123 | } |
---|
124 | /** |
---|
125 | * Function to evaluate NR function |
---|
126 | * @param pars: parameters |
---|
127 | * @param q: q-value |
---|
128 | * @return: function value |
---|
129 | */ |
---|
130 | double refl_analytical_1D(ReflParameters *pars, double q) { |
---|
131 | double dp[47]; |
---|
132 | |
---|
133 | dp[0] = pars->n_layers; |
---|
134 | dp[1] = pars->scale; |
---|
135 | dp[2] = pars->thick_inter0; |
---|
136 | dp[3] = pars->func_inter0; |
---|
137 | dp[4] = pars->sld_bottom0; |
---|
138 | dp[5] = pars->sld_medium; |
---|
139 | dp[6] = pars->background; |
---|
140 | |
---|
141 | dp[7] = pars->sld_flat1; |
---|
142 | dp[8] = pars->sld_flat2; |
---|
143 | dp[9] = pars->sld_flat3; |
---|
144 | dp[10] = pars->sld_flat4; |
---|
145 | dp[11] = pars->sld_flat5; |
---|
146 | dp[12] = pars->sld_flat6; |
---|
147 | dp[13] = pars->sld_flat7; |
---|
148 | dp[14] = pars->sld_flat8; |
---|
149 | dp[15] = pars->sld_flat9; |
---|
150 | dp[16] = pars->sld_flat10; |
---|
151 | |
---|
152 | dp[17] = pars->thick_inter1; |
---|
153 | dp[18] = pars->thick_inter2; |
---|
154 | dp[19] = pars->thick_inter3; |
---|
155 | dp[20] = pars->thick_inter4; |
---|
156 | dp[21] = pars->thick_inter5; |
---|
157 | dp[22] = pars->thick_inter6; |
---|
158 | dp[23] = pars->thick_inter7; |
---|
159 | dp[24] = pars->thick_inter8; |
---|
160 | dp[25] = pars->thick_inter9; |
---|
161 | dp[26] = pars->thick_inter10; |
---|
162 | |
---|
163 | dp[27] = pars->thick_flat1; |
---|
164 | dp[28] = pars->thick_flat2; |
---|
165 | dp[29] = pars->thick_flat3; |
---|
166 | dp[30] = pars->thick_flat4; |
---|
167 | dp[31] = pars->thick_flat5; |
---|
168 | dp[32] = pars->thick_flat6; |
---|
169 | dp[33] = pars->thick_flat7; |
---|
170 | dp[34] = pars->thick_flat8; |
---|
171 | dp[35] = pars->thick_flat9; |
---|
172 | dp[36] = pars->thick_flat10; |
---|
173 | |
---|
174 | dp[37] = pars->func_inter1; |
---|
175 | dp[38] = pars->func_inter2; |
---|
176 | dp[39] = pars->func_inter3; |
---|
177 | dp[40] = pars->func_inter4; |
---|
178 | dp[41] = pars->func_inter5; |
---|
179 | dp[42] = pars->func_inter6; |
---|
180 | dp[43] = pars->func_inter7; |
---|
181 | dp[44] = pars->func_inter8; |
---|
182 | dp[45] = pars->func_inter9; |
---|
183 | dp[46] = pars->func_inter10; |
---|
184 | |
---|
185 | return re_kernel(dp, q); |
---|
186 | } |
---|
187 | |
---|
188 | /** |
---|
189 | * Function to evaluate NR function |
---|
190 | * @param pars: parameters of NR |
---|
191 | * @param q: q-value |
---|
192 | * @return: function value |
---|
193 | */ |
---|
194 | double refl_analytical_2D(ReflParameters *pars, double q, double phi) { |
---|
195 | return refl_analytical_1D(pars,q); |
---|
196 | } |
---|
197 | |
---|
198 | double refl_analytical_2DXY(ReflParameters *pars, double qx, double qy){ |
---|
199 | return refl_analytical_1D(pars,sqrt(qx*qx+qy*qy)); |
---|
200 | } |
---|