Example #1
0
def derive_unit(expr, name=None):
    """Derive SI-unit from an expression, omitting scale factors."""
    from essm.variables import Variable
    from essm.variables.utils import extract_variables
    from sympy.physics.units import Dimension
    from sympy.physics.units.dimensions import dimsys_SI

    variables = extract_variables(expr)
    for var1 in variables:
        q1 = Quantity('q_' + str(var1))
        q1.set_dimension(
            Dimension(Quantity.get_dimensional_expr(var1.definition.unit)))
        q1.set_scale_factor(var1.definition.unit)
        expr = expr.xreplace({var1: q1})
    dim = Dimension(Quantity.get_dimensional_expr(expr))
    return functools.reduce(
        operator.mul,
        (SI_DIMENSIONS[d]**p
         for d, p in dimsys_SI.get_dimensional_dependencies(dim).items()), 1)
Example #2
0
def derive_baseunit(expr, name=None):
    """Derive SI base unit from an expression, omitting scale factors."""
    from essm.variables import Variable
    from essm.variables.utils import extract_variables
    from sympy.physics.units import Dimension
    from sympy.physics.units.systems.si import dimsys_SI

    Variable.check_unit(expr)  # check for dimensional consistency
    variables = extract_variables(expr)
    for var1 in variables:
        q1 = Quantity('q_' + str(var1))
        SI.set_quantity_dimension(
            q1,
            Dimension(
                SI.get_dimensional_expr(derive_baseunit(
                    var1.definition.unit))))
        SI.set_quantity_scale_factor(q1, var1.definition.unit)
        expr = expr.xreplace({var1: q1})
    dim = Dimension(Variable.get_dimensional_expr(expr))
    return functools.reduce(
        operator.mul,
        (SI_BASE_DIMENSIONS[Symbol(d)]**p
         for d, p in dimsys_SI.get_dimensional_dependencies(dim).items()), 1)
Example #3
0
def test_variable_extraction():
    """Test extract variables from expression."""
    expr = demo_fall.rhs
    assert extract_variables(expr) == {demo_g, demo_fall.definition.t}