#!/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('\n') g.write('\n') g.write('\n') g.write(' \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(' \n'.format(partn)) g.write(' \n'.format(ncells)) g.write(' \n'.format(fname)) g.write(' \n') if ndims == 2: g.write(' \n') # co-ordinates in separate arrays elif ndims == 3: g.write(' \n') # co-ordinates in separate arrays for coord in range(ndims): g.write(' \n') g.write(' \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(' \n') g.write(' \n') g.write(' {}:/{}\n'.format(args.mesh, chunk[0])) g.write(' \n') g.write(' \n') g.write(' \n') g.write(' \n') g.write(' \n') g.write(' {}\n'.format(partn)) # tag with partition number g.write(' \n') g.write(' \n') g.write(' \n') # connectivities file f = open(fname, 'w') f.write('\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('\n') f.close() print('connectivities written to ' + fname) g.write(' \n') g.write('\n') g.close()