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