From 86c26031d0a2f6ac477dedab716224ee17d4e87b Mon Sep 17 00:00:00 2001 From: SciML Bot Date: Thu, 31 Jul 2025 17:17:26 -0400 Subject: [PATCH] Apply JuliaFormatter to fix code formatting MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Applied JuliaFormatter with SciML style guide - Formatted 5 files 🤖 Generated by OrgMaintenanceScripts.jl --- .JuliaFormatter.toml | 3 + LICENSE.md | 1 - README.md | 274 +++++++++++++++++++++---------------------- src/MATLABDiffEq.jl | 32 +++-- test/runtests.jl | 16 ++- 5 files changed, 156 insertions(+), 170 deletions(-) create mode 100644 .JuliaFormatter.toml diff --git a/.JuliaFormatter.toml b/.JuliaFormatter.toml new file mode 100644 index 0000000..3494a9f --- /dev/null +++ b/.JuliaFormatter.toml @@ -0,0 +1,3 @@ +style = "sciml" +format_markdown = true +format_docstrings = true diff --git a/LICENSE.md b/LICENSE.md index a26dbb8..d19dce7 100644 --- a/LICENSE.md +++ b/LICENSE.md @@ -19,4 +19,3 @@ The MATLABDiffEq.jl package is licensed under the MIT "Expat" License: > LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, > OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE > SOFTWARE. -> diff --git a/README.md b/README.md index 9a658e0..84a6490 100644 --- a/README.md +++ b/README.md @@ -37,24 +37,24 @@ but are not exported. Thus to use `ode45`, you would specify the algorithm as using MATLABDiffEq, ParameterizedFunctions f = @ode_def LotkaVolterra begin - dx = 1.5x - x*y - dy = -3y + x*y + dx = 1.5x - x*y + dy = -3y + x*y end -tspan = (0.0,10.0) -u0 = [1.0,1.0] -prob = ODEProblem(f,u0,tspan) -sol = solve(prob,MATLABDiffEq.ode45()) +tspan = (0.0, 10.0) +u0 = [1.0, 1.0] +prob = ODEProblem(f, u0, tspan) +sol = solve(prob, MATLABDiffEq.ode45()) -function lorenz(du,u,p,t) - du[1] = 10.0(u[2]-u[1]) - du[2] = u[1]*(28.0-u[3]) - u[2] - du[3] = u[1]*u[2] - (8/3)*u[3] +function lorenz(du, u, p, t) + du[1] = 10.0(u[2]-u[1]) + du[2] = u[1]*(28.0-u[3]) - u[2] + du[3] = u[1]*u[2] - (8/3)*u[3] end -u0 = [1.0;0.0;0.0] -tspan = (0.0,100.0) -prob = ODEProblem(lorenz,u0,tspan) -sol = solve(prob,MATLABDiffEq.ode45()) +u0 = [1.0; 0.0; 0.0] +tspan = (0.0, 100.0) +prob = ODEProblem(lorenz, u0, tspan) +sol = solve(prob, MATLABDiffEq.ode45()) ``` ## Measuring Overhead @@ -82,11 +82,11 @@ Generally, for long enough problems the overhead is minimal. Example: ```julia using DiffEqBase, ParameterizedFunctions, MATLABDiffEq f = @ode_def_bare RigidBodyBench begin - dy1 = -2*y2*y3 - dy2 = 1.25*y1*y3 - dy3 = -0.5*y1*y2 + 0.25*sin(t)^2 + dy1 = -2*y2*y3 + dy2 = 1.25*y1*y3 + dy3 = -0.5*y1*y2 + 0.25*sin(t)^2 end -prob = ODEProblem(f,[1.0;0.0;0.9],(0.0,100.0)) +prob = ODEProblem(f, [1.0; 0.0; 0.9], (0.0, 100.0)) alg = MATLABDiffEq.ode45() algstr = string(typeof(alg).name.name) ``` @@ -94,16 +94,16 @@ algstr = string(typeof(alg).name.name) For this, we get the following: ```julia -julia> @time sol = solve(prob,alg); +julia> @time sol = solve(prob, alg); 0.063918 seconds (38.84 k allocations: 1.556 MB) -julia> @time sol = solve(prob,alg); +julia> @time sol = solve(prob, alg); 0.062600 seconds (38.84 k allocations: 1.556 MB) -julia> @time sol = solve(prob,alg); +julia> @time sol = solve(prob, alg); 0.061438 seconds (38.84 k allocations: 1.556 MB) -julia> @time sol = solve(prob,alg); +julia> @time sol = solve(prob, alg); 0.065460 seconds (38.84 k allocations: 1.556 MB) julia> @time MATLABDiffEq.eval_string("[t,u] = $(algstr)(diffeqf,tspan,u0,options);") @@ -131,14 +131,14 @@ MATLAB solvers do outperform that of Python and R. ```julia f = @ode_def_bare LotkaVolterra begin - dx = a*x - b*x*y - dy = -c*y + d*x*y + dx = a*x - b*x*y + dy = -c*y + d*x*y end a b c d -p = [1.5,1,3,1] -tspan = (0.0,10.0) -u0 = [1.0,1.0] -prob = ODEProblem(f,u0,tspan,p) -sol = solve(prob,Vern7(),abstol=1/10^14,reltol=1/10^14) +p = [1.5, 1, 3, 1] +tspan = (0.0, 10.0) +u0 = [1.0, 1.0] +prob = ODEProblem(f, u0, tspan, p) +sol = solve(prob, Vern7(), abstol = 1/10^14, reltol = 1/10^14) test_sol = TestSolution(sol) setups = [Dict(:alg=>DP5()) @@ -152,32 +152,29 @@ setups = [Dict(:alg=>DP5()) Dict(:alg=>SciPyDiffEq.odeint()) Dict(:alg=>deSolveDiffEq.lsoda()) Dict(:alg=>deSolveDiffEq.ode45()) - Dict(:alg=>CVODE_Adams()) - ] - -names = [ - "Julia: DP5" - "Hairer: dopri5" - "Julia: Tsit5" - "Julia: Vern7" - "MATLAB: ode45" - "MATLAB: ode113" - "SciPy: RK45" - "SciPy: LSODA" - "SciPy: odeint" - "deSolve: lsoda" - "deSolve: ode45" - "Sundials: Adams" - ] + Dict(:alg=>CVODE_Adams())] + +names = ["Julia: DP5" + "Hairer: dopri5" + "Julia: Tsit5" + "Julia: Vern7" + "MATLAB: ode45" + "MATLAB: ode113" + "SciPy: RK45" + "SciPy: LSODA" + "SciPy: odeint" + "deSolve: lsoda" + "deSolve: ode45" + "Sundials: Adams"] abstols = 1.0 ./ 10.0 .^ (6:13) reltols = 1.0 ./ 10.0 .^ (3:10) -wp = WorkPrecisionSet(prob,abstols,reltols,setups; - names = names, - appxsol=test_sol,dense=false, - save_everystep=false,numruns=100,maxiters=10000000, - timeseries_errors=false,verbose=false) -plot(wp,title="Non-stiff 1: Lotka-Volterra") +wp = WorkPrecisionSet(prob, abstols, reltols, setups; + names = names, + appxsol = test_sol, dense = false, + save_everystep = false, numruns = 100, maxiters = 10000000, + timeseries_errors = false, verbose = false) +plot(wp, title = "Non-stiff 1: Lotka-Volterra") ``` ![](https://user-images.githubusercontent.com/1814174/71537082-ef42ac00-28e4-11ea-9acc-67dfd9990221.png) @@ -186,12 +183,12 @@ plot(wp,title="Non-stiff 1: Lotka-Volterra") ```julia f = @ode_def_bare RigidBodyBench begin - dy1 = -2*y2*y3 - dy2 = 1.25*y1*y3 - dy3 = -0.5*y1*y2 + 0.25*sin(t)^2 + dy1 = -2*y2*y3 + dy2 = 1.25*y1*y3 + dy3 = -0.5*y1*y2 + 0.25*sin(t)^2 end -prob = ODEProblem(f,[1.0;0.0;0.9],(0.0,100.0)) -sol = solve(prob,Vern7(),abstol=1/10^14,reltol=1/10^14) +prob = ODEProblem(f, [1.0; 0.0; 0.9], (0.0, 100.0)) +sol = solve(prob, Vern7(), abstol = 1/10^14, reltol = 1/10^14) test_sol = TestSolution(sol) setups = [Dict(:alg=>DP5()) @@ -205,32 +202,29 @@ setups = [Dict(:alg=>DP5()) Dict(:alg=>SciPyDiffEq.odeint()) Dict(:alg=>deSolveDiffEq.lsoda()) Dict(:alg=>deSolveDiffEq.ode45()) - Dict(:alg=>CVODE_Adams()) - ] - -names = [ - "Julia: DP5" - "Hairer: dopri5" - "Julia: Tsit5" - "Julia: Vern7" - "MATLAB: ode45" - "MATLAB: ode113" - "SciPy: RK45" - "SciPy: LSODA" - "SciPy: odeint" - "deSolve: lsoda" - "deSolve: ode45" - "Sundials: Adams" - ] + Dict(:alg=>CVODE_Adams())] + +names = ["Julia: DP5" + "Hairer: dopri5" + "Julia: Tsit5" + "Julia: Vern7" + "MATLAB: ode45" + "MATLAB: ode113" + "SciPy: RK45" + "SciPy: LSODA" + "SciPy: odeint" + "deSolve: lsoda" + "deSolve: ode45" + "Sundials: Adams"] abstols = 1.0 ./ 10.0 .^ (6:13) reltols = 1.0 ./ 10.0 .^ (3:10) -wp = WorkPrecisionSet(prob,abstols,reltols,setups; - names = names, - appxsol=test_sol,dense=false, - save_everystep=false,numruns=100,maxiters=10000000, - timeseries_errors=false,verbose=false) -plot(wp,title="Non-stiff 2: Rigid-Body") +wp = WorkPrecisionSet(prob, abstols, reltols, setups; + names = names, + appxsol = test_sol, dense = false, + save_everystep = false, numruns = 100, maxiters = 10000000, + timeseries_errors = false, verbose = false) +plot(wp, title = "Non-stiff 2: Rigid-Body") ``` ![](https://user-images.githubusercontent.com/1814174/71537083-ef42ac00-28e4-11ea-8dc7-a5dca0518baf.png) @@ -239,12 +233,12 @@ plot(wp,title="Non-stiff 2: Rigid-Body") ```julia rober = @ode_def begin - dy₁ = -k₁*y₁+k₃*y₂*y₃ - dy₂ = k₁*y₁-k₂*y₂^2-k₃*y₂*y₃ - dy₃ = k₂*y₂^2 + dy₁ = -k₁*y₁+k₃*y₂*y₃ + dy₂ = k₁*y₁-k₂*y₂^2-k₃*y₂*y₃ + dy₃ = k₂*y₂^2 end k₁ k₂ k₃ -prob = ODEProblem(rober,[1.0,0.0,0.0],(0.0,1e5),[0.04,3e7,1e4]) -sol = solve(prob,CVODE_BDF(),abstol=1/10^14,reltol=1/10^14) +prob = ODEProblem(rober, [1.0, 0.0, 0.0], (0.0, 1e5), [0.04, 3e7, 1e4]) +sol = solve(prob, CVODE_BDF(), abstol = 1/10^14, reltol = 1/10^14) test_sol = TestSolution(sol) abstols = 1.0 ./ 10.0 .^ (7:8) @@ -261,30 +255,27 @@ setups = [Dict(:alg=>Rosenbrock23()) Dict(:alg=>SciPyDiffEq.BDF()) Dict(:alg=>SciPyDiffEq.odeint()) Dict(:alg=>deSolveDiffEq.lsoda()) - Dict(:alg=>CVODE_BDF()) - ] - -names = [ - "Julia: Rosenbrock23" - "Julia: TRBDF2" - "Julia: radau" - "Hairer: rodas" - "Hairer: radau" - "MATLAB: ode23s" - "MATLAB: ode15s" - "SciPy: LSODA" - "SciPy: BDF" - "SciPy: odeint" - "deSolve: lsoda" - "Sundials: CVODE" - ] - -wp = WorkPrecisionSet(prob,abstols,reltols,setups; - names = names,print_names = true, - dense=false,verbose = false, - save_everystep=false,appxsol=test_sol, - maxiters=Int(1e5)) -plot(wp,title="Stiff 1: ROBER", legend=:topleft) + Dict(:alg=>CVODE_BDF())] + +names = ["Julia: Rosenbrock23" + "Julia: TRBDF2" + "Julia: radau" + "Hairer: rodas" + "Hairer: radau" + "MATLAB: ode23s" + "MATLAB: ode15s" + "SciPy: LSODA" + "SciPy: BDF" + "SciPy: odeint" + "deSolve: lsoda" + "Sundials: CVODE"] + +wp = WorkPrecisionSet(prob, abstols, reltols, setups; + names = names, print_names = true, + dense = false, verbose = false, + save_everystep = false, appxsol = test_sol, + maxiters = Int(1e5)) +plot(wp, title = "Stiff 1: ROBER", legend = :topleft) ``` ![](https://user-images.githubusercontent.com/1814174/71537080-ef42ac00-28e4-11ea-9abd-37631cd18ad9.png) @@ -293,23 +284,23 @@ plot(wp,title="Stiff 1: ROBER", legend=:topleft) ```julia f = @ode_def Hires begin - dy1 = -1.71*y1 + 0.43*y2 + 8.32*y3 + 0.0007 - dy2 = 1.71*y1 - 8.75*y2 - dy3 = -10.03*y3 + 0.43*y4 + 0.035*y5 - dy4 = 8.32*y2 + 1.71*y3 - 1.12*y4 - dy5 = -1.745*y5 + 0.43*y6 + 0.43*y7 - dy6 = -280.0*y6*y8 + 0.69*y4 + 1.71*y5 - - 0.43*y6 + 0.69*y7 - dy7 = 280.0*y6*y8 - 1.81*y7 - dy8 = -280.0*y6*y8 + 1.81*y7 + dy1 = -1.71*y1 + 0.43*y2 + 8.32*y3 + 0.0007 + dy2 = 1.71*y1 - 8.75*y2 + dy3 = -10.03*y3 + 0.43*y4 + 0.035*y5 + dy4 = 8.32*y2 + 1.71*y3 - 1.12*y4 + dy5 = -1.745*y5 + 0.43*y6 + 0.43*y7 + dy6 = -280.0*y6*y8 + 0.69*y4 + 1.71*y5 - + 0.43*y6 + 0.69*y7 + dy7 = 280.0*y6*y8 - 1.81*y7 + dy8 = -280.0*y6*y8 + 1.81*y7 end u0 = zeros(8) u0[1] = 1 u0[8] = 0.0057 -prob = ODEProblem(f,u0,(0.0,321.8122)) +prob = ODEProblem(f, u0, (0.0, 321.8122)) -sol = solve(prob,Rodas5(),abstol=1/10^14,reltol=1/10^14) +sol = solve(prob, Rodas5(), abstol = 1/10^14, reltol = 1/10^14) test_sol = TestSolution(sol) abstols = 1.0 ./ 10.0 .^ (5:8) @@ -326,29 +317,26 @@ setups = [Dict(:alg=>Rosenbrock23()) Dict(:alg=>SciPyDiffEq.BDF()) Dict(:alg=>SciPyDiffEq.odeint()) Dict(:alg=>deSolveDiffEq.lsoda()) - Dict(:alg=>CVODE_BDF()) - ] - -names = [ - "Julia: Rosenbrock23" - "Julia: TRBDF2" - "Julia: radau" - "Hairer: rodas" - "Hairer: radau" - "MATLAB: ode23s" - "MATLAB: ode15s" - "SciPy: LSODA" - "SciPy: BDF" - "SciPy: odeint" - "deSolve: lsoda" - "Sundials: CVODE" - ] - -wp = WorkPrecisionSet(prob,abstols,reltols,setups; - names = names,print_names = true, - save_everystep=false,appxsol=test_sol, - maxiters=Int(1e5),numruns=100) -plot(wp,title="Stiff 2: Hires",legend=:topleft) + Dict(:alg=>CVODE_BDF())] + +names = ["Julia: Rosenbrock23" + "Julia: TRBDF2" + "Julia: radau" + "Hairer: rodas" + "Hairer: radau" + "MATLAB: ode23s" + "MATLAB: ode15s" + "SciPy: LSODA" + "SciPy: BDF" + "SciPy: odeint" + "deSolve: lsoda" + "Sundials: CVODE"] + +wp = WorkPrecisionSet(prob, abstols, reltols, setups; + names = names, print_names = true, + save_everystep = false, appxsol = test_sol, + maxiters = Int(1e5), numruns = 100) +plot(wp, title = "Stiff 2: Hires", legend = :topleft) ``` ![](https://user-images.githubusercontent.com/1814174/71537081-ef42ac00-28e4-11ea-950f-59c762ce9a69.png) diff --git a/src/MATLABDiffEq.jl b/src/MATLABDiffEq.jl index 989d060..0168f90 100644 --- a/src/MATLABDiffEq.jl +++ b/src/MATLABDiffEq.jl @@ -15,19 +15,18 @@ struct ode15s <: MATLABAlgorithm end struct ode15i <: MATLABAlgorithm end function DiffEqBase.__solve( - prob::DiffEqBase.AbstractODEProblem{uType,tupType,isinplace}, - alg::AlgType, - timeseries = [], - ts = [], - ks = []; - saveat = eltype(tupType)[], - timeseries_errors = true, - reltol = 1e-3, - abstol = 1e-6, - callback = nothing, - kwargs..., -) where {uType,tupType,isinplace,AlgType<:MATLABAlgorithm} - + prob::DiffEqBase.AbstractODEProblem{uType, tupType, isinplace}, + alg::AlgType, + timeseries = [], + ts = [], + ks = []; + saveat = eltype(tupType)[], + timeseries_errors = true, + reltol = 1e-3, + abstol = 1e-6, + callback = nothing, + kwargs... +) where {uType, tupType, isinplace, AlgType <: MATLABAlgorithm} tType = eltype(tupType) if prob.tspan[end] - prob.tspan[1] < tType(0) @@ -61,7 +60,7 @@ function DiffEqBase.__solve( states(sys), parameters(sys), independent_variables(sys)[1], - target = ModelingToolkit.MATLABTarget(), + target = ModelingToolkit.MATLABTarget() ) # Send the variables @@ -87,7 +86,7 @@ function DiffEqBase.__solve( # Reshape the result if needed if uType <: AbstractArray timeseries = Vector{uType}(undef, length(ts)) - for i = 1:length(ts) + for i in 1:length(ts) timeseries[i] = @view timeseries_tmp[i, :] end else @@ -102,12 +101,11 @@ function DiffEqBase.__solve( ts, timeseries, timeseries_errors = timeseries_errors, - stats = stats, + stats = stats ) end function buildDEStats(solverstats::Dict) - destats = DiffEqBase.Stats(0) destats.nf = if (haskey(solverstats, "nfevals")) solverstats["nfevals"] diff --git a/test/runtests.jl b/test/runtests.jl index 98eb9e2..09a7c86 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -20,15 +20,13 @@ tspan = (0.0, 100.0) prob = ODEProblem(lorenz, u0, tspan) sol = solve(prob, MATLABDiffEq.ode45()) -algs = [ - MATLABDiffEq.ode23 - MATLABDiffEq.ode45 - MATLABDiffEq.ode113 - MATLABDiffEq.ode23s - MATLABDiffEq.ode23t - MATLABDiffEq.ode23tb - MATLABDiffEq.ode15s -] +algs = [MATLABDiffEq.ode23 + MATLABDiffEq.ode45 + MATLABDiffEq.ode113 + MATLABDiffEq.ode23s + MATLABDiffEq.ode23t + MATLABDiffEq.ode23tb + MATLABDiffEq.ode15s] for alg in algs sol = solve(prob, alg())