source: src/Box.cpp@ 9cd807

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 9cd807 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
RevLine 
[83c09a]1/*
2 * Box.cpp
3 *
4 * Created on: Jun 30, 2010
5 * Author: crueger
6 */
7
[bf3817]8// include config.h
9#ifdef HAVE_CONFIG_H
10#include <config.h>
11#endif
12
[bbbad5]13#include "Helpers/MemDebug.hpp"
[6e00dd]14
[83c09a]15#include "Box.hpp"
16
[f429d7]17#include <cmath>
[16648f]18#include <iostream>
[d2938f]19#include <cstdlib>
[f429d7]20
[57f243]21#include "LinearAlgebra/Matrix.hpp"
22#include "LinearAlgebra/Vector.hpp"
23#include "LinearAlgebra/Plane.hpp"
[83c09a]24
[6e00dd]25#include "Helpers/Assert.hpp"
26
[16648f]27using namespace std;
28
[83c09a]29Box::Box()
30{
31 M= new Matrix();
32 M->one();
33 Minv = new Matrix();
34 Minv->one();
[77374e]35 conditions.resize(3);
36 conditions[0] = conditions[1] = conditions[2] = Wrap;
[83c09a]37}
38
[7579a4b]39Box::Box(const Box& src){
40 M=new Matrix(*src.M);
41 Minv = new Matrix(*src.Minv);
[77374e]42 conditions = src.conditions;
[7579a4b]43}
44
[83c09a]45Box::~Box()
46{
47 delete M;
48 delete Minv;
49}
50
[7579a4b]51const Matrix &Box::getM() const{
[83c09a]52 return *M;
53}
[7579a4b]54const Matrix &Box::getMinv() const{
[83c09a]55 return *Minv;
56}
57
58void Box::setM(Matrix _M){
[3bf9e2]59 ASSERT(_M.determinant()!=0,"Matrix in Box construction was not invertible");
[83c09a]60 *M =_M;
61 *Minv = M->invert();
62}
[7579a4b]63
[014475]64Vector Box::translateIn(const Vector &point) const{
[3dcb1f]65 return (*M) * point;
66}
67
[014475]68Vector Box::translateOut(const Vector &point) const{
[3dcb1f]69 return (*Minv) * point;
70}
71
[014475]72Vector Box::WrapPeriodically(const Vector &point) const{
[f429d7]73 Vector helper = translateOut(point);
74 for(int i=NDIM;i--;){
[c72562]75
76 switch(conditions[i]){
77 case Wrap:
78 helper.at(i)=fmod(helper.at(i),1);
[d2938f]79 helper.at(i)+=(helper.at(i)>=0)?0:1;
[c72562]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
[f429d7]96 }
97 return translateIn(helper);
98}
99
[0ff6b5]100bool Box::isInside(const Vector &point) const
101{
102 bool result = true;
[29ac78]103 Vector tester = translateOut(point);
[0ff6b5]104
105 for(int i=0;i<NDIM;i++)
[f3be87]106 result = result &&
107 ((conditions[i] == Ignore) ||
108 ((tester[i] >= -MYEPSILON) &&
109 ((tester[i] - 1.) < MYEPSILON)));
[0ff6b5]110
111 return result;
112}
113
114
[a630fd]115VECTORSET(std::list) Box::explode(const Vector &point,int n) const{
[16648f]116 ASSERT(isInside(point),"Exploded point not inside Box");
[12cf773]117 VECTORSET(std::list) res;
[89e820]118
[16648f]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;
[89e820]140 }
[7ac4af]141
[d2938f]142 bool done = false;
143 while(!done){
[16648f]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
[d2938f]162 int n = abs(index[i]) + ((index[i]<0)?-1:0);
[16648f]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");
[7ac4af]173 }
[16648f]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
[d2938f]180 int pos=0;
[16648f]181 ++index[pos];
[d2938f]182 while(index[pos]>n){
[16648f]183 index[pos++]=-n;
[d2938f]184 if(pos>=dims) { // it's trying to increase one beyond array... all vectors generated
185 done = true;
186 break;
187 }
[16648f]188 ++index[pos];
[7ac4af]189 }
190 }
191 return res;
192}
193
[16648f]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
[014475]199double Box::periodicDistanceSquared(const Vector &point1,const Vector &point2) const{
[f1c838]200 Vector helper1 = WrapPeriodically(point1);
201 Vector helper2 = WrapPeriodically(point2);
202 VECTORSET(std::list) expansion = explode(helper1);
203 double res = expansion.minDistSquared(helper2);
[014475]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;
[527de2]211}
212
[77374e]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
[29ac78]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
[e1ab97]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
[7579a4b]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);
[77374e]255 conditions = src.conditions;
[7579a4b]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.