argiope/mesh.py
def write_xdmf(mesh, path, dataformat = "XML"):
  """
  Dumps the mesh to XDMF format.
  """
  pattern = Template(open(MODPATH + "/templates/mesh/xdmf.xdmf").read())
  attribute_pattern = Template(open(MODPATH + "/templates/mesh/xdmf_attribute.xdmf").read())
  # MAPPINGS
  cell_map = {
      "tri3":   4,
      "quad4":  5,
      "tetra4": 6,
      "pyra5":  7,
      "prism6": 8,
      "hexa8":  9}
  # REFERENCES
  nodes, elements = mesh.nodes.data, mesh.elements.data
  fields = mesh.fields
  # NUMBERS
  Ne, Nn = len(elements), len(nodes)
  # NODES
  nodes_map = np.arange(nodes.index.max()+1)
  nodes_map[nodes.index] = np.arange(len(nodes.index))
  nodes_map[0] = -1
  # ELEMENTS
  cols = ["n{0}".format(i) for i in range(elements.shape[1]-1)]
  connectivities  = mesh.elements.data[cols].as_matrix()
  connectivities[np.isnan(connectivities)] = 0
  connectivities = connectivities.astype(np.int32)
  connectivities = nodes_map[connectivities]
  labels          = np.array(elements.index)
  etypes          = np.array([cell_map[t] for t in elements.etype])
  lconn           = Ne + (connectivities != -1).sum()
  # FIELDS
  fields_string = ""
  field_data = {}
  for tag, field in fields.items():
      field_data[tag] = {}
      field.data.sort_index(inplace = True)
      fshape = field.data.shape[1]
      if   fshape  == 1: ftype = "Scalar"
      elif fshape  == 3: ftype = "Vector"
      elif fshape  == 2: 
        ftype = "Vector"
        # UGLY HACK...
        field = copy.copy(field)
        field.data["v3"] = np.zeros_like(field.data.index)
        fields[tag] = field
        # BACK TO NORMAL  
      elif fshape  == 6: ftype = "Tensor6"
      elif fshape  == 4: 
        ftype = "Tensor6"
        # UGLY HACK...
        field = copy.copy(field)
        field.data["v13"] = np.zeros_like(field.data.index)
        field.data["v23"] = np.zeros_like(field.data.index)
        fields[tag] = field
        # BACK TO NORMAL  
      if field.metadata.position == "Nodal": 
        position = "Node"
      if field.metadata.position == "Element":
        position = "Cell"  
      field_data[tag]["TAG"]           = tag
      field_data[tag]["ATTRIBUTETYPE"] = ftype
      field_data[tag]["FORMAT"]        = dataformat
      field_data[tag]["FIELD_DIMENSION"] = " ".join([str(l) for l in field.data.shape])
      field_data[tag]["POSITION"]      = position                             
  if dataformat == "XML":
    #NODES
    nodes_string = "\n".join([11*" " + "{0} {1} {2}".format(
                              n.x, 
                              n.y, 
                              n.z) 
                        for i, n in nodes.iterrows()])
    # ELEMENTS
    elements_string = ""
    for i in range(Ne):
      elements_string += 11*" " + str(etypes[i]) + " "
      c = connectivities[i]
      c = c[np.where(c != -1)]
      elements_string += " ".join([str(i) for i in c]) + "\n"
    elements_strings = elements_string[:-1]  
    # FIELDS
    for tag, field in fields.items():
      fdata = field.data.to_csv(sep = " ", 
                                index = False, 
                                header = False).split("\n")
      fdata = [11 * " " + l for l in fdata]
      fdata = "\n".join(fdata)
      field_data[tag]["DATA"] = fdata
      fields_string += attribute_pattern.substitute(**field_data[tag])     
  elif dataformat == "HDF":
    hdf = pd.HDFStore(path + ".h5")
    hdf.put("COORDS", mesh.nodes.data[list("xyz")])
    flatconn = np.zeros(lconn, dtype = np.int32)
    pos = 0
    for i in range(Ne):
      c = connectivities[i]
      c = c[np.where(c != -1)]
      lc = len(c)
      flatconn[pos] = etypes[i]
      flatconn[pos + 1 + np.arange(lc)] = c
      pos += 1 + lc
    hdf.put("CONNECTIVITY", pd.DataFrame(flatconn))  
    nodes_string = 11*" " + "{0}.h5:/COORDS/block0_values".format(path)
    elements_string = 11*" " + "{0}.h5:/CONNECTIVITY/block0_values".format(path)
    for tag, field in fields.items():
      fstrings[tag] = fstrings[tag].replace("#DATA", 
         11*" " + "{0}.h5:/FIELDS/{1}/block0_values".format(path, tag))
      fields_string += fstrings[tag]         
      hdf.put("FIELDS/{0}".format(tag), fields.data)
    hdf.close()
  """
  pattern = pattern.replace("#ELEMENT_NUMBER", str(Ne))
  pattern = pattern.replace("#CONN_DIMENSION", str(lconn))
  pattern = pattern.replace("#CONN_PATH", elements_string)
  pattern = pattern.replace("#NODE_NUMBER", str(Nn))
  pattern = pattern.replace("#NODE_PATH", nodes_string)
  pattern = pattern.replace("#DATAFORMAT", dataformat)
  pattern = pattern.replace("#ATTRIBUTES", fields_string) 
  """
  fields_string = "\n".join([attribute_pattern.substitute(**value) for key, value in field_data.items()])
  pattern = pattern.substitute(
     ELEMENT_NUMBER = str(Ne),
     CONN_DIMENSION = str(lconn),
     CONN_PATH      = elements_string,
     NODE_NUMBER    = str(Nn),
     NODE_PATH      = nodes_string,
     DATAFORMAT     = dataformat,
     ATTRIBUTES     = fields_string)
  open(path + ".xdmf", "wb").write(pattern)
def write_xdmf(mesh, path, dataformat = "XML"):
  """
  Dumps the mesh to XDMF format.
  """
  pattern = Template(open(MODPATH + "/templates/mesh/xdmf.xdmf").read())
  attribute_pattern = Template(open(MODPATH + "/templates/mesh/xdmf_attribute.xdmf").read())
  # MAPPINGS
  cell_map = {
      "tri3":   4,
      "quad4":  5,
      "tetra4": 6,
      "pyra5":  7,
      "prism6": 8,
      "hexa8":  9}
  # REFERENCES
  nodes, elements = mesh.nodes.data, mesh.elements.data
  fields = mesh.fields
  # NUMBERS
  Ne, Nn = len(elements), len(nodes)
  # NODES
  nodes_map = np.arange(nodes.index.max()+1)
  nodes_map[nodes.index] = np.arange(len(nodes.index))
  nodes_map[0] = -1
  # ELEMENTS
  cols = ["n{0}".format(i) for i in range(elements.shape[1]-1)]
  connectivities  = mesh.elements.data[cols].as_matrix()
  connectivities[np.isnan(connectivities)] = 0
  connectivities = connectivities.astype(np.int32)
  connectivities = nodes_map[connectivities]
  labels          = np.array(elements.index)
  etypes          = np.array([cell_map[t] for t in elements.etype])
  lconn           = Ne + (connectivities != -1).sum()
  # FIELDS
  fields_string = ""
  field_data = {}
  for tag, field in fields.items():
      field_data[tag] = {}
      field.data.sort_index(inplace = True)
      fshape = field.data.shape[1]
      if   fshape  == 1: ftype = "Scalar"
      elif fshape  == 3: ftype = "Vector"
      elif fshape  == 2: 
        ftype = "Vector"
        # UGLY HACK...
        field = copy.copy(field)
        field.data["v3"] = np.zeros_like(field.data.index)
        fields[tag] = field
        # BACK TO NORMAL  
      elif fshape  == 6: ftype = "Tensor6"
      elif fshape  == 4: 
        ftype = "Tensor6"
        # UGLY HACK...
        field = copy.copy(field)
        field.data["v13"] = np.zeros_like(field.data.index)
        field.data["v23"] = np.zeros_like(field.data.index)
        fields[tag] = field
        # BACK TO NORMAL  
      if field.metadata.position == "Nodal": 
        position = "Node"
      if field.metadata.position == "Element":
        position = "Cell"  
      field_data[tag]["TAG"]           = tag
      field_data[tag]["ATTRIBUTETYPE"] = ftype
      field_data[tag]["FORMAT"]        = dataformat
      field_data[tag]["FIELD_DIMENSION"] = " ".join([str(l) for l in field.data.shape])
      field_data[tag]["POSITION"]      = position                             
  if dataformat == "XML":
    #NODES
    nodes_string = "\n".join([11*" " + "{0} {1} {2}".format(
                              n.x, 
                              n.y, 
                              n.z) 
                        for i, n in nodes.iterrows()])
    # ELEMENTS
    elements_string = ""
    for i in range(Ne):
      elements_string += 11*" " + str(etypes[i]) + " "
      c = connectivities[i]
      c = c[np.where(c != -1)]
      elements_string += " ".join([str(i) for i in c]) + "\n"
    elements_strings = elements_string[:-1]  
    # FIELDS
    for tag, field in fields.items():
      fdata = field.data.to_csv(sep = " ", 
                                index = False, 
                                header = False).split("\n")
      fdata = [11 * " " + l for l in fdata]
      fdata = "\n".join(fdata)
      field_data[tag]["DATA"] = fdata
      fields_string += attribute_pattern.substitute(**field_data[tag])     
  elif dataformat == "HDF":
    hdf = pd.HDFStore(path + ".h5")
    hdf.put("COORDS", mesh.nodes.data[list("xyz")])
    flatconn = np.zeros(lconn, dtype = np.int32)
    pos = 0
    for i in range(Ne):
      c = connectivities[i]
      c = c[np.where(c != -1)]
      lc = len(c)
      flatconn[pos] = etypes[i]
      flatconn[pos + 1 + np.arange(lc)] = c
      pos += 1 + lc
    hdf.put("CONNECTIVITY", pd.DataFrame(flatconn))  
    nodes_string = 11*" " + "{0}.h5:/COORDS/block0_values".format(path)
    elements_string = 11*" " + "{0}.h5:/CONNECTIVITY/block0_values".format(path)
    for tag, field in fields.items():
      fstrings[tag] = fstrings[tag].replace("#DATA", 
         11*" " + "{0}.h5:/FIELDS/{1}/block0_values".format(path, tag))
      fields_string += fstrings[tag]         
      hdf.put("FIELDS/{0}".format(tag), fields.data)
    hdf.close()
  """
  pattern = pattern.replace("#ELEMENT_NUMBER", str(Ne))
  pattern = pattern.replace("#CONN_DIMENSION", str(lconn))
  pattern = pattern.replace("#CONN_PATH", elements_string)
  pattern = pattern.replace("#NODE_NUMBER", str(Nn))
  pattern = pattern.replace("#NODE_PATH", nodes_string)
  pattern = pattern.replace("#DATAFORMAT", dataformat)
  pattern = pattern.replace("#ATTRIBUTES", fields_string) 
  """
  fields_string = "\n".join([attribute_pattern.substitute(**value) for key, value in field_data.items()])
  pattern = pattern.substitute(
     ELEMENT_NUMBER = str(Ne),
     CONN_DIMENSION = str(lconn),
     CONN_PATH      = elements_string,
     NODE_NUMBER    = str(Nn),
     NODE_PATH      = nodes_string,
     DATAFORMAT     = dataformat,
     ATTRIBUTES     = fields_string)
  open(path + ".xdmf", "wb").write(pattern)
def write_xdmf ( mesh , path , dataformat = "XML" ) : pattern = Template ( open ( MODPATH + "/templates/mesh/xdmf.xdmf" ) . read ( ) ) attribute_pattern = Template ( open ( MODPATH + "/templates/mesh/xdmf_attribute.xdmf" ) . read ( ) ) # MAPPINGS cell_map = { "tri3" : 4 , "quad4" : 5 , "tetra4" : 6 , "pyra5" : 7 , "prism6" : 8 , "hexa8" : 9 } # REFERENCES nodes , elements = mesh . nodes . data , mesh . elements . data fields = mesh . fields # NUMBERS Ne , Nn = len ( elements ) , len ( nodes ) # NODES nodes_map = np . arange ( nodes . index . max ( ) + 1 ) nodes_map [ nodes . index ] = np . arange ( len ( nodes . index ) ) nodes_map [ 0 ] = - 1 # ELEMENTS cols = [ "n{0}" . format ( i ) for i in range ( elements . shape [ 1 ] - 1 ) ] connectivities = mesh . elements . data [ cols ] . as_matrix ( ) connectivities [ np . isnan ( connectivities ) ] = 0 connectivities = connectivities . astype ( np . int32 ) connectivities = nodes_map [ connectivities ] labels = np . array ( elements . index ) etypes = np . array ( [ cell_map [ t ] for t in elements . etype ] ) lconn = Ne + ( connectivities != - 1 ) . sum ( ) # FIELDS fields_string = "" field_data = { } for tag , field in fields . items ( ) : field_data [ tag ] = { } field . data . sort_index ( inplace = True ) fshape = field . data . shape [ 1 ] if fshape == 1 : ftype = "Scalar" elif fshape == 3 : ftype = "Vector" elif fshape == 2 : ftype = "Vector" # UGLY HACK... field = copy . copy ( field ) field . data [ "v3" ] = np . zeros_like ( field . data . index ) fields [ tag ] = field # BACK TO NORMAL   elif fshape == 6 : ftype = "Tensor6" elif fshape == 4 : ftype = "Tensor6" # UGLY HACK... field = copy . copy ( field ) field . data [ "v13" ] = np . zeros_like ( field . data . index ) field . data [ "v23" ] = np . zeros_like ( field . data . index ) fields [ tag ] = field # BACK TO NORMAL   if field . metadata . position == "Nodal" : position = "Node" if field . metadata . position == "Element" : position = "Cell" field_data [ tag ] [ "TAG" ] = tag field_data [ tag ] [ "ATTRIBUTETYPE" ] = ftype field_data [ tag ] [ "FORMAT" ] = dataformat field_data [ tag ] [ "FIELD_DIMENSION" ] = " " . join ( [ str ( l ) for l in field . data . shape ] ) field_data [ tag ] [ "POSITION" ] = position if dataformat == "XML" : #NODES nodes_string = "\n" . join ( [ 11 * " " + "{0} {1} {2}" . format ( n . x , n . y , n . z ) for i , n in nodes . iterrows ( ) ] ) # ELEMENTS elements_string = "" for i in range ( Ne ) : elements_string += 11 * " " + str ( etypes [ i ] ) + " " c = connectivities [ i ] c = c [ np . where ( c != - 1 ) ] elements_string += " " . join ( [ str ( i ) for i in c ] ) + "\n" elements_strings = elements_string [ : - 1 ] # FIELDS for tag , field in fields . items ( ) : fdata = field . data . to_csv ( sep = " " , index = False , header = False ) . split ( "\n" ) fdata = [ 11 * " " + l for l in fdata ] fdata = "\n" . join ( fdata ) field_data [ tag ] [ "DATA" ] = fdata fields_string += attribute_pattern . substitute ( * * field_data [ tag ] ) elif dataformat == "HDF" : hdf = pd . HDFStore ( path + ".h5" ) hdf . put ( "COORDS" , mesh . nodes . data [ list ( "xyz" ) ] ) flatconn = np . zeros ( lconn , dtype = np . int32 ) pos = 0 for i in range ( Ne ) : c = connectivities [ i ] c = c [ np . where ( c != - 1 ) ] lc = len ( c ) flatconn [ pos ] = etypes [ i ] flatconn [ pos + 1 + np . arange ( lc ) ] = c pos += 1 + lc hdf . put ( "CONNECTIVITY" , pd . DataFrame ( flatconn ) ) nodes_string = 11 * " " + "{0}.h5:/COORDS/block0_values" . format ( path ) elements_string = 11 * " " + "{0}.h5:/CONNECTIVITY/block0_values" . format ( path ) for tag , field in fields . items ( ) : fstrings [ tag ] = fstrings [ tag ] . replace ( "#DATA" , 11 * " " + "{0}.h5:/FIELDS/{1}/block0_values" . format ( path , tag ) ) fields_string += fstrings [ tag ] hdf . put ( "FIELDS/{0}" . format ( tag ) , fields . data ) hdf . close ( ) """
  pattern = pattern.replace("#ELEMENT_NUMBER", str(Ne))
  pattern = pattern.replace("#CONN_DIMENSION", str(lconn))
  pattern = pattern.replace("#CONN_PATH", elements_string)
  pattern = pattern.replace("#NODE_NUMBER", str(Nn))
  pattern = pattern.replace("#NODE_PATH", nodes_string)
  pattern = pattern.replace("#DATAFORMAT", dataformat)
  pattern = pattern.replace("#ATTRIBUTES", fields_string) 
  """ fields_string = "\n" . join ( [ attribute_pattern . substitute ( * * value ) for key , value in field_data . items ( ) ] ) pattern = pattern . substitute ( ELEMENT_NUMBER = str ( Ne ) , CONN_DIMENSION = str ( lconn ) , CONN_PATH = elements_string , NODE_NUMBER = str ( Nn ) , NODE_PATH = nodes_string , DATAFORMAT = dataformat , ATTRIBUTES = fields_string ) open ( path + ".xdmf" , "wb" ) . write ( pattern )
Dumps the mesh to XDMF format.
Dumps the mesh to XDMF format .