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
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.
[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]
type = SamplerReceiver
[]
[]
[Postprocessors]
[left_bc]
type = PointValue
point = '0 0 0'
variable = u
[]
[right_bc]
type = PointValue
point = '1 0 0'
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 = Uniform
lower_bound = 0
upper_bound = 0.5
[]
[uniform_right]
type = Uniform
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 = MonteCarlo
num_rows = 10
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 SamplerParameterTransfer is utilized to communicate the sampler data to the sub-application. The 'parameters' input lists the parameters on the sub-applications to perturb.
[Transfers]
[sub]
type = SamplerParameterTransfer
to_multi_app = sub
sampler = sample
parameters = 'BCs/left/value BCs/right/value'
execute_on = INITIAL
check_multiapp_execute_on = false
[]
[]
(modules/stochastic_tools/test/tests/transfers/monte_carlo/monte_carlo.i)[StochasticTools]
auto_create_executioner = false
[]
[Distributions]
[uniform_left]
type = Uniform
lower_bound = 0
upper_bound = 0.5
[]
[uniform_right]
type = Uniform
lower_bound = 1
upper_bound = 2
[]
[]
[Samplers]
[sample]
type = MonteCarlo
num_rows = 10
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 = SamplerParameterTransfer
to_multi_app = sub
sampler = sample
parameters = 'BCs/left/value BCs/right/value'
execute_on = INITIAL
check_multiapp_execute_on = false
[]
[]
[Executioner]
type = Transient
num_steps = 5
dt = 0.01
[]
[Outputs]
execute_on = 'INITIAL TIMESTEP_END'
[]
(modules/stochastic_tools/test/tests/transfers/monte_carlo/monte_carlo.i)