physics_calculator 0.0.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.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in physics_calculator.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,319 @@
1
+ require "physics_calculator/version"
2
+
3
+ module PhysicsCalculator
4
+
5
+ include Math
6
+ require 'matrix' # methods for matrix and vector manipulation
7
+
8
+ # extend standard library vector class
9
+
10
+ class ::Vector
11
+
12
+ # dot_product as alias for cross product
13
+ def dot_product(v)
14
+ self.inner_product(v)
15
+ end
16
+
17
+ # define cross product
18
+ def cross_product_(v)
19
+ raise ArgumentError, 'Vectors must be 3D' if (self.size != 3 || v.size != 3)
20
+ Vector.[](self[1]*v[2] - self[2]*v[1], self[2]*v[0] - self[0]*v[2], self[0]*v[1] - self[1]*v[0])
21
+ end
22
+
23
+ # define distance between two vectors
24
+ def distance(v)
25
+ (self - v).r
26
+ end
27
+
28
+ # define unit vector pointing from v to self
29
+ def unit_(v)
30
+ (self - v).normalize
31
+ end
32
+
33
+ end
34
+
35
+ # Physical Constants (SI Units)
36
+
37
+ SPEED_LIGHT = 299792458.0 # speed of light in a vacuum (m/s)
38
+ PLANCK = 6.62606957e-34 # planck's constant (J*s)
39
+ PLANCK_REDUCED = 1.054571726e-34 # reduced planck's constant
40
+ PERMITTIVITY = 8.854187817e-12 # vacuum permittivity (F/m)
41
+ PERMEABILITY = 1.2566370614e-6 # vacuum permeability ((V*s)/(A*m))
42
+ CHARGE_ELEMENTARY = 1.602176565e-19 # elementary charge (C)
43
+ FINE_STRUCTURE = 7.2973525698e-3 # fine structure constant (dimensionless)
44
+ MASS_ELECTRON = 9.10938215e-31 # electron rest mass (kg)
45
+ MASS_PROTON = 1.672621777e-27 # proton rest mass (kg)
46
+ MASS_NEUTRON = 1.674927351e-27 # neutron rest mass (kg)
47
+ MASS_EARTH = 5.97217e24 # mass of the Earth (kg)
48
+ MASS_SUN = 1.98855e30 # mass of the Sun (kg)
49
+ MASS_MOON = 7.3477e22 # mass of the Moon (kg)
50
+ GRAVITATIONAL = 6.67384e-11 # gravitational constant ((N*m^2)/kg^2))
51
+ ACCELERATION_GRAVITY = 9.80665 # acceleration due to gravity on Earth's surface (m/s^2)
52
+ BOLTZMANN = 1.3806488e-23 # Boltzmann constant
53
+ ENERGY_BINDING = -2.1799e-19 # energy of ground state of electron in hydrogen atom (Joules)
54
+ RYDBERG = 1.09739e7 # Rydberg constant (1/m)
55
+
56
+ # Aliases for convenience
57
+
58
+ C = SPEED_LIGHT
59
+ G = GRAVITATIONAL
60
+ H = PLANCK_REDUCED
61
+ I = Complex::I
62
+ X_ = Vector.[](1,0,0)
63
+ Y_ = Vector.[](0,1,0)
64
+ Z_ = Vector.[](0,0,1)
65
+ ACCELERATION_GRAVITY_ = -ACCELERATION_GRAVITY*Z_
66
+
67
+
68
+ # arguments with suffix _ expect 3 dimensional vectors
69
+ # arguments expect SI units
70
+
71
+
72
+ # classical mechanics
73
+
74
+
75
+ # gravitational force on mass1 at position1_ due to mass2 at position2_ (vector in Newtons)
76
+ def force_gravity_(mass1, position1_, mass2, position2_)
77
+ ((-G * mass1 * mass2) / (position1_.distance(position2_)**2)) * position1_.unit_(position2_)
78
+ end
79
+
80
+ # weight of mass at surface of Earth with -Z_ pointing at the center of Earth (vector in Newtons)
81
+ def force_weight_(mass)
82
+ ACCELERATION_GRAVITY_ * mass
83
+ end
84
+
85
+ # position of object under constant acceleration_ at time given initial conditions position0_ and velocity0_ (vector in meters)
86
+ def position_constant_acceleration_(position0_, velocity0_, acceleration_, time)
87
+ 0.5*acceleration_*time**2 + velocity0_ * time + position0_
88
+ end
89
+
90
+ # velocity of object under constant acceleration_ at time given initial condition velocity0_ (vector in m / s)
91
+ def velocity_constant_acceleration_(velocity0_, acceleration_, time)
92
+ velocity0_ + acceleration_ * time
93
+ end
94
+
95
+ # position of object under acceleration due to gravity at time given initial conditions position0_ and velocity0_ (vector in meters)
96
+ def position_gravity_acceleration_(position0_, velocity0_, time)
97
+ position_constant_acceleration_(position0_, velocity0_, ACCELERATION_GRAVITY_, time)
98
+ end
99
+
100
+ # velocity of object under acceleration due to gravity at time given initial condition velocity0_ (vector in m / s)
101
+ def velocity_gravity_acceleration_(velocity0_, time)
102
+ velocity_constant_acceleration_(velocity0_, ACCELERATION_GRAVITY_, time)
103
+ end
104
+
105
+ # linear momentum of mass at velocity_ (vector in kg * m^2)
106
+ def momentum_(mass, velocity_)
107
+ mass * velocity_
108
+ end
109
+
110
+ # kinetic energy of mass at velocity_ (scalar in Joules)
111
+ def energy_kinetic(mass, velocity_)
112
+ 0.5*mass*(velocity_.r)**2
113
+ end
114
+
115
+ # torque from applying f_ at position_ around axis_ (vector in N * m)
116
+ def torque_(axis_, position_, force_)
117
+ (position_ - axis_).cross_product_(force_)
118
+ end
119
+
120
+ # angular momentum from momentum mass*velocity_ around axis vector (vector in J * s)
121
+ def angular_momentum_(axis_, position_, mass, velocity_)
122
+ (position_ - axis_).cross_product_(momentum_(mass,velocity_))
123
+ end
124
+
125
+ # angular momentum of rotating rigid body with moment_inertia rotating at angular_velocity_ (vector in J * s)
126
+ def angular_momentum_rigid_(moment_inertia, angular_velocity_)
127
+ moment_inertia * angular_velocity_
128
+ end
129
+
130
+ # reduced mass of mass1 and mass2 (scalar in kilograms)
131
+ def mass_reduced(mass1, mass2)
132
+ (Float(mass1*mass2))/(mass1 + mass2)
133
+ end
134
+
135
+ # moment of inertia using parallel axis theorem using mass with moment_inertia_cm with axis_distance between the center of mass and axis of rotation (scalar in kg * m^2)
136
+ def moment_inertia_parallel_axis(moment_inertia_cm, mass, axis_distance)
137
+ moment_inertia_cm + mass*axis_distance**2
138
+ end
139
+
140
+ # moment of inertia of mass at distance away from axis of rotation(scalar in kg * m^2)
141
+ def moment_inertia_point_mass(mass, distance)
142
+ mass * distance**2
143
+ end
144
+
145
+ # moment of inertia of mass1 and mass2 at distance away from axis of rotation (scalar in kg * m^2)
146
+ def moment_inertia_2_point_mass(mass1, mass2, distance)
147
+ mass_reduced(mass1, mass2)*distance**2
148
+ end
149
+
150
+ # moment of inertia of rod with length and mass rotating about center of mass (scalar in kg * m^2)
151
+ def moment_inertia_rod(mass, length)
152
+ (mass*length**2) / 12.0
153
+ end
154
+
155
+ # moment of inertia of hollow sphere with radius and mass rotating about center of mass (scalar in kg * m^2)
156
+ def moment_inertia_sphere(mass, radius)
157
+ (2*mass*radius**2) / 3.0
158
+ end
159
+
160
+ # moment of inertia of solid ball with radius and mass rotating about center of mass (scalar in kg * m^2)
161
+ def moment_inertia_ball(mass, radius)
162
+ (2*mass*radius**2) / 5.0
163
+ end
164
+
165
+
166
+ # quantum mechanics
167
+
168
+
169
+ # de Broglie wavelength for a particle with momentum p (scalar in meters)
170
+ def wavelength_de_broglie(momentum)
171
+ PLANCK / momentum
172
+ end
173
+
174
+ # Hermite polynomial of degree n evaluated at x (helper for QHO eigenstates)
175
+ def hermite_polynomial(n, x)
176
+ if n == 0
177
+ 1
178
+ elsif n == 1
179
+ 2*x
180
+ else
181
+ (2*x*hermite_polynomial(n-1, x) - 2*(n-1)*hermite_polynomial(n-2, x))
182
+ end
183
+ end
184
+
185
+ # one dimensional quantum harmonic oscillator nth eigenstate for particle with mass, frequency, and position (scalar dimensionless)
186
+ def eigenstate_qho(n, mass, frequency, position)
187
+ xi = Math.sqrt((mass*frequency) / H)*position
188
+ (((mass*frequency)/(PI*H))**0.25)*(((2**n)*Math.gamma(n+1))**0.5)*hermite_polynomial(n, xi)*Math.exp((-xi**2) / 2)
189
+ end
190
+
191
+ # one dimensional quantum harmonic oscillator nth eigenstate energy for particle with mass, and frequency (scalar in Joules)
192
+ def energy_qho(n, frequency)
193
+ (n + 0.5) * H * frequency
194
+ end
195
+
196
+ # eigenstate of particle in one dimensional infinite square well potential from 0 to width with mass and position (scalar dimensionless)
197
+ def eigenstate_infinite_well(n, width, position)
198
+ sqrt(2.0 / width) * sin((n * PI * position) / width)
199
+ end
200
+
201
+ # energy of particle in one dimensional infinite square well potential from 0 to width with mass (scalar in Joules)
202
+ def energy_infinite_well(n, width, mass)
203
+ (n**2 * PI**2 * H**2) / (2 * mass * width**2)
204
+ end
205
+
206
+ # energy of the nth (n = 1, 2, 3 ...) state of the electron in a hydrogen atom according to the Bohr model (scalar in Joules)
207
+ def energy_bohr(n)
208
+ ENERGY_BINDING / n**2
209
+ end
210
+
211
+ # wavelength of light emitted in transition of electron from n_initial state to n_final state according to the Rydberg formula (scalar in meters)
212
+ def wavelength_rydberg(n_initial, n_final)
213
+ 1.0 / (RYDBERG*(1.0 / (n_final**2) - 1.0 / (n_initial**2))).abs
214
+ end
215
+
216
+
217
+ # electricity and magnetism
218
+
219
+
220
+ # Lorentz force on particle with charge moving at velocity_ through electric_field_ and magnetic_field_
221
+ def force_lorentz_(charge, velocity_, electric_field_, magnetic_field_)
222
+ charge * (electric_field_ + velocity_.cross_product_(magnetic_field_))
223
+ end
224
+
225
+ # force felt by charge1 at position1_ due to charge2 at position2_
226
+ def force_coulombs_law_(charge1, position1_, charge2, position2_)
227
+ (((1/(4*PI*PERMITTIVITY))*charge1*charge2)/(position1_.distance(position2_)**2))*position1_.unit_(position2_)
228
+ end
229
+
230
+ # electric field at position_ due to point charge at charge_position_ (vector in N / C)
231
+ def electric_field_point_charge_(charge, charge_position_, position_)
232
+ (((1/(4*PI*PERMITTIVITY))*charge)/(position_.distance(charge_position_)**2))*position_.unit_(charge_position_)
233
+ end
234
+
235
+ # magnetic field at position_ due to point charge at charge_position_ moving at charge_velocity_ (vector in Teslas)
236
+ def magnetic_field_point_charge_(charge, charge_position_, charge_velocity_, position_)
237
+ (PERMEABILITY*charge)/(4*PI*position_.distance(charge_position_)**2)*charge_velocity_.cross_product_(position_.unit_(charge_position_))
238
+ end
239
+
240
+ # equivalent resistance of resistance1 and resistance2 in series (scalar in ohms)
241
+ def resistance_series(resistance1, resistance2)
242
+ resistance1+resistance2
243
+ end
244
+
245
+ # equivalent resistance of resistance1 and resistance2 in parallel (scalar in ohms)
246
+ def resistance_parallel(resistance1, resistance2)
247
+ Float(resistance1*resistance2)/(resistance1+resistance2)
248
+ end
249
+
250
+ # equivalent inductance of inductance1 and inductance2 in series (scalar in henry)
251
+ def inductance_series(inductance1, inductance2)
252
+ inductance1+inductance2
253
+ end
254
+
255
+ # equivalent inductance of inductance1 and inductance2 in parallel (scalar in henry)
256
+ def inductance_parallel(inductance1, inductance2)
257
+ Float(inductance1*inductance2)/(inductance1+inductance2)
258
+ end
259
+
260
+ # equivalent capacitance of capacitance1 and capacitance2 in series (scalar in Farads)
261
+ def capacitance_series(capacitance1, capacitance2)
262
+ Float(capacitance1*capacitance2)/(capacitance1+capacitance2)
263
+ end
264
+
265
+ # equivalent capacitance of capacitance1 and capacitance2 in parallel (scalar in Farads)
266
+ def capacitance_parallel(capacitance1, capacitance2)
267
+ capacitance1+capacitance2
268
+ end
269
+
270
+ # voltage due to current across resistance (scalar in Volts)
271
+ def voltage_ohm_law(current, resistance)
272
+ current*resistance
273
+ end
274
+
275
+
276
+ # special relativity
277
+
278
+
279
+ # lorentz factor for object moving at velocity (scalar dimensionless)
280
+ def lorentz_factor(speed)
281
+ (1.0 / (Math.sqrt(1.0-(speed/C)**2)))
282
+ end
283
+
284
+ # time dilation for an object moving at velocity over time_interval (scalar in seconds)
285
+ def time_dilation(time_interval, speed)
286
+ lorentz_factor(speed)*time_interval
287
+ end
288
+
289
+ # length contraction for an object with length moving at speed (scalar in meters)
290
+ def length_contraction(length, speed)
291
+ length / lorentz_factor(speed)
292
+ end
293
+
294
+ # relativistic mass of an object moving at speed (scalar in kilograms)
295
+ def mass_relativistic(mass, speed)
296
+ lorentz_factor(speed)*mass
297
+ end
298
+
299
+ # relativistic momentum of an object with mass moving at speed (scalar in kg * m / s)
300
+ def momentum_relativistic(mass, speed)
301
+ lorentz_factor(speed) * mass * speed
302
+ end
303
+
304
+ # rest energy of mass (scalar in Joules)
305
+ def energy_rest(mass)
306
+ mass*C**2
307
+ end
308
+
309
+ # energy of a photon with frequency (scalar in Joules)
310
+ def energy_photon(frequency)
311
+ PLANCK*frequency
312
+ end
313
+
314
+ # momentum of a photon with frequency (scalar in kg * m / s)
315
+ def momentum_photon(frequency)
316
+ energy_photon(frequency)/C
317
+ end
318
+
319
+ end
@@ -0,0 +1,3 @@
1
+ module PhysicsCalculator
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "physics_calculator/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "physics_calculator"
7
+ s.version = PhysicsCalculator::VERSION
8
+ s.date = '2013-09-04'
9
+ s.authors = ["Cristian Derr"]
10
+ s.email = ["cristian.j.derr@gmail.com"]
11
+ s.license = 'MIT'
12
+ s.summary = "Physics Calculator"
13
+ s.description = "Gem that contains many useful methods using physics formulas. Many of them use the vector standard library. Formulas range from classical mechanics, quantum mechanics, electricity and magnetism, and special relativity."
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+
19
+ # specify any dependencies here; for example:
20
+ s.add_development_dependency "rspec"
21
+ # s.add_runtime_dependency "rest-client"
22
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: physics_calculator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Cristian Derr
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-09-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec
16
+ requirement: &25960440 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *25960440
25
+ description: Gem that contains many useful methods using physics formulas. Many of
26
+ them use the vector standard library. Formulas range from classical mechanics, quantum
27
+ mechanics, electricity and magnetism, and special relativity.
28
+ email:
29
+ - cristian.j.derr@gmail.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - .gitignore
35
+ - Gemfile
36
+ - Rakefile
37
+ - lib/physics_calculator.rb
38
+ - lib/physics_calculator/version.rb
39
+ - physics_calculator.gemspec
40
+ homepage:
41
+ licenses:
42
+ - MIT
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 1.8.16
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: Physics Calculator
65
+ test_files: []