Exporting kinetic energy of each element in each cartesian directions for all modes in modal

Options
Pernelle Marone-Hitz
Pernelle Marone-Hitz Member, Moderator, Employee Posts: 813
First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member

Script for exporting kinetic energy of each element in each cartesian directions for all modes from modal analysis

Tagged:

Answers

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 813
    First Comment First Anniversary Ansys Employee Solution Developer Community of Practice Member
    edited December 2023
    Options

    Adapt the following code:

    def EvaluateDirectionalKE(mode):
        import math
        import units
        reader = Model.Analyses[1].GetResultsData()
        reader.CurrentResultSet = mode
        mesh = Model.Analyses[0].MeshData
        freqs = reader.ListTimeFreq
    
        elementIds = mesh.ElementIds
    
        deformationx = reader.GetResult("U")
        deformationx.SelectComponents(["X"])
        deformationy = reader.GetResult("U")
        deformationy.SelectComponents(["Y"])
        deformationz = reader.GetResult("U")
        deformationz.SelectComponents(["Z"])
    
        deformation_total = {}
        deformation_ratio_x = {}
        deformation_ratio_y = {}
        deformation_ratio_z = {}
        KE_X = {}
        KE_Y = {}
        KE_Z = {}
    
        KE = {}
        energy_result = reader.GetResult("ENERGY")
        input_unit = energy_result.GetComponentInfo("KINETIC").Unit
        conversion_factor = units.ConvertUnit(1., input_unit, "J", "Energy")
        energy_result.SelectComponents(["KINETIC"])
    
        for elementId in elementIds:
            # nodeIds = mesh.NodeIdsFromElementIds([elementId])
            element = mesh.ElementById(elementId)
            nodeIds = element.NodeIds
    
            deformation_x = deformationx.GetNodeValues(nodeIds)
            deformation_x_sum = math.fsum(deformation_x)
            deformation_y = deformationy.GetNodeValues(nodeIds)
            deformation_y_sum = math.fsum(deformation_y)
            deformation_z = deformationz.GetNodeValues(nodeIds)
            deformation_z_sum = math.fsum(deformation_z)
            deformation_total[elementId] = list(map(lambda x: math.sqrt(
                x[0]**2 + x[1]**2 + x[2]**2), zip(deformation_x, deformation_y, deformation_z)))
            deformation_total_sum = math.fsum(deformation_total[elementId])
            deformation_ratio_x[elementId] = deformation_x_sum / \
                deformation_total_sum
            deformation_ratio_y[elementId] = deformation_y_sum / \
                deformation_total_sum
            deformation_ratio_z[elementId] = deformation_z_sum / \
                deformation_total_sum
    
            energy_value = energy_result.GetElementValues([elementId], True)
            converted_energy_value = [
                value * conversion_factor for value in energy_value]
    
            KE_X[elementId] = [x * y for x,
                               y in zip(converted_energy_value, [deformation_ratio_x[elementId]])]
            KE_Y[elementId] = [x * y for x,
                               y in zip(converted_energy_value, [deformation_ratio_y[elementId]])]
            KE_Z[elementId] = [x * y for x,
                               y in zip(converted_energy_value, [deformation_ratio_z[elementId]])]
            KE[elementId] = [KE_X[elementId][0],
                             KE_Y[elementId][0], KE_Z[elementId][0]]
        fname = "D:\\KE_Mode_%i.txt" % mode
    
        with open(fname, 'w') as f:
            f.write('KINETIC ENERGY for MODE %i in X , Y , Z DIRECTIONS \n\n' % mode)
    
            f.write('ID: | Direction X | Direction Y | Direction Z \n\n')
            for k, v in KE.items():
                f.write(str(k) + ' , ' +
                        str(v[0]) + ' , ' + str(v[1]) + ' , ' + str(v[2]) + '\n')
        f.close()
        reader.Dispose()
    
    
    def modalKE():
        reader1 = Model.Analyses[0].GetResultsData()
        for mode in range(len(reader1.ListTimeFreq)):
            EvaluateDirectionalKE(mode)
    
    
    modalKE()
    
  • Mike.Thompson
    Mike.Thompson Member, Employee Posts: 300
    First Anniversary First Comment 5 Likes Ansys Employee
    edited December 2023
    Options

    Great solution @Pernelle Marone-Hitz .

    For general use:
    Note that the “mode” is not always the same as the result set. Depending on the type of solver used the results sets can also include the stability or also the negative freq. values. Look at the reader.ListTimeFreq property to see a list of the results set values if you are not sure.
    You can also use DPF to get the time frequency support for the results file which can be helpful to match the set number to the desired mode number