- another minor solver update to fix
[blender.git] / intern / elbeem / intern / solver_relax.h
1 /******************************************************************************
2  *
3  * El'Beem - the visual lattice boltzmann freesurface simulator
4  * All code distributed as part of El'Beem is covered by the version 2 of the 
5  * GNU General Public License. See the file COPYING for details.
6  * Copyright 2003-2005 Nils Thuerey
7  *
8  * Combined 2D/3D Lattice Boltzmann relaxation macros
9  *
10  *****************************************************************************/
11
12 #if FSGR_STRICT_DEBUG==1
13 #define CAUSE_PANIC { this->mPanic=1; /* *((int*)(0x0)) = 1; crash*/ }
14 #else // FSGR_STRICT_DEBUG==1
15 #define CAUSE_PANIC { this->mPanic=1; } /*set flag*/
16 #endif // FSGR_STRICT_DEBUG==1
17
18
19
20
21 // off for non testing
22 #define PRECOLLIDE_MODS(rho,ux,uy,uz, grav) \
23         ux += (grav)[0]; \
24         uy += (grav)[1]; \
25         uz += (grav)[2]; \
26
27 #define TEST_IF_CHECK 
28
29
30         
31 /******************************************************************************
32  * normal relaxation
33  *****************************************************************************/
34
35 // standard arrays
36 #define CSRC_C    RAC(ccel                                , dC )
37 #define CSRC_E    RAC(ccel + (-1)             *(dTotalNum), dE )
38 #define CSRC_W    RAC(ccel + (+1)             *(dTotalNum), dW )
39 #define CSRC_N    RAC(ccel + (-mLevel[lev].lOffsx)        *(dTotalNum), dN )
40 #define CSRC_S    RAC(ccel + (+mLevel[lev].lOffsx)        *(dTotalNum), dS )
41 #define CSRC_NE   RAC(ccel + (-mLevel[lev].lOffsx-1)      *(dTotalNum), dNE)
42 #define CSRC_NW   RAC(ccel + (-mLevel[lev].lOffsx+1)      *(dTotalNum), dNW)
43 #define CSRC_SE   RAC(ccel + (+mLevel[lev].lOffsx-1)      *(dTotalNum), dSE)
44 #define CSRC_SW   RAC(ccel + (+mLevel[lev].lOffsx+1)      *(dTotalNum), dSW)
45 #define CSRC_T    RAC(ccel + (-mLevel[lev].lOffsy)        *(dTotalNum), dT )
46 #define CSRC_B    RAC(ccel + (+mLevel[lev].lOffsy)        *(dTotalNum), dB )
47 #define CSRC_ET   RAC(ccel + (-mLevel[lev].lOffsy-1)      *(dTotalNum), dET)
48 #define CSRC_EB   RAC(ccel + (+mLevel[lev].lOffsy-1)      *(dTotalNum), dEB)
49 #define CSRC_WT   RAC(ccel + (-mLevel[lev].lOffsy+1)      *(dTotalNum), dWT)
50 #define CSRC_WB   RAC(ccel + (+mLevel[lev].lOffsy+1)      *(dTotalNum), dWB)
51 #define CSRC_NT   RAC(ccel + (-mLevel[lev].lOffsy-mLevel[lev].lOffsx) *(dTotalNum), dNT)
52 #define CSRC_NB   RAC(ccel + (+mLevel[lev].lOffsy-mLevel[lev].lOffsx) *(dTotalNum), dNB)
53 #define CSRC_ST   RAC(ccel + (-mLevel[lev].lOffsy+mLevel[lev].lOffsx) *(dTotalNum), dST)
54 #define CSRC_SB   RAC(ccel + (+mLevel[lev].lOffsy+mLevel[lev].lOffsx) *(dTotalNum), dSB)
55
56 #define XSRC_C(x)    RAC(ccel + (x)                 *dTotalNum, dC )
57 #define XSRC_E(x)    RAC(ccel + ((x)-1)             *dTotalNum, dE )
58 #define XSRC_W(x)    RAC(ccel + ((x)+1)             *dTotalNum, dW )
59 #define XSRC_N(x)    RAC(ccel + ((x)-mLevel[lev].lOffsx)        *dTotalNum, dN )
60 #define XSRC_S(x)    RAC(ccel + ((x)+mLevel[lev].lOffsx)        *dTotalNum, dS )
61 #define XSRC_NE(x)   RAC(ccel + ((x)-mLevel[lev].lOffsx-1)      *dTotalNum, dNE)
62 #define XSRC_NW(x)   RAC(ccel + ((x)-mLevel[lev].lOffsx+1)      *dTotalNum, dNW)
63 #define XSRC_SE(x)   RAC(ccel + ((x)+mLevel[lev].lOffsx-1)      *dTotalNum, dSE)
64 #define XSRC_SW(x)   RAC(ccel + ((x)+mLevel[lev].lOffsx+1)      *dTotalNum, dSW)
65 #define XSRC_T(x)    RAC(ccel + ((x)-mLevel[lev].lOffsy)        *dTotalNum, dT )
66 #define XSRC_B(x)    RAC(ccel + ((x)+mLevel[lev].lOffsy)        *dTotalNum, dB )
67 #define XSRC_ET(x)   RAC(ccel + ((x)-mLevel[lev].lOffsy-1)      *dTotalNum, dET)
68 #define XSRC_EB(x)   RAC(ccel + ((x)+mLevel[lev].lOffsy-1)      *dTotalNum, dEB)
69 #define XSRC_WT(x)   RAC(ccel + ((x)-mLevel[lev].lOffsy+1)      *dTotalNum, dWT)
70 #define XSRC_WB(x)   RAC(ccel + ((x)+mLevel[lev].lOffsy+1)      *dTotalNum, dWB)
71 #define XSRC_NT(x)   RAC(ccel + ((x)-mLevel[lev].lOffsy-mLevel[lev].lOffsx) *dTotalNum, dNT)
72 #define XSRC_NB(x)   RAC(ccel + ((x)+mLevel[lev].lOffsy-mLevel[lev].lOffsx) *dTotalNum, dNB)
73 #define XSRC_ST(x)   RAC(ccel + ((x)-mLevel[lev].lOffsy+mLevel[lev].lOffsx) *dTotalNum, dST)
74 #define XSRC_SB(x)   RAC(ccel + ((x)+mLevel[lev].lOffsy+mLevel[lev].lOffsx) *dTotalNum, dSB)
75
76
77
78 #define OMEGA(l) mLevel[(l)].omega
79
80 #define EQC (  DFL1*(rho - usqr))
81 #define EQN (  DFL2*(rho + uy*(4.5*uy + 3.0) - usqr))
82 #define EQS (  DFL2*(rho + uy*(4.5*uy - 3.0) - usqr))
83 #define EQE (  DFL2*(rho + ux*(4.5*ux + 3.0) - usqr))
84 #define EQW (  DFL2*(rho + ux*(4.5*ux - 3.0) - usqr))
85 #define EQT (  DFL2*(rho + uz*(4.5*uz + 3.0) - usqr))
86 #define EQB (  DFL2*(rho + uz*(4.5*uz - 3.0) - usqr))
87                     
88 #define EQNE ( DFL3*(rho + (+ux+uy)*(4.5*(+ux+uy) + 3.0) - usqr))
89 #define EQNW ( DFL3*(rho + (-ux+uy)*(4.5*(-ux+uy) + 3.0) - usqr))
90 #define EQSE ( DFL3*(rho + (+ux-uy)*(4.5*(+ux-uy) + 3.0) - usqr))
91 #define EQSW ( DFL3*(rho + (-ux-uy)*(4.5*(-ux-uy) + 3.0) - usqr))
92 #define EQNT ( DFL3*(rho + (+uy+uz)*(4.5*(+uy+uz) + 3.0) - usqr))
93 #define EQNB ( DFL3*(rho + (+uy-uz)*(4.5*(+uy-uz) + 3.0) - usqr))
94 #define EQST ( DFL3*(rho + (-uy+uz)*(4.5*(-uy+uz) + 3.0) - usqr))
95 #define EQSB ( DFL3*(rho + (-uy-uz)*(4.5*(-uy-uz) + 3.0) - usqr))
96 #define EQET ( DFL3*(rho + (+ux+uz)*(4.5*(+ux+uz) + 3.0) - usqr))
97 #define EQEB ( DFL3*(rho + (+ux-uz)*(4.5*(+ux-uz) + 3.0) - usqr))
98 #define EQWT ( DFL3*(rho + (-ux+uz)*(4.5*(-ux+uz) + 3.0) - usqr))
99 #define EQWB ( DFL3*(rho + (-ux-uz)*(4.5*(-ux-uz) + 3.0) - usqr))
100
101
102 // this is a bit ugly, but necessary for the CSRC_ access...
103 #define MSRC_C    m[dC ]
104 #define MSRC_N    m[dN ]
105 #define MSRC_S    m[dS ]
106 #define MSRC_E    m[dE ]
107 #define MSRC_W    m[dW ]
108 #define MSRC_T    m[dT ]
109 #define MSRC_B    m[dB ]
110 #define MSRC_NE   m[dNE]
111 #define MSRC_NW   m[dNW]
112 #define MSRC_SE   m[dSE]
113 #define MSRC_SW   m[dSW]
114 #define MSRC_NT   m[dNT]
115 #define MSRC_NB   m[dNB]
116 #define MSRC_ST   m[dST]
117 #define MSRC_SB   m[dSB]
118 #define MSRC_ET   m[dET]
119 #define MSRC_EB   m[dEB]
120 #define MSRC_WT   m[dWT]
121 #define MSRC_WB   m[dWB]
122
123 // this is a bit ugly, but necessary for the ccel local access...
124 #define CCEL_C    RAC(ccel, dC )
125 #define CCEL_N    RAC(ccel, dN )
126 #define CCEL_S    RAC(ccel, dS )
127 #define CCEL_E    RAC(ccel, dE )
128 #define CCEL_W    RAC(ccel, dW )
129 #define CCEL_T    RAC(ccel, dT )
130 #define CCEL_B    RAC(ccel, dB )
131 #define CCEL_NE   RAC(ccel, dNE)
132 #define CCEL_NW   RAC(ccel, dNW)
133 #define CCEL_SE   RAC(ccel, dSE)
134 #define CCEL_SW   RAC(ccel, dSW)
135 #define CCEL_NT   RAC(ccel, dNT)
136 #define CCEL_NB   RAC(ccel, dNB)
137 #define CCEL_ST   RAC(ccel, dST)
138 #define CCEL_SB   RAC(ccel, dSB)
139 #define CCEL_ET   RAC(ccel, dET)
140 #define CCEL_EB   RAC(ccel, dEB)
141 #define CCEL_WT   RAC(ccel, dWT)
142 #define CCEL_WB   RAC(ccel, dWB)
143 // for coarse to fine interpol access
144 #define CCELG_C(f)    (RAC(ccel, dC )*mGaussw[(f)])
145 #define CCELG_N(f)    (RAC(ccel, dN )*mGaussw[(f)])
146 #define CCELG_S(f)    (RAC(ccel, dS )*mGaussw[(f)])
147 #define CCELG_E(f)    (RAC(ccel, dE )*mGaussw[(f)])
148 #define CCELG_W(f)    (RAC(ccel, dW )*mGaussw[(f)])
149 #define CCELG_T(f)    (RAC(ccel, dT )*mGaussw[(f)])
150 #define CCELG_B(f)    (RAC(ccel, dB )*mGaussw[(f)])
151 #define CCELG_NE(f)   (RAC(ccel, dNE)*mGaussw[(f)])
152 #define CCELG_NW(f)   (RAC(ccel, dNW)*mGaussw[(f)])
153 #define CCELG_SE(f)   (RAC(ccel, dSE)*mGaussw[(f)])
154 #define CCELG_SW(f)   (RAC(ccel, dSW)*mGaussw[(f)])
155 #define CCELG_NT(f)   (RAC(ccel, dNT)*mGaussw[(f)])
156 #define CCELG_NB(f)   (RAC(ccel, dNB)*mGaussw[(f)])
157 #define CCELG_ST(f)   (RAC(ccel, dST)*mGaussw[(f)])
158 #define CCELG_SB(f)   (RAC(ccel, dSB)*mGaussw[(f)])
159 #define CCELG_ET(f)   (RAC(ccel, dET)*mGaussw[(f)])
160 #define CCELG_EB(f)   (RAC(ccel, dEB)*mGaussw[(f)])
161 #define CCELG_WT(f)   (RAC(ccel, dWT)*mGaussw[(f)])
162 #define CCELG_WB(f)   (RAC(ccel, dWB)*mGaussw[(f)])
163
164
165 #if PARALLEL==1
166 #define CSMOMEGA_STATS(dlev, domega) 
167 #else // PARALLEL==1
168 #if FSGR_OMEGA_DEBUG==1
169 #define CSMOMEGA_STATS(dlev, domega) \
170         mLevel[dlev].avgOmega += domega; mLevel[dlev].avgOmegaCnt+=1.0; 
171 #else // FSGR_OMEGA_DEBUG==1
172 #define CSMOMEGA_STATS(dlev, domega) 
173 #endif // FSGR_OMEGA_DEBUG==1
174 #endif // PARALLEL==1
175
176
177 // used for main loops and grav init
178 // source set
179 #define SRCS(l) mLevel[(l)].setCurr
180 // target set
181 #define TSET(l) mLevel[(l)].setOther
182
183 // treatment of freeslip reflection
184 // used both for OPT and nonOPT
185 #define DEFAULT_STREAM_FREESLIP(l,invl,mnbf) \
186                 /*const int inv_l = this->dfInv[l];*/ \
187                 int nb1 = 0, nb2 = 0;   /* is neighbor in this direction an obstacle? */\
188                 LbmFloat newval = 0.0; /* new value for m[l], differs for free/part slip */\
189                 const int dx = this->dfVecX[invl], dy = this->dfVecY[invl], dz = this->dfVecZ[invl]; \
190                 if(dz==0) { \
191                         nb1 = !(RFLAG(lev, i,   j+dy,k, SRCS(lev))&(CFFluid|CFInter)); \
192                         nb2 = !(RFLAG(lev, i+dx,j,   k, SRCS(lev))&(CFFluid|CFInter)); \
193                         if((nb1)&&(!nb2)) { \
194                                 /* x reflection */\
195                                 newval = QCELL(lev, i+dx,j,k,SRCS(lev), this->dfRefX[l]); \
196                         } else  \
197                         if((!nb1)&&(nb2)) { \
198                                 /* y reflection */\
199                                 newval = QCELL(lev, i,j+dy,k,SRCS(lev), this->dfRefY[l]); \
200                         } else { \
201                                 /* normal no slip in all other cases */\
202                                 newval = QCELL(lev, i,j,k,SRCS(lev), invl); \
203                         } \
204                 } else /* z=0 */\
205                 if(dy==0) { \
206                         nb1 = !(RFLAG(lev, i,j,k+dz, SRCS(lev))&(CFFluid|CFInter)); \
207                         nb2 = !(RFLAG(lev, i+dx,j,k, SRCS(lev))&(CFFluid|CFInter)); \
208                         if((nb1)&&(!nb2)) { \
209                                 /* x reflection */\
210                                 newval = QCELL(lev, i+dx,j,k,SRCS(lev), this->dfRefX[l]); \
211                         } else  \
212                         if((!nb1)&&(nb2)) { \
213                                 /* z reflection */\
214                                 newval = QCELL(lev, i,j,k+dz,SRCS(lev), this->dfRefZ[l]); \
215                         } else { \
216                                 /* normal no slip in all other cases */\
217                                 newval = ( QCELL(lev, i,j,k,SRCS(lev), invl) ); \
218                         } \
219                         /* end y=0 */ \
220                 } else { \
221                         /* x=0 */\
222                         nb1 = !(RFLAG(lev, i,j,k+dz, SRCS(lev))&(CFFluid|CFInter)); \
223                         nb2 = !(RFLAG(lev, i,j+dy,k, SRCS(lev))&(CFFluid|CFInter)); \
224                         if((nb1)&&(!nb2)) { \
225                                 /* y reflection */\
226                                 newval = QCELL(lev, i,j+dy,k,SRCS(lev), this->dfRefY[l]); \
227                         } else  \
228                         if((!nb1)&&(nb2)) { \
229                                 /* z reflection */\
230                                 newval = QCELL(lev, i,j,k+dz,SRCS(lev), this->dfRefZ[l]); \
231                         } else { \
232                                 /* normal no slip in all other cases */\
233                                 newval = ( QCELL(lev, i,j,k,SRCS(lev), invl) ); \
234                         } \
235                 } \
236                 if(mnbf & CFBndPartslip) { /* part slip interpolation */ \
237                         const LbmFloat partv = mObjectPartslips[(int)(mnbf>>24)]; \
238                         m[l] = RAC(ccel, this->dfInv[l] ) * partv + newval * (1.0-partv); /* part slip */ \
239                 } else {\
240                         m[l] = newval; /* normal free slip*/\
241                 }\
242
243 // complete default stream&collide, 2d/3d
244 /* read distribution funtions of adjacent cells = sweep step */ 
245 #if OPT3D==0 
246
247 #if FSGR_STRICT_DEBUG==1
248 #define MARKCELLCHECK \
249         debugMarkCell(lev,i,j,k); CAUSE_PANIC;
250 #define STREAMCHECK(id,ni,nj,nk,nl) \
251         if((m[nl] < -1.0) || (m[nl]>1.0)) {\
252                 errMsg("STREAMCHECK","ID"<<id<<" Invalid streamed DF nl"<<nl<<" value:"<<m[nl]<<" at "<<PRINT_IJK<<" from "<<PRINT_VEC(ni,nj,nk)<<" nl"<<(nl)<<\
253                                 " nfc"<< RFLAG(lev, ni,nj,nk, mLevel[lev].setCurr)<<" nfo"<< RFLAG(lev, ni,nj,nk, mLevel[lev].setOther)  ); \
254                 /*FORDF0{ errMsg("STREAMCHECK"," at "<<PRINT_IJK<<" df "<<l<<"="<<m[l] ); } */ \
255                 MARKCELLCHECK; \
256         }
257 #define COLLCHECK \
258         if( (rho>2.0) || (rho<-1.0) || (ABS(ux)>1.0) || (ABS(uy)>1.0) |(ABS(uz)>1.0) ) {\
259                 errMsg("COLLCHECK","Invalid collision values r:"<<rho<<" u:"PRINT_VEC(ux,uy,uz)<<" at? "<<PRINT_IJK ); \
260                 /*FORDF0{ errMsg("COLLCHECK"," at? "<<PRINT_IJK<<" df "<<l<<"="<<m[l] ); }*/ \
261                 MARKCELLCHECK; \
262         }
263 #else
264 #define STREAMCHECK(id, ni,nj,nk,nl) 
265 #define COLLCHECK
266 #endif
267
268 // careful ux,uy,uz need to be inited before!
269
270 #define DEFAULT_STREAM \
271                 m[dC] = RAC(ccel,dC); \
272                 STREAMCHECK(1, i,j,k, dC); \
273                 FORDF1 { \
274                         CellFlagType nbf = nbflag[ this->dfInv[l] ];\
275                         if(nbf & CFBnd) { \
276                                 if(nbf & CFBndNoslip) { \
277                                         /* no slip, default */ \
278                                         m[l] = RAC(ccel, this->dfInv[l] ); /* noslip */ \
279                                         STREAMCHECK(2, i,j,k, l); \
280                                 } else if(nbf & (CFBndFreeslip|CFBndPartslip)) { \
281                                         /* free slip */ \
282                                         if(l<=LBMDIM*2) { \
283                                                 m[l] = RAC(ccel, this->dfInv[l] ); STREAMCHECK(3, i,j,k, l); /* noslip for <dim*2 */ \
284                                         } else { \
285                                                 const int inv_l = this->dfInv[l]; \
286                                                 DEFAULT_STREAM_FREESLIP(l,inv_l,nbf); \
287                                         } /* l>2*dim free slip */ \
288                                         \
289                                 } /* type reflect */\
290                                 else {\
291                                         errMsg("LbmFsgrSolver","Invalid Bnd type at "<<PRINT_IJK<<" f"<<convertCellFlagType2String(nbf)<<",nbdir"<<this->dfInv[l] ); \
292                                 } \
293                                 if(nbf&CFBndMoving) m[l]+=QCELL_NBINV(lev, i, j, k, SRCS(lev), l,l); /* obs. speed*/ \
294                         } else { \
295                                 m[l] = QCELL_NBINV(lev, i, j, k, SRCS(lev), l,l); \
296                                 STREAMCHECK(4, i+this->dfVecX[this->dfInv[l]], j+this->dfVecY[this->dfInv[l]],k+this->dfVecZ[this->dfInv[l]], l); \
297                         } \
298                 }   
299
300
301 // careful ux,uy,uz need to be inited before!
302 #define DEFAULT_COLLIDEG(grav) \
303                         this->collideArrays(i,j,k, m, rho,ux,uy,uz, OMEGA(lev), grav, mLevel[lev].lcsmago, &mDebugOmegaRet, &lcsmqo ); \
304                         CSMOMEGA_STATS(lev,mDebugOmegaRet); \
305                         FORDF0 { RAC(tcel,l) = m[l]; }   \
306                         usqr = 1.5 * (ux*ux + uy*uy + uz*uz);  \
307                         COLLCHECK;
308 #define OPTIMIZED_STREAMCOLLIDE \
309                         m[0] = RAC(ccel,0); \
310                         FORDF1 { /* df0 is set later on... */ \
311                                 /* FIXME CHECK INV ? */\
312                                 if(RFLAG_NBINV(lev, i,j,k,SRCS(lev),l)&CFBnd) { errMsg("???", "bnd-err-nobndfl"); CAUSE_PANIC;  \
313                                 } else { m[l] = QCELL_NBINV(lev, i, j, k, SRCS(lev), l, l); } \
314                                 STREAMCHECK(8, i+this->dfVecX[this->dfInv[l]], j+this->dfVecY[this->dfInv[l]],k+this->dfVecZ[this->dfInv[l]], l); \
315                         }   \
316                         rho=m[0]; \
317                         /* ux = mLevel[lev].gravity[0]; uy = [1]; uz = mLevel[lev].gravity[2]; */ \
318                         this->collideArrays(i,j,k, m, rho,ux,uy,uz, OMEGA(lev), mLevel[lev].gravity, mLevel[lev].lcsmago , &mDebugOmegaRet, &lcsmqo   ); \
319                         CSMOMEGA_STATS(lev,mDebugOmegaRet); \
320                         FORDF0 { RAC(tcel,l) = m[l]; } \
321                         usqr = 1.5 * (ux*ux + uy*uy + uz*uz);  \
322                         COLLCHECK;
323
324 #else  // 3D, opt OPT3D==true
325
326
327 // handle mov. obj 
328 #if FSGR_STRICT_DEBUG==1
329 #define LBMDS_ADDMOV(linv,l) if(nbflag[linv]&CFBndMoving){ \
330                 if(QCELL_NBINV(lev, i, j, k, SRCS(lev), l,dFlux)== (mSimulationTime+this->mpParam->getTimestep()) ) { \
331                         m[l]+=QCELL_NBINV(lev, i, j, k, SRCS(lev), l,l); /* obs. speed*/ \
332                 } else { \
333                         CAUSE_PANIC; errMsg("INVALID_MOV_OBJ_TIME"," at "<<PRINT_IJK<<" from l"<<l<<" t="<<mSimulationTime<<" ct="<<QCELL_NBINV(lev, i, j, k, SRCS(lev), l,dFlux)); \
334                 } \
335         }
336 #else // FSGR_STRICT_DEBUG==1
337 #define LBMDS_ADDMOV(linv,l) if(nbflag[linv]&CFBndMoving){ m[l]+=QCELL_NBINV(lev, i, j, k, SRCS(lev), l,l); } /* obs. speed*/ 
338 #endif // FSGR_STRICT_DEBUG==1
339
340 // default stream opt3d add moving bc val
341 #define DEFAULT_STREAM \
342                 m[dC] = RAC(ccel,dC); \
343                 /* explicit streaming */ \
344                 if((!nbored & CFBnd)) { \
345                         /* no boundary near?, no real speed diff.? */ \
346                         m[dN ] = CSRC_N ; m[dS ] = CSRC_S ; \
347                         m[dE ] = CSRC_E ; m[dW ] = CSRC_W ; \
348                         m[dT ] = CSRC_T ; m[dB ] = CSRC_B ; \
349                         m[dNE] = CSRC_NE; m[dNW] = CSRC_NW; m[dSE] = CSRC_SE; m[dSW] = CSRC_SW; \
350                         m[dNT] = CSRC_NT; m[dNB] = CSRC_NB; m[dST] = CSRC_ST; m[dSB] = CSRC_SB; \
351                         m[dET] = CSRC_ET; m[dEB] = CSRC_EB; m[dWT] = CSRC_WT; m[dWB] = CSRC_WB; \
352                 } else { \
353                         /* explicit streaming, normal velocity always zero for obstacles */ \
354                         if(nbflag[dS ]&CFBnd) { m[dN ] = RAC(ccel,dS ); LBMDS_ADDMOV(dS ,dN ); } else { m[dN ] = CSRC_N ; } \
355                         if(nbflag[dN ]&CFBnd) { m[dS ] = RAC(ccel,dN ); LBMDS_ADDMOV(dN ,dS ); } else { m[dS ] = CSRC_S ; } \
356                         if(nbflag[dW ]&CFBnd) { m[dE ] = RAC(ccel,dW ); LBMDS_ADDMOV(dW ,dE ); } else { m[dE ] = CSRC_E ; } \
357                         if(nbflag[dE ]&CFBnd) { m[dW ] = RAC(ccel,dE ); LBMDS_ADDMOV(dE ,dW ); } else { m[dW ] = CSRC_W ; } \
358                         if(nbflag[dB ]&CFBnd) { m[dT ] = RAC(ccel,dB ); LBMDS_ADDMOV(dB ,dT ); } else { m[dT ] = CSRC_T ; } \
359                         if(nbflag[dT ]&CFBnd) { m[dB ] = RAC(ccel,dT ); LBMDS_ADDMOV(dT ,dB ); } else { m[dB ] = CSRC_B ; } \
360                         \
361                         /* also treat free slip here */ \
362                         if(nbflag[dSW]&CFBnd) { if(nbflag[dSW]&CFBndNoslip){ m[dNE] = RAC(ccel,dSW); LBMDS_ADDMOV(dSW,dNE); }else{ DEFAULT_STREAM_FREESLIP(dNE,dSW,nbflag[dSW]);} LBMDS_ADDMOV(dSW,dNE); } else { m[dNE] = CSRC_NE; } \
363                         if(nbflag[dSE]&CFBnd) { if(nbflag[dSE]&CFBndNoslip){ m[dNW] = RAC(ccel,dSE); LBMDS_ADDMOV(dSE,dNW); }else{ DEFAULT_STREAM_FREESLIP(dNW,dSE,nbflag[dSE]);} LBMDS_ADDMOV(dSE,dNW); } else { m[dNW] = CSRC_NW; } \
364                         if(nbflag[dNW]&CFBnd) { if(nbflag[dNW]&CFBndNoslip){ m[dSE] = RAC(ccel,dNW); LBMDS_ADDMOV(dNW,dSE); }else{ DEFAULT_STREAM_FREESLIP(dSE,dNW,nbflag[dNW]);} LBMDS_ADDMOV(dNW,dSE); } else { m[dSE] = CSRC_SE; } \
365                         if(nbflag[dNE]&CFBnd) { if(nbflag[dNE]&CFBndNoslip){ m[dSW] = RAC(ccel,dNE); LBMDS_ADDMOV(dNE,dSW); }else{ DEFAULT_STREAM_FREESLIP(dSW,dNE,nbflag[dNE]);} LBMDS_ADDMOV(dNE,dSW); } else { m[dSW] = CSRC_SW; } \
366                         if(nbflag[dSB]&CFBnd) { if(nbflag[dSB]&CFBndNoslip){ m[dNT] = RAC(ccel,dSB); LBMDS_ADDMOV(dSB,dNT); }else{ DEFAULT_STREAM_FREESLIP(dNT,dSB,nbflag[dSB]);} LBMDS_ADDMOV(dSB,dNT); } else { m[dNT] = CSRC_NT; } \
367                         if(nbflag[dST]&CFBnd) { if(nbflag[dST]&CFBndNoslip){ m[dNB] = RAC(ccel,dST); LBMDS_ADDMOV(dST,dNB); }else{ DEFAULT_STREAM_FREESLIP(dNB,dST,nbflag[dST]);} LBMDS_ADDMOV(dST,dNB); } else { m[dNB] = CSRC_NB; } \
368                         if(nbflag[dNB]&CFBnd) { if(nbflag[dNB]&CFBndNoslip){ m[dST] = RAC(ccel,dNB); LBMDS_ADDMOV(dNB,dST); }else{ DEFAULT_STREAM_FREESLIP(dST,dNB,nbflag[dNB]);} LBMDS_ADDMOV(dNB,dST); } else { m[dST] = CSRC_ST; } \
369                         if(nbflag[dNT]&CFBnd) { if(nbflag[dNT]&CFBndNoslip){ m[dSB] = RAC(ccel,dNT); LBMDS_ADDMOV(dNT,dSB); }else{ DEFAULT_STREAM_FREESLIP(dSB,dNT,nbflag[dNT]);} LBMDS_ADDMOV(dNT,dSB); } else { m[dSB] = CSRC_SB; } \
370                         if(nbflag[dWB]&CFBnd) { if(nbflag[dWB]&CFBndNoslip){ m[dET] = RAC(ccel,dWB); LBMDS_ADDMOV(dWB,dET); }else{ DEFAULT_STREAM_FREESLIP(dET,dWB,nbflag[dWB]);} LBMDS_ADDMOV(dWB,dET); } else { m[dET] = CSRC_ET; } \
371                         if(nbflag[dWT]&CFBnd) { if(nbflag[dWT]&CFBndNoslip){ m[dEB] = RAC(ccel,dWT); LBMDS_ADDMOV(dWT,dEB); }else{ DEFAULT_STREAM_FREESLIP(dEB,dWT,nbflag[dWT]);} LBMDS_ADDMOV(dWT,dEB); } else { m[dEB] = CSRC_EB; } \
372                         if(nbflag[dEB]&CFBnd) { if(nbflag[dEB]&CFBndNoslip){ m[dWT] = RAC(ccel,dEB); LBMDS_ADDMOV(dEB,dWT); }else{ DEFAULT_STREAM_FREESLIP(dWT,dEB,nbflag[dEB]);} LBMDS_ADDMOV(dEB,dWT); } else { m[dWT] = CSRC_WT; } \
373                         if(nbflag[dET]&CFBnd) { if(nbflag[dET]&CFBndNoslip){ m[dWB] = RAC(ccel,dET); LBMDS_ADDMOV(dET,dWB); }else{ DEFAULT_STREAM_FREESLIP(dWB,dET,nbflag[dET]);} LBMDS_ADDMOV(dET,dWB); } else { m[dWB] = CSRC_WB; } \
374                 } 
375
376
377
378 #define COLL_CALCULATE_DFEQ(dstarray) \
379                         dstarray[dN ] = EQN ; dstarray[dS ] = EQS ; \
380                         dstarray[dE ] = EQE ; dstarray[dW ] = EQW ; \
381                         dstarray[dT ] = EQT ; dstarray[dB ] = EQB ; \
382                         dstarray[dNE] = EQNE; dstarray[dNW] = EQNW; dstarray[dSE] = EQSE; dstarray[dSW] = EQSW; \
383                         dstarray[dNT] = EQNT; dstarray[dNB] = EQNB; dstarray[dST] = EQST; dstarray[dSB] = EQSB; \
384                         dstarray[dET] = EQET; dstarray[dEB] = EQEB; dstarray[dWT] = EQWT; dstarray[dWB] = EQWB; 
385 #define COLL_CALCULATE_NONEQTENSOR(csolev, srcArray ) \
386                         lcsmqadd  = (srcArray##NE - lcsmeq[ dNE ]); \
387                         lcsmqadd -= (srcArray##NW - lcsmeq[ dNW ]); \
388                         lcsmqadd -= (srcArray##SE - lcsmeq[ dSE ]); \
389                         lcsmqadd += (srcArray##SW - lcsmeq[ dSW ]); \
390                         lcsmqo = (lcsmqadd*    lcsmqadd); \
391                         lcsmqadd  = (srcArray##ET - lcsmeq[  dET ]); \
392                         lcsmqadd -= (srcArray##EB - lcsmeq[  dEB ]); \
393                         lcsmqadd -= (srcArray##WT - lcsmeq[  dWT ]); \
394                         lcsmqadd += (srcArray##WB - lcsmeq[  dWB ]); \
395                         lcsmqo += (lcsmqadd*    lcsmqadd); \
396                         lcsmqadd  = (srcArray##NT - lcsmeq[  dNT ]); \
397                         lcsmqadd -= (srcArray##NB - lcsmeq[  dNB ]); \
398                         lcsmqadd -= (srcArray##ST - lcsmeq[  dST ]); \
399                         lcsmqadd += (srcArray##SB - lcsmeq[  dSB ]); \
400                         lcsmqo += (lcsmqadd*    lcsmqadd); \
401                         lcsmqo *= 2.0; \
402                         lcsmqadd  = (srcArray##E  -  lcsmeq[ dE  ]); \
403                         lcsmqadd += (srcArray##W  -  lcsmeq[ dW  ]); \
404                         lcsmqadd += (srcArray##NE -  lcsmeq[ dNE ]); \
405                         lcsmqadd += (srcArray##NW -  lcsmeq[ dNW ]); \
406                         lcsmqadd += (srcArray##SE -  lcsmeq[ dSE ]); \
407                         lcsmqadd += (srcArray##SW -  lcsmeq[ dSW ]); \
408                         lcsmqadd += (srcArray##ET  - lcsmeq[ dET ]); \
409                         lcsmqadd += (srcArray##EB  - lcsmeq[ dEB ]); \
410                         lcsmqadd += (srcArray##WT  - lcsmeq[ dWT ]); \
411                         lcsmqadd += (srcArray##WB  - lcsmeq[ dWB ]); \
412                         lcsmqo += (lcsmqadd*    lcsmqadd); \
413                         lcsmqadd  = (srcArray##N  -  lcsmeq[ dN  ]); \
414                         lcsmqadd += (srcArray##S  -  lcsmeq[ dS  ]); \
415                         lcsmqadd += (srcArray##NE -  lcsmeq[ dNE ]); \
416                         lcsmqadd += (srcArray##NW -  lcsmeq[ dNW ]); \
417                         lcsmqadd += (srcArray##SE -  lcsmeq[ dSE ]); \
418                         lcsmqadd += (srcArray##SW -  lcsmeq[ dSW ]); \
419                         lcsmqadd += (srcArray##NT  - lcsmeq[ dNT ]); \
420                         lcsmqadd += (srcArray##NB  - lcsmeq[ dNB ]); \
421                         lcsmqadd += (srcArray##ST  - lcsmeq[ dST ]); \
422                         lcsmqadd += (srcArray##SB  - lcsmeq[ dSB ]); \
423                         lcsmqo += (lcsmqadd*    lcsmqadd); \
424                         lcsmqadd  = (srcArray##T  -  lcsmeq[ dT  ]); \
425                         lcsmqadd += (srcArray##B  -  lcsmeq[ dB  ]); \
426                         lcsmqadd += (srcArray##NT -  lcsmeq[ dNT ]); \
427                         lcsmqadd += (srcArray##NB -  lcsmeq[ dNB ]); \
428                         lcsmqadd += (srcArray##ST -  lcsmeq[ dST ]); \
429                         lcsmqadd += (srcArray##SB -  lcsmeq[ dSB ]); \
430                         lcsmqadd += (srcArray##ET  - lcsmeq[ dET ]); \
431                         lcsmqadd += (srcArray##EB  - lcsmeq[ dEB ]); \
432                         lcsmqadd += (srcArray##WT  - lcsmeq[ dWT ]); \
433                         lcsmqadd += (srcArray##WB  - lcsmeq[ dWB ]); \
434                         lcsmqo += (lcsmqadd*    lcsmqadd); \
435                         lcsmqo = sqrt(lcsmqo); /* FIXME check effect of sqrt*/ \
436
437 //                      COLL_CALCULATE_CSMOMEGAVAL(csolev, lcsmomega); 
438
439 // careful - need lcsmqo 
440 #define COLL_CALCULATE_CSMOMEGAVAL(csolev, dstomega ) \
441                         dstomega =  1.0/\
442                                         ( 3.0*( mLevel[(csolev)].lcnu+mLevel[(csolev)].lcsmago_sqr*(\
443                                                         -mLevel[(csolev)].lcnu + sqrt( mLevel[(csolev)].lcnu*mLevel[(csolev)].lcnu + 18.0*mLevel[(csolev)].lcsmago_sqr* lcsmqo ) \
444                                                         / (6.0*mLevel[(csolev)].lcsmago_sqr)) \
445                                                 ) +0.5 ); 
446
447 #define DEFAULT_COLLIDE_LES(grav) \
448                         rho = + MSRC_C  + MSRC_N  \
449                                 + MSRC_S  + MSRC_E  \
450                                 + MSRC_W  + MSRC_T  \
451                                 + MSRC_B  + MSRC_NE \
452                                 + MSRC_NW + MSRC_SE \
453                                 + MSRC_SW + MSRC_NT \
454                                 + MSRC_NB + MSRC_ST \
455                                 + MSRC_SB + MSRC_ET \
456                                 + MSRC_EB + MSRC_WT \
457                                 + MSRC_WB; \
458                         \
459                         ux = MSRC_E - MSRC_W \
460                                 + MSRC_NE - MSRC_NW \
461                                 + MSRC_SE - MSRC_SW \
462                                 + MSRC_ET + MSRC_EB \
463                                 - MSRC_WT - MSRC_WB ;  \
464                         \
465                         uy = MSRC_N - MSRC_S \
466                                 + MSRC_NE + MSRC_NW \
467                                 - MSRC_SE - MSRC_SW \
468                                 + MSRC_NT + MSRC_NB \
469                                 - MSRC_ST - MSRC_SB ;  \
470                         \
471                         uz = MSRC_T - MSRC_B \
472                                 + MSRC_NT - MSRC_NB \
473                                 + MSRC_ST - MSRC_SB \
474                                 + MSRC_ET - MSRC_EB \
475                                 + MSRC_WT - MSRC_WB ;  \
476                         PRECOLLIDE_MODS(rho,ux,uy,uz, grav); \
477                         usqr = 1.5 * (ux*ux + uy*uy + uz*uz); \
478                         COLL_CALCULATE_DFEQ(lcsmeq); \
479                         COLL_CALCULATE_NONEQTENSOR(lev, MSRC_); \
480                         COLL_CALCULATE_CSMOMEGAVAL(lev, lcsmomega); \
481                         CSMOMEGA_STATS(lev,lcsmomega); \
482                         \
483                         RAC(tcel,dC ) = (1.0-lcsmomega)*MSRC_C  + lcsmomega*EQC ; \
484                         \
485                         RAC(tcel,dN ) = (1.0-lcsmomega)*MSRC_N  + lcsmomega*lcsmeq[ dN ]; \
486                         RAC(tcel,dS ) = (1.0-lcsmomega)*MSRC_S  + lcsmomega*lcsmeq[ dS ]; \
487                         RAC(tcel,dE ) = (1.0-lcsmomega)*MSRC_E  + lcsmomega*lcsmeq[ dE ]; \
488                         RAC(tcel,dW ) = (1.0-lcsmomega)*MSRC_W  + lcsmomega*lcsmeq[ dW ]; \
489                         RAC(tcel,dT ) = (1.0-lcsmomega)*MSRC_T  + lcsmomega*lcsmeq[ dT ]; \
490                         RAC(tcel,dB ) = (1.0-lcsmomega)*MSRC_B  + lcsmomega*lcsmeq[ dB ]; \
491                         \
492                         RAC(tcel,dNE) = (1.0-lcsmomega)*MSRC_NE + lcsmomega*lcsmeq[ dNE]; \
493                         RAC(tcel,dNW) = (1.0-lcsmomega)*MSRC_NW + lcsmomega*lcsmeq[ dNW]; \
494                         RAC(tcel,dSE) = (1.0-lcsmomega)*MSRC_SE + lcsmomega*lcsmeq[ dSE]; \
495                         RAC(tcel,dSW) = (1.0-lcsmomega)*MSRC_SW + lcsmomega*lcsmeq[ dSW]; \
496                         RAC(tcel,dNT) = (1.0-lcsmomega)*MSRC_NT + lcsmomega*lcsmeq[ dNT]; \
497                         RAC(tcel,dNB) = (1.0-lcsmomega)*MSRC_NB + lcsmomega*lcsmeq[ dNB]; \
498                         RAC(tcel,dST) = (1.0-lcsmomega)*MSRC_ST + lcsmomega*lcsmeq[ dST]; \
499                         RAC(tcel,dSB) = (1.0-lcsmomega)*MSRC_SB + lcsmomega*lcsmeq[ dSB]; \
500                         RAC(tcel,dET) = (1.0-lcsmomega)*MSRC_ET + lcsmomega*lcsmeq[ dET]; \
501                         RAC(tcel,dEB) = (1.0-lcsmomega)*MSRC_EB + lcsmomega*lcsmeq[ dEB]; \
502                         RAC(tcel,dWT) = (1.0-lcsmomega)*MSRC_WT + lcsmomega*lcsmeq[ dWT]; \
503                         RAC(tcel,dWB) = (1.0-lcsmomega)*MSRC_WB + lcsmomega*lcsmeq[ dWB]; 
504
505 #define DEFAULT_COLLIDE_NOLES(grav) \
506                         rho = + MSRC_C  + MSRC_N  \
507                                 + MSRC_S  + MSRC_E  \
508                                 + MSRC_W  + MSRC_T  \
509                                 + MSRC_B  + MSRC_NE \
510                                 + MSRC_NW + MSRC_SE \
511                                 + MSRC_SW + MSRC_NT \
512                                 + MSRC_NB + MSRC_ST \
513                                 + MSRC_SB + MSRC_ET \
514                                 + MSRC_EB + MSRC_WT \
515                                 + MSRC_WB; \
516                         \
517                         ux = MSRC_E - MSRC_W \
518                                 + MSRC_NE - MSRC_NW \
519                                 + MSRC_SE - MSRC_SW \
520                                 + MSRC_ET + MSRC_EB \
521                                 - MSRC_WT - MSRC_WB ;  \
522                         \
523                         uy = MSRC_N - MSRC_S \
524                                 + MSRC_NE + MSRC_NW \
525                                 - MSRC_SE - MSRC_SW \
526                                 + MSRC_NT + MSRC_NB \
527                                 - MSRC_ST - MSRC_SB ;  \
528                         \
529                         uz = MSRC_T - MSRC_B \
530                                 + MSRC_NT - MSRC_NB \
531                                 + MSRC_ST - MSRC_SB \
532                                 + MSRC_ET - MSRC_EB \
533                                 + MSRC_WT - MSRC_WB ;  \
534                         PRECOLLIDE_MODS(rho, ux,uy,uz, grav); \
535                         usqr = 1.5 * (ux*ux + uy*uy + uz*uz); \
536                         \
537                         RAC(tcel,dC ) = (1.0-OMEGA(lev))*MSRC_C  + OMEGA(lev)*EQC ; \
538                         \
539                         RAC(tcel,dN ) = (1.0-OMEGA(lev))*MSRC_N  + OMEGA(lev)*EQN ; \
540                         RAC(tcel,dS ) = (1.0-OMEGA(lev))*MSRC_S  + OMEGA(lev)*EQS ; \
541                         RAC(tcel,dE ) = (1.0-OMEGA(lev))*MSRC_E  + OMEGA(lev)*EQE ; \
542                         RAC(tcel,dW ) = (1.0-OMEGA(lev))*MSRC_W  + OMEGA(lev)*EQW ; \
543                         RAC(tcel,dT ) = (1.0-OMEGA(lev))*MSRC_T  + OMEGA(lev)*EQT ; \
544                         RAC(tcel,dB ) = (1.0-OMEGA(lev))*MSRC_B  + OMEGA(lev)*EQB ; \
545                         \
546                         RAC(tcel,dNE) = (1.0-OMEGA(lev))*MSRC_NE + OMEGA(lev)*EQNE; \
547                         RAC(tcel,dNW) = (1.0-OMEGA(lev))*MSRC_NW + OMEGA(lev)*EQNW; \
548                         RAC(tcel,dSE) = (1.0-OMEGA(lev))*MSRC_SE + OMEGA(lev)*EQSE; \
549                         RAC(tcel,dSW) = (1.0-OMEGA(lev))*MSRC_SW + OMEGA(lev)*EQSW; \
550                         RAC(tcel,dNT) = (1.0-OMEGA(lev))*MSRC_NT + OMEGA(lev)*EQNT; \
551                         RAC(tcel,dNB) = (1.0-OMEGA(lev))*MSRC_NB + OMEGA(lev)*EQNB; \
552                         RAC(tcel,dST) = (1.0-OMEGA(lev))*MSRC_ST + OMEGA(lev)*EQST; \
553                         RAC(tcel,dSB) = (1.0-OMEGA(lev))*MSRC_SB + OMEGA(lev)*EQSB; \
554                         RAC(tcel,dET) = (1.0-OMEGA(lev))*MSRC_ET + OMEGA(lev)*EQET; \
555                         RAC(tcel,dEB) = (1.0-OMEGA(lev))*MSRC_EB + OMEGA(lev)*EQEB; \
556                         RAC(tcel,dWT) = (1.0-OMEGA(lev))*MSRC_WT + OMEGA(lev)*EQWT; \
557                         RAC(tcel,dWB) = (1.0-OMEGA(lev))*MSRC_WB + OMEGA(lev)*EQWB; 
558
559
560
561 #define OPTIMIZED_STREAMCOLLIDE_LES \
562                         /* only surrounded by fluid cells...!, so safe streaming here... */ \
563                         m[dC ] = CSRC_C ; \
564                         m[dN ] = CSRC_N ; m[dS ] = CSRC_S ; \
565                         m[dE ] = CSRC_E ; m[dW ] = CSRC_W ; \
566                         m[dT ] = CSRC_T ; m[dB ] = CSRC_B ; \
567                         m[dNE] = CSRC_NE; m[dNW] = CSRC_NW; m[dSE] = CSRC_SE; m[dSW] = CSRC_SW; \
568                         m[dNT] = CSRC_NT; m[dNB] = CSRC_NB; m[dST] = CSRC_ST; m[dSB] = CSRC_SB; \
569                         m[dET] = CSRC_ET; m[dEB] = CSRC_EB; m[dWT] = CSRC_WT; m[dWB] = CSRC_WB; \
570                         \
571                         rho = MSRC_C  + MSRC_N + MSRC_S  + MSRC_E + MSRC_W  + MSRC_T  \
572                                 + MSRC_B  + MSRC_NE + MSRC_NW + MSRC_SE + MSRC_SW + MSRC_NT \
573                                 + MSRC_NB + MSRC_ST + MSRC_SB + MSRC_ET + MSRC_EB + MSRC_WT + MSRC_WB; \
574                         ux = MSRC_E - MSRC_W + MSRC_NE - MSRC_NW + MSRC_SE - MSRC_SW \
575                                 + MSRC_ET + MSRC_EB - MSRC_WT - MSRC_WB;  \
576                         uy = MSRC_N - MSRC_S + MSRC_NE + MSRC_NW - MSRC_SE - MSRC_SW \
577                                 + MSRC_NT + MSRC_NB - MSRC_ST - MSRC_SB;  \
578                         uz = MSRC_T - MSRC_B + MSRC_NT - MSRC_NB + MSRC_ST - MSRC_SB \
579                                 + MSRC_ET - MSRC_EB + MSRC_WT - MSRC_WB;  \
580                         PRECOLLIDE_MODS(rho, ux,uy,uz, mLevel[lev].gravity); \
581                         usqr = 1.5 * (ux*ux + uy*uy + uz*uz); \
582                         COLL_CALCULATE_DFEQ(lcsmeq); \
583                         COLL_CALCULATE_NONEQTENSOR(lev, MSRC_) \
584                         COLL_CALCULATE_CSMOMEGAVAL(lev, lcsmomega); \
585                         CSMOMEGA_STATS(lev,lcsmomega); \
586                         \
587                         RAC(tcel,dC ) = (1.0-lcsmomega)*MSRC_C  + lcsmomega*EQC ; \
588                         RAC(tcel,dN ) = (1.0-lcsmomega)*MSRC_N  + lcsmomega*lcsmeq[ dN ];  \
589                         RAC(tcel,dS ) = (1.0-lcsmomega)*MSRC_S  + lcsmomega*lcsmeq[ dS ];  \
590                         RAC(tcel,dE ) = (1.0-lcsmomega)*MSRC_E  + lcsmomega*lcsmeq[ dE ]; \
591                         RAC(tcel,dW ) = (1.0-lcsmomega)*MSRC_W  + lcsmomega*lcsmeq[ dW ];  \
592                         RAC(tcel,dT ) = (1.0-lcsmomega)*MSRC_T  + lcsmomega*lcsmeq[ dT ];  \
593                         RAC(tcel,dB ) = (1.0-lcsmomega)*MSRC_B  + lcsmomega*lcsmeq[ dB ]; \
594                         \
595                         RAC(tcel,dNE) = (1.0-lcsmomega)*MSRC_NE + lcsmomega*lcsmeq[ dNE];  \
596                         RAC(tcel,dNW) = (1.0-lcsmomega)*MSRC_NW + lcsmomega*lcsmeq[ dNW];  \
597                         RAC(tcel,dSE) = (1.0-lcsmomega)*MSRC_SE + lcsmomega*lcsmeq[ dSE];  \
598                         RAC(tcel,dSW) = (1.0-lcsmomega)*MSRC_SW + lcsmomega*lcsmeq[ dSW]; \
599                         \
600                         RAC(tcel,dNT) = (1.0-lcsmomega)*MSRC_NT + lcsmomega*lcsmeq[ dNT];  \
601                         RAC(tcel,dNB) = (1.0-lcsmomega)*MSRC_NB + lcsmomega*lcsmeq[ dNB];  \
602                         RAC(tcel,dST) = (1.0-lcsmomega)*MSRC_ST + lcsmomega*lcsmeq[ dST];  \
603                         RAC(tcel,dSB) = (1.0-lcsmomega)*MSRC_SB + lcsmomega*lcsmeq[ dSB]; \
604                         \
605                         RAC(tcel,dET) = (1.0-lcsmomega)*MSRC_ET + lcsmomega*lcsmeq[ dET];  \
606                         RAC(tcel,dEB) = (1.0-lcsmomega)*MSRC_EB + lcsmomega*lcsmeq[ dEB]; \
607                         RAC(tcel,dWT) = (1.0-lcsmomega)*MSRC_WT + lcsmomega*lcsmeq[ dWT];  \
608                         RAC(tcel,dWB) = (1.0-lcsmomega)*MSRC_WB + lcsmomega*lcsmeq[ dWB];  \
609
610 #define OPTIMIZED_STREAMCOLLIDE_UNUSED \
611                         /* only surrounded by fluid cells...!, so safe streaming here... */ \
612                         rho = CSRC_C  + CSRC_N + CSRC_S  + CSRC_E + CSRC_W  + CSRC_T  \
613                                 + CSRC_B  + CSRC_NE + CSRC_NW + CSRC_SE + CSRC_SW + CSRC_NT \
614                                 + CSRC_NB + CSRC_ST + CSRC_SB + CSRC_ET + CSRC_EB + CSRC_WT + CSRC_WB; \
615                         ux = CSRC_E - CSRC_W + CSRC_NE - CSRC_NW + CSRC_SE - CSRC_SW \
616                                 + CSRC_ET + CSRC_EB - CSRC_WT - CSRC_WB;  \
617                         uy = CSRC_N - CSRC_S + CSRC_NE + CSRC_NW - CSRC_SE - CSRC_SW \
618                                 + CSRC_NT + CSRC_NB - CSRC_ST - CSRC_SB;  \
619                         uz = CSRC_T - CSRC_B + CSRC_NT - CSRC_NB + CSRC_ST - CSRC_SB \
620                                 + CSRC_ET - CSRC_EB + CSRC_WT - CSRC_WB;  \
621                         PRECOLLIDE_MODS(rho, ux,uy,uz, mLevel[lev].gravity); \
622                         usqr = 1.5 * (ux*ux + uy*uy + uz*uz); \
623                         COLL_CALCULATE_DFEQ(lcsmeq); \
624                         COLL_CALCULATE_NONEQTENSOR(lev, CSRC_) \
625                         COLL_CALCULATE_CSMOMEGAVAL(lev, lcsmomega); \
626                         \
627                         RAC(tcel,dC ) = (1.0-lcsmomega)*CSRC_C  + lcsmomega*EQC ; \
628                         RAC(tcel,dN ) = (1.0-lcsmomega)*CSRC_N  + lcsmomega*lcsmeq[ dN ];  \
629                         RAC(tcel,dS ) = (1.0-lcsmomega)*CSRC_S  + lcsmomega*lcsmeq[ dS ];  \
630                         RAC(tcel,dE ) = (1.0-lcsmomega)*CSRC_E  + lcsmomega*lcsmeq[ dE ]; \
631                         RAC(tcel,dW ) = (1.0-lcsmomega)*CSRC_W  + lcsmomega*lcsmeq[ dW ];  \
632                         RAC(tcel,dT ) = (1.0-lcsmomega)*CSRC_T  + lcsmomega*lcsmeq[ dT ];  \
633                         RAC(tcel,dB ) = (1.0-lcsmomega)*CSRC_B  + lcsmomega*lcsmeq[ dB ]; \
634                         \
635                         RAC(tcel,dNE) = (1.0-lcsmomega)*CSRC_NE + lcsmomega*lcsmeq[ dNE];  \
636                         RAC(tcel,dNW) = (1.0-lcsmomega)*CSRC_NW + lcsmomega*lcsmeq[ dNW];  \
637                         RAC(tcel,dSE) = (1.0-lcsmomega)*CSRC_SE + lcsmomega*lcsmeq[ dSE];  \
638                         RAC(tcel,dSW) = (1.0-lcsmomega)*CSRC_SW + lcsmomega*lcsmeq[ dSW]; \
639                         \
640                         RAC(tcel,dNT) = (1.0-lcsmomega)*CSRC_NT + lcsmomega*lcsmeq[ dNT];  \
641                         RAC(tcel,dNB) = (1.0-lcsmomega)*CSRC_NB + lcsmomega*lcsmeq[ dNB];  \
642                         RAC(tcel,dST) = (1.0-lcsmomega)*CSRC_ST + lcsmomega*lcsmeq[ dST];  \
643                         RAC(tcel,dSB) = (1.0-lcsmomega)*CSRC_SB + lcsmomega*lcsmeq[ dSB]; \
644                         \
645                         RAC(tcel,dET) = (1.0-lcsmomega)*CSRC_ET + lcsmomega*lcsmeq[ dET];  \
646                         RAC(tcel,dEB) = (1.0-lcsmomega)*CSRC_EB + lcsmomega*lcsmeq[ dEB]; \
647                         RAC(tcel,dWT) = (1.0-lcsmomega)*CSRC_WT + lcsmomega*lcsmeq[ dWT];  \
648                         RAC(tcel,dWB) = (1.0-lcsmomega)*CSRC_WB + lcsmomega*lcsmeq[ dWB];  \
649
650 #define OPTIMIZED_STREAMCOLLIDE_NOLES \
651                         /* only surrounded by fluid cells...!, so safe streaming here... */ \
652                         rho = CSRC_C  + CSRC_N + CSRC_S  + CSRC_E + CSRC_W  + CSRC_T  \
653                                 + CSRC_B  + CSRC_NE + CSRC_NW + CSRC_SE + CSRC_SW + CSRC_NT \
654                                 + CSRC_NB + CSRC_ST + CSRC_SB + CSRC_ET + CSRC_EB + CSRC_WT + CSRC_WB; \
655                         ux = CSRC_E - CSRC_W + CSRC_NE - CSRC_NW + CSRC_SE - CSRC_SW \
656                                 + CSRC_ET + CSRC_EB - CSRC_WT - CSRC_WB;  \
657                         uy = CSRC_N - CSRC_S + CSRC_NE + CSRC_NW - CSRC_SE - CSRC_SW \
658                                 + CSRC_NT + CSRC_NB - CSRC_ST - CSRC_SB;  \
659                         uz = CSRC_T - CSRC_B + CSRC_NT - CSRC_NB + CSRC_ST - CSRC_SB \
660                                 + CSRC_ET - CSRC_EB + CSRC_WT - CSRC_WB;  \
661                         PRECOLLIDE_MODS(rho, ux,uy,uz, mLevel[lev].gravity); \
662                         usqr = 1.5 * (ux*ux + uy*uy + uz*uz); \
663                         RAC(tcel,dC ) = (1.0-OMEGA(lev))*CSRC_C  + OMEGA(lev)*EQC ; \
664                         RAC(tcel,dN ) = (1.0-OMEGA(lev))*CSRC_N  + OMEGA(lev)*EQN ;  \
665                         RAC(tcel,dS ) = (1.0-OMEGA(lev))*CSRC_S  + OMEGA(lev)*EQS ;  \
666                         RAC(tcel,dE ) = (1.0-OMEGA(lev))*CSRC_E  + OMEGA(lev)*EQE ; \
667                         RAC(tcel,dW ) = (1.0-OMEGA(lev))*CSRC_W  + OMEGA(lev)*EQW ;  \
668                         RAC(tcel,dT ) = (1.0-OMEGA(lev))*CSRC_T  + OMEGA(lev)*EQT ;  \
669                         RAC(tcel,dB ) = (1.0-OMEGA(lev))*CSRC_B  + OMEGA(lev)*EQB ; \
670                          \
671                         RAC(tcel,dNE) = (1.0-OMEGA(lev))*CSRC_NE + OMEGA(lev)*EQNE;  \
672                         RAC(tcel,dNW) = (1.0-OMEGA(lev))*CSRC_NW + OMEGA(lev)*EQNW;  \
673                         RAC(tcel,dSE) = (1.0-OMEGA(lev))*CSRC_SE + OMEGA(lev)*EQSE;  \
674                         RAC(tcel,dSW) = (1.0-OMEGA(lev))*CSRC_SW + OMEGA(lev)*EQSW; \
675                          \
676                         RAC(tcel,dNT) = (1.0-OMEGA(lev))*CSRC_NT + OMEGA(lev)*EQNT;  \
677                         RAC(tcel,dNB) = (1.0-OMEGA(lev))*CSRC_NB + OMEGA(lev)*EQNB;  \
678                         RAC(tcel,dST) = (1.0-OMEGA(lev))*CSRC_ST + OMEGA(lev)*EQST;  \
679                         RAC(tcel,dSB) = (1.0-OMEGA(lev))*CSRC_SB + OMEGA(lev)*EQSB; \
680                          \
681                         RAC(tcel,dET) = (1.0-OMEGA(lev))*CSRC_ET + OMEGA(lev)*EQET;  \
682                         RAC(tcel,dEB) = (1.0-OMEGA(lev))*CSRC_EB + OMEGA(lev)*EQEB; \
683                         RAC(tcel,dWT) = (1.0-OMEGA(lev))*CSRC_WT + OMEGA(lev)*EQWT;  \
684                         RAC(tcel,dWB) = (1.0-OMEGA(lev))*CSRC_WB + OMEGA(lev)*EQWB;  \
685
686
687 // debug version1
688 #define STREAMCHECK(ni,nj,nk,nl) 
689 #define COLLCHECK
690 #define OPTIMIZED_STREAMCOLLIDE_DEBUG \
691                         m[0] = RAC(ccel,0); \
692                         FORDF1 { /* df0 is set later on... */ \
693                                 if(RFLAG_NB(lev, i,j,k,SRCS(lev),l)&CFBnd) { errMsg("???", "bnd-err-nobndfl"); CAUSE_PANIC;\
694                                 } else { m[l] = QCELL_NBINV(lev, i, j, k, SRCS(lev), l, l); } \
695                                 STREAMCHECK(9, i+this->dfVecX[this->dfInv[l]], j+this->dfVecY[this->dfInv[l]],k+this->dfVecZ[this->dfInv[l]], l); \
696                         }   \
697                         /* rho=m[0]; ux = mLevel[lev].gravity[0]; uy = mLevel[lev].gravity[1]; uz = mLevel[lev].gravity[2]; */ \
698                         this->collideArrays(i,j,k, m, rho,ux,uy,uz, OMEGA(lev), mLevel[lev].gravity, mLevel[lev].lcsmago , &mDebugOmegaRet, &lcsmqo   ); \
699                         CSMOMEGA_STATS(lev,mDebugOmegaRet); \
700                         FORDF0 { RAC(tcel,l) = m[l]; } \
701                         usqr = 1.5 * (ux*ux + uy*uy + uz*uz);  \
702                         COLLCHECK;
703
704
705
706 // more debugging
707 /*DEBUG \
708                         m[0] = RAC(ccel,0); \
709                         FORDF1 { \
710                                 if(RFLAG_NB(lev, i,j,k,SRCS(lev),l)&CFBnd) { errMsg("???", "bnd-err-nobndfl");CAUSE_PANIC; \
711                                 } else { m[l] = QCELL_NBINV(lev, i, j, k, SRCS(lev), l, l); } \
712                         }   \
713 errMsg("T","QSDM at %d,%d,%d  lcsmqo=%25.15f, lcsmomega=%f \n", i,j,k, lcsmqo,lcsmomega ); \
714                         rho=m[0]; ux = mLevel[lev].gravity[0]; uy = mLevel[lev].gravity[1]; uz = mLevel[lev].gravity[2]; \
715                         this->collideArrays(i,j,k, m, rho,ux,uy,uz, OMEGA(lev), mLevel[lev].lcsmago  , &mDebugOmegaRet, &lcsmqo  ); \
716                         CSMOMEGA_STATS(lev,mDebugOmegaRet); \
717                         */
718 #if USE_LES==1
719 #define DEFAULT_COLLIDEG(grav) DEFAULT_COLLIDE_LES(grav)
720 #define OPTIMIZED_STREAMCOLLIDE OPTIMIZED_STREAMCOLLIDE_LES
721 #else 
722 #define DEFAULT_COLLIDEG(grav) DEFAULT_COLLIDE_NOLES(grav)
723 #define OPTIMIZED_STREAMCOLLIDE OPTIMIZED_STREAMCOLLIDE_NOLES
724 #endif
725
726 #endif
727
728 #define USQRMAXCHECK(Cusqr,Cux,Cuy,Cuz,  CmMaxVlen,CmMxvx,CmMxvy,CmMxvz) \
729                         if(Cusqr>CmMaxVlen) { \
730                                 CmMxvx = Cux; CmMxvy = Cuy; CmMxvz = Cuz; CmMaxVlen = Cusqr; \
731                         } /* stats */ 
732
733
734
735 /******************************************************************************
736  * interpolateCellFromCoarse macros
737  *****************************************************************************/
738
739
740 // WOXDY_N = Weight Order X Dimension Y _ number N
741 #define WO1D1   ( 1.0/ 2.0)
742 #define WO1D2   ( 1.0/ 4.0)
743 #define WO1D3   ( 1.0/ 8.0)
744
745 #define WO2D1_1 (-1.0/16.0)
746 #define WO2D1_9 ( 9.0/16.0)
747
748 #define WO2D2_11 (WO2D1_1 * WO2D1_1)
749 #define WO2D2_19 (WO2D1_9 * WO2D1_1)
750 #define WO2D2_91 (WO2D1_9 * WO2D1_1)
751 #define WO2D2_99 (WO2D1_9 * WO2D1_9)
752
753 #define WO2D3_111 (WO2D1_1 * WO2D1_1 * WO2D1_1)
754 #define WO2D3_191 (WO2D1_9 * WO2D1_1 * WO2D1_1)
755 #define WO2D3_911 (WO2D1_9 * WO2D1_1 * WO2D1_1)
756 #define WO2D3_991 (WO2D1_9 * WO2D1_9 * WO2D1_1)
757 #define WO2D3_119 (WO2D1_1 * WO2D1_1 * WO2D1_9)
758 #define WO2D3_199 (WO2D1_9 * WO2D1_1 * WO2D1_9)
759 #define WO2D3_919 (WO2D1_9 * WO2D1_1 * WO2D1_9)
760 #define WO2D3_999 (WO2D1_9 * WO2D1_9 * WO2D1_9)
761
762 #if FSGR_STRICT_DEBUG==1
763 #define ADD_INT_DFSCHECK(alev, ai,aj,ak, at, afac, l) \
764                                 if(     (((1.0-(at))>0.0) && (!(QCELL((alev), (ai),(aj),(ak),mLevel[(alev)].setCurr , l) > -1.0 ))) || \
765                                                 (((    (at))>0.0) && (!(QCELL((alev), (ai),(aj),(ak),mLevel[(alev)].setOther, l) > -1.0 ))) ){ \
766                                         errMsg("INVDFSCHECK", " l"<<(alev)<<" "<<PRINT_VEC((ai),(aj),(ak))<<" fc:"<<RFLAG((alev), (ai),(aj),(ak),mLevel[(alev)].setCurr )<<" fo:"<<RFLAG((alev), (ai),(aj),(ak),mLevel[(alev)].setOther )<<" dfl"<<l ); \
767                                         debugMarkCell((alev), (ai),(aj),(ak));\
768                                         CAUSE_PANIC; \
769                                 }
770                                 // end ADD_INT_DFSCHECK
771 #define ADD_INT_FLAGCHECK(alev, ai,aj,ak, at, afac) \
772                                 if(     (((1.0-(at))>0.0) && (!(RFLAG((alev), (ai),(aj),(ak),mLevel[(alev)].setCurr )&(CFInter|CFFluid|CFGrCoarseInited) ))) || \
773                                                 (((    (at))>0.0) && (!(RFLAG((alev), (ai),(aj),(ak),mLevel[(alev)].setOther)&(CFInter|CFFluid|CFGrCoarseInited) ))) ){ \
774                                         errMsg("INVFLAGCINTCHECK", " l"<<(alev)<<" at:"<<(at)<<" "<<PRINT_VEC((ai),(aj),(ak))<<\
775                                                         " fc:"<<   convertCellFlagType2String(RFLAG((alev), (ai),(aj),(ak),mLevel[(alev)].setCurr  )) <<\
776                                                         " fold:"<< convertCellFlagType2String(RFLAG((alev), (ai),(aj),(ak),mLevel[(alev)].setOther )) ); \
777                                         debugMarkCell((alev), (ai),(aj),(ak));\
778                                         CAUSE_PANIC; \
779                                 }
780                                 // end ADD_INT_DFSCHECK
781                                 
782                                 //if(   !(RFLAG(lev+1, (ix),(iy),(iz), mLevel[lev+1].setCurr) & CFUnused) ){
783                                 //errMsg("INTFLAGUNU", PRINT_VEC(i,j,k)<<" child at "<<PRINT_VEC((ix),(iy),(iz)) );
784                                 //if(iy==15) errMsg("IFFC", PRINT_VEC(i,j,k)<<" child interpolated at "<<PRINT_VEC((ix),(iy),(iz)) );
785                                 //if(((ix)>10)&&(iy>5)&&(iz>5)) { debugMarkCell(lev+1, (ix),(iy),(iz) ); }
786 #define INTUNUTCHECK(ix,iy,iz) \
787                                 if(     (RFLAG(lev+1, (ix),(iy),(iz), mLevel[lev+1].setCurr) != (CFFluid|CFGrFromCoarse)) ){\
788                                         errMsg("INTFLAGUNU_CHECK", PRINT_VEC(i,j,k)<<" child not unused at l"<<(lev+1)<<" "<<PRINT_VEC((ix),(iy),(iz))<<" flag: "<<  RFLAG(lev+1, (ix),(iy),(iz), mLevel[lev+1].setCurr) ); \
789                                         debugMarkCell((lev+1), (ix),(iy),(iz));\
790                                         CAUSE_PANIC; \
791                                 }\
792                                 RFLAG(lev+1, (ix),(iy),(iz), mLevel[lev+1].setCurr) |= CFGrCoarseInited; \
793                                 // INTUNUTCHECK 
794 #define INTSTRICTCHECK(ix,iy,iz,caseId) \
795                                 if(     QCELL(lev+1, (ix),(iy),(iz), mLevel[lev+1].setCurr, l) <= 0.0 ){\
796                                         errMsg("INVDFCCELLCHECK", "caseId:"<<caseId<<" "<<PRINT_VEC(i,j,k)<<" child inter at "<<PRINT_VEC((ix),(iy),(iz))<<" invalid df "<<l<<" = "<< QCELL(lev+1, (ix),(iy),(iz), mLevel[lev+1].setCurr, l) ); \
797                                         debugMarkCell((lev+1), (ix),(iy),(iz));\
798                                         CAUSE_PANIC; \
799                                 }\
800                                 // INTSTRICTCHECK
801
802 #else// FSGR_STRICT_DEBUG==1
803 #define ADD_INT_FLAGCHECK(alev, ai,aj,ak, at, afac) 
804 #define ADD_INT_DFSCHECK(alev, ai,aj,ak, at, afac, l) 
805 #define INTSTRICTCHECK(x,y,z,caseId) 
806 #define INTUNUTCHECK(ix,iy,iz) 
807 #endif// FSGR_STRICT_DEBUG==1
808
809
810 #if FSGR_STRICT_DEBUG==1
811 #define INTDEBOUT \
812                 { /*LbmFloat rho,ux,uy,uz;*/ \
813                         rho = ux=uy=uz=0.0; \
814                         FORDF0{ LbmFloat m = QCELL(lev,i,j,k, dstSet, l); \
815                                 rho += m; ux  += (this->dfDvecX[l]*m); uy  += (this->dfDvecY[l]*m); uz  += (this->dfDvecZ[l]*m);  \
816                                 if(ABS(m)>1.0) { errMsg("interpolateCellFromCoarse", "ICFC_DFCHECK cell  "<<PRINT_IJK<<" m"<<l<<":"<< m );CAUSE_PANIC;}\
817                                 /*errMsg("interpolateCellFromCoarse", " cell "<<PRINT_IJK<<" df"<<l<<":"<<m );*/ \
818                         }  \
819                         /*if(this->mPanic) { errMsg("interpolateCellFromCoarse", "ICFC_DFOUT cell  "<<PRINT_IJK<<" rho:"<<rho<<" u:"<<PRINT_VEC(ux,uy,uz)<<" b"<<PRINT_VEC(betx,bety,betz) ); }*/ \
820                         if(markNbs) errMsg("interpolateCellFromCoarse", " cell "<<PRINT_IJK<<" rho:"<<rho<<" u:"<<PRINT_VEC(ux,uy,uz)<<" b"<<PRINT_VEC(betx,bety,betz) );  \
821                         /*errMsg("interpolateCellFromCoarse", "ICFC_DFDEBUG cell  "<<PRINT_IJK<<" rho:"<<rho<<" u:"<<PRINT_VEC(ux,uy,uz)<<" b"<<PRINT_VEC(betx,bety,betz) ); */\
822                 } \
823                 /* both cases are ok to interpolate */  \
824                 if( (!(RFLAG(lev,i,j,k, dstSet) & CFGrFromCoarse)) &&   \
825                                 (!(RFLAG(lev,i,j,k, dstSet) & CFUnused)) ) {    \
826                         /* might also have CFGrCoarseInited (shouldnt be a problem here)*/      \
827                         errMsg("interpolateCellFromCoarse", "CHECK cell not CFGrFromCoarse? "<<PRINT_IJK<<" flag:"<< RFLAG(lev,i,j,k, dstSet)<<" fstr:"<<convertCellFlagType2String(  RFLAG(lev,i,j,k, dstSet) ));      \
828                         /* FIXME check this warning...? return; this can happen !? */   \
829                         /*CAUSE_PANIC;*/        \
830                 }       \
831                 // end INTDEBOUT
832 #else // FSGR_STRICT_DEBUG==1
833 #define INTDEBOUT 
834 #endif // FSGR_STRICT_DEBUG==1
835
836         
837 // t=0.0 -> only current
838 // t=0.5 -> mix
839 // t=1.0 -> only other
840 #if OPT3D==0 
841 #define ADD_INT_DFS(alev, ai,aj,ak, at, afac) \
842                                                 ADD_INT_FLAGCHECK(alev, ai,aj,ak, at, afac); \
843                                                 FORDF0{ \
844                                                         LbmFloat df = ( \
845                                                                         QCELL((alev), (ai),(aj),(ak),mLevel[(alev)].setCurr , l)*(1.0-(at)) + \
846                                                                         QCELL((alev), (ai),(aj),(ak),mLevel[(alev)].setOther, l)*(    (at)) \
847                                                                         ) ; \
848                                                         ADD_INT_DFSCHECK(alev, ai,aj,ak, at, afac, l); \
849                                                         df *= (afac); \
850                                                         rho += df;  \
851                                                         ux  += (this->dfDvecX[l]*df);  \
852                                                         uy  += (this->dfDvecY[l]*df);   \
853                                                         uz  += (this->dfDvecZ[l]*df);   \
854                                                         intDf[l] += df; \
855                                                 } 
856 // write interpolated dfs back to cell (correct non-eq. parts)
857 #define IDF_WRITEBACK_ \
858                 FORDF0{ \
859                         LbmFloat eq = getCollideEq(l, rho,ux,uy,uz);\
860                         QCELL(lev,i,j,k, dstSet, l) = (eq+ (intDf[l]-eq)*mDfScaleDown);\
861                 } \
862                 /* check that all values are ok */ \
863                 INTDEBOUT
864 #define IDF_WRITEBACK \
865                 LbmFloat omegaDst, omegaSrc;\
866                 /* smago new */ \
867                 LbmFloat feq[LBM_DFNUM]; \
868                 LbmFloat dfScale = mDfScaleDown; \
869                 FORDF0{ \
870                         feq[l] = getCollideEq(l, rho,ux,uy,uz); \
871                 } \
872                 if(mLevel[lev  ].lcsmago>0.0) {\
873                         LbmFloat Qo = this->getLesNoneqTensorCoeff(intDf,feq); \
874                         omegaDst  = this->getLesOmega(mLevel[lev+0].omega,mLevel[lev+0].lcsmago,Qo); \
875                         omegaSrc = this->getLesOmega(mLevel[lev-1].omega,mLevel[lev-1].lcsmago,Qo); \
876                 } else {\
877                         omegaDst = mLevel[lev+0].omega; \
878                         omegaSrc = mLevel[lev-1].omega;\
879                 } \
880                  \
881                 dfScale   = (mLevel[lev+0].timestep/mLevel[lev-1].timestep)* (1.0/omegaDst-1.0)/ (1.0/omegaSrc-1.0);  \
882                 FORDF0{ \
883                         /*errMsg("SMAGO"," org"<<mDfScaleDown<<" n"<<dfScale<<" qc"<< QCELL(lev,i,j,k, dstSet, l)<<" idf"<<intDf[l]<<" eq"<<feq[l] ); */ \
884                         QCELL(lev,i,j,k, dstSet, l) = (feq[l]+ (intDf[l]-feq[l])*dfScale);\
885                 } \
886                 /* check that all values are ok */ \
887                 INTDEBOUT
888
889 #else //OPT3D==0 
890
891 #define ADDALLVALS \
892         addVal = addDfFacT * RAC(addfcel , dC ); \
893                                                 intDf[dC ] += addVal; rho += addVal; \
894         addVal  = addDfFacT * RAC(addfcel , dN ); \
895                      uy+=addVal;               intDf[dN ] += addVal; rho += addVal; \
896         addVal  = addDfFacT * RAC(addfcel , dS ); \
897                      uy-=addVal;               intDf[dS ] += addVal; rho += addVal; \
898         addVal  = addDfFacT * RAC(addfcel , dE ); \
899         ux+=addVal;                            intDf[dE ] += addVal; rho += addVal; \
900         addVal  = addDfFacT * RAC(addfcel , dW ); \
901         ux-=addVal;                            intDf[dW ] += addVal; rho += addVal; \
902         addVal  = addDfFacT * RAC(addfcel , dT ); \
903                                   uz+=addVal;  intDf[dT ] += addVal; rho += addVal; \
904         addVal  = addDfFacT * RAC(addfcel , dB ); \
905                                   uz-=addVal;  intDf[dB ] += addVal; rho += addVal; \
906         addVal  = addDfFacT * RAC(addfcel , dNE); \
907         ux+=addVal; uy+=addVal;               intDf[dNE] += addVal; rho += addVal; \
908         addVal  = addDfFacT * RAC(addfcel , dNW); \
909         ux-=addVal; uy+=addVal;               intDf[dNW] += addVal; rho += addVal; \
910         addVal  = addDfFacT * RAC(addfcel , dSE); \
911         ux+=addVal; uy-=addVal;               intDf[dSE] += addVal; rho += addVal; \
912         addVal  = addDfFacT * RAC(addfcel , dSW); \
913         ux-=addVal; uy-=addVal;               intDf[dSW] += addVal; rho += addVal; \
914         addVal  = addDfFacT * RAC(addfcel , dNT); \
915                      uy+=addVal; uz+=addVal;  intDf[dNT] += addVal; rho += addVal; \
916         addVal  = addDfFacT * RAC(addfcel , dNB); \
917                      uy+=addVal; uz-=addVal;  intDf[dNB] += addVal; rho += addVal; \
918         addVal  = addDfFacT * RAC(addfcel , dST); \
919                      uy-=addVal; uz+=addVal;  intDf[dST] += addVal; rho += addVal; \
920         addVal  = addDfFacT * RAC(addfcel , dSB); \
921                      uy-=addVal; uz-=addVal;  intDf[dSB] += addVal; rho += addVal; \
922         addVal  = addDfFacT * RAC(addfcel , dET); \
923         ux+=addVal;              uz+=addVal;  intDf[dET] += addVal; rho += addVal; \
924         addVal  = addDfFacT * RAC(addfcel , dEB); \
925         ux+=addVal;              uz-=addVal;  intDf[dEB] += addVal; rho += addVal; \
926         addVal  = addDfFacT * RAC(addfcel , dWT); \
927         ux-=addVal;              uz+=addVal;  intDf[dWT] += addVal; rho += addVal; \
928         addVal  = addDfFacT * RAC(addfcel , dWB); \
929         ux-=addVal;              uz-=addVal;  intDf[dWB] += addVal; rho += addVal; 
930
931 #define ADD_INT_DFS(alev, ai,aj,ak, at, afac) \
932         addDfFacT = at*afac; \
933         addfcel = RACPNT((alev), (ai),(aj),(ak),mLevel[(alev)].setOther); \
934         ADDALLVALS\
935         addDfFacT = (1.0-at)*afac; \
936         addfcel = RACPNT((alev), (ai),(aj),(ak),mLevel[(alev)].setCurr); \
937         ADDALLVALS
938
939 // also ugly...
940 #define INTDF_C    intDf[dC ]
941 #define INTDF_N    intDf[dN ]
942 #define INTDF_S    intDf[dS ]
943 #define INTDF_E    intDf[dE ]
944 #define INTDF_W    intDf[dW ]
945 #define INTDF_T    intDf[dT ]
946 #define INTDF_B    intDf[dB ]
947 #define INTDF_NE   intDf[dNE]
948 #define INTDF_NW   intDf[dNW]
949 #define INTDF_SE   intDf[dSE]
950 #define INTDF_SW   intDf[dSW]
951 #define INTDF_NT   intDf[dNT]
952 #define INTDF_NB   intDf[dNB]
953 #define INTDF_ST   intDf[dST]
954 #define INTDF_SB   intDf[dSB]
955 #define INTDF_ET   intDf[dET]
956 #define INTDF_EB   intDf[dEB]
957 #define INTDF_WT   intDf[dWT]
958 #define INTDF_WB   intDf[dWB]
959
960
961 // write interpolated dfs back to cell (correct non-eq. parts)
962 #define IDF_WRITEBACK_LES \
963                 dstcell = RACPNT(lev, i,j,k,dstSet); \
964                 usqr = 1.5 * (ux*ux + uy*uy + uz*uz);  \
965                 \
966                 lcsmeq[dC] = EQC ; \
967                 COLL_CALCULATE_DFEQ(lcsmeq); \
968                 COLL_CALCULATE_NONEQTENSOR(lev, INTDF_ )\
969                 COLL_CALCULATE_CSMOMEGAVAL(lev+0, lcsmDstOmega); \
970                 COLL_CALCULATE_CSMOMEGAVAL(lev-1, lcsmSrcOmega); \
971                 \
972                 lcsmdfscale   = (mLevel[lev+0].timestep/mLevel[lev-1].timestep)* (1.0/lcsmDstOmega-1.0)/ (1.0/lcsmSrcOmega-1.0);  \
973                 RAC(dstcell, dC ) = (lcsmeq[dC ] + (intDf[dC ]-lcsmeq[dC ] )*lcsmdfscale);\
974                 RAC(dstcell, dN ) = (lcsmeq[dN ] + (intDf[dN ]-lcsmeq[dN ] )*lcsmdfscale);\
975                 RAC(dstcell, dS ) = (lcsmeq[dS ] + (intDf[dS ]-lcsmeq[dS ] )*lcsmdfscale);\
976                 RAC(dstcell, dE ) = (lcsmeq[dE ] + (intDf[dE ]-lcsmeq[dE ] )*lcsmdfscale);\
977                 RAC(dstcell, dW ) = (lcsmeq[dW ] + (intDf[dW ]-lcsmeq[dW ] )*lcsmdfscale);\
978                 RAC(dstcell, dT ) = (lcsmeq[dT ] + (intDf[dT ]-lcsmeq[dT ] )*lcsmdfscale);\
979                 RAC(dstcell, dB ) = (lcsmeq[dB ] + (intDf[dB ]-lcsmeq[dB ] )*lcsmdfscale);\
980                 RAC(dstcell, dNE) = (lcsmeq[dNE] + (intDf[dNE]-lcsmeq[dNE] )*lcsmdfscale);\
981                 RAC(dstcell, dNW) = (lcsmeq[dNW] + (intDf[dNW]-lcsmeq[dNW] )*lcsmdfscale);\
982                 RAC(dstcell, dSE) = (lcsmeq[dSE] + (intDf[dSE]-lcsmeq[dSE] )*lcsmdfscale);\
983                 RAC(dstcell, dSW) = (lcsmeq[dSW] + (intDf[dSW]-lcsmeq[dSW] )*lcsmdfscale);\
984                 RAC(dstcell, dNT) = (lcsmeq[dNT] + (intDf[dNT]-lcsmeq[dNT] )*lcsmdfscale);\
985                 RAC(dstcell, dNB) = (lcsmeq[dNB] + (intDf[dNB]-lcsmeq[dNB] )*lcsmdfscale);\
986                 RAC(dstcell, dST) = (lcsmeq[dST] + (intDf[dST]-lcsmeq[dST] )*lcsmdfscale);\
987                 RAC(dstcell, dSB) = (lcsmeq[dSB] + (intDf[dSB]-lcsmeq[dSB] )*lcsmdfscale);\
988                 RAC(dstcell, dET) = (lcsmeq[dET] + (intDf[dET]-lcsmeq[dET] )*lcsmdfscale);\
989                 RAC(dstcell, dEB) = (lcsmeq[dEB] + (intDf[dEB]-lcsmeq[dEB] )*lcsmdfscale);\
990                 RAC(dstcell, dWT) = (lcsmeq[dWT] + (intDf[dWT]-lcsmeq[dWT] )*lcsmdfscale);\
991                 RAC(dstcell, dWB) = (lcsmeq[dWB] + (intDf[dWB]-lcsmeq[dWB] )*lcsmdfscale);\
992                 /* IDF_WRITEBACK optimized */
993
994 #define IDF_WRITEBACK_NOLES \
995                 dstcell = RACPNT(lev, i,j,k,dstSet); \
996                 usqr = 1.5 * (ux*ux + uy*uy + uz*uz);  \
997                 \
998                 RAC(dstcell, dC ) = (EQC  + (intDf[dC ]-EQC  )*mDfScaleDown);\
999                 RAC(dstcell, dN ) = (EQN  + (intDf[dN ]-EQN  )*mDfScaleDown);\
1000                 RAC(dstcell, dS ) = (EQS  + (intDf[dS ]-EQS  )*mDfScaleDown);\
1001                 /*old*/ RAC(dstcell, dE ) = (EQE  + (intDf[dE ]-EQE  )*mDfScaleDown);\
1002                 RAC(dstcell, dW ) = (EQW  + (intDf[dW ]-EQW  )*mDfScaleDown);\
1003                 RAC(dstcell, dT ) = (EQT  + (intDf[dT ]-EQT  )*mDfScaleDown);\
1004                 RAC(dstcell, dB ) = (EQB  + (intDf[dB ]-EQB  )*mDfScaleDown);\
1005                 /*old*/ RAC(dstcell, dNE) = (EQNE + (intDf[dNE]-EQNE )*mDfScaleDown);\
1006                 RAC(dstcell, dNW) = (EQNW + (intDf[dNW]-EQNW )*mDfScaleDown);\
1007                 RAC(dstcell, dSE) = (EQSE + (intDf[dSE]-EQSE )*mDfScaleDown);\
1008                 RAC(dstcell, dSW) = (EQSW + (intDf[dSW]-EQSW )*mDfScaleDown);\
1009                 RAC(dstcell, dNT) = (EQNT + (intDf[dNT]-EQNT )*mDfScaleDown);\
1010                 RAC(dstcell, dNB) = (EQNB + (intDf[dNB]-EQNB )*mDfScaleDown);\
1011                 RAC(dstcell, dST) = (EQST + (intDf[dST]-EQST )*mDfScaleDown);\
1012                 RAC(dstcell, dSB) = (EQSB + (intDf[dSB]-EQSB )*mDfScaleDown);\
1013                 RAC(dstcell, dET) = (EQET + (intDf[dET]-EQET )*mDfScaleDown);\
1014                 /*old*/ RAC(dstcell, dEB) = (EQEB + (intDf[dEB]-EQEB )*mDfScaleDown);\
1015                 RAC(dstcell, dWT) = (EQWT + (intDf[dWT]-EQWT )*mDfScaleDown);\
1016                 RAC(dstcell, dWB) = (EQWB + (intDf[dWB]-EQWB )*mDfScaleDown);\
1017                 /* IDF_WRITEBACK optimized */
1018
1019 #if USE_LES==1
1020 #define IDF_WRITEBACK IDF_WRITEBACK_LES
1021 #else 
1022 #define IDF_WRITEBACK IDF_WRITEBACK_NOLES
1023 #endif
1024
1025 #endif// OPT3D==0 
1026
1027
1028
1029 /******************************************************************************/
1030 /*! relaxation LES functions */
1031 /******************************************************************************/
1032
1033
1034 inline LbmFloat LbmFsgrSolver::getLesNoneqTensorCoeff(
1035                 LbmFloat df[],                          
1036                 LbmFloat feq[] ) {
1037         LbmFloat Qo = 0.0;
1038         for(int m=0; m< ((LBMDIM*LBMDIM)-LBMDIM)/2 ; m++) { 
1039                 LbmFloat qadd = 0.0;
1040                 for(int l=1; l<this->cDfNum; l++) { 
1041                         if(this->lesCoeffOffdiag[m][l]==0.0) continue;
1042                         qadd += this->lesCoeffOffdiag[m][l]*(df[l]-feq[l]);
1043                 }
1044                 Qo += (qadd*qadd);
1045         }
1046         Qo *= 2.0; // off diag twice
1047         for(int m=0; m<LBMDIM; m++) { 
1048                 LbmFloat qadd = 0.0;
1049                 for(int l=1; l<this->cDfNum; l++) { 
1050                         if(this->lesCoeffDiag[m][l]==0.0) continue;
1051                         qadd += this->lesCoeffDiag[m][l]*(df[l]-feq[l]);
1052                 }
1053                 Qo += (qadd*qadd);
1054         }
1055         Qo = sqrt(Qo);
1056         return Qo;
1057 };
1058
1059 inline LbmFloat LbmFsgrSolver::getLesOmega(LbmFloat omega, LbmFloat csmago, LbmFloat Qo) {
1060         const LbmFloat tau = 1.0/omega;
1061         const LbmFloat nu = (2.0*tau-1.0) * (1.0/6.0);
1062         const LbmFloat C = csmago;
1063         const LbmFloat Csqr = C*C;
1064         LbmFloat S = -nu + sqrt( nu*nu + 18.0*Csqr*Qo ) / (6.0*Csqr);
1065         return( 1.0/( 3.0*( nu+Csqr*S ) +0.5 ) );
1066 }
1067
1068 #define DEBUG_CALCPRINTCELL(str,df) {\
1069                 LbmFloat prho=df[0], pux=0., puy=0., puz=0.; \
1070                 for(int dfl=1; dfl<this->cDfNum; dfl++) { \
1071                         prho += df[dfl];  \
1072                         pux  += (this->dfDvecX[dfl]*df[dfl]);  \
1073                         puy  += (this->dfDvecY[dfl]*df[dfl]);  \
1074                         puz  += (this->dfDvecZ[dfl]*df[dfl]);  \
1075                 } \
1076                 errMsg("DEBUG_CALCPRINTCELL",">"<<str<<" rho="<<prho<<" vel="<<ntlVec3Gfx(pux,puy,puz) ); \
1077         } /* END DEBUG_CALCPRINTCELL */ 
1078
1079 // "normal" collision
1080 inline void LbmFsgrSolver::collideArrays(
1081                 int i, int j, int k, // position - more for debugging
1082                 LbmFloat df[],                          
1083                 LbmFloat &outrho, // out only!
1084                 // velocity modifiers (returns actual velocity!)
1085                 LbmFloat &mux, LbmFloat &muy, LbmFloat &muz, 
1086                 LbmFloat omega, 
1087                 LbmVec gravity,
1088                 LbmFloat csmago, 
1089                 LbmFloat *newOmegaRet, LbmFloat *newQoRet
1090         ) {
1091         int l;
1092         LbmFloat rho=df[0]; 
1093         LbmFloat ux = 0; //mux;
1094         LbmFloat uy = 0; //muy;
1095         LbmFloat uz = 0; //muz; 
1096         LbmFloat feq[19];
1097         LbmFloat omegaNew;
1098         LbmFloat Qo = 0.0;
1099
1100         for(l=1; l<this->cDfNum; l++) { 
1101                 rho += df[l]; 
1102                 ux  += (this->dfDvecX[l]*df[l]); 
1103                 uy  += (this->dfDvecY[l]*df[l]);  
1104                 uz  += (this->dfDvecZ[l]*df[l]);  
1105         }  
1106
1107         PRECOLLIDE_MODS(rho,ux,uy,uz, gravity);
1108         for(l=0; l<this->cDfNum; l++) { 
1109                 feq[l] = getCollideEq(l,rho,ux,uy,uz); 
1110         }
1111
1112         if(csmago>0.0) {
1113                 Qo = getLesNoneqTensorCoeff(df,feq);
1114                 omegaNew = getLesOmega(omega,csmago,Qo);
1115         } else {
1116                 omegaNew = omega; // smago off...
1117         }
1118         if(newOmegaRet) *newOmegaRet = omegaNew; // return value for stats
1119         if(newQoRet)    *newQoRet = Qo; // return value of non-eq. stress tensor
1120
1121         for(l=0; l<this->cDfNum; l++) { 
1122                 df[l] = (1.0-omegaNew ) * df[l] + omegaNew * feq[l]; 
1123         }  
1124         //if((i==16)&&(j==10)) DEBUG_CALCPRINTCELL( "2dcoll "<<PRINT_IJK, df);
1125
1126         mux = ux;
1127         muy = uy;
1128         muz = uz;
1129         outrho = rho;
1130 };
1131