-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
115 lines (92 loc) · 2.93 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using AmgclSharp;
namespace AmgclTest;
internal class Program
{
private static void Main()
{
List<int> ptr = new();
List<int> col = new();
List<double> val = new();
List<double> rhs = new();
int n = sample_problem(64, val, col, ptr, rhs);
Amg amg = new();
amg.ParamsCreate();
amg.ParamsSetInt("precond.coarse_enough", 1000);
amg.ParamsSetString("precond.coarsening.type", "smoothed_aggregation");
amg.ParamsSetFloat("precond.coarsening.aggr.eps_strong", 1e-3f);
amg.ParamsSetString("precond.relax.type", "spai0");
amg.ParamsSetString("solver.type", "bicgstabl");
amg.ParamsSetInt("solver.L", 1);
amg.ParamsSetInt("solver.maxiter", 100);
amg.SolverCreate(n, ptr.ToArray(), col.ToArray(), val.ToArray());
amg.ParamsDestroy();
double[] x = new double[n];
amg.SolverSolve(rhs.ToArray(), x);
Console.WriteLine($"Iterations: {amg.Iterations}");
Console.WriteLine($"Error: {amg.Residual}");
amg.SolverDestroy();
}
static int sample_problem(
int n,
List<double> val,
List<int> col,
List<int> ptr,
List<double> rhs,
double anisotropy = 1.0
)
{
int n3 = n * n * n;
ptr.Clear();
col.Clear();
val.Clear();
rhs.Clear();
double hx = 1;
double hy = hx * anisotropy;
double hz = hy * anisotropy;
ptr.Add(0);
for (int k = 0, idx = 0; k < n; ++k)
{
for (int j = 0; j < n; ++j)
{
for (int i = 0; i < n; ++i, ++idx)
{
if (k > 0)
{
col.Add(idx - n * n);
val.Add(-1.0 / (hz * hz));
}
if (j > 0)
{
col.Add(idx - n);
val.Add(-1.0 / (hy * hy));
}
if (i > 0)
{
col.Add(idx - 1);
val.Add(-1.0 / (hx * hx));
}
col.Add(idx);
val.Add((2 / (hx * hx) + 2 / (hy * hy) + 2 / (hz * hz)));
if (i + 1 < n)
{
col.Add(idx + 1);
val.Add(-1.0 / (hx * hx));
}
if (j + 1 < n)
{
col.Add(idx + n);
val.Add(-1.0 / (hy * hy));
}
if (k + 1 < n)
{
col.Add(idx + n * n);
val.Add(-1.0 / (hz * hz));
}
rhs.Add(1.0);
ptr.Add(col.Count());
}
}
}
return n3;
}
}