source: sasmodels/sasmodels/sesans.py @ e4e5e29

costrafo411
Last change on this file since e4e5e29 was e4e5e29, checked in by jhbakker, 7 years ago

include 2D cosine transform in sesans.py

  • Property mode set to 100644
File size: 3.9 KB
Line 
1"""
2Conversion of scattering cross section from SANS (I(q), or rather, ds/dO) in absolute
3units (cm-1)into SESANS correlation function G using a Hankel transformation, then converting
4the SESANS correlation function into polarisation from the SESANS experiment
5
6Everything is in units of metres except specified otherwise (NOT TRUE!!!)
7Everything is in conventional units (nm for spin echo length)
8
9Wim Bouwman (w.g.bouwman@tudelft.nl), June 2013
10"""
11
12from __future__ import division
13
14import numpy as np  # type: ignore
15from numpy import pi, exp  # type: ignore
16from scipy.special import j0
17
18class SesansTransform(object):
19    """
20    Spin-Echo SANS transform calculator.  Similar to a resolution function,
21    the SesansTransform object takes I(q) for the set of *q_calc* values and
22    produces a transformed dataset
23
24    *SElength* (A) is the set of spin-echo lengths in the measured data.
25
26    *zaccept* (1/A) is the maximum acceptance of scattering vector in the spin
27    echo encoding dimension (for ToF: Q of min(R) and max(lam)).
28
29    *Rmax* (A) is the maximum size sensitivity; larger radius requires more
30    computation time.
31    """
32    #: SElength from the data in the original data units; not used by transform
33    #: but the GUI uses it, so make sure that it is present.
34    q = None  # type: np.ndarray
35
36    #: q values to calculate when computing transform
37    q_calc = None  # type: np.ndarray
38
39    # transform arrays
40    _H = None  # type: np.ndarray
41    _H0 = None # type: np.ndarray
42
43    def __init__(self, z, SElength, zaccept, Rmax):
44        # type: (np.ndarray, float, float) -> None
45        #import logging; logging.info("creating SESANS transform")
46        self.q = z
47        # isoriented flag determines whether data is from an oriented sample or not, should be in the data or selectable in GUI.
48        #if self.isoriented==False
49        self._set_hankel(SElength, zaccept, Rmax)
50        #if self.isoriented==True
51        self._set_cosmat(SElength, zaccept, Rmax)
52
53    def apply(self, Iq):
54        if len(Iq.size) == 1: # if isotropic, do Hankel transform
55            # type: (np.ndarray) -> np.ndarray
56            G0 = np.dot(self._H0, Iq)
57            G = np.dot(self._H.T, Iq)
58            P = G - G0
59        elif len(Iq.size) == 2:
60            dq=self.q_calc[0]
61            G0 = sum(np.dot(self._cos0, Iq)*dq)
62            G = sum(np.dot(self._cosmat.T, Iq)*dq)
63            P = G - G0
64        return P
65
66    def _set_hankel(self, SElength, zaccept, Rmax):
67        # type: (np.ndarray, float, float) -> None
68        # Force float32 arrays, otherwise run into memory problems on some machines
69        SElength = np.asarray(SElength, dtype='float32')
70
71        #Rmax = #value in text box somewhere in FitPage?
72        q_max = 2*pi / (SElength[1] - SElength[0])
73        q_min = 0.1 * 2*pi / (np.size(SElength) * SElength[-1])
74        q = np.arange(q_min, q_max, q_min, dtype='float32')
75        dq = q_min
76
77        H0 = np.float32(dq/(2*pi)) * q
78
79        repq = np.tile(q, (SElength.size, 1)).T
80        repSE = np.tile(SElength, (q.size, 1))
81        H = np.float32(dq/(2*pi)) * j0(repSE*repq) * repq
82
83        self.q_calc = q
84        self._H, self._H0 = H, H0
85
86    def _set_cosmat(self, SElength, zaccept, Rmax):
87        # type: (np.ndarray, float, float) -> None
88        # Force float32 arrays, otherwise run into memory problems on some machines
89        SElength = np.asarray(SElength, dtype='float32')
90        #qymax and qzmax depend on detector shape
91        qzmax=
92        # Rmax = #value in text box somewhere in FitPage?
93        q_max = 2 * pi / (SElength[1] - SElength[0])
94        q_min = 0.1 * 2 * pi / (np.size(SElength) * SElength[-1])
95
96        q = np.arange(q_min, q_max, q_min, dtype='float32')
97        dq = q_min
98
99        cos0 = np.float32(dq / (2 * pi))
100
101        repq = np.tile(q, (SElength.size, 1)).T
102        repSE = np.tile(SElength, (q.size, 1))
103        cosmat = np.float32(dq / (2 * pi)) * np.cos(repSE * repq)
104
105        self.q_calc = q
106        self._cosmat, self._cos0 = cosmat, cos0
Note: See TracBrowser for help on using the repository browser.