Package PyFoam :: Package Applications :: Module BlockMeshRewrite
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.Applications.BlockMeshRewrite

  1  """ 
  2  Application-class that implements pyFoamBlockMeshRewrite.py 
  3  """ 
  4  from optparse import OptionGroup 
  5   
  6  from .PyFoamApplication import PyFoamApplication 
  7  from PyFoam.Basics.RestructuredTextHelper import RestructuredTextHelper 
  8  from PyFoam.Basics.FoamOptionParser import Subcommand 
  9   
 10  from PyFoam.RunDictionary.BlockMesh import BlockMesh 
 11   
 12  from os import path 
 13  import sys,re 
 14   
 15  from PyFoam.ThirdParty.six import print_ 
 16   
17 -class BlockMeshRewrite(PyFoamApplication):
18 - def __init__(self, 19 args=None, 20 **kwargs):
21 description="""\ 22 This utility manipulates blockMeshDict. Manipulation happens on a textual basis . 23 This means that the utility assumes that the blockMeshDict is sensibly formated 24 (this means for instance that there is only one block/vertex per line and they only 25 go over one line 26 """ 27 PyFoamApplication.__init__(self, 28 args=args, 29 description=description, 30 usage="%prog COMMAND <blockMeshDict>", 31 changeVersion=False, 32 subcommands=True, 33 **kwargs)
34
35 - def addOptions(self):
36 # Building the subcommands 37 refineCmd=Subcommand(name='refine', 38 help="Refine the blocks in the blockMesh by multiplying them with a fixed factor", 39 aliases=("resolution",), 40 nr=0, 41 exactNr=False) 42 self.parser.addSubcommand(refineCmd) 43 refineGrp=OptionGroup(refineCmd.parser, 44 "Refinement", 45 "Parameters for refining the mesh") 46 refineCmd.parser.add_option_group(refineGrp) 47 refineGrp.add_option("--factor", 48 action="store", 49 dest="factor", 50 default=None, 51 help="Factor to scale the mesh with. Either a single scalar to multiply all directions with or three comma-separated scalars (one for each direction). This option is required") 52 refineGrp.add_option("--offsets", 53 action="store", 54 dest="offsets", 55 default=None, 56 help="Offset to add to the scaling. Three comma-separated scalars (one for each direction). Not required") 57 58 numberCmd=Subcommand(name='number', 59 help="Add comments with the vertex numbers to the mesh-file", 60 aliases=("count",), 61 nr=0, 62 exactNr=False) 63 self.parser.addSubcommand(numberCmd) 64 numberGrp=OptionGroup(numberCmd.parser, 65 "Numbering", 66 "Parameters for the numbering of the vertices") 67 numberCmd.parser.add_option_group(numberGrp) 68 numberGrp.add_option("--prefix", 69 action="store", 70 dest="numberPrefix", 71 default="Vertex Nr.", 72 help="Text to be added before the vertex number. Default: %default") 73 74 stripCmd=Subcommand(name='stripNumber', 75 help="Remove comments after the vertex number", 76 aliases=("strip",), 77 nr=0, 78 exactNr=False) 79 self.parser.addSubcommand(stripCmd) 80 81 mergeCmd=Subcommand(name='mergeVertices', 82 help="Merge", 83 aliases=("add",), 84 nr=0, 85 exactNr=False) 86 self.parser.addSubcommand(mergeCmd) 87 mergeGrp=OptionGroup(mergeCmd.parser, 88 "Merging", 89 "Parameters for the merging of vertices") 90 mergeCmd.parser.add_option_group(mergeGrp) 91 mergeGrp.add_option("--other-mesh", 92 action="store", 93 dest="otherMesh", 94 default=None, 95 help="The blockMeshDict from which vertexes are to be added") 96 97 renumberCmd=Subcommand(name='renumberVertices', 98 help="Renumber vertices so that they match the vertices of another blockMeshDict", 99 aliases=("add",), 100 nr=0, 101 exactNr=False) 102 self.parser.addSubcommand(renumberCmd) 103 renumberGrp=OptionGroup(renumberCmd.parser, 104 "Merging", 105 "Parameters for the merging of vertices") 106 renumberCmd.parser.add_option_group(renumberGrp) 107 renumberGrp.add_option("--other-mesh", 108 action="store", 109 dest="otherMesh", 110 default=None, 111 help="The blockMeshDict from which numbers are to be taken and renumbered") 112 113 normalizeCmd=Subcommand(name='normalizePatches', 114 help="Normalize patches by rotating the vertices in the patch so that the smalles number comes first", 115 aliases=("rotate",), 116 nr=0, 117 exactNr=False) 118 self.parser.addSubcommand(normalizeCmd) 119 120 for cmd in [refineCmd,numberCmd,stripCmd,mergeCmd,renumberCmd, 121 normalizeCmd]: 122 inputGrp=OptionGroup(cmd.parser, 123 "blockMeshDict In", 124 "Where the blockMeshDict is read from") 125 cmd.parser.add_option_group(inputGrp) 126 127 inputGrp.add_option("--from-stdin", 128 action="store_true", 129 dest="stdin", 130 default=False, 131 help="Instead of reading from file read from stdin (used for piping into other commands)") 132 133 outputGrp=OptionGroup(cmd.parser, 134 "blockMeshDict Out", 135 "Where the processed blockMeshDict is written to") 136 cmd.parser.add_option_group(outputGrp) 137 outputGrp.add_option("--to-stdout", 138 action="store_true", 139 dest="stdout", 140 default=False, 141 help="Instead of writing to file write to stdout (used for piping into other commands)") 142 outputGrp.add_option("--overwrite", 143 action="store_true", 144 dest="overwrite", 145 default=False, 146 help="Overwrite the original file") 147 outputGrp.add_option("--outfile", 148 action="store", 149 dest="outfile", 150 default=None, 151 help="File to write the result to")
152
153 - def run(self):
154 if self.opts.stdin: 155 if len(self.parser.getArgs())>0: 156 self.error("If --from-stdin specified no arguments are allowed but we have",self.parser.getArgs()) 157 else: 158 if len(self.parser.getArgs())!=1: 159 self.error("Only one blockMeshDict can be specified") 160 161 outOptNr=(1 if self.opts.stdout else 0)+(1 if self.opts.overwrite else 0)+(1 if self.opts.outfile else 0) 162 163 if outOptNr!=1: 164 self.error("Specify one (and only one) of the options '--to-stdout', '--overwrite', '--outfile'") 165 if self.opts.stdin and self.opts.overwrite: 166 self.error("'--from-stdin' and '--overwrite' are incompatible") 167 168 if self.opts.stdin: 169 srcMesh=BlockMesh(self.stdin) 170 else: 171 srcMesh=BlockMesh(self.parser.getArgs()[0]) 172 173 if self.cmdname=="refine": 174 if self.opts.factor==None: 175 self.error("Unspecified option '--factor'") 176 try: 177 factor=float(self.opts.factor) 178 except ValueError: 179 factor=eval("("+self.opts.factor+")") 180 if len(factor)!=3: 181 self.error("--factor must either be a scalar or 3 comma-separated values") 182 offset=(0,0,0) 183 if self.opts.offsets: 184 offset=eval("("+self.opts.offsets+")") 185 srcMesh.refineMesh(factor,offset,getContent=True,addOld=False) 186 elif self.cmdname=="number": 187 srcMesh.numberVertices(self.opts.numberPrefix) 188 elif self.cmdname=="stripNumber": 189 srcMesh.stripVertexNumber() 190 elif self.cmdname=="mergeVertices": 191 if self.opts.otherMesh==None: 192 self.error("'--other-mesh' has to be specified for this") 193 srcMesh.mergeVertices(self.opts.otherMesh) 194 elif self.cmdname=="renumberVertices": 195 if self.opts.otherMesh==None: 196 self.error("'--other-mesh' has to be specified for this") 197 srcMesh.renumberVertices(self.opts.otherMesh) 198 elif self.cmdname=="normalizePatches": 199 srcMesh.normalizePatches() 200 else: 201 self.error("Unimplemented sub-command",self.cmdname) 202 203 if self.opts.stdout: 204 sys.stdout.write(srcMesh.content) 205 elif self.opts.overwrite: 206 srcMesh.writeFile() 207 else: 208 srcMesh.writeFileAs(self.opts.outfile)
209 210 # Should work with Python3 and Python2 211