Skip to content

Commit

Permalink
logspace10 -> logspace_from_lin
Browse files Browse the repository at this point in the history
  • Loading branch information
bjodah committed Jun 2, 2016
1 parent 4b6fe1d commit ddee364
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 19 deletions.
7 changes: 7 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
v0.4.1
======
- Fixes for enhanced robustness:
- ``.kinetics.ode.get_odesys``
- ``.chemistry.as_per_substance_array``
- Minor changes.

v0.4.0
======
- Multiple fixes throughout
Expand Down
2 changes: 1 addition & 1 deletion chempy/chemistry.py
Original file line number Diff line number Diff line change
Expand Up @@ -1301,7 +1301,7 @@ def as_per_substance_array(self, cont, dtype='float64', unit=None, raise_on_unk=
raise KeyError("Unkown substance key: %s" % k)
cont = [cont[k] for k in substance_keys]

cont = np.asarray(cont, dtype=dtype)
cont = np.atleast_1d(np.asarray(cont, dtype=dtype).squeeze())
if cont.shape[-1] != self.ns:
raise ValueError("Incorrect size")
return cont*(unit if unit is not None else 1)
Expand Down
2 changes: 1 addition & 1 deletion chempy/kinetics/rates.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class _Radiolytic(RadiolyticBase):
parameter_keys = (doserate_name, 'density')
print_name = 'Radiolytic' if doserate_name == 'doserate' else ('Radiolytic{'+doserate_name+'}')

def g_value(self, variables, backend): # for subclasses
def g_value(self, variables, backend=math): # for subclasses
return self.arg(variables, 0, backend=backend)

def __call__(self, variables, backend=math):
Expand Down
3 changes: 1 addition & 2 deletions chempy/tests/test_chemistry.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,7 @@ def test_ReactionSystem__as_per_substance_array_dict():
m = default_units.metre
M = default_units.molar
rs = ReactionSystem([], [Substance('H2O')])
c = rs.as_per_substance_array({'H2O': 1*M},
unit=M)
c = rs.as_per_substance_array({'H2O': 1*M}, unit=M)
assert c.dimensionality == M.dimensionality
assert abs(c[0]/(1000*mol/m**3) - 1) < 1e-16

Expand Down
7 changes: 4 additions & 3 deletions chempy/tests/test_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

from ..util.testing import requires
from ..units import (
allclose, get_derived_unit, is_unitless, linspace, logspace10,
allclose, get_derived_unit, is_unitless, linspace, logspace_from_lin,
SI_base_registry, unitless_in_registry, format_string, get_physical_quantity,
to_unitless, magnitude, default_unit_in_registry, Backend, latex_of_unit,
unit_of, unit_registry_to_human_readable, units_library,
Expand Down Expand Up @@ -104,6 +104,7 @@ def test_to_unitless():
assert abs(to_unitless(3/(u.second*u.molar),
u.metre**3/u.mole/u.second) - 3e-3) < 1e-12
assert abs(to_unitless(2*u.dm3, u.cm3) - 2000) < 1e-12
assert abs(to_unitless(2*u.m3, u.dm3) - 2000) < 1e-12
assert (float(to_unitless(UncertainQuantity(2, u.dm3, .3), u.cm3)) - 2000) < 1e-12

g1 = UncertainQuantity(4.46, u.per100eV, 0)
Expand Down Expand Up @@ -138,8 +139,8 @@ def test_linspace():


@requires(units_library)
def test_logspace10():
ls = logspace10(2*u.second, 3*u.second)
def test_logspace_from_lin():
ls = logspace_from_lin(2*u.second, 3*u.second)
assert abs(to_unitless(ls[0], u.hour) - 2/3600.) < 1e-15
assert abs(to_unitless(ls[-1], u.hour) - 3/3600.) < 1e-15

Expand Down
37 changes: 27 additions & 10 deletions chempy/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,21 @@
- ``chempy.units.default_constants``
- ``chempy.units.SI_base_registry``
together with some functions.
Currently `quantities <https://pypi.python.org/pypi/quantities>`_ is used as
the underlying package to handle units. If it is possible you should try to
only use the `chempy.units` module (in case ``ChemPy`` changes this backend).
only use the `chempy.units` module (in case ``ChemPy`` changes this backend),
and avoid relying on any attributes of the Quantity instances (and rather use
functions in `chempy.units`).
"""
from __future__ import (absolute_import, division, print_function)

from functools import reduce
from math import log10
from operator import mul

from ._util import NameSpace
# Currently we use quantities for units. This may change, therefore use this
# file for all units. A requirement is first-class numpy support.

units_library = 'quantities' # info used for selective testing.

Expand All @@ -41,6 +42,7 @@
default_units = NameSpace(pq)
default_units.decimetre = pq.UnitQuantity(
'decimetre', default_units.m / 10.0, u_symbol='dm')
default_units.m3 = default_units.metre**3
default_units.dm3 = default_units.decimetre**3
default_units.cm3 = default_units.centimetre**3
if not hasattr(default_units, 'molar'):
Expand Down Expand Up @@ -321,7 +323,15 @@ def allclose(a, b, rtol=1e-8, atol=None):


def linspace(start, stop, num=50):
""" Analogous to ``numpy.linspace``. """
""" Analogous to ``numpy.linspace``.
Examples
--------
>>> abs(linspace(2, 8, num=3)[1] - 5) < 1e-15
True
"""

# work around for quantities v0.10.1 and NumPy
import numpy as np
unit = unit_of(start)
Expand All @@ -330,13 +340,20 @@ def linspace(start, stop, num=50):
return np.linspace(start_, stop_, num)*unit


def logspace10(start, stop, num=50):
""" Logarithmically spaced data points """
def logspace_from_lin(start, stop, num=50):
""" Logarithmically spaced data points
Examples
--------
>>> abs(logspace_from_lin(2, 8, num=3)[1] - 4) < 1e-15
True
"""
import numpy as np
unit = unit_of(start)
start_ = log10(to_unitless(start, unit))
stop_ = log10(to_unitless(stop, unit))
return 10**np.linspace(start_, stop_, num)*unit
start_ = np.log2(to_unitless(start, unit))
stop_ = np.log2(to_unitless(stop, unit))
return np.exp2(np.linspace(start_, stop_, num))*unit


def _sum(iterable):
Expand Down
4 changes: 2 additions & 2 deletions chempy/util/bkh.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from itertools import chain

from chempy.kinetics.ode import get_odesys
from chempy.units import to_unitless, linspace, logspace10
from chempy.units import to_unitless, linspace, logspace_from_lin


def integration_with_sliders(
Expand Down Expand Up @@ -36,7 +36,7 @@ def integration_with_sliders(
if x_axis_type == 'linear':
tout = linspace(tend*0, tend)
elif x_axis_type == 'log':
tout = logspace10(tend*1e-9, tend)
tout = logspace_from_lin(tend*1e-9, tend)
else:
raise NotImplementedError("Unknown x_axis_type: %s" % x_axis_type)

Expand Down

0 comments on commit ddee364

Please sign in to comment.