I'm trying to adapt Nasser's method for using a control variable inside Manipulate to selectively choose what part of the code should be recalculated depending on the controls changed. I've also read his previous notes but the last version seems much more simple and something I can follow.
I need this functionality for a simulation I'm working on about the field lines of an electric dipole.
In the simulation it should be possible to move the two charges q1 and q2 generating the field and also to move a third point P. The point P only serves to see the field line passing through P.
Since the single field line originating from P should have different graphical parameters than the field lines of the "background field" I created two different Streamplots: "fieldlines" is for the general field and "fieldpoint" is for the single line through P. They are combined together with the "Show" command.
So my problem is similar to the one addressed in the first link. If I move the point P I don't want the Streamplot fieldlines of the electric field lines recalculated, but just the Streamplot fieldpoint (dealing with the single line through P). On the contrary both Streamplots should be recalculated when the two charges generating the field are moved or changed.
Up to now, following Nasser's model I've come up with the following "example" code (my intended simulation is much more complicated):
Manipulate[tick;
Show[f1, f2],
Grid[{
{"q1pos",
Slider2D[
Dynamic[q1pos, {q1pos = #; f1 = fieldlines[q1pos, q2pos];
f2 = fieldpoint[q1pos, q2pos, pt];
tick = Not[tick]} &], {{-6, -6}, {6, 6}}], Dynamic[q1pos]},
{"q2pos",
Slider2D[
Dynamic[q2pos, {q2pos = #; f1 = fieldlines[q1pos, q2pos];
f2 = fieldpoint[q1pos, q2pos, pt];
tick = Not[tick]} &], {{-6, -6}, {6, 6}}],
Dynamic[q2pos]}, {"pt",
Slider2D[Dynamic[pt, {pt = #; f2 = fieldpoint[q1pos, q2pos, pt];
tick = Not[tick]} &], {{-6, -6}, {6, 6}}], Dynamic[pt]}
}],
{{tick, False}, None},
{{q1pos, {-2, 0}}, None},
{{q2pos, {2, 0}}, None},
{{pt, {-2.5, 2}}, None},
{{f1, fieldlines[{-2, 0}, {2, 0}]}, None},
{{f2, fieldpoint[{-2, 0}, {2, 0}, {-2.5, 2}]}, None},
TrackedSymbols :> {tick},
Initialization :> (
{
field[x_Real, y_Real] := Module[{},
(*heavy computation goes here*)
{(2 (x - q1pos[[1]]))/
EuclideanDistance[q1pos, {x, y}]^3 + (-2 (x - q2pos[[1]]))/
EuclideanDistance[q2pos, {x, y}]^3, (2 (y - q1pos[[2]]))/
EuclideanDistance[q1pos, {x, y}]^3 + (-2 (y - q2pos[[2]]))/
EuclideanDistance[q2pos, {x, y}]^3}];
fieldlines[q1pos_, q2pos_] := Module[{},
(*heavy computation goes here*)
StreamPlot[field[x, y], {x, -5, 5}, {y, -5, 5},
StreamPoints -> pnts,
StreamScale -> 1/20, ImageSize -> 600]];
fieldpoint[q1pos_, q2pos_, pt_] := Module[{},
(*heavy computation goes here*)
StreamPlot[field[x, y], {x, -5, 5}, {y, -5, 5},
StreamPoints -> {{{pt, {Thickness[0.005], RGBColor[1, 0, 0],
Arrowheads[0.02]}}}, Automatic, {ForwardBackward, 400}},
ImageSize -> 600]];
pnts = Tuples[{-3, -2, -1, 0, 1, 2, 3}, 2]
}
)
]
But there are problems with this code:
- The first run is very slow; the second run is ok (there must be some problem with the inizializaton).
- When I move the point P I can see that the Streamplot of the "background" field lines is not recalculated. That's fine and that's what I wanted. Anyway the single field line loses some of the specifications (arrows). The same thing happens when one of the charges q1 or q2 are moved.
- I'd like to use three Locator instead of three Slider2D for q1, q2 and P in my simulation, but I haven't found yet if that's possible and how to do that.
Any help?