|
| 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) |
0 commit comments