afivo-streamer 1.1
1D/2D/3D streamer simulations with AMR
Loading...
Searching...
No Matches
m_geometry.f90
Go to the documentation of this file.
1!> Module that provides routines for geometric operations and calculations.
2!> Methods and types have a prefix GM_, short for 'geometry'.
4
5! TODO: Describe methods. Till now those GM methods are not used elsewhere
6 implicit none
7 private
8
9 integer, parameter :: dp = kind(0.0d0)
10
11 ! Public methods
12 public :: gm_dist_line
13 public :: gm_dist_vec_line
14 public :: gm_density_line
15 public :: gm_sigmoid
16 public :: gm_gaussian
17 public :: gm_smoothstep
18
19contains
20
21 !> Compute distance vector between point and its projection onto a line
22 !> between r0 and r1
23 pure subroutine gm_dist_vec_line(r, r0, r1, n_dim, dist_vec, frac)
24 integer, intent(in) :: n_dim
25 real(dp), intent(in) :: r(n_dim), r0(n_dim), r1(n_dim)
26 real(dp), intent(out) :: dist_vec(n_dim)
27 real(dp), intent(out) :: frac !< Fraction [0,1] along line
28 real(dp) :: line_len2
29
30 line_len2 = sum((r1 - r0)**2)
31 frac = sum((r - r0) * (r1 - r0))
32
33 if (frac <= 0.0_dp) then
34 frac = 0.0_dp
35 dist_vec = r - r0
36 else if (frac >= line_len2) then
37 frac = 1.0_dp
38 dist_vec = r - r1
39 else
40 dist_vec = r - (r0 + frac/line_len2 * (r1 - r0))
41 frac = frac / line_len2
42 end if
43 end subroutine gm_dist_vec_line
44
45 function gm_dist_line(r, r0, r1, n_dim) result(dist)
46 integer, intent(in) :: n_dim
47 real(dp), intent(in) :: r(n_dim), r0(n_dim), r1(n_dim)
48 real(dp) :: dist, dist_vec(n_dim), frac
49 call gm_dist_vec_line(r, r0, r1, n_dim, dist_vec, frac)
50 dist = norm2(dist_vec)
51 end function gm_dist_line
52
53 function gm_density_line(r, r0, r1, n_0, n_1, n_dim, width, falloff_t) result(val)
54 integer, intent(in) :: n_dim
55 real(dp), intent(in) :: r(n_dim), r0(n_dim), r1(n_dim), width
56 real(dp), intent(in) :: n_0, n_1
57 character(len=*), intent(in) :: falloff_t
58 real(dp) :: dist, val, dist_vec(n_dim), frac
59
60 call gm_dist_vec_line(r, r0, r1, n_dim, dist_vec, frac)
61 dist = norm2(dist_vec)
62
63 select case (falloff_t)
64 case ('sigmoid')
65 val = gm_sigmoid(dist, width)
66 case ('gaussian')
67 val = gm_gaussian(dist, width)
68 case ('smoothstep')
69 val = gm_smoothstep(dist, width)
70 case ('step')
71 val = gm_step(dist, width)
72 case ('laser')
73 val = gm_laser(dist_vec, width, n_dim)
74 case default
75 print *, "GM_density_line: unknown fall-off type: ", trim(falloff_t)
76 print *, "Valid options: sigmoid, gaussian, smoothstep, step, laser"
77 stop
78 end select
79
80 ! Interpolate density between start and endpoint
81 val = val * (frac * n_0 + (1-frac) * n_1)
82 end function gm_density_line
83
84 function gm_sigmoid(dist, width) result(val)
85 real(dp), intent(in) :: dist, width
86 real(dp) :: val, tmp
87
88 tmp = dist / width
89 if (tmp > log(0.5_dp * huge(1.0_dp))) then
90 val = 0
91 else
92 val = 2 / (1 + exp(tmp))
93 end if
94 end function gm_sigmoid
95
96 function gm_gaussian(dist, width) result(val)
97 real(dp), intent(in) :: dist, width
98 real(dp) :: val
99 val = exp(-(dist/width)**2)
100 end function gm_gaussian
101
102 function gm_smoothstep(dist, width) result(val)
103 real(dp), intent(in) :: dist, width
104 real(dp) :: val, temp
105 if (dist < width) then
106 val = 1
107 else if (dist < 2 * width) then
108 temp = dist/width - 1
109 val = (1- (3 * temp**2 - 2 * temp**3))
110 else
111 val = 0.0_dp
112 end if
113 end function gm_smoothstep
114
115 function gm_step(dist, width) result(val)
116 real(dp), intent(in) :: dist, width
117 real(dp) :: val
118 if (dist < width) then
119 val = 1
120 else
121 val = 0.0_dp
122 end if
123 end function gm_step
124
125 function gm_laser(dist_vec, width, n_dim) result(val)
126 integer, intent(in) :: n_dim
127 real(dp), intent(in) :: dist_vec(n_dim), width
128 real(dp) :: val, xz(2), dy, dxz
129
130 xz(1) = dist_vec(1)
131 xz(2) = dist_vec(3)
132 dy = abs(dist_vec(2))
133 dxz = norm2(xz)
134
135 if (dy < width .and. dxz < width) then
136 val = 1
137 else
138 val = exp(1-(dy**2 + dxz**2)/width**2)
139 end if
140 end function gm_laser
141
142end module m_geometry
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_sigmoid(dist, width)
real(dp) function, public gm_density_line(r, r0, r1, n_0, n_1, n_dim, width, falloff_t)
real(dp) function, public gm_gaussian(dist, width)
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.
real(dp) function, public gm_smoothstep(dist, width)