energy_adapter.py 2.44 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
from ..multi_field import MultiField
7
8
from ..operators.scaling_operator import ScalingOperator
from ..operators.block_diagonal_operator import BlockDiagonalOperator
Martin Reinecke's avatar
Martin Reinecke committed
9
import numpy as np
Martin Reinecke's avatar
Martin Reinecke committed
10
11
12


class EnergyAdapter(Energy):
Martin Reinecke's avatar
Martin Reinecke committed
13
14
    def __init__(self, position, op, controller=None, preconditioner=None,
                 constants=[]):
Martin Reinecke's avatar
Martin Reinecke committed
15
16
        super(EnergyAdapter, self).__init__(position)
        self._op = op
Martin Reinecke's avatar
Martin Reinecke committed
17
        self._val = self._grad = self._metric = None
Martin Reinecke's avatar
Martin Reinecke committed
18
19
        self._controller = controller
        self._preconditioner = preconditioner
Martin Reinecke's avatar
Martin Reinecke committed
20
        self._constants = constants
Martin Reinecke's avatar
Martin Reinecke committed
21
22

    def at(self, position):
Martin Reinecke's avatar
Martin Reinecke committed
23
        return EnergyAdapter(position, self._op, self._controller,
Martin Reinecke's avatar
Martin Reinecke committed
24
                             self._preconditioner, self._constants)
Martin Reinecke's avatar
Martin Reinecke committed
25

Martin Reinecke's avatar
Martin Reinecke committed
26
    def _fill_all(self):
Martin Reinecke's avatar
Martin Reinecke committed
27
28
29
        if len(self._constants) == 0:
            tmp = self._op(Linearization.make_var(self._position))
        else:
30
31
32
33
            ops = [ScalingOperator(0. if key in self._constants else 1., dom)
                   for key, dom in self._position.domain.items()]
            bdop = BlockDiagonalOperator(self._position.domain, tuple(ops))
            tmp = self._op(Linearization(self._position, bdop))
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
34
        self._val = tmp.val.local_data[()]
Martin Reinecke's avatar
Martin Reinecke committed
35
        self._grad = tmp.gradient
Martin Reinecke's avatar
Martin Reinecke committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
        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
50

Martin Reinecke's avatar
Martin Reinecke committed
51
52
    @property
    def value(self):
Martin Reinecke's avatar
Martin Reinecke committed
53
        if self._val is None:
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
54
            self._val = self._op(self._position).local_data[()]
Martin Reinecke's avatar
Martin Reinecke committed
55
        return self._val
Martin Reinecke's avatar
Martin Reinecke committed
56
57
58

    @property
    def gradient(self):
Martin Reinecke's avatar
Martin Reinecke committed
59
60
61
        if self._grad is None:
            self._fill_all()
        return self._grad
Martin Reinecke's avatar
Martin Reinecke committed
62
63
64

    @property
    def metric(self):
Martin Reinecke's avatar
Martin Reinecke committed
65
66
67
        if self._metric is None:
            self._fill_all()
        return self._metric