source: src/LinearAlgebra/RealSpaceMatrix.cpp@ 9b410d

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 9b410d was 9b410d, checked in by Frederik Heber <heber@…>, 14 years ago

Replace MYEPSILON in LinearAlgebra/ by LINALG_MYEPSILON.

  • this is preparatory for external use lib libmolecuilderLinearAlgebra.
  • new file LinearAlgebra/defs.hpp.
  • Property mode set to 100644
File size: 9.4 KB
RevLine 
[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
43using namespace std;
[325390]44
[cca9ef]45RealSpaceMatrix::RealSpaceMatrix()
[325390]46{
[3bc926]47 content = new MatrixContent(NDIM, NDIM);
[3dbb9d]48 createViews();
[325390]49}
50
[cca9ef]51RealSpaceMatrix::RealSpaceMatrix(const double* src)
[3bc926]52{
53 content = new MatrixContent(NDIM, NDIM, src);
54 createViews();
55}
[325390]56
[cca9ef]57RealSpaceMatrix::RealSpaceMatrix(const RealSpaceMatrix &src)
[3bc926]58{
59 content = new MatrixContent(src.content);
[3dbb9d]60 createViews();
[325390]61}
62
[cca9ef]63RealSpaceMatrix::RealSpaceMatrix(const MatrixContent &src)
[3bc926]64{
65 content = new MatrixContent(src);
[3dbb9d]66 createViews();
[325390]67}
68
[cca9ef]69RealSpaceMatrix::RealSpaceMatrix(MatrixContent* _content)
[3dbb9d]70{
[3bc926]71 content = new MatrixContent(_content);
[3dbb9d]72 createViews();
73}
[325390]74
[cca9ef]75RealSpaceMatrix::~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]88void 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]107void RealSpaceMatrix::setIdentity(){
[3bc926]108 content->setIdentity();
[1da5f5]109}
110
[cca9ef]111void RealSpaceMatrix::setZero(){
[3bc926]112 content->setZero();
[a439e5]113}
114
[cca9ef]115void 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]128void 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]153RealSpaceMatrix &RealSpaceMatrix::operator=(const RealSpaceMatrix &src)
[3bc926]154{
155 // MatrixContent checks for self-assignment
156 *content = *(src.content);
[325390]157 return *this;
158}
159
[cca9ef]160const RealSpaceMatrix &RealSpaceMatrix::operator+=(const RealSpaceMatrix &rhs)
[3bc926]161{
162 *content += *(rhs.content);
[325390]163 return *this;
164}
165
[cca9ef]166const RealSpaceMatrix &RealSpaceMatrix::operator-=(const RealSpaceMatrix &rhs)
[3bc926]167{
168 *content -= *(rhs.content);
[325390]169 return *this;
170}
171
[cca9ef]172const RealSpaceMatrix &RealSpaceMatrix::operator*=(const RealSpaceMatrix &rhs)
[3bc926]173{
174 (*content) *= (*rhs.content);
[325390]175 return *this;
176}
177
[cca9ef]178const RealSpaceMatrix RealSpaceMatrix::operator+(const RealSpaceMatrix &rhs) const
[3bc926]179{
[cca9ef]180 RealSpaceMatrix tmp = *this;
[325390]181 tmp+=rhs;
182 return tmp;
183}
184
[cca9ef]185const RealSpaceMatrix RealSpaceMatrix::operator-(const RealSpaceMatrix &rhs) const
[3bc926]186{
[cca9ef]187 RealSpaceMatrix tmp = *this;
[325390]188 tmp-=rhs;
189 return tmp;
190}
191
[cca9ef]192const 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]199double &RealSpaceMatrix::at(size_t i, size_t j)
[3bc926]200{
201 return content->at(i,j);
[325390]202}
203
[cca9ef]204const double RealSpaceMatrix::at(size_t i, size_t j) const
[3bc926]205{
206 return content->at(i,j);
[436f04]207}
208
[cca9ef]209Vector &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]215const 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]221Vector &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]227const 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]233Vector &RealSpaceMatrix::diagonal()
[3bc926]234{
[3dbb9d]235 return *diagonal_ptr;
236}
237
[cca9ef]238const Vector &RealSpaceMatrix::diagonal() const
[3bc926]239{
[3dbb9d]240 return *diagonal_ptr;
241}
242
[cca9ef]243void RealSpaceMatrix::set(size_t i, size_t j, const double value)
[3bc926]244{
245 content->set(i,j, value);
[cadbc1]246}
247
[cca9ef]248double 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]258RealSpaceMatrix 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]278RealSpaceMatrix RealSpaceMatrix::transpose() const
[3bc926]279{
[cca9ef]280 RealSpaceMatrix res = RealSpaceMatrix(const_cast<const MatrixContent *>(content)->transpose());
[41ea3c]281 return res;
282}
283
[cca9ef]284RealSpaceMatrix &RealSpaceMatrix::transpose()
[6c438f]285{
[3bc926]286 content->transpose();
[6c438f]287 return *this;
288}
289
[cca9ef]290Vector 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]298const RealSpaceMatrix &RealSpaceMatrix::operator*=(const double factor)
[3bc926]299 {
300 *content *= factor;
[325390]301 return *this;
302}
303
[cca9ef]304const RealSpaceMatrix operator*(const double factor,const RealSpaceMatrix& mat)
[3bc926]305{
[cca9ef]306 RealSpaceMatrix tmp = mat;
[3bc926]307 return tmp *= factor;
[325390]308}
309
[cca9ef]310const RealSpaceMatrix operator*(const RealSpaceMatrix &mat,const double factor)
[3bc926]311{
[325390]312 return factor*mat;
313}
[d10eb6]314
[cca9ef]315bool 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]324RealSpaceMatrix 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]339ostream &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]352Vector operator*(const RealSpaceMatrix &mat,const Vector &vec)
[3bc926]353{
354 return (*mat.content) * vec;
[4b94bb]355}
356
[cca9ef]357Vector &operator*=(Vector& lhs,const RealSpaceMatrix &mat)
[3bc926]358{
[4b94bb]359 lhs = mat*lhs;
360 return lhs;
361}
362
Note: See TracBrowser for help on using the repository browser.