Skip to content

Commit

Permalink
Merge 0e5a81b into 002c889
Browse files Browse the repository at this point in the history
  • Loading branch information
jirikuncar committed Jan 19, 2018
2 parents 002c889 + 0e5a81b commit c4e8a4f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 29 deletions.
31 changes: 3 additions & 28 deletions essm/variables/_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@
import six
from sympy import Basic, S
from sympy.physics.units import Dimension, Quantity
# FIXME use SymPy 1.1
# from sympy.physics.units.quantities import \
# _Quantity_constructor_postprocessor_Add
from sympy.physics.units.quantities import \
_Quantity_constructor_postprocessor_Add

from .units import derive_unit
from ..bases import RegistryType
Expand Down Expand Up @@ -135,31 +134,7 @@ def __doc__(self):
return self.definition.__doc__


def _Quantity_constructor_postprocessor_Add(expr):
# Construction postprocessor for the addition,
# checks for dimension mismatches of the addends, thus preventing
# expressions like `meter + second` to be created.

deset = {
tuple(
sorted(
Dimension(Quantity.get_dimensional_expr(i))
.get_dimensional_dependencies().items()))
for i in expr.args
if i.free_symbols == set() and # do not raise if there are symbols
# (free symbols could contain the units corrections)
not i.is_number}
# If `deset` has more than one element, then some dimensions do not
# match in the sum:
if len(deset) > 1:
raise ValueError("summation of quantities of incompatible dimensions")
return expr


Basic._constructor_postprocessor_mapping[Quantity] = {
"Add": [_Quantity_constructor_postprocessor_Add], }

Basic._constructor_postprocessor_mapping[BaseVariable] = {
"Add": [_Quantity_constructor_postprocessor_Add], }
"Add": [_Quantity_constructor_postprocessor_Add], }

__all__ = ('Variable', )
3 changes: 2 additions & 1 deletion essm/variables/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,12 @@ def derive_quantity(expr, name=None):

def derive_unit(expr, name=None):
"""Derive SI-unit from an expression, omitting scale factors."""
from sympy.physics.units.dimensions import dimsys_SI
dim = Dimension(Quantity.get_dimensional_expr(expr))
return functools.reduce(
operator.mul, (
SI_DIMENSIONS[d] ** p
for d, p in Dimension.get_dimensional_dependencies(dim).items()),
for d, p in dimsys_SI.get_dimensional_dependencies(dim).items()),
1)


Expand Down

0 comments on commit c4e8a4f

Please sign in to comment.