Ejemplo n.º 1
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()
Ejemplo n.º 2
0
def _read_from_xdmf_file(fun, directory, filename, suffix, components=None):
    if components is not None:
        filename = filename + "_component_" + "".join(components)
        function_name = "function_" + "".join(components)
    else:
        function_name = "function"
    fun_rank = fun.value_rank()
    fun_dim = product(fun.value_shape())
    assert fun_rank <= 2
    if ((fun_rank is 1 and fun_dim not in (2, 3))
            or (fun_rank is 2 and fun_dim not in (4, 9))):
        fun_V = fun.function_space()
        for i in range(fun_dim):
            if components is not None:
                filename_i = filename + "_subcomponent_" + str(i)
            else:
                filename_i = filename + "_component_" + str(i)
            fun_i_V = get_function_subspace(fun_V, i)
            fun_i = Function(fun_i_V)
            if not _read_from_xdmf_file(fun_i, directory, filename_i, suffix,
                                        None):
                return False
            else:
                assign(fun.sub(i), fun_i)
        return True
    else:
        full_filename_checkpoint = os.path.join(str(directory),
                                                filename + "_checkpoint.xdmf")
        file_exists = False
        if is_io_process() and os.path.exists(full_filename_checkpoint):
            file_exists = True
        file_exists = is_io_process.mpi_comm.bcast(file_exists,
                                                   root=is_io_process.root)
        if file_exists:
            if suffix is not None:
                assert SuffixIO.exists_file(directory, filename + "_suffix")
                last_suffix = SuffixIO.load_file(directory,
                                                 filename + "_suffix")
                if suffix <= last_suffix:
                    if full_filename_checkpoint in _all_xdmf_files:
                        assert _all_xdmf_latest_suffix[
                            full_filename_checkpoint] == suffix - 1
                        _all_xdmf_latest_suffix[
                            full_filename_checkpoint] = suffix
                    else:
                        assert suffix == 0
                        _all_xdmf_files[full_filename_checkpoint] = XDMFFile(
                            full_filename_checkpoint)
                        _all_xdmf_latest_suffix[full_filename_checkpoint] = 0
                    _all_xdmf_files[full_filename_checkpoint].read_checkpoint(
                        fun, function_name, suffix)
                    return True
                else:
                    return False
            else:
                with XDMFFile(full_filename_checkpoint) as file_checkpoint:
                    file_checkpoint.read_checkpoint(fun, function_name, 0)
                return True
        else:
            return False
Ejemplo n.º 3
0
 def _load_content_item_type_shape(self, full_directory):
     assert ContentItemTypeIO.exists_file(full_directory,
                                          "content_item_type")
     content_item_type = ContentItemTypeIO.load_file(
         full_directory, "content_item_type")
     assert ContentItemShapeIO.exists_file(full_directory,
                                           "content_item_shape")
     assert content_item_type in ("matrix", "vector", "function",
                                  "scalar", "functions_list",
                                  "basis_functions_matrix", "empty")
     if content_item_type == "matrix":
         (M, N) = ContentItemShapeIO.load_file(
             full_directory,
             "content_item_shape",
             globals={"OnlineSizeDict": OnlineSizeDict})
         return backend.Matrix(M, N)
     elif content_item_type == "vector":
         N = ContentItemShapeIO.load_file(
             full_directory,
             "content_item_shape",
             globals={"OnlineSizeDict": OnlineSizeDict})
         return backend.Vector(N)
     elif content_item_type == "function":
         N = ContentItemShapeIO.load_file(
             full_directory,
             "content_item_shape",
             globals={"OnlineSizeDict": OnlineSizeDict})
         return backend.Function(N)
     elif content_item_type == "scalar":
         return 0.
     elif content_item_type == "functions_list":
         # self._content has already been populated with empty items
         assert isinstance(self._content[self._smallest_key],
                           AbstractFunctionsList)
         return self._content[self._smallest_key]
     elif content_item_type == "basis_functions_matrix":
         # self._content has already been populated with empty items
         assert isinstance(self._content[self._smallest_key],
                           AbstractBasisFunctionsMatrix)
         return self._content[self._smallest_key]
     elif content_item_type == "empty":
         return None
     else:
         # impossible to arrive here anyway thanks to the assert
         raise ValueError("Invalid content item type.")
Ejemplo n.º 4
0
def import_(solution, directory, filename, suffix=None, component=None):
    if TextIO.exists_file(directory, filename):
        loaded_solution = TextIO.load_file(directory, filename)
        assert len(solution) == len(loaded_solution)
        for (i, solution_i) in enumerate(loaded_solution):
            solution[i] = float(solution_i)
        return True
    else:
        return False
Ejemplo n.º 5
0
def import_(solution, directory, filename, suffix=None, component=None):
    if suffix is not None:
        filename = filename + "_" + str(suffix)
    if TextIO.exists_file(directory, filename):
        loaded_solution = TextIO.load_file(directory, filename)
        assert len(solution) == len(loaded_solution)
        for (i, solution_i) in enumerate(loaded_solution):
            solution[i] = float(solution_i)
    else:
        raise OSError
Ejemplo n.º 6
0
 def load(self, directory, filename):
     if len(self._enrich_memory) > 0:  # avoid loading multiple times
         return False
     else:
         assert LengthIO.exists_file(directory, filename + "_length")
         len_memory = LengthIO.load_file(directory, filename + "_length")
         for index in range(len_memory):
             memory = DelayedLinearSolver()
             memory.load(directory, filename + "_" + str(index))
             self.enrich(memory)
         return True
Ejemplo n.º 7
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
Ejemplo n.º 8
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
Ejemplo n.º 9
0
 def exists_file(directory, filename):
     if not filename.endswith(".vmp"):
         filename = filename + ".vmp"
     return TextIO.exists_file(directory, filename)
Ejemplo n.º 10
0
 def _load_len(self, full_directory):
     assert LenIO.exists_file(full_directory, "len")
     return LenIO.load_file(full_directory, "len")
Ejemplo n.º 11
0
 def _init_last_index(self):
     if IndexIO.exists_file(self._directory, self._filename + "_index.sfx"):
         self._last_index = IndexIO.load_file(self._directory,
                                              self._filename + "_index.sfx")
     else:
         self._last_index = -1
Ejemplo n.º 12
0
 def _load_Nmax(self, full_directory):
     assert NmaxIO.exists_file(full_directory, "Nmax")
     return NmaxIO.load_file(full_directory, "Nmax")