source: src/LinearAlgebra/RealSpaceMatrix.cpp@ 111387

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

RandomNumberGeneratorFactory is now used instead of rand() throughout the code.

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