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