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