-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
Graham's Law #8162
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Graham's Law #8162
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
""" | ||
Title: Graham's Law of Effusion | ||
|
||
Description: Graham's law of effusion states that the rate of effusion of a gas is | ||
inversely proportional to the square root of the molar mass of its particles: | ||
|
||
r1/r2 = sqrt(m2/m1) | ||
|
||
r1 = Rate of effusion for the first gas. | ||
r2 = Rate of effusion for the second gas. | ||
m1 = Molar mass of the first gas. | ||
m2 = Molar mass of the second gas. | ||
|
||
(Description adapted from https://en.wikipedia.org/wiki/Graham%27s_law) | ||
""" | ||
|
||
from math import pow, sqrt | ||
|
||
|
||
def validate(*values) -> bool: | ||
for value in values: | ||
if value >= 0.0: | ||
return True | ||
else: | ||
raise ValueError( | ||
"""Invalid inputs. Effusion rates and molar masses must be a positive | ||
value.""" | ||
) | ||
|
||
return False | ||
|
||
|
||
def effusion_ratio(molar_mass_1: float, molar_mass_2: float) -> float: | ||
""" | ||
Input Parameters: | ||
----------------- | ||
molar_mass_1: Molar mass of the first gas (g/mol, kg/kmol, etc.) | ||
molar_mass_2: Molar mass of the second gas (g/mol, kg/kmol, etc.) | ||
|
||
Returns: | ||
-------- | ||
>>> effusion_ratio(2.016, 4.002) | ||
1.408943 | ||
>>> effusion_ratio(-2.016, 4.002) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: Invalid inputs. Effusion rates and molar masses must be a positive | ||
value. | ||
>>> effusion_ratio(2.016) | ||
Traceback (most recent call last): | ||
... | ||
TypeError: effusion_ratio() missing 1 required positional argument: 'molar_mass_2' | ||
""" | ||
validate(molar_mass_1, molar_mass_2) | ||
return round(sqrt(molar_mass_2 / molar_mass_1), 6) | ||
|
||
|
||
def first_effusion_rate( | ||
effusion_rate: float, molar_mass_1: float, molar_mass_2: float | ||
) -> float: | ||
""" | ||
Input Parameters: | ||
----------------- | ||
effusion_rate: Effustion rate of second gas (m^2/s, mm^2/s, etc.) | ||
molar_mass_1: Molar mass of the first gas (g/mol, kg/kmol, etc.) | ||
molar_mass_2: Molar mass of the second gas (g/mol, kg/kmol, etc.) | ||
|
||
Returns: | ||
-------- | ||
>>> first_effusion_rate(1, 2.016, 4.002) | ||
1.408943 | ||
>>> first_effusion_rate(-1, 2.016, 4.002) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: Invalid inputs. Effusion rates and molar masses must be a positive | ||
value. | ||
>>> first_effusion_rate(1) | ||
Traceback (most recent call last): | ||
... | ||
TypeError: first_effusion_rate() missing 2 required positional arguments: | ||
'molar_mass_1' and 'molar_mass_2' | ||
>>> first_effusion_rate(1, 2.016) | ||
Traceback (most recent call last): | ||
... | ||
TypeError: first_effusion_rate() missing 1 required positional argument: | ||
'molar_mass_2' | ||
""" | ||
validate(effusion_rate, molar_mass_1, molar_mass_2) | ||
return round(effusion_rate * sqrt(molar_mass_2 / molar_mass_1), 6) | ||
|
||
|
||
def second_effusion_rate( | ||
effusion_rate: float, molar_mass_1: float, molar_mass_2: float | ||
) -> float: | ||
""" | ||
Input Parameters: | ||
----------------- | ||
effusion_rate: Effustion rate of second gas (m^2/s, mm^2/s, etc.) | ||
molar_mass_1: Molar mass of the first gas (g/mol, kg/kmol, etc.) | ||
molar_mass_2: Molar mass of the second gas (g/mol, kg/kmol, etc.) | ||
|
||
Returns: | ||
-------- | ||
>>> second_effusion_rate(1, 2.016, 4.002) | ||
0.709752 | ||
>>> second_effusion_rate(-1, 2.016, 4.002) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: Invalid inputs. Effusion rates and molar masses must be a positive | ||
value. | ||
>>> second_effusion_rate(1) | ||
Traceback (most recent call last): | ||
... | ||
TypeError: second_effusion_rate() missing 2 required positional arguments: | ||
'molar_mass_1' and 'molar_mass_2' | ||
>>> second_effusion_rate(1, 2.016) | ||
Traceback (most recent call last): | ||
... | ||
TypeError: second_effusion_rate() missing 1 required positional argument: | ||
'molar_mass_2' | ||
""" | ||
validate(effusion_rate, molar_mass_1, molar_mass_2) | ||
return round(effusion_rate / sqrt(molar_mass_2 / molar_mass_1), 6) | ||
|
||
|
||
def first_molar_mass( | ||
molar_mass: float, effusion_rate_1: float, effusion_rate_2: float | ||
) -> float: | ||
""" | ||
Input Parameters: | ||
----------------- | ||
molar_mass: Molar mass of the first gas (g/mol, kg/kmol, etc.) | ||
effusion_rate_1: Effustion rate of first gas (m^2/s, mm^2/s, etc.) | ||
effusion_rate_2: Effustion rate of second gas (m^2/s, mm^2/s, etc.) | ||
|
||
Returns: | ||
-------- | ||
>>> first_molar_mass(2, 1.408943, 0.709752) | ||
0.507524 | ||
>>> first_molar_mass(-1, 2.016, 4.002) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: Invalid inputs. Effusion rates and molar masses must be a positive | ||
value. | ||
>>> first_molar_mass(1) | ||
Traceback (most recent call last): | ||
... | ||
TypeError: first_molar_mass() missing 2 required positional arguments: | ||
'effusion_rate_1' and 'effusion_rate_2' | ||
>>> first_molar_mass(1, 2.016) | ||
Traceback (most recent call last): | ||
... | ||
TypeError: first_molar_mass() missing 1 required positional argument: | ||
'effusion_rate_2' | ||
""" | ||
validate(molar_mass, effusion_rate_1, effusion_rate_2) | ||
return round(molar_mass / pow(effusion_rate_1 / effusion_rate_2, 2), 6) | ||
|
||
|
||
def second_molar_mass( | ||
molar_mass: float, effusion_rate_1: float, effusion_rate_2: float | ||
) -> float: | ||
""" | ||
Input Parameters: | ||
----------------- | ||
molar_mass: Molar mass of the first gas (g/mol, kg/kmol, etc.) | ||
effusion_rate_1: Effustion rate of first gas (m^2/s, mm^2/s, etc.) | ||
effusion_rate_2: Effustion rate of second gas (m^2/s, mm^2/s, etc.) | ||
|
||
Returns: | ||
-------- | ||
>>> second_molar_mass(2, 1.408943, 0.709752) | ||
1.970351 | ||
>>> second_molar_mass(-2, 1.408943, 0.709752) | ||
Traceback (most recent call last): | ||
... | ||
ValueError: Invalid inputs. Effusion rates and molar masses must be a positive | ||
value. | ||
>>> second_molar_mass(1) | ||
Traceback (most recent call last): | ||
... | ||
TypeError: second_molar_mass() missing 2 required positional arguments: | ||
'effusion_rate_1' and 'effusion_rate_2' | ||
>>> second_molar_mass(1, 2.016) | ||
Traceback (most recent call last): | ||
... | ||
TypeError: second_molar_mass() missing 1 required positional argument: | ||
'effusion_rate_2' | ||
""" | ||
validate(molar_mass, effusion_rate_1, effusion_rate_2) | ||
return round(pow(effusion_rate_1 / effusion_rate_2, 2) / molar_mass, 6) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file
physics/grahams_law.py
, please provide doctest for the functionvalidate
Please provide type hint for the parameter:
values