Molecular Dynamic Simulations in JavaScriptdaniel-kunin.com/img/reports/Molecular Dynamic... · of...

13
Molecular Dynamic Simulations in JavaScript Daniel Kunin Abstract Using a Newtonian mechanical model for potential energy and a force- based layout scheme for graph drawing I investigated the viability and accu- racy of a web-based molecular dynamics visualization. I implemented such a simulation in JavaScript for diatomic molecules and suggested improvements moving forward to expand the range and effectiveness of such a visualization. supervised by Dr. Matthew Harrison February 1, 2017

Transcript of Molecular Dynamic Simulations in JavaScriptdaniel-kunin.com/img/reports/Molecular Dynamic... · of...

Page 1: Molecular Dynamic Simulations in JavaScriptdaniel-kunin.com/img/reports/Molecular Dynamic... · of time it will stay stable is also random. The simulation approach using a bond length

Molecular Dynamic Simulations inJavaScript

Daniel Kunin

Abstract

Using a Newtonian mechanical model for potential energy and a force-based layout scheme for graph drawing I investigated the viability and accu-racy of a web-based molecular dynamics visualization. I implemented such asimulation in JavaScript for diatomic molecules and suggested improvementsmoving forward to expand the range and effectiveness of such a visualization.

supervised by

Dr. Matthew Harrison

February 1, 2017

Page 2: Molecular Dynamic Simulations in JavaScriptdaniel-kunin.com/img/reports/Molecular Dynamic... · of time it will stay stable is also random. The simulation approach using a bond length

Contents

1 Introduction 2

2 Model 22.1 Potential Energy . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32.2 Determining Constants . . . . . . . . . . . . . . . . . . . . . . . . . 32.3 Equation for Molecular Motion . . . . . . . . . . . . . . . . . . . . 42.4 Initial Parameters . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

2.4.1 Position . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42.4.2 Velocity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42.4.3 Charge, Mass, and Atomic Radius . . . . . . . . . . . . . . 5

2.5 Velocity Verlet Algorithm . . . . . . . . . . . . . . . . . . . . . . . 5

3 Simulation 53.1 Implementation in Matlab . . . . . . . . . . . . . . . . . . . . . . . 53.2 Transitioning to JavaScript . . . . . . . . . . . . . . . . . . . . . . 73.3 Frequency and Amplitude . . . . . . . . . . . . . . . . . . . . . . . 83.4 Effect of Temperature . . . . . . . . . . . . . . . . . . . . . . . . . 9

4 Improvements 10

5 Conclusion 11

7 Bibliography 12

1

Page 3: Molecular Dynamic Simulations in JavaScriptdaniel-kunin.com/img/reports/Molecular Dynamic... · of time it will stay stable is also random. The simulation approach using a bond length

APMA 1860 – Fall 2016 Page 2 of 12

1 Introduction

For my senior capstone project in Graphs and Networks, I will create a browser-based, dynamic, 3-dimensional simulation of organic molecules. Organic molecule’scan be thought of as graph networks where nodes represent atoms in the moleculeand edges are covalent bonds between these atoms. In fact, chemical graph theoryis a well established field that uses algorithms and insights of graph theory andapplies it to molecular interactions.

Inspired by energy based algorithms for visualizing networks, my goal is to aug-ment the classic ball and stick representation of organic molecules to reflect thedynamic relationship of atoms. Using a force-based layout scheme for graph draw-ing, I will create a 3-dimensional simulation that accurately displays the constantharmonic movement of molecules around their numerous local energy minimums.The major changes I will be implementing to the classic force-based layout schemeare:

1. Defining the energy function according to the physics of intramolecularforces

2. Removing the decay or friction term such that the graph (molecule) neverreaches a stable equilibrium

3. Visualizing the position vectors of each atom in three dimensions as thesimulation runs

I will be designing the project as a front-end web application such that it can berun entirely on a web browser. Web technologies are by far the most effectivemeans of getting software utilized. While there already exists many moleculardynamics simulation softwares, very few of these are accessible or completelyweb-based. Recent advancements in web technologies allow for complex physicssimulations, such as this project, to be run entirely on a user’s browser while notforfeiting complexity.

2 Model

I will use a Newtonian mechanical model for the potential energy and molecularmotion of a molecule. Additionally, I will use the Velocity Verlet algorithm tosolve the initial value problem for the motion of atoms in a molecule. Below is amore detailed explanation of this method and necessary definitions.

2

Page 4: Molecular Dynamic Simulations in JavaScriptdaniel-kunin.com/img/reports/Molecular Dynamic... · of time it will stay stable is also random. The simulation approach using a bond length

APMA 1860 – Fall 2016 Page 3 of 12

2.1 Potential Energy

The potential energy of a molecule can be represented as the sum of five differentpotential energy functions [3].

E = Ebond + Eangle + Etorsion + Eelectro + Evdw

Below are the definitions for these five potential energy functions:

Ebond =∑

(i,j)∈Sbonds

kbij(rij − r0ij)2

Eangle =∑

(i,j,k)∈Sangle

kaijk(αijk − α0ijk)2

Etorsion =∑

(i,j,k,l)∈Storsion

ktijkl[1 + cos(nαijkl − α0ijkl)]

Eelectro =∑

(i,j)∈Selectro

qiqjeijrij

Evdw =∑

(i,j)∈Svdw

εij[(σijrij

)12 − 2(σijrij

)6]

I will start by focusing on the energy functions above that depend only on pairsof atoms (Ebond, Eelectro, Evdw). This will be sufficient for modeling diatomicmolecules such as halogen molecules (i.e. H2, F2, Cl2, Br2, I2) or heteronuclearmolecules (i.e. CO,NO). From here I can then add the energy functions that de-pend on triplets and quadruplets of atoms (Eangle, Etorsion), enabling simulationsof more complicated molecules.

2.2 Determining Constants

Determining the correct constants used in the potential energy functions is es-sential for the accurateness of the simulation. These constants depend on thespecific atoms in the molecule and many of the constants are unknown or roughlyapproximated through experimentation. Because I will be focusing on diatomicmolecules, I will start by defining the following constants: kbij , eij , εij , σij .

• kbij - I will approximate this constant through Badger’s Rule. Badger’s Ruleis an expression for approximating the bond force constant for diatomicmolecules [1].

3

Page 5: Molecular Dynamic Simulations in JavaScriptdaniel-kunin.com/img/reports/Molecular Dynamic... · of time it will stay stable is also random. The simulation approach using a bond length

APMA 1860 – Fall 2016 Page 4 of 12

• eij - I will make the assumption that this constant is Coulombs Constant(8.98755 ∗ 109Nm2/C2) for all pairs of molecules.

• εij , σij - I will determine these constants (Lennard-Jones parameters) frompublished experimental results [2].

2.3 Equation for Molecular Motion

Using Newton’s Second Law (F = ma), we can define the molecular motion ofeach atom in the molecule as the following.

mix′′i = fi(x1, x2, ..., xn)

mix′′i = −∂E

∂xi

wheremi = mass of atom ixi = position vector of atom i

If we define the potential energy function only in terms of Ebond, Eelectro, and Evdw,we can then evaluate the potential energy as a function of rij , the distance betweenatoms i and j. Under this scheme, the potential energy function is partiallyseparable, and we can define the partial derivates of E as the following:

∂E

∂xk=

n∑j=k+1

E′(rjk)xkrjk

−k∑

i=1

E′(rik)xkrik

2.4 Initial Parameters

By defining the initial position(x0i ) and velocity(v0i ) of all atoms in the molecule,their will be a unique solution to the equation of molecular motion defined above.

2.4.1 Position

Using Protein Data Bank (PDB) files, I will start all atoms at their experimentallydetermined position in the molecule’s global minimum energy conformer.

x0i = PDB coordinate vector

2.4.2 Velocity

I will determine the initial velocity of each atom in the molecule using a GaussianDistribution such that the expected kinetic energy of the entire molecule reflects

4

Page 6: Molecular Dynamic Simulations in JavaScriptdaniel-kunin.com/img/reports/Molecular Dynamic... · of time it will stay stable is also random. The simulation approach using a bond length

APMA 1860 – Fall 2016 Page 5 of 12

the current temperature of the simulation [3].

v0ii.i.d.∼ N(0,

√3kBT

mi)

kB = Boltzmann constantT = temperature in Kelvinmi = mass of atom i

2.4.3 Charge, Mass, and Atomic Radius

In addition to the initial position and velocity of each atom in the molecule, Iwill also need the charge, mass and atomic radius. The first two parameters(charge and mass) will be used when evaluating the energy functions. The latterparameter (atomic radius) will be used when visualizing the molecule. To obtainthese parameters I will use the following Wikipedia data sources: charge, mass,atomic radius.

2.5 Velocity Verlet Algorithm

The Velocity Verlet algorithm is an algorithm for efficient numerical integration.This algorithm is commonly used when describing the motion of particles in New-tonian physics given initial parameters. Because atomic movements are very rapid,the algorithm must be run with a very small time step, on the order of a fem-tosecond (1.0 ∗ 10−15 second) [3].

xk+1i = xki + hvki +

h2fki2mi

vk+1i = vki +

h(fki + fk+1i )

2mi

where

h = time-stepmi = mass of atom ixki = xi(tk)vki = vi(tk)

fki =∂E

∂xkitk = t0 + kh

The Velocity Verlet Algorithm has a third-order accuracy for the calculations ofposition and velocity of particles and exhibits relatively good stability over longtime frames [3].

3 Simulation

3.1 Implementation in Matlab

For the initial implementation of the Verlet Algorithm and energy functions Iwrote the code in Matlab. This allowed me to easily investigate the functionality

5

Page 7: Molecular Dynamic Simulations in JavaScriptdaniel-kunin.com/img/reports/Molecular Dynamic... · of time it will stay stable is also random. The simulation approach using a bond length

APMA 1860 – Fall 2016 Page 6 of 12

of my implementation by plotting the potential, kinetic and total energy andaverage bond length of the molecule as a function of time. Upon initial inspectionI realized that the constants I was using for the van der Waals potential energy(Evdw) were incorrect and caused the total potential energy to be on a completelydifferent scale than the kinetic energy. Unable to find more accurate parameters Idecided to omit van der Waals energy (Evdw) from my simulation. The followingsimulation results rely only on Ebond and Eelectro and use a time step of 1.0∗10−16seconds.

Figure 1: Plot of potential and kinetic energy (left) and total energy and averagebond length (right) for a 5∗10−14 second simulation of a hydrogen molecule (H2)at 100 Kelvin.

As seen above, the kinetic energy, potential energy and average bond length areall displaying harmonic motion as would be expected. Unexpectedly, the to-tal energy is also displaying harmonic motion. Ideally, we would see an inverserelationship between potential and kinetic energy such that the total energy isconstant. However, in the simulation above the total energy of the molecule iscertainly oscillating and decreasing. This most likely due to the fact that theVelocity Verlet algorithm does not perfectly determine position and velocity, thevolume and energy of the molecule will change throughout the simulation [3].Eventually, the molecule will become unstable, causing it to either collapse in onitself or split. By putting a minimum and maximum threshold on the averagebond length, then the simulation can run for as long as the molecule stays stable.Below is the result of the same hydrogen gas simulation using this new approach.

6

Page 8: Molecular Dynamic Simulations in JavaScriptdaniel-kunin.com/img/reports/Molecular Dynamic... · of time it will stay stable is also random. The simulation approach using a bond length

APMA 1860 – Fall 2016 Page 7 of 12

Figure 2: Plot of total energy and average bond length as a function of time fora hydrogen molecule simulation at 100 Kelvin bound by a scalar threshold of 0.5and 2 of the original bond length.

Using this new approach the above hydrogen simulation ran for nearly 3, 500 iter-ations before loosing stability. Because each simulation is stochastic the amountof time it will stay stable is also random. The simulation approach using a bondlength threshold instead of a time interval is the only way to let simulations runfor as long as possible while still providing meaningful results.

3.2 Transitioning to JavaScript

After confirming that the simulation was functioning in Matlab, I transitioned tovisualizing the position vectors of each atom using JavaScript, an open-source,front-end language for web development.

Figure 3: Image of Fluorine molecule from current site.

I adapted an open source template for 3D rendered molecules and uploaded thediatomic molecules I wanted to investigate. I then re-implemented the simulationin JavaScript and added a method for updating the position of each atom. To dothis I used three JavaScript libraries extensively:

7

Page 9: Molecular Dynamic Simulations in JavaScriptdaniel-kunin.com/img/reports/Molecular Dynamic... · of time it will stay stable is also random. The simulation approach using a bond length

APMA 1860 – Fall 2016 Page 8 of 12

• three.js - A JS library used to render 3D graphics in WebGL.

• math.js - A JS library that supplements JavaScript with many mathemat-ical functions and data types.

• jStat.js - A JS library that implements advanced statistical operations.

The project is currently hosted on an Amazon server and can be found at the fol-lowing link: http://molecular-dynamics-simulation.s3-website-us-east-1.amazonaws.com

3.3 Frequency and Amplitude

After successfully visualizing the molecular dynamics simulation in JavaScript Istarted to notice patterns in the motion of the molecules. For example, a simula-tion of an iodine molecule (I2) seemed to stay stable for longer than a simulationof a hydrogen molecule (H2). In order to investigate how different moleculesresponded to the simulation I wrote an additional function that computed theaverage frequency and amplitude of the bond length throughout the simulation.There are obvious clusters in this data that corresponds with the molecule beingsimulated.

Figure 4: Ten replicates of average amplitude and frequency of bond length forhomonuclear (left) and heteronuclear (right) molecules at 100 Kelvin simulation.

In addition to the clustering, there are many trends in the data that appear to becorrelated with the molecular makeup of the molecule being simulated. One suchtrend is that as the mass of the simulated molecule increases (H2 → I2), the aver-age frequency of the bond length decreases, except for nitrogen and oxygen wherethe trend is switched. Additionally, all of the halogen molecules (F2, Cl2, Br2, I2)have high amplitudes with the exception of Chlorine (Cl2). Further, it appearsthat some clusters have mostly vertical variance in amplitude, while others havemostly horizontal variance in frequency. The patterns and clustering of this data

8

Page 10: Molecular Dynamic Simulations in JavaScriptdaniel-kunin.com/img/reports/Molecular Dynamic... · of time it will stay stable is also random. The simulation approach using a bond length

APMA 1860 – Fall 2016 Page 9 of 12

shows promising results that the simulation is taking into account the specificproperties of each molecule.

3.4 Effect of Temperature

I further investigated how the temperature of the simulation affected the dynamicsof the molecule. Upon initial inspection there appeared to be no obvious changesin the simulation of a molecule at different temperatures. To investigate this Iplotted the average frequency and amplitude of the bond length of a hydrogenmolecule as a function of temperature.

Figure 5: Plots of average frequency (left) and amplitude (right) of a hydrogenmolecule bond as a function of simulation temperature.

Each data point in the time series above is a the average of three identical simu-lations. I did this to try and reduce variance in the data, but even so it is hardto discern any noticeable changes in the properties of the system. From visualinspection, it appears that the frequency of the bond length decreases as the tem-perature of the simulation increases, while the amplitude of the bond length staysgenerally the same. I then ran the same experiment but plotted the average timeof the simulation as a function of temperature as shown below.

9

Page 11: Molecular Dynamic Simulations in JavaScriptdaniel-kunin.com/img/reports/Molecular Dynamic... · of time it will stay stable is also random. The simulation approach using a bond length

APMA 1860 – Fall 2016 Page 10 of 12

Figure 6: Plot of time of simulation for a hydrogen molecule as a function oftemperature.

Similar to the frequency, there appears to be an inverse relationship betweenthe time of a simulation and the temperature of a simulation. This relationshipis expected as the molecule will have more energy leading to a larger deviationbetween the real dynamics of the molecule and the estimates from the VelocityVerlet Algorithm. Which, will in turn lead to the simulation becoming unstablesooner.

4 Improvements

There are a lot of improvements to be made to both the implementation of theenergy functions and the visualization of the position vectors. The first majorimprovement would be to model the remaining energy functions (Eangle, Etorsion)to account for more complicated molecular structures. However, as seen with thevan der Waals potential energy (Evdw), implementing the energy function is onlyhalf the challenge. Finding accurate constants is necessary for the accuracy ofthe simulation. The next major improvement would be to find a way of speedingup the JavaScript simulation. Currently, the JavaScript simulation must use astep size nearly 100 times larger than the one used in the Matlab simulation sothat the visualization even appears to be dynamic. This larger step size leadsto a less accurate and shorter simulation. Finding a way of decreasing the stepsize while keeping the current speed of the simulation would be a major improve-ment. Finally, I think a lot more could be explored of the relationship betweenthe molecule being simulated and the descriptive statistics of the simulation. Iwould be interested in how this relationship could be explained or confirmed bytheoretical and experimental findings.

10

Page 12: Molecular Dynamic Simulations in JavaScriptdaniel-kunin.com/img/reports/Molecular Dynamic... · of time it will stay stable is also random. The simulation approach using a bond length

APMA 1860 – Fall 2016 Page 11 of 12

5 Conclusion

Using a Newtonian mechanical model for potential energy, experimentally deter-mined energy constants, and a force-based layout scheme for graph drawing, Isuccessfully implemented a browser-based molecular dynamics simulation of di-atomic molecules in three dimensions. I further investigated descriptive statisticsof my simulation and showed that the simulation is taking into account the spe-cific properties of each molecule. Finally, I suggested improvements to both thesimulation and the visualization moving forward. As web-technologies continue toimprove, complex physics simulations can be adapted to be completely web-basedin order to increase accessibility and exposure.

11

Page 13: Molecular Dynamic Simulations in JavaScriptdaniel-kunin.com/img/reports/Molecular Dynamic... · of time it will stay stable is also random. The simulation approach using a bond length

APMA 1860 – Fall 2016 Page 12 of 12

References

[1] Badger, Richard M. ”The Relation Between Internuclear Distances and theForce Constants of Diatomic Molecules.” Physical Review 48.3 (1935): 284-85. Web.

[2] Jasper, Ahren W., and James A. Miller. ”Lennard-Jones Parameters for Com-bustion and Chemical Kinetics Modeling from Full-dimensional IntermolecularPotentials.” Combustion and Flame 161.1 (2014): 101-10. Web.

[3] Wu Zhijun. Lecture Notes on Computational Structural Biology. Singapore:n.p., 2008. Print.

12