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