Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return self.par
def optimize_openopt(self, solver='interalg'):
if not have_openopt:
raise RuntimeError('OpenOpt not available.')
p = GLP(self.get_cost_function, self.par.get_array(),
lb=self.par.get_lower_bounds(),
ub=self.par.get_upper_bounds())
r = p.solve(solver)
print(r, file=log)
self.set_parameters_from_array(r.xf)
print('=== OPTIMIZED PARAMETER SET ===', file=log)
self.get_cost_function(r.xf)
return self.par
class CombinedFit(Fit):
__slots__ = [ 'targets' ]
def __init__(self, calc, par, targets):
Fit.__init__(self, calc, par)
self.targets = targets
def set_parameters_from_array(self, p):
for target in self.targets:
target.set_parameters_from_array(p)
def set_parameters(self, p):
for target in self.targets:
target.set_parameters(p)
a0 = self.get_lattice_constant()
r_Ec = self.w_Ec*( Ec + self.Ec )
r_a0 = self.w_a0*( a0 - self.a0 )
if log is not None:
print('# %20s Ec = %20.10f eV/at. (%20.10f eV/at.) - %20.10f' \
% ( 'Crystal (%s)' % self.crystalstr, Ec, -self.Ec, r_Ec ))
print('# %20s a0 = %20.10f A (%20.10f A) - %20.10f' \
% ( '', a0, self.a0, r_a0 ))
r = [ r_Ec, r_a0 ]
return r
class FitSurface(Fit):
__slots__ = [ 'a0', 'calc', 'crystal', 'Ec', 'fmax', 'par', 'w_a0', 'w_Ec' ]
def __init__(self, calc, par, els, crystal,
Esurf,
w_Esurf = 1.0):
self.Esurf = Esurf
self.w_Esurf = sqrt(w_Esurf)
self.els = els
self.calc = calc
self.par = par
self.crystal = crystal
if self.C44 is not None:
r_C44 = self.w_C44*( C44 - self.C44 )
r += [ r_C44 ]
if log is not None:
print('# %20s C44 = %20.10f GPa (%20.10f GPa) - %20.10f' \
% ( '', C44/GPa, self.C44/GPa, r_C44 ))
if self.Cp is not None:
r_Cp = self.w_Cp*( Cp - self.Cp )
r += [ r_Cp ]
if log is not None:
print('# %20s Cp = %20.10f GPa (%20.10f GPa) - %20.10f' \
% ( '', Cp/GPa, self.Cp/GPa, r_Cp ))
return r
class FitTetragonalCrystal(Fit):
__slots__ = [ 'a0','c0', 'calc', 'crystal', 'Ec', 'fmax', 'par', 'w_a0','w_c0', 'w_Ec' ]
def __init__(self, calc, par, els,
Ec, a0, c0, c_a=None,
B=None, C11=None, C12=None,C13=None, C33=None, C44=None, C66=None,SFE=None,
w_Ec=1.0, w_a0=1.0,w_c0=1.0, w_c_a=1.0,
w_B=1.0, w_C11=1.0, w_C12=1.0,w_C13=1.0,w_C33=1.0,w_C44=1.0,w_C66=1.0, w_Cp=1.0,w_SFE=None,
fmax=1e-6, eps=0.001,
ecoh_ref=None,
size=[1,1,1]):
Fit.__init__(self, calc, par)
self.els = els
self.a0 = a0
def set_calculator(self, calc):
"""
Set the calculator
"""
pass
def get_residuals(self, log=None):
return self.func(self.par, log=log)
### Single point
class FitSinglePoint(Fit):
__slots__ = [ 'atoms', 'energy', 'forces', 'stress', 'w_energy', 'w_forces',
'w_stress' ]
def __init__(self, calc, par, atoms, w_energy=None, w_forces=None,
w_stress=None):
Fit.__init__(self, calc, par)
self.original_atoms = atoms
self.w_energy = w_energy
self.w_forces = w_forces
self.w_stress = w_stress
self.energy = self.atoms.get_potential_energy()
self.forces = self.atoms.get_forces().copy()
self.stress = self.atoms.get_stress().copy()
r_C44 = self.w_C44*( C44 - self.C44 )
r += [ r_C44 ]
if log is not None:
print('# %20s C44 = %20.10f GPa (%20.10f GPa) - %20.10f' \
% ( '', C44/GPa, self.C44/GPa, r_C44 ))
if self.C66 is not None:
r_C66 = self.w_C66*( C66 - self.C66 )
r += [ r_C66 ]
if log is not None:
print('# %20s C66 = %20.10f GPa (%20.10f GPa) - %20.10f' \
% ( '', C66/GPa, self.C66/GPa, r_C66 ))
return r
class FitHexagonalCrystal(Fit):
__slots__ = [ 'a0', 'c0', 'calc', 'crystal', 'Ec', 'fmax', 'par', 'w_a0',
'w_Ec' ]
def __init__(self, calc, par, els,
Ec, a0, c0,
w_Ec = 1.0, w_a0 = 1.0,
fmax = 0.01):
Fit.__init__(self, calc, par)
self.els = els
self.Ec = Ec
self.a0 = a0
self.c0 = c0
def get_residuals(self, log=None):
D0 = self.atoms.get_potential_energy()
r0 = self.atoms.get_distance(0, 1)
r_D0 = self.w_D0*(D0+self.D0)
r_r0 = self.w_r0*(r0-self.r0)
if log is not None:
print('# %20s D0 = %20.10f eV (%20.10f eV) - %20.10f' \
% ( 'Dimer', D0, -self.D0, r_D0 ), file=log)
print('# %20s r0 = %20.10f A (%20.10f A) - %20.10f' \
% ( '', r0, self.r0, r_r0 ), file=log)
return r_D0, r_r0
class FitCubicCrystal(Fit):
__slots__ = [ 'a0', 'calc', 'crystal', 'Ec', 'fmax', 'par', 'w_a0', 'w_Ec' ]
def __init__(self, calc, par, els,
Ec, a0,
B=None, C11=None, C12=None, C44=None, Cp=None,SFE=None,
w_Ec=1.0, w_a0=1.0,
w_B=1.0, w_C11=1.0, w_C12=1.0, w_C44=1.0, w_Cp=1.0,w_SFE=1.0,
fmax=1e-6, eps=0.001,
ecoh_ref=None,
size=[1,1,1]):
Fit.__init__(self, calc, par)
self.els = els
self.a0 = a0
cp = np.array(self.par.get_array())
dp = cp - p
dp = sqrt(np.sum(dp*dp))
if mix is not None:
self.par.set_array(mix*cp + (1-mix)*p)
self.par.set_variable(globalv)
return self.par
### Generic penalty function
class Penalty(Fit):
__slots__ = [ 'func' ]
def __init__(self, calc, par, func):
Fit.__init__(self, calc, par)
self.func = func
def set_calculator(self, calc):
"""
Set the calculator
"""
pass
def get_residuals(self, log=None):
r += cr
w += list(self.w_forces*np.ones_like(self.forces).flatten())
if self.w_stress is not None:
cr = list(
(self.w_stress*(
self.atoms.get_stress() - self.stress
)/self.stress).flatten()
)
r += cr
w += list(self.w_stress*np.ones_like(self.stress).flatten())
return r
### Specific structures
class FitDimer(Fit):
__slots__ = [ 'D0', 'fmax', 'r0', 'w_D0', 'w_r0' ]
def __init__(self, calc, par, els, D0, r0,
w_D0=1.0, w_r0=1.0,
vacuum=10.0, fmax=1e-6):
Fit.__init__(self, calc, par)
self.els = els
self.D0 = D0
self.r0 = r0
self.w_D0 = sqrt(w_D0)/self.D0
self.w_r0 = sqrt(w_r0)/self.r0