def optimize_molecule(molecule,
                      calculator,
                      torsions=(),
                      thresh=0.001,
                      optimize_cell=False):
    """
    Optimize the molecule and return the optimized energy
    torsions should be a list of lists with value, i, j, k, l

    :type molecule: ase.Atoms
    :type Calculator: ase Calculator
    :type torsions: iterable of (value, lists of ints) tuple defining torsions to be constrained
    :type thresh: float
    :type optimize_cell: bool
    :return: the energy in hartree
    """

    if torsions:
        constraints = ase.constraints.FixInternals(dihedrals=torsions)
        molecule.set_constraint(constraints)

    molecule.set_calculator(calculator)

    if optimize_cell:
        ucf = ase.constraints.ExpCellFilter(molecule)
        opt = optimize.BFGS(ucf)
    else:
        opt = optimize.BFGS(molecule)

    opt.run(fmax=thresh, steps=500)

    return molecule.get_potential_energy() / EV
Beispiel #2
0
def optimizePos(atoms, calc, fmax):
    import ase.optimize as aseopt

    atoms.set_calculator(calc)
    dyn = aseopt.BFGS(atoms=atoms,
                      logfile='qn.log',
                      trajectory='qn.traj',
                      restart='qn.pckl')
    dyn.run(fmax=fmax)
Beispiel #3
0
    def optimizePos(self, atoms, calc, saveWF=False):
        atoms.set_calculator(calc)

        maxForce = np.amax(abs(atoms.get_forces()))
        if maxForce > self.fmax:
            parprint("max force = %f, optimizing positions" % (maxForce))
            dyn = optimize.BFGS(atoms=atoms,
                                logfile='qn.log',
                                trajectory='qn.traj',
                                restart='qn.pckl')
            dyn.run(fmax=self.fmax())

        if saveWF and self.dftcode == 'gpaw':
            atoms.calc.write('preCalc_inp.gpw',
                             mode='all')  #for use in getXCContribs
            atoms, calc = self.gpawRestart()
Beispiel #4
0
def optimizePos(atoms, dft, calcObj, magInit, restart, saveWF):
    ### CREATE CALC
    calcMaker = gpawCalc if dft == 'gpaw' else qeCalc
    calc = calcMaker(calcObj, restart=restart)
    ### INITIALIZE ATOMS OBJECT
    atoms.set_calculator(calc)
    atoms.set_initial_magnetic_moments(magInit)

    ### OPTIMIZE POSITIONS IF NECESSARY
    maxForce = np.amax(abs(atoms.get_forces()))
    if maxForce > calcObj.fMax:
        parprint("max force = %f, optimizing positions" % (maxForce))
        dyn = optimize.BFGS(atoms=atoms,
                            logfile='qn.log',
                            trajectory='qn.traj',
                            restart='qn.pckl')
        dyn.run(fmax=calcObj.fMax)

    if saveWF and dft == 'gpaw':
        atoms.calc.write('preCalc_inp.gpw',
                         mode='all')  #for use in getXCContribs
        atoms, calc = gpawRestart(calcObj)
def supercell_ionic_relaxation(superCellDefect, optimizer_type = 'QuasiNewton',
                               ladder_begin = 0, ladder_end = 2,
                               charge=0):
	# structure relaxation witht he calculator already existing in the atoms object
	# returns the potential energy of the supercell before and after the relaxation as a tuple
	# 	usage: BR, AR = supercell_ionic_relaxation(defect_supercell)
	# if log ==  False, both potential energy will be 0

	# added support for different types of optimizer
	# local optimizer:  QuasiNewton, BFGS, LBFGS, GPMin, MDMin and FIRE.
	# preconditioned optimizer: to be added
	# global optimizer: to be added 

	# added support for the jacob's ladder
	# the ladder: 
	#			LDA,
	# 			PBE,
	# 			revPBE
	#			RPBE,
	#			PBE0,
	#			B3LYP.

	# ladder_begin, beginning step of the ladder, included
	# ladder_end, ending step of the ladder, NOT INCLUDED

	potential_energy_BR = 0.0
	potential_energy_AR = 0.0

	ladder = ['LDA', 'PBE', 'revPBE', 'RPBE', 'PBE0', 'B3LYP']
	# calc = superCellDefect.get_calculator()
	# calc_backup = calc.get_xc_functional()

	# if log:
	# 	io.write('defect_supercell_before_relaxation.cube', superCellDefect)
	# 	potential_energy_BR = superCellDefect.get_potential_energy()
	# else:
	# 	pass

	if ladder_end > len(ladder):
		ladder_end = len(ladder)

	# loop climbing the ladder
	for calc_step in ladder[ ladder_begin : ladder_end ]:
		# print('=====================', file = log_file)
		# print('starting:', file = log_file)
		# print(calc_step, file = log_file)

		calc = GPAW(mode='fd',
		            kpts={'size': (2, 2, 2), 'gamma': False},
		            xc=calc_step,
		            charge=charge,
		            occupations=FermiDirac(0.01)
		            )

		# calc.set(xc = calc_step)
		superCellDefect.set_calculator(calc)

		if optimizer_type == 'QuasiNewton':
			relax = optimize.QuasiNewton(superCellDefect)
		elif optimizer_type == 'BFGS':
			relax = optimize.BFGS(superCellDefect)
		elif optimizer_type == 'LBFGS':	
			relax = optimize.BFGSLineSearch(superCellDefect)
		elif optimizer_type == 'GPMin':
			relax = optimize.GPMin(superCellDefect)
		elif optimizer_type == 'FIRE':
			relax = optimize.FIRE(superCellDefect)
		elif optimizer_type == 'MDMin':
			relax = optimize.MDMin(superCellDefect)
		else:
			print('optimizer not supported at the moment, falling back to QuasiNewton')
			relax = optimize.QuasiNewton(superCellDefect)

		relax.run(fmax = 0.05)

		potential_current = superCellDefect.get_potential_energy()
		# print('lattice energy after relaxation: %5.7f eV' % potential_current, file = log_file)
		# print('=====================', file = log_file)

	# put the original xc back to the calculator
	# calc.set(xc = calc_backup)

	# relax = QuasiNewton(superCellDefect)
	# relax.run(fmax=0.05)
	
	# if log:
	# 	io.write('defect_supercell_after_relaxation.cube', superCellDefect)
	# 	potential_energy_AR = superCellDefect.get_potential_energy()
	# else:
	# 	pass

	return potential_energy_BR, potential_energy_AR
calc = espresso(
    pw=pw,
    dw=pw * 10,
    xc=xc,
    kpts=kpt,
    spinpol=spinpol,
    convergence={
        'energy': eConv,
        'mixing': mixing,
        'nmix': nmix,
        'maxsteps': maxsteps,
        'diag': 'david'
    },
    nbands=nbands,
    sigma=sigma,
    dipole={'status': dipole},
    outdir='calcdir'  #output directory
    ,
    psppath='/nfs/slac/g/suncatfs/sw/external/esp-psp/gbrv1.5pbe',
    output={'removesave': True})
atoms.set_calculator(calc)
dyn = optimize.BFGS(atoms=atoms,
                    logfile='qn.log',
                    trajectory='qn.traj',
                    restart='qn.pckl')
dyn.run(fmax=0.05)
io.write('out.traj', calc.get_final_structure())
with open('converged.log', 'w') as f:
    f.write(str(atoms.get_potential_energy()))
Beispiel #7
0
        outdir      = calcdir,
        output      = output,
        parflags    = parflags)


############################
# Coarse Optimization
############################
initial.set_calculator(calc)

Traj = PickleTrajectory(traj,'a',initial)

initial.rattle(stdev= 0.01)

print 'pw = %d, fmax = %f'%(.8*pw_cutoff,2*fmax)
dyn = optimize.BFGS(initial, logfile='qn.log', trajectory=Traj)
dyn.run(fmax=2*fmax)

print 'pw %d to f %f'%(pw_cutoff,fmax)


##############################
# Fine Optimization
##############################

initial.set_initial_magnetic_moments(magmoms)
initial.set_calculator(calc2)
dyn = optimize.BFGS(initial, logfile='qn.log', trajectory=Traj)
dyn.run(fmax=fmax)

######################