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