pip install Cython
export HDF5_DIR=/home/data/jcshi/nfs_project/NFS/contrib/hdf5
pip install pyCGNS-5.1.tar.gz
pyCGNS usage: Check this
webpage http://pycgns.sourceforge.net/PAT/readme.html
.
Use PAT
to manipulate the tree. Use MAP
to
load and save the cgns file.
Example script: tecplot2cgns.py
as follows,
import numpy as np
= "sol/sol_tec.dat"
tec_fpath
#############################
# Read tecplot file header
# Assume that each line gives info of each section
# 1st line is variables
# 2nd line is zone info
# 3rd line is datatype
#############################
= open(tec_fpath,'r')
tec_f
= tec_f.readline().upper()
line1 = tec_f.readline().upper()
line2 = tec_f.readline().upper()
line3
tec_f.close()
= line1.strip().split("=")
line1_list assert(line1_list[0] == 'VARIABLES')
= line1_list[1].split(',')
var_list
= line2.strip().split(",")
line2_list
= [i for i, s in enumerate(line2_list) if 'ZONETYPE' in s]
idx assert(len(idx)==1)
= line2_list[idx[0]].strip().split('=')[1]
zonetype_str
= [i for i, s in enumerate(line2_list) if 'SOLUTIONTIME' in s]
idx assert(len(idx)==1)
= line2_list[idx[0]].strip().split('=')[1]
sol_time_str = float(sol_time_str)
sol_time
= [i for i, s in enumerate(line2_list) if 'DATAPACKING' in s]
idx assert(len(idx)==1)
= line2_list[idx[0]].strip().split('=')[1]
datapacking_str assert(datapacking_str == 'POINT')
= [i for i, s in enumerate(line2_list) if 'NODES' in s]
idx assert(len(idx)==1)
= line2_list[idx[0]].strip().split('=')[1]
n_node = int(n_node)
n_node
= [i for i, s in enumerate(line2_list) if 'ELEMENTS' in s]
idx assert(len(idx)==1)
= line2_list[idx[0]].strip().split('=')[1]
n_elem = int(n_elem)
n_elem #############################
# Read the variables data
#############################
= np.genfromtxt(tec_fpath,skip_header=3,max_rows=n_node)
var_mat = var_mat[:,0]
x_arr = var_mat[:,1]
y_arr = var_mat[:,2]
cell_id_arr = var_mat[:,3]
p_order_arr = var_mat[:,4]
cpu_id_arr = var_mat[:,5]
rho_arr = var_mat[:,6]
u_arr = var_mat[:,7]
v_arr = var_mat[:,8]
p_arr
# For now, skip the elem connectivity section. Because I do not need it.
##############################
# Write to CGNS file
##############################
import CGNS.PAT.cgnsutils as CGU
import CGNS.PAT.cgnslib as CGL
import CGNS.PAT.cgnskeywords as CK
import CGNS.MAP as CGM
=CGL.newCGNSTree()
T=CGL.newBase(T,'hpMusic_base',2,2)
B=CGL.newZone(B,'Solution',np.array([n_node, n_elem, 0]),CK.Unstructured_s,'')
Z=CGL.newGridCoordinates(Z,name='GridCoordinates')
GC=CGL.newFlowSolution(Z,name='FlowSolution',gridlocation=None)
FS=CGU.getNodeByPath(FS,'GridLocation')
GL
CGU.nodeDelete(FS,GL)
= CGL.newDataArray(GC,'CoordinateX',value=x_arr)
coordinatex_node = CGL.newDataArray(GC,'CoordinateY',value=y_arr)
coordinatey_node = CGL.newDataArray(FS,'Density',value=rho_arr)
density_node = CGL.newDataArray(FS,'VelocityX',value=u_arr)
velocityx_node = CGL.newDataArray(FS,'VelocityY',value=v_arr)
velocityy_node = CGL.newDataArray(FS,'Pressure',value=p_arr)
p_node
"sol_tec.cgns",T) CGM.save(
The above Python script is not working. It dumps out a CGNS file. However, the data in that file is messed up.