Skip to content

Instantly share code, notes, and snippets.

@terjehaukaas
Last active April 2, 2024 02:36
Show Gist options
  • Save terjehaukaas/e820aa3e18a0035371d0ca92b5746fd9 to your computer and use it in GitHub Desktop.
Save terjehaukaas/e820aa3e18a0035371d0ca92b5746fd9 to your computer and use it in GitHub Desktop.
G2 Example 10
from G2AnalysisSDOFNonlinearDynamic import *
from G2MaterialBilinear import *
import numpy as np
import matplotlib.pyplot as plt
# Target natural period of structure
structuralPeriod = 0.5
# Material parameters [N, m]
E = 1e4
alpha = 0.05
# Mass [kg]
M = (structuralPeriod/2/np.pi)**2 * E
# Damping ratio
dampingRatio = 0.05
# Calculate natural period of structure
naturalPeriod = 2*np.pi/np.sqrt(E/M)
# Print information about the structure
refg = 0.5
Fref = M*9.81*refg
print('\n'"K=%.0fkN/m, M=%.2fkg, Tn=%.3fsec, F(%.1fg)=%.2fkN, u(F(%.1fg))=%.3fm" % (E/1000, M, naturalPeriod, refg, Fref/1000, refg, Fref/E))
# Ground motion scaling
gmScaling = 1
# Delta-t
dt = 0.02
# Define DDM parameters
DDMparameters = [['Material', 'E'], ['Material', 'fy'], ['Material', 'alpha'], ['Mass'], ['Damping'], ['GroundMotion', 'Scaling']]
numBasicParameters = 6
parameterNames = ['Stiffness', 'Strength', 'Hardening', 'Mass', 'Damping', 'Scaling']
# Ground motion file
groundMotion = "GroundMotionElCentro.txt"
# Set the coefficient of variation
covSpec = 0.1
# Plotting colours
colours = ['blue', 'red', 'green', 'orange', 'magenta', 'gray', 'cyan', 'purple', 'pink']
# Yield displacement
uy = 0.03
fy = E * uy
parameters = [E, fy, alpha, M, dampingRatio, gmScaling]
# Load and plot ground motion
gm = []
f = open(groundMotion, "r")
lines = f.readlines()
for oneline in lines:
splitline = oneline.split()
for j in range(len(splitline)):
value = 9.81 * float(splitline[j]) * gmScaling
gm.append(value)
gm = np.array(gm)
duration = dt * (len(gm)-1)
plt.ion()
plt.figure()
plt.autoscale(True)
plt.grid(True)
plt.xlabel("Time [sec.]")
plt.ylabel("$\ddot{u}_g [m/s^2$]")
plt.title("Ground Motion ($\ddot{u}_{g,max}=%.2fg$)" % (np.max(np.abs(gm))/9.81))
plt.plot(np.linspace(0.0, duration, len(gm)), gm, 'k-', linewidth=1.0)
plt.savefig('Figure1.pdf', format='pdf')
print('\n'"Click somewhere in the plot to continue...")
plt.waitforbuttonpress()
# Run analysis
material = bilinearMaterial(['Bilinear', E, fy, alpha])
t, uTrack, vTrack, aTrack, dudx, dvdx, dadx, dnl = nonlinearDynamicSDOFAnalysis(duration, dt, material, M, dampingRatio, gm, gmScaling, DDMparameters)
# Plot displacement response
plt.ion()
plt.figure()
plt.autoscale(True)
plt.grid(True)
plt.xlabel("Time [sec.]")
plt.ylabel("u [m]")
plt.title("Displacement Response ($u_{max}=%.2fmm$)" % (np.max(np.abs(uTrack))*1000))
for i in range(1, len(uTrack)):
if dnl[i] < 1:
plt.plot([t[i-1], t[i]], [uTrack[i-1], uTrack[i]], 'r-', linewidth=1.0)
else:
plt.plot([t[i-1], t[i]], [uTrack[i-1], uTrack[i]], 'k-', linewidth=1.0)
plt.savefig('Figure2.pdf', format='pdf')
print('\n'"Click somewhere in the plot to continue...")
plt.waitforbuttonpress()
# Plot velocity response
plt.ion()
plt.figure()
plt.autoscale(True)
plt.grid(True)
plt.xlabel("Time [sec.]")
plt.ylabel("$\dot{u}$ [m/s]")
plt.title("Velocity Response ($\dot{u}_{max}=%.2fm/s$)" % (np.max(np.abs(vTrack))))
plt.plot(t, vTrack, 'k-', linewidth=1.0)
plt.savefig('Figure3.pdf', format='pdf')
print('\n'"Click somewhere in the plot to continue...")
plt.waitforbuttonpress()
# Plot total acceleration
plt.ion()
plt.figure()
plt.autoscale(True)
plt.grid(True)
plt.xlabel("Time [sec.]")
plt.ylabel("$\ddot{u}$ [m/s^2]")
plt.title("Total Acceleration ($\ddot{u}_{max}=%.2fg$)" % (np.max(np.abs(aTrack))/9.81))
plt.plot(t, aTrack, 'k-', linewidth=1.0)
plt.savefig('Figure4.pdf', format='pdf')
print('\n'"Click somewhere in the plot to continue...")
plt.waitforbuttonpress()
# Plot response sensitivities as (du/dx)*sigma
plt.ion()
plt.figure()
for j in range(numBasicParameters):
theLabel = parameterNames[j]
if np.max(np.abs(dudx[j,:])) != 0:
plt.plot(t, dudx[j, :]*covSpec*parameters[j], '-', linewidth=1.0, color=colours[j], label=theLabel)
plt.autoscale(True)
plt.grid(True)
plt.xlabel("Time [sec.]")
plt.ylabel("$(\partial u / \partial x_i) \cdot \sigma_i$ ")
plt.title("Response Sensitivities")
plt.legend(loc='lower left', prop={'size': 9})
print('\n'"Click somewhere in the plot to continue...")
plt.waitforbuttonpress()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment