miniproject script
This commit is contained in:
parent
30aecda6dc
commit
f1a0e8c82f
162
ME_2016/.ipynb_checkpoints/joblib_demo-checkpoint.ipynb
Normal file
162
ME_2016/.ipynb_checkpoints/joblib_demo-checkpoint.ipynb
Normal file
@ -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<?, ?it/s]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "display_data"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"Parallel execution time: 5.86 seconds\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"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.1"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 5
|
||||||
|
}
|
||||||
355
ME_2016/.ipynb_checkpoints/miniproject-checkpoint.ipynb
Normal file
355
ME_2016/.ipynb_checkpoints/miniproject-checkpoint.ipynb
Normal file
File diff suppressed because one or more lines are too long
174
ME_2016/joblib_demo.ipynb
Normal file
174
ME_2016/joblib_demo.ipynb
Normal file
File diff suppressed because one or more lines are too long
355
ME_2016/miniproject.ipynb
Normal file
355
ME_2016/miniproject.ipynb
Normal file
File diff suppressed because one or more lines are too long
@ -394,7 +394,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 37,
|
"execution_count": 10,
|
||||||
"id": "0766e4d9-1f49-4449-a35d-05de86a3580e",
|
"id": "0766e4d9-1f49-4449-a35d-05de86a3580e",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
@ -410,10 +410,10 @@
|
|||||||
{
|
{
|
||||||
"data": {
|
"data": {
|
||||||
"text/latex": [
|
"text/latex": [
|
||||||
"$\\displaystyle - C_{1} = 280000 - 40 C_{2}$"
|
"$\\displaystyle C_{1} = 280000 - 400 C_{2}$"
|
||||||
],
|
],
|
||||||
"text/plain": [
|
"text/plain": [
|
||||||
"Eq(-C_1, 280000 - 40*C_2)"
|
"Eq(C_1, 280000 - 400*C_2)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
@ -444,7 +444,7 @@
|
|||||||
"source": [
|
"source": [
|
||||||
"C_1, C_2 = sm.symbols('C_1, C_2')\n",
|
"C_1, C_2 = sm.symbols('C_1, C_2')\n",
|
||||||
"\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",
|
"display('Left BC', left_BC)\n",
|
||||||
"\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",
|
"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",
|
"cell_type": "code",
|
||||||
"execution_count": 38,
|
"execution_count": 11,
|
||||||
"id": "765d2f17-7d1a-43a8-8b46-88ffed3ddaa3",
|
"id": "765d2f17-7d1a-43a8-8b46-88ffed3ddaa3",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"data": {
|
"data": {
|
||||||
"text/plain": [
|
"text/plain": [
|
||||||
"{C_1: -240714.285714286, C_2: 982.142857142858}"
|
"{C_1: -164000.000000000, C_2: 1110.00000000000}"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
@ -490,20 +490,20 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 39,
|
"execution_count": 12,
|
||||||
"id": "736b23d6-b2b3-43fe-bc16-5b1e5ae5c07b",
|
"id": "736b23d6-b2b3-43fe-bc16-5b1e5ae5c07b",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"data": {
|
"data": {
|
||||||
"text/latex": [
|
"text/latex": [
|
||||||
"$\\displaystyle -997.420634920635$"
|
"$\\displaystyle -856.777777777778$"
|
||||||
],
|
],
|
||||||
"text/plain": [
|
"text/plain": [
|
||||||
"-997.420634920635"
|
"-856.777777777778"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
"execution_count": 39,
|
"execution_count": 12,
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"output_type": "execute_result"
|
"output_type": "execute_result"
|
||||||
}
|
}
|
||||||
@ -521,45 +521,9 @@
|
|||||||
"Just trying some stuff"
|
"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",
|
"cell_type": "markdown",
|
||||||
"id": "85cf7fed-e3cd-41cf-ab69-c790f972c212",
|
"id": "e819cb10-9498-4522-8b69-0f7c46e5b7d7",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"source": [
|
"source": [
|
||||||
"## Part B"
|
"## Part B"
|
||||||
@ -575,7 +539,7 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 24,
|
"execution_count": 13,
|
||||||
"id": "ca0c2b96-652e-4f67-81b0-f98aecfb1707",
|
"id": "ca0c2b96-652e-4f67-81b0-f98aecfb1707",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
@ -586,8 +550,8 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"=========FINAL ANSWER=========\n",
|
"=========FINAL ANSWER=========\n",
|
||||||
"3C:\n",
|
"3C:\n",
|
||||||
"At the left face: -5550 F \n",
|
"At the left face: 111 F \n",
|
||||||
"At the right face:-10411 F\n",
|
"At the right face:-857 F\n",
|
||||||
"=========FINAL ANSWER=========\n",
|
"=========FINAL ANSWER=========\n",
|
||||||
"\n"
|
"\n"
|
||||||
]
|
]
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user