~reducedmodelling/fluidity/ROM_Non-intrusive-ann

« back to all changes in this revision

Viewing changes to bin/transform_mesh

  • Committer: fangf at ac
  • Date: 2012-11-06 12:21:31 UTC
  • mto: This revision was merged to the branch mainline in revision 3989.
  • Revision ID: fangf@imperial.ac.uk-20121106122131-u2zvt7fxc1r3zeou
updated

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env python
 
2
 
 
3
from optparse import OptionParser
 
4
import sys
 
5
import shutil
 
6
import math
 
7
 
 
8
 
 
9
#####################################################################
 
10
# Script starts here.
 
11
optparser=OptionParser(usage='usage: %prog transformation mesh',
 
12
                       add_help_option=True,
 
13
                       description="""Applies a coordinate transformation to the given mesh.""")
 
14
 
 
15
optparser.set_usage(
 
16
                  "usage: %prog <transformation> <mesh>\n\n"+
 
17
                  "<transformation> is a python expression giving the coordinate transformation.\n"+
 
18
                  "<mesh> is the name of the triangle mesh files. You need a mesh.node, mesh.face and mesh.ele file.\n"+
 
19
                  "\n"+
 
20
                  "Example:\n"
 
21
                  "To rescale the z-dimension by a factor of 1000,\n"
 
22
                  "%prog '(x,y,1000*z)' mesh.\n"
 
23
                     )
 
24
 
 
25
(options, argv) = optparser.parse_args()
 
26
 
 
27
if len(argv)!=2:
 
28
    optparser.print_help()
 
29
    sys.exit(1)
 
30
 
 
31
transformation=argv[0]
 
32
mesh_name=argv[1]
 
33
 
 
34
# make all definitions of the math module available in
 
35
# the transformation expression.
 
36
globals=math.__dict__
 
37
 
 
38
f=file(mesh_name+'.node','r')
 
39
newf=file(mesh_name+'.node.tmp','w')
 
40
header=f.readline()
 
41
nodes=int(header.split(' ')[0])
 
42
dim=int(header.split(' ')[1])
 
43
newf.write(header)
 
44
 
 
45
for line in f:
 
46
  # remove spaces leading and trailing spaces and the end of line character:
 
47
  line=line.lstrip().rstrip()
 
48
  if line.startswith('#'):
 
49
    continue
 
50
  cols=line.split(' ')
 
51
  index=int(cols[0])
 
52
  globals['x']=float(cols[1])
 
53
  globals['y']=float(cols[2])
 
54
  if dim==3:
 
55
    globals['z']=float(cols[3])
 
56
  xyz=eval(transformation, globals)
 
57
  if dim==2:
 
58
    newf.write(`index`+' '+`xyz[0]`+' '+`xyz[1]`+'\n')
 
59
  else:
 
60
    newf.write(`index`+' '+`xyz[0]`+' '+`xyz[1]`+' '+`xyz[2]`+'\n')
 
61
 
 
62
newf.close()
 
63
f.close()
 
64
 
 
65
shutil.move(mesh_name+'.node', mesh_name+'.node.bak')
 
66
shutil.move(mesh_name+'.node.tmp', mesh_name+'.node')
 
 
b'\\ No newline at end of file'