Interactive demos in IPython


In [9]:
%matplotlib notebook
from __future__ import division, print_function
import numpy as np
import scipy.linalg as LA
import matplotlib.pyplot as plt
from ipywidgets import interact
from IPython.display import display
from matplotlib import rcParams


rcParams['font.family'] = 'serif'
rcParams['font.size'] = 16

Fourier series of periodic functions


In [2]:
def waves(N=10, f=1, wtype='square'):
    t = np.linspace(0, 2, 1000)
    x = np.zeros_like(t)
   
    for k in range(1, N+1):
        if wtype=='square':
            x = x + 4/np.pi*np.sin(2*np.pi*(2*k - 1)*f*t)/(2*k-1)
        if wtype=='sawtooth':
            x = x + 2*(-1)**(k+1)/np.pi*np.sin(2*np.pi*k*f*t)/k
        if wtype=='triangle':
            n = k - 1
            x = x + 8/np.pi**2*(-1)**n*np.sin(2*np.pi*(2*n + 1)*f*t)/(2*n +1)**2
    
    plt.subplots(figsize=(8,4))
    plt.plot(t, x, lw=2)
    plt.ylim(-1.5, 1.5)

In [3]:
w = interact(waves,
             N=(1,400),
             f=(1.,10.),
             wtype=('square','sawtooth','triangle'))


Mohr circle for 2D stresses


In [4]:
def mohr(S11=0, S12=0, S22=1):
    """Plot Mohr circle for a 2D tensor"""
    center = [(S11 + S22)/2.0, 0.0]
    radius = np.sqrt((S11 - S22)**2/4.0 + S12**2)
    Smin = center[0] - radius
    Smax = center[0] + radius
    
    
    print("Minimum Normal Stress: ", np.round(Smin,6))
    print("Maximum Normal Stress: ", np.round(Smax, 6))
    print("Average Normal Stress: ", np.round(center[0], 6))
    print("Minimum Shear Stress: ", np.round(-radius, 6))
    print("Maximum Shear Stress: ", np.round(radius, 6))
   
    plt.subplots(figsize=(6,6))
    circ = plt.Circle((center[0],0), radius, facecolor='#cce885', lw=3,
    edgecolor='#5c8037') 
    plt.axis('image')
    ax = plt.gca() 
    ax.add_artist(circ)
    ax.set_xlim(Smin - .1*radius, Smax + .1*radius)
    ax.set_ylim(-1.1*radius, 1.1*radius)
    plt.plot([S22, S11], [S12, -S12], 'ko')
    plt.plot([S22, S11], [S12, -S12], 'k')
    plt.plot(center[0], center[1], 'o', mfc='w')
    plt.text(S22 + 0.1*radius, S12, 'A')
    plt.text(S11 + 0.1*radius, -S12, 'B')
    plt.xlabel(r"$\sigma$", size=18)
    plt.ylabel(r"$\tau$", size=18)

In [5]:
w = interact(mohr,
             S11=(-100.,100.),
             S12=(-100.,100.),
             S22=(-100.,100.))


Minimum Normal Stress:  0.0
Maximum Normal Stress:  1.0
Average Normal Stress:  0.5
Minimum Shear Stress:  -0.5
Maximum Shear Stress:  0.5

#


In [36]:
def mohr3D(S11=1, S22=2, S33=3, S23=0, S13=0, S12=0):
    """Plot Mohr circle for a 2D tensor"""
    S =  np.array([
        [S11, S12, S13],
        [S12, S22, S23],
        [S13, S23, S33]])
    vals = LA.eigvalsh(S)
    vals.sort()
    S3, S2, S1 = vals
    print("Principal values: {}, {}, {}".format(S1, S2, S3))
    c1 = 0.5*(S1 + S3)
    r1 = 0.5*(S1 - S3)
    c2 = 0.5*(S2 + S3)
    r2 = 0.5*(S2 - S3)
    c3 = 0.5*(S1 + S2)
    r3 = 0.5*(S1 - S1)
    Smin = c1 - r1
    Smax = c1 + r1
     
    plt.subplots(figsize=(6,6))
    circ1 = plt.Circle((c1,0), r1, facecolor='#cce885', lw=3,
    edgecolor='#5c8037')
    circ2 = plt.Circle((c2,0), r2, facecolor='white', lw=3,
    edgecolor='#377eb8')
    circ3 = plt.Circle((c3,0), r3, facecolor='white', lw=3,
    edgecolor='#e41a1c')
    plt.axis('image')
    ax = plt.gca() 
    ax.add_artist(circ1)
    ax.add_artist(circ2)
    ax.add_artist(circ3)
    ax.set_xlim(Smin - .1*r1, Smax + .1*r1)
    ax.set_ylim(-1.1*r1, 1.1*r1)
    plt.xlabel(r"$\sigma$", size=18)
    plt.ylabel(r"$\tau$", size=18)

In [37]:
w = interact(mohr3D,
             S11=(-100.,100.),
             S22=(-100.,100.),
             S33=(-100.,100.),
             S23=(-100.,100.),
             S13=(-100.,100.),
             S12=(-100.,100.))


Principal values: 3.0, 2.0, 1.0

In [24]:
S1, S2, S3 = vals

In [ ]:


In [25]:
vals


Out[25]:
array([ 1.,  2.,  3.])

In [7]:
from IPython.core.display import HTML
def css_styling():
    styles = open('./styles/custom_barba.css', 'r').read()
    return HTML(styles)
css_styling()


Out[7]:

In [ ]: