Skip to content

Commit

Permalink
Doing more with properties
Browse files Browse the repository at this point in the history
  • Loading branch information
cmccomb committed Jan 1, 2024
1 parent 37fd864 commit a3efb5c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 36 deletions.
16 changes: 16 additions & 0 deletions trussme/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,22 @@ def direction(self) -> NDArray[float]:
)
return vector_length / numpy.linalg.norm(vector_length)

@property
def stiffness(self) -> float:
"""float: The axial stiffness of the member"""
return self.elastic_modulus * self.area / self.length

@property
def stiffness_vector(self) -> NDArray[float]:
"""NDArray[float]: The vector stiffness vector of the member"""
return self.stiffness * self.direction

@property
def stiffness_matrix(self) -> NDArray[float]:
"""NDArray[float]: The local stiffness matrix of the member"""
d2 = numpy.outer(self.direction, self.direction)
return self.stiffness * numpy.block([[d2, -d2], [-d2, d2]])

@property
def mass(self) -> float:
"""float: The total mass of the member"""
Expand Down
49 changes: 13 additions & 36 deletions trussme/truss.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,24 +306,12 @@ def __load_matrix(self) -> NDArray[float]:

return loads

@property
def __coordinate_matrix(self) -> NDArray[float]:
return numpy.array([joint.coordinates for joint in self.joints]).T

@property
def __connection_matrix(self) -> NDArray[float]:
return numpy.array(
[[member.begin_joint.idx, member.end_joint.idx] for member in self.members]
).T

@property
def __areas(self) -> NDArray[float]:
return numpy.array([member.area for member in self.members])

@property
def __elastic_moduli(self) -> NDArray[float]:
return numpy.array([member.elastic_modulus for member in self.members])

def analyze(self):
"""
Analyze the truss
Expand All @@ -338,13 +326,10 @@ def analyze(self):
"""
loads = self.__load_matrix
coordinates = self.__coordinate_matrix
connections = self.__connection_matrix
reactions = numpy.array(
[joint.translation_restricted for joint in self.joints]
).T
elastic_modulus = self.__elastic_moduli
area = self.__areas

tj: NDArray[float] = numpy.zeros([3, self.number_of_members])
dof: NDArray[float] = numpy.zeros(
Expand All @@ -357,17 +342,9 @@ def analyze(self):
ff: NDArray[float] = numpy.where(deflections.T.flat == 1)[0]

for idx, member in enumerate(self.members):
d2 = numpy.outer(member.direction, member.direction)
ss = (
member.elastic_modulus
* member.area
/ member.length
* numpy.block([[d2, -d2], [-d2, d2]])
)
ss = member.stiffness_matrix
tj[:, idx] = member.stiffness_vector

tj[:, idx] = (
member.elastic_modulus * member.area / member.length * member.direction
)
e = list(
range((3 * member.begin_joint.idx), (3 * member.begin_joint.idx + 3))
) + list(range((3 * member.end_joint.idx), (3 * member.end_joint.idx + 3)))
Expand All @@ -386,13 +363,6 @@ def analyze(self):
ff = numpy.where(deflections.T == 1)
for i in range(len(ff[0])):
deflections[ff[1][i], ff[0][i]] = flat_deflections[i]
forces = numpy.sum(
numpy.multiply(
tj,
deflections[:, connections[1, :]] - deflections[:, connections[0, :]],
),
axis=0,
)

# Compute the reactions
reactions = (
Expand All @@ -401,10 +371,6 @@ def analyze(self):
.T
)

# Store the results
for i in range(self.number_of_members):
self.members[i].force = forces[i]

# Store the results
for i in range(self.number_of_joints):
for j in range(3):
Expand All @@ -415,6 +381,17 @@ def analyze(self):
self.joints[i].reactions[j] = 0.0
self.joints[i].deflections[j] = float(deflections[j, i])

forces = numpy.sum(
numpy.multiply(
tj,
deflections[:, connections[1, :]] - deflections[:, connections[0, :]],
),
axis=0,
)
# Store the results
for i in range(self.number_of_members):
self.members[i].force = forces[i]

def to_json(self, file_name: str) -> None:
"""
Saves the truss to a JSON file
Expand Down

0 comments on commit a3efb5c

Please sign in to comment.