def main(argv=None): import argparse if argv is None: # Usual case argv = sys.argv[1:] parser = argparse.ArgumentParser( description='''Plot differences between estimates of the Perron Frobenius function of an integral operator''') parser.add_argument('--u', type=float, default=(2.0e-5), help='log fractional deviation') parser.add_argument('--dy', type=float, default=3.2e-4, help='y_1-y_0 = log(x_1/x_0)') parser.add_argument('--n_g0', type=int, default=200, help='number of integration elements in value') parser.add_argument( '--n_h0', type=int, default=200, help= 'number of integration elements in slope. Require n_h > 192 u/(dy^2).' ) parser.add_argument('--n_g1', type=int, default=225) parser.add_argument('--n_h1', type=int, default=225) args = parser.parse_args(argv) d_g = {'big': 2 * args.u / args.n_g0, 'small': 2 * args.u / args.n_g1} h_lim = np.sqrt(48 * args.u) d_h = {'big': 2 * h_lim / args.n_h0, 'small': 2 * h_lim / args.n_h1} tol = 5e-6 maxiter = 150 LO_ = {} for size in ('small', 'big'): op = LO(args.u, args.dy, d_g[size], d_h[size]) op.power(small=tol, n_iter=maxiter) print('For %s, n_g=%d and n_h=%d\n' % (size, op.n_g, op.n_h)) LO_[size] = op diffs = {} plots = [] # to keep safe from garbage collection for a, b in (('big', 'small'), ('small', 'big')): x = LO_[a].xyz() d, f = LO_[b].diff(x[0], x[1], x[2], rv=True) print('%s.diff(%s)=%g' % (b, a, d)) plots.append(plot(LO_[a], f)) ML.show() return 0
def main(argv=None): import argparse import numpy as np import time import resource if argv is None: # Usual case argv = sys.argv[1:] parser = argparse.ArgumentParser( description="""Initialize LO_step instance, calculate eigenfuction and store in archive directory""" ) parser.add_argument("--u", type=float, default=(48.0), help="log fractional deviation") parser.add_argument("--dy", type=float, default=0.2, help="y_1-y_0 = log(x_1/x_0)") parser.add_argument("--n_g", type=int, default=400, help="number of integration elements in value") parser.add_argument( "--n_h", type=int, default=400, help="number of integration elements in slope. Require n_h > 192 u/(dy^2)." ) parser.add_argument("--max_iter", type=int, default=2000, help="Maximum number power iterations") parser.add_argument("--tol", type=float, default=(1.0e-6), help="Stopping criterion for power") parser.add_argument( "--out_file", type=str, default=None, help="Name of result file. Default derived from other args." ) parser.add_argument("--out_dir", type=str, default="archive", help="Directory for result") args = parser.parse_args(argv) if args.out_file == None: args.out_file = "%d_g_%d_h_%d_y" % (args.n_g, args.n_h, int(args.dy / 1e-5)) d_g = 2 * args.u / args.n_g h_lim = np.sqrt(48 * args.u) d_h = 2 * h_lim / args.n_h t_start = time.time() op = LO(args.u, args.dy, d_g, d_h) op.power(small=args.tol, n_iter=args.max_iter, verbose=True) t_stop = time.time() more = { "eigenvalue": float(op.eigenvalue), "iterations": op.iterations, "time": (t_stop - t_start), "memory": resource.getrusage(resource.RUSAGE_SELF).ru_maxrss, } op.archive(args.out_file, more, args.out_dir)
def main(argv=None): import argparse if argv is None: # Usual case argv = sys.argv[1:] parser = argparse.ArgumentParser( description='''Plot differences between estimates of the Perron Frobenius function of an integral operator''') parser.add_argument('--u', type=float, default=(2.0e-5), help='log fractional deviation') parser.add_argument('--dy', type=float, default=3.2e-4, help='y_1-y_0 = log(x_1/x_0)') parser.add_argument('--n_g0', type=int, default=200, help='number of integration elements in value') parser.add_argument('--n_h0', type=int, default=200, help= 'number of integration elements in slope. Require n_h > 192 u/(dy^2).') parser.add_argument('--n_g1', type=int, default=225) parser.add_argument('--n_h1', type=int, default=225) args = parser.parse_args(argv) d_g = {'big':2*args.u/args.n_g0, 'small':2*args.u/args.n_g1} h_lim = np.sqrt(48*args.u) d_h = {'big':2*h_lim/args.n_h0, 'small':2*h_lim/args.n_h1} tol = 5e-6 maxiter = 150 LO_ = {} for size in ('small', 'big'): op = LO( args.u, args.dy, d_g[size], d_h[size]) op.power(small=tol, n_iter=maxiter) print('For %s, n_g=%d and n_h=%d\n'%(size, op.n_g, op.n_h)) LO_[size] = op diffs = {} plots = [] # to keep safe from garbage collection for a,b in (('big','small'),('small','big')): x = LO_[a].xyz() d, f = LO_[b].diff(x[0],x[1],x[2],rv=True) print('%s.diff(%s)=%g'%(b, a, d)) plots.append(plot(LO_[a], f)) ML.show() return 0
def main(argv=None): '''For looking at sensitivity of time and results to u, dy, n_g, n_h. ''' import argparse global DEBUG if argv is None: # Usual case argv = sys.argv[1:] parser = argparse.ArgumentParser( description='Plot eigenfunction of the integral equation') parser.add_argument('--u', type=float, default=(2.0e-5), help='log fractional deviation') parser.add_argument('--dy', type=float, default=3.2e-4, help='y_1-y_0 = log(x_1/x_0)') parser.add_argument('--n_g', type=int, default=200, help='number of integration elements in value') parser.add_argument( '--n_h', type=int, default=200, help= 'number of integration elements in slope. Require n_h > 192 u/(dy^2).' ) parser.add_argument('--m_g', type=int, default=50, help='number of points for plot in g direction') parser.add_argument('--m_h', type=int, default=50, help='number of points for plot in h direction') parser.add_argument('--eigenvalue', action='store_true') parser.add_argument('--debug', action='store_true') parser.add_argument('--eigenfunction', type=str, default=None, help="Calculate eigenfunction and write to this file") parser.add_argument( '--Av', type=str, default=None, help="Illustrate A applied to some points and write to this file") args = parser.parse_args(argv) if args.Av == None and args.eigenfunction == None and not args.eigenvalue: print('Nothing to do') return 0 params = { 'axes.labelsize': 18, # Plotting parameters for latex 'text.fontsize': 15, 'legend.fontsize': 15, 'text.usetex': True, 'font.family': 'serif', 'font.serif': 'Computer Modern Roman', 'xtick.labelsize': 15, 'ytick.labelsize': 15 } mpl.rcParams.update(params) if args.debug: DEBUG = True mpl.rcParams['text.usetex'] = False else: mpl.use('PDF') import matplotlib.pyplot as plt # must be after mpl.use from scipy.sparse.linalg import LinearOperator from mpl_toolkits.mplot3d import Axes3D # Mysteriously necessary #for "projection='3d'". from matplotlib import cm from matplotlib.ticker import LinearLocator, FormatStrFormatter from first_c import LO_step #from first import LO_step # Factor of 9 slower g_step = 2 * args.u / (args.n_g - 1) h_max = (24 * args.u)**.5 h_step = 2 * h_max / (args.n_h - 1) A = LO_step(args.u, args.dy, g_step, h_step) if args.eigenvalue: from scipy.sparse.linalg import eigs as sparse_eigs from numpy.linalg import norm tol = 1e-10 w, b = sparse_eigs(A, tol=tol, k=10) print('Eigenvalues from scipy.sparse.linalg.eigs:') for val in w: print('norm:%e, %s' % (norm(val), val)) A.power(small=tol) print('From A.power() %e' % (A.eigenvalue, )) if args.eigenfunction != None: # Calculate and plot the eigenfunction tol = 5e-6 maxiter = 1000 A.power(small=tol, n_iter=maxiter, verbose=True) floor = 1e-20 * max(A.eigenvector).max() fig = plt.figure(figsize=(24, 12)) ax1 = fig.add_subplot(1, 2, 1, projection='3d', elev=15, azim=135) ax2 = fig.add_subplot(1, 2, 2, projection='3d', elev=15, azim=45) plot_eig(A, floor, ax1, args) plot_eig(A, floor, ax2, args) if not DEBUG: fig.savefig(open(args.eigenfunction, 'wb'), format='pdf') if DEBUG: plt.show() return 0
def main(argv=None): '''For looking at sensitivity of time and results to u, dy, n_g, n_h. ''' import argparse global DEBUG if argv is None: # Usual case argv = sys.argv[1:] parser = argparse.ArgumentParser( description='Plot eigenfunction of the integral equation') parser.add_argument('--u', type=float, default=(2.0e-5), help='log fractional deviation') parser.add_argument('--dy', type=float, default=3.2e-4, help='y_1-y_0 = log(x_1/x_0)') parser.add_argument('--n_g', type=int, default=200, help='number of integration elements in value') parser.add_argument('--n_h', type=int, default=200, help='number of integration elements in slope. Require n_h > 192 u/(dy^2).') parser.add_argument('--m_g', type=int, default=50, help='number of points for plot in g direction') parser.add_argument('--m_h', type=int, default=50, help='number of points for plot in h direction') parser.add_argument('--eigenvalue', action='store_true') parser.add_argument('--debug', action='store_true') parser.add_argument('--eigenfunction', type=str, default=None, help="Calculate eigenfunction and write to this file") parser.add_argument('--Av', type=str, default=None, help="Illustrate A applied to some points and write to this file") args = parser.parse_args(argv) if args.Av == None and args.eigenfunction == None and not args.eigenvalue: print('Nothing to do') return 0 params = {'axes.labelsize': 18, # Plotting parameters for latex 'text.fontsize': 15, 'legend.fontsize': 15, 'text.usetex': True, 'font.family':'serif', 'font.serif':'Computer Modern Roman', 'xtick.labelsize': 15, 'ytick.labelsize': 15} mpl.rcParams.update(params) if args.debug: DEBUG = True mpl.rcParams['text.usetex'] = False else: mpl.use('PDF') import matplotlib.pyplot as plt # must be after mpl.use from scipy.sparse.linalg import LinearOperator from mpl_toolkits.mplot3d import Axes3D # Mysteriously necessary #for "projection='3d'". from matplotlib import cm from matplotlib.ticker import LinearLocator, FormatStrFormatter from first_c import LO_step #from first import LO_step # Factor of 9 slower g_step = 2*args.u/(args.n_g-1) h_max = (24*args.u)**.5 h_step = 2*h_max/(args.n_h-1) A = LO_step( args.u, args.dy, g_step, h_step) if args.eigenvalue: from scipy.sparse.linalg import eigs as sparse_eigs from numpy.linalg import norm tol = 1e-10 w,b = sparse_eigs(A, tol=tol, k=10) print('Eigenvalues from scipy.sparse.linalg.eigs:') for val in w: print('norm:%e, %s'%(norm(val), val)) A.power(small=tol) print('From A.power() %e'%(A.eigenvalue,)) if args.eigenfunction != None: # Calculate and plot the eigenfunction tol = 5e-6 maxiter = 1000 A.power(small=tol, n_iter=maxiter, verbose=True) floor = 1e-20*max(A.eigenvector).max() fig = plt.figure(figsize=(24,12)) ax1 = fig.add_subplot(1,2,1, projection='3d', elev=15, azim=135) ax2 = fig.add_subplot(1,2,2, projection='3d', elev=15, azim=45) plot_eig(A, floor, ax1, args) plot_eig(A, floor, ax2, args) if not DEBUG: fig.savefig( open(args.eigenfunction, 'wb'), format='pdf') if DEBUG: plt.show() return 0