@@ -86,25 +86,22 @@ def _remove_unwanted_atoms(atom_section, atoms_to_keep):
8686 return [atom for atom in atom_section if atom [0 ] in atoms_to_keep ]
8787
8888def _neutralize_charges (atom_section , tolerance ):
89- """Attempts to neutralize the charges by adjusting within the given tolerance."""
89+ """Adjusts charges to neutralize the system within the specified tolerance."""
9090 charges = np .array ([float (atom [2 ]) for atom in atom_section ])
9191 total_charge = np .sum (charges )
92-
9392 if abs (total_charge ) < 1e-6 :
9493 return charges # Already neutral
95-
9694 adjustment_needed = - total_charge
97-
98- # Try adjusting each charge within the tolerance to achieve neutrality
99- for i , charge in enumerate (charges ):
100- possible_adjustment = charge * tolerance
101- if abs (possible_adjustment ) >= abs (adjustment_needed ):
102- new_charges = charges .copy ()
103- new_charges [i ] += adjustment_needed
104- if abs (np .sum (new_charges )) < 1e-6 :
105- return new_charges
106-
107- return None # Unable to neutralize within the given tolerance
95+ # Calculate the maximum allowed adjustment for each atom
96+ max_adjustments = np .abs (charges ) * tolerance
97+ # Try to distribute the adjustment proportionally
98+ adjustment_factors = max_adjustments / np .sum (max_adjustments )
99+ adjustments = adjustment_needed * adjustment_factors
100+ new_charges = charges + adjustments
101+ if abs (np .sum (new_charges )) < 1e-6 :
102+ return new_charges
103+ else :
104+ return None # Unable to neutralize within the given tolerance
108105
109106def _write_modified_rtp_file (file , lines , atom_section ):
110107 """Writes the modified content back to the .rtp file."""
0 commit comments