Exemple #1
0
        def __getitem__(self, key):
            """
            return the subtensors of size "key" for every element in content. (e.g. submatrices [1:5,1:5] of the affine expansion of A)
            """
            it = AffineExpansionStorageContent_Iterator(
                self._content,
                flags=["multi_index", "refs_ok"],
                op_flags=["readonly"])
            slices = slice_to_array(
                self._content[it.multi_index], key,
                self._component_name_to_basis_component_length,
                self._component_name_to_basis_component_index)

            if slices in self._precomputed_slices:
                return self._precomputed_slices[slices]
            else:
                output = _AffineExpansionStorage.__new__(
                    type(self), *self._content.shape)
                output.__init__(*self._content.shape)
                while not it.finished:
                    # Slice content and assign
                    output[it.multi_index] = self._do_slicing(
                        self._content[it.multi_index], key)
                    # Increment
                    it.iternext()
                self._precomputed_slices[slices] = output
                return output
Exemple #2
0
 def _load_dicts(self, full_directory):
     assert DictIO.exists_file(
         full_directory, "component_name_to_basis_component_index")
     self._component_name_to_basis_component_index = DictIO.load_file(
         full_directory,
         "component_name_to_basis_component_index",
         globals={
             "ComponentNameToBasisComponentIndexDict":
             ComponentNameToBasisComponentIndexDict
         })
     assert DictIO.exists_file(
         full_directory, "component_name_to_basis_component_length")
     self._component_name_to_basis_component_length = DictIO.load_file(
         full_directory,
         "component_name_to_basis_component_length",
         globals={"OnlineSizeDict": OnlineSizeDict})
     it = AffineExpansionStorageContent_Iterator(
         self._content,
         flags=["multi_index", "refs_ok"],
         op_flags=["readonly"])
     while not it.finished:
         if self._component_name_to_basis_component_index is not None:
             self._content[
                 it.
                 multi_index]._component_name_to_basis_component_index = self._component_name_to_basis_component_index
         if self._component_name_to_basis_component_length is not None:
             self._content[
                 it.
                 multi_index]._component_name_to_basis_component_length = self._component_name_to_basis_component_length
         it.iternext()
Exemple #3
0
 def load(self, directory, filename):
     if self._content is not None:  # avoid loading multiple times
         if self._content.size > 0:
             it = AffineExpansionStorageContent_Iterator(
                 self._content,
                 flags=["multi_index", "refs_ok"],
                 op_flags=["readonly"])
             while not it.finished:
                 if self._content[
                         it.
                         multi_index] is not None:  # ... but only if there is at least one element different from None
                     if isinstance(self._content[it.multi_index],
                                   AbstractFunctionsList):
                         if len(
                                 self._content[it.multi_index]
                         ) > 0:  # ... unless it is an empty FunctionsList
                             return False
                     elif isinstance(self._content[it.multi_index],
                                     AbstractBasisFunctionsMatrix):
                         if sum(
                                 self._content[it.multi_index].
                                 _component_name_to_basis_component_length
                                 .values()
                         ) > 0:  # ... unless it is an empty BasisFunctionsMatrix
                             return False
                     else:
                         return False
                 it.iternext()
     # Get full directory name
     full_directory = Folders.Folder(
         os.path.join(str(directory), filename))
     # Exit in the trivial case of empty affine expansion
     if self._content.size is 0:
         return True
     # Load content item type and shape
     reference_item = self._load_content_item_type_shape(full_directory)
     # Initialize iterator
     it = AffineExpansionStorageContent_Iterator(
         self._content, flags=["c_index", "multi_index", "refs_ok"])
     # Load content
     self._load_content(reference_item, it, full_directory)
     # Load dicts
     self._load_dicts(full_directory)
     # Reset precomputed slices
     self._precomputed_slices.clear()
     self._prepare_trivial_precomputed_slice(reference_item)
     # Return
     return True
Exemple #4
0
 def save(self, directory, filename):
     # Get full directory name
     full_directory = Folders.Folder(os.path.join(str(directory), filename))
     full_directory.create()
     # Export depending on type
     TypeIO.save_file(self._type, full_directory, "type")
     assert self._type in ("basis_functions_matrix", "empty", "error_estimation_operators_11", "error_estimation_operators_21", "error_estimation_operators_22", "functions_list", "operators")
     if self._type in ("basis_functions_matrix", "functions_list"):
         # Save delayed functions
         delayed_functions = self._content[self._type]
         it = NonAffineExpansionStorageContent_Iterator(delayed_functions, flags=["c_index", "multi_index", "refs_ok"], op_flags=["readonly"])
         while not it.finished:
             delayed_function = delayed_functions[it.multi_index]
             delayed_function.save(full_directory, "delayed_functions_" + str(it.index))
             it.iternext()
     elif self._type == "empty":
         pass
     elif self._type in ("error_estimation_operators_11", "error_estimation_operators_21", "error_estimation_operators_22"):
         # Save delayed functions
         delayed_function_type = {
             DelayedBasisFunctionsMatrix: "DelayedBasisFunctionsMatrix",
             DelayedLinearSolver: "DelayedLinearSolver"
         }
         assert len(self._content["delayed_functions"]) is 2
         for (index, delayed_functions) in enumerate(self._content["delayed_functions"]):
             it = NonAffineExpansionStorageContent_Iterator(delayed_functions, flags=["c_index", "refs_ok"], op_flags=["readonly"])
             while not it.finished:
                 delayed_function = delayed_functions[it.index]
                 DelayedFunctionsTypeIO.save_file(delayed_function_type[type(delayed_function)], full_directory, "delayed_functions_" + str(index) + "_" + str(it.index) + "_type")
                 DelayedFunctionsProblemNameIO.save_file(delayed_function.get_problem_name(), full_directory, "delayed_functions_" + str(index) + "_" + str(it.index) + "_problem_name")
                 delayed_function.save(full_directory, "delayed_functions_" + str(index) + "_" + str(it.index) + "_content")
                 it.iternext()
         ErrorEstimationInnerProductIO.save_file(get_reduced_problem_from_error_estimation_inner_product(self._content["inner_product_matrix"]).truth_problem.name(), full_directory, "inner_product_matrix_problem_name")
     elif self._type == "operators":
         # Save truth content
         it = NonAffineExpansionStorageContent_Iterator(self._content["truth_operators"], flags=["c_index", "multi_index", "refs_ok"], op_flags=["readonly"])
         while not it.finished:
             operator = self._content["truth_operators"][it.multi_index]
             assert isinstance(operator, (AbstractParametrizedTensorFactory, NumericForm))
             if isinstance(operator, AbstractParametrizedTensorFactory):
                 problem_name = get_problem_from_parametrized_operator(operator).name()
                 (term, index) = get_term_and_index_from_parametrized_operator(operator)
                 TruthContentItemIO.save_file("ParametrizedTensorFactory", full_directory, "truth_operator_" + str(it.index) + "_type")
                 TruthContentItemIO.save_file((problem_name, term, index), full_directory, "truth_operator_" + str(it.index))
             elif isinstance(operator, NumericForm):
                 TruthContentItemIO.save_file("NumericForm", full_directory, "truth_operator_" + str(it.index) + "_type")
                 TruthContentItemIO.save_file(operator, full_directory, "truth_operator_" + str(it.index))
             else:
                 raise TypeError("Invalid operator type")
             it.iternext()
         assert "truth_operators_as_expansion_storage" in self._content
         # Save basis functions content
         assert len(self._content["basis_functions"]) in (0, 1, 2)
         BasisFunctionsContentLengthIO.save_file(len(self._content["basis_functions"]), full_directory, "basis_functions_length")
         for (index, basis_functions) in enumerate(self._content["basis_functions"]):
             BasisFunctionsProblemNameIO.save_file(get_reduced_problem_from_basis_functions(basis_functions).truth_problem.name(), full_directory, "basis_functions_" + str(index) + "_problem_name")
             BasisFunctionsProblemNameIO.save_file(basis_functions._components_name, full_directory, "basis_functions_" + str(index) + "_components_name")
     else:
         raise ValueError("Invalid type")
 def load(self, directory, filename):
     if self._type != "empty":  # avoid loading multiple times
         if self._type in ("basis_functions_matrix", "functions_list"):
             delayed_functions = self._content[self._type]
             it = NonAffineExpansionStorageContent_Iterator(
                 delayed_functions,
                 flags=["c_index", "multi_index", "refs_ok"],
                 op_flags=["readonly"])
             while not it.finished:
                 if isinstance(delayed_functions[it.multi_index],
                               DelayedFunctionsList):
                     assert self._type == "functions_list"
                     if len(
                             delayed_functions[it.multi_index]
                     ) > 0:  # ... unless it is an empty FunctionsList
                         return False
                 elif isinstance(delayed_functions[it.multi_index],
                                 DelayedBasisFunctionsMatrix):
                     assert self._type == "basis_functions_matrix"
                     if sum(
                             delayed_functions[it.multi_index].
                             _component_name_to_basis_component_length.
                             values()
                     ) > 0:  # ... unless it is an empty BasisFunctionsMatrix
                         return False
                 else:
                     raise TypeError("Invalid delayed functions")
                 it.iternext()
         else:
             return False
     # Get full directory name
     full_directory = Folders.Folder(os.path.join(str(directory), filename))
     # Detect trivial case
     assert TypeIO.exists_file(full_directory, "type")
     imported_type = TypeIO.load_file(full_directory, "type")
     self._type = imported_type
     assert self._type in ("basis_functions_matrix", "empty",
                           "error_estimation_operators_11",
                           "error_estimation_operators_21",
                           "error_estimation_operators_22",
                           "functions_list", "operators")
     if self._type in ("basis_functions_matrix", "functions_list"):
         # Load delayed functions
         assert self._type in self._content
         delayed_functions = self._content[self._type]
         it = NonAffineExpansionStorageContent_Iterator(
             delayed_functions, flags=["c_index", "multi_index", "refs_ok"])
         while not it.finished:
             delayed_function = delayed_functions[it.multi_index]
             delayed_function.load(full_directory,
                                   "delayed_functions_" + str(it.index))
             it.iternext()
     elif self._type == "empty":
         pass
     elif self._type in ("error_estimation_operators_11",
                         "error_estimation_operators_21",
                         "error_estimation_operators_22"):
         # Load delayed functions
         assert "delayed_functions" not in self._content
         self._content["delayed_functions"] = [
             NonAffineExpansionStorageContent_Base(self._shape[0],
                                                   dtype=object),
             NonAffineExpansionStorageContent_Base(self._shape[1],
                                                   dtype=object)
         ]
         for (index, delayed_functions) in enumerate(
                 self._content["delayed_functions"]):
             it = NonAffineExpansionStorageContent_Iterator(
                 delayed_functions, flags=["c_index", "refs_ok"])
             while not it.finished:
                 assert DelayedFunctionsTypeIO.exists_file(
                     full_directory, "delayed_functions_" + str(index) +
                     "_" + str(it.index) + "_type")
                 delayed_function_type = DelayedFunctionsTypeIO.load_file(
                     full_directory, "delayed_functions_" + str(index) +
                     "_" + str(it.index) + "_type")
                 assert DelayedFunctionsProblemNameIO.exists_file(
                     full_directory, "delayed_functions_" + str(index) +
                     "_" + str(it.index) + "_problem_name")
                 delayed_function_problem_name = DelayedFunctionsProblemNameIO.load_file(
                     full_directory, "delayed_functions_" + str(index) +
                     "_" + str(it.index) + "_problem_name")
                 delayed_function_problem = get_problem_from_problem_name(
                     delayed_function_problem_name)
                 assert delayed_function_type in (
                     "DelayedBasisFunctionsMatrix", "DelayedLinearSolver")
                 if delayed_function_type == "DelayedBasisFunctionsMatrix":
                     delayed_function = DelayedBasisFunctionsMatrix(
                         delayed_function_problem.V)
                     delayed_function.init(
                         delayed_function_problem.components)
                 elif delayed_function_type == "DelayedLinearSolver":
                     delayed_function = DelayedLinearSolver()
                 else:
                     raise ValueError("Invalid delayed function")
                 delayed_function.load(
                     full_directory, "delayed_functions_" + str(index) +
                     "_" + str(it.index) + "_content")
                 delayed_functions[it.index] = delayed_function
                 it.iternext()
         # Load inner product
         assert ErrorEstimationInnerProductIO.exists_file(
             full_directory, "inner_product_matrix_problem_name")
         inner_product_matrix_problem_name = ErrorEstimationInnerProductIO.load_file(
             full_directory, "inner_product_matrix_problem_name")
         inner_product_matrix_problem = get_problem_from_problem_name(
             inner_product_matrix_problem_name)
         inner_product_matrix_reduced_problem = get_reduced_problem_from_problem(
             inner_product_matrix_problem)
         self._content[
             "inner_product_matrix"] = inner_product_matrix_reduced_problem._error_estimation_inner_product
         # Recompute shape
         assert "delayed_functions_shape" not in self._content
         self._content["delayed_functions_shape"] = DelayedTransposeShape(
             (self._content["delayed_functions"][0][0],
              self._content["delayed_functions"][1][0]))
         # Prepare precomputed slices
         self._precomputed_slices.clear()
         self._prepare_trivial_precomputed_slice()
     elif self._type == "empty":
         pass
     elif self._type == "operators":
         # Load truth content
         assert "truth_operators" not in self._content
         self._content[
             "truth_operators"] = NonAffineExpansionStorageContent_Base(
                 self._shape, dtype=object)
         it = NonAffineExpansionStorageContent_Iterator(
             self._content["truth_operators"],
             flags=["c_index", "multi_index", "refs_ok"])
         while not it.finished:
             assert TruthContentItemIO.exists_file(
                 full_directory,
                 "truth_operator_" + str(it.index) + "_type")
             operator_type = TruthContentItemIO.load_file(
                 full_directory,
                 "truth_operator_" + str(it.index) + "_type")
             assert operator_type in ("NumericForm",
                                      "ParametrizedTensorFactory")
             if operator_type == "NumericForm":
                 assert TruthContentItemIO.exists_file(
                     full_directory, "truth_operator_" + str(it.index))
                 value = TruthContentItemIO.load_file(
                     full_directory, "truth_operator_" + str(it.index))
                 self._content["truth_operators"][
                     it.multi_index] = NumericForm(value)
             elif operator_type == "ParametrizedTensorFactory":
                 assert TruthContentItemIO.exists_file(
                     full_directory, "truth_operator_" + str(it.index))
                 (problem_name, term, index) = TruthContentItemIO.load_file(
                     full_directory, "truth_operator_" + str(it.index))
                 truth_problem = get_problem_from_problem_name(problem_name)
                 self._content["truth_operators"][
                     it.multi_index] = truth_problem.operator[term][index]
             else:
                 raise ValueError("Invalid operator type")
             it.iternext()
         assert "truth_operators_as_expansion_storage" not in self._content
         self._prepare_truth_operators_as_expansion_storage()
         # Load basis functions content
         assert BasisFunctionsContentLengthIO.exists_file(
             full_directory, "basis_functions_length")
         basis_functions_length = BasisFunctionsContentLengthIO.load_file(
             full_directory, "basis_functions_length")
         assert basis_functions_length in (0, 1, 2)
         assert "basis_functions" not in self._content
         self._content["basis_functions"] = list()
         for index in range(basis_functions_length):
             assert BasisFunctionsProblemNameIO.exists_file(
                 full_directory,
                 "basis_functions_" + str(index) + "_problem_name")
             basis_functions_problem_name = BasisFunctionsProblemNameIO.load_file(
                 full_directory,
                 "basis_functions_" + str(index) + "_problem_name")
             assert BasisFunctionsProblemNameIO.exists_file(
                 full_directory,
                 "basis_functions_" + str(index) + "_components_name")
             basis_functions_components_name = BasisFunctionsProblemNameIO.load_file(
                 full_directory,
                 "basis_functions_" + str(index) + "_components_name")
             basis_functions_problem = get_problem_from_problem_name(
                 basis_functions_problem_name)
             basis_functions_reduced_problem = get_reduced_problem_from_problem(
                 basis_functions_problem)
             basis_functions = basis_functions_reduced_problem.basis_functions
             if basis_functions_components_name != basis_functions_problem.components:
                 basis_functions = basis_functions[
                     basis_functions_components_name]
             self._content["basis_functions"].append(basis_functions)
         # Recompute shape
         self._content["basis_functions_shape"] = DelayedTransposeShape(
             self._content["basis_functions"])
         # Reset precomputed slices
         self._precomputed_slices.clear()
         self._prepare_trivial_precomputed_slice()
     else:
         raise ValueError("Invalid type")
     return True