diff options
Diffstat (limited to 'pyfrm2xdmf')
-rwxr-xr-x | pyfrm2xdmf | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/pyfrm2xdmf b/pyfrm2xdmf new file mode 100755 index 0000000..a45a74e --- /dev/null +++ b/pyfrm2xdmf @@ -0,0 +1,99 @@ +#!/usr/bin/env python +import argparse +import os +import re +import string +from subprocess import check_output + +def meshFile(param): + global base + base, ext = os.path.splitext(param) + if ext.lower() != '.pyfrm': + raise argparse.ArgumentTypeError('Mesh file must have a .pyfrm extension') + return param + +parser = argparse.ArgumentParser(description="extract connectivities from mesh file") +parser.add_argument("mesh", help="mesh file (.pyfrm)", type=meshFile) +args = parser.parse_args() + +# Xdmf file +g = open(os.path.join(base + '.xdmf'), 'w') + +g.write('<?xml version="1.0" ?>\n') +g.write('<!DOCTYPE Xdmf SYSTEM "Xdmf.dtd" []>\n') +g.write('<Xdmf xmlns:xi="http://www.w3.org/2003/XInclude" Version="2.2">\n') +g.write(' <Domain>\n') + +# use 'h5ls' command to provide array dimensions +h5ls_output = check_output(["h5ls", args.mesh]) + +for line in h5ls_output.splitlines(): + spt = re.search('spt', line) # restrict to 'spt' arrays + if spt: + quad = re.search('quad', line) # restrict to quad cells + if quad: + order = {1:0, 2:1, 3:3, 4:2} # XDMF:PyFR vertex numbering + ncells = int(re.search(' (\d+),', line).group(1)) + nverts = 4 + ndims = 2 + chunk = string.split(line) + bname = re.sub('spt', 'con', chunk[0]) + fname = os.path.join(bname + '.xml') + partn = int(re.search('\d+', bname).group()) + g.write(' <Grid Name="Partition{}" GridType="Uniform">\n'.format(partn)) + g.write(' <Topology TopologyType="Quadrilateral" NumberOfElements="{}">\n'.format(ncells)) + g.write(' <xi:include href="{}"/>\n'.format(fname)) + g.write(' </Topology>\n') + if ndims == 2: + g.write(' <Geometry GeometryType="X_Y">\n') # co-ordinates in separate arrays + elif ndims == 3: + g.write(' <Geometry GeometryType="X_Y_Z">\n') # co-ordinates in separate arrays + for coord in range(ndims): + g.write(' <DataItem ItemType="HyperSlab"\n') + g.write(' Dimensions="{} 1 1"\n'.format(ncells*nverts)) + g.write(' Type="HyperSlab">\n') + g.write(' <DataItem\n') # start, stride and count of hyperslab region + g.write(' Dimensions="3 3"\n') + g.write(' Format="XML">\n') + g.write(' 0 0 {}\n'.format(coord)) # select co-ordinate + g.write(' 1 1 1\n') # select every vertex in every cell + g.write(' {:<3} {} 1\n'.format(nverts, ncells)) # loop over cells (first) and vertices (second) + g.write(' </DataItem>\n') + g.write(' <DataItem\n') + g.write(' Name="Points" \n') + g.write(' Dimensions="{} {} {}"\n'.format(nverts, ncells, ndims)) + g.write(' Format="HDF">\n') + g.write(' {}:/{}\n'.format(args.mesh, chunk[0])) + g.write(' </DataItem>\n') + g.write(' </DataItem>\n') + + g.write(' </Geometry>\n') + g.write(' <Attribute Name="Partition" Center="Grid">\n') + g.write(' <DataItem\n') + g.write(' Dimensions="1"\n') + g.write(' Format="XML">\n') + g.write(' {}\n'.format(partn)) # tag with partition number + g.write(' </DataItem>\n') + g.write(' </Attribute>\n') + g.write(' </Grid>\n') + + # connectivities file + f = open(fname, 'w') + + f.write('<DataItem DataType="Int"\n') + f.write(' Dimensions="{} {}"\n'.format(ncells, nverts)) + f.write(' Format="XML">\n') + + for i in range (0, ncells): + f.write(' ') + for j in range (1, nverts+1): + f.write(' ' + repr(order[j]*ncells+i).ljust(1)) + f.write('\n') + + f.write('</DataItem>\n') + f.close() + print('connectivities written to ' + fname) + +g.write(' </Domain>\n') +g.write('</Xdmf>\n') +g.close() |