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