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