Back to Documentation/Tutorial

Wrapping a groovy model

This tutorial is the continuation of the first step on design of experiments. You can either do this first step or load this file.

Goal: In this tutorial step, we explore a simple differential equation model writing in groovy language. The model compute predator / prey dynamics, according to parameters. Let r, K, C be model parameters. Let n and p be respectively the initial cardinality of the prey group and the initial number of predators.

We explore, for r, K, C fixed values, the dynamics of the populations according to their initial cardinalities.

Defining Model inputs

  • Open the "inputStructure" frame.
  • Add input by right-clicking on "inputStructure" entry and choosing "Add a child...". It opens the "New data structure node" dialog box.
  • Type the name of the input in the "Name" field for example "n"
  • Choose "Double" in the "Data type" list.
  • Click "OK" button.
  • Using the same scenario, add the "p" input.

Input could be accessed in all the "Exploration_loop" using the variable "input" (input.n, input.p).

Linking factors and inputs

Factor values could be accessed using "factors" object.

  • Add a processor by right clicking the "Input_generation" node and clicking the "Add a processor" entry.

It opens a dialog box to choose which type of processor you want.

  • Pick the "GroovyProcessor" in the dialog box list.
  • Validate by clicking "OK" button, it closes the dialog box.
  • In the application frame, click the just created GroovyProcessor in order to edit the groovy code in the "Editor" frame.
  • Copy and Paste the following code in order to link factors and inputs.
/* Linking factors and inputs */
input.n = factors.a + 0.1
input.p = factors.b + 0.1

Remark that at this step factors could be manipulated to be adapted to the model properties.

Defining and linking Model outputs

  • Open the "outputStructure" frame.
  • Add output by right-clicking on "outputStructure" entry and choosing "Add a child...".

It open the "New data structure node" dialog box.

  • Type the name of the output in the "Name" field for example "trajectory".
  • Choose "Complex" in the "Data type" list.
  • Check "sequence data" box to create an array of complex node;

The "outputStructure.trajectory" object will be used to store each state of the dynamic, step by step.

  • Aggregate outputStructure by editing "output" in "Variable" frame:
    • Right click on "output" variable
    • Check the "Gathered after the exploration loop" box
    • Validate by clicking "OK" button
  • Change the groovy code of the "Final_processing" as following:
import org.simexplorer.graph.Grapher
g = Grapher.getInstance()
g.title = "Predator Prey Dynamic"
g.addSeries(output.trajectory)

Declaring the model

  • Deploy "Exploration_loop" node by clicking its collapsed triangle.
  • Deploy "Model_launcher" node by clicking its collapsed triangle, it is empty.
  • Add a processor by right clicking the "Model_launcher" node and clicking the "Add a processor" entry.

It opens a dialog box to choose wich type of processor do you want.

  • Pick the "GroovyProcessor" in the dialog box list.
  • Validate by clicking "OK" button, it closes the dialogue box.
  • In the application frame, click the just created GroovyProcessor in order to edit the groovy code in the "Editor" frame.
  • Copy and Paste the following code in order to declare the groovy model.
/* Declare Model */
class Model {

    double a = 1,  nu = 1,  alpha = 1,  dt = 0.01;
    double r,  K,  C;

    Model(double r, double K, double C) {
        this.r = r;
        this.K = K;
        this.C = C;
    }

    double dn(double n, double p) {
        return (r * n * (1 - n / K) - a * n * p) * dt;
    }   

    double dp(double n, double p) {
        if (n < (C / a)) {
            return (-nu * p + alpha * a / 2 * n * p - alpha * a * a / (2 * C) * n * n * p) * dt;
        } else {
            return (-nu * p + alpha * a / 2 * n * p - alpha * C / 2 * p) * dt;
        }
    }

    HashMap getNextPosition(double n, double p) {
        def pos = [:]
        pos.n =  (n + dn(n,p))
        pos.p =  (p + dp(n,p))
        return pos
    }
}

/* Init Model */
def r = 20
def K = 30
def C = 20
def m = new Model(r, K, C)

/* Init trajectory */
def ni = input.n
def pi = input.p
def traj = []
output.trajectory = traj
traj << [n:ni,p:pi]

/* Compute trajectory */
def tmax = 500
//println "n\tp"
//println "" + ni + "\t" + pi
while(traj.size() < tmax){
  traj << m.getNextPosition(traj[-1].n, traj[-1].p)
  //println "" + traj[-1].n + "\t" + traj[-1].p
}

This groovy code :

  • declare the Model
  • init the Model
  • init a specific trajectory
  • compute trajectory.

Your second SimExplorer exploration application is ready to be ran, it computes trajectories associated top each sceanrii and display its on a 2D chart.

  • Run the application by clicking on the run button.

You should see such a picture:

Retrieve the final content of the application using this file.

logo cemagref

logo iscpif

logo lifegrid

logo region auvergne

logo patres project