@@ -244,18 +244,23 @@ def iterdict(d, out_list, flag=None, indent=0):
244
244
out_list .insert (index , " " * indent + k + " " + v )
245
245
246
246
247
- def calculate_multiplicity (atom_names , atom_types ):
247
+ def calculate_multiplicity (atom_names , atom_types , charge = 0 ):
248
248
"""
249
- Calculate the multiplicity based on atom species and quantities .
249
+ Calculate the multiplicity based on atom species, quantities, and system charge .
250
250
:param atom_names: List of element symbols.
251
251
:param atom_types: List of atom type indices.
252
+ :param charge: System charge (default: 0).
252
253
:return: Multiplicity.
253
254
"""
254
255
# Calculate the total number of electrons
255
256
total_electrons = 0
256
257
for idx in atom_types :
257
258
element = atom_names [idx ]
258
259
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
259
264
260
265
# Determine multiplicity based on the total number of electrons
261
266
# Even number of electrons -> singlet (multiplicity = 1)
@@ -279,7 +284,12 @@ def make_cp2k_input(sys_data, fp_params):
279
284
280
285
atom_names = sys_data ["atom_names" ]
281
286
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 )
283
293
284
294
# get update from user
285
295
user_config = fp_params
0 commit comments