Exemple #1
0
def ImplicitFunctionVtuCut(inputVtu, implicitFunction):
  """
  Perform a cut of a vtu. Cutting as in ClipCow.py vtkCutter example of vtk
  documentation 5.0.4, and Fluidity test case lock_exchange_tet.xml results
  variable.
  """
  
  # The cutter
  cutter = vtk.vtkCutter()
  cutter.SetCutFunction(implicitFunction)
  if vtk.vtkVersion.GetVTKMajorVersion() <= 5:
    cutter.SetInput(inputVtu.ugrid)
  else:
    cutter.SetInputData(inputVtu.ugrid)
  # Cut
  cutter.Update()
  cutUPoly = cutter.GetOutput()
  # Construct output  
  result = vtu()
  result.ugrid = PolyDataToUnstructuredGrid(cutUPoly)
  
  if result.ugrid.GetNumberOfPoints() == 0:
    debug.deprint("Warning: Cut vtu contains no nodes")
  
  return result
Exemple #2
0
def ImplicitFunctionVtuCut(inputVtu, implicitFunction):
    """
  Perform a cut of a vtu. Cutting as in ClipCow.py vtkCutter example of vtk
  documentation 5.0.4, and Fluidity test case lock_exchange_tet.xml results
  variable.
  """

    # The cutter
    cutter = vtk.vtkCutter()
    cutter.SetCutFunction(implicitFunction)
    if vtk.vtkVersion.GetVTKMajorVersion() <= 5:
        cutter.SetInput(inputVtu.ugrid)
    else:
        cutter.SetInputData(inputVtu.ugrid)
    # Cut
    cutter.Update()
    cutUPoly = cutter.GetOutput()
    # Construct output
    result = vtu()
    result.ugrid = PolyDataToUnstructuredGrid(cutUPoly)

    if result.ugrid.GetNumberOfPoints() == 0:
        debug.deprint("Warning: Cut vtu contains no nodes")

    return result
Exemple #3
0
def ReadMsh(filename):
    """
  Read a Gmsh msh file
  """

    fileHandle = open(filename, "rb")

    basename = filename.split(".")[0]
    hasHalo = filehandling.FileExists(basename + ".halo")

    # Read the MeshFormat section

    line = ReadNonCommentLine(fileHandle)
    assert (line == "$MeshFormat")

    line = ReadNonCommentLine(fileHandle)
    lineSplit = line.split()
    assert (len(lineSplit) == 3)
    version = lineSplit[0]
    fileType = int(lineSplit[1])
    dataSize = int(lineSplit[2])

    if version[0] == "4" and version < "4.1":
        raise Exception("gmshtools doesn't handle msh4 with minor version 0")

    if fileType == 1:
        # Binary format
        if version[0] == "4":
            mesh = ReadBinaryMshV4(fileHandle, dataSize)
        elif version[0] == "2":
            mesh = ReadBinaryMshV2(fileHandle, dataSize)
        else:
            raise Exception("Unknown gmsh major version")
    elif fileType == 0:
        # ASCII format
        if version[0] == "4":
            mesh = ReadAsciiMshV4(fileHandle)
        elif version[0] == "2":
            mesh = ReadAsciiMshV2(fileHandle)
        else:
            raise Exception("Unknown gmsh major version")
    else:
        raise Exception("File type " + str(fileType) + " not recognised")

    fileHandle.close()

    if hasHalo:
        # Read the .halo file
        debug.dprint("Reading .halo file")

        if mesh_halos.HaloIOSupport():
            halos = mesh_halos.ReadHalos(basename + ".halo")
            mesh.SetHalos(halos)
        else:
            debug.deprint("Warning: No .halo I/O support")

    return mesh
Exemple #4
0
 def _SetElementTypeIdFromData(self, dim, nodeCount):
   if (dim, nodeCount) in self._elementTypeIdsMap:
     self._elementTypeId = self._elementTypeIdsMap[(dim, nodeCount)]
   else:
     debug.deprint("Warning: Unknown element type with " + str(nodeCount) + " nodes in " + str(dim) + " dimensions")
     self._elementTypeId = ELEMENT_UNKNOWN
   
   self._RaiseEvent("elementTypeIdChange")
   
   return
Exemple #5
0
 def _SetElementTypeIdFromData(self, dim, nodeCount):
   if (dim, nodeCount) in self._elementTypeIdsMap:
     self._elementTypeId = self._elementTypeIdsMap[(dim, nodeCount)]
   else:
     debug.deprint("Warning: Unknown element type with " + str(nodeCount) + " nodes in " + str(dim) + " dimensions")
     self._elementTypeId = ELEMENT_UNKNOWN
   
   self._RaiseEvent("elementTypeIdChange")
   
   return
Exemple #6
0
def AddtoVtu(vtu, add, scale=None):
    """
  Add a vtu onto an input vtu
  """

    if VtuMatchLocations(vtu, add):
        for fieldName in vtu.GetFieldNames():
            AddtoVtuField(vtu, add, fieldName, scale=scale)
    else:
        # This could get expensive
        debug.deprint("Warning: vtu locations do not match - remapping")
        remappedAddVtu = RemappedVtu(add, vtu)
        for fieldName in vtu.GetFieldNames():
            AddtoVtuField(vtu, remappedAddVtu, fieldName, scale=scale)

    return
Exemple #7
0
def AddtoVtu(vtu, add, scale = None):
  """
  Add a vtu onto an input vtu
  """

  if VtuMatchLocations(vtu, add):
    for fieldName in vtu.GetFieldNames():
      AddtoVtuField(vtu, add, fieldName, scale = scale)
  else:
    # This could get expensive
    debug.deprint("Warning: vtu locations do not match - remapping")
    remappedAddVtu = RemappedVtu(add, vtu)
    for fieldName in vtu.GetFieldNames():
      AddtoVtuField(vtu, remappedAddVtu, fieldName, scale = scale)
  
  return
Exemple #8
0
    def InterpolatedValue(self, name, latitude, longitude, time):
        """
    Tri-linearly interpolate the supplied field at the supplied latitude,
    longitude and time
    """

        data = self._file.variables[name]

        if latitude > self._latitudes[0] or latitude < self._latitudes[-1]:
            debug.deprint("latitude = " + str(latitude))
            debug.deprint("Valid latitude range = " +
                          str((self._latitudes[-1], self._latitudes[0])))
            raise Exception("Invalid latitude")
        while longitude < 0.0:
            longitude += 360.0
        while longitude > 360.0:
            longitude -= 360.0

        if time < self._times[0] or time > self._times[-1]:
            debug.deprint("time = " + str(time))
            debug.deprint("Valid time range = " +
                          str((self._times[0], self._times[-1])))
            raise Exception("Invalid time")

        left = structured_fields.IndexBinarySearch(longitude, self._longitudes)
        right = left + 1
        assert (not right == left)
        longRatio = (longitude - self._longitudes[left]) / (
            self._longitudes[right] - self._longitudes[left])
        left %= len(self._longitudes) - 1
        right %= len(self._longitudes) - 1

        upper = structured_fields.IndexBinaryDecSearch(latitude,
                                                       self._latitudes)
        lower = min(upper + 1, len(self._latitudes) - 1)
        if upper == lower:
            latRatio = 1.0
        else:
            latRatio = (latitude - self._latitudes[lower]) / (
                self._latitudes[upper] - self._latitudes[lower])

        before = structured_fields.IndexBinarySearch(time, self._times)
        after = min(before + 1, len(self._times) - 1)
        if after == before:
            timeRatio = 1.0
        else:
            timeRatio = (time - self._times[before]) / (self._times[after] -
                                                        self._times[before])

        timeVals = [None, None]
        for i, timeIndex in enumerate([before, after]):
            timeVals[i] = calc.BilinearlyInterpolate(self.Value(name, timeIndex, upper, left), self.Value(name, timeIndex, upper, right), \
              self.Value(name, timeIndex, lower, left), self.Value(name, timeIndex, lower, right), \
              longRatio, latRatio)

        return calc.LinearlyInterpolate(timeVals[0], timeVals[1], timeRatio)
Exemple #9
0
def VtuToMesh(vtu, idsName="IDs"):
    """
  Construct a mesh from the supplied vtu
  """

    dim = vtktools.VtuDim(vtu)
    boundingBox = vtktools.VtuBoundingBox(vtu)
    dimIndices = boundingBox.UsedDimIndices()

    mesh = Mesh(dim)

    # Read the points
    for location in vtu.GetLocations():
        location = numpy.array(location)
        mesh.AddNodeCoord(location[dimIndices])

    # Read the boundary / region IDs
    cellData = vtu.ugrid.GetCellData().GetArray(idsName)

    # Read the elements
    for i in range(vtu.ugrid.GetNumberOfCells()):
        cell = vtu.ugrid.GetCell(i)
        nodeIds = cell.GetPointIds()
        if cellData is None:
            id = None
        else:
            id = cellData.GetTuple1(i)

        type = vtktools.VtkType(vtkTypeId=cell.GetCellType())
        element = elements.Element(
            nodes=[nodeIds.GetId(i) for i in range(nodeIds.GetNumberOfIds())],
            ids=id)

        if type.GetDim() == dim - 1:
            mesh.AddSurfaceElement(element)
        elif type.GetDim() == dim:
            mesh.AddVolumeElement(element)
        else:
            debug.deprint(
                "Warning: Found element in vtu that is neither a surface nor volume element"
            )

    return mesh
Exemple #10
0
        def ParseRawS(s, delimiter):
            newS = {}
            for key1 in s.keys():
                assert (not key1 in ["val", "value"])
                if isinstance(s[key1], dict):
                    if len(s[key1].keys()) == 1 and s[key1].keys()[0] in [
                            "val", "value"
                    ]:
                        newS[str(key1)] = s[key1][s[key1].keys()[0]]
                    else:
                        subS = ParseRawS(s[key1], delimiter)
                        newS[str(key1)] = {}
                        for key2 in subS.keys():
                            newS[str(key1)][str(key2)] = subS[key2]
                else:
                    rank = len(s[key1].shape)
                    if rank > 1:
                        assert (rank == 2)
                        if includeMc:
                            # Add in this vector

                            # stat_parser gives this in an inconvenient matrix order. Take the
                            # transpose here to make life easier.
                            newS[str(key1)] = s[key1].transpose()

                        # Add in the vector field components
                        for i in range(len(s[key1])):
                            newS[str(key1) + delimiter +
                                 str(i + 1)] = s[key1][i]
                    else:
                        try:
                            # Add in this scalar
                            newS[str(key1)] = s[key1]
                        except TypeError:
                            debug.deprint(
                                "Type error for data " + str(s[key1]), 0)
                            raise Exception("ParseRawS failure")
                        except ValueError:
                            debug.deprint(
                                "Value error for data " + str(s[key1]), 0)
                            raise Exception("ParseRawS failure")

            return newS
Exemple #11
0
 def _initialise_math(self):
   """
   Detect math routines
   """
   
   if NumpySupport():
     # Use numpy if we can, as the math calls are performance critical
     self._cos = numpy.cos
     self._sin = numpy.sin
     self._atan = numpy.arctan
     self._atan2 = numpy.arctan2
   else:
     # Otherwise, fall back to math
     debug.deprint("Warning: Unable to use numpy module in Lomb-Scargle periodogram")
     self._cos = math.cos
     self._sin = math.sin
     self._atan = math.atan
     self._atan2 = math.atan2
   
   return
Exemple #12
0
  def InterpolatedValue(self, name, latitude, longitude, time): 
    """
    Tri-linearly interpolate the supplied field at the supplied latitude,
    longitude and time
    """
     
    data = self._file.variables[name]

    if latitude > self._latitudes[0] or latitude < self._latitudes[-1]:
      debug.deprint("latitude = " + str(latitude))
      debug.deprint("Valid latitude range = " + str((self._latitudes[-1], self._latitudes[0])))
      raise Exception("Invalid latitude")
    while longitude < 0.0:
      longitude += 360.0
    while longitude > 360.0:
      longitude -= 360.0

    if time < self._times[0] or time > self._times[-1]:
      debug.deprint("time = " + str(time))
      debug.deprint("Valid time range = " + str((self._times[0], self._times[-1])))
      raise Exception("Invalid time")

    left = structured_fields.IndexBinarySearch(longitude, self._longitudes)
    right = left + 1
    assert(not right == left)
    longRatio = (longitude - self._longitudes[left]) / (self._longitudes[right] - self._longitudes[left])
    left %= len(self._longitudes) - 1
    right %= len(self._longitudes) - 1

    upper = structured_fields.IndexBinaryDecSearch(latitude, self._latitudes)
    lower = min(upper + 1, len(self._latitudes) - 1)
    if upper == lower:
      latRatio = 1.0
    else:
      latRatio = (latitude - self._latitudes[lower]) / (self._latitudes[upper] - self._latitudes[lower])

    before = structured_fields.IndexBinarySearch(time, self._times)
    after = min(before + 1, len(self._times) - 1)
    if after == before:
      timeRatio = 1.0
    else:
      timeRatio = (time - self._times[before]) / (self._times[after] - self._times[before])

    timeVals = [None, None]
    for i, timeIndex in enumerate([before, after]):
      timeVals[i] = calc.BilinearlyInterpolate(self.Value(name, timeIndex, upper, left), self.Value(name, timeIndex, upper, right), \
        self.Value(name, timeIndex, lower, left), self.Value(name, timeIndex, lower, right), \
        longRatio, latRatio)

    return calc.LinearlyInterpolate(timeVals[0], timeVals[1], timeRatio)
Exemple #13
0
def VtuToMesh(vtu, idsName = "IDs"):
  """
  Construct a mesh from the supplied vtu
  """
  
  dim = vtktools.VtuDim(vtu)
  boundingBox = vtktools.VtuBoundingBox(vtu)
  dimIndices = boundingBox.UsedDimIndices()
  
  mesh = Mesh(dim)
  
  # Read the points
  for location in vtu.GetLocations():
    location = numpy.array(location)
    mesh.AddNodeCoord(location[dimIndices])
    
  # Read the boundary / region IDs
  cellData = vtu.ugrid.GetCellData().GetArray(idsName)
    
  # Read the elements
  for i in range(vtu.ugrid.GetNumberOfCells()):
    cell = vtu.ugrid.GetCell(i)
    nodeIds = cell.GetPointIds()
    if cellData is None:
      id = None
    else:
      id = cellData.GetTuple1(i)
    
    type = vtktools.VtkType(vtkTypeId = cell.GetCellType())
    element = elements.Element(nodes = vtktools.FromVtkNodeOrder([nodeIds.GetId(i) for i in range(nodeIds.GetNumberOfIds())], type), ids = id)
    
    if type.GetDim() == dim - 1:
      mesh.AddSurfaceElement(element)
    elif type.GetDim() == dim:
      mesh.AddVolumeElement(element)
    else:
      debug.deprint("Warning: Found element in vtu that is neither a surface nor volume element")

  return mesh
Exemple #14
0
    def _initialise_math(self):
        """
    Detect math routines
    """

        if NumpySupport():
            # Use numpy if we can, as the math calls are performance critical
            self._cos = numpy.cos
            self._sin = numpy.sin
            self._atan = numpy.arctan
            self._atan2 = numpy.arctan2
        else:
            # Otherwise, fall back to math
            debug.deprint(
                "Warning: Unable to use numpy module in Lomb-Scargle periodogram"
            )
            self._cos = math.cos
            self._sin = math.sin
            self._atan = math.atan
            self._atan2 = math.atan2

        return
 def ParseRawS(s, delimiter):    
   newS = {}
   for key1 in s.keys():
     assert(not key1 in ["val", "value"])
     if isinstance(s[key1], dict):
       if len(s[key1].keys()) == 1 and s[key1].keys()[0] in ["val", "value"]:
         newS[str(key1)] = s[key1][s[key1].keys()[0]]
       else:
         subS = ParseRawS(s[key1], delimiter)
         newS[str(key1)] = {}
         for key2 in subS.keys():
           newS[str(key1)][str(key2)] = subS[key2]
     else:        
       rank = len(s[key1].shape)
       if rank > 1:
         assert(rank == 2)
         if includeMc:
           # Add in this vector
           
           # stat_parser gives this in an inconvenient matrix order. Take the
           # transpose here to make life easier.
           newS[str(key1)] = s[key1].transpose()
           
         # Add in the vector field components
         for i in range(len(s[key1])):
           newS[str(key1) + delimiter + str(i + 1)] = s[key1][i]
       else:
         try:
           # Add in this scalar
           newS[str(key1)] = s[key1]
         except TypeError:
           debug.deprint("Type error for data " + str(s[key1]), 0)
           raise Exception("ParseRawS failure")
         except ValueError:
           debug.deprint("Value error for data " + str(s[key1]), 0)
           raise Exception("ParseRawS failure")
       
   return newS
Exemple #16
0
def ModelPvtuToVtu(pvtu):
    """
  Convert a parallel vtu to a serial vtu but without any fields. Does nothing
  (except generate a copy) if the supplied vtu is already a serial vtu.
  """

    # Step 1: Extract the ghost levels, and check that we have a parallel vtu

    result = vtu()
    ghostLevel = pvtu.ugrid.GetCellData().GetArray("vtkGhostLevels")
    if ghostLevel is None:
        # We have a serial vtu
        debug.deprint("Warning: input file contains no vtkGhostLevels")
        ghostLevel = [0 for i in range(pvtu.ugrid.GetNumberOfCells())]
    else:
        # We have a parallel vtu
        ghostLevel = [
            ghostLevel.GetValue(i)
            for i in range(ghostLevel.GetNumberOfComponents() *
                           ghostLevel.GetNumberOfTuples())
        ]

    # Step 2: Collect the non-ghost cell IDs

    debug.dprint("Input cells: " + str(pvtu.ugrid.GetNumberOfCells()))

    cellIds = []
    keepCell = [False for i in range(pvtu.ugrid.GetNumberOfCells())]
    oldCellIdToNew = [None for i in range(pvtu.ugrid.GetNumberOfCells())]

    # Collect the new non-ghost cell IDs and generate the cell renumbering map
    index = 0
    for i, level in enumerate(ghostLevel):
        if calc.AlmostEquals(level, 0.0):
            cellIds.append(i)
            keepCell[i] = True
            oldCellIdToNew[i] = index
            index += 1

    debug.dprint("Non-ghost cells: " + str(len(cellIds)))

    # Step 3: Collect the non-ghost node IDs

    debug.dprint("Input points: " + str(pvtu.ugrid.GetNumberOfPoints()))

    keepNode = [False for i in range(pvtu.ugrid.GetNumberOfPoints())]

    # Find a list of candidate non-ghost node IDs, based on nodes attached to
    # non-ghost cells
    keepNodeCount = 0
    for cellId in cellIds:
        cellNodeIds = pvtu.ugrid.GetCell(cellId).GetPointIds()
        cellNodeIds = [
            cellNodeIds.GetId(i) for i in range(cellNodeIds.GetNumberOfIds())
        ]
        keepNodeCount += len(cellNodeIds)
        for nodeId in cellNodeIds:
            keepNode[nodeId] = True

    uniqueKeepNodeCount = keepNode.count(True)
    debug.dprint("Non-ghost nodes (pass 1): " + str(uniqueKeepNodeCount))
    if uniqueKeepNodeCount == keepNodeCount:
        debug.dprint("Assuming pvtu is discontinuous")
        # we're keeping all non-ghost nodes:
        nodeIds = [
            i for i in range(pvtu.ugrid.GetNumberOfPoints()) if keepNode[i]
        ]
        oldNodeIdToNew = numpy.array([None] * pvtu.ugrid.GetNumberOfPoints())
        oldNodeIdToNew[nodeIds] = range(keepNodeCount)
    else:
        # for the CG case we still have duplicate nodes that need to be removed
        oldNodeIdToNew, nodeIds = PvtuToVtuRemoveDuplicateNodes(pvtu, keepNode)

    # Step 4: Generate the new locations
    locations = pvtu.GetLocations()
    locations = numpy.array([locations[i] for i in nodeIds])
    points = vtk.vtkPoints()
    points.SetDataTypeToDouble()
    for location in locations:
        points.InsertNextPoint(location)
    result.ugrid.SetPoints(points)

    # Step 5: Generate the new cells
    for cellId in cellIds:
        cell = pvtu.ugrid.GetCell(cellId)
        cellNodeIds = cell.GetPointIds()
        cellNodeIds = [
            cellNodeIds.GetId(i) for i in range(cellNodeIds.GetNumberOfIds())
        ]
        idList = vtk.vtkIdList()
        for nodeId in cellNodeIds:
            oldNodeId = nodeId
            nodeId = oldNodeIdToNew[nodeId]
            assert (not nodeId is None)
            assert (nodeId >= 0)
            assert (nodeId <= len(nodeIds))
            idList.InsertNextId(nodeId)
        result.ugrid.InsertNextCell(cell.GetCellType(), idList)

    return result, oldNodeIdToNew, oldCellIdToNew
Exemple #17
0
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
"""
VTK tools
"""

import copy
import math
import sys
import unittest

import fluidity.diagnostics.debug as debug

try:
    import vtk
except ImportError:
    debug.deprint("Warning: Failed to import vtk module")

try:
    from vtktools import *
except ImportError:
    debug.deprint("Warning: Failed to import vtktools module")

import fluidity.diagnostics.bounds as bounds
import fluidity.diagnostics.calc as calc
import fluidity.diagnostics.elements as elements
import fluidity.diagnostics.optimise as optimise
import fluidity.diagnostics.simplices as simplices
import fluidity.diagnostics.utils as utils


def VtkSupport():
Exemple #18
0
def ModelPvtuToVtu(pvtu):
    """
  Convert a parallel vtu to a serial vtu but without any fields. Does nothing
  (except generate a copy) if the supplied vtu is already a serial vtu.
  """

    # Step 1: Extract the ghost levels, and check that we have a parallel vtu

    result = vtu()
    ghostLevel = pvtu.ugrid.GetCellData().GetArray("vtkGhostLevels")
    if ghostLevel is None:
        # We have a serial vtu
        debug.deprint("Warning: VtuFromPvtu passed a serial vtu")
        ghostLevel = [0 for i in range(vtu.ugrid.GetNumberOfCells())]
    else:
        # We have a parallel vtu
        ghostLevel = [
            ghostLevel.GetValue(i)
            for i in range(ghostLevel.GetNumberOfComponents() *
                           ghostLevel.GetNumberOfTuples())
        ]

    # Step 2: Collect the non-ghost cell IDs

    debug.dprint("Input cells: " + str(pvtu.ugrid.GetNumberOfCells()))

    cellIds = []
    keepCell = [False for i in range(pvtu.ugrid.GetNumberOfCells())]
    oldCellIdToNew = [None for i in range(pvtu.ugrid.GetNumberOfCells())]

    # Collect the new non-ghost cell IDs and generate the cell renumbering map
    index = 0
    for i, level in enumerate(ghostLevel):
        if calc.AlmostEquals(level, 0.0):
            cellIds.append(i)
            keepCell[i] = True
            oldCellIdToNew[i] = index
            index += 1

    debug.dprint("Non-ghost cells: " + str(len(cellIds)))

    # Step 3: Collect the non-ghost node IDs

    debug.dprint("Input points: " + str(pvtu.ugrid.GetNumberOfPoints()))

    nodeIds = []
    keepNode = [False for i in range(pvtu.ugrid.GetNumberOfPoints())]
    oldNodeIdToNew = [None for i in range(pvtu.ugrid.GetNumberOfPoints())]

    # Find a list of candidate non-ghost node IDs, based on nodes attached to
    # non-ghost cells
    for cellId in cellIds:
        cellNodeIds = pvtu.ugrid.GetCell(cellId).GetPointIds()
        cellNodeIds = [
            cellNodeIds.GetId(i) for i in range(cellNodeIds.GetNumberOfIds())
        ]
        for nodeId in cellNodeIds:
            keepNode[nodeId] = True

    debug.dprint("Non-ghost nodes (pass 1): " + str(keepNode.count(True)))

    # Detect duplicate nodes

    # Jumping through Python 2.3 hoops for cx1 - in >= 2.4, can just pass a cmp
    # argument to list.sort
    class LocationSorter(utils.Sorter):
        def __init__(self, x, y, order=[0, 1, 2]):
            utils.Sorter.__init__(self, x, y)
            self._order = order

            return

        def __cmp__(self, val):
            def cmp(x, y, order):
                for comp in order:
                    if x[comp] > y[comp]:
                        return 1
                    elif x[comp] < y[comp]:
                        return -1

                return 0

            return cmp(self._key, val.GetKey(), self._order)

    def Dup(x, y, tol):
        for i, xVal in enumerate(x):
            if abs(xVal - y[i]) > tol:
                return False

        return True

    locations = pvtu.GetLocations()
    lbound, ubound = VtuBoundingBox(pvtu).GetBounds()
    tol = calc.L2Norm([ubound[i] - lbound[i]
                       for i in range(len(lbound))]) / 1.0e12
    debug.dprint("Duplicate node tolerance: " + str(tol))

    duplicateNodeMap = [None for i in range(pvtu.ugrid.GetNumberOfPoints())]
    duplicateNodeMapInverse = [[] for i in range(len(duplicateNodeMap))]
    # We need to sort the locations using all possible combinations of component
    # order, to take account of all possible floating point errors.
    orders = [[0], [[0, 1], [1, 0]],
              [[0, 1, 2], [0, 2, 1], [1, 0, 2], [1, 2, 0], [2, 0, 1],
               [2, 1, 0]]][VtuDim(pvtu) - 1]
    for order in orders:
        debug.dprint("Processing component order: " + str(order))

        # Generate a sorted list of locations, with their node IDs
        sortPack = [
            LocationSorter(location.tolist(), i, order)
            for i, location in enumerate(locations)
        ]
        sortPack.sort()
        permutedLocations = [pack.GetKey() for pack in sortPack]
        permutedNodeIds = [pack.GetValue() for pack in sortPack]

        # This rather horrible construction maps all except the first node in each set
        # of duplicate nodes to the first node in the set of duplicate nodes, for the
        # sorted current non-ghost locations
        i = 0
        while i < len(permutedLocations) - 1:
            j = i
            while j < len(permutedLocations) - 1:
                if Dup(permutedLocations[i], permutedLocations[j + 1], tol):
                    if keepNode[permutedNodeIds[j + 1]]:
                        oldNodeId = permutedNodeIds[j + 1]
                        newNodeId = permutedNodeIds[i]
                        while not duplicateNodeMap[newNodeId] is None:
                            newNodeId = duplicateNodeMap[newNodeId]
                            if newNodeId == oldNodeId:
                                # This is already mapped the other way
                                break
                        if newNodeId == oldNodeId:
                            # Can only occur from early exit of the above loop
                            j += 1
                            continue

                        def MapInverses(oldNodeId, newNodeId):
                            for nodeId in duplicateNodeMapInverse[oldNodeId]:
                                assert (not nodeId == newNodeId)
                                assert (keepNode[newNodeId])
                                keepNode[nodeId] = False
                                duplicateNodeMap[nodeId] = newNodeId
                                duplicateNodeMapInverse[newNodeId].append(
                                    nodeId)
                                MapInverses(nodeId, newNodeId)
                            duplicateNodeMapInverse[oldNodeId] = []

                            return

                        keepNode[newNodeId] = True
                        keepNode[oldNodeId] = False
                        # Map everything mapped to the old node ID to the new node ID
                        MapInverses(oldNodeId, newNodeId)
                        duplicateNodeMap[oldNodeId] = newNodeId
                        duplicateNodeMapInverse[newNodeId].append(oldNodeId)
                    j += 1
                else:
                    break
            i = j
            i += 1

        debug.dprint("Non-ghost nodes: " + str(keepNode.count(True)))

    # Collect the final non-ghost node IDs and generate the node renumbering map
    nodeIds = []
    index = 0
    for i, keep in enumerate(keepNode):
        if keep:
            nodeIds.append(i)
            oldNodeIdToNew[i] = index
            index += 1
    for i, nodeId in enumerate(duplicateNodeMap):
        if not nodeId is None:
            assert (oldNodeIdToNew[i] is None)
            assert (not oldNodeIdToNew[nodeId] is None)
            oldNodeIdToNew[i] = oldNodeIdToNew[nodeId]

    debug.dprint("Non-ghost nodes (pass 2): " + str(len(nodeIds)))

    # Step 4: Generate the new locations
    locations = pvtu.GetLocations()
    locations = numpy.array([locations[i] for i in nodeIds])
    points = vtk.vtkPoints()
    points.SetDataTypeToDouble()
    for location in locations:
        points.InsertNextPoint(location)
    result.ugrid.SetPoints(points)

    # Step 5: Generate the new cells
    for cellId in cellIds:
        cell = pvtu.ugrid.GetCell(cellId)
        cellNodeIds = cell.GetPointIds()
        cellNodeIds = [
            cellNodeIds.GetId(i) for i in range(cellNodeIds.GetNumberOfIds())
        ]
        idList = vtk.vtkIdList()
        for nodeId in cellNodeIds:
            oldNodeId = nodeId
            nodeId = oldNodeIdToNew[nodeId]
            assert (not nodeId is None)
            assert (nodeId >= 0)
            assert (nodeId <= len(nodeIds))
            idList.InsertNextId(nodeId)
        result.ugrid.InsertNextCell(cell.GetCellType(), idList)

    return result, oldNodeIdToNew, oldCellIdToNew
Exemple #19
0
"""
Some mathematical routines
"""

from __future__ import print_function

import copy
import math
import unittest

import fluidity.diagnostics.debug as debug

try:
    import numpy
except ImportError:
    debug.deprint("Warning: Failed to import numpy module")
try:
    import scipy.linalg
except ImportError:
    debug.deprint("Warning: Failed to import scipy.linalg module")
try:
    import scipy.stats
except ImportError:
    debug.deprint("Warning: Failed to import scipy.stats module")
try:
    import vtk
except ImportError:
    debug.deprint("Warning: Failed to import vtk module")

import fluidity.diagnostics.optimise as optimise
import fluidity.diagnostics.utils as utils
Exemple #20
0
def ReadAsciiMshV4(fileHandle):
    line = ReadNonCommentLine(fileHandle)
    assert (line == "$EndMeshFormat")

    # skip to the Nodes section
    while line != "$Nodes":
        line = ReadNonCommentLine(fileHandle)
    assert (line == "$Nodes")

    line = ReadNonCommentLine(fileHandle)
    lineSplit = line.split()
    numBlocks = int(lineSplit[0])
    numNodes = int(lineSplit[1])
    seenNode = [False] * numNodes
    nodeIds = []
    nodes = []
    lbound = [calc.Inf() for i in range(3)]
    ubound = [-calc.Inf() for i in range(3)]
    for b in range(numBlocks):
        line = ReadNonCommentLine(fileHandle)
        lineSplit = line.split()
        subNodes = int(lineSplit[3])
        tagArr = [int(ReadNonCommentLine(fileHandle)) for i in range(subNodes)]
        for i in range(subNodes):
            line = ReadNonCommentLine(fileHandle)
            lineSplit = line.split()
            assert (len(lineSplit) == 3)

            nodeId = tagArr[i]
            coord = [float(comp) for comp in lineSplit]
            assert (nodeId > 0)
            assert (not seenNode[nodeId - 1])

            seenNode[nodeId - 1] = True
            nodeIds.append(nodeId)
            nodes.append(coord)

            for j in range(3):
                lbound[j] = min(lbound[j], coord[j])
                ubound[j] = max(ubound[j], coord[j])

    line = ReadNonCommentLine(fileHandle)
    assert (line == "$EndNodes")

    nodes = utils.KeyedSort(nodeIds, nodes)
    bound = bounds.BoundingBox(lbound, ubound)
    indices = bound.UsedDimIndices()
    dim = len(indices)
    if dim < 3:
        nodes = [[coord[index] for index in indices] for coord in nodes]

    mesh = meshes.Mesh(dim)
    mesh.AddNodeCoords(nodes)

    # read the Elements section
    line = ReadNonCommentLine(fileHandle)
    assert (line == "$Elements")

    line = ReadNonCommentLine(fileHandle)
    lineSplit = line.split()
    numEntities = int(lineSplit[0])
    numElems = int(lineSplit[1])
    for i in range(numEntities):
        line = ReadNonCommentLine(fileHandle)
        lineSplit = line.split()

        entityDim = int(lineSplit[0])
        entityTag = int(lineSplit[1])
        elementType = int(lineSplit[2])
        elemsInBlock = int(lineSplit[3])

        type = GmshElementType(gmshElementTypeId=elementType)

        for j in range(elemsInBlock):
            line = ReadNonCommentLine(fileHandle)
            lineSplit = line.split()
            assert (len(lineSplit) == 1 + type.GetNodeCount())

            ids = [entityTag, int(lineSplit[0])]
            nodes = FromGmshNodeOrder(
                [int(node) - 1 for node in lineSplit[1:]], type)
            element = elements.Element(nodes, ids)

            if type.GetDim() == dim - 1:
                mesh.AddSurfaceElement(element)
            elif type.GetDim() == dim:
                mesh.AddVolumeElement(element)
            else:
                debug.deprint("Warning: Element of type " + str(type) +
                              " encountered in " + str(dim) + " dimensions")

    line = ReadNonCommentLine(fileHandle)
    assert (line == "$EndElements")

    return mesh
Exemple #21
0
def ModelPvtuToVtu(pvtu):
  """
  Convert a parallel vtu to a serial vtu but without any fields. Does nothing
  (except generate a copy) if the supplied vtu is already a serial vtu.
  """
  
  # Step 1: Extract the ghost levels, and check that we have a parallel vtu
  
  result = vtu()
  ghostLevel = pvtu.ugrid.GetCellData().GetArray("vtkGhostLevels")
  if ghostLevel is None:
    # We have a serial vtu
    debug.deprint("Warning: input file contains no vtkGhostLevels")
    ghostLevel = [0 for i in range(pvtu.ugrid.GetNumberOfCells())]
  else:
    # We have a parallel vtu
    ghostLevel = [ghostLevel.GetValue(i) for i in range(ghostLevel.GetNumberOfComponents() * ghostLevel.GetNumberOfTuples())]
  
  # Step 2: Collect the non-ghost cell IDs
  
  debug.dprint("Input cells: " + str(pvtu.ugrid.GetNumberOfCells()))
  
  cellIds = []
  keepCell = [False for i in range(pvtu.ugrid.GetNumberOfCells())]
  oldCellIdToNew = [None for i in range(pvtu.ugrid.GetNumberOfCells())]
  
  # Collect the new non-ghost cell IDs and generate the cell renumbering map
  index = 0
  for i, level in enumerate(ghostLevel):
    if calc.AlmostEquals(level, 0.0):
      cellIds.append(i)
      keepCell[i] = True
      oldCellIdToNew[i] = index
      index += 1
      
  debug.dprint("Non-ghost cells: " + str(len(cellIds)))
  
  # Step 3: Collect the non-ghost node IDs
  
  debug.dprint("Input points: " + str(pvtu.ugrid.GetNumberOfPoints()))
 
  keepNode = [False for i in range(pvtu.ugrid.GetNumberOfPoints())]
  
  # Find a list of candidate non-ghost node IDs, based on nodes attached to
  # non-ghost cells
  keepNodeCount = 0
  for cellId in cellIds:
    cellNodeIds = pvtu.ugrid.GetCell(cellId).GetPointIds()
    cellNodeIds = [cellNodeIds.GetId(i) for i in range(cellNodeIds.GetNumberOfIds())]
    keepNodeCount += len(cellNodeIds)
    for nodeId in cellNodeIds:
      keepNode[nodeId] = True
      
  uniqueKeepNodeCount = keepNode.count(True)
  debug.dprint("Non-ghost nodes (pass 1): " + str(uniqueKeepNodeCount))
  if uniqueKeepNodeCount==keepNodeCount:
    debug.dprint("Assuming pvtu is discontinuous")
    # we're keeping all non-ghost nodes:
    nodeIds = [i for i in range(pvtu.ugrid.GetNumberOfPoints()) if keepNode[i]]
    oldNodeIdToNew = numpy.array([None]*pvtu.ugrid.GetNumberOfPoints())
    oldNodeIdToNew[nodeIds] = range(keepNodeCount)
  else:
    # for the CG case we still have duplicate nodes that need to be removed
    oldNodeIdToNew, nodeIds = PvtuToVtuRemoveDuplicateNodes(pvtu, keepNode)

  # Step 4: Generate the new locations
  locations = pvtu.GetLocations()
  locations = numpy.array([locations[i] for i in nodeIds])
  points = vtk.vtkPoints()
  points.SetDataTypeToDouble()
  for location in locations:
    points.InsertNextPoint(location)
  result.ugrid.SetPoints(points)

  # Step 5: Generate the new cells
  for cellId in cellIds:
    cell = pvtu.ugrid.GetCell(cellId)
    cellNodeIds = cell.GetPointIds()
    cellNodeIds = [cellNodeIds.GetId(i) for i in range(cellNodeIds.GetNumberOfIds())]
    idList = vtk.vtkIdList()
    for nodeId in cellNodeIds:
      oldNodeId = nodeId
      nodeId = oldNodeIdToNew[nodeId]
      assert(not nodeId is None)
      assert(nodeId >= 0)
      assert(nodeId <= len(nodeIds))
      idList.InsertNextId(nodeId)
    result.ugrid.InsertNextCell(cell.GetCellType(), idList)

  return result, oldNodeIdToNew, oldCellIdToNew
Exemple #22
0
"""

import datetime
import math
import os
import unittest

import fluidity.diagnostics.debug as debug

from scipy.version import version as SciPyVersion

if tuple(int(x) for x in SciPyVersion.split('.')) < (0, 9, 0):
  try:
    import Scientific.IO.NetCDF as netcdf
  except:
    debug.deprint("Warning: Your SciPy version is too old (<0.9.0) and \nthe Scientific.IO.NetCDF module failed to import")
else:
  try:
    import scipy.io.netcdf as netcdf
  except:
    debug.deprint("Warning: Failed to import scipy.io.netcdf module")




import fluidity.diagnostics.calc as calc
import fluidity.diagnostics.filehandling as filehandling
import fluidity.diagnostics.structured_fields as structured_fields

class Era15:
  """
import os
import subprocess
import tempfile
import unittest

try:
  import numpy
except ImportError:
  debug.deprint("Warning: Failed to import numpy module")

import fluidity.diagnostics.debug as debug

try:
  from fluidity_tools import *
except:
  debug.deprint("Warning: Failed to import fluidity_tools module")
  
import fluidity.diagnostics.calc as calc
import fluidity.diagnostics.filehandling as filehandling
import fluidity.diagnostics.utils as utils
  
def FluidityBinary(binaries = ["dfluidity-debug", "dfluidity", "fluidity-debug", "fluidity"]):
  """
  Return the command used to call Fluidity
  """

  binary = None
  
  for possibleBinary in binaries:
    process = subprocess.Popen(["which", possibleBinary], stdout = subprocess.PIPE, stderr = subprocess.PIPE)
    process.wait()
Exemple #24
0
def ReadBinaryMshV2(fileHandle, dataSize):
    if dataSize == 4:
        realFormat = "f"
    elif dataSize == 8:
        realFormat = "d"
    else:
        raise Exception("Unrecognised real size " + str(dataSize))

    swap = ByteSwap(fileHandle)

    line = ReadNonCommentLine(fileHandle)
    assert (line == "$EndMeshFormat")

    # Read the Nodes section (no PhysicalNames section in binary)
    line = ReadNonCommentLine(fileHandle)
    assert (line == "$Nodes")

    # number-of-nodes
    # node-number x-coord y-coord z-coord
    # ...

    line = ReadNonCommentLine(fileHandle)
    nNodes = int(line)
    # Assume dense node IDs, but not necessarily ordered
    seenNode = [False for i in range(nNodes)]
    nodeIds = []
    nodes = []
    lbound = [calc.Inf() for i in range(3)]
    ubound = [-calc.Inf() for i in range(3)]
    for i in range(nNodes):
        iArr = array.array("i")
        rArr = array.array(realFormat)
        iArr.fromfile(fileHandle, 1)
        rArr.fromfile(fileHandle, 3)
        if swap:
            iArr.byteswap()
            rArr.byteswap()
        nodeId = iArr[0]
        coord = rArr
        assert (nodeId > 0)
        assert (not seenNode[nodeId - 1])
        seenNode[nodeId - 1] = True
        nodeIds.append(nodeId)
        nodes.append(coord)
        for j in range(3):
            lbound[j] = min(lbound[j], coord[j])
            ubound[j] = max(ubound[j], coord[j])

    line = ReadNonCommentLine(fileHandle)
    assert (line == "$EndNodes")

    nodes = utils.KeyedSort(nodeIds, nodes)
    bound = bounds.BoundingBox(lbound, ubound)
    indices = bound.UsedDimIndices()
    dim = len(indices)
    if dim < 3:
        nodes = [[coord[index] for index in indices] for coord in nodes]

    mesh = meshes.Mesh(dim)
    mesh.AddNodeCoords(nodes)

    # Read the Elements section
    line = ReadNonCommentLine(fileHandle)
    assert (line == "$Elements")

    # number-of-elements
    # element-header-binary
    # element-binary
    # ...

    # where element-header-binary is: elm-type num-elm num-tags
    # where element-binary is:
    #   num-elm * (4 + num-tags*4 + node-num*4)
    # node-num physical-tag elementary-tag node-nums ...

    line = ReadNonCommentLine(fileHandle)
    nEles = int(line)
    i = 0
    while i < nEles:
        iArr = array.array("i")
        iArr.fromfile(fileHandle, 3)
        if swap:
            iArr.byteswap()
        typeId = iArr[0]
        nSubEles = iArr[1]
        nIds = iArr[2]

        type = GmshElementType(gmshElementTypeId=typeId)

        for j in range(nSubEles):
            iArr = array.array("i")
            iArr.fromfile(fileHandle, 1 + nIds + type.GetNodeCount())
            if swap:
                iArr.byteswap()
            eleId = iArr[0]
            assert (eleId > 0)
            ids = iArr[1:1 + nIds]
            nodes = FromGmshNodeOrder(
                utils.OffsetList(iArr[-type.GetNodeCount():], -1), type)

            element = elements.Element(nodes, ids)

            if type.GetDim() == dim - 1:
                mesh.AddSurfaceElement(element)
            elif type.GetDim() == dim:
                mesh.AddVolumeElement(element)
            else:
                debug.deprint("Warning: Element of type " + str(type) +
                              " encountered in " + str(dim) + " dimensions")

        i += nSubEles
    assert (i == nEles)

    line = ReadNonCommentLine(fileHandle)
    assert (line == "$EndElements")

    return mesh
Exemple #25
0
"""
Some mathematical routines
"""

from __future__ import print_function

import copy
import math
import unittest

import fluidity.diagnostics.debug as debug

try:
  import numpy
except ImportError:
  debug.deprint("Warning: Failed to import numpy module")
try:
  import scipy.linalg
except ImportError:
  debug.deprint("Warning: Failed to import scipy.linalg module")
try:
  import scipy.stats
except ImportError:
  debug.deprint("Warning: Failed to import scipy.stats module")
try:
  import vtk
except ImportError:
  debug.deprint("Warning: Failed to import vtk module")

import fluidity.diagnostics.optimise as optimise
import fluidity.diagnostics.utils as utils
Exemple #26
0
def WriteTriangle(mesh, baseName):
  """
  Write triangle files with the given base name
  """
    
  def FileFooter():
    return "# Created by triangletools.WriteTriangle\n" + \
           "# Command: " + " ".join(sys.argv) + "\n" + \
           "# " + str(time.ctime()) + "\n"

  debug.dprint("Writing triangle mesh with base name " + baseName)
    
  # Write the .node file
  debug.dprint("Writing .node file")
  
  nodeHandle = open(baseName + ".node", "w")
  
  # Write the meta data
  nodeHandle.write(utils.FormLine([mesh.NodeCount(), mesh.GetDim(), 0, 0]))
  
  # Write the nodes
  for i in range(mesh.NodeCount()):
    nodeHandle.write(utils.FormLine([i + 1, mesh.GetNodeCoord(i)]))
  nodeHandle.write(FileFooter())
  nodeHandle.close()
    
  if mesh.GetDim() == 1:
    # Write the .bound file
    debug.dprint("Writing .bound file")
    
    boundHandle = open(baseName + ".bound", "w")
    
    # Write the meta data
    nBoundIds = 0
    for i in range(mesh.SurfaceElementCount()):
      if i == 0:
        nBoundIds = len(mesh.GetSurfaceElement(i).GetIds())
      else:
        assert(nBoundIds == len(mesh.GetSurfaceElement(i).GetIds()))
    boundHandle.write(utils.FormLine([mesh.SurfaceElementCount(), nBoundIds]))
    
    # Write the bounds
    for i in range(mesh.SurfaceElementCount()):
      boundHandle.write(utils.FormLine([i + 1, utils.OffsetList(mesh.GetSurfaceElement(i).GetNodes(), 1), mesh.GetSurfaceElement(i).GetIds()]))
    boundHandle.write(FileFooter())
    boundHandle.close()
  elif mesh.GetDim() == 2:
    # Write the .edge file
    debug.dprint("Writing .edge file")
    
    edgeHandle = open(baseName + ".edge", "w")
    
    # Write the meta data
    nEdgeIds = 0
    for i in range(mesh.SurfaceElementCount()):
      if i == 0:
        nEdgeIds = len(mesh.GetSurfaceElement(i).GetIds())
      else:
        assert(nEdgeIds == len(mesh.GetSurfaceElement(i).GetIds()))
    edgeHandle.write(utils.FormLine([mesh.SurfaceElementCount(), nEdgeIds]))
    
    # Write the edges
    for i in range(mesh.SurfaceElementCount()):
      edgeHandle.write(utils.FormLine([i + 1, utils.OffsetList(mesh.GetSurfaceElement(i).GetNodes(), 1), mesh.GetSurfaceElement(i).GetIds()]))
    edgeHandle.write(FileFooter())
    edgeHandle.close()
  elif mesh.GetDim() == 3:
    # Write the .face file
    debug.dprint("Writing .face file")
    
    faceHandle = open(baseName + ".face", "w")
    
    # Write the meta data
    nFaceIds = 0
    for i in range(mesh.SurfaceElementCount()):
      if i == 0:
        nFaceIds = len(mesh.GetSurfaceElement(i).GetIds())
      else:
        assert(nFaceIds == len(mesh.GetSurfaceElement(i).GetIds()))
    faceHandle.write(utils.FormLine([mesh.SurfaceElementCount(), nFaceIds]))
    
    # Write the faces
    for i in range(mesh.SurfaceElementCount()):
      faceHandle.write(utils.FormLine([i + 1, utils.OffsetList(mesh.GetSurfaceElement(i).GetNodes(), 1), mesh.GetSurfaceElement(i).GetIds()]))
    faceHandle.write(FileFooter())
    faceHandle.close()
    
  # Write the .ele file
  debug.dprint("Writing .ele file")
  
  eleHandle = open(baseName + ".ele", "w")
  
  # Write the meta data
  nNodesPerEle = 0
  nEleIds = 0
  for i in range(mesh.VolumeElementCount()):
    if i == 0:
      nEleIds = len(mesh.GetVolumeElement(i).GetIds())
      nNodesPerEle = mesh.GetVolumeElement(i).GetLoc()
    else:
      assert(nEleIds == len(mesh.GetVolumeElement(i).GetIds()))
      assert(nNodesPerEle == mesh.GetVolumeElement(i).GetLoc())
  eleHandle.write(utils.FormLine([mesh.VolumeElementCount(), nNodesPerEle, nEleIds]))
  
  # Write the eles
  for i in range(mesh.VolumeElementCount()):
    # Note: Triangle mesh indexes nodes from 1, Mesh s index nodes from 0
    eleHandle.write(utils.FormLine([i + 1, utils.OffsetList(mesh.GetVolumeElement(i).GetNodes(), 1), mesh.GetVolumeElement(i).GetIds()]))
  eleHandle.write(FileFooter())
  eleHandle.close()
  
  
  halos = mesh.GetHalos()
  if halos.HaloCount() > 0:
    # Write the .halo file
    debug.dprint("Writing .halo file")
    
    if mesh_halos.HaloIOSupport():
      mesh_halos.WriteHalos(halos, baseName + ".halo")
    else:
      debug.deprint("Warning: No .halo I/O support")

  debug.dprint("Finished writing triangle file")
    
  return
Exemple #27
0
def ReadBinaryMshV4(fileHandle, dataSize):
    if dataSize == 4:
        sizeFormat = "i"
    elif dataSize == 8:
        sizeFormat = "l"
    else:
        raise Exception("Unrecognised size_t size " + str(dataSize))

    swap = ByteSwap(fileHandle)

    line = ReadNonCommentLine(fileHandle)
    assert (line == "$EndMeshFormat")

    # skip ahead to Nodes section (possibly bypassing Entities)
    while line != "$Nodes":
        line = ReadNonCommentLine(fileHandle)
    assert (line == "$Nodes")

    # numEntityBlock(size_t) numNodes(size_t)
    #   minNodeTag(size_t) maxNodeTag(size_t)
    #
    # entityDim(int) entityTag(int) parametric(int) numNodes(size_t)
    #
    #  nodeTag(size_t) ...
    #  x(double) y(double) z(double) ...
    # ...
    sArr = array.array(sizeFormat)
    sArr.fromfile(fileHandle, 4)
    if swap:
        sArr.byteswap()
    numBlocks = sArr[0]
    numNodes = sArr[1]
    # assume dense nodes (we can check using the min/max id fields)
    seenNode = [False] * numNodes
    nodeIds = []
    nodes = []
    lbound = [calc.Inf() for i in range(3)]
    ubound = [-calc.Inf() for i in range(3)]
    for b in range(numBlocks):
        iArr = array.array("i")
        sArr = array.array(sizeFormat)
        iArr.fromfile(fileHandle, 3)
        sArr.fromfile(fileHandle, 1)
        if swap:
            iArr.byteswap()
            sArr.byteswap()

        subNodes = sArr[0]
        tagArr = array.array(sizeFormat)
        tagArr.fromfile(fileHandle, subNodes)
        if swap:
            tagArr.byteswap()

        for i in range(subNodes):
            rArr = array.array("d")
            rArr.fromfile(fileHandle, 3)
            if swap:
                rArr.byteswap()

            nodeId = tagArr[i]
            coord = rArr
            assert (nodeId > 0)
            assert (not seenNode[nodeId - 1])
            seenNode[nodeId - 1] = True
            nodeIds.append(nodeId)
            nodes.append(coord)
            for j in range(3):
                lbound[j] = min(lbound[j], coord[j])
                ubound[j] = max(ubound[j], coord[j])

    line = ReadNonCommentLine(fileHandle)
    assert (line == "$EndNodes")

    nodes = utils.KeyedSort(nodeIds, nodes)
    bound = bounds.BoundingBox(lbound, ubound)
    indices = bound.UsedDimIndices()
    dim = len(indices)
    if dim < 3:
        nodes = [[coord[index] for index in indices] for coord in nodes]

    mesh = meshes.Mesh(dim)
    mesh.AddNodeCoords(nodes)

    # read the Elements section
    line = ReadNonCommentLine(fileHandle)
    assert (line == "$Elements")

    # numEntityBlocks(size_t) numElements(size_t)
    #   minElementTag(size_t) maxElementTag(size_t)
    #
    # entityDim(int) entityTag(int) elementType(int) numElems(size_t)
    #   elementTag(size_t) nodeTag(size_t) ...
    #   ...
    # ...
    sArr = array.array(sizeFormat)
    sArr.fromfile(fileHandle, 4)
    if swap:
        sArr.byteswap()
    numEntities = sArr[0]
    numElems = sArr[1]
    for i in range(numEntities):
        iArr = array.array("i")
        sArr = array.array(sizeFormat)
        iArr.fromfile(fileHandle, 3)
        sArr.fromfile(fileHandle, 1)

        if swap:
            iArr.byteswap()
            sArr.byteswap()

        entityDim = iArr[0]
        entityTag = iArr[1]
        elementType = iArr[2]
        elemsInBlock = sArr[0]

        type = GmshElementType(gmshElementTypeId=elementType)

        for j in range(elemsInBlock):
            sArr = array.array(sizeFormat)
            sArr.fromfile(1 + type.GetNodeCount())
            if swap:
                sArr.byteswap()

            ids = [entityTag, sArr[0]]
            nodes = FromGmshNodeOrder(utils.OffsetList(sArr[1:], -1), type)
            element = elements.Element(nodes, ids)

            if type.GetDim() == dim - 1:
                mesh.AddSurfaceElement(element)
            elif type.GetDim() == dim:
                mesh.AddVolumeElement(element)
            else:
                debug.deprint("Warning: Element of type " + str(type) +
                              " encountered in " + str(dim) + " dimensions")

        line = ReadNonCommentLine(fileHandle)
        assert (line == "$EndElements")

        return mesh
Exemple #28
0
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

"""
Finite element element classes
"""

import copy
import unittest

import fluidity.diagnostics.debug as debug

try:
  import numpy
except ImportError:
  debug.deprint("Warning: Failed to import numpy module")
try:
  import scipy
except ImportError:
  debug.deprint("Warning: Failed to import scipy module")
try:
  import scipy.interpolate
except ImportError:
  debug.deprint("Warning: Failed to import scipy.interpolate module")

import fluidity.diagnostics.calc as calc
import fluidity.diagnostics.events as events
import fluidity.diagnostics.optimise as optimise
import fluidity.diagnostics.utils as utils

def NumpySupport():
Exemple #29
0
def ReadAsciiMshV2(fileHandle):
    line = ReadNonCommentLine(fileHandle)
    assert (line == "$EndMeshFormat")

    # Read the Nodes section
    line = ReadNonCommentLine(fileHandle)
    assert (line == "$Nodes")

    line = ReadNonCommentLine(fileHandle)
    nNodes = int(line)
    # Assume dense node IDs, but not necessarily ordered
    seenNode = [False for i in range(nNodes)]
    nodeIds = []
    nodes = []
    lbound = [calc.Inf() for i in range(3)]
    ubound = [-calc.Inf() for i in range(3)]
    for i in range(nNodes):
        line = ReadNonCommentLine(fileHandle)
        lineSplit = line.split()
        assert (len(lineSplit) == 4)
        nodeId = int(lineSplit[0])
        coord = [float(comp) for comp in lineSplit[1:]]
        assert (nodeId > 0)
        assert (not seenNode[nodeId - 1])
        seenNode[nodeId - 1] = True
        nodeIds.append(nodeId)
        nodes.append(coord)
        for j in range(3):
            lbound[j] = min(lbound[j], coord[j])
            ubound[j] = max(ubound[j], coord[j])

    line = ReadNonCommentLine(fileHandle)
    assert (line == "$EndNodes")

    nodes = utils.KeyedSort(nodeIds, nodes)
    bound = bounds.BoundingBox(lbound, ubound)
    indices = bound.UsedDimIndices()
    dim = len(indices)
    if dim < 3:
        nodes = [[coord[index] for index in indices] for coord in nodes]

    mesh = meshes.Mesh(dim)
    mesh.AddNodeCoords(nodes)

    # Read the Elements section

    line = ReadNonCommentLine(fileHandle)
    assert (line == "$Elements")

    line = ReadNonCommentLine(fileHandle)
    nEles = int(line)
    for i in range(nEles):
        line = ReadNonCommentLine(fileHandle)
        lineSplit = line.split()
        assert (len(lineSplit) > 3)
        eleId = int(lineSplit[0])
        assert (eleId > 0)
        typeId = int(lineSplit[1])
        nIds = int(lineSplit[2])

        type = GmshElementType(gmshElementTypeId=typeId)
        ids = [int(id) for id in lineSplit[3:3 + nIds]]
        nodes = FromGmshNodeOrder(
            [int(node) - 1 for node in lineSplit[-type.GetNodeCount():]], type)
        element = elements.Element(nodes, ids)
        if type.GetDim() == dim - 1:
            mesh.AddSurfaceElement(element)
        elif type.GetDim() == dim:
            mesh.AddVolumeElement(element)
        else:
            debug.deprint("Warning: Element of type " + str(type) +
                          " encountered in " + str(dim) + " dimensions")

    line = ReadNonCommentLine(fileHandle)
    assert (line == "$EndElements")

    return mesh
Exemple #30
0
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

"""
Finite element mesh classes
"""

import unittest

import fluidity.diagnostics.debug as debug

try:
  import numpy
except:
  debug.deprint("Warning: Failed to import numpy module")

import fluidity.diagnostics.bounds as bounds
import fluidity.diagnostics.calc as calc
import fluidity.diagnostics.elements as elements
import fluidity.diagnostics.events as events
import fluidity.diagnostics.mesh_halos as mesh_halos
import fluidity.diagnostics.optimise as optimise
import fluidity.diagnostics.utils as utils
import fluidity.diagnostics.vtutools as vtktools

try:
  import vtk
except ImportError:
  debug.deprint("Warning: Failed to import vtk module")
        
Exemple #31
0
def ReadMsh(filename):
  """
  Read a Gmsh msh file
  """
      
  def ReadNonCommentLine(fileHandle):
    line = fileHandle.readline()
    while len(line) > 0:
      line = line.strip()
      if len(line) > 0:
        return line
      line = fileHandle.readline()
      
    return line
  
  fileHandle = open(filename, "r")

  basename = filename.split(".")[0]
  hasHalo = filehandling.FileExists(basename + ".halo")
  
  # Read the MeshFormat section
  
  line = ReadNonCommentLine(fileHandle)
  assert(line == "$MeshFormat")
  
  line = ReadNonCommentLine(fileHandle)
  lineSplit = line.split()
  assert(len(lineSplit) == 3)
  version = float(lineSplit[0])
  fileType = int(lineSplit[1])
  dataSize = int(lineSplit[2])  
  if fileType == 1:
    # Binary format
    
    if dataSize == 4:
      realFormat = "f"
    elif dataSize == 8:
      realFormat = "d"
    else:
      raise Exception("Unrecognised real size " + str(dataSize))
      
    iArr = array.array("i")    
    iArr.fromfile(fileHandle, 1)
    if iArr[0] == 1:
      swap = False
    else:
      iArr.byteswap()
      if iArr[0] == 1:
        swap = True
      else:
        raise Exception("Invalid one byte")
    
    line = ReadNonCommentLine(fileHandle)
    assert(line == "$EndMeshFormat")
    
    # Read the Nodes section
    
    line = ReadNonCommentLine(fileHandle)
    assert(line == "$Nodes")    
    
    line = ReadNonCommentLine(fileHandle)
    nNodes = int(line)
    # Assume dense node IDs, but not necessarily ordered
    seenNode = [False for i in range(nNodes)]
    nodeIds = []
    nodes = []
    lbound = [calc.Inf() for i in range(3)]
    ubound = [-calc.Inf() for i in range(3)]
    for i in range(nNodes):
      iArr = array.array("i")
      rArr = array.array(realFormat)
      iArr.fromfile(fileHandle, 1)
      rArr.fromfile(fileHandle, 3)
      if swap:
        iArr.byteswap()
        rArr.byteswap()
      nodeId = iArr[0]
      coord = rArr
      assert(nodeId > 0)
      assert(not seenNode[nodeId - 1])
      seenNode[nodeId - 1] = True
      nodeIds.append(nodeId)
      nodes.append(coord)
      for j in range(3):
        lbound[j] = min(lbound[j], coord[j])
        ubound[j] = max(ubound[j], coord[j])
    
    line = ReadNonCommentLine(fileHandle)
    assert(line == "$EndNodes")
      
    nodes = utils.KeyedSort(nodeIds, nodes)
    bound = bounds.BoundingBox(lbound, ubound)
    indices = bound.UsedDimIndices()
    dim = len(indices)
    if dim < 3:
      nodes = [[coord[index] for index in indices] for coord in nodes]
    
    mesh = meshes.Mesh(dim)
    mesh.AddNodeCoords(nodes)
      
    # Read the Elements section
    
    line = ReadNonCommentLine(fileHandle)
    assert(line == "$Elements")  
    
    line = ReadNonCommentLine(fileHandle)
    nEles = int(line)
    i = 0
    while i < nEles:
      iArr = array.array("i")
      iArr.fromfile(fileHandle, 3)
      if swap:
        iArr.byteswap()
      typeId = iArr[0]
      nSubEles = iArr[1]
      nIds = iArr[2]
      
      type = GmshElementType(gmshElementTypeId = typeId)
      
      for j in range(nSubEles):
        iArr = array.array("i")
        iArr.fromfile(fileHandle, 1 + nIds + type.GetNodeCount())
        if swap:
          iArr.byteswap()
        eleId = iArr[0]
        assert(eleId > 0)
        ids = iArr[1:1 + nIds]
        nodes = FromGmshNodeOrder(utils.OffsetList(iArr[-type.GetNodeCount():], -1), type)
        
        element = elements.Element(nodes, ids)
        
        if type.GetDim() == dim - 1:
          mesh.AddSurfaceElement(element)
        elif type.GetDim() == dim:
          mesh.AddVolumeElement(element)
        else:
          debug.deprint("Warning: Element of type " + str(type) + " encountered in " + str(dim) + " dimensions")
          
      i += nSubEles
    assert(i == nEles)
    
    line = ReadNonCommentLine(fileHandle)
    assert(line == "$EndElements")
  elif fileType == 0:
    # ASCII format
    
    line = ReadNonCommentLine(fileHandle)
    assert(line == "$EndMeshFormat")
    
    # Read the Nodes section
    
    line = ReadNonCommentLine(fileHandle)
    assert(line == "$Nodes")
    
    line = ReadNonCommentLine(fileHandle)
    nNodes = int(line)
    # Assume dense node IDs, but not necessarily ordered
    seenNode = [False for i in range(nNodes)]
    nodeIds = []
    nodes = []
    lbound = [calc.Inf() for i in range(3)]
    ubound = [-calc.Inf() for i in range(3)]
    for i in range(nNodes):
      line = ReadNonCommentLine(fileHandle)
      lineSplit = line.split()
      assert(len(lineSplit) == 4)
      nodeId = int(lineSplit[0])
      coord = [float(comp) for comp in lineSplit[1:]]
      assert(nodeId > 0)
      assert(not seenNode[nodeId - 1])
      seenNode[nodeId - 1] = True
      nodeIds.append(nodeId)
      nodes.append(coord)
      for j in range(3):
        lbound[j] = min(lbound[j], coord[j])
        ubound[j] = max(ubound[j], coord[j])
    
    line = ReadNonCommentLine(fileHandle)
    assert(line == "$EndNodes")
      
    nodes = utils.KeyedSort(nodeIds, nodes)
    bound = bounds.BoundingBox(lbound, ubound)
    indices = bound.UsedDimIndices()
    dim = len(indices)
    if dim < 3:
      nodes = [[coord[index] for index in indices] for coord in nodes]
    
    mesh = meshes.Mesh(dim)
    mesh.AddNodeCoords(nodes)
    
    # Read the Elements section
    
    line = ReadNonCommentLine(fileHandle)
    assert(line == "$Elements")  
    
    line = ReadNonCommentLine(fileHandle)
    nEles = int(line)
    for i in range(nEles):
      line = ReadNonCommentLine(fileHandle)
      lineSplit = line.split()
      assert(len(lineSplit) > 3)
      eleId = int(lineSplit[0])
      assert(eleId > 0)
      typeId = int(lineSplit[1])
      nIds = int(lineSplit[2])
      
      type = GmshElementType(gmshElementTypeId = typeId)
      ids = [int(id) for id in lineSplit[3:3 + nIds]]
      nodes = FromGmshNodeOrder([int(node) - 1 for node in lineSplit[-type.GetNodeCount():]], type)
      element = elements.Element(nodes, ids)
      if type.GetDim() == dim - 1:
        mesh.AddSurfaceElement(element)
      elif type.GetDim() == dim:
        mesh.AddVolumeElement(element)
      else:
        debug.deprint("Warning: Element of type " + str(type) + " encountered in " + str(dim) + " dimensions")
    
    line = ReadNonCommentLine(fileHandle)
    assert(line == "$EndElements")
    
    # Ignore all remaining sections
  else:
    raise Exception("File type " + str(fileType) + " not recognised")
  
  fileHandle.close()

  if hasHalo:
    # Read the .halo file
    debug.dprint("Reading .halo file")

    if mesh_halos.HaloIOSupport():
      halos = mesh_halos.ReadHalos(basename + ".halo")
      mesh.SetHalos(halos)
    else:
      debug.deprint("Warning: No .halo I/O support")
  
  return mesh
Exemple #32
0
"""
VTK tools
"""

import copy
import math
import sys
import unittest

import fluidity.diagnostics.debug as debug

try:
  import vtk
except ImportError:
  debug.deprint("Warning: Failed to import vtk module")

try:
  from vtktools import *
except ImportError:
  debug.deprint("Warning: Failed to import vtktools module")
  
import fluidity.diagnostics.bounds as bounds
import fluidity.diagnostics.calc as calc
import fluidity.diagnostics.elements as elements
import fluidity.diagnostics.optimise as optimise
import fluidity.diagnostics.simplices as simplices
import fluidity.diagnostics.utils as utils

def VtkSupport():
  return "vtk" in globals()
Exemple #33
0
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
"""
ERA data set handling routines
"""

import datetime
import math
import os
import unittest

import fluidity.diagnostics.debug as debug

try:
    import Scientific.IO.NetCDF as netcdf
except:
    debug.deprint("Warning: Failed to import Scientific.IO.NetCDF module")

import fluidity.diagnostics.calc as calc
import fluidity.diagnostics.filehandling as filehandling
import fluidity.diagnostics.structured_fields as structured_fields


class Era15:
    """
  Class for handling ERA15 10m wind data
  """

    # Zero point for time data
    _epoch = datetime.datetime(1900, 1, 1, 0, 0, 0)

    def __init__(self,
"""

import os
import tempfile
import unittest

import fluidity.diagnostics.debug as debug

try:
  import xml.dom.ext
except ImportError:
  pass
try:
  import xml.dom.minidom
except ImportError:
  debug.deprint("Warning: Failed to import xml.dom.minidom module")

import fluidity.diagnostics.filehandling as filehandling
import fluidity.diagnostics.optimise as optimise
import fluidity.diagnostics.utils as utils

def XmlSupport():
  try:
    import xml.dom.minidom
    return True
  except ImportError:
    return False

def XmlExtSupport():
  try:
    import xml.dom.ext
Exemple #35
0
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

"""
Finite element element classes
"""

import copy
import unittest

import fluidity.diagnostics.debug as debug

try:
  import numpy
except ImportError:
  debug.deprint("Warning: Failed to import numpy module")
try:
  import scipy
except ImportError:
  debug.deprint("Warning: Failed to import scipy module")
try:
  import scipy.interpolate
except ImportError:
  debug.deprint("Warning: Failed to import scipy.interpolate module")

import fluidity.diagnostics.calc as calc
import fluidity.diagnostics.events as events
import fluidity.diagnostics.optimise as optimise
import fluidity.diagnostics.utils as utils

def NumpySupport():
Exemple #36
0
Graph plotting utilities
"""

import os
import tempfile
import unittest

import fluidity.diagnostics.debug as debug
import fluidity.diagnostics.gui as gui
import fluidity.diagnostics.utils as utils

if not gui.GuiDisabledByEnvironment():
    try:
        import matplotlib
    except ImportError:
        debug.deprint("Warning: Failed to import matplotlib module")


def MatplotlibSupport():
    return "matplotlib" in globals()


if MatplotlibSupport():
    matplotlib.use("Cairo")
    try:
        import matplotlib.backends.backend_gtk
    except:
        debug.deprint("Warning: Failed to import matplotlib.backends.backend_gtk module")

if not gui.GuiDisabledByEnvironment():
    try:
Exemple #37
0
Graph plotting utilities
"""

import os
import tempfile
import unittest

import fluidity.diagnostics.debug as debug
import fluidity.diagnostics.gui as gui
import fluidity.diagnostics.utils as utils

if not gui.GuiDisabledByEnvironment():
    try:
        import matplotlib
    except ImportError:
        debug.deprint("Warning: Failed to import matplotlib module")


def MatplotlibSupport():
    return "matplotlib" in globals()


if MatplotlibSupport():
    matplotlib.use("Cairo")
    try:
        import matplotlib.backends.backend_gtk
    except:
        debug.deprint(
            "Warning: Failed to import matplotlib.backends.backend_gtk module")

if not gui.GuiDisabledByEnvironment():
def ReadMsh(filename):
  """
  Read a Gmsh msh file
  """
      
  def ReadNonCommentLine(fileHandle):
    line = fileHandle.readline()
    while len(line) > 0:
      line = line.strip()
      if len(line) > 0:
        return line
      line = fileHandle.readline()
      
    return line
  
  fileHandle = open(filename, "r")
  
  # Read the MeshFormat section
  
  line = ReadNonCommentLine(fileHandle)
  assert(line == "$MeshFormat")
  
  line = ReadNonCommentLine(fileHandle)
  lineSplit = line.split()
  assert(len(lineSplit) == 3)
  version = float(lineSplit[0])
  fileType = int(lineSplit[1])
  dataSize = int(lineSplit[2])  
  if fileType == 1:
    # Binary format
    
    if dataSize == 4:
      realFormat = "f"
    elif dataSize == 8:
      realFormat = "d"
    else:
      raise Exception("Unrecognised real size " + str(dataSize))
      
    iArr = array.array("i")    
    iArr.fromfile(fileHandle, 1)
    if iArr[0] == 1:
      swap = False
    else:
      iArr.byteswap()
      if iArr[0] == 1:
        swap = True
      else:
        raise Exception("Invalid one byte")
    
    line = ReadNonCommentLine(fileHandle)
    assert(line == "$EndMeshFormat")
    
    # Read the Nodes section
    
    line = ReadNonCommentLine(fileHandle)
    assert(line == "$Nodes")    
    
    line = ReadNonCommentLine(fileHandle)
    nNodes = int(line)
    # Assume dense node IDs, but not necessarily ordered
    seenNode = [False for i in range(nNodes)]
    nodeIds = []
    nodes = []
    lbound = [calc.Inf() for i in range(3)]
    ubound = [-calc.Inf() for i in range(3)]
    for i in range(nNodes):
      iArr = array.array("i")
      rArr = array.array(realFormat)
      iArr.fromfile(fileHandle, 1)
      rArr.fromfile(fileHandle, 3)
      if swap:
        iArr.byteswap()
        rArr.byteswap()
      nodeId = iArr[0]
      coord = rArr
      assert(nodeId > 0)
      assert(not seenNode[nodeId - 1])
      seenNode[nodeId - 1] = True
      nodeIds.append(nodeId)
      nodes.append(coord)
      for j in range(3):
        lbound[j] = min(lbound[j], coord[j])
        ubound[j] = max(ubound[j], coord[j])
    
    line = ReadNonCommentLine(fileHandle)
    assert(line == "$EndNodes")
      
    nodes = utils.KeyedSort(nodeIds, nodes)
    bound = bounds.BoundingBox(lbound, ubound)
    indices = bound.UsedDimIndices()
    dim = len(indices)
    if dim < 3:
      nodes = [[coord[index] for index in indices] for coord in nodes]
    
    mesh = meshes.Mesh(dim)
    mesh.AddNodeCoords(nodes)
      
    # Read the Elements section
    
    line = ReadNonCommentLine(fileHandle)
    assert(line == "$Elements")  
    
    line = ReadNonCommentLine(fileHandle)
    nEles = int(line)
    i = 0
    while i < nEles:
      iArr = array.array("i")
      iArr.fromfile(fileHandle, 3)
      if swap:
        iArr.byteswap()
      typeId = iArr[0]
      nSubEles = iArr[1]
      assert(nSubEles > 0)  # Helps avoid inf looping
      nIds = iArr[2]
      
      type = GmshElementType(gmshElementTypeId = typeId)
      
      for j in range(nSubEles):
        iArr = array.array("i")
        iArr.fromfile(fileHandle, 1 + nIds + type.GetNodeCount())
        if swap:
          iArr.byteswap()
        eleId = iArr[0]
        assert(eleId > 0)
        ids = iArr[1:1 + nIds]
        nodes = FromGmshNodeOrder(utils.OffsetList(iArr[-type.GetNodeCount():], -1), type)
        
        element = elements.Element(nodes, ids)
        
        if type.GetDim() == dim - 1:
          mesh.AddSurfaceElement(element)
        elif type.GetDim() == dim:
          mesh.AddVolumeElement(element)
        else:
          debug.deprint("Warning: Element of type " + str(type) + " encountered in " + str(dim) + " dimensions")
          
      i += nSubEles
    assert(i == nEles)
    
    line = ReadNonCommentLine(fileHandle)
    assert(line == "$EndElements")
  elif fileType == 0:
    # ASCII format
    
    line = ReadNonCommentLine(fileHandle)
    assert(line == "$EndMeshFormat")
    
    # Read the Nodes section
    
    line = ReadNonCommentLine(fileHandle)
    assert(line == "$Nodes")
    
    line = ReadNonCommentLine(fileHandle)
    nNodes = int(line)
    # Assume dense node IDs, but not necessarily ordered
    seenNode = [False for i in range(nNodes)]
    nodeIds = []
    nodes = []
    lbound = [calc.Inf() for i in range(3)]
    ubound = [-calc.Inf() for i in range(3)]
    for i in range(nNodes):
      line = ReadNonCommentLine(fileHandle)
      lineSplit = line.split()
      assert(len(lineSplit) == 4)
      nodeId = int(lineSplit[0])
      coord = [float(comp) for comp in lineSplit[1:]]
      assert(nodeId > 0)
      assert(not seenNode[nodeId - 1])
      seenNode[nodeId - 1] = True
      nodeIds.append(nodeId)
      nodes.append(coord)
      for j in range(3):
        lbound[j] = min(lbound[j], coord[j])
        ubound[j] = max(ubound[j], coord[j])
    
    line = ReadNonCommentLine(fileHandle)
    assert(line == "$EndNodes")
      
    nodes = utils.KeyedSort(nodeIds, nodes)
    bound = bounds.BoundingBox(lbound, ubound)
    indices = bound.UsedDimIndices()
    dim = len(indices)
    if dim < 3:
      nodes = [[coord[index] for index in indices] for coord in nodes]
    
    mesh = meshes.Mesh(dim)
    mesh.AddNodeCoords(nodes)
    
    # Read the Elements section
    
    line = ReadNonCommentLine(fileHandle)
    assert(line == "$Elements")  
    
    line = ReadNonCommentLine(fileHandle)
    nEles = int(line)
    for i in range(nEles):
      line = ReadNonCommentLine(fileHandle)
      lineSplit = line.split()
      assert(len(lineSplit) > 3)
      eleId = int(lineSplit[0])
      assert(eleId > 0)
      typeId = int(lineSplit[1])
      nIds = int(lineSplit[3])
      
      type = GmshElementType(gmshElementTypeId = typeId)
      ids = [int(id) for id in lineSplit[3:3 + nIds]]
      nodes = FromGmshNodeOrder([int(node) - 1 for node in lineSplit[-type.GetNodeCount():]], type)
      element = elements.Element(nodes, ids)
      
      if type.GetDim() == dim - 1:
        mesh.AddSurfaceElement(element)
      elif type.GetDim() == dim:
        mesh.AddVolumeElement(element)
      else:
        debug.deprint("Warning: Element of type " + str(type) + " encountered in " + str(dim) + " dimensions")
    
    line = ReadNonCommentLine(fileHandle)
    assert(line == "$EndElements")
    
    # Ignore all remaining sections
  else:
    raise Exception("File type " + str(fileType) + " not recognised")
  
  fileHandle.close()
  
  return mesh
def ReadGid(filename):
  """
  Read a GiD file with the given filename, and return it as a mesh
  """
  
  debug.dprint("Reading GiD mesh with filename " + filename)
  
  fileHandle = open(filename, "r")
  
  # Read the header
  header = fileHandle.readline()
  lineSplit = header.split()
  assert(lineSplit[0] == "MESH")
  dimension = None
  elemType = None
  nnode = None
  for i, word in enumerate(lineSplit[:len(lineSplit) - 1]):
    if word == "dimension":
      dimension = int(lineSplit[i + 1])
      assert(dimension >= 0)
    elif word == "ElemType":
      elemType = lineSplit[i + 1]
    elif word == "Nnode":
      nnode = int(lineSplit[i + 1])
      assert(nnode >= 0)
  assert(not dimension is None)
  assert(not elemType is None)
  assert(not nnode is None)
  
  debug.dprint("Dimension = " + str(dimension))
  debug.dprint("Element type = " + elemType)
  debug.dprint("Element nodes = " + str(nnode))
  
  # Read the nodes
  nodeCoords = []
  line = fileHandle.readline()
  index = 0
  while len(line) > 0:
    if line.strip() == "Coordinates":
      line = fileHandle.readline()
      while not line.strip() == "end coordinates":
        assert(len(line) > 0)
        index += 1
        lineSplit = line.split()
        assert(len(lineSplit) == 1 + dimension)
        assert(int(lineSplit[0]) == index)
        nodeCoords.append([float(coord) for coord in lineSplit[1:]])
        line = fileHandle.readline()
      break
    line = fileHandle.readline()
  debug.dprint("Nodes: " + str(index))
  
  # Check for unused dimensions
  lbound = [calc.Inf() for i in range(dimension)]
  ubound = [-calc.Inf() for i in range(dimension)]
  for nodeCoord in nodeCoords:
    for i, val in enumerate(nodeCoord):
      lbound[i] = min(lbound[i], val)
      ubound[i] = max(ubound[i], val)
  boundingBox = bounds.BoundingBox(lbound, ubound)
  actualDimension = boundingBox.UsedDim()
  if not dimension == actualDimension:
    debug.deprint("Dimension suggested by bounds = " + str(actualDimension))
    debug.deprint("Warning: Header dimension inconsistent with bounds")
    dimension = actualDimension
    coordMask = boundingBox.UsedDimCoordMask()
    nodeCoords = [utils.MaskList(nodeCoord, coordMask) for nodeCoord in nodeCoords]
  
  mesh = meshes.Mesh(dimension)
  mesh.AddNodeCoords(nodeCoords)
  
  fileHandle.seek(0)
  # Read the volume elements
  line = fileHandle.readline()
  index = 0
  while len(line) > 0:
    if line.strip() == "Elements":
      line = fileHandle.readline()
      while not line.strip() == "end elements":
        assert(len(line) > 0)
        index += 1
        lineSplit = line.split()
        assert(len(lineSplit) == 1 + nnode)
        assert(int(lineSplit[0]) == index)
        # Note: GiD file indexes nodes from 1, Mesh s index nodes from 0
        mesh.AddVolumeElement(elements.Element(nodes = FromGidNodeOrder([int(node) - 1 for node in lineSplit[1:]],  elements.ElementType(dim = dimension, nodeCount = nnode))))
        line = fileHandle.readline()
      break
    line = fileHandle.readline()    
  debug.dprint("Elements: " + str(index))
  
  fileHandle.close()
  
  debug.dprint("Finished reading GiD mesh")
  
  return mesh
Exemple #40
0
import datetime
import math
import os
import unittest

import fluidity.diagnostics.debug as debug

from scipy.version import version as SciPyVersion

if tuple(int(x) for x in SciPyVersion.split('.')) < (0, 9, 0):
    try:
        import Scientific.IO.NetCDF as netcdf
    except:
        debug.deprint(
            "Warning: Your SciPy version is too old (<0.9.0) and \nthe Scientific.IO.NetCDF module failed to import"
        )
else:
    try:
        import scipy.io.netcdf as netcdf
    except:
        debug.deprint("Warning: Failed to import scipy.io.netcdf module")

import fluidity.diagnostics.calc as calc
import fluidity.diagnostics.filehandling as filehandling
import fluidity.diagnostics.structured_fields as structured_fields


class Era15:
    """
  Class for handling ERA15 10m wind data
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

"""
Structured field classes
"""

import copy
import math
import unittest

import fluidity.diagnostics.debug as debug

try:
  import numpy
except:
  debug.deprint("Warning: Failed to import numpy module")
try:
  import vtk
except:
  debug.deprint("Warning: Failed to import vtk module")

import fluidity.diagnostics.annulus_mesh as annulus_mesh
import fluidity.diagnostics.calc as calc
import fluidity.diagnostics.elements as elements
import fluidity.diagnostics.meshes as meshes
import fluidity.diagnostics.optimise as optimise
import fluidity.diagnostics.utils as utils

class StructuredField2D:
  def __init__(self, xCoords, yCoords, type = None, shape = None, data = None, name = None):
    if type is None:
Exemple #42
0
import os
import subprocess
import tempfile
import unittest

try:
    import numpy
except ImportError:
    debug.deprint("Warning: Failed to import numpy module")

import fluidity.diagnostics.debug as debug

try:
    from fluidity_tools import *
except:
    debug.deprint("Warning: Failed to import fluidity_tools module")

import fluidity.diagnostics.calc as calc
import fluidity.diagnostics.filehandling as filehandling
import fluidity.diagnostics.utils as utils


def FluidityBinary(
        binaries=[
            "dfluidity-debug", "dfluidity", "fluidity-debug", "fluidity"
        ]):
    """
  Return the command used to call Fluidity
  """

    binary = None
Exemple #43
0
"""
ERA data set handling routines
"""

import datetime
import math
import os
import unittest

import fluidity.diagnostics.debug as debug

try:
  import Scientific.IO.NetCDF as netcdf
except:
  debug.deprint("Warning: Failed to import Scientific.IO.NetCDF module")

import fluidity.diagnostics.calc as calc
import fluidity.diagnostics.filehandling as filehandling
import fluidity.diagnostics.structured_fields as structured_fields

class Era15:
  """
  Class for handling ERA15 10m wind data
  """

  # Zero point for time data
  _epoch = datetime.datetime(1900, 1, 1, 0, 0, 0)

  def __init__(self, filename, latitudeName = "latitude", longitudeName = "longitude", timeName = "time"):
    self._file = netcdf.NetCDFFile(filename, "r")
Exemple #44
0
def ReadTriangle(baseName):
  """
  Read triangle files with the given base name, and return it as a mesh
  """
      
  def StripComment(line):
    if "#" in line:
      return line.split("#")[0]
    else:
      return line
      
  def ReadNonCommentLine(fileHandle):
    line = fileHandle.readline()
    while len(line) > 0:
      line = StripComment(line).strip()
      if len(line) > 0:
        return line
      line = fileHandle.readline()
      
    return line
  
  # Determine which files exist
  assert(filehandling.FileExists(baseName + ".node"))
  hasBound = filehandling.FileExists(baseName + ".bound")
  hasEdge = filehandling.FileExists(baseName + ".edge")
  hasFace = filehandling.FileExists(baseName + ".face")
  hasEle = filehandling.FileExists(baseName + ".ele")
  hasHalo = filehandling.FileExists(baseName + ".halo")
    
  # Read the .node file
  
  nodeHandle = open(baseName + ".node", "r")
  
  # Extract the meta data
  line = ReadNonCommentLine(nodeHandle)
  lineSplit = line.split()
  assert(len(lineSplit) == 4)
  nNodes = int(lineSplit[0])
  assert(nNodes >= 0)
  dim = int(lineSplit[1])
  assert(dim >= 0)
  nNodeAttrs = int(lineSplit[2])
  assert(nNodeAttrs >= 0)
  nNodeIds = int(lineSplit[3])
  assert(nNodeIds >= 0)
  
  mesh = meshes.Mesh(dim)
  
  # Read the nodes
  debug.dprint("Reading .node file")
  
  for line in nodeHandle.readlines():
    line = StripComment(line)
    if len(line.strip()) == 0:
      continue
    lineSplit = line.split()
    assert(len(lineSplit) == 1 + dim + nNodeAttrs + nNodeIds)
    mesh.AddNodeCoord([float(coord) for coord in lineSplit[1:dim + 1]])
  assert(mesh.NodeCoordsCount() == nNodes)
  nodeHandle.close()
    
  if hasBound and dim == 1:
    # Read the .bound file
    debug.dprint("Reading .bound file")
    
    boundHandle = open(baseName + ".bound", "r")
    
    # Extract the meta data
    line = ReadNonCommentLine(boundHandle)
    lineSplit = line.split()
    assert(len(lineSplit) == 2)
    nBounds = int(lineSplit[0])
    assert(nBounds >= 0)
    nBoundIds = int(lineSplit[1])
    assert(nBoundIds >= 0)
    
    # Read the bounds
    for line in boundHandle.readlines():
      line = StripComment(line)
      if len(line.strip()) == 0:
        continue
      lineSplit = line.split()
      assert(len(lineSplit) == 2 + nBoundIds)
      element = elements.Element()
      for node in lineSplit[1:2]:
        element.AddNode(int(node) - 1)
      element.SetIds([int(boundId) for boundId in lineSplit[2:]])
      mesh.AddSurfaceElement(element)
    assert(mesh.SurfaceElementCount() == nBounds)
    boundHandle.close()
    
  if hasEdge and dim == 2:
    # Read the .edge file
    debug.dprint("Reading .edge file")
    
    edgeHandle = open(baseName + ".edge", "r")
    
    # Extract the meta data
    line = ReadNonCommentLine(edgeHandle)
    lineSplit = line.split()
    assert(len(lineSplit) == 2)
    nEdges = int(lineSplit[0])
    assert(nEdges >= 0)
    nEdgeIds = int(lineSplit[1])
    assert(nEdgeIds >= 0)
    
    # Read the edges
    for line in edgeHandle.readlines():
      line = StripComment(line)
      if len(line.strip()) == 0:
        continue
      lineSplit = line.split()
      assert(len(lineSplit) == 3 + nEdgeIds)
      element = elements.Element()
      for node in lineSplit[1:3]:
        element.AddNode(int(node) - 1)
      element.SetIds([int(edgeId) for edgeId in lineSplit[3:]])
      mesh.AddSurfaceElement(element)
    assert(mesh.SurfaceElementCount() == nEdges)
    edgeHandle.close()
      
  if hasFace and dim > 2:
    # Read the .face file
    debug.dprint("Reading .face file")
    
    faceHandle = open(baseName + ".face", "r")
    
    # Extract the meta data
    line = ReadNonCommentLine(faceHandle)
    lineSplit = line.split()
    assert(len(lineSplit) == 2)
    nFaces = int(lineSplit[0])
    assert(nFaces >= 0)
    nFaceIds = int(lineSplit[1])
    assert(nFaceIds >= 0)
    
    # Read the faces
    for line in faceHandle.readlines():
      line = StripComment(line)
      if len(line.strip()) == 0:
        continue
      lineSplit = line.split()
      assert(len(lineSplit) >= 4 + nFaceIds)
      element = elements.Element()
      for node in lineSplit[1:len(lineSplit) - nFaceIds]:
        element.AddNode(int(node) - 1)
      element.SetIds([int(faceId) for faceId in lineSplit[len(lineSplit) - nFaceIds:]])
      mesh.AddSurfaceElement(element)
    assert(mesh.SurfaceElementCount() == nFaces)
    faceHandle.close()
    
  if hasEle:
    # Read the .ele file
    debug.dprint("Reading .ele file")
    
    eleHandle = open(baseName + ".ele", "r")
    
    # Extract the meta data
    line = ReadNonCommentLine(eleHandle)
    lineSplit = line.split()
    assert(len(lineSplit) == 3)
    nEles = int(lineSplit[0])
    assert(nEles >= 0)
    nNodesPerEle = int(lineSplit[1])
    assert(nNodesPerEle >= 0)
    nEleIds = int(lineSplit[2])
    assert(nEleIds >= 0)
    
    # Read the eles
    for line in eleHandle.readlines():
      line = StripComment(line)
      if len(line.strip()) == 0:
        continue
      lineSplit = line.split()
      assert(len(lineSplit) == 1 + nNodesPerEle + nEleIds)
      element = elements.Element()
      for node in lineSplit[1:len(lineSplit) - nEleIds]:
        element.AddNode(int(node) - 1)
      element.SetIds([int(eleId) for eleId in lineSplit[len(lineSplit) - nEleIds:]])
      mesh.AddVolumeElement(element)
    assert(mesh.VolumeElementCount() == nEles)
    eleHandle.close()
    
  if hasHalo:
    # Read the .halo file
    debug.dprint("Reading .halo file")
  
    if mesh_halos.HaloIOSupport():
      halos = mesh_halos.ReadHalos(baseName + ".halo")
      mesh.SetHalos(halos)
    else:
      debug.deprint("Warning: No .halo I/O support")
    
  return mesh
import os
import unittest

import fluidity.diagnostics.debug as debug
import fluidity.diagnostics.optimise as optimise
import fluidity.diagnostics.utils as utils

def GuiDisabledByEnvironment():
  return "DIAGNOSTICS_GUI_DISABLED" in os.environ and os.environ["DIAGNOSTICS_GUI_DISABLED"]

if not GuiDisabledByEnvironment():
  try:
    import gobject
  except:
    debug.deprint("Warning: Failed to import gobject module")
  try:
    import gtk
  except:
    debug.deprint("Warning: Failed to import gtk module")
  
def DisplayWindow(window):
  """
  Launch the GTK main loop to display the supplied window
  """
  
  window.connect("destroy", gtk.main_quit)
  window.show()
  gtk.main()
  
  return