The following is the example code.
- You could decide to either just type all the data about the truss in
.py
file or read the data in.json
file. As for .json file, we will discuss it later. - If you want to do structural analysis on 2D truss, just switch the dimension of truss by changing the value of variable
TRUSS_DIMENSION
(Only can be 2 or 3). - By the way, you could use
slientruss3d.plot.TrussPlotter
to plot the result of structural analysis for your truss. We will discuss its details in Here !
from slientruss3d.truss import Truss
from slientruss3d.type import SupportType, MemberType
def TestExample():
# -------------------- Global variables --------------------
TEST_OUTPUT_FILE = f"./test_output.json"
TRUSS_DIMENSION = 3
# ----------------------------------------------------------
# Truss object:
truss = Truss(dim=TRUSS_DIMENSION)
# Truss settings:
joints = [(0, 0, 0), (360, 0, 0), (360, 180, 0), (0, 200, 0), (120, 100, 180)]
supports = [SupportType.PIN, SupportType.ROLLER_Z, SupportType.PIN, SupportType.PIN, SupportType.NO]
forces = [(1, (0, -10000, 5000))]
members = [(0, 4), (1, 4), (2, 4), (3, 4), (1, 2), (1, 3)]
memberType = MemberType(1, 1e7, 1)
# Read data in this [.py]:
for joint, support in zip(joints, supports):
truss.AddNewJoint(joint, support)
for jointID, force in forces:
truss.AddExternalForce(jointID, force)
for jointID0, jointID1 in members:
truss.AddNewMember(jointID0, jointID1, memberType)
# Do direct stiffness method:
truss.Solve()
# Dump all the structural analysis results into a .json file:
truss.DumpIntoJSON(TEST_OUTPUT_FILE)
# Get result of structural analysis:
displace, stress, resistance = truss.GetDisplacements(), truss.GetInternalStresses(), truss.GetResistances()
return displace, stress, resistance
(Not every method or property is listed here)
Truss(dim) -> None
dim
: Dimension of the truss (only can be2
or3
).
Truss.AddNewJoint(vector, supportType=SupportType.NO) -> None
-
vector
: Position of each joints in the truss. -
supportType
: Support type of the joint. The following is the options of support type in slientruss3d:- SupportType.NO (not a support)
- SupportType.PIN
- SupportType.ROLLER_X
- SupportType.ROLLER_Y
- SupportType.ROLLER_Z (only in 3d truss)
Truss.AddExternalForce(jointID, vector) -> None
jointID
: ID number of the joint.vector
: Force vector of each joints in the truss.
Truss.AddNewMember(jointID0, jointID1, memberType=MemberType()) -> None
jointID0
: ID number of the first joint of this member.jointID1
: ID number of the second joint of this member.memberType
: Member type which contain the information aboutcross-sectional area
,Young's modulus
,density
of this member.
Here is the detail of class MemberType
:
class MemberType:
def __init__(self, a=1., e=1., density=1.):
self.a = float(a)
self.e = float(e)
self.density = float(density)
def __repr__(self):
return f"MemberType(a={self.a}, e={self.e}, density={self.density})"
def __eq__(self, other):
return IsZero(self.a - other.a) and IsZero(self.e - other.e) and IsZero(self.density - other.density)
def __hash__(self):
return (self.a, self.e, self.density).__hash__()
def Set(self, other):
self.a, self.e, self.density = other.a, other.e, other.density
def Serialize(self):
return [self.a, self.e, self.density]
def Copy(self):
return MemberType(self.a, self.e, self.density)
Truss.GetKMatrix() -> numpy.array
- It will return a numpy array which is the assembled K matrix.
Truss.Solve() -> None
-
Do the structral analysis of your truss by
direct stiffness method
. After that, all theinternal force (not stress!)
of each member,displacement
andtotal force
at each joint will solved and stored in the Truss object. You could get them with some getters defined in Truss.As said in Description, slientruss3d is made for
stable
truss analysis. So once you call the methodTruss.Solve()
, it will check whether your truss is stable or not with the propertyTruss.isStable
. If your truss is not stable, an exceptionTrussNotStableError
will be raised.
Truss.GetInternalStresses() -> dict[int, float]
-
Get inetrnal stress at each member. It returns a dictionary whose key is
member ID
and value isstress
.Note that if you haven't done structural analysis yet, this method will return
None
.
Truss.GetInternalForces(isProtect=True) -> dict[int, float]
-
Get inetrnal force at each member. It returns a dictionary whose key is
member ID
and value isforce magnitude
.Note that if you haven't done structural analysis yet, this method will return
None
. -
isProtect
: If it'sTrue
, then return a deep-copy of the result of internal forces stored in Truss object. Otherwise, return its reference directly, but remember not to change any value in this reference.
Truss.GetDisplacements(isProtect=True) -> dict[int, numpy.array]
-
Get displacement at each joint. It returns a dictionary whose key is
joint ID
and value isdisplacement vector
.Note that if you haven't done structural analysis yet, this method will return
None
. -
isProtect
: If it'sTrue
, then return a deep-copy of the result of joint displacements stored in Truss object. Otherwise, return its reference directly, but remember not to change any value in this reference.
Truss.GetResistances() -> dict[int, numpy.array]
-
Get resistance at each support. It returns a dictionary whose key is
joint ID
and value isresistance vector
.Note that if you haven't done structural analysis yet, this method will return
None
.
Truss.LoadFromJSON(path=None, isOutputFile=False, data=None) -> Truss
path
: Filename of the JSON file.isOutputFile
: Whether the JSON file or dictionary data contains the result of structural analysis.data
: Directly assign adictionary
whose format is the same as Format of JSON. If it's not none, do not assgin any value to the argumentpath
.
Truss.DumpIntoJSON(path) -> None
-
path
: Filename of the JSON in which you want to store the result of structural analysis.More about the utility of JSON will be introduced in Combine with JSON !
Truss.Serialize() -> dict
-
Return a dictionary which contains all the information about the truss.
The format is the same as Format of JSON.
Truss.IsInternalStressAllowed(limit, isGetSumViolation=False, isGetSumNonViolation=False) -> tuple[bool, dict | float] | tuple[bool, dict | float, float]
limit
: Allowable stress.isGetSumViolation
: Sum of the exceeding quantities of members that violate allowable stress.isGetSumNonViolation
: Sum of differences between allowable stress and the stresses of members that don't violate allowable stress. If it's True, then the length of return is 3, otherwise 2.
If the parameter isGetSumViolation
is True, then the method returns
- boolean : indicates whether the truss violates the allowable limit or not.
- float : sum of absolute values of exceeding stresses or displacements.
Truss.IsDisplacementAllowed(limit, isGetSumViolation=False, isGetSumNonViolation=False) -> tuple[bool, dict | float] | tuple[bool, dict | float, float]
limit
: Allowable displacement.isGetSumViolation
: Sum of the exceeding quantities of joints that violate allowable displacement.isGetSumNonViolation
: Sum of differences between allowable displacement and the displacements of joints that don't violate allowable displacement. If it's True, then the length of return is 3, otherwise 2.
If the parameter isGetSumViolation
is True, then the method returns
- boolean : indicates whether the truss violates the allowable limit or not.
- float : sum of absolute values of exceeding stresses or displacements.
Truss.Copy() -> Truss
- Weight of the truss.
Truss.weight : float
- Whether the truss is stable or not ?
Truss.isStable : bool
- Whether the truss has been done structral analysis or not ?
Truss.isSolved : bool
- Number of joints.
Truss.nJoint : int
- Number of members.
Truss.nMember : int
- Number of loads.
Truss.nForce : int
- Number of supports (support type is not
SupportType.NO
).
Truss.nSupport : int
- Number of resistances.
Truss.nResistance : int
- Dimension of the truss
Truss.dim : int
Member(joint0, joint1, dim=3, memberType=MemberType()) -> None
Member.IsTension(forceVec) -> bool
forceVec
: The internal force vector onjoint1
.
Member.Serialize() -> dict
- Return a dictionary which contains all the information about the member. The following is its format:
{
"joint0" : list[float], # Position of joint0
"joint1" : list[float], # Position of joint1
"memberType": list[float] # Cross-sectional area, Young's modulus, density
}
Member.Copy() -> Member
- Weight of the member.
Member.weight : float
- Length of the member.
Member.length : float
- Cross-sectional area.
Member.a : float
- Young's modulus.
Member.e : float
- Density.
Member.density : float
- Member type. (Has both getter and setter)
Member.memberType : MemberType
- Cosine values of every axis.
Member.cosines : list[float]
- E * A / L.
Member.k : float
- Stiffness matrix (K).
Member.matK : numpy.array
- Dimension of the member.
Member.dim : int