Blender V2.61 - r43446

controlparticles.h

Go to the documentation of this file.
00001 
00004 // --------------------------------------------------------------------------
00005 //
00006 // El'Beem - the visual lattice boltzmann freesurface simulator
00007 // All code distributed as part of El'Beem is covered by the version 2 of the 
00008 // GNU General Public License. See the file COPYING for details.  
00009 //
00010 // Copyright 2008 Nils Thuerey , Richard Keiser, Mark Pauly, Ulrich Ruede
00011 //
00012 // control particle classes
00013 //
00014 // --------------------------------------------------------------------------
00015 
00016 #ifndef CONTROLPARTICLES_H
00017 #define CONTROLPARTICLES_H
00018 
00019 #include "ntl_geometrymodel.h"
00020 
00021 // indicator for LBM inclusion
00022 //#ifndef LBMDIM
00023 
00024 //#include <NxFoundation.h>
00025 //#include <vector>
00026 //class MultisphGUI;
00027 //#define NORMALIZE(a) a.normalize()
00028 //#define MAGNITUDE(a) a.magnitude()
00029 //#define CROSS(a,b,c) a.cross(b,c)
00030 //#define ABS(a) (a>0. ? (a) : -(a))
00031 //#include "cpdefines.h"
00032 
00033 //#else // LBMDIM
00034 
00035 // use compatibility defines
00036 //#define NORMALIZE(a) normalize(a)
00037 //#define MAGNITUDE(a) norm(a)
00038 //#define CROSS(a,b,c) a=cross(b,c)
00039 
00040 //#endif // LBMDIM
00041 
00042 #define MAGNITUDE(a) norm(a)
00043 
00044 // math.h compatibility
00045 #define CP_PI ((LbmFloat)3.14159265358979323846)
00046 
00047 // project 2d test cases onto plane?
00048 // if not, 3d distance is used for 2d sim as well
00049 #define CP_PROJECT2D 1
00050 
00051 
00052 // default init for mincpdist, ControlForces::maxDistance
00053 #define CPF_MAXDINIT 10000.
00054 
00055 // storage of influence for a fluid cell/particle in lbm/sph
00056 class ControlForces
00057 {
00058 public:
00059     ControlForces() { };
00060     ~ControlForces() {};
00061 
00062     // attraction force
00063     LbmFloat weightAtt;
00064     LbmVec forceAtt;
00065     // velocity influence
00066     LbmFloat weightVel;
00067     LbmVec forceVel;
00068     // maximal distance influence, 
00069     // first is max. distance to first control particle
00070     // second attraction strength
00071     LbmFloat maxDistance;
00072     LbmVec forceMaxd;
00073 
00074     LbmFloat compAvWeight;
00075     LbmVec compAv;
00076 
00077     void resetForces() {
00078         weightAtt = weightVel = 0.;
00079         maxDistance = CPF_MAXDINIT;
00080         forceAtt = forceVel = forceMaxd = LbmVec(0.,0.,0.);
00081         compAvWeight=0.; compAv=LbmVec(0.);
00082     };
00083 };
00084 
00085 
00086 // single control particle
00087 class ControlParticle
00088 {
00089 public:
00090     ControlParticle() { reset(); };
00091     ~ControlParticle() {};
00092 
00093     // control parameters
00094     
00095     // position
00096     LbmVec pos;
00097     // size (influences influence radius)
00098     LbmFloat size;
00099     // overall strength of influence
00100     LbmFloat influence;
00101     // rotation axis
00102     LbmVec rotaxis;
00103 
00104     // computed values
00105 
00106     // velocity
00107     LbmVec vel;
00108     // computed density
00109     LbmFloat density;
00110     LbmFloat densityWeight;
00111 
00112     LbmVec avgVel;
00113     LbmVec avgVelAcc;
00114     LbmFloat avgVelWeight;
00115 
00116     // init all zero / defaults
00117     void reset();
00118 };
00119 
00120 
00121 // container for a particle configuration at time t
00122 class ControlParticleSet
00123 {
00124 public:
00125 
00126     // time of particle set
00127     LbmFloat time;
00128     // particle positions
00129     std::vector<ControlParticle> particles;
00130 
00131 };
00132 
00133 
00134 // container & management of control particles
00135 class ControlParticles
00136 {
00137 public:
00138     ControlParticles();
00139     ~ControlParticles();
00140 
00141     // reset datastructures for next influence step
00142     // if motion object is given, particle 1 of second system is used for overall 
00143     // position and speed offset
00144     void prepareControl(LbmFloat simtime, LbmFloat dt, ControlParticles *motion);
00145     // post control operations
00146     void finishControl(std::vector<ControlForces> &forces, LbmFloat iatt, LbmFloat ivel, LbmFloat imaxd);
00147     // recalculate 
00148     void calculateKernelWeight();
00149 
00150     // calculate forces at given position, and modify velocity
00151     // according to timestep (from initControl)
00152     void calculateCpInfluenceOpt (ControlParticle *cp, LbmVec fluidpos, LbmVec fluidvel, ControlForces *force, LbmFloat fillFactor);
00153     void calculateMaxdForce      (ControlParticle *cp, LbmVec fluidpos, ControlForces *force);
00154 
00155     // no. of particles
00156     inline int getSize() { return (int)_particles.size(); }
00157     int getTotalSize();
00158     // get particle [i]
00159     inline ControlParticle* getParticle(int i){ return &_particles[i]; }
00160 
00161     // set influence parameters
00162     void setInfluenceTangential(LbmFloat set) { _influenceTangential=set; }
00163     void setInfluenceAttraction(LbmFloat set) { _influenceAttraction=set; }
00164     void setInfluenceMaxdist(LbmFloat set)    { _influenceMaxdist=set; }
00165     // calculate for delta t
00166     void setInfluenceVelocity(LbmFloat set, LbmFloat dt);
00167     // get influence parameters
00168     inline LbmFloat getInfluenceAttraction()    { return _influenceAttraction; }
00169     inline LbmFloat getInfluenceTangential()    { return _influenceTangential; }
00170     inline LbmFloat getInfluenceVelocity()      { return _influenceVelocity; }
00171     inline LbmFloat getInfluenceMaxdist()       { return _influenceMaxdist; }
00172     inline LbmFloat getCurrTimestep()           { return _currTimestep; }
00173 
00174     void setRadiusAtt(LbmFloat set)       { _radiusAtt=set; }
00175     inline LbmFloat getRadiusAtt()        { return _radiusAtt; }
00176     void setRadiusVel(LbmFloat set)       { _radiusVel=set; }
00177     inline LbmFloat getRadiusVel()        { return _radiusVel; }
00178     void setRadiusMaxd(LbmFloat set)      { _radiusMaxd=set; }
00179     inline LbmFloat getRadiusMaxd()       { return _radiusMaxd; }
00180     void setRadiusMinMaxd(LbmFloat set)   { _radiusMinMaxd=set; }
00181     inline LbmFloat getRadiusMinMaxd()    { return _radiusMinMaxd; }
00182 
00183     LbmFloat getControlTimStart();
00184     LbmFloat getControlTimEnd();
00185 
00186     // set/get characteristic length (and inverse)
00187     void setCharLength(LbmFloat set)      { _charLength=set; _charLengthInv=1./_charLength; }
00188     inline LbmFloat getCharLength()       { return _charLength;}
00189     inline LbmFloat getCharLengthInv()    { return _charLengthInv;}
00190 
00191     // set init parameters
00192     void setInitTimeScale(LbmFloat set)  { _initTimeScale = set; };
00193     void setInitMirror(string set)  { _initMirror = set; };
00194     string getInitMirror()          { return _initMirror; };
00195 
00196     void setLastOffset(LbmVec set) { _initLastPartOffset = set; };
00197     void setLastScale(LbmVec set)  { _initLastPartScale = set; };
00198     void setOffset(LbmVec set) { _initPartOffset = set; };
00199     void setScale(LbmVec set)  { _initPartScale = set; };
00200 
00201     // set/get cps params
00202     void setCPSWith(LbmFloat set)       { mCPSWidth = set; };
00203     void setCPSTimestep(LbmFloat set)   { mCPSTimestep = set; };
00204     void setCPSTimeStart(LbmFloat set)  { mCPSTimeStart = set; };
00205     void setCPSTimeEnd(LbmFloat set)    { mCPSTimeEnd = set; };
00206     void setCPSMvmWeightFac(LbmFloat set) { mCPSWeightFac = set; };
00207 
00208     LbmFloat getCPSWith()       { return mCPSWidth; };
00209     LbmFloat getCPSTimestep()   { return mCPSTimestep; };
00210     LbmFloat getCPSTimeStart()  { return mCPSTimeStart; };
00211     LbmFloat getCPSTimeEnd()    { return mCPSTimeEnd; };
00212     LbmFloat getCPSMvmWeightFac() { return mCPSWeightFac; };
00213 
00214     void setDebugInit(int set)       { mDebugInit = set; };
00215 
00216     // set init parameters
00217     void setFluidSpacing(LbmFloat set)  { _fluidSpacing = set; };
00218 
00219     // load positions & timing from text file
00220     int initFromTextFile(string filename);
00221     int initFromTextFileOld(string filename);
00222     // load positions & timing from gzipped binary file
00223     int initFromBinaryFile(string filename);
00224     int initFromMVCMesh(string filename);
00225     // init an example test case
00226     int initExampleSet();
00227 
00228     // init for a given time
00229     void initTime(LbmFloat t, LbmFloat dt);
00230 
00231     // blender test init
00232     void initBlenderTest();
00233     
00234     int initFromObject(ntlGeometryObjModel *model);
00235 
00236 protected:
00237     // sets influence params
00238     friend class MultisphGUI;
00239 
00240     // tangential and attraction influence
00241     LbmFloat _influenceTangential, _influenceAttraction;
00242     // direct velocity influence
00243     LbmFloat _influenceVelocity;
00244     // maximal distance influence
00245     LbmFloat _influenceMaxdist;
00246 
00247     // influence radii
00248     LbmFloat _radiusAtt, _radiusVel, _radiusMinMaxd, _radiusMaxd;
00249 
00250     // currently valid time & timestep
00251     LbmFloat _currTime, _currTimestep;
00252     // all particles
00253     std::vector<ControlParticle> _particles;
00254 
00255     // particle sets
00256     std::vector<ControlParticleSet> mPartSets;
00257 
00258     // additional parameters for initing particles
00259     LbmFloat _initTimeScale;
00260     LbmVec _initPartOffset;
00261     LbmVec _initPartScale;
00262     LbmVec _initLastPartOffset;
00263     LbmVec _initLastPartScale;
00264     // mirror particles for loading?
00265     string _initMirror;
00266 
00267     // row spacing paramter, e.g. use for approximation of kernel area/volume
00268     LbmFloat _fluidSpacing;
00269     // save current kernel weight
00270     LbmFloat _kernelWeight;
00271     // charateristic length in world coordinates for normalizatioon of forces
00272     LbmFloat _charLength, _charLengthInv;
00273 
00274 
00276     void calculateCPS(string filename);
00278     ntlVec3Gfx mvCPSStart, mvCPSEnd;
00279     gfxReal mCPSWidth, mCPSTimestep;
00280     gfxReal mCPSTimeStart, mCPSTimeEnd;
00281     gfxReal mCPSWeightFac;
00282 
00283     int mDebugInit;
00284 
00285     
00286 protected:
00287     // apply init transformations
00288     void applyTrafos();
00289 
00290     // helper function for init -> swap components everywhere
00291     void swapCoords(int a,int b);
00292     // helper function for init -> mirror time
00293     void mirrorTime();
00294 
00295     // helper, init given array
00296     void initTimeArray(LbmFloat t, std::vector<ControlParticle> &parts);
00297 
00298     bool checkPointInside(ntlTree *tree, ntlVec3Gfx org, gfxReal &distance);
00299 };
00300 
00301 
00302 
00303 #endif
00304