#!/usr/bin/env ruby
require("gsl")

dim = 2

func = Proc.new { |t, y, dydt, mu|
  dydt[0] = y[1]
  dydt[1] = -y[0] - mu*y[1]*(y[0]*y[0] - 1.0)
}

jac = Proc.new { |t, y, dfdy, dfdt, mu|
  dfdy.set(0, 0, 0.0)
  dfdy.set(0, 1, 1.0)
  dfdy.set(1, 0, -2*mu*y[0]*y[1] - 1.0)
  dfdy.set(1, 1, -mu*(y[0]*y[0] - 1.0))
  dfdt[0] = 0.0
  dfdt[1] = 0.0
}
s = Odeiv::Step.new(Odeiv::Step::RKF45, dim)
#s = Odeiv::Step.new(BSIMP, dim)
#p s.name
#p s.order

c = Odeiv::Control.y_new(1e-6, 0.0)

e = Odeiv::Evolve.new(dim)

#sys = Odeiv::System.new(func, jac, dim)
#sys = Odeiv::System.new(func, nil, dim)
sys = Odeiv::System.new(func, dim)

mu = 10.0
sys.set_params(mu)
#p sys.params

t = 0.0; tend = 100.0
h = 1e-6
y = Vector.new([1.0, 0.0])

GSL::ieee_env_setup()

file = File.open("odeiv.dat", "w")
while t < tend
  status = e.apply(c, s, sys, t, tend, h, y)
  break if status != GSL::SUCCESS
  file.printf("%.5e %.5e %.5e %.5e\n", t, y[0], y[1], h)
end
file.close

system("echo \"set grid; plot 'odeiv.dat' w lp\" | gnuplot -persist")
File.delete("odeiv.dat")

