| [bcf653] | 1 | /* | 
|---|
|  | 2 | * Project: MoleCuilder | 
|---|
|  | 3 | * Description: creates and alters molecular systems | 
|---|
|  | 4 | * Copyright (C)  2010 University of Bonn. All rights reserved. | 
|---|
|  | 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
| [f60610] | 8 | /* | 
|---|
| [78b593] | 9 | * LinearSystemOfEquations.cpp | 
|---|
| [f60610] | 10 | * | 
|---|
|  | 11 | *  Created on: Jan 8, 2010 | 
|---|
|  | 12 | *      Author: heber | 
|---|
|  | 13 | */ | 
|---|
|  | 14 |  | 
|---|
| [bf3817] | 15 | // include config.h | 
|---|
|  | 16 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 17 | #include <config.h> | 
|---|
|  | 18 | #endif | 
|---|
|  | 19 |  | 
|---|
| [ad011c] | 20 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| [112b09] | 21 |  | 
|---|
| [e4fe8d] | 22 | #include "Helpers/defs.hpp" | 
|---|
| [78b593] | 23 | #include "LinearAlgebra/LinearSystemOfEquations.hpp" | 
|---|
| [0d4424] | 24 | #include "LinearAlgebra/MatrixContent.hpp" | 
|---|
| [bbf1bd] | 25 | #include "LinearAlgebra/VectorContent.hpp" | 
|---|
| [ad011c] | 26 | #include "CodePatterns/Assert.hpp" | 
|---|
|  | 27 | #include "CodePatterns/logger.hpp" | 
|---|
| [57f243] | 28 | #include "LinearAlgebra/Vector.hpp" | 
|---|
| [f60610] | 29 |  | 
|---|
|  | 30 | #include <gsl/gsl_permutation.h> | 
|---|
|  | 31 |  | 
|---|
|  | 32 | /** Constructor for class LinearSystemOfEquations. | 
|---|
|  | 33 | * Allocates Vector and Matrix classes. | 
|---|
|  | 34 | * \param m column dimension | 
|---|
|  | 35 | * \param n row dimension | 
|---|
|  | 36 | */ | 
|---|
|  | 37 | LinearSystemOfEquations::LinearSystemOfEquations(int m, int n) : rows(m), columns(n), IsSymmetric(false) | 
|---|
|  | 38 | { | 
|---|
| [0d4424] | 39 | A = new MatrixContent(m, n); | 
|---|
| [bbf1bd] | 40 | b = new VectorContent(m); | 
|---|
|  | 41 | x = new VectorContent(n); | 
|---|
| [f60610] | 42 | }; | 
|---|
|  | 43 |  | 
|---|
|  | 44 | /** Destructor for class LinearSystemOfEquations. | 
|---|
|  | 45 | * Destructs Vector and Matrix classes. | 
|---|
|  | 46 | */ | 
|---|
|  | 47 | LinearSystemOfEquations::~LinearSystemOfEquations() | 
|---|
|  | 48 | { | 
|---|
|  | 49 | delete(A); | 
|---|
|  | 50 | delete(b); | 
|---|
|  | 51 | delete(x); | 
|---|
|  | 52 | }; | 
|---|
|  | 53 |  | 
|---|
|  | 54 | /** Sets whether matrix is to be regarded as symmetric. | 
|---|
|  | 55 | * Note that we do not check whether it really is, just take upper diagonal. | 
|---|
|  | 56 | * \param symmetric true or false | 
|---|
|  | 57 | */ | 
|---|
|  | 58 | bool LinearSystemOfEquations::SetSymmetric(bool symmetric) | 
|---|
|  | 59 | { | 
|---|
| [bbf1bd] | 60 | ASSERT (rows == columns, "Rows and columns don't have equal size! Setting symmetric not possible."); | 
|---|
| [f60610] | 61 | return (IsSymmetric = symmetric); | 
|---|
|  | 62 | }; | 
|---|
|  | 63 |  | 
|---|
|  | 64 | /** Initializes vector b to the components of the given vector. | 
|---|
|  | 65 | * \param *x Vector with equal dimension (no check!) | 
|---|
|  | 66 | */ | 
|---|
|  | 67 | void LinearSystemOfEquations::Setb(Vector *x) | 
|---|
|  | 68 | { | 
|---|
| [bbf1bd] | 69 | ASSERT  ( columns == NDIM, "Vector class is always three-dimensional, unlike this LEqS!"); | 
|---|
|  | 70 | b->setFromVector(*x); | 
|---|
| [f60610] | 71 | }; | 
|---|
|  | 72 |  | 
|---|
|  | 73 | /** Initializes vector b to the components of the given vector. | 
|---|
|  | 74 | * \param *x array with equal dimension (no check!) | 
|---|
|  | 75 | */ | 
|---|
|  | 76 | void LinearSystemOfEquations::Setb(double *x) | 
|---|
|  | 77 | { | 
|---|
| [bbf1bd] | 78 | b->setFromDoubleArray(x); | 
|---|
| [f60610] | 79 | }; | 
|---|
|  | 80 |  | 
|---|
|  | 81 | /** Initializes matrix a to the components of the given array. | 
|---|
|  | 82 | * note that sort order should be | 
|---|
|  | 83 | * \param *x array with equal dimension (no check!) | 
|---|
|  | 84 | */ | 
|---|
|  | 85 | void LinearSystemOfEquations::SetA(double *x) | 
|---|
|  | 86 | { | 
|---|
| [0d4424] | 87 | A->setFromDoubleArray(x); | 
|---|
| [f60610] | 88 | }; | 
|---|
|  | 89 |  | 
|---|
|  | 90 | /** Returns the solution vector x \f$A \cdot x = b\f$ as an array of doubles. | 
|---|
|  | 91 | * \param *array pointer allocated array for solution on return (no bounds check, dimension must match) | 
|---|
|  | 92 | * \return true - solving possible, false - some error occured. | 
|---|
|  | 93 | */ | 
|---|
|  | 94 | bool LinearSystemOfEquations::GetSolutionAsArray(double *&array) | 
|---|
|  | 95 | { | 
|---|
|  | 96 | bool status = Solve(); | 
|---|
|  | 97 |  | 
|---|
|  | 98 | // copy solution | 
|---|
|  | 99 | for (size_t i=0;i<x->dimension;i++) { | 
|---|
| [bbf1bd] | 100 | array[i] = x->at(i); | 
|---|
| [f60610] | 101 | } | 
|---|
|  | 102 | return status; | 
|---|
|  | 103 | }; | 
|---|
|  | 104 |  | 
|---|
|  | 105 | /** Returns the solution vector x \f$A \cdot x = b\f$ as an array of doubles. | 
|---|
|  | 106 | * \param &x solution vector on return (must be 3-dimensional) | 
|---|
|  | 107 | * \return true - solving possible, false - some error occured. | 
|---|
|  | 108 | */ | 
|---|
|  | 109 | bool LinearSystemOfEquations::GetSolutionAsVector(Vector &v) | 
|---|
|  | 110 | { | 
|---|
| [bbf1bd] | 111 | ASSERT (rows == NDIM, "Solution can only be returned as vector if number of columns is NDIM."); | 
|---|
| [f60610] | 112 | bool status = Solve(); | 
|---|
|  | 113 |  | 
|---|
|  | 114 | // copy solution | 
|---|
|  | 115 | for (size_t i=0;i<x->dimension;i++) | 
|---|
| [bbf1bd] | 116 | v[i] = x->at(i); | 
|---|
| [f60610] | 117 | return status; | 
|---|
|  | 118 | }; | 
|---|
|  | 119 |  | 
|---|
|  | 120 | /** Solves the given system of \f$A \cdot x = b\f$. | 
|---|
|  | 121 | * Use either LU or Householder decomposition. | 
|---|
|  | 122 | * Solution is stored in LinearSystemOfEquations::x | 
|---|
|  | 123 | * \return true - solving possible, false - some error occured. | 
|---|
|  | 124 | */ | 
|---|
|  | 125 | bool LinearSystemOfEquations::Solve() | 
|---|
|  | 126 | { | 
|---|
|  | 127 | // calculate solution | 
|---|
|  | 128 | int s; | 
|---|
|  | 129 | if (IsSymmetric) { // use LU | 
|---|
|  | 130 | gsl_permutation * p = gsl_permutation_alloc (x->dimension); | 
|---|
| [0d4424] | 131 | gsl_linalg_LU_decomp (A->content, p, &s); | 
|---|
| [bbf1bd] | 132 | gsl_linalg_LU_solve (A->content, p, b->content, x->content); | 
|---|
| [f60610] | 133 | gsl_permutation_free (p); | 
|---|
|  | 134 | } else {  // use Householder | 
|---|
| [0d4424] | 135 | //MatrixContent *backup = new MatrixContent(rows,columns); | 
|---|
| [f60610] | 136 | //*backup = *A; | 
|---|
| [bbf1bd] | 137 | gsl_linalg_HH_solve (A->content, b->content, x->content); | 
|---|
| [f60610] | 138 | //*A = *backup; | 
|---|
|  | 139 | //delete(backup); | 
|---|
|  | 140 | } | 
|---|
|  | 141 | return true; | 
|---|
|  | 142 | }; | 
|---|
|  | 143 |  | 
|---|