From 1b4027f29bc2f13e891acc1af6f8f85be41213e6 Mon Sep 17 00:00:00 2001 From: Jukka Aho Date: Fri, 25 May 2018 18:01:54 +0300 Subject: [PATCH] Fix time interpolation (#23) --- src/fields.jl | 9 +++++---- test/test_time_interpolate.jl | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) create mode 100644 test/test_time_interpolate.jl diff --git a/src/fields.jl b/src/fields.jl index 4ec5a4b..f7b09ef 100644 --- a/src/fields.jl +++ b/src/fields.jl @@ -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 @@ -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 diff --git a/test/test_time_interpolate.jl b/test/test_time_interpolate.jl new file mode 100644 index 0000000..895f923 --- /dev/null +++ b/test/test_time_interpolate.jl @@ -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)