source: src/Box.cpp@ 71d81a

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 71d81a was 71d81a, checked in by Frederik Heber <heber@…>, 13 years ago

FIX: Box::Box did not initialize Conditions_t with RealSpaceMatrix argument.

  • Property mode set to 100644
File size: 8.7 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010-2011 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 "CodePatterns/MemDebug.hpp"
21
22#include "Box.hpp"
23
24#include <cmath>
25#include <iostream>
26#include <cstdlib>
27
28#include "CodePatterns/Assert.hpp"
29#include "CodePatterns/Log.hpp"
30#include "CodePatterns/Verbose.hpp"
31#include "Helpers/defs.hpp"
32#include "LinearAlgebra/RealSpaceMatrix.hpp"
33#include "LinearAlgebra/Vector.hpp"
34#include "LinearAlgebra/Plane.hpp"
35#include "Shapes/BaseShapes.hpp"
36#include "Shapes/ShapeOps.hpp"
37
38
39Box::Box() :
40 M(new RealSpaceMatrix()),
41 Minv(new RealSpaceMatrix())
42{
43 internal_list.reserve(pow(3,3));
44 coords.reserve(NDIM);
45 index.reserve(NDIM);
46 M->setIdentity();
47 Minv->setIdentity();
48 conditions.resize(3);
49 conditions[0] = conditions[1] = conditions[2] = Wrap;
50}
51
52Box::Box(const Box& src) :
53 conditions(src.conditions),
54 M(new RealSpaceMatrix(*src.M)),
55 Minv(new RealSpaceMatrix(*src.Minv))
56{
57 internal_list.reserve(pow(3,3));
58 coords.reserve(NDIM);
59 index.reserve(NDIM);
60}
61
62Box::Box(RealSpaceMatrix _M) :
63 M(new RealSpaceMatrix(_M)),
64 Minv(new RealSpaceMatrix())
65{
66 internal_list.reserve(pow(3,3));
67 coords.reserve(NDIM);
68 index.reserve(NDIM);
69 ASSERT(M->determinant()!=0,"Matrix in Box construction was not invertible");
70 *Minv = M->invert();
71 conditions.resize(3);
72 conditions[0] = conditions[1] = conditions[2] = Wrap;
73}
74
75Box::~Box()
76{
77 delete M;
78 delete Minv;
79}
80
81const RealSpaceMatrix &Box::getM() const{
82 return *M;
83}
84const RealSpaceMatrix &Box::getMinv() const{
85 return *Minv;
86}
87
88void Box::setM(RealSpaceMatrix _M){
89 ASSERT(_M.determinant()!=0,"Matrix in Box construction was not invertible");
90 *M =_M;
91 *Minv = M->invert();
92}
93
94Vector Box::translateIn(const Vector &point) const{
95 return (*M) * point;
96}
97
98Vector Box::translateOut(const Vector &point) const{
99 return (*Minv) * point;
100}
101
102Vector Box::WrapPeriodically(const Vector &point) const{
103 Vector helper = translateOut(point);
104 for(int i=NDIM;i--;){
105
106 switch(conditions[i]){
107 case Wrap:
108 helper.at(i)=fmod(helper.at(i),1);
109 helper.at(i)+=(helper.at(i)>=0)?0:1;
110 break;
111 case Bounce:
112 {
113 // there probably is a better way to handle this...
114 // all the fabs and fmod modf probably makes it very slow
115 double intpart,fracpart;
116 fracpart = modf(fabs(helper.at(i)),&intpart);
117 helper.at(i) = fabs(fracpart-fmod(intpart,2));
118 }
119 break;
120 case Ignore:
121 break;
122 default:
123 ASSERT(0,"No default case for this");
124 break;
125 }
126
127 }
128 return translateIn(helper);
129}
130
131bool Box::isInside(const Vector &point) const
132{
133 bool result = true;
134 Vector tester = translateOut(point);
135
136 for(int i=0;i<NDIM;i++)
137 result = result &&
138 ((conditions[i] == Ignore) ||
139 ((tester[i] >= -MYEPSILON) &&
140 ((tester[i] - 1.) < MYEPSILON)));
141
142 return result;
143}
144
145
146VECTORSET(std::vector) Box::explode(const Vector &point,int n) const{
147 ASSERT(isInside(point),"Exploded point not inside Box");
148 internal_explode(point, n);
149 VECTORSET(std::vector) res(internal_list);
150 return res;
151}
152
153void Box::internal_explode(const Vector &point,int n) const{
154 internal_list.clear();
155 size_t list_index = 0;
156
157 Vector translater = translateOut(point);
158 Vector mask; // contains the ignored coordinates
159
160 // count the number of coordinates we need to do
161 int dims = 0; // number of dimensions that are not ignored
162 coords.clear();
163 index.clear();
164 for(int i=0;i<NDIM;++i){
165 if(conditions[i]==Ignore){
166 mask[i]=translater[i];
167 continue;
168 }
169 coords.push_back(i);
170 index.push_back(-n);
171 dims++;
172 } // there are max vectors in total we need to create
173 internal_list.resize(pow(2*n+1, dims));
174
175 if(!dims){
176 // all boundaries are ignored
177 internal_list[list_index++] = point;
178 return;
179 }
180
181 bool done = false;
182 while(!done){
183 // create this vector
184 Vector helper;
185 for(int i=0;i<dims;++i){
186 switch(conditions[coords[i]]){
187 case Wrap:
188 helper[coords[i]] = index[i]+translater[coords[i]];
189 break;
190 case Bounce:
191 {
192 // Bouncing the coordinate x produces the series:
193 // 0 -> x
194 // 1 -> 2-x
195 // 2 -> 2+x
196 // 3 -> 4-x
197 // 4 -> 4+x
198 // the first number is the next bigger even number (n+n%2)
199 // the next number is the value with alternating sign (x-2*(n%2)*x)
200 // the negative numbers produce the same sequence reversed and shifted
201 int n = abs(index[i]) + ((index[i]<0)?-1:0);
202 int sign = (index[i]<0)?-1:+1;
203 int even = n%2;
204 helper[coords[i]]=n+even+translater[coords[i]]-2*even*translater[coords[i]];
205 helper[coords[i]]*=sign;
206 }
207 break;
208 case Ignore:
209 ASSERT(0,"Ignored coordinate handled in generation loop");
210 break;
211 default:
212 ASSERT(0,"No default case for this switch-case");
213 break;
214 }
215
216 }
217 // add back all ignored coordinates (not handled in above loop)
218 helper+=mask;
219 ASSERT(list_index < internal_list.size(),
220 "Box::internal_explode() - we have estimated the number of vectors wrong: "
221 +toString(list_index) +" >= "+toString(internal_list.size())+".");
222 internal_list[list_index++] = translateIn(helper);
223 // set the new indexes
224 int pos=0;
225 ++index[pos];
226 while(index[pos]>n){
227 index[pos++]=-n;
228 if(pos>=dims) { // it's trying to increase one beyond array... all vectors generated
229 done = true;
230 break;
231 }
232 ++index[pos];
233 }
234 }
235}
236
237VECTORSET(std::vector) Box::explode(const Vector &point) const{
238 ASSERT(isInside(point),"Exploded point not inside Box");
239 return explode(point,1);
240}
241
242double Box::periodicDistanceSquared(const Vector &point1,const Vector &point2) const{
243 Vector helper1(!isInside(point1) ? WrapPeriodically(point1) : point1);
244 Vector helper2(!isInside(point2) ? WrapPeriodically(point2) : point2);
245 internal_explode(helper1,1);
246 double res = internal_list.minDistSquared(helper2);
247 return res;
248}
249
250double Box::periodicDistance(const Vector &point1,const Vector &point2) const{
251 double res;
252 res = sqrt(periodicDistanceSquared(point1,point2));
253 return res;
254}
255
256double Box::DistanceToBoundary(const Vector &point) const
257{
258 std::map<double, Plane> DistanceSet;
259 std::vector<std::pair<Plane,Plane> > Boundaries = getBoundingPlanes();
260 for (int i=0;i<NDIM;++i) {
261 const double tempres1 = Boundaries[i].first.distance(point);
262 const double tempres2 = Boundaries[i].second.distance(point);
263 DistanceSet.insert( make_pair(tempres1, Boundaries[i].first) );
264 LOG(1, "Inserting distance " << tempres1 << " and " << tempres2 << ".");
265 DistanceSet.insert( make_pair(tempres2, Boundaries[i].second) );
266 }
267 ASSERT(!DistanceSet.empty(), "Box::DistanceToBoundary() - no distances in map!");
268 return (DistanceSet.begin())->first;
269}
270
271Shape Box::getShape() const{
272 return transform(Cuboid(Vector(0,0,0),Vector(1,1,1)),(*M));
273}
274
275const Box::Conditions_t Box::getConditions() const
276{
277 return conditions;
278}
279
280void Box::setCondition(int i,Box::BoundaryCondition_t condition){
281 conditions[i]=condition;
282}
283
284const std::vector<std::pair<Plane,Plane> > Box::getBoundingPlanes() const
285{
286 std::vector<std::pair<Plane,Plane> > res;
287 for(int i=0;i<NDIM;++i){
288 Vector base1,base2,base3;
289 base2[(i+1)%NDIM] = 1.;
290 base3[(i+2)%NDIM] = 1.;
291 Plane p1(translateIn(base1),
292 translateIn(base2),
293 translateIn(base3));
294 Vector offset;
295 offset[i]=1;
296 Plane p2(translateIn(base1+offset),
297 translateIn(base2+offset),
298 translateIn(base3+offset));
299 res.push_back(make_pair(p1,p2));
300 }
301 ASSERT(res.size() == 3, "Box::getBoundingPlanes() - does not have three plane pairs!");
302 return res;
303}
304
305void Box::setCuboid(const Vector &endpoint){
306 ASSERT(endpoint[0]>0 && endpoint[1]>0 && endpoint[2]>0,"Vector does not define a full cuboid");
307 M->setIdentity();
308 M->diagonal()=endpoint;
309 Vector &dinv = Minv->diagonal();
310 for(int i=NDIM;i--;)
311 dinv[i]=1/endpoint[i];
312}
313
314Box &Box::operator=(const Box &src){
315 if(&src!=this){
316 delete M;
317 delete Minv;
318 M = new RealSpaceMatrix(*src.M);
319 Minv = new RealSpaceMatrix(*src.Minv);
320 conditions = src.conditions;
321 }
322 return *this;
323}
324
325Box &Box::operator=(const RealSpaceMatrix &mat){
326 setM(mat);
327 return *this;
328}
329
330std::ostream & operator << (std::ostream& ost, const Box &m)
331{
332 ost << m.getM();
333 return ost;
334}
Note: See TracBrowser for help on using the repository browser.