예제 #1
0
# Copyright (C) 2015-2021 by the RBniCS authors
#
# This file is part of RBniCS.
#
# SPDX-License-Identifier: LGPL-3.0-or-later

from rbnics.backends.basic import copy as basic_copy
from rbnics.backends.online.numpy.function import Function
from rbnics.backends.online.numpy.matrix import Matrix
from rbnics.backends.online.numpy.vector import Vector
from rbnics.backends.online.numpy.wrapping.function_copy import basic_function_copy
from rbnics.backends.online.numpy.wrapping.tensor_copy import basic_tensor_copy
from rbnics.utils.decorators import backend_for, list_of, ModuleWrapper

backend = ModuleWrapper(Function, Matrix, Vector)
wrapping_for_wrapping = ModuleWrapper()
function_copy = basic_function_copy(backend, wrapping_for_wrapping)
tensor_copy = basic_tensor_copy(backend, wrapping_for_wrapping)
wrapping = ModuleWrapper(function_copy=function_copy, tensor_copy=tensor_copy)
copy_base = basic_copy(backend, wrapping)


@backend_for("numpy",
             inputs=((Function.Type(), list_of(Function.Type()), Matrix.Type(),
                      Vector.Type()), ))
def copy(arg):
    return copy_base(arg)
예제 #2
0
# Copyright (C) 2015-2021 by the RBniCS authors
#
# This file is part of RBniCS.
#
# SPDX-License-Identifier: LGPL-3.0-or-later

from rbnics.backends.basic import export as basic_export
from rbnics.backends.online.numpy.function import Function
from rbnics.backends.online.numpy.matrix import Matrix
from rbnics.backends.online.numpy.vector import Vector
from rbnics.backends.online.numpy.wrapping import function_save, tensor_save
from rbnics.utils.decorators import backend_for, ModuleWrapper
from rbnics.utils.io import Folders

backend = ModuleWrapper(Function, Matrix, Vector)
wrapping = ModuleWrapper(function_save, tensor_save)
export_base = basic_export(backend, wrapping)


# Export a solution to file
@backend_for("numpy",
             inputs=((Function.Type(), Matrix.Type(), Vector.Type()),
                     (Folders.Folder, str), str, (int, None), (int, str, None))
             )
def export(solution, directory, filename, suffix=None, component=None):
    export_base(solution, directory, filename, suffix, component)
예제 #3
0
except ImportError:
    has_IDA = False
else:
    has_IDA = True
from rbnics.backends.abstract import TimeStepping as AbstractTimeStepping, TimeDependentProblemWrapper
from rbnics.backends.online.basic.wrapping import DirichletBC
from rbnics.backends.online.numpy.assign import assign
from rbnics.backends.online.numpy.copy import function_copy
from rbnics.backends.online.numpy.function import Function
from rbnics.backends.online.numpy.linear_solver import LinearSolver
from rbnics.backends.online.numpy.nonlinear_solver import NonlinearSolver, NonlinearProblemWrapper
from rbnics.utils.decorators import BackendFor


@BackendFor("numpy",
            inputs=(TimeDependentProblemWrapper, Function.Type(),
                    Function.Type(), (Function.Type(), None)))
class TimeStepping(AbstractTimeStepping):
    def __init__(self,
                 problem_wrapper,
                 solution,
                 solution_dot,
                 solution_dot_dot=None):
        assert solution_dot_dot is None
        ic = problem_wrapper.ic_eval()
        if ic is not None:
            assign(solution, ic)
        self.problem = _TimeDependentProblem(problem_wrapper.residual_eval,
                                             solution, solution_dot,
                                             problem_wrapper.bc_eval,
                                             problem_wrapper.jacobian_eval,
예제 #4
0
파일: transpose.py 프로젝트: mfkiwl/RBniCS
from rbnics.backends.online.numpy.matrix import Matrix
from rbnics.backends.online.numpy.non_affine_expansion_storage import NonAffineExpansionStorage
from rbnics.backends.online.numpy.tensors_list import TensorsList
from rbnics.backends.online.numpy.vector import Vector
from rbnics.backends.online.numpy.wrapping import (
    function_to_vector, matrix_mul_vector, vector_mul_vector,
    vectorized_matrix_inner_vectorized_matrix)
from rbnics.utils.decorators import backend_for, ModuleWrapper

backend = ModuleWrapper(BasisFunctionsMatrix, Function, FunctionsList, Matrix,
                        NonAffineExpansionStorage, TensorsList, Vector)
DelayedTransposeWithArithmetic = BasicDelayedTransposeWithArithmetic(backend)
wrapping = ModuleWrapper(
    function_to_vector,
    matrix_mul_vector,
    vector_mul_vector,
    vectorized_matrix_inner_vectorized_matrix,
    DelayedTransposeWithArithmetic=DelayedTransposeWithArithmetic)
online_backend = ModuleWrapper(OnlineMatrix=Matrix, OnlineVector=Vector)
online_wrapping = ModuleWrapper()
transpose_base = basic_transpose(backend, wrapping, online_backend,
                                 online_wrapping)


@backend_for("numpy",
             inputs=((BasisFunctionsMatrix, DelayedTransposeWithArithmetic,
                      Function.Type(), FunctionsList, TensorsList,
                      Vector.Type()), ))
def transpose(arg):
    return transpose_base(arg)
예제 #5
0
#
# SPDX-License-Identifier: LGPL-3.0-or-later

from numpy.linalg import solve
from rbnics.backends.abstract import LinearProblemWrapper
from rbnics.backends.online.basic import LinearSolver as BasicLinearSolver
from rbnics.backends.online.numpy.function import Function
from rbnics.backends.online.numpy.matrix import Matrix
from rbnics.backends.online.numpy.transpose import DelayedTransposeWithArithmetic
from rbnics.backends.online.numpy.vector import Vector
from rbnics.utils.decorators import BackendFor, DictOfThetaType, ModuleWrapper, ThetaType

backend = ModuleWrapper(Function, Matrix, Vector)
wrapping = ModuleWrapper(DelayedTransposeWithArithmetic=DelayedTransposeWithArithmetic)
LinearSolver_Base = BasicLinearSolver(backend, wrapping)


@BackendFor("numpy", inputs=((Matrix.Type(), DelayedTransposeWithArithmetic, LinearProblemWrapper),
                             Function.Type(),
                             (Vector.Type(), DelayedTransposeWithArithmetic, None),
                             ThetaType + DictOfThetaType + (None,)))
class LinearSolver(LinearSolver_Base):
    def set_parameters(self, parameters):
        assert len(parameters) == 0, "NumPy linear solver does not accept parameters yet"

    def solve(self):
        solution = solve(self.lhs, self.rhs)
        self.solution.vector()[:] = solution
        if self.monitor is not None:
            self.monitor(self.solution)
예제 #6
0
#
# RBniCS 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with RBniCS. If not, see <http://www.gnu.org/licenses/>.
#

from rbnics.backends.basic import transpose as basic_transpose
from rbnics.backends.online.numpy.basis_functions_matrix import BasisFunctionsMatrix
from rbnics.backends.online.numpy.function import Function
from rbnics.backends.online.numpy.functions_list import FunctionsList
from rbnics.backends.online.numpy.matrix import Matrix
from rbnics.backends.online.numpy.non_affine_expansion_storage import NonAffineExpansionStorage
from rbnics.backends.online.numpy.tensors_list import TensorsList
from rbnics.backends.online.numpy.vector import Vector
from rbnics.backends.online.numpy.wrapping import function_to_vector, matrix_mul_vector, vector_mul_vector, vectorized_matrix_inner_vectorized_matrix
from rbnics.utils.decorators import backend_for, ModuleWrapper

backend = ModuleWrapper(BasisFunctionsMatrix, Function, FunctionsList, Matrix, NonAffineExpansionStorage, TensorsList, Vector)
wrapping = ModuleWrapper(function_to_vector, matrix_mul_vector, vector_mul_vector, vectorized_matrix_inner_vectorized_matrix)
online_backend = ModuleWrapper(OnlineMatrix=Matrix, OnlineVector=Vector)
online_wrapping = ModuleWrapper()
transpose_base = basic_transpose(backend, wrapping, online_backend, online_wrapping)

@backend_for("numpy", inputs=((BasisFunctionsMatrix, Function.Type(), FunctionsList, TensorsList, Vector.Type()), ))
def transpose(arg):
    return transpose_base(arg)
예제 #7
0
    from assimulo.solvers.sundials import IDAError
    from assimulo.problem import Implicit_Problem
except ImportError:
    has_IDA = False
else:
    has_IDA = True
from rbnics.backends.abstract import TimeStepping as AbstractTimeStepping, TimeDependentProblemWrapper
from rbnics.backends.online.basic.wrapping import DirichletBC
from rbnics.backends.online.numpy.assign import assign
from rbnics.backends.online.numpy.copy import function_copy
from rbnics.backends.online.numpy.function import Function
from rbnics.backends.online.numpy.linear_solver import LinearSolver
from rbnics.backends.online.numpy.nonlinear_solver import NonlinearSolver, NonlinearProblemWrapper
from rbnics.utils.decorators import BackendFor

@BackendFor("numpy", inputs=(TimeDependentProblemWrapper, Function.Type(), Function.Type(), (Function.Type(), None)))
class TimeStepping(AbstractTimeStepping):
    def __init__(self, problem_wrapper, solution, solution_dot, solution_dot_dot=None):
        assert problem_wrapper.time_order() in (1, 2)
        if problem_wrapper.time_order() == 1:
            assert solution_dot_dot is None
            ic = problem_wrapper.ic_eval()
            if ic is not None:
                assign(solution, ic)
            self.problem = _TimeDependentProblem1(problem_wrapper.residual_eval, solution, solution_dot, problem_wrapper.bc_eval, problem_wrapper.jacobian_eval, problem_wrapper.set_time)
            self.solver = self.problem.create_solver({"problem_type": "linear"})
        elif problem_wrapper.time_order() == 2:
            assert solution_dot_dot is not None
            ic_eval_output = problem_wrapper.ic_eval()
            assert isinstance(ic_eval_output, tuple) or ic_eval_output is None
            if ic_eval_output is not None:
예제 #8
0
# RBniCS 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with RBniCS. If not, see <http://www.gnu.org/licenses/>.
#

from numpy.linalg import solve
from rbnics.backends.online.basic import LinearSolver as BasicLinearSolver
from rbnics.backends.online.numpy.matrix import Matrix
from rbnics.backends.online.numpy.product import DelayedTransposeWithArithmetic
from rbnics.backends.online.numpy.vector import Vector
from rbnics.backends.online.numpy.function import Function
from rbnics.utils.decorators import BackendFor, DictOfThetaType, ModuleWrapper, ThetaType

backend = ModuleWrapper(Matrix, Vector)
wrapping = ModuleWrapper(DelayedTransposeWithArithmetic=DelayedTransposeWithArithmetic)
LinearSolver_Base = BasicLinearSolver(backend, wrapping)

@BackendFor("numpy", inputs=(Matrix.Type(), Function.Type(), Vector.Type(), ThetaType + DictOfThetaType + (None,)))
class LinearSolver(LinearSolver_Base):
    def set_parameters(self, parameters):
        assert len(parameters) == 0, "NumPy linear solver does not accept parameters yet"
        
    def solve(self):
        solution = solve(self.lhs, self.rhs)
        self.solution.vector()[:] = solution
        return self.solution
예제 #9
0
from scipy.optimize.nonlin import Jacobian, nonlin_solve
from rbnics.backends.abstract import NonlinearSolver as AbstractNonlinearSolver, NonlinearProblemWrapper
from rbnics.backends.online.basic.nonlinear_solver import _NonlinearProblem as _BasicNonlinearProblem
from rbnics.backends.online.numpy.function import Function
from rbnics.backends.online.numpy.matrix import Matrix
from rbnics.backends.online.numpy.transpose import DelayedTransposeWithArithmetic
from rbnics.backends.online.numpy.vector import Vector
from rbnics.utils.decorators import BackendFor, ModuleWrapper

backend = ModuleWrapper(Matrix, Vector)
wrapping = ModuleWrapper(
    DelayedTransposeWithArithmetic=DelayedTransposeWithArithmetic)
_NonlinearProblem_Base = _BasicNonlinearProblem(backend, wrapping)


@BackendFor("numpy", inputs=(NonlinearProblemWrapper, Function.Type()))
class NonlinearSolver(AbstractNonlinearSolver):
    def __init__(self, problem_wrapper, solution):
        self.problem = _NonlinearProblem(problem_wrapper.residual_eval,
                                         solution, problem_wrapper.bc_eval(),
                                         problem_wrapper.jacobian_eval)
        self.monitor = problem_wrapper.monitor
        # Additional storage which will be setup by set_parameters
        self._absolute_tolerance = None
        self._line_search = True
        self._maximum_iterations = None
        self._monitor = None
        self._relative_tolerance = None
        self._report = False
        self._solution_tolerance = None
예제 #10
0
from numpy.linalg import solve
from rbnics.backends.abstract import LinearProblemWrapper
from rbnics.backends.online.basic import LinearSolver as BasicLinearSolver
from rbnics.backends.online.numpy.function import Function
from rbnics.backends.online.numpy.matrix import Matrix
from rbnics.backends.online.numpy.transpose import DelayedTransposeWithArithmetic
from rbnics.backends.online.numpy.vector import Vector
from rbnics.utils.decorators import BackendFor, DictOfThetaType, ModuleWrapper, ThetaType

backend = ModuleWrapper(Function, Matrix, Vector)
wrapping = ModuleWrapper(
    DelayedTransposeWithArithmetic=DelayedTransposeWithArithmetic)
LinearSolver_Base = BasicLinearSolver(backend, wrapping)


@BackendFor("numpy",
            inputs=((Matrix.Type(), DelayedTransposeWithArithmetic,
                     LinearProblemWrapper), Function.Type(),
                    (Vector.Type(), DelayedTransposeWithArithmetic,
                     None), ThetaType + DictOfThetaType + (None, )))
class LinearSolver(LinearSolver_Base):
    def set_parameters(self, parameters):
        assert len(parameters
                   ) == 0, "NumPy linear solver does not accept parameters yet"

    def solve(self):
        solution = solve(self.lhs, self.rhs)
        self.solution.vector()[:] = solution
        if self.monitor is not None:
            self.monitor(self.solution)
예제 #11
0
# RBniCS 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with RBniCS. If not, see <http://www.gnu.org/licenses/>.
#

from numbers import Number
from scipy.integrate import simps
from rbnics.backends.abstract import TimeQuadrature as AbstractTimeQuadrature
from rbnics.backends.online.numpy.function import Function
from rbnics.utils.decorators import BackendFor, list_of, tuple_of

@BackendFor("numpy", inputs=(tuple_of(Number), list_of(Function.Type())))
class TimeQuadrature(AbstractTimeQuadrature):
    def __init__(self, time_interval, function_over_time):
        assert len(function_over_time) > 1
        self._time_step_size = (time_interval[1] - time_interval[0])/(len(function_over_time) - 1)
        self._function_over_time = function_over_time
        
    def integrate(self):
        vector_over_time = list()
        N = self._function_over_time[0].N
        for function in self._function_over_time:
            assert function.N == N
            vector_over_time.append(function.vector())
        integrated_vector = simps(vector_over_time, dx=self._time_step_size, axis=0)
        integrated_function = Function(N)
        integrated_function.vector()[:] = integrated_vector
예제 #12
0
# Copyright (C) 2015-2020 by the RBniCS authors
#
# This file is part of RBniCS.
#
# SPDX-License-Identifier: LGPL-3.0-or-later

from rbnics.backends.basic import export as basic_export
from rbnics.backends.online.numpy.function import Function
from rbnics.backends.online.numpy.matrix import Matrix
from rbnics.backends.online.numpy.vector import Vector
from rbnics.backends.online.numpy.wrapping import function_save, tensor_save
from rbnics.utils.decorators import backend_for, ModuleWrapper
from rbnics.utils.io import Folders

backend = ModuleWrapper(Function, Matrix, Vector)
wrapping = ModuleWrapper(function_save, tensor_save)
export_base = basic_export(backend, wrapping)


# Export a solution to file
@backend_for("numpy", inputs=((Function.Type(), Matrix.Type(), Vector.Type()), (Folders.Folder, str),
                              str, (int, None), (int, str, None)))
def export(solution, directory, filename, suffix=None, component=None):
    export_base(solution, directory, filename, suffix, component)
예제 #13
0
# Copyright (C) 2015-2021 by the RBniCS authors
#
# This file is part of RBniCS.
#
# SPDX-License-Identifier: LGPL-3.0-or-later

from rbnics.backends.online.basic.assign import assign as basic_assign
from rbnics.backends.online.numpy.function import Function
from rbnics.backends.online.numpy.matrix import Matrix
from rbnics.backends.online.numpy.vector import Vector
from rbnics.utils.decorators import backend_for, list_of, ModuleWrapper

backend = ModuleWrapper(Function, Matrix, Vector)
assign_base = basic_assign(backend)


@backend_for("numpy", inputs=((Function.Type(), list_of(Function.Type()), Matrix.Type(), Vector.Type()),
                              (Function.Type(), list_of(Function.Type()), Matrix.Type(), Vector.Type())))
def assign(object_to, object_from):
    assign_base(object_to, object_from)
예제 #14
0
파일: transpose.py 프로젝트: ljnpu/RBniCS
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with RBniCS. If not, see <http://www.gnu.org/licenses/>.
#

from rbnics.backends.online.basic import transpose as basic_transpose
from rbnics.backends.online.basic.wrapping import DelayedTransposeWithArithmetic as BasicDelayedTransposeWithArithmetic
from rbnics.backends.online.numpy.basis_functions_matrix import BasisFunctionsMatrix
from rbnics.backends.online.numpy.function import Function
from rbnics.backends.online.numpy.functions_list import FunctionsList
from rbnics.backends.online.numpy.matrix import Matrix
from rbnics.backends.online.numpy.non_affine_expansion_storage import NonAffineExpansionStorage
from rbnics.backends.online.numpy.tensors_list import TensorsList
from rbnics.backends.online.numpy.vector import Vector
from rbnics.backends.online.numpy.wrapping import function_to_vector, matrix_mul_vector, vector_mul_vector, vectorized_matrix_inner_vectorized_matrix
from rbnics.utils.decorators import backend_for, ModuleWrapper

backend = ModuleWrapper(BasisFunctionsMatrix, Function, FunctionsList, Matrix, NonAffineExpansionStorage, TensorsList, Vector)
DelayedTransposeWithArithmetic = BasicDelayedTransposeWithArithmetic(backend)
wrapping = ModuleWrapper(function_to_vector, matrix_mul_vector, vector_mul_vector, vectorized_matrix_inner_vectorized_matrix, DelayedTransposeWithArithmetic=DelayedTransposeWithArithmetic)
online_backend = ModuleWrapper(OnlineMatrix=Matrix, OnlineVector=Vector)
online_wrapping = ModuleWrapper()
transpose_base = basic_transpose(backend, wrapping, online_backend, online_wrapping)

@backend_for("numpy", inputs=((BasisFunctionsMatrix, DelayedTransposeWithArithmetic, Function.Type(), FunctionsList, TensorsList, Vector.Type()), ))
def transpose(arg):
    return transpose_base(arg)
예제 #15
0
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with RBniCS. If not, see <http://www.gnu.org/licenses/>.
#

from numpy.linalg import solve
from rbnics.backends.online.basic import LinearSolver as BasicLinearSolver
from rbnics.backends.online.numpy.matrix import Matrix
from rbnics.backends.online.numpy.vector import Vector
from rbnics.backends.online.numpy.function import Function
from rbnics.utils.decorators import BackendFor, DictOfThetaType, ThetaType

LinearSolver_Base = BasicLinearSolver


@BackendFor("numpy",
            inputs=(Matrix.Type(), Function.Type(), Vector.Type(),
                    ThetaType + DictOfThetaType + (None, )))
class LinearSolver(LinearSolver_Base):
    def set_parameters(self, parameters):
        assert len(parameters
                   ) == 0, "NumPy linear solver does not accept parameters yet"

    def solve(self):
        solution = solve(self.lhs, self.rhs)
        self.solution.vector()[:] = solution
        return self.solution