source: src/Box.cpp@ 796aa6

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 796aa6 was e1ab97, checked in by Tillmann Crueger <crueger@…>, 14 years ago

Added a function that allows fast creation of a cuboid box by giving the second endpoint

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