source: src/LinearAlgebra/RealSpaceMatrix.cpp@ e4fe8d

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

Moved defs.?pp to subdir (and library) Helpers.

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