Beispiel #1
0
    def __init__(self, products=None, estimator=None, visualizer=None,
                 name=None, **kwargs):

        products = FrozenDict(products or {})

        if products:
            for k, v in products.items():
                setattr(self, f'{k}_product', v)
                setattr(self, f'{k}_norm', induced_norm(v))

        self.__auto_init(locals())
Beispiel #2
0
class DiscretizationBase(DiscretizationInterface):
    """Base class for |Discretizations| providing some common functionality."""

    sid_ignore = DiscretizationInterface.sid_ignore | {'visualizer'}
    add_with_arguments = DiscretizationInterface.add_with_arguments | {'operators'}
    special_operators = frozenset()

    def __init__(self, operators=None, products=None, estimator=None, visualizer=None,
                 cache_region=None, name=None, **kwargs):

        operators = {} if operators is None else dict(operators)

        # handle special operators
        for on in self.special_operators:
            # special operators must be provided as keyword argument to __init__
            assert on in kwargs
            # special operators may not already exist as attributes
            assert not hasattr(self, on)
            # special operators may not be contained in operators dict
            assert on not in operators

            op = kwargs[on]
            # operators either have to be None or an Operator
            assert op is None \
                or isinstance(op, OperatorInterface) \
                or all(isinstance(o, OperatorInterface) for o in op)
            # set special operator as an attribute
            setattr(self, on, op)
            # add special operator to the operators dict
            operators[on] = op

        self.operators = FrozenDict(operators)
        self.linear = all(op is None or op.linear for op in operators.values())
        self.products = FrozenDict(products or {})
        self.estimator = estimator
        self.visualizer = visualizer
        self.enable_caching(cache_region)
        self.name = name

        if products:
            for k, v in products.items():
                setattr(self, '{}_product'.format(k), v)
                setattr(self, '{}_norm'.format(k), induced_norm(v))

        self.build_parameter_type(*operators.values())

    def with_(self, **kwargs):
        assert set(kwargs.keys()) <= self.with_arguments

        if 'operators' in kwargs:
            # extract special operators from provided operators dict
            operators = kwargs['operators'].copy()
            for on in self.special_operators:
                if on in operators:
                    assert on not in kwargs or kwargs[on] == operators[on]
                    kwargs[on] = operators.pop(on)
            kwargs['operators'] = operators
        else:
            # when an operators dict is not specified make sure that we use the old operators dict
            # but without the special operators
            kwargs['operators'] = {on: op for on, op in self.operators.items()
                                   if on not in self.special_operators}

        # delete empty 'operators' dicts for cases where __init__ does not take
        # an 'operator' argument
        if 'operators' not in self._init_arguments:
            operators = kwargs.pop('operators')
            # in that case, there should not be any operators left in 'operators'
            assert not operators

        return super(DiscretizationBase, self).with_(**kwargs)

    def visualize(self, U, **kwargs):
        """Visualize a solution |VectorArray| U.

        Parameters
        ----------
        U
            The |VectorArray| from
            :attr:`~pymor.discretizations.interfaces.DiscretizationInterface.solution_space`
            that shall be visualized.
        kwargs
            See docstring of `self.visualizer.visualize`.
        """
        if self.visualizer is not None:
            self.visualizer.visualize(U, self, **kwargs)
        else:
            raise NotImplementedError('Discretization has no visualizer.')

    def estimate(self, U, mu=None):
        if self.estimator is not None:
            return self.estimator.estimate(U, mu=mu, d=self)
        else:
            raise NotImplementedError('Discretization has no estimator.')
Beispiel #3
0
class DiscretizationBase(DiscretizationInterface):
    """Base class for |Discretizations| providing some common functionality."""

    sid_ignore = DiscretizationInterface.sid_ignore | {'visualizer'}
    add_with_arguments = DiscretizationInterface.add_with_arguments | {'operators'}
    special_operators = frozenset()

    def __init__(self, operators=None, products=None, estimator=None, visualizer=None,
                 cache_region=None, name=None, **kwargs):

        operators = {} if operators is None else dict(operators)

        # handle special operators
        for on in self.special_operators:
            # special operators must be provided as keyword argument to __init__
            assert on in kwargs
            # special operators may not already exist as attributes
            assert not hasattr(self, on)
            # special operators may not be contained in operators dict
            assert on not in operators

            op = kwargs[on]
            # operators either have to be None or an Operator
            assert op is None \
                or isinstance(op, OperatorInterface) \
                or all(isinstance(o, OperatorInterface) for o in op)
            # set special operator as an attribute
            setattr(self, on, op)
            # add special operator to the operators dict
            operators[on] = op

        self.operators = FrozenDict(operators)
        self.linear = all(op is None or op.linear for op in operators.values())
        self.products = FrozenDict(products or {})
        self.estimator = estimator
        self.visualizer = visualizer
        self.enable_caching(cache_region)
        self.name = name

        if products:
            for k, v in products.items():
                setattr(self, '{}_product'.format(k), v)
                setattr(self, '{}_norm'.format(k), induced_norm(v))

        self.build_parameter_type(*operators.values())

    def with_(self, **kwargs):
        assert set(kwargs.keys()) <= self.with_arguments

        if 'operators' in kwargs:
            # extract special operators from provided operators dict
            operators = kwargs['operators'].copy()
            for on in self.special_operators:
                if on in operators:
                    assert on not in kwargs or kwargs[on] == operators[on]
                    kwargs[on] = operators.pop(on)
            kwargs['operators'] = operators
        else:
            # when an operators dict is not specified make sure that we use the old operators dict
            # but without the special operators
            kwargs['operators'] = {on: op for on, op in self.operators.items()
                                   if on not in self.special_operators}

        # delete empty 'operators' dicts for cases where __init__ does not take
        # an 'operator' argument
        if 'operators' not in self._init_arguments:
            operators = kwargs.pop('operators')
            # in that case, there should not be any operators left in 'operators'
            assert not operators

        return super(DiscretizationBase, self).with_(**kwargs)

    def visualize(self, U, **kwargs):
        """Visualize a solution |VectorArray| U.

        Parameters
        ----------
        U
            The |VectorArray| from
            :attr:`~pymor.discretizations.interfaces.DiscretizationInterface.solution_space`
            that shall be visualized.
        kwargs
            See docstring of `self.visualizer.visualize`.
        """
        if self.visualizer is not None:
            self.visualizer.visualize(U, self, **kwargs)
        else:
            raise NotImplementedError('Discretization has no visualizer.')

    def estimate(self, U, mu=None):
        if self.estimator is not None:
            return self.estimator.estimate(U, mu=mu, discretization=self)
        else:
            raise NotImplementedError('Discretization has no estimator.')