• 362,568

# Archive for December, 2012

## Dipole Moment from constant electric field

Posted by peeterjoot on December 27, 2012

## Question: Dipole Moment from constant electric field

In [1] it is stated that the force per unit angle on a dipole system as illustrated in Fig.1 is

\begin{aligned}F_\theta = -p \mathcal{E} \sin\theta,\end{aligned} \hspace{\stretch{1}}(1.0.1)

where $\mathbf{p} = q \mathbf{r}$. The text was also referring to torques, and it wasn’t clear to me if the result was the torque or the force. Derive the result to resolve any doubt (in retrospect dimensional analysis would also have worked).

Fig1: Dipole moment coordinate

Let’s put the electric field in the $\hat{\mathbf{x}}$ direction ($\theta = 0$), so that the potential acting on charge $i$ is given implicitly by

\begin{aligned}\mathbf{F}_i = q_i \mathcal{E} \hat{\mathbf{x}} = -\nabla \phi_i = -\hat{\mathbf{x}} \frac{d{{\phi_i}}}{dx}\end{aligned} \hspace{\stretch{1}}(1.0.2)

or

\begin{aligned}\phi_i = -q_i (x_i - x_0).\end{aligned} \hspace{\stretch{1}}(1.0.3)

Our positions, and velocities are

\begin{aligned}\mathbf{r}_{1,2} = \pm \frac{r}{2} \hat{\mathbf{x}} e^{\hat{\mathbf{x}} \hat{\mathbf{y}} \theta}\end{aligned} \hspace{\stretch{1}}(1.0.4a)

\begin{aligned}\frac{d{{\mathbf{r}_{1,2}}}}{dt} = \pm \frac{r}{2} \dot{\theta} \hat{\mathbf{y}} e^{\hat{\mathbf{x}} \hat{\mathbf{y}} \theta}.\end{aligned} \hspace{\stretch{1}}(1.0.4b)

Our kinetic energy is

\begin{aligned}T = \frac{1}{{2}} \sum_i m_i \left( \frac{d{{\mathbf{r}_i}}}{dt} \right)^2 = \frac{1}{{2}} \sum_i m_i \left( \frac{r}{2} \right)^2 \dot{\theta}^2= \frac{1}{{2}} (m_1 + m_2) \left( \frac{r}{2} \right)^2 \dot{\theta}^2.\end{aligned} \hspace{\stretch{1}}(1.0.5)

For our potential energies we require the $x$ component of the position vectors, which are

\begin{aligned}x_i = \mathbf{r}_i \cdot \hat{\mathbf{x}}=\pm \left\langle{{ \frac{r}{2} \hat{\mathbf{x}} e^{\hat{\mathbf{x}} \hat{\mathbf{y}} \theta} \hat{\mathbf{x}}}}\right\rangle=\pm \frac{r}{2} \cos\theta\end{aligned} \hspace{\stretch{1}}(1.0.6)

Our potentials are

\begin{aligned}\phi_1 = -q_1 \mathcal{E} \frac{r}{2} \cos\theta + \phi_0\end{aligned} \hspace{\stretch{1}}(1.0.7a)

\begin{aligned}\phi_2 = q_2 \mathcal{E} \frac{r}{2} \cos\theta + \phi_0\end{aligned} \hspace{\stretch{1}}(1.0.7b)

Our system Lagrangian, after dropping the constant reference potential that doesn’t effect the dynamics is

\begin{aligned}\mathcal{L} = \frac{1}{{2}} (m_1 + m_2) \left( \frac{r}{2} \right)^2 \dot{\theta}^2+q_1 \mathcal{E} \frac{r}{2} \cos\theta-q_2 \mathcal{E} \frac{r}{2} \cos\theta\end{aligned} \hspace{\stretch{1}}(1.0.8)

For this problem we had two equal masses and equal magnitude charges $m = m_1 = m_2$ and $q = q_1 = -q_2$

\begin{aligned}\mathcal{L} = \frac{1}{{4}} m r^2 \dot{\theta}^2 + q r \mathcal{E} \cos\theta\end{aligned} \hspace{\stretch{1}}(1.0.9)

\begin{aligned}p_\theta = \frac{\partial {\mathcal{L}}}{\partial {\dot{\theta}}} = \frac{1}{{2}} m r^2 \dot{\theta}\end{aligned} \hspace{\stretch{1}}(1.0.10)

\begin{aligned}\frac{\partial {\mathcal{L}}}{\partial {\theta}} = -q r \mathcal{E} \sin\theta=\frac{d{{p_\theta}}}{dt}=\frac{1}{{2}} m r^2 \dot{d}{\theta}\end{aligned} \hspace{\stretch{1}}(1.0.11)

Putting these together, with $p = q r$, we have the result stated in the text

\begin{aligned}F_\theta = \frac{d{{p_\theta}}}{dt} = -p \mathcal{E} \sin\theta.\end{aligned} \hspace{\stretch{1}}(1.0.12)

# References

[1] E.A. Jackson. Equilibrium statistical mechanics. Dover Pubns, 2000.

## Stirling’s like approximation for log factorial

Posted by peeterjoot on December 24, 2012

# Motivation

Back in my undergrad days, I rarely took notes, since most of what we covered was in the text books. The only notes I’d taken from the course where we did some thermodynamics was half an $8\, 1/2 \times 11$ scrap paper with a simple Stirling like approximation for $\ln N!$ (sitting in my old text [1]). Here it is

# Guts

First write the factorial in product form

\begin{aligned}N! = \Pi_{k = 0}^{N-1} (N - k).\end{aligned} \hspace{\stretch{1}}(1.2.1)

so that it’s logarithm is a sum

\begin{aligned}\ln N! = \sum_{k = 0}^{N-1} \ln( N - k ).\end{aligned} \hspace{\stretch{1}}(1.2.2)

We can now derivatives of both sides with respect to $N$, ignoring the fact that $N$ is a discrete variable. For the left hand side, writing $f = N!$, we have

\begin{aligned}\frac{d \ln f }{dN} = \frac{1}{{f}} \frac{d f}{dN}.\end{aligned} \hspace{\stretch{1}}(1.2.3)

Now for the right hand side

\begin{aligned}\frac{d}{dN} \sum_{k = 0}^{N-1} \ln( N - k ) = \sum_{k = 0}^{N-1} \frac{1}{{ N - k }}\approx\int_{0}^{N-1} \frac{dk}{ N - k }= \int_{N}^{1} \frac{-du}{ u }= \ln N - \ln 1 = \ln N\end{aligned} \hspace{\stretch{1}}(1.2.4)

Merging 1.2.3 and 1.2.4 into two differentials, and integrating we have

\begin{aligned}\frac{df}{f} \approx \ln N dN.\end{aligned} \hspace{\stretch{1}}(1.2.5)

Observing that

\begin{aligned}(x \ln x -x)' = x \frac{1}{{x}} + \ln x - 1 = \ln x,\end{aligned} \hspace{\stretch{1}}(1.2.6)

we have

\begin{aligned}\ln N! \approx N \ln N - N + C.\end{aligned} \hspace{\stretch{1}}(1.2.7)

Even though we are interested in large $N$, we note that with $N = 1$ we have

\begin{aligned}0 = -1 + C,\end{aligned} \hspace{\stretch{1}}(1.2.8)

but this is small compared to $N$ for large $N$, so we have

\begin{aligned}\boxed{\ln N! \approx \ln N^N - N}\end{aligned} \hspace{\stretch{1}}(1.2.9)

Expontiating, this is

\begin{aligned}\boxed{N! \approx N^N e^{-N}}.\end{aligned} \hspace{\stretch{1}}(1.2.10)

I think that our Professor’s point at the time was that when we only care about the logarithm of $N!$ we can get away with 1.2.9, and can avoid the complexity and care required to do a proper Stirling’s approximation.

# References

[1] C. Kittel and H. Kroemer. Thermal physics. WH Freeman, 1980.

## reorganization of PHY356 (Quantum I) notes.

Posted by peeterjoot on December 22, 2012

Looking for something in my old notes while studying for my optics exam, I found that my old phy356 notes were very disorganized.  That was my first attempt to compile my class notes into a bookish unit, and it was not done terribly well.  Here’s a reorganized version:

The new structure is

Basic formalism

•    Lecture notes: Review
•    Problems

Commutator and time evolution

•    Rotations using matrix exponentials
•    On commutation of exponentials
•    Desai Chapter II notes
•    Unitary exponential sandwich
•    Lecture notes: Review
•    Problems

Dynamical equations

•    Problems

Free particles

•    Notes for Desai chapter IV
•    Lecture notes: Review
•    Problems

Spin 1/2

•    Lecture notes: Review
•    Lecture: Orbital and Intrinsic Momentum
•    Problems

Gauge invariance, angular momentum and spin

•    Interaction with orbital angular momentum

Stern-Gerlach

•    Lecture: Stern Gerlach

Lecture: Making Sense of Quantum Mechanics

Bound state problems

•    Hydrogen like atom, and Laguerre polynomials
•    Lecture: Bound-state problems
•    Lecture: Hydrogen atom
•    Problems

Harmonic oscillator

•    Lecture: Harmonic oscillator
•    Problems

Coherent states

•    interaction with a electric field

Rotations and angular momentum

•    Lecture: Rotations and Angular Momentum
•    Notes for Desai Chapter 26

Exam prep and review

•    Questions from the Dec 2007 PHY355H1F exam
•    Questions from the Dec 2008 PHY355H1F exam
•    A problem on spherical harmonics (2010 final exam reflection)

## Final update to course notes for Fall 2012, UofT Modern Optics (PHY485H1F), taught by Prof. Joseph H. Thywissen

Posted by peeterjoot on December 17, 2012

As mentioned previously, I had been batching any postings of my course notes and problems from University of Toronto PHY485H1F Advanced Classical Optics, instead of posting anything individually.  This is now updated with some corrections from exam review, and two additional exam practice questions:

December 17, 2012 2010 q5 Gaussian beam problem.

December 15, 2012 Fraunhofer diffraction pattern for four circular aperatures (also from 2010 final)

## cost of a global array access?

Posted by peeterjoot on December 17, 2012

For an array where the value of

   myGlobalArrayOfInts[ THE_ARRAY_OFFSET_FOR_OPERATION__FOO ] == THE_VALUE_FOR_OPERATION__FOO


some developer used

   int value = THE_VALUE_FOR_OPERATION__FOO ; // for performance, just set it.


   int value = myGlobalArrayOfInts[ THE_ARRAY_OFFSET_FOR_OPERATION__FOO ]


At one point in time the assumption that myGlobalArrayOfInts[ THE_ARRAY_OFFSET_FOR_OPERATION__FOO ] was never have been any different than THE_VALUE_FOR_OPERATION__FOO was true. However, this bit of premature optimization inevitably broke when maintainance programming required that this array value be changed.

The developer fixing this asked me “How expensive is this array access”. I can’t exactly quantify the cost of this, although at least half of it in this case is probably going after the address of the global itself. On AIX I believe that this may require loading the TOC register if the last global access was in a different shared lib, and there’s probably some similar notion on most other systems. I happened to be using a Linux build at that point of time, so to help quantify the cost I took a look at the asm generated for a function of the form

int foo0()
{
return myGlobalArrayOfInts[ THE_ARRAY_OFFSET_FOR_OPERATION__FOO ] ;
}


Here’s the code that was generated

0000000000002f50 :
2f50:   48 8b 15 00 00 00 00    mov    0x0(%rip),%rdx        # 2f57
2f57:   48 63 42 14             movslq 0x14(%rdx),%rax
2f5b:   c3                      retq


We’ve essentially got two dereferences required for this array dereference, one that’s probably getting the address of the global itself. I’d guess that this mov instruction ends up rewritten by the linker later, putting in actual values for the location of this global instead of the ’00 00 00 00′ seen in the instruction dump. Then we’ve got one more dereference to get at the global value itself.

Would this really have made a measurable difference in a function that was 841 lines of code long, (1067 straight line instruction count)? Doubtful.

## C++11 play. Simple hashing, a comparison with perl

Posted by peeterjoot on December 17, 2012

I’m very impressed with how easy C++11 makes it possible to implement a simple hash

#include <set>
#include <string>
#include <iostream>

using namespace std ;

int main()
{
set< string > d ;

d.insert( "uu:long" ) ;
d.insert( "uu:long" ) ;
d.insert( "uu:long long" ) ;

d.insert( "qq:int" ) ;
d.insert( "Test:int" ) ;

for ( auto & kv : d )
{
cout << kv << endl ;
}
}


If I had to do hashing like this before I’d probably have seen if I could generate a text file, and then post process it with perl, where the same hashing would look like:

#!/usr/bin/perl

my %d ;

$d{'uu:long'}++ ;$d{'uu:long'}++ ;
$d{'uu:long long'}++ ;$d{'qq:long'}++ ;
$d{'Test:int'}++ ; foreach ( keys %d ) { print "$_\n" ;
}


Other the the header includes and namespace statement, it’s not really any harder to do this in C++ now. I just have to wait 5 years before all our product compilers catch up with the standard, I could start thinking about using this sort of code in production. Unfortunately in production I’d also have to deal with exceptions, and the (hidden) fact that the std::allocator is not generally appropriate for use within DB2 code.

## Update to course notes for Fall 2012, UofT Modern Optics (PHY485H1F), taught by Prof. Joseph H. Thywissen

Posted by peeterjoot on December 7, 2012

As mentioned previously, and originally I’m batching any postings of my course notes and problems from University of Toronto PHY485H1F Advanced Classical Optics. These are now updated with the following additional content:

December 07, 2012 Midterm review. Lloyd’s mirror

December 06, 2012 Number of photons per free space mode

December 06, 2012 Spectral line width (coherence time) of laser

December 06, 2012 Problem set. Gaussian Beams

December 02, 2012 Guoy phase shifts and higher order Gaussian modes

December 01, 2012 Fresnel form approximate solution to paraxial wave equation

November 27, 2012 Solving the homogeneous paraxial wave equation

November 23, 2012 vector identities

November 21, 2012 Planck blackbody summation

November 21, 2012 Gaussian modes

November 15, 2012 Laser pump rates

## ease of getting strncat wrong.

Posted by peeterjoot on December 5, 2012

I’d done a test build with the clang compiler of our db2 code and fired off some defects and emails to some of the code owners about messages from that compiler like:

foo.C:1220:15: warning: the value of the size argument in ‘strncat’ is too large, might lead to a buffer overflow [-Wstrncat-size]

It’s a great message, clear and unambiguous: “your code is wrong”!

Here’s a sample of the code in question that clang was complaining about:

   strncat( dirPath, ptrToDirectories, ( sizeof( dirPath ) - strlen( dirPath ) ) );


The compiler could just as well complain about simpler strncat calls.  Imagine at first that dirPath was all zeros, then the above code would be equivalent to:

   strncat( dirPath, ptrToDirectories, sizeof( dirPath ) ) ;


It’s not immediately obvious reading the strncat man page that this is wrong, since that man page says “[strncat] will use at most n characters from src”. However, it also says “As with strcat(), the resulting string in dest is always null terminated.”

Does this mean that it will use no more than n characters from src, but that it null terminates within the range of those n-characters, or does this mean use (if strlen(src) > n) n characters from src and then add a null terminator after that?

To answer that question, and rule out a possible false positive warning from the compiler I wrote the following simple bit of test code:

#include <string.h>
#include <stdio.h>

struct string
{
char dest[2] ;
char overflow ;
} ;

int main()
{
string o ;
o.dest[0] = 0  ;
o.dest[1] = 1  ;
o.overflow = 1 ;

printf( "%d\n", (int)o.overflow ) ;

strncat( o.dest, "abc", sizeof(o.dest) ) ;

printf( "%d\n", (int)o.overflow ) ;

return 0 ;
}


And the verdict was:

\$ g++ -g t.C ; a.out
1
0

Yup, the code was bad, and the compiler is without fault. We actually have a different function for strncat’ing in our code that takes the actual buffer size, to make this harder to get wrong, but the code in question was not using that.