Debian Codegraph @

docs_sphinx/user/units.rst

raw
1 Physical units
2 ==============
3
4 .. contents::
5 :local:
6 :depth: 1
7
8 Brian includes a system for physical units. The base units are defined by their
9 standard SI unit names: ``amp``/``ampere``, ``kilogram``/``kilogramme``,
10 ``second``, ``metre``/``meter``, ``mole``/``mol``, ``kelvin``, and ``candela``.
11 In addition to these base units, Brian defines a set of derived units:
12 ``coulomb``, ``farad``, ``gram``/``gramme``, ``hertz``, ``joule``, ``liter``/
13 ``litre``, ``molar``, ``pascal``, ``ohm``, ``siemens``, ``volt``, ``watt``,
14 together with prefixed versions (e.g. ``msiemens = 0.001*siemens``) using the
15 prefixes ``p, n, u, m, k, M, G, T`` (two exceptions to this rule: ``kilogram``
16 is not defined with any additional prefixes, and ``metre`` and ``meter`` are
17 additionaly defined with the "centi" prefix, i.e. ``cmetre``/``cmeter``).
18 For convenience, a couple of additional useful standard abbreviations such as
19 ``cm`` (instead of ``cmetre``/``cmeter``), ``nS`` (instead of ``nsiemens``),
20 ``ms`` (instead of ``msecond``), ``Hz`` (instead of ``hertz``), ``mM``
21 (instead of ``mmolar``) are included. To avoid clashes with common variable
22 names, no one-letter abbreviations are provided (e.g. you can use ``mV`` or
23 ``nS``, but *not* ``V`` or ``S``).
24
25 Using units
26 -----------
27 You can generate a physical quantity by multiplying a scalar or vector value
28 with its physical unit::
29
30 >>> tau = 20*ms
31 >>> print(tau)
32 20. ms
33 >>> rates = [10, 20, 30]*Hz
34 >>> print(rates)
35 [ 10. 20. 30.] Hz
36
37 Brian will check the consistency of operations on units and raise an error for
38 dimensionality mismatches::
39
40 >>> tau += 1 # ms? second? # doctest: +ELLIPSIS +IGNORE_EXCEPTION_DETAIL
41 Traceback (most recent call last):
42 ...
43 DimensionMismatchError: Cannot calculate ... += 1, units do not match (units are second and 1).
44 >>> 3*kgram + 3*amp # doctest: +ELLIPSIS +IGNORE_EXCEPTION_DETAIL
45 Traceback (most recent call last):
46 ...
47 DimensionMismatchError: Cannot calculate 3. kg + 3. A, units do not match (units are kilogram and amp).
48
49 Most Brian functions will also complain about non-specified or incorrect units::
50
51 >>> G = NeuronGroup(10, 'dv/dt = -v/tau: volt', dt=0.5) # doctest: +ELLIPSIS +IGNORE_EXCEPTION_DETAIL
52 Traceback (most recent call last):
53 ...
54 DimensionMismatchError: Function "__init__" expected a quantitity with unit second for argument "dt" but got 0.5 (unit is 1).
55
56 Numpy functions have been overwritten to correctly work with units (see the
57 :doc:`developer documentation <../developer/units>` for more details)::
58
59 >>> print(mean(rates))
60 20. Hz
61 >>> print(rates.repeat(2))
62 [ 10. 10. 20. 20. 30. 30.] Hz
63
64 Removing units
65 --------------
66 There are various options to remove the units from a value (e.g. to use it with
67 analysis functions that do not correctly work with units)
68
69 * Divide the value by its unit (most of the time the recommended option
70 because it is clear about the scale)
71 * Transform it to a pure numpy array in the base unit by calling `asarray`
72 (no copy) or `array` (copy)
73 * Directly get the unitless value of a state variable by appending an underscore
74 to the name
75
76 ::
77
78 >>> tau/ms
79 20.0
80 >>> asarray(rates)
81 array([ 10., 20., 30.])
82 >>> G = NeuronGroup(5, 'dv/dt = -v/tau: volt')
83 >>> print(G.v_[:])
84 [ 0. 0. 0. 0. 0.]
85
86
87 Temperatures
88 ------------
89 Brian only supports temperatures defined in °K, using the provided ``kelvin``
90 unit object. Other conventions such as °C, or °F are not compatible with Brian's
91 unit system, because they cannot be expressed as a multiplicative scaling of the
92 SI base unit kelvin (their zero point is different). However, in biological
93 experiments and modeling, temperatures are typically reported in °C. How to use
94 such temperatures depends on whether they are used as *temperature differences*
95 or as *absolute temperatures*:
96
97 temperature differences
98 Their major use case is the correction of time constants for differences in
99 temperatures based on the `Q10 temperature coefficient <https://en.wikipedia.org/wiki/Q10_(temperature_coefficient)>`_.
100 In this case, all temperatures can directly use ``kelvin`` even though the
101 temperatures are reported in Celsius, since temperature differences in
102 Celsius and Kelvin are identical.
103
104 absolute temperatures
105 Equations such as the `Goldman–Hodgkin–Katz voltage equation <https://en.wikipedia.org/wiki/Goldman_equation>`_
106 have a factor that depends on the absolute temperature measured in Kelvin.
107 To get this temperature from a temperature reported in °C, you can use the
108 ``zero_celsius`` constant from the `brian2.units.constants` package (see
109 below)::
110
111 from brian2.units.constants import zero_celsius
112
113 celsius_temp = 27
114 abs_temp = celsius_temp*kelvin + zero_celsius
115
116 .. note:: Earlier versions of Brian had a ``celsius`` unit which was in fact
117 identical to ``kelvin``. While this gave the correct results for
118 temperature differences, it did not correctly work for absolute
119 temperatures. To avoid confusion and possible misinterpretation,
120 the ``celsius`` unit has therefore been removed.
121
122 .. _constants:
123
124 Constants
125 ---------
126 The `brian2.units.constants` package provides a range of physical constants that
127 can be useful for detailed biological models. Brian provides the following
128 constants:
129
130 ==================== ================== ======================= ==================================================================
131 Constant Symbol(s) Brian name Value
132 ==================== ================== ======================= ==================================================================
133 Avogadro constant :math:`N_A, L` ``avogadro_constant`` :math:`6.022140857\times 10^{23}\,\mathrm{mol}^{-1}`
134 Boltzmann constant :math:`k` ``boltzmann_constant`` :math:`1.38064852\times 10^{-23}\,\mathrm{J}\,\mathrm{K}^{-1}`
135 Electric constant :math:`\epsilon_0` ``electric_constant`` :math:`8.854187817\times 10^{-12}\,\mathrm{F}\,\mathrm{m}^{-1}`
136 Electron mass :math:`m_e` ``electron_mass`` :math:`9.10938356\times 10^{-31}\,\mathrm{kg}`
137 Elementary charge :math:`e` ``elementary_charge`` :math:`1.6021766208\times 10^{-19}\,\mathrm{C}`
138 Faraday constant :math:`F` ``faraday_constant`` :math:`96485.33289\,\mathrm{C}\,\mathrm{mol}^{-1}`
139 Gas constant :math:`R` ``gas_constant`` :math:`8.3144598\,\mathrm{J}\,\mathrm{mol}^{-1}\,\mathrm{K}^{-1}`
140 Magnetic constant :math:`\mu_0` ``magnetic_constant`` :math:`12.566370614\times 10^{-7}\,\mathrm{N}\,\mathrm{A}^{-2}`
141 Molar mass constant :math:`M_u` ``molar_mass_constant`` :math:`1\times 10^{-3}\,\mathrm{kg}\,\mathrm{mol}^{-1}`
142 0°C ``zero_celsius`` :math:`273.15\,\mathrm{K}`
143 ==================== ================== ======================= ==================================================================
144
145 Note that these constants are not imported by default, you will have to
146 explicitly import them from `brian2.units.constants`. During the import, you
147 can also give them shorter names using Python's ``from ... import ... as ...``
148 syntax. For example, to calculate the :math:`\frac{RT}{F}` factor that appears
149 in the `Goldman–Hodgkin–Katz voltage equation <https://en.wikipedia.org/wiki/Goldman_equation>`_
150 you can use::
151
152 from brian2 import *
153 from brian2.units.constants import zero_celsius, gas_constant as R, faraday_constant as F
154
155 celsius_temp = 27
156 T = celsius_temp*kelvin + zero_celsius
157 factor = R*T/F
158
159
160 .. admonition:: The following topics are not essential for beginners.
161
162 |
163
164 Importing units
165 ---------------
166 Brian generates standard names for units, combining the unit name (e.g.
167 "siemens") with a prefixes (e.g. "m"), and also generates squared and cubed
168 versions by appending a number. For example, the units "msiemens", "siemens2",
169 "usiemens3" are all predefined. You can import these units from the package
170 ``brian2.units.allunits`` -- accordingly, an
171 ``from brian2.units.allunits import *`` will result in everything from
172 ``Ylumen3`` (cubed yotta lumen) to ``ymol`` (yocto mole) being imported.
173
174 A better choice is normally to do ``from brian2.units import *`` or import
175 everything ``from brian2 import *`` which only imports the units mentioned in
176 the introductory paragraph (base units, derived units, and some standard
177 abbreviations).
178
179 In-place operations on quantities
180 ---------------------------------
181 In-place operations on quantity arrays change the underlying array, in the
182 same way as for standard numpy arrays. This means, that any other variables
183 referencing the same object will be affected as well::
184
185 >>> q = [1, 2] * mV
186 >>> r = q
187 >>> q += 1*mV
188 >>> q
189 array([ 2., 3.]) * mvolt
190 >>> r
191 array([ 2., 3.]) * mvolt
192
193 In contrast, scalar quantities will never change the underlying value but
194 instead return a new value (in the same way as standard Python scalars)::
195
196 >>> x = 1*mV
197 >>> y = x
198 >>> x *= 2
199 >>> x
200 2. * mvolt
201 >>> y
202 1. * mvolt