Basic parameter space tutorial#
In this tutorial you will learn the basics of constructing a parameter space that can be used in the romtools workflows. Here, we will create a simple 2D parameter space that will be used in downstream examples.
See the parameter-space API.
# First, import the relevant modules.
import numpy as np
from romtools.workflows import ParameterSpace
'''
Now, we will design our parameter space class.
This parameter space is designed to work with the 1D advection diffusion reaction equation:
c u_x - nu * u_xx = 1
where u is the state, c is the advection speed, and nu is the viscosity.
'''
# We inherit from ParameterSpace, which defines the required interface.
class BasicParameterSpace(ParameterSpace):
def __init__(self):
self.var_names = ['c', 'nu']
self.dim_ = 2
self.lower_bounds_ = np.array([0.5, 1.e-3])
self.upper_bounds_ = np.array([1.0, 1.e-1])
def get_names(self):
return self.var_names
def get_dimensionality(self):
return self.dim_
def generate_samples(self, number_of_samples, seed=None):
rng = np.random.default_rng(seed)
return rng.uniform(
self.lower_bounds_, self.upper_bounds_,
size=(number_of_samples, self.dim_),
)
myBasicParameterSpace = BasicParameterSpace()
samples = myBasicParameterSpace.generate_samples(3, seed=1)
samples
# What if a parameter space does not implement the full interface?
# Here we intentionally omit get_names().
class BadBasicParameterSpace(ParameterSpace):
def __init__(self):
self.var_names = ['x', 'y']
self.dim_ = 2
self.means_ = np.array([1, 2])
self.stds_ = np.array([0.1, 0.2])
def get_dimensionality(self):
return self.dim_
def generate_samples(self, number_of_samples: int, seed=None):
rng = np.random.default_rng(seed)
return rng.normal(
self.means_, self.stds_,
size=(number_of_samples, self.dim_),
)
# Instantiating the incomplete implementation should fail because get_names
# remains abstract. Catch the expected error so the tutorial is executable.
try:
BadBasicParameterSpace()
except TypeError as exc:
print(f'Expected error: {exc}')
else:
raise AssertionError('Expected BadBasicParameterSpace to remain abstract')