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 testTransformOrigin2(self): p1 = Pslg.GridPoint(3, 4) p2 = Pslg.GridPoint(5, 4) p3 = Pslg.GridPoint(3, 4.5) e = ElementAwarePslg.Element(p1, p2, p3, 0, 0) values = Integrator.transformOrigin(e, 0, 0) self.assertAlmostEqual(values[0], 3) self.assertAlmostEqual(values[1], 4) values = Integrator.transformOrigin(e, 1, 0) self.assertAlmostEqual(values[0], 5) self.assertAlmostEqual(values[1], 4) values = Integrator.transformOrigin(e, 0, 1) self.assertAlmostEqual(values[0], 3) self.assertAlmostEqual(values[1], 4.5) values = Integrator.transformOrigin(e, 0.5, 0) self.assertAlmostEqual(values[0], 4) self.assertAlmostEqual(values[1], 4) values = Integrator.transformOrigin(e, 0, 0.5) self.assertAlmostEqual(values[0], 3) self.assertAlmostEqual(values[1], 4.25) values = Integrator.transformOrigin(e, 0.5, 0.5) self.assertAlmostEqual(values[0], 4) self.assertAlmostEqual(values[1], 4.25) values = Integrator.transformOrigin(e, 0.25, 0.5) self.assertAlmostEqual(values[0], 3.5) self.assertAlmostEqual(values[1], 4.25)
def testIsSideOf2(self): p1 = Pslg.GridPoint(0,0) p2 = Pslg.GridPoint(0,1) p3 = Pslg.GridPoint(1,0) q1 = Pslg.GridPoint(1,1) q2 = Pslg.GridPoint(-1,-1) e = ElementAwarePslg.Element(p1, p2, p3, 0, 0) s1 = Pslg.Segment(q1, q2) self.assertFalse(e.isSideOf(s1)) s1 = Pslg.Segment(p1, q1) s2 = Pslg.Segment(p2, q1) s3 = Pslg.Segment(p3, q1) self.assertFalse(e.isSideOf(s1)) self.assertFalse(e.isSideOf(s2)) self.assertFalse(e.isSideOf(s3)) s1 = Pslg.Segment(q2, p1) s2 = Pslg.Segment(q2, p2) s3 = Pslg.Segment(q2, p3) self.assertFalse(e.isSideOf(s1)) self.assertFalse(e.isSideOf(s2)) self.assertFalse(e.isSideOf(s3))
def testTransformOrigin3(self): p1 = Pslg.GridPoint(4, 4) p2 = Pslg.GridPoint(7, 5) p3 = Pslg.GridPoint(5, 7) e = ElementAwarePslg.Element(p1, p2, p3, 0, 0) values = Integrator.transformOrigin(e, 0, 0) self.assertAlmostEqual(values[0], 4) self.assertAlmostEqual(values[1], 4) values = Integrator.transformOrigin(e, 1, 0) self.assertAlmostEqual(values[0], 7) self.assertAlmostEqual(values[1], 5) values = Integrator.transformOrigin(e, 0, 1) self.assertAlmostEqual(values[0], 5) self.assertAlmostEqual(values[1], 7) values = Integrator.transformOrigin(e, 0.5, 0) self.assertAlmostEqual(values[0], 5.5) self.assertAlmostEqual(values[1], 4.5) values = Integrator.transformOrigin(e, 0, 0.5) self.assertAlmostEqual(values[0], 4.5) self.assertAlmostEqual(values[1], 5.5) values = Integrator.transformOrigin(e, 0.5, 0.5) self.assertAlmostEqual(values[0], 6) self.assertAlmostEqual(values[1], 6) values = Integrator.transformOrigin(e, 0.25, 0.5) self.assertAlmostEqual(values[0], 4 + 5.0 / 4.0) self.assertAlmostEqual(values[1], 4 + 7.0 / 4.0)
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 testAreaComputation3(self): p1 = Pslg.GridPoint(0,0) p2 = Pslg.GridPoint(1,0) p3 = Pslg.GridPoint(0.5, math.sqrt(3)/2.0) e = ElementAwarePslg.Element(p1, p2, p3, 0, 0) result = e.getArea() self.assertAlmostEqual(result, math.sqrt(3)/4.0, 9)
def testAreaComputation1(self): p1 = Pslg.GridPoint(0,0) p2 = Pslg.GridPoint(1,0) p3 = Pslg.GridPoint(0,1) e = ElementAwarePslg.Element(p1, p2, p3, 0, 0) result = e.getArea() self.assertAlmostEqual(result, 0.5, 9)
def testAreaComputation4(self): p1 = Pslg.GridPoint(2,2) p2 = Pslg.GridPoint(5,4) p3 = Pslg.GridPoint(5,6) e = ElementAwarePslg.Element(p1, p2, p3, 0, 0) result = e.getArea() self.assertAlmostEqual(result, 3, 9)
def loadEleFile(file, pslg, baseFilename): nodeFile = open(baseFilename + ".node", "r") loadNodeFile(nodeFile, pslg) (trianglesNumber, nodesNumber, attributesNumber) = loadEleHeader(file) segmentSet = set() for i in range(0, trianglesNumber): line = getLine(file) tokens = string.split(line) if len(tokens) != 1 + nodesNumber + attributesNumber: raise IOError("Invalid file format (triangle " + str(i) + " has invalid format)") #Read triangle index index = int(tokens[0]) #Read nodes x = None; y = None; elementConstructorArgs = [] for j in range(0, nodesNumber): startPoint = int(tokens[j + 1]) endPoint = int(tokens[((j + 1) % nodesNumber) + 1]) elementConstructorArgs.append(pslg.points[startPoint]) segmentSet.add((min(startPoint, endPoint), max(startPoint, endPoint))) if x is None or y is None: x = pslg.points[startPoint].x y = pslg.points[startPoint].y else: x = 0.5 * x + 0.5 * pslg.points[startPoint].x y = 0.5 * y + 0.5 * pslg.points[startPoint].y #Read attributes id = None if attributesNumber == 1: id = int(tokens[1 + nodesNumber]) elementConstructorArgs.append(id) #Append the index elementConstructorArgs.append(i) #Add the element if pslg.__class__ is ElementAwarePslg.ElementAwarePslg: pslg.elements.append(ElementAwarePslg.Element(*elementConstructorArgs)) #Add the triangle region # triangleRegion = Pslg.Region(x, y) # if id is not None: # triangleRegion.id = id # pslg.regions.append(triangleRegion) #Add the segments for (startpoint, endpoint) in segmentSet: pslg.segments.append(Pslg.Segment(pslg.points[startpoint], pslg.points[endpoint])) return
def testFirstDegreePolynomial5(self): p1 = Pslg.GridPoint(0, 0) p2 = Pslg.GridPoint(1, 0) p3 = Pslg.GridPoint(0, 1) e = ElementAwarePslg.Element(p1, p2, p3, 0, 0) def f(x, y): return 2.0 * x + 3.0 * y + 4.0 result = Integrator.integrate2D(f, e) self.assertAlmostEqual(result, 2.0 / 6.0 + 3.0 / 6.0 + 2.0, 9)
def testSecondDegreePolynomial1(self): p1 = Pslg.GridPoint(0, 0) p2 = Pslg.GridPoint(1, 0) p3 = Pslg.GridPoint(0, 1) e = ElementAwarePslg.Element(p1, p2, p3, 0, 0) def f(x, y): return x * x result = Integrator.integrate2D(f, e) self.assertAlmostEqual(result, 1.0 / 12.0, 9)
def testConstantFunction1(self): p1 = Pslg.GridPoint(0, 0) p2 = Pslg.GridPoint(1, 0) p3 = Pslg.GridPoint(0, 1) e = ElementAwarePslg.Element(p1, p2, p3, 0, 0) def f(x, y): return 1 result = Integrator.integrate2D(f, e) self.assertAlmostEqual(result, 0.5, 9)
def testIntegration7(self): p1 = Pslg.GridPoint(2, 2) p2 = Pslg.GridPoint(5, 4) p3 = Pslg.GridPoint(5, 6) e = ElementAwarePslg.Element(p1, p2, p3, 0, 0) def f(x, y): return 2.0 * x * x + 3.0 * x * y + 4.0 * y * y + 5.0 * x + 6.0 * y + 7.0 result = Integrator.integrate2D(f, e) expected = 600.5 self.assertAlmostEqual(result, expected, 9)
def testSecondDegreePolynomial7(self): p1 = Pslg.GridPoint(0, 0) p2 = Pslg.GridPoint(1, 0) p3 = Pslg.GridPoint(0, 1) e = ElementAwarePslg.Element(p1, p2, p3, 0, 0) def f(x, y): return 2.0 * x * x + 3.0 * x * y + 4.0 * y * y + 5.0 * x + 6.0 * y + 7.0 result = Integrator.integrate2D(f, e) expected = 5.9583333333333339 self.assertAlmostEqual(result, expected, 9)
def testIntegration6(self): p1 = Pslg.GridPoint(2, 2) p2 = Pslg.GridPoint(5, 4) p3 = Pslg.GridPoint(5, 6) e = ElementAwarePslg.Element(p1, p2, p3, 0, 0) def f(x, y): return 7.0 result = Integrator.integrate2D(f, e) expected = 21 self.assertAlmostEqual(result, expected, 9)
def testCheckPointInElement(self): #Arrange x1 = Pslg.GridPoint(0, 0) x2 = Pslg.GridPoint(2, 0) x3 = Pslg.GridPoint(1, 1) e = ElementAwarePslg.Element(x1, x2, x3, 0) program = Program.FemResultsViewer(None, None) #Act & Assert self.assertTrue(program.CheckPointInElement(e, 0, 0)) self.assertTrue(program.CheckPointInElement(e, 2, 0)) self.assertTrue(program.CheckPointInElement(e, 1, 1)) self.assertTrue(program.CheckPointInElement(e, 1, 0)) self.assertTrue(program.CheckPointInElement(e, 1.5, 0.5)) self.assertTrue(program.CheckPointInElement(e, 0.5, 0.5)) self.assertTrue(program.CheckPointInElement(e, 1, 0.5))
def testIsSideOf1(self): p1 = Pslg.GridPoint(0,0) p2 = Pslg.GridPoint(0,1) p3 = Pslg.GridPoint(1,0) e = ElementAwarePslg.Element(p1, p2, p3, 0, 0) s1 = Pslg.Segment(p1, p2) s2 = Pslg.Segment(p2, p3) s3 = Pslg.Segment(p3, p1) self.assertTrue(e.isSideOf(s1)) self.assertTrue(e.isSideOf(s2)) self.assertTrue(e.isSideOf(s3)) s1 = Pslg.Segment(p2, p1) s2 = Pslg.Segment(p3, p2) s3 = Pslg.Segment(p1, p3) self.assertTrue(e.isSideOf(s1)) self.assertTrue(e.isSideOf(s2)) self.assertTrue(e.isSideOf(s3))
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 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])
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 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 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