Skip to content

Commit 925b1ed

Browse files
committed
feat: ✨ implementation of boyle's law
1 parent 93943aa commit 925b1ed

1 file changed

Lines changed: 181 additions & 0 deletions

File tree

physics/boyles_law.py

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
"""
2+
Title : Implementation of Boyle's law.
3+
4+
Description :
5+
Boyle's law, also referred to as the Boyle-Mariotte law, or Mariotte's law
6+
(especially in France), is an experimental gas law that describes the relationship
7+
between pressure and volume of a confined gas.
8+
9+
Boyle's law is a gas law which states that the pressure exerted by a gas
10+
(of a given mass, kept at a constant temperature) is inversely proportional to the
11+
volume occupied by it.
12+
13+
In other words, the pressure and volume of a gas are inversely proportional to each
14+
other as long as the temperature and the quantity of gas are kept constant.
15+
Boyle's law was put forward by the Anglo-Irish chemist Robert Boyle in the year 1662
16+
17+
For a gas, the relationship between volume and pressure (at constant mass and
18+
temperature) can be expressed mathematically as follows.
19+
20+
P ∝ (1/V)
21+
22+
Where P is the pressure exerted by the gas and V is the volume occupied by it. This
23+
proportionality can be converted into an equation by adding a constant, k.
24+
25+
P = k*(1/V) ⇒ PV = k
26+
27+
Boyle's law states that when the temperature of a given mass of confined gas is
28+
constant,the product of its pressure and volume is also constant. When comparing the
29+
same substance under two different sets of conditions, the law can be expressed as:
30+
31+
P1V1 = P2V2
32+
33+
Where,
34+
35+
P1 is the initial pressure exerted by the gas in Pascals (P)
36+
V1 is the initial volume occupied by the gas Litres (L)
37+
P2 is the final pressure exerted by the gas Pascals (P)
38+
V2 is the final volume occupied by the gas Litres (L)
39+
40+
This equation can be used to predict the increase in the pressure exerted by a gas
41+
on the walls of its container when the volume of its container is decreased
42+
(and its quantity and absolute temperature remain unchanged).
43+
44+
Sources :
45+
https://en.wikipedia.org/wiki/Boyle%27s_law
46+
https://byjus.com/chemistry/boyles-law/
47+
"""
48+
49+
valid_variables: list[str] = ["v1", "v2", "p1", "p2"]
50+
51+
52+
def check_validity(values: dict[str, float]) -> bool:
53+
"""
54+
55+
Function takes dictionary as an input and returns True if the input
56+
is valid
57+
58+
>>> check_validity({})
59+
Traceback (most recent call last):
60+
...
61+
ValueError: Invalid input expected 3 items got 0
62+
63+
>>> check_validity({'v1':2,'v2':4,'k':6})
64+
Traceback (most recent call last):
65+
...
66+
ValueError: Invalid input k is not a valid variable
67+
68+
>>> check_validity({'v1':2,'v2':4,'p1':6})
69+
True
70+
71+
"""
72+
if len(values) == 3:
73+
for value in values:
74+
if value not in valid_variables:
75+
msg = f"Invalid input {value} is not a valid variable"
76+
raise ValueError(msg)
77+
return True
78+
else:
79+
msg = f"Invalid input expected {3} items got {len(values)}"
80+
raise ValueError(msg)
81+
82+
83+
def find_target_variable(values: dict[str, float]) -> str:
84+
"""
85+
86+
Function is used to get the valid target variable whose value needs to be found
87+
using Boyle's Law.
88+
Function takes a dictionary as an input and returns a string
89+
90+
>>> find_target_variable({})
91+
Traceback (most recent call last):
92+
...
93+
ValueError: Invalid input expected 3 items got 0
94+
95+
>>> find_target_variable({'v1':1,'v2':2,'p2':4})
96+
'p1'
97+
98+
>>> find_target_variable({'v1':1,'v2':2,'k':4})
99+
Traceback (most recent call last):
100+
...
101+
ValueError: Invalid input k is not a valid variable
102+
103+
"""
104+
is_valid = check_validity(values)
105+
if is_valid:
106+
for variable in valid_variables:
107+
if variable not in values:
108+
return variable
109+
raise ValueError("Input is invalid")
110+
else:
111+
raise ValueError("Input is invalid")
112+
113+
114+
def boyles_law(values: dict[str, float]) -> dict[str, str]:
115+
"""
116+
117+
Function calculates the the unknown pressure or volume using Boyle's law.
118+
Function takes a dictionary as an input. It contains values for respective
119+
pressure and volumes and computes the required value and returns it as
120+
output
121+
122+
>>> boyles_law({'p1':2,'v2':1})
123+
Traceback (most recent call last):
124+
...
125+
ValueError: Invalid input expected 3 items got 2
126+
127+
>>> boyles_law({})
128+
Traceback (most recent call last):
129+
...
130+
ValueError: Invalid input expected 3 items got 0
131+
132+
>>> boyles_law({'p1':2,'v2':1, 'k':6})
133+
Traceback (most recent call last):
134+
...
135+
ValueError: Invalid input k is not a valid variable
136+
137+
>>> boyles_law({'p1':100,'v2':150, 'v1':120})
138+
{'p2': '80.0 Pa'}
139+
140+
>>> boyles_law({'p1':10,'v1':20, 'p2':20})
141+
{'v2': '10.0 L'}
142+
143+
>>> boyles_law({'v1':13,'p2':17, 'v2':19})
144+
{'p1': '24.846 Pa'}
145+
146+
>>> boyles_law({'v2':27,'p1':25, 'p2':29})
147+
{'v1': '31.32 L'}
148+
149+
"""
150+
is_valid = check_validity(values)
151+
if is_valid:
152+
target = find_target_variable(values)
153+
float_precision = ".3f"
154+
if target == "p1":
155+
p1 = float(
156+
format((values["p2"] * values["v2"]) / values["v1"], float_precision)
157+
)
158+
return {"p1": f"{p1} Pa"}
159+
elif target == "v1":
160+
v1 = float(
161+
format((values["p2"] * values["v2"]) / values["p1"], float_precision)
162+
)
163+
return {"v1": f"{v1} L"}
164+
elif target == "p2":
165+
p2 = float(
166+
format((values["p1"] * values["v1"]) / values["v2"], float_precision)
167+
)
168+
return {"p2": f"{p2} Pa"}
169+
else:
170+
v2 = float(
171+
format((values["p1"] * values["v1"]) / values["p2"], float_precision)
172+
)
173+
return {"v2": f"{v2} L"}
174+
else:
175+
raise ValueError("Input is invalid")
176+
177+
178+
if __name__ == "__main__":
179+
import doctest
180+
181+
doctest.testmod()

0 commit comments

Comments
 (0)