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