Skip to content

Commit

Permalink
Code simplification and property usage
Browse files Browse the repository at this point in the history
  • Loading branch information
cmccomb committed Dec 15, 2023
1 parent 0655f12 commit 0e43f6c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 71 deletions.
4 changes: 2 additions & 2 deletions trussme/evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"connections": numpy.ndarray,
"loads": numpy.ndarray,
"reactions": numpy.ndarray,
"area": numpy.ndarray,
"elastic_modulus": numpy.ndarray
"area": float,
"elastic_modulus": float
})


Expand Down
62 changes: 16 additions & 46 deletions trussme/member.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,78 +132,48 @@ def __init__(self, joint_a: Joint, joint_b: Joint):
# Material properties
self.material: Material = None

# Dependent variables
self.area: float = 0.0 # Cross-sectional area
self.I: float = 0.0 # Moment of inertia
self.LW: float = 0.0 # Linear weight

# Variables to store information about truss state
self.force: float = 0
self.fos_yielding: float = 0
self.fos_buckling: float = 0
self.mass: float = 0

# Variable to store location in truss
self.joints = [joint_a, joint_b]
self.length: float = 0.0
self.end_a = []
self.end_b = []

# Calculate properties
self.set_shape(Pipe(t=0.002, r=0.02), update_props=False)
self.set_material(MATERIALS[0], update_props=True)

self.calc_properties()

def set_shape(self, new_shape: Shape, update_props: bool = True):
self.shape = new_shape

# If required, update properties
if update_props:
self.calc_properties()

def set_material(self, new_material: Material, update_props: bool = True):
# Set material properties
self.material = new_material

# If required, update properties
if update_props:
self.calc_properties()

def calc_properties(self):
# Calculate moment of inertia
self.calc_moi()

# Calculate the cross-sectional area
self.calc_area()

# Calculate the linear mass
self.calc_lw()
@property
def moment_of_inertia(self) -> float:
return self.shape.moi()

# Update length, etc.
self.calc_geometry()

def calc_moi(self):
self.I = self.shape.moi()
@property
def area(self) -> float:
return self.shape.area()

def calc_area(self):
self.area = self.shape.area()
@property
def linear_weight(self) -> float:
return self.area * self.material["density"]

def calc_lw(self):
self.LW = self.area * self.material["density"]
@property
def length(self) -> float:
return numpy.linalg.norm(self.joints[0].coordinates - self.joints[1].coordinates)

def calc_geometry(self):
self.end_a = self.joints[0].coordinates
self.end_b = self.joints[1].coordinates
self.length = numpy.linalg.norm(self.end_a - self.end_b)
self.mass = self.length*self.LW
@property
def mass(self) -> float:
return self.length*self.linear_weight

def set_force(self, the_force):
self.force = the_force
self.fos_yielding = self.material["yield_strength"]/abs(self.force/self.area)
self.fos_buckling = -((numpy.pi**2)*self.material["elastic_modulus"]*self.I
self.fos_buckling = -((numpy.pi**2)*self.material["elastic_modulus"]*self.moment_of_inertia
/(self.length**2))/self.force

def update_joints(self, joint_a, joint_b):
self.joints = [joint_a, joint_b]
self.calc_geometry()
4 changes: 2 additions & 2 deletions trussme/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def print_stress_analysis(f, the_truss, verbose=False):
for m in the_truss.members:
rows.append("Member_"+"{0:02d}".format(m.idx))
data.append([m.area,
format(m.I, '.2e'),
format(m.moment_of_inertia, '.2e'),
format(m.force/pow(10, 3), '.2f'),
m.fos_yielding,
m.fos_buckling if m.fos_buckling > 0 else "N/A"])
Expand Down Expand Up @@ -263,7 +263,7 @@ def print_recommendations(f, the_truss, verbose=False):
if m.fos_yielding < tyf:
pw(f, "\t- Member_"+'{0:02d}'.format(m.idx)+" is yielding. "
"Try increasing the cross-sectional area.", v=verbose)
pw(f, "\t\t- Current area: " + format(m.I, '.2e') + " m^2", v=verbose)
pw(f, "\t\t- Current area: " + format(m.moment_of_inertia, '.2e') + " m^2", v=verbose)
pw(f, "\t\t- Recommended area: "
+ format(m.area*the_truss.goals["min_fos_yielding"]
/ m.fos_yielding, '.2e') + " m^2", v=verbose)
Expand Down
41 changes: 20 additions & 21 deletions trussme/truss.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,7 @@ def __init__(self):
# Make a list to store materials in
self.materials: list[Material] = [MATERIALS[0]]

# Variables to store number of joints and members
self.number_of_joints: int = 0
self.number_of_members: int = 0

# Variables to store truss characteristics
self.mass: float = 0.0
self.fos_yielding: float = 0.0
self.fos_buckling: float = 0.0
self.fos_total: float = 0.0
Expand All @@ -43,6 +38,21 @@ def __init__(self):
"max_deflection": -1}
self.THERE_ARE_GOALS: bool = False

@property
def number_of_members(self) -> int:
return len(self.members)

@property
def number_of_joints(self) -> int:
return len(self.joints)

@property
def mass(self):
mass = 0
for m in self.members:
mass += m.mass
return mass

def set_goal(self, **kwargs):
self.THERE_ARE_GOALS = True
for key in kwargs:
Expand All @@ -67,38 +77,29 @@ def set_goal(self, **kwargs):
def add_support(self, coordinates: NDArray[float], d: int = 3):
# Make the joint
self.joints.append(Joint(coordinates))
self.joints[self.number_of_joints].pinned(d=d)
self.joints[-1].idx = self.number_of_joints
self.number_of_joints += 1
self.joints[-1].pinned(d=d)
self.joints[-1].idx = self.number_of_joints -1

def add_joint(self, coordinates: NDArray[float], d: int = 3):
# Make the joint
self.joints.append(Joint(coordinates))
self.joints[self.number_of_joints].free(d=d)
self.joints[-1].idx = self.number_of_joints
self.number_of_joints += 1
self.joints[-1].free(d=d)
self.joints[-1].idx = self.number_of_joints -1

def add_member(self, joint_index_a: int, joint_index_b: int):
# Make a member
self.members.append(Member(self.joints[joint_index_a],
self.joints[joint_index_b]))

self.members[-1].idx = self.number_of_members
self.members[-1].idx = self.number_of_members - 1

# Update joints
self.joints[joint_index_a].members.append(self.members[-1])
self.joints[joint_index_b].members.append(self.members[-1])

self.number_of_members += 1

def move_joint(self, joint_index: int, coordinates: NDArray[float]):
self.joints[joint_index].coordinates = coordinates

def calc_mass(self):
self.mass = 0
for m in self.members:
self.mass += m.mass

def set_load(self, joint_index: int, load: NDArray[float]):
self.joints[joint_index].loads = load

Expand Down Expand Up @@ -176,8 +177,6 @@ def calc_fos(self):

def __report(self, file_name: str = "", verbose: bool = False):

# DO the calcs
self.calc_mass()
self.calc_fos()

if file_name is "":
Expand Down

0 comments on commit 0e43f6c

Please sign in to comment.