# Path: sucemfem/Utilities/LinalgSolvers.py
# def solve_sparse_system ( A, b, preconditioner_type='ilu' ):
#     """
#     This function solves the sparse linear system Ax = b for A a scipy sparse matrix, and b a numpy or scipy array
#     
#     By default an incomplete LU preconditioner is used with the bicgstab iterative solver
#     
#     @param A: a square matrix 
#     @param b: the RHS vector
#     @param preconditioner_type: Preconditioner type string
#     """
#                     
#     solver = BiCGStabSolver ( A, preconditioner_type )
# #    solver = BiCGStabSolver ( A ) #removed preconditioner -> 2D Waveguide works!    
#     x = solver.solve(b)
#     return x
# 
# def calculate_residual ( A, x, b ):
#     """
#     Calculate the residual of the system Ax = b
#     
#     @param A: a matrix
#     @param x: a vector
#     @param b: a vector 
#     """
#     return np.linalg.norm( A*x - b.reshape(x.shape) )


import sys
import unittest
import numpy as np
import scipy.sparse
from sucemfem.Utilities.LinalgSolvers import solve_sparse_system, calculate_residual
## SUCEM is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with SUCEM. If not, see <http://www.gnu.org/licenses/>. 
##
## Contact: cemagga@gmail.com 
# Authors:
# Neilen Marais <nmarais@gmail.com>
# Evan Lezar <mail@evanlezar.com>

"""this is a set of test cases for the testing of the linear algebra solvers in FenicsCode/Utilities"""


sys.path.insert(0, '../')
del sys.path[0]


class TestSparseSolver ( unittest.TestCase ):
    def test_sparse_identity ( self ):
        N = 1000;
        A = scipy.sparse.eye ( N, N )
        b = np.random.rand ( N )

        try:
            x = solve_sparse_system ( A, b )
        except AttributeError:
            # Deal with older scipy versions that do not have spilu module

# Path: sucemfem/Utilities/LinalgSolvers.py
# def solve_sparse_system ( A, b, preconditioner_type='ilu' ):
#     """
#     This function solves the sparse linear system Ax = b for A a scipy sparse matrix, and b a numpy or scipy array
#     
#     By default an incomplete LU preconditioner is used with the bicgstab iterative solver
#     
#     @param A: a square matrix 
#     @param b: the RHS vector
#     @param preconditioner_type: Preconditioner type string
#     """
#                     
#     solver = BiCGStabSolver ( A, preconditioner_type )
# #    solver = BiCGStabSolver ( A ) #removed preconditioner -> 2D Waveguide works!    
#     x = solver.solve(b)
#     return x
# 
# def calculate_residual ( A, x, b ):
#     """
#     Calculate the residual of the system Ax = b
#     
#     @param A: a matrix
#     @param x: a vector
#     @param b: a vector 
#     """
#     return np.linalg.norm( A*x - b.reshape(x.shape) )


# Path: sucemfem/Utilities/tests/test_LinalgSolvers.py
import sys
import unittest
import numpy as np
import scipy.sparse
from sucemfem.Utilities.LinalgSolvers import solve_sparse_system, calculate_residual

## SUCEM is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with SUCEM. If not, see <http://www.gnu.org/licenses/>. 
##
## Contact: cemagga@gmail.com 
# Authors:
# Neilen Marais <nmarais@gmail.com>
# Evan Lezar <mail@evanlezar.com>

"""this is a set of test cases for the testing of the linear algebra solvers in FenicsCode/Utilities"""


sys.path.insert(0, '../')
del sys.path[0]


class TestSparseSolver ( unittest.TestCase ):
    def test_sparse_identity ( self ):
        N = 1000;
        A = scipy.sparse.eye ( N, N )
        b = np.random.rand ( N )

        try:
            x = solve_sparse_system ( A, b )
        except AttributeError:
            # Deal with older scipy versions that do not have spilu module

            x = solve_sparse_system ( A, b, preconditioner_type='diagonal')