File: itkFEMItpackSparseMatrix.cxx

package info (click to toggle)
insighttoolkit 3.20.1%2Bgit20120521-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 80,672 kB
  • ctags: 85,253
  • sloc: cpp: 458,133; ansic: 196,222; fortran: 28,000; python: 3,839; tcl: 1,811; sh: 1,184; java: 583; makefile: 428; csh: 220; perl: 193; xml: 20
file content (534 lines) | stat: -rw-r--r-- 10,774 bytes parent folder | download | duplicates (2)
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    itkFEMItpackSparseMatrix.cxx
  Language:  C++
  Date:      $Date$
  Version:   $Revision$

  Copyright (c) Insight Software Consortium. All rights reserved.
  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.

     This software is distributed WITHOUT ANY WARRANTY; without even 
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
     PURPOSE.  See the above copyright notices for more information.

=========================================================================*/
#include "itkFEMItpackSparseMatrix.h"
#include "itpack.h"

namespace itk {
namespace fem {


ItpackSparseMatrix::ItpackSparseMatrix()
{
  m_MatrixFinalized = 0;
  m_MatrixInitialized = 0;
  m_NZ = 0;
  m_N = 0;
  /* m_IER = 0; */     /* initialize */
  m_MODE = 1;    /* add to existing entries when building matrix */
  m_LEVEL = -1;  /* no error messages */
  m_NOUT = 0;    /* output unit number */

  m_IA = 0;
  m_JA = 0;
  m_IWORK = 0;
  m_A = 0;
}

ItpackSparseMatrix::ItpackSparseMatrix(integer order)
{
  m_MatrixFinalized = 0;
  m_MatrixInitialized = 0;
  m_NZ = 0;
  m_N = order;
  /* m_IER = 0; */     /* initialize */
  m_MODE = 1;    /* add to existing entries when building matrix */
  m_LEVEL = -1;  /* no error messages */
  m_NOUT = 0;    /* output unit number */

  m_IA = 0;
  m_JA = 0;
  m_IWORK = 0;
  m_A = 0;
}


ItpackSparseMatrix::ItpackSparseMatrix(integer order, integer maxNonZeroValues)
{
  m_MatrixFinalized = 0;
  m_MatrixInitialized = 0;
  m_N = order;
  m_NZ = maxNonZeroValues;
  /* m_IER = 0; */      /* initialize */
  m_MODE = 1;     /* add to existing entries when building matrix */
  m_LEVEL = -1;   /* no error messages */
  m_NOUT = 0;     /* output unit number */

  m_IA = 0;
  m_JA = 0;
  m_IWORK = 0;
  m_A = 0;

}


void ItpackSparseMatrix::Initialize()
{

  /* is matrix ready for initialization */
  if ( (m_N <= 0) || (m_NZ <= 0) ) 
    { 
    /* FIX ME: error handling */
    throw FEMException(__FILE__, __LINE__, "ItpackSparseMatrix::Initialize");
    }

  /* initialize itpack variables */
  if (m_IA != 0)
    {
    delete [] m_IA;
    }
  if (m_JA != 0)
    {
    delete [] m_JA;
    }
  if (m_IWORK != 0)
    {
    delete [] m_IWORK;
    }
  if (m_A != 0)
    {
    delete [] m_A;
    }
  m_IA =    new integer [ m_N + 1 ];
  m_JA =    new integer [ m_NZ ];
  m_IWORK = new integer [ m_NZ ];
  m_A =     new doublereal [ m_NZ ];

  int i;
  for (i=0; i<m_NZ; i++) 
    {
    m_JA[i] = 0;
    m_IWORK[i] = 0;
    m_A[i] = 0.0;
    }
  for (i=0; i<=m_N; i++) 
    {
    m_IA[i] = 0;
    }

  /* initialize sparse matrix storage via itpack routine */
  sbini_( &m_N, &m_NZ, m_IA, m_JA, m_A, m_IWORK );
  
  /* set info flags */
  m_MatrixInitialized = 1;
  m_MatrixFinalized = 0;

  /* Do this to avoid itpack ignorance (unless it's somehow my ignorance) */
  for (i=0; i<m_N; i++)
    {
    this->Set(i,i,0.0);
    }

  return;
}

void ItpackSparseMatrix::Clear()
{

  /* free variables */
  if (m_IA != 0)
    {
    delete [] m_IA;
    }
  if (m_JA != 0)
    {
    delete [] m_JA;
    }
  if (m_IWORK != 0)
    {
    delete [] m_IWORK;
    }
  if (m_A != 0)
    {
    delete [] m_A;
    }
 
  m_MatrixFinalized = 0;
  m_MatrixInitialized = 0;
  m_N = 0;
  m_NZ = 0;
  /* m_IER = 0;   */
  m_MODE = 1;   
  m_LEVEL = -1;  
  m_NOUT = 0;

  m_IA = 0;
  m_JA = 0;
  m_IWORK = 0;
  m_A = 0;
}

void ItpackSparseMatrix::Finalize()
{

  /* check */
  if ( (m_MatrixFinalized != 0) || (m_MatrixInitialized == 0) ) 
    {
    throw FEMException(__FILE__, __LINE__, "ItpackSparseMatrix::Finalize");
    }

  //std::cout << "sbend_ ... " << std::endl;
  //this->PrintCompressedRow();

  /* finalize */
  sbend_( &m_N, &m_NZ, m_IA, m_JA, m_A, m_IWORK );
  
  //this->PrintCompressedRow();
  //std::cout << "sbend_ " << m_IER << std::endl;
 
  /* set info flag */
  m_MatrixFinalized = 1;

  return;
}


void ItpackSparseMatrix::UnFinalize()
{

  /* check if this op makes sense*/
  if ( (m_MatrixFinalized == 0) || (m_MatrixInitialized == 0) )
    {
    throw FEMException(__FILE__, __LINE__, "ItpackSparseMatrix::UnFinalize");
    }

  integer IER = 0;

  sbagn_(&m_N, &m_NZ, m_IA, m_JA, m_A, m_IWORK, &m_LEVEL, &m_NOUT, &IER);

  if (IER > 0)
    {
    throw FEMExceptionItpackSparseMatrixSbagn(__FILE__, __LINE__, "ItpackSparseMatrix::UnFinalize", IER);
    }

  /* set info flag */
  m_MatrixFinalized = 0;

  return;
}


void ItpackSparseMatrix::Set(integer i, integer j, doublereal value)
{

  /* check for dynamic form */
  if (m_MatrixInitialized == 0)
    {

    /* initialize if prepared */
    if ( (m_N <= 0) || (m_NZ <= 0) )
      {
      throw FEMException(__FILE__, __LINE__, "ItpackSparseMatrix::Set");
      }
    else 
      {
      this->Initialize();
      }
    }

  if (m_MatrixFinalized == 1) 
    {
    this->UnFinalize();
    }

  /* replace an existing value */
  m_MODE = 0;

  /* add entry (itpack expects 1-based indices */
  integer IER;
  integer fortranI = i+1;
  integer fortranJ = j+1;
  sbsij_(&m_N, &m_NZ, m_IA, m_JA, m_A, m_IWORK, &fortranI, &fortranJ, &value, &m_MODE, &m_LEVEL, &m_NOUT, &IER);

  if (IER > 700)
    {
    throw FEMExceptionItpackSparseMatrixSbsij(__FILE__, __LINE__, "ItpackSparseMatrix::Set", IER);
    }


  return;
}


void ItpackSparseMatrix::Add(integer i, integer j, doublereal value)
{

  /* ignore add zero */
  if (value == 0.0) 
    {
    return;
    }

  /* check for dynamic form */
  if (m_MatrixInitialized == 0)
    {

    /* initialize if prepared */
    if ( (m_N <= 0) || (m_NZ <= 0) )
      {
      throw FEMException(__FILE__, __LINE__, "ItpackSparseMatrix::Add");
      }
    else 
      {
      this->Initialize();
      }
    }
  if (m_MatrixFinalized != 0) 
    {
    this->UnFinalize();
    }

  /* add to an existing value */
  m_MODE = 1;

  /* add entry (itpack expects 1-based indices */
  integer IER;
  integer fortranI = i+1;
  integer fortranJ = j+1;
  sbsij_(&m_N, &m_NZ, m_IA, m_JA, m_A, m_IWORK, &fortranI, &fortranJ, &value, &m_MODE, &m_LEVEL, &m_NOUT, &IER);
  
  if (IER > 700)
    {
    throw FEMExceptionItpackSparseMatrixSbsij(__FILE__, __LINE__, "ItpackSparseMatrix::Set", IER);
    }

  return;
}

ItpackSparseMatrix::doublereal ItpackSparseMatrix::Get(integer i, integer j)
{

  doublereal returnValue = 0.0; /* set to default return value */
  integer fortranJ = j+1;
  integer lower;
  integer upper;

  /* check for readiness */
  if (m_MatrixInitialized != 0)
    {  

    /* ensure matrix is in readable form */
    if (m_MatrixFinalized == 0)
      {
      this->Finalize();
      }

    /* get search bounds in appropriate row */
    lower = m_IA[i]-1;
    upper = m_IA[i+1]-1;

    /* Find value if it exists */
    for (int k=lower; k<upper; k++)
      {
      if (m_JA[k] == fortranJ)
        {
        returnValue = m_A[k];
        }
      }
    }

  return returnValue;
}


ItpackSparseMatrix::doublereal* ItpackSparseMatrix::GetA()
{
  if (m_MatrixInitialized == 0) return 0;
  if (m_MatrixFinalized == 0) Finalize();

  return m_A;
}


ItpackSparseMatrix::integer* ItpackSparseMatrix::GetIA()
{
  if (m_MatrixInitialized == 0) return 0;
  if (m_MatrixFinalized == 0) Finalize();

  return m_IA;
}


ItpackSparseMatrix::integer* ItpackSparseMatrix::GetJA()
{
  if (m_MatrixInitialized == 0) return 0;
  if (m_MatrixFinalized == 0) Finalize();

  return m_JA;
}


void ItpackSparseMatrix::mult(doublereal* vector, doublereal* result)
{

  /* finalize matrix */
  if (m_MatrixFinalized == 0)
    {
    this->Finalize();
    }

  /* loop and temp variables */
  int lower;
  int upper;
  int i;
  int j;

  /* prepare result vector */
  //delete [] result;
  //result = new doublereal [ m_N ];
  for (i=0; i<m_N; i++)
    {
    result[i] = 0.0;
    }

  /* perform the mult operation */
  for (i=0; i<m_N; i++)
    {

    lower = m_IA[i]-1;
    upper = m_IA[i+1]-1;

    for (j=lower; j<upper; j++)
      {
      result[i] += m_A[j] * vector[ m_JA[j] - 1 ];
      }
    }

  return;
}


void ItpackSparseMatrix::mult(ItpackSparseMatrix* rightMatrix, ItpackSparseMatrix* resultMatrix)
{

  /* ensure appropriate matrix sizes */
  if (m_N != rightMatrix->GetOrder())
    {
    return;
    }

  /* finalize matrix */
  if (m_MatrixFinalized == 0)
    {
    this->Finalize();
    }


  /* loop and temp variables */
  int lower;          /* lower bounds for column indices vector */
  int upper;          /* upper bounds for column indices vector */
  int i;              /* loop over rows */
  int j;              /* loop over columns */
  int k;              /* iterate through row */
  doublereal summed;  /* temp holder for row.column */

  /* perform the mult operation */
  for (i=0; i<m_N; i++)
    {
    for (j=0; j<m_N; j++)
      {

      /* bounds of values located in current row */
      lower = m_IA[i]-1;
      upper = m_IA[i+1]-1;
    
      // sum up row*column elements
      summed = 0.0;
      for (k=lower; k<upper; k++)
        {
        summed += m_A[k] * rightMatrix->Get( m_JA[k]-1, j );
        }

      // insert sum to result matrix
      if (summed != 0.0)
        {
        resultMatrix->Set(i,j,summed);
        }
      
      }
    }
  
}


void ItpackSparseMatrix::SetCompressedRow(integer* ia, integer* ja, doublereal *a) 
{
  m_IA = ia;
  m_JA = ja;
  m_A = a;
  m_MatrixFinalized = 1;
  m_MatrixInitialized = 1;
}


ItpackSparseMatrix::~ItpackSparseMatrix()
{
  delete [] m_IA;
  delete [] m_JA;
  delete [] m_A;
  delete [] m_IWORK;
}


FEMExceptionItpackSparseMatrixSbagn::FEMExceptionItpackSparseMatrixSbagn(const char *file, unsigned int lineNumber, std::string location, integer errorCode) :
  FEMException(file,lineNumber)
{
  std::string solverError;

  if (errorCode == 703) 
    {
    solverError = "maximumNumberOfNonZeroValuesInMatrix is too small";
    }
  else
    {
    solverError = "Unknown error code returned";
    }

  OStringStream buf;
  buf << "Error: " << solverError;

  SetDescription(buf.str().c_str());
  
  SetLocation(location);
}

FEMExceptionItpackSparseMatrixSbsij::FEMExceptionItpackSparseMatrixSbsij(const char *file, unsigned int lineNumber, std::string location, integer errorCode) :
  FEMException(file,lineNumber)
{
  std::string solverError;

  switch (errorCode) 
    {
    case 701 :
      solverError = "Improper index of matrix";
      break;
    case 702 :
      solverError = "maximumNumberOfNonZeroValuesInMatrix is too small";
      break;
    default :
      solverError = "Unknown error code returned";
    }

  OStringStream buf;
  buf << "Error: " << solverError;

  SetDescription(buf.str().c_str());

  SetLocation(location);
}


}}  // end namespace itk::fem