{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Generating roughness elements" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The standard use case for generating the roughness elements is to use a configuration file containing the necessary parameters.\n", "\n", "These parameters account for the number of elements being generated in each direction and also the offset and spacing applied to each row." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'N_elements_x': 7,\n", " 'N_elements_y': 5,\n", " 'element_params': {'height': 5.0, 'width': 5.0},\n", " 'spacing_params': {'line_offset': 10.0,\n", " 'offset_direction': 'x',\n", " 'spacing': (20.0, 20.0)}}\n" ] } ], "source": [ "import pathlib\n", "import pprint\n", "from cfdmod.use_cases.roughness_gen import GenerationParams\n", "\n", "pp = pprint.PrettyPrinter()\n", "\n", "cfg_file = pathlib.Path(\"./fixtures/tests/roughness_gen/roughness_params.yaml\")\n", "cfg = GenerationParams.from_file(cfg_file)\n", "\n", "pp.pprint(cfg.model_dump())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "It's also possible to generate the configurations through code.\n", "This can be done by creating an instance of `GenerationParams` and using it for the next steps.\n", "\n", "The example above can be instanciated as" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from cfdmod.use_cases.roughness_gen import SpacingParams, ElementParams\n", "\n", "element_params = ElementParams(height=5, width=5)\n", "\n", "spacing_params = SpacingParams(spacing=(2, 2), line_offset=5, offset_direction=\"x\")\n", "\n", "manual_cfg = GenerationParams(\n", " N_elements_x=10, N_elements_y=10, element_params=element_params, spacing_params=spacing_params\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Build element and linear patterns\n", "\n", "In order to build all the elements, linear patterns are applied.\n", "The first one applies to create a row in the offset direction.\n", "The second one applies to replicate the original row and apply an offset to odd index rows." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Single element: Vertices count: 6 Triangles count: 2\n", "Single line Vertices count: 42 Triangles count: 14\n", "Replicated lines Vertices count: 210 Triangles count: 70\n" ] } ], "source": [ "from cfdmod.use_cases.roughness_gen import build_single_element, linear_pattern\n", "\n", "triangles, normals = build_single_element(cfg.element_params)\n", "\n", "single_line_triangles, single_line_normals = linear_pattern(\n", " triangles,\n", " normals,\n", " direction=cfg.spacing_params.offset_direction,\n", " n_repeats=cfg.single_line_elements,\n", " spacing_value=cfg.single_line_spacing,\n", ")\n", "\n", "full_triangles, full_normals = linear_pattern(\n", " single_line_triangles,\n", " single_line_normals,\n", " direction=cfg.perpendicular_direction,\n", " n_repeats=cfg.multi_line_elements,\n", " spacing_value=cfg.multi_line_spacing,\n", " offset_value=cfg.spacing_params.line_offset,\n", ")\n", "\n", "print(\n", " \"Single element:\",\n", " f\"Vertices count: {triangles.shape[0] * triangles.shape[1]}\",\n", " f\"Triangles count: {len(triangles)}\",\n", ")\n", "print(\n", " \"Single line\",\n", " f\"Vertices count: {single_line_triangles.shape[0] * single_line_triangles.shape[1]}\",\n", " f\"Triangles count: {len(single_line_triangles)}\",\n", ")\n", "print(\n", " \"Replicated lines\",\n", " f\"Vertices count: {full_triangles.shape[0] * full_triangles.shape[1]}\",\n", " f\"Triangles count: {len(full_triangles)}\",\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Export generated geometry\n", "\n", "In order to use the generated geometry, the user must export the triangles and normals, using the geometry API and the STL format (triangles, normals)." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "from cfdmod.api.geometry.STL import export_stl\n", "import pathlib\n", "\n", "output_path = pathlib.Path(\"./output/roughness_gen\")\n", "\n", "export_stl(output_path / \"roughness_elements.stl\", full_triangles, full_normals)" ] } ], "metadata": { "kernelspec": { "display_name": "cfdmod-XMkUSlb0-py3.10", "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.10.11" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }