Python script to convert initial stress data from a .unv file to external data format.

Akhil K S
Akhil K S Member, Employee Posts: 2
Ansys Employee Photogenic First Anniversary
✭✭✭

Following script can be modified based on the given .unv file. User need to set correctly the starting line number for node location and stress results.

import csv
from collections import OrderedDict

#User Inputs
locStart = 14 #Line where the Node location starting
stressStart = 37277 #Line where the Stress results starting

filepath_csv = "C:\\ExtData.csv"
filepath_unv = "C:\\Simufact.unv"


with open(filepath_unv, 'r') as unvfile:
    allLines = unvfile.readlines()

nodeLoc = OrderedDict()
count = 0
check = True
while check:
    index = locStart+count-1
    nID = allLines[index].strip().split()[0]
    if nID !="-1":
        nLoc = allLines[index+1].strip().split()
        nodeLoc[nID] = nLoc
        count +=2
    elif nID =="-1":
        check = False

stressComp = OrderedDict()
count = 0
check = True
while check:
    index = stressStart+count-1
    nID = allLines[index].strip().split()[0]
    if nID !="-1":
        sComp = allLines[index+1].strip().split()
        stressComp[nID] = sComp
        count +=2
    elif nID =="-1":
        check = False

combinedDict = OrderedDict()
for key in nodeLoc.keys():
    combinedDict[key] = nodeLoc[key]+stressComp[key]

with open(filepath_csv, 'wb') as csvfile:
    csvfile.write('NodeID'+','+'X'+','+'Y'+','+'Z'+','+'SX'+','+'SY'+','+'SZ'+','+'SXY'+','+'SYZ'+','+'SXZ')
    csvfile.write('\n')
    writer = csv.writer(csvfile)
    for key,value in combinedDict.items():
        writer.writerow([key,value[0],value[1],value[2],value[3],value[4],value[5],value[6],value[7],value[8]])
csvfile.close()