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