def test_9_testfunctions(self): """ Testing testfunctions (multi-threading and inherited parallelization) """ test_name = 'pygpc_test_9_testfunctions' print(test_name) tests = [] tests.append(pygpc.Peaks()) tests.append(pygpc.Franke()) tests.append(pygpc.Lim2002()) tests.append(pygpc.Ishigami()) tests.append(pygpc.ManufactureDecay()) tests.append(pygpc.GenzContinuous()) tests.append(pygpc.GenzCornerPeak()) tests.append(pygpc.GenzOscillatory()) tests.append(pygpc.GenzProductPeak()) tests.append(pygpc.Ridge()) tests.append(pygpc.SphereFun()) tests.append(pygpc.OakleyOhagan2004()) tests.append(pygpc.Welch1992()) tests.append(pygpc.HyperbolicTangent()) tests.append(pygpc.MovingParticleFrictionForce()) tests.append(pygpc.SurfaceCoverageSpecies()) tests.append(pygpc.GenzDiscontinuous()) for n_cpu in [4, 0]: if n_cpu != 0: print("Running testfunctions using multi-threading with {} cores...".format(n_cpu)) else: print("Running testfunctions using inherited function parallelization...") com = pygpc.Computation(n_cpu=n_cpu) for t in tests: grid = pygpc.RandomGrid(parameters_random=t.problem.parameters_random, options={"n_grid": 100, "seed": 1}) res = com.run(model=t.problem.model, problem=t.problem, coords=grid.coords, coords_norm=grid.coords_norm, i_iter=None, i_subiter=None, fn_results=None, print_func_time=False) com.close()
#%% # Depending on the grid and its density, the methods will behave differently. # Here, we use 100 random sampling points in the parameter space defined before. # define grid n_grid = 100 grid = pygpc.Random(parameters_random=problem.parameters_random, n_grid=n_grid, seed=1) #%% # We are setting up a Computation instance to evaluate the model function in the 100 grid points # initializing Computation class com = pygpc.Computation(n_cpu=0, matlab_model=False) # evaluating model function res = com.run(model=model, problem=problem, coords=grid.coords, coords_norm=grid.coords_norm, i_iter=None, i_subiter=None, fn_results=None, print_func_time=False) #%% # We are looping over the different methods and evaluate the gradients. The forward approximation method "FD_fwd" # returns the gradient for every grid point whereas the first and second order approximation "FD_1st" and "FD_2nd" # only return the gradient in grid points with sufficient number of neighbors. The indices stored in
# generating a 100x100 2D tensored grid x1_arr = np.linspace(-1, 1, 100) x2_arr = np.linspace(-1, 1, 100) x1, x2 = np.meshgrid(x1_arr, x2_arr) # flattening the grid to [(100*100) x 2] (random parameters only) sampling_points = np.hstack((x1.flatten()[:, np.newaxis], x2.flatten()[:, np.newaxis])) # initializing Computation class # n_cpu = 0 : use this if the model is capable of to evaluate all sampling points in parallel # n_cpu = 1 : the model is called in serial for every sampling point. # n_cpu > 1 : A multiprocessing.Pool will be opened and n_cpu sampling points are calculated in parallel com = pygpc.Computation(n_cpu=0) # running the model res = com.run(model=model, problem=problem, coords=sampling_points, coords_norm=sampling_points, i_iter=None, i_subiter=None, fn_results=None, print_func_time=None) # plotting results fig = plt.figure(figsize=(7, 5)) ax = fig.add_subplot(1, 1, 1, projection='3d')