source: src/Box.cpp@ ff58f1

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 ff58f1 was bf3817, checked in by Frederik Heber <heber@…>, 14 years ago

Added ifdef HAVE_CONFIG and config.h include to each and every cpp file.

  • is now topmost in front of MemDebug.hpp (and any other).
  • Property mode set to 100644
File size: 6.2 KB
Line 
1/*
2 * Box.cpp
3 *
4 * Created on: Jun 30, 2010
5 * Author: crueger
6 */
7
8// include config.h
9#ifdef HAVE_CONFIG_H
10#include <config.h>
11#endif
12
13#include "Helpers/MemDebug.hpp"
14
15#include "Box.hpp"
16
17#include <cmath>
18#include <iostream>
19#include <cstdlib>
20
21#include "LinearAlgebra/Matrix.hpp"
22#include "LinearAlgebra/Vector.hpp"
23#include "LinearAlgebra/Plane.hpp"
24
25#include "Helpers/Assert.hpp"
26
27using namespace std;
28
29Box::Box()
30{
31 M= new Matrix();
32 M->one();
33 Minv = new Matrix();
34 Minv->one();
35 conditions.resize(3);
36 conditions[0] = conditions[1] = conditions[2] = Wrap;
37}
38
39Box::Box(const Box& src){
40 M=new Matrix(*src.M);
41 Minv = new Matrix(*src.Minv);
42 conditions = src.conditions;
43}
44
45Box::~Box()
46{
47 delete M;
48 delete Minv;
49}
50
51const Matrix &Box::getM() const{
52 return *M;
53}
54const Matrix &Box::getMinv() const{
55 return *Minv;
56}
57
58void Box::setM(Matrix _M){
59 ASSERT(_M.determinant()!=0,"Matrix in Box construction was not invertible");
60 *M =_M;
61 *Minv = M->invert();
62}
63
64Vector Box::translateIn(const Vector &point) const{
65 return (*M) * point;
66}
67
68Vector Box::translateOut(const Vector &point) const{
69 return (*Minv) * point;
70}
71
72Vector Box::WrapPeriodically(const Vector &point) const{
73 Vector helper = translateOut(point);
74 for(int i=NDIM;i--;){
75
76 switch(conditions[i]){
77 case Wrap:
78 helper.at(i)=fmod(helper.at(i),1);
79 helper.at(i)+=(helper.at(i)>=0)?0:1;
80 break;
81 case Bounce:
82 {
83 // there probably is a better way to handle this...
84 // all the fabs and fmod modf probably makes it very slow
85 double intpart,fracpart;
86 fracpart = modf(fabs(helper.at(i)),&intpart);
87 helper.at(i) = fabs(fracpart-fmod(intpart,2));
88 }
89 break;
90 case Ignore:
91 break;
92 default:
93 ASSERT(0,"No default case for this");
94 }
95
96 }
97 return translateIn(helper);
98}
99
100bool Box::isInside(const Vector &point) const
101{
102 bool result = true;
103 Vector tester = translateOut(point);
104
105 for(int i=0;i<NDIM;i++)
106 result = result &&
107 ((conditions[i] == Ignore) ||
108 ((tester[i] >= -MYEPSILON) &&
109 ((tester[i] - 1.) < MYEPSILON)));
110
111 return result;
112}
113
114
115VECTORSET(std::list) Box::explode(const Vector &point,int n) const{
116 ASSERT(isInside(point),"Exploded point not inside Box");
117 VECTORSET(std::list) res;
118
119 Vector translater = translateOut(point);
120 Vector mask; // contains the ignored coordinates
121
122 // count the number of coordinates we need to do
123 int dims = 0; // number of dimensions that are not ignored
124 vector<int> coords;
125 vector<int> index;
126 for(int i=0;i<NDIM;++i){
127 if(conditions[i]==Ignore){
128 mask[i]=translater[i];
129 continue;
130 }
131 coords.push_back(i);
132 index.push_back(-n);
133 dims++;
134 } // there are max vectors in total we need to create
135
136 if(!dims){
137 // all boundaries are ignored
138 res.push_back(point);
139 return res;
140 }
141
142 bool done = false;
143 while(!done){
144 // create this vector
145 Vector helper;
146 for(int i=0;i<dims;++i){
147 switch(conditions[coords[i]]){
148 case Wrap:
149 helper[coords[i]] = index[i]+translater[coords[i]];
150 break;
151 case Bounce:
152 {
153 // Bouncing the coordinate x produces the series:
154 // 0 -> x
155 // 1 -> 2-x
156 // 2 -> 2+x
157 // 3 -> 4-x
158 // 4 -> 4+x
159 // the first number is the next bigger even number (n+n%2)
160 // the next number is the value with alternating sign (x-2*(n%2)*x)
161 // the negative numbers produce the same sequence reversed and shifted
162 int n = abs(index[i]) + ((index[i]<0)?-1:0);
163 int sign = (index[i]<0)?-1:+1;
164 int even = n%2;
165 helper[coords[i]]=n+even+translater[coords[i]]-2*even*translater[coords[i]];
166 helper[coords[i]]*=sign;
167 }
168 break;
169 case Ignore:
170 ASSERT(0,"Ignored coordinate handled in generation loop");
171 default:
172 ASSERT(0,"No default case for this switch-case");
173 }
174
175 }
176 // add back all ignored coordinates (not handled in above loop)
177 helper+=mask;
178 res.push_back(translateIn(helper));
179 // set the new indexes
180 int pos=0;
181 ++index[pos];
182 while(index[pos]>n){
183 index[pos++]=-n;
184 if(pos>=dims) { // it's trying to increase one beyond array... all vectors generated
185 done = true;
186 break;
187 }
188 ++index[pos];
189 }
190 }
191 return res;
192}
193
194VECTORSET(std::list) Box::explode(const Vector &point) const{
195 ASSERT(isInside(point),"Exploded point not inside Box");
196 return explode(point,1);
197}
198
199double Box::periodicDistanceSquared(const Vector &point1,const Vector &point2) const{
200 Vector helper1 = WrapPeriodically(point1);
201 Vector helper2 = WrapPeriodically(point2);
202 VECTORSET(std::list) expansion = explode(helper1);
203 double res = expansion.minDistSquared(helper2);
204 return res;
205}
206
207double Box::periodicDistance(const Vector &point1,const Vector &point2) const{
208 double res;
209 res = sqrt(periodicDistanceSquared(point1,point2));
210 return res;
211}
212
213const Box::Conditions_t Box::getConditions(){
214 return conditions;
215}
216
217void Box::setCondition(int i,Box::BoundaryCondition_t condition){
218 conditions[i]=condition;
219}
220
221const vector<pair<Plane,Plane> > Box::getBoundingPlanes(){
222 vector<pair<Plane,Plane> > res;
223 for(int i=0;i<NDIM;++i){
224 Vector base1,base2,base3;
225 base2[(i+1)%NDIM] = 1.;
226 base3[(i+2)%NDIM] = 1.;
227 Plane p1(translateIn(base1),
228 translateIn(base2),
229 translateIn(base3));
230 Vector offset;
231 offset[i]=1;
232 Plane p2(translateIn(base1+offset),
233 translateIn(base2+offset),
234 translateIn(base3+offset));
235 res.push_back(make_pair(p1,p2));
236 }
237 return res;
238}
239
240void Box::setCuboid(const Vector &endpoint){
241 ASSERT(endpoint[0]>0 && endpoint[1]>0 && endpoint[2]>0,"Vector does not define a full cuboid");
242 M->one();
243 M->diagonal()=endpoint;
244 Vector &dinv = Minv->diagonal();
245 for(int i=NDIM;i--;)
246 dinv[i]=1/endpoint[i];
247}
248
249Box &Box::operator=(const Box &src){
250 if(&src!=this){
251 delete M;
252 delete Minv;
253 M = new Matrix(*src.M);
254 Minv = new Matrix(*src.Minv);
255 conditions = src.conditions;
256 }
257 return *this;
258}
259
260Box &Box::operator=(const Matrix &mat){
261 setM(mat);
262 return *this;
263}
Note: See TracBrowser for help on using the repository browser.