Skip to content

Newmark beta method - #2187

Merged
oscardssmith merged 40 commits into
SciML:masterfrom
termi-official:do/newmark-beta
Nov 26, 2025
Merged

oscardssmith merged 40 commits into
SciML:masterfrom
termi-official:do/newmark-beta

Conversation

@termi-official

@termi-official termi-official commented May 10, 2024 •

Copy link
Copy Markdown
Contributor

Prototype to resolve #411 .

Checklist

  • Appropriate tests were added
  • Any code changes were done in a way that does not break public API
  • All documentation related to code changes were updated
  • The new code follows the
    contributor guidelines, in particular the SciML Style Guide and
    COLPRAC.
  • Any new documentation only uses public API

Comment thread src/alg_utils.jl Resolved
@termi-official

termi-official commented May 15, 2024 •

Copy link
Copy Markdown
Contributor Author

The last remaining problem is a bit tricky. For a given DynamicalODEFunction Newmark needs to solve for a "future accelleration" by solving the problem $Δtₙ f₁(innertmp₂ + z β Δtₙ², innertmp₁ + z γ Δtₙ,t) = z$ for $z$ with given $innertmp, \alpha,\beta$.

The first issue I run into is passing down the analytical Jacobian:

using OrdinaryDiffEq
function f1_harmonic(dv, v, u, p, t)
    dv .= -u
end
function f2_harmonic(du, v, u, p, t)
    du .= v
end
harmonic_jac_f1(J, v, u, p, t) = J[1,1] = -1.0
ff_harmonic = DynamicalODEFunction(f1_harmonic, f2_harmonic; jac = harmonic_jac_f1)
DiffEqBase.has_jac(ff_harmonic) # false
DiffEqBase.has_jac(ff_harmonic.f1) # false
ff_f1 = ODEFunction{false}(ODEFunction{false}(f1_harmonic); jac=harmonic_jac_f1!)
ff_harmonic = DynamicalODEFunction(ff_f1, f2_harmonic; jac = harmonic_jac_f1)
DiffEqBase.has_jac(ff_harmonic) # false
DiffEqBase.has_jac(ff_harmonic.f1) # false 

The second problem is getting the AD on track. My current strategy is to pass a helper type ArrayPartitionNLSolveHelper around, which constructs an ArrayPartition when multiplied with a Vector. The idea is to provide an interface for inplace evaluation of f.f1 (DynamicalODEFunction) which is compatible with the evaluation form required by the internal nonlinear solver, such that we can call $f₁(innertmp + z \alpha,t)$, where $\alpha$ is the mentioned ArrayPartitionNLSolveHelper. Should we head down this road or is there possibly a better design?

@termi-official

Copy link
Copy Markdown
Contributor Author

I think I can call this now a first prototype. I am not very sure why it manages to converge to the correct solution, but I assume the residual is just wrong up to scaling. Another problem is that the Jacobian is per construction wrong. The derivative of $f_1$ w.r.t. the input parameter is a rectangular matrix. For Newmark we assume that the input space is partitioned into 2 equally sized parts (velocity $v$ and displacement $u$) and that $f_2$ is just $du = v$. The first assumption tells us that the Jacobian is blocked with 2 blocks. For the inner nonlinear solve in Newmark we now obtain the system matrix for the Newton as $\partial_z f_1(u+\gamma_1 z,v+\gamma_2 z) = \gamma_1 J_{1,1}(u+\gamma_1 z,v+\gamma_2 z) + \gamma_2 J_{1,2}(u+\gamma_1 z,v+\gamma_2 z)$ where $J_{i,j}$ is the i,j-th block of the Jacobian of $f$.

However, this seems to break a few assumptions taken in the code. Hence I could need some feedback on how to proceed before putting time into a design which we do not want in the library. I already think that ArrayPartitionNLSolveHelper is not great, but I cannot figure out a better design by myself either.

Furthermore I cannot figure out how to integrate the W-transformation here. Any pointers?

Comment thread src/integrators/integrator_utils.jl Resolved
Comment thread src/misc_utils.jl Resolved
Comment thread src/nlsolve/newton.jl Resolved
Comment thread src/nlsolve/newton.jl Resolved
Comment thread src/nlsolve/type.jl Resolved

@termi-official termi-official left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback. I have updated the implementation to master and made the tests for linear problems (harmonic and damped oscillator) pass. However, I had to manually unroll the implementation. I left some inline comments here on Github on the code sections where I am stuck (and on which I will resume working after fixing the most pressing issues).

Comment on lines +69 to +82
uₙ₊₁ = uₙ + dt * vₙ + dt^2/2 * ((1-2β)*aₙ + 2β*aₙ₊₁)
vₙ₊₁ = vₙ + dt * ((1-γ)*aₙ + γ*aₙ₊₁)
# Compute residual
f.f1(atmp, vₙ₊₁, uₙ₊₁, p, t)
integrator.stats.nf += 1
residual = M*(aₙ₊₁ - atmp)
# Compute jacobian
f.jac(J, vₙ₊₁, uₙ₊₁, (γ*dt, β*dt*dt), p, t)
# Solve for increment
Δaₙ₊₁ = (M-J) \ residual
aₙ₊₁ .-= Δaₙ₊₁ # Looks like I messed up the signs somewhere :')
increment_norm = integrator.opts.internalnorm(Δaₙ₊₁, t)
increment_norm < 1e-4 && break
i == 10 && error("Newton diverged. ||Δaₙ₊₁||=$increment_norm")

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oscardssmith I think I need some help here regarding the integration with OrdinaryDiffEqNonlinearSolve.jl .

A critical missing piece is an interface for the evaluation of the Jacobian in a suitable form (especially via ad). For now I have just bypassed this with a custom jacobian function, e.g. for the harmonic oscillator in the test

    function harmonic_jac(J, v, u, weights, p, t)
        J[1,1] = weights[1] * (0.0) + weights[2] * (-1.0)
        J[1,2] = weights[1] * (0.0) + weights[2] * ( 0.0)
        J[2,2] = weights[1] * (0.0) + weights[2] * (-1.0)
        J[2,1] = weights[1] * (0.0) + weights[2] * ( 0.0)
    end

because many implicit second order ODE methods requires that J = Δtₙ²β ∂fᵤ + Δtₙγ ∂fᵥ . See above for a "full" derivation.

Comment thread src/misc_utils.jl Resolved
Comment thread src/OrdinaryDiffEq.jl Resolved
Comment on lines +35 to +36
vₙ + dt * ((1 - γ) * aₙ + γ * aₙ₊₁),
uₙ + dt * vₙ + dt^2 / 2 * ((1 - 2β) * aₙ + 2β * aₙ₊₁)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ad some @.. to reduce allocations?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This catches fire. DimensionMismatch: array could not be broadcast to match destination

@oscardssmith

Copy link
Copy Markdown
Member

@ChrisRackauckas any ideas on naming? Denis doesn't love OrdinaryDiffEqNewmark since there could be more solvers of this rough form that aren't Nemark methods added in the future, but we don't have better ideas on naming.

@ChrisRackauckas

Copy link
Copy Markdown
Member

Generally Second Order multistep implicit methods?

@termi-official

Copy link
Copy Markdown
Contributor Author

Typically these methods are single step methods. So OrdinaryDiffEqSecondOrderImplicit maybe and the BDF type methods go into a different package?

@oscardssmith

Copy link
Copy Markdown
Member

The annoying thing about OrdinaryDiffEqSecondOrderImplicit is that It's unclear from the name whether Second Order refers to the solver or the ODE. It's a shame that math peeps are so bad at naming things.

@termi-official

Copy link
Copy Markdown
Contributor Author

This is good to go for now (minus the package name). We are missing an interface to query the manual Jacobian in SciML for further optimizations.

@termi-official

Copy link
Copy Markdown
Contributor Author

Any suggestions or comments for the package name? I do not have a strong opinion here. I have changed it now to OrdinaryDiffEqSecondOrderImplicit but I can also just change it back to OrdinaryDiffEqNewmark.

name = "OrdinaryDiffEqNewmark"
uuid = "d204908a-63b9-11ef-18f5-cfec7123a93b"
authors = ["Dennis Ogiermann <[email protected]>"]
version = "0.0.1"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
version = "0.0.1"
version = "0.0.1"

What is the versioning policy for SciML repos?

Comment thread .github/workflows/CI.yml
- OrdinaryDiffEqLinear
- OrdinaryDiffEqLowOrderRK
- OrdinaryDiffEqLowStorageRK
- OrdinaryDiffEqNewmark

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CI does not pick up the correct OrdinaryDiffEqCore version for the new package. Is there an easy fix?

@oscardssmith
oscardssmith merged commit 1fbbbbe into SciML:master Nov 26, 2025
@termi-official
termi-official deleted the do/newmark-beta branch November 26, 2025 20:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Numark-beta method

4 participants