energy_adapter.py 1.87 KB
Newer Older
Martin Reinecke's avatar
Martin Reinecke committed
1
2
3
4
5
from __future__ import absolute_import, division, print_function

from ..compat import *
from ..minimization.energy import Energy
from ..linearization import Linearization
Martin Reinecke's avatar
Martin Reinecke committed
6
import numpy as np
Martin Reinecke's avatar
Martin Reinecke committed
7
8
9


class EnergyAdapter(Energy):
Martin Reinecke's avatar
Martin Reinecke committed
10
    def __init__(self, position, op, controller=None, preconditioner=None):
Martin Reinecke's avatar
Martin Reinecke committed
11
12
        super(EnergyAdapter, self).__init__(position)
        self._op = op
Martin Reinecke's avatar
Martin Reinecke committed
13
        self._val = self._grad = self._metric = None
Martin Reinecke's avatar
Martin Reinecke committed
14
15
        self._controller = controller
        self._preconditioner = preconditioner
Martin Reinecke's avatar
Martin Reinecke committed
16
17

    def at(self, position):
Martin Reinecke's avatar
Martin Reinecke committed
18
19
        return EnergyAdapter(position, self._op, self._controller,
                             self._preconditioner)
Martin Reinecke's avatar
Martin Reinecke committed
20

Martin Reinecke's avatar
Martin Reinecke committed
21
22
    def _fill_all(self):
        tmp = self._op(Linearization.make_var(self._position))
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
23
        self._val = tmp.val.local_data[()]
Martin Reinecke's avatar
Martin Reinecke committed
24
        self._grad = tmp.gradient
Martin Reinecke's avatar
Martin Reinecke committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
        if self._controller is not None:
            from ..operators.linear_operator import LinearOperator
            from ..operators.inversion_enabler import InversionEnabler

            if self._preconditioner is None:
                precond = None
            elif isinstance(self._preconditioner, LinearOperator):
                precond = self._preconditioner
            elif isinstance(self._preconditioner, Energy):
                precond = self._preconditioner.at(self._position).metric
            self._metric = InversionEnabler(tmp._metric, self._controller,
                                            precond)
        else:
            self._metric = tmp._metric
Martin Reinecke's avatar
Martin Reinecke committed
39

Martin Reinecke's avatar
Martin Reinecke committed
40
41
    @property
    def value(self):
Martin Reinecke's avatar
Martin Reinecke committed
42
        if self._val is None:
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
43
            self._val = self._op(self._position).local_data[()]
Martin Reinecke's avatar
Martin Reinecke committed
44
        return self._val
Martin Reinecke's avatar
Martin Reinecke committed
45
46
47

    @property
    def gradient(self):
Martin Reinecke's avatar
Martin Reinecke committed
48
49
50
        if self._grad is None:
            self._fill_all()
        return self._grad
Martin Reinecke's avatar
Martin Reinecke committed
51
52
53

    @property
    def metric(self):
Martin Reinecke's avatar
Martin Reinecke committed
54
55
56
        if self._metric is None:
            self._fill_all()
        return self._metric