aboutsummaryrefslogtreecommitdiff
path: root/pyfrm2xdmf
blob: a45a74e18b970d497bb098c48d5a6774d473f83b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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()