Blender  V2.59
FLUID_3D.h
Go to the documentation of this file.
00001 
00004 
00005 // This file is part of Wavelet Turbulence.
00006 // 
00007 // Wavelet Turbulence is free software: you can redistribute it and/or modify
00008 // it under the terms of the GNU General Public License as published by
00009 // the Free Software Foundation, either version 3 of the License, or
00010 // (at your option) any later version.
00011 // 
00012 // Wavelet Turbulence is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 // 
00017 // You should have received a copy of the GNU General Public License
00018 // along with Wavelet Turbulence.  If not, see <http://www.gnu.org/licenses/>.
00019 // 
00020 // Copyright 2008 Theodore Kim and Nils Thuerey
00021 // 
00022 // FLUID_3D.h: interface for the FLUID_3D class.
00023 //
00025 // Heavy parallel optimization done. Many of the old functions now
00026 // take begin and end parameters and process only specified part of the data.
00027 // Some functions were divided into multiple ones.
00028 //              - MiikaH
00030 
00031 #ifndef FLUID_3D_H
00032 #define FLUID_3D_H
00033 
00034 #include <cstdlib>
00035 #include <cmath>
00036 #include <cstring>
00037 #include <iostream>
00038 #include "OBSTACLE.h"
00039 // #include "WTURBULENCE.h"
00040 #include "VEC3.h"
00041 
00042 // timestep default value for nice appearance
00043 #define DT_DEFAULT 0.1f;
00044 
00045 using namespace std;
00046 using namespace BasicVector;
00047 class WTURBULENCE;
00048 
00049 class FLUID_3D  
00050 {
00051         public:
00052                 FLUID_3D(int *res, /* int amplify, */ float *p0);
00053                 FLUID_3D() {};
00054                 virtual ~FLUID_3D();
00055 
00056                 void initBlenderRNA(float *alpha, float *beta, float *dt_factor, float *vorticity, int *border_colli);
00057                 
00058                 // create & allocate vector noise advection 
00059                 void initVectorNoise(int amplify);
00060 
00061                 void addSmokeColumn();
00062                 static void addSmokeTestCase(float* field, Vec3Int res);
00063 
00064                 void step(float dt);
00065                 void addObstacle(OBSTACLE* obstacle);
00066 
00067                 const float* xVelocity() { return _xVelocity; }; 
00068                 const float* yVelocity() { return _yVelocity; }; 
00069                 const float* zVelocity() { return _zVelocity; }; 
00070 
00071                 int xRes() const { return _xRes; };
00072                 int yRes() const { return _yRes; };
00073                 int zRes() const { return _zRes; };
00074 
00075         public:  
00076                 // dimensions
00077                 int _xRes, _yRes, _zRes, _maxRes;
00078                 Vec3Int _res;
00079                 size_t _totalCells;
00080                 int _slabSize;
00081                 float _dx;
00082                 float _p0[3];
00083                 float _p1[3];
00084                 float _totalTime;
00085                 int _totalSteps;
00086                 int _totalImgDumps;
00087                 int _totalVelDumps;
00088 
00089                 void artificialDampingSL(int zBegin, int zEnd);
00090                 void artificialDampingExactSL(int pos);
00091 
00092                 // fields
00093                 float* _density;
00094                 float* _densityOld;
00095                 float* _heat;
00096                 float* _heatOld;
00097                 float* _xVelocity;
00098                 float* _yVelocity;
00099                 float* _zVelocity;
00100                 float* _xVelocityOld;
00101                 float* _yVelocityOld;
00102                 float* _zVelocityOld;
00103                 float* _xForce;
00104                 float* _yForce;
00105                 float* _zForce;
00106                 unsigned char*  _obstacles;
00107 
00108                 // Required for proper threading:
00109                 float* _xVelocityTemp;
00110                 float* _yVelocityTemp;
00111                 float* _zVelocityTemp;
00112                 float* _heatTemp;
00113                 float* _densityTemp;
00114 
00115                 // CG fields
00116                 int _iterations;
00117 
00118                 // simulation constants
00119                 float _dt;
00120                 float *_dtFactor;
00121                 float _vorticityEps;
00122                 float _heatDiffusion;
00123                 float *_vorticityRNA;   // RNA-pointer.
00124                 float *_alpha; // for the buoyancy density term <-- as pointer to get blender RNA in here
00125                 float *_beta; // was _buoyancy <-- as pointer to get blender RNA in here
00126                 float _tempAmb; /* ambient temperature */
00127                 float _constantScaling;
00128 
00129                 bool _domainBcFront;  // z
00130                 bool _domainBcTop;    // y
00131                 bool _domainBcLeft;   // x
00132                 bool _domainBcBack;   // DOMAIN_BC_FRONT
00133                 bool _domainBcBottom; // DOMAIN_BC_TOP
00134                 bool _domainBcRight;  // DOMAIN_BC_LEFT
00135                 int *_borderColli; // border collision rules <-- as pointer to get blender RNA in here
00136                 int _colloPrev;         // To track whether value has been changed (to not
00137                                                         // have to recalibrate borders if nothing has changed
00138                 void setBorderCollisions();
00139 
00140                 // WTURBULENCE object, if active
00141                 // WTURBULENCE* _wTurbulence;
00142 
00143                 // boundary setting functions
00144                 void copyBorderAll(float* field, int zBegin, int zEnd);
00145 
00146                 // timestepping functions
00147                 void wipeBoundaries(int zBegin, int zEnd);
00148                 void wipeBoundariesSL(int zBegin, int zEnd);
00149                 void addForce(int zBegin, int zEnd);
00150                 void addVorticity(int zBegin, int zEnd);
00151                 void addBuoyancy(float *heat, float *density, int zBegin, int zEnd);
00152 
00153                 // solver stuff
00154                 void project();
00155                 void diffuseHeat();
00156                 void solvePressure(float* field, float* b, unsigned char* skip);
00157                 void solvePressurePre(float* field, float* b, unsigned char* skip);
00158                 void solveHeat(float* field, float* b, unsigned char* skip);
00159 
00160 
00161                 // handle obstacle boundaries
00162                 void setObstacleBoundaries(float *_pressure, int zBegin, int zEnd);
00163                 void setObstaclePressure(float *_pressure, int zBegin, int zEnd);
00164 
00165         public:
00166                 // advection, accessed e.g. by WTURBULENCE class
00167                 //void advectMacCormack();
00168                 void advectMacCormackBegin(int zBegin, int zEnd);
00169                 void advectMacCormackEnd1(int zBegin, int zEnd);
00170                 void advectMacCormackEnd2(int zBegin, int zEnd);
00171 
00172                 // boundary setting functions
00173                 static void copyBorderX(float* field, Vec3Int res, int zBegin, int zEnd);
00174                 static void copyBorderY(float* field, Vec3Int res, int zBegin, int zEnd);
00175                 static void copyBorderZ(float* field, Vec3Int res, int zBegin, int zEnd);
00176                 static void setNeumannX(float* field, Vec3Int res, int zBegin, int zEnd);
00177                 static void setNeumannY(float* field, Vec3Int res, int zBegin, int zEnd);
00178                 static void setNeumannZ(float* field, Vec3Int res, int zBegin, int zEnd);
00179                 static void setZeroX(float* field, Vec3Int res, int zBegin, int zEnd);
00180                 static void setZeroY(float* field, Vec3Int res, int zBegin, int zEnd);
00181                 static void setZeroZ(float* field, Vec3Int res, int zBegin, int zEnd);
00182                 static void setZeroBorder(float* field, Vec3Int res, int zBegin, int zEnd) {
00183                         setZeroX(field, res, zBegin, zEnd);
00184                         setZeroY(field, res, zBegin, zEnd);
00185                         setZeroZ(field, res, zBegin, zEnd);
00186                 };
00187 
00188                 
00189 
00190                 // static advection functions, also used by WTURBULENCE
00191                 static void advectFieldSemiLagrange(const float dt, const float* velx, const float* vely,  const float* velz,
00192                                 float* oldField, float* newField, Vec3Int res, int zBegin, int zEnd);
00193                 static void advectFieldMacCormack1(const float dt, const float* xVelocity, const float* yVelocity, const float* zVelocity, 
00194                                 float* oldField, float* tempResult, Vec3Int res, int zBegin, int zEnd);
00195                 static void advectFieldMacCormack2(const float dt, const float* xVelocity, const float* yVelocity, const float* zVelocity, 
00196                                 float* oldField, float* newField, float* tempResult, float* temp1,Vec3Int res, const unsigned char* obstacles, int zBegin, int zEnd);
00197 
00198 
00199                 // temp ones for testing
00200                 /*static void advectFieldMacCormack(const float dt, const float* xVelocity, const float* yVelocity, const float* zVelocity, 
00201                                 float* oldField, float* newField, float* temp1, float* temp2, Vec3Int res, const unsigned char* obstacles);*/
00202                 /*static void advectFieldSemiLagrange2(const float dt, const float* velx, const float* vely,  const float* velz,
00203                                 float* oldField, float* newField, Vec3Int res);*/
00204 
00205                 // maccormack helper functions
00206                 static void clampExtrema(const float dt, const float* xVelocity, const float* yVelocity,  const float* zVelocity,
00207                                 float* oldField, float* newField, Vec3Int res, int zBegin, int zEnd);
00208                 static void clampOutsideRays(const float dt, const float* xVelocity, const float* yVelocity,  const float* zVelocity,
00209                                 float* oldField, float* newField, Vec3Int res, const unsigned char* obstacles, const float *oldAdvection, int zBegin, int zEnd);
00210 
00211 
00212 
00213                 // output helper functions
00214                 // static void writeImageSliceXY(const float *field, Vec3Int res, int slice, string prefix, int picCnt, float scale=1.);
00215                 // static void writeImageSliceYZ(const float *field, Vec3Int res, int slice, string prefix, int picCnt, float scale=1.);
00216                 // static void writeImageSliceXZ(const float *field, Vec3Int res, int slice, string prefix, int picCnt, float scale=1.);
00217                 // static void writeProjectedIntern(const float *field, Vec3Int res, int dir1, int dir2, string prefix, int picCnt, float scale=1.); 
00218 };
00219 
00220 #endif