| [83c09a] | 1 | /*
|
|---|
| 2 | * Box.cpp
|
|---|
| 3 | *
|
|---|
| 4 | * Created on: Jun 30, 2010
|
|---|
| 5 | * Author: crueger
|
|---|
| 6 | */
|
|---|
| 7 |
|
|---|
| [527de2] | 8 | //#include "Helpers/MemDebug.hpp"
|
|---|
| [6e00dd] | 9 |
|
|---|
| [83c09a] | 10 | #include "Box.hpp"
|
|---|
| 11 |
|
|---|
| [f429d7] | 12 | #include <cmath>
|
|---|
| 13 |
|
|---|
| [83c09a] | 14 | #include "Matrix.hpp"
|
|---|
| [3dcb1f] | 15 | #include "vector.hpp"
|
|---|
| [83c09a] | 16 |
|
|---|
| [6e00dd] | 17 | #include "Helpers/Assert.hpp"
|
|---|
| 18 |
|
|---|
| [83c09a] | 19 | Box::Box()
|
|---|
| 20 | {
|
|---|
| 21 | M= new Matrix();
|
|---|
| 22 | M->one();
|
|---|
| 23 | Minv = new Matrix();
|
|---|
| 24 | Minv->one();
|
|---|
| [77374e] | 25 | conditions.resize(3);
|
|---|
| 26 | conditions[0] = conditions[1] = conditions[2] = Wrap;
|
|---|
| [83c09a] | 27 | }
|
|---|
| 28 |
|
|---|
| [7579a4b] | 29 | Box::Box(const Box& src){
|
|---|
| 30 | M=new Matrix(*src.M);
|
|---|
| 31 | Minv = new Matrix(*src.Minv);
|
|---|
| [77374e] | 32 | conditions = src.conditions;
|
|---|
| [7579a4b] | 33 | }
|
|---|
| 34 |
|
|---|
| [83c09a] | 35 | Box::~Box()
|
|---|
| 36 | {
|
|---|
| 37 | delete M;
|
|---|
| 38 | delete Minv;
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| [7579a4b] | 41 | const Matrix &Box::getM() const{
|
|---|
| [83c09a] | 42 | return *M;
|
|---|
| 43 | }
|
|---|
| [7579a4b] | 44 | const Matrix &Box::getMinv() const{
|
|---|
| [83c09a] | 45 | return *Minv;
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | void Box::setM(Matrix _M){
|
|---|
| [3bf9e2] | 49 | ASSERT(_M.determinant()!=0,"Matrix in Box construction was not invertible");
|
|---|
| [83c09a] | 50 | *M =_M;
|
|---|
| 51 | *Minv = M->invert();
|
|---|
| 52 | }
|
|---|
| [7579a4b] | 53 |
|
|---|
| [014475] | 54 | Vector Box::translateIn(const Vector &point) const{
|
|---|
| [3dcb1f] | 55 | return (*M) * point;
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| [014475] | 58 | Vector Box::translateOut(const Vector &point) const{
|
|---|
| [3dcb1f] | 59 | return (*Minv) * point;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| [014475] | 62 | Vector Box::WrapPeriodically(const Vector &point) const{
|
|---|
| [f429d7] | 63 | Vector helper = translateOut(point);
|
|---|
| 64 | for(int i=NDIM;i--;){
|
|---|
| 65 | double intpart,fracpart;
|
|---|
| 66 | fracpart = modf(helper.at(i),&intpart);
|
|---|
| 67 | if(fracpart<0.)
|
|---|
| 68 | fracpart+=1.;
|
|---|
| 69 | helper.at(i)=fracpart;
|
|---|
| 70 | }
|
|---|
| 71 | return translateIn(helper);
|
|---|
| 72 | }
|
|---|
| 73 |
|
|---|
| [0ff6b5] | 74 | bool Box::isInside(const Vector &point) const
|
|---|
| 75 | {
|
|---|
| 76 | bool result = true;
|
|---|
| 77 | Vector tester = (*Minv) * point;
|
|---|
| 78 |
|
|---|
| 79 | for(int i=0;i<NDIM;i++)
|
|---|
| [0ae36b] | 80 | result = result && ((tester[i] >= -MYEPSILON) && ((tester[i] - 1.) < MYEPSILON));
|
|---|
| [0ff6b5] | 81 |
|
|---|
| 82 | return result;
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| [a630fd] | 86 | VECTORSET(std::list) Box::explode(const Vector &point,int n) const{
|
|---|
| [12cf773] | 87 | VECTORSET(std::list) res;
|
|---|
| [89e820] | 88 |
|
|---|
| 89 | // translate the Vector into each of the 27 neighbourhoods
|
|---|
| 90 |
|
|---|
| [a630fd] | 91 | // first create all translation Vectors
|
|---|
| 92 | // there are (n*2+1)^3 such vectors
|
|---|
| 93 | int max_dim = (n*2+1);
|
|---|
| 94 | int max_dim2 = max_dim*max_dim;
|
|---|
| 95 | int max = max_dim2*max_dim;
|
|---|
| [7ac4af] | 96 | // only one loop to avoid unneccessary jumps
|
|---|
| [a630fd] | 97 | for(int i = 0;i<max;++i){
|
|---|
| 98 | // get all coordinates for this iteration
|
|---|
| 99 | int n1 = (i%max_dim)-n;
|
|---|
| 100 | int n2 = ((i/max_dim)%max_dim)-n;
|
|---|
| 101 | int n3 = ((i/max_dim2))-n;
|
|---|
| 102 | Vector translation = translateIn(Vector(n1,n2,n3));
|
|---|
| 103 | res.push_back(translation);
|
|---|
| [89e820] | 104 | }
|
|---|
| 105 | // translate all the translation vector by the offset defined by point
|
|---|
| 106 | res.translate(point);
|
|---|
| 107 | return res;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| [7ac4af] | 110 | VECTORSET(std::list) Box::explode(const Vector &point) const{
|
|---|
| 111 | VECTORSET(std::list) res;
|
|---|
| 112 |
|
|---|
| 113 | // translate the Vector into each of the 27 neighbourhoods
|
|---|
| 114 |
|
|---|
| 115 | // first create all 27 translation Vectors
|
|---|
| 116 | // these loops depend on fixed parameters and can easily be expanded
|
|---|
| 117 | // by the compiler to allow code without jumps
|
|---|
| 118 | for(int n1 = -1;n1<=1;++n1){
|
|---|
| 119 | for(int n2 = -1;n2<=1;++n2){
|
|---|
| 120 | for(int n3 = -1;n3<=1;++n3){
|
|---|
| 121 | // get all coordinates for this iteration
|
|---|
| 122 | Vector translation = translateIn(Vector(n1,n2,n3));
|
|---|
| 123 | res.push_back(translation);
|
|---|
| 124 | }
|
|---|
| 125 | }
|
|---|
| 126 | }
|
|---|
| 127 | // translate all the translation vector by the offset defined by point
|
|---|
| 128 | res.translate(point);
|
|---|
| 129 | return res;
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| [014475] | 132 | double Box::periodicDistanceSquared(const Vector &point1,const Vector &point2) const{
|
|---|
| 133 | VECTORSET(std::list) expansion = explode(point1);
|
|---|
| 134 | double res = expansion.minDistSquared(point2);
|
|---|
| 135 | return res;
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | double Box::periodicDistance(const Vector &point1,const Vector &point2) const{
|
|---|
| 139 | double res;
|
|---|
| 140 | res = sqrt(periodicDistanceSquared(point1,point2));
|
|---|
| 141 | return res;
|
|---|
| [527de2] | 142 | }
|
|---|
| 143 |
|
|---|
| [77374e] | 144 | const Box::Conditions_t Box::getConditions(){
|
|---|
| 145 | return conditions;
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | void Box::setCondition(int i,Box::BoundaryCondition_t condition){
|
|---|
| 149 | conditions[i]=condition;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| [7579a4b] | 152 | Box &Box::operator=(const Box &src){
|
|---|
| 153 | if(&src!=this){
|
|---|
| 154 | delete M;
|
|---|
| 155 | delete Minv;
|
|---|
| 156 | M = new Matrix(*src.M);
|
|---|
| 157 | Minv = new Matrix(*src.Minv);
|
|---|
| [77374e] | 158 | conditions = src.conditions;
|
|---|
| [7579a4b] | 159 | }
|
|---|
| 160 | return *this;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | Box &Box::operator=(const Matrix &mat){
|
|---|
| 164 | setM(mat);
|
|---|
| 165 | return *this;
|
|---|
| 166 | }
|
|---|