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.
The API for the parameter space is documented here: https://pressio.github.io/rom-tools-and-workflows/romtools/workflows/parameter_spaces.html
#First, let's import the relavant modules:
import romtools
import numpy as np
from romtools.workflows import ParameterSpace
module 'mpi4py' is not installed
'''
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 the class from ParameterSpace, which gives the abstract base template
class BasicParameterSpace(ParameterSpace):
def __init__(self):
#We will have two variables, x and y
self.var_names = ['c','nu']
# The dimension of the parameter space is 2 (c and nu)
self.dim_ = 2
# In this example we will consider the two variables to be uncorrelated, and we will assign them uniform distributions
self.lower_bounds_ = np.array([0.5,1.e-3])
self.upper_bounds_ = np.array([1e-3,1e-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):
samples = np.random.uniform(self.lower_bounds_,self.upper_bounds_,size=(number_of_samples,self.dim_))
return samples
#Now, let's instatiate the parameter space
if __name__ == "__main__":
myBasicParameterSpace = BasicParameterSpace()
#That's it! We now have a parameter space that can be used for, e.g., basic sampling
#What if we tried to create a parameter space that didn't meet the interface of the ParameterSpace
# As an example, let's say we didn't include the "get_names" method
class BadBasicParameterSpace(ParameterSpace):
def __init__(self):
#We will have two variables, x and y
self.var_names = ['x','y']
# The dimension of the parameter space is 2 (x and y)
self.dim_ = 2
# In this example we will consider uncorrelated variables w/ mean [1,2] and standard deviation [0.1,0.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):
samples = np.random.normal(self.means_,self.stds_,size=(number_of_samples,self.dim_))
return samples
#Python will throw an error when we try to instatiate
if __name__ == "__main__":
myBadBasicParameterSpace = BadBasicParameterSpace()
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[3], line 25
23 #Python will throw an error when we try to instatiate
24 if __name__ == "__main__":
---> 25 myBadBasicParameterSpace = BadBasicParameterSpace()
TypeError: Can't instantiate abstract class BadBasicParameterSpace with abstract method get_names