acgc.modetools API documentation

# acgc.modetools

Graphical display of eigenmodes

View Source

     1#!/usr/bin/env python3
     2'''Graphical display of eigenmodes'''
     3
     4import numpy as np
     5import matplotlib.pyplot as plt
     6
     7def show_modes( labels, eigval, eigvec, ax=None ):
     8    '''Plot figure showing eigenmodes
     9
    10    This program assumes that all modes will be displayed, so it is expected that
    11    len(labels) = len(eigval) = nmodes and eigvec has shape nmodes x nmodes.
    12
    13    Arguments
    14    ---------
    15    labels : list, str
    16        names for the component variables decomposed into modes
    17    eigval : list or array, float
    18        eigenvalues
    19    eigvec : 2D array, float
    20        eigenvector matrix. Vectors should be the columns of eigvec
    21    ax : subplot axis (optional)
    22        location where graph will be displayed. If not provided, then a new axis will be created.
    23    '''
    24
    25    # Number of modes
    26    nmodes = len(eigval)
    27
    28    # Ensure number of labels equals number of modes
    29    if len(labels) != nmodes:
    30        raise ValueError( 'Provide one label per eigenmode')
    31
    32    # Create new axis if one is not passed
    33    if ax is None:
    34        ax = plt.axes()
    35
    36    # Sort the modes from largest to smallest eigenvalue
    37    idx = np.argsort( np.abs(eigval) )[::-1]
    38    eigval = eigval[idx]
    39    eigvec = eigvec[:,idx]
    40
    41    # Plot the modes
    42    for i in range(nmodes):
    43        # Vertical gray line for reference
    44        ax.plot( [i*2,i*2], [0,nmodes-1], color='gray')
    45        # Plot mode i
    46        ax.plot( i*2 + eigvec[::-1,i],  np.arange(nmodes), 'o-' )
    47
    48    # xaxis labels
    49    xlabel = [ '{:#.2g}\n({:#.2g})'.format(x,np.abs(1/x)) for x in eigval]
    50
    51    # Set ticks and labels
    52    ax.set( yticks=np.arange(nmodes),
    53            yticklabels=labels[::-1],
    54            xticks=np.arange(nmodes)*2,
    55            xticklabels=xlabel,
    56            ylabel='Reservoir',
    57            xlabel='Modes: Eigenvalue\n(Time scale)')
    58
    59    plt.tight_layout()

def show_modes(labels, eigval, eigvec, ax=None): View Source

     8def show_modes( labels, eigval, eigvec, ax=None ):
     9    '''Plot figure showing eigenmodes
    10
    11    This program assumes that all modes will be displayed, so it is expected that
    12    len(labels) = len(eigval) = nmodes and eigvec has shape nmodes x nmodes.
    13
    14    Arguments
    15    ---------
    16    labels : list, str
    17        names for the component variables decomposed into modes
    18    eigval : list or array, float
    19        eigenvalues
    20    eigvec : 2D array, float
    21        eigenvector matrix. Vectors should be the columns of eigvec
    22    ax : subplot axis (optional)
    23        location where graph will be displayed. If not provided, then a new axis will be created.
    24    '''
    25
    26    # Number of modes
    27    nmodes = len(eigval)
    28
    29    # Ensure number of labels equals number of modes
    30    if len(labels) != nmodes:
    31        raise ValueError( 'Provide one label per eigenmode')
    32
    33    # Create new axis if one is not passed
    34    if ax is None:
    35        ax = plt.axes()
    36
    37    # Sort the modes from largest to smallest eigenvalue
    38    idx = np.argsort( np.abs(eigval) )[::-1]
    39    eigval = eigval[idx]
    40    eigvec = eigvec[:,idx]
    41
    42    # Plot the modes
    43    for i in range(nmodes):
    44        # Vertical gray line for reference
    45        ax.plot( [i*2,i*2], [0,nmodes-1], color='gray')
    46        # Plot mode i
    47        ax.plot( i*2 + eigvec[::-1,i],  np.arange(nmodes), 'o-' )
    48
    49    # xaxis labels
    50    xlabel = [ '{:#.2g}\n({:#.2g})'.format(x,np.abs(1/x)) for x in eigval]
    51
    52    # Set ticks and labels
    53    ax.set( yticks=np.arange(nmodes),
    54            yticklabels=labels[::-1],
    55            xticks=np.arange(nmodes)*2,
    56            xticklabels=xlabel,
    57            ylabel='Reservoir',
    58            xlabel='Modes: Eigenvalue\n(Time scale)')
    59
    60    plt.tight_layout()

Plot figure showing eigenmodes

This program assumes that all modes will be displayed, so it is expected that len(labels) = len(eigval) = nmodes and eigvec has shape nmodes x nmodes.

###### Arguments

labels : list, str names for the component variables decomposed into modes eigval : list or array, float eigenvalues eigvec : 2D array, float eigenvector matrix. Vectors should be the columns of eigvec ax : subplot axis (optional) location where graph will be displayed. If not provided, then a new axis will be created.