{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Example 1: Basic Mann Model Fit\n\nThis example demonstrates fitting the Mann model eddy lifetime function to the Kaimal one-point spectra and cross-spectra.\n\nFor reference, the Mann eddy lifetime function is given by\n\n\\begin{align}\\tau^{\\mathrm{Mann}}(k)=\\frac{(k L)^{-\\frac{2}{3}}}{\\sqrt{{ }_2 F_1\\left(1 / 3,17 / 6 ; 4 / 3 ;-(k L)^{-2}\\right)}}\\,.\\end{align}\n\nThis set of models it widely used for flat, homogeneous terrains.\n\n``drdmannturb`` can also be used directly to generate the corresponding 3D turbulence field, as demonstrated in Examples 8 and 9.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Import packages\n\nFirst, we import the packages needed for this example.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import torch\n\nfrom drdmannturb import EddyLifetimeType\nfrom drdmannturb.parameters import (\n LossParameters,\n NNParameters,\n PhysicalParameters,\n ProblemParameters,\n)\nfrom drdmannturb.spectra_fitting import CalibrationProblem, OnePointSpectraDataGenerator\n\ndevice = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n\nif torch.cuda.is_available():\n torch.set_default_tensor_type(\"torch.cuda.FloatTensor\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Set up physical parameters and domain. We perform the spectra fitting over the\n$k_1 z$ space $[10^{{-1}}, 10^2]$ with 20 points.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "zref = 40 # reference height\nustar = 1.773 # friction velocity\n\n# Intitial parameter guesses for fitting the Mann model\nL = 0.59 * zref # length scale\nGamma = 3.9 # time scale\nsigma = 3.2 * ustar**2.0 / zref ** (2.0 / 3.0) # magnitude (\u03c3 = \u03b1\u03f5^{2/3})\n\nprint(f\"Physical Parameters: {L,Gamma,sigma}\")\n\nk1 = torch.logspace(-1, 2, 20) / zref" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## ``CalibrationProblem`` Construction\nThe following cell defines the ``CalibrationProblem`` using default values\nfor the ``NNParameters`` and ``LossParameters`` dataclasses.\nNotice that ``EddyLifetimeType.MANN`` specifies the Mann model for the eddy lifetime\nfunction, $\\tau$, meaning no neural network is used in learning $\\tau$.\nThus, we only learn the parameters $L$, $\\Gamma$, and $\\sigma$.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pb = CalibrationProblem(\n nn_params=NNParameters(),\n prob_params=ProblemParameters(eddy_lifetime=EddyLifetimeType.MANN, nepochs=2),\n loss_params=LossParameters(),\n phys_params=PhysicalParameters(\n L=L, Gamma=Gamma, sigma=sigma, ustar=ustar, domain=k1\n ),\n device=device,\n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Data Generation\nWe now collect ``Data = (, )`` and specify the\nreference height (``zref``) to be used during calibration. Note that ``DataType.KAIMAL``\nis used by default.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "Data = OnePointSpectraDataGenerator(data_points=k1, zref=zref, ustar=ustar).Data" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The model is now fit to the provided spectra given in ``Data``.\n\nNotee that the Mann eddy lifetime function relies on evaluating a hypergeometric function,\nwhich only has a CPU implementation through ``Scipy``; cf. Example 7.\n\nHaving the necessary components, the model is \"calibrated\" (fit) to the provided spectra.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "optimal_parameters = pb.calibrate(data=Data)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We conclude by printing the optimized parameters and generating a plot showing the\nfit to the Kaimal spectra.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pb.print_calibrated_params()\npb.plot()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.16" } }, "nbformat": 4, "nbformat_minor": 0 }