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