Demonstration of basic functionalities¶
Load and Run a Model¶
This is the Hello MOOSE program. It shows how to get MOOSE to do its most basic operations: to load, run, and graph a model defined in an external model definition file.
The loadModel function is the core of this example. It can accept a range of file and model types, including kkit, cspace and GENESIS .p files. It autodetects the file type and loads in the simulation.
The wildcardFind function finds all objects matching the specified path, in this case Table objects hoding the simulation results. They were all defined in the model file.
Start, Stop, and setting clocks¶
This demo shows how to start, stop, and continue a simulation. This is commonly done when we want to run a model till settling, then change a parameter or deliver a stimulus, and then continue the simulation.
Here, the model is just the output of a PulseGen object which generates periodic pulses. The demo shows how to start the simulation. using the moose.reinit command to reset the model to its initial state, and moose.start command to run the model for the specified duration. We issue multiple moose.start commands and do different things to the model between them. First, we change the delay of the pulseGen. Then we show a number of ways to assign the timestep (dt) to the table object in the simulation. Note that throughout this simulation the pulsegen is going at a uniform rate, it is just being sampled by the output table at different intervals.
Example of StimulusTable using Poisson random numbers.
Creates a StimulusTable and assigns it signal representing events in a Poisson process. The output of the StimTable is sent to a DiffAmp object for buffering and then recorded in a regular table.
Example of StimulusTable using Poisson random numbers. Creates a StimulusTable and assigns it signal representing events in a Poisson process. The output of the StimTable is sent to a DiffAmp object for buffering and then recorded in a regular table.
Run Python from MOOSE¶
The PyRun class can take a double input through trigger field. Whenever another object sends an input to this field, the runString is executed.
The fun part of this is that you can use the input value in your python statements in runString. This is stored in a local variable called input_. You can rename this by setting inputVar field.
Things become even more interesting when you can send out a value computed using Python. PyRun objects allow you to define a local variable called output and whatever value you assign to this, will be sent out through the source field output on successful execution of the runString.
You can rename the output variable by setting outputVar field.
In this example, we send the output of a pulsegen object sending out the values 1, 2, 3 during each pulse and compute the square of these numbers in Python and set output to this square.
The calculated value is assigned to the output variable and in turn sent out to a Table object's input and gets recorded.
By default PyRun executes the runString whenever a trigger message is received and when its process method is called at each timestep. In both cases it sends out the output value. Since this may cause inaccuracies depending on what the Python statements in runString do, a mode can be specified to disable one of the above. We set
mode = 2to disable the process method. Note that this could also have been done by setting its
tick = -1.
mode = 1will disable trigger message and
mode = 0, the default, enables both.
You can use the PyRun class to run Python statements from MOOSE at runtime. This opens up many possibilities of interleaving computing in Python and MOOSE. You can also use this for debugging simulations.
In this example we demonstrate the use of PyRun objects to execute Python statements from MOOSE. Here is a couple of fun things to indicate the power of MOOSE-Python integration.
First we create a PyRun object called Hello. In its initString we put in Python statements that prints the element's string representation using pymoose-API. When
moose.reinit()is called, this causes MOOSE to execute these Python statements which include Python calling a MOOSE function (Python->MOOSE->Python->MOOSE) - isn't that cool!
We also initialize a counter called hello_count to 0.
The statements in initString gets executed once, when we call
In the runString we put a couple of print statements to indicate the name of the object which is running and the current count. Then we increase the count directly.
When we call
moose.start(), the runString gets executed at each time step.
The other PyRun object we create, is /World. In its initString apart from ordinary print statements and initialization, we define a Python function called
incr_count. This silly little function just increments the global world_count by 1.
The runString for World simply calls this function to increment the count and print it.
We may notice that we assign tick 0 to Hello and tick 1 to World. Looking at the output, you will realize that the sequences of the ticks strictly maintain the sequence of execution.