[421b7d2] | 1 | /* matrix.c |
---|
[d1b1380] | 2 | * Matrix building and solving routines |
---|
[2d8d46d] | 3 | * Copyright (C) 1993-2003,2010,2013,2024 Olly Betts |
---|
[846746e] | 4 | * |
---|
[89231c4] | 5 | * This program is free software; you can redistribute it and/or modify |
---|
| 6 | * it under the terms of the GNU General Public License as published by |
---|
| 7 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 8 | * (at your option) any later version. |
---|
[846746e] | 9 | * |
---|
| 10 | * This program is distributed in the hope that it will be useful, |
---|
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
[89231c4] | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 13 | * GNU General Public License for more details. |
---|
[846746e] | 14 | * |
---|
[89231c4] | 15 | * You should have received a copy of the GNU General Public License |
---|
| 16 | * along with this program; if not, write to the Free Software |
---|
[ecbc6c18] | 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
[d1b1380] | 18 | */ |
---|
| 19 | |
---|
[2164fa4] | 20 | /*#define SOR 1*/ |
---|
[702f518] | 21 | |
---|
[032ed06] | 22 | #if 0 |
---|
| 23 | # define DEBUG_INVALID 1 |
---|
| 24 | #endif |
---|
| 25 | |
---|
[4c83f84] | 26 | #include <config.h> |
---|
[d1b1380] | 27 | |
---|
| 28 | #include "debug.h" |
---|
[a420b49] | 29 | #include "cavern.h" |
---|
[c082b69] | 30 | #include "filename.h" |
---|
| 31 | #include "message.h" |
---|
[d1b1380] | 32 | #include "netbits.h" |
---|
| 33 | #include "matrix.h" |
---|
| 34 | #include "out.h" |
---|
| 35 | |
---|
| 36 | #undef PRINT_MATRICES |
---|
| 37 | #define PRINT_MATRICES 0 |
---|
| 38 | |
---|
| 39 | #undef DEBUG_MATRIX_BUILD |
---|
| 40 | #define DEBUG_MATRIX_BUILD 0 |
---|
| 41 | |
---|
| 42 | #undef DEBUG_MATRIX |
---|
| 43 | #define DEBUG_MATRIX 0 |
---|
| 44 | |
---|
| 45 | #if PRINT_MATRICES |
---|
[9965b2b] | 46 | static void print_matrix(real *M, real *B, long n); |
---|
[d1b1380] | 47 | #endif |
---|
| 48 | |
---|
[9965b2b] | 49 | static void choleski(real *M, real *B, long n); |
---|
[3fde384f] | 50 | |
---|
[d1b1380] | 51 | #ifdef SOR |
---|
[9965b2b] | 52 | static void sor(real *M, real *B, long n); |
---|
[d1b1380] | 53 | #endif |
---|
| 54 | |
---|
[a420b49] | 55 | /* for M(row, col) col must be <= row, so Y <= X */ |
---|
[9965b2b] | 56 | # define M(X, Y) ((real *)M)[((((OSSIZE_T)(X)) * ((X) + 1)) >> 1) + (Y)] |
---|
[421b7d2] | 57 | /* +(Y>X?0*printf("row<col (line %d)\n",__LINE__):0) */ |
---|
[9965b2b] | 58 | /*#define M_(X, Y) ((real *)M)[((((OSSIZE_T)(Y)) * ((Y) + 1)) >> 1) + (X)]*/ |
---|
[d1b1380] | 59 | |
---|
[2d8d46d] | 60 | #define COLOUR_FIXED -1 |
---|
| 61 | #define COLOUR_TODO -2 |
---|
[d1b1380] | 62 | |
---|
[2d8d46d] | 63 | static void set_row(node *stn, node *from, int row_number) { |
---|
| 64 | // We store the matrix row/column index in stn->colour for quick and easy |
---|
| 65 | // lookup when copying out the solved station coordinates. |
---|
| 66 | stn->colour = row_number; |
---|
| 67 | for (int d = 0; d < 3; d++) { |
---|
| 68 | linkfor *leg = stn->leg[d]; |
---|
| 69 | if (!leg) break; |
---|
| 70 | node *to = leg->l.to; |
---|
| 71 | if (to == from || to->colour != COLOUR_TODO) { |
---|
| 72 | continue; |
---|
| 73 | } |
---|
| 74 | if (fZeros(data_here(leg) ? &leg->v : &reverse_leg(leg)->v)) { |
---|
| 75 | set_row(to, stn, row_number); |
---|
| 76 | } |
---|
| 77 | } |
---|
| 78 | } |
---|
[d1b1380] | 79 | |
---|
[2d8d46d] | 80 | #ifdef NO_COVARIANCES |
---|
| 81 | # define FACTOR 1 |
---|
| 82 | #else |
---|
| 83 | # define FACTOR 3 |
---|
| 84 | #endif |
---|
[d1b1380] | 85 | |
---|
[032ed06] | 86 | extern void |
---|
[d9b5db53] | 87 | solve_matrix(node *list) |
---|
[032ed06] | 88 | { |
---|
| 89 | node *stn; |
---|
[2d8d46d] | 90 | bool unfixed_stations = false; |
---|
[d9b5db53] | 91 | FOR_EACH_STN(stn, list) { |
---|
[103c026] | 92 | if (!fixed(stn)) { |
---|
[2d8d46d] | 93 | unfixed_stations = true; |
---|
| 94 | stn->colour = COLOUR_TODO; |
---|
[103c026] | 95 | } else { |
---|
[2d8d46d] | 96 | stn->colour = COLOUR_FIXED; |
---|
[103c026] | 97 | } |
---|
[032ed06] | 98 | } |
---|
[2d8d46d] | 99 | if (!unfixed_stations) { |
---|
| 100 | return; |
---|
| 101 | } |
---|
[032ed06] | 102 | |
---|
[2d8d46d] | 103 | // Assign a matrix row/column index to each group of stations with the same |
---|
| 104 | // pos. |
---|
| 105 | long n = 0; |
---|
[2164fa4] | 106 | FOR_EACH_STN(stn, list) { |
---|
[2d8d46d] | 107 | if (stn->colour == COLOUR_TODO) { |
---|
| 108 | set_row(stn, NULL, n++); |
---|
| 109 | } |
---|
[2164fa4] | 110 | } |
---|
[2d8d46d] | 111 | SVX_ASSERT(n > 0); |
---|
[d1b1380] | 112 | |
---|
[2d8d46d] | 113 | // Array to map from row/column index to pos. We fill this in as we build |
---|
| 114 | // the matrix, and use it to know where to copy the solved station |
---|
| 115 | // coordinates to. |
---|
| 116 | pos **stn_tab = osmalloc((OSSIZE_T)(n * ossizeof(pos*))); |
---|
[3fde384f] | 117 | |
---|
[2d8d46d] | 118 | /* (OSSIZE_T) cast may be needed if n >= 181 */ |
---|
| 119 | real *M = osmalloc((OSSIZE_T)((((OSSIZE_T)n * FACTOR * (n * FACTOR + 1)) >> 1)) * ossizeof(real)); |
---|
| 120 | real *B = osmalloc((OSSIZE_T)(n * FACTOR * ossizeof(real))); |
---|
[dbd68203] | 121 | |
---|
[647407d] | 122 | if (!fQuiet) { |
---|
[2d8d46d] | 123 | if (n == 1) |
---|
[a4adf09] | 124 | out_current_action(msg(/*Solving one equation*/78)); |
---|
| 125 | else |
---|
[2d8d46d] | 126 | out_current_action1(msg(/*Solving %d simultaneous equations*/75), n); |
---|
[dbd68203] | 127 | } |
---|
| 128 | |
---|
[3fde384f] | 129 | #ifdef NO_COVARIANCES |
---|
[5bb3dc4] | 130 | int dim = 2; |
---|
[3fde384f] | 131 | #else |
---|
[2d8d46d] | 132 | int dim = 0; /* Collapse loop to a single iteration. */ |
---|
[3fde384f] | 133 | #endif |
---|
[a420b49] | 134 | for ( ; dim >= 0; dim--) { |
---|
[907fe10] | 135 | /* Initialise M and B to zero - zeroing "linearly" will minimise |
---|
[421b7d2] | 136 | * paging when the matrix is large */ |
---|
[66de220] | 137 | { |
---|
[2d8d46d] | 138 | int end = n * FACTOR; |
---|
| 139 | for (int row = 0; row < end; row++) B[row] = (real)0.0; |
---|
| 140 | end = ((OSSIZE_T)n * FACTOR * (n * FACTOR + 1)) >> 1; |
---|
| 141 | for (int row = 0; row < end; row++) M[row] = (real)0.0; |
---|
[66de220] | 142 | } |
---|
[dbd68203] | 143 | |
---|
[3c7ab9a] | 144 | /* Construct matrix by going through the stn list. |
---|
[421b7d2] | 145 | * |
---|
[907fe10] | 146 | * All legs between two fixed stations can be ignored here. |
---|
[421b7d2] | 147 | * |
---|
[3c7ab9a] | 148 | * Other legs we want to add exactly once to M. To achieve this we |
---|
[07ff034] | 149 | * want to: |
---|
[3c7ab9a] | 150 | * |
---|
| 151 | * - add forward legs between two unfixed stations, |
---|
| 152 | * |
---|
| 153 | * - add legs from unfixed stations to fixed stations (we do them from |
---|
| 154 | * the unfixed end so we don't need to detect when we're at a fixed |
---|
| 155 | * point cut line and determine which side we're currently dealing |
---|
| 156 | * with). |
---|
| 157 | * |
---|
| 158 | * To implement this, we only look at legs from unfixed stations and add |
---|
| 159 | * a leg if to a fixed station, or to an unfixed station and it's a |
---|
| 160 | * forward leg. |
---|
| 161 | */ |
---|
[d9b5db53] | 162 | FOR_EACH_STN(stn, list) { |
---|
[2d8d46d] | 163 | if (dim == 0) { |
---|
| 164 | if (stn->colour != COLOUR_FIXED) { |
---|
| 165 | stn_tab[stn->colour] = stn->name->pos; |
---|
| 166 | } |
---|
| 167 | } |
---|
| 168 | |
---|
[2164fa4] | 169 | #ifdef NO_COVARIANCES |
---|
| 170 | real e; |
---|
| 171 | #else |
---|
[dac18d8] | 172 | svar e; |
---|
[eb18f4d] | 173 | delta a; |
---|
[2164fa4] | 174 | #endif |
---|
[b5d3988] | 175 | #if DEBUG_MATRIX_BUILD |
---|
[dbd68203] | 176 | print_prefix(stn->name); |
---|
[b5d3988] | 177 | printf(" used: %d colour %ld\n", |
---|
[a420b49] | 178 | (!!stn->leg[2]) << 2 | (!!stn -> leg[1]) << 1 | (!!stn->leg[0]), |
---|
[b5d3988] | 179 | stn->colour); |
---|
[3fde384f] | 180 | |
---|
[5bb3dc4] | 181 | for (int dirn = 0; dirn <= 2 && stn->leg[dirn]; dirn++) { |
---|
[907fe10] | 182 | printf("Leg %d, vx=%f, reverse=%d, to ", dirn, |
---|
| 183 | stn->leg[dirn]->v[0], stn->leg[dirn]->l.reverse); |
---|
| 184 | print_prefix(stn->leg[dirn]->l.to->name); |
---|
| 185 | putnl(); |
---|
| 186 | } |
---|
[dbd68203] | 187 | putnl(); |
---|
[d1b1380] | 188 | #endif /* DEBUG_MATRIX_BUILD */ |
---|
[b5d3988] | 189 | |
---|
[2d8d46d] | 190 | int f = stn->colour; |
---|
| 191 | if (f != COLOUR_FIXED) { |
---|
[5bb3dc4] | 192 | for (int dirn = 0; dirn <= 2 && stn->leg[dirn]; dirn++) { |
---|
[907fe10] | 193 | linkfor *leg = stn->leg[dirn]; |
---|
| 194 | node *to = leg->l.to; |
---|
[2d8d46d] | 195 | int t = to->colour; |
---|
| 196 | if (t == COLOUR_FIXED) { |
---|
[907fe10] | 197 | bool fRev = !data_here(leg); |
---|
| 198 | if (fRev) leg = reverse_leg(leg); |
---|
| 199 | /* Ignore equated nodes */ |
---|
[3fde384f] | 200 | #ifdef NO_COVARIANCES |
---|
[907fe10] | 201 | e = leg->v[dim]; |
---|
| 202 | if (e != (real)0.0) { |
---|
| 203 | e = ((real)1.0) / e; |
---|
| 204 | M(f,f) += e; |
---|
[f52dcc7] | 205 | B[f] += e * POS(to, dim); |
---|
[907fe10] | 206 | if (fRev) { |
---|
[f52dcc7] | 207 | B[f] += leg->d[dim]; |
---|
[907fe10] | 208 | } else { |
---|
[f52dcc7] | 209 | B[f] -= leg->d[dim]; |
---|
[564f471] | 210 | } |
---|
[907fe10] | 211 | } |
---|
[3fde384f] | 212 | #else |
---|
[907fe10] | 213 | if (invert_svar(&e, &leg->v)) { |
---|
| 214 | if (fRev) { |
---|
| 215 | adddd(&a, &POSD(to), &leg->d); |
---|
| 216 | } else { |
---|
| 217 | subdd(&a, &POSD(to), &leg->d); |
---|
| 218 | } |
---|
[5bb3dc4] | 219 | delta b; |
---|
[907fe10] | 220 | mulsd(&b, &e, &a); |
---|
[5bb3dc4] | 221 | for (int i = 0; i < 3; i++) { |
---|
[907fe10] | 222 | M(f * FACTOR + i, f * FACTOR + i) += e[i]; |
---|
| 223 | B[f * FACTOR + i] += b[i]; |
---|
[564f471] | 224 | } |
---|
[907fe10] | 225 | M(f * FACTOR + 1, f * FACTOR) += e[3]; |
---|
| 226 | M(f * FACTOR + 2, f * FACTOR) += e[4]; |
---|
| 227 | M(f * FACTOR + 2, f * FACTOR + 1) += e[5]; |
---|
| 228 | } |
---|
[3fde384f] | 229 | #endif |
---|
[907fe10] | 230 | } else if (data_here(leg)) { |
---|
| 231 | /* forward leg, unfixed -> unfixed */ |
---|
[d1b1380] | 232 | #if DEBUG_MATRIX |
---|
[16a78e0] | 233 | # ifdef NO_COVARIANCES |
---|
[907fe10] | 234 | printf("Leg %d to %d, var %f, delta %f\n", f, t, e, |
---|
| 235 | leg->d[dim]); |
---|
[16a78e0] | 236 | # else |
---|
| 237 | printf("Leg %d to %d, var (%f, %f, %f; %f, %f, %f), " |
---|
| 238 | "delta %f\n", f, t, e[0], e[1], e[2], e[3], e[4], e[5], |
---|
| 239 | leg->d[dim]); |
---|
| 240 | # endif |
---|
[d1b1380] | 241 | #endif |
---|
[907fe10] | 242 | /* Ignore equated nodes & lollipops */ |
---|
[3fde384f] | 243 | #ifdef NO_COVARIANCES |
---|
[907fe10] | 244 | e = leg->v[dim]; |
---|
| 245 | if (t != f && e != (real)0.0) { |
---|
| 246 | e = ((real)1.0) / e; |
---|
| 247 | M(f,f) += e; |
---|
| 248 | M(t,t) += e; |
---|
| 249 | if (f < t) M(t,f) -= e; else M(f,t) -= e; |
---|
[5bb3dc4] | 250 | real a = e * leg->d[dim]; |
---|
[907fe10] | 251 | B[f] -= a; |
---|
| 252 | B[t] += a; |
---|
| 253 | } |
---|
[3fde384f] | 254 | #else |
---|
[907fe10] | 255 | if (t != f && invert_svar(&e, &leg->v)) { |
---|
| 256 | mulsd(&a, &e, &leg->d); |
---|
[5bb3dc4] | 257 | for (int i = 0; i < 3; i++) { |
---|
[907fe10] | 258 | M(f * FACTOR + i, f * FACTOR + i) += e[i]; |
---|
| 259 | M(t * FACTOR + i, t * FACTOR + i) += e[i]; |
---|
| 260 | if (f < t) |
---|
| 261 | M(t * FACTOR + i, f * FACTOR + i) -= e[i]; |
---|
| 262 | else |
---|
| 263 | M(f * FACTOR + i, t * FACTOR + i) -= e[i]; |
---|
| 264 | B[f * FACTOR + i] -= a[i]; |
---|
| 265 | B[t * FACTOR + i] += a[i]; |
---|
| 266 | } |
---|
| 267 | M(f * FACTOR + 1, f * FACTOR) += e[3]; |
---|
| 268 | M(t * FACTOR + 1, t * FACTOR) += e[3]; |
---|
| 269 | M(f * FACTOR + 2, f * FACTOR) += e[4]; |
---|
| 270 | M(t * FACTOR + 2, t * FACTOR) += e[4]; |
---|
| 271 | M(f * FACTOR + 2, f * FACTOR + 1) += e[5]; |
---|
| 272 | M(t * FACTOR + 2, t * FACTOR + 1) += e[5]; |
---|
| 273 | if (f < t) { |
---|
| 274 | M(t * FACTOR + 1, f * FACTOR) -= e[3]; |
---|
| 275 | M(t * FACTOR, f * FACTOR + 1) -= e[3]; |
---|
| 276 | M(t * FACTOR + 2, f * FACTOR) -= e[4]; |
---|
| 277 | M(t * FACTOR, f * FACTOR + 2) -= e[4]; |
---|
| 278 | M(t * FACTOR + 2, f * FACTOR + 1) -= e[5]; |
---|
| 279 | M(t * FACTOR + 1, f * FACTOR + 2) -= e[5]; |
---|
| 280 | } else { |
---|
| 281 | M(f * FACTOR + 1, t * FACTOR) -= e[3]; |
---|
| 282 | M(f * FACTOR, t * FACTOR + 1) -= e[3]; |
---|
| 283 | M(f * FACTOR + 2, t * FACTOR) -= e[4]; |
---|
| 284 | M(f * FACTOR, t * FACTOR + 2) -= e[4]; |
---|
| 285 | M(f * FACTOR + 2, t * FACTOR + 1) -= e[5]; |
---|
| 286 | M(f * FACTOR + 1, t * FACTOR + 2) -= e[5]; |
---|
[dbd68203] | 287 | } |
---|
| 288 | } |
---|
[907fe10] | 289 | #endif |
---|
[564f471] | 290 | } |
---|
[907fe10] | 291 | } |
---|
[dbd68203] | 292 | } |
---|
[d1b1380] | 293 | } |
---|
| 294 | |
---|
| 295 | #if PRINT_MATRICES |
---|
[2d8d46d] | 296 | print_matrix(M, B, n * FACTOR); /* 'ave a look! */ |
---|
[d1b1380] | 297 | #endif |
---|
| 298 | |
---|
| 299 | #ifdef SOR |
---|
[032ed06] | 300 | /* defined in network.c, may be altered by -z<letters> on command line */ |
---|
[a420b49] | 301 | if (optimize & BITA('i')) |
---|
[2d8d46d] | 302 | sor(M, B, n * FACTOR); |
---|
[dbd68203] | 303 | else |
---|
[d1b1380] | 304 | #endif |
---|
[2d8d46d] | 305 | choleski(M, B, n * FACTOR); |
---|
[d1b1380] | 306 | |
---|
[dbd68203] | 307 | { |
---|
[2d8d46d] | 308 | for (int m = (int)(n - 1); m >= 0; m--) { |
---|
[3fde384f] | 309 | #ifdef NO_COVARIANCES |
---|
[c19f129] | 310 | stn_tab[m]->p[dim] = B[m]; |
---|
[032ed06] | 311 | if (dim == 0) { |
---|
[4c07c51] | 312 | SVX_ASSERT2(pos_fixed(stn_tab[m]), |
---|
[032ed06] | 313 | "setting station coordinates didn't mark pos as fixed"); |
---|
| 314 | } |
---|
[3fde384f] | 315 | #else |
---|
[5bb3dc4] | 316 | for (int i = 0; i < 3; i++) { |
---|
[c19f129] | 317 | stn_tab[m]->p[i] = B[m * FACTOR + i]; |
---|
[702f518] | 318 | } |
---|
[4c07c51] | 319 | SVX_ASSERT2(pos_fixed(stn_tab[m]), |
---|
[032ed06] | 320 | "setting station coordinates didn't mark pos as fixed"); |
---|
[d1b1380] | 321 | #endif |
---|
[4a59b4f] | 322 | } |
---|
[dbd68203] | 323 | } |
---|
| 324 | } |
---|
| 325 | osfree(B); |
---|
| 326 | osfree(M); |
---|
[2d8d46d] | 327 | osfree(stn_tab); |
---|
| 328 | |
---|
| 329 | #if DEBUG_MATRIX |
---|
| 330 | FOR_EACH_STN(stn, list) { |
---|
| 331 | printf("(%8.2f, %8.2f, %8.2f ) ", POS(stn, 0), POS(stn, 1), POS(stn, 2)); |
---|
| 332 | print_prefix(stn->name); |
---|
| 333 | putnl(); |
---|
| 334 | } |
---|
| 335 | #endif |
---|
[d1b1380] | 336 | } |
---|
| 337 | |
---|
[702f518] | 338 | /* Solve MX=B for X by Choleski factorisation - modified Choleski actually |
---|
| 339 | * since we factor into LDL' while Choleski is just LL' |
---|
| 340 | */ |
---|
[d1b1380] | 341 | /* Note M must be symmetric positive definite */ |
---|
| 342 | /* routine is entitled to scribble on M and B if it wishes */ |
---|
[a420b49] | 343 | static void |
---|
[9965b2b] | 344 | choleski(real *M, real *B, long n) |
---|
[a420b49] | 345 | { |
---|
[5bb3dc4] | 346 | for (int j = 1; j < n; j++) { |
---|
[3fde384f] | 347 | real V; |
---|
[5bb3dc4] | 348 | for (int i = 0; i < j; i++) { |
---|
[421b7d2] | 349 | V = (real)0.0; |
---|
[5bb3dc4] | 350 | for (int k = 0; k < i; k++) V += M(i,k) * M(j,k) * M(k,k); |
---|
[a420b49] | 351 | M(j,i) = (M(j,i) - V) / M(i,i); |
---|
[dbd68203] | 352 | } |
---|
| 353 | V = (real)0.0; |
---|
[5bb3dc4] | 354 | for (int k = 0; k < j; k++) V += M(j,k) * M(j,k) * M(k,k); |
---|
[3fde384f] | 355 | M(j,j) -= V; /* may be best to add M() last for numerical reasons too */ |
---|
[dbd68203] | 356 | } |
---|
[d1b1380] | 357 | |
---|
[dbd68203] | 358 | /* Multiply x by L inverse */ |
---|
[5bb3dc4] | 359 | for (int i = 0; i < n - 1; i++) { |
---|
| 360 | for (int j = i + 1; j < n; j++) { |
---|
[dbd68203] | 361 | B[j] -= M(j,i) * B[i]; |
---|
[3fde384f] | 362 | } |
---|
[dbd68203] | 363 | } |
---|
[d1b1380] | 364 | |
---|
[dbd68203] | 365 | /* Multiply x by D inverse */ |
---|
[5bb3dc4] | 366 | for (int i = 0; i < n; i++) { |
---|
[dbd68203] | 367 | B[i] /= M(i,i); |
---|
[3fde384f] | 368 | } |
---|
| 369 | |
---|
| 370 | /* Multiply x by (L transpose) inverse */ |
---|
[5bb3dc4] | 371 | for (int i = (int)(n - 1); i > 0; i--) { |
---|
| 372 | for (int j = i - 1; j >= 0; j--) { |
---|
[421b7d2] | 373 | B[j] -= M(i,j) * B[i]; |
---|
[3fde384f] | 374 | } |
---|
[dbd68203] | 375 | } |
---|
[d1b1380] | 376 | |
---|
[dbd68203] | 377 | /* printf("\n%ld/%ld\n\n",flops,flopsTot); */ |
---|
[d1b1380] | 378 | } |
---|
| 379 | |
---|
| 380 | #ifdef SOR |
---|
| 381 | /* factor to use for SOR (must have 1 <= SOR_factor < 2) */ |
---|
[702f518] | 382 | #define SOR_factor 1.93 /* 1.95 */ |
---|
[d1b1380] | 383 | |
---|
| 384 | /* Solve MX=B for X by SOR of Gauss-Siedel */ |
---|
| 385 | /* routine is entitled to scribble on M and B if it wishes */ |
---|
[a420b49] | 386 | static void |
---|
[9965b2b] | 387 | sor(real *M, real *B, long n) |
---|
[a420b49] | 388 | { |
---|
[dbd68203] | 389 | long it = 0; |
---|
[d1b1380] | 390 | |
---|
[5bb3dc4] | 391 | real *X = osmalloc(n * ossizeof(real)); |
---|
[d1b1380] | 392 | |
---|
[5bb3dc4] | 393 | const real threshold = 0.00001; |
---|
[d1b1380] | 394 | |
---|
[647407d] | 395 | printf("reciprocating diagonal\n"); /* TRANSLATE */ |
---|
[d1b1380] | 396 | |
---|
[3fde384f] | 397 | /* munge diagonal so we can multiply rather than divide */ |
---|
[5bb3dc4] | 398 | for (int row = n - 1; row >= 0; row--) { |
---|
[dbd68203] | 399 | M(row,row) = 1 / M(row,row); |
---|
[702f518] | 400 | X[row] = 0; |
---|
[dbd68203] | 401 | } |
---|
[d1b1380] | 402 | |
---|
[647407d] | 403 | printf("starting iteration\n"); /* TRANSLATE */ |
---|
[d1b1380] | 404 | |
---|
[5bb3dc4] | 405 | real t; |
---|
[dbd68203] | 406 | do { |
---|
| 407 | /*printf("*");*/ |
---|
| 408 | it++; |
---|
| 409 | t = 0.0; |
---|
[5bb3dc4] | 410 | for (int row = 0; row < n; row++) { |
---|
| 411 | real x = B[row]; |
---|
| 412 | int col; |
---|
[a420b49] | 413 | for (col = 0; col < row; col++) x -= M(row,col) * X[col]; |
---|
| 414 | for (col++; col < n; col++) x -= M(col,row) * X[col]; |
---|
[dbd68203] | 415 | x *= M(row,row); |
---|
[3b8b342] | 416 | real sor_delta = (x - X[row]) * SOR_factor; |
---|
| 417 | X[row] += sor_delta; |
---|
| 418 | real t2 = fabs(sor_delta); |
---|
[dbd68203] | 419 | if (t2 > t) t = t2; |
---|
| 420 | } |
---|
[3b8b342] | 421 | printf("% 6ld: %8.6f\n", it, t); |
---|
[dbd68203] | 422 | } while (t >= threshold && it < 100000); |
---|
[d1b1380] | 423 | |
---|
[dbd68203] | 424 | if (t >= threshold) { |
---|
| 425 | fprintf(stderr, "*not* converged after %ld iterations\n", it); |
---|
| 426 | BUG("iteration stinks"); |
---|
| 427 | } |
---|
[d1b1380] | 428 | |
---|
[647407d] | 429 | printf("%ld iterations\n", it); /* TRANSLATE */ |
---|
[d1b1380] | 430 | |
---|
| 431 | #if 0 |
---|
[dbd68203] | 432 | putnl(); |
---|
[5bb3dc4] | 433 | for (int row = n - 1; row >= 0; row--) { |
---|
[dbd68203] | 434 | t = 0.0; |
---|
[5bb3dc4] | 435 | for (int col = 0; col < row; col++) t += M(row, col) * X[col]; |
---|
[a420b49] | 436 | t += X[row] / M(row, row); |
---|
| 437 | for (col = row + 1; col < n; col++) |
---|
| 438 | t += M(col, row) * X[col]; |
---|
[b5d3988] | 439 | printf("[ %f %f ]\n", t, B[row]); |
---|
[dbd68203] | 440 | } |
---|
[d1b1380] | 441 | #endif |
---|
| 442 | |
---|
[5bb3dc4] | 443 | for (int row = n - 1; row >= 0; row--) B[row] = X[row]; |
---|
[d1b1380] | 444 | |
---|
[dbd68203] | 445 | osfree(X); |
---|
[647407d] | 446 | printf("\ndone\n"); /* TRANSLATE */ |
---|
[dbd68203] | 447 | } |
---|
[d1b1380] | 448 | #endif |
---|
| 449 | |
---|
| 450 | #if PRINT_MATRICES |
---|
[a420b49] | 451 | static void |
---|
[9965b2b] | 452 | print_matrix(real *M, real *B, long n) |
---|
[a420b49] | 453 | { |
---|
[dbd68203] | 454 | printf("Matrix, M and vector, B:\n"); |
---|
[5bb3dc4] | 455 | for (long row = 0; row < n; row++) { |
---|
| 456 | long col; |
---|
[a420b49] | 457 | for (col = 0; col <= row; col++) printf("%6.2f\t", M(row, col)); |
---|
| 458 | for (; col <= n; col++) printf(" \t"); |
---|
[dbd68203] | 459 | printf("\t%6.2f\n", B[row]); |
---|
| 460 | } |
---|
| 461 | putnl(); |
---|
| 462 | return; |
---|
[d1b1380] | 463 | } |
---|
| 464 | #endif |
---|