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