domain_tuple_field_inserter.py 2.65 KB
Newer Older
Martin Reinecke's avatar
Martin Reinecke committed
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
from __future__ import absolute_import, division, print_function
20

Philipp Arras's avatar
Fixups  
Philipp Arras committed
21
import numpy as np
22
23

from ..compat import *
Philipp Arras's avatar
Fixups  
Philipp Arras committed
24
from ..domain_tuple import DomainTuple
25
from ..field import Field
Philipp Arras's avatar
Fixups  
Philipp Arras committed
26
27
28
from .linear_operator import LinearOperator


Philipp Arras's avatar
Philipp Arras committed
29
class DomainTupleFieldInserter(LinearOperator):
30
    def __init__(self, domain, new_space, ind, infront=False):
Philipp Arras's avatar
Philipp Arras committed
31
32
33
34
35
36
        '''Writes the content of a field into one slice of a DomainTuple.

        Parameters
        ----------
        domain : Domain, tuple of Domain or DomainTuple
        new_space : Domain, tuple of Domain or DomainTuple
37
38
39
40
41
        ind : Integer
            Index of the same space as new_space
        infront : Boolean
            If true, the new domain is added in the beginning of the
            DomainTuple. Otherwise it is added at the end.
Philipp Arras's avatar
Philipp Arras committed
42
43
44
        '''
        # FIXME Add assertions
        self._domain = DomainTuple.make(domain)
45
46
47
48
49
        if infront:
            self._target = DomainTuple.make([new_space] + list(self.domain))
        else:
            self._target = DomainTuple.make(list(self.domain) + [new_space])
        self._infront = infront
Philipp Arras's avatar
Philipp Arras committed
50
51
52
53
54
55
56
        self._capability = self.TIMES | self.ADJOINT_TIMES
        self._ind = ind

    def apply(self, x, mode):
        self._check_input(x, mode)
        if mode == self.TIMES:
            res = np.zeros(self.target.shape, dtype=x.dtype)
57
58
59
60
            if self._infront:
                res[self._ind] = x.to_global_data()
            else:
                res[..., self._ind] = x.to_global_data()
Philipp Arras's avatar
Philipp Arras committed
61
62
            return Field.from_global_data(self.target, res)
        else:
63
64
65
66
67
68
69
            if self._infront:
                return Field.from_global_data(self.domain,
                                              x.to_global_data()[self._ind])
            else:
                return Field.from_global_data(
                    self.domain,
                    x.to_global_data()[..., self._ind])