Debian Codegraph @

examples/simulations/gray-scott/plot/gsplot.py

raw
1 #!/usr/bin/env python3
2
3 # SPDX-FileCopyrightText: 2026 Oak Ridge National Laboratory and Contributors
4 #
5 # SPDX-License-Identifier: Apache-2.0
6
7 from adios2 import Adios, Stream # pylint: disable=import-error
8 import argparse
9 import numpy as np # pylint: disable=import-error
10 import matplotlib.pyplot as plt # pylint: disable=import-error
11 import matplotlib.gridspec as gridspec # pylint: disable=import-error
12 import decomp # pylint: disable=import-error
13
14
15 def SetupArgs():
16 parser = argparse.ArgumentParser()
17 parser.add_argument("--instream", "-i", help="Name of the input stream", required=True)
18 parser.add_argument("--outfile", "-o", help="Name of the output file", default="screen")
19 parser.add_argument("--varname", "-v", help="Name of variable read", default="U")
20 parser.add_argument(
21 "--nompi", "-nompi", help="ADIOS was installed without MPI", action="store_true"
22 )
23 parser.add_argument(
24 "--displaysec",
25 "-dsec",
26 help="Float representing gap between plot window refresh",
27 default=0.1,
28 )
29 parser.add_argument(
30 "--nx",
31 "-nx",
32 help="Integer representing process decomposition in the x direction",
33 default=1,
34 )
35 parser.add_argument(
36 "--ny",
37 "-ny",
38 help="Integer representing process decomposition in the y direction",
39 default=1,
40 )
41 parser.add_argument(
42 "--nz",
43 "-nz",
44 help="Integer representing process decomposition in the z direction",
45 default=1,
46 )
47 parser.add_argument(
48 "--plane",
49 "-plane",
50 help="The 2D plane to be displayed/stored xy/yz/xz/all",
51 default="yz",
52 )
53 args = parser.parse_args()
54
55 args.displaysec = float(args.displaysec)
56 args.nx = int(args.nx)
57 args.ny = int(args.ny)
58 args.nz = int(args.nz)
59
60 if args.plane not in ("xz", "yz", "xy", "all"):
61 raise ValueError("Input argument --plane must be one of xz/yz/xy/all")
62
63 return args
64
65
66 def Plot2D(plane_direction, data, args, fullshape, step, fontsize):
67 # Plotting part
68 displaysec = args.displaysec
69 gs = gridspec.GridSpec(1, 1)
70 fig = plt.figure(1, figsize=(8, 8))
71 ax = fig.add_subplot(gs[0, 0])
72 colorax = ax.imshow(
73 data,
74 origin="lower",
75 interpolation="quadric",
76 extent=[0, fullshape[1], 0, fullshape[0]],
77 cmap=plt.get_cmap("gist_ncar"),
78 )
79 cbar = fig.colorbar(colorax, orientation="horizontal")
80 cbar.ax.tick_params(labelsize=fontsize - 4)
81
82 for i in range(args.ny):
83 y = fullshape[0] / args.ny * i
84 ax.plot([0, fullshape[1]], [y, y], color="black")
85
86 for i in range(args.nx):
87 x = fullshape[1] / args.nx * i
88 ax.plot([x, x], [0, fullshape[0]], color="black")
89
90 ax.set_title(
91 "{0}, {1} plane, step {2}".format(args.varname, plane_direction, step),
92 fontsize=fontsize,
93 )
94 ax.set_xlabel(plane_direction[0], fontsize=fontsize)
95 ax.set_ylabel(plane_direction[1], fontsize=fontsize)
96 plt.tick_params(labelsize=fontsize - 8)
97 plt.ion()
98 if args.outfile == "screen":
99 plt.show()
100 plt.pause(displaysec)
101 elif args.outfile.endswith(".bp"):
102 global writer
103 # print("plot to file, step = ", step)
104 # if step == 0:
105 # writer = Stream(args.outfile, "w")
106 #
107 writer.begin_step()
108 writer.write(args.varname, data, data.shape, [0, 0], data.shape)
109 writer.end_step()
110 else:
111 imgfile = args.outfile + "{0:0>5}".format(step) + "_" + plane_direction + ".png"
112 fig.savefig(imgfile)
113
114 plt.clf()
115
116
117 def read_data(args, fr, start_coord, size_dims):
118 var1 = args.varname
119 data = fr.read(var1, start_coord, size_dims)
120 data = np.squeeze(data)
121 return data
122
123
124 if __name__ == "__main__":
125 # fontsize on plot
126 fontsize = 24
127
128 args = SetupArgs()
129 # print(args)
130
131 # Setup up 2D communicators if MPI is installed
132 mpi = decomp.MPISetup(args, 3)
133 myrank = mpi.rank["app"]
134
135 # Read the data from this object
136 adios = Adios("adios2.xml", mpi.comm_app)
137 io = adios.declare_io("SimulationOutput")
138 fr = Stream(io, args.instream, "r", mpi.comm_app)
139
140 if args.outfile.endswith(".bp"):
141 global writer
142 writer = Stream(args.outfile, "w")
143
144 # Get the ADIOS selections -- equally partition the data if parallelization is requested
145
146 # Read through the steps, one at a time
147 plot_step = 0
148 for fr_step in fr.steps():
149 # print(f"loop status = {fr_step.step_status()} "
150 # f"step = {fr.current_step()} counter={fr.loop_index()}")
151 start, size, fullshape = mpi.Partition_3D_3D(fr, args)
152 cur_step = fr_step.current_step()
153 vars_info = fr.available_variables()
154 # print (vars_info)
155 shape3_str = vars_info[args.varname]["Shape"].split(",")
156 shape3 = list(map(int, shape3_str))
157 sim_step = fr_step.read("step")
158
159 if myrank == 0:
160 print(
161 "GS Plot step {0} processing simulation output step {1} or computation"
162 "step {2}".format(plot_step, cur_step, sim_step),
163 flush=True,
164 )
165 # if cur_step == 0:
166 # print("Variable" + pdfvar + " shape is {" + vars_info[pdfvar]["Shape"]+"}")
167
168 if args.plane in ("xy", "all"):
169 data = read_data(args, fr_step, [0, 0, int(shape3[2] / 2)], [shape3[0], shape3[1], 1])
170 Plot2D("xy", data, args, fullshape, sim_step, fontsize)
171
172 if args.plane in ("xz", "all"):
173 data = read_data(args, fr_step, [0, int(shape3[1] / 2), 0], [shape3[0], 1, shape3[2]])
174 Plot2D("xz", data, args, fullshape, sim_step, fontsize)
175
176 if args.plane in ("yz", "all"):
177 data = read_data(args, fr_step, [int(shape3[0] / 2), 0, 0], [1, shape3[1], shape3[2]])
178 Plot2D("yz", data, args, fullshape, sim_step, fontsize)
179 plot_step = plot_step + 1
180
181 fr.close()
182 if args.outfile.endswith(".bp"):
183 writer.close()