
    \h                         S r SSKJrJr  SSKJr  SSKJr  SSKJ	r	  SSK
Jr  SSKJr  SSKJrJr  SS	KJr  SS
KJr  SSKJr  SSKrSSKJr  SSKJr  SSKJr  SSKJrJr  SS/0r \" SSS/0S9r! " S S5      r"g)zB
This module can be used to solve problems related
to 2D Trusses.
    )ataninf)Add)INF)Mul)Symbol)sympify)Matrixpi)import_module)sqrt)zerosN)Quantity)plot)doctest_depends_on)sincosz
Truss.draw
matplotlibnumpyfromlistarange)import_kwargsc                   $   \ rS rSrSrS r\S 5       r\S 5       r\S 5       r	\S 5       r
\S 5       r\S	 5       r\S
 5       r\S 5       r\S 5       rS rS rS rS rS rS rS rS rS rS rS r\" SS9S!S j5       rS rS rS rS r S r!g)"Truss   a  
A Truss is an assembly of members such as beams,
connected by nodes, that create a rigid structure.
In engineering, a truss is a structure that
consists of two-force members only.

Trusses are extremely important in engineering applications
and can be seen in numerous real-world applications like bridges.

Examples
========

There is a Truss consisting of four nodes and five
members connecting the nodes. A force P acts
downward on the node D and there also exist pinned
and roller joints on the nodes A and B respectively.

.. image:: truss_example.png

>>> from sympy.physics.continuum_mechanics.truss import Truss
>>> t = Truss()
>>> t.add_node(("node_1", 0, 0), ("node_2", 6, 0), ("node_3", 2, 2), ("node_4", 2, 0))
>>> t.add_member(("member_1", "node_1", "node_4"), ("member_2", "node_2", "node_4"), ("member_3", "node_1", "node_3"))
>>> t.add_member(("member_4", "node_2", "node_3"), ("member_5", "node_3", "node_4"))
>>> t.apply_load(("node_4", 10, 270))
>>> t.apply_support(("node_1", "pinned"), ("node_2", "roller"))
c                     / U l         0 U l        0 U l        0 U l        / U l        / U l        / U l        / U l        0 U l        0 U l	        0 U l
        0 U l        0 U l        g)z
Initializes the class
N)_nodes_members_loads	_supports_node_labels_node_positions_node_position_x_node_position_y_nodes_occupied_member_lengths_reaction_loads_internal_forces_node_coordinatesselfs    _/var/www/auris/envauris/lib/python3.13/site-packages/sympy/physics/continuum_mechanics/truss.py__init__Truss.__init__;   sh     ! " "!!! "!#    c                     U R                   $ )z<
Returns the nodes of the truss along with their positions.
)r   r*   s    r,   nodesTruss.nodesM       
 {{r/   c                     U R                   $ )z'
Returns the node labels of the truss.
)r!   r*   s    r,   node_labelsTruss.node_labelsT   s    
    r/   c                     U R                   $ )z2
Returns the positions of the nodes of the truss.
)r"   r*   s    r,   node_positionsTruss.node_positions[       
 ###r/   c                     U R                   $ )zG
Returns the members of the truss along with the start and end points.
)r   r*   s    r,   membersTruss.membersb   s    
 }}r/   c                     U R                   $ )z1
Returns the length of each member of the truss.
)r&   r*   s    r,   member_lengthsTruss.member_lengthsi   r:   r/   c                     U R                   $ )zi
Returns the nodes with provided supports along with the kind of support provided i.e.
pinned or roller.
)r    r*   s    r,   supportsTruss.supportsp   s     ~~r/   c                     U R                   $ )z(
Returns the loads acting on the truss.
)r   r*   s    r,   loadsTruss.loadsx   r3   r/   c                     U R                   $ )zN
Returns the reaction forces for all supports which are all initialized to 0.
)r'   r*   s    r,   reaction_loadsTruss.reaction_loads   r:   r/   c                     U R                   $ )zM
Returns the internal forces for all members which are all initialized to 0.
)r(   r*   s    r,   internal_forcesTruss.internal_forces   s    
 $$$r/   c                     U GH  nUS   nUS   n[        U5      nUS   n[        U5      nX0R                  ;   a  [        S5      eXE/U R                  R                  5       ;   a  [        S5      eU R                  R                  X4U45        U R                  R                  U5        U R                  R                  XE45        U R                  R                  U5        U R                  R                  U5        XE/U R                  U'   GM
     g)a  
This method adds a node to the truss along with its name/label and its location.
Multiple nodes can be added at the same time.

Parameters
==========
The input(s) for this method are tuples of the form (label, x, y).

label:  String or a Symbol
    The label for a node. It is the only way to identify a particular node.

x: Sympifyable
    The x-coordinate of the position of the node.

y: Sympifyable
    The y-coordinate of the position of the node.

Examples
========

>>> from sympy.physics.continuum_mechanics.truss import Truss
>>> t = Truss()
>>> t.add_node(('A', 0, 0))
>>> t.nodes
[('A', 0, 0)]
>>> t.add_node(('B', 3, 0), ('C', 4, 1))
>>> t.nodes
[('A', 0, 0), ('B', 3, 0), ('C', 4, 1)]
r         z!Node needs to have a unique labelz+A node already exists at the given positionN)
r	   r)   
ValueErrorvaluesr   appendr!   r"   r#   r$   )r+   argsilabelxys         r,   add_nodeTruss.add_node   s    > AaDE!A
AdA
A... !DEE41188:: !NOO ""Ea=1!!((/$$++QF3%%,,Q/%%,,Q/12&&u-% r/   c                    U GH  n[        [        U R                  5      5       H6  nU R                  U   U:X  d  M  U R                  U   nU R
                  U   nM8     X R                  ;  a  [        S5      eU R                  R                  5       nU H8  nX R                  U   S   :X  d  X R                  U   S   :X  d  M/  [        S5      e   U R                  R                  UWW45        U R                  R                  U5        U R                  R                  XE45        U R                  R                  U5        U R
                  R                  U5        X R                  ;   a  U R                  R                  U5        X R                  ;   a  U R                  R                  U5        U R                  R                  U5        GM     g)a   
This method removes a node from the truss.
Multiple nodes can be removed at the same time.

Parameters
==========
The input(s) for this method are the labels of the nodes to be removed.

label:  String or Symbol
    The label of the node to be removed.

Examples
========

>>> from sympy.physics.continuum_mechanics.truss import Truss
>>> t = Truss()
>>> t.add_node(('A', 0, 0), ('B', 3, 0), ('C', 5, 0))
>>> t.nodes
[('A', 0, 0), ('B', 3, 0), ('C', 5, 0)]
>>> t.remove_node('A', 'C')
>>> t.nodes
[('B', 3, 0)]
z No such node exists in the trussr   rN   z0The given node already has member attached to itN)rangelenr1   r!   r#   r$   r)   rP   r   copyr   remover"   r   popr    )r+   rS   rU   rT   rV   rW   members_duplicatemembers           r,   remove_nodeTruss.remove_node   s|   0 E3tzz?+$$Q'50--a0A--a0A ,
 222 !CDD %)MM$6$6$8!/Ff 5a 88E]]SYEZ[\E]<]()[\\ 0 ""E1a=1!!((/$$++QF3%%,,Q/%%,,Q/KK'KKOOE*NN*NN&&u-&&**51/ r/   c                 l   U GH-  nUS   nUS   nUS   nX@R                   ;  d  XPR                   ;  d  XE:X  a  [        S5      eX0R                  ;   a  [        S5      eU R                  R	                  XE45      (       a  [        S5      eXE/U R                  U'   [        U R                   U   S   U R                   U   S   -
  S-  U R                   U   S   U R                   U   S   -
  S-  -   5      U R                  U'   SU R                  XE4'   SU R                  XT4'   SU R                  U'   GM0     g)	a  
This method adds a member between any two nodes in the given truss.

Parameters
==========
The input(s) of the method are tuple(s) of the form (label, start, end).

label: String or Symbol
    The label for a member. It is the only way to identify a particular member.

start: String or Symbol
    The label of the starting point/node of the member.

end: String or Symbol
    The label of the ending point/node of the member.

Examples
========

>>> from sympy.physics.continuum_mechanics.truss import Truss
>>> t = Truss()
>>> t.add_node(('A', 0, 0), ('B', 3, 0), ('C', 2, 2))
>>> t.add_member(('AB', 'A', 'B'), ('BC', 'B', 'C'))
>>> t.members
{'AB': ['A', 'B'], 'BC': ['B', 'C']}
r   rN   rO   z;The start and end points of the member must be unique nodesz9A member with the same label already exists for the trussz-A member already exists between the two nodesTN)r)   rP   r   r%   getr   r&   r(   )r+   rS   rT   rU   startends         r,   
add_memberTruss.add_member   s   6 AaDEaDEA$C222cAWAW6W[`[e !^__--' !\]]%%))5,77 !PQQ ).|e$.2D4J4J34OPQ4RSWSiSijoSpqrSs4svw3w{  |R  |R  SV  |W  XY  |Z  [_  [q  [q  rw  [x  yz  [{  |{  ~  {  4  /@$$U+37$$UZ037$$SZ0/0%%e,' r/   c                    U H  nX R                   ;  a  [        S5      eU R                  R                  U R                   U   S   U R                   U   S   45        U R                  R                  U R                   U   S   U R                   U   S   45        U R                   R                  U5        U R                  R                  U5        U R
                  R                  U5        M     g)a  
This method removes members from the given truss.

Parameters
==========
labels: String or Symbol
    The label for the member to be removed.

Examples
========

>>> from sympy.physics.continuum_mechanics.truss import Truss
>>> t = Truss()
>>> t.add_node(('A', 0, 0), ('B', 3, 0), ('C', 2, 2))
>>> t.add_member(('AB', 'A', 'B'), ('AC', 'A', 'C'), ('BC', 'B', 'C'))
>>> t.members
{'AB': ['A', 'B'], 'AC': ['A', 'C'], 'BC': ['B', 'C']}
>>> t.remove_member('AC', 'BC')
>>> t.members
{'AB': ['A', 'B']}
z"No such member exists in the Trussr   rN   N)r   rP   r%   r_   r&   r(   )r+   rS   rU   s      r,   remove_memberTruss.remove_member%  s    , EMM) !EFF $$(($--*>q*A4==QVCWXYCZ)[\$$(($--*>q*A4==QVCWXYCZ)[\!!%($$((/%%))%0 r/   c                 b   U GH  nUS   nUS   nX0R                   ;  a  [        S5      eX@R                   ;   a  [        S5      eU R                   GHU  nUS   U:X  d  M  XES   US   4U R                  U R                  R                  X5S   US   45      '   X@R                  U R                  R                  US   5      '   U R                   U   U R                   U'   U R                   R                  U5        US   U R                  ;   a=  U R                  US      U R                  U'   U R                  R                  US   5        X@R                  ;   Gaf  U R                  U   S:X  Gao  S[        U5      -   S-   U R                  ;   a  S[        U5      -   S	-   U R                  ;   a  U R                  S[        U5      -   S-      U R                  S[        U5      -   S-   '   U R                  S[        U5      -   S	-      U R                  S[        U5      -   S	-   '   U R                  R                  S[        U5      -   S-   5        U R                  R                  S[        U5      -   S	-   5        U R                  U   U R                  U'   U R                  U    HZ  nUS   S
:X  d  M  US==   [        S[        U5      -   S	-   5      -  ss'   US   S:X  a  U R                  U   R                  U5          O   U R                  U    HZ  nUS   S:X  d  M  US==   [        S[        U5      -   S-   5      -  ss'   US   S:X  a  U R                  U   R                  U5          O   U R                  U[        S[        U5      -   S-   5      S5        U R                  U[        S[        U5      -   S	-   5      S
5        U R                  R                  U5        GO)U R                  U   S:X  a  U R                  U   U R                  U'   U R                  U    HZ  nUS   S
:X  d  M  US==   [        S[        U5      -   S	-   5      -  ss'   US   S:X  a  U R                  U   R                  U5          O   U R                  U[        S[        U5      -   S	-   5      S
5        U R                  R                  U5        OFX0R                  ;   a7  U R                  U   U R                  U'   U R                  R                  U5        U R                   GH  nU R                  U   S   US   :X  a  X@R                  U   S'   SU R                  X@R                  U   S   4'   SU R                  U R                  U   S   U4'   U R                  R                  X0R                  U   S   45        U R                  R                  U R                  U   S   U45        M  U R                  U   S   US   :X  d  M  X@R                  U   S'   SU R                  U R                  U   S   U4'   SU R                  X@R                  U   S   4'   U R                  R                  U R                  U   S   U45        U R                  R                  X0R                  U   S   45        GM     GMX     GM     g)a?  
This method changes the label(s) of the specified node(s).

Parameters
==========
The input(s) of this method are tuple(s) of the form (label, new_label).

label: String or Symbol
    The label of the node for which the label has
    to be changed.

new_label: String or Symbol
    The new label of the node.

Examples
========

>>> from sympy.physics.continuum_mechanics.truss import Truss
>>> t = Truss()
>>> t.add_node(('A', 0, 0), ('B', 3, 0))
>>> t.nodes
[('A', 0, 0), ('B', 3, 0)]
>>> t.change_node_label(('A', 'C'), ('B', 'D'))
>>> t.nodes
[('C', 0, 0), ('D', 3, 0)]
r   rN   z!No such node exists for the Trussz*A node with the given label already existsrO   pinnedR__x_yZ   rollerTN)r)   rP   r   indexr!   r_   r    strr'   r   r   r^   
apply_loadr   r%   )r+   rS   rT   rU   	new_labelnodeloadra   s           r,   change_node_labelTruss.change_node_labelF  s   6 AaDE!I222 !DEE444 !MNN KKDAw%'U^ef`gimnoipTqDKK$5$5u1gtAw6O$PQNW))$*;*;*A*A$q'*JK<@<R<RSX<Y..y9..22597dnn48<tAw8ODNN95 NN..tAw7$6#~~i8HD#'E
?4#74;O;O#OTXY\]bYcTcdhThlp  mA  mA  UAUYUiUijnorsxoyjyz~j~UD$8$8c)n9LT9Q$RUYUiUijnorsxoyjyz~j~UD$8$8c)n9LT9Q$R$($8$8$<$<T#e*_T=Q$R$($8$8$<$<T#e*_T=Q$R9=U9KI 6,0KK	,BD'+Aw"}(,Q6$s5z/$:N3O(O+/7a<,0KK,>,E,Ed,K(- -C -1KK	,BD'+Aw!|(,Q6$s5z/$:N3O(O+/7a<,0KK,>,E,Ed,K(- -C !%	6$s9~BUVZBZ;[]^ _ $	6$s9~BUVZBZ;[]_ ` $ 6!%	!:h!F9=U9KI 6,0KK,>D'+Aw"}(,Q6$s5z/$:N3O(O+/7a<,0KK,>,E,Ed,K(- -? !%	6$s9~BUVZBZ;[]_ ` $ 6$39=U9KI 6 $ 6&*mmF#}}V4Q747B;Df 5a 8^b 4 4ivAVWXAY5Z [^b 4 4dmmF6KA6NPY5Z [ $ 4 4 8 8%vAVWXAY9Z [ $ 4 4 8 8$--:OPQ:RTY9Z [!%v!6q!9T!W!D;Df 5a 8^b 4 4dmmF6KA6NPY5Z [^b 4 4ivAVWXAY5Z [ $ 4 4 8 8$--:OPQ:RTY9Z [ $ 4 4 8 8%vAVWXAY9Z [ '4] ( r/   c                 <   U GH  nUS   nUS   nX0R                   ;  a  [        S5      e[        U R                   5      R                  5       nU H  nXc:X  d  M
  U R                   U   S   U R                   U   S   /U R                   U'   U R                   R	                  U5        U R
                  U   U R
                  U'   U R
                  R	                  U5        U R                  U   U R                  U'   U R                  R	                  U5        M     GM     g)a@  
This method changes the label(s) of the specified member(s).

Parameters
==========
The input(s) of this method are tuple(s) of the form (label, new_label)

label: String or Symbol
    The label of the member for which the label has
    to be changed.

new_label: String or Symbol
    The new label of the member.

Examples
========

>>> from sympy.physics.continuum_mechanics.truss import Truss
>>> t = Truss()
>>> t.add_node(('A', 0, 0), ('B', 3, 0), ('D', 5, 0))
>>> t.nodes
[('A', 0, 0), ('B', 3, 0), ('D', 5, 0)]
>>> t.change_node_label(('A', 'C'))
>>> t.nodes
[('C', 0, 0), ('B', 3, 0), ('D', 5, 0)]
>>> t.add_member(('BC', 'B', 'C'), ('BD', 'B', 'D'))
>>> t.members
{'BC': ['B', 'C'], 'BD': ['B', 'D']}
>>> t.change_member_label(('BC', 'BC_new'), ('BD', 'BD_new'))
>>> t.members
{'BC_new': ['B', 'C'], 'BD_new': ['B', 'D']}
r   rN   z#No such member exists for the TrussN)r   rP   listr]   r_   r&   r(   )r+   rS   rT   rU   rw   r`   ra   s          r,   change_member_labelTruss.change_member_label  s    B AaDE!IMM) !FGG$($7$<$<$>!/F48MM&4I!4Ldmm\bNcdeNf3gi0))%0:>:N:Nu:U,,Y7,,007;?;P;PQV;W--i8--11%8 0 r/   c                    U H  nUS   nUS   nUS   n[        U5      n[        U5      nX0R                  ;  a  [        S5      eX0R                  ;   a!  U R                  U   R	                  XE/5        Mr  XE//U R                  U'   M     g)a  
This method applies external load(s) at the specified node(s).

Parameters
==========
The input(s) of the method are tuple(s) of the form (location, magnitude, direction).

location: String or Symbol
    Label of the Node at which load is applied.

magnitude: Sympifyable
    Magnitude of the load applied. It must always be positive and any changes in
    the direction of the load are not reflected here.

direction: Sympifyable
    The angle, in degrees, that the load vector makes with the horizontal
    in the counter-clockwise direction. It takes the values 0 to 360,
    inclusive.

Examples
========

>>> from sympy.physics.continuum_mechanics.truss import Truss
>>> from sympy import symbols
>>> t = Truss()
>>> t.add_node(('A', 0, 0), ('B', 3, 0))
>>> P = symbols('P')
>>> t.apply_load(('A', P, 90), ('A', P/2, 45), ('A', P/4, 90))
>>> t.loads
{'A': [[P, 90], [P/2, 45], [P/4, 90]]}
r   rN   rO   z$Load must be applied at a known nodeN)r	   r)   rP   r   rR   r+   rS   rT   location	magnitude	directions         r,   rv   Truss.apply_load  s    @ AtH!I!I	*I	*I555 !GHH {{*KK)00)1GH.7-C,DDKK) r/   c                 p   U H  nUS   nUS   nUS   n[        U5      n[        U5      nX0R                  ;  a  [        S5      eXE/U R                  U   ;  a  [        S5      eU R                  U   R	                  XE/5        U R                  U   / :X  d  M  U R                  R                  U5        M     g)a  
This method removes already
present external load(s) at specified node(s).

Parameters
==========
The input(s) of this method are tuple(s) of the form (location, magnitude, direction).

location: String or Symbol
    Label of the Node at which load is applied and is to be removed.

magnitude: Sympifyable
    Magnitude of the load applied.

direction: Sympifyable
    The angle, in degrees, that the load vector makes with the horizontal
    in the counter-clockwise direction. It takes the values 0 to 360,
    inclusive.

Examples
========

>>> from sympy.physics.continuum_mechanics.truss import Truss
>>> from sympy import symbols
>>> t = Truss()
>>> t.add_node(('A', 0, 0), ('B', 3, 0))
>>> P = symbols('P')
>>> t.apply_load(('A', P, 90), ('A', P/2, 45), ('A', P/4, 90))
>>> t.loads
{'A': [[P, 90], [P/2, 45], [P/4, 90]]}
>>> t.remove_load(('A', P/4, 90), ('A', P/2, 45))
>>> t.loads
{'A': [[P, 90]]}
r   rN   rO   z&Load must be removed from a known nodezENo load of this magnitude and direction has been applied at this nodeN)r	   r)   rP   r   r^   r_   r   s         r,   remove_loadTruss.remove_load  s    F AtH!I!I	*I	*I555 !IJJ )X1FF$%lmmKK)00)1GH{{8$*)! r/   c           
         U GHb  nUS   nUS   nX0R                   ;  a  [        S5      eX0R                  ;  a  US:X  aY  U R                  U[	        S[        U5      -   S-   5      S45        U R                  U[	        S[        U5      -   S-   5      S45        OUS	:X  a,  U R                  U[	        S[        U5      -   S-   5      S45        OU R                  U   S:X  a3  US	:X  a,  U R                  U[	        S[        U5      -   S-   5      S45        OEU R                  U   S	:X  a2  US:X  a,  U R                  U[	        S[        U5      -   S-   5      S45        X@R                  U'   GMe     g
)a  
This method adds a pinned or roller support at specified node(s).

Parameters
==========
The input(s) of this method are of the form (location, type).

location: String or Symbol
    Label of the Node at which support is added.

type: String
    Type of the support being provided at the node.

Examples
========

>>> from sympy.physics.continuum_mechanics.truss import Truss
>>> t = Truss()
>>> t.add_node(('A', 0, 0), ('B', 3, 0))
>>> t.apply_support(('A', 'pinned'), ('B', 'roller'))
>>> t.supports
{'A': 'pinned', 'B': 'roller'}
r   rN   z%Support must be added on a known nodern   ro   rp   rq   rr   rs   N)r)   rP   r    rv   r   ru   r   )r+   rS   rT   r   types        r,   apply_supportTruss.apply_support;  sY   0 AtHQ4D555 !HII >>1x'6$s8}:LT:Q3RTU(VW6$s8}:LT:Q3RTV(WX)6$s8}:LT:Q3RTV(WX^^H-9x'(((F4H;Md;R4SUV)WX^^H-9x'6$s8}:LT:Q3RTU(VW+/x(' r/   c           
      
   U H  nX R                   ;  a  [        S5      eX R                  ;  a  [        S5      eU R                  U   S:X  aY  U R                  U[	        S[        U5      -   S-   5      S45        U R                  U[	        S[        U5      -   S-   5      S45        O?U R                  U   S	:X  a,  U R                  U[	        S[        U5      -   S-   5      S45        U R                  R                  U5        M     g
)a  
This method removes support from specified node(s.)

Parameters
==========

locations: String or Symbol
    Label of the Node(s) at which support is to be removed.

Examples
========

>>> from sympy.physics.continuum_mechanics.truss import Truss
>>> t = Truss()
>>> t.add_node(('A', 0, 0), ('B', 3, 0))
>>> t.apply_support(('A', 'pinned'), ('B', 'roller'))
>>> t.supports
{'A': 'pinned', 'B': 'roller'}
>>> t.remove_support('A','B')
>>> t.supports
{}
z No such node exists in the Trussz+No support has been added to the given nodern   ro   rp   r   rq   rr   rs   N)r)   rP   r    r   r   ru   r_   )r+   rS   r   s      r,   remove_supportTruss.remove_supporth  s    . H555 !CDD/ !NOO >>(+x7$$htCM7I$7N0OQR%ST$$htCM7I$7N0OQS%TU^^H-9$$htCM7I$7N0OQS%TU""8, r/   c                    SnU R                    HR  nUS   U R                  ;   d  M  U R                  US      S:X  a  US-  nM5  U R                  US      S:X  d  MM  US-  nMT     S[        U R                   5      -  [        U R                  5      U-   :w  a  [	        S5      e[        S[        U R                   5      -  5       VVs/ s H5  n[        S[        U R                   5      -  5       Vs/ s H  nSPM     snPM7     nnn[        S[        U R                  5      -  S5      nSnU R                    H  nUS   U R                  ;   a  U R                  US       H  nUS   [        S[        US   5      -   S-   5      :w  d  M)  US   [        S[        US   5      -   S	-   5      :w  d  MO  Xg==   US   [        [        US   -  S
-  5      -  -  ss'   XgS-   ==   US   [        [        US   -  S
-  5      -  -  ss'   M     US-  nM     Sn	Sn
U R                    H  nUS   U R                  ;   am  U R                  US      S:X  a*  XZ   U	==   S-  ss'   XZS-      U	S-   ==   S-  ss'   U	S-  n	O-U R                  US      S:X  a  XZS-      U	==   S-  ss'   U	S-  n	U
S-  n
M     U R                   GH  nU R                  U   S   nU R                  U   S   n[        U R                  U   S   U R                  U   S   -
  S-  U R                  U   S   U R                  U   S   -
  S-  -   5      nU R                   R#                  U5      nU R                   R#                  U5      nU R                  U   S   U R                  U   S   -
  U-  nU R                  U   S   U R                  U   S   -
  U-  nU R                  U   S   U R                  U   S   -
  U-  nU R                  U   S   U R                  U   S   -
  U-  nX_S-     U	==   U-  ss'   X_S-  S-      U	==   U-  ss'   UUS-     U	==   U-  ss'   UUS-  S-      U	==   U-  ss'   U	S-  n	GM     [%        U5      S-  U-  n0 U l        Sn[(        nU R                    Hc  nUS   U R                  ;   d  M  U R                  US       H5  n[+        US   5      [        [,        [.        4;  d  M&  [1        UUS   5      nM7     Me     [        [        U5      5       HB  n[+        UU   5      [        [,        [.        4;  d  M&  [3        UU   U-  5      S:  d  M=  SUU'   MD     U R                    H  nUS   U R                  ;   d  M  U R                  US      S:X  aR  UU   U R&                  S[        US   5      -   S-   '   UUS-      U R&                  S[        US   5      -   S	-   '   US-  nM  U R                  US      S:X  d  M  UU   U R&                  S[        US   5      -   S	-   '   US-  nM     U R                   H  nUU   U R4                  U'   US-  nM     gs  snf s  snnf )a  
This method solves for all reaction forces of all supports and all internal forces
of all the members in the truss, provided the Truss is solvable.

A Truss is solvable if the following condition is met,

2n >= r + m

Where n is the number of nodes, r is the number of reaction forces, where each pinned
support has 2 reaction forces and each roller has 1, and m is the number of members.

The given condition is derived from the fact that a system of equations is solvable
only when the number of variables is lesser than or equal to the number of equations.
Equilibrium Equations in x and y directions give two equations per node giving 2n number
equations. However, the truss needs to be stable as well and may be unstable if 2n > r + m.
The number of variables is simply the sum of the number of reaction forces and member
forces.

.. note::
   The sign convention for the internal forces present in a member revolves around whether each
   force is compressive or tensile. While forming equations for each node, internal force due
   to a member on the node is assumed to be away from the node i.e. each force is assumed to
   be compressive by default. Hence, a positive value for an internal force implies the
   presence of compressive force in the member and a negative value implies a tensile force.

Examples
========

>>> from sympy.physics.continuum_mechanics.truss import Truss
>>> t = Truss()
>>> t.add_node(("node_1", 0, 0), ("node_2", 6, 0), ("node_3", 2, 2), ("node_4", 2, 0))
>>> t.add_member(("member_1", "node_1", "node_4"), ("member_2", "node_2", "node_4"), ("member_3", "node_1", "node_3"))
>>> t.add_member(("member_4", "node_2", "node_3"), ("member_5", "node_3", "node_4"))
>>> t.apply_load(("node_4", 10, 270))
>>> t.apply_support(("node_1", "pinned"), ("node_2", "roller"))
>>> t.solve()
>>> t.reaction_loads
{'R_node_1_x': 0, 'R_node_1_y': 20/3, 'R_node_2_y': 10/3}
>>> t.internal_forces
{'member_1': 20/3, 'member_2': 20/3, 'member_3': -20*sqrt(2)/3, 'member_4': -10*sqrt(5)/3, 'member_5': 10}
r   rn   rO   rs   rN   z The given truss cannot be solvedro   rp   rq      g|=N)r   r    r\   r   rP   r[   r   r1   r   r   ru   r   r   r   r   r)   r!   rt   r
   r'   r   r   r   r   minabsr(   )r+   count_reaction_loadsrx   jrT   coefficients_matrixload_matrixload_matrix_rowry   colsrowra   rf   rg   lengthstart_index	end_indexhorizontal_component_startvertical_component_starthorizontal_component_endvertical_component_endforces_matrixmin_loads                          r,   solveTruss.solve  s   T  !KKDAw$..(>>$q'*H4(A-(^^DG,h6(A-(   ST]]!36J!JJ?@@OTUVWZ[_[f[fWgUgOhiOh!53t{{3C1C+DE+Da+DEOhiAc$**o-q1KKDAw$++% KKQ0DAwtCQL'8'= >>47FSWX[\`ab\cXdSdeiSiLjCj#4QBtAwJsN@S8SS4#a$78DGC4PQ7
SVDW<WW8 1 q O   KKDAw$..(>>$q'*H4',T2a72'A.tAv6!;6AID^^DG,h6'A.t494AID1HC   mmFMM&)!,E--'*C411%8;D<R<RSV<WXY<ZZ]^^bfbxbxy~b  AB  cC  DH  DZ  DZ  [^  D_  `a  Db  cb  ef  bf  f  gF++11%8K))//4I*.*@*@*Ea*HI_I_`eIfghIi*ikq)q&(,(>(>s(CA(FtG]G]^cGdefGg(gio'o$(,(>(>u(Ea(HI_I_`cIdefIg(gio'o$&*&<&<U&CA&FtG]G]^aGbcdGe&egm%m"A.t48RR4Aa06:RR6	!,T26NN2	!A.t48NN4AID $   34b8+E!KKDAw$++% KKQ0DDG}VS#,>>#&xa#9 1  
 s=)*AM!$%fc3-??}Q'0158'(M!$ + KKDAw$..(>>$q'*H4CPQRCSD((c$q'l):4)?@CPQRSTQTCUD((c$q'l):4)?@FA^^DG,h6CPQRCSD((c$q'l):4)?@FA   mmF,9!,<D!!&)FA $ 	}  Fis   )Y/Y;YY)r   )modulesNc                 :   [         (       d  [        S5      e[        S5      n/ n/ n/ nU R                  U5      nX6-  nU R	                  5       nXW-  nU R                  5       nX8-  nU R                  5       n	XI-  n[        * n
[        n[        * n[        nU R                   Ho  n[        XR                  U   S   5      n
[        XR                  U   S   5      n[        XR                  U   S   5      n[        XR                  U   S   5      nMq     [        U
S-  US-  -
  S-   US-  US-  -
  S-   5      nXS-  US-  -
  S-   :X  a+  [        SUSS4USXKSU-  -
  U
S-  4USU-  -
  U
S-  4SUS	9	nU$ [        SUSS4USXMSU-  -
  US-  4USU-  -
  US-  4SUS	9	nU$ )
a'  
Returns a plot object of the Truss with all its nodes, members,
supports and loads.

.. note::
    The user must be careful while entering load values in their
    directions. The draw function assumes a sign convention that
    is used for plotting loads.

    Given a right-handed coordinate system with XYZ coordinates,
    the supports are assumed to be such that the reaction forces of a
    pinned support is in the +X and +Y direction while those of a
    roller support is in the +Y direction. For the load, the range
    of angles, one can input goes all the way to 360 degrees which, in the
    the plot is the angle that the load vector makes with the positive x-axis in the anticlockwise direction.

    For example, for a 90-degree angle, the load will be a vertically
    directed along +Y while a 270-degree angle denotes a vertical
    load as well but along -Y.

Examples
========

.. plot::
    :context: close-figs
    :format: doctest
    :include-source: True

    >>> from sympy.physics.continuum_mechanics.truss import Truss
    >>> import math
    >>> t = Truss()
    >>> t.add_node(("A", -4, 0), ("B", 0, 0), ("C", 4, 0), ("D", 8, 0))
    >>> t.add_node(("E", 6, 2/math.sqrt(3)))
    >>> t.add_node(("F", 2, 2*math.sqrt(3)))
    >>> t.add_node(("G", -2, 2/math.sqrt(3)))
    >>> t.add_member(("AB","A","B"), ("BC","B","C"), ("CD","C","D"))
    >>> t.add_member(("AG","A","G"), ("GB","G","B"), ("GF","G","F"))
    >>> t.add_member(("BF","B","F"), ("FC","F","C"), ("CE","C","E"))
    >>> t.add_member(("FE","F","E"), ("DE","D","E"))
    >>> t.apply_support(("A","pinned"), ("D","roller"))
    >>> t.apply_load(("G", 3, 90), ("E", 3, 90), ("F", 2, 90))
    >>> p = t.draw()
    >>> p  # doctest: +ELLIPSIS
    Plot object containing:
    [0]: cartesian line: 1 for x over (1.0, 1.0)
    ...
    >>> p.show()
z-To use this function numpy module is requiredrV   r   rN   皙?皙?Fg?)markersshowannotationsxlimylimaxis
rectangles)r   ImportErrorr   _draw_nodes_draw_members_draw_supports_draw_loadsr   r)   maxr   r   )r+   	subs_dictrV   r   r   r   node_markersmember_rectanglessupport_markersload_annotationsxmaxxminymaxyminrx   lim	sing_plots                    r,   draw
Truss.draw  sv   d uMNN3K
''	2 ..0'
--/"++-'tt**Dt33D9!<=Dt33D9!<=Dt33D9!<=Dt33D9!<=D	 + $s(48#A%tCxS'8':;Sc!!##QAq	7T_lpqtltgtvz{~v~f  HL  MQ  RU  MU  HU  W[  \_  W_  G`  gl  yC  DI
  QAq	7T_lpqtltgtvz{~v~f  HL  MQ  RU  MU  HU  W[  \_  W_  G`  gl  yC  DIr/   c                 `   / nU R                    GHM  n[        U R                   U   S   5      [        [        4;   aF  U R                   U   S   U;   a%  XR                   U   S      U R                   U   S'   O[	        S5      e[        U R                   U   S   5      [
        :X  a  U R                   U   S   R                  5       nU Hj  n[        U5      [        [        4;   d  M  US :X  d  XQ;  a  [	        S5      eU R                   U   S==   U-  ss'   U R                   U   S==   X   -  ss'   Ml     [        U R                   U   S   5      [        [        4;   aH  U R                   U   S   U;   a'  XR                   U   S      U R                   U   S'   GM  [	        S5      e[        U R                   U   S   5      [
        :X  d  GM  U R                   U   S   R                  5       nU Hj  n[        U5      [        [        4;   d  M  US :X  d  XQ;  a  [	        S5      eU R                   U   S==   U-  ss'   U R                   U   S==   X   -  ss'   Ml     GMP     U R                    H=  nUR                  U R                   U   S   /U R                   U   S   //SSSS.5        M?     U$ )Nr   z/provided substituted dictionary is not adequaterN   o   blackrS   marker
markersizecolor)r)   r   r   r   rP   r   as_coeff_MulrR   )r+   r   r   rx   objectsobjects         r,   r   Truss._draw_nodes_  s   **DT++D1!45&(9KK))$/2i?6?@V@VW[@\]^@_6`D**403$%VWWt--d3A673>006q9FFH%FF|'99$d?f.E",-^"__ 2248;vE; 2248;y?PP; & T++D1!45&(9KK))$/2i?6?@V@VW[@\]^@_6`D**403$%VWWt--d3A673>006q9FFH%FF|'99$d?f.E",-^"__ 2248;vE; 2248;y?PP; &/ +> **D!33D9!<=@V@VW[@\]^@_?`a !"#	 + r/   c                 	   / n[         * n[         n[         * n[         nU R                   Ho  n[        X R                  U   S   5      n[        X0R                  U   S   5      n[        X@R                  U   S   5      n[        XPR                  U   S   5      nMq     [	        SU-  SU-  -
  5      [	        SU-  SU-  -
  5      :  a  SU-  SU-  -
  nOSU-  SU-  -
  nU R
                   GH  nU R                  U R
                  U   S      S   n	U R                  U R
                  U   S      S   n
U R                  U R
                  U   S      S   nU R                  U R
                  U   S      S   nX:w  Ga  X:w  Ga  X:  a  UR                  U	SU-  [        [        S-  [        X-
  X-
  -  5      -   5      -  S-  -
  U
SU-  [        [        S-  [        X-
  X-
  -  5      -   5      -  S-  -
  4[        X-
  S-  X-
  S-  -   5      SU-  [        R                  " S5      -  -   SU-  S[        X-
  X-
  -  5      -  [        -  S	S
.5        GMg  UR                  USU-  [        [        S-  [        X-
  X-
  -  5      -   5      -  S-  -
  USU-  [        [        S-  [        X-
  X-
  -  5      -   5      -  S-  -
  4[        X-
  S-  X-
  S-  -   5      SU-  [        R                  " S5      -  -   SU-  S[        X-
  X-
  -  5      -  [        -  S	S
.5        GM1  X:X  a  X:  ad  UR                  U	SU-  S-  -
  U
SU-  S-  -
  4[        X-
  S-  X-
  S-  -   5      SU-  SS[        R                  " SX-
  5      -
  -  S	S
.5        GM  UR                  U	SU-  S-  -
  U
SU-  S-  -
  4[        X-
  S-  X-
  S-  -   5      SU-  SS[        R                  " SX-
  5      -
  -  S	S
.5        GM  X:  aj  UR                  U	SU-  S-  -
  U
SU-  S-  -
  4[        X-
  S-  X-
  S-  -   5      SU-  S-  -   SU-  S[        R                  " SX-
  5      -  S	S
.5        GMr  UR                  USU-  S-  -
  USU-  S-  -
  4[        X-
  S-  X-
  S-  -   5      SU-  S-  -   * SU-  S[        R                  " SX-
  5      -  S	S
.5        GM     U$ )Nr   rN   r   r   g{Gzt?   rO   r   brown)xywidthheightangler   rr   g{Gzt)r   r)   r   r   r   r   rR   r   r   r   r   r   mathcopysign)r+   r   r   r   r   r   rx   max_diffra   x1y1x2y2s                r,   r   Truss._draw_members  s   tt**Dt33D9!<=Dt33D9!<=Dt33D9!<=Dt33D9!<=D	 + s4xD !#c$hs4x&7"884xD(H4xD(HmmF''f(=a(@A!DB''f(=a(@A!DB''f(=a(@A!DB''f(=a(@A!DBv"&5%,,"$U8^C1T2525/=R8R4S%STU%U"UWYZ_`hZhilmopqmqrvxzx}  AC  AF  xG  sH  nH  jI  [I  JK  [K  XK  "L$("%!RUQJ)>$?htyyYZ|@[$[%*8^$'bebe_(=$=b$@$+ &,,"$U8^C1T2525/=R8R4S%STU%U"UWYZ_`hZhilmopqmqrvxzx}  AC  AF  xG  sH  nH  jI  [I  JK  [K  XK  "L$("%!RUQJ)>$?htyyYZ|@[$[%*8^$'bebe_(=$=b$@$+ 5%,,"$U8^A%5"5r%.:J7J!K$("%!RUQJ)>$?%*8^$&$--25*A(A$B$+ &,,"$U8^A%5"5r%.:J7J!K$("%!RUQJ)>$?%+H_$&$--25*A(A$B$+ 5%,,"$U8^A%5"5r%.:J7J!K$("%!RUQJ)>$?hq@P$P%*8^$&t}}Q'>$>$+ &,,"$U8^A%5"5r%.:J7J!K&*BEA:ruqj+@&A%.QRBR&R$S%*8^$&t}}Q'>$>$+w $J ! r/   c           	      4   / n[         * n[         n[         * n[         nU R                   Ho  n[        X R                  U   S   5      n[        X0R                  U   S   5      n[        X@R                  U   S   5      n[        XPR                  U   S   5      nMq     [	        SU-  SU-  -
  5      [	        SU-  SU-  -
  5      :  a  SU-  SU-  -
  nOSU-  SU-  -
  nU R
                   GH*  nU R
                  U   S:X  a}  UR                  U R                  U   S   /U R                  U   S   //SSSS	S
.5        UR                  U R                  U   S   /U R                  U   S   SU-  -
  //SSSS.5        M  U R
                  U   S:X  d  M  UR                  U R                  U   S   /U R                  U   S   SU-  -
  //SSSS	S
.5        UR                  U R                  U   S   /U R                  U   S   SU-  -
  //SSSS.5        GM-     U$ )Nr   rN   r   r   rn         r   none)rS   r   r   r   markerfacecolorgQ?_   r   rs   g{Gz?r      g333333?)r   r)   r   r   r   r    rR   )r+   r   r   r   r   r   rx   r   s           r,   r   Truss._draw_supports  s   tt**Dt33D9!<=Dt33D9!<=Dt33D9!<=Dt33D9!<=D	 +
 s4xD !#c$hs4x&7"884xD(H4xD(HNND~~d#X-&& "33D9!<=!33D9!<=  "#%' '*0	  && "33D9!<=!33D9!<U8^KL  "%%' '
 %x/&& "33D9!<=!33D9!<T(]JK  "%%' '*0	  && "33D9!<=!33D9!<VH_LM  "%%' '
O #d r/   c                    / n[         * n[         n[         * n[         nU R                   Ho  n[        X R                  U   S   5      n[        X0R                  U   S   5      n[        X@R                  U   S   5      n[        XPR                  U   S   5      nMq     [	        SU-  SU-  -
  5      [	        SU-  SU-  -
  5      :  a  SU-  SU-  -
  S-   nOSU-  SU-  -
  S-   nU R
                   GH  nU R
                  U    GHh  nUS   [        S[        U5      -   S-   5      [        S[        U5      -   S-   5      4;   a  MA  U R                  U   S   n	U R                  U   S   n
UR                  S	U	[        R                  " [        US   -  S
-  5      US-  -  -
  U
[        R                  " [        US   -  S
-  5      US-  -  -
  4XS-  [	        X#-
  5      -   [	        XE-
  5      -   [        R                  " [        US   -  S
-  5      -  S-  -
  XS-  [	        X#-
  5      -   [	        XE-
  5      -   [        R                  " [        US   -  S
-  5      -  S-  -
  4SSSSS.S.5        GMk     GM     U$ )Nr   rN   r   r   r   ro   rp   rq    r   d      g      ?r   )r   
headlength	headwidth	facecolor)textr   xytext
arrowprops)r   r)   r   r   r   r   r   ru   rR   r   r   r   r   )r+   r   r   r   r   r   rx   r   ry   rV   rW   s              r,   r   Truss._draw_loads-  s   tt**Dt33D9!<=Dt33D9!<=Dt33D9!<=Dt33D9!<=D	 + s4xD !#c$hs4x&7"884xD(*H4xD(*HKKDD)7vd3t9nT&9:F4D	>RVCV<WXX**403**403 ''!dhhr$q'z#~6EEdhhr$q'z#~6EE
 |C	N:3ty>I488TVW[\]W^T^_bTbKccdfff|C	N:3ty>I488TVW[\]W^T^_bTbKccdfff" /2q^e%f *  (  r/   )r(   r   r&   r   r)   r!   r#   r$   r"   r   r%   r'   r    )N)"__name__
__module____qualname____firstlineno____doc__r-   propertyr1   r5   r8   r<   r?   rB   rE   rH   rK   rX   rb   rh   rk   rz   r~   rv   r   r   r   r   r   r   r   r   r   r   __static_attributes__ r/   r,   r   r      s;   8$$   ! ! $ $   $ $     $ $ % %17j/2f.1`1B]\~/9b.E`3*j+0Z%-Nqf 
+Y ,Yx+ZY!vDL' r/   r   )#r   cmathr   r   sympy.core.addr   sympy.core.evalfr   sympy.core.mulr   sympy.core.symbolr   sympy.core.sympifyr	   sympyr
   r   sympy.external.importtoolsr   (sympy.functions.elementary.miscellaneousr   sympy.matrices.denser   r   sympy.physics.units.quantitiesr   sympy.plottingr   sympy.utilities.decoratorr   r   r   __doctest_requires__r   r   r   r/   r,   <module>r
     si         $ &  4 9 &  3  8  &7  	gj(-DEv  v r/   