afivo-streamer 1.1
1D/2D/3D streamer simulations with AMR
Loading...
Searching...
No Matches
m_field.f90
Go to the documentation of this file.
1!> Module to compute electric fields
2module m_field
3#include "../afivo/src/cpp_macros.h"
4 use m_af_all
5 use m_types
6 use m_streamer
7
8 implicit none
9 private
10
11 integer, parameter :: scalar_voltage = 1
12 integer, parameter :: tabulated_voltage = 2
13
14 !> How the electric field or voltage is specified
15 integer :: field_given_by = -1
16
17 !> List of times
18 real(dp), allocatable :: field_table_times(:)
19
20 !> List of voltages
21 real(dp), allocatable :: field_table_values(:)
22
23 !> Linear rise time of field (s)
24 real(dp) :: field_rise_time = 0.0_dp
25
26 !> Pulse width excluding rise and fall time
27 real(dp) :: field_pulse_width = huge_real
28
29 !> Number of voltage pulses
30 integer :: field_num_pulses = 1
31
32 !> Time of one complete voltage pulse
33 real(dp), public, protected :: field_pulse_period = huge_real
34
35 !> The (initial) vertical applied electric field
36 real(dp) :: field_amplitude = undefined_real
37
38 !> The applied voltage (vertical direction)
39 real(dp) :: field_voltage = undefined_real
40
41 !> Whether electrode 1 is grounded or at the applied voltage
42 logical :: field_electrode_grounded = .false.
43
44 !> Whether electrode 2 is grounded or at the applied voltage
45 logical :: field_electrode2_grounded = .false.
46
47 !> Electrode 1: first relative coordinate
48 real(dp) :: rod_r0(ndim) = -1.0e100_dp
49
50 !> Electrode 1: second relative coordinate
51 real(dp) :: rod_r1(ndim) = -1.0e100_dp
52
53 !> Electrode 2: first relative coordinate
54 real(dp) :: rod2_r0(ndim) = -1.0e100_dp
55
56 !> Electrode 2: second relative coordinate
57 real(dp) :: rod2_r1(ndim) = -1.0e100_dp
58
59 !> Electrode 1 radius (in m)
60 real(dp) :: rod_radius = -1.0e100_dp
61
62 !> Electrode 2 radius (in m)
63 real(dp) :: rod2_radius = -1.0e100_dp
64
65 !> Electrode 1: fraction of conical part (if conical)
66 real(dp) :: cone_length_frac = -1.0e100_dp
67
68 !> Electrode 2: fraction of conical part (if conical)
69 real(dp) :: cone2_length_frac = -1.0e100_dp
70
71 !> Electrode 1: tip radius (if conical)
72 real(dp) :: cone_tip_radius = -1.0e100_dp
73
74 !> Electrode 2: tip radius (if conical)
75 real(dp) :: cone2_tip_radius = -1.0e100_dp
76
77 ! Internal variables
78
79 !> Electrode 1: 'origin' of spherical tip (if conical)
80 real(dp) :: cone_tip_center(ndim)
81 !> Electrode 2: 'origin' of spherical tip (if conical)
82 real(dp) :: cone2_tip_center(ndim)
83
84 !> Electrode 1: radius of curvature of spherical tip (if conical)
85 real(dp) :: cone_tip_r_curvature
86 !> Electrode 2: radius of curvature of spherical tip (if conical)
87 real(dp) :: cone2_tip_r_curvature
88
89 !> The current applied voltage
90 real(dp), public, protected :: current_voltage = 0.0_dp
91
92 character(string_len) :: field_bc_type = "homogeneous"
93
94 public :: field_initialize
95 public :: field_compute
96 public :: field_set_rhs
97 public :: field_set_voltage
98
99 public :: field_bc_homogeneous
100 public :: field_from_potential
101 public :: field_compute_energy
102 public :: field_get_e_vector
103
104contains
105
106 !> Initialize this module
107 subroutine field_initialize(tree, cfg, mg)
108 use m_config
109 use m_table_data
111 type(af_t), intent(inout) :: tree
112 type(cfg_t), intent(inout) :: cfg !< Settings
113 type(mg_t), intent(inout) :: mg !< Multigrid option struct
114 character(len=string_len) :: given_by, user_value
115 character(len=string_len) :: electrode_type
116 integer :: first_blank
117
118 ! This is for backward compatibility
119 call cfg_add_get(cfg, "field_amplitude", field_amplitude, &
120 "The (initial) vertical applied electric field (V/m)")
121
122 given_by = undefined_str
123 call cfg_add_get(cfg, "field_given_by", given_by, &
124 "How the electric field or voltage is specified")
125
126 ! Split given_by string
127 given_by = adjustl(given_by)
128 first_blank = index(given_by, " ")
129 user_value = adjustl(given_by(first_blank:))
130 given_by = given_by(1:first_blank-1)
131
132 select case (given_by)
133 case ("voltage")
134 field_given_by = scalar_voltage
135 read(user_value, *) field_voltage
136 case ("field")
137 field_given_by = scalar_voltage
138 read(user_value, *) field_voltage
139 ! Convert to a voltage
140 field_voltage = -st_domain_len(ndim) * field_voltage
141 case ("voltage_table")
142 field_given_by = tabulated_voltage
143
144 ! Read in the table
145 call table_from_file(trim(user_value), "voltage_vs_time", &
146 field_table_times, field_table_values)
147 case ("field_table")
148 field_given_by = tabulated_voltage
149
150 ! Read in the table
151 call table_from_file(trim(user_value), "field_vs_time", &
152 field_table_times, field_table_values)
153 ! Convert to a voltage
154 field_table_values = -st_domain_len(ndim) * field_table_values
155 case (undefined_str)
156 if (field_amplitude <= undefined_real) then
157 error stop "field_amplitude not specified"
158 else
159 print *, "Warning: field_amplitude is deprecated, use field_given_by"
160 field_given_by = scalar_voltage
161 field_voltage = -st_domain_len(ndim) * field_amplitude
162 end if
163 case default
164 print *, "field_given_by value: ", trim(given_by), ", options are:"
165 print *, "1. voltage <value in V>"
166 print *, "2. field <value in V/m>"
167 print *, "3. voltage_table <filename>"
168 print *, "4. field_table <filename>"
169 error stop "Unknown field_given_by value"
170 end select
171
172 call cfg_add_get(cfg, "field_rise_time", field_rise_time, &
173 "Linear rise time of field (s)")
174 call cfg_add_get(cfg, "field_pulse_width", field_pulse_width, &
175 "Pulse width excluding rise and fall time (s)")
176 call cfg_add_get(cfg, "field_num_pulses", field_num_pulses, &
177 "Number of voltage pulses (default: 1)")
178 call cfg_add_get(cfg, "field_pulse_period", field_pulse_period, &
179 "Time of one complete voltage pulse (s)")
180
181 if (field_pulse_width < huge_real .and. field_rise_time <= 0) &
182 error stop "Set field_rise_time when using field_pulse_width"
183
184 if (field_num_pulses > 1) then
186 error stop "field_num_pulses > 1 requires field_pulse_period"
187 if (field_pulse_width >= huge_real) &
188 error stop "field_num_pulses > 1 requires field_pulse_width"
189 if (field_pulse_width + 2 * field_rise_time > field_pulse_period) &
190 error stop "field_pulse_period shorter than one full pulse"
191 end if
192
193 call cfg_add_get(cfg, "field_bc_type", field_bc_type, &
194 "Boundary condition for electric potential")
195
196 !< [electrode_settings]
197 call cfg_add_get(cfg, "field_electrode_grounded", field_electrode_grounded, &
198 "Whether electrode 1 is grounded or at the applied voltage")
199 call cfg_add_get(cfg, "field_electrode2_grounded", field_electrode2_grounded, &
200 "Whether electrode 2 is grounded or at the applied voltage")
201 call cfg_add_get(cfg, "field_rod_r0", rod_r0, &
202 "Electrode 1: first relative coordinate")
203 call cfg_add_get(cfg, "field_rod_r1", rod_r1, &
204 "Electrode 1: second relative coordinate")
205 call cfg_add_get(cfg, "field_rod2_r0", rod2_r0, &
206 "Electrode 2: first relative coordinate")
207 call cfg_add_get(cfg, "field_rod2_r1", rod2_r1, &
208 "Electrode 2: second relative coordinate")
209 call cfg_add_get(cfg, "field_rod_radius", rod_radius, &
210 "Electrode 1 radius (in m)")
211 call cfg_add_get(cfg, "field_rod2_radius", rod2_radius, &
212 "Electrode 2 radius (in m)")
213 call cfg_add_get(cfg, "cone_tip_radius", cone_tip_radius, &
214 "Electrode 1: tip radius (if conical)")
215 call cfg_add_get(cfg, "cone_length_frac", cone_length_frac, &
216 "Electrode 1: fraction of conical part (if conical)")
217 call cfg_add_get(cfg, "cone2_tip_radius", cone2_tip_radius, &
218 "Electrode 2: tip radius (if conical)")
219 call cfg_add_get(cfg, "cone2_length_frac", cone2_length_frac, &
220 "Electrode 2: fraction of conical part (if conical)")
221
222 rod_r0 = st_domain_origin + rod_r0 * st_domain_len
223 rod_r1 = st_domain_origin + rod_r1 * st_domain_len
224 rod2_r0 = st_domain_origin + rod2_r0 * st_domain_len
225 rod2_r1 = st_domain_origin + rod2_r1 * st_domain_len
226
227 electrode_type = "rod"
228 call cfg_add_get(cfg, "field_electrode_type", electrode_type, &
229 "Electrode: sphere, rod, rod_cone_top, rod_rod, sphere_rod, user")
230 !< [electrode_settings]
231
232 if (associated(user_potential_bc)) then
233 mg%sides_bc => user_potential_bc
234 else
235 ! Use one of the predefined boundary conditions
236 select case (field_bc_type)
237 case ("homogeneous")
238 mg%sides_bc => field_bc_homogeneous
239 case ("neumann")
240 mg%sides_bc => field_bc_neumann
241 case ("all_neumann")
242 mg%sides_bc => field_bc_all_neumann
243 case default
244 error stop "field_bc_select error: invalid condition"
245 end select
246 end if
247
248 ! Set the multigrid options. First define the variables to use
249 mg%i_phi = i_phi
250 mg%i_tmp = i_tmp
251 mg%i_rhs = i_rhs
252
253 if (st_use_dielectric) tree%mg_i_eps = i_eps
254
255 if (st_use_electrode) then
256 select case (electrode_type)
257 case ("sphere")
258 ! A single spherical electrode
259 if (any(rod_r0 <= -1.0e10_dp)) &
260 error stop "field_rod_r0 not set correctly"
261 if (rod_radius <= 0) &
262 error stop "field_rod_radius not set correctly"
263 mg%lsf => sphere_lsf
264 case ("rod")
265 ! A single rod electrode with a semi-spherical cap
266 call check_general_electrode_parameters()
267 mg%lsf => rod_lsf
268 case ("rod_cone_top")
269 ! A single rod-shaped electrode with a conical top
270 call check_general_electrode_parameters()
271 if (cone_tip_radius <= 0 .or. cone_tip_radius > rod_radius) &
272 error stop "cone_tip_radius should be smaller than rod radius"
273 if (cone_length_frac < 0 .or. cone_length_frac > 1) &
274 error stop "cone_length_frac not set correctly"
275
276 call get_conical_rod_properties(rod_r0, rod_r1, rod_radius, &
277 cone_tip_radius, cone_tip_center, cone_tip_r_curvature)
278
279 mg%lsf => conical_rod_lsf
280 case ("rod_rod")
281 ! Two rod electrodes with semi-spherical caps
282 call check_general_electrode_parameters()
283
284 if (any(rod2_r0 <= -1.0e10_dp)) &
285 error stop "field_rod2_r0 not set correctly"
286 if (any(rod2_r1 <= -1.0e10_dp)) &
287 error stop "field_rod2_r1 not set correctly"
288 if (rod2_radius <= 0) &
289 error stop "field_rod2_radius not set correctly"
290
291 mg%lsf => rod_rod_lsf
292
293 ! Provide a function to set the voltage on the electrodes
294 mg%lsf_boundary_function => rod_rod_get_potential
295 case ("sphere_rod")
296 ! Sphere and rod electrode with semi-spherical cap
297 call check_general_electrode_parameters()
298
299 if (any(rod2_r0 <= -1.0e10_dp)) &
300 error stop "field_rod2_r0 not set correctly"
301 if (any(rod2_r1 <= -1.0e10_dp)) &
302 error stop "field_rod2_r1 not set correctly"
303 if (rod2_radius <= 0) &
304 error stop "field_rod2_radius not set correctly"
305
306 mg%lsf => sphere_rod_lsf
307 mg%lsf_boundary_function => sphere_rod_get_potential
308 case ("two_rod_cone_electrodes")
309 ! Two rod-shaped electrodes with conical tops (for now assumed to have
310 ! the same shape)
311 call check_general_electrode_parameters()
312 if (any(rod2_r0 <= -1.0e10_dp)) &
313 error stop "field_rod2_r0 not set correctly"
314 if (any(rod2_r1 <= -1.0e10_dp)) &
315 error stop "field_rod2_r1 not set correctly"
316 if (rod2_radius <= 0) &
317 error stop "field_rod2_radius not set correctly"
318 if (cone_tip_radius <= 0 .or. cone_tip_radius > rod_radius) &
319 error stop "cone tip radius should be smaller than rod radius"
320 if (cone2_tip_radius <= 0 .or. cone2_tip_radius > rod2_radius) &
321 error stop "cone2 tip radius should be smaller than rod2 radius"
322 if (cone_length_frac < 0 .or. cone_length_frac > 1) &
323 error stop "cone_length_frac not set correctly"
324 if (cone2_length_frac < 0 .or. cone2_length_frac > 1) &
325 error stop "cone2_length_frac not set correctly"
326
327 call get_conical_rod_properties(rod_r0, rod_r1, rod_radius, &
328 cone_tip_radius, cone_tip_center, cone_tip_r_curvature)
329 call get_conical_rod_properties(rod2_r0, rod2_r1, rod2_radius, &
330 cone2_tip_radius, cone2_tip_center, cone2_tip_r_curvature)
331
332 mg%lsf => two_conical_rods_lsf
333
334 ! Provide a function to set the voltage on the electrodes
335 mg%lsf_boundary_function => two_conical_rods_get_potential
336 case ("user")
337 if (.not. associated(user_lsf)) then
338 error stop "user_lsf not set"
339 else
340 mg%lsf => user_lsf
341 end if
342
343 if (associated(user_lsf_bc)) then
344 mg%lsf_boundary_function => user_lsf_bc
345 end if
346 case default
347 print *, "Electrode types: sphere, rod, rod_cone_top, rod_rod, user"
348 error stop "Invalid electrode type"
349 end select
350
351 call af_set_cc_methods(tree, i_lsf, funcval=set_lsf_box)
352 tree%mg_i_lsf = i_lsf
353
354 mg%lsf_dist => mg_lsf_dist_gss
355
356 if (rod_radius <= 0) then
357 error stop "set field_rod_radius to smallest length scale of electrode"
358 end if
359 mg%lsf_length_scale = rod_radius
360 end if
361
362 call af_set_cc_methods(tree, i_electric_fld, &
363 af_bc_neumann_zero, af_gc_interp)
364
365 end subroutine field_initialize
366
367 subroutine check_general_electrode_parameters()
368 if (any(rod_r0 <= -1.0e10_dp)) &
369 error stop "field_rod_r0 not set correctly"
370 if (any(rod_r1 <= -1.0e10_dp)) &
371 error stop "field_rod_r1 not set correctly"
372 if (rod_radius <= 0) &
373 error stop "field_rod_radius not set correctly"
374 end subroutine check_general_electrode_parameters
375
376 subroutine field_set_rhs(tree, s_in)
378 use m_chemistry
379 use m_dielectric
380 type(af_t), intent(inout) :: tree
381 integer, intent(in) :: s_in
382 real(dp), parameter :: fac = -uc_elem_charge / uc_eps0
383 real(dp) :: q
384 integer :: lvl, i, id, nc, n, ix
385
386 nc = tree%n_cell
387
388 ! Set the source term (rhs)
389 !$omp parallel private(lvl, i, id, n, ix, q)
390 do lvl = 1, tree%highest_lvl
391 !$omp do
392 do i = 1, size(tree%lvls(lvl)%leaves)
393 id = tree%lvls(lvl)%leaves(i)
394
395 tree%boxes(id)%cc(dtimes(:), i_rhs) = 0.0_dp
396
397 do n = 1, size(charged_species_itree)
398 ix = charged_species_itree(n) + s_in
399 q = charged_species_charge(n) * fac
400
401 tree%boxes(id)%cc(dtimes(:), i_rhs) = &
402 tree%boxes(id)%cc(dtimes(:), i_rhs) + &
403 q * tree%boxes(id)%cc(dtimes(:), ix)
404 end do
405 end do
406 !$omp end do nowait
407 end do
408 !$omp end parallel
409
410 if (st_use_dielectric) then
411 call surface_surface_charge_to_rhs(tree, diel, i_surf_dens, i_rhs, fac)
412 end if
413
414 end subroutine field_set_rhs
415
416 !> Compute electric field on the tree. First perform multigrid to get electric
417 !> potential, then take numerical gradient to geld field.
418 subroutine field_compute(tree, mg, s_in, time, have_guess)
419 use m_chemistry
420 type(af_t), intent(inout) :: tree
421 type(mg_t), intent(inout) :: mg ! Multigrid option struct
422 integer, intent(in) :: s_in
423 real(dp), intent(in) :: time
424 logical, intent(in) :: have_guess
425 integer :: i
426 real(dp) :: max_rhs, residual_threshold, conv_fac
427 real(dp) :: residual_ratio
428 integer, parameter :: max_initial_iterations = 100
429 real(dp), parameter :: max_residual = 1e8_dp
430 real(dp), parameter :: min_residual = 1e-6_dp
431 real(dp) :: residuals(max_initial_iterations)
432
433 call field_set_rhs(tree, s_in)
434 call field_set_voltage(tree, time)
435
436 call af_tree_maxabs_cc(tree, mg%i_rhs, max_rhs)
437
438 ! With an electrode, the initial convergence testing should be less strict
439 if (st_use_electrode) then
440 conv_fac = 1e-8_dp
441 else
442 conv_fac = 1e-10_dp
443 end if
444
445 ! Set threshold based on rhs and on estimate of round-off error, given by
446 ! delta phi / dx^2 = (phi/L * dx)/dx^2
447 ! Note that we use min_residual in case max_rhs and current_voltage are zero
448 residual_threshold = max(min_residual, &
450 conv_fac * abs(current_voltage)/(st_domain_len(ndim) * af_min_dr(tree)))
451
452 if (st_use_electrode) then
453 if (field_electrode_grounded) then
454 mg%lsf_boundary_value = 0.0_dp
455 else
456 mg%lsf_boundary_value = current_voltage
457 end if
458 end if
459
460 ! Perform a FMG cycle when we have no guess
461 if (.not. have_guess) then
462 do i = 1, max_initial_iterations
463 call mg_fas_fmg(tree, mg, .true., .true.)
464 call af_tree_maxabs_cc(tree, mg%i_tmp, residuals(i))
465
466 if (residuals(i) < residual_threshold) then
467 exit
468 else if (i > 2) then
469 ! Check if the residual is not changing much anymore, and if it is
470 ! small enough, in which case we assume convergence
471 residual_ratio = minval(residuals(i-2:i)) / &
472 maxval(residuals(i-2:i))
473 if (residual_ratio < 2.0_dp .and. residual_ratio > 0.5_dp &
474 .and. residuals(i) < max_residual) exit
475 end if
476 end do
477
478 ! Check for convergence
479 if (i == max_initial_iterations + 1) then
480 print *, "Iteration residual"
481 do i = 1, max_initial_iterations
482 write(*, "(I4,E18.2)") i, residuals(i)
483 end do
484 print *, "Maybe increase multigrid_max_rel_residual?"
485 error stop "No convergence in initial field computation"
486 end if
487 end if
488
489 ! Perform cheaper V-cycles
491 call mg_fas_vcycle(tree, mg, .true.)
492 call af_tree_maxabs_cc(tree, mg%i_tmp, residuals(i))
493 if (residuals(i) < residual_threshold) exit
494 end do
495
496 call field_from_potential(tree, mg)
497
498 end subroutine field_compute
499
500 !> Compute field from potential
501 subroutine field_from_potential(tree, mg)
503 use m_dielectric
504 type(af_t), intent(inout) :: tree
505 type(mg_t), intent(in) :: mg ! Multigrid option struct
506
507 if (st_use_dielectric) then
508 call mg_compute_phi_gradient(tree, mg, electric_fld, -1.0_dp)
509 call surface_correct_field_fc(tree, diel, i_surf_dens, &
511 call mg_compute_field_norm(tree, electric_fld, i_electric_fld)
512 else
513 call mg_compute_phi_gradient(tree, mg, electric_fld, -1.0_dp, i_electric_fld)
514 end if
515
516 ! Set the field norm also in ghost cells
517 call af_gc_tree(tree, [i_electric_fld])
518 end subroutine field_from_potential
519
520 !> Set the current voltage
521 subroutine field_set_voltage(tree, time)
523 use m_lookup_table
525 type(af_t), intent(in) :: tree
526 real(dp), intent(in) :: time
527 real(dp) :: electric_fld, t, tmp
528
529 if (associated(user_field_amplitude)) then
532 return
533 end if
534
535 select case (field_given_by)
536 case (scalar_voltage)
537 current_voltage = 0.0_dp
538
539 if (time < field_pulse_period * field_num_pulses) then
540 t = modulo(time, field_pulse_period)
541
542 if (t < field_rise_time) then
543 current_voltage = field_voltage * (t/field_rise_time)
544 else if (t < field_pulse_width + field_rise_time) then
545 current_voltage = field_voltage
546 else
547 tmp = t - (field_pulse_width + field_rise_time)
548 current_voltage = field_voltage * max(0.0_dp, &
549 (1 - tmp/field_rise_time))
550 end if
551 end if
552 case (tabulated_voltage)
553 call lt_lin_interp_list(field_table_times, field_table_values, &
554 time, current_voltage)
555 end select
556 end subroutine field_set_voltage
557
558 !> Dirichlet boundary conditions for the potential in the last dimension,
559 !> Neumann zero boundary conditions in the other directions
560 subroutine field_bc_homogeneous(box, nb, iv, coords, bc_val, bc_type)
561 type(box_t), intent(in) :: box
562 integer, intent(in) :: nb
563 integer, intent(in) :: iv
564 real(dp), intent(in) :: coords(ndim, box%n_cell**(ndim-1))
565 real(dp), intent(out) :: bc_val(box%n_cell**(ndim-1))
566 integer, intent(out) :: bc_type
567
568 if (af_neighb_dim(nb) == ndim) then
569 if (af_neighb_low(nb)) then
570 bc_type = af_bc_dirichlet
571 bc_val = 0.0_dp
572 else
573 bc_type = af_bc_dirichlet
574 bc_val = current_voltage
575 end if
576 else
577 bc_type = af_bc_neumann
578 bc_val = 0.0_dp
579 end if
580 end subroutine field_bc_homogeneous
581
582 !> A Dirichlet zero and non-zero Neumann boundary condition for the potential
583 !> in the last dimension, Neumann zero boundary conditions in the other
584 !> directions
585 subroutine field_bc_neumann(box, nb, iv, coords, bc_val, bc_type)
586 type(box_t), intent(in) :: box
587 integer, intent(in) :: nb
588 integer, intent(in) :: iv
589 real(dp), intent(in) :: coords(ndim, box%n_cell**(ndim-1))
590 real(dp), intent(out) :: bc_val(box%n_cell**(ndim-1))
591 integer, intent(out) :: bc_type
592
593 if (af_neighb_dim(nb) == ndim) then
594 if (af_neighb_low(nb)) then
595 bc_type = af_bc_dirichlet
596 bc_val = 0.0_dp
597 else
598 bc_type = af_bc_neumann
599 bc_val = current_voltage/st_domain_len(ndim)
600 end if
601 else
602 bc_type = af_bc_neumann
603 bc_val = 0.0_dp
604 end if
605 end subroutine field_bc_neumann
606
607 !> All Neumann zero boundary conditions for the potential
608 subroutine field_bc_all_neumann(box, nb, iv, coords, bc_val, bc_type)
609 type(box_t), intent(in) :: box
610 integer, intent(in) :: nb
611 integer, intent(in) :: iv
612 real(dp), intent(in) :: coords(ndim, box%n_cell**(ndim-1))
613 real(dp), intent(out) :: bc_val(box%n_cell**(ndim-1))
614 integer, intent(out) :: bc_type
615
616 bc_type = af_bc_neumann
617 bc_val = 0.0_dp
618 end subroutine field_bc_all_neumann
619
620 ! This routine sets the level set function for a simple rod
621 subroutine set_lsf_box(box, iv)
622 type(box_t), intent(inout) :: box
623 integer, intent(in) :: iv
624 integer :: ijk, nc
625 real(dp) :: rr(ndim)
626
627 nc = box%n_cell
628 do kji_do(0,nc+1)
629 rr = af_r_cc(box, [ijk])
630 box%cc(ijk, iv) = mg%lsf(rr)
631 end do; close_do
632 end subroutine set_lsf_box
633
634 real(dp) function sphere_lsf(r)
635 real(dp), intent(in) :: r(ndim)
636 sphere_lsf = norm2(r - rod_r0) - rod_radius
637 end function sphere_lsf
638
639 real(dp) function rod_lsf(r)
640 use m_geometry
641 real(dp), intent(in) :: r(ndim)
642 rod_lsf = gm_dist_line(r, rod_r0, rod_r1, ndim) - rod_radius
643 end function rod_lsf
644
645 !> Compute several parameters for a conical rod
646 subroutine get_conical_rod_properties(r0, r1, &
647 rod_radius, tip_radius, cone_tip_center, cone_tip_r_curvature)
648 real(dp), intent(in) :: r0(ndim) !< Beginning of rod
649 real(dp), intent(in) :: r1(ndim) !< End of conical part
650 real(dp), intent(in) :: rod_radius !< Radius of rod
651 real(dp), intent(in) :: tip_radius !< Radius of curvature of tip
652 real(dp), intent(out) :: cone_tip_center(ndim)
653 real(dp), intent(out) :: cone_tip_r_curvature
654 real(dp) :: cone_angle, cone_length
655
656 ! Determine (half) the opening angle of the top cone, which goes from
657 ! rod_radius to tip_radius over cone_length
658 cone_length = cone_length_frac * norm2(r1 - r0)
659 cone_angle = atan((rod_radius - tip_radius) / cone_length)
660
661 ! We have a point on a sphere with coordinates of the form (R*cos(a),
662 ! R*sin(a)) = (tip_radius, y), so we can get R and subtract R sin(a) to
663 ! obtain the center of the sphere
664 cone_tip_r_curvature = tip_radius/cos(cone_angle)
665 cone_tip_center = r1 - sin(cone_angle) * &
666 cone_tip_r_curvature * (r1 - r0)/norm2(r1 - r0)
667 end subroutine get_conical_rod_properties
668
669 !> Helper function to compute lsf for a rod with a conical tip
670 pure function conical_rod_lsf_arg(r, r0, r1, cone_tip_center, &
671 rod_radius, tip_radius, cone_length_frac, r_curvature) result (lsf)
672 use m_geometry
673 real(dp), intent(in) :: r(ndim)
674 real(dp), intent(in) :: r0(ndim), r1(ndim), cone_tip_center(ndim)
675 real(dp), intent(in) :: rod_radius, tip_radius, cone_length_frac
676 real(dp), intent(in) :: r_curvature
677 real(dp) :: lsf
678 real(dp) :: dist_vec(ndim), frac, radius_at_height, tmp
679
680 ! Project onto line from r0 to r1
681 call gm_dist_vec_line(r, r0, r1, ndim, dist_vec, frac)
682
683 if (frac <= 1 - cone_length_frac) then
684 ! Cylindrical part
685 lsf = norm2(dist_vec) - rod_radius
686 else if (frac < 1.0_dp) then
687 ! Conical part
688 tmp = (1 - frac) / cone_length_frac ! between 0 and 1
689 radius_at_height = tip_radius + tmp * (rod_radius - tip_radius)
690 lsf = norm2(dist_vec) - radius_at_height
691 else
692 ! Spherical tip
693 lsf = norm2(r - cone_tip_center) - r_curvature
694 end if
695 end function conical_rod_lsf_arg
696
697 real(dp) function conical_rod_lsf(r)
698 real(dp), intent(in) :: r(ndim)
699 conical_rod_lsf = conical_rod_lsf_arg(r, rod_r0, rod_r1, cone_tip_center, &
700 rod_radius, cone_tip_radius, cone_length_frac, cone_tip_r_curvature)
701 end function conical_rod_lsf
702
703 !> Get lsf for two conical rods
704 real(dp) function two_conical_rods_lsf(r)
705 real(dp), intent(in) :: r(ndim)
706 real(dp) :: lsf_1, lsf_2
707
708 lsf_1 = conical_rod_lsf_arg(r, rod_r0, rod_r1, cone_tip_center, &
709 rod_radius, cone_tip_radius, cone_length_frac, cone_tip_r_curvature)
710 lsf_2 = conical_rod_lsf_arg(r, rod2_r0, rod2_r1, cone2_tip_center, &
711 rod2_radius, cone2_tip_radius, cone2_length_frac, cone2_tip_r_curvature)
712 two_conical_rods_lsf = min(lsf_1, lsf_2)
713 end function two_conical_rods_lsf
714
715 real(dp) function two_conical_rods_get_potential(r) result(phi)
716 real(dp), intent(in) :: r(ndim)
717 real(dp) :: lsf_1, lsf_2
718
719 lsf_1 = conical_rod_lsf_arg(r, rod_r0, rod_r1, cone_tip_center, &
720 rod_radius, cone_tip_radius, cone_length_frac, cone_tip_r_curvature)
721 lsf_2 = conical_rod_lsf_arg(r, rod2_r0, rod2_r1, cone2_tip_center, &
722 rod2_radius, cone2_tip_radius, cone2_length_frac, cone2_tip_r_curvature)
723
724 if (lsf_1 < lsf_2) then
725 ! Closer to electrode 1
726 if (field_electrode_grounded) then
727 phi = 0.0_dp
728 else
729 phi = current_voltage
730 end if
731 else
732 if (field_electrode2_grounded) then
733 phi = 0.0_dp
734 else
735 phi = current_voltage
736 end if
737 end if
738 end function two_conical_rods_get_potential
739
740 !> Get level set function for case of two rods
741 real(dp) function rod_rod_lsf(r)
742 use m_geometry
743 real(dp), intent(in) :: r(ndim)
744
745 rod_rod_lsf = min(gm_dist_line(r, rod_r0, rod_r1, ndim) - rod_radius, &
746 gm_dist_line(r, rod2_r0, rod2_r1, ndim) - rod2_radius)
747 end function rod_rod_lsf
748
749 !> Get potential to apply at electrode when there are two rods
750 function rod_rod_get_potential(r) result(phi)
751 use m_geometry
752 real(dp), intent(in) :: r(ndim)
753 real(dp) :: phi, lsf_1, lsf_2
754
755 ! Determine distance to electrodes
756 lsf_1 = gm_dist_line(r, rod_r0, rod_r1, ndim) - rod_radius
757 lsf_2 = gm_dist_line(r, rod2_r0, rod2_r1, ndim) - rod2_radius
758
759 if (lsf_1 < lsf_2) then
760 ! Closer to electrode 1
761 if (field_electrode_grounded) then
762 phi = 0.0_dp
763 else
764 phi = current_voltage
765 end if
766 else
767 if (field_electrode2_grounded) then
768 phi = 0.0_dp
769 else
770 phi = current_voltage
771 end if
772 end if
773 end function rod_rod_get_potential
774
775 !> Get level set function for case of sphere and rod
776 real(dp) function sphere_rod_lsf(r)
777 use m_geometry
778 real(dp), intent(in) :: r(ndim)
779
780 sphere_rod_lsf = min(sphere_lsf(r), &
781 gm_dist_line(r, rod2_r0, rod2_r1, ndim) - rod2_radius)
782 end function sphere_rod_lsf
783
784 !> Get potential to apply at electrode for sphere-rod case
785 function sphere_rod_get_potential(r) result(phi)
786 use m_geometry
787 real(dp), intent(in) :: r(ndim)
788 real(dp) :: phi, lsf_1, lsf_2
789
790 ! Determine distance to electrodes
791 lsf_1 = sphere_lsf(r)
792 lsf_2 = gm_dist_line(r, rod2_r0, rod2_r1, ndim) - rod2_radius
793
794 if (lsf_1 < lsf_2) then
795 ! Closer to electrode 1
796 if (field_electrode_grounded) then
797 phi = 0.0_dp
798 else
799 phi = current_voltage
800 end if
801 else
802 if (field_electrode2_grounded) then
803 phi = 0.0_dp
804 else
805 phi = current_voltage
806 end if
807 end if
808 end function sphere_rod_get_potential
809
810 !> Compute total field energy in Joule, defined as the volume integral over
811 !> 1/2 * epsilon * E^2
812 subroutine field_compute_energy(tree, field_energy)
813 type(af_t), intent(in) :: tree
814 real(dp), intent(out) :: field_energy
815
816 call af_reduction(tree, field_energy_box, reduce_sum, 0.0_dp, field_energy)
817 end subroutine field_compute_energy
818
819 !> Get the electrostatic field energy in a box
820 real(dp) function field_energy_box(box)
822 type(box_t), intent(in) :: box
823#if NDIM == 2
824 integer :: i
825 real(dp), parameter :: twopi = 2 * acos(-1.0_dp)
826#endif
827 real(dp) :: w(dtimes(box%n_cell))
828 integer :: nc
829
830 nc = box%n_cell
831
832 if (st_use_dielectric) then
833 w = 0.5_dp * uc_eps0 * box%cc(dtimes(1:nc), i_eps) * product(box%dr)
834 else
835 w = 0.5_dp * uc_eps0 * product(box%dr)
836 end if
837
838#if NDIM == 2
839 if (box%coord_t == af_cyl) then
840 ! Weight by 2 * pi * r
841 do i = 1, nc
842 w(i, :) = w(i, :) * twopi * af_cyl_radius_cc(box, i)
843 end do
844 end if
845#endif
846
847 field_energy_box = sum(w * box%cc(dtimes(1:nc), i_electric_fld)**2)
848 end function field_energy_box
849
850 real(dp) function reduce_sum(a, b)
851 real(dp), intent(in) :: a, b
852 reduce_sum = a + b
853 end function reduce_sum
854
855 function field_get_e_vector(box) result(E_vector)
856 type(box_t), intent(in) :: box
857 real(dp) :: e_vector(dtimes(1:box%n_cell), ndim)
858 integer :: nc
859
860 nc = box%n_cell
861
862#if NDIM == 1
863 e_vector(dtimes(:), 1) = 0.5_dp * (box%fc(1:nc, 1, electric_fld) + &
864 box%fc(2:nc+1, 1, electric_fld))
865#elif NDIM == 2
866 e_vector(dtimes(:), 1) = 0.5_dp * (box%fc(1:nc, 1:nc, 1, electric_fld) + &
867 box%fc(2:nc+1, 1:nc, 1, electric_fld))
868 e_vector(dtimes(:), 2) = 0.5_dp * (box%fc(1:nc, 1:nc, 2, electric_fld) + &
869 box%fc(1:nc, 2:nc+1, 2, electric_fld))
870#elif NDIM == 3
871 e_vector(dtimes(:), 1) = 0.5_dp * (box%fc(1:nc, 1:nc, 1:nc, 1, electric_fld) + &
872 box%fc(2:nc+1, 1:nc, 1:nc, 1, electric_fld))
873 e_vector(dtimes(:), 2) = 0.5_dp * (box%fc(1:nc, 1:nc, 1:nc, 2, electric_fld) + &
874 box%fc(1:nc, 2:nc+1, 1:nc, 2, electric_fld))
875 e_vector(dtimes(:), 3) = 0.5_dp * (box%fc(1:nc, 1:nc, 1:nc, 3, electric_fld) + &
876 box%fc(1:nc, 1:nc, 2:nc+1, 3, electric_fld))
877#endif
878 end function field_get_e_vector
879
880end module m_field
Module for handling chemical reactions.
integer, dimension(:), allocatable, public, protected charged_species_itree
List with indices of charged species.
integer, dimension(:), allocatable, public, protected charged_species_charge
List with charges of charged species.
Module with settings and routines to handle dielectrics.
integer, parameter, public i_surf_dens
type(surfaces_t), public diel
To store dielectric surface.
Module to compute electric fields.
Definition m_field.f90:2
subroutine, public field_set_rhs(tree, s_in)
Definition m_field.f90:377
real(dp), public, protected field_pulse_period
Time of one complete voltage pulse.
Definition m_field.f90:33
subroutine, public field_bc_homogeneous(box, nb, iv, coords, bc_val, bc_type)
Dirichlet boundary conditions for the potential in the last dimension, Neumann zero boundary conditio...
Definition m_field.f90:561
real(dp) function, dimension(dtimes(1:box%n_cell), ndim), public field_get_e_vector(box)
Definition m_field.f90:856
subroutine, public field_from_potential(tree, mg)
Compute field from potential.
Definition m_field.f90:502
subroutine, public field_compute_energy(tree, field_energy)
Compute total field energy in Joule, defined as the volume integral over 1/2 * epsilon * E^2.
Definition m_field.f90:813
subroutine, public field_set_voltage(tree, time)
Set the current voltage.
Definition m_field.f90:522
real(dp), public, protected current_voltage
The current applied voltage.
Definition m_field.f90:90
subroutine, public field_initialize(tree, cfg, mg)
Initialize this module.
Definition m_field.f90:108
subroutine, public field_compute(tree, mg, s_in, time, have_guess)
Compute electric field on the tree. First perform multigrid to get electric potential,...
Definition m_field.f90:419
Module that provides routines for geometric operations and calculations. Methods and types have a pre...
Definition m_geometry.f90:3
real(dp) function, public gm_dist_line(r, r0, r1, n_dim)
pure subroutine, public gm_dist_vec_line(r, r0, r1, n_dim, dist_vec, frac)
Compute distance vector between point and its projection onto a line between r0 and r1.
This module contains several pre-defined variables like:
Definition m_streamer.f90:6
integer, public, protected i_eps
Index can be set to include a dielectric.
type(mg_t), public mg
Multigrid option structure.
logical, public, protected st_use_electrode
Whether to include an electrode.
integer, public, protected i_lsf
Index can be set to include an electrode.
logical, public, protected st_use_dielectric
Whether a dielectric is used.
integer, public, protected electric_fld
Index of electric field vector.
integer, public, protected i_rhs
Index of source term Poisson.
integer, public, protected i_phi
Index of electrical potential.
integer, public, protected st_multigrid_num_vcycles
Number of V-cycles to perform per time step.
integer, public, protected i_electric_fld
Index of electric field norm.
real(dp), dimension(ndim), public, protected st_domain_len
Domain length per dimension.
real(dp), dimension(ndim), public, protected st_domain_origin
Origin of domain.
integer, public, protected i_tmp
Index of temporary variable.
real(dp), public, protected st_multigrid_max_rel_residual
Module with settings and routines for tabulated data.
subroutine, public table_from_file(file_name, data_name, x_data, y_data)
Routine to read in tabulated data from a file.
Module with basic types.
Definition m_types.f90:2
real(dp), parameter huge_real
Huge number.
Definition m_types.f90:16
character(len= *), parameter undefined_str
Undefined string.
Definition m_types.f90:10
real(dp), parameter undefined_real
Undefined number.
Definition m_types.f90:13
Module that contains physical and numerical constants.
real(dp), parameter uc_elem_charge
real(dp), parameter uc_eps0
This module contains all the methods that users can customize.
procedure(mg_func_lsf), pointer user_lsf_bc
Function to get boundary value for level set function.
procedure(field_func), pointer user_field_amplitude
To set the field amplitude manually.
procedure(af_subr_bc), pointer user_potential_bc
To set custom boundary conditions for the electric potential.
procedure(mg_func_lsf), pointer user_lsf
Custom level-set function to define an electrode.