Skip to content

Linear

FieldScan

Source code in cmtj/utils/linear.py
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
class FieldScan:

    @staticmethod
    def _trig_compute(theta, phi) -> Tuple:
        """Compute trigonometric functions for theta and phi.
        :param theta: theta angle in [deg].
        :param phi: phi angle in [deg].
        :returns: trigonometric functions for theta and phi."""
        st = np.sin(np.deg2rad(theta))
        ct = np.cos(np.deg2rad(theta))
        sp = np.sin(np.deg2rad(phi))
        cp = np.cos(np.deg2rad(phi))
        return st, ct, sp, cp

    @staticmethod
    def angle2vector(theta, phi, amplitude=1) -> CVector:
        """Convert spherical coordinates to cartesian coordinates.
        :param theta: polar angle in degrees.
        :param phi: azimuthal angle in degrees.
        :param amplitude: amplitude of target vector.
        :returns: cartesian vector."""
        st, ct, sp, cp = FieldScan._trig_compute(theta, phi)
        return CVector(
            st * cp * amplitude,
            st * sp * amplitude,
            ct * amplitude,
        )

    @staticmethod
    def vector2angle(x, y, z) -> Tuple:
        """Convert cartesian coordinates to spherical coordinates.
        :param x: x coordinate of the vector.
        :param y: y coordinate of the vector.
        :param z: z coordinate of the vector.
        :returns (theta, phi, r)
        https://github.com/numpy/numpy/issues/5228
        """
        r = np.sqrt(x**2 + y**2 + z**2)
        theta = np.rad2deg(np.arctan2(np.sqrt(x**2 + y**2), z))
        phi = np.rad2deg(np.arctan2(y, x))
        return theta, phi, r

    @staticmethod
    def cvector2angle(vector: CVector) -> Tuple:
        """
        :param vector: cartesian vector.
        :returns (theta, phi, r)
        https://github.com/numpy/numpy/issues/5228
        """
        return FieldScan.vector2angle(vector.x, vector.y, vector.z)

    @staticmethod
    def amplitude_scan(
        start: float,
        stop: float,
        steps: int,
        theta: float,
        phi: float,
        back: bool = False,
    ) -> Tuple[np.ndarray, np.ndarray]:
        """
        Compute a linear magnitude sweep. Angles given in deg.
        :param start: start of the sweep
        :param stop: end of the sweep
        :param steps: number of steps
        :param theta: polar angle in deg.
        :param phi: azimuthal angle in deg.
        :returns: linear amplitude, field vectors
        """
        Hspan = np.linspace(start, stop, endpoint=True, num=steps)
        st, ct, sp, cp = FieldScan._trig_compute(theta, phi)
        Hx = st * cp * Hspan
        Hy = st * sp * Hspan
        Hz = ct * Hspan
        if back:
            forward = np.vstack((Hx, Hy, Hz)).T
            back = forward[::-1]
            return np.concatenate((Hspan, Hspan[::-1]),
                                  axis=0), np.concatenate((forward, back),
                                                          axis=0)
        return Hspan, np.vstack((Hx, Hy, Hz)).T

    @staticmethod
    def theta_scan(start: float, stop: float, steps: int, amplitude: float,
                   phi: float) -> Tuple[np.ndarray, np.ndarray]:
        """
        Compute a linear theta angle sweep. Angles given in deg.
        :param start: polar angle start of the sweep
        :param stop: polar angle end of the sweep
        :param steps: number of steps
        :param amplitude: amplitude of the scanned field.
        :param phi: azimuthal angle in deg.
        """
        theta_span = np.linspace(start, stop, endpoint=True, num=steps)
        st, ct, sp, cp = FieldScan._trig_compute(theta_span, phi)
        Hx = st * cp * amplitude
        Hy = st * sp * amplitude
        Hz = ct * amplitude
        return theta_span, np.vstack((Hx, Hy, Hz)).T

    @staticmethod
    def phi_scan(start: float, stop: float, steps: int, amplitude: float,
                 theta: float) -> Tuple[np.ndarray, np.ndarray]:
        """
        Compute a linear phi angle sweep. Angles given in deg.
        :param start: azimuthal angle start of the sweep
        :param stop: azimuthal angle end of the sweep
        :param steps: number of steps
        :param amplitude: amplitude of the scanned field
        :param theta: polar angle in deg.
        """
        phi_span = np.linspace(start, stop, endpoint=True, num=steps)
        st, ct, sp, cp = FieldScan._trig_compute(theta, phi_span)
        Hx = st * cp * amplitude
        Hy = st * sp * amplitude
        Hz = ct * amplitude * np.ones_like(Hy)
        return phi_span, np.vstack((Hx, Hy, Hz)).T

amplitude_scan(start, stop, steps, theta, phi, back=False) staticmethod

Compute a linear magnitude sweep. Angles given in deg.

Parameters:

Name Type Description Default
start float

start of the sweep

required
stop float

end of the sweep

required
steps int

number of steps

required
theta float

polar angle in deg.

required
phi float

azimuthal angle in deg.

required

Returns:

Type Description
Tuple[np.ndarray, np.ndarray]

linear amplitude, field vectors

Source code in cmtj/utils/linear.py
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
@staticmethod
def amplitude_scan(
    start: float,
    stop: float,
    steps: int,
    theta: float,
    phi: float,
    back: bool = False,
) -> Tuple[np.ndarray, np.ndarray]:
    """
    Compute a linear magnitude sweep. Angles given in deg.
    :param start: start of the sweep
    :param stop: end of the sweep
    :param steps: number of steps
    :param theta: polar angle in deg.
    :param phi: azimuthal angle in deg.
    :returns: linear amplitude, field vectors
    """
    Hspan = np.linspace(start, stop, endpoint=True, num=steps)
    st, ct, sp, cp = FieldScan._trig_compute(theta, phi)
    Hx = st * cp * Hspan
    Hy = st * sp * Hspan
    Hz = ct * Hspan
    if back:
        forward = np.vstack((Hx, Hy, Hz)).T
        back = forward[::-1]
        return np.concatenate((Hspan, Hspan[::-1]),
                              axis=0), np.concatenate((forward, back),
                                                      axis=0)
    return Hspan, np.vstack((Hx, Hy, Hz)).T

angle2vector(theta, phi, amplitude=1) staticmethod

Convert spherical coordinates to cartesian coordinates.

Parameters:

Name Type Description Default
theta

polar angle in degrees.

required
phi

azimuthal angle in degrees.

required
amplitude

amplitude of target vector.

1

Returns:

Type Description
CVector

cartesian vector.

Source code in cmtj/utils/linear.py
22
23
24
25
26
27
28
29
30
31
32
33
34
@staticmethod
def angle2vector(theta, phi, amplitude=1) -> CVector:
    """Convert spherical coordinates to cartesian coordinates.
    :param theta: polar angle in degrees.
    :param phi: azimuthal angle in degrees.
    :param amplitude: amplitude of target vector.
    :returns: cartesian vector."""
    st, ct, sp, cp = FieldScan._trig_compute(theta, phi)
    return CVector(
        st * cp * amplitude,
        st * sp * amplitude,
        ct * amplitude,
    )

cvector2angle(vector) staticmethod

Parameters:

Name Type Description Default
vector CVector

cartesian vector.

required

Returns:

Type Description
Tuple

//github.com/numpy/numpy/issues/5228

Source code in cmtj/utils/linear.py
50
51
52
53
54
55
56
57
@staticmethod
def cvector2angle(vector: CVector) -> Tuple:
    """
    :param vector: cartesian vector.
    :returns (theta, phi, r)
    https://github.com/numpy/numpy/issues/5228
    """
    return FieldScan.vector2angle(vector.x, vector.y, vector.z)

phi_scan(start, stop, steps, amplitude, theta) staticmethod

Compute a linear phi angle sweep. Angles given in deg.

Parameters:

Name Type Description Default
start float

azimuthal angle start of the sweep

required
stop float

azimuthal angle end of the sweep

required
steps int

number of steps

required
amplitude float

amplitude of the scanned field

required
theta float

polar angle in deg.

required
Source code in cmtj/utils/linear.py
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
@staticmethod
def phi_scan(start: float, stop: float, steps: int, amplitude: float,
             theta: float) -> Tuple[np.ndarray, np.ndarray]:
    """
    Compute a linear phi angle sweep. Angles given in deg.
    :param start: azimuthal angle start of the sweep
    :param stop: azimuthal angle end of the sweep
    :param steps: number of steps
    :param amplitude: amplitude of the scanned field
    :param theta: polar angle in deg.
    """
    phi_span = np.linspace(start, stop, endpoint=True, num=steps)
    st, ct, sp, cp = FieldScan._trig_compute(theta, phi_span)
    Hx = st * cp * amplitude
    Hy = st * sp * amplitude
    Hz = ct * amplitude * np.ones_like(Hy)
    return phi_span, np.vstack((Hx, Hy, Hz)).T

theta_scan(start, stop, steps, amplitude, phi) staticmethod

Compute a linear theta angle sweep. Angles given in deg.

Parameters:

Name Type Description Default
start float

polar angle start of the sweep

required
stop float

polar angle end of the sweep

required
steps int

number of steps

required
amplitude float

amplitude of the scanned field.

required
phi float

azimuthal angle in deg.

required
Source code in cmtj/utils/linear.py
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
@staticmethod
def theta_scan(start: float, stop: float, steps: int, amplitude: float,
               phi: float) -> Tuple[np.ndarray, np.ndarray]:
    """
    Compute a linear theta angle sweep. Angles given in deg.
    :param start: polar angle start of the sweep
    :param stop: polar angle end of the sweep
    :param steps: number of steps
    :param amplitude: amplitude of the scanned field.
    :param phi: azimuthal angle in deg.
    """
    theta_span = np.linspace(start, stop, endpoint=True, num=steps)
    st, ct, sp, cp = FieldScan._trig_compute(theta_span, phi)
    Hx = st * cp * amplitude
    Hy = st * sp * amplitude
    Hz = ct * amplitude
    return theta_span, np.vstack((Hx, Hy, Hz)).T

vector2angle(x, y, z) staticmethod

Convert cartesian coordinates to spherical coordinates.

Parameters:

Name Type Description Default
x

x coordinate of the vector.

required
y

y coordinate of the vector.

required
z

z coordinate of the vector.

required

Returns:

Type Description
Tuple

//github.com/numpy/numpy/issues/5228

Source code in cmtj/utils/linear.py
36
37
38
39
40
41
42
43
44
45
46
47
48
@staticmethod
def vector2angle(x, y, z) -> Tuple:
    """Convert cartesian coordinates to spherical coordinates.
    :param x: x coordinate of the vector.
    :param y: y coordinate of the vector.
    :param z: z coordinate of the vector.
    :returns (theta, phi, r)
    https://github.com/numpy/numpy/issues/5228
    """
    r = np.sqrt(x**2 + y**2 + z**2)
    theta = np.rad2deg(np.arctan2(np.sqrt(x**2 + y**2), z))
    phi = np.rad2deg(np.arctan2(y, x))
    return theta, phi, r