Ignore:
Timestamp:
Feb 13, 2013, 3:26:15 PM (10 years ago)
Author:
steve
Message:

Rolling in Padarn's update to fit_interpolation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/anuga_core/source/anuga/utilities/test_cg_solve.py

    r7849 r8690  
    203203        assert num.allclose(x,xe)
    204204
     205
     206
     207    def test_sparse_solve_using_c_ext(self):
     208        """Solve Small Sparse Matrix"""
     209
     210        A = [[2.0, -1.0, 0.0, 0.0 ],
     211             [-1.0, 2.0, -1.0, 0.0],
     212             [0.0, -1.0, 2.0, -1.0],
     213             [0.0,0.0, -1.0, 2.0]]
     214
     215        A = Sparse_CSR(Sparse(A))
     216
     217        xe = [0.0, 1.0, 2.0, 3.0]
     218        b  = A*xe
     219        x =  [0.0, 0.0, 0.0, 0.0]
     220
     221        x = conjugate_gradient(A,b,x,use_c_cg=True)
     222
     223        assert num.allclose(x,xe)
     224
     225    def test_max_iter_using_c_ext(self):
     226        """Test max iteration Small Sparse Matrix"""
     227
     228        A = [[2.0, -1.0, 0.0, 0.0 ],
     229             [-1.0, 2.0, -1.0, 0.0],
     230             [0.0, -1.0, 2.0, -1.0],
     231             [0.0,0.0, -1.0, 2.0]]
     232
     233        A = Sparse_CSR(Sparse(A))
     234
     235        xe = [0.0, 1.0, 2.0, 3.0]
     236        b  = A*xe
     237        x =  [0.0, 0.0, 0.0, 0.0]
     238
     239        try:
     240            x = conjugate_gradient(A,b,x,imax=2,use_c_cg=True)
     241        except ConvergenceError:
     242            pass
     243        else:
     244            msg = 'Should have raised exception'
     245            raise TestError, msg
     246
     247
     248    def test_solve_large_using_c_ext(self):
     249        """Standard 1d laplacian """
     250
     251        n = 50
     252        A = Sparse(n,n)
     253
     254        for i in num.arange(0,n):
     255            A[i,i] = 1.0
     256            if i > 0 :
     257                A[i,i-1] = -0.5
     258            if i < n-1 :
     259                A[i,i+1] = -0.5
     260
     261        xe = num.ones( (n,), num.float)
     262
     263        b  = A*xe
     264
     265        A = Sparse_CSR(A)
     266
     267        x = conjugate_gradient(A,b,b,tol=1.0e-5,use_c_cg=True)
     268
     269        assert num.allclose(x,xe)
     270
     271    def test_solve_large_2d_using_c_ext(self):
     272        """Standard 2d laplacian"""
     273
     274        n = 20
     275        m = 10
     276
     277        A = Sparse(m*n, m*n)
     278
     279        for i in num.arange(0,n):
     280            for j in num.arange(0,m):
     281                I = j+m*i
     282                A[I,I] = 4.0
     283                if i > 0  :
     284                    A[I,I-m] = -1.0
     285                if i < n-1 :
     286                    A[I,I+m] = -1.0
     287                if j > 0  :
     288                    A[I,I-1] = -1.0
     289                if j < m-1 :
     290                    A[I,I+1] = -1.0
     291
     292        xe = num.ones( (n*m,), num.float)
     293        A = Sparse_CSR(A)
     294        b  = A*xe
     295        x = conjugate_gradient(A,b,b,iprint=1,use_c_cg=True)
     296
     297        assert num.allclose(x,xe)
     298
     299    def test_solve_large_2d_csr_matrix_using_c_ext(self):
     300        """Standard 2d laplacian with csr format
     301        """
     302
     303        n = 100
     304        m = 100
     305
     306        A = Sparse(m*n, m*n)
     307
     308        for i in num.arange(0,n):
     309            for j in num.arange(0,m):
     310                I = j+m*i
     311                A[I,I] = 4.0
     312                if i > 0  :
     313                    A[I,I-m] = -1.0
     314                if i < n-1 :
     315                    A[I,I+m] = -1.0
     316                if j > 0  :
     317                    A[I,I-1] = -1.0
     318                if j < m-1 :
     319                    A[I,I+1] = -1.0
     320
     321        xe = num.ones( (n*m,), num.float)
     322
     323        # Convert to csr format
     324        #print 'start covert'
     325        A = Sparse_CSR(A)
     326        #print 'finish covert'
     327        b = A*xe
     328        x = conjugate_gradient(A,b,b,iprint=20,use_c_cg=True)
     329
     330        assert num.allclose(x,xe)
     331
     332
     333    def test_solve_large_2d_with_default_guess_using_c_ext(self):
     334        """Standard 2d laplacian using default first guess"""
     335
     336        n = 20
     337        m = 10
     338
     339        A = Sparse(m*n, m*n)
     340
     341        for i in num.arange(0,n):
     342            for j in num.arange(0,m):
     343                I = j+m*i
     344                A[I,I] = 4.0
     345                if i > 0  :
     346                    A[I,I-m] = -1.0
     347                if i < n-1 :
     348                    A[I,I+m] = -1.0
     349                if j > 0  :
     350                    A[I,I-1] = -1.0
     351                if j < m-1 :
     352                    A[I,I+1] = -1.0
     353
     354        xe = num.ones( (n*m,), num.float)
     355        A = Sparse_CSR(A)
     356        b  = A*xe
     357        x = conjugate_gradient(A,b,use_c_cg=True)
     358
     359        assert num.allclose(x,xe)
     360
     361
     362    def test_sparse_solve_matrix_using_c_ext(self):
     363        """Solve Small Sparse Matrix"""
     364
     365        A = [[2.0, -1.0, 0.0, 0.0 ],
     366             [-1.0, 2.0, -1.0, 0.0],
     367             [0.0, -1.0, 2.0, -1.0],
     368             [0.0,0.0, -1.0, 2.0]]
     369
     370        A = Sparse_CSR(Sparse(A))
     371
     372        xe = [[0.0, 0.0],[1.0, 1.0],[2.0 ,2.0],[3.0, 3.0]]
     373        b = A*xe
     374        x = [[0.0, 0.0],[0.0, 0.0],[0.0 ,0.0],[0.0, 0.0]]
     375        x = conjugate_gradient(A,b,x,iprint=0,use_c_cg=True)
     376       
     377
     378        assert num.allclose(x,xe)
     379
     380
     381
     382    def test_sparse_solve_using_c_ext_with_jacobi(self):
     383        """Solve Small Sparse Matrix"""
     384
     385        A = [[2.0, -1.0, 0.0, 0.0 ],
     386             [-1.0, 2.0, -1.0, 0.0],
     387             [0.0, -1.0, 2.0, -1.0],
     388             [0.0,0.0, -1.0, 2.0]]
     389
     390        A = Sparse_CSR(Sparse(A))
     391
     392        xe = [0.0, 1.0, 2.0, 3.0]
     393        b  = A*xe
     394        x =  [0.0, 0.0, 0.0, 0.0]
     395        x = conjugate_gradient(A,b,x,use_c_cg=True,precon='Jacobi')
     396
     397        assert num.allclose(x,xe)
     398
     399    def test_max_iter_using_c_ext_with_jacobi(self):
     400        """Test max iteration Small Sparse Matrix"""
     401
     402        A = [[2.0, -1.0, 0.0, 0.0 ],
     403             [-1.0, 2.0, -1.0, 0.0],
     404             [0.0, -1.0, 2.0, -1.0],
     405             [0.0,0.0, -1.0, 2.0]]
     406
     407        A = Sparse_CSR(Sparse(A))
     408
     409        xe = [0.0, 1.0, 2.0, 3.0]
     410        b  = A*xe
     411        x =  [0.0, 0.0, 0.0, 0.0]
     412
     413        try:
     414            x = conjugate_gradient(A,b,x,imax=2,use_c_cg=True, precon='Jacobi')
     415        except ConvergenceError:
     416            pass
     417        else:
     418            msg = 'Should have raised exception'
     419            raise TestError, msg
     420
     421
     422    def test_solve_large_using_c_ext_with_jacobi(self):
     423        """Standard 1d laplacian """
     424
     425        n = 50
     426        A = Sparse(n,n)
     427
     428        for i in num.arange(0,n):
     429            A[i,i] = 1.0
     430            if i > 0 :
     431                A[i,i-1] = -0.5
     432            if i < n-1 :
     433                A[i,i+1] = -0.5
     434
     435        xe = num.ones( (n,), num.float)
     436
     437        b  = A*xe
     438
     439        A = Sparse_CSR(A)
     440
     441        x = conjugate_gradient(A,b,b,tol=1.0e-5,use_c_cg=True, precon='Jacobi')
     442
     443        assert num.allclose(x,xe)
     444
     445    def test_solve_large_2d_using_c_ext_with_jacobi(self):
     446        """Standard 2d laplacian"""
     447
     448        n = 20
     449        m = 10
     450
     451        A = Sparse(m*n, m*n)
     452
     453        for i in num.arange(0,n):
     454            for j in num.arange(0,m):
     455                I = j+m*i
     456                A[I,I] = 4.0
     457                if i > 0  :
     458                    A[I,I-m] = -1.0
     459                if i < n-1 :
     460                    A[I,I+m] = -1.0
     461                if j > 0  :
     462                    A[I,I-1] = -1.0
     463                if j < m-1 :
     464                    A[I,I+1] = -1.0
     465
     466        xe = num.ones( (n*m,), num.float)
     467        A = Sparse_CSR(A)
     468        b  = A*xe
     469        x = conjugate_gradient(A,b,b,iprint=1,use_c_cg=True, precon='Jacobi')
     470
     471        assert num.allclose(x,xe)
     472
     473    def test_solve_large_2d_csr_matrix_using_c_ext_with_jacobi(self):
     474        """Standard 2d laplacian with csr format
     475        """
     476
     477        n = 100
     478        m = 100
     479
     480        A = Sparse(m*n, m*n)
     481
     482        for i in num.arange(0,n):
     483            for j in num.arange(0,m):
     484                I = j+m*i
     485                A[I,I] = 4.0
     486                if i > 0  :
     487                    A[I,I-m] = -1.0
     488                if i < n-1 :
     489                    A[I,I+m] = -1.0
     490                if j > 0  :
     491                    A[I,I-1] = -1.0
     492                if j < m-1 :
     493                    A[I,I+1] = -1.0
     494
     495        xe = num.ones( (n*m,), num.float)
     496
     497        # Convert to csr format
     498        #print 'start covert'
     499        A = Sparse_CSR(A)
     500        #print 'finish covert'
     501        b = A*xe
     502        x = conjugate_gradient(A,b,b,iprint=20,use_c_cg=True, precon='Jacobi')
     503
     504        assert num.allclose(x,xe)
     505
     506
     507    def test_solve_large_2d_with_default_guess_using_c_ext_with_jacobi(self):
     508        """Standard 2d laplacian using default first guess"""
     509
     510        n = 20
     511        m = 10
     512
     513        A = Sparse(m*n, m*n)
     514
     515        for i in num.arange(0,n):
     516            for j in num.arange(0,m):
     517                I = j+m*i
     518                A[I,I] = 4.0
     519                if i > 0  :
     520                    A[I,I-m] = -1.0
     521                if i < n-1 :
     522                    A[I,I+m] = -1.0
     523                if j > 0  :
     524                    A[I,I-1] = -1.0
     525                if j < m-1 :
     526                    A[I,I+1] = -1.0
     527
     528        xe = num.ones( (n*m,), num.float)
     529        A = Sparse_CSR(A)
     530        b  = A*xe
     531        x = conjugate_gradient(A,b,use_c_cg=True, precon='Jacobi')
     532
     533        assert num.allclose(x,xe)
     534
     535
     536    def test_sparse_solve_matrix_using_c_ext_with_jacobi(self):
     537        """Solve Small Sparse Matrix"""
     538
     539        A = [[2.0, -1.0, 0.0, 0.0 ],
     540             [-1.0, 2.0, -1.0, 0.0],
     541             [0.0, -1.0, 2.0, -1.0],
     542             [0.0,0.0, -1.0, 2.0]]
     543
     544        A = Sparse_CSR(Sparse(A))
     545
     546        xe = [[0.0, 0.0],[1.0, 1.0],[2.0 ,2.0],[3.0, 3.0]]
     547        b = A*xe
     548        x = [[0.0, 0.0],[0.0, 0.0],[0.0 ,0.0],[0.0, 0.0]]
     549        x = conjugate_gradient(A,b,x,iprint=0,use_c_cg=True, precon='Jacobi')
     550       
     551
     552        assert num.allclose(x,xe)
     553
    205554################################################################################
    206555
Note: See TracChangeset for help on using the changeset viewer.