GSoC 2014 Application Sudhanshu Mishra : Optics Module - sympy/sympy GitHub Wiki

About me

###Basic Information Name: Sudhanshu Mishra
Email: [email protected]
University: BITS Pilani, India
IRC: debugger22
GitHub: debugger22
Blog: http://lifelesscode.blogspot.com

###Background Work and Programming Skills I am a second year undergraduate student of Information Systems at BITS Pilani, India. I like Physics and I've been using Python for almost two years. Following are some of the open source projects that I created using Python:

  • BinPy For electronics computation and simulation. It is a library to provide a base to develop circuit based applications and educational software on top of it. BinPy is a clear representation of fundamentals. Everything has been written from scratch such as gates, logical operations, etc. This package does not depend on any external library other than pure Python. We want to extend the hardware programming concepts to Python.
  • Jarvis A small attempt to clone Jarvis like system using Google's STT and TTS.
  • Sentiment Analyzer A sentiment analyzer especially for tweets.

A few of my other projects can be seen at https://github.com/debugger22. I use xubuntu, Sublime text for development. I've been using Git for a long time and have mastered the basics.

Contributions to sympy

######Merged

######Open

The Idea

Abstract

After considering the traditional areas of application; mechanics, relativity and quantum mechanics, we can extend our examination to other areas of physics which would appear, from the literature, to be using computer algebra efficiently: optics, fluid mechanics, plasma physics and other applications.

My proposal intends to extend the work done related to optics in SymPy by introducing some good applications. Till now we have only module which can do computations related to Gaussian optics and a bit of geometrical optics.

This new addition will enable the users to solve Optics related problems very easily, rather than penning down the long and untidy formulas for solving equations after equations. It's about give the data and take away the result. Don't care about the arduous mid-way, dealing with non-linear equations under the baffling sign convention. Why should we care for this? We want the result.I want to make it intuitive.

Introducing optics using SymPy will reduce a lot of effort in teaching, learning and the practical applications of the same. My project will cover majorly following branches:

  • General Optics
  • Wave Optics
  • Major results used in optics
  • Adding documentations in the form of IPython notebooks of examples/illustrations provided in a fundamental optics book

###Inspiration My project proposal idea is inspired from my affection towards physics. Using the power of SymPy's mathematics it would be very easy to implement the results in optics. This idea is inspired by a book Waves and Optics Simulations by Consortium For Upper-Level Physics Software.

This is the discussion in the mailing list, that led to finalizing my GSoC project idea:

###Theory ####General Optics ######Concepts of light rays, reflection, refraction, mirrors, lenses, optical path, Fermat's principle, etc. To derive one of the most fundamental principles in geometrical optics, it is appropriate to define a quantity called the optical path. The path d of a light ray in any medium is given by the product velocity times time i.e d=vt.

Reflection of paraxial rays by spherical mirrors and parabolic mirrors will be implemented.

Results involving refraction using Prism(including thin prism), Lens Formula, Lens Makers' Formula will also be covered in this part. We can not go much into geometrical optics as it would require graphical representations of the diagram. Only generalized results would be implemented.

It is common in Physics to describe fields in terms of variational principles. Perhaps the oldest variational principle in physics is Fermat's principle governing geometrical optics. It reduces to Snell's law of refraction for a special case. Refraction matrix, translation matrix, paraxial approximation and the cardinal points can be implemented using this.

The time T a point of the electromagnetic wave needs to cover a path between the points A and B is given by:
Fermat's Principle
c is the speed of light in vacuum, ds an infinitesimal displacement along the ray, v = ds/dt the speed of light in a medium and n = c/v the refractive index of that medium.

####Wave Optics Partial differential equations are central to our understanding of physics. By using SymPy's PDE solvers we can demonstrate the similarities and differences between the various equations of wave optics.

Following are some topics which will be covered under wave optics:

  • Classical Wave Equation, SHM
  • Superposition of waves

The essential characteristic of energy transport by waves is that waves obey the superposition principle. This means that two waves in the same spatial region can interfere, rearranging the energy in space in a pattern often quite different from that of either wave alone. Since light propagates as a wave, we will analyze this phenomenon.

  • Interference of two beams of lights
  • Fraunhofer Diffraction

By Single Slit We can calculate properties of Fraunhofer diffraction pattern produced by an infinitely long slit, of width b. A plane wave is assumed to fall normally on the slit, and we wish to calculate the intensity distribution on the focal plane of lens L.

fraunhofer single slit a

We can assume that the slit consists of a large number of equally spaced point sources and that each point on the slit is a source of Huygens’ secondary wavelets which interfere with the wavelets emanating from other points. Let the point sources be at A1, A2, A3, . . ., and let the distance between two consecutive points be Δ. hence b = (n-1)*Δ.

fraunhofer single slit a

We will now calculate the resultant field produced by these n sources at point P, with P being an arbitrary point (on the focal plane of the lens) receiving parallel rays making an angle q with the normal to the slit.

For an incident plane wave, points A1, A2,... are in phase, and, therefore, the additional path traversed by the disturbance emanating from point A2 will be A2A2', where A2' is the foot of the perpendicular drawn from A1 on A2B2. This follows from the fact that the optical paths A1B1P and A2'B2P are the same. If the diffracted rays make an angle q with the normal to the slit, then the path difference is A2A2' = Δ*sin(θ) and the corresponding phase difference φ = (2π/λ)*Δ*sin(θ).

  • Fresnel Diffraction

    Fresnel integrals, S(x) and C(x), are two transcendental functions that are used in optics, which are closely related to the error function (erf). They arise in the description of far field Fresnel diffraction phenomena, and are defined through the following integral representations: Fresnel's Integrals
    The simultaneous parametric plot of S(x) and C(x) is the Euler spiral (also known as the Cornu spiral or clothoid). These integrals are used in the calculation of total intensity at a point on Cornu's spiral.

  • Scattering and Dispersion

###Implementation Note: Prototype examples have been provided wherever necessary. These examples are just the prototypes. API architecture may vary from the way it is shown here, it will be decided after discussing it with my mentor(s).

####General Optics I'll start with vector representation of rays, planes. Using vectors I'll introduce specular reflection in which the direction of a reflected ray is determined by the vector of incidence and the surface normal vector.Given an incident direction di from the surface to the light source and the surface normal direction dn, the specularly reflected direction ds will be:

Specular Reflection

>>> from sympy import sqrt
>>> from sympy.physics.optics import *
>>> r1 = Ray(sqrt(3), sqrt(3), sqrt(3)) #A unit vector(Not necessarily) This Ray class will inherit Vector
>>> n = Vector(a, b, c)  #Normal vector to the plane
>>> p = Plane(x0,y0,z0, normal=n)  #Equation of a plane
>>> r2 = p.incident(r1)  #r2 will be the reflected ray
>>> m = SphericalMirror(f = 10, type="concave") #A concave mirror situated at origin with principal axis as x-axis
>>> r3 = m.incident(r1) #r3 will be the reflected ray

Cartesian sign convention will be followed everywhere dealing with the rays. ######Fermat's Principle and Snell's Law

>>> from sympy.physics.optics import *
>>> A = Point(a, b, c)  #3D Point
>>> B = Point(p, q, r)
>>> f = FermatsPrinciple(A=A,B=B,c=speed)  #This formula will be able to find any parameter when others are provided
>>> T = f.T  #Minimum time
>>> S = f.S  #Optical path

Similarly Snell's law will also take few parameters and compute others.

######Lens Maker's Formula

>>> from sympy.physics.optics import *
>>> mFormula = LensMakersFormula(n=a, n0=b, r1=c, r2=d)  #This formula will be able to find any parameter when others are provided
>>> P = mFormula.P  #Power of the lens

Similarly thin lens equation will also be implemented.

Optical systems, in general, are made up of a large number of refracting surfaces (as in a combination of lenses), and any ray can be traced through the system by using the Snell's law for a refracting surface. To obtain the position of the final image due to such a system, one has to calculate step by step the position of the image due to each surface. This process becomes complicated as the number of surfaces increase. We can solve this problem using matrix method in paraxial optics. This method indeed lends itself to direct use in computers for tracing rays through complicated optical systems. For full description please refer to page no. 68 of Ajoy Ghatak, Optics, Tata McGraw-Hill Education, 2005.

######Waves and superposition A class for transverse waves can be created by taking frequency and amplitude or other parameters as input and several operations can be done on it. This fundamental class will help in working on quantum optics in future. Other properties like phase angles, phase velocity, wave velocity, intensity, etc. will also be implemented.

The mathematical problem is: How do we find the wavefunction for the combined wave resulting from interference of two or more waves?

We can consider two harmonic transverse waves of the same frequency and wavelength, both moving along the x-axis, but differing in phase by the angle δ. The wavefunctions are the expressions describing the E-fields. We will assume the fields to oscillate in the y-direction, so by E we mean the y-component of E. We have for the two waves:

  • E1(x, t) = E01 * cos(k*x - ω*t)
  • E2(x, t) = E02 * cos(k*x - ω*t + δ)

The superposition of these waves will result in another harmonic wave with the same frequency and wavelength, moving in the x-direction, so E = E1 + E2 must have the form E(x, t) = E0 * cos(k*x - ω*t + φ). Our problem is to find the constants E0 and φ in terms of the amplitudes of the original waves and the phase difference δ. To solve this problem we can use Euler's theorem. For complete description please see reference #9.

>>> from sympy.physics.optics import *
>>> w1 = TWave(f=100, a=0.001)  #First Wave
>>> w2 = TWave(f=100, a=0.0001)  #Second Wave
>>> #Superposition
>>> w3 = w1 + w2    #Constructive Interference
>>> w4 = w1 - w2    #Destructive Interference

We can also plot these waves using internal plotting module of SymPy. I'll also work on this if I get time.

######Interference Consider this diagram. This diagram shows a basic setup of Young's double slit experiment popularly known as YDSE.

YDSE

Assuming S1 and S2 as two slits and OD at x-axis and OP at y-axis, O being origin, we can implement results for interference patterns.

Application of Fresnel's biprism method of interference is also similar to YDSE.

######Fraunhofer Diffraction As discussed in the theory we can very easily implement this.

>>> from sympy.physics.optics import *
>>> diffraction_angle = fraunhofer_diffraction(width=10**(-6), lambda = 10**(-9), D = 0.01, y = 0.02)

Similarly two slit and N-slit diffraction can be implemented. For more details please see Pg. No. 267 and 269 of reference #3.

I am not adding implementation details of Fresnel's diffraction because it is also similar to it. Although we can discuss exact API architecture in comments later.

Fresnel's integrals was already added 2 years back in SymPy(PR). So I'll just connect it with solving diffraction problem using Fresnel's Integrals.

Cauchy's equation and Sellmeier's equation are very easy to implement and require no descriptive implementation details beside API can be discussed later in comments according to the preference of my mentor.

###Timeline #####Community Bonding Period(April 21 - May 19) Goal : Community bonding

  • During this period I'll majorly focus on interacting with the contributors of sympy.physics.calculus and ode/pde solvers and ask guidance from my mentor which will make it easier for me to implement these things because solving equations is the most challenging part of my project.
  • I will start adding modules which will be required in the application of optics. These modules will be mathematics of 3D Points, Vectors and their operations(including projection and rotation), Planes, etc.

#####Week 1(May 19 - May 26) Goal: Implement Basic Geometrical Optics

  • In this week I'll start with simple things like rays, paraxial rays, fundamental principles of reflection and refraction and Fermat's principle
  • Introduction of spherical mirrors and parabolic mirrors

#####Week 2(May 27 - June 2) Goal: Adding more to Geometrical Optics

  • Addition of concepts like thin lens, lens formula and prism
  • Solving problems of combination of thin lenses using matrix method described in implementation above

#####Week 3(June 3 - June 9) Goal: Fix bugs, write docs and extensive tests

  • During this week I'll make sure that the work done in first two weeks are complete and foolproof.
  • I'll write descriptive docstrings, tests and fix bugs for the same
  • If time permits I'll add properties like Image Size, etc.

#####Week 4(June 10 - June 16) Goal: Start wave optics

  • During this week I'll write code for classical wave equation and simple harmonic motion
  • Introduction of properties like phase angles, phase velocity, wave velocity, amplitude, intensity, frequency and wavelength

#####Week 5 - Week 6(June 17 - June 30) Goal: Superposition of waves

  • This period will include following work
    • Addition of SHMs along the same line and right angles
    • Superposition of two/many waves as discussed in the implementation
    • Write docs and tests for implementations done so far

#####Week 7(July 1 - July 7) Goal: Add interference of two beams of lights

  • This period will include following work
    • Relations/results in Young's experiment (double source interference)
    • Fresnel's biprism method of interference
  • Write descriptive docs and extensive tests and bug fixes if any

####Midterm Evaluation

#####Week 8(July 8 - July 14) Goal: Continuing the interference

  • Add equation of intensity of fringe
  • Add concept of positions of the maxima and minima
  • Addition of following work related to interference involving multiple reflections
    • Newton's Rings
    • Complex amplitudes
    • Intensity function

#####Week 9(July 15 - July 21) Goal: Add Fraunhofer diffraction

  • This topic includes:
    • Diffraction by a single slit
    • Rectangular aperture and its intensity
    • Resolving powers

#####Week 10(July 22 - July 28) Goal Add Fresnel diffraction and scattering

  • During this period I'll add results involved in Fresnel diffraction including
    • Cornu's spiral
    • Fresnel's integrals
    • Solving diffraction problems using Fresnel's integrals
  • Add Rayleigh's law of scattering

#####Week 11(July 29 - August 4) Goal: Add dispersion and extensive docs and tests of last three weeks

  • Dispersion of a prism
  • Cauchy's equation
  • Sellmeier's equation

#####Week 12 - 13(August 5 - August 19) Goal: Write iPython notebooks for illustrations of a book for docs

  • In this week I'll write examples provided in a book of the topics which I'v already implemented during the summer in form of iPython notebooks. As suggested by Ondrej Certik, it would be a valuable resource for sympy docs.
  • In case of errors, I'll fix them simultaneously.

#####Post GSoC Post GSoC I'll be studying quantum optics and its application and usage in a CAS like SymPy. If it happens to be worthy, I'll add it also. Beside optics I can't think of anything specific as of now but I'll keep on fixing bugs, helping other people in merging their PRs in SymPy.

This idea struck me after the deadline of the proposal. I can also work on generating SVG of reflection/refraction phenomenon implemented throughout the summer although I'll have to do some research on this for wave optics.

###References

  1. W. Brouwer, Matrix Methods in Optical Instrument Design, Benjamin(1963)
  2. E. L. O'Neill, Introduction to Statistical Optics
  3. Ajoy Ghatak, Optics, Tata McGraw-Hill Education, 2005
  4. F. A. Jenkins, H. E. White, Fundamentals of Optics, McGRAW-HILL
  5. Max Born, Emil Wolf, Principles of Optics, Cambridge University Press
  6. https://en.wikipedia.org/wiki/Fermat%27s_principle
  7. https://en.wikipedia.org/wiki/Fresnel_integral
  8. Uses Made of Computer Algebra in Physics
  9. Superposition of Harmonic Waves
⚠️ **GitHub.com Fallback** ⚠️