MFC
Exascale flow solver
Loading...
Searching...
No Matches
m_perturbation.fpp.f90
Go to the documentation of this file.
1# 1 "/home/runner/work/MFC/MFC/src/pre_process/m_perturbation.fpp"
2!>
3!! @file
4!! @brief Contains module m_perturbation
5
6!> @brief Perturbs initial mean flow fields with random noise, mixing-layer instabilities, or simplex noise
8
11 use m_mpi_proxy
13 use m_helper
15 use ieee_arithmetic
16
17 implicit none
18
19 real(wp), allocatable, dimension(:,:,:,:) :: q_prim_temp
20
21contains
22
23 !> Allocate the temporary primitive variable array used by elliptic smoothing.
25
26 if (elliptic_smoothing) then
27 allocate (q_prim_temp(0:m,0:n,0:p,1:sys_size))
28 end if
29
31
32 !> Randomly perturb partial density fields at the interface of a spherical volume fraction region.
33 impure subroutine s_perturb_sphere(q_prim_vf)
34
35 type(scalar_field), dimension(sys_size), intent(inout) :: q_prim_vf
36 integer :: i, j, k, l
37 real(wp) :: perturb_alpha
38 real(wp) :: rand_real
39
40 call random_seed()
41
42 do k = 0, p
43 do j = 0, n
44 do i = 0, m
45 call random_number(rand_real)
46
47 perturb_alpha = q_prim_vf(eqn_idx%E + perturb_sph_fluid)%sf(i, j, k)
48
49 ! Perturb partial density fields to match perturbed volume fraction fields when the volume fraction is not near
50 ! 0 or 1
51 if ((.not. f_approx_equal(perturb_alpha, 0._wp)) .and. (.not. f_approx_equal(perturb_alpha, 1._wp))) then
52 do l = 1, num_fluids
53 q_prim_vf(l)%sf(i, j, k) = q_prim_vf(eqn_idx%E + l)%sf(i, j, k)*fluid_rho(l)
54 end do
55 end if
56 end do
57 end do
58 end do
59
60 end subroutine s_perturb_sphere
61
62 !> Add random noise to the velocity and void fraction of the surrounding flow field.
63 impure subroutine s_perturb_surrounding_flow(q_prim_vf)
64
65 type(scalar_field), dimension(sys_size), intent(inout) :: q_prim_vf
66 integer :: i, j, k
67 real(wp) :: rand_real
68
69 call random_seed()
70
71 do k = 0, p
72 do j = 0, n
73 do i = 0, m
74 call random_number(rand_real)
75 rand_real = rand_real*perturb_flow_mag
76 q_prim_vf(eqn_idx%mom%end)%sf(i, j, k) = rand_real*q_prim_vf(eqn_idx%mom%beg)%sf(i, j, k)
77 q_prim_vf(eqn_idx%mom%beg)%sf(i, j, k) = (1._wp + rand_real)*q_prim_vf(eqn_idx%mom%beg)%sf(i, j, k)
78 if (bubbles_euler) then
79 q_prim_vf(eqn_idx%alf)%sf(i, j, k) = (1._wp + rand_real)*q_prim_vf(eqn_idx%alf)%sf(i, j, k)
80 end if
81 end do
82 end do
83 end do
84
85 end subroutine s_perturb_surrounding_flow
86
87 !> Iteratively smooth all primitive variable fields using a discrete elliptic (Laplacian) filter.
88 impure subroutine s_elliptic_smoothing(q_prim_vf, bc_type, q_T_sf)
89
90 type(scalar_field), dimension(sys_size), intent(inout) :: q_prim_vf
91 type(integer_field), dimension(1:num_dims,1:2), intent(in) :: bc_type
92 type(scalar_field), optional, intent(inout) :: q_t_sf
93 integer :: i, j, k, l, q
94
96 ! Communication of buffer regions and apply boundary conditions
97 call s_populate_variables_buffers(bc_type, q_prim_vf, pb%sf, mv%sf, q_t_sf)
98
99 ! Perform smoothing and store in temp array
100 if (n == 0) then
101 do j = 0, m
102 do i = 1, sys_size
103 q_prim_temp(j, 0, 0, i) = (1._wp/4._wp)*(q_prim_vf(i)%sf(j + 1, 0, 0) + q_prim_vf(i)%sf(j - 1, 0, &
104 & 0) + 2._wp*q_prim_vf(i)%sf(j, 0, 0))
105 end do
106 end do
107 else if (p == 0) then
108 do k = 0, n
109 do j = 0, m
110 do i = 1, sys_size
111 q_prim_temp(j, k, 0, i) = (1._wp/8._wp)*(q_prim_vf(i)%sf(j + 1, k, 0) + q_prim_vf(i)%sf(j - 1, k, &
112 & 0) + q_prim_vf(i)%sf(j, k + 1, 0) + q_prim_vf(i)%sf(j, k - 1, &
113 & 0) + 4._wp*q_prim_vf(i)%sf(j, k, 0))
114 end do
115 end do
116 end do
117 else
118 do l = 0, p
119 do k = 0, n
120 do j = 0, m
121 do i = 1, sys_size
122 q_prim_temp(j, k, l, i) = (1._wp/12._wp)*(q_prim_vf(i)%sf(j + 1, k, l) + q_prim_vf(i)%sf(j - 1, &
123 & k, l) + q_prim_vf(i)%sf(j, k + 1, l) + q_prim_vf(i)%sf(j, k - 1, &
124 & l) + q_prim_vf(i)%sf(j, k, l + 1) + q_prim_vf(i)%sf(j, k, &
125 & l - 1) + 6._wp*q_prim_vf(i)%sf(j, k, l))
126 end do
127 end do
128 end do
129 end do
130 end if
131
132 ! Copy smoothed data back to array of scalar fields
133 do l = 0, p
134 do k = 0, n
135 do j = 0, m
136 do i = 1, sys_size
137 q_prim_vf(i)%sf(j, k, l) = q_prim_temp(j, k, l, i)
138 end do
139 end do
140 end do
141 end do
142 end do
143
144 end subroutine s_elliptic_smoothing
145
146 !> Perturb velocity and volume fraction fields using multi-octave simplex noise.
147 subroutine s_perturb_simplex(q_prim_vf)
148
149 type(scalar_field), dimension(sys_size), intent(inout) :: q_prim_vf
150 real(wp) :: mag, freq, scale, vel_rsm
151 real(wp), dimension(:,:), allocatable :: ofs
152 integer :: nOffsets
153 real(wp) :: xl, yl, zl
154 integer :: i, j, k, l, q
155
156 noffsets = max(num_dims, num_fluids)
157
158 allocate (ofs(noffsets, num_dims))
159
160 ! Store offsets
161 do i = 1, num_dims
162 do j = 1, num_dims
163 ofs(j, i) = simplex_params%perturb_vel_offset(j, i)
164 end do
165 end do
166
167 ! Perturb velocities
168 do i = 1, num_dims
169 if (simplex_params%perturb_vel(i)) then
170 freq = simplex_params%perturb_vel_freq(i)
171 scale = simplex_params%perturb_vel_scale(i)
172 do l = 0, p
173 do k = 0, n
174 do j = 0, m
175 xl = freq*(x_cc(j) + ofs(i, 1))
176 yl = freq*(y_cc(k) + ofs(i, 2))
177 if (num_dims == 2) then
178 mag = f_simplex2d(xl, yl)
179 else if (num_dims == 3) then
180 zl = freq*(z_cc(l) + ofs(i, 3))
181 mag = f_simplex3d(xl, yl, zl)
182 end if
183
184 vel_rsm = 0._wp
185 do q = 1, num_dims
186 vel_rsm = vel_rsm + q_prim_vf(eqn_idx%mom%beg + q - 1)%sf(j, k, l)**2._wp
187 end do
188 vel_rsm = sqrt(vel_rsm)
189
190 q_prim_vf(eqn_idx%mom%beg + i - 1)%sf(j, k, l) = q_prim_vf(eqn_idx%mom%beg + i - 1)%sf(j, k, &
191 & l) + vel_rsm*scale*mag
192 end do
193 end do
194 end do
195 end if
196 end do
197
198 ! Store offsets
199 do i = 1, num_dims
200 do j = 1, num_fluids
201 ofs(j, i) = simplex_params%perturb_dens_offset(j, i)
202 end do
203 end do
204
205 ! Perturb densities
206 do i = 1, num_fluids
207 if (simplex_params%perturb_dens(i)) then
208 freq = simplex_params%perturb_dens_freq(i)
209 scale = simplex_params%perturb_dens_scale(i)
210 do l = 0, p
211 do k = 0, n
212 do j = 0, m
213 xl = freq*(x_cc(j) + ofs(i, 1))
214 yl = freq*(y_cc(k) + ofs(i, 2))
215 if (num_dims == 2) then
216 mag = f_simplex2d(xl, yl)
217 else if (num_dims == 3) then
218 zl = freq*(z_cc(l) + ofs(i, 3))
219 mag = f_simplex3d(xl, yl, zl)
220 end if
221 q_prim_vf(eqn_idx%cont%beg + i - 1)%sf(j, k, l) = q_prim_vf(eqn_idx%cont%beg + i - 1)%sf(j, k, &
222 & l) + q_prim_vf(eqn_idx%cont%beg + i - 1)%sf(j, k, l)*scale*mag
223 end do
224 end do
225 end do
226 end if
227 end do
228
229 deallocate (ofs)
230
231 end subroutine s_perturb_simplex
232
233 !> Compute velocity perturbations for a temporal mixing layer with a hyperbolic tangent mean streamwise velocity profile, using
234 !! an inverted version of the spectrum-based synthetic turbulence generation method proposed by Guo et al. (2023, JFM).
235 subroutine s_perturb_mixlayer(q_prim_vf)
236
237 type(scalar_field), dimension(sys_size), intent(inout) :: q_prim_vf
238 real(wp), dimension(mixlayer_perturb_nk) :: k, Ek
239 real(wp), dimension(3, 3) :: Rij, Lmat
240 real(wp), dimension(3) :: velfluc, sig_tmp, sig, khat, xi
241 real(wp) :: dk, alpha, Eksum, q, uu0, phi
242 integer :: i, j, l, r, ierr
243
244 dk = 1._wp/mixlayer_perturb_nk
245
246 ! Compute prescribed energy spectra
247 eksum = 0._wp
248 do i = 1, mixlayer_perturb_nk
249 k(i) = dk*i
250 ek(i) = (k(i)/mixlayer_perturb_k0)**4._wp*exp(-2._wp*(k(i)/mixlayer_perturb_k0)**2._wp)
251 eksum = eksum + ek(i)
252 end do
253
254 ! Main loop
255 do r = 0, n
256 ! Compute prescribed Reynolds stress tensor with about half magnitude of its self-similar value
257 rij(:,:) = 0._wp
258 uu0 = patch_icpp(1)%vel(1)**2._wp*(1._wp - tanh(y_cc(r)*mixlayer_vel_coef)**2._wp)
259 rij(1, 1) = 0.05_wp*uu0
260 rij(2, 2) = 0.03_wp*uu0
261 rij(3, 3) = 0.03_wp*uu0
262 rij(1, 2) = -0.02_wp*uu0
263 rij(2, 1) = rij(1, 2)
264
265 ! Cholesky decomposition for inhomogeneity and anisotropy
266 lmat = 0._wp
267 lmat(1, 1) = sqrt(rij(1, 1))
268 if (abs(lmat(1, 1)) < sgm_eps) lmat(1, 1) = sgm_eps
269 lmat(2, 1) = rij(2, 1)/lmat(1, 1)
270 lmat(2, 2) = sqrt(rij(2, 2) - lmat(2, 1)**2._wp)
271 if (abs(lmat(2, 2)) < sgm_eps) lmat(2, 2) = sgm_eps
272 lmat(3, 1) = rij(3, 1)/lmat(1, 1)
273 lmat(3, 2) = (rij(3, 2) - lmat(3, 1)*lmat(2, 1))/lmat(2, 2)
274 lmat(3, 3) = sqrt(rij(3, 3) - lmat(3, 1)**2._wp - lmat(3, 2)**2._wp)
275
276 ! Compute perturbation for each Fourier component
277 do i = 1, mixlayer_perturb_nk
278 ! Generate random numbers for unit wavevector khat, random unit vector xi, and random mode phase phi
279 if (proc_rank == 0) then
280 call s_generate_random_perturbation(khat, xi, phi, i, y_cc(r))
281 end if
282
283#ifdef MFC_MPI
284 call mpi_bcast(khat, 3, mpi_p, 0, mpi_comm_world, ierr)
285 call mpi_bcast(xi, 3, mpi_p, 0, mpi_comm_world, ierr)
286 call mpi_bcast(phi, 1, mpi_p, 0, mpi_comm_world, ierr)
287#endif
288
289 ! Compute mode direction by two-time cross product
290 sig_tmp = f_cross(xi, khat)
291 sig_tmp = sig_tmp/sqrt(sum(sig_tmp**2._wp))
292 sig = f_cross(khat, sig_tmp)
293
294 ! Compute perturbation for each grid
295 do l = 0, p
296 do j = 0, m
297 q = sqrt(ek(i)/eksum)
298 alpha = k(i)*(khat(1)*x_cc(j) + khat(2)*y_cc(r) + khat(3)*z_cc(l)) + 2._wp*pi*phi
299 velfluc = 2._wp*q*sig*cos(alpha)
300 velfluc = matmul(lmat, velfluc)
301 q_prim_vf(eqn_idx%mom%beg)%sf(j, r, l) = q_prim_vf(eqn_idx%mom%beg)%sf(j, r, l) + velfluc(1)
302 q_prim_vf(eqn_idx%mom%beg + 1)%sf(j, r, l) = q_prim_vf(eqn_idx%mom%beg + 1)%sf(j, r, l) + velfluc(2)
303 q_prim_vf(eqn_idx%mom%beg + 2)%sf(j, r, l) = q_prim_vf(eqn_idx%mom%beg + 2)%sf(j, r, l) + velfluc(3)
304 end do
305 end do
306 end do
307 end do
308
309 end subroutine s_perturb_mixlayer
310
311 !> Generate deterministic pseudo-random wave vector, polarization, and phase for a perturbation mode.
312 subroutine s_generate_random_perturbation(khat, xi, phi, ik, yloc)
313
314 integer, intent(in) :: ik
315 real(wp), intent(in) :: yloc
316 real(wp), dimension(3), intent(out) :: khat, xi
317 real(wp), intent(out) :: phi
318 real(wp) :: theta, eta
319 integer :: seed, kfac, yfac
320
321 kfac = ik*amplifier
322 yfac = nint((sin(yloc) + 1._wp)*amplifier)
323 seed = nint(0.5_wp*modmul(kfac) + 0.5_wp*modmul(yfac))
324
325 call s_prng(theta, seed)
326 call s_prng(eta, seed)
327 khat = f_unit_vector(theta, eta)
328
329 call s_prng(theta, seed)
330 call s_prng(eta, seed)
331 xi = f_unit_vector(theta, eta)
332
333 call s_prng(phi, seed)
334
335 end subroutine s_generate_random_perturbation
336
337 !> Generate a unit vector uniformly distributed on the sphere from two random parameters.
338 function f_unit_vector(theta, eta) result(vec)
339
340 real(wp), intent(in) :: theta, eta
341 real(wp) :: zeta, xi
342 real(wp), dimension(3) :: vec
343
344 xi = 2._wp*pi*theta
345 zeta = acos(2._wp*eta - 1._wp)
346 vec(1) = sin(zeta)*cos(xi)
347 vec(2) = sin(zeta)*sin(xi)
348 vec(3) = cos(zeta)
349
350 end function f_unit_vector
351
352 !> Generate a pseudo-random number between 0 and 1 using a linear congruential generator.
353 subroutine s_prng(var, seed)
354
355 integer, intent(inout) :: seed
356 real(wp), intent(out) :: var
357
358 seed = mod(modmul(seed), modulus)
359 var = seed/real(modulus, wp)
360
361 end subroutine s_prng
362
363 !> Compute a modular multiplication step for the linear congruential pseudo-random number generator.
364 function modmul(a) result(val)
365
366 integer, intent(in) :: a
367 integer :: val
368 real(wp) :: x, y
369
370 x = (multiplier/real(modulus, wp))*a + (increment/real(modulus, wp))
371 y = nint((x - floor(x))*decimal_trim)/decimal_trim
372 val = nint(y*modulus)
373
374 end function modmul
375
376 !> Deallocate the temporary primitive variable array used by elliptic smoothing.
378
379 if (elliptic_smoothing) then
380 deallocate (q_prim_temp)
381 end if
382
383 end subroutine s_finalize_perturbation_module
384
385end module m_perturbation
integer, intent(in) k
integer, intent(in) j
integer, intent(in) l
Noncharacteristic and processor boundary condition application for ghost cells and buffer regions.
impure subroutine, public s_populate_variables_buffers(bc_type, q_prim_vf, pb_in, mv_in, q_t_sf)
Populate the buffers of the primitive variables based on the selected boundary conditions.
Shared derived types for field data, patch geometry, bubble dynamics, and MPI I/O structures.
Defines global parameters for the computational domain, simulation algorithm, and initial conditions.
real(wp) perturb_flow_mag
Magnitude of perturbation with perturb_flow flag.
real(wp) mixlayer_perturb_k0
Peak wavenumber for mixlayer perturbation (default: most unstable mode).
integer num_fluids
Number of different fluids present in the flow.
real(wp), dimension(:), allocatable y_cc
integer proc_rank
Rank of the local processor Number of cells in the x-, y- and z-coordinate directions.
integer sys_size
Number of unknowns in the system of equations.
type(simplex_noise_params) simplex_params
integer num_dims
Number of spatial dimensions.
real(wp), dimension(:), allocatable x_cc
Locations of cell-centers (cc) in x-, y- and z-directions, respectively.
integer mixlayer_perturb_nk
Number of Fourier modes for perturbation with mixlayer_perturb flag.
integer perturb_sph_fluid
Fluid to be perturbed with perturb_sph flag.
real(wp), dimension(num_fluids_max) fluid_rho
real(wp), dimension(:), allocatable z_cc
type(ic_patch_parameters), dimension(num_patches_max) patch_icpp
IC patch parameters (max: num_patches_max).
real(wp) mixlayer_vel_coef
Coefficient for the hyperbolic tangent streamwise velocity profile.
type(eqn_idx_info) eqn_idx
All conserved-variable equation index ranges and scalars.
Utility routines for bubble model setup, coordinate transforms, array sampling, and special functions...
pure real(wp) function, dimension(3), public f_cross(a, b)
Compute the cross product of two vectors.
Broadcasts user inputs and decomposes the domain across MPI ranks for pre-processing.
Perturbs initial mean flow fields with random noise, mixing-layer instabilities, or simplex noise.
integer function modmul(a)
Compute a modular multiplication step for the linear congruential pseudo-random number generator.
impure subroutine s_perturb_sphere(q_prim_vf)
Randomly perturb partial density fields at the interface of a spherical volume fraction region.
impure subroutine s_perturb_surrounding_flow(q_prim_vf)
Add random noise to the velocity and void fraction of the surrounding flow field.
real(wp) function, dimension(3) f_unit_vector(theta, eta)
Generate a unit vector uniformly distributed on the sphere from two random parameters.
subroutine s_generate_random_perturbation(khat, xi, phi, ik, yloc)
Generate deterministic pseudo-random wave vector, polarization, and phase for a perturbation mode.
subroutine s_prng(var, seed)
Generate a pseudo-random number between 0 and 1 using a linear congruential generator.
real(wp), dimension(:,:,:,:), allocatable q_prim_temp
impure subroutine s_finalize_perturbation_module()
Deallocate the temporary primitive variable array used by elliptic smoothing.
impure subroutine s_elliptic_smoothing(q_prim_vf, bc_type, q_t_sf)
Iteratively smooth all primitive variable fields using a discrete elliptic (Laplacian) filter.
impure subroutine s_initialize_perturbation_module()
Allocate the temporary primitive variable array used by elliptic smoothing.
subroutine s_perturb_simplex(q_prim_vf)
Perturb velocity and volume fraction fields using multi-octave simplex noise.
subroutine s_perturb_mixlayer(q_prim_vf)
Compute velocity perturbations for a temporal mixing layer with a hyperbolic tangent mean streamwise ...
2D and 3D simplex noise generation for procedural initial condition perturbations
real(wp) function, public f_simplex2d(xin, yin)
Evaluate 2D simplex noise at the given coordinates and return a value in [-1, 1].
real(wp) function, public f_simplex3d(xin, yin, zin)
Evaluate 3D simplex noise at the given coordinates and return a value in [-1, 1].
Derived type annexing an integer scalar field (SF).
Derived type annexing a scalar field (SF).