Skip to content

Commit c131ec9

Browse files
Merge branch '07'
2 parents 549b58c + 9e51ab4 commit c131ec9

File tree

5 files changed

+43
-63
lines changed

5 files changed

+43
-63
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ DASSL.jl
33

44
[![Build Status](https://travis-ci.org/JuliaDiffEq/DASSL.jl.svg?branch=master)](https://travis-ci.org/JuliaDiffEq/DASSL.jl)
55
[![Coverage Status](https://img.shields.io/coveralls/pwl/DASSL.jl.svg)](https://coveralls.io/r/pwl/DASSL.jl)
6-
[![DASSL](http://pkg.julialang.org/badges/DASSL_0.5.svg)](http://pkg.julialang.org/?pkg=DASSL)
76

87
This is an implementation of DASSL algorithm for solving algebraic
98
differential equations. To inastall a stable version run
@@ -34,7 +33,7 @@ prob = DAEProblem(resrob,u0,du0,tspan)
3433
sol = solve(prob, dassl())
3534
```
3635

37-
For more details on using this interface, [see the ODE tutorial](http://docs.juliadiffeq.org/latest/tutorials/ode_example.html).
36+
For more details on using this interface, [see the ODE tutorial](http://docs.juliadiffeq.org/latest/tutorials/ode_example.html).
3837

3938
Examples
4039
--------

src/DASSL.jl

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ export dasslIterator, dasslSolve
66

77
using Reexport
88
@reexport using DiffEqBase
9+
using LinearAlgebra
10+
911
import DiffEqBase: solve
1012

1113
export dassl
@@ -20,7 +22,8 @@ mutable struct JacData
2022
jac # Jacobian matrix for the newton solver
2123
end
2224

23-
function dasslStep(F,
25+
function dasslStep(channel,
26+
F,
2427
y0::AbstractVector{T},
2528
tstart::Real;
2629
reltol = 1e-3,
@@ -45,9 +48,9 @@ function dasslStep(F,
4548
ord = 1 # initial method order
4649
tout = [tstart] # initial time
4750
h = initstep # current step size
48-
yout = Array(typeof(y0),1)
51+
yout = Array{typeof(y0)}(undef,1)
4952
yout[1] = y0
50-
dyout = Array(typeof(y0),1)
53+
dyout = Array{typeof(y0)}(undef,1)
5154
dyout[1] = dy0 # initial guess for dy0
5255
num_rejected = 0 # number of rejected steps
5356
num_fail = 0 # number of consecutive failures
@@ -128,12 +131,12 @@ function dasslStep(F,
128131

129132
# remove old results
130133
if length(tout) > ord+3
131-
shift!(tout)
132-
shift!(yout)
133-
shift!(dyout)
134+
popfirst!(tout)
135+
popfirst!(yout)
136+
popfirst!(dyout)
134137
end
135138

136-
produce(tout[end],yout[end],dyout[end])
139+
push!(channel,(tout[end],yout[end],dyout[end]))
137140

138141
# determine the new step size and order, including the current step
139142
(r,ord) = newStepOrder(tout,yout,normy,err,ord,num_fail,maxorder)
@@ -147,14 +150,13 @@ end
147150

148151

149152
# the iterator version of the dasslSolve
150-
dasslIterator(F, y0, t0; args...) = Task(()->dasslStep(F, y0, t0; args...))
151-
153+
dasslIterator(F, y0, t0; args...) = Channel((channel)->dasslStep(channel,F, y0, t0; args...))
152154

153155
# solves the equation F with initial data y0 over for times t in tspan=[t0,t1]
154156
function dasslSolve(F, y0::AbstractVector, tspan; dy0 = zero(y0), args...)
155-
tout = Array(typeof(tspan[1]),1)
156-
yout = Array(typeof(y0),1)
157-
dyout = Array(typeof(y0),1)
157+
tout = Array{typeof(tspan[1])}(undef,1)
158+
yout = Array{typeof(y0)}(undef,1)
159+
dyout = Array{typeof(y0)}(undef,1)
158160
tout[1] = tspan[1]
159161
yout[1] = y0
160162
dyout[1] = dy0
@@ -451,7 +453,7 @@ function stepper(ord::Integer,
451453
f_newton, # we want to find zeroes of this function
452454
normy) # the norm used to estimate error needs weights
453455

454-
alpha = Array(eltype(t),ord+1)
456+
alpha = Array{eltype(t)}(undef,ord+1)
455457

456458
for i = 1:ord
457459
alpha[i] = h_next/(t_next-t[end-i+1])
@@ -470,7 +472,7 @@ function stepper(ord::Integer,
470472
alpha[ord+1] = h_next/(t_next-t0)
471473

472474
alpha0 = -sum(alpha[1:ord])
473-
M = max(alpha[ord+1],abs(alpha[ord+1]+alphas-alpha0))
475+
M = max(alpha[ord+1],abs.(alpha[ord+1]+alphas-alpha0))
474476
err::eltype(t) = normy((yc-y0))*M
475477

476478

@@ -540,7 +542,7 @@ function newton_iteration(f,
540542
# after the first iteration the normy turned out to be very small,
541543
# terminate and return the first correction step
542544

543-
ep = eps(eltype(abs(y0))) # this is the epsilon for type y0
545+
ep = eps(eltype(abs.(y0))) # this is the epsilon for type y0
544546

545547
if norm1 < 100*ep*normy(y0)
546548
status=0
@@ -586,7 +588,7 @@ function dassl_norm(v, wt)
586588
end
587589

588590
function dassl_weights(y,reltol,abstol)
589-
reltol*abs(y).+abstol
591+
@. reltol*abs(y)+abstol
590592
end
591593

592594
# returns the value of the interpolation polynomial at the point x0
@@ -689,13 +691,13 @@ function numerical_jacobian(F,reltol,abstol,weights)
689691
# delta for approximation of jacobian. I removed the
690692
# sign(h_next*dy0) from the definition of delta because it was
691693
# causing trouble when dy0==0 (which happens for ord==1)
692-
edelta = diagm(max(abs(y),abs(h*dy),wt)*sqrt(ep))
694+
edelta = diagm(0=>max.(abs.(y),abs.(h*dy),wt)*sqrt(ep))
693695

694696
b=dy-a*y
695697
f(y1) = F(t,y1,a*y1+b)
696698

697699
n = length(y)
698-
jac = Array(eltype(y),n,n)
700+
jac = Array{eltype(y)}(undef,n,n)
699701
for i=1:n
700702
jac[:,i]=(f(y+edelta[:,i])-f(y))/edelta[i,i]
701703
end

src/common.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
abstract type DASSLDAEAlgorithm <: AbstractODEAlgorithm end
1+
abstract type DASSLDAEAlgorithm <: DiffEqBase.AbstractDAEAlgorithm end
22
struct dassl <: DASSLDAEAlgorithm
33
maxorder
44
factorize_jacobian
@@ -7,10 +7,12 @@ end
77
dassl(;maxorder = 6,factorize_jacobian = true) = dassl(maxorder,factorize_jacobian)
88

99
function solve(
10-
prob::AbstractDAEProblem{uType,duType,tType,isinplace},
10+
prob::DiffEqBase.AbstractDAEProblem{uType,duType,tupType,isinplace},
1111
alg::DASSLDAEAlgorithm,args...;timeseries_errors=true,
1212
abstol=1e-5,reltol=1e-3,dt = 1e-4, dtmin = 0.0, dtmax = Inf,
13-
callback=nothing,kwargs...) where {uType,duType,tType,isinplace}
13+
callback=nothing,kwargs...) where {uType,duType,tupType,isinplace}
14+
15+
tType = eltype(tupType)
1416

1517
if callback != nothing || prob.callback != nothing
1618
error("DASSL is not compatible with callbacks.")
@@ -52,6 +54,6 @@ function solve(
5254
end
5355
=#
5456

55-
build_solution(prob,alg,ts,timeseries,
57+
DiffEqBase.build_solution(prob,alg,ts,timeseries,
5658
timeseries_errors = timeseries_errors)
5759
end

test/common.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using DiffEqProblemLibrary, DiffEqBase, DASSL
2+
using DiffEqProblemLibrary.DAEProblemLibrary: importdaeproblems; importdaeproblems()
3+
using DiffEqProblemLibrary.DAEProblemLibrary: prob_dae_resrob
24

35
prob = prob_dae_resrob
46

test/runtests.jl

Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
1-
using DASSL
2-
using Base.Test
3-
using FactCheck
1+
using DASSL, Test
42

5-
facts("Testing maxorder") do
3+
@testset "Testing maxorder" begin
64

75
F(t,y,dy)=(dy+y.^2)
86
Fy(t,y,dy)=diagm(2y)
97
Fdy(t,y,dy)=eye(length(y))
108

11-
sol(t)=1./(1 .+ t)
9+
sol(t)=1.0./(1 .+ t)
1210
tspan=[0.0, 1.0]
1311

1412
atol = 1.0e-5
@@ -19,53 +17,30 @@ facts("Testing maxorder") do
1917
for order=1:6
2018
# scalar version
2119
(tn,yn,dyn)=DASSL.dasslSolve(F, sol(0.0), tspan, maxorder = order)
22-
aerror = maximum(abs(yn-sol(tn)))
23-
rerror = maximum(abs(yn-sol(tn))./abs(sol(tn)))
20+
aerror = maximum(abs.(yn-sol(tn)))
21+
rerror = maximum(abs.(yn-sol(tn))./abs.(sol(tn)))
2422
nsteps = length(tn)
2523

26-
@fact aerror --> less_than(2*nsteps*atol)
27-
@fact rerror --> less_than(2*nsteps*rtol)
24+
@test aerror < (2*nsteps*atol)
25+
@test rerror < (2*nsteps*rtol)
2826

2927
# vector version
3028
(tnV,ynV,dynV)=DASSL.dasslSolve(F,[sol(0.0)], tspan, maxorder = order)
3129

32-
@fact vcat(ynV...) --> yn
33-
@fact vcat(dynV...) --> dyn
30+
@test vcat(ynV...) == yn
31+
@test vcat(dynV...) == dyn
3432

3533
# analytical jacobian version (vector)
3634
(tna,yna,dyna)=dasslSolve(F, [sol(0.0)], tspan, maxorder = order, Fy = Fy, Fdy = Fdy)
37-
aerror = maximum(abs(map(first,yn)-sol(tn)))
38-
rerror = maximum(abs(map(first,yn)-sol(tn))./abs(sol(tn)))
35+
aerror = maximum(abs.(map(first,yn)-sol(tn)))
36+
rerror = maximum(abs.(map(first,yn)-sol(tn))./abs.(sol(tn)))
3937
nsteps = length(tn)
4038

41-
@fact aerror --> less_than(2*nsteps*atol)
42-
@fact rerror --> less_than(2*nsteps*rtol)
39+
@test aerror < (2*nsteps*atol)
40+
@test rerror < (2*nsteps*rtol)
4341
end
4442
end
4543

46-
facts("Testing minimal error tolerances") do
47-
eps=1e-6
48-
# van der Pol equation
49-
Fvdp(t,y,dy)=([dy[1]+y[2],
50-
eps*dy[2]-y[1]+y[2]*(1.0-y[1]^2)])
51-
y0 = [2., 0]
52-
tn = (0.,[2.,0.],[0.,-2/eps])
53-
54-
tol = 1e0
55-
while true
56-
tol/=10
57-
sol=dasslIterator(Fvdp, y0, 1.0; reltol=tol, abstol=tol)
58-
59-
try
60-
(tn,yn,dyn)=consume(sol)
61-
catch
62-
break
63-
end
64-
65-
end
66-
@fact tol --> roughly(1e-15)
67-
end
68-
69-
facts("Testing common interface") do
44+
@testset "Testing common interface" begin
7045
include("common.jl")
7146
end

0 commit comments

Comments
 (0)