/
linearSolverKlu.c
405 lines (346 loc) · 12.4 KB
/
linearSolverKlu.c
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
* This file is part of OpenModelica.
*
* Copyright (c) 1998-2014, Open Source Modelica Consortium (OSMC),
* c/o Linköpings universitet, Department of Computer and Information Science,
* SE-58183 Linköping, Sweden.
*
* All rights reserved.
*
* THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THE BSD NEW LICENSE OR THE
* GPL VERSION 3 LICENSE OR THE OSMC PUBLIC LICENSE (OSMC-PL) VERSION 1.2.
* ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS PROGRAM CONSTITUTES
* RECIPIENT'S ACCEPTANCE OF THE OSMC PUBLIC LICENSE OR THE GPL VERSION 3,
* ACCORDING TO RECIPIENTS CHOICE.
*
* The OpenModelica software and the OSMC (Open Source Modelica Consortium)
* Public License (OSMC-PL) are obtained from OSMC, either from the above
* address, from the URLs: http://www.openmodelica.org or
* http://www.ida.liu.se/projects/OpenModelica, and in the OpenModelica
* distribution. GNU version 3 is obtained from:
* http://www.gnu.org/copyleft/gpl.html. The New BSD License is obtained from:
* http://www.opensource.org/licenses/BSD-3-Clause.
*
* This program is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, EXCEPT AS
* EXPRESSLY SET FORTH IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE
* CONDITIONS OF OSMC-PL.
*
*/
/*! \file linearSolverKlu.c
*/
#include "omc_config.h"
#ifdef WITH_UMFPACK
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "simulation_data.h"
#include "simulation/simulation_info_json.h"
#include "util/omc_error.h"
#include "omc_math.h"
#include "util/varinfo.h"
#include "model_help.h"
#include "linearSystem.h"
#include "linearSolverKlu.h"
static void printMatrixCSC(int* Ap, int* Ai, double* Ax, int n);
static void printMatrixCSR(int* Ap, int* Ai, double* Ax, int n);
/*! \fn allocate memory for linear system solver Klu
*
*/
int
allocateKluData(int n_row, int n_col, int nz, void** voiddata)
{
DATA_KLU* data = (DATA_KLU*) malloc(sizeof(DATA_KLU));
assertStreamPrint(NULL, 0 != data, "Could not allocate data for linear solver Klu.");
data->symbolic = NULL;
data->numeric = NULL;
data->n_col = n_col;
data->n_row = n_row;
data->nnz = nz;
data->Ap = (int*) calloc((n_row+1),sizeof(int));
data->Ai = (int*) calloc(nz,sizeof(int));
data->Ax = (double*) calloc(nz,sizeof(double));
data->work = (double*) calloc(n_col,sizeof(double));
data->numberSolving = 0;
klu_defaults(&(data->common));
*voiddata = (void*)data;
return 0;
}
/*! \fn free memory for linear system solver Klu
*
*/
int
freeKluData(void **voiddata)
{
TRACE_PUSH
DATA_KLU* data = (DATA_KLU*) *voiddata;
free(data->Ap);
free(data->Ai);
free(data->Ax);
free(data->work);
if(data->symbolic)
klu_free_symbolic(&data->symbolic, &data->common);
if(data->numeric)
klu_free_numeric(&data->numeric, &data->common);
TRACE_POP
return 0;
}
/*! \fn getAnalyticalJacobian
*
* function calculates analytical jacobian
*
* \param [ref] [data]
* \param [in] [sysNumber]
*
* \author wbraun
*
*/
static
int getAnalyticalJacobian(DATA* data, threadData_t *threadData, int sysNumber)
{
int i,ii,j,k,l;
LINEAR_SYSTEM_DATA* systemData = &(((DATA*)data)->simulationInfo->linearSystemData[sysNumber]);
const int index = systemData->jacobianIndex;
ANALYTIC_JACOBIAN* jacobian = &(data->simulationInfo->analyticJacobians[systemData->jacobianIndex]);
ANALYTIC_JACOBIAN* parentJacobian = systemData->parentJacobian;
int nth = 0;
int nnz = jacobian->sparsePattern.numberOfNoneZeros;
for(i=0; i < jacobian->sizeRows; i++)
{
jacobian->seedVars[i] = 1;
((systemData->analyticalJacobianColumn))(data, threadData, jacobian, parentJacobian);
for(j = 0; j < jacobian->sizeCols; j++)
{
if(jacobian->seedVars[j] == 1)
{
ii = jacobian->sparsePattern.leadindex[j];
while(ii < jacobian->sparsePattern.leadindex[j+1])
{
l = jacobian->sparsePattern.index[ii];
systemData->setAElement(i, l, -jacobian->resultVars[l], nth, (void*) systemData, threadData);
nth++;
ii++;
};
}
};
/* de-activate seed variable for the corresponding color */
jacobian->seedVars[i] = 0;
}
return 0;
}
/*! \fn residual_wrapper for the residual function
*
*/
static int residual_wrapper(double* x, double* f, void** data, int sysNumber)
{
int iflag = 0;
(*((DATA*)data[0])->simulationInfo->linearSystemData[sysNumber].residualFunc)(data, x, f, &iflag);
return 0;
}
/*! \fn solve linear system with Klu method
*
* \param [in] [data]
* [sysNumber] index of the corresponding linear system
*
*
* author: wbraun
*/
int
solveKlu(DATA *data, threadData_t *threadData, int sysNumber, double* aux_x)
{
void *dataAndThreadData[2] = {data, threadData};
LINEAR_SYSTEM_DATA* systemData = &(data->simulationInfo->linearSystemData[sysNumber]);
DATA_KLU* solverData = (DATA_KLU*)systemData->solverData[0];
_omc_scalar residualNorm = 0;
int i, j, status = 0, success = 0, n = systemData->size, eqSystemNumber = systemData->equationIndex, indexes[2] = {1,eqSystemNumber};
double tmpJacEvalTime;
int reuseMatrixJac = (data->simulationInfo->currentContext == CONTEXT_SYM_JACOBIAN && data->simulationInfo->currentJacobianEval > 0);
infoStreamPrintWithEquationIndexes(LOG_LS, 0, indexes, "Start solving Linear System %d (size %d) at time %g with Klu Solver",
eqSystemNumber, (int) systemData->size,
data->localData[0]->timeValue);
rt_ext_tp_tick(&(solverData->timeClock));
if (0 == systemData->method)
{
if (!reuseMatrixJac){
/* set A matrix */
solverData->Ap[0] = 0;
systemData->setA(data, threadData, systemData);
solverData->Ap[solverData->n_row] = solverData->nnz;
}
/* set b vector */
systemData->setb(data, threadData, systemData);
} else {
if (!reuseMatrixJac){
solverData->Ap[0] = 0;
/* calculate jacobian -> matrix A*/
if(systemData->jacobianIndex != -1){
getAnalyticalJacobian(data, threadData, sysNumber);
} else {
assertStreamPrint(threadData, 1, "jacobian function pointer is invalid" );
}
solverData->Ap[solverData->n_row] = solverData->nnz;
}
/* calculate vector b (rhs) */
memcpy(solverData->work, aux_x, sizeof(double)*solverData->n_row);
residual_wrapper(solverData->work, systemData->b, dataAndThreadData, sysNumber);
}
tmpJacEvalTime = rt_ext_tp_tock(&(solverData->timeClock));
systemData->jacobianTime += tmpJacEvalTime;
infoStreamPrint(LOG_LS_V, 0, "### %f time to set Matrix A and vector b.", tmpJacEvalTime);
if (ACTIVE_STREAM(LOG_LS_V))
{
infoStreamPrint(LOG_LS_V, 1, "Old solution x:");
for(i = 0; i < solverData->n_row; ++i)
infoStreamPrint(LOG_LS_V, 0, "[%d] %s = %g", i+1, modelInfoGetEquation(&data->modelData->modelDataXml,eqSystemNumber).vars[i], aux_x[i]);
messageClose(LOG_LS_V);
infoStreamPrint(LOG_LS_V, 1, "Matrix A n_rows = %d", solverData->n_row);
for (i=0; i<solverData->n_row; i++){
infoStreamPrint(LOG_LS_V, 0, "%d. Ap => %d -> %d", i, solverData->Ap[i], solverData->Ap[i+1]);
for (j=solverData->Ap[i]; j<solverData->Ap[i+1]; j++){
infoStreamPrint(LOG_LS_V, 0, "A[%d,%d] = %f", i, solverData->Ai[j], solverData->Ax[j]);
}
}
messageClose(LOG_LS_V);
for (i=0; i<solverData->n_row; i++)
infoStreamPrint(LOG_LS_V, 0, "b[%d] = %e", i, systemData->b[i]);
}
rt_ext_tp_tick(&(solverData->timeClock));
/* symbolic pre-ordering of A to reduce fill-in of L and U */
if (0 == solverData->numberSolving)
{
infoStreamPrint(LOG_LS_V, 0, "Perform analyze settings:\n - ordering used: %d\n - current status: %d", solverData->common.ordering, solverData->common.status);
solverData->symbolic = klu_analyze(solverData->n_col, solverData->Ap, solverData->Ai, &solverData->common);
}
/* if reuseMatrixJac use also previous factorization */
if (!reuseMatrixJac)
{
/* compute the LU factorization of A */
if (0 == solverData->common.status){
if(solverData->numeric){
/* Just refactor using the same pivots, but check that the refactor is still accurate */
klu_refactor(solverData->Ap, solverData->Ai, solverData->Ax, solverData->symbolic, solverData->numeric, &solverData->common);
klu_rgrowth(solverData->Ap, solverData->Ai, solverData->Ax, solverData->symbolic, solverData->numeric, &solverData->common);
infoStreamPrint(LOG_LS_V, 0, "Klu rgrowth after refactor: %f", solverData->common.rgrowth);
/* If rgrowth is small then do a whole factorization with new pivots (What should this tolerance be?) */
if (solverData->common.rgrowth < 1e-3){
klu_free_numeric(&solverData->numeric, &solverData->common);
solverData->numeric = klu_factor(solverData->Ap, solverData->Ai, solverData->Ax, solverData->symbolic, &solverData->common);
infoStreamPrint(LOG_LS_V, 0, "Klu new factorization performed.");
}
} else {
solverData->numeric = klu_factor(solverData->Ap, solverData->Ai, solverData->Ax, solverData->symbolic, &solverData->common);
}
}
}
if (0 == solverData->common.status){
if (1 == systemData->method){
if (klu_solve(solverData->symbolic, solverData->numeric, solverData->n_col, 1, systemData->b, &solverData->common)){
success = 1;
}
} else {
if (klu_tsolve(solverData->symbolic, solverData->numeric, solverData->n_col, 1, systemData->b, &solverData->common)){
success = 1;
}
}
}
infoStreamPrint(LOG_LS_V, 0, "Solve System: %f", rt_ext_tp_tock(&(solverData->timeClock)));
/* print solution */
if (1 == success){
if (1 == systemData->method){
/* take the solution */
for(i = 0; i < solverData->n_row; ++i)
aux_x[i] += systemData->b[i];
/* update inner equations */
residual_wrapper(aux_x, solverData->work, dataAndThreadData, sysNumber);
residualNorm = _omc_gen_euclideanVectorNorm(solverData->work, solverData->n_row);
if ((isnan(residualNorm)) || (residualNorm>1e-4)){
warningStreamPrint(LOG_LS, 0,
"Failed to solve linear system of equations (no. %d) at time %f. Residual norm is %.15g.",
(int)systemData->equationIndex, data->localData[0]->timeValue, residualNorm);
success = 0;
}
} else {
/* the solution is automatically in x */
memcpy(aux_x, systemData->b, sizeof(double)*systemData->size);
}
if (ACTIVE_STREAM(LOG_LS_V))
{
if (1 == systemData->method) {
infoStreamPrint(LOG_LS_V, 1, "Residual Norm %.15g of solution x:", residualNorm);
} else {
infoStreamPrint(LOG_LS_V, 1, "Solution x:");
}
infoStreamPrint(LOG_LS_V, 0, "System %d numVars %d.", eqSystemNumber, modelInfoGetEquation(&data->modelData->modelDataXml,eqSystemNumber).numVar);
for(i = 0; i < systemData->size; ++i)
infoStreamPrint(LOG_LS_V, 0, "[%d] %s = %g", i+1, modelInfoGetEquation(&data->modelData->modelDataXml,eqSystemNumber).vars[i], aux_x[i]);
messageClose(LOG_LS_V);
}
}
else
{
warningStreamPrint(LOG_STDOUT, 0,
"Failed to solve linear system of equations (no. %d) at time %f, system status %d.",
(int)systemData->equationIndex, data->localData[0]->timeValue, status);
}
solverData->numberSolving += 1;
return success;
}
static
void printMatrixCSC(int* Ap, int* Ai, double* Ax, int n)
{
int i, j, k, l;
char **buffer = (char**)malloc(sizeof(char*)*n);
for (l=0; l<n; l++)
{
buffer[l] = (char*)malloc(sizeof(char)*n*20);
buffer[l][0] = 0;
}
k = 0;
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
{
if ((k < Ap[i + 1]) && (Ai[k] == j))
{
sprintf(buffer[j], "%s %5g ", buffer[j], Ax[k]);
k++;
}
else
{
sprintf(buffer[j], "%s %5g ", buffer[j], 0.0);
}
}
}
for (l = 0; l < n; l++)
{
infoStreamPrint(LOG_LS_V, 0, "%s", buffer[l]);
free(buffer[l]);
}
free(buffer);
}
static
void printMatrixCSR(int* Ap, int* Ai, double* Ax, int n)
{
int i, j, k;
char *buffer = (char*)malloc(sizeof(char)*n*15);
k = 0;
for (i = 0; i < n; i++)
{
buffer[0] = 0;
for (j = 0; j < n; j++)
{
if ((k < Ap[i + 1]) && (Ai[k] == j))
{
sprintf(buffer, "%s %5.2g ", buffer, Ax[k]);
k++;
}
else
{
sprintf(buffer, "%s %5.2g ", buffer, 0.0);
}
}
infoStreamPrint(LOG_LS_V, 0, "%s", buffer);
}
free(buffer);
}
#endif