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