Skip to content
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

Adding avg and mps speed formulae for ideal gases #10229

Merged
merged 29 commits into from
Oct 14, 2023

Conversation

Baron105
Copy link
Contributor

Describe your change:

Added functions to calculate the average and most probable speeds of molecules in a gas given the temperature and molar mass of the gas. Also added a few doctests for testing.

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

@algorithms-keeper algorithms-keeper bot added the awaiting reviews This PR is ready to be reviewed label Oct 10, 2023
@algorithms-keeper algorithms-keeper bot added the require descriptive names This PR needs descriptive function and/or variable names label Oct 10, 2023
Copy link

@algorithms-keeper algorithms-keeper bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

to find the sum of the proper divisors of a number.
"""

def sum_of_divisors(n: int) -> int:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: n


return sum

def is_amicable_pair(a: int, b: int) -> bool:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: a

Please provide descriptive name for the parameter: b

Copy link

@algorithms-keeper algorithms-keeper bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

"""


def sum_of_divisors(n: int) -> int:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: n

return sum


def is_amicable_pair(a: int, b: int) -> bool:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: a

Please provide descriptive name for the parameter: b

@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Oct 10, 2023
@algorithms-keeper algorithms-keeper bot removed the require descriptive names This PR needs descriptive function and/or variable names label Oct 10, 2023
@algorithms-keeper algorithms-keeper bot removed the tests are failing Do not merge until tests pass label Oct 10, 2023
Comment on lines 49 to 51
# necessary constants
PI = 3.1415926535 # pi
R = 8.3144626181 # gas constant
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we import these constants from existing libraries rather than hard-coding them?

Comment on lines 69 to 70
else:
return (8 * R * temperature / (PI * molar_mass)) ** 0.5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
else:
return (8 * R * temperature / (PI * molar_mass)) ** 0.5
return (8 * R * temperature / (PI * molar_mass)) ** 0.5

Comment on lines 88 to 89
else:
return (2 * R * temperature / molar_mass) ** 0.5
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
else:
return (2 * R * temperature / molar_mass) ** 0.5
return (2 * R * temperature / molar_mass) ** 0.5

Comment on lines 65 to 68
if temperature < 0:
raise Exception("Absolute temperature cannot be less than 0 K")
if molar_mass <= 0:
raise Exception("Molar mass should be greater than 0 kg/mol")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the sake of completeness, could you add doctests that test these cases?

Comment on lines 84 to 87
if temperature < 0:
raise Exception("Absolute temperature cannot be less than 0 K")
if molar_mass <= 0:
raise Exception("Molar mass should be greater than 0 kg/mol")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same with these cases

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's rename the file to speeds_of_gas_molecules.py to keep it shorter

@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Oct 12, 2023
@algorithms-keeper algorithms-keeper bot removed the tests are failing Do not merge until tests pass label Oct 12, 2023
@Baron105
Copy link
Contributor Author

Incorporated all the suggestions in the file. Please check @tianyizheng02. Thank you for the detailed review.

Comment on lines 24 to 26
---------------------
| vavg = √8RT/πM |
---------------------
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
---------------------
| vavg =8RT/πM |
---------------------
---------------------
| vavg =(8RT/πM) |
---------------------

For clarity

Comment on lines 32 to 34
---------------------
| vmp = √2RT/M |
---------------------
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
---------------------
| vmp =2RT/M |
---------------------
---------------------
| vmp =(2RT/M) |
---------------------

Comment on lines 40 to 42
---------------------
| vrms = √3RT/m |
---------------------
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
---------------------
| vrms =3RT/m |
---------------------
---------------------
| vrms =(3RT/M) |
---------------------

| vmp = √2RT/M |
---------------------

The root-mean-square speed is another measure of the average speed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No function for the RMS speed?

Comment on lines 2 to 5
The root-mean-square, average and most probable speeds are derived from
the Maxwell-Boltzmann distribution. The Maxwell-Boltzmann distribution is a
probability distribution that describes the distribution of speeds for particles
in a gas.
Copy link
Contributor

@tianyizheng02 tianyizheng02 Oct 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The root-mean-square, average and most probable speeds are derived from
the Maxwell-Boltzmann distribution. The Maxwell-Boltzmann distribution is a
probability distribution that describes the distribution of speeds for particles
in a gas.
The root-mean-square, average and most probable speeds of gas molecules are
derived from the Maxwell-Boltzmann distribution. The Maxwell-Boltzmann
distribution is a probability distribution that describes the distribution of
speeds of particles in an ideal gas.

A few minor additions for clarity

@Baron105
Copy link
Contributor Author

Sure, adding the above documentation changes.

I did not add the rms function as there is already a file that defines the function ie physics/rms_speed_of_molecule.py as that would be repeating the function. Should I add it here for a complete comparison of all three speeds?

@Baron105
Copy link
Contributor Author

@tianyizheng02 please check if the current file is ok

@tianyizheng02
Copy link
Contributor

I did not add the rms function as there is already a file that defines the function ie physics/rms_speed_of_molecule.py as that would be repeating the function. Should I add it here for a complete comparison of all three speeds?

No need, I forgot that we already had a file for the RMS speed. However, it'd make more sense in my opinion to move the existing RMS speed code into your code file since they're all about gas molecule speeds—that could be work for a later PR.

@tianyizheng02 tianyizheng02 merged commit 0b2c9fb into TheAlgorithms:master Oct 14, 2023
@algorithms-keeper algorithms-keeper bot removed the awaiting reviews This PR is ready to be reviewed label Oct 14, 2023
@tianyizheng02
Copy link
Contributor

Thanks for your contribution!

@Baron105
Copy link
Contributor Author

Thanks for your contribution!

Glad to contribute! Thank you for taking time to review the file and giving great advice!

sedatguzelsemme pushed a commit to sedatguzelsemme/Python that referenced this pull request Sep 15, 2024
* avg and mps speed formulae added

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* avg and mps speed formulae added

* fixed_spacing

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* spacing

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* ws

* added amicable numbers

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* added amicable numbers

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* spacing

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* removed

* changed name of file and added code improvements

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* issues fixed due to pi

* requested changes added

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
@isidroas isidroas mentioned this pull request Jan 25, 2025
14 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants