Extracting results from a Path in Transient analysis and creating a 3D Surface Plot in PyDPF

George Tzortzopoulos
George Tzortzopoulos Member, Employee Posts: 12
10 Comments First Anniversary Name Dropper Photogenic
✭✭✭

How to extract results from a path in a Transient analysis and create a spatiotemporal 3D surface plot in PyDPF?

Tagged:

Comments

  • George Tzortzopoulos
    George Tzortzopoulos Member, Employee Posts: 12
    10 Comments First Anniversary Name Dropper Photogenic
    ✭✭✭
    edited July 2023

    The methodology for constructing a conceptual path (not an actual path + result in the tree) with start+end points and # of divisions and mapping a result to these points in PyDPF can be found in the link below:

    For creating now the spatiotemporal 3D surface plot of the path result, the Numpy and Matplotlib Python libraries are required. Here is an example of working with Temperature results:

    from ansys.dpf import core as dpf
    from ansys.dpf import core as dpf
    from ansys.dpf.core.plotter import DpfPlotter import numpy as np
    import matplotlib.pyplot as plt
    from matplotlib import cm """
    Load result file and create model database
    """ path = "my_Path" ds = dpf.DataSources(path)
    model = dpf.Model(ds) mesh = model.metadata.meshed_region """
    Select preferable Named Selection
    """ get_NSs = model.metadata.available_named_selections my_mesh_scoping = model.metadata.named_selection("my_Named_Selection") """
    Scope Named Selection to corresponding Mesh
    """ scoping_op = dpf.operators.mesh.from_scoping()
    scoping_op.inputs.scoping.connect(my_mesh_scoping)
    scoping_op.inputs.mesh.connect(mesh) my_mesh = scoping_op.outputs.mesh() """
    Get Temperature results at all time instants
    """ get_all_temps = model.results.temperature.on_all_time_freqs
    get_fieldContainers_temperature = get_all_temps(mesh_scoping=my_mesh_scoping).eval() """
    Create Path Field
    """ startCoordinate = [-0.022225, -0.012065, 0.00375918]
    endCoordinate = [-0.046355, 0.012065, 0.00375918]
    ipoints = 47 dxyz = (np.array(endCoordinate) - np.array(startCoordinate))/(ipoints+1) pathCoordinates = [startCoordinate]
    for i in range(1,ipoints+2):
    pathCoordinates.append((startCoordinate + i * dxyz).tolist()) field_coord = dpf.fields_factory.create_3d_vector_field(len(pathCoordinates))
    field_coord.data = pathCoordinates
    field_coord.scoping.ids = list(range(1, len(pathCoordinates) + 1)) """
    Map Temperatures to Path coordinates
    """ mapping_op = dpf.operators.mapping.on_coordinates(
    fields_container=get_fieldContainers_temperature,
    coordinates=field_coord,
    create_support=True,
    mesh=mesh
    )
    temp_fields_mapped = mapping_op.outputs.fields_container() """
    Plot Temperature w.r.t to Path Distance & Time using Matplotlib - 3D Surface Plot
    """ tf = model.metadata.time_freq_support
    time = np.array(tf.time_frequencies.data) start = np.array(pathCoordinates[0])
    distance = np.zeros(len(pathCoordinates))
    for i in range(len(pathCoordinates)):
    finish = np.array(pathCoordinates[i])
    distance[i] = np.linalg.norm(start-finish) Temps = np.zeros((len(time), len(distance)))
    for i in range(Temps.shape[0]):
    Temps[i,:] = np.array(temp_fields_mapped[i].data) Distance, Time = np.meshgrid(distance*1000, time) fig, ax = plt.subplots(subplot_kw={"projection": "3d"}) surf = ax.plot_surface(Time, Distance, Temps, cmap=cm.coolwarm, linewidth=0, antialiased=False)
    fig.colorbar(surf, shrink=0.5, aspect=5, label="Temperature [degC]")
    ax.view_init(elev=30, azim=30, roll=0)
    ax.set_xlabel("Time [s]")
    ax.set_ylabel("Path Distance [mm]")
    ax.set_zlabel("Temperature [degC]")

    plt.show()