Skip to content

Commit

Permalink
Merge pull request #39 from arm61/numba
Browse files Browse the repository at this point in the history
Numba
  • Loading branch information
Andrew McCluskey authored Dec 19, 2018
2 parents 5276234 + db3d102 commit 3baab17
Show file tree
Hide file tree
Showing 15 changed files with 548 additions and 397 deletions.
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
# built documents.
#
# The short X.Y version.
version = '1.2.0'
version = '1.2.1'
# The full version, including alpha/beta/rc tags.
release = version

Expand Down
21 changes: 13 additions & 8 deletions pylj/forcefields.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import numpy as np
from numba import jit


@jit
def lennard_jones(dr, constants, force=False):
r"""Calculate the energy or force for a pair of particles using the
Lennard-Jones (A/B variant) forcefield.
Expand All @@ -27,13 +29,14 @@ def lennard_jones(dr, constants, force=False):
The potential energy or force between the particles.
"""
if force:
return 12 * constants[0] * np.power(dr, -13) - (6 * constants[1] *
np.power(dr, -7))
return 12 * constants[0] * np.power(dr, -13) - (
6 * constants[1] * np.power(dr, -7)
)
else:
return constants[0] * np.power(dr, -12) - (constants[1] *
np.power(dr, -6))
return constants[0] * np.power(dr, -12) - (constants[1] * np.power(dr, -6))


@jit
def buckingham(dr, constants, force=False):
r""" Calculate the energy or force for a pair of particles using the
Buckingham forcefield.
Expand All @@ -60,8 +63,10 @@ def buckingham(dr, constants, force=False):
the potential energy or force between the particles.
"""
if force:
return constants[0] * constants[1] * np.exp(-constants[1] * dr) - \
6 * constants[2] / np.power(dr, 7)
return constants[0] * constants[1] * np.exp(-constants[1] * dr) - 6 * constants[
2
] / np.power(dr, 7)
else:
return constants[0] * np.exp(-constants[1] * dr) \
- constants[2] / np.power(dr, 6)
return constants[0] * np.exp(-constants[1] * dr) - constants[2] / np.power(
dr, 6
)
43 changes: 29 additions & 14 deletions pylj/mc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@
from pylj import forcefields as ff


def initialise(number_of_particles, temperature, box_length, init_conf,
mass=39.948, constants=[1.363e-134, 9.273e-78],
forcefield=ff.lennard_jones):
def initialise(
number_of_particles,
temperature,
box_length,
init_conf,
mass=39.948,
constants=[1.363e-134, 9.273e-78],
forcefield=ff.lennard_jones,
):
"""Initialise the particle positions (this can be either as a square or
random arrangement), velocities (based on the temperature defined, and #
calculate the initial forces/accelerations.
Expand Down Expand Up @@ -34,11 +40,18 @@ def initialise(number_of_particles, temperature, box_length, init_conf,
System information.
"""
from pylj import util
system = util.System(number_of_particles, temperature, box_length,
constants, forcefield, mass,
init_conf=init_conf)
system.particles['xvelocity'] = 0
system.particles['yvelocity'] = 0

system = util.System(
number_of_particles,
temperature,
box_length,
constants,
forcefield,
mass,
init_conf=init_conf,
)
system.particles["xvelocity"] = 0
system.particles["yvelocity"] = 0
return system


Expand Down Expand Up @@ -87,8 +100,10 @@ def select_random_particle(particles):
The current position of the chosen particle.
"""
random_particle = np.random.randint(0, particles.size)
position_store = [particles['xposition'][random_particle],
particles['yposition'][random_particle]]
position_store = [
particles["xposition"][random_particle],
particles["yposition"][random_particle],
]
return random_particle, position_store


Expand All @@ -110,8 +125,8 @@ def get_new_particle(particles, random_particle, box_length):
Information about the particles, updated to account for the change of
selected particle position.
"""
particles['xposition'][random_particle] = np.random.uniform(0, box_length)
particles['yposition'][random_particle] = np.random.uniform(0, box_length)
particles["xposition"][random_particle] = np.random.uniform(0, box_length)
particles["yposition"][random_particle] = np.random.uniform(0, box_length)
return particles


Expand Down Expand Up @@ -149,8 +164,8 @@ def reject(position_store, particles, random_particle):
Information about the particles, with the particle returned to the
original position
"""
particles['xposition'][random_particle] = position_store[0]
particles['yposition'][random_particle] = position_store[1]
particles["xposition"][random_particle] = position_store[0]
particles["yposition"][random_particle] = position_store[1]
return particles


Expand Down
Loading

0 comments on commit 3baab17

Please sign in to comment.