Afivo  0.3
particles_to_grid_2d.f90
1 
5 program particles_to_grid_2d
6  use m_a2_all
7 
8  implicit none
9 
10  integer, parameter :: box_size = 8
11  integer, parameter :: i_phi = 1
12  integer, parameter :: n_particles = 10*1000*1000
13  real(dp), parameter :: domain_len = 1.0_dp
14  real(dp), parameter :: r_min(2) = -0.5_dp * domain_len
15  real(dp), parameter :: dr = domain_len / box_size
16  real(dp), allocatable :: coordinates(:, :), weights(:)
17 
18  type(a2_t) :: tree
19  type(ref_info_t) :: refine_info
20  integer :: n, id, ix_list(2, 1)
21  integer :: count_rate, t_start, t_end
22  real(dp) :: sum_density
23 
24  print *, "Running particles_to_grid_2d"
25  print *, "Number of threads", af_get_max_threads()
26 
27  ! Initialize tree
28  call a2_init(tree, & ! Tree to initialize
29  box_size, & ! A box contains box_size**DIM cells
30  n_var_cell=1, & ! Number of cell-centered variables
31  n_var_face=0, & ! Number of face-centered variables
32  dr=dr, & ! Distance between cells on base level
33  cc_names=["phi"], & ! Variable names
34  r_min=r_min)
35 
36  ! Set up geometry
37  id = 1
38  ix_list(:, id) = 1 ! Set index of box
39 
40  ! Create the base mesh, using the box indices and their neighbor information
41  call a2_set_base(tree, 1, ix_list)
42 
43  call a2_set_cc_methods(tree, i_phi, a2_bc_neumann_zero, a2_gc_interp)
44 
45  do
46  call a2_adjust_refinement(tree, refine_routine, refine_info, 0)
47  if (refine_info%n_add == 0) exit
48  end do
49 
50  allocate(coordinates(2, n_particles))
51  allocate(weights(n_particles))
52 
53  call random_number(coordinates(:, :))
54  weights(:) = 1.0_dp
55 
56  do n = 1, n_particles
57  coordinates(:, n) = (coordinates(:, n) - 0.5_dp) * domain_len
58  ! coordinates(:, n) = (sqrt(coordinates(:, n)) - 0.5_dp) * domain_len
59  end do
60 
61  call system_clock(t_start, count_rate)
62  call a2_particles_to_grid(tree, i_phi, coordinates, weights, n_particles, 0)
63  call system_clock(t_end, count_rate)
64  call a2_write_silo(tree, "particles_to_grid_2d_0", 1, 0.0_dp, dir="output")
65  call a2_tree_sum_cc(tree, i_phi, sum_density)
66  print *, "zeroth order: ", (t_end-t_start) / real(count_rate, dp), " seconds"
67  print *, "integrated density: ", sum_density
68 
69  call a2_tree_clear_cc(tree, i_phi)
70 
71  call system_clock(t_start, count_rate)
72  call a2_particles_to_grid(tree, i_phi, coordinates, weights, n_particles, 1)
73  call system_clock(t_end, count_rate)
74  call a2_write_silo(tree, "particles_to_grid_2d_1", 1, 0.0_dp, dir="output")
75  call a2_tree_sum_cc(tree, i_phi, sum_density)
76  print *, "first order: ", (t_end-t_start) / real(count_rate, dp), " seconds"
77  print *, "integrated density: ", sum_density
78 
79 contains
80 
81  subroutine refine_routine(box, cell_flags)
82  type(box2_t), intent(in) :: box
83  integer, intent(out) :: cell_flags(box%n_cell, box%n_cell)
84 
85  if (all(box%r_min < 0.0_dp) .and. box%lvl <= 5) then
86  cell_flags(:, :) = af_do_ref
87  else
88  cell_flags(:, :) = af_keep_ref
89  end if
90  end subroutine refine_routine
91 
92 end program particles_to_grid_2d
Module which contains all Afivo modules, so that a user does not have to include them separately...
Definition: m_a2_all.f90:4