Example showing how to use multigrid and compare with an analytic solution. A standard 5-point Laplacian is used.
1 #include "../src/cpp_macros.h"
12 integer,
parameter :: box_size = 16
13 integer,
parameter :: n_iterations = 10
14 integer :: domain_size(NDIM)
15 real(dp) :: domain_len(NDIM)
24 type(ref_info_t) :: refine_info
26 real(dp) :: residu(2), anal_err(2)
27 character(len=100) :: fname
30 integer :: count_rate,t_start,t_end
32 print *,
"Running poisson_basic_" // dimname
33 print *,
"Number of threads", af_get_max_threads()
40 call gauss_init(gs, [1.0_dp, 1.0_dp], [0.04_dp, 0.04_dp], &
41 reshape([dtimes(0.1_dp), &
42 dtimes(0.75_dp)], [ndim,2]))
46 call af_add_cc_variable(tree,
"phi", ix=i_phi)
47 call af_add_cc_variable(tree,
"sol", ix=i_sol)
48 call af_add_cc_variable(tree,
"rhs", ix=i_rhs)
49 call af_add_cc_variable(tree,
"err", ix=i_err)
50 call af_add_cc_variable(tree,
"tmp", ix=i_tmp)
51 call af_add_cc_variable(tree,
"Dx", ix=i_gradx)
52 call af_add_cc_variable(tree,
"eDx", ix=i_egradx)
54 domain_len(1) = 3.0_dp
55 domain_len(2:) = 1.0_dp
56 domain_size(1) = 3 * box_size
57 domain_size(2:) = box_size
66 call system_clock(t_start, count_rate)
70 call af_loop_box(tree, set_initial_condition)
76 call af_adjust_refinement(tree, refine_routine, refine_info)
79 if (refine_info%n_add == 0)
exit
82 call system_clock(t_end, count_rate)
84 write(*,
"(A,Es10.3,A)")
" Wall-clock time generating AMR grid: ", &
85 (t_end-t_start) / real(count_rate,dp),
" seconds"
87 call af_print_info(tree)
94 mg%sides_bc => sides_bc
99 call mg_init(tree, mg)
101 print *,
"Multigrid iteration | max residual | max error"
102 call system_clock(t_start, count_rate)
104 do mg_iter = 1, n_iterations
108 call mg_fas_fmg(tree, mg, set_residual=.true., have_guess=(mg_iter>1))
111 call af_loop_box(tree, set_error)
114 call af_tree_min_cc(tree, i_tmp, residu(1))
115 call af_tree_max_cc(tree, i_tmp, residu(2))
116 call af_tree_min_cc(tree, i_err, anal_err(1))
117 call af_tree_max_cc(tree, i_err, anal_err(2))
118 write(*,
"(I8,13x,2(Es14.5))") mg_iter, maxval(abs(residu)), &
119 maxval(abs(anal_err))
124 write(fname,
"(A,I0)")
"output/poisson_basic_" // dimname //
"_", mg_iter
125 call af_write_silo(tree, trim(fname))
128 call system_clock(t_end, count_rate)
130 write(*,
"(A,I0,A,E10.3,A)") &
131 " Wall-clock time after ", n_iterations, &
132 " iterations: ", (t_end-t_start) / real(count_rate, dp), &
137 call af_destroy(tree)
143 subroutine refine_routine(box, cell_flags)
144 type(box_t),
intent(in) :: box
145 integer,
intent(out) :: cell_flags(DTIMES(box%n_cell))
147 real(dp) :: rr(NDIM), dr2, drhs
150 dr2 = maxval(box%dr)**2
153 rr = af_r_cc(box, [ijk])
157 drhs = dr2 * box%cc(ijk, i_rhs)
159 if (abs(drhs) > 1e-3_dp .and. box%lvl < 7 - ndim)
then
160 cell_flags(ijk) = af_do_ref
162 cell_flags(ijk) = af_keep_ref
165 end subroutine refine_routine
170 subroutine set_initial_condition(box)
171 type(box_t),
intent(inout) :: box
173 real(dp) :: rr(NDIM), grad(NDIM)
179 rr = af_r_cc(box, [ijk])
182 box%cc(ijk, i_rhs) = gauss_laplacian(gs, rr)
183 call gauss_gradient(gs, rr, grad)
184 box%cc(ijk, i_gradx) = grad(1)
185 box%cc(ijk, i_sol) = gauss_value(gs, rr)
187 end subroutine set_initial_condition
192 subroutine set_error(box)
193 type(box_t),
intent(inout) :: box
195 real(dp) :: rr(NDIM), gradx
200 rr = af_r_cc(box, [ijk])
201 box%cc(ijk, i_err) = box%cc(ijk, i_phi) - gauss_value(gs, rr)
203 gradx = 0.5_dp * (box%cc(i+1, i_phi) - &
204 box%cc(i-1, i_phi)) / box%dr(1)
206 gradx = 0.5_dp * (box%cc(i+1, j, i_phi) - &
207 box%cc(i-1, j, i_phi)) / box%dr(1)
209 gradx = 0.5_dp * (box%cc(i+1, j, k, i_phi) - &
210 box%cc(i-1, j, k, i_phi)) / box%dr(1)
212 box%cc(ijk, i_egradx) = gradx - box%cc(ijk, i_gradx)
215 end subroutine set_error
219 subroutine sides_bc(box, nb, iv, coords, bc_val, bc_type)
220 type(box_t),
intent(in) :: box
221 integer,
intent(in) :: nb
222 integer,
intent(in) :: iv
223 real(dp),
intent(in) :: coords(NDIM, box%n_cell**(NDIM-1))
224 real(dp),
intent(out) :: bc_val(box%n_cell**(NDIM-1))
225 integer,
intent(out) :: bc_type
229 bc_type = af_bc_dirichlet
232 do n = 1, box%n_cell**(ndim-1)
233 bc_val(n) = gauss_value(gs, coords(:, n))
235 end subroutine sides_bc
237 end program poisson_basic
Module which contains all Afivo modules, so that a user does not have to include them separately.
This module can be used to construct solutions consisting of one or more Gaussians.