コード例 #1
0
    def _error_analysis(self, N_generator=None, filename=None, **kwargs):
        if N_generator is None:
            def N_generator():
                N = self.EIM_approximation.N
                for n in range(1, N + 1):  # n = 1, ... N
                    yield n

        def N_generator_max():
            *_, Nmax = N_generator()
            return Nmax

        interpolation_method_name = self.EIM_approximation.parametrized_expression.interpolation_method_name()
        description = self.EIM_approximation.parametrized_expression.description()

        print(TextBox(interpolation_method_name + " error analysis begins for" + "\n"
                      + "\n".join(description), fill="="))
        print("")

        error_analysis_table = ErrorAnalysisTable(self.testing_set)
        error_analysis_table.set_Nmax(N_generator_max())
        error_analysis_table.add_column("error", group_name="eim", operations=("mean", "max"))
        error_analysis_table.add_column("relative_error", group_name="eim", operations=("mean", "max"))

        for (mu_index, mu) in enumerate(self.testing_set):
            print(TextLine(interpolation_method_name + " " + str(mu_index), fill=":"))

            self.EIM_approximation.set_mu(mu)

            # Evaluate the exact function on the truth grid
            self.EIM_approximation.evaluate_parametrized_expression()

            for n in N_generator():
                self.EIM_approximation.solve(n)
                (_, error, _) = self.EIM_approximation.compute_maximum_interpolation_error(n)
                (_, relative_error, _) = self.EIM_approximation.compute_maximum_interpolation_relative_error(n)
                error_analysis_table["error", n, mu_index] = abs(error)
                error_analysis_table["relative_error", n, mu_index] = abs(relative_error)

        # Print
        print("")
        print(error_analysis_table)

        print("")
        print(TextBox(interpolation_method_name + " error analysis ends for" + "\n"
                      + "\n".join(description), fill="="))
        print("")

        # Export error analysis table
        error_analysis_table.save(self.folder["error_analysis"], "error_analysis" if filename is None else filename)
コード例 #2
0
 def solve_and_computer_error(mu):
     self.EIM_approximation.set_mu(mu)
     
     self.EIM_approximation.solve()
     self.EIM_approximation.snapshot = self.load_snapshot()
     (_, maximum_error, _) = self.EIM_approximation.compute_maximum_interpolation_error()
     return abs(maximum_error)
コード例 #3
0
ファイル: eim_approximation.py プロジェクト: pp1565156/RBniCS
 def compute_maximum_interpolation_relative_error(self, N=None):
     (absolute_error, maximum_absolute_error,
      maximum_location) = self.compute_maximum_interpolation_error(N)
     (maximum_snapshot_value, _) = max(abs(self.snapshot))
     return (absolute_error / maximum_snapshot_value,
             maximum_absolute_error / maximum_snapshot_value,
             maximum_location)
コード例 #4
0
ファイル: eim_approximation.py プロジェクト: pp1565156/RBniCS
    def _solve(self, rhs_, N=None):
        if N is None:
            N = self.N

        if N > 0:
            self._interpolation_coefficients = OnlineFunction(N)

            # Evaluate the parametrized expression at interpolation locations
            rhs = evaluate(rhs_, self.interpolation_locations[:N])

            (max_abs_rhs, _) = max(abs(rhs))
            if max_abs_rhs == 0.:
                # If the rhs is zero, then we are interpolating the zero function
                # and the default zero coefficients are enough.
                pass
            else:
                # Extract the interpolation matrix
                lhs = self.interpolation_matrix[0][:N, :N]

                # Solve the interpolation problem
                solver = OnlineLinearSolver(lhs,
                                            self._interpolation_coefficients,
                                            rhs)
                solver.solve()
        else:
            self._interpolation_coefficients = None  # OnlineFunction
コード例 #5
0
 def update_basis_greedy(self, error, maximum_error):
     if abs(maximum_error) > 0.:
         self.EIM_approximation.basis_functions.enrich(error/maximum_error)
     else:
         # Trivial case, greedy will stop at the first iteration
         assert self.EIM_approximation.N == 0
         self.EIM_approximation.basis_functions.enrich(error) # error is actually zero
     self.EIM_approximation.basis_functions.save(self.EIM_approximation.folder["basis"], "basis")
     self.EIM_approximation.N += 1
コード例 #6
0
 def compute_maximum_interpolation_relative_error(self, N=None):
     (absolute_error, maximum_absolute_error, maximum_location) = self.compute_maximum_interpolation_error(N)
     (maximum_snapshot_value, _) = max(abs(self.snapshot))
     if maximum_snapshot_value != 0.:
         return (absolute_error/maximum_snapshot_value, maximum_absolute_error/maximum_snapshot_value, maximum_location)
     else:
         if maximum_absolute_error == 0.:
             return (absolute_error, maximum_absolute_error, maximum_location) # the first two arguments are a zero expression and zero scalar
         else:
             return (None, float("NaN"), maximum_location) # the first argument should be a NaN expression
コード例 #7
0
    def greedy(self):
        assert self.EIM_approximation.basis_generation == "Greedy"

        # Print some additional information on the consistency of the reduced basis
        if self.EIM_approximation.N > 0:  # skip during initialization
            self.EIM_approximation.solve()
            self.EIM_approximation.snapshot = self.load_snapshot()
            error = self.EIM_approximation.snapshot - self.EIM_approximation.basis_functions * self.EIM_approximation._interpolation_coefficients
            error_on_interpolation_locations = evaluate(
                error, self.EIM_approximation.interpolation_locations)
            (maximum_error, _) = max(abs(error))
            (maximum_error_on_interpolation_locations,
             _) = max(abs(error_on_interpolation_locations)
                      )  # for consistency check, should be zero
            print("interpolation error for current mu =", abs(maximum_error))
            print(
                "interpolation error on interpolation locations for current mu =",
                abs(maximum_error_on_interpolation_locations))

        # Carry out the actual greedy search
        def solve_and_computer_error(mu):
            self.EIM_approximation.set_mu(mu)

            self.EIM_approximation.solve()
            self.EIM_approximation.snapshot = self.load_snapshot()
            (_, maximum_error,
             _) = self.EIM_approximation.compute_maximum_interpolation_error()
            return abs(maximum_error)

        if self.EIM_approximation.N == 0:
            print("find initial mu")
        else:
            print("find next mu")
        (error_max,
         error_argmax) = self.training_set.max(solve_and_computer_error)
        self.EIM_approximation.set_mu(self.training_set[error_argmax])
        self.greedy_selected_parameters.append(self.training_set[error_argmax])
        self.greedy_selected_parameters.save(self.folder["post_processing"],
                                             "mu_greedy")
        self.greedy_errors.append(error_max)
        self.greedy_errors.save(self.folder["post_processing"], "error_max")
        if abs(self.greedy_errors[0]) > 0.:
            return (abs(error_max), abs(error_max / self.greedy_errors[0]))
        else:
            # Trivial case, greedy should stop after one iteration after having store a zero basis function
            assert len(self.greedy_errors) in (1, 2)
            if len(self.greedy_errors) is 1:
                assert self.EIM_approximation.N == 0
                # Tweak the tolerance to force getting in the greedy loop
                self.tol = -1.
            elif len(self.greedy_errors) is 2:
                assert error_max == 0.
                assert self.EIM_approximation.N == 1
                # Tweak back the tolerance to force getting out of the greedy loop
                assert self.tol == -1.
                self.tol = 1.
            return (0., 0.)
コード例 #8
0
 def compute_maximum_interpolation_error(self, N=None):
     if N is None:
         N = self.N
     
     # Compute the error (difference with the eim approximation)
     if N > 0:
         error = self.snapshot - self.basis_functions[:N]*self._interpolation_coefficients
     else:
         error = copy(self.snapshot) # need a copy because it will be rescaled
     
     # Get the location of the maximum error
     (maximum_error, maximum_location) = max(abs(error))
     
     # Return
     return (error, maximum_error, maximum_location)
コード例 #9
0
 def greedy(self):
     assert self.EIM_approximation.basis_generation == "Greedy"
     
     # Print some additional information on the consistency of the reduced basis
     self.EIM_approximation.solve()
     self.EIM_approximation.snapshot = self.load_snapshot()
     error = self.EIM_approximation.snapshot - self.EIM_approximation.basis_functions*self.EIM_approximation._interpolation_coefficients
     error_on_interpolation_locations = evaluate(error, self.EIM_approximation.interpolation_locations)
     (maximum_error, _) = max(abs(error))
     (maximum_error_on_interpolation_locations, _) = max(abs(error_on_interpolation_locations)) # for consistency check, should be zero
     print("interpolation error for current mu =", abs(maximum_error))
     print("interpolation error on interpolation locations for current mu =", abs(maximum_error_on_interpolation_locations))
     
     # Carry out the actual greedy search
     def solve_and_computer_error(mu):
         self.EIM_approximation.set_mu(mu)
         
         self.EIM_approximation.solve()
         self.EIM_approximation.snapshot = self.load_snapshot()
         (_, maximum_error, _) = self.EIM_approximation.compute_maximum_interpolation_error()
         return abs(maximum_error)
         
     print("find next mu")
     (error_max, error_argmax) = self.training_set.max(solve_and_computer_error)
     self.EIM_approximation.set_mu(self.training_set[error_argmax])
     self.greedy_selected_parameters.append(self.training_set[error_argmax])
     self.greedy_selected_parameters.save(self.folder["post_processing"], "mu_greedy")
     self.greedy_errors.append(error_max)
     self.greedy_errors.save(self.folder["post_processing"], "error_max")
     if abs(self.greedy_errors[0]) > 0.:
         return (abs(error_max), abs(error_max/self.greedy_errors[0]))
     else:
         # Trivial case, greedy will stop at the first iteration
         assert len(self.greedy_errors) == 1
         assert self.EIM_approximation.N == 1
         return (0., 0.)