{ "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", "See the {doc}`parameter-space API `.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "358fb709-9c81-45a3-a871-2549de121580", "metadata": {}, "outputs": [], "source": [ "# First, import the relevant modules.\n", "import numpy as np\n", "from romtools.workflows import ParameterSpace" ] }, { "cell_type": "code", "execution_count": null, "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 from ParameterSpace, which defines the required interface.\n", "class BasicParameterSpace(ParameterSpace):\n", "\n", " def __init__(self):\n", " self.var_names = ['c', 'nu']\n", " self.dim_ = 2\n", " self.lower_bounds_ = np.array([0.5, 1.e-3])\n", " self.upper_bounds_ = np.array([1.0, 1.e-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", " rng = np.random.default_rng(seed)\n", " return rng.uniform(\n", " self.lower_bounds_, self.upper_bounds_,\n", " size=(number_of_samples, self.dim_),\n", " )\n", "\n", "\n", "myBasicParameterSpace = BasicParameterSpace()\n", "samples = myBasicParameterSpace.generate_samples(3, seed=1)\n", "samples" ] }, { "cell_type": "code", "execution_count": null, "id": "217759fb-6ec9-4488-897b-e5b4db9f4b2c", "metadata": {}, "outputs": [], "source": [ "# What if a parameter space does not implement the full interface?\n", "# Here we intentionally omit get_names().\n", "class BadBasicParameterSpace(ParameterSpace):\n", "\n", " def __init__(self):\n", " self.var_names = ['x', 'y']\n", " self.dim_ = 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", " rng = np.random.default_rng(seed)\n", " return rng.normal(\n", " self.means_, self.stds_,\n", " size=(number_of_samples, self.dim_),\n", " )\n", "\n", "\n", "# Instantiating the incomplete implementation should fail because get_names\n", "# remains abstract. Catch the expected error so the tutorial is executable.\n", "try:\n", " BadBasicParameterSpace()\n", "except TypeError as exc:\n", " print(f'Expected error: {exc}')\n", "else:\n", " raise AssertionError('Expected BadBasicParameterSpace to remain abstract')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "name": "python", "version": "3" } }, "nbformat": 4, "nbformat_minor": 5 }