| Title: | APSIM Next Generation |
|---|---|
| Description: | The Agricultural Production Systems sIMulator ('APSIM') is a widely used to simulate the agricultural systems for multiple crops. This package is designed to create, modify and run 'apsimx' files in the 'APSIM' Next Generation <https://www.apsim.info/>. |
| Authors: | Bangyou Zheng [aut, cre] |
| Maintainer: | Bangyou Zheng <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.5.1 |
| Built: | 2026-05-13 06:40:12 UTC |
| Source: | https://github.com/byzheng/rapsimng |
append a model into apsimx
append_model(l, path, model)append_model(l, path, model)
l |
the list of apsimx file |
path |
If numeric, the path returned by search_path or search_node. If character, the path supported by apsimx |
model |
A new model which should be a list of new models |
The modified list with new value
wheat <- read_apsimx(system.file("extdata/wheat.apsimx", package = "rapsimng")) replacements <- new_model("Core.Replacements") wheat_new <- insert_model(wheat, 1, replacements) replacements_node <- search_path(wheat_new, ".Simulations.Replacements") replacements_node$path # Add a cultivar folder under replacements cultivar_folder <- new_model("PMF.CultivarFolder", "Cultivars") wheat_new <- insert_model(wheat_new, replacements_node$path, cultivar_folder) cultivar_folder_node <- search_path(wheat_new, ".Simulations.Replacements.Cultivars") cultivar_folder_node$path # Add an new cultivar cultivar <- new_model("PMF.Cultivar", "Hartog") wheat_new <- insert_model(wheat_new, cultivar_folder_node$path, cultivar) cultivar_node <- search_path(wheat_new, ".Simulations.Replacements.Cultivars.Hartog") cultivar_node$path # Append another cultivar cultivar2 <- new_model("PMF.Cultivar", "Axe") wheat_new <- append_model(wheat_new, cultivar_node$path, list(cultivar2)) cultivar2_node <- search_path(wheat_new, ".Simulations.Replacements.Cultivars.Axe") cultivar2_node$pathwheat <- read_apsimx(system.file("extdata/wheat.apsimx", package = "rapsimng")) replacements <- new_model("Core.Replacements") wheat_new <- insert_model(wheat, 1, replacements) replacements_node <- search_path(wheat_new, ".Simulations.Replacements") replacements_node$path # Add a cultivar folder under replacements cultivar_folder <- new_model("PMF.CultivarFolder", "Cultivars") wheat_new <- insert_model(wheat_new, replacements_node$path, cultivar_folder) cultivar_folder_node <- search_path(wheat_new, ".Simulations.Replacements.Cultivars") cultivar_folder_node$path # Add an new cultivar cultivar <- new_model("PMF.Cultivar", "Hartog") wheat_new <- insert_model(wheat_new, cultivar_folder_node$path, cultivar) cultivar_node <- search_path(wheat_new, ".Simulations.Replacements.Cultivars.Hartog") cultivar_node$path # Append another cultivar cultivar2 <- new_model("PMF.Cultivar", "Axe") wheat_new <- append_model(wheat_new, cultivar_node$path, list(cultivar2)) cultivar2_node <- search_path(wheat_new, ".Simulations.Replacements.Cultivars.Axe") cultivar2_node$path
Disable models in apsimx
disable_models(l, paths)disable_models(l, paths)
l |
the list of apsimx file |
paths |
If numeric, the path returned by search_path or search_node. If character, the path supported by apsimx |
The modified list with new value
wheat <- read_apsimx(system.file("extdata/Wheat.json", package = "rapsimng")) a <- disable_models(wheat, '[Wheat].Phenology.ThermalTime')wheat <- read_apsimx(system.file("extdata/Wheat.json", package = "rapsimng")) a <- disable_models(wheat, '[Wheat].Phenology.ThermalTime')
Helper and wrapper functions to calculate frost and heat damage from temperature and growth stage.
default_frost_heat_params() stage_sens_frost(growth_stage, params) stage_sens_heat(growth_stage, params) temp_damage_frost(mint, params) temp_damage_heat(maxt, params) daily_damage_frost(mint, growth_stage, params) daily_damage_heat(maxt, growth_stage, params) daily_damage_frost_heat(mint, maxt, growth_stage, params) cum_damage_frost(mint, growth_stage, params) cum_damage_heat(maxt, growth_stage, params) cum_damage_frost_heat(mint, maxt, growth_stage, params) calc_daily_damage_frost_heat( mint, maxt, growth_stage, crop = c("Wheat", "Canola"), params = NULL ) calc_cum_damage_frost_heat( mint, maxt, growth_stage, crop = c("Wheat", "Canola"), params = NULL )default_frost_heat_params() stage_sens_frost(growth_stage, params) stage_sens_heat(growth_stage, params) temp_damage_frost(mint, params) temp_damage_heat(maxt, params) daily_damage_frost(mint, growth_stage, params) daily_damage_heat(maxt, growth_stage, params) daily_damage_frost_heat(mint, maxt, growth_stage, params) cum_damage_frost(mint, growth_stage, params) cum_damage_heat(maxt, growth_stage, params) cum_damage_frost_heat(mint, maxt, growth_stage, params) calc_daily_damage_frost_heat( mint, maxt, growth_stage, crop = c("Wheat", "Canola"), params = NULL ) calc_cum_damage_frost_heat( mint, maxt, growth_stage, crop = c("Wheat", "Canola"), params = NULL )
growth_stage |
Numeric vector of daily growth stage. |
params |
A crop parameter list. If provided, it will override the |
mint |
Numeric vector of daily minimum temperature. |
maxt |
Numeric vector of daily maximum temperature. |
crop |
Crop name to use from |
The damage functions are based on the APSIM NG Model FrostHeatDamageFunctions. See full documentation here.
A named list with crop-specific parameter lists.
A numeric vector of frost stage sensitivity (0-1).
A numeric vector of heat stage sensitivity (0-1).
A numeric vector of frost damage contribution from temperature.
A numeric vector of heat damage contribution from temperature.
A numeric vector of daily frost damage.
A numeric vector of daily heat damage.
A numeric vector of combined daily frost and heat damage.
A scalar cumulative frost damage.
A scalar cumulative heat damage.
A scalar cumulative frost and heat damage.
A tibble with daily stage sensitivity, temperature damage, and daily frost/heat/combined damage.
A tibble with cumulative frost damage, cumulative heat damage, and cumulative combined frost-heat damage.
default_frost_heat_params() calc_daily_damage_frost_heat( mint = c(-2, 0, 4), maxt = c(29, 33, 36), growth_stage = c(7.0, 8.0, 9.0), crop = "Wheat" ) calc_cum_damage_frost_heat( mint = c(-2, 0, 4), maxt = c(29, 33, 36), growth_stage = c(7.0, 8.0, 9.0), crop = "Wheat" )default_frost_heat_params() calc_daily_damage_frost_heat( mint = c(-2, 0, 4), maxt = c(29, 33, 36), growth_stage = c(7.0, 8.0, 9.0), crop = "Wheat" ) calc_cum_damage_frost_heat( mint = c(-2, 0, 4), maxt = c(29, 33, 36), growth_stage = c(7.0, 8.0, 9.0), crop = "Wheat" )
Get all cultivar parameters in a model
get_cultivar(l, alias = TRUE)get_cultivar(l, alias = TRUE)
l |
The list of apsimx file |
alias |
Whether to export alias |
A data frame for all cultivar parameters
wheat <- read_apsimx(system.file("extdata/Wheat.json", package = "rapsimng")) get_cultivar(wheat) get_cultivar(wheat, alias = FALSE)wheat <- read_apsimx(system.file("extdata/Wheat.json", package = "rapsimng")) get_cultivar(wheat) get_cultivar(wheat, alias = FALSE)
Get an experiment node by name
get_experiment(apsimx, experiment)get_experiment(apsimx, experiment)
apsimx |
A parsed APSIMX model from |
experiment |
A single experiment name to locate. |
An apsimxNode object for the experiment.
wheat <- read_apsimx(system.file("extdata/wheat.apsimx", package = "rapsimng")) exp <- search_path(wheat, path = "[Experiment]") get_experiment(wheat, exp$node$Name)wheat <- read_apsimx(system.file("extdata/wheat.apsimx", package = "rapsimng")) exp <- search_path(wheat, path = "[Experiment]") get_experiment(wheat, exp$node$Name)
Get the met file name for an experiment
get_metfile(l, is_stop = TRUE)get_metfile(l, is_stop = TRUE)
l |
A list or apsimxNode red by read_apsimx |
is_stop |
Whether stop the function when error |
The met file name in a experiment
wheat <- read_apsimx(system.file("extdata/wheat.apsimx", package = "rapsimng")) exp <- search_path(wheat, path = "[Experiment]") get_metfile(exp)wheat <- read_apsimx(system.file("extdata/wheat.apsimx", package = "rapsimng")) exp <- search_path(wheat, path = "[Experiment]") get_metfile(exp)
Get the parent node from a path
get_parent(l, path)get_parent(l, path)
l |
the list of apsimx file |
path |
If numeric, the path returned by search_path or search_node. If character, the path supported by apsimx |
A new list for parent
wheat <- read_apsimx(system.file("extdata/Wheat.json", package = "rapsimng")) a <- search_path(wheat, '[Structure].BranchingRate') get_parent(wheat, a$path)wheat <- read_apsimx(system.file("extdata/Wheat.json", package = "rapsimng")) a <- search_path(wheat, '[Structure].BranchingRate') get_parent(wheat, a$path)
This function calculates the Plant Available Water Capacity (PAWC) from soil properties in an APSIM Next Generation (.apsimx) file. PAWC represents the amount of water that can be stored in the soil and is available for plant uptake.
get_pawc(l, soil_path = NULL, crop = NULL, depth = NULL)get_pawc(l, soil_path = NULL, crop = NULL, depth = NULL)
l |
The list of apsimx file |
soil_path |
The path to the soil model. If NULL (default), searches for the first Soil model. Can be a character string specifying the soil name. |
crop |
The crop name for crop-specific lower limit (LL). If NULL (default), uses LL15 and does not apply XF. If provided, uses the crop-specific LL and XF values from the SoilCrop parameters. |
depth |
The depth (in mm) to which PAWC should be calculated. If NULL (default), calculates for all layers. If specified, applies proportional weighting to layers that are partially or completely beyond this depth. |
PAWC represents the soil's water storage capacity between field capacity (DUL) and the permanent wilting point (LL). It is a critical parameter for:
PAWC is calculated for each soil layer using the formula:
When crop = NULL and depth = NULL:
When crop is specified:
When depth is specified (with or without crop):
Where:
DUL (Drained Upper Limit): The volumetric water content (mm/mm) after the soil has drained under gravity, typically 2-3 days after saturation
LL (Lower Limit): The volumetric water content (mm/mm) below which plants cannot extract water. This can be either:
LL15: Generic lower limit at 15 bar suction (used when crop = NULL)
Crop-specific LL: Lower limit specific to a crop type, accounting for different rooting characteristics
Thickness: Layer thickness in millimeters
XF: Exploration factor (0-1), representing the fraction of a layer explored by roots. Only applied when crop is specified; represents the proportion of the layer volume that roots can explore
Weight: Depth weighting factor (0-1), only applied when depth is specified:
1.0 for layers completely within the specified depth
Proportional fraction for layers partially within the depth
0.0 for layers completely beyond the specified depth
The total PAWC is the sum across all layers:
A numeric value of PAWC in mm
wheat <- read_apsimx(system.file("extdata/wheat.apsimx", package = "rapsimng")) # Get total PAWC using LL15 (generic) pawc <- get_pawc(wheat) pawc # Get PAWC for wheat crop (uses crop-specific LL and XF) pawc_wheat <- get_pawc(wheat, crop = "Wheat") pawc_wheat # Get PAWC to 1200 mm depth pawc_1200 <- get_pawc(wheat, depth = 1200) pawc_1200 # Get wheat PAWC to 1500 mm depth pawc_wheat_1500 <- get_pawc(wheat, crop = "Wheat", depth = 1500) pawc_wheat_1500wheat <- read_apsimx(system.file("extdata/wheat.apsimx", package = "rapsimng")) # Get total PAWC using LL15 (generic) pawc <- get_pawc(wheat) pawc # Get PAWC for wheat crop (uses crop-specific LL and XF) pawc_wheat <- get_pawc(wheat, crop = "Wheat") pawc_wheat # Get PAWC to 1200 mm depth pawc_1200 <- get_pawc(wheat, depth = 1200) pawc_1200 # Get wheat PAWC to 1500 mm depth pawc_wheat_1500 <- get_pawc(wheat, crop = "Wheat", depth = 1500) pawc_wheat_1500
Get simulations for a factorial experiment
get_simulations(l)get_simulations(l)
l |
A list from read_apsim with Factorial.Permutation as root. |
A list with Factor as name and Levels as values
wheat <- read_apsimx(system.file("extdata/wheat.apsimx", package = "rapsimng")) permutation <- search_path(wheat, path = "[Factors].Permutation") get_simulations(permutation$node)wheat <- read_apsimx(system.file("extdata/wheat.apsimx", package = "rapsimng")) permutation <- search_path(wheat, path = "[Factors].Permutation") get_simulations(permutation$node)
Insert a model into apsimx
insert_model(l, path, model)insert_model(l, path, model)
l |
the list of apsimx file |
path |
If numeric, the path returned by search_path or search_node. If character, the path supported by apsimx |
model |
A new model |
The modified list with new value
wheat <- read_apsimx(system.file("extdata/wheat.apsimx", package = "rapsimng")) replacements <- new_model("Core.Replacements") wheat_new <- insert_model(wheat, 1, replacements) replacements_node <- search_path(wheat_new, ".Simulations.Replacements") replacements_node$path # Add a cultivar folder under replacements cultivar_folder <- new_model("PMF.CultivarFolder", "Cultivars") wheat_new <- insert_model(wheat_new, replacements_node$path, cultivar_folder) cultivar_folder_node <- search_path(wheat_new, ".Simulations.Replacements.Cultivars") cultivar_folder_node$path # Add an new cultivar cultivar <- new_model("PMF.Cultivar", "Hartog") wheat_new <- insert_model(wheat_new, cultivar_folder_node$path, cultivar) cultivar_node <- search_path(wheat_new, ".Simulations.Replacements.Cultivars.Hartog") cultivar_node$pathwheat <- read_apsimx(system.file("extdata/wheat.apsimx", package = "rapsimng")) replacements <- new_model("Core.Replacements") wheat_new <- insert_model(wheat, 1, replacements) replacements_node <- search_path(wheat_new, ".Simulations.Replacements") replacements_node$path # Add a cultivar folder under replacements cultivar_folder <- new_model("PMF.CultivarFolder", "Cultivars") wheat_new <- insert_model(wheat_new, replacements_node$path, cultivar_folder) cultivar_folder_node <- search_path(wheat_new, ".Simulations.Replacements.Cultivars") cultivar_folder_node$path # Add an new cultivar cultivar <- new_model("PMF.Cultivar", "Hartog") wheat_new <- insert_model(wheat_new, cultivar_folder_node$path, cultivar) cultivar_node <- search_path(wheat_new, ".Simulations.Replacements.Cultivars.Hartog") cultivar_node$path
Insert models into apsimx
insert_models(l, path, models)insert_models(l, path, models)
l |
the list of apsimx file |
path |
If numeric, the path returned by search_path or search_node. If character, the path supported by apsimx |
models |
New models |
The modified list with new value
wheat <- read_apsimx(system.file("extdata/wheat.apsimx", package = "rapsimng")) replacements <- new_model("Core.Replacements") wheat_new <- insert_model(wheat, 1, replacements) replacements_node <- search_path(wheat_new, ".Simulations.Replacements") replacements_node$path # Add a cultivar folder under replacements cultivar_folder <- new_model("PMF.CultivarFolder", "Cultivars") wheat_new <- insert_model(wheat_new, replacements_node$path, cultivar_folder) cultivar_folder_node <- search_path(wheat_new, ".Simulations.Replacements.Cultivars") cultivar_folder_node$path # Add an new cultivar cultivar <- new_model("PMF.Cultivar", "Hartog") wheat_new <- insert_model(wheat_new, cultivar_folder_node$path, cultivar) cultivar_node <- search_path(wheat_new, ".Simulations.Replacements.Cultivars.Hartog") cultivar_node$pathwheat <- read_apsimx(system.file("extdata/wheat.apsimx", package = "rapsimng")) replacements <- new_model("Core.Replacements") wheat_new <- insert_model(wheat, 1, replacements) replacements_node <- search_path(wheat_new, ".Simulations.Replacements") replacements_node$path # Add a cultivar folder under replacements cultivar_folder <- new_model("PMF.CultivarFolder", "Cultivars") wheat_new <- insert_model(wheat_new, replacements_node$path, cultivar_folder) cultivar_folder_node <- search_path(wheat_new, ".Simulations.Replacements.Cultivars") cultivar_folder_node$path # Add an new cultivar cultivar <- new_model("PMF.Cultivar", "Hartog") wheat_new <- insert_model(wheat_new, cultivar_folder_node$path, cultivar) cultivar_node <- search_path(wheat_new, ".Simulations.Replacements.Cultivars.Hartog") cultivar_node$path
This function installs the ApsimNG simulation software on the local system. It handles the download and installation process for the APSIM Next Generation agricultural modeling platform.
install_apsimng( repo = "https://github.com/APSIMInitiative/ApsimX.git", branch = "master", install_dir = "apsimx_build", overwrite = FALSE )install_apsimng( repo = "https://github.com/APSIMInitiative/ApsimX.git", branch = "master", install_dir = "apsimx_build", overwrite = FALSE )
repo |
Character string specifying the repository of ApsimNG to install. |
branch |
Character string specifying the branch of the repository to install. |
install_dir |
Character string specifying the directory where ApsimNG should be installed. |
overwrite |
Logical. If TRUE, overwrites any existing installation at the specified install directory. |
This function downloads and installs ApsimNG from the official repository. It performs version checking, handles dependencies, and configures the installation for use with R. The function requires administrative privileges on Windows systems.
No return value.
## Not run: # Install latest version install_apsimng() install_apsimng(install_dir = "C:/MyPrograms/ApsimNG") ## End(Not run)## Not run: # Install latest version install_apsimng() install_apsimng(install_dir = "C:/MyPrograms/ApsimNG") ## End(Not run)
Keep simulations for a factorial experiment
keep_simulations(l, s)keep_simulations(l, s)
l |
A list from read_apsim with Factorial.Permutation as root. |
s |
a list with factor as name and levels as value to keep. The factor is kept if it is not specified. |
A new list with removed simulations.
wheat <- read_apsimx(system.file("extdata/wheat.apsimx", package = "rapsimng")) permutation <- search_path(wheat, path = "[Factors].Permutation") permutation_new <- keep_simulations(permutation$node, list(V = "2")) get_simulations(permutation_new) permutation_new <- keep_simulations(permutation$node, list(Cv = c("Axe", "Bolac"))) get_simulations(permutation_new) permutation_new <- keep_simulations(permutation$node, list(V = "1", Cv = c("Axe", "Bolac"))) get_simulations(permutation_new)wheat <- read_apsimx(system.file("extdata/wheat.apsimx", package = "rapsimng")) permutation <- search_path(wheat, path = "[Factors].Permutation") permutation_new <- keep_simulations(permutation$node, list(V = "2")) get_simulations(permutation_new) permutation_new <- keep_simulations(permutation$node, list(Cv = c("Axe", "Bolac"))) get_simulations(permutation_new) permutation_new <- keep_simulations(permutation$node, list(V = "1", Cv = c("Axe", "Bolac"))) get_simulations(permutation_new)
List all reports in the database
list_report(file)list_report(file)
file |
the file path to apsimx or db file |
a vector of all reports
## Not run: file <- system.file("extdata/wheat.apsimx", package = "rapsimng") list_report(file) ## End(Not run)## Not run: file <- system.file("extdata/wheat.apsimx", package = "rapsimng") list_report(file) ## End(Not run)
Set the log level of apsimx file
log_level(l, level = c("Error", "Warning", "Information", "Diagnostic", "All"))log_level(l, level = c("Error", "Warning", "Information", "Diagnostic", "All"))
l |
the list of apsimx file |
level |
log level with option Error, Warning, Information, Diagnostic, All |
a new apsimx file
Create the minimum requirements to run an APSIM Next Generation
minimum_apsimng(install_path, output)minimum_apsimng(install_path, output)
install_path |
The installed path of APSIM Next Generation |
output |
The output folder |
## Not run: minimum_apsimng("C:/ProgramFiles/APSIMNG", "minimum_apsimng") ## End(Not run)## Not run: minimum_apsimng("C:/ProgramFiles/APSIMNG", "minimum_apsimng") ## End(Not run)
Generate new cultivars with parameter which can be used in Replacements
new_cultivar(df, cultivar_folder = "Cultivars")new_cultivar(df, cultivar_folder = "Cultivars")
df |
A data frame for new parameters with three columns, i.e. name, parameter and value. |
cultivar_folder |
folder name for cultivars |
An APSIMX list
df <- data.frame(name = c("C1", "C1", "C2", "C2"), parameter = c("[Phenology].CAMP.FLNparams.MinLN", "[Phenology].CAMP.FLNparams.VrnLN", "[Phenology].CAMP.FLNparams.MinLN", "[Phenology].CAMP.FLNparams.VrnLN"), value = c(5, 6, 7, 8)) a <- new_cultivar(df)df <- data.frame(name = c("C1", "C1", "C2", "C2"), parameter = c("[Phenology].CAMP.FLNparams.MinLN", "[Phenology].CAMP.FLNparams.VrnLN", "[Phenology].CAMP.FLNparams.MinLN", "[Phenology].CAMP.FLNparams.VrnLN"), value = c(5, 6, 7, 8)) a <- new_cultivar(df)
Create a new model
new_model(model, name = model)new_model(model, name = model)
model |
The name of new model |
name |
The new name |
new_model(model = "PMF.Cultivar") new_model(model = "PMF.Cultivar", name = "example")new_model(model = "PMF.Cultivar") new_model(model = "PMF.Cultivar", name = "example")
Read APSIMX file
read_apsimx(path)read_apsimx(path)
path |
The file path or URL to apsimx file |
A list object of apsimx file
file <- system.file("extdata/wheat.apsimx", package = "rapsimng") m <- read_apsimx(file)file <- system.file("extdata/wheat.apsimx", package = "rapsimng") m <- read_apsimx(file)
Read apsimx database in db file format
read_report(file, report)read_report(file, report)
file |
the file path to apsimx or db file |
report |
the report name |
a data.frame for a report
## Not run: file <- system.file("extdata/wheat.apsimx", package = "rapsimng") read_report(file, "HarvestReport") ## End(Not run)## Not run: file <- system.file("extdata/wheat.apsimx", package = "rapsimng") read_report(file, "HarvestReport") ## End(Not run)
Remove a model with new values
remove_model(l, path)remove_model(l, path)
l |
the list of apsimx file |
path |
If numeric, the path returned by search_path or search_node. If character, the path supported by apsimx |
The modified list with new value
wheat <- read_apsimx(system.file("extdata/Wheat.json", package = "rapsimng")) a <- search_path(wheat, '[Wheat].Phenology.ThermalTime') wheat_new <- remove_model(wheat, a$path) b <- search_path(wheat_new, '[Wheat].Phenology.ThermalTime') bwheat <- read_apsimx(system.file("extdata/Wheat.json", package = "rapsimng")) a <- search_path(wheat, '[Wheat].Phenology.ThermalTime') wheat_new <- remove_model(wheat, a$path) b <- search_path(wheat_new, '[Wheat].Phenology.ThermalTime') b
Replace a model with new values
replace_model(l, path, model)replace_model(l, path, model)
l |
the list of apsimx file |
path |
If numeric, the path returned by search_path or search_node. If character, the path supported by apsimx |
model |
A new model |
The modified list with new value
wheat <- read_apsimx(system.file("extdata/Wheat.json", package = "rapsimng")) a <- search_path(wheat, '[Wheat].Phenology.ThermalTime') a$node$Children[[1]]$X[[2]] <- 27 wheat_new <- replace_model(wheat, a$path, a$node) b <- search_path(wheat_new, '[Wheat].Phenology.ThermalTime') b$node$Children[[1]]$Xwheat <- read_apsimx(system.file("extdata/Wheat.json", package = "rapsimng")) a <- search_path(wheat, '[Wheat].Phenology.ThermalTime') a$node$Children[[1]]$X[[2]] <- 27 wheat_new <- replace_model(wheat, a$path, a$node) b <- search_path(wheat_new, '[Wheat].Phenology.ThermalTime') b$node$Children[[1]]$X
Run apsimx file using Models.exe
run_models( path, models_exe = NULL, pattern = NULL, recurse = FALSE, csv = FALSE, parallel = NULL, ncpus = NULL, verbose = FALSE )run_models( path, models_exe = NULL, pattern = NULL, recurse = FALSE, csv = FALSE, parallel = NULL, ncpus = NULL, verbose = FALSE )
path |
The path to an .apsimx file. May include wildcard. |
models_exe |
path to Models, the executable for running apsimx simulations. If NULL, the function will look for "Models.exe" on Windows or "Models" on Linux in the system PATH. |
pattern |
Use to filter simulation names to run. |
recurse |
Recursively search subdirectories for files matching ApsimXFileSpec. FALSE in default. |
csv |
Export all reports to .csv files. FALSE in default. |
parallel |
Use the multi-process job runner. If FALSE, use single threaded; if TRUE, use the multi-process job runner |
ncpus |
Set the number of processors to use. All processes in default |
verbose |
Write messages to StdOut when a simulation starts/finishes. Only has an effect when running a directory of .apsimx files (*.apsimx). |
Find element(s) in apsimx file
search_node(l, all = FALSE, max_depth = 1e+06, case_insensitive = TRUE, ...)search_node(l, all = FALSE, max_depth = 1e+06, case_insensitive = TRUE, ...)
l |
The list of apsimx file |
all |
Whether to find all elements |
max_depth |
The maximum depth to search |
case_insensitive |
Whether case sensitive |
... |
Other names arguments for property to match |
A list matching all criteria if all equals to TRUE, A list with node and path if all equals to FALSE (default)
wheat <- read_apsimx(system.file("extdata/Wheat.json", package = "rapsimng")) # Return empty list if not found search_node(wheat, Name = "Simulations1") # Find root level a <- search_node(wheat, Name = "Simulations") a$path # Find sub-level a <- search_node(wheat, Name = "Wheat") a$path a <- search_node(wheat, `$type` = "Models.PMF.Cultivar, Models") a$path # Find multiple attributes a <- search_node(wheat, Name = 'PotentialBranchingRate', `$type` = "Models.Functions.PhaseLookup, Models") a$path a$node$Name # Find all cultivar nodes a <- search_node(wheat, `$type` = "Models.PMF.Cultivar, Models", all = TRUE) length(a)wheat <- read_apsimx(system.file("extdata/Wheat.json", package = "rapsimng")) # Return empty list if not found search_node(wheat, Name = "Simulations1") # Find root level a <- search_node(wheat, Name = "Simulations") a$path # Find sub-level a <- search_node(wheat, Name = "Wheat") a$path a <- search_node(wheat, `$type` = "Models.PMF.Cultivar, Models") a$path # Find multiple attributes a <- search_node(wheat, Name = 'PotentialBranchingRate', `$type` = "Models.Functions.PhaseLookup, Models") a$path a$node$Name # Find all cultivar nodes a <- search_node(wheat, `$type` = "Models.PMF.Cultivar, Models", all = TRUE) length(a)
Find a model in the apsimx file using specified path
search_path(l, path, case_insensitive = TRUE)search_path(l, path, case_insensitive = TRUE)
l |
the list of apsimx file |
path |
The specified path (See details) |
case_insensitive |
Whether case sensitive |
The list for the specified path.
Absolute paths have a leading ‘.’ e.g.
.Simulations.Test.Clock - absolute path - refers to the clock model in the 'Test' simulation.
Scoped paths have a leading model type in square brackets. A model of the specified name, in scope, is located before applying the rest of the path.
[Soil].Water - scoped path - refers to the Water model that is a child of a model that has the name 'Soil' that is in scope
wheat <- read_apsimx(system.file("extdata/Wheat.json", package = "rapsimng")) # Return empty list if not found search_path(wheat, "[Simulations1]") # Search root path a <- search_path(wheat, '.Simulations') a$path a$node$Name # Level one a <- search_path(wheat, '.Simulations.Wheat1') a$path a$node$Name # Level two a <- search_path(wheat, '.Simulations.Wheat') a$path a$node$Name # Level three a <- search_path(wheat, '.Simulations.Wheat.BranchingRate') a$path a$node$Name a <- search_path(wheat, '.Simulations.Wheat.Structure') a$path a$node$Name # Level four a <- search_path(wheat, '.Simulations.Wheat.Structure.BranchingRate') a$path a$node$Name a <- search_path(wheat, '.Simulations.Wheat.Structure.BranchingRate1') a$path a$node$Name # scoped # Root path a <- search_path(wheat, '[Simulations1]') a <- search_path(wheat, '[Simulations]') a$path a$node$Name # Level two a <- search_path(wheat, '[Simulations].Wheat1') a <- search_path(wheat, '[Simulations1].Wheat') a$path a$node$Name a <- search_path(wheat, '[Whea]') a <- search_path(wheat, '[Wheat]') a$path a$node$Name # Level three a <- search_path(wheat, '[Wheat].BranchingRate') a <- search_path(wheat, '[Wheat].Structure') a$path a$node$Name a <- search_path(wheat, '[Structure]') a$path a$node$Name # Level four a <- search_path(wheat, '[Structure].BranchingRate') a$path a$node$Name a <- search_path(wheat, '[Structure].BranchingRate1') a <- search_path(wheat, '[Structure1].BranchingRate')wheat <- read_apsimx(system.file("extdata/Wheat.json", package = "rapsimng")) # Return empty list if not found search_path(wheat, "[Simulations1]") # Search root path a <- search_path(wheat, '.Simulations') a$path a$node$Name # Level one a <- search_path(wheat, '.Simulations.Wheat1') a$path a$node$Name # Level two a <- search_path(wheat, '.Simulations.Wheat') a$path a$node$Name # Level three a <- search_path(wheat, '.Simulations.Wheat.BranchingRate') a$path a$node$Name a <- search_path(wheat, '.Simulations.Wheat.Structure') a$path a$node$Name # Level four a <- search_path(wheat, '.Simulations.Wheat.Structure.BranchingRate') a$path a$node$Name a <- search_path(wheat, '.Simulations.Wheat.Structure.BranchingRate1') a$path a$node$Name # scoped # Root path a <- search_path(wheat, '[Simulations1]') a <- search_path(wheat, '[Simulations]') a$path a$node$Name # Level two a <- search_path(wheat, '[Simulations].Wheat1') a <- search_path(wheat, '[Simulations1].Wheat') a$path a$node$Name a <- search_path(wheat, '[Whea]') a <- search_path(wheat, '[Wheat]') a$path a$node$Name # Level three a <- search_path(wheat, '[Wheat].BranchingRate') a <- search_path(wheat, '[Wheat].Structure') a$path a$node$Name a <- search_path(wheat, '[Structure]') a$path a$node$Name # Level four a <- search_path(wheat, '[Structure].BranchingRate') a$path a$node$Name a <- search_path(wheat, '[Structure].BranchingRate1') a <- search_path(wheat, '[Structure1].BranchingRate')
Set a parameter with a new value
set_parameter_value(l, parameter, value)set_parameter_value(l, parameter, value)
l |
the list of apsimx file |
parameter |
the name of parameter with APSIM NG specification |
value |
the new value |
A list with replaced value
wheat <- read_apsimx(system.file("extdata/Wheat.json", package = "rapsimng")) new_wheat <- set_parameter_value(wheat, "[Structure].BranchingRate.PotentialBranchingRate.Reproductive.Zero.FixedValue", 1) new_wheat2 <- search_path(new_wheat, "[Structure].BranchingRate.PotentialBranchingRate.Reproductive.Zero") new_wheat2$node$FixedValue new_wheat <- set_parameter_value( wheat, "[Structure].HeightModel.WaterStress.XYPairs.Y", "0.1,1.1") new_wheat2 <- search_path(new_wheat, "[Structure].HeightModel.WaterStress.XYPairs") new_wheat2$node$Ywheat <- read_apsimx(system.file("extdata/Wheat.json", package = "rapsimng")) new_wheat <- set_parameter_value(wheat, "[Structure].BranchingRate.PotentialBranchingRate.Reproductive.Zero.FixedValue", 1) new_wheat2 <- search_path(new_wheat, "[Structure].BranchingRate.PotentialBranchingRate.Reproductive.Zero") new_wheat2$node$FixedValue new_wheat <- set_parameter_value( wheat, "[Structure].HeightModel.WaterStress.XYPairs.Y", "0.1,1.1") new_wheat2 <- search_path(new_wheat, "[Structure].HeightModel.WaterStress.XYPairs") new_wheat2$node$Y
This function takes an APSIMX file and splits it into separate simulations for all factors except cultivar factors. Then saving the results to the specified output location.
split_apsimx(file, output)split_apsimx(file, output)
file |
Character. Path to the input APSIMX file. |
output |
Character. Path to the directory where the split components will be saved. |
No return value. The function is called for its side effects.
## Not run: split_apsimx("path/to/input.apsimx", "path/to/output/directory") ## End(Not run)## Not run: split_apsimx("path/to/input.apsimx", "path/to/output/directory") ## End(Not run)
Test whether all files under published folder of apsimx are required
test_apsimx(base, example)test_apsimx(base, example)
base |
the base folder path to apsimx publish |
example |
an example apsimx file |
A vector a required files
This function assumes the file is apsimx format. A new Replacements node is added if it is not exist. The existing cultivar parameters are updated. New cultivar is created.
update_cultivar( l, df, add = TRUE, use_folder = TRUE, cultivar_folder = "Cultivars" )update_cultivar( l, df, add = TRUE, use_folder = TRUE, cultivar_folder = "Cultivars" )
l |
The list of apsimx file |
df |
A data frame for new parameters with three columns, i.e. name, parameter and value. |
add |
Whether to add extra nodes (e.g. replacements, Cultivars folder and new cultivar) |
use_folder |
use cultivar folder to add new cultivars |
cultivar_folder |
folder name for cultivars |
The modified apsimx file
wheat <- read_apsimx(system.file("extdata/wheat.apsimx", package = "rapsimng")) # Update cultivars df <- data.frame(name = rep("Hartog", 3), parameter = c("[Phenology].MinimumLeafNumber.FixedValue", "[Phenology].VrnSensitivity.FixedValue", "[Phenology].PpSensitivity.FixedValue"), value = c(9, 7, 3)) wheat_cultivar <- update_cultivar(wheat, df) hartog <- search_path(wheat_cultivar, "[Replacements].Hartog") hartog$pathwheat <- read_apsimx(system.file("extdata/wheat.apsimx", package = "rapsimng")) # Update cultivars df <- data.frame(name = rep("Hartog", 3), parameter = c("[Phenology].MinimumLeafNumber.FixedValue", "[Phenology].VrnSensitivity.FixedValue", "[Phenology].PpSensitivity.FixedValue"), value = c(9, 7, 3)) wheat_cultivar <- update_cultivar(wheat, df) hartog <- search_path(wheat_cultivar, "[Replacements].Hartog") hartog$path
The with_apsimx function automates the process of setting up and running APSIM Next Generation (NG) simulations
in a temporary or specified folder. This approach helps reduce I/O overhead, particularly in high-performance
computing (HPC) environments with network file systems (NFS). The frequent file I/O operations performed by APSIM NG
(e.g., writing to SQLite databases) can strain file systems, and redirecting simulations to a local folder mitigates this issue.
The function can also perform optional post-processing of the simulation results.
with_apsimx( models, file, mets = NULL, target = tempdir(), clean = c("none", "simulations", "mets", "all"), post_process = NULL, ... )with_apsimx( models, file, mets = NULL, target = tempdir(), clean = c("none", "simulations", "mets", "all"), post_process = NULL, ... )
models |
A character string specifying the path to the APSIM NG executable ( |
file |
A character string specifying the path to the |
mets |
A character vector specifying paths to meteorological data ( |
target |
A character string specifying the target directory where simulations will be run.
Defaults to the R system temporary directory ( |
clean |
A character string specifying which files or directories to clean after the simulation. Options are:
|
post_process |
An optional function for post-processing simulation results.
The function must accept a |
... |
Additional arguments passed to both the |
If a post_process function is provided, its return value is returned. Otherwise, the function returns NULL.
run_models: Runs APSIM simulations.
do.call: Dynamically calls functions with named arguments.
## Not run: # Run simulations without post-processing with_apsimx( models = "path/to/apsimx", file = "path/to/input.apsimx", mets = c("path/to/met1.met", "path/to/met2.met"), clean = "all" ) # Run simulations with post-processing post_process_function <- function(folder) { output_files <- list.files(folder, full.names = TRUE) message("Output files:", paste(output_files, collapse = "\n")) } result <- with_apsimx( models = "path/to/apsimx", file = "path/to/input.apsimx", mets = c("path/to/met1.met", "path/to/met2.met"), post_process = post_process_function ) ## End(Not run)## Not run: # Run simulations without post-processing with_apsimx( models = "path/to/apsimx", file = "path/to/input.apsimx", mets = c("path/to/met1.met", "path/to/met2.met"), clean = "all" ) # Run simulations with post-processing post_process_function <- function(folder) { output_files <- list.files(folder, full.names = TRUE) message("Output files:", paste(output_files, collapse = "\n")) } result <- with_apsimx( models = "path/to/apsimx", file = "path/to/input.apsimx", mets = c("path/to/met1.met", "path/to/met2.met"), post_process = post_process_function ) ## End(Not run)
Write APSIMX file
write_apsimx(l, file)write_apsimx(l, file)
l |
the list of apsimx file |
file |
The file path to apsimx file |
A list object of apsimx file