I have written the following DynamicModule
. The idea is that you can change the boundary, calculate the eigenfunctions and then animate them. Here is the code:
DynamicModule[{pts, bdr = {{0, 0}, {1, 0}, {1, 1}, {0, 1}}, vals,
funs, n = 2, ar = False},
pts = Table[{6, 6} + 3 {Cos[θ], Sin[θ]}, {θ, 0,
2 π - 2 π/10, 2 π/10}];
bdr = BoundaryDiscretizeGraphics[Line[Join[pts, {pts[[1]]}]]];
{vals, funs} =
NDEigensystem[{-Laplacian[u[x, y], {x, y}]},
u[x, y], {x, y} ∈ bdr, 6];
ar = False;
Column[{
Row[{
Button["Calculate",
bdr = BoundaryDiscretizeGraphics[Line[Join[pts, {pts[[1]]}]]];
{vals, funs} =
NDEigensystem[{-Laplacian[u[x, y], {x, y}]},
u[x, y], {x, y} ∈ bdr, 6]
, ImageSize -> 1 72],
Spacer[18],
"Mode Number = ",
Slider[Dynamic[n], {2, 6, 1}, Appearance -> "Labeled"],
Spacer[18],
Button["Toggle Animate", If[ar, ar = False, ar = True],
ImageSize -> 2 72]
}],
LocatorPane[Dynamic[pts],
Dynamic@Animate[Show[
Graphics[{Line[Join[pts, {pts[[1]]}]]}, Frame -> True,
PlotRange -> {{0, 12}, {0, 12}}, ImageSize -> 10 72],
ContourPlot[funs[[n]] Cos[t], {x, y} ∈ bdr,
Axes -> None, Frame -> None, AspectRatio -> Automatic,
ColorFunction ->
Function[f, {Opacity[0.75], ColorData["TemperatureMap"][f]}]]
],
{t, 0, 2 π}, AnimationRunning -> ar]
]
}]
]
This is mostly working. The problem is you cannot use the animation slider If you set up a boundary, calculate and then try and use it you get this
The locator has jumped to the animation slider controls. As a workaround I added the Toggle Animate button. This seems to work sometimes but not always. I am not clear what is wrong. How can I animate when requested.
The version is 11.2.0 for Microsoft Windows (64-bit) (September 11, 2017)
Thanks for any help.
Edit
A minor point raised by Kuba. I am simulating an acoustic cavity. For this case the potential function is equivalent to the acoustic pressure while the gradient is equivalent to the acoustic particle velocity. Thus for the boundary conditions I have no Dirichlet boundary condition and use the fact that Mathematica assumes zero Neumann boundary conditions to make the acoustic velocity zero at the boundaries. To have, for example, a membrane simulation add the Dirichlet condition.
DynamicModule
instead. There, you would have to wrap dymanic variables withDynamic
. The advantage is thatDynamic
has a second, optional argument that lets you control when and how a change of the dynamic variable takes effect in the body ofDynamicModule
. $\endgroup$DynamicModule
. Are you suggesting aDynamicModule
within aDynamicModule
? I am familiar with using the second argument of Dynamic so I am not sure what you are suggesting. $\endgroup$DynamicModule
instead ofAnimate
. But you know, these dynamic programming things cause headaches to me... Anyways, Kuba seems to have it solved for you. $\endgroup$