I am working on the file "ME249-Lecture-0.ipynb".
The first task is:
The discretization of the time derivative is crude. A better discretization is the 2nd-order Runge-Kutta:

\begin{eqnarray}
\phi_i^{n+1/2}&=&\phi_i^n-\frac{\Delta t}{2}\frac{F^n_{i+\frac{1}{2}} - F^n_{i-\frac{1}{2}}}{\Delta x}\\
\phi_i^{n+1}&=&\phi_i^n-\Delta t\frac{F^{n+1/2}_{i+\frac{1}{2}} - F^{n+1/2}_{i-\frac{1}{2}}}{\Delta x}
\end{eqnarray}

Can you write Python code for it?

N=200
Simulation_time = 5.
dx = L/N
x_phi = np.linspace(dx/2.,L-dx/2.,N)
x_u = np.linspace(0.,L,N+1)
u_0 = 1.
num_scheme = 'CS'
u = u_0*np.ones(N+1)
phi = np.zeros(N)
flux = np.zeros(N+1)
divflux = np.zeros(N)
phiold = np.zeros(N)

phi = init_simulation(x_phi,N)
phi_init = phi.copy()
rk_coef = np.array([0.5,1.])

number_of_iterations = 100
dt = Simulation_time/number_of_iterations

t = 0.
for it in range (number_of_iterations):
    phiold = phi
    for irk in range(2):
        flux = compute_flux_advanced(phi,u,N,num_scheme)
        divflux = flux_divergence(flux,N,dx)
        phi = phiold-rk_coef[irk]*dt*divflux
    t += dt
plt.plot(x_phi,phi,lw=2,label='simulated')
plt.plot(x_phi,phi_init,lw=2,label='initial')
plt.legend(loc=2, bbox_to_anchor=[0, 1],
           ncol=2, shadow=True, fancybox=True)
plt.xlabel('$x$', fontdict = font)
plt.ylabel('$\phi$', fontdict = font)
plt.xlim(0,L)
plt.show()


I am working on the file "ME249-Lecture-0.ipynb".
The first task is:
The first two lines deal with the ability to show your graphs (generated via matplotlib) within this notebook, the remaining two lines import matplotlib's sublibrary pyplot as plt and numpy as np.

Step 2: Initialization of variables and allocations of memory

The first real coding task is to define your variables, with the exception of the time-related variables (you will understand why). Note that in our equation, we can store $\phi^n$ into one variable providing that we create a flux variable $F$.

 Q1: Explain why.

Can you write Python code for it?

L = 8*np.pi
N = 200
dx = L/N
u_0 = 1.

phi = np.zeros(N)
F = np.zeros(N+1)
u = u_0*np.ones(N+1)
x_phi = np.linspace(dx/2.,L-dx/2.,N)
x_u = np.linspace(0.,L,N+1)


I am working on the file "ME249-Lecture-3.ipynb".
The first task is:

 Comparison with Finite Difference Derivatives

When the number of Fourier node is sufficient to capture all scales, derivatives computed in the Fourier space are essentially exact. The following code compares the exact first derivative with a first order upwind scheme:
$$
\left.\frac{\delta u}{\delta x}\right\vert_i=\frac{u_i-u_{i-1}}{\Delta x}
$$
For finite difference derivative, we will use the symbol $\delta/\delta x$ rather than $d/dx$.

Show that the scheme is first order and write the leading term in the truncation error.

Can you write Python code for it?

du_fd = np.zeros(Nx,dtype='float64')
dx = Lx/Nx
du_fd[1:Nx] = (u[1:Nx]-u[0:Nx-1])/dx
du_fd[0] = (u[0]-u[Nx-1])/dx
plt.plot(x,du_fd,'r-',lw=2,label='FD derivative')
plt.plot(x,du,'b--',lw=2,label='exact derivative')
plt.legend(loc=3, bbox_to_anchor=[0, 1],
           ncol=3, shadow=True, fancybox=True)
plt.ylabel('$du/dx$',fontsize = 18)
plt.xlabel('$x$',fontsize = 18)
plt.xticks(fontsize = 16)
plt.yticks(fontsize = 16)
plt.xlim(0,Lx)
plt.show()
print('error:',np.linalg.norm(du_fd-du,np.inf))


I am working on the file "ME249-Lecture-3.ipynb".
The first task is:

Derivation in Fourier Space

Going back to the Fourier series,
$$
u(x) = \sum_{n=-\infty}^{\infty}a_n\exp\left(\hat{\jmath}\frac{2\pi nx}{L_x}\right)=\sum_{n=-\infty}^{\infty}a_n\exp\left(\hat{\jmath}k_nx\right)
$$
with
$$
k_n=\frac{2\pi n}{L_x}\,
$$
it is obvious that any $m$ derivative of the real variable $u$ be:
$$
\frac{d^mu}{dx^m} = \sum_{n=-\infty}^{\infty}\left(\hat{\jmath}k_n\right)^ma_n\exp\left(\hat{\jmath}k_nx\right)
$$
In other words, if $u_n$ is defined as:
$$
u_n(x) = a_n\exp\left(\hat{\jmath}k_nx\right)
$$
then
$$
\frac{d^mu_n}{dx^m} = \left(\hat{\jmath}k_n\right)^m u_n\,.
$$
A $m$ derivativation in the Fourier space amounts to the multiplication of each Fourier coefficient $a_n$ by $\left(\hat{\jmath}k_n\right)^m$.
The following is a code for the first derivative of $u$:
Can you write Python code for it?

u_hat = np.fft.fft(u)
k = np.hstack((np.arange(0,Nx/2+1),np.arange(-Nx/2+1,0)))
ik = 1j*k
v_hat = ik*u_hat

v = np.real(np.fft.ifft(v_hat))
plt.plot(x,v,'r-',lw=2,label='$FT^{-1}[\hat{\jmath}kFT[u]]$')
plt.plot(x,du,'b--',lw=2,label='exact derivative')
plt.legend(loc=3, bbox_to_anchor=[0, 1],
           ncol=3, shadow=True, fancybox=True)
plt.ylabel('$du/dx$',fontsize = 18)
plt.xlabel('$x$',fontsize = 18)
plt.xticks(fontsize = 16)
plt.yticks(fontsize = 16)
plt.xlim(0,Lx)
plt.show()
print('error:',np.linalg.norm(v-du))

mk2 = ik*ik
v_hat = mk2*u_hat

v = np.real(np.fft.ifft(v_hat))
plt.plot(x,v,'r-',lw=2,label='$FT^{-1}[-k^2FT[u]]$')
plt.plot(x,ddu,'b--',lw=2,label='exact derivative')
plt.legend(loc=3, bbox_to_anchor=[0, 1],
           ncol=3, shadow=True, fancybox=True)
plt.ylabel('$d^2u/dx^2$',fontsize = 18)
plt.xlabel('$x$',fontsize = 18)
plt.xticks(fontsize = 16)
plt.yticks(fontsize = 16)
plt.xlim(0,Lx)
plt.show()
print('error:',np.linalg.norm(v-ddu))


I am working on the file "ME249-Lecture-3.ipynb".
The first task is:

 High-Pass Filter

From the example below, develop a function for a high-pass filter.

Can you write Python code for it?

u_hat = np.fft.fft(u)
kfilter = Nwave/2
k = np.linspace(0,Nx-1,Nx)
filter_mask = np.where((k < kfilter) | (k > Nx-kfilter) )
u_hat[filter_mask] = 0.+0.j
v = np.real(np.fft.ifft(u_hat))
plt.plot(x,v,'r-',lw=2,label='Filtered (FT)')
plt.plot(x,np.sum(uwave[:,kfilter:Nwave+1],axis=1),'b--',lw=2,label='Filtered (exact)')
plt.plot(x,u,'g:',lw=2,label='original')
plt.legend(loc=3, bbox_to_anchor=[0, 1],
           ncol=3, shadow=True, fancybox=True)
plt.xticks(fontsize = 16)
plt.yticks(fontsize = 16)
plt.xlim(0,Lx)
plt.show()

F = np.zeros(Nx/2+1,dtype='float64')
F_filter = np.zeros(Nx/2+1,dtype='float64')
u_hat = np.fft.fft(u)
F = np.real(u_hat[0:Nx/2+1]*np.conj(u_hat[0:Nx/2+1]))
v_hat = np.fft.fft(v)
F_filter = np.real(v_hat[0:Nx/2+1]*np.conj(v_hat[0:Nx/2+1]))
k = np.hstack((np.arange(0,Nx/2+1),np.arange(-Nx/2+1,0)))
plt.loglog(k[0:Nx/2+1],F,'r-',lw=2,label=r'$\Phi_u$')
plt.loglog(k[0:Nx/2+1],F_filter,'b-',lw=2,label=r'$\Phi_v$')
plt.legend(loc=3, bbox_to_anchor=[0, 1],
           ncol=3, shadow=True, fancybox=True)
plt.xlabel('$k$', fontdict = font)
plt.ylabel('$\Phi(k)$', fontdict = font)
plt.xticks(fontsize = 16)
plt.yticks(y_ticks,fontsize = 16)
plt.show()


I am working on the file "ME249-Lecture-3.ipynb".
The first task is:

Low-Pass Filter

The following code filters the original signal by half the wavenumbers using FFT and compares to exact filtered function
Can you write Python code for it?

# filtering the smaller waves
def low_pass_filter_fourier(a,k,kcutoff):
    N = a.shape[0]
    a_hat = np.fft.fft(u)
    filter_mask = np.where(np.abs(k) > kcut)
    a_hat[filter_mask] = 0.0 + 0.0j
    a_filter = np.real(np.fft.ifft(a_hat))
    return a_filter
kcut=Nwave/2+1
k = np.hstack((np.arange(0,Nx/2+1),np.arange(-Nx/2+1,0)))
v = low_pass_filter_fourier(u,k,kcut)
u_filter_exact = np.sum(uwave[:,0:kcut+1],axis=1)
plt.plot(x,v,'r-',lw=2,label='filtered with fft')
plt.plot(x,u_filter_exact,'b--',lw=2,label='filtered (exact)')
plt.plot(x,u,'g:',lw=2,label='original')
plt.legend(loc=3, bbox_to_anchor=[0, 1],
           ncol=3, shadow=True, fancybox=True)
plt.xlabel('$x$', fontdict = font)
plt.ylabel('$u$', fontdict = font)
plt.show()
print('error:',np.linalg.norm(v-u_filter_exact,np.inf))

F = np.zeros(Nx/2+1,dtype='float64')
F_filter = np.zeros(Nx/2+1,dtype='float64')
u_hat = np.fft.fft(u)
F = np.real(u_hat[0:Nx/2+1]*np.conj(u_hat[0:Nx/2+1]))
v_hat = np.fft.fft(v)
F_filter = np.real(v_hat[0:Nx/2+1]*np.conj(v_hat[0:Nx/2+1]))
k = np.hstack((np.arange(0,Nx/2+1),np.arange(-Nx/2+1,0)))
plt.loglog(k[0:Nx/2+1],F,'r-',lw=2,label='F_u')
plt.loglog(k[0:Nx/2+1],F_filter,'b-',lw=2,label='F_v')
plt.legend(loc=3, bbox_to_anchor=[0, 1],
           ncol=3, shadow=True, fancybox=True)
plt.xlabel('$k$', fontdict = font)
plt.ylabel('$F_u(k)$', fontdict = font)
plt.show()
plt.show


# filtering the smaller waves
def low_pass_filter_fourier(a,k,kcutoff):
    N = a.shape[0]
    a_hat = np.fft.fft(a)
    filter_mask = np.where(np.abs(k) > kcut)
    a_hat[filter_mask] = 0.0 + 0.0j
    a_filter = np.real(np.fft.ifft(a_hat))
    return a_filter
kcut=Nwave/2+1
k = np.hstack((np.arange(0,Nx/2+1),np.arange(-Nx/2+1,0)))
v = low_pass_filter_fourier(u,k,kcut)
u_filter_exact = np.sum(uwave[:,0:kcut+1],axis=1)
plt.plot(x,v,'r-',lw=2,label='filtered with fft')
plt.plot(x,u_filter_exact,'b--',lw=2,label='filtered (exact)')
plt.plot(x,u,'g:',lw=2,label='original')
plt.legend(loc=3, bbox_to_anchor=[0, 1],
           ncol=3, shadow=True, fancybox=True)
plt.xlabel('$x$', fontdict = font)
plt.ylabel('$u$', fontdict = font)
plt.xticks(fontsize = 16)
plt.yticks(fontsize = 16)
plt.show()
print('error:',np.linalg.norm(v-u_filter_exact,np.inf))

F = np.zeros(Nx/2+1,dtype='float64')
F_filter = np.zeros(Nx/2+1,dtype='float64')
u_hat = np.fft.fft(u)
F = np.real(u_hat[0:Nx/2+1]*np.conj(u_hat[0:Nx/2+1]))
v_hat = np.fft.fft(v)
F_filter = np.real(v_hat[0:Nx/2+1]*np.conj(v_hat[0:Nx/2+1]))
k = np.hstack((np.arange(0,Nx/2+1),np.arange(-Nx/2+1,0)))
plt.loglog(k[0:Nx/2+1],F,'r-',lw=2,label='$\Phi_u$')
plt.loglog(k[0:Nx/2+1],F_filter,'b-',lw=2,label='$\Phi_v$')
plt.legend(loc=3, bbox_to_anchor=[0, 1],
           ncol=3, shadow=True, fancybox=True)
plt.xlabel('$k$', fontdict = font)
plt.ylabel('$\Phi_u(k)$', fontdict = font)
plt.xticks(fontsize = 16)
plt.yticks(y_ticks,fontsize = 16)
plt.show()


I am working on the file "ME249-Lecture-3.ipynb".
The first task is:

Spectrum

For now we will define the spectrum f $f$ as

$$
F(k_n) = \hat{f}_n.\hat{f}_n^*
$$

which can be interpreted as the energy contained in the $k_n$ wavenumber. This is helpful when searching for the most energitic scales or waves in our system. Thanks to the symmetries of the FFT, the spectrum is defined over $n=0$ to $N_x/2$
Can you write Python code for it?

F = np.zeros(Nx/2+1,dtype='float64')
F = np.real(u_hat[0:Nx/2+1]*np.conj(u_hat[0:Nx/2+1]))
k = np.hstack((np.arange(0,Nx/2+1),np.arange(-Nx/2+1,0)))
plt.loglog(k[0:Nx/2+1],F,'r-',lw=2,label='F_u')
plt.legend(loc=3, bbox_to_anchor=[0, 1],
           ncol=3, shadow=True, fancybox=True)
plt.xlabel('$k$', fontdict = font)
plt.ylabel('$F_u(k)$', fontdict = font)
plt.show()
plt.show


I am working on the file "ME249-Lecture-3.ipynb".
The first task is:

- Explain why $\omega\Delta x$ can be defined from $0$ to $\pi$.

- Write a code to illustrate the effects of the imaginary part and real part on the derivative on the following function
$$
f(x) = cos(nx)
$$
defined for $x\in[0,2\pi]$, discretized with $N$ points. Study a few $n$, ranging from large scales to small scales. Note the two effects we seek to identify are phase change and amplitude change.

- Derive the modified wavenumber for the second order central finite difference scheme
$$
\frac{\delta f}{\delta x}=\frac{f_{i+1}-f_{i-1}}{2\Delta x}
$$

- Create a second order upwind scheme and derive the modified wavenumber. Compare the performance of the first order and the second order schemes. For the second order upwind scheme, find $a$, $b$ and $c$ such that
$$
\frac{\delta f}{\delta x}=\frac{af_{i-2}+bf_{i-1}+cf_i}{\Delta x}
$$

Can you write Python code for it?

!ipython nbconvert --to html ME249-Lecture-3-YOURNAME.ipynb


I am working on the file "ME249-Lecture-3.ipynb".
The first task is:

Solve the questions in green blocks. Save the file as ME249-Lecture-3-YOURNAME.ipynb and change YOURNAME in the bottom cell. Send me and the grader the html file not the ipynb file.

Discrete Fourier Series

Consider a function $f$ periodic over a domain $0\leq x\leq 2\pi$, discretized by $N_x$ points. The longest wavelength wave that can be contained in the domain is $L_x$. A phyiscal understanding of Fourier series is the representation of a system as the sum of many waves fo wavelengths smaller or equal to $L_x$. In a discrete sense, the series of wave used to decompose the system is defined as:
$$
a_n\exp\left(\hat{\jmath}\frac{2\pi n}{Lx}\right)
$$
such that

$$
f(x) = \sum_{n=-\infty}^{\infty}a_n\exp\left(\hat{\jmath}\frac{2\pi nx}{Lx}\right)
$$

and

$$
a_n = \frac{1}{L_x}\int_Lf(x)\exp\left(-\hat{\jmath}\frac{2\pi nx}{Lx}\right)dx
$$

Here $\hat{\jmath}^2=-1$.Often the reduction to wavenumber is used, where

$$
k_n = \frac{2\pi n}{L_x}
$$

Note that if $x$ is time instead of distance, $L_x$ is a time $T$ and the smallest frequency contained in the domain is $f_0=1/T_0$ and the wavenumber $n$ is $k_n=2\pi f_0n=2\pi f_n$ with $f_n$ for $\vert n\vert >1$ are the higher frequencies.

Discrete Fourier Transform (DFT)

In scientific computing we are interested in applying Fourier series on vectors or matrices, containing a integer number of samples. The DFT is the fourier series for the number of samples. DFT functions available in python or any other language only care about the number of samples, therefore the wavenumber is

$$
k_n=\frac{2\pi n}{N_x}
$$

Consider a function $f$ periodic over a domain $0\leq x\leq 2\pi$, discretized by $N_x$ points. The nodal value is $f_i$ located at $x_i=(i+1)\Delta x$ with $\Delta x=L_x/Nx$. The DFT is defined as
<p class='alert...
Can you write Python code for it?

import matplotlib.pyplot as plt
import numpy as np

Lx = 2.*np.pi
Nx = 64
u = np.zeros(Nx,dtype='float64')
du = np.zeros(Nx,dtype='float64')
ddu = np.zeros(Nx,dtype='float64')
k_0 = 2.*np.pi/Lx
x = np.linspace(Lx/Nx,Lx,Nx)
Nwave = 24
uwave = np.zeros((Nx,Nwave),dtype='float64')
duwave = np.zeros((Nx,Nwave),dtype='float64')
dduwave = np.zeros((Nx,Nwave),dtype='float64')
ampwave = np.random.random(Nwave)
phasewave = np.random.random(Nwave)*2*np.pi
for iwave in range(Nwave):
    uwave[:,iwave] = ampwave[iwave]*np.cos(k_0*iwave*x+phasewave[iwave])
    duwave[:,iwave] = -k_0*iwave*ampwave[iwave]*np.sin(k_0*iwave*x+phasewave[iwave])
    dduwave[:,iwave] = -(k_0*iwave)**2*ampwave[iwave]*np.cos(k_0*iwave*x+phasewave[iwave])
u = np.sum(uwave,axis=1)
du = np.sum(duwave,axis=1)
ddu = np.sum(dduwave,axis=1)
#print(u)
plt.plot(x,u,lw=2)
plt.xlim(0,Lx)
#plt.legend(loc=3, bbox_to_anchor=[0, 1],
#           ncol=3, shadow=True, fancybox=True)
plt.xlabel('$x$', fontdict = font)
plt.ylabel('$u$', fontdict = font)
plt.xticks(fontsize = 16)
plt.yticks(fontsize = 16)
plt.show()
plt.plot(x,du,lw=2)
plt.xlim(0,Lx)
#plt.legend(loc=3, bbox_to_anchor=[0, 1],
#           ncol=3, shadow=True, fancybox=True)
plt.xlabel('$x$', fontdict = font)
plt.ylabel('$du/dx$', fontdict = font)
plt.xticks(fontsize = 16)
plt.yticks(fontsize = 16)
plt.show()
plt.plot(x,ddu,lw=2)
plt.xlim(0,Lx)
#plt.legend(loc=3, bbox_to_anchor=[0, 1],
#           ncol=3, shadow=True, fancybox=True)
plt.xlabel('$x$', fontdict = font)
plt.ylabel('$d^2u/dx^2$', fontdict = font)
plt.xticks(fontsize = 16)
plt.yticks(fontsize = 16)
plt.show()


I am working on the file "ME249-Lecture-3.ipynb".
The first task is:
First, let's perform a sanity check, i.e.
$$
u=FT^{-1}\left[FT\left[u\right]\right]
$$
where $FT$ designates the Fourier transform and $FT^{-1}$ its inverse.
Can you write Python code for it?

#check FT^-1(FT(u)) - Sanity check
u_hat = np.fft.fft(u)
v = np.real(np.fft.ifft(u_hat))

plt.plot(x,u,'r-',lw=2,label='$u$')
plt.plot(x,v,'b--',lw=2,label='$FT^{-1}[FT[u]]$')
plt.xlim(0,Lx)
plt.legend(loc=3, bbox_to_anchor=[0, 1],
           ncol=3, shadow=True, fancybox=True)
plt.xlabel('$x$', fontdict = font)
plt.ylabel('$u$', fontdict = font)
plt.xticks(fontsize = 16)
plt.yticks(fontsize = 16)
plt.show()
print('error',np.linalg.norm(u-v,np.inf))


I am working on the file "ME249-Lecture-3.ipynb".
The first task is:
First, let's verify that the derivation of the modified wavenumber is correct.
Can you write Python code for it?

u_hat = np.fft.fft(u)
k = np.hstack((np.arange(0,Nx/2+1),np.arange(-Nx/2+1,0)))
dx = Lx/Nx
ikm = 1j*np.sin(2.*np.pi/Nx*k)/dx+(1-np.cos(2.*np.pi/Nx*k))/dx
v_hat = ikm*u_hat
dum = np.real(np.fft.ifft(v_hat))
plt.plot(x,dum,'r-',lw=2,label='$FT^{-1}[\hat{\jmath}\omega_{mod}FT[u]]$')
plt.plot(x,du_fd,'b--',lw=2,label='$\delta u/\delta x$')
plt.legend(loc=3, bbox_to_anchor=[0, 1],
           ncol=3, shadow=True, fancybox=True)
plt.ylabel('$du/dx$',fontsize = 18)
plt.xlabel('$x$',fontsize = 18)
plt.xticks(fontsize = 16)
plt.yticks(fontsize = 16)
plt.xlim(0,Lx)
plt.show()
print('error:',np.linalg.norm(du_fd-dum,np.inf))


I am working on the file "ME249-Lecture-4.ipynb".
The first task is:

Compact Finite Difference Schemes

The central compact finite difference scheme is defined as (in its general form):

$$
\beta f'_{i-2}+\alpha f'_{i-1}+f'_i+\alpha f'_{i+1}+\beta f'_{i+2}=
c\frac{f_{i+3}-f_{i-3}}{6\Delta}+b\frac{f_{i+2}-f_{i-2}}{4\Delta}+a\frac{f_{i+1}-f_{i-1}}{2\Delta}
$$

Note that if $\beta=\alpha=b=c=0$, the second order explicit central scheme is retrieved. Here we will limit our study to a tridagonal system:
$$
\alpha f'{i-1}+f'_i+\alpha f'{i+1}=a\frac{f_{i+1}-f_{i-1}}{2\Delta}
$$
and we wish to derive a fourth-order compact scheme.
The Taylor series expansion needed here are:
$$
f_{i\pm1}=f_i\pm\Delta f'i+\frac{\Delta^2}{2!}f''_i\pm\frac{\Delta^3}{3!}f'''_i+\frac{\Delta^4}{4!}f^{(4)}_i+{\cal O}(\Delta^5)
$$
$$
f'{i\pm1}=f'_i\pm\Delta f''_i+\frac{\Delta^2}{2!}f'''_i\pm\frac{\Delta^3}{3!}f^{(3)}_i+{\cal O}(\Delta^4)
$$
The goal is to find $\alpha$ and $a$ such that, the substitution of the Taylor series expansions above in the numerical scheme yields
$$
f'_i=f'_i+{\cal O}(\Delta^4)
$$
The left hand side develops into:
$$
\begin{split}
\alpha f'{i-1}+f'_i+\alpha f'{i+1}&=(1+2\alpha)f'i\
&+(-\alpha+\alpha)\Delta f''_i\
&+\alpha\Delta^2f'''_i\
&+(-\alpha+\alpha)\frac{\Delta^3}{3!}f^{(3)}_i+{\cal O}(\Delta^4)
\end{split}
$$
while the right hand side is:
$$
\begin{split}
a\frac{f{i+1}-f_{i-1}}{2\Delta}&=(-a+a)\frac{1}{2\Delta}f_i\
&+af'_i\
&+(-a+a)\frac{\Delta}{2}f''_i\
&+a\frac{\Delta^2}{3!}f'''_i\
&+(-a+a)\frac{\Delta^3}{4!}f^{(4)}_i+{\cal O}(\Delta^4)
\end{split}
$$
Fourth order is achieved if:
$$
1+2\alpha=a
$$
and
$$
\alpha = \frac{a}{3!}
$$

The fourth order compact scheme is
$$
\frac{1}{4} f'_{i-1}+f'_i+\frac{1}{4} f'_{i+1}=\frac{3}{2}\frac{f_{i+1}-f_{i-1}}{2\Delta}
$$
or
$$
 f'_{i-1}+4f'_i+ f'_{i+1}=3\frac{f_{i+1}-f_{i-1}}{\Delta}
$$

Consider a periodic function $f$ defined on $x\in[0,2\pi]$, discretized on a uniform mesh $x_i=i\Delta$ with $i=0,...,N-1$ and $\Delta=2\pi/(N)$. The system can be written in...
Can you write Python code for it?

Nitermax = 1000
it = 0
error_threshold = 1e-6
error = np.inf
phi = np.zeros(Nx)
phi_old = np.zeros(Nx)
error_jacobi = np.zeros(Nitermax)
b = np.zeros(Nx)
#generate rhs
b[1:Nx-1] = 0.75*(u[2:Nx]-u[0:Nx-2])/dx
b[0] = 0.75*(u[1]-u[Nx-1])/dx
b[Nx-1] = 0.75*(u[0]-u[Nx-2])/dx
for it in range(Nitermax):
    phi_old = np.copy(phi)
    phi[1:Nx-1] = -0.25*(phi_old[0:Nx-2] + phi_old[2:Nx]) \
                    +b[1:Nx-1]
    phi[0] = -0.25*(phi_old[1] + phi_old[Nx-1]) \
                    +b[0]
    phi[Nx-1] = -0.25*(phi_old[Nx-2] + phi_old[0]) \
                    +b[Nx-1]
    error_jacobi[it] = np.max(np.abs(phi-phi_old))
    if (error_jacobi[it] < error_threshold): break

#print(error)
it_jacobi = it
plt.semilogy(error_jacobi[0:it_jacobi+1],lw=2,label='Jacobi')
plt.xlabel('Iterations', fontdict = font)
plt.ylabel('error', fontdict = font)
plt.xticks(fontsize = 16)
plt.yticks(fontsize = 16)
plt.legend(loc=3, bbox_to_anchor=[0, 1],
           ncol=3, shadow=True, fancybox=True)
plt.show()
plt.plot(x,phi-du)
plt.show()
print(np.max(np.abs(phi-du)))


I am working on the file "ME249-Pb1-steady-laminar-channel-flow.ipynb".
The first task is:

 Steady State Laminar Channel with variable density

Variable density can occur through thermodynamics (e.g. geophysical flows with temperature/density gradients), non-Newtonian flow dynamics (e.q. shear dependent viscosity) and turbulent flows. In the latter, the viscosity is split into the viscosity of the fluid and the dissipative contribution of turbulence:
$$
\mu_{total}= \mu +\mu_t
$$
where $\mu_t$ is the turbulent viscosity calculated from a turbulence model (to be defined later).

Equation

Assuming a spatially variable viscosity $\mu$, the steady state, fully developed channel flow governing equation now writes:
$$
0=G+\frac{d}{dy}\mu\frac{dU}{dy}
$$
with walls located at $y=\pm h$.

Discrete Equation

Here we assume that velocity and viscosity are collocated. As before, the second derivative is obtained by computing the shear stress $\mu(du/dy)$ at nodes $j\pm1/2$ and then by taking the derivative of the shear stress at node $j$. Note that the viscosity needs to be interpolated at nodes $j\pm1/2$:
$$
\frac{1}{\Delta y}\left(\frac{\mu_{j+1}+\mu_{j}}{2}\frac{U_{j+1}-U_{j}}{\Delta y}-\frac{\mu_{j}+\mu_{j-1}}{2}\frac{U_{j}-U_{j-1}}{\Delta y}\right)=-G\;,
$$
which is rearranged as
$$
\frac{\mu_{j}+\mu_{j-1}}{2\Delta y^2}U_{j-1}-\frac{\mu_{j-1}+2\mu_{j}+\mu_{j-1}}{2\Delta y^2}U_j+\frac{\mu_{j}+\mu_{j-1}}{2\Delta y^2}U_{j+1} = -G\,.
$$

Write a code for a turbulent flow using the following model:
$$
0=G+\frac{d}{dy}(\mu+\mu_t)\frac{dU}{dy}
$$
with
$$
\mu_t = \rho L_m\left\vert\frac{dU}{dy}\right\vert
$$
and
$$
L_m=\kappa y\left(1-e^{-y^+/A^+}\right)
$$

with $\kappa=0.41$, $y$ is the distance from the wall, $A^+=25$, $y^+=yu_\tau/\nu$, $\mu=1/300$ and $G=1$. The skin-friction velocity is defined as:

$$
\rho u_\tau^2=\mu\left.\frac{dU}{dy}\right\vert_{wall}
$$

Use nufd to calculate the gradient of velocity. The function takes the locations of computational nodes as an argument and return a matrix, e.g....
Can you write Python code for it?

from scipy.sparse import csr_matrix
def nufd(x):
    n = len(x)
    h = x[1:]-x[:n-1]
    a0 = -(2*h[0]+h[1])/(h[0]*(h[0]+h[1]))
    ak = -h[1:]/(h[:n-2]*(h[:n-2]+h[1:]))
    an = h[-1]/(h[-2]*(h[-1]+h[-2]))
    b0 = (h[0]+h[1])/(h[0]*h[1])
    bk = (h[1:] - h[:n-2])/(h[:n-2]*h[1:])
    bn = -(h[-1]+h[-2])/(h[-1]*h[-2])
    c0 = -h[0]/(h[1]*(h[0]+h[1]))
    ck = h[:n-2]/(h[1:]*(h[:n-2]+h[1:]))
    cn = (2*h[-1]+h[-2])/(h[-1]*(h[-2]+h[-1]))
    val  = np.hstack((a0,ak,an,b0,bk,bn,c0,ck,cn))
    row = np.tile(np.arange(n),3)
    dex = np.hstack((0,np.arange(n-2),n-3))
    col = np.hstack((dex,dex+1,dex+2))
    D = csr_matrix((val,(row,col)),shape=(n,n))
    return D

def matrix_coeff(mu):
    #mu needs to be N+2
    global dy #tells python the dy defined in the main code
    n = len(mu)
    a = np.zeros(n-2)
    b = np.zeros(n-2)
    c = np.zeros(n-2)
    a[:] = (mu[0:-2]+mu[1:-1])/(2.*dy**2)
    b[:] = -(mu[0:-2]+2.*mu[1:-1]+mu[2:])/(2.*dy**2)
    c[:] = (mu[1:-1]+mu[2:])/(2.*dy**2)
    return a,b,c
def rhs(n,a,c):
    global G_0,U_lower_wall,U_upper_wall
    r = -G_0*np.ones(n)
    # boundary conditions
    r[0] -= a[0]*U_lower_wall
    r[n-1] -= c[n-1]*U_upper_wall
    return r

N = 512

mu = 1/300.
rho = 1.
nu = mu/rho
Aplus = 25.
h = 1. # half height of the channel
G_0 = 1. #=-dP/dx pressure gradient
U_lower_wall = 0.
U_upper_wall = 0.
kappa = 0.41

# allocate memory for variables
U = np.zeros(N)
U_all = np.zeros(N+2)

# define location of computational nodes
y_all = np.linspace(-h, +h, N+2)
dy = y_all[1] - y_all[0]
y = np.linspace(-h+dy, +h-dy, N)

#Compute the derivation matrix to obtain first derivative in y
Dy_all = nufd(y_all)

# first guess for a,b,c: mu_t=0
mu_total = mu*np.ones(N+2)
#Matrix coefficients
a = np.zeros(N)
b = np.zeros(N)
c = np.zeros(N)
a,b,c = matrix_coeff(mu_total)
#Right hand side vector
r = -G_0*np.ones(N)
r -= G

#Boundary conditions
r[0] -= a[0]*U_lower_wall
r[N-1] -= c[N-1]*U_upper_wall

#Laminar solution (first guess)
U = TDMAsolver(a, b, c, r)
U_all[1:N+1] = U
U_all[0] =...


I am working on the file "ME249-Pb1-steady-laminar-channel-flow.ipynb".
The first task is:

Error Quantification

Visual verification is subjective. You should always rely on a statistical approach, such the $L^2$ or $L^\infty$.

$$
L^2 = \left[\sum_{i=0}^{N-1}(U^{numerical}_j-U_j^{exact})^2\right]^{1/2}
$$
$$
L^\infty = \max_{i=0,N-1}\left\vert U^{numerical}_j-U_j^{exact}\right\vert
$$

Can you write Python code for it?

L2 = np.linalg.norm(U_all-U_exact,2)
print("L2 error= %1.4e" %L2)
Linf = np.linalg.norm(U_all-U_exact,np.inf)
print("Linfinity error= %1.4e" %Linf)


I am working on the file "ME249-Pb1-steady-laminar-channel-flow.ipynb".
The first task is:

Numerical solution

Here we will solve the flow inside a channel of half-height $h=1$, a pressure gradient $G=1$ and a viscosity $\mu=1/10$. Both walls are fixed.

Write the code to solve our ODE.

Solution

Can you write Python code for it?

#number of computational nodes between walls
N = 128

#Flow and geometry parameters
mu = 1/10. #dynamic viscosity
h = 1. #half height of the channel
G = 1. #=-dP/dx pressure gradient
U_lower_wall = 0. #Velocity of the lower wall
U_upper_wall = 0. #Velocity of the upper wall

#Allocation of memory
U = np.zeros(N)                  #Velocity. On the sketch: from node j=0 to N-1
U_all = np.zeros(N+2)            #Velocity. On the sketch: from node j=-1 to N
y_all = np.linspace(-h, +h, N+2) #location of computational nodes from the lower to the upper wall
dy = y_all[1] - y_all[0]         #space between computational nodes
y = np.linspace(-h+dy, +h-dy, N) #location of computational nodes for vector U

#Matrix coefficients
a = mu/dy**2*np.ones(N)
b = -2.*mu/dy**2*np.ones(N)
c = mu/dy**2*np.ones(N)
#Right hand side vector
r = -G*np.ones(N)

#Boundary conditions
r[0] -= a[0]*U_lower_wall
r[N-1] -= c[N-1]*U_upper_wall

#solution using Thomas Algorithm
U = TDMAsolver(a, b, c, r)

#Copy array U onto U_all for plotting
U_all[1:N+1] = U
U_all[0] = U_lower_wall
U_all[N+1] = U_upper_wall

#Plot
plt.plot(y_all,U_all,lw=2,label = r"numerical solution")
U_exact = G/(2*mu)*(h**2-np.power(y_all,2))
plt.plot(y_all,U_exact,'r--',lw=2,label = r"exact solution")
plt.xlabel(r"$y/h$", fontdict = fontlabel)
plt.ylabel(r"$U(y)$", fontdict = fontlabel)
plt.legend(loc=3, bbox_to_anchor=[0, 1],
           ncol=2, shadow=True, fancybox=True)
plt.show()


I am working on the file "ME249-Pb1-steady-laminar-channel-flow.ipynb".
The first task is:

Demonstrate that you can estimate the derivative of $U$ at node $j+1/2$, using $U_j$ and $U_{j+1}$

To answer this question, you will use the following Taylor series expansion:
$$
U(y\pm\Delta y/2)=U(y)+\sum_{n=1}^\infty\frac{(\pm\Delta y/2)^n}{n!}\frac{d^nU}{dy^n}
$$

 Solution

\begin{eqnarray}
U_{j+1} &=& U_{j+1/2}+\frac{\Delta y}{2}\left.\frac{dU}{dy}\right\vert_{j+1/2}+\frac{(\Delta y/2)^2}{2!}\left.\frac{d^2U}{dy^2}\right\vert_{j+1/2}+\frac{(\Delta y/2)^3}{3!}\left.\frac{d^3U}{dy^3}\right\vert_{j+1/2}+{\cal O}(\Delta y^4)\
-U(j) &=& U{j+1/2}+\frac{-\Delta y}{2}\left.\frac{dU}{dy}\right\vert_{j+1/2}+\frac{(-\Delta y/2)^2}{2!}\left.\frac{d^2U}{dy^2}\right\vert_{j+1/2}+\frac{(-\Delta y/2)^3}{3!}\left.\frac{d^3U}{dy^3}\right\vert_{j+1/2}+{\cal O}(\Delta y^4)\
=U_{j+1}-U_j&=&0+\Delta y\left.\frac{dU}{dy}\right\vert_{j+1/2}+\frac{\Delta y^3}{24}\left.\frac{d^3U}{dy^3}\right\vert_{j+1/2}+{\cal O}(\Delta y^4)
\end{eqnarray}
Therefore

$$
\left.\frac{dU}{dy}\right\vert_{j+1/2}=\frac{U_{j+1}-U_j}{\Delta y}+{\cal O}(\Delta y^2)
$$

Using the derivative above, show that a numerical scheme for the second derivative of $U$ is
$$
\frac{d^2U}{dy^2}= \frac{U_{j-1}-2U_j+U_{j+1}}{\Delta y^2}+{\cal O}(\Delta y^2)
$$

The term ${\cal O}(\Delta y^2)$ represents the truncation error, i.e. all the terms from the Taylor series expansion that are affected with $\Delta y^n$ for $n\geq 2$.

 Solution

Using Taylor expansion series:
\begin{eqnarray}
\frac{1}{\Delta y^2}U_{j+1} &=&\frac{1}{\Delta y^2}\left[U_j+\Delta y\left.\frac{dU}{dy}\right\vert_j+\frac{\Delta y^2}{2!}\left.\frac{d^2U}{dy^2}\right\vert_j+\frac{\Delta y^3}{3!}\left.\frac{d^3U}{dy^3}\right\vert_j+\frac{\Delta y^4}{4!}\left.\frac{d^4U}{dy^4}\right\vert_j+{\cal O}(\Delta y^5)\right]\
-\frac{2}{\Delta y^2}U_{j} &=&\frac{-2}{\Delta y^2}\left[U_j\right]\
+\frac{1}{\Delta y^2}U_{j-1}...
Can you write Python code for it?

import numpy as np
def TDMAsolver(a, b, c, d):
    '''
    TDMA solver, a b c d can be NumPy array type or Python list type.
    refer to http://en.wikipedia.org/wiki/Tridiagonal_matrix_algorithm
    '''
    nf = len(a)     # number of equations
    ac, bc, cc, dc = map(np.array, (a, b, c, d))     # copy the array
    for it in range(1, nf):
        mc = ac[it]/bc[it-1]
        bc[it] = bc[it] - mc*cc[it-1]
        dc[it] = dc[it] - mc*dc[it-1]

    xc = ac
    xc[-1] = dc[-1]/bc[-1]

    for il in range(nf-2, -1, -1):
        xc[il] = (dc[il]-cc[il]*xc[il+1])/bc[il]

    del bc, cc, dc  # delete variables from memory

    return xc


I am working on the file "MES/arakawaBracket/ArakawaOfDDXf2/calculations/exactSolutions.ipynb".
The first task is:
Define the function to take the derivative of
NOTE:

z must be periodic
The field $f(\rho, \theta)$ must be of class infinity in $z=0$ and $z=2\pi$
The field $f(\rho, \theta)$ must be single valued when $\rho\to0$
The field $f(\rho, \theta)$ must be continuous in the $\rho$ direction with $f(\rho, \theta + \pi)$
Eventual BC in $\rho$ must be satisfied
Can you write Python code for it?

# We need Lx
from boututils.options import BOUTOptions
myOpts = BOUTOptions(folder)
Lx = eval(myOpts.geom['Lx'])

# Two gaussians

# The skew sinus
# In cartesian coordinates we would like a sinus with with a wave-vector in the direction
# 45 degrees with respect to the first quadrant. This can be achieved with a wave vector
# k = [1/sqrt(2), 1/sqrt(2)]
# sin((1/sqrt(2))*(x + y))
# We would like 2 nodes, so we may write
# sin((1/sqrt(2))*(x + y)*(2*pi/(2*Lx)))
# Rewriting this to cylindrical coordinates, gives
# sin((1/sqrt(2))*(x*(cos(z)+sin(z)))*(2*pi/(2*Lx)))

# The gaussian
# In cartesian coordinates we would like
# f = exp(-(1/(2*w^2))*((x-x0)^2 + (y-y0)^2))
# In cylindrical coordinates, this translates to
# f = exp(-(1/(2*w^2))*(x^2 + y^2 + x0^2 + y0^2 - 2*(x*x0+y*y0) ))
#   = exp(-(1/(2*w^2))*(rho^2 + rho0^2 - 2*rho*rho0*(cos(theta)*cos(theta0)+sin(theta)*sin(theta0)) ))
#   = exp(-(1/(2*w^2))*(rho^2 + rho0^2 - 2*rho*rho0*(cos(theta - theta0)) ))

# A parabola
# In cartesian coordinates, we have
# ((x-x0)/Lx)^2
# Chosing this function to have a zero value at the edge yields in cylindrical coordinates
# ((x*cos(z)+Lx)/(2*Lx))^2

# Scaling with 40 to get S in order of unity

w = 0.8*Lx
rho0 = 0.3*Lx
theta0 = 5*pi/4
the_vars['n'] = 40*sin((1/sqrt(2))*(x*(cos(z)+sin(z)))*(2*pi/(2*Lx)))*\
                  exp(-(1/(2*w**2))*(x**2 + rho0**2 - 2*x*rho0*(cos(z - theta0)) ))*\
                  ((x*cos(z)+Lx)/(2*Lx))**2

# Mixmode

# Need the x^3 in order to let the second derivative of the field go towards one value when rho -> 0
#    (needed in Arakawa brackets)
# Mutliply with a mix of modes
# Multiply with a tanh in order to make the variation in x more homogeneous

# Scaling with 10 to make variations in phi comparable to those of n

the_vars['phi'] = 10*(6+((x/(Lx))**3)*\
                cos(2*z)*\
                (
                   cos(2*pi*(x/Lx)) + sin(2*pi*(x/Lx))
                 + cos(3*2*pi*(x/Lx)) + cos(2*2*pi*(x/Lx))
                )\
                *(1/2)*(1-tanh((1/8)*(x))))


I am working on the file "MES/arakawaBracket/ArakawaOfDDXf2/calculations/exactSolutions.ipynb".
The first task is:
Define the variables
Can you write Python code for it?


I am working on the file "MES/arakawaBracket/ArakawaOfDDXf2/calculations/exactSolutions.ipynb".
The first task is:
Initialize
Can you write Python code for it?

folder = '../mixModeAndGaussian/'
metric = get_metric()


I am working on the file "MES/arakawaBracket/ArakawaOfDDXf2/calculations/exactSolutions.ipynb".
The first task is:
Plot
Can you write Python code for it?

make_plot(folder=folder, the_vars=the_vars, plot2d=True, include_aux=False, save=False)


I am working on the file "MES/boundaries/2-uEParSheath/calculations/exactSolutions.ipynb".
The first task is:
Define the variables
Can you write Python code for it?

# Initialization
the_vars = {}

# We need Lx
from boututils.options import BOUTOptions
myOpts = BOUTOptions(folder)
Lx = eval(myOpts.geom['Lx'])
Ly = eval(myOpts.geom['Ly'])
mu = eval(myOpts.cst['mu']) # Needed for Lambda
Lambda = eval(myOpts.cst['Lambda'])
phiRef = eval(myOpts.cst['phiRef'])

# No y variation in the profile

# The potential

# The skew sinus
# In cartesian coordinates we would like a sinus with with a wave-vector in the direction
# 45 degrees with respect to the first quadrant. This can be achieved with a wave vector
# k = [1/sqrt(2), 1/sqrt(2)]
# sin((1/sqrt(2))*(x + y))
# We would like 2 nodes, so we may write
# sin((1/sqrt(2))*(x + y)*(2*pi/(2*Lx)))

the_vars['phi'] = sin((1/sqrt(2))*(x + y)*(2*pi/(2*Lx)))

# The profile
the_vars['profile'] = sin(2*pi*x/Lx)**2

# The parallel velocity, given by the sheath boundary condition
the_vars['uEPar'] = exp(Lambda-(phiRef+the_vars['phi']))*the_vars['profile']


I am working on the file "MES/boundaries/2-uEParSheath/calculations/exactSolutions.ipynb".
The first task is:
Print the variables in BOUT++ format
Can you write Python code for it?


I am working on the file "MES/boundaries/3-cauchyBC/calculations/exactSolutions.ipynb".
The first task is:
Define the variables
Can you write Python code for it?

# Initialization
the_vars = {}

# We need Lx
from boututils.options import BOUTOptions
myOpts = BOUTOptions(folder)
Lx = eval(myOpts.geom['Lx'])
Ly = eval(myOpts.geom['Ly'])

# Gaussian with sinus and parabola

# The skew sinus
# In cartesian coordinates we would like a sinus with with a wave-vector in the direction
# 45 degrees with respect to the first quadrant. This can be achieved with a wave vector
# k = [1/sqrt(2), 1/sqrt(2)]
# sin((1/sqrt(2))*(x + y))
# We would like 2 nodes, so we may write
# sin((1/sqrt(2))*(x + y)*(2*pi/(2*Lx)))

# The gaussian
# In cartesian coordinates we would like
# f = exp(-(1/(2*w^2))*((x-x0)^2 + (y-y0)^2))

# A parabola
# In cartesian coordinates, we have
# ((x-x0)/Lx)^2

wx = 0.5*Lx
wy = 0.5*Ly
x0 = 0.3*Lx
y0 = 0.5*Ly
the_vars['f'] = sin((1/sqrt(2))*(x + y)*(2*pi/(2*Lx)))*\
                exp(-(((x-x0)**2/(2*wx**2)) + ((y-y0)**2/(2*wy**2))))*\
                ((x-x0)/Lx)**2

the_vars['a'] = the_vars['f'].subs(y, 0)
the_vars['b'] = the_vars['f'].diff(y).subs(y, 0)


I am working on the file "MES/boundaries/3-cauchyBC/calculations/exactSolutions.ipynb".
The first task is:
Plot
Can you write Python code for it?

make_plot(folder=folder, the_vars=the_vars, plot2d=True, include_aux=False, direction='y')


I am working on the file "MES/boundaries/4-jParSheath/calculations/exactSolutions.ipynb".
The first task is:
Initialize
Can you write Python code for it?


I am working on the file "MES/boundaries/4-jParSheath/calculations/exactSolutions.ipynb".
The first task is:
Plot
Can you write Python code for it?

#make_plot(folder=folder, the_vars=the_vars, plot2d=True, include_aux=False, direction='y')


I am working on the file "MES/divOfScalarTimesVector/1a-divPerp/calculations/exactSolutions.ipynb".
The first task is:
Calculate the solution
Can you write Python code for it?

the_vars['S'] =   (1/metric.J)*DDX(metric.J*the_vars['f^x'], metric=metric)\
                + 0\
                + (1/metric.J)*DDZ(metric.J*the_vars['f^z'], metric=metric)


I am working on the file "MES/divOfScalarTimesVector/1a-divPerp/calculations/exactSolutions.ipynb".
The first task is:
Define manifactured solutions
Due to orthogonality we have that
$$
S = \nabla \cdot \mathbf{f}\perp
  = \nabla\perp \cdot \mathbf{f}_\perp
  = \frac{1}{J} \partial_i \left(Jf^i\right)
  = \frac{1}{J} \partial_x \left(Jf^x\right) + \frac{1}{J} \partial_z \left(Jf^z\right)
$$
In cylindrical coordinates $J=\rho$, so this gives
$$
f = \frac{1}{\rho} \partial_\rho \left(\rho f^\rho\right) + \frac{1}{\rho} \partial_\theta \left(\rho f^\theta\right)
  = \partial_\rho  f^\rho + \frac{f^\rho}{\rho} + \partial_\theta f^\theta
$$
NOTE:

z must be periodic
The field $f(\rho, \theta)$ must be of class infinity in $z=0$ and $z=2\pi$
The field $f(\rho, \theta)$ must be single valued when $\rho\to0$
The field $f(\rho, \theta)$ must be continuous in the $\rho$ direction with $f(\rho, \theta + \pi)$
Eventual BC in $\rho$ must be satisfied
Can you write Python code for it?

# We need Lx
from boututils.options import BOUTOptions
myOpts = BOUTOptions(folder)
Lx = eval(myOpts.geom['Lx'])

# Two gaussians
# NOTE: S actually looks good

# The skew sinus
# In cartesian coordinates we would like a sinus with with a wave-vector in the direction
# 45 degrees with respect to the first quadrant. This can be achieved with a wave vector
# k = [1/sqrt(2), 1/sqrt(2)]
# sin((1/sqrt(2))*(x + y))
# We would like 2 nodes, so we may write
# sin((1/sqrt(2))*(x + y)*(2*pi/(2*Lx)))
# Rewriting this to cylindrical coordinates, gives
# sin((1/sqrt(2))*(x*(cos(z)+sin(z)))*(2*pi/(2*Lx)))

# The gaussian
# In cartesian coordinates we would like
# f = exp(-(1/(2*w^2))*((x-x0)^2 + (y-y0)^2))
# In cylindrical coordinates, this translates to
# f = exp(-(1/(2*w^2))*(x^2 + y^2 + x0^2 + y0^2 - 2*(x*x0+y*y0) ))
#   = exp(-(1/(2*w^2))*(rho^2 + rho0^2 - 2*rho*rho0*(cos(theta)*cos(theta0)+sin(theta)*sin(theta0)) ))
#   = exp(-(1/(2*w^2))*(rho^2 + rho0^2 - 2*rho*rho0*(cos(theta - theta0)) ))

# A parabola
# In cartesian coordinates, we have
# ((x-x0)/Lx)^2
# Chosing this function to have a zero value at the edge yields in cylindrical coordinates
# ((x*cos(z)+Lx)/(2*Lx))^2

w = 0.8*Lx
rho0 = 0.3*Lx
theta0 = 5*pi/4
the_vars['f^x'] = sin((1/sqrt(2))*(x*(cos(z)+sin(z)))*(2*pi/(2*Lx)))*\
                exp(-(1/(2*w**2))*(x**2 + rho0**2 - 2*x*rho0*(cos(z - theta0)) ))*\
                ((x*cos(z)+Lx)/(2*Lx))**2

# The gaussian

# In cartesian coordinates we would like
# f = exp(-(1/(2*w^2))*((x-x0)^2 + (y-y0)^2))
# In cylindrical coordinates, this translates to
# f = exp(-(1/(2*w^2))*(x^2 + y^2 + x0^2 + y0^2 - 2*(x*x0+y*y0) ))
#   = exp(-(1/(2*w^2))*(rho^2 + rho0^2 - 2*rho*rho0*(cos(theta)*cos(theta0)+sin(theta)*sin(theta0)) ))
#   = exp(-(1/(2*w^2))*(rho^2 + rho0^2 - 2*rho*rho0*(cos(theta - theta0)) ))

w = 0.5*Lx
rho0 = 0.2*Lx
theta0 = pi
the_vars['f^z'] = exp(-(1/(2*w**2))*(x**2 + rho0**2 - 2*x*rho0*(cos(z - theta0)) ))


I am working on the file "MES/divOfScalarTimesVector/1a-divPerp/calculations/exactSolutions.ipynb".
The first task is:
Define the variables
Can you write Python code for it?


I am working on the file "MES/divOfScalarTimesVector/1a-divPerp/calculations/exactSolutions.ipynb".
The first task is:
Initialize
Can you write Python code for it?

folder = '../twoGaussians/'
metric = get_metric()


I am working on the file "MES/divOfScalarTimesVector/1a-divPerp/calculations/exactSolutions.ipynb".
The first task is:
Plot
Can you write Python code for it?

make_plot(folder=folder, the_vars=the_vars, plot2d=True, include_aux=False)


I am working on the file "MES/divOfScalarTimesVector/1b-JTimesDivPerp/calculations/exactSolutions.ipynb".
The first task is:
Calculate the solution
Can you write Python code for it?

the_vars['S'] =   DDX(metric.J*the_vars['f^x'], metric=metric)\
                + 0\
                + DDZ(metric.J*the_vars['f^z'], metric=metric)


I am working on the file "MES/divOfScalarTimesVector/1b-JTimesDivPerp/calculations/exactSolutions.ipynb".
The first task is:
Define manifactured solutions
Due to orthogonality we have that
$$
S = J\nabla \cdot \mathbf{f}\perp
  = J\nabla\perp \cdot \mathbf{f}_\perp
  = \partial_i \left(Jf^i\right)
  = \partial_x \left(Jf^x\right) + \partial_z \left(Jf^z\right)
$$
In cylindrical coordinates $J=\rho$, so this gives
$$
f = \partial_\rho \left(\rho f^\rho\right) + \partial_\theta \left(\rho f^\theta\right)
  = \rho\partial_\rho  f^\rho + f^\rho + \rho\partial_\theta f^\theta
$$
NOTE:

z must be periodic
The field $f(\rho, \theta)$ must be of class infinity in $z=0$ and $z=2\pi$
The field $f(\rho, \theta)$ must be single valued when $\rho\to0$
The field $f(\rho, \theta)$ must be continuous in the $\rho$ direction with $f(\rho, \theta + \pi)$
Eventual BC in $\rho$ must be satisfied
Can you write Python code for it?


I am working on the file "MES/divOfScalarTimesVector/1b-JTimesDivPerp/calculations/exactSolutions.ipynb".
The first task is:
Define the variables
Can you write Python code for it?


I am working on the file "MES/divOfScalarTimesVector/1b-JTimesDivPerp/calculations/exactSolutions.ipynb".
The first task is:
Initialize
Can you write Python code for it?


I am working on the file "MES/divOfScalarTimesVector/1b-JTimesDivPerp/calculations/exactSolutions.ipynb".
The first task is:
Plot
Can you write Python code for it?


I am working on the file "MES/divOfScalarTimesVector/1b-JTimesDivPerp/calculations/exactSolutions.ipynb".
The first task is:
Print the variables in BOUT++ format
Can you write Python code for it?


I am working on the file "MES/divOfScalarTimesVector/2a-divSource/calculations/exactSolutions.ipynb".
The first task is:
Define manifactured solutions
We have that
$$S = \nabla\cdot(S_n\nabla_\perp\phi) = S_n\nabla_\perp^2\phi + \nabla S_n\cdot \nabla_\perp \phi = S_n\nabla_\perp^2\phi + \nabla_\perp S_n\cdot \nabla_\perp \phi$$
We will use the Delp2 operator for the perpendicular Laplace operator (as the y-derivatives vanishes in cylinder geometry). We have
Delp2$(f)=g^{xx}\partial_x^2 f + g^{zz}\partial_z^2 f + 2g^{xz}\partial_x\partial_z f + G^1\partial_x f + G^3\partial_z f$
Using the cylinder geometry, we get that
Delp2$(f)=\partial_x^2 f + \frac{1}{x^2}\partial_z^2 f + \frac{1}{x}\partial_x f$
Further on, due to orthogonality we have that
$$\nabla_\perp S_n\cdot \nabla_\perp \phi = \mathbf{e}^i\cdot \mathbf{e}^i(\partial_i S_n)(\partial_i \phi)
 = g^{xx}(\partial_x S_n)(\partial_x \phi) + g^{zz}(\partial_z S_n)(\partial_z \phi) = (\partial_x S_n)(\partial_x \phi) + \frac{1}{x^2}(\partial_z S_n)(\partial_z \phi)$$
This gives
$$S = \nabla\cdot(S_n\nabla_\perp\phi) = S_n\partial_x^2 \phi + S_n\frac{1}{x^2}\partial_z^2 \phi + S_n\frac{1}{x}\partial_x \phi + (\partial_x S_n)(\partial_x \phi) + \frac{1}{x^2}(\partial_z S_n)(\partial_z \phi)$$
We will use this to calculate the analytical solution.
NOTE:

z must be periodic
The field $f(\rho, \theta)$ must be of class infinity in $z=0$ and $z=2\pi$
The field $f(\rho, \theta)$ must be single valued when $\rho\to0$
The field $f(\rho, \theta)$ must be continuous in the $\rho$ direction with $f(\rho, \theta + \pi)$
Eventual BC in $\rho$ must be satisfied
Can you write Python code for it?

# We need Lx
from boututils.options import BOUTOptions
myOpts = BOUTOptions(folder)
Lx = eval(myOpts.geom['Lx'])

# Two normal gaussians

# The gaussian
# In cartesian coordinates we would like
# f = exp(-(1/(2*w^2))*((x-x0)^2 + (y-y0)^2))
# In cylindrical coordinates, this translates to
# f = exp(-(1/(2*w^2))*(x^2 + y^2 + x0^2 + y0^2 - 2*(x*x0+y*y0) ))
#   = exp(-(1/(2*w^2))*(rho^2 + rho0^2 - 2*rho*rho0*(cos(theta)*cos(theta0)+sin(theta)*sin(theta0)) ))
#   = exp(-(1/(2*w^2))*(rho^2 + rho0^2 - 2*rho*rho0*(cos(theta - theta0)) ))

w = 0.8*Lx
rho0 = 0.3*Lx
theta0 = 5*pi/4
the_vars['phi'] = exp(-(1/(2*w**2))*(x**2 + rho0**2 - 2*x*rho0*(cos(z - theta0)) ))

w = 0.5*Lx
rho0 = 0.2*Lx
theta0 = 0
the_vars['S_n'] = exp(-(1/(2*w**2))*(x**2 + rho0**2 - 2*x*rho0*(cos(z - theta0)) ))


I am working on the file "MES/divOfScalarTimesVector/2a-divSource/calculations/exactSolutions.ipynb".
The first task is:
Initialize
Can you write Python code for it?


I am working on the file "MES/divOfScalarTimesVector/2a-divSource/calculations/exactSolutions.ipynb".
The first task is:
Plot
Can you write Python code for it?


I am working on the file "MES/divOfScalarTimesVector/2b-JTimesDivSource/calculations/exactSolutions.ipynb".
The first task is:
Plot
Can you write Python code for it?


I am working on the file "MES/integrals/volumeIntegral/calculations/exactSolutions.ipynb".
The first task is:
Calculating the solution
Can you write Python code for it?

rhoInt        =  integrate(the_vars['f']*x, (x, 0 ,Lx))
rhoYInt       =  integrate(rhoInt         , (y, 0 ,Ly))
the_vars['S'] = (integrate(rhoYInt        , (z, 0, 2*np.pi))).evalf()


I am working on the file "MES/integrals/volumeIntegral/calculations/exactSolutions.ipynb".
The first task is:
Define the variables
Can you write Python code for it?


I am working on the file "MES/integrals/volumeIntegral/calculations/exactSolutions.ipynb".
The first task is:
Print the variables in BOUT++ format
Can you write Python code for it?


I am working on the file "MES/laplaceInversion/Naulinsolver0Bndry/calculations/exactSolutions.ipynb".
The first task is:
Define the variables
Can you write Python code for it?


I am working on the file "MES/laplaceInversion/Naulinsolver0Bndry/calculations/exactSolutions.ipynb".
The first task is:
Initialize
Can you write Python code for it?


I am working on the file "MES/laplaceInversion/Naulinsolver0Bndry/calculations/exactSolutions.ipynb".
The first task is:
Plot
Can you write Python code for it?

make_plot(folder=folder, the_vars=the_vars, plot2d=True, include_aux=False, save = True)


I am working on the file "MES/laplaceInversion/Naulinsolver0Bndry/calculations/exactSolutions.ipynb".
The first task is:
Print the variables in BOUT++ format
Can you write Python code for it?


I am working on the file "MES/laplaceInversion/Naulinsolver0Bndry/calculations/exactSolutions.ipynb".
The first task is:
We have that
$$\Omega^D = \nabla\cdot(n\nabla_\perp(\phi))$$
Can you write Python code for it?

# Calculate the solution
the_vars['vortD'] =   the_vars['n']*Delp2(the_vars[r'$\phi$'], metric=metric)\
                    + metric.g11*DDX(the_vars['n'], metric=metric)*DDX(the_vars[r'$\phi$'], metric=metric)\
                    + metric.g33*DDZ(the_vars['n'], metric=metric)*DDZ(the_vars[r'$\phi$'], metric=metric)


I am working on the file "MES/laplaceInversion/Naulinsolver0Bndry/calculations/exactSolutions.ipynb".
The first task is:
We have that
$$\Omega^D = \nabla\cdot(n\nabla_\perp\phi) = n\nabla_\perp^2\phi + \nabla n\cdot \nabla_\perp \phi = n\nabla_\perp^2\phi + \nabla_\perp n\cdot \nabla_\perp \phi$$
Due to orthogonality we have that
$$\nabla_\perp n\cdot \nabla_\perp \phi = \mathbf{e}^i\cdot \mathbf{e}^i(\partial_i n)(\partial_i \phi)
 = g^{xx}(\partial_x n)(\partial_x \phi) + g^{zz}(\partial_z n)(\partial_z \phi) = (\partial_x n)(\partial_x \phi) + \frac{1}{x^2}(\partial_z n)(\partial_z \phi)$$
We will use the Delp2 operator for the perpendicular Laplace operator (as the y-derivatives vanishes). We have
Delp2$(f)=g^{xx}\partial_x^2 f + g^{zz}\partial_z^2 f + 2g^{xz}\partial_x\partial_z f + G^1\partial_x f + G^3\partial_z f$
Using the cylinder geometry, we get that
Delp2$(f)=\partial_x^2 f + \frac{1}{x^2}\partial_z^2 f + \frac{1}{x}\partial_x f$
This gives
$$\Omega^D = \nabla\cdot(n\nabla_\perp\phi) = n\partial_x^2 \phi + n\frac{1}{x^2}\partial_z^2 \phi + n\frac{1}{x}\partial_x \phi + (\partial_x n)(\partial_x \phi) + \frac{1}{x^2}(\partial_z n)(\partial_z \phi)$$
NOTE:

z must be periodic
The field $f(\rho, \theta)$ must be of class infinity in $z=0$ and $z=2\pi$
The field $f(\rho, \theta)$ must be single valued when $\rho\to0$
The field $f(\rho, \theta)$ must be continuous in the $\rho$ direction with $f(\rho, \theta + \pi)$
Eventual BC in $\rho$ must be satisfied
Can you write Python code for it?

# We need Lx
from boututils.options import BOUTOptions
myOpts = BOUTOptions(folder)
Lx = eval(myOpts.geom['Lx'])

# Two gaussians

# The skew sinus
# In cartesian coordinates we would like a sinus with with a wave-vector in the direction
# 45 degrees with respect to the first quadrant. This can be achieved with a wave vector
# k = [1/sqrt(2), 1/sqrt(2)]
# sin((1/sqrt(2))*(x + y))
# We would like 2 nodes, so we may write
# sin((1/sqrt(2))*(x + y)*(2*pi/(2*Lx)))
# Rewriting this to cylindrical coordinates, gives
# sin((1/sqrt(2))*(x*(cos(z)+sin(z)))*(2*pi/(2*Lx)))

# The gaussian
# In cartesian coordinates we would like
# f = exp(-(1/(2*w^2))*((x-x0)^2 + (y-y0)^2))
# In cylindrical coordinates, this translates to
# f = exp(-(1/(2*w^2))*(x^2 + y^2 + x0^2 + y0^2 - 2*(x*x0+y*y0) ))
#   = exp(-(1/(2*w^2))*(rho^2 + rho0^2 - 2*rho*rho0*(cos(theta)*cos(theta0)+sin(theta)*sin(theta0)) ))
#   = exp(-(1/(2*w^2))*(rho^2 + rho0^2 - 2*rho*rho0*(cos(theta - theta0)) ))

# A parabola
# In cartesian coordinates, we have
# ((x-x0)/Lx)^2
# Chosing this function to have a zero value at the edge yields in cylindrical coordinates
# ((x*cos(z)+Lx)/(2*Lx))^2
# +2 to ensure positivity of n

w = 0.8*Lx
rho0 = 0.3*Lx
theta0 = 5*pi/4
the_vars['n'] = sin((1/sqrt(2))*(x*(cos(z)+sin(z)))*(2*pi/(2*Lx)))*\
                  exp(-(1/(2*w**2))*(x**2 + rho0**2 - 2*x*rho0*(cos(z - theta0)) ))*\
                  ((x*cos(z)+Lx)/(2*Lx))**2\
                  +2

# The gaussian

# In cartesian coordinates we would like
# f = exp(-(1/(2*w^2))*((x-x0)^2 + (y-y0)^2))
# In cylindrical coordinates, this translates to
# f = exp(-(1/(2*w^2))*(x^2 + y^2 + x0^2 + y0^2 - 2*(x*x0+y*y0) ))
#   = exp(-(1/(2*w^2))*(rho^2 + rho0^2 - 2*rho*rho0*(cos(theta)*cos(theta0)+sin(theta)*sin(theta0)) ))
#   = exp(-(1/(2*w^2))*(rho^2 + rho0^2 - 2*rho*rho0*(cos(theta - theta0)) ))
# Multiplication of cos(pi*x/(2*Lx)) to give zero boundaries

w = 0.5*Lx
rho0 = 0.2*Lx
theta0 = pi
the_vars[r'$\phi$'] = exp(-(1/(2*w**2))*(x**2 + rho0**2 - 2*x*rho0*(cos(z - theta0)) ))*cos(pi*x/(2*Lx))


I am working on the file "MES/polAvg/calculations/exactSolutions.ipynb".
The first task is:
Calculating the solution
Can you write Python code for it?

the_vars['S'] = (integrate(the_vars['f'], (z, 0, 2*np.pi))/(2*np.pi)).evalf()


I am working on the file "MES/polAvg/calculations/exactSolutions.ipynb".
The first task is:
Define the function to take the derivative of
NOTE:
These do not need to be fulfilled in order to get convergence

z must be periodic
The field $f(\rho, \theta)$ must be of class infinity in $z=0$ and $z=2\pi$
The field $f(\rho, \theta)$ must be continuous in the $\rho$ direction with $f(\rho, \theta + \pi)$

But this needs to be fulfilled:
1. The field $f(\rho, \theta)$ must be single valued when $\rho\to0$
2. Eventual BC in $\rho$ must be satisfied
Can you write Python code for it?

# We need Lx
from boututils.options import BOUTOptions
myOpts = BOUTOptions(folder)
Lx = eval(myOpts.geom['Lx'])

# Z hat function

# NOTE: The function is not continuous over origo

s = 2
c = pi
w = pi/2
the_vars['f'] = ((1/2)*(tanh(s*(z-(c-w/2)))-tanh(s*(z-(c+w/2)))))*sin(3*2*pi*x/Lx)


I am working on the file "MES/polAvg/calculations/exactSolutions.ipynb".
The first task is:
Define the variables
Can you write Python code for it?


I am working on the file "MES/polAvg/calculations/exactSolutions.ipynb".
The first task is:
Initialize
Can you write Python code for it?

folder = '../zHat/'
metric = get_metric()


I am working on the file "MES/polAvg/calculations/exactSolutions.ipynb".
The first task is:
Plot
Can you write Python code for it?


I am working on the file "MES/singleOperators/1-DDXCylinder/calculations/exactSolutions.ipynb".
The first task is:
Calculating the solution
Can you write Python code for it?

the_vars['S'] = DDX(the_vars['f'], metric=metric)


I am working on the file "MES/singleOperators/1-DDXCylinder/calculations/exactSolutions.ipynb".
The first task is:
Define the function to take the derivative of
NOTE:

z must be periodic
The field $f(\rho, \theta)$ must be of class infinity in $z=0$ and $z=2\pi$
The field $f(\rho, \theta)$ must be single valued when $\rho\to0$
The field $f(\rho, \theta)$ must be continuous in the $\rho$ direction with $f(\rho, \theta + \pi)$
Eventual BC in $\rho$ must be satisfied
Can you write Python code for it?

# We need Lx
from boututils.options import BOUTOptions
myOpts = BOUTOptions(folder)
Lx = eval(myOpts.geom['Lx'])

# Gaussian with sinus and parabola

# The skew sinus
# In cartesian coordinates we would like a sinus with with a wave-vector in the direction
# 45 degrees with respect to the first quadrant. This can be achieved with a wave vector
# k = [1/sqrt(2), 1/sqrt(2)]
# sin((1/sqrt(2))*(x + y))
# We would like 2 nodes, so we may write
# sin((1/sqrt(2))*(x + y)*(2*pi/(2*Lx)))
# Rewriting this to cylindrical coordinates, gives
# sin((1/sqrt(2))*(x*(cos(z)+sin(z)))*(2*pi/(2*Lx)))

# The gaussian
# In cartesian coordinates we would like
# f = exp(-(1/(2*w^2))*((x-x0)^2 + (y-y0)^2))
# In cylindrical coordinates, this translates to
# f = exp(-(1/(2*w^2))*(x^2 + y^2 + x0^2 + y0^2 - 2*(x*x0+y*y0) ))
#   = exp(-(1/(2*w^2))*(rho^2 + rho0^2 - 2*rho*rho0*(cos(theta)*cos(theta0)+sin(theta)*sin(theta0)) ))
#   = exp(-(1/(2*w^2))*(rho^2 + rho0^2 - 2*rho*rho0*(cos(theta - theta0)) ))

# A parabola
# In cartesian coordinates, we have
# ((x-x0)/Lx)^2
# Chosing this function to have a zero value at the edge yields in cylindrical coordinates
# ((x*cos(z)+Lx)/(2*Lx))^2

w = 0.8*Lx
rho0 = 0.3*Lx
theta0 = 5*pi/4
the_vars['f'] = sin((1/sqrt(2))*(x*(cos(z)+sin(z)))*(2*pi/(2*Lx)))*\
                exp(-(1/(2*w**2))*(x**2 + rho0**2 - 2*x*rho0*(cos(z - theta0)) ))*\
                ((x*cos(z)+Lx)/(2*Lx))**2


I am working on the file "MES/singleOperators/1-DDXCylinder/calculations/exactSolutions.ipynb".
The first task is:
Define the variables
Can you write Python code for it?


I am working on the file "MES/singleOperators/1-DDXCylinder/calculations/exactSolutions.ipynb".
The first task is:
Initialize
Can you write Python code for it?

folder = '../properZ/'
metric = get_metric()


I am working on the file "MES/singleOperators/1.1-DDXCylinderWideStencilCenter/calculations/exactSolutions.ipynb".
The first task is:
Define the function to take the derivative of
NOTE:

z must be periodic
The field $f(\rho, \theta)$ must be of class infinity in $z=0$ and $z=2\pi$
The field $f(\rho, \theta)$ must be single valued when $\rho\to0$
The field $f(\rho, \theta)$ must be continuous in the $\rho$ direction with $f(\rho, \theta + \pi)$
Eventual BC in $\rho$ must be satisfied
Can you write Python code for it?


I am working on the file "MES/singleOperators/1.1-DDXCylinderWideStencilCenter/calculations/exactSolutions.ipynb".
The first task is:
Initialize
Can you write Python code for it?

folder = '../gaussian/'
metric = get_metric()


I am working on the file "MES/singleOperators/1.1-DDXCylinderWideStencilCenter/calculations/exactSolutions.ipynb".
The first task is:
Plot
Can you write Python code for it?

make_plot(folder=folder, the_vars=the_vars, plot2d=True, include_aux=False, save=True)


I am working on the file "MES/singleOperators/1.1-DDXCylinderWideStencilCenter/calculations/exactSolutions.ipynb".
The first task is:
Print the variables in BOUT++ format
Can you write Python code for it?


I am working on the file "MES/singleOperators/2-D2DX2Cylinder/calculations/exactSolutions.ipynb".
The first task is:
Calculating the solution
Can you write Python code for it?

the_vars['S'] = DDX(DDX(the_vars['f'], metric=metric), metric=metric)


I am working on the file "MES/singleOperators/2-D2DX2Cylinder/calculations/exactSolutions.ipynb".
The first task is:
Define the function to take the derivative of
NOTE:

z must be periodic
The field $f(\rho, \theta)$ must be of class infinity in $z=0$ and $z=2\pi$
The field $f(\rho, \theta)$ must be single valued when $\rho\to0$
The field $f(\rho, \theta)$ must be continuous in the $\rho$ direction with $f(\rho, \theta + \pi)$
Eventual BC in $\rho$ must be satisfied
Can you write Python code for it?


I am working on the file "MES/singleOperators/2-D2DX2Cylinder/calculations/exactSolutions.ipynb".
The first task is:
Initialize
Can you write Python code for it?


I am working on the file "MES/singleOperators/2-D2DX2Cylinder/calculations/exactSolutions.ipynb".
The first task is:
Plot
Can you write Python code for it?


I am working on the file "MES/singleOperators/2-D2DX2Cylinder/calculations/exactSolutions.ipynb".
The first task is:
Print the variables in BOUT++ format
Can you write Python code for it?


I am working on the file "MES/singleOperators/3-DDXOverJCylinder/calculations/exactSolutions.ipynb".
The first task is:
Calculating the solution
Can you write Python code for it?

the_vars['S'] = (1/metric.J)*DDX(the_vars['f'], metric=metric)


I am working on the file "MES/singleOperators/3-DDXOverJCylinder/calculations/exactSolutions.ipynb".
The first task is:
NOTE:

z must be periodic
The field $f(\rho, \theta)$ must be of class infinity in $z=0$ and $z=2\pi$
The field $f(\rho, \theta)$ must be single valued when $\rho\to0$
The field $f(\rho, \theta)$ must be continuous in the $\rho$ direction with $f(\rho, \theta + \pi)$
Eventual BC in $\rho$ must be satisfied
Can you write Python code for it?

# We need Lx
from boututils.options import BOUTOptions
myOpts = BOUTOptions(folder)
Lx = eval(myOpts.geom['Lx'])

# We make f a tanh function
# NOTE: We get a blow up in S here

# We multiply with cos(6*pi*x/(2*Lx)) in order to give it a modulation, and to get a non-zero value at the boundary
# We multiply with (x/Lx) in order for S not to blow up at rho=0
s = 0.15
c = 50
w = 30
the_vars['f'] = ((1/2) - (1/2)*(tanh(s*(x-(c - (w/2))))))*cos(6*pi*x/(2*Lx))*sin(2*z)


I am working on the file "MES/singleOperators/3-DDXOverJCylinder/calculations/exactSolutions.ipynb".
The first task is:
Plot
Can you write Python code for it?

make_plot(folder=folder, the_vars=the_vars, plot2d=True, include_aux=False)


I am working on the file "MES/singleOperators/3-DDXOverJCylinder/calculations/exactSolutions.ipynb".
The first task is:
Print the variables in BOUT++ format
Can you write Python code for it?


I am working on the file "MES/singleOperators/5-D2DZ2Cylinder/calculations/exactSolutions.ipynb".
The first task is:
Calculating the solution
Can you write Python code for it?

the_vars['S'] = DDZ(DDZ(the_vars['f'], metric=metric), metric=metric)


I am working on the file "MES/singleOperators/5-D2DZ2Cylinder/calculations/exactSolutions.ipynb".
The first task is:
Define the variables
Can you write Python code for it?


I am working on the file "MES/singleOperators/5-D2DZ2Cylinder/calculations/exactSolutions.ipynb".
The first task is:
Plot
Can you write Python code for it?


I am working on the file "MES/singleOperators/properZFailConvergence.ipynb".
The first task is:
The function called "proper Z" (as it )
Can you write Python code for it?

Lx=symbols('Lx')
# We multiply with cos(6*pi*x/(2*Lx)) in order to give it a modulation, and to get a non-zero value at the boundary
s = 0.15
c = 50
w = 30
f = ((1/2) - (1/2)*(tanh(s*(x-(c - (w/2))))))*cos(6*pi*x/(2*Lx))*sin(2*z)
display(Eq(symbols('f'),f))

theLimit = Limit(f,x,0,dir='+')
display(Eq(theLimit, theLimit.doit()))
theLimit = Limit(f,x,0,dir='-')
display(Eq(theLimit, theLimit.doit()))


I am working on the file "MES/singleOperators/subConvergenceDDXOverJ.ipynb".
The first task is:
In the first inner point $x=\frac{h}{2}$, so we get
Can you write Python code for it?

# Cannot have to identical symbols in the order, so we do a workaround
firstInnerJ = (1/metric.J).subs(x,h/2)
invJCFDFirstInner = simplify(CFD*firstInnerJ)
display(invJCFDFirstInner)


I am working on the file "MES/singleOperators/subConvergenceDDXOverJ.ipynb".
The first task is:
The centered difference
The centered finite differecnce approximation can be found by combining the forward and backward finite differences, evaluated half between grid points.
Can you write Python code for it?

# Symbols to easen printing
symFW, symBW, symCFD = symbols('FW, BW, CFD')
x0, h = symbols('x0, h')
f = Function('f')

FW = f(x+h/2).series(x+h/2, x0=x0, n=4)
FW = FW.subs(x-x0,0)
display(Eq(symFW,FW))

BW = f(x-h/2).series(x-h/2, x0=x0, n=4)
BW = BW.subs(x-x0,0)
display(Eq(symBW,BW))

display(Eq(symFW-symBW,FW - BW))
CFD = simplify((FW-BW)/h)
display(Eq(symCFD, CFD))


I am working on the file "METODOINVERSO.ipynb".
The first task is:
Distribucion Exponencial
Funcion de Probabilidad
$$F(x)=\lambda e^{- \lambda x}$$
Funcion cuantil de la Distribucion exponencial
$$F^{-1}(x)=\frac{-ln(1-p)}{\lambda}$$
Can you write Python code for it?

lambda_=1

def funexp(l,x):
    lmda=[]
    for i in range(x):
        lmda.append(l*math.exp(-l*i))
    return lmda
fprobe=funexp(lambda_,x)

def funcexp(x,l):
    p=[]
    pi=[]
    p=gen(x)

    for i in range(x):
        pi.append(-math.log10(1-p[i])/l)
    return pi

fcue=funcuanexp(x,lambda_)
for i in range(len(fcue)):
    print "{0:.2f}".format(fcue[i])


I am working on the file "METODOINVERSO.ipynb".
The first task is:
Distribucion Uniforme Discreta
Funcion de Probabilidad
$$F(x)=\frac{1}{x}$$
Can you write Python code for it?

#funcion inversa binomial
def fig(x):
    p=[]
    p=gena(x)
    pi=[]
    for i in range(x):
        for j in range(len(fag)):
            if p[i]<fag[j]:
                pi.append(j)
                break
    return pi

fing=fig(x)
print fing

#Funcion de probabilidad Uniforme Discreta
def funpun(x):
    fpu=[]
    for i in range(x):
        fpu.append(1/float(x))
    return fpu

fpu=funpun(x)

def funaun(x):
    facu=[]
    facu.append(fpu[0])
    for i in range(x-1):
        facu.append(fpu[i+1]+facu[i])
    return facu

facu=funaun(x)
plt.plot(facu)

#funcion inversa Uniforme
def fiun(x):
    p=[]
    p=gena(x)
    pi=[]
    for i in range(x):
        for j in range(len(facu)):
            if p[i]<facu[j]:
                pi.append(j)
                break
    return pi

fing=fiun(x)
print fing


I am working on the file "MI250 Lab1 simple regression example.ipynb".
The first task is:
Create dataset and plot
Can you write Python code for it?


I am working on the file "MI250 Lab1 simple regression example.ipynb".
The first task is:
Create the model
pymc3 follows a standard structure for model creation.

create an instance of the model
with that model, assign the priors, the expected outcome and then the Likelihood distribution of the expected values
Can you write Python code for it?

basic_model = Model()

with basic_model:

    # Priors for unknown model parameters
    alpha = Normal('alpha', mu=0, sd=100)
    beta = Normal('beta', mu=0, sd=100)
    sigma = Uniform('sigma', lower=0, upper=10000)

    # Expected value of outcome
    mu = alpha + beta*x

    # Likelihood (sampling distribution) of observations
    Y_obs = Normal('Y_obs', mu=mu, sd=sigma, observed=y)


I am working on the file "MI250 Lab1 simple regression example.ipynb".
The first task is:
Inspect the traces
Can you write Python code for it?

traceplot(trace, lines={k: v['mean'] for k, v in df_summary(trace).iterrows()})

summary(trace)

df_summary(trace, alpha =0.1)

def plot_posterior_cr(mdl, trc, rawdata, xlims, npoints=1000):
    '''
    Convenience fn: plot the posterior predictions from mdl given trcs
    '''

    ## extract traces
    trc_mu = trace_to_dataframe(trc)[['alpha','beta']]
    trc_sd = trace_to_dataframe(trc)['sigma']

    ## recreate the likelihood
    x = np.linspace(xlims[0], xlims[1], npoints).reshape((npoints,1))
    X = x ** np.ones((npoints,2)) * np.array([0, 1])
    like_mu = np.dot(X,trc_mu.T) + trc_mu.median()['alpha']
    like_sd = np.tile(trc_sd.T,(npoints,1))
    like = np.random.normal(like_mu, like_sd)

    ## Calculate credible regions and plot over the datapoints
    dfp = pd.DataFrame(np.percentile(like,[2.5, 25, 50, 75, 97.5], axis=1).T
                         ,columns=['025','250','500','750','975'])
    dfp['x'] = x

    pal = sns.color_palette('Purples')
    f, ax1d = plt.subplots(1,1, figsize=(7,7))
    ax1d.fill_between(dfp['x'], dfp['025'], dfp['975'], alpha=0.5
                      ,color=pal[1], label='CR 95%')
    ax1d.fill_between(dfp['x'], dfp['250'], dfp['750'], alpha=0.4
                      ,color=pal[4], label='CR 50%')
    ax1d.plot(dfp['x'], dfp['500'], alpha=0.5, color=pal[5], label='Median')
    _ = plt.legend()
    _ = ax1d.set_xlim(xlims)
    _ = sns.regplot(x='x', y='y', data=rawdata, fit_reg=False
            ,scatter_kws={'alpha':0.8,'s':80, 'lw':2,'edgecolor':'w'}, ax=ax1d)

xlims = (df['x'].min() - np.ptp(df['x'])/10
                 ,df['x'].max() + np.ptp(df['x'])/10)

plot_posterior_cr(basic_model, trace, df, xlims)


I am working on the file "MI250 Lab1 simple regression example.ipynb".
The first task is:
Priors....
Can you write Python code for it?

#find_MAP can be used to establish a useful starting point for the sampling
map_estimate = find_MAP(model=basic_model)

print(map_estimate)


I am working on the file "MI250 Lab4 Population PKPD model.ipynb".
The first task is:
Create the arrays to pass into the model from the data dataframe
Can you write Python code for it?

fxa = np.array(data['fxa.inh.obs'])
cobs = np.array(data.cobs)
dose = np.array(data.dose)

# These variables are used to specify the model parameters that
n_patients = len(data['subject'].unique())
subject = data['subject'].values-1


I am working on the file "MI250 Lab4 Population PKPD model.ipynb".
The first task is:
Create the model
Can you write Python code for it?

pkpd_model = Model()

with pkpd_model:

    # Hyperparameter Priors

    omega_ec50 = Uniform('omega_ec50', lower =0, upper =10)
    mu_ec50 = Lognormal ('mu_ec50', mu =0, tau = 6)

    emax = Uniform('emax', lower=0, upper =100)
    gamma = Uniform('gamma', lower=0, upper=10)

    sigma = Uniform ('sigma', lower =0, upper =100)

    ec50 = Lognormal('ec50', mu=mu_ec50, tau = omega_ec50, shape = n_patients)

    # Expected value of outcome
    fxaMean = (emax*(cobs**gamma)) / ((ec50[subject]**gamma) + (cobs**gamma))

    # Likelihood (sampling distribution) of observations
    effect = Normal('effect', mu=fxaMean, sd =sigma, observed =fxa)
    effect_pred = Normal('effect_pred', mu=fxaMean, sd =sigma, shape =len(cobs))


I am working on the file "MI250 Lab4 Population PKPD model.ipynb".
The first task is:
Initiate the sampling
Can you write Python code for it?

with pkpd_model:

    # obtain starting values via MAP
    start = find_MAP()

    # draw 2000 posterior samples
    trace = sample(2000,  start=start)

t=traceplot(trace[500:])


I am working on the file "MI250 Lab4 Population PKPD model.ipynb".
The first task is:
Lets plot out the posterior predicted values for each value of cobs to get a feel for the model fit - a rough posterior predictive check
Can you write Python code for it?

t_df = trace_to_dataframe(trace)
filter_col = [col for col in list(t_df) if col.startswith('effect_pred__')]
col= pd.DataFrame()
to_col =pd.DataFrame()

for n, cols in enumerate(filter_col):
        to_col['effect_pred']=t_df[cols]
        to_col['cobs'] = cobs[n]
        col = pd.concat([col, to_col])

plt.figure(figsize=(10,10))
plt.scatter(col['cobs'], col['effect_pred'], alpha =0.02, s= 15 ,color ='grey')
plt.scatter(data.cobs, data['fxa.inh.obs'], alpha =1, color='red')
means = col.groupby('cobs', as_index=False).aggregate(np.mean)
plt.scatter(means.cobs, means['effect_pred'], color='blue')
#plt.axis([-10, 100, 0, 15])


I am working on the file "MI250 Lab4 Population PKPD model.ipynb".
The first task is:
Looks pretty good
Plot out the individual predictions for each dose level
Can you write Python code for it?

col= np.empty([1,6])

for n, cols in enumerate(filter_col):

        a = subject[n]+1
        b = dose[n]
        c = cobs[n]
        d = t_df[cols].quantile(q=0.5)
        e = t_df[cols].quantile(q=0.95)
        f = t_df[cols].quantile(q=0.05)
        g = np.array([a,b,c,d,e,f]).reshape(1,6)
        col = np.concatenate((col,g))

col = np.delete(col, (0), axis=0)
col = pd.DataFrame(col, columns=['subject', 'dose', 'cobs', 'mean', 'max', 'min'])
col = col.sort_index(by=['subject', 'cobs'])

effect = sns.FacetGrid(col, col="dose",hue ="subject" ,col_wrap=3, size=3, sharex=False)
effect.map(plt.plot, "cobs", "mean", marker="o", ms=4)
#effect.map(plt.plot, "cobs", "max", linestyle ='--')
#effect.map(plt.plot, "cobs", "min", linestyle ='--')


I am working on the file "MI250 Lecture 4 - Simple Hierarchical mixed effect max model.ipynb".
The first task is:
And now plot individual studies using seaborn
Can you write Python code for it?

effect= sns.FacetGrid(col, col="study",hue ="study" ,col_wrap=3, size=3, sharex=True)
effect.map(plt.plot, "dose", "mean", marker="o", ms=4)
effect.map(plt.plot, "dose", "max", linestyle ='--')
effect.map(plt.plot, "dose", "min", linestyle ='--')


I am working on the file "MI250 Lecture 4 - Simple Hierarchical mixed effect max model.ipynb".
The first task is:
Create dataframe to plot each study separately
Can you write Python code for it?

col= np.empty([1,5])

for n, cols in enumerate(filter_col):
        a = study[n]+1
        b = dose[n]
        c = t_df[cols].quantile(q=0.5)
        d = t_df[cols].quantile(q=0.95)
        e = t_df[cols].quantile(q=0.05)
        f = np.array([a,b,c,d,e]).reshape(1,5)
        col = np.concatenate((col,f))

col = np.delete(col, (0), axis=0)
col = pd.DataFrame(col, columns=['study', 'dose', 'mean', 'max', 'min'])
col = col.sort_index(by=['study'])

col.head()


I am working on the file "MI250 Lecture 4 - Simple Hierarchical mixed effect max model.ipynb".
The first task is:
Initiate the Bayesian sampling
Can you write Python code for it?

import scipy
from pymc3 import find_MAP, NUTS, sample

with pkpd_model:

    # obtain starting values via MAP
    start = find_MAP(fmin=scipy.optimize.fmin_powell)

    # draw 2000 posterior samples
    trace = sample(2000,  start=start)


I am working on the file "MI250 Lecture 4 - Simple Hierarchical mixed effect max model.ipynb".
The first task is:
Manipulate data
Note the data passed into the model must be a np array. Although pandas series work ok in this example in more complex examples I have found that pandas series sometime throw indexing errors
Can you write Python code for it?

mean_response = np.array(data.Mean_response)
dose = np.array(data.Dose)

# Since we are interested in modelling the inter study variation
# we must create some variables to pass into the model parameters
# How many studies...
n_studies = len(data.Study.unique())
# An array that is used to index the studies, reduced by -1 as the index starts at 0 not 1
study = np.array(data.Study.values-1)

# array to adjust sigma for sample size
n= np.array(data.n)


I am working on the file "MI250 Lecture 4 - Simple Hierarchical mixed effect max model.ipynb".
The first task is:
Plot the predicted values from trace on top of the original data
Can you write Python code for it?

t_df = trace_to_dataframe(trace)
filter_col = [col for col in list(t_df) if col.startswith('resp_pred__')]
col= pd.DataFrame()
to_col =pd.DataFrame()

for n, cols in enumerate(filter_col):
        to_col['resp_pred']=t_df[cols]
        to_col['dose'] = dose[n]
        col = pd.concat([col, to_col])
plt.figure(figsize=(6,6))
plt.scatter(col['dose'], col['resp_pred'], alpha =0.02, s= 15 ,color ='grey')
plt.scatter(data.Dose, data.Mean_response, alpha =1, color='red')
means = col.groupby('dose', as_index=False).aggregate(np.mean)
plt.plot(means.dose, means.resp_pred)
plt.axis([-10, 100, 0, 15])


I am working on the file "MIR calculator.ipynb".
The first task is:
Answering the questions you don't know doesn't affect your mean score. It just increases the variability, making it possible to get slightly higher (and lower) marks.
Let's explore if this is affected by the study level.
Can you write Python code for it?

mean_list = []
std_list = []
for study_level in range(0,11):
    distrib1, distrib2 = solve_ManyExams(study_level, max_iter=50000) # Lots of iterations to get clean distributions
    mean_diff = np.mean(distrib1) - np.mean(distrib2)
    std_diff = np.std(distrib1) - np.std(distrib2)
    mean_list.append(mean_diff)
    std_list.append(std_diff)

plt.plot(list(range(0,11)),mean_list, label="Mean score gain")
plt.plot(list(range(0,11)),std_list, label="Variability gain")
plt.xlabel("Study level")
plt.ylabel("Score difference / 10")
plt.xticks([x for x in range(0, 11)])
plt.legend()
plt.show()


I am working on the file "MIR calculator.ipynb".
The first task is:
There's chances that you can luck out and get close to a 1.5/10 even if you haven't studied at all! Notice, however, that if you are unlucky, you might score a -1.5/10. Ouch!
But it looks like a normal distribution centered around 0 to me. Is it actually gaining you anything on average?
Let's take a look at the distribution of marks for the two types of strategy, at the same study level.
Can you write Python code for it?

def solve_ManyExams(study_level, max_iter=1000):
    '''
    Returns two lists:
        distrib1: The marks answering randomly unknown questions
        distrib2: The marks answering only the questions you know.'''
    iteration = 0
    distrib1 = []
    distrib2 = []
    while iteration < max_iter:
        mir1 = MIR(
            study_level=study_level, n_questions=225, answer_unknowns=True)
        mir2 = MIR(
            study_level=study_level, n_questions=225, answer_unknowns=False)
        distrib1.append(mir1.solveExam())
        distrib2.append(mir2.solveExam())
        iteration += 1
    return(distrib1, distrib2)

distrib1, distrib2 = solve_ManyExams(study_level=5)
sns.distplot(distrib1, hist=False, label='Answering unknowns')
sns.distplot(distrib2, hist=False, label='Not answering unknowns')
plt.xlabel("Score / 10")
plt.ylabel("Density")
plt.title("10000 MIRs, study level of 5/10")
plt.legend()
plt.show()

print("Answering unknowns:\t%.2f+-%.2f\n" % (np.mean(distrib1), np.std(distrib1)))
print("Not Answering unknowns:\t%.2f+-%.2f" % (np.mean(distrib2), np.std(distrib2)))


I am working on the file "MIR calculator.ipynb".
The first task is:
What happens if we take the exam 10000 times randomly, without having studied at all?
Can you write Python code for it?

itera = 0
mir = MIR(study_level = 0, n_questions=225, answer_unknowns=True)
max_iter = 10000
while itera < max_iter:
    itera += 1
    plt.plot(mir._getScoreProgress())
plt.ylabel("Score / 10")
plt.xlabel("Question number")
plt.title("{} random MIR exams".format(max_iter))
plt.xlim(0, 225)
plt.show()


I am working on the file "MISC/Faraday.ipynb".
The first task is:
Make a function for the Galactic foreground:
$$
F_{\rm gal}(\phi) = (2\phi_{\rm fg})^{-1}~~~~-\phi_{\rm fg} \lt \phi \lt \phi_{\rm fg}
$$
and zero elsewhere. Therefore:
$$
P_{\rm gal}(\lambda^2) = \frac{\sin (2\phi_{\rm fg} \lambda^2)}{2\phi_{\rm fg}\lambda^2}
$$
Make an array of $\lambda^2$ values:
Can you write Python code for it?


I am working on the file "MISC/Faraday.ipynb".
The first task is:
The total polarized signal will be the sum of the radio galaxy contribution and the Galactic contribution:
Can you write Python code for it?


I am working on the file "MISC/Faraday.ipynb".
The first task is:
We're going to specify that $\phi_{\rm fg}= 2\,{\rm rad\,m^{-2}}$. We can then compute the Galactic contribution at each value of $\lambda^2$:
Can you write Python code for it?

phi_fg = 2.
P_gal = np.sin(2*phi_fg*lam_sq)/(2*phi_fg*lam_sq) + 0*1j


I am working on the file "MISC/ForRory.ipynb".
The first task is:
Create a fake image with a step change:
Can you write Python code for it?


I am working on the file "MISC/ForRory.ipynb".
The first task is:
Plot the fake shape to see how it looks:
Can you write Python code for it?


I am working on the file "MISC/ForRory.ipynb".
The first task is:
Then we detect an edge.
I'm using Canny edge detection to find the edge of the shadow: https://docs.opencv.org/3.3.1/da/d22/tutorial_py_canny.html
I haven't tuned it very finely, so it's not particularly brilliant... it sounds like you algorithm works better, so you should replace this bit.
Can you write Python code for it?

img= cv2.imread('image.png',0)

# Use Otsu filtering to determine thresholds for finding the edge:
high_thresh, thresh_im = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
low_thresh = 0.9*high_thresh

# find the edges using Canny method:
edges = cv2.Canny(img,low_thresh,high_thresh)

# plot the output:
pl.plot(1),pl.imshow(edges,cmap = 'gray')
pl.show()


I am working on the file "MISC/ForRory.ipynb".
The first task is:
To make multiple noise realisations we need to make a loop. This will output $N_{\rm real}$ images, each with the same underlying pattern but a different realisation of noise from the same Gaussian distribution.
Can you write Python code for it?

n_real = 100 # number of realisations

for i in range(0,n_real):

    # same underlying pattern:
    fakeimage = np.zeros((128,128))
    fakeimage[31:96,31:96] = 10.0

    # add a new noise realisation:
    fakeimage+=np.random.normal(0,0.02,size=(128,128))

    # save it to an image:
    pl.imsave("image_"+str(i)+".png",fakeimage)


I am working on the file "MISC/NVSS_selection.ipynb".
The first task is:
Calculate angular distance at this redshift, assuming some cosmology:
Can you write Python code for it?

from astropy.cosmology import WMAP9 as cosmo
D_A = cosmo.kpc_proper_per_arcmin(z)


I am working on the file "MISC/NVSS_selection.ipynb".
The first task is:
Pick a range of sizes to test. Here I'm selecting evenly spaced sizes in log space from 100 to 10000, at a spacing of $10^{0.1}$:
Can you write Python code for it?


I am working on the file "MISC/NVSS_selection.ipynb".
The first task is:
Plot the output:
Can you write Python code for it?

pl.subplot(111)
pl.plot(10**logSize,Prad)
pl.axis([200.,6000.,1e23,1e29])
pl.loglog()
pl.title(r'Detection threshold for $z=0.01$')
pl.ylabel(r'Radio Power [W/Hz]')
pl.xlabel(r'Size [kpc]')
pl.show()


I am working on the file "MISC/NVSS_selection.ipynb".
The first task is:
Set a redshift:
Can you write Python code for it?


I am working on the file "MISC/NVSS_selection.ipynb".
The first task is:
Use the NVSS 5$\sigma$ threshold as our limiting point. $\sigma = 0.5$mJy/beam.
Can you write Python code for it?


I am working on the file "MISC/NVSS_selection.ipynb".
The first task is:
Work out the area for each source using our simple 2 circle model (note that AngSize is an array so this will also be an array):
Can you write Python code for it?

Area = 2.*(np.pi*(AngSize/4.)**2)  # arcmin^2


I am working on the file "MISC/NVSS_selection.ipynb".
The first task is:
Work out the integrated flux density for this source:
Can you write Python code for it?


I am working on the file "MISC/NVSS_selection.ipynb".
The first task is:
Work out the luminosity distance at this redshift and convert from Mpc to metres:
Can you write Python code for it?

mpc2m = 3.09e22
D_L = cosmo.luminosity_distance(z)*mpc2m


I am working on the file "MITgcm_WOA13_mixing.ipynb".
The first task is:
Cumulative Distribution
By integrating
$$ \int_{\sigma_{min}}^\sigma V(\sigma) d\sigma $$
we obtain the cumulative distribution function.
Can you write Python code for it?

fig = plt.figure(figsize=(14,6))
ax = fig.add_subplot(111)

for k in atlases:

    delta_rho = rholevs[1] - rholevs[0]
    ds = dsets[k]
    vol_net = ds.volume_rho.sel(Y=slice(-80,-30)).sum(dim=('X','Y'))
    vol_cum = vol_net.values.cumsum(axis=0)
    plt.plot(rholevs[1:], vol_cum, '.-')

plt.legend(atlases, loc='upper left')
plt.title(r'Southern Ocean Volume Cumulative Density (m$^3$)')
ax.set_xlabel(r'$\sigma_0$')
ax.grid()


I am working on the file "MITgcm_WOA13_mixing.ipynb".
The first task is:
Vertical Diffusive Fluxes
Can you write Python code for it?

rk_sign = -1

plt.figure(figsize=(14,6))
for n, (name, k) in enumerate(zip(
                   ['THETA', 'SALT', 'SIGMA0'],
                   ['_TH', '_SLT', 'Tr01'])):

    ax = plt.subplot(1,3,n+1)
    for aname in dsets:
        ds  = dsets[aname]
        net_vflux = rk_sign*(
                        ds['DFrI' + k] + ds['DFrE' + k]
                    )[0].sel(Y=slice(-80,-30)).sum(dim=('X','Y'))
        plt.plot(net_vflux, net_vflux.Zl)

    ax.set_ylim([-2000,0])
    vmax = abs(net_vflux[5:]).max()
    ax.set_xlim([-vmax, vmax])
    plt.grid()
    plt.legend(atlases, loc='lower right')
    plt.title('Net SO vertical flux of %s' % name)
    plt.xlabel(r'%s units * m$^{3}$ / s' % name)


I am working on the file "MITgcm_WOA13_mixing.ipynb".
The first task is:
Volume Distribution
The volume distribution function $V(\sigma_0)$ is normalized by the bin size, giving a results that is independent of the choice of density bin spacing.
Can you write Python code for it?

def calc_volume(ds, rholevs, zrange=slice(0,-6000)):
    vol = ds.HFacC * ds.drF * ds.rA
    delta_rho = rholevs[1] - rholevs[0]
    ds['volume_rho'] = xgcm.regrid_vertical(vol.sel(Z=zrange),
                                   ds.TRAC01[0].sel(Z=zrange),
                                   rholevs, 'Z') / delta_rho
for ds in dsets.values():
    calc_volume(ds, rholevs)

fig = plt.figure(figsize=(14,6))
ax = fig.add_subplot(111)

for k in atlases:

    ds = dsets[k]
    # net transformation in Southern Ocean
    vol_net = ds.volume_rho.sel(Y=slice(-80,-30)).sum(dim=('X','Y'))
    vol_net.plot.line(marker='.')

plt.legend(atlases, loc='upper left')
plt.title(r'Southern Ocean Volume Density [m$^3$ / (kg m$^{-3}$)]')
ax.set_xlabel(r'$\sigma_0$')
ax.grid()


I am working on the file "ML Algorithm - Random Forests.ipynb".
The first task is:
Forests of randomized trees
The sklearn.ensemble module includes two averaging algorithms based on randomized decision trees: the RandomForest algorithm and the Extra-Trees method. These two has perturb-and-combine style: a diverse set of classifiers is created by introducing randomness in the classifier construction. The prediction of the ensemble is given as the averaged prediction of the individual classifiers.
Can you write Python code for it?

from sklearn.ensemble import RandomForestClassifier
X = [[0,0], [1,1]]
Y = [0, 1]
clf = RandomForestClassifier(n_estimators=10)
clf = clf.fit(X,Y)


I am working on the file "ML Algorithm - Ridge Regression.ipynb".
The first task is:
Setting the regularization parameter: generalized Cross-Validation
RidgeCV implements the ridge regression with built-in cross-validation of the alpha parameter.
Can you write Python code for it?

from sklearn import linear_model
clf = linear_model.RidgeCV(alphas=[0.1, 1.0, 10.0])
clf.fit([[0,0],[0,0],[1,1]],[0,.1,1])

clf.alpha_


I am working on the file "ML Process - Preprocessing.ipynb".
The first task is:
Another utility class: StandardScaler, that implements the Transformer API to compute the mean and standard deviation on a training dataset so as to be able to later reapply the same transformation on the testing set.
Can you write Python code for it?

scaler = preprocessing.StandardScaler().fit(X)
scaler

scaler.mean_

scaler.scale_

scaler.transform(X)

scaler.transform([[-1.,1.,0.]])


I am working on the file "ML Process - Preprocessing.ipynb".
The first task is:
Scaling features to a range
Example: scaling feature to lie between a given minimum and maximum value, often between zero and one. Or the maximum absolute value of each feature is scaled to unit size
Use MinMaxScaler or MaxAbsScaler.
Can you write Python code for it?

X_train = np.array([[1., -1., 2.],
                    [2.,  0., 0.],
                    [0.,  1.,-1.]])
min_max_scaler = preprocessing.MinMaxScaler()
X_train_minmax = min_max_scaler.fit_transform(X_train)
X_train_minmax

X_test = np.array([[-3., -1., 4.]])
X_test_minmax = min_max_scaler.transform(X_test)
X_test_minmax

min_max_scaler.scale_

min_max_scaler.min_


I am working on the file "ML Process - Preprocessing.ipynb".
The first task is:
The preprocessing module provides a companion function binarize to be used when the transformer API is not necessary.
binarize and Binarizer accept both dense array-like and sparse matrices from scipy.sparse as input.
Encoding categorical features
Integer representation cannot be used directly with scikit-learn estimators, as these expect continuous input, and would interpret the categories as being ordered, which is often not desired.
One possibility to convert categorical features  to features that can be used with scikit-learn estimators is to use a one-of-K or one-hot encoding, which is implemented in OneHotEncoder. This estimator transforms each categorical feature with m possible values into m binary features, with only one active.
Can you write Python code for it?


I am working on the file "ML dataset - Iris.ipynb".
The first task is:
Apply Random Forests
Can you write Python code for it?

from sklearn.ensemble import RandomForestClassifier
clf = RandomForestClassifier(n_estimators = 10)
clf.fit(X_train, y_train)

clf.predict(X_test)

y_test

## Cross validation score
clf.score(X_test, y_test)


I am working on the file "ML+part+2.ipynb".
The first task is:
Other examples
https://nbviewer.jupyter.org/gist/decisionstats/8b762caa7b7deebb68e3f275daf02a9d
https://nbviewer.jupyter.org/gist/decisionstats/47a2324b14ebfd22657b40ec1ae5b480
Can you write Python code for it?

import matplotlib.pyplot as plt
%matplotlib  inline
from collections import OrderedDict
from sklearn.datasets import make_classification
from sklearn.ensemble import RandomForestClassifier, ExtraTreesClassifier

# Author: Kian Ho <hui.kian.ho@gmail.com>
#         Gilles Louppe <g.louppe@gmail.com>
#         Andreas Mueller <amueller@ais.uni-bonn.de>
#
# License: BSD 3 Clause

print(__doc__)

RANDOM_STATE = 123

# Generate a binary classification dataset.
X, y = make_classification(n_samples=500, n_features=25,
                           n_clusters_per_class=1, n_informative=15,
                           random_state=RANDOM_STATE)

# NOTE: Setting the `warm_start` construction parameter to `True` disables
# support for parallelized ensembles but is necessary for tracking the OOB
# error trajectory during training.
ensemble_clfs = [
    ("RandomForestClassifier, max_features='sqrt'",
        RandomForestClassifier(warm_start=True, oob_score=True,
                               max_features="sqrt",
                               random_state=RANDOM_STATE)),
    ("RandomForestClassifier, max_features='log2'",
        RandomForestClassifier(warm_start=True, max_features='log2',
                               oob_score=True,
                               random_state=RANDOM_STATE)),
    ("RandomForestClassifier, max_features=None",
        RandomForestClassifier(warm_start=True, max_features=None,
                               oob_score=True,
                               random_state=RANDOM_STATE))
]

# Map a classifier name to a list of (<n_estimators>, <error rate>) pairs.
error_rate = OrderedDict((label, []) for label, _ in ensemble_clfs)

# Range of `n_estimators` values to explore.
min_estimators = 15
max_estimators = 175

for label, clf in ensemble_clfs:
    for i in range(min_estimators, max_estimators + 1):
        clf.set_params(n_estimators=i)
        clf.fit(X, y)

        # Record the OOB error for each `n_estimators=i` setting.
        oob_error = 1 - clf.oob_score_
       ...


I am working on the file "ML+part+2.ipynb".
The first task is:
http://archive.ics.uci.edu/ml/datasets/Pen-Based+Recognition+of+Handwritten+Digits
We create a digit database by collecting 250 samples from 44 writers.
Can you write Python code for it?

from sklearn import datasets
iris = datasets.load_iris()
digits = datasets.load_digits()

from sklearn import svm
clf = svm.SVC(gamma=0.001, C=100.)

digits.data

digits.target

iris.data

iris.target

clf.fit(digits.data[:-1], digits.target[:-1])

clf.predict(digits.data[-1:])

clf = svm.SVC()
iris = datasets.load_iris()
X, y = iris.data, iris.target
clf.fit(X, y)

import pickle
s = pickle.dumps(clf)
clf2 = pickle.loads(s)
clf2.predict(X[0:1])

y[0]

y

clf2.predict(X)

(clf2.predict(X) != y).sum()

from sklearn import tree
from sklearn.datasets import load_iris

iris.target

clf = tree.DecisionTreeClassifier()
clf = clf.fit(iris.data, iris.target)

clf

clf.predict(X)

iris.target

(clf.predict(X)!=iris.target).sum()

! pip install pydotplus

import pydotplus as pydot

from IPython.display import Image

from sklearn.externals.six import StringIO


I am working on the file "ML-Example1.ipynb".
The first task is:
Training a classifier
Can you write Python code for it?

titanic_X = titanic_df[['Pclass', 'Sex', 'Age']].as_matrix()
titanic_y = titanic_df['Survived'].as_matrix()

shape(titanic_X), shape(titanic_y)

titanic_X

titanic_y

from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()

titanic_X[:,1] = le.fit_transform(titanic_X[:,1])
print titanic_X

from sklearn.preprocessing import Imputer
imp = Imputer(missing_values='NaN', strategy='mean', axis=0)
titanic_X = imp.fit_transform(titanic_X)
print titanic_X

from sklearn import tree
clf = tree.DecisionTreeClassifier(max_depth=3)
clf = clf.fit(titanic_X, titanic_y)

titanic_y_predicted = clf.predict(titanic_X)

titanic_df.insert(1,'Survior pred', titanic_y_predicted)
titanic_df

from sklearn.metrics import accuracy_score
accuracy_score(titanic_y, titanic_y_predicted)

from sklearn.externals.six import StringIO
import pyparsing
import pydot
from IPython.display import Image
dot_data = StringIO()
tree.export_graphviz(clf, out_file=dot_data, feature_names=['Pclass', 'Sex', 'Age'])
graph = pydot.graph_from_dot_data(dot_data.getvalue())
img = Image(graph.create_png())

img.width=800; img

titanic_y_predicted_proba = clf.predict_proba(titanic_X)

titanic_df.insert(2,'Survior prob', titanic_y_predicted_proba[:,1])
titanic_df

# Pclass, Sex, Age
x_male_student = sp.array([3, 1, 21])
x_rich_countess = sp.array([1, 0, 65])
x_midleclass_mother = sp.array([2, 0, 40])
x_baby = sp.array([1, 0, 1])

clf.predict_proba(x_male_student)

clf.predict_proba(x_rich_countess)

clf.predict_proba(x_midleclass_mother)

clf.predict_proba(x_baby)


I am working on the file "ML-LR-part2.ipynb".
The first task is:
After you have "un-zipped" the data file you have a file named kc_house_data.csv" We will load that into a pandas data frame and take a look at the first few lines of data. [ We will call the data-frame df for now, which is common practice.]
Can you write Python code for it?

df = pd.read_csv("kc_house_data.csv")  # create a dataframe with pandas "pd"

df.head()  # display the first few lines of the dataframe "df"


I am working on the file "ML-LR-part2.ipynb".
The first task is:
Here is some basic stats for some of the data.
Can you write Python code for it?


I am working on the file "ML-LR-part2.ipynb".
The first task is:
How about the average house sale price in each zipcode ...
Can you write Python code for it?

df.groupby('zipcode')['price'].mean()  # group by zipcode and compute the mean of prices in a zipcode


I am working on the file "ML-LR-part2.ipynb".
The first task is:
It looks like there are 70 different sip codes in King county. Lets see how many house sales there were in each.
Can you write Python code for it?


I am working on the file "ML-LR-part2.ipynb".
The first task is:
The data looks very clean. We wont have to do any type or format conversions. Lets make a quick check for missing values. [ looks OK ]
Can you write Python code for it?

df.isnull().values.any()  # check for missing values


I am working on the file "ML-LR-part2.ipynb".
The first task is:
The increase of price with sqft_living space is pretty clear and the "Pearson r value" is .7 indicating a reasonable correlation.  However, the data distributions show a big concentration of values in the lower left of the plot. That makes sense, most houses are between 1200 and 3000 sqft and a few hundred thousand dollars. We can eliminate the very expensive and very large houses and take another look at the data.
If we set the size (xlim) from 500 to 3500sqft and the price (ylim) from 100,000 to \$1,000,000 the data still shows the trend but it looks very scattered.
Can you write Python code for it?

sns.jointplot('sqft_living','price', data=df2, xlim=(500,3500), ylim=(100000,1000000), size=10, alpha=.5, marker='+')


I am working on the file "ML-LR-part2.ipynb".
The first task is:
The two zipcodes that look the most interesting to me are 98103 and 98039. 98103 has the most house sale values, 602, with an average sale price of \$584,919.  The most expensive zipcode 98039 has 50 sale values with an average sale price of \$2,160,606.
We can create "selectors" by creating lists of true-false values for data entries that match these two zipcodes and then use those to filter our data-frame.
Can you write Python code for it?


I am working on the file "ML-LR-part2.ipynb".
The first task is:
Using the "selectors" above we can look at plots of price vs sqft_living in those zipcodes.
Can you write Python code for it?

sns.jointplot('sqft_living','price', data=df2[zip98103], size=10, alpha=.5, marker='+')

sns.jointplot('sqft_living','price', data=df2[zip98039], size=10, alpha=.5, marker='+')


I am working on the file "ML-LR-part2.ipynb".
The first task is:
You can see that "lot" size is not well correlated to price but the data for living space is reasonable. Visually the best feature to use looks like sqft_living as we expected.
Lets pull that data out of the data-frame into a new frame.
Can you write Python code for it?

df2 = df[["price", "sqft_living"]]
df2.head()


I am working on the file "ML-LR-part3.ipynb".
The first task is:
Hopefully that gives you an idea of where we are going next when we look at an algorithm and program to find the minimum of $J$.
Just so you don't think that I've forgotten about our 98039 zipcode housing data here's a plot of that with errors added in and a line fit to the data.
Can you write Python code for it?

df_98039 = pd.read_csv("df_98039.csv")
x = df_98039['sqft_living']
y = df_98039['price']
fig, ax = plt.subplots()
ax.plot(x,y, 'o', color='g', label='training data')
ax.plot(x, 5000 + 560*x, label='h(x) = 5000 + 560*x')
for i in range(len(x)):
    ax.plot([x[i], x[i]], [5000 + 560*x[i],y[i]], '-', color='c')
plt.legend();


I am working on the file "ML-LR-part4.ipynb".
The first task is:
Alternative to back transformation of parameters
Instead of doing the back transformation above it is also simple to to just keep the optimized parameters
from the scaled calculation and transform input and output to the model function.
We use a transformed value of $x$ for input, compute the value of h,
and then use the inverse transformation of $y$ to get the original scale for the 'price'.
$$ x' = \frac{x - \overline x }{\sigma_x}, \quad y = \sigma_y y' + \overline y $$
or
$$ h(x) =  \sigma_y \left(a_0 + a_1 \left( \frac{x - \overline x }{\sigma_x} \right) \right) + \overline y$$
That is probably an easier way to transform back to the original values.
Can you write Python code for it?

def h_predict(x_sqft, a0, a1) :
    h_predict = y_std * (a0 + a1 * (x_sqft - x_mean)/x_std) + y_mean
    return h_predict

a = 0; a1 = 0.940886884316
h_predict(8000, a0, a1)


I am working on the file "ML-LR-part4.ipynb".
The first task is:
Back transformation to original data scales
The data transformation was obviously very helpful to solve the problem but it would be nice to get the "solved" model function back to it's original value scales. For that we will need to do a back transformation. It is a bit tricky since the cost function $J(a_0,a_1)$ has a dependency on $y$.
Near the minimum $h_a(x)$ is approximately equal to $y$.  So in the transformed variables we have,
$$ h'(x') = \frac{h(x)-\overline h(x)}{\sigma_h} = a_0 + a_1 \left( \frac{x - \overline x}{\sigma_x} \right) = y' = \frac{h(x) - \overline y}{\sigma_y}  $$
solving for $h(x)$ gives,
$$ h_a(x) = a_0 \sigma_y + \overline y - a_1 \frac{\sigma_y}{\sigma_x} \overline x + a_1 \frac{\sigma_y}{\sigma_x} x$$
That is just the equation of a line with
$$ b_1 = slope = a_1 \frac{\sigma_y}{\sigma_x} $$
and intercept
$$ b_0 = intercept = a_0 \sigma_y + \overline y - a_1 \frac{\sigma_y}{\sigma_x} \overline x$$
Thus, we can define $h_b(x)$ with a back transformation as,
$$ h_b(x) = b_0 + b_1 x$$
This is equivalent to our original model function.
Here's a plot in the original values of our data and the model $h_b(x)$ using the optimized parameters we found, $a_0 = 0, a_1 = 0.9409$ with the mean normalized data.
Can you write Python code for it?

a0 = 0; a1 = 0.940886884316
b1 = y_std/x_std * a1
b0 = y_std * a0 - b1*x_mean + y_mean

fig, ax = plt.subplots()
ax.plot(x,y, 'o', color='g', label='training data')
ax.plot(x, b0 + b1*x, label='h(x) = %.2f + %.2f x' %(b0, b1))
plt.legend();


I am working on the file "ML-LR-part4.ipynb".
The first task is:
From that model we can predict that the cost of an 8000 sqft house in zipcode 98039 would be
$$ h_b(8000) = \$4772453.17$$
I can't afford it!
Can you write Python code for it?


I am working on the file "ML-LR-part4.ipynb".
The first task is:
Here is what happens if your step size is too small. It's slow to converge.
Can you write Python code for it?

def plot_steps( guess, alpha, nsteps) :
    fig, ax = plt.subplots()
    x = np.linspace(-3,3,100)
    ax.plot(x, f(x))
    x = guess
    ax.plot(x,f(x), 'o', label='start x=%.2f' %x )
    for i in range(nsteps):
        xold = x
        x = xp1(x,alpha)
        ax.plot(x,f(x), 'o', label='x = %.2f' %x)
        ax.plot([xold,x],[f(xold),f(x)], '-')
    plt.legend();

plot_steps( -2.5, 0.02, 10 )


I am working on the file "ML-LR-part4.ipynb".
The first task is:
Here's a quiz for you :-) If you set the step size $\alpha$ to 0.5 how many steps will it take to find the minimum? [Hint: think about where the minimum is i.e. $x=0$ then look at the gradient descent update formula $x_{k+1} \gets x_k - \alpha 2x_k$ -- You could cheat too and download the notebook and make the plot
The step size problem is not too bad if you have your data scaled well (preconditioned). More sophisticated optimization algorithms would use something called a "line search" to find a good step size.

It is important to understand the algorithms you use and know what the algorithm parameters do. If you just blindly use some code you may not get very good results and waste a lot of time.

Gradient Descent parameter optimization for 98039 zipcode housing data.
OK, lets do this with our Housing "price" vs "sqft_living" data. We will iterate through gradient descent and look at the results as we go.
First get the data loaded. It's on my github page with the notebooks
Can you write Python code for it?

df_98039 = pd.read_csv("df_98039.csv")
x = df_98039['sqft_living']
y = df_98039['price']
m = len(x)  # Number of data pairs


I am working on the file "ML-LR-part4.ipynb".
The first task is:
If it's beyond a critical size it will "blow up" and start going up hill to infinity!
Can you write Python code for it?


I am working on the file "ML-LR-part4.ipynb".
The first task is:
If the step size is too big it may overshoot the minimum and oscillate without getting to the minimum.
Can you write Python code for it?


I am working on the file "ML-LR-part4.ipynb".
The first task is:
It is converging to $a_0 = 0$ and a_1 = 0.9409. Notice how simple the contours look now!

With this mean normalization it is 100 time faster to converge than our first scaling try, and that first attempt wasn't even at the minimum yet!

So that you can see how the line of the model function changes during the gradient descent optimization I'll plot a few iterations of the data and model function as the parameters $a_0, a_1$ update.
Can you write Python code for it?

def plot_model(x,y,a0,a1) :
    fig, ax = plt.subplots()
    ax.plot(x,y, 'o', color='g', label='training data')
    ax.plot(x, h(xs, a0, a1), label='h(x) = %.2f + %.2f * x' %(a0,a1)  )
    plt.legend();

a0 = -5; a1=5
for i in range(5):
    a0,a1 = ap1(a0,a1, gJ=gradJ(a0,a1,xs,ys,m), alpha=.6 )
    plot_model(xs,ys,a0,a1)