B u `#""@s<dZddlmZmZmZddlmZmZmZm Z m Z m Z m Z m Z mZmZmZmZddlmZmZddlmZmZddlmZddlmZmZddlmZdd lm Z dd l!m"Z"d d Z#d dZ$ddZ%ddZ&ddZ'ddZ(ddZ)ee'ee%ee(ee(edee&ee'e de e(e de dee)edede e)edediZ*ddZ+dS)zu Pyomo Units Checking Module This module has some helpful methods to support checking units on Pyomo module objects. )units UnitsErrorUnitExtractionVisitor) 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-n62dbgi3/pyomo/util/check_units.pycheck_units_equivalents rcGsbt|d\}}xJ|ddD]:}t|\}}tt||s tdt|t|q WdS)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` rNz+Units between {} and {} are not consistent.)r_get_units_tuplerZ_pint_units_equivalentrformatstr)rZpyomo_unit_compareZpint_unit_compareexpr pyomo_unit 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. Ngr ) 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_expressionr$)objrrr&_assert_units_consistent_property_expr}sr8cCst|\}}dS)z Raise an exception if any units in expr are inconsistent. # this call will raise an error if an inconsistency is found pyomo_unit, pint_unit = units._get_units_tuple(expr=expr) N)rr!)r$r%r&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-)r7 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)r7Zobjtypehandlercdatarrrr-s6     r-),__doc__Zpyomo.core.base.units_containerrrrZpyomo.core.baserrrrr r r r r rrrZ pyomo.daerrZ pyomo.networkrrZ pyomo.mpecrZ pyomo.gdprrZpyomo.core.expr.template_exprrZpyomo.core.expr.numvaluerZpyomo.util.componentsrrrr.r5r8r6r;r?r-rrrrsD8