In [1]:
%matplotlib notebook
import numpy as np
import matplotlib.pyplot as plt
import particlesim.api
import particlesim.helpers_for_tests
import particlesim.utils.xyz
import particlesim.utils.config_parser
import particlesim.utils.conversion
from particlesim.k_cython import fast_distances, fast_distances_cpdef
from particlesim.neighbouring import NeighbouringCellLinkedLists, NeighbouringPrimitiveLists
import time
from mpl_toolkits.mplot3d import Axes3D
In [3]:
def create_system_conf_with_na_and_cl(num_na, num_cl, box_size = 12, neighbouring=False):
particles = particlesim.helpers_for_tests.create_positions(box_size=box_size,number_of_particles=num_na+num_cl)
na = {'sigma':1.21496,'epsilon':0.0469,'charge':1}
cl = {'sigma':2.02234, 'epsilon':0.15,'charge':-1.0}
return particlesim.api.SystemConfiguration(xyz=particles,
sigmas=([na['sigma']]*num_na)+([cl['sigma']]*num_cl),
epsilons=[na['epsilon']]*num_na+[cl['epsilon']]*num_cl,
charges=[na['charge']]*num_na+[cl['charge']]*num_cl,
box_size=box_size, neighbouring=neighbouring)
In [11]:
"""This is the ENTIRE SHORTRANGE FUNCTION!!! Fast cython neighborlists but ALWAYS N^2 RUNTIME"""
nr_particles, times = [], []
N = 400
for i in range(10,N,N//40):
sys_conf = create_system_conf_with_na_and_cl(i//2,i//2,16,neighbouring=False)
shorty = sys_conf._total_potential.shortrange
st = time.clock()
pot = shorty.shortrange(sys_conf.xyz)
end = time.clock()
times.append(end-st); nr_particles.append(i)
#print(i, "particles, time: ", end-st, " potential= ", pot)
plt.close()
plt.title("Time for Shortrange Potential Function");plt.ylabel("Time in Seconds");plt.xlabel("Number of particles")
plt.plot(nr_particles, times); plt.grid()
In [14]:
"""This is the ENTIRE SHORTRANGE FUNCTION!!! Python CellLL but ALWAYS N^2 RUNTIME"""
nr_particles, times = [], []
N = 100
for i in range(10,N,(N//40)):
# means we're using cell linked lists
sys_conf = create_system_conf_with_na_and_cl(i//2,i//2,16,neighbouring=True)
shorty = sys_conf._total_potential.shortrange
st = time.clock()
pot = shorty.shortrange(sys_conf.xyz)
end = time.clock()
times.append(end-st); nr_particles.append(i)
print(i, "particles, time: ", end-st, " potential= ", pot)
plt.close()
plt.title("Time for Shortrange Potential Function for Cell Linked Lists")
plt.ylabel("Time in Seconds")
plt.xlabel("Number of particles")
plt.plot(nr_particles, times)
plt.grid()
plt.show()
In [17]:
"""This is a Primitive List Neighbour Structure in Python"""
box_len = 5
nr_part = 250
nr_particles, times = [], []
for i in range(10, nr_part, 20):
#dists = np.zeros((i,i))
#pos = np.random.rand(nr_part,3)
pos = np.arange(i*3).reshape(i,3).astype("float")%box_len
st = time.clock()
neigh_list = NeighbouringPrimitiveLists(pos, 1.2, 5)
end = time.clock()
nr_particles.append(i); times.append(end-st)
print("Time: ", end-st)
plt.close()
plt.title("Time for Primitive Cython Lists Computation (without cpdef)")
plt.ylabel("Time in Seconds")
plt.xlabel("Number of particles")
nr_particles = np.array(nr_particles); times = np.array(times)
plt.plot(nr_particles, times)
plt.grid()
plt.show()
In [16]:
"""This is a Primitive List Structure in CYTHON"""
box_len = 5
nr_part = 900
nr_particles, times = [], []
for i in range(10, nr_part, 20):
dists = np.zeros((i,i))
#pos = np.random.rand(nr_part,3)
pos = np.arange(i*3).reshape(i,3).astype("float")%box_len
st = time.clock()
fast_distances_cpdef(pos, box_len, dists)
end = time.clock()
nr_particles.append(i); times.append(end-st)
print("Time: ", end-st)
plt.close()
plt.title("Time for Primitive Cython Lists Computation")
plt.ylabel("Time in Seconds")
plt.xlabel("Number of particles")
nr_particles = np.array(nr_particles); times = np.array(times)
plt.plot(nr_particles, times)
plt.grid()
plt.show()
In [13]:
"""Achieved Speedup"""
box_len = 5
nr_part = 300
nr_particles, times = [], []
for i in range(10, nr_part, 20):
dists = np.zeros((i,i))
#pos = np.random.rand(nr_part,3)
pos = np.arange(i*3).reshape(i,3).astype("float")%box_len
st = time.clock()
fast_distances_cpdef(pos, box_len, dists)
end = time.clock()
nr_particles.append(i); times.append(end-st)
st = time.clock()
neigh_list = NeighbouringPrimitiveLists(pos, 1.2, box_len)
end = time.clock()
times[-1] = (end-st)*1.0/times[-1]
print("Time: ", end-st)
plt.close()
plt.title("Speedup for Varying Numbers of Particles")
plt.ylabel("Time in Seconds")
plt.xlabel("Number of particles")
nr_particles = np.array(nr_particles); times = np.array(times)
plt.plot(nr_particles, times)
plt.grid()
plt.show()
In [36]:
"""This is a CELL LINKED List Neighbour Structure in Python"""
box_len = 10
nr_part = 2000
nr_particles, times = [], []
for i in range(10, nr_part, 20):
#dists = np.zeros((i,i))
#pos = np.random.rand(nr_part,3)*box_len
pos = np.arange(i*3).reshape(i,3).astype("float")%box_len
st = time.clock()
neigh_list = NeighbouringCellLinkedLists(pos, 1.2, box_len)
end = time.clock()
nr_particles.append(i); times.append(end-st)
print("Time: ", end-st)
plt.close()
plt.title("Time for Cell Linked List Structure")
plt.ylabel("Time in Seconds")
plt.xlabel("Number of particles")
nr_particles = np.array(nr_particles); times = np.array(times)
times2 = (times[2:]+times[1:-1]+times[:-2])/3.0
#plt.plot(nr_particles[:-2], times2)
plt.plot(nr_particles, times)
plt.grid()
plt.show()