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