newtonian 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d51fbf9467ba64653f74175c3e8100caeb107132
4
+ data.tar.gz: 3a6e6ef225fd3775bea6a992c134d08ec696282d
5
+ SHA512:
6
+ metadata.gz: e5bc0299f85133ce3ae1b2a4f898fff31d72b7cf3b12f67b8be7a484f8c93840d1441ba6f3080b0f9e6e95d616478e5d75ad562edc1d33bc805cd9e371dbf34d
7
+ data.tar.gz: 1da92ca29b9c0f316e62aadb46b3eee8c0bd660ef58888d0151e32f18415dcdca487b0e19ba1b99db5518a703a0c4fcf9ffc35d22768b940c818cfabc26fde52
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in newtonian.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Tobi Lehman
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,99 @@
1
+ # Newtonian
2
+
3
+ Newtonian physics gives a way to predict the future state of a system of massive objects in a Euclidean space. This gem provides a library of classes such as Point, Vector, Force. This gem enables a system built from these classes to be evolved forward in time according to Newton's laws of motion.
4
+
5
+ Note: all bold quantities are vectors. SI units are assumed everywhere, here's a brief list in case you don't remember all the SI units:
6
+
7
+ - Time is measured in seconds (s)
8
+ - Length is measured in meters (m)
9
+ - Mass is measured in kilograms (kg)
10
+
11
+ There are more, but none are relevant to the newtonian library, it only deals with space, time and mass.
12
+
13
+ The unit for force is sometimes abbreviated (N), as Newtons, but it is defined in terms of kilograms, meters and seconds.
14
+
15
+ ## Newton's laws of motion
16
+
17
+ 1. An object either remains at rest or continues to move at a constant velocity
18
+ unless acted upon by a force.
19
+
20
+ 2. The vector sum of external forces **F** acting on an object is equal to the object's mass m
21
+ times the acceleration vector **a**.
22
+
23
+ 3. When one body exerts a force on another body, the other body exerts an equal and opposite force on the original body.
24
+
25
+ The second law can be summarized as:
26
+
27
+ ### **F** = m **a**
28
+
29
+ The third law can be summarized as:
30
+
31
+ ### **F<sub>1</sub>** = -**F<sub>2</sub>**
32
+
33
+ ## Conservation of momentum
34
+
35
+ Newton derived the conservation of momentum from his three laws, but later developments in physics, such as electromagnetism and quantum mechanics show that conservation of momentum is actually the more fundamental principle, so I will state it here axiomatically.
36
+
37
+ Momentum is usually denoted with a **p**, and it is defines as the product of an objects mass and velocity:
38
+
39
+ **p** = m**v**
40
+
41
+ Newton's second and third laws can also be summarized as
42
+
43
+ **F** = (d/dt)**p**
44
+
45
+ Where (d/dt) means the "rate of change with respect to time". If you are not familiar with calculus, this is called a [derivative](https://en.wikipedia.org/wiki/Derivative).
46
+
47
+ ## Newton's law of universal gravitation
48
+
49
+ Given two objects with masses m<sub>1</sub> and m<sub>2</sub>, the force of gravity on object two from object one is:
50
+
51
+ **F<sub>21</sub>** = (Gm<sub>1</sub>m<sub>2</sub>/r<sup>2</sup>)**r&#770;<sub>21</sub>**
52
+
53
+ Where r = ||**r<sub>21</sub>**|| (the norm (length) of the vector from object 2 to object 1), and
54
+
55
+ **r&#770;<sub>21</sub>** is a vector in the direction of **r<sub>21</sub>**, but with a magnitude (norm) of 1.
56
+
57
+ And G is the gravitational constant: 6.673×10<sup>−11</sup> N (m/kg)<sup>2</sup>
58
+
59
+ ![image of two massive bodies and their force on each other](img/f21_gravity.png)
60
+
61
+ ## Using this library to apply Newton's laws
62
+
63
+
64
+
65
+
66
+ ## Installation
67
+
68
+ Add this line to your application's Gemfile:
69
+
70
+ ```ruby
71
+ gem 'newtonian'
72
+ ```
73
+
74
+ And then execute:
75
+
76
+ $ bundle
77
+
78
+ Or install it yourself as:
79
+
80
+ $ gem install newtonian
81
+
82
+ ## Usage
83
+
84
+ TODO: Write usage instructions here
85
+
86
+ ## Development
87
+
88
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
89
+
90
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
91
+
92
+ ## Contributing
93
+
94
+ Bug reports and pull requests are welcome on GitHub at https://github.com/tlehman/newtonian.
95
+
96
+
97
+ ## License
98
+
99
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "newtonian"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
Binary file
@@ -0,0 +1,18 @@
1
+ class Body
2
+ attr_reader :mass
3
+ attr_accessor :position, :velocity
4
+
5
+ def initialize(mass:, position:, velocity:)
6
+ @mass = mass
7
+ @position = position.is_a?(Vector) ? position : Vector.new(position)
8
+ @velocity = velocity.is_a?(Vector) ? velocity : Vector.new(velocity)
9
+ end
10
+
11
+ def force_from(body)
12
+ rvec = body.position - position
13
+ r = rvec.norm
14
+ rhat = rvec * (1/r)
15
+ rhat * (Newtonian.G * mass * body.mass / r**2)
16
+ end
17
+
18
+ end
@@ -0,0 +1,9 @@
1
+ Dir["lib/*.rb"].each { |f| require "./#{f}" }
2
+
3
+ module Newtonian
4
+ class DimensionError < StandardError; end
5
+
6
+ def self.G
7
+ 6.673*10**-11
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module Newtonian
2
+ VERSION = "0.2.1"
3
+ end
@@ -0,0 +1,36 @@
1
+ class Point
2
+ attr_reader :coordinates
3
+
4
+ def initialize(coordinates)
5
+ @coordinates = coordinates
6
+ end
7
+
8
+ def dimension
9
+ coordinates.length
10
+ end
11
+
12
+ def +(vector)
13
+ if vector.dimension != dimension
14
+ raise Newtonian::DimensionError
15
+ end
16
+
17
+ Vector.new(coordinates.zip(vector.components).map {|(vi,wi)| vi+wi })
18
+ end
19
+
20
+ def -(point)
21
+ if point.dimension != dimension
22
+ raise Newtonian::DimensionError
23
+ end
24
+
25
+ diffs = coordinates.zip(point.coordinates).map {|(ui,vi)| ui-vi }
26
+ ::Vector.new(diffs)
27
+ end
28
+
29
+ def distance(point)
30
+ if point.dimension != dimension
31
+ raise Newtonian::DimensionError
32
+ end
33
+
34
+ (self - point).norm
35
+ end
36
+ end
@@ -0,0 +1,11 @@
1
+ module Trigonometry
2
+ refine Numeric do
3
+ def degrees
4
+ (self*Math::PI / 180.0)
5
+ end
6
+
7
+ def radians
8
+ self * (180/Math::PI)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,41 @@
1
+ class Universe
2
+ attr_reader :dimensions, :bodies
3
+
4
+ def initialize(dimensions:, bodies:)
5
+ @dimensions = dimensions
6
+ @bodies = bodies
7
+ end
8
+
9
+ def evolve(dt)
10
+ each_pair do |(body_i, body_j)|
11
+ force_ij = body_i.force_from(body_j)
12
+ # dv = (F/m)dt
13
+ f_over_m = force_ij * (1.0/body_i.mass)
14
+ body_i.velocity += f_over_m * dt
15
+ body_i.position += body_i.velocity * dt
16
+ end
17
+ end
18
+
19
+ def self.big?
20
+ true
21
+ end
22
+
23
+ private
24
+
25
+ def each_pair
26
+ # get all pairs of distinct bodies
27
+ #
28
+ # 1 2 3 4
29
+ # 1 . . .
30
+ # 2 . . .
31
+ # 3 . . .
32
+ # 4 . . .
33
+ #
34
+ bodies.each do |body_i|
35
+ bodies.each do |body_j|
36
+ next if body_i == body_j
37
+ yield [body_i, body_j]
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,45 @@
1
+ class Vector
2
+ attr_reader :components
3
+
4
+ def initialize(components)
5
+ @components = components
6
+ end
7
+
8
+ def dimension
9
+ @components.length
10
+ end
11
+
12
+ def +(vector)
13
+ if vector.dimension != dimension
14
+ raise Newtonian::DimensionError
15
+ end
16
+
17
+ Vector.new(components.zip(vector.components).map {|(vi,wi)| vi+wi })
18
+ end
19
+
20
+ def -(vector)
21
+ self + (vector * -1)
22
+ end
23
+
24
+ def *(scalar)
25
+ Vector.new(components.map{|c| scalar*c })
26
+ end
27
+
28
+ def ==(vector)
29
+ return false if vector.dimension != dimension
30
+
31
+ components == vector.components
32
+ end
33
+
34
+ def norm
35
+ Math.sqrt(dot(self))
36
+ end
37
+
38
+ def dot(vector)
39
+ if vector.dimension != dimension
40
+ raise Newtonian::DimensionError
41
+ end
42
+
43
+ components.zip(vector.components).map {|(vi,wi)| vi*wi }.inject(&:+)
44
+ end
45
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'newtonian/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "newtonian"
8
+ spec.version = Newtonian::VERSION
9
+ spec.authors = ["Tobi Lehman"]
10
+ spec.email = ["mail@tobilehman.com"]
11
+
12
+ spec.summary = %q{An elementary Newtonian physics engine}
13
+ spec.description = %q{Newtonian physics gives a way to predict the future state of a system of massive objects in a Euclidean space. This gem provides a library of classes such as Point, Vector, Force. This gem enables a system built from these classes to be evolved forward in time according to Newton's laws of motion.}
14
+ spec.homepage = "https://github.com/tlehman/newtonian"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec"
25
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: newtonian
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.1
5
+ platform: ruby
6
+ authors:
7
+ - Tobi Lehman
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-02-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Newtonian physics gives a way to predict the future state of a system
56
+ of massive objects in a Euclidean space. This gem provides a library of classes
57
+ such as Point, Vector, Force. This gem enables a system built from these classes
58
+ to be evolved forward in time according to Newton's laws of motion.
59
+ email:
60
+ - mail@tobilehman.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - ".gitignore"
66
+ - ".rspec"
67
+ - ".travis.yml"
68
+ - Gemfile
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - bin/console
73
+ - bin/setup
74
+ - img/f21_gravity.png
75
+ - lib/body.rb
76
+ - lib/newtonian.rb
77
+ - lib/newtonian/version.rb
78
+ - lib/point.rb
79
+ - lib/trigonometry.rb
80
+ - lib/universe.rb
81
+ - lib/vector.rb
82
+ - newtonian.gemspec
83
+ homepage: https://github.com/tlehman/newtonian
84
+ licenses:
85
+ - MIT
86
+ metadata: {}
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 2.2.2
104
+ signing_key:
105
+ specification_version: 4
106
+ summary: An elementary Newtonian physics engine
107
+ test_files: []