# Monte Carlo Example

Possibly the simplest example of a stochastic analysis is to perform Monte Carlo analysis of a given simulation. That is, solve a single problem and vary parameters within this simulation with a random set of perturbed parameters.

## Problem Statement

The first step is to define the simulation to perform, in this case the simulation considered is a 1D transient diffusion equation with Dirichlet boundary conditions on each end of the domain: find such that

(1) where , , and defines a continuous uniform distribution with and defining the lower and upper limits of the distribution, respectively.

## Sub-Application Input

The problem defined above, with respect to the MultiApps system, is a sub-application. The complete input file for the problem is provided in Listing 1. The only item required to enable the stochastic analysis is the Controls block, which contains a SamplerReceiver object, the use of which will be explained in the following section.

Listing 1: Complete input file for executing the transient diffusion problem.

[Mesh]
type = GeneratedMesh
dim = 1
nx = 10
[]

[Variables]
[./u]
[../]
[]

[Kernels]
[./diff]
type = Diffusion
variable = u
[../]
[./time]
type = TimeDerivative
variable = u
[../]
[]

[BCs]
[./left]
type = DirichletBC
variable = u
boundary = left
value = 0
[../]
[./right]
type = DirichletBC
variable = u
boundary = right
value = 1
[../]
[]

[Executioner]
type = Transient
num_steps = 5
dt = 0.01
solve_type = PJFNK
petsc_options_iname = '-pc_type -pc_hypre_type'
petsc_options_value = 'hypre boomeramg'
[]

[Controls]
[./stochastic]
[../]
[]

[Postprocessors]
[./left_bc]
type = NodalVariableValue
nodeid = 0
variable = u
[../]
[./right_bc]
type = NodalVariableValue
nodeid = 10
variable = u
[../]
[]

[Outputs]
csv = true
[]

(modules/stochastic_tools/test/tests/transfers/monte_carlo/sub.i)

## Master Input

The master application, with respect to the MultiApps system, is the driver of the stochastic simulations, by itself it does not perform a solve. The complete input file for the master application is shown in Listing 2, but the import sections will be detailed individually.

First, Distributions for each of the two stochastic boundary conditions are defined.

[Distributions]
[./uniform_left]
type = UniformDistribution
lower_bound = 0
upper_bound = 0.5
[../]
[./uniform_right]
type = UniformDistribution
lower_bound = 1
upper_bound = 2
[../]
[]

(modules/stochastic_tools/test/tests/transfers/monte_carlo/monte_carlo.i)

Second, a MonteCarloSampler is defined that utilizes the two distributions and creates the Monte Carlo samples.

[Samplers]
[./sample]
type = MonteCarloSampler
n_samples = 5
distributions = 'uniform_left uniform_right'
execute_on = INITIAL # create random numbers on initial and use them for each timestep
[../]
[]

(modules/stochastic_tools/test/tests/transfers/monte_carlo/monte_carlo.i)

Notice, that this sampler only executes on "initial", which means that the random numbers are created once during the initial setup of the problem and not changed again during the simulation.

Next, a SamplerTransientMultiApp object is created. This object creates and runs a sub-application for each sample provided by the sampler object.

[MultiApps]
[./sub]
type = SamplerTransientMultiApp
input_files = sub.i
sampler = sample
[../]
[]

(modules/stochastic_tools/test/tests/transfers/monte_carlo/monte_carlo.i)

Finally, the SamplerTransfer is utilized to communicate the sampler data to the sub-application. The 'parameters' input lists the parameters on the sub-applications to perturb and the 'to_control' specifies the SamplerReceiver object in the sub-application.

[Transfers]
[./sub]
type = SamplerTransfer
multi_app = sub
parameters = 'BCs/left/value BCs/right/value'
to_control = 'stochastic'
execute_on = INITIAL
check_multiapp_execute_on = false
[../]
[]

(modules/stochastic_tools/test/tests/transfers/monte_carlo/monte_carlo.i)

Listing 2: Complete input file for master application for executing Monte Carlo stochastic simulations.

[Mesh]
type = GeneratedMesh
dim = 1
nx = 1
ny = 1
[]

[Variables]
[./u]
[../]
[]

[Distributions]
[./uniform_left]
type = UniformDistribution
lower_bound = 0
upper_bound = 0.5
[../]
[./uniform_right]
type = UniformDistribution
lower_bound = 1
upper_bound = 2
[../]
[]

[Samplers]
[./sample]
type = MonteCarloSampler
n_samples = 5
distributions = 'uniform_left uniform_right'
execute_on = INITIAL # create random numbers on initial and use them for each timestep
[../]
[]

[MultiApps]
[./sub]
type = SamplerTransientMultiApp
input_files = sub.i
sampler = sample
[../]
[]

[Transfers]
[./sub]
type = SamplerTransfer
multi_app = sub
parameters = 'BCs/left/value BCs/right/value'
to_control = 'stochastic'
execute_on = INITIAL
check_multiapp_execute_on = false
[../]
[]

[Executioner]
type = Transient
num_steps = 5
dt = 0.01
[]

[Problem]
solve = false
kernel_coverage_check = false
[]

[Outputs]
execute_on = 'INITIAL TIMESTEP_END'
[]

(modules/stochastic_tools/test/tests/transfers/monte_carlo/monte_carlo.i)