source: src/linearsystemofequations.cpp@ d6c485

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

Wrapper class for gsl_linalg along with working Unit test.

Signed-off-by: Frederik Heber <heber@…>

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