Math-RungeKutta
view release on metacpan or search on metacpan
lua/three-body.lua view on Meta::CPAN
#!/usr/bin/lua
-- ----------------------------------------------------------------- --
-- This Lua5 script is Copyright (c) 2010, Peter J Billam --
-- www.pjb.com.au --
-- --
-- This script is free software; you can redistribute it and/or --
-- modify it under the same terms as Lua5 itself. --
-- ----------------------------------------------------------------- --
local Version = '1.0 for Lua5'
local VersionDate = '1aug2010';
local Synopsis = [[
program_name [options] [filenames]
]]
local RK = require 'RungeKutta'
-- my ($maxcols, $maxrows);
-- eval 'require "Term/Size.pm"';
-- if ($@) {
-- $maxcols = `tput cols`;
-- if ($^O eq 'linux') { $maxrows = (`tput lines` + 0);
-- } else { $maxrows = (`tput rows` + 0);
-- }
-- } else {
-- ($maxcols, $maxrows) = &Term::Size::chars(*STDERR);
-- }
-- $maxcols = $maxcols || 80; $maxcols--;
-- $maxrows = $maxrows || 24;
local maxcols = 80
local maxrows = 36
local xmin=-1.4; local xrange=3.0;
local ymin=-1.6; local yrange=3.0;
-- t x0 y0 vx0 vy0 x1 y1 vx1 vy1 x2 y2 vx2 vy2
local m = {1100,10000,95}
local t=0
local y = {0,1, 3.0,0, 0,0, -0.27,0, 0,-1, -3.0,0}
y[7] = -1.0 * (m[1]*y[3] + m[3]*y[11]) / m[2] -- zero overall momentum
local G=.001;
local tmax = 20.0;
local dt = 0.01;
local epsilon = 0.000001;
local lastx = {1,1,1}
local lasty = {1,1,1}
function display(t, y)
-- printf "%g 1=%g,%g 2=%g,%g 3=%g,%g\n",
-- $t,$y[0],$y[1],$y[4],$y[5],$y[8],$y[9];
local x_cur; local y_cur;
local symb = {'o','O','.'}
move(0,0)
io.write(string.format("t = %g ", t))
for i=1, 3 do
move(lastx[i],lasty[i])
io.write(symb[i])
local tmp = 1 + 4 * (i-1)
x_cur = math.floor((y[tmp] - xmin) * maxcols / xrange)
tmp = 2 + 4 * (i-1)
y_cur = math.floor((y[tmp] - ymin) * maxrows / yrange)
if x_cur>=0 and x_cur<maxcols and y_cur>=0 and y_cur<maxrows then
move (x_cur,y_cur); reverse(); io.write(symb[i]); normal()
lastx[i] = x_cur; lasty[i] = y_cur
end
end
end
function move(x,y)
io.write(string.format("\027[%d;%dH", y+1, x+1))
end
function bold () io.write("\027[1m") end
function normal () io.write("\027[0m") end
function clear () io.write("\027[H\027[J") end
function reverse () io.write("\027[7m") end
function dydt(t, y)
( run in 1.796 second using v1.01-cache-2.11-cpan-39bf76dae61 )