-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathphysics_unit.py
More file actions
180 lines (140 loc) · 4.55 KB
/
physics_unit.py
File metadata and controls
180 lines (140 loc) · 4.55 KB
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
from numpy import pi
from operator import add, sub, mul, truediv
class Unit():
def __init__(self, **parts):
self.parts = {part: power for part, power in parts.items()}
def __repr__(self):
letters = ' '.join('%s^%s' % (part, power)
if not power==1 else '%s' % part
for part, power in
sorted(self.parts.items(), key=lambda x: x[0]))
return '[%s]' % letters
def _operate(self, other, operator):
if not isinstance(other, Unit):
if isinstance(other, float) or isinstance(other, int):
return operator(self, PhysicalQuantity(other, Unit()))
else:
raise ValueError("%s is neither a unit or a value!" % other)
parts = self.parts.copy()
for p in other.parts:
if p in parts:
parts[p] = operator(parts[p], other.parts[p])
else:
parts[p] = operator(0, other.parts[p])
parts = {key:val for key, val in parts.items() if val!=0}
return Unit(**parts)
def __pow__(self, other):
if other==0:
return Unit()
elif other>0:
operator = mul
else:
operator = truediv
res = Unit()
for _ in range(abs(other)):
res = operator(res, self)
return res
def __mul__(self, other):
return self._operate(other, add)
def __rmul__(self, other):
return self
def __truediv__(self, other):
return self._operate(other, sub)
def __rtruediv__(self, other):
return (self.__truediv__(other))**(-1)
def __eq__(self, other):
return self.parts.items() == other.parts.items()
def __add__(self, other):
if self == other:
return Unit(**self.parts)
else:
raise ValueError("Units that are not the same cannot be added together.")
def __radd__(self, other):
raise ValueError("Units that are not the same cannot be added together.")
def __sub__(self, other):
if self == other:
return Unit(**self.parts)
else:
raise ValueError("Units that are not the same cannot be subtracted together.")
def __rsub__(self, other):
raise ValueError("Units that are not the same cannot be added together.")
def __len__(self):
return sum(abs(x) for x in self.parts.values())
class PhysicalQuantity:
def __init__(self, value, unit):
self.value = value
self.unit = unit
def _operate(self, other, operator):
unit = self.unit
value = other
if isinstance(other, PhysicalQuantity):
unit = operator(unit, other.unit)
value = other.value
value = operator(self.value, value)
if len(unit)==0:
return value
return PhysicalQuantity(value, unit)
def __pow__(self, other):
if isinstance(other, PhysicalQuantity):
raise ValueError("Can't have a PQ in the exponent.")
else:
value = pow(self.value, other)
unit = pow(self.unit, other)
return PhysicalQuantity(value, unit)
def __mul__(self, other):
return self._operate(other, mul)
def __rmul__(self, other):
return self._operate(other, mul)
def __truediv__(self, other):
return self._operate(other, truediv)
def __rtruediv__(self, other):
return (self.__truediv__(other))**(-1)
def __add__(self, other):
return self._operate(other, add)
def __sub__(self, other):
return self._operate(other, sub)
def __repr__(self):
return '%s %s' % (self.value, self.unit)
PQ = PhysicalQuantity
# SI-units
kg = Unit(kg=1)
m = Unit(m=1)
s = Unit(s=1)
A = Unit(A=1)
K = Unit(K=1)
mol = Unit(mol=1)
cd = Unit(cd=1)
# SI non base units
J = kg*m**2/s**2
N = kg*m/s**2
u = Unit(u=1)
eV = Unit(eV=1)
# constants
c = PQ(299792458, m/s)
G = PQ(6.67408*10**(-11), m**3/kg/s**2)
h = PQ(6.626070040 * 10**(-34), J*s)
hb = h/(2*pi)
mu0 = PQ(4*pi*10**(-7), N/A**2)
e0 = (mu0*c**2)**(-1)
e = PQ(1.6021766208 * 10**(-19), A*s)
m_e = PQ(9.10938356 * 10**(-31), kg)
m_p = PQ(1.672621898 * 10**(-27), kg)
m_n = PQ(1.674927471*10**(-27), kg)
m_u = PQ(1.660539040*10**(-27), kg/u)
J_eV = e*PQ(1, J/(A*s)/eV)
'''
# REDEFINING STUFF; UGLY AHEAD
# SI non base units
J = PQ(1, kg*m**2/s**2)
N = PQ(1, kg*m/s**2)
u = PQ(1, u)
eV = PQ(1, eV)
# SI-units
kg = PQ(1, kg)
m = PQ(1, m)
s = PQ(1, s)
A = PQ(1, A)
K = PQ(1, K)
mol = PQ(1, mol)
cd = PQ(1, cd)
'''