# Category: Differential Equations
# Name: Vibrating circular drumhead (wave equation)
#
# Make an animation of the various modes of a vibrating circular drumhead.
# The equation is u_{tt} = \nabla^2 u in polar coordinates and the
# drum is of radius 1.  The varius modes n,m are of the form
# BesselJn(n,k*r)*cos(n*theta)*sin(k*t)
# where k is the mth zero of the BesselJn(n,x)
# The superposition is a superposition of 3 different modes,
# with some shifts also applied in the theta direction for
# added complexity

the_answer = AskButtons("Which mode?  n-m where n is the Bessel J_n to use",
                        "0-1", # 1
                        "0-2", # 2
                        "0-3", # 3
                        "1-1", # 4
                        "1-2", # 5
                        "1-3", # 6
                        "2-1", # 7
                        "2-2", # 8
                        "2-3", # 9
                        "Superposition" # 10
                        );

# the zeros of the Bessel functions
knm = [2.4048, 5.5201, 8.6537
       3.8317, 7.0156, 10.1735
       5.1356, 8.4172, 11.6198];

node_lines = null;

function node_circle(r) = r*[cos(0:2*pi/ceil(r*40):2*pi);sin(0:2*pi/ceil(r*40):2*pi);0]';

if the_answer == 1 then (
  n = 0;
  k = knm@(n+1,1);
) else if the_answer == 2 then (
  n = 0;
  k = knm@(n+1,2);
  node_lines@(1) = node_circle(knm@(n+1,1)/k);
) else if the_answer == 3 then (
  n = 0;
  k = knm@(n+1,3);
  node_lines@(1) = node_circle(knm@(n+1,1)/k);
  node_lines@(2) = node_circle(knm@(n+1,2)/k);
) else if the_answer == 4 then (
  n = 1;
  k = knm@(n+1,1);
  node_lines@(1) = [0,-1,0;0,1,0];
) else if the_answer == 5 then (
  n = 1;
  k = knm@(n+1,2);
  node_lines@(1) = [0,-1,0;0,1,0];
  node_lines@(2) = node_circle(knm@(n+1,1)/k);
) else if the_answer == 6 then (
  n = 1;
  k = knm@(n+1,3);
  node_lines@(1) = [0,-1,0;0,1,0];
  node_lines@(2) = node_circle(knm@(n+1,1)/k);
  node_lines@(3) = node_circle(knm@(n+1,2)/k);
) else if the_answer == 7 then (
  n = 2;
  k = knm@(n+1,1);
  node_lines@(1) = (1/sqrt(2))*[-1,-1,0;1,1,0];
  node_lines@(2) = (1/sqrt(2))*[1,-1,0;-1,1,0];
) else if the_answer == 8 then (
  n = 2;
  k = knm@(n+1,2);
  node_lines@(1) = (1/sqrt(2))*[-1,-1,0;1,1,0];
  node_lines@(2) = (1/sqrt(2))*[1,-1,0;-1,1,0];
  node_lines@(3) = node_circle(knm@(n+1,1)/k);
) else if the_answer == 9 then (
  n = 2;
  k = knm@(n+1,3);
  node_lines@(1) = (1/sqrt(2))*[-1,-1,0;1,1,0];
  node_lines@(2) = (1/sqrt(2))*[1,-1,0;-1,1,0];
  node_lines@(3) = node_circle(knm@(n+1,1)/k);
  node_lines@(4) = node_circle(knm@(n+1,2)/k);
) else (
  # Superposition
  n = -1; # signals superposition

  # Two put together
  coeff1 = 0.5;
  shift1 = 0.0;
  n1 = 1;
  k1 = knm@(n1+1,2);

  coeff2 = 0.3;
  shift2 = 0.5;
  n2 = 0;
  k2 = knm@(n2+1,3);

  coeff3 = 0.3;
  shift3 = 2;
  n3 = 2;
  k3 = knm@(n3+1,1);
);

if not IsNull(node_lines) and 
   1 == AskButtons("Draw node lines over the graph?",
                        "yes", "no") then (
  draw_node_lines = true
) else (
  draw_node_lines = false
);

SurfacePlotDrawLegends = false; # don't draw the legend
PlotWindowPresent(); # Make sure the window is raised

# use standard variable names (in case they got reset)
SurfacePlotVariableNames = ["x","y","z"];

#One mode
if n >= 0 then (
  for t=0.0 to 30.0 by 0.03 do (
    data = null;
    for r=0 to 1.0 by 1.0/10 do (
      for theta=0 to 2*pi by pi/16 do (
        x = r*cos(theta);
        y = r*sin(theta);
        data = [data;[x,y,BesselJn(n,k*r)*cos(n*theta)*sin(k*t)]]
      )
    );

    # Plot the data
    PlotCanvasFreeze();
    SurfacePlotData(data,[-1,1,-1,1,-1,1]);
    if draw_node_lines then (
      for nline in node_lines do (
        SurfacePlotDrawLine(nline,"color","blue","thickness",3)
      )
    );
    PlotCanvasThaw();
  )
) else (
  for t=0.0 to 30.0 by 0.03 do (
    data = null;
    for r=0 to 1.0 by 1.0/10 do (
      for theta=0 to 2*pi by pi/16 do (
        x = r*cos(theta);
        y = r*sin(theta);
        val = coeff1*BesselJn(n1,k1*r)*cos(n1*theta+shift1)*sin(k1*t) +
              coeff2*BesselJn(n2,k2*r)*cos(n2*theta+shift2)*sin(k2*t) +
              coeff3*BesselJn(n3,k3*r)*cos(n3*theta+shift3)*sin(k3*t);
        data = [data;[x,y,val]]
      )
    );

    # Plot the data
    SurfacePlotData(data,[-1,1,-1,1,-1,1])
  )
);
