source: src/LinearAlgebra/linearsystemofequations.cpp@ bf3817

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

Added ifdef HAVE_CONFIG and config.h include to each and every cpp file.

  • is now topmost in front of MemDebug.hpp (and any other).
  • Property mode set to 100644
File size: 3.7 KB
Line 
1/*
2 * linearsystemofequations.cpp
3 *
4 * Created on: Jan 8, 2010
5 * Author: heber
6 */
7
8// include config.h
9#ifdef HAVE_CONFIG_H
10#include <config.h>
11#endif
12
13#include "Helpers/MemDebug.hpp"
14
15#include "defs.hpp"
16#include "LinearAlgebra/gslmatrix.hpp"
17#include "LinearAlgebra/gslvector.hpp"
18#include "LinearAlgebra/linearsystemofequations.hpp"
19#include "Helpers/logger.hpp"
20#include "LinearAlgebra/Vector.hpp"
21
22#include <cassert>
23#include <gsl/gsl_permutation.h>
24
25/** Constructor for class LinearSystemOfEquations.
26 * Allocates Vector and Matrix classes.
27 * \param m column dimension
28 * \param n row dimension
29 */
30LinearSystemOfEquations::LinearSystemOfEquations(int m, int n) : rows(m), columns(n), IsSymmetric(false)
31{
32 A = new GSLMatrix(m, n);
33 b = new GSLVector(m);
34 x = new GSLVector(n);
35};
36
37/** Destructor for class LinearSystemOfEquations.
38 * Destructs Vector and Matrix classes.
39 */
40LinearSystemOfEquations::~LinearSystemOfEquations()
41{
42 delete(A);
43 delete(b);
44 delete(x);
45};
46
47/** Sets whether matrix is to be regarded as symmetric.
48 * Note that we do not check whether it really is, just take upper diagonal.
49 * \param symmetric true or false
50 */
51bool LinearSystemOfEquations::SetSymmetric(bool symmetric)
52{
53 assert (rows == columns && "Rows and columns don't have equal size! Setting symmetric not possible.");
54 return (IsSymmetric = symmetric);
55};
56
57/** Initializes vector b to the components of the given vector.
58 * \param *x Vector with equal dimension (no check!)
59 */
60void LinearSystemOfEquations::Setb(Vector *x)
61{
62 assert ( columns == NDIM && "Vector class is always three-dimensional, unlike this LEqS!");
63 b->SetFromVector(*x);
64};
65
66/** Initializes vector b to the components of the given vector.
67 * \param *x array with equal dimension (no check!)
68 */
69void LinearSystemOfEquations::Setb(double *x)
70{
71 b->SetFromDoubleArray(x);
72};
73
74/** Initializes matrix a to the components of the given array.
75 * note that sort order should be
76 * \param *x array with equal dimension (no check!)
77 */
78void LinearSystemOfEquations::SetA(double *x)
79{
80 A->SetFromDoubleArray(x);
81};
82
83/** Returns the solution vector x \f$A \cdot x = b\f$ as an array of doubles.
84 * \param *array pointer allocated array for solution on return (no bounds check, dimension must match)
85 * \return true - solving possible, false - some error occured.
86 */
87bool LinearSystemOfEquations::GetSolutionAsArray(double *&array)
88{
89 bool status = Solve();
90
91 // copy solution
92 for (size_t i=0;i<x->dimension;i++) {
93 array[i] = x->Get(i);
94 }
95 return status;
96};
97
98/** Returns the solution vector x \f$A \cdot x = b\f$ as an array of doubles.
99 * \param &x solution vector on return (must be 3-dimensional)
100 * \return true - solving possible, false - some error occured.
101 */
102bool LinearSystemOfEquations::GetSolutionAsVector(Vector &v)
103{
104 assert(rows == NDIM && "Solution can only be returned as vector if number of columns is NDIM.");
105 bool status = Solve();
106
107 // copy solution
108 for (size_t i=0;i<x->dimension;i++)
109 v[i] = x->Get(i);
110 return status;
111};
112
113/** Solves the given system of \f$A \cdot x = b\f$.
114 * Use either LU or Householder decomposition.
115 * Solution is stored in LinearSystemOfEquations::x
116 * \return true - solving possible, false - some error occured.
117 */
118bool LinearSystemOfEquations::Solve()
119{
120 // calculate solution
121 int s;
122 if (IsSymmetric) { // use LU
123 gsl_permutation * p = gsl_permutation_alloc (x->dimension);
124 gsl_linalg_LU_decomp (A->matrix, p, &s);
125 gsl_linalg_LU_solve (A->matrix, p, b->vector, x->vector);
126 gsl_permutation_free (p);
127 } else { // use Householder
128 //GSLMatrix *backup = new GSLMatrix(rows,columns);
129 //*backup = *A;
130 gsl_linalg_HH_solve (A->matrix, b->vector, x->vector);
131 //*A = *backup;
132 //delete(backup);
133 }
134 return true;
135};
136
Note: See TracBrowser for help on using the repository browser.