| [bcf653] | 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 | 
 | 
|---|
| [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 | 
 | 
|---|
| [bbbad5] | 20 | #include "Helpers/MemDebug.hpp"
 | 
|---|
| [6e00dd] | 21 | 
 | 
|---|
| [83c09a] | 22 | #include "Box.hpp"
 | 
|---|
 | 23 | 
 | 
|---|
| [f429d7] | 24 | #include <cmath>
 | 
|---|
| [16648f] | 25 | #include <iostream>
 | 
|---|
| [d2938f] | 26 | #include <cstdlib>
 | 
|---|
| [f429d7] | 27 | 
 | 
|---|
| [57f243] | 28 | #include "LinearAlgebra/Matrix.hpp"
 | 
|---|
 | 29 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
 | 30 | #include "LinearAlgebra/Plane.hpp"
 | 
|---|
| [83c09a] | 31 | 
 | 
|---|
| [6e00dd] | 32 | #include "Helpers/Assert.hpp"
 | 
|---|
 | 33 | 
 | 
|---|
| [16648f] | 34 | using namespace std;
 | 
|---|
 | 35 | 
 | 
|---|
| [83c09a] | 36 | Box::Box()
 | 
|---|
 | 37 | {
 | 
|---|
 | 38 |   M= new Matrix();
 | 
|---|
 | 39 |   M->one();
 | 
|---|
 | 40 |   Minv = new Matrix();
 | 
|---|
 | 41 |   Minv->one();
 | 
|---|
| [77374e] | 42 |   conditions.resize(3);
 | 
|---|
 | 43 |   conditions[0] = conditions[1] = conditions[2] = Wrap;
 | 
|---|
| [83c09a] | 44 | }
 | 
|---|
 | 45 | 
 | 
|---|
| [7579a4b] | 46 | Box::Box(const Box& src){
 | 
|---|
 | 47 |   M=new Matrix(*src.M);
 | 
|---|
 | 48 |   Minv = new Matrix(*src.Minv);
 | 
|---|
| [77374e] | 49 |   conditions = src.conditions;
 | 
|---|
| [7579a4b] | 50 | }
 | 
|---|
 | 51 | 
 | 
|---|
| [83c09a] | 52 | Box::~Box()
 | 
|---|
 | 53 | {
 | 
|---|
 | 54 |   delete M;
 | 
|---|
 | 55 |   delete Minv;
 | 
|---|
 | 56 | }
 | 
|---|
 | 57 | 
 | 
|---|
| [7579a4b] | 58 | const Matrix &Box::getM() const{
 | 
|---|
| [83c09a] | 59 |   return *M;
 | 
|---|
 | 60 | }
 | 
|---|
| [7579a4b] | 61 | const Matrix &Box::getMinv() const{
 | 
|---|
| [83c09a] | 62 |   return *Minv;
 | 
|---|
 | 63 | }
 | 
|---|
 | 64 | 
 | 
|---|
 | 65 | void Box::setM(Matrix _M){
 | 
|---|
| [3bf9e2] | 66 |   ASSERT(_M.determinant()!=0,"Matrix in Box construction was not invertible");
 | 
|---|
| [83c09a] | 67 |   *M    =_M;
 | 
|---|
 | 68 |   *Minv = M->invert();
 | 
|---|
 | 69 | }
 | 
|---|
| [7579a4b] | 70 | 
 | 
|---|
| [014475] | 71 | Vector Box::translateIn(const Vector &point) const{
 | 
|---|
| [3dcb1f] | 72 |   return (*M) * point;
 | 
|---|
 | 73 | }
 | 
|---|
 | 74 | 
 | 
|---|
| [014475] | 75 | Vector Box::translateOut(const Vector &point) const{
 | 
|---|
| [3dcb1f] | 76 |   return (*Minv) * point;
 | 
|---|
 | 77 | }
 | 
|---|
 | 78 | 
 | 
|---|
| [014475] | 79 | Vector Box::WrapPeriodically(const Vector &point) const{
 | 
|---|
| [f429d7] | 80 |   Vector helper = translateOut(point);
 | 
|---|
 | 81 |   for(int i=NDIM;i--;){
 | 
|---|
| [c72562] | 82 | 
 | 
|---|
 | 83 |     switch(conditions[i]){
 | 
|---|
 | 84 |     case Wrap:
 | 
|---|
 | 85 |       helper.at(i)=fmod(helper.at(i),1);
 | 
|---|
| [d2938f] | 86 |       helper.at(i)+=(helper.at(i)>=0)?0:1;
 | 
|---|
| [c72562] | 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 | 
 | 
|---|
| [f429d7] | 103 |   }
 | 
|---|
 | 104 |   return translateIn(helper);
 | 
|---|
 | 105 | }
 | 
|---|
 | 106 | 
 | 
|---|
| [0ff6b5] | 107 | bool Box::isInside(const Vector &point) const
 | 
|---|
 | 108 | {
 | 
|---|
 | 109 |   bool result = true;
 | 
|---|
| [29ac78] | 110 |   Vector tester = translateOut(point);
 | 
|---|
| [0ff6b5] | 111 | 
 | 
|---|
 | 112 |   for(int i=0;i<NDIM;i++)
 | 
|---|
| [f3be87] | 113 |     result = result &&
 | 
|---|
 | 114 |               ((conditions[i] == Ignore) ||
 | 
|---|
 | 115 |                ((tester[i] >= -MYEPSILON) &&
 | 
|---|
 | 116 |                ((tester[i] - 1.) < MYEPSILON)));
 | 
|---|
| [0ff6b5] | 117 | 
 | 
|---|
 | 118 |   return result;
 | 
|---|
 | 119 | }
 | 
|---|
 | 120 | 
 | 
|---|
 | 121 | 
 | 
|---|
| [a630fd] | 122 | VECTORSET(std::list) Box::explode(const Vector &point,int n) const{
 | 
|---|
| [16648f] | 123 |   ASSERT(isInside(point),"Exploded point not inside Box");
 | 
|---|
| [12cf773] | 124 |   VECTORSET(std::list) res;
 | 
|---|
| [89e820] | 125 | 
 | 
|---|
| [16648f] | 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;
 | 
|---|
| [89e820] | 147 |   }
 | 
|---|
| [7ac4af] | 148 | 
 | 
|---|
| [d2938f] | 149 |   bool done = false;
 | 
|---|
 | 150 |   while(!done){
 | 
|---|
| [16648f] | 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
 | 
|---|
| [d2938f] | 169 |             int n = abs(index[i]) + ((index[i]<0)?-1:0);
 | 
|---|
| [16648f] | 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");
 | 
|---|
| [7ac4af] | 180 |       }
 | 
|---|
| [16648f] | 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
 | 
|---|
| [d2938f] | 187 |     int pos=0;
 | 
|---|
| [16648f] | 188 |     ++index[pos];
 | 
|---|
| [d2938f] | 189 |     while(index[pos]>n){
 | 
|---|
| [16648f] | 190 |       index[pos++]=-n;
 | 
|---|
| [d2938f] | 191 |       if(pos>=dims) { // it's trying to increase one beyond array... all vectors generated
 | 
|---|
 | 192 |         done = true;
 | 
|---|
 | 193 |         break;
 | 
|---|
 | 194 |       }
 | 
|---|
| [16648f] | 195 |       ++index[pos];
 | 
|---|
| [7ac4af] | 196 |     }
 | 
|---|
 | 197 |   }
 | 
|---|
 | 198 |   return res;
 | 
|---|
 | 199 | }
 | 
|---|
 | 200 | 
 | 
|---|
| [16648f] | 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 | 
 | 
|---|
| [014475] | 206 | double Box::periodicDistanceSquared(const Vector &point1,const Vector &point2) const{
 | 
|---|
| [f1c838] | 207 |   Vector helper1 = WrapPeriodically(point1);
 | 
|---|
 | 208 |   Vector helper2 = WrapPeriodically(point2);
 | 
|---|
 | 209 |   VECTORSET(std::list) expansion = explode(helper1);
 | 
|---|
 | 210 |   double res = expansion.minDistSquared(helper2);
 | 
|---|
| [014475] | 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;
 | 
|---|
| [527de2] | 218 | }
 | 
|---|
 | 219 | 
 | 
|---|
| [77374e] | 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 | 
 | 
|---|
| [29ac78] | 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 | 
 | 
|---|
| [e1ab97] | 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 | 
 | 
|---|
| [7579a4b] | 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);
 | 
|---|
| [77374e] | 262 |     conditions = src.conditions;
 | 
|---|
| [7579a4b] | 263 |   }
 | 
|---|
 | 264 |   return *this;
 | 
|---|
 | 265 | }
 | 
|---|
 | 266 | 
 | 
|---|
 | 267 | Box &Box::operator=(const Matrix &mat){
 | 
|---|
 | 268 |   setM(mat);
 | 
|---|
 | 269 |   return *this;
 | 
|---|
 | 270 | }
 | 
|---|