source: src/LinearAlgebra/LinearSystemOfEquations.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 ad011c, checked in by Frederik Heber <heber@…>, 14 years ago

CodePatterns places all includes now in subfolder CodePatterns/.

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