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