# Finite Difference Operators (div and curl)

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

## 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 on other sites
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 on other sites
He did say finite difference...

##### 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 on other sites
Quote:
 Original post by PAiNICBut 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 on other sites
Thanks Eelco, exactly what I needed!!!

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 12
• 30
• 9
• 16
• 12