
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "auto_examples/01_basic_mann_parameters_fit.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_auto_examples_01_basic_mann_parameters_fit.py>`
        to download the full example code

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_auto_examples_01_basic_mann_parameters_fit.py:


===============================
Example 1: Basic Mann Model Fit
===============================

This example demonstrates fitting the Mann model eddy lifetime function to the Kaimal one-point spectra and cross-spectra.

For reference, the Mann eddy lifetime function is given by

.. math::

    \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)}}\,.

This set of models it widely used for flat, homogeneous terrains.

``drdmannturb`` can also be used directly to generate the corresponding 3D turbulence field, as demonstrated in Examples 8 and 9.

.. GENERATED FROM PYTHON SOURCE LINES 21-25

Import packages
---------------

First, we import the packages needed for this example.

.. GENERATED FROM PYTHON SOURCE LINES 25-42

.. code-block:: Python


    import torch

    from drdmannturb import EddyLifetimeType
    from drdmannturb.parameters import (
        LossParameters,
        NNParameters,
        PhysicalParameters,
        ProblemParameters,
    )
    from drdmannturb.spectra_fitting import CalibrationProblem, OnePointSpectraDataGenerator

    device = "cuda" if torch.cuda.is_available() else "cpu"

    if torch.cuda.is_available():
        torch.set_default_tensor_type("torch.cuda.FloatTensor")








.. GENERATED FROM PYTHON SOURCE LINES 43-45

Set up physical parameters and domain. We perform the spectra fitting over the
:math:`k_1 z` space :math:`[10^{{-1}}, 10^2]` with 20 points.

.. GENERATED FROM PYTHON SOURCE LINES 45-58

.. code-block:: Python


    zref = 40  # reference height
    ustar = 1.773  # friction velocity

    # Intitial parameter guesses for fitting the Mann model
    L = 0.59 * zref  # length scale
    Gamma = 3.9  # time scale
    sigma = 3.2 * ustar**2.0 / zref ** (2.0 / 3.0)  # magnitude (σ = αϵ^{2/3})

    print(f"Physical Parameters: {L,Gamma,sigma}")

    k1 = torch.logspace(-1, 2, 20) / zref





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    Physical Parameters: (23.599999999999998, 3.9, 0.8600574364289042)




.. GENERATED FROM PYTHON SOURCE LINES 59-66

``CalibrationProblem`` Construction
-----------------------------------
The following cell defines the ``CalibrationProblem`` using default values
for the ``NNParameters`` and ``LossParameters`` dataclasses.
Notice that ``EddyLifetimeType.MANN`` specifies the Mann model for the eddy lifetime
function, :math:`\tau`, meaning no neural network is used in learning :math:`\tau`.
Thus, we only learn the parameters :math:`L`, :math:`\Gamma`, and :math:`\sigma`.

.. GENERATED FROM PYTHON SOURCE LINES 66-76

.. code-block:: Python

    pb = CalibrationProblem(
        nn_params=NNParameters(),
        prob_params=ProblemParameters(eddy_lifetime=EddyLifetimeType.MANN, nepochs=2),
        loss_params=LossParameters(),
        phys_params=PhysicalParameters(
            L=L, Gamma=Gamma, sigma=sigma, ustar=ustar, domain=k1
        ),
        device=device,
    )








.. GENERATED FROM PYTHON SOURCE LINES 77-82

Data Generation
---------------
We now collect ``Data = (<data points>, <data values>)`` and specify the
reference height (``zref``) to be used during calibration. Note that ``DataType.KAIMAL``
is used by default.

.. GENERATED FROM PYTHON SOURCE LINES 82-85

.. code-block:: Python


    Data = OnePointSpectraDataGenerator(data_points=k1, zref=zref, ustar=ustar).Data








.. GENERATED FROM PYTHON SOURCE LINES 86-92

The model is now fit to the provided spectra given in ``Data``.

Notee that the Mann eddy lifetime function relies on evaluating a hypergeometric function,
which only has a CPU implementation through ``Scipy``; cf. Example 7.

Having the necessary components, the model is "calibrated" (fit) to the provided spectra.

.. GENERATED FROM PYTHON SOURCE LINES 92-95

.. code-block:: Python


    optimal_parameters = pb.calibrate(data=Data)





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    ========================================
    Initial loss: 0.036969418218857554
    ========================================

      0%|          | 0/2 [00:00<?, ?it/s]
     50%|█████     | 1/2 [00:03<00:03,  3.67s/it]
    100%|██████████| 2/2 [00:04<00:00,  2.20s/it]
    100%|██████████| 2/2 [00:04<00:00,  2.42s/it]
    ========================================
    Spectra fitting concluded with final loss: 0.025248721189996617




.. GENERATED FROM PYTHON SOURCE LINES 96-98

We conclude by printing the optimized parameters and generating a plot showing the
fit to the Kaimal spectra.

.. GENERATED FROM PYTHON SOURCE LINES 98-100

.. code-block:: Python

    pb.print_calibrated_params()
    pb.plot()



.. image-sg:: /auto_examples/images/sphx_glr_01_basic_mann_parameters_fit_001.png
   :alt: One-point spectra, Eddy lifetime
   :srcset: /auto_examples/images/sphx_glr_01_basic_mann_parameters_fit_001.png
   :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 .. code-block:: none

    ========================================
    Optimal calibrated L        :  27.2532 
    Optimal calibrated Γ        :   3.6762 
    Optimal calibrated αϵ^{2/3} :   0.7988 
    ========================================





.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 5.465 seconds)


.. _sphx_glr_download_auto_examples_01_basic_mann_parameters_fit.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: 01_basic_mann_parameters_fit.ipynb <01_basic_mann_parameters_fit.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: 01_basic_mann_parameters_fit.py <01_basic_mann_parameters_fit.py>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
