Initialisation and the basics of MD simulation - Swinburne University

7 downloads 634 Views 823KB Size Report
... 3122, Australia. Email: [email protected] ... For sufficiently massive atoms/molecules, the ..... A neighbour list allows us to dramatically reduce the number of.
Initialisation and the basics of MD simulation

Dr Billy D. Todd Centre for Molecular Simulation Swinburne University of Technology PO Box 218, Hawthorn Victoria 3122, Australia Email: [email protected]

 B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology

1

Overview In this module we will go through, step by step, the basic initialisation of a molecular dynamics simulation. We also look at how to calculate molecular interactions, with particular emphasis on some simulation ‘tricks’ which significantly speed up this calculation. In particular we will examine: • • • • •

Input parameters for a simulation Initialising the positions of all atoms Initialising the momenta of all atoms Neighbour lists Cell code.

 B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology

2

Topic 1: Input parameters for MD simulation Perhaps the most attractive feature of molecular dynamics is its essentially ‘exact’ nature. One may simulate the complete dynamical trajectories of systems of atoms or molecules in the gas, liquid or solid phase while using only a minimal number of justifiable assumptions. There are only two major assumptions: • The classical nature of the dynamics (though there are techniques that incorporate quantum mechanical features into an MD simulation, e.g., for solids, the so-called Car-Parrinello method [1]), and • The nature of the intermolecular forces (these are usually effective 2body forces, sometimes with the inclusion of 3-body terms [2]).

 B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology

3

Topic 1: Input parameters for MD simulation (cont.) • The first assumption is justifiable for systems of (chargeless) atoms or molecules. The Born-Oppenheimer approximation [3] allows one to express the Hamiltonian of a collection of atoms as a function only of the nuclear positions and momenta. The rapid motion of the electrons is essentially averaged out. For sufficiently massive atoms/molecules, the dynamics is thus essentially classical and quantum effects can be ignored. • The second assumption allows us to express the interatomic force as functions only of the separation of pairs of particles. This has been covered in more detail in Module 3. Even when it is necessary to invoke 3-body forces in an MD simulation, these may also be expressed as suitable combinations of pairwise interactions [4].

 B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology

4

Topic 1: Input parameters for MD simulation (cont.) Having taken note of the underlying assumptions, an MD simulation may be expressed schematically as follows. If first run, initialise atomic coordinates and momenta

Read input

Initialise variables

If not first run, read in stored coordinates and momenta from previous run

Calculate and store all relevant phase variables

Calculate intermolecular forces

Update velocities and move atoms

End simulation

Increment timestep

 B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology

5

Topic 1: Input parameters for MD simulation (cont.)

• • •

Consider now the first of the boxes above. What exactly do we need to read into the program as input? To characterise a thermodynamic system we of course need to know the following: Number of atoms/molecules Density Temperature

In addition, we need to know other things, such as • Total simulation time for the run • Averaging times • Size of the integration timestep • Potential parameters (e.g., σ, ε, interaction cut-off radius, etc. See Module 3 for details)  B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology

6

Topic 1: Input parameters for MD simulation (cont.) Once the input parameters have been read into the MD program we first need to determine the system volume, based upon the number of particles and input density. Assuming that our (3-dimensional) simulation cell is cubic, then clearly V = N/ρ where V is the volume, N is the number of atoms and ρ is the density. The length of the simulation cell, L, is thus L = V1/3, where L is the length of the simulation cell. We will cover potential parameters a little later when we deal with neighbour lists.

 B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology

7

Topic 1: Input parameters for MD simulation (cont.) The other very important operation we perform at this early stage is to initialise all the variables. Often this will involve setting these variables to zero. Initialisation of the momenta and coordinates is covered in the next topic, but in addition we need to set all storage variables to either zero (if we are starting the simulation for the first time or we wish to discard the data of a previous run), or else read them from an existing file (if we wish to accumulate existing data from a previous run). In Problem 1, you will be asked to read in data and initialise some simulation variables. It is designed to give you a practical appreciation of the initialisation process.

 B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology

8

Topic 2: Initialising the coordinates

• • • •

When an MD simulation is started for the very first time, one needs to specify the initial arrangement of the atoms in space, as well as their initial momenta. These need to follow only a few rules: Atoms must be arranged such that they do not overlap on each other All atoms must fit within the boundaries of the simulation cell The momenta should be scaled such that they match an initial input temperature The sum of the momenta should be initialised to zero for momentum conservation.

 B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology

9

Topic 2: Initialising the coordinates (cont.) For an homogeneous system, one generally sets up a lattice of atoms that obey the above rules. A simple cubic ‘square’ lattice is simplest of all. Recall that this is only an initialisation stage. At the onset the atoms will be given momenta, and then their trajectories will evolve with time. After a period of time the system will approach thermodynamic equilibrium and then one may make measurements of interest. An algorithm that sets up a 3-dimensional lattice for a system of N spherical atoms is given in the following pages. (Note: for molecules the initialisation process is a little more involved). A step-by-step description of all the stages is also included on p.14.

 B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology

10

Topic 2: Initialising the coordinates (cont.) // Code to initialise an fcc lattice of atoms (from Allen & Tildesley) int i, ix, iy, iz, nc, index, m; double cell, cell2; nc = (int)(pow(NP/4,1./3.) + 0.1); //NP is number of atoms cell = 1./(double)nc; cell2 = 0.5*cell; // set up unit cell phase->x0[1] = 0.0; phase->x0[2] = cell2; phase->x0[3] = 0.0; phase->x0[4] = cell2; phase->y0[1] = 0.0; phase->y0[2] = cell2; phase->y0[3] = cell2; phase->y0[4] = 0.0; phase->z0[1] = 0.0; phase->z0[2] = 0.0; phase->z0[3] = cell2; phase->z0[4] = cell2;  B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology

11

Topic 2: Initialising the coordinates (cont.) m=0; for (iz=1; izx0[index] + cell*double(ix-1); phase->y0[index] + cell*double(iy-1); phase->z0[index] + cell*double(iz-1);

// centre box at (0,0,0) for (i=1; ix0[i] -= 0.5; phase->y0[i] -= 0.5; phase->z0[i] -= 0.5; }

 B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology

12

Topic 2: Initialising the coordinates (cont.) // scale coordinates to box size for (i=1; ix0[i] *= cube; phase->y0[i] *= cube; phase->z0[i] *= cube; }

 B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology

13

Topic 2: Initialising the coordinates (cont.) • Note that the lattice can only exist for NP = 4*nc3, where nc is any positive integer. For example, N = 4, 32, 108, 256, etc are valid values of N. Other values of N are possible by different initialisations, but not on a square fcc lattice (see ref [5]). • On p.11 the coordinates of the first four atoms are defined. • On p.12 all the other atom coordinates are defined. This ensures that atoms do not overlap and are arranged in an ordered cubic fcc lattice. Also note that coordinates at this stage are all positive. • The final loop on p.12 simply re-positions the coordinates such that the centre of the simulation cell is defined as the origin (0, 0, 0). Note that this is not really necessary, but it is convenient. • On p.13, the loop scales the coordinates such that |xi|, |yi|, |zi| ≤ L. • Note also that the lattice has been set up assuming σ = 1, i.e., that we are using reduced coordinates.  B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology

14

Topic 2: Initialising the coordinates (cont.) In Problem 2 you will be asked to plot out the initial coordinates of an N=108 system of atoms, using the algorithm described above. The purpose of this exercise is to give you a 3-dimensional perspective of the structure of the initial lattice, and to ensure that you appreciate how the algorithm works.

 B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology

15

Topic 3: Initialising the momenta Recall on p.9 the initial momenta need to satisfy only two rules: • The momenta should be scaled such that they match an initial input temperature, and • The sum of the momenta should be initialised to zero for momentum conservation. In what follows we show how to do this. We will also make use of a feature which you came across in some detail in Modules 1-6, namely the random number generator. We use this to randomly assign values to the momenta of each atom. However, a random number generator in an MD program is only ever called once, at the very first time the simulation is run. Thus, it is not that important for the random number generator to be free of long-time periodicities (unlike the case for Monte Carlo simulations). Thus we can make use of the intrinsic C random number generator and not feel guilty about doing so! Note that in what follows we assume reduced units, i.e., m = k = 1, where m is the atomic mass of particles, and k is Boltzmann’s constant.  B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology

16

Topic 3: Initialising the momenta (cont.) When the program executes for the first time a call should be made to a function (let us call it ‘randomMomenta’) that: • Generates random numbers to be assigned to the three components of momenta for every atom • Calculates the total momentum of the system, and thus sets the total momentum to zero, and • Scales the momenta such that the kinetic temperature is equal to the input temperature of the system (we will cover how to fix the temperature in a simulation in a later Module). We now consider each dot point above in more detail, and present possible implementation code.

 B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology

17

Topic 3: Initialising the momenta (cont.) Generation of the random number As previously mentioned, we will use the intrinsic C library random number generator ‘rand’ to generate random numbers. Other suitable random number generators exist in the literature and are easily implementable [4]. Our random number generator needs to only produce random (real) numbers between 0 and 1. A simple implementation is double randomReal (void) { double rnum; rnum = (double) rand() / ((double) RAND_MAX + 1); // random number // between [0, 1] return rnum; }  B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology

18

Topic 3: Initialising the momenta (cont.) In the above code note that the intrinsic function rand generates a random real number between 0 and RAND_MAX. RAND_MAX is set by the C compiler you are using, and is conditioned upon the user’s machine. On a typical Macintosh or PC, RAND_MAX is usually 32,767. On a Unix workstation a typical value is 2,147,483,647(!!). Thus, there are a total of RAND_MAX + 1 possible numbers that rand can generate. Diving the output of rand by RAND_MAX + 1 ensures our random number (rnum) lies between 0 and 1. Note also that the function rand is located in the intrinsic C library stdlib.h, so your program must include the header file .  B. D. Todd, Centre for Molecular Simulation, Swinburne University of Technology

19

Topic 3: Initialising the momenta (cont.) Implementation of randomMomenta We now wish to associate atomic momenta with the random numbers generated, zero the total momenta (for momentum conservation), and scale the momenta such that they represent the kinetic temperature of the system. This is implemented as follows: double pxsum = 0, pysum = 0, pzsum = 0, kineticTemp = 0, scale; int i; // Generate random momenta for (i=1;i

Suggest Documents