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