Skip to content

Commit f9cb1dc

Browse files
Add first law of thermodynamics algorithm
1 parent 788d95b commit f9cb1dc

1 file changed

Lines changed: 171 additions & 0 deletions

File tree

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
"""
2+
________________________________________________________________________________________
3+
The first law of thermodynamics states that, when energy passes into or out of a system
4+
(as work, heat, or matter), the system's internal energy changes in accordance with the
5+
law of conservation of energy. This also results in the observation that, in an
6+
externally isolated system, even with internal changes, the sum of all forms of energy
7+
must remain constant, as energy cannot be created or destroyed.
8+
9+
Check out the formula used to calculate this flux:
10+
--------------
11+
| Q = ΔU + W |
12+
--------------
13+
14+
Q = heat added or removed from the system.
15+
ΔU = variation of internal energy of the system.
16+
W = work done by the system on its surroundings.
17+
18+
OBS: All units must be equal to each other.
19+
(Description adapted from https://en.wikipedia.org/wiki/Laws_of_thermodynamics )
20+
"""
21+
22+
23+
def __check_args(argument: float) -> None:
24+
"""
25+
Check that the arguments are valid.
26+
>>> __check_args("50")
27+
Traceback (most recent call last):
28+
...
29+
TypeError: Invalid argument. Should be an integer or float.
30+
"""
31+
32+
# Ensure valid instance
33+
if not isinstance(argument, (int, float)):
34+
raise TypeError("Invalid argument. Should be an integer or float.")
35+
36+
37+
def __categorize_system(argument_value: float, argument_name: str) -> None:
38+
"""
39+
Categorizes the system based on the work done, heat added/removed,
40+
and internal energy variation.
41+
>>> __categorize_system(0, "work")
42+
The system is isochoric (constant volume).
43+
>>> __categorize_system(50, "heat")
44+
The system is endothermic (absorbing heat).
45+
>>> __categorize_system(-20, "internal_energy_variation")
46+
The internal energy of the system is decreasing. It cooling down.
47+
>>> __categorize_system(10, "invalid")
48+
Traceback (most recent call last):
49+
...
50+
ValueError: Should be 'work', 'heat', or 'internal_energy_variation'.
51+
"""
52+
53+
if argument_name == "work":
54+
if argument_value == 0:
55+
print("The system is isochoric (constant volume).")
56+
elif argument_value > 0:
57+
print("The system is expanding.")
58+
elif argument_value < 0:
59+
print("The system is compressing.")
60+
61+
elif argument_name == "heat":
62+
if argument_value == 0:
63+
print("The system is adiabatic (no heat exchange).")
64+
elif argument_value > 0:
65+
print("The system is endothermic (absorbing heat).")
66+
elif argument_value < 0:
67+
print("The system is exothermic (releasing heat).")
68+
69+
elif argument_name == "internal_energy_variation":
70+
if argument_value == 0:
71+
print("The system is isothermic (constant internal energy)")
72+
elif argument_value > 0:
73+
print("The internal energy of the system is increasing. It heating up.")
74+
elif argument_value < 0:
75+
print("The internal energy of the system is decreasing. It cooling down.")
76+
77+
else:
78+
raise ValueError("Should be 'work', 'heat', or 'internal_energy_variation'.")
79+
80+
81+
def work(heat: float, internal_energy_variation: float) -> float:
82+
"""
83+
>>> work(50.0, -20.0)
84+
The system is endothermic (absorbing heat).
85+
The internal energy of the system is decreasing. It cooling down.
86+
The system is expanding.
87+
70.0
88+
>>> work(50.0, 50.0)
89+
The system is endothermic (absorbing heat).
90+
The internal energy of the system is increasing. It heating up.
91+
The system is isochoric (constant volume).
92+
0.0
93+
>>> work(-50.0, 20.0)
94+
The system is exothermic (releasing heat).
95+
The internal energy of the system is increasing. It heating up.
96+
The system is compressing.
97+
-70.0
98+
"""
99+
__check_args(heat)
100+
__check_args(internal_energy_variation)
101+
102+
__categorize_system(heat, "heat")
103+
__categorize_system(internal_energy_variation, "internal_energy_variation")
104+
105+
work = heat - internal_energy_variation
106+
__categorize_system(work, "work")
107+
return round(work, 1)
108+
109+
110+
def heat(internal_energy_variation: float, work: float) -> float:
111+
"""
112+
>>> heat(-20.0, 30.0)
113+
The internal energy of the system is decreasing. It cooling down.
114+
The system is expanding.
115+
The system is endothermic (absorbing heat).
116+
10.0
117+
>>> heat(50.0, 0.0)
118+
The internal energy of the system is increasing. It heating up.
119+
The system is isochoric (constant volume).
120+
The system is endothermic (absorbing heat).
121+
50.0
122+
>>> heat(20.0, -70.0)
123+
The internal energy of the system is increasing. It heating up.
124+
The system is compressing.
125+
The system is exothermic (releasing heat).
126+
-50.0
127+
"""
128+
__check_args(internal_energy_variation)
129+
__check_args(work)
130+
131+
__categorize_system(internal_energy_variation, "internal_energy_variation")
132+
__categorize_system(work, "work")
133+
134+
heat = round(internal_energy_variation + work, 1)
135+
__categorize_system(heat, "heat")
136+
return heat
137+
138+
139+
def internal_energy_variation(heat: float, work: float) -> float:
140+
"""
141+
>>> internal_energy_variation(50.0, 30.0)
142+
The system is endothermic (absorbing heat).
143+
The system is expanding.
144+
The internal energy of the system is increasing. It heating up.
145+
20.0
146+
>>> internal_energy_variation(50.0, 0.0)
147+
The system is endothermic (absorbing heat).
148+
The system is isochoric (constant volume).
149+
The internal energy of the system is increasing. It heating up.
150+
50.0
151+
>>> internal_energy_variation(-50.0, -70.0)
152+
The system is exothermic (releasing heat).
153+
The system is compressing.
154+
The internal energy of the system is increasing. It heating up.
155+
20.0
156+
"""
157+
__check_args(heat)
158+
__check_args(work)
159+
160+
__categorize_system(heat, "heat")
161+
__categorize_system(work, "work")
162+
163+
internal_energy_variation = round(heat - work, 1)
164+
__categorize_system(internal_energy_variation, "internal_energy_variation")
165+
return internal_energy_variation
166+
167+
168+
if __name__ == "__main__":
169+
from doctest import testmod
170+
171+
testmod()

0 commit comments

Comments
 (0)