Skip to content

Commit

Permalink
Add TimeSequence tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aryavorskiy committed Mar 24, 2024
1 parent ae55510 commit 7fec8ce
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 2 deletions.
70 changes: 68 additions & 2 deletions src/timesequence.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function Base.show(io::IO, mime::MIME"text/plain", tseq::TimeSequence{ET}) where
if i == maxlen < length(tseq)
print(io, "")
else
print(io, " $(tseq.times[i]) => ")
print(io, " $(round(tseq.times[i], digits=5)) => ")
show(io, mime, tseq.values[i])
end
end
Expand Down Expand Up @@ -149,6 +149,39 @@ end
Differentiate the values stored in `tseq` by time using the symmetric difference formula.
The new values are written into `tseq`.
## Example
```jldoctest
julia> using LatticeModels
julia> tseq = TimeSequence(0:0.1:10, 0:0.1:10) # f(t) = t
TimeSequence{Float64} with 101 entry
Timestamps in range 0.0 .. 10.0:
0.0 => 0.0
0.1 => 0.1
0.2 => 0.2
0.3 => 0.3
0.4 => 0.4
0.5 => 0.5
0.6 => 0.6
0.7 => 0.7
0.8 => 0.8
julia> differentiate!(tseq) # f'(t) = 1
TimeSequence{Float64} with 100 entries
Timestamps in range 0.05 .. 9.95:
0.05 => 1.0
0.15 => 1.0
0.25 => 1.0
0.35 => 1.0
0.45 => 1.0
0.55 => 1.0
0.65 => 1.0
0.75 => 1.0
0.85 => 1.0
```
"""
function differentiate!(tseq::TimeSequence)
length(tseq) < 2 && error("Cannot differentiate TimeSequence of length $(length(tseq))")
Expand All @@ -174,7 +207,40 @@ differentiate(tseq::TimeSequence) = differentiate!(copy(tseq))
integrate!(tseq::TimeSequence)
Integrate the values stored in `tseq` over time using the trapezoidal rule.
The new values are written into `tseq`.
The new values are written into `tseq`. The first value is set to zero.
## Example
```jldoctest
julia> using LatticeModels
julia> tseq = TimeSequence(0:0.1:10, 0:0.1:10) # f(t) = t
TimeSequence{Float64} with 101 entry
Timestamps in range 0.0 .. 10.0:
0.0 => 0.0
0.1 => 0.1
0.2 => 0.2
0.3 => 0.3
0.4 => 0.4
0.5 => 0.5
0.6 => 0.6
0.7 => 0.7
0.8 => 0.8
julia> integrate!(tseq) # F(t) = t^2 / 2
TimeSequence{Float64} with 101 entry
Timestamps in range 0.0 .. 10.0:
0.0 => 0.0
0.1 => 0.005
0.2 => 0.02
0.3 => 0.045
0.4 => 0.08
0.5 => 0.125
0.6 => 0.18
0.7 => 0.245
0.8 => 0.32
```
"""
function integrate!(tseq::TimeSequence)
length(tseq) < 2 && error("Cannot integrate TimeSequence of length $(length(tseq))")
Expand Down
13 changes: 13 additions & 0 deletions test/test_timedeps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@
@test rec2[t = 0.9..2.1] == TimeSequence([1, 2], [xy, xy .* 2])
@test integrate(rec) == rec2
@test_throws KeyError rec2[0.5]

ts = TimeSequence(0:0.1:10, (0:0.1:10).^ 2)
ts2 = empty(ts)
@test typeof(ts2) == TimeSequence{Float64}
@test length(ts2) == 0
for t in 7:0.1:10
delete!(ts, t)
end
delete!(ts, t=5..7)
for t in 0:0.1:4.9
ts2[t] = t^2
end
@test ts == ts2
end

@testset "Evolution" begin
Expand Down

0 comments on commit 7fec8ce

Please sign in to comment.