# Vector field for magnetic di-poles

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

## Recommended Posts

Hi,

Just registered and writing my first post in GameDev!!

[b]Problem Statement
[/b]I wanted to simulate the magnetic field lines (vectors, i.e, direction and intensity) in [color="#ff8c00"][i]OpenGL [/i][/color]using the [color="#ff8c00"][i]Line Integral Convolution[/i][/color] technique. Something like this "[url="http://www.mare.ee/indrek/ephi/"]http://www.mare.ee/indrek/ephi/[/url]".

[b]Questions[/b]
[list=1][*]Is LIC a library? If yes, where can I find one for VC++ 2010 ultimate?[*]An example/tutorial explaining the usage in 2D and 3D(if possible).[/list]
If I seem to sound naive and abstract in my talking please forgive me and provide me necessary pointers to start with!

Thanks,

##### Share on other sites
Moved to Maths and Physics, as the question is not related to OpenGL.

##### Share on other sites
I'm confused to what you actually want. LIC is a visualization technique. Do you want to create a simulation that constructs the vector field? Or have you got already got the vector field data and want to perform LIC to visualize it? Or both?

Assuming you are talking about the visualization aspect, finding an off-the-shelf LIC implementation might be hard due to varying grid types and data formats for the vector field data. Here is a link to some LIC C code (it's not OpenGL though). It might still be useful for you to get a grasp of the idea.

[url="http://www.zhanpingliu.org/research/flowvis/LIC/LIC_Source.htm"]LIC code[/url]

If this isn't helpful let me know and I will dig out my C++/OpenGL implementation of LIC for you. It's not a stand alone library but you can have the class source to see how I did it. It's also not the best implementation, as it was a quick hack, but the results looked good and it ran at a reasonable rate.

Does it have to be LIC? You could try something [url="http://www.win.tue.nl/~vanwijk/ibfv/"]IBFV[/url] (theres some example code on there too), which gives similar results but should be faster.

##### Share on other sites
If you're only interested in divergence-free flows like magnetic fields, why not construct streamfunctions and use marching squares to draw the level sets? This will give you nice global properties; e.g., integration error will never cause field lines to cross, etc.

##### Share on other sites
Thank you Unicron!! That was very informative!!

[quote name='_Unicron_' timestamp='1300366216' post='4786974']
I'm confused to what you actually want. LIC is a visualization technique. Do you want to create a simulation that constructs the vector field? Or have you got already got the vector field data and want to perform LIC to visualize it? Or both?
[/quote]
[color="#2f4f4f"]Let me start from the beginning.
[/color][list=1][*][color="#2f4f4f"]My requirement is to build a simple simulation of magnetic field lines between two or three bar magnets in OpenGL.[/color][*][color="#2f4f4f"]Also, if the magnets are too close to each other, based on their alignment, they should..
[/color][list][*][color="#2f4f4f"]attract, or[/color][*][color="#2f4f4f"]repel, and/or
[/color][*][color="#2f4f4f"]turn i.e. show some torque on its axis.
[/color][/list][*][color="#2f4f4f"]As a first step, I am searching for some implementation which has a plane (say xy-plane) with grid lines and each grid having an associated vector. These vectors will be later used when a magnet is placed on the plane to show the direction of the field and its intensity. It is here I came to know about LIC.[/color][/list][color="#2f4f4f"]Now, I would like to understand more about LIC from an already existing tutorial (if one exists). So, in essence, "I was asking how to create a simulation that constructs the vector field using the LIC technique".

[b]Question[/b]
From what you say, LIC is used only after we have a vector field in hand?? [/color]

[quote name='_Unicron_' timestamp='1300366216' post='4786974']
If this isn't helpful let me know and I will dig out my C++/OpenGL implementation of LIC for you. It's not a stand alone library but you can have the class source to see how I did it. It's also not the best implementation, as it was a quick hack, but the results looked good and it ran at a reasonable rate.
[/quote] [color="#2f4f4f"]Yes. Please do share your sample program. I will run it and see if it can be helpful.[/color]

[quote name='_Unicron_' timestamp='1300366216' post='4786974']
Does it have to be LIC? You could try something [url="http://www.win.tue.nl/%7Evanwijk/ibfv/"]IBFV[/url] (theres some example code on there too), which gives similar results but should be faster.
[/quote]
[color="#2f4f4f"]
Not specifically. Just trying different options. I am very new to graphics programming. So, do not know which is good or bad. Trying everything I come across. IBFV is good. I tried it on my system and it performs well.[/color]

##### Share on other sites
[quote name='Emergent' timestamp='1300377543' post='4787060']
If you're only interested in divergence-free flows like magnetic fields, why not construct streamfunctions and use marching squares to draw the level sets? This will give you nice global properties; e.g., integration error will never cause field lines to cross, etc.
[/quote]

[color="#2f4f4f"]Hmm.. Looks like this sounds too techy to me! Can you explain to me in detail on stream functions, marching squares, level sets etc. Any good material online? Pls refer my above post for my actual requirement in case you wanted more details. Thanks.
[/color]

##### Share on other sites
[b]Question[/b]
From what you say, LIC is used only after we have a vector field in hand?? [/color]
[color="#2F4F4F"][color="#000000"][/quote][/color][/color]

Yes, LIC is a visualization technique. It does not create a vector field. LIC takes as input a reference image (usually a noise texture) and a vector field. The reference image is basically then smeared along the vector field.

##### Share on other sites
A quick clarification -

You don't create a simulation "in" OpenGL, you create a simulation with a programming language such as C++ and "use" OpenGL to render a visual representation of the state of the simulation.

##### Share on other sites
[color="#2f4f4f"]Unicron, can you give me some ideas on continuing..
[/color][list=1][*][color="#2f4f4f"]My vector field idea is derived from [url="https://phet.colorado.edu/sims/faraday/faraday_en.jnlp"]here[/url]. Run this and you see a vector per grid.[/color][*][color="#2f4f4f"]If my plane has such a grid and respective vectors, I will control them to behave as I say.[/color][/list][color="#2f4f4f"]Give me some ideas for the above implementation. Let me also know your opinions. [/color]

##### Share on other sites
[color="#2f4f4f"]Hmm.. Looks like this sounds too techy to me! Can you explain to me in detail on stream functions, marching squares, level sets etc. Any good material online? Pls refer my above post for my actual requirement in case you wanted more details. Thanks.[/color]
[/quote]

Really, the word "streamfunction" is used in fluid dynamics; in electromagnetics it's called the "magnetic potential," so that's the phrase I should have used; either way, the math is the same.

You're working in 2d? The idea is that you use a scalar function 'phi' to describe the magnetic field 'B' in the following way:

J = [0 1; -1 0] is a 90-degree rotation matrix (here, I used a semicolon to separate rows of the matrix). In other words, you compute the gradient, rotate it 90 degrees (so now you have a vector pointing along the level set), and say that this is the magnetic field. The field lines are just the level sets of 'phi.'

You can draw the level sets, like I said earlier, using the Marching Cubes algorithm. It's described [url="http://en.wikipedia.org/wiki/Marching_squares"]here[/url].

All this leaves is how to actually compute 'phi.' The idea here is simple: Figure out how to compute one analytically for a single bar magnet; then add 'em up for all your bar magnets. I would model a bar magnet as a solenoid.

I guess the only thing I haven't explained in this post is exactly what the magnetic potential due to a solenoid is. For now, I'll leave that to you! :-)

##### Share on other sites
[font="Trebuchet MS"][size="2"]Thanks Emergent for the ideas, but I am afraid, again all these sound very new to me unfortunately.

I pity myself for neglecting physics and graphics in my school. Nevertheless, I have to complete my project I am working on ASAP as I am running out of time pretty quickly. Unavoidably, I have wasted a week searching for a [u]prototype app[/u] that nearly suites my requirement but in vain. I go very anxious these days.

At this point, Can I ask you to involve in some hardcore "OpenGL, GLUT + Physics + Math" discussions with me!!? You might have to adjust with me for a week(or two) probably. Consider this as my humble request. Thanks.

So, let me start with a questionnaire of all my little knowings..
[/size][/font][list][*][font="Trebuchet MS"][size="2"]I understand that C/C++ is the logic behind the scene and OpenGL is the front end for my visualization. Am I write?
Which one is it good to work on - C or C++? Specifically, VC(Win32) or VC++(MFC)?

Somewhere I've read that MFC is not so good!!?? In that case, developing a gaming app in simple C - with out OOPS - isn't going to be too hard?

[/size][/font][*][font="Trebuchet MS"][size="2"]Is there any specific design (pattern) we need to abide when developing a good graphics application for such interactions? Basically, my C++ classes and the graphics objects have to communicate and do the needful in a very reliable way!? Just a thought.

[/size][/font][*][font="Trebuchet MS"][size="2"]The approach in my mind (pls correct wherever required.)
[/size][/font][list=1][*][font="Trebuchet MS"][size="2"]In a 3D, create an XY plane(say 500 x 500) which can be rotated and viewed from any angle using mouse.[/size][/font][*][font="Trebuchet MS"][size="2"]Create an object "Bar Magnet1 (cuboid with N and S)" and place it on the above plane, say at origin - center of the screen.[/size][/font][*][font="Trebuchet MS"][size="2"]Now this magnet has got some properties like B, H, magnetic moment etc etc. (I need some help here on which properties really play an important role).[/size][/font][*][font="Trebuchet MS"][size="2"]Calculate the field intensity using differential equations. Which algorithm to be considered?
Okay, but what about the direction of the field - this is important as to show attraction or repulsion. How can we get the direction? This is just the angle wrt an imaginary line in the 2D plane, right?[/size][/font][*][font="Trebuchet MS"][size="2"]Now this field reduces with distance. To visualize this in the 3D picture, which is better to go for? Arrow plot or LIC and how to achieve that?

[/size][/font][/list][*][font="Trebuchet MS"][size="2"]Right now [i]gamedev [/i]is the ONLY hope to complete my project on time - atleast to get some idea on what to do. If you could point to relevant demos/tuts and help me connect the dots, that would be real help.[/size][/font][/list][font="Trebuchet MS"][size="2"]Thanks for all your help!![/size][/font]

##### Share on other sites
Is someone there to guide!!? OR ATLEAST POINT ME TO SOME GOOD TUTORIAL ON SIMULATING DYNAMICS. Whatever you know might turn out helpful to me.

##### Share on other sites
Ok, let's start simple then.

2.) Follow [url="http://www.lighthouse3d.com/opengl/glut/"]this tutorial[/url] . It will walk you through setting up a window and doing simple drawing using OpenGL.

3.) Write a function to draw a vector field that you have stored in an array. Forget the fancy stuff; just draw a bunch of arrows. Test it on a simple vector field, e.g. u=-y, v=x.

4.) Write a function to generate the magnetic field corresponding to a single, infinite wire perpendicular to the plane. It is,

B = (mu_0 I) /(2 pi ||r||^2) J r

where 'r' is the 2d displacement vector from the point where the wire intersects the plane, I is the current, mu_0 is the [url="http://en.wikipedia.org/wiki/Vacuum_permeability"]magnetic permeability constant[/url], and J = [0 1; -1 0] is a 90-degree rotation matrix. Test it using the function you produced in part #3.

5.) Write a function to generate the magnetic field due to a solenoid. To do this, just add up many of the fields that you figured out in part #4. [url="http://en.wikipedia.org/wiki/File:VFPt_Solenoid_correct2.svg"]This image[/url] shows you how. You are approximating your bar magnet by a solenoid, so this produces the field due to a single bar magnet.

6.) Write a function to generate the magnetic field due to a number of bar magnets. To do this, add up a number of the fields you got in part #5.

Ultimately, you're going to need to figure out C++, the basics of the OpenGL API, and a little bit of physics, to pull this off. If you need more resources... For the physics, my suggestion is to get a simple -- say, high-school- or college-freshman -level -- textbook. For C++, the tutorials [url="http://www.cprogramming.com/tutorial.html"]here[/url] are a good place to start. For OpenGL, you might also want to check out the tutorials [url="http://nehe.gamedev.net/"]here[/url]. Still, don't let this scare you; walk through the steps I listed, and you should be able to do this. And by the time you get to #6, you should be comfortable enough with what you're doing to direct yourself.

Good luck!

##### Share on other sites
[quote name='Emergent' timestamp='1300747795' post='4788853']
Ok, let's start simple then.

2.) Follow [url="http://www.lighthouse3d.com/opengl/glut/"]this tutorial[/url] . It will walk you through setting up a window and doing simple drawing using OpenGL.

3.) Write a function to draw a vector field that you have stored in an array. Forget the fancy stuff; just draw a bunch of arrows. Test it on a simple vector field, e.g. u=-y, v=x.

4.) Write a function to generate the magnetic field corresponding to a single, infinite wire perpendicular to the plane. It is,

B = (mu_0 I) /(2 pi ||r||^2) J r

where 'r' is the 2d displacement vector from the point where the wire intersects the plane, I is the current, mu_0 is the [url="http://en.wikipedia.org/wiki/Vacuum_permeability"]magnetic permeability constant[/url], and J = [0 1; -1 0] is a 90-degree rotation matrix. Test it using the function you produced in part #3.

5.) Write a function to generate the magnetic field due to a solenoid. To do this, just add up many of the fields that you figured out in part #4. [url="http://en.wikipedia.org/wiki/File:VFPt_Solenoid_correct2.svg"]This image[/url] shows you how. You are approximating your bar magnet by a solenoid, so this produces the field due to a single bar magnet.

6.) Write a function to generate the magnetic field due to a number of bar magnets. To do this, add up a number of the fields you got in part #5.

Ultimately, you're going to need to figure out C++, the basics of the OpenGL API, and a little bit of physics, to pull this off. If you need more resources... For the physics, my suggestion is to get a simple -- say, high-school- or college-freshman -level -- textbook. For C++, the tutorials [url="http://www.cprogramming.com/tutorial.html"]here[/url] are a good place to start. For OpenGL, you might also want to check out the tutorials [url="http://nehe.gamedev.net/"]here[/url]. Still, don't let this scare you; walk through the steps I listed, and you should be able to do this. And by the time you get to #6, you should be comfortable enough with what you're doing to direct yourself.

Good luck!
[/quote]

Great and patient reply .. As a fellow member , i would like to thank u
gamedev is really gonna help everyone.. i suggest it for every graphics programmer.

##### Share on other sites
It really wouldn't hurt to read a textbook on Differential Equations.

Also, you might want to see Chapter 20: Scientific Visualization in [url="http://www.amazon.com/Advanced-Graphics-Programming-Kaufmann-Computer/dp/1558606599"]Advanced Graphics Programming Using OpenGL[/url]. The book is a goldmine.

##### Share on other sites
[quote name='Emergent' timestamp='1300747795' post='4788853']
Ok, let's start simple then.

2.) Follow [url="http://www.lighthouse3d.com/opengl/glut/"]this tutorial[/url] . It will walk you through setting up a window and doing simple drawing using OpenGL.

3.) Write a function to draw a vector field that you have stored in an array. Forget the fancy stuff; just draw a bunch of arrows. Test it on a simple vector field, e.g. u=-y, v=x.

4.) Write a function to generate the magnetic field corresponding to a single, infinite wire perpendicular to the plane. It is,

B = (mu_0 I) /(2 pi ||r||^2) J r

where 'r' is the 2d displacement vector from the point where the wire intersects the plane, I is the current, mu_0 is the [url="http://en.wikipedia.org/wiki/Vacuum_permeability"]magnetic permeability constant[/url], and J = [0 1; -1 0] is a 90-degree rotation matrix. Test it using the function you produced in part #3.

5.) Write a function to generate the magnetic field due to a solenoid. To do this, just add up many of the fields that you figured out in part #4. [url="http://en.wikipedia.org/wiki/File:VFPt_Solenoid_correct2.svg"]This image[/url] shows you how. You are approximating your bar magnet by a solenoid, so this produces the field due to a single bar magnet.

6.) Write a function to generate the magnetic field due to a number of bar magnets. To do this, add up a number of the fields you got in part #5.

Ultimately, you're going to need to figure out C++, the basics of the OpenGL API, and a little bit of physics, to pull this off. If you need more resources... For the physics, my suggestion is to get a simple -- say, high-school- or college-freshman -level -- textbook. For C++, the tutorials [url="http://www.cprogramming.com/tutorial.html"]here[/url] are a good place to start. For OpenGL, you might also want to check out the tutorials [url="http://nehe.gamedev.net/"]here[/url]. Still, don't let this scare you; walk through the steps I listed, and you should be able to do this. And by the time you get to #6, you should be comfortable enough with what you're doing to direct yourself.

Good luck!
[/quote]

Thank you Emergent for all the details! I am seeing improvement myself. So far have been able to come up with a very simple vector field like the one attached. You are really cool!! Thanks.

I will let you know if I am stuck somewhere.

[attachment=1723:simple_vector_field.PNG]

##### Share on other sites
[quote name='Emergent' timestamp='1300747795' post='4788853']
Good luck!
[/quote]

Thank you Emergent for all the details! I am seeing improvement myself. So far have been able to come up with a very simple vector field like the one attached. You are really cool!! Thanks.

I will let you know if I am stuck somewhere.

[attachment=1723:simple_vector_field.PNG]
[/quote]

Hi Emergent,

Hope you are doing good!!
[list=1][*]I have modeled a pseudo magnetic field in OpenGL, which looks similar to this [img]http://i.stack.imgur.com/la9Pt.jpg[/img][*]The vectors are discretely calculated by retrieving appropriate values from a data base which has the Bx, By values of a magnet (modeled in FEMM).[*]Now the question is how can I calculate the force of magnet A on magnet B and vice versa?? (Appreciate if some idea on Torque also is given)..[/list] I have got all the values like, cross section of the magnets, the separation b/w the magnets, the angle difference between the two magnets, (Bx, By) at any point etc.

Google dint help me. So, posting here and [i]SO[/i]..

##### Share on other sites
Now the question is how can I calculate the force of magnet A on magnet B and vice versa?? (Appreciate if some idea on Torque also is given).. I have got all the values like, cross section of the magnets, the separation b/w the magnets, the angle difference between the two magnets, (Bx, By) at any point etc.
[/quote]

To answer your question, take a look at [url="http://en.wikipedia.org/wiki/Magnetic_field#Force_on_current-carrying_wire"]this Wikipedia article[/url] . The punchline is the equation

F = I (L x B)

which relates, for the three-dimensional case,

F, the force vector acting on the wire
L, the displacement vector from the start of the wire to the end,
B, the magnetic field vector where the wire is, and
I, the current carried by the wire (a scalar).

The idea will be this: You're approximating your magnets as solenoids -- collections of many wires, which in this case are perpendicular to the plane -- so what you can do is just compute the force vector acting on each of these wires. The sum of all the forces acting on the wires of the solenoid is the net force on the magnet. Since you know the displacement of each wire from the magnet's center of mass, you can also compute a torque due to each (Remember [url="http://en.wikipedia.org/wiki/Torque"]tau=rxF[/url]), and add these up to compute the net torque on the magnet about its center of mass.

Finally, the force equation I gave you is 3d, and your simulation is 2d, so we'll need to interpret the above equation appropriately. This means,

L = (0,0,1)
or
L = (0,0,-1)

depending on whether the wire is going into or out of the plane, and, if your magnetic field at the wire is the 2d vector (bx, by), then

B = (bx, by, 0) .

The vector F that you get will lie in the plane; i.e., it will have the form,

F = (fx, fy, 0)

so you will be able to forget the third component and just consider the force vector (fx, fy).

To summarize, what you're going to want to do is,

...For each magnet M
...{
........Force_on_M = (0,0)
........Torque_on_M = 0
........For each wire W comprising the solenoid you're using to approximate M
........{
............Compute the force on W using the equation F = I (L x B)
............and the torque on the magnet tau=rxF due to it.
............Force_on_M += F
............Torque_on_M += tau
........}
...}

Finally, let me mention the approximations I'm making:
1.) I'm assuming all the fields are slowly-changing. In real life, a changing magnetic field creates an electric field (which, if changing, creates a magnetic field, and so on); this is what constitutes electromagnetic waves. This simulation will not deal with that.
2.) I'm being a little sloppy with the third dimension.

Nevertheless, I think all the basic ideas are here.

##### Share on other sites
[quote name='Emergent' timestamp='1316375936' post='4863118']
Nevertheless, I think all the basic ideas are here.
[/quote]

Oh my... you are my HERO!! Thanks a lot Emmy!
Will try out and let you know ..