Exemplo n.º 1
0
 def _molecular_dynamics(self, resume=None):
     """Performs a molecular dynamics simulation, until mdmin is
     exceeded. If resuming, the file number (md%05i) is expected."""
     self._log('msg', 'Molecular dynamics: md%05i' % self._counter)
     mincount = 0
     energies, oldpositions = [], []
     thermalized = False
     if resume:
         self._log('msg', 'Resuming MD from md%05i.traj' % resume)
         if os.path.getsize('md%05i.traj' % resume) == 0:
             self._log(
                 'msg', 'md%05i.traj is empty. Resuming from '
                 'qn%05i.traj.' % (resume, resume - 1))
             atoms = io.read('qn%05i.traj' % (resume - 1), index=-1)
         else:
             images = io.PickleTrajectory('md%05i.traj' % resume, 'r')
             for atoms in images:
                 energies.append(atoms.get_potential_energy())
                 oldpositions.append(atoms.positions.copy())
                 passedmin = self._passedminimum(energies)
                 if passedmin:
                     mincount += 1
             self._atoms.set_momenta(atoms.get_momenta())
             thermalized = True
         self._atoms.positions = atoms.get_positions()
         self._log('msg',
                   'Starting MD with %i existing energies.' % len(energies))
     if not thermalized:
         MaxwellBoltzmannDistribution(self._atoms,
                                      temp=self._temperature * units.kB,
                                      force_temp=True)
     traj = io.PickleTrajectory('md%05i.traj' % self._counter, 'a',
                                self._atoms)
     dyn = VelocityVerlet(self._atoms, dt=self._timestep * units.fs)
     log = MDLogger(dyn,
                    self._atoms,
                    'md%05i.log' % self._counter,
                    header=True,
                    stress=False,
                    peratom=False)
     dyn.attach(log, interval=1)
     dyn.attach(traj, interval=1)
     while mincount < self._mdmin:
         dyn.run(1)
         energies.append(self._atoms.get_potential_energy())
         passedmin = self._passedminimum(energies)
         if passedmin:
             mincount += 1
         oldpositions.append(self._atoms.positions.copy())
     # Reset atoms to minimum point.
     self._atoms.positions = oldpositions[passedmin[0]]
Exemplo n.º 2
0
 def _read_minima(self):
     """Reads in the list of minima from the minima file."""
     exists = os.path.exists(self._minima_traj)
     if exists:
         empty = os.path.getsize(self._minima_traj) == 0
     if os.path.exists(self._minima_traj):
         if not empty:
             traj = io.PickleTrajectory(self._minima_traj, 'r')
             self._minima = [atoms for atoms in traj]
         else:
             self._minima = []
         return True
     else:
         self._minima = []
         return False
Exemplo n.º 3
0
 def _plot_qn(self, index, line):
     """Plots a dashed vertical line for the optimization."""
     if line[1] == 'performing MD':
         return
     file = os.path.join(self._rundirectory, 'qn%05i.traj' % index)
     if os.path.getsize(file) == 0:
         return
     traj = io.PickleTrajectory(file, 'r')
     energies = [traj[0].get_potential_energy(),
                 traj[-1].get_potential_energy()]
     if index > 0:
         file = os.path.join(self._rundirectory, 'md%05i.traj' % index)
         atoms = io.read(file, index=-3)
         energies[0] = atoms.get_potential_energy()
     self._ax.plot([index + 0.25] * 2, energies, ':k')
Exemplo n.º 4
0
 def _plot_md(self, step, line):
     """Adds a curved plot of molecular dynamics trajectory."""
     if step == 0:
         return
     energies = [self._data[step - 1][0]]
     file = os.path.join(self._rundirectory, 'md%05i.traj' % step)
     traj = io.PickleTrajectory(file, 'r')
     for atoms in traj:
         energies.append(atoms.get_potential_energy())
     xi = step - 1 + .5
     if len(energies) > 2:
         xf = xi + (step + 0.25 - xi) * len(energies) / (len(energies) - 2.)
     else:
         xf = step
     if xf > (step + .75):
         xf = step
     self._ax.plot(np.linspace(xi, xf, num=len(energies)), energies, '-k')
Exemplo n.º 5
0
     images.append(initial.copy())

if restart:
   for j in range(1,intermediate_images+1):
     nebimage=io.read('neb%d.traj' % j)
     nebimage.set_initial_magnetic_moments(mag)
     images.append(nebimage)

images.append(final)

if not climb:
  neb = NEB(images,k=k)
if climb:
  neb = NEB(images,climb=True,k=k)

m.set_neb(neb)

#only for first step, linear interpolation here.
if not restart:
  neb.interpolate()

if not climb:
   qn = FIRE(neb, logfile='qn.log')
if climb:
   qn = FIRE(neb, logfile='qn.log')

for j in range(1,intermediate_images+1):
    traj = io.PickleTrajectory('neb%d.traj' % j, 'a', images[j])
    qn.attach(traj)

qn.run(fmax=0.05)
Exemplo n.º 6
0
 def _record_minimum(self):
     """Adds the current atoms configuration to the minima list."""
     traj = io.PickleTrajectory(self._minima_traj, 'a')
     traj.write(self._atoms)
     self._read_minima()
     self._log('msg', 'Recorded minima #%i.' % (len(self._minima) - 1))
box = 5.     # box dimension
h = 0.25     # grid spacing
width = 0.01 # Fermi width
nbands = 6   # bands in GS calculation
nconv = 4    # bands in GS calculation to converge
R = 2.99     # starting distance
iex = 1      # excited state index
d = 0.01     # step for numerical force evaluation
exc = 'LDA'  # xc for the linear response TDDFT kernel

s = Cluster([Atom('Na'), Atom('Na', [0, 0, R])])
s.minimal_box(box, h=h)

c = GPAW(h=h, nbands=nbands, eigensolver='cg',
         occupations=FermiDirac(width=width),
         setups={'Na': '1'},
         convergence={'bands':nconv})
c.calculate(s)
lr = LrTDDFT(c, xc=exc, eps=0.1, jend=nconv-1)

ex = ExcitedState(lr, iex, d=d)
s.set_calculator(ex)

ftraj='relax_ex' + str(iex)
ftraj += '_box' + str(box) + '_h' + str(h)
ftraj += '_d' + str(d) + '.traj'
traj = io.PickleTrajectory(ftraj, 'w', s)
dyn = optimize.FIRE(s)
dyn.attach(traj.write)
dyn.run(fmax=0.05)