orbiter 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0ff7911d0476677d3acb652adbe1d4723fa61199
4
+ data.tar.gz: f4a056a95ff34c1ccaf12796c2a719cee4e6a4ea
5
+ SHA512:
6
+ metadata.gz: 1ab6aa144eb18f4a5a95c1f48d2984e14e6174c41bde0a9691ccc77b84f977275af662e83946895eaa032994e647f8b2ca6ccf8af5caa7506f47b118a596d1a2
7
+ data.tar.gz: 5fbebd1b4434c79dfd959f9837592f4e4f3ea43b169d8e0a25ca3ce9583434183acbb18ba50dfb6f55492357214b292a42d037813fe40850f32cb30f195376ce
@@ -0,0 +1,17 @@
1
+ module Orbiter
2
+ class Free_body
3
+ attr_accessor :mass, :x, :y, :vel_x, :vel_y, :acc_x, :acc_y, :old_acc_x, :old_acc_y
4
+
5
+ def initialize(attrs = {})
6
+ @mass = attrs.fetch(:mass, 10)
7
+ @x = attrs.fetch(:x, 0)
8
+ @y = attrs.fetch(:y, 0)
9
+ @vel_x = attrs.fetch(:vel_x, 0)
10
+ @vel_y = attrs.fetch(:vel_y, 0)
11
+ @acc_x = attrs.fetch(:acc_x, 0)
12
+ @acc_y = attrs.fetch(:acc_y, 0)
13
+ @old_acc_x = attrs.fetch(:old_acc_x, 0)
14
+ @old_acc_y = attrs.fetch(:old_acc_y, 0)
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,2 @@
1
+ require_relative 'free_body.rb'
2
+ require_relative 'orbit_updater.rb'
@@ -0,0 +1,50 @@
1
+ module Orbiter
2
+ class Orbit_updater
3
+
4
+ #Changing these breaks tests, which expect both to = 1
5
+ G_CONSTANT = 1
6
+ TIME_CONSTANT = 1
7
+
8
+ def self.calc_dist(body_a, body_b)
9
+ Math.sqrt( (body_a.x - body_b.x)**2 + (body_a.y - body_b.y)**2 )
10
+ end
11
+
12
+ def self.calc_grav_acc(target, other_bodies)
13
+ target.old_acc_x = target.acc_x
14
+ target.old_acc_y = target.acc_y
15
+ target.acc_x = 0.0
16
+ target.acc_y = 0.0
17
+ other_bodies.each do |other_body|
18
+ if (dist = calc_dist(target, other_body)) > 0
19
+ acc = G_CONSTANT*other_body.mass / (dist ** 2)
20
+ target.acc_x += acc * Math.cos(calc_angle(target, other_body))
21
+ target.acc_y += acc * Math.sin(calc_angle(target, other_body))
22
+ end
23
+ end
24
+ end
25
+
26
+ def self.calc_angle(body_a, body_b)
27
+ y_diff = body_b.y - body_a.y
28
+ x_diff = body_b.x - body_a.x
29
+ Math.atan2(y_diff, x_diff)
30
+ end
31
+
32
+ def self.update_velocity(target)
33
+ #binding.pry
34
+ target.vel_x += TIME_CONSTANT * (target.old_acc_x + target.acc_x ) / 2
35
+ target.vel_y += TIME_CONSTANT * (target.old_acc_y + target.acc_y ) / 2
36
+ end
37
+
38
+ def self.update_position(target)
39
+ target.x += TIME_CONSTANT * ( target.vel_x + TIME_CONSTANT * target.acc_x / 2 )
40
+ target.y += TIME_CONSTANT * ( target.vel_y + TIME_CONSTANT * target.acc_y / 2 )
41
+ end
42
+
43
+ def self.track(file, bodies)
44
+ bodies.each do |body|
45
+ file.print("#{body.x}\t#{body.y}\t")
46
+ end
47
+ file.puts()
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,27 @@
1
+ require_relative 'orbit_helper.rb'
2
+
3
+ module Orbiter
4
+
5
+ class Orbit_runner
6
+
7
+ def self.run(attrs = {})
8
+ @bodies = attrs.fetch(:bodies, [Orbiter::Free_body.new(mass: 10, x: 100, y: 100, vel_x: -5, vel_y: -1),
9
+ Orbiter::Free_body.new(mass:1000)])
10
+ @output = attrs[:output] ? File.open(attrs[:output], 'w') : $stdout
11
+
12
+ 100.times do
13
+ @bodies.each do |body|
14
+ Orbiter::Orbit_updater.calc_grav_acc(body, @bodies)
15
+ Orbiter::Orbit_updater.update_position(body)
16
+ Orbiter::Orbit_updater.calc_grav_acc(body, @bodies)
17
+ Orbiter::Orbit_updater.update_velocity(body)
18
+ end
19
+ Orbiter::Orbit_updater.track(@output, @bodies)
20
+ end
21
+
22
+ @output.close
23
+
24
+ puts "Thanks for running!"
25
+ end
26
+ end
27
+ end
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: orbiter
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Will Weiss
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-25 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email: wweiss00@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/orbiter.rb
20
+ - lib/free_body.rb
21
+ - lib/orbit_helper.rb
22
+ - lib/orbit_updater.rb
23
+ homepage:
24
+ licenses:
25
+ - MIT
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 2.0.7
44
+ signing_key:
45
+ specification_version: 4
46
+ summary: For tracking the orbits of n free bodies in a 2D space
47
+ test_files: []