Welcome to mirror list, hosted at ThFree Co, Russian Federation.

simulation_object.cpp « intern « elbeem « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: eabbeac5662dea6944f25069861f8b3b7339e90e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/******************************************************************************
 *
 * El'Beem - Free Surface Fluid Simulation with the Lattice Boltzmann Method
 * Copyright 2003,2004 Nils Thuerey
 *
 * Basic interface for all simulation modules
 *
 *****************************************************************************/

#include "simulation_object.h"
#include "ntl_bsptree.h"
#include "ntl_scene.h"
#include "factory_lbm.h"
#include "lbmfunctions.h"

#ifdef _WIN32
#else
#include <sys/time.h>
#endif




/******************************************************************************
 * Constructor
 *****************************************************************************/
SimulationObject::SimulationObject() :
	ntlGeometryShader(),
	mGeoStart(-100.0), mGeoEnd(100.0),
	mGeoInitId(-1), mpGiTree(NULL), mpGiObjects(NULL),
	mpGlob(NULL),
	mPanic( false ),
	mDebugType( 1 /* =FLUIDDISPNothing*/ ),
	mSolverType("-"), mStepsPerFrame( 10 ),
	mpLbm(NULL),
	mpParam( NULL ),
	mShowSurface(true), mShowParticles(false),
	mSelectedCid( NULL ),

	stnOld("opt"),
	stnFsgr("fsgr")
{
	mpParam = new Parametrizer();

	for(int i=0; i<MAX_DEBDISPSET; i++) {
		mDebDispSet[i].type  = (i);
		mDebDispSet[i].on    = false;
		mDebDispSet[i].scale = 1.0;
	}

	// reset time
	mTime 						= 0.0;
	mDisplayTime 			= 0.0;
}


/******************************************************************************
 * Destructor
 *****************************************************************************/
SimulationObject::~SimulationObject()
{
	if(mpGiTree != NULL) delete mpGiTree;
	delete mpLbm;
  delete mpParam;
	debMsgStd("SimulationObject",DM_MSG,"El'Beem Done!\n",10);
}



/*****************************************************************************/
/*! init tree for certain geometry init */
/*****************************************************************************/
void SimulationObject::initGeoTree(int id) {
	if(mpGlob == NULL) { errorOut("SimulationObject::initGeoTree error: Requires globals!"); exit(1); }
	mGeoInitId = id;
	ntlScene *scene = mpGlob->getScene();
	mpGiObjects = scene->getObjects();

	if(mpGiTree != NULL) delete mpGiTree;
	char treeFlag = (1<<(mGeoInitId+4));
	mpGiTree = new ntlTree( 20, 4, // warning - fixed values for depth & maxtriangles here...
												scene, treeFlag );
}

/*****************************************************************************/
/*! destroy tree etc. when geometry init done */
/*****************************************************************************/
void SimulationObject::freeGeoTree() {
	if(mpGiTree != NULL) delete mpGiTree;
}




/******************************************************************************
 * simluation interface: initialize simulation using the given configuration file 
 *****************************************************************************/
int SimulationObject::initializeLbmSimulation(ntlRenderGlobals *glob)
{
	mpGlob = glob;
	if(!getVisible()) {
		mpAttrs->setAllUsed();
		return 0;
	}

	//mDimension is deprecated
	mSolverType = mpAttrs->readString("solver", mSolverType, "SimulationObject","mSolverType", false ); 
	if(mSolverType == stnFsgr) {
		mpLbm = createSolverLbmFsgr(); 
	} else if(mSolverType == stnOld) {
#ifdef LBM_INCLUDE_TESTSOLVERS
		// old solver for gfx demo
		mpLbm = createSolverOld();
#endif // LBM_TESTSOLVER
	} else {
		errorOut("SimulationObject::initializeLbmSimulation : Invalid solver type - note that mDimension is deprecated, use the 'solver' keyword instead");
		exit(1);
	}


  /* check lbm pointer */
	if(mpLbm == NULL) {
		errorOut("SimulationObject::initializeLbmSimulation : Unable to init dim"<<mSolverType<<" LBM solver! ");
		exit(1);
	}
	debugOut("SimulationObject::initialized "<< mpLbm->getIdString() <<" LBM solver! ", 2);

	// for non-param simulations
	mpLbm->setParametrizer( mpParam );
	mpParam->setAttrList( getAttributeList() );
	mpParam->setSize( mpLbm->getSizeX(), mpLbm->getSizeY(), mpLbm->getSizeZ() );
	mpParam->parseAttrList();

	mpLbm->setAttrList( getAttributeList() );
	mpLbm->parseAttrList();
	mParts.parseAttrList( getAttributeList() );
	mParts.setName( getName() + "_part" );
	mParts.initialize( glob );
	
	// init material settings
	string matMc("default");
	matMc = mpAttrs->readString("material_surf", matMc, "SimulationObject","matMc", false );
	mShowSurface   = mpAttrs->readInt("showsurface", mShowSurface, "SimulationObject","mShowSurface", false ); 
	mShowParticles = mpAttrs->readInt("showparticles", mShowParticles, "SimulationObject","mShowParticles", false ); 

	checkBoundingBox( mGeoStart, mGeoEnd, "SimulationObject::initializeSimulation" );
	mpLbm->setGeoStart( mGeoStart );
	mpLbm->setGeoEnd( mGeoEnd );
	mpLbm->setRenderGlobals( mpGlob );
	mpLbm->setName( getName() + "_lbm" );
	mpLbm->initialize( NULL, mpGlob->getScene()->getObjects() );

	// print cell type stats
	const int jmax = sizeof(CellFlagType)*8;
	int totalCells = 0;
	int flagCount[jmax];
	for(int j=0; j<jmax ; j++) flagCount[j] = 0;
	int diffInits = 0;
	LbmSolverInterface::CellIdentifier cid = mpLbm->getFirstCell();
	for(; mpLbm->noEndCell( cid );
	      mpLbm->advanceCell( cid ) ) {
		int flag = mpLbm->getCellFlag(cid,0);
		int flag2 = mpLbm->getCellFlag(cid,1);
		if(flag != flag2) {
			diffInits++;
		}
		for(int j=0; j<jmax ; j++) {
			if( flag&(1<<j) ) flagCount[j]++;
		}
		totalCells++;
	}
	mpLbm->deleteCellIterator( &cid );

#if ELBEEM_BLENDER!=1
	char charNl = '\n';
	debugOutNnl("SimulationObject::initializeLbmSimulation celltype stats: " <<charNl, 5);
	debugOutNnl("no. of cells = "<<totalCells<<", "<<charNl ,5);
	for(int j=0; j<jmax ; j++) {
		std::ostringstream out;
		if(flagCount[j]>0) {
			out<<"\t" << flagCount[j] <<" x "<< convertCellFlagType2String( (CellFlagType)(1<<j) ) <<", " << charNl;
			debugOutNnl(out.str(), 5);
		}
	}
	// compute dist. of empty/bnd - fluid - if
	// cfEmpty   = (1<<0), cfBnd  = (1<< 2), cfFluid   = (1<<10), cfInter   = (1<<11),
	{
		std::ostringstream out;
		out.precision(2); out.width(4);
		int totNum = flagCount[0]+flagCount[2]+flagCount[10]+flagCount[11];
		double ebFrac = (double)(flagCount[0]+flagCount[2]) / totNum;
		double flFrac = (double)(flagCount[10]) / totNum;
		double ifFrac = (double)(flagCount[11]) / totNum;
		double eifFrac = (double)(flagCount[11]+flagCount[26]+flagCount[27]) / totNum;
		//???
		out<<"\tFractions: [empty/bnd - fluid - interface - ext. if]  =  [" << ebFrac<<" - " << flFrac<<" - " << ifFrac<<" - " << eifFrac <<"] "<< charNl;

		if(diffInits > 0) {
			debugOut("SimulationObject::initializeLbmSimulation celltype Warning: Diffinits="<<diffInits<<" !!!!!!!!!" , 5);
		}
		debugOutNnl(out.str(), 5);
	}
#endif // ELBEEM_BLENDER==1

	mpLbm->initParticles( &mParts );

	// this has to be inited here - before, the values might be unknown
	ntlGeometryObject *surf = mpLbm->getSurfaceGeoObj();
	if(surf) {
		surf->setName( "final" ); // final surface mesh 
		// warning - this might cause overwriting effects for multiple sims and geom dump...
		surf->setCastShadows( true );
		surf->setReceiveShadows( false );
		surf->searchMaterial( glob->getMaterials() );
		if(mShowSurface) mObjects.push_back( surf );
	}
	
	mParts.setStart( mGeoStart );
	mParts.setEnd( mGeoEnd );
	mParts.setCastShadows( false );
	mParts.setReceiveShadows( false );
	mParts.searchMaterial( glob->getMaterials() );
	if(mShowParticles) mObjects.push_back( &mParts );

	// add objects to display for debugging (e.g. levelset particles)
	vector<ntlGeometryObject *> debugObjs = mpLbm->getDebugObjects();
	for(size_t i=0;i<debugObjs.size(); i++) {
		debugObjs[i]->setCastShadows( false );
		debugObjs[i]->setReceiveShadows( false );
		debugObjs[i]->searchMaterial( glob->getMaterials() );
		mObjects.push_back( debugObjs[i] );
	}
	return 0;
}


/******************************************************************************
 * simluation interface: advance simulation another step (whatever delta time that might be) 
 *****************************************************************************/
void SimulationObject::step( void )
{
	mpLbm->step();

	mParts.savePreviousPositions();
	mpLbm->advanceParticles( &mParts );
	mTime += mpParam->getStepTime();
	if(mpLbm->getPanic()) mPanic = true;

  //debMsgStd("SimulationObject::step",DM_MSG," Sim '"<<mName<<"' stepped to "<<mTime<<" (stept="<<(mpParam->getStepTime())<<", framet="<<getFrameTime()<<") ", 10);
}
/*! prepare visualization of simulation for e.g. raytracing */
void SimulationObject::prepareVisualization( void ) {
	if(mPanic) return;
	mpLbm->prepareVisualization();
}


/******************************************************************************/
/* get current start simulation time */
double SimulationObject::getStartTime( void ) {
	//return mpParam->calculateAniStart();
	return mpParam->getAniStart();
}
/* get time for a single animation frame */
double SimulationObject::getFrameTime( void ) {
	return mpParam->getAniFrameTime();
}
/* get time for a single time step  */
double SimulationObject::getStepTime( void ) {
	return mpParam->getStepTime();
}


/******************************************************************************
 * return a pointer to the geometry object of this simulation 
 *****************************************************************************/
//ntlGeometryObject *SimulationObject::getGeometry() { return mpMC; }
vector<ntlGeometryObject *>::iterator 
SimulationObject::getObjectsBegin()
{
	return mObjects.begin();
}
vector<ntlGeometryObject *>::iterator 
SimulationObject::getObjectsEnd()
{
	return mObjects.end();
}





/******************************************************************************
 * GUI - display debug info 
 *****************************************************************************/

void SimulationObject::drawDebugDisplay() {
#ifndef NOGUI
	//debugOut(" SD: "<<mDebugType<<" v"<<getVisible()<<" don"<< (mDebDispSet[mDebugType].on) , 10);
	if(!getVisible()) return;

	if( mDebugType > (MAX_DEBDISPSET-1) ){
		errorOut("SimulationObject::drawDebugDisplay : Invalid debug type!");
		exit(1);
	}

	mDebDispSet[ mDebugType ].on = true;
	//errorOut( mDebugType <<"//"<< mDebDispSet[mDebugType].type );
	mpLbm->debugDisplay( &mDebDispSet[ mDebugType ] );

	::lbmMarkedCellDisplay<>( mpLbm );
#endif
}

/* GUI - display interactive info  */
void SimulationObject::drawInteractiveDisplay()
{
#ifndef NOGUI
	if(!getVisible()) return;
	if(mSelectedCid) {
		// in debugDisplayNode if dispset is on is ignored...
		::debugDisplayNode<>( &mDebDispSet[ FLUIDDISPGrid ], mpLbm, mSelectedCid );
	}
#endif
}


/*******************************************************************************/
// GUI - handle mouse movement for selection 
/*******************************************************************************/
void SimulationObject::setMousePos(int x,int y, ntlVec3Gfx org, ntlVec3Gfx dir)
{
	normalize( dir );
	// assume 2D sim is in XY plane...
	
	double zplane = (mGeoEnd[2]-mGeoStart[2])*0.5;
	double zt = (zplane-org[2]) / dir[2];
	ntlVec3Gfx pos(
			org[0]+ dir[0] * zt,
			org[1]+ dir[1] * zt, 0.0);

	mSelectedCid = mpLbm->getCellAt( pos );
	//errMsg("SMP ", mName<< x<<" "<<y<<" - "<<dir );
	x = y = 0; // remove warning
}
			

void SimulationObject::setMouseClick()
{
	if(mSelectedCid) {
		::debugPrintNodeInfo<>( mpLbm, mSelectedCid, mpLbm->getNodeInfoString() );
	}
}