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