NumPy to the rescue! No more Loops.
NumPy Library
We started the day with the introduction of a Python library called NumPy. We used it to simplify the operations we have been working with. As shown during our lecture and group work today, NumPy is very powerful. Another one of its benefits is efficiency. We compared the Python library definitions of functions to the ones we modified to use NumPy, and we observed significant performance gains. Under the hood NumPy uses C, making it faster than the standard Python implementation.
NumPy Arrays
One of the benefits of NumPy is how clean the code becomes. To work with NumPy we had to convert our lists to numpy.ndarray objects. The previous days we had been storing the coordinates in lists. NumPy allows us to initialize a numpy.ndarray with a list. Thus it was easy to transform. Array operations using the NumPy library are concise and intuitive. For example, all we needed to do to multiply one array with another was to literally multiply one by the other. We were able to remove embedded 'for loops'. NumPy takes care of the heavy lifting for us so there is no need to loop through the elements in the array.
a1 = np.array([2, 1, 0])
a2 = np.array([1, 3, 5])
a1 * a2
Note: It is important to understand that in multidimensional arrays shape matters, meaning to perform operations on multiple arrays they need to be compatible.
Discussion
Today we had our discussion session in different groups. Kevin and I joined with Joyce to talk about the NumPy arrays and how we could leverage them in the Monte Carlo Simulation.
We were already given the calculate_distance_np to start with but we still needed to update some other functions to be able to use the library.
def calculate_distance_np(coord1, coord2, box_length=None):
"""
Calculate the distance between two points.
When box_length is set, the minimum image convention is
used to calculate the distance between the points.
Parameters
----------
coord1, coord2 : np.array
The coordinates of the points, [x, y, z]
box_length : float, optional
The box length
Returns
-------
distance : float
The distance between the two points accounting for periodic boundaries
"""
coord_dist=coord1-coord2
if box_length:
coord_dist = coord_dist - (box_length * np.round(coord_dist/box_length))
if coord_dist.ndim <2:
#reshaping the array, make it have 2 dimensions
coord_dist = coord_dist.reshape (1,-1)
coord_dist=coord_dist**2
dist_sum= coord_dist.sum(axis=1)
distance = np.sqrt(dist_sum)
return distance
We considered the different places and functions where using NumPy would make a difference. Since some of the functions are handling the operations done on the large number of coordinates and looping through them, they were a good starting point.
We concluded that we needed to create the following based on their standard implementation
- calculate_pair_energy_np
- calculate_total_energy_np
- and possibly the calculate_lj_np
Implementation
Once we moved back to our original team2 members we implemented the solutions. For the top two, we were able to remove the 'for loops', but to do that we had to modify the calculate_LJ to take in an np array.
Comparing run time
During our morning lecture, we also learned about calculating elapsed time and comparing the two implementations to validate the efficiency of NumPy. Again we confirmed that using NumPy does offer a performance gain.