def WriteImages(self, datadescription, rescale_lookuptable=False): """This method will update all views, if present and write output images, as needed.""" timestep = datadescription.GetTimeStep() cinema_dirs = [] for view in self.__ViewsList: if (view.cpFrequency and timestep % view.cpFrequency == 0) or \ datadescription.GetForceOutput() == True: fname = view.cpFileName fname = fname.replace("%t", str(timestep)) if view.cpFitToScreen != 0: if view.IsA("vtkSMRenderViewProxy") == True: view.ResetCamera() elif view.IsA("vtkSMContextViewProxy") == True: view.ResetDisplay() else: print(' do not know what to do with a ', view.GetClassName()) view.ViewTime = datadescription.GetTime() if rescale_lookuptable: self.RescaleDataRange(view, datadescription.GetTime()) cinemaOptions = view.cpCinemaOptions if cinemaOptions and 'camera' in cinemaOptions: if 'composite' in view.cpCinemaOptions and view.cpCinemaOptions[ 'composite'] == True: dirname = self.UpdateCinema(view, datadescription, specLevel="B") else: dirname = self.UpdateCinema(view, datadescription, specLevel="A") if dirname: cinema_dirs.append(dirname) else: # for png quality = 0 means no compression. compression can be a potentially # very costly serial operation on process 0 if fname.endswith('png'): simple.SaveScreenshot( fname, view, magnification=view.cpMagnification, quality=0) else: simple.SaveScreenshot( fname, view, magnification=view.cpMagnification) if len(cinema_dirs) > 1: import paraview.cinemaIO.pv_introspect as pv_introspect pv_introspect.make_workspace_file("cinema", cinema_dirs)
def writeLightArray(self, path, source): rep = simple.Show(source, self.view) rep.Representation = 'Surface' rep.DiffuseColor = [1, 1, 1] simple.ColorBy(rep, ('POINTS', None)) # Grab data tmpFileName = path + '__.png' self.view.LockBounds = 1 simple.SaveScreenshot(tmpFileName, self.view) self.view.LockBounds = 0 if self.canWrite: # Convert data self.reader.SetFileName(tmpFileName) self.reader.Update() rgbArray = self.reader.GetOutput().GetPointData().GetArray(0) arraySize = rgbArray.GetNumberOfTuples() rawArray = vtkUnsignedCharArray() rawArray.SetNumberOfTuples(arraySize) for idx in range(arraySize): light = rgbArray.GetTuple3(idx)[0] rawArray.SetTuple1(idx, light) with open(path, 'wb') as f: f.write(buffer(rawArray)) # Delete temporary file if self.cleanAfterMe: os.remove(tmpFileName) simple.Hide(source, self.view)
def AdjustCameraAndSave(renderViewIn, Output, ImageResolution=(2048, 2048), CamDirVec=None, CamUpVec=None): # Set Defaults if CamDirVec is not None: CamDirVec = np.array(CamDirVec) * 1.0 else: CamDirVec = np.array([0.0, 0.0, 1.0]) if CamUpVec is not None: CamUpVec = np.array(CamUpVec) * 1.0 else: CamUpVec = np.array([0.0, 1.0, 0.0]) # - Adjusts Camera Direction renderViewIn.CameraFocalPoint.SetData([0.0, 0.0, 0.0]) renderViewIn.CameraPosition.SetData(CamDirVec) renderViewIn.CameraViewUp = CamUpVec renderViewIn.CameraPosition = [700, 700, 2000] renderViewIn.CameraFocalPoint = [700, 700, 500] renderViewIn.CameraParallelProjection = 1 renderViewIn.CameraParallelScale = 700 #renderViewIn.ResetCamera() # - Adjusts Background renderViewIn.Background = [1.0, 1.0, 1.0] # - Save pvs.SaveScreenshot(Output, viewOrLayout=renderViewIn, ImageResolution=ImageResolution)
def SaveAnimation(steps, renderView, sources_ft, timearrays, pattern="a_{:}.png", force=False): for index,step in enumerate(steps): outfile = pattern.format(step) if os.path.isfile(outfile) and not force: Log("skip existing {:}".format(outfile)) continue open(outfile, 'a').close() SetTimeStep(index, sources_ft, timearrays) Log("{:}/{:}: {:}".format(index + 1, len(steps), outfile)) para.SaveScreenshot(outfile, renderView)
def SaveAnimation(renderView, sources_ft, timearrays, pattern="a_{:}.png"): steps = GetArgSteps() for index, step in enumerate(steps): fout = pattern.format(step) if os.path.isfile(fout): Log("skip existing {:}".format(fout)) continue SetTimeStep(index, sources_ft, timearrays) Log("{:}/{:}: {:}".format(index + 1, len(steps), fout)) para.SaveScreenshot(fout, renderView)
def importDataset(dataDir, datafile, description, autoApply=True): if not os.path.exists(datafile): print "Data file \"%s\" does not exist" % datafile return basename = os.path.basename(datafile) filedir = os.path.join(dataDir, basename) os.mkdir(filedir) shutil.copyfile(datafile, os.path.join(filedir, basename)) result = { 'name': basename, 'size': humanReadableSize(os.path.getsize(datafile)), 'description': description, 'thumbnails': [], 'autoApply': autoApply, 'data': { 'file': basename, 'bounds': None, 'arrays': [], 'time': [], }, } reader = simple.OpenDataFile(datafile) rep = simple.Show(reader) rep.Visibility = 1 view = simple.Render() view.ViewSize = [400, 400] ds = reader.GetClientSideObject().GetOutputDataObject(0) pointArrayMap = {} cellArrayMap = {} loadArrayDataMultiBlock(ds, pointArrayMap, cellArrayMap) bounds = getBounds(ds) if 'TimestepValues' in reader.ListProperties() and len(reader.TimestepValues) > 0: for idx in range(len(reader.TimestepValues)): t = reader.TimestepValues[idx] result['data']['time'].append({ 'idx': idx, 'value': t }) reader.UpdatePipeline(t) ds = reader.GetClientSideObject().GetOutputDataObject(0) loadArrayDataMultiBlock(ds, pointArrayMap, cellArrayMap) newBounds = getBounds(ds) bounds = unionBounds(bounds, newBounds) result['data']['arrays'] = pointArrayMap.values() + cellArrayMap.values() result['data']['bounds'] = bounds tnpath = os.path.join(filedir, 'thumbnail0.png') simple.SaveScreenshot(tnpath, view) result['thumbnails'].append('thumbnail0.png') with open(os.path.join(filedir, 'index.json'), 'w') as fp: json.dump(result, fp)
PV_routines.Save_PV_data_to_picture_file( "Clip_VTK_data_to_VTK_" + "FiniteElementsOnSphere" + '_0.vtu', "ResultField", 'NODES', "Clip_VTK_data_to_VTK_" + "FiniteElementsOnSphere") # Plot over slice circle finiteElementsOnSphere_0vtu = pvs.XMLUnstructuredGridReader( FileName=["FiniteElementsOnSphere" + '_0.vtu']) slice1 = pvs.Slice(Input=finiteElementsOnSphere_0vtu) slice1.SliceType.Normal = [0.5, 0.5, 0.5] renderView1 = pvs.GetActiveViewOrCreate('RenderView') finiteElementsOnSphere_0vtuDisplay = pvs.Show(finiteElementsOnSphere_0vtu, renderView1) pvs.ColorBy(finiteElementsOnSphere_0vtuDisplay, ('POINTS', 'ResultField')) slice1Display = pvs.Show(slice1, renderView1) pvs.SaveScreenshot("./FiniteElementsOnSphere" + "_Slice" + '.png', magnification=1, quality=100, view=renderView1) plotOnSortedLines1 = pvs.PlotOnSortedLines(Input=slice1) lineChartView2 = pvs.CreateView('XYChartView') plotOnSortedLines1Display = pvs.Show(plotOnSortedLines1, lineChartView2) plotOnSortedLines1Display.UseIndexForXAxis = 0 plotOnSortedLines1Display.XArrayName = 'arc_length' plotOnSortedLines1Display.SeriesVisibility = ['ResultField (1)'] pvs.SaveScreenshot("./FiniteElementsOnSphere" + "_PlotOnSortedLine_" + '.png', magnification=1, quality=100, view=lineChartView2) pvs.Delete(lineChartView2) print("Integral of the numerical solution", my_ResultField.integral(0)) print(
# This is how you can run this script: # pvpython Test_Simple.py import paraview.simple as pv pv.Sphere() pv.Show() pv.SaveScreenshot("Simple.png") print( "Rendering done. You should find an image file named `Simple.png` that shows a sphere." )
def gen_screenshot(state, dest=OUTPUT_DIR): for i, view in enumerate(simple.GetViews()): simple.SaveScreenshot(f"{dest}/{state.stem}_{i}.png", view) print(f"{state}: view #{i} saved") simple.ResetSession()
renderView.CameraViewUp = [0.76, 0.44, 0.49] # Define a colour look-up table using (data value, r, g, b) tuples # Use value range from first timestep lut = pvs.GetColorTransferFunction('pressure') valueRange = data.CellData['pressure'].GetRange() lut.RGBPoints = [ valueRange[0], 0.23, 0.30, 0.75, valueRange[1], 0.71, 0.02, 0.15 ] # Show clip filter in the render view, render grid surfaces, # and colour surfaces using the pressure field and our LUT clipDisplay = pvs.Show(clip, renderView) clipDisplay.Representation = 'Surface With Edges' clipDisplay.ColorArrayName = ['CELLS', 'pressure'] clipDisplay.LookupTable = lut # Loop over time steps and render an image for each one timeSteps = data.TimestepValues for istep in range(0, len(timeSteps)): print("Rendering time step %i" % istep) # Set time step and render image renderView.ViewTime = timeSteps[istep] pvs.Render() # Write image file istepstr = "%i" % istep pvs.SaveScreenshot("lfric_clip_" + istepstr.zfill(2) + ".png")
# Even if you start multiple pvbatch using MPI, this script is only # executed by rank 0. Check this assumption. assert mpi4py.MPI.COMM_WORLD.rank == 0 # Output directory provided on command line. outdir = sys.argv[1] # Render a cone. pv.Cone() pv.Show() pv.Render() print("rendered") # PNG image (serial). filename = "%s/cone.png" % outdir pv.SaveScreenshot(filename) print(filename) # Legacy VTK file (ASCII, serial). filename = "%s/cone.vtk" % outdir pv.SaveData(filename, FileType="Ascii") print(filename) # XML VTK files (parallel). filename = ("%s/cone.pvtp" % outdir) writer = pv.XMLPPolyDataWriter(FileName=filename) writer.UpdatePipeline() print(filename) # Done. print("done")
def WriteImages(self, datadescription, rescale_lookuptable=False): """This method will update all views, if present and write output images, as needed.""" timestep = datadescription.GetTimeStep() cinema_dirs = [] for view in self.__ViewsList: if (view.cpFrequency and timestep % view.cpFrequency == 0) or \ datadescription.GetForceOutput() == True: fname = view.cpFileName fname = fname.replace("%t", str(timestep)) if view.cpFitToScreen != 0: if view.IsA("vtkSMRenderViewProxy") == True: view.ResetCamera() elif view.IsA("vtkSMContextViewProxy") == True: view.ResetDisplay() else: print ' do not know what to do with a ', view.GetClassName( ) view.ViewTime = datadescription.GetTime() if rescale_lookuptable: self.RescaleDataRange(view, datadescription.GetTime()) cinemaOptions = view.cpCinemaOptions if cinemaOptions and 'camera' in cinemaOptions: dirname = None if 'composite' in cinemaOptions: dirname = self.UpdateCinemaComposite( view, datadescription) else: dirname = self.UpdateCinema(view, datadescription) if dirname: cinema_dirs.append(dirname) else: # for png quality = 0 means no compression. compression can be a potentially # very costly serial operation on process 0 if fname.endswith('png'): simple.SaveScreenshot( fname, view, magnification=view.cpMagnification, quality=0) else: simple.SaveScreenshot( fname, view, magnification=view.cpMagnification) if len(cinema_dirs) > 1: workspace = open('cinema/info.json', 'w') workspace.write('{\n') workspace.write(' "metadata": {\n') workspace.write(' "type": "workbench"\n') workspace.write(' },\n') workspace.write(' "runs": [\n') for i in range(0, len(cinema_dirs)): workspace.write(' {\n') workspace.write(' "title": "%s",\n' % cinema_dirs[i]) workspace.write(' "description": "%s",\n' % cinema_dirs[i]) workspace.write(' "path": "%s"\n' % cinema_dirs[i]) if i + 1 < len(cinema_dirs): workspace.write(' },\n') else: workspace.write(' }\n') workspace.write(' ]\n') workspace.write('}\n') workspace.close()
# 1. Create a virtual environment with ParaView's Python: # ``` # path/to/paraview/install/python3 -m venv ./env # ``` # 2. Install `h5py` in the environment **using ParaView's HDF5**: # ``` # . ./env/bin/activate # HDF5_DIR=path/to/paraview/install/ pip install --no-binary=h5py h5py # ``` # Note that the `HDF5_DIR` should have `include` and `lib` directories with ParaView's HDF5. # 3. Set the path below to `path/to/env/lib/python3.7/site-packages` extra_packages_path = '/data/home/nfischer/test-paraview/env/lib/python3.7/site-packages' # Then, this is how you can run this script: # pvpython Test_H5.py import paraview.simple as pv import sys sys.path.append(extra_packages_path) import h5py pv.Sphere() pv.Show() pv.SaveScreenshot("H5.png") print( "Rendering done. You should find an image file named `H5.png` that shows a sphere." )
# current camera placement for renderView1 renderView1.CameraPosition = [1.5707999467849731, 0.5, 4.076661427044123] renderView1.CameraFocalPoint = [1.5707999467849731, 0.5, 0.7853999733924866] renderView1.CameraParallelScale = 1.8259971497854517 # save animation pv.SaveAnimation('/home/avmo/anim.avi', renderView1, ImageResolution=[1600, 496], FrameWindow=[0, 35]) # current camera placement for renderView1 renderView1.CameraPosition = [1.5707999467849731, 0.5, 4.076661427044123] renderView1.CameraFocalPoint = [1.5707999467849731, 0.5, 0.7853999733924866] renderView1.CameraParallelScale = 1.8259971497854517 # save screenshot pv.SaveScreenshot('/home/avmo/test.png', renderView1, ImageResolution=[1600, 499], OverrideColorPalette='WhiteBackground') # create a new 'Contour' contour1 = pv.Contour(Input=ablnek5000) contour1.ContourBy = ['POINTS', 'velocity_mag'] contour1.Isosurfaces = [0.5744097200222313] contour1.PointMergeMethod = 'Uniform Binning' # set active source pv.SetActiveSource(contour1) # show data in view contour1Display = pv.Show(contour1, renderView1) # trace defaults for the display properties. contour1Display.Representation = 'Surface'
a3DText1 = pv.a3DText() a3DText1.Text = "(C) 2018 AEC, LHEP, University of Bern, Switzerland" a3DText1Display = pv.Show(a3DText1, renderView1) nc.GetColorRGB("Red", rgb) a3DText1Display.DiffuseColor = rgb a3DText1Display.Orientation = [0., 90., 180.] a3DText1Display.Position = [0., -10., 15.] nc.GetColorRGB("Grey", rgb) renderView1.Background = rgb renderView1.Update() viewAngle = 10. renderView1.CameraViewAngle = viewAngle renderView1.CameraPosition = [ (-1.1 * tpcLength / (2. * np.tan(np.deg2rad(viewAngle / 2.)))), 0., 0. ] renderView1.CameraFocalPoint = [0., 0., 0.] renderView1.CameraViewUp = [0.0, 0.0, -1.0] renderView1.ViewSize = [800, 1000] if createPlot == "view": pv.ExportView(args.plotFile, view=renderView1) elif createPlot == "screenshot": pv.SaveScreenshot(args.plotFile, magnification=2, quality=100, view=renderView1) else: pv.Interact(view=renderView1)
import pv_utils as utils import paraview.simple as pv rdaTec = 'data/rhoda.tec' rhcTec = 'data/rhohc.tec' rnpTec = 'data/rho_fld_np.tec' surfTec = 'data/rho_surf.tec' white = [1.0, 1.0, 1.0] red = [0.67, 0.0, 0.0] green = [0.0, 0.67, 0.0] gold = [1.0, 0.85, 0.0] black = [0.0, 0.0, 0.0] center = utils.GetCenter(tecFile=rdaTec) renderView = pv.CreateRenderView(ViewSize=[700, 500], Background=white) utils.ColorSurface(tecFile=rdaTec, opacity=0.5) utils.NewContour(tecFile=rdaTec, color=red, opacity=0.5) utils.NewContour(tecFile=rhcTec, color=green, opacity=0.5) utils.NewContour(tecFile=rnpTec, color=gold, opacity=1.0) utils.NewContour(tecFile=surfTec, color=black, opacity=0.5) utils.SetCameraFocus(tecFile=rdaTec) utils.SetOrientation(camPosDir=[-1.0, -0.5, 0.3]) pv.SaveScreenshot('contours2.png')
def WriteImages(self, datadescription, rescale_lookuptable=False, image_quality=None, padding_amount=0): """This method will update all views, if present and write output images, as needed. **Parameters** datadescription Catalyst data-description object rescale_lookuptable (bool, optional) If True, when all lookup tables are rescaled using current data ranges before saving the images. Defaults to False. image_quality (int, optional) If specified, should be a value in the range (0, 100) that specifies the image quality. For JPEG, 0 is low quality i.e. max compression, 100 is best quality i.e. least compression. For legacy reasons, this is inverted for PNG (which uses lossless compression). For PNG, 0 is no compression i.e maximum image size, while 100 is most compressed and hence least image size. If not specified, for saving PNGs 0 is assumed to minimize preformance impact. padding_amount (int, optional) Amount to pad the time index by. """ timestep = datadescription.GetTimeStep() cinema_dirs = [] for view in self.__ViewsList: if (view.cpFrequency and timestep % view.cpFrequency == 0) or \ datadescription.GetForceOutput() == True: fname = view.cpFileName ts = str(timestep).rjust(padding_amount, '0') fname = fname.replace("%t", ts) if view.cpFitToScreen != 0: if view.IsA("vtkSMRenderViewProxy") == True: view.ResetCamera() elif view.IsA("vtkSMContextViewProxy") == True: view.ResetDisplay() else: print(' do not know what to do with a ', view.GetClassName()) view.ViewTime = datadescription.GetTime() if rescale_lookuptable: self.RescaleDataRange(view, datadescription.GetTime()) cinemaOptions = view.cpCinemaOptions if cinemaOptions and 'camera' in cinemaOptions: if 'composite' in view.cpCinemaOptions and view.cpCinemaOptions[ 'composite'] == True: dirname = self.UpdateCinema(view, datadescription, specLevel="B") else: dirname = self.UpdateCinema(view, datadescription, specLevel="A") if dirname: cinema_dirs.append(dirname) else: if image_quality is None and fname.endswith('png'): # for png quality = 0 means no compression. compression can be a potentially # very costly serial operation on process 0 quality = 0 elif image_quality is not None: quality = int(image_quality) else: # let simple.SaveScreenshot pick a default. quality = None simple.SaveScreenshot(fname, view, magnification=view.cpMagnification, quality=quality) if len(cinema_dirs) > 1: import cinema_python.adaptors.paraview.pv_introspect as pv_introspect pv_introspect.make_workspace_file("cinema", cinema_dirs)
def Save_PV_data_to_picture_file(inputFileName, field_name, node_or_cell,outputFileName ): pvs._DisableFirstRenderCameraReset() #pvs.HideAll(view=None)#Not available in paraview 5.1.2 view = pvs.GetActiveView() sources = pvs.GetSources().values() for aSource in sources: pvs.Hide(aSource, view) # create a new 'XML Unstructured Grid Reader' reader = pvs.XMLUnstructuredGridReader(FileName=[inputFileName]) if node_or_cell== 'CELLS': reader.CellArrayStatus = [field_name] elif node_or_cell== 'NODES': reader.PointArrayStatus = [field_name] else: raise ValueError("unknown type : should be CELLS or NODES") # get active view renderView1 = pvs.GetActiveViewOrCreate('RenderView') # uncomment following to set a specific view size # renderView1.ViewSize = [1057, 499] # show data in view display = pvs.Show(reader, renderView1); # trace defaults for the display properties. display.ColorArrayName = [None, ''] display.GlyphType = 'Arrow' display.ScalarOpacityUnitDistance = 0.02234159571242408 # reset view to fit data renderView1.ResetCamera() # set scalar coloring if node_or_cell== 'CELLS': pvs.ColorBy(display, ('CELLS', field_name)) elif node_or_cell== 'NODES': pvs.ColorBy(display, ('POINTS', field_name)) else: raise ValueError("unknown type : should be CELLS or NODES") # rescale color and/or opacity maps used to include current data range display.RescaleTransferFunctionToDataRange(True) # show color bar/color legend display.SetScalarBarVisibility(renderView1, True) pvs.SaveScreenshot(outputFileName+".png", magnification=1, quality=100, view=renderView1) display.SetScalarBarVisibility(renderView1, False) if field_name=='Velocity' : #pvs.HideAll(view=None)#Not available in paraview 5.1.2 view = pvs.GetActiveView() sources = pvs.GetSources().values() for aSource in sources: pvs.Hide(aSource, view) # create a new 'Stream Tracer' streamTracer1 = pvs.StreamTracer(Input=reader, SeedType='Point Source') streamTracer1.Vectors = ['CELLS', 'Velocity'] # init the 'Point Source' selected for 'SeedType' streamTracer1.SeedType.Center = [0.5, 0.5, 0.0] streamTracer1.SeedType.Radius = 0.0 # Properties modified on streamTracer1 streamTracer1.SeedType = 'High Resolution Line Source' # Properties modified on streamTracer1.SeedType streamTracer1.SeedType.Point1 = [0.0, 0.0, 0.0] streamTracer1.SeedType.Point2 = [1.0, 1.0, 0.0] streamTracer1.SeedType.Resolution = 20# Pb : claims attribute Resolution does not exist # show data in view streamTracer1Display = pvs.Show(streamTracer1, renderView1) # create a new 'Stream Tracer' streamTracer2 = pvs.StreamTracer(Input=reader, SeedType='Point Source') streamTracer2.Vectors = ['CELLS', 'Velocity'] # init the 'Point Source' selected for 'SeedType' streamTracer2.SeedType.Center = [0.5, 0.5, 0.0] streamTracer2.SeedType.Radius = 0.0 # Properties modified on streamTracer2 streamTracer2.SeedType = 'High Resolution Line Source' # Properties modified on streamTracer2.SeedType streamTracer2.SeedType.Point1 = [0.0, 1.0, 0.0] streamTracer2.SeedType.Point2 = [1.0, 0.0, 0.0] streamTracer2.SeedType.Resolution = 25# Pb : claims attribute Resolution does not exist # show data in view streamTracer2Display = pvs.Show(streamTracer2, renderView1) pvs.SaveScreenshot(outputFileName+"_streamlines.png", magnification=1, quality=100, view=renderView1) pvs.Delete()
import pv_utils as utils import paraview.simple as pv center = utils.GetCenter(tecFile='data/rhoda.tec') renderView = pv.CreateRenderView(ViewSize=[700, 500], Background=[1.0, 1.0, 1.0]) utils.ColorSurface(tecFile='data/rhoda.tec', view=renderView, opacity=1.0) utils.SetCameraFocus(tecFile='data/rhoda.tec', view=renderView, camFoc=center) utils.SetOrientation(view=renderView, camPosDir=[-1.0, -0.5, 0.3]) pv.SaveScreenshot('surface.png', magnification=1, quality=100, view=renderView)
def render_frames( scene, frames_dir=None, frame_window=None, render_missing_frames=False, save_state_to_file=None, no_render=False, show_preview=False, show_progress=False, job_id=None, ): # Validate scene if scene["View"]["ViewSize"][0] % 16 != 0: logger.warning( "The view width should be a multiple of 16 to be compatible with" " QuickTime.") if scene["View"]["ViewSize"][1] % 2 != 0: logger.warning( "The view height should be even to be compatible with QuickTime.") render_start_time = time.time() # Setup layout layout = pv.CreateLayout("Layout") # Setup view if "Background" in scene["View"]: bg_config = scene["View"]["Background"] del scene["View"]["Background"] if isinstance(bg_config, list): if isinstance(bg_config[0], list): assert len(bg_config) == 2, ( "When 'Background' is a list of colors, it must have 2" " entries.") bg_config = dict( BackgroundColorMode="Gradient", Background=parse_as.color(bg_config[0]), Background2=parse_as.color(bg_config[1]), ) else: bg_config = dict( BackgroundColorMode="Single Color", Background=parse_as.color(bg_config), ) bg_config["UseColorPaletteForBackground"] = 0 scene["View"].update(bg_config) bg_config = None else: bg_config = None view = pv.CreateRenderView(**scene["View"]) pv.AssignViewToLayout(view=view, layout=layout, hint=0) # Set spherical background texture if bg_config is not None: bg_config["BackgroundColorMode"] = "Texture" skybox_datasource = bg_config["Datasource"] del bg_config["Datasource"] background_texture = pvserver.rendering.ImageTexture( FileName=parse_as.path(scene["Datasources"][skybox_datasource])) background_sphere = pv.Sphere(Radius=bg_config["Radius"], ThetaResolution=100, PhiResolution=100) background_texture_map = pv.TextureMaptoSphere(Input=background_sphere) pv.Show( background_texture_map, view, Texture=background_texture, BackfaceRepresentation="Cull Frontface", Ambient=1.0, ) # Load the waveform data file waveform_h5file, waveform_subfile = parse_as.file_and_subfile( scene["Datasources"]["Waveform"]) waveform_data = WaveformDataReader(FileName=waveform_h5file, Subfile=waveform_subfile) pv.UpdatePipeline() # Generate volume data from the waveform. Also sets the available time range. # TODO: Pull KeepEveryNthTimestep out of datasource waveform_to_volume_configs = scene["WaveformToVolume"] if isinstance(waveform_to_volume_configs, dict): waveform_to_volume_configs = [{ "Object": waveform_to_volume_configs, }] if "VolumeRepresentation" in scene: waveform_to_volume_configs[0]["VolumeRepresentation"] = scene[ "VolumeRepresentation"] waveform_to_volume_objects = [] for waveform_to_volume_config in waveform_to_volume_configs: volume_data = WaveformToVolume( WaveformData=waveform_data, SwshCacheDirectory=parse_as.path( scene["Datasources"]["SwshCache"]), **waveform_to_volume_config["Object"], ) if "Modes" in waveform_to_volume_config["Object"]: volume_data.Modes = waveform_to_volume_config["Object"]["Modes"] if "Polarizations" in waveform_to_volume_config["Object"]: volume_data.Polarizations = waveform_to_volume_config["Object"][ "Polarizations"] waveform_to_volume_objects.append(volume_data) # Compute timing and frames information time_range_in_M = ( volume_data.TimestepValues[0], volume_data.TimestepValues[-1], ) logger.debug(f"Full available data time range: {time_range_in_M} (in M)") if "FreezeTime" in scene["Animation"]: frozen_time = scene["Animation"]["FreezeTime"] logger.info(f"Freezing time at {frozen_time}.") view.ViewTime = frozen_time animation = None else: if "Crop" in scene["Animation"]: time_range_in_M = scene["Animation"]["Crop"] logger.debug(f"Cropping time range to {time_range_in_M} (in M).") animation_speed = scene["Animation"]["Speed"] frame_rate = scene["Animation"]["FrameRate"] num_frames = animate.num_frames( max_animation_length=time_range_in_M[1] - time_range_in_M[0], animation_speed=animation_speed, frame_rate=frame_rate, ) animation_length_in_seconds = num_frames / frame_rate animation_length_in_M = animation_length_in_seconds * animation_speed time_per_frame_in_M = animation_length_in_M / num_frames logger.info(f"Rendering {animation_length_in_seconds:.2f}s movie with" f" {num_frames} frames ({frame_rate} FPS or" f" {animation_speed:.2e} M/s or" f" {time_per_frame_in_M:.2e} M/frame)...") if frame_window is not None: animation_window_num_frames = frame_window[1] - frame_window[0] animation_window_time_range = ( time_range_in_M[0] + frame_window[0] * time_per_frame_in_M, time_range_in_M[0] + (frame_window[1] - 1) * time_per_frame_in_M, ) logger.info( f"Restricting rendering to {animation_window_num_frames} frames" f" (numbers {frame_window[0]} to {frame_window[1] - 1}).") else: animation_window_num_frames = num_frames animation_window_time_range = time_range_in_M frame_window = (0, num_frames) # Setup animation so that sources can retrieve the `UPDATE_TIME_STEP` animation = pv.GetAnimationScene() # animation.UpdateAnimationUsingDataTimeSteps() # Since the data can be evaluated at arbitrary times we define the time steps # here by setting the number of frames within the full range animation.PlayMode = "Sequence" animation.StartTime = animation_window_time_range[0] animation.EndTime = animation_window_time_range[1] animation.NumberOfFrames = animation_window_num_frames logger.debug( f"Animating from scene time {animation.StartTime} to" f" {animation.EndTime} in {animation.NumberOfFrames} frames.") def scene_time_from_real(real_time): return (real_time / animation_length_in_seconds * animation_length_in_M) # For some reason the keyframe time for animations is expected to be within # (0, 1) so we need to transform back and forth from this "normalized" time def scene_time_from_normalized(normalized_time): return animation.StartTime + normalized_time * ( animation.EndTime - animation.StartTime) def normalized_time_from_scene(scene_time): return (scene_time - animation.StartTime) / (animation.EndTime - animation.StartTime) # Setup progress measuring already here so volume data computing for # initial frame is measured if show_progress and not no_render: logging.getLogger().handlers = [TqdmLoggingHandler()] animation_window_frame_range = tqdm.trange( animation_window_num_frames, desc="Rendering", unit="frame", miniters=1, position=job_id, ) else: animation_window_frame_range = range(animation_window_num_frames) # Set the initial time step animation.GoToFirst() # Display the volume data. This will trigger computing the volume data at the # current time step. for volume_data, waveform_to_volume_config in zip( waveform_to_volume_objects, waveform_to_volume_configs): vol_repr = (waveform_to_volume_config["VolumeRepresentation"] if "VolumeRepresentation" in waveform_to_volume_config else {}) volume_color_by = config_color.extract_color_by(vol_repr) if (vol_repr["VolumeRenderingMode"] == "GPU Based" and len(volume_color_by) > 2): logger.warning( "The 'GPU Based' volume renderer doesn't support multiple" " components.") volume = pv.Show(volume_data, view, **vol_repr) pv.ColorBy(volume, value=volume_color_by) if "Slices" in scene: for slice_config in scene["Slices"]: slice_obj_config = slice_config.get("Object", {}) slice = pv.Slice(Input=volume_data) slice.SliceType = "Plane" slice.SliceOffsetValues = [0.0] slice.SliceType.Origin = slice_obj_config.get( "Origin", [0.0, 0.0, -0.3]) slice.SliceType.Normal = slice_obj_config.get( "Normal", [0.0, 0.0, 1.0]) slice_rep = pv.Show(slice, view, **slice_config.get("Representation", {})) pv.ColorBy(slice_rep, value=volume_color_by) # Display the time if "TimeAnnotation" in scene: time_annotation = pv.AnnotateTimeFilter(volume_data, **scene["TimeAnnotation"]) pv.Show(time_annotation, view, **scene["TimeAnnotationRepresentation"]) # Add spheres if "Spheres" in scene: for sphere_config in scene["Spheres"]: sphere = pv.Sphere(**sphere_config["Object"]) pv.Show(sphere, view, **sphere_config["Representation"]) # Add trajectories and objects that follow them if "Trajectories" in scene: for trajectory_config in scene["Trajectories"]: trajectory_name = trajectory_config["Name"] radial_scale = (trajectory_config["RadialScale"] if "RadialScale" in trajectory_config else 1.0) # Load the trajectory data traj_data_reader = TrajectoryDataReader( RadialScale=radial_scale, **scene["Datasources"]["Trajectories"][trajectory_name], ) # Make sure the data is loaded so we can retrieve timesteps. # TODO: This should be fixed in `TrajectoryDataReader` by # communicating time range info down the pipeline, but we had issues # with that (see also `WaveformDataReader`). traj_data_reader.UpdatePipeline() if "Objects" in trajectory_config: with animate.restore_animation_state(animation): follow_traj = FollowTrajectory( TrajectoryData=traj_data_reader) for traj_obj_config in trajectory_config["Objects"]: for traj_obj_key in traj_obj_config: if traj_obj_key in [ "Representation", "Visibility", "TimeShift", "Glyph", ]: continue traj_obj_type = getattr(pv, traj_obj_key) traj_obj_glyph = traj_obj_type( **traj_obj_config[traj_obj_key]) follow_traj.UpdatePipeline() traj_obj = pv.Glyph(Input=follow_traj, GlyphType=traj_obj_glyph) # Can't set this in the constructor for some reason traj_obj.ScaleFactor = 1.0 for glyph_property in (traj_obj_config["Glyph"] if "Glyph" in traj_obj_config else []): setattr( traj_obj, glyph_property, traj_obj_config["Glyph"][glyph_property], ) traj_obj.UpdatePipeline() if "TimeShift" in traj_obj_config: traj_obj = animate.apply_time_shift( traj_obj, traj_obj_config["TimeShift"]) pv.Show(traj_obj, view, **traj_obj_config["Representation"]) if "Visibility" in traj_obj_config: animate.apply_visibility( traj_obj, traj_obj_config["Visibility"], normalized_time_from_scene, scene_time_from_real, ) if "Tail" in trajectory_config: with animate.restore_animation_state(animation): traj_tail = TrajectoryTail(TrajectoryData=traj_data_reader) if "TimeShift" in trajectory_config: traj_tail = animate.apply_time_shift( traj_tail, trajectory_config["TimeShift"]) tail_config = trajectory_config["Tail"] traj_color_by = config_color.extract_color_by(tail_config) if "Visibility" in tail_config: tail_visibility_config = tail_config["Visibility"] del tail_config["Visibility"] else: tail_visibility_config = None tail_rep = pv.Show(traj_tail, view, **tail_config) pv.ColorBy(tail_rep, value=traj_color_by) if tail_visibility_config is not None: animate.apply_visibility( traj_tail, tail_visibility_config, normalized_time_from_scene=normalized_time_from_scene, scene_time_from_real=scene_time_from_real, ) if "Move" in trajectory_config: move_config = trajectory_config["Move"] logger.debug( f"Animating '{move_config['guiName']}' along trajectory.") with h5py.File(trajectory_file, "r") as traj_data_file: trajectory_data = np.array( traj_data_file[trajectory_subfile]) if radial_scale != 1.0: trajectory_data[:, 1:] *= radial_scale logger.debug(f"Trajectory data shape: {trajectory_data.shape}") animate.follow_path( gui_name=move_config["guiName"], trajectory_data=trajectory_data, num_keyframes=move_config["NumKeyframes"], scene_time_range=time_range_in_M, normalized_time_from_scene=normalized_time_from_scene, ) # Add non-spherical horizon shapes (instead of spherical objects following # trajectories) if "Horizons" in scene: for horizon_config in scene["Horizons"]: with animate.restore_animation_state(animation): horizon = pv.PVDReader(FileName=scene["Datasources"] ["Horizons"][horizon_config["Name"]]) if horizon_config.get("InterpolateTime", False): horizon = pv.TemporalInterpolator( Input=horizon, DiscreteTimeStepInterval=0) if "TimeShift" in horizon_config: horizon = animate.apply_time_shift(horizon, horizon_config["TimeShift"], animation) # Try to make horizon surfaces smooth. At low angular resoluton # they still show artifacts, so perhaps more can be done. horizon = pv.ExtractSurface(Input=horizon) horizon = pv.GenerateSurfaceNormals(Input=horizon) horizon_rep_config = horizon_config.get("Representation", {}) if "Representation" not in horizon_rep_config: horizon_rep_config["Representation"] = "Surface" if "AmbientColor" not in horizon_rep_config: horizon_rep_config["AmbientColor"] = [0.0, 0.0, 0.0] if "DiffuseColor" not in horizon_rep_config: horizon_rep_config["DiffuseColor"] = [0.0, 0.0, 0.0] if "Specular" not in horizon_rep_config: horizon_rep_config["Specular"] = 0.2 if "SpecularPower" not in horizon_rep_config: horizon_rep_config["SpecularPower"] = 10 if "SpecularColor" not in horizon_rep_config: horizon_rep_config["SpecularColor"] = [1.0, 1.0, 1.0] if "ColorBy" in horizon_rep_config: horizon_color_by = config_color.extract_color_by( horizon_rep_config) else: horizon_color_by = None horizon_rep = pv.Show(horizon, view, **horizon_rep_config) if horizon_color_by is not None: pv.ColorBy(horizon_rep, value=horizon_color_by) # Animate visibility if "Visibility" in horizon_config: animate.apply_visibility( horizon, horizon_config["Visibility"], normalized_time_from_scene=normalized_time_from_scene, scene_time_from_real=scene_time_from_real, ) if "Contours" in horizon_config: for contour_config in horizon_config["Contours"]: contour = pv.Contour(Input=horizon, **contour_config["Object"]) contour_rep = pv.Show(contour, view, **contour_config["Representation"]) pv.ColorBy(contour_rep, None) if "Visibility" in horizon_config: animate.apply_visibility( contour, horizon_config["Visibility"], normalized_time_from_scene= normalized_time_from_scene, scene_time_from_real=scene_time_from_real, ) # Configure transfer functions if "TransferFunctions" in scene: for tf_config in scene["TransferFunctions"]: colored_field = tf_config["Field"] transfer_fctn = pv.GetColorTransferFunction(colored_field) opacity_fctn = pv.GetOpacityTransferFunction(colored_field) tf.configure_transfer_function(transfer_fctn, opacity_fctn, tf_config["TransferFunction"]) # Save state file before configuring camera keyframes. # TODO: Make camera keyframes work with statefile if save_state_to_file is not None: pv.SaveState(save_state_to_file + ".pvsm") # Camera shots # TODO: Make this work with freezing time while the camera is swinging if animation is None: for i, shot in enumerate(scene["CameraShots"]): if (i == len(scene["CameraShots"]) - 1 or (shot["Time"] if "Time" in shot else 0.0) >= view.ViewTime): camera_motion.apply(shot) break else: camera_motion.apply_swings( scene["CameraShots"], scene_time_range=time_range_in_M, scene_time_from_real=scene_time_from_real, normalized_time_from_scene=normalized_time_from_scene, ) # Report time if animation is not None: report_time_cue = pv.PythonAnimationCue() report_time_cue.Script = """ def start_cue(self): pass def tick(self): import paraview.simple as pv import logging logger = logging.getLogger('Animation') scene_time = pv.GetActiveView().ViewTime logger.info(f"Scene time: {scene_time}") def end_cue(self): pass """ animation.Cues.append(report_time_cue) if show_preview and animation is not None: animation.PlayMode = "Real Time" animation.Duration = 10 animation.Play() animation.PlayMode = "Sequence" if no_render: logger.info("No rendering requested. Total time:" f" {time.time() - render_start_time:.2f}s") return if frames_dir is None: raise RuntimeError("Trying to render but `frames_dir` is not set.") if os.path.exists(frames_dir): logger.warning( f"Output directory '{frames_dir}' exists, files may be overwritten." ) else: os.makedirs(frames_dir) if animation is None: pv.Render() pv.SaveScreenshot(os.path.join(frames_dir, "frame.png")) else: # Iterate over frames manually to support filling in missing frames. # If `pv.SaveAnimation` would support that, here's how it could be # invoked: # pv.SaveAnimation( # os.path.join(frames_dir, 'frame.png'), # view, # animation, # FrameWindow=frame_window, # SuffixFormat='.%06d') # Note that `FrameWindow` appears to be buggy, so we set up the # `animation` according to the `frame_window` above so the frame files # are numberd correctly. for animation_window_frame_i in animation_window_frame_range: frame_i = frame_window[0] + animation_window_frame_i frame_file = os.path.join(frames_dir, f"frame.{frame_i:06d}.png") if render_missing_frames and os.path.exists(frame_file): continue logger.debug(f"Rendering frame {frame_i}...") animation.AnimationTime = ( animation.StartTime + time_per_frame_in_M * animation_window_frame_i) pv.Render() pv.SaveScreenshot(frame_file) logger.info(f"Rendered frame {frame_i}.") logger.info( f"Rendering done. Total time: {time.time() - render_start_time:.2f}s")
def solve(filename,resolution,meshType, testColor): start = time.time() test_desc["Mesh_type"]=meshType test_desc["Test_color"]=testColor #Chargement du maillage triangulaire de la sphère #======================================================================================= my_mesh = cdmath.Mesh(filename+".med") if(not my_mesh.isTriangular()) : raise ValueError("Wrong cell types : mesh is not made of triangles") if(my_mesh.getMeshDimension()!=2) : raise ValueError("Wrong mesh dimension : expected a surface of dimension 2") if(my_mesh.getSpaceDimension()!=3) : raise ValueError("Wrong space dimension : expected a space of dimension 3") nbNodes = my_mesh.getNumberOfNodes() nbCells = my_mesh.getNumberOfCells() test_desc["Space_dimension"]=my_mesh.getSpaceDimension() test_desc["Mesh_dimension"]=my_mesh.getMeshDimension() test_desc["Mesh_number_of_elements"]=my_mesh.getNumberOfNodes() test_desc["Mesh_cell_type"]=my_mesh.getElementTypes() print("Mesh building/loading done") print("nb of nodes=", nbNodes) print("nb of cells=", nbCells) #Discrétisation du second membre et détermination des noeuds intérieurs #====================================================================== my_RHSfield = cdmath.Field("RHS_field", cdmath.NODES, my_mesh, 1) maxNbNeighbours = 0#This is to determine the number of non zero coefficients in the sparse finite element rigidity matrix #parcours des noeuds pour discrétisation du second membre et extraction du nb max voisins d'un noeud for i in range(nbNodes): Ni=my_mesh.getNode(i) x = Ni.x() y = Ni.y() z = Ni.z() my_RHSfield[i]=12*y*(3*x*x-y*y)/pow(x*x+y*y+z*z,3/2)#vecteur propre du laplacien sur la sphère if my_mesh.isBorderNode(i): # Détection des noeuds frontière raise ValueError("Mesh should not contain borders") else: maxNbNeighbours = max(1+Ni.getNumberOfCells(),maxNbNeighbours) test_desc["Mesh_max_number_of_neighbours"]=maxNbNeighbours print("Right hand side discretisation done") print("Max nb of neighbours=", maxNbNeighbours) print("Integral of the RHS", my_RHSfield.integral(0)) # Construction de la matrice de rigidité et du vecteur second membre du système linéaire #======================================================================================= Rigidite=cdmath.SparseMatrixPetsc(nbNodes,nbNodes,maxNbNeighbours)# warning : third argument is number of non zero coefficients per line RHS=cdmath.Vector(nbNodes) # Vecteurs gradient de la fonction de forme associée à chaque noeud d'un triangle GradShapeFunc0=cdmath.Vector(3) GradShapeFunc1=cdmath.Vector(3) GradShapeFunc2=cdmath.Vector(3) normalFace0=cdmath.Vector(3) normalFace1=cdmath.Vector(3) #On parcourt les triangles du domaine for i in range(nbCells): Ci=my_mesh.getCell(i) #Contribution à la matrice de rigidité nodeId0=Ci.getNodeId(0) nodeId1=Ci.getNodeId(1) nodeId2=Ci.getNodeId(2) N0=my_mesh.getNode(nodeId0) N1=my_mesh.getNode(nodeId1) N2=my_mesh.getNode(nodeId2) #Build normal to cell Ci normalFace0[0]=Ci.getNormalVector(0,0) normalFace0[1]=Ci.getNormalVector(0,1) normalFace0[2]=Ci.getNormalVector(0,2) normalFace1[0]=Ci.getNormalVector(1,0) normalFace1[1]=Ci.getNormalVector(1,1) normalFace1[2]=Ci.getNormalVector(1,2) normalCell = normalFace0.crossProduct(normalFace1) test = normalFace0.tensProduct(normalFace1) normalCell = normalCell/normalCell.norm() cellMat=cdmath.Matrix(4) cellMat[0,0]=N0.x() cellMat[0,1]=N0.y() cellMat[0,2]=N0.z() cellMat[1,0]=N1.x() cellMat[1,1]=N1.y() cellMat[1,2]=N1.z() cellMat[2,0]=N2.x() cellMat[2,1]=N2.y() cellMat[2,2]=N2.z() cellMat[3,0]=normalCell[0] cellMat[3,1]=normalCell[1] cellMat[3,2]=normalCell[2] cellMat[0,3]=1 cellMat[1,3]=1 cellMat[2,3]=1 cellMat[3,3]=0 #Formule des gradients voir EF P1 -> calcul déterminants GradShapeFunc0[0]= cellMat.partMatrix(0,0).determinant()/2 GradShapeFunc0[1]=-cellMat.partMatrix(0,1).determinant()/2 GradShapeFunc0[2]= cellMat.partMatrix(0,2).determinant()/2 GradShapeFunc1[0]=-cellMat.partMatrix(1,0).determinant()/2 GradShapeFunc1[1]= cellMat.partMatrix(1,1).determinant()/2 GradShapeFunc1[2]=-cellMat.partMatrix(1,2).determinant()/2 GradShapeFunc2[0]= cellMat.partMatrix(2,0).determinant()/2 GradShapeFunc2[1]=-cellMat.partMatrix(2,1).determinant()/2 GradShapeFunc2[2]= cellMat.partMatrix(2,2).determinant()/2 #Création d'un tableau (numéro du noeud, gradient de la fonction de forme GradShapeFuncs={nodeId0 : GradShapeFunc0} GradShapeFuncs[nodeId1]=GradShapeFunc1 GradShapeFuncs[nodeId2]=GradShapeFunc2 # Remplissage de la matrice de rigidité et du second membre for j in [nodeId0,nodeId1,nodeId2] : #Ajout de la contribution de la cellule triangulaire i au second membre du noeud j RHS[j]=Ci.getMeasure()/3*my_RHSfield[j]+RHS[j] # intégrale dans le triangle du produit f x fonction de base #Contribution de la cellule triangulaire i à la ligne j du système linéaire for k in [nodeId0,nodeId1,nodeId2] : Rigidite.addValue(j,k,GradShapeFuncs[j]*GradShapeFuncs[k]/Ci.getMeasure()) print("Linear system matrix building done") # Résolution du système linéaire #================================= LS=cdmath.LinearSolver(Rigidite,RHS,100,1.E-2,"CG","ILU")#Remplacer CG par CHOLESKY pour solveur direct LS.isSingular()#En raison de l'absence de bord LS.setComputeConditionNumber() SolSyst=LS.solve() print "Preconditioner used : ", LS.getNameOfPc() print "Number of iterations used : ", LS.getNumberOfIter() print "Final residual : ", LS.getResidu() print("Linear system solved") test_desc["Linear_solver_algorithm"]=LS.getNameOfMethod() test_desc["Linear_solver_preconditioner"]=LS.getNameOfPc() test_desc["Linear_solver_precision"]=LS.getTolerance() test_desc["Linear_solver_maximum_iterations"]=LS.getNumberMaxOfIter() test_desc["Linear_system_max_actual_iterations_number"]=LS.getNumberOfIter() test_desc["Linear_system_max_actual_error"]=LS.getResidu() test_desc["Linear_system_max_actual_condition number"]=LS.getConditionNumber() # Création du champ résultat #=========================== my_ResultField = cdmath.Field("ResultField", cdmath.NODES, my_mesh, 1) for j in range(nbNodes): my_ResultField[j]=SolSyst[j];#remplissage des valeurs pour les noeuds intérieurs #sauvegarde sur le disque dur du résultat dans un fichier paraview my_ResultField.writeVTK("FiniteElementsOnSpherePoisson_"+meshType+str(nbNodes)) end = time.time() print("Integral of the numerical solution", my_ResultField.integral(0)) print("Numerical solution of poisson equation on a sphere using finite elements done") #Calcul de l'erreur commise par rapport à la solution exacte #=========================================================== #The following formulas use the fact that the exact solution is equal the right hand side divided by 12 max_abs_sol_exacte=0 erreur_abs=0 max_sol_num=0 min_sol_num=0 for i in range(nbNodes) : if max_abs_sol_exacte < abs(my_RHSfield[i]) : max_abs_sol_exacte = abs(my_RHSfield[i]) if erreur_abs < abs(my_RHSfield[i]/12 - my_ResultField[i]) : erreur_abs = abs(my_RHSfield[i]/12 - my_ResultField[i]) if max_sol_num < my_ResultField[i] : max_sol_num = my_ResultField[i] if min_sol_num > my_ResultField[i] : min_sol_num = my_ResultField[i] max_abs_sol_exacte = max_abs_sol_exacte/12 print("Absolute error = max(| exact solution - numerical solution |) = ",erreur_abs ) print("Relative error = max(| exact solution - numerical solution |)/max(| exact solution |) = ",erreur_abs/max_abs_sol_exacte) print ("Maximum numerical solution = ", max_sol_num, " Minimum numerical solution = ", min_sol_num) test_desc["Computational_time_taken_by_run"]=end-start test_desc["Absolute_error"]=erreur_abs test_desc["Relative_error"]=erreur_abs/max_abs_sol_exacte #Postprocessing : #================ # save 3D picture PV_routines.Save_PV_data_to_picture_file("FiniteElementsOnSpherePoisson_"+meshType+str(nbNodes)+'_0.vtu',"ResultField",'NODES',"FiniteElementsOnSpherePoisson_"+meshType+str(nbNodes)) # save 3D clip VTK_routines.Clip_VTK_data_to_VTK("FiniteElementsOnSpherePoisson_"+meshType+str(nbNodes)+'_0.vtu',"Clip_VTK_data_to_VTK_"+ "FiniteElementsOnSpherePoisson_"+meshType+str(nbNodes)+'_0.vtu',[0.25,0.25,0.25], [-0.5,-0.5,-0.5],resolution ) PV_routines.Save_PV_data_to_picture_file("Clip_VTK_data_to_VTK_"+"FiniteElementsOnSpherePoisson_"+meshType+str(nbNodes)+'_0.vtu',"ResultField",'NODES',"Clip_VTK_data_to_VTK_"+"FiniteElementsOnSpherePoisson_"+meshType+str(nbNodes)) # save plot around circumference finiteElementsOnSphere_0vtu = pvs.XMLUnstructuredGridReader(FileName=["FiniteElementsOnSpherePoisson_"+meshType+str(nbNodes)+'_0.vtu']) slice1 = pvs.Slice(Input=finiteElementsOnSphere_0vtu) slice1.SliceType.Normal = [0.5, 0.5, 0.5] renderView1 = pvs.GetActiveViewOrCreate('RenderView') finiteElementsOnSphere_0vtuDisplay = pvs.Show(finiteElementsOnSphere_0vtu, renderView1) pvs.ColorBy(finiteElementsOnSphere_0vtuDisplay, ('POINTS', 'ResultField')) slice1Display = pvs.Show(slice1, renderView1) pvs.SaveScreenshot("./FiniteElementsOnSpherePoisson"+"_Slice_"+meshType+str(nbNodes)+'.png', magnification=1, quality=100, view=renderView1) plotOnSortedLines1 = pvs.PlotOnSortedLines(Input=slice1) pvs.SaveData('./FiniteElementsOnSpherePoisson_PlotOnSortedLines'+meshType+str(nbNodes)+'.csv', proxy=plotOnSortedLines1) lineChartView2 = pvs.CreateView('XYChartView') plotOnSortedLines1Display = pvs.Show(plotOnSortedLines1, lineChartView2) plotOnSortedLines1Display.UseIndexForXAxis = 0 plotOnSortedLines1Display.XArrayName = 'arc_length' plotOnSortedLines1Display.SeriesVisibility = ['ResultField (1)'] pvs.SaveScreenshot("./FiniteElementsOnSpherePoisson"+"_PlotOnSortedLine_"+meshType+str(nbNodes)+'.png', magnification=1, quality=100, view=lineChartView2) pvs.Delete(lineChartView2) with open('test_Poisson'+str(my_mesh.getMeshDimension())+'D_EF_'+meshType+str(nbCells)+ "Cells.json", 'w') as outfile: json.dump(test_desc, outfile) return erreur_abs/max_abs_sol_exacte, nbNodes, min_sol_num, max_sol_num, end - start
def batchVis(c1File,particleFile,step,saveAs): """Renders a bijel top down view in paraview and saves a screenshot.""" import paraview.simple as pv # visualize a vtk file c1 = pv.LegacyVTKReader(FileNames=c1File) p = pv.LegacyVTKReader(FileNames=particleFile) renderView1 = pv.GetActiveViewOrCreate('RenderView') renderView1.ViewSize = [1298, 860] renderView1.Background = [1.0, 1.0, 1.0] renderView1.InteractionMode = '2D' pDisplay = pv.Show(p, renderView1) c1Display = pv.Show(c1, renderView1) # create particle glyphs glyph = pv.Glyph(Input=p,GlyphType="Sphere") glyph.ScaleFactor = 1.0 glyph.GlyphMode = 'All Points' glyph.GlyphType.Radius = 1.0 glyph.GlyphType.ThetaResolution = 20 glyph.GlyphType.PhiResolution = 20 glyph.Scalars = ['POINTS','radius'] glyph.Vectors = ['POINTS','None'] glyph.ScaleMode = 'scalar' # show data in view glyphDisplay = pv.Show(glyph, renderView1) pv.ColorBy(glyphDisplay, None) pv.SetActiveSource(c1) pv.ColorBy(c1Display, ('POINTS', 'c1')) c1Display.RescaleTransferFunctionToDataRange(True) c1Display.SetRepresentationType('Volume') # make box outline # box = pv.Box() # box.XLength = 128.0 # box.YLength = 128.0 # box.ZLength = 64.0 # box.Center = [64.0, 64.0, 32.0] # boxDisplay = pv.Show(box, renderView1) # boxDisplay.SetRepresentationType('Outline') # boxDisplay.AmbientColor = [0.0, 0.0, 0.0] # set coloring of c1 c1LUT = pv.GetColorTransferFunction('c1') c1LUT.RGBPoints = [0.006000000052154064, 0.231373, 0.298039, 0.752941, 0.5120000033639371, 0.865003, 0.865003, 0.865003, 1.0180000066757202, 0.705882, 0.0156863, 0.14902] c1LUT.ColorSpace = 'Diverging' c1LUT.BelowRangeColor = [0.0, 0.0, 0.0] c1LUT.AboveRangeColor = [1.0, 1.0, 1.0] c1LUT.NanColor = [1.0, 1.0, 0.0] c1LUT.Discretize = 1 c1LUT.NumberOfTableValues = 256 c1LUT.ScalarRangeInitialized = 1.0 c1LUT.AllowDuplicateScalars = 1 c1PWF = pv.GetOpacityTransferFunction('c1') c1PWF.Points = [0.0, 0.05, 0.5, 0.0, 0.3, 0.05, 0.5, 0.0, 0.4, 0.5, 0.5, 0.0, 0.6, 0.5, 0.5, 0.0, 0.7, 0.05, 0.5, 0.0, 1., 0.05, 0.5, 0.0] # annotate time step in rendering # text = pv.Text # text.Text = 'Step '+str(step) # textDisplay = pv.Show(text,renderView1) # textDisplay.Color = [0.0, 0.0, 0.0] # textDisplay.WindowLocation = 'UpperCenter' # reset view to fit data renderView1.ResetCamera() # pv.Render() # save screen shot viewLayout1 = pv.GetLayout() print(saveAs) pv.SaveScreenshot(saveAs, layout=viewLayout1, magnification=1, quality=100) # clean up # pv.Delete(box) pv.Delete(glyph) pv.Delete(p) pv.Delete(c1) del c1 del p del glyph
def writeArray(self, path, source, name, component=0): rep = simple.Show(source, self.view) rep.Representation = 'Surface' rep.DiffuseColor = [1, 1, 1] dataRange = [0.0, 1.0] fieldToColorBy = ['POINTS', name] self.view.ArrayNameToDraw = name self.view.ArrayComponentToDraw = component pdi = source.GetPointDataInformation() cdi = source.GetCellDataInformation() if pdi.GetArray(name): self.view.DrawCells = 0 dataRange = pdi.GetArray(name).GetRange(component) fieldToColorBy[0] = 'POINTS' elif cdi.GetArray(name): self.view.DrawCells = 1 dataRange = cdi.GetArray(name).GetRange(component) fieldToColorBy[0] = 'CELLS' else: print("No array with that name", name) return realRange = dataRange if dataRange[0] == dataRange[1]: dataRange = [dataRange[0] - 0.1, dataRange[1] + 0.1] simple.ColorBy(rep, fieldToColorBy) # Grab data tmpFileName = path + '__.png' self.view.ScalarRange = dataRange self.view.LockBounds = 1 self.view.StartCaptureValues() simple.SaveScreenshot(tmpFileName, self.view) self.view.StopCaptureValues() self.view.LockBounds = 0 if self.canWrite: # Convert data self.reader.SetFileName(tmpFileName) self.reader.Update() rgbArray = self.reader.GetOutput().GetPointData().GetArray(0) arraySize = rgbArray.GetNumberOfTuples() rawArray = vtkFloatArray() rawArray.SetNumberOfTuples(arraySize) minValue = 10000.0 maxValue = -100000.0 delta = (dataRange[1] - dataRange[0]) / 16777215.0 # 2^24 - 1 => 16,777,215 for idx in range(arraySize): rgb = rgbArray.GetTuple3(idx) if rgb[0] != 0 or rgb[1] != 0 or rgb[2] != 0: value = dataRange[0] + delta * float( rgb[0] * 65536 + rgb[1] * 256 + rgb[2] - 1) rawArray.SetTuple1(idx, value) minValue = min(value, minValue) maxValue = max(value, maxValue) else: rawArray.SetTuple1(idx, float('NaN')) # print ('Array bounds', minValue, maxValue, 'compare to', dataRange) with open(path, 'wb') as f: f.write(buffer(rawArray)) # Delete temporary file if self.cleanAfterMe: os.remove(tmpFileName) # Remove representation from view simple.Hide(source, self.view) return realRange
print 'Plotting kernel %s, range(%f, %f)' % (data.Name, maxval, maxval) # show data from kerner_kernelxdmf kerner_kernelxdmfDisplay = simple.Show(kerner_kernelxdmf, renderView1) # trace defaults for the display properties. kerner_kernelxdmfDisplay.ColorArrayName = ['POINTS', data.Name] kerner_kernelxdmfDisplay.LookupTable = LUT # show color legend kerner_kernelxdmfDisplay.SetScalarBarVisibility(renderView1, True) filename_out = os.path.join(kernel_plot_dir, '%s.png' % data.Name) simple.SaveScreenshot(filename=filename_out, view=renderView1, magnification=2) print ' ...done!' # Loop over all Cell variables in file and print them for data in kerner_kernelxdmf.CellData: if data.Name[0:3] == 'K_x': # Get complete array of this variable ncells = kerner_all.GetNumberOfCells() data_list = [] for i in range(0, ncells): newval = kerner_all.GetCellData().GetArray(data.Name).GetValue(i) data_list.append(newval) data_array = np.asarray(data_list)
def WriteImages(self, datadescription, rescale_lookuptable=False, image_quality=None, padding_amount=0): """This method will update all views, if present and write output images, as needed. **Parameters** datadescription Catalyst data-description object rescale_lookuptable (bool, optional) If True, when all lookup tables are rescaled using current data ranges before saving the images. Defaults to False. image_quality (int, optional) If specified, should be a value in the range (0, 100) that specifies the image quality. For JPEG, 0 is low quality i.e. max compression, 100 is best quality i.e. least compression. For legacy reasons, this is inverted for PNG (which uses lossless compression). For PNG, 0 is no compression i.e maximum image size, while 100 is most compressed and hence least image size. If not specified, for saving PNGs 0 is assumed to minimize performance impact. padding_amount (int, optional) Amount to pad the time index by. """ timestep = datadescription.GetTimeStep() cinema_dirs = [] for view in self.__ViewsList: if (view.cpFrequency and self.NeedToOutput(datadescription, view.cpFrequency)) or \ datadescription.GetForceOutput() == True: fname = view.cpFileName ts = str(timestep).rjust(padding_amount, '0') fname = fname.replace("%t", ts) if view.cpFitToScreen != 0: view.ViewTime = datadescription.GetTime() if view.IsA("vtkSMRenderViewProxy") == True: view.ResetCamera() elif view.IsA("vtkSMContextViewProxy") == True: view.ResetDisplay() else: print(' do not know what to do with a ', view.GetClassName()) view.ViewTime = datadescription.GetTime() if rescale_lookuptable: self.RescaleDataRange(view, datadescription.GetTime()) cinemaOptions = view.cpCinemaOptions if cinemaOptions and 'camera' in cinemaOptions: if 'composite' in view.cpCinemaOptions and view.cpCinemaOptions[ 'composite'] == True: dirname, filelist = self.UpdateCinema(view, datadescription, specLevel="B") else: dirname, filelist = self.UpdateCinema(view, datadescription, specLevel="A") if dirname: self.__AppendCViewToCinemaDTable( timestep, "view_%s" % self.__ViewsList.index(view), filelist) cinema_dirs.append(dirname) else: if '/' in fname and createDirectoriesIfNeeded: oktowrite = [1.] import vtk comm = vtk.vtkMultiProcessController.GetGlobalController( ) if comm.GetLocalProcessId() == 0: import os newDir = fname[0:fname.rfind('/')] try: os.makedirs(newDir) except OSError: if not os.path.isdir(newDir): print("ERROR: Cannot make directory for", fname, ". No image will be output.") oktowrite[0] = 0. comm.Broadcast(oktowrite, 1, 0) if oktowrite[0] == 0: # we can't make the directory so no reason to update the pipeline return if image_quality is None and fname.endswith('png'): # for png quality = 0 means no compression. compression can be a potentially # very costly serial operation on process 0 quality = 0 elif image_quality is not None: quality = int(image_quality) else: # let simple.SaveScreenshot pick a default. quality = None simple.SaveScreenshot(fname, view, magnification=view.cpMagnification, quality=quality) self.__AppendToCinemaDTable( timestep, "view_%s" % self.__ViewsList.index(view), fname) if len(cinema_dirs) > 1: import paraview.tpl.cinema_python.adaptors.paraview.pv_introspect as pv_introspect pv_introspect.make_workspace_file("cinema\\", cinema_dirs) self.__FinalizeCinemaDTable()
import pv_utils as utils import paraview.simple as pv rdaTec = 'data/rhoda.tec' rhcTec = 'data/rhohc.tec' rnpTec = 'data/rho_fld_np.tec' surfTec = 'data/rho_surf.tec' white = [1.0, 1.0, 1.0] red = [0.67, 0.0, 0.0] green = [0.0, 0.67, 0.0] gold = [1.0, 0.85, 0.0] black = [0.0, 0.0, 0.0] center = utils.GetCenter(tecFile=rdaTec) renderView = pv.CreateRenderView( ViewSize=[700, 500], Background=white ) utils.ColorSurface( tecFile=rdaTec, view=renderView, opacity=0.5 ) utils.NewContour( tecFile=rdaTec, view=renderView, isoFrac=0.5, color=red, opacity=0.5 ) utils.NewContour( tecFile=rhcTec, view=renderView, isoFrac=0.5, color=green, opacity=0.5 ) utils.NewContour( tecFile=rnpTec, view=renderView, isoFrac=0.5, color=gold, opacity=1.0) utils.NewContour( tecFile=surfTec, view=renderView, isoFrac=0.5, color=black, opacity=0.5 ) utils.SetCameraFocus( tecFile=rdaTec, view=renderView, camFoc=center ) utils.SetOrientation( view=renderView, camPosDir=[-1.0, -0.5, 0.3] ) pv.SaveScreenshot('contours.png', magnification=1, quality=100, view=renderView)