| 
 | 1 | +using Plots  | 
 | 2 | +using BenchmarkTools  | 
 | 3 | + | 
 | 4 | +# Generate a rectangular point cloud  | 
 | 5 | +include("../test/point_cloud.jl")  | 
 | 6 | + | 
 | 7 | +"""  | 
 | 8 | +    plot_benchmarks(benchmark, n_points_per_dimension, iterations;  | 
 | 9 | +                    seed = 1, perturbation_factor_position = 1.0,  | 
 | 10 | +                    parallel = true, title = "")  | 
 | 11 | +
  | 
 | 12 | +Run a benchmark for with several neighborhood searches multiple times for increasing numbers  | 
 | 13 | +of points and plot the results.  | 
 | 14 | +
  | 
 | 15 | +# Arguments  | 
 | 16 | +- `benchmark`:              The benchmark function. See [`benchmark_count_neigbors`](@ref)  | 
 | 17 | +                            and [`benchmark_n_body`](@ref).  | 
 | 18 | +- `n_points_per_dimension`: Initial resolution as tuple. The product is the initial number  | 
 | 19 | +                            of points. For example, use `(100, 100)` for a 2D benchmark or  | 
 | 20 | +                            `(10, 10, 10)` for a 3D benchmark.  | 
 | 21 | +- `iterations`:             Number of refinement iterations  | 
 | 22 | +
  | 
 | 23 | +# Keywords  | 
 | 24 | +- `parallel = true`:        Loop over all points in parallel  | 
 | 25 | +- `title = ""`:             Title of the plot  | 
 | 26 | +- `seed = 1`:               Seed to perturb the point positions. Different seeds yield  | 
 | 27 | +                            slightly different point positions.  | 
 | 28 | +- `perturbation_factor_position = 1.0`: Perturb point positions by this factor. A factor of  | 
 | 29 | +                                        `1.0` corresponds to points being moved by  | 
 | 30 | +                                        a maximum distance of `0.5` along each axis.  | 
 | 31 | +"""  | 
 | 32 | +function plot_benchmarks(benchmark, n_points_per_dimension, iterations;  | 
 | 33 | +                         parallel = true, title = "",  | 
 | 34 | +                         seed = 1, perturbation_factor_position = 1.0)  | 
 | 35 | +    neighborhood_searches_names = ["TrivialNeighborhoodSearch";;  | 
 | 36 | +                                   "GridNeighborhoodSearch"]  | 
 | 37 | + | 
 | 38 | +    # Multiply number of points in each iteration (roughly) by this factor  | 
 | 39 | +    scaling_factor = 4  | 
 | 40 | +    per_dimension_factor = scaling_factor^(1 / length(n_points_per_dimension))  | 
 | 41 | +    sizes = [round.(Int, n_points_per_dimension .* per_dimension_factor^(iter - 1))  | 
 | 42 | +             for iter in 1:iterations]  | 
 | 43 | + | 
 | 44 | +    n_particles = prod.(sizes)  | 
 | 45 | +    times = zeros(iterations, length(neighborhood_searches_names))  | 
 | 46 | + | 
 | 47 | +    for iter in 1:iterations  | 
 | 48 | +        coordinates = point_cloud(sizes[iter], seed = seed,  | 
 | 49 | +                                  perturbation_factor_position = perturbation_factor_position)  | 
 | 50 | + | 
 | 51 | +        search_radius = 3.0  | 
 | 52 | + | 
 | 53 | +        neighborhood_searches = [  | 
 | 54 | +            TrivialNeighborhoodSearch{size(coordinates, 1)}(search_radius,  | 
 | 55 | +                                                            axes(coordinates, 2)),  | 
 | 56 | +            GridNeighborhoodSearch{size(coordinates, 1)}(search_radius,  | 
 | 57 | +                                                         size(coordinates, 2)),  | 
 | 58 | +        ]  | 
 | 59 | + | 
 | 60 | +        for i in eachindex(neighborhood_searches)  | 
 | 61 | +            neighborhood_search = neighborhood_searches[i]  | 
 | 62 | +            initialize!(neighborhood_search, coordinates, coordinates)  | 
 | 63 | + | 
 | 64 | +            time = benchmark(neighborhood_search, coordinates, parallel = parallel)  | 
 | 65 | +            times[iter, i] = time  | 
 | 66 | +            time_string = BenchmarkTools.prettytime(time * 1e9)  | 
 | 67 | +            println("$(neighborhood_searches_names[i])")  | 
 | 68 | +            println("with $(join(sizes[iter], "x")) = $(prod(sizes[iter])) particles finished in $time_string\n")  | 
 | 69 | +        end  | 
 | 70 | +    end  | 
 | 71 | + | 
 | 72 | +    plot(n_particles, times,  | 
 | 73 | +         xaxis = :log, yaxis = :log,  | 
 | 74 | +         xticks = (n_particles, n_particles),  | 
 | 75 | +         xlabel = "#particles", ylabel = "Runtime [s]",  | 
 | 76 | +         legend = :outerright, size = (750, 400), dpi = 200,  | 
 | 77 | +         label = neighborhood_searches_names, title = title)  | 
 | 78 | +end  | 
0 commit comments