[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 |
|
---|
[ad011c] | 20 | #include "CodePatterns/MemDebug.hpp"
|
---|
[6e00dd] | 21 |
|
---|
[83c09a] | 22 | #include "Box.hpp"
|
---|
| 23 |
|
---|
[f429d7] | 24 | #include <cmath>
|
---|
[16648f] | 25 | #include <iostream>
|
---|
[d2938f] | 26 | #include <cstdlib>
|
---|
[f429d7] | 27 |
|
---|
[06aedc] | 28 | #include "CodePatterns/Assert.hpp"
|
---|
| 29 | #include "CodePatterns/Log.hpp"
|
---|
| 30 | #include "CodePatterns/Verbose.hpp"
|
---|
| 31 | #include "Helpers/defs.hpp"
|
---|
[cca9ef] | 32 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
|
---|
[57f243] | 33 | #include "LinearAlgebra/Vector.hpp"
|
---|
| 34 | #include "LinearAlgebra/Plane.hpp"
|
---|
[c538d1] | 35 | #include "Shapes/BaseShapes.hpp"
|
---|
| 36 | #include "Shapes/ShapeOps.hpp"
|
---|
[83c09a] | 37 |
|
---|
[6e00dd] | 38 |
|
---|
[16648f] | 39 | using namespace std;
|
---|
| 40 |
|
---|
[025048] | 41 | VECTORSET(std::list) Box::internal_list;
|
---|
| 42 |
|
---|
[528b3e] | 43 | Box::Box() :
|
---|
| 44 | M(new RealSpaceMatrix()),
|
---|
| 45 | Minv(new RealSpaceMatrix())
|
---|
[83c09a] | 46 | {
|
---|
[9eb7580] | 47 | M->setIdentity();
|
---|
| 48 | Minv->setIdentity();
|
---|
[77374e] | 49 | conditions.resize(3);
|
---|
| 50 | conditions[0] = conditions[1] = conditions[2] = Wrap;
|
---|
[83c09a] | 51 | }
|
---|
| 52 |
|
---|
[528b3e] | 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();
|
---|
[7579a4b] | 65 | }
|
---|
| 66 |
|
---|
[83c09a] | 67 | Box::~Box()
|
---|
| 68 | {
|
---|
| 69 | delete M;
|
---|
| 70 | delete Minv;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
[cca9ef] | 73 | const RealSpaceMatrix &Box::getM() const{
|
---|
[83c09a] | 74 | return *M;
|
---|
| 75 | }
|
---|
[cca9ef] | 76 | const RealSpaceMatrix &Box::getMinv() const{
|
---|
[83c09a] | 77 | return *Minv;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
[cca9ef] | 80 | void Box::setM(RealSpaceMatrix _M){
|
---|
[3bf9e2] | 81 | ASSERT(_M.determinant()!=0,"Matrix in Box construction was not invertible");
|
---|
[83c09a] | 82 | *M =_M;
|
---|
| 83 | *Minv = M->invert();
|
---|
| 84 | }
|
---|
[7579a4b] | 85 |
|
---|
[014475] | 86 | Vector Box::translateIn(const Vector &point) const{
|
---|
[3dcb1f] | 87 | return (*M) * point;
|
---|
| 88 | }
|
---|
| 89 |
|
---|
[014475] | 90 | Vector Box::translateOut(const Vector &point) const{
|
---|
[3dcb1f] | 91 | return (*Minv) * point;
|
---|
| 92 | }
|
---|
| 93 |
|
---|
[014475] | 94 | Vector Box::WrapPeriodically(const Vector &point) const{
|
---|
[f429d7] | 95 | Vector helper = translateOut(point);
|
---|
| 96 | for(int i=NDIM;i--;){
|
---|
[c72562] | 97 |
|
---|
| 98 | switch(conditions[i]){
|
---|
| 99 | case Wrap:
|
---|
| 100 | helper.at(i)=fmod(helper.at(i),1);
|
---|
[d2938f] | 101 | helper.at(i)+=(helper.at(i)>=0)?0:1;
|
---|
[c72562] | 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 |
|
---|
[f429d7] | 118 | }
|
---|
| 119 | return translateIn(helper);
|
---|
| 120 | }
|
---|
| 121 |
|
---|
[0ff6b5] | 122 | bool Box::isInside(const Vector &point) const
|
---|
| 123 | {
|
---|
| 124 | bool result = true;
|
---|
[29ac78] | 125 | Vector tester = translateOut(point);
|
---|
[0ff6b5] | 126 |
|
---|
| 127 | for(int i=0;i<NDIM;i++)
|
---|
[f3be87] | 128 | result = result &&
|
---|
| 129 | ((conditions[i] == Ignore) ||
|
---|
| 130 | ((tester[i] >= -MYEPSILON) &&
|
---|
| 131 | ((tester[i] - 1.) < MYEPSILON)));
|
---|
[0ff6b5] | 132 |
|
---|
| 133 | return result;
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 |
|
---|
[a630fd] | 137 | VECTORSET(std::list) Box::explode(const Vector &point,int n) const{
|
---|
[16648f] | 138 | ASSERT(isInside(point),"Exploded point not inside Box");
|
---|
[025048] | 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();
|
---|
[89e820] | 146 |
|
---|
[16648f] | 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
|
---|
[025048] | 166 | internal_list.push_back(point);
|
---|
| 167 | return;
|
---|
[89e820] | 168 | }
|
---|
| 169 |
|
---|
[d2938f] | 170 | bool done = false;
|
---|
| 171 | while(!done){
|
---|
[16648f] | 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
|
---|
[d2938f] | 190 | int n = abs(index[i]) + ((index[i]<0)?-1:0);
|
---|
[16648f] | 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");
|
---|
[025048] | 199 | break;
|
---|
[16648f] | 200 | default:
|
---|
| 201 | ASSERT(0,"No default case for this switch-case");
|
---|
[025048] | 202 | break;
|
---|
[7ac4af] | 203 | }
|
---|
| 204 |
|
---|
[16648f] | 205 | }
|
---|
| 206 | // add back all ignored coordinates (not handled in above loop)
|
---|
| 207 | helper+=mask;
|
---|
[025048] | 208 | internal_list.push_back(translateIn(helper));
|
---|
[16648f] | 209 | // set the new indexes
|
---|
[d2938f] | 210 | int pos=0;
|
---|
[16648f] | 211 | ++index[pos];
|
---|
[d2938f] | 212 | while(index[pos]>n){
|
---|
[16648f] | 213 | index[pos++]=-n;
|
---|
[d2938f] | 214 | if(pos>=dims) { // it's trying to increase one beyond array... all vectors generated
|
---|
| 215 | done = true;
|
---|
| 216 | break;
|
---|
[7ac4af] | 217 | }
|
---|
[16648f] | 218 | ++index[pos];
|
---|
[7ac4af] | 219 | }
|
---|
| 220 | }
|
---|
| 221 | }
|
---|
| 222 |
|
---|
[16648f] | 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 |
|
---|
[014475] | 228 | double Box::periodicDistanceSquared(const Vector &point1,const Vector &point2) const{
|
---|
[025048] | 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);
|
---|
[014475] | 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;
|
---|
[527de2] | 240 | }
|
---|
| 241 |
|
---|
[66fd49] | 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) );
|
---|
[47d041] | 250 | LOG(1, "Inserting distance " << tempres1 << " and " << tempres2 << ".");
|
---|
[66fd49] | 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 |
|
---|
[c538d1] | 257 | Shape Box::getShape() const{
|
---|
| 258 | return transform(Cuboid(Vector(0,0,0),Vector(1,1,1)),(*M));
|
---|
[6c438f] | 259 | }
|
---|
| 260 |
|
---|
[66fd49] | 261 | const Box::Conditions_t Box::getConditions() const
|
---|
| 262 | {
|
---|
[77374e] | 263 | return conditions;
|
---|
| 264 | }
|
---|
[c538d1] | 265 |
|
---|
[77374e] | 266 | void Box::setCondition(int i,Box::BoundaryCondition_t condition){
|
---|
| 267 | conditions[i]=condition;
|
---|
| 268 | }
|
---|
| 269 |
|
---|
[66fd49] | 270 | const vector<pair<Plane,Plane> > Box::getBoundingPlanes() const
|
---|
| 271 | {
|
---|
[29ac78] | 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 | }
|
---|
[66fd49] | 287 | ASSERT(res.size() == 3, "Box::getBoundingPlanes() - does not have three plane pairs!");
|
---|
[29ac78] | 288 | return res;
|
---|
| 289 | }
|
---|
| 290 |
|
---|
[e1ab97] | 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");
|
---|
[9eb7580] | 293 | M->setIdentity();
|
---|
[e1ab97] | 294 | M->diagonal()=endpoint;
|
---|
| 295 | Vector &dinv = Minv->diagonal();
|
---|
| 296 | for(int i=NDIM;i--;)
|
---|
| 297 | dinv[i]=1/endpoint[i];
|
---|
[c538d1] | 298 | }
|
---|
| 299 |
|
---|
[7579a4b] | 300 | Box &Box::operator=(const Box &src){
|
---|
| 301 | if(&src!=this){
|
---|
| 302 | delete M;
|
---|
| 303 | delete Minv;
|
---|
[cca9ef] | 304 | M = new RealSpaceMatrix(*src.M);
|
---|
| 305 | Minv = new RealSpaceMatrix(*src.Minv);
|
---|
[77374e] | 306 | conditions = src.conditions;
|
---|
[7579a4b] | 307 | }
|
---|
| 308 | return *this;
|
---|
| 309 | }
|
---|
| 310 |
|
---|
[cca9ef] | 311 | Box &Box::operator=(const RealSpaceMatrix &mat){
|
---|
[7579a4b] | 312 | setM(mat);
|
---|
| 313 | return *this;
|
---|
| 314 | }
|
---|
[528b3e] | 315 |
|
---|
| 316 | ostream & operator << (ostream& ost, const Box &m)
|
---|
| 317 | {
|
---|
| 318 | ost << m.getM();
|
---|
| 319 | return ost;
|
---|
| 320 | }
|
---|