| 1 | /*
 | 
|---|
| 2 |  * Box.cpp
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *  Created on: Jun 30, 2010
 | 
|---|
| 5 |  *      Author: crueger
 | 
|---|
| 6 |  */
 | 
|---|
| 7 | 
 | 
|---|
| 8 | //#include "Helpers/MemDebug.hpp"
 | 
|---|
| 9 | 
 | 
|---|
| 10 | #include "Box.hpp"
 | 
|---|
| 11 | 
 | 
|---|
| 12 | #include <cmath>
 | 
|---|
| 13 | 
 | 
|---|
| 14 | #include "Matrix.hpp"
 | 
|---|
| 15 | #include "vector.hpp"
 | 
|---|
| 16 | #include "Plane.hpp"
 | 
|---|
| 17 | 
 | 
|---|
| 18 | #include "Helpers/Assert.hpp"
 | 
|---|
| 19 | 
 | 
|---|
| 20 | Box::Box()
 | 
|---|
| 21 | {
 | 
|---|
| 22 |   M= new Matrix();
 | 
|---|
| 23 |   M->one();
 | 
|---|
| 24 |   Minv = new Matrix();
 | 
|---|
| 25 |   Minv->one();
 | 
|---|
| 26 |   conditions.resize(3);
 | 
|---|
| 27 |   conditions[0] = conditions[1] = conditions[2] = Wrap;
 | 
|---|
| 28 | }
 | 
|---|
| 29 | 
 | 
|---|
| 30 | Box::Box(const Box& src){
 | 
|---|
| 31 |   M=new Matrix(*src.M);
 | 
|---|
| 32 |   Minv = new Matrix(*src.Minv);
 | 
|---|
| 33 |   conditions = src.conditions;
 | 
|---|
| 34 | }
 | 
|---|
| 35 | 
 | 
|---|
| 36 | Box::~Box()
 | 
|---|
| 37 | {
 | 
|---|
| 38 |   delete M;
 | 
|---|
| 39 |   delete Minv;
 | 
|---|
| 40 | }
 | 
|---|
| 41 | 
 | 
|---|
| 42 | const Matrix &Box::getM() const{
 | 
|---|
| 43 |   return *M;
 | 
|---|
| 44 | }
 | 
|---|
| 45 | const Matrix &Box::getMinv() const{
 | 
|---|
| 46 |   return *Minv;
 | 
|---|
| 47 | }
 | 
|---|
| 48 | 
 | 
|---|
| 49 | void Box::setM(Matrix _M){
 | 
|---|
| 50 |   ASSERT(_M.determinant()!=0,"Matrix in Box construction was not invertible");
 | 
|---|
| 51 |   *M    =_M;
 | 
|---|
| 52 |   *Minv = M->invert();
 | 
|---|
| 53 | }
 | 
|---|
| 54 | 
 | 
|---|
| 55 | Vector Box::translateIn(const Vector &point) const{
 | 
|---|
| 56 |   return (*M) * point;
 | 
|---|
| 57 | }
 | 
|---|
| 58 | 
 | 
|---|
| 59 | Vector Box::translateOut(const Vector &point) const{
 | 
|---|
| 60 |   return (*Minv) * point;
 | 
|---|
| 61 | }
 | 
|---|
| 62 | 
 | 
|---|
| 63 | Vector Box::WrapPeriodically(const Vector &point) const{
 | 
|---|
| 64 |   Vector helper = translateOut(point);
 | 
|---|
| 65 |   for(int i=NDIM;i--;){
 | 
|---|
| 66 | 
 | 
|---|
| 67 |     switch(conditions[i]){
 | 
|---|
| 68 |     case Wrap:
 | 
|---|
| 69 |       helper.at(i)=fmod(helper.at(i),1);
 | 
|---|
| 70 |       helper.at(i)+=(helper.at(i)>0)?0:1;
 | 
|---|
| 71 |       break;
 | 
|---|
| 72 |     case Bounce:
 | 
|---|
| 73 |       {
 | 
|---|
| 74 |         // there probably is a better way to handle this...
 | 
|---|
| 75 |         // all the fabs and fmod modf probably makes it very slow
 | 
|---|
| 76 |         double intpart,fracpart;
 | 
|---|
| 77 |         fracpart = modf(fabs(helper.at(i)),&intpart);
 | 
|---|
| 78 |         helper.at(i) = fabs(fracpart-fmod(intpart,2));
 | 
|---|
| 79 |       }
 | 
|---|
| 80 |       break;
 | 
|---|
| 81 |     case Ignore:
 | 
|---|
| 82 |       break;
 | 
|---|
| 83 |     default:
 | 
|---|
| 84 |       ASSERT(0,"No default case for this");
 | 
|---|
| 85 |     }
 | 
|---|
| 86 | 
 | 
|---|
| 87 |   }
 | 
|---|
| 88 |   return translateIn(helper);
 | 
|---|
| 89 | }
 | 
|---|
| 90 | 
 | 
|---|
| 91 | bool Box::isInside(const Vector &point) const
 | 
|---|
| 92 | {
 | 
|---|
| 93 |   bool result = true;
 | 
|---|
| 94 |   Vector tester = translateOut(point);
 | 
|---|
| 95 | 
 | 
|---|
| 96 |   for(int i=0;i<NDIM;i++)
 | 
|---|
| 97 |     result = result && ((tester[i] >= -MYEPSILON) && ((tester[i] - 1.) < MYEPSILON));
 | 
|---|
| 98 | 
 | 
|---|
| 99 |   return result;
 | 
|---|
| 100 | }
 | 
|---|
| 101 | 
 | 
|---|
| 102 | 
 | 
|---|
| 103 | VECTORSET(std::list) Box::explode(const Vector &point,int n) const{
 | 
|---|
| 104 |   VECTORSET(std::list) res;
 | 
|---|
| 105 | 
 | 
|---|
| 106 |   // translate the Vector into each of the 27 neighbourhoods
 | 
|---|
| 107 | 
 | 
|---|
| 108 |   // first create all translation Vectors
 | 
|---|
| 109 |   // there are (n*2+1)^3 such vectors
 | 
|---|
| 110 |   int max_dim = (n*2+1);
 | 
|---|
| 111 |   int max_dim2 = max_dim*max_dim;
 | 
|---|
| 112 |   int max = max_dim2*max_dim;
 | 
|---|
| 113 |   // only one loop to avoid unneccessary jumps
 | 
|---|
| 114 |   for(int i = 0;i<max;++i){
 | 
|---|
| 115 |     // get all coordinates for this iteration
 | 
|---|
| 116 |     int n1 = (i%max_dim)-n;
 | 
|---|
| 117 |     int n2 = ((i/max_dim)%max_dim)-n;
 | 
|---|
| 118 |     int n3 = ((i/max_dim2))-n;
 | 
|---|
| 119 |     Vector translation = translateIn(Vector(n1,n2,n3));
 | 
|---|
| 120 |     res.push_back(translation);
 | 
|---|
| 121 |   }
 | 
|---|
| 122 |   // translate all the translation vector by the offset defined by point
 | 
|---|
| 123 |   res.translate(point);
 | 
|---|
| 124 |   return res;
 | 
|---|
| 125 | }
 | 
|---|
| 126 | 
 | 
|---|
| 127 | VECTORSET(std::list) Box::explode(const Vector &point) const{
 | 
|---|
| 128 |   VECTORSET(std::list) res;
 | 
|---|
| 129 | 
 | 
|---|
| 130 |   // translate the Vector into each of the 27 neighbourhoods
 | 
|---|
| 131 | 
 | 
|---|
| 132 |   // first create all 27 translation Vectors
 | 
|---|
| 133 |   // these loops depend on fixed parameters and can easily be expanded
 | 
|---|
| 134 |   // by the compiler to allow code without jumps
 | 
|---|
| 135 |   for(int n1 = -1;n1<=1;++n1){
 | 
|---|
| 136 |     for(int n2 = -1;n2<=1;++n2){
 | 
|---|
| 137 |       for(int n3 = -1;n3<=1;++n3){
 | 
|---|
| 138 |         // get all coordinates for this iteration
 | 
|---|
| 139 |         Vector translation = translateIn(Vector(n1,n2,n3));
 | 
|---|
| 140 |         res.push_back(translation);
 | 
|---|
| 141 |       }
 | 
|---|
| 142 |     }
 | 
|---|
| 143 |   }
 | 
|---|
| 144 |   // translate all the translation vector by the offset defined by point
 | 
|---|
| 145 |   res.translate(point);
 | 
|---|
| 146 |   return res;
 | 
|---|
| 147 | }
 | 
|---|
| 148 | 
 | 
|---|
| 149 | double Box::periodicDistanceSquared(const Vector &point1,const Vector &point2) const{
 | 
|---|
| 150 |   Vector helper1 = WrapPeriodically(point1);
 | 
|---|
| 151 |   Vector helper2 = WrapPeriodically(point2);
 | 
|---|
| 152 |   VECTORSET(std::list) expansion = explode(helper1);
 | 
|---|
| 153 |   double res = expansion.minDistSquared(helper2);
 | 
|---|
| 154 |   return res;
 | 
|---|
| 155 | }
 | 
|---|
| 156 | 
 | 
|---|
| 157 | double Box::periodicDistance(const Vector &point1,const Vector &point2) const{
 | 
|---|
| 158 |   double res;
 | 
|---|
| 159 |   res = sqrt(periodicDistanceSquared(point1,point2));
 | 
|---|
| 160 |   return res;
 | 
|---|
| 161 | }
 | 
|---|
| 162 | 
 | 
|---|
| 163 | const Box::Conditions_t Box::getConditions(){
 | 
|---|
| 164 |   return conditions;
 | 
|---|
| 165 | }
 | 
|---|
| 166 | 
 | 
|---|
| 167 | void Box::setCondition(int i,Box::BoundaryCondition_t condition){
 | 
|---|
| 168 |   conditions[i]=condition;
 | 
|---|
| 169 | }
 | 
|---|
| 170 | 
 | 
|---|
| 171 | const vector<pair<Plane,Plane> >  Box::getBoundingPlanes(){
 | 
|---|
| 172 |   vector<pair<Plane,Plane> > res;
 | 
|---|
| 173 |   for(int i=0;i<NDIM;++i){
 | 
|---|
| 174 |     Vector base1,base2,base3;
 | 
|---|
| 175 |     base2[(i+1)%NDIM] = 1.;
 | 
|---|
| 176 |     base3[(i+2)%NDIM] = 1.;
 | 
|---|
| 177 |     Plane p1(translateIn(base1),
 | 
|---|
| 178 |              translateIn(base2),
 | 
|---|
| 179 |              translateIn(base3));
 | 
|---|
| 180 |     Vector offset;
 | 
|---|
| 181 |     offset[i]=1;
 | 
|---|
| 182 |     Plane p2(translateIn(base1+offset),
 | 
|---|
| 183 |              translateIn(base2+offset),
 | 
|---|
| 184 |              translateIn(base3+offset));
 | 
|---|
| 185 |     res.push_back(make_pair(p1,p2));
 | 
|---|
| 186 |   }
 | 
|---|
| 187 |   return res;
 | 
|---|
| 188 | }
 | 
|---|
| 189 | 
 | 
|---|
| 190 | Box &Box::operator=(const Box &src){
 | 
|---|
| 191 |   if(&src!=this){
 | 
|---|
| 192 |     delete M;
 | 
|---|
| 193 |     delete Minv;
 | 
|---|
| 194 |     M    = new Matrix(*src.M);
 | 
|---|
| 195 |     Minv = new Matrix(*src.Minv);
 | 
|---|
| 196 |     conditions = src.conditions;
 | 
|---|
| 197 |   }
 | 
|---|
| 198 |   return *this;
 | 
|---|
| 199 | }
 | 
|---|
| 200 | 
 | 
|---|
| 201 | Box &Box::operator=(const Matrix &mat){
 | 
|---|
| 202 |   setM(mat);
 | 
|---|
| 203 |   return *this;
 | 
|---|
| 204 | }
 | 
|---|