Jump to content

  • Log In with Google      Sign In   
  • Create Account


Vector field for magnetic di-poles


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
18 replies to this topic

#1 Advaita   Members   -  Reputation: 100

Like
0Likes
Like

Posted 16 March 2011 - 11:21 PM

Hi,

Just registered and writing my first post in GameDev!! :rolleyes:

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

Questions
  • 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).
If I seem to sound naive and abstract in my talking please forgive me and provide me necessary pointers to start with! :(

Thanks,
Aditya

Sponsor:

#2 Brother Bob   Moderators   -  Reputation: 7779

Like
0Likes
Like

Posted 17 March 2011 - 03:40 AM

Moved to Maths and Physics, as the question is not related to OpenGL.

#3 cstony   Members   -  Reputation: 300

Like
1Likes
Like

Posted 17 March 2011 - 06:50 AM

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.

LIC code

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 IBFV (theres some example code on there too), which gives similar results but should be faster.

#4 Emergent   Members   -  Reputation: 967

Like
0Likes
Like

Posted 17 March 2011 - 09:59 AM

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.

#5 Advaita   Members   -  Reputation: 100

Like
0Likes
Like

Posted 17 March 2011 - 10:37 AM

Thank you Unicron!! That was very informative!!

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?

Let me start from the beginning.
  • My requirement is to build a simple simulation of magnetic field lines between two or three bar magnets in OpenGL.
  • Also, if the magnets are too close to each other, based on their alignment, they should..
    • attract, or
    • repel, and/or
    • turn i.e. show some torque on its axis.
  • 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.
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".

Question
From what you say, LIC is used only after we have a vector field in hand??


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.

Yes. Please do share your sample program. I will run it and see if it can be helpful.

Does it have to be LIC? You could try something IBFV (theres some example code on there too), which gives similar results but should be faster.


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.


#6 Advaita   Members   -  Reputation: 100

Like
0Likes
Like

Posted 17 March 2011 - 10:44 AM

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.


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.


#7 cstony   Members   -  Reputation: 300

Like
0Likes
Like

Posted 17 March 2011 - 11:33 AM


Question
From what you say, LIC is used only after we have a vector field in hand??



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.

#8 WavyVirus   Members   -  Reputation: 735

Like
0Likes
Like

Posted 17 March 2011 - 01:47 PM

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.




#9 Advaita   Members   -  Reputation: 100

Like
0Likes
Like

Posted 18 March 2011 - 09:20 AM

Unicron, can you give me some ideas on continuing..
  • My vector field idea is derived from here. Run this and you see a vector per grid.
  • If my plane has such a grid and respective vectors, I will control them to behave as I say.
Give me some ideas for the above implementation. Let me also know your opinions.

#10 Emergent   Members   -  Reputation: 967

Like
0Likes
Like

Posted 18 March 2011 - 10:34 AM

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.


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:

B = J grad phi

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 here.

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! :-)

#11 Advaita   Members   -  Reputation: 100

Like
0Likes
Like

Posted 20 March 2011 - 08:02 PM

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 prototype app 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..
  • 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?

  • 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.

  • The approach in my mind (pls correct wherever required.)
    • In a 3D, create an XY plane(say 500 x 500) which can be rotated and viewed from any angle using mouse.
    • Create an object "Bar Magnet1 (cuboid with N and S)" and place it on the above plane, say at origin - center of the screen.
    • 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).
    • 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?
    • 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?

  • Right now gamedev 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.
Thanks for all your help!!

#12 Advaita   Members   -  Reputation: 100

Like
0Likes
Like

Posted 21 March 2011 - 01:56 PM

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.

#13 Emergent   Members   -  Reputation: 967

Like
1Likes
Like

Posted 21 March 2011 - 04:49 PM

Ok, let's start simple then.

My advice:

1.) Install Visual C++ Express.

2.) Follow this tutorial . 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 magnetic permeability constant, 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. This image 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 here are a good place to start. For OpenGL, you might also want to check out the tutorials here. 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!

#14 krishna teja   Members   -  Reputation: 100

Like
0Likes
Like

Posted 21 March 2011 - 10:09 PM

Ok, let's start simple then.

My advice:

1.) Install Visual C++ Express.

2.) Follow this tutorial . 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 magnetic permeability constant, 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. This image 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 here are a good place to start. For OpenGL, you might also want to check out the tutorials here. 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!


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.

#15 taby   Members   -  Reputation: 335

Like
0Likes
Like

Posted 21 March 2011 - 11:09 PM

It really wouldn't hurt to read a textbook on Differential Equations.

Also, you might want to see Chapter 20: Scientific Visualization in Advanced Graphics Programming Using OpenGL. The book is a goldmine.

#16 Advaita   Members   -  Reputation: 100

Like
0Likes
Like

Posted 26 March 2011 - 09:30 PM

Ok, let's start simple then.

My advice:

1.) Install Visual C++ Express.

2.) Follow this tutorial . 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 magnetic permeability constant, 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. This image 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 here are a good place to start. For OpenGL, you might also want to check out the tutorials here. 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!


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.

simple_vector_field.PNG

#17 Advaita   Members   -  Reputation: 100

Like
0Likes
Like

Posted 18 September 2011 - 08:51 AM


Good luck!


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.

simple_vector_field.PNG


Hi Emergent,

Hope you are doing good!!
  • I have modeled a pseudo magnetic field in OpenGL, which looks similar to this Posted Image
  • 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)..
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 SO..

#18 Emergent   Members   -  Reputation: 967

Like
0Likes
Like

Posted 18 September 2011 - 01:58 PM

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.


Glad to see your project has been moving along successfully.

To answer your question, take a look at this Wikipedia article . 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 tau=rxF), 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.

#19 Advaita   Members   -  Reputation: 100

Like
0Likes
Like

Posted 18 September 2011 - 02:42 PM

Nevertheless, I think all the basic ideas are here.


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




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS