Group Abstract Group Abstract

Message Boards Message Boards

Controls in DynamicModule: animation does not start

Posted 3 months ago

I have a problem when using DynamicModule to represent a dynamical system of the type State(t)->F(State(t+1)). In the attached code, the animation does not start. Could any one tell me why? Thanks.

POSTED BY: Christian Mullon
7 Replies

Getting rid of stepOnce[] makes the animation start:

DynamicModule[{t = 0},
 Dynamic[t++, UpdateInterval -> 20]]

However, the UpdateInterval option is not obeyed. I have no idea why all this.

POSTED BY: Gianluca Gorni

A good goal to have is for the DynamicModule to be self-contained, depending only on things in the system that essentially don't change. The point of Initialization is that when the kernel starts up, stepOnce[] is not defined. If the DynamicModule[] is displayed when someone opens the notebook, the Front End will start running the module before the user has a chance to define stepOnce[]. The Front End will run the Initialization before running the body of the module. If stepOnce[] is defined there, you can be sure it will always be defined in any instance of a module. I'd recommend Initialization over the option SaveDefinitions -> True, but you could look that up. It figures out the initialization automatically, but sometimes one is surprised by what it did.

The use of ClearAll[stepOnce] is good practice when making definitions. The thing to remember is that func[...] := value adds a definition rule to the list of definitions for func (DownValues[func]). The addition will replace an old definition if the patterns on the left of the := are equivalent, but any previous definitions that areare not equivalent will remain and could cause trouble. The same considerations apply to clearing attributes, etc.

Personally, I would use the localized version. The definition does not use much memory or take much time. Sometimes different dynamic modules need access to the same global resources. Then the global version might be a helpful example. Bulletproofing this can be more work than it's worth. At the least, you need to make sure that the global resources are defined and available before your modules try to use them.

POSTED BY: Michael Rogers
POSTED BY: Michael Rogers

Here's a fix;

DynamicModule[{t = 0, slider = 0, stepOnce},
 stepOnce[] := (t++;);
 Column[{
   Grid[{{"Slider", 
      Slider[Dynamic[slider], {0, 1}, ContinuousAction -> False], 
      Dynamic@NumberForm[slider, {4, 2}]}}, Alignment -> Left, 
    Spacings -> {2, 1}],
   Dynamic[
    Refresh[stepOnce[];
     Row[{"t = ", t}], UpdateInterval -> 2, TrackedSymbols :> {}]]
   }
  ]
 ]

If I can find the time, I'll explain later. It's complicated.

POSTED BY: Michael Rogers

Thanks for these clear explanation of a subject that appears not clear. A question I have about your last code is: you write that stepOnce is global; however you clear it and redefine it in the Initialization. What would you suggest to define stepOnce outside the DynamicModule?

POSTED BY: Christian Mullon

This is exactly what I need. Thank you. If find the logic of DynamicModule quite unclear.

POSTED BY: Christian Mullon

Thank you Gianluca, your solution works; in the real application, step is a much more complicated module; I believe that the issue comes from the use of a function such as step inside a dynamic module.

POSTED BY: Christian Mullon
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard