def testBuildShapeFunctionsForPslg(self): fake1 = Pslg.GridPoint(-1, -1) fake2 = Pslg.GridPoint(1, -1) pslg = ElementAwarePslg.ElementAwarePslg() for x in range(0, 10): pslg.points.append(Pslg.GridPoint(x, 1)) pslg.elements.append( ElementAwarePslg.Element(pslg.points[x], fake1, fake2, 0, x)) functions = ShapeFunctions.buildShapeFunctionsForPslg(pslg) for x in range(0, 10): s1 = functions[x][0] s2 = functions[x][1] s3 = functions[x][2] self.assertAlmostEquals(s1.evaluate(x, 1), 1, 9) self.assertAlmostEquals(s1.evaluate(fake1.x, fake1.y), 0, 9) self.assertAlmostEquals(s1.evaluate(fake2.x, fake2.y), 0, 9) self.assertAlmostEquals(s2.evaluate(x, 1), 0, 9) self.assertAlmostEquals(s2.evaluate(fake1.x, fake1.y), 1, 9) self.assertAlmostEquals(s2.evaluate(fake2.x, fake2.y), 0, 9) self.assertAlmostEquals(s3.evaluate(x, 1), 0, 9) self.assertAlmostEquals(s3.evaluate(fake1.x, fake1.y), 0, 9) self.assertAlmostEquals(s3.evaluate(fake2.x, fake2.y), 1, 9)
def loadEle(filename): pslg = ElementAwarePslg.ElementAwarePslg() file = open(filename, "r") try: PslgIo.readFromFile(file, pslg, filename) finally: file.close() return pslg
def testInitialization1(self): #Define grid pslg = ElementAwarePslg.ElementAwarePslg() p1 = Pslg.GridPoint(0,0) p2 = Pslg.GridPoint(1,1) p3 = Pslg.GridPoint(2,2) p4 = Pslg.GridPoint(0,2) p5 = Pslg.GridPoint(2,0) p1.index = 0 p2.index = 1 p3.index = 2 p4.index = 3 p5.index = 4 s1 = Pslg.Segment(p1, p4) s2 = Pslg.Segment(p4, p3) s3 = Pslg.Segment(p3, p5) s4 = Pslg.Segment(p5, p1) s5 = Pslg.Segment(p1, p2) s6 = Pslg.Segment(p2, p4) s7 = Pslg.Segment(p5, p2) s8 = Pslg.Segment(p2, p3) e1 = ElementAwarePslg.Element(p4, p2, p1, Parameters.Parameters.omegaThreeIdentifier, 0) e2 = ElementAwarePslg.Element(p4, p2, p3, Parameters.Parameters.omegaThreeIdentifier, 1) e3 = ElementAwarePslg.Element(p2, p5, p3, 0, 2) e4 = ElementAwarePslg.Element(p1, p2, p5, 0, 3) pslg.points.extend([p1, p2, p3, p4, p5]) pslg.segments.extend([s1, s2, s3, s4, s5, s6, s7, s8]) pslg.elements.extend([e1, e2, e3, e4]) #Assign boundary markers p1.boundaryMarker = Parameters.Parameters.omegaDIdentifier p2.boundaryMarker = Parameters.Parameters.omegaDIdentifier p5.boundaryMarker = Parameters.Parameters.omegaDIdentifier #Create parameters parameters = Parameters.Parameters() parameters.initialize(pslg) #Assert self.assertEqual(len(parameters.omegaD), 5) self.assertEqual(len(filter(lambda x: x == (s4, e4), parameters.omegaD)), 1) self.assertEqual(len(filter(lambda x: x == (s5, e1), parameters.omegaD)), 1) self.assertEqual(len(filter(lambda x: x == (s5, e4), parameters.omegaD)), 1) self.assertEqual(len(filter(lambda x: x == (s7, e3), parameters.omegaD)), 1) self.assertEqual(len(filter(lambda x: x == (s7, e4), parameters.omegaD)), 1)
def testGMatrix2(self): #Define grid pslg = ElementAwarePslg.ElementAwarePslg() x1 = Pslg.GridPoint(0, -2) x2 = Pslg.GridPoint(-2, 0) x3 = Pslg.GridPoint(0, 0) x4 = Pslg.GridPoint(2, 0) x5 = Pslg.GridPoint(0, 2) x1.index = 0 x2.index = 1 x3.index = 2 x4.index = 3 x5.index = 4 x1.boundaryMarker = Parameters.Parameters.omegaDIdentifier x4.boundaryMarker = Parameters.Parameters.omegaDIdentifier x5.boundaryMarker = Parameters.Parameters.omegaDIdentifier s1 = Pslg.Segment(x1, x4) s2 = Pslg.Segment(x4, x5) s3 = Pslg.Segment(x5, x2) s4 = Pslg.Segment(x2, x1) s5 = Pslg.Segment(x1, x3) s6 = Pslg.Segment(x3, x5) s7 = Pslg.Segment(x2, x3) s8 = Pslg.Segment(x3, x4) e1 = ElementAwarePslg.Element( x2, x3, x5, Parameters.Parameters.omegaThreeIdentifier, 0) e2 = ElementAwarePslg.Element(x3, x4, x5, 0, 1) e3 = ElementAwarePslg.Element( x2, x1, x3, Parameters.Parameters.omegaThreeIdentifier, 2) e4 = ElementAwarePslg.Element(x3, x1, x4, 0, 3) pslg.points.extend([x1, x2, x3, x4, x5]) pslg.segments.extend([s1, s2, s3, s4, s5, s6, s7, s8]) pslg.elements.extend([e1, e2, e3, e4]) #Create parameters parameters = Parameters.Parameters() parameters.initialize(pslg) #Get shape functions shapeFunctions = ShapeFunctions.buildShapeFunctionsForPslg(pslg) #Get the matrices (G, A, BPrime) = Assembler.precomputeMatrices(shapeFunctions, parameters) #Test symetry for i in range(0, len(pslg.points)): for j in range(0, len(pslg.points)): self.assertAlmostEqual(G[i, j], G[j, i], 9) #Test values self.assertAlmostEqual(G[0, 0], 4.0 / 3.0, 9) self.assertAlmostEqual(G[0, 1], 0.0, 9) self.assertAlmostEqual(G[0, 2], 0.0, 9) self.assertAlmostEqual(G[0, 3], 0.0, 9) self.assertAlmostEqual(G[0, 4], 0.0, 9) self.assertAlmostEqual(G[1, 0], 0.0, 9) self.assertAlmostEqual(G[1, 1], 4.0 / 3.0, 9) self.assertAlmostEqual(G[1, 2], 0.0, 9) self.assertAlmostEqual(G[1, 3], 0.0, 9) self.assertAlmostEqual(G[1, 4], 0.0, 9) self.assertAlmostEqual(G[2, 0], 0.0, 9) self.assertAlmostEqual(G[2, 1], 0.0, 9) self.assertAlmostEqual(G[2, 2], 8.0 / 3.0, 9) self.assertAlmostEqual(G[2, 3], 0.0, 9) self.assertAlmostEqual(G[2, 4], 0.0, 9) self.assertAlmostEqual(G[3, 0], 0.0, 9) self.assertAlmostEqual(G[3, 1], 0.0, 9) self.assertAlmostEqual(G[3, 2], 0.0, 9) self.assertAlmostEqual(G[3, 3], 4.0 / 3.0, 9) self.assertAlmostEqual(G[3, 4], 0.0, 9) self.assertAlmostEqual(G[4, 0], 0.0, 9) self.assertAlmostEqual(G[4, 1], 0.0, 9) self.assertAlmostEqual(G[4, 2], 0.0, 9) self.assertAlmostEqual(G[4, 3], 0.0, 9) self.assertAlmostEqual(G[4, 4], 4.0 / 3.0, 9) return
def testComputeProductionVector3(self): #Define grid pslg = ElementAwarePslg.ElementAwarePslg() x1 = Pslg.GridPoint(0, -2) x2 = Pslg.GridPoint(-2, 0) x3 = Pslg.GridPoint(0, 0) x4 = Pslg.GridPoint(2, 0) x5 = Pslg.GridPoint(0, 2) x1.index = 0 x2.index = 1 x3.index = 2 x4.index = 3 x5.index = 4 x1.boundaryMarker = Parameters.Parameters.omegaDIdentifier x4.boundaryMarker = Parameters.Parameters.omegaDIdentifier x5.boundaryMarker = Parameters.Parameters.omegaDIdentifier s1 = Pslg.Segment(x1, x4) s2 = Pslg.Segment(x4, x5) s3 = Pslg.Segment(x5, x2) s4 = Pslg.Segment(x2, x1) s5 = Pslg.Segment(x1, x3) s6 = Pslg.Segment(x3, x5) s7 = Pslg.Segment(x2, x3) s8 = Pslg.Segment(x3, x4) e1 = ElementAwarePslg.Element( x2, x3, x5, Parameters.Parameters.omegaThreeIdentifier, 0) e2 = ElementAwarePslg.Element(x3, x4, x5, 0, 1) e3 = ElementAwarePslg.Element( x2, x1, x3, Parameters.Parameters.omegaThreeIdentifier, 2) e4 = ElementAwarePslg.Element(x3, x1, x4, 0, 3) pslg.points.extend([x1, x2, x3, x4, x5]) pslg.segments.extend([s1, s2, s3, s4, s5, s6, s7, s8]) pslg.elements.extend([e1, e2, e3, e4]) #Create the coefficient vector z = numpy.matrix( [1.0 / 11.0, 15.0 / 44.0, 1.0 / 8.0, -1.0 / 11.0, 7.0 / 44.0]).transpose() #Create parameters parameters = Parameters.Parameters() parameters.initialize(pslg) #Tweak the parameters parameters.productionEffciency = lambda x, y: 1.0 parameters.productionThreshold = 1.0 #Get shape functions shapeFunctions = ShapeFunctions.buildShapeFunctionsForPslg(pslg) #Get the matrices P = Assembler.computeProductionVector(z, shapeFunctions, parameters) #Test values expected = numpy.matrix( [295.0 / 528.0, 45.0 / 44.0, 289.0 / 264.0, 0, 283.0 / 528.0]).transpose() self.assertTrue((abs(P - expected) < 1E-10).all()) return
def testGMatrix1(self): #Define grid pslg = ElementAwarePslg.ElementAwarePslg() p1 = Pslg.GridPoint(0, 0) p2 = Pslg.GridPoint(1, 1) p3 = Pslg.GridPoint(2, 2) p4 = Pslg.GridPoint(0, 2) p5 = Pslg.GridPoint(2, 0) p1.index = 0 p2.index = 1 p3.index = 2 p4.index = 3 p5.index = 4 e1 = ElementAwarePslg.Element( p4, p2, p1, Parameters.Parameters.omegaThreeIdentifier, 0) e2 = ElementAwarePslg.Element( p4, p2, p3, Parameters.Parameters.omegaThreeIdentifier, 1) e3 = ElementAwarePslg.Element(p2, p5, p3, 0, 2) e4 = ElementAwarePslg.Element(p1, p2, p5, 0, 3) pslg.points.extend([p1, p2, p3, p4, p5]) pslg.elements.extend([e1, e2, e3, e4]) #Create parameters parameters = Parameters.Parameters() parameters.initialize(pslg) #Get shape functions shapeFunctions = ShapeFunctions.buildShapeFunctionsForPslg(pslg) #Get the matrices (G, A, BPrime) = Assembler.precomputeMatrices(shapeFunctions, parameters) #Test symetry for i in range(0, len(pslg.points)): for j in range(0, len(pslg.points)): self.assertAlmostEqual(G[i, j], G[j, i], 9) #Test values self.assertAlmostEqual(G[0, 0], 2.0 / 3.0, 9) self.assertAlmostEqual(G[0, 1], 0.0, 9) self.assertAlmostEqual(G[0, 2], 0.0, 9) self.assertAlmostEqual(G[0, 3], 0.0, 9) self.assertAlmostEqual(G[0, 4], 0.0, 9) self.assertAlmostEqual(G[1, 0], 0.0, 9) self.assertAlmostEqual(G[1, 1], 4.0 / 3.0, 9) self.assertAlmostEqual(G[1, 2], 0.0, 9) self.assertAlmostEqual(G[1, 3], 0.0, 9) self.assertAlmostEqual(G[1, 4], 0.0, 9) self.assertAlmostEqual(G[2, 0], 0.0, 9) self.assertAlmostEqual(G[2, 1], 0.0, 9) self.assertAlmostEqual(G[2, 2], 2.0 / 3.0, 9) self.assertAlmostEqual(G[2, 3], 0.0, 9) self.assertAlmostEqual(G[2, 4], 0.0, 9) self.assertAlmostEqual(G[3, 0], 0.0, 9) self.assertAlmostEqual(G[3, 1], 0.0, 9) self.assertAlmostEqual(G[3, 2], 0.0, 9) self.assertAlmostEqual(G[3, 3], 2.0 / 3.0, 9) self.assertAlmostEqual(G[3, 4], 0.0, 9) self.assertAlmostEqual(G[4, 0], 0.0, 9) self.assertAlmostEqual(G[4, 1], 0.0, 9) self.assertAlmostEqual(G[4, 2], 0.0, 9) self.assertAlmostEqual(G[4, 3], 0.0, 9) self.assertAlmostEqual(G[4, 4], 2.0 / 3.0, 9) return
def testComputeProductionVector1(self): #Define grid pslg = ElementAwarePslg.ElementAwarePslg() x1 = Pslg.GridPoint(0, -2) x2 = Pslg.GridPoint(0, 0) x3 = Pslg.GridPoint(0, 2) x4 = Pslg.GridPoint(-2, 0) x5 = Pslg.GridPoint(2, 0) x1.index = 0 x2.index = 1 x3.index = 2 x4.index = 3 x5.index = 4 s1 = Pslg.Segment(x1, x5) s2 = Pslg.Segment(x5, x3) s3 = Pslg.Segment(x3, x4) s4 = Pslg.Segment(x4, x1) s5 = Pslg.Segment(x4, x2) s6 = Pslg.Segment(x2, x5) s7 = Pslg.Segment(x3, x2) s8 = Pslg.Segment(x2, x1) e1 = ElementAwarePslg.Element(x1, x2, x4, 0, 0) e2 = ElementAwarePslg.Element( x1, x2, x5, Parameters.Parameters.omegaThreeIdentifier, 1) e3 = ElementAwarePslg.Element(x4, x2, x3, 0, 2) e4 = ElementAwarePslg.Element( x3, x2, x5, Parameters.Parameters.omegaThreeIdentifier, 3) pslg.points.extend([x1, x2, x3, x4, x5]) pslg.segments.extend([s1, s2, s3, s4, s5, s6, s7, s8]) pslg.elements.extend([e1, e2, e3, e4]) #Create the coefficient vector z = numpy.matrix([1, 1, -1, 100, 1]).transpose() #Create parameters parameters = Parameters.Parameters() parameters.initialize(pslg) #Tweak the parameters parameters.productionEffciency = lambda x, y: 1.0 parameters.productionThreshold = 1.0 #Get shape functions shapeFunctions = ShapeFunctions.buildShapeFunctionsForPslg(pslg) #Get the matrices P = Assembler.computeProductionVector(z, shapeFunctions, parameters) #Test symetry self.assertAlmostEqual(P[0, 0], 0, 9) self.assertAlmostEqual(P[1, 0], 1.0 / 3.0, 9) self.assertAlmostEqual(P[2, 0], 2.0 / 3.0, 9) self.assertAlmostEqual(P[3, 0], 0, 9) self.assertAlmostEqual(P[4, 0], 1.0 / 3.0, 9) return
def testAMatrix1(self): #Define grid pslg = ElementAwarePslg.ElementAwarePslg() p1 = Pslg.GridPoint(0, 0) p2 = Pslg.GridPoint(1, 1) p3 = Pslg.GridPoint(2, 2) p4 = Pslg.GridPoint(0, 2) p5 = Pslg.GridPoint(2, 0) p1.index = 0 p2.index = 1 p3.index = 2 p4.index = 3 p5.index = 4 e1 = ElementAwarePslg.Element(p4, p2, p1, 1, 0) e2 = ElementAwarePslg.Element(p4, p2, p3, 2, 1) e3 = ElementAwarePslg.Element(p2, p5, p3, 3, 2) e4 = ElementAwarePslg.Element(p1, p2, p5, 4, 3) pslg.points.extend([p1, p2, p3, p4, p5]) pslg.elements.extend([e1, e2, e3, e4]) #Create parameters parameters = Parameters.Parameters() parameters.initialize(pslg) parameters.diffusionTensor = [[lambda x, y: 2, lambda x, y: 3], [lambda x, y: 5, lambda x, y: 7]] #Get shape functions shapeFunctions = ShapeFunctions.buildShapeFunctionsForPslg(pslg) #Get the matrices (G, A, BPrime) = Assembler.precomputeMatrices(shapeFunctions, parameters) self.assertAlmostEqual(A[0, 0], -17.0 / 2.0, 9) self.assertAlmostEqual(A[0, 1], 17.0 / 2.0, 9) self.assertAlmostEqual(A[0, 2], 0, 9) self.assertAlmostEqual(A[0, 3], 3.0 / 4.0, 9) self.assertAlmostEqual(A[0, 4], -3.0 / 4.0, 9) self.assertAlmostEqual(A[1, 0], 17.0 / 2.0, 9) self.assertAlmostEqual(A[1, 1], -18, 9) self.assertAlmostEqual(A[1, 2], 17.0 / 2.0, 9) self.assertAlmostEqual(A[1, 3], 0.5, 9) self.assertAlmostEqual(A[1, 4], 0.5, 9) self.assertAlmostEqual(A[2, 0], 0, 9) self.assertAlmostEqual(A[2, 1], 17.0 / 2.0, 9) self.assertAlmostEqual(A[2, 2], -17.0 / 2.0, 9) self.assertAlmostEqual(A[2, 3], -3.0 / 4.0, 9) self.assertAlmostEqual(A[2, 4], 3.0 / 4.0, 9) self.assertAlmostEqual(A[3, 0], 7.0 / 4.0, 9) self.assertAlmostEqual(A[3, 1], 0.5, 9) self.assertAlmostEqual(A[3, 2], -7.0 / 4.0, 9) self.assertAlmostEqual(A[3, 3], -1.0 / 2.0, 9) self.assertAlmostEqual(A[3, 4], 0, 9) self.assertAlmostEqual(A[4, 0], -7.0 / 4.0, 9) self.assertAlmostEqual(A[4, 1], 0.5, 9) self.assertAlmostEqual(A[4, 2], 7.0 / 4.0, 9) self.assertAlmostEqual(A[4, 3], 0, 9) self.assertAlmostEqual(A[4, 4], -1.0 / 2.0, 9) return
def testSolveSingleStep1(self): #Define grid pslg = ElementAwarePslg.ElementAwarePslg() x1 = Pslg.GridPoint(0, -2) x2 = Pslg.GridPoint(-2, 0) x3 = Pslg.GridPoint(0, 0) x4 = Pslg.GridPoint(2, 0) x5 = Pslg.GridPoint(0, 2) x1.index = 0 x2.index = 1 x3.index = 2 x4.index = 3 x5.index = 4 x1.boundaryMarker = Parameters.Parameters.omegaDIdentifier x4.boundaryMarker = Parameters.Parameters.omegaDIdentifier x5.boundaryMarker = Parameters.Parameters.omegaDIdentifier s1 = Pslg.Segment(x1, x4) s2 = Pslg.Segment(x4, x5) s3 = Pslg.Segment(x5, x2) s4 = Pslg.Segment(x2, x1) s5 = Pslg.Segment(x1, x3) s6 = Pslg.Segment(x3, x5) s7 = Pslg.Segment(x2, x3) s8 = Pslg.Segment(x3, x4) e1 = ElementAwarePslg.Element( x2, x3, x5, Parameters.Parameters.omegaThreeIdentifier, 0) e2 = ElementAwarePslg.Element(x3, x4, x5, 0, 1) e3 = ElementAwarePslg.Element( x2, x1, x3, Parameters.Parameters.omegaThreeIdentifier, 2) e4 = ElementAwarePslg.Element(x3, x1, x4, 0, 3) pslg.points.extend([x1, x2, x3, x4, x5]) pslg.segments.extend([s1, s2, s3, s4, s5, s6, s7, s8]) pslg.elements.extend([e1, e2, e3, e4]) #Create the coefficient vector zOriginal = numpy.matrix([0, 0, 0, 0, 0]).transpose() zPrev = numpy.matrix([0, 0, 0, 0, 0]).transpose() #Create parameters parameters = Parameters.Parameters() parameters.initialize(pslg) #Tweak the parameters parameters.diffusionTensor = [[lambda x, y: 1.0, lambda x, y: 1.0], [lambda x, y: 1.0, lambda x, y: 1.0]] parameters.productionEffciency = lambda x, y: 1.0 parameters.productionThreshold = 1.0 parameters.releaseEfficiency = lambda t: 0.0 parameters.initialDensity = lambda x, y: 0.0 #Get shape functions shapeFunctions = ShapeFunctions.buildShapeFunctionsForPslg(pslg) #Get the matrices (G, A, BPrime) = Assembler.precomputeMatrices(shapeFunctions, parameters) P = Assembler.computeProductionVector(zOriginal, shapeFunctions, parameters) #Set time parameters.deltaT = 0.01 parameters.tEnd = 1.0 t = 0 #Compute parts of equation LeftSide = G - parameters.deltaT / 2.0 * ( A + parameters.releaseEfficiency(t + parameters.deltaT) * BPrime) MostOfRightSide = (G + parameters.deltaT / 2.0 * (A + parameters.releaseEfficiency(t) * BPrime) ) * zOriginal + parameters.deltaT / 2.0 * P #Perform test zNew = Solver.SolveSingleStep(shapeFunctions, parameters, parameters.deltaT, LeftSide, MostOfRightSide, zPrev) #Test values expected = numpy.matrix( [1.0 / 11.0, 15.0 / 44.0, 1.0 / 8.0, -1.0 / 11.0, 7.0 / 44.0]).transpose() self.assertTrue((abs(zNew - expected) < 1E-10).all())
def testSolveInTime1(self): #Define grid pslg = ElementAwarePslg.ElementAwarePslg() x1 = Pslg.GridPoint(0, -2) x2 = Pslg.GridPoint(0, 0) x3 = Pslg.GridPoint(0, 2) x4 = Pslg.GridPoint(-2, 0) x5 = Pslg.GridPoint(2, 0) x1.index = 0 x2.index = 1 x3.index = 2 x4.index = 3 x5.index = 4 x1.boundaryMarker = Parameters.Parameters.omegaDIdentifier x3.boundaryMarker = Parameters.Parameters.omegaDIdentifier x5.boundaryMarker = Parameters.Parameters.omegaDIdentifier s1 = Pslg.Segment(x1, x5) s2 = Pslg.Segment(x5, x3) s3 = Pslg.Segment(x3, x4) s4 = Pslg.Segment(x4, x1) s5 = Pslg.Segment(x4, x2) s6 = Pslg.Segment(x2, x5) s7 = Pslg.Segment(x3, x2) s8 = Pslg.Segment(x2, x1) e1 = ElementAwarePslg.Element(x1, x2, x4, 0, 0) e2 = ElementAwarePslg.Element( x1, x2, x5, Parameters.Parameters.omegaThreeIdentifier, 1) e3 = ElementAwarePslg.Element(x4, x2, x3, 0, 2) e4 = ElementAwarePslg.Element( x3, x2, x5, Parameters.Parameters.omegaThreeIdentifier, 3) pslg.points.extend([x1, x2, x3, x4, x5]) pslg.segments.extend([s1, s2, s3, s4, s5, s6, s7, s8]) pslg.elements.extend([e1, e2, e3, e4]) #Create parameters parameters = Parameters.Parameters() parameters.initialize(pslg) #Tweak the parameters parameters.diffusionTensor = [[lambda x, y: 1.0, lambda x, y: 1.0], [lambda x, y: 1.0, lambda x, y: 1.0]] parameters.productionEffciency = lambda x, y: 1.0 parameters.productionThreshold = 1.0 parameters.releaseEfficiency = lambda t: 0.0 #Get shape functions shapeFunctions = ShapeFunctions.buildShapeFunctionsForPslg(pslg) #Prepare matrices G = scipy.sparse.csc_matrix(scipy.diag([1, 1, 1, 1, 1])) BPrime = scipy.sparse.csc_matrix(scipy.zeros((5, 5))) #Prepare vector zt = numpy.matrix([2, 2, 2, 2, 2]).transpose() #Set time t = 0 #Solve zNew = Solver.SolveInTime(shapeFunctions, parameters, G, G, BPrime, zt, t) #Assert for i in range(0, 5): self.assertEqual(zt[i, 0], zNew[i, 0])