Skip to content

Commit 2b9b2ee

Browse files
authored
Merge pull request #168 from ajnonaka/ajn_pytuq
AMReX+PyTUQ
2 parents cb0fbfd + bc1ba81 commit 2b9b2ee

File tree

12 files changed

+823
-5
lines changed

12 files changed

+823
-5
lines changed

.github/workflows/docs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
.github/workflows/dependencies/documentation.sh
2020
echo "Installing python packages for docs..."
2121
python3 -m pip install --upgrade pip
22-
python3 -m pip install sphinx sphinx_rtd_theme breathe sphinxcontrib.bibtex docutils
22+
python3 -m pip install sphinx sphinx_rtd_theme breathe sphinxcontrib.bibtex docutils sphinx-copybutton sphinx-design
2323
2424
- name: Install and Build
2525
run: |

Docs/Readme.sphinx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ If you have conda, you can install the necessary packages as follows:
2323

2424
2. Type "conda install sphinx_rtd_theme"
2525

26+
3. Type "conda install sphinx-design"
27+
28+
4. Type "conda install -c conda-forge sphinx-copybutton"
29+
2630
If you don't have a conda Python installation, you get one by doing the following:
2731

2832
1. Go to https://conda.io/miniconda.html and download the Python 3.6 64-bit (bash installer), "Miniconda3-latest-Linux-x86_64.sh". Save this script to your hard drive.

Docs/source/HeatEquation_UQ.rst

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
.. _tutorials_uq:
2+
3+
.. _guided_pytuq_integration:
4+
5+
.. _pytuq_quickstart:
6+
7+
UQ with PyTUQ
8+
===========
9+
10+
.. admonition:: **Time to Complete**: 30-60 minutes
11+
:class: note
12+
13+
**What you will learn**:
14+
- Install PyTUQ
15+
- Run an AMReX+PyTUQ to perform sensitivity analysis
16+
17+
Overview
18+
--------
19+
20+
AMReX simulations deliver high-fidelity, accurate results for complex physics problems, but the effect on simulation results due to uncertain inputs can require hundreds or thousands of simulations, making comprehensive analysis computationally prohibitive.
21+
22+
This tutorial demonstrates how to improve efficiency without sacrificing accuracy by using polynomial chaos expansions to build fast surrogate models that identify which input parameters truly matter.
23+
24+
PyTUQ (Python interface to the UQ Toolkit) provides specialized tools for surrogate construction and global sensitivity analysis, enabling rapid parameter space exploration and dimensionality reduction for scientific applications.
25+
26+
We demonstrate how to integrate PyTUQ with your AMReX application through a practical workflow; the AMReX-based heat equation tutorial is equipped to perform sensitivity analysis.
27+
28+
In these examples we model the heat equation
29+
30+
.. math:: \frac{\partial\phi}{\partial t} = D\nabla^2 \phi,
31+
32+
with initial condition
33+
34+
.. math:: \phi_0 = 1 + A e^{-r^2 / (2V)},
35+
36+
where ``r`` is the distance from the center of the domain, and with uncertain parameters ``diffusion_coeff`` (:math:`D`), ``init_amplitude`` (:math:`A`), and ``init_variance`` (:math:`V`).
37+
The outputs of interest are the maximum temperature, mean temperature, standard deviation of temperature, and the temperature at a specified point.
38+
39+
.. toctree::
40+
:maxdepth: 1
41+
42+
HeatEquation_UQ_MathematicalDetails
43+
44+
Located in ``amrex-tutorials/ExampleCodes/UQ/HeatEquation``, this example illustrates a complete forward UQ workflow from parameter sampling randomized input parameters to perform sensitivity analysis.
45+
By understanding this example, you will have a basis for understanding how to adapt this workflow to your own AMReX application.
46+
47+
More specifically, you can directly compare/diff ``amrex-tutorials/ExampleCodes/UQ/HeatEquation/main.cpp`` against the original heat equation tutorial ``amrex-tutorials/GuidedTutorials/HeatEquation_Simple/main.cpp`` to see exactly what source code changes are made to the AMReX application in this example.
48+
49+
Installation
50+
------------
51+
52+
We now describe the installation and workflow process on a local workstation.
53+
First, install pytuq using this script (based on information provided in `pytuq/README.md <https://github.com/sandialabs/pytuq/blob/main/README.md>`_):
54+
55+
.. code-block:: bash
56+
:caption: Pytuq installation script
57+
58+
#!/bin/bash
59+
60+
# 1. Clone repositories
61+
git clone https://github.com/sandialabs/pytuq.git
62+
63+
# 2. Create a conda environment with python (optional, you can add to an existing env)
64+
conda create --name pytuq
65+
conda activate pytuq
66+
conda install python=3.11
67+
68+
# 3. Install PyTUQ and requirements
69+
cd pytuq
70+
pip install -r requirements.txt
71+
pip install .
72+
conda install dill
73+
74+
# 4. Verify installation
75+
conda list | grep pytuq # Should show pytuq 1.0.0
76+
77+
Examples
78+
--------
79+
80+
C++ AMReX + PyTUQ (BASH driven)
81+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
82+
83+
**Prerequisites**:
84+
85+
- AMReX and pytuq cloned at the same directory level as amrex-tutorials
86+
- pytuq installation described above
87+
- GNU Parallel for task management: ``sudo apt-get install parallel`` (mpiexec option also exists in script)
88+
89+
.. code-block:: bash
90+
:caption: Build AMReX appplication
91+
92+
cd /path/to/amrex-tutorials/ExampleCodes/UQ/HeatEquation/
93+
make -j4
94+
95+
.. code-block:: bash
96+
:caption: Run sensitivity analysis with bash script
97+
98+
./workflow_uqpc.x
99+
100+
Understanding GNU Parallel Workflow Pattern
101+
-------------------------------------------
102+
103+
The ``workflow_uqpc.x`` bash script relies on the user augmenting their codes to write outputs of interest to ASCII text files.
104+
In this case, the ``main.cpp`` was modified from the ``amrex-tutorials/GuidedTutorials/HeatEquation_Simple/main.cpp`` in the following ways:
105+
106+
First, support for parsing ``diffusion_coeff``, ``init_amplitude``, and ``init_variance`` from the input file and command line were added.
107+
108+
Second, support for writing outputs of interest to ASCII text files is added. The ``datalog_filename`` input parameter is generated by the bash
109+
script to give each simulation output a unique identifier.
110+
The ``datalog_int`` parameter gives the user the option to write the outputs of interest at a given time step interval, but in this example,
111+
the outputs of interest after the final step are those that matter, and are extracted by the bash script to create a master output file
112+
containing a separate set of simulation outputs of interest in each row.
113+
114+
The bash script calls PyTUQ scripts that generate an input parameter files for training and testing (``ptrain.txt`` and ``ptest.txt``)
115+
based on polynomial chaos settings, and then uses GNU Parallel to run multiple simulations
116+
efficiently and collect outputs into results files (``ytrain.txt`` and ``ytest.txt``) that PyTUQ can use for surrogate model fitting.
117+
118+
Understanding the Output
119+
------------------------
120+
121+
| `pnames.txt`, `outnames.txt`
122+
| -names of input and output parameters
123+
124+
| `param_margpc.txt`
125+
| -Each row contains the mean and standard deviation for an uncertain input parameter
126+
127+
| `qtrain.txt`, `qtest.txt`
128+
| -each row is a separate set of normal random variables used to generate uncertain inputs
129+
130+
| `ptrain.txt`, `ptest.txt`, `pall.txt`
131+
| -each row is a separate set of input parameters for each simulation
132+
133+
| `stdoutlog_train##.txt`, `stdoutlog_test##.txt`
134+
| -All standard output from AMReX simulations for training and testing data. The numbers refer to separate simulations.
135+
136+
| `datalog_train##.txt`, `datalog_test##.txt`
137+
| -Specifically-chosen output from AMReX simulations for training and testing data. In this example it reports the outputs (max, mean, standard deviation, and specific cell temperature) at user-specified intervals.
138+
139+
| `ytrain.txt`, `ytest.txt`, `yall.txt`
140+
| -agglomeration of outputs of interest from all simulations
141+
142+
| `results.pk`
143+
| -Python pickle file encapsulating the results
144+
145+
| `labels.txt`
146+
| -list of labels of simulation types (training or testing) for diagnostic/plot generation purposes
147+
148+
| `xx_<INVAR1>_<INVAR2>.png`
149+
| -scatter plot of 2 inputs for training and testing
150+
151+
| `pcoord_1.png`, `pcoord_1_lab_Testing.png`, `pcoord_1_lab_Training.png`
152+
| -graphical representation of how inputs correlate to outputs for each individual simulation
153+
154+
| `yx_<OUTVAR>.png`, `yx_<OUTVAR>_log.png`
155+
| -scatter plots of output as a function of each input
156+
157+
| `yxx_#.png`
158+
| -scatter plots of output a function of multiple inputs
159+
160+
| `pdf_tri_inputs.png`, `pdf_tri_output.png`
161+
| -PDFs of inputs and outputs
162+
163+
| `ensemble.png`
164+
| -graphical display of all output values
165+
166+
| `idm_#_training.png`, `idm_#_testing.png`
167+
| -graphical display of output values
168+
169+
| `dm_#.png`
170+
| -”data vs model” parity plots for each output; compares the predicted values from a surrogate model or approximation against the true or actual values from the full computational model
171+
172+
| `fit_s#_training.png`, `fit_s#_testing.png`
173+
| -Shows model vs PC approximation for a single simulation.
174+
175+
| `pdf_output_#.png`, `pdf_joyplot.png`
176+
| -PDF of output variables
177+
178+
| `allsens_main.txt`, `sens_main.png`
179+
| -raw data and plot for parameter sensitivities
180+
181+
| `jsens_#.png`, `Jsens_ave.png`
182+
| -joint sensitivities of output with respect to inputs
183+
184+
| `sensmat_main.png`
185+
| -sensitivity matrix
186+
187+
| `pcslices_o#.png`
188+
| -polynomial chaos fits
189+
190+
| `pccont_o#_d#_d#.png`
191+
| -polynomial chaos fits of output variables with respect to two input variables
192+
193+
194+
Additional Resources
195+
--------------------
196+
197+
**PyTUQ Resources:**
198+
199+
- `PyTUQ Documentation <https://sandialabs.github.io/pytuq>`_
200+
- `PyTUQ Examples directory <https://github.com/sandialabs/pytuq/tree/main/examples>`_
201+
- eebaill, ksargsyan, & Bert Debusschere. (2025). sandialabs/pytuq: v1.0.0z (v1.0.0z). Zenodo. https://doi.org/10.5281/zenodo.17110054
202+
203+
**AMReX Resources:**
204+
205+
- `AMReX Documentation <https://amrex-codes.github.io/amrex/docs_html/>`_
206+
207+
**Uncertainty Quantification Theory:**
208+
209+
- Ghanem, Roger, David Higdon, and Houman Owhadi, eds. *Handbook of Uncertainty Quantification*. Vol. 6. New York: Springer, 2017. (For workflow, plotting, and analysis specifics)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
.. _pytuq_mathematical_details:
2+
3+
Parameter Sensitivity via Polynomial Chaos
4+
-------------------------------------------
5+
6+
In this example, PyTUQ constructs polynomial chaos expansions to analyze how uncertain physical parameters affect simulation outputs in a heat diffusion problem.
7+
8+
The Heat Equation
9+
^^^^^^^^^^^^^^^^^
10+
11+
The governing equation for this tutorial is the heat diffusion equation:
12+
13+
.. math:: \frac{\partial\phi}{\partial t} = D\nabla^2 \phi,
14+
15+
with initial condition
16+
17+
.. math:: \phi_0 = 1 + A e^{-r^2 / (2V)},
18+
19+
where ``r`` is the distance from the center of the domain, and with uncertain parameters ``diffusion_coeff`` (:math:`D`), ``init_amplitude`` (:math:`A`), and ``init_variance`` (:math:`V`).
20+
21+
22+
23+
Quantities of Interest (Outputs)
24+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
25+
26+
The simulation extracts four statistical quantities at the final timestep:
27+
28+
1. **max_temp**: Maximum temperature in the domain
29+
2. **mean_temp**: Average temperature across all cells
30+
3. **std_temp**: Standard deviation of temperature
31+
4. **cell_temp**: Temperature in a particular cell, in this case, cell (9,9,9)
32+
33+
These outputs are computed in ``main.cpp``and written to the datalog file.
34+
35+
PyTUQ Workflow
36+
^^^^^^^^^^^^^^
37+
38+
PyTUQ uses polynomial chaos expansion to construct a surrogate model:
39+
40+
1. **Parameter sampling**: Generate sample points in the 3D parameter space based on the specified distributions
41+
2. **Forward simulations**: Run the AMReX heat equation solver for each parameter set
42+
3. **Surrogate construction**: Fit polynomial chaos coefficients to map inputs → outputs
43+
4. **Sensitivity analysis**: Compute Sobol indices to identify which parameters most influence each output
44+
45+
The connection is:
46+
47+
- **Inputs**: ParmParse parameters (``diffusion_coeff``, ``init_amplitude``, ``init_variance``) specified in ``inputs`` file or command line
48+
- **Outputs**: Quantities of interest extracted from datalog files

Docs/source/conf.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ def get_amrex_version():
3939
extensions = ['sphinx.ext.mathjax',
4040
'sphinx.ext.githubpages',
4141
'sphinx.ext.viewcode',
42+
'sphinx_design',
43+
'sphinx_copybutton',
4244
'sphinx.ext.intersphinx',
4345
'sphinx_rtd_theme']
4446

@@ -51,6 +53,9 @@ def get_amrex_version():
5153
# Add any paths that contain templates here, relative to this directory.
5254
templates_path = ['ytemplates']
5355

56+
# sphinx-copybutton configuration
57+
copybutton_exclude = '.linenos, .gp, .go'
58+
5459
# The suffix(es) of source filenames.
5560
# You can specify multiple suffix as a list of string:
5661
#

Docs/source/index.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ sorted by the following categories:
5050
- :ref:`GPU<tutorials_gpu>` -- Offload work to the GPUs using AMReX tools.
5151
- :ref:`Linear Solvers<tutorials_linearsolvers>` -- Examples of several linear solvers.
5252
- :ref:`ML/PYTORCH<tutorials_ml>` -- Use of pytorch models to replace point-wise computational kernels.
53+
- :ref:`UQ with pytuq<tutorials_uq>` -- Use of pytuq methods to perform generalized sensitivity analysis.
5354
- :ref:`MPMD<tutorials_mpmd>` -- Usage of AMReX-MPMD (Multiple Program Multiple Data) framework.
5455
- :ref:`MUI<tutorials_mui>` -- Incorporates the MxUI/MUI (Multiscale Universal interface) frame into AMReX.
5556
- :ref:`Particles<tutorials_particles>` -- Basic usage of AMReX's particle data structures.
@@ -72,6 +73,7 @@ sorted by the following categories:
7273
GPU_Tutorial
7374
LinearSolvers_Tutorial
7475
ML_Tutorial
76+
HeatEquation_UQ
7577
MPMD_Tutorials
7678
MUI_Tutorial
7779
Particles_Tutorial
@@ -97,6 +99,8 @@ sorted by the following categories:
9799

98100
.. _`Linear Solvers`: LinearSolvers_Tutorial.html
99101

102+
.. _`UQ`: UQ_Tutorial.html
103+
100104
.. _`MPMD`: MPMD_Tutorials.html
101105

102106
.. _`MUI`: MUI_Tutorial.html
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# AMREX_HOME defines the directory in which we will find all the AMReX code.
2+
AMREX_HOME ?= ../../../../amrex
3+
4+
DEBUG = FALSE
5+
USE_MPI = TRUE
6+
USE_OMP = FALSE
7+
COMP = gnu
8+
DIM = 3
9+
10+
include $(AMREX_HOME)/Tools/GNUMake/Make.defs
11+
12+
include ./Make.package
13+
14+
include $(AMREX_HOME)/Src/Base/Make.package
15+
16+
include $(AMREX_HOME)/Tools/GNUMake/Make.rules
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
CEXE_sources += main.cpp
2+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Grid/Domain parameters
2+
n_cell = 32 # number of cells on each side of domain
3+
max_grid_size = 16 # size of each box (or grid)
4+
5+
# Time stepping parameters
6+
nsteps = 100 # total steps in simulation
7+
dt = 5.e-8 # time step
8+
9+
# Output control
10+
plot_int = -1 # how often to write plotfile (-1 = no plots)
11+
datalog_int = -1 # how often to write datalog (-1 = no regular output)
12+
13+
# Physics parameters (these are what we vary for UQ and values here will be overwritten
14+
diffusion_coeff = 100. # diffusion coefficient for heat equation
15+
init_amplitude = 1. # amplitude of initial temperature profile
16+
init_variance = 0.01 # variance of initial temperature profile

0 commit comments

Comments
 (0)