1+
2+ namespace CSparse . Double . Examples
3+ {
4+ using System ;
5+ using CSparse . Interop . Hypre ;
6+
7+ static class TestHypre
8+ {
9+ public static void Run ( )
10+ {
11+ NativeMethods . HYPRE_Init ( ) ;
12+
13+ int n = 33 ;
14+ var h = 1.0 / ( n + 1 ) ;
15+
16+ var A = CSparse . Double . Generate . Laplacian ( n , n ) ;
17+ var b = Vector . Create ( A . RowCount , h * h ) ;
18+
19+ using ( var hypreA = new HypreMatrix < double > ( A , true ) )
20+ {
21+ Check ( TestAMG ( hypreA , b ) , "AMG" ) ;
22+ Check ( TestPCG ( hypreA , b ) , "PCG" ) ;
23+ Check ( TestPCG_AMG ( hypreA , b ) , "PCG + AMG" ) ;
24+ Check ( TestPCG_ParaSails ( hypreA , b ) , "PCG + ParaSails" ) ;
25+ Check ( TestFlexGMRES_AMG ( hypreA , b ) , "FlexGMRES + AMG" ) ;
26+ }
27+
28+ NativeMethods . HYPRE_Finalize ( ) ;
29+ }
30+
31+ private static void Check ( HypreResult result , string name )
32+ {
33+ Console . WriteLine ( name ) ;
34+ Console . WriteLine ( " Iterations = {0}" , result . NumIterations ) ;
35+ Console . WriteLine ( " Final Relative Residual Norm = {0}" , result . RelResidualNorm ) ;
36+ Console . WriteLine ( ) ;
37+ }
38+
39+ private static HypreResult TestAMG ( HypreMatrix < double > A , double [ ] _b )
40+ {
41+ var _x = Vector . Create ( A . RowCount , 0.0 ) ;
42+
43+ using var x = new HypreVector < double > ( _x ) ;
44+ using var b = new HypreVector < double > ( _b ) ;
45+
46+ using var solver = new BoomerAMG < double > ( ) ;
47+
48+ solver . SetOldDefault ( ) ; // Falgout coarsening with modified classical interpolation
49+ solver . SetRelaxType ( 3 ) ; // G-S/Jacobi hybrid relaxation
50+ solver . SetRelaxOrder ( 1 ) ; // uses C/F relaxation
51+ solver . SetNumSweeps ( 1 ) ; // Sweeps on each level
52+ solver . SetMaxLevels ( 20 ) ;
53+ solver . SetTol ( 1e-7 ) ;
54+ //solver.PrintLevel = 3;
55+
56+ return solver . Solve ( A , x , b ) ;
57+ }
58+
59+ private static HypreResult TestPCG ( HypreMatrix < double > A , double [ ] _b )
60+ {
61+ var _x = Vector . Create ( A . RowCount , 0.0 ) ;
62+
63+ using var x = new HypreVector < double > ( _x ) ;
64+ using var b = new HypreVector < double > ( _b ) ;
65+
66+ using var solver = new PCG < double > ( ) ;
67+
68+ solver . SetMaxIter ( 1000 ) ;
69+ solver . SetTol ( 1e-7 ) ;
70+ solver . SetTwoNorm ( 1 ) ; // use the two norm as the stopping criteria
71+ //solver.PrintLevel = 2;
72+ //solver.Logging = 1;
73+
74+ return solver . Solve ( A , x , b ) ;
75+ }
76+
77+ private static HypreResult TestPCG_AMG ( HypreMatrix < double > A , double [ ] _b )
78+ {
79+ var _x = Vector . Create ( A . RowCount , 0.0 ) ;
80+
81+ using var x = new HypreVector < double > ( _x ) ;
82+ using var b = new HypreVector < double > ( _b ) ;
83+
84+ using var precond = new BoomerAMG < double > ( ) ;
85+
86+ //precond.PrintLevel = 1;
87+ precond . SetCoarsenType ( 6 ) ;
88+ precond . SetOldDefault ( ) ;
89+ precond . SetRelaxType ( 6 ) ; // Sym G.S./Jacobi hybrid
90+ precond . SetNumSweeps ( 1 ) ;
91+ precond . SetTol ( 0.0 ) ;
92+ precond . SetMaxIter ( 1 ) ; // do only one iteration!
93+
94+ using var solver = new PCG < double > ( precond ) ;
95+
96+ solver . SetMaxIter ( 1000 ) ;
97+ solver . SetTol ( 1e-7 ) ;
98+ solver . SetTwoNorm ( 1 ) ; // use the two norm as the stopping criteria
99+ //solver.PrintLevel = 2;
100+ //solver.Logging = 1;
101+
102+ return solver . Solve ( A , x , b ) ;
103+ }
104+
105+ private static HypreResult TestPCG_ParaSails ( HypreMatrix < double > A , double [ ] _b )
106+ {
107+ var _x = Vector . Create ( A . RowCount , 0.0 ) ;
108+
109+ using var x = new HypreVector < double > ( _x ) ;
110+ using var b = new HypreVector < double > ( _b ) ;
111+
112+ using var precond = new ParaSails < double > ( true ) ;
113+
114+ precond . SetParams ( 0.1 , 1 ) ;
115+ precond . SetFilter ( 0.05 ) ;
116+ //precond.Logging = 3;
117+
118+ using var solver = new PCG < double > ( precond ) ;
119+
120+ solver . SetMaxIter ( 1000 ) ;
121+ solver . SetTol ( 1e-7 ) ;
122+ solver . SetTwoNorm ( 1 ) ; // use the two norm as the stopping criteria
123+ //solver.PrintLevel = 2;
124+ //solver.Logging = 1;
125+
126+ return solver . Solve ( A , x , b ) ;
127+ }
128+
129+ private static HypreResult TestFlexGMRES_AMG ( HypreMatrix < double > A , double [ ] _b )
130+ {
131+ var _x = Vector . Create ( A . RowCount , 0.0 ) ;
132+
133+ using var x = new HypreVector < double > ( _x ) ;
134+ using var b = new HypreVector < double > ( _b ) ;
135+
136+ using var precond = new BoomerAMG < double > ( ) ;
137+
138+ //precond.PrintLevel = 1;
139+ precond . SetCoarsenType ( 6 ) ;
140+ precond . SetOldDefault ( ) ;
141+ precond . SetRelaxType ( 6 ) ; // Sym G.S./Jacobi hybrid
142+ precond . SetNumSweeps ( 1 ) ;
143+ precond . SetTol ( 0.0 ) ; // conv. tolerance zero
144+ precond . SetMaxIter ( 1 ) ; // do only one iteration!
145+
146+ using var solver = new FlexGMRES < double > ( precond ) ;
147+
148+ solver . SetKDim ( 30 ) ;
149+ solver . SetMaxIter ( 1000 ) ;
150+ solver . SetTol ( 1e-7 ) ;
151+ //solver.PrintLevel = 2;
152+ //solver.Logging = 1;
153+
154+ return solver . Solve ( A , x , b ) ;
155+ }
156+ }
157+ }
0 commit comments