Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added new linear solver based on singular value decomposition in Nox #19

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions trilinos-nox/packages/nox/src-lapack/NOX_LAPACK_LinearSolver.H
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ namespace NOX {

//! LAPACK wrappers
Teuchos::LAPACK<int,T> lapack;


int lwork;
int rank;
double* sv;//singular values
double* work;

};

Expand All @@ -150,7 +156,10 @@ NOX::LAPACK::LinearSolver<T>::LinearSolver(int n) :
pivots(n),
isValidLU(false),
blas(),
lapack()
lapack(),
lwork(10*mat.numRows()+1),
sv(new double [mat.numRows()]),//this array is only used as output and is not necessary
work(new double [lwork])//this array is only used as output and is not necessary
{
}

Expand All @@ -161,13 +170,18 @@ NOX::LAPACK::LinearSolver<T>::LinearSolver(const NOX::LAPACK::LinearSolver<T>& s
pivots(s.pivots),
isValidLU(s.isValidLU),
blas(),
lapack()
lapack(),
lwork(10*mat.numRows()+1),
sv(new double [mat.numRows()]),//this array is only used as output and is not necessary
work(new double [lwork])//this array is only used as output and is not necessary
{
}

template <typename T>
NOX::LAPACK::LinearSolver<T>::~LinearSolver()
{
if(sv) delete [] sv;
if(work) delete [] work;
}

template <typename T>
Expand All @@ -179,6 +193,9 @@ NOX::LAPACK::LinearSolver<T>::operator=(const NOX::LAPACK::LinearSolver<T>& s)
lu = s.lu;
pivots = s.pivots;
isValidLU = s.isValidLU;
lwork = s.lwork;
sv = new double [mat.numRows()];//this array is only used as output and is not necessary
work = new double [lwork];//this array is only used as output and is not necessary
}

return *this;
Expand Down Expand Up @@ -229,13 +246,17 @@ NOX::LAPACK::LinearSolver<T>::solve(bool trans, int ncols, T* output)
{
int info;
int n = mat.numRows();
int rcond = -1.0;

// Compute LU factorization if necessary
if (!isValidLU) {
lu = mat;
lapack.GETRF(n, n, &lu(0,0), n, &pivots[0], &info);
if (info != 0)
if (info != 0){//try solving a least squares problem instead (in case the linear system has infinitely many solutions)
lu=mat;
lapack.GELSS(n,n,ncols,&lu(0,0), n, output, n, sv, rcond, &rank, work, lwork, &info);
return false;
}
isValidLU = true;
}

Expand Down