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