5 program poisson_cyl_dielectric
11 integer,
parameter :: box_size = 8
12 integer,
parameter :: n_iterations = 10
20 type(ref_info_t) :: ref_info
22 real(dp) :: residu(2), anal_err(2)
23 character(len=100) :: fname
26 integer :: count_rate,t_start, t_end
28 print *,
"Running poisson_cyl_dielectric"
29 print *,
"Number of threads", af_get_max_threads()
32 call gauss_init(gs, [1.0_dp], [0.1_dp], &
33 reshape([0.0_dp, 0.25_dp], [2,1]))
35 call af_add_cc_variable(tree,
"phi", ix=i_phi)
36 call af_add_cc_variable(tree,
"rhs", ix=i_rhs)
37 call af_add_cc_variable(tree,
"err", ix=i_err)
38 call af_add_cc_variable(tree,
"tmp", ix=i_tmp)
39 call af_add_cc_variable(tree,
"eps", ix=i_eps)
45 [box_size, box_size], &
48 call af_print_info(tree)
50 call system_clock(t_start, count_rate)
53 call af_loop_box(tree, set_init_cond)
56 call af_adjust_refinement(tree, ref_routine, ref_info)
59 if (ref_info%n_add == 0)
exit
61 call system_clock(t_end, count_rate)
63 write(*,
"(A,Es10.3,A)")
" Wall-clock time generating AMR grid: ", &
64 (t_end-t_start) / real(count_rate,dp),
" seconds"
66 call af_print_info(tree)
73 mg%sides_bc => sides_bc
79 call mg_init(tree, mg)
81 print *,
"Multigrid iteration | max residual | max error"
82 call system_clock(t_start, count_rate)
84 do mg_iter = 1, n_iterations
88 call mg_fas_fmg(tree, mg, .true., mg_iter>1)
91 call af_loop_box(tree, set_err)
94 call af_tree_min_cc(tree, i_tmp, residu(1))
95 call af_tree_max_cc(tree, i_tmp, residu(2))
96 call af_tree_min_cc(tree, i_err, anal_err(1))
97 call af_tree_max_cc(tree, i_err, anal_err(2))
98 write(*,
"(I8,2Es14.5)") mg_iter, maxval(abs(residu)), &
101 write(fname,
"(A,I0)")
"output/poisson_cyl_dielectric_", mg_iter
102 call af_write_silo(tree, trim(fname))
104 call system_clock(t_end, count_rate)
106 write(*,
"(A,I0,A,E10.3,A)") &
107 " Wall-clock time after ", n_iterations, &
108 " iterations: ", (t_end-t_start) / real(count_rate, dp), &
113 call af_destroy(tree)
118 subroutine ref_routine(box, cell_flags)
119 type(box_t),
intent(in) :: box
120 integer,
intent(out) :: cell_flags(box%n_cell, box%n_cell)
125 dr2 = maxval(box%dr)**2
130 crv = dr2 * abs(box%cc(i, j, i_rhs)) / box%cc(i, j, i_eps)
133 if (crv > 1.0e-3_dp)
then
134 cell_flags(i, j) = af_do_ref
136 cell_flags(i, j) = af_keep_ref
140 end subroutine ref_routine
143 subroutine set_init_cond(box)
144 type(box_t),
intent(inout) :: box
146 real(dp) :: rz(2), grad(2), qbnd, tmp
149 box%cc(:, :, i_phi) = 0
153 rz = af_r_cc(box, [i,j])
156 if (rz(1) < 0.5_dp .and. rz(2) < 0.5_dp)
then
157 box%cc(i, j, i_eps) = 100.0_dp
159 box%cc(i, j, i_eps) = 1.0_dp
163 box%cc(i, j, i_rhs) = gauss_laplacian_cyl(gs, rz) * box%cc(i, j, i_eps)
171 rz = af_rr_cc(box, [i + 0.5_dp, real(j, dp)])
174 call gauss_gradient(gs, rz, grad)
175 qbnd = (box%cc(i+1, j, i_eps) - box%cc(i, j, i_eps)) * &
179 tmp = box%cc(i+1, j, i_eps) / &
180 (box%cc(i, j, i_eps) + box%cc(i+1, j, i_eps))
181 box%cc(i+1, j, i_rhs) = box%cc(i+1, j, i_rhs) + tmp * qbnd
182 box%cc(i, j, i_rhs) = box%cc(i, j, i_rhs) + (1-tmp) * qbnd
189 rz = af_rr_cc(box, [real(i, dp), j + 0.5_dp])
192 call gauss_gradient(gs, rz, grad)
193 qbnd = (box%cc(i, j+1, i_eps) - box%cc(i, j, i_eps)) * &
197 tmp = box%cc(i, j+1, i_eps) / &
198 (box%cc(i, j, i_eps) + box%cc(i, j+1, i_eps))
199 box%cc(i, j+1, i_rhs) = box%cc(i, j+1, i_rhs) + tmp * qbnd
200 box%cc(i, j, i_rhs) = box%cc(i, j, i_rhs) + (1-tmp) * qbnd
203 end subroutine set_init_cond
206 subroutine set_err(box)
207 type(box_t),
intent(inout) :: box
214 rz = af_r_cc(box, [i,j])
215 box%cc(i, j, i_err) = box%cc(i, j, i_phi) - gauss_value(gs, rz)
218 end subroutine set_err
221 subroutine sides_bc(box, nb, iv, coords, bc_val, bc_type)
222 type(box_t),
intent(in) :: box
223 integer,
intent(in) :: nb
224 integer,
intent(in) :: iv
225 real(dp),
intent(in) :: coords(NDIM, box%n_cell**(NDIM-1))
226 real(dp),
intent(out) :: bc_val(box%n_cell**(NDIM-1))
227 integer,
intent(out) :: bc_type
230 if (nb == af_neighb_lowx)
then
232 bc_type = af_bc_neumann
236 bc_type = af_bc_dirichlet
239 do n = 1, box%n_cell**(ndim-1)
240 bc_val(n) = gauss_value(gs, coords(:, n))
243 end subroutine sides_bc
245 end program poisson_cyl_dielectric
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.