{ "cells": [ { "cell_type": "markdown", "id": "f389f465-1007-443d-873b-d27bfa74cbc7", "metadata": {}, "source": [ "# External QoiModel tutorial\n", "\n", "In this tutorial you will learn the basics of the QoiModel interface. The QoIModel interface is almost identical to the model interface,\n", "but has an additional method called compute_qoi() where it returns a (usually scalar) quantity of interest. This type of model is useful for various workflows, such as training a ROM to a desired tolerance on a specified QoI. The API for the model interface is provided here:\n", "\n", "https://pressio.github.io/rom-tools-and-workflows/romtools/workflows/models.html\n" ] }, { "cell_type": "code", "execution_count": 5, "id": "358fb709-9c81-45a3-a871-2549de121580", "metadata": {}, "outputs": [], "source": [ "#First, let's import the relavant modules:\n", "import romtools\n", "import os\n", "import numpy as np\n", "from matplotlib import pyplot as plt\n", "from romtools.workflows import models" ] }, { "cell_type": "code", "execution_count": 6, "id": "b1ffa4be-246c-44a9-b486-f517b9f7acb9", "metadata": {}, "outputs": [], "source": [ "# As a starting point, we will use the model created in the external_model tutorial\n", "from ipynb.fs.full.external_model import adrExternalRomToolsModel" ] }, { "cell_type": "code", "execution_count": 11, "id": "217759fb-6ec9-4488-897b-e5b4db9f4b2c", "metadata": {}, "outputs": [], "source": [ "#Now, we will create a QoI model. As a QoI, we will take the gradient of the state at the right of the domain\n", "class adrExternalRomToolsQoiModel(adrExternalRomToolsModel):\n", " # Our class will inherit all the methods from adrExternalRomToolsModel\n", " \n", " def compute_qoi(self, run_directory: str, parameter_sample: dict):\n", " # Note that compute_qoi is always called after run_model\n", " solution = np.load(run_directory + '/solution.npz')\n", " u = solution['u']\n", " x = solution['x']\n", " dx = x[1] - x[0] #we use a uniform grid\n", " ux_at_right_edge = (0. - u[-1])/dx\n", " return ux_at_right_edge\n", "\n", "#That's it!" ] }, { "cell_type": "code", "execution_count": 12, "id": "dbf42fd4-340f-4074-9682-69a9df424b26", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1.28125738e-14 5.94704074e-02 1.18467441e-01 1.76917136e-01\n", " 2.34733971e-01 2.91819061e-01 3.48058072e-01 4.03318803e-01\n", " 4.57448398e-01 5.10270118e-01 5.61579606e-01 6.11140577e-01\n", " 6.58679825e-01 7.03881455e-01 7.46380215e-01 7.85753780e-01\n", " 8.21513841e-01 8.53095786e-01 8.79846785e-01 9.01012003e-01\n", " 9.15718660e-01 9.22957608e-01 9.21562017e-01 9.10182740e-01\n", " 8.87259825e-01 8.50989580e-01 7.99286485e-01 7.29739155e-01\n", " 6.39559430e-01 5.25523499e-01 3.83903828e-01 2.10390458e-01\n", " 0.00000000e+00] [0. 0.03125 0.0625 0.09375 0.125 0.15625 0.1875 0.21875 0.25\n", " 0.28125 0.3125 0.34375 0.375 0.40625 0.4375 0.46875 0.5 0.53125\n", " 0.5625 0.59375 0.625 0.65625 0.6875 0.71875 0.75 0.78125 0.8125\n", " 0.84375 0.875 0.90625 0.9375 0.96875 1. ]\n", "The QoI is -6.7324946711637725\n" ] } ], "source": [ "#Let's do an example run of the model similar to how it will be used in a workflow.\n", "\n", "if __name__==\"__main__\":\n", " # Instantiate our model\n", " myModel = adrExternalRomToolsQoiModel()\n", "\n", " #First, let's make a dictionary that is a stand in for the parameter space\n", " parameter_sample = {}\n", " parameter_sample['c'] = 0.5\n", " parameter_sample['nu'] = 1e-1\n", "\n", " #Now, let's populate our run directory. In this example, we will just use the current run directory.\n", " #In a full workflow, romtools will manage creating directories\n", " myModel.populate_run_directory(os.getcwd(),parameter_sample)\n", "\n", " #Now, let's run the model\n", " run_dir = os.getcwd()\n", "\n", " myModel.run_model(run_dir,parameter_sample)\n", "\n", " qoi = myModel.compute_qoi(run_dir,parameter_sample)\n", "\n", " print('The QoI is ' + str(qoi))\n", " #We will finish by cleaning up the files we created\n", " os.system('rm params.dat')\n", " os.system('rm solution.npz')" ] }, { "cell_type": "code", "execution_count": null, "id": "80fce597-9ea5-40dc-a500-703ae98d6acf", "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.9.7" } }, "nbformat": 4, "nbformat_minor": 5 }