source: src/Box.cpp@ e670e4

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 e670e4 was 06aedc, checked in by Frederik Heber <heber@…>, 14 years ago

libMolecuilderLinearAlgebra is now a self-contained library fit for external use.

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