o
    ohc                     @   s   d Z ddlmZmZ ddlmZ ddlmZ ddlm	Z	m
Z
mZ ddlmZ ddlmZmZ ddlmZ dd	lmZ g d
ZG dd deeZG dd deZG dd deZdS )aL  Activation dynamics for musclotendon models.

Musculotendon models are able to produce active force when they are activated,
which is when a chemical process has taken place within the muscle fibers
causing them to voluntarily contract. Biologically this chemical process (the
diffusion of :math:`\textrm{Ca}^{2+}` ions) is not the input in the system,
electrical signals from the nervous system are. These are termed excitations.
Activation dynamics, which relates the normalized excitation level to the
normalized activation level, can be modeled by the models present in this
module.

    )ABCabstractmethod)cached_property)Symbol)FloatIntegerRational)tanh)MutableDenseMatrixzeros)_NamedMixin)dynamicsymbols)ActivationBase FirstOrderActivationDeGroote2016ZerothOrderActivationc                   @   s  e Zd ZdZdd Zeedd Zedd Z	edd	 Z
ed
d Zedd Zeedd Zeedd Zeedd Zeedd Zeedd Zeedd Zeedd Zeedd Zeedd Zed d! Zd"d# Zd$d% Zd&S )'r   a  Abstract base class for all activation dynamics classes to inherit from.

    Notes
    =====

    Instances of this class cannot be directly instantiated by users. However,
    it can be used to created custom activation dynamics types through
    subclassing.

    c                 C   s.   t || _td| | _td| | _dS )z#Initializer for ``ActivationBase``.e_a_N)strnamer   _e_aselfr    r   y/var/www/html/construction_image-detection-poc/venv/lib/python3.10/site-packages/sympy/physics/biomechanics/activation.py__init__,   s   
zActivationBase.__init__c                 C      dS )zOAlternate constructor that provides recommended defaults for
        constants.Nr   clsr   r   r   r   with_defaults4   s   zActivationBase.with_defaultsc                 C      | j S )zDynamic symbol representing excitation.

        Explanation
        ===========

        The alias ``e`` can also be used to access the same attribute.

        r   r   r   r   r   
excitation;      
zActivationBase.excitationc                 C   r    )zDynamic symbol representing excitation.

        Explanation
        ===========

        The alias ``excitation`` can also be used to access the same attribute.

        r!   r"   r   r   r   eG   r$   zActivationBase.ec                 C   r    )zDynamic symbol representing activation.

        Explanation
        ===========

        The alias ``a`` can also be used to access the same attribute.

        r   r"   r   r   r   
activationS   r$   zActivationBase.activationc                 C   r    )zDynamic symbol representing activation.

        Explanation
        ===========

        The alias ``activation`` can also be used to access the same attribute.

        r&   r"   r   r   r   a_   r$   zActivationBase.ac                 C   r   ):Order of the (differential) equation governing activation.Nr   r"   r   r   r   orderk   s   zActivationBase.orderc                 C   r   )Ordered column matrix of functions of time that represent the state
        variables.

        Explanation
        ===========

        The alias ``x`` can also be used to access the same attribute.

        Nr   r"   r   r   r   
state_varsq      zActivationBase.state_varsc                 C   r   )Ordered column matrix of functions of time that represent the state
        variables.

        Explanation
        ===========

        The alias ``state_vars`` can also be used to access the same attribute.

        Nr   r"   r   r   r   x   r-   zActivationBase.xc                 C   r   )Ordered column matrix of functions of time that represent the input
        variables.

        Explanation
        ===========

        The alias ``r`` can also be used to access the same attribute.

        Nr   r"   r   r   r   
input_vars   r-   zActivationBase.input_varsc                 C   r   )Ordered column matrix of functions of time that represent the input
        variables.

        Explanation
        ===========

        The alias ``input_vars`` can also be used to access the same attribute.

        Nr   r"   r   r   r   r   r-   zActivationBase.rc                 C   r   )z  Ordered column matrix of non-time varying symbols present in ``M``
        and ``F``.

        Only symbolic constants are returned. If a numeric type (e.g. ``Float``)
        has been used instead of ``Symbol`` for a constant then that attribute
        will not be included in the matrix returned by this property. This is
        because the primary use of this property attribute is to provide an
        ordered sequence of the still-free symbols that require numeric values
        during code generation.

        Explanation
        ===========

        The alias ``p`` can also be used to access the same attribute.

        Nr   r"   r   r   r   	constants      zActivationBase.constantsc                 C   r   )a  Ordered column matrix of non-time varying symbols present in ``M``
        and ``F``.

        Only symbolic constants are returned. If a numeric type (e.g. ``Float``)
        has been used instead of ``Symbol`` for a constant then that attribute
        will not be included in the matrix returned by this property. This is
        because the primary use of this property attribute is to provide an
        ordered sequence of the still-free symbols that require numeric values
        during code generation.

        Explanation
        ===========

        The alias ``constants`` can also be used to access the same attribute.

        Nr   r"   r   r   r   p   r6   zActivationBase.pc                 C   r   )<  Ordered square matrix of coefficients on the LHS of ``M x' = F``.

        Explanation
        ===========

        The square matrix that forms part of the LHS of the linear system of
        ordinary differential equations governing the activation dynamics:

        ``M(x, r, t, p) x' = F(x, r, t, p)``.

        Nr   r"   r   r   r   M      zActivationBase.Mc                 C   r   )1  Ordered column matrix of equations on the RHS of ``M x' = F``.

        Explanation
        ===========

        The column matrix that forms the RHS of the linear system of ordinary
        differential equations governing the activation dynamics:

        ``M(x, r, t, p) x' = F(x, r, t, p)``.

        Nr   r"   r   r   r   F   r:   zActivationBase.Fc                 C   r   )z

        Explanation
        ===========

        The solution to the linear system of ordinary differential equations
        governing the activation dynamics:

        ``M(x, r, t, p) x' = F(x, r, t, p)``.

        Nr   r"   r   r   r   rhs   s   zActivationBase.rhsc                 C   s(   t | t |kr
dS | j|jkrdS dS )z'Equality check for activation dynamics.FT)typer   )r   otherr   r   r   __eq__  s
   zActivationBase.__eq__c                 C   s   | j j d| jdS )z.Default representation of activation dynamics.())	__class____name__r   r"   r   r   r   __repr__
  s   zActivationBase.__repr__N)rD   
__module____qualname____doc__r   classmethodr   r   propertyr#   r%   r'   r(   r*   r,   r/   r1   r3   r5   r7   r9   r<   r=   r@   rE   r   r   r   r   r       sZ    




r   c                       s   e Zd ZdZ fddZedd Zedd Zedd	 Z	ed
d Z
edd Zedd Zedd Zedd Zedd Zedd Zdd Z  ZS )r   a  Simple zeroth-order activation dynamics mapping excitation to
    activation.

    Explanation
    ===========

    Zeroth-order activation dynamics are useful in instances where you want to
    reduce the complexity of your musculotendon dynamics as they simple map
    exictation to activation. As a result, no additional state equations are
    introduced to your system. They also remove a potential source of delay
    between the input and dynamics of your system as no (ordinary) differential
    equations are involed.

    c                    s   t  | | j| _dS )zInitializer for ``ZerothOrderActivation``.

        Parameters
        ==========

        name : str
            The name identifier associated with the instance. Must be a string
            of length at least 1.

        N)superr   r   r   r   rC   r   r   r     s   zZerothOrderActivation.__init__c                 C   s   | |S )a  Alternate constructor that provides recommended defaults for
        constants.

        Explanation
        ===========

        As this concrete class doesn't implement any constants associated with
        its dynamics, this ``classmethod`` simply creates a standard instance
        of ``ZerothOrderActivation``. An implementation is provided to ensure
        a consistent interface between all ``ActivationBase`` concrete classes.

        r   r   r   r   r   r   0  s   z#ZerothOrderActivation.with_defaultsc                 C   r   )r)   r   r   r"   r   r   r   r*   @     zZerothOrderActivation.orderc                 C   
   t ddS )a  Ordered column matrix of functions of time that represent the state
        variables.

        Explanation
        ===========

        As zeroth-order activation dynamics simply maps excitation to
        activation, this class has no associated state variables and so this
        property return an empty column ``Matrix`` with shape (0, 1).

        The alias ``x`` can also be used to access the same attribute.

        r      r   r"   r   r   r   r,   E     
z ZerothOrderActivation.state_varsc                 C   rN   )a  Ordered column matrix of functions of time that represent the state
        variables.

        Explanation
        ===========

        As zeroth-order activation dynamics simply maps excitation to
        activation, this class has no associated state variables and so this
        property return an empty column ``Matrix`` with shape (0, 1).

        The alias ``state_vars`` can also be used to access the same attribute.

        r   rO   rP   r"   r   r   r   r/   V  rQ   zZerothOrderActivation.xc                 C      t | jgS )a  Ordered column matrix of functions of time that represent the input
        variables.

        Explanation
        ===========

        Excitation is the only input in zeroth-order activation dynamics and so
        this property returns a column ``Matrix`` with one entry, ``e``, and
        shape (1, 1).

        The alias ``r`` can also be used to access the same attribute.

        Matrixr   r"   r   r   r   r1   g     z ZerothOrderActivation.input_varsc                 C   rR   )a  Ordered column matrix of functions of time that represent the input
        variables.

        Explanation
        ===========

        Excitation is the only input in zeroth-order activation dynamics and so
        this property returns a column ``Matrix`` with one entry, ``e``, and
        shape (1, 1).

        The alias ``input_vars`` can also be used to access the same attribute.

        rS   r"   r   r   r   r3   x  rU   zZerothOrderActivation.rc                 C   rN   )aN  Ordered column matrix of non-time varying symbols present in ``M``
        and ``F``.

        Only symbolic constants are returned. If a numeric type (e.g. ``Float``)
        has been used instead of ``Symbol`` for a constant then that attribute
        will not be included in the matrix returned by this property. This is
        because the primary use of this property attribute is to provide an
        ordered sequence of the still-free symbols that require numeric values
        during code generation.

        Explanation
        ===========

        As zeroth-order activation dynamics simply maps excitation to
        activation, this class has no associated constants and so this property
        return an empty column ``Matrix`` with shape (0, 1).

        The alias ``p`` can also be used to access the same attribute.

        r   rO   rP   r"   r   r   r   r5        
zZerothOrderActivation.constantsc                 C   rN   )aV  Ordered column matrix of non-time varying symbols present in ``M``
        and ``F``.

        Only symbolic constants are returned. If a numeric type (e.g. ``Float``)
        has been used instead of ``Symbol`` for a constant then that attribute
        will not be included in the matrix returned by this property. This is
        because the primary use of this property attribute is to provide an
        ordered sequence of the still-free symbols that require numeric values
        during code generation.

        Explanation
        ===========

        As zeroth-order activation dynamics simply maps excitation to
        activation, this class has no associated constants and so this property
        return an empty column ``Matrix`` with shape (0, 1).

        The alias ``constants`` can also be used to access the same attribute.

        r   rO   rP   r"   r   r   r   r7     rV   zZerothOrderActivation.pc                 C   s   t g S )a  Ordered square matrix of coefficients on the LHS of ``M x' = F``.

        Explanation
        ===========

        The square matrix that forms part of the LHS of the linear system of
        ordinary differential equations governing the activation dynamics:

        ``M(x, r, t, p) x' = F(x, r, t, p)``.

        As zeroth-order activation dynamics have no state variables, this
        linear system has dimension 0 and therefore ``M`` is an empty square
        ``Matrix`` with shape (0, 0).

        )rT   r"   r   r   r   r9     s   zZerothOrderActivation.Mc                 C   rN   )a  Ordered column matrix of equations on the RHS of ``M x' = F``.

        Explanation
        ===========

        The column matrix that forms the RHS of the linear system of ordinary
        differential equations governing the activation dynamics:

        ``M(x, r, t, p) x' = F(x, r, t, p)``.

        As zeroth-order activation dynamics have no state variables, this
        linear system has dimension 0 and therefore ``F`` is an empty column
        ``Matrix`` with shape (0, 1).

        r   rO   rP   r"   r   r   r   r<     s   
zZerothOrderActivation.Fc                 C   rN   )a  Ordered column matrix of equations for the solution of ``M x' = F``.

        Explanation
        ===========

        The solution to the linear system of ordinary differential equations
        governing the activation dynamics:

        ``M(x, r, t, p) x' = F(x, r, t, p)``.

        As zeroth-order activation dynamics have no state variables, this
        linear has dimension 0 and therefore this method returns an empty
        column ``Matrix`` with shape (0, 1).

        r   rO   rP   r"   r   r   r   r=     s   
zZerothOrderActivation.rhs)rD   rF   rG   rH   r   rI   r   rJ   r*   r,   r/   r1   r3   r5   r7   r9   r<   r=   __classcell__r   r   rL   r   r     s0    









r   c                       s6  e Zd ZdZ			d0 fdd	Zedd Zedd Zej	d	d Zed
d Z
edd Zej	dd Zedd Zedd Zej	dd Zedd Zedd Zedd Zedd Zedd Zedd Zed d! Zed"d# Zed$d% Zed&d' Zd(d) Zed*d+ Zd,d- Zd.d/ Z  ZS )1r   aA  First-order activation dynamics based on De Groote et al., 2016 [1]_.

    Explanation
    ===========

    Gives the first-order activation dynamics equation for the rate of change
    of activation with respect to time as a function of excitation and
    activation.

    The function is defined by the equation:

    .. math::

        \frac{da}{dt} = \left(\frac{\frac{1}{2} + a0}{\tau_a \left(\frac{1}{2}
            + \frac{3a}{2}\right)} + \frac{\left(\frac{1}{2}
            + \frac{3a}{2}\right) \left(\frac{1}{2} - a0\right)}{\tau_d}\right)
            \left(e - a\right)

    where

    .. math::

        a0 = \frac{\tanh{\left(b \left(e - a\right) \right)}}{2}

    with constant values of :math:`tau_a = 0.015`, :math:`tau_d = 0.060`, and
    :math:`b = 10`.

    References
    ==========

    .. [1] De Groote, F., Kinney, A. L., Rao, A. V., & Fregly, B. J., Evaluation
           of direct collocation optimal control problem formulations for
           solving the muscle redundancy problem, Annals of biomedical
           engineering, 44(10), (2016) pp. 2922-2936

    Nc                    s"   t  | || _|| _|| _dS )a  Initializer for ``FirstOrderActivationDeGroote2016``.

        Parameters
        ==========
        activation time constant : Symbol | Number | None
            The value of the activation time constant governing the delay
            between excitation and activation when excitation exceeds
            activation.
        deactivation time constant : Symbol | Number | None
            The value of the deactivation time constant governing the delay
            between excitation and activation when activation exceeds
            excitation.
        smoothing_rate : Symbol | Number | None
            The slope of the hyperbolic tangent function used to smooth between
            the switching of the equations where excitation exceed activation
            and where activation exceeds excitation. The recommended value to
            use is ``10``, but values between ``0.1`` and ``100`` can be used.

        N)rK   r   activation_time_constantdeactivation_time_constantsmoothing_rate)r   r   rX   rY   rZ   rL   r   r   r     s   
z)FirstOrderActivationDeGroote2016.__init__c                 C   s&   t d}t d}t d}| ||||S )aw  Alternate constructor that will use the published constants.

        Explanation
        ===========

        Returns an instance of ``FirstOrderActivationDeGroote2016`` using the
        three constant values specified in the original publication.

        These have the values:

        :math:`tau_a = 0.015`
        :math:`tau_d = 0.060`
        :math:`b = 10`

        z0.015z0.060z10.0)r   )r   r   tau_atau_dbr   r   r   r   8  s   z.FirstOrderActivationDeGroote2016.with_defaultsc                 C   r    )zDelay constant for activation.

        Explanation
        ===========

        The alias ```tau_a`` can also be used to access the same attribute.

        _tau_ar"   r   r   r   rX   N  r$   z9FirstOrderActivationDeGroote2016.activation_time_constantc                 C   R   t | drdt| d| j d}t||d u r$td| j | _d S || _d S )Nr_   z2Can't set attribute `activation_time_constant` to * as it is immutable and already has value .tau_a_)hasattrreprr_   AttributeErrorr   r   )r   r[   msgr   r   r   rX   Z     
(c                 C   r    )zDelay constant for activation.

        Explanation
        ===========

        The alias ``activation_time_constant`` can also be used to access the
        same attribute.

        r^   r"   r   r   r   r[   e     z&FirstOrderActivationDeGroote2016.tau_ac                 C   r    )zDelay constant for deactivation.

        Explanation
        ===========

        The alias ``tau_d`` can also be used to access the same attribute.

        _tau_dr"   r   r   r   rY   r  r$   z;FirstOrderActivationDeGroote2016.deactivation_time_constantc                 C   r`   )Nrk   z4Can't set attribute `deactivation_time_constant` to ra   rb   tau_d_)rd   re   rk   rf   r   r   )r   r\   rg   r   r   r   rY   ~  rh   c                 C   r    )zDelay constant for deactivation.

        Explanation
        ===========

        The alias ``deactivation_time_constant`` can also be used to access the
        same attribute.

        rj   r"   r   r   r   r\     ri   z&FirstOrderActivationDeGroote2016.tau_dc                 C   r    )zSmoothing constant for the hyperbolic tangent term.

        Explanation
        ===========

        The alias ``b`` can also be used to access the same attribute.

        _br"   r   r   r   rZ     r$   z/FirstOrderActivationDeGroote2016.smoothing_ratec                 C   sN   t | drd|d| jd}t||d u r"td| j | _d S || _d S )Nrn   z(Can't set attribute `smoothing_rate` to ra   rb   b_)rd   rn   rf   r   r   )r   r]   rg   r   r   r   rZ     s   
(c                 C   r    )zSmoothing constant for the hyperbolic tangent term.

        Explanation
        ===========

        The alias ``smoothing_rate`` can also be used to access the same
        attribute.

        rm   r"   r   r   r   r]     ri   z"FirstOrderActivationDeGroote2016.bc                 C   r   )r)   rO   r   r"   r   r   r   r*     rM   z&FirstOrderActivationDeGroote2016.orderc                 C   rR   )r+   rT   r   r"   r   r   r   r,        z+FirstOrderActivationDeGroote2016.state_varsc                 C   rR   )r.   rp   r"   r   r   r   r/     rq   z"FirstOrderActivationDeGroote2016.xc                 C   rR   )r0   rS   r"   r   r   r   r1     rq   z+FirstOrderActivationDeGroote2016.input_varsc                 C   rR   )r2   rS   r"   r   r   r   r3     rq   z"FirstOrderActivationDeGroote2016.rc                 C   4   | j | j| jg}dd |D }|rt|S tddS )r4   c                 S      g | ]}|j s|qS r   	is_number.0cr   r   r   
<listcomp>      z>FirstOrderActivationDeGroote2016.constants.<locals>.<listcomp>r   rO   r_   rk   rn   rT   r   r   r5   symbolic_constantsr   r   r   r5        z*FirstOrderActivationDeGroote2016.constantsc                 C   rr   )a  Ordered column matrix of non-time varying symbols present in ``M``
        and ``F``.

        Explanation
        ===========

        Only symbolic constants are returned. If a numeric type (e.g. ``Float``)
        has been used instead of ``Symbol`` for a constant then that attribute
        will not be included in the matrix returned by this property. This is
        because the primary use of this property attribute is to provide an
        ordered sequence of the still-free symbols that require numeric values
        during code generation.

        The alias ``constants`` can also be used to access the same attribute.

        c                 S   rs   r   rt   rv   r   r   r   ry     rz   z6FirstOrderActivationDeGroote2016.p.<locals>.<listcomp>r   rO   r{   r|   r   r   r   r7     r~   z"FirstOrderActivationDeGroote2016.pc                 C   s   t tdgS )r8   rO   )rT   r   r"   r   r   r   r9     s   z"FirstOrderActivationDeGroote2016.Mc                 C   rR   )r;   rT   _da_eqnr"   r   r   r   r<   -  s   z"FirstOrderActivationDeGroote2016.Fc                 C   rR   )a  Ordered column matrix of equations for the solution of ``M x' = F``.

        Explanation
        ===========

        The solution to the linear system of ordinary differential equations
        governing the activation dynamics:

        ``M(x, r, t, p) x' = F(x, r, t, p)``.

        r   r"   r   r   r   r=   <  s   z$FirstOrderActivationDeGroote2016.rhsc                 C   st   t dd}|t| j| j| j   }|t dd| j  }|| | j|  }|||  | j }|| | j| j  }|S )NrO         )r   r	   rn   r   r   r_   rk   )r   HALFa0a1a2a3activation_dynamics_equationr   r   r   r   J  s   
z(FirstOrderActivationDeGroote2016._da_eqnc                 C   sL   t | t |kr
dS | j| j| j| jf}|j|j|j|jf}||kr$dS dS )z8Equality check for ``FirstOrderActivationDeGroote2016``.FT)r>   r   r[   r\   r]   )r   r?   
self_attrsother_attrsr   r   r   r@   T  s   z'FirstOrderActivationDeGroote2016.__eq__c              
   C   s.   | j j d| jd| jd| jd| jd
S )z7Representation of ``FirstOrderActivationDeGroote2016``.rA   z, activation_time_constant=z, deactivation_time_constant=z, smoothing_rate=rB   )rC   rD   r   r[   r\   r]   r"   r   r   r   rE   ^  s   z)FirstOrderActivationDeGroote2016.__repr__)NNN)rD   rF   rG   rH   r   rI   r   rJ   rX   setterr[   rY   r\   rZ   r]   r*   r,   r/   r1   r3   r5   r7   r9   r<   r=   r   r   r@   rE   rW   r   r   rL   r   r     sb    ' 










	










	
r   N)rH   abcr   r   	functoolsr   sympy.core.symbolr   sympy.core.numbersr   r   r   %sympy.functions.elementary.hyperbolicr	   sympy.matrices.denser
   rT   r   !sympy.physics.biomechanics._mixinr   sympy.physics.mechanicsr   __all__r   r   r   r   r   r   r   <module>   s     p d