{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Example 8: Fluctuation Field Generation\n\nThis example demonstrates the utilities for generating synthetic turbulence, which can be either\nfrom a pre-trained DRD model, or based on some well-known spectra models. ``DRDMannTurb``\nprovides several utilities for plotting the resulting fields through Plotly, which can be done\nin several contexts as well as utilities for saving to VTK for downstream analysis in, e.g.,\nParaView.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. centered::\n This example may take a few seconds to load. Please be patient if using\n Plotly, as it requires some time to render 3D graphics.\n\n\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Import packages\n\nFirst, we import the packages we need for this example.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "from pathlib import Path\n\nimport numpy as np\nimport torch\n\nfrom drdmannturb.fluctuation_generation import (\n plot_velocity_components, # utility function for plotting each velocity component in the field, not used in this example\n)\nfrom drdmannturb.fluctuation_generation import (\n GenerateFluctuationField,\n plot_velocity_magnitude,\n)\n\npath = Path().resolve()\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": [ "## Setting Physical Parameters\nHere, we set the physical parameters of the environment in which the synthetic wind field is generated:\nthe friction velocity $u_\\mathrm{red} = 11.4$ roughness height $z_0=0.02$ and reference height\nof $90$. The physical domain is determined by dimensions in 3D as well as the discretization\nsize (grid levels) in each dimension.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "z0 = 0.02\nzref = 90\nuref = 11.4\nustar = uref * 0.41 / np.log(zref / z0)\nplexp = 0.2 # power law exponent\nwindprofiletype = \"PL\" # choosing power law, use log with \"LOG\" here instead\n\nL = 0.593 * zref # length scale\nGamma = 3.89 # time scale\nsigma = 0.052 # magnitude (\u03c3 = \u03b1\u03f5^{2/3})\n\nLx = 720\nLy = 64\nLz = 64\n\nnBlocks = 2\ngrid_dimensions = np.array([Lx / 4, Ly, Lz])\n\ngrid_levels = np.array([6, 4, 4])\n\nseed = None" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Generating Fluctuation Field from Mann Model\nFluctuation fields are generated block-by-block, rather than over the domain entirely.\nPlease see section V, B of the original DRD paper for further discussion. Here, we will use 4 blocks.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "Type_Model = \"Mann\" ### 'Mann', 'VK', 'DRD'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Physical Parameters\nThe Mann model requires three parameters, length scale, time scale, and spectrum amplitude scale,\nwhich are defined above\n\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "gen_mann = GenerateFluctuationField(\n ustar,\n zref,\n grid_dimensions,\n grid_levels,\n length_scale=L,\n time_scale=Gamma,\n energy_spectrum_scale=sigma,\n model=Type_Model,\n seed=seed,\n)\n\nfluctuation_field_mann = gen_mann.generate(\n nBlocks, zref, uref, z0, windprofiletype, plexp\n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Adding the mean velocity profile\nThe mean velocity profile follows the power law profile\n\n\\begin{align}\\left\\langle U_1(z)\\right\\rangle= U_{\\text{ref}}\\left( \\frac{z}{z_{\\text{ref}}} \\right)^\\alpha\\end{align}\n\nwhere $U_{\\text{ref}}$ is the reference velocity and $z_{\\text{ref}}$ is the reference height.\n\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "spacing = tuple(grid_dimensions / (2.0**grid_levels + 1))\n\nfig_magnitude_mann = plot_velocity_magnitude(\n spacing, fluctuation_field_mann, transparent=True\n)\n\n# this is a Plotly figure, which can be visualized with the ``.show()`` method in different contexts. While these utilities\n# may be useful for quick visualization, we recommend using Paraview to visualize higher resolution output. We will cover\n# saving to a portable VTK format further in this example.\n\nfig_magnitude_mann # .show(\"browser\"), or for specific browser, use .show(\"firefox\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Evaluating Divergence Properties and Plotting\n``DRDMannTurb`` provides utilities for computing the divergence of the resulting fluctuation field as well as\nvisualizing results. At the continuum level, the DRD model should yield an approximately divergence-free fluctuation\nfield, which we observe to within a reasonable tolerance. Also, the divergence is expected to decrease as the\n resolution of the fluctuation field is improved.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "spacing = tuple(grid_dimensions / (2.0**grid_levels + 1))\n\ngen_mann.evaluate_divergence(spacing, fluctuation_field_mann).max()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Saving Generated Fluctuation Field as VTK\nFor higher resolution fluctuation fields, we suggest using Paraview. To transfer the generated data\nfrom our package, we provide the ``.save_to_vtk()`` method.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "filename = str(\n path / \"./outputs/IEC_simple\"\n if path.name == \"examples\"\n else path / \"./outputs/IEC_simple\"\n)\n\ngen_mann.save_to_vtk(filename)" ] } ], "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 }