Skip to content

Some bugs #16

Open
Open
@baggepinnen

Description

@baggepinnen

I was playing around a bit and noticed some bugs. The first two snippets are attempts at MWE of the final snippet which is the desired calculation

function simulate(x0::Float64; T = 10.0, dt = 0.0001, vt = 1.0)
    times = 0.0:dt:T
    positions = zeros(length(times))
    v = 0.0
    a = 0.0
    x = x0
    @avx for ii in eachindex(times)
        x = x + v * dt
        positions[ii] = x/x0
    end
    times, positions
end

simulate(10.0) # UndefVarError, but runs without @avx
function simulate(x0::Float64; T = 10.0, dt = 0.0001, vt = 1.0)
    times = 0.0:dt:T
    positions = zeros(length(times))
    v = 0.0
    a = 0.0
    x = x0
    @avx for ii in eachindex(times)
        t = times[ii]
        x = x + v * dt
        positions[ii] = x/x0
    end
    times, positions
end

simulate(10.0) # StackOverflowError

The final and desired code is the following

@inline friction(v::Float64, vt::Float64) =  v > vt ? -3v : -3vt*sign(v)
function simulate(x0::Float64; T = 10.0, dt = 0.0001, vt = 1.0)
    times = 0.0:dt:T
    positions = zeros(length(times))
    v = 0.0
    a = 0.0
    x = x0
    @avx for ii in eachindex(times)
        t = times[ii]
        a = friction(v, vt) - 100.0*x
        a = - 100.0*x
        v = v + a * dt
        x = x + v * dt
        positions[ii] = x/x0
    end
    times, positions
end

simulate(10.0)

Original code from discourse thread

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions