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
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
5 * Copyright (C) 2013 Frederik Heber. All rights reserved.
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/>.
22 */
23
24/*
25 * Box.cpp
26 *
27 * Created on: Jun 30, 2010
28 * Author: crueger
29 */
30
31// include config.h
32#ifdef HAVE_CONFIG_H
33#include <config.h>
34#endif
35
36//#include "CodePatterns/MemDebug.hpp"
37
38#include "Box.hpp"
39
40#include <cmath>
41#include <cstdlib>
42#include <iostream>
43#include <sstream>
44
45#include "CodePatterns/Assert.hpp"
46#include "CodePatterns/Log.hpp"
47#include "CodePatterns/Observer/Channels.hpp"
48#include "CodePatterns/Observer/Notification.hpp"
49#include "CodePatterns/Verbose.hpp"
50#include "Helpers/defs.hpp"
51#include "LinearAlgebra/RealSpaceMatrix.hpp"
52#include "LinearAlgebra/Vector.hpp"
53#include "LinearAlgebra/Plane.hpp"
54#include "Shapes/BaseShapes.hpp"
55#include "Shapes/ShapeOps.hpp"
56
57
58Box::Box() :
59 Observable("Box", NotificationType_MAX),
60 M(new RealSpaceMatrix()),
61 Minv(new RealSpaceMatrix())
62{
63 internal_list.reserve(pow(3,3));
64 coords.reserve(NDIM);
65 index.reserve(NDIM);
66
67 M->setIdentity();
68 Minv->setIdentity();
69}
70
71Box::Box(const Box& src) :
72 Observable("Box", NotificationType_MAX),
73 conditions(src.conditions),
74 M(new RealSpaceMatrix(*src.M)),
75 Minv(new RealSpaceMatrix(*src.Minv))
76{
77 internal_list.reserve(pow(3,3));
78 coords.reserve(NDIM);
79 index.reserve(NDIM);
80}
81
82Box::Box(RealSpaceMatrix _M) :
83 Observable("Box", NotificationType_MAX),
84 M(new RealSpaceMatrix(_M)),
85 Minv(new RealSpaceMatrix())
86{
87 internal_list.reserve(pow(3,3));
88 coords.reserve(NDIM);
89 index.reserve(NDIM);
90
91 ASSERT(M->determinant()!=0,"Matrix in Box construction was not invertible");
92 *Minv = M->invert();
93}
94
95Box::~Box()
96{
97 delete M;
98 delete Minv;
99}
100
101const RealSpaceMatrix &Box::getM() const{
102 return *M;
103}
104const RealSpaceMatrix &Box::getMinv() const{
105 return *Minv;
106}
107
108void Box::setM(const RealSpaceMatrix &_M){
109 ASSERT(_M.determinant()!=0,"Matrix in Box construction was not invertible");
110 OBSERVE;
111 if (_M != *M)
112 NOTIFY(MatrixChanged);
113 *M =_M;
114 *Minv = M->invert();
115}
116
117Vector Box::translateIn(const Vector &point) const{
118 return (*M) * point;
119}
120
121Vector Box::translateOut(const Vector &point) const{
122 return (*Minv) * point;
123}
124
125Vector Box::enforceBoundaryConditions(const Vector &point) const{
126 Vector helper = translateOut(point);
127 for(int i=NDIM;i--;){
128
129 switch(conditions[i]){
130 case BoundaryConditions::Wrap:
131 helper.at(i)=fmod(helper.at(i),1);
132 helper.at(i)+=(helper.at(i)>=0)?0:1;
133 break;
134 case BoundaryConditions::Bounce:
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;
143 case BoundaryConditions::Ignore:
144 break;
145 default:
146 ASSERT(0,"No default case for this");
147 break;
148 }
149
150 }
151 return translateIn(helper);
152}
153
154bool Box::isInside(const Vector &point) const
155{
156 bool result = true;
157 Vector tester = translateOut(point);
158
159 for(int i=0;i<NDIM;i++)
160 result = result &&
161 ((conditions[i] == BoundaryConditions::Ignore) ||
162 ((tester[i] >= -MYEPSILON) &&
163 ((tester[i] - 1.) < MYEPSILON)));
164
165 return result;
166}
167
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
182
183VECTORSET(std::vector) Box::explode(const Vector &point,int n) const{
184 ASSERT(isInside(point),"Exploded point not inside Box");
185 internal_explode(point, n);
186 VECTORSET(std::vector) res(internal_list);
187 return res;
188}
189
190void Box::internal_explode(const Vector &point,int n) const{
191// internal_list.clear();
192 size_t list_index = 0;
193
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
199 coords.clear();
200 index.clear();
201 for(int i=0;i<NDIM;++i){
202 if(conditions[i]==BoundaryConditions::Ignore){
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
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 }
216
217 if(!dims){
218 // all boundaries are ignored
219 internal_list[list_index++] = point;
220 return;
221 }
222
223 bool done = false;
224 while(!done){
225 // create this vector
226 Vector helper;
227 for(int i=0;i<dims;++i){
228 switch(conditions[coords[i]]){
229 case BoundaryConditions::Wrap:
230 helper[coords[i]] = index[i]+translater[coords[i]];
231 break;
232 case BoundaryConditions::Bounce:
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
243 int n = abs(index[i]) + ((index[i]<0)?-1:0);
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;
250 case BoundaryConditions::Ignore:
251 ASSERT(0,"Ignored coordinate handled in generation loop");
252 break;
253 default:
254 ASSERT(0,"No default case for this switch-case");
255 break;
256 }
257
258 }
259 // add back all ignored coordinates (not handled in above loop)
260 helper+=mask;
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);
265 // set the new indexes
266 int pos=0;
267 ++index[pos];
268 while(index[pos]>n){
269 index[pos++]=-n;
270 if(pos>=dims) { // it's trying to increase one beyond array... all vectors generated
271 done = true;
272 break;
273 }
274 ++index[pos];
275 }
276 }
277}
278
279VECTORSET(std::vector) Box::explode(const Vector &point) const{
280 ASSERT(isInside(point),"Exploded point not inside Box");
281 return explode(point,1);
282}
283
284const Vector Box::periodicDistanceVector(const Vector &point1,const Vector &point2) const{
285 Vector helper1(enforceBoundaryConditions(point1));
286 Vector helper2(enforceBoundaryConditions(point2));
287 internal_explode(helper1,1);
288 const Vector res = internal_list.minDistance(helper2);
289 return res;
290}
291
292double Box::periodicDistanceSquared(const Vector &point1,const Vector &point2) const{
293 const Vector res = periodicDistanceVector(point1, point2);
294 return res.NormSquared();
295}
296
297double Box::periodicDistance(const Vector &point1,const Vector &point2) const{
298 double res = sqrt(periodicDistanceSquared(point1,point2));
299 return res;
300}
301
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) );
310 LOG(1, "Inserting distance " << tempres1 << " and " << tempres2 << ".");
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
317Shape Box::getShape() const{
318 return transform(Cuboid(Vector(0,0,0),Vector(1,1,1)),(*M));
319}
320
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
329{
330 return conditions.get();
331}
332
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;
341 if (conditions.get(i) != _condition)
342 NOTIFY(BoundaryConditionsChanged);
343 conditions.set(i, _condition);
344}
345
346void Box::setConditions(const BoundaryConditions::Conditions_t & _conditions)
347{
348 OBSERVE;
349 if (conditions.get() != _conditions)
350 NOTIFY(BoundaryConditionsChanged);
351 conditions.set(_conditions);
352}
353
354void Box::setConditions(const std::string & _conditions)
355{
356 OBSERVE;
357 NOTIFY(BoundaryConditionsChanged);
358 std::stringstream inputstream(_conditions);
359 inputstream >> conditions;
360}
361
362void Box::setConditions(const std::vector< std::string >& _conditions)
363{
364 OBSERVE;
365 NOTIFY(BoundaryConditionsChanged);
366 conditions.set(_conditions);
367}
368
369const std::vector<std::pair<Plane,Plane> > Box::getBoundingPlanes() const
370{
371 std::vector<std::pair<Plane,Plane> > res;
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 }
386 ASSERT(res.size() == 3, "Box::getBoundingPlanes() - does not have three plane pairs!");
387 return res;
388}
389
390void Box::setCuboid(const Vector &endpoint)
391{
392 OBSERVE;
393 NOTIFY(MatrixChanged);
394 ASSERT(endpoint[0]>0 && endpoint[1]>0 && endpoint[2]>0,"Vector does not define a full cuboid");
395 M->setIdentity();
396 M->diagonal()=endpoint;
397 Vector &dinv = Minv->diagonal();
398 for(int i=NDIM;i--;)
399 dinv[i]=1/endpoint[i];
400}
401
402Box &Box::operator=(const Box &src)
403{
404 if(&src!=this){
405 OBSERVE;
406 // new matrix
407 NOTIFY(MatrixChanged);
408 delete M;
409 delete Minv;
410 M = new RealSpaceMatrix(*src.M);
411 Minv = new RealSpaceMatrix(*src.Minv);
412 // new boundary conditions
413 NOTIFY(BoundaryConditionsChanged);
414 conditions = src.conditions;
415 }
416 return *this;
417}
418
419Box &Box::operator=(const RealSpaceMatrix &mat)
420{
421 OBSERVE;
422 NOTIFY(MatrixChanged);
423 setM(mat);
424 return *this;
425}
426
427std::ostream & operator << (std::ostream& ost, const Box &m)
428{
429 ost << m.getM();
430 return ost;
431}
Note: See TracBrowser for help on using the repository browser.