1 """
2 Application class that implements pyFoamChangeBoundaryName.py
3
4 Author:
5 Martin Beaudoin, Hydro-Quebec, 2010.
6
7 """
8
9 from PyFoamApplication import PyFoamApplication
10
11 from os import path
12 import sys
13 from optparse import OptionGroup
14
15 from PyFoam.RunDictionary.ParsedParameterFile import ParsedParameterFile
16 from PyFoam.RunDictionary.TimeDirectory import TimeDirectory
17
20 description="""\
21 Changes the name of a boundary in the boundary-file. Also if a
22 time-step is specified
23 """
24 PyFoamApplication.__init__(self,args=args,
25 description=description,
26 usage="%prog <caseDirectory> <boundaryName> <new name>",
27 changeVersion=False,
28 nr=3,
29 interspersed=True)
30
32 change=OptionGroup(self.parser,
33 "Change",
34 "Change specific options")
35 self.parser.add_option_group(change)
36 change.add_option("--test",
37 action="store_true",
38 default=False,
39 dest="test",
40 help="Only print the new boundary file")
41 change.add_option("--time-step",
42 action="store",
43 default=None,
44 dest="timestep",
45 help="If specified all the field-files in that directory are updated")
46
48 fName=self.parser.getArgs()[0]
49 bName=self.parser.getArgs()[1]
50 nName=self.parser.getArgs()[2]
51
52 boundary=ParsedParameterFile(path.join(".",fName,"constant","polyMesh","boundary"),debug=False,boundaryDict=True)
53
54 bnd=boundary.content
55
56 if type(bnd)!=list:
57 self.error("Problem with boundary file (not a list)")
58
59 found=False
60
61 for val in bnd:
62 if val==bName:
63 found=True
64 elif found:
65 bnd[bnd.index(bName)]=nName
66 break
67
68 if not found:
69 self.error("Boundary",bName,"not found in",bnd[::2])
70
71 if self.opts.test:
72 print boundary
73 else:
74 boundary.writeFile()
75 self.addToCaseLog(fName)
76
77 if self.opts.timestep:
78 print "Updating the files of timestep",self.opts.timestep
79 td=TimeDirectory(fName,self.opts.timestep,
80 yieldParsedFiles=True)
81
82 for f in td:
83 print "Updating",f.name
84 f["boundaryField"][nName]=f["boundaryField"][bName]
85 del f["boundaryField"][bName]
86 f.writeFile()
87