Objective: For the system of clusters we were asked to find the center of mass of a system of clusters. Each cluster contained many particles of identical mass. The theory behind our code and our logic was based on the center of mass equations. Because center of mass allows one to treat any object, shape, as if the mass is concentrated at its center of mass. We used this logic to understand that if we could treat each individual cluster by its center of mass as opposed to using all of its points. Afterwards we were able to use the coordinates of the center of masses of each cluster and find the center of mass of the entire system. The other way to achieve this would have been to take the center of mass of all the individual particles of every cluster at once. The reason these two methods work is because you can equate an object to its center of mass.
def Center_Of_Mass(filename):
"""This fuction takes as input a file with the coordinates of the points
and returns the coordinates of the center of mass of the distribution.
PARAMETERS:
===========
filename = Name of the file that has the coordinates of the points.
RETURNS:
========
A tuple with the coordinates of the center of mass and the coordinates of
all the points in the distribution.
"""
# Three arrays that hold values for X,Y, and Z coordinates of given particles.
xval = []
yval = []
zval = []
# Opens particle distribution file
f = open(filename, "r")
# Calculates center of mass for each column of given particle distribution and places it in according arrays
for line in f:
numbers = line.split()
x = numbers[0]
y = numbers[1]
z = numbers[2]
xval.append(float(x))
yval.append(float(y))
zval.append(float(z))
sumX = sum(xval)
sumY = sum(yval)
sumZ = sum(zval)
totalMassX = len(xval)
totalMassY = len(yval)
totalMassZ = len(zval)
comX = sumX / totalMassX
comY = sumY / totalMassY
comZ = sumZ / totalMassZ
#returns center of mass and every value in specified array
return (comX, comY, comZ, xval, yval, zval)
# Creates 3D plot
from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
%matplotlib notebook
%matplotlib notebook
# Uses function to find center of mass and plots that center of mass with red dot.
fig = plt.figure()
ax = plt.axes(projection="3d")
coX, coY, coZ, x_points, y_points, z_points, = Center_Of_Mass('spherical_distribution.txt')
ax.scatter3D(coX, coY, coZ, c='red', cmap='hsv');
# Plots center of mass of each particle given and adds it to an array.
coX, coY, coZ, x_points, y_points, z_points, = Center_Of_Mass('spherical_distribution_0.txt')
ax.scatter3D(coX, coY, coZ, c='blue', cmap='hsv');
ax.scatter3D(x_points, y_points, z_points, c='gray', cmap='hsv',alpha = 0.05);
xcox = []
ycoy = []
zcoz = []
xcox.append(coX)
ycoy.append(coY)
zcoz.append(coZ)
coX, coY, coZ, x_points, y_points, z_points, = Center_Of_Mass('spherical_distribution_1.txt')
ax.scatter3D(coX, coY, coZ, c='blue', cmap='hsv');
ax.scatter3D(x_points, y_points, z_points, c='gray', cmap='hsv',alpha = 0.05);
xcox.append(coX)
ycoy.append(coY)
zcoz.append(coZ)
coX, coY, coZ, x_points, y_points, z_points, = Center_Of_Mass('spherical_distribution_2.txt')
ax.scatter3D(coX, coY, coZ, c='blue', cmap='hsv');
ax.scatter3D(x_points, y_points, z_points, c='gray', cmap='hsv',alpha = 0.05);
xcox.append(coX)
ycoy.append(coY)
zcoz.append(coZ)
coX, coY, coZ, x_points, y_points, z_points, = Center_Of_Mass('spherical_distribution_3.txt')
ax.scatter3D(coX, coY, coZ, c='blue', cmap='hsv');
ax.scatter3D(x_points, y_points, z_points, c='gray', cmap='hsv',alpha = 0.05);
xcox.append(coX)
ycoy.append(coY)
zcoz.append(coZ)
coX, coY, coZ, x_points, y_points, z_points, = Center_Of_Mass('spherical_distribution_4.txt')
ax.scatter3D(coX, coY, coZ, c='blue', cmap='hsv');
ax.scatter3D(x_points, y_points, z_points, c='gray', cmap='hsv',alpha = 0.05);
xcox.append(coX)
ycoy.append(coY)
zcoz.append(coZ)
coX, coY, coZ, x_points, y_points, z_points, = Center_Of_Mass('spherical_distribution_5.txt')
ax.scatter3D(coX, coY, coZ, c='blue', cmap='hsv');
ax.scatter3D(x_points, y_points, z_points, c='gray', cmap='hsv',alpha = 0.05);
xcox.append(coX)
ycoy.append(coY)
zcoz.append(coZ)
coX, coY, coZ, x_points, y_points, z_points, = Center_Of_Mass('spherical_distribution_6.txt')
ax.scatter3D(coX, coY, coZ, c='blue', cmap='hsv');
ax.scatter3D(x_points, y_points, z_points, c='gray', cmap='hsv',alpha = 0.05);
xcox.append(coX)
ycoy.append(coY)
zcoz.append(coZ)
coX, coY, coZ, x_points, y_points, z_points, = Center_Of_Mass('spherical_distribution_7.txt')
ax.scatter3D(coX, coY, coZ, c='blue', cmap='hsv');
ax.scatter3D(x_points, y_points, z_points, c='gray', cmap='hsv',alpha = 0.05);
xcox.append(coX)
ycoy.append(coY)
zcoz.append(coZ)
coX, coY, coZ, x_points, y_points, z_points, = Center_Of_Mass('spherical_distribution_8.txt')
ax.scatter3D(coX, coY, coZ, c='blue', cmap='hsv');
ax.scatter3D(x_points, y_points, z_points, c='gray', cmap='hsv',alpha = 0.05);
xcox.append(coX)
ycoy.append(coY)
zcoz.append(coZ)
coX, coY, coZ, x_points, y_points, z_points, = Center_Of_Mass('spherical_distribution_9.txt')
ax.scatter3D(coX, coY, coZ, c='blue', cmap='hsv');
ax.scatter3D(x_points, y_points, z_points, c='gray', cmap='hsv',alpha = 0.05);
xcox.append(coX)
ycoy.append(coY)
zcoz.append(coZ)
# Calculates center of mass of accumulated center of masses of each particle given.
comsumX = sum(xcox)
comsumY = sum(ycoy)
comsumZ = sum(zcoz)
comtotalMassX = len(xcox)
comtotalMassY = len(ycoy)
comtotalMassZ = len(zcoz)
com_of_x = comsumX / comtotalMassX
com_of_y = comsumY / comtotalMassY
com_of_z = comsumZ / comtotalMassZ
#Plots that center of mass of center of masses with green dot
ax.scatter3D(com_of_x, com_of_y, com_of_z, c='green', cmap='hsv');
Objective: The objective was to find the center of mass of a spherical distribution. This was done by break each vector to its vector components;x,y,z. Afterwards we calculated the center of mass coordinate for each vector component. Due to each mass being identical the total mass can be represent by the total number of particles in the spherical distribution. By doing this we were able to arrive at the center of mass.
def Center_Of_Mass(filename):
"""This fuction takes as input a file with the coordinates of the points
and returns the coordinates of the center of mass of the distribution.
PARAMETERS:
===========
filename = Name of the file that has the coordinates of the points.
RETURNS:
========
A tuple with the coordinates of the center of mass and the coordinates of
all the points in the distribution.
"""
xval = []
yval = []
zval = []
f = open(filename, "r")
for line in f:
numbers = line.split()
x = numbers[0]
y = numbers[1]
z = numbers[2]
xval.append(float(x))
yval.append(float(y))
zval.append(float(z))
sumX = sum(xval)
sumY = sum(yval)
sumZ = sum(zval)
totalMassX = len(xval)
totalMassY = len(yval)
totalMassZ = len(zval)
comX = sumX / totalMassX
comY = sumY / totalMassY
comZ = sumZ / totalMassZ
return (comX, comY, comZ, xval, yval, zval)
from mpl_toolkits import mplot3d
import numpy as np
import matplotlib.pyplot as plt
%matplotlib notebook
%matplotlib notebook
fig = plt.figure()
ax = plt.axes(projection="3d")
coX, coY, coZ, x_points, y_points, z_points, = Center_Of_Mass('spherical_distribution_1.txt')
ax.scatter3D(coX, coY, coZ, c='red', cmap='hsv');
ax.scatter3D(x_points, y_points, z_points, c='purple', cmap='hsv',alpha = 0.05);