B u `;#@sddlZddlmZddlmZddlmZddlmZddl m Z ddl m Z dZ d d ZGd d d eZed GdddeZdS)N) ComponentMap)UnknownSetDimen)Var)ModelComponentFactory) ContinuousSet)iterkeys) DerivativeVar DAE_Errorcsfdd}|S)zk This method returns a function that returns a component by calling it rather than indexing it cs|S)N)args)varr 5/tmp/pip-unpacked-wheel-n62dbgi3/pyomo/dae/diffvar.py_funsz$create_access_function.._funr )r rr )r r create_access_functions rc@seZdZdZdS)r z,Exception raised while processing DAE ModelsN)__name__ __module__ __qualname____doc__r r r r r sr z#Derivative of a Var in a DAE model.c@s@eZdZdZddZddZddZdd Zd d Zd d Z dS)raK Represents derivatives in a model and defines how a :py:class:`Var` is differentiated The :py:class:`DerivativeVar ` component is used to declare a derivative of a :py:class:`Var `. The constructor accepts a single positional argument which is the :py:class:`Var` that's being differentiated. A :py:class:`Var ` may only be differentiated with respect to a :py:class:`ContinuousSet` that it is indexed by. The indexing sets of a :py:class:`DerivativeVar ` are identical to those of the :py:class:`Var ` it is differentiating. Parameters ---------- sVar : ``pyomo.environ.Var`` The variable being differentiated wrt : ``pyomo.dae.ContinuousSet`` or tuple Equivalent to `withrespectto` keyword argument. The :py:class:`ContinuousSet` that the derivative is being taken with respect to. Higher order derivatives are represented by including the :py:class:`ContinuousSet` multiple times in the tuple sent to this keyword. i.e. ``wrt=(m.t, m.t)`` would be the second order derivative with respect to ``m.t`` c Kst|tstd|d|kr.d|kr.td|dd}|d|}yt|j}Wntk rt|_i|_ | dkrd}nt | }d}xnt|D]b\}}|jtkr||j|<|j} | dkrtd||jfn| tkrtd||jf||j7}qWt|j}YnX|dkr0td||dkrb|d krPtd |tt|jg}nt|tkr||jkrtd ||f|g}nxt|tkst|t krxD|D]<}t|tk rtd |||jkrtd ||fqWt |}n td |d d|D} | t| } | |j krRtd|j|j | jft||j | <||_||_|dttj|| f|dS)NzE%s is not a variable. Can only take the derivative of a Varcomponent.wrtZ withrespecttozHCannot specify both 'wrt' and 'withrespectto keywords in a DerivativeVarrzThe variable %s is indexed by a Set (%s) with a non-fixed dimension. A DerivativeVar may only be indexed by Sets with constant dimensionzThe variable %s is indexed by a Set (%s) with an unknown dimension. A DerivativeVar may only be indexed by Sets with known constant dimensionzxThe variable %s is not indexed by any ContinuousSets. A derivative may only be taken with respect to a continuous domainzThe variable %s is indexed by multiple ContinuousSets. The desired ContinuousSet must be specified using the keyword argument 'wrt'zJInvalid derivative: The variable %s is not indexed by the ContinuousSet %szdCannot take the derivative with respect to %s. Expected a ContinuousSet or a tuple of ContinuousSetscSsg|] }t|qSr )str).0ir r r sz*DerivativeVar.__init__..zYCannot create a new derivative variable for variable %s: derivative already defined as %sctype) isinstancerr TypeErrorpoplenZ_contsetAttributeErrorrZ _derivativeZdimlistZ index_setZsubsets enumeraterrZdimennamernextrtypetuplesortweakrefref_sVar_wrt setdefaultr__init__) selfZsVarkwdsrZ num_contsetZ sidx_setslocrsZ_dimZwrtkeyr r r r,Js                zDerivativeVar.__init__cCs|jS)z Return the a list of :py:class:`ContinuousSet` components the derivative is being taken with respect to. Returns ------- `list` )r*)r-r r r get_continuousset_listsz$DerivativeVar.get_continuousset_listcCs$x|jD]}d|krdSqWdS)z Check to see if all the :py:class:`ContinuousSets` this derivative is taken with respect to have been discretized. Returns ------- `boolean` schemeFT)r*Zget_discretization_info)r-rr r r is_fully_discretizeds  z"DerivativeVar.is_fully_discretizedcCs|jS)z Return the :py:class:`Var` that is being differentiated. Returns ------- :py:class:`Var` )r))r-r r r get_state_varszDerivativeVar.get_state_varcCs$y|jSt|j|_|jSdS)aZ Returns the current discretization expression for this derivative or creates an access function to its :py:class:`Var` the first time this method is called. The expression gets built up as the discretization transformations are sequentially applied to each :py:class:`ContinuousSet` in the model. N)_exprrr))r-r r r get_derivative_expressions  z'DerivativeVar.get_derivative_expressioncCs ||_dS)z Sets``_expr``, an expression representing the discretization equations linking the :class:`DerivativeVar` to its state :class:`Var` N)r5)r-exprr r r set_derivative_expressionsz'DerivativeVar.set_derivative_expressionN) rrrrr,r1r3r4r6r8r r r r r$s h  r)r'Zpyomo.common.collectionsrZpyomo.core.base.setrZpyomo.core.base.varrZpyomo.core.base.pluginrZpyomo.dae.contsetrsixr__all__r Exceptionr registerrr r r r  s