材料力学「2部材トラス,3部材,複雑なトラス いろいろ(その3/3)」sympyのTrussで。

:::note info
[(その1/3)](https://qiita.com/mrrclb48z/items/b49395a9a0b358c9d604)
[(その2/3)](https://qiita.com/mrrclb48z/items/118385a1bcc196e3540d)
(その2/3) 本ページ
:::

# 複雑なトラスをsympyのTrussで
・荷重を受ける複雑なトラスをsympyのTrussで
#### ver0.1  
```python
# ver0.1
from sympy.physics.continuum_mechanics.truss import Truss
import math
from sympy import *
P=1  # var('P')
L=1  # var('L')
H=Rational(1,2)*L*sqrt(3)
t = Truss()
t.add_node     (("A",Rational(1,2)*L,H),("B",Rational(1,2)*L+L,H)) 
t.add_node     (("C",0              ,0),("D",1*L              ,0),("E",2*L,0)) 
t.add_member   (("AB","A","B"))   
t.add_member   (("CA","C","A"),("AD","A","D"),("DB","D","B"),("BE","B","E"))   
t.add_member   (("CD","C","D"),("DE","D","E"))   
t.apply_support(("C" ,"pinned"),("E","roller"))
t.apply_load   (("D",P,270.0))
p = t.draw()
p.show()
t.solve()
# print("#",t.reaction_loads )           
# print("#",t.internal_forces)
# 
var('Q,A,E,l')
U= (Q*t.internal_forces["AB"])**2*l/(2*A*E) \
  +(Q*t.internal_forces["CA"])**2*l/(2*A*E) \
  +(Q*t.internal_forces["AD"])**2*l/(2*A*E) \
  +(Q*t.internal_forces["DB"])**2*l/(2*A*E) \
  +(Q*t.internal_forces["BE"])**2*l/(2*A*E) \
  +(Q*t.internal_forces["CD"])**2*l/(2*A*E) \
  +(Q*t.internal_forces["DE"])**2*l/(2*A*E)
rep={Q:4*10**3,l:3,A:20*10**(-6),E:200*10**9}
print("#",round(diff(U,Q).subs(rep)*10**3,1))
# 5.5
```
・縦横比は1:1でありません。

![111.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/109698/c91747dd-d0a4-4890-8dc6-b2f504ba7315.png)

#### ver0.2(ver0.1と同じです。)　
・数値計算を何が何でも1行で。
```python
# ver0.2
from sympy.physics.continuum_mechanics.truss import Truss
import math
from sympy import *
P=1  # var('P')
L=1  # var('L')
H=Rational(1,2)*L*sqrt(3)
t = Truss()
t.add_node     (("A",Rational(1,2)*L,H),("B",Rational(1,2)*L+L,H)) 
t.add_node     (("C",0              ,0),("D",1*L              ,0),("E",2*L,0)) 
t.add_member   (("AB","A","B"))   
t.add_member   (("CA","C","A"),("AD","A","D"),("DB","D","B"),("BE","B","E"))   
t.add_member   (("CD","C","D"),("DE","D","E"))   
t.apply_support(("C" ,"pinned"),("E","roller"))
t.apply_load   (("D",P,270.0))
p = t.draw()
# p.show()
t.solve()
# print("#",t.reaction_loads )           
# print("#",t.internal_forces)
# 
var('Q,A,E,l')
rep={Q:4*10**3,l:3,A:20*10**(-6),E:200*10**9}
print("#",round(sum({k:(Q*v)**2*l/(2*A*E) for k,v in t.internal_forces.items()}.values()).diff(Q) \
                    .subs(rep) \
               *10**3,1))
# 5.5
```
#### ver1.1
・Trussの [「property member_lengths」](https://docs.sympy.org/latest/modules/physics/continuum_mechanics/truss.html#sympy.physics.continuum_mechanics.truss.Truss.member_lengths)がありました。
```python
# ver1.1
from sympy.physics.continuum_mechanics.truss import Truss
from sympy import *
var('P')
var('L,H,l,A,E',positive=True)  
def myTruss(L):
    def myAdd_member(*args):
        def make_triples(s):
            return (s,s[0],s[1])
        for s in args:
            t.add_member(make_triples(s))      
    t = Truss()
    t.add_node     (('A',Rational(1,2)*L  ,Rational(1,2)*L*sqrt(3)), \
                    ('B',Rational(1,2)*L+L,Rational(1,2)*L*sqrt(3))) 
    t.add_node     (('C',0,0),('D',1*L,0),('E',2*L,0)) 
    myAdd_member   ( 'AB','CA','AD','DB','BE','CD','DE')  
    t.apply_support(('C' ,'pinned'),('E','roller'))
    t.apply_load   (('D',P,270.0))   
    return t
# -------------------------------------------------------------------------------------
myTruss(1).draw().show()       # 作図(l=1)
t=myTruss(l);t.solve()         # 計算のみ
δV=sum({k:1/(2*A*E)*v1**2*v2  \
        for (k,v1),(_,v2) in zip(t.internal_forces.items(),t.member_lengths.items())} \
        .values()).diff(P).simplify()
print('#',      δV)
print('#',round(δV.subs({P:4*10**3,l:3,A:20*10**(-6),E:200*10**9})*10**3,1))
# 11*P*l/(6*A*E)
# 5.5
```

# いつもの? sympyの実行環境と 参考のおすすめです。
[(テンプレート)](https://qiita.com/mrrclb48z/items/00dd08b0317069be9342)

# いつもと違うおすすめです。

#### 文献

https://www.amazon.co.jp/%E6%9D%90%E6%96%99%E5%8A%9B%E5%AD%A6-JSME%E3%83%86%E3%82%AD%E3%82%B9%E3%83%88%E3%82%B7%E3%83%AA%E3%83%BC%E3%82%BA-%E6%97%A5%E6%9C%AC%E6%A9%9F%E6%A2%B0%E5%AD%A6%E4%BC%9A/dp/4888981582