-
Notifications
You must be signed in to change notification settings - Fork 1
/
gen_sim.py
107 lines (87 loc) · 3.14 KB
/
gen_sim.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import numpy as np
import shutil
import sys
import cv2
import os
import pathlib
from fluidsim.solvers.ns2d.solver import Simul
from fluidsim import load_sim_for_plot, load_state_phys_file
from matplotlib import pyplot as plt
from PIL import Image
params = Simul.create_default_params()
# Delete the results folder
#shutil.rmtree(sim.output.path_run)
# air viscosity at 25 Cº
# (https://www.engineersedge.com/physics/viscosity_of_air_dynamic_and_kinematic_14483.htm)
#params.nu_2 = 1.562e-3 #
#params.nu_4 = 2.468e-5
#params.nu_8 = 1.032e-4
#params.nu_m4 = 4.562e-5
# Simulation workspace and simulation time
params.time_stepping.t_end = 120
params.time_stepping.it_end = 50
params.oper.nx = 256
params.oper.ny = 256
params.oper.Lx = 10
params.oper.Ly = 10
params.forcing.enable = True
params.forcing.type = 'tcrandom_anisotropic'
#if params.forcing.type == 'tcrandom_anisotropic':
# params.forcing.tcrandom_anisotropic.angle = np.pi * np.random.rand() / 2
params.forcing.nkmin_forcing = 0
params.forcing.nkmax_forcing = 2
#params.forcing.type = 'milestone'
#params.forcing.milestone.objects.number = np.random.randint(2, 5)
#params.forcing.milestone.objects.diameter = np.random.rand() + 1.5
'''forcing_type = ['proportional', 'tcrandom', 'tcrandom_anisotropic']
#forcing_type = ['tcrandom', 'tcrandom_anisotropic']
#forcing_idx = np.random.randint(len(forcing_type))
#params.forcing.type = forcing_type[forcing_idx]
number_cylinders = 3
params.forcing.type = 'milestone'
params.forcing.milestone.nx_max = min(
params.oper.nx, round(32 * number_cylinders * params.oper.nx / params.oper.ny)
)
objects = params.forcing.milestone.objects
objects.number = number_cylinders'''
# Physics parameters
Re = np.random.rand() * 10 ** (np.random.randint(12, 14))
V0 = 10.0
L = 1
params.nu_2 = V0 * L / Re # Kinematic Viscosity
delta_x = params.oper.Lx / params.oper.nx
exp_f = 2 * np.random.rand() + 6.0
params.forcing.forcing_rate = 1.0
params.nu_8 = 0.2 / 3.0 * delta_x**exp_f
# Init conditions
params.init_fields.type = 'dipole'
params.init_fields.noise.velo_max = 12
params.init_fields.noise.length = 2 * np.random.rand() + 4.0
params.init_fields.modif_after_init = True
# Output parameters
params.output.periods_save.spatial_means = 1.0
params.output.periods_save.spectra = 1.
params.output.periods_save.phys_fields = 0.1
sim = Simul(params)
if sys.argv[1] == 'sim':
sim.time_stepping.start()
else:
sim = load_state_phys_file(sim.output.path_run)
FPS = 10
BASE_FOLDER = pathlib.Path(f'{sim.output.path_run}_video')
BASE_FOLDER.mkdir(parents=True, exist_ok=True)
video_name = BASE_FOLDER / 'video.mp4'
fourcc = cv2.VideoWriter_fourcc(*'MP4V')
#for t in range(params.time_stepping.it_end + 1):
for i, t in enumerate(np.linspace(0.0, params.time_stepping.t_end, params.time_stepping.t_end * FPS)):
img_filename = BASE_FOLDER / f'step_{t:.4f}.png'
sim.output.phys_fields.plot(time=t)
plt.savefig(img_filename)
frame = Image.open(img_filename)
if i == 0:
video = cv2.VideoWriter(str(video_name.resolve()), fourcc, FPS, frame.size)
video.write(np.array(frame)[..., 0:3])
plt.close()
print(f'Processed frame {t}.')
cv2.destroyAllWindows()
video.release()