Using python library mapdl extract Eequivalent stress of the result(file.rst)?

Options
Erubus
Erubus Member Posts: 4
First Comment
edited April 16 in Structures

I used the following code to extract the equivalent stress and displacement. Currently, the displacement can correspond to the workbench data, but the equivalent stress is not. May I ask if there is a problem with my extraction code? The code is as follows:

displacement.py

def extract_displacement(self: rst.Result, rsets=None):
    if rsets is None:
        rsets = range(self.nsets)
    elif isinstance(rsets, int):
        rsets = [rsets]
    elif not isinstance(rsets, rst.Iterable):
        raise TypeError("rsets must be an iterable like [0, 1, 2] or range(3)")

    displacement_data = {}

    for i in rsets:
        nnum, disp = self.nodal_displacement(i)

        # total displacement
        total_displacement = np.sqrt(np.sum(disp ** 2, axis=1))
        # max and min
        max_displacement = np.max(total_displacement)
        min_displacement = np.min(total_displacement)

        # x、y、z component displacement
        max_x = np.max(disp[:, 0])
        min_x = np.min(disp[:, 0])
        max_y = np.max(disp[:, 1])
        min_y = np.min(disp[:, 1])
        max_z = np.max(disp[:, 2])
        min_z = np.min(disp[:, 2])

        # write to file
        displacement_data[i] = {
            "total": {
                "max": max_displacement,
                "min": min_displacement,
            },
            "x": {
                "max": max_x,
                "min": min_x,
            },
            "y": {
                "max": max_y,
                "min": min_y,
            },
            "z": {
                "max": max_z,
                "min": min_z,
            },
        }

        # export displacement picture
        result.plot_nodal_displacement(
            i,
            screenshot='displacement_{}.png'.format(i),
            show_displacement=True,
            window_size=[800, 600],
            interactive=False
        )

    with open('displacement.json', 'w') as json_file:
        json.dump(displacement_data, json_file, indent=4)

    return

stress.py

def extract_stress(self: rst.Result, rsets=None):
    if rsets is None:
        rsets = range(self.nsets)
    elif isinstance(rsets, int):
        rsets = [rsets]
    elif not isinstance(rsets, rst.Iterable):
        raise TypeError("rsets must be an iterable like [0, 1, 2] or range(3)")

    stress_data = {}
    for i in rsets:
        max_sigma1 = max_sigma2 = max_sigma3 = max_sint = max_seqv = float('-inf')
        enum, element_stresses, enode = self.element_stress(i, principal=True)

        if element_stresses is None or not any(np.isfinite(stress).any() for stress in element_stresses):
            print(f"No valid stress data available for result set {i}.")
            continue

        for stress_array in element_stresses:
            if np.isnan(stress_array).all():
                continue

            max_sigma1 = max(max_sigma1, np.max(stress_array[:, 0]))
            max_sigma2 = max(max_sigma2, np.max(stress_array[:, 1]))
            max_sigma3 = max(max_sigma3, np.max(stress_array[:, 2]))
            max_sint = max(max_sint, np.max(stress_array[:, 3]))
            max_seqv = max(max_seqv, np.max(stress_array[:, 4]))

        # save map
        stress_data[i] = {
            "max_sigma1": max_sigma1,
            "max_sigma2": max_sigma2,
            "max_sigma3": max_sigma3,
            "max_sint": max_sint,
            "max_seqv": max_seqv
        }

        # export stress picture
        for comp in ["X", "Y", "Z", "XY", "XZ", "YZ"]:
            self.plot_nodal_stress(
                i,
                comp=comp,
                screenshot='stress_{}_{}.png'.format(i, comp),
                show_displacement=True,
                window_size=[800, 600],
                interactive=False
            )
    # export json
    with open('stress.json', 'w') as json_file:
        json.dump(stress_data, json_file, indent=4)

    return

I look forward to hearing from you. Thank you.

Answers