| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2010-2011 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 <iostream>
 | 
|---|
| 26 | #include <cstdlib>
 | 
|---|
| 27 | 
 | 
|---|
| 28 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| 29 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 30 | #include "CodePatterns/Verbose.hpp"
 | 
|---|
| 31 | #include "Helpers/defs.hpp"
 | 
|---|
| 32 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
 | 
|---|
| 33 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
| 34 | #include "LinearAlgebra/Plane.hpp"
 | 
|---|
| 35 | #include "Shapes/BaseShapes.hpp"
 | 
|---|
| 36 | #include "Shapes/ShapeOps.hpp"
 | 
|---|
| 37 | 
 | 
|---|
| 38 | 
 | 
|---|
| 39 | Box::Box() :
 | 
|---|
| 40 |     M(new RealSpaceMatrix()),
 | 
|---|
| 41 |     Minv(new RealSpaceMatrix())
 | 
|---|
| 42 | {
 | 
|---|
| 43 |   internal_list.reserve(pow(3,3));
 | 
|---|
| 44 |   coords.reserve(NDIM);
 | 
|---|
| 45 |   index.reserve(NDIM);
 | 
|---|
| 46 |   M->setIdentity();
 | 
|---|
| 47 |   Minv->setIdentity();
 | 
|---|
| 48 |   conditions.resize(3);
 | 
|---|
| 49 |   conditions[0] = conditions[1] = conditions[2] = Wrap;
 | 
|---|
| 50 | }
 | 
|---|
| 51 | 
 | 
|---|
| 52 | Box::Box(const Box& src) :
 | 
|---|
| 53 |   conditions(src.conditions),
 | 
|---|
| 54 |   M(new RealSpaceMatrix(*src.M)),
 | 
|---|
| 55 |   Minv(new RealSpaceMatrix(*src.Minv))
 | 
|---|
| 56 | {
 | 
|---|
| 57 |   internal_list.reserve(pow(3,3));
 | 
|---|
| 58 |   coords.reserve(NDIM);
 | 
|---|
| 59 |   index.reserve(NDIM);
 | 
|---|
| 60 | }
 | 
|---|
| 61 | 
 | 
|---|
| 62 | Box::Box(RealSpaceMatrix _M) :
 | 
|---|
| 63 |     M(new RealSpaceMatrix(_M)),
 | 
|---|
| 64 |     Minv(new RealSpaceMatrix())
 | 
|---|
| 65 | {
 | 
|---|
| 66 |   internal_list.reserve(pow(3,3));
 | 
|---|
| 67 |   coords.reserve(NDIM);
 | 
|---|
| 68 |   index.reserve(NDIM);
 | 
|---|
| 69 |   ASSERT(M->determinant()!=0,"Matrix in Box construction was not invertible");
 | 
|---|
| 70 |   *Minv = M->invert();
 | 
|---|
| 71 |   conditions.resize(3);
 | 
|---|
| 72 |   conditions[0] = conditions[1] = conditions[2] = Wrap;
 | 
|---|
| 73 | }
 | 
|---|
| 74 | 
 | 
|---|
| 75 | Box::~Box()
 | 
|---|
| 76 | {
 | 
|---|
| 77 |   delete M;
 | 
|---|
| 78 |   delete Minv;
 | 
|---|
| 79 | }
 | 
|---|
| 80 | 
 | 
|---|
| 81 | const RealSpaceMatrix &Box::getM() const{
 | 
|---|
| 82 |   return *M;
 | 
|---|
| 83 | }
 | 
|---|
| 84 | const RealSpaceMatrix &Box::getMinv() const{
 | 
|---|
| 85 |   return *Minv;
 | 
|---|
| 86 | }
 | 
|---|
| 87 | 
 | 
|---|
| 88 | void Box::setM(RealSpaceMatrix _M){
 | 
|---|
| 89 |   ASSERT(_M.determinant()!=0,"Matrix in Box construction was not invertible");
 | 
|---|
| 90 |   *M    =_M;
 | 
|---|
| 91 |   *Minv = M->invert();
 | 
|---|
| 92 | }
 | 
|---|
| 93 | 
 | 
|---|
| 94 | Vector Box::translateIn(const Vector &point) const{
 | 
|---|
| 95 |   return (*M) * point;
 | 
|---|
| 96 | }
 | 
|---|
| 97 | 
 | 
|---|
| 98 | Vector Box::translateOut(const Vector &point) const{
 | 
|---|
| 99 |   return (*Minv) * point;
 | 
|---|
| 100 | }
 | 
|---|
| 101 | 
 | 
|---|
| 102 | Vector Box::WrapPeriodically(const Vector &point) const{
 | 
|---|
| 103 |   Vector helper = translateOut(point);
 | 
|---|
| 104 |   for(int i=NDIM;i--;){
 | 
|---|
| 105 | 
 | 
|---|
| 106 |     switch(conditions[i]){
 | 
|---|
| 107 |     case Wrap:
 | 
|---|
| 108 |       helper.at(i)=fmod(helper.at(i),1);
 | 
|---|
| 109 |       helper.at(i)+=(helper.at(i)>=0)?0:1;
 | 
|---|
| 110 |       break;
 | 
|---|
| 111 |     case Bounce:
 | 
|---|
| 112 |       {
 | 
|---|
| 113 |         // there probably is a better way to handle this...
 | 
|---|
| 114 |         // all the fabs and fmod modf probably makes it very slow
 | 
|---|
| 115 |         double intpart,fracpart;
 | 
|---|
| 116 |         fracpart = modf(fabs(helper.at(i)),&intpart);
 | 
|---|
| 117 |         helper.at(i) = fabs(fracpart-fmod(intpart,2));
 | 
|---|
| 118 |       }
 | 
|---|
| 119 |       break;
 | 
|---|
| 120 |     case Ignore:
 | 
|---|
| 121 |       break;
 | 
|---|
| 122 |     default:
 | 
|---|
| 123 |       ASSERT(0,"No default case for this");
 | 
|---|
| 124 |       break;
 | 
|---|
| 125 |     }
 | 
|---|
| 126 | 
 | 
|---|
| 127 |   }
 | 
|---|
| 128 |   return translateIn(helper);
 | 
|---|
| 129 | }
 | 
|---|
| 130 | 
 | 
|---|
| 131 | bool Box::isInside(const Vector &point) const
 | 
|---|
| 132 | {
 | 
|---|
| 133 |   bool result = true;
 | 
|---|
| 134 |   Vector tester = translateOut(point);
 | 
|---|
| 135 | 
 | 
|---|
| 136 |   for(int i=0;i<NDIM;i++)
 | 
|---|
| 137 |     result = result &&
 | 
|---|
| 138 |               ((conditions[i] == Ignore) ||
 | 
|---|
| 139 |                ((tester[i] >= -MYEPSILON) &&
 | 
|---|
| 140 |                ((tester[i] - 1.) < MYEPSILON)));
 | 
|---|
| 141 | 
 | 
|---|
| 142 |   return result;
 | 
|---|
| 143 | }
 | 
|---|
| 144 | 
 | 
|---|
| 145 | 
 | 
|---|
| 146 | VECTORSET(std::vector) Box::explode(const Vector &point,int n) const{
 | 
|---|
| 147 |   ASSERT(isInside(point),"Exploded point not inside Box");
 | 
|---|
| 148 |   internal_explode(point, n);
 | 
|---|
| 149 |   VECTORSET(std::vector) res(internal_list);
 | 
|---|
| 150 |   return res;
 | 
|---|
| 151 | }
 | 
|---|
| 152 | 
 | 
|---|
| 153 | void Box::internal_explode(const Vector &point,int n) const{
 | 
|---|
| 154 |   internal_list.clear();
 | 
|---|
| 155 |   size_t list_index = 0;
 | 
|---|
| 156 | 
 | 
|---|
| 157 |   Vector translater = translateOut(point);
 | 
|---|
| 158 |   Vector mask; // contains the ignored coordinates
 | 
|---|
| 159 | 
 | 
|---|
| 160 |   // count the number of coordinates we need to do
 | 
|---|
| 161 |   int dims = 0; // number of dimensions that are not ignored
 | 
|---|
| 162 |   coords.clear();
 | 
|---|
| 163 |   index.clear();
 | 
|---|
| 164 |   for(int i=0;i<NDIM;++i){
 | 
|---|
| 165 |     if(conditions[i]==Ignore){
 | 
|---|
| 166 |       mask[i]=translater[i];
 | 
|---|
| 167 |       continue;
 | 
|---|
| 168 |     }
 | 
|---|
| 169 |     coords.push_back(i);
 | 
|---|
| 170 |     index.push_back(-n);
 | 
|---|
| 171 |     dims++;
 | 
|---|
| 172 |   } // there are max vectors in total we need to create
 | 
|---|
| 173 |   internal_list.resize(pow(2*n+1, dims));
 | 
|---|
| 174 | 
 | 
|---|
| 175 |   if(!dims){
 | 
|---|
| 176 |     // all boundaries are ignored
 | 
|---|
| 177 |     internal_list[list_index++] = point;
 | 
|---|
| 178 |     return;
 | 
|---|
| 179 |   }
 | 
|---|
| 180 | 
 | 
|---|
| 181 |   bool done = false;
 | 
|---|
| 182 |   while(!done){
 | 
|---|
| 183 |     // create this vector
 | 
|---|
| 184 |     Vector helper;
 | 
|---|
| 185 |     for(int i=0;i<dims;++i){
 | 
|---|
| 186 |       switch(conditions[coords[i]]){
 | 
|---|
| 187 |         case Wrap:
 | 
|---|
| 188 |           helper[coords[i]] = index[i]+translater[coords[i]];
 | 
|---|
| 189 |           break;
 | 
|---|
| 190 |         case Bounce:
 | 
|---|
| 191 |           {
 | 
|---|
| 192 |             // Bouncing the coordinate x produces the series:
 | 
|---|
| 193 |             // 0 -> x
 | 
|---|
| 194 |             // 1 -> 2-x
 | 
|---|
| 195 |             // 2 -> 2+x
 | 
|---|
| 196 |             // 3 -> 4-x
 | 
|---|
| 197 |             // 4 -> 4+x
 | 
|---|
| 198 |             // the first number is the next bigger even number (n+n%2)
 | 
|---|
| 199 |             // the next number is the value with alternating sign (x-2*(n%2)*x)
 | 
|---|
| 200 |             // the negative numbers produce the same sequence reversed and shifted
 | 
|---|
| 201 |             int n = abs(index[i]) + ((index[i]<0)?-1:0);
 | 
|---|
| 202 |             int sign = (index[i]<0)?-1:+1;
 | 
|---|
| 203 |             int even = n%2;
 | 
|---|
| 204 |             helper[coords[i]]=n+even+translater[coords[i]]-2*even*translater[coords[i]];
 | 
|---|
| 205 |             helper[coords[i]]*=sign;
 | 
|---|
| 206 |           }
 | 
|---|
| 207 |           break;
 | 
|---|
| 208 |         case Ignore:
 | 
|---|
| 209 |           ASSERT(0,"Ignored coordinate handled in generation loop");
 | 
|---|
| 210 |           break;
 | 
|---|
| 211 |         default:
 | 
|---|
| 212 |           ASSERT(0,"No default case for this switch-case");
 | 
|---|
| 213 |           break;
 | 
|---|
| 214 |       }
 | 
|---|
| 215 | 
 | 
|---|
| 216 |     }
 | 
|---|
| 217 |     // add back all ignored coordinates (not handled in above loop)
 | 
|---|
| 218 |     helper+=mask;
 | 
|---|
| 219 |     ASSERT(list_index < internal_list.size(),
 | 
|---|
| 220 |         "Box::internal_explode() - we have estimated the number of vectors wrong: "
 | 
|---|
| 221 |         +toString(list_index) +" >= "+toString(internal_list.size())+".");
 | 
|---|
| 222 |     internal_list[list_index++] = translateIn(helper);
 | 
|---|
| 223 |     // set the new indexes
 | 
|---|
| 224 |     int pos=0;
 | 
|---|
| 225 |     ++index[pos];
 | 
|---|
| 226 |     while(index[pos]>n){
 | 
|---|
| 227 |       index[pos++]=-n;
 | 
|---|
| 228 |       if(pos>=dims) { // it's trying to increase one beyond array... all vectors generated
 | 
|---|
| 229 |         done = true;
 | 
|---|
| 230 |         break;
 | 
|---|
| 231 |       }
 | 
|---|
| 232 |       ++index[pos];
 | 
|---|
| 233 |     }
 | 
|---|
| 234 |   }
 | 
|---|
| 235 | }
 | 
|---|
| 236 | 
 | 
|---|
| 237 | VECTORSET(std::vector) Box::explode(const Vector &point) const{
 | 
|---|
| 238 |   ASSERT(isInside(point),"Exploded point not inside Box");
 | 
|---|
| 239 |   return explode(point,1);
 | 
|---|
| 240 | }
 | 
|---|
| 241 | 
 | 
|---|
| 242 | double Box::periodicDistanceSquared(const Vector &point1,const Vector &point2) const{
 | 
|---|
| 243 |   Vector helper1(!isInside(point1) ? WrapPeriodically(point1) : point1);
 | 
|---|
| 244 |   Vector helper2(!isInside(point2) ? WrapPeriodically(point2) : point2);
 | 
|---|
| 245 |   internal_explode(helper1,1);
 | 
|---|
| 246 |   double res = internal_list.minDistSquared(helper2);
 | 
|---|
| 247 |   return res;
 | 
|---|
| 248 | }
 | 
|---|
| 249 | 
 | 
|---|
| 250 | double Box::periodicDistance(const Vector &point1,const Vector &point2) const{
 | 
|---|
| 251 |   double res;
 | 
|---|
| 252 |   res = sqrt(periodicDistanceSquared(point1,point2));
 | 
|---|
| 253 |   return res;
 | 
|---|
| 254 | }
 | 
|---|
| 255 | 
 | 
|---|
| 256 | double Box::DistanceToBoundary(const Vector &point) const
 | 
|---|
| 257 | {
 | 
|---|
| 258 |   std::map<double, Plane> DistanceSet;
 | 
|---|
| 259 |   std::vector<std::pair<Plane,Plane> > Boundaries = getBoundingPlanes();
 | 
|---|
| 260 |   for (int i=0;i<NDIM;++i) {
 | 
|---|
| 261 |     const double tempres1 = Boundaries[i].first.distance(point);
 | 
|---|
| 262 |     const double tempres2 = Boundaries[i].second.distance(point);
 | 
|---|
| 263 |     DistanceSet.insert( make_pair(tempres1, Boundaries[i].first) );
 | 
|---|
| 264 |     LOG(1, "Inserting distance " << tempres1 << " and " << tempres2 << ".");
 | 
|---|
| 265 |     DistanceSet.insert( make_pair(tempres2, Boundaries[i].second) );
 | 
|---|
| 266 |   }
 | 
|---|
| 267 |   ASSERT(!DistanceSet.empty(), "Box::DistanceToBoundary() - no distances in map!");
 | 
|---|
| 268 |   return (DistanceSet.begin())->first;
 | 
|---|
| 269 | }
 | 
|---|
| 270 | 
 | 
|---|
| 271 | Shape Box::getShape() const{
 | 
|---|
| 272 |   return transform(Cuboid(Vector(0,0,0),Vector(1,1,1)),(*M));
 | 
|---|
| 273 | }
 | 
|---|
| 274 | 
 | 
|---|
| 275 | const Box::Conditions_t Box::getConditions() const
 | 
|---|
| 276 | {
 | 
|---|
| 277 |   return conditions;
 | 
|---|
| 278 | }
 | 
|---|
| 279 | 
 | 
|---|
| 280 | void Box::setCondition(int i,Box::BoundaryCondition_t condition){
 | 
|---|
| 281 |   conditions[i]=condition;
 | 
|---|
| 282 | }
 | 
|---|
| 283 | 
 | 
|---|
| 284 | const std::vector<std::pair<Plane,Plane> >  Box::getBoundingPlanes() const
 | 
|---|
| 285 | {
 | 
|---|
| 286 |   std::vector<std::pair<Plane,Plane> > res;
 | 
|---|
| 287 |   for(int i=0;i<NDIM;++i){
 | 
|---|
| 288 |     Vector base1,base2,base3;
 | 
|---|
| 289 |     base2[(i+1)%NDIM] = 1.;
 | 
|---|
| 290 |     base3[(i+2)%NDIM] = 1.;
 | 
|---|
| 291 |     Plane p1(translateIn(base1),
 | 
|---|
| 292 |              translateIn(base2),
 | 
|---|
| 293 |              translateIn(base3));
 | 
|---|
| 294 |     Vector offset;
 | 
|---|
| 295 |     offset[i]=1;
 | 
|---|
| 296 |     Plane p2(translateIn(base1+offset),
 | 
|---|
| 297 |              translateIn(base2+offset),
 | 
|---|
| 298 |              translateIn(base3+offset));
 | 
|---|
| 299 |     res.push_back(make_pair(p1,p2));
 | 
|---|
| 300 |   }
 | 
|---|
| 301 |   ASSERT(res.size() == 3, "Box::getBoundingPlanes() - does not have three plane pairs!");
 | 
|---|
| 302 |   return res;
 | 
|---|
| 303 | }
 | 
|---|
| 304 | 
 | 
|---|
| 305 | void Box::setCuboid(const Vector &endpoint){
 | 
|---|
| 306 |   ASSERT(endpoint[0]>0 && endpoint[1]>0 && endpoint[2]>0,"Vector does not define a full cuboid");
 | 
|---|
| 307 |   M->setIdentity();
 | 
|---|
| 308 |   M->diagonal()=endpoint;
 | 
|---|
| 309 |   Vector &dinv = Minv->diagonal();
 | 
|---|
| 310 |   for(int i=NDIM;i--;)
 | 
|---|
| 311 |     dinv[i]=1/endpoint[i];
 | 
|---|
| 312 | }
 | 
|---|
| 313 | 
 | 
|---|
| 314 | Box &Box::operator=(const Box &src){
 | 
|---|
| 315 |   if(&src!=this){
 | 
|---|
| 316 |     delete M;
 | 
|---|
| 317 |     delete Minv;
 | 
|---|
| 318 |     M    = new RealSpaceMatrix(*src.M);
 | 
|---|
| 319 |     Minv = new RealSpaceMatrix(*src.Minv);
 | 
|---|
| 320 |     conditions = src.conditions;
 | 
|---|
| 321 |   }
 | 
|---|
| 322 |   return *this;
 | 
|---|
| 323 | }
 | 
|---|
| 324 | 
 | 
|---|
| 325 | Box &Box::operator=(const RealSpaceMatrix &mat){
 | 
|---|
| 326 |   setM(mat);
 | 
|---|
| 327 |   return *this;
 | 
|---|
| 328 | }
 | 
|---|
| 329 | 
 | 
|---|
| 330 | std::ostream & operator << (std::ostream& ost, const Box &m)
 | 
|---|
| 331 | {
 | 
|---|
| 332 |   ost << m.getM();
 | 
|---|
| 333 |   return ost;
 | 
|---|
| 334 | }
 | 
|---|