Exemplo n.º 1
0
def run_md(atoms):
    from ase import units
    from ase.md.velocitydistribution import MaxwellBoltzmannDistribution
    from ase.md import VelocityVerlet

    traj = ase.io.Trajectory("traj.traj", 'w')

    calc = Amp.load("amp.amp")
    atoms.set_calculator(calc)
    atoms.get_potential_energy()
    MaxwellBoltzmannDistribution(atoms, 10. * units.kB)
    traj.write(atoms)
    dyn = VelocityVerlet(atoms, dt=1. * units.fs)
    f = open("md.ene", "w")
    f.write("{:^5s}{:^10s}{:^10s}{:^10s}\n".format("time", "Etot", "Epot",
                                                   "Ekin"))
    for step in range(100):
        pot = atoms.get_potential_energy()  #
        kin = atoms.get_kinetic_energy()
        tot = pot + kin
        f.write("{:5d}{:10.5f}{:10.5f}{:10.5f}\n".format(step, tot, pot, kin))
        print("{}: Total Energy={}, POT={}, KIN={}".format(
            step, tot, pot, kin))
        dyn.run(10)
        traj.write(atoms)
    f.close()
Exemplo n.º 2
0
Arquivo: Morse.py Projeto: auag92/n2dm
def TestEnergyConservation():
    print "Running TestEnergyConservation..."

    calc = Morse(elements, epsilon, alpha, rmin)
    atoms = SimpleCubic('Ar', size=(10,10,10), latticeconstant=5.0)
    n = 0
    while n < 100:
        i = np.random.randint(len(atoms)-1)
        if atoms[i].number != atomic_numbers['Ru']:
            atoms[i].number = atomic_numbers['Ru']
            n += 1
    atoms.set_calculator(calc)

    # Set initial momentum
    MaxwellBoltzmannDistribution(atoms, 300*units.kB)

    # Run dynamics
    dyn = VelocityVerlet(atoms, 1.0 * units.fs, logfile='test-energy.dat', loginterval=10)
    dyn.run(10)
    etot = (atoms.get_potential_energy() + atoms.get_kinetic_energy())/len(atoms)
    print "%-9s %-9s %-9s" % ("Epot", "Ekin", "Sum")
    for i in range(25):
        if i:
            dyn.run(100)
        epot = atoms.get_potential_energy()/len(atoms)
        ekin = atoms.get_kinetic_energy()/len(atoms)
        print "%9.5f %9.5f %9.5f" % (epot, ekin, epot+ekin)
        ReportTest("Step %i." % (i,), epot+ekin, etot, 1e-3, silent=True)
Exemplo n.º 3
0
def test_idealgas():
    from ase.md import VelocityVerlet
    from ase.build import bulk
    from ase.units import kB
    from ase.md.velocitydistribution import MaxwellBoltzmannDistribution
    from ase.calculators.idealgas import IdealGas
    import numpy as np

    atoms = bulk('Kr').repeat((10, 10, 10))
    assert len(atoms) == 1000

    atoms.center(vacuum=100)
    atoms.set_calculator(IdealGas())

    T = 1000

    MaxwellBoltzmannDistribution(atoms, T * kB)
    print("Temperature: {} K".format(atoms.get_temperature()))

    md = VelocityVerlet(atoms, timestep=0.1)
    for i in range(5):
        md.run(5)
        s = atoms.get_stress(include_ideal_gas=True)
        p = -s[:3].sum() / 3
        v = atoms.get_volume()
        N = len(atoms)
        T = atoms.get_temperature()
        print("pV = {}  NkT = {}".format(p * v, N * kB * T))
        assert np.fabs(p * v - N * kB * T) < 1e-6
Exemplo n.º 4
0
def run_md(fdata, atoms):
    from ase import units
    from ase.md.velocitydistribution import MaxwellBoltzmannDistribution
    from ase.md import VelocityVerlet
    fdata = fdata[:-7]

    traj = ase.io.Trajectory(fdata + ".traj", 'w')

    calc = Amp.load("amp.amp")
    atoms.set_calculator(calc)
    atoms.get_potential_energy()
    MaxwellBoltzmannDistribution(atoms,
                                 100. * units.kB)  #Boltzmann constant eV/K
    traj.write(atoms)
    dyn = VelocityVerlet(atoms, dt=1. * units.fs)

    for step in range(200):
        pot = atoms.get_potential_energy()  #
        kin = atoms.get_kinetic_energy()
        with open(fdata + '.txt', 'a') as f:
            f.write("{}: Total Energy={}, POT={}, KIN={}\n".format(
                step, pot + kin, pot, kin))
        dyn.run(5)
        ase.io.write(fdata + '.xyz', ase.io.read(fdata + '.traj'), append=True)
        traj.write(atoms)
Exemplo n.º 5
0
    def run(self, calc, filename):
        slab = self.starting_geometry.copy()
        slab.set_calculator(calc)
        np.random.seed(1)
        MaxwellBoltzmannDistribution(slab, self.temp * units.kB)
        if self.ensemble == "NVE":
            dyn = VelocityVerlet(slab, self.dt * units.fs)
        elif self.ensemble == "nvtberendsen":
            dyn = nvtberendsen.NVTBerendsen(slab,
                                            self.dt * units.fs,
                                            self.temp,
                                            taut=300 * units.fs)
        elif self.ensemble == "langevin":
            dyn = Langevin(slab, self.dt * units.fs, self.temp * units.kB,
                           0.002)
        traj = ase.io.Trajectory(filename + ".traj", "w", slab)
        dyn.attach(traj.write, interval=1)
        try:
            fixed_atoms = len(slab.constraints[0].get_indices())
        except:
            fixed_atoms = 0
            pass

        def printenergy(a=slab):
            """Function to print( the potential, kinetic, and total energy)"""
            epot = a.get_potential_energy() / len(a)
            ekin = a.get_kinetic_energy() / (len(a) - fixed_atoms)
            print("Energy per atom: Epot = %.3feV Ekin = %.3feV (T=%3.0fK) "
                  "Etot = %.3feV" % (epot, ekin, ekin /
                                     (1.5 * units.kB), epot + ekin))

        if printenergy:
            dyn.attach(printenergy, interval=10)
        dyn.run(self.count)
Exemplo n.º 6
0
def test_idealgas():
    rng = np.random.RandomState(17)
    atoms = bulk('Kr').repeat((10, 10, 10))
    assert len(atoms) == 1000

    atoms.center(vacuum=100)
    atoms.calc = IdealGas()
    natoms = len(atoms)

    md_temp = 1000

    MaxwellBoltzmannDistribution(atoms, md_temp * kB, rng=rng)
    print("Temperature: {} K".format(atoms.get_temperature()))

    md = VelocityVerlet(atoms, timestep=0.1)
    for i in range(5):
        md.run(5)
        stress = atoms.get_stress(include_ideal_gas=True)
        stresses = atoms.get_stresses(include_ideal_gas=True)
        assert stresses.mean(0) == pytest.approx(stress)
        pressure = -stress[:3].sum() / 3
        pV = pressure * atoms.cell.volume
        NkT = natoms * kB * atoms.get_temperature()
        print(f"pV = {pV}  NkT = {NkT}")
        assert pV == pytest.approx(NkT, abs=1e-6)
Exemplo n.º 7
0
def canonical(atoms,dt=1.0,steps=1000,output=10,name=None,verbose=False):
    """ Perform little canonical simulation. 
    
    parameters:
    -----------
    atoms: atoms with calculator attached
    dt: time step in fs
    steps: how many md steps
    output: output frequency
    name: TrajectoryRecording name
    verbose: increase verbosity    
    """
    if name==None:
        try:
            name=atoms.get_chemical_formula(mode="hill")
        except:
            name='microcanonical'
    name+='.trj'        
    traj=PickleTrajectory(name,'w',atoms)
    rec=TrajectoryRecording(atoms,verbose)
    md=VelocityVerlet(atoms,dt*fs)
    md.attach(rec,interval=output)  
    md.attach(traj.write,interval=output)  
    md.run(steps) 
    return rec
Exemplo n.º 8
0
def microcanonical(atoms,
                   dt=1.0,
                   steps=100,
                   output=1,
                   name=None,
                   verbose=False):
    """ Perform little microcanonical simulation. 
    
    parameters:
    -----------
    atoms:
    dt: time step in fs
    steps: how many md steps
    output: output frequency
    name: TrajectoryRecording name
    verbose: increase verbosity
    
    Return TrajectoryRecording object for further analysis.
    """
    if name == None:
        try:
            name = atoms.get_chemical_formula(mode="hill")
        except:
            name = 'microcanonical'
    name += '.trj'
    traj = Trajectory(name, 'w', atoms)
    rec = TrajectoryRecording(atoms, verbose)
    md = VelocityVerlet(atoms, dt * fs)
    md.attach(rec, interval=output)
    md.attach(traj.write, interval=output)
    md.run(steps)
    return rec
Exemplo n.º 9
0
def amp_md(atoms, nstep, dt):
    from ase import units
    from ase.md.velocitydistribution import MaxwellBoltzmannDistribution
    from ase.md import VelocityVerlet

    traj = ase.io.Trajectory("traj.traj", 'w')

    try:
        calc = Amp.load("amp.amp")
    except FileNotFoundError:
        try:
            calc = Amp.load("amp-untrained-parameters.amp") 
        except FileNotFoundError:
            print("Error: amp-pes.amp file does not exist, input amp-pot file by -p")
            sys.exit(1)

    atoms.set_calculator(calc)
    atoms.get_potential_energy()
    MaxwellBoltzmannDistribution(atoms, 300 * units.kB)
    traj.write(atoms)
    dyn = VelocityVerlet(atoms, dt=dt * units.fs)
    f = open("md.ene", "w")
    f.write("{:^5s} {:^10s} {:^10s} {:^10s}\n".format("time","Etot","Epot","Ekin"))
    for step in range(nstep):
        pot = atoms.get_potential_energy()  # 
        kin = atoms.get_kinetic_energy()
        tot = pot + kin
        f.write("{:5d}{:10.5f}{:10.5f}{:10.5f}\n".format(step, tot, pot, kin))
        print("{}: Total Energy={}, POT={}, KIN={}".format(step, tot, pot, kin))
        dyn.run(2)
        traj.write(atoms)                   # write kinetic energy, but pot is not seen in ase
    f.close()        
Exemplo n.º 10
0
def TestEnergyConservation():
    print "Running TestEnergyConservation..."

    calc = Morse(elements, epsilon, alpha, rmin)
    atoms = SimpleCubic('Ar', size=(10, 10, 10), latticeconstant=5.0)
    n = 0
    while n < 100:
        i = np.random.randint(len(atoms) - 1)
        if atoms[i].number != atomic_numbers['Ru']:
            atoms[i].number = atomic_numbers['Ru']
            n += 1
    atoms.set_calculator(calc)

    # Set initial momentum
    MaxwellBoltzmannDistribution(atoms, 300 * units.kB)

    # Run dynamics
    dyn = VelocityVerlet(atoms,
                         1.0 * units.fs,
                         logfile='test-energy.dat',
                         loginterval=10)
    dyn.run(10)
    etot = (atoms.get_potential_energy() +
            atoms.get_kinetic_energy()) / len(atoms)
    print "%-9s %-9s %-9s" % ("Epot", "Ekin", "Sum")
    for i in range(25):
        if i:
            dyn.run(100)
        epot = atoms.get_potential_energy() / len(atoms)
        ekin = atoms.get_kinetic_energy() / len(atoms)
        print "%9.5f %9.5f %9.5f" % (epot, ekin, epot + ekin)
        ReportTest("Step %i." % (i, ), epot + ekin, etot, 1e-3, silent=True)
Exemplo n.º 11
0
 def _molecular_dynamics(self, step, N):
     """Performs a molecular dynamics simulation, until mdmin is
     exceeded. If resuming, the file number (md%05i) is expected."""
     mincount = 0
     energies, oldpositions = [], []
     thermalized = False
     if not thermalized:
         self.MaxwellBoltzmannDistribution(N,
                                      temp=self.temperature * kB,
                                      force_temp=True)
     traj = io.Trajectory('md.traj', 'a', self.atoms)
     dyn = VelocityVerlet(self.atoms, dt=self.timestep * units.fs)
     log = MDLogger(dyn, self.atoms, 'md.log',
                    header=True, stress=False, peratom=False)
     dyn.attach(log, interval=1)
     dyn.attach(traj, interval=1)
     os.remove('md.log')
     os.remove('md.traj')
     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.º 12
0
    def test_nve_against_ase(self):
        from ase.build import bulk

        ase_atoms = bulk("Ar", cubic=True) * [8, 8, 8]
        ase_atoms.calc = aLJ(
            epsilon=self.epsilon, sigma=self.sigma, rc=self.rc, ro=self.ro, smooth=True
        )
        ase_dyn = VelocityVerlet(ase_atoms, timestep=self.dt)

        asax_atoms = bulk("Ar", cubic=True) * [8, 8, 8]
        asax_atoms.calc = jLJ(
            epsilon=self.epsilon, sigma=self.sigma, rc=self.rc, ro=self.ro, x64=True
        )
        asax_dyn = VelocityVerlet(asax_atoms, timestep=self.dt)

        for i in range(10):
            ase_dyn.run(steps=1)
            asax_dyn.run(steps=1)

            self.assertAllClose(
                ase_atoms.get_positions(wrap=True),
                asax_atoms.get_positions(wrap=True),
                atol=1e-8,
                equal_nan=False,
            )
Exemplo n.º 13
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:
                with io.Trajectory('md%05i.traj' % resume, 'r') as images:
                    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,
                                         temperature_K=self._temperature,
                                         force_temp=True)
        traj = io.Trajectory('md%05i.traj' % self._counter, 'a', self._atoms)
        dyn = VelocityVerlet(self._atoms, timestep=self._timestep * units.fs)
        log = MDLogger(dyn,
                       self._atoms,
                       'md%05i.log' % self._counter,
                       header=True,
                       stress=False,
                       peratom=False)

        with traj, dyn, log:
            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.º 14
0
def test():

    # Generate atomic system to create test data.
    atoms = fcc110('Cu', (2, 2, 2), vacuum=7.)
    adsorbate = Atoms([
        Atom('H', atoms[7].position + (0., 0., 2.)),
        Atom('H', atoms[7].position + (0., 0., 5.))
    ])
    atoms.extend(adsorbate)
    atoms.set_constraint(FixAtoms(indices=[0, 2]))
    calc = EMT()  # cheap calculator
    atoms.set_calculator(calc)

    # Run some molecular dynamics to generate data.
    trajectory = io.Trajectory('data.traj', 'w', atoms=atoms)
    MaxwellBoltzmannDistribution(atoms, temp=300. * units.kB)
    dynamics = VelocityVerlet(atoms, dt=1. * units.fs)
    dynamics.attach(trajectory)
    for step in range(50):
        dynamics.run(5)
    trajectory.close()

    # Train the calculator.
    train_images, test_images = randomize_images('data.traj')

    calc = Amp(descriptor=Behler(), regression=NeuralNetwork())
    calc.train(train_images, energy_goal=0.001, force_goal=None)

    # Plot and test the predictions.
    import matplotlib
    matplotlib.use('Agg')
    from matplotlib import pyplot

    fig, ax = pyplot.subplots()

    for image in train_images:
        actual_energy = image.get_potential_energy()
        predicted_energy = calc.get_potential_energy(image)
        ax.plot(actual_energy, predicted_energy, 'b.')

    for image in test_images:
        actual_energy = image.get_potential_energy()
        predicted_energy = calc.get_potential_energy(image)
        ax.plot(actual_energy, predicted_energy, 'r.')

    ax.set_xlabel('Actual energy, eV')
    ax.set_ylabel('Amp energy, eV')

    fig.savefig('parityplot.png')
def test():

    # Generate atomic system to create test data.
    atoms = fcc110('Cu', (2, 2, 2), vacuum=7.)
    adsorbate = Atoms([Atom('H', atoms[7].position + (0., 0., 2.)),
                       Atom('H', atoms[7].position + (0., 0., 5.))])
    atoms.extend(adsorbate)
    atoms.set_constraint(FixAtoms(indices=[0, 2]))
    calc = EMT()  # cheap calculator
    atoms.set_calculator(calc)

    # Run some molecular dynamics to generate data.
    trajectory = io.Trajectory('data.traj', 'w', atoms=atoms)
    MaxwellBoltzmannDistribution(atoms, temp=300. * units.kB)
    dynamics = VelocityVerlet(atoms, dt=1. * units.fs)
    dynamics.attach(trajectory)
    for step in range(50):
        dynamics.run(5)
    trajectory.close()

    # Train the calculator.
    train_images, test_images = randomize_images('data.traj')

    calc = Amp(descriptor=Behler(),
               regression=NeuralNetwork())
    calc.train(train_images, energy_goal=0.001, force_goal=None)

    # Plot and test the predictions.
    import matplotlib
    matplotlib.use('Agg')
    from matplotlib import pyplot

    fig, ax = pyplot.subplots()

    for image in train_images:
        actual_energy = image.get_potential_energy()
        predicted_energy = calc.get_potential_energy(image)
        ax.plot(actual_energy, predicted_energy, 'b.')

    for image in test_images:
        actual_energy = image.get_potential_energy()
        predicted_energy = calc.get_potential_energy(image)
        ax.plot(actual_energy, predicted_energy, 'r.')

    ax.set_xlabel('Actual energy, eV')
    ax.set_ylabel('Amp energy, eV')

    fig.savefig('parityplot.png')
Exemplo n.º 16
0
def test_forcecurve(plt):
    from ase.build import bulk
    from ase.calculators.emt import EMT
    from ase.utils.forcecurve import force_curve
    from ase.md import VelocityVerlet
    from ase.units import fs
    from ase.io import read

    atoms = bulk('Au', cubic=True) * (2, 1, 1)
    atoms.calc = EMT()
    atoms.rattle(stdev=0.05)

    md = VelocityVerlet(atoms, timestep=12.0 * fs, trajectory='tmp.traj')
    md.run(steps=10)
    images = read('tmp.traj', ':')
    force_curve(images)
Exemplo n.º 17
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.º 18
0
def generate_data(count, filename='training.traj'):
    """Generates test or training data with a simple MD simulation."""
    if os.path.exists(filename):
        return
    traj = ase.io.Trajectory(filename, 'w')
    atoms = fcc110('Pt', (2, 2, 2), vacuum=7.)
    atoms.extend(Atoms([Atom('Cu', atoms[7].position + (0., 0., 2.5)),
                        Atom('Cu', atoms[7].position + (0., 0., 5.))]))
    atoms.set_constraint(FixAtoms(indices=[0, 2]))
    atoms.set_calculator(EMT())
    atoms.get_potential_energy()
    traj.write(atoms)
    MaxwellBoltzmannDistribution(atoms, 300. * units.kB)
    dyn = VelocityVerlet(atoms, dt=1. * units.fs)
    for step in range(count - 1):
        dyn.run(50)
        traj.write(atoms)
Exemplo n.º 19
0
def test_verlet():
    with seterr(all='raise'):
        a = Atoms('4X',
                  masses=[1, 2, 3, 4],
                  positions=[(0, 0, 0), (1, 0, 0), (0, 1, 0), (0.1, 0.2, 0.7)],
                  calculator=TstPotential())
        print(a.get_forces())
        md = VelocityVerlet(a, timestep=0.5 * fs, logfile='-', loginterval=500)
        traj = Trajectory('4N.traj', 'w', a)
        md.attach(traj.write, 100)
        e0 = a.get_total_energy()
        md.run(steps=10000)
        del traj
        assert abs(read('4N.traj').get_total_energy() - e0) < 0.0001

        qn = QuasiNewton(a)
        qn.run(0.001)
        assert abs(a.get_potential_energy() - 1.0) < 0.000002
Exemplo n.º 20
0
def test_verlet_asap(asap3):
    with seterr(all='raise'):
        a = bulk('Au').repeat((2, 2, 2))
        a[5].symbol = 'Ag'
        a.pbc = (True, True, False)
        print(a)
        a.calc = asap3.EMT()
        MaxwellBoltzmannDistribution(a, 300 * kB, force_temp=True)
        Stationary(a)
        assert abs(a.get_temperature() - 300) < 0.0001
        print(a.get_forces())
        md = VelocityVerlet(a, timestep=2 * fs, logfile='-', loginterval=500)
        traj = Trajectory('Au7Ag.traj', 'w', a)
        md.attach(traj.write, 100)
        e0 = a.get_total_energy()
        md.run(steps=10000)
        del traj
        assert abs(a.get_total_energy() - e0) < 0.0001
        assert abs(read('Au7Ag.traj').get_total_energy() - e0) < 0.0001
Exemplo n.º 21
0
def run_md(atoms):
    from ase import units
    from ase.md.velocitydistribution import MaxwellBoltzmannDistribution
    from ase.md import VelocityVerlet

    traj = ase.io.Trajectory("traj.traj", 'w')

    calc = Amp.load("amp.amp")
    atoms.set_calculator(calc)
    atoms.get_potential_energy()
    MaxwellBoltzmannDistribution(atoms, 300. * units.kB)
    traj.write(atoms)
    dyn = VelocityVerlet(atoms, dt=1. * units.fs)
    for step in range(100):
        pot = atoms.get_potential_energy()  #
        kin = atoms.get_kinetic_energy()
        print("{}: Total Energy={}, POT={}, KIN={}".format(
            step, pot + kin, pot, kin))
        dyn.run(10)
        traj.write(atoms)
Exemplo n.º 22
0
def generate_data(count):
    """Generates test or training data with a simple MD simulation."""
    atoms = fcc110('Pt', (2, 2, 2), vacuum=7.)
    adsorbate = Atoms([Atom('Cu', atoms[7].position + (0., 0., 2.5)),
                       Atom('Cu', atoms[7].position + (0., 0., 5.))])
    atoms.extend(adsorbate)
    atoms.set_constraint(FixAtoms(indices=[0, 2]))
    atoms.set_calculator(EMT())
    MaxwellBoltzmannDistribution(atoms, 300. * units.kB)
    dyn = VelocityVerlet(atoms, dt=1. * units.fs)
    newatoms = atoms.copy()
    newatoms.set_calculator(EMT())
    newatoms.get_potential_energy()
    images = [newatoms]
    for step in range(count - 1):
        dyn.run(50)
        newatoms = atoms.copy()
        newatoms.set_calculator(EMT())
        newatoms.get_potential_energy()
        images.append(newatoms)
    return images
def generate_data(count):
    """Generates test or training data with a simple MD simulation."""
    atoms = fcc110('Pt', (2, 2, 1), vacuum=7.)
    adsorbate = Atoms([Atom('Cu', atoms[3].position + (0., 0., 2.5)),
                       Atom('Cu', atoms[3].position + (0., 0., 5.))])
    atoms.extend(adsorbate)
    atoms.set_constraint(FixAtoms(indices=[0, 2]))
    atoms.set_calculator(EMT())
    MaxwellBoltzmannDistribution(atoms, 300. * units.kB)
    dyn = VelocityVerlet(atoms, dt=1. * units.fs)
    newatoms = atoms.copy()
    newatoms.set_calculator(EMT())
    newatoms.get_potential_energy()
    images = [newatoms]
    for step in range(count - 1):
        dyn.run(50)
        newatoms = atoms.copy()
        newatoms.set_calculator(EMT())
        newatoms.get_potential_energy()
        images.append(newatoms)
    return images
Exemplo n.º 24
0
def check_energy_conservation(atoms,
                              dt=1.0 * fs,
                              steps=200,
                              tol=0.01,
                              plot=False):
    """
    For given initial atoms, check the energy conservation with NVE simulation.
     
    @param atoms: ase atoms instance
    @param dt:    time step
    @param steps: number of time steps used in the check
    @param plot:  use maplotlib to plot the etot,epot -graph
    
    Energy is conserved if fluctuation of total energy is tolerance times
    the fluctuation of potential energy.
    """
    dyn = VelocityVerlet(atoms, dt=dt)
    epot = []
    ekin = []
    for i in range(steps):
        dyn.run(1)
        epot.append(atoms.get_potential_energy())
        ekin.append(atoms.get_kinetic_energy())
    epot = np.array(epot)
    ekin = np.array(ekin)
    etot = epot + ekin
    depot = np.sqrt(np.var(epot))
    detot = np.sqrt(np.var(etot))
    if plot:
        import pylab as pl
        pl.plot(epot, label='epot')
        pl.plot(etot, label='etot')
        pl.legend()
        pl.xlabel('time step')
        pl.ylabel('energy (eV)')
        pl.show()
    if detot < tol * depot:
        return True
    else:
        return False
Exemplo n.º 25
0
def test_md():
    from ase import Atoms
    from ase.calculators.emt import EMT
    from ase.md import VelocityVerlet
    from ase.io import Trajectory

    a = 3.6
    b = a / 2
    fcc = Atoms('Cu', positions=[(0, 0, 0)],
                cell=[(0, b, b), (b, 0, b), (b, b, 0)],
                pbc=1)
    fcc *= (2, 1, 1)
    fcc.set_calculator(EMT())
    fcc.set_momenta([(0.9, 0.0, 0.0), (-0.9, 0, 0)])
    md = VelocityVerlet(fcc, timestep=0.1)

    def f():
        print(fcc.get_potential_energy(), fcc.get_total_energy())
    md.attach(f)
    md.attach(Trajectory('Cu2.traj', 'w', fcc).write, interval=3)
    md.run(steps=20)
    Trajectory('Cu2.traj', 'r')[-1]
Exemplo n.º 26
0
def generate_training_set(individual, db_name):
    """
    Do MD using EMT calculator with each slab for 10 steps and add it to train.db
    """

    db = connect(db_name)
    for miller_indices in individual:

        # Do MD
        slab = generate_slab(miller_indices, 5)
        slab = make_supercell(slab, P=[[3, 1, 1], [1, 3, 1], [1, 1, 1]])

        slab.set_calculator(EMT())
        slab.get_potential_energy()

        db.write(slab)

        MaxwellBoltzmannDistribution(slab, 300. * units.kB)
        dyn = VelocityVerlet(slab, dt=1. * units.fs)
        for i in range(1, 10):
            dyn.run(10)
            db.write(slab)
Exemplo n.º 27
0
def check_energy_conservation(atoms,dt=1.0*fs,steps=200,tol=0.01,plot=False):
    """
    For given initial atoms, check the energy conservation with NVE simulation.
     
    @param atoms: ase atoms instance
    @param dt:    time step
    @param steps: number of time steps used in the check
    @param plot:  use maplotlib to plot the etot,epot -graph
    
    Energy is conserved if fluctuation of total energy is tolerance times
    the fluctuation of potential energy.
    """
    dyn = VelocityVerlet(atoms,dt=dt)
    epot = []
    ekin = []
    for i in range(steps):
        dyn.run(1)
        epot.append( atoms.get_potential_energy() )
        ekin.append( atoms.get_kinetic_energy() )
    epot = np.array(epot)
    ekin = np.array(ekin)
    etot = epot + ekin
    depot = np.sqrt( np.var(epot) )
    detot = np.sqrt( np.var(etot) )
    if plot:
        import pylab as pl
        pl.plot(epot,label='epot')
        pl.plot(etot,label='etot')
        pl.legend()
        pl.xlabel('time step')
        pl.ylabel('energy (eV)')
        pl.show()
    if detot < tol*depot:
        return True
    else:
        return False    
Exemplo n.º 28
0
from ase.build import bulk
from ase.calculators.emt import EMT
from ase.utils.forcecurve import force_curve
from ase.md import VelocityVerlet
from ase.units import fs
from ase.io import read

atoms = bulk('Au', cubic=True) * (2, 1, 1)
atoms.calc = EMT()
atoms.rattle(stdev=0.05)

md = VelocityVerlet(atoms, timestep=12.0 * fs, trajectory='tmp.traj')
md.run(steps=10)
images = read('tmp.traj', ':')
force_curve(images)

# import pylab as plt
# plt.show()
Exemplo n.º 29
0
Arquivo: md.py Projeto: yfyh2013/ase
from ase import Atoms
from ase.calculators.emt import EMT
from ase.md import VelocityVerlet
from ase.io import Trajectory

a = 3.6
b = a / 2
fcc = Atoms('Cu',
            positions=[(0, 0, 0)],
            cell=[(0, b, b), (b, 0, b), (b, b, 0)],
            pbc=1)
fcc *= (2, 1, 1)
fcc.set_calculator(EMT())
fcc.set_momenta([(0.9, 0.0, 0.0), (-0.9, 0, 0)])
md = VelocityVerlet(fcc, dt=0.1)


def f():
    print(fcc.get_potential_energy(), fcc.get_total_energy())


md.attach(f)
md.attach(Trajectory('Cu2.traj', 'w', fcc).write, interval=3)
md.run(steps=20)
fcc2 = Trajectory('Cu2.traj', 'r')[-1]
Exemplo n.º 30
0
import numpy as np
from ase import Atoms
from ase.units import fs
from ase.calculators.test import TestPotential
from ase.calculators.emt import EMT
from ase.md import VelocityVerlet
from ase.io import PickleTrajectory, read
from ase.optimize import QuasiNewton

np.seterr(all='raise')
a = Atoms('4X',
          masses=[1, 2, 3, 4],
          positions=[(0, 0, 0),
                     (1, 0, 0),
                     (0, 1, 0),
                     (0.1, 0.2, 0.7)],
          calculator=TestPotential())
print a.get_forces()
md = VelocityVerlet(a, dt=0.5 * fs, logfile='-', loginterval=500)
traj = PickleTrajectory('4N.traj', 'w', a)
md.attach(traj.write, 100)
e0 = a.get_total_energy()
md.run(steps=10000)
del traj
assert abs(read('4N.traj').get_total_energy() - e0) < 0.0001

qn = QuasiNewton(a)
qn.run(0.001)
assert abs(a.get_potential_energy() - 1.0) < 0.000002
Exemplo n.º 31
0
from ase.constraints import FixAtoms
from ase.calculators.siesta import Siesta
from ase.md import VelocityVerlet
from ase import units

# Read in the geometry from a xyz file, set the cell, boundary conditions and center
atoms = read('geom.xyz')
atoms.set_cell([7.66348,7.66348,7.66348*2])
atoms.set_pbc((1,1,1))
atoms.center()

# Set initial velocities for hydrogen atoms along the z-direction
p = atoms.get_momenta()
p[0,2]= -1.5
p[1,2]= -1.5
atoms.set_momenta(p)

# Keep some atoms fixed during the simulation
atoms.set_constraint(FixAtoms(indices=range(18,38)))

# Set the calculator and attach it to the system
calc = Siesta('si001+h2',basis='SZ',xc='PBE',meshcutoff=50*units.Ry)
calc.set_fdf('PAO.EnergyShift', 0.25 * units.eV) 
calc.set_fdf('PAO.SplitNorm', 0.15)       
atoms.set_calculator(calc)

# Set the VelocityVerlet algorithm and run it
dyn = VelocityVerlet(atoms,dt=1.0 * units.fs,trajectory='si001+h2.traj')
dyn.run(steps=100)

Exemplo n.º 32
0
def test_hookean():
    """
    Test of Hookean constraint.

    Checks for activity in keeping a bond, preventing vaporization, and
    that energy is conserved in NVE dynamics.
    """

    import numpy as np
    from ase import Atoms, Atom
    from ase.build import fcc110
    from ase.calculators.emt import EMT
    from ase.constraints import FixAtoms, Hookean
    from ase.md import VelocityVerlet
    from ase import units

    class SaveEnergy:
        """Class to save energy."""
        def __init__(self, atoms):
            self.atoms = atoms
            self.energies = []

        def __call__(self):
            self.energies.append(atoms.get_total_energy())

    # Make Pt 110 slab with Cu2 adsorbate.
    atoms = fcc110('Pt', (2, 2, 2), vacuum=7.)
    adsorbate = Atoms([
        Atom('Cu', atoms[7].position + (0., 0., 2.5)),
        Atom('Cu', atoms[7].position + (0., 0., 5.0))
    ])
    atoms.extend(adsorbate)
    calc = EMT()
    atoms.calc = calc

    # Constrain the surface to be fixed and a Hookean constraint between
    # the adsorbate atoms.
    constraints = [
        FixAtoms(indices=[atom.index for atom in atoms
                          if atom.symbol == 'Pt']),
        Hookean(a1=8, a2=9, rt=2.6, k=15.),
        Hookean(a1=8, a2=(0., 0., 1., -15.), k=15.)
    ]
    atoms.set_constraint(constraints)

    # Give it some kinetic energy.
    momenta = atoms.get_momenta()
    momenta[9, 2] += 20.
    momenta[9, 1] += 2.
    atoms.set_momenta(momenta)

    # Propagate in Velocity Verlet (NVE).
    dyn = VelocityVerlet(atoms, timestep=1.0 * units.fs)
    energies = SaveEnergy(atoms)
    dyn.attach(energies)
    dyn.run(steps=100)

    # Test the max bond length and position.
    bondlength = np.linalg.norm(atoms[8].position - atoms[9].position)
    assert bondlength < 3.0
    assert atoms[9].z < 15.0

    # Test that energy was conserved.
    assert max(energies.energies) - min(energies.energies) < 0.01

    # Make sure that index shuffle works.
    neworder = list(range(len(atoms)))
    neworder[8] = 9  # Swap two atoms.
    neworder[9] = 8
    atoms = atoms[neworder]
    assert atoms.constraints[1].indices[0] == 9
    assert atoms.constraints[1].indices[1] == 8
    assert atoms.constraints[2].index == 9
Exemplo n.º 33
0
from ase import Atoms
from ase.units import fs
from ase.calculators.test import TestPotential
from ase.md import VelocityVerlet
from ase.io import Trajectory, read
from ase.optimize import QuasiNewton
from ase.utils import seterr

with seterr(all='raise'):
    a = Atoms('4X',
              masses=[1, 2, 3, 4],
              positions=[(0, 0, 0), (1, 0, 0), (0, 1, 0), (0.1, 0.2, 0.7)],
              calculator=TestPotential())
    print(a.get_forces())
    md = VelocityVerlet(a, timestep=0.5 * fs, logfile='-', loginterval=500)
    traj = Trajectory('4N.traj', 'w', a)
    md.attach(traj.write, 100)
    e0 = a.get_total_energy()
    md.run(steps=10000)
    del traj
    assert abs(read('4N.traj').get_total_energy() - e0) < 0.0001

    qn = QuasiNewton(a)
    qn.run(0.001)
    assert abs(a.get_potential_energy() - 1.0) < 0.000002
Exemplo n.º 34
0
from ase.io import read
from ase.constraints import FixAtoms
from ase.calculators.siesta import Siesta
from ase.md import VelocityVerlet
from ase import units

# Read in the geometry from a xyz file, set the cell, boundary conditions and center
atoms = read('geom.xyz')
atoms.set_cell([7.66348, 7.66348, 7.66348 * 2])
atoms.set_pbc((1, 1, 1))
atoms.center()

# Set initial velocities for hydrogen atoms along the z-direction
p = atoms.get_momenta()
p[0, 2] = -1.5
p[1, 2] = -1.5
atoms.set_momenta(p)

# Keep some atoms fixed during the simulation
atoms.set_constraint(FixAtoms(indices=range(18, 38)))

# Set the calculator and attach it to the system
calc = Siesta('si001+h2', basis='SZ', xc='PBE', meshcutoff=50 * units.Ry)
calc.set_fdf('PAO.EnergyShift', 0.25 * units.eV)
calc.set_fdf('PAO.SplitNorm', 0.15)
atoms.set_calculator(calc)

# Set the VelocityVerlet algorithm and run it
dyn = VelocityVerlet(atoms, dt=1.0 * units.fs, trajectory='si001+h2.traj')
dyn.run(steps=100)
Exemplo n.º 35
0
#opt.run(fmax=1e-3)

ase.io.write('crack_3.xyz', c, format='extxyz')

dyn = VelocityVerlet(c, params.dt, logfile=None)
set_initial_velocities(dyn.atoms)    

crack_pos = []
traj = NetCDFTrajectory('traj.nc', 'w', c)
dyn.attach(traj.write, 10, dyn.atoms, arrays=['stokes', 'momenta'])
dyn.attach(find_crack_tip, 10, dyn.atoms,
           dt=params.dt*10, store=True, results=crack_pos)

# run for 2000 time steps to reach steady state at initial load
for i in range(20):
    dyn.run(100)
    if extend_strip(dyn.atoms, params.a, params.N, params.M, params.vacuum):
        set_constraints(dyn.atoms, params.a, delta_strain=None)

# start decreasing strain
set_constraints(dyn.atoms, params.a, delta_strain=delta_strain)

for i in range(1000):
    dyn.run(100)
    if extend_strip(dyn.atoms, params.a, params.N, params.M, params.vacuum):
        set_constraints(dyn.atoms, params.a, delta_strain)

traj.close()

time = 10.0*dyn.dt*np.arange(dyn.get_number_of_steps()/10)
np.savetxt('crackpos.dat', np.c_[time, crack_pos])
Exemplo n.º 36
0
def test_verlet_thermostats_asap(asap3):
    rng = np.random.RandomState(0)
    calculator = asap3.EMT()
    T_low = 10
    T_high = 300
    md_kwargs = {'timestep': 0.5 * fs, 'logfile': '-', 'loginterval': 500}
    with seterr(all='raise'):
        a = bulk('Au').repeat((4, 4, 4))
        a[5].symbol = 'Ag'

        # test thermalization by MaxwellBoltzmannDistribution
        thermalize(T_high, a, rng)
        assert abs(a.get_temperature() - T_high) < 0.0001

        # test conservation of total energy e0 using Verlet
        a_verlet, traj = prepare_md(a, calculator)
        e0 = a_verlet.get_total_energy()
        md = VelocityVerlet(a_verlet, **md_kwargs)
        md.attach(traj.write, 100)
        md.run(steps=10000)
        traj_verlet = read('Au7Ag.traj', index=':')
        assert abs(traj_verlet[-1].get_total_energy() - e0) < 0.0001

        # test reproduction of Verlet by Langevin and Andersen for thermostats
        # switched off
        pos_verlet = [t.get_positions() for t in traj_verlet[:3]]
        md_kwargs.update({'temperature_K': T_high})
        for MDalgo in [Langevin, Andersen]:
            a_md, traj = prepare_md(a, calculator)
            if MDalgo is Langevin:
                md = MDalgo(a_md, friction=0.0, rng=rng, **md_kwargs)
            elif MDalgo is Andersen:
                md = MDalgo(a_md, andersen_prob=0.0, rng=rng, **md_kwargs)
            md.attach(traj, 100)
            md.run(steps=200)
            traj_md = read('Au7Ag.traj', index=':')
            pos_md = [t.get_positions() for t in traj_md[:3]]
            assert np.allclose(pos_verlet, pos_md)  # Verlet reproduced?

            # test thermalization to target temperature by thermostats and
            # conservation of average temperature by thermostats
            md.set_timestep(4 * fs)
            if MDalgo is Langevin:
                md.set_friction(0.01)
            elif MDalgo is Andersen:
                md.set_andersen_prob(0.01)
            thermalize(T_low, a_md, rng)  # thermalize with low temperature (T)
            assert abs(a_md.get_temperature() - T_low) < 0.0001

            md.run(steps=500)  # equilibration, i.e. thermalization to high T
            temp = []

            def recorder():
                temp.append(a_md.get_temperature())

            md.attach(recorder, interval=1)
            md.run(7000)
            temp = np.array(temp)
            avgtemp = np.mean(temp)
            fluct = np.std(temp)
            print("Temperature is {:.2f} K +/- {:.2f} K".format(
                avgtemp, fluct))
            assert abs(avgtemp - T_high) < 10.0
Exemplo n.º 37
0
    if atoms[i].number != atomic_numbers['Ru']:
        atoms[i].number = atomic_numbers['Ru']
        n += 1
atoms.set_calculator(calc)

# Set initial momentum
MaxwellBoltzmannDistribution(atoms, 1000 * units.kB)

# Run dynamics
startcpu, startwall = time.clock(), time.time()

dyn = VelocityVerlet(atoms,
                     1.0 * units.fs,
                     logfile='test-energy.dat',
                     loginterval=10)
dyn.run(10)
etot = (atoms.get_potential_energy() + atoms.get_kinetic_energy()) / len(atoms)
#obs.close()
for i in range(25):
    if i:
        dyn.run(1000)
    epot = atoms.get_potential_energy() / len(atoms)
    ekin = atoms.get_kinetic_energy() / len(atoms)
    print "%9.5f %9.5f   %9.5f" % (epot, ekin, epot + ekin)
    ReportTest("Step %i." % (i, ), epot + ekin, etot, 1e-3, silent=True)

cpu, wall = time.clock() - startcpu, time.time() - startwall
fraction = cpu / wall

print ""
print ""
Exemplo n.º 38
0
def ribs(params, voidcount, voidrad, frame_count=1000):

    calc = IdealBrittleSolid(rc=params.rc,
                             k=params.k,
                             a=params.a,
                             beta=params.beta)

    x_dimer = np.linspace(params.a - (params.rc - params.a),
                          params.a + 1.1 * (params.rc - params.a), 51)
    dimers = [
        Atoms('Si2', [(0, 0, 0), (x, 0, 0)], cell=[10., 10., 10.], pbc=True)
        for x in x_dimer
    ]
    calc.set_reference_crystal(dimers[0])
    e_dimer = []
    f_dimer = []
    f_num = []
    for d in dimers:
        d.set_calculator(calc)
        e_dimer.append(d.get_potential_energy())
        f_dimer.append(d.get_forces())
        f_num.append(calc.calculate_numerical_forces(d))
    e_dimer = np.array(e_dimer)
    f_dimer = np.array(f_dimer)
    f_num = np.array(f_num)
    assert abs(f_dimer - f_num).max() < 0.1

    #! crystal is created here, the length and height can be modified here as well
    #! edit 3N changed to different values to test
    crystal = triangular_lattice_slab(params.a, params.lm * params.N, params.N)
    calc.set_reference_crystal(crystal)
    crystal.set_calculator(calc)

    e0 = crystal.get_potential_energy()
    l = crystal.cell[0, 0]
    h = crystal.cell[1, 1]
    print('l=', l, 'h=', h)

    # compute surface (Griffith) energy
    b = crystal.copy()
    b.set_calculator(calc)
    shift = calc.parameters['rc'] * 2
    y = crystal.positions[:, 1]
    b.positions[y > h / 2, 1] += shift
    b.cell[1, 1] += shift
    e1 = b.get_potential_energy()
    E_G = (e1 - e0) / l
    print('Griffith energy', E_G)

    # compute Griffith strain
    eps = 0.0  # initial strain is zero
    eps_max = 2 / np.sqrt(3) * (params.rc - params.a) * np.sqrt(
        params.N - 1) / h  # Griffith strain assuming harmonic energy
    deps = eps_max / 100.  # strain increment
    e_over_l = 0.0  # initial energy per unit length is zero
    energy = []
    strain = []
    while e_over_l < E_G:
        c = crystal.copy()
        c.set_calculator(calc)
        c.positions[:, 1] *= (1.0 + eps)
        c.cell[1, 1] *= (1.0 + eps)
        e_over_l = c.get_potential_energy() / l
        energy.append(e_over_l)
        strain.append(eps)
        eps += deps

    energy = np.array(energy)
    eps_of_e = interp1d(energy, strain, kind='linear')
    eps_G = eps_of_e(E_G)

    print('Griffith strain', eps_G)

    c = crystal.copy()
    c.info['E_G'] = E_G
    c.info['eps_G'] = eps_G

    # open up the cell along x and y by introducing some vaccum
    orig_cell_width = c.cell[0, 0]
    orig_cell_height = c.cell[1, 1]
    c.center(params.vacuum, axis=0)
    c.center(params.vacuum, axis=1)

    # centre the slab on the origin
    c.positions[:, 0] -= c.positions[:, 0].mean()
    c.positions[:, 1] -= c.positions[:, 1].mean()

    c.info['cell_origin'] = [-c.cell[0, 0] / 2, -c.cell[1, 1] / 2, 0.0]
    ase.io.write('crack_1.xyz', c, format='extxyz')

    width = (c.positions[:, 0].max() - c.positions[:, 0].min())
    height = (c.positions[:, 1].max() - c.positions[:, 1].min())

    c.info['OrigHeight'] = height

    print((
        'Made slab with %d atoms, original width and height: %.1f x %.1f A^2' %
        (len(c), width, height)))

    top = c.positions[:, 1].max()
    bottom = c.positions[:, 1].min()
    left = c.positions[:, 0].min()
    right = c.positions[:, 0].max()

    crack_seed_length = 0.2 * width
    strain_ramp_length = 8.0 * params.a  # make this bigger until crack looks nicer
    delta_strain = params.strain_rate * params.dt

    # fix top and bottom rows, and setup Stokes damping mask
    # initial use constant strain
    set_constraints(c, params.a)

    # apply initial displacment field
    c.positions[:, 1] += thin_strip_displacement_y(
        c.positions[:, 0], c.positions[:, 1], params.delta * eps_G,
        left + crack_seed_length,
        left + crack_seed_length + strain_ramp_length)

    print('Applied initial load: delta=%.2f strain=%.4f' %
          (params.delta, params.delta * eps_G))

    ase.io.write('crack_2.xyz', c, format='extxyz')

    c.set_calculator(calc)

    cl, cs, cr = calc.get_wave_speeds(c)

    print("rayleigh speed = %f" % cr)

    # relax initial structure
    # opt = FIRE(c)
    # opt.run(fmax=1e-3)

    ase.io.write('crack_3.xyz', c, format='extxyz')

    #length and height of the slab is defined here
    L = params.N * params.lm
    H = params.N * 2
    #Atomic Void Simulations
    #😵------------------------------------------------------------------------------
    #the following lines of code were written to convert 1D positions into a 2D array
    #so as to make manipulation of slab easier
    if True:
        #void parameters are defined here.
        #around a max of [0.3--1.7] recommended
        y_offset = 0.1
        #y offset is a fraction of distance from the end of the slab
        x_offset = 0.4
        rad = voidrad

        #this reference code is for 160x40 slab
        #in steps of 40 i.e. the height, create a list upto the length of slab
        #row0 = range(0,6400,40)
        row0 = range(0, len(c), H)
        #the 2D array will be held in slab
        slab = []
        for col in range(H):
            row = []
            for r in row0:
                i = col + r
                row.append(c.positions[i])
            slab.append(row)

        slab = np.array(slab)
        #all items in the array reversed, needed because the salb is built from bottom left up
        #in other words, reflected in x axis
        slab = slab[::-1]
        # print(slab[0])

        # slab[modifiers.mask(h=H,w=L, center=[int(L*x_offset),int((H - 1)*y_offset)], radius=rad)] = 0

        #multiple voids if need be
        if True:
            for i in range(voidcount):
                y_offset = rand.uniform(0.1, 0.9)
                x_offset = rand.uniform(0.3, 0.95)
                slab[modifiers.mask(
                    h=H,
                    w=L,
                    center=[int(L * x_offset),
                            int((H - 1) * y_offset)],
                    radius=rad)] = 0
        #reversed the slab back again here
        slab = slab[::-1]
        # # this is a useful text-array representation of the slab, for debugging purposes
        # mtext = open('masktest.txt','w')
        # for row in slab:
        #        mtext.write(str(row).replace("\n",",")+"\n")
        # mtext.close

        slab_1d = []
        for col in range(L):
            for row in range(H):
                slab_1d.append(slab[row, col])
        slab_1d = np.array(slab_1d)

        todel = []
        for i in range(len(c)):
            if slab_1d[i][2] == 0:
                todel.append(i)
        print(todel)
        del c[todel]
        # return
    #End of Void Simulation
    #-------------------------------------------------------------------------------

    #Grain Boundary Simulations
    #-------------------------------------------------------------------------------
    if False:
        hi = 2

    #-------------------------------------------------------------------------------

    #! replaced velcityVerlet with Lagevin to add temperature parameter
    if params.v_verlet:
        dyn = VelocityVerlet(c, params.dt * units.fs, logfile=None)
        # set_initial_velocities(dyn.atoms)
    else:
        print("Using NVT!")
        # mbd(c, 20 * units.kB, force_temp = True)
        # dyn = Langevin(c,params.dt*units.fs,params.T*units.kB, 5)
        dyn = NVTBerendsen(c,
                           params.dt * units.fs,
                           params.T,
                           taut=0.5 * 1000 * units.fs)

    #dyn.atoms.rattle(1e-3) # non-deterministic simulations - adjust to suit

    #!simulation outputs numbered, avoids deleting exisiting results
    iterFile = open("simIteration.txt", 'r+')
    iteration = int(iterFile.readlines()[0]) + 1
    if params.overwrite_output:
        iteration -= 1
    iterFile.seek(0, 0)
    iterFile.write(str(iteration))
    iterFile.close()

    dir = "./.simout/void/sim_" + str(iteration) + "_" + str(
        params.keep_test).lower() + "_" + params.desc + "/"
    cf.createFolder(dir)

    #!Saving parameter values for each iteration of the simulation
    logFile = open(dir + "params_log.txt", 'w')
    for p, value in params.compose_params().iteritems():
        logFile.write(p + " ==> " + str(value) + "\n")
    logFile.close
    crack_pos = []
    if params.keep_test:
        traj = NetCDFTrajectory(dir + 'traj' + str(iteration) + '.nc', 'w', c)
        dyn.attach(traj.write, 10, dyn.atoms, arrays=['stokes', 'momenta'])

    # #! isolating crack tip_x for saving
    # crack_tip_file2 = open(dir+'tip_x.txt','w')
    # crack_tip_file2.close()
    tip_x_file = open(dir + 'tip_x.txt', 'a')
    console_output = open(dir + 'console_output.txt', 'a')
    coord_file = open(dir + 'coordinates.csv', 'a')

    coordinates = []
    distances = []

    dyn.attach(find_crack_tip,
               10,
               dyn.atoms,
               tipxfile=tip_x_file,
               cout=console_output,
               coord=coordinates,
               d=distances,
               dt=params.dt * 10,
               store=True,
               results=crack_pos)

    # run for 2000 time steps to reach steady state at initial load
    # for i in range(10):
    #     dyn.run(250)
    #     if extend_strip(dyn.atoms, params.a, params.N, params.M, params.vacuum):
    #         set_constraints(dyn.atoms, params.a)

    # start decreasing strain
    #set_constraints(dyn.atoms, params.a, delta_strain=delta_strain)

    # strain_atoms = ConstantStrainRate(dyn.atoms.info['OrigHeight'],
    #                                   delta_strain)
    # dyn.attach(strain_atoms.apply_strain, 1, dyn. atoms   )

    # for i in range(50):
    #     dyn.run(100)
    #     if extend_strip(dyn.atoms, params.a, params.N, params.M, params.vacuum):
    #         set_constraints(dyn.atoms, params.a)

    # #cleardel dyn.observers[-1] # stop increasing the strain

    # for i in range(1000):
    #     dyn.run(100)
    #     if extend_strip(dyn.atoms, params.a, params.N, params.M, params.vacuum):
    #         set_constraints(dyn.atoms, params.a)

    dyn.run(int(1 * frame_count) * 10 + 10)

    # print("\n\n\n\n\n -----Adding Temperature------\n\n\n\n\n")
    # # mbd(c, 2*params.T * units.kB, force_temp = True)
    # dyn.set_temperature(params.T*units.kB)
    # dyn.run(int(0.5*frame_count)*10+10)
    for c in coordinates:
        coord_file.write(str(c[0]) + ',' + str(c[1]) + '\n')
    coord_file.close()

    if params.keep_test:
        traj.close()
    tip_x_file.close()
    console_output.close()
Exemplo n.º 39
0
from ase.md import VelocityVerlet
from ase.build import bulk
from ase.units import kB
from ase.md.velocitydistribution import MaxwellBoltzmannDistribution
from ase.calculators.idealgas import IdealGas
import numpy as np

atoms = bulk('Kr').repeat((10, 10, 10))
assert len(atoms) == 1000

atoms.center(vacuum=100)
atoms.set_calculator(IdealGas())

T = 1000

MaxwellBoltzmannDistribution(atoms, T * kB)
print("Temperature: {} K".format(atoms.get_temperature()))

md = VelocityVerlet(atoms, timestep=0.1)
for i in range(5):
    md.run(5)
    s = atoms.get_stress(include_ideal_gas=True)
    p = -s[:3].sum() / 3
    v = atoms.get_volume()
    N = len(atoms)
    T = atoms.get_temperature()
    print("pV = {}  NkT = {}".format(p * v, N * kB * T))
    assert np.fabs(p * v - N * kB * T) < 1e-6
Exemplo n.º 40
0
from ase import Atoms
from ase.calculators.emt import EMT
from ase.md import VelocityVerlet
from ase.io import Trajectory

a = 3.6
b = a / 2
fcc = Atoms('Cu', positions=[(0, 0, 0)],
            cell=[(0, b, b), (b, 0, b), (b, b, 0)],
            pbc=1)
fcc *= (2, 1, 1)
fcc.set_calculator(EMT())
fcc.set_momenta([(0.9, 0.0, 0.0), (-0.9, 0, 0)])
md = VelocityVerlet(fcc, dt=0.1)
def f():
    print(fcc.get_potential_energy(), fcc.get_total_energy())
md.attach(f)
md.attach(Trajectory('Cu2.traj', 'w', fcc).write, interval=3)
md.run(steps=20)
fcc2 = Trajectory('Cu2.traj', 'r')[-1]