Exemplo n.º 1
0
 def __init__(self, array, transposed=False, space_id=None, name=None):
     self._array = array.copy()
     if transposed:
         self.source = array.space
         self.range = NumpyVectorSpace(len(array), space_id)
     else:
         self.source = NumpyVectorSpace(len(array), space_id)
         self.range = array.space
     self.transposed = transposed
     self.space_id = space_id
     self.name = name
Exemplo n.º 2
0
 def __init__(self, matrix, source_id=None, range_id=None, solver_options=None, name=None):
     assert matrix.ndim <= 2
     if matrix.ndim == 1:
         matrix = np.reshape(matrix, (1, -1))
     self.source = NumpyVectorSpace(matrix.shape[1], source_id)
     self.range = NumpyVectorSpace(matrix.shape[0], range_id)
     self.solver_options = solver_options
     self.name = name
     self.matrix = matrix
     self.source_id = source_id
     self.range_id = range_id
     self.sparse = issparse(matrix)
Exemplo n.º 3
0
 def __init__(self, mapping, transpose_mapping=None, dim_source=1, dim_range=1, linear=False, parameter_type=None,
              source_id=None, range_id=None, solver_options=None, name=None):
     self.source = NumpyVectorSpace(dim_source, source_id)
     self.range = NumpyVectorSpace(dim_range, range_id)
     self.solver_options = solver_options
     self.name = name
     self._mapping = mapping
     self._transpose_mapping = transpose_mapping
     self.linear = linear
     if parameter_type is not None:
         self.build_parameter_type(parameter_type)
     self.source_id = source_id  # needed for with_
     self.range_id = range_id
Exemplo n.º 4
0
 def action_ZeroOperator(self, op):
     range_basis, source_basis = self.range_basis, self.source_basis
     if source_basis is not None and range_basis is not None:
         from pymor.operators.numpy import NumpyMatrixOperator
         return NumpyMatrixOperator(np.zeros(
             (len(range_basis), len(source_basis))),
                                    name=op.name)
     else:
         new_source = NumpyVectorSpace(
             len(source_basis)) if source_basis is not None else op.source
         new_range = NumpyVectorSpace(
             len(range_basis)) if range_basis is not None else op.range
         return ZeroOperator(new_range, new_source, name=op.name)
Exemplo n.º 5
0
    def __init__(self, matrix, source_id=None, range_id=None, solver_options=None, name=None):
        assert matrix.ndim <= 2
        if matrix.ndim == 1:
            matrix = np.reshape(matrix, (1, -1))
        try:
            matrix.setflags(write=False)  # make numpy arrays read-only
        except AttributeError:
            pass

        self.__auto_init(locals())
        self.source = NumpyVectorSpace(matrix.shape[1], source_id)
        self.range = NumpyVectorSpace(matrix.shape[0], range_id)
        self.sparse = issparse(matrix)
Exemplo n.º 6
0
 def __init__(self,
              mapping,
              adjoint_mapping=None,
              dim_source=1,
              dim_range=1,
              linear=False,
              parameter_type=None,
              source_id=None,
              range_id=None,
              solver_options=None,
              name=None):
     self.__auto_init(locals())
     self.source = NumpyVectorSpace(dim_source, source_id)
     self.range = NumpyVectorSpace(dim_range, range_id)
Exemplo n.º 7
0
 def action_ZeroOperator(self, op, range_basis, source_basis, product=None):
     if source_basis is not None and range_basis is not None:
         from pymor.operators.numpy import NumpyMatrixOperator
         return NumpyMatrixOperator(np.zeros(
             (len(range_basis), len(source_basis))),
                                    source_id=op.source.id,
                                    range_id=op.range.id,
                                    name=op.name)
     else:
         new_source = (NumpyVectorSpace(len(source_basis), op.source.id)
                       if source_basis is not None else op.source)
         new_range = (NumpyVectorSpace(len(range_basis), op.range.id)
                      if range_basis is not None else op.range)
         return ZeroOperator(new_source, new_range, name=op.name)
Exemplo n.º 8
0
 def __init__(self, matrix, source_id=None, range_id=None, solver_options=None, name=None):
     super().__init__(matrix, source_id=source_id, range_id=range_id, solver_options=solver_options, name=name)
     functional = self.range_id is None
     vector = self.source_id is None
     if functional and vector:
         raise NotImplementedError
     if vector:
         self.source = NumpyVectorSpace(1, source_id)
     else:
         self.source = NumpyListVectorSpace(matrix.shape[1], source_id)
     if functional:
         self.range = NumpyVectorSpace(1, range_id)
     else:
         self.range = NumpyListVectorSpace(matrix.shape[0], range_id)
     self.functional = functional
     self.vector = vector
Exemplo n.º 9
0
Arquivo: cg.py Projeto: simon-ca/pymor
 def __init__(self,
              grid,
              boundary_info,
              advection_function=None,
              advection_constant=None,
              dirichlet_clear_columns=False,
              dirichlet_clear_diag=False,
              solver_options=None,
              name=None):
     assert grid.reference_element(0) in {square
                                          }, 'A square grid is expected!'
     assert advection_function is None \
         or (isinstance(advection_function, FunctionInterface) and
             advection_function.dim_domain == grid.dim_outer and
             advection_function.shape_range == (grid.dim_outer,))
     self.source = self.range = NumpyVectorSpace(grid.size(grid.dim))
     self.grid = grid
     self.boundary_info = boundary_info
     self.advection_constant = advection_constant
     self.advection_function = advection_function
     self.dirichlet_clear_columns = dirichlet_clear_columns
     self.dirichlet_clear_diag = dirichlet_clear_diag
     self.solver_options = solver_options
     self.name = name
     if advection_function is not None:
         self.build_parameter_type(inherits=(advection_function, ))
Exemplo n.º 10
0
Arquivo: cg.py Projeto: simon-ca/pymor
 def __init__(self, grid, function):
     assert function.dim_domain == grid.dim_outer
     assert function.shape_range == ()
     self.grid = grid
     self.function = function
     self.range = NumpyVectorSpace(grid.size(grid.dim))
     self.build_parameter_type(inherits=(function, ))
Exemplo n.º 11
0
class MonomOperator(Operator):
    source = range = NumpyVectorSpace(1)

    def __init__(self, order, monom=None):
        self.monom = monom if monom else Polynomial(
            np.identity(order + 1)[order])
        assert isinstance(self.monom, Polynomial)
        self.order = order
        self.derivative = self.monom.deriv()
        self.linear = order == 1

    def apply(self, U, mu=None):
        return self.source.make_array(self.monom(U.to_numpy()))

    def apply_adjoint(self, U, mu=None):
        return self.apply(U, mu=None)

    def jacobian(self, U, mu=None):
        assert len(U) == 1
        return NumpyMatrixOperator(
            self.derivative(U.to_numpy()).reshape((1, 1)))

    def apply_inverse(self,
                      V,
                      mu=None,
                      initial_guess=None,
                      least_squares=False):
        return self.range.make_array(1. / V.to_numpy())
Exemplo n.º 12
0
 def __init__(self,
              mapping,
              dim_source=1,
              dim_range=1,
              linear=False,
              parameter_type=None,
              solver_options=None,
              name=None):
     self.source = NumpyVectorSpace(dim_source)
     self.range = NumpyVectorSpace(dim_range)
     self.solver_options = solver_options
     self.name = name
     self._mapping = mapping
     self.linear = linear
     if parameter_type is not None:
         self.build_parameter_type(parameter_type, local_global=True)
Exemplo n.º 13
0
        def apply(self, U, mu=None):
            """
            Solves using step_hyperbolic() and refactoring result such that it is a dq quantity and
            time-step, dt is divided out.  Step hyperbolic takes the current solution state and
            updates it as such:
              q^(n+1) = q^n - dt/dx*apdq - dt/dx*amdq - dt/dx*(F_(i) - F_(i-1))/kappa
              where apdq, amdq are the positive and negative waves from the Riemann solver,
              and F is the 2nd order correction flux into and out of the finite volume cell
            See Leveque, R.J., Finite Volume Methods for Hyperbolic Problems,
                  Cambridge Texts in Applied Mathematics, 2002. for details, or check the
            Clawpack/PyClaw documentation at: http://www.clawpack.org/
            """
            assert U in self.source

            # Get current state and solve hyperbolic step with PyClaw
            q0 = np.copy(self.claw.solution.state.q,
                         order='F')  # Solution state before hyperbolic step
            print("t:", self.claw.solution.t, "dt:", self.claw.solver.dt)
            self.claw.solver.step_hyperbolic(
                self.claw.solution)  # Solve FV using PyClaw

            # Refactor result to be compatible with axpy() in time-stepping routine
            q = -(self.claw.solution.state.q - q0) / self.claw.solver.dt
            q = np.reshape(q, self.claw.solution.state.q.size, order='F')

            self.claw.solution.t += self.claw.solver.dt  # Update solution time

            # Using step()
            # self.claw.solver.step(self.claw.solution, take_one_step=True, tstart=0, tend=self.claw.solver.dt)

            # Return vector space with result
            return NumpyVectorSpace(
                self.claw.solution.state.q.size).make_array([q])
Exemplo n.º 14
0
def test_to_matrix():
    np.random.seed(0)
    A = np.random.randn(2, 2)
    B = np.random.randn(3, 3)
    C = np.random.randn(3, 3)

    X = np.bmat([[np.eye(2) + A, np.zeros((2, 3))],
                 [np.zeros((3, 2)), B.dot(C.T)]])

    C = sps.csc_matrix(C)

    Aop = NumpyMatrixOperator(A)
    Bop = NumpyMatrixOperator(B)
    Cop = NumpyMatrixOperator(C)

    Xop = BlockDiagonalOperator([
        LincombOperator([IdentityOperator(NumpyVectorSpace(2)), Aop], [1, 1]),
        Concatenation(Bop, AdjointOperator(Cop))
    ])

    assert np.allclose(X, to_matrix(Xop))
    assert np.allclose(X, to_matrix(Xop, format='csr').toarray())

    np.random.seed(0)
    V = np.random.randn(10, 2)
    Vva = NumpyVectorArray(V.T)
    Vop = VectorArrayOperator(Vva)
    assert np.allclose(V, to_matrix(Vop))
    Vop = VectorArrayOperator(Vva, transposed=True)
    assert np.allclose(V, to_matrix(Vop).T)
Exemplo n.º 15
0
Arquivo: cg.py Projeto: emamy/pymor
class InterpolationOperator(NumpyMatrixBasedOperator):
    """Vector-like Lagrange interpolation |Operator| for continuous finite element spaces.

    Parameters
    ----------
    grid
        The |Grid| on which to interpolate.
    function
        The |Function| to interpolate.
    """

    source = NumpyVectorSpace(1)
    linear = True

    def __init__(self, grid, function):
        assert function.dim_domain == grid.dim
        assert function.shape_range == ()
        self.grid = grid
        self.function = function
        self.range = CGVectorSpace(grid)
        self.build_parameter_type(function)

    def _assemble(self, mu=None):
        return self.function.evaluate(self.grid.centers(self.grid.dim),
                                      mu=mu).reshape((-1, 1))
Exemplo n.º 16
0
def random_array(dims, length, seed):
    if isinstance(dims, Number):
        dims = (dims, )
    return MPIVectorSpaceAutoComm(tuple(NumpyVectorSpace(dim)
                                        for dim in dims)).make_array(
                                            mpi.call(_random_array, dims,
                                                     length, seed))
Exemplo n.º 17
0
class VectorOperator(VectorArrayOperator):
    """Wrap a vector as a vector-like |Operator|.

    Given a vector `v` of dimension `d`, this class represents
    the operator ::

        op: R^1 ----> R^d
             x  |---> x⋅v

    In particular::

        VectorOperator(vector).as_range_array() == vector

    Parameters
    ----------
    vector
        |VectorArray| of length 1 containing the vector `v`.
    name
        Name of the operator.
    """

    linear = True
    source = NumpyVectorSpace(1)

    def __init__(self, vector, name=None):
        assert isinstance(vector, VectorArrayInterface)
        assert len(vector) == 1
        super().__init__(vector, adjoint=False, name=name)
Exemplo n.º 18
0
    def __init__(self, T, initial_data, operator, rhs, mass=None, time_stepper=None, num_values=None,
                 output_functional=None, products=None, error_estimator=None, visualizer=None, name=None):

        if isinstance(rhs, VectorArray):
            assert rhs in operator.range
            rhs = VectorOperator(rhs, name='rhs')
        if isinstance(initial_data, VectorArray):
            assert initial_data in operator.source
            initial_data = VectorOperator(initial_data, name='initial_data')
        mass = mass or IdentityOperator(operator.source)
        rhs = rhs or ZeroOperator(operator.source, NumpyVectorSpace(1))

        assert isinstance(time_stepper, TimeStepper)
        assert initial_data.source.is_scalar
        assert operator.source == initial_data.range
        assert rhs.linear and rhs.range == operator.range and rhs.source.is_scalar
        assert mass.linear and mass.source == mass.range == operator.source
        assert output_functional is None or output_functional.source == operator.source

        super().__init__(products=products, error_estimator=error_estimator, visualizer=visualizer, name=name)

        self.parameters_internal = {'t': 1}
        self.__auto_init(locals())
        self.solution_space = operator.source
        self.linear = operator.linear and (output_functional is None or output_functional.linear)
        if output_functional is not None:
            self.dim_output = output_functional.range.dim
Exemplo n.º 19
0
Arquivo: fv.py Projeto: simon-ca/pymor
    def __init__(self,
                 grid,
                 boundary_info,
                 numerical_flux,
                 dirichlet_data=None,
                 solver_options=None,
                 name=None):
        assert dirichlet_data is None or isinstance(dirichlet_data,
                                                    FunctionInterface)

        self.grid = grid
        self.boundary_info = boundary_info
        self.numerical_flux = numerical_flux
        self.dirichlet_data = dirichlet_data
        self.solver_options = solver_options
        self.name = name
        if (isinstance(dirichlet_data, FunctionInterface)
                and boundary_info.has_dirichlet
                and not dirichlet_data.parametric):
            self._dirichlet_values = self.dirichlet_data(
                grid.centers(1)[boundary_info.dirichlet_boundaries(1)])
            self._dirichlet_values = self._dirichlet_values.ravel()
            self._dirichlet_values_flux_shaped = self._dirichlet_values.reshape(
                (-1, 1))
        self.build_parameter_type(inherits=(numerical_flux, dirichlet_data))
        self.source = self.range = NumpyVectorSpace(grid.size(0))
        self.add_with_arguments = self.add_with_arguments.union(
            'numerical_flux_{}'.format(arg)
            for arg in numerical_flux.with_arguments)
Exemplo n.º 20
0
 def action_ConstantOperator(self, op):
     dim_range, dim_source = self.dim_range, self.dim_source
     source = op.source if dim_source is None else NumpyVectorSpace(
         dim_source)
     value = op.value if dim_range is None else NumpyVectorSpace.make_array(
         op.value.to_numpy()[:, :dim_range])
     return ConstantOperator(value, source, name=op.name)
Exemplo n.º 21
0
 def __init__(self, claw):
     self.claw = claw
     self.num_eqn = claw.solution.state.q.shape[0]
     self.mx = claw.solution.state.q.shape[1]
     self.my = claw.solution.state.q.shape[2]
     self.mz = claw.solution.state.q.shape[3]
     self.solution_space = NumpyVectorSpace(claw.solution.state.q.size)
     self.claw.start_frame = self.claw.solution.start_frame
Exemplo n.º 22
0
 def projected(self, range_basis, source_basis, product=None, name=None):
     assert source_basis is None or source_basis in self.source
     assert range_basis is None or range_basis in self.range
     assert product is None or product.source == product.range == self.range
     if source_basis is not None and range_basis is not None:
         from pymor.operators.numpy import NumpyMatrixOperator
         return NumpyMatrixOperator(np.zeros(
             (len(range_basis), len(source_basis))),
                                    name=self.name + '_projected')
     else:
         new_source = NumpyVectorSpace(
             len(source_basis)) if source_basis is not None else self.source
         new_range = NumpyVectorSpace(
             len(range_basis)) if range_basis is not None else self.range
         return ZeroOperator(new_source,
                             new_range,
                             name=self.name + '_projected')
Exemplo n.º 23
0
 def action_IdentityOperator(self, op):
     dim_range, dim_source = self.dim_range, self.dim_source
     if dim_range != dim_source:
         raise RuleNotMatchingError(
             'dim_range and dim_source must be equal.')
     space = op.source if dim_source is None else NumpyVectorSpace(
         dim_source)
     return IdentityOperator(space, name=op.name)
Exemplo n.º 24
0
def test_identity_lincomb():
    space = NumpyVectorSpace(10)
    identity = IdentityOperator(space)
    ones = space.ones()
    idid = (identity + identity)
    assert almost_equal(ones * 2, idid.apply(ones))
    assert almost_equal(ones * 2, idid.apply_adjoint(ones))
    assert almost_equal(ones * 0.5, idid.apply_inverse(ones))
    assert almost_equal(ones * 0.5, idid.apply_inverse_adjoint(ones))
Exemplo n.º 25
0
def test_to_matrix_ComponentProjection():
    dofs = np.array([0, 1, 2, 4, 8])
    n = 10
    A = np.zeros((len(dofs), n))
    A[range(len(dofs)), dofs] = 1

    source = NumpyVectorSpace(n)
    Aop = ComponentProjection(dofs, source)
    assert_type_and_allclose(A, Aop, 'sparse')
Exemplo n.º 26
0
    def __init__(self, restricted_operator, interpolation_matrix, source_basis_dofs,
                 projected_collateral_basis, triangular, solver_options=None, name=None):

        name = name or f'{restricted_operator.name}_projected'

        self.__auto_init(locals())
        self.source = NumpyVectorSpace(len(source_basis_dofs))
        self.range = projected_collateral_basis.space
        self.linear = restricted_operator.linear
Exemplo n.º 27
0
def test_complex():
    np.random.seed(0)
    I = np.eye(5)
    A = np.random.randn(5, 5)
    B = np.random.randn(5, 5)
    C = np.random.randn(3, 5)

    Iop = NumpyMatrixOperator(I)
    Aop = NumpyMatrixOperator(A)
    Bop = NumpyMatrixOperator(B)
    Cva = NumpyVectorSpace.from_data(C)

    # assemble_lincomb
    assert not np.iscomplexobj(
        Aop.assemble_lincomb((Iop, Bop), (1, 1))._matrix)
    assert not np.iscomplexobj(
        Aop.assemble_lincomb((Aop, Bop), (1, 1))._matrix)
    assert np.iscomplexobj(
        Aop.assemble_lincomb((Aop, Bop), (1 + 0j, 1 + 0j))._matrix)
    assert np.iscomplexobj(Aop.assemble_lincomb((Aop, Bop), (1j, 1))._matrix)
    assert np.iscomplexobj(Aop.assemble_lincomb((Bop, Aop), (1, 1j))._matrix)

    # apply_inverse
    assert not np.iscomplexobj(Aop.apply_inverse(Cva).data)
    assert np.iscomplexobj((Aop * 1j).apply_inverse(Cva).data)
    assert np.iscomplexobj(
        Aop.assemble_lincomb((Aop, Bop), (1, 1j)).apply_inverse(Cva).data)
    assert np.iscomplexobj(Aop.apply_inverse(Cva * 1j).data)

    # append
    for rsrv in (0, 10):
        for o_ind in (slice(None), [0]):
            va = NumpyVectorSpace(5).empty(reserve=rsrv)
            va.append(Cva)
            D = np.random.randn(1, 5) + 1j * np.random.randn(1, 5)
            Dva = NumpyVectorSpace.from_data(D)

            assert not np.iscomplexobj(va.data)
            assert np.iscomplexobj(Dva.data)
            va.append(Dva[o_ind])
            assert np.iscomplexobj(va.data)

    # scal
    assert not np.iscomplexobj(Cva.data)
    assert np.iscomplexobj((Cva * 1j).data)
    assert np.iscomplexobj((Cva * (1 + 0j)).data)

    # axpy
    assert not np.iscomplexobj(Cva.data)
    Cva[0].axpy(1, Dva)
    assert np.iscomplexobj(Cva.data)

    Cva = NumpyVectorSpace.from_data(C)
    assert not np.iscomplexobj(Cva.data)
    Cva[0].axpy(1j, Dva)
    assert np.iscomplexobj(Cva.data)
Exemplo n.º 28
0
 def __init__(self, operator, range_basis, source_basis, product=None, solver_options=None):
     assert isinstance(operator, OperatorInterface)
     assert source_basis is None or source_basis in operator.source
     assert range_basis is None or range_basis in operator.range
     assert (product is None
             or (isinstance(product, OperatorInterface)
                 and range_basis is not None
                 and operator.range == product.source
                 and product.range == product.source))
     self.build_parameter_type(operator)
     self.source = NumpyVectorSpace(len(source_basis)) if source_basis is not None else operator.source
     self.range = NumpyVectorSpace(len(range_basis)) if range_basis is not None else operator.range
     self.solver_options = solver_options
     self.name = operator.name
     self.operator = operator
     self.source_basis = source_basis.copy() if source_basis is not None else None
     self.range_basis = range_basis.copy() if range_basis is not None else None
     self.linear = operator.linear
     self.product = product
Exemplo n.º 29
0
        def __init__(self, neural_network, output_functional=None, products=None,
                     error_estimator=None, visualizer=None, name=None):

            super().__init__(products=products, error_estimator=error_estimator, visualizer=visualizer, name=name)

            self.__auto_init(locals())
            self.solution_space = NumpyVectorSpace(neural_network.output_dimension)
            self.linear = output_functional is None or output_functional.linear
            if output_functional is not None:
                self.output_space = output_functional.range
Exemplo n.º 30
0
 def action_ConstantOperator(self, op):
     range_basis, source_basis = self.range_basis, self.source_basis
     if range_basis is not None:
         projected_value = NumpyVectorSpace.make_array(range_basis.dot(op.value).T)
     else:
         projected_value = op.value
     if source_basis is None:
         return ConstantOperator(projected_value, op.source, name=op.name)
     else:
         return ConstantOperator(projected_value, NumpyVectorSpace(len(source_basis)), name=op.name)