import pyslammer as slam
Batch Simulations
This notebook shows an example use case of pyslammer for running batch simulations.
Setup
The next steps assume you’ve already installed pySLAMMER from PYPI. See the quickstart guide for installation instructions.
Import pyslammer using:
Additional Python libraries, such as matplotlib
may also be useful.
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.style.use(slam.psfigstyle)
= np.linspace(0.01,0.7,100)
kys = slam.sample_ground_motions()
histories = {} output
Batch Simulations
The code below evaluates all combinations of \(k_y\) contained in kys
and every motion in histories
. Some key features (motion, ky, max displacement, and displacement time histories) are stored in a dictionary, which is then converted to a pandas dataframe.
= 0
run for ky in kys:
for motion, hist in histories.items():
= slam.RigidAnalysis(ky,histories[motion].accel, histories[motion].dt,target_pga=0.3)
result = {"motion":motion, "ky":ky, "k_max":np.max(result.a_in), "d_max": result.max_sliding_disp, "dt":result.dt, "disp": result
output[run]
.sliding_disp}+= 1 run
# convert the output to a pandas dataframe
= pd.DataFrame.from_dict(output,orient='index')
df df
motion | ky | k_max | d_max | dt | disp | |
---|---|---|---|---|---|---|
0 | Morgan_Hill_1984_CYC-285 | 0.01 | 0.112099 | 1.539165e-01 | 0.005 | [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ... |
1 | Nisqually_2001_UNR-058 | 0.01 | 0.300000 | 2.076698e+00 | 0.010 | [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0842021724855... |
2 | Imperial_Valley_1979_BCR-230 | 0.01 | 0.300000 | 7.304110e-01 | 0.005 | [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ... |
3 | Northridge_1994_PAC-175 | 0.01 | 0.255128 | 2.405517e-01 | 0.020 | [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ... |
4 | Chi-Chi_1999_TCU068-090 | 0.01 | 0.300000 | 7.088829e+00 | 0.005 | [0.0, 0.0, 0.0, 0.0, 0.0, 1.6940658945086008e-... |
... | ... | ... | ... | ... | ... | ... |
1795 | Landers_1992_LCN-345 | 0.70 | 0.239638 | 2.967952e-16 | 0.005 | [0.0, 0.0, 0.0, 3.3881317890172015e-25, 1.3552... |
1796 | N_Palm_Springs_1986_WWT-180 | 0.70 | 0.300000 | 6.957730e-17 | 0.005 | [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ... |
1797 | Kobe_1995_TAK-090 | 0.70 | 0.300000 | 1.098963e-16 | 0.010 | [0.0, 0.0, 0.0, 0.0, 6.776263578034403e-25, 2.... |
1798 | Coyote_Lake_1979_G02-050 | 0.70 | 0.231868 | 7.516732e-17 | 0.005 | [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ... |
1799 | Northridge_1994_VSP-360 | 0.70 | 0.219967 | 2.099396e-16 | 0.005 | [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ... |
1800 rows × 6 columns
Comparing multiple motions
The results of the analyses can be plotted to show trends in total accumulated displacement with \(k_y\) for each ground motion in the sample ground motion suite.
'all')
plt.close(= plt.subplots()
fig, ax for key, grp in df.groupby(['motion']):
"ky"]/grp["k_max"], grp["d_max"], label=key[0], alpha=0.5)
ax.scatter(grp[='upper left', bbox_to_anchor=(1, 1))
ax.legend(loc0,1)
ax.set_xlim('log')
ax.set_yscale(1e-3,1e1)
ax.set_ylim('$k_y / k_{max}$')
ax.set_xlabel('Maximum Displacement (m)')
ax.set_ylabel(
plt.tight_layout() plt.show()

Variation in a single motion’s time history
Alternatively, for any given motion, the displacement time histories for different values of \(k_y\) could be of interest. In Figure 2, the results from all the simulations with the Imperial_Valley_1979_BCR-230
motion are shown. Although a different dimension of the data are being shown, the figure is simply pulling from the the results dataframe used in Figure 1.
Code
import matplotlib.cm as cm
from matplotlib.colors import LogNorm
= "Imperial_Valley_1979_BCR-230"
motion
'all')
plt.close(
# Create a figure and axes
= plt.subplots(figsize=(6, 4))
fig, ax
# Create a color map
= plt.colormaps['Spectral']#cm.get_cmap('viridis')
cmap = LogNorm(df['ky'].min(), df['ky'].max())
norm
= df[df["motion"]==motion].iloc[0]['dt']
dt = df[df["motion"]==motion].iloc[0]['disp'].shape[0]
npts = np.linspace(0, dt*npts, npts)
time
for index, row in df[df["motion"]==motion].iterrows():
= cmap(norm(row['ky']))
color 'disp'], color=color)
ax.plot(time, row[
# Add a color bar
= cm.ScalarMappable(cmap=cmap, norm=norm)
sm
sm.set_array([])= fig.colorbar(sm, ax=ax, label='ky')
cbar
# Set the colorbar ticks and labels
= [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7]
ticks
cbar.set_ticks(ticks)f'{tick:.2f}' for tick in ticks])
cbar.set_ticklabels([
# Set the x-axis and y-axis labels
'Time')
ax.set_xlabel('Displacement')
ax.set_ylabel(
ax.set_title(motion) plt.show()
