Algorithm Search⌘K

Algorithmcore

Newmark-beta method

Also called Newmark method.

Steps M a + C v + K d = F in time with parameters beta and gamma. beta = 1/4, gamma = 1/2 is second order and unconditionally stable for linear problems only.

Time
Θ(n b^2 + m n b) worst
Space
Θ(n b) extra
Where
b = the half bandwidth of the effective matrix; m = the number of time steps; n = the number of unknowns
Properties
deterministicone step
Origin
Nathan M. Newmark, 1959

The Newmark-beta method is a one-step time integrator for the second order equations of structural dynamics Md¨+Cd˙+Kd=F(t)M\ddot d + C\dot d + K d = F(t), which come from a finite element discretization in space. It advances displacement dd, velocity vv and acceleration aa with two parameters β\beta and γ\gamma: γ=12\gamma = \tfrac12 gives second order accuracy, β=0\beta = 0 gives the explicit central difference method, and β=14\beta = \tfrac14, γ=12\gamma = \tfrac12 gives the implicit average acceleration rule, which is unconditionally stable and conserves the energy exactly for linear undamped problems. Unconditional stability is a property of linear problems: for nonlinear problems the same rule can let the energy grow. Graphics simulators of cloth and soft bodies often step with backward Euler, which is stable but damps every motion strongly, and Newmark with β=14\beta = \tfrac14 adds second order accuracy and no artificial damping at the price of the same one linear solve per step.

In plain words

A vibrating structure moves, so its position, its speed and its rate of change of speed change at every instant. A computer can only look at the structure at separate moments, a small time apart. The Newmark method gets the next position and speed from the current ones and from a guess about how the speed changes during the step. One setting assumes that the rate of change of speed is the average of its values at the start and the end of the step. This setting never blows up for a linear structure, whatever the size of the step, and it keeps the energy of a free vibration exactly. Another setting uses only the start values. That one is cheap, but it fails when the step is too long compared with the fastest vibration.

The problem

Input. A symmetric positive definite mass matrix MM, a damping matrix CC and a stiffness matrix KK, all n×nn \times n, a load F(t)F(t), initial values d0d_0 and v0v_0, a time step dtdt and a number of steps mm. On finite element pages hh is the mesh size, so the time step is dtdt here.

Output. Approximations dk≈d(tk)d_k \approx d(t_k), vk≈d˙(tk)v_k \approx \dot d(t_k) and ak≈d¨(tk)a_k \approx \ddot d(t_k) at tk=k dtt_k = k\,dt for k=0,…,mk = 0, \ldots, m, such that Mak+Cvk+Kdk=F(tk)M a_k + C v_k + K d_k = F(t_k) holds at every step and

dk+1=dk+dt vk+dt22((1−2β)ak+2βak+1),vk+1=vk+dt((1−γ)ak+γak+1).d_{k+1} = d_k + dt\, v_k + \frac{dt^2}{2}\big((1 - 2\beta) a_k + 2\beta a_{k+1}\big), \qquad v_{k+1} = v_k + dt\big((1 - \gamma) a_k + \gamma a_{k+1}\big).

The algorithm

The step first forms predictors d~\tilde d and v~\tilde v from the old values, then solves one linear system for the new acceleration, then corrects.

  1. NEWMARK(M, C, K, F, d_0, v_0, dt, m, beta, gamma)
  2. 1a_0 <- solve M a = F(0) - C v_0 - K d_0
  3. 2for k <- 0 to m - 1 do
  4. 3dp <- d_k + dt v_k + (dt^2 / 2)(1 - 2 beta) a_k ▷ displacement predictor
  5. 4vp <- v_k + (1 - gamma) dt a_k ▷ velocity predictor
  6. 5a_{k+1} <- solve (M + gamma dt C + beta dt^2 K) a = F(t_{k+1}) - C vp - K dp
  7. 6d_{k+1} <- dp + beta dt^2 a_{k+1}
  8. 7v_{k+1} <- vp + gamma dt a_{k+1}
  9. 8return (d_k, v_k, a_k) for k = 0 .. m

The matrix M∗=M+γ dt C+β dt2KM^* = M + \gamma\,dt\,C + \beta\,dt^2 K of line L5 does not change when dtdt is fixed, so a code factors it once. With β=0\beta = 0, C=0C = 0 and a diagonal (lumped) MM, line L5 is a division per unknown and the method is explicit.

Nonlinear problems. For an internal force fint(d)f_{\rm int}(d) in place of KdK d, line L5 becomes the nonlinear equation Ma+C(v~+γ dt a)+fint(d~+β dt2a)=F(tk+1)M a + C(\tilde v + \gamma\,dt\,a) + f_{\rm int}(\tilde d + \beta\,dt^2 a) = F(t_{k+1}). A Newton iteration solves it with the matrix M+γ dt C+β dt2KT(d)M + \gamma\,dt\,C + \beta\,dt^2 K_T(d), where KT=∂fint/∂dK_T = \partial f_{\rm int}/\partial d is the tangent stiffness at the current iterate. The reference code does this in newmark_nonlinear.

Common members of the family.

name β\beta γ\gamma type order stable for ω dt\omega\,dt (undamped)
central difference 00 12\tfrac12 explicit with lumped MM 2 ≤2\le 2
Fox-Goodwin 112\tfrac1{12} 12\tfrac12 implicit 2 ≤6=2.449\le \sqrt6 = 2.449
linear acceleration 16\tfrac16 12\tfrac12 implicit 2 ≤12=3.464\le \sqrt{12} = 3.464
average acceleration (trapezoidal rule) 14\tfrac14 12\tfrac12 implicit 2 all
damped Newmark 14(γ+12)2\tfrac14(\gamma + \tfrac12)^2 >12> \tfrac12 implicit 1 all

Here ω\omega is the highest natural frequency of the mesh, the square root of the largest eigenvalue of Kx=ω2MxK x = \omega^2 M x. The table holds for C=0C = 0. The limit for γ≥12\gamma \ge \tfrac12 and 2β<γ2\beta < \gamma is ω dt≤(γ/2−β)−1/2\omega\,dt \le (\gamma/2 - \beta)^{-1/2}.

Example

One step by hand. Take m=k=1m = k = 1 (so ω=1\omega = 1), no damping, no load, d0=1d_0 = 1, v0=0v_0 = 0, dt=12dt = \tfrac12, and the average acceleration rule. Line L1 gives a0=−1a_0 = -1. Line L3 gives d~=1+0+18⋅12⋅(−1)=1516\tilde d = 1 + 0 + \tfrac18 \cdot \tfrac12 \cdot (-1) = \tfrac{15}{16} and line L4 gives v~=−14\tilde v = -\tfrac14. Line L5 solves (1+14⋅14) a1=−1516(1 + \tfrac14 \cdot \tfrac14)\,a_1 = -\tfrac{15}{16}, so a1=−1517a_1 = -\tfrac{15}{17}. Then d1=1516−116⋅1517=1517d_1 = \tfrac{15}{16} - \tfrac1{16}\cdot\tfrac{15}{17} = \tfrac{15}{17} and v1=−14−14⋅1517=−817v_1 = -\tfrac14 - \tfrac14 \cdot \tfrac{15}{17} = -\tfrac{8}{17}. The energy 12(v12+d12)=12⋅64+225289=12\tfrac12(v_1^2 + d_1^2) = \tfrac12 \cdot \tfrac{64 + 225}{289} = \tfrac12 is the initial energy. The exact values are cos⁡12=0.87758\cos\tfrac12 = 0.87758 and −sin⁡12=−0.47943-\sin\tfrac12 = -0.47943.

kk dkd_k vkv_k exact cos⁡tk\cos t_k exact −sin⁡tk-\sin t_k
0 1 0 1 0
1 15/17 = 0.88235 -8/17 = -0.47059 0.87758 -0.47943
2 161/289 = 0.55709 -240/289 = -0.83045 0.54030 -0.84147
3 495/4913 = 0.10075 -4888/4913 = -0.99491 0.07074 -0.99749
4 -0.37929 -0.92528 -0.41615 -0.90930

The discrete solution is dk=cos⁡(kθ)d_k = \cos(k\theta), vk=−sin⁡(kθ)v_k = -\sin(k\theta) with θ=2arctan⁡(ω dt/2)=0.48996\theta = 2\arctan(\omega\,dt/2) = 0.48996 per step in place of 0.50.5. The amplitude is exact and the phase lags: the period grows by the factor ω dt/(2arctan⁡(ω dt/2))\omega\,dt / (2\arctan(\omega\,dt/2)), which is 1.02051.0205 for ω dt=12\omega\,dt = \tfrac12, 1.07841.0784 for ω dt=1\omega\,dt = 1 and 1.27321.2732 for ω dt=2\omega\,dt = 2. For small steps the relative period error is about (ω dt)2/12(\omega\,dt)^2/12.

Other members on the same problem. After 4 steps of dt=12dt = \tfrac12 (at t=2t = 2, exact d=−0.41615d = -0.41615) the central difference method gives d4=−0.43555d_4 = -0.43555, Fox-Goodwin −0.41639-0.41639 and linear acceleration −0.39764-0.39764. Fox-Goodwin is the most accurate of the four here, but it is only conditionally stable.

Correctness

The full proofs are in the correctness proof. Three facts carry the method.

  • Consistency and order. Insert the exact solution into the two update formulas. The velocity formula has a residual (12−γ) dt2 d...+O(dt3)(\tfrac12 - \gamma)\,dt^2\,\dddot d + O(dt^3) and the displacement formula (16−β) dt3 d...+O(dt4)(\tfrac16 - \beta)\,dt^3\,\dddot d + O(dt^4). So the local truncation error per unit time is O(dt)O(dt) unless γ=12\gamma = \tfrac12, and O(dt2)O(dt^2) when γ=12\gamma = \tfrac12. For the average acceleration rule the proof bounds the global error in the energy norm by C T dt2C\,T\,dt^2 directly. For the other stable members the global error has the order of the local one by the usual stability argument (Hughes 2000, Chapter 9), and the reference tests measure orders 2 and 1.
  • Stability for linear problems. For one mode d¨+ω2d=0\ddot d + \omega^2 d = 0, the displacements satisfy the three-term recurrence (1+βΩ2)dk+1−(2−(12−2β+γ)Ω2)dk+(1+(12+β−γ)Ω2)dk−1=0(1 + \beta\Omega^2) d_{k+1} - (2 - (\tfrac12 - 2\beta + \gamma)\Omega^2) d_k + (1 + (\tfrac12 + \beta - \gamma)\Omega^2) d_{k-1} = 0 with Ω=ω dt\Omega = \omega\,dt. For Ω>0\Omega > 0 the steps stay bounded exactly when γ≥12\gamma \ge \tfrac12 and either 2β≥γ2\beta \ge \gamma or Ω2≤2/(γ−2β)\Omega^2 \le 2/(\gamma - 2\beta), with strict inequality when γ=12\gamma = \tfrac12. The modes of Kx=ω2MxK x = \omega^2 M x with C=0C = 0 decouple, so the same holds for the full system.
  • Energy. For β=14\beta = \tfrac14, γ=12\gamma = \tfrac12, C=0C = 0 and F=0F = 0, the quantity 12vk⊤Mvk+12dk⊤Kdk\tfrac12 v_k^\top M v_k + \tfrac12 d_k^\top K d_k is the same at every step, for any dtdt, by a two-line identity.
Proof: Order, linear stability and energy of the Newmark-beta methodpartial

Setting. MM is symmetric positive definite, KK is symmetric and β≥0\beta \ge 0, so that M+γ dt C+β dt2KM + \gamma\,dt\,C + \beta\,dt^2 K is invertible for the problems below. Lines L1 to L7 give, for every k≥0k \ge 0,

(U1)dk+1=dk+dt vk+dt2((12−β)ak+βak+1),(U2)vk+1=vk+dt((1−γ)ak+γak+1),\text{(U1)}\quad d_{k+1} = d_k + dt\,v_k + dt^2\big((\tfrac12 - \beta) a_k + \beta a_{k+1}\big), \qquad \text{(U2)}\quad v_{k+1} = v_k + dt\big((1 - \gamma) a_k + \gamma a_{k+1}\big),

and Mak+Cvk+Kdk=F(tk)M a_k + C v_k + K d_k = F(t_k). Indeed line L6 gives dk+1=d~+β dt2ak+1d_{k+1} = \tilde d + \beta\,dt^2 a_{k+1} with d~\tilde d from line L3, and line L7 gives vk+1=v~+γ dt ak+1v_{k+1} = \tilde v + \gamma\,dt\,a_{k+1} with v~\tilde v from line L4. The system of line L5 is Mak+1+Cvk+1+Kdk+1=F(tk+1)M a_{k+1} + C v_{k+1} + K d_{k+1} = F(t_{k+1}) written with these two expressions. Line L1 gives the equation at k=0k = 0. The status is partial because Theorem 9 cites, and does not prove, the convergence of the members other than the average acceleration rule.

Lemma 1 (two-step form). For every k≥1k \ge 1, whatever the equation of motion,

dk+1−2dk+dk−1=dt2(βak+1+(12−2β+γ)ak+(12+β−γ)ak−1).d_{k+1} - 2 d_k + d_{k-1} = dt^2\big(\beta a_{k+1} + (\tfrac12 - 2\beta + \gamma) a_k + (\tfrac12 + \beta - \gamma) a_{k-1}\big).

Proof. Subtract (U1) for the step from k−1k - 1 to kk from (U1) for the step from kk to k+1k + 1. This gives dk+1−2dk+dk−1=dt (vk−vk−1)+dt2((12−β)(ak−ak−1)+β(ak+1−ak))d_{k+1} - 2d_k + d_{k-1} = dt\,(v_k - v_{k-1}) + dt^2\big((\tfrac12 - \beta)(a_k - a_{k-1}) + \beta(a_{k+1} - a_k)\big). By (U2), vk−vk−1=dt((1−γ)ak−1+γak)v_k - v_{k-1} = dt\big((1 - \gamma) a_{k-1} + \gamma a_k\big). Collect the terms: ak+1a_{k+1} has the coefficient β\beta, aka_k has γ+12−β−β\gamma + \tfrac12 - \beta - \beta, and ak−1a_{k-1} has 1−γ−12+β1 - \gamma - \tfrac12 + \beta. □\square

Corollary (central difference). For β=0\beta = 0 and γ=12\gamma = \tfrac12, Lemma 1 reads dk+1−2dk+dk−1=dt2akd_{k+1} - 2d_k + d_{k-1} = dt^2 a_k. With C=0C = 0 and Mak=F(tk)−fint(dk)M a_k = F(t_k) - f_{\rm int}(d_k) this is the central difference method M(dk+1−2dk+dk−1)/dt2+fint(dk)=F(tk)M(d_{k+1} - 2d_k + d_{k-1})/dt^2 + f_{\rm int}(d_k) = F(t_k), also for a nonlinear fintf_{\rm int}.

Lemma 2 (modal reduction). Let C=0C = 0 and F=0F = 0, and let KK be positive semidefinite. Let Φ\Phi be the matrix of generalized eigenvectors, KΦ=MΦ diag(ωi2)K\Phi = M\Phi\,\mathrm{diag}(\omega_i^2) with Φ⊤MΦ=I\Phi^\top M \Phi = I. Write dk=Φqkd_k = \Phi q_k, vk=Φpkv_k = \Phi p_k and ak=Φrka_k = \Phi r_k. Then each component (qk,i,pk,i,rk,i)(q_{k,i}, p_{k,i}, r_{k,i}) is the Newmark sequence of the scalar problem q¨+ωi2q=0\ddot q + \omega_i^2 q = 0 with the same dtdt, β\beta and γ\gamma.

Proof. Φ\Phi is invertible, and (U1) and (U2) are linear with scalar coefficients, so they hold for qq, pp and rr in place of dd, vv and aa. Multiply Mak+Kdk=0M a_k + K d_k = 0 by Φ⊤\Phi^\top: Φ⊤MΦrk+Φ⊤KΦqk=rk+diag(ωi2)qk=0\Phi^\top M \Phi r_k + \Phi^\top K \Phi q_k = r_k + \mathrm{diag}(\omega_i^2) q_k = 0. So rk,i=−ωi2qk,ir_{k,i} = -\omega_i^2 q_{k,i}, which is the scalar equation of motion. □\square

So the sequence (dk,vk)(d_k, v_k) is bounded for all initial data exactly when each scalar mode is bounded for all initial data.

Lemma 3 (scalar step and its eigenvalues). For d¨+ω2d=0\ddot d + \omega^2 d = 0 put Ω=ω dt\Omega = \omega\,dt and D=1+βΩ2>0D = 1 + \beta\Omega^2 > 0. The step is a linear map xk+1=Axkx_{k+1} = A x_k of xk=(dk,dt vk)x_k = (d_k, dt\,v_k). Its eigenvalues are the roots of

p(λ)=λ2+bλ+c,b=−2−(12−2β+γ)Ω2D,c=1+(12+β−γ)Ω2D.p(\lambda) = \lambda^2 + b\lambda + c, \qquad b = -\frac{2 - (\tfrac12 - 2\beta + \gamma)\Omega^2}{D}, \qquad c = \frac{1 + (\tfrac12 + \beta - \gamma)\Omega^2}{D}.

Proof. With ak=−ω2dka_k = -\omega^2 d_k, Lemma 1 becomes Ddk+1−(2−(12−2β+γ)Ω2)dk+(1+(12+β−γ)Ω2)dk−1=0D d_{k+1} - \big(2 - (\tfrac12 - 2\beta + \gamma)\Omega^2\big) d_k + \big(1 + (\tfrac12 + \beta - \gamma)\Omega^2\big) d_{k-1} = 0, that is dk+1+b dk+c dk−1=0d_{k+1} + b\,d_k + c\,d_{k-1} = 0 for k≥1k \ge 1. Put yk=(dk,dk+1)y_k = (d_k, d_{k+1}). Then yk+1=Pyky_{k+1} = P y_k for all k≥0k \ge 0, where PP is the companion matrix of pp, with rows (0,1)(0, 1) and (−c,−b)(-c, -b). By (U1) with a=−ω2da = -\omega^2 d, Ddk+1=(1−(12−β)Ω2)dk+dt vkD d_{k+1} = (1 - (\tfrac12 - \beta)\Omega^2) d_k + dt\,v_k, so yk=Txky_k = T x_k with TT lower triangular with diagonal entries 11 and 1/D1/D, so TT is invertible. Since every x0x_0 is a possible initial value, TAx=PTxT A x = P T x for all xx, and A=T−1PTA = T^{-1} P T has the same eigenvalues as PP, the roots of pp. □\square

Lemma 4 (roots in the unit disk). Let bb and cc be real. Both roots of λ2+bλ+c\lambda^2 + b\lambda + c have modulus at most 1 exactly when ∣c∣≤1|c| \le 1 and ∣b∣≤1+c|b| \le 1 + c.

Proof. If the roots are not real, they are conjugate, ∣λ∣2=c|\lambda|^2 = c, and b2<4cb^2 < 4c. Then c>0c > 0 and ∣b∣<2c≤1+c|b| < 2\sqrt c \le 1 + c, so the condition holds exactly when c≤1c \le 1, which is exactly when ∣λ∣≤1|\lambda| \le 1. If the roots λ1≤λ2\lambda_1 \le \lambda_2 are real, then p(1)=1+b+c=(1−λ1)(1−λ2)p(1) = 1 + b + c = (1 - \lambda_1)(1 - \lambda_2), p(−1)=1−b+c=(1+λ1)(1+λ2)p(-1) = 1 - b + c = (1 + \lambda_1)(1 + \lambda_2) and c=λ1λ2c = \lambda_1\lambda_2. If both roots are in [−1,1][-1, 1], all three facts give p(±1)≥0p(\pm1) \ge 0 and ∣c∣≤1|c| \le 1, and p(±1)≥0p(\pm 1) \ge 0 is ∣b∣≤1+c|b| \le 1 + c. Conversely let p(±1)≥0p(\pm 1) \ge 0 and ∣c∣≤1|c| \le 1. If λ2>1\lambda_2 > 1, then p(1)≥0p(1) \ge 0 forces λ1≥1\lambda_1 \ge 1, and c=λ1λ2>1c = \lambda_1 \lambda_2 > 1, a contradiction. If λ1<−1\lambda_1 < -1, then p(−1)≥0p(-1) \ge 0 forces λ2≤−1\lambda_2 \le -1 and again c>1c > 1. □\square

Theorem 5 (linear stability). Let Ω>0\Omega > 0. The scalar sequence xkx_k is bounded for all initial data exactly when γ≥12\gamma \ge \tfrac12 and either 2β≥γ2\beta \ge \gamma, or Ω2≤2/(γ−2β)\Omega^2 \le 2/(\gamma - 2\beta) with strict inequality when γ=12\gamma = \tfrac12.

Proof. A direct computation from Lemma 3 gives

p(1)=Ω2D,p(−1)=4+(4β−2γ)Ω2D,1−c=(γ−12)Ω2D,1+c=p(1)+p(−1)2.p(1) = \frac{\Omega^2}{D}, \qquad p(-1) = \frac{4 + (4\beta - 2\gamma)\Omega^2}{D}, \qquad 1 - c = \frac{(\gamma - \tfrac12)\Omega^2}{D}, \qquad 1 + c = \frac{p(1) + p(-1)}{2}.

The powers AkA^k stay bounded exactly when the roots of pp are in the closed unit disk and a root of modulus 1 is not a double root, because a 2×22 \times 2 matrix with a double eigenvalue λ\lambda is either λI\lambda I or similar to a Jordan block, and A=T−1PTA = T^{-1} P T is not λI\lambda I (the companion matrix PP has the entry 11 off the diagonal).

By Lemma 4 the roots are in the closed disk exactly when c≤1c \le 1, 1+c≥01 + c \ge 0 and p(±1)≥0p(\pm 1) \ge 0. Here p(1)>0p(1) > 0 always. The condition c≤1c \le 1 is γ≥12\gamma \ge \tfrac12. The condition p(−1)≥0p(-1) \ge 0 is 2β≥γ2\beta \ge \gamma or Ω2≤2/(γ−2β)\Omega^2 \le 2/(\gamma - 2\beta). It implies 1+c≥01 + c \ge 0 by the last identity.

Double roots on the circle: a double root 11 needs p(1)=0p(1) = 0, which is impossible for Ω>0\Omega > 0. A double root −1-1 needs p(−1)=0p(-1) = 0 and c=1c = 1. If p(−1)=0p(-1) = 0, then 1+c=Ω2/(2D)1 + c = \Omega^2/(2D), and c=1c = 1 means Ω2(1−4β)=4\Omega^2(1 - 4\beta) = 4. With Ω2=2/(γ−2β)\Omega^2 = 2/(\gamma - 2\beta) this is 2−8β=4γ−8β2 - 8\beta = 4\gamma - 8\beta, that is γ=12\gamma = \tfrac12. Non-real roots are distinct. So the only bad case inside the closed disk is γ=12\gamma = \tfrac12 with Ω2=2/(γ−2β)\Omega^2 = 2/(\gamma - 2\beta). □\square

Corollaries. For C=0C = 0, by Lemma 2 the full system is stable for every dtdt when 2β≥γ≥122\beta \ge \gamma \ge \tfrac12, and otherwise (for γ≥12\gamma \ge \tfrac12) when ωmax⁡dt<(γ/2−β)−1/2\omega_{\max} dt < (\gamma/2 - \beta)^{-1/2}. This limit is 22 for β=0\beta = 0, 6\sqrt 6 for β=112\beta = \tfrac1{12} and 12\sqrt{12} for β=16\beta = \tfrac16, all with γ=12\gamma = \tfrac12. For γ>12\gamma > \tfrac12 and β=14(γ+12)2\beta = \tfrac14(\gamma + \tfrac12)^2 we have 2β−γ=12(γ−12)2≥02\beta - \gamma = \tfrac12(\gamma - \tfrac12)^2 \ge 0, so the damped member is unconditionally stable. For a rigid body mode, Ω=0\Omega = 0, p(λ)=(λ−1)2p(\lambda) = (\lambda - 1)^2 and dk=d0+k dt v0d_k = d_0 + k\,dt\,v_0, which is the exact free motion.

Phase of the average acceleration rule. For β=14\beta = \tfrac14 and γ=12\gamma = \tfrac12, c=1c = 1 and −b/2=(1−Ω2/4)/(1+Ω2/4)-b/2 = (1 - \Omega^2/4)/(1 + \Omega^2/4). With φ=arctan⁡(Ω/2)\varphi = \arctan(\Omega/2) this is (1−tan⁡2φ)/(1+tan⁡2φ)=cos⁡2φ(1 - \tan^2\varphi)/(1 + \tan^2\varphi) = \cos 2\varphi, so the roots are e±2iφe^{\pm 2 i \varphi}: modulus 1 and the angle 2arctan⁡(Ω/2)2\arctan(\Omega/2) per step in place of the exact Ω\Omega.

Lemma 6 (consistency). Let dd be a smooth exact solution with a=d¨a = \ddot d. Define the defects τkv=v(tk+1)−v(tk)−dt((1−γ)a(tk)+γa(tk+1))\tau^v_k = v(t_{k+1}) - v(t_k) - dt\big((1 - \gamma)a(t_k) + \gamma a(t_{k+1})\big) and τkd=d(tk+1)−d(tk)−dt v(tk)−dt2((12−β)a(tk)+βa(tk+1))\tau^d_k = d(t_{k+1}) - d(t_k) - dt\,v(t_k) - dt^2\big((\tfrac12 - \beta)a(t_k) + \beta a(t_{k+1})\big). Then

τkv=(12−γ) dt2 a˙(tk)+O(dt3),τkd=(16−β) dt3 a˙(tk)+O(dt4).\tau^v_k = (\tfrac12 - \gamma)\,dt^2\,\dot a(t_k) + O(dt^3), \qquad \tau^d_k = (\tfrac16 - \beta)\,dt^3\,\dot a(t_k) + O(dt^4).

Proof. Insert the Taylor expansions v(t+dt)=v+dt a+12dt2a˙+O(dt3)v(t + dt) = v + dt\,a + \tfrac12 dt^2 \dot a + O(dt^3), a(t+dt)=a+dt a˙+O(dt2)a(t + dt) = a + dt\,\dot a + O(dt^2) and d(t+dt)=d+dt v+12dt2a+16dt3a˙+O(dt4)d(t + dt) = d + dt\,v + \tfrac12 dt^2 a + \tfrac16 dt^3 \dot a + O(dt^4). In τv\tau^v the terms in dt adt\,a cancel and 12dt2a˙−γ dt2a˙\tfrac12 dt^2\dot a - \gamma\,dt^2 \dot a remains. In τd\tau^d the terms in dt vdt\,v and dt2adt^2 a cancel and 16dt3a˙−β dt3a˙\tfrac16 dt^3 \dot a - \beta\,dt^3\dot a remains. □\square

So the defect per unit time, τ/dt\tau/dt, is O(dt)O(dt) for γ≠12\gamma \ne \tfrac12 (its leading term is not zero when a˙≠0\dot a \ne 0) and O(dt2)O(dt^2) for γ=12\gamma = \tfrac12. For γ=12\gamma = \tfrac12, τkv\tau^v_k is the error of the trapezoidal rule for ∫a\int a, so ∣τkv∣≤112dt3max⁡∣d(4)∣|\tau^v_k| \le \tfrac1{12} dt^3 \max |d^{(4)}| for each component, and ∣τkd∣≤112dt3max⁡∣d(3)∣+16dt4max⁡∣d(4)∣|\tau^d_k| \le \tfrac{1}{12} dt^3 \max|d^{(3)}| + \tfrac{1}{6} dt^4 \max|d^{(4)}| for β=14\beta = \tfrac14.

Theorem 7 (energy of the average acceleration rule). Let β=14\beta = \tfrac14, γ=12\gamma = \tfrac12, C=0C = 0, F=0F = 0 and KK symmetric. Then Ek=12vk⊤Mvk+12dk⊤KdkE_k = \tfrac12 v_k^\top M v_k + \tfrac12 d_k^\top K d_k is the same for all kk, for every dt>0dt > 0.

Proof. (U2) gives vk+1−vk=dt2(ak+ak+1)v_{k+1} - v_k = \tfrac{dt}2(a_k + a_{k+1}), and (U1) then gives dk+1−dk=dt vk+dt2(vk+1−vk)=dt2(vk+vk+1)d_{k+1} - d_k = dt\,v_k + \tfrac{dt}{2}(v_{k+1} - v_k) = \tfrac{dt}2(v_k + v_{k+1}). The equation of motion gives M(vk+1−vk)=−dt2K(dk+dk+1)M(v_{k+1} - v_k) = -\tfrac{dt}2 K(d_k + d_{k+1}). For a symmetric matrix SS, x⊤Sx−y⊤Sy=(x−y)⊤S(x+y)x^\top S x - y^\top S y = (x - y)^\top S (x + y). So

Ek+1−Ek=−dt4(dk+dk+1)⊤K(vk+vk+1)+dt4(vk+vk+1)⊤K(dk+dk+1)=0,E_{k+1} - E_k = -\tfrac{dt}{4}(d_k + d_{k+1})^\top K (v_k + v_{k+1}) + \tfrac{dt}{4}(v_k + v_{k+1})^\top K (d_k + d_{k+1}) = 0,

because KK is symmetric. □\square

The two relations of the proof are the trapezoidal rule for d˙=v\dot d = v and Mv˙=−KdM\dot v = -K d. With a load and damping the same steps give the trapezoidal rule for d˙=v\dot d = v, Mv˙=F−Cv−KdM \dot v = F - C v - K d. For a nonlinear fintf_{\rm int} the last step fails, because fint(dk)+fint(dk+1)f_{\rm int}(d_k) + f_{\rm int}(d_{k+1}) is not a gradient difference, and the energy is not conserved (the reference test test_nonlinear_energy_is_not_exact measures a change of more than 10−410^{-4}).

Remark (conserved quadratic form for γ=12\gamma = \tfrac12). For γ=12\gamma = \tfrac12 the scalar recurrence of Lemma 3 has c=1c = 1, so it reads Ddk+1−Bdk+Ddk−1=0D d_{k+1} - B d_k + D d_{k-1} = 0 with B=−bDB = -bD. Then Qk=Ddk+12−Bdk+1dk+Ddk2Q_k = D d_{k+1}^2 - B d_{k+1} d_k + D d_k^2 satisfies Qk−Qk−1=(dk+1−dk−1)(D(dk+1+dk−1)−Bdk)=0Q_k - Q_{k-1} = (d_{k+1} - d_{k-1})\big(D(d_{k+1} + d_{k-1}) - B d_k\big) = 0. So every member with γ=12\gamma = \tfrac12 conserves a quadratic form for linear undamped problems, and by Lemma 2 this holds mode by mode.

Theorem 8 (second order convergence of the average acceleration rule). Let β=14\beta = \tfrac14, γ=12\gamma = \tfrac12, C=0C = 0, KK symmetric positive definite, and let the exact solution have a bounded fourth derivative on [0,T][0, T]. Start from the exact d0d_0 and v0v_0. Let ek=d(tk)−dke_k = d(t_k) - d_k and fk=v(tk)−vkf_k = v(t_k) - v_k. Then for k dt≤Tk\,dt \le T,

(fk⊤Mfk+ek⊤Kek)1/2≤C0 T dt2,\big(f_k^\top M f_k + e_k^\top K e_k\big)^{1/2} \le C_0\,T\,dt^2,

where C0C_0 depends on MM, KK and the bounds of the third and fourth derivatives of dd.

Proof. The exact solution satisfies (U1) and (U2) with the defects τkd\tau^d_k and τkv\tau^v_k added, and Ma(t)+Kd(t)=F(t)M a(t) + K d(t) = F(t). Subtract the numerical scheme. The errors satisfy the scheme for F=0F = 0 with the defects added: fk+1−fk=dt2(αk+αk+1)+τkvf_{k+1} - f_k = \tfrac{dt}2(\alpha_k + \alpha_{k+1}) + \tau^v_k and ek+1−ek=dt fk+dt24(αk+αk+1)+τkde_{k+1} - e_k = dt\,f_k + \tfrac{dt^2}4(\alpha_k + \alpha_{k+1}) + \tau^d_k, with Mαk=−KekM\alpha_k = -K e_k. As in Theorem 7, ek+1−ek=dt2(fk+fk+1)+ρke_{k+1} - e_k = \tfrac{dt}2(f_k + f_{k+1}) + \rho_k with ρk=τkd−dt2τkv\rho_k = \tau^d_k - \tfrac{dt}2\tau^v_k, and M(fk+1−fk)=−dt2K(ek+ek+1)+MτkvM(f_{k+1} - f_k) = -\tfrac{dt}2 K(e_k + e_{k+1}) + M\tau^v_k. The same computation as in Theorem 7 gives, for Ek=12(fk⊤Mfk+ek⊤Kek)\mathcal E_k = \tfrac12(f_k^\top M f_k + e_k^\top K e_k),

Ek+1−Ek=12(τkv)⊤M(fk+fk+1)+12ρk⊤K(ek+ek+1).\mathcal E_{k+1} - \mathcal E_k = \tfrac12 (\tau^v_k)^\top M (f_k + f_{k+1}) + \tfrac12 \rho_k^\top K (e_k + e_{k+1}).

Write ∥x∥M=(x⊤Mx)1/2\|x\|_M = (x^\top M x)^{1/2}, ∥x∥K=(x⊤Kx)1/2\|x\|_K = (x^\top K x)^{1/2} and sk=(2Ek)1/2s_k = (2\mathcal E_k)^{1/2}. By the Cauchy-Schwarz inequality in these inner products, ∥fk∥M≤sk\|f_k\|_M \le s_k and ∥ek∥K≤sk\|e_k\|_K \le s_k, so 12(sk+12−sk2)≤12δk(sk+sk+1)\tfrac12(s_{k+1}^2 - s_k^2) \le \tfrac12 \delta_k (s_k + s_{k+1}) with δk=∥τkv∥M+∥ρk∥K\delta_k = \|\tau^v_k\|_M + \|\rho_k\|_K. Divide by 12(sk+sk+1)\tfrac12(s_k + s_{k+1}) (if it is 00 the claim sk+1≤sk+δks_{k+1} \le s_k + \delta_k is trivial) to get sk+1≤sk+δks_{k+1} \le s_k + \delta_k. With s0=0s_0 = 0, sk≤∑j<kδjs_k \le \sum_{j < k} \delta_j. By Lemma 6, δj≤C0 dt3\delta_j \le C_0\,dt^3, and there are k≤T/dtk \le T/dt terms. □\square

Theorem 9 (convergence of the other members, cited). For fixed β\beta and γ\gamma in the stable set of Theorem 5, with the inequality strict, a linear problem and a smooth solution, the global error on [0,T][0, T] is O(dt2)O(dt^2) for γ=12\gamma = \tfrac12 and O(dt)O(dt) for γ>12\gamma > \tfrac12. This follows from Lemma 6 and a bound on the powers of the step matrix that is uniform in dtdt (Hughes 2000, Chapter 9). We do not repeat that bound here. The reference tests measure the orders 2 for β∈{0,112,16,14}\beta \in \{0, \tfrac1{12}, \tfrac16, \tfrac14\} and 1 for γ=0.6\gamma = 0.6.

Complexity

In the real-RAM model, for a linear problem with fixed dtdt, the effective matrix is factored once. For a banded matrix with half bandwidth bb, as from a finite element mesh with a good node order, the Cholesky factorization costs Θ(nb2)\Theta(n b^2) and each step costs Θ(nb)\Theta(n b) for the matrix-vector products and the two triangular solves. The total for mm steps is Θ(nb2+mnb)\Theta(n b^2 + m n b), and the memory is Θ(nb)\Theta(n b) for the factor. The explicit case (β=0\beta = 0, lumped MM, CC diagonal or zero) needs no factorization, and a step costs one product with KK, Θ(nnz(K))\Theta(\text{nnz}(K)), but dtdt must stay below 2/ωmax⁡2/\omega_{\max}, and ωmax⁡\omega_{\max} grows like 1/h1/h for a mesh of size hh. A nonlinear step costs one tangent assembly and one factorization per Newton iteration. Newton converges locally and quadratically when the tangent is exact and the predictor is close enough; the number of iterations is a measurement (at most 2 corrections per step to a residual of 10−1210^{-12} for the Duffing test of the reference code, with 50 to 200 steps). The reference code uses dense Gaussian elimination from scimat, so it costs Θ(n3)\Theta(n^3) per step and is meant only for small systems.

Properties

  • Second order exactly when γ=12\gamma = \tfrac12. Any γ>12\gamma > \tfrac12 adds numerical damping and drops the order to 1.
  • No amplitude error for the trapezoidal rule. Both roots have modulus 1 for every Ω\Omega, so the method neither damps nor amplifies a linear mode. The price is that high, unresolved modes of a coarse mesh also keep their full amplitude.
  • Equivalent forms. With β=14\beta = \tfrac14, γ=12\gamma = \tfrac12 the method is the trapezoidal rule applied to the first order system d˙=v\dot d = v, Mv˙=F−Cv−KdM\dot v = F - C v - K d. With β=0\beta = 0, γ=12\gamma = \tfrac12 the displacements satisfy M(dk+1−2dk+dk−1)/dt2+Kdk=FkM(d_{k+1} - 2d_k + d_{k-1})/dt^2 + K d_k = F_k when C=0C = 0, the central difference method, which for M=IM = I is the position form of Verlet integration.
  • Not L-stable. The trapezoidal rule has spectral radius 1 as Ω→∞\Omega \to \infty, so it is A-stable but does not damp stiff modes, in contrast with backward Euler.
  • Symplectic members. For γ=12\gamma = \tfrac12 the method conserves a modified quadratic energy for linear problems. For β=0\beta = 0 it is the symplectic Stormer-Verlet scheme also for nonlinear forces.

Variants and improvements

  • Generalized-alpha method. Evaluate the equation of motion at shifted times inside the step. This keeps second order and gives a chosen amount of damping of the high modes, which Newmark can only get with γ>12\gamma > \tfrac12 and first order.
  • HHT-alpha and WBZ-alpha. The two special cases of the generalized-alpha family, which shift only the stiffness and damping terms (Hilber, Hughes and Taylor) or only the inertia term (Wood, Bossak and Zienkiewicz).
  • d-form and v-form. Solve for dk+1d_{k+1} or vk+1v_{k+1} in place of ak+1a_{k+1}. The three forms give the same values in exact arithmetic. The d-form is common in nonlinear codes, because the tangent acts on the displacement.
  • Explicit predictor-corrector. Use the predictors as the new values and the old acceleration in the force evaluation. This is explicit for any β\beta but has a step limit.
  • Energy-momentum methods. For nonlinear elastodynamics, replace the trapezoidal force average by a discrete gradient so that energy, linear momentum and angular momentum are all conserved (Simo and Tarnow 1992).

When to use it

Use the average acceleration rule for linear and mildly nonlinear structural dynamics where the response of interest is in the low modes and you want no artificial damping, for example the response of a building to an earthquake record. Use the central difference member with a lumped mass matrix for short events such as impact, crash and wave propagation, where the step must be small anyway to resolve the physics. When the mesh has spurious high modes that must be damped, or when a nonlinear problem shows energy growth, use the generalized-alpha method in place of γ>12\gamma > \tfrac12.

Pitfalls

  • Unconditional stability is only for linear problems. For nonlinear problems the energy of the trapezoidal rule can grow without bound for large steps (Hughes 1976, Simo and Tarnow 1992). A large step that works for the linearized problem can fail on the nonlinear one.
  • Damping by γ>12\gamma > \tfrac12. It damps the high modes, but it also loses second order for all modes. The error then grows linearly in dtdt.
  • The explicit limit uses the highest frequency. The limit dt≤2/ωmax⁡dt \le 2/\omega_{\max} comes from the stiffest element of the mesh, not from the physics. One small or very stiff element limits the whole model. The consistent mass matrix gives a larger ωmax⁡\omega_{\max} than the lumped one, so it gives a smaller stable step.
  • Initial acceleration. Line L1 must solve the equation of motion for a0a_0. Setting a0=0a_0 = 0 for a loaded or displaced start gives a first order error that persists.
  • Undamped high modes with the trapezoidal rule. A sudden load excites mesh modes that the trapezoidal rule never damps, so the computed velocities and stresses show noise of high frequency that a finer step does not remove.
  • Period error. The method has no amplitude error but has a phase error. Over many periods the phase error dominates. For the trapezoidal rule the period error is about (ω dt)2/12(\omega\,dt)^2/12, so to keep it below 1 percent, use at least about 20 steps per period of the highest mode of interest.

Implementations

Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.

newmark_beta_method.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/newmark-beta-method/. The authors are listed in prov/DKETH0.jsonl.

"""Newmark-beta time stepping for M a + C v + K d = F(t) and its nonlinear form.

Matrices are lists of rows and vectors are lists, as in scimat. The step is dt.
With Fraction data and Fraction parameters the linear steps are exact.
"""

import math

import scimat


def newmark(M, C, K, F, d0, v0, dt, nsteps, beta=0.25, gamma=0.5):
    """Integrate M a + C v + K d = F(t) with the Newmark-beta method.

    F is a function of t that returns the load vector. Return the lists of
    displacements, velocities and accelerations at t = 0, dt, ..., nsteps dt.
    """
    n = len(d0)
    d, v = list(d0), list(v0)
    rhs0 = scimat.sub(scimat.sub(F(0 * dt), scimat.matvec(C, v)), scimat.matvec(K, d))
    a = scimat.solve(M, rhs0)  # @line L1
    lhs = [[M[i][j] + gamma * dt * C[i][j] + beta * dt * dt * K[i][j] for j in range(n)] for i in range(n)]
    ds, vs, accs = [list(d)], [list(v)], [list(a)]
    for k in range(nsteps):  # @line L2
        t1 = (k + 1) * dt
        dp = [d[i] + dt * v[i] + dt * dt / 2 * (1 - 2 * beta) * a[i] for i in range(n)]  # @line L3
        vp = [v[i] + (1 - gamma) * dt * a[i] for i in range(n)]  # @line L4
        r = scimat.sub(scimat.sub(F(t1), scimat.matvec(C, vp)), scimat.matvec(K, dp))
        a = scimat.solve(lhs, r)  # @line L5
        d = [dp[i] + beta * dt * dt * a[i] for i in range(n)]  # @line L6
        v = [vp[i] + gamma * dt * a[i] for i in range(n)]  # @line L7
        ds.append(d)
        vs.append(v)
        accs.append(a)
    return ds, vs, accs  # @line L8


def newmark_nonlinear(M, C, fint, tangent, F, d0, v0, dt, nsteps, beta=0.25, gamma=0.5, tol=1e-12, maxit=30):
    """Integrate M a + C v + fint(d) = F(t) with Newmark-beta and Newton iterations.

    tangent(d) returns the stiffness matrix d fint / d d. Each step solves the
    residual equation for a_(n+1) by Newton's method with the effective
    tangent M + gamma dt C + beta dt^2 K_T. Return (ds, vs, accs, iterations).
    """
    n = len(d0)
    d, v = list(d0), list(v0)
    a = scimat.solve(M, scimat.sub(scimat.sub(F(0 * dt), scimat.matvec(C, v)), fint(d)))
    ds, vs, accs, its = [list(d)], [list(v)], [list(a)], []
    for k in range(nsteps):
        t1 = (k + 1) * dt
        dp = [d[i] + dt * v[i] + dt * dt / 2 * (1 - 2 * beta) * a[i] for i in range(n)]
        vp = [v[i] + (1 - gamma) * dt * a[i] for i in range(n)]
        anew = list(a)
        for it in range(maxit):
            dn = [dp[i] + beta * dt * dt * anew[i] for i in range(n)]
            vn = [vp[i] + gamma * dt * anew[i] for i in range(n)]
            res = scimat.sub(scimat.add(scimat.add(scimat.matvec(M, anew), scimat.matvec(C, vn)), fint(dn)), F(t1))
            if scimat.norm2(res) <= tol * (1 + scimat.norm2(F(t1))):
                break
            kt = tangent(dn)
            jac = [[M[i][j] + gamma * dt * C[i][j] + beta * dt * dt * kt[i][j] for j in range(n)] for i in range(n)]
            da = scimat.solve(jac, res)
            anew = [anew[i] - da[i] for i in range(n)]
        else:
            raise RuntimeError("Newton iteration did not converge")
        a = anew
        d = [dp[i] + beta * dt * dt * a[i] for i in range(n)]
        v = [vp[i] + gamma * dt * a[i] for i in range(n)]
        ds.append(d)
        vs.append(v)
        accs.append(a)
        its.append(it)
    return ds, vs, accs, its


def amplification_matrix(omega_dt, beta, gamma, xi=0.0):
    """Return the 2 by 2 matrix A with (d, dt v)_(n+1) = A (d, dt v)_n.

    It is for the free single degree of freedom oscillator a + 2 xi omega v +
    omega^2 d = 0 with dt = 1 and omega = omega_dt, so it depends only on
    omega dt, xi and the parameters.
    """
    m, c, k = [[1.0]], [[2 * xi * omega_dt]], [[omega_dt * omega_dt]]
    cols = []
    for d0, v0 in ((1.0, 0.0), (0.0, 1.0)):
        ds, vs, _ = newmark(m, c, k, lambda t: [0.0], [d0], [v0], 1.0, 1, beta, gamma)
        cols.append((ds[1][0], vs[1][0]))
    return [[cols[0][0], cols[1][0]], [cols[0][1], cols[1][1]]]


def spectral_radius_2x2(a):
    """Return the largest modulus of the eigenvalues of a real 2 by 2 matrix."""
    tr = a[0][0] + a[1][1]
    det = a[0][0] * a[1][1] - a[0][1] * a[1][0]
    disc = tr * tr / 4 - det
    if disc >= 0:
        s = math.sqrt(disc)
        return max(abs(tr / 2 + s), abs(tr / 2 - s))
    return math.sqrt(det)


def critical_omega_dt(beta, gamma):
    """Return the stability limit of omega dt for an undamped linear oscillator.

    Valid for gamma >= 1/2. Return math.inf when 2 beta >= gamma (unconditional).
    """
    if 2 * beta >= gamma:
        return math.inf
    return 1 / math.sqrt(gamma / 2 - beta)
scimat.py 227 lines
# SPDX-License-Identifier: MIT
# Shared helper of the scientific-computing pages of https://algorithm.tamnd.com/. The authors are listed in prov/.

"""Small dense linear algebra and error norms for the scientific-computing pages.

A matrix is a list of rows and a vector is a list. Entries may be int,
float or fractions.Fraction. The routines that do not take a square root
(solve, inv, det and the arithmetic) keep Fraction entries exact.
cholesky, cholesky_solve and norm2 take square roots and return floats.

API (fixed; other pages copy this file byte for byte):
    zeros(m, n=None)        m by n zero matrix (n = m if not given)
    eye(n)                  n by n identity
    add(A, B), sub(A, B)    entrywise sum and difference (vectors or matrices)
    scale(c, A)             c times A (vector or matrix)
    matmul(A, B)            matrix product
    matvec(A, x)            matrix times vector
    transpose(A)            transpose
    dot(x, y)               inner product of two vectors
    norm2(x)                Euclidean norm of a vector, as a float
    outer(x, y)             outer product x y^T
    solve(A, b)             Gaussian elimination with partial pivoting;
                            b is a vector or a matrix of right sides
    inv(A)                  inverse of A
    det(A)                  determinant of A
    cholesky(A)             lower triangular L with A = L L^T (floats)
    cholesky_solve(L, b)    solve L L^T x = b for a vector b
    err_l1(e, dx=1)         sum |e_j| dx
    err_l2(e, dx=1)         sqrt(sum e_j^2 dx), as a float
    err_linf(e, dx=1)       max |e_j| (dx is not used)
    observed_order(errors)  [log2(E_i / E_(i+1)) for successive errors]
"""

import math


def zeros(m, n=None):
    """Return an m by n matrix of zeros (n = m if n is None)."""
    if n is None:
        n = m
    return [[0] * n for _ in range(m)]


def eye(n):
    """Return the n by n identity matrix."""
    out = zeros(n, n)
    for i in range(n):
        out[i][i] = 1
    return out


def _is_matrix(a):
    return len(a) > 0 and isinstance(a[0], list)


def add(a, b):
    """Return a + b for two vectors or two matrices of the same shape."""
    if _is_matrix(a):
        return [[x + y for x, y in zip(ra, rb)] for ra, rb in zip(a, b)]
    return [x + y for x, y in zip(a, b)]


def sub(a, b):
    """Return a - b for two vectors or two matrices of the same shape."""
    if _is_matrix(a):
        return [[x - y for x, y in zip(ra, rb)] for ra, rb in zip(a, b)]
    return [x - y for x, y in zip(a, b)]


def scale(c, a):
    """Return c times a, for a vector or a matrix a."""
    if _is_matrix(a):
        return [[c * x for x in row] for row in a]
    return [c * x for x in a]


def transpose(a):
    """Return the transpose of the matrix a."""
    return [list(col) for col in zip(*a)]


def matmul(a, b):
    """Return the matrix product a b."""
    bt = transpose(b)
    return [[sum(x * y for x, y in zip(row, col)) for col in bt] for row in a]


def matvec(a, x):
    """Return the product of the matrix a and the vector x."""
    return [sum(aij * xj for aij, xj in zip(row, x)) for row in a]


def dot(x, y):
    """Return the inner product of the vectors x and y."""
    return sum(a * b for a, b in zip(x, y))


def norm2(x):
    """Return the Euclidean norm of the vector x, as a float."""
    return math.sqrt(float(dot(x, x)))


def outer(x, y):
    """Return the outer product x y^T as a matrix."""
    return [[a * b for b in y] for a in x]


def _eliminate(a, rhs):
    """Reduce a to upper triangular form with partial pivoting.

    a is a copy of the square matrix, rhs a list of right side rows (one
    row per row of a) that receives the same row operations. Return the
    number of row swaps. Raise ValueError if a pivot is zero.
    """
    n = len(a)
    swaps = 0
    for k in range(n):
        p = max(range(k, n), key=lambda i: abs(a[i][k]))
        if a[p][k] == 0:
            raise ValueError("matrix is singular")
        if p != k:
            a[k], a[p] = a[p], a[k]
            rhs[k], rhs[p] = rhs[p], rhs[k]
            swaps += 1
        for i in range(k + 1, n):
            f = a[i][k] / a[k][k]
            if f == 0:
                continue
            for j in range(k, n):
                a[i][j] -= f * a[k][j]
            for j in range(len(rhs[i])):
                rhs[i][j] -= f * rhs[k][j]
    return swaps


def solve(a, b):
    """Solve a x = b by Gaussian elimination with partial pivoting.

    b is a vector (the result is a vector) or a matrix with one column per
    right side (the result is a matrix). Fraction entries stay exact.
    Raise ValueError if a is singular.
    """
    n = len(a)
    work = [list(row) for row in a]
    vector = not _is_matrix(b)
    rhs = [[v] for v in b] if vector else [list(row) for row in b]
    _eliminate(work, rhs)
    m = len(rhs[0]) if n else 0
    x = [[0] * m for _ in range(n)]
    for i in range(n - 1, -1, -1):
        for j in range(m):
            s = rhs[i][j]
            for k in range(i + 1, n):
                s -= work[i][k] * x[k][j]
            x[i][j] = s / work[i][i]
    if vector:
        return [row[0] for row in x]
    return x


def inv(a):
    """Return the inverse of the square matrix a."""
    return solve(a, eye(len(a)))


def det(a):
    """Return the determinant of the square matrix a (0 if singular)."""
    work = [list(row) for row in a]
    rhs = [[] for _ in a]
    try:
        swaps = _eliminate(work, rhs)
    except ValueError:
        return 0
    d = -1 if swaps % 2 else 1
    for i in range(len(a)):
        d *= work[i][i]
    return d


def cholesky(a):
    """Return the lower triangular L with a = L L^T, in floats.

    Raise ValueError if a is not symmetric positive definite.
    """
    n = len(a)
    low = [[0.0] * n for _ in range(n)]
    for j in range(n):
        s = float(a[j][j]) - sum(low[j][k] ** 2 for k in range(j))
        if s <= 0.0:
            raise ValueError("matrix is not positive definite")
        low[j][j] = math.sqrt(s)
        for i in range(j + 1, n):
            t = float(a[i][j]) - sum(low[i][k] * low[j][k] for k in range(j))
            low[i][j] = t / low[j][j]
    return low


def cholesky_solve(low, b):
    """Solve L L^T x = b, with L from cholesky, for a vector b."""
    n = len(low)
    y = [0.0] * n
    for i in range(n):
        y[i] = (float(b[i]) - sum(low[i][k] * y[k] for k in range(i))) / low[i][i]
    x = [0.0] * n
    for i in range(n - 1, -1, -1):
        x[i] = (y[i] - sum(low[k][i] * x[k] for k in range(i + 1, n))) / low[i][i]
    return x


def err_l1(e, dx=1):
    """Return the discrete L1 norm sum |e_j| dx."""
    return sum(abs(v) for v in e) * dx


def err_l2(e, dx=1):
    """Return the discrete L2 norm sqrt(sum e_j^2 dx), as a float."""
    return math.sqrt(float(sum(v * v for v in e) * dx))


def err_linf(e, dx=1):
    """Return the max norm max |e_j|. dx is accepted for a uniform call and not used."""
    return max(abs(v) for v in e)


def observed_order(errors):
    """Return [log2(E_i / E_(i+1))] for a list of errors on meshes h, h/2, h/4, ..."""
    return [math.log2(float(errors[i]) / float(errors[i + 1])) for i in range(len(errors) - 1)]
test_newmark_beta_method.py 181 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/newmark-beta-method/. The authors are listed in prov/DKETH0.jsonl.

import math
from fractions import Fraction as Fr

import scimat
from newmark_beta_method import (
    amplification_matrix,
    critical_omega_dt,
    newmark,
    newmark_nonlinear,
    spectral_radius_2x2,
)


def free_sdof(omega, dt, nsteps, beta, gamma, d0=1.0, v0=0.0):
    return newmark([[1.0]], [[0.0]], [[omega * omega]], lambda t: [0.0], [d0], [v0], dt, nsteps, beta, gamma)


def test_first_step_exact_fractions():
    # m = k = 1, d0 = 1, v0 = 0, dt = 1/2, average acceleration: d1 = 15/17, v1 = -8/17.
    ds, vs, accs = newmark([[Fr(1)]], [[Fr(0)]], [[Fr(1)]], lambda t: [Fr(0)], [Fr(1)], [Fr(0)], Fr(1, 2), 1, Fr(1, 4), Fr(1, 2))
    assert accs[0] == [Fr(-1)]
    assert ds[1] == [Fr(15, 17)]
    assert vs[1] == [Fr(-8, 17)]
    assert accs[1] == [Fr(-15, 17)]


def test_average_acceleration_conserves_energy_exactly():
    # For an undamped linear system, (1/2)(v.Mv + d.Kd) is the same at every step, for any dt.
    M = [[Fr(2), Fr(0)], [Fr(0), Fr(1)]]
    K = [[Fr(6), Fr(-2)], [Fr(-2), Fr(4)]]
    C = [[Fr(0)] * 2 for _ in range(2)]
    ds, vs, _ = newmark(M, C, K, lambda t: [Fr(0), Fr(0)], [Fr(1), Fr(0)], [Fr(0), Fr(1)], Fr(3), 12, Fr(1, 4), Fr(1, 2))

    def energy(d, v):
        return (scimat.dot(v, scimat.matvec(M, v)) + scimat.dot(d, scimat.matvec(K, d))) / 2

    e0 = energy(ds[0], vs[0])
    assert all(energy(d, v) == e0 for d, v in zip(ds, vs))


def test_trapezoidal_phase_is_two_arctan():
    # The eigenvalues of the average acceleration step have modulus 1 and argument 2 atan(omega dt / 2).
    for w in (0.1, 0.7, 2.0, 10.0, 100.0):
        a = amplification_matrix(w, 0.25, 0.5)
        det = a[0][0] * a[1][1] - a[0][1] * a[1][0]
        tr = a[0][0] + a[1][1]
        assert abs(det - 1) < 1e-12
        assert abs(tr / 2 - math.cos(2 * math.atan(w / 2))) < 1e-12


def errors_at_T(beta, gamma, steps_list, T=2.0):
    errs = []
    for ns in steps_list:
        ds, vs, _ = free_sdof(1.0, T / ns, ns, beta, gamma)
        errs.append(math.hypot(ds[-1][0] - math.cos(T), vs[-1][0] + math.sin(T)))
    return errs


def test_second_order_for_gamma_half():
    for beta in (0.25, 1 / 6, 1 / 12, 0.0):
        p = scimat.observed_order(errors_at_T(beta, 0.5, [40, 80, 160, 320]))
        assert all(abs(q - 2) < 0.2 for q in p), (beta, p)


def test_first_order_for_gamma_above_half():
    gamma = 0.6
    beta = (gamma + 0.5) ** 2 / 4
    p = scimat.observed_order(errors_at_T(beta, gamma, [80, 160, 320, 640]))
    assert all(abs(q - 1) < 0.2 for q in p), p


def test_central_difference_limit_two():
    assert critical_omega_dt(0.0, 0.5) == 2
    assert spectral_radius_2x2(amplification_matrix(1.99, 0.0, 0.5)) <= 1 + 1e-12
    assert spectral_radius_2x2(amplification_matrix(2.01, 0.0, 0.5)) > 1.05


def test_linear_acceleration_limit():
    wc = critical_omega_dt(1 / 6, 0.5)
    assert abs(wc - math.sqrt(12)) < 1e-12
    assert spectral_radius_2x2(amplification_matrix(wc * 0.999, 1 / 6, 0.5)) <= 1 + 1e-9
    assert spectral_radius_2x2(amplification_matrix(wc * 1.01, 1 / 6, 0.5)) > 1


def test_unconditional_stability_region():
    for gamma in (0.5, 0.55, 0.7, 1.0):
        beta = gamma / 2
        for w in (0.5, 5.0, 50.0, 5e3):
            # near omega dt = infinity the step matrix is close to a Jordan block, so allow rounding
            assert spectral_radius_2x2(amplification_matrix(w, beta, gamma)) <= 1 + 1e-9
    assert critical_omega_dt(0.25, 0.5) == math.inf


def test_dissipation_with_gamma_above_half():
    gamma = 0.6
    beta = (gamma + 0.5) ** 2 / 4
    rho = spectral_radius_2x2(amplification_matrix(1e6, beta, gamma))
    assert rho < 0.9
    assert spectral_radius_2x2(amplification_matrix(1e6, 0.25, 0.5)) > 1 - 1e-9


def test_two_dof_against_modal_solution():
    # M = I, K = [[2, -1], [-1, 2]]: modes (1, 1) with omega = 1 and (1, -1) with omega = sqrt(3).
    K = [[2.0, -1.0], [-1.0, 2.0]]
    I2 = [[1.0, 0.0], [0.0, 1.0]]
    Z = [[0.0, 0.0], [0.0, 0.0]]
    T = 3.0
    w2 = math.sqrt(3.0)

    def exact(t):
        q1, q2 = math.cos(t), 0.5 * math.cos(w2 * t)
        return [q1 + q2, q1 - q2]

    errs = []
    for ns in (60, 120, 240):
        ds, _, _ = newmark(I2, Z, K, lambda t: [0.0, 0.0], exact(0.0), [0.0, 0.0], T / ns, ns)
        errs.append(max(abs(x - y) for x, y in zip(ds[-1], exact(T))))
    p = scimat.observed_order(errs)
    assert all(abs(q - 2) < 0.2 for q in p), p


def test_forced_damped_oscillator():
    # a + 0.2 v + d = cos(t) from rest; compare with the exact solution.
    xi, T = 0.1, 4.0

    def exact(t):
        # particular solution (0.2 sin t) / 0.04 = 5 sin t; homogeneous part fixes d(0) = 0, v(0) = 0.
        wd = math.sqrt(1 - xi * xi)
        A = 0.0
        B = -5.0 / wd
        return 5 * math.sin(t) + math.exp(-xi * t) * (A * math.cos(wd * t) + B * math.sin(wd * t))

    errs = []
    for ns in (50, 100, 200):
        ds, _, _ = newmark([[1.0]], [[0.2]], [[1.0]], lambda t: [math.cos(t)], [0.0], [0.0], T / ns, ns)
        errs.append(abs(ds[-1][0] - exact(T)))
    p = scimat.observed_order(errs)
    assert all(abs(q - 2) < 0.2 for q in p), p


def rk4_duffing(d, v, T, ns):
    h = T / ns

    def f(x, y):
        return y, -x - x ** 3

    for _ in range(ns):
        k1 = f(d, v)
        k2 = f(d + h / 2 * k1[0], v + h / 2 * k1[1])
        k3 = f(d + h / 2 * k2[0], v + h / 2 * k2[1])
        k4 = f(d + h * k3[0], v + h * k3[1])
        d += h / 6 * (k1[0] + 2 * k2[0] + 2 * k3[0] + k4[0])
        v += h / 6 * (k1[1] + 2 * k2[1] + 2 * k3[1] + k4[1])
    return d, v


def test_nonlinear_duffing_against_rk4():
    # a + d + d^3 = 0, d(0) = 1, v(0) = 0; RK4 with 4000 steps is the reference.
    T = 3.0
    dref, _ = rk4_duffing(1.0, 0.0, T, 4000)
    errs = []
    for ns in (50, 100, 200):
        ds, _, _, its = newmark_nonlinear(
            [[1.0]], [[0.0]], lambda d: [d[0] + d[0] ** 3], lambda d: [[1 + 3 * d[0] ** 2]], lambda t: [0.0], [1.0], [0.0], T / ns, ns
        )
        assert max(its) <= 8
        errs.append(abs(ds[-1][0] - dref))
    p = scimat.observed_order(errs)
    assert all(abs(q - 2) < 0.2 for q in p), p


def test_nonlinear_energy_is_not_exact():
    # The trapezoidal rule does not conserve the energy of a nonlinear spring exactly.
    ds, vs, _, _ = newmark_nonlinear(
        [[1.0]], [[0.0]], lambda d: [d[0] + d[0] ** 3], lambda d: [[1 + 3 * d[0] ** 2]], lambda t: [0.0], [1.0], [0.0], 0.5, 40
    )
    e = [v[0] ** 2 / 2 + d[0] ** 2 / 2 + d[0] ** 4 / 4 for d, v in zip(ds, vs)]
    assert max(abs(x - e[0]) for x in e) > 1e-4

History

Nathan Newmark published the method in 1959, in a paper on computing the response of structures to blast and earthquake loads, and gave the two parameters, the choice γ=12\gamma = \tfrac12 for no artificial damping and the stability limits for the linear oscillator. The finite element community made it the standard integrator for structural dynamics in the 1970s. Hughes showed in 1976 that the energy argument for the average acceleration rule fails for nonlinear problems. Hilber, Hughes and Taylor in 1977 and Chung and Hulbert in 1993 added controlled numerical damping without the loss of order. Simo and Tarnow in 1992 gave schemes that conserve energy and momentum for nonlinear elastodynamics.

References

  1. N. M. Newmark, A method of computation for structural dynamics, Journal of the Engineering Mechanics Division (ASCE) 85(3) (1959) 67-94, the two-parameter step for displacement and velocity, with the stability limits
  2. Thomas J. R. Hughes, The Finite Element Method, Linear Static and Dynamic Finite Element Analysis, Dover, 2000, Chapter 9 (the Newmark method, its stability and accuracy analysis, numerical dissipation and dispersion)
  3. T. J. R. Hughes, Stability, convergence and growth and decay of energy of the average acceleration method in nonlinear structural dynamics, Computers and Structures 6(4-5) (1976) 313-324, energy of the average acceleration method for nonlinear problems
  4. J. C. Simo and N. Tarnow, The discrete energy-momentum method. Conserving algorithms for nonlinear elastodynamics, Zeitschrift fur angewandte Mathematik und Physik 43(5) (1992) 757-792, loss of stability of the trapezoidal rule in nonlinear elastodynamics, and energy-momentum conserving schemes

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH1 sketch
  • a full proof of correctness
  • a full proof of time.worst
  • a full proof of space.extra
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • a scene preset for the example