Skip to content

Commit

Permalink
Merge pull request #52 from fab4100/docker
Browse files Browse the repository at this point in the history
Fixing examples Jupyter notebooks and adding Dockerfile
  • Loading branch information
arm61 committed Feb 17, 2021
2 parents aa53927 + 1a6b12c commit 33e5c08
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 29 deletions.
6 changes: 6 additions & 0 deletions docker/.gitignore
@@ -0,0 +1,6 @@
.ipynb*
.cache
.config
.ipython
.jupyter
.local
46 changes: 46 additions & 0 deletions docker/Dockerfile
@@ -0,0 +1,46 @@
FROM ubuntu:20.04

# update aptitude
ARG DEBIAN_FRONTEND=noninteractive
ENV TZ=Europe/Zurich
RUN apt-get update && apt-get -y --fix-missing upgrade

# install aptitude essentials
RUN apt-get -y install \
build-essential \
meson \
cmake \
git \
vim \
curl \
openmpi-bin \
openmpi-common \
libhdf5-openmpi-dev \
python3-dev \
python3-pip \
python3-numpy \
python3-matplotlib \
python3-pandas \
python3-scipy \
python3-xlrd \
python3-ipython \
dirmngr apt-transport-https lsb-release ca-certificates
RUN curl -sL https://deb.nodesource.com/setup_12.x | bash -
RUN apt-get -y install nodejs

RUN python3 -m pip install --upgrade pip
RUN python3 -m pip install jupyter jupyterlab ipympl
RUN python3 -m pip install pylj
RUN jupyter labextension install @jupyter-widgets/jupyterlab-manager
RUN jupyter labextension install jupyter-matplotlib

ENTRYPOINT ["jupyter", "lab"]
CMD ["--ip=0.0.0.0", "--port=8888", "--no-browser"]

# run the container with:
# $ docker run -it --rm -p 8888:8888 <image name>

# add non-root user
RUN useradd -m pylj
WORKDIR /home/pylj
USER pylj
3 changes: 3 additions & 0 deletions docker/build.sh
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
set -e
docker build -f Dockerfile -t 'pythoninchemistry:latest' .
7 changes: 7 additions & 0 deletions docker/run.sh
@@ -0,0 +1,7 @@
#!/usr/bin/env bash
set -e

CWD=$(pwd -P)
docker run \
-u $(id -u):$(id -g) -v ${CWD}:${CWD} -w ${CWD} -e HOME=${CWD} \
-it --rm -p 8888:8888 pythoninchemistry:latest
22 changes: 13 additions & 9 deletions examples/ideal_gas_law/ideal_gas_law.ipynb
Expand Up @@ -6,9 +6,12 @@
"metadata": {},
"outputs": [],
"source": [
"import warnings\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"from pylj import md, comp, util, sample"
"from pylj import md, util, sample\n",
"\n",
"warnings.filterwarnings('ignore')"
]
},
{
Expand All @@ -28,7 +31,7 @@
"\n",
"One of the most popular models used to simulate interparticle interactions is known as the **Lennard-Jones** function. This models the London dispersion interactions between atoms. Hopefully the London dispersion interactions are familiar as consisting of: \n",
"\n",
"- **van der Waals attraction**: the attraction between atoms that occurs as a result of the formation of instantenous dipole formation,\n",
"- **van der Waals attraction**: the attraction between atoms that occurs as a result of the formation of instantaneous dipole formation,\n",
"- **Pauli's exclusion principle**: the repulsion which stops atoms overlapping as no two electrons can have the same quantum state. \n",
"\n",
"This means that the Lennard-Jones function is attractive when particles are close enough to induce dipole formation but **very** repulsive when the particles are too close. The Lennard-Jones function has the following form,\n",
Expand Down Expand Up @@ -69,6 +72,8 @@
"source": [
"r = np.linspace( ◽◽◽, ◽◽◽, ◽◽◽ )\n",
"E = ◽◽◽\n",
"\n",
"%matplotlib widget\n",
"plt.plot( ◽◽◽, ◽◽◽ )\n",
"plt.xlabel( ◽◽◽ )\n",
"plt.ylabel( ◽◽◽ )\n",
Expand Down Expand Up @@ -120,6 +125,8 @@
"\n",
"r = np.linspace( ◽◽◽, ◽◽◽, ◽◽◽ )\n",
"F = ◽◽◽\n",
"\n",
"%matplotlib widget\n",
"plt.plot( ◽◽◽, ◽◽◽ )\n",
"plt.xlabel( ◽◽◽ )\n",
"plt.ylabel( ◽◽◽ )\n",
Expand Down Expand Up @@ -150,7 +157,7 @@
"source": [
"def md_simulation(number_of_particles, temperature, box_length, number_of_steps, sampling_class):\n",
" # Creates the visualisation environment\n",
" %matplotlib notebook\n",
" %matplotlib widget\n",
" # Initialise the system\n",
" system = md.initialise(number_of_particles, temperature, box_length, 'square')\n",
" # This sets the sampling class\n",
Expand All @@ -160,16 +167,13 @@
" # Begin the molecular dynamics loop\n",
" for i in range(0, number_of_steps):\n",
" # At each step, calculate the forces on each particle and get acceleration\n",
" system.particles, system.distances, system.forces, system.energies = comp.compute_forces(system.particles, \n",
" system.box_length, \n",
" system.cut_off)\n",
" system.compute_force() \n",
" # Run the equations of motion integrator algorithm\n",
" system.particles = md.velocity_verlet(system.particles, system.timestep_length, system.box_length, \n",
" system.cut_off)\n",
" system.integrate(md.velocity_verlet)\n",
" # Sample the thermodynamic and structural parameters of the system\n",
" system = md.sample(system.particles, system.box_length, system.initial_particles, system)\n",
" # Allow the system to interact with a heat bath\n",
" system.particles = comp.heat_bath(system.particles, system.temperature_sample, temperature)\n",
" system.heat_bath(temperature)\n",
" # Iterate the time\n",
" system.time += system.timestep_length\n",
" system.step += 1\n",
Expand Down
22 changes: 14 additions & 8 deletions examples/molecular_dynamics/intro_to_molecular_dynamics.ipynb
Expand Up @@ -6,9 +6,12 @@
"metadata": {},
"outputs": [],
"source": [
"import warnings\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"from pylj import md, comp, util, sample, pairwise"
"from pylj import md, util, sample, forcefields\n",
"\n",
"warnings.filterwarnings('ignore')"
]
},
{
Expand Down Expand Up @@ -94,10 +97,12 @@
"outputs": [],
"source": [
"r = np.linspace( 3e-10, 8e-10, 100 )\n",
"E = pairwise.lennard_jones_energy(1.36e-134, 9.27e-78, r)\n",
"E = forcefields.lennard_jones(r, [1.36e-134, 9.27e-78])\n",
"\n",
"%matplotlib widget\n",
"plt.plot( r, E )\n",
"plt.xlabel( 'r/m' )\n",
"plt.ylabel( 'E/J' )\n",
"plt.xlabel( '$r/m$' )\n",
"plt.ylabel( '$E/J$' )\n",
"plt.show()"
]
},
Expand Down Expand Up @@ -142,7 +147,7 @@
"metadata": {},
"outputs": [],
"source": [
"%matplotlib notebook\n",
"%matplotlib widget\n",
"simulation = md.initialise(16, 300, 50, 'square')\n",
"sample_system = sample.JustCell(simulation)"
]
Expand Down Expand Up @@ -194,7 +199,7 @@
"\n",
"$$ \\mathbf{f} = -\\frac{\\partial E}{\\partial r}. $$\n",
"\n",
"The force of a given interaction can be found using the `pairwise.lennard_jones_force` function available in pylj, below plot the force for the interaction between two argon atoms as was completed for the energy above. "
"The force of a given interaction can be found using the `forcefields.lennard(dr, constants, force=True)` function available in pylj, below plot the force for the interaction between two argon atoms as was completed for the energy above. "
]
},
{
Expand All @@ -203,9 +208,10 @@
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"r = ◽◽◽\n",
"f = ◽◽◽\n",
"\n",
"%matplotlib widget\n",
"plt.plot( ◽◽◽, ◽◽◽ )\n",
"plt.xlabel( ◽◽◽ )\n",
"plt.ylabel( ◽◽◽ )\n",
Expand Down Expand Up @@ -281,7 +287,7 @@
"metadata": {},
"outputs": [],
"source": [
"%matplotlib notebook\n",
"%matplotlib widget\n",
"simulation = md.initialise(16, 300, 50, 'square')\n",
"sample_system = sample.JustCell(simulation)\n",
"simulation.compute_force()\n",
Expand Down
27 changes: 17 additions & 10 deletions examples/monte_carlo/intro_to_monte_carlo.ipynb
Expand Up @@ -6,9 +6,12 @@
"metadata": {},
"outputs": [],
"source": [
"import warnings\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"from pylj import mc, comp, util, sample, pairwise"
"from pylj import mc, util, sample, forcefields\n",
"\n",
"warnings.filterwarnings('ignore')"
]
},
{
Expand Down Expand Up @@ -67,9 +70,11 @@
"source": [
"r = np.linspace( ◽◽◽, ◽◽◽, ◽◽◽ )\n",
"E = ◽◽◽\n",
"plt.plot( ◽◽◽, ◽◽◽ )\n",
"plt.xlabel( ◽◽◽ )\n",
"plt.ylabel( ◽◽◽ )\n",
"\n",
"%matplotlib widget\n",
"plt.plot(r, E)\n",
"plt.xlabel(\"$r/m$\")\n",
"plt.ylabel(\"$E/J$\")\n",
"plt.show()"
]
},
Expand All @@ -84,7 +89,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"We are going to use the software pylj. This is a Python library, which means it is a collection of functions to enable the simulation by Monte Carlo. An example of a function in pylj is the `pairwise.lennard_jones_energy`, which can be used similar to the `lennard_jones` function that you have defined above."
"We are going to use the software pylj. This is a Python library, which means it is a collection of functions to enable the simulation by Monte Carlo. An example of a function in pylj is the `forcefields.lennard_jones`, which can be used similar to the `lennard_jones` function that you have defined above."
]
},
{
Expand All @@ -94,7 +99,9 @@
"outputs": [],
"source": [
"r = np.linspace( 3e-10, 8e-10, 100 )\n",
"E = pairwise.lennard_jones_energy(1.36e-134, 9.27e-78, r)\n",
"E = forcefields.lennard_jones(r, [1.36e-134, 9.27e-78])\n",
"\n",
"%matplotlib widget\n",
"plt.plot( r, E )\n",
"plt.xlabel( 'r/m' )\n",
"plt.ylabel( 'E/J' )\n",
Expand Down Expand Up @@ -142,7 +149,7 @@
"metadata": {},
"outputs": [],
"source": [
"%matplotlib notebook\n",
"%matplotlib widget\n",
"simulation = mc.initialise(16, 300, 20, 'square')\n",
"sample_system = sample.JustCell(simulation)"
]
Expand Down Expand Up @@ -176,7 +183,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"pylj has a function for the determination of the energy of each interaction, `pairwise.compute_energy`. This can be run as follows. "
"pylj has a function for the determination of the energy of each interaction, `System.compute_energy`. This can be run as follows. "
]
},
{
Expand Down Expand Up @@ -207,8 +214,8 @@
"metadata": {},
"outputs": [],
"source": [
"random_particle = simulation.select_random_particle()\n",
"simulation.new_random_position(random_particle)"
"simulation.select_random_particle()\n",
"simulation.new_random_position()"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion examples/simple_examples/molecular_dynamics.ipynb
Expand Up @@ -22,7 +22,7 @@
"source": [
"def md_simulation(number_of_particles, temperature, box_length, number_of_steps, sample_frequency):\n",
" # Creates the visualisation environment\n",
" %matplotlib notebook\n",
" %matplotlib widget\n",
" # Initialise the system\n",
" system = md.initialise(number_of_particles, temperature, box_length, 'square')\n",
" # This sets the sampling class\n",
Expand Down
2 changes: 1 addition & 1 deletion examples/simple_examples/monte_carlo.ipynb
Expand Up @@ -22,7 +22,7 @@
"source": [
"def mc_simulation(number_of_particles, temperature, box_length, number_of_steps, sample_frequency):\n",
" # Creates the visualisation environment\n",
" %matplotlib notebook\n",
" %matplotlib widget\n",
" # Initialise the system placing the particles on a square lattice\n",
" system = mc.initialise(number_of_particles, temperature, box_length, 'square')\n",
" # This sets the sampling class as Energy, which shows the energy of the system\n",
Expand Down

0 comments on commit 33e5c08

Please sign in to comment.