B @`!"@s8dZddlmZmZddlmZmZmZmZm Z m Z m Z m Z m Z mZmZmZddlmZmZddlmZmZddlmZddlmZmZddlmZdd lmZdd l m!Z!d d Z"d dZ#ddZ$ddZ%ddZ&ddZ'ddZ(ee&ee$ee'ee'edee%ee&e dee'e de dee(edede e(e dediZ)ddZ*dS)zu Pyomo Units Checking Module This module has some helpful methods to support checking units on Pyomo module objects. )units UnitsError) Objective ConstraintVarParamSuffixSetRangeSetBlockExternalFunction Expressionvalue BooleanVar) ContinuousSet DerivativeVar)PortArc)Complementarity)Disjunct Disjunction) IndexTemplate) native_types)iter_componentcGs&y t|dStk r dSXdS)a Returns True if the units associated with each of the expressions passed as arguments are all equivalent (and False otherwise). Note that this method will raise an exception if the units are inconsistent within an expression (since the units for that expression are not valid). Parameters ---------- args : an argument list of Pyomo expressions Returns ------- bool : True if all the expressions passed as argments have the same units TFN)assert_units_equivalentr)argsr:/tmp/pip-unpacked-wheel-bi3529v6/pyomo/util/check_units.pycheck_units_equivalents rcGsLdd|D}|d}x0|D](}t||stdt|t|qWdS)a Raise an exception if the units are inconsistent within an expression, or not equivalent across all the passed expressions. Parameters ---------- args : an argument list of Pyomo expressions The Pyomo expressions to test Raises ------ :py:class:`pyomo.core.base.units_container.UnitsError`, :py:class:`pyomo.core.base.units_container.InconsistentUnitsError` cSsg|]}t|qSr)r_get_pint_units).0argrrr Esz+assert_units_equivalent..rz+Units between {} and {} are not consistent.N)rZ_equivalent_pint_unitsrformatstr)rZ pint_unitsZpint_unit_compare pint_unitrrrr5s  rcCs|t}|jdk r*t|jdkr*||j||j|jdk rZt|jdkrZ||jt|dkrpt|nt|dS)z Raise an exception if the any units in lower, body, upper on a ConstraintData object are not consistent or are not equivalent with each other. Ng) listlowerrappendbodyupperlenassert_units_consistentr)Zcondatarrrr(_assert_units_consistent_constraint_dataMs      r.c Cs|j}|j}|dks|dkr dSx^|jD]T}|j|}|j|}|rrx2|D]}||}||}t||qNWq(t||q(WdS)zR Raise an exception if the any units do not match for the connected ports N)sourceZ destinationvars is_indexedr) ZarcdataZsportdportkeyZsvarZdvarkZsvardataZdvardatarrr!_assert_units_consistent_arc_dataes    r5cCst|jdS)zk Check the .expr property of the object and raise an exception if the units are not consistent N)#_assert_units_consistent_expressionexpr)objrrr&_assert_units_consistent_property_expr}sr9cCst|}dS)zC Raise an exception if any units in expr are inconsistent. N)rr)r7r%rrrr6sr6cCs$x|jdddD] }t|qWdS)zw This method gets all the components from the block and checks if the units are consistent on each of them FT)Z descend_intoactiveN)Zcomponent_objectsr-)r8 componentrrr_assert_units_consistent_blocksr<Nc Cst|}|tkrdS|s$|tkrZy t|Wn$tk rTtd|YnXdS|jt krtt d|jt |j}|dkrdS| rxx| D]6}y ||Wqtk rtd|YqXqWn4y ||Wn&tk r td|YnXdS)aP This method raises an exception if the units are not consistent on the passed in object. Argument obj can be one of the following components: Pyomo Block (or Model), Constraint, Objective, Expression, or it can be a Pyomo expression object Parameters ---------- obj : Pyomo component (e.g., Block, Model, Constraint, Objective, or Expression) or Pyomo expression The object or expression to test Raises ------ :py:class:`pyomo.core.base.units_container.UnitsError`, :py:class:`pyomo.core.base.units_container.InconsistentUnitsError` Nz Units problem with expression {}z3Units checking not supported for object of type {}.zError in units when checking {}) typerZis_expression_typerr6rprintr#ctype_component_data_handlers TypeErrorr1values)r8Zobjtypehandlercdatarrrr-s6     r-)+__doc__Zpyomo.core.base.units_containerrrZpyomo.core.baserrrrrr r r r r rrZ pyomo.daerrZ pyomo.networkrrZ pyomo.mpecrZ pyomo.gdprrZpyomo.core.expr.template_exprrZpyomo.core.expr.numvaluerZpyomo.util.componentsrrrr.r5r9r6r<r@r-rrrrsD8