Saturday, November 3, 2012

Fluid mechanics: continuity equation

In order to overcome the problem in FLIP solver, I looked into fluid mechanics.

It turns out the condition for incompressibility is not correct, or to say, based on an incorrect assumption.
For continuous fluid, the governing equation is called Continuity Equation
I'll skip the proving part of this equation, lots of reading materials could be found online discussing proving of this equation. In symbolic form, this equation could be expressed as:
Compare to the incompressible confinement, we could see, it's just assuming material derivative for density is 0. Yet this assumption is not correct if fluid cells are marked by the particles: temporay incoherence would lead to a non-zero density material derivative.

One big problem associated with adding this term to the confinement is, we'll have to deal with both time derivative and spacial gradient in the Eulerian grid.

So one of the possible solutions would be: combine SPH and FLIP together in this step. The density carried by each particle provides sufficient information for the material derivative. I'll keep on reading and thinking about other possible solutions.


Solution for compressibility problem in Eulerian method

As I mentioned before, FLIP solver suffers from compressibility problem. In fact, all the grid-based method might be suffering from comressibility problem.

You might consider me to be naive, but I'll try to convince you:

The grid based solver calculate the velocity field based on two formula:

1st is the Navier Stokes equation, 2nd is the incompressible constraint.

For grid solver, all the calculations are done based on one simple assumption:
all the fluid cells have the same density as the rest density.

And this is the key why these solvers are compressible. Though, it's just a small difference, which is hard to tell visually. This problem would become apparent in FLIP solver: with the same particle input, using different grid resolution would lead to different result, some shrink the volume(high resolution), some increase the volume(low resolution).

I tried to associate a density coefficient for each cell for the pressure solve, but it does not contribute. If you combine the rhow and p term in the 1st equation as a whole, you'll find out although the density coefficient would affect the value for pressure, when calculating back to velocity field, this term would be eliminated.

So I realize the problem lies in the second formula. There should be something more than just velocity field to conserve the volume.

It turns out I'm right. Well it's a pity that I found out someone has already done research into this before, or maybe I could be the first one.
With a simple search I found 2 papers dealing with this problem:
This one by Nuttapong Chentanez and Matthias Muller,
and this one by Michael Lentine, Mridul Aanjaneya and Ronald Fedkiw.

What is interesting is that what I've been doing always match these guys research. The papers I've been referring are always done by these names.

So next step: reading these papers and integrate into the FLIP solver. This might be useful for SPH as well, but I need to take a further look.


Tuesday, October 30, 2012

Flip solver update

for the past few days I've been working on this solver.

I imported the ghost sph sampler and anisotropic kernel part for surface reconstruction. This time everything was done with tbb(Thread Building Blocks), parallelizing all the particles. The performance is really satisfying: for 150K particles, the mesh reconstruction took less than 2 seconds per frame, including neighbor search, iterative matrix decomposition and color field gathering.

Compare to my previous implementation of this part, due to everything is done on the fly, the memory deallocation cost reduced drastically, and that is one of the major reason for boosting the performance.


However, I discovered a huge problem for this solver.
It is COMPRESSIBLE!
The error is introduced by particle advection. Within each single frame, the projection is ensured to be incompressible, however, after particles being advected, the number for fluid cells would change, and that is the reason why it is compressible.

With the 150k particle configuration, using 50*50*50 grid would lead to volume increasing, while 100*100*100 would shrink volume into a thin sheet.


In fact, setting the divergence to be free would not be sufficient for FLIP solver. Because the density(or mass) is carried with the particles, while for the grid solving part, there's no way in ensuring the density of each cell to be constant.

This problem could be alleviated with a certain grid size for a certain particle distribution. Yet only alleviation is possible, cause the particle and grid are kind of coupled in terms of density.

Right now I had an idea which might be useful for decoupling the two, but I need to take a further look into the physics and math.

I'll discuss with the author of FLIP paper, and hopefully I can find a better way for it soon.

Wednesday, October 24, 2012

FLIP solver done!

Finally finished the FLIP solver.

Right now it's a first version, so still, lots of details need to be improved.

A quick demo for the new solver is here.


Well I'll keep on working on this project and come up with way better polished demo.

Sunday, October 21, 2012

Fast sweeping

Still working on the FLIP solver.
The fast sweeping algorithm is significant for FLIP solver, in generating levelset data and extending velocity field.

Based on Hongkai Zhao's paper I implemented the fast sweeping algorithm for the FLIP solver.
However proving part for the paper was using 2d example. So I extended the proving part to 3D, and here's the scanned image of the proving(also the pseudo code for the core part is included).
Compared to the implementation I did during the summer, the current one would be more abstract because of I'm using the result of my own proving directly. As a result of which, the operation would be faster, because the number of comparison and numerical calculation are minimized.

As I always says, math and physics are real science compared to computer science. In my biased personal opinion, a huge difference between science and engineering is: science is continuous and engineering is discretized.

Thanks math and physics for always providing guidance for everything.
I really enjoy this kind of life, surrounded by science. Maybe I should go for a physics phd later. Seriously I'm not kidding.

Thursday, October 4, 2012

New solver for the fluid simulation project

SPH suffers from severe compressible problem. A combination of WCSPH( http://cg.informatik.uni-freiburg.de/publications/2007_SCA_SPH.pdf ) and PCISPH( http://dl.acm.org/citation.cfm?id=1531346 ) could be a good improvement of SPH. 

However, it's far from enough. It could be a good solution for the inner particles. while for the boundary particles(either in contact with air or solid), the pressure gathering is incorrect, which will lead to an artifact.

Ghost sph(http://www.cs.ubc.ca/~rbridson/docs/schechter-siggraph2012-ghostsph.pdf) to me is the best solution for improving the quality of SPH. But the problem is ghost sph is kind of expensive. The number of sample particles for the solids is usually much more than the fluid particles. And re-sampling ghost particles is not trivial task.

In order to solve the compressibility problem and maintain the details that particles bring about, I decided to turn to FLIP(http://www.cs.ubc.ca/~rbridson/docs/zhu-siggraph05-sandfluid.pdf) for help.

FLIP is a combination of Lagrangian method and Eulerian method. Particles are used for advection, which eliminates the numerical dissipation caused by Eulerian methods, and MACGrid is used for solving the Poisson equation, which solve the pressure distribution problem perfectly.

For the past few days I've been reading lots of papers related to Eulerian fluid simulation, including Robert Bridson's book: Fluid Simulation for Computer Graphics. And finally I got a good understanding of each steps that is necessary for FLIP solver.

I'll list my understanding of the core steps in FLIP solver here:

1. transfer velocity from particles to grid. The grid is only used for solving the pressure distribution, so only the velocity field is needed. The way used for transferring is splatting each particle's velocity onto the grid using tri-linear weighting.

2. generate a level set from the particles. The level set is necessary for later use. 

3. extend the velocity field to the whole grid. Before this step, only the grid cells that intersect the particles have a non-zero velocity. In order to get the correct velocity distribution, we have to extrapolate the velocity to the whole grid based on one simple principal: the dot product of the gradient of velocity and the gradient of level set should be zero. Which means the extrapolated velocity should not change in the normal direction of fluid surface.

4. solve for Poisson equation. I haven't start coding with this part. Yet from the previous smoke simulation project, this should be similar. The key is setting boundary condition, using ghost pressure and apply pre-conditioner.

5. extend the velocity again. In the previous step, only the velocity field of the fluid cells have been updated, so in order to get a correct value in interpolating back to particles, we need to update the rest part of the grid.

6. transfer back to the particles. Update particles' velocity based on the new velocity field.

That's basically my understanding for the FLIP solver from reading during the past few days. I'm starting to write this solver part and integrate that for my fluid simulation project.

Friday, September 28, 2012

Level set vs Color field

For the mesh extraction part of particle based fluid simulation, you have to create a surface field before actually execute the mesh extraction function(e.g. marching cube). And generally there're two ways of doing this: level set and color field.

For level set, the idea is to splat each particle to the region it belongs to, and in terms of different values assigned to a certain sample point, min operator could be employed to get the exact data. 

However, this could be slow. The level set field for a certain particle is continuous, that is, no matter how far the sample point is, there's always a value for that. So you have to decide a radius that the particle have to splat to. Theoretically, the larger the splatting radius is, the closer the final result is to the "ideal" field. 

Another huge problem is, this could only be useful for spherical particles. I'm using anisotropic particles(elliptic), and calculating the distance from an arbitrary point to an ellipsoid is not trivial task. With my current configuration, a single frame would cost more that 10 mins to create the level set field only. The problem lies in the solving part. By using ellipsoids, the distance solving part relies on iterative method, and that is the bottleneck for performance.

So in order to calculate a field faster, I turned to the "color field". Level set is defined as "signed distance field", while the color field is defined as "1 for the particle center, and 0 for outside region", and for the position within the radius of the particle, the value is decided by the smoothing kernel(e.g. B-cubic kernel).

The only problem for color field is, it does not satisfy the Eikonal Equation. And this would lead to a improper value for the normal. For the past few days, I've been thinking about methods that could eliminate or improve this part. 

One of my idea is extend the kernel. Right now the kernel is limited to the (-r, r) range, and other from that, all the values are 0. Let's imagine the level set method works in a similar way as kernel: the kernel for level set has no boundary. And that's what I've mentioned about: no matter how far the sample point is, there's always a value. 

If we could design a kernel without boundary, and also could return a value that somehow reflect the definition of color field, the problem would be improved. (Not totally solved cause the Eikonal Equation still remains a problem). 

If anyone has good ideas about this, don't hesitate to contact me. This could be huge.

Fluid simulation project

During the past few days, I re-wrote my fluid simulation project.

I modulized everything in a way similar to the fluid simulation pipeline in DreamWorks. Also I modified the mesh extraction part to make it much more faster.

The performance right now is 15~20 seconds per frame using single core CPU for 150K particles. I didn't use the optimized algorithm for single core because I'm planning to doing everything in parallel on CPU. So in terms of performance, this is the worst case. However, to my satisfaction this is still pretty quick.


Here is my new demo reel, including a clip for fluid simulation using 125K particles.

Yet this is not a good demo, because:
1. I'm using a too small smoothing radius for the particles, and ends up with really bumpy surface, which should not be the case.
2. SPH method suffers from severe compressible problem. That would lead to an additional layer in in the surface extraction part.
3. Initialization part was kind of wacky. I've done another simulation using the sample method for initializing particles from the Ghost SPH paper I've been working on. And the result is better.


For the 1st and the 3rd problem, I've already improved my project to solve these problem. The openGL version images are already there, and I'm planning to render out a maya version for a better demo.

However, because of the compressibility, the second problem could not be easily solve. I've tried to use WCSPH, yet still I can not find a proper configuration for that implementation, and also I don't think that is a good way to solve the problem. It's just a numerical method dedicated for this problem, but not physically based. My original plan is to implement the Ghost SPH as a complimentary part of my simulator, yet that paper suffers from lacking of elaboration, and even by contacting the author I still did not get a satisfying answer. Now I've started to wonder how they implement that paper.

The good news is, I'm planning to turn to FLIP for the solver part. FLIP combine the best part of eulerian method and lagrangian method, and I believe that would be the best solution for me. Hopefully that won't be too hard.

Another item on the to-do-list is to substitute marching cube with dual contour method.

BTW I might implement another version using PCISPH combine WCSPH for comparison. My personal expectation for the FLIP method is to be better than PCISPH + WCSPH.

In the end, I'm still obsessed with ghost SPH. If anyone want to discuss about that with me, I'll be love to talk about that.

I've been working on this project for a relative long time, and tried lots of things. like the creating level set field for ellipsoids(which is extremely time consuming), and converting obj file to level set. these would be done after I finished the compressibility problem. In addition, my tracer project has to be postponed, cause this project has the most priority to me.

Wednesday, September 19, 2012

Demo Reel v0.22

Updated the demo reel.

Too much homework recently. Do not have enough time for my personal project.

I hope that I can get more time for updating the fluid sim. I've done lots of improvement during the summer but haven't integrated into the reel yet.


Friday, September 14, 2012

New demo for the GPU tracer

the image rendered with tone mapping satisfied me a lot. So I made a new demo for the tracer. I turned off the antweak bar and the fps viewer for less distraction.

Here's the new demo:

Monday, September 10, 2012

Tone mapping

I made a slight change to my GPU path tracer in color transferring.

Previous I was using gamma correction, with gamma equals 2.2, now I'm using the tone mapping operator proposed by Paul Debevec. 

The difference is shown as following:

In the 1st comparison group I turned off the depth of field, just in order to focus on the color difference. And the render time is only 150s, no sufficient for full convergence but good enough for color comparison.
 Image rendered with tone mapping operator
Image rendered with Gamma correction

In the 2nd comparison group I kept all the features on and take 600s for the image to be fully converged.

Image rendered with tone mapping operator

Image rendered with Gamma correction

For the gamma correction group I'm using radiance 16 for the light, but for the tone mapping group I'm using 75 for light.

Personally speaking I prefer tone mapping. It's not that shiny and looks way much better!

New tracer

Based on the fact that I got stuck in implementing the GPU KD-tree paper, I decided to turn to something else.

I've been reading things related to HDR and found it's pretty interesting, and I'm not satisfied with my previous two tracers. So I want to re-write my tracer.

The plan could be like:

Step 1: set up the all the openGL stuff. Rendering everything on to a frame buffer and display the frame buffer with time. Setting up the basic scene is necessary for this step, including camera and the most simple ray trace function(could be just returning a color based on the pixel coordinates).

Step 2: enrich the scene. Setting up objects class and material class. Implement basic ray trace algorithm, which is easy. The material class should be compatible with the HDR, cause that's what I'm trying to focus on for this project. The result of this step is ray-traced image of a simple scene(could be a cornell box with a single sphere).

Step 3: build a CPU kd-tree for complicated objects like stanford bunny or armadillo. Change ray trace with monte carlo path trace. My implementation of material class have to be changed to be better modulized to used BRDF. The result of this one should be a nice rendered image which could function as reference image. Yet the process must be really slow.

Step 4: try implementing the GPU kd-tree paper again and ship everything onto GPU, or try implement photon mapping in the tracer. Haven't thought about that far yet.

Hopefully this project could keep me busy for a while. I'll keep the progress updated.

Friday, August 3, 2012

Cube sample

The most important thing today is I finished the cube sampling part.

Usually a cube is consist of 6 discontinuous surfaces, which is hard for constructing level set field. Actually I've done several experiments to create level set from discrete meshes like a triangle mesh, but the I failed all the time because I don't know how to deal with the discontinuity.

This time I'm using a trick to avoid the discontinuity. By using spherical surface on the corner and cylindrical surface on the edge, I could create correct level set data for a "soft" cube.

This is important because in most cases, the scene is just a box, and I need this cube sample to do construct all the solid particles.

Now everything new has pretty much be finished. The only thing left is to assemble all the parts together. Yet SigGraph is approaching, so I might not have enough time to finish this project. Anyway I'll try my best.


Here's a image of cube sample. The test scene for me would be a large spherical water drop falling in a cube. Or maybe a water cube falling in a sphere. I'm so excited it's almost done!

Solid particle sample finished

A good news is I totally understood the process of sampling today. 

I've been reading this paper for around 10 times to finally figure out the main process of sampling. I really wish the authors could explain more on the basic process instead of focusing on their contribution.

Solid particles are sampled only once at the beginning of the simulation. And later the solid particles would not move, or move only with the solid object. The paper mentioned there is a velocity for solid particle. That is its ghost velocity and would only be used in calculating the viscosity force for fluid particles that is near the solid surface. In integration, there is no real velocity for solid particles(as long as solid object does not move).

Because the relaxation process is really really slow for fluid volume samples, and based on the fact that all the fluid sample and solid sample has only to be done once during the simulation, I decided to write a file exporter/importer. I can run the simulator with a relatively higher relaxation iteration number and thus getting a better distribution of all the particles, and export particles to a certain file. This has to be done only once, and later when I will be testing other parts, I can just import initial state of particle from the generated file instead of sampling again.

Now the biggest problem to be is related to projection. In the sample process, a random sample is supposed to be projected to the surface which is described by a level set. Yet when I tried to use level set grid to do the projection work, I ended up with a crappy result. I think that is my fault in implementation. Hopefully I will fix that tomorrow, cause I don't want to use implicit surface only.

I'll post a picture of all kinds of particles when I fixed the existing problems.

Thursday, August 2, 2012

Ghost SPH Sample Finished

Finished the sampling part of ghost SPH.

The sampling process take a few steps as follows. The input is a level set field which describe the shape of the fluid.

1. Sample the surface, namely those voxels where level set change sign.

2. Relaxing surface particles. This would yield better distribution of surface particles(blue noise).

3. Sample the interior part of fluid. Using the surface particles as initial seed.

4. Apply volume relaxation. This is a extremely slow process, multiple iterations, and within each iteration, neighbor search has to be applied to each particle.

5. Sample air particles. Similar as volume sample. Using surface particles as seeds, sample air particles outside the surface within a single-smooth-kernel layer with the help of level set.

The result so far looks like this:

The green particles are ghost particles, and blue ones as fluid particles.

The next step would be transplanting all the calculation parts in my previous SPH simulator into this new one.

My understanding for the paper might not be 100 percent correct, now I still have a few confusion related to this paper. I'm contacting the author recently and hopefully they could unravel my questions.

I'll keep updated.

Tuesday, July 31, 2012

Preliminary result about Ghost SPH

I started re-writing my SPH simulator a few days ago, and the desired new feature is ghost SPH based on Hagit and Bridson's new paper: Ghost SPH for Animating Water
http://www.cs.ubc.ca/~rbridson/docs/schechter-siggraph2012-ghostsph.pdf

A main problem about SPH lies in the density gathering.

In real world, water is almost incompressible, which means at each sample point in the water, the density should be almost the same. However, in simulation, density for each particle may vary within a wide range. This would lead to conspicuous artifacts like y-stacking.

Though generally speaking the result still looks like water, the details could not satisfy me when I pay close attention to the details. After all it's the details that matters in getting distinguished.

To eliminate the problem, there're two important things to do: correct the density gathering and re-model the pressure calculation.

1. correct density gathering. This is the core idea of the ghost sph paper. By using another layer of ghost particles, we could eliminate the density deficiency for particles near the surface. Also the paper discussed about how to initialize the particles, which is rarely seen in other SPH paper and is exactly what I need. The technique used for initialize SPH particles is Poisson Disk Sampling, which could arrange all the particles with blue noise. The sampling technique is based on Bridson's another paper: Fast Poisson Disk Sampling in Arbitrary Dimensions
http://www.cs.ubc.ca/~rbridson/docs/bridson-siggraph07-poissondisk.pdf

2. re-model the method to calculate the pressure. Using Tait equation based on the WCSPH paper (whose performance could be improved by implementing the PCISPH). The Tait equation generate a pressure proportional to (rhow/rhow0)^7, so a correct density gathering is a prerequisite, or the pressure force would be too powerful and lead the simulator to an unstable stage.

Now I've pretty much finished the sampling part. The particle sampling is consist of 4 parts: surface sample, surface relaxation, volume sample, volume relaxation.

Here're two images of the result of applying particle sample to a simple spherical level set grid.




Friday, July 27, 2012

The new SPH simulator

Recently I've been working on re-writing my SPH simulator.

There're lots of problems in the previous version that drastically slow done the simulator. For simulating 125K particles, it takes around 3 mins to get a single frame with drawing triangle meshes.

The surface reconstruction part was not done by myself so I did not know the detail. However recently I found out why the previous version is so slow and rewrite the surface reconstruction part, including marching cube and openGL utilities. The result is a 6 time's improvement in performance.

Because of the glut library is pretty old, so I choose glfw library for this new version. I also modified the neighbor searching method to support multi-thread execution. For the surface reconstruction, anisotropic kernel is calculated for each particle to better describe the density distribution.

Here're some comparing images of a water-crown scene.
1. drawing elliptic particles only.
2. drawing triangle meshes with isotropic kernel. i.e. drawing spheres to present particles.

3. drawing triangle meshes with anisotropic kernel. i.e. drawing ellipsoids to present particles.

I've tried to implement WCSPH but I found out my initialization is problematic. Based on the fact that there's few paper or blog talking about initialization of SPH, I decide to implement the Bridson's Ghost SPH paper first. After finishing that paper, WCSPH could be finished with slight changes and so is the PCISPH.

These papers have been on my "to-do list" for a few weeks, but I was too tired after work everyday so the progress is slow. I'll keep updated.

Also, considering that I'm getting more and more familiar with openGL, I'm considering re-write my path tracer and try to add something new.

Tutorial: installing boost on Visual Studio 2010

Well because of work I got started to use the boost library. It's so handy that I want to install it on my own laptop. I followed a video to install the library, and here's my experience that I'm willing to share.

1. Download the latest boost library: http://www.boost.org/users/download/

2. Unzip the file to a certain folder, which would be used later as a path in the user configuration in Visual Studio 2010. Here I'll use "D:\boost_1_50_0\" for example.

3. A large part of the library is only header file, need not to be compiled. To get a full version(due to the "geek's nature"), we need to compile the library. 

Click "Start"->"All programs"->"Microsoft Visual Studio 2010"->"Visual Studio Tools"->"Visual Studio Command Prompt (2010)"

This command window has correct environment configuration for VS2010. Now go to the place where you unzip the boost library using "cd" command. Here the location is "D:\boost_1_50_0\", so the command is "cd D:\boost_1_50_0\".

4. Run command "bootstrap". Running this could give you a executable called "bjam", which is the building system for boost.

5. Run command:
bjam toolset=msvc-10.0 variant=debug,release threading=multi link=static 
The parameters setting may vary depending on user's own interest.

Notice that the compiling process may take more than 20 minutes. So take the time to do something else.

6. Now we've finished compiling the library, and it's time to turn to setting Visual Studio. Go to "C:\Users\%USERNAME\AppData\Local\Microsoft\MSBuild\v4.0"
Open the Microsoft.Cpp.Win32.user.props file and modify it.

Under the <IncludePath> tag add the root folder where you extract the boost library. Here is "D:\boost_1_50_0;"

Unver the <LibraryPath> tag do a similar thing, but with different path, under the folder "/stage/lib". Here is "D:\boost_1_50_0\stage\lib;"

In this way you don't have to configure your every project to support boost. As long as you're logging in as this certain user, the boost library would always be found without costing your time to set project property.


Try to some example code from boost document!
Create an empty win32 console project and add these lines of code to the main.cpp and see the result.
#include <boost/regex.hpp>
#include <iostream>
#include <string>

int main()
{
    std::string line;
    boost::regex pat( "^Subject: (Re: |Aw: )*(.*)" );

    while (std::cin)
    {
        std::getline(std::cin, line);
        boost::smatch matches;
        if (boost::regex_match(line, matches, pat))
            std::cout << matches[2] << std::endl;
    }
}

I followed this video tutorial: 
Big thanks to the author and hope everyone could benefit from that.

Sunday, May 20, 2012

GPU Path Tracer

Path tracing algorithm is extremely suitable for GPU implementation. So I made a GPU path tracer using C++ and CUDA.

The whole process is tracked in another blog of mine here: http://xingdugpu.blogspot.com/
And the final result is the first post.

My first step is to build a GPU ray tracer. I could build path tracer or photon mapping tracer with such a basic framework. All the preparation stuffs are done in this part, like how to upload the scene to GPU using a texture, and how to display a texture in the view port.
Here's a demo showing the result of the simple GPU ray tracer.

The next step is turn it into a path tracer. The algorithm actually is more simple for path tracing. For each hit point, no matter it is diffuse or reflective or refractive, one secondary ray has to be generated. The only difference lies in the BRDF, so it's more uniformed and more suitable for GPU implementation. A rough result could be seen from the following images.
Yet, the refraction is not correct. Not only because I used a low max depth, but also because no fresnel reflection is included. To make it right, fresnel reflection is added. Also the depth of field is included by changing the camera model and doing a distributed ray tracing, and it is for free in path tracing. Images below shows the result adding depth of field and fresnel reflection.

The last step is tuning the color. The radiance gathering process is using radiance instead of rgb values. A gamma correction is used here to generate more mild images as below:

Other available resources about the project are listed here. 
The paper's my favorite.