def do_fp(h=1e-3,eps=1e-10,debug=False):
  q=[1.,2.,10.,5.,2.,2.0,2*np.pi,0,2.]
  m,M,k,b,l0,a,om,ph,g = q
  debug = False

  hop = Hop()
  p = hop.P(q,debug)

  ph0 = np.pi# - np.pi/(2**2)
  phf = ph0 + 20*2*np.pi
  t0 = 0.; tf = (phf-ph0)/om
  n  = np.infty
  t = (t0,tf)
  j = 2
  z = np.array([2.0,2.0])
  #j = 1
  #z = np.array([0.1,2.0,0.,2.0])
  ph = ph0
  p.j = j
  q = [m,M,k,b,l0,a,om,ph,g]
  p.q = q

  Zeno = False

  import time; 
  
  start = time.clock()
  trjs = flow(hop, p, h, eps, (t0,tf), z, debug=debug)

  #hop.plot(trjs[-10:])
  hop.plot(trjs)
  xlim = plt.xlim(); ylim = plt.ylim()
  plt.xlim(xlim); plt.ylim(ylim)

  print "%0.2f sec for sim & plot" % (time.clock() - start)

  #dbg()

  qq = q[:]
  pp = hop.P(qq); pp.j = trjs[-1].p.j
  zz = trjs[-1].x[-1]
  # Deformation of Poincare section
  psi = (lambda t,x : flow(hop,pp,h,eps,(0.,2*t*np.pi/om),x)[-1].x[-1])
  # Poincare map
  pmap = lambda x : psi(1.,x)
  start = time.clock()
  # find periodic orbit
  z0 = rx.fp(pmap,zz,modes=[1,2])
  trjs0 = flow(hop, p, h, eps, (t0,tf), z0)
  hop.plot(trjs0,clf=False)
  
  print '      z0 = %s' % z0
  # linearize Poincare map
  J = rx.jac(pmap,z0)
  # compute eigenvalues of P-map
  print ' spec J  = %s' % ( np.linalg.eigvals(J) )
  print '|spec J| = %s' % np.abs( np.linalg.eigvals(J) )
  print np.array([(dd,np.abs(np.linalg.eigvals(rx.jac(pmap,z0,d=dd))).max()) for dd in np.logspace(-2,-6,40)])

  print "%0.2f sec for fp & jac " % (time.clock() - start)

  
  z = trjs0[-1].x[-1]
  sig = np.mod(ph0 + om*(tf-t0),2*np.pi) # == phf
  if len(z) == 2:
    y,dy = np.array(z)
    z = np.array([sig,y,dy])
  elif len(z) == 4:
    sig,y,dy,x,dx = np.array(z)
    z = np.array([sig,y,dy,x,dx])
  else:
    dbg()

  return hop,z,p
def do_pmap():
  q=[1.,2.,10.,5.,2.,2.0,2*np.pi,np.pi,2.]
  m,M,k,b,l0,a,om,ph,g = q
  debug = False

  hop = Hop()
  p = hop.P(q,debug)

  ph0 = np.pi/2.
  t0 = ph0*np.pi/om
  tf = (10*2 + ph0)*np.pi/om
  n  = np.infty
  t = (t0,tf)
  j = 1
  z = np.array([0.2,1.75,0.,0.]) # nice transient
  #j = 2
  #z = np.array([1.0,0.0])
  #z = np.array([1.6,0.6])
  #z = np.array([1.4,0.6])
  p.j = j

  h = 1e-2
  eps = 1e-12
  Zeno = False

  import time; 
  
  start = time.clock()
  trjs = flow(hop, p, h, eps, (t0*om,tf*om), z)

  #hop.plot(trjs[-10:])
  hop.plot(trjs,fn='fig/hoptrj.eps')
  xlim = plt.xlim(); ylim = plt.ylim()
  plt.xlim(xlim); plt.ylim(ylim)

  q0 = q[:]; p0 = hop.P(q0,debug); p0.j = 1; z0 = np.array([0.25,1.75,0.,0.])
  hop.sch(3*np.pi/2,[0.25,2.15,0,0],p0,text=True,fn='fig/hopA.eps')
  #hop.sch(3*np.pi/2,[0.25,2.15,0,0],p0,text=False,fn='fig/hopAplain.eps')
  p0.j = 2
  hop.sch(np.pi/4,[1.5,0],p0,text=True,fn='fig/hopG.eps')
  #hop.sch(np.pi/4,[1.5,0],p0,text=False,fn='fig/hopGplain.eps')
  #hop.sch(t0,z,p,text=True,fn='fig/hop.pdf')
  p0.j = 1

  print "%0.2f sec for sim & plot" % (time.clock() - start)

  qq = q[:]
  pp = hop.P(qq); pp.j = trjs[-1].p.j
  zz = trjs[-1].x[-1]
  # Deformation of Poincare section
  psi = (lambda t,x : flow(hop,pp,h,eps,(ph0*np.pi,(2*t+ph0)*np.pi),x)[-1].x[-1])
  # Poincare map
  pmap = lambda x : psi(1.,x)
  start = time.clock()
  # find periodic orbit
  z0 = rx.fp(pmap,zz,modes=[1,2])
  print '      z0 = %s' % z0
  # linearize Poincare map
  J = rx.jac(pmap,z0)
  # compute eigenvalues of P-map
  print ' spec J  = %s' % ( np.linalg.eigvals(J) )
  print '|spec J| = %s' % np.abs( np.linalg.eigvals(J) )
  print 'max |spec J| as a function of perturbation:'
  #print np.array([(dd,np.abs(np.linalg.eigvals(rx.jac(pmap,z0,d=dd))).max()) for dd in np.logspace(-2,-6,10)])

  print "%0.2f sec for fp & jac " % (time.clock() - start)
    x = np.array(x)
    # don't simulate from invalid initial condition
    if p.j == 2 and dp.Lambda(t0,x,p) <= 0:
      return np.nan*x

    trjs = rx.Euler((t0,tf), x, p, dp, h, eps, n)

    if len(trjs[-1].t) <= 2:
      trjs = trjs[:-1]
    trj = trjs[-1]

    s = (tf - trj.t[-2]) / (trj.t[-1]-trj.t[-2])
    trj.t[-1] = (1-s)*trj.t[-2] + s*trj.t[-1]
    trj.x[-1] = (1-s)*trj.x[-2] + s*trj.x[-1]

    return trjs

  # Deformation of Poincare section
  psi = (lambda t,x : flow(dp,p,h,eps,((0.5+dt)*np.pi,(0.5+dt)*np.pi+2*np.pi*t),x)[-1].x[-1])

  # Poincare map
  pmap = lambda x : psi(1.,x)

  # find periodic orbit
  x0 = rx.fp(pmap,x)
  # linearize Poincare map
  J = rx.jac(pmap,x0)
  # compute eigenvalues of P-map
  print np.abs(np.linalg.eigvals(J))