Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IDA-key error when saving positions #290

Open
JKRT opened this issue Dec 14, 2020 · 4 comments
Open

IDA-key error when saving positions #290

JKRT opened this issue Dec 14, 2020 · 4 comments

Comments

@JKRT
Copy link

JKRT commented Dec 14, 2020

Hi!

I have the following system (see below) describing a bouncing ball.
However, when running this with
julia> plot(BouncingBallRealsSimulate((0.0, 3.0)), vars=(2))

I get the the following error:

┌ Warning: IDAGetDky failed with error code =
│   flag = -25
└ @ Sundials C:\Users\John\.julia\packages\Sundials\xAoTr\src\simple.jl:20

The plot however, seems to be as I would expect

image

I found one post by Martin Otter, in the Sundials package. However, I am not sure..

using DiffEqBase
using DifferentialEquations
using Plots
using Sundials
using Revise

function BouncingBallRealsStartConditions(p, t0)
  local x0 = Array{Float64}(undef, 2)
  local dx0 = Array{Float64}(undef, 2)
  x0[2] = (1.0) #= h =#
  return x0, dx0
end

function BouncingBallRealsDifferentialVars()
  return Bool[1, 1]
end

function BouncingBallRealsDAE_equations(res, dx, x, p, t) #=time=#
  res[1] = ((dx[1]) - ((-(p[1])))) #= g =#
  res[2] = ((dx[2]) - (x[1])) #= v =#
end

function BouncingBallRealsParameterVars()
  p = Array{Float64}(undef, 2)
  p[1] = (9.81) #= g =#
  p[2] = (0.7) #= e =#
  return p
end

function BouncingBallRealsCallbackSet(p)#=parameters=#
  condition1(x, t, integrator) = begin
    test = x[2] - 0
    return test
  end
  affect1!(integrator) =
    begin
      integrator.u[1] = -((p[2]) * ((integrator.u[1]))) #= e =#
    end
  cb1 = ContinuousCallback(condition1,
                           affect1!,
                           rootfind=true,
                           save_positions=(true, true),
                           affect_neg! = affect1!)
  return CallbackSet(cb1)
end

function BouncingBallRealsSimulate(tspan = (0.0, 5.0))
  # Define problem
  p = BouncingBallRealsParameterVars()
  (x0, dx0) = BouncingBallRealsStartConditions(p, tspan[1])
  differential_vars = BouncingBallRealsDifferentialVars()
  #= Pass the residual equations =#
  problem = DAEProblem(
    BouncingBallRealsDAE_equations,
    dx0,
    x0,
    tspan,
    p,
    differential_vars = differential_vars,
    callback = BouncingBallRealsCallbackSet(p),
  )
  solution = solve(problem, IDA())
  return solution
end

Cheers,
John

@ChrisRackauckas
Copy link
Member

It means that the interpolation did something funny, and we can investigate that, but the integration seems fine. Transferring to Sundials.jl

@JKRT
Copy link
Author

JKRT commented Jul 7, 2021

Forgot to update:

using DiffEqBase
using DifferentialEquations
using Plots
using Sundials
using Revise

function BouncingBallRealsCallbackSet(p)#=parameters=#
  condition1(x, t, integrator) = begin
    test = x[2] - 0
    return test
  end
  affect1!(integrator) =
    begin
      integrator.u[1] = -((p[2]) * ((integrator.u[1]))) #= e =#
    end
  cb1 = ContinuousCallback(condition1,
                           affect1!,
                           rootfind=true,
                           save_positions=(false, false),
                           affect_neg! = affect1!)
  return CallbackSet(cb1)
end

Setting save_positions to (false, false) does not give the warning, plot still does look alright.

using DiffEqBase
using DifferentialEquations
using Plots
using Sundials
using Revise

function BouncingBallRealsStartConditions(p, t0)
  local x0 = Array{Float64}(undef, 2)
  local dx0 = Array{Float64}(undef, 2)
  x0[2] = (1.0) #= h =#
  return x0, dx0
end

function BouncingBallRealsDifferentialVars()
  return Bool[1, 1]
end

function BouncingBallRealsDAE_equations(res, dx, x, p, t) #=time=#
  res[1] = ((dx[1]) - ((-(p[1])))) #= g =#
  res[2] = ((dx[2]) - (x[1])) #= v =#
end

function BouncingBallRealsParameterVars()
  p = Array{Float64}(undef, 2)
  p[1] = (9.81) #= g =#
  p[2] = (0.7) #= e =#
  return p
end

function BouncingBallRealsCallbackSet(p)#=parameters=#
  condition1(x, t, integrator) = begin
    test = x[2] - 0
    return test
  end
  affect1!(integrator) =
    begin
      integrator.u[1] = -((p[2]) * ((integrator.u[1]))) #= e =#
    end
  cb1 = ContinuousCallback(condition1,
                           affect1!,
                           rootfind=true,
                           save_positions=(true, true),
                           affect_neg! = affect1!)
  return CallbackSet(cb1)
end

function BouncingBallRealsSimulate(tspan = (0.0, 5.0))
  # Define problem
  p = BouncingBallRealsParameterVars()
  (x0, dx0) = BouncingBallRealsStartConditions(p, tspan[1])
  differential_vars = BouncingBallRealsDifferentialVars()
  #= Pass the residual equations =#
  problem = DAEProblem(
    BouncingBallRealsDAE_equations,
    dx0,
    x0,
    tspan,
    p,
    differential_vars = differential_vars,
    callback = BouncingBallRealsCallbackSet(p),
  )
  solution = solve(problem, IDA())
  return solution
end

Looks alright
image

However, if I add a saving callback:

    begin
        saved_values_BouncingBallReals = SavedValues(Float64, Tuple{Float64,Array})
        function BouncingBallRealsCallbackSet(aux)
            local p = aux[1]
            begin
                function condition1(x, t, integrator)
                    x[1] - 0.0
                end
                function affect1!(integrator)
                    integrator.u[2] = -(p[1] * integrator.u[2])
                end
                cb1 = ContinuousCallback(
                    condition1,
                    affect1!,
                    rootfind = true,
                    save_positions = (false, false),
                    affect_neg! = affect1!,
                )
            end
            begin
                savingFunction(u, t, integrator) =
                    let
                        (t, deepcopy(integrator.p[2]))
                    end
                cb2 = SavingCallback(savingFunction, saved_values_BouncingBallReals)
            end
            return CallbackSet(cb1, cb2)
        end
    end

The error occurs again.
I think it is related to this problem: #292 but also SciML/DifferentialEquations.jl#547 it seems Martin Otter got a similar issue with saving callbacks and Sundials

@ChrisRackauckas
Copy link
Member

Yes, SavingCallback will not be able to solve this. We need something different in saving if we want to merge that in.

@JKRT
Copy link
Author

JKRT commented Jul 8, 2021

Ah sorry my mistake, I was imprecise as usual 💯

I did not indent for the saving callback to solve the issue, rather I wanted to highlight that the issue seems to be with saving in general since the error only occurs for me when I:

A) Either run save positions (Which I should)
B) Use a saving callback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants