Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

abacus: fix bug in collecting force #669

Merged
merged 4 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions dpdata/abacus/scf.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def collect_force(outlines):
def get_force(outlines, natoms):
force = collect_force(outlines)
if len(force) == 0:
return [[]]
return None
Copy link

Choose a reason for hiding this comment

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

Tip

Codebase Verification

The change in get_force to return None instead of an empty list is not handled correctly in the codebase. Specifically, in dpdata/abacus/scf.py, the return value of get_force is used without checking for None, which could lead to errors.

  • dpdata/abacus/scf.py: The get_force function's return value is used directly without checking for None.

Please update the code to handle the None return value appropriately.

Analysis chain

Verify the impact of returning None instead of an empty list on other parts of the code.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that all usages of `get_force` handle the new `None` return value correctly.

# Test: Search for the function usage. Expect: Proper handling of `None`.
rg --type python $'get_force'

Length of output: 59



Script:

#!/bin/bash
# Description: Verify that all usages of `get_force` handle the new `None` return value correctly.

# Test: Search for the function usage. Expect: Proper handling of `None`.
rg 'get_force'

Length of output: 291



Script:

#!/bin/bash
# Description: Extract lines around the usage of `get_force` to verify handling of `None` return value.

# Extract lines around the usage in dpdata/abacus/scf.py
rg -A 5 -B 5 'get_force' dpdata/abacus/scf.py

# Extract lines around the usage in dpdata/qe/scf.py
rg -A 5 -B 5 'get_force' dpdata/qe/scf.py

# Extract lines around the usage in dpdata/plugins/ase.py
rg -A 5 -B 5 'get_force' dpdata/plugins/ase.py

Length of output: 1938

else:
return np.array(force[-1]) # only return the last force

Expand Down Expand Up @@ -285,7 +285,7 @@ def get_frame(fname):
data["cells"] = cell[np.newaxis, :, :]
data["coords"] = coords[np.newaxis, :, :]
data["energies"] = np.array(energy)[np.newaxis]
data["forces"] = force[np.newaxis, :, :]
data["forces"] = np.empty((0,)) if force is None else force[np.newaxis, :, :]
if stress is not None:
data["virials"] = stress[np.newaxis, :, :]
data["orig"] = np.zeros(3)
Expand Down
17 changes: 17 additions & 0 deletions tests/abacus.scf/INPUT.ch4-noforcestress
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
INPUT_PARAMETERS
#Parameters (General)
suffix ch4-noForceStress
stru_file STRU.ch4 #the filename of file containing atom positions
kpoint_file KPT.ch4 #the name of file containing k points
pseudo_dir ./
nbands 8
#Parameters (Accuracy)
ecutwfc 100
symmetry 1
scf_nmax 50
smearing_method gauss #type of smearing: gauss; fd; fixed; mp; mp2; mv
smearing_sigma 0.01
mixing_type plain
mixing_beta 0.5
cal_force 1
cal_stress 1
Loading