Skip to content

Commit

Permalink
Introduce LiteralSource/FileSource
Browse files Browse the repository at this point in the history
  • Loading branch information
inducer committed Jun 20, 2014
1 parent 8b4a237 commit 78c566c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
45 changes: 36 additions & 9 deletions meshpy/gmsh.py
Expand Up @@ -28,16 +28,34 @@ def error_clean_up(self):
_erase_dir(self.path)


class LiteralSource(object):
def __init__(self, source, extension):
self.source = source
self.extension = extension


class FileSource(object):
def __init__(self, filename):
self.filename = filename


class GmshRunner(object):
def __init__(self, source, dimensions, order=None,
incomplete_elements=None, other_options=[],
extension="geo", gmsh_executable="gmsh"):
if isinstance(source, str):
from warnings import warn
warn("passing a string as 'source' is deprecated--use "
"LiteralSource or FileSource",
DeprecationWarning)

source = LiteralSource(source, extension)

self.source = source
self.dimensions = dimensions
self.order = order
self.incomplete_elements = incomplete_elements
self.other_options = other_options
self.extension = extension
self.gmsh_executable = gmsh_executable

if dimensions not in [1, 2, 3]:
Expand All @@ -48,13 +66,22 @@ def __enter__(self):
temp_dir_mgr = _TempDirManager()
try:
working_dir = temp_dir_mgr.path
from os.path import join
source_file_name = join(working_dir, "temp."+self.extension)
source_file = open(source_file_name, "w")
try:
source_file.write(self.source)
finally:
source_file.close()
from os.path import join, abspath, exists

if isinstance(self.source, LiteralSource):
source_file_name = join(
working_dir, "temp."+self.source.extension)
source_file = open(source_file_name, "w")
try:
source_file.write(self.source.source)
finally:
source_file.close()
elif isinstance(self.source, FileSource):
source_file_name = abspath(self.source.filename)
if not exists(source_file_name):
raise IOError("'%s' does not exist" % source_file_name)
else:
raise RuntimeError("'source' type unrecognized")

output_file_name = join(working_dir, "output.msh")
cmdline = [
Expand Down Expand Up @@ -97,7 +124,7 @@ def __enter__(self):
self.output_file = open(output_file_name, "r")

self.temp_dir_mgr = temp_dir_mgr
return self.output_file
return self
except:
temp_dir_mgr.clean_up()
raise
Expand Down
11 changes: 7 additions & 4 deletions meshpy/gmsh_reader.py
Expand Up @@ -27,6 +27,8 @@
import numpy as np
#import numpy.linalg as la
from pytools import memoize_method, Record
from meshpy.gmsh import LiteralSource, FileSource # noqa


__doc__ = """
.. exception:: GmshFileFormatError
Expand Down Expand Up @@ -54,6 +56,9 @@
Reader
------
.. autoclass:: LiteralSource
.. autoclass:: FileSource
.. autofunction:: read_gmsh
.. autofunction:: generate_gmsh
Expand Down Expand Up @@ -149,9 +154,6 @@ def lexicographic_node_tuples(self):
in this element. Each tuple has a length equal to the dimension
of the element. The tuples constituents are non-negative integers
whose sum is less than or equal to the order of the element.
The order in which these nodes are generated dictates the local
node numbering.
"""
from pytools import \
generate_nonnegative_integer_tuples_summing_to_at_most
Expand Down Expand Up @@ -347,7 +349,8 @@ class GmshMeshReceiverNumPy(GmshMeshReceiverBase):
"""

def __init__(self):
# Use data fields similar to meshpy.triangle.MeshInfo and meshpy.tet.MeshInfo
# Use data fields similar to meshpy.triangle.MeshInfo and
# meshpy.tet.MeshInfo
self.points = None
self.elements = None
self.element_types = None
Expand Down

0 comments on commit 78c566c

Please sign in to comment.