{ "cells": [ { "cell_type": "markdown", "id": "f389f465-1007-443d-873b-d27bfa74cbc7", "metadata": {}, "source": [ "# Basic parameter space tutorial\n", "\n", "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.\n", "\n", "The API for the parameter space is documented here: https://pressio.github.io/rom-tools-and-workflows/romtools/workflows/parameter_spaces.html\n" ] }, { "cell_type": "code", "execution_count": 6, "id": "358fb709-9c81-45a3-a871-2549de121580", "metadata": {}, "outputs": [], "source": [ "#First, let's import the relavant modules:\n", "import romtools\n", "import numpy as np\n", "from romtools.workflows import ParameterSpace" ] }, { "cell_type": "code", "execution_count": 7, "id": "b1ffa4be-246c-44a9-b486-f517b9f7acb9", "metadata": {}, "outputs": [], "source": [ "'''\n", "Now, we will design our parameter space class\n", "This parameter space is designed to work with the 1D advection diffusion reaction equation:\n", "\n", "c u_x - nu * u_xx = 1\n", "\n", "where u is the state, c, is the advection speed, and nu is the viscosity.\n", "'''\n", "\n", "#We inherit the class from ParameterSpace, which gives the abstract base template \n", "class BasicParameterSpace(ParameterSpace):\n", "\n", " def __init__(self):\n", " #We will have two variables, x and y\n", " self.var_names = ['c','nu']\n", "\n", " # The dimension of the parameter space is 2 (c and nu)\n", " self.dim_ = 2\n", "\n", " # In this example we will consider the two variables to be uncorrelated, and we will assign them uniform distributions\n", " self.lower_bounds_ = np.array([0.5,1.e-3])\n", " self.upper_bounds_ = np.array([1e-3,1e-1])\n", " \n", " def get_names(self):\n", " return self.var_names\n", "\n", " def get_dimensionality(self):\n", " return self.dim_\n", "\n", " def generate_samples(self, number_of_samples, seed=None):\n", " samples = np.random.uniform(self.lower_bounds_,self.upper_bounds_,size=(number_of_samples,self.dim_))\n", " return samples\n", "\n", "\n", "#Now, let's instatiate the parameter space\n", "if __name__ == \"__main__\":\n", " myBasicParameterSpace = BasicParameterSpace()\n", "\n", "#That's it! We now have a parameter space that can be used for, e.g., basic sampling" ] }, { "cell_type": "code", "execution_count": 5, "id": "217759fb-6ec9-4488-897b-e5b4db9f4b2c", "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "Can't instantiate abstract class BadBasicParameterSpace without an implementation for abstract method 'get_names'", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[5], line 25\u001b[0m\n\u001b[1;32m 23\u001b[0m \u001b[38;5;66;03m#Python will throw an error when we try to instatiate\u001b[39;00m\n\u001b[1;32m 24\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;18m__name__\u001b[39m \u001b[38;5;241m==\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m__main__\u001b[39m\u001b[38;5;124m\"\u001b[39m:\n\u001b[0;32m---> 25\u001b[0m myBadBasicParameterSpace \u001b[38;5;241m=\u001b[39m BadBasicParameterSpace()\n", "\u001b[0;31mTypeError\u001b[0m: Can't instantiate abstract class BadBasicParameterSpace without an implementation for abstract method 'get_names'" ] } ], "source": [ "#What if we tried to create a parameter space that didn't meet the interface of the ParameterSpace\n", "# As an example, let's say we didn't include the \"get_names\" method\n", "class BadBasicParameterSpace(ParameterSpace):\n", "\n", " def __init__(self):\n", " #We will have two variables, x and y\n", " self.var_names = ['x','y']\n", "\n", " # The dimension of the parameter space is 2 (x and y)\n", " self.dim_ = 2\n", "\n", " # In this example we will consider uncorrelated variables w/ mean [1,2] and standard deviation [0.1,0.2]\n", " self.means_ = np.array([1,2])\n", " self.stds_ = np.array([0.1,0.2]) \n", "\n", " def get_dimensionality(self):\n", " return self.dim_\n", "\n", " def generate_samples(self, number_of_samples: int, seed=None):\n", " samples = np.random.normal(self.means_,self.stds_,size=(number_of_samples,self.dim_))\n", " return samples\n", "\n", "#Python will throw an error when we try to instatiate\n", "if __name__ == \"__main__\":\n", " myBadBasicParameterSpace = BadBasicParameterSpace()" ] }, { "cell_type": "code", "execution_count": null, "id": "7455293e-1975-4c6e-8254-cb50eaae7345", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.4" } }, "nbformat": 4, "nbformat_minor": 5 }