On touch’s use of futimes() and a filesystem bug.

February 25, 2015 C/C++ development and debugging. , , , , ,

In my last post, Why does touch include a utimensat() syscall?, I’d pointed out that strace reports an utimensat syscall from touch, and noted that this appeared to have the effect of clearing the subsecond portion of the files mtime.

It turns out that strace was slightly lying, and we actually have a call to futimens( fd, NULL ).  I was able to see that by debugging into coreutils’s touch.c and see what it is doing.  The purpose of this futimens() syscall is supposed to be to set the time to the current time.  It appears that clearcase V8 + MVFS + futimes() doesn’t respect the microsecond granular times that were implemented in V8.   This API, as currently implemented will set the file’s mtime to the current time, but only respects the seconds portion of that time.

With clearcase V8 MVFS view private files having the capability for subsecond granularity, the end result is that this pushes the modification time backwards if you get unlucky enough to execute the futimes() in the same second as the file creation.  That is almost always.  It can be seen to work correctly if you put a long enough sleep in the code between the initial creation point for one file, and the “touch” of the second.  Here’s a bit of standalone code that illustrates:

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <getopt.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>

int main( int argc, char ** argv )
{
   int c                = 0 ;
   long sleepTimeUsec    = 10000 ;
   int fd1                = -1 ;
   int fd2                = -1 ;
   char * err             = NULL ;
   int rc                = 0 ;
   struct timespec ts = { 0, 0 } ;

   while ( (c = getopt( argc, argv, "s:" )) != EOF )
   {
      if ( c == 's' )
      {
         sleepTimeUsec = strtol( optarg, &err, 10 ) ;
         if ( err[0] )
         {
            fprintf( stderr, "unexpected input in -s parameter '%s'\n",
                     optarg ) ;
            return 1 ;
         }
      }
   }

   unlink( "touchFirst" ) ;
   unlink( "touchSecond" ) ;

   fd1 = open( "touchFirst", O_CREAT ) ;
   if ( -1 == fd1 )
   {
      fprintf( stderr, "create: 'touchFirst' failed: %d (%s)\n", 
               errno, strerror( errno ) ) ;
      return 2 ;
   }
   close( fd1 ) ;

   fd2 = open( "touchSecond", O_CREAT ) ;
   if ( -1 == fd2 )
   {
      fprintf( stderr, "create: 'touchSecond' failed: %d (%s)\n", 
               errno, strerror( errno ) ) ;
      return 3 ;
   }

   ts.tv_sec = sleepTimeUsec / 1000000 ;
   ts.tv_nsec = (sleepTimeUsec % 1000000)*1000 ;
   rc = nanosleep( &ts, NULL ) ;
   if ( -1 == rc )
   {
      fprintf( stderr, "nanosleep(%lu,%lu) failed: %d (%s)\n", 
               (unsigned long)ts.tv_sec, (unsigned long)ts.tv_nsec,
               errno, strerror( errno ) ) ;
      return 4 ;
   }

   rc = futimens( fd2, NULL ) ;
   if ( -1 == rc )
   {
      fprintf( stderr, "futimens failed: %d (%s)\n", 
               errno, strerror( errno ) ) ;
      return 5 ;
   }

   close( fd2 ) ;

   return 0 ;
}

Here’s a pair of calls that illustrate the bug.

/vbs/engn/t2> a.out -s 500000 ;  ls --full-time touch*
-r-x--xr-- 1 peeterj pdxdb2 0 2015-02-25 23:16:36.498049000 -0500 touchFirst
-r-x--xr-- 1 peeterj pdxdb2 0 2015-02-25 23:16:36.000000000 -0500 touchSecond
/vbs/engn/t2> a.out -s 500000 ;  ls --full-time touch*
-r-x--xr-- 1 peeterj pdxdb2 0 2015-02-25 23:16:38.900498000 -0500 touchFirst
-r-x--xr-- 1 peeterj pdxdb2 0 2015-02-25 23:16:39.000000000 -0500 touchSecond

The first is the buggy call, and the time goes backwards despite a half second sleep. The second call is okay, because 0.9+0.5 of a second ends up in the next second, so the second “touched” file has a timestamp after the creation of the second file (as expected.)

Why does touch include a utimensat() syscall?

February 25, 2015 perl and general scripting hackery , , , , , , , , ,

I’m seeing odd time sequencing of files when using clearcase version 8 dynamic views, which makes me wonder about an aspect of the (gnu) touch command. Running:

> cat u
rm -f touchedEarlier touchedLater

perl -e "open (F, '> touchedEarlier') || die"
touch touchedLater

ls --full-time touchedEarlier touchedLater

produces:

> ./u
-rw-r--r-- 1 peeterj pdxdb2 0 2015-02-25 11:42:05.833044000 -0500 touchedEarlier
-rw-r--r-- 1 peeterj pdxdb2 0 2015-02-25 11:42:05.000000000 -0500 touchedLater

Notice that the file that is touched by doing a perl “open” ends up with a later time, despite the fact that it was done logically earlier than the touch.

Running this command outside of a clearcase dynamic view shows zeros only in the subsecond times (also the behaviour of clearcase V7). Needless to say, this difference in file times from their creation sequence wreaks havoc on make.

I was curious how the two touch methods differed, and stracing them shows that the touch differs by including a utimesat() syscall. The perl touch is:

open(“touchedEarlier”, O_WRONLY|O_CREAT|O_TRUNC, 0666) = 3
ioctl(3, SNDCTL_TMR_TIMEBASE or TCGETS, 0x7fff29cd29f0) = -1 ENOTTY (Inappropriate ioctl for device)
lseek(3, 0, SEEK_CUR) = 0
fstat(3, {st_mode=S_IFREG|0644, st_size=0, …}) = 0
fcntl(3, F_SETFD, FD_CLOEXEC) = 0
close(3) = 0
exit_group(0) = ?

whereas the touch command has:

open(“touchedLater”, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK, 0666) = 3
dup2(3, 0) = 0
close(3) = 0
dup2(0, 0) = 0
utimensat(0, NULL, NULL, 0) = 0
close(0) = 0
close(1) = 0
close(2) = 0
exit_group(0) = ?

It appears that the touch command explicitly zeros the subsecond portion of the files’ timestamp.

I also see that perl’s File::Touch module does the same thing, but uses a different mechanism. I see the following in a strace of such a Touch() call:

stat(“xxyyzz”, 0x656060)                = -1 ENOENT (No such file or directory)
open(“xxyyzz”, O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK, 0666) = 3
ioctl(3, SNDCTL_TMR_TIMEBASE or TCGETS, 0x7fff16bb3c60) = -1 ENOTTY (Inappropriate ioctl for device)
lseek(3, 0, SEEK_CUR)                   = 0
fstat(3, {st_mode=S_IFREG|0644, st_size=0, …}) = 0
fcntl(3, F_SETFD, FD_CLOEXEC)           = 0
close(3)                                = 0
utimes(“xxyyzz”, {{1424884553, 0}, {1424884553, 0}}) = 0

I am very curious why touch and perl’s File::Touch() both explicitly zero the subsecond modification time for the file (using utimensat() or utimes() syscalls)?

Maxwell’s equations in tensor form with magnetic sources

February 22, 2015 ece1229 , , , , , , , , , , , , , , , , , , , , , , , , ,

[Click here for a PDF of this post with nicer formatting]

Following the principle that one should always relate new formalisms to things previously learned, I’d like to know what Maxwell’s equations look like in tensor form when magnetic sources are included. As a verification that the previous Geometric Algebra form of Maxwell’s equation that includes magnetic sources is correct, I’ll start with the GA form of Maxwell’s equation, find the tensor form, and then verify that the vector form of Maxwell’s equations can be recovered from the tensor form.

Tensor form

With four-vector potential \( A \), and bivector electromagnetic field \( F = \grad \wedge A \), the GA form of Maxwell’s equation is

\begin{equation}\label{eqn:gaMagneticSourcesToTensorToVector:20}
\grad F = \frac{J}{\epsilon_0 c} + M I.
\end{equation}

The left hand side can be unpacked into vector and trivector terms \( \grad F = \grad \cdot F + \grad \wedge F \), which happens to also separate the sources nicely as a side effect

\begin{equation}\label{eqn:gaMagneticSourcesToTensorToVector:60}
\grad \cdot F = \frac{J}{\epsilon_0 c}
\end{equation}
\begin{equation}\label{eqn:gaMagneticSourcesToTensorToVector:80}
\grad \wedge F = M I.
\end{equation}

The electric source equation can be unpacked into tensor form by dotting with the four vector basis vectors. With the usual definition \( F^{\alpha \beta} = \partial^\alpha A^\beta – \partial^\beta A^\alpha \), that is

\begin{equation}\label{eqn:gaMagneticSourcesToTensorToVector:100}
\begin{aligned}
\gamma^\mu \cdot \lr{ \grad \cdot F }
&=
\gamma^\mu \cdot \lr{ \grad \cdot \lr{ \grad \wedge A } } \\
&=
\gamma^\mu \cdot \lr{ \gamma^\nu \partial_\nu \cdot
\lr{ \gamma_\alpha \partial^\alpha \wedge \gamma_\beta A^\beta } } \\
&=
\gamma^\mu \cdot \lr{ \gamma^\nu \cdot \lr{ \gamma_\alpha \wedge \gamma_\beta
} } \partial_\nu \partial^\alpha A^\beta \\
&=
\inv{2}
\gamma^\mu \cdot \lr{ \gamma^\nu \cdot \lr{ \gamma_\alpha \wedge \gamma_\beta } }
\partial_\nu F^{\alpha \beta} \\
&=
\inv{2} \delta^{\nu \mu}_{[\alpha \beta]} \partial_\nu F^{\alpha \beta} \\
&=
\inv{2} \partial_\nu F^{\nu \mu}

\inv{2} \partial_\nu F^{\mu \nu} \\
&=
\partial_\nu F^{\nu \mu}.
\end{aligned}
\end{equation}

So the first tensor equation is

\begin{equation}\label{eqn:gaMagneticSourcesToTensorToVector:120}
\boxed{
\partial_\nu F^{\nu \mu} = \inv{c \epsilon_0} J^\mu.
}
\end{equation}

To unpack the magnetic source portion of Maxwell’s equation, put it first into dual form, so that it has four vectors on each side

\begin{equation}\label{eqn:gaMagneticSourcesToTensorToVector:140}
\begin{aligned}
M
&= – \lr{ \grad \wedge F} I \\
&= -\frac{1}{2} \lr{ \grad F + F \grad } I \\
&= -\frac{1}{2} \lr{ \grad F I – F I \grad } \\
&= – \grad \cdot \lr{ F I }.
\end{aligned}
\end{equation}

Dotting with \( \gamma^\mu \) gives

\begin{equation}\label{eqn:gaMagneticSourcesToTensorToVector:160}
\begin{aligned}
M^\mu
&= \gamma^\mu \cdot \lr{ \grad \cdot \lr{ – F I } } \\
&= \gamma^\mu \cdot \lr{ \gamma^\nu \partial_\nu \cdot \lr{ -\frac{1}{2}
\gamma^\alpha \wedge \gamma^\beta I F_{\alpha \beta} } } \\
&= -\inv{2}
\gpgradezero{
\gamma^\mu \cdot \lr{ \gamma^\nu \cdot \lr{ \gamma^\alpha \wedge \gamma^\beta I } }
}
\partial_\nu F_{\alpha \beta}.
\end{aligned}
\end{equation}

This scalar grade selection is a complete antisymmetrization of the indexes

\begin{equation}\label{eqn:gaMagneticSourcesToTensorToVector:180}
\begin{aligned}
\gpgradezero{
\gamma^\mu \cdot \lr{ \gamma^\nu \cdot \lr{ \gamma^\alpha \wedge \gamma^\beta I } }
}
&=
\gpgradezero{
\gamma^\mu \cdot \lr{ \gamma^\nu \cdot \lr{
\gamma^\alpha \gamma^\beta
\gamma_0 \gamma_1 \gamma_2 \gamma_3
} }
} \\
&=
\gpgradezero{
\gamma_0 \gamma_1 \gamma_2 \gamma_3
\gamma^\mu \gamma^\nu \gamma^\alpha \gamma^\beta
} \\
&=
\delta^{\mu \nu \alpha \beta}_{3 2 1 0} \\
&=
\epsilon^{\mu \nu \alpha \beta },
\end{aligned}
\end{equation}

so the magnetic source portion of Maxwell’s equation, in tensor form, is

\begin{equation}\label{eqn:gaMagneticSourcesToTensorToVector:200}
\boxed{
\inv{2} \epsilon^{\nu \alpha \beta \mu}
\partial_\nu F_{\alpha \beta}
=
M^\mu.
}
\end{equation}

Relating the tensor to the fields

The electromagnetic field has been identified with the electric and magnetic fields by

\begin{equation}\label{eqn:gaMagneticSourcesToTensorToVector:220}
F = \boldsymbol{\mathcal{E}} + c \mu_0 \boldsymbol{\mathcal{H}} I ,
\end{equation}

or in coordinates

\begin{equation}\label{eqn:gaMagneticSourcesToTensorToVector:240}
\inv{2} \gamma_\mu \wedge \gamma_\nu F^{\mu \nu}
= E^a \gamma_a \gamma_0 + c \mu_0 H^a \gamma_a \gamma_0 I.
\end{equation}

By forming the dot product sequence \( F^{\alpha \beta} = \gamma^\beta \cdot \lr{ \gamma^\alpha \cdot F } \), the electric and magnetic field components can be related to the tensor components. The electric field components follow by inspection and are

\begin{equation}\label{eqn:gaMagneticSourcesToTensorToVector:260}
E^b = \gamma^0 \cdot \lr{ \gamma^b \cdot F } = F^{b 0}.
\end{equation}

The magnetic field relation to the tensor components follow from

\begin{equation}\label{eqn:gaMagneticSourcesToTensorToVector:280}
\begin{aligned}
F^{r s}
&= F_{r s} \\
&= \gamma_s \cdot \lr{ \gamma_r \cdot \lr{ c \mu_0 H^a \gamma_a \gamma_0 I
} } \\
&=
c \mu_0 H^a \gpgradezero{ \gamma_s \gamma_r \gamma_a \gamma_0 I } \\
&=
c \mu_0 H^a \gpgradezero{ -\gamma^0 \gamma^1 \gamma^2 \gamma^3
\gamma_s \gamma_r \gamma_a \gamma_0 } \\
&=
c \mu_0 H^a \gpgradezero{ -\gamma^1 \gamma^2 \gamma^3
\gamma_s \gamma_r \gamma_a } \\
&=
– c \mu_0 H^a \delta^{[3 2 1]}_{s r a} \\
&=
c \mu_0 H^a \epsilon_{ s r a }.
\end{aligned}
\end{equation}

Expanding this for each pair of spacelike coordinates gives

\begin{equation}\label{eqn:gaMagneticSourcesToTensorToVector:320}
F^{1 2} = c \mu_0 H^3 \epsilon_{ 2 1 3 } = – c \mu_0 H^3
\end{equation}
\begin{equation}\label{eqn:gaMagneticSourcesToTensorToVector:340}
F^{2 3} = c \mu_0 H^1 \epsilon_{ 3 2 1 } = – c \mu_0 H^1
\end{equation}
\begin{equation}\label{eqn:gaMagneticSourcesToTensorToVector:360}
F^{3 1} = c \mu_0 H^2 \epsilon_{ 1 3 2 } = – c \mu_0 H^2,
\end{equation}

or

\begin{equation}\label{eqn:gaMagneticSourcesToTensorToVector:380}
\boxed{
\begin{aligned}
E^1 &= F^{1 0} \\
E^2 &= F^{2 0} \\
E^3 &= F^{3 0} \\
H^1 &= -\inv{c \mu_0} F^{2 3} \\
H^2 &= -\inv{c \mu_0} F^{3 1} \\
H^3 &= -\inv{c \mu_0} F^{1 2}.
\end{aligned}
}
\end{equation}

Recover the vector equations from the tensor equations

Starting with the non-dual Maxwell tensor equation, expanding the timelike index gives

\begin{equation}\label{eqn:gaMagneticSourcesToTensorToVector:480}
\begin{aligned}
\inv{c \epsilon_0} J^0
&= \inv{\epsilon_0} \rho \\
&=
\partial_\nu F^{\nu 0} \\
&=
\partial_1 F^{1 0}
+\partial_2 F^{2 0}
+\partial_3 F^{3 0}
\end{aligned}
\end{equation}

This is Gauss’s law

\begin{equation}\label{eqn:gaMagneticSourcesToTensorToVector:500}
\boxed{
\spacegrad \cdot \boldsymbol{\mathcal{E}}
=
\rho/\epsilon_0.
}
\end{equation}

For a spacelike index, any one is representive. Expanding index 1 gives

\begin{equation}\label{eqn:gaMagneticSourcesToTensorToVector:520}
\begin{aligned}
\inv{c \epsilon_0} J^1
&= \partial_\nu F^{\nu 1} \\
&= \inv{c} \partial_t F^{0 1}
+ \partial_2 F^{2 1}
+ \partial_3 F^{3 1} \\
&= -\inv{c} E^1
+ \partial_2 (c \mu_0 H^3)
+ \partial_3 (-c \mu_0 H^2) \\
&=
\lr{ -\inv{c} \PD{t}{\boldsymbol{\mathcal{E}}} + c \mu_0 \spacegrad \cross \boldsymbol{\mathcal{H}} } \cdot \Be_1.
\end{aligned}
\end{equation}

Extending this to the other indexes and multiplying through by \( \epsilon_0 c \) recovers the Ampere-Maxwell equation (assuming linear media)

\begin{equation}\label{eqn:gaMagneticSourcesToTensorToVector:540}
\boxed{
\spacegrad \cross \boldsymbol{\mathcal{H}} = \boldsymbol{\mathcal{J}} + \PD{t}{\boldsymbol{\mathcal{D}}}.
}
\end{equation}

The expansion of the 0th free (timelike) index of the dual Maxwell tensor equation is

\begin{equation}\label{eqn:gaMagneticSourcesToTensorToVector:400}
\begin{aligned}
M^0
&=
\inv{2} \epsilon^{\nu \alpha \beta 0}
\partial_\nu F_{\alpha \beta} \\
&=
-\inv{2} \epsilon^{0 \nu \alpha \beta}
\partial_\nu F_{\alpha \beta} \\
&=
-\inv{2}
\lr{
\partial_1 (F_{2 3} – F_{3 2})
+\partial_2 (F_{3 1} – F_{1 3})
+\partial_3 (F_{1 2} – F_{2 1})
} \\
&=

\lr{
\partial_1 F_{2 3}
+\partial_2 F_{3 1}
+\partial_3 F_{1 2}
} \\
&=

\lr{
\partial_1 (- c \mu_0 H^1 ) +
\partial_2 (- c \mu_0 H^2 ) +
\partial_3 (- c \mu_0 H^3 )
},
\end{aligned}
\end{equation}

but \( M^0 = c \rho_m \), giving us Gauss’s law for magnetism (with magnetic charge density included)

\begin{equation}\label{eqn:gaMagneticSourcesToTensorToVector:420}
\boxed{
\spacegrad \cdot \boldsymbol{\mathcal{H}} = \rho_m/\mu_0.
}
\end{equation}

For the spacelike indexes of the dual Maxwell equation, only one need be computed (say 1), and cyclic permutation will provide the rest. That is

\begin{equation}\label{eqn:gaMagneticSourcesToTensorToVector:440}
\begin{aligned}
M^1
&= \inv{2} \epsilon^{\nu \alpha \beta 1} \partial_\nu F_{\alpha \beta} \\
&=
\inv{2} \lr{ \partial_2 \lr{F_{3 0} – F_{0 3}} }
+\inv{2} \lr{ \partial_3 \lr{F_{0 2} – F_{0 2}} }
+\inv{2} \lr{ \partial_0 \lr{F_{2 3} – F_{3 2}} } \\
&=
– \partial_2 F^{3 0}
+ \partial_3 F^{2 0}
+ \partial_0 F_{2 3} \\
&=
-\partial_2 E^3 + \partial_3 E^2 + \inv{c} \PD{t}{} \lr{ – c \mu_0 H^1 } \\
&= – \lr{ \spacegrad \cross \boldsymbol{\mathcal{E}} + \mu_0 \PD{t}{\boldsymbol{\mathcal{H}}} } \cdot \Be_1.
\end{aligned}
\end{equation}

Extending this to the rest of the coordinates gives the Maxwell-Faraday equation (as extended to include magnetic current density sources)

\begin{equation}\label{eqn:gaMagneticSourcesToTensorToVector:460}
\boxed{
\spacegrad \cross \boldsymbol{\mathcal{E}} = -\boldsymbol{\mathcal{M}} – \mu_0 \PD{t}{\boldsymbol{\mathcal{H}}}.
}
\end{equation}

This takes things full circle, going from the vector differential Maxwell’s equations, to the Geometric Algebra form of Maxwell’s equation, to Maxwell’s equations in tensor form, and back to the vector form. Not only is the tensor form of Maxwell’s equations with magnetic sources now known, the translation from the tensor and vector formalism has also been verified, and miraculously no signs or factors of 2 were lost or gained in the process.

Energy momentum conservation with magnetic sources. Comparison to frequency domain and reciprocity theorem.

February 20, 2015 ece1229 , , ,

[Click here for a PDF of this post with nicer formatting]

In the frequency domain

In the frequency domain with \( \boldsymbol{\mathcal{E}} = \textrm{Re} \BE e^{j \omega t}, \boldsymbol{\mathcal{H}} = \textrm{Re} \BH e^{j \omega t} \). Using the electric field dot product as an example, note that we can write

\begin{equation}\label{eqn:energyMomentumWithMagneticSources:480}
\boldsymbol{\mathcal{E}} = \inv{2} \lr{ \BE e^{j \omega t} + \BE^\conj e^{-j \omega t} },
\end{equation}

so

\begin{equation}\label{eqn:energyMomentumWithMagneticSources:500}
\begin{aligned}
\boldsymbol{\mathcal{E}}^2
&=
\inv{2} \lr{ \BE e^{j \omega t} + \BE^\conj e^{-j \omega t} }
\cdot
\inv{2} \lr{ \BE e^{j \omega t} + \BE^\conj e^{-j \omega t} } \\
&=
\inv{4} \lr{
\BE^2 e^{2 j \omega t}
+ \BE \cdot \BE^\conj + \BE^\conj \cdot \BE
+\lr{\BE^\conj}^2 e^{-2 j \omega t}
} \\
&=
\inv{2} \textrm{Re}
\lr{
\BE \cdot \BE^\conj
+
\BE^2 e^{2 j \omega t}
}.
\end{aligned}
\end{equation}

Similarly, for the cross product

\begin{equation}\label{eqn:energyMomentumWithMagneticSources:540}
\begin{aligned}
\boldsymbol{\mathcal{E}} \cross \boldsymbol{\mathcal{H}}
&=
\inv{4}
\lr{
\BE \cross \BH e^{2 j \omega t}
+ \BE \cross \BH^\conj + \BE^\conj \cross \BH
+ \lr{ \BE^\conj \cross \BH^\conj } e^{-2 j \omega t}
} \\
&=
\inv{2}
\textrm{Re}
\lr{
\BE \cross \BH^\conj
+
\BE \cross \BH e^{2 j \omega t}
}.
\end{aligned}
\end{equation}

Given phasor representations of the sources \( \boldsymbol{\mathcal{M}} = \BM e^{j \omega t}, \boldsymbol{\mathcal{J}} = \BJ e^{j \omega t} \), \ref{eqn:energyMomentumWithMagneticSources:40} can be recast into (a messy) phasor form

\begin{equation}\label{eqn:energyMomentumWithMagneticSources:560}
\begin{aligned}
\inv{2} &\textrm{Re} \inv{2} \PD{t}{} \lr{
\epsilon_0 \BE \cdot \BE^\conj
+ \mu_0 \BH \cdot \BH^\conj
+ \epsilon_0 \BE^2 e^{ 2 j \omega t}
+ \mu_0 \BH^2 e^{ 2 j \omega t}
} \\
&+
\inv{2} \textrm{Re} \spacegrad \cdot \lr{
\BE \cross \BH^\conj
+\BE \cross \BH e^{ 2 j \omega t}
} \\
&=
\inv{2} \textrm{Re}
\lr{
– \BH \cdot \BM^\conj
– \BE \cdot \BJ^\conj
– \BH \cdot \BM e^{2 j \omega t}
– \BE \cdot \BJ e^{2 j \omega t}
}.
\end{aligned}
\end{equation}

In particular, when averaged over one period, the oscillatory terms vanish. The time averaged equivalent of the Poynting theorem is thus

\begin{equation}\label{eqn:energyMomentumWithMagneticSources:580}
0 =
{\left[
\textrm{Re}
\lr{
\inv{2} \PD{t}{} \lr{
\epsilon_0 \BE \cdot \BE^\conj
+ \mu_0 \BH \cdot \BH^\conj
}
+
\spacegrad \cdot \lr{
\BE \cross \BH^\conj
}
+
\BH \cdot \BM^\conj
+
\BE \cdot \BJ^\conj
}
\right]
}_{\textrm{av}}.
\end{equation}

Comparison to the reciprocity theorem result

The reciprocity theorem had a striking similarity to the Poynting theorem above, which isn’t suprising since both were derived by calculating the divergence of a Poynting like quantity.

Here’s a repetition of the reciprocity divergence calculation without the single frequency (phasor) assumption

\begin{equation}\label{eqn:energyMomentumWithMagneticSources:600}
\begin{aligned}
\spacegrad \cdot &\lr{
\boldsymbol{\mathcal{E}}^{(a)} \cross \boldsymbol{\mathcal{H}}^{(b)}
-\boldsymbol{\mathcal{E}}^{(b)} \cross \boldsymbol{\mathcal{H}}^{(a)}
} \\
&=
\boldsymbol{\mathcal{H}}^{(b)} \cdot \lr{ \spacegrad \cross \boldsymbol{\mathcal{E}}^{(a)} } -\boldsymbol{\mathcal{E}}^{(a)} \cdot \lr{ \spacegrad \cross \boldsymbol{\mathcal{H}}^{(b)} } \\
&\quad
-\boldsymbol{\mathcal{H}}^{(a)} \cdot \lr{ \spacegrad \cross \boldsymbol{\mathcal{E}}^{(b)} } +\boldsymbol{\mathcal{E}}^{(b)} \cdot \lr{ \spacegrad \cross \boldsymbol{\mathcal{H}}^{(a)} } \\
&=
-\boldsymbol{\mathcal{H}}^{(b)} \cdot \lr{ \mu_0 \partial_t \boldsymbol{\mathcal{H}}^{(a)} + \boldsymbol{\mathcal{M}}^{(a)} }
-\boldsymbol{\mathcal{E}}^{(a)} \cdot \lr{ \boldsymbol{\mathcal{J}}^{(b)} + \epsilon_0 \partial_t \boldsymbol{\mathcal{E}}^{(b)} } \\
&\quad
+\boldsymbol{\mathcal{H}}^{(a)} \cdot \lr{ \mu_0 \partial_t \boldsymbol{\mathcal{H}}^{(b)} + \boldsymbol{\mathcal{M}}^{(b)} }
+\boldsymbol{\mathcal{E}}^{(b)} \cdot \lr{ \boldsymbol{\mathcal{J}}^{(a)} + \epsilon_0 \partial_t \boldsymbol{\mathcal{E}}^{(a)} } \\
&=
\epsilon_0
\lr{
\boldsymbol{\mathcal{E}}^{(b)} \cdot \partial_t \boldsymbol{\mathcal{E}}^{(a)}
-\boldsymbol{\mathcal{E}}^{(a)} \cdot \partial_t \boldsymbol{\mathcal{E}}^{(b)}
}
+
\mu_0
\lr{
\boldsymbol{\mathcal{H}}^{(a)} \cdot \partial_t \boldsymbol{\mathcal{H}}^{(b)}
-\boldsymbol{\mathcal{H}}^{(b)} \cdot \partial_t \boldsymbol{\mathcal{H}}^{(a)}
} \\
&+\boldsymbol{\mathcal{H}}^{(a)} \cdot \boldsymbol{\mathcal{M}}^{(b)}
-\boldsymbol{\mathcal{H}}^{(b)} \cdot \boldsymbol{\mathcal{M}}^{(a)}
+\boldsymbol{\mathcal{E}}^{(b)} \cdot \boldsymbol{\mathcal{J}}^{(a)}
-\boldsymbol{\mathcal{E}}^{(a)} \cdot \boldsymbol{\mathcal{J}}^{(b)}
\end{aligned}
\end{equation}

What do these time derivative terms look like in the frequency domain?

\begin{equation}\label{eqn:energyMomentumWithMagneticSources:620}
\begin{aligned}
\boldsymbol{\mathcal{E}}^{(b)} \cdot \partial_t \boldsymbol{\mathcal{E}}^{(a)}
&=
\inv{4}
\lr{
\BE^{(b)} e^{j \omega t}
+
{\BE^{(b)}}^\conj e^{-j \omega t}
}
\cdot
\partial_t
\lr{
\BE^{(a)} e^{j \omega t}
+
{\BE^{(a)}}^\conj e^{-j \omega t}
} \\
&=
\frac{j \omega}{4}
\lr{
\BE^{(b)} e^{j \omega t}
+
{\BE^{(b)}}^\conj e^{-j \omega t}
}
\cdot
\lr{
\BE^{(a)} e^{j \omega t}

{\BE^{(a)}}^\conj e^{-j \omega t}
} \\
&=
\frac{\omega}{4}
\lr{
j \BE^{(a)} \cdot { \BE^{(b)} }^\conj
-j \BE^{(b)} \cdot { \BE^{(a)} }^\conj
+j \BE^{(a)} \cdot \BE^{(b)} e^{ 2 j \omega t }
-j { \BE^{(a)}}^\conj \cdot { \BE^{(b)} }^\conj e^{ -2 j \omega t }
} \\
&=
\inv{2} \textrm{Re}
\lr{
j \omega \BE^{(a)} \cdot { \BE^{(b)} }^\conj
+ j \omega \BE^{(a)} \cdot \BE^{(b)} e^{ 2 j \omega t }
}
\end{aligned}
\end{equation}

Taking the difference,

\begin{equation}\label{eqn:energyMomentumWithMagneticSources:640}
\begin{aligned}
\boldsymbol{\mathcal{E}}^{(b)} \cdot \partial_t \boldsymbol{\mathcal{E}}^{(a)}
-\boldsymbol{\mathcal{E}}^{(a)} \cdot \partial_t \boldsymbol{\mathcal{E}}^{(b)}
&=
\inv{2} \textrm{Re}
\lr{
j \omega \BE^{(a)} \cdot { \BE^{(b)} }^\conj
– j \omega \BE^{(b)} \cdot { \BE^{(a)} }^\conj
+ j \omega \BE^{(a)} \cdot \BE^{(b)} e^{ 2 j \omega t }
– j \omega \BE^{(b)} \cdot \BE^{(a)} e^{ 2 j \omega t }
} \\
&=
– \omega \textrm{Im}
\lr{
\BE^{(a)} \cdot { \BE^{(b)} }^\conj
+ \BE^{(a)} \cdot \BE^{(b)} e^{ 2 j \omega t }
},
\end{aligned}
\end{equation}

so we have

\begin{equation}\label{eqn:energyMomentumWithMagneticSources:660}
0
=
{
\left[
\spacegrad \cdot \textrm{Re} \lr{
\BE^{(a)} \cross {\BH^{(b)}}^\conj
-\BE^{(b)} \cross {\BH^{(a)}}^\conj
}
+
\omega \textrm{Im}
\lr{
\epsilon_0
\BE^{(a)} \cdot { \BE^{(b)} }^\conj
+
\mu_0
\BH^{(a)} \cdot { \BH^{(b)} }^\conj
}
+ \textrm{Re}
\lr{
-\BH^{(a)} \cdot { \BM^{(b)} }^\conj
+\BH^{(b)} \cdot { \BM^{(a)} }^\conj
-\BE^{(b)} \cdot { \BJ^{(a)} }^\conj
+\BE^{(a)} \cdot { \BJ^{(b)} }^\conj
}
\right]
}_{\textrm{av}}.
\end{equation}

Observe that the perfect cancellation of the time derivative terms only occurs when the cross product differences were those of the phasors. When those cross differences are those of the actual fields, like those in the Poynting theorem, there is a frequency dependent term is that expansion.

Energy momentum conservation with magnetic sources

February 20, 2015 ece1229 , , , , , , , , , , , , , , ,

[Click here for a PDF of this post with nicer formatting]

Maxwell’s equations with magnetic sources

The form of Maxwell’s equations to be used here are expressed in terms of \( \boldsymbol{\mathcal{E}} \) and \( \boldsymbol{\mathcal{H}} \), assume linear media, and do not assume a phasor representation

\begin{equation}\label{eqn:energyMomentumWithMagneticSources:120}
\spacegrad \cross \boldsymbol{\mathcal{E}} = – \boldsymbol{\mathcal{M}} – \mu_0 \PD{t}{\boldsymbol{\mathcal{H}}}
\end{equation}
\begin{equation}\label{eqn:energyMomentumWithMagneticSources:140}
\spacegrad \cross \boldsymbol{\mathcal{H}} = \boldsymbol{\mathcal{J}} + \epsilon_0 \PD{t}{\boldsymbol{\mathcal{E}}}
\end{equation}
\begin{equation}\label{eqn:energyMomentumWithMagneticSources:160}
\spacegrad \cdot \boldsymbol{\mathcal{E}} = \rho/\epsilon_0
\end{equation}
\begin{equation}\label{eqn:energyMomentumWithMagneticSources:180}
\spacegrad \cdot \boldsymbol{\mathcal{H}} = \rho_m/\mu_0.
\end{equation}

Energy momentum conservation

With magnetic sources the Poynting and energy conservation relationship has to be adjusted slightly. Let’s derive that result, starting with the divergence of the Poynting vector

\begin{equation}\label{eqn:energyMomentumWithMagneticSources:20}
\begin{aligned}
\spacegrad \cdot \lr{ \boldsymbol{\mathcal{E}} \cross \boldsymbol{\mathcal{H}} }
&=
\boldsymbol{\mathcal{H}} \cdot \lr{ \spacegrad \cross \boldsymbol{\mathcal{E}} }
-\boldsymbol{\mathcal{E}} \cdot \lr{ \spacegrad \cross \boldsymbol{\mathcal{H}} } \\
&=
-\boldsymbol{\mathcal{H}} \cdot \lr{ \mu_0 \partial_t \boldsymbol{\mathcal{H}} + \boldsymbol{\mathcal{M}} }
-\boldsymbol{\mathcal{E}} \cdot \lr{ \boldsymbol{\mathcal{J}} + \epsilon_0 \partial_t \boldsymbol{\mathcal{E}} } \\
&=
– \mu_0 \boldsymbol{\mathcal{H}} \cdot \partial_t \boldsymbol{\mathcal{H}} – \boldsymbol{\mathcal{H}} \cdot \boldsymbol{\mathcal{M}}
– \epsilon_0 \boldsymbol{\mathcal{E}} \cdot \partial_t \boldsymbol{\mathcal{E}} – \boldsymbol{\mathcal{E}} \cdot \boldsymbol{\mathcal{J}},
\end{aligned}
\end{equation}

or

\begin{equation}\label{eqn:energyMomentumWithMagneticSources:40}
\boxed{
\inv{2} \PD{t}{} \lr{ \epsilon_0 \boldsymbol{\mathcal{E}}^2 + \mu_0 \boldsymbol{\mathcal{H}}^2 }
+
\spacegrad \cdot \lr{ \boldsymbol{\mathcal{E}} \cross \boldsymbol{\mathcal{H}} }
=
– \boldsymbol{\mathcal{H}} \cdot \boldsymbol{\mathcal{M}}
– \boldsymbol{\mathcal{E}} \cdot \boldsymbol{\mathcal{J}}.
}
\end{equation}

The usual relationship is only modified by one additional term. Recall from electrodynamics [2] that \ref{eqn:energyMomentumWithMagneticSources:40} (when the magnetic current density \( \boldsymbol{\mathcal{M}} \) is omitted) is just one of four components of the energy momentum conservation equation

\begin{equation}\label{eqn:energyMomentumWithMagneticSources:80}
\partial_\mu T^{\mu \nu} = – \inv{c} F^{\nu \lambda} j_\lambda.
\end{equation}

Note that \ref{eqn:energyMomentumWithMagneticSources:80} was likely not in SI units. The next task is to generalize this classical relationship to incorporate the magnetic sources used in antenna theory. With an eye towards the relativistic nature of the energy momentum tensor, it is natural to assume that the remainder of the energy momentum tensor conservation relation can be found by taking the time derivatives of the Poynting vector.

\begin{equation}\label{eqn:energyMomentumWithMagneticSources:200}
\PD{t}{} \lr{ \boldsymbol{\mathcal{E}} \cross \boldsymbol{\mathcal{H}} }
=
\PD{t}{\boldsymbol{\mathcal{E}}} \cross \boldsymbol{\mathcal{H}}
+ \boldsymbol{\mathcal{E}} \cross \PD{t}{\boldsymbol{\mathcal{H}} }
=
\inv{\epsilon_0}
\lr{ \spacegrad \cross \boldsymbol{\mathcal{H}} – \boldsymbol{\mathcal{J}} } \cross \boldsymbol{\mathcal{H}}
+
\inv{\mu_0}
\boldsymbol{\mathcal{E}} \cross
\lr{

\spacegrad \cross \boldsymbol{\mathcal{E}} – \boldsymbol{\mathcal{M}} },
\end{equation}

or

\begin{equation}\label{eqn:energyMomentumWithMagneticSources:220}
\inv{c^2} \PD{t}{} \lr{ \boldsymbol{\mathcal{E}} \cross \boldsymbol{\mathcal{H}} }
+
\mu_0 \boldsymbol{\mathcal{J}} \cross \boldsymbol{\mathcal{H}}
+\epsilon_0
\boldsymbol{\mathcal{E}} \cross \boldsymbol{\mathcal{M}}
=
-\mu_0 \boldsymbol{\mathcal{H}} \cross \lr{ \spacegrad \cross \boldsymbol{\mathcal{H}} }
– \epsilon_0 \boldsymbol{\mathcal{E}} \cross \lr{ \spacegrad \cross \boldsymbol{\mathcal{E}} }.
\end{equation}

The \( \mu_0 \boldsymbol{\mathcal{J}} \cross \boldsymbol{\mathcal{H}} = \boldsymbol{\mathcal{J}} \cross \BB \) is a portion of the Lorentz force equation in its density form. To put \ref{eqn:energyMomentumWithMagneticSources:220} into the desired form, the remainder of the Lorentz force force equation \( \rho \boldsymbol{\mathcal{E}} = \epsilon_0 \boldsymbol{\mathcal{E}} \spacegrad \cdot \boldsymbol{\mathcal{E}} \) must be added to both sides. To extend the magnetic current term to its full dual (magnetic) Lorentz force structure, the quantity to add to both sides is \( \rho_m \boldsymbol{\mathcal{H}} = \mu_0 \boldsymbol{\mathcal{H}} \spacegrad \cdot \boldsymbol{\mathcal{H}} \). Performing these manipulations gives

\begin{equation}\label{eqn:energyMomentumWithMagneticSources:240}
\inv{c^2} \PD{t}{} \lr{ \boldsymbol{\mathcal{E}} \cross \boldsymbol{\mathcal{H}} }
+
\rho \BE + \mu_0 \boldsymbol{\mathcal{J}} \cross \boldsymbol{\mathcal{H}}
+ \rho_m \boldsymbol{\mathcal{H}}
+ \epsilon_0 \boldsymbol{\mathcal{E}} \cross \boldsymbol{\mathcal{M}}
=
\mu_0
\lr{
\boldsymbol{\mathcal{H}} \spacegrad \cdot \boldsymbol{\mathcal{H}}
-\boldsymbol{\mathcal{H}} \cross \lr{ \spacegrad \cross \boldsymbol{\mathcal{H}} }
}
+ \epsilon_0
\lr{
\boldsymbol{\mathcal{E}} \spacegrad \cdot \boldsymbol{\mathcal{E}}

\boldsymbol{\mathcal{E}} \cross \lr{ \spacegrad \cross \boldsymbol{\mathcal{E}} }
}.
\end{equation}

It seems slightly surprising the sign of the magnetic equivalent of the Lorentz force terms have an alternation of sign. This is, however, consistent with the duality transformations outlined in ([1] table 3.2)

\begin{equation}\label{eqn:energyMomentumWithMagneticSources:280}
\rho \rightarrow \rho_m
\end{equation}
\begin{equation}\label{eqn:energyMomentumWithMagneticSources:300}
\boldsymbol{\mathcal{J}} \rightarrow \boldsymbol{\mathcal{M}}
\end{equation}
\begin{equation}\label{eqn:energyMomentumWithMagneticSources:320}
\mu_0 \rightarrow \epsilon_0
\end{equation}
\begin{equation}\label{eqn:energyMomentumWithMagneticSources:340}
\boldsymbol{\mathcal{E}} \rightarrow \boldsymbol{\mathcal{H}}
\end{equation}
\begin{equation}\label{eqn:energyMomentumWithMagneticSources:360}
\boldsymbol{\mathcal{H}} \rightarrow -\boldsymbol{\mathcal{E}},
\end{equation}

for

\begin{equation}\label{eqn:energyMomentumWithMagneticSources:380}
\rho \BE + \mu_0 \boldsymbol{\mathcal{J}} \cross \boldsymbol{\mathcal{H}}
\rightarrow
\rho_m \BH + \epsilon_0 \boldsymbol{\mathcal{M}} \cross \lr{ -\boldsymbol{\mathcal{E}}}
=
\rho_m \BH + \epsilon_0 \boldsymbol{\mathcal{E}} \cross \boldsymbol{\mathcal{M}}.
\end{equation}

Comfortable that the LHS has the desired structure, the RHS can expressed as a divergence. Just expanding one of the differences of vector products on the RHS does not obviously show that this is possible, for example

\begin{equation}\label{eqn:energyMomentumWithMagneticSources:400}
\begin{aligned}
\Be_a \cdot
\lr{
\boldsymbol{\mathcal{E}} \spacegrad \cdot \boldsymbol{\mathcal{E}}

\boldsymbol{\mathcal{E}} \cross \lr{ \spacegrad \cross \boldsymbol{\mathcal{E}} }
}
&=
E_a \partial_b E_b

\epsilon_{a b c} E_b \epsilon_{c r s} \partial_r E_s \\
&=
E_a \partial_b E_b

\delta_{a b}^{[r s]} E_b \partial_r E_s \\
&=
E_a \partial_b E_b

E_b \lr{
\partial_a E_b
-\partial_b E_a
} \\
&=
E_a \partial_b E_b
– E_b \partial_a E_b
+ E_b \partial_b E_a.
\end{aligned}
\end{equation}

This happens to equal

\begin{equation}\label{eqn:energyMomentumWithMagneticSources:420}
\begin{aligned}
\spacegrad \cdot \lr{ \lr{E_a E_b – \inv{2} \delta_{a b} \boldsymbol{\mathcal{E}}^2 } \Be_b }
&=
\partial_b
\lr{E_a E_b – \inv{2} \delta_{a b} \boldsymbol{\mathcal{E}}^2 } \\
&=
E_b \partial_b E_a
+ E_a \partial_b E_b

\inv{2} \delta_{a b} 2 E_c \partial_b E_c \\
i&=
E_b \partial_b E_a
+ E_a \partial_b E_b
– E_b \partial_a E_b.
\end{aligned}
\end{equation}

This allows a final formulation of the remaining energy momentum conservation equation in its divergence form. Let

\begin{equation}\label{eqn:energyMomentumWithMagneticSources:440}
T^{a b} =
\epsilon_0 \lr{ E_a E_b – \inv{2} \delta_{a b} \boldsymbol{\mathcal{E}}^2 }
+ \mu_0 \lr{ H_a H_b – \inv{2} \delta_{a b} \boldsymbol{\mathcal{H}}^2 },
\end{equation}

so that the remaining energy momentum conservation equation, extended to both electric and magnetic sources, is

\begin{equation}\label{eqn:energyMomentumWithMagneticSources:460}
\boxed{
\inv{c^2} \PD{t}{} \lr{ \boldsymbol{\mathcal{E}} \cross \boldsymbol{\mathcal{H}} }
+
\rho \BE + \mu_0 \boldsymbol{\mathcal{J}} \cross \boldsymbol{\mathcal{H}}
+ \rho_m \boldsymbol{\mathcal{H}}
+ \epsilon_0 \boldsymbol{\mathcal{E}} \cross \boldsymbol{\mathcal{M}}
=
\Be_a \spacegrad \cdot \lr{ T^{a b} \Be_b }.
}
\end{equation}

On the LHS we have the rate of change of momentum density, the electric Lorentz force density terms, the dual (magnetic) Lorentz force density terms, and on the RHS the the momentum flux terms.

References

[1] Constantine A Balanis. Antenna theory: analysis and design. John Wiley \& Sons, 3rd edition, 2005.

[2] Peeter Joot. Relativistic Electrodynamics., chapter {Energy Momentum Tensor.} peeterjoot.com, 2011. URL https://peeterjoot.com/archives/math2011/phy450.pdf. [Online; accessed 18-February-2015].