Package PyFoam :: Package Site :: Module ConvertMixingPlaneBoundaryToNewSyntax
[hide private]
[frames] | no frames]

Source Code for Module PyFoam.Site.ConvertMixingPlaneBoundaryToNewSyntax

  1  """ 
  2  Application-class that implements pyFoamConvertMixingPlaneToNewSyntax.py 
  3   
  4  Adjust the mixingPlane interface definition in the boundary 
  5  file to the latest supported syntax. 
  6   
  7  Author: 
  8    Martin Beaudoin, Hydro-Quebec, 2012.  All rights reserved 
  9   
 10  """ 
 11   
 12  from PyFoam.Applications.PyFoamApplication import PyFoamApplication 
 13  from PyFoam.RunDictionary.ParsedParameterFile import ParsedParameterFile 
 14  from PyFoam.ThirdParty.six import print_ 
 15  from os import path 
 16  import sys 
 17   
 18  # ------> Start of Python code snippet copied from an external source 
 19  # 
 20  # Author : Brian Beck 
 21  # http://code.activestate.com/recipes/410692-readable-switch-construction-without-lambdas-or-di/ 
 22  # 
 23  # License for this Python code snippet: 
 24  # 
 25  # This code that was deposited before July 15, 2008 on aspn.activestate.com. 
 26  # It is governed by the Python license (http://www.python.org/psf/license/) 
 27  # in accordance with the Python Cookbook agreement. 
 28  # 
 29  # Description: 
 30  # Python's lack of a 'switch' statement has garnered much discussion and even 
 31  # a PEP. The most popular substitute uses dictionaries to map cases to 
 32  # functions, which requires lots of defs or lambdas. While the approach shown 
 33  # here may be O(n) for cases, it aims to duplicate C's original 'switch' 
 34  # functionality and structure with reasonable accuracy. 
 35  # 
 36  # This class provides the functionality we want. You only need to look at 
 37  # this if you want to know how this works. It only needs to be defined 
 38  # once, no need to muck around with its internals. 
 39  # 
 40   
41 -class switch(object):
42 - def __init__(self, value):
43 self.value = value 44 self.fall = False
45
46 - def __iter__(self):
47 """Return the match method once, then stop""" 48 yield self.match 49 raise StopIteration
50
51 - def match(self, *args):
52 """Indicate whether or not to enter a case suite""" 53 if self.fall or not args: 54 return True 55 elif self.value in args: # changed for v1.5, see below 56 self.fall = True 57 return True 58 else: 59 return False
60 61 # ------> End of Python code snippet copied from an external source 62 63 #################################################################### 64 # 65 # The rest of this source code was written by me. 66 # Martin Beaudoin, June 2012 67
68 -class ConvertMixingPlaneBoundaryToNewSyntax(PyFoamApplication):
69 - def __init__(self,args=None):
70 description=""" 71 Change MixingPlane boundary condition parameters 72 """ 73 PyFoamApplication.__init__(self, 74 args=args, 75 description=description, 76 usage="%prog <caseDirectory>", 77 interspersed=True, 78 changeVersion=False, 79 nr=1)
80
81 - def addOptions(self):
82 83 self.parser.add_option("--test", 84 action="store_true", 85 default=False, 86 dest="test", 87 help="Only print the new boundary file")
88
89 - def run(self):
90 fName=self.parser.getArgs()[0] 91 92 boundary=ParsedParameterFile(path.join(".",fName,"constant","polyMesh","boundary"),debug=False,boundaryDict=True) 93 94 bnd=boundary.content 95 96 if type(bnd)!=list: 97 print_("Problem with boundary file (not a list)") 98 sys.exit(-1) 99 100 found=False 101 102 for index in range(0, len(bnd), 2): 103 104 indexDefPatch=index+1 105 106 oldAssembly="" 107 oldOrientation="" 108 109 if bnd[indexDefPatch]["type"]=="mixingPlane": 110 if bnd[indexDefPatch].has_key("assembly"): 111 print_(" Replacing the parameter 'assembly' for patch", bnd[index]) 112 oldAssembly=bnd[indexDefPatch]["assembly"] 113 del bnd[indexDefPatch]["assembly"] 114 115 if bnd[indexDefPatch].has_key("orientation"): 116 print_(" Replacing the parameter 'orientation' for patch", bnd[index]) 117 oldOrientation=bnd[indexDefPatch]["orientation"] 118 del bnd[indexDefPatch]["orientation"] 119 120 if bnd[indexDefPatch].has_key("ribbonPatch")==False: 121 bnd[indexDefPatch]["ribbonPatch"]={} 122 123 if bnd[indexDefPatch].has_key("zone")==False: 124 bnd[indexDefPatch]["zone"]=bnd[index] + "Zone" 125 126 if oldAssembly != "": 127 # Converting "assembly" to ribbonPatch/discretisation 128 for case in switch(oldAssembly): 129 if case('master'): 130 bnd[indexDefPatch]["ribbonPatch"]["discretisation"]="masterPatch" 131 break 132 if case('slave'): 133 bnd[indexDefPatch]["ribbonPatch"]["discretisation"]="slavePatch" 134 break 135 if case('both'): 136 bnd[indexDefPatch]["ribbonPatch"]["discretisation"]="bothPatches" 137 break 138 if case('userdefined'): 139 bnd[indexDefPatch]["ribbonPatch"]["discretisation"]="userDefined" 140 break 141 if case(): # default 142 print_("Unsupported assembly type: ", oldAssembly) 143 144 if oldOrientation != "": 145 # Converting "orientation" to ribbonPatch/ribbonPatchSweepAxis and 146 # ribbonPatch/ribbonPatchStackAxis 147 for case in switch(oldOrientation): 148 149 if case('dirX_spanY'): 150 bnd[indexDefPatch]["ribbonPatch"]["stackAxis"]="X" 151 bnd[indexDefPatch]["ribbonPatch"]["sweepAxis"]="Y" 152 break 153 if case('dirX_spanZ'): 154 bnd[indexDefPatch]["ribbonPatch"]["stackAxis"]="X" 155 bnd[indexDefPatch]["ribbonPatch"]["sweepAxis"]="Z" 156 break 157 if case('dirY_spanX'): 158 bnd[indexDefPatch]["ribbonPatch"]["stackAxis"]="Y" 159 bnd[indexDefPatch]["ribbonPatch"]["sweepAxis"]="X" 160 break 161 if case('dirY_spanZ'): 162 bnd[indexDefPatch]["ribbonPatch"]["stackAxis"]="Y" 163 bnd[indexDefPatch]["ribbonPatch"]["sweepAxis"]="Z" 164 break 165 if case('dirZ_spanX'): 166 bnd[indexDefPatch]["ribbonPatch"]["stackAxis"]="Z" 167 bnd[indexDefPatch]["ribbonPatch"]["sweepAxis"]="X" 168 break 169 if case('dirZ_spanY'): 170 bnd[indexDefPatch]["ribbonPatch"]["stackAxis"]="Z" 171 bnd[indexDefPatch]["ribbonPatch"]["sweepAxis"]="Y" 172 break 173 if case('dirR_spanTheta'): 174 bnd[indexDefPatch]["ribbonPatch"]["stackAxis"]="R" 175 bnd[indexDefPatch]["ribbonPatch"]["sweepAxis"]="Theta" 176 break 177 if case('dirR_spanZ'): 178 bnd[indexDefPatch]["ribbonPatch"]["stackAxis"]="R" 179 bnd[indexDefPatch]["ribbonPatch"]["sweepAxis"]="Z" 180 break 181 if case('dirTheta_spanZ'): 182 bnd[indexDefPatch]["ribbonPatch"]["stackAxis"]="Theta" 183 bnd[indexDefPatch]["ribbonPatch"]["sweepAxis"]="Z" 184 break 185 if case('dirTheta_spanR'): 186 bnd[indexDefPatch]["ribbonPatch"]["stackAxis"]="Theta" 187 bnd[indexDefPatch]["ribbonPatch"]["sweepAxis"]="R" 188 break 189 if case('dirZ_spanTheta'): 190 bnd[indexDefPatch]["ribbonPatch"]["stackAxis"]="Z" 191 bnd[indexDefPatch]["ribbonPatch"]["sweepAxis"]="Theta" 192 break 193 if case('dirZ_spanR'): 194 bnd[indexDefPatch]["ribbonPatch"]["stackAxis"]="Z" 195 bnd[indexDefPatch]["ribbonPatch"]["sweepAxis"]="R" 196 break 197 if case(): # default 198 print_("Unsupported orientation type: ", oldOrientation) 199 200 if self.parser.getOptions().test: 201 print_(boundary) 202 else: 203 boundary.writeFile()
204