summaryrefslogtreecommitdiffstatshomepage
path: root/src/lib/netlist/solver/nld_ms_w.h
blob: 3470cd86f2216972deaec0fe2faf30993b7d8b8b (plain) (blame)
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
// license:GPL-2.0+
// copyright-holders:Couriersud
/*
 * nld_ms_direct.h
 *
 *
 * Woodbury Solver
 *
 * Computes the updated solution of A given that the change in A is
 *
 * A <- A + (U x transpose(V))   U,V matrices
 *
 * The approach is describes in "Numerical Recipes in C", Second edition, Page 75ff
 *
 * Whilst the book proposes to invert the matrix R=(I+transpose(V)*Z) we define
 *
 *       w = transpose(V)*y
 *       a = R^-1 * w
 *
 * and consequently
 *
 *       R * a = w
 *
 * And solve for a using Gaussian elimination. This is a lot faster.
 *
 * One fact omitted in the book is the fact that actually the matrix Z which contains
 * in it's columns the solutions of
 *
 *      A * zk = uk
 *
 * for uk being unit vectors for full rank (max(k) == n) is identical to the
 * inverse of A.
 *
 * The approach performs relatively well for matrices up to n ~ 40 (kidniki using frontiers).
 * Kidniki without frontiers has n==88. Here, the average number of Newton-Raphson
 * loops increase to 20. It looks like that the approach for larger matrices
 * introduces numerical instability.
 */

#ifndef NLD_MS_W_H_
#define NLD_MS_W_H_

#include "nld_matrix_solver.h"
#include "nld_solver.h"
#include "plib/vector_ops.h"

#include <algorithm>

namespace netlist
{
namespace solver
{

	template <typename FT, int SIZE>
	class matrix_solver_w_t: public matrix_solver_ext_t<FT, SIZE>
	{
		friend class matrix_solver_t;

	public:
		using float_ext_type = FT;
		using float_type = FT;

		// FIXME: dirty hack to make this compile
		static constexpr const std::size_t storage_N = 100;

		matrix_solver_w_t(netlist_state_t &anetlist, const pstring &name,
			const analog_net_t::list_t &nets,
			const solver_parameters_t *params, const std::size_t size)
		: matrix_solver_ext_t<FT, SIZE>(anetlist, name, nets, params, size)
		, m_cnt(0)
		{
			this->build_mat_ptr(m_A);
		}

		void reset() override { matrix_solver_t::reset(); }

	protected:
		unsigned vsolve_non_dynamic(const bool newton_raphson) override;
		unsigned solve_non_dynamic(const bool newton_raphson);

		void LE_invert();

		template <typename T>
		void LE_compute_x(T & x);


		template <typename T1, typename T2>
		float_ext_type &A(const T1 &r, const T2 &c) { return m_A[r][c]; }
		template <typename T1, typename T2>
		float_ext_type &W(const T1 &r, const T2 &c) { return m_W[r][c]; }

		/* access to Ainv for fixed columns over row, there store transposed */
		template <typename T1, typename T2>
		float_ext_type &Ainv(const T1 &r, const T2 &c) { return m_Ainv[c][r]; }
		template <typename T1>
		float_ext_type &RHS(const T1 &r) { return m_RHS[r]; }


		template <typename T1, typename T2>
		float_ext_type &lA(const T1 &r, const T2 &c) { return m_lA[r][c]; }


	private:
		template <typename T, std::size_t N, std::size_t M>
		using array2D = std::array<std::array<T, M>, N>;
		static constexpr std::size_t m_pitch  = (((  storage_N) + 7) / 8) * 8;
		array2D<float_ext_type, storage_N, m_pitch> m_A;
		array2D<float_ext_type, storage_N, m_pitch> m_Ainv;
		array2D<float_ext_type, storage_N, m_pitch> m_W;
		std::array<float_ext_type, storage_N> m_RHS; // right hand side - contains currents

		array2D<float_ext_type, storage_N, m_pitch> m_lA;

		/* temporary */
		array2D<float_ext_type, storage_N, m_pitch> H;
		std::array<unsigned, storage_N> rows;
		array2D<unsigned, storage_N, m_pitch> cols;
		std::array<unsigned, storage_N> colcount;

		unsigned m_cnt;
	};

	// ----------------------------------------------------------------------------------------
	// matrix_solver_direct
	// ----------------------------------------------------------------------------------------

	template <typename FT, int SIZE>
	void matrix_solver_w_t<FT, SIZE>::LE_invert()
	{
		const std::size_t kN = this->size();

		for (std::size_t i = 0; i < kN; i++)
		{
			for (std::size_t j = 0; j < kN; j++)
			{
				W(i,j) = lA(i,j) = A(i,j);
				Ainv(i,j) = 0.0;
			}
			Ainv(i,i) = 1.0;
		}
		/* down */
		for (std::size_t i = 0; i < kN; i++)
		{
			/* FIXME: Singular matrix? */
			const float_type f = 1.0 / W(i,i);
			const auto * const p = this->m_terms[i].m_nzrd.data();
			const size_t e = this->m_terms[i].m_nzrd.size();

			/* Eliminate column i from row j */

			const auto * const pb = this->m_terms[i].m_nzbd.data();
			const size_t eb = this->m_terms[i].m_nzbd.size();
			for (std::size_t jb = 0; jb < eb; jb++)
			{
				const auto j = pb[jb];
				const float_type f1 = - W(j,i) * f;
				if (f1 != 0.0)
				{
					for (std::size_t k = 0; k < e; k++)
						W(j,p[k]) += W(i,p[k]) * f1;
					for (std::size_t k = 0; k <= i; k ++)
						Ainv(j,k) += Ainv(i,k) * f1;
				}
			}
		}
		/* up */
		for (std::size_t i = kN; i-- > 0; )
		{
			/* FIXME: Singular matrix? */
			const float_type f = 1.0 / W(i,i);
			for (std::size_t j = i; j-- > 0; )
			{
				const float_type f1 = - W(j,i) * f;
				if (f1 != 0.0)
				{
					for (std::size_t k = i; k < kN; k++)
						W(j,k) += W(i,k) * f1;
					for (std::size_t k = 0; k < kN; k++)
						Ainv(j,k) += Ainv(i,k) * f1;
				}
			}
			for (std::size_t k = 0; k < kN; k++)
			{
				Ainv(i,k) *= f;
			}
		}
	}

	template <typename FT, int SIZE>
	template <typename T>
	void matrix_solver_w_t<FT, SIZE>::LE_compute_x(
			T & x)
	{
		const std::size_t kN = this->size();

		for (std::size_t i=0; i<kN; i++)
			x[i] = 0.0;

		for (std::size_t k=0; k<kN; k++)
		{
			const float_type f = RHS(k);

			for (std::size_t i=0; i<kN; i++)
				x[i] += Ainv(i,k) * f;
		}
	}


	template <typename FT, int SIZE>
	unsigned matrix_solver_w_t<FT, SIZE>::solve_non_dynamic(const bool newton_raphson)
	{
		const auto iN = this->size();

		// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
		std::array<float_type, storage_N> new_V;
		// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
		std::array<float_type, storage_N> t;  // FIXME: convert to member
		// NOLINTNEXTLINE(cppcoreguidelines-pro-type-member-init)
		std::array<float_type, storage_N> w;

		if ((m_cnt % 50) == 0)
		{
			/* complete calculation */
			this->LE_invert();
			this->LE_compute_x(new_V);
		}
		else
		{
			/* Solve Ay = b for y */
			this->LE_compute_x(new_V);

			/* determine changed rows */

			unsigned rowcount=0;
			#define VT(r,c) (A(r,c) - lA(r,c))

			for (unsigned row = 0; row < iN; row ++)
			{
				unsigned cc=0;
				auto &nz = this->m_terms[row].m_nz;
				for (auto & col : nz)
				{
					if (A(row,col) != lA(row,col))
						cols[rowcount][cc++] = col;
				}
				if (cc > 0)
				{
					colcount[rowcount] = cc;
					rows[rowcount++] = row;
				}
			}
			if (rowcount > 0)
			{
				/* construct w = transform(V) * y
				 * dim: rowcount x iN
				 * */
				for (unsigned i = 0; i < rowcount; i++)
				{
					const unsigned r = rows[i];
					nl_fptype tmp = 0.0;
					for (unsigned k = 0; k < iN; k++)
						tmp += VT(r,k) * new_V[k];
					w[i] = tmp;
				}

				for (unsigned i = 0; i < rowcount; i++)
					for (unsigned k=0; k< rowcount; k++)
						H[i][k] = 0.0;

				for (unsigned i = 0; i < rowcount; i++)
					H[i][i] = 1.0;
				/* Construct H = (I + VT*Z) */
				for (unsigned i = 0; i < rowcount; i++)
					for (unsigned k=0; k< colcount[i]; k++)
					{
						const unsigned col = cols[i][k];
						float_type f = VT(rows[i],col);
						if (f!=0.0)
							for (unsigned j= 0; j < rowcount; j++)
								H[i][j] += f * Ainv(col,rows[j]);
					}

				/* Gaussian elimination of H */
				for (unsigned i = 0; i < rowcount; i++)
				{
					if (H[i][i] == 0.0)
						plib::perrlogger("{} H singular\n", this->name());
					const float_type f = 1.0 / H[i][i];
					for (unsigned j = i+1; j < rowcount; j++)
					{
						const float_type f1 = - f * H[j][i];

						if (f1!=0.0)
						{
							float_type *pj = &H[j][i+1];
							const float_type *pi = &H[i][i+1];
							for (unsigned k = 0; k < rowcount-i-1; k++)
								pj[k] += f1 * pi[k];
								//H[j][k] += f1 * H[i][k];
							w[j] += f1 * w[i];
						}
					}
				}
				/* Back substitution */
				//inv(H) w = t     w = H t
				for (unsigned j = rowcount; j-- > 0; )
				{
					float_type tmp = 0;
					const float_type *pj = &H[j][j+1];
					const float_type *tj = &t[j+1];
					for (unsigned k = 0; k < rowcount-j-1; k++)
						tmp += pj[k] * tj[k];
						//tmp += H[j][k] * t[k];
					t[j] = (w[j] - tmp) / H[j][j];
				}

				/* x = y - Zt */
				for (unsigned i=0; i<iN; i++)
				{
					float_type tmp = 0.0;
					for (unsigned j=0; j<rowcount;j++)
					{
						const unsigned row = rows[j];
						tmp += Ainv(i,row) * t[j];
					}
					new_V[i] -= tmp;
				}
			}
		}
		m_cnt++;

		if (false)
			for (unsigned i=0; i<iN; i++)
			{
				float_type tmp = 0.0;
				for (unsigned j=0; j<iN; j++)
				{
					tmp += A(i,j) * new_V[j];
				}
				if (std::abs(tmp-RHS(i)) > 1e-6)
					plib::perrlogger("{} failed on row {}: {} RHS: {}\n", this->name(), i, std::abs(tmp-RHS(i)), RHS(i));
			}

		const float_type err = (newton_raphson ? this->delta(new_V) : 0.0);
		this->store(new_V);
		return (err > this->m_params.m_accuracy) ? 2 : 1;
	}

	template <typename FT, int SIZE>
	unsigned matrix_solver_w_t<FT, SIZE>::vsolve_non_dynamic(const bool newton_raphson)
	{
		this->clear_square_mat(this->m_A);
		this->fill_matrix(this->m_RHS);

		this->m_stat_calculations++;
		return this->solve_non_dynamic(newton_raphson);
	}

} // namespace solver
} // namespace netlist

#endif /* NLD_MS_DIRECT_H_ */