bmad-sim / NonlinearNormalForm.jl

Nonlinear normal form analysis using truncated power series
https://bmad-sim.github.io/NonlinearNormalForm.jl/
GNU General Public License v3.0
1 stars 0 forks source link

`@fast` macro #12

Open mattsignorelli opened 5 months ago

mattsignorelli commented 5 months ago

At the time of this issue, the function gofix looks like

function gofix(xy::DAMap, order=1)
  desc = getdesc(xy)
  nv = numvars(desc)

  # 1: v = map-identity in harmonic planes, identity in spin
  v = zero(xy)
  for i=1:nv
    @inbounds v.x[i] =  xy.x[i] - mono(i,use=desc)
  end
  v.Q.q[1] = 1

  # 2: map is cut to order 2 or above
  cut!(v,v,order+1)

  # 3: map is inverted at least to order 1:
  inv!(v,v)

  # 4: a map x is created with dimension nv
  # x is zero except for the parameters and delta if coasting
  x = zero(v) # default identity in parameters
  x.Q.q[1] = 1 # identity in spin
  compose!(v,v,x)

  # 5: add back in identity
  for i=1:nv
    @inbounds v.x[i] += mono(i,use=desc)
  end

  return v
end

This is pretty fast (could be made faster by proper use of work temporaries accessible by user of inv and compose) , however this is not the easiest to read. gofix could instead be written as

function gofix(xy::DAMap, order=1)
  m = cut(xy, order+1)
  return (m-I)^-1∘zero(m)+I
end

This is much easier to understand and read, however much slower. For the best of both worlds, a macro must be implemented to convert the above easy-to-read function to its faster less-easy-to-read function. The approach could be the same as that taken for @FastGTPSA in GTPSA.jl