Skip to content

Commit

Permalink
fixed mem leak with pdata in gui, code and test case cleanup
Browse files Browse the repository at this point in the history
--HG--
branch : particleSys
  • Loading branch information
thunil committed Sep 17, 2013
1 parent f4eccf1 commit 87c7f7f
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 108 deletions.
21 changes: 12 additions & 9 deletions scenes/flip02_surfacePdata.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
from manta import *

# solver params
dim = 3
res = 44
dim = 2
res = 64
gs = vec3(res,res,res)
if (dim==2):
gs.z=1
Expand Down Expand Up @@ -56,32 +56,35 @@
# FLIP
pp.advectInGrid(flaggrid=flags, vel=vel, integrationMode=IntRK4, deleteInObstacle=False )

# make sure we have velocities throught liquid region
mapPartsToMAC(vel=vel, flags=flags, velOld=velOld, parts=pp, partVel=pVel, weight=tmpVec3 )
extrapolateMACFromWeight( vel=vel , distance=2, weight=tmpVec3 )
extrapolateMACFromWeight( vel=vel , distance=2, weight=tmpVec3 )
markFluidCells( parts=pp, flags=flags )

# create approximate surface level set, resample particles
# note - this is slow right now, and could be optimized by only computing a narrow band
unionParticleLevelset( pp, phi )
phi.reinitMarching(flags=flags, maxTime=2 )
pVel.setSource( vel, isMAC=True )
pTest.setSource( tstGrid );
adjustNumber( parts=pp, vel=vel, flags=flags, minParticles=1*minParticles, maxParticles=2*minParticles, phi=phi )

markFluidCells( parts=pp, flags=flags )

# forces & pressure solve
addGravity(flags=flags, vel=vel, gravity=(0,-0.001,0))

# pressure solve
setWallBcs(flags=flags, vel=vel)
solvePressure(flags=flags, vel=vel, pressure=pressure)
setWallBcs(flags=flags, vel=vel)

# we dont have any levelset, ie no extrapolation, so make sure the velocities are valid
# make sure we have proper velocities
extrapolateMACSimple( flags=flags, vel=vel )

flipVelocityUpdate(vel=vel, velOld=velOld, flags=flags, parts=pp, partVel=pVel, flipRatio=0.97 )

if (dim==3):
phi.createMesh(mesh)

gui.screenshot( 'flipt_6_3d_%04d.png' % t );
#gui.screenshot( 'flipt_%04d.png' % t );
#s.printMemInfo()
pp.save( 'parts_%04d.txt' % t );
s.step()

1 change: 1 addition & 0 deletions scenes/plume_2d.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@
setWallBcs(flags=flags, vel=vel)

s.step()

17 changes: 10 additions & 7 deletions source/flip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,12 @@ ParticleBase* FlipSystem::clone() {
FlipSystem::~FlipSystem() { };


//*****************************************************************************
//*****************************************************************************
//*****************************************************************************

// NT_DEBUG , warning - duplicate functions for now, clean up at some point!
// FLIP functions for use with particle data field
// warning - duplicate functions for now, clean up at some point!

// init

Expand Down Expand Up @@ -264,6 +267,11 @@ PYTHON void markFluidCells(BasicParticleSystem& parts, FlagGrid& flags) {
}
}

// for testing purposes only...
PYTHON void testInitGridWithPos(RealGrid &grid) {
FOR_IJK(grid) { grid(i,j,k) = norm( Vec3(i,j,k) ); }
}

// compute simple levelset without interpolation (fast, low quality), to be used during simulation
KERNEL(pts, single)
void ComputeUnionLevelset(BasicParticleSystem& p, LevelsetGrid& phi, Real radius=1.) {
Expand Down Expand Up @@ -422,7 +430,7 @@ PYTHON void mapPartsToGrid ( FlagGrid& flags, Grid<Real>& target , BasicParti
PYTHON void mapPartsToGridVec3( FlagGrid& flags, Grid<Vec3>& target , BasicParticleSystem& parts , ParticleDataImpl<Vec3>& source ) {
mapLinearRealHelper<Vec3>(flags,target,parts,source);
}
// integers need "max" mode
// integers need "max" mode, not yet implemented
//PYTHON void mapPartsToGridInt ( FlagGrid& flags, Grid<int >& target , BasicParticleSystem& parts , ParticleDataImpl<int >& source ) {
// mapLinearRealHelper<int >(flags,target,parts,source);
//}
Expand Down Expand Up @@ -470,11 +478,6 @@ PYTHON void flipVelocityUpdate(FlagGrid& flags, MACGrid& vel , MACGrid& velOld ,
knMapLinearMACGridToVec3_FLIP( parts, flags, vel, velOld, partVel, flipRatio );
}

// for testing only...
PYTHON void testInitGridWithPos(RealGrid &grid) {
FOR_IJK(grid) { grid(i,j,k) = norm( Vec3(i,j,k) ); }
}


} // namespace

8 changes: 8 additions & 0 deletions source/fluidsolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ void FluidSolver::printTimings() {
printf("Total : %s\n\n", total.toString().c_str());
}

void FluidSolver::printMemInfo() {
std::ostringstream msg;
msg << "Allocated grids: int " << mGridsInt.used <<"/"<< mGridsInt.grids.size() <<", ";
msg << "real "<< mGridsReal.used <<"/"<< mGridsReal.grids.size() <<", ";
msg << "vec3 "<< mGridsVec.used <<"/"<< mGridsVec.grids.size() <<". ";
printf("%s\n", msg.str().c_str() );
}

void FluidSolver::saveMeanTimings(string filename) {
ofstream ofs(filename.c_str());
if (!ofs.good())
Expand Down
3 changes: 2 additions & 1 deletion source/fluidsolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class FluidSolver : public PbClass {
//! output performace statistics
PYTHON void printTimings();
PYTHON void saveMeanTimings(std::string filename);
PYTHON void printMemInfo();

//! Advance the solver one timestep, update GUI if present
PYTHON void step();
Expand Down Expand Up @@ -87,4 +88,4 @@ class FluidSolver : public PbClass {

}

#endif
#endif
93 changes: 50 additions & 43 deletions source/gui/painter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,9 +332,9 @@ template<> void GridPainter<int>::paint() {
glColor3f(0.5,0,0);

bool rbox = true;
bool lines = mLocalGrid->getSize().max() <= 40;
lines=true; // debug
if (lines) {
bool skipFluid = mLocalGrid->getSize().max() > 40;
bool drawLines = mLocalGrid->getSize().max() <= 80;
if (drawLines) {
//glDepthFunc(GL_LESS);
glBegin(GL_LINES);
FOR_P_SLICE(mLocalGrid, mDim, mPlane) {
Expand All @@ -349,6 +349,7 @@ lines=true; // debug
} else if (flag & FlagGrid::TypeEmpty) {
glColor3f(0.25,0,0);
} else if (flag & FlagGrid::TypeFluid) {
if(skipFluid) continue;
glColor3f(0,0,0.75);
} else {
glColor3f(0.5,0,0); // unknown
Expand Down Expand Up @@ -387,18 +388,51 @@ template<> void GridPainter<Real>::paint() {
//glPolygonOffset(1.0,1.0);
//glDepthFunc(GL_LESS);

/*FOR_P_SLICE(mLocalGrid, mDim, mPlane) {
int flag = FlagGrid::TypeFluid;
if (flags && (mLocalGrid->getType() & GridBase::TypeLevelset) == 0) flag = flags->get(p);
if (flag & FlagGrid::TypeObstacle)
glColor3f(0.15,0.15,0.15);
else if (flag & FlagGrid::TypeOutflow)
glColor3f(0.3,0.0,0.0);
else if (flag & FlagGrid::TypeEmpty)
glColor3f(0.,0.2,0.);
else {
Real v = mLocalGrid->get(p) * scaler;
const bool useOldDrawStyle = false;
if(useOldDrawStyle) {
// original mantaflow drawing style

FOR_P_SLICE(mLocalGrid, mDim, mPlane) {
int flag = FlagGrid::TypeFluid;
if (flags && (mLocalGrid->getType() & GridBase::TypeLevelset) == 0) flag = flags->get(p);
if (flag & FlagGrid::TypeObstacle)
glColor3f(0.15,0.15,0.15);
else if (flag & FlagGrid::TypeOutflow)
glColor3f(0.3,0.0,0.0);
else if (flag & FlagGrid::TypeEmpty)
glColor3f(0.,0.2,0.);
else {
Real v = mLocalGrid->get(p) * scaler;

if (isLevelset) {
v = max(min(v*0.2, 1.0),-1.0);
if (v>=0)
glColor3f(v,0,0.5);
else
glColor3f(0.5, 1.0+v, 0.);
} else {
if (v>0)
glColor3f(v,0,0);
else
glColor3f(0,0,-v);
}
}

if ((flag & FlagGrid::TypeEmpty) == 0) {
getCellCoordinates(p, box, mDim);
for (int n=0;n<4;n++)
glVertex(box[n], dx);
}
}
glEnd();
} else {
// "new" drawing style

// ignore flags, its a bit dangerous to skip outside info

FOR_P_SLICE(mLocalGrid, mDim, mPlane)
{
Real v = mLocalGrid->get(p) * scaler;
if (isLevelset) {
v = max(min(v*0.2, 1.0),-1.0);
if (v>=0)
Expand All @@ -411,40 +445,13 @@ template<> void GridPainter<Real>::paint() {
else
glColor3f(0,0,-v);
}
}
if ((flag & FlagGrid::TypeEmpty) == 0) {

getCellCoordinates(p, box, mDim);
for (int n=0;n<4;n++)
glVertex(box[n], dx);
}
glEnd();
}
glEnd(); */

// ignore flags, its a bit dangerous :)

FOR_P_SLICE(mLocalGrid, mDim, mPlane)
{
Real v = mLocalGrid->get(p) * scaler;
if (isLevelset) {
v = max(min(v*0.2, 1.0),-1.0);
if (v>=0)
glColor3f(v,0,0.5);
else
glColor3f(0.5, 1.0+v, 0.);
} else {
if (v>0)
glColor3f(v,0,0);
else
glColor3f(0,0,-v);
}

getCellCoordinates(p, box, mDim);
for (int n=0;n<4;n++)
glVertex(box[n], dx);
}
glEnd();

//glDepthFunc(GL_ALWAYS);
//glPolygonOffset(0,0);
}
Expand Down
68 changes: 32 additions & 36 deletions source/particle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,34 @@ using namespace std;
namespace Manta {


//template<class S>
ParticleBase::ParticleBase(FluidSolver* parent) :
PbClass(parent), mAllowCompress(true), mFreePdata(false) {
}

ParticleBase::~ParticleBase()
{
// make sure data fields now parent system is deleted
for(int i=0; i<(int)mPartData.size(); ++i)
mPartData[i]->setParticleSys(NULL);

if(mFreePdata) {
for(int i=0; i<(int)mPartData.size(); ++i)
delete mPartData[i];
}

}

void ParticleBase::cloneParticleData(ParticleBase* nm) {
/*ParticleBase* nm = new ParticleBase(getParent());
compress();
nm->mData = mData;
nm->setName(getName());*/
std::string ParticleBase::infoString() const {
return "ParticleSystem " + mName + " <no info>";
}

// clone additional data
void ParticleBase::cloneParticleData(ParticleBase* nm) {
// clone additional data , and make sure the copied particle system deletes it
nm->mFreePdata = true;
for(int i=0; i<(int)mPartData.size(); ++i) {
ParticleDataBase* pdata = mPartData[i]->clone();
nm->registerPdata(pdata);
}
//return nm;
}

void ParticleBase::deregister(ParticleDataBase* pdata) {
Expand Down Expand Up @@ -119,30 +125,25 @@ std::string ParticleBase::debugInfoPdata()
return sstr.str();
}


//! basic particle system


BasicParticleSystem::BasicParticleSystem(FluidSolver* parent)
: ParticleSystem<BasicParticleData>(parent) {
this->mAllowCompress = false;
: ParticleSystem<BasicParticleData>(parent) {
this->mAllowCompress = false;
}

std::string BasicParticleSystem::infoString() const {
std::ostringstream s;
s << "oParticleSystem '" << getName() << "' [" << size() << " parts]";
if( this->getNumPdata()>0) s << " Pdata: "<< this->getNumPdata() <<" " ;
// NT_DEBUG, not working, check...
return s.str();
};

// file io


static void writeParticlesText(string name, BasicParticleSystem* p) {
void BasicParticleSystem::writeParticlesText(string name) {
ofstream ofs(name.c_str());
if (!ofs.good())
errMsg("can't open file!");
for(int i=0; i<p->size(); ++i) {
ofs << i<<": "<< p->getPos(i) <<" , "<< p->getStatus(i) <<"\n";
ofs << this->size()<<", pdata: "<< mPartData.size()<<" ("<<mPdataInt.size()<<","<<mPdataReal.size()<<","<<mPdataVec3.size()<<") \n";
for(int i=0; i<this->size(); ++i) {
ofs << i<<": "<< this->getPos(i) <<" , "<< this->getStatus(i) <<". ";
for(int pd=0; pd<(int)mPdataInt.size() ; ++pd) ofs << mPdataInt [pd]->get(i)<<" ";
for(int pd=0; pd<(int)mPdataReal.size(); ++pd) ofs << mPdataReal[pd]->get(i)<<" ";
for(int pd=0; pd<(int)mPdataVec3.size(); ++pd) ofs << mPdataVec3[pd]->get(i)<<" ";
ofs << "\n";
}
ofs.close();
}
Expand All @@ -151,23 +152,18 @@ void BasicParticleSystem::load(string name) {
if (name.find_last_of('.') == string::npos)
errMsg("file '" + name + "' does not have an extension");
string ext = name.substr(name.find_last_of('.'));
if (false) { // ext == ".txt")
//readParticlesText(name, this);
}
//else if (ext == ".uni")
//readGridUni(name, this);
else
/*if ( ext == ".txt")
readParticlesText(name, this);
} else */
errMsg("particle '" + name +"' filetype not supported for loading");
}

void BasicParticleSystem::save(string name) {
if (name.find_last_of('.') == string::npos)
errMsg("file '" + name + "' does not have an extension");
string ext = name.substr(name.find_last_of('.'));
if (ext == ".txt")
writeParticlesText(name, this);
//else if (ext == ".uni")
//writeGridUni(name, this);
if (ext == ".txt")
this->writeParticlesText(name);
else
errMsg("particle '" + name +"' filetype not supported for saving");
}
Expand Down
Loading

0 comments on commit 87c7f7f

Please sign in to comment.