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.

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

[Mesh<<<{"href": "../../../syntax/Mesh/index.html"}>>>]
  type = GeneratedMesh
  dim = 1
  nx = 10
[]

[Variables<<<{"href": "../../../syntax/Variables/index.html"}>>>]
  [u]
  []
[]

[Kernels<<<{"href": "../../../syntax/Kernels/index.html"}>>>]
  [diff]
    type = Diffusion<<<{"description": "The Laplacian operator ($-\\nabla \\cdot \\nabla u$), with the weak form of $(\\nabla \\phi_i, \\nabla u_h)$.", "href": "../../../source/kernels/Diffusion.html"}>>>
    variable<<<{"description": "The name of the variable that this residual object operates on"}>>> = u
  []
  [time]
    type = TimeDerivative<<<{"description": "The time derivative operator with the weak form of $(\\psi_i, \\frac{\\partial u_h}{\\partial t})$.", "href": "../../../source/kernels/TimeDerivative.html"}>>>
    variable<<<{"description": "The name of the variable that this residual object operates on"}>>> = u
  []
[]

[BCs<<<{"href": "../../../syntax/BCs/index.html"}>>>]
  [left]
    type = DirichletBC<<<{"description": "Imposes the essential boundary condition $u=g$, where $g$ is a constant, controllable value.", "href": "../../../source/bcs/DirichletBC.html"}>>>
    variable<<<{"description": "The name of the variable that this residual object operates on"}>>> = u
    boundary<<<{"description": "The list of boundary IDs from the mesh where this object applies"}>>> = left
    value<<<{"description": "Value of the BC"}>>> = 0
  []
  [right]
    type = DirichletBC<<<{"description": "Imposes the essential boundary condition $u=g$, where $g$ is a constant, controllable value.", "href": "../../../source/bcs/DirichletBC.html"}>>>
    variable<<<{"description": "The name of the variable that this residual object operates on"}>>> = u
    boundary<<<{"description": "The list of boundary IDs from the mesh where this object applies"}>>> = right
    value<<<{"description": "Value of the BC"}>>> = 1
  []
[]

[Executioner<<<{"href": "../../../syntax/Executioner/index.html"}>>>]
  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<<<{"href": "../../../syntax/Controls/index.html"}>>>]
  [stochastic]
    type = SamplerReceiver<<<{"description": "Control for receiving data from a Sampler via SamplerParameterTransfer.", "href": "../../../source/controls/SamplerReceiver.html"}>>>
  []
[]

[Postprocessors<<<{"href": "../../../syntax/Postprocessors/index.html"}>>>]
  [left_bc]
    type = PointValue<<<{"description": "Compute the value of a variable at a specified location", "href": "../../../source/postprocessors/PointValue.html"}>>>
    point<<<{"description": "The physical point where the solution will be evaluated."}>>> = '0 0 0'
    variable<<<{"description": "The name of the variable that this postprocessor operates on."}>>> = u
  []
  [right_bc]
    type = PointValue<<<{"description": "Compute the value of a variable at a specified location", "href": "../../../source/postprocessors/PointValue.html"}>>>
    point<<<{"description": "The physical point where the solution will be evaluated."}>>> = '1 0 0'
    variable<<<{"description": "The name of the variable that this postprocessor operates on."}>>> = u
  []
[]

[Outputs<<<{"href": "../../../syntax/Outputs/index.html"}>>>]
  csv<<<{"description": "Output the scalar variable and postprocessors to a *.csv file using the default CSV output."}>>> = true
[]
(moose/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<<<{"href": "../../../syntax/Distributions/index.html"}>>>]
  [uniform_left]
    type = Uniform<<<{"description": "Continuous uniform distribution.", "href": "../../../source/distributions/Uniform.html"}>>>
    lower_bound<<<{"description": "Distribution lower bound"}>>> = 0
    upper_bound<<<{"description": "Distribution upper bound"}>>> = 0.5
  []
  [uniform_right]
    type = Uniform<<<{"description": "Continuous uniform distribution.", "href": "../../../source/distributions/Uniform.html"}>>>
    lower_bound<<<{"description": "Distribution lower bound"}>>> = 1
    upper_bound<<<{"description": "Distribution upper bound"}>>> = 2
  []
[]
(moose/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<<<{"href": "../../../syntax/Samplers/index.html"}>>>]
  [sample]
    type = MonteCarlo<<<{"description": "Monte Carlo Sampler.", "href": "../../../source/samplers/MonteCarloSampler.html"}>>>
    num_rows<<<{"description": "The number of rows per matrix to generate."}>>> = 10
    distributions<<<{"description": "The distribution names to be sampled, the number of distributions provided defines the number of columns per matrix."}>>> = 'uniform_left uniform_right'
    execute_on<<<{"description": "The list of flag(s) indicating when this object should be executed. For a description of each flag, see https://mooseframework.inl.gov/source/interfaces/SetupInterface.html."}>>> = INITIAL # create random numbers on initial and use them for each timestep
  []
[]
(moose/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<<<{"href": "../../../syntax/MultiApps/index.html"}>>>]
  [sub]
    type = SamplerTransientMultiApp<<<{"description": "Creates a sub-application for each row of each Sampler matrix.", "href": "../../../source/multiapps/SamplerTransientMultiApp.html"}>>>
    input_files<<<{"description": "The input file for each App.  If this parameter only contains one input file it will be used for all of the Apps.  When using 'positions_from_file' it is also admissable to provide one input_file per file."}>>> = sub.i
    sampler<<<{"description": "The Sampler object to utilize for creating MultiApps."}>>> = sample
  []
[]
(moose/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<<<{"href": "../../../syntax/Transfers/index.html"}>>>]
  [sub]
    type = SamplerParameterTransfer<<<{"description": "Copies Sampler data to a SamplerReceiver object.", "href": "../../../source/transfers/SamplerParameterTransfer.html"}>>>
    to_multi_app<<<{"description": "The name of the MultiApp to transfer the data to"}>>> = sub
    sampler<<<{"description": "A the Sampler object that Transfer is associated.."}>>> = sample
    parameters<<<{"description": "A list of parameters (on the sub application) to control with the Sampler data. The order of the parameters listed here should match the order of the items in the Sampler."}>>> = 'BCs/left/value BCs/right/value'
    execute_on<<<{"description": "The list of flag(s) indicating when this object should be executed. For a description of each flag, see https://mooseframework.inl.gov/source/interfaces/SetupInterface.html."}>>> = INITIAL
    check_multiapp_execute_on<<<{"description": "When false the check between the multiapp and transfer execute on flags is not performed."}>>> = false
  []
[]
(moose/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.

[StochasticTools<<<{"href": "../../../syntax/StochasticTools/index.html"}>>>]
  auto_create_executioner<<<{"description": "Automatically setup the Executioner block for a master application without a simulation."}>>> = false
[]

[Distributions<<<{"href": "../../../syntax/Distributions/index.html"}>>>]
  [uniform_left]
    type = Uniform<<<{"description": "Continuous uniform distribution.", "href": "../../../source/distributions/Uniform.html"}>>>
    lower_bound<<<{"description": "Distribution lower bound"}>>> = 0
    upper_bound<<<{"description": "Distribution upper bound"}>>> = 0.5
  []
  [uniform_right]
    type = Uniform<<<{"description": "Continuous uniform distribution.", "href": "../../../source/distributions/Uniform.html"}>>>
    lower_bound<<<{"description": "Distribution lower bound"}>>> = 1
    upper_bound<<<{"description": "Distribution upper bound"}>>> = 2
  []
[]

[Samplers<<<{"href": "../../../syntax/Samplers/index.html"}>>>]
  [sample]
    type = MonteCarlo<<<{"description": "Monte Carlo Sampler.", "href": "../../../source/samplers/MonteCarloSampler.html"}>>>
    num_rows<<<{"description": "The number of rows per matrix to generate."}>>> = 10
    distributions<<<{"description": "The distribution names to be sampled, the number of distributions provided defines the number of columns per matrix."}>>> = 'uniform_left uniform_right'
    execute_on<<<{"description": "The list of flag(s) indicating when this object should be executed. For a description of each flag, see https://mooseframework.inl.gov/source/interfaces/SetupInterface.html."}>>> = INITIAL # create random numbers on initial and use them for each timestep
  []
[]

[MultiApps<<<{"href": "../../../syntax/MultiApps/index.html"}>>>]
  [sub]
    type = SamplerTransientMultiApp<<<{"description": "Creates a sub-application for each row of each Sampler matrix.", "href": "../../../source/multiapps/SamplerTransientMultiApp.html"}>>>
    input_files<<<{"description": "The input file for each App.  If this parameter only contains one input file it will be used for all of the Apps.  When using 'positions_from_file' it is also admissable to provide one input_file per file."}>>> = sub.i
    sampler<<<{"description": "The Sampler object to utilize for creating MultiApps."}>>> = sample
  []
[]

[Transfers<<<{"href": "../../../syntax/Transfers/index.html"}>>>]
  [sub]
    type = SamplerParameterTransfer<<<{"description": "Copies Sampler data to a SamplerReceiver object.", "href": "../../../source/transfers/SamplerParameterTransfer.html"}>>>
    to_multi_app<<<{"description": "The name of the MultiApp to transfer the data to"}>>> = sub
    sampler<<<{"description": "A the Sampler object that Transfer is associated.."}>>> = sample
    parameters<<<{"description": "A list of parameters (on the sub application) to control with the Sampler data. The order of the parameters listed here should match the order of the items in the Sampler."}>>> = 'BCs/left/value BCs/right/value'
    execute_on<<<{"description": "The list of flag(s) indicating when this object should be executed. For a description of each flag, see https://mooseframework.inl.gov/source/interfaces/SetupInterface.html."}>>> = INITIAL
    check_multiapp_execute_on<<<{"description": "When false the check between the multiapp and transfer execute on flags is not performed."}>>> = false
  []
[]

[Executioner<<<{"href": "../../../syntax/Executioner/index.html"}>>>]
  type = Transient
  num_steps = 5
  dt = 0.01
[]

[Outputs<<<{"href": "../../../syntax/Outputs/index.html"}>>>]
  execute_on<<<{"description": "The list of flag(s) indicating when this object should be executed. For a description of each flag, see https://mooseframework.inl.gov/source/interfaces/SetupInterface.html."}>>> = 'INITIAL TIMESTEP_END'
[]
(moose/modules/stochastic_tools/test/tests/transfers/monte_carlo/monte_carlo.i)