-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmamah1.py
executable file
·206 lines (178 loc) · 4.91 KB
/
mamah1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#!/usr/bin/env python
# -*- coding: utf-8 -*-
__author__ = "Max Kovgan <[email protected]>,"
import sys
if sys.version_info <= (2, 6, 0):
print "This script requires python v2.6 and up"
sys.exit(1)
import math
from openuhw import OpenUHWSolver, OpenUHWResult
AVOGADRO_CONSTANT = float(6.022141E23) # entities/mol
PI = math.pi
mamah1 = OpenUHWSolver()
def box_volume(l, w=None, h=None):
"""
returns l*w*h.
l length
w width
h height
"""
length = l
width = w if w else l
height = h if h else l
return length * width * height
# def cube_volume(d):
# return box_volume(d)
def sphere_volume(r):
return ((4.0 / 3.0) * PI) * box_volume(r)
def ex_1():
"""
hw1 ex1
"""
result = OpenUHWResult(title=u"ex1")
a = float(1.0E6)
b = float(5.0E-3)
c = float(0.5E2)
result.answers.append(
dict(
descr=u"ABC",
result=a * b * c,
)
)
result.answers.append(
dict(
descr=u"AC^(-2)",
result=(a / (c * c)),
)
)
return result
def ex_2():
result = OpenUHWResult(title=u"ex2")
r_atom = float(1.0E-10)
r_earth = float(6.37E6)
v_atom = box_volume(r_atom) ## annoying assumption
v_earth = sphere_volume(r_earth)
ratio = v_earth / v_atom
result.answers.append(
dict(
descr=u"V(earth)/'V'(atom)",
result=ratio,
)
)
return result
def ex_3():
result = OpenUHWResult(title=u"ex3")
## box a,b,c
a = float(9.7E-2) # m
b = float(5.3E-2) # m
c = float(4.4E-2) # m
err = float(0.5E-3) # m
vol_box = box_volume(a, w=b, h=c)
## minimal box
a_min = a - err
b_min = b - err
c_min = c - err
## maximal box
a_max = a + err
b_max = b + err
c_max = c + err
## compare:
vol_min = box_volume(a_min, b_min, c_min)
vol_max = box_volume(a_max, b_max, c_max)
vol_err = (vol_max - vol_min) / 2
result.answers.append(
dict(
descr=u"V(box)±err",
result=u"{vol_box}m^3±{vol_err}m^3".format(**locals()),
)
)
return result
def ex_4():
result = OpenUHWResult(title=u"ex4")
density_hg = float(13.58) # in g/cm^3
atomic_mass_hg = float(200.59) # in AMU i.e. g/mol
## how many atoms of Hg are there in 1 cm^3?
vol = float(1) # in cm^3
mass_hg = vol * density_hg ## grams
molar_mass = mass_hg / atomic_mass_hg # mols
atoms_count = molar_mass * AVOGADRO_CONSTANT
result.answers.append(
dict(
descr=u"atoms_count(hg, vol=1 cm^3)",
result=atoms_count,
)
)
molar_mass = 1 # moles
mass_hg = molar_mass * atomic_mass_hg # grams
vol = mass_hg / density_hg
result.answers.append(
dict(
descr=u"volume(hg, 1 mol)",
result=vol,
)
)
return result
def ex_5():
result = OpenUHWResult(title=u"ex5")
sound_speed_air = float(333.33) ## m/sec
light_speed_air = float(3.0E8) ## m/sec
## how many orders of magnitude is speed of light higher than speed of sound?
### ans: numb. of orders of magnitude := log10 ( ratio )
### question is: how many WHOLE orders of magnitude ? it's floor rounding!
result.answers.append(
dict(
descr=u"orders_of_mag(light,sound)",
# result=math.log10(light_speed_air/sound_speed_air), ## the exact is 5.99 :) so it's 5!
result=math.floor(math.log10(light_speed_air / sound_speed_air)),
)
)
return result
def ex_6():
result = OpenUHWResult(title=u"ex6")
## work in given units:
density = float(1.55E-3) # in g/cm^-3
density_per_litre = float(2.22E22) # atoms
density_per_cubic_m = density_per_litre * float(1.0E3) # atoms
moa_kg = density_per_cubic_m * density # kg
### convert to SI:
kg2amu_coefficient = float(1.660538921E-27)
# mass_of_an_atom = mass_of_an_atom_in_kg * float(1.0E-3)
moa = moa_kg
### hence:
amu = moa / kg2amu_coefficient
## how many orders of magnitude is speed of light higher than speed of sound?
result.answers.append(
dict(
descr=u"AMU(gas)",
result=u"{}".format(str(amu)),
)
)
return result
# def ex_7():
# result = OpenUHWResult(title=u"ex7")
# ## work in given units:
# isotopes_data = [
# dict(mass=float(23.985), probability=float(0.79), ),
# dict(mass=float(24.9858), probability=None, ),
# dict(mass=float(25.9826), probability=None, ),
# ]
# # iso_1_probability = float(0.79)
# std_mass_in_amu = float(24.305)
#
# result.answers.append(
# dict(
# descr=u"P(max(mass, {isotopes}))",
# result=u"{}".format(str(amu)),
# )
# )
# return result
solvers_list = [
ex_1,
ex_2,
ex_3,
ex_4,
ex_5,
ex_6,
]
[mamah1.add_solver(s) for s in solvers_list]
mamah1.solve(print_solution=True)