Throughout the book we used sd.sdof_response, the pure-NumPy engine of
Chapter 3, for the forward solve, and we kept an
independent OpenSeesPy run nearby as a cross-check (Chapter 2,
Chapter 5, Chapter 6).
Chapters 7 and 8 build their own OpenSees models on the same pattern. That
OpenSees run lives in the tiny helper osdof.py, called as
from osdof import opensees_sdof. This appendix opens that helper up and walks
through it line by line, then collects the handful of variants you reach for most
often. It is a copy-paste reference, not a tutorial: every block below is
short, commented, and runs on its own.
OpenSees is a finite-element framework, so even a single mass on a spring is
assembled from the same primitives a full building uses: a model, nodes, a
material, an element, a pattern of loads, an integrator, and an
analysis that marches in time. Building the SDOF by hand is the cleanest way to
meet those primitives, because nothing is hidden. The sections below take them in
that order, one primitive at a time.
Everything in this appendix needs only NumPy and OpenSeesPy:
import numpy as np
import openseespy.opensees as ops11. The model: one fixed node, one mass node¶
Every analysis starts by wiping any previous state and declaring the model
domain. We work in one spatial dimension (-ndm 1) with one degree of
freedom per node (-ndf 1): the only motion is translation along the single
axis. Node 1 sits at the origin and is fully fixed (the ground); node 2 sits
at the same point and carries the mass m. They are coincident because a
zeroLength spring will connect them, and its deformation is the difference of
their displacements.
m = 2.0 # roof mass (t)
ops.wipe() # clear any previous model
ops.model('basic', '-ndm', 1, '-ndf', 1) # 1-D domain, 1 DOF/node
ops.node(1, 0.0) # node 1 at x = 0
ops.fix(1, 1) # ...fully fixed: this is the ground
ops.node(2, 0.0) # node 2 at x = 0 (coincident)
ops.mass(2, m) # ...carries the lumped mass m
print("mass on node 2 =", ops.nodeMass(2, 1), "t")mass on node 2 = 2.0 t
The lone DOF lives on node 2, and its displacement is the relative
displacement of the oscillator, because node 1 never moves. For base
excitation we keep node 1 fixed and inject the shaking as an effective force
(Section 4); the DOF then reads the response relative to the ground, exactly
the quantity that strains the columns.
22. Stiffness: an elastic material on a zeroLength element¶
The spring is two objects, a material and an element. The uniaxial material
defines a force-deformation law, here Elastic, so force is simply times
deformation. The zeroLength element then ties node 1 to node 2 and
assigns that material to act along direction 1 (-dir 1), the only direction
there is.
k = 50.0 # lateral stiffness (kN/m)
ops.uniaxialMaterial('Elastic', 1, k) # material 1: force = k * deformation
ops.element('zeroLength', 1, 1, 2, # element 1 connects nodes 1 -> 2
'-mat', 1, '-dir', 1) # material 1 acts along DOF direction 1That is the entire structure: a mass on a linear spring to the ground.
33. Damping: Rayleigh, tuned to hit c exactly¶
OpenSees forms a damping matrix as a Rayleigh combination . For a single DOF, and , so a purely mass-proportional choice gives . Setting therefore reproduces our target exactly, no approximation:
zeta = 0.05 # damping ratio (5%)
wn = np.sqrt(k / m) # natural frequency (rad/s)
c = 2.0 * zeta * m * wn # target damping coefficient (kN*s/m)
ops.rayleigh(c / m, 0.0, 0.0, 0.0) # alpha_M = c/m -> C = c ; beta_K = 0
print(f"c = {c:.3f} kN*s/m -> alpha_M = {c/m:.3f}")c = 1.000 kN*s/m -> alpha_M = 0.500
The four arguments of rayleigh are (mass-proportional),
(current-stiffness-proportional), and two more for initial- and
committed-stiffness terms, which we leave at zero.
44. Loading: a force history through a Path time series¶
A load in OpenSees is a pattern of nodal loads scaled in time by a
time series. For an arbitrary force history sampled at spacing dt,
the Path series carries the values and a Plain pattern applies them to the
DOF. The load(2, 1.0) says the spatial load on node 2 is 1.0, scaled by the
series, so the series values are the force in kN directly.
dt = 0.01
p = np.zeros(8) # (any force history; zeros = free vibration)
ops.timeSeries('Path', 1, '-dt', dt, # series 1: values at 0, dt, 2dt, ...
'-values', *map(float, p))
ops.pattern('Plain', 1, 1) # pattern 1 driven by time series 1
ops.load(2, 1.0) # unit reference load on node 2's DOFGround motion, the effective-force trick: an earthquake shakes the base, not the roof. The relative-displacement equation of motion (see Chapter 1) is
so a base acceleration record is identical to an effective force
applied to the fixed-base model. We never
move node 1; we just feed into the same Path series and the
DOF reports the relative response:
tg, ag = sd.demo_record(dt=0.02, duration=10.0) # a ground accel record (m/s^2)
p_eff = -m * ag # effective force (kN)
ops.timeSeries('Path', 2, '-dt', 0.02, '-values', *map(float, p_eff))
print(f"peak ground accel = {np.max(np.abs(ag)):.2f} m/s^2"
f" -> peak effective force = {np.max(np.abs(p_eff)):.2f} kN")peak ground accel = 2.94 m/s^2 -> peak effective force = 5.89 kN
55. The analysis: build the solver, then march in time¶
With the model loaded, we assemble the analysis from one object of each kind. Each line below picks how a piece of the solve is done; for a linear SDOF the choices are the standard, robust defaults:
ops.constraints('Plain') # how to impose the fixity at node 1
ops.numberer('Plain') # DOF numbering (trivial for 1 DOF)
ops.system('BandGeneral') # linear-system storage/solver
ops.test('NormDispIncr', 1e-10, 10) # convergence test: tol, max iters
ops.algorithm('Linear') # solution algorithm (no iteration needed)
ops.integrator('Newmark', 0.5, 0.25) # Newmark, gamma=1/2 beta=1/4 (avg accel)
ops.analysis('Transient') # time-history (transient) analysisThe heart is integrator('Newmark', 0.5, 0.25): Newmark’s
average-acceleration rule (, ), which is
unconditionally stable and second-order accurate (the workhorse of nonlinear
time-history analysis) and the same scheme sd.newmark implements in NumPy. The
algorithm('Linear') does one solve per step because the system is linear; the
nonlinear model of Chapter 7 swaps in algorithm('Newton') to iterate.
We then march: call analyze(1, dt) to advance one step of size dt, and
read the state off the DOF with nodeDisp / nodeVel / nodeAccel after each
step; analyze returns 0 on success.
# Read the initial state, then step the (currently zero-force) system once.
u0_read = ops.nodeDisp(2, 1)
flag = ops.analyze(1, dt) # 0 = converged
print("analyze flag:", flag, " u after one step:", ops.nodeDisp(2, 1))analyze flag: 0 u after one step: 0.0
66. Initial conditions: a nudge for free vibration¶
For free vibration we impose an initial displacement and/or velocity on the DOF
before the time march, with setNodeDisp / setNodeVel. The -commit flag
writes the value into the committed state so the integrator starts from it. We
also commit the dynamically consistent initial acceleration
: without it Newmark begins from and injects a
spurious first-step transient, an error that would otherwise swamp
the true (much smaller) period-elongation error:
ops.setNodeDisp(2, 1, u0, '-commit') # initial displacement u0 on node 2
ops.setNodeVel(2, 1, v0, '-commit') # initial velocity v0
ops.setNodeAccel(2, 1, -(c*v0 + k*u0)/m, '-commit') # consistent a0 (here p0 = 0)That is the last primitive, and Sections 1 to 6 together are the whole of the basic
opensees_sdof helper: model, mass, spring, Rayleigh damping, a (zero) load
pattern, the Newmark analysis, and the initial nudge. (osdof.py builds three
more on the same skeleton: opensees_sdof_ground for base motion via
UniformExcitation, and opensees_mdof_eigen / opensees_mdof_ground for the
multi-story building of Chapter 8.)
7A complete, verified example: free-vibration decay¶
Let us assemble all six pieces into one runnable script and check it against theory. We release a 2 t / 50 kN/m frame with 5 % damping from a 20 mm nudge and let it ring. Free vibration with and has the closed-form solution (from Chapter 2)
where is the damped natural frequency
(returned as p["wd"] by sd.natural below). The peaks of the response lie on
the exponential envelope , so the OpenSees
peaks should touch it.
# --- properties ---------------------------------------------------------
m, k, zeta = 2.0, 50.0, 0.05
wn = np.sqrt(k / m)
c = 2.0 * zeta * m * wn
u0, v0 = 0.020, 0.0 # 20 mm nudge, released from rest
dt, T = 0.005, 6.0
N = int(T / dt)
# --- build the model (Sections 1-4) -------------------------------------
ops.wipe()
ops.model('basic', '-ndm', 1, '-ndf', 1)
ops.node(1, 0.0); ops.fix(1, 1)
ops.node(2, 0.0); ops.mass(2, m)
ops.uniaxialMaterial('Elastic', 1, k)
ops.element('zeroLength', 1, 1, 2, '-mat', 1, '-dir', 1)
ops.rayleigh(c / m, 0.0, 0.0, 0.0) # exact c for the SDOF
ops.timeSeries('Path', 1, '-dt', dt, '-values', *([0.0] * N)) # p(t) = 0
ops.pattern('Plain', 1, 1); ops.load(2, 1.0)
# --- build the analysis (Section 5) -------------------------------------
ops.constraints('Plain'); ops.numberer('Plain'); ops.system('BandGeneral')
ops.test('NormDispIncr', 1e-10, 10); ops.algorithm('Linear')
ops.integrator('Newmark', 0.5, 0.25); ops.analysis('Transient')
# --- initial nudge (Section 6) ------------------------------------------
ops.setNodeDisp(2, 1, u0, '-commit')
ops.setNodeVel(2, 1, v0, '-commit')
ops.setNodeAccel(2, 1, -(c * v0 + k * u0) / m, '-commit') # consistent a0 (p0 = 0)
# --- march and record ---------------------------------------------------
t = np.arange(N) * dt
u = np.empty(N); u[0] = ops.nodeDisp(2, 1)
for i in range(N - 1):
ops.analyze(1, dt)
u[i + 1] = ops.nodeDisp(2, 1)
print(f"Tn = {2*np.pi/wn:.3f} s, built {N} steps at dt = {dt*1000:.0f} ms")Tn = 1.257 s, built 1200 steps at dt = 5 ms
Now overlay the analytic solution and its envelope, and report the error.
p = sd.natural(m, c, k)
wd = p["wd"]
env = u0 * np.exp(-zeta * wn * t) # decay envelope
u_an = np.exp(-zeta * wn * t) * (u0 * np.cos(wd * t)
+ (zeta * wn * u0 / wd) * np.sin(wd * t))
err = np.max(np.abs(u - u_an)) / u0 * 100 # % of u0
fig, ax = plt.subplots(figsize=(7.6, 3.2))
ax.plot(t, u * 1000, color=sd.PLT[0], lw=2.4, label="OpenSeesPy (Newmark)")
ax.plot(t, u_an * 1000, "--", color=sd.PLT[1], lw=1.6, label="analytic")
ax.plot(t, env * 1000, ":", color=sd.PLT[6], lw=1.4, label=r"$\pm u_0 e^{-\zeta\omega_n t}$")
ax.plot(t, -env * 1000, ":", color=sd.PLT[6], lw=1.4)
ax.axhline(0, color="#9aa3ab", lw=1.0)
ax.set_xlabel(r"time $t$ (s)"); ax.set_ylabel(r"drift $u$ (mm)")
ax.legend(loc="upper right", ncol=3)
plt.show()
print(f"max OpenSees-vs-analytic error = {err:.2f} % of the initial nudge")
max OpenSees-vs-analytic error = 0.04 % of the initial nudge

max OpenSees-vs-analytic error = 0.04 % of the initial nudge
Figure 1:OpenSeesPy free-vibration decay (solid) of the 2 t / 50 kN/m / 5 %-damped frame, released from a 20 mm nudge, against the analytic damped solution (dashed) and its envelope (dotted).
The trace hugs the analytic curve and touches the envelope at every peak. The
small gap that remains, 0.04 % of the nudge at most, is not a modeling error:
OpenSees marches with Newmark, which elongates the period slightly at finite
, while the analytic solution is exact. That period error is
and accumulates as a growing phase lag, so the gap shrinks
quadratically, and halving should cut it to a quarter, as the last
cell of this section confirms. This is the very gap Chapter 2 and
Chapter 3 discuss when comparing the exact
matrix-exponential stepper of sd.sdof_response to a Newmark march. (Committing
the consistent initial acceleration in Section 6 is what keeps this residual at
the genuine period-elongation floor rather than an
start-up transient.)
Finally, confirm that this hand-built script and the packaged opensees_sdof
helper are the same thing; they should agree to machine precision:
from osdof import opensees_sdof
th, uh, vh, ah = opensees_sdof(m, c, k, np.zeros(N), dt, u0=u0, v0=v0)
print(f"hand-built vs osdof.opensees_sdof : "
f"{np.max(np.abs(uh - u)) / u0 * 100:.2e} % of u0")hand-built vs osdof.opensees_sdof : 0.00e+00 % of u0
With the helper in hand, halving the time step takes one more call:
# Halve the time step and measure the error against the analytic solution again.
t2, u2, _, _ = opensees_sdof(m, c, k, np.zeros(2 * N), dt / 2, u0=u0, v0=v0)
u2_an = np.exp(-zeta * wn * t2) * (u0 * np.cos(wd * t2) + (zeta * wn * u0 / wd) * np.sin(wd * t2))
err2 = np.max(np.abs(u2 - u2_an)) / u0 * 100
print(f"error at dt = {dt*1000:.1f} ms: {err:.4f} % of u0; at dt = {dt*500:.2f} ms: {err2:.4f} % of u0"
f" (ratio {err / err2:.2f})")error at dt = 5.0 ms: 0.0384 % of u0; at dt = 2.50 ms: 0.0096 % of u0 (ratio 4.00)
The error falls from 0.038 % to 0.0096 % of the nudge, a ratio of 4.0, the signature of a second-order method. An error left over from a wrong starting acceleration would only have halved. The same factor of four appeared in Chapter 2, where the OpenSees march was compared with the exact NumPy stepper.
8Quick reference¶
The whole linear 1-D dynamics workflow, in the order you build it:
| Step | Call | Purpose |
|---|---|---|
| Domain | model('basic','-ndm',1,'-ndf',1) | 1-D, 1 DOF/node |
| Nodes | node(1,0.); fix(1,1) / node(2,0.); mass(2,m) | ground + mass |
| Spring | uniaxialMaterial('Elastic',1,k) + element('zeroLength',...,'-dir',1) | stiffness k (or Steel01 for yielding) |
| Damping | rayleigh(c/m, 0., 0., 0.) | exact c for the SDOF |
| Load | timeSeries('Path',...,'-dt',dt,'-values',*p) + pattern('Plain',...) + load(2,1.) | force history p(t) (use -m*ag for quakes) |
| Solver | constraints/numberer/system/test/algorithm + integrator('Newmark',0.5,0.25) + analysis('Transient') | assemble the time-stepper |
| Initial | setNodeDisp(2,1,u0,'-commit') / setNodeVel(2,1,v0,'-commit') | free-vibration nudge |
| March | analyze(1,dt) then nodeDisp/nodeVel/nodeAccel(2,1) | step and read response |
For everyday cross-checks you do not need to retype any of this: just
from osdof import opensees_sdof and call
opensees_sdof(m, c, k, p, dt, u0=0, v0=0), which returns t, u, v, a and is
documented above line by line. For base motion,
opensees_sdof_ground(m, c, k, ag, dt) takes the acceleration record directly.
Appendix B lists all four helpers with their arguments.