Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,50 @@ In Rstudio, the vignette may not be built per default. You will turn this on in
```{r}
devtools::build_vignettes("rLPJGUESS")
```
### Workflow when using the package:

#### 1. Define a folder to run the simulations, e.g.
```{r}
mainDir <- file.path(getwd(), "LPJrunTest")
```
and place the binary (exectuable) of LPJ-GUESS into this folder (the binary does not come with this package).

#### 2. Provide a valid instruction file that you usually use to define the set-up of LPJ-GUESS to the InferParameterAndDesignList function.
```{r}
defaultparameters <- InferParameterAndDesignList(list(main = paste0(mainDir,"LPJ_instruction_file.ins")),
NameMainFile = "main.ins", NamePftFile = "pft.ins",
vectorvaluedparams = c("rootdist","eps_mon",
"storfrac_mon","photo",
"fertdates","fertrate"))
```

This function extracts the parameters, design and driver-files used to run LPJ-GUESS and splits the instruction into a main file containing inputs and a PFT file, which has all the settings. Because we cannot handle vectorvalued parameters at the moment, they have to be provided to this function as well.

#### 3. Call the adjust template function, which rewrites the templates to new destinations (IMPORTANT: the files need to be in the same directory as the binary, here: LPJrunTest)
```{r}
AdjustTemplates(defaultparameters = defaultparameters$defaultparameters,
defaultlist = defaultparameters$defaultlist,
MainTemplateDestination = "./LPJrunTest/main_new.ins",
PftTemplateDestination = "./LPJrunTest/pft_new.ins",
NameMainFile = "main.ins", NamePftFile = "pft.ins")
```

#### 4. Generate a set of parameters for the species (GetRunAbleParameters), we want to simulate, here Fagus sylvatica (Fag_syl):
```{r}
parameters <- GetRunAbleParameters(defaultparameters = defaultparameters, PFTs = c("Fag_syl"))
```
#### 5. Change some of these parameters and provide it as matrix with a set of parameters per row and parameter names as colnames (here: new parameter matrix is called parameters_new)

#### 6. Define the settings and input files, we want to use as input for the simulations in a list (the required files depend on the LPJ-Version, here: the list is called LPJsettings)

#### 7. Define the setup (i.e. sequential, parallel, which kind of parallelisation, here a SOCK cluster with numCores cores)
```{r}
LPJsetup <- setupLPJParallel(numCores = numCores, clusterType = "SOCK",
mainDir = mainDir)
```

#### 8. Run the simulations with the model
```{r}
results <- runLPJ(x = LPJsetup, parameterList = parameters_new,
typeList = typeList, settings = LPJsettings)
```
106 changes: 106 additions & 0 deletions rLPJGUESS/R/AdjustTemplates.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#' @title This function writes the two main templates in the correct order
#' @description This function takes the defaultparameters and a defaultlist of
#' of files and writes the main and pft instruction file into the defined destinations
#' which are MainTemplateDestination and PftTemplateDestination
#' @param defaultobjects R object produced by \code{\link{InferParameterAndDesignList}}. Its first list contains the data frame with default parameters extracted from the instruction files
#' @param defaultlist a matrix with the design parameters which should be used
#' in rlpjguess runs
#' @param MainTemplateDestination a character string indicating the location to which
#' one should write the main file
#' @param PftTemplateDestination a character string indicating the location to which
#' one should write the pft file
#' @param NameMainFile a character string with the name of the main file
#' which was produced with the default parameters before
#' @param NamePftFile a character string with the name of the pft file
#' which was produced with the default parameters before
#' @return writes the two files which serve as templates
#' @author Johannes Oberpriller
#' @export


AdjustTemplates <- function(defaultobjects,
MainTemplateDestination = NULL,
PftTemplateDestination = NULL,
NameMainFile = NULL,
NamePftFile = NULL){



# check if there is a destination to save the main template

if(is.null(MainTemplateDestination)){
stop("Please provide a destination for the Main template")
}

#check if there is a destination to save the pft/species template

else if(is.null(PftTemplateDestination)){
stop("Please provide a destination for the PFT template")
}

else if(is.null(NameMainFile)){
stop("Please provide a valid main template")
}

else if(is.null(NamePftFile)){
stop("Please provide a valid pft template")
}


## define the thinks used here

defaultparameters = defaultobjects$defaultparameters
defaultlist = defaultobjects$defaultlist

## read in text and adjust the templates


tx <- readLines(NamePftFile)
for(i in 1:nrow(defaultparameters)){
if(defaultparameters[i,"group"] == "run"){
tx <- gsub(paste0(defaultparameters[i,"name"]," "),
paste0(defaultparameters[i,"name"]," ",
defaultparameters[i,"rlpjname"]," !"), x = tx)
}
else{
linenumber_group = grep(paste0("group \"",defaultparameters[i,"group"],"\""),
x = tx, fixed = T)
linenumber_pft = grep(paste0("pft \"",defaultparameters[i,"group"],"\""),
x = tx, fixed = T)
linenumber_st = grep(paste0("st \"",defaultparameters[i,"group"],"\""),
x = tx, fixed = T)
linenumber = c(linenumber_group, linenumber_pft, linenumber_st)
for(j in 1:length(linenumber)){
endofgroup = grep(")", x = substring(tx[(linenumber[j]+1):length(tx)],1,1))[1]
exchange = grep(paste0(as.character(defaultparameters[i,"name"])),
x = tx[(linenumber[j]+1):(linenumber[j]+1+endofgroup+1)], fixed = T)
tx[(linenumber[j])+exchange] <- gsub(paste0(defaultparameters[i,"name"]," "),
paste0(defaultparameters[i,"name"]," ",
defaultparameters[i,"rlpjname"]," ","!"),
x = tx[(linenumber[j])+exchange])
}
}
}



writeLines(tx, con = PftTemplateDestination)


files <- defaultlist
#change the actual file names with the rlpjnames

maintemplate <- readLines(NameMainFile)

for(i in 1:nrow(files)){
maintemplate <- gsub(paste0("\"",files[i,"name"],"\""),
paste0("\"",files[i,"name"],"\"",
" ","(str ", "\"_",files[i,"name"],"_\")","!"),
x = maintemplate)
}

#writes the Template to the given MainTemplateDestination
writeLines(maintemplate, con = MainTemplateDestination)


}
49 changes: 49 additions & 0 deletions rLPJGUESS/R/GetRunAbleParameters.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#' @title A function to separate all default values into parameters, design parameters and input files.
#' @description This function organizes the parameters extracted from the instruction files in order to run the model.
#' @param defaultobjects R object produced by \code{\link{InferParameterAndDesignList}}. Its first list contains the data frame with default parameters extracted from the instruction files
#' @param PFTs the PFTs or species, which should be included into the model run
#' @keywords rLPJGUESS
#' @author Johannes Oberpriller
#' @return list consisting of a data.frame for runable parameters, a data.frame for design parameters and a data.frame for the default input files.
#' @export

GetRunAbleParameters <- function(defaultobjects,PFTs){


LPJparameters_PFT <- matrix(defaultobjects$defaultparameters[,4])
rownames(LPJparameters_PFT) = defaultobjects$defaultparameters[,3]
LPJrunParameters = matrix(LPJparameters_PFT[-which(substr(rownames(LPJparameters_PFT),1,3)== "run"),])
rownames(LPJrunParameters) = rownames(LPJparameters_PFT)[-which(substr(rownames(LPJparameters_PFT),1,3)== "run")]

designLPJ = LPJparameters_PFT[which(substr(rownames(LPJparameters_PFT),1,3)== "run"),]
designLPJ = designLPJ[-which(names(designLPJ) == "run_outputdirectory")]

lpjvalues = as.matrix(defaultobjects$defaultlist[,c(2,3)])

filelist = split(t(lpjvalues),rep(1:nrow(lpjvalues),each = ncol(lpjvalues)))

names(filelist) = gsub("_", ".",defaultobjects$defaultlist[,1])

spp <- as.matrix(LPJrunParameters[grep("_include", rownames(LPJrunParameters )),])
PFTsRows <- c()
for(i in PFTs) PFTsRows <- c(PFTsRows, grep(i, rownames(spp)))
spp[-PFTsRows, 1] <- 0
spp[PFTsRows,1] <- 1
print(spp)

parameterStandard_PFT <- as.matrix(LPJrunParameters[-grep("_include",
rownames(LPJrunParameters)),])

parameterStandard_PFT <- rbind(parameterStandard_PFT, spp)


tmp <- as.list(parameterStandard_PFT)
namestmp <- rownames(parameterStandard_PFT)
names(tmp) <- namestmp

parameterStandard_PFT <- tmp


return(list(runParameters = parameterStandard_PFT, design = designLPJ, defaultfiles = filelist))
}

Loading