How do I convert LS-Dyna results to USD format?

Member, Moderator, Employee Posts: 472
100 Answers 250 Likes 100 Comments Second Anniversary
✭✭✭✭
edited November 2023 in Structures

How do I convert LS-Dyna results to USD format?

Tagged:

Comments

  • Member, Moderator, Employee Posts: 472
    100 Answers 250 Likes 100 Comments Second Anniversary
    ✭✭✭✭
    edited November 2023

    This example converts a Shell Model to USD format and stores some results at several time-steps as Attributes for the Primitive "/d3plot"

    Tested with:

    1. usd-core version 23.11
    2. Python version 3.10.10
    3. ansys-dpf-core 0.10.0
    1. from pxr import Sdf, Usd, UsdGeom
    2. from ansys.dpf import core as dpf
    3.  
    4. timescopingToken = "ansys:lsdyna:timescoping"
    5. nodeIndicesToken = "ansys:lsdyna:nodeindices"
    6. attributesTokens = ["ansys:lsdyna:acceleration", "ansys:lsdyna:displacement", "ansys:lsdyna:stress",
    7. "ansys:lsdyna:velocity"]
    8. globalFieldTokenK = ["ansys:lsdyna:global_k", "ansys:lsdyna:global_k_time"]
    9. globalFieldTokenU = ["ansys:lsdyna:global_u", "ansys:lsdyna:global_u_time"]
    10. globalFieldTokenH = ["ansys:lsdyna:global_h", "ansys:lsdyna:global_h_time"]
    11. globalFieldTokenV = ["ansys:lsdyna:global_v", "ansys:lsdyna:global_v_time"]
    12. descriptionToken = "ansy:dpf:description"
    13. fieldTypeToken = "ansys:dpf:selectedFieldType"
    14.  
    15. nbFramesPerTimestep = 5
    16.  
    17. # create mesh from a d3plot file
    18. def create_dpf_prim_with_stage(filepath: str, primname: str, stage):
    19. # loads the data
    20. print("Loading model " + filepath)
    21. ds = dpf.DataSources()
    22. ds.set_result_file_path(filepath, "d3plot") # Result file
    23.  
    24. # Instantiate Model Class
    25. model = dpf.Model(ds)
    26. nbResults = model.metadata.time_freq_support.n_sets
    27. timeScoping = []
    28. for ts in range(nbResults):
    29. timeScoping.append(ts)
    30.  
    31. # Whole mesh
    32. mesh = model.metadata.meshed_region
    33.  
    34. # Extract skin mesh
    35. op = dpf.operators.mesh.skin(mesh=mesh)
    36. skin_mesh = op.outputs.mesh.get_data()
    37.  
    38. # Extract Coordinates results
    39. coord = model.results.coordinates(time_scoping=timeScoping)
    40. coord_fc = coord.outputs.fields_container.get_data()
    41.  
    42. # Extract Acceleration results
    43. accel = model.results.acceleration(time_scoping=timeScoping)
    44. accel_fc = accel.outputs.fields_container.get_data()
    45.  
    46. # Extract Displacement results
    47. disp = model.results.displacement(time_scoping=timeScoping)
    48. disp_fc = disp.outputs.fields_container.get_data()
    49.  
    50. # Extract Stress results
    51. stress = model.results.stress(time_scoping=timeScoping)
    52. stress_fc = stress.outputs.fields_container.get_data()
    53.  
    54. # Extract Velocity results
    55. vel = model.results.velocity(time_scoping=timeScoping)
    56. vel_fc = vel.outputs.fields_container.get_data()
    57.  
    58. # globals
    59. K = model.results.global_kinetic_energy.eval()
    60. U = model.results.global_internal_energy().eval()
    61. H = model.results.global_total_energy().eval()
    62. V = model.results.global_velocity().eval()
    63.  
    64. # add info to the current stage
    65. usd_mesh = UsdGeom.Mesh.Define(stage, primname)
    66.  
    67. usd_mesh.GetPrim().CreateAttribute(globalFieldTokenK[0], Sdf.ValueTypeNames.FloatArray).Set(K[0].data)
    68. usd_mesh.GetPrim().CreateAttribute(globalFieldTokenU[0], Sdf.ValueTypeNames.FloatArray).Set(U[0].data)
    69. usd_mesh.GetPrim().CreateAttribute(globalFieldTokenH[0], Sdf.ValueTypeNames.FloatArray).Set(H[0].data)
    70. usd_mesh.GetPrim().CreateAttribute(globalFieldTokenV[0], Sdf.ValueTypeNames.FloatArray).Set(V[0].data)
    71.  
    72. usd_mesh.GetPrim().CreateAttribute(globalFieldTokenK[1], Sdf.ValueTypeNames.FloatArray).Set(
    73. K.time_freq_support.time_frequencies.data)
    74. usd_mesh.GetPrim().CreateAttribute(globalFieldTokenU[1], Sdf.ValueTypeNames.FloatArray).Set(
    75. U.time_freq_support.time_frequencies.data)
    76. usd_mesh.GetPrim().CreateAttribute(globalFieldTokenH[1], Sdf.ValueTypeNames.FloatArray).Set(
    77. H.time_freq_support.time_frequencies.data)
    78. usd_mesh.GetPrim().CreateAttribute(globalFieldTokenV[1], Sdf.ValueTypeNames.FloatArray).Set(
    79. V.time_freq_support.time_frequencies.data)
    80.  
    81. # add number of timesteps as an attribute
    82. usd_mesh.GetPrim().CreateAttribute(timescopingToken, Sdf.ValueTypeNames.IntArray).Set(timeScoping)
    83.  
    84. """indices, faceCount and nodeIndices doesn't change overtime."""
    85. nbElements = len(mesh.elements)
    86. indices = []
    87. faceCounts = []
    88. nodeIndices = []
    89.  
    90. # fills vertices, indices and face counts
    91. for elementId in range(nbElements):
    92. element = mesh.elements[elementId]
    93. # face represents a triangle or quad
    94. faceCounts.append(len(element.nodes))
    95. # adds vertex coordinates
    96. for node in element.nodes:
    97. nodeIndices.append(node.index)
    98. # add indices
    99. indices.append(len(indices))
    100.  
    101. # Create Indices
    102. usd_mesh.GetFaceVertexIndicesAttr().Set(indices)
    103.  
    104. # Face counts (3 for tris, 4 for quads...)
    105. usd_mesh.GetFaceVertexCountsAttr().Set(faceCounts)
    106.  
    107. # Node indoces for further field access
    108. usd_mesh.GetPrim().CreateAttribute(nodeIndicesToken, Sdf.ValueTypeNames.IntArray).Set(nodeIndices)
    109.  
    110. # descriptions
    111. usd_mesh.GetPrim().CreateAttribute(descriptionToken, Sdf.ValueTypeNames.String).Set(
    112. model.metadata.result_info.__str__())
    113.  
    114. # Go through all the timesteps
    115. for i in range(nbResults):
    116. print("Importing timecode " + str(i) + "/" + str(nbResults))
    117.  
    118. # Create the current timecode
    119. timeCode = Usd.TimeCode(float(i * nbFramesPerTimestep))
    120.  
    121. # set custom attributes in the USD
    122. usd_mesh.GetPrim().CreateAttribute(attributesTokens[0], Sdf.ValueTypeNames.Float3Array).Set(accel_fc[i].data,
    123. timeCode)
    124. usd_mesh.GetPrim().CreateAttribute(attributesTokens[1], Sdf.ValueTypeNames.Float3Array).Set(disp_fc[i].data,
    125. timeCode)
    126. usd_mesh.GetPrim().CreateAttribute(attributesTokens[2], Sdf.ValueTypeNames.Float3Array).Set(stress_fc[i].data,
    127. timeCode)
    128. usd_mesh.GetPrim().CreateAttribute(attributesTokens[3], Sdf.ValueTypeNames.Float3Array).Set(vel_fc[i].data,
    129. timeCode)
    130.  
    131. # Fill vertices, indices and faces arrays
    132. vertices = []
    133.  
    134. # fills vertices
    135. curCoord = coord_fc[i].data
    136. for nodeIdx in nodeIndices:
    137. vertices.append(curCoord[nodeIdx])
    138.  
    139. # Create vertices
    140. usd_mesh.GetPointsAttr().Set(vertices, timeCode)
    141.  
    142.  
    143. def export():
    144. # create a new stage
    145. stage = Usd.Stage.CreateInMemory('dpf_dyna_ov.usd')
    146.  
    147. # convert
    148. create_dpf_prim_with_stage(r"\LS-DYNA\RESULTS\PATH\d3plot", "/d3plot", stage)
    149.  
    150. # save the stage in the end
    151. stage.GetRootLayer().Export(r"\Output\path\dpf_dyna_ov.usda")
    152.  
    153.  
    154. # Entry point
    155. if __name__ == '__main__':
    156. export()
    157.  

Welcome!

It looks like you're new here. Sign in or register to get started.