Fluid bugfix [#8395] [#6200]: this should solve the popping/inconsistency/flickering...
[blender-staging.git] / intern / elbeem / intern / solver_util.cpp
1 /******************************************************************************
2  *
3  * El'Beem - Free Surface Fluid Simulation with the Lattice Boltzmann Method
4  * Copyright 2003-2006 Nils Thuerey
5  *
6  * Standard LBM Factory implementation
7  *
8  *****************************************************************************/
9
10 #include "solver_class.h"
11 #include "solver_relax.h"
12 #include "particletracer.h"
13
14 // MPT
15 #include "ntl_world.h"
16 #include "simulation_object.h"
17
18 #include <stdlib.h>
19 #include <zlib.h>
20 #ifndef sqrtf
21 #define sqrtf sqrt
22 #endif
23
24 /******************************************************************************
25  * helper functions
26  *****************************************************************************/
27
28 // try to enhance surface?
29 #define SURFACE_ENH 2
30
31 extern bool glob_mpactive;
32 extern bool glob_mpnum;
33 extern bool glob_mpindex;
34
35 //! for raytracing
36 void LbmFsgrSolver::prepareVisualization( void ) {
37         int lev = mMaxRefine;
38         int workSet = mLevel[lev].setCurr;
39
40         int mainGravDir=0;
41         LbmFloat mainGravLen = 0.;
42         FORDF1{
43                 LbmFloat thisGravLen = dot(LbmVec(dfVecX[l],dfVecY[l],dfVecZ[l]), getNormalized(mLevel[mMaxRefine].gravity) );
44                 if(thisGravLen>mainGravLen) {
45                         mainGravLen = thisGravLen;
46                         mainGravDir = l;
47                 }
48         }
49
50 #if LBMDIM==2
51         // 2d, place in the middle of isofield slice (k=2)
52 #  define ZKD1 0
53         // 2d z offset = 2, lbmGetData adds 1, so use one here
54 #  define ZKOFF 1
55         // reset all values...
56         for(int k= 0; k< 5; ++k) 
57    for(int j=0;j<mLevel[lev].lSizey-0;j++) 
58     for(int i=0;i<mLevel[lev].lSizex-0;i++) {
59                 *mpIso->lbmGetData(i,j,ZKOFF)=0.0;
60         }
61 #else // LBMDIM==2
62         // 3d, use normal bounds
63 #  define ZKD1 1
64 #  define ZKOFF k
65         // reset all values...
66         for(int k= getForZMinBnd(); k< getForZMaxBnd(lev); ++k) 
67    for(int j=0;j<mLevel[lev].lSizey-0;j++) 
68     for(int i=0;i<mLevel[lev].lSizex-0;i++) {
69                 *mpIso->lbmGetData(i,j,ZKOFF)=0.0;
70         }
71 #endif // LBMDIM==2
72
73         // MPT, ignore
74         if((glob_mpactive) && (glob_mpnum>1) && (glob_mpindex==0)) {
75                 mpIso->resetAll(0.);
76         }
77
78
79         LbmFloat minval = mIsoValue*1.05; // / mIsoWeight[13]; 
80         // add up...
81         float val = 0.0;
82         for(int k= getForZMin1(); k< getForZMax1(lev); ++k) 
83    for(int j=1;j<mLevel[lev].lSizey-1;j++) 
84     for(int i=1;i<mLevel[lev].lSizex-1;i++) {
85                         const CellFlagType cflag = RFLAG(lev, i,j,k,workSet);
86                         //if(cflag&(CFBnd|CFEmpty)) {
87
88 #if SURFACE_ENH==0
89
90                         // no enhancements...
91                         if( (cflag&(CFFluid|CFUnused)) ) {
92                                 val = 1.;
93                         } else if( (cflag&CFInter) ) {
94                                 val = (QCELL(lev, i,j,k,workSet, dFfrac)); 
95                         } else {
96                                 continue;
97                         }
98
99 #else // SURFACE_ENH!=1
100                         if(cflag&CFBnd) {
101                                 // treated in second loop
102                                 continue;
103                         } else if(cflag&CFUnused) {
104                                 val = 1.;
105                         } else if( (cflag&CFFluid) && (cflag&CFNoBndFluid)) {
106                                 // optimized fluid
107                                 val = 1.;
108                         } else if( (cflag&(CFEmpty|CFInter|CFFluid)) ) {
109                                 int noslipbnd = 0;
110                                 int intercnt = 0;
111                                 FORDF1 { 
112                                         const CellFlagType nbflag = RFLAG_NB(lev, i,j,k, workSet,l);
113                                         if(nbflag&CFInter){ intercnt++; }
114
115                                         // check all directions otherwise we get bugs with splashes on obstacles
116                                         // if(l!=mainGravDir) continue; // only check bnd along main grav. dir
117                                         //if((nbflag&CFBnd)&&(nbflag&CFBndNoslip)){ noslipbnd=1; }
118                                         if((nbflag&CFBnd)){ noslipbnd=1; }
119                                 }
120
121                                 if(cflag&CFEmpty) {
122                                         // special empty treatment
123                                         if((noslipbnd)&&(intercnt>6)) {
124                                                 *mpIso->lbmGetData(i,j,ZKOFF) += minval;
125                                         } else if((noslipbnd)&&(intercnt>0)) {
126                                                 // necessary?
127                                                 *mpIso->lbmGetData(i,j,ZKOFF) += mIsoValue*0.9;
128                                         } else {
129                                                 // nothing to do...
130                                         }
131
132                                         continue;
133                                 } else if(cflag&(CFNoNbEmpty|CFFluid)) {
134                                         // no empty nb interface cells are treated as full
135                                         val=1.0;
136                                 } else {
137                                         val = (QCELL(lev, i,j,k,workSet, dFfrac)); 
138                                 }
139                                 
140                                 if(noslipbnd) {
141                                         if(val<minval) val = minval; 
142                                         *mpIso->lbmGetData(i,j,ZKOFF) += minval-( val * mIsoWeight[13] ); 
143                                 }
144                         } else { // all others, unused?
145                                 continue;
146                         } 
147 #endif // SURFACE_ENH>0
148
149                         *mpIso->lbmGetData( i-1 , j-1 ,ZKOFF-ZKD1) += ( val * mIsoWeight[0] ); 
150                         *mpIso->lbmGetData( i   , j-1 ,ZKOFF-ZKD1) += ( val * mIsoWeight[1] ); 
151                         *mpIso->lbmGetData( i+1 , j-1 ,ZKOFF-ZKD1) += ( val * mIsoWeight[2] ); 
152
153                         *mpIso->lbmGetData( i-1 , j   ,ZKOFF-ZKD1) += ( val * mIsoWeight[3] ); 
154                         *mpIso->lbmGetData( i   , j   ,ZKOFF-ZKD1) += ( val * mIsoWeight[4] ); 
155                         *mpIso->lbmGetData( i+1 , j   ,ZKOFF-ZKD1) += ( val * mIsoWeight[5] ); 
156
157                         *mpIso->lbmGetData( i-1 , j+1 ,ZKOFF-ZKD1) += ( val * mIsoWeight[6] ); 
158                         *mpIso->lbmGetData( i   , j+1 ,ZKOFF-ZKD1) += ( val * mIsoWeight[7] ); 
159                         *mpIso->lbmGetData( i+1 , j+1 ,ZKOFF-ZKD1) += ( val * mIsoWeight[8] ); 
160
161
162                         *mpIso->lbmGetData( i-1 , j-1  ,ZKOFF  ) += ( val * mIsoWeight[9] ); 
163                         *mpIso->lbmGetData( i   , j-1  ,ZKOFF  ) += ( val * mIsoWeight[10] ); 
164                         *mpIso->lbmGetData( i+1 , j-1  ,ZKOFF  ) += ( val * mIsoWeight[11] ); 
165
166                         *mpIso->lbmGetData( i-1 , j    ,ZKOFF  ) += ( val * mIsoWeight[12] ); 
167                         *mpIso->lbmGetData( i   , j    ,ZKOFF  ) += ( val * mIsoWeight[13] ); 
168                         *mpIso->lbmGetData( i+1 , j    ,ZKOFF  ) += ( val * mIsoWeight[14] ); 
169
170                         *mpIso->lbmGetData( i-1 , j+1  ,ZKOFF  ) += ( val * mIsoWeight[15] ); 
171                         *mpIso->lbmGetData( i   , j+1  ,ZKOFF  ) += ( val * mIsoWeight[16] ); 
172                         *mpIso->lbmGetData( i+1 , j+1  ,ZKOFF  ) += ( val * mIsoWeight[17] ); 
173
174
175                         *mpIso->lbmGetData( i-1 , j-1 ,ZKOFF+ZKD1) += ( val * mIsoWeight[18] ); 
176                         *mpIso->lbmGetData( i   , j-1 ,ZKOFF+ZKD1) += ( val * mIsoWeight[19] ); 
177                         *mpIso->lbmGetData( i+1 , j-1 ,ZKOFF+ZKD1) += ( val * mIsoWeight[20] ); 
178
179                         *mpIso->lbmGetData( i-1 , j   ,ZKOFF+ZKD1) += ( val * mIsoWeight[21] ); 
180                         *mpIso->lbmGetData( i   , j   ,ZKOFF+ZKD1)+= ( val * mIsoWeight[22] ); 
181                         *mpIso->lbmGetData( i+1 , j   ,ZKOFF+ZKD1) += ( val * mIsoWeight[23] ); 
182
183                         *mpIso->lbmGetData( i-1 , j+1 ,ZKOFF+ZKD1) += ( val * mIsoWeight[24] ); 
184                         *mpIso->lbmGetData( i   , j+1 ,ZKOFF+ZKD1) += ( val * mIsoWeight[25] ); 
185                         *mpIso->lbmGetData( i+1 , j+1 ,ZKOFF+ZKD1) += ( val * mIsoWeight[26] ); 
186         }
187
188         // TEST!?
189 #if SURFACE_ENH>=2
190
191         if(mFsSurfGenSetting&fssgNoObs) {
192                 for(int k= getForZMin1(); k< getForZMax1(lev); ++k) 
193                  for(int j=1;j<mLevel[lev].lSizey-1;j++) 
194                         for(int i=1;i<mLevel[lev].lSizex-1;i++) {
195                                 const CellFlagType cflag = RFLAG(lev, i,j,k,workSet);
196                                 if(cflag&(CFBnd)) {
197                                         CellFlagType nbored=0;
198                                         LbmFloat avgfill=0.,avgfcnt=0.;
199                                         FORDF1 { 
200                                                 const int ni = i+dfVecX[l];
201                                                 const int nj = j+dfVecY[l];
202                                                 const int nk = ZKOFF+dfVecZ[l];
203
204                                                 const CellFlagType nbflag = RFLAG(lev, ni,nj,nk, workSet);
205                                                 nbored |= nbflag;
206                                                 if(nbflag&CFInter) {
207                                                         avgfill += QCELL(lev, ni,nj,nk, workSet,dFfrac); avgfcnt += 1.;
208                                                 } else if(nbflag&CFFluid) {
209                                                         avgfill += 1.; avgfcnt += 1.;
210                                                 } else if(nbflag&CFEmpty) {
211                                                         avgfill += 0.; avgfcnt += 1.;
212                                                 }
213
214                                                 //if( (ni<0) || (nj<0) || (nk<0) 
215                                                  //|| (ni>=mLevel[mMaxRefine].lSizex) 
216                                                  //|| (nj>=mLevel[mMaxRefine].lSizey) 
217                                                  //|| (nk>=mLevel[mMaxRefine].lSizez) ) continue;
218                                         }
219
220                                         if(nbored&CFInter) {
221                                                 if(avgfcnt>0.) avgfill/=avgfcnt;
222                                                 *mpIso->lbmGetData(i,j,ZKOFF) = avgfill; continue;
223                                         } 
224                                         else if(nbored&CFFluid) {
225                                                 *mpIso->lbmGetData(i,j,ZKOFF) = 1.; continue;
226                                         }
227
228                                 }
229                         }
230
231                 // move surface towards inner "row" of obstacle
232                 // cells if necessary (all obs cells without fluid/inter
233                 // nbs (=iso==0) next to obstacles...)
234                 for(int k= getForZMin1(); k< getForZMax1(lev); ++k) 
235                         for(int j=1;j<mLevel[lev].lSizey-1;j++) 
236                                 for(int i=1;i<mLevel[lev].lSizex-1;i++) {
237                                         const CellFlagType cflag = RFLAG(lev, i,j,k,workSet);
238                                         if( (cflag&(CFBnd)) && (*mpIso->lbmGetData(i,j,ZKOFF)==0.)) {
239                                                 int bndnbcnt=0;
240                                                 FORDF1 { 
241                                                         const int ni = i+dfVecX[l];
242                                                         const int nj = j+dfVecY[l];
243                                                         const int nk = ZKOFF+dfVecZ[l];
244                                                         const CellFlagType nbflag = RFLAG(lev, ni,nj,nk, workSet);
245                                                         if(nbflag&CFBnd) bndnbcnt++;
246                                                 }
247                                                 if(bndnbcnt>0) *mpIso->lbmGetData(i,j,ZKOFF)=mIsoValue*0.95; 
248                                         }
249                                 }
250         }
251         // */
252
253         if(mFsSurfGenSetting&fssgNoNorth) 
254                 for(int k= getForZMinBnd(); k< getForZMaxBnd(lev); ++k) 
255                         for(int j=0;j<mLevel[lev].lSizey-0;j++) {
256                                 *mpIso->lbmGetData(0,                   j,ZKOFF) = *mpIso->lbmGetData(1,                   j,ZKOFF);
257                         }
258         if(mFsSurfGenSetting&fssgNoEast) 
259                 for(int k= getForZMinBnd(); k< getForZMaxBnd(lev); ++k) 
260                         for(int i=0;i<mLevel[lev].lSizex-0;i++) {
261                                 *mpIso->lbmGetData(i,0,                   ZKOFF) = *mpIso->lbmGetData(i,1,                   ZKOFF);
262                         }
263         if(mFsSurfGenSetting&fssgNoSouth) 
264                 for(int k= getForZMinBnd(); k< getForZMaxBnd(lev); ++k) 
265                         for(int j=0;j<mLevel[lev].lSizey-0;j++) {
266                                 *mpIso->lbmGetData(mLevel[lev].lSizex-1,j,ZKOFF) = *mpIso->lbmGetData(mLevel[lev].lSizex-2,j,ZKOFF);
267                         }
268         if(mFsSurfGenSetting&fssgNoWest) 
269                 for(int k= getForZMinBnd(); k< getForZMaxBnd(lev); ++k) 
270                         for(int i=0;i<mLevel[lev].lSizex-0;i++) {
271                                 *mpIso->lbmGetData(i,mLevel[lev].lSizey-1,ZKOFF) = *mpIso->lbmGetData(i,mLevel[lev].lSizey-2,ZKOFF);
272                         }
273         if(LBMDIM>2) {
274                 if(mFsSurfGenSetting&fssgNoBottom) 
275                         for(int j=0;j<mLevel[lev].lSizey-0;j++) 
276                                 for(int i=0;i<mLevel[lev].lSizex-0;i++) {
277                                         *mpIso->lbmGetData(i,j,0                   ) = *mpIso->lbmGetData(i,j,1                   );
278                                 } 
279                 if(mFsSurfGenSetting&fssgNoTop) 
280                         for(int j=0;j<mLevel[lev].lSizey-0;j++) 
281                                 for(int i=0;i<mLevel[lev].lSizex-0;i++) {
282                                         *mpIso->lbmGetData(i,j,mLevel[lev].lSizez-1) = *mpIso->lbmGetData(i,j,mLevel[lev].lSizez-2);
283                                 } 
284         }
285 #endif // SURFACE_ENH>=2
286
287
288         // update preview, remove 2d?
289         if((mOutputSurfacePreview)&&(LBMDIM==3)) {
290                 int pvsx = (int)(mPreviewFactor*mSizex);
291                 int pvsy = (int)(mPreviewFactor*mSizey);
292                 int pvsz = (int)(mPreviewFactor*mSizez);
293                 //float scale = (float)mSizex / previewSize;
294                 LbmFloat scalex = (LbmFloat)mSizex/(LbmFloat)pvsx;
295                 LbmFloat scaley = (LbmFloat)mSizey/(LbmFloat)pvsy;
296                 LbmFloat scalez = (LbmFloat)mSizez/(LbmFloat)pvsz;
297                 for(int k= 0; k< (pvsz-1); ++k) 
298                 for(int j=0;j< pvsy;j++) 
299                 for(int i=0;i< pvsx;i++) {
300                                         *mpPreviewSurface->lbmGetData(i,j,k) = *mpIso->lbmGetData( (int)(i*scalex), (int)(j*scaley), (int)(k*scalez) );
301                                 }
302                 // set borders again...
303                 for(int k= 0; k< (pvsz-1); ++k) {
304                         for(int j=0;j< pvsy;j++) {
305                                 *mpPreviewSurface->lbmGetData(0,j,k) = *mpIso->lbmGetData( 0, (int)(j*scaley), (int)(k*scalez) );
306                                 *mpPreviewSurface->lbmGetData(pvsx-1,j,k) = *mpIso->lbmGetData( mSizex-1, (int)(j*scaley), (int)(k*scalez) );
307                         }
308                         for(int i=0;i< pvsx;i++) {
309                                 *mpPreviewSurface->lbmGetData(i,0,k) = *mpIso->lbmGetData( (int)(i*scalex), 0, (int)(k*scalez) );
310                                 *mpPreviewSurface->lbmGetData(i,pvsy-1,k) = *mpIso->lbmGetData( (int)(i*scalex), mSizey-1, (int)(k*scalez) );
311                         }
312                 }
313                 for(int j=0;j<pvsy;j++)
314                         for(int i=0;i<pvsx;i++) {      
315                                 *mpPreviewSurface->lbmGetData(i,j,0) = *mpIso->lbmGetData( (int)(i*scalex), (int)(j*scaley) , 0);
316                                 *mpPreviewSurface->lbmGetData(i,j,pvsz-1) = *mpIso->lbmGetData( (int)(i*scalex), (int)(j*scaley) , mSizez-1);
317                         }
318
319                 if(mFarFieldSize>=1.2) {
320                         // also remove preview border
321                         for(int k= 0; k< (pvsz-1); ++k) {
322                                 for(int j=0;j< pvsy;j++) {
323                                         *mpPreviewSurface->lbmGetData(0,j,k) = 
324                                         *mpPreviewSurface->lbmGetData(1,j,k) =  
325                                         *mpPreviewSurface->lbmGetData(2,j,k);
326                                         *mpPreviewSurface->lbmGetData(pvsx-1,j,k) = 
327                                         *mpPreviewSurface->lbmGetData(pvsx-2,j,k) = 
328                                         *mpPreviewSurface->lbmGetData(pvsx-3,j,k);
329                                         //0.0;
330                                 }
331                                 for(int i=0;i< pvsx;i++) {
332                                         *mpPreviewSurface->lbmGetData(i,0,k) = 
333                                         *mpPreviewSurface->lbmGetData(i,1,k) = 
334                                         *mpPreviewSurface->lbmGetData(i,2,k);
335                                         *mpPreviewSurface->lbmGetData(i,pvsy-1,k) = 
336                                         *mpPreviewSurface->lbmGetData(i,pvsy-2,k) = 
337                                         *mpPreviewSurface->lbmGetData(i,pvsy-3,k);
338                                         //0.0;
339                                 }
340                         }
341                         for(int j=0;j<pvsy;j++)
342                                 for(int i=0;i<pvsx;i++) {      
343                                         *mpPreviewSurface->lbmGetData(i,j,0) = 
344                                         *mpPreviewSurface->lbmGetData(i,j,1) = 
345                                         *mpPreviewSurface->lbmGetData(i,j,2);
346                                         *mpPreviewSurface->lbmGetData(i,j,pvsz-1) = 
347                                         *mpPreviewSurface->lbmGetData(i,j,pvsz-2) = 
348                                         *mpPreviewSurface->lbmGetData(i,j,pvsz-3);
349                                         //0.0;
350                         }
351                 }
352         }
353
354         // MPT
355         #if LBM_INCLUDE_TESTSOLVERS==1
356         mrIsoExchange();
357         #endif // LBM_INCLUDE_TESTSOLVERS==1
358
359         return;
360 }
361
362 /*! calculate speeds of fluid objects (or inflow) */
363 void LbmFsgrSolver::recalculateObjectSpeeds() {
364         const bool debugRecalc = false;
365         int numobjs = (int)(this->mpGiObjects->size());
366         // note - (numobjs + 1) is entry for domain settings
367
368         if(debugRecalc) errMsg("recalculateObjectSpeeds","start, #obj:"<<numobjs);
369         if(numobjs>255-1) {
370                 errFatal("LbmFsgrSolver::recalculateObjectSpeeds","More than 256 objects currently not supported...",SIMWORLD_INITERROR);
371                 return;
372         }
373         mObjectSpeeds.resize(numobjs+1);
374         for(int i=0; i<(int)(numobjs+0); i++) {
375                 mObjectSpeeds[i] = vec2L(this->mpParam->calculateLattVelocityFromRw( vec2P( (*this->mpGiObjects)[i]->getInitialVelocity(mSimulationTime) )));
376                 if(debugRecalc) errMsg("recalculateObjectSpeeds","id"<<i<<" set to "<< mObjectSpeeds[i]<<", unscaled:"<< (*this->mpGiObjects)[i]->getInitialVelocity(mSimulationTime) );
377         }
378
379         // also reinit part slip values here
380         mObjectPartslips.resize(numobjs+1);
381         for(int i=0; i<=(int)(numobjs+0); i++) {
382                 if(i<numobjs) {
383                         mObjectPartslips[i] = (LbmFloat)(*this->mpGiObjects)[i]->getGeoPartSlipValue();
384                 } else {
385                         // domain setting
386                         mObjectPartslips[i] = this->mDomainPartSlipValue;
387                 }
388                 LbmFloat set = mObjectPartslips[i];
389
390                 // as in setInfluenceVelocity
391                 const LbmFloat dt = mLevel[mMaxRefine].timestep;
392                 const LbmFloat dtInter = 0.01;
393                 //LbmFloat facFv = 1.-set;
394                 // mLevel[mMaxRefine].timestep
395                 LbmFloat facNv = (LbmFloat)( 1.-pow( (double)(set), (double)(dt/dtInter)) );
396                 errMsg("mObjectPartslips","id:"<<i<<"/"<<numobjs<<"  ts:"<<dt<< " its:"<<(dt/dtInter) <<" set"<<set<<" nv"<<facNv<<" test:"<<
397                          pow( (double)(1.-facNv),(double)(dtInter/dt))  );
398                 mObjectPartslips[i] = facNv;
399
400                 if(debugRecalc) errMsg("recalculateObjectSpeeds","id"<<i<<" parts "<< mObjectPartslips[i] );
401         }
402
403         if(debugRecalc) errMsg("recalculateObjectSpeeds","done, domain:"<<mObjectPartslips[numobjs]<<" n"<<numobjs);
404 }
405
406
407
408 /*****************************************************************************/
409 /*! debug object display */
410 /*****************************************************************************/
411 vector<ntlGeometryObject*> LbmFsgrSolver::getDebugObjects() { 
412         vector<ntlGeometryObject*> debo; 
413         if(this->mOutputSurfacePreview) {
414                 debo.push_back( mpPreviewSurface );
415         }
416 #if LBM_INCLUDE_TESTSOLVERS==1
417         if(mUseTestdata) {
418                 vector<ntlGeometryObject*> tdebo; 
419                 tdebo = mpTest->getDebugObjects();
420                 for(size_t i=0; i<tdebo.size(); i++) debo.push_back( tdebo[i] );
421         }
422 #endif // ELBEEM_PLUGIN
423         return debo; 
424 }
425
426 /******************************************************************************
427  * particle handling
428  *****************************************************************************/
429
430 /*! init particle positions */
431 int LbmFsgrSolver::initParticles() { 
432   int workSet = mLevel[mMaxRefine].setCurr;
433   int tries = 0;
434   int num = 0;
435         ParticleTracer *partt = mpParticles;
436
437   partt->setStart( this->mvGeoStart + ntlVec3Gfx(mLevel[mMaxRefine].nodeSize*0.5) );
438   partt->setEnd  ( this->mvGeoEnd   + ntlVec3Gfx(mLevel[mMaxRefine].nodeSize*0.5) );
439
440   partt->setSimStart( ntlVec3Gfx(0.0) );
441   partt->setSimEnd  ( ntlVec3Gfx(mSizex,   mSizey,   getForZMaxBnd(mMaxRefine)) );
442   
443   while( (num<partt->getNumInitialParticles()) && (tries<100*partt->getNumInitialParticles()) ) {
444     LbmFloat x,y,z,t;
445     x = 1.0+(( (LbmFloat)(mSizex-3.) )          * (rand()/(RAND_MAX+1.0)) );
446     y = 1.0+(( (LbmFloat)(mSizey-3.) )          * (rand()/(RAND_MAX+1.0)) );
447     z = 1.0+(( (LbmFloat) getForZMax1(mMaxRefine)-2. )* (rand()/(RAND_MAX+1.0)) );
448     int i = (int)(x+0.5);
449     int j = (int)(y+0.5);
450     int k = (int)(z+0.5);
451     if(LBMDIM==2) {
452       k = 0; z = 0.5; // place in the middle of domain
453     }
454
455     //if( RFLAG(mMaxRefine, i,j,k, workSet)& (CFFluid) ) 
456     //&& ( RFLAG(mMaxRefine, i,j,k, workSet)& CFNoNbFluid ) 
457     //if( RFLAG(mMaxRefine, i,j,k, workSet) & (CFFluid|CFInter|CFMbndInflow) ) { 
458     if( RFLAG(mMaxRefine, i,j,k, workSet) & (CFNoBndFluid|CFUnused) ) { 
459                         bool cellOk = true;
460                         //? FORDF1 { if(!(RFLAG_NB(mMaxRefine,i,j,k,workSet, l) & CFFluid)) cellOk = false; }
461                         if(!cellOk) continue;
462       // in fluid...
463       partt->addParticle(x,y,z);
464                         partt->getLast()->setStatus(PART_IN);
465                         partt->getLast()->setType(PART_TRACER);
466
467                         partt->getLast()->setSize(1.);
468                         // randomize size
469                         partt->getLast()->setSize(0.5 + (rand()/(RAND_MAX+1.0)));
470
471                         if( ( partt->getInitStart()>0.)
472                                         && ( partt->getInitEnd()>0.)
473                                         && ( partt->getInitEnd()>partt->getInitStart() )) {
474                 t = partt->getInitStart()+ (partt->getInitEnd()-partt->getInitStart())*(rand()/(RAND_MAX+1.0));
475                                 partt->getLast()->setLifeTime( -t );
476                         }
477       num++;
478     }
479     tries++;
480   } // */
481
482         /*FSGR_FORIJK1(mMaxRefine) {
483                 if( (RFLAG(mMaxRefine,i,j,k, workSet) & (CFNoBndFluid)) ) {
484         LbmFloat rndn = (rand()/(RAND_MAX+1.0));
485                         if(rndn>0.0) {
486                                 ntlVec3Gfx pos( (LbmFloat)(i)-0.5, (LbmFloat)(j)-0.5, (LbmFloat)(k)-0.5 );
487                                 if(LBMDIM==2) { pos[2]=0.5; }
488                                 partt->addParticle( pos[0],pos[1],pos[2] );
489                                 partt->getLast()->setStatus(PART_IN);
490                                 partt->getLast()->setType(PART_TRACER);
491                                 partt->getLast()->setSize(1.0);
492                         }
493                 }
494         } // */
495
496
497         // DEBUG TEST
498 #if LBM_INCLUDE_TESTSOLVERS==1
499         if(mUseTestdata) { 
500                 const bool partDebug=false;
501                 if(mpTest->mPartTestcase==0){ errMsg("LbmTestdata"," part init "<<mpTest->mPartTestcase); }
502                 if(mpTest->mPartTestcase==-12){ 
503                         const int lev = mMaxRefine;
504                         for(int i=5;i<15;i++) {
505                                 LbmFloat x,y,z;
506                                 y = 0.5+(LbmFloat)(i);
507                                 x = mLevel[lev].lSizex/20.0*10.0;
508                                 z = mLevel[lev].lSizez/20.0*2.0;
509                                 partt->addParticle(x,y,z);
510                                 partt->getLast()->setStatus(PART_IN);
511                                 partt->getLast()->setType(PART_BUBBLE);
512                                 partt->getLast()->setSize(  (-4.0+(LbmFloat)i)/1.0  );
513                                 if(partDebug) errMsg("PARTTT","SET "<<PRINT_VEC(x,y,z)<<" p"<<partt->getLast()->getPos() <<" s"<<partt->getLast()->getSize() );
514                         }
515                 }
516                 if(mpTest->mPartTestcase==-11){ 
517                         const int lev = mMaxRefine;
518                         for(int i=5;i<15;i++) {
519                                 LbmFloat x,y,z;
520                                 y = 10.5+(LbmFloat)(i);
521                                 x = mLevel[lev].lSizex/20.0*10.0;
522                                 z = mLevel[lev].lSizez/20.0*40.0;
523                                 partt->addParticle(x,y,z);
524                                 partt->getLast()->setStatus(PART_IN);
525                                 partt->getLast()->setType(PART_DROP);
526                                 partt->getLast()->setSize(  (-4.0+(LbmFloat)i)/1.0  );
527                                 if(partDebug) errMsg("PARTTT","SET "<<PRINT_VEC(x,y,z)<<" p"<<partt->getLast()->getPos() <<" s"<<partt->getLast()->getSize() );
528                         }
529                 }
530                 // place floats on rectangular region FLOAT_JITTER_BND
531                 if(mpTest->mPartTestcase==-10){ 
532                         const int lev = mMaxRefine;
533                         const int sx = mLevel[lev].lSizex;
534                         const int sy = mLevel[lev].lSizey;
535                         //for(int j=-(int)(sy*0.25);j<-(int)(sy*0.25)+2;++j) { for(int i=-(int)(sx*0.25);i<-(int)(sy*0.25)+2;++i) {
536                         //for(int j=-(int)(sy*1.25);j<(int)(2.25*sy);++j) { for(int i=-(int)(sx*1.25);i<(int)(2.25*sx);++i) {
537                         for(int j=-(int)(sy*0.3);j<(int)(1.3*sy);++j) { for(int i=-(int)(sx*0.3);i<(int)(1.3*sx);++i) {
538                         //for(int j=-(int)(sy*0.2);j<(int)(0.2*sy);++j) { for(int i= (int)(sx*0.5);i<= (int)(0.51*sx);++i) {
539                                         LbmFloat x,y,z;
540                                         x = 0.0+(LbmFloat)(i);
541                                         y = 0.0+(LbmFloat)(j);
542                                         //z = mLevel[lev].lSizez/10.0*2.5 - 1.0;
543                                         z = mLevel[lev].lSizez/20.0*9.5 - 1.0;
544                                         //z = mLevel[lev].lSizez/20.0*4.5 - 1.0;
545                                         partt->addParticle(x,y,z);
546                                         //if( (i>0)&&(i<sx) && (j>0)&&(j<sy) ) { partt->getLast()->setStatus(PART_IN); } else { partt->getLast()->setStatus(PART_OUT); }
547                                         partt->getLast()->setStatus(PART_IN);
548                                         partt->getLast()->setType(PART_FLOAT);
549                                         partt->getLast()->setSize( 15.0 );
550                                         if(partDebug) errMsg("PARTTT","SET "<<PRINT_VEC(x,y,z)<<" p"<<partt->getLast()->getPos() <<" s"<<partt->getLast()->getSize() );
551                          }
552                 }       }
553         } 
554         // DEBUG TEST
555 #endif // LBM_INCLUDE_TESTSOLVERS
556
557         
558   debMsgStd("LbmFsgrSolver::initParticles",DM_MSG,"Added "<<num<<" particles, genProb:"<<this->mPartGenProb<<", tries:"<<tries, 10);
559   if(num != partt->getNumParticles()) return 1;
560
561         return 0;
562 }
563
564 // helper function for particle debugging
565 /*static string getParticleStatusString(int state) {
566         std::ostringstream out;
567         if(state&PART_DROP)   out << "dropp ";
568         if(state&PART_TRACER) out << "tracr ";
569         if(state&PART_BUBBLE) out << "bubbl ";
570         if(state&PART_FLOAT)  out << "float ";
571         if(state&PART_INTER)  out << "inter ";
572
573         if(state&PART_IN)   out << "inn ";
574         if(state&PART_OUT)  out << "out ";
575         if(state&PART_INACTIVE)  out << "INACT ";
576         if(state&PART_OUTFLUID)  out << "outfluid ";
577         return out.str();
578 } // */
579
580 #define P_CHANGETYPE(p, newtype) \
581                 p->setLifeTime(0.); \
582     /* errMsg("PIT","U pit"<<(p)->getId()<<" pos:"<< (p)->getPos()<<" status:"<<convertFlags2String((p)->getFlags())<<" to "<< (newtype) ); */ \
583                 p->setType(newtype); 
584
585 // tracer defines
586 #define TRACE_JITTER 0.025
587 #define TRACE_RAND (rand()/(RAND_MAX+1.0))*TRACE_JITTER-(TRACE_JITTER*0.5)
588 #define FFGET_NORM(var,dl) \
589                                                         if(RFLAG_NB(lev,i,j,k,workSet, dl) &(CFInter)){ (var) = QCELL_NB(lev,i,j,k,workSet,dl,dFfrac); } \
590                                                         else if(RFLAG_NB(lev,i,j,k,workSet, dl) &(CFFluid|CFUnused)){ (var) = 1.; } else (var) = 0.0;
591
592 // float jitter
593 #define FLOAT_JITTER_BND (FLOAT_JITTER*2.0)
594 #define FLOAT_JITTBNDRAND(x) ((rand()/(RAND_MAX+1.0))*FLOAT_JITTER_BND*(1.-(x/(LbmFloat)maxdw))-(FLOAT_JITTER_BND*(1.-(x)/(LbmFloat)maxdw)*0.5)) 
595
596 #define DEL_PART { \
597         /*errMsg("PIT","DEL AT "<< __LINE__<<" type:"<<p->getType()<<"  ");  */ \
598         p->setActive( false ); \
599         continue; }
600
601 void LbmFsgrSolver::advanceParticles() { 
602   const int level = mMaxRefine;
603   const int workSet = mLevel[level].setCurr;
604         LbmFloat vx=0.0,vy=0.0,vz=0.0;
605         //int debugOutCounter=0; // debug output counter
606
607         myTime_t parttstart = getTime(); 
608         const LbmFloat cellsize = this->mpParam->getCellSize();
609         const LbmFloat timestep = this->mpParam->getTimestep();
610         //const LbmFloat viscAir = 1.79 * 1e-5; // RW2L kin. viscosity, mu
611         //const LbmFloat viscWater = 1.0 * 1e-6; // RW2L kin. viscosity, mu
612         const LbmFloat rhoAir = 1.2;  // [kg m^-3] RW2L
613         const LbmFloat rhoWater = 1000.0; // RW2L
614         const LbmFloat minDropSize = 0.0005; // [m], = 2mm  RW2L
615         const LbmVec   velAir(0.); // [m / s]
616
617         const LbmFloat r1 = 0.005;  // r max
618         const LbmFloat r2 = 0.0005; // r min
619         const LbmFloat v1 = 9.0; // v max
620         const LbmFloat v2 = 2.0; // v min
621         const LbmVec rwgrav = vec2L( this->mpParam->getGravity(mSimulationTime) );
622         const bool useff = (mFarFieldSize>1.2); // if(mpTest->mFarfMode>0){ 
623
624         // TODO scale bubble/part damping dep. on timestep, also drop bnd rev damping
625         const int cutval = mCutoff; // use full border!?
626         if(this->mStepCnt%50==49) { mpParticles->cleanup(); }
627   for(vector<ParticleObject>::iterator pit= mpParticles->getParticlesBegin();
628       pit!= mpParticles->getParticlesEnd(); pit++) {
629     //if((*pit).getPos()[2]>10.) errMsg("PIT"," pit"<<(*pit).getId()<<" pos:"<< (*pit).getPos()<<" status:["<<getParticleStatusString((*pit).getFlags())<<"] vel:"<< (*pit).getVel()  );
630     if( (*pit).getActive()==false ) continue;
631                 // skip until reached
632                 ParticleObject *p = &(*pit);
633                 if(p->getLifeTime()<0.){ 
634                         if(p->getLifeTime() < -mSimulationTime) continue; 
635                         else p->setLifeTime(-mLevel[level].timestep); // zero for following update
636                 }
637     int i,j,k;
638                 p->setLifeTime(p->getLifeTime()+mLevel[level].timestep);
639
640                 // nearest neighbor, particle positions don't include empty bounds
641                 ntlVec3Gfx pos = p->getPos();
642                 i= (int)pos[0]; j= (int)pos[1]; k= (int)pos[2];// no offset necessary
643                 if(LBMDIM==2) { k = 0; }
644
645                 // only testdata handling, all for sws
646 #if LBM_INCLUDE_TESTSOLVERS==1
647                 if(useff && (mpTest->mFarfMode>0)) {
648                         p->setStatus(PART_OUT);
649                         mpTest->handleParticle(p, i,j,k); continue;
650                 } 
651 #endif // LBM_INCLUDE_TESTSOLVERS==1
652
653                 // in out tests
654                 if(p->getStatus()&PART_IN) { // IN
655                         if( (i<cutval)||(i>mSizex-1-cutval)||
656                                         (j<cutval)||(j>mSizey-1-cutval)
657                                         //||(k<cutval)||(k>mSizez-1-cutval) 
658                                         ) {
659                                 if(!useff) { DEL_PART;
660                                 } else { 
661                                         p->setStatus(PART_OUT); 
662                                 }
663                         } 
664                 } else { // OUT rough check
665                         // check in again?
666                         if( (i>=cutval)&&(i<=mSizex-1-cutval)&&
667                                         (j>=cutval)&&(j<=mSizey-1-cutval)
668                                         ) {
669                                 p->setStatus(PART_IN);
670                         }
671                 }
672
673                 if( (p->getType()==PART_BUBBLE) ||
674                     (p->getType()==PART_TRACER) ) {
675
676                         // no interpol
677                         vx = vy = vz = 0.0;
678                         if(p->getStatus()&PART_IN) { // IN
679                                 if(k>=cutval) {
680                                         if(k>mSizez-1-cutval) DEL_PART; 
681
682                                         if( RFLAG(level, i,j,k, workSet)&(CFFluid|CFUnused) ) {
683                                                 // still ok
684                                                 int partLev = level;
685                                                 int si=i, sj=j, sk=k;
686                                                 while(partLev>0 && RFLAG(partLev, si,sj,sk, workSet)&(CFUnused)) {
687                                                         partLev--;
688                                                         si/=2;
689                                                         sj/=2;
690                                                         sk/=2;
691                                                 }
692                                                 // get velocity from fluid cell
693                                                 if( RFLAG(partLev, si,sj,sk, workSet)&(CFFluid) ) {
694                                                         LbmFloat *ccel = RACPNT(partLev, si,sj,sk, workSet);
695                                                         FORDF1{
696                                                                 LbmFloat cdf = RAC(ccel, l);
697                                                                 // TODO update below
698                                                                 vx  += (this->dfDvecX[l]*cdf); 
699                                                                 vy  += (this->dfDvecY[l]*cdf);  
700                                                                 vz  += (this->dfDvecZ[l]*cdf);  
701                                                         }
702                                                         // remove gravity influence
703                                                         const LbmFloat lesomega = mLevel[level].omega; // no les
704                                                         vx -= mLevel[level].gravity[0] * lesomega*0.5;
705                                                         vy -= mLevel[level].gravity[1] * lesomega*0.5;
706                                                         vz -= mLevel[level].gravity[2] * lesomega*0.5;
707                                                 } // fluid vel
708
709                                         } else { // OUT
710                                                 // out of bounds, deactivate...
711                                                 // FIXME make fsgr treatment
712                                                 if(p->getType()==PART_BUBBLE) { P_CHANGETYPE(p, PART_FLOAT ); continue; }
713                                         }
714                                 } else {
715                                         // below 3d region, just rise
716                                 }
717                         } else { // OUT
718 #                               if LBM_INCLUDE_TESTSOLVERS==1
719                                 if(useff) { mpTest->handleParticle(p, i,j,k); }
720                                 else DEL_PART;
721 #                               else // LBM_INCLUDE_TESTSOLVERS==1
722                                 DEL_PART;
723 #                               endif // LBM_INCLUDE_TESTSOLVERS==1
724                                 // TODO use x,y vel...?
725                         }
726
727                         ntlVec3Gfx v = p->getVel(); // dampen...
728                         if( (useff)&& (p->getType()==PART_BUBBLE) ) {
729                                 // test rise
730
731                                 if(mPartUsePhysModel) {
732                                         LbmFloat radius = p->getSize() * minDropSize;
733                                         LbmVec   velPart = vec2L(p->getVel()) *cellsize/timestep; // L2RW, lattice velocity
734                                         LbmVec   velWater = LbmVec(vx,vy,vz) *cellsize/timestep;// L2RW, fluid velocity
735                                         LbmVec   velRel = velWater - velPart;
736                                         //LbmFloat velRelNorm = norm(velRel);
737                                         LbmFloat pvolume = rhoAir * 4.0/3.0 * M_PI* radius*radius*radius; // volume: 4/3 pi r^3
738
739                                         LbmVec fb = -rwgrav* pvolume *rhoWater;
740                                         LbmVec fd = velRel*6.0*M_PI*radius* (1e-3); //viscWater;
741                                         LbmVec change = (fb+fd) *10.0*timestep  *(timestep/cellsize);
742                                         /*if(debugOutCounter<0) {
743                                                 errMsg("PIT","BTEST1   vol="<<pvolume<<" radius="<<radius<<" vn="<<velRelNorm<<" velPart="<<velPart<<" velRel"<<velRel);
744                                                 errMsg("PIT","BTEST2        cellsize="<<cellsize<<" timestep="<<timestep<<" viscW="<<viscWater<<" ss/mb="<<(timestep/(pvolume*rhoAir)));
745                                                 errMsg("PIT","BTEST2        grav="<<rwgrav<<"  " );
746                                                 errMsg("PIT","BTEST2        change="<<(change)<<" fb="<<(fb)<<" fd="<<(fd)<<" ");
747                                                 errMsg("PIT","BTEST2        change="<<norm(change)<<" fb="<<norm(fb)<<" fd="<<norm(fd)<<" ");
748                                         } // DEBUG */
749                                                 
750                                         LbmVec fd2 = (LbmVec(vx,vy,vz)-vec2L(p->getVel())) * 6.0*M_PI*radius* (1e-3); //viscWater;
751                                         LbmFloat w = 0.99;
752                                         vz = (1.0-w)*vz + w*(p->getVel()[2]-0.5*(p->getSize()/5.0)*mLevel[level].gravity[2]);
753                                         v = ntlVec3Gfx(vx,vy,vz)+vec2G(fd2);
754                                         p->setVel( v );
755                                 } else {
756                                         // non phys, half old, half fluid, use slightly slower acc
757                                         v = v*0.5 + ntlVec3Gfx(vx,vy,vz)* 0.5-vec2G(mLevel[level].gravity)*0.5;
758                                         p->setVel( v * 0.99 );
759                                 }
760                                 p->advanceVel();
761
762                         } else if(p->getType()==PART_TRACER) {
763                                 v = ntlVec3Gfx(vx,vy,vz);
764                                 CellFlagType fflag = RFLAG(level, i,j,k, workSet);
765
766                                 if(fflag&(CFFluid|CFInter) ) { p->setInFluid(true);
767                                 } else { p->setInFluid(false); }
768
769                                 if( (( fflag&CFFluid ) && ( fflag&CFNoBndFluid )) ||
770                                                 (( fflag&CFInter ) && (!(fflag&CFNoNbFluid)) ) ) {
771                                         // only real fluid
772 #                                       if LBMDIM==3
773                                         p->advance( TRACE_RAND,TRACE_RAND,TRACE_RAND);
774 #                                       else
775                                         p->advance( TRACE_RAND,TRACE_RAND, 0.);
776 #                                       endif
777
778                                 } else {
779                                         // move inwards along normal, make sure normal is valid first
780                                         // todo use class funcs!
781                                         const int lev = level;
782                                         LbmFloat nx=0.,ny=0.,nz=0., nv1,nv2;
783                                         bool nonorm = false;
784                                         if(i<=0)              { nx = -1.; nonorm = true; }
785                                         if(i>=mSizex-1) { nx =  1.; nonorm = true; }
786                                         if(j<=0)              { ny = -1.; nonorm = true; }
787                                         if(j>=mSizey-1) { ny =  1.; nonorm = true; }
788 #                                       if LBMDIM==3
789                                         if(k<=0)              { nz = -1.; nonorm = true; }
790                                         if(k>=mSizez-1) { nz =  1.; nonorm = true; }
791 #                                       endif // LBMDIM==3
792                                         if(!nonorm) {
793                                                 FFGET_NORM(nv1,dE); FFGET_NORM(nv2,dW);
794                                                 nx = 0.5* (nv2-nv1);
795                                                 FFGET_NORM(nv1,dN); FFGET_NORM(nv2,dS);
796                                                 ny = 0.5* (nv2-nv1);
797 #                                               if LBMDIM==3
798                                                 FFGET_NORM(nv1,dT); FFGET_NORM(nv2,dB);
799                                                 nz = 0.5* (nv2-nv1);
800 #                                               else // LBMDIM==3
801                                                 nz = 0.;
802 #                                               endif // LBMDIM==3
803                                         } else {
804                                                 v = p->getVel() + vec2G(mLevel[level].gravity);
805                                         }
806                                         p->advanceVec( (ntlVec3Gfx(nx,ny,nz)) * -0.1 ); // + vec2G(mLevel[level].gravity);
807                                 }
808                         }
809
810                         p->setVel( v );
811                         p->advanceVel();
812                 } 
813
814                 // drop handling
815                 else if(p->getType()==PART_DROP) {
816                         ntlVec3Gfx v = p->getVel(); // dampen...
817
818                         if(mPartUsePhysModel) {
819                                 LbmFloat radius = p->getSize() * minDropSize;
820                                 LbmVec   velPart = vec2L(p->getVel()) *cellsize /timestep; // * cellsize / timestep; // L2RW, lattice velocity
821                                 LbmVec   velRel = velAir - velPart;
822                                 //LbmVec   velRelLat = velRel /cellsize*timestep; // L2RW
823                                 LbmFloat velRelNorm = norm(velRel);
824                                 // TODO calculate values in lattice units, compute CD?!??!
825                                 LbmFloat mb = rhoWater * 4.0/3.0 * M_PI* radius*radius*radius; // mass: 4/3 pi r^3 rho
826                                 const LbmFloat rw = (r1-radius)/(r1-r2);
827                                 const LbmFloat rmax = (0.5 + 0.5*rw);
828                                 const LbmFloat vmax = (v2 + (v1-v2)* (1.0-rw) );
829                                 const LbmFloat cd = (rmax) * (velRelNorm)/(vmax);
830
831                                 LbmVec fg = rwgrav * mb;//  * (1.0-rhoAir/rhoWater);
832                                 LbmVec fd = velRel* velRelNorm* cd*M_PI *rhoAir *0.5 *radius*radius;
833                                 LbmVec change = (fg+   fd ) *timestep / mb  *(timestep/cellsize);
834                                 //if(k>0) { errMsg("\nPIT","NTEST1   mb="<<mb<<" radius="<<radius<<" vn="<<velRelNorm<<" velPart="<<velPart<<" velRel"<<velRel<<" pgetVel="<<p->getVel() ); }
835
836                                 v += vec2G(change);
837                                 p->setVel(v); 
838                                 // NEW
839                         } else {
840                                 p->setVel( v ); 
841                                 int gravk = (int)(p->getPos()[2]+mLevel[level].gravity[2]);
842                                 if(gravk>=0 && gravk<mSizez && RFLAG(level, i,j,gravk, workSet)&CFBnd) {
843                                         // dont add for "resting" parts
844                                         v[2] = 0.;
845                                         p->setVel( v*0.9 ); // restdamping
846                                 } else {
847                                         p->addToVel( vec2G(mLevel[level].gravity) );
848                                 }
849                         } // OLD
850                         p->advanceVel();
851
852                         if(p->getStatus()&PART_IN) { // IN
853                                 if(k<cutval) { DEL_PART; continue; }
854                                 if(k<=mSizez-1-cutval){ 
855                                         CellFlagType pflag = RFLAG(level, i,j,k, workSet);
856                                         //errMsg("PIT move"," at "<<PRINT_IJK<<" flag"<<convertCellFlagType2String(pflag) );
857                                         if(pflag & (CFBnd)) {
858                                                 handleObstacleParticle(p);
859                                                 continue;
860                                         } else if(pflag & (CFEmpty)) {
861                                                 // still ok
862                                         } else if((pflag & CFInter) 
863                                                   //&&(!(RFLAG(level, i,j,k, workSet)& CFNoNbFluid)) 
864                                                                                 ) {
865                                                 // add to no nb fluid i.f.'s, so skip if interface with fluid nb
866                                         } else if(pflag  & (CFFluid|CFUnused|CFInter) ){ // interface cells ignored here due to previous check!
867                                                 // add dropmass again, (these are only interf. with nonbfl.)
868                                                 int oi= (int)(pos[0]-1.25*v[0]+0.5);
869                                                 int oj= (int)(pos[1]-1.25*v[1]+0.5);
870                                                 int ok= (int)(pos[2]-1.25*v[2]+0.5);
871                                                 const LbmFloat size = p->getSize();
872                                                 const LbmFloat dropmass = ParticleObject::getMass(mPartDropMassSub*size);
873                                                 bool orgcellok = false;
874                                                 if( (oi<0)||(oi>mSizex-1)||
875                                                     (oj<0)||(oj>mSizey-1)||
876                                                     (ok<0)||(ok>mSizez-1) ) {
877                                                         // org cell not ok!
878                                                 } else if( RFLAG(level, oi,oj,ok, workSet) & (CFInter) ){
879                                                         orgcellok = true;
880                                                 } else {
881                                                         // search upward for interface
882                                                         oi=i; oj=j; ok=k;
883                                                         for(int kk=0; kk<5 && ok<=mSizez-2; kk++) {
884                                                                 ok++; // check sizez-2 due to this increment!
885                                                                 if( RFLAG(level, oi,oj,ok, workSet) & (CFInter) ){
886                                                                         kk = 5; orgcellok = true;
887                                                                 }
888                                                         }
889                                                 }
890
891                                                 //errMsg("PTIMPULSE"," new v"<<v<<" at "<<PRINT_VEC(oi,oj,ok)<<" , was "<<PRINT_VEC(i,j,k)<<" ok "<<orgcellok );
892                                                 if(orgcellok) {
893                                                         QCELL(level, oi,oj,ok, workSet, dMass) += dropmass;
894                                                         QCELL(level, oi,oj,ok, workSet, dFfrac) += dropmass; // assume rho=1?
895
896                                                         if(RFLAG(level, oi,oj,ok, workSet) & CFNoBndFluid){
897                                                         // check speed, perhaps normalize
898                                                         gfxReal vlensqr = normNoSqrt(v);
899                                                         if(vlensqr > 0.166*0.166) {
900                                                                 v *= 1./sqrtf((float)vlensqr)*0.166;
901                                                         }
902                                                         // compute cell velocity
903                                                         LbmFloat *tcel = RACPNT(level, oi,oj,ok, workSet);
904                                                         LbmFloat velUx=0., velUy=0., velUz=0.;
905                                                         FORDF0 { 
906                                                                 velUx  += (this->dfDvecX[l]*RAC(tcel,l));
907                                                                 velUy  += (this->dfDvecY[l]*RAC(tcel,l)); 
908                                                                 velUz  += (this->dfDvecZ[l]*RAC(tcel,l)); 
909                                                         }
910                                                         // add impulse
911                                                         /*
912                                                         LbmFloat cellVelSqr = velUx*velUx+ velUy*velUy+ velUz*velUz;
913                                                         //errMsg("PTIMPULSE"," new v"<<v<<" cvs"<<cellVelSqr<<"="<<sqrt(cellVelSqr));
914                                                         if(cellVelSqr< 0.166*0.166) {
915                                                                 FORDF1 { 
916                                                                         const LbmFloat add = 3. * dropmass * this->dfLength[l]*(v[0]*this->dfDvecX[l]+v[1]*this->dfDvecY[l]+v[2]*this->dfDvecZ[l]);
917                                                                         RAC(tcel,l) += add;
918                                                                 } } // */
919                                                         } // only add impulse away from obstacles! 
920                                                 } // orgcellok
921
922                                                 // FIXME make fsgr treatment
923                                                 P_CHANGETYPE(p, PART_FLOAT ); continue; 
924                                                 // jitter in cell to prevent stacking when hitting a steep surface
925                                                 ntlVec3Gfx cpos = p->getPos(); 
926                                                 cpos[0] += (rand()/(RAND_MAX+1.0))-0.5;
927                                                 cpos[1] += (rand()/(RAND_MAX+1.0))-0.5; 
928                                                 cpos[2] += (rand()/(RAND_MAX+1.0))-0.5; 
929                                                 p->setPos(cpos);
930                                         } else {
931                                                 DEL_PART;
932                                                 this->mNumParticlesLost++;
933                                         }
934                                 }
935                         } else { // OUT
936 #                               if LBM_INCLUDE_TESTSOLVERS==1
937                                 if(useff) { mpTest->handleParticle(p, i,j,k); }
938                                 else{ DEL_PART; }
939 #                               else // LBM_INCLUDE_TESTSOLVERS==1
940                                 DEL_PART; 
941 #                               endif // LBM_INCLUDE_TESTSOLVERS==1
942                         }
943
944                 } // air particle
945
946                 // inter particle
947                 else if(p->getType()==PART_INTER) {
948                         // unused!?
949                         if(p->getStatus()&PART_IN) { // IN
950                                 if((k<cutval)||(k>mSizez-1-cutval)) {
951                                         // undecided particle above or below... remove?
952                                         DEL_PART; 
953                                 }
954
955                                 CellFlagType pflag = RFLAG(level, i,j,k, workSet);
956                                 if(pflag& CFInter ) {
957                                         // still ok
958                                 } else if(pflag& (CFFluid|CFUnused) ) {
959                                         P_CHANGETYPE(p, PART_FLOAT ); continue;
960                                 } else if(pflag& CFEmpty ) {
961                                         P_CHANGETYPE(p, PART_DROP ); continue;
962                                 } else if(pflag& CFBnd ) {
963                                         P_CHANGETYPE(p, PART_FLOAT ); continue;
964                                 }
965                         } else { // OUT
966                                 // undecided particle outside... remove?
967                                 DEL_PART; 
968                         }
969                 }
970
971                 // float particle
972                 else if(p->getType()==PART_FLOAT) {
973
974                         if(p->getStatus()&PART_IN) { // IN
975                                 if(k<cutval) DEL_PART; 
976                                 // not valid for mass... 
977                                 vx = vy = vz = 0.0;
978
979                                 // define from particletracer.h
980 #if MOVE_FLOATS==1
981                                 const int DEPTH_AVG=3; // only average interface vels
982                                 int ccnt=0;
983                                 for(int kk=0;kk<DEPTH_AVG;kk+=1) {
984                                         if((k-kk)<1) continue;
985                                         if(RFLAG(level, i,j,k, workSet)&(CFInter)) {} else continue;
986                                         ccnt++;
987                                         FORDF1{
988                                                 LbmFloat cdf = QCELL(level, i,j,k-kk, workSet, l);
989                                                 vx  += (this->dfDvecX[l]*cdf); 
990                                                 vy  += (this->dfDvecY[l]*cdf);  
991                                                 vz  += (this->dfDvecZ[l]*cdf);  
992                                         }
993                                 }
994                                 if(ccnt) {
995                                 // use halved surface velocity (todo, use omega instead)
996                                 vx /=(LbmFloat)(ccnt * 2.0); // half xy speed! value2
997                                 vy /=(LbmFloat)(ccnt * 2.0);
998                                 vz /=(LbmFloat)(ccnt); }
999 #else // MOVE_FLOATS==1
1000                                 vx=vy=0.; //p->setVel(ntlVec3Gfx(0.) ); // static_float
1001 #endif // MOVE_FLOATS==1
1002                                 vx += (rand()/(RAND_MAX+1.0))*(FLOAT_JITTER*0.2)-(FLOAT_JITTER*0.2*0.5);
1003                                 vy += (rand()/(RAND_MAX+1.0))*(FLOAT_JITTER*0.2)-(FLOAT_JITTER*0.2*0.5);
1004
1005                                 //bool delfloat = false;
1006                                 if( ( RFLAG(level, i,j,k, workSet)& (CFFluid|CFUnused) ) ) {
1007                                         // in fluid cell
1008                                         vz = p->getVel()[2]-1.0*mLevel[level].gravity[2]; // simply rise...
1009                                         if(vz<0.) vz=0.;
1010                                 } else if( ( RFLAG(level, i,j,k, workSet)& CFBnd ) ) {
1011                                         // force downwards movement, move below obstacle...
1012                                         //vz = p->getVel()[2]+1.0*mLevel[level].gravity[2]; // fall...
1013                                         //if(vz>0.) vz=0.;
1014                                         DEL_PART; 
1015                                 } else if( ( RFLAG(level, i,j,k, workSet)& CFInter ) ) {
1016                                         // keep in interface , one grid cell offset is added in part. gen
1017                                 } else { // all else...
1018                                         if( ( RFLAG(level, i,j,k-1, workSet)& (CFFluid|CFInter) ) ) {
1019                                                 vz = p->getVel()[2]+2.0*mLevel[level].gravity[2]; // fall...
1020                                                 if(vz>0.) vz=0.; }
1021                                         else { DEL_PART; }
1022                                 }
1023
1024                                 p->setVel( vec2G( ntlVec3Gfx(vx,vy,vz) ) ); //?
1025                                 p->advanceVel();
1026                         } else {
1027 #if LBM_INCLUDE_TESTSOLVERS==1
1028                                 if(useff) { mpTest->handleParticle(p, i,j,k); }
1029                                 else DEL_PART; 
1030 #else // LBM_INCLUDE_TESTSOLVERS==1
1031                                 DEL_PART; 
1032 #endif // LBM_INCLUDE_TESTSOLVERS==1
1033                         }
1034                                 
1035                         // additional bnd jitter
1036                         if((0) && (useff) && (p->getLifeTime()<3.*mLevel[level].timestep)) {
1037                                 // use half butoff border 1/8
1038                                 int maxdw = (int)(mLevel[level].lSizex*0.125*0.5);
1039                                 if(maxdw<3) maxdw=3;
1040                                 if((j>=0)&&(j<=mSizey-1)) {
1041                                         if(ABS(i-(               cutval))<maxdw) { p->advance(  FLOAT_JITTBNDRAND( ABS(i-(               cutval))), 0.,0.); }
1042                                         if(ABS(i-(mSizex-1-cutval))<maxdw) { p->advance(  FLOAT_JITTBNDRAND( ABS(i-(mSizex-1-cutval))), 0.,0.); }
1043                                 }
1044                         }
1045                 }  // PART_FLOAT
1046                 
1047                 // unknown particle type        
1048                 else {
1049                         errMsg("LbmFsgrSolver::advanceParticles","PIT pit invalid type!? "<<p->getStatus() );
1050                 }
1051   }
1052         myTime_t parttend = getTime(); 
1053         debMsgStd("LbmFsgrSolver::advanceParticles",DM_MSG,"Time for particle update:"<< getTimeString(parttend-parttstart)<<", #particles:"<<mpParticles->getNumParticles() , 10 );
1054 }
1055
1056 void LbmFsgrSolver::notifySolverOfDump(int dumptype, int frameNr,char *frameNrStr,string outfilename) {
1057         int workSet = mLevel[mMaxRefine].setCurr;
1058         std::ostringstream name;
1059
1060         // debug - raw dump of ffrac values, as text!
1061         if(mDumpRawText) { 
1062                 name << outfilename<< frameNrStr <<".dump";
1063                 FILE *file = fopen(name.str().c_str(),"w");
1064                 if(file) {
1065
1066                         for(int k= getForZMinBnd(); k< getForZMaxBnd(mMaxRefine); ++k)  {
1067                                 for(int j=0;j<mLevel[mMaxRefine].lSizey-0;j++)  {
1068                                         for(int i=0;i<mLevel[mMaxRefine].lSizex-0;i++) {
1069                                                 float val = 0.;
1070                                                 if(RFLAG(mMaxRefine, i,j,k, workSet) & CFInter) {
1071                                                         val = QCELL(mMaxRefine,i,j,k, mLevel[mMaxRefine].setCurr,dFfrac);
1072                                                         if(val<0.) val=0.;
1073                                                         if(val>1.) val=1.;
1074                                                 }
1075                                                 if(RFLAG(mMaxRefine, i,j,k, workSet) & CFFluid) val = 1.;
1076                                                 fprintf(file, "%f ",val); // text
1077                                                 //errMsg("W", PRINT_IJK<<" val:"<<val);
1078                                         }
1079                                         fprintf(file, "\n"); // text
1080                                 }
1081                                 fprintf(file, "\n"); // text
1082                         }
1083                         fclose(file);
1084
1085                 } // file
1086         } // */
1087
1088         if(mDumpRawBinary) {
1089                 if(!mDumpRawBinaryZip) {
1090                         // unzipped, only fill
1091                         name << outfilename<< frameNrStr <<".bdump";
1092                         FILE *file = fopen(name.str().c_str(),"w");
1093                         if(file) {
1094                                 for(int k= getForZMinBnd(); k< getForZMaxBnd(mMaxRefine); ++k)  {
1095                                         for(int j=0;j<mLevel[mMaxRefine].lSizey-0;j++)  {
1096                                                 for(int i=0;i<mLevel[mMaxRefine].lSizex-0;i++) {
1097                                                         float val = 0.;
1098                                                         if(RFLAG(mMaxRefine, i,j,k, workSet) & CFInter) {
1099                                                                 val = QCELL(mMaxRefine,i,j,k, mLevel[mMaxRefine].setCurr,dFfrac);
1100                                                                 if(val<0.) val=0.;
1101                                                                 if(val>1.) val=1.;
1102                                                         }
1103                                                         if(RFLAG(mMaxRefine, i,j,k, workSet) & CFFluid) val = 1.;
1104                                                         fwrite( &val, sizeof(val), 1, file); // binary
1105                                                 }
1106                                         }
1107                                 }
1108                                 fclose(file);
1109                         } // file
1110                 } // unzipped
1111                 else {
1112                         // zipped, use iso values
1113                         prepareVisualization();
1114                         name << outfilename<< frameNrStr <<".bdump.gz";
1115                         gzFile gzf = gzopen(name.str().c_str(),"wb9");
1116                         if(gzf) {
1117                                 // write size
1118                                 int s;
1119                                 s=mSizex;       gzwrite(gzf, &s, sizeof(s));
1120                                 s=mSizey;       gzwrite(gzf, &s, sizeof(s));
1121                                 s=mSizez;       gzwrite(gzf, &s, sizeof(s));
1122
1123                                 // write isovalues
1124                                 for(int k= getForZMinBnd(); k< getForZMaxBnd(mMaxRefine); ++k)  {
1125                                         for(int j=0;j<mLevel[mMaxRefine].lSizey;j++)  {
1126                                                 for(int i=0;i<mLevel[mMaxRefine].lSizex;i++) {
1127                                                         float val = 0.;
1128                                                         val = *mpIso->lbmGetData( i,j,k );
1129                                                         gzwrite(gzf, &val, sizeof(val));
1130                                                 }
1131                                         }
1132                                 }
1133                                 gzclose(gzf);
1134                         } // gzf
1135                 } // zip
1136         } // bin dump
1137
1138         dumptype = 0; frameNr = 0; // get rid of warning
1139 }
1140
1141 /*! move a particle at a boundary */
1142 void LbmFsgrSolver::handleObstacleParticle(ParticleObject *p) {
1143         //if(normNoSqrt(v)<=0.) continue; // skip stuck
1144         /*
1145                  p->setVel( v * -1. ); // revert
1146                  p->advanceVel(); // move back twice...
1147                  if( RFLAG(mMaxRefine, i,j,k, workSet)& (CFBndNoslip)) {
1148                  p->setVel( v * -0.5 ); // revert & dampen
1149                  }
1150                  p->advanceVel();  
1151         // */
1152         // TODO mark/remove stuck parts!?
1153
1154         const int level = mMaxRefine;
1155         const int workSet = mLevel[level].setCurr;
1156         LbmVec v = vec2L( p->getVel() );
1157         if(normNoSqrt(v)<=0.) { 
1158                 p->setVel(vec2G(mLevel[level].gravity)); 
1159         }
1160
1161         CellFlagType pflag = CFBnd;
1162         ntlVec3Gfx posOrg(p->getPos());
1163         ntlVec3Gfx npos(0.);
1164         int ni=1,nj=1,nk=1;
1165         int tries = 0;
1166
1167         // try to undo movement
1168         p->advanceVec( (p->getVel()-vec2G(mLevel[level].gravity)) * -2.);  
1169
1170         npos = p->getPos(); ni= (int)npos[0]; 
1171         nj= (int)npos[1]; nk= (int)npos[2];
1172         if(LBMDIM==2) { nk = 0; }
1173         //errMsg("BOUNDCPAR"," t"<<PRINT_VEC(ni,nj,nk)<<" v"<<v<<" p"<<npos);
1174
1175         // delete out of domain
1176         if(!checkDomainBounds(level,ni,nj,nk)) {
1177                 //errMsg("BOUNDCPAR"," DEL! ");
1178                 p->setActive( false ); 
1179                 return;
1180         }
1181         pflag =  RFLAG(level, ni,nj,nk, workSet);
1182         
1183         // try to force particle out of boundary
1184         bool haveNorm = false;
1185         LbmVec bnormal;
1186         if(pflag&CFBnd) {
1187                 npos = posOrg; ni= (int)npos[0]; 
1188                 nj= (int)npos[1]; nk= (int)npos[2];
1189                 if(LBMDIM==2) { nk = 0; }
1190
1191                 computeObstacleSurfaceNormalAcc(ni,nj,nk, &bnormal[0]);
1192                 haveNorm = true;
1193                 normalize(bnormal);
1194                 bnormal *= 0.25;
1195
1196                 tries = 1;
1197                 while(pflag&CFBnd && tries<=5) {
1198                         // use increasing step sizes
1199                         p->advanceVec( vec2G( bnormal *0.5 *(gfxReal)tries ) );  
1200                         npos = p->getPos();
1201                         ni= (int)npos[0]; 
1202                         nj= (int)npos[1]; 
1203                         nk= (int)npos[2];
1204
1205                         // delete out of domain
1206                         if(!checkDomainBounds(level,ni,nj,nk)) {
1207                                 //errMsg("BOUNDCPAR"," DEL! ");
1208                                 p->setActive( false ); 
1209                                 return;
1210                         }
1211                         pflag =  RFLAG(level, ni,nj,nk, workSet);
1212                         tries++;
1213                 }
1214
1215                 // really stuck, delete...
1216                 if(pflag&CFBnd) {
1217                         p->setActive( false ); 
1218                         return;
1219                 }
1220         }
1221
1222         // not in bound anymore!
1223         if(!haveNorm) {
1224                 CellFlagType *bflag = &RFLAG(level, ni,nj,nk, workSet);
1225                 LbmFloat     *bcell = RACPNT(level, ni,nj,nk, workSet);
1226                 computeObstacleSurfaceNormal(bcell,bflag, &bnormal[0]);
1227         }
1228         normalize(bnormal);
1229         LbmVec normComp = bnormal * dot(vec2L(v),bnormal);
1230         //errMsg("BOUNDCPAR","bnormal"<<bnormal<<" normComp"<<normComp<<" newv"<<(v-normComp) );
1231         v = (v-normComp)*0.9; // only move tangential
1232         v *= 0.9; // restdamping , todo use timestep
1233         p->setVel(vec2G(v));
1234         p->advanceVel();
1235 }
1236
1237 /*****************************************************************************/
1238 /*! internal quick print function (for debugging) */
1239 /*****************************************************************************/
1240 void 
1241 LbmFsgrSolver::printLbmCell(int level, int i, int j, int k, int set) {
1242         stdCellId *newcid = new stdCellId;
1243         newcid->level = level;
1244         newcid->x = i;
1245         newcid->y = j;
1246         newcid->z = k;
1247
1248         // this function is not called upon clicking, then its from setMouseClick
1249         debugPrintNodeInfo( newcid, set );
1250         delete newcid;
1251 }
1252 void 
1253 LbmFsgrSolver::debugMarkCellCall(int level, int vi,int vj,int vk) {
1254         stdCellId *newcid = new stdCellId;
1255         newcid->level = level;
1256         newcid->x = vi;
1257         newcid->y = vj;
1258         newcid->z = vk;
1259         this->addCellToMarkedList( newcid );
1260 }
1261
1262                 
1263 /*****************************************************************************/
1264 // implement CellIterator<UniformFsgrCellIdentifier> interface
1265 /*****************************************************************************/
1266
1267
1268
1269 // values from guiflkt.cpp
1270 extern double guiRoiSX, guiRoiSY, guiRoiSZ, guiRoiEX, guiRoiEY, guiRoiEZ;
1271 extern int guiRoiMaxLev, guiRoiMinLev;
1272 #define CID_SX (int)( (mLevel[cid->level].lSizex-1) * guiRoiSX )
1273 #define CID_SY (int)( (mLevel[cid->level].lSizey-1) * guiRoiSY )
1274 #define CID_SZ (int)( (mLevel[cid->level].lSizez-1) * guiRoiSZ )
1275
1276 #define CID_EX (int)( (mLevel[cid->level].lSizex-1) * guiRoiEX )
1277 #define CID_EY (int)( (mLevel[cid->level].lSizey-1) * guiRoiEY )
1278 #define CID_EZ (int)( (mLevel[cid->level].lSizez-1) * guiRoiEZ )
1279
1280 CellIdentifierInterface* 
1281 LbmFsgrSolver::getFirstCell( ) {
1282         int level = mMaxRefine;
1283
1284 #if LBMDIM==3
1285         if(mMaxRefine>0) { level = mMaxRefine-1; } // NO1HIGHESTLEV DEBUG
1286 #endif
1287         level = guiRoiMaxLev;
1288         if(level>mMaxRefine) level = mMaxRefine;
1289         
1290         //errMsg("LbmFsgrSolver::getFirstCell","Celliteration started...");
1291         stdCellId *cid = new stdCellId;
1292         cid->level = level;
1293         cid->x = CID_SX;
1294         cid->y = CID_SY;
1295         cid->z = CID_SZ;
1296         return cid;
1297 }
1298
1299 LbmFsgrSolver::stdCellId* 
1300 LbmFsgrSolver::convertBaseCidToStdCid( CellIdentifierInterface* basecid) {
1301         //stdCellId *cid = dynamic_cast<stdCellId*>( basecid );
1302         stdCellId *cid = (stdCellId*)( basecid );
1303         return cid;
1304 }
1305
1306 void LbmFsgrSolver::advanceCell( CellIdentifierInterface* basecid) {
1307         stdCellId *cid = convertBaseCidToStdCid(basecid);
1308         if(cid->getEnd()) return;
1309
1310         //debugOut(" ADb "<<cid->x<<","<<cid->y<<","<<cid->z<<" e"<<cid->getEnd(), 10);
1311         cid->x++;
1312         if(cid->x > CID_EX){ cid->x = CID_SX; cid->y++; 
1313                 if(cid->y > CID_EY){ cid->y = CID_SY; cid->z++; 
1314                         if(cid->z > CID_EZ){ 
1315                                 cid->level--;
1316                                 cid->x = CID_SX; 
1317                                 cid->y = CID_SY; 
1318                                 cid->z = CID_SZ; 
1319                                 if(cid->level < guiRoiMinLev) {
1320                                         cid->level = guiRoiMaxLev;
1321                                         cid->setEnd( true );
1322                                 }
1323                         }
1324                 }
1325         }
1326         //debugOut(" ADa "<<cid->x<<","<<cid->y<<","<<cid->z<<" e"<<cid->getEnd(), 10);
1327 }
1328
1329 bool LbmFsgrSolver::noEndCell( CellIdentifierInterface* basecid) {
1330         stdCellId *cid = convertBaseCidToStdCid(basecid);
1331         return (!cid->getEnd());
1332 }
1333
1334 void LbmFsgrSolver::deleteCellIterator( CellIdentifierInterface** cid ) {
1335         delete *cid;
1336         *cid = NULL;
1337 }
1338
1339 CellIdentifierInterface* LbmFsgrSolver::getCellAt( ntlVec3Gfx pos ) {
1340         //int cellok = false;
1341         pos -= (this->mvGeoStart);
1342
1343         LbmFloat mmaxsize = mLevel[mMaxRefine].nodeSize;
1344         for(int level=mMaxRefine; level>=0; level--) { // finest first
1345         //for(int level=0; level<=mMaxRefine; level++) { // coarsest first
1346                 LbmFloat nsize = mLevel[level].nodeSize;
1347                 int x,y,z;
1348                 // CHECK +- maxsize?
1349                 x = (int)((pos[0]+0.5*mmaxsize) / nsize );
1350                 y = (int)((pos[1]+0.5*mmaxsize) / nsize );
1351                 z = (int)((pos[2]+0.5*mmaxsize) / nsize );
1352                 if(LBMDIM==2) z = 0;
1353
1354                 // double check...
1355                 if(x<0) continue;
1356                 if(y<0) continue;
1357                 if(z<0) continue;
1358                 if(x>=mLevel[level].lSizex) continue;
1359                 if(y>=mLevel[level].lSizey) continue;
1360                 if(z>=mLevel[level].lSizez) continue;
1361
1362                 // return fluid/if/border cells
1363                 if( ( (RFLAG(level, x,y,z, mLevel[level].setCurr)&(CFUnused)) ) ||
1364                           ( (level<mMaxRefine) && (RFLAG(level, x,y,z, mLevel[level].setCurr)&(CFUnused|CFEmpty)) ) ) {
1365                         continue;
1366                 } // */
1367
1368                 stdCellId *newcid = new stdCellId;
1369                 newcid->level = level;
1370                 newcid->x = x;
1371                 newcid->y = y;
1372                 newcid->z = z;
1373                 //errMsg("cellAt",this->mName<<" "<<pos<<" l"<<level<<":"<<x<<","<<y<<","<<z<<" "<<convertCellFlagType2String(RFLAG(level, x,y,z, mLevel[level].setCurr)) );
1374                 return newcid;
1375         }
1376
1377         return NULL;
1378 }
1379
1380
1381 // INFO functions
1382
1383 int      LbmFsgrSolver::getCellSet      ( CellIdentifierInterface* basecid) {
1384         stdCellId *cid = convertBaseCidToStdCid(basecid);
1385         return mLevel[cid->level].setCurr;
1386         //return mLevel[cid->level].setOther;
1387 }
1388
1389 int      LbmFsgrSolver::getCellLevel    ( CellIdentifierInterface* basecid) {
1390         stdCellId *cid = convertBaseCidToStdCid(basecid);
1391         return cid->level;
1392 }
1393
1394 ntlVec3Gfx   LbmFsgrSolver::getCellOrigin   ( CellIdentifierInterface* basecid) {
1395         ntlVec3Gfx ret;
1396
1397         stdCellId *cid = convertBaseCidToStdCid(basecid);
1398         ntlVec3Gfx cs( mLevel[cid->level].nodeSize );
1399         if(LBMDIM==2) { cs[2] = 0.0; }
1400
1401         if(LBMDIM==2) {
1402                 ret =(this->mvGeoStart + ntlVec3Gfx( cid->x *cs[0], cid->y *cs[1], (this->mvGeoEnd[2]-this->mvGeoStart[2])*0.5 )
1403                                 + ntlVec3Gfx(0.0,0.0,cs[1]*-0.25)*cid->level )
1404                         +getCellSize(basecid);
1405         } else {
1406                 ret =(this->mvGeoStart + ntlVec3Gfx( cid->x *cs[0], cid->y *cs[1], cid->z *cs[2] ))
1407                         +getCellSize(basecid);
1408         }
1409         return (ret);
1410 }
1411
1412 ntlVec3Gfx   LbmFsgrSolver::getCellSize     ( CellIdentifierInterface* basecid) {
1413         // return half size
1414         stdCellId *cid = convertBaseCidToStdCid(basecid);
1415         ntlVec3Gfx retvec( mLevel[cid->level].nodeSize * 0.5 );
1416         // 2d display as rectangles
1417         if(LBMDIM==2) { retvec[2] = 0.0; }
1418         return (retvec);
1419 }
1420
1421 LbmFloat LbmFsgrSolver::getCellDensity  ( CellIdentifierInterface* basecid,int set) {
1422         stdCellId *cid = convertBaseCidToStdCid(basecid);
1423
1424         // skip non-fluid cells
1425         if(RFLAG(cid->level, cid->x,cid->y,cid->z, set)&(CFFluid|CFInter)) {
1426                 // ok go on...
1427         } else {
1428                 return 0.;
1429         }
1430
1431         LbmFloat rho = 0.0;
1432         FORDF0 { rho += QCELL(cid->level, cid->x,cid->y,cid->z, set, l); } // ORG
1433         return ((rho-1.0) * mLevel[cid->level].simCellSize / mLevel[cid->level].timestep) +1.0; // ORG
1434         /*if(RFLAG(cid->level, cid->x,cid->y,cid->z, set)&CFInter) { // test
1435                 LbmFloat ux,uy,uz;
1436                 ux=uy=uz= 0.0;
1437                 int lev = cid->level;
1438                 LbmFloat df[27], feqOld[27];
1439                 FORDF0 {
1440                         rho += QCELL(lev, cid->x,cid->y,cid->z, set, l);
1441                         ux += this->dfDvecX[l]* QCELL(lev, cid->x,cid->y,cid->z, set, l);
1442                         uy += this->dfDvecY[l]* QCELL(lev, cid->x,cid->y,cid->z, set, l);
1443                         uz += this->dfDvecZ[l]* QCELL(lev, cid->x,cid->y,cid->z, set, l);
1444                         df[l] = QCELL(lev, cid->x,cid->y,cid->z, set, l);
1445                 }
1446                 FORDF0 {
1447                         feqOld[l] = getCollideEq(l, rho,ux,uy,uz); 
1448                 }
1449                 // debugging mods
1450                 //const LbmFloat Qo = this->getLesNoneqTensorCoeff(df,feqOld);
1451                 //const LbmFloat modOmega = this->getLesOmega(mLevel[lev].omega, mLevel[lev].lcsmago,Qo);
1452                 //rho = (2.0-modOmega) *25.0;
1453                 //rho = Qo*100.0;
1454                 //if(cid->x==24){ errMsg("MODOMT"," at "<<PRINT_VEC(cid->x,cid->y,cid->z)<<" = "<<rho<<" "<<Qo); }
1455                 //else{ rho=0.0; }
1456         } // test 
1457         return rho; // test */
1458 }
1459
1460 LbmVec   LbmFsgrSolver::getCellVelocity ( CellIdentifierInterface* basecid,int set) {
1461         stdCellId *cid = convertBaseCidToStdCid(basecid);
1462
1463         // skip non-fluid cells
1464         if(RFLAG(cid->level, cid->x,cid->y,cid->z, set)&(CFFluid|CFInter)) {
1465                 // ok go on...
1466         } else {
1467                 return LbmVec(0.0);
1468         }
1469
1470         LbmFloat ux,uy,uz;
1471         ux=uy=uz= 0.0;
1472         FORDF0 {
1473                 ux += this->dfDvecX[l]* QCELL(cid->level, cid->x,cid->y,cid->z, set, l);
1474                 uy += this->dfDvecY[l]* QCELL(cid->level, cid->x,cid->y,cid->z, set, l);
1475                 uz += this->dfDvecZ[l]* QCELL(cid->level, cid->x,cid->y,cid->z, set, l);
1476         }
1477         LbmVec vel(ux,uy,uz);
1478         // TODO fix...
1479         return (vel * mLevel[cid->level].simCellSize / mLevel[cid->level].timestep * this->mDebugVelScale); // normal
1480 }
1481
1482 LbmFloat   LbmFsgrSolver::getCellDf( CellIdentifierInterface* basecid,int set, int dir) {
1483         stdCellId *cid = convertBaseCidToStdCid(basecid);
1484         return QCELL(cid->level, cid->x,cid->y,cid->z, set, dir);
1485 }
1486 LbmFloat   LbmFsgrSolver::getCellMass( CellIdentifierInterface* basecid,int set) {
1487         stdCellId *cid = convertBaseCidToStdCid(basecid);
1488         return QCELL(cid->level, cid->x,cid->y,cid->z, set, dMass);
1489 }
1490 LbmFloat   LbmFsgrSolver::getCellFill( CellIdentifierInterface* basecid,int set) {
1491         stdCellId *cid = convertBaseCidToStdCid(basecid);
1492         if(RFLAG(cid->level, cid->x,cid->y,cid->z, set)&CFInter) return QCELL(cid->level, cid->x,cid->y,cid->z, set, dFfrac);
1493         if(RFLAG(cid->level, cid->x,cid->y,cid->z, set)&CFFluid) return 1.0;
1494         return 0.0;
1495         //return QCELL(cid->level, cid->x,cid->y,cid->z, set, dFfrac);
1496 }
1497 CellFlagType LbmFsgrSolver::getCellFlag( CellIdentifierInterface* basecid,int set) {
1498         stdCellId *cid = convertBaseCidToStdCid(basecid);
1499         return RFLAG(cid->level, cid->x,cid->y,cid->z, set);
1500 }
1501
1502 LbmFloat LbmFsgrSolver::getEquilDf( int l ) {
1503         return this->dfEquil[l];
1504 }
1505
1506
1507 ntlVec3Gfx LbmFsgrSolver::getVelocityAt   (float xp, float yp, float zp) {
1508         ntlVec3Gfx avgvel(0.0);
1509         LbmFloat   avgnum = 0.;
1510
1511         // taken from getCellAt!
1512         const int level = mMaxRefine;
1513         const int workSet = mLevel[level].setCurr;
1514         const LbmFloat nsize = mLevel[level].nodeSize;
1515         const int x = (int)((-this->mvGeoStart[0]+xp-0.5*nsize) / nsize );
1516         const int y = (int)((-this->mvGeoStart[1]+yp-0.5*nsize) / nsize );
1517         int       z = (int)((-this->mvGeoStart[2]+zp-0.5*nsize) / nsize );
1518         if(LBMDIM==2) z=0;
1519         //errMsg("DUMPVEL","p"<<PRINT_VEC(xp,yp,zp)<<" at "<<PRINT_VEC(x,y,z)<<" max"<<PRINT_VEC(mLevel[level].lSizex,mLevel[level].lSizey,mLevel[level].lSizez) );
1520
1521         // return fluid/if/border cells
1522         // search neighborhood, do smoothing
1523         FORDF0{ 
1524                 const int i = x+this->dfVecX[l];
1525                 const int j = y+this->dfVecY[l];
1526                 const int k = z+this->dfVecZ[l];
1527
1528                 if( (i<0) || (j<0) || (k<0) 
1529                  || (i>=mLevel[level].lSizex) 
1530                  || (j>=mLevel[level].lSizey) 
1531                  || (k>=mLevel[level].lSizez) ) continue;
1532
1533                 if( (RFLAG(level, i,j,k, mLevel[level].setCurr)&(CFFluid|CFInter)) ) {
1534                         ntlVec3Gfx vel(0.0);
1535                         LbmFloat *ccel = RACPNT(level, i,j,k ,workSet); // omp
1536                         for(int n=1; n<this->cDfNum; n++) {
1537                                 vel[0]  += (this->dfDvecX[n]*RAC(ccel,n));
1538                                 vel[1]  += (this->dfDvecY[n]*RAC(ccel,n)); 
1539                                 vel[2]  += (this->dfDvecZ[n]*RAC(ccel,n)); 
1540                         } 
1541
1542                         avgvel += vel;
1543                         avgnum += 1.0;
1544                         if(l==0) { // center slightly more weight
1545                                 avgvel += vel; avgnum += 1.0;
1546                         }
1547                 } // */
1548         }
1549
1550         if(avgnum>0.) {
1551                 ntlVec3Gfx retv = avgvel / avgnum;
1552                 retv *= nsize/mLevel[level].timestep;
1553                 // scale for current animation settings (frame time)
1554                 retv *= mpParam->getCurrentAniFrameTime();
1555                 //errMsg("DUMPVEL","t"<<mSimulationTime<<" at "<<PRINT_VEC(xp,yp,zp)<<" ret:"<<retv<<", avgv:"<<avgvel<<" n"<<avgnum<<" nsize"<<nsize<<" ts"<<mLevel[level].timestep<<" fr"<<mpParam->getCurrentAniFrameTime() );
1556                 return retv;
1557         }
1558         // no cells here...?
1559         //errMsg("DUMPVEL"," at "<<PRINT_VEC(xp,yp,zp)<<" v"<<avgvel<<" n"<<avgnum<<" no vel !?");
1560         return ntlVec3Gfx(0.);
1561 }
1562
1563 #if LBM_USE_GUI==1
1564 //! show simulation info (implement SimulationObject pure virtual func)
1565 void 
1566 LbmFsgrSolver::debugDisplay(int set){ 
1567         //lbmDebugDisplay< LbmFsgrSolver >( set, this ); 
1568         lbmDebugDisplay( set ); 
1569 }
1570 #endif
1571
1572 /*****************************************************************************/
1573 // strict debugging functions
1574 /*****************************************************************************/
1575 #if FSGR_STRICT_DEBUG==1
1576 #define STRICT_EXIT *((int *)0)=0;
1577
1578 int LbmFsgrSolver::debLBMGI(int level, int ii,int ij,int ik, int is) {
1579         if(level <  0){ errMsg("LbmStrict::debLBMGI"," invLev- l"<<level<<"|"<<ii<<","<<ij<<","<<ik<<" s"<<is); STRICT_EXIT; } 
1580         if(level >  mMaxRefine){ errMsg("LbmStrict::debLBMGI"," invLev+ l"<<level<<"|"<<ii<<","<<ij<<","<<ik<<" s"<<is); STRICT_EXIT; } 
1581
1582         if((ii==-1)&&(ij==0)) {
1583                 // special case for main loop, ok
1584         } else {
1585                 if(ii<0){ errMsg("LbmStrict"," invX- l"<<level<<"|"<<ii<<","<<ij<<","<<ik<<" s"<<is); STRICT_EXIT; }
1586                 if(ij<0){ errMsg("LbmStrict"," invY- l"<<level<<"|"<<ii<<","<<ij<<","<<ik<<" s"<<is); STRICT_EXIT; }
1587                 if(ii>mLevel[level].lSizex-1){ errMsg("LbmStrict"," invX+ l"<<level<<"|"<<ii<<","<<ij<<","<<ik<<" s"<<is); STRICT_EXIT; }
1588                 if(ij>mLevel[level].lSizey-1){ errMsg("LbmStrict"," invY+ l"<<level<<"|"<<ii<<","<<ij<<","<<ik<<" s"<<is); STRICT_EXIT; }
1589         }
1590         if(ik<0){ errMsg("LbmStrict"," invZ- l"<<level<<"|"<<ii<<","<<ij<<","<<ik<<" s"<<is); STRICT_EXIT; }
1591         if(ik>mLevel[level].lSizez-1){ errMsg("LbmStrict"," invZ+ l"<<level<<"|"<<ii<<","<<ij<<","<<ik<<" s"<<is); STRICT_EXIT; }
1592         if(is<0){ errMsg("LbmStrict"," invS- l"<<level<<"|"<<ii<<","<<ij<<","<<ik<<" s"<<is); STRICT_EXIT; }
1593         if(is>1){ errMsg("LbmStrict"," invS+ l"<<level<<"|"<<ii<<","<<ij<<","<<ik<<" s"<<is); STRICT_EXIT; }
1594         return _LBMGI(level, ii,ij,ik, is);
1595 };
1596
1597 CellFlagType& LbmFsgrSolver::debRFLAG(int level, int xx,int yy,int zz,int set){
1598         return _RFLAG(level, xx,yy,zz,set);   
1599 };
1600
1601 CellFlagType& LbmFsgrSolver::debRFLAG_NB(int level, int xx,int yy,int zz,int set, int dir) {
1602         if(dir<0)         { errMsg("LbmStrict"," invD- l"<<level<<"|"<<xx<<","<<yy<<","<<zz<<" s"<<set<<" d"<<dir); STRICT_EXIT; }
1603         // warning might access all spatial nbs
1604         if(dir>this->cDirNum){ errMsg("LbmStrict"," invD+ l"<<level<<"|"<<xx<<","<<yy<<","<<zz<<" s"<<set<<" d"<<dir); STRICT_EXIT; }
1605         return _RFLAG_NB(level, xx,yy,zz,set, dir);
1606 };
1607
1608 CellFlagType& LbmFsgrSolver::debRFLAG_NBINV(int level, int xx,int yy,int zz,int set, int dir) {
1609         if(dir<0)         { errMsg("LbmStrict"," invD- l"<<level<<"|"<<xx<<","<<yy<<","<<zz<<" s"<<set<<" d"<<dir); STRICT_EXIT; }
1610         if(dir>this->cDirNum){ errMsg("LbmStrict"," invD+ l"<<level<<"|"<<xx<<","<<yy<<","<<zz<<" s"<<set<<" d"<<dir); STRICT_EXIT; }
1611         return _RFLAG_NBINV(level, xx,yy,zz,set, dir);
1612 };
1613
1614 int LbmFsgrSolver::debLBMQI(int level, int ii,int ij,int ik, int is, int l) {
1615         if(level <  0){ errMsg("LbmStrict::debLBMQI"," invLev- l"<<level<<"|"<<ii<<","<<ij<<","<<ik<<" s"<<is); STRICT_EXIT; } 
1616         if(level >  mMaxRefine){ errMsg("LbmStrict::debLBMQI"," invLev+ l"<<level<<"|"<<ii<<","<<ij<<","<<ik<<" s"<<is); STRICT_EXIT; } 
1617
1618         if((ii==-1)&&(ij==0)) {
1619                 // special case for main loop, ok
1620         } else {
1621                 if(ii<0){ errMsg("LbmStrict"," invX- l"<<level<<"|"<<ii<<","<<ij<<","<<ik<<" s"<<is); STRICT_EXIT; }
1622                 if(ij<0){ errMsg("LbmStrict"," invY- l"<<level<<"|"<<ii<<","<<ij<<","<<ik<<" s"<<is); STRICT_EXIT; }
1623                 if(ii>mLevel[level].lSizex-1){ errMsg("LbmStrict"," invX+ l"<<level<<"|"<<ii<<","<<ij<<","<<ik<<" s"<<is); STRICT_EXIT; }
1624                 if(ij>mLevel[level].lSizey-1){ errMsg("LbmStrict"," invY+ l"<<level<<"|"<<ii<<","<<ij<<","<<ik<<" s"<<is); STRICT_EXIT; }
1625         }
1626         if(ik<0){ errMsg("LbmStrict"," invZ- l"<<level<<"|"<<ii<<","<<ij<<","<<ik<<" s"<<is); STRICT_EXIT; }
1627         if(ik>mLevel[level].lSizez-1){ errMsg("LbmStrict"," invZ+ l"<<level<<"|"<<ii<<","<<ij<<","<<ik<<" s"<<is); STRICT_EXIT; }
1628         if(is<0){ errMsg("LbmStrict"," invS- l"<<level<<"|"<<ii<<","<<ij<<","<<ik<<" s"<<is); STRICT_EXIT; }
1629         if(is>1){ errMsg("LbmStrict"," invS+ l"<<level<<"|"<<ii<<","<<ij<<","<<ik<<" s"<<is); STRICT_EXIT; }
1630         if(l<0)        { errMsg("LbmStrict"," invD- "<<" l"<<l); STRICT_EXIT; }
1631         if(l>this->cDfNum){  // dFfrac is an exception
1632                 if((l != dMass) && (l != dFfrac) && (l != dFlux)){ errMsg("LbmStrict"," invD+ "<<" l"<<l); STRICT_EXIT; } }
1633 #if COMPRESSGRIDS==1
1634         //if((!this->mInitDone) && (is!=mLevel[level].setCurr)){ STRICT_EXIT; } // COMPRT debug
1635 #endif // COMPRESSGRIDS==1
1636         return _LBMQI(level, ii,ij,ik, is, l);
1637 };
1638
1639 LbmFloat& LbmFsgrSolver::debQCELL(int level, int xx,int yy,int zz,int set,int l) {
1640         //errMsg("LbmStrict","debQCELL debug: l"<<level<<"|"<<xx<<","<<yy<<","<<zz<<" s"<<set<<" l"<<l<<" index"<<LBMGI(level, xx,yy,zz,set)); 
1641         return _QCELL(level, xx,yy,zz,set,l);
1642 };
1643
1644 LbmFloat& LbmFsgrSolver::debQCELL_NB(int level, int xx,int yy,int zz,int set, int dir,int l) {
1645         if(dir<0)        { errMsg("LbmStrict"," invD- l"<<level<<"|"<<xx<<","<<yy<<","<<zz<<" s"<<set<<" d"<<dir); STRICT_EXIT; }
1646         if(dir>this->cDfNum){ errMsg("LbmStrict"," invD+ l"<<level<<"|"<<xx<<","<<yy<<","<<zz<<" s"<<set<<" d"<<dir); STRICT_EXIT; }
1647         return _QCELL_NB(level, xx,yy,zz,set, dir,l);
1648 };
1649
1650 LbmFloat& LbmFsgrSolver::debQCELL_NBINV(int level, int xx,int yy,int zz,int set, int dir,int l) {
1651         if(dir<0)        { errMsg("LbmStrict"," invD- l"<<level<<"|"<<xx<<","<<yy<<","<<zz<<" s"<<set<<" d"<<dir); STRICT_EXIT; }
1652         if(dir>this->cDfNum){ errMsg("LbmStrict"," invD+ l"<<level<<"|"<<xx<<","<<yy<<","<<zz<<" s"<<set<<" d"<<dir); STRICT_EXIT; }
1653         return _QCELL_NBINV(level, xx,yy,zz,set, dir,l);
1654 };
1655
1656 LbmFloat* LbmFsgrSolver::debRACPNT(int level,  int ii,int ij,int ik, int is ) {
1657         return _RACPNT(level, ii,ij,ik, is );
1658 };
1659
1660 LbmFloat& LbmFsgrSolver::debRAC(LbmFloat* s,int l) {
1661         if(l<0)        { errMsg("LbmStrict"," invD- "<<" l"<<l); STRICT_EXIT; }
1662         if(l>dTotalNum){ errMsg("LbmStrict"," invD+ "<<" l"<<l); STRICT_EXIT; } 
1663         //if(l>this->cDfNum){ // dFfrac is an exception 
1664         //if((l != dMass) && (l != dFfrac) && (l != dFlux)){ errMsg("LbmStrict"," invD+ "<<" l"<<l); STRICT_EXIT; } }
1665         return _RAC(s,l);
1666 };
1667
1668 #endif // FSGR_STRICT_DEBUG==1
1669
1670
1671 /******************************************************************************
1672  * GUI&debugging functions
1673  *****************************************************************************/
1674
1675
1676 #if LBM_USE_GUI==1
1677 #define USE_GLUTILITIES
1678 #include "../gui/gui_utilities.h"
1679
1680 //! display a single node
1681 void LbmFsgrSolver::debugDisplayNode(int dispset, CellIdentifierInterface* cell ) {
1682         //debugOut(" DD: "<<cell->getAsString() , 10);
1683         ntlVec3Gfx org      = this->getCellOrigin( cell );
1684         ntlVec3Gfx halfsize = this->getCellSize( cell );
1685         int    set      = this->getCellSet( cell );
1686         //debugOut(" DD: "<<cell->getAsString()<<" "<< (dispset->type) , 10);
1687
1688         bool     showcell = true;
1689         int      linewidth = 1;
1690         ntlColor col(0.5);
1691         LbmFloat cscale = 1.0; //dispset->scale;
1692
1693 #define DRAWDISPCUBE(col,scale) \
1694         {       glLineWidth( linewidth ); \
1695           glColor3f( (col)[0], (col)[1], (col)[2]); \
1696                 ntlVec3Gfx s = org-(halfsize * (scale)); \
1697                 ntlVec3Gfx e = org+(halfsize * (scale)); \
1698                 drawCubeWire( s,e ); }
1699
1700         CellFlagType flag = this->getCellFlag(cell, set );
1701         // always check types
1702         if(flag& CFInvalid  ) { if(!guiShowInvalid  ) return; }
1703         if(flag& CFUnused   ) { if(!guiShowInvalid  ) return; }
1704         if(flag& CFEmpty    ) { if(!guiShowEmpty    ) return; }
1705         if(flag& CFInter    ) { if(!guiShowInterface) return; }
1706         if(flag& CFNoDelete ) { if(!guiShowNoDelete ) return; }
1707         if(flag& CFBnd      ) { if(!guiShowBnd      ) return; }
1708
1709         // only dismiss one of these types 
1710         if(flag& CFGrFromCoarse)  { if(!guiShowCoarseInner  ) return; } // inner not really interesting
1711         else
1712         if(flag& CFGrFromFine) { if(!guiShowCoarseBorder ) return; }
1713         else
1714         if(flag& CFFluid    )    { if(!guiShowFluid    ) return; }
1715
1716         switch(dispset) {
1717                 case FLUIDDISPNothing: {
1718                                 showcell = false;
1719                         } break;
1720                 case FLUIDDISPCelltypes: {
1721                                 cscale = 0.5;
1722
1723                                 if(flag& CFNoDelete) { // debug, mark nodel cells
1724                                         ntlColor ccol(0.7,0.0,0.0);
1725                                         DRAWDISPCUBE(ccol, 0.1);
1726                                 }
1727                                 if(flag& CFPersistMask) { // mark persistent flags
1728                                         ntlColor ccol(0.5);
1729                                         DRAWDISPCUBE(ccol, 0.125);
1730                                 }
1731                                 if(flag& CFNoBndFluid) { // mark persistent flags
1732                                         ntlColor ccol(0,0,1);
1733                                         DRAWDISPCUBE(ccol, 0.075);
1734                                 }
1735
1736                                 if(flag& CFInvalid) {
1737                                         cscale = 0.50;
1738                                         col = ntlColor(0.0,0,0.0);
1739                                 }
1740                                 else if(flag& CFBnd) {
1741                                         cscale = 0.59;
1742                                         col = ntlColor(0.4);
1743                                 }
1744
1745                                 else if(flag& CFInter) {
1746                                         cscale = 0.55;
1747                                         col = ntlColor(0,1,1);
1748
1749                                 } else if(flag& CFGrFromCoarse) {
1750                                         // draw as - with marker
1751                                         ntlColor col2(0.0,1.0,0.3);
1752                                         DRAWDISPCUBE(col2, 0.1);
1753                                         cscale = 0.5;
1754                                         showcell=false; // DEBUG
1755                                 }
1756                                 else if(flag& CFFluid) {
1757                                         cscale = 0.5;
1758                                         if(flag& CFGrToFine) {
1759                                                 ntlColor col2(0.5,0.0,0.5);
1760                                                 DRAWDISPCUBE(col2, 0.1);
1761                                                 col = ntlColor(0,0,1);
1762                                         }
1763                                         if(flag& CFGrFromFine) {
1764                                                 ntlColor col2(1.0,1.0,0.0);
1765                                                 DRAWDISPCUBE(col2, 0.1);
1766                                                 col = ntlColor(0,0,1);
1767                                         } else if(flag& CFGrFromCoarse) {
1768                                                 // draw as fluid with marker
1769                                                 ntlColor col2(0.0,1.0,0.3);
1770                                                 DRAWDISPCUBE(col2, 0.1);
1771                                                 col = ntlColor(0,0,1);
1772                                         } else {
1773                                                 col = ntlColor(0,0,1);
1774                                         }
1775                                 }
1776                                 else if(flag& CFEmpty) {
1777                                         showcell=false;
1778                                 }
1779
1780                         } break;
1781                 case FLUIDDISPVelocities: {
1782                                 // dont use cube display
1783                                 LbmVec vel = this->getCellVelocity( cell, set );
1784                                 glBegin(GL_LINES);
1785                                 glColor3f( 0.0,0.0,0.0 );
1786                                 glVertex3f( org[0], org[1], org[2] );
1787                                 org += vec2G(vel * 10.0 * cscale);
1788                                 glColor3f( 1.0,1.0,1.0 );
1789                                 glVertex3f( org[0], org[1], org[2] );
1790                                 glEnd();
1791                                 showcell = false;
1792                         } break;
1793                 case FLUIDDISPCellfills: {
1794                                 cscale = 0.5;
1795                                 if(flag& CFFluid) {
1796                                         cscale = 0.75;
1797                                         col = ntlColor(0,0,0.5);
1798                                 }
1799                                 else if(flag& CFInter) {
1800                                         cscale = 0.75 * this->getCellMass(cell,set);
1801                                         col = ntlColor(0,1,1);
1802                                 }
1803                                 else {
1804                                         showcell=false;
1805                                 }
1806
1807                                         if( ABS(this->getCellMass(cell,set)) < 10.0 ) {
1808                                                 cscale = 0.75 * this->getCellMass(cell,set);
1809                                         } else {
1810                                                 showcell = false;
1811                                         }
1812                                         if(cscale>0.0) {
1813                                                 col = ntlColor(0,1,1);
1814                                         } else {
1815                                                 col = ntlColor(1,1,0);
1816                                         }
1817                         // TODO
1818                         } break;
1819                 case FLUIDDISPDensity: {
1820                                 LbmFloat rho = this->getCellDensity(cell,set);
1821                                 cscale = rho*rho * 0.25;
1822                                 col = ntlColor( MIN(0.5+cscale,1.0) , MIN(0.0+cscale,1.0), MIN(0.0+cscale,1.0) );
1823                                 cscale *= 2.0;
1824                         } break;
1825                 case FLUIDDISPGrid: {
1826                                 cscale = 0.59;
1827                                 col = ntlColor(1.0);
1828                         } break;
1829                 default: {
1830                                 cscale = 0.5;
1831                                 col = ntlColor(1.0,0.0,0.0);
1832                         } break;
1833         }
1834
1835         if(!showcell) return;
1836         if(cscale==0.0) return; // dont draw zero values
1837         DRAWDISPCUBE(col, cscale);
1838 }
1839
1840 //! debug display function
1841 //  D has to implement the CellIterator interface
1842 void LbmFsgrSolver::lbmDebugDisplay(int dispset) {
1843         // DEBUG always display testdata
1844 #if LBM_INCLUDE_TESTSOLVERS==1
1845         if(mUseTestdata){ 
1846                 cpDebugDisplay(dispset); 
1847                 mpTest->testDebugDisplay(dispset); 
1848         }
1849 #endif // LBM_INCLUDE_TESTSOLVERS==1
1850         if(dispset<=FLUIDDISPNothing) return;
1851         //if(!dispset->on) return;
1852         glDisable( GL_LIGHTING ); // dont light lines
1853
1854 #if LBM_INCLUDE_TESTSOLVERS==1
1855         if((!mUseTestdata)|| (mUseTestdata)&&(mpTest->mFarfMode<=0)) {
1856 #endif // LBM_INCLUDE_TESTSOLVERS==1
1857
1858         LbmFsgrSolver::CellIdentifier cid = this->getFirstCell();
1859         for(; this->noEndCell( cid );
1860               this->advanceCell( cid ) ) {
1861                 this->debugDisplayNode(dispset, cid );
1862         }
1863         delete cid;
1864
1865 #if LBM_INCLUDE_TESTSOLVERS==1
1866         } // 3d check
1867 #endif // LBM_INCLUDE_TESTSOLVERS==1
1868
1869         glEnable( GL_LIGHTING ); // dont light lines
1870 }
1871
1872 //! debug display function
1873 //  D has to implement the CellIterator interface
1874 void LbmFsgrSolver::lbmMarkedCellDisplay() {
1875         //fluidDispSettings dispset;
1876         // trick - display marked cells as grid displa -> white, big
1877         int dispset = FLUIDDISPGrid;
1878         glDisable( GL_LIGHTING ); // dont light lines
1879         
1880         LbmFsgrSolver::CellIdentifier cid = this->markedGetFirstCell();
1881         while(cid) {
1882                 this->debugDisplayNode(dispset, cid );
1883                 cid = this->markedAdvanceCell();
1884         }
1885         delete cid;
1886
1887         glEnable( GL_LIGHTING ); // dont light lines
1888 }
1889
1890 #endif // LBM_USE_GUI==1
1891
1892 //! display a single node
1893 void LbmFsgrSolver::debugPrintNodeInfo(CellIdentifierInterface* cell, int forceSet) {
1894                 //string printInfo,
1895                 // force printing of one set? default = -1 = off
1896   bool printDF     = false;
1897   bool printRho    = false;
1898   bool printVel    = false;
1899   bool printFlag   = false;
1900   bool printGeom   = false;
1901   bool printMass=false;
1902         bool printBothSets = false;
1903         string printInfo = this->getNodeInfoString();
1904
1905         for(size_t i=0; i<printInfo.length()-0; i++) {
1906                 char what = printInfo[i];
1907                 switch(what) {
1908                         case '+': // all on
1909                                                                 printDF = true; printRho = true; printVel = true; printFlag = true; printGeom = true; printMass = true ;
1910                                                                 printBothSets = true; break;
1911                         case '-': // all off
1912                                                                 printDF = false; printRho = false; printVel = false; printFlag = false; printGeom = false; printMass = false; 
1913                                                                 printBothSets = false; break;
1914                         case 'd': printDF = true; break;
1915                         case 'r': printRho = true; break;
1916                         case 'v': printVel = true; break;
1917                         case 'f': printFlag = true; break;
1918                         case 'g': printGeom = true; break;
1919                         case 'm': printMass = true; break;
1920                         case 's': printBothSets = true; break;
1921                         default: 
1922                                 errFatal("debugPrintNodeInfo","Invalid node info id "<<what,SIMWORLD_GENERICERROR); return;
1923                 }
1924         }
1925
1926         ntlVec3Gfx org      = this->getCellOrigin( cell );
1927         ntlVec3Gfx halfsize = this->getCellSize( cell );
1928         int    set      = this->getCellSet( cell );
1929         debMsgStd("debugPrintNodeInfo",DM_NOTIFY, "Printing cell info '"<<printInfo<<"' for node: "<<cell->getAsString()<<" from "<<this->getName()<<" currSet:"<<set , 1);
1930         if(printGeom) debMsgStd("                  ",DM_MSG, "Org:"<<org<<" Halfsize:"<<halfsize<<" ", 1);
1931
1932         int setmax = 2;
1933         if(!printBothSets) setmax = 1;
1934         if(forceSet>=0) setmax = 1;
1935
1936         for(int s=0; s<setmax; s++) {
1937                 int workset = set;
1938                 if(s==1){ workset = (set^1); }          
1939                 if(forceSet>=0) workset = forceSet;
1940                 debMsgStd("                  ",DM_MSG, "Printing set:"<<workset<<" orgSet:"<<set, 1);
1941                 
1942                 if(printDF) {
1943                         for(int l=0; l<LBM_DFNUM; l++) { // FIXME ??
1944                                 debMsgStd("                  ",DM_MSG, "  Df"<<l<<": "<<this->getCellDf(cell,workset,l), 1);
1945                         }
1946                 }
1947                 if(printRho) {
1948                         debMsgStd("                  ",DM_MSG, "  Rho: "<<this->getCellDensity(cell,workset), 1);
1949                 }
1950                 if(printVel) {
1951                         debMsgStd("                  ",DM_MSG, "  Vel: "<<this->getCellVelocity(cell,workset), 1);
1952                 }
1953                 if(printFlag) {
1954                         CellFlagType flag = this->getCellFlag(cell,workset);
1955                         debMsgStd("                  ",DM_MSG, "  Flg: "<< flag<<" "<<convertFlags2String( flag ) <<" "<<convertCellFlagType2String( flag ), 1);
1956                 }
1957                 if(printMass) {
1958                         debMsgStd("                  ",DM_MSG, "  Mss: "<<this->getCellMass(cell,workset), 1);
1959                 }
1960                 // dirty... TODO fixme
1961                 debMsgStd("                  ",DM_MSG, "  Flx: "<<this->getCellDf(cell,workset,dFlux), 1);
1962         }
1963 }
1964
1965