root / ase / md / velocitydistribution.py @ 14
Historique | Voir | Annoter | Télécharger (1,49 ko)
| 1 | 1 | tkerber | # VelocityDistributions.py -- set up a velocity distribution
|
|---|---|---|---|
| 2 | 1 | tkerber | |
| 3 | 1 | tkerber | """Module for setting up e.g. Maxwell-Boltzmann velocity distributions.
|
| 4 | 1 | tkerber |
|
| 5 | 1 | tkerber | Currently, only one function is defined, MaxwellBoltzmannDistribution,
|
| 6 | 1 | tkerber | which sets the momenta of a list of atoms according to a
|
| 7 | 1 | tkerber | Maxwell-Boltzmann distribution at a given temperature.
|
| 8 | 1 | tkerber | """
|
| 9 | 1 | tkerber | |
| 10 | 1 | tkerber | import numpy as np |
| 11 | 1 | tkerber | import sys |
| 12 | 1 | tkerber | |
| 13 | 1 | tkerber | # For parallel GPAW simulations, the random velocities should be distributed.
|
| 14 | 1 | tkerber | # Uses gpaw world communicator as default, but allow option of
|
| 15 | 1 | tkerber | # specifying other communicator (for ensemble runs)
|
| 16 | 1 | tkerber | if '_gpaw' in sys.modules: |
| 17 | 1 | tkerber | # http://wiki.fysik.dtu.dk/gpaw
|
| 18 | 1 | tkerber | from gpaw.mpi import world as gpaw_world |
| 19 | 1 | tkerber | else:
|
| 20 | 1 | tkerber | gpaw_world = None
|
| 21 | 1 | tkerber | |
| 22 | 1 | tkerber | def _maxwellboltzmanndistribution(masses, temp, communicator=gpaw_world): |
| 23 | 1 | tkerber | xi = np.random.standard_normal((len(masses),3)) |
| 24 | 1 | tkerber | if communicator is not None: |
| 25 | 1 | tkerber | communicator.broadcast(xi, 0)
|
| 26 | 1 | tkerber | momenta = xi * np.sqrt(masses * temp)[:,np.newaxis] |
| 27 | 1 | tkerber | return momenta
|
| 28 | 1 | tkerber | |
| 29 | 1 | tkerber | def MaxwellBoltzmannDistribution(atoms, temp, communicator=gpaw_world): |
| 30 | 1 | tkerber | """Sets the momenta to a Maxwell-Boltzmann distribution."""
|
| 31 | 1 | tkerber | momenta = _maxwellboltzmanndistribution(atoms.get_masses(), temp, communicator) |
| 32 | 1 | tkerber | atoms.set_momenta(momenta) |
| 33 | 1 | tkerber | |
| 34 | 1 | tkerber | def Stationary(atoms): |
| 35 | 1 | tkerber | "Sets the center-of-mass momentum to zero."
|
| 36 | 1 | tkerber | p = atoms.get_momenta() |
| 37 | 1 | tkerber | p0 = np.sum(p, 0)
|
| 38 | 1 | tkerber | # We should add a constant velocity, not momentum, to the atoms
|
| 39 | 1 | tkerber | m = atoms.get_masses() |
| 40 | 1 | tkerber | mtot = np.sum(m) |
| 41 | 1 | tkerber | v0 = p0/mtot |
| 42 | 1 | tkerber | p -= v0*m[:,np.newaxis] |
| 43 | 1 | tkerber | atoms.set_momenta(p) |