From f1a0e8c82fdc8b0ce9dcb389128b118cfcf9cf3c Mon Sep 17 00:00:00 2001 From: Dane Sabo Date: Thu, 31 Oct 2024 17:36:33 -0400 Subject: [PATCH] miniproject script --- .../joblib_demo-checkpoint.ipynb | 162 ++++++++ .../miniproject-checkpoint.ipynb | 355 ++++++++++++++++++ ME_2016/joblib_demo.ipynb | 174 +++++++++ ME_2016/miniproject.ipynb | 355 ++++++++++++++++++ NUCE_2100/HW6.ipynb | 64 +--- 5 files changed, 1060 insertions(+), 50 deletions(-) create mode 100644 ME_2016/.ipynb_checkpoints/joblib_demo-checkpoint.ipynb create mode 100644 ME_2016/.ipynb_checkpoints/miniproject-checkpoint.ipynb create mode 100644 ME_2016/joblib_demo.ipynb create mode 100644 ME_2016/miniproject.ipynb diff --git a/ME_2016/.ipynb_checkpoints/joblib_demo-checkpoint.ipynb b/ME_2016/.ipynb_checkpoints/joblib_demo-checkpoint.ipynb new file mode 100644 index 0000000..c3579dd --- /dev/null +++ b/ME_2016/.ipynb_checkpoints/joblib_demo-checkpoint.ipynb @@ -0,0 +1,162 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "id": "33f1ce1d-1a09-46d1-9304-3d02c715903f", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Benchmarking...\n", + "Single-process execution time: 37.67 seconds\n" + ] + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "e29a55948eae404da54cadb0ff8396ff", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Running Simulations: 0%| | 0/1000 [00:00" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "# Plotting all results on the same plot (using the parallel results)\n", + "plt.figure(figsize=(10, 10))\n", + "for i, (t, solution) in enumerate(parallel_results):\n", + " #print(solution[:10,:])\n", + " plt.plot(solution[:,0], solution[:, 2], label=f'Initial Condition {i+1}')" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.12.3" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/ME_2016/joblib_demo.ipynb b/ME_2016/joblib_demo.ipynb new file mode 100644 index 0000000..b8a5798 --- /dev/null +++ b/ME_2016/joblib_demo.ipynb @@ -0,0 +1,174 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "33f1ce1d-1a09-46d1-9304-3d02c715903f", + "metadata": { + "scrolled": true + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Benchmarking...\n", + "Single-process execution time: 51.11 seconds\n" + ] + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "be8e486cced340e99178163a19bcb27a", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Running Simulations: 0%| | 0/1000 [00:00" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "import numpy as np\n", + "from scipy.integrate import odeint\n", + "from joblib import Parallel, delayed\n", + "import matplotlib.pyplot as plt\n", + "import time\n", + "from tqdm.notebook import tqdm\n", + "from numba import jit\n", + "\n", + "# Accelerate the ODE system using numba\n", + "@jit(nopython=True)\n", + "def simple_oscillator(y, t):\n", + " x, v = y # y[0] = position (x), y[1] = velocity (v)\n", + " dxdt = v # dx/dt = v\n", + " dvdt = -x # dv/dt = -x\n", + " return [dxdt, dvdt]\n", + "\n", + "# Function to simulate a batch of initial conditions\n", + "def simulate_batch(batch):\n", + " t = np.linspace(0, 1000, 100000) # Larger time steps for benchmarking\n", + " results = [(t, odeint(simple_oscillator, ic, t, atol=1e-9)) for ic in batch]\n", + " return results\n", + "\n", + "# Function to run simulations using joblib for parallelism\n", + "def parallel_simulations(initial_conditions_list, batch_size=2):\n", + " # Split the initial conditions into batches to reduce overhead\n", + " batches = [\n", + " initial_conditions_list[i:i + batch_size]\n", + " for i in range(0, len(initial_conditions_list), batch_size)\n", + " ]\n", + " \n", + " # Run parallel simulations with joblib\n", + " results = Parallel(n_jobs=-1)(\n", + " delayed(simulate_batch)(batch) for batch in tqdm(batches, desc=\"Running Simulations\")\n", + " )\n", + " \n", + " # Flatten the list of batches into a single list of results\n", + " return [item for batch_result in results for item in batch_result]\n", + "\n", + "# Benchmarking function\n", + "def benchmark(initial_conditions_list):\n", + " print(\"Benchmarking...\")\n", + "\n", + " # Single-process execution\n", + " start_time = time.time()\n", + " single_process_results = [simulate_batch([ic])[0] for ic in initial_conditions_list]\n", + " single_process_time = time.time() - start_time\n", + " print(f\"Single-process execution time: {single_process_time:.2f} seconds\")\n", + "\n", + " # Parallel execution with joblib\n", + " start_time = time.time()\n", + " parallel_results = parallel_simulations(initial_conditions_list, batch_size=2)\n", + " parallel_time = time.time() - start_time\n", + " print(f\"Parallel execution time: {parallel_time:.2f} seconds\")\n", + "\n", + " return single_process_results, parallel_results\n", + "\n", + "# List of initial conditions for benchmarking\n", + "initial_conditions_list = [\n", + " [1.0, 0.0], # Initial position 1.0, velocity 0.0\n", + " [0.5, 0.5], # Initial position 0.5, velocity 0.5\n", + " [-1.0, 0.0], # Initial position -1.0, velocity 0.0\n", + " [0.0, 1.0], # Initial position 0.0, velocity 1.0\n", + " [2.0, -1.0] # Initial position 2.0, velocity -1.0\n", + "] * 400 # Increase the workload for meaningful benchmarking\n", + "\n", + "# Run the benchmark\n", + "single_process_results, parallel_results = benchmark(initial_conditions_list)\n", + "\n", + "# Plotting all results on the same plot (using the parallel results)\n", + "plt.figure(figsize=(10, 6))\n", + "for i, (t, solution) in enumerate(parallel_results):\n", + " plt.plot(t, solution[:, 0], label=f'Initial Condition {i+1}')\n", + "\n", + "# Add labels, title, legend, and grid\n", + "plt.xlabel('Time (t)')\n", + "plt.ylabel('Position (x)')\n", + "plt.title('ODE Simulations: Simple Harmonic Oscillator')\n", + "plt.legend(loc='upper right', bbox_to_anchor=(1.15, 1))\n", + "plt.grid(True)\n", + "\n", + "# Display the plot\n", + "plt.show()" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "367b156c-a843-4fec-9d95-5aeeb2de38c6", + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "03f1d22f-7efa-41de-9c8e-d165213172e2", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.12.3" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/ME_2016/miniproject.ipynb b/ME_2016/miniproject.ipynb new file mode 100644 index 0000000..ab990b2 --- /dev/null +++ b/ME_2016/miniproject.ipynb @@ -0,0 +1,355 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 2, + "id": "1409d50c-9251-4f36-af8d-8f8481182601", + "metadata": {}, + "outputs": [], + "source": [ + "import sympy as sm" + ] + }, + { + "cell_type": "markdown", + "id": "1ebf9cbc-a151-4178-aba5-9f5f66c1209b", + "metadata": {}, + "source": [ + "Manipulating the norm into an usable equation:" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "33137bb3-e298-455b-9174-27c005a84e45", + "metadata": {}, + "outputs": [ + { + "data": { + "text/latex": [ + "$\\displaystyle \\left[\\begin{matrix}b \\frac{d}{d t} x{\\left(t \\right)} + x{\\left(t \\right)} + \\frac{d^{2}}{d t^{2}} x{\\left(t \\right)}\\\\b \\frac{d}{d t} y{\\left(t \\right)} + y{\\left(t \\right)} + \\frac{d^{2}}{d t^{2}} y{\\left(t \\right)}\\end{matrix}\\right]$" + ], + "text/plain": [ + "Matrix([\n", + "[b*Derivative(x(t), t) + x(t) + Derivative(x(t), (t, 2))],\n", + "[b*Derivative(y(t), t) + y(t) + Derivative(y(t), (t, 2))]])" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Working with their LHS of Eq 2:\n", + "t = sm.symbols('t')\n", + "x = sm.Function('x')(t)\n", + "y = sm.Function('y')(t)\n", + "\n", + "#Create State Vector\n", + "X = sm.Matrix([x, y])\n", + "\n", + "#Create EQ 2 LHS:\n", + "b = sm.symbols('b')\n", + "LHS = X.diff('t','t') + b*X.diff('t') + X\n", + "LHS" + ] + }, + { + "cell_type": "code", + "execution_count": 88, + "id": "3edae373-94fd-4458-887a-8a01c220a1f6", + "metadata": {}, + "outputs": [ + { + "data": { + "text/latex": [ + "$\\displaystyle \\left[\\begin{matrix}\\frac{p_{n} \\left(x_{n} - x{\\left(t \\right)}\\right)}{\\left(h^{2} + \\left|{x_{n} - x{\\left(t \\right)}}\\right|^{2} + \\left|{y_{n} - y{\\left(t \\right)}}\\right|^{2}\\right)^{2.5}}\\\\\\frac{p_{n} \\left(y_{n} - y{\\left(t \\right)}\\right)}{\\left(h^{2} + \\left|{x_{n} - x{\\left(t \\right)}}\\right|^{2} + \\left|{y_{n} - y{\\left(t \\right)}}\\right|^{2}\\right)^{2.5}}\\end{matrix}\\right]$" + ], + "text/plain": [ + "Matrix([\n", + "[p_n*(x_n - x(t))/(h**2 + Abs(x_n - x(t))**2 + Abs(y_n - y(t))**2)**2.5],\n", + "[p_n*(y_n - y(t))/(h**2 + Abs(x_n - x(t))**2 + Abs(y_n - y(t))**2)**2.5]])" + ] + }, + "execution_count": 88, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "#Now, create the RHS for a given x_n and y_n of a magnet\n", + "p_n, x_n, y_n, h = sm.symbols('p_n, x_n, y_n, h')\n", + "X_n = sm.Matrix([x_n, y_n])\n", + "\n", + "RHS_gen = p_n * (X_n - X)/((X_n-X).norm()**2 + h**2)**(5/2)\n", + "RHS_gen" + ] + }, + { + "cell_type": "code", + "execution_count": 97, + "id": "a0920c7a-4abd-4f63-9804-a5207a6c2693", + "metadata": {}, + "outputs": [ + { + "data": { + "text/latex": [ + "$\\displaystyle \\left[\\begin{matrix}b \\frac{d}{d t} x{\\left(t \\right)} + x{\\left(t \\right)} + \\frac{d^{2}}{d t^{2}} x{\\left(t \\right)}\\\\b \\frac{d}{d t} y{\\left(t \\right)} + y{\\left(t \\right)} + \\frac{d^{2}}{d t^{2}} y{\\left(t \\right)}\\end{matrix}\\right] = \\left[\\begin{matrix}- \\frac{x{\\left(t \\right)} - 1}{\\left(h^{2} + \\left|{x{\\left(t \\right)} - 1}\\right|^{2} + \\left|{y{\\left(t \\right)} - 1}\\right|^{2}\\right)^{2.5}} - \\frac{x{\\left(t \\right)} - 1}{\\left(h^{2} + \\left|{x{\\left(t \\right)} - 1}\\right|^{2} + \\left|{y{\\left(t \\right)} + 1}\\right|^{2}\\right)^{2.5}} - \\frac{x{\\left(t \\right)} + 1}{\\left(h^{2} + \\left|{x{\\left(t \\right)} + 1}\\right|^{2} + \\left|{y{\\left(t \\right)} - 1}\\right|^{2}\\right)^{2.5}} - \\frac{x{\\left(t \\right)} + 1}{\\left(h^{2} + \\left|{x{\\left(t \\right)} + 1}\\right|^{2} + \\left|{y{\\left(t \\right)} + 1}\\right|^{2}\\right)^{2.5}}\\\\- \\frac{y{\\left(t \\right)} - 1}{\\left(h^{2} + \\left|{x{\\left(t \\right)} - 1}\\right|^{2} + \\left|{y{\\left(t \\right)} - 1}\\right|^{2}\\right)^{2.5}} - \\frac{y{\\left(t \\right)} - 1}{\\left(h^{2} + \\left|{x{\\left(t \\right)} + 1}\\right|^{2} + \\left|{y{\\left(t \\right)} - 1}\\right|^{2}\\right)^{2.5}} - \\frac{y{\\left(t \\right)} + 1}{\\left(h^{2} + \\left|{x{\\left(t \\right)} - 1}\\right|^{2} + \\left|{y{\\left(t \\right)} + 1}\\right|^{2}\\right)^{2.5}} - \\frac{y{\\left(t \\right)} + 1}{\\left(h^{2} + \\left|{x{\\left(t \\right)} + 1}\\right|^{2} + \\left|{y{\\left(t \\right)} + 1}\\right|^{2}\\right)^{2.5}}\\end{matrix}\\right]$" + ], + "text/plain": [ + "Eq(Matrix([\n", + "[b*Derivative(x(t), t) + x(t) + Derivative(x(t), (t, 2))],\n", + "[b*Derivative(y(t), t) + y(t) + Derivative(y(t), (t, 2))]]), Matrix([\n", + "[-(x(t) - 1)/(h**2 + Abs(x(t) - 1)**2 + Abs(y(t) - 1)**2)**2.5 - (x(t) - 1)/(h**2 + Abs(x(t) - 1)**2 + Abs(y(t) + 1)**2)**2.5 - (x(t) + 1)/(h**2 + Abs(x(t) + 1)**2 + Abs(y(t) - 1)**2)**2.5 - (x(t) + 1)/(h**2 + Abs(x(t) + 1)**2 + Abs(y(t) + 1)**2)**2.5],\n", + "[-(y(t) - 1)/(h**2 + Abs(x(t) - 1)**2 + Abs(y(t) - 1)**2)**2.5 - (y(t) - 1)/(h**2 + Abs(x(t) + 1)**2 + Abs(y(t) - 1)**2)**2.5 - (y(t) + 1)/(h**2 + Abs(x(t) - 1)**2 + Abs(y(t) + 1)**2)**2.5 - (y(t) + 1)/(h**2 + Abs(x(t) + 1)**2 + Abs(y(t) + 1)**2)**2.5]]))" + ] + }, + "execution_count": 97, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Now we combine these two halves, and substitue the RHS for magnet locations on the unit circle.\n", + "X_1 = [1, 1]; X_2 = [1, -1]; X_3 = [-1, -1]; X_4 = [-1, 1]\n", + "p = 1\n", + "EQ_2 = sm.Eq(LHS,\n", + " RHS_gen.subs({x_n: X_1[0], y_n: X_1[1], p_n: p}) +\n", + " RHS_gen.subs({x_n: X_2[0], y_n: X_2[1], p_n: p}) +\n", + " RHS_gen.subs({x_n: X_3[0], y_n: X_3[1], p_n: p}) +\n", + " RHS_gen.subs({x_n: X_4[0], y_n: X_4[1], p_n: p})\n", + " )\n", + "EQ_2.simplify()" + ] + }, + { + "cell_type": "markdown", + "id": "a9ef3e10-851b-4fbb-bc20-c758dd7f66b5", + "metadata": {}, + "source": [ + "So with this we have four states:\n", + "\n", + "$\\left[\\matrix{x \\\\ \\dot x \\\\ y \\\\ \\dot y}\\right] = \\left[\\matrix{z_1 \\\\ z_2 \\\\ z_3 \\\\ z_4}\\right]$" + ] + }, + { + "cell_type": "markdown", + "id": "07ce02f2-1a39-44b3-90a2-bffdac9fe9bc", + "metadata": {}, + "source": [ + "We can rearrange our equation that we got from SymPy into two first order differential equations:\n", + "\n", + "$$\\left[\\matrix{\\dot z_1 \\\\ \\dot z_2 \\\\ \\dot z_3 \\\\ \\dot z_4} \\right] = \n", + "\\left[\\matrix{\n", + "z_2 \\\\\n", + "-\\frac{z_1 - 1}{\\left(h^2+(z_1-1)^2 + (z_3 - 1)^2 \\right)^{2.5}} \n", + " -\\frac{z_1 - 1}{\\left(h^2+(z_1-1)^2 + (z_3 + 1)^2 \\right)^{2.5}} \n", + " -\\frac{z_1 + 1}{\\left(h^2+(z_1+1)^2 + (z_3 - 1)^2 \\right)^{2.5}} \n", + " -\\frac{z_1 + 1}{\\left(h^2+(z_1+1)^2 + (z_3 + 1)^2 \\right)^{2.5}} \n", + " - b z_2 - z_1 \\\\\n", + "z_4 \\\\\n", + "-\\frac{z_3 - 1}{\\left(h^2+(z_1-1)^2 + (z_3 - 1)^2 \\right)^{2.5}} \n", + " -\\frac{z_3 - 1}{\\left(h^2+(z_1+1)^2 + (z_3 - 1)^2 \\right)^{2.5}} \n", + " -\\frac{z_3 + 1}{\\left(h^2+(z_1-1)^2 + (z_3 + 1)^2 \\right)^{2.5}} \n", + " -\\frac{z_3 + 1}{\\left(h^2+(z_1+1)^2 + (z_3 + 1)^2 \\right)^{2.5}} \n", + " - b z_4 - z_3\\\\\n", + "}\\right]$$" + ] + }, + { + "cell_type": "markdown", + "id": "fddb7795-21f3-409a-bbc9-e2bba02eb8a2", + "metadata": {}, + "source": [ + "and this we can use with SciPy's odeint.\n", + "\n", + "Let's do it:" + ] + }, + { + "cell_type": "code", + "execution_count": 165, + "id": "9cd6761f-ab45-4e05-a658-d7ca4f715536", + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "from scipy.integrate import odeint\n", + "from joblib import Parallel, delayed\n", + "import matplotlib.pyplot as plt\n", + "import time\n", + "from tqdm.notebook import tqdm\n", + "from numba import jit\n", + "\n", + "''' \n", + "Code below is heavily borrowed from Nikhil Bajaj, University of Pittsburgh from ME2016 Nonlinear Dynamics 1.\n", + "\n", + "Major Changes:\n", + "Oscilator was changed into the magnetic pendulum. 4 states instead of 2\n", + "\n", + "'''\n", + "# Declare Global Variables\n", + "h = 0.1\n", + "b_global = 0.175\n", + "\n", + "# Accelerate the ODE system using numba\n", + "@jit(nopython=True)\n", + "def magnetic_pendulum(Z, t):\n", + " z_1, z_2, z_3, z_4, b = Z #z_1 = x, z_2 = x_dot, z_3 = y, z_4 = y_dot, z_5 = b (for convenience to change damping)\n", + " dz_1dt = z_2 \n", + " dz_2dt = -(z_1 - 1)/(h**2 + (z_1 - 1)**2 + (z_3 - 1)**2)**2.5 \\\n", + " - (z_1 - 1)/(h**2 + (z_1 - 1)**2 + (z_3 + 1)**2)**2.5 \\\n", + " - (z_1 + 1)/(h**2 + (z_1 + 1)**2 + (z_3 - 1)**2)**2.5 \\\n", + " - (z_1 + 1)/(h**2 + (z_1 + 1)**2 + (z_3 + 1)**2)**2.5 \\\n", + " - b*z_2 - z_1\n", + " dz_3dt = z_4 \n", + " dz_4dt = -(z_3 - 1)/(h**2 + (z_1 - 1)**2 + (z_3 - 1)**2)**2.5 \\\n", + " - (z_3 - 1)/(h**2 + (z_1 + 1)**2 + (z_3 - 1)**2)**2.5 \\\n", + " - (z_3 + 1)/(h**2 + (z_1 - 1)**2 + (z_3 + 1)**2)**2.5 \\\n", + " - (z_3 + 1)/(h**2 + (z_1 + 1)**2 + (z_3 + 1)**2)**2.5 \\\n", + " - b*z_4 - z_3\n", + " db_dt = 0\n", + " return [dz_1dt, dz_2dt, dz_3dt, dz_4dt, db_dt]\n", + "\n", + "# Function to simulate a batch of initial conditions\n", + "def simulate_batch(batch):\n", + " t = np.linspace(0, 15, 1000) # Larger time steps for benchmarking\n", + " results = [(t, odeint(magnetic_pendulum, ic, t)) for ic in batch]\n", + " return results\n", + "\n", + "# Function to run simulations using joblib for parallelism\n", + "def parallel_simulations(initial_conditions_list, batch_size=2):\n", + " # Split the initial conditions into batches to reduce overhead\n", + " batches = [\n", + " initial_conditions_list[i:i + batch_size]\n", + " for i in range(0, len(initial_conditions_list), batch_size)\n", + " ]\n", + " \n", + " # Run parallel simulations with joblib\n", + " results = Parallel(n_jobs=-1)(\n", + " delayed(simulate_batch)(batch) for batch in tqdm(batches, desc=\"Running Simulations\")\n", + " )\n", + " \n", + " # Flatten the list of batches into a single list of results\n", + " return [item for batch_result in results for item in batch_result]\n", + "\n", + "# Benchmarking function\n", + "def benchmark(initial_conditions_list):\n", + " print(\"Benchmarking...\")\n", + "\n", + " # Single-process execution\n", + " #start_time = time.time()\n", + " #single_process_results = [simulate_batch([ic])[0] for ic in initial_conditions_list]\n", + " #single_process_time = time.time() - start_time\n", + " #print(f\"Single-process execution time: {single_process_time:.2f} seconds\")\n", + "\n", + " # Parallel execution with joblib\n", + " start_time = time.time()\n", + " parallel_results = parallel_simulations(initial_conditions_list, batch_size=2)\n", + " parallel_time = time.time() - start_time\n", + " print(f\"Parallel execution time: {parallel_time:.2f} seconds\")\n", + "\n", + " return None, parallel_results\n", + "\n", + "# List of initial conditions for benchmarking\n", + "num_pixels = 100\n", + "nudge = 1e-1\n", + "value_range = np.linspace(-2+nudge, 2+nudge, num_pixels)\n", + "zeros = np.zeros([num_pixels**2])\n", + "b_vec = np.ones([num_pixels**2])*b_global\n", + "x_values, y_values = np.meshgrid(value_range, value_range)\n", + "\n", + "initial_conditions_list = np.column_stack((x_values.flatten(), zeros, y_values.flatten(), zeros, b_vec))\n", + "#initial_conditions_list\n", + "#plt.plot(x_values,y_values, 'x')" + ] + }, + { + "cell_type": "code", + "execution_count": 166, + "id": "ed23a81b-9596-43ef-8a4d-c634f3ee1f4a", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Benchmarking...\n" + ] + }, + { + "data": { + "application/vnd.jupyter.widget-view+json": { + "model_id": "aa361c214f25438e94092e9bd95c15e6", + "version_major": 2, + "version_minor": 0 + }, + "text/plain": [ + "Running Simulations: 0%| | 0/5000 [00:00" + ] + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "# Plotting all results on the same plot (using the parallel results)\n", + "plt.figure(figsize=(10, 10))\n", + "for i, (t, solution) in enumerate(parallel_results):\n", + " #print(solution[:10,:])\n", + " plt.plot(solution[:,0], solution[:, 2], label=f'Initial Condition {i+1}')" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.12.3" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/NUCE_2100/HW6.ipynb b/NUCE_2100/HW6.ipynb index 8113205..a1e18a0 100644 --- a/NUCE_2100/HW6.ipynb +++ b/NUCE_2100/HW6.ipynb @@ -394,7 +394,7 @@ }, { "cell_type": "code", - "execution_count": 37, + "execution_count": 10, "id": "0766e4d9-1f49-4449-a35d-05de86a3580e", "metadata": {}, "outputs": [ @@ -410,10 +410,10 @@ { "data": { "text/latex": [ - "$\\displaystyle - C_{1} = 280000 - 40 C_{2}$" + "$\\displaystyle C_{1} = 280000 - 400 C_{2}$" ], "text/plain": [ - "Eq(-C_1, 280000 - 40*C_2)" + "Eq(C_1, 280000 - 400*C_2)" ] }, "metadata": {}, @@ -444,7 +444,7 @@ "source": [ "C_1, C_2 = sm.symbols('C_1, C_2')\n", "\n", - "left_BC = sm.Eq(C_1, h_left*(T_left - C_2/k ))\n", + "left_BC = sm.Eq(C_1, h_left*(T_left - C_2))\n", "display('Left BC', left_BC)\n", "\n", "right_BC = sm.Eq(q_ppprime*x_right + C_1, h_right*(T_right + (q_ppprime*x_right**2/2/k + C_1/k*x_right + C_2)))\n", @@ -461,14 +461,14 @@ }, { "cell_type": "code", - "execution_count": 38, + "execution_count": 11, "id": "765d2f17-7d1a-43a8-8b46-88ffed3ddaa3", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "{C_1: -240714.285714286, C_2: 982.142857142858}" + "{C_1: -164000.000000000, C_2: 1110.00000000000}" ] }, "metadata": {}, @@ -490,20 +490,20 @@ }, { "cell_type": "code", - "execution_count": 39, + "execution_count": 12, "id": "736b23d6-b2b3-43fe-bc16-5b1e5ae5c07b", "metadata": {}, "outputs": [ { "data": { "text/latex": [ - "$\\displaystyle -997.420634920635$" + "$\\displaystyle -856.777777777778$" ], "text/plain": [ - "-997.420634920635" + "-856.777777777778" ] }, - "execution_count": 39, + "execution_count": 12, "metadata": {}, "output_type": "execute_result" } @@ -521,45 +521,9 @@ "Just trying some stuff" ] }, - { - "cell_type": "code", - "execution_count": 35, - "id": "7af1e713-fc89-4679-99aa-f2bbacfb0c8e", - "metadata": {}, - "outputs": [], - "source": [ - "x = sm.symbols('x')\n", - "T = sm.Function('T')(x)\n", - "gov_eq = sm.Eq(0,T.diff(x)+q_ppprime)" - ] - }, - { - "cell_type": "code", - "execution_count": 36, - "id": "5edad66f-d4c8-4b51-8c92-343f83f65944", - "metadata": {}, - "outputs": [ - { - "data": { - "text/latex": [ - "$\\displaystyle T{\\left(x \\right)} = C_{1} - 50000000.0 x$" - ], - "text/plain": [ - "Eq(T(x), C1 - 50000000.0*x)" - ] - }, - "execution_count": 36, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "sm.dsolve(gov_eq)" - ] - }, { "cell_type": "markdown", - "id": "85cf7fed-e3cd-41cf-ab69-c790f972c212", + "id": "e819cb10-9498-4522-8b69-0f7c46e5b7d7", "metadata": {}, "source": [ "## Part B" @@ -575,7 +539,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 13, "id": "ca0c2b96-652e-4f67-81b0-f98aecfb1707", "metadata": {}, "outputs": [ @@ -586,8 +550,8 @@ "\n", "=========FINAL ANSWER=========\n", "3C:\n", - "At the left face: -5550 F \n", - "At the right face:-10411 F\n", + "At the left face: 111 F \n", + "At the right face:-857 F\n", "=========FINAL ANSWER=========\n", "\n" ]