source: src/Box.cpp@ 5344e4

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 5344e4 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
RevLine 
[bcf653]1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
[d103d3]4 * Copyright (C) 2010-2011 University of Bonn. All rights reserved.
[bcf653]5 * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
6 */
7
[83c09a]8/*
9 * Box.cpp
10 *
11 * Created on: Jun 30, 2010
12 * Author: crueger
13 */
14
[bf3817]15// include config.h
16#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
19
[ad011c]20#include "CodePatterns/MemDebug.hpp"
[6e00dd]21
[83c09a]22#include "Box.hpp"
23
[f429d7]24#include <cmath>
[16648f]25#include <iostream>
[d2938f]26#include <cstdlib>
[f429d7]27
[06aedc]28#include "CodePatterns/Assert.hpp"
29#include "CodePatterns/Log.hpp"
30#include "CodePatterns/Verbose.hpp"
31#include "Helpers/defs.hpp"
[cca9ef]32#include "LinearAlgebra/RealSpaceMatrix.hpp"
[57f243]33#include "LinearAlgebra/Vector.hpp"
34#include "LinearAlgebra/Plane.hpp"
[c538d1]35#include "Shapes/BaseShapes.hpp"
36#include "Shapes/ShapeOps.hpp"
[83c09a]37
[6e00dd]38
[528b3e]39Box::Box() :
40 M(new RealSpaceMatrix()),
41 Minv(new RealSpaceMatrix())
[83c09a]42{
[de29ad6]43 internal_list.reserve(pow(3,3));
44 coords.reserve(NDIM);
45 index.reserve(NDIM);
[9eb7580]46 M->setIdentity();
47 Minv->setIdentity();
[77374e]48 conditions.resize(3);
49 conditions[0] = conditions[1] = conditions[2] = Wrap;
[83c09a]50}
51
[528b3e]52Box::Box(const Box& src) :
53 conditions(src.conditions),
54 M(new RealSpaceMatrix(*src.M)),
55 Minv(new RealSpaceMatrix(*src.Minv))
[de29ad6]56{
57 internal_list.reserve(pow(3,3));
58 coords.reserve(NDIM);
59 index.reserve(NDIM);
60}
[528b3e]61
62Box::Box(RealSpaceMatrix _M) :
63 M(new RealSpaceMatrix(_M)),
64 Minv(new RealSpaceMatrix())
65{
[de29ad6]66 internal_list.reserve(pow(3,3));
67 coords.reserve(NDIM);
68 index.reserve(NDIM);
[528b3e]69 ASSERT(M->determinant()!=0,"Matrix in Box construction was not invertible");
70 *Minv = M->invert();
[71d81a]71 conditions.resize(3);
72 conditions[0] = conditions[1] = conditions[2] = Wrap;
[7579a4b]73}
74
[83c09a]75Box::~Box()
76{
77 delete M;
78 delete Minv;
79}
80
[cca9ef]81const RealSpaceMatrix &Box::getM() const{
[83c09a]82 return *M;
83}
[cca9ef]84const RealSpaceMatrix &Box::getMinv() const{
[83c09a]85 return *Minv;
86}
87
[cca9ef]88void Box::setM(RealSpaceMatrix _M){
[3bf9e2]89 ASSERT(_M.determinant()!=0,"Matrix in Box construction was not invertible");
[83c09a]90 *M =_M;
91 *Minv = M->invert();
92}
[7579a4b]93
[014475]94Vector Box::translateIn(const Vector &point) const{
[3dcb1f]95 return (*M) * point;
96}
97
[014475]98Vector Box::translateOut(const Vector &point) const{
[3dcb1f]99 return (*Minv) * point;
100}
101
[014475]102Vector Box::WrapPeriodically(const Vector &point) const{
[f429d7]103 Vector helper = translateOut(point);
104 for(int i=NDIM;i--;){
[c72562]105
106 switch(conditions[i]){
107 case Wrap:
108 helper.at(i)=fmod(helper.at(i),1);
[d2938f]109 helper.at(i)+=(helper.at(i)>=0)?0:1;
[c72562]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");
[68c923]124 break;
[c72562]125 }
126
[f429d7]127 }
128 return translateIn(helper);
129}
130
[0ff6b5]131bool Box::isInside(const Vector &point) const
132{
133 bool result = true;
[29ac78]134 Vector tester = translateOut(point);
[0ff6b5]135
136 for(int i=0;i<NDIM;i++)
[f3be87]137 result = result &&
138 ((conditions[i] == Ignore) ||
139 ((tester[i] >= -MYEPSILON) &&
140 ((tester[i] - 1.) < MYEPSILON)));
[0ff6b5]141
142 return result;
143}
144
145
[de29ad6]146VECTORSET(std::vector) Box::explode(const Vector &point,int n) const{
[16648f]147 ASSERT(isInside(point),"Exploded point not inside Box");
[025048]148 internal_explode(point, n);
[de29ad6]149 VECTORSET(std::vector) res(internal_list);
[025048]150 return res;
151}
152
153void Box::internal_explode(const Vector &point,int n) const{
154 internal_list.clear();
[de29ad6]155 size_t list_index = 0;
[89e820]156
[16648f]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
[de29ad6]162 coords.clear();
163 index.clear();
[16648f]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
[de29ad6]173 internal_list.resize(pow(2*n+1, dims));
[16648f]174
175 if(!dims){
176 // all boundaries are ignored
[de29ad6]177 internal_list[list_index++] = point;
[025048]178 return;
[89e820]179 }
180
[d2938f]181 bool done = false;
182 while(!done){
[16648f]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
[d2938f]201 int n = abs(index[i]) + ((index[i]<0)?-1:0);
[16648f]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");
[025048]210 break;
[16648f]211 default:
212 ASSERT(0,"No default case for this switch-case");
[025048]213 break;
[7ac4af]214 }
215
[16648f]216 }
217 // add back all ignored coordinates (not handled in above loop)
218 helper+=mask;
[de29ad6]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);
[16648f]223 // set the new indexes
[d2938f]224 int pos=0;
[16648f]225 ++index[pos];
[d2938f]226 while(index[pos]>n){
[16648f]227 index[pos++]=-n;
[d2938f]228 if(pos>=dims) { // it's trying to increase one beyond array... all vectors generated
229 done = true;
230 break;
[7ac4af]231 }
[16648f]232 ++index[pos];
[7ac4af]233 }
234 }
235}
236
[de29ad6]237VECTORSET(std::vector) Box::explode(const Vector &point) const{
[16648f]238 ASSERT(isInside(point),"Exploded point not inside Box");
239 return explode(point,1);
240}
241
[014475]242double Box::periodicDistanceSquared(const Vector &point1,const Vector &point2) const{
[025048]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);
[014475]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;
[527de2]254}
255
[66fd49]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) );
[47d041]264 LOG(1, "Inserting distance " << tempres1 << " and " << tempres2 << ".");
[66fd49]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
[c538d1]271Shape Box::getShape() const{
272 return transform(Cuboid(Vector(0,0,0),Vector(1,1,1)),(*M));
[6c438f]273}
274
[66fd49]275const Box::Conditions_t Box::getConditions() const
276{
[77374e]277 return conditions;
278}
[c538d1]279
[77374e]280void Box::setCondition(int i,Box::BoundaryCondition_t condition){
281 conditions[i]=condition;
282}
283
[de29ad6]284const std::vector<std::pair<Plane,Plane> > Box::getBoundingPlanes() const
[66fd49]285{
[de29ad6]286 std::vector<std::pair<Plane,Plane> > res;
[29ac78]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 }
[66fd49]301 ASSERT(res.size() == 3, "Box::getBoundingPlanes() - does not have three plane pairs!");
[29ac78]302 return res;
303}
304
[e1ab97]305void Box::setCuboid(const Vector &endpoint){
306 ASSERT(endpoint[0]>0 && endpoint[1]>0 && endpoint[2]>0,"Vector does not define a full cuboid");
[9eb7580]307 M->setIdentity();
[e1ab97]308 M->diagonal()=endpoint;
309 Vector &dinv = Minv->diagonal();
310 for(int i=NDIM;i--;)
311 dinv[i]=1/endpoint[i];
[c538d1]312}
313
[7579a4b]314Box &Box::operator=(const Box &src){
315 if(&src!=this){
316 delete M;
317 delete Minv;
[cca9ef]318 M = new RealSpaceMatrix(*src.M);
319 Minv = new RealSpaceMatrix(*src.Minv);
[77374e]320 conditions = src.conditions;
[7579a4b]321 }
322 return *this;
323}
324
[cca9ef]325Box &Box::operator=(const RealSpaceMatrix &mat){
[7579a4b]326 setM(mat);
327 return *this;
328}
[528b3e]329
[de29ad6]330std::ostream & operator << (std::ostream& ost, const Box &m)
[528b3e]331{
332 ost << m.getM();
333 return ost;
334}
Note: See TracBrowser for help on using the repository browser.