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