Skip to content

ReadExodusData

Repository source: ReadExodusData


Description

The example uses vtkExodusIIReader to read an ExodusII file. The nodal variable to read is the second argument. The nodal variable is displayed with a color map.

Other languages

See (Python), (PythonicAPI)

Question

If you have a question about this example, please use the VTK Discourse Forum

Code

ReadExodusData.cxx

#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkCompositeDataGeometryFilter.h>
#include <vtkExodusIIReader.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>

int main(int argc, char* argv[])
{
  vtkNew<vtkNamedColors> colors;

  if (argc < 3)
  {
    std::cout << "Usage: " << argv[0]
              << " exodus_file.e nodal_variable e.g mug.e convected";
    return EXIT_FAILURE;
  }

  // Read Exodus Data
  vtkNew<vtkExodusIIReader> reader;
  reader->SetFileName(argv[1]);
  reader->UpdateInformation();
  reader->SetTimeStep(10);
  reader->SetAllArrayStatus(vtkExodusIIReader::NODAL,
                            1); // enables all NODAL variables

  // Create Geometry
  vtkNew<vtkCompositeDataGeometryFilter> geometry;
  geometry->SetInputConnection(0, reader->GetOutputPort(0));

  // Mapper
  vtkNew<vtkPolyDataMapper> mapper;
  mapper->SetInputConnection(geometry->GetOutputPort());
  mapper->SelectColorArray(argv[2]);
  mapper->SetScalarModeToUsePointFieldData();
  mapper->InterpolateScalarsBeforeMappingOn();

  // Actor
  vtkNew<vtkActor> actor;
  actor->SetMapper(mapper);

  // Renderer
  vtkNew<vtkRenderer> renderer;
  renderer->AddViewProp(actor);
  renderer->SetBackground(colors->GetColor3d("DimGray").GetData());

  renderer->GetActiveCamera()->SetPosition(9.0, 9.0, 7.0);
  renderer->GetActiveCamera()->SetFocalPoint(0, 0, 0);
  renderer->GetActiveCamera()->SetViewUp(0.2, -0.7, 0.7);
  renderer->GetActiveCamera()->SetDistance(14.5);

  // Window and Interactor
  vtkNew<vtkRenderWindow> window;
  window->AddRenderer(renderer);
  window->SetSize(600, 600);
  window->SetWindowName("ReadExodusData");

  vtkNew<vtkRenderWindowInteractor> interactor;
  interactor->SetRenderWindow(window);
  interactor->Initialize();

  // Show the result
  window->Render();
  interactor->Start();

  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 3.12 FATAL_ERROR)

project(ReadExodusData)

find_package(VTK COMPONENTS 
  CommonColor
  CommonCore
  FiltersGeometry
  IOExodus
  InteractionStyle
  RenderingContextOpenGL2
  RenderingCore
  RenderingFreeType
  RenderingGL2PSOpenGL2
  RenderingOpenGL2
)

if (NOT VTK_FOUND)
  message(FATAL_ERROR "ReadExodusData: Unable to find the VTK build folder.")
endif()

# Prevent a "command line is too long" failure in Windows.
set(CMAKE_NINJA_FORCE_RESPONSE_FILE "ON" CACHE BOOL "Force Ninja to use response files.")
add_executable(ReadExodusData MACOSX_BUNDLE ReadExodusData.cxx )
  target_link_libraries(ReadExodusData PRIVATE ${VTK_LIBRARIES}
)
# vtk_module_autoinit is needed
vtk_module_autoinit(
  TARGETS ReadExodusData
  MODULES ${VTK_LIBRARIES}
)

Download and Build ReadExodusData

Click here to download ReadExodusData and its CMakeLists.txt file. Once the tarball ReadExodusData.tar has been downloaded and extracted,

cd ReadExodusData/build

If VTK is installed:

cmake ..

If VTK is not installed but compiled on your system, you will need to specify the path to your VTK build:

cmake -DVTK_DIR:PATH=/home/me/vtk_build ..

Build the project:

make

and run it:

./ReadExodusData

WINDOWS USERS

Be sure to add the VTK bin directory to your path. This will resolve the VTK dll's at run time.