# Coordinate systems

## Introduction

This notebooks provides examples of curvilinear coordinates. Particularly,
it presents the coordinate surfaces for three common coordinate systems.

### Use of PyVista in this notebook

We use `PyVista` to represent the coordinate surfaces and `ìtkwidgets`
to render them interactively in the notebook.

If installing things manually, the following would be required


    conda install -c conda-forge pyvista
    conda install -c conda-forge itkwidgets


**More information:** https://docs.pyvista.org/examples/index.html


```python
import numpy as np
import pyvista as pv
from itkwidgets import view
```


```python
red = (0.9, 0.1, 0.1)
blue = (0.2, 0.5, 0.7)
green = (0.3, 0.7, 0.3)
```

## Cylindrical coordinates


The [ISO standard 80000-2](https://en.wikipedia.org/wiki/ISO_80000-2) recommends the use of $(ρ, φ, z)$, where $ρ$ is the radial coordinate, $\varphi$ the azimuth, and $z$ the height.

For the conversion between cylindrical and Cartesian coordinates, it is convenient to assume that the reference plane of the former is the Cartesian $xy$-plane (with equation $z=0$, and the cylindrical axis is the Cartesian $z$-axis. Then the $z$-coordinate is the same in both systems, and the correspondence between cylindrical $(\rho, \varphi)$ and Cartesian $(x, y)$ are the same as for polar coordinates, namely

\begin{align}
x &= \rho \cos \varphi \\
y &= \rho \sin \varphi
\end{align}

in one direction, and

\begin{align}
    \rho &= \sqrt{x^2+y^2} \\
    \varphi &= \begin{cases}
        0 & \mbox{if } x = 0 \mbox{ and } y = 0\\
        \arcsin\left(\frac{y}{\rho}\right) & \mbox{if } x \geq 0 \\	
        \arctan\left(\frac{y}{x}\right) & \mbox{if } x > 0 \\	
        -\arcsin\left(\frac{y}{\rho}\right) + \pi & \mbox{if } x < 0
    \end{cases}
\end{align}

in the other. It is also common to use [$\varphi = \mathrm{atan2}(y, x)$](https://en.wikipedia.org/wiki/Atan2).


```python
# Cylinder
phi, z = np.mgrid[0:2*np.pi:31j, -2:2*np.pi:31j]
x = 2*np.cos(phi)
y = 2*np.sin(phi)
cylinder = pv.StructuredGrid(x, y, z)
```


```python
# Vertical plane
rho, z = np.mgrid[0:3:31j, -2:2*np.pi:31j]
x = rho*np.cos(np.pi/4)
y = rho*np.sin(np.pi/4)
vert_plane = pv.StructuredGrid(x, y, z)
```


```python
# Horizontal plane
rho, phi = np.mgrid[0:3:31j, 0:2*np.pi:31j]
x = rho*np.cos(phi)
y = rho*np.sin(phi)
z = np.ones_like(x)
hor_plane = pv.StructuredGrid(x, y, z)
```


```python
view(geometries=[cylinder, vert_plane, hor_plane],
     geometry_colors=[blue, red, green])
```


    Viewer(geometries=[{'vtkClass': 'vtkPolyData', 'points': {'vtkClass': 'vtkPoints', 'name': '_points', 'numberO…


## Spherical coordinates

The use of $(r, θ, φ)$ to denote radial distance, inclination (or elevation), and azimuth, respectively, is common practice in physics, and is specified by [ISO standard 80000-2](https://en.wikipedia.org/wiki/ISO_80000-2).


The spherical coordinates of a point can be obtained from its [Cartesian coordinate system](https://en.wikipedia.org/wiki/ISO_80000-2) $(x, y, z)$

\begin{align}
r&=\sqrt{x^2 + y^2 + z^2} \\
\theta &= \arccos\frac{z}{\sqrt{x^2 + y^2 + z^2}} = \arccos\frac{z}{r} \\
\varphi &= \arctan \frac{y}{x}
\end{align}

The inverse tangent denoted in $\varphi = \arctan\left(\frac{y}{x}\right)$ must be suitably defined , taking into account the correct quadrant of $(x, y)$ (using the function ``atan2``).

Conversely, the Cartesian coordinates may be retrieved from the spherical coordinates (_radius_ $r$, _inclination_ $\theta$, _azimuth_ $\varphi$), where $r \in [0, \infty)$, $\theta \in [0, \pi]$ and $\varphi \in [0, 2\pi)$, by:

\begin{align}
x&=r \, \sin\theta \, \cos\varphi \\
y&=r \, \sin\theta \, \sin\varphi \\
z&=r \, \cos\theta
\end{align}


```python
theta, phi = np.mgrid[0:np.pi:21j, 0:np.pi:21j]
```


```python
# Sphere
x = np.sin(phi) * np.cos(theta)
y = np.sin(phi) * np.sin(theta)
z = np.cos(phi)
sphere = pv.StructuredGrid(x, y, z)
sphere2 =  pv.StructuredGrid(-x, -y, z)
```


```python
# Cone
x = theta/3 * np.cos(phi)
y = theta/3 * np.sin(phi)
z = theta/3
cone = pv.StructuredGrid(x, y, z)
cone2 = pv.StructuredGrid(-x, -y, z)
```


```python
# Plane
x = theta/np.pi
y = theta/np.pi
z = phi - np.pi/2
plane = pv.StructuredGrid(x, y, z)
```


```python
view(geometries=[sphere, sphere2, cone, cone2, plane],
     geometry_colors=[blue, blue, red, red, green],
     geometry_opacities=[1.0, 0.5, 1.0, 0.5, 1.0])
```


    Viewer(geometries=[{'vtkClass': 'vtkPolyData', 'points': {'vtkClass': 'vtkPoints', 'name': '_points', 'numberO…


## Ellipsoidal coordinates


```python
v, theta = np.mgrid[0:np.pi/2:21j, 0:np.pi:21j]
a = 3
b = 2
c = 1
```


```python
# Ellipsoid
lam = 3
x =  np.sqrt(a**2  + lam) * np.cos(v) * np.cos(theta)
y = np.sqrt(b**2  + lam)* np.cos(v) * np.sin(theta)
z = np.sqrt(c**2 + lam) * np.sin(v)
ellipsoid = pv.StructuredGrid(x, y, z)
ellipsoid2 = pv.StructuredGrid(x, y, -z)
ellipsoid3 = pv.StructuredGrid(x, -y, z)
ellipsoid4 = pv.StructuredGrid(x, -y, -z)
```


```python
# Hyperboloid of one sheet
mu = 2
x = np.sqrt(a**2 + mu) * np.cosh(v) * np.cos(theta)
y = np.sqrt(b**2 + mu) * np.cosh(v) * np.sin(theta)
z = np.sqrt(c**2 + mu) * np.sinh(v)
z = np.sqrt(c**2 + mu) * np.sinh(v)
hyper = pv.StructuredGrid(x, y, z)
hyper2 = pv.StructuredGrid(x, y, -z)
hyper3 = pv.StructuredGrid(x, -y, z)
hyper4 = pv.StructuredGrid(x, -y, -z)
```


```python
# Hyperboloid of two sheets
nu = 1
x = np.sqrt(a**2 + nu) * np.cosh(v)
y = np.sqrt(c**2 + nu) * np.sinh(v) * np.sin(theta)
z = np.sqrt(b**2 + nu) * np.sinh(v) * np.cos(theta)
hyper_up = pv.StructuredGrid(x, y, z)
hyper_down = pv.StructuredGrid(-x, y, z)
hyper_up2 = pv.StructuredGrid(x, -y, z)
hyper_down2 = pv.StructuredGrid(-x, -y, z)
```


```python
view(geometries=[ellipsoid, ellipsoid2, ellipsoid3, ellipsoid4,
                 hyper, hyper2, hyper3, hyper4,
                 hyper_up, hyper_down, hyper_up2, hyper_down2],
     geometry_colors=[blue]*4 + [red]*4 + [green]*4)
```


    Viewer(geometries=[{'vtkClass': 'vtkPolyData', 'points': {'vtkClass': 'vtkPoints', 'name': '_points', 'numberO…


## References

1. Wikipedia contributors. ["Cylindrical coordinate system."](https://en.wikipedia.org/wiki/Cylindrical_coordinate_system) Wikipedia, The Free Encyclopedia. Wikipedia, The Free Encyclopedia, 12 Dec. 2016. Web. 9 Feb. 2017.

2. Wikipedia contributors. ["Spherical coordinate system."](https://en.wikipedia.org/wiki/Spherical_coordinate_system) Wikipedia, The Free Encyclopedia. Wikipedia, The Free Encyclopedia, 29 Jan. 2017. Web. 9 Feb. 2017.

3. Sullivan et al., (2019). [PyVista: 3D plotting and mesh analysis through a streamlined interface for the Visualization Toolkit (VTK)](https://joss.theoj.org/papers/10.21105/joss.01450). Journal of Open Source Software, 4(37), 1450, https://doi.org/10.21105/joss.01450


```python
from IPython.core.display import HTML
def css_styling():
    styles = open('./styles/custom_barba.css', 'r').read()
    return HTML(styles)
css_styling()
```





<link href='http://fonts.googleapis.com/css?family=Fenix' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Alegreya+Sans:100,300,400,500,700,800,900,100italic,300italic,400italic,500italic,700italic,800italic,900italic' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Source+Code+Pro:300,400' rel='stylesheet' type='text/css'>
<style>
/* Based on Lorena Barba template available at: https://github.com/barbagroup/AeroPython/blob/master/styles/custom.css*/
@font-face {
font-family: "Computer Modern";
src: url('http://mirrors.ctan.org/fonts/cm-unicode/fonts/otf/cmunss.otf');
}
div.cell{
width:800px;
margin-left:16% !important;
margin-right:auto;
}
h1 {
font-family: 'Alegreya Sans', sans-serif;
}
h2 {
font-family: 'Fenix', serif;
}
h3{
font-family: 'Fenix', serif;
margin-top:12px;
margin-bottom: 3px;
}
h4{
font-family: 'Fenix', serif;
}
h5 {
font-family: 'Alegreya Sans', sans-serif;
}	
div.text_cell_render{
font-family: 'Alegreya Sans',Computer Modern, "Helvetica Neue", Arial, Helvetica, Geneva, sans-serif;
line-height: 135%;
font-size: 120%;
width:600px;
margin-left:auto;
margin-right:auto;
}
.CodeMirror{
font-family: "Source Code Pro";
font-size: 90%;
}
/* .prompt{
display: None;
}*/
.text_cell_render h1 {
font-weight: 200;
font-size: 50pt;
line-height: 100%;
color:#CD2305;
margin-bottom: 0.5em;
margin-top: 0.5em;
display: block;
}	
.text_cell_render h5 {
font-weight: 300;
font-size: 16pt;
color: #CD2305;
font-style: italic;
margin-bottom: .5em;
margin-top: 0.5em;
display: block;
}
.warning{
color: rgb( 240, 20, 20 )
}
</style>








```python

```

763580f8371381e7ea3712e7f1fcd11a318cddf7
ipynb
Jupyter Notebook
notebooks/vector_calculus-pyvista.ipynb
nicoguaro/AdvancedMath
2749068de442f67b89d3f57827367193ce61a09c
MIT
2017-06-29T17:45:20.000Z
2022-02-06T20:14:29.000Z
notebooks/vector_calculus-pyvista.ipynb
nicoguaro/AdvancedMath
2749068de442f67b89d3f57827367193ce61a09c
MIT
notebooks/vector_calculus-pyvista.ipynb
nicoguaro/AdvancedMath
2749068de442f67b89d3f57827367193ce61a09c
MIT
2019-04-22T08:08:56.000Z
2022-01-27T08:15:53.000Z
Qwen/Qwen-72B
1. YES
2. YES