Exemplo n.º 1
0
 def __setitem__(self, key, item):
     if self._type != "empty":
         assert self._type == "operators"
     else:
         self._type = "operators"
     # Reset attributes, similarly to what is done for Vector and Matrix operators
     if key == self._smallest_key:  # this assumes that __getitem__ is not random acces but called for increasing key
         self._content.pop("truth_operators_as_expansion_storage", None)
         self._content[
             "truth_operators"] = NonAffineExpansionStorageContent_Base(
                 self._shape, dtype=object)
         self._content["basis_functions"] = list()  # will stay empty
         self._content.pop("basis_functions_shape", None)
     # Store
     if isinstance(item, Number):
         self._content["truth_operators"][key] = NumericForm(item)
     else:
         assert isinstance(item, AbstractParametrizedTensorFactory)
         assert len(item._spaces) is 0
         self._content["truth_operators"][key] = item
     # Recompute (trivial) shape
     if "basis_functions_shape" not in self._content:
         self._content["basis_functions_shape"] = DelayedTransposeShape(
             self._content["basis_functions"])
     # Compute truth expansion storage and prepare precomputed slices
     if key == self._largest_key:  # this assumes that __getitem__ is not random acces but called for increasing key
         self._prepare_truth_operators_as_expansion_storage()
Exemplo n.º 2
0
 def __setitem__(self, key, item):
     if self._type != "empty":
         assert self._type == "functions_list"
     else:
         self._type = "functions_list"
         self._content[self._type] = NonAffineExpansionStorageContent_Base(self._shape, dtype=object)
     self._content[self._type][key] = DelayedFunctionsList(item.space)
Exemplo n.º 3
0
 def __setitem__(self, key, item):
     if self._type != "empty":
         assert self._type == "basis_functions_matrix"
     else:
         self._type = "basis_functions_matrix"
         self._content[self._type] = NonAffineExpansionStorageContent_Base(self._shape, dtype=object)
     self._content[self._type][key] = DelayedBasisFunctionsMatrix(item.space)
     self._content[self._type][key].init(item._components_name)
Exemplo n.º 4
0
 def __getitem__(self, key):
     assert self._type in ("error_estimation_operators_22", "operators")
     if self._type == "error_estimation_operators_22":
         assert len(key) is 2
         assert "delayed_functions" in self._content
         assert len(self._content["delayed_functions"]) is 2
         assert "delayed_functions_shape" in self._content
         
         slice_ = slice_to_array(self._content["delayed_functions_shape"], key, self._content["delayed_functions_shape"]._component_name_to_basis_component_length, self._content["delayed_functions_shape"]._component_name_to_basis_component_index)
         
         if slice_ in self._precomputed_slices:
             return self._precomputed_slices[slice_]
         else:
             output = NonAffineExpansionStorage.__new__(type(self), *self._shape)
             output.__init__(*self._shape)
             output._type = self._type
             output._content["inner_product_matrix"] = self._content["inner_product_matrix"]
             output._content["delayed_functions"] = [NonAffineExpansionStorageContent_Base(self._shape[0], dtype=object), NonAffineExpansionStorageContent_Base(self._shape[1], dtype=object)]
             for q in range(self._shape[0]):
                 output._content["delayed_functions"][0][q] = self._content["delayed_functions"][0][q][key[0]]
             for q in range(self._shape[1]):
                 output._content["delayed_functions"][1][q] = self._content["delayed_functions"][1][q][key[1]]
             output._content["delayed_functions_shape"] = DelayedTransposeShape((output._content["delayed_functions"][0][0], output._content["delayed_functions"][1][0]))
             self._precomputed_slices[slice_] = output
             return output
     elif self._type == "operators":
         assert len(key) is 2
         assert "basis_functions" in self._content
         assert len(self._content["basis_functions"]) is 2
         assert "basis_functions_shape" in self._content
         
         slices = slice_to_array(self._content["basis_functions_shape"], key, self._content["basis_functions_shape"]._component_name_to_basis_component_length, self._content["basis_functions_shape"]._component_name_to_basis_component_index)
         
         if slices in self._precomputed_slices:
             return self._precomputed_slices[slices]
         else:
             output = NonAffineExpansionStorage.__new__(type(self), *self._shape)
             output.__init__(*self._shape)
             output._type = self._type
             output._content["truth_operators"] = self._content["truth_operators"]
             output._content["truth_operators_as_expansion_storage"] = self._content["truth_operators_as_expansion_storage"]
             output._content["basis_functions"] = list()
             output._content["basis_functions"].append(self._content["basis_functions"][0][key[0]])
             output._content["basis_functions"].append(self._content["basis_functions"][1][key[1]])
             output._content["basis_functions_shape"] = DelayedTransposeShape(output._content["basis_functions"])
             self._precomputed_slices[slices] = output
             return output
     else:
         raise ValueError("Invalid type")
Exemplo n.º 5
0
 def __setitem__(self, key, item):
     assert isinstance(item._args[0],
                       (AbstractBasisFunctionsMatrix,
                        DelayedBasisFunctionsMatrix, DelayedLinearSolver))
     if isinstance(item._args[0], AbstractBasisFunctionsMatrix):
         if self._type != "empty":
             assert self._type == "operators"
         else:
             self._type = "operators"
         # Reset attributes if size has changed
         if key == self._smallest_key:  # this assumes that __getitem__ is not random acces but called for increasing key
             self._content.pop("truth_operators_as_expansion_storage", None)
             self._content[
                 "truth_operators"] = NonAffineExpansionStorageContent_Base(
                     self._shape, dtype=object)
             self._content["basis_functions"] = list()
             self._content.pop("basis_functions_shape", None)
         # Store
         assert len(item._args) in (2, 3)
         if len(self._content["basis_functions"]) is 0:
             assert isinstance(item._args[0], AbstractBasisFunctionsMatrix)
             self._content["basis_functions"].append(item._args[0])
         else:
             assert item._args[0] is self._content["basis_functions"][0]
         self._content["truth_operators"][key] = item._args[1]
         if len(item._args) > 2:
             if len(self._content["basis_functions"]) is 1:
                 assert isinstance(item._args[2],
                                   AbstractBasisFunctionsMatrix)
                 self._content["basis_functions"].append(item._args[2])
             else:
                 assert item._args[2] is self._content["basis_functions"][1]
         # Recompute shape
         if "basis_functions_shape" not in self._content:
             self._content["basis_functions_shape"] = DelayedTransposeShape(
                 self._content["basis_functions"])
         # Compute truth expansion storage and prepare precomputed slices
         if key == self._largest_key:  # this assumes that __getitem__ is not random acces but called for increasing key
             self._prepare_truth_operators_as_expansion_storage()
             self._precomputed_slices.clear()
             self._prepare_trivial_precomputed_slice()
     elif isinstance(item._args[0],
                     (DelayedBasisFunctionsMatrix, DelayedLinearSolver)):
         assert len(item._args) is 3
         assert isinstance(
             item._args[2],
             (DelayedBasisFunctionsMatrix, DelayedLinearSolver))
         if isinstance(item._args[0], DelayedLinearSolver):
             assert isinstance(item._args[2], DelayedLinearSolver)
             if self._type != "empty":
                 assert self._type == "error_estimation_operators_11"
             else:
                 self._type = "error_estimation_operators_11"
         elif isinstance(item._args[0], DelayedBasisFunctionsMatrix):
             if isinstance(item._args[2], DelayedLinearSolver):
                 if self._type != "empty":
                     assert self._type == "error_estimation_operators_21"
                 else:
                     self._type = "error_estimation_operators_21"
             elif isinstance(item._args[2], DelayedBasisFunctionsMatrix):
                 if self._type != "empty":
                     assert self._type == "error_estimation_operators_22"
                 else:
                     self._type = "error_estimation_operators_22"
             else:
                 raise TypeError(
                     "Invalid arguments to NonAffineExpansionStorage")
         else:
             raise TypeError(
                 "Invalid arguments to NonAffineExpansionStorage")
         # Reset attributes if size has changed
         if key == self._smallest_key:  # this assumes that __getitem__ is not random acces but called for increasing key
             self._content["delayed_functions"] = [
                 NonAffineExpansionStorageContent_Base(self._shape[0],
                                                       dtype=object),
                 NonAffineExpansionStorageContent_Base(self._shape[1],
                                                       dtype=object)
             ]
             self._content.pop("delayed_functions_shape", None)
             self._content.pop("inner_product_matrix", None)
         # Store
         if key[1] == self._smallest_key[
                 1]:  # this assumes that __getitem__ is not random acces but called for increasing key
             self._content["delayed_functions"][0][key[0]] = item._args[0]
         else:
             assert item._args[0] is self._content["delayed_functions"][0][
                 key[0]]
         if "inner_product_matrix" not in self._content:
             self._content["inner_product_matrix"] = item._args[1]
         else:
             assert item._args[1] is self._content["inner_product_matrix"]
         if key[0] == self._smallest_key[
                 0]:  # this assumes that __getitem__ is not random acces but called for increasing key
             self._content["delayed_functions"][1][key[1]] = item._args[2]
         else:
             assert item._args[2] is self._content["delayed_functions"][1][
                 key[1]]
         # Recompute shape
         if "delayed_functions_shape" not in self._content:
             self._content[
                 "delayed_functions_shape"] = DelayedTransposeShape(
                     (item._args[0], item._args[2]))
         else:
             assert DelayedTransposeShape((
                 item._args[0],
                 item._args[2])) == self._content["delayed_functions_shape"]
         # Prepare precomputed slices
         if key == self._largest_key:  # this assumes that __getitem__ is not random acces but called for increasing key
             self._precomputed_slices.clear()
             self._prepare_trivial_precomputed_slice()
     else:
         raise TypeError("Invalid arguments to NonAffineExpansionStorage")
Exemplo n.º 6
0
 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