testUmfpack.py :  » Development » PySparse » pysparse-1.1 » Test » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » PySparse 
PySparse » pysparse 1.1 » Test » testUmfpack.py
import math, unittest
import numpy
from pysparse import poisson
from pysparse.pysparseMatrix import PysparseMatrix,PysparseIdentityMatrix,PysparseSpDiagsMatrix
from pysparse.pysparseUmfpack import PysparseUmfpackSolver

import sys

def macheps():
    "compute machine epsilon"
    eps = 1.0
    while (1.0 + eps > 1.0):
        eps /= 2.0
    return 2.0 * eps
    
class SpDiagsTestCase(unittest.TestCase):
    def setUp(self):
        self.n = 10000
        self.e = numpy.ones(self.n)
        self.A = PysparseSpDiagsMatrix(size=self.n,
                                       vals=(-2*self.e,self.e,2*self.e),
                                       pos=(-1,0,1))
        self.b = self.A * self.e
        self.tol = 100*macheps()
        self.LU = None
        self.err = 0.0
        self.fmt = '\t%8.2e  %8.2e  %8d  %8d  %8d  %6.2f  %6.2f\n'

    def tearDown(self):
        self.LU.fetch_lunz()
        sys.stdout.write(self.fmt % (self.err, self.tol, self.A.getNnz(),
                                     self.LU.lnz, self.LU.unz,
                                     self.LU.factorizationTime,
                                     self.LU.solutionTime))
        
    def computeError(self, x):
        self.err = numpy.linalg.norm(x - self.e, ord=numpy.inf)
        return self.err

    def testVanilla(self):
        self.LU = PysparseUmfpackSolver(self.A)
        self.LU.solve(self.b)
        self.failUnless(self.computeError(self.LU.sol) < self.tol)

    def testUnsymmetric(self):
        self.LU = PysparseUmfpackSolver(self.A, strategy='unsymmetric')
        self.LU.solve(self.b)
        self.failUnless(self.computeError(self.LU.sol) < self.tol)

    def testSymmetric(self):
        self.LU = PysparseUmfpackSolver(self.A, strategy='symmetric')
        self.LU.solve(self.b)
        self.failUnless(self.computeError(self.LU.sol) < self.tol)

    def test2by2(self):
        self.LU = PysparseUmfpackSolver(self.A, strategy='2by2')
        self.LU.solve(self.b)
        self.failUnless(self.computeError(self.LU.sol) < self.tol)

    def testNoScaling(self):
        self.LU = PysparseUmfpackSolver(self.A, scale='none')
        self.LU.solve(self.b)
        self.failUnless(self.computeError(self.LU.sol) < self.tol)

    def testScaleMax(self):
        self.LU = PysparseUmfpackSolver(self.A, scale='max')
        self.LU.solve(self.b)
        self.failUnless(self.computeError(self.LU.sol) < self.tol)


            
class Poisson1dTestCase(unittest.TestCase):
    def setUp(self):
        self.n = 50000
        self.B = PysparseMatrix( matrix=poisson.poisson1d(self.n) )
        
        self.x_exact = numpy.ones(self.n)/math.sqrt(self.n)
        self.normx = 1.0/math.sqrt(self.n)
        self.b = self.B * self.x_exact
        
        lmbd_min = 4.0 * math.sin(math.pi/2.0/self.n) ** 2
        lmbd_max = 4.0 * math.sin((self.n - 1)*math.pi/2.0/self.n) ** 2
        cond = lmbd_max/lmbd_min
        self.tol = cond * macheps()
        self.relerr = 0.0
        self.nnz = self.B.getNnz()
        self.LU = None
        self.fmt = '\t%8.2e  %8.2e  %8d  %8d  %8d  %6.2f  %6.2f\n'

    def computeError(self, x):
        absErr = numpy.linalg.norm(x - self.x_exact, ord=numpy.inf)
        self.relerr = absErr/(1 + self.normx)
        return self.relerr

    def tearDown(self):
        self.LU.fetch_lunz()
        sys.stdout.write(self.fmt % (self.relerr, self.tol, self.nnz,
                                     self.LU.lnz, self.LU.unz,
                                     self.LU.factorizationTime,
                                     self.LU.solutionTime))
        
    def testPoisson1dDefault(self):
        self.LU = PysparseUmfpackSolver(self.B)
        self.LU.solve(self.b)
        self.failUnless(self.computeError(self.LU.sol) < self.tol)

    def testPoisson1dUnsymmetric(self):
        self.LU = PysparseUmfpackSolver(self.B, strategy='unsymmetric')
        self.LU.solve(self.b)
        self.failUnless(self.computeError(self.LU.sol) < self.tol)

    def testPoisson1dSymmetric(self):
        self.LU = PysparseUmfpackSolver(self.B, strategy='symmetric')
        self.LU.solve(self.b)
        self.failUnless(self.computeError(self.LU.sol) < self.tol)

    def testPoisson1d2by2(self):
        self.LU = PysparseUmfpackSolver(self.B, strategy='2by2')
        self.LU.solve(self.b)
        self.failUnless(self.computeError(self.LU.sol) < self.tol)

    def testPoisson1dNoScaling(self):
        self.LU = PysparseUmfpackSolver(self.B, scale='none')
        self.LU.solve(self.b)
        self.failUnless(self.computeError(self.LU.sol) < self.tol)

    def testPoisson1dScaleMax(self):
        self.LU = PysparseUmfpackSolver(self.B, scale='max')
        self.LU.solve(self.b)
        self.failUnless(self.computeError(self.LU.sol) < self.tol)



        
class Poisson2dTestCase(unittest.TestCase):
    def setUp(self):
        self.n = 200
        self.B = PysparseMatrix( matrix=poisson.poisson2d(self.n) )
        
        self.x_exact = numpy.ones(self.n*self.n)/self.n
        self.normx = 1.0/self.n
        self.b = self.B * self.x_exact

        h = 1.0/self.n
        lmbd_min = 4.0/h/h * (math.sin(math.pi*h/2.0) ** 2 +
                              math.sin(math.pi*h/2.0) ** 2)
        lmbd_max = 4.0/h/h * (math.sin((self.n - 1)*math.pi*h/2.0) ** 2 +
                              math.sin((self.n - 1)*math.pi*h/2.0) ** 2)
        cond = lmbd_max/lmbd_min
        self.tol = cond * macheps()
        self.relerr = 0.0
        self.nnz = self.B.getNnz()
        self.LU = None
        self.fmt = '\t%8.2e  %8.2e  %8d  %8d  %8d  %6.2f  %6.2f\n'

    def computeError(self, x):
        absErr = numpy.linalg.norm(x - self.x_exact, ord=numpy.inf)
        self.relerr = absErr/(1 + self.normx)
        return self.relerr

    def tearDown(self):
        self.LU.fetch_lunz()
        sys.stdout.write(self.fmt % (self.relerr, self.tol, self.nnz,
                                     self.LU.lnz, self.LU.unz,
                                     self.LU.factorizationTime,
                                     self.LU.solutionTime))
        
    def testPoisson2dDefault(self):
        self.LU = PysparseUmfpackSolver(self.B)
        self.LU.solve(self.b)
        self.failUnless(self.computeError(self.LU.sol) < self.tol)

    def testPoisson2dUnsymmetric(self):
        self.LU = PysparseUmfpackSolver(self.B, strategy='unsymmetric')
        self.LU.solve(self.b)
        self.failUnless(self.computeError(self.LU.sol) < self.tol)

    def testPoisson2dSymmetric(self):
        self.LU = PysparseUmfpackSolver(self.B, strategy='symmetric')
        self.LU.solve(self.b)
        self.failUnless(self.computeError(self.LU.sol) < self.tol)

    def testPoisson2d2by2(self):
        self.LU = PysparseUmfpackSolver(self.B, strategy='2by2')
        self.LU.solve(self.b)
        self.failUnless(self.computeError(self.LU.sol) < self.tol)

    def testPoisson2dNoScaling(self):
        self.LU = PysparseUmfpackSolver(self.B, scale='none')
        self.LU.solve(self.b)
        self.failUnless(self.computeError(self.LU.sol) < self.tol)

    def testPoisson2dScaleMax(self):
        self.LU = PysparseUmfpackSolver(self.B, scale='max')
        self.LU.solve(self.b)
        self.failUnless(self.computeError(self.LU.sol) < self.tol)



if __name__ == '__main__':
    headfmt = '\t%8s  %8s  %8s  %8s  %8s  %6s  %6s'
    header = headfmt % ('RelErr','Tol','nnz(A)','nnz(L)',
                        'nnz(U)','Fact','Solve')
    lhead = len(header)
    sys.stderr.write(header + '\n')
    sys.stderr.write('\t' + '-' * lhead + '\n')
    unittest.main()
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.