Skip to content

Commit cba1364

Browse files
Fixes
1 parent 1f0ab42 commit cba1364

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

benchmarks/DynamicalODE/Henon-Heiles_energy_conservation_benchmark.jmd

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,22 @@ In order to separate these two, we will use `iip` for the in-place names and `oo
1919
using OrdinaryDiffEq, Plots, DiffEqCallbacks
2020
using SciMLBenchmarks
2121
using TaylorIntegration, LinearAlgebra, StaticArrays
22+
23+
# Set plotting formats
2224
gr(fmt=:png)
2325
default(fmt=:png)
2426

2527
T(p) = 1//2 * norm(p)^2
26-
V(q) = 1//2 * (q[1]^2 + q[2]^2 + 2q[1]^2 * q[2]- 2//3 * q[2]^3)
27-
H(p,q, params) = T(p) + V(q)
28+
V(q) = 1//2 * (q[1]^2 + q[2]^2 + 2 * q[1]^2 * q[2] - 2//3 * q[2]^3)
29+
H(p, q, params) = T(p) + V(q)
2830

29-
function iip_dq(dq,p,q,params,t)
31+
function iip_dq(dq, p, q, params, t)
3032
dq[1] = p[1]
3133
dq[2] = p[2]
3234
end
3335

34-
function iip_dp(dp,p,q,params,t)
35-
dp[1] = -q[1] * (1 + 2q[2])
36+
function iip_dp(dp, p, q, params, t)
37+
dp[1] = -q[1] * (1 + 2 * q[2])
3638
dp[2] = -q[2] - (q[1]^2 - q[2]^2)
3739
end
3840

@@ -44,7 +46,7 @@ function oop_dq(p, q, params, t)
4446
end
4547

4648
function oop_dp(p, q, params, t)
47-
dp1 = -q[1] * (1 + 2q[2])
49+
dp1 = -q[1] * (1 + 2 * q[2])
4850
dp2 = -q[2] - (q[1]^2 - q[2]^2)
4951
@SVector [dp1, dp2]
5052
end
@@ -56,19 +58,33 @@ function hamilton(du, u, p, t)
5658
dq, q = @views u[3:4], du[3:4]
5759
dp, p = @views u[1:2], du[1:2]
5860

59-
dp[1] = -q[1] * (1 + 2q[2])
61+
dp[1] = -q[1] * (1 + 2 * q[2])
6062
dp[2] = -q[2] - (q[1]^2 - q[2]^2)
6163
dq .= p
6264

6365
return nothing
6466
end
6567

68+
function manifold_jacobian!(J, u)
69+
p, q = u[1:2], u[3:4]
70+
71+
J[1, 1] = p[1]
72+
J[1, 2] = p[2]
73+
74+
J[1, 3] = q[1] + 4 * q[1] * q[2]
75+
J[1, 4] = q[2] + 2 * q[1]^2 - 2 * q[2]^2
76+
77+
J[2:4, :] .= 0
78+
79+
nothing
80+
end
81+
6682
function g(resid, u, p)
6783
resid[1] = H([u[1], u[2]], [u[3], u[4]], nothing) - E
6884
resid[2:4] .= 0
6985
end
7086

71-
const cb = ManifoldProjection(g, nlopts=Dict(:ftol => 1e-13), autodiff = AutoForwardDiff())
87+
const cb = ManifoldProjection(g, nlopts=Dict(:ftol => 1e-13), manifold_jacobian=manifold_jacobian)
7288

7389
const E = H(iip_p0, iip_q0, nothing)
7490
```

benchmarks/DynamicalODE/Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,4 @@ Plots = "2.0.0, 1"
2222
SciMLBenchmarks = "0.1.3"
2323
StaticArrays = "1.9.7"
2424
Statistics = "1.11.1"
25-
TaylorIntegration = "0.16.1"
25+
TaylorIntegration = "0.16.1"

benchmarks/DynamicalODE/Quadrupole_boson_Hamiltonian_energy_conservation_benchmark.jmd

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,23 @@ function iip_dq(dq, p, q, params, t)
2929
dq[2] = A * p[2]
3030
end
3131

32+
function manifold_jacobian!(J, u)
33+
p₁, p₂ = u[1], u[2]
34+
q₁, q₂ = u[3], u[4]
35+
36+
q₁_sq = q₁^2
37+
q₂_sq = q₂^2
38+
q_sum_sq = q₁_sq + q₂_sq
39+
40+
J[1,1] = A * p₁
41+
J[1,2] = A * p₂
42+
J[1,3] = A * q₁ + (B/√2) * (3q₂_sq - 3q₁_sq) + D * q₁ * q_sum_sq
43+
J[1,4] = A * q₂ + (B/√2) * 6q₁q₂ + D * q₂ * q_sum_sq
44+
J[2:4,:] .= 0
45+
46+
nothing
47+
end
48+
3249
function iip_dp(dp, p, q, params, t)
3350
dp[1] = -A * q[1] - 3 * B / sqrt(2) * (q[2]^2 - q[1]^2) - D * q[1] * (q[1]^2 + q[2]^2)
3451
dp[2] = -q[2] * (A + 3 * sqrt(2) * B * q[1] + D * (q[1]^2 + q[2]^2))
@@ -67,7 +84,7 @@ function g(resid, u, p)
6784
end
6885

6986
const E = H(iip_p0, iip_q0, nothing)
70-
const cb = ManifoldProjection(g, nlopts=Dict(:ftol=>1e-13), autodiff = AutoForwardDiff());
87+
const cb = ManifoldProjection(g, nlopts=Dict(:ftol=>1e-13), manifold_jacobian! = manifold_jacobian!);
7188
```
7289

7390
For the comparison we will use the following function

0 commit comments

Comments
 (0)