[c97724e] | 1 | """ |
---|
[d459d4e] | 2 | Conversion of scattering cross section from SANS (I(q), or rather, ds/dO) in absolute |
---|
| 3 | units (cm-1)into SESANS correlation function G using a Hankel transformation, then converting |
---|
| 4 | the SESANS correlation function into polarisation from the SESANS experiment |
---|
[c97724e] | 5 | |
---|
[d459d4e] | 6 | Everything is in units of metres except specified otherwise (NOT TRUE!!!) |
---|
| 7 | Everything is in conventional units (nm for spin echo length) |
---|
[c97724e] | 8 | |
---|
| 9 | Wim Bouwman (w.g.bouwman@tudelft.nl), June 2013 |
---|
| 10 | """ |
---|
| 11 | |
---|
| 12 | from __future__ import division |
---|
| 13 | |
---|
| 14 | import numpy as np |
---|
| 15 | from numpy import pi, exp |
---|
| 16 | from scipy.special import jv as besselj |
---|
[02e70ff] | 17 | #import direct_model.DataMixin as model |
---|
| 18 | |
---|
[09ebe8c] | 19 | def make_q(q_max, Rmax): |
---|
[384d114] | 20 | r""" |
---|
[09ebe8c] | 21 | Return a $q$ vector suitable for SESANS covering from $2\pi/ (10 R_{\max})$ |
---|
[d459d4e] | 22 | to $q_max$. This is the integration range of the Hankel transform; bigger range and |
---|
| 23 | more points makes a better numerical integration. |
---|
| 24 | Smaller q_min will increase reliable spin echo length range. |
---|
| 25 | Rmax is the "radius" of the largest expected object and can be set elsewhere. |
---|
| 26 | q_max is determined by the acceptance angle of the SESANS instrument. |
---|
[09ebe8c] | 27 | """ |
---|
[a154ad16] | 28 | from sas.sascalc.data_util.nxsunit import Converter |
---|
| 29 | |
---|
[c97724e] | 30 | q_min = dq = 0.1 * 2*pi / Rmax |
---|
[a154ad16] | 31 | return np.arange(q_min, Converter("1/A")(q_max[0], units=q_max[1]), dq) |
---|
[02e70ff] | 32 | |
---|
[a06430c] | 33 | def make_all_q(data): |
---|
[d459d4e] | 34 | """ |
---|
| 35 | Return a $q$ vector suitable for calculating the total scattering cross section for |
---|
| 36 | calculating the effect of finite acceptance angles on Time of Flight SESANS instruments. |
---|
| 37 | If no acceptance is given, or unwanted (set "unwanted" flag in paramfile), no all_q vector is needed. |
---|
| 38 | If the instrument has a rectangular acceptance, 2 all_q vectors are needed. |
---|
| 39 | If the instrument has a circular acceptance, 1 all_q vector is needed |
---|
| 40 | |
---|
| 41 | """ |
---|
| 42 | if not data.has_no_finite_acceptance: |
---|
[02e70ff] | 43 | return [] |
---|
[d459d4e] | 44 | elif data.has_yz_acceptance(data): |
---|
[02e70ff] | 45 | # compute qx, qy |
---|
| 46 | Qx, Qy = np.meshgrid(qx, qy) |
---|
| 47 | return [Qx, Qy] |
---|
| 48 | else: |
---|
| 49 | # else only need q |
---|
[d459d4e] | 50 | # data.has_z_acceptance |
---|
[02e70ff] | 51 | return [q] |
---|
| 52 | |
---|
| 53 | def transform(data, q_calc, Iq_calc, qmono, Iq_mono): |
---|
[d459d4e] | 54 | """ |
---|
| 55 | Decides which transform type is to be used, based on the experiment data file contents (header) |
---|
| 56 | (2016-03-19: currently controlled from parameters script) |
---|
| 57 | nqmono is the number of q vectors to be used for the detector integration |
---|
| 58 | """ |
---|
[02e70ff] | 59 | nqmono = len(qmono) |
---|
| 60 | if nqmono == 0: |
---|
| 61 | result = call_hankel(data, q_calc, Iq_calc) |
---|
| 62 | elif nqmono == 1: |
---|
| 63 | q = qmono[0] |
---|
| 64 | result = call_HankelAccept(data, q_calc, Iq_calc, q, Iq_mono) |
---|
| 65 | else: |
---|
| 66 | Qx, Qy = [qmono[0], qmono[1]] |
---|
| 67 | Qx = np.reshape(Qx, nqx, nqy) |
---|
| 68 | Qy = np.reshape(Qy, nqx, nqy) |
---|
| 69 | Iq_mono = np.reshape(Iq_mono, nqx, nqy) |
---|
| 70 | qx = Qx[0, :] |
---|
| 71 | qy = Qy[:, 0] |
---|
| 72 | result = call_Cosine2D(data, q_calc, Iq_calc, qx, qy, Iq_mono) |
---|
| 73 | |
---|
| 74 | return result |
---|
| 75 | |
---|
| 76 | def call_hankel(data, q_calc, Iq_calc): |
---|
| 77 | return hankel(data.x, data.lam * 1e-9, |
---|
| 78 | data.sample.thickness / 10, |
---|
| 79 | q_calc, Iq_calc) |
---|
| 80 | |
---|
| 81 | def call_HankelAccept(data, q_calc, Iq_calc, q_mono, Iq_mono): |
---|
| 82 | return hankel(data.x, data.lam * 1e-9, |
---|
| 83 | data.sample.thickness / 10, |
---|
| 84 | q_calc, Iq_calc) |
---|
| 85 | |
---|
[d459d4e] | 86 | def call_Cosine2D(data, q_calc, Iq_calc, qx, qy, Iq_mono): |
---|
[251b40b] | 87 | return hankel(data.x, data.y, data.lam * 1e-9, |
---|
[02e70ff] | 88 | data.sample.thickness / 10, |
---|
| 89 | q_calc, Iq_calc) |
---|
| 90 | |
---|
| 91 | def TotalScatter(model, parameters): #Work in progress!! |
---|
| 92 | # Calls a model with existing model parameters already in place, then integrate the product of q and I(q) from 0 to (4*pi/lambda) |
---|
| 93 | allq = np.linspace(0,4*pi/wavelength,1000) |
---|
[251b40b] | 94 | allIq = 1 |
---|
[02e70ff] | 95 | integral = allq*allIq |
---|
| 96 | |
---|
| 97 | |
---|
| 98 | |
---|
| 99 | def Cosine2D(wavelength, magfield, thickness, qy, qz, Iqy, Iqz, modelname): #Work in progress!! Needs to call model still |
---|
| 100 | #============================================================================== |
---|
| 101 | # 2D Cosine Transform if "wavelength" is a vector |
---|
| 102 | #============================================================================== |
---|
| 103 | #allq is the q-space needed to create the total scattering cross-section |
---|
| 104 | |
---|
| 105 | Gprime = np.zeros_like(wavelength, 'd') |
---|
| 106 | s = np.zeros_like(wavelength, 'd') |
---|
| 107 | sd = np.zeros_like(wavelength, 'd') |
---|
| 108 | Gprime = np.zeros_like(wavelength, 'd') |
---|
| 109 | f = np.zeros_like(wavelength, 'd') |
---|
[251b40b] | 110 | for i, wavelength_i in enumerate(wavelength): |
---|
| 111 | z = magfield*wavelength_i |
---|
| 112 | allq=np.linspace() #for calculating the Q-range of the scattering power integral |
---|
| 113 | allIq=np.linspace() # This is the model applied to the allq q-space. Needs to refference the model somehow |
---|
| 114 | alldq = (allq[1]-allq[0])*1e10 |
---|
| 115 | sigma[i]=wavelength[i]^2*thickness/2/pi*np.sum(allIq*allq*alldq) |
---|
| 116 | s[i]=1-exp(-sigma) |
---|
| 117 | for j, Iqy_j, qy_j in enumerate(qy): |
---|
| 118 | for k, Iqz_k, qz_k in enumerate(qz): |
---|
| 119 | Iq = np.sqrt(Iqy_j^2+Iqz_k^2) |
---|
| 120 | q = np.sqrt(qy_j^2 + qz_k^2) |
---|
| 121 | Gintegral = Iq*cos(z*Qz_k) |
---|
| 122 | Gprime[i] += Gintegral |
---|
| 123 | # sigma = wavelength^2*thickness/2/pi* allq[i]*allIq[i] |
---|
| 124 | # s[i] += 1-exp(Totalscatter(modelname)*thickness) |
---|
| 125 | # For now, work with standard 2-phase scatter |
---|
| 126 | |
---|
| 127 | |
---|
| 128 | sd[i] += Iq |
---|
| 129 | f[i] = 1-s[i]+sd[i] |
---|
| 130 | P[i] = (1-sd[i]/f[i])+1/f[i]*Gprime[i] |
---|
[02e70ff] | 131 | |
---|
| 132 | |
---|
| 133 | |
---|
| 134 | |
---|
| 135 | def HankelAccept(wavelength, magfield, thickness, q, Iq, theta, modelname): |
---|
| 136 | #============================================================================== |
---|
| 137 | # HankelTransform with fixed circular acceptance angle (circular aperture) for Time of Flight SESANS |
---|
| 138 | #============================================================================== |
---|
| 139 | #acceptq is the q-space needed to create limited acceptance effect |
---|
| 140 | SElength= wavelength*magfield |
---|
| 141 | G = np.zeros_like(SElength, 'd') |
---|
| 142 | threshold=2*pi*theta/wavelength |
---|
[251b40b] | 143 | for i, SElength_i in enumerate(SElength): |
---|
| 144 | allq=np.linspace() #for calculating the Q-range of the scattering power integral |
---|
| 145 | allIq=np.linspace() # This is the model applied to the allq q-space. Needs to refference the model somehow |
---|
| 146 | alldq = (allq[1]-allq[0])*1e10 |
---|
| 147 | sigma[i]=wavelength[i]^2*thickness/2/pi*np.sum(allIq*allq*alldq) |
---|
| 148 | s[i]=1-exp(-sigma) |
---|
| 149 | |
---|
| 150 | dq = (q[1]-q[0])*1e10 |
---|
| 151 | a = (x<threshold) |
---|
| 152 | acceptq = a*q |
---|
| 153 | acceptIq = a*Iq |
---|
| 154 | |
---|
| 155 | G[i] = np.sum(besselj(0, acceptq*SElength_i)*acceptIq*acceptq*dq) |
---|
| 156 | |
---|
| 157 | # G[i]=np.sum(integral) |
---|
| 158 | |
---|
| 159 | G *= dq*1e10*2*pi |
---|
| 160 | |
---|
| 161 | P = exp(thickness*wavelength**2/(4*pi**2)*(G-G[0])) |
---|
[02e70ff] | 162 | |
---|
[c97724e] | 163 | def hankel(SElength, wavelength, thickness, q, Iq): |
---|
[384d114] | 164 | r""" |
---|
[c97724e] | 165 | Compute the expected SESANS polarization for a given SANS pattern. |
---|
| 166 | |
---|
[384d114] | 167 | Uses the hankel transform followed by the exponential. The values for *zz* |
---|
| 168 | (or spin echo length, or delta), wavelength and sample thickness should |
---|
| 169 | come from the dataset. $q$ should be chosen such that the oscillations |
---|
| 170 | in $I(q)$ are well sampled (e.g., $5 \cdot 2 \pi/d_{\max}$). |
---|
[c97724e] | 171 | |
---|
[384d114] | 172 | *SElength* [A] is the set of $z$ points at which to compute the |
---|
| 173 | Hankel transform |
---|
[c97724e] | 174 | |
---|
| 175 | *wavelength* [m] is the wavelength of each individual point *zz* |
---|
| 176 | |
---|
| 177 | *thickness* [cm] is the sample thickness. |
---|
| 178 | |
---|
[384d114] | 179 | *q* [A$^{-1}$] is the set of $q$ points at which the model has been |
---|
| 180 | computed. These should be equally spaced. |
---|
[c97724e] | 181 | |
---|
[384d114] | 182 | *I* [cm$^{-1}$] is the value of the SANS model at *q* |
---|
[c97724e] | 183 | """ |
---|
[190fc2b] | 184 | G = np.zeros_like(SElength, 'd') |
---|
[02e70ff] | 185 | #============================================================================== |
---|
| 186 | # Hankel Transform method if "wavelength" is a scalar; mono-chromatic SESANS |
---|
| 187 | #============================================================================== |
---|
[190fc2b] | 188 | for i, SElength_i in enumerate(SElength): |
---|
| 189 | integral = besselj(0, q*SElength_i)*Iq*q |
---|
| 190 | G[i] = np.sum(integral) |
---|
[384d114] | 191 | |
---|
| 192 | # [m^-1] step size in q, needed for integration |
---|
[190fc2b] | 193 | dq = (q[1]-q[0])*1e10 |
---|
[384d114] | 194 | |
---|
| 195 | # integration step, convert q into [m**-1] and 2 pi circle integration |
---|
| 196 | G *= dq*1e10*2*pi |
---|
| 197 | |
---|
[c97724e] | 198 | P = exp(thickness*wavelength**2/(4*pi**2)*(G-G[0])) |
---|
| 199 | |
---|
| 200 | return P |
---|