From 4ec94c97879aafef15f7663135745e4ba61e62cf Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Mon, 17 May 2021 00:15:33 +0200 Subject: Extract first public LiterateLB version --- CMakeLists.txt | 40 + assets.cmake | 42 + default.nix | 152 + lbm.org | 5817 +++++++++++++++++++++++ org.css | 213 + tangle/LLBM/base.h | 6 + tangle/LLBM/boundary.h | 13 + tangle/LLBM/bulk.h | 3 + tangle/LLBM/call_tag.h | 12 + tangle/LLBM/descriptor.h | 298 ++ tangle/LLBM/kernel/bounce_back.h | 44 + tangle/LLBM/kernel/bounce_back_moving_wall.h | 44 + tangle/LLBM/kernel/bouzidi.h | 39 + tangle/LLBM/kernel/collect_curl.h | 44 + tangle/LLBM/kernel/collect_moments.h | 45 + tangle/LLBM/kernel/collect_q_criterion.h | 104 + tangle/LLBM/kernel/collect_shear_layer_normal.h | 139 + tangle/LLBM/kernel/collect_streamlines.h | 48 + tangle/LLBM/kernel/collect_velocity_norm.h | 61 + tangle/LLBM/kernel/collide.h | 131 + tangle/LLBM/kernel/equilibrium_density_wall.h | 226 + tangle/LLBM/kernel/equilibrium_velocity_wall.h | 222 + tangle/LLBM/kernel/executor.h | 171 + tangle/LLBM/kernel/free_slip.h | 82 + tangle/LLBM/kernel/initialize.h | 44 + tangle/LLBM/kernel/propagate.h | 20 + tangle/LLBM/kernel/smagorinsky_collide.h | 183 + tangle/LLBM/lattice.h | 131 + tangle/LLBM/materials.h | 108 + tangle/LLBM/memory.h | 134 + tangle/LLBM/operator.h | 27 + tangle/LLBM/propagate.h | 111 + tangle/LLBM/sdf.h | 75 + tangle/LLBM/sdf_boundary.h | 114 + tangle/LLBM/volumetric.h | 134 + tangle/LLBM/wall.h | 4 + tangle/asset/noise/blue_0.png | Bin 0 -> 3763 bytes tangle/asset/noise/blue_1.png | Bin 0 -> 3775 bytes tangle/asset/noise/blue_2.png | Bin 0 -> 3752 bytes tangle/asset/noise/blue_3.png | Bin 0 -> 3789 bytes tangle/asset/noise/blue_4.png | Bin 0 -> 3754 bytes tangle/asset/palette/4wave_ROTB.png | Bin 0 -> 1572 bytes tangle/asset/palette/4wave_equal.png | Bin 0 -> 1485 bytes tangle/asset/palette/5wave_cool.png | Bin 0 -> 1601 bytes tangle/asset/palette/autumn.png | Bin 0 -> 827 bytes tangle/asset/palette/blue.png | Bin 0 -> 1223 bytes tangle/asset/palette/blue_orange.png | Bin 0 -> 1457 bytes tangle/asset/palette/green_brown.png | Bin 0 -> 1269 bytes tangle/asset/palette/orange.png | Bin 0 -> 1165 bytes tangle/asset/shader/blur.frag | 20 + tangle/benchmark-ldc.cu | 95 + tangle/channel-with-sphere.cu | 85 + tangle/ldc-2d.cu | 82 + tangle/ldc-3d.cu | 58 + tangle/magnus.cu | 116 + tangle/nozzle.cu | 73 + tangle/sampler/curl_norm.h | 77 + tangle/sampler/q_criterion.h | 90 + tangle/sampler/sampler.h | 32 + tangle/sampler/shear_layer.h | 67 + tangle/sampler/velocity_norm.h | 79 + tangle/taylor-couette.cu | 75 + tangle/tmp/noise_overview.png | Bin 0 -> 14996 bytes tangle/tmp/test_noise.png | Bin 0 -> 26571 bytes tangle/util/camera.h | 86 + tangle/util/colormap.h | 38 + tangle/util/noise.h | 39 + tangle/util/render_window.h | 95 + tangle/util/texture.h | 25 + tangle/util/timer.h | 19 + tangle/util/volumetric_example.h | 121 + 71 files changed, 10453 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 assets.cmake create mode 100644 default.nix create mode 100644 lbm.org create mode 100644 org.css create mode 100644 tangle/LLBM/base.h create mode 100644 tangle/LLBM/boundary.h create mode 100644 tangle/LLBM/bulk.h create mode 100644 tangle/LLBM/call_tag.h create mode 100644 tangle/LLBM/descriptor.h create mode 100644 tangle/LLBM/kernel/bounce_back.h create mode 100644 tangle/LLBM/kernel/bounce_back_moving_wall.h create mode 100644 tangle/LLBM/kernel/bouzidi.h create mode 100644 tangle/LLBM/kernel/collect_curl.h create mode 100644 tangle/LLBM/kernel/collect_moments.h create mode 100644 tangle/LLBM/kernel/collect_q_criterion.h create mode 100644 tangle/LLBM/kernel/collect_shear_layer_normal.h create mode 100644 tangle/LLBM/kernel/collect_streamlines.h create mode 100644 tangle/LLBM/kernel/collect_velocity_norm.h create mode 100644 tangle/LLBM/kernel/collide.h create mode 100644 tangle/LLBM/kernel/equilibrium_density_wall.h create mode 100644 tangle/LLBM/kernel/equilibrium_velocity_wall.h create mode 100644 tangle/LLBM/kernel/executor.h create mode 100644 tangle/LLBM/kernel/free_slip.h create mode 100644 tangle/LLBM/kernel/initialize.h create mode 100644 tangle/LLBM/kernel/propagate.h create mode 100644 tangle/LLBM/kernel/smagorinsky_collide.h create mode 100644 tangle/LLBM/lattice.h create mode 100644 tangle/LLBM/materials.h create mode 100644 tangle/LLBM/memory.h create mode 100644 tangle/LLBM/operator.h create mode 100644 tangle/LLBM/propagate.h create mode 100644 tangle/LLBM/sdf.h create mode 100644 tangle/LLBM/sdf_boundary.h create mode 100644 tangle/LLBM/volumetric.h create mode 100644 tangle/LLBM/wall.h create mode 100644 tangle/asset/noise/blue_0.png create mode 100644 tangle/asset/noise/blue_1.png create mode 100644 tangle/asset/noise/blue_2.png create mode 100644 tangle/asset/noise/blue_3.png create mode 100644 tangle/asset/noise/blue_4.png create mode 100644 tangle/asset/palette/4wave_ROTB.png create mode 100644 tangle/asset/palette/4wave_equal.png create mode 100644 tangle/asset/palette/5wave_cool.png create mode 100644 tangle/asset/palette/autumn.png create mode 100644 tangle/asset/palette/blue.png create mode 100644 tangle/asset/palette/blue_orange.png create mode 100644 tangle/asset/palette/green_brown.png create mode 100644 tangle/asset/palette/orange.png create mode 100644 tangle/asset/shader/blur.frag create mode 100644 tangle/benchmark-ldc.cu create mode 100644 tangle/channel-with-sphere.cu create mode 100644 tangle/ldc-2d.cu create mode 100644 tangle/ldc-3d.cu create mode 100644 tangle/magnus.cu create mode 100644 tangle/nozzle.cu create mode 100644 tangle/sampler/curl_norm.h create mode 100644 tangle/sampler/q_criterion.h create mode 100644 tangle/sampler/sampler.h create mode 100644 tangle/sampler/shear_layer.h create mode 100644 tangle/sampler/velocity_norm.h create mode 100644 tangle/taylor-couette.cu create mode 100644 tangle/tmp/noise_overview.png create mode 100644 tangle/tmp/test_noise.png create mode 100644 tangle/util/camera.h create mode 100644 tangle/util/colormap.h create mode 100644 tangle/util/noise.h create mode 100644 tangle/util/render_window.h create mode 100644 tangle/util/texture.h create mode 100644 tangle/util/timer.h create mode 100644 tangle/util/volumetric_example.h diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..abe1087 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,40 @@ +cmake_minimum_required(VERSION 3.10) +project(literatelb LANGUAGES CXX CUDA) + +find_package(CUDAToolkit REQUIRED) +find_package(SFML 2.5 COMPONENTS graphics window system REQUIRED) +find_package(ImGui-SFML REQUIRED) + +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE Release) +endif() + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} --expt-relaxed-constexpr --default-stream per-thread --extended-lambda") +set(CMAKE_CUDA_ARCHITECTURES 50) + +include_directories( + ${CMAKE_CURRENT_SOURCE_DIR}/tangle + ${CMAKE_CURRENT_BINARY_DIR}) + +include(assets.cmake) +serialize_assets(${CMAKE_CURRENT_SOURCE_DIR}/tangle/asset + ${CMAKE_CURRENT_BINARY_DIR}/assets.cc + ${CMAKE_CURRENT_BINARY_DIR}/assets.h) +add_library(assets ${CMAKE_CURRENT_BINARY_DIR}/assets.cc) + +link_libraries( + sfml-graphics + sfml-window + sfml-system + ImGui-SFML::ImGui-SFML + CUDA::cuda_driver + assets) + +file(GLOB EXAMPLES ${CMAKE_CURRENT_SOURCE_DIR}/tangle/*.cu) +foreach(examplefile ${EXAMPLES}) + get_filename_component(examplename ${examplefile} NAME_WE) + add_executable(${examplename} ${examplefile}) +endforeach() diff --git a/assets.cmake b/assets.cmake new file mode 100644 index 0000000..11868f8 --- /dev/null +++ b/assets.cmake @@ -0,0 +1,42 @@ +function(serialize_folder name dir data header) + file(APPEND ${header} "namespace ${name} {\n") + file(APPEND ${data} "namespace ${name} {\n") + file(GLOB files ${dir}/*) + list(LENGTH files file_count) + foreach(file ${files}) + string(REGEX MATCH "([^/]+)$" filename ${file}) + string(REGEX REPLACE "\\.| |-" "_" filename ${filename}) + file(READ ${file} filedata HEX) + string(REGEX REPLACE "([0-9a-f][0-9a-f])" "0x\\1," filedata ${filedata}) + file(APPEND ${data} "const unsigned char file_${filename}[] = {${filedata}};\n") + file(APPEND ${data} "const unsigned file_${filename}_size = sizeof(file_${filename});\n") + file(APPEND ${header} "extern const unsigned char file_${filename}[];\n") + file(APPEND ${header} "extern const unsigned file_${filename}_size;\n") + endforeach() + file(APPEND ${data} "}\n") + file(APPEND ${header} "const File files[] {\n") + foreach(file ${files}) + string(REGEX MATCH "([^/]+)$" filename ${file}) + string(REGEX REPLACE "\\.| |-" "_" flat_filename ${filename}) + file(APPEND ${header} "File{\"${filename}\", file_${flat_filename}, file_${flat_filename}_size},\n") + endforeach() + file(APPEND ${header} "};\n") + file(APPEND ${header} "const unsigned file_count = ${file_count};\n") + file(APPEND ${header} "}\n") +endfunction() + +function(serialize_assets dir data header) + file(WRITE ${data} "") + file(APPEND ${data} "#include \"${header}\"\n") + file(APPEND ${data} "namespace assets {\n") + file(WRITE ${header} "") + file(APPEND ${header} "#pragma once\n") + file(APPEND ${header} "#include \n") + file(APPEND ${header} "namespace assets {\n") + file(APPEND ${header} "struct File { const std::string name; const unsigned char* data; const unsigned size; };\n") + serialize_folder("palette" ${dir}/palette ${data} ${header}) + serialize_folder("noise" ${dir}/noise ${data} ${header}) + serialize_folder("shader" ${dir}/shader ${data} ${header}) + file(APPEND ${data} "}\n") + file(APPEND ${header} "}\n") +endfunction() diff --git a/default.nix b/default.nix new file mode 100644 index 0000000..6d19d77 --- /dev/null +++ b/default.nix @@ -0,0 +1,152 @@ +{ pkgs ? import { + overlays = [ + (import (builtins.fetchTarball { + url = https://github.com/nix-community/emacs-overlay/archive/0bb3c36bb8cddd92b788d8ce474c39475148d5e2.tar.gz; + })) + ]; +}, ... }: + +let + cuda-samples-common-headers = pkgs.stdenv.mkDerivation rec { + name = "cuda-samples-common-headers"; + version = "11.1"; + + src = pkgs.fetchFromGitHub { + owner = "NVIDIA"; + repo = "cuda-samples"; + rev = "v${version}"; + sha256 = "1kjixk50i8y1bkiwbdn5lkv342crvkmbvy1xl5j3lsa1ica21kwh"; + }; + + phases = [ "installPhase" ]; + + installPhase = '' + mkdir -p $out/include/cuda-samples/Common + cp -r $src/Common/* $out/include/cuda-samples/Common + ''; + }; + + imgui-sfml = pkgs.stdenv.mkDerivation rec { + name = "imgui-sfml"; + version = "2.1"; + + src = pkgs.fetchFromGitHub { + owner = "eliasdaler"; + repo = "imgui-sfml"; + rev = "v${version}"; + sha256 = "1g8gqly156miv12ajapnhmxfcv9i3fqhdmdy45gmdw47kh8ly5zj"; + }; + + buildInputs = with pkgs; [ + cmake + ]; + + propagatedBuildInputs = with pkgs; [ + libGL + sfml + xorg.libX11 + ]; + + cmakeFlags = let + imgui_src = pkgs.fetchFromGitHub { + owner = "ocornut"; + repo = "imgui"; + rev = "v1.68"; + sha256 = "0a7b4fljybvpls84rqzsb2p4r89ic2g6w2m9h0209xlhm4k0x7qr"; + }; + in [ + "-DIMGUI_DIR=${imgui_src}" + "-DBUILD_SHARED_LIBS=ON" + "-DIMGUI_SFML_BUILD_EXAMPLES=ON" + ]; + }; + +in pkgs.stdenv.mkDerivation rec { + name = "LiterateLB"; + + src = ./.; + + nativeBuildInputs = with pkgs; [ + cmake + addOpenGLRunpath + ]; + + buildInputs = let + local-python = pkgs.python3.withPackages (python-packages: with python-packages; [ + sympy + numpy + Mako + matplotlib + scipy + ]); + + in with pkgs; [ + local-python + cudatoolkit_11 + cuda-samples-common-headers + linuxPackages.nvidia_x11 + libGL + sfml + imgui-sfml + ]; + + phases = [ "buildPhase" "installPhase" ]; + + buildPhase = let + tangle-el = pkgs.writeTextFile { + name = "tangle.el"; + text = '' + (toggle-debug-on-error) + (require 'org) + (org-babel-do-load-languages 'org-babel-load-languages '((python . t))) + (setq org-confirm-babel-evaluate nil) + (setq org-babel-confirm-evaluate-answer-no t) + (find-file "lbm.org") + (setq org-babel-default-header-args + (cons '(:result-params . ("none")) + (assq-delete-all :result-params org-babel-default-header-args))) + (org-babel-execute-buffer) + (setq org-babel-default-header-args + (cons '(:result-params . ("output")) + (assq-delete-all :result-params org-babel-default-header-args))) + (org-babel-tangle) + ''; + }; + + in '' + cp $src/lbm.org . + mkdir -p tangle/asset + cp -r $src/tangle/asset/noise tangle/asset + cp -r $src/tangle/asset/palette tangle/asset + ${pkgs.emacsUnstable-nox}/bin/emacs --no-init-file --script ${tangle-el} + cp $src/CMakeLists.txt . + cp $src/assets.cmake . + mkdir build + pushd build + cmake .. + make + popd + ''; + + installPhase = '' + mkdir -p $out/bin + for program in tangle/*.cu; do + cp build/$(basename $program .cu) $out/bin/$(basename $program .cu) + addOpenGLRunpath $out/bin/$(basename $program .cu) + done + + mkdir $out/include + cp -r tangle/LLBM $out/include/ + ''; + + env = pkgs.buildEnv { + name = name; + paths = buildInputs; + }; + + shellHook = '' + export NIX_SHELL_NAME="${name}" + export CUDA_PATH="${pkgs.cudatoolkit_11}" + export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/run/opengl-driver/lib + ''; +} diff --git a/lbm.org b/lbm.org new file mode 100644 index 0000000..6df9a50 --- /dev/null +++ b/lbm.org @@ -0,0 +1,5817 @@ +#+TITLE: A literate Lattice Boltzmann Code +#+SUBTITLE: [[https://kummerlaender.eu][Adrian Kummerländer]] +#+STARTUP: latexpreview +#+HTML_DOCTYPE: html5 +#+OPTIONS: toc:nil html-postamble:nil html5-fancy:t html-style:nil +#+PROPERTY: header-args :exports both :mkdirp yes :noweb no-export :eval no-export +#+PROPERTY: header-args:python+ :var lattice="D2Q9" +#+PROPERTY: header-args:cpp+ :main no :eval no +#+HTML_MATHJAX: path:"https://static.kummerlaender.eu/mathjax/MathJax.js?config=TeX-AMS_HTML" + +#+HTML_HEAD: + +#+BEGIN_ABSTRACT +This file describes a full Lattice Boltzmann code featuring both 2D and 3D lattices, a workable selection of boundary conditions, Smagorinsky +turbulence modelling, expression-level code optimization and even a full ray marcher for just-in-time volumetric visualization in addition +to a set of interesting examples. All of this runs on GPUs using CUDA near the maximum possible performance on that platform. + +*This document is a [[*Open tasks][work in progress]].* +#+BEGIN_EXPORT html +