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

Check dimensions #94

Merged
merged 7 commits into from
Oct 27, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
14 changes: 9 additions & 5 deletions essm/variables/_core.py
Original file line number Diff line number Diff line change
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,14 @@ 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 = set(Variable.collect_factor_and_basedimension(
arg)[1] for arg in expr.args)
schymans marked this conversation as resolved.
Show resolved Hide resolved
if fds != {Dimension(1)}:
print(fds)
schymans marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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