Skip to content

Commit a385f9f

Browse files
feat: enhance robustness considering charge and manually-set multiplicity
1 parent 7631c46 commit a385f9f

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

dpgen/generator/lib/cp2k.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,18 +244,23 @@ def iterdict(d, out_list, flag=None, indent=0):
244244
out_list.insert(index, " " * indent + k + " " + v)
245245

246246

247-
def calculate_multiplicity(atom_names, atom_types):
247+
def calculate_multiplicity(atom_names, atom_types, charge=0):
248248
"""
249-
Calculate the multiplicity based on atom species and quantities.
249+
Calculate the multiplicity based on atom species, quantities, and system charge.
250250
:param atom_names: List of element symbols.
251251
:param atom_types: List of atom type indices.
252+
:param charge: System charge (default: 0).
252253
:return: Multiplicity.
253254
"""
254255
# Calculate the total number of electrons
255256
total_electrons = 0
256257
for idx in atom_types:
257258
element = atom_names[idx]
258259
total_electrons += atomic_numbers.get(element, 0)
260+
261+
# Subtract/add electrons based on system charge
262+
# Positive charge means we remove electrons, negative charge means we add electrons
263+
total_electrons -= charge
259264

260265
# Determine multiplicity based on the total number of electrons
261266
# Even number of electrons -> singlet (multiplicity = 1)
@@ -279,7 +284,12 @@ def make_cp2k_input(sys_data, fp_params):
279284

280285
atom_names = sys_data["atom_names"]
281286
atom_types = sys_data["atom_types"]
282-
multiplicity = calculate_multiplicity(atom_names, atom_types)
287+
# Get system charge if provided, default to 0
288+
charge = sys_data.get("charge", 0)
289+
if "MULTIPLICITY" in fp_params.get("FORCE_EVAL", {}).get("DFT", {}):
290+
multiplicity = fp_params["FORCE_EVAL"]["DFT"]["MULTIPLICITY"]
291+
else:
292+
multiplicity = calculate_multiplicity(atom_names, atom_types, charge)
283293

284294
# get update from user
285295
user_config = fp_params

0 commit comments

Comments
 (0)