def writeCase(self): """ writeCase() will collect case settings, and finally build a runnable case. """ cfdMessage("Start to write case to folder {}\n".format( self.working_dir)) if not os.path.exists(self.working_dir): raise IOError("Path " + self.solver_obj.working_dir + " does not exist.") # Perform initialisation here rather than __init__ in case of path changes self.case_folder = os.path.join(self.working_dir, self.solver_obj.InputCaseName) self.case_folder = os.path.expanduser(os.path.abspath( self.case_folder)) self.mesh_file_name = os.path.join(self.case_folder, self.solver_obj.InputCaseName, u".unv") self.template_path = os.path.join(CfdTools.get_module_path(), "data", "defaults") solverSettingsDict = CfdTools.getSolverSettings(self.solver_obj) # Collect settings into single dictionary if not self.mesh_obj: raise RuntimeError("No mesh object found in analysis") phys_settings = dict( zip(self.physics_model.PropertiesList, (getattr(self.physics_model, prop) for prop in self.physics_model.PropertiesList))) if 'gx' in phys_settings: phys_settings['gx'] = Units.Quantity( phys_settings['gx']).getValueAs('m/s^2') phys_settings['gy'] = Units.Quantity( phys_settings['gy']).getValueAs('m/s^2') phys_settings['gz'] = Units.Quantity( phys_settings['gz']).getValueAs('m/s^2') self.settings = { 'physics': phys_settings, 'fluidProperties': [], # Order is important, so use a list 'initialValues': self.initial_conditions, 'boundaries': dict((b.Label, b.BoundarySettings) for b in self.bc_group), 'bafflesPresent': self.bafflesPresent(), 'porousZones': {}, 'porousZonesPresent': False, 'initialisationZones': { o.Label: o.initialisationZoneProperties for o in self.initialisationZone_objs }, 'initialisationZonesPresent': len(self.initialisationZone_objs) > 0, 'zones': { o.Label: { 'PartNameList': tuple(o.partNameList) } for o in self.zone_objs }, 'zonesPresent': len(self.zone_objs) > 0, 'meshType': self.mesh_obj.Proxy.Type, 'meshDimension': self.mesh_obj.ElementDimension, 'solver': solverSettingsDict, 'system': {}, 'runChangeDictionary': False } self.processSystemSettings() self.processSolverSettings() self.processFluidProperties() self.processBoundaryConditions() self.processInitialConditions() self.clearCase() self.exportZoneStlSurfaces() if self.porousZone_objs: self.processPorousZoneProperties() self.processInitialisationZoneProperties() self.settings['createPatchesFromSnappyBaffles'] = False cfdMessage("Matching boundary conditions ...\n") self.setupPatchNames() TemplateBuilder.TemplateBuilder(self.case_folder, self.template_path, self.settings) self.writeMesh() # Update Allrun permission - will fail silently on Windows fname = os.path.join(self.case_folder, "Allrun") import stat s = os.stat(fname) os.chmod(fname, s.st_mode | stat.S_IEXEC) # Move mesh files, after being edited, to polyMesh.org CfdTools.movePolyMesh(self.case_folder) cfdMessage("Successfully wrote {} case to folder {}\n".format( self.solver_obj.SolverName, self.working_dir)) return True
def write_case(self, updating=False): """ Write_case() will collect case setings, and finally build a runnable case. """ print("Start to write case to folder {}\n".format( self.solver_obj.WorkingDir)) _cwd = os.curdir if not os.path.exists(self.solver_obj.WorkingDir): raise IOError("Path " + self.solver_obj.WorkingDir + " does not exist.") os.chdir( self.solver_obj.WorkingDir ) # pyFoam can not write to cwd if FreeCAD is started NOT from terminal try: # Make sure we restore cwd after exception here # Perform initialisation here rather than __init__ in case of path changes self.case_folder = os.path.join(self.solver_obj.WorkingDir, self.solver_obj.InputCaseName) self.case_folder = os.path.expanduser( os.path.abspath(self.case_folder)) self.mesh_file_name = os.path.join(self.case_folder, self.solver_obj.InputCaseName, u".unv") self.template_path = os.path.join(CfdTools.get_module_path(), "data", "defaults") solverSettingsDict = CfdTools.getSolverSettings(self.solver_obj) # Collect settings into single dictionary if not self.mesh_obj: raise RuntimeError("No mesh object found in analysis") self.settings = { 'physics': self.physics_model, 'fluidProperties': [], # Order is important, so use a list 'initialValues': self.initial_conditions, 'boundaries': dict((b.Label, b.BoundarySettings) for b in self.bc_group), 'bafflesPresent': self.bafflesPresent(), 'porousZones': {}, 'porousZonesPresent': False, 'initialisationZones': { o.Label: o.initialisationZoneProperties for o in self.initialisationZone_objs }, 'initialisationZonesPresent': len(self.initialisationZone_objs) > 0, 'zones': { o.Label: { 'PartNameList': tuple(o.partNameList) } for o in self.zone_objs }, 'zonesPresent': len(self.zone_objs) > 0, 'meshType': self.mesh_obj.Proxy.Type, 'solver': solverSettingsDict, 'system': {} } self.processSystemSettings() self.processSolverSettings() self.processFluidProperties() self.processBoundaryConditions() self.processInitialConditions() self.clearCase() self.exportZoneStlSurfaces() if self.porousZone_objs: self.processPorousZoneProperties() self.processInitialisationZoneProperties() self.settings['createPatchesFromSnappyBaffles'] = False if self.mesh_obj.Proxy.Type == "CfdMeshCart": # Cut-cell Cartesian self.setupPatchNames() TemplateBuilder.TemplateBuilder(self.case_folder, self.template_path, self.settings) self.writeMesh() # Update Allrun permission - will fail silently on Windows fname = os.path.join(self.case_folder, "Allrun") import stat s = os.stat(fname) os.chmod(fname, s.st_mode | stat.S_IEXEC) # Move mesh files, after being edited, to polyMesh.org CfdTools.movePolyMesh(self.case_folder) except: raise finally: os.chdir(_cwd) # Restore working dir print("Successfully wrote {} case to folder {}\n".format( self.solver_obj.SolverName, self.solver_obj.WorkingDir)) return True