-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathexample.py
304 lines (243 loc) · 11.7 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
def TestTimeConsuming():
from slientruss3d.truss import Truss
# Global variables
TEST_FILE_NUMBER = 47
TEST_LOAD_CASE = 0
TEST_INPUT_FILE = f"./data/bar-{TEST_FILE_NUMBER}_input_{TEST_LOAD_CASE}.json"
TRUSS_DIMENSION = 2
TEST_TIME_NUMBER = 30
# Truss object and read .json:
truss = Truss(dim=TRUSS_DIMENSION)
truss.LoadFromJSON(TEST_INPUT_FILE)
# Do direct stiffness method:
import time
ts = []
for _ in range(TEST_TIME_NUMBER):
t0 = time.time()
truss.Solve()
ts.append(time.time() - t0)
mean = sum(ts) / len(ts)
print(f"Time for structural analysis = {mean}(s)")
return mean
def TestPlot():
from slientruss3d.truss import Truss
from slientruss3d.plot import TrussPlotter
# Global variables
TEST_FILE_NUMBER = 10
TEST_LOAD_CASE = 0
TEST_INPUT_FILE = f"./data/bar-{TEST_FILE_NUMBER}_output_{TEST_LOAD_CASE}.json"
TEST_PLOT_SAVE_PATH = f"./plot/bar-{TEST_FILE_NUMBER}_plot_{TEST_LOAD_CASE}.png"
TRUSS_DIMENSION = 2
IS_EQUAL_AXIS = True
IS_PLOT_STRESS = True
IS_SAVE_PLOT = False
MAX_SCALED_DISPLACEMENT = 2
MAX_SCALED_FORCE = 100
POINT_SIZE_SCALE_FACTOR = 1
ARROW_SIZE_SCALE_FACTOR = 1
# Truss object:
truss = Truss(dim=TRUSS_DIMENSION)
# You could directly read the output .json file.
truss.LoadFromJSON(TEST_INPUT_FILE, isOutputFile=True)
# Show or save the structural analysis result figure:
TrussPlotter(truss,
isEqualAxis=IS_EQUAL_AXIS,
isPlotStress=IS_PLOT_STRESS,
maxScaledDisplace=MAX_SCALED_DISPLACEMENT,
maxScaledForce=MAX_SCALED_FORCE,
pointScale=POINT_SIZE_SCALE_FACTOR,
arrowScale=ARROW_SIZE_SCALE_FACTOR).Plot(IS_SAVE_PLOT, TEST_PLOT_SAVE_PATH)
def TestExample():
from slientruss3d.truss import Truss
from slientruss3d.type import SupportType, MemberType
from slientruss3d.plot import TrussPlotter
# -------------------- Global variables --------------------
# Files settings:
TEST_OUTPUT_FILE = f"./test_output.json"
TEST_PLOT_SAVE_PATH = f"./test_plot.png"
# Some settings:
TRUSS_DIMENSION = 3
IS_PLOT_TRUSS = True
IS_SAVE_PLOT = False
# Plot layout settings:
IS_EQUAL_AXIS = True # Whether to use actual aspect ratio in the truss figure or not.
MAX_SCALED_DISPLACEMENT = 30 # Scale the max value of all dimensions of displacements.
MAX_SCALED_FORCE = 100 # Scale the max value of all dimensions of force arrows.
POINT_SIZE_SCALE_FACTOR = 1 # Scale the default size of joint point in the truss figure.
ARROW_SIZE_SCALE_FACTOR = 1 # Scale the default size of force arrow in the truss figure.
# ----------------------------------------------------------
# Truss object:
truss = Truss(dim=TRUSS_DIMENSION)
# Read data in this [.py]:
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)
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)
# Show or save the structural analysis result figure:
if IS_PLOT_TRUSS:
TrussPlotter(truss,
isEqualAxis=IS_EQUAL_AXIS,
maxScaledDisplace=MAX_SCALED_DISPLACEMENT,
maxScaledForce=MAX_SCALED_FORCE,
pointScale=POINT_SIZE_SCALE_FACTOR,
arrowScale=ARROW_SIZE_SCALE_FACTOR).Plot(IS_SAVE_PLOT, TEST_PLOT_SAVE_PATH)
# Get result of structural analysis:
displace, stress, resistance = truss.GetDisplacements(), truss.GetInternalStresses(), truss.GetResistances()
return displace, stress, resistance
def TestLoadFromJSON():
from slientruss3d.truss import Truss
from slientruss3d.plot import TrussPlotter
# -------------------- Global variables --------------------
# Files settings:
TEST_FILE_NUMBER = 10
TEST_LOAD_CASE = 0
TEST_INPUT_FILE = f"./data/bar-{TEST_FILE_NUMBER}_input_{TEST_LOAD_CASE}.json"
TEST_OUTPUT_FILE = f"./data/bar-{TEST_FILE_NUMBER}_output_{TEST_LOAD_CASE}.json"
TEST_PLOT_SAVE_PATH = f"./plot/bar-{TEST_FILE_NUMBER}_plot_{TEST_LOAD_CASE}.png"
# Some settings:
TRUSS_DIMENSION = 2
IS_PLOT_TRUSS = True
IS_SAVE_PLOT = False
# Plot layout settings:
IS_EQUAL_AXIS = True # Whether to use actual aspect ratio in the truss figure or not.
MAX_SCALED_DISPLACEMENT = 10 # Scale the max value of all dimensions of displacements.
MAX_SCALED_FORCE = 50 # Scale the max value of all dimensions of force arrows.
POINT_SIZE_SCALE_FACTOR = 1 # Scale the default size of joint point in the truss figure.
ARROW_SIZE_SCALE_FACTOR = 1 # Scale the default size of force arrow in the truss figure.
# ----------------------------------------------------------
# Truss object:
truss = Truss(dim=TRUSS_DIMENSION)
# Read data in [.json]:
truss.LoadFromJSON(TEST_INPUT_FILE)
# Do direct stiffness method:
truss.Solve()
# Dump all the structural analysis results into a .json file:
truss.DumpIntoJSON(TEST_OUTPUT_FILE)
# Show or save the structural analysis result figure:
if IS_PLOT_TRUSS:
TrussPlotter(truss,
isEqualAxis=IS_EQUAL_AXIS,
maxScaledDisplace=MAX_SCALED_DISPLACEMENT,
maxScaledForce=MAX_SCALED_FORCE,
pointScale=POINT_SIZE_SCALE_FACTOR,
arrowScale=ARROW_SIZE_SCALE_FACTOR).Plot(IS_SAVE_PLOT, TEST_PLOT_SAVE_PATH)
# Get result of structural analysis:
displace, stress, resistance = truss.GetDisplacements(), truss.GetInternalStresses(), truss.GetResistances()
return displace, stress, resistance
def TestGA():
from slientruss3d.truss import Truss
from slientruss3d.type import MemberType
from slientruss3d.ga import GA
import random
# Allowable stress and displacement:
ALLOWABLE_STRESS = 30000.
ALLOWABLE_DISPLACEMENT = 10.
# Type the member types you want to use here:
MEMBER_TYPE_LIST = [MemberType(inch, random.uniform(1e7, 3e7), random.uniform(0.1, 1.0)) for inch in range(1, 21)]
# GA settings:
MAX_ITERATION = None
PATIENCE_ITERATION = 50
# Truss object:
truss = Truss(3)
truss.LoadFromJSON('./data/bar-120_input_0.json')
# Do GA:
ga = GA(truss, MEMBER_TYPE_LIST, ALLOWABLE_STRESS, ALLOWABLE_DISPLACEMENT, nIteration=MAX_ITERATION, nPatience=PATIENCE_ITERATION)
minGene, (fitness, isInternalAllowed, isDisplaceAllowed), finalPop, bestFitnessHistory = ga.Evolve()
# Translate optimal gene to member types:
truss.SetMemberTypes(ga.TranslateGene(minGene))
# Save result:
truss.Solve()
truss.DumpIntoJSON('bar-120_ga_0.json')
def TestGenerateCubeTruss():
from slientruss3d.generate import GenerateRandomCubeTrusses
# Some parameters for your generated cube truss:
GRID_RANGE = (5, 5, 5)
CUBE_NUMBER = 7
GENERATE_NUMBER = 10
EDGE_LENGTH_RANGE = (100, 200)
EXTERNAL_FORCE_RANGE = [(-1000, 1000), (-1000, 1000), (-1000, 1000)]
IS_DO_STRUCTURAL_ANALYSIS = True
IS_PLOT_GENERATED_TRUSS = True
SAVE_FOLDER = './generate'
# Generate cube-like trusses:
trussList = GenerateRandomCubeTrusses(gridRange=GRID_RANGE,
numCubeRange=(CUBE_NUMBER, CUBE_NUMBER),
numEachRange=(1, GENERATE_NUMBER),
lengthRange=EDGE_LENGTH_RANGE,
forceRange=EXTERNAL_FORCE_RANGE,
isDoStructuralAnalysis=IS_DO_STRUCTURAL_ANALYSIS,
isPlotTruss=IS_PLOT_GENERATED_TRUSS,
saveFolder=SAVE_FOLDER,
seed=42)
return trussList
def TestDataAugmentation():
from slientruss3d.generate import GenerateRandomCubeTrusses
from slientruss3d.generate import MoveToCentroid, RandomTranslation, AddJointNoise, RandomResetPin, NoChange, TrussDataAugmenterList
# Some parameters for your generated cube truss:
GRID_RANGE = (5, 5, 5)
CUBE_NUMBER = 4
EDGE_LENGTH_RANGE = (100, 200)
EXTERNAL_FORCE_RANGE = [(-1000, 1000), (-1000, 1000), (-1000, 1000)]
IS_DO_STRUCTURAL_ANALYSIS = True
IS_PLOT_GENERATED_TRUSS = True
SAVE_FOLDER = './generate/augmentations'
# Data augmentor:
transforms = TrussDataAugmenterList(
NoChange(), # Do nothing
MoveToCentroid(), # Move the centroid of the truss to [0., 0., 0.]
RandomTranslation(translateRange=[-30., 30.]), # Translate the whole truss randomly
AddJointNoise(noiseMeans=[0., 0., 0.], noiseStds=[10., 10., 10.]), # Add guassian noise to all positions of the joints
RandomResetPin(minNumPin=5, maxNumPinRatio=0.6) # Reset the positions and number of pin supports
)
# Generate cube-like truss:
truss = GenerateRandomCubeTrusses(gridRange=GRID_RANGE,
numCubeRange=(CUBE_NUMBER, CUBE_NUMBER),
numEachRange=(1, 1),
lengthRange=EDGE_LENGTH_RANGE,
forceRange=EXTERNAL_FORCE_RANGE,
isDoStructuralAnalysis=IS_DO_STRUCTURAL_ANALYSIS,
isPlotTruss=IS_PLOT_GENERATED_TRUSS,
saveFolder=SAVE_FOLDER,
seed=42,
augmenter=transforms # <- Do data augmentation !!!
)[0]
return truss
def TestTrussHeteroData():
from slientruss3d.data import TrussHeteroDataCreator
from slientruss3d.type import TaskType
from slientruss3d.truss import Truss
JSON_FILE = "./data/bar-25_input_0.json"
TRUSS_DIM = 3
IS_USE_TRUSS = False
# Creator to create PyG's HeteroData:
creator = TrussHeteroDataCreator(taskType=TaskType.OPTIMIZATION)
# Create PyG's HeteroData from JSON file or Truss object:
if IS_USE_TRUSS:
truss = Truss(TRUSS_DIM).LoadFromJSON(JSON_FILE)
graph = creator.FromTruss(truss, trussSrc=JSON_FILE)
else:
graph = creator.FromJSON(JSON_FILE, TRUSS_DIM)
# Print the structure of the HeteroData:
print("Hetero Graph Structure:\n" + "-" * 50)
print(graph)
if __name__ == '__main__':
pass
# TestTimeConsuming()
# TestExample()
# TestLoadFromJSON()
# TestPlot()
# TestGA()
# TestGenerateCubeTruss()
# TestDataAugmentation()
# TestTrussHeteroData()