Package meshpy :: Module gmsh
[hide private]
[frames] | no frames]

Source Code for Module meshpy.gmsh

  1  import logging 
  2  logger = logging.getLogger(__name__) 
  3   
  4   
5 -class GmshError(RuntimeError):
6 pass
7 8 9 # {{{ tools 10
11 -def _erase_dir(dir):
12 from os import listdir, unlink, rmdir 13 from os.path import join 14 for name in listdir(dir): 15 unlink(join(dir, name)) 16 rmdir(dir)
17 18
19 -class _TempDirManager(object):
20 - def __init__(self):
21 from tempfile import mkdtemp 22 self.path = mkdtemp()
23
24 - def sub(self, n):
25 from os.path import join 26 return join(self.path, n)
27
28 - def clean_up(self):
29 _erase_dir(self.path)
30
31 - def error_clean_up(self):
32 _erase_dir(self.path)
33 34
35 -class LiteralSource(object):
36 """ 37 .. versionadded:: 2014.1 38 """
39 - def __init__(self, source, extension):
40 self.source = source 41 self.extension = extension
42 43
44 -class FileSource(object):
45 """ 46 .. versionadded:: 2014.1 47 """
48 - def __init__(self, filename):
49 self.filename = filename
50 51
52 -class GmshRunner(object):
53 - def __init__(self, source, dimensions, order=None, 54 incomplete_elements=None, other_options=[], 55 extension="geo", gmsh_executable="gmsh"):
56 if isinstance(source, str): 57 from warnings import warn 58 warn("passing a string as 'source' is deprecated--use " 59 "LiteralSource or FileSource", 60 DeprecationWarning) 61 62 source = LiteralSource(source, extension) 63 64 self.source = source 65 self.dimensions = dimensions 66 self.order = order 67 self.incomplete_elements = incomplete_elements 68 self.other_options = other_options 69 self.gmsh_executable = gmsh_executable 70 71 if dimensions not in [1, 2, 3]: 72 raise RuntimeError("dimensions must be one of 1,2,3")
73
74 - def __enter__(self):
75 self.temp_dir_mgr = None 76 temp_dir_mgr = _TempDirManager() 77 try: 78 working_dir = temp_dir_mgr.path 79 from os.path import join, abspath, exists 80 81 if isinstance(self.source, LiteralSource): 82 source_file_name = join( 83 working_dir, "temp."+self.source.extension) 84 source_file = open(source_file_name, "w") 85 try: 86 source_file.write(self.source.source) 87 finally: 88 source_file.close() 89 elif isinstance(self.source, FileSource): 90 source_file_name = abspath(self.source.filename) 91 if not exists(source_file_name): 92 raise IOError("'%s' does not exist" % source_file_name) 93 else: 94 raise RuntimeError("'source' type unrecognized") 95 96 output_file_name = join(working_dir, "output.msh") 97 cmdline = [ 98 self.gmsh_executable, 99 "-%d" % self.dimensions, 100 "-o", output_file_name, 101 "-nopopup"] 102 103 if self.order is not None: 104 cmdline.extend(["-order", str(self.order)]) 105 106 if self.incomplete_elements is not None: 107 cmdline.extend(["-string", 108 "Mesh.SecondOrderIncomplete = %d;" 109 % int(self.incomplete_elements)]) 110 111 cmdline.extend(self.other_options) 112 cmdline.append(source_file_name) 113 114 logger.info("invoking gmsh: '%s'" % " ".join(cmdline)) 115 from pytools.prefork import call_capture_output 116 retcode, stdout, stderr = call_capture_output( 117 cmdline, working_dir) 118 logger.info("return from gmsh") 119 120 if stderr and "error" in stderr.lower(): 121 msg = "gmsh execution failed with message:\n\n" 122 if stdout: 123 msg += stdout+"\n" 124 msg += stderr+"\n" 125 raise GmshError(msg) 126 127 if stderr: 128 from warnings import warn 129 130 msg = "gmsh issued the following messages:\n\n" 131 if stdout: 132 msg += stdout+"\n" 133 msg += stderr+"\n" 134 warn(msg) 135 136 self.output_file = open(output_file_name, "r") 137 138 self.temp_dir_mgr = temp_dir_mgr 139 return self 140 except: 141 temp_dir_mgr.clean_up() 142 raise
143
144 - def __exit__(self, type, value, traceback):
145 self.output_file.close() 146 if self.temp_dir_mgr is not None: 147 self.temp_dir_mgr.clean_up()
148