DOFMatrix.cc 19 KB
Newer Older
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
#include "DOFMatrix.h"
#include <algorithm>
#include <png.h>
#include "QPsiPhi.h"
#include "BasisFunction.h"
#include "Boundary.h"
#include "DOFAdmin.h"
#include "ElInfo.h"
#include "FiniteElemSpace.h"
#include "Mesh.h"
#include "DOFVector.h"
#include "Operator.h"
#include "BoundaryCondition.h"
#include "BoundaryManager.h"
#include "ElementMatrix.h"
#include "Assembler.h"

namespace AMDiS {

  DOFMatrix *DOFMatrix::traversePtr = NULL;

  DOFMatrix::DOFMatrix()
  {
    rowFESpace = NULL;
    colFESpace = NULL;
  }

  DOFMatrix::DOFMatrix(const FiniteElemSpace* rowFESpace_,
		       const FiniteElemSpace* colFESpace_,
		       ::std::string name_)
    : rowFESpace(rowFESpace_),
      colFESpace(colFESpace_),
      name(name_), 
      coupleMatrix(false)
  {
    TEST_EXIT(rowFESpace)("no rowFESpace\n");
  
38
    if (!colFESpace) {
39
40
41
42
43
44
45
46
47
48
49
50
      colFESpace = rowFESpace;
    }

    if (rowFESpace && rowFESpace->getAdmin())
      (const_cast<DOFAdmin*>( rowFESpace->getAdmin()))->addDOFIndexed(this);

    boundaryManager = NEW BoundaryManager;
  }

  DOFMatrix::DOFMatrix(const DOFMatrix& rhs)
    : name(rhs.name+"copy")
  {
51
    *this = rhs;
52
53
54
55
56
57
    if (rowFESpace && rowFESpace->getAdmin())
      (const_cast<DOFAdmin*>( rowFESpace->getAdmin()))->addDOFIndexed(this);
  }

  DOFMatrix::~DOFMatrix()
  {
58
59
    FUNCNAME("DOFMatrix::~DOFMatrix()");

60
    if (rowFESpace && rowFESpace->getAdmin()) {
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
      (const_cast<DOFAdmin*>(rowFESpace->getAdmin()))->removeDOFIndexed(this);
    }  
  }

  void DOFMatrix::print() const
  {
    FUNCNAME("DOFMatrix::print()");
    int  i, j, jcol;
    DOFMatrix::MatrixRow row;

    int sizeUsed = rowFESpace->getAdmin()->getUsedSize();

    if (static_cast<int>(matrix.size()) < sizeUsed) {
      WARNING("DOFMatrix not yet initialized\n");
      return;
    }

    for (i = 0; i < sizeUsed; i++) { 
      row = matrix[i];
      MSG("row %3d:",i);
      int rowSize = static_cast<int>( row.size());
      for (j = 0; j < rowSize; j++) {
	jcol = row[j].col;
	if (entryUsed(i,j)) {
	  Msg::print(" (%3d,%20.17lf)", jcol, row[j].entry);
	}
      }
      Msg::print("\n");
    }
    return;
  }

  void DOFMatrix::printRow(int i) const
  {
    FUNCNAME("DOFMatrix::printRow()");

    int sizeUsed = rowFESpace->getAdmin()->getUsedSize();

    if (static_cast<int>(matrix.size()) < sizeUsed) {
      WARNING("DOFMatrix not yet initialized\n");
      return;
    }

    DOFMatrix::MatrixRow row = matrix[i];
    MSG("row %3d:",i);
    int rowSize = static_cast<int>( row.size());
    for (int j = 0; j < rowSize; j++) {
      int jcol = row[j].col;
      if (entryUsed(i,j)) {
	Msg::print(" (%3d,%20.17lf)", jcol, row[j].entry);
      }
    }
    Msg::print("\n");   
  }

  /****************************************************************************/
  /*  clear: remove all entries from dof_matrix                               */
  /****************************************************************************/

  void DOFMatrix::clear()
  {
122
123
    int mSize = static_cast<int>(matrix.size());
    for (int i = 0; i < mSize; i++) {
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
      matrix[i].resize(0);
    }
    return;
  }

  bool DOFMatrix::symmetric()
  {
    FUNCNAME("DOFMatrix::symmetric()");

    DegreeOfFreedom row, col;
    double entry, tol = 1e-5;

    DOFMatrix::Iterator matrixRow(this, USED_DOFS);

    for (matrixRow.reset(); !matrixRow.end(); ++matrixRow) {
      row = matrixRow.getDOFIndex();
      int rowSize = matrixRow->size();
      for (int i = 0; i < rowSize; i++) {
	col = (*matrixRow)[i].col;
	entry = (*matrixRow)[i].entry;
	if (abs(entry - logAcc(col, row)) > tol) {
	  MSG("matrix[%d][%d] = %e, matrix[%d][%d] = %e\n",
	      row, col, entry, col, row, logAcc(col, row));

	  return false;
	}
      }
    }

    return true;
  }

  void DOFMatrix::test()
  {
    FUNCNAME("DOFMatrix::test()");

    int non_symmetric = 0, found = 0;

    /* test symmetry */
    for (int i = 0; i < static_cast<int>(matrix.size()); i++) { 
      double sum = 0.0;
      DOFMatrix::MatrixRow *row = &matrix[i];
      for (int j = 0; j < static_cast<int>(row->size()); j++) {
	int jcol = (*row)[j].col;
	if (entryUsed(i, j)) {
	  found = 0;
	  if ((*row)[j].entry != (*row)[j].entry) {
	    MSG("mat[%d,%d]=%10.5e ???\n", i, jcol, (*row)[j].entry);
	    WAIT;
	  }
	  DOFMatrix::MatrixRow *row2 = &matrix[jcol];
	  for (int k = 0; k < static_cast<int>(row->size()); k++) {
	    int kcol = (*row)[k].col;
	    if (entryUsed(jcol, k)) {
	      if (kcol == i) {
		found = 1;
		if (abs((*row2)[k].entry - (*row)[j].entry) > 1.E-5) {
		  non_symmetric = 1;
		  MSG("mat[%d,%d]=%10.5e != mat[%d,%d]=%10.5e\n",
		      i, jcol, (*row)[j].entry, jcol, i, (*row2)[k].entry);
		}
		row2 = NULL;
		break;
	      }
	    }
	  }
	  if (!found) {
	    non_symmetric = 1;
	    MSG("mat[%d,%d] not found\n", jcol, i);
	  }
	}
      }
      if (abs(sum) > 1.E-5) {
	MSG("Zeilensumme[%d] = %10.5e\n", i, sum);
      }
    }

    if (non_symmetric) {
      MSG("matrix `%s' not symmetric.\n", name.data());
    } else {
      MSG("matrix `%s' is symmetric.\n", name.data());
    }
  }


  DOFMatrix& DOFMatrix::operator=(const DOFMatrix& rhs)
  {
    rowFESpace = rhs.rowFESpace;
    colFESpace = rhs.colFESpace;
    operators = rhs.operators;
    operatorFactor = rhs.operatorFactor;
    matrix = rhs.matrix;
    if (rhs.boundaryManager) {
      boundaryManager = new BoundaryManager(*rhs.boundaryManager);
    } else {
      boundaryManager=NULL;
    }

    return *this;
  }


  void DOFMatrix::addElementMatrix(double sign, 
				   const ElementMatrix &elMat, 
				   const BoundaryType *bound,
				   bool add)
  {
    FUNCNAME("DOFMatrix::addElementMatrix");

    DegreeOfFreedom row, col;
    double entry;

    int n_row = elMat.rowIndices.getSize();
    int n_col = elMat.colIndices.getSize();

    for (int i = 0; i < n_row; i++)  {   // for all rows of element matrix
      row = elMat.rowIndices[i];

      BoundaryCondition *condition = 
	bound ? boundaryManager->getBoundaryCondition(bound[i]) : NULL;

      if (condition && condition->isDirichlet()) {
	MatrixRow *matrixRow = &(matrix[row]);
247
	if (coupleMatrix) {
248
249
250
251
252
253
	  matrixRow->resize(0);
	} else {
	  matrixRow->resize(1);
	  ((*matrixRow)[0]).col = row;
	  ((*matrixRow)[0]).entry = 1.0;
	}     
Thomas Witkowski's avatar
Thomas Witkowski committed
254
      } else {     
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
	for (int j = 0; j < n_col; j++) {  // for all columns
	  col = elMat.colIndices[j];
	  entry = elMat[i][j];
	  addSparseDOFEntry(sign, row, col, entry, add); 
	}
      }
    }
  }



  DegreeOfFreedom DOFMatrix::logToPhysIndex(DegreeOfFreedom a,
					    DegreeOfFreedom b) const
  {
    int j;

    for (j = 0; j < static_cast<int>(matrix[a].size()); j++) 
      if (b == matrix[a][j].col) 
	break;

    return (j == static_cast<int>(matrix[a].size())) ? -1 : j;
  }


  double DOFMatrix::logAcc(DegreeOfFreedom a,DegreeOfFreedom b) const
  {
    int j;

    for (j = 0; j < static_cast<int>(matrix[a].size()); j++) 
      if (b == matrix[a][j].col) 
	break;

    return (j == static_cast<int>(matrix[a].size())) ? 0.0 : matrix[a][j].entry;
  }

  void DOFMatrix::changeColOfEntry(DegreeOfFreedom a,
				   DegreeOfFreedom b,
				   DegreeOfFreedom c)
  {
    int j;

    for (j=0; j<static_cast<int>(matrix[a].size());j++) 
      if (b==matrix[a][j].col) break;
    if (j!=static_cast<int>(matrix[a].size())) matrix[a][j].col=c;
  }

301
302
303
  void DOFMatrix::addSparseDOFEntry(double sign, int irow, 
				    int jcol, double entry,
				    bool add)
304
  {
305
    FUNCNAME("DOFMatrix::addSparseDOFEntry()");
306
307
308

    MatrixRow *row = &(matrix[irow]);

309
    if (add && !entry) 
310
      return;
311

312
313
    int freeCol = -1;
    int rowSize = static_cast<int>( row->size());
314

315
316
317
    TEST_EXIT_DBG(jcol >= 0 && 
		  jcol < colFESpace->getAdmin()->getUsedSize())
      ("Column index %d out of range 0-%d\n", jcol, colFESpace->getAdmin()->getUsedSize() - 1);
318
319

    // first entry is diagonal entry
320
    if (rowFESpace == colFESpace)
321
322
323
324
325
326
      if (rowSize == 0) {
	MatEntry newEntry = {irow, 0.0};
	row->push_back(newEntry);
	rowSize = 1;
      }

327
    int i;
328
    // search jcol
Thomas Witkowski's avatar
Thomas Witkowski committed
329
    for (i = 0; i < rowSize; i++) {
330
      // jcol found ?
Thomas Witkowski's avatar
Thomas Witkowski committed
331
      if ((*row)[i].col == jcol) {
332
333
334
	break;
      }
      // remember free entry
Thomas Witkowski's avatar
Thomas Witkowski committed
335
      if ((*row)[i].col == UNUSED_ENTRY) {
336
337
338
	freeCol = i;
      }
      // no more entries
Thomas Witkowski's avatar
Thomas Witkowski committed
339
      if ((*row)[i].col == NO_MORE_ENTRIES) {
340
	freeCol = i;
Thomas Witkowski's avatar
Thomas Witkowski committed
341
	if (rowSize > i+1) {
342
343
344
345
346
347
348
	  (*row)[i+1].entry = NO_MORE_ENTRIES;
	}
	break;
      }
    }

    // jcol found?
Thomas Witkowski's avatar
Thomas Witkowski committed
349
350
351
    if (i < rowSize) {
      if (!add) 
	(*row)[i].entry = 0.0;
352
353
      (*row)[i].entry += sign * entry;
    } else {
354
      if (freeCol == -1) {
355
356
357
358
	MatEntry newEntry = {jcol, sign * entry};
	row->push_back(newEntry);
      } else {
	(*row)[freeCol].col = jcol;
Thomas Witkowski's avatar
Thomas Witkowski committed
359
360
	if (!add) 
	  (*row)[freeCol].entry = 0.0;
361
362
	(*row)[freeCol].entry += sign * entry;
      }
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
  }

  void DOFMatrix::addMatEntry(int row, MatEntry entry)
  {
    matrix[row].push_back(entry);
  }

  void DOFMatrix::addMatEntry(int row,  DegreeOfFreedom col, double value)
  {
    MatEntry entry;
    entry.col = col;
    entry.entry = value;
    matrix[row].push_back(entry);
  }

  void DOFMatrix::addRow(::std::vector<MatEntry> row)
  {
    matrix.push_back(row);
  }

  void DOFMatrix::compressDOFIndexed(int first, int last, 
				     ::std::vector<DegreeOfFreedom> &newDOF)
  {
    int i, j, k, col;
    ::std::vector<MatEntry> *row;

    for(i = first; i <= last; i++) {
      if((k = newDOF[i]) >= 0) {
	matrix[k].swap(matrix[i]);
	matrix[i].resize(0);
      }
    }
    int usedSize = rowFESpace->getAdmin()->getUsedSize();
    for(i=0; i < usedSize; i++) {
      row = reinterpret_cast< ::std::vector<MatEntry>*>(&(matrix[i])); 
      int rowSize = static_cast<int>(row->size());
      for (j=0; j < rowSize; j++) {
	col = (*row)[j].col;
	if (entryUsed(i,j)) (*row)[j].col = newDOF[col];
      }    
    }
  }

  void DOFMatrix::freeDOFContent(int index)
  {
    int        i, j, col=0, col2;

    if (0 < matrix[index].size()) {
      // for all columns in this row
      int size = static_cast<int>(matrix[index].size());
      for (i=0; i<size; i++) {
	// if entry is used
	if (entryUsed(index,i)) {
	  // get column of this entry
	  col = matrix[index][i].col;
	  if (col != index) {  // remove symmetric entry if exists
	    int colsize = static_cast<int>(matrix[col].size());
	    for (j=0; j< colsize; j++) {
	      col2 = matrix[col][j].col;
	      if (col2 == index) {
		matrix[col][j].col = DOFMatrix::UNUSED_ENTRY;
	      }
	      else if (col2 == DOFMatrix::NO_MORE_ENTRIES) {
		break;
	      }
	    }
	  }
	}
	else if (col == DOFMatrix::NO_MORE_ENTRIES) {
	  break;
	}
      }
      matrix[index].resize(0);	
    }
  }


441
442
  void DOFMatrix::assemble(double factor, ElInfo *elInfo, 
			   const BoundaryType *bound, Operator *op)
443
  {
444
    FUNCNAME("DOFMatrix::assemble()");
445

446
    if (!op && operators.size() == 0) {
447
      return;
448
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
449
    
450
    Operator *operat = op ? op : operators[0];
451
452
453
454

    // Do no combine the next two lines into one!
    ElementMatrix *elementMatrix = NULL;
    elementMatrix = operat->getAssembler(omp_get_thread_num())->initElementMatrix(elementMatrix, elInfo);
Thomas Witkowski's avatar
Thomas Witkowski committed
455
    
456
    if (op) {
457
458
459
460
      op->getElementMatrix(elInfo, elementMatrix);
    } else {
      ::std::vector<Operator*>::iterator it;
      ::std::vector<double*>::iterator factorIt;
461
      for (it = operators.begin(), factorIt = operatorFactor.begin();	
462
463
	   it != operators.end(); 
	   ++it, ++factorIt) 
464
465
466
467
468
	{
	  (*it)->getElementMatrix(elInfo, 
				  elementMatrix, 
				  *factorIt ? **factorIt : 1.0);
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
469
      
470
    }
471
472
473
474
   
    addElementMatrix(factor, *elementMatrix, bound);   

    DELETE elementMatrix;
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
  }

  Flag DOFMatrix::getAssembleFlag()
  {
    Flag fillFlag(0);
    ::std::vector<Operator*>::iterator op;
    for(op = operators.begin(); op != operators.end(); ++op) {
      fillFlag |= (*op)->getFillFlag();
    }
    return fillFlag;
  }

  void DOFMatrix::mm(MatrixTranspose aTranspose,
		     DOFMatrix& a, 
		     MatrixTranspose bTranspose,
		     DOFMatrix& b)
  {
    FUNCNAME("DOFMatrix::mm()");

    WARNING("implementation not finished!!!\n");

496
    TEST_EXIT_DBG(a.getColFESpace() == b.getRowFESpace())
497
      ("a.colFESpace != b.rowFESpace\n");
498
    TEST_EXIT_DBG(rowFESpace == a.getRowFESpace())
499
      ("rowFESpace != a.rowFESpace\n");  
500
    TEST_EXIT_DBG(colFESpace == b.getColFESpace())
501
502
503
504
      ("colFESpace != b.colFESpace\n");  

    clear();

505
    int i, j;
506

507
    if (aTranspose == NoTranspose && bTranspose == NoTranspose) {
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
      int cols  = b.getColFESpace()->getAdmin()->getUsedSize();
      DOFMatrix::Iterator rowIterator(this, USED_DOFS);

      // for every row ...
      for(rowIterator.reset(); !rowIterator.end(); ++rowIterator) {
	int rowIndex = rowIterator.getDOFIndex();
	// and every column of result   
	for(i=0; i < cols; i++) {
	  double entry = 0.0;
	  // for every entry in a[i] ...
	  for(j=0; j < static_cast<int>( a[rowIndex].size()); j++) {
	    int logIndex = a[rowIndex][j].col;
	    int physIndex = b.logToPhysIndex(logIndex, i);
	    if(physIndex != -1) {
	      entry += a[rowIndex][j].entry * b[logIndex][physIndex].entry;
	    }
	  }
525
	  if (entry != 0.0) {
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
	    addSparseDOFEntry(1.0, rowIndex, i, entry);
	  }
	}
      }
    } else if(aTranspose == Transpose && bTranspose == NoTranspose) {
      DOFMatrix::Iterator aIterator(&a, USED_DOFS);
      DOFMatrix::Iterator bIterator(&b, USED_DOFS);
      for(aIterator.reset(), bIterator.reset(); 
	  !aIterator.end(); 
	  ++aIterator, ++bIterator) 
	{
	  ::std::vector<MatEntry>::const_iterator aRowIt;
	  ::std::vector<MatEntry>::const_iterator bRowIt;
	  for(aRowIt = aIterator->begin(); aRowIt != aIterator->end(); ++aRowIt) {
	    int aCol = aRowIt->col;
	    if(aCol == UNUSED_ENTRY) continue;
	    if(aCol == NO_MORE_ENTRIES) break;
	    for(bRowIt = bIterator->begin(); bRowIt !=bIterator->end(); ++bRowIt) {
	      int bCol = bRowIt->col;
	      if(bCol == UNUSED_ENTRY) continue;
	      if(bCol == NO_MORE_ENTRIES) break;

	      double entry = aRowIt->entry * bRowIt->entry;

550
	      if (entry != 0.0) {
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
		addSparseDOFEntry(1.0, aCol, bCol, entry);	    
	      }
	    }
	  }
	}
    } else if(aTranspose == NoTranspose && bTranspose == Transpose) {
      ERROR_EXIT("not yet\n");
    } else if(aTranspose == Transpose && bTranspose == Transpose) {
      ERROR_EXIT("not yet\n");
    }
  }

  void DOFMatrix::axpy(double a, 
		       const DOFMatrix& x,
		       const DOFMatrix& y)
  {
567
568
569
570
    FUNCNAME("DOFMatrix::axpy()");

    TEST_EXIT_DBG(x.getRowFESpace() == y.getRowFESpace() &&
		  rowFESpace == x.getRowFESpace())
571
      ("row fe-spaces not equal\n");
572
573
    TEST_EXIT_DBG(x.getColFESpace() == y.getColFESpace() &&
		  colFESpace == x.getColFESpace())
574
      ("col fe-spaces not equal\n");
575
    
576
577
578
579
580
581
582
583
584
585
586
587
588
589
    DOFMatrix::Iterator rowIterator(this, USED_DOFS);
    DOFMatrix::Iterator xIterator(const_cast<DOFMatrix*>(&x), USED_DOFS);
    DOFMatrix::Iterator yIterator(const_cast<DOFMatrix*>(&y), USED_DOFS);

    int i, rowIndex, colIndex;

    for(rowIterator.reset(), xIterator.reset(), yIterator.reset();
	!rowIterator.end();
	++rowIterator, ++xIterator, ++yIterator)
      {
	rowIndex = rowIterator.getDOFIndex();
	// add x contributions to this row
	for(i=0; i < static_cast<int>((*xIterator).size()); i++) {
	  colIndex = (*xIterator)[i].col;
590
	  if (colIndex >= 0) {
591
592
593
594
595
596
	    addSparseDOFEntry(a, rowIndex, colIndex, (*xIterator)[i].entry);
	  }
	}
	// add y contributions to this row
	for(i=0; i < static_cast<int>((*yIterator).size()); i++) {
	  colIndex = (*yIterator)[i].col;
597
	  if (colIndex >= 0) {
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
	    addSparseDOFEntry(1.0, rowIndex, colIndex, (*yIterator)[i].entry);
	  }
	}
      }
  }

  void DOFMatrix::scal(double b) 
  {
    int i;
    DOFMatrix::Iterator rowIterator(this, USED_DOFS);
    for(rowIterator.reset(); !rowIterator.end(); ++rowIterator) {
      for(i=0; i < static_cast<int>((*rowIterator).size()); i++) {
	if((*rowIterator)[i].col >= 0) {
	  (*rowIterator)[i].entry *= b;
	}
      }
    }
  }

  void DOFMatrix::copy(const DOFMatrix& rhs) 
  {
    clear();
    DOFMatrix::Iterator rhsIterator(const_cast<DOFMatrix*>(&rhs), USED_DOFS);
    DOFMatrix::Iterator thisIterator(this, USED_DOFS);
    ::std::vector<MatEntry>::const_iterator colIt;
    ::std::vector<MatEntry>::const_iterator colBegin;
    ::std::vector<MatEntry>::const_iterator colEnd;
    for(rhsIterator.reset(), thisIterator.reset();
	!rhsIterator.end(); 
	++rhsIterator, ++thisIterator) 
      {
	colBegin = rhsIterator->begin();
	colEnd = rhsIterator->end();
	for(colIt = colBegin; colIt != colEnd; ++colIt) {
	  MatEntry matEntry;
	  matEntry.col = colIt->col;
	  matEntry.entry = colIt->entry;
	  thisIterator->push_back(matEntry);
	}
      }
  }

  void DOFMatrix::createPictureFile(const char* filename, int dim)
  {
    png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);

    if (!png_ptr)
       return;

    png_bytep rowPointers[dim];
    for (int i = 0; i < dim; i++) {
      rowPointers[i] = (png_byte*)png_malloc(png_ptr, dim);

      for (int j = 0; j < dim; j++) {
	rowPointers[i][j] = 255;
      }
    }

    double scalFactor = static_cast<double>(dim) / static_cast<double>(matrix.size());

    for (int i = 0; i < static_cast<int>(matrix.size()); i++) {    
      int pi = static_cast<int>(static_cast<double>(i) * scalFactor);

661
      TEST_EXIT_DBG((pi >= 0) && (pi < dim))("PI");
662
663
664
665
666

      for (int j = 0; j < static_cast<int>(matrix[i].size()); j++) {
	
	int pj = static_cast<int>(static_cast<double>(matrix[i][j].col) * scalFactor);

667
	TEST_EXIT_DBG((pj >= 0) && (pj < dim))("PJ");
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758

	rowPointers[pi][pj] = 0;
      }
    }
   
    FILE *fp = fopen(filename, "wb");
    TEST_EXIT(fp)("Cannot open file for writing matrix picture file!\n");

    png_infop info_ptr = png_create_info_struct(png_ptr);

    if (!info_ptr) {
       png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
       return;
    }

    png_init_io(png_ptr, fp);

    png_set_IHDR(png_ptr, info_ptr, dim, dim, 8,
		 PNG_COLOR_TYPE_GRAY, 
		 PNG_INTERLACE_NONE,
		 PNG_COMPRESSION_TYPE_DEFAULT,
		 PNG_FILTER_TYPE_DEFAULT);

    png_set_rows(png_ptr, info_ptr, rowPointers);

    png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);

    png_destroy_write_struct(&png_ptr, &info_ptr);

    fclose(fp);
  }


  int DOFMatrix::memsize() 
  {   
    int sizeDOFMatrix = sizeof(DOFMatrix);   
    int sizeMatrix = sizeof(::std::vector<MatrixRow>);
    for (int i = 0; i < static_cast<int>(matrix.size()); i++) {
      sizeMatrix += sizeof(MatrixRow) + matrix[i].size() * sizeof(MatEntry);
    }
    
    return sizeDOFMatrix + sizeMatrix;
  }


  double norm(::std::vector<MatEntry> *row)
  {
    double result = 0.0;
    ::std::vector<MatEntry>::iterator it;
    for (it = row->begin(); it < row->end(); ++it) {
      result += (*it).entry * (*it).entry;
    }

    return(sqrt(result));
  }

  double min(::std::vector<MatEntry> *row)
  {
    double result = 0.0;
    if (row->size() > 0) {
      result = (*row)[0].entry;
    }

    ::std::vector<MatEntry>::iterator it;
    for (it = row->begin(); it < row->end(); ++it) {
      if ((*it).entry < result) {
	result = (*it).entry;
      }
    }

    return(result);
  }

  double max(::std::vector<MatEntry> *row)
  {
    double result = 0.0;
    if (row->size() > 0) {
      result = (*row)[0].entry;
    }

    ::std::vector<MatEntry>::iterator it;
    for (it = row->begin(); it < row->end(); ++it) {
      if ((*it).entry > result) {
	result = (*it).entry;
      }
    }

    return(result);
  }

}