 # Thread: [VB6] Simple SPH (smoothed particles hydrodynamics)

1. ## [VB6] Simple SPH (smoothed particles hydrodynamics)

Simple SPH

SPH is a technique for simulating fluids.

I think this is the simplest 2D implementation of SPH

It uses a slightly modified Spatial grid to detect the pairs of particles to be processed.

It's very simple, based on 3 main "rules":
• Attraction
• Density & Pressure
• Viscosity

ATTRACTION:
When the particles are close together they attract each other with a force proportional to the square of the inverse of the distance.
If r is the distance between them (normalized in the range 0-1)
Their attraction strength is equal to (1-r) ^ 2

Density and PRESSURE:
The density of each particle is calculated by adding up the weight of neighboring cells. (within the radius h)
The weight of neighboring cells is given by the Density Kernel:
Code:
```Private Function SmoothKernel_3(ByVal r As Single) As Single
r = r * 2!
If r <= 1! Then
SmoothKernel_3 = 1! - 1.5! * r * r + 0.75! * r * r * r
Else
r = 2! - r
SmoothKernel_3 = 0.25! * r * r * r
End If
End Function```
The pressure of each particle is given by
Pressure (I) = (Density (I) - RestDensity) / RestDensity

The replusion force due to pressure is given by the average pressure of the pair of particles multiplied by the Density Kernel * (1 - r)

VISCOSITY

The force due to the viscosity between two particles is given by
This smoothing kernel: -0.5 * r ^ 3 + r ^ 2 + 1 / (2 * r) - 1
It is applied when the particles are approaching each other.
When they are moving away, however, only a small part of this force is applied.

vbRichClient is needed
Have fun

look at smoothing kernel graph here:
https://www.desmos.com/calculator/vz0kydxch7

2D

EDIT

Here is the 3D version!

• cSpatialGrid3D To quick detect pairs of points at a fixed minumun distance in a 3D space
• m3DEasyCam To manage a virtual camera and convert 3D world coordinates to the screen ones
• mQuickSort To draw points from farthest to nearest to camera

move clicked-mouse on picture to rotate camera.

EDIT 2

Update to 3D v4
Basically:
-new simplified camera model
-Fake particels at boundaries

EDIT 3
Latest 3D Version on GitHub
From 3D version V.5 , camera has been improved, now it can manage lines which have one of its points that goes behind the camera by using "near-clipping-plane" technique.  Reply With Quote

2. ## Re: [VB6] Simple SPH (smoothed particles hydrodynamics)

Wow that's beautiful work! Mesmerizing   Reply With Quote

3. ## Re: [VB6] Simple SPH (smoothed particles hydrodynamics) Originally Posted by jpbro Wow that's beautiful work! Mesmerizing Tell me about it, and with SO little code. Yes I examined it some. There are *very* few in the world who can pull this off, no matter how much code is used.  Reply With Quote

4. ## Re: [VB6] Simple SPH (smoothed particles hydrodynamics)

Can you model something like these "planets" impacting?

https://vp.nyt.com/video/2019/05/01/...eo_wg_720p.mp4

cheers,
</wqw>  Reply With Quote

5. ## Re: [VB6] Simple SPH (smoothed particles hydrodynamics)

Well, that would require 3D calculations, so off hand, a fair amount of extra calculation compared to a 2D fluid calculation.
Have to wonder whether the planet calculation has to take into consideration the various layers and type of material the planets are made of and account for those in the particle calculations.  Reply With Quote

6. ## Re: [VB6] Simple SPH (smoothed particles hydrodynamics)

Very interesting code, thank you reexre.

In addition, the math function expressions in desmos are equally interesting. Is there a way to implement similar functionalities with vbRichClient5?  Reply With Quote

7. ## Re: [VB6] Simple SPH (smoothed particles hydrodynamics)

I'm not sure what you're asking.
The equations you posted are pretty much the same as what reexre posted that he is using in the first post.
Since he is using the same or similar equations and using vbrichclient to illustrate the implementation on a large number of particles, it you wanted to modify the equations slightly, then there shouldn't be any issue with using vbrichclient since that is what is already being done.  Reply With Quote

8. ## Re: [VB6] Simple SPH (smoothed particles hydrodynamics) Originally Posted by passel I'm not sure what you're asking.
The equations you posted are pretty much the same as what reexre posted that he is using in the first post.
Since he is using the same or similar equations and using vbrichclient to illustrate the implementation on a large number of particles, it you wanted to modify the equations slightly, then there shouldn't be any issue with using vbrichclient since that is what is already being done.
I mean how to draw math-equations with vbRichClient5 or pure VB6.  Reply With Quote

9. ## Re: [VB6] Simple SPH (smoothed particles hydrodynamics)

Ok, to replicate the graphs shown?
That should be easy enough with pure VB6, and I'm sure also vbRichClient5, although I haven't used vbRichClient.
I guess you can't see the whole equation for the Density Smooth.

With "pure VB6", you could just create a picturebox to use as the graph area, set the ScaleWidth and ScaleHeight to the size you want for your plot area and set the ScaleLeft and ScaleTop appropriately.

I guess looking at the web page, perhaps it is a little more complicated than that since you can click on the graph, and drag to scroll the graph vertically and horizontally to see more of the graph, so the plot's virtual size can be much larger than what can fit on the screen at any given time. And you can zoom in and out so the scale is modifiable. If you want to replicate the functionality of the desmos graphing interface, then that would be quite a bit of work, i.e. the display of point values along the curve with tooltip type windows, etc...

I'm sure it can be done in pure VB6 or other graphic library. The question is how much of the functionality are you looking to implement.  Reply With Quote

10. ## Re: [VB6] Simple SPH (smoothed particles hydrodynamics)

Yes, the workload of developing a general math equation editor is large.  Reply With Quote

11. ## Re: [VB6] Simple SPH (smoothed particles hydrodynamics)

SPH 3D
soon I will publish it.
after some refining.  Reply With Quote

12. ## Re: [VB6] Simple SPH (smoothed particles hydrodynamics)

Wow! Can you impl gravity centering to a point like a planet and possibly a moon?

I see that the liquid already has some volume so in this case this volume will fill up a (rotating) sphere.

cheers,
</wqw>  Reply With Quote

13. ## Re: [VB6] Simple SPH (smoothed particles hydrodynamics)

@wqweto

This is my new test.
I added an Attraction to Global Center of Mass
Thanks for the suggestion.
Quite near your Planets demo... but I don't think I'll go more further  Reply With Quote

14. ## Re: [VB6] Simple SPH (smoothed particles hydrodynamics)

This is extremely cool interactive demo! Both liquids don't mix like water and oil :-))

cheers,
</wqw>  Reply With Quote

15. ## Re: [VB6] Simple SPH (smoothed particles hydrodynamics)

3D version in post #1  Reply With Quote

16. ## Re: [VB6] Simple SPH (smoothed particles hydrodynamics)

3D Version on GitHub since today.

now 3D version (V.5) has an improved camera that can manage lines which have one of its points that goes behind the camera by using "near-clipping-plane" technique.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Featured