Jekyll2020-08-24T14:49:52+00:00https://pcrumley.github.io/feed.xmlVoorwerpjesPersonal website of Patrick CrumleyPatrick CrumleyPlotting Magnetic Field Lines in 2D? Here’s what you need to know.2020-08-23T00:00:00+00:002020-08-23T00:00:00+00:00https://pcrumley.github.io/blog/magnetic-field-lines<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.10.2/dist/katex.min.css" integrity="sha384-yFRtMMDnQtDRO8rLpMIKrtPCD5jdktao2TV19YiZYWMDkUR5GQZR/NOVTdquEx1j" crossorigin="anonymous" />
<script defer="" src="https://cdn.jsdelivr.net/npm/katex@0.10.2/dist/katex.min.js" integrity="sha384-9Nhn55MVVN0/4OFx7EE5kpFBPsEMZxKTCnA+4fqDmg12eCTqGi6+BB2LjY8brQxJ" crossorigin="anonymous"></script>
<script defer="" src="https://cdn.jsdelivr.net/npm/katex@0.10.2/dist/contrib/auto-render.min.js" integrity="sha384-kWPLUVMOks5AQFrykwIup5lo0m3iMkkHrD0uJ4H5cjeGihAutqP0yW0J6dpFiVkI" crossorigin="anonymous" onload="renderMathInElement(document.body);"></script>
<p>One of the major difficulties of understanding plasmas is just getting a handle on
what the magnetic field is doing. If you’re lucky enough to be running
simulations in 2D, you’ll have the value of the magnetic field at every point on
your grid, but it still can be hard to understand the geometry of it all.
That’s where <a href="https://en.wikipedia.org/wiki/Magnetic_field">field lines</a> come
in.</p>
<p>There’s this trick that is passed around by word of mouth amongst theoretical
plasma physicists, but I can’t find it online or in print anywhere. Hopefully
the solid use of SEO in my blog title will help it become more widely known
(Don’t @ me, <a href="https://www.nytimes.com/column/the-daily">Michael Barbaro</a>).
The trick makes calculating field lines in 2D very fast, but as you’ll
see, it only works for magnetic or other divergence-free 2D vector fields.</p>
<p>In 2D, the field lines in the \(xy\) plane can be calculated as lines of
equipotential of \(A_z\). The trick is: <strong>you can calculate \(A_z\) at any
point in the grid by integrating \(-B_y\) along \(x\), and \(B_x\) along
\(y\).</strong></p>
<p>If you want to just <a href="https://en.wikipedia.org/wiki/Argument_from_authority">believe me</a>
and skip the vector calculus, scroll to the bottom for a simple python snippet
showing how to use this trick efficiently. Otherwise, let’s take a little
detour to prove we’ve made a valid construction of \(A_z\).</p>
<h3 id="simple-calculation-of-the-of-the-magnetic-vector-potential-a_z">Simple calculation of the of the magnetic vector potential, \(A_z\).</h3>
<p>Recall Maxwell’s EQ’s.
\[ \mathbf{E} = \nabla V - \frac{\partial \mathbf{A}}{\partial t} \]
\[ \mathbf{B} = \nabla \times \mathbf{A} \]</p>
<p>We are free to choose any \(A\) that satisfies the above equation, due to gauge
freedom. Calculate curl of \(A\)
\[ \nabla \times \mathbf{A} =
\hat{i}\left[\frac{\partial A_z}{\partial y} - \frac{\partial A_y}{\partial z}\right]
-\hat{j}\left[\frac{\partial A_z}{\partial x} - \frac{\partial A_x}{\partial z}\right]
+\hat{k}\left[\frac{\partial A_y}{\partial x} - \frac{\partial A_x}{\partial y}\right]
\]
Now because the simulation is \(2D\), we know that
\( \frac{\partial}{\partial z} \) is zero for all quantities. Furthermore,
we only need to know \(A_z\), so we just need to construct any continuous and
infinitely differentiable \(A_z\) that is a valid solution to
the following set of partial differential equations.
\[ B_x = \frac{\partial A_z}{\partial y} \]
\[ B_y = -\frac{\partial A_z}{\partial x} \]
One choice of \(A_z\) that satisfies the above equations is the following:
\[ A_z(x,y) = - \int_0^{x}{B_y(x’,0)\ dx’} + \int_0^{y}{B_x(x,y’)\ dy’}.\]</p>
<p>Hopefully you can
<a href="https://en.wikipedia.org/wiki/Fundamental_theorem_of_calculus">convince yourself</a>
that \(\frac{\partial A_z}{\partial y} = B_x \), So we’ll just show the algebra for
\[B_y = -\frac{\partial A_z}{\partial x} = B_y(x,0) - \int_0^y{ \frac{\partial B_x}{\partial x}\ dy’}. \]</p>
<p>Because \(\nabla \cdot B = 0\) and fields are 2D;
\(\frac{\partial B_x}{\partial x} = -\frac{\partial B_y}{\partial y}.\)
Substituting this in…
\[-\frac{\partial A_z}{\partial x} = B_y(x,0) + \int_0^y{ \frac{\partial B_y}{\partial y}\ dy’} = B_y(x,0) + B_y(x, y) - B_y(x,0) \]
or
\[B_y = -\frac{\partial A_z}{\partial x}.\]</p>
<p>Which is precisely what we needed to show, so we have proven our choice of
\(A_z\) is valid. However, you explicitly use the fact the field is
solenoidal to calculate the field lines, so this trick will not work for
electric fields. You’ll need to do the full Helmholtz decomposition or trace
them out as streamlines for that.</p>
<p>Let’s say you have two 2D numpy arrays, <code class="language-plaintext highlighter-rouge">bx</code> and <code class="language-plaintext highlighter-rouge">by</code>, corresponding
to the \(x\) and \(y\) components of the field respectively. With some
Numpy magic and disregarding units, \(A_z\) can be calculated and field lines
plotted with:</p>
<div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code> <span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="n">np</span>
<span class="kn">import</span> <span class="nn">matplotlib.pyplot</span> <span class="k">as</span> <span class="n">plt</span>
<span class="n">bydx</span> <span class="o">=</span> <span class="o">-</span><span class="n">np</span><span class="p">.</span><span class="n">cumsum</span><span class="p">(</span><span class="n">by</span><span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="p">:])</span>
<span class="n">bxdy</span> <span class="o">=</span> <span class="n">np</span><span class="p">.</span><span class="n">cumsum</span><span class="p">(</span><span class="n">bx</span><span class="p">[:,</span> <span class="p">:],</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="n">Az</span> <span class="o">=</span> <span class="n">bydx</span><span class="p">[</span><span class="n">np</span><span class="p">.</span><span class="n">newaxis</span><span class="p">,</span> <span class="p">:]</span> <span class="o">+</span> <span class="n">bxdy</span>
<span class="n">plt</span><span class="p">.</span><span class="n">contour</span><span class="p">(</span><span class="n">Az</span><span class="p">)</span>
</code></pre></div></div>
<p>Isn’t it beautiful to program with multidimensional arrays? Thank you,
<a href="https://en.wikipedia.org/wiki/APL_(programming_language)">Kenneth E. Iverson</a></p>Patrick CrumleyHello, there!2020-08-21T00:00:00+00:002020-08-21T00:00:00+00:00https://pcrumley.github.io/blog/hello-there<p>Hi. I’m Patrick. Right now I work as a <a href="https://web.astro.princeton.edu/people/patrick-crumley">computational
astrophysicist</a>, but I
am also in the middle of a career transition. I am starting a blog to find ways
to connect in this new normal, write down my thoughts, and document things about
my work in and outside of academia that I’m proud of or think might help others.</p>
<p>I’m choosing to call my blog <em>Voorwerpjes</em>, which is a Dutch word that
translates into ‘little things’. But it is also the name of the class of the
astronomical object pictured below, <a href="https://en.wikipedia.org/wiki/Hanny%27s_Voorwerp">Hanny’s
Voorwerp</a>. Voorwerpjes are
echoes of supermassive black holes, an anomalous astronomical object discovered by
a citizen scientist in the Netherlands, Hanny van Arkel. She was volunteering as
part of the <a href="https://www.zooniverse.org/projects/zookeeper/galaxy-zoo/">Galaxy
Zoo</a> project when
she saw something new. It turned out to be a
whole class of objects no one had seen before.</p>
<p>Galaxy Zoo is a wonderful project that has lots volunteers who donate their time to
categorize hundreds of thousands of galaxies based on their shape. It always
gives me a fuzzy feeling to think about—the project combines the best parts of big
data and technology; using the internet to build community and collaboration between
experts and non-experts.</p>
<p>I hope to fill up this space with little things.</p>
<p><img src="/assets/images/hannys_voorwerp.jpg" alt="Hanny's Voorwerp, credit to NASA" />
Credit: NASA and the Galaxy Zoo Team</p>Patrick CrumleyHi. I’m Patrick. Right now I work as a computational astrophysicist, but I am also in the middle of a career transition. I am starting a blog to find ways to connect in this new normal, write down my thoughts, and document things about my work in and outside of academia that I’m proud of or think might help others.