"seed": 1 }) # define gPC gpc = pygpc.Reg(problem=problem, order=[n_basis_order] * n_dim, order_max=n_basis_order, order_max_norm=1, interaction_order=n_dim, interaction_order_current=n_dim, options=options) gpc.grid = grid # get number of basis functions n_basis = pygpc.get_num_coeffs_sparse([n_basis_order] * n_dim, n_basis_order, n_dim, n_dim, n_dim, 1) # create coefficient matrix coeffs = np.ones((len(gpc.basis.b), n_qoi)) #%% # Running the benchmark # ^^^^^^^^^^^^^^^^^^^^^ # Per default the **omp**-backend is set. Let's try them all and see how the performance changes. # If you have installed the CUDA backend you can add "cuda" to the list of backends. # It is the fastest one and outperforms all other backends. import time backends = ["python", "cpu", "omp"] # "cuda" labels = ["Python", "C++", "C++ OpenMP"] # "CUDA"
options["error_type"] = "nrmsd" options["n_samples_validation"] = 1e3 options["n_cpu"] = 0 options["fn_results"] = fn_results options["save_session_format"] = '.pkl' options["gradient_enhanced"] = False options["gradient_calculation"] = "FD_1st2nd" options["gradient_calculation_options"] = {"dx": 0.05, "distance_weight": -2} options["backend"] = "omp" options["grid"] = pygpc.Random options["grid_options"] = None # Define grid n_coeffs = pygpc.get_num_coeffs_sparse( order_dim_max=options["order"], order_glob_max=options["order_max"], order_inter_max=options["interaction_order"], dim=problem.dim) grid = pygpc.Random(parameters_random=problem.parameters_random, n_grid=options["matrix_ratio"] * n_coeffs, seed=1) # Define algorithm algorithm = pygpc.Static(problem=problem, options=options, grid=grid) #%% # Running the gpc # --------------- # Initialize gPC Session session = pygpc.Session(algorithm=algorithm)
def test_1_Static_gpc(self): """ Algorithm: Static Method: Regression Solver: Moore-Penrose Grid: RandomGrid """ global folder, plot test_name = 'pygpc_test_1_Static_gpc' print(test_name) # define model model = pygpc.testfunctions.Peaks() # define problem parameters = OrderedDict() parameters["x1"] = pygpc.Beta(pdf_shape=[1, 1], pdf_limits=[1.2, 2]) parameters["x2"] = 1.25 parameters["x3"] = pygpc.Beta(pdf_shape=[1, 1], pdf_limits=[0, 0.6]) problem = pygpc.Problem(model, parameters) # gPC options options = dict() options["method"] = "reg" options["solver"] = "Moore-Penrose" options["settings"] = None options["order"] = [9, 9] options["order_max"] = 9 options["interaction_order"] = 2 options["matrix_ratio"] = 2 options["error_type"] = "nrmsd" options["n_cpu"] = 0 options["fn_results"] = os.path.join(folder, test_name) options["gradient_enhanced"] = True options["GPU"] = False # generate grid n_coeffs = pygpc.get_num_coeffs_sparse(order_dim_max=options["order"], order_glob_max=options["order_max"], order_inter_max=options["interaction_order"], dim=problem.dim) grid = pygpc.RandomGrid(parameters_random=problem.parameters_random, options={"n_grid": options["matrix_ratio"] * n_coeffs, "seed": 1}) # define algorithm algorithm = pygpc.Static(problem=problem, options=options, grid=grid) # run gPC algorithm gpc, coeffs, results = algorithm.run() # Post-process gPC pygpc.get_sensitivities_hdf5(fn_gpc=options["fn_results"], output_idx=None, calc_sobol=True, calc_global_sens=True, calc_pdf=True, algorithm="standard", n_samples=1e3) # Validate gPC vs original model function (Monte Carlo) nrmsd = pygpc.validate_gpc_mc(gpc=gpc, coeffs=coeffs, n_samples=int(1e4), output_idx=0, fn_out=None, plot=plot) files_consistent, error_msg = pygpc.check_file_consistency(options["fn_results"] + ".hdf5") print("> Maximum NRMSD (gpc vs original): {:.2}%".format(np.max(nrmsd))) # self.expect_true(np.max(nrmsd) < 0.1, 'gPC test failed with NRMSD error = {:1.2f}%'.format(np.max(nrmsd)*100)) print("> Checking file consistency...") self.expect_true(files_consistent, error_msg) print("done!\n")