source: src/linearsystemofequations.cpp@ 3839e5

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 3839e5 was 1dc9ec, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Merge branch 'VectorRefactoring' into stable

Conflicts:

molecuilder/src/vector.cpp

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