This version now includes the fluid control sources, however the Blender
[blender.git] / intern / elbeem / intern / solver_control.cpp
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  *
7  * Copyright 2003-2008 Nils Thuerey 
8  *
9  * control extensions
10  *
11  *****************************************************************************/
12 #include "solver_class.h"
13 #include "solver_relax.h"
14 #include "particletracer.h"
15
16 #include "solver_control.h"
17
18 #include "controlparticles.h"
19
20
21
22 /******************************************************************************
23  * LbmControlData control set
24  *****************************************************************************/
25
26 LbmControlSet::LbmControlSet() :
27         mCparts(NULL), mCpmotion(NULL), mContrPartFile(""), mCpmotionFile(""),
28         mcForceAtt(0.), mcForceVel(0.), mcForceMaxd(0.), 
29         mcRadiusAtt(0.), mcRadiusVel(0.), mcRadiusMind(0.), mcRadiusMaxd(0.), 
30         mcCpScale(1.), mcCpOffset(0.)
31 {
32 }
33 LbmControlSet::~LbmControlSet() {
34         if(mCparts) delete mCparts;
35         if(mCpmotion) delete mCpmotion;
36 }
37 void LbmControlSet::initCparts() {
38         mCparts = new ControlParticles();
39         mCpmotion = new ControlParticles();
40 }
41
42
43
44 /******************************************************************************
45  * LbmControlData control
46  *****************************************************************************/
47
48 LbmControlData::LbmControlData() : 
49         mSetForceStrength(0.),
50         mCons(), mCpUpdateInterval(16), mCpOutfile(""),
51         mCpForces(), mCpKernel(), mMdKernel(),
52         mDiffVelCon(1.),
53         mDebugCpscale(0.), 
54         mDebugVelScale(0.), 
55         mDebugCompavScale(0.), 
56         mDebugAttScale(0.), 
57         mDebugMaxdScale(0.),
58         mDebugAvgVelScale(0.)
59 {
60 }
61
62 LbmControlData::~LbmControlData() 
63 {
64 }
65
66
67 void LbmControlData::parseControldataAttrList(AttributeList *attr) {
68         // controlpart vars
69         mSetForceStrength = attr->readFloat("tforcestrength", mSetForceStrength,"LbmControlData", "mSetForceStrength", false);
70         //errMsg("tforcestrength set to "," "<<mSetForceStrength);
71         mCpUpdateInterval = attr->readInt("controlparticle_updateinterval", mCpUpdateInterval,"LbmControlData","mCpUpdateInterval", false);
72         // tracer output file
73         mCpOutfile = attr->readString("controlparticle_outfile",mCpOutfile,"LbmControlData","mCpOutfile", false);
74         if(getenv("ELBEEM_CPOUTFILE")) {
75                 string outfile(getenv("ELBEEM_CPOUTFILE"));
76                 mCpOutfile = outfile;
77                 debMsgStd("LbmControlData::parseAttrList",DM_NOTIFY,"Using envvar ELBEEM_CPOUTFILE to set mCpOutfile to "<<outfile<<","<<mCpOutfile,7);
78         }
79
80         for(int cpii=0; cpii<10; cpii++) {
81                 string suffix("");
82                 //if(cpii>0)
83                 {  suffix = string("0"); suffix[0]+=cpii; }
84                 LbmControlSet *cset;
85                 cset = new LbmControlSet();
86                 cset->initCparts();
87
88                 cset->mContrPartFile = attr->readString("controlparticle"+suffix+"_file",cset->mContrPartFile,"LbmControlData","cset->mContrPartFile", false);
89                 if((cpii==0) && (getenv("ELBEEM_CPINFILE")) ) {
90                         string infile(getenv("ELBEEM_CPINFILE"));
91                         cset->mContrPartFile = infile;
92                         debMsgStd("LbmControlData::parseAttrList",DM_NOTIFY,"Using envvar ELBEEM_CPINFILE to set mContrPartFile to "<<infile<<","<<cset->mContrPartFile,7);
93                 }
94
95                 LbmFloat cpvort=0.;
96                 cset->mcRadiusAtt =  attr->readChannelSinglePrecFloat("controlparticle"+suffix+"_radiusatt", 0., "LbmControlData","mcRadiusAtt" );
97                 cset->mcRadiusVel =  attr->readChannelSinglePrecFloat("controlparticle"+suffix+"_radiusvel", 0., "LbmControlData","mcRadiusVel" );
98                 cset->mcRadiusVel =  attr->readChannelSinglePrecFloat("controlparticle"+suffix+"_radiusvel", 0., "LbmControlData","mcRadiusVel" );
99                 cset->mCparts->setRadiusAtt(cset->mcRadiusAtt.get(0.));
100                 cset->mCparts->setRadiusVel(cset->mcRadiusVel.get(0.));
101
102                 // WARNING currently only for first set
103                 //if(cpii==0) {
104                 cset->mcForceAtt  =  attr->readChannelSinglePrecFloat("controlparticle"+suffix+"_attraction", 0. , "LbmControlData","cset->mcForceAtt", false);
105                 cset->mcForceVel  =  attr->readChannelSinglePrecFloat("controlparticle"+suffix+"_velocity",   0. , "LbmControlData","mcForceVel", false);
106                 cset->mcForceMaxd =  attr->readChannelSinglePrecFloat("controlparticle"+suffix+"_maxdist",    0. , "LbmControlData","mcForceMaxd", false);
107                 cset->mCparts->setInfluenceAttraction(cset->mcForceAtt.get(0.) );
108                 // warning - stores temprorarily, value converted to dt dep. factor
109                 cset->mCparts->setInfluenceVelocity(cset->mcForceVel.get(0.) , 0.01 ); // dummy dt
110                 cset->mCparts->setInfluenceMaxdist(cset->mcForceMaxd.get(0.) );
111                 cpvort =  attr->readFloat("controlparticle"+suffix+"_vorticity",   cpvort, "LbmControlData","cpvort", false);
112                 cset->mCparts->setInfluenceTangential(cpvort);
113                         
114                 cset->mcRadiusMind =  attr->readChannelSinglePrecFloat("controlparticle"+suffix+"_radiusmin", cset->mcRadiusMind.get(0.), "LbmControlData","mcRadiusMind", false);
115                 cset->mcRadiusMaxd =  attr->readChannelSinglePrecFloat("controlparticle"+suffix+"_radiusmax", cset->mcRadiusMind.get(0.), "LbmControlData","mcRadiusMaxd", false);
116                 cset->mCparts->setRadiusMinMaxd(cset->mcRadiusMind.get(0.));
117                 cset->mCparts->setRadiusMaxd(cset->mcRadiusMaxd.get(0.));
118                 //}
119
120                 // now local...
121                 //LbmVec cpOffset(0.), cpScale(1.);
122                 LbmFloat cpTimescale = 1.;
123                 string cpMirroring("");
124
125                 //cset->mcCpOffset = attr->readChannelVec3f("controlparticle"+suffix+"_offset", ntlVec3f(0.),"LbmControlData","mcCpOffset", false);
126                 //cset->mcCpScale =  attr->readChannelVec3f("controlparticle"+suffix+"_scale",  ntlVec3f(1.), "LbmControlData","mcCpScale", false);
127                 cset->mcCpOffset = attr->readChannelVec3f("controlparticle"+suffix+"_offset", ntlVec3f(0.),"LbmControlData","mcCpOffset", false);
128                 cset->mcCpScale =  attr->readChannelVec3f("controlparticle"+suffix+"_scale",  ntlVec3f(1.), "LbmControlData","mcCpScale", false);
129                 cpTimescale =  attr->readFloat("controlparticle"+suffix+"_timescale",  cpTimescale, "LbmControlData","cpTimescale", false);
130                 cpMirroring =  attr->readString("controlparticle"+suffix+"_mirror",  cpMirroring, "LbmControlData","cpMirroring", false);
131
132                 LbmFloat cpsWidth = cset->mCparts->getCPSWith();
133                 cpsWidth =  attr->readFloat("controlparticle"+suffix+"_cpswidth",  cpsWidth, "LbmControlData","cpsWidth", false);
134                 LbmFloat cpsDt = cset->mCparts->getCPSTimestep();
135                 cpsDt =  attr->readFloat("controlparticle"+suffix+"_cpstimestep",  cpsDt, "LbmControlData","cpsDt", false);
136                 LbmFloat cpsTstart = cset->mCparts->getCPSTimeStart();
137                 cpsTstart =  attr->readFloat("controlparticle"+suffix+"_cpststart",  cpsTstart, "LbmControlData","cpsTstart", false);
138                 LbmFloat cpsTend = cset->mCparts->getCPSTimeEnd();
139                 cpsTend =  attr->readFloat("controlparticle"+suffix+"_cpstend",  cpsTend, "LbmControlData","cpsTend", false);
140                 LbmFloat cpsMvmfac = cset->mCparts->getCPSMvmWeightFac();
141                 cpsMvmfac =  attr->readFloat("controlparticle"+suffix+"_cpsmvmfac",  cpsMvmfac, "LbmControlData","cpsMvmfac", false);
142                 cset->mCparts->setCPSWith(cpsWidth);
143                 cset->mCparts->setCPSTimestep(cpsDt);
144                 cset->mCparts->setCPSTimeStart(cpsTstart);
145                 cset->mCparts->setCPSTimeEnd(cpsTend);
146                 cset->mCparts->setCPSMvmWeightFac(cpsMvmfac);
147
148                 cset->mCparts->setOffset( vec2L(cset->mcCpOffset.get(0.)) );
149                 cset->mCparts->setScale( vec2L(cset->mcCpScale.get(0.)) );
150                 cset->mCparts->setInitTimeScale( cpTimescale );
151                 cset->mCparts->setInitMirror( cpMirroring );
152
153                 int mDebugInit = 0;
154                 mDebugInit = attr->readInt("controlparticle"+suffix+"_debuginit", mDebugInit,"LbmControlData","mDebugInit", false);
155                 cset->mCparts->setDebugInit(mDebugInit);
156
157                 // motion particle settings
158                 LbmVec mcpOffset(0.), mcpScale(1.);
159                 LbmFloat mcpTimescale = 1.;
160                 string mcpMirroring("");
161
162                 cset->mCpmotionFile = attr->readString("cpmotion"+suffix+"_file",cset->mCpmotionFile,"LbmControlData","mCpmotionFile", false);
163                 mcpTimescale =  attr->readFloat("cpmotion"+suffix+"_timescale",  mcpTimescale, "LbmControlData","mcpTimescale", false);
164                 mcpMirroring =  attr->readString("cpmotion"+suffix+"_mirror",  mcpMirroring, "LbmControlData","mcpMirroring", false);
165                 mcpOffset = vec2L( attr->readVec3d("cpmotion"+suffix+"_offset", vec2P(mcpOffset),"LbmControlData","cpOffset", false) );
166                 mcpScale =  vec2L( attr->readVec3d("cpmotion"+suffix+"_scale",  vec2P(mcpScale), "LbmControlData","cpScale", false) );
167
168                 cset->mCpmotion->setOffset( vec2L(mcpOffset) );
169                 cset->mCpmotion->setScale( vec2L(mcpScale) );
170                 cset->mCpmotion->setInitTimeScale( mcpTimescale );
171                 cset->mCpmotion->setInitMirror( mcpMirroring );
172
173                 if(cset->mContrPartFile.length()>1) {
174                         errMsg("LbmControlData","Using control particle set "<<cpii<<" file:"<<cset->mContrPartFile<<" cpmfile:"<<cset->mCpmotionFile<<" mirr:'"<<cset->mCpmotion->getInitMirror()<<"' " );
175                         mCons.push_back( cset );
176                 } else {
177                         delete cset;
178                 }
179         }
180
181         // debug, testing - make sure theres at least an empty set
182         if(mCons.size()<1) {
183                 mCons.push_back( new LbmControlSet() );
184                 mCons[0]->initCparts();
185         }
186
187         // take from first set
188         for(int cpii=1; cpii<(int)mCons.size(); cpii++) {
189                 mCons[cpii]->mCparts->setRadiusMinMaxd( mCons[0]->mCparts->getRadiusMinMaxd() );
190                 mCons[cpii]->mCparts->setRadiusMaxd(    mCons[0]->mCparts->getRadiusMaxd() );
191                 mCons[cpii]->mCparts->setInfluenceAttraction( mCons[0]->mCparts->getInfluenceAttraction() );
192                 mCons[cpii]->mCparts->setInfluenceTangential( mCons[0]->mCparts->getInfluenceTangential() );
193                 mCons[cpii]->mCparts->setInfluenceVelocity( mCons[0]->mCparts->getInfluenceVelocity() , 0.01 ); // dummy dt
194                 mCons[cpii]->mCparts->setInfluenceMaxdist( mCons[0]->mCparts->getInfluenceMaxdist() );
195         }
196         
197         // invert for usage in relax macro
198         mDiffVelCon =  1.-attr->readFloat("cpdiffvelcon",  mDiffVelCon, "LbmControlData","mDiffVelCon", false);
199
200         mDebugCpscale     =  attr->readFloat("cpdebug_cpscale",  mDebugCpscale, "LbmControlData","mDebugCpscale", false);
201         mDebugMaxdScale   =  attr->readFloat("cpdebug_maxdscale",  mDebugMaxdScale, "LbmControlData","mDebugMaxdScale", false);
202         mDebugAttScale    =  attr->readFloat("cpdebug_attscale",  mDebugAttScale, "LbmControlData","mDebugAttScale", false);
203         mDebugVelScale    =  attr->readFloat("cpdebug_velscale",  mDebugVelScale, "LbmControlData","mDebugVelScale", false);
204         mDebugCompavScale =  attr->readFloat("cpdebug_compavscale",  mDebugCompavScale, "LbmControlData","mDebugCompavScale", false);
205         mDebugAvgVelScale =  attr->readFloat("cpdebug_avgvelsc",  mDebugAvgVelScale, "LbmControlData","mDebugAvgVelScale", false);
206 }
207
208
209 void 
210 LbmFsgrSolver::initCpdata()
211 {
212         // enable for cps via env. vars
213         //if( (getenv("ELBEEM_CPINFILE")) || (getenv("ELBEEM_CPOUTFILE")) ){ mUseTestdata=1; }
214
215         // NT blender integration manual test setup
216         if(1) {
217                 // manually switch on! if this is zero, nothing is done...
218                 mpControl->mSetForceStrength = this->mTForceStrength = 1.;
219                 mpControl->mCons.clear();
220
221                 // add new set
222                 LbmControlSet *cset;
223
224                 cset = new LbmControlSet();
225                 cset->initCparts();
226                 // dont load any file
227                 cset->mContrPartFile = string("");
228
229                 // set radii for attraction & velocity forces
230                 // set strength of the forces
231                 // don't set directly! but use channels: 
232                 // mcForceAtt, mcForceVel, mcForceMaxd, mcRadiusAtt, mcRadiusVel, mcRadiusMind, mcRadiusMaxd etc.
233
234                 // wrong: cset->mCparts->setInfluenceAttraction(1.15); cset->mCparts->setRadiusAtt(1.5);
235                 // right, e.g., to init some constant values:
236                 cset->mcForceAtt = AnimChannel<float>(0.2);
237                 cset->mcRadiusAtt = AnimChannel<float>(0.75);
238                 cset->mcForceVel = AnimChannel<float>(0.2);
239                 cset->mcRadiusVel = AnimChannel<float>(0.75);
240
241                 // this value can be left at 0.5:
242                 cset->mCparts->setCPSMvmWeightFac(0.5);
243
244                 mpControl->mCons.push_back( cset );
245
246                 // instead of reading from file (cset->mContrPartFile), manually init some particles
247                 mpControl->mCons[0]->mCparts->initBlenderTest();
248
249                 // other values that might be interesting to change:
250                 //cset->mCparts->setCPSTimestep(0.02);
251                 //cset->mCparts->setCPSTimeStart(0.);
252                 //cset->mCparts->setCPSTimeEnd(1.); 
253
254                 //mpControl->mDiffVelCon = 1.; // more rigid velocity control, 0 (default) allows more turbulence
255         }
256
257         // control particle -------------------------------------------------------------------------------------
258
259         // init cppf stage, use set 0!
260         if(mCppfStage>0) {
261                 if(mpControl->mCpOutfile.length()<1) mpControl->mCpOutfile = string("cpout"); // use getOutFilename !?
262                 char strbuf[100];
263                 const char *cpFormat = "_d%dcppf%d";
264
265                 // initial coarse stage, no input
266                 if(mCppfStage==1) {
267                         mpControl->mCons[0]->mContrPartFile = "";
268                 } else {
269                         // read from prev stage
270                         snprintf(strbuf,100, cpFormat  ,LBMDIM,mCppfStage-1);
271                         mpControl->mCons[0]->mContrPartFile = mpControl->mCpOutfile;
272                         mpControl->mCons[0]->mContrPartFile += strbuf;
273                         mpControl->mCons[0]->mContrPartFile += ".cpart2";
274                 }
275
276                 snprintf(strbuf,100, cpFormat  ,LBMDIM,mCppfStage);
277                 mpControl->mCpOutfile += strbuf;
278         } // */
279         
280         for(int cpssi=0; cpssi<(int)mpControl->mCons.size(); cpssi++) {
281                 ControlParticles *cparts = mpControl->mCons[cpssi]->mCparts;
282                 ControlParticles *cpmotion = mpControl->mCons[cpssi]->mCpmotion;
283
284                 // now set with real dt
285                 cparts->setInfluenceVelocity( mpControl->mCons[cpssi]->mcForceVel.get(0.), mLevel[mMaxRefine].timestep);
286                 cparts->setCharLength( mLevel[mMaxRefine].nodeSize );
287                 cparts->setCharLength( mLevel[mMaxRefine].nodeSize );
288                 errMsg("LbmControlData","CppfStage "<<mCppfStage<<" in:"<<mpControl->mCons[cpssi]->mContrPartFile<<
289                                 " out:"<<mpControl->mCpOutfile<<" cl:"<< cparts->getCharLength() );
290
291                 // control particle test init
292                 if(mpControl->mCons[cpssi]->mCpmotionFile.length()>=1) cpmotion->initFromTextFile(mpControl->mCons[cpssi]->mCpmotionFile);
293                 // not really necessary...
294                 //? cparts->setFluidSpacing( mLevel[mMaxRefine].nodeSize        ); // use grid coords!?
295                 //? cparts->calculateKernelWeight();
296                 //? debMsgStd("LbmFsgrSolver::initCpdata",DM_MSG,"ControlParticles - motion inited: "<<cparts->getSize() ,10);
297
298                 // ensure both are on for env. var settings
299                 // when no particles, but outfile enabled, initialize
300                 const int lev = mMaxRefine;
301                 if((mpParticles) && (mpControl->mCpOutfile.length()>=1) && (cpssi==0)) {
302                         // check if auto num
303                         if( (mpParticles->getNumInitialParticles()<=1) && 
304                                         (mpParticles->getNumParticles()<=1) ) { // initParticles done afterwards anyway
305                                 int tracers = 0;
306                                 const int workSet = mLevel[lev].setCurr;
307                                 FSGR_FORIJK_BOUNDS(lev) { 
308                                         if(RFLAG(lev,i,j,k, workSet)&(CFFluid)) tracers++;
309                                 }
310                                 if(LBMDIM==3) tracers /= 8;
311                                 else          tracers /= 4;
312                                 mpParticles->setNumInitialParticles(tracers);
313                                 mpParticles->setDumpTextFile(mpControl->mCpOutfile);
314                                 debMsgStd("LbmFsgrSolver::initCpdata",DM_MSG,"ControlParticles - set tracers #"<<tracers<<", actual #"<<mpParticles->getNumParticles() ,10);
315                         }
316                         if(mpParticles->getDumpTextInterval()<=0.) {
317                                 mpParticles->setDumpTextInterval(mLevel[lev].timestep * mLevel[lev].lSizex);
318                                 debMsgStd("LbmFsgrSolver::initCpdata",DM_MSG,"ControlParticles - dump delta t not set, using dti="<< mpParticles->getDumpTextInterval()<<", sim dt="<<mLevel[lev].timestep, 5 );
319                         }
320                         mpParticles->setDumpParts(true); // DEBUG? also dump as particle system
321                 }
322
323                 if(mpControl->mCons[cpssi]->mContrPartFile.length()>=1) cparts->initFromTextFile(mpControl->mCons[cpssi]->mContrPartFile);
324                 cparts->setFluidSpacing( mLevel[lev].nodeSize   ); // use grid coords!?
325                 cparts->calculateKernelWeight();
326                 debMsgStd("LbmFsgrSolver::initCpdata",DM_MSG,"ControlParticles mCons"<<cpssi<<" - inited, parts:"<<cparts->getTotalSize()<<","<<cparts->getSize()<<" dt:"<<mpParam->getTimestep()<<" control time:"<<cparts->getControlTimStart()<<" to "<<cparts->getControlTimEnd() ,10);
327         } // cpssi
328
329         if(getenv("ELBEEM_CPINFILE")) {
330                 this->mTForceStrength = 1.0;
331         }
332         this->mTForceStrength = mpControl->mSetForceStrength;
333         if(mpControl->mCpOutfile.length()>=1) mpParticles->setDumpTextFile(mpControl->mCpOutfile);
334
335         // control particle  init end -------------------------------------------------------------------------------------
336
337         // make sure equiv to solver init
338         if(this->mTForceStrength>0.) { \
339                 mpControl->mCpForces.resize( mMaxRefine+1 );
340                 for(int lev = 0; lev<=mMaxRefine; lev++) {
341                         LONGINT rcellSize = (mLevel[lev].lSizex*mLevel[lev].lSizey*mLevel[lev].lSizez);
342                         debMsgStd("LbmFsgrSolver::initControl",DM_MSG,"mCpForces init, lev="<<lev<<" rcs:"<<(int)(rcellSize+4)<<","<<(rcellSize*sizeof(ControlForces)/(1024*1024)), 9 );
343                         mpControl->mCpForces[lev].resize( (int)(rcellSize+4) );
344                         //for(int i=0 ;i<rcellSize; i++) mpControl->mCpForces.push_back( ControlForces() );
345                         for(int i=0 ;i<rcellSize; i++) mpControl->mCpForces[lev][i].resetForces();
346                 }
347         } // on?
348
349         debMsgStd("LbmFsgrSolver::initCpdata",DM_MSG,"ControlParticles #mCons "<<mpControl->mCons.size()<<" done", 6);
350 }
351
352
353 #define CPODEBUG 0
354 //define CPINTER ((int)(mpControl->mCpUpdateInterval))
355
356 #define KERN(x,y,z)    mpControl->mCpKernel[ (((z)*cpkarWidth + (y))*cpkarWidth + (x)) ]
357 #define MDKERN(x,y,z)  mpControl->mMdKernel[ (((z)*mdkarWidth + (y))*mdkarWidth + (x)) ]
358
359 #define BOUNDCHECK(x,low,high)  ( ((x)<low) ? low : (((x)>high) ? high : (x) )  )
360 #define BOUNDSKIP(x,low,high)  ( ((x)<low) || ((x)>high) )
361
362 void 
363 LbmFsgrSolver::handleCpdata()
364 {
365         myTime_t cpstart = getTime();
366         int cpChecks=0;
367         int cpInfs=0;
368         //debMsgStd("ControlData::handleCpdata",DM_MSG,"called... "<<this->mTForceStrength,1);
369
370         // add cp influence
371   if((true) && (this->mTForceStrength>0.)) {
372                 // ok continue...
373         } // on off
374         else {
375                 return;
376         }
377         
378         if((mpControl->mCpUpdateInterval<1) || (this->mStepCnt%mpControl->mCpUpdateInterval==0)) {
379                 // do full reinit later on... 
380         }
381         else if(this->mStepCnt>mpControl->mCpUpdateInterval) {
382                 // only reinit new cells
383                 // TODO !? remove loop dependance!?
384 #define NOFORCEENTRY(lev, i,j,k) (LBMGET_FORCE(lev, i,j,k).maxDistance==CPF_MAXDINIT) 
385                 // interpolate missing
386                 for(int lev=0; lev<=mMaxRefine; lev++) {
387                 FSGR_FORIJK_BOUNDS(lev) { 
388                         if( (RFLAG(lev,i,j,k, mLevel[lev].setCurr)) & (CFFluid|CFInter) )  
389                         //if( (RFLAG(lev,i,j,k, mLevel[lev].setCurr)) & (CFInter) )  
390                         //if(0)
391                         { // only check new inter? RFLAG?check
392                                 if(NOFORCEENTRY(lev, i,j,k)) {
393                                         //errMsg("CP","FE_MISSING at "<<PRINT_IJK<<" f"<<LBMGET_FORCE(lev, i,j,k).weightAtt<<" md"<<LBMGET_FORCE(lev, i,j,k).maxDistance );
394
395                                         LbmFloat nbs=0.;
396                                         ControlForces vals;
397                                         vals.resetForces(); vals.maxDistance = 0.;
398                                         for(int l=1; l<this->cDirNum; l++) { 
399                                                 int ni=i+this->dfVecX[l], nj=j+this->dfVecY[l], nk=k+this->dfVecZ[l];
400                                                 //errMsg("CP","FE_MISSING check "<<PRINT_VEC(ni,nj,nk)<<" f"<<LBMGET_FORCE(lev, ni,nj,nk).weightAtt<<" md"<<LBMGET_FORCE(lev, ni,nj,nk).maxDistance );
401                                                 if(!NOFORCEENTRY(lev, ni,nj,nk)) {
402                                                         //? vals.weightAtt   += LBMGET_FORCE(lev, ni,nj,nk).weightAtt;
403                                                         //? vals.forceAtt    += LBMGET_FORCE(lev, ni,nj,nk).forceAtt;
404                                                         vals.maxDistance += LBMGET_FORCE(lev, ni,nj,nk).maxDistance;
405                                                         vals.forceMaxd   += LBMGET_FORCE(lev, ni,nj,nk).forceMaxd;
406                                                         vals.weightVel   += LBMGET_FORCE(lev, ni,nj,nk).weightVel;
407                                                         vals.forceVel    += LBMGET_FORCE(lev, ni,nj,nk).forceVel; 
408                                                         // ignore att/compAv/avgVel here for now
409                                                         nbs += 1.;
410                                                 }
411                                         }
412                                         if(nbs>0.) {
413                                                 nbs = 1./nbs;
414                                                 //? LBMGET_FORCE(lev, i,j,k).weightAtt   =      vals.weightAtt*nbs;
415                                                 //? LBMGET_FORCE(lev, i,j,k).forceAtt    = vals.forceAtt*nbs;
416                                                 LBMGET_FORCE(lev, i,j,k).maxDistance = vals.maxDistance*nbs;
417                                                 LBMGET_FORCE(lev, i,j,k).forceMaxd   =  vals.forceMaxd*nbs;
418                                                 LBMGET_FORCE(lev, i,j,k).weightVel   =  vals.weightVel*nbs;
419                                                 LBMGET_FORCE(lev, i,j,k).forceVel    = vals.forceVel*nbs;
420                                         }
421                                                 /*ControlForces *ff = &LBMGET_FORCE(lev, i,j,k);  // DEBUG
422                                                 errMsg("CP","FE_MISSING rec at "<<PRINT_IJK // DEBUG
423                                                                 <<" w:"<<ff->weightAtt<<" wa:" <<PRINT_VEC( ff->forceAtt[0],ff->forceAtt[1],ff->forceAtt[2] )  
424                                                                 <<" v:"<<ff->weightVel<<" wv:" <<PRINT_VEC( ff->forceVel[0],ff->forceVel[1],ff->forceVel[2] )  
425                                                                 <<" v:"<<ff->maxDistance<<" wv:" <<PRINT_VEC( ff->forceMaxd[0],ff->forceMaxd[1],ff->forceMaxd[2] )  ); // DEBUG */
426                                         // else errMsg("CP","FE_MISSING rec at "<<PRINT_IJK<<" failed!"); // DEBUG
427                                         
428                                 }
429                         }
430                 }} // ijk, lev
431
432                 // mStepCnt > mpControl->mCpUpdateInterval
433                 return;
434         } else {
435                 // nothing to do ...
436                 return;
437         }
438
439         // reset
440         for(int lev=0; lev<=mMaxRefine; lev++) {
441                 FSGR_FORIJK_BOUNDS(lev) { LBMGET_FORCE(lev,i,j,k).resetForces(); }
442         }
443         // do setup for coarsest level
444         const int coarseLev = 0;
445         const int fineLev = mMaxRefine;
446
447         // init for current time
448         for(int cpssi=0; cpssi<(int)mpControl->mCons.size(); cpssi++) {
449                 ControlParticles *cparts = mpControl->mCons[cpssi]->mCparts;
450
451                 LbmControlSet *cset = mpControl->mCons[cpssi];
452                 cparts->setRadiusAtt(cset->mcRadiusAtt.get(mSimulationTime));
453                 cparts->setRadiusVel(cset->mcRadiusVel.get(mSimulationTime));
454                 cparts->setInfluenceAttraction(cset->mcForceAtt.get(mSimulationTime) );
455                 cparts->setInfluenceMaxdist(cset->mcForceMaxd.get(mSimulationTime) );
456                 cparts->setRadiusMinMaxd(cset->mcRadiusMind.get(mSimulationTime));
457                 cparts->setRadiusMaxd(cset->mcRadiusMaxd.get(mSimulationTime));
458                 cparts->calculateKernelWeight(); // always necessary!?
459                 cparts->setOffset( vec2L(cset->mcCpOffset.get(mSimulationTime)) );
460                 cparts->setScale(  vec2L(cset->mcCpScale.get(mSimulationTime)) );
461
462                 cparts->setInfluenceVelocity( cset->mcForceVel.get(mSimulationTime), mLevel[fineLev].timestep );
463                 cparts->setLastOffset( vec2L(cset->mcCpOffset.get(mSimulationTime-mLevel[fineLev].timestep)) );
464                 cparts->setLastScale(  vec2L(cset->mcCpScale.get(mSimulationTime-mLevel[fineLev].timestep)) );
465         }
466
467         // check actual values
468         LbmFloat iatt  = mpControl->mCons[0]->mCparts->getInfluenceAttraction();
469         LbmFloat ivel  = mpControl->mCons[0]->mCparts->getInfluenceVelocity();
470         LbmFloat imaxd = mpControl->mCons[0]->mCparts->getInfluenceMaxdist();
471         //errMsg("FINCIT","iatt="<<iatt<<" ivel="<<ivel<<" imaxd="<<imaxd);
472         for(int cpssi=1; cpssi<(int)mpControl->mCons.size(); cpssi++) {
473                 LbmFloat iatt2  = mpControl->mCons[cpssi]->mCparts->getInfluenceAttraction();
474                 LbmFloat ivel2  = mpControl->mCons[cpssi]->mCparts->getInfluenceVelocity();
475                 LbmFloat imaxd2 = mpControl->mCons[cpssi]->mCparts->getInfluenceMaxdist();
476                 if(iatt2 >iatt)   iatt = iatt2;
477                 if(ivel2 >ivel)   ivel = ivel2;
478                 if(imaxd2>imaxd)  imaxd= imaxd2;
479                 //errMsg("FINCIT"," "<<cpssi<<" iatt2="<<iatt2<<" ivel2="<<ivel2<<" imaxd2="<<imaxd<<" NEW "<<" iatt="<<iatt<<" ivel="<<ivel<<" imaxd="<<imaxd);
480         }
481
482         if(iatt==0. && ivel==0. && imaxd==0.) {
483                 debMsgStd("ControlData::initControl",DM_MSG,"Skipped, all zero...",4);
484                 return;
485         }
486         //iatt  = mpControl->mCons[1]->mCparts->getInfluenceAttraction(); //ivel  = mpControl->mCons[1]->mCparts->getInfluenceVelocity(); //imaxd = mpControl->mCons[1]->mCparts->getInfluenceMaxdist(); // TTTTTT
487
488         // do control setup
489         for(int cpssi=0; cpssi<(int)mpControl->mCons.size(); cpssi++) {
490                 ControlParticles *cparts = mpControl->mCons[cpssi]->mCparts;
491                 ControlParticles *cpmotion = mpControl->mCons[cpssi]->mCpmotion;
492
493                 // TEST!?
494                 bool radmod = false;
495                 const LbmFloat minRadSize = mLevel[coarseLev].nodeSize * 1.5;
496                 if((cparts->getRadiusAtt()>0.) && (cparts->getRadiusAtt()<minRadSize) && (!radmod) ) {
497                         LbmFloat radfac = minRadSize / cparts->getRadiusAtt(); radmod=true;
498                         debMsgStd("ControlData::initControl",DM_MSG,"Modified radii att, fac="<<radfac, 7);
499                         cparts->setRadiusAtt(cparts->getRadiusAtt()*radfac);
500                         cparts->setRadiusVel(cparts->getRadiusVel()*radfac);
501                         cparts->setRadiusMaxd(cparts->getRadiusMaxd()*radfac);
502                         cparts->setRadiusMinMaxd(cparts->getRadiusMinMaxd()*radfac);
503                 } else if((cparts->getRadiusVel()>0.) && (cparts->getRadiusVel()<minRadSize) && (!radmod) ) {
504                         LbmFloat radfac = minRadSize / cparts->getRadiusVel();
505                         debMsgStd("ControlData::initControl",DM_MSG,"Modified radii vel, fac="<<radfac, 7);
506                         cparts->setRadiusVel(cparts->getRadiusVel()*radfac);
507                         cparts->setRadiusMaxd(cparts->getRadiusMaxd()*radfac);
508                         cparts->setRadiusMinMaxd(cparts->getRadiusMinMaxd()*radfac);
509                 } else if((cparts->getRadiusMaxd()>0.) && (cparts->getRadiusMaxd()<minRadSize) && (!radmod) ) {
510                         LbmFloat radfac = minRadSize / cparts->getRadiusMaxd();
511                         debMsgStd("ControlData::initControl",DM_MSG,"Modified radii maxd, fac="<<radfac, 7);
512                         cparts->setRadiusMaxd(cparts->getRadiusMaxd()*radfac);
513                         cparts->setRadiusMinMaxd(cparts->getRadiusMinMaxd()*radfac);
514                 }
515                 if(radmod) {
516                         debMsgStd("ControlData::initControl",DM_MSG,"Modified radii: att="<< 
517                                 cparts->getRadiusAtt()<<", vel=" << cparts->getRadiusVel()<<", maxd=" <<
518                                 cparts->getRadiusMaxd()<<", mind=" << cparts->getRadiusMinMaxd() ,5);
519                 }
520
521                 cpmotion->prepareControl( mSimulationTime+((LbmFloat)mpControl->mCpUpdateInterval)*(mpParam->getTimestep()), mpParam->getTimestep(), NULL );
522                 cparts->prepareControl( mSimulationTime+((LbmFloat)mpControl->mCpUpdateInterval)*(mpParam->getTimestep()), mpParam->getTimestep(), cpmotion );
523         }
524
525         // do control...
526         for(int lev=0; lev<=mMaxRefine; lev++) {
527                 LbmFloat levVolume = 1.;
528                 LbmFloat levForceScale = 1.;
529                 for(int ll=lev; ll<mMaxRefine; ll++) {
530                         if(LBMDIM==3) levVolume *= 8.;
531                         else levVolume *= 4.;
532                         levForceScale *= 2.;
533                 }
534                 errMsg("LbmFsgrSolver::handleCpdata","levVolume="<<levVolume<<" levForceScale="<<levForceScale );
535         //todo: scale velocity, att by level timestep!?
536                 
537         for(int cpssi=0; cpssi<(int)mpControl->mCons.size(); cpssi++) {
538                 ControlParticles *cparts = mpControl->mCons[cpssi]->mCparts;
539                 // ControlParticles *cpmotion = mpControl->mCons[cpssi]->mCpmotion;
540
541                 const LbmFloat velLatticeScale = mLevel[lev].timestep/mLevel[lev].nodeSize;
542                 LbmFloat gsx = ((mvGeoEnd[0]-mvGeoStart[0])/(LbmFloat)mLevel[lev].lSizex);
543                 LbmFloat gsy = ((mvGeoEnd[1]-mvGeoStart[1])/(LbmFloat)mLevel[lev].lSizey);
544                 LbmFloat gsz = ((mvGeoEnd[2]-mvGeoStart[2])/(LbmFloat)mLevel[lev].lSizez);
545 #if LBMDIM==2
546                 gsz = gsx;
547 #endif
548                 LbmFloat goffx = mvGeoStart[0];
549                 LbmFloat goffy = mvGeoStart[1];
550                 LbmFloat goffz = mvGeoStart[2];
551
552                 //const LbmFloat cpwIncFac = 2.0;
553                 // max to two thirds of domain size
554                 const int cpw = MIN( mLevel[lev].lSizex/3, MAX( (int)( cparts->getRadiusAtt()  /gsx) +1  , 2) ); // normal kernel, att,vel
555                 const int cpkarWidth = 2*cpw+1;
556                 mpControl->mCpKernel.resize(cpkarWidth* cpkarWidth* cpkarWidth);
557                 ControlParticle cpt; cpt.reset();
558                 cpt.pos = LbmVec( (gsx*(LbmFloat)cpw)+goffx, (gsy*(LbmFloat)cpw)+goffy, (gsz*(LbmFloat)cpw)+goffz );  // optimize?
559                 cpt.density = 0.5; cpt.densityWeight = 0.5;
560 #if LBMDIM==3
561                 for(int k= 0; k<cpkarWidth; ++k) {
562 #else // LBMDIM==3
563                 { int k = cpw;
564 #endif 
565                         for(int j= 0; j<cpkarWidth; ++j) 
566                                 for(int i= 0; i<cpkarWidth; ++i) {
567                                         KERN(i,j,k).resetForces();
568                                         //LbmFloat dx = i-cpw; LbmFloat dy = j-cpw; LbmFloat dz = k-cpw;
569                                         //LbmVec dv = ( LbmVec(dx,dy,dz) );
570                                         //LbmFloat dl = norm( dv ); //LbmVec dir = dv / dl;
571                                         LbmVec pos = LbmVec( (gsx*(LbmFloat)i)+goffx, (gsy*(LbmFloat)j)+goffy, (gsz*(LbmFloat)k)+goffz );  // optimize?
572                                         cparts->calculateCpInfluenceOpt( &cpt, pos, LbmVec(0,0,0), &KERN(i,j,k)  ,1. );
573                                         /*if((CPODEBUG)&&(k==cpw)) errMsg("kern"," at "<<PRINT_IJK<<" pos"<<pos<<" cpp"<<cpt.pos 
574                                                 <<" wf:"<<KERN(i,j,k).weightAtt<<" wa:"<< PRINT_VEC( KERN(i,j,k).forceAtt[0],KERN(i,j,k).forceAtt[1],KERN(i,j,k).forceAtt[2] )  
575                                                 <<" wf:"<<KERN(i,j,k).weightVel<<" wa:"<< PRINT_VEC( KERN(i,j,k).forceVel[0],KERN(i,j,k).forceVel[1],KERN(i,j,k).forceVel[2] )  
576                                                 <<" wf:"<<KERN(i,j,k).maxDistance<<" wa:"<< PRINT_VEC( KERN(i,j,k).forceMaxd[0],KERN(i,j,k).forceMaxd[1],KERN(i,j,k).forceMaxd[2] )  ); // */
577                                         KERN(i,j,k).weightAtt *= 2.;
578                                         KERN(i,j,k).forceAtt *= 2.;
579                                         //KERN(i,j,k).forceAtt[1] *= 2.; KERN(i,j,k).forceAtt[2] *= 2.;
580                                         KERN(i,j,k).weightVel *= 2.;
581                                         KERN(i,j,k).forceVel *= 2.;
582                                         //KERN(i,j,k).forceVel[1] *= 2.; KERN(i,j,k).forceVel[2] *= 2.;
583                                 }
584                 }
585
586                 if(CPODEBUG) errMsg("cpw"," = "<<cpw<<" f"<< cparts->getRadiusAtt()<<" gsx"<<gsx<<" kpw"<<cpkarWidth); // DEBUG
587                 // first cp loop - add att and vel forces
588                 for(int cppi=0; cppi<cparts->getSize(); cppi++) {
589                         ControlParticle *cp = cparts->getParticle(cppi);
590                         if(cp->influence<=0.) continue;
591                         const int cpi = (int)( (cp->pos[0]-goffx)/gsx );
592                         const int cpj = (int)( (cp->pos[1]-goffy)/gsy );
593                         int       cpk = (int)( (cp->pos[2]-goffz)/gsz );
594                         /*if( ((LBMDIM==3)&&(BOUNDSKIP(cpk - cpwsm, getForZMinBnd(), getForZMaxBnd(lev) ))) ||
595                                 ((LBMDIM==3)&&(BOUNDSKIP(cpk + cpwsm, getForZMinBnd(), getForZMaxBnd(lev) ))) ||
596                                 BOUNDSKIP(cpj - cpwsm, 0, mLevel[lev].lSizey ) ||
597                                 BOUNDSKIP(cpj + cpwsm, 0, mLevel[lev].lSizey ) ||
598                                 BOUNDSKIP(cpi - cpwsm, 0, mLevel[lev].lSizex ) ||
599                                 BOUNDSKIP(cpi + cpwsm, 0, mLevel[lev].lSizex ) ) {
600                                 continue;
601                                 } // */
602                         int is,ie,js,je,ks,ke;
603                         ks = BOUNDCHECK(cpk - cpw, getForZMinBnd(), getForZMaxBnd(lev) );
604                         ke = BOUNDCHECK(cpk + cpw, getForZMinBnd(), getForZMaxBnd(lev) );
605                         js = BOUNDCHECK(cpj - cpw, 0, mLevel[lev].lSizey );
606                         je = BOUNDCHECK(cpj + cpw, 0, mLevel[lev].lSizey );
607                         is = BOUNDCHECK(cpi - cpw, 0, mLevel[lev].lSizex );
608                         ie = BOUNDCHECK(cpi + cpw, 0, mLevel[lev].lSizex );
609                         if(LBMDIM==2) { cpk = 0; ks = 0; ke = 1; }
610                         if(CPODEBUG) errMsg("cppft","i"<<cppi<<" cpw"<<cpw<<" gpos"<<PRINT_VEC(cpi,cpj,cpk)<<"   i:"<<is<<","<<ie<<"   j:"<<js<<","<<je<<"   k:"<<ks<<","<<ke<<" "); // DEBUG
611                         cpInfs++;
612
613                         for(int k= ks; k<ke; ++k) {
614                                 for(int j= js; j<je; ++j) {
615
616                                         CellFlagType *pflag = &RFLAG(lev,is,j,k, mLevel[lev].setCurr);
617                                         ControlForces *kk = &KERN( is-cpi+cpw, j-cpj+cpw, k-cpk+cpw);
618                                         ControlForces *ff = &LBMGET_FORCE(lev,is,j,k); 
619                                         pflag--; kk--; ff--;
620
621                                         for(int i= is; i<ie; ++i) {
622                                                 // first cp loop (att,vel)
623                                                 pflag++; kk++; ff++;
624
625                                                 //add weight for bnd cells
626                                                 const LbmFloat pwforce = kk->weightAtt;
627                                                 // control particle mod,
628                                                 // dont add multiple CFFluid fsgr boundaries
629                                                 if(lev==mMaxRefine) {
630                                                         //if( ( ((*pflag)&(CFFluid )) && (lev==mMaxRefine) ) ||
631                                                                         //( ((*pflag)&(CFGrNorm)) && (lev <mMaxRefine) ) ) {
632                                                         if((*pflag)&(CFFluid|CFUnused)) {
633                                                                 // check not fromcoarse?
634                                                                 cp->density += levVolume* kk->weightAtt; // old CFFluid
635                                                         } else if( (*pflag) & (CFEmpty) ) {  
636                                                                 cp->density -= levVolume* 0.5; 
637                                                         } else { //if( ((*pflag) & (CFBnd)) ) {  
638                                                                 cp->density -= levVolume* 0.2;  // penalty
639                                                         }
640                                                 } else {
641                                                         //if((*pflag)&(CFGrNorm)) {
642                                                                 //cp->density += levVolume* kk->weightAtt; // old CFFluid
643                                                         //} 
644                                                 }
645                                                 //else if(!((*pflag) & (CFUnused)) ) {  cp->density -= levVolume* 0.2; } // penalty
646
647                                                 if( (*pflag) & (CFFluid|CFInter) )  // RFLAG_check
648                                                 {
649
650                                                         cpChecks++;
651                                                         //const LbmFloat pwforce = kk->weightAtt;
652                                                         LbmFloat pwvel = kk->weightVel;
653                                                         if((pwforce==0.)&&(pwvel==0.)) { continue; }
654                                                         ff->weightAtt += 1e-6; // for distance
655
656                                                         if(pwforce>0.) {
657                                                                 ff->weightAtt += pwforce *cp->densityWeight *cp->influence;
658                                                                 ff->forceAtt += kk->forceAtt *levForceScale *cp->densityWeight *cp->influence;
659
660                                                                 // old fill handling here
661                                                                 const int workSet =mLevel[lev].setCurr;
662                                                                 LbmFloat ux=0., uy=0., uz=0.;
663                                                                 FORDF1{  
664                                                                         const LbmFloat dfn = QCELL(lev, i,j,k, workSet, l);
665                                                                         ux  += (this->dfDvecX[l]*dfn);
666                                                                         uy  += (this->dfDvecY[l]*dfn); 
667                                                                         uz  += (this->dfDvecZ[l]*dfn); 
668                                                                 }
669                                                                 // control particle mod
670                                                                 cp->avgVelWeight += levVolume*pwforce;
671                                                                 cp->avgVelAcc += LbmVec(ux,uy,uz) * levVolume*pwforce;
672                                                         }
673
674                                                         if(pwvel>0.) {
675                                                                 // TODO make switch? vel.influence depends on density weight... 
676                                                                 // (reduced lowering with 0.75 factor)
677                                                                 pwvel *=  cp->influence *(1.-0.75*cp->densityWeight);
678                                                                 // control particle mod
679                                                                 // todo use Omega instead!?
680                                                                 ff->forceVel += cp->vel*levVolume*pwvel * velLatticeScale; // levVolume?
681                                                                 ff->weightVel += levVolume*pwvel; // levVolume?
682                                                                 ff->compAv += cp->avgVel*levVolume*pwvel; // levVolume?
683                                                                 ff->compAvWeight += levVolume*pwvel; // levVolume?
684                                                         }
685
686                                                         if(CPODEBUG) errMsg("cppft","i"<<cppi<<" at "<<PRINT_IJK<<" kern:"<<
687                                                                         PRINT_VEC(i-cpi+cpw, j-cpj+cpw, k-cpk+cpw )
688                                                                         //<<" w:"<<ff->weightAtt<<" wa:"
689                                                                         //<<PRINT_VEC( ff->forceAtt[0],ff->forceAtt[1],ff->forceAtt[2] )  
690                                                                         //<<" v:"<<ff->weightVel<<" wv:"
691                                                                         //<<PRINT_VEC( ff->forceVel[0],ff->forceVel[1],ff->forceVel[2] )  
692                                                                         //<<" v:"<<ff->maxDistance<<" wv:"
693                                                                         //<<PRINT_VEC( ff->forceMaxd[0],ff->forceMaxd[1],ff->forceMaxd[2] )  
694                                                                         );
695                                                 } // celltype
696
697                                         } // ijk
698                                 } // ijk
699                         } // ijk
700                 } // cpi, end first cp loop (att,vel)
701                 debMsgStd("LbmFsgrSolver::handleCpdata",DM_MSG,"Force cpgrid "<<cpssi<<" generated checks:"<<cpChecks<<" infs:"<<cpInfs ,9);
702         } //cpssi
703         } // lev
704
705         // second loop
706         for(int lev=0; lev<=mMaxRefine; lev++) {
707                 LbmFloat levVolume = 1.;
708                 LbmFloat levForceScale = 1.;
709                 for(int ll=lev; ll<mMaxRefine; ll++) {
710                         if(LBMDIM==3) levVolume *= 8.;
711                         else levVolume *= 4.;
712                         levForceScale *= 2.;
713                 }
714         // prepare maxd forces
715         for(int cpssi=0; cpssi<(int)mpControl->mCons.size(); cpssi++) {
716                 ControlParticles *cparts = mpControl->mCons[cpssi]->mCparts;
717
718                 // WARNING copied from above!
719                 const LbmFloat velLatticeScale = mLevel[lev].timestep/mLevel[lev].nodeSize;
720                 LbmFloat gsx = ((mvGeoEnd[0]-mvGeoStart[0])/(LbmFloat)mLevel[lev].lSizex);
721                 LbmFloat gsy = ((mvGeoEnd[1]-mvGeoStart[1])/(LbmFloat)mLevel[lev].lSizey);
722                 LbmFloat gsz = ((mvGeoEnd[2]-mvGeoStart[2])/(LbmFloat)mLevel[lev].lSizez);
723 #if LBMDIM==2
724                 gsz = gsx;
725 #endif
726                 LbmFloat goffx = mvGeoStart[0];
727                 LbmFloat goffy = mvGeoStart[1];
728                 LbmFloat goffz = mvGeoStart[2];
729
730                 //const LbmFloat cpwIncFac = 2.0;
731                 const int mdw = MIN( mLevel[lev].lSizex/2, MAX( (int)( cparts->getRadiusMaxd() /gsx) +1  , 2) ); // wide kernel, md
732                 const int mdkarWidth = 2*mdw+1;
733                 mpControl->mMdKernel.resize(mdkarWidth* mdkarWidth* mdkarWidth);
734                 ControlParticle cpt; cpt.reset();
735                 cpt.density = 0.5; cpt.densityWeight = 0.5;
736                 cpt.pos = LbmVec( (gsx*(LbmFloat)mdw)+goffx, (gsy*(LbmFloat)mdw)+goffy, (gsz*(LbmFloat)mdw)+goffz );  // optimize?
737 #if LBMDIM==3
738                 for(int k= 0; k<mdkarWidth; ++k) {
739 #else // LBMDIM==3
740                 { int k = mdw;
741 #endif 
742                         for(int j= 0; j<mdkarWidth; ++j) 
743                                 for(int i= 0; i<mdkarWidth; ++i) {
744                                         MDKERN(i,j,k).resetForces();
745                                         LbmVec pos = LbmVec( (gsx*(LbmFloat)i)+goffx, (gsy*(LbmFloat)j)+goffy, (gsz*(LbmFloat)k)+goffz );  // optimize?
746                                         cparts->calculateMaxdForce( &cpt, pos, &MDKERN(i,j,k)  );
747                                 }
748                 }
749
750                 // second cpi loop, maxd forces
751                 if(cparts->getInfluenceMaxdist()>0.) {
752                         for(int cppi=0; cppi<cparts->getSize(); cppi++) {
753                                 ControlParticle *cp = cparts->getParticle(cppi);
754                                 if(cp->influence<=0.) continue;
755                                 const int cpi = (int)( (cp->pos[0]-goffx)/gsx );
756                                 const int cpj = (int)( (cp->pos[1]-goffy)/gsy );
757                                 int       cpk = (int)( (cp->pos[2]-goffz)/gsz );
758
759                                 int is,ie,js,je,ks,ke;
760                                 ks = BOUNDCHECK(cpk - mdw, getForZMinBnd(), getForZMaxBnd(lev) );
761                                 ke = BOUNDCHECK(cpk + mdw, getForZMinBnd(), getForZMaxBnd(lev) );
762                                 js = BOUNDCHECK(cpj - mdw, 0, mLevel[lev].lSizey );
763                                 je = BOUNDCHECK(cpj + mdw, 0, mLevel[lev].lSizey );
764                                 is = BOUNDCHECK(cpi - mdw, 0, mLevel[lev].lSizex );
765                                 ie = BOUNDCHECK(cpi + mdw, 0, mLevel[lev].lSizex );
766                                 if(LBMDIM==2) { cpk = 0; ks = 0; ke = 1; }
767                                 if(CPODEBUG) errMsg("cppft","i"<<cppi<<" mdw"<<mdw<<" gpos"<<PRINT_VEC(cpi,cpj,cpk)<<"   i:"<<is<<","<<ie<<"   j:"<<js<<","<<je<<"   k:"<<ks<<","<<ke<<" "); // DEBUG
768                                 cpInfs++;
769
770                                 for(int k= ks; k<ke; ++k)
771                                  for(int j= js; j<je; ++j) {
772                                         CellFlagType *pflag = &RFLAG(lev,is-1,j,k, mLevel[lev].setCurr);
773                                         for(int i= is; i<ie; ++i) {
774                                                 // second cpi loop, maxd forces
775                                                 pflag++;
776                                                 if( (*pflag) & (CFFluid|CFInter) )  // RFLAG_check
777                                                 {
778                                                         cpChecks++;
779                                                         ControlForces *ff = &LBMGET_FORCE(lev,i,j,k); 
780                                                         if(ff->weightAtt == 0.) {
781                                                                 ControlForces *kk = &MDKERN( i-cpi+mdw, j-cpj+mdw, k-cpk+mdw);
782                                                                 const LbmFloat pmdf = kk->maxDistance;
783                                                                 if((ff->maxDistance > pmdf) || (ff->maxDistance<0.))
784                                                                         ff->maxDistance = pmdf;
785                                                                 ff->forceMaxd = kk->forceMaxd;
786                                                                 // todo use Omega instead!?
787                                                                 ff->forceVel = cp->vel* velLatticeScale;
788                                                         }
789                                                 } // celltype
790                                 } } // ijk
791                         } // cpi, md loop 
792                 } // maxd inf>0 */
793
794
795                 debMsgStd("ControlData::initControl",DM_MSG,"Maxd cpgrid "<<cpssi<<" generated checks:"<<cpChecks<<" infs:"<<cpInfs ,9);
796         } //cpssi
797
798         // normalize, only done once for the whole array
799         mpControl->mCons[0]->mCparts->finishControl( mpControl->mCpForces[lev], iatt,ivel,imaxd );
800
801         } // lev loop
802
803         myTime_t cpend = getTime(); 
804         debMsgStd("ControlData::handleCpdata",DM_MSG,"Time for cpgrid generation:"<< getTimeString(cpend-cpstart)<<", checks:"<<cpChecks<<" infs:"<<cpInfs<<" " ,8);
805
806         // warning, may return  before
807 }
808
809 #if LBM_USE_GUI==1
810
811 #define USE_GLUTILITIES
812 #include "../gui/gui_utilities.h"
813
814 void LbmFsgrSolver::cpDebugDisplay(int dispset)
815 {
816         for(int cpssi=0; cpssi<(int)mpControl->mCons.size(); cpssi++) {
817                 ControlParticles *cparts = mpControl->mCons[cpssi]->mCparts;
818                 //ControlParticles *cpmotion = mpControl->mCons[cpssi]->mCpmotion;
819                 // display cp parts
820                 const bool cpCubes = false;
821                 const bool cpDots = true;
822                 const bool cpCpdist = true;
823                 const bool cpHideIna = true;
824                 glShadeModel(GL_FLAT);
825                 glDisable( GL_LIGHTING ); // dont light lines
826
827                 // dot influence
828                 if((mpControl->mDebugCpscale>0.) && cpDots) {
829                         glPointSize(mpControl->mDebugCpscale * 8.);
830                         glBegin(GL_POINTS);
831                         for(int i=0; i<cparts->getSize(); i++) {
832                                 if((cpHideIna)&&( (cparts->getParticle(i)->influence<=0.) || (cparts->getParticle(i)->size<=0.) )) continue;
833                                 ntlVec3Gfx org( vec2G(cparts->getParticle(i)->pos ) );
834                                 //LbmFloat halfsize = 0.5; 
835                                 LbmFloat scale = cparts->getParticle(i)->densityWeight;
836                                 //glColor4f( scale,scale,scale,scale );
837                                 glColor4f( 0.,scale,0.,scale );
838                                 glVertex3f( org[0],org[1],org[2] );
839                                 //errMsg("lbmDebugDisplay","CP "<<i<<" at "<<org); // DEBUG
840                         }
841                         glEnd();
842                 }
843
844                 // cp positions
845                 if((mpControl->mDebugCpscale>0.) && cpDots) {
846                         glPointSize(mpControl->mDebugCpscale * 3.);
847                         glBegin(GL_POINTS); 
848                         glColor3f( 0,1,0 );
849                 }
850                 for(int i=0; i<cparts->getSize(); i++) {
851                         if((cpHideIna)&&( (cparts->getParticle(i)->influence<=0.) || (cparts->getParticle(i)->size<=0.) )) continue;
852                         ntlVec3Gfx  org( vec2G(cparts->getParticle(i)->pos ) );
853                         LbmFloat halfsize = 0.5; 
854                         LbmFloat scale = cparts->getRadiusAtt() * cparts->getParticle(i)->densityWeight;
855                         if(cpCubes){    glLineWidth( 1 ); 
856                                 glColor3f( 1,1,1 );
857                                 ntlVec3Gfx s = org-(halfsize * (scale)); 
858                                 ntlVec3Gfx e = org+(halfsize * (scale)); 
859                                 drawCubeWire( s,e ); }
860                         if((mpControl->mDebugCpscale>0.) && cpDots) {
861                                 glVertex3f( org[0],org[1],org[2] );
862                         }
863                 }
864                 if(cpDots) glEnd();
865
866                 if(mpControl->mDebugAvgVelScale>0.) {
867                         const float scale = mpControl->mDebugAvgVelScale;
868
869                         glColor3f( 1.0,1.0,1 );
870                         glBegin(GL_LINES); 
871                         for(int i=0; i<cparts->getSize(); i++) {
872                                 if((cpHideIna)&&( (cparts->getParticle(i)->influence<=0.) || (cparts->getParticle(i)->size<=0.) )) continue;
873                                 ntlVec3Gfx  org( vec2G(cparts->getParticle(i)->pos ) );
874
875                                 //errMsg("CPAVGVEL","i"<<i<<" pos"<<org<<" av"<<cparts->getParticle(i)->avgVel);// DEBUG
876                                 float dx = cparts->getParticle(i)->avgVel[0];
877                                 float dy = cparts->getParticle(i)->avgVel[1];
878                                 float dz = cparts->getParticle(i)->avgVel[2];
879                                 dx *= scale; dy *= scale; dz *= scale;
880                                 glVertex3f( org[0],org[1],org[2] );
881                                 glVertex3f( org[0]+dx,org[1]+dy,org[2]+dz );
882                         }
883                         glEnd();
884                 } // */
885
886                 if( (LBMDIM==2) && (cpCpdist) ) {
887                         
888                         // debug, for use of e.g. LBMGET_FORCE LbmControlData *mpControl = this;
889 #                       define TESTGET_FORCE(lev,i,j,k)   mpControl->mCpForces[lev][ ((k*mLevel[lev].lSizey)+j)*mLevel[lev].lSizex+i ]
890         
891                         glBegin(GL_LINES);
892                         //const int lev=0; 
893                         for(int lev=0; lev<=mMaxRefine; lev++) {
894                         FSGR_FORIJK_BOUNDS(lev) {
895                                         LbmVec pos = LbmVec( 
896                                                 ((mvGeoEnd[0]-mvGeoStart[0])/(LbmFloat)mLevel[lev].lSizex) * ((LbmFloat)i+0.5) + mvGeoStart[0], 
897                                                 ((mvGeoEnd[1]-mvGeoStart[1])/(LbmFloat)mLevel[lev].lSizey) * ((LbmFloat)j+0.5) + mvGeoStart[1], 
898                                                 ((mvGeoEnd[2]-mvGeoStart[2])/(LbmFloat)mLevel[lev].lSizez) * ((LbmFloat)k+0.5) + mvGeoStart[2]  );
899                                         if(LBMDIM==2) pos[2] = ((mvGeoEnd[2]-mvGeoStart[2])*0.5 + mvGeoStart[2]);
900
901                                         if((mpControl->mDebugMaxdScale>0.) && (TESTGET_FORCE(lev,i,j,k).weightAtt<=0.) )
902                                         if(TESTGET_FORCE(lev,i,j,k).maxDistance>=0.) 
903                                         if(TESTGET_FORCE(lev,i,j,k).maxDistance<CPF_MAXDINIT ) {
904                                                 const float scale = mpControl->mDebugMaxdScale*10001.;
905                                                 float dx = TESTGET_FORCE(lev,i,j,k).forceMaxd[0];
906                                                 float dy = TESTGET_FORCE(lev,i,j,k).forceMaxd[1];
907                                                 float dz = TESTGET_FORCE(lev,i,j,k).forceMaxd[2];
908                                                 dx *= scale; dy *= scale; dz *= scale;
909                                                 glColor3f( 0,1,0 );
910                                                 glVertex3f( pos[0],pos[1],pos[2] );
911                                                 glVertex3f( pos[0]+dx,pos[1]+dy,pos[2]+dz );
912                                         } // */
913                                         if((mpControl->mDebugAttScale>0.) && (TESTGET_FORCE(lev,i,j,k).weightAtt>0.)) {
914                                                 const float scale = mpControl->mDebugAttScale*100011.;
915                                                 float dx = TESTGET_FORCE(lev,i,j,k).forceAtt[0];
916                                                 float dy = TESTGET_FORCE(lev,i,j,k).forceAtt[1];
917                                                 float dz = TESTGET_FORCE(lev,i,j,k).forceAtt[2];
918                                                 dx *= scale; dy *= scale; dz *= scale;
919                                                 glColor3f( 1,0,0 );
920                                                 glVertex3f( pos[0],pos[1],pos[2] );
921                                                 glVertex3f( pos[0]+dx,pos[1]+dy,pos[2]+dz );
922                                         } // */
923                                         // why check maxDistance?
924                                         if((mpControl->mDebugVelScale>0.) && (TESTGET_FORCE(lev,i,j,k).maxDistance+TESTGET_FORCE(lev,i,j,k).weightVel>0.)) {
925                                                 float scale = mpControl->mDebugVelScale*1.;
926                                                 float wvscale = TESTGET_FORCE(lev,i,j,k).weightVel;
927                                                 float dx = TESTGET_FORCE(lev,i,j,k).forceVel[0];
928                                                 float dy = TESTGET_FORCE(lev,i,j,k).forceVel[1];
929                                                 float dz = TESTGET_FORCE(lev,i,j,k).forceVel[2];
930                                                 scale *= wvscale;
931                                                 dx *= scale; dy *= scale; dz *= scale;
932                                                 glColor3f( 0.2,0.2,1 );
933                                                 glVertex3f( pos[0],pos[1],pos[2] );
934                                                 glVertex3f( pos[0]+dx,pos[1]+dy,pos[2]+dz );
935                                         } // */
936                                         if((mpControl->mDebugCompavScale>0.) && (TESTGET_FORCE(lev,i,j,k).compAvWeight>0.)) {
937                                                 const float scale = mpControl->mDebugCompavScale*1.;
938                                                 float dx = TESTGET_FORCE(lev,i,j,k).compAv[0];
939                                                 float dy = TESTGET_FORCE(lev,i,j,k).compAv[1];
940                                                 float dz = TESTGET_FORCE(lev,i,j,k).compAv[2];
941                                                 dx *= scale; dy *= scale; dz *= scale;
942                                                 glColor3f( 0.2,0.2,1 );
943                                                 glVertex3f( pos[0],pos[1],pos[2] );
944                                                 glVertex3f( pos[0]+dx,pos[1]+dy,pos[2]+dz );
945                                         } // */
946                         } // att,maxd
947                         }
948                         glEnd();
949                 }
950         } // cpssi
951
952         //fprintf(stderr,"BLA\n");
953         glEnable( GL_LIGHTING ); // dont light lines
954         glShadeModel(GL_SMOOTH);
955 }
956
957 #else // LBM_USE_GUI==1
958 void LbmFsgrSolver::cpDebugDisplay(int dispset) { }
959 #endif // LBM_USE_GUI==1
960
961