Ejemplo n.º 1
0
        def _offline(self):
            print(TextBox(self.truth_problem.name() + " " + self.label + " offline phase begins", fill="="))
            print("")
            
            for (mu_index, mu) in enumerate(self.training_set):
                print(TextLine(str(mu_index), fill="#"))
                
                self.truth_problem.set_mu(mu)
                
                print("truth solve for mu =", self.truth_problem.mu)
                snapshot = self.truth_problem.solve()
                self.truth_problem.export_solution(self.folder["snapshots"], "truth_" + str(mu_index), snapshot)
                snapshot = self.postprocess_snapshot(snapshot, mu_index)
                
                print("update snapshots matrix")
                self.update_snapshots_matrix(snapshot)

                print("")
                mu_index += 1
                
            print(TextLine("perform POD", fill="#"))
            self.compute_basis_functions()
            
            print("")
            print("build reduced operators")
            self.reduced_problem.build_reduced_operators()
            
            print("")
            print(TextBox(self.truth_problem.name() + " " + self.label + " offline phase ends", fill="="))
            print("")
Ejemplo n.º 2
0
        def _offline(self):
            print(
                TextBox(self.truth_problem.name() + " " + self.label +
                        " offline phase begins",
                        fill="="))
            print("")

            # Initialize first parameter to be used
            self.reduced_problem.build_reduced_operators()
            self.reduced_problem.build_error_estimation_operators()
            (absolute_error_estimator_max,
             relative_error_estimator_max) = self.greedy()
            print(
                "initial maximum absolute error estimator over training set =",
                absolute_error_estimator_max)
            print(
                "initial maximum relative error estimator over training set =",
                relative_error_estimator_max)

            print("")

            iteration = 0
            while self.reduced_problem.N < self.Nmax and relative_error_estimator_max >= self.tol:
                print(TextLine("N = " + str(self.reduced_problem.N), fill="#"))

                print("truth solve for mu =", self.truth_problem.mu)
                snapshot = self.truth_problem.solve()
                self.truth_problem.export_solution(self.folder["snapshots"],
                                                   "truth_" + str(iteration),
                                                   snapshot)
                snapshot = self.postprocess_snapshot(snapshot, iteration)

                print("update basis matrix")
                self.update_basis_matrix(snapshot)
                iteration += 1

                print("build reduced operators")
                self.reduced_problem.build_reduced_operators()

                print("reduced order solve")
                self.reduced_problem.solve()

                print("build operators for error estimation")
                self.reduced_problem.build_error_estimation_operators()

                (absolute_error_estimator_max,
                 relative_error_estimator_max) = self.greedy()
                print("maximum absolute error estimator over training set =",
                      absolute_error_estimator_max)
                print("maximum relative error estimator over training set =",
                      relative_error_estimator_max)

                print("")

            print(
                TextBox(self.truth_problem.name() + " " + self.label +
                        " offline phase ends",
                        fill="="))
            print("")
    def _speedup_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 speedup analysis begins", fill="="))
        print("")

        speedup_analysis_table = SpeedupAnalysisTable(self.testing_set)
        speedup_analysis_table.set_Nmax(N)
        speedup_analysis_table.add_column("speedup",
                                          group_name="speedup",
                                          operations=("min", "mean", "max"))

        exact_timer = Timer("parallel")
        SCM_timer = Timer("serial")

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

            self.SCM_approximation.set_mu(mu)

            exact_timer.start()
            self.SCM_approximation.evaluate_stability_factor()
            elapsed_exact = exact_timer.stop()

            for n in range(1, N + 1):  # n = 1, ... N
                n_arg = N_generator(n)

                if n_arg is not None:
                    SCM_timer.start()
                    self.SCM_approximation.get_stability_factor_lower_bound(
                        n_arg)
                    self.SCM_approximation.get_stability_factor_upper_bound(
                        n_arg)
                    elapsed_SCM = SCM_timer.stop()
                    speedup_analysis_table[
                        "speedup", n, mu_index] = elapsed_exact / elapsed_SCM
                else:
                    speedup_analysis_table["speedup", n,
                                           mu_index] = NotImplemented

        # Print
        print("")
        print(speedup_analysis_table)

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

        # Export speedup analysis table
        speedup_analysis_table.save(
            self.folder["speedup_analysis"],
            "speedup_analysis" if filename is None else filename)
Ejemplo n.º 4
0
    def _speedup_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 + " speedup analysis begins for" + "\n"
                      + "\n".join(description), fill="="))
        print("")

        speedup_analysis_table = SpeedupAnalysisTable(self.testing_set)
        speedup_analysis_table.set_Nmax(N_generator_max())
        speedup_analysis_table.add_column("speedup", group_name="speedup", operations=("min", "mean", "max"))

        evaluate_timer = Timer("parallel")
        EIM_timer = Timer("serial")

        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
            evaluate_timer.start()
            self.EIM_approximation.evaluate_parametrized_expression()
            elapsed_evaluate = evaluate_timer.stop()

            for n in N_generator():
                EIM_timer.start()
                self.EIM_approximation.solve(n)
                elapsed_EIM = EIM_timer.stop()
                speedup_analysis_table["speedup", n, mu_index] = elapsed_evaluate / elapsed_EIM

        # Print
        print("")
        print(speedup_analysis_table)

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

        # Export speedup analysis table
        speedup_analysis_table.save(self.folder["speedup_analysis"],
                                    "speedup_analysis" if filename is None else filename)
Ejemplo n.º 5
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)
Ejemplo n.º 6
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 _offline(self):
        print(TextBox("SCM offline phase begins", fill="="))
        print("")

        # Compute the bounding box \mathcal{B}
        self.compute_bounding_box()
        print("")

        # Arbitrarily start from the first parameter in the training set
        self.SCM_approximation.set_mu(self.training_set[0])
        relative_error_estimator_max = 2. * self.tol

        while self.SCM_approximation.N < self.Nmax and relative_error_estimator_max >= self.tol:
            print(
                TextLine("SCM N = " + str(self.SCM_approximation.N), fill="~"))

            # Store the greedy parameter
            self.store_greedy_selected_parameters()

            # Evaluate the stability factor
            print("evaluate the stability factor for mu =",
                  self.SCM_approximation.mu)
            (stability_factor,
             eigenvector) = self.SCM_approximation.evaluate_stability_factor()
            print("stability factor =", stability_factor)

            # Update data structures related to upper bound vectors
            upper_bound_vector = self.compute_upper_bound_vector(eigenvector)
            self.update_upper_bound_vectors(upper_bound_vector)

            # Prepare for next iteration
            print("find next mu")
            (error_estimator_max, relative_error_estimator_max) = self.greedy()
            print("maximum SCM error estimator =", error_estimator_max)
            print("maximum SCM relative error estimator =",
                  relative_error_estimator_max)

            print("")

        print(TextBox("SCM offline phase ends", fill="="))
        print("")
Ejemplo n.º 8
0
    def _offline(self):
        interpolation_method_name = self.EIM_approximation.parametrized_expression.interpolation_method_name()
        description = self.EIM_approximation.parametrized_expression.description()
        
        # Evaluate the parametrized expression for all parameters in the training set
        print(TextBox(interpolation_method_name + " preprocessing phase begins for" + "\n" + "\n".join(description), fill="="))
        print("")
        
        for (mu_index, mu) in enumerate(self.training_set):
            print(TextLine(interpolation_method_name + " " + str(mu_index), fill=":"))
            
            self.EIM_approximation.set_mu(mu)
            
            print("evaluate parametrized expression at mu =", mu)
            self.EIM_approximation.evaluate_parametrized_expression()
            self.EIM_approximation.export_solution(self.folder["snapshots"], "truth_" + str(mu_index))
            
            print("add to snapshots")
            self.add_to_snapshots(self.EIM_approximation.snapshot)

            print("")
            
        # If basis generation is POD, compute the first POD modes of the snapshots
        if self.EIM_approximation.basis_generation == "POD":
            print("compute basis")
            N_POD = self.compute_basis_POD()
            print("")
        
        print(TextBox(interpolation_method_name + " preprocessing phase ends for" + "\n" + "\n".join(description), fill="="))
        print("")
        
        print(TextBox(interpolation_method_name + " offline phase begins for" + "\n" + "\n".join(description), fill="="))
        print("")
        
        if self.EIM_approximation.basis_generation == "Greedy":
            # Arbitrarily start from the first parameter in the training set
            self.EIM_approximation.set_mu(self.training_set[0])
            
            # Carry out greedy selection
            relative_error_max = 2.*self.tol
            while self.EIM_approximation.N < self.Nmax and relative_error_max >= self.tol:
                print(TextLine(interpolation_method_name + " N = " + str(self.EIM_approximation.N), fill=":"))
                
                self._print_greedy_interpolation_solve_message()
                self.EIM_approximation.solve()
                
                print("compute and locate maximum interpolation error")
                self.EIM_approximation.snapshot = self.load_snapshot()
                (error, maximum_error, maximum_location) = self.EIM_approximation.compute_maximum_interpolation_error()
                
                print("update locations with", maximum_location)
                self.update_interpolation_locations(maximum_location)
                
                print("update basis")
                self.update_basis_greedy(error, maximum_error)
                
                print("update interpolation matrix")
                self.update_interpolation_matrix()
                
                (error_max, relative_error_max) = self.greedy()
                print("maximum interpolation error =", error_max)
                print("maximum interpolation relative error =", relative_error_max)
                
                print("")
        else:
            while self.EIM_approximation.N < N_POD:
                print(TextLine(interpolation_method_name + " N = " + str(self.EIM_approximation.N), fill=":"))
            
                print("solve interpolation for basis number", self.EIM_approximation.N)
                self.EIM_approximation._solve(self.EIM_approximation.basis_functions[self.EIM_approximation.N])
                
                print("compute and locate maximum interpolation error")
                self.EIM_approximation.snapshot = self.EIM_approximation.basis_functions[self.EIM_approximation.N]
                (error, maximum_error, maximum_location) = self.EIM_approximation.compute_maximum_interpolation_error()
                
                print("update locations with", maximum_location)
                self.update_interpolation_locations(maximum_location)
                
                self.EIM_approximation.N += 1
                
                print("update interpolation matrix")
                self.update_interpolation_matrix()
                
                print("")
                
        print(TextBox(interpolation_method_name + " offline phase ends for" + "\n" + "\n".join(description), fill="="))
        print("")
    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)
Ejemplo n.º 10
0
        def _speedup_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

            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 +
                        " speedup analysis begins",
                        fill="="))
            print("")

            speedup_analysis_table = SpeedupAnalysisTable(self.testing_set)
            speedup_analysis_table.set_Nmax(N_generator_max())
            speedup_analysis_table.add_column("speedup_solve",
                                              group_name="speedup_solve",
                                              operations=("min", "mean",
                                                          "max"))
            speedup_analysis_table.add_column("speedup_output",
                                              group_name="speedup_output",
                                              operations=("min", "mean",
                                                          "max"))

            truth_timer = Timer("parallel")
            reduced_timer = Timer("serial")

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

                self.reduced_problem.set_mu(mu)

                truth_timer.start()
                self.truth_problem.solve(**kwargs)
                elapsed_truth_solve = truth_timer.stop()

                truth_timer.start()
                self.truth_problem.compute_output()
                elapsed_truth_output = truth_timer.stop()

                for (n_int, n_arg) in N_generator_items():
                    reduced_timer.start()
                    solution = self.reduced_problem.solve(n_arg, **kwargs)
                    elapsed_reduced_solve = reduced_timer.stop()

                    reduced_timer.start()
                    output = self.reduced_problem.compute_output()
                    elapsed_reduced_output = reduced_timer.stop()

                    if solution is not NotImplemented:
                        speedup_analysis_table[
                            "speedup_solve", n_int,
                            mu_index] = elapsed_truth_solve / elapsed_reduced_solve
                    else:
                        speedup_analysis_table["speedup_solve", n_int,
                                               mu_index] = NotImplemented
                    if output is not NotImplemented:
                        speedup_analysis_table[
                            "speedup_output", n_int,
                            mu_index] = (elapsed_truth_solve +
                                         elapsed_truth_output) / (
                                             elapsed_reduced_solve +
                                             elapsed_reduced_output)
                    else:
                        speedup_analysis_table["speedup_output", n_int,
                                               mu_index] = NotImplemented

            # Print
            print("")
            print(speedup_analysis_table)

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

            # Export speedup analysis table
            speedup_analysis_table.save(
                self.folder["speedup_analysis"],
                "speedup_analysis" if filename is None else filename)
Ejemplo n.º 11
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)
Ejemplo n.º 12
0
 def _speedup_analysis(self, N_generator=None, filename=None, **kwargs):
     if N_generator is None:
         def N_generator(n):
             return n
             
     N = self.reduced_problem.N
     if isinstance(N, dict):
         N = min(N.values())
         
     print(TextBox(self.truth_problem.name() + " " + self.label + " speedup analysis begins", fill="="))
     print("")
     
     speedup_analysis_table = SpeedupAnalysisTable(self.testing_set)
     speedup_analysis_table.set_Nmax(N)
     speedup_analysis_table.add_column("speedup_solve", group_name="speedup_solve", operations=("min", "mean", "max"))
     speedup_analysis_table.add_column("speedup_output", group_name="speedup_output", operations=("min", "mean", "max"))
     
     truth_timer = Timer("parallel")
     reduced_timer = Timer("serial")
                 
     for (mu_index, mu) in enumerate(self.testing_set):
         print(TextLine(str(mu_index), fill="#"))
         
         self.reduced_problem.set_mu(mu)
         
         truth_timer.start()
         self.truth_problem.solve(**kwargs)
         elapsed_truth_solve = truth_timer.stop()
         
         truth_timer.start()
         self.truth_problem.compute_output()
         elapsed_truth_output = truth_timer.stop()
         
         for n in range(1, N + 1): # n = 1, ... N
             n_arg = N_generator(n)
             
             if n_arg is not None:
                 reduced_timer.start()
                 solution = self.reduced_problem.solve(n_arg, **kwargs)
                 elapsed_reduced_solve = reduced_timer.stop()
                 
                 reduced_timer.start()
                 output = self.reduced_problem.compute_output()
                 elapsed_reduced_output = reduced_timer.stop()
             else:
                 solution = NotImplemented
                 output = NotImplemented
             
             if solution is not NotImplemented:
                 speedup_analysis_table["speedup_solve", n, mu_index] = elapsed_truth_solve/elapsed_reduced_solve
             else:
                 speedup_analysis_table["speedup_solve", n, mu_index] = NotImplemented
             if output is not NotImplemented:
                 speedup_analysis_table["speedup_output", n, mu_index] = (elapsed_truth_solve + elapsed_truth_output)/(elapsed_reduced_solve + elapsed_reduced_output)
             else:
                 speedup_analysis_table["speedup_output", n, mu_index] = NotImplemented
     
     # Print
     print("")
     print(speedup_analysis_table)
     
     print("")
     print(TextBox(self.truth_problem.name() + " " + self.label + " speedup analysis ends", fill="="))
     print("")
     
     # Export speedup analysis table
     speedup_analysis_table.save(self.folder["speedup_analysis"], "speedup_analysis" if filename is None else filename)
Ejemplo n.º 13
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)