Adding observation to simulations and SimulData manipulation
Here, we will show how to use the visualization tool of
. You will first need to add in your project the MECSYCO-visu jar and the dependencies (jfreechart, JCommon, Jzy3d 0.9.1, Gluegen and JOGL).
We will also manipulate the model artifact in order to create special output for the observer.
You can use any time step, but for better results, the default time step is advised.
For detailed information, see User Guide MECSYCO-visu.
Agent, dispatcher and connection
The observer is used as any other agent, but we give it the capacity to get more than one model artifact thanks to a dispatcher. For the first step, you need to create the observing system (agent and dispatcher):
Then you can connect the output port of the Lorenz agents to our observer like we did before for connecting the agents.
Add of special outputs
Some observing artifact need specific SimulData to work. We will now create specific ports where the usual data will be converted to the correct SimulData we need, i.e. a Tuple3 of Double values, a SimulVector of Double values, and a Tuple2 of Double values (cf User Guide section “Simulation data” or SimulData manipulation ).
Take a look to our model artifact (EquationModelArtifact) at the method named getExternalOutputEvent. This is where we manage the outputs of our model in order to create output ports. As you can see, 3 condition were not filled. They are our new outputs port (obs2D, obs3d and obsVector).
We need to create a Tuple2 filled with real values. We will try to catch the X and Y values, to do this we have the method getVariable contains in the model (Equation). We can now create the event that will contains those values under the form of a Tuple2, with a time stamp:
This time we need a Tuple3. This is the same process as before, but we add the Z value.
Lastly, we need a SimulVector, but as the guide said, the constructor is contained in the Java class ArrayedSimulVector. This vector will contain the value of X, Y and Z:
This condition is already filled. It allows the user to use any kind of port’s name, but due to the method getVariable, only X, Y and Z can really work (state variable of the model).
The Z value is not contained in every model (LorenzX do not), so only agents’ link to LorenzY and LorenzZ can use the last two output ports.
You can now use them now to create the links (AgentX to obs with obs2D, AgentZ to obs with obs3d and obsVector for example).
Of course, obs is also an agent so at the end of the main, a startModelSoftware and a start are mandatory.
You can now use all graphics available in MECSYCO-visu! But how?
Try to add code 13 after the new links you have created:
In this code we named the input of the observing agent following this mapping:
- input “X” link to AgentX output
- input “Y” link to AgentY output
- input “Z” link to AgentZ output
- input “XY” link to the agent using output “obs2D”
- input “XYZ” link to the agent using output “obs3d”
- input “XYZVector” link to the agent using output “obsVector”
As you can see, it is not mandatory here to have the same names for input and output, only the same CentralizedEventCouplingArtifact is needed.
Nice graphics you have there, no? (Figure5) In all this case, we used the live version of our graphics as a consequence, the simulation take more time than before. In order to avoid this issue, it is advised to use the postmortem version of the graphs (replace Live by PostMortem).
The visualization package does not only contain visualization tool, but post treatment tool too.
One of them is the data comparator. It will compare the result of the simulation with data sheets. For the Lorenz case, the data sheets with the right format are already provided. You only need to turn on the Debug system, and put it in info mode. To do so, thanks to your project explorer, go to the file mecsyco.properties in the folder conf, and make sure you have the following configuration:
Now, everything is ready so let just try code 15 and see what the console has to say (Figure 6):
The package also contains a logging system, which means that you can save your result in external files. The logging can be done by creating one file per output you need to log, or by creating one for all:
Since no agent have an output port that provide a simple Tuple1 containing the output value, we have to use the alternative option to create one file per output (line 7 to 12). You are free to create the output ports Xobs, Yobs and Zobs yourself and try the commented code (it means that the inputs of the observer are also named Xobs, Yobs and Zobs)! As the result you should have the following file created in data_log:
Analyze with R
The last functionality of MECSYCO-visu is the possibility to launch an R script in order to treat your result. This functionality is not optimal yet:
- When installing R, choose a file with a path without space or special character
- File path (of the file to treat) as to be define in the script
ResultR2.csv is a file created by LogToRProject using the ports given (here X, Y, Z). This file can be use by your Rscript. In this example, we already provide you a script (DemoR.R) that will analyze the file named “result.csv”” in the folder “data_log” and create a file named “ResultR.csv” in the same folder. This Script will give the min and max value of each column, and the mean value. The csv to treat with this script follow the following structure:
- Without header
- The first column is not analyzed, so it could be the name, or the time step
- The others columns contain only real values
- Column separator are “;” and decimal separator are “.”
The results will also be found in “data_log”: