source: src/Box.cpp@ abae35

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

Lots of small compiler warning fixes.

Issues:

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