source: src/Box.cpp@ 5169d1b

Candidate_v1.7.1 stable v1.7.1
Last change on this file since 5169d1b was c8cb0d, checked in by Frederik Heber <frederik.heber@…>, 6 weeks ago

Streamlines channel creation in Observables.

  • CodePatterns is now version 1.3.4.
  • we no longer need to add the channels manually in the cstor of a class that derives from Observable. Instead, we just need to pass the maximum number of channels (as they are typically enumerated anyway) and they are generated and added.
  • added mutex protection when inserting.
  • adjusted class Relay to forward similar convenience cstors.
  • adjusted all call sites in molecuilder.
  • Property mode set to 100644
File size: 11.3 KB
RevLine 
[bcf653]1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
[0aa122]4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
[5aaa43]5 * Copyright (C) 2013 Frederik Heber. All rights reserved.
[94d5ac6]6 *
7 *
8 * This file is part of MoleCuilder.
9 *
10 * MoleCuilder is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * MoleCuilder is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
[bcf653]22 */
23
[83c09a]24/*
25 * Box.cpp
26 *
27 * Created on: Jun 30, 2010
28 * Author: crueger
29 */
30
[bf3817]31// include config.h
32#ifdef HAVE_CONFIG_H
33#include <config.h>
34#endif
35
[9eb71b3]36//#include "CodePatterns/MemDebug.hpp"
[6e00dd]37
[83c09a]38#include "Box.hpp"
39
[f429d7]40#include <cmath>
[d2938f]41#include <cstdlib>
[c52e08]42#include <iostream>
43#include <sstream>
[f429d7]44
[06aedc]45#include "CodePatterns/Assert.hpp"
46#include "CodePatterns/Log.hpp"
[99f4ee]47#include "CodePatterns/Observer/Channels.hpp"
48#include "CodePatterns/Observer/Notification.hpp"
[06aedc]49#include "CodePatterns/Verbose.hpp"
50#include "Helpers/defs.hpp"
[cca9ef]51#include "LinearAlgebra/RealSpaceMatrix.hpp"
[57f243]52#include "LinearAlgebra/Vector.hpp"
53#include "LinearAlgebra/Plane.hpp"
[c538d1]54#include "Shapes/BaseShapes.hpp"
55#include "Shapes/ShapeOps.hpp"
[83c09a]56
[6e00dd]57
[528b3e]58Box::Box() :
[c8cb0d]59 Observable("Box", NotificationType_MAX),
[528b3e]60 M(new RealSpaceMatrix()),
61 Minv(new RealSpaceMatrix())
[83c09a]62{
[de29ad6]63 internal_list.reserve(pow(3,3));
64 coords.reserve(NDIM);
65 index.reserve(NDIM);
[99f4ee]66
[9eb7580]67 M->setIdentity();
68 Minv->setIdentity();
[83c09a]69}
70
[528b3e]71Box::Box(const Box& src) :
[c8cb0d]72 Observable("Box", NotificationType_MAX),
[99f4ee]73 conditions(src.conditions),
74 M(new RealSpaceMatrix(*src.M)),
75 Minv(new RealSpaceMatrix(*src.Minv))
[de29ad6]76{
77 internal_list.reserve(pow(3,3));
78 coords.reserve(NDIM);
79 index.reserve(NDIM);
80}
[528b3e]81
82Box::Box(RealSpaceMatrix _M) :
[c8cb0d]83 Observable("Box", NotificationType_MAX),
[528b3e]84 M(new RealSpaceMatrix(_M)),
85 Minv(new RealSpaceMatrix())
86{
[de29ad6]87 internal_list.reserve(pow(3,3));
88 coords.reserve(NDIM);
89 index.reserve(NDIM);
[99f4ee]90
[528b3e]91 ASSERT(M->determinant()!=0,"Matrix in Box construction was not invertible");
92 *Minv = M->invert();
[7579a4b]93}
94
[83c09a]95Box::~Box()
96{
97 delete M;
98 delete Minv;
99}
100
[cca9ef]101const RealSpaceMatrix &Box::getM() const{
[83c09a]102 return *M;
103}
[cca9ef]104const RealSpaceMatrix &Box::getMinv() const{
[83c09a]105 return *Minv;
106}
107
[25aa5ad]108void Box::setM(const RealSpaceMatrix &_M){
[3bf9e2]109 ASSERT(_M.determinant()!=0,"Matrix in Box construction was not invertible");
[99f4ee]110 OBSERVE;
[d00370]111 if (_M != *M)
112 NOTIFY(MatrixChanged);
[83c09a]113 *M =_M;
114 *Minv = M->invert();
115}
[7579a4b]116
[014475]117Vector Box::translateIn(const Vector &point) const{
[3dcb1f]118 return (*M) * point;
119}
120
[014475]121Vector Box::translateOut(const Vector &point) const{
[3dcb1f]122 return (*Minv) * point;
123}
124
[712886]125Vector Box::enforceBoundaryConditions(const Vector &point) const{
[f429d7]126 Vector helper = translateOut(point);
127 for(int i=NDIM;i--;){
[c72562]128
129 switch(conditions[i]){
[d66cb7]130 case BoundaryConditions::Wrap:
[c72562]131 helper.at(i)=fmod(helper.at(i),1);
[d2938f]132 helper.at(i)+=(helper.at(i)>=0)?0:1;
[c72562]133 break;
[d66cb7]134 case BoundaryConditions::Bounce:
[c72562]135 {
136 // there probably is a better way to handle this...
137 // all the fabs and fmod modf probably makes it very slow
138 double intpart,fracpart;
139 fracpart = modf(fabs(helper.at(i)),&intpart);
140 helper.at(i) = fabs(fracpart-fmod(intpart,2));
141 }
142 break;
[d66cb7]143 case BoundaryConditions::Ignore:
[c72562]144 break;
145 default:
146 ASSERT(0,"No default case for this");
[68c923]147 break;
[c72562]148 }
149
[f429d7]150 }
151 return translateIn(helper);
152}
153
[0ff6b5]154bool Box::isInside(const Vector &point) const
155{
156 bool result = true;
[29ac78]157 Vector tester = translateOut(point);
[0ff6b5]158
159 for(int i=0;i<NDIM;i++)
[f3be87]160 result = result &&
[d66cb7]161 ((conditions[i] == BoundaryConditions::Ignore) ||
[f3be87]162 ((tester[i] >= -MYEPSILON) &&
163 ((tester[i] - 1.) < MYEPSILON)));
[0ff6b5]164
165 return result;
166}
167
[2a0271]168bool Box::isValid(const Vector &point) const
169{
170 bool result = true;
171 Vector tester = translateOut(point);
172
173 for(int i=0;i<NDIM;i++)
174 result = result &&
175 ((conditions[i] != BoundaryConditions::Ignore) ||
176 ((tester[i] >= -MYEPSILON) &&
177 ((tester[i] - 1.) < MYEPSILON)));
178
179 return result;
180}
181
[0ff6b5]182
[de29ad6]183VECTORSET(std::vector) Box::explode(const Vector &point,int n) const{
[16648f]184 ASSERT(isInside(point),"Exploded point not inside Box");
[025048]185 internal_explode(point, n);
[de29ad6]186 VECTORSET(std::vector) res(internal_list);
[025048]187 return res;
188}
189
190void Box::internal_explode(const Vector &point,int n) const{
[31021ab]191// internal_list.clear();
[de29ad6]192 size_t list_index = 0;
[89e820]193
[16648f]194 Vector translater = translateOut(point);
195 Vector mask; // contains the ignored coordinates
196
197 // count the number of coordinates we need to do
198 int dims = 0; // number of dimensions that are not ignored
[de29ad6]199 coords.clear();
200 index.clear();
[16648f]201 for(int i=0;i<NDIM;++i){
[d66cb7]202 if(conditions[i]==BoundaryConditions::Ignore){
[16648f]203 mask[i]=translater[i];
204 continue;
205 }
206 coords.push_back(i);
207 index.push_back(-n);
208 dims++;
209 } // there are max vectors in total we need to create
[a2a2f7]210
211 {
212 const size_t new_size = pow(2*n+1, dims);
213 if (internal_list.size() != new_size)
214 internal_list.resize(new_size);
215 }
[16648f]216
217 if(!dims){
218 // all boundaries are ignored
[de29ad6]219 internal_list[list_index++] = point;
[025048]220 return;
[89e820]221 }
222
[d2938f]223 bool done = false;
224 while(!done){
[16648f]225 // create this vector
226 Vector helper;
227 for(int i=0;i<dims;++i){
228 switch(conditions[coords[i]]){
[d66cb7]229 case BoundaryConditions::Wrap:
[16648f]230 helper[coords[i]] = index[i]+translater[coords[i]];
231 break;
[d66cb7]232 case BoundaryConditions::Bounce:
[16648f]233 {
234 // Bouncing the coordinate x produces the series:
235 // 0 -> x
236 // 1 -> 2-x
237 // 2 -> 2+x
238 // 3 -> 4-x
239 // 4 -> 4+x
240 // the first number is the next bigger even number (n+n%2)
241 // the next number is the value with alternating sign (x-2*(n%2)*x)
242 // the negative numbers produce the same sequence reversed and shifted
[d2938f]243 int n = abs(index[i]) + ((index[i]<0)?-1:0);
[16648f]244 int sign = (index[i]<0)?-1:+1;
245 int even = n%2;
246 helper[coords[i]]=n+even+translater[coords[i]]-2*even*translater[coords[i]];
247 helper[coords[i]]*=sign;
248 }
249 break;
[d66cb7]250 case BoundaryConditions::Ignore:
[16648f]251 ASSERT(0,"Ignored coordinate handled in generation loop");
[025048]252 break;
[16648f]253 default:
254 ASSERT(0,"No default case for this switch-case");
[025048]255 break;
[7ac4af]256 }
257
[16648f]258 }
259 // add back all ignored coordinates (not handled in above loop)
260 helper+=mask;
[de29ad6]261 ASSERT(list_index < internal_list.size(),
262 "Box::internal_explode() - we have estimated the number of vectors wrong: "
263 +toString(list_index) +" >= "+toString(internal_list.size())+".");
264 internal_list[list_index++] = translateIn(helper);
[16648f]265 // set the new indexes
[d2938f]266 int pos=0;
[16648f]267 ++index[pos];
[d2938f]268 while(index[pos]>n){
[16648f]269 index[pos++]=-n;
[d2938f]270 if(pos>=dims) { // it's trying to increase one beyond array... all vectors generated
271 done = true;
272 break;
[7ac4af]273 }
[16648f]274 ++index[pos];
[7ac4af]275 }
276 }
277}
278
[de29ad6]279VECTORSET(std::vector) Box::explode(const Vector &point) const{
[16648f]280 ASSERT(isInside(point),"Exploded point not inside Box");
281 return explode(point,1);
282}
283
[7b9fe0]284const Vector Box::periodicDistanceVector(const Vector &point1,const Vector &point2) const{
[712886]285 Vector helper1(enforceBoundaryConditions(point1));
286 Vector helper2(enforceBoundaryConditions(point2));
[025048]287 internal_explode(helper1,1);
[7b9fe0]288 const Vector res = internal_list.minDistance(helper2);
[014475]289 return res;
290}
291
[7b9fe0]292double Box::periodicDistanceSquared(const Vector &point1,const Vector &point2) const{
293 const Vector res = periodicDistanceVector(point1, point2);
294 return res.NormSquared();
295}
296
[014475]297double Box::periodicDistance(const Vector &point1,const Vector &point2) const{
[7b9fe0]298 double res = sqrt(periodicDistanceSquared(point1,point2));
[014475]299 return res;
[527de2]300}
301
[66fd49]302double Box::DistanceToBoundary(const Vector &point) const
303{
304 std::map<double, Plane> DistanceSet;
305 std::vector<std::pair<Plane,Plane> > Boundaries = getBoundingPlanes();
306 for (int i=0;i<NDIM;++i) {
307 const double tempres1 = Boundaries[i].first.distance(point);
308 const double tempres2 = Boundaries[i].second.distance(point);
309 DistanceSet.insert( make_pair(tempres1, Boundaries[i].first) );
[47d041]310 LOG(1, "Inserting distance " << tempres1 << " and " << tempres2 << ".");
[66fd49]311 DistanceSet.insert( make_pair(tempres2, Boundaries[i].second) );
312 }
313 ASSERT(!DistanceSet.empty(), "Box::DistanceToBoundary() - no distances in map!");
314 return (DistanceSet.begin())->first;
315}
316
[c538d1]317Shape Box::getShape() const{
318 return transform(Cuboid(Vector(0,0,0),Vector(1,1,1)),(*M));
[6c438f]319}
320
[c52e08]321const std::string Box::getConditionNames() const
322{
323 std::stringstream outputstream;
324 outputstream << conditions;
325 return outputstream.str();
326}
327
328const BoundaryConditions::Conditions_t & Box::getConditions() const
[66fd49]329{
[d66cb7]330 return conditions.get();
[77374e]331}
[c538d1]332
[c52e08]333const BoundaryConditions::BoundaryCondition_t Box::getCondition(size_t i) const
334{
335 return conditions.get(i);
336}
337
338void Box::setCondition(size_t i, const BoundaryConditions::BoundaryCondition_t _condition)
339{
340 OBSERVE;
[d00370]341 if (conditions.get(i) != _condition)
342 NOTIFY(BoundaryConditionsChanged);
[c52e08]343 conditions.set(i, _condition);
344}
345
346void Box::setConditions(const BoundaryConditions::Conditions_t & _conditions)
347{
348 OBSERVE;
[d00370]349 if (conditions.get() != _conditions)
[c52e08]350 NOTIFY(BoundaryConditionsChanged);
351 conditions.set(_conditions);
352}
353
354void Box::setConditions(const std::string & _conditions)
[99f4ee]355{
356 OBSERVE;
357 NOTIFY(BoundaryConditionsChanged);
[c52e08]358 std::stringstream inputstream(_conditions);
359 inputstream >> conditions;
[77374e]360}
361
[97f9b9]362void Box::setConditions(const std::vector< std::string >& _conditions)
363{
364 OBSERVE;
365 NOTIFY(BoundaryConditionsChanged);
366 conditions.set(_conditions);
367}
368
[de29ad6]369const std::vector<std::pair<Plane,Plane> > Box::getBoundingPlanes() const
[66fd49]370{
[de29ad6]371 std::vector<std::pair<Plane,Plane> > res;
[29ac78]372 for(int i=0;i<NDIM;++i){
373 Vector base1,base2,base3;
374 base2[(i+1)%NDIM] = 1.;
375 base3[(i+2)%NDIM] = 1.;
376 Plane p1(translateIn(base1),
377 translateIn(base2),
378 translateIn(base3));
379 Vector offset;
380 offset[i]=1;
381 Plane p2(translateIn(base1+offset),
382 translateIn(base2+offset),
383 translateIn(base3+offset));
384 res.push_back(make_pair(p1,p2));
385 }
[66fd49]386 ASSERT(res.size() == 3, "Box::getBoundingPlanes() - does not have three plane pairs!");
[29ac78]387 return res;
388}
389
[99f4ee]390void Box::setCuboid(const Vector &endpoint)
391{
392 OBSERVE;
393 NOTIFY(MatrixChanged);
[e1ab97]394 ASSERT(endpoint[0]>0 && endpoint[1]>0 && endpoint[2]>0,"Vector does not define a full cuboid");
[9eb7580]395 M->setIdentity();
[e1ab97]396 M->diagonal()=endpoint;
397 Vector &dinv = Minv->diagonal();
398 for(int i=NDIM;i--;)
399 dinv[i]=1/endpoint[i];
[c538d1]400}
401
[99f4ee]402Box &Box::operator=(const Box &src)
403{
[7579a4b]404 if(&src!=this){
[99f4ee]405 OBSERVE;
406 // new matrix
407 NOTIFY(MatrixChanged);
[7579a4b]408 delete M;
409 delete Minv;
[cca9ef]410 M = new RealSpaceMatrix(*src.M);
411 Minv = new RealSpaceMatrix(*src.Minv);
[99f4ee]412 // new boundary conditions
413 NOTIFY(BoundaryConditionsChanged);
[77374e]414 conditions = src.conditions;
[7579a4b]415 }
416 return *this;
417}
418
[99f4ee]419Box &Box::operator=(const RealSpaceMatrix &mat)
420{
421 OBSERVE;
422 NOTIFY(MatrixChanged);
[7579a4b]423 setM(mat);
424 return *this;
425}
[528b3e]426
[de29ad6]427std::ostream & operator << (std::ostream& ost, const Box &m)
[528b3e]428{
429 ost << m.getM();
430 return ost;
431}
Note: See TracBrowser for help on using the repository browser.