Skip to content

Commit

Permalink
Fix time interpolation (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
ahojukka5 authored May 25, 2018
1 parent f77832e commit 1b4027f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/fields.jl
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,9 @@ function interpolate_field(field::DCTV, time)
if t0 < time < t1
y0 = field.data[i-1].second
y1 = field.data[i].second
f = (time-t0)/(t1-t0)
return f*y0 + (1-f)*y1
dy = y1-y0
dt = t1-t0
return y0 + (time-t0)*dy/dt
end
end
end
Expand Down Expand Up @@ -179,8 +180,8 @@ function interpolate_field{N,T}(field::DVTV{N,T}, time)
if t0 < time < t1
y0 = field.data[i-1].second
y1 = field.data[i].second
f = (time-t0)/(t1-t0)
return map((a,b) -> f*a + (1-f)*b, y0, y1)
dt = t1-t0
return map((a,b) -> a + (time-t0)*(b-a)/dt, y0, y1)
end
end
end
Expand Down
21 changes: 21 additions & 0 deletions test/test_time_interpolate.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# This file is a part of JuliaFEM.
# License is MIT: see https://github.com/JuliaFEM/FEMBase.jl/blob/master/LICENSE

using FEMBase
using FEMBase.Test

# Time interpolation of fields

element = Element(Seg2, [1, 2])
update!(element, "force", 1.0 => 15.0)
update!(element, "force", 2.0 => 30.0)
@test isapprox(element("force", 1.0), 15.0)
@test isapprox(element("force", 1.2), 18.0)
@test isapprox(element("force", 1.6), 24.0)
@test isapprox(element("force", 2.0), 30.0)

update!(element, "temperature", 0.0 => (1.0, 3.0))
update!(element, "temperature", 1.0 => (2.0, 14.0))
@test isapprox(element("temperature", (0.0,), 0.0), 2.0)
@test isapprox(element("temperature", (0.0,), 0.3), 3.8)
@test isapprox(element("temperature", (0.0,), 1.0), 8.0)

0 comments on commit 1b4027f

Please sign in to comment.