Пример #1
0
 def error_analysis(self, N_generator=None, filename=None, **kwargs):
     if "components" in kwargs:
         components = kwargs["components"]
     else:
         components = self.truth_problem.components
     
     def solution_preprocess_setitem(list_over_time):
         list_squared_over_time = [v**2 for v in list_over_time]
         time_quadrature = TimeQuadrature((0., self.truth_problem.T), list_squared_over_time)
         return sqrt(time_quadrature.integrate())
         
     def output_preprocess_setitem(list_over_time):
         time_quadrature = TimeQuadrature((0., self.truth_problem.T), list_over_time)
         return time_quadrature.integrate()
     
     if len(components) > 1:
         all_components_string = ""
         for component in components:
             all_components_string += component
             for column_prefix in ("error_", "relative_error_"):
                 ErrorAnalysisTable.preprocess_setitem(column_prefix + component, solution_preprocess_setitem)
         for column_prefix in ("error_", "error_estimator_", "relative_error_", "relative_error_estimator_"):
             ErrorAnalysisTable.preprocess_setitem(column_prefix + all_components_string, solution_preprocess_setitem)
     else:
         component = components[0]
         for column_prefix in ("error_", "error_estimator_", "relative_error_", "relative_error_estimator_"):
             ErrorAnalysisTable.preprocess_setitem(column_prefix + component, solution_preprocess_setitem)
         
     for column in ("error_output", "error_estimator_output", "relative_error_output", "relative_error_estimator_output"):
         ErrorAnalysisTable.preprocess_setitem(column, solution_preprocess_setitem)
     
     DifferentialProblemReductionMethod_DerivedClass.error_analysis(self, N_generator, filename, **kwargs)
     
     ErrorAnalysisTable.clear_setitem_preprocessing()
Пример #2
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)
 def error_analysis(self, N_generator=None, filename=None, **kwargs):
     # Carry out primal error analysis ...
     DifferentialProblemReductionMethod_DerivedClass.error_analysis(self, N_generator, filename, **kwargs)
     # ... and then dual error analysis
     ErrorAnalysisTable.suppress_group("output_error")
     ErrorAnalysisTable.suppress_group("output_relative_error")
     self.dual_reduction_method.error_analysis(N_generator, filename, **kwargs)
     ErrorAnalysisTable.clear_suppressed_groups()
Пример #4
0
 def _error_analysis(self, N_generator=None, filename=None, **kwargs):
     if N_generator is None:
         def N_generator(n):
             return n
             
     N = self.SCM_approximation.N
     
     print(TextBox("SCM error analysis begins", fill="="))
     print("")
     
     error_analysis_table = ErrorAnalysisTable(self.testing_set)
     error_analysis_table.set_Nmax(N)
     error_analysis_table.add_column("normalized_error", group_name="scm", operations=("min", "mean", "max"))
     
     for (mu_index, mu) in enumerate(self.testing_set):
         print(TextLine("SCM " + str(mu_index), fill="~"))
         
         self.SCM_approximation.set_mu(mu)
         
         (exact, _) = self.SCM_approximation.evaluate_stability_factor()
         for n in range(1, N + 1): # n = 1, ... N
             n_arg = N_generator(n)
             
             if n_arg is not None:
                 LB = self.SCM_approximation.get_stability_factor_lower_bound(n_arg)
                 UB = self.SCM_approximation.get_stability_factor_upper_bound(n_arg)
                 
                 if LB/UB < 0 and not isclose(LB/UB, 0.): # if LB/UB << 0
                     print("SCM warning at mu = " + str(mu) + ": LB = " + str(LB) + " < 0")
                 if LB/UB > 1 and not isclose(LB/UB, 1.): # if LB/UB >> 1
                     print("SCM warning at mu = " + str(mu) + ": LB = " + str(LB) + " > UB = " + str(UB))
                 if LB/exact > 1 and not isclose(LB/exact, 1.): # if LB/exact >> 1
                     print("SCM warning at mu = " + str(mu) + ": LB = " + str(LB) + " > exact =" + str(exact))
                 
                 error_analysis_table["normalized_error", n, mu_index] = (exact - LB)/UB
             else:
                 error_analysis_table["normalized_error", n, mu_index] = NotImplemented
     
     # Print
     print("")
     print(error_analysis_table)
     
     print("")
     print(TextBox("SCM error analysis ends", fill="="))
     print("")
     
     # Export error analysis table
     error_analysis_table.save(self.folder["error_analysis"], "error_analysis" if filename is None else filename)
    def _error_analysis(self, N_generator=None, filename=None, **kwargs):
        if N_generator is None:

            def N_generator():
                N = self.SCM_approximation.N
                for n in range(1, N + 1):  # n = 1, ... N
                    yield n

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

        print(TextBox("SCM error analysis begins", fill="="))
        print("")

        error_analysis_table = ErrorAnalysisTable(self.testing_set)
        error_analysis_table.set_Nmax(N_generator_max())
        error_analysis_table.add_column("normalized_error",
                                        group_name="scm",
                                        operations=("min", "mean", "max"))

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

            self.SCM_approximation.set_mu(mu)

            (exact_stability_factor,
             _) = self.SCM_approximation.evaluate_stability_factor()
            for n in N_generator():
                stability_factor_lower_bound = self.SCM_approximation.get_stability_factor_lower_bound(
                    n)
                stability_factor_upper_bound = self.SCM_approximation.get_stability_factor_upper_bound(
                    n)
                ratio_lower_bound_to_upper_bound = stability_factor_lower_bound / stability_factor_upper_bound
                ratio_lower_bound_to_exact = stability_factor_lower_bound / exact_stability_factor

                if ratio_lower_bound_to_upper_bound < 0. and not isclose(
                        ratio_lower_bound_to_upper_bound, 0.):
                    # if ratio_lower_bound_to_upper_bound << 0
                    print("SCM warning at mu = " + str(mu) +
                          ": stability factor lower bound = " +
                          str(stability_factor_lower_bound) + " < 0")
                if ratio_lower_bound_to_upper_bound > 1. and not isclose(
                        ratio_lower_bound_to_upper_bound, 1.):
                    # if ratio_lower_bound_to_upper_bound >> 1
                    print("SCM warning at mu = " + str(mu) +
                          ": stability factor lower bound = " +
                          str(stability_factor_lower_bound) +
                          " > stability factor upper bound = " +
                          str(stability_factor_upper_bound))
                if ratio_lower_bound_to_exact > 1. and not isclose(
                        ratio_lower_bound_to_exact, 1.):
                    # if ratio_lower_bound_to_exact >> 1
                    print("SCM warning at mu = " + str(mu) +
                          ": stability factor lower bound = " +
                          str(stability_factor_lower_bound) +
                          " > exact stability factor =" +
                          str(exact_stability_factor))

                error_analysis_table["normalized_error", n, mu_index] = (
                    exact_stability_factor - stability_factor_lower_bound
                ) / stability_factor_upper_bound

        # Print
        print("")
        print(error_analysis_table)

        print("")
        print(TextBox("SCM error analysis ends", fill="="))
        print("")

        # Export error analysis table
        error_analysis_table.save(
            self.folder["error_analysis"],
            "error_analysis" if filename is None else filename)
Пример #6
0
        def _error_analysis(self, N_generator=None, filename=None, **kwargs):
            if N_generator is None:

                def N_generator():
                    N = self.reduced_problem.N
                    if isinstance(N, dict):
                        N = min(N.values())
                    for n in range(1, N + 1):  # n = 1, ... N
                        yield n

            if "components" in kwargs:
                components = kwargs["components"]
            else:
                components = self.truth_problem.components

            def N_generator_items():
                for n in N_generator():
                    assert isinstance(n, (dict, int))
                    if isinstance(n, int):
                        yield (n, n)
                    elif isinstance(n, dict):
                        assert len(n) == 1
                        (n_int, n_online_size_dict) = n.popitem()
                        assert isinstance(n_int, int)
                        assert isinstance(n_online_size_dict, OnlineSizeDict)
                        yield (n_int, n_online_size_dict)
                    else:
                        raise TypeError(
                            "Invalid item generated by N_generator")

            def N_generator_max():
                *_, Nmax = N_generator_items()
                assert isinstance(Nmax, tuple)
                assert len(Nmax) == 2
                assert isinstance(Nmax[0], int)
                return Nmax[0]

            print(
                TextBox(self.truth_problem.name() + " " + self.label +
                        " error analysis begins",
                        fill="="))
            print("")

            error_analysis_table = ErrorAnalysisTable(self.testing_set)
            error_analysis_table.set_Nmax(N_generator_max())
            for component in components:
                error_analysis_table.add_column("error_" + component,
                                                group_name="solution_" +
                                                component,
                                                operations=("mean", "max"))
                error_analysis_table.add_column("relative_error_" + component,
                                                group_name="solution_" +
                                                component,
                                                operations=("mean", "max"))
            error_analysis_table.add_column("error_output",
                                            group_name="output",
                                            operations=("mean", "max"))
            error_analysis_table.add_column("relative_error_output",
                                            group_name="output",
                                            operations=("mean", "max"))

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

                self.reduced_problem.set_mu(mu)

                for (n_int, n_arg) in N_generator_items():
                    self.reduced_problem.solve(n_arg, **kwargs)
                    error = self.reduced_problem.compute_error(**kwargs)
                    relative_error = self.reduced_problem.compute_relative_error(
                        **kwargs)

                    self.reduced_problem.compute_output()
                    error_output = self.reduced_problem.compute_error_output(
                        **kwargs)
                    relative_error_output = self.reduced_problem.compute_relative_error_output(
                        **kwargs)

                    if len(components) > 1:
                        for component in components:
                            error_analysis_table["error_" + component, n_int,
                                                 mu_index] = error[component]
                            error_analysis_table[
                                "relative_error_" + component, n_int,
                                mu_index] = relative_error[component]
                    else:
                        component = components[0]
                        error_analysis_table["error_" + component, n_int,
                                             mu_index] = error
                        error_analysis_table["relative_error_" + component,
                                             n_int, mu_index] = relative_error

                    error_analysis_table["error_output", n_int,
                                         mu_index] = error_output
                    error_analysis_table["relative_error_output", n_int,
                                         mu_index] = relative_error_output

            # Print
            print("")
            print(error_analysis_table)

            print("")
            print(
                TextBox(self.truth_problem.name() + " " + self.label +
                        " error analysis ends",
                        fill="="))
            print("")

            # Export error analysis table
            error_analysis_table.save(
                self.folder["error_analysis"],
                "error_analysis" if filename is None else filename)
Пример #7
0
 def _error_analysis(self, N_generator=None, filename=None, **kwargs):
     if N_generator is None:
         def N_generator(n):
             return n
             
     if "components" in kwargs:
         components = kwargs["components"]
     else:
         components = self.truth_problem.components
         
     N = self.reduced_problem.N
     if isinstance(N, dict):
         N = min(N.values())
         
     print(TextBox(self.truth_problem.name() + " " + self.label + " error analysis begins", fill="="))
     print("")
     
     error_analysis_table = ErrorAnalysisTable(self.testing_set)
     error_analysis_table.set_Nmax(N)
     for component in components:
         error_analysis_table.add_column("error_" + component, group_name="solution_" + component, operations=("mean", "max"))
         error_analysis_table.add_column("relative_error_" + component, group_name="solution_" + component, operations=("mean", "max"))
     error_analysis_table.add_column("error_output", group_name="output", operations=("mean", "max"))
     error_analysis_table.add_column("relative_error_output", group_name="output", operations=("mean", "max"))
     
     for (mu_index, mu) in enumerate(self.testing_set):
         print(TextLine(str(mu_index), fill="#"))
         
         self.reduced_problem.set_mu(mu)
                     
         for n in range(1, N + 1): # n = 1, ... N
             n_arg = N_generator(n)
             
             if n_arg is not None:
                 self.reduced_problem.solve(n_arg, **kwargs)
                 error = self.reduced_problem.compute_error(**kwargs)
                 relative_error = self.reduced_problem.compute_relative_error(**kwargs)
                 
                 self.reduced_problem.compute_output()
                 error_output = self.reduced_problem.compute_error_output(**kwargs)
                 relative_error_output = self.reduced_problem.compute_relative_error_output(**kwargs)
             else:
                 if len(components) > 1:
                     error = {component: NotImplemented for component in components}
                     relative_error = {component: NotImplemented for component in components}
                 else:
                     error = NotImplemented
                     relative_error = NotImplemented
                 
                 error_output = NotImplemented
                 relative_error_output = NotImplemented
                 
             if len(components) > 1:
                 for component in components:
                     error_analysis_table["error_" + component, n, mu_index] = error[component]
                     error_analysis_table["relative_error_" + component, n, mu_index] = relative_error[component]
             else:
                 component = components[0]
                 error_analysis_table["error_" + component, n, mu_index] = error
                 error_analysis_table["relative_error_" + component, n, mu_index] = relative_error
             
             error_analysis_table["error_output", n, mu_index] = error_output
             error_analysis_table["relative_error_output", n, mu_index] = relative_error_output
     
     # Print
     print("")
     print(error_analysis_table)
     
     print("")
     print(TextBox(self.truth_problem.name() + " " + self.label + " error analysis ends", fill="="))
     print("")
     
     # Export error analysis table
     error_analysis_table.save(self.folder["error_analysis"], "error_analysis" if filename is None else filename)