CoCalc Public FilesWorkshop-1-CF / Workshop1-Classical_fields.ipynbOpen with one click!
Authors: Phoenix Everill, Moustafa Gharamti, Ciarán McDonnell, Bruce Saleeb-Mousa
Views : 44
Compute Environment: Ubuntu 20.04 (Default)

Workshop 1

PHYS2003 Classical Fields

Week Two

In this module, you will work a lot with scalar and vector fields. We will expect you to be able to use pen and paper to do tasks such as sketching vector fields and contours of scalar fields. However, it is also useful to be able to use a computer to do these things. In this workshop, you will explore how fields can be plotted in Python.

If you don't have your 1st year Python book with you, then you may need to refer to the online notes from the first year computing web pages to remind yourself of the basic syntax.

Scaler Fields

In [ ]:

In lecture 11, you saw that a scalar field is any function of position which has a scalar value at each point r=(x,y,z)\bm{r}=(x, y, z). We usually visualize scalar fields by plotting contours over which the scalar function is a constant. You should be familiar with contour lines on maps, so we will consider geographical contours as our examples.
A particular hill is described mathematically in plane polar co-ordinates (r,θ)(r, θ) by the expression F=her2F=he^{-r^2} where FF is the height above sea level (F=0)(F = 0), hh is the height of the summit and r=x2+y2r=\sqrt{x^2+y^2}.

  1. Geographical maps plot contours corresponding to regular increments in height. For example, on the OS map of the campus area in the lecture notes, you can see that contours of the hill in Wollaton Park are drawn at 45m45m, 50m50m and 55m55m above sea level. Contours are closer together where hills are steepest, because it is not necessary to move as far horizontally for a given change in height than it is where ground is less steep.
    Write a Python program to plot FF as a function of xx for y=0y = 0 and h=1h=1. Using this plot, try to identify where a map would have contours close together and where they would be further apart. Note that for this module, you will not need to worry about relative separations of contours unless explicitly asked to do so.
In [8]:
import numpy as np import matplotlib.pyplot as plt x = np.arange(-2,2,0.01) y = 0 h = 1 r = np.sqrt(x**2+y**2) f = h*np.exp(-r**2) plt.plot(x,f)
[<matplotlib.lines.Line2D at 0x7f50ab1403a0>]
  1. Use Python to plot contours of the hill for h=1h = 1 (say). Note that you will only evaluate the function at specific points, which are then joined by straight lines. Therefore the contours may not appear to be smooth. Choose sufficient points so that the contours are fairly smooth, but not so many that the matrix sizes are huge. Note that finding a suitable number of points is best determined by trial and error.
    Python chooses contours with equal ‘height’ separations, like on a map. Do the shapes of the contours and their relative separations agree with your predictions from parts (a) and (b)?
In [16]:
import numpy as np import matplotlib.pyplot as plt x = np.arange(-2,2,0.1) y = np.arange(-2,2,0.1) [x1,y1]=np.meshgrid(x,y) h = 1 r = np.sqrt(x1**2+y1**2) f = h*np.exp(-r**2) plt.contour(x1,y1,f)
<matplotlib.contour.QuadContourSet at 0x7f50ab083d00>

Vector Fields

Vector fields are functions of position that have a vector value at each point r\bm{r}. We saw in the guided-learning booklet that vector fields are represented by arrows, as with wind velocities on weather maps. The field at a point is represented by an arrow, such that the magnitude is indicated by the length of the arrow and the direction is indicated by the direction of the arrow. Python can easily plot vector fields using the functions quiver in 2D and quiver3 in 3D. Why is it called quiver? Easy - in archery, a quiver is a container for arrows!

More specifically, quiver(x,y,u,v) will display arrows representing a vector with components (u,v)(u, v) at points (x,y)(x, y). xx and yy can be set up using meshgrid. Note that the Python help calls uu and vv ‘velocity vectors’, but uu and vv can represent any vector field. Note also that Python draws arrows such that the base of the arrow is at the point being represented. Some other computer programs place the middle of the arrow at the point represented.
Consider the vector field F=exp(y2)(ij)\mathbf{F}=exp(-y^2)(\mathbf{i}-\mathbf{j})
  1. By looking at equation of F\mathbf{F}, determine the directions and qualitatively estimate relative magnitudes of the vector field on a rectangular grid of points. Enter you answer as text in the box below by pressing on edit at the right corner of the box.
In [ ]:

For small values of y, large arrows with bearing 135 degrees and with arrows decreasing in magnitude but maintaining the same direction as y increases.

  1. Relating to the notation quiver(x,y,u,v), we can see that we need u=exp(y2)u = exp(−y^2) and v=exp(y2)v = −exp(−y^2) for this problem. (Make sure you understand why.) Use the quiver function to plot the vector field. You will need to choose a suitable grid of xx and yy values. Do the results agree with your prediction? If not, can you see why what you thought was wrong? Ask if you need an explanation.
In [24]:
q=np.arange(-2,2,0.2) [x,y]=np.meshgrid(q,q) u=np.exp(-y**2) v=-np.exp(-y**2) plt.figure(3,figsize=(4,3)) ax3=plt.axes([-2,-2,2,2]) ax3.quiver(x,y,u,v) plt.xlabel('x') plt.ylabel('y')
Text(0, 0.5, 'y')
  1. Python automatically chooses the lengths of the arrows to fit the chosen grid. This can be altered using an optional scale parameter. Re-plot your vector field with the scale factor such that the arrows are half the length of your previous plot.
In [32]:
q=np.arange(-2,2,0.2) [x,y]=np.meshgrid(q,q) u=np.exp(-y**2) v=-np.exp(-y**2) plt.figure(3,figsize=(4,3)) ax3=plt.axes([-2,-2,2,2]) ax3.quiver(x,y,u,v,angles='xy', scale_units='xy', scale=10) plt.xlabel('x') plt.ylabel('y')
Text(0, 0.5, 'y')
  1. Experiment with choosing different separations of the grid points for xx and yy.
In [35]:
q=np.arange(-2,2,0.1) [x,y]=np.meshgrid(q,q) u=np.exp(-y**2) v=-np.exp(-y**2) plt.figure(3,figsize=(4,3)) ax3=plt.axes([-2,-2,2,2]) ax3.quiver(x,y,u,v,angles='xy', scale_units='xy', scale=10) plt.xlabel('x') plt.ylabel('y')
Text(0, 0.5, 'y')


Do as much of the following as you have time for.

First Exercise

  • A landscape with two adjacent summits is described by the function
  • G=h(e(x2+y2)+e[(x2)2+y2])G = h\left(\mathrm{e}^{-(x^2 + y^2)} + \mathrm{e}^{-[(x - 2)^2 + y^2]}\right)
    1. Close to the summit of one hill, the contribution to the height of the other hill is negligible. Use Python to plot GG for y=0y=0 and h=1h=1. Can you predict (without doing any calculations) where there will be a saddle point (i.e. a point where the height increases if we move in some directions but decreases if we move in other directions)?
    In [ ]:
    1. Use Python to plot contours of the landscape represented by equation of GG. Do the shapes of the contours and the locations of the summits and saddle point agree with your predictions?
    In [ ]:
    1. Python can label the contours with their contour values. Look up the clabel function from matlibplot.pyplot.axes() and use the information there to label your contours in (b).
    In [ ]:

    Second Exercise

  • In plane polar coordinates, we can define unit vectors r^\hat{\mathbf{r}} and θ^\hat{\bm{\theta}} to point in directions of increasing rr and θ\theta respectively. They can be expressed in Cartesian coordinates as

    r=xri+yrj,\mathbf{r} = \frac{x}{r}\mathbf{i} + \frac{y}{r}\mathbf{j},
    θ^=yri+xrj,\hat{\bm{\theta}} = -\frac{y}{r} \mathbf{i} + \frac{x}{r}\mathbf{j},
    where r=x2+y2r = \sqrt{x^2+y^2}.
  • A vector field representing a particular circular fluid flow is expressed as

    F=arθ^,\displaystyle \mathbf{F} = \frac{a}{r} \hat{\bm{\theta}},
    where aa is a constant.
    1. Verify that the expression for F\mathbf{F} in Cartesian coordinates is

      F=ayr2i+axr2j,\mathbf{F} = -\frac{ay}{r^2}\mathbf{i} + \frac{ax}{r^2}\mathbf{j},

      and hence identify the appropriate quantities to use for uu and vv in quiver for this problem.


    1. Use Python to plot the vector field on a rectangular grid of points set up using meshgrid. Choose a=1a=1. As F\mathbf{F} is infinite at the origin, it is best not to include the origin in your grid points. If you do include the origin, some functions will still work but others will give problems. A suitable grid definition is w = np.arange(-1.1, 1.12, 0.2)
      Your result should show the circular nature of the flow. Remember (from last year) that if you use set_aspect('equal') the result should be displayed with equal scaling of the xx and yy axes (i.e. the axis frame should appear as a square rather than a rectangle and the flow as a circle rather than as an ellipse). However, if you have a wide screen monitor and the resolution is set incorrectly, your circle will still look like an ellipse.
    In [ ]:
    1. Now consider the vector field F=arθ^+br^\displaystyle\mathbf{F} = \frac{a}{r} \hat{\bm{\theta}} + b \hat{\mathbf{r}}, where aa and bb are both constants. As there is a radial component in addition to the circular motion, this represents a spiral flow.
      Use Python to plot the vector field for a=1a=1 and b=2b = 2. Do the results show a spiral flow as expected? What do you expect the field to look like when b=2b = -2? Check in Python.
    In [ ]:

    In this worksheet, you have explored how Python and a pen-and-paper can be used to help visualise scalar and vector fields. Worksheet 1 on Moodle contains additional written and Python questions. If you finish this Workshop with time to spare, you may wish to look at this now.} Alternatively, you could experiment with plotting vector and scalar fields in Wolfram Alpha. Although you will not be formally taught how to do that in this module, you may find it a useful way of quickly visualising fields.

    Model answers to workshops will be added to your CoCalc directory and will be available on Moodle after you have had time to attempt the questions yourselves.