How can I import Nastran superelement data from a .dmig file?

M
M Member, Employee Posts: 252
50 Answers 100 Comments 100 Likes Second Anniversary
✭✭✭✭
edited October 2024 in Structures

You can use the Ansys Mechanical Automation/Python interface to import GRID data from a Natran file into Ansys Mechanical.

Nastran uses a different format and language than Ansys but you can, with some caveats, parse the data from a Nastran superelement and bring that data into Ansys Mechanical. Because the format is not very nice, the default import of an Imported Condensed Part lists zeros for the node data for super elements. A few lines of code (and some careful checking by the user) can help import the X,Y,Z data.

The first block simply reads in the data.
The GRID data has a very regular format but does not have a break between the values and moves the Z node value into a separate line. For this reason you need to use the locations in the line of data to parse out the values.
This is not guaranteed to work, so naturally one would want to check that the values match the imports!

Note the only check is for lines in the requested input file that start with GRID.
The X and Y data then need to be starting at space 40 (x_start) and be 16 spaces long (format length). These values are parameters and if for some reason your import doesn't work, open the .dmig file and determine where the X value starts (x_start) and the length to the start of the Y value (format length).

# Read in GRID* data
input =  {}
path2dmig = ExtAPI.UserInterface.UIRenderer.ShowFileOpenDialog()
format_length = 16
x_start = 40
with open(path2dmig,'r') as f:
    data = f.readlines()
    # get the data out of the GRID lines and the next line:
    ctr = 0
    for line in data:
        if line[0:4] == 'GRID':
            grid_data = line.split()
            point = int(grid_data[1])
            #xy = grid_data.[-1]
            x = float(line[x_start:x_start+format_length].replace('D','E'))
            y = float(line[x_start+format_length:x_start+format_length*2].replace('D','E'))
            # write to an input parameter 
            z = float(data[ctr+1].split()[-1].replace('D','E'))
            input[point] = [x,y,z]
        ctr+=1

The first section will just create a dictionary of the X,Y,Z values.

I only had a small test file so if you have a large .dmig file, you might want to put in a break after the last GRID+1. Figure out what ends the code block of GRID and if found, break.

The second part of the code assumes you already have defined the imported condensed part and it has it's default name ("Imported Condensed Part").

Do not import it as that will lock the node values!

Then run the script and the second line (ReadInterface) will read the default node location (zeros).
The loop after will correct them from the input data created in block 1.

se = ExtAPI.DataModel.GetObjectsByName("Imported Condensed Part")[0]

se.ReadInterfaceData()

for line in input:
    node = line
    x = input[node][0]
    y = input[node][1]
    z = input[node][2]
    print (node)
    se.UpdateNodeLocation(node,Quantity(x," mm"),Quantity(y," mm"),Quantity(z," mm"),Quantity("0 [deg]"),Quantity("0 [deg]"),Quantity("0 [deg]"))

Tree.Refresh() 

se.ImportCondensedPart() # test!

The list of locations in the Imported Condensed Part should then update to the input values from the first block.

Always confirm any input values since different formatting of the values could cause the parsing to get the wrong data!

Special thanks to Patrick for helping.

Comments

  • James Derrick
    James Derrick Administrator, Employee Posts: 303
    Ancient Membership 100 Comments 100 Likes 25 Answers
    admin

    Thanks @M ! This is great stuff. Would you be interested in spinning this out into an article on the portal?