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(e_idx + 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(e_idx + 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) :: perturb_alpha
68 real(wp) :: rand_real
69
70 call random_seed()
71
72 do k = 0, p
73 do j = 0, n
74 do i = 0, m
75 perturb_alpha = q_prim_vf(e_idx + perturb_flow_fluid)%sf(i, j, k)
76 call random_number(rand_real)
77 rand_real = rand_real*perturb_flow_mag
78 q_prim_vf(mom_idx%end)%sf(i, j, k) = rand_real*q_prim_vf(mom_idx%beg)%sf(i, j, k)
79 q_prim_vf(mom_idx%beg)%sf(i, j, k) = (1._wp + rand_real)*q_prim_vf(mom_idx%beg)%sf(i, j, k)
80 if (bubbles_euler) then
81 q_prim_vf(alf_idx)%sf(i, j, k) = (1._wp + rand_real)*q_prim_vf(alf_idx)%sf(i, j, k)
82 end if
83 end do
84 end do
85 end do
86
87 end subroutine s_perturb_surrounding_flow
88
89 !> Iteratively smooth all primitive variable fields using a discrete elliptic (Laplacian) filter.
90 impure subroutine s_elliptic_smoothing(q_prim_vf, bc_type)
91
92 type(scalar_field), dimension(sys_size), intent(inout) :: q_prim_vf
93 type(integer_field), dimension(1:num_dims,1:2), intent(in) :: bc_type
94 integer :: i, j, k, l, q
95
97 ! Communication of buffer regions and apply boundary conditions
98 call s_populate_variables_buffers(bc_type, q_prim_vf, pb%sf, mv%sf)
99
100 ! Perform smoothing and store in temp array
101 if (n == 0) then
102 do j = 0, m
103 do i = 1, sys_size
104 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, &
105 & 0) + 2._wp*q_prim_vf(i)%sf(j, 0, 0))
106 end do
107 end do
108 else if (p == 0) then
109 do k = 0, n
110 do j = 0, m
111 do i = 1, sys_size
112 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, &
113 & 0) + q_prim_vf(i)%sf(j, k + 1, 0) + q_prim_vf(i)%sf(j, k - 1, &
114 & 0) + 4._wp*q_prim_vf(i)%sf(j, k, 0))
115 end do
116 end do
117 end do
118 else
119 do l = 0, p
120 do k = 0, n
121 do j = 0, m
122 do i = 1, sys_size
123 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, &
124 & k, l) + q_prim_vf(i)%sf(j, k + 1, l) + q_prim_vf(i)%sf(j, k - 1, &
125 & l) + q_prim_vf(i)%sf(j, k, l + 1) + q_prim_vf(i)%sf(j, k, &
126 & l - 1) + 6._wp*q_prim_vf(i)%sf(j, k, l))
127 end do
128 end do
129 end do
130 end do
131 end if
132
133 ! Copy smoothed data back to array of scalar fields
134 do l = 0, p
135 do k = 0, n
136 do j = 0, m
137 do i = 1, sys_size
138 q_prim_vf(i)%sf(j, k, l) = q_prim_temp(j, k, l, i)
139 end do
140 end do
141 end do
142 end do
143 end do
144
145 end subroutine s_elliptic_smoothing
146
147 !> Perturb velocity and volume fraction fields using multi-octave simplex noise.
148 subroutine s_perturb_simplex(q_prim_vf)
149
150 type(scalar_field), dimension(sys_size), intent(inout) :: q_prim_vf
151 real(wp) :: mag, freq, scale, vel_rsm
152 real(wp), dimension(:,:), allocatable :: ofs
153 integer :: nOffsets
154 real(wp) :: xl, yl, zl
155 integer :: i, j, k, l, q
156
157 noffsets = max(num_dims, num_fluids)
158
159 allocate (ofs(noffsets, num_dims))
160
161 ! Store offsets
162 do i = 1, num_dims
163 do j = 1, num_dims
164 ofs(j, i) = simplex_params%perturb_vel_offset(j, i)
165 end do
166 end do
167
168 ! Perturb velocities
169 do i = 1, num_dims
170 if (simplex_params%perturb_vel(i)) then
171 freq = simplex_params%perturb_vel_freq(i)
172 scale = simplex_params%perturb_vel_scale(i)
173 do l = 0, p
174 do k = 0, n
175 do j = 0, m
176 xl = freq*(x_cc(j) + ofs(i, 1))
177 yl = freq*(y_cc(k) + ofs(i, 2))
178 if (num_dims == 2) then
179 mag = f_simplex2d(xl, yl)
180 else if (num_dims == 3) then
181 zl = freq*(z_cc(l) + ofs(i, 3))
182 mag = f_simplex3d(xl, yl, zl)
183 end if
184
185 vel_rsm = 0._wp
186 do q = 1, num_dims
187 vel_rsm = vel_rsm + q_prim_vf(momxb + q - 1)%sf(j, k, l)**2._wp
188 end do
189 vel_rsm = sqrt(vel_rsm)
190
191 q_prim_vf(momxb + i - 1)%sf(j, k, l) = q_prim_vf(momxb + i - 1)%sf(j, k, 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(contxb + i - 1)%sf(j, k, l) = q_prim_vf(contxb + i - 1)%sf(j, k, &
222 & l) + q_prim_vf(contxb + 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(momxb)%sf(j, r, l) = q_prim_vf(momxb)%sf(j, r, l) + velfluc(1)
302 q_prim_vf(momxb + 1)%sf(j, r, l) = q_prim_vf(momxb + 1)%sf(j, r, l) + velfluc(2)
303 q_prim_vf(momxb + 2)%sf(j, r, l) = q_prim_vf(momxb + 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 integer :: i
358
359 seed = mod(modmul(seed), modulus)
360 var = seed/real(modulus, wp)
361
362 end subroutine s_prng
363
364 !> Compute a modular multiplication step for the linear congruential pseudo-random number generator.
365 function modmul(a) result(val)
366
367 integer, intent(in) :: a
368 integer :: val
369 real(wp) :: x, y
370
371 x = (multiplier/real(modulus, wp))*a + (increment/real(modulus, wp))
372 y = nint((x - floor(x))*decimal_trim)/decimal_trim
373 val = nint(y*modulus)
374
375 end function modmul
376
377 !> Deallocate the temporary primitive variable array used by elliptic smoothing.
379
380 if (elliptic_smoothing) then
381 deallocate (q_prim_temp)
382 end if
383
384 end subroutine s_finalize_perturbation_module
385
386end 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)
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 perturb_flow_fluid
Fluid to be perturbed with perturb_flow flag.
type(int_bounds_info) mom_idx
Indexes of first & last momentum eqns.
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).
integer e_idx
Index of total energy equation.
real(wp) mixlayer_vel_coef
Coefficient for the hyperbolic tangent streamwise velocity profile.
integer alf_idx
Index of void fraction.
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.
impure subroutine s_elliptic_smoothing(q_prim_vf, bc_type)
Iteratively smooth all primitive variable fields using a discrete elliptic (Laplacian) filter.
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_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).