Skip to content

Commit 89e36b3

Browse files
Add first_law_of_thermodynamics.py
1 parent 28959e0 commit 89e36b3

1 file changed

Lines changed: 151 additions & 0 deletions

File tree

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
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+
"""
27+
28+
# Ensure valid instance
29+
if not isinstance(argument, (int, float)):
30+
raise TypeError("Invalid argument. Should be an integer or float.")
31+
32+
33+
def categorize_system(argument_value: float, argument_name: str) -> None:
34+
"""
35+
Categorizes the system based on the work done, heat added/removed,
36+
and internal energy variation.
37+
"""
38+
39+
if argument_name == "work":
40+
if argument_value == 0:
41+
print("The system is isochoric (constant volume).")
42+
elif argument_value > 0:
43+
print("The system is expanding.")
44+
elif argument_value < 0:
45+
print("The system is compressing.")
46+
47+
elif argument_name == "heat":
48+
if argument_value == 0:
49+
print("The system is adiabatic (no heat exchange).")
50+
elif argument_value > 0:
51+
print("The system is endothermic (absorbing heat).")
52+
elif argument_value < 0:
53+
print("The system is exothermic (releasing heat).")
54+
55+
elif argument_name == "internal_energy_variation":
56+
if argument_value == 0:
57+
print("The system is isothermic (constant internal energy)")
58+
elif argument_value > 0:
59+
print("The internal energy of the system is increasing. It heating up.")
60+
elif argument_value < 0:
61+
print("The internal energy of the system is decreasing. It cooling down.")
62+
63+
64+
def work(heat: float, internal_energy_variation: float) -> float:
65+
"""
66+
>>> work(50.0, -20.0)
67+
The system is endothermic (absorbing heat).
68+
The internal energy of the system is decreasing. It cooling down.
69+
The system is expanding.
70+
70.0
71+
>>> work(50.0, 50.0)
72+
The system is endothermic (absorbing heat).
73+
The internal energy of the system is increasing. It heating up.
74+
The system is isochoric (constant volume).
75+
0.0
76+
>>> work(-50.0, 20.0)
77+
The system is exothermic (releasing heat).
78+
The internal energy of the system is increasing. It heating up.
79+
The system is compressing.
80+
-70.0
81+
"""
82+
check_args(heat)
83+
check_args(internal_energy_variation)
84+
85+
categorize_system(heat, "heat")
86+
categorize_system(internal_energy_variation, "internal_energy_variation")
87+
88+
work = heat - internal_energy_variation
89+
categorize_system(work, "work")
90+
return round(work, 1)
91+
92+
def heat(internal_energy_variation: float, work: float) -> float:
93+
"""
94+
>>> heat(-20.0, 30.0)
95+
The internal energy of the system is decreasing. It cooling down.
96+
The system is expanding.
97+
The system is endothermic (absorbing heat).
98+
10.0
99+
>>> heat(50.0, 0.0)
100+
The internal energy of the system is increasing. It heating up.
101+
The system is isochoric (constant volume).
102+
The system is endothermic (absorbing heat).
103+
50.0
104+
>>> heat(20.0, -70.0)
105+
The internal energy of the system is increasing. It heating up.
106+
The system is compressing.
107+
The system is exothermic (releasing heat).
108+
-50.0
109+
"""
110+
check_args(internal_energy_variation)
111+
check_args(work)
112+
113+
categorize_system(internal_energy_variation, "internal_energy_variation")
114+
categorize_system(work, "work")
115+
116+
heat = round(internal_energy_variation + work, 1)
117+
categorize_system(heat, "heat")
118+
return heat
119+
120+
def internal_energy_variation(heat: float, work: float) -> float:
121+
"""
122+
>>> internal_energy_variation(50.0, 30.0)
123+
The system is endothermic (absorbing heat).
124+
The system is expanding.
125+
The internal energy of the system is increasing. It heating up.
126+
20.0
127+
>>> internal_energy_variation(50.0, 0.0)
128+
The system is endothermic (absorbing heat).
129+
The system is isochoric (constant volume).
130+
The internal energy of the system is increasing. It heating up.
131+
50.0
132+
>>> internal_energy_variation(-50.0, -70.0)
133+
The system is exothermic (releasing heat).
134+
The system is compressing.
135+
The internal energy of the system is increasing. It heating up.
136+
20.0
137+
"""
138+
check_args(heat)
139+
check_args(work)
140+
141+
categorize_system(heat, "heat")
142+
categorize_system(work, "work")
143+
144+
internal_energy_variation = round(heat - work, 1)
145+
categorize_system(internal_energy_variation, "internal_energy_variation")
146+
return internal_energy_variation
147+
148+
if __name__ == "__main__":
149+
from doctest import testmod
150+
151+
testmod()

0 commit comments

Comments
 (0)