[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 |
|
---|
[325390] | 8 | /*
|
---|
[cca9ef] | 9 | * RealSpaceMatrix.cpp
|
---|
[325390] | 10 | *
|
---|
| 11 | * Created on: Jun 25, 2010
|
---|
| 12 | * Author: crueger
|
---|
| 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"
|
---|
[bbbad5] | 21 |
|
---|
[325390] | 22 | #include "Exceptions/NotInvertibleException.hpp"
|
---|
[ad011c] | 23 | #include "CodePatterns/Assert.hpp"
|
---|
[e4fe8d] | 24 | #include "Helpers/defs.hpp"
|
---|
[6d5a10] | 25 | #include "Helpers/fast_functions.hpp"
|
---|
[9b410d] | 26 | #include "LinearAlgebra/defs.hpp"
|
---|
[6d5a10] | 27 | #include "LinearAlgebra/MatrixContent.hpp"
|
---|
| 28 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
|
---|
[57f243] | 29 | #include "LinearAlgebra/Vector.hpp"
|
---|
[3bc926] | 30 | #include "LinearAlgebra/VectorContent.hpp"
|
---|
[a5028f] | 31 | #include "RandomNumbers/RandomNumberGeneratorFactory.hpp"
|
---|
| 32 | #include "RandomNumbers/RandomNumberGenerator.hpp"
|
---|
[325390] | 33 |
|
---|
| 34 | #include <gsl/gsl_blas.h>
|
---|
[a439e5] | 35 | #include <gsl/gsl_eigen.h>
|
---|
| 36 | #include <gsl/gsl_matrix.h>
|
---|
| 37 | #include <gsl/gsl_multimin.h>
|
---|
| 38 | #include <gsl/gsl_vector.h>
|
---|
[325390] | 39 | #include <cmath>
|
---|
[c49c96] | 40 | #include <iostream>
|
---|
[9b410d] | 41 | #include <limits>
|
---|
[c49c96] | 42 |
|
---|
| 43 | using namespace std;
|
---|
[325390] | 44 |
|
---|
[cca9ef] | 45 | RealSpaceMatrix::RealSpaceMatrix()
|
---|
[325390] | 46 | {
|
---|
[3bc926] | 47 | content = new MatrixContent(NDIM, NDIM);
|
---|
[3dbb9d] | 48 | createViews();
|
---|
[325390] | 49 | }
|
---|
| 50 |
|
---|
[cca9ef] | 51 | RealSpaceMatrix::RealSpaceMatrix(const double* src)
|
---|
[3bc926] | 52 | {
|
---|
| 53 | content = new MatrixContent(NDIM, NDIM, src);
|
---|
| 54 | createViews();
|
---|
| 55 | }
|
---|
[325390] | 56 |
|
---|
[cca9ef] | 57 | RealSpaceMatrix::RealSpaceMatrix(const RealSpaceMatrix &src)
|
---|
[3bc926] | 58 | {
|
---|
| 59 | content = new MatrixContent(src.content);
|
---|
[3dbb9d] | 60 | createViews();
|
---|
[325390] | 61 | }
|
---|
| 62 |
|
---|
[cca9ef] | 63 | RealSpaceMatrix::RealSpaceMatrix(const MatrixContent &src)
|
---|
[3bc926] | 64 | {
|
---|
| 65 | content = new MatrixContent(src);
|
---|
[3dbb9d] | 66 | createViews();
|
---|
[325390] | 67 | }
|
---|
| 68 |
|
---|
[cca9ef] | 69 | RealSpaceMatrix::RealSpaceMatrix(MatrixContent* _content)
|
---|
[3dbb9d] | 70 | {
|
---|
[3bc926] | 71 | content = new MatrixContent(_content);
|
---|
[3dbb9d] | 72 | createViews();
|
---|
| 73 | }
|
---|
[325390] | 74 |
|
---|
[cca9ef] | 75 | RealSpaceMatrix::~RealSpaceMatrix()
|
---|
[325390] | 76 | {
|
---|
[3dbb9d] | 77 | // delete all views
|
---|
| 78 | for(int i=NDIM;i--;){
|
---|
| 79 | delete rows_ptr[i];
|
---|
| 80 | }
|
---|
| 81 | for(int i=NDIM;i--;){
|
---|
| 82 | delete columns_ptr[i];
|
---|
| 83 | }
|
---|
| 84 | delete diagonal_ptr;
|
---|
[fee079] | 85 | delete content;
|
---|
[325390] | 86 | }
|
---|
| 87 |
|
---|
[cca9ef] | 88 | void RealSpaceMatrix::createViews(){
|
---|
[3dbb9d] | 89 | // create row views
|
---|
| 90 | for(int i=NDIM;i--;){
|
---|
[60dada] | 91 | VectorContent *rowContent = content->getRowVector(i);
|
---|
[3dbb9d] | 92 | rows_ptr[i] = new Vector(rowContent);
|
---|
[bbf1bd] | 93 | ASSERT(rowContent == NULL, "RealSpaceMatrix::createViews() - rowContent was not taken over as supposed to happen!");
|
---|
[3dbb9d] | 94 | }
|
---|
| 95 | // create column views
|
---|
| 96 | for(int i=NDIM;i--;){
|
---|
[60dada] | 97 | VectorContent *columnContent = content->getColumnVector(i);
|
---|
[3dbb9d] | 98 | columns_ptr[i] = new Vector(columnContent);
|
---|
[bbf1bd] | 99 | ASSERT(columnContent == NULL, "RealSpaceMatrix::createViews() - columnContent was not taken over as supposed to happen!");
|
---|
[3dbb9d] | 100 | }
|
---|
| 101 | // create diagonal view
|
---|
[60dada] | 102 | VectorContent *diagonalContent = content->getDiagonalVector();
|
---|
[3dbb9d] | 103 | diagonal_ptr = new Vector(diagonalContent);
|
---|
[bbf1bd] | 104 | ASSERT(diagonalContent == NULL, "RealSpaceMatrix::createViews() - diagonalContent was not taken over as supposed to happen!");
|
---|
[3dbb9d] | 105 | }
|
---|
| 106 |
|
---|
[cca9ef] | 107 | void RealSpaceMatrix::setIdentity(){
|
---|
[3bc926] | 108 | content->setIdentity();
|
---|
[1da5f5] | 109 | }
|
---|
| 110 |
|
---|
[cca9ef] | 111 | void RealSpaceMatrix::setZero(){
|
---|
[3bc926] | 112 | content->setZero();
|
---|
[a439e5] | 113 | }
|
---|
| 114 |
|
---|
[cca9ef] | 115 | void RealSpaceMatrix::setRotation(const double x, const double y, const double z)
|
---|
[31fb1d] | 116 | {
|
---|
| 117 | set(0,0, cos(y)*cos(z));
|
---|
| 118 | set(0,1, cos(z)*sin(x)*sin(y) - cos(x)*sin(z));
|
---|
| 119 | set(0,2, cos(x)*cos(z)*sin(y) + sin(x) * sin(z));
|
---|
| 120 | set(1,0, cos(y)*sin(z));
|
---|
| 121 | set(1,1, cos(x)*cos(z) + sin(x)*sin(y)*sin(z));
|
---|
| 122 | set(1,2, -cos(z)*sin(x) + cos(x)*sin(y)*sin(z));
|
---|
| 123 | set(2,0, -sin(y));
|
---|
| 124 | set(2,1, cos(y)*sin(x));
|
---|
| 125 | set(2,2, cos(x)*cos(y));
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[66fd49] | 128 | void RealSpaceMatrix::setRandomRotation()
|
---|
| 129 | {
|
---|
| 130 | double phi[NDIM];
|
---|
[a5028f] | 131 | RandomNumberGenerator &random = RandomNumberGeneratorFactory::getInstance().makeRandomNumberGenerator();
|
---|
| 132 | const double rng_min = random.min();
|
---|
| 133 | const double rng_max = random.max();
|
---|
| 134 |
|
---|
[66fd49] | 135 |
|
---|
| 136 | for (int i=0;i<NDIM;i++) {
|
---|
[a5028f] | 137 | phi[i] = (random()/(rng_max-rng_min))*(2.*M_PI);
|
---|
| 138 | std::cout << "Random angle is " << phi[i] << std::endl;
|
---|
[66fd49] | 139 | }
|
---|
| 140 |
|
---|
| 141 | set(0,0, cos(phi[0]) *cos(phi[2]) + (sin(phi[0])*sin(phi[1])*sin(phi[2])));
|
---|
| 142 | set(0,1, sin(phi[0]) *cos(phi[2]) - (cos(phi[0])*sin(phi[1])*sin(phi[2])));
|
---|
| 143 | set(0,2, cos(phi[1])*sin(phi[2]) );
|
---|
| 144 | set(1,0, -sin(phi[0])*cos(phi[1]) );
|
---|
| 145 | set(1,1, cos(phi[0])*cos(phi[1]) );
|
---|
| 146 | set(1,2, sin(phi[1]) );
|
---|
| 147 | set(2,0, -cos(phi[0]) *sin(phi[2]) + (sin(phi[0])*sin(phi[1])*cos(phi[2])));
|
---|
| 148 | set(2,1, -sin(phi[0]) *sin(phi[2]) - (cos(phi[0])*sin(phi[1])*cos(phi[2])));
|
---|
| 149 | set(2,2, cos(phi[1])*cos(phi[2]) );
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 |
|
---|
[cca9ef] | 153 | RealSpaceMatrix &RealSpaceMatrix::operator=(const RealSpaceMatrix &src)
|
---|
[3bc926] | 154 | {
|
---|
| 155 | // MatrixContent checks for self-assignment
|
---|
| 156 | *content = *(src.content);
|
---|
[325390] | 157 | return *this;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
[cca9ef] | 160 | const RealSpaceMatrix &RealSpaceMatrix::operator+=(const RealSpaceMatrix &rhs)
|
---|
[3bc926] | 161 | {
|
---|
| 162 | *content += *(rhs.content);
|
---|
[325390] | 163 | return *this;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
[cca9ef] | 166 | const RealSpaceMatrix &RealSpaceMatrix::operator-=(const RealSpaceMatrix &rhs)
|
---|
[3bc926] | 167 | {
|
---|
| 168 | *content -= *(rhs.content);
|
---|
[325390] | 169 | return *this;
|
---|
| 170 | }
|
---|
| 171 |
|
---|
[cca9ef] | 172 | const RealSpaceMatrix &RealSpaceMatrix::operator*=(const RealSpaceMatrix &rhs)
|
---|
[3bc926] | 173 | {
|
---|
| 174 | (*content) *= (*rhs.content);
|
---|
[325390] | 175 | return *this;
|
---|
| 176 | }
|
---|
| 177 |
|
---|
[cca9ef] | 178 | const RealSpaceMatrix RealSpaceMatrix::operator+(const RealSpaceMatrix &rhs) const
|
---|
[3bc926] | 179 | {
|
---|
[cca9ef] | 180 | RealSpaceMatrix tmp = *this;
|
---|
[325390] | 181 | tmp+=rhs;
|
---|
| 182 | return tmp;
|
---|
| 183 | }
|
---|
| 184 |
|
---|
[cca9ef] | 185 | const RealSpaceMatrix RealSpaceMatrix::operator-(const RealSpaceMatrix &rhs) const
|
---|
[3bc926] | 186 | {
|
---|
[cca9ef] | 187 | RealSpaceMatrix tmp = *this;
|
---|
[325390] | 188 | tmp-=rhs;
|
---|
| 189 | return tmp;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
[cca9ef] | 192 | const RealSpaceMatrix RealSpaceMatrix::operator*(const RealSpaceMatrix &rhs) const
|
---|
[3bc926] | 193 | {
|
---|
[cca9ef] | 194 | RealSpaceMatrix tmp(content);
|
---|
[3bc926] | 195 | tmp *= rhs;
|
---|
| 196 | return tmp;
|
---|
[325390] | 197 | }
|
---|
| 198 |
|
---|
[cca9ef] | 199 | double &RealSpaceMatrix::at(size_t i, size_t j)
|
---|
[3bc926] | 200 | {
|
---|
| 201 | return content->at(i,j);
|
---|
[325390] | 202 | }
|
---|
| 203 |
|
---|
[cca9ef] | 204 | const double RealSpaceMatrix::at(size_t i, size_t j) const
|
---|
[3bc926] | 205 | {
|
---|
| 206 | return content->at(i,j);
|
---|
[436f04] | 207 | }
|
---|
| 208 |
|
---|
[cca9ef] | 209 | Vector &RealSpaceMatrix::row(size_t i)
|
---|
[3bc926] | 210 | {
|
---|
[3dbb9d] | 211 | ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
|
---|
| 212 | return *rows_ptr[i];
|
---|
| 213 | }
|
---|
| 214 |
|
---|
[cca9ef] | 215 | const Vector &RealSpaceMatrix::row(size_t i) const
|
---|
[3bc926] | 216 | {
|
---|
[3dbb9d] | 217 | ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
|
---|
| 218 | return *rows_ptr[i];
|
---|
| 219 | }
|
---|
| 220 |
|
---|
[cca9ef] | 221 | Vector &RealSpaceMatrix::column(size_t i)
|
---|
[3bc926] | 222 | {
|
---|
[3dbb9d] | 223 | ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
|
---|
| 224 | return *columns_ptr[i];
|
---|
| 225 | }
|
---|
| 226 |
|
---|
[cca9ef] | 227 | const Vector &RealSpaceMatrix::column(size_t i) const
|
---|
[3bc926] | 228 | {
|
---|
[3dbb9d] | 229 | ASSERT(i>=0&&i<NDIM,"Index i for Matrix access out of range");
|
---|
| 230 | return *columns_ptr[i];
|
---|
| 231 | }
|
---|
| 232 |
|
---|
[cca9ef] | 233 | Vector &RealSpaceMatrix::diagonal()
|
---|
[3bc926] | 234 | {
|
---|
[3dbb9d] | 235 | return *diagonal_ptr;
|
---|
| 236 | }
|
---|
| 237 |
|
---|
[cca9ef] | 238 | const Vector &RealSpaceMatrix::diagonal() const
|
---|
[3bc926] | 239 | {
|
---|
[3dbb9d] | 240 | return *diagonal_ptr;
|
---|
| 241 | }
|
---|
| 242 |
|
---|
[cca9ef] | 243 | void RealSpaceMatrix::set(size_t i, size_t j, const double value)
|
---|
[3bc926] | 244 | {
|
---|
| 245 | content->set(i,j, value);
|
---|
[cadbc1] | 246 | }
|
---|
| 247 |
|
---|
[cca9ef] | 248 | double RealSpaceMatrix::determinant() const{
|
---|
[325390] | 249 | return at(0,0)*at(1,1)*at(2,2)
|
---|
| 250 | + at(0,1)*at(1,2)*at(2,0)
|
---|
| 251 | + at(0,2)*at(1,0)*at(2,1)
|
---|
| 252 | - at(2,0)*at(1,1)*at(0,2)
|
---|
| 253 | - at(2,1)*at(1,2)*at(0,0)
|
---|
| 254 | - at(2,2)*at(1,0)*at(0,1);
|
---|
| 255 | }
|
---|
| 256 |
|
---|
[a439e5] | 257 |
|
---|
[cca9ef] | 258 | RealSpaceMatrix RealSpaceMatrix::invert() const{
|
---|
[325390] | 259 | double det = determinant();
|
---|
[9b410d] | 260 | if(fabs(det) <= LINALG_MYEPSILON){
|
---|
[325390] | 261 | throw NotInvertibleException(__FILE__,__LINE__);
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | double detReci = 1./det;
|
---|
[cca9ef] | 265 | RealSpaceMatrix res;
|
---|
[436f04] | 266 | res.set(0,0, detReci*RDET2(at(1,1),at(2,1),at(1,2),at(2,2))); // A_11
|
---|
| 267 | res.set(1,0, -detReci*RDET2(at(1,0),at(2,0),at(1,2),at(2,2))); // A_21
|
---|
| 268 | res.set(2,0, detReci*RDET2(at(1,0),at(2,0),at(1,1),at(2,1))); // A_31
|
---|
| 269 | res.set(0,1, -detReci*RDET2(at(0,1),at(2,1),at(0,2),at(2,2))); // A_12
|
---|
| 270 | res.set(1,1, detReci*RDET2(at(0,0),at(2,0),at(0,2),at(2,2))); // A_22
|
---|
| 271 | res.set(2,1, -detReci*RDET2(at(0,0),at(2,0),at(0,1),at(2,1))); // A_32
|
---|
| 272 | res.set(0,2, detReci*RDET2(at(0,1),at(1,1),at(0,2),at(1,2))); // A_13
|
---|
| 273 | res.set(1,2, -detReci*RDET2(at(0,0),at(1,0),at(0,2),at(1,2))); // A_23
|
---|
| 274 | res.set(2,2, detReci*RDET2(at(0,0),at(1,0),at(0,1),at(1,1))); // A_33
|
---|
[325390] | 275 | return res;
|
---|
| 276 | }
|
---|
| 277 |
|
---|
[cca9ef] | 278 | RealSpaceMatrix RealSpaceMatrix::transpose() const
|
---|
[3bc926] | 279 | {
|
---|
[cca9ef] | 280 | RealSpaceMatrix res = RealSpaceMatrix(const_cast<const MatrixContent *>(content)->transpose());
|
---|
[41ea3c] | 281 | return res;
|
---|
| 282 | }
|
---|
| 283 |
|
---|
[cca9ef] | 284 | RealSpaceMatrix &RealSpaceMatrix::transpose()
|
---|
[6c438f] | 285 | {
|
---|
[3bc926] | 286 | content->transpose();
|
---|
[6c438f] | 287 | return *this;
|
---|
| 288 | }
|
---|
| 289 |
|
---|
[cca9ef] | 290 | Vector RealSpaceMatrix::transformToEigenbasis()
|
---|
[a439e5] | 291 | {
|
---|
[3bc926] | 292 | gsl_vector *eval = content->transformToEigenbasis();
|
---|
[a439e5] | 293 | Vector evalues(gsl_vector_get(eval,0), gsl_vector_get(eval,1), gsl_vector_get(eval,2));
|
---|
[80cecb5] | 294 | gsl_vector_free(eval);
|
---|
[a439e5] | 295 | return evalues;
|
---|
| 296 | }
|
---|
| 297 |
|
---|
[cca9ef] | 298 | const RealSpaceMatrix &RealSpaceMatrix::operator*=(const double factor)
|
---|
[3bc926] | 299 | {
|
---|
| 300 | *content *= factor;
|
---|
[325390] | 301 | return *this;
|
---|
| 302 | }
|
---|
| 303 |
|
---|
[cca9ef] | 304 | const RealSpaceMatrix operator*(const double factor,const RealSpaceMatrix& mat)
|
---|
[3bc926] | 305 | {
|
---|
[cca9ef] | 306 | RealSpaceMatrix tmp = mat;
|
---|
[3bc926] | 307 | return tmp *= factor;
|
---|
[325390] | 308 | }
|
---|
| 309 |
|
---|
[cca9ef] | 310 | const RealSpaceMatrix operator*(const RealSpaceMatrix &mat,const double factor)
|
---|
[3bc926] | 311 | {
|
---|
[325390] | 312 | return factor*mat;
|
---|
| 313 | }
|
---|
[d10eb6] | 314 |
|
---|
[cca9ef] | 315 | bool RealSpaceMatrix::operator==(const RealSpaceMatrix &rhs) const
|
---|
[3bc926] | 316 | {
|
---|
| 317 | return (*content == *(rhs.content));
|
---|
[0eb2dc] | 318 | }
|
---|
| 319 |
|
---|
[d10eb6] | 320 | /** Blows the 6-dimensional \a cell_size array up to a full NDIM by NDIM matrix.
|
---|
| 321 | * \param *symm 6-dim array of unique symmetric matrix components
|
---|
| 322 | * \return allocated NDIM*NDIM array with the symmetric matrix
|
---|
| 323 | */
|
---|
[cca9ef] | 324 | RealSpaceMatrix ReturnFullMatrixforSymmetric(const double * const symm)
|
---|
[d10eb6] | 325 | {
|
---|
[cca9ef] | 326 | RealSpaceMatrix matrix;
|
---|
[436f04] | 327 | matrix.set(0,0, symm[0]);
|
---|
| 328 | matrix.set(1,0, symm[1]);
|
---|
| 329 | matrix.set(2,0, symm[3]);
|
---|
| 330 | matrix.set(0,1, symm[1]);
|
---|
| 331 | matrix.set(1,1, symm[2]);
|
---|
| 332 | matrix.set(2,1, symm[4]);
|
---|
| 333 | matrix.set(0,2, symm[3]);
|
---|
| 334 | matrix.set(1,2, symm[4]);
|
---|
| 335 | matrix.set(2,2, symm[5]);
|
---|
[d10eb6] | 336 | return matrix;
|
---|
| 337 | };
|
---|
[c49c96] | 338 |
|
---|
[cca9ef] | 339 | ostream &operator<<(ostream &ost,const RealSpaceMatrix &mat)
|
---|
[3bc926] | 340 | {
|
---|
[c49c96] | 341 | for(int i = 0;i<NDIM;++i){
|
---|
| 342 | ost << "\n";
|
---|
| 343 | for(int j = 0; j<NDIM;++j){
|
---|
| 344 | ost << mat.at(i,j);
|
---|
| 345 | if(j!=NDIM-1)
|
---|
| 346 | ost << "; ";
|
---|
| 347 | }
|
---|
| 348 | }
|
---|
| 349 | return ost;
|
---|
| 350 | }
|
---|
[4b94bb] | 351 |
|
---|
[cca9ef] | 352 | Vector operator*(const RealSpaceMatrix &mat,const Vector &vec)
|
---|
[3bc926] | 353 | {
|
---|
| 354 | return (*mat.content) * vec;
|
---|
[4b94bb] | 355 | }
|
---|
| 356 |
|
---|
[cca9ef] | 357 | Vector &operator*=(Vector& lhs,const RealSpaceMatrix &mat)
|
---|
[3bc926] | 358 | {
|
---|
[4b94bb] | 359 | lhs = mat*lhs;
|
---|
| 360 | return lhs;
|
---|
| 361 | }
|
---|
| 362 |
|
---|