Skip to content

Commit da488ad

Browse files
committed
A safer error messages when an error occurs in the custom function gradient
1 parent 124cc2c commit da488ad

3 files changed

Lines changed: 28 additions & 66 deletions

File tree

Modules/SchaMinimizer.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
It is possible to use it to perform the anharmonic minimization
2525
"""
2626

27+
from inspect import signature
28+
2729
#import Ensemble
2830
import numpy as np
2931
import difflib
@@ -451,9 +453,34 @@ def minimization_step(self, custom_function_gradient = None):
451453

452454
# Perform the gradient restriction
453455
if custom_function_gradient is not None:
456+
457+
# Check the number of parameters
454458
try:
459+
sig = signature(custom_function_gradient).parameters
460+
except Exception as e:
461+
print(e)
462+
463+
MSG = '''
464+
While inspecting the custom_function_gradient an error was rised.
465+
Maybe you did not pass the minimizer a valid function?
466+
'''
467+
raise ValueError(MSG)
468+
469+
470+
if len(sig) not in [2,3]:
471+
MSG = '''
472+
Error, the custom_function_gradient must have either 2 or 3 arguments:
473+
- dynamical_matrix_gradient
474+
- structure gradient
475+
- [Optional] The minimizer (self) object
476+
477+
The function you provided accepts {} arguments instead.
478+
'''.format(len(sig))
479+
raise ValueError(MSG)
480+
481+
if len(sig) == 3:
455482
custom_function_gradient(dyn_grad, struct_grad, self)
456-
except:
483+
else:
457484
custom_function_gradient(dyn_grad, struct_grad)
458485

459486

File renamed without changes.

tests/test_gradient_evolution/test_relax_just_grad.py

Lines changed: 0 additions & 65 deletions
This file was deleted.

0 commit comments

Comments
 (0)