Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Old version compatibility #86

Merged
merged 2 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions brainunit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,30 @@
from ._unit_constants import *
from ._unit_shortcuts import *
from ._unit_shortcuts import __all__ as _std_units_all
# from .constants import *
# from .constants import __all__ as _constants_all

__all__ = (
['math', 'linalg', 'autograd', 'fft', 'constants', 'sparse'] +
[
'math',
'linalg',
'autograd',
'fft',
'constants',
'sparse'
] +
_common_all +
_std_units_all +
_base_all +
_celsius_all
)
del _common_all, _std_units_all, _base_all, _celsius_all, _matplotlib_compat

# old version compatibility
avogadro_constant = constants.avogadro
boltzmann_constant = constants.boltzmann
electric_constant = constants.electric
electron_mass = constants.electron_mass
elementary_charge = constants.elementary_charge
faraday_constant = constants.faraday
gas_constant = constants.gas
magnetic_constant = constants.magnetic
molar_mass_constant = constants.molar_mass
14 changes: 13 additions & 1 deletion brainunit/_unit_constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@
from ._unit_common import joule, kilogram, second, meter, radian, pascal, meter2, meter3, kelvin, watt, newton
from .math import pi

__all__ = [
"metric_ton", "grain", "lb", "pound", "slinch", "blob", "slug", "oz", "ounce", "stone", "long_ton", "short_ton",
"troy_ounce", "troy_pound", "carat", "atomic_mass", "u", "um_u", "degree", "arcmin", "arcminute", "arcsec",
"arcsecond", "minute", "hour", "day", "week", "month", "year", "julian_year", "inch", "foot", "yard", "mile",
"mil", "point", "pica", "survey_foot", "survey_mile", "nautical_mile", "fermi", "angstrom", "micron",
"astronomical_unit", "au", "light_year", "parsec", "atm", "atmosphere", "bar", "mmHg", "torr", "psi",
"hectare", "acre", "gallon", "gallon_US", "gallon_imp", "fluid_ounce", "fluid_ounce_US", "fluid_ounce_imp",
"bbl", "barrel", "speed_unit", "kmh", "mph", "mach", "speed_of_sound", "knot", "degree_Fahrenheit", "eV",
"electron_volt", "calorie", "calorie_th", "calorie_IT", "erg", "Btu", "Btu_IT", "Btu_th", "ton_TNT", "hp",
"horsepower", "dyn", "dyne", "lbf", "pound_force", "kgf", "kilogram_force", "IMF"
]

# ----- Mass -----
metric_ton = Unit.create(kilogram.dim, name="metric ton", dispname="t", scale=kilogram.scale + 3)
grain = Unit.create(kilogram.dim, name="grain", dispname="gr", scale=kilogram.scale - 5, factor=6.479891)
Expand Down Expand Up @@ -146,4 +158,4 @@

https://physics.nist.gov/cuu/Constants/

"""
"""
36 changes: 28 additions & 8 deletions brainunit/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,28 +51,48 @@
kilogram,
meter,
mole,
newton, radian, second, pascal, meter2, meter3, watt,
newton,
radian,
second,
meter2,
meter3,
watt,
)
from ._unit_constants import speed_unit

__all__ = [
'arcmin', 'arcminute', 'arcsec', 'arcsecond', 'atomic_mass', 'au', 'astronomical_unit',
'angstrom', 'atm', 'atmosphere', 'avogadro', 'bar', 'blob', 'boltzmann', 'Btu', 'Btu_IT',
'Btu_th', 'carat', 'calorie', 'calorie_IT', 'calorie_th', 'day', 'degree', 'degree_Fahrenheit',
'dyn', 'dyne', 'eV', 'electron_mass', 'electric', 'electronvolt', 'elementary_charge', 'erg',
'faraday', 'fermi', 'fluid_ounce', 'fluid_ounce_US', 'fluid_ounce_imp', 'foot', 'gas', 'grain',
'gallon', 'gallon_US', 'gallon_imp', 'gram', 'hectare', 'hour', 'hp', 'horsepower', 'IMF',
'inch', 'julian_year', 'kelvin', 'kgf', 'kilogram_force', 'knot', 'lb', 'lbf', 'light_year',
'long_ton', 'mach', 'magnetic', 'meter', 'metric_ton', 'micron', 'mil', 'mile', 'minute',
'molar_mass', 'month', 'mph', 'nautical_mile', 'newton', 'ounce', 'oz', 'parsec', 'pica',
'point', 'pound', 'psi', 'radian', 'second', 'short_ton', 'slug', 'slinch', 'speed_unit',
'stone', 'survey_foot', 'survey_mile', 'torr', 'troy_ounce', 'troy_pound', 'ton_TNT', 'week',
'watt', 'year', 'zero_celsius'
]

#: Avogadro constant (http://physics.nist.gov/cgi-bin/cuu/Value?na)
avogadro_constant = np.asarray(6.022140857e23) / mole
avogadro = np.asarray(6.022140857e23) / mole
#: Boltzmann constant (physics.nist.gov/cgi-bin/cuu/Value?k)
boltzmann_constant = np.asarray(1.38064852e-23) * (joule / kelvin)
boltzmann = np.asarray(1.38064852e-23) * (joule / kelvin)
#: electric constant (http://physics.nist.gov/cgi-bin/cuu/Value?ep0)
electric_constant = np.asarray(8.854187817e-12) * (farad / meter)
electric = np.asarray(8.854187817e-12) * (farad / meter)
#: Electron rest mass (physics.nist.gov/cgi-bin/cuu/Value?me)
electron_mass = np.asarray(9.10938356e-31) * kilogram
#: Elementary charge (physics.nist.gov/cgi-bin/cuu/Value?e)
elementary_charge = np.asarray(1.6021766208e-19) * coulomb
#: Faraday constant (http://physics.nist.gov/cgi-bin/cuu/Value?f)
faraday_constant = np.asarray(96485.33289) * (coulomb / mole)
faraday = np.asarray(96485.33289) * (coulomb / mole)
#: gas constant (http://physics.nist.gov/cgi-bin/cuu/Value?r)
gas_constant = np.asarray(8.3144598) * (joule / mole / kelvin)
gas = np.asarray(8.3144598) * (joule / mole / kelvin)
#: Magnetic constant (http://physics.nist.gov/cgi-bin/cuu/Value?mu0)
magnetic_constant = np.asarray(4 * np.pi * 1e-7) * (newton / amp ** 2)
magnetic = np.asarray(4 * np.pi * 1e-7) * (newton / amp ** 2)
#: Molar mass constant (http://physics.nist.gov/cgi-bin/cuu/Value?mu)
molar_mass_constant = np.asarray(1.) * (gram / mole)
molar_mass = np.asarray(1.) * (gram / mole)
#: zero degree Celsius
zero_celsius = np.asarray(273.15) * kelvin

Expand Down
27 changes: 15 additions & 12 deletions brainunit/constants_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,36 +47,39 @@
'dyn', 'lbf', 'kgf', 'IMF',
]


class TestConstant(unittest.TestCase):

def test_constants(self):
import brainunit.constants as constants

# Check that the expected names exist and have the correct dimensions
assert constants.avogadro_constant.dim == (1 / mole).dim
assert constants.boltzmann_constant.dim == (joule / kelvin).dim
assert constants.electric_constant.dim == (farad / meter).dim
assert constants.avogadro.dim == (1 / mole).dim
assert constants.boltzmann.dim == (joule / kelvin).dim
assert constants.electric.dim == (farad / meter).dim
assert constants.electron_mass.dim == kilogram.dim
assert constants.elementary_charge.dim == coulomb.dim
assert constants.faraday_constant.dim == (coulomb / mole).dim
assert constants.gas_constant.dim == (joule / mole / kelvin).dim
assert constants.magnetic_constant.dim == (newton / amp2).dim
assert constants.molar_mass_constant.dim == (kilogram / mole).dim
assert constants.faraday.dim == (coulomb / mole).dim
assert constants.gas.dim == (joule / mole / kelvin).dim
assert constants.magnetic.dim == (newton / amp2).dim
assert constants.molar_mass.dim == (kilogram / mole).dim
assert constants.zero_celsius.dim == kelvin.dim

# Check the consistency between a few constants
assert u.math.allclose(
constants.gas_constant.mantissa,
(constants.avogadro_constant * constants.boltzmann_constant).mantissa,
constants.gas.mantissa,
(constants.avogadro * constants.boltzmann).mantissa,
)
assert u.math.allclose(
constants.faraday_constant.mantissa,
(constants.avogadro_constant * constants.elementary_charge).mantissa,
constants.faraday.mantissa,
(constants.avogadro * constants.elementary_charge).mantissa,
)

def test_quantity_constants_and_unit_constants(self):
import brainunit.constants as quantity_constants
import brainunit._unit_constants as unit_constants
for c in constants_list:
q_c = getattr(quantity_constants, c)
u_c = getattr(unit_constants, c)
assert q_c.to_decimal(q_c.unit) == (1. * u_c).to_decimal(q_c.unit), f"Mismatch between {c} in quantity_constants and unit_constants"
assert q_c.to_decimal(q_c.unit) == (1. * u_c).to_decimal(
q_c.unit), f"Mismatch between {c} in quantity_constants and unit_constants"
Loading