Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
ift
NIFTy
Commits
9584a133
Commit
9584a133
authored
Sep 06, 2019
by
Philipp Arras
Committed by
Lukas Platz
Sep 06, 2019
Browse files
add AbsDeltaEnergyController
parent
5ded9f44
Pipeline
#60211
passed with stages
in 8 minutes and 25 seconds
Changes
2
Pipelines
1
Show whitespace changes
Inline
Side-by-side
nifty5/__init__.py
View file @
9584a133
...
...
@@ -59,7 +59,7 @@ from .probing import probe_with_posterior_samples, probe_diagonal, \
from
.minimization.line_search
import
LineSearch
from
.minimization.iteration_controllers
import
(
IterationController
,
GradientNormController
,
DeltaEnergyController
,
GradInfNormController
)
GradInfNormController
,
AbsDeltaEnergyController
)
from
.minimization.minimizer
import
Minimizer
from
.minimization.conjugate_gradient
import
ConjugateGradient
from
.minimization.nonlinear_cg
import
NonlinearCG
...
...
nifty5/minimization/iteration_controllers.py
View file @
9584a133
...
...
@@ -18,6 +18,7 @@
from
..logger
import
logger
from
..utilities
import
NiftyMeta
import
numpy
as
np
from
time
import
time
class
IterationController
(
metaclass
=
NiftyMeta
):
...
...
@@ -266,3 +267,75 @@ class DeltaEnergyController(IterationController):
return
self
.
CONVERGED
return
self
.
CONTINUE
class
AbsDeltaEnergyController
(
IterationController
):
"""An iteration controller checking (mainly) the energy change from one
iteration to the next.
Parameters
----------
deltaE : float
If the difference between the last and current energies is below this
value, the convergence counter will be increased in this iteration.
convergence_level : int, default=1
The number which the convergence counter must reach before the
iteration is considered to be converged
iteration_limit : int, optional
The maximum number of iterations that will be carried out.
name : str, optional
if supplied, this string and some diagnostic information will be
printed after every iteration
"""
def
__init__
(
self
,
deltaE
,
convergence_level
=
1
,
iteration_limit
=
None
,
name
=
None
,
file_name
=
None
):
self
.
_deltaE
=
deltaE
self
.
_convergence_level
=
convergence_level
self
.
_iteration_limit
=
iteration_limit
self
.
_name
=
name
self
.
_file_name
=
file_name
def
start
(
self
,
energy
):
self
.
_itcount
=
-
1
self
.
_ccount
=
0
self
.
_Eold
=
0.
return
self
.
check
(
energy
)
def
check
(
self
,
energy
):
self
.
_itcount
+=
1
inclvl
=
False
Eval
=
energy
.
value
diff
=
abs
(
self
.
_Eold
-
Eval
)
if
self
.
_itcount
>
0
:
if
diff
<
self
.
_deltaE
:
inclvl
=
True
self
.
_Eold
=
Eval
if
inclvl
:
self
.
_ccount
+=
1
else
:
self
.
_ccount
=
max
(
0
,
self
.
_ccount
-
1
)
# report
if
self
.
_name
is
not
None
:
logger
.
info
(
"{}: Iteration #{} energy={:.6E} diff={:.6E} crit={:.6E}"
.
format
(
self
.
_name
,
self
.
_itcount
,
Eval
,
diff
,
self
.
_deltaE
))
# Are we done?
if
self
.
_iteration_limit
is
not
None
:
if
self
.
_itcount
>=
self
.
_iteration_limit
:
logger
.
warning
(
"{} Iteration limit reached. Assuming convergence"
.
format
(
""
if
self
.
_name
is
None
else
self
.
_name
+
": "
))
return
self
.
CONVERGED
if
self
.
_ccount
>=
self
.
_convergence_level
:
return
self
.
CONVERGED
# Write energy to file
if
self
.
_file_name
is
not
None
:
with
open
(
self
.
_file_name
,
'a+'
)
as
f
:
f
.
write
(
'{} {} {}
\n
'
.
format
(
time
(),
energy
.
value
,
diff
))
return
self
.
CONTINUE
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment