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