physics_calculator 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,58 +3,96 @@ require "physics_calculator/version"
3
3
  module PhysicsCalculator
4
4
 
5
5
  include Math
6
- require 'matrix' # methods for matrix and vector manipulation
6
+ # methods for matrix and vector manipulation
7
+ require 'matrix'
7
8
 
8
9
  # extend standard library vector class
9
10
 
10
11
  class ::Vector
11
12
 
12
- # dot_product as alias for cross product
13
- def dot_product(v)
14
- self.inner_product(v)
13
+ # dot_product as alias for inner product
14
+ def dot_product(v_)
15
+ self.inner_product(v_)
15
16
  end
16
17
 
17
18
  # 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])
19
+ def cross_product_(v_)
20
+ raise ArgumentError, 'Vectors must be 3D' if (self.size != 3 || v_.size != 3)
21
+ 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
22
  end
22
23
 
23
24
  # define distance between two vectors
24
- def distance(v)
25
- (self - v).r
25
+ def distance(v_)
26
+ (self - v_).r
26
27
  end
27
28
 
28
29
  # define unit vector pointing from v to self
29
- def unit_(v)
30
- (self - v).normalize
30
+ def unit_(v_)
31
+ (self - v_).normalize
31
32
  end
32
33
 
33
34
  end
34
35
 
35
36
  # Physical Constants (SI Units)
36
37
 
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)
38
+
39
+ # speed of light in a vacuum (m/s)
40
+ SPEED_LIGHT = 299792458.0
41
+
42
+ # planck's constant (J*s)
43
+ PLANCK = 6.62606957e-34
44
+
45
+ # reduced planck's constant
46
+ PLANCK_REDUCED = 1.054571726e-34
47
+
48
+ # vacuum permittivity (F/m)
49
+ PERMITTIVITY = 8.854187817e-12
50
+
51
+ # vacuum permeability ((V*s)/(A*m))
52
+ PERMEABILITY = 1.2566370614e-6
53
+
54
+ # elementary charge (C)
55
+ CHARGE_ELEMENTARY = 1.602176565e-19
56
+
57
+ # fine structure constant (dimensionless)
58
+ FINE_STRUCTURE = 7.2973525698e-3
59
+
60
+ # electron rest mass (kg)
61
+ MASS_ELECTRON = 9.10938215e-31
62
+
63
+ # proton rest mass (kg)
64
+ MASS_PROTON = 1.672621777e-27
65
+
66
+ # neutron rest mass (kg)
67
+ MASS_NEUTRON = 1.674927351e-27
68
+
69
+ # mass of the Earth (kg)
70
+ MASS_EARTH = 5.97217e24
71
+
72
+ # mass of the Sun (kg)
73
+ MASS_SUN = 1.98855e30
74
+
75
+ # mass of the Moon (kg)
76
+ MASS_MOON = 7.3477e22
77
+
78
+ # gravitational constant ((N*m^2)/kg^2))
79
+ GRAVITATIONAL = 6.67384e-11
80
+
81
+ # acceleration due to gravity on Earth's surface (m/s^2)
82
+ ACCELERATION_GRAVITY = 9.80665
83
+
84
+ # Boltzmann constant
85
+ BOLTZMANN = 1.3806488e-23
86
+
87
+ # energy of ground state of electron in hydrogen atom (Joules)
88
+ ENERGY_BINDING = -2.1799e-19
89
+
90
+ # Rydberg constant (1/m)
91
+ RYDBERG = 1.09739e7
55
92
 
56
93
  # Aliases for convenience
57
94
 
95
+
58
96
  C = SPEED_LIGHT
59
97
  G = GRAVITATIONAL
60
98
  H = PLANCK_REDUCED
@@ -117,7 +155,7 @@ module PhysicsCalculator
117
155
  (position_ - axis_).cross_product_(force_)
118
156
  end
119
157
 
120
- # angular momentum from momentum mass*velocity_ around axis vector (vector in J * s)
158
+ # angular momentum from momentum mass*velocity_ around axis_ (vector in J * s)
121
159
  def angular_momentum_(axis_, position_, mass, velocity_)
122
160
  (position_ - axis_).cross_product_(momentum_(mass,velocity_))
123
161
  end
@@ -137,7 +175,7 @@ module PhysicsCalculator
137
175
  moment_inertia_cm + mass*axis_distance**2
138
176
  end
139
177
 
140
- # moment of inertia of mass at distance away from axis of rotation(scalar in kg * m^2)
178
+ # moment of inertia of mass at distance away from axis of rotation (scalar in kg * m^2)
141
179
  def moment_inertia_point_mass(mass, distance)
142
180
  mass * distance**2
143
181
  end
@@ -166,7 +204,7 @@ module PhysicsCalculator
166
204
  # quantum mechanics
167
205
 
168
206
 
169
- # de Broglie wavelength for a particle with momentum p (scalar in meters)
207
+ # de Broglie wavelength for a particle with momentum (scalar in meters)
170
208
  def wavelength_de_broglie(momentum)
171
209
  PLANCK / momentum
172
210
  end
@@ -1,3 +1,3 @@
1
1
  module PhysicsCalculator
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -0,0 +1,352 @@
1
+ require 'rspec'
2
+ require 'physics_calculator'
3
+ include PhysicsCalculator
4
+
5
+ RSpec::Matchers.define :be_close_vector_to do |expected|
6
+ match do |actual|
7
+ n = 0
8
+ (0..2).each do |x|
9
+ a_value = actual[x]
10
+ e_value = expected[x]
11
+ if e_value == 0
12
+ tolerance = 0.0001
13
+ else
14
+ tolerance = e_value.abs / 10000.0
15
+ end
16
+ if (a_value > e_value - tolerance) and (a_value < e_value + tolerance)
17
+ n += 1
18
+ end
19
+ end
20
+ n == 3
21
+ end
22
+ end
23
+
24
+ RSpec::Matchers.define :be_close_scalar_to do |expected|
25
+ match do |actual|
26
+ if expected == 0
27
+ tolerance = 0.0001
28
+ else
29
+ tolerance = expected.abs / 10000.0
30
+ end
31
+ (actual > expected - tolerance) and (actual < expected + tolerance)
32
+ end
33
+ end
34
+
35
+ describe PhysicsCalculator do
36
+
37
+
38
+ vec1_ = Vector.[](1, 2, 3)
39
+ vec2_ = Vector.[](4, 5, 6)
40
+ vec3_ = Vector.[](1, 0, -1)
41
+
42
+ # added vector methods
43
+
44
+ describe Vector do
45
+
46
+ describe '#dot_product' do
47
+ it 'returns dot product of two vectors' do
48
+ expect(vec1_.dot_product(vec2_)).to eq 32
49
+ end
50
+ end
51
+
52
+ describe '#cross_product_' do
53
+ it 'returns cross product of two vectors' do
54
+ expect(vec1_.cross_product_(vec2_)).to be_close_vector_to [-3,6,-3]
55
+ end
56
+
57
+ it 'raises error if vectors are not 3D' do
58
+ expect {Vector.[](4,2).cross_product_(Vector.[](3,0))}.to raise_error ArgumentError
59
+ end
60
+ end
61
+
62
+ describe '#distance' do
63
+ it 'returns magnitude of difference of two vectors' do
64
+ expect(vec2_.distance(vec1_)).to be_close_scalar_to 5.19615
65
+ end
66
+ end
67
+
68
+ describe '#unit_' do
69
+ it 'returns correct unit vector pointing from first vector to second' do
70
+ expect(vec2_.unit_(vec1_)).to be_close_vector_to [0.57735, 0.57735, 0.57735]
71
+ end
72
+ end
73
+
74
+ end
75
+
76
+ # classical mechanics
77
+
78
+ describe '#force_gravity_' do
79
+ it 'returns correct force of gravity' do
80
+ expect(force_gravity_(10, vec1_, 20, vec2_)).to be_close_vector_to [2.85418e-10, 2.85418e-10, 2.85418e-10]
81
+ end
82
+ end
83
+
84
+ describe '#force_weight_' do
85
+ it 'returns the correct weight force' do
86
+ expect(force_weight_(10)).to be_close_vector_to [0, 0, -98.0665]
87
+ end
88
+ end
89
+
90
+ describe '#position_constant_acceleration_' do
91
+ it 'returns correct position under constant acceleration' do
92
+ expect(position_constant_acceleration_(vec1_, vec2_, vec3_, 10)).to be_close_vector_to [91, 52, 13]
93
+ end
94
+ end
95
+
96
+ describe '#velocity_constant_acceleration_' do
97
+ it 'returns correct velocity under constant acceleration' do
98
+ expect(velocity_constant_acceleration_(vec1_, vec2_, 10)).to be_close_vector_to [41, 52, 63]
99
+ end
100
+ end
101
+
102
+ describe '#position_gravity_acceleration_' do
103
+ it 'returns correct position under acceleration due to gravity' do
104
+ expect(position_gravity_acceleration_(vec1_, vec2_, 10)).to be_close_vector_to [41, 52, -427.3275]
105
+ end
106
+ end
107
+
108
+ describe '#velocity_gravity_acceleration_' do
109
+ it 'returns correct velocity under acceleration due to gravity' do
110
+ expect(velocity_gravity_acceleration_(vec1_, 10)).to be_close_vector_to [1, 2, -95.0665]
111
+ end
112
+ end
113
+
114
+ describe '#momentum_' do
115
+ it 'returns correct linear momentum' do
116
+ expect(momentum_(10, vec1_)).to be_close_vector_to [10, 20, 30]
117
+ end
118
+ end
119
+
120
+ describe '#energy_kinetic' do
121
+ it 'returns correct kinetic energy' do
122
+ expect(energy_kinetic(10, vec1_)).to be_close_scalar_to 70
123
+ end
124
+ end
125
+
126
+ describe '#torque_' do
127
+ it 'returns correct torque' do
128
+ expect(torque_(vec1_, vec2_, vec3_)).to be_close_vector_to [-3, 6, -3]
129
+ end
130
+ end
131
+
132
+ describe '#angular_momentum_' do
133
+ it 'returns correct angular momentum' do
134
+ expect(angular_momentum_(vec1_, vec2_, 10, vec3_)).to be_close_vector_to [-30, 60, -30]
135
+ end
136
+ end
137
+
138
+ describe '#angular_momentum_rigid_' do
139
+ it 'returns correct angular momentum' do
140
+ expect(angular_momentum_rigid_(10, vec1_)).to be_close_vector_to [10, 20, 30]
141
+ end
142
+ end
143
+
144
+ describe '#mass_reduced' do
145
+ it 'returns correct reduced mass' do
146
+ expect(mass_reduced(3, 4)).to be_close_scalar_to 1.714286
147
+ end
148
+ end
149
+
150
+ describe '#moment_inertia_parallel_axis' do
151
+ it 'returns correct moment of inertia' do
152
+ expect(moment_inertia_parallel_axis(20,10,5)).to be_close_scalar_to 270
153
+ end
154
+ end
155
+
156
+ describe '#moment_inertia_point_mass' do
157
+ it 'returns correct moment of inertia' do
158
+ expect(moment_inertia_point_mass(2,10)).to be_close_scalar_to 200
159
+ end
160
+ end
161
+
162
+ describe '#moment_inertia_2_point_mass' do
163
+ it 'returns correct moment of inertia' do
164
+ expect(moment_inertia_2_point_mass(4,6,3)).to be_close_scalar_to 21.6
165
+ end
166
+ end
167
+
168
+ describe '#moment_inertia_rod' do
169
+ it 'returns correct moment of inertia' do
170
+ expect(moment_inertia_rod(6, 10)).to be_close_scalar_to 50
171
+ end
172
+ end
173
+
174
+ describe '#moment_inertia_sphere' do
175
+ it 'returns correct moment of inertia' do
176
+ expect(moment_inertia_sphere(3,5)).to be_close_scalar_to 50
177
+ end
178
+ end
179
+
180
+ describe '#moment_inertia_ball' do
181
+ it 'returns correct moment of inertia' do
182
+ expect(moment_inertia_ball(3,5)).to be_close_scalar_to 30
183
+ end
184
+ end
185
+
186
+ # quantum mechanics
187
+
188
+ describe '#wavelength_de_broglie' do
189
+ it 'returns correct de Broglie wavelength' do
190
+ expect(wavelength_de_broglie(10)).to be_close_scalar_to 6.62607e-35
191
+ end
192
+ end
193
+
194
+ describe '#hermite_polynomial' do
195
+ it 'returns correct hermite polynomial value' do
196
+ expect(hermite_polynomial(3, 5)).to be_close_scalar_to 940
197
+ end
198
+ end
199
+
200
+ describe '#eigenstate_qho' do
201
+ it 'returns correct value of qho eigenstate' do
202
+ expect(eigenstate_qho(0, 5, 10, 0)).to be_close_scalar_to 6.23284e8
203
+ end
204
+ end
205
+
206
+ describe '#energy_qho' do
207
+ it 'returns correct qho energy' do
208
+ expect(energy_qho(4, 10)).to be_close_scalar_to 4.746e-33
209
+ end
210
+ end
211
+
212
+ describe '#eigenstate_infinite_well' do
213
+ it 'returns correct infinite well eigenstate' do
214
+ expect(eigenstate_infinite_well(4,2,10.1)).to be_close_scalar_to 0.587785
215
+ end
216
+ end
217
+
218
+
219
+ describe '#energy_infinite_well' do
220
+ it 'returns correct infinite well energy' do
221
+ expect(energy_infinite_well(3, 10, 5)).to be_close_scalar_to 9.878585e-70
222
+ end
223
+ end
224
+
225
+ describe '#energy_bohr' do
226
+ it 'returns correct Bohr energy' do
227
+ expect(energy_bohr(6)).to be_close_scalar_to -6.055278e-21
228
+ end
229
+ end
230
+
231
+ describe '#wavelength_rydberg' do
232
+ it 'returns correct wavelength using Rydberg formula' do
233
+ expect(wavelength_rydberg(2,5)).to be_close_scalar_to 4.339e-7
234
+ end
235
+ end
236
+
237
+ # electricity and magnetism
238
+
239
+ describe '#force_lorentz_' do
240
+ it 'returns correct Lorentz force' do
241
+ expect(force_lorentz_(10, vec1_, vec2_, vec3_)).to be_close_vector_to [20,90,40]
242
+ end
243
+ end
244
+
245
+ describe '#force_coulombs_law_' do
246
+ it 'returns correct force using Coulomb law' do
247
+ expect(force_coulombs_law_(5,vec1_,-10,vec2_)).to be_close_vector_to [9.6092e9, 9.6092e9, 9.6092e9]
248
+ end
249
+ end
250
+
251
+ describe '#electric_field_point_charge_' do
252
+ it 'returns correct electric field due to point charge' do
253
+ expect(electric_field_point_charge_(10,vec1_, vec2_)).to be_close_vector_to [1.92184e9, 1.92184e9, 1.92184e9]
254
+ end
255
+ end
256
+
257
+ describe '#magnetic_field_point_charge_' do
258
+ it 'returns correct magnetic field due to point charge' do
259
+ expect(magnetic_field_point_charge_(5,vec1_,vec2_,vec3_)).to be_close_vector_to [-4.47214e-8,8.94427e-8,-4.47214e-8]
260
+ end
261
+ end
262
+
263
+ describe '#resistance_series' do
264
+ it 'returns correct resistance' do
265
+ expect(resistance_series(4,5)).to be_close_scalar_to 9
266
+ end
267
+ end
268
+
269
+ describe '#resistance_parallel' do
270
+ it 'returns correct resistance' do
271
+ expect(resistance_parallel(4,5)).to be_close_scalar_to 2.22222
272
+ end
273
+ end
274
+
275
+ describe '#inductance_series' do
276
+ it 'returns correct inductance' do
277
+ expect(inductance_series(4,5)).to be_close_scalar_to 9
278
+ end
279
+ end
280
+
281
+ describe '#inductance_parallel' do
282
+ it 'returns correct inductance' do
283
+ expect(inductance_parallel(4,5)).to be_close_scalar_to 2.22222
284
+ end
285
+ end
286
+
287
+ describe '#capacitance_series' do
288
+ it 'returns correct capacitance' do
289
+ expect(capacitance_series(4,5)).to be_close_scalar_to 2.22222
290
+ end
291
+ end
292
+
293
+ describe '#capacitance_parallel' do
294
+ it 'returns correct capacitance' do
295
+ expect(capacitance_parallel(4,5)).to be_close_scalar_to 9
296
+ end
297
+ end
298
+
299
+
300
+ # special relativity
301
+
302
+ speed = 259620269
303
+
304
+ describe '#lorentz_factor' do
305
+ it 'returns correct lorentz factor value' do
306
+ expect(lorentz_factor(speed)).to be_within(0.001).of 2.0
307
+ end
308
+ end
309
+
310
+ describe '#time_dilation' do
311
+ it 'returns correct time dilation' do
312
+ expect(time_dilation(10, speed)).to be_within(0.01).percent_of 20.0
313
+ end
314
+ end
315
+
316
+ describe '#length_contraction' do
317
+ it 'returns correct length contraction' do
318
+ expect(length_contraction(10, speed)).to be_within(0.01).percent_of 5.0
319
+ end
320
+ end
321
+
322
+ describe '#mass_relativistic' do
323
+ it 'returns correct relativistic mass' do
324
+ expect(mass_relativistic(10, speed)).to be_within(0.01).percent_of 20.0
325
+ end
326
+ end
327
+
328
+ describe '#momentum_relativistic' do
329
+ it 'returns correct relativistic momentum' do
330
+ expect(momentum_relativistic(10, speed)).to be_within(0.01).percent_of 5191948531
331
+ end
332
+ end
333
+
334
+ describe '#energy_rest' do
335
+ it 'returns correct rest energy' do
336
+ expect(energy_rest(10)).to be_within(0.01).percent_of 8.9875518e17
337
+ end
338
+ end
339
+
340
+ describe '#energy_photon' do
341
+ it 'returns correct photon energy' do
342
+ expect(energy_photon(1e10)).to be_within(0.01).percent_of 6.62606957e-24
343
+ end
344
+ end
345
+
346
+ describe '#momentum_photon' do
347
+ it 'returns correct photon momentum' do
348
+ expect(momentum_photon(1e10)).to be_within(0.01).percent_of 2.2102189e-32
349
+ end
350
+ end
351
+
352
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: physics_calculator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2013-09-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &25960440 !ruby/object:Gem::Requirement
16
+ requirement: &27904776 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *25960440
24
+ version_requirements: *27904776
25
25
  description: Gem that contains many useful methods using physics formulas. Many of
26
26
  them use the vector standard library. Formulas range from classical mechanics, quantum
27
27
  mechanics, electricity and magnetism, and special relativity.
@@ -37,6 +37,7 @@ files:
37
37
  - lib/physics_calculator.rb
38
38
  - lib/physics_calculator/version.rb
39
39
  - physics_calculator.gemspec
40
+ - spec/physics_calculator_spec.rb
40
41
  homepage:
41
42
  licenses:
42
43
  - MIT
@@ -63,3 +64,4 @@ signing_key:
63
64
  specification_version: 3
64
65
  summary: Physics Calculator
65
66
  test_files: []
67
+ has_rdoc: