Example #1
0
choice = input('\n Input Choice:\n' + \
           '1:  Run smoothed_aggregation_solver\n' + \
           '2:  Run rootnode_solver\n' )

# Create matrix and candidate vectors.  B has 3 columns, representing 
# rigid body modes of the mesh. B[:,0] and B[:,1] are translations in 
# the X and Y directions while B[:,2] is a rotation.
A,B = linear_elasticity((200,200), format='bsr')

# Construct solver using AMG based on Smoothed Aggregation (SA)
if choice == 1:
    mls = smoothed_aggregation_solver(A, B=B, smooth='energy')
elif choice == 2:
    mls = rootnode_solver(A, B=B, smooth='energy')
else:
    raise ValueError("Enter a choice of 1 or 2")

# Display hierarchy information
print mls

# Create random right hand side
b = scipy.rand(A.shape[0],1)

# Solve Ax=b
residuals = []
x = mls.solve(b, tol=1e-10, residuals=residuals)
print "Number of iterations:  %d\n"%len(residuals)

# Output convergence
print_cycle_history(residuals, mls, verbose=True, plotting=True)
            'symmetry':'hermitian', 'keep':True}
    presmoother =('gauss_seidel', {'sweep':'symmetric', 'iterations':1})
    postsmoother=('gauss_seidel', {'sweep':'symmetric', 'iterations':1})
    
    ##
    # Construct solver and solve
    sa = smoothed_aggregation_solver(A, B=B, smooth=smooth, \
             strength=strength, presmoother=presmoother, \
             postsmoother=postsmoother, **SA_build_args)
    resvec = []
    x = sa.solve(b, x0=x0, residuals=resvec, **SA_solve_args)
    print "\n*************************************************************"
    print "*************************************************************"
    print "Observe that standard SA parameters for this p=5 discontinuous \n" + \
          "Galerkin system yield an inefficient solver.\n"
    print_cycle_history(resvec, sa, verbose=True, plotting=False)

    ##
    # Now, construct and solve with appropriate parameters 
    p = 5
    improve_candidates = [('block_gauss_seidel', {'sweep':'symmetric', 'iterations':p}),
                ('gauss_seidel', {'sweep':'symmetric', 'iterations':p})]   
    aggregate = ['naive', 'standard']
    # the initial conforming aggregation step requires no prolongation smoothing
    smooth=[None, ('energy', {'krylov' : 'cg', 'maxiter' : p})]
    strength =[('distance', {'V' : data['vertices'], 'theta':5e-5, 'relative_drop':False}),\
               ('evolution', {'k':4, 'proj_type':'l2', 'epsilon':2.0})]
    sa = smoothed_aggregation_solver(A, B=B, smooth=smooth, improve_candidates=improve_candidates,\
             strength=strength, presmoother=presmoother, aggregate=aggregate,\
             postsmoother=postsmoother, **SA_build_args)
    resvec = []
Example #3
0
import scipy

from pyamg.gallery import stencil_grid
from pyamg.gallery.diffusion import diffusion_stencil_2d
from pyamg.strength import classical_strength_of_connection
from pyamg.classical.classical import ruge_stuben_solver

from convergence_tools import print_cycle_history

if __name__ == '__main__':
    n = 100
    nx = n
    ny = n

    # Rotated Anisotropic Diffusion
    stencil = diffusion_stencil_2d(type='FE',epsilon=0.001,theta=scipy.pi/3)

    A = stencil_grid(stencil, (nx,ny), format='csr')
    S = classical_strength_of_connection(A, 0.0)

    numpy.random.seed(625)
    x = scipy.rand(A.shape[0])
    b = A*scipy.rand(A.shape[0])

    ml = ruge_stuben_solver(A, max_coarse=10)

    resvec = []
    x = ml.solve(b, x0=x, maxiter=20, tol=1e-14, residuals=resvec)

    print_cycle_history(resvec, ml, verbose=True, plotting=True)
    elif choice == 2:
        sa_symmetric = rootnode_solver(A, B=B, smooth=smooth, \
                   strength=strength, presmoother=presmoother, \
                   postsmoother=postsmoother, **SA_build_args)
    else:
        raise ValueError("Enter a choice of 1 or 2")


    sa_symmetric = smoothed_aggregation_solver(A, B=B, smooth=smooth, \
                   strength=strength, presmoother=presmoother, \
                   postsmoother=postsmoother, **SA_build_args)
    resvec = []
    x = sa_symmetric.solve(b, x0=x0, residuals=resvec, **SA_solve_args)
    print "\nObserve that standard SA parameters for Hermitian systems\n" + \
          "yield a nonconvergent stand-alone solver.\n"
    print_cycle_history(resvec, sa_symmetric, verbose=True, plotting=False)

    ##
    # Now, construct and solve with nonsymmetric SA parameters 
    smooth=('energy', {'krylov' : 'gmres', 'degree':2})
    SA_build_args['symmetry'] = 'nonsymmetric'
    strength=[('evolution', {'k':2, 'epsilon':4.0})]
    presmoother =('gauss_seidel_nr', {'sweep':'symmetric', 'iterations':1})
    postsmoother=('gauss_seidel_nr', {'sweep':'symmetric', 'iterations':1})

    ##
    # Construct solver and solve
    ##
    # Construct solver and solve
    if choice == 1:
        sa_nonsymmetric = smoothed_aggregation_solver(A, B=B, smooth=smooth, \
            'symmetry':'hermitian', 'keep':True}
    presmoother = ('gauss_seidel', {'sweep': 'symmetric', 'iterations': 1})
    postsmoother = ('gauss_seidel', {'sweep': 'symmetric', 'iterations': 1})

    ##
    # Construct solver and solve
    sa = smoothed_aggregation_solver(A, B=B, smooth=smooth, \
             strength=strength, presmoother=presmoother, \
             postsmoother=postsmoother, **SA_build_args)
    resvec = []
    x = sa.solve(b, x0=x0, residuals=resvec, **SA_solve_args)
    print "\n*************************************************************"
    print "*************************************************************"
    print "Observe that standard SA parameters for this p=5 discontinuous \n" + \
          "Galerkin system yield an inefficient solver.\n"
    print_cycle_history(resvec, sa, verbose=True, plotting=False)

    ##
    # Now, construct and solve with appropriate parameters
    p = 5
    improve_candidates = [('block_gauss_seidel', {
        'sweep': 'symmetric',
        'iterations': p
    }), ('gauss_seidel', {
        'sweep': 'symmetric',
        'iterations': p
    })]
    aggregate = ['naive', 'standard']
    # the initial conforming aggregation step requires no prolongation smoothing
    smooth = [None, ('energy', {'krylov': 'cg', 'maxiter': p})]
    strength =[('distance', {'V' : data['vertices'], 'theta':5e-5, 'relative_drop':False}),\
Example #6
0
choice = input('\n Input Choice:\n' + \
           '1:  Run smoothed_aggregation_solver\n' + \
           '2:  Run rootnode_solver\n' )

# Create matrix and candidate vectors.  B has 3 columns, representing
# rigid body modes of the mesh. B[:,0] and B[:,1] are translations in
# the X and Y directions while B[:,2] is a rotation.
A, B = linear_elasticity((200, 200), format='bsr')

# Construct solver using AMG based on Smoothed Aggregation (SA)
if choice == 1:
    mls = smoothed_aggregation_solver(A, B=B, smooth='energy')
elif choice == 2:
    mls = rootnode_solver(A, B=B, smooth='energy')
else:
    raise ValueError("Enter a choice of 1 or 2")

# Display hierarchy information
print mls

# Create random right hand side
b = scipy.rand(A.shape[0], 1)

# Solve Ax=b
residuals = []
x = mls.solve(b, tol=1e-10, residuals=residuals)
print "Number of iterations:  %d\n" % len(residuals)

# Output convergence
print_cycle_history(residuals, mls, verbose=True, plotting=True)
    elif choice == 2:
        sa_symmetric = rootnode_solver(A, B=B, smooth=smooth, \
                   strength=strength, presmoother=presmoother, \
                   postsmoother=postsmoother, **SA_build_args)
    else:
        raise ValueError("Enter a choice of 1 or 2")


    sa_symmetric = smoothed_aggregation_solver(A, B=B, smooth=smooth, \
                   strength=strength, presmoother=presmoother, \
                   postsmoother=postsmoother, **SA_build_args)
    resvec = []
    x = sa_symmetric.solve(b, x0=x0, residuals=resvec, **SA_solve_args)
    print "\nObserve that standard SA parameters for Hermitian systems\n" + \
          "yield a nonconvergent stand-alone solver.\n"
    print_cycle_history(resvec, sa_symmetric, verbose=True, plotting=False)

    ##
    # Now, construct and solve with nonsymmetric SA parameters
    smooth = ('energy', {'krylov': 'gmres', 'degree': 2})
    SA_build_args['symmetry'] = 'nonsymmetric'
    strength = [('evolution', {'k': 2, 'epsilon': 4.0})]
    presmoother = ('gauss_seidel_nr', {'sweep': 'symmetric', 'iterations': 1})
    postsmoother = ('gauss_seidel_nr', {'sweep': 'symmetric', 'iterations': 1})
    improve_candidates = [('gauss_seidel_nr', {
        'sweep': 'symmetric',
        'iterations': 4
    }), None]

    ##
    # Construct solver and solve