From cc72a74442b843ee489f20285fd19cd0cc8353f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20K=C3=B6hler?= <27728103+Ceyron@users.noreply.github.com> Date: Fri, 10 May 2024 09:54:30 +0200 Subject: [PATCH] Streamlit for Navier-Stokes dynamics --- navier_stokes_dynamics.py | 292 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 292 insertions(+) create mode 100644 navier_stokes_dynamics.py diff --git a/navier_stokes_dynamics.py b/navier_stokes_dynamics.py new file mode 100644 index 0000000..5a5bff6 --- /dev/null +++ b/navier_stokes_dynamics.py @@ -0,0 +1,292 @@ +""" +This is a streamlit app. +""" +import base64 +import dataclasses +import io +import json +import random +from dataclasses import dataclass +from typing import Optional + +import jax +import jax.numpy as jnp +import matplotlib.pyplot as plt +import numpy as np +import streamlit as st +import streamlit.components.v1 as components +from IPython.display import DisplayObject +from matplotlib.colors import Colormap, LinearSegmentedColormap, ListedColormap + +import exponax as ex + +st.set_page_config(layout="wide") +jax.config.update("jax_platform_name", "cpu") + +with st.sidebar: + st.title("Exponax Dynamics Brewer") + dimension_type = st.select_slider( + "Number of Spatial Dimensions (ST=Spatio-Temporal plot)", + options=[ + "2d", + "2d ST", + ], + ) + num_points = st.slider("Number of points", 16, 256, 48) + num_steps = st.slider("Number of steps", 1, 300, 50) + num_modes_init = st.slider("Number of modes in the initial condition", 1, 40, 5) + num_substeps = st.slider("Number of substeps", 1, 100, 1) + + v_range = st.slider("Value range", 0.1, 10.0, 1.0) + + st.divider() + + # domain_extent = st.select_slider("Domain Extent", [1.0, 2 * jnp.pi]) + + dt_cols = st.columns(3) + with dt_cols[0]: + dt_mantissa = st.slider("dt mantissa", 0.0, 1.0, 0.1) + with dt_cols[1]: + dt_exponent = st.slider("dt exponent", -5, 5, 0) + dt_sign = "+" + dt = float(f"{dt_sign}{dt_mantissa}e{dt_exponent}") + + diffusivity_cols = st.columns(3) + with diffusivity_cols[0]: + diffusivity_mantissa = st.slider("diffusivity mantissa", 0.0, 1.0, 0.1) + with diffusivity_cols[1]: + diffusivity_exponent = st.slider("diffusivity exponent", -5, 5, -2) + diffusivity_sign = "+" + diffusivity = float( + f"{diffusivity_sign}{diffusivity_mantissa}e{diffusivity_exponent}" + ) + + use_kolmogorov = st.toggle("Use Kolmogorov", value=False) + + if use_kolmogorov: + domain_extent = 2 * jnp.pi + + injection_mode = st.slider("Injection Mode", 1, 20, 4) + injection_scale = st.slider("Injection Scale", 0.1, 10.0, 1.0) + + else: + domain_extent = 1.0 + + st.write(f"dt: {dt}") + st.write(f"diffusivity: {diffusivity}") + st.write(f"domain_extent: {domain_extent}") + + # st.write(f"Linear: {linear_tuple}") + # st.write(f"Nonlinear: {nonlinear_tuple}") + +if dimension_type in ["1d ST", "1d"]: + num_spatial_dims = 1 +elif dimension_type in ["2d ST", "2d"]: + num_spatial_dims = 2 +elif dimension_type == "3d": + num_spatial_dims = 3 + +if use_kolmogorov: + stepper = ex.RepeatedStepper( + ex.stepper.KolmogorovFlowVorticity( + num_spatial_dims, + domain_extent, + num_points, + dt / num_substeps, + diffusivity=diffusivity, + drag=-0.1, + injection_mode=injection_mode, + injection_scale=injection_scale, + ), + num_substeps, + ) +else: + stepper = ex.RepeatedStepper( + ex.stepper.NavierStokesVorticity( + num_spatial_dims, + domain_extent, + num_points, + dt / num_substeps, + diffusivity=diffusivity, + drag=0.0, + ), + num_substeps, + ) + +if num_spatial_dims == 1: + ic_gen = ex.ic.RandomSineWaves1d( + num_spatial_dims, cutoff=num_modes_init, max_one=True + ) +else: + ic_gen = ex.ic.RandomTruncatedFourierSeries( + num_spatial_dims, cutoff=num_modes_init, max_one=True + ) +u_0 = ic_gen(num_points, key=jax.random.PRNGKey(0)) + +trj = ex.rollout(stepper, num_steps, include_init=True)(u_0) + + +TEMPLATE_IFRAME = """ +