source: src/Box.cpp@ f3be87

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

Made the Box::isInside() method respect ignored boundaries

  • Property mode set to 100644
File size: 4.8 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 &&
98 ((conditions[i] == Ignore) ||
99 ((tester[i] >= -MYEPSILON) &&
100 ((tester[i] - 1.) < MYEPSILON)));
101
102 return result;
103}
104
105
106VECTORSET(std::list) Box::explode(const Vector &point,int n) const{
107 VECTORSET(std::list) res;
108
109 // translate the Vector into each of the 27 neighbourhoods
110
111 // first create all translation Vectors
112 // there are (n*2+1)^3 such vectors
113 int max_dim = (n*2+1);
114 int max_dim2 = max_dim*max_dim;
115 int max = max_dim2*max_dim;
116 // only one loop to avoid unneccessary jumps
117 for(int i = 0;i<max;++i){
118 // get all coordinates for this iteration
119 int n1 = (i%max_dim)-n;
120 int n2 = ((i/max_dim)%max_dim)-n;
121 int n3 = ((i/max_dim2))-n;
122 Vector translation = translateIn(Vector(n1,n2,n3));
123 res.push_back(translation);
124 }
125 // translate all the translation vector by the offset defined by point
126 res.translate(point);
127 return res;
128}
129
130VECTORSET(std::list) Box::explode(const Vector &point) const{
131 VECTORSET(std::list) res;
132
133 // translate the Vector into each of the 27 neighbourhoods
134
135 // first create all 27 translation Vectors
136 // these loops depend on fixed parameters and can easily be expanded
137 // by the compiler to allow code without jumps
138 for(int n1 = -1;n1<=1;++n1){
139 for(int n2 = -1;n2<=1;++n2){
140 for(int n3 = -1;n3<=1;++n3){
141 // get all coordinates for this iteration
142 Vector translation = translateIn(Vector(n1,n2,n3));
143 res.push_back(translation);
144 }
145 }
146 }
147 // translate all the translation vector by the offset defined by point
148 res.translate(point);
149 return res;
150}
151
152double Box::periodicDistanceSquared(const Vector &point1,const Vector &point2) const{
153 Vector helper1 = WrapPeriodically(point1);
154 Vector helper2 = WrapPeriodically(point2);
155 VECTORSET(std::list) expansion = explode(helper1);
156 double res = expansion.minDistSquared(helper2);
157 return res;
158}
159
160double Box::periodicDistance(const Vector &point1,const Vector &point2) const{
161 double res;
162 res = sqrt(periodicDistanceSquared(point1,point2));
163 return res;
164}
165
166const Box::Conditions_t Box::getConditions(){
167 return conditions;
168}
169
170void Box::setCondition(int i,Box::BoundaryCondition_t condition){
171 conditions[i]=condition;
172}
173
174const vector<pair<Plane,Plane> > Box::getBoundingPlanes(){
175 vector<pair<Plane,Plane> > res;
176 for(int i=0;i<NDIM;++i){
177 Vector base1,base2,base3;
178 base2[(i+1)%NDIM] = 1.;
179 base3[(i+2)%NDIM] = 1.;
180 Plane p1(translateIn(base1),
181 translateIn(base2),
182 translateIn(base3));
183 Vector offset;
184 offset[i]=1;
185 Plane p2(translateIn(base1+offset),
186 translateIn(base2+offset),
187 translateIn(base3+offset));
188 res.push_back(make_pair(p1,p2));
189 }
190 return res;
191}
192
193Box &Box::operator=(const Box &src){
194 if(&src!=this){
195 delete M;
196 delete Minv;
197 M = new Matrix(*src.M);
198 Minv = new Matrix(*src.Minv);
199 conditions = src.conditions;
200 }
201 return *this;
202}
203
204Box &Box::operator=(const Matrix &mat){
205 setM(mat);
206 return *this;
207}
Note: See TracBrowser for help on using the repository browser.