Esempio n. 1
0
 def _save_auxiliary_basis_functions_matrix(self, key):
     # Get full directory name
     full_directory = Folders.Folder(os.path.join(str(self._auxiliary_io_directory), self._auxiliary_io_filename))
     full_directory.create()
     # Save auxiliary basis functions matrix
     auxiliary_basis_functions_matrix = self._auxiliary_basis_functions_matrix[key]
     full_directory_plus_key = Folders.Folder(full_directory + "/auxiliary_basis_functions/" + self._auxiliary_key_to_folder(key))
     full_directory_plus_key.create()
     auxiliary_basis_functions_matrix.save(full_directory_plus_key, "auxiliary_basis")
Esempio n. 2
0
 def _load_auxiliary_basis_functions_matrix(self, key, auxiliary_reduced_problem, auxiliary_reduced_V):
     # Get full directory name
     full_directory = Folders.Folder(os.path.join(str(self._auxiliary_io_directory), self._auxiliary_io_filename))
     full_directory.create()
     # Load auxiliary basis functions matrix
     full_directory_plus_key = Folders.Folder(full_directory + "/auxiliary_basis_functions/" + self._auxiliary_key_to_folder(key))
     if not full_directory_plus_key.create():
         auxiliary_basis_functions_matrix = self._init_auxiliary_basis_functions_matrix(key, auxiliary_reduced_problem, auxiliary_reduced_V)
         auxiliary_basis_functions_matrix.load(full_directory_plus_key, "auxiliary_basis")
         self._auxiliary_basis_functions_matrix[key] = auxiliary_basis_functions_matrix
         return True
     else:
         return False
Esempio n. 3
0
 def save(self, directory, filename):
     # Get full directory name
     full_directory = Folders.Folder(
         os.path.join(str(directory), filename))
     full_directory.create()
     # Save reduced mesh
     self._reduced_mesh.save(directory, filename)
Esempio n. 4
0
 def save(self, directory, filename):
     # Get full directory name
     full_directory = Folders.Folder(os.path.join(str(directory), filename))
     full_directory.create()
     # Save problem corresponding to self._lhs
     assert self._lhs is not None
     LHSIO.save_file(
         get_reduced_problem_from_riesz_solve_inner_product(
             self._lhs).truth_problem.name(), full_directory,
         "lhs_problem_name")
     # Save problem corresponding to self._solution
     assert self._solution is not None
     SolutionIO.save_file(
         get_reduced_problem_from_riesz_solve_storage(
             self._solution).truth_problem.name(), full_directory,
         "solution_problem_name")
     # Save problem and operator corresponding to self._rhs
     assert self._rhs is not None
     assert isinstance(self._rhs,
                       (AbstractParametrizedTensorFactory, DelayedProduct))
     if isinstance(self._rhs, AbstractParametrizedTensorFactory):
         RHSIO.save_file("ParametrizedTensorFactory", full_directory,
                         "rhs_type")
         rhs_arg_0 = self._rhs
         rhs_problem_name_0 = get_problem_from_parametrized_operator(
             rhs_arg_0).name()
         (rhs_term_0, rhs_index_0
          ) = get_term_and_index_from_parametrized_operator(rhs_arg_0)
         RHSIO.save_file((rhs_problem_name_0, rhs_term_0, rhs_index_0),
                         full_directory, "rhs_arg_0")
     elif isinstance(self._rhs, DelayedProduct):
         RHSIO.save_file("DelayedProduct", full_directory, "rhs_type")
         assert len(self._rhs._args) is 3
         rhs_arg_0 = self._rhs._args[0]
         assert rhs_arg_0 == -1.0
         RHSIO.save_file(rhs_arg_0, full_directory, "rhs_arg_0")
         assert isinstance(self._rhs._args[1],
                           AbstractParametrizedTensorFactory)
         rhs_arg_1 = self._rhs._args[1]
         rhs_problem_name_1 = get_problem_from_parametrized_operator(
             rhs_arg_1).name()
         (rhs_term_1, rhs_index_1
          ) = get_term_and_index_from_parametrized_operator(rhs_arg_1)
         RHSIO.save_file((rhs_problem_name_1, rhs_term_1, rhs_index_1),
                         full_directory, "rhs_arg_1")
         rhs_arg_2 = self._rhs._args[2]
         rhs_problem_name_2 = rhs_problem_name_1
         (rhs_component_2, rhs_index_2
          ) = get_component_and_index_from_basis_function(rhs_arg_2)
         RHSIO.save_file((rhs_problem_name_2, rhs_component_2, rhs_index_2),
                         full_directory, "rhs_arg_2")
     else:
         raise TypeError("Invalid rhs")
     # Save problem corresponding to self._bcs
     BCsIO.save_file(
         get_reduced_problem_from_riesz_solve_homogeneous_dirichlet_bc(
             self._bcs).truth_problem.name(), full_directory,
         "bcs_problem_name")
     # Save parameters
     ParametersIO.save_file(self._parameters, full_directory, "parameters")
Esempio n. 5
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")
Esempio n. 6
0
 def save(self, directory, filename):
     # Get full directory name
     full_directory = Folders.Folder(os.path.join(str(directory), filename))
     full_directory.create()
     # Save Nmax
     self._save_Nmax(full_directory)
     # Save non hierarchical content
     for (N, affine_expansion_N) in self._content.items():
         self._save_content(N, affine_expansion_N, directory, filename)
 def patched_method(self, directory, filename):
     # Get full directory name
     full_directory = Folders.Folder(
         os.path.join(
             str(directory),
             _OfflineOnlineExpansionStorage_Base.
             _current_stage))
     full_directory.create()
     # Call original implementation
     return original_method(full_directory, filename)
Esempio n. 8
0
def create_folders():
    folders = list()
    folders.append("basis")
    folders.append("networks")
    folders.append("reduced_solve")
    folders.append("reduced_error")
    folders.append("sets")
    folders.append("truth_solve")
    for folder in folders:
        Folders.Folder(folder).create()
Esempio n. 9
0
 def save(self, directory, filename):
     # Get full directory name
     full_directory = Folders.Folder(os.path.join(str(directory), filename))
     full_directory.create()
     # Save list length
     self._save_len(full_directory)
     # Save list item vector dimension
     self._save_item_vector_dimension(full_directory)
     # Save list
     self._save_list(full_directory)
Esempio n. 10
0
 def save(self, directory, filename):
     self._assert_dict_lengths()
     # Get full directory name
     full_directory = Folders.Folder(os.path.join(str(directory), filename))
     full_directory.create()
     # Nmax
     self._save_Nmax(directory, filename)
     # reduced_mesh
     for (index, reduced_mesh) in self.reduced_mesh.items():
         mesh_filename = os.path.join(str(directory), filename, "reduced_mesh_" + str(index))
         with MeshFile(self.mesh.mpi_comm(), self.mesh.geometry().dim(), mesh_filename, "w") as output_file:
             output_file.write(reduced_mesh)
     # cannot save reduced_function_spaces to file
     # reduced_subdomain_data
     if self.subdomain_data is not None:
         for (index, reduced_subdomain_data) in self.reduced_subdomain_data.items():
             subdomain_index = 0
             for (subdomain, reduced_subdomain) in reduced_subdomain_data.items():
                 subdomain_filename = os.path.join(str(directory), filename, "reduced_mesh_" + str(index) + "_subdomain_" + str(subdomain_index))
                 with MeshFunctionFile(self.mesh.mpi_comm(), self.mesh.geometry().dim(), subdomain_filename, "w") as output_file:
                     output_file.write(reduced_subdomain)
                 subdomain_index += 1
     # reduced_mesh_markers
     for (index, reduced_mesh_markers) in self.reduced_mesh_markers.items():
         marker_filename = os.path.join(str(directory), filename, "reduced_mesh_" + str(index) + "_markers")
         with MeshFunctionFile(self.mesh.mpi_comm(), self.mesh.geometry().dim(), marker_filename, "w") as output_file:
             output_file.write(reduced_mesh_markers)
     # Init
     self._init_for_save_if_needed()
     # reduced_mesh_dofs_list
     exportable_reduced_mesh_dofs_list = ExportableList("pickle")
     for reduced_mesh_dof in self.reduced_mesh_dofs_list:
         for (component, reduced_mesh_dof__component) in enumerate(reduced_mesh_dof):
             exportable_reduced_mesh_dofs_list.append(self.reduced_mesh_dofs_list__dof_map_writer_mapping[component][reduced_mesh_dof__component])
     exportable_reduced_mesh_dofs_list.save(full_directory, "dofs")
     # reduced_mesh_reduced_dofs_list
     for (index, reduced_mesh_reduced_dofs_list) in self.reduced_mesh_reduced_dofs_list.items():
         exportable_reduced_mesh_reduced_dofs_list = ExportableList("pickle")
         for reduced_mesh_reduced_dof in reduced_mesh_reduced_dofs_list:
             for (component, reduced_mesh_reduced_dof__component) in enumerate(reduced_mesh_reduced_dof):
                 exportable_reduced_mesh_reduced_dofs_list.append(self.reduced_mesh_reduced_dofs_list__dof_map_writer_mapping[index][component][reduced_mesh_reduced_dof__component])
         exportable_reduced_mesh_reduced_dofs_list.save(full_directory, "reduced_dofs_" + str(index))
         
     # == Auxiliary basis functions == #
     # We will not save anything, because saving to file is handled by get_auxiliary_* methods.
     # We need howewer to store the directory and filename where to save
     if self._auxiliary_io_directory is None:
         self._auxiliary_io_directory = directory
     else:
         assert self._auxiliary_io_directory == directory
     if self._auxiliary_io_filename is None:
         self._auxiliary_io_filename = filename
     else:
         assert self._auxiliary_io_filename == filename
Esempio n. 11
0
 def load(self, directory, filename):
     if len(self._content) > 0: # avoid loading multiple times
         return False
     # Get full directory name
     full_directory = Folders.Folder(os.path.join(str(directory), filename))
     # Load Nmax
     Nmax = self._load_Nmax(full_directory)
     # Load non hierarchical content
     for N in range(1, Nmax + 1):
         self._content[N] = self._load_content(N, directory, filename)
     # Return
     return True
Esempio n. 12
0
 def _save_auxiliary_reduced_function_space(self, key):
     # Get full directory name
     full_directory = Folders.Folder(os.path.join(str(self._auxiliary_io_directory), self._auxiliary_io_filename))
     full_directory.create()
     # Init
     self._init_for_auxiliary_save_if_needed()
     # Save auxiliary dofs and reduced dofs
     auxiliary_dofs_to_reduced_dofs = self._auxiliary_dofs_to_reduced_dofs[key]
     # ... auxiliary dofs
     exportable_auxiliary_dofs = ExportableList("pickle")
     for auxiliary_dof in auxiliary_dofs_to_reduced_dofs.keys():
         exportable_auxiliary_dofs.append(self._auxiliary_dofs__dof_map_writer_mapping[key[0]][auxiliary_dof])
     full_directory_plus_key__dofs = Folders.Folder(full_directory + "/auxiliary_dofs/" + self._auxiliary_key_to_folder(key))
     full_directory_plus_key__dofs.create()
     exportable_auxiliary_dofs.save(full_directory_plus_key__dofs, "auxiliary_dofs")
     # ... auxiliary reduced dofs
     exportable_auxiliary_reduced_dofs = ExportableList("pickle")
     for auxiliary_reduced_dof in auxiliary_dofs_to_reduced_dofs.values():
         exportable_auxiliary_reduced_dofs.append(self._auxiliary_reduced_dofs__dof_map_writer_mapping[key][auxiliary_reduced_dof])
     full_directory_plus_key__reduced_dofs = Folders.Folder(full_directory + "/auxiliary_reduced_dofs/" + self._auxiliary_key_to_folder(key))
     full_directory_plus_key__reduced_dofs.create()
     exportable_auxiliary_reduced_dofs.save(full_directory_plus_key__reduced_dofs, "auxiliary_reduced_dofs")
Esempio n. 13
0
 def _load_auxiliary_reduced_function_space(self, key):
     # Get full directory name
     full_directory = Folders.Folder(os.path.join(str(self._auxiliary_io_directory), self._auxiliary_io_filename))
     full_directory.create()
     # Init
     self._init_for_auxiliary_load_if_needed()
     # Load auxiliary dofs and reduced dofs
     importable_auxiliary_dofs = ExportableList("pickle")
     importable_auxiliary_reduced_dofs = ExportableList("pickle")
     full_directory_plus_key__dofs = Folders.Folder(full_directory + "/auxiliary_dofs/" + self._auxiliary_key_to_folder(key))
     full_directory_plus_key__reduced_dofs = Folders.Folder(full_directory + "/auxiliary_reduced_dofs/" + self._auxiliary_key_to_folder(key))
     if not full_directory_plus_key__dofs.create() and not full_directory_plus_key__reduced_dofs.create():
         importable_auxiliary_dofs.load(full_directory_plus_key__dofs, "auxiliary_dofs")
         importable_auxiliary_reduced_dofs.load(full_directory_plus_key__reduced_dofs, "auxiliary_reduced_dofs")
         auxiliary_dofs_to_reduced_dofs = dict()
         for (dof_input, reduced_dof_input) in zip(importable_auxiliary_dofs, importable_auxiliary_reduced_dofs):
             dof = self._auxiliary_dofs__dof_map_reader_mapping[key[0]][dof_input[0]][dof_input[1]]
             reduced_dof = self._auxiliary_reduced_dofs__dof_map_reader_mapping[key][reduced_dof_input[0]][reduced_dof_input[1]]
             auxiliary_dofs_to_reduced_dofs[dof] = reduced_dof
         self._auxiliary_dofs_to_reduced_dofs[key] = auxiliary_dofs_to_reduced_dofs
         return True
     else:
         return False
Esempio n. 14
0
 def load(self, directory, filename):
     if len(self._list) > 0:  # avoid loading multiple times
         return False
     else:
         # Get full directory name
         full_directory = Folders.Folder(os.path.join(str(directory), filename))
         # Load list length
         len_ = self._load_len(full_directory)
         # Load list item vector dimension
         reference_vector = self._load_item_vector_dimension(full_directory)
         # Load list
         self._load_list(len_, reference_vector, full_directory)
         # Return
         return True
Esempio n. 15
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
Esempio n. 16
0
 def save(self, directory, filename):
     # Get full directory name
     full_directory = Folders.Folder(
         os.path.join(str(directory), filename))
     full_directory.create()
     # Exit in the trivial case of empty affine expansion
     if self._content.size is 0:
         return
     # Initialize iterator
     it = AffineExpansionStorageContent_Iterator(
         self._content,
         flags=["c_index", "multi_index", "refs_ok"],
         op_flags=["readonly"])
     # Save content item type and shape
     self._save_content_item_type_shape(self._content[it.multi_index],
                                        it, full_directory)
     # Save content
     self._save_content(self._content[it.multi_index], it,
                        full_directory)
     # Save dicts
     self._save_dicts(full_directory)
Esempio n. 17
0
 def load(self, directory, filename):
     # Get full directory name
     full_directory = Folders.Folder(os.path.join(str(directory), filename))
     # Load problem corresponding to self._lhs, and update self._lhs accordingly
     assert self._lhs is None
     assert LHSIO.exists_file(full_directory, "lhs_problem_name")
     lhs_problem_name = LHSIO.load_file(full_directory, "lhs_problem_name")
     lhs_problem = get_problem_from_problem_name(lhs_problem_name)
     lhs_reduced_problem = get_reduced_problem_from_problem(lhs_problem)
     self._lhs = lhs_reduced_problem._riesz_solve_inner_product
     # Load problem corresponding to self._solution, and update self._solution accordingly
     assert self._solution is None
     assert SolutionIO.exists_file(full_directory, "solution_problem_name")
     solution_problem_name = SolutionIO.load_file(full_directory,
                                                  "solution_problem_name")
     solution_problem = get_problem_from_problem_name(solution_problem_name)
     solution_reduced_problem = get_reduced_problem_from_problem(
         solution_problem)
     self._solution = solution_reduced_problem._riesz_solve_storage
     # Load problem and operator corresponding to self._rhs, and update self._solution accordingly
     assert self._rhs is None
     assert RHSIO.exists_file(full_directory, "rhs_type")
     rhs_type = RHSIO.load_file(full_directory, "rhs_type")
     assert rhs_type in ("ParametrizedTensorFactory", "DelayedProduct")
     if rhs_type == "ParametrizedTensorFactory":
         assert RHSIO.exists_file(full_directory, "rhs_arg_0")
         (rhs_problem_name_0, rhs_term_0,
          rhs_index_0) = RHSIO.load_file(full_directory, "rhs_arg_0")
         rhs_problem_0 = get_problem_from_problem_name(rhs_problem_name_0)
         rhs_arg_0 = rhs_problem_0.operator[rhs_term_0][rhs_index_0]
         assert isinstance(rhs_arg_0, AbstractParametrizedTensorFactory)
         self._rhs = rhs_arg_0
     elif rhs_type == "DelayedProduct":
         assert RHSIO.exists_file(full_directory, "rhs_arg_0")
         rhs_arg_0 = RHSIO.load_file(full_directory, "rhs_arg_0")
         assert rhs_arg_0 == -1.0
         assert RHSIO.exists_file(full_directory, "rhs_arg_1")
         (rhs_problem_name_1, rhs_term_1,
          rhs_index_1) = RHSIO.load_file(full_directory, "rhs_arg_1")
         rhs_problem_1 = get_problem_from_problem_name(rhs_problem_name_1)
         rhs_arg_1 = rhs_problem_1.operator[rhs_term_1][rhs_index_1]
         assert isinstance(rhs_arg_1, AbstractParametrizedTensorFactory)
         assert RHSIO.exists_file(full_directory, "rhs_arg_2")
         (rhs_problem_name_2, rhs_component_2,
          rhs_index_2) = RHSIO.load_file(full_directory, "rhs_arg_2")
         rhs_problem_2 = get_problem_from_problem_name(rhs_problem_name_2)
         rhs_reduced_problem_2 = get_reduced_problem_from_problem(
             rhs_problem_2)
         if rhs_component_2 is not None:
             rhs_arg_2 = rhs_reduced_problem_2.basis_functions[
                 rhs_component_2][rhs_index_2]
         else:
             rhs_arg_2 = rhs_reduced_problem_2.basis_functions[rhs_index_2]
         rhs = DelayedProduct(rhs_arg_0)
         rhs *= rhs_arg_1
         rhs *= rhs_arg_2
         self._rhs = rhs
     else:
         raise TypeError("Invalid rhs")
     # Load problem corresponding to self._bcs, and update self._bcs accordingly
     assert self._bcs is None
     assert BCsIO.exists_file(full_directory, "bcs_problem_name")
     bcs_problem_name = BCsIO.load_file(full_directory, "bcs_problem_name")
     bcs_problem = get_problem_from_problem_name(bcs_problem_name)
     bcs_reduced_problem = get_reduced_problem_from_problem(bcs_problem)
     self._bcs = bcs_reduced_problem._riesz_solve_homogeneous_dirichlet_bc
     # Load parameters
     assert len(self._parameters) is 0
     assert ParametersIO.exists_file(full_directory, "parameters")
     self._parameters = ParametersIO.load_file(full_directory, "parameters")
     # Return
     return True
Esempio n. 18
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