source: src/Box.cpp@ 5f8660a

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

Added copyright note to each .cpp file and an extensive one to builder.cpp.

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