If it fails, it throws an error (not really catch-able) or prints to the screen "Optimizer: Optimization failed!". Of failed optimizations, the former occurs much more often, I'd expect. I can't think right off how to even trigger the latter. If you'd like, you can set a PSIvariable to the optimization iteration (or to the negative for failure) as below, and test on that. The value can be got through
get_variable('OPTIMIZATION CYCLE') (note that it'll be a double, not an int). If that proves useful, can absorb into codebase.
@@ -1182,6 +1182,7 @@ def optimize(name, **kwargs):
if psi4.get_option('OPTKING', 'OPT_TYPE') == 'IRC':
thisenergy = old_thisenergy
print('Optimizer: Optimization complete!')
+ psi4.set_variable('OPTIMIZATION CYCLE', n)
psi4.print_out('\n Final optimized geometry and variables:\n')
# Check if user wants to see the intcos; if so, don't delete them.
@@ -1212,6 +1213,7 @@ def optimize(name, **kwargs):
elif optking_rval == psi4.PsiReturnType.Failure:
print('Optimizer: Optimization failed!')
+ psi4.set_variable('OPTIMIZATION CYCLE', -1 * n)
if (psi4.get_option('OPTKING', 'KEEP_INTCOS') == False):