Skip to content
Merged
Changes from 5 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
104 changes: 48 additions & 56 deletions dpti/hti_liq.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,45 +28,64 @@ def make_iter_name(iter_index):
return "task_hti." + ("%04d" % iter_index)


def parse_lj_sigma_epsilon(ret, sparam, hybrid=False):
element_num = sparam.get("element_num", 1)
sigma_key_index = filter(
lambda t: t[0] <= t[1],
((i, j) for i in range(element_num) for j in range(element_num)),
)
activation = sparam["activation"]
epsilon = sparam.get("epsilon", None)
epsilon_0_0 = sparam.get("epsilon_0_0", None)
if hybrid:
pair_coeff_str = "lj/cut/soft "
else:
pair_coeff_str = ""
if epsilon is not None:
assert (
epsilon_0_0 is None
), "epsilon and epsilon_0_0 cannot be set at the same time"
ret += f"variable EPSILON equal {epsilon:f}\n"
for i, j in sigma_key_index:
ret += "pair_coeff {} {} {p:s}${{EPSILON}} {:f} {:f}\n".format(
i + 1,
j + 1,
pair_coeff_str,
sparam["sigma_" + str(i) + "_" + str(j)],
)
else:
assert epsilon_0_0 is not None, "epsilon or epsilon_0_0 must be set"
for i, j in sigma_key_index:
ret += "pair_coeff {} {} {:s}{:f} {:f} {:f}\n".format(
i + 1,
j + 1,
pair_coeff_str,
sparam["epsilon_" + str(i) + "_" + str(j)],
sparam["sigma_" + str(i) + "_" + str(j)],
activation,
)
return ret
Comment on lines +31 to +88
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

The new function centralizes parameter processing nicely, but contains a formatting bug.

This new function appropriately centralizes the logic for processing Lennard-Jones parameters, making the code more maintainable. However, there's a formatting error in the string template.

In line 50, the string contains a {p:s} placeholder, but no corresponding p value is provided in the .format() call. Fix it like this:

-            ret += "pair_coeff      {} {} {p:s}${{EPSILON}} {:f} {:f}\n".format(
+            ret += "pair_coeff      {} {} {}${{EPSILON}} {:f} {:f}\n".format(
                 i + 1,
                 j + 1,
                 pair_coeff_str,
                 sparam["sigma_" + str(i) + "_" + str(j)],
+                activation,
             )
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
def parse_lj_sigma_epsilon(ret, sparam, hybrid=False):
element_num = sparam.get("element_num", 1)
sigma_key_index = filter(
lambda t: t[0] <= t[1],
((i, j) for i in range(element_num) for j in range(element_num)),
)
activation = sparam["activation"]
epsilon = sparam.get("epsilon", None)
epsilon_0_0 = sparam.get("epsilon_0_0", None)
if hybrid:
pair_coeff_str = "lj/cut/soft "
else:
pair_coeff_str = ""
if epsilon is not None:
assert (
epsilon_0_0 is None
), "epsilon and epsilon_0_0 cannot be set at the same time"
ret += f"variable EPSILON equal {epsilon:f}\n"
for i, j in sigma_key_index:
ret += "pair_coeff {} {} {p:s}${{EPSILON}} {:f} {:f}\n".format(
i + 1,
j + 1,
pair_coeff_str,
sparam["sigma_" + str(i) + "_" + str(j)],
)
else:
assert epsilon_0_0 is not None, "epsilon or epsilon_0_0 must be set"
for i, j in sigma_key_index:
ret += "pair_coeff {} {} {:s}{:f} {:f} {:f}\n".format(
i + 1,
j + 1,
pair_coeff_str,
sparam["epsilon_" + str(i) + "_" + str(j)],
sparam["sigma_" + str(i) + "_" + str(j)],
activation,
)
return ret
def parse_lj_sigma_epsilon(ret, sparam, hybrid=False):
element_num = sparam.get("element_num", 1)
sigma_key_index = filter(
lambda t: t[0] <= t[1],
((i, j) for i in range(element_num) for j in range(element_num)),
)
activation = sparam["activation"]
epsilon = sparam.get("epsilon", None)
epsilon_0_0 = sparam.get("epsilon_0_0", None)
if hybrid:
pair_coeff_str = "lj/cut/soft "
else:
pair_coeff_str = ""
if epsilon is not None:
assert (
epsilon_0_0 is None
), "epsilon and epsilon_0_0 cannot be set at the same time"
ret += f"variable EPSILON equal {epsilon:f}\n"
for i, j in sigma_key_index:
ret += "pair_coeff {} {} {}${{EPSILON}} {:f} {:f}\n".format(
i + 1,
j + 1,
pair_coeff_str,
sparam["sigma_" + str(i) + "_" + str(j)],
activation,
)
else:
assert epsilon_0_0 is not None, "epsilon or epsilon_0_0 must be set"
for i, j in sigma_key_index:
ret += "pair_coeff {} {} {:s}{:f} {:f} {:f}\n".format(
i + 1,
j + 1,
pair_coeff_str,
sparam["epsilon_" + str(i) + "_" + str(j)],
sparam["sigma_" + str(i) + "_" + str(j)],
activation,
)
return ret
🧰 Tools
🪛 Ruff (0.8.2)

50-55: .format call is missing argument(s) for placeholder(s): p

(F524)



def _ff_soft_on(lamb, sparam):
nn = sparam["n"]
alpha_lj = sparam["alpha_lj"]
rcut = sparam["rcut"]
epsilon = sparam["epsilon"]
# sigma = sparam['sigma']
activation = sparam["activation"]
ret = ""
ret += f"variable EPSILON equal {epsilon:f}\n"
ret += f"pair_style lj/cut/soft {nn:f} {alpha_lj:f} {rcut:f}\n"
ret = parse_lj_sigma_epsilon(ret, sparam, hybrid=False)

element_num = sparam.get("element_num", 1)
sigma_key_index = filter(
lambda t: t[0] <= t[1],
((i, j) for i in range(element_num) for j in range(element_num)),
)
for i, j in sigma_key_index:
ret += "pair_coeff {} {} ${{EPSILON}} {:f} {:f}\n".format(
i + 1,
j + 1,
sparam["sigma_" + str(i) + "_" + str(j)],
activation,
)

# ret += 'pair_coeff * * ${EPSILON} %f %f\n' % (sigma, activation)
ret += "fix tot_pot all adapt/fep 0 pair lj/cut/soft epsilon * * v_LAMBDA scale yes\n"
ret += "compute e_diff all fep ${TEMP} pair lj/cut/soft epsilon * * v_EPSILON\n"
ret += "compute lj_pe all pair lj/cut/soft\n"
ret += "variable e_diff equal c_lj_pe/v_LAMBDA\n"
return ret


def _ff_deep_on(lamb, sparam, model, if_meam=False, meam_model=None):
nn = sparam["n"]
alpha_lj = sparam["alpha_lj"]
rcut = sparam["rcut"]
epsilon = sparam["epsilon"]
# sigma = sparam['sigma']
activation = sparam["activation"]
ret = ""
ret += f"variable EPSILON equal {epsilon:f}\n"
ret += "variable ONE equal 1\n"
if if_meam:
ret += f"pair_style hybrid/overlay meam lj/cut/soft {nn:f} {alpha_lj:f} {rcut:f}\n"
Expand All @@ -76,20 +95,8 @@ def _ff_deep_on(lamb, sparam, model, if_meam=False, meam_model=None):
ret += f"pair_style hybrid/overlay deepmd {model} lj/cut/soft {nn:f} {alpha_lj:f} {rcut:f}\n"
ret += "pair_coeff * * deepmd\n"

element_num = sparam.get("element_num", 1)
sigma_key_index = filter(
lambda t: t[0] <= t[1],
((i, j) for i in range(element_num) for j in range(element_num)),
)
for i, j in sigma_key_index:
ret += "pair_coeff {} {} lj/cut/soft ${{EPSILON}} {:f} {:f}\n".format(
i + 1,
j + 1,
sparam["sigma_" + str(i) + "_" + str(j)],
activation,
)
ret = parse_lj_sigma_epsilon(ret, sparam, hybrid=True)

# ret += 'pair_coeff * * lj/cut/soft ${EPSILON} %f %f\n' % (sigma, activation)
if if_meam:
ret += "fix tot_pot all adapt/fep 0 pair meam scale * * v_LAMBDA\n"
ret += "compute e_diff all fep ${TEMP} pair meam scale * * v_ONE\n"
Expand All @@ -98,20 +105,16 @@ def _ff_deep_on(lamb, sparam, model, if_meam=False, meam_model=None):
"fix tot_pot all adapt/fep 0 pair deepmd scale * * v_LAMBDA\n"
)
ret += "compute e_diff all fep ${TEMP} pair deepmd scale * * v_ONE\n"
ret += "variable e_diff equal c_e_diff[1]\n"
return ret


def _ff_soft_off(lamb, sparam, model, if_meam=False, meam_model=None):
nn = sparam["n"]
alpha_lj = sparam["alpha_lj"]
rcut = sparam["rcut"]
epsilon = sparam["epsilon"]
# sigma = sparam['sigma']
activation = sparam["activation"]
ret = ""
ret += "variable INV_LAMBDA equal 1-${LAMBDA}\n"
ret += f"variable EPSILON equal {epsilon:f}\n"
ret += "variable INV_EPSILON equal -${EPSILON}\n"
if if_meam:
ret += f"pair_style hybrid/overlay meam lj/cut/soft {nn:f} {alpha_lj:f} {rcut:f}\n"
ret += f'pair_coeff * * meam {meam_model["library"]} {meam_model["element"]} {meam_model["potential"]} {meam_model["element"]}\n'
Expand All @@ -120,22 +123,11 @@ def _ff_soft_off(lamb, sparam, model, if_meam=False, meam_model=None):
ret += f"pair_style hybrid/overlay deepmd {model} lj/cut/soft {nn:f} {alpha_lj:f} {rcut:f}\n"
ret += "pair_coeff * * deepmd\n"

element_num = sparam.get("element_num", 1)
sigma_key_index = filter(
lambda t: t[0] <= t[1],
((i, j) for i in range(element_num) for j in range(element_num)),
)
for i, j in sigma_key_index:
ret += "pair_coeff {} {} lj/cut/soft ${{EPSILON}} {:f} {:f}\n".format(
i + 1,
j + 1,
sparam["sigma_" + str(i) + "_" + str(j)],
activation,
)
ret = parse_lj_sigma_epsilon(ret, sparam, hybrid=True)

# ret += 'pair_coeff * * lj/cut/soft ${EPSILON} %f %f\n' % (sigma, activation)
ret += "fix tot_pot all adapt/fep 0 pair lj/cut/soft epsilon * * v_INV_LAMBDA scale yes\n"
ret += "compute e_diff all fep ${TEMP} pair lj/cut/soft epsilon * * v_INV_EPSILON\n"
ret += "compute lj_pe all pair lj/cut/soft\n"
ret += "variable e_diff equal -c_lj_pe/v_INV_LAMBDA\n"
return ret


Expand Down Expand Up @@ -202,7 +194,7 @@ def _gen_lammps_input_ideal(
ret += f"timestep {timestep}\n"
ret += "compute allmsd all msd\n"
ret += "thermo ${THERMO_FREQ}\n"
ret += "thermo_style custom step ke pe etotal enthalpy temp press vol c_e_diff[1] c_allmsd[*]\n"
ret += "thermo_style custom step ke pe etotal enthalpy temp press vol v_e_diff c_allmsd[*]\n"
ret += "thermo_modify format 9 %.16e\n"
ret += "dump 1 all custom ${DUMP_FREQ} dump.hti id type x y z vx vy vz\n"
if ens == "nvt":
Expand Down
Loading