Commit 2f658b1b authored by Philipp Arras's avatar Philipp Arras
Browse files

Add logging to NewtonCG

parent 93f596a5
Pipeline #75203 passed with stages
in 8 minutes and 24 seconds
......@@ -166,7 +166,8 @@ class NewtonCG(DescentMinimizer):
"""
def __init__(self, controller, napprox=0, line_searcher=None, name=None,
nreset=20, max_cg_iterations=200, energy_reduction_factor=0.1):
nreset=20, max_cg_iterations=200, energy_reduction_factor=0.1,
activate_logging=False):
if line_searcher is None:
line_searcher = LineSearch(preferred_initial_step_size=1.)
super(NewtonCG, self).__init__(controller=controller,
......@@ -176,6 +177,7 @@ class NewtonCG(DescentMinimizer):
self._nreset = nreset
self._max_cg_iterations = max_cg_iterations
self._alpha = energy_reduction_factor
self._history = [] if activate_logging else None
def get_descent_direction(self, energy, old_value=None):
if old_value is None:
......@@ -184,14 +186,25 @@ class NewtonCG(DescentMinimizer):
ediff = self._alpha*(old_value-energy.value)
ic = AbsDeltaEnergyController(
ediff, iteration_limit=self._max_cg_iterations, name=self._name)
if self._history is not None:
ic.activate_logging()
e = QuadraticEnergy(0*energy.position, energy.metric, energy.gradient)
p = None
if self._napprox > 1:
met = energy.metric
p = makeOp(approximation2endo(met, self._napprox)).inverse
e, conv = ConjugateGradient(ic, nreset=self._nreset)(e, p)
if self._history is not None:
self._history.extend(ic.pop_history())
return -e.position
def pop_inversion_energy_history(self):
if self._history is None:
raise RuntimeError('Logging has not been enabled.')
hist = self._history
self._history = []
return hist
class L_BFGS(DescentMinimizer):
def __init__(self, controller, line_searcher=LineSearch(),
......
......@@ -98,6 +98,12 @@ class IterationController(metaclass=NiftyMeta):
before, it is reset."""
self._history = []
def activate_logging(self):
"""Activates the logging functionality. If the log has been populated
before, it stays as it is."""
if self._history is None:
self._history = []
def append_history(func):
@functools.wraps(func)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment