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