Learn R Programming

simplace

R package to interact with the modeling framework Simplace

Introduction

This package provides methods to interact with the modelling framework Simplace - Scientific Impact assessment and Modelling PLatform for Advanced Crop and Ecosystem management. See www.simplace.net for more information on Simplace. Simplace is written in Java (and some parts in Scala) so one can access it from R via rJava. The purpose of this package is to simplify the interaction between R and Simplace, by providing functions to:

  • initialize and configure Simplace
  • load a simulation (solution and project)
  • parameterize the simulation
  • run whole simulation or run it stepwise
  • get simulation output and convert it to formats suitable for R

Installing the Simplace Framework

For installing Simplace, please consult the webpage www.simplace.net.

A brief guide to install Simplace:

  • If you don't have installed Java, please install an appropriate version of the (JRE or JDK) from openjdk.org or adoptium.net (recommended).
  • Get Simplace from www.simplace.net
  • Install the simplace package in R:
install.packages('simplace')

The most recent development version can be installed from github:

devtools::install_github("gk-crop/simplace_rpkg")

If you encounter errors, make sure to install the packages devtools and rJava.

Basic Usage

The usage of Simplace in R follows roughly this scheme:

  • init Simplace by providing the path to your simplace installation directory, your working directory and your outputs
  • open a Simplace project form a solution (and project) file
  • create a list of simulation parameters you want to change
  • create and run a Simulation
  • get the result from the simulation
  • convert the result to a R object (data.frame, list etc.)

Troubleshooting

  • Package rJava should be installed automatically with simplace. If not, install it manually:

install.packages('rJava')

  • Architecture of R and Java have to match. If you are using 64-bit Java, you have to use 64-bit R.
  • If you want to use the development version instead of the console mode, make sure that the projects simplace_core, simplace_modules and optionally simplace_run are in a common directory and set the installation dir to this directory.

Example

Run the simulation

library(simplace)
SimplaceInstallationDir <- findSimplaceInstallations()

Solution <- paste(SimplaceInstallationDir,
        "simplace_run/simulation/gk/solution/complete/Complete.sol.xml",sep="")

simplace <- initSimplace(SimplaceInstallationDir)

openProject(simplace, Solution)

parameter <- list()
parameter$enddate <- "31-12-1992"

sid <- createSimulation(simplace,parameter)
runSimulations(simplace)

result <- getResult(simplace,"DIAGRAM_OUT", sid);

closeProject(simplace)

After specifying the directories and the solution, the framework is initialized and the project opened. The end date of the simulation is (re)set and the simulation is run. After the run the result is retrieved.

Get the result and plot it

resf <- resultToDataframe(result)

dates <- 300:730
weights <- resf[dates,
    c("TOP_LINE_Roots","TOP_LINE_Leaves","TOP_LINE_Stems","TOP_LINE_StorageOrgans")]
matplot(dates,weights,type="l",xlab="Days",ylab="Weight [g/m2]",main="Simulated Biomass")
legend(300,800,legend=c("Roots","Leaves","Stems","Storage Organs"),lty=1:4,col=1:4)

The result is converted to a dataframe. Interesting variables are extracted and then plotted.

Get arrays and plot them as contour plot

resultlistexp <- resultToList(result,expand=TRUE)
water <- resultlistexp$BOTTOM_ARRAY_VolumetricWaterContent
wmat <- do.call(rbind,water)
wmatpart <- wmat[dates,]
layers <- dim(wmatpart)[2]
filled.contour(dates,-(layers:1),wmatpart[,layers:1],
               xlab="Day", ylab="Layer", main="Water content in soil",
               color.palette = function(n){rgb((n:1)/n,(n:1)/n,1)})

As the result contains an array which holds the water content for 40 layers, it is transformed to a list and the array is expanded.

Copy Link

Version

Install

install.packages('simplace')

Monthly Downloads

241

Version

5.1.2

License

GPL-2

Issues

Pull Requests

Stars

Forks

Maintainer

Gunther Krauss

Last Published

October 9th, 2024

Functions in simplace (5.1.2)

initSimplaceDefault

Initialises Simplace with work- and outputdir for different settings
setSimulationValues

Changes values of the current simulation
runProject

Runs the opened project
setSlotCount

Sets number of used CPUs
varmapToList

Converts the varmap to a list
stepSimulation

Run simulation stepwise
simplace

simplace: Interface to use the modelling framework 'SIMPLACE'
stepAllSimulations

Run all simulations in queue stepwise
setProjectLines

Sets the lines of the project data files that should be used when running a project.
setSimplaceDirectories

Set working-, output-, projects- and data-directory
getSimplaceDirectories

Get the directories (work-, output-, projects- and data-dir)
getSimulationIDs

Lists IDs of the performed simulations
initSimplace

Initialisation of Framework
getUnitsOfResult

Get the units of the result variables
setAllSimulationValues

Changes values of the all simulations in queue
runSimulations

Run the created simulations
findFirstSimplaceInstallation

Search for simplace installation and returns first match
resetSimulationQueue

Clears the list of simulations
findSimplaceInstallations

Search for simplace installations and returns results as vector
resultToDataframe

Convert result to dataframe
openProject

Opens a Simplace project
closeProject

Close Project
createSimulation

Creates a simulation and substitute parameters
setCheckLevel

Sets the check level of the framework
getDatatypesOfResult

Get the datatypes of the result variables
getResult

Fetch output from a simulation
resultToList

Convert result to list
setLogLevel

Sets the log level of the framework