In [1]:
from math import sqrt
import torch
import matplotlib.pyplot as plt
from torchfem import Truss
torch.set_default_dtype(torch.double)
Figure 5.1 - Truss sample¶
In [2]:
n1 = torch.linspace(0.0, 4.0, 5)
n2 = torch.linspace(0.0, 1.0, 2)
n1, n2 = torch.stack(torch.meshgrid(n1, n2, indexing="xy"))
nodes = torch.stack([n1.ravel(), n2.ravel()], dim=1)
elements = torch.tensor(
[
[0, 1],
[1, 2],
[2, 3],
[3, 4],
[5, 6],
[6, 7],
[7, 8],
[8, 9],
[1, 5],
[0, 6],
[2, 6],
[1, 7],
[3, 7],
[2, 8],
[4, 8],
[3, 9],
[1, 6],
[2, 7],
[3, 8],
[4, 9],
]
)
truss = Truss(nodes, elements)
truss.forces[4, 1] = -0.1
truss.constraints[0, 0] = True
truss.constraints[0, 1] = True
truss.constraints[5, 0] = True
truss.areas[:] = 10.0
truss.moduli[:] = 1.0
u, f = truss.solve()
sigma = truss.compute_stress(u)
truss.plot()
plt.savefig("../figures/truss_sample.svg", bbox_inches="tight")
plt.show()
Figure 5.2 - Single truss element¶
In [3]:
nodes = torch.tensor([[0.0, 0.0], [1.0, 0.5]])
elements = torch.tensor([[0, 1]])
plt.figure(figsize=(3, 3))
single_truss = Truss(nodes, elements)
single_truss.plot()
plt.savefig("../figures/single_truss.svg", bbox_inches="tight")
plt.show()
Figure 5.3 - Truss sample¶
In [4]:
truss.plot(u=u, sigma=sigma)
plt.savefig("../figures/truss_sample_solved.svg", bbox_inches="tight")
plt.show()
Example 26 - Three bar truss¶
Consider the truss shown below, which is subjected to a force $P$ indicated by the gray arrow and supports indicated by gray triangles. It has three nodes $$ \mathcal{N} = \{\mathbf{x}^0=(1,0)^\top, \mathbf{x}^1=(0,0)^\top,\mathbf{x}^2=(0,1)^\top \} $$ and three elements $$ \mathcal{E} = \{(\mathbf{x}^0, \mathbf{x}^1), (\mathbf{x}^0, \mathbf{x}^2), (\mathbf{x}^1, \mathbf{x}^2)\}. $$
In [5]:
nodes = torch.tensor([[1.0, 0.0], [0.0, 0.0], [0.0, 1.0]])
elements = torch.tensor([[0, 1], [0, 2], [1, 2]])
three_bar_truss = Truss(nodes, elements)
three_bar_truss.forces[0, 1] = -0.2
three_bar_truss.constraints[1, 0] = True
three_bar_truss.constraints[1, 1] = True
three_bar_truss.constraints[2, 0] = True
three_bar_truss.moduli[:] = 10.0
plt.figure(figsize=(3, 3))
three_bar_truss.plot()
plt.savefig("../figures/three_bar_truss.svg", bbox_inches="tight", transparent=True)
plt.show()
u, f = three_bar_truss.solve()
sigma = three_bar_truss.compute_stress(u)
plt.figure(figsize=(3, 3))
three_bar_truss.plot(u=u, sigma=sigma)
plt.savefig(
"../figures/three_bar_truss_solved.svg", bbox_inches="tight", transparent=True
)
plt.show()