From 0ad0ddd913a91466e936ffc81e6ee64d5b8998d1 Mon Sep 17 00:00:00 2001 From: LM Date: Mon, 26 Feb 2024 10:01:03 +0100 Subject: [PATCH] rename tutorial harmonic oscillators .py file --- .pre-commit-config.yaml | 8 --- docs/tutorials/harmonic_oscillator.py | 82 +++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 8 deletions(-) create mode 100644 docs/tutorials/harmonic_oscillator.py diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 986c109..c31f91a 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -26,11 +26,3 @@ repos: - id: isort name: isort (python) args: ["--multi-line", "3", "--profile", "black", "--treat-comment-as-code", "# %%", "--float-to-top"] - - repo: https://github.com/mwouts/jupytext - rev: v1.16.1 - hooks: - - id: jupytext - args: [--sync, --pipe, black] - files: ^docs/tutorials/(.*\.py|.*\.ipynb)$ - additional_dependencies: - - black==24.2.0 diff --git a/docs/tutorials/harmonic_oscillator.py b/docs/tutorials/harmonic_oscillator.py new file mode 100644 index 0000000..d6336c3 --- /dev/null +++ b/docs/tutorials/harmonic_oscillator.py @@ -0,0 +1,82 @@ +# --- +# jupyter: +# jupytext: +# formats: ipynb,py:light +# text_representation: +# extension: .py +# format_name: light +# format_version: '1.5' +# jupytext_version: 1.16.1 +# kernelspec: +# display_name: .venv +# language: python +# name: python3 +# --- + +# + [markdown] magic_args="[markdown]" +# # Harmonic Oscillators +# +# In this tutorial, we demo how to generate data of harmonic oscillators. +# - + +# %% +import matplotlib.pyplot as plt + +from hamiltonian_flow.models.harmonic_oscillator import HarmonicOscillator + +# %% + +# %% +n_periods = 3 +n_samples_per_period = 200 + +# + [markdown] magic_args="[markdown]" +# ## Simple Harmonic Oscillator +# - + +# %% +sho_omega = 0.5 + +sho = HarmonicOscillator(system={"omega": sho_omega}) + +# %% +df_sho = sho(n_periods=n_periods, n_samples_per_period=n_samples_per_period) +df_sho.head() + +# %% +fig, ax = plt.subplots(figsize=(10, 6.18)) + +df_sho.plot(x="t", y="x", marker=".", ax=ax) + +ax.set_title(rf"Simple Harmonic Oscillator ($\omega = {sho_omega}$)") +ax.set_ylabel(r"Displacement $x(t)$") +ax.set_xlabel(r"$t$") + +# + [markdown] magic_args="[markdown]" +# ## Damped Harmonic Oscillator +# - + +# %% +dho_systems = { + "Underdamped": {"omega": 0.5, "zeta": 0.2}, + "Critical Damped": {"omega": 0.5, "zeta": 1}, + "Overdamped": { + "omega": 0.5, + "zeta": 1.2, + }, +} + + +for s_name, s in dho_systems.items(): + fig, ax = plt.subplots(figsize=(10, 6.18)) + + HarmonicOscillator(system=s)( + n_periods=n_periods, n_samples_per_period=n_samples_per_period + ).plot(x="t", y="x", marker=".", ax=ax) + + ax.set_title( + rf"{s_name} Harmonic Oscillator ($\omega = {s.get('omega')}$, $\zeta = {s.get('zeta')}$)" + ) + ax.set_ylabel(r"Displacement $x(t)$") + ax.set_xlabel(r"$t$") + plt.show()