17 #include "itkNumericTraits.h"
56 m_RPARM[7] = 500.0 * NumericTraits<double>::min();
71 throw FEMExceptionLinearSystem(__FILE__, __LINE__,
"LinearSystemWrapperItpack::InitializeMatrix",
"System order not set");
79 throw FEMExceptionLinearSystem(__FILE__, __LINE__,
"LinearSystemWrapperItpack::InitializeMatrix",
"Maximum number of non zeros not set");
89 (*m_Matrices)[matrixIndex].Clear();
90 (*m_Matrices)[matrixIndex].SetOrder(
m_Order);
101 if ( !(*
m_Matrices)[matrixIndex].GetOrder() )
return false;
102 if ( !(*
m_Matrices)[matrixIndex].GetMaxNonZeroValues() )
return false;
114 throw FEMExceptionLinearSystem(__FILE__, __LINE__,
"LinearSystemWrapperItpack::InitializeVector",
"System order not set");
130 delete [] (*m_Vectors)[vectorIndex];
137 for (
unsigned int i=0; i<
m_Order; i++)
139 (*m_Vectors)[vectorIndex][i] = 0.0;
150 if ( !(*
m_Vectors)[vectorIndex] )
return false;
162 throw FEMExceptionLinearSystem(__FILE__, __LINE__,
"LinearSystemWrapperItpack::InitializeSolution",
"System order not set");
179 delete [] (*m_Solutions)[solutionIndex];
186 for (
unsigned int i=0; i<
m_Order; i++)
188 (*m_Solutions)[solutionIndex][i] = 0.0;
199 if ( !(*
m_Solutions)[solutionIndex] )
return false;
214 (*m_Matrices)[matrixIndex].Clear();
227 if ( !(*
m_Vectors)[vectorIndex] )
return;
230 delete [] (*m_Vectors)[vectorIndex];
231 (*m_Vectors)[vectorIndex] = 0;
247 delete [] (*m_Solutions)[solutionIndex];
248 (*m_Solutions)[solutionIndex] = 0;
258 throw FEMExceptionLinearSystem(__FILE__, __LINE__,
"LinearSystemWrapperItpack::GetMatrixValue",
"No matrices have been allocated");
279 throw FEMExceptionLinearSystem(__FILE__, __LINE__,
"LinearSystemWrapperItpack::SetMatrixValue",
"No matrices have been allocated");
291 ((*m_Matrices)[matrixIndex]).Set(i,j,value);
300 throw FEMExceptionLinearSystem(__FILE__, __LINE__,
"LinearSystemWrapperItpack::AddMatrixValue",
"No matrices have been allocated");
311 ((*m_Matrices)[matrixIndex]).Add(i,j,value);
321 throw FEMExceptionLinearSystem(__FILE__, __LINE__,
"LinearSystemWrapperItpack::GetColumnsOfNonZeroMatrixElementsInRow",
"No matrices have been allocated");
325 throw FEMExceptionLinearSystemBounds(__FILE__, __LINE__,
"LinearSystemWrapperItpack::GetColumnsOfNonZeroMatrixElementsInRow",
"m_Matrices[]", row);
329 throw FEMExceptionLinearSystemBounds(__FILE__, __LINE__,
"LinearSystemWrapperItpack::GetColumnsOfNonZeroMatrixElementsInRow",
"m_Matrices", matrixIndex);
340 unsigned int lower = mat->
m_IA[row]-1;
341 unsigned int upper = mat->
m_IA[row+1]-1;
343 for(
unsigned int j=lower; j<upper; j++)
345 cols.push_back(mat->
m_JA[j]-1);
350 int wrk=mat->
m_IA[row]-1;
353 cols.push_back(mat->
m_JA[wrk]-1);
366 throw FEMExceptionLinearSystem(__FILE__, __LINE__,
"LinearSystemWrapperItpack::ScaleMatrix",
"No matrices have been allocated");
374 doublereal *values = (*m_Matrices)[matrixIndex].GetA();
375 for (i=0; i<(*m_Matrices)[matrixIndex].GetIA()[this->
m_Order] - 1; i++)
377 values[i] = values[i]*scale;
388 throw FEMExceptionLinearSystem(__FILE__, __LINE__,
"LinearSystemWrapperItpack::GetVectorValue",
"No vectors have been allocated");
400 throw FEMExceptionLinearSystem(__FILE__, __LINE__,
"LinearSystemWrapperItpack::GetVectorValue",
"Indexed vector not yet allocated");
413 throw FEMExceptionLinearSystem(__FILE__, __LINE__,
"LinearSystemWrapperItpack::SetVectorValue",
"No vectors have been allocated");
425 throw FEMExceptionLinearSystem(__FILE__, __LINE__,
"LinearSystemWrapperItpack::SetVectorValue",
"Indexed vector not yet allocated");
429 (*m_Vectors)[vectorIndex][i] = value;
438 throw FEMExceptionLinearSystem(__FILE__, __LINE__,
"LinearSystemWrapperItpack::AddVectorValue",
"No vectors have been allocated");
450 throw FEMExceptionLinearSystem(__FILE__, __LINE__,
"LinearSystemWrapperItpack::AddVectorValue",
"Indexed vector not yet allocated");
454 (*m_Vectors)[vectorIndex][i] += value;
474 throw FEMExceptionLinearSystem(__FILE__, __LINE__,
"LinearSystemWrapperItpack::SetSolutionValue",
"No solutions have been allocated");
486 throw FEMExceptionLinearSystem(__FILE__, __LINE__,
"LinearSystemWrapperItpack::SetSolutionValue",
"Indexed solution not yet allocated");
490 (*m_Solutions)[solutionIndex][i] = value;
500 throw FEMExceptionLinearSystem(__FILE__, __LINE__,
"LinearSystemWrapperItpack::AddSolutionValue",
"No solutions have been allocated");
512 throw FEMExceptionLinearSystem(__FILE__, __LINE__,
"LinearSystemWrapperItpack::AddSolutionValue",
"Indexed solution not yet allocated");
515 (*m_Solutions)[solutionIndex][i] += value;
525 throw FEMExceptionLinearSystem(__FILE__, __LINE__,
"LinearSystemWrapperItpack::Solve",
"Not all necessary data members have been allocated");
528 throw FEMExceptionLinearSystem(__FILE__, __LINE__,
"LinearSystemWrapperItpack::AddSolutionValue",
"Primary matrix never filled");
627 for (i=0; i<(3*N); i++)
635 int max_num_iterations=
m_IPARM[0];
639 (*m_Vectors)[0], (*m_Solutions)[0], &(IWKSP[0]), &NW, &(WKSP[0]), &(
m_IPARM[0]), &(
m_RPARM[0]), &IERR);
646 if ( (IERR % 10) == 3 )
671 throw FEMExceptionLinearSystem(__FILE__, __LINE__,
"LinearSystemWrapperItpack::SwapMatrices",
"No matrices allocated");
682 int n = (*m_Matrices)[matrixIndex2].GetOrder();
683 int nz = (*m_Matrices)[matrixIndex2].GetMaxNonZeroValues();
684 integer* ia = (*m_Matrices)[matrixIndex2].GetIA();
685 integer *ja = (*m_Matrices)[matrixIndex2].GetJA();
686 doublereal *a = (*m_Matrices)[matrixIndex2].GetA();
688 (*m_Matrices)[matrixIndex2].SetOrder( (*
m_Matrices)[matrixIndex1].GetOrder() );
689 (*m_Matrices)[matrixIndex2].SetMaxNonZeroValues ( (*
m_Matrices)[matrixIndex1].GetMaxNonZeroValues() );
690 (*m_Matrices)[matrixIndex2].SetCompressedRow((*
m_Matrices)[matrixIndex1].GetIA(),
693 (*m_Matrices)[matrixIndex1].SetOrder( n );
694 (*m_Matrices)[matrixIndex1].SetMaxNonZeroValues ( nz );
695 (*m_Matrices)[matrixIndex1].SetCompressedRow(ia, ja, a);
706 throw FEMExceptionLinearSystem(__FILE__, __LINE__,
"LinearSystemWrapperItpack::SwapVectors",
"No vectors allocated");
719 (*m_Vectors)[vectorIndex1] = (*m_Vectors)[vectorIndex2];
720 (*m_Vectors)[vectorIndex2] = temp;
729 throw FEMExceptionLinearSystem(__FILE__, __LINE__,
"LinearSystemWrapperItpack::SwapSolutions",
"No solutions allocated");
742 (*m_Solutions)[solutionIndex1] = (*m_Solutions)[solutionIndex2];
743 (*m_Solutions)[solutionIndex2] = temp;
753 throw FEMExceptionLinearSystem(__FILE__, __LINE__,
"LinearSystemWrapperItpack::CopySolution2Vector",
"No vectors allocated");
757 throw FEMExceptionLinearSystem(__FILE__, __LINE__,
"LinearSystemWrapperItpack::CopySolution2Vector",
"No solutions allocated");
772 for (
unsigned int i=0; i<
m_Order; i++)
774 (*m_Vectors)[vectorIndex][i] = (*m_Solutions)[solutionIndex][i];
785 throw FEMExceptionLinearSystem(__FILE__, __LINE__,
"LinearSystemWrapperItpack::CopySolution2Vector",
"No vectors allocated");
789 throw FEMExceptionLinearSystem(__FILE__, __LINE__,
"LinearSystemWrapperItpack::CopySolution2Vector",
"No solutions allocated");
804 for (
unsigned int i=0; i<
m_Order; i++)
806 (*m_Solutions)[solutionIndex][i] = (*m_Vectors)[vectorIndex][i];
816 throw FEMExceptionLinearSystem(__FILE__, __LINE__,
"LinearSystemWrapperItpack::MultiplyMatrixMatrix",
"No matrices allocated");
831 (*m_Matrices)[leftMatrixIndex].mult( &((*
m_Matrices)[rightMatrixIndex]), &((*
m_Matrices)[resultMatrixIndex]) );
842 throw FEMExceptionLinearSystem(__FILE__, __LINE__,
"LinearSystemWrapperItpack::MultiplyMatrixVector",
"No matrices allocated");
846 throw FEMExceptionLinearSystem(__FILE__, __LINE__,
"LinearSystemWrapperItpack::MultiplyMatrixVector",
"No vectors allocated");
862 (*m_Matrices)[matrixIndex].mult( (*
m_Vectors)[vectorIndex], (*
m_Vectors)[resultVectorIndex] );
878 delete [] (*m_Vectors)[i];
891 delete [] (*m_Solutions)[i];
905 std::string solverError;
909 errorCode = errorCode % 10;
915 solverError =
"Invalid order of system";
918 solverError =
"Workspace is not large enough";
921 solverError =
"Failure to converge before reaching maximum number of iterations";
924 solverError =
"Invalid order of black subsystem";
927 solverError =
"A diagonal element is not positive";
930 solverError =
"No diagonal element in a row";
933 solverError =
"Red-black indexing is not possible";
936 solverError =
"No entry in a row of the original matrix";
939 solverError =
"No entry in a row of the permuted matrix";
942 solverError =
"Sorting error in a row of the permuted matrix";
945 solverError =
"A diagonal element is not positive";
948 solverError =
"No diagonal element in a row";
951 solverError =
"Failure to converge before reaching maximum number of iterations";
954 solverError =
"Function does not change sign at endpoints";
957 solverError =
"Successive iterations are not monotone increasing";
960 solverError =
"Unknown error code returned";
964 buf <<
"Error: " << solverError;