source: src/vector_ops.cpp@ c6efc1

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 c6efc1 was fa5a6a, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Added unittests for planes

  • Property mode set to 100644
File size: 6.9 KB
Line 
1/*
2 * vector_ops.cpp
3 *
4 * Created on: Apr 1, 2010
5 * Author: crueger
6 */
7
8#include "vector.hpp"
9#include "Plane.hpp"
10#include "log.hpp"
11#include "verbose.hpp"
12#include "gslmatrix.hpp"
13#include "leastsquaremin.hpp"
14#include "info.hpp"
15#include "Helpers/fast_functions.hpp"
16#include "Exceptions/LinearDependenceException.hpp"
17
18#include <gsl/gsl_linalg.h>
19#include <gsl/gsl_matrix.h>
20#include <gsl/gsl_permutation.h>
21#include <gsl/gsl_vector.h>
22
23/**
24 * !@file
25 * These files defines several common operation on vectors that should not
26 * become part of the main vector class, because they are either to complex
27 * or need methods from other subsystems that should not be moved to
28 * the LinAlg-Subsystem
29 */
30
31/** Creates a new vector as the one with least square distance to a given set of \a vectors.
32 * \param *vectors set of vectors
33 * \param num number of vectors
34 * \return true if success, false if failed due to linear dependency
35 */
36bool LSQdistance(Vector &res,const Vector **vectors, int num)
37{
38 int j;
39
40 for (j=0;j<num;j++) {
41 Log() << Verbose(1) << j << "th atom's vector: " << vectors[j] << endl;
42 }
43
44 int np = 3;
45 struct LSQ_params par;
46
47 const gsl_multimin_fminimizer_type *T =
48 gsl_multimin_fminimizer_nmsimplex;
49 gsl_multimin_fminimizer *s = NULL;
50 gsl_vector *ss, *y;
51 gsl_multimin_function minex_func;
52
53 size_t iter = 0, i;
54 int status;
55 double size;
56
57 /* Initial vertex size vector */
58 ss = gsl_vector_alloc (np);
59 y = gsl_vector_alloc (np);
60
61 /* Set all step sizes to 1 */
62 gsl_vector_set_all (ss, 1.0);
63
64 /* Starting point */
65 par.vectors = vectors;
66 par.num = num;
67
68 for (i=NDIM;i--;)
69 gsl_vector_set(y, i, (vectors[0]->at(i) - vectors[1]->at(i))/2.);
70
71 /* Initialize method and iterate */
72 minex_func.f = &LSQ;
73 minex_func.n = np;
74 minex_func.params = (void *)&par;
75
76 s = gsl_multimin_fminimizer_alloc (T, np);
77 gsl_multimin_fminimizer_set (s, &minex_func, y, ss);
78
79 do
80 {
81 iter++;
82 status = gsl_multimin_fminimizer_iterate(s);
83
84 if (status)
85 break;
86
87 size = gsl_multimin_fminimizer_size (s);
88 status = gsl_multimin_test_size (size, 1e-2);
89
90 if (status == GSL_SUCCESS)
91 {
92 printf ("converged to minimum at\n");
93 }
94
95 printf ("%5d ", (int)iter);
96 for (i = 0; i < (size_t)np; i++)
97 {
98 printf ("%10.3e ", gsl_vector_get (s->x, i));
99 }
100 printf ("f() = %7.3f size = %.3f\n", s->fval, size);
101 }
102 while (status == GSL_CONTINUE && iter < 100);
103
104 for (i=(size_t)np;i--;)
105 res[i] = gsl_vector_get(s->x, i);
106 gsl_vector_free(y);
107 gsl_vector_free(ss);
108 gsl_multimin_fminimizer_free (s);
109
110 return true;
111};
112
113/** Rotates the vector relative to the origin around the axis given by \a *axis by an angle of \a alpha.
114 * \param *axis rotation axis
115 * \param alpha rotation angle in radian
116 */
117Vector RotateVector(const Vector &vec,const Vector &axis, const double alpha)
118{
119 Vector a,y;
120 Vector res;
121 // normalise this vector with respect to axis
122 a = vec;
123 a.ProjectOntoPlane(axis);
124 // construct normal vector
125 try {
126 y = Plane(axis,a,0).getNormal();
127 }
128 catch (MathException &excp) {
129 // The normal vector cannot be created if there is linar dependency.
130 // Then the vector to rotate is on the axis and any rotation leads to the vector itself.
131 return vec;
132 }
133 y.Scale(vec.Norm());
134 // scale normal vector by sine and this vector by cosine
135 y.Scale(sin(alpha));
136 a.Scale(cos(alpha));
137 res = vec.Projection(axis);
138 // add scaled normal vector onto this vector
139 res += y;
140 // add part in axis direction
141 res += a;
142 return res;
143};
144
145/** Calculates the intersection of the two lines that are both on the same plane.
146 * This is taken from Weisstein, Eric W. "Line-Line Intersection." From MathWorld--A Wolfram Web Resource. http://mathworld.wolfram.com/Line-LineIntersection.html
147 * \param *out output stream for debugging
148 * \param *Line1a first vector of first line
149 * \param *Line1b second vector of first line
150 * \param *Line2a first vector of second line
151 * \param *Line2b second vector of second line
152 * \return true - \a this will contain the intersection on return, false - lines are parallel
153 */
154Vector GetIntersectionOfTwoLinesOnPlane(const Vector &Line1a, const Vector &Line1b, const Vector &Line2a, const Vector &Line2b)
155{
156 Info FunctionInfo(__func__);
157
158 Vector res;
159
160 auto_ptr<GSLMatrix> M = auto_ptr<GSLMatrix>(new GSLMatrix(4,4));
161
162 M->SetAll(1.);
163 for (int i=0;i<3;i++) {
164 M->Set(0, i, Line1a[i]);
165 M->Set(1, i, Line1b[i]);
166 M->Set(2, i, Line2a[i]);
167 M->Set(3, i, Line2b[i]);
168 }
169
170 //Log() << Verbose(1) << "Coefficent matrix is:" << endl;
171 //for (int i=0;i<4;i++) {
172 // for (int j=0;j<4;j++)
173 // cout << "\t" << M->Get(i,j);
174 // cout << endl;
175 //}
176 if (fabs(M->Determinant()) > MYEPSILON) {
177 Log() << Verbose(1) << "Determinant of coefficient matrix is NOT zero." << endl;
178 throw LinearDependenceException(__FILE__,__LINE__);
179 }
180
181 Log() << Verbose(1) << "INFO: Line1a = " << Line1a << ", Line1b = " << Line1b << ", Line2a = " << Line2a << ", Line2b = " << Line2b << "." << endl;
182
183
184 // constuct a,b,c
185 Vector a = Line1b - Line1a;
186 Vector b = Line2b - Line2a;
187 Vector c = Line2a - Line1a;
188 Vector d = Line2b - Line1b;
189 Log() << Verbose(1) << "INFO: a = " << a << ", b = " << b << ", c = " << c << "." << endl;
190 if ((a.NormSquared() < MYEPSILON) || (b.NormSquared() < MYEPSILON)) {
191 res.Zero();
192 Log() << Verbose(1) << "At least one of the lines is ill-defined, i.e. offset equals second vector." << endl;
193 throw LinearDependenceException(__FILE__,__LINE__);
194 }
195
196 // check for parallelity
197 Vector parallel;
198 double factor = 0.;
199 if (fabs(a.ScalarProduct(b)*a.ScalarProduct(b)/a.NormSquared()/b.NormSquared() - 1.) < MYEPSILON) {
200 parallel = Line1a - Line2a;
201 factor = parallel.ScalarProduct(a)/a.Norm();
202 if ((factor >= -MYEPSILON) && (factor - 1. < MYEPSILON)) {
203 res = Line2a;
204 Log() << Verbose(1) << "Lines conincide." << endl;
205 return res;
206 } else {
207 parallel = Line1a - Line2b;
208 factor = parallel.ScalarProduct(a)/a.Norm();
209 if ((factor >= -MYEPSILON) && (factor - 1. < MYEPSILON)) {
210 res = Line2b;
211 Log() << Verbose(1) << "Lines conincide." << endl;
212 return res;
213 }
214 }
215 Log() << Verbose(1) << "Lines are parallel." << endl;
216 res.Zero();
217 throw LinearDependenceException(__FILE__,__LINE__);
218 }
219
220 // obtain s
221 double s;
222 Vector temp1, temp2;
223 temp1 = c;
224 temp1.VectorProduct(b);
225 temp2 = a;
226 temp2.VectorProduct(b);
227 Log() << Verbose(1) << "INFO: temp1 = " << temp1 << ", temp2 = " << temp2 << "." << endl;
228 if (fabs(temp2.NormSquared()) > MYEPSILON)
229 s = temp1.ScalarProduct(temp2)/temp2.NormSquared();
230 else
231 s = 0.;
232 Log() << Verbose(1) << "Factor s is " << temp1.ScalarProduct(temp2) << "/" << temp2.NormSquared() << " = " << s << "." << endl;
233
234 // construct intersection
235 res = a;
236 res.Scale(s);
237 res += Line1a;
238 Log() << Verbose(1) << "Intersection is at " << res << "." << endl;
239
240 return res;
241};
Note: See TracBrowser for help on using the repository browser.