source: src/LinearAlgebra/RealSpaceMatrix.cpp@ 56f73b

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 56f73b was ad011c, checked in by Frederik Heber <heber@…>, 14 years ago

CodePatterns places all includes now in subfolder CodePatterns/.

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