1 2 3 4 5 6 7 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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
"""
.. currentmodule:: skrf.constants
========================================
constants (:mod:`skrf.constants`)
========================================
This module contains constants, numerical approximations, and unit conversions
.. data:: c
Velocity of light constant (from scipy)
.. data:: INF
A very very large value (1e99)
.. data:: ONE
1 + epsilon where epsilon is small. Used to avoid numerical error.
.. data:: ZERO
0 + epsilon where epsilon is small. Used to avoid numerical error.
.. data:: LOG_OF_NEG
Very low but minus infinity value for numerical purposes.
.. data:: K_BOLTZMANN
Boltzmann constant (1.38064852e-23)
.. data:: S_DEFINITIONS
S-parameter definition labels:
- 'power' for power-waves definition,
- 'pseudo' for pseudo-waves definition.
- 'traveling' corresponds to the initial implementation.
.. data:: S_DEF_DEFAULT
Default S-parameter definition: 'power', for power-wave definition.
.. autodata:: S_DEF_HFSS_DEFAULT
Default ANSYS HFSS S-parameter definition: 'traveling'
.. autodata:: SweepTypeT
.. autodata:: FrequencyUnitT
Frequency units: "Hz", "kHz", "MHz", "GHz", "THz" (case-insensitive).
.. autosummary::
:toctree: generated/
to_meters
"""
from __future__ import annotations
from collections.abc import Sequence
from numbers import Number
from typing import Literal, get_args
import numpy as np
from scipy.constants import c, inch, mil
# used as substitutes to handle mathematical singularities.
INF = 1e99
"""
High but not infinite value for numerical purposes.
"""
ALMOST_ZERO = 1e-12
"""
Very tiny but not zero value to handle mathematical singularities.
"""
ZERO = 1e-4
"""
A very small values, often used for numerical comparisons.
"""
ONE = 1.0 + 1/1e14
"""
Almost one but not one to handle mathematical singularities.
"""
LOG_OF_NEG = -100
"""
Very low but minus infinity value for numerical purposes.
"""
K_BOLTZMANN = 1.38064852e-23
"""
Boltzmann constant (1.38064852e-23)
"""
T0 = 290.
"""
Room temperature (kind of)
"""
EIG_COND = 1e-9
"""
Eigenvalue ratio compared to the maximum eigenvalue in :meth:`~skrf.mathFunctions.nudge_eig`.
EIG_COND * max(eigenvalue)
"""
EIG_MIN = 1e-12
"""
Minimum eigenvalue used in :meth:`~skrf.mathFunctions.nudge_eig`
"""
# S-parameter definition labels and default definition
SdefT = Literal["power", "pseudo", "traveling"]
S_DEFINITIONS: list[SdefT] = list(get_args(SdefT))
S_DEF_DEFAULT = 'power'
S_DEF_HFSS_DEFAULT = 'traveling'
FrequencyUnitT = Literal["Hz", "kHz", "MHz", "GHz", "THz"]
"""
Frequency units: "Hz", "kHz", "MHz", "GHz", "THz" (case-insensitive).
"""
FREQ_UNITS: dict[FrequencyUnitT, float] = {"Hz": 1.0, "kHz": 1e3, "MHz": 1e6, "GHz": 1e9, "THz": 1e12}
SweepTypeT = Literal["lin", "log"]
"""
Frequency sweep type, either "lin" or "log".
"""
CoordT = Literal["cart", "polar"]
InterpolKindT = Literal["linear", "cubic", "nearest", "zero", "slinear", "quadratic", "rational"]
PrimaryPropertiesT = Literal['s', 'z', 'y', 'a', 'g', 'h', 't']
ComponentFuncT = Literal["re", "im", "mag", "db", "db10", "rad", "deg", "arcl", "rad_unwrap", "deg_unwrap",
"arcl_unwrap", "vswr", "time", "time_db", "time_mag", "time_impulse", "time_step"]
SparamFormatT = Literal["db", "ri", "ma"]
PortOrderT = Literal["first", "second", "third", "last", "auto"]
CircuitComponentT = Literal["_is_circuit_port", "_is_circuit_ground", "_is_circuit_open"]
MemoryLayoutT = Literal["C", "F"]
ErrorFunctionsT = Literal["average_l1_norm", "average_l2_norm", "maximum_l1_norm", "average_normalized_l1_norm"]
NumberLike = Number | Sequence[Number] | np.ndarray
global distance_dict
distance_dict = {
'm': 1.,
'cm': 1e-2,
'mm': 1e-3,
'um': 1e-6,
'in': inch,
'mil': mil,
's': c,
'us': 1e-6*c,
'ns': 1e-9*c,
'ps': 1e-12*c,
}
def to_meters(d: NumberLike, unit: str = 'm', v_g: float = c) -> NumberLike:
"""
Translate various units of distance into meters.
Parameters
----------
d : number or array-like
value(s) to convert
unit : str
the unit to that x is in:
['m','cm','um','in','mil','s','us','ns','ps']
v_g : float
group velocity in m/s
Returns
-------
d_m : number of array-like
distance in meters
"""
_distance_dict = {
'm': 1.,
'cm': 1e-2,
'mm': 1e-3,
'um': 1e-6,
'in': inch,
'mil': mil,
's': v_g,
'us': 1e-6*v_g,
'ns': 1e-9*v_g,
'ps': 1e-12*v_g,
}
unit = unit.lower()
try:
return _distance_dict[unit]*d
except KeyError as err:
raise(ValueError('Incorrect unit')) from err
|