Ejemplo n.º 1
0
    def Map(self, coord):
        if optimise.DebuggingEnabled():
            assert (len(coord) == 3)
            assert (coord[2] <= self._maxZ)
            assert (coord[2] >= self._minZ)

        distanceFromBottom = coord[2] - self._minZ
        r = calc.L2Norm(coord[:2])
        if optimise.DebuggingEnabled():
            assert (r >= self._a and r <= self._b)
        distanceFromInnerWall = r - self._a
        distanceFromOuterWall = self._b - r

        if calc.AlmostEquals(self._phiTop, 0.0):
            localMaxZ = self._maxZ
        elif self._phiTop > 0.0:
            localMaxZ = self._maxZ - distanceFromOuterWall * math.tan(
                self._phiTop)
        else:
            localMaxZ = self._maxZ + distanceFromInnerWall * math.tan(
                self._phiTop)
        if calc.AlmostEquals(self._phiBottom, 0.0):
            localMinZ = self._minZ
        elif self._phiBottom > 0.0:
            localMinZ = self._minZ + distanceFromInnerWall * math.tan(
                self._phiBottom)
        else:
            localMinZ = self._minZ - distanceFromOuterWall * math.tan(
                self._phiBottom)

        newZ = localMinZ + (distanceFromBottom /
                            (self._maxZ - self._minZ)) * (localMaxZ -
                                                          localMinZ)

        return [coord[0], coord[1], newZ]
Ejemplo n.º 2
0
def CylindricalVectorsToCartesian(coordinates, data):
    """
  Project the supplied cylindrical coordinates (r-phi-z) vectors to 3D Cartesian
  (x-y-z). coordinates must be in Cartesian.
  """

    if optimise.DebuggingEnabled():
        assert (len(coordinates) == len(data))
        for i, coord in enumerate(coordinates):
            assert (len(coord) == 3)
            assert (len(data[i]) == 3)

    newData = numpy.empty((len(data), 3))
    for i, coord in enumerate(coordinates):
        datum = data[i]

        rMag = L2Norm(coord[:2])
        x = [coord[0] / rMag, -coord[1] / rMag]
        y = [-x[1], x[0]]

        newData[i, :] = [
            datum[0] * x[0] + datum[1] * x[1],
            datum[0] * y[0] + datum[1] * y[1], datum[2]
        ]

    return newData
Ejemplo n.º 3
0
def CartesianVectorsToPolar(coordinates, data):
    """
  Project the supplied 2D Cartesian (x-y) vectors to polar coordinates
  (r-theta). coordinates must be in Cartesian.
  """

    if optimise.DebuggingEnabled():
        assert (len(coordinates) == len(data))
        for i, coord in enumerate(coordinates):
            assert (len(coord) == 2)
            assert (len(data[i]) == 2)

    newData = numpy.empty((len(data), 2))
    for i, coord in enumerate(coordinates):
        datum = data[i]

        rMag = L2Norm(coord[:2])
        r = [coord[0] / rMag, coord[1] / rMag]
        theta = [-r[1], r[0]]

        newData[i] = [
            datum[0] * r[0] + datum[1] * r[1],
            datum[0] * theta[0] + datum[1] * theta[1]
        ]

    return newData
Ejemplo n.º 4
0
 def AddSurfaceElement(self, element):
   if optimise.DebuggingEnabled():
     for node in element.GetNodes():
       assert(self.ValidNode(node))
       
   element.SetDim(self.GetDim() - 1)
   self._surfaceElements.append(element)
   
   return  
Ejemplo n.º 5
0
  def SurfaceElementFixedNodeCount(self):
    if self.SurfaceElementCount() == 0:
      return 0
  
    nnode = self.GetSurfaceElement(0).NodeCount()
    if optimise.DebuggingEnabled():
      for element in self.GetSurfaceElements()[1:]:
        assert(element.NodeCount() == nnode)

    return nnode
Ejemplo n.º 6
0
def TetVolume(nodeCoords, signed=False):
    """
  Return the volume of the tetrahedron with the supplied node coordinates
  """

    if optimise.DebuggingEnabled():
        type = elements.ElementType(dim=len(nodeCoords[0]),
                                    nodeCount=len(nodeCoords))
        assert (type.GetElementTypeId() == elements.ELEMENT_TETRAHEDRON)

    return SimplexVolume(nodeCoords, signed=signed)
Ejemplo n.º 7
0
    def SetData(self, x, y, z, levels=None):
        assert (len(z) == len(y))
        if optimise.DebuggingEnabled():
            for val in z:
                assert (len(val) == len(x))

        self._NewFigure()
        if levels is None:
            self._SetPlot(pylab.contourf(x, y, z))
        else:
            self._SetPlot(pylab.contourf(x, y, z, levels))

        return
Ejemplo n.º 8
0
def AddtoVtuField(vtu, add, fieldName, scale=None):
    """
  Add a field from a vtu onto the corresponding field in an input vtu
  """

    if optimise.DebuggingEnabled():
        assert (VtuMatchLocations(vtu, add))

    if scale is None:
        vtu.AddFieldToField(fieldName, add.GetField(fieldName))
    else:
        vtu.AddFieldToField(fieldName, add.GetField(fieldName) * scale)

    return
Ejemplo n.º 9
0
    def SetData(self, x, y):
        assert (len(x) == len(y))

        if len(y) > 0 and utils.CanLen(y[0]):
            fields = len(y[0])
            if optimise.DebuggingEnabled():
                for comp in y[1:]:
                    assert (len(comp) == fields)

            yData = []
            for i in range(fields):
                yData.append([y[j][i] for j in range(len(y))])
        else:
            fields = 1
            yData = [y]

        self._NewFigure()

        colours = []
        for i in range(fields):
            colourNumber = 256 * 256 * 256 * i / fields

            colourComp = str(hex(colourNumber % 256))[2:]
            while len(colourComp) < 2:
                colourComp = "0" + colourComp
            colour = colourComp
            colourNumber /= 256

            colourComp = str(hex(colourNumber % 256))[2:]
            while len(colourComp) < 2:
                colourComp = "0" + colourComp
            colour = colourComp + colour
            colourNumber /= 256

            colourComp = str(hex(colourNumber % 256))[2:]
            while len(colourComp) < 2:
                colourComp = "0" + colourComp
            colour = "#" + colourComp + colour

            colours.append(colour)

        args = []
        for i in range(fields):
            args += [x, yData[i], colours[i]]

        pylab.plot(*args)
        self._SetPlot(pylab.gca())

        return
Ejemplo n.º 10
0
    def Map(self, coord):
        if optimise.DebuggingEnabled():
            assert (len(coord) == 3)
            assert (coord[2] <= self._maxZ)
            assert (coord[2] >= self._minZ)

        r = calc.L2Norm(coord[:2])
        phi = math.atan2(coord[1], coord[0])

        phi += self._phi * ((coord[2] - self._minZ) /
                            (self._maxZ - self._minZ))

        newX = r * math.cos(phi)
        newY = r * math.sin(phi)

        return [newX, newY, coord[2]]
Ejemplo n.º 11
0
def VtuBoundingBox(vtu):
    """
  Return the bounding box of the supplied vtu
  """

    vtuBounds = vtu.ugrid.GetBounds()
    lbound, ubound = [vtuBounds[2 * i] for i in range(len(vtuBounds) / 2)], [
        vtuBounds[2 * i + 1] for i in range(len(vtuBounds) / 2)
    ]
    if len(lbound) > 0 and lbound[0] > ubound[0]:
        if optimise.DebuggingEnabled():
            for i in range(1, len(lbound)):
                assert (lbound[i] > ubound[i])
        lbound = [0.0 for i in range(len(lbound))]
        ubound = [0.0 for i in range(len(ubound))]

    return bounds.BoundingBox(lbound, ubound)
Ejemplo n.º 12
0
def IndexBinaryUboundSearch(val, values, increasing=True):
    """
  Find the min (max) index into values such that values[index] >= val, where
  if increasing is True (False), assumes the values are in increasing
  (decreasing) order
  """

    if isinstance(values, numpy.ndarray):
        values = values.tolist()

    if increasing:
        lValues = copy.deepcopy(values)
        values = lValues
        values.reverse()

    if optimise.DebuggingEnabled():
        testValues = copy.deepcopy(values)
        testValues.sort()
        testValues.reverse()
        assert (values == testValues)

    minUpper = 0
    maxUpper = len(values) - 1
    upper = maxUpper
    while maxUpper - minUpper > 0:
        # Choose new bounds for lower
        if values[upper] < val:
            maxUpper = upper
        else:
            minUpper = upper

        # Calculate a new guess for lower
        oldUpper = upper
        upper = int(float(maxUpper + minUpper) / 2.0)
        if oldUpper == upper:
            if maxUpper - minUpper <= 1:
                break
            else:
                upper -= 1

    if not increasing:
        return upper
    else:
        return len(values) - upper - 1
Ejemplo n.º 13
0
def TransposeListList(inputList):
  """
  Transpose the input list of lists (which must not be ragged)
  """
  
  if optimise.DebuggingEnabled():
    if len(inputList) > 0:
      assert(CanLen(inputList[0]))
      size = len(inputList[0])
      for entry in inputList[1:]:
        assert(CanLen(entry))
        assert(len(inputList[1]) == size)
  
  if len(inputList) == 0:
    return []
  
  tList = [[] for i in range(len(inputList[0]))]
  for entry in inputList:
    for i, datum in enumerate(entry):
      tList[i].append(datum)
  
  return tList
Ejemplo n.º 14
0
def KeyedSort(keys, *values, **kargs):
    """
  Return a re-ordering of values, with the remapping equal to the sort mapping
  of keys. Each key must correspond to a unique value. Optional keyword
  arguments:
    returnSortedKeys  Return the sorted keys as well as the sorted values
  """

    returnSortedKeys = False
    for key in kargs:
        if key == "returnSortedKeys":
            returnSortedKeys = True
        else:
            raise Exception("Unexpected keyword argument \"" + key + "\"")

    sortList = []
    for i, key in enumerate(keys):
        sortList.append(
            Sorter(key, tuple([subValues[i] for subValues in values])))

    sortList.sort()
    if optimise.DebuggingEnabled():
        for i in range(len(sortList) - 1):
            if sortList[i].GetKey() == sortList[i + 1].GetKey():
                assert (sortList[i].GetValue() == sortList[i].GetValue())

    result = []
    if returnSortedKeys:
        result.append([sorter.GetKey() for sorter in sortList])

    for i in range(len(values)):
        result.append([sorter.GetValue()[i] for sorter in sortList])

    if len(result) == 1:
        result = result[0]
    else:
        result = tuple(result)

    return result