Skip to content

Commit 0ef2858

Browse files
authored
Merge pull request #365 from jungsdao/main
optimizer as an argument for optimize gracefully handle optimizers that do and don't accept `smax`
2 parents ba5b9b5 + 48cbcad commit 0ef2858

2 files changed

Lines changed: 19 additions & 10 deletions

File tree

.github/workflows/pytests.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,9 @@ jobs:
209209
- name: Install buildcell
210210
run: |
211211
sudo apt-get install gfortran
212-
wget https://www.mtg.msm.cam.ac.uk/system/files/documents/airss-0.9.1.tgz
213-
tar xzf airss-0.9.1.tgz
214-
cd airss-0.9.1
212+
wget https://www.mtg.msm.cam.ac.uk/files/airss-v0.9.4.tgz
213+
tar xzf airss-v0.9.4.tgz
214+
cd airss
215215
make buildcell
216216
mkdir -p $HOME/bin
217217
cp src/buildcell/src/buildcell $HOME/bin/

wfl/generate/optimize.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def _new_log(self, forces=None):
3434
def _run_autopara_wrappable(atoms, calculator, fmax=1.0e-3, smax=None, steps=1000, pressure=None,
3535
stress_mask=None, keep_symmetry=True, traj_step_interval=1, traj_subselect=None,
3636
skip_failures=True, results_prefix='last_op__optimize_', verbose=False, update_config_type="append",
37-
rng=None, _autopara_per_item_info=None,
37+
optimizer=PreconLBFGS, rng=None, _autopara_per_item_info=None,
3838
**opt_kwargs):
3939
"""runs a structure optimization. By default calculator properties will be stored in keys
4040
prefixed with "last_op__optimize_", which may be overwritten by next operation.
@@ -73,8 +73,10 @@ def _run_autopara_wrappable(atoms, calculator, fmax=1.0e-3, smax=None, steps=100
7373
optimisation logs are not printed unless this is True
7474
update_config_type: ["append" | "overwrite" | False], default "append"
7575
whether/how to add at.info['optimize_config_type'] to at.info['config_type']
76+
optimizer : ASE optimizer, default PreconLBFGS
77+
optimizer to use.
7678
opt_kwargs
77-
keyword arguments for PreconLBFGS
79+
keyword arguments for optimizer
7880
rng: numpy.random.Generator, default None
7981
random number generator to use (needed for pressure sampling, initial temperature, or Langevin dynamics)
8082
_autopara_per_item_info: dict
@@ -93,9 +95,6 @@ def _run_autopara_wrappable(atoms, calculator, fmax=1.0e-3, smax=None, steps=100
9395

9496
calculator = construct_calculator_picklesafe(calculator)
9597

96-
if smax is None:
97-
smax = fmax
98-
9998
if keep_symmetry:
10099
# noinspection PyUnresolvedReferences,PyUnresolvedReferences
101100
try:
@@ -134,7 +133,7 @@ def _run_autopara_wrappable(atoms, calculator, fmax=1.0e-3, smax=None, steps=100
134133
else:
135134
wrapped_at = at
136135

137-
opt = PreconLBFGS(wrapped_at, **opt_kwargs_to_use)
136+
opt = optimizer(wrapped_at, **opt_kwargs_to_use)
138137

139138
# default status, will be overwritten for first and last configs in traj
140139
at.info['optimize_config_type'] = 'optimize_mid'
@@ -162,7 +161,17 @@ def process_step():
162161
converged = False
163162

164163
try:
165-
converged = opt.run(fmax=fmax, smax=smax, steps=steps)
164+
try:
165+
converged = opt.run(fmax=fmax, smax=smax if smax is not None else fmax, steps=steps)
166+
except TypeError as exc:
167+
if "unexpected keyword argument" in str(exc):
168+
# opt.run doesn't accept smax
169+
if smax is not None:
170+
# passed in explicitly, fail
171+
raise
172+
converged = opt.run(fmax=fmax, steps=steps)
173+
else: # some other error
174+
raise
166175
except Exception as exc:
167176
# label actual failed optimizations
168177
# when this happens, the atomic config somehow ends up with a 6-vector stress, which can't be

0 commit comments

Comments
 (0)