Skip to content

adding new physics algorithm: center of mass #10743

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 5 commits into from
Oct 21, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions physics/center_of_mass.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
"""
Calculating the center of mass for a discrete system of particles,
given their positions and masses.

Description:

In physics, the center of mass of a distribution of mass in space
(sometimes referred to as the barycenter or balance point) is the
unique point at any given time where the weighted relative position
of the distributed mass sums to zero. This is the point to which a
force may be applied to cause a linear acceleration without an angular acceleration.
Calculations in mechanics are often simplified when formulated with respect to
the center of mass. It is a hypothetical point where the entire mass of an object
may be assumed to be concentrated to visualise its motion. In other words, the
center of mass is the particle equivalent of a given object for application of
Newton's laws of motion.

In the case of a system of particles P_i, i = 1, ..., n , each with mass m_i
that are located in space with coordinates r_i, i = 1, ..., n , the coordinates
R of the center of mass correspond to:

R = (Σ(mi * ri) / Σ(mi))

Reference: https://en.wikipedia.org/wiki/Center_of_mass#:~:text=The%20center%20of%20mass%20is%20the%20unique%20point%20at%20the,distribution%20of%20mass%20in%20space.

"""


def center_of_mass(
particles: list[tuple[float, float, float, float]]
) -> tuple[float, float, float]:
"""
Input Parameters
----------------
particles (List[Tuple[float, float, float, float]]):
A list of particles where each particle is a tuple with it´s (x, y, z)
position and it´s mass.

Returns
-------
Tuple[float, float, float]:
A tuple with de coordinates of the center of mass (Xcm, Ycm, Zcm)
rounded to two decimal places.

Examples
--------

>>> center_of_mass([(1.5, 4, 3.4, 4), (5, 6.8, 7, 8.1), (9.4, 10.1, 11.6, 12.9)])
(6.71, 8.05, 8.8)


>>> center_of_mass([(1, 2, 3, 4), (5, 6, 7, 8), (9, 10, 11, 12)])
(6.33, 7.33, 8.33)

>>> center_of_mass([(1, 2, 3, -4), (5, 6, 7, 8), (9, 10, 11, 12)])
Traceback (most recent call last):
...
ValueError: Mass of all particles must be greater than 0

>>> center_of_mass([(1, 2, 3, 0), (5, 6, 7, 8), (9, 10, 11, 12)])
Traceback (most recent call last):
...
ValueError: Mass of all particles must be greater than 0

>>> center_of_mass([])
Traceback (most recent call last):
...
ValueError: No particles provided

"""
if not particles:
raise ValueError("No particles provided")

for particle in particles:
if particle[3] <= 0:
raise ValueError("Mass of all particles must be greater than 0")

total_mass = sum(particle[3] for particle in particles)

center_of_mass_x = round(
sum(particle[0] * particle[3] for particle in particles) / total_mass, 2
)
center_of_mass_y = round(
sum(particle[1] * particle[3] for particle in particles) / total_mass, 2
)
center_of_mass_z = round(
sum(particle[2] * particle[3] for particle in particles) / total_mass, 2
)
return (center_of_mass_x, center_of_mass_y, center_of_mass_z)


if __name__ == "__main__":
import doctest

doctest.testmod()