Skip to content

Commit 2cdc78f

Browse files
committed
Merge branch 'topic/default/plot-forcing-region' into 'branch/default'
fluidsim/base/forcing/specific.py: add plot_forcing_region function to tc_random See merge request fluiddyn/fluidsim!455
2 parents 803954b + d98e7fc commit 2cdc78f

File tree

5 files changed

+71
-22
lines changed

5 files changed

+71
-22
lines changed

fluidsim/base/forcing/anisotropic.py

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,10 @@ def plot_forcing_region(self):
264264
ax.set_aspect("equal")
265265

266266
title = (
267-
pforcing.type
268-
+ "; "
269-
+ rf"$nk_{{min}} = {pforcing.nkmin_forcing} \delta k_v$; "
270-
+ rf"$nk_{{max}} = {pforcing.nkmax_forcing} \delta k_v$; "
271-
+ "\n"
267+
f"{pforcing.type}; "
268+
rf"$nk_{{min}} = {pforcing.nkmin_forcing} \delta k_v$; "
269+
rf"$nk_{{max}} = {pforcing.nkmax_forcing} \delta k_v$;"
270+
"\n"
272271
+ r"$\theta_f = {:.0f}^\circ$; ".format(degrees(self.angle))
273272
+ rf"Forced modes = {self.nb_forced_modes}"
274273
)
@@ -458,23 +457,10 @@ def plot_forcing_region(self):
458457
ax.text(loc_label_x, loc_label_y, r"$\delta \theta_f$")
459458

460459
# Plot forced modes in red
461-
indices_forcing = np.argwhere(self.COND_NO_F == False)
460+
indices_forcing = np.argwhere(~self.COND_NO_F)
462461
for i, index in enumerate(indices_forcing):
463-
if ndim == 2:
464-
ax.plot(
465-
Kh[0, index[1]],
466-
Kv[index[0], 0],
467-
"ro",
468-
label="Forced mode" if i == 0 else "",
469-
)
470-
else:
471-
ax.plot(
472-
Kh[0, index[1], index[2]],
473-
Kv[index[0], 0, 0],
474-
"ro",
475-
label="Forced mode" if i == 0 else "",
476-
)
477-
462+
label = "Forced mode" if i == 0 else None
463+
ax.plot(Kh[*index], Kv[*index], "ro", label=label)
478464
ax.grid(linestyle="--", alpha=0.4)
479465
ax.legend()
480466

fluidsim/base/forcing/specific.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@
4848
from pathlib import Path
4949

5050
import numpy as np
51+
import matplotlib.pyplot as plt
52+
import matplotlib.patches as patches
5153

5254
from fluiddyn.util import mpi
5355
from fluiddyn.calcul.easypyfft import fftw_grid_size
@@ -866,6 +868,61 @@ def __init__(self, sim):
866868
else:
867869
self.period_change_f0f1 = time_correlation
868870

871+
def plot_forcing_region(self):
872+
"""Plots the forcing region"""
873+
pforcing = self.params.forcing
874+
kf_max = self.kmax_forcing
875+
876+
try:
877+
self.params.oper.nz
878+
except AttributeError:
879+
ndim = 2
880+
else:
881+
ndim = 3
882+
883+
if ndim == 2:
884+
Kh = self.oper_coarse.KX
885+
Kv = self.oper_coarse.KY
886+
deltakv = self.oper.deltaky
887+
else:
888+
Kh = np.sqrt(self.oper_coarse.Kx**2 + self.oper_coarse.Ky**2)
889+
Kv = self.oper_coarse.Kz
890+
deltakv = self.oper.deltakz
891+
892+
fig, ax = plt.subplots()
893+
ax.set_aspect("equal")
894+
895+
title = (
896+
f"{pforcing.type}; "
897+
rf"$nk_{{min}} = {pforcing.nkmin_forcing} \delta k_v$; "
898+
rf"$nk_{{max}} = {pforcing.nkmax_forcing} \delta k_v$;"
899+
f"\nForced modes = {self.nb_forced_modes}"
900+
)
901+
902+
ax.set_title(title)
903+
ax.set_xlabel(r"$k_h$")
904+
ax.set_ylabel(r"$k_z$")
905+
906+
# Parameters figure
907+
908+
# Set limits to 120% of the kf_max
909+
factor = 1.2
910+
ax.set_xlim([0.0, factor * kf_max])
911+
ax.set_ylim([0.0, factor * kf_max])
912+
913+
xticks = np.arange(0.0, factor * kf_max, deltakv)
914+
yticks = np.arange(0.0, factor * kf_max, deltakv)
915+
ax.set_xticks(xticks)
916+
ax.set_yticks(yticks)
917+
918+
# Plot forced modes in red
919+
indices_forcing = np.argwhere(~self.COND_NO_F)
920+
for i, index in enumerate(indices_forcing):
921+
label = "Forced mode" if i == 0 else ""
922+
ax.plot(Kh[*index], Kv[*index], "ro", label=label)
923+
ax.grid(linestyle="--", alpha=0.4)
924+
ax.legend()
925+
869926
def forcingc_raw_each_time(self, a_fft):
870927
"""Return a coarse forcing as a linear combination of 2 random arrays
871928

fluidsim/solvers/ns2d/strat/test_solver.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ def test_(self):
114114
P_tot = means["PK_tot"] + means["PA_tot"]
115115
assert np.allclose(P_tot, self.sim.params.forcing.forcing_rate)
116116

117+
if mpi.nb_proc == 1:
118+
self.sim.forcing.forcing_maker.plot_forcing_region()
119+
117120

118121
class TestForcingConstantRateEnergyAP(TestForcingConstantRateEnergy):
119122
@classmethod

fluidsim/solvers/ns3d/test_solver.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ def test_forcing(self):
135135
assert kx_max >= kf_max
136136
assert ky_max >= kf_max
137137

138+
if mpi.nb_proc == 1:
139+
sim.forcing.forcing_maker.plot_forcing_region()
140+
138141

139142
class TestOutput(TestSimulBase):
140143
@classmethod

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,5 +269,5 @@ showcontent = true
269269

270270
[tool.ruff]
271271
line-length = 82
272-
target-version = "py39"
272+
target-version = "py311"
273273
exclude = [".venv", "build", ".*", "**/__py*__", "**/__numba__", "doc/_build", "**/.ipynb_checkpoints"]

0 commit comments

Comments
 (0)