Jump to content
  • Advertisement
Sign in to follow this  
PAiNIC

Finite Difference Operators (div and curl)

This topic is 4761 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

To some it may seem like a basic question, but a search of the web has turned me up with nothing. I'm trying to work out how to write code for the divergence and curl operators on vector fields for use in a project and can't figure out how to implement them. Definition of Divergence: http://en.wikipedia.org/wiki/Divergence Definition of Curl: http://en.wikipedia.org/wiki/Curl If anyone can throw me some C/C++ code or pseudocode as to how these operators would be implemented, I assume as: div(float Fx, float Fy, Float Fz); curl(float Fx, float Fy, Float Fz); I would be eternally grateful

Share this post


Link to post
Share on other sites
Advertisement
Seeing as neither C nor C++ doesn't natively support algebraic manipulation of multivariate equations, I don't see how it would be possible to give C or C++ code for div or curl.

And even if it was possible, the result of div and curl are both functions. So the function signature of div and curl would looks something like:

struct vector3;

float (*)(vector3) div( (float (*)(vector3) );
vector3 (*)(vector3) curl ( (float (*)(vector3) );

Of course, that would require the runtime generation of functions, so that probably wouldn't be a working solution. Using function objects as input and output might give you something like:

boost::function<float (vector3)> div(boost::function(float (vector3)));
boost::function<vector3 (vector3)> curl(boost::function(float (vector3)));


You might be able to create a function object that use numerical methods to approximate div and curl by numerical approximations of partial differentials at a given point of evaluation. That works out by choosing a small stepsize h to perform the differential division by instead of taking the limit as h approaches 0.

Share this post


Link to post
Share on other sites
Quote:
You might be able to create a function object that use numerical methods to approximate div and curl by numerical approximations of partial differentials at a given point of evaluation. That works out by choosing a small stepsize h to perform the differential division by instead of taking the limit as h approaches 0. /EndQuote

Yep! That's what I need. Using a small stepsize to do the idfferential equation. An arbitrary small constant h would be perfect. It's the implementation of the differential eq'n using a small value of h that I can't understand. I get that Fx, Fy and Fz are values of a function at a specific point in the function field (in this case, velocity field, and Fx, Fy and Fz are velocity values at a point in the field). But what are dx, dy and dz?

Share this post


Link to post
Share on other sites
Quote:
Original post by PAiNIC
But what are dx, dy and dz?


in this context, thye are most probably the partial derivatives of the field youre dealing with.

once you have all partial derivatives at a point in your field, you should be able to obtain the divergence and curl quite easily using their mathematical definition.

suppose your vectorfield is given as a grid of vectors, you could evaluate partial derivatives at any point by looking at the surrouinding gridpoints.

say you want the ptderiv. in 2d at (2.5;3.6) with respect to x.

linearly interpolate the vectors between (2;3)/(2;4) and (3;3)/(3;4) to obtain (2;3.6) and (3;3.6). (draw it out that probably makes it easier to understand)

the difference between those two divided by the grid spacing will give you the* partial derivative of the vectorfield with respect to x.

*actually, not 'the', but 'a'. you could also use higher order interpolation and obtain a different result, but i dont think youd want to.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!