multi_field.py 4.99 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Copyright(C) 2013-2018 Max-Planck-Society
#
# NIFTy is being developed at the Max-Planck-Institut fuer Astrophysik
# and financially supported by the Studienstiftung des deutschen Volkes.

Martin Reinecke's avatar
Martin Reinecke committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from ..field import Field
import numpy as np
from .multi_domain import MultiDomain


class MultiField(object):
    def __init__(self, val):
        """
        Parameters
        ----------
        val : dict
        """
        self._val = val

    def __getitem__(self, key):
        return self._val[key]

    def keys(self):
        return self._val.keys()

    def items(self):
        return self._val.items()

    def values(self):
        return self._val.values()

    @property
    def domain(self):
Martin Reinecke's avatar
Martin Reinecke committed
47
48
        return MultiDomain.make(
            {key: val.domain for key, val in self._val.items()})
Martin Reinecke's avatar
Martin Reinecke committed
49

Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
50
51
52
53
    @property
    def dtype(self):
        return {key: val.dtype for key, val in self._val.items()}

54
55
    @staticmethod
    def from_random(random_type, domain, dtype=np.float64, **kwargs):
Martin Reinecke's avatar
fixes    
Martin Reinecke committed
56
        dtype = MultiField.build_dtype(dtype, domain)
57
58
        return MultiField({key: Field.from_random(random_type, domain[key],
                                                  dtype[key], **kwargs)
Martin Reinecke's avatar
fixes    
Martin Reinecke committed
59
                           for key in domain.keys()})
60

61
62
63
64
65
66
67
68
69
70
71
72
    def fill(self, fill_value):
        """Fill `self` uniformly with `fill_value`

        Parameters
        ----------
        fill_value: float or complex or int
            The value to fill the field with.
        """
        for val in self._val.values():
            val.fill(fill_value)
        return self

Martin Reinecke's avatar
Martin Reinecke committed
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
    def _check_domain(self, other):
        if other.domain != self.domain:
            raise ValueError("domains are incompatible.")

    def vdot(self, x):
        result = 0.
        self._check_domain(x)
        for key, sub_field in self.items():
            result += sub_field.vdot(x[key])
        return result

    def lock(self):
        for v in self.values():
            v.lock()
        return self

Martin Reinecke's avatar
Martin Reinecke committed
89
90
91
92
    @property
    def locked(self):
        return all(v.locked for v in self.values())

Martin Reinecke's avatar
Martin Reinecke committed
93
94
    def copy(self):
        return MultiField({key: val.copy() for key, val in self.items()})
95

Martin Reinecke's avatar
Martin Reinecke committed
96
97
98
99
100
101
    def locked_copy(self):
        if self.locked:
            return self
        return MultiField({key: val.locked_copy()
                          for key, val in self.items()})

102
103
    def empty_copy(self):
        return MultiField({key: val.empty_copy() for key, val in self.items()})
Martin Reinecke's avatar
Martin Reinecke committed
104

Martin Reinecke's avatar
Martin Reinecke committed
105
106
107
108
109
110
111
112
    @staticmethod
    def build_dtype(dtype, domain):
        if isinstance(dtype, dict):
            return dtype
        if dtype is None:
            dtype = np.float64
        return {key: dtype for key in domain.keys()}

Martin Reinecke's avatar
Martin Reinecke committed
113
    @staticmethod
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
114
    def empty(domain, dtype=None):
Martin Reinecke's avatar
tweaks    
Martin Reinecke committed
115
        dtype = MultiField.build_dtype(dtype, domain)
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
116
        return MultiField({key: Field.empty(dom, dtype=dtype[key])
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
117
                           for key, dom in domain.items()})
Martin Reinecke's avatar
Martin Reinecke committed
118
119

    @staticmethod
Martin Reinecke's avatar
step 1    
Martin Reinecke committed
120
121
    def full(domain, val):
        return MultiField({key: Field.full(dom, val)
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
122
                           for key, dom in domain.items()})
Martin Reinecke's avatar
Martin Reinecke committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137

    def norm(self):
        """ Computes the L2-norm of the field values.

        Returns
        -------
        norm : float
            The L2-norm of the field values.
        """
        return np.sqrt(np.abs(self.vdot(x=self)))

    def __neg__(self):
        return MultiField({key: -val for key, val in self.items()})

    def conjugate(self):
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
138
139
        return MultiField({key: sub_field.conjugate()
                           for key, sub_field in self.items()})
Martin Reinecke's avatar
Martin Reinecke committed
140
141
142
143
144
145
146
147
148
149
150
151


for op in ["__add__", "__radd__", "__iadd__",
           "__sub__", "__rsub__", "__isub__",
           "__mul__", "__rmul__", "__imul__",
           "__div__", "__rdiv__", "__idiv__",
           "__truediv__", "__rtruediv__", "__itruediv__",
           "__floordiv__", "__rfloordiv__", "__ifloordiv__",
           "__pow__", "__rpow__", "__ipow__",
           "__lt__", "__le__", "__gt__", "__ge__", "__eq__", "__ne__"]:
    def func(op):
        def func2(self, other):
Martin Reinecke's avatar
cleanup    
Martin Reinecke committed
152
153
154
155
156
157
158
159
            if isinstance(other, MultiField):
                self._check_domain(other)
                result_val = {key: getattr(sub_field, op)(other[key])
                              for key, sub_field in self.items()}
            else:
                result_val = {key: getattr(val, op)(other)
                              for key, val in self.items()}
            return MultiField(result_val)
Martin Reinecke's avatar
Martin Reinecke committed
160
161
        return func2
    setattr(MultiField, op, func(op))