Skip to content
Snippets Groups Projects
user_workflow_vignette.Rmd 6.8 KiB
Newer Older
---
title: "r2ogs6 User Guide"
author: "Ruben Heinrich"
output: html_document
#output: rmarkdown::html_vignette
#vignette: >
#  %\VignetteIndexEntry{r2ogs6 User Guide}
#  %\VignetteEngine{knitr::rmarkdown}
#  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  eval = nzchar(Sys.getenv("r2ogs6_ensemble_guide_eval")),
  collapse = TRUE,
  comment = "#>"
)
# the python library vtk needs an explicit import statement for knitting
vtk <- reticulate::import("vtk")
dsa <- reticulate::import("vtk.numpy_interface.dataset_adapter")
devtools::load_all(".")
```

```{r setup}
library(r2ogs6)
```

## Prerequisites

This guide assumes you have `r2ogs6` and its dependencies installed. If that's not the case, please take a look at the installation instructions provided in the `README.md` file of the repository.
 
After loading `r2ogs6`, we first need to set the package options so it knows where to look for OpenGeoSys 6.

```r
# Set path for OpenGeoSys 6
options("r2ogs6.default_ogs6_bin_path" = "your_ogs6_bin_path")
## Creating your simulation object
To represent a simulation object, `r2ogs6` uses an `R6` class called `OGS6`. If you're new to `R6` objects, don't worry. Creating a simulation object is easy. We call the class constructor and provide it with some parameters:

* `sim_name` The name of your simulation

* `sim_path` All relevant files for your simulation will be in here

```{r}
# Change this to fit your system
# sim_path <- system.file("extdata/benchmarks/flow_no_strain",
#                         package = "r2ogs6")
sim_path <- tempdir()
ogs6_obj <- OGS6$new(sim_name = "my_simulation",
```

And that's it, we now have a simulation object. 


## Defining the simulation parameters
From here on there's two ways you can define the simulation parameters. Either you load a benchmark file or you define your simulation manually.
### Loading a benchmark file
The quickest and easiest way to define a simulation is by using an already existing benchmark. If you take a look at the [OpenGeoSys documentation](https://www.opengeosys.org/docs/benchmarks/elliptic/elliptic-dirichlet/), you'll find plenty of benchmarks to choose from along with a link to their project file on GitLab at the top of the respective page.
For demonstration purposes, I will use a project from the `HydroMechanics` benchmarks, which can be found [here](https://gitlab.opengeosys.org/ogs/ogs/-/tree/master/Tests/Data/HydroMechanics/IdealGas/flow_no_strain).
# Change this to fit your system
prj_path <- paste0(sim_path, "/flow_no_strain.prj")
prj_path <- system.file("extdata/benchmarks/flow_no_strain/flow_no_strain.prj",
                       package = "r2ogs6")
read_in_prj(ogs6_obj, prj_path = prj_path, read_in_gml = T)
NOTE: `r2ogs6` has not been tested with every existing benchmark. Due to the large number of input parameters, you might encounter cases where the import fails.
### Setting up your own OpenGeoSys 6 simulation
Setting up your own simulation is possible too. 
Since there's plenty of required and optional input parameters, you might want to call `get_status()` occasionally to get a brief overview of your simulation. This tells you which input parameters are missing before you can run a simulation.
# Call on the OGS6 object (note the R6 style)
ogs6_obj$get_status()
Since we haven't defined anything so far, you'll see a lot of red there. But the results gave us a hint what we can add. We'll go from there and try to find out more about the possible input data. Say we want to find out more about `process` objects.
```{r}
# To take a look at the documentation, use ? followed by the name of a class
?prj_process
```
As a rule of thumb, classes are named with the prefix `prj_` followed by their XML tag name in the `.prj` file. The only exceptions to this rule are subclasses where this would lead to duplicate class names. The class `prj_time_loop` for example contains a subclass representing a `process` child element which is not to be confused with the `process` children of the first level `processes` node directly under the root node of the `.prj` file. Because of this, that subclass is named `prj_tl_process`. Let's try adding something now.

To add data to our simulation object, we use `OGS6$add()`. We can use this method with any top level `.prj` element, which means we're not limited to `prj_parameter` objects. 

```{r eval = F}
# Add a parameter
ogs6_obj$add(prj_parameter(name = "E",
                           type = "Constant",
                           value = 1e+10))


# Add a process variable
ogs6_obj$add(
    prj_process_variable(
        name = "pressure",
        components = 1,
        order = 1,
        initial_condition = "pressure0",
        boundary_conditions = list(
            boundary_condition = prj_boundary_condition(
                type = "Neumann",
                parameter = "flux_in",
                geometrical_set = "cube_1x1x1_geometry",
                geometry = "left",
                component = 0
            )
        )
Since I already read in a `.prj` file earlier, I won't run the above snippet. If you'd like a complete example of manually defining simulation parameters, there's a script `flow_free_expansion.R` in the `examples/workflow_demos` folder.
As soon as we've added all necessary parameters, we can try starting our simulation. This will run a few additional checks and then start OpenGeoSys 6. If `write_logfile` is set to `FALSE`, the output from OpenGeoSys 6 will be shown on the console.
```{r results='hide'}
ogs6_run_simulation(ogs6_obj, write_logfile = TRUE)
## Retrieve the results
After our simulation is finished, we might want to plot some results. But how do we retrieve them? If all went as expected, we don't need to call an extra function for that because `ogs6_run_simulation()` already calls `ogs6_read_output_files()` internally. We only need to decide what information we want to extract. Say we're interested in the `pressure` Parameter from the last timestep. For this easy example, only one `.pvd` file was produced.
```{r include = T}
ogs6_read_output_files(ogs6_obj)
```
```{r fig.width=5}
# Extract relevant info into dataframe
result_df <- ogs6_obj$pvds[[1]]$get_point_data(keys = c("pressure"))
result_df <- result_df[(result_df$timestep!=0),]
  
# Plot results
ggplot(result_df,
       aes(x = x,
           y = y,
           color = pressure)) +
    #geom_raster(interpolate = T)+
    #geom_contour_filled()+
    ylab("y coordinate") +
    theme_bw()
## Running multiple simulations
If we want to run not one but multiple simulations, we can use the simulation object we just created as a blueprint for an ensemble run. The workflow for this is described in detail [here](ensemble_workflow_vignette.Rmd).