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