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