#!/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
}

#solver = Odeiv::Solver.new(Odeiv::Step::RK8PD, [1e-6, 0.0, 1, 0], func, jac, dim)
#solver = Odeiv::Solver.new(Odeiv::Step::RK8PD, [1e-6, 0.0, 1, 0], func, nil, dim)
#solver = Odeiv::Solver.new(Odeiv::Step::RK8PD, [1e-6, 0.0], func, nil, dim)
#solver = Odeiv::Solver.new(Odeiv::Step::RKF45, [1e-6, 0.0], func, jac, dim)
#solver = Odeiv::Solver.new(Odeiv::Step::RKF45, [1e-6, 0.0], func, nil, dim)
solver = Odeiv::Solver.new(Odeiv::Step::BSIMP, [1e-6, 0.0], func, jac, dim)
solver.set_params(10.0)

#p solver.step.name
#s = Odeiv::Step.new(Odeiv::STEP_RKCK, dim)
#solver.set_step(s)
#p solver.step.name
#p solver.control.name

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

GSL::ieee_env_setup()

file = File.open("odeiv2.dat", "w")
while t < tend
  status = solver.apply(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 \"plot 'odeiv2.dat' w lp\" | gnuplot -persist")
File.delete("odeiv2.dat")
