2D fields¶
This page demonstrates 2D visualization, including streamlines and contours.
The data is taken from the AmbipolarWind test setup (https://github.com/idefix-code/idefix/tree/master/test/MHD/AmbipolarWind)
1from idefix2python import RunContext, Pipeline, Fig, MapMovie2D
2import numpy as np
3from pathlib import Path
4
5projectPath = Path(__file__).parent / "data_examples"
6task = "2D_test"
7
8
9def compute_mach_p(v):
10 data = v.data
11 cs2 = data["PRS"] / data["RHO"]
12 return np.sqrt(data["VX1"] ** 2 + data["VX2"] ** 2) / cs2
13
14
15quantities = [
16 MapMovie2D(
17 "RHO",
18 r"$\rho$",
19 plot_coords=[0, 0],
20 title="Density",
21 streamlines=["VX1", "VX2"],
22 ),
23 # Create a computed field for Mach_p and draw a contour at Mach_p = 1
24 MapMovie2D(
25 "Mach_p",
26 r"$\mathcal{M}_p$",
27 plot_coords=[0, 1],
28 title="Poloidal Mach Number",
29 compute=compute_mach_p,
30 contours=[1],
31 contour_color="green",
32 ),
33]
34fig0 = Fig(quantities, suptitle="Density and Mach number on a heatmap")
35runContext = RunContext(task, projectPath)
36
37if __name__ == "__main__":
38 pipeline = Pipeline(runContext, [fig0])
39 pipeline.run()
2D_test_fig0.mp4
The result is not very good because the pipeline automatically computed the bounds of the colorbar by looking at the minimum/maximum values all over the simulation. Also, you might want a different colormap.
There are two ways to add bounds:
Passing
vminandvmaxarguments to the quantities (see the Particle quantities page for an example).Adding a
config.jsonfile.
1from idefix2python import RunContext, Pipeline, Fig, MapMovie2D
2import numpy as np
3from pathlib import Path
4
5projectPath = Path(__file__).parent / "data_examples"
6task = "2D_test"
7configPath = projectPath / "config.json"
8
9
10def compute_mach_p(v):
11 data = v.data
12 cs2 = data["PRS"] / data["RHO"]
13 return np.sqrt(data["VX1"] ** 2 + data["VX2"] ** 2) / cs2
14
15
16quantities = [
17 MapMovie2D(
18 "RHO",
19 r"$\rho$",
20 plot_coords=[0, 0],
21 title="Density",
22 streamlines=["VX1", "VX2"],
23 ),
24 # Create a computed field for Mach_p and draw a contour at Mach_p = 1
25 MapMovie2D(
26 "Mach_p",
27 r"$\mathcal{M}_p$",
28 plot_coords=[0, 1],
29 title="Poloidal Mach Number",
30 compute=compute_mach_p,
31 contours=[1],
32 contour_color="green",
33 ),
34]
35fig0 = Fig(quantities, suptitle="Density and Mach number on a beautiful heatmap")
36
37runContext = RunContext(
38 task, projectPath, configPath=configPath, custom_name="2D_test_config"
39)
40
41
42if __name__ == "__main__":
43 pipeline = Pipeline(runContext, [fig0])
44 pipeline.run()
config.json¶
1{
2 "RHO": {
3 "norm": "log",
4 "bounds": [
5 1e-7,
6 1
7 ],
8 "style_kwargs": {
9 "cmap": "inferno"
10 }
11 },
12 "Mach_p": {
13 "bounds": [
14 0.1,
15 20
16 ],
17 "style_kwargs": {
18 "cmap": "cool"
19 },
20 "norm": "log"
21 }
22}
2D_test_fig0.mp4
1from idefix2python import RunContext, Pipeline, Fig, MapMovie2D
2import numpy as np
3from pathlib import Path
4
5projectPath = Path(__file__).parent / "data_examples"
6task = "2D_test"
7configPath = projectPath / "config.json"
8
9
10# If we want to show only the z>0 part of the disk
11def zoom(x1, x2):
12 # In this run geometry is spherical so x1, x2 correspond to r, theta
13 return np.ones_like(x1, dtype=bool), x2 <= np.pi / 2
14
15
16def compute_mach_p(v):
17 data = v.data
18 cs2 = data["PRS"] / data["RHO"]
19 return np.sqrt(data["VX1"] ** 2 + data["VX2"] ** 2) / cs2
20
21
22quantities = [
23 MapMovie2D(
24 "RHO",
25 r"$\rho$",
26 plot_coords=[0, 0],
27 title="Density",
28 streamlines=["VX1", "VX2"],
29 ),
30 # Create a computed field for Mach_p and draw a contour at Mach_p = 1
31 MapMovie2D(
32 "Mach_p",
33 r"$\mathcal{M}_p$",
34 plot_coords=[0, 1],
35 title="Poloidal Mach Number",
36 compute=compute_mach_p,
37 contours=[1],
38 contour_color="green",
39 ),
40]
41fig0 = Fig(quantities, suptitle="Density and Mach number on a beautiful heatmap")
42
43runContext = RunContext(
44 task, projectPath, configPath=configPath, zoom=zoom, custom_name="2D_test_zoom"
45)
46
47
48if __name__ == "__main__":
49 pipeline = Pipeline(runContext, [fig0])
50 pipeline.run()
2D_test_zoom_fig0_config.mp4