argiope/mesh.py
def read_msh(path):
  """
  Reads a GMSH MSH file and returns a :class:`Mesh` instance. 
  
  :arg path: path to MSH file.
  :type path: str
  
  """
  elementMap = { 15:"point1",
                 1:"line2",
                 2:"tri3",
                 3:"quad4",
                 4:"tetra4",
                 5:"hexa8",
                 6:"prism6",
                 7:"pyra4",
                 
               }
  lines = np.array(open(path, "r").readlines())
  locs = {}
  nl = len(lines)
  for i in range(nl):
    line = lines[i].lower().strip()
    if line.startswith("$"):
      if line.startswith("$end"):
        locs[env].append(i)
      else:
        env = line[1:] 
        locs[env] = [i]
  nodes = pd.read_csv(
          io.StringIO("\n".join(
                   lines[locs["nodes"][0]+2:locs["nodes"][1]])), 
                   sep = " ",
                   names = ["labels", "x", "y", "z"])
  
  elements = {"labels":[], "etype":[], "conn": [], "tags":[], "sets":{}}
  
  for line in lines[locs["elements"][0]+2:locs["elements"][1]]:
    d = np.array([int(w) for w in line.split()])
    elements["labels"].append( d[0] )
    elements["etype"].append(elementMap[d[1]] )
    elements["tags"].append( d[3: 3+d[2]] ) 
    elements["conn"].append(d[3+d[2]:])
  elements["labels"] = np.array(elements["labels"])
  physicalNames = {}
  for line in lines[locs["physicalnames"][0]+2:locs["physicalnames"][1]]:
    w = line.split()
    physicalNames[int(w[1])] = w[2].replace('"', '')
  sets = {}
  tags = np.array([t[0] for t in elements["tags"]])
  for k in physicalNames.keys():
    sets[physicalNames[k]] = np.array([t == k for t in tags])     
  """
  for tag, values in sets.items():
    elements["sets"][tag] = elements["labels"][values]     
  """
  elements["sets"] = sets
  return Mesh(nlabels = nodes["labels"], 
              coords = np.array(nodes[["x", "y", "z"]]),
              elabels = elements["labels"],
              conn = elements["conn"],
              types = elements["etype"],
              esets = elements["sets"])
def read_msh(path):
  """
  Reads a GMSH MSH file and returns a :class:`Mesh` instance. 
  
  :arg path: path to MSH file.
  :type path: str
  
  """
  elementMap = { 15:"point1",
                 1:"line2",
                 2:"tri3",
                 3:"quad4",
                 4:"tetra4",
                 5:"hexa8",
                 6:"prism6",
                 7:"pyra4",
                 
               }
  lines = np.array(open(path, "r").readlines())
  locs = {}
  nl = len(lines)
  for i in range(nl):
    line = lines[i].lower().strip()
    if line.startswith("$"):
      if line.startswith("$end"):
        locs[env].append(i)
      else:
        env = line[1:] 
        locs[env] = [i]
  nodes = pd.read_csv(
          io.StringIO("\n".join(
                   lines[locs["nodes"][0]+2:locs["nodes"][1]])), 
                   sep = " ",
                   names = ["labels", "x", "y", "z"])
  
  elements = {"labels":[], "etype":[], "conn": [], "tags":[], "sets":{}}
  
  for line in lines[locs["elements"][0]+2:locs["elements"][1]]:
    d = np.array([int(w) for w in line.split()])
    elements["labels"].append( d[0] )
    elements["etype"].append(elementMap[d[1]] )
    elements["tags"].append( d[3: 3+d[2]] ) 
    elements["conn"].append(d[3+d[2]:])
  elements["labels"] = np.array(elements["labels"])
  physicalNames = {}
  for line in lines[locs["physicalnames"][0]+2:locs["physicalnames"][1]]:
    w = line.split()
    physicalNames[int(w[1])] = w[2].replace('"', '')
  sets = {}
  tags = np.array([t[0] for t in elements["tags"]])
  for k in physicalNames.keys():
    sets[physicalNames[k]] = np.array([t == k for t in tags])     
  """
  for tag, values in sets.items():
    elements["sets"][tag] = elements["labels"][values]     
  """
  elements["sets"] = sets
  return Mesh(nlabels = nodes["labels"], 
              coords = np.array(nodes[["x", "y", "z"]]),
              elabels = elements["labels"],
              conn = elements["conn"],
              types = elements["etype"],
              esets = elements["sets"])
Reads a GMSH MSH file and returns a :class:`Mesh` instance. 
  
  :arg path: path to MSH file.
  :type path: str