Open ireaml opened 5 months ago
@ireaml Thanks for reporting. At first glance, everything looks ok with the script. I will run a few tests and get back to you. Could you please tell me how you did the integration after the run?
Thanks for the quick response!
I just used an adapted version of the calphy.phase.integrate_reversible_scaling()
setting scale_energy=False
as exemplified in calphy.routines.routine_tscale
](https://github.com/ICAMS/calphy/blob/main/calphy/routines.py#L376C5-L376C19) (and providing the corresponding values for the initial temperature, pressure and the free energy at the initial temperature. It's the same script I used for ts
(but setting scale_energy=False
for tscale
).
import os
import numpy as np
from scipy.integrate import cumtrapz
import scipy.constants as const
from ase.io.lammpsdata import read_lammps_data
#Constants
h = const.physical_constants["Planck constant in eV/Hz"][0]
hbar = h/(2*np.pi)
kb = const.physical_constants["Boltzmann constant in eV/K"][0]
kbJ = const.physical_constants["Boltzmann constant"][0]
Na = const.physical_constants["Avogadro constant"][0]
eV2J = const.eV
def _integrate_rs(
simfolder,
f0,
t,
natoms,
p=0,
nsims=5,
scale_energy=False,
return_values=False,
):
ws = []
p = p/(10000*160.21766208)
# The code below is adapted to read forward/backward files from different directory structures
files = os.listdir(simfolder)
fwd_files = [f for f in files if f.startswith("ts.forward_")]
if fwd_files:
print("Found forward files:", fwd_files)
files_fwd = [f for f in os.listdir(simfolder) if f.startswith("ts.forward_")]
files_bwd = [f for f in os.listdir(simfolder) if f.startswith("ts.backward_")]
simfolders = [simfolder] * len(files_fwd)
# else if we have Iter_i folders with each containing the .dat files
elif os.path.exists(os.path.join(simfolder, "Iter_1", "ts.forward_1.dat")):
folders = [f for f in os.listdir(simfolder) if f.startswith("Iter_")]
simfolders = [os.path.join(simfolder, folder) for folder in folders]
# Get forward file in each folder
files_fwd, files_bwd = [], []
for folder in simfolders:
files_fwd.append([f for f in os.listdir(folder) if f.startswith("ts.forward_")][0])
files_bwd.append([f for f in os.listdir(folder) if f.startswith("ts.backward_")][0])
else:
raise FileNotFoundError("No forward files found")
# Get ist of numbers from filenames
nsims = [int(f.split("forward_")[1].split(".")[0]) for f in files_fwd]
print(f"Found {len(nsims)} iterations")
for i in range(len(nsims)):
simfolder = simfolders[i]
fdx, fp, fvol, flambda = np.loadtxt(
os.path.join(simfolder, files_fwd[i]), unpack=True, comments="#"
)
bdx, bp, bvol, blambda = np.loadtxt(
os.path.join(simfolder, files_bwd[i]), unpack=True, comments="#"
)
if scale_energy:
fdx /= flambda
bdx /= blambda
#add pressure contribution
fvol = fvol/natoms
bvol = bvol/natoms
fdx = fdx + p*fvol
bdx = bdx + p*bvol
wf = cumtrapz(fdx, flambda, initial=0)
wb = cumtrapz(bdx[::-1], blambda[::-1], initial=0)
w = (wf + wb) / (2*flambda)
ws.append(w)
wmean = np.mean(ws, axis=0)
werr = np.std(ws, axis=0)
temp = t/flambda
f = f0/flambda + 1.5*kb*temp*np.log(flambda) + wmean
if not return_values:
outfile = os.path.join(simfolder, "temperature_sweep.dat")
np.savetxt(outfile, np.column_stack((temp, f, werr)))
else:
return (temp, f, were)
Thanks! I will take a look and get back to you. I should be able to take a look before the end of the week.
Hi!
Thanks for developing such a useful package!
I have used the reversible scaling method to calculate free energies at high temperatures (switching from 100 K to 840 K). It works great and agrees with equilibrium thermodynamic integration. I wanted to switch to the
tscale
method because it would allow me to use theKOKKOS
acceleration withlammps
(not available for thepair hybrid/scaled
). However, when comparing the results fromts
andtscale
, seems like there's something wrong withtscale
(as shown below)?I've tested using long equilibration and switching times for
tscale
(N_eq = 25000 and N_switch=250000 with dt=0.002 ps) but we get similar results as for the short switching time (shown in the figure above). My system is a Tellurium interstitial in CdTe but I've observed the same issue for bulk CdTe (with a different forcefield).All settings between
tscale
andts
are the same. I've checked the equilibrated structure after thetscale
switch from 100 K to 840 K and looks ok. The lammps input script I used for thetscale
simulation is attached below (adapted fromcalphy.phase.temperature_scaling
).Wondering if you've seen this disagreement before or have an idea of what could be causing it?
Thank you!
Input
lammps
file fortscale
simulation: