Statistiques
| Révision :

root / ase / md / velocitydistribution.py @ 13

Historique | Voir | Annoter | Télécharger (1,49 ko)

1
# VelocityDistributions.py -- set up a velocity distribution
2

    
3
"""Module for setting up e.g. Maxwell-Boltzmann velocity distributions.
4

5
Currently, only one function is defined, MaxwellBoltzmannDistribution,
6
which sets the momenta of a list of atoms according to a
7
Maxwell-Boltzmann distribution at a given temperature.
8
"""
9

    
10
import numpy as np
11
import sys
12

    
13
# For parallel GPAW simulations, the random velocities should be distributed.
14
# Uses gpaw world communicator as default, but allow option of 
15
# specifying other communicator (for ensemble runs)
16
if '_gpaw' in sys.modules:
17
    # http://wiki.fysik.dtu.dk/gpaw
18
    from gpaw.mpi import world as gpaw_world
19
else:
20
    gpaw_world = None
21

    
22
def _maxwellboltzmanndistribution(masses, temp, communicator=gpaw_world):
23
    xi = np.random.standard_normal((len(masses),3))
24
    if communicator is not None:
25
       communicator.broadcast(xi, 0)
26
    momenta = xi * np.sqrt(masses * temp)[:,np.newaxis]
27
    return momenta
28

    
29
def MaxwellBoltzmannDistribution(atoms, temp, communicator=gpaw_world):
30
    """Sets the momenta to a Maxwell-Boltzmann distribution."""
31
    momenta = _maxwellboltzmanndistribution(atoms.get_masses(), temp, communicator)
32
    atoms.set_momenta(momenta)
33

    
34
def Stationary(atoms):
35
    "Sets the center-of-mass momentum to zero."
36
    p = atoms.get_momenta()
37
    p0 = np.sum(p, 0)
38
    # We should add a constant velocity, not momentum, to the atoms
39
    m = atoms.get_masses()
40
    mtot = np.sum(m)
41
    v0 = p0/mtot
42
    p -= v0*m[:,np.newaxis]
43
    atoms.set_momenta(p)
44