.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "tutorials\export_data\vtu_export.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_tutorials_export_data_vtu_export.py: .. _ref_tutorials_export_data_vtu_export: Export DPF Objects to VTU ========================== Export DPF data objects (mesh and fields) directly to VTU format using the ``vtu_export`` operator for fine-grained control over what gets exported. The ``vtu_export`` operator is ideal when you have already processed or modified data in DPF: filtered, averaged, transformed, or assembled from multiple sources. Unlike ``migrate_to_vtu``, which exports directly from result files, this operator works from DPF objects (|MeshedRegion| and |Field|/|FieldsContainer|). You can use it to export custom computed fields as well as data that does not come directly from simulation files. .. note:: Use ``vtu_export`` when working with processed DPF objects or custom workflows. To export entire result files without preprocessing, see :ref:`ref_tutorials_export_data_migrate_to_vtu`. .. GENERATED FROM PYTHON SOURCE LINES 48-52 Import Required Modules ----------------------- Import the required modules. .. GENERATED FROM PYTHON SOURCE LINES 52-60 .. code-block:: Python from pathlib import Path import numpy as np from ansys.dpf import core as dpf from ansys.dpf.core import examples, operators as ops .. GENERATED FROM PYTHON SOURCE LINES 61-66 Set Up the Model ---------------- Load a static structural result file and extract the |MeshedRegion| that will be reused throughout this tutorial. .. GENERATED FROM PYTHON SOURCE LINES 66-74 .. code-block:: Python # Load the result file and create a Model result_file = examples.find_static_rst() my_model = dpf.Model(data_sources=dpf.DataSources(result_path=result_file)) # Get the MeshedRegion mesh = my_model.metadata.meshed_region .. GENERATED FROM PYTHON SOURCE LINES 75-79 Basic VTU Export ---------------- Export a mesh and |FieldsContainer| that you have already loaded in DPF. .. GENERATED FROM PYTHON SOURCE LINES 79-104 .. code-block:: Python # Get displacement results as a FieldsContainer for all time steps displacement_fc = my_model.results.displacement.on_all_time_freqs.eval() # Create the output directory output_dir = "./dpf_objects_export" Path(output_dir).mkdir(parents=True, exist_ok=True) # Create the vtu_export operator export_op = ops.serialization.vtu_export( directory=output_dir, mesh=mesh, fields1=displacement_fc, base_name="displacement_results", ) # Execute the export and retrieve the output DataSources export_op.eval() # List the exported files from the output directory exported_files = list(Path(output_dir).glob("*.vtu")) print(f"Exported {len(exported_files)} VTU file(s)") for path in exported_files[:3]: print(f" {path}") .. rst-class:: sphx-glr-script-out .. code-block:: none Exported 1 VTU file(s) dpf_objects_export\displacement_results_T000000001.vtu .. GENERATED FROM PYTHON SOURCE LINES 105-110 Export Multiple Field Types --------------------------- Export multiple field types (displacement, stress, etc.) simultaneously by using both the ``fields1`` and ``fields2`` input pins. .. GENERATED FROM PYTHON SOURCE LINES 110-139 .. code-block:: Python # Create the output directory output_dir_multi = "./multi_field_export" Path(output_dir_multi).mkdir(parents=True, exist_ok=True) # Get stress results for all time steps (elemental_nodal location) stress_fc = my_model.results.stress.on_all_time_freqs.eval() # Average elemental_nodal stress to nodal location — vtu_export requires # Nodal or Elemental data, not elemental_nodal stress_nodal_fc = ops.averaging.elemental_nodal_to_nodal_fc(fields_container=stress_fc).eval() # Create the vtu_export operator with multiple fields export_multi = ops.serialization.vtu_export( directory=output_dir_multi, mesh=mesh, fields1=displacement_fc, fields2=stress_nodal_fc, base_name="multi_field_results", ) # Execute the export export_multi.eval() print( f"Exported {len(list(Path(output_dir_multi).glob('*.vtu')))} " "VTU file(s) with displacement and stress" ) .. rst-class:: sphx-glr-script-out .. code-block:: none Exported 1 VTU file(s) with displacement and stress .. GENERATED FROM PYTHON SOURCE LINES 140-145 Export Processed Data --------------------- Export data that was processed through DPF operators. Here, the von Mises equivalent stress is computed from the stress |FieldsContainer| before export. .. GENERATED FROM PYTHON SOURCE LINES 145-170 .. code-block:: Python # Create the output directory output_dir_processed = "./processed_export" Path(output_dir_processed).mkdir(parents=True, exist_ok=True) # Compute the Von Mises equivalent stress from the stress FieldsContainer # von_mises_eqv_fc on elemental_nodal stress produces elemental_nodal scalars; # average to nodal location before export von_mises_en_fc = ops.invariant.von_mises_eqv_fc(fields_container=stress_fc).eval() von_mises_fc = ops.averaging.elemental_nodal_to_nodal_fc(fields_container=von_mises_en_fc).eval() # Export the processed Von Mises stress FieldsContainer export_processed = ops.serialization.vtu_export( directory=output_dir_processed, mesh=mesh, fields1=von_mises_fc, base_name="von_mises_stress", ) export_processed.eval() print( f"Exported Von Mises stress to {len(list(Path(output_dir_processed).glob('*.vtu')))} VTU file(s)" ) .. rst-class:: sphx-glr-script-out .. code-block:: none Exported Von Mises stress to 1 VTU file(s) .. GENERATED FROM PYTHON SOURCE LINES 171-176 Export a Single Time Step ------------------------- Export only a specific time step by working with an individual |Field| instead of a |FieldsContainer|. .. GENERATED FROM PYTHON SOURCE LINES 176-205 .. code-block:: Python # Create the output directory output_dir_single = "./single_timestep_export" Path(output_dir_single).mkdir(parents=True, exist_ok=True) # Get displacement for only the first time step time_scoping = dpf.Scoping(location=dpf.locations.time_freq) time_scoping.ids = [1] displacement_single_fc = my_model.results.displacement.on_time_scoping(time_scoping).eval() # Get the first Field from the FieldsContainer disp_field = displacement_single_fc[0] # Export the single Field export_single = ops.serialization.vtu_export( directory=output_dir_single, mesh=mesh, fields1=disp_field, base_name="displacement_timestep_1", ) # Execute the export export_single.eval() print( f"Exported single time step to {len(list(Path(output_dir_single).glob('*.vtu')))} VTU file(s)" ) .. rst-class:: sphx-glr-script-out .. code-block:: none Exported single time step to 1 VTU file(s) .. GENERATED FROM PYTHON SOURCE LINES 206-211 Export with Mesh Property Fields --------------------------------- Include mesh |PropertyField| data (such as material IDs) in the VTU output by passing it to the ``fields2`` input pin. .. GENERATED FROM PYTHON SOURCE LINES 211-236 .. code-block:: Python # Create the output directory output_dir_props = "./property_export" Path(output_dir_props).mkdir(parents=True, exist_ok=True) # Get the material property field from the MeshedRegion mat_prop = mesh.property_field("mat") # Export displacement results together with the material property field export_props = ops.serialization.vtu_export( directory=output_dir_props, mesh=mesh, fields1=displacement_fc, fields2=mat_prop, base_name="results_with_material", ) # Execute the export export_props.eval() print( f"Exported results with material properties to " f"{len(list(Path(output_dir_props).glob('*.vtu')))} VTU file(s)" ) .. rst-class:: sphx-glr-script-out .. code-block:: none Exported results with material properties to 1 VTU file(s) .. GENERATED FROM PYTHON SOURCE LINES 237-248 Control Output Format --------------------- Choose different write modes for different trade-offs between file size and readability. The ``write_mode`` parameter accepts: - ``rawbinarycompressed`` (default): Smallest file size - ``rawbinary``: Binary format without compression - ``base64appended``: Base64-encoded binary data appended to XML - ``base64inline``: Base64-encoded binary data inline with XML - ``ascii``: Human-readable text format (useful for debugging) .. GENERATED FROM PYTHON SOURCE LINES 248-285 .. code-block:: Python # Create the output directory for format comparison output_dir_fmt = "./format_comparison" Path(output_dir_fmt).mkdir(parents=True, exist_ok=True) # Get a single Field for this comparison disp_field_fmt = displacement_fc[0] # Export in compressed binary mode (default) export_binary = ops.serialization.vtu_export( directory=output_dir_fmt, mesh=mesh, fields1=disp_field_fmt, base_name="displacement_binary", write_mode="rawbinarycompressed", ) # Export in ASCII mode for comparison export_ascii = ops.serialization.vtu_export( directory=output_dir_fmt, mesh=mesh, fields1=disp_field_fmt, base_name="displacement_ascii", write_mode="ascii", ) # Execute both exports export_binary.eval() export_ascii.eval() # Compare file sizes binary_file = next(Path(output_dir_fmt).glob("*binary*.vtu")) ascii_file = next(Path(output_dir_fmt).glob("*ascii*.vtu")) print(f"Compressed binary file size: {binary_file.stat().st_size / 1024:.2f} KB") print(f"ASCII file size: {ascii_file.stat().st_size / 1024:.2f} KB") .. rst-class:: sphx-glr-script-out .. code-block:: none Compressed binary file size: 2.88 KB ASCII file size: 5.11 KB .. GENERATED FROM PYTHON SOURCE LINES 286-291 Export as Point Cloud --------------------- Set ``as_point_cloud=True`` to export only mesh nodes without element connectivity. This is useful for sparse data or particle simulations. .. GENERATED FROM PYTHON SOURCE LINES 291-311 .. code-block:: Python # Create the output directory output_dir_cloud = "./point_cloud_export" Path(output_dir_cloud).mkdir(parents=True, exist_ok=True) # Export displacement as a point cloud (nodes only, no element connectivity) export_cloud = ops.serialization.vtu_export( directory=output_dir_cloud, mesh=mesh, fields1=disp_field_fmt, base_name="displacement_points", as_point_cloud=True, ) # Execute the export export_cloud.eval() print(f"Exported point cloud to {len(list(Path(output_dir_cloud).glob('*.vtu')))} VTU file(s)") print("Note: File contains only point data without element connectivity") .. rst-class:: sphx-glr-script-out .. code-block:: none Exported point cloud to 1 VTU file(s) Note: File contains only point data without element connectivity .. GENERATED FROM PYTHON SOURCE LINES 312-317 Create and Export Custom Data ----------------------------- Create a custom scalar |Field| and export it alongside the mesh. Custom fields can represent any nodal quantity not available in the original result file. .. GENERATED FROM PYTHON SOURCE LINES 317-346 .. code-block:: Python # Create the output directory output_dir_custom = "./custom_data_export" Path(output_dir_custom).mkdir(parents=True, exist_ok=True) # Create a custom scalar Field associated to mesh nodes custom_field = dpf.Field(location=dpf.locations.nodal, nature=dpf.natures.scalar) custom_field.scoping = mesh.nodes.scoping # Compute the distance from origin for each node coords = mesh.nodes.coordinates_field.data distances = np.sqrt(np.sum(coords**2, axis=1)) custom_field.data = distances # Name the field so it is identifiable in ParaView or VisIt custom_field.name = "distance_from_origin" # Export the custom Field export_custom = ops.serialization.vtu_export( directory=output_dir_custom, mesh=mesh, fields1=custom_field, base_name="custom_distance_field", ) # Execute the export export_custom.eval() print(f"Exported custom field to {len(list(Path(output_dir_custom).glob('*.vtu')))} VTU file(s)") .. rst-class:: sphx-glr-script-out .. code-block:: none Exported custom field to 1 VTU file(s) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.511 seconds) .. _sphx_glr_download_tutorials_export_data_vtu_export.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: vtu_export.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: vtu_export.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: vtu_export.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_