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 "Type of electrode (sphere, rod, rod_cone_top, rod_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 ("two_rod_cone_electrodes")
296 ! Two rod-shaped electrodes with conical tops (for now assumed to have
297 ! the same shape)
298 call check_general_electrode_parameters()
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 if (cone_tip_radius <= 0 .or. cone_tip_radius > rod_radius) &
306 error stop "cone tip radius should be smaller than rod radius"
307 if (cone2_tip_radius <= 0 .or. cone2_tip_radius > rod2_radius) &
308 error stop "cone2 tip radius should be smaller than rod2 radius"
309 if (cone_length_frac < 0 .or. cone_length_frac > 1) &
310 error stop "cone_length_frac not set correctly"
311 if (cone2_length_frac < 0 .or. cone2_length_frac > 1) &
312 error stop "cone2_length_frac not set correctly"
313
314 call get_conical_rod_properties(rod_r0, rod_r1, rod_radius, &
315 cone_tip_radius, cone_tip_center, cone_tip_r_curvature)
316 call get_conical_rod_properties(rod2_r0, rod2_r1, rod2_radius, &
317 cone2_tip_radius, cone2_tip_center, cone2_tip_r_curvature)
318
319 mg%lsf => two_conical_rods_lsf
320
321 ! Provide a function to set the voltage on the electrodes
322 mg%lsf_boundary_function => two_conical_rods_get_potential
323 case ("user")
324 if (.not. associated(user_lsf)) then
325 error stop "user_lsf not set"
326 else
327 mg%lsf => user_lsf
328 end if
329
330 if (associated(user_lsf_bc)) then
331 mg%lsf_boundary_function => user_lsf_bc
332 end if
333 case default
334 print *, "Electrode types: sphere, rod, rod_cone_top, rod_rod, user"
335 error stop "Invalid electrode type"
336 end select
337
338 call af_set_cc_methods(tree, i_lsf, funcval=set_lsf_box)
339 tree%mg_i_lsf = i_lsf
340
341 mg%lsf_dist => mg_lsf_dist_gss
342
343 if (rod_radius <= 0) then
344 error stop "set field_rod_radius to smallest length scale of electrode"
345 end if
346 mg%lsf_length_scale = rod_radius
347 end if
348
349 call af_set_cc_methods(tree, i_electric_fld, &
350 af_bc_neumann_zero, af_gc_interp)
351
352 end subroutine field_initialize
353
354 subroutine check_general_electrode_parameters()
355 if (any(rod_r0 <= -1.0e10_dp)) &
356 error stop "field_rod_r0 not set correctly"
357 if (any(rod_r1 <= -1.0e10_dp)) &
358 error stop "field_rod_r1 not set correctly"
359 if (rod_radius <= 0) &
360 error stop "field_rod_radius not set correctly"
361 end subroutine check_general_electrode_parameters
362
363 subroutine field_set_rhs(tree, s_in)
365 use m_chemistry
366 use m_dielectric
367 type(af_t), intent(inout) :: tree
368 integer, intent(in) :: s_in
369 real(dp), parameter :: fac = -uc_elem_charge / uc_eps0
370 real(dp) :: q
371 integer :: lvl, i, id, nc, n, ix
372
373 nc = tree%n_cell
374
375 ! Set the source term (rhs)
376 !$omp parallel private(lvl, i, id, n, ix, q)
377 do lvl = 1, tree%highest_lvl
378 !$omp do
379 do i = 1, size(tree%lvls(lvl)%leaves)
380 id = tree%lvls(lvl)%leaves(i)
381
382 tree%boxes(id)%cc(dtimes(:), i_rhs) = 0.0_dp
383
384 do n = 1, size(charged_species_itree)
385 ix = charged_species_itree(n) + s_in
386 q = charged_species_charge(n) * fac
387
388 tree%boxes(id)%cc(dtimes(:), i_rhs) = &
389 tree%boxes(id)%cc(dtimes(:), i_rhs) + &
390 q * tree%boxes(id)%cc(dtimes(:), ix)
391 end do
392 end do
393 !$omp end do nowait
394 end do
395 !$omp end parallel
396
397 if (st_use_dielectric) then
398 call surface_surface_charge_to_rhs(tree, diel, i_surf_dens, i_rhs, fac)
399 end if
400
401 end subroutine field_set_rhs
402
403 !> Compute electric field on the tree. First perform multigrid to get electric
404 !> potential, then take numerical gradient to geld field.
405 subroutine field_compute(tree, mg, s_in, time, have_guess)
406 use m_chemistry
407 type(af_t), intent(inout) :: tree
408 type(mg_t), intent(inout) :: mg ! Multigrid option struct
409 integer, intent(in) :: s_in
410 real(dp), intent(in) :: time
411 logical, intent(in) :: have_guess
412 integer :: i
413 real(dp) :: max_rhs, residual_threshold, conv_fac
414 real(dp) :: residual_ratio
415 integer, parameter :: max_initial_iterations = 100
416 real(dp), parameter :: max_residual = 1e8_dp
417 real(dp), parameter :: min_residual = 1e-6_dp
418 real(dp) :: residuals(max_initial_iterations)
419
420 call field_set_rhs(tree, s_in)
421 call field_set_voltage(tree, time)
422
423 call af_tree_maxabs_cc(tree, mg%i_rhs, max_rhs)
424
425 ! With an electrode, the initial convergence testing should be less strict
426 if (st_use_electrode) then
427 conv_fac = 1e-8_dp
428 else
429 conv_fac = 1e-10_dp
430 end if
431
432 ! Set threshold based on rhs and on estimate of round-off error, given by
433 ! delta phi / dx^2 = (phi/L * dx)/dx^2
434 ! Note that we use min_residual in case max_rhs and current_voltage are zero
435 residual_threshold = max(min_residual, &
437 conv_fac * abs(current_voltage)/(st_domain_len(ndim) * af_min_dr(tree)))
438
439 if (st_use_electrode) then
440 if (field_electrode_grounded) then
441 mg%lsf_boundary_value = 0.0_dp
442 else
443 mg%lsf_boundary_value = current_voltage
444 end if
445 end if
446
447 ! Perform a FMG cycle when we have no guess
448 if (.not. have_guess) then
449 do i = 1, max_initial_iterations
450 call mg_fas_fmg(tree, mg, .true., .true.)
451 call af_tree_maxabs_cc(tree, mg%i_tmp, residuals(i))
452
453 if (residuals(i) < residual_threshold) then
454 exit
455 else if (i > 2) then
456 ! Check if the residual is not changing much anymore, and if it is
457 ! small enough, in which case we assume convergence
458 residual_ratio = minval(residuals(i-2:i)) / &
459 maxval(residuals(i-2:i))
460 if (residual_ratio < 2.0_dp .and. residual_ratio > 0.5_dp &
461 .and. residuals(i) < max_residual) exit
462 end if
463 end do
464
465 ! Check for convergence
466 if (i == max_initial_iterations + 1) then
467 print *, "Iteration residual"
468 do i = 1, max_initial_iterations
469 write(*, "(I4,E18.2)") i, residuals(i)
470 end do
471 print *, "Maybe increase multigrid_max_rel_residual?"
472 error stop "No convergence in initial field computation"
473 end if
474 end if
475
476 ! Perform cheaper V-cycles
478 call mg_fas_vcycle(tree, mg, .true.)
479 call af_tree_maxabs_cc(tree, mg%i_tmp, residuals(i))
480 if (residuals(i) < residual_threshold) exit
481 end do
482
483 call field_from_potential(tree, mg)
484
485 end subroutine field_compute
486
487 !> Compute field from potential
488 subroutine field_from_potential(tree, mg)
490 use m_dielectric
491 type(af_t), intent(inout) :: tree
492 type(mg_t), intent(in) :: mg ! Multigrid option struct
493
494 if (st_use_dielectric) then
495 call mg_compute_phi_gradient(tree, mg, electric_fld, -1.0_dp)
496 call surface_correct_field_fc(tree, diel, i_surf_dens, &
498 call mg_compute_field_norm(tree, electric_fld, i_electric_fld)
499 else
500 call mg_compute_phi_gradient(tree, mg, electric_fld, -1.0_dp, i_electric_fld)
501 end if
502
503 ! Set the field norm also in ghost cells
504 call af_gc_tree(tree, [i_electric_fld])
505 end subroutine field_from_potential
506
507 !> Set the current voltage
508 subroutine field_set_voltage(tree, time)
510 use m_lookup_table
512 type(af_t), intent(in) :: tree
513 real(dp), intent(in) :: time
514 real(dp) :: electric_fld, t, tmp
515
516 if (associated(user_field_amplitude)) then
519 return
520 end if
521
522 select case (field_given_by)
523 case (scalar_voltage)
524 current_voltage = 0.0_dp
525
526 if (time < field_pulse_period * field_num_pulses) then
527 t = modulo(time, field_pulse_period)
528
529 if (t < field_rise_time) then
530 current_voltage = field_voltage * (t/field_rise_time)
531 else if (t < field_pulse_width + field_rise_time) then
532 current_voltage = field_voltage
533 else
534 tmp = t - (field_pulse_width + field_rise_time)
535 current_voltage = field_voltage * max(0.0_dp, &
536 (1 - tmp/field_rise_time))
537 end if
538 end if
539 case (tabulated_voltage)
540 call lt_lin_interp_list(field_table_times, field_table_values, &
541 time, current_voltage)
542 end select
543 end subroutine field_set_voltage
544
545 !> Dirichlet boundary conditions for the potential in the last dimension,
546 !> Neumann zero boundary conditions in the other directions
547 subroutine field_bc_homogeneous(box, nb, iv, coords, bc_val, bc_type)
548 type(box_t), intent(in) :: box
549 integer, intent(in) :: nb
550 integer, intent(in) :: iv
551 real(dp), intent(in) :: coords(ndim, box%n_cell**(ndim-1))
552 real(dp), intent(out) :: bc_val(box%n_cell**(ndim-1))
553 integer, intent(out) :: bc_type
554
555 if (af_neighb_dim(nb) == ndim) then
556 if (af_neighb_low(nb)) then
557 bc_type = af_bc_dirichlet
558 bc_val = 0.0_dp
559 else
560 bc_type = af_bc_dirichlet
561 bc_val = current_voltage
562 end if
563 else
564 bc_type = af_bc_neumann
565 bc_val = 0.0_dp
566 end if
567 end subroutine field_bc_homogeneous
568
569 !> A Dirichlet zero and non-zero Neumann boundary condition for the potential
570 !> in the last dimension, Neumann zero boundary conditions in the other
571 !> directions
572 subroutine field_bc_neumann(box, nb, iv, coords, bc_val, bc_type)
573 type(box_t), intent(in) :: box
574 integer, intent(in) :: nb
575 integer, intent(in) :: iv
576 real(dp), intent(in) :: coords(ndim, box%n_cell**(ndim-1))
577 real(dp), intent(out) :: bc_val(box%n_cell**(ndim-1))
578 integer, intent(out) :: bc_type
579
580 if (af_neighb_dim(nb) == ndim) then
581 if (af_neighb_low(nb)) then
582 bc_type = af_bc_dirichlet
583 bc_val = 0.0_dp
584 else
585 bc_type = af_bc_neumann
586 bc_val = current_voltage/st_domain_len(ndim)
587 end if
588 else
589 bc_type = af_bc_neumann
590 bc_val = 0.0_dp
591 end if
592 end subroutine field_bc_neumann
593
594 !> All Neumann zero boundary conditions for the potential
595 subroutine field_bc_all_neumann(box, nb, iv, coords, bc_val, bc_type)
596 type(box_t), intent(in) :: box
597 integer, intent(in) :: nb
598 integer, intent(in) :: iv
599 real(dp), intent(in) :: coords(ndim, box%n_cell**(ndim-1))
600 real(dp), intent(out) :: bc_val(box%n_cell**(ndim-1))
601 integer, intent(out) :: bc_type
602
603 bc_type = af_bc_neumann
604 bc_val = 0.0_dp
605 end subroutine field_bc_all_neumann
606
607 ! This routine sets the level set function for a simple rod
608 subroutine set_lsf_box(box, iv)
609 type(box_t), intent(inout) :: box
610 integer, intent(in) :: iv
611 integer :: ijk, nc
612 real(dp) :: rr(ndim)
613
614 nc = box%n_cell
615 do kji_do(0,nc+1)
616 rr = af_r_cc(box, [ijk])
617 box%cc(ijk, iv) = mg%lsf(rr)
618 end do; close_do
619 end subroutine set_lsf_box
620
621 real(dp) function sphere_lsf(r)
622 real(dp), intent(in) :: r(ndim)
623 sphere_lsf = norm2(r - rod_r0) - rod_radius
624 end function sphere_lsf
625
626 real(dp) function rod_lsf(r)
627 use m_geometry
628 real(dp), intent(in) :: r(ndim)
629 rod_lsf = gm_dist_line(r, rod_r0, rod_r1, ndim) - rod_radius
630 end function rod_lsf
631
632 !> Compute several parameters for a conical rod
633 subroutine get_conical_rod_properties(r0, r1, &
634 rod_radius, tip_radius, cone_tip_center, cone_tip_r_curvature)
635 real(dp), intent(in) :: r0(ndim) !< Beginning of rod
636 real(dp), intent(in) :: r1(ndim) !< End of conical part
637 real(dp), intent(in) :: rod_radius !< Radius of rod
638 real(dp), intent(in) :: tip_radius !< Radius of curvature of tip
639 real(dp), intent(out) :: cone_tip_center(ndim)
640 real(dp), intent(out) :: cone_tip_r_curvature
641 real(dp) :: cone_angle, cone_length
642
643 ! Determine (half) the opening angle of the top cone, which goes from
644 ! rod_radius to tip_radius over cone_length
645 cone_length = cone_length_frac * norm2(r1 - r0)
646 cone_angle = atan((rod_radius - tip_radius) / cone_length)
647
648 ! We have a point on a sphere with coordinates of the form (R*cos(a),
649 ! R*sin(a)) = (tip_radius, y), so we can get R and subtract R sin(a) to
650 ! obtain the center of the sphere
651 cone_tip_r_curvature = tip_radius/cos(cone_angle)
652 cone_tip_center = r1 - sin(cone_angle) * &
653 cone_tip_r_curvature * (r1 - r0)/norm2(r1 - r0)
654 end subroutine get_conical_rod_properties
655
656 !> Helper function to compute lsf for a rod with a conical tip
657 pure function conical_rod_lsf_arg(r, r0, r1, cone_tip_center, &
658 rod_radius, tip_radius, cone_length_frac, r_curvature) result (lsf)
659 use m_geometry
660 real(dp), intent(in) :: r(ndim)
661 real(dp), intent(in) :: r0(ndim), r1(ndim), cone_tip_center(ndim)
662 real(dp), intent(in) :: rod_radius, tip_radius, cone_length_frac
663 real(dp), intent(in) :: r_curvature
664 real(dp) :: lsf
665 real(dp) :: dist_vec(ndim), frac, radius_at_height, tmp
666
667 ! Project onto line from r0 to r1
668 call gm_dist_vec_line(r, r0, r1, ndim, dist_vec, frac)
669
670 if (frac <= 1 - cone_length_frac) then
671 ! Cylindrical part
672 lsf = norm2(dist_vec) - rod_radius
673 else if (frac < 1.0_dp) then
674 ! Conical part
675 tmp = (1 - frac) / cone_length_frac ! between 0 and 1
676 radius_at_height = tip_radius + tmp * (rod_radius - tip_radius)
677 lsf = norm2(dist_vec) - radius_at_height
678 else
679 ! Spherical tip
680 lsf = norm2(r - cone_tip_center) - r_curvature
681 end if
682 end function conical_rod_lsf_arg
683
684 real(dp) function conical_rod_lsf(r)
685 real(dp), intent(in) :: r(ndim)
686 conical_rod_lsf = conical_rod_lsf_arg(r, rod_r0, rod_r1, cone_tip_center, &
687 rod_radius, cone_tip_radius, cone_length_frac, cone_tip_r_curvature)
688 end function conical_rod_lsf
689
690 !> Get lsf for two conical rods
691 real(dp) function two_conical_rods_lsf(r)
692 real(dp), intent(in) :: r(ndim)
693 real(dp) :: lsf_1, lsf_2
694
695 lsf_1 = conical_rod_lsf_arg(r, rod_r0, rod_r1, cone_tip_center, &
696 rod_radius, cone_tip_radius, cone_length_frac, cone_tip_r_curvature)
697 lsf_2 = conical_rod_lsf_arg(r, rod2_r0, rod2_r1, cone2_tip_center, &
698 rod2_radius, cone2_tip_radius, cone2_length_frac, cone2_tip_r_curvature)
699 two_conical_rods_lsf = min(lsf_1, lsf_2)
700 end function two_conical_rods_lsf
701
702 real(dp) function two_conical_rods_get_potential(r) result(phi)
703 real(dp), intent(in) :: r(ndim)
704 real(dp) :: lsf_1, lsf_2
705
706 lsf_1 = conical_rod_lsf_arg(r, rod_r0, rod_r1, cone_tip_center, &
707 rod_radius, cone_tip_radius, cone_length_frac, cone_tip_r_curvature)
708 lsf_2 = conical_rod_lsf_arg(r, rod2_r0, rod2_r1, cone2_tip_center, &
709 rod2_radius, cone2_tip_radius, cone2_length_frac, cone2_tip_r_curvature)
710
711 if (lsf_1 < lsf_2) then
712 ! Closer to electrode 1
713 if (field_electrode_grounded) then
714 phi = 0.0_dp
715 else
716 phi = current_voltage
717 end if
718 else
719 if (field_electrode2_grounded) then
720 phi = 0.0_dp
721 else
722 phi = current_voltage
723 end if
724 end if
725 end function two_conical_rods_get_potential
726
727 !> Get level set function for case of two rods
728 real(dp) function rod_rod_lsf(r)
729 use m_geometry
730 real(dp), intent(in) :: r(ndim)
731
732 rod_rod_lsf = min(gm_dist_line(r, rod_r0, rod_r1, ndim) - rod_radius, &
733 gm_dist_line(r, rod2_r0, rod2_r1, ndim) - rod2_radius)
734 end function rod_rod_lsf
735
736 !> Get potential to apply at electrode when there are two rods
737 function rod_rod_get_potential(r) result(phi)
738 use m_geometry
739 real(dp), intent(in) :: r(ndim)
740 real(dp) :: phi, lsf_1, lsf_2
741
742 ! Determine distance to electrodes
743 lsf_1 = gm_dist_line(r, rod_r0, rod_r1, ndim) - rod_radius
744 lsf_2 = gm_dist_line(r, rod2_r0, rod2_r1, ndim) - rod2_radius
745
746 if (lsf_1 < lsf_2) then
747 ! Closer to electrode 1
748 if (field_electrode_grounded) then
749 phi = 0.0_dp
750 else
751 phi = current_voltage
752 end if
753 else
754 if (field_electrode2_grounded) then
755 phi = 0.0_dp
756 else
757 phi = current_voltage
758 end if
759 end if
760 end function rod_rod_get_potential
761
762 !> Compute total field energy in Joule, defined as the volume integral over
763 !> 1/2 * epsilon * E^2
764 subroutine field_compute_energy(tree, field_energy)
765 type(af_t), intent(in) :: tree
766 real(dp), intent(out) :: field_energy
767
768 call af_reduction(tree, field_energy_box, reduce_sum, 0.0_dp, field_energy)
769 end subroutine field_compute_energy
770
771 !> Get the electrostatic field energy in a box
772 real(dp) function field_energy_box(box)
774 type(box_t), intent(in) :: box
775#if NDIM == 2
776 integer :: i
777 real(dp), parameter :: twopi = 2 * acos(-1.0_dp)
778#endif
779 real(dp) :: w(dtimes(box%n_cell))
780 integer :: nc
781
782 nc = box%n_cell
783
784 if (st_use_dielectric) then
785 w = 0.5_dp * uc_eps0 * box%cc(dtimes(1:nc), i_eps) * product(box%dr)
786 else
787 w = 0.5_dp * uc_eps0 * product(box%dr)
788 end if
789
790#if NDIM == 2
791 if (box%coord_t == af_cyl) then
792 ! Weight by 2 * pi * r
793 do i = 1, nc
794 w(i, :) = w(i, :) * twopi * af_cyl_radius_cc(box, i)
795 end do
796 end if
797#endif
798
799 field_energy_box = sum(w * box%cc(dtimes(1:nc), i_electric_fld)**2)
800 end function field_energy_box
801
802 real(dp) function reduce_sum(a, b)
803 real(dp), intent(in) :: a, b
804 reduce_sum = a + b
805 end function reduce_sum
806
807 function field_get_e_vector(box) result(E_vector)
808 type(box_t), intent(in) :: box
809 real(dp) :: e_vector(dtimes(1:box%n_cell), ndim)
810 integer :: nc
811
812 nc = box%n_cell
813
814#if NDIM == 1
815 e_vector(dtimes(:), 1) = 0.5_dp * (box%fc(1:nc, 1, electric_fld) + &
816 box%fc(2:nc+1, 1, electric_fld))
817#elif NDIM == 2
818 e_vector(dtimes(:), 1) = 0.5_dp * (box%fc(1:nc, 1:nc, 1, electric_fld) + &
819 box%fc(2:nc+1, 1:nc, 1, electric_fld))
820 e_vector(dtimes(:), 2) = 0.5_dp * (box%fc(1:nc, 1:nc, 2, electric_fld) + &
821 box%fc(1:nc, 2:nc+1, 2, electric_fld))
822#elif NDIM == 3
823 e_vector(dtimes(:), 1) = 0.5_dp * (box%fc(1:nc, 1:nc, 1:nc, 1, electric_fld) + &
824 box%fc(2:nc+1, 1:nc, 1:nc, 1, electric_fld))
825 e_vector(dtimes(:), 2) = 0.5_dp * (box%fc(1:nc, 1:nc, 1:nc, 2, electric_fld) + &
826 box%fc(1:nc, 2:nc+1, 1:nc, 2, electric_fld))
827 e_vector(dtimes(:), 3) = 0.5_dp * (box%fc(1:nc, 1:nc, 1:nc, 3, electric_fld) + &
828 box%fc(1:nc, 1:nc, 2:nc+1, 3, electric_fld))
829#endif
830 end function field_get_e_vector
831
832end 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:364
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:548
real(dp) function, dimension(dtimes(1:box%n_cell), ndim), public field_get_e_vector(box)
Definition m_field.f90:808
subroutine, public field_from_potential(tree, mg)
Compute field from potential.
Definition m_field.f90:489
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:765
subroutine, public field_set_voltage(tree, time)
Set the current voltage.
Definition m_field.f90:509
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:406
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.