Export data contained in a DataFrame to a new format#

In this script a static simulation is used as an example to show how to export data from a DataFrame to another format.

Perform required imports#

Perform required imports. # This example uses a supplied file that you can get by importing the DPF examples package.

from ansys.dpf import post
from ansys.dpf.post import examples

Get Simulation object#

Get the Simulation object that allows access to the result. The Simulation object must be instantiated with the path for the result file. For example, "C:/Users/user/my_result.rst" on Windows or "/home/user/my_result.rst" on Linux.

example_path = examples.download_crankshaft()
# to automatically detect the simulation type, use:
simulation = post.load_simulation(example_path)

# to enable auto-completion, use the equivalent:
simulation = post.StaticMechanicalSimulation(example_path)

# print the simulation to get an overview of what's available
print(simulation)
Static Mechanical Simulation.


Data Sources
------------------------------
/opt/hostedtoolcache/Python/3.10.14/x64/lib/python3.10/site-packages/ansys/dpf/core/examples/result_files/crankshaft/crankshaft.rst

DPF Model
------------------------------
Static analysis
Unit system: MKS: m, kg, N, s, V, A, degC
Physics Type: Mechanical
Available results:
     -  displacement: Nodal Displacement
     -  velocity: Nodal Velocity
     -  acceleration: Nodal Acceleration
     -  reaction_force: Nodal Force
     -  stress: ElementalNodal Stress
     -  elemental_volume: Elemental Volume
     -  stiffness_matrix_energy: Elemental Energy-stiffness matrix
     -  artificial_hourglass_energy: Elemental Hourglass Energy
     -  thermal_dissipation_energy: Elemental thermal dissipation energy
     -  kinetic_energy: Elemental Kinetic Energy
     -  co_energy: Elemental co-energy
     -  incremental_energy: Elemental incremental energy
     -  elastic_strain: ElementalNodal Strain
     -  element_euler_angles: ElementalNodal Element Euler Angles
     -  structural_temperature: ElementalNodal Structural temperature
------------------------------
DPF  Meshed Region:
  69762 nodes
  39315 elements
  Unit: m
  With solid (3D) elements
------------------------------
DPF  Time/Freq Support:
  Number of sets: 3
Cumulative     Time (s)       LoadStep       Substep
1              1.000000       1              1
2              2.000000       1              2
3              3.000000       1              3

Extract elemental nodal stress#

stress = simulation.stress_nodal(all_sets=True)
print(stress)
            results      S (Pa)
            set_ids           1           2           3
node_ids components
    4872         XX  1.8630e+05 -2.6097e+05 -1.4740e+06
                 YY  8.2844e+06  2.8386e+07  5.9846e+07
                 ZZ -1.3700e+07 -7.9701e+06  1.7550e+07
                 XY  5.9991e+05  6.6079e+05 -7.5702e+05
                 YZ -1.9344e+08 -3.8683e+08 -5.7889e+08
                 XZ -6.2727e+06 -2.8690e+06  1.0085e+07
     ...        ...         ...         ...         ...

Export to a numpy.ndarray#

# To export to a numpy.ndarray, the DataFrame must contain data only for a single combination
# of values for column labels.

# select one step as our dataframe contains data for several
stress_1 = stress.select(set_ids=1)
print(stress_1)

# export it as a numpy.ndarray
stress_1_array = stress_1.array
print(stress_1_array)
             results      S (Pa)
             set_ids           1
 node_ids components
     4872         XX  1.8630e+05
                  YY  8.2844e+06
                  ZZ -1.3700e+07
                  XY  5.9991e+05
                  YZ -1.9344e+08
                  XZ -6.2727e+06
      ...        ...         ...

[[ 1.86304666e+05  8.28436396e+06 -1.37003496e+07  5.99907420e+05
  -1.93439042e+08 -6.27268473e+06]
 [-4.26076223e+05  4.27353118e+06  3.61489620e+05  1.27870163e+07
  -1.64836014e+08  3.78081724e+05]
 [ 4.36222667e+06  2.02005176e+07  3.49514000e+07  5.31671006e+07
  -1.69700130e+08  4.16285682e+06]
 ...
 [ 2.43008628e+06  3.45306104e+05 -6.96869804e+07 -1.88974926e+06
   1.78308703e+08  3.70653393e+07]
 [-3.15639127e+06 -4.87652891e+07 -1.52290762e+08  3.59354599e+07
   1.97891422e+08  2.65947999e+07]
 [ 6.32054643e+05 -3.24065639e+07 -1.18464495e+08  4.38019212e+07
   2.00763823e+08  2.51322083e+07]]

Total running time of the script: (0 minutes 0.389 seconds)

Gallery generated by Sphinx-Gallery