Debian Codegraph @

examples/simulations/gray-scott/catalyst/gs-pipeline.py

raw
1 # SPDX-FileCopyrightText: 2026 Oak Ridge National Laboratory and Contributors
2 #
3 # SPDX-License-Identifier: Apache-2.0
4
5 import argparse
6 from paraview.simple import *
7
8 from paraview import print_info
9
10 # ------------------------------------------------------------------------------
11 # Catalyst options
12 from paraview import catalyst
13
14 options = catalyst.Options()
15 options.GlobalTrigger = "TimeStep"
16 options.EnableCatalystLive = 1
17 options.CatalystLiveTrigger = "TimeStep"
18 options.ExtractsOutputDirectory = "/tmp"
19 # options.ExtractsOutputDirectory = '.'
20
21 DISABLE_EXTRACTOR = False
22
23
24 # setup and returns the view
25 def SetupRenderView():
26 # Create a new 'Render View'
27 view = CreateView("RenderView")
28
29 camera = GetActiveCamera()
30 camera.Azimuth(45)
31 camera.Elevation(45)
32
33 SetActiveView(None)
34
35 # create new layout object 'Layout #1'
36 layout1 = CreateLayout(name="Layout #1")
37 layout1.AssignView(0, view)
38 layout1.SetSize(824, 656)
39
40 # restore active view
41 SetActiveView(view)
42 return view
43
44
45 # A different proxy type is used depending on whether you're using
46 # catalyst, or doing post hoc visualization
47 # this returns the catalyst producer
48 def SetupCatalystProducer():
49 producer = TrivialProducer(registrationName="fides")
50 return producer
51
52
53 # this returns the fides proxy which should be used for post hoc vis
54 # i.e., you're reading .bp files
55 def SetupFidesReader(json, bp, sst):
56 if json is None:
57 # in this case the bp file must contain the Fides attributes
58 fides = FidesReader(StreamSteps=1, FileName=bp)
59 return fides
60
61 fides = FidesJSONReader(StreamSteps=1, FileName=json)
62 if sst:
63 fides.DataSourceEngines = ["source", "SST"]
64 # 'source' is the name of the ADIOS data source in the JSON data model
65 fides.DataSourcePath = ["source", bp]
66 # required to update the fides reader
67 fides.UpdatePipelineInformation()
68 return fides
69
70
71 # takes in a producer and view and sets up the visualization pipeline
72 def SetupVisPipeline(producer, view):
73 Show(producer, view, "UniformGridRepresentation")
74 view.ResetCamera()
75
76 # get color transfer function/color map for 'U'
77 uLUT = GetColorTransferFunction("V")
78 uLUT.AutomaticRescaleRangeMode = "Clamp and update every timestep"
79 uLUT.RescaleOnVisibilityChange = 1
80
81 contour1 = Contour(registrationName="Contour1", Input=producer)
82 contour1.ContourBy = ["POINTS", "V"]
83 contour1.Isosurfaces = [0.1, 0.3, 0.5, 0.7]
84 contour1.PointMergeMethod = "Uniform Binning"
85
86 contour1Display = Show(contour1, view, "GeometryRepresentation")
87
88 contour1Display.Representation = "Surface"
89 contour1Display.ColorArrayName = ["POINTS", "V"]
90
91 Hide(producer, view)
92
93 # trace defaults for the display properties.
94 contour1Display.Representation = "Surface"
95 contour1Display.ColorArrayName = ["POINTS", "V"]
96 contour1Display.SetScaleArray = ["POINTS", "V"]
97 contour1Display.ScaleTransferFunction = "PiecewiseFunction"
98 contour1Display.LookupTable = uLUT
99
100 uLUTColorBar = GetScalarBar(uLUT, view)
101 uLUTColorBar.Title = "V"
102 uLUTColorBar.ComponentTitle = ""
103
104 # show color legend
105 contour1Display.SetScalarBarVisibility(view, True)
106 return contour1, contour1Display
107
108
109 # sets up an extractor for writing out PNG images
110 def SetupExtractor(view):
111 # create extractor
112 pNG1 = CreateExtractor("PNG", view, registrationName="PNG1")
113 # trace defaults for the extractor.
114 pNG1.Trigger = "TimeStep"
115
116 # init the 'PNG' selected for 'Writer'
117 pNG1.Writer.FileName = "output_{timestep:06d}.png"
118 pNG1.Writer.ImageResolution = [800, 800]
119 pNG1.Writer.Format = "PNG"
120
121
122 # Catalyst uses this to update the pipeline and print out some info on each time step
123 # you don't need to call this directly in your script; ParaView Catalyst will call it for you
124 def catalyst_execute(info):
125 print_info("in '%s::catalyst_execute'", __name__)
126 global pipeline, display
127 pipeline.UpdatePipeline()
128 display.RescaleTransferFunctionToDataRange()
129
130 print_info("executing (cycle={}, time={})".format(info.cycle, info.time))
131 print_info("U-range: {}".format(producer.PointData["U"].GetRange(0)))
132 print_info("V-range: {}".format(producer.PointData["V"].GetRange(0)))
133
134
135 def ParseArgs():
136 parser = argparse.ArgumentParser()
137 parser.add_argument(
138 "-j",
139 "--json_filename",
140 help="path to Fides JSON file",
141 type=str,
142 required=False,
143 )
144 parser.add_argument("-b", "--bp_filename", help="path to bp file", type=str, required=True)
145 parser.add_argument("--staging", help="use SST engine", action="store_true")
146 args = parser.parse_args()
147 return args
148
149
150 def StreamingVis(args):
151 # adios/fides step status
152 NotReady = 1
153 EndOfStream = 2
154
155 # setup the reader, view, pipeline
156 fides = SetupFidesReader(args.json_filename, args.bp_filename, args.staging)
157 view = SetupRenderView()
158
159 step = 0
160 while True:
161 status = NotReady
162 while status == NotReady:
163 # must call PrepareNextStep to get Fides ready to read the
164 # next step
165 fides.PrepareNextStep()
166 fides.UpdatePipelineInformation()
167 status = fides.NextStepStatus
168 if status == EndOfStream:
169 # done reading the file
170 return
171 if step == 0:
172 # set up the pipeline on the first step
173 pipeline, display = SetupVisPipeline(fides, view)
174
175 # need to update the pipeline and then save the output
176 pipeline.UpdatePipeline()
177 display.RescaleTransferFunctionToDataRange()
178 output = f"output-{step:05d}.png"
179 SaveScreenshot(output, view, ImageResolution=[800, 800])
180 step += 1
181
182
183 # ------------------------------------------------------------------------------
184 if __name__ == "__main__":
185 print("in __main__()")
186 args = ParseArgs()
187 StreamingVis(args)
188 else:
189 # in this case we're running from Catalyst
190 view = SetupRenderView()
191 producer = SetupCatalystProducer()
192 pipeline, display = SetupVisPipeline(producer, view)
193
194 # normally not needed, but a bug fix is in progress to fix an issue
195 # when using ParaView Live with extractors
196 if not DISABLE_EXTRACTOR:
197 SetupExtractor(view)