Пример #1
0
 def __init__(self,
              config_section=None,
              key_generator=None,
              import_=None,
              export=None,
              filename_generator=None):
     self._config_section = config_section
     if self._config_section is None:
         self._storage = dict()
         self._key_generator = None
         self._import = None
         self._export = None
         self._filename_generator = None
     else:
         from rbnics.utils.config import config  # cannot import at global scope
         cache_options = config.get(self._config_section, "cache")
         assert isinstance(cache_options, set)
         if "RAM" in cache_options:
             cache_size = config.get(self._config_section,
                                     "RAM cache limit")
             assert isinstance(cache_size, str)
             if cache_size == "unlimited":
                 self._storage = dict()
             else:
                 assert cache_size.isdigit()
                 cache_size = int(cache_size)
                 assert cache_size > 0
                 self._storage = lrucache(cache_size)
             assert key_generator is not None
             self._key_generator = key_generator
         else:
             self._storage = DisabledStorage()
             self._key_generator = key_generator
         if "disk" in cache_options:
             cache_size = config.get(self._config_section,
                                     "disk cache limit")
             assert isinstance(cache_size, str)
             assert cache_size == "unlimited"
             assert import_ is not None
             self._import = import_
             assert export is not None
             self._export = export
             assert filename_generator is not None
             self._filename_generator = filename_generator
         else:
             self._import = None
             self._export = None
             self._filename_generator = None
Пример #2
0
    def __init__(self, truth_problem, parametrized_expression, folder_prefix,
                 basis_generation):
        # Call the parent initialization
        ParametrizedProblem.__init__(self, folder_prefix)
        # Store the parametrized expression
        self.parametrized_expression = parametrized_expression
        self.truth_problem = truth_problem
        assert basis_generation in ("Greedy", "POD")
        self.basis_generation = basis_generation

        # $$ ONLINE DATA STRUCTURES $$ #
        # Online reduced space dimension
        self.N = 0
        # Define additional storage for EIM
        self.interpolation_locations = parametrized_expression.create_interpolation_locations_container(
        )  # interpolation locations selected by the greedy (either a ReducedVertices or ReducedMesh)
        self.interpolation_matrix = OnlineAffineExpansionStorage(
            1)  # interpolation matrix
        # Solution
        self._interpolation_coefficients = None  # OnlineFunction

        # $$ OFFLINE DATA STRUCTURES $$ #
        self.snapshot = None  # will be filled in by Function, Vector or Matrix as appropriate in the EIM preprocessing
        self.snapshot_cache = dict()  # of Function, Vector or Matrix
        # Basis functions container
        self.basis_functions = parametrized_expression.create_basis_container()
        # I/O
        self.folder["basis"] = os.path.join(self.folder_prefix, "basis")
        self.folder["cache"] = os.path.join(self.folder_prefix, "cache")
        self.folder["reduced_operators"] = os.path.join(
            self.folder_prefix, "reduced_operators")
        self.cache_config = config.get("EIM", "cache")
Пример #3
0
    def __init__(self, V, **kwargs):

        # Call to parent
        ParametrizedProblem.__init__(self, self.name())

        # Input arguments
        self.V = V
        # Form names and order (to be filled in by child classes)
        self.terms = list()
        self.terms_order = dict()
        self.components = list()
        # Number of terms in the affine expansion
        self.Q = dict()  # from string to integer
        # Matrices/vectors resulting from the truth discretization
        self.OperatorExpansionStorage = AffineExpansionStorage
        self.operator = dict()  # from string to OperatorExpansionStorage
        self.inner_product = None  # AffineExpansionStorage (for problems with one component) or dict of AffineExpansionStorage (for problem with several components), even though it will contain only one matrix
        self._combined_inner_product = None
        self.projection_inner_product = None  # AffineExpansionStorage (for problems with one component) or dict of AffineExpansionStorage (for problem with several components), even though it will contain only one matrix
        self._combined_projection_inner_product = None
        self.dirichlet_bc = None  # AffineExpansionStorage (for problems with one component) or dict of AffineExpansionStorage (for problem with several components)
        self.dirichlet_bc_are_homogeneous = None  # bool (for problems with one component) or dict of bools (for problem with several components)
        self._combined_and_homogenized_dirichlet_bc = None
        # Solution
        self._solution = Function(self.V)
        self._solution_cache = dict()  # of Functions
        self._output = 0
        self._output_cache = dict()  # of Numbers
        self._output_cache__current_cache_key = None
        # I/O
        self.folder["cache"] = os.path.join(self.folder_prefix, "cache")
        self.cache_config = config.get("problems", "cache")
 def __init__(self, truth_problem, term, multiply_by_theta, spectrum, eigensolver_parameters, folder_prefix):
     # Call the parent initialization
     ParametrizedProblem.__init__(self, folder_prefix) # this class does not export anything
     self.truth_problem = truth_problem
     
     # Matrices/vectors resulting from the truth discretization
     self.term = term
     assert isinstance(self.term, (tuple, str))
     if isinstance(self.term, tuple):
         assert len(self.term) == 2
         isinstance(self.term[0], str)
         isinstance(self.term[1], int)
     self.multiply_by_theta = multiply_by_theta
     assert isinstance(self.multiply_by_theta, bool)
     self.operator = None # AffineExpansionStorage
     self.inner_product = None # AffineExpansionStorage, even though it will contain only one matrix
     self.spectrum = spectrum
     self.eigensolver_parameters = eigensolver_parameters
     
     # Avoid useless computations
     self._eigenvalue = 0.
     self._eigenvalue_cache = dict()
     self._eigenvector = Function(truth_problem.V)
     self._eigenvector_cache = dict()
     self.folder["cache"] = os.path.join(folder_prefix, "cache")
     self.cache_config = config.get("problems", "cache")
Пример #5
0
    def __init__(self, truth_problem, folder_prefix, **kwargs):
        # Call the parent initialization
        ParametrizedProblem.__init__(self, folder_prefix)
        # Store the parametrized problem object and the bc list
        self.truth_problem = truth_problem

        # Define additional storage for SCM
        self.B_min = BoundingBoxSideList(
        )  # minimum values of the bounding box mathcal{B}. Vector of size Q
        self.B_max = BoundingBoxSideList(
        )  # maximum values of the bounding box mathcal{B}. Vector of size Q
        self.training_set = None  # SCM algorithm needs the training set also in the online stage
        self.greedy_selected_parameters = GreedySelectedParametersList(
        )  # list storing the parameters selected during the training phase
        self.greedy_selected_parameters_complement = dict(
        )  # dict, over N, of list storing the complement of parameters selected during the training phase
        self.UB_vectors = UpperBoundsList(
        )  # list of Q-dimensional vectors storing the infimizing elements at the greedily selected parameters
        self.N = 0
        self.M_e = kwargs[
            "M_e"]  # integer denoting the number of constraints based on the exact eigenvalues, or None
        self.M_p = kwargs[
            "M_p"]  # integer denoting the number of constraints based on the previous lower bounds, or None

        # I/O
        self.folder["cache"] = os.path.join(self.folder_prefix,
                                            "reduced_cache")
        self.cache_config = config.get("SCM", "cache")
        self.folder["reduced_operators"] = os.path.join(
            self.folder_prefix, "reduced_operators")

        # Coercivity constant eigen problem
        self.exact_coercivity_constant_calculator = ParametrizedCoercivityConstantEigenProblem(
            truth_problem, "a", True, "smallest",
            kwargs["coercivity_eigensolver_parameters"], self.folder_prefix)

        # Store here input parameters provided by the user that are needed by the reduction method
        self._input_storage_for_SCM_reduction = dict()
        self._input_storage_for_SCM_reduction[
            "bounding_box_minimum_eigensolver_parameters"] = kwargs[
                "bounding_box_minimum_eigensolver_parameters"]
        self._input_storage_for_SCM_reduction[
            "bounding_box_maximum_eigensolver_parameters"] = kwargs[
                "bounding_box_maximum_eigensolver_parameters"]

        # Avoid useless linear programming solves
        self._alpha_LB = 0.
        self._alpha_LB_cache = dict()
        self._alpha_UB = 0.
        self._alpha_UB_cache = dict()
Пример #6
0
    def __init__(self, truth_problem, **kwargs):

        # Call to parent
        ParametrizedProblem.__init__(self, truth_problem.name())

        # $$ ONLINE DATA STRUCTURES $$ #
        # Online reduced space dimension
        self.N = None  # integer (for problems with one component) or dict of integers (for problem with several components)
        self.N_bc = None  # integer (for problems with one component) or dict of integers (for problem with several components)
        self.dirichlet_bc = None  # bool (for problems with one component) or dict of bools (for problem with several components)
        self.dirichlet_bc_are_homogeneous = None  # bool (for problems with one component) or dict of bools (for problem with several components)
        self._combined_and_homogenized_dirichlet_bc = None
        # Form names and order
        self.terms = truth_problem.terms
        self.terms_order = truth_problem.terms_order
        self.components = truth_problem.components
        # Number of terms in the affine expansion
        self.Q = dict()  # from string to integer
        # Reduced order operators
        self.OperatorExpansionStorage = OnlineAffineExpansionStorage
        self.operator = dict()  # from string to OperatorExpansionStorage
        self.inner_product = None  # AffineExpansionStorage (for problems with one component) or dict of AffineExpansionStorage (for problem with several components), even though it will contain only one matrix
        self._combined_inner_product = None
        self.projection_inner_product = None  # AffineExpansionStorage (for problems with one component) or dict of AffineExpansionStorage (for problem with several components), even though it will contain only one matrix
        self._combined_projection_inner_product = None
        # Solution
        self._solution = None  # OnlineFunction
        self._solution_cache = dict()  # of Functions
        self._output = 0
        self._output_cache = dict()  # of Numbers
        self._output_cache__current_cache_key = None

        # $$ OFFLINE DATA STRUCTURES $$ #
        # High fidelity problem
        self.truth_problem = truth_problem
        # Basis functions matrix
        self.basis_functions = None  # BasisFunctionsMatrix
        # I/O
        self.folder["basis"] = os.path.join(self.folder_prefix, "basis")
        self.folder["reduced_operators"] = os.path.join(
            self.folder_prefix, "reduced_operators")
        self.cache_config = config.get("reduced problems", "cache")
Пример #7
0
                            getattr(sys.modules[__name__ + "." + backend + ".wrapping"], class_or_function_impl))
                    if hasattr(sys.modules[module_name], "__all__"):
                        if class_or_function_name not in sys.modules[module_name].__all__:
                            sys.modules[module_name].__all__.append(class_or_function_name)

    # Make sure that backends do not contain dispatcher functions (but rather, actual functions and classes)
    import inspect
    for backend in required_backends:
        for class_or_function_name in sys.modules[__name__ + "." + backend].__all__:
            class_or_function = getattr(sys.modules[__name__ + "." + backend], class_or_function_name)
            assert inspect.isclass(class_or_function) or inspect.isfunction(class_or_function)

    # In contrast, make sure that this module only contains dispatcher objects
    from rbnics.utils.decorators.dispatch import Dispatcher
    for dispatcher_name in sys.modules[__name__].__all__:
        dispatcher = getattr(sys.modules[__name__], dispatcher_name)
        # if there was at least a concrete implementation by @BackendFor or @backend_for
        if isinstance(getattr(backends_cache, dispatcher_name), Dispatcher):
            assert isinstance(dispatcher, Dispatcher)

    # Store some additional classes, defined in the abstract module, which are base classes but not backends,
    # and thus have not been processed by @BackendFor and @backend_for decorators
    for extra_class in ("LinearProblemWrapper", "NonlinearProblemWrapper", "TimeDependentProblemWrapper"):
        assert not hasattr(sys.modules[__name__], extra_class)
        setattr(sys.modules[__name__], extra_class, getattr(sys.modules[__name__ + ".abstract"], extra_class))
        sys.modules[__name__].__all__.append(extra_class)


# Load required backends
load_backends(config.get("backends", "required backends"))
Пример #8
0
    # Import (current) online backend
    importlib.import_module(__name__ + "." + online_backend)
    importlib.import_module(__name__ + "." + online_backend + ".wrapping")

    # Copy all classes and functions from (current) online backend to this module with "Online" and "online_" prefixes, respectively
    for class_or_function_name in sys.modules[__name__ + "." +
                                              online_backend].__all__:
        class_or_function = getattr(
            sys.modules[__name__ + "." + online_backend],
            class_or_function_name)
        assert inspect.isclass(class_or_function) or inspect.isfunction(
            class_or_function)
        if class_or_function_name[0].isupper(
        ):  # less restrictive than inspect.isclass(class_or_function), because for instance OnlineMatrix is implemented as function rather than a class
            prefix = "Online"
        else:  # less restrictive than inspect.isfunction(class_or_function)
            prefix = "online_"
        setattr(sys.modules[__name__], prefix + class_or_function_name,
                class_or_function)
        sys.modules[__name__].__all__.append(prefix + class_or_function_name)

    # Also copy the online wrapping module, without prefixes
    sys.modules[__name__ + ".wrapping"] = sys.modules[__name__ + "." +
                                                      online_backend].wrapping


# Get the online backend name
from rbnics.utils.config import config
set_online_backend(config.get("backends", "online backend"))