UMFPACKSolver.cc 6.68 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
/*===========================================================================*\
 *                                                                           *
 *                               CoMISo                                      *
 *      Copyright (C) 2008-2009 by Computer Graphics Group, RWTH Aachen      *
 *                           www.rwth-graphics.de                            *
 *                                                                           *
 *---------------------------------------------------------------------------* 
 *  This file is part of CoMISo.                                             *
 *                                                                           *
 *  CoMISo is free software: you can redistribute it and/or modify           *
 *  it under the terms of the GNU General Public License as published by     *
 *  the Free Software Foundation, either version 3 of the License, or        *
 *  (at your option) any later version.                                      *
 *                                                                           *
 *  CoMISo is distributed in the hope that it will be useful,                *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of           *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
 *  GNU General Public License for more details.                             *
 *                                                                           *
 *  You should have received a copy of the GNU General Public License        *
 *  along with CoMISo.  If not, see <http://www.gnu.org/licenses/>.          *
 *                                                                           *
\*===========================================================================*/ 


26 27 28
//== COMPILE-TIME PACKAGE REQUIREMENTS ========================================
#include <CoMISo/Config/config.hh>
#if COMISO_SUITESPARSE_AVAILABLE
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

#include "UMFPACKSolver.hh"


namespace COMISO {

UMFPACKSolver::UMFPACKSolver()
{
  // initialize zero pointers
  symbolic_ = 0;
  numeric_  = 0;

  tolerance_ = 1e-8;
  
  //  ordering_ = CHOLMOD_AMD;
    
  show_timings_ = false;
}


  //-----------------------------------------------------------------------------
  

UMFPACKSolver::~UMFPACKSolver()
{
    if( symbolic_ )
    {
      umfpack_di_free_symbolic( &symbolic_);
      symbolic_ = 0;
    }

    if( numeric_ )
    {
      umfpack_di_free_numeric( &numeric_);
      numeric_ = 0;
    }
}
  

//-----------------------------------------------------------------------------


bool UMFPACKSolver::calc_system( const std::vector<int>&    _colptr, 
				 const std::vector<int>&    _rowind, 
				 const std::vector<double>& _values)
{
  if(show_timings_) sw_.start();

  colptr_ = _colptr;
  rowind_ = _rowind;
  values_ = _values;

Max Lyon's avatar
Max Lyon committed
81
  int n = (int)colptr_.size() - 1;
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

  // clean up
  if( symbolic_ )
  {
    umfpack_di_free_symbolic( &symbolic_);
    symbolic_ = 0;
  }
  if( numeric_ )
  {
    umfpack_di_free_numeric( &numeric_);
    numeric_ = 0;
  }

  if(show_timings_)
  {
    std::cerr << " UMFPACK Timing cleanup: " << sw_.stop()/1000.0 << "s\n";
    sw_.start();
  }

  int status;

  // symbolic factorization
  status = umfpack_di_symbolic(n,n,&(colptr_[0]), &(rowind_[0]), &(values_[0]), &symbolic_, 0, 0);

  if( status != UMFPACK_OK )
  {
    std::cout << "UMFPACK_symbolic failed" << std::endl;
    print_error( status);
    return false;
  }

  if(show_timings_)
  {
    std::cerr << " SuiteSparseQR_symbolic Timing: " << sw_.stop()/1000.0 << "s\n";
    sw_.start();
  }

  // numeric factorization
  status = umfpack_di_numeric ( &(colptr_[0]), &(rowind_[0]), &(values_[0]), symbolic_, &numeric_, 0, 0);

  if( status != UMFPACK_OK )
  {
    std::cout << "UMFPACK_numeric failed" << std::endl;
    print_error( status);
    return false;
  }

  if(show_timings_)
  {
    std::cerr << " SuiteSparseQR_numeric Timing: " << sw_.stop()/1000.0 << "s\n";
    sw_.start();
  }

  return true;
}



  //-----------------------------------------------------------------------------

    
void UMFPACKSolver::print_error( int _status )
{
  switch(_status)
  {
147 148 149 150 151 152
    case UMFPACK_OK                    : std::cerr << "UMFPACK Error: UMFPACK_OK\n"; break;
    case UMFPACK_ERROR_n_nonpositive   : std::cerr << "UMFPACK Error: UMFPACK_ERROR_n_nonpositive\n"; break;
    case UMFPACK_ERROR_invalid_matrix  : std::cerr << "UMFPACK Error: UMFPACK_ERROR_invalid_matrix\n"; break;
    case UMFPACK_ERROR_out_of_memory   : std::cerr << "UMFPACK Error: UMFPACK_ERROR_out_of_memory\n"; break;
    case UMFPACK_ERROR_argument_missing: std::cerr << "UMFPACK Error: UMFPACK_ERROR_argument_missing\n"; break;
    case UMFPACK_ERROR_internal_error  : std::cerr << "UMFPACK Error: UMFPACK_ERROR_internal_error\n"; break;
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
    default: std::cerr << "UMFPACK Error: UNSPECIFIED ERROR\n";
  }
}

  //-----------------------------------------------------------------------------

    
bool UMFPACKSolver::update_system( const std::vector<int>& _colptr, 
				   const std::vector<int>& _rowind, 
				   const std::vector<double>& _values )
{
  if( !symbolic_ )
    return false;


  if(show_timings_) sw_.start();

  colptr_ = _colptr;
  rowind_ = _rowind;
  values_ = _values;

  if( numeric_ )
  {
    umfpack_di_free_numeric( &numeric_);
    numeric_ = 0;
  }

  if(show_timings_)
  {
    std::cerr << " UMFPACK Timing cleanup: " << sw_.stop()/1000.0 << "s\n";
    sw_.start();
  }

  int status;

  // numeric factorization
  status = umfpack_di_numeric ( &(colptr_[0]), &(rowind_[0]), &(values_[0]), symbolic_, &numeric_, 0, 0);

  if( status != UMFPACK_OK )
  {
    std::cout << "UMFPACK_numeric failed" << std::endl;
    print_error( status);
    return false;
  }

  if(show_timings_)
  {
    std::cerr << " SuiteSparseQR_numeric Timing: " << sw_.stop()/1000.0 << "s\n";
    sw_.start();
  }

  return true;
}


//-----------------------------------------------------------------------------
  

bool UMFPACKSolver::solve( double * _x, double * _b)
{
  if(show_timings_) sw_.start();

  //  const unsigned int n = colptr_.size() - 1;

  int status = umfpack_di_solve( UMFPACK_A, &(colptr_[0]), &(rowind_[0]), &(values_[0]), 
				 _x, _b, numeric_, 0, 0);


  if( status != UMFPACK_OK )
  {
    std::cout << "UMFPACK_solve failed" << std::endl;
    print_error( status);
    return false;
  }

  if(show_timings_)
  {
    std::cerr << " UMFPACK_sove Timing: " << sw_.stop()/1000.0 << "s\n";
    sw_.start();
  }


    return true;
}


//-----------------------------------------------------------------------------


  
}
244 245 246 247

//=============================================================================
#endif // COMISO_SUITESPARSE_AVAILABLE
//=============================================================================