lixiaojun
2023-04-12 fc6b7c9852f18e42fb9bccaf0cc22fbe5389d179
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
// <copyright file="Milu0.cs" company="Math.NET">
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
//
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
 
using System;
using IStation.Numerics.LinearAlgebra.Solvers;
using IStation.Numerics.LinearAlgebra.Storage;
 
namespace IStation.Numerics.LinearAlgebra.Complex32.Solvers
{
    using Numerics;
 
    /// <summary>
    /// A simple milu(0) preconditioner.
    /// </summary>
    /// <remarks>
    /// Original Fortran code by Yousef Saad (07 January 2004)
    /// </remarks>
    public sealed class MILU0Preconditioner : IPreconditioner<Complex32>
    {
        // Matrix stored in Modified Sparse Row (MSR) format containing the L and U
        // factors together.
 
        // The diagonal (stored in alu(0:n-1) ) is inverted. Each i-th row of the matrix
        // contains the i-th row of L (excluding the diagonal entry = 1) followed by
        // the i-th row of U.
        private Complex32[] _alu;
 
        // The row pointers (stored in jlu(0:n) ) and column indices to off-diagonal elements.
        private int[] _jlu;
 
        // Pointer to the diagonal elements in MSR storage (for faster LU solving).
        private int[] _diag;
 
        /// <param name="modified">Use modified or standard ILU(0)</param>
        public MILU0Preconditioner(bool modified = true)
        {
            UseModified = modified;
        }
 
        /// <summary>
        /// Gets or sets a value indicating whether to use modified or standard ILU(0).
        /// </summary>
        public bool UseModified { get; set; }
 
        /// <summary>
        /// Gets a value indicating whether the preconditioner is initialized.
        /// </summary>
        public bool IsInitialized { get; private set; }
 
        /// <summary>
        /// Initializes the preconditioner and loads the internal data structures.
        /// </summary>
        /// <param name="matrix">The matrix upon which the preconditioner is based. </param>
        /// <exception cref="ArgumentNullException">If <paramref name="matrix"/> is <see langword="null" />.</exception>
        /// <exception cref="ArgumentException">If <paramref name="matrix"/> is not a square or is not an
        /// instance of SparseCompressedRowMatrixStorage.</exception>
        public void Initialize(Matrix<Complex32> matrix)
        {
            var csr = matrix.Storage as SparseCompressedRowMatrixStorage<Complex32>;
            if (csr == null)
            {
                throw new ArgumentException("Matrix must be in sparse storage format", nameof(matrix));
            }
 
            // Dimension of matrix
            int n = csr.RowCount;
            if (n != csr.ColumnCount)
            {
                throw new ArgumentException("Matrix must be square.", nameof(matrix));
            }
 
            // Original matrix compressed sparse row storage.
            Complex32[] a = csr.Values;
            int[] ja = csr.ColumnIndices;
            int[] ia = csr.RowPointers;
 
            _alu = new Complex32[ia[n] + 1];
            _jlu = new int[ia[n] + 1];
            _diag = new int[n];
 
            int code = Compute(n, a, ja, ia, _alu, _jlu, _diag, UseModified);
            if (code > -1)
            {
                throw new NumericalBreakdownException("Zero pivot encountered on row " + code + " during ILU process");
            }
 
            IsInitialized = true;
        }
 
        /// <summary>
        /// Approximates the solution to the matrix equation <b>Ax = b</b>.
        /// </summary>
        /// <param name="input">The right hand side vector b.</param>
        /// <param name="result">The left hand side vector x.</param>
        public void Approximate(Vector<Complex32> input, Vector<Complex32> result)
        {
            if (_alu == null)
            {
                throw new ArgumentException("The requested matrix does not exist.");
            }
 
            if ((result.Count != input.Count) || (result.Count != _diag.Length))
            {
                throw new ArgumentException("All vectors must have the same dimensionality.");
            }
 
            int n = _diag.Length;
 
            // Forward solve.
            for (int i = 0; i < n; i++)
            {
                result[i] = input[i];
                for (int k = _jlu[i]; k < _diag[i]; k++)
                {
                    result[i] = result[i] - _alu[k] * result[_jlu[k]];
                }
            }
 
            // Backward solve.
            for (int i = n - 1; i >= 0; i--)
            {
                for (int k = _diag[i]; k < _jlu[i + 1]; k++)
                {
                    result[i] = result[i] - _alu[k] * result[_jlu[k]];
                }
                result[i] = _alu[i] * result[i];
            }
        }
 
        /// <summary>
        /// MILU0 is a simple milu(0) preconditioner.
        /// </summary>
        /// <param name="n">Order of the matrix.</param>
        /// <param name="a">Matrix values in CSR format (input).</param>
        /// <param name="ja">Column indices (input).</param>
        /// <param name="ia">Row pointers (input).</param>
        /// <param name="alu">Matrix values in MSR format (output).</param>
        /// <param name="jlu">Row pointers and column indices (output).</param>
        /// <param name="ju">Pointer to diagonal elements (output).</param>
        /// <param name="modified">True if the modified/MILU algorithm should be used (recommended)</param>
        /// <returns>Returns 0 on success or k > 0 if a zero pivot was encountered at step k.</returns>
        private int Compute(int n, Complex32[] a, int[] ja, int[] ia, Complex32[] alu, int[] jlu, int[] ju, bool modified)
        {
            var iw = new int[n];
            int i;
 
            // Set initial pointer value.
            int p = n + 1;
            jlu[0] = p;
 
            // Initialize work vector.
            for (i = 0; i < n; i++)
            {
                iw[i] = -1;
            }
 
            // The main loop.
            for (i = 0; i < n; i++)
            {
                int pold = p;
 
                // Generating row i of L and U.
                int j;
                for (j = ia[i]; j < ia[i + 1]; j++)
                {
                    // Copy row i of A, JA, IA into row i of ALU, JLU (LU matrix).
                    int jcol = ja[j];
 
                    if (jcol == i)
                    {
                        alu[i] = a[j];
                        iw[jcol] = i;
                        ju[i] = p;
                    }
                    else
                    {
                        alu[p] = a[j];
                        jlu[p] = ja[j];
                        iw[jcol] = p;
                        p = p + 1;
                    }
                }
 
                jlu[i + 1] = p;
 
                Complex32 s = Complex32.Zero;
 
                int k;
                for (j = pold; j < ju[i]; j++)
                {
                    int jrow = jlu[j];
                    Complex32 tl = alu[j] * alu[jrow];
                    alu[j] = tl;
 
                    // Perform linear combination.
                    for (k = ju[jrow]; k < jlu[jrow + 1]; k++)
                    {
                        int jw = iw[jlu[k]];
                        if (jw != -1)
                        {
                            alu[jw] = alu[jw] - tl * alu[k];
                        }
                        else
                        {
                            // Accumulate fill-in values.
                            s = s + tl * alu[k];
                        }
                    }
                }
 
                if (modified)
                {
                    alu[i] = alu[i] - s;
                }
 
                if (alu[i] == Complex32.Zero)
                {
                    return i;
                }
 
                // Invert and store diagonal element.
                alu[i] = 1.0f / alu[i];
 
                // Reset pointers in work array.
                iw[i] = -1;
                for (k = pold; k < p; k++)
                {
                    iw[jlu[k]] = -1;
                }
            }
 
            return -1;
        }
    }
}