-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.py
executable file
·618 lines (413 loc) · 20.1 KB
/
main.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
import os
import sys
import quadrature
import quadrilaterals
import analysis
import model
import material
import front2back
import time as tm
import numpy as np
import itertools as it
import matplotlib.pyplot as plt
def submit(job, pipe=sys.stdout.write):
"""
Submit job.
Parameters
----------
job: front2back.BackendJob
The job to be submitted.
pipe: function
The function to pipe progress messages. When called by the user
interface, messages are by default piped to the message window.
"""
pipe(' \n\n')
pipe(' Job {}\n'.format(job.getName()))
pipe(' -------------------------------\n')
pipe(' Submitted \n')
pipe(' {}\n'.format(tm.ctime()))
# Read job definition
jobName = job.getName()
jobModel = job.getModel()
jobThickness = job.getThickness()
jobDamage = job.getDamage()
jobMaterial = job.getMaterial()
jobBoundary1, jobBoundary2, jobBoundary3 = job.getBoundaries()
jobWastage = job.getCorrosion()
jobTemperature = job.getTemperature()
jobAnalysis = job.getAnalysis()
if jobAnalysis == 'Modal':
modes, normalization = job.getModalSettings().values()
else:
alpha, beta, period, increment, lcase = job.getTimeHistorySettings().values()
# Define element labels of damaged areas
if jobModel == 0: # 'Healthy state'
damagedElements = []
elif jobModel == 1: # 'Damaged state 1'
damagedElements = [49*6]
elif jobModel == 2: # 'Damaged state 2'
damagedElements = [49*6, 49*6+1]
elif jobModel == 3: # 'Damaged state 3'
damagedElements = [49*6, 49*6+1, 49*6+2]
elif jobModel == 4: # 'Damaged state 4'
damagedElements = [100*6+5]
elif jobModel == 5: # 'Damaged state 5'
damagedElements = [100*6+5, 100*6+4]
elif jobModel == 6: # 'Damaged state 6'
damagedElements = [100*6+5, 100*6+4, 100*6+3]
# Define Geometry
L1 = 12.5 # Length of the left-hand span
L2 = 12.5 # Length of the right-hand span
length = L1+L2 # Dimension in x-axis
density = 2000 # Material density
height_start = 0.60 # Dimension in y-axis
height_end = height_start
width_start = 0.1 # Dimension in z-axis
width_end = width_start
nel_x = 200 # Number of elements in x-axis
nel_y = 6 # Number of elements in y-axis
el_size_x = length/nel_x # Element size in x-direction
el_size_y = height_start/nel_y # Element size in y-direction
points_x = np.arange(0, length*(1+1/nel_x)-1e-10, length/nel_x)
counter = it.count(0)
points_y = []
nodes = []
indices = []
# Define model nodes
for i, x in enumerate(points_x):
h = height_start-x/length*(height_start-height_end)
points_y.append(np.arange(-h/2, h*(1/2+1/nel_y)-1e-10, h/nel_y))
for y in points_y[i]:
nodes.append(model.Node([x, y, 0]))
nodes[-1].SetValue('adof', ['x', 'y'])
label = next(counter)
if x < length-1e-10 and y < h/2-1e-10:
indices.append(label)
# Define model elements
elements = []
etype = quadrilaterals.Quad4()
irule = quadrature.Gauss.inQuadrilateral(rule=2).info
for i, j in enumerate(indices):
# Define element nodes
enodes = [nodes[j], nodes[j+nel_y+1], nodes[j+nel_y+2], nodes[j+1]]
# Coordinates of element center-point
xc = np.sum([node.coords[0] for node in enodes])/len(enodes)
yc = np.sum([node.coords[1] for node in enodes])/len(enodes)
# Coordinates of element integration points
xi = xc+irule[:, 0]*el_size_x
yi = yc+irule[:, 1]*el_size_y
# Interpolate temperature at gauss points
temperature = np.interp(xi, jobTemperature[:, 1]*length, jobTemperature[:, 0])
# Calculate stiffnes reduction
reduction = jobDamage/100 if i in damagedElements else 0
# Define material properties for each integration point
E = np.interp(temperature, jobMaterial[:, 2], jobMaterial[:, 0])*(1-reduction)
n = np.interp(temperature, jobMaterial[:, 2], jobMaterial[:, 1])
materials = []
for k in range(len(xi)):
materials.append(material.LinearElastic(E[k], n[k], density))
# Interpolate thickness at gauss points
wastage = np.interp(xi, jobWastage[:, 1]*length, jobWastage[:, 0])
thickness = jobThickness*(np.ones(len(xi))-wastage)
# Define elements and modify damaged ones
elements.append(model.Element(enodes, etype, materials, thickness, irule))
# Initialize model
model1 = model.Model(nodes, elements)
# Interpolate temperature at boundary locations
temp1 = np.interp(0, jobTemperature[:, 1]*length, jobTemperature[:, 0])
temp2 = np.interp(length/2, jobTemperature[:, 1]*length, jobTemperature[:, 0])
temp3 = np.interp(length, jobTemperature[:, 1]*length, jobTemperature[:, 0])
# Interpolate boundary values at temperature value
kx1 = np.interp(temp1, jobBoundary1[:, 2], jobBoundary1[:, 0])
ky1 = np.interp(temp1, jobBoundary1[:, 2], jobBoundary1[:, 1])
kx2 = np.interp(temp2, jobBoundary2[:, 2], jobBoundary2[:, 0])
ky2 = np.interp(temp2, jobBoundary2[:, 2], jobBoundary2[:, 1])
kx3 = np.interp(temp3, jobBoundary3[:, 2], jobBoundary3[:, 0])
ky3 = np.interp(temp3, jobBoundary3[:, 2], jobBoundary3[:, 1])
# Apply boundary conditions
dtol = 1e-5+el_size_x/2 # Tolerance for node searching
blabels = [] # Labels of boundary nodes
lposition = 0 # Left-hand support
mposition = L1 # Intermediate support
rposition = length # Right-hand support
for node in nodes:
x, y = node.coords[0], node.coords[1]
# Left-hand side constraints
if np.abs(x-lposition) < dtol and np.abs(y + height_start/2) < dtol:
blabels.append(node.label)
model1.constraints.addSpring(node.label, ['x', 'y'], [kx1, ky1])
elif np.abs(x-(lposition+el_size_x)) < dtol and np.abs(y+height_start/2) < dtol:
blabels.append(node.label)
model1.constraints.addSpring(node.label, ['x', 'y'], [kx1, ky1])
elif np.abs(x-(lposition+el_size_x*2)) < dtol and np.abs(y+height_start/2) < dtol:
blabels.append(node.label)
model1.constraints.addSpring(node.label, ['x', 'y'], [kx1, ky1])
elif np.abs(x-(lposition+el_size_x*3)) < dtol and np.abs(y+height_start/2) < dtol:
blabels.append(node.label)
model1.constraints.addSpring(node.label, ['x', 'y'], [kx1, ky1])
# Mid-point constraints
elif np.abs(x-(mposition-el_size_x*2)) < dtol and np.abs(y+height_start/2) < dtol:
blabels.append(node.label)
model1.constraints.addSpring(node.label, ['x', 'y'], [kx2, ky2])
elif np.abs(x-(mposition-el_size_x)) < dtol and np.abs(y+height_start/2) < dtol:
blabels.append(node.label)
model1.constraints.addSpring(node.label, ['x', 'y'], [kx2, ky2])
elif np.abs(x-mposition) < dtol and np.abs(y+height_start/2) < dtol:
blabels.append(node.label)
model1.constraints.addSpring(node.label, ['x', 'y'], [kx2, ky2])
elif np.abs(x-(mposition+el_size_x)) < dtol and np.abs(y+height_start/2) < dtol:
blabels.append(node.label)
model1.constraints.addSpring(node.label, ['x', 'y'], [kx2, ky2])
elif np.abs(x-(mposition+el_size_x*2)) < dtol and np.abs(y+height_start/2) < dtol:
blabels.append(node.label)
model1.constraints.addSpring(node.label, ['x', 'y'], [kx2, ky2])
# Right-hand side constraints
elif np.abs(x-(rposition-el_size_x*3)) < dtol and np.abs(y+height_start/2) < dtol:
blabels.append(node.label)
model1.constraints.addSpring(node.label, ['x', 'y'], [kx3, ky3])
elif np.abs(x-(rposition-el_size_x*2)) < dtol and np.abs(y+height_start/2) < dtol:
blabels.append(node.label)
model1.constraints.addSpring(node.label, ['x', 'y'], [kx3, ky3])
elif np.abs(x-(rposition-el_size_x)) < dtol and np.abs(y+height_start/2) < dtol:
blabels.append(node.label)
model1.constraints.addSpring(node.label, ['x', 'y'], [kx3, ky3])
elif np.abs(x-rposition) < dtol and np.abs(y+height_start/2) < dtol:
blabels.append(node.label)
model1.constraints.addSpring(node.label, ['x', 'y'], [kx3, ky3])
# Extract degrees of freedom for output locations.
columns = np.arange(5, nel_x+5, 10)[np.newaxis].T*(nel_y+1)
olabels = np.tile(np.array([1, 3, 5]), len(columns)).reshape((len(columns), 3))
olabels = (columns+olabels).reshape((olabels.size,))
odofs = np.sort(np.hstack((olabels*2, olabels*2+1)))
ocoords = np.array([(nodes[j].coords[0], nodes[j].coords[1]) for j in olabels])
# Save labels and coordinates of nodes where response quantities are
# extracted.
output = np.vstack((olabels, ocoords.T)).T
labels, frmt = 'label x y', ['%d', '%10.5f', '%10.5f']
np.savetxt('Output_nodes.dat', output, fmt=frmt, header=labels)
# Save labels of measurement degrees of freedom
xlabels = np.array([str(item)+'x' for item in olabels])
ylabels = np.array([str(item)+'y' for item in olabels])
labels = np.vstack((xlabels, ylabels)).T.flatten()
labels = ' '.join(label for label in labels)
# Run analysis
if jobAnalysis == 'Modal':
# Submit modal analysis
pipe(' \n')
pipe(' Started: analysis \n')
modal = analysis.Modal(model1)
modal.setNumberOfEigenvalues(modes)
modal.setNormalizationMethod(normalization)
modal.submit()
pipe(' Completed: analysis \n\n')
# Extract mode shapes at output locations
frequencies = modal.frequencies
modes = modal.modes[odofs, :]
# Save results
pipe(' Started: writting output \n')
np.savetxt(jobName+'_frequencies.dat', frequencies)
np.savetxt(jobName+'_modes.dat', modes, header=labels)
pipe(' Completed: writting output \n\n')
# pipe(' Saved output files \n')
elif jobAnalysis == 'Time history':
model1.setDampingCoefficients(alpha, beta)
# Select load case
if lcase == 0:
nlabels = np.arange(nel_y+1, (nel_x+1)*(nel_y+1) ,nel_y+1)
lcase = np.loadtxt('Load_case_1.dat', skiprows=1)
velocity, load = lcase[0], lcase[1]
for j, nlabel in enumerate(nlabels):
t1 = nodes[nlabels[j-1]].coords[0]/velocity
t2 = nodes[nlabels[j]].coords[0]/velocity
if j == 0:
t1 = t2-1e-5
if j == nlabels.shape[0]-1:
t3 = t2+1e-5
else:
t3 = nodes[nlabels[j+1]].coords[0]/velocity
time = np.array([t1, t2, t3])
force = np.array([0, 1e3*load, 0])
amplitude = [np.array([time, force])]
model.Load(model1).addForce(nodes[nlabel].label, 'y', amplitude)
elif lcase == 1:
nlabel = 63*(nel_y+1)-1
lcase = np.loadtxt('Load_case_2.dat', skiprows=1)
time, force = lcase[:, 0], lcase[:, 1]
amplitude = [np.array([time, force])]
model.Load(model1).addForce(nodes[nlabel].label, 'y', amplitude)
elif lcase == 2:
nlabel = 139*(nel_y+1)-1
lcase = np.loadtxt('Load_case_3.dat', skiprows=1)
time, force = lcase[:, 0], lcase[:, 1]
amplitude = [np.array([time, force])]
model.Load(model1).addForce(nodes[nlabel].label, 'y', amplitude)
elif lcase == 3:
lcase = np.loadtxt('Load_case_4.dat', skiprows=1)
time, forces = lcase[:, 0], lcase[:, 1:]
nlabels = np.arange(nel_y+1, (nel_x+1)*(nel_y+1), nel_y+1)
for j, nlabel in enumerate(nlabels):
amplitude = [np.array([time, forces[:, j]])]
model.Load(model1).addForce(nodes[nlabel].label, 'y', amplitude)
# Define dynamic analysis
pipe(' \n')
pipe(' Started: analysis \n')
dynamics = analysis.Dynamics(model1)
dynamics.setTimePeriod(period)
dynamics.setIncrementSize(increment)
dynamics.submit()
pipe(' Completed: analysis \n\n')
time = np.arange(0, period+increment, increment)
nmodes = dynamics.displacement.shape[0]
displacement = np.zeros((nmodes, time.size))
acceleration = np.zeros((nmodes, time.size))
for m in range(nmodes):
displacement[m, :] = np.interp(time, dynamics.time, dynamics.displacement[m, :])
acceleration[m, :] = np.interp(time, dynamics.time, dynamics.acceleration[m, :])
# Extract displacements and accelerations at output degrees of freedom
displacements = dynamics.modes[odofs, :].dot(displacement).T
accelerations = dynamics.modes[odofs, :].dot(acceleration).T
# Extract strains at output degrees of freedom
strains = np.zeros((time.size, len(olabels), 3)) # define time_steps
rcoords = [[1, 1], [1, -1], [-1, -1], [-1, 1]]
strain_history = np.zeros((time.size, 3))
for k, olabel in enumerate(olabels):
elabels = np.sort(nodes[olabel].links)
for elabel, (r1, r2) in zip(elabels, rcoords):
ncoords = elements[elabel].getNodeCoordinates()
ipoints = elements[elabel].getIntegrationPoints()
edofs = elements[elabel].getNodeDegreesOfFreedom()
disp = dynamics.modes[edofs, :].dot(displacement)# .T
element = elements[elabel].getType()
# 1. rows of disp contain element displacements
# 2. columns of disp should contain time steps
strain = element.getStrain(ncoords, disp, ipoints, r1, r2).T
# 1. columns of strain contain components Exx, Eyy, Exy
# 2. rows of strain contain time steps
strain_history += strain
strains[:, k, :] = strain_history/len(nodes[olabel].links)
strain_history[:] = 0
strains = strains.reshape((time.size, len(olabels)*3))
# Save results (displacements, accelerations and strains)
pipe(' Started: writting output \n')
labels = ''.join([
'Node-{}-Ux'.format(label).ljust(24, ' ')+
'Node-{}-Uy'.format(label).ljust(24, ' ') for label in olabels])
fname = jobName+'_displacements.dat'
np.savetxt(fname, displacements, fmt='% .16e', header=labels)
labels = ''.join([
'Node-{}-Ax'.format(label).ljust(24, ' ')+
'Node-{}-Ay'.format(label).ljust(24, ' ') for label in olabels])
fname = jobName+'_accelerations.dat'
np.savetxt(fname, accelerations, fmt='% .16e', header=labels)
labels = ''.join([
'Node-{}-Exx'.format(label).ljust(24, ' ')+
'Node-{}-Eyy'.format(label).ljust(24, ' ')+
'Node-{}-Exy'.format(label).ljust(24, ' ') for label in olabels])
fname = jobName+'_strains.dat'
np.savetxt(fname, strains, fmt='% .16e', header=labels)
pipe(' Completed: writting output \n\n')
elif jobAnalysis == 'Static':
nlabel = 63*(nel_y+1)-1
# lcase = np.loadtxt('Load_case_2.dat', skiprows=1)
# time, force = lcase[0, 0], lcase[1, 1]
time = 30 # np.linspace(0, 30, 10000)
force = 1e3
amplitude = [np.array([time, force])]
model.Load(model1).addForce(nodes[nlabel].label, 'y', amplitude)
# Define static analysis
pipe(' \n')
pipe(' Started: analysis \n')
static = analysis.Static(model1)
static.submit()
pipe(' Completed: analysis \n\n')
# Extract displacements at output degrees of freedom
displacements = static.displacement[odofs]# [np.newaxis]
# Extract strains at output nodes
strains = np.zeros((len(olabels), 3))
rcoords = [[1, 1], [1, -1], [-1, -1], [-1, 1]]
for k, olabel in enumerate(olabels):
elabels = np.sort(nodes[olabel].links)
for elabel, (r1, r2) in zip(elabels, rcoords):
ncoords = elements[elabel].getNodeCoordinates()
ipoints = elements[elabel].getIntegrationPoints()
edofs = elements[elabel].getNodeDegreesOfFreedom()
disp = static.displacement[edofs]
element = elements[elabel].getType()
strain = element.getStrain(ncoords, disp, ipoints, r1, r2)
nodes[olabel].strain += strain
strains[k, :] = nodes[olabel].strain
strains = strains.reshape((1, strains.size))
# Save results
labels = ''.join([
'Node-{}-Exx'.format(label).ljust(24, ' ')+
'Node-{}-Eyy'.format(label).ljust(24, ' ')+
'Node-{}-Exy'.format(label).ljust(24, ' ') for label in olabels])
pipe(' Started: writting output\n')
np.savetxt(jobName+'_displacements.dat', displacements, header=labels)
np.savetxt(jobName+'_strains.dat', strains, fmt='% .16e', header=labels)
pipe(' Completed: writting output\n')
pipe(' Completed \n')
pipe(' {}\n'.format(tm.ctime()))
# Plot mode shapes
# for mode_no in range(5):
# for item, mode in zip(list(model1.ndof.keys()), modal.modes[:, mode_no]):
# nodes[item[0]].dsp[item[1]] = mode
# # for item, mode in zip(list(model1.ndof.keys()), static.displacement):
# # nodes[item[0]].dsp[item[1]] = mode
# plt.figure()
# n = str(mode_no+1)
# # plt.subplot('41'+n)
# # plt.title('Mode '+n+' - '+str(modal.frequencies[mode_no])[:5]+' Hz', fontsize=11)
# plt.title('Mode '+n+' - '+'{:.3f} Hz'.format(frequencies[mode_no]), fontsize=11)
# plt.axis('equal')
# plt.xlim(0, length)
# plt.xticks([], [])
# plt.yticks([], [])
# for i in range(len(elements)):
# if elements[i].label in damagedElements:
# clr = 'k'
# width = 1
# else:
# clr = 'r'
# width = 0.5
# elements[i].deformed(scale=1e2, color=clr, lnwidth=width)
# # elements[i].plotLabel()
# # for lab in olabels:
# # plt.plot(nodes[lab].coords[0], nodes[lab].coords[1], 'o')
# # for lab in [63*7-1, 139*7-1]:
# # plt.plot(nodes[lab].coords[0], nodes[lab].coords[1], 'o')
# # for lab in nlabel:
# # plt.plot(nodes[lab].coords[0], nodes[lab].coords[1], 'o')
# plt.tight_layout()
# plt.show()
# # Plot modal response
# plt.figure()
# plt.plot(dynamics.displacement[0, :])
# plt.tight_layout()
# plt.show()
if __name__ == '__main__':
job = front2back.BackendJob('Job-1')
job.setModel(0)
job.setThickness(0.1)
job.setDamage(0.1)
# Set default values for material properties (E, n, T)
job.setMaterial(np.array([[3e10, 0.3, 10]]))
# Set default values for boundary conditions (Kx, Ky, T)
job.setBoundaries(
np.array([[1e15, 1e10, 20]]), # Left-hand support
np.array([[1e15, 1e10, 20]]), # Intermediate support
np.array([[1e15, 1e10, 20]]) # Right-had support
)
# Set default values for corrosion wastage (W, x/L)
job.setCorrosion(np.array([[0.0, 0.5]]))
# Set default values for environmental temperature (T, x/L)
job.setTemperature(np.array([[10, 0.5]]))
# # Modal analysis settings
# job.setAnalysis('Modal')
# # Set default values for modal analysis (Modes, normalization)
# job.setModalSettings(10, 'Mass')
# Time history settings
job.setAnalysis('Time history')
# Set default values for time history analysis (a, b, period, step, load)
job.setTimeHistorySettings(0.002, 0.0001, 200, 0.005, 3)
submit(job)