romtools.workflows.dakota.dakota_coupler
This module implements the class required to couple a model to Dakota. To couple to Dakota, a user should
- Complete a Model class for their application of interest
- Complete a driver script that instantiates the model and calls this coupler for use as the Dakota analysis driver
1# 2# ************************************************************************ 3# 4# ROM Tools and Workflows 5# Copyright 2019 National Technology & Engineering Solutions of Sandia,LLC 6# (NTESS) 7# 8# Under the terms of Contract DE-NA0003525 with NTESS, the 9# U.S. Government retains certain rights in this software. 10# 11# ROM Tools and Workflows is licensed under BSD-3-Clause terms of use: 12# 13# Redistribution and use in source and binary forms, with or without 14# modification, are permitted provided that the following conditions 15# are met: 16# 17# 1. Redistributions of source code must retain the above copyright 18# notice, this list of conditions and the following disclaimer. 19# 20# 2. Redistributions in binary form must reproduce the above copyright 21# notice, this list of conditions and the following disclaimer in the 22# documentation and/or other materials provided with the distribution. 23# 24# 3. Neither the name of the copyright holder nor the names of its 25# contributors may be used to endorse or promote products derived 26# from this software without specific prior written permission. 27# 28# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 29# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 30# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 31# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 32# COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 33# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 34# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 35# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 37# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 38# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 39# POSSIBILITY OF SUCH DAMAGE. 40# 41# Questions? Contact Eric Parish (ejparis@sandia.gov) 42# 43# ************************************************************************ 44# 45 46''' 47This module implements the class required to couple a model to Dakota. 48To couple to Dakota, a user should 491. Complete a Model class for their application of interest 502. Complete a driver script that instantiates the model and calls this coupler for use as the Dakota analysis driver 51''' 52 53import os 54import sys 55import time 56import numpy as np 57 58from romtools.workflows.models import QoiModel 59 60 61def _create_parameter_dict(parameter_names, parameter_values): 62 return dict(zip(parameter_names, parameter_values)) 63 64 65def run_model_for_dakota( 66 model: QoiModel, 67 multifidelity_flag: bool = False, 68 add_core_time_metadata: bool = False, 69): 70 ''' 71 This function should be used in a driver script that will be called with 72 `python <driver.py> params.in results.out` 73 74 by Dakota. The parameter space is built by parsing params.in, 75 while the output QoI will be saved to results.out 76 77 Args: 78 model: rom-tools model instance (FOM or ROM) 79 multifidelity_flag: True if being used with Dakota's multifidelity (MF) tools 80 add_core_time_metadata: True if computational cost should be included in QoI file 81 Useful for MF UQ when model does not have a cost model. 82 83 ''' 84 # Read parameters from param.in file (1st command line argument) 85 dtype = [("floats", float), ("strings", "U100")] 86 data = np.genfromtxt(sys.argv[1], dtype=dtype, encoding=None, delimiter=" ") 87 88 data_floats = data["floats"] 89 data_strings = data["strings"] 90 91 num_vars = int(data_floats[0]) 92 parameter_values = np.array(data_floats[1 : 1 + num_vars]) 93 parameter_names = data_strings[1 : 1 + num_vars] 94 95 # Run directory is current dir, results dir is from Dakota 96 run_directory = os.getcwd() 97 results_file = sys.argv[2] 98 99 # Initialize and run ROM 100 parameter_sample = _create_parameter_dict(parameter_names, parameter_values) 101 model.populate_run_directory(run_directory, parameter_sample) 102 t0 = time.time() 103 model.run_model(run_directory, parameter_sample) 104 t = time.time() - t0 105 106 # Compute model QoI and save it to file 107 qoi = model.compute_qoi(run_directory, parameter_sample) 108 109 if multifidelity_flag: 110 assert qoi.size == 1, "For MF UQ, a scalar QoI is required" 111 112 if add_core_time_metadata: 113 qoi = np.append(qoi, t) # Cost metadata for Q 114 115 np.savetxt(results_file, qoi)
def
run_model_for_dakota( model: romtools.workflows.models.QoiModel, multifidelity_flag: bool = False, add_core_time_metadata: bool = False):
66def run_model_for_dakota( 67 model: QoiModel, 68 multifidelity_flag: bool = False, 69 add_core_time_metadata: bool = False, 70): 71 ''' 72 This function should be used in a driver script that will be called with 73 `python <driver.py> params.in results.out` 74 75 by Dakota. The parameter space is built by parsing params.in, 76 while the output QoI will be saved to results.out 77 78 Args: 79 model: rom-tools model instance (FOM or ROM) 80 multifidelity_flag: True if being used with Dakota's multifidelity (MF) tools 81 add_core_time_metadata: True if computational cost should be included in QoI file 82 Useful for MF UQ when model does not have a cost model. 83 84 ''' 85 # Read parameters from param.in file (1st command line argument) 86 dtype = [("floats", float), ("strings", "U100")] 87 data = np.genfromtxt(sys.argv[1], dtype=dtype, encoding=None, delimiter=" ") 88 89 data_floats = data["floats"] 90 data_strings = data["strings"] 91 92 num_vars = int(data_floats[0]) 93 parameter_values = np.array(data_floats[1 : 1 + num_vars]) 94 parameter_names = data_strings[1 : 1 + num_vars] 95 96 # Run directory is current dir, results dir is from Dakota 97 run_directory = os.getcwd() 98 results_file = sys.argv[2] 99 100 # Initialize and run ROM 101 parameter_sample = _create_parameter_dict(parameter_names, parameter_values) 102 model.populate_run_directory(run_directory, parameter_sample) 103 t0 = time.time() 104 model.run_model(run_directory, parameter_sample) 105 t = time.time() - t0 106 107 # Compute model QoI and save it to file 108 qoi = model.compute_qoi(run_directory, parameter_sample) 109 110 if multifidelity_flag: 111 assert qoi.size == 1, "For MF UQ, a scalar QoI is required" 112 113 if add_core_time_metadata: 114 qoi = np.append(qoi, t) # Cost metadata for Q 115 116 np.savetxt(results_file, qoi)
This function should be used in a driver script that will be called with
python <driver.py> params.in results.out
by Dakota. The parameter space is built by parsing params.in, while the output QoI will be saved to results.out
Arguments:
- model: rom-tools model instance (FOM or ROM)
- multifidelity_flag: True if being used with Dakota's multifidelity (MF) tools
- add_core_time_metadata: True if computational cost should be included in QoI file Useful for MF UQ when model does not have a cost model.