Skip to content

Commit

Permalink
Lots of additional cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
cmccomb committed Dec 15, 2023
1 parent fba8393 commit a158d63
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 49 deletions.
10 changes: 5 additions & 5 deletions tests/test_truss.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import unittest
import os
import filecmp
from trussme import truss
import trussme

TEST_TRUSS_FILENAME = os.path.join(os.path.dirname(__file__), 'example.trs')

Expand All @@ -11,7 +11,7 @@ class TestSequenceFunctions(unittest.TestCase):

def test_build_methods(self):
# Build truss from scratch
t1 = truss.Truss()
t1 = trussme.Truss()
t1.add_support(numpy.array([0.0, 0.0, 0.0]), d=2)
t1.add_joint(numpy.array([1.0, 0.0, 0.0]), d=2)
t1.add_joint(numpy.array([2.0, 0.0, 0.0]), d=2)
Expand Down Expand Up @@ -57,7 +57,7 @@ def test_build_methods(self):
max_deflection=6e-3)

# Build truss from file
t2 = truss.Truss(TEST_TRUSS_FILENAME)
t2 = trussme.read_trs(TEST_TRUSS_FILENAME)
t2.set_goal(min_fos_buckling=1.5,
min_fos_yielding=1.5,
max_mass=5.0,
Expand All @@ -79,7 +79,7 @@ def test_build_methods(self):

def test_save_and_rebuild(self):
# Build truss from file
t2 = truss.Truss(TEST_TRUSS_FILENAME)
t2 = trussme.read_trs(TEST_TRUSS_FILENAME)
t2.set_goal(min_fos_buckling=1.5,
min_fos_yielding=1.5,
max_mass=5.0,
Expand All @@ -90,7 +90,7 @@ def test_save_and_rebuild(self):
t2.save_truss(os.path.join(os.path.dirname(__file__), 'asdf.trs'))

# Rebuild
t3 = truss.Truss(os.path.join(os.path.dirname(__file__), 'asdf.trs'))
t3 = trussme.read_trs(os.path.join(os.path.dirname(__file__), 'asdf.trs'))
t3.set_goal(min_fos_buckling=1.5,
min_fos_yielding=1.5,
max_mass=5.0,
Expand Down
2 changes: 2 additions & 0 deletions trussme/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from trussme.truss import Truss, read_trs
from trussme.physical_properties import MATERIALS
1 change: 1 addition & 0 deletions trussme/joint.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from numpy.typing import NDArray
from typing import Literal


class Joint(object):

def __init__(self, coordinates: NDArray[float]):
Expand Down
93 changes: 49 additions & 44 deletions trussme/truss.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

class Truss(object):

def __init__(self, file_name: str = ""):
def __init__(self):
# Make a list to store members in
self.members: list[Member] = []

Expand Down Expand Up @@ -42,48 +42,6 @@ def __init__(self, file_name: str = ""):
"max_deflection": -1}
self.THERE_ARE_GOALS = False

if file_name is not "":
with open(file_name, 'r') as f:
for idx, line in enumerate(f):
if line[0] is "S":
info = line.split()[1:]
self.materials.append({
"name": info[0],
"rho": float(info[1]),
"E": float(info[2]),
"Fy": float(info[3]),
})

elif line[0] is "J":
info = line.split()[1:]
self.add_joint(numpy.array(
[float(x) for x in info[:3]]))
self.joints[-1].translation = numpy.array(
[[int(x)] for x in info[3:]])
elif line[0] is "M":
info = line.split()[1:]
self.add_member(int(info[0]), int(info[1]))
material = next(item for item in self.materials if item["name"] == info[2])
self.members[-1].set_material(material)
self.members[-1].set_shape(info[3])

# Parse parameters
ks = []
vs = []
for param in range(4, len(info)):
kvpair = info[param].split("=")
ks.append(kvpair[0])
vs.append(float(kvpair[1]))
self.members[-1].set_parameters(**dict(zip(ks, vs)))
elif line[0] is "L":
info = line.split()[1:]
self.joints[int(info[0])].loads[0] = float(info[1])
self.joints[int(info[0])].loads[1] = float(info[2])
self.joints[int(info[0])].loads[2] = float(info[3])
elif line[0] is not "#" and not line.isspace():
raise ValueError("'"+line[0] +
"' is not a valid line beginner.")

def set_goal(self, **kwargs):
self.THERE_ARE_GOALS = True
for key in kwargs:
Expand Down Expand Up @@ -293,4 +251,51 @@ def save_truss(self, file_name: str = ""):
f.write("\n")

# Do the loads
f.write(load_string)
f.write(load_string)


def read_trs(file_name: str) -> Truss:
truss = Truss()

with open(file_name, 'r') as f:
for idx, line in enumerate(f):
if line[0] is "S":
info = line.split()[1:]
truss.materials.append({
"name": info[0],
"rho": float(info[1]),
"E": float(info[2]),
"Fy": float(info[3]),
})

elif line[0] is "J":
info = line.split()[1:]
truss.add_joint(numpy.array(
[float(x) for x in info[:3]]))
truss.joints[-1].translation = numpy.array(
[[int(x)] for x in info[3:]])
elif line[0] is "M":
info = line.split()[1:]
truss.add_member(int(info[0]), int(info[1]))
material = next(item for item in truss.materials if item["name"] == info[2])
truss.members[-1].set_material(material)
truss.members[-1].set_shape(info[3])

# Parse parameters
ks = []
vs = []
for param in range(4, len(info)):
kvpair = info[param].split("=")
ks.append(kvpair[0])
vs.append(float(kvpair[1]))
truss.members[-1].set_parameters(**dict(zip(ks, vs)))
elif line[0] is "L":
info = line.split()[1:]
truss.joints[int(info[0])].loads[0] = float(info[1])
truss.joints[int(info[0])].loads[1] = float(info[2])
truss.joints[int(info[0])].loads[2] = float(info[3])
elif line[0] is not "#" and not line.isspace():
raise ValueError("'" + line[0] +
"' is not a valid line beginner.")

return truss

0 comments on commit a158d63

Please sign in to comment.