# The plotter def invisible_atoms(a): """Return True for atoms that should be invisible.""" r = atoms.get_positions() centerofmass = r.sum(axis=0) / len(atoms) return (r[:,2] < centerofmass[2]) plotter = PrimiPlotter(atoms) plotter.set_invisibility_function(invisible_atoms) plotter.set_colors(mycolors) # Map tags to colors # plotter.set_output(X11Window()) # Plot in a window on the screen plotter.set_output(JpegFile("ptm")) # Save plots in files plt0000.gif ... plotter.set_rotation((10.0, 5.0, 0)) # Attach the plotter to the PTMobserver object. That guarantees # that the plotter is called AFTER the PTM analysis has been done. # Similarly, a Trajectory should be attached to the PTMobserver # object. By using interval=1 (the default), the plotter is called # every time PTMobserver is called, i.e. every plotinterval # timesteps. ptm.attach(plotter.plot) # The main loop for t in temperatures: dyn.set_temperature(units.kB*t) for i in range(nsteps/100): dyn.run(100) print "E_total = %-10.5f T = %.0f K (goal: %.0f K, step %d of %d)" %\ (atoms.get_total_energy()/len(atoms), atoms.get_temperature(), t, i, nsteps/100)
else: atoms = None atoms = MakeParallelAtoms(atoms, cpulayout) atoms.set_calculator(EMT()) print "Number of atoms:", atoms.get_number_of_atoms() print "Heating to %d K using Langevin" % T_goal lgv = Langevin(atoms, 5 * units.fs, temperature=2*T_goal*units.kB, friction=0.05) while atoms.get_kinetic_energy() < 1.5 * atoms.get_number_of_atoms() * T_goal * units.kB: lgv.run(5) T = atoms.get_kinetic_energy() / (1.5 * atoms.get_number_of_atoms() * units.kB) print "Temperature is now %.2f K" % (T,) print "Desired temperature reached!" lgv.set_temperature(T_goal*units.kB) for i in range(2): lgv.run(20) s = atoms.get_stress() p = -(s[0] + s[1] + s[2])/3.0 / units.GPa T = atoms.get_kinetic_energy() / (1.5 * atoms.get_number_of_atoms() * units.kB) print "Pressure is %f GPa, desired pressure is %f GPa (T = %.2f K)" % (p, p_goal, T) dv = (p - p_goal) / bulk print "Adjusting volume by", dv cell = atoms.get_cell() atoms.set_cell(cell * (1.0 + dv/3.0)) T = atoms.get_kinetic_energy() / (1.5 * atoms.get_number_of_atoms() * units.kB) print "Temperature is now %.2f K" % (T,)
# Make a trajectory traj = PickleTrajectory('MD_Cluster.traj', "w", atoms) dyn.attach(traj, interval=500) # Automatically writes the initial configuration # Print the energies def printenergy(a, step=[ 0, ]): n = len(a) ekin = a.get_kinetic_energy() / n epot = a.get_potential_energy() / n print("%4d: E_kin = %-9.5f E_pot = %-9.5f E_tot = %-9.5f T = %.1f K" % (step[0], ekin, epot, ekin + epot, 2.0 / 3.0 * ekin / units.kB)) step[0] += 1 printenergy(atoms) view(atoms) #If computer is busy, the plot will appear with some delay. # Now do the dynamics, doing 5000 timesteps, writing energies every 50 steps dyn.attach(printenergy, 50, atoms) dyn.run(5000) view(atoms) # Now increase the temperature to 2000 K and continue dyn.set_temperature(2000 * units.kB) dyn.run(5000) view(atoms)
print "Heating to %d K using Langevin" % T_goal lgv = Langevin(atoms, 5 * units.fs, temperature=2 * T_goal * units.kB, friction=0.05) while atoms.get_kinetic_energy( ) < 1.5 * atoms.get_number_of_atoms() * T_goal * units.kB: lgv.run(5) T = atoms.get_kinetic_energy() / (1.5 * atoms.get_number_of_atoms() * units.kB) print "Temperature is now %.2f K" % (T, ) print "Desired temperature reached!" lgv.set_temperature(T_goal * units.kB) for i in range(2): lgv.run(20) s = atoms.get_stress() p = -(s[0] + s[1] + s[2]) / 3.0 / units.GPa T = atoms.get_kinetic_energy() / (1.5 * atoms.get_number_of_atoms() * units.kB) print "Pressure is %f GPa, desired pressure is %f GPa (T = %.2f K)" % ( p, p_goal, T) dv = (p - p_goal) / bulk print "Adjusting volume by", dv cell = atoms.get_cell() atoms.set_cell(cell * (1.0 + dv / 3.0)) T = atoms.get_kinetic_energy() / (1.5 * atoms.get_number_of_atoms() *
# Set the momenta corresponding to T=1600K. The temperature will # quickly drop to half of that as the energy is distributed evenly # among the kinetic and potential energy. MaxwellBoltzmannDistribution(atoms, 1600*units.kB) # Make a trajectory traj = PickleTrajectory('MD_Cluster.traj', "w", atoms) dyn.attach(traj, interval=500) # Automatically writes the initial configuration # Print the energies def printenergy(a, step=[0,]): n = len(a) ekin = a.get_kinetic_energy() / n epot = a.get_potential_energy() / n print ("%4d: E_kin = %-9.5f E_pot = %-9.5f E_tot = %-9.5f T = %.1f K" % (step[0], ekin, epot, ekin+epot, 2.0/3.0*ekin/units.kB)) step[0] += 1 printenergy(atoms) view(atoms) #If computer is busy, the plot will appear with some delay. # Now do the dynamics, doing 5000 timesteps, writing energies every 50 steps dyn.attach(printenergy, 50, atoms) dyn.run(5000) view(atoms) # Now increase the temperature to 2000 K and continue dyn.set_temperature(2000 * units.kB) dyn.run(5000) view(atoms)
# Interval between plots plotinterval = 500 # Make the Langevin dynamics module dyn = Langevin(atoms, 5*units.fs, units.kB*temperatures[0], 0.002) # The plotter plotter = PrimiPlotter(atoms) plotter.set_output(X11Window()) plotter.set_rotation((10.0, 5.0, 0)) dyn.attach(plotter.plot, interval=plotinterval) # Some functions for calculating the actual temperature, energy, ... def temperature(a): return 2.0/3.0 * a.get_kinetic_energy() / (len(a) * units.kB) def etotal(a): return (a.get_kinetic_energy() + a.get_potential_energy()) / len(a) # The main loop for t in temperatures: dyn.set_temperature(units.kB*t) for i in range(nsteps/100): dyn.run(100) print "E_total = %-10.5f T = %.0f K (goal: %.0f K, step %d of %d)" %\ (etotal(atoms), temperature(atoms), t, i, nsteps/100)