Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi lattice constant optimization #47

Merged
merged 3 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 75 additions & 33 deletions pynta/calculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,37 +348,79 @@ def add_sella_constraint(cons,d):
constructor(**constraint_dict)
return

def get_lattice_parameter(metal,surface_type,software,software_kwargs,da=0.1,options={"xatol":1e-4},a0=None):
def get_lattice_parameters(metal,surface_type,software,software_kwargs,da=0.1,a0=None):
soft = name_to_ase_software(software)(**software_kwargs)
def f(a):
slab = bulk(metal,surface_type[:3],a=a)
slab.calc = soft
slab.pbc = (True, True, True)
return slab.get_potential_energy()
if a0 is None:
a0 = reference_states[chemical_symbols.index(metal)]['a']
avals = np.arange(a0-da,a0+da,0.01)
outavals = []
Evals = []
print("a,E")
for a in avals:
try:
E = f(a)
outavals.append(a)
Evals.append(E)
print((a,E))
except:
pass
print("a values:")
print(outavals)
print("E values:")
print(Evals)
inds = np.argsort(np.array(Evals))[:7]
p = np.polyfit(np.array(outavals)[inds],np.array(Evals)[inds],2)
a = -p[1]/(2.0*p[0])
print("ASE reference a: {}".format(a0))
print("Interpolated a: {}".format(a))
out = opt.minimize_scalar(f,method='bounded',bounds=(a-0.01,a+0.01),options=options)
print(out)
print("Optimized a: {}".format(out.x))
return out.x
if surface_type != "hcp0001":
options={"xatol":1e-4}
def f(a):
slab = bulk(metal,surface_type[:3],a=a)
slab.calc = soft
slab.pbc = (True, True, True)
return slab.get_potential_energy()
if a0 is None:
a0 = reference_states[chemical_symbols.index(metal)]['a']
avals = np.arange(a0-da,a0+da,0.01)
outavals = []
Evals = []
print("a,E")
for a in avals:
try:
E = f(a)
outavals.append(a)
Evals.append(E)
print((a,E))
except:
pass
print("a values:")
print(outavals)
print("E values:")
print(Evals)
inds = np.argsort(np.array(Evals))[:7]
p = np.polyfit(np.array(outavals)[inds],np.array(Evals)[inds],2)
a = -p[1]/(2.0*p[0])
print("ASE reference a: {}".format(a0))
print("Interpolated a: {}".format(a))
out = opt.minimize_scalar(f,method='bounded',bounds=(a-0.01,a+0.01),options=options)
print(out)
print("Optimized a: {}".format(out.x))
return out.x
else:
options={"gtol":1e-10,'xrtol':0.0001}
def f(a):
slab = bulk(metal,surface_type[:3],a=a[0],c=a[1])
slab.calc = soft
slab.pbc = (True, True, True)
return slab.get_potential_energy()
if a0 is None:
a0 = reference_states[chemical_symbols.index(metal)]['a']
cpera = reference_states[chemical_symbols.index(metal)]['c/a']
c0 = cpera * a0
print("ASE Reference a,c: {}".format((a0,c0)))

dx = 0.01
avals = a0 * np.linspace(1 - dx, 1 + dx, 3)
cvals = c0 * np.linspace(1 - dx, 1 + dx, 3)
A = np.zeros((9,6))
Evals = np.zeros(9)
iter = 0
for a in avals:
for c in cvals:
A[iter,:] = np.array([1.0,a,c,a**2,a*c,c**2])
Evals[iter] = f([a,c])
iter += 1

p = np.linalg.lstsq(A,Evals)[0]

p1 = p[1:3]
p2 = np.array([(2 * p[3], p[4]),
(p[4], 2 * p[5])])
a02, c02 = np.linalg.solve(p2.T, -p1)

init_guess = [a02,c02]

print("Interpolated a,c: {}".format((a02,c02)))

out = opt.minimize(f,x0=init_guess,method="BFGS",options=options)
print(out)
print("Optimized a,c: {}".format(out.x))
return out.x
23 changes: 15 additions & 8 deletions pynta/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import yaml
from copy import deepcopy
import numpy as np
from pynta.calculator import get_lattice_parameter
from pynta.calculator import get_lattice_parameters
from fireworks import LaunchPad, Workflow
from fireworks.queue.queue_launcher import rapidfire as rapidfirequeue
from fireworks.features.multi_launcher import launch_multiprocess
Expand Down Expand Up @@ -41,7 +41,7 @@ def __init__(self,path,rxns_file,surface_type,metal,label,launchpad_path=None,fw
irc_mode="fixed", #choose irc mode: 'skip', 'relaxed', 'fixed'
lattice_opt_software_kwargs={'kpts': (25,25,25), 'ecutwfc': 70, 'degauss':0.02, 'mixing_mode': 'plain'},
reset_launchpad=False,queue_adapter_path=None,num_jobs=25,max_num_hfsp_opts=None,#max_num_hfsp_opts is mostly for fast testing
Eharmtol=3.0,Eharmfiltertol=30.0,Ntsmin=5,frozen_layers=2,fmaxopt=0.05,fmaxirc=0.1,fmaxopthard=0.05):
Eharmtol=3.0,Eharmfiltertol=30.0,Ntsmin=5,frozen_layers=2,fmaxopt=0.05,fmaxirc=0.1,fmaxopthard=0.05,c=None):

self.surface_type = surface_type
if launchpad_path:
Expand All @@ -56,6 +56,7 @@ def __init__(self,path,rxns_file,surface_type,metal,label,launchpad_path=None,fw
self.vacuum = vacuum
self.a = a
self.pbc = pbc
self.c = c
self.software = software
self.socket = socket
self.repeats = repeats
Expand Down Expand Up @@ -133,14 +134,20 @@ def generate_slab(self,skip_launch=False):
slab_type = getattr(ase.build,self.surface_type)
#optimize the lattice constant
if self.a is None:
a = get_lattice_parameter(self.metal,self.surface_type,self.software,self.lattice_opt_software_kwargs)
print("computed lattice constant of: {} Angstroms".format(a))
self.a = a
else:
a = self.a
a = get_lattice_parameters(self.metal,self.surface_type,self.software,self.lattice_opt_software_kwargs)
print("computed lattice constants of: {} Angstroms".format(a))
if isinstance(a,float):
self.a = a
else:
self.a = a[0]
self.c = a[1]

logger.info('Construct slab with optimal lattice constant')
#construct slab with optimial lattice constant
slab = slab_type(self.metal,self.repeats,a,self.vacuum)
if self.c:
slab = slab_type(symbol=self.metal,size=self.repeats,a=self.a,vacuum=self.vacuum,c=self.c)
else:
slab = slab_type(symbol=self.metal,size=self.repeats,a=self.a,vacuum=self.vacuum)
slab.pbc = self.pbc
write(os.path.join(self.path,"slab_init.xyz"),slab)
self.slab_path = os.path.join(self.path,"slab.xyz")
Expand Down
4 changes: 2 additions & 2 deletions pynta/testCalculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

class UtilsTest(unittest.TestCase):

def test_get_lattice_parameter(self):
a = get_lattice_parameter('Cu','fcc111','EMT',dict())
def test_get_lattice_parameters(self):
a = get_lattice_parameters('Cu','fcc111','EMT',dict())
self.assertAlmostEqual(a,3.5898294220923366)

if __name__ == '__main__':
Expand Down
Loading