argiope/mesh.py
def write_inp(mesh, path = None, maxwidth = 40, sections = "solid"):
  """
  Exports the mesh to the INP format.
  """
  def set_to_inp(sets, keyword):
    ss = ""
    for sk in sets.keys():
      labels = sets[sk].loc[sets[sk]].index.values
      labels = list(labels)
      labels.sort()
      if len(labels)!= 0:
        ss += "*{0}, {0}={1}\n".format(keyword, sk)
        ss += argiope.utils.list_to_string(labels) + "\n"
    return ss.strip()           

  # DATA
  mesh = mesh.copy()

  # NODES
  nodes_output = (mesh.nodes.coords.to_csv(header = False).split())
  nodes_output = ("\n".join(["  " + s.replace(",", ", ") for s in nodes_output]))
  
  # NODE SETS
  if "sets" in mesh.nodes.columns.levels[0]: 
    nsets = set_to_inp(mesh.nodes.sets, "NSET")
  else:
    nsets = "**"
  
  # SURFACES 
  surf_output = []
  if "surfaces" in mesh.elements.keys():
    sk = mesh.elements.surfaces.keys()
    for sindex in  np.unique(sk.labels[0]):
      slabel = sk.levels[0][sindex]
      surface = mesh.elements.surfaces[slabel]
      if surface.values.sum() != 0:
        mesh.surface_to_element_sets(slabel)
        surf_output.append( "*SURFACE, TYPE=ELEMENT, NAME={0}".format(slabel))
        for findex in surface.keys():
          if surface[findex].sum() != 0:
            surf_output.append("  _SURF_{0}_FACE{1}, S{1}".format(slabel, 
                                                                  findex[1:])) 
  else:
    surf_output.append("**")
  
  # ELEMENTS
  elements_output = ""
  for etype, group in mesh.elements.groupby((("type", "solver", ""),)):
    els = group.conn.replace(0, np.nan).to_csv(header = False, 
                                               float_format='%.0f').split()
    elements_output += "*ELEMENT, TYPE={0}\n".format(etype)
    elements_output += ("\n".join(["  " + s.strip().strip(",").
                             replace(",", ", ") for s in els]))
    elements_output += "\n"
  elements_output = elements_output.strip() 
  el_sets = {} 

  # MATERIALS
  section_output = ""
  for material, group in mesh.elements.groupby("materials"):
    slabel = "_MAT_{0}".format(material)
    section_output += "*ELSET, ELSET=_MAT_{0}\n{1}\n".format(
                 material,
                 argiope.utils.list_to_string(group.index.values))
    
    
    #mesh.elements[("sets", slabel, "")] = False
    #mesh.elements.loc[group.index, ("sets", slabel, "")] = True
    if sections == "solid":
      section_output += "*SOLID SECTION, ELSET=_MAT_{0}, MATERIAL={0}\n".format(
       material)

  # ELEMENTS SETS
  if "sets" in mesh.elements.columns.levels[0]: 
    esets = set_to_inp(mesh.elements.sets.swaplevel(1,0, axis = 1)[""],"ELSET")
  else:
    esets = "**"
  """
  ek = mesh.elements.sets.keys()
  for esindex in  np.unique(ek.labels[0]):
    eslabel = ek.levels[0][esindex]
    eset = mesh.elements.sets[slabel]
  """
     
  # PATTERN
  pattern = Template(open(MODPATH + "/templates/mesh/inp.inp").read())
  pattern = pattern.substitute(
    NODES     = nodes_output,
    NODE_SETS = nsets,
    ELEMENTS  = elements_output,
    ELEMENT_SETS = esets,
    ELEMENT_SURFACES = "\n".join(surf_output),
    SECTIONS = section_output.strip())
  pattern = pattern.strip()
  if path == None:            
    return pattern
  else:
    open(path, "w").write(pattern)
def write_inp(mesh, path = None, maxwidth = 40, sections = "solid"):
  """
  Exports the mesh to the INP format.
  """
  def set_to_inp(sets, keyword):
    ss = ""
    for sk in sets.keys():
      labels = sets[sk].loc[sets[sk]].index.values
      labels = list(labels)
      labels.sort()
      if len(labels)!= 0:
        ss += "*{0}, {0}={1}\n".format(keyword, sk)
        ss += argiope.utils.list_to_string(labels) + "\n"
    return ss.strip()           

  # DATA
  mesh = mesh.copy()

  # NODES
  nodes_output = (mesh.nodes.coords.to_csv(header = False).split())
  nodes_output = ("\n".join(["  " + s.replace(",", ", ") for s in nodes_output]))
  
  # NODE SETS
  if "sets" in mesh.nodes.columns.levels[0]: 
    nsets = set_to_inp(mesh.nodes.sets, "NSET")
  else:
    nsets = "**"
  
  # SURFACES 
  surf_output = []
  if "surfaces" in mesh.elements.keys():
    sk = mesh.elements.surfaces.keys()
    for sindex in  np.unique(sk.labels[0]):
      slabel = sk.levels[0][sindex]
      surface = mesh.elements.surfaces[slabel]
      if surface.values.sum() != 0:
        mesh.surface_to_element_sets(slabel)
        surf_output.append( "*SURFACE, TYPE=ELEMENT, NAME={0}".format(slabel))
        for findex in surface.keys():
          if surface[findex].sum() != 0:
            surf_output.append("  _SURF_{0}_FACE{1}, S{1}".format(slabel, 
                                                                  findex[1:])) 
  else:
    surf_output.append("**")
  
  # ELEMENTS
  elements_output = ""
  for etype, group in mesh.elements.groupby((("type", "solver", ""),)):
    els = group.conn.replace(0, np.nan).to_csv(header = False, 
                                               float_format='%.0f').split()
    elements_output += "*ELEMENT, TYPE={0}\n".format(etype)
    elements_output += ("\n".join(["  " + s.strip().strip(",").
                             replace(",", ", ") for s in els]))
    elements_output += "\n"
  elements_output = elements_output.strip() 
  el_sets = {} 

  # MATERIALS
  section_output = ""
  for material, group in mesh.elements.groupby("materials"):
    slabel = "_MAT_{0}".format(material)
    section_output += "*ELSET, ELSET=_MAT_{0}\n{1}\n".format(
                 material,
                 argiope.utils.list_to_string(group.index.values))
    
    
    #mesh.elements[("sets", slabel, "")] = False
    #mesh.elements.loc[group.index, ("sets", slabel, "")] = True
    if sections == "solid":
      section_output += "*SOLID SECTION, ELSET=_MAT_{0}, MATERIAL={0}\n".format(
       material)

  # ELEMENTS SETS
  if "sets" in mesh.elements.columns.levels[0]: 
    esets = set_to_inp(mesh.elements.sets.swaplevel(1,0, axis = 1)[""],"ELSET")
  else:
    esets = "**"
  """
  ek = mesh.elements.sets.keys()
  for esindex in  np.unique(ek.labels[0]):
    eslabel = ek.levels[0][esindex]
    eset = mesh.elements.sets[slabel]
  """
     
  # PATTERN
  pattern = Template(open(MODPATH + "/templates/mesh/inp.inp").read())
  pattern = pattern.substitute(
    NODES     = nodes_output,
    NODE_SETS = nsets,
    ELEMENTS  = elements_output,
    ELEMENT_SETS = esets,
    ELEMENT_SURFACES = "\n".join(surf_output),
    SECTIONS = section_output.strip())
  pattern = pattern.strip()
  if path == None:            
    return pattern
  else:
    open(path, "w").write(pattern)
Exports the mesh to the INP format.