source: src/Box.cpp@ f1c838

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

FIX: periodicDistance could be off, if both vectors are more then one Box-lenght apart

  • Property mode set to 100644
File size: 4.7 KB
Line 
1/*
2 * Box.cpp
3 *
4 * Created on: Jun 30, 2010
5 * Author: crueger
6 */
7
8//#include "Helpers/MemDebug.hpp"
9
10#include "Box.hpp"
11
12#include <cmath>
13
14#include "Matrix.hpp"
15#include "vector.hpp"
16#include "Plane.hpp"
17
18#include "Helpers/Assert.hpp"
19
20Box::Box()
21{
22 M= new Matrix();
23 M->one();
24 Minv = new Matrix();
25 Minv->one();
26 conditions.resize(3);
27 conditions[0] = conditions[1] = conditions[2] = Wrap;
28}
29
30Box::Box(const Box& src){
31 M=new Matrix(*src.M);
32 Minv = new Matrix(*src.Minv);
33 conditions = src.conditions;
34}
35
36Box::~Box()
37{
38 delete M;
39 delete Minv;
40}
41
42const Matrix &Box::getM() const{
43 return *M;
44}
45const Matrix &Box::getMinv() const{
46 return *Minv;
47}
48
49void Box::setM(Matrix _M){
50 ASSERT(_M.determinant()!=0,"Matrix in Box construction was not invertible");
51 *M =_M;
52 *Minv = M->invert();
53}
54
55Vector Box::translateIn(const Vector &point) const{
56 return (*M) * point;
57}
58
59Vector Box::translateOut(const Vector &point) const{
60 return (*Minv) * point;
61}
62
63Vector Box::WrapPeriodically(const Vector &point) const{
64 Vector helper = translateOut(point);
65 for(int i=NDIM;i--;){
66
67 switch(conditions[i]){
68 case Wrap:
69 helper.at(i)=fmod(helper.at(i),1);
70 helper.at(i)+=(helper.at(i)>0)?0:1;
71 break;
72 case Bounce:
73 {
74 // there probably is a better way to handle this...
75 // all the fabs and fmod modf probably makes it very slow
76 double intpart,fracpart;
77 fracpart = modf(fabs(helper.at(i)),&intpart);
78 helper.at(i) = fabs(fracpart-fmod(intpart,2));
79 }
80 break;
81 case Ignore:
82 break;
83 default:
84 ASSERT(0,"No default case for this");
85 }
86
87 }
88 return translateIn(helper);
89}
90
91bool Box::isInside(const Vector &point) const
92{
93 bool result = true;
94 Vector tester = translateOut(point);
95
96 for(int i=0;i<NDIM;i++)
97 result = result && ((tester[i] >= -MYEPSILON) && ((tester[i] - 1.) < MYEPSILON));
98
99 return result;
100}
101
102
103VECTORSET(std::list) Box::explode(const Vector &point,int n) const{
104 VECTORSET(std::list) res;
105
106 // translate the Vector into each of the 27 neighbourhoods
107
108 // first create all translation Vectors
109 // there are (n*2+1)^3 such vectors
110 int max_dim = (n*2+1);
111 int max_dim2 = max_dim*max_dim;
112 int max = max_dim2*max_dim;
113 // only one loop to avoid unneccessary jumps
114 for(int i = 0;i<max;++i){
115 // get all coordinates for this iteration
116 int n1 = (i%max_dim)-n;
117 int n2 = ((i/max_dim)%max_dim)-n;
118 int n3 = ((i/max_dim2))-n;
119 Vector translation = translateIn(Vector(n1,n2,n3));
120 res.push_back(translation);
121 }
122 // translate all the translation vector by the offset defined by point
123 res.translate(point);
124 return res;
125}
126
127VECTORSET(std::list) Box::explode(const Vector &point) const{
128 VECTORSET(std::list) res;
129
130 // translate the Vector into each of the 27 neighbourhoods
131
132 // first create all 27 translation Vectors
133 // these loops depend on fixed parameters and can easily be expanded
134 // by the compiler to allow code without jumps
135 for(int n1 = -1;n1<=1;++n1){
136 for(int n2 = -1;n2<=1;++n2){
137 for(int n3 = -1;n3<=1;++n3){
138 // get all coordinates for this iteration
139 Vector translation = translateIn(Vector(n1,n2,n3));
140 res.push_back(translation);
141 }
142 }
143 }
144 // translate all the translation vector by the offset defined by point
145 res.translate(point);
146 return res;
147}
148
149double Box::periodicDistanceSquared(const Vector &point1,const Vector &point2) const{
150 Vector helper1 = WrapPeriodically(point1);
151 Vector helper2 = WrapPeriodically(point2);
152 VECTORSET(std::list) expansion = explode(helper1);
153 double res = expansion.minDistSquared(helper2);
154 return res;
155}
156
157double Box::periodicDistance(const Vector &point1,const Vector &point2) const{
158 double res;
159 res = sqrt(periodicDistanceSquared(point1,point2));
160 return res;
161}
162
163const Box::Conditions_t Box::getConditions(){
164 return conditions;
165}
166
167void Box::setCondition(int i,Box::BoundaryCondition_t condition){
168 conditions[i]=condition;
169}
170
171const vector<pair<Plane,Plane> > Box::getBoundingPlanes(){
172 vector<pair<Plane,Plane> > res;
173 for(int i=0;i<NDIM;++i){
174 Vector base1,base2,base3;
175 base2[(i+1)%NDIM] = 1.;
176 base3[(i+2)%NDIM] = 1.;
177 Plane p1(translateIn(base1),
178 translateIn(base2),
179 translateIn(base3));
180 Vector offset;
181 offset[i]=1;
182 Plane p2(translateIn(base1+offset),
183 translateIn(base2+offset),
184 translateIn(base3+offset));
185 res.push_back(make_pair(p1,p2));
186 }
187 return res;
188}
189
190Box &Box::operator=(const Box &src){
191 if(&src!=this){
192 delete M;
193 delete Minv;
194 M = new Matrix(*src.M);
195 Minv = new Matrix(*src.Minv);
196 conditions = src.conditions;
197 }
198 return *this;
199}
200
201Box &Box::operator=(const Matrix &mat){
202 setM(mat);
203 return *this;
204}
Note: See TracBrowser for help on using the repository browser.