Convert IPython Notebook to Slides

Convert and display in place:
jupyter-nbconvert --to slides mynotebook.ipynb --reveal-prefix=reveal.js --post serve

Convert to html:
jupyter-nbconvert --to slides mynotebook.ipynb --reveal-prefix=reveal.js

as always, try:
jupyter-nbconvert --help

Copying Lists in Python

make sure you don’t fall into the trap of copying pointers in Python:


import numpy as np
a = np.zeros(2)
print("a = ", a)
b = np.zeros(2)
print("b = ", b)
b = a # this assignment is simply a pointer copy - b points to the same data pointed to by a
a[0]=33.33 # this will actually change b as well
print("b = a results in a shallow copy:")
print("a = ", a)
print("b = ", b)
# to avoid this, do a deep copy:
print("To create a data copy, use b = a.copy() or b = list(a):")
a = np.zeros(2)
print("a = ", a)
b = np.zeros(2)
print("b = ", b)
print("b = a.copy or b = list(a) results in a data copy:")
b = a.copy() # this assignment will make a copy of the data
a[0]=33.33 # this will actually change b as well
print("a = ", a)
print("b = ",b)

How to Highlight Code in Keynote

First install a utility called highlight. It is available through macports and homebrew.

sudo port install highlight

Now that highlight is installed, you can “apply” it to a file and pipe it to the clipboard:

highlight -O rtf MyCode.cpp | pbcopy

Now go to Kenote and simply paste from clipboard (or command + v). Highlight also provides a ton of syntax highlighting styles. You can see those by typing:

highlight -w

You can apply a style with the -s or --style option:

highlight -O rtf MyInput.yaml --style darkblue | pbcopy

Refs:
http://jtimberman.housepub.org/blog/2015/02/10/awesome-syntax-highlighting-in-keynote/

C++ – Calling Overloaded Operator () from Pointer

Here are three ways to call a C++ classes’ operator () using a pointer:
Say you have a C++ class name MyClass that overloads the operator “()”, something along the lines:

Class MyClass{
...
public:
  void operator()(int a, double b);
}
MyClass* theClass = new MyClass;

// method 1: use a reference
MyClass& classRef = *theClass;
classRef(a, b);

// method 2: dereference in place
(*theClass)(a,b);

// method 3: dereference the ugly way:
theClass->operator()(a,b);

I use method 2.

Add to Calendar for Google Inbox

For those who have adopted inbox for their workflow, here’s how to add an event to your calendar.

  1. Your must be using google calendar & Chrom
  2. Download the google calendar extension and add it to Chrome. You will need to authorize your calendar.
  3. Back to inbox, select the text corresponding to your event (e.g. Thursday February 2, 2017 – 1:30 – 2:30 p.m., MEB 3105)
  4. Right click and you will see an “add to calendar” menu item

Good luck!

Unbounded Kinetic Energy in Forward-Euler Inviscid Flows

Observation

When using a forward-Euler method for the time integration of the momentum equation for an inviscid-flow, it appears that the kinetic energy of the flow grows unbounded in time, regardless of the timestep size.

Problem Statement

Estimate the change in total kinetic energy when using forward-Euler to integrate the Euler momentum equations in a periodic box.

Approach

To solve this problem, we need to do the following:

  1. Formulate a FE semi-discrete formula for the Euler equations.
  2. Construct the local kinetic energy (i.e. pointwise) by taking the dot product of the semi-discrete velocity field.
  3. Using this information, construct an equation for the change in total kinetic energy with time, e.g. ${\displaystyle \frac{\Delta K}{\Delta t}} $.
  4. Analyze the right-hand-side (RHS) of this equation in the context of a periodic box. If the RHS > 0, then the kinetic energy will increase.

Show Me the Math

It is best to use tensor notation for this problem. We start with the Euler equations

\begin{equation}
\frac{\partial u_{i}}{\partial x_{i}}=0
\end{equation}

\begin{equation}
\frac{\partial u_{i}}{\partial t}=-u_{j}\frac{\partial u_{i}}{\partial x_{j}}-\frac{\partial p}{\partial x_{i}}\equiv F_{i}
\end{equation}

where $i\in\{1,2,3\}$ denotes the $i^{\text{th}}$ spatial direction.

Using forward-euler time integration, we construct the semi-discrete formula

\begin{equation}
\frac{u_{i}^{n+1}-u_{i}^{n}}{\Delta t}+\mathcal{O}\left(\Delta t\right)=F_{i}^{n};\quad F_{i}^{n}\equiv u_{j}^{n}\frac{\partial u_{i}^{n}}{\partial x_{j}}+\frac{\partial p^{n}}{\partial x_{i}}
\end{equation}

where $t^{n+1}=t^{n}+\Delta t$ and $\Delta t$ is the fixed timestep size. For simplity of the exposition, we drop the “order” notation and set

\begin{equation}
u_{i}^{n+1}=u_{i}^{n}-\Delta t\, F_{i}^{n}\label{eq:ke-euler-discrete}
\end{equation}

Now, in a staggered arrangement, the kinetic energy lives at cell centers while the velocity components are stored on staggered volumes. For simpliciy, we assume a uniform, staggered grid. Consequently, the staggered-volumes coincide with the faces of the cell centered ones. Then, one calculates the local kinetic energy, $k$, as

\begin{equation}
k=\tfrac{1}{2}u_{i}u_{i}=\tfrac{1}{2}(u_{1}^{2}+u_{2}^{2}+u_{3}^{2})
\end{equation}

where Einstein summation is implied on repeated inidces. Note that the local kinetic energy is defined as the pointwise kinetic energy – the kinetic energy in every cell. Now, we can formulate the following sub-problem: given a discrete velocity field $u_{i}^{n}$ such that $\frac{\partial u_{i}^{n}}{\partial x_{i}}=0$ (discretely), estimate the kinetic energy, $ k^{n+1}$, at the next timestep. In other words, use \eqref{eq:ke-euler-discrete} to calculate $k^{n+1}$.

We start by multiplying \eqref{eq:ke-euler-discrete} through with $u_{i}^{n+1}$, we have
\begin{equation}
u_{i}^{n+1}u_{i}^{n+1}=u_{i}^{n+1}\left(u_{i}^{n}-\Delta t\, F_{i}^{n}\right)
\end{equation}

Or, using $k=\frac{1}{2}u_{i}u_{i}$, we have
\begin{equation}
2k^{n+1}=\left(u_{i}^{n}-\Delta t\, F_{i}^{n}\right)\left(u_{i}^{n}-\Delta t\, F_{i}^{n}\right)
\end{equation}

Expanding the right-hand-side, we get
\begin{equation}
2k^{n+1}=u_{i}^{n}u_{i}^{n}-2\Delta t\, u_{i}^{n}F_{i}^{n}+\Delta t^{2}F_{i}^{n}F_{i}^{n}
\end{equation}
or
\begin{equation}
2k^{n+1}=2k^{n}-2\Delta t\, u_{i}^{n}F_{i}^{n}+\Delta t^{2}F_{i}^{n}F_{i}^{n}\label{eq:kn1-0}
\end{equation}
We now rearrange \eqref{eq:kn1-0} as follows
\begin{equation}
\frac{k^{n+1}-k^{n}}{\Delta t}=-u_{i}^{n}F_{i}^{n}+\frac{1}{2}\Delta tF_{i}^{n}F_{i}^{n}
\end{equation}
or
\begin{equation}
\frac{\Delta k}{\Delta t}=-u_{i}^{n}F_{i}^{n}+\frac{1}{2}\Delta tF_{i}^{n}F_{i}^{n}\label{eq:dkdt-0}
\end{equation}

This equation tells us how much the implied kinetic energy changes with time. It is an implied kinetic energy equation given that it was constructed from the velocity field not from the transported kinetic energy.

We now focus our attention on the first term in the RHS of \eqref{eq:dkdt-0}, i.e. $u_{i}^{n}F_{i}^{n}$. Substituting for $F_{i}^{n}$, we have

\begin{equation}
u_{i}^{n}F_{i}^{n}=u_{i}^{n}\left(u_{j}^{n}\frac{\partial u_{i}^{n}}{\partial x_{j}}+\frac{\partial p^{n}}{\partial x_{i}}\right)=u_{i}^{n}u_{j}^{n}\frac{\partial u_{i}^{n}}{\partial x_{j}}+u_{i}^{n}\frac{\partial p^{n}}{\partial x_{i}}\label{eq:ui-fn}
\end{equation}

The purpose now is to try to convert this term into a divergence form. We will see why later. Starting with the first term $u_{i}^{n}u_{j}^{n}\frac{\partial u_{i}^{n}}{\partial x_{j}}$, we know that

\begin{equation}
\frac{\partial u_{i}^{n}u_{i}^{n}u_{j}^{n}}{\partial x_{j}}=u_{i}^{n}u_{i}^{n}\frac{\partial u_{j}^{n}}{\partial x_{j}}+u_{j}^{n}\frac{\partial u_{i}^{n}u_{i}^{n}}{\partial x_{j}}=u_{i}^{n}u_{i}^{n}\frac{\partial u_{j}^{n}}{\partial x_{j}}+2u_{i}^{n}u_{j}^{n}\frac{\partial u_{i}^{n}}{\partial x_{j}}
\end{equation}

But, assuming that continuity is satified discretely at every cell, we can set $\frac{\partial u_{j}^{n}}{\partial x_{j}}=0$. Then, we recover
\begin{equation}
u_{i}^{n}u_{j}^{n}\frac{\partial u_{i}^{n}}{\partial x_{j}}=\frac{1}{2}\frac{\partial u_{i}^{n}u_{i}^{n}u_{j}^{n}}{\partial x_{j}}=\frac{\partial k^{n}u_{j}^{n}}{\partial x_{j}}
\end{equation}
Lastly, the second term in \eqref{eq:ui-fn} is easily replaced by
\begin{equation}
u_{i}^{n}\frac{\partial p^{n}}{\partial x_{i}}=\frac{\partial u_{i}^{n}p^{n}}{\partial x_{i}}-p^{n}\frac{\partial u_{i}^{n}}{\partial x_{i}}
\end{equation}

Again, by assuming that continuity is satisfied discretely, we can drop the last term in the previous equation and use
\begin{equation}
u_{i}^{n}\frac{\partial p^{n}}{\partial x_{i}}=\frac{\partial u_{i}^{n}p^{n}}{\partial x_{i}}
\end{equation}

Upon substitution of the modified terms back into the implied kinetic energy equation \eqref{eq:dkdt-0}, we have

\begin{equation}
\frac{\Delta k}{\Delta t}=-\frac{\partial k^{n}u_{j}^{n}}{\partial x_{j}}-\frac{\partial u_{i}^{n}p^{n}}{\partial x_{i}}+\frac{1}{2}\Delta tF_{i}^{n}F_{i}^{n}
\end{equation}

In principle, for an inviscid flow in a period box, there should be no production or dissipation of total kinetic energy. The total kinetic energy is defined as the volumetric integral of $k$ over the domain, i.e.

\begin{equation}
\mathrm{K}=\int_{V}k\,\mathrm{d}V
\end{equation}

Since there is no production or dissipation in our problem, we expect that $\text{K}=const$ or

\begin{equation}
\frac{\partial}{\partial t}\int_{V}k\,\mathrm{d}V=0
\end{equation}

Looking at our semi-discrete equation, upon integration over the periodic box, we have

\begin{equation}
\frac{\Delta}{\Delta t}\int_{V}k\mathrm{d}V=-\int_{V}\frac{\partial k^{n}u_{j}^{n}}{\partial x_{j}}\mathrm{d}V-\int_{V}\frac{\partial u_{i}^{n}p^{n}}{\partial x_{i}}\mathrm{d}V+\frac{1}{2}\Delta t\int_{V}F_{i}^{n}F_{i}^{n}\mathrm{d}V
\end{equation}

Being periodic, we have, for any vector $\mathbf{v}$,

\begin{equation}
\int_{V}\nabla\cdot\mathbf{v}\mathrm{d}V=\int_{V}\frac{\partial v_{i}}{\partial x_{i}}\mathrm{d}V=\int_{\mathcal{S}}\mathbf{v}\cdot\mathbf{n}\mathrm{d}\mathcal{S}=0
\end{equation}

Then, all terms that are written in divergence form vanish identically. Hence, one is left with,

\begin{equation}
\frac{\Delta\mathrm{K}}{\Delta t}=\frac{1}{2}\Delta t\int_{V}F_{i}^{n}F_{i}^{n}\mathrm{d}V\geq0
\end{equation}

and therefore, the total kinetic energy grows unboundedly with time when using a forward-Euler scheme on the Euler equations, in a periodic box.

Cite as: Tony Saad, "Unbounded Kinetic Energy in Forward-Euler Inviscid Flows," in Dr. Saad's Notes, retrieved on June 30, 2016, http://www.tonysaad.net/notes/unbounded-kinetic-energy-in-forward-euler-inviscid-flows/.

Some Regex Notes

I happened to be dealing with a bunch of mallocs that look like this

double* f =  (double*)malloc(sizeof(double)*nx*ny*nz);
double* g = (double*)malloc(sizeof(double)*nx);
double* h = (double*)malloc(sizeof(double)*nx*nz);

I wanted to convert all of those those to use new, i.e.

double* f = new double[nx*ny*nz]

Using regex made all this possible

search for: \(double\*\)malloc\(sizeof\(double\)\*(.*)\)
replace with: new double[$1]

 

A Note on the Vector Form of the Navier-Stokes Equations


The Navier-Stokes equations, written in conservative vector form, are

\begin{equation}
\frac{\partial \rho \mathbf{u}}{\partial t} = -\nabla\cdot\mathbf{u}\rho\mathbf{u} – \nabla\cdot\boldsymbol{\tau} – \nabla p
\end{equation}

I always liked to use this form of the Navier-Stokes equations. I think it is simple and convenient. But, today I realized that it could easily lead to trouble, specifically when interpreting the convective term. For example, if one needs to expand the convective term things could easily get confusing. The reason is that the convective term, as written here, is known as a diadic – when two vectors are stacked next to each other $\mathbf{u}\rho \mathbf{u}$.  The divergence of a diadic is given by
\begin{equation}
\nabla\cdot\mathbf{A}\mathbf{B} = \mathbf{A}\cdot\nabla\mathbf{B} + \mathbf{A}\nabla\cdot\mathbf{B}
\end{equation}
But this may be confusing for the convection term since you can easily lose track of what A and B are. Here’s how I do it – but first let me remind you of a few things:

  1. It is essential to distinguish which quantity is being transported in the convective term. In the case of a conserved scalar $\rho\phi$, the convective term is $\nabla\cdot\mathbf{u}\rho\phi$. Here, $\mathbf{u}$ is the advective velocity and the quantity being transport is $\rho\phi$. While it is not wrong to write the convective term as $\nabla\cdot\rho\mathbf{u}\phi$ – using the former approach makes things a bit clearer.
  2. On to the momentum equations, and virtue of item 1, the convective term is $\nabla\cdot\mathbf{u}\rho\mathbf{u}$, the transported quantity is $\rho\mathbf{u}$ and the advective velocity is $\mathbf{u}$. But which velocity component does $\mathbf{u}$ correspond to in $\rho\mathbf{u}$?

To resolve this last issue, I use the following mixed tensor-vector notation

\begin{equation}
\frac{\partial \rho u_i}{\partial t} = -\nabla\cdot\mathbf{u}\rho u_i – \nabla\cdot\boldsymbol{\tau}_i – \frac{\partial p}{\partial x_i}
\end{equation}

I use this form whenever I want to derive the weak form of the momentum equations to avoid using diadics. When all is said and done, I switch back to the vector form for convenience.

Cite as: Tony Saad, "A Note on the Vector Form of the Navier-Stokes Equations," in Dr. Saad's Notes, retrieved on April 15, 2015, http://www.tonysaad.net/notes/a-note-on-the-vector-form-of-the-navier-stokes-equations/.

How to Sync Zotero with a Box Sync Enterprise Account

  • So the University of Utah provides us with 50 GB of storage with Box Sync.
  • Box Sync supports webdav natively.
  • On another note I use Zotero for my citation management.
  • The great thing about Zotero is that it allows your to store your library on your own webdav.

Combining the above I am able to store my entire Zotero library (~1.5 GB and counting) on Box Sync! Here are the steps:

Create an External Password for Box Sync

For Enterprise accounts like those provided by Universities, you MUST create what box calls an “External Password”. Do the following:

  • Login to your box account in a browser
  • Go to your profile (upper right where your name is shown)
  • Scroll to the bottom of that page and create the external password

Link Zotero to Box WebDav

  • Go to Zotero->Preferences
  • Choose the Sync Tab
  • For File Syncing choose WebDav
  • For URL: Choose https://dav.box.com/dav
  • For username: Choose your enterprise or university login credentials

and you’re done!

Zotero Box Sync