Skip to content

Commit

Permalink
Merge 42486fb into d36b37e
Browse files Browse the repository at this point in the history
  • Loading branch information
schymans committed Oct 23, 2020
2 parents d36b37e + 42486fb commit 982695f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
13 changes: 8 additions & 5 deletions essm/variables/_core.py
Expand Up @@ -28,7 +28,7 @@

from sympy import (Abs, Add, Basic, Derivative, Function, Integral, log, Mul,
Piecewise, Pow, S, Symbol)
from sympy.physics.units import Dimension, Quantity, convert_to
from sympy.physics.units import (Dimension, Quantity, convert_to)
from sympy.physics.units.systems.si import dimsys_SI, SI
from sympy.physics.units.util import check_dimensions

Expand Down Expand Up @@ -230,10 +230,13 @@ def collect_factor_and_basedimension(expr):
sum([x[0] for x in expr.args]))
return factor, dim
elif isinstance(expr, Function):
fds = [Variable.collect_factor_and_basedimension(
arg) for arg in expr.args]
return (expr.func(*(f[0] for f in fds)),
expr.func(*(d[1] for d in fds)))
fds = {Variable.collect_factor_and_basedimension(
arg)[1] for arg in expr.args}
if fds != {Dimension(1)}:
raise ValueError(
'Arguments in function are not dimensionless, '
'but have dimensions of {0}'.format(fds))
return expr, Dimension(1)
elif isinstance(expr, Dimension):
return 1, expr
else:
Expand Down
3 changes: 2 additions & 1 deletion essm/variables/units.py
Expand Up @@ -165,5 +165,6 @@ def derive_base_dimension(dim):

__all__ = (
'derive_baseunit', 'derive_unit', 'markdown',
'joule', 'kelvin', 'kilogram', 'meter', 'mole', 'pascal', 'second', 'watt'
'joule', 'kelvin', 'kilogram', 'meter', 'mole',
'pascal', 'second', 'watt'
)
16 changes: 15 additions & 1 deletion tests/test_equations.py
Expand Up @@ -11,7 +11,7 @@
derive_baseunit)
from essm.variables.utils import (extract_variables, replace_defaults,
replace_variables)
from sympy import Derivative, exp, log, S, Symbol, solve, sqrt
from sympy import cos, Derivative, exp, log, S, Symbol, solve, sqrt
from sympy.physics.units import Quantity, length, meter


Expand Down Expand Up @@ -113,6 +113,20 @@ class valid_units(Equation):
expr = Eq(demo_v, sqrt(demo_d * demo_d1) / demo_fall.definition.t)


def test_units_exp():
"""Check units in exp."""

class valid_units_exp(Equation):
expr = Eq(demo_d/demo_d1, exp(demo_1))


def test_units_cos():
"""Check units in cos."""

class valid_units_exp(Equation):
expr = Eq(1, 1/(cos(demo_1)))


def test_integrate():
"""Test that variables can be used as integration symbols."""
from sympy import integrate
Expand Down

0 comments on commit 982695f

Please sign in to comment.