1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010-2012 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 <cstdlib>
|
---|
26 | #include <iostream>
|
---|
27 | #include <sstream>
|
---|
28 |
|
---|
29 | #include "CodePatterns/Assert.hpp"
|
---|
30 | #include "CodePatterns/Log.hpp"
|
---|
31 | #include "CodePatterns/Observer/Channels.hpp"
|
---|
32 | #include "CodePatterns/Observer/Notification.hpp"
|
---|
33 | #include "CodePatterns/Verbose.hpp"
|
---|
34 | #include "Helpers/defs.hpp"
|
---|
35 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
|
---|
36 | #include "LinearAlgebra/Vector.hpp"
|
---|
37 | #include "LinearAlgebra/Plane.hpp"
|
---|
38 | #include "Shapes/BaseShapes.hpp"
|
---|
39 | #include "Shapes/ShapeOps.hpp"
|
---|
40 |
|
---|
41 |
|
---|
42 | Box::Box() :
|
---|
43 | Observable("Box"),
|
---|
44 | M(new RealSpaceMatrix()),
|
---|
45 | Minv(new RealSpaceMatrix())
|
---|
46 | {
|
---|
47 | internal_list.reserve(pow(3,3));
|
---|
48 | coords.reserve(NDIM);
|
---|
49 | index.reserve(NDIM);
|
---|
50 |
|
---|
51 | // observable stuff
|
---|
52 | Channels *OurChannel = new Channels;
|
---|
53 | NotificationChannels.insert( std::make_pair(this, OurChannel) );
|
---|
54 | // add instance for each notification type
|
---|
55 | for (size_t type = 0; type < NotificationType_MAX; ++type)
|
---|
56 | OurChannel->addChannel(type);
|
---|
57 |
|
---|
58 | M->setIdentity();
|
---|
59 | Minv->setIdentity();
|
---|
60 | }
|
---|
61 |
|
---|
62 | Box::Box(const Box& src) :
|
---|
63 | Observable("Box"),
|
---|
64 | conditions(src.conditions),
|
---|
65 | M(new RealSpaceMatrix(*src.M)),
|
---|
66 | Minv(new RealSpaceMatrix(*src.Minv))
|
---|
67 | {
|
---|
68 | internal_list.reserve(pow(3,3));
|
---|
69 | coords.reserve(NDIM);
|
---|
70 | index.reserve(NDIM);
|
---|
71 |
|
---|
72 | // observable stuff
|
---|
73 | Channels *OurChannel = new Channels;
|
---|
74 | NotificationChannels.insert( std::make_pair(this, OurChannel) );
|
---|
75 | // add instance for each notification type
|
---|
76 | for (size_t type = 0; type < NotificationType_MAX; ++type)
|
---|
77 | OurChannel->addChannel(type);
|
---|
78 | }
|
---|
79 |
|
---|
80 | Box::Box(RealSpaceMatrix _M) :
|
---|
81 | Observable("Box"),
|
---|
82 | M(new RealSpaceMatrix(_M)),
|
---|
83 | Minv(new RealSpaceMatrix())
|
---|
84 | {
|
---|
85 | internal_list.reserve(pow(3,3));
|
---|
86 | coords.reserve(NDIM);
|
---|
87 | index.reserve(NDIM);
|
---|
88 |
|
---|
89 | // observable stuff
|
---|
90 | Channels *OurChannel = new Channels;
|
---|
91 | NotificationChannels.insert( std::make_pair(this, OurChannel) );
|
---|
92 | // add instance for each notification type
|
---|
93 | for (size_t type = 0; type < NotificationType_MAX; ++type)
|
---|
94 | OurChannel->addChannel(type);
|
---|
95 |
|
---|
96 | ASSERT(M->determinant()!=0,"Matrix in Box construction was not invertible");
|
---|
97 | *Minv = M->invert();
|
---|
98 | }
|
---|
99 |
|
---|
100 | Box::~Box()
|
---|
101 | {
|
---|
102 | // observable stuff
|
---|
103 | std::map<Observable *, Channels*>::iterator iter = NotificationChannels.find(this);
|
---|
104 | delete iter->second;
|
---|
105 | NotificationChannels.erase(iter);
|
---|
106 |
|
---|
107 | delete M;
|
---|
108 | delete Minv;
|
---|
109 | }
|
---|
110 |
|
---|
111 | const RealSpaceMatrix &Box::getM() const{
|
---|
112 | return *M;
|
---|
113 | }
|
---|
114 | const RealSpaceMatrix &Box::getMinv() const{
|
---|
115 | return *Minv;
|
---|
116 | }
|
---|
117 |
|
---|
118 | void Box::setM(RealSpaceMatrix _M){
|
---|
119 | ASSERT(_M.determinant()!=0,"Matrix in Box construction was not invertible");
|
---|
120 | OBSERVE;
|
---|
121 | if (_M != *M)
|
---|
122 | NOTIFY(MatrixChanged);
|
---|
123 | *M =_M;
|
---|
124 | *Minv = M->invert();
|
---|
125 | }
|
---|
126 |
|
---|
127 | Vector Box::translateIn(const Vector &point) const{
|
---|
128 | return (*M) * point;
|
---|
129 | }
|
---|
130 |
|
---|
131 | Vector Box::translateOut(const Vector &point) const{
|
---|
132 | return (*Minv) * point;
|
---|
133 | }
|
---|
134 |
|
---|
135 | Vector Box::enforceBoundaryConditions(const Vector &point) const{
|
---|
136 | Vector helper = translateOut(point);
|
---|
137 | for(int i=NDIM;i--;){
|
---|
138 |
|
---|
139 | switch(conditions[i]){
|
---|
140 | case BoundaryConditions::Wrap:
|
---|
141 | helper.at(i)=fmod(helper.at(i),1);
|
---|
142 | helper.at(i)+=(helper.at(i)>=0)?0:1;
|
---|
143 | break;
|
---|
144 | case BoundaryConditions::Bounce:
|
---|
145 | {
|
---|
146 | // there probably is a better way to handle this...
|
---|
147 | // all the fabs and fmod modf probably makes it very slow
|
---|
148 | double intpart,fracpart;
|
---|
149 | fracpart = modf(fabs(helper.at(i)),&intpart);
|
---|
150 | helper.at(i) = fabs(fracpart-fmod(intpart,2));
|
---|
151 | }
|
---|
152 | break;
|
---|
153 | case BoundaryConditions::Ignore:
|
---|
154 | break;
|
---|
155 | default:
|
---|
156 | ASSERT(0,"No default case for this");
|
---|
157 | break;
|
---|
158 | }
|
---|
159 |
|
---|
160 | }
|
---|
161 | return translateIn(helper);
|
---|
162 | }
|
---|
163 |
|
---|
164 | bool Box::isInside(const Vector &point) const
|
---|
165 | {
|
---|
166 | bool result = true;
|
---|
167 | Vector tester = translateOut(point);
|
---|
168 |
|
---|
169 | for(int i=0;i<NDIM;i++)
|
---|
170 | result = result &&
|
---|
171 | ((conditions[i] == BoundaryConditions::Ignore) ||
|
---|
172 | ((tester[i] >= -MYEPSILON) &&
|
---|
173 | ((tester[i] - 1.) < MYEPSILON)));
|
---|
174 |
|
---|
175 | return result;
|
---|
176 | }
|
---|
177 |
|
---|
178 | bool Box::isValid(const Vector &point) const
|
---|
179 | {
|
---|
180 | bool result = true;
|
---|
181 | Vector tester = translateOut(point);
|
---|
182 |
|
---|
183 | for(int i=0;i<NDIM;i++)
|
---|
184 | result = result &&
|
---|
185 | ((conditions[i] != BoundaryConditions::Ignore) ||
|
---|
186 | ((tester[i] >= -MYEPSILON) &&
|
---|
187 | ((tester[i] - 1.) < MYEPSILON)));
|
---|
188 |
|
---|
189 | return result;
|
---|
190 | }
|
---|
191 |
|
---|
192 |
|
---|
193 | VECTORSET(std::vector) Box::explode(const Vector &point,int n) const{
|
---|
194 | ASSERT(isInside(point),"Exploded point not inside Box");
|
---|
195 | internal_explode(point, n);
|
---|
196 | VECTORSET(std::vector) res(internal_list);
|
---|
197 | return res;
|
---|
198 | }
|
---|
199 |
|
---|
200 | void Box::internal_explode(const Vector &point,int n) const{
|
---|
201 | internal_list.clear();
|
---|
202 | size_t list_index = 0;
|
---|
203 |
|
---|
204 | Vector translater = translateOut(point);
|
---|
205 | Vector mask; // contains the ignored coordinates
|
---|
206 |
|
---|
207 | // count the number of coordinates we need to do
|
---|
208 | int dims = 0; // number of dimensions that are not ignored
|
---|
209 | coords.clear();
|
---|
210 | index.clear();
|
---|
211 | for(int i=0;i<NDIM;++i){
|
---|
212 | if(conditions[i]==BoundaryConditions::Ignore){
|
---|
213 | mask[i]=translater[i];
|
---|
214 | continue;
|
---|
215 | }
|
---|
216 | coords.push_back(i);
|
---|
217 | index.push_back(-n);
|
---|
218 | dims++;
|
---|
219 | } // there are max vectors in total we need to create
|
---|
220 | internal_list.resize(pow(2*n+1, dims));
|
---|
221 |
|
---|
222 | if(!dims){
|
---|
223 | // all boundaries are ignored
|
---|
224 | internal_list[list_index++] = point;
|
---|
225 | return;
|
---|
226 | }
|
---|
227 |
|
---|
228 | bool done = false;
|
---|
229 | while(!done){
|
---|
230 | // create this vector
|
---|
231 | Vector helper;
|
---|
232 | for(int i=0;i<dims;++i){
|
---|
233 | switch(conditions[coords[i]]){
|
---|
234 | case BoundaryConditions::Wrap:
|
---|
235 | helper[coords[i]] = index[i]+translater[coords[i]];
|
---|
236 | break;
|
---|
237 | case BoundaryConditions::Bounce:
|
---|
238 | {
|
---|
239 | // Bouncing the coordinate x produces the series:
|
---|
240 | // 0 -> x
|
---|
241 | // 1 -> 2-x
|
---|
242 | // 2 -> 2+x
|
---|
243 | // 3 -> 4-x
|
---|
244 | // 4 -> 4+x
|
---|
245 | // the first number is the next bigger even number (n+n%2)
|
---|
246 | // the next number is the value with alternating sign (x-2*(n%2)*x)
|
---|
247 | // the negative numbers produce the same sequence reversed and shifted
|
---|
248 | int n = abs(index[i]) + ((index[i]<0)?-1:0);
|
---|
249 | int sign = (index[i]<0)?-1:+1;
|
---|
250 | int even = n%2;
|
---|
251 | helper[coords[i]]=n+even+translater[coords[i]]-2*even*translater[coords[i]];
|
---|
252 | helper[coords[i]]*=sign;
|
---|
253 | }
|
---|
254 | break;
|
---|
255 | case BoundaryConditions::Ignore:
|
---|
256 | ASSERT(0,"Ignored coordinate handled in generation loop");
|
---|
257 | break;
|
---|
258 | default:
|
---|
259 | ASSERT(0,"No default case for this switch-case");
|
---|
260 | break;
|
---|
261 | }
|
---|
262 |
|
---|
263 | }
|
---|
264 | // add back all ignored coordinates (not handled in above loop)
|
---|
265 | helper+=mask;
|
---|
266 | ASSERT(list_index < internal_list.size(),
|
---|
267 | "Box::internal_explode() - we have estimated the number of vectors wrong: "
|
---|
268 | +toString(list_index) +" >= "+toString(internal_list.size())+".");
|
---|
269 | internal_list[list_index++] = translateIn(helper);
|
---|
270 | // set the new indexes
|
---|
271 | int pos=0;
|
---|
272 | ++index[pos];
|
---|
273 | while(index[pos]>n){
|
---|
274 | index[pos++]=-n;
|
---|
275 | if(pos>=dims) { // it's trying to increase one beyond array... all vectors generated
|
---|
276 | done = true;
|
---|
277 | break;
|
---|
278 | }
|
---|
279 | ++index[pos];
|
---|
280 | }
|
---|
281 | }
|
---|
282 | }
|
---|
283 |
|
---|
284 | VECTORSET(std::vector) Box::explode(const Vector &point) const{
|
---|
285 | ASSERT(isInside(point),"Exploded point not inside Box");
|
---|
286 | return explode(point,1);
|
---|
287 | }
|
---|
288 |
|
---|
289 | const Vector Box::periodicDistanceVector(const Vector &point1,const Vector &point2) const{
|
---|
290 | Vector helper1(enforceBoundaryConditions(point1));
|
---|
291 | Vector helper2(enforceBoundaryConditions(point2));
|
---|
292 | internal_explode(helper1,1);
|
---|
293 | const Vector res = internal_list.minDistance(helper2);
|
---|
294 | return res;
|
---|
295 | }
|
---|
296 |
|
---|
297 | double Box::periodicDistanceSquared(const Vector &point1,const Vector &point2) const{
|
---|
298 | const Vector res = periodicDistanceVector(point1, point2);
|
---|
299 | return res.NormSquared();
|
---|
300 | }
|
---|
301 |
|
---|
302 | double Box::periodicDistance(const Vector &point1,const Vector &point2) const{
|
---|
303 | double res = sqrt(periodicDistanceSquared(point1,point2));
|
---|
304 | return res;
|
---|
305 | }
|
---|
306 |
|
---|
307 | double Box::DistanceToBoundary(const Vector &point) const
|
---|
308 | {
|
---|
309 | std::map<double, Plane> DistanceSet;
|
---|
310 | std::vector<std::pair<Plane,Plane> > Boundaries = getBoundingPlanes();
|
---|
311 | for (int i=0;i<NDIM;++i) {
|
---|
312 | const double tempres1 = Boundaries[i].first.distance(point);
|
---|
313 | const double tempres2 = Boundaries[i].second.distance(point);
|
---|
314 | DistanceSet.insert( make_pair(tempres1, Boundaries[i].first) );
|
---|
315 | LOG(1, "Inserting distance " << tempres1 << " and " << tempres2 << ".");
|
---|
316 | DistanceSet.insert( make_pair(tempres2, Boundaries[i].second) );
|
---|
317 | }
|
---|
318 | ASSERT(!DistanceSet.empty(), "Box::DistanceToBoundary() - no distances in map!");
|
---|
319 | return (DistanceSet.begin())->first;
|
---|
320 | }
|
---|
321 |
|
---|
322 | Shape Box::getShape() const{
|
---|
323 | return transform(Cuboid(Vector(0,0,0),Vector(1,1,1)),(*M));
|
---|
324 | }
|
---|
325 |
|
---|
326 | const std::string Box::getConditionNames() const
|
---|
327 | {
|
---|
328 | std::stringstream outputstream;
|
---|
329 | outputstream << conditions;
|
---|
330 | return outputstream.str();
|
---|
331 | }
|
---|
332 |
|
---|
333 | const BoundaryConditions::Conditions_t & Box::getConditions() const
|
---|
334 | {
|
---|
335 | return conditions.get();
|
---|
336 | }
|
---|
337 |
|
---|
338 | const BoundaryConditions::BoundaryCondition_t Box::getCondition(size_t i) const
|
---|
339 | {
|
---|
340 | return conditions.get(i);
|
---|
341 | }
|
---|
342 |
|
---|
343 | void Box::setCondition(size_t i, const BoundaryConditions::BoundaryCondition_t _condition)
|
---|
344 | {
|
---|
345 | OBSERVE;
|
---|
346 | if (conditions.get(i) != _condition)
|
---|
347 | NOTIFY(BoundaryConditionsChanged);
|
---|
348 | conditions.set(i, _condition);
|
---|
349 | }
|
---|
350 |
|
---|
351 | void Box::setConditions(const BoundaryConditions::Conditions_t & _conditions)
|
---|
352 | {
|
---|
353 | OBSERVE;
|
---|
354 | if (conditions.get() != _conditions)
|
---|
355 | NOTIFY(BoundaryConditionsChanged);
|
---|
356 | conditions.set(_conditions);
|
---|
357 | }
|
---|
358 |
|
---|
359 | void Box::setConditions(const std::string & _conditions)
|
---|
360 | {
|
---|
361 | OBSERVE;
|
---|
362 | NOTIFY(BoundaryConditionsChanged);
|
---|
363 | std::stringstream inputstream(_conditions);
|
---|
364 | inputstream >> conditions;
|
---|
365 | }
|
---|
366 |
|
---|
367 | void Box::setConditions(const std::vector< std::string >& _conditions)
|
---|
368 | {
|
---|
369 | OBSERVE;
|
---|
370 | NOTIFY(BoundaryConditionsChanged);
|
---|
371 | conditions.set(_conditions);
|
---|
372 | }
|
---|
373 |
|
---|
374 | const std::vector<std::pair<Plane,Plane> > Box::getBoundingPlanes() const
|
---|
375 | {
|
---|
376 | std::vector<std::pair<Plane,Plane> > res;
|
---|
377 | for(int i=0;i<NDIM;++i){
|
---|
378 | Vector base1,base2,base3;
|
---|
379 | base2[(i+1)%NDIM] = 1.;
|
---|
380 | base3[(i+2)%NDIM] = 1.;
|
---|
381 | Plane p1(translateIn(base1),
|
---|
382 | translateIn(base2),
|
---|
383 | translateIn(base3));
|
---|
384 | Vector offset;
|
---|
385 | offset[i]=1;
|
---|
386 | Plane p2(translateIn(base1+offset),
|
---|
387 | translateIn(base2+offset),
|
---|
388 | translateIn(base3+offset));
|
---|
389 | res.push_back(make_pair(p1,p2));
|
---|
390 | }
|
---|
391 | ASSERT(res.size() == 3, "Box::getBoundingPlanes() - does not have three plane pairs!");
|
---|
392 | return res;
|
---|
393 | }
|
---|
394 |
|
---|
395 | void Box::setCuboid(const Vector &endpoint)
|
---|
396 | {
|
---|
397 | OBSERVE;
|
---|
398 | NOTIFY(MatrixChanged);
|
---|
399 | ASSERT(endpoint[0]>0 && endpoint[1]>0 && endpoint[2]>0,"Vector does not define a full cuboid");
|
---|
400 | M->setIdentity();
|
---|
401 | M->diagonal()=endpoint;
|
---|
402 | Vector &dinv = Minv->diagonal();
|
---|
403 | for(int i=NDIM;i--;)
|
---|
404 | dinv[i]=1/endpoint[i];
|
---|
405 | }
|
---|
406 |
|
---|
407 | Box &Box::operator=(const Box &src)
|
---|
408 | {
|
---|
409 | if(&src!=this){
|
---|
410 | OBSERVE;
|
---|
411 | // new matrix
|
---|
412 | NOTIFY(MatrixChanged);
|
---|
413 | delete M;
|
---|
414 | delete Minv;
|
---|
415 | M = new RealSpaceMatrix(*src.M);
|
---|
416 | Minv = new RealSpaceMatrix(*src.Minv);
|
---|
417 | // new boundary conditions
|
---|
418 | NOTIFY(BoundaryConditionsChanged);
|
---|
419 | conditions = src.conditions;
|
---|
420 | }
|
---|
421 | return *this;
|
---|
422 | }
|
---|
423 |
|
---|
424 | Box &Box::operator=(const RealSpaceMatrix &mat)
|
---|
425 | {
|
---|
426 | OBSERVE;
|
---|
427 | NOTIFY(MatrixChanged);
|
---|
428 | setM(mat);
|
---|
429 | return *this;
|
---|
430 | }
|
---|
431 |
|
---|
432 | std::ostream & operator << (std::ostream& ost, const Box &m)
|
---|
433 | {
|
---|
434 | ost << m.getM();
|
---|
435 | return ost;
|
---|
436 | }
|
---|