---
title: "VMap"
author: "D. Renard"
date: "21 février 2022"
output:
  pdf_document: default
  html_document: default
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
rm(list=ls())
library(RGeostats)
constant.define("nproc",0)
```

## Introduction

This chapter is meant to give a demonstration of the calculation of the Variogram Map and the corresponding Fitting procedure

# Data Set

We create a Model composed of nested anisotropic structures

```{r}
model = model.create(vartype="Nugget Effect",sill=0.1)
model = model.create(vartype="Exponential",sill=0.2,range=c(0.1,0.3),
                     aniso.angles=30,model=model)
model = model.create(vartype="Spherical",sill=0.1,range=c(0.4,0.6),
                     aniso.angles=60,model=model)
model
```

We create the data set as a non-conditional simulation over a grid

```{r}
grid = db.create(nx=c(100,100),dx=c(0.01,0.01))
grid = simtub(,grid,model)
```

Calculate the Variogram Map. This calculation is adapted to the structure of the input Data Set (i.e. grid). Note that, if data were defined on a set of isolated points, we would have to use vmap.calc() instead of vmap.grid().

```{r}
vmap = vmap.grid(grid,nx=25,ny=25)
plot(vmap)
```

Fitting the Model from the Variogram Map. This step takes some time as the inference is performed on a Variogram Map which contains 51*51 cells as information.

```{r}
modres = vmap.auto(vmap)
```

The result is quite convincing when compared to the initial model used to perform the simulation, as shown in the figure which represents, side by side, the experimental variogram map and the one resulting from the Model that has just be inferred.

```{r}
modres
```

We can see that the resulting model has found 3 structures:
- a nugget effect with a sill of 0.13 (instead of 0.1)
- a short range anisotropic structure: Gaussian with long range of 0.12 and short range of 0.04 and a rotation angle of 28deg (instead of an Exponential with long range of 0.3 and short range of 0.1 and a rotation of 60 deg [for the small range direction])
- a long range structure: a spherical with a long range of 0.5 and a short range of 0.18 and a rotation of 123deg (instead of a spherical with long range of 0.6 and a short range of 0.4 and a rotation of 67deg [for the long range direction]). 
