Skip to content

Commit

Permalink
add tests and fix bugs they exposed
Browse files Browse the repository at this point in the history
  • Loading branch information
mbercx committed Mar 9, 2022
1 parent b85a461 commit 5304b03
Show file tree
Hide file tree
Showing 9 changed files with 2,900 additions and 17 deletions.
28 changes: 15 additions & 13 deletions aiida_quantumespresso/calculations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,16 +505,15 @@ def _generate_PWCPinputdata(cls, parameters, settings, pseudos, structure, kpoin
# ------------ ATOMIC_POSITIONS -----------
coordinates = [site.position for site in structure.sites]
if use_fractional:
atomic_positions_card_list = ['ATOMIC_POSITIONS crystal']
atomic_positions_card_header = 'ATOMIC_POSITIONS crystal\n'
coordinates = numpy.dot(coordinates, numpy.linalg.inv(numpy.array(structure.cell))).tolist()
else:
atomic_positions_card_list = ['ATOMIC_POSITIONS angstrom']
atomic_positions_card_header = 'ATOMIC_POSITIONS angstrom\n'

for site, site_coords in zip(structure.sites, coordinates):
atomic_positions_card_list.append(
'{0} {1:18.10f} {2:18.10f} {3:18.10f}'. # pylint: disable=consider-using-f-string
format(site.kind_name.ljust(6), *site_coords)
)
atomic_positions_card_list = [
'{0} {1:18.10f} {2:18.10f} {3:18.10f}'.format(site.kind_name.ljust(6), *site_coords) # pylint: disable=consider-using-f-string
for site, site_coords in zip(structure.sites, coordinates)
]

fixed_coords = settings.pop('FIXED_COORDS', None)

Expand All @@ -525,12 +524,15 @@ def _generate_PWCPinputdata(cls, parameters, settings, pseudos, structure, kpoin
raise exceptions.InputValidationError(
f'Input structure has {len(structure.sites)} sites, but fixed_coords has length {len(fixed_coords)}'
)
fixed_coords_strings = [' {:d} {:d} {:d}'.format(*row) for row in numpy.int32(fixed_coords).tolist()] # pylint: disable=consider-using-f-string

for atomic_pos_str, fixed_coords_str in zip(atomic_positions_card_list[1:], fixed_coords_strings):
atomic_pos_str += fixed_coords_str

atomic_positions_card = '\n'.join(atomic_positions_card_list) + '\n'
fixed_coords_strings = [
' {:d} {:d} {:d}'.format(*row) for row in numpy.int32(numpy.invert(fixed_coords)).tolist() # pylint: disable=consider-using-f-string
]
atomic_positions_card_list = [
atomic_pos_str + fixed_coords_str
for atomic_pos_str, fixed_coords_str in zip(atomic_positions_card_list, fixed_coords_strings)
]

atomic_positions_card = atomic_positions_card_header + '\n'.join(atomic_positions_card_list) + '\n'

# Optional ATOMIC_FORCES card
atomic_forces = settings.pop('ATOMIC_FORCES', None)
Expand Down
9 changes: 5 additions & 4 deletions aiida_quantumespresso/parsers/pw.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,13 +250,14 @@ def is_ionically_converged(self, trajectory, except_final_scf=False):
return verify_convergence_trajectory(
trajectory=trajectory,
index=-1,
threshold_forces=threshold_forces
threshold_forces=threshold_forces,
fixed_coords=fixed_coords
)

if relax_type == 'vc-relax':
values = [threshold_forces, threshold_stress, external_pressure]
converged_relax = verify_convergence_trajectory(trajectory, -2, *values, fixed_coords)
converged_final = verify_convergence_trajectory(trajectory, -1, *values, fixed_coords)
values = [threshold_forces, threshold_stress, external_pressure, fixed_coords]
converged_relax = verify_convergence_trajectory(trajectory, -2, *values)
converged_final = verify_convergence_trajectory(trajectory, -1, *values)

return converged_relax and (converged_final or except_final_scf)

Expand Down
32 changes: 32 additions & 0 deletions tests/calculations/test_pw.py
Original file line number Diff line number Diff line change
Expand Up @@ -320,3 +320,35 @@ def test_pw_validate_inputs_restart_nscf(
inputs['parameters'] = orm.Dict(dict=parameters)
with pytest.warns(Warning, match='`restart_mode` should be set to `from_scratch` for a `.*`.'):
generate_calc_job(fixture_sandbox, entry_point_name, inputs)


def test_fixed_coords(fixture_sandbox, generate_calc_job, generate_inputs_pw, file_regression):
"""Test a ``PwCalculation`` where the ``fixed_coords`` setting was provided."""
entry_point_name = 'quantumespresso.pw'

inputs = generate_inputs_pw()
inputs['settings'] = orm.Dict(dict={'FIXED_COORDS': [[True, True, False], [False, True, False]]})
generate_calc_job(fixture_sandbox, entry_point_name, inputs)

with fixture_sandbox.open('aiida.in') as handle:
input_written = handle.read()

file_regression.check(input_written, encoding='utf-8', extension='.in')


@pytest.mark.parametrize(['fixed_coords', 'error_message'], [
([[True, True], [False, True]], 'The `fixed_coords` setting must be a list of lists with length 3.'),
([[True, True, 1], [False, True, False]
], 'All elements in the `fixed_coords` setting lists must be either `True` or `False`.'),
([
[True, True, False],
], 'Input structure has 2 sites, but fixed_coords has length 1'),
])
def test_fixed_coords_validation(fixture_sandbox, generate_calc_job, generate_inputs_pw, fixed_coords, error_message):
"""Test the validation of the ``fixed_coords`` setting for the ``PwCalculation``."""
entry_point_name = 'quantumespresso.pw'

inputs = generate_inputs_pw()
inputs['settings'] = orm.Dict(dict={'FIXED_COORDS': fixed_coords})
with pytest.raises(ValueError, match=error_message):
generate_calc_job(fixture_sandbox, entry_point_name, inputs)
28 changes: 28 additions & 0 deletions tests/calculations/test_pw/test_fixed_coords.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
&CONTROL
calculation = 'scf'
outdir = './out/'
prefix = 'aiida'
pseudo_dir = './pseudo/'
verbosity = 'high'
/
&SYSTEM
ecutrho = 2.4000000000d+02
ecutwfc = 3.0000000000d+01
ibrav = 0
nat = 2
ntyp = 1
/
&ELECTRONS
electron_maxstep = 60
/
ATOMIC_SPECIES
Si 28.0855 Si.upf
ATOMIC_POSITIONS angstrom
Si 0.0000000000 0.0000000000 0.0000000000 0 0 1
Si 1.3575000000 1.3575000000 1.3575000000 1 0 1
K_POINTS automatic
2 2 2 0 0 0
CELL_PARAMETERS angstrom
2.7150000000 2.7150000000 0.0000000000
2.7150000000 0.0000000000 2.7150000000
0.0000000000 2.7150000000 2.7150000000
Loading

0 comments on commit 5304b03

Please sign in to comment.