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