Skip to content
Permalink
94d4a18c60
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
174 lines (174 sloc) 6.29 KB
{
"cells": [
{
"cell_type": "markdown",
"id": "72781f91-76a5-4f52-8998-5115c58993d2",
"metadata": {},
"source": [
"Project 1 ME3275\n",
"\n",
"Xinyu Zhao, Fall 2023\n",
"\n",
"A typical transport equation comprises a transient term, a convection (or advection to avoid confusion with heat transfer terminology) term, a diffusion term and a source term. We start from the simpliest problem in the first project: a 1-D advection of a wave form. The wave form can be acoustic waves or any other signals physically, and can take an initial shape of a step function or a sine function or anything else. In this project, we will transport a step function and the governing equation takes the following form: "
]
},
{
"cell_type": "markdown",
"id": "86af6e8f-d636-41c1-9fec-845047af156c",
"metadata": {},
"source": [
"$$\n",
"\\frac{\\partial u} {\\partial t} + c \\frac{\\partial u}{\\partial x} = 0~~(1)\n",
"$$"
]
},
{
"cell_type": "markdown",
"id": "227eb921-7e40-4773-9096-5b186b943fd6",
"metadata": {},
"source": []
},
{
"cell_type": "markdown",
"id": "ac2130e6-f834-4fc4-a174-ab29b1674d4e",
"metadata": {},
"source": [
"The equation is a linear partial differential equation that has similar properties as a hyperbolic equation. $c$ is a constant here, $t$ indicates time, and $x$ is distance. The goal is to solve for $u(x,t)$, which requires one boundary condition and initial condition. Let the initial condition be $u(x,0) = u_0(x)$. Then the exact solution of the equation is $u(x,t)=u_0(x-ct)$. "
]
},
{
"cell_type": "markdown",
"id": "93c3e864-aa1e-4558-a6dc-ba1f0ccab051",
"metadata": {},
"source": [
"Consider a computational domain of $L$ = 2 m. $N$=41 grid points (including the two boundary points)\n",
"will be used to discretize the 1-D computational domain, with equal spacing (i.e., $\\Delta x$ =\n",
"$L/(N-1)$). We discretize this equation in both space and time. Consider discretizing the\n",
"spatial coordinate $x$ into points that we index from i=0 to N, and stepping in discrete time\n",
"interval of size $\\Delta t$ = 0.025 s, and $c$ is a constant of 1 m/s. Applying the first-order explicit scheme in time and first-order backward difference in space, the discretized equation at location $i$ and time $n$ becomes:\r",
"$$\n",
"(u^{n+1}_i - u^{n}_i)/\\Delta t + c(u^n_i - u^n_{i-1})/\\Delta x = 0~~(2)\n",
"$$\n",
"where $n$ and $n+1$ are two consecutive steps in time, while $i-1$ and $i$ are two neighboring points of the discretized $x$ coordinate. Given the initial conditions, the $\\underline{only}$ unknown in this equation is $u^{n+1}_i$. "
]
},
{
"cell_type": "markdown",
"id": "9860a66e-48b8-407a-8681-84dcbcbb3ab3",
"metadata": {},
"source": [
"Question (a) Re-arrange Equation (2), and obtain an explicit solution for $u^{n+1}_i$ as a function of all other terms in the equation."
]
},
{
"cell_type": "markdown",
"id": "8906c731-5fe6-4981-8223-cf57e283c912",
"metadata": {},
"source": [
"Question (b) Set the initial conditions of the velocity (i.e., $u_0$) using the following code. Add your own plotting code to visualize the initial shape of the velocity $u$."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "62c86f84-97b1-4967-ae91-54ffa8c36a88",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np #here we load numpy as in HW1\n",
"import matplotlib as plt #here we load matplotlib as in HW1\n",
"import time, sys #we load some additional functions"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "490caae1-d07c-4f8c-8d1d-b4863a68cd36",
"metadata": {},
"outputs": [],
"source": [
"## Set up your problem: geometry, mesh, and constants\n",
"nx = 41; # total number of grid points\n",
"L = 2.0; # meter\n",
"dx = L/(nx-1); # grid spacing\n",
"nt = 25; # total number of time steps\n",
"dt = .025; # second\n",
"c = 1; # m/s\n",
"\n",
"## initialize the velocity field\n",
"u = np.ones(nx); ## create an array of the length of nx, filled with value 1 m/s\n",
"u[int(.5 / dx):int(1 / dx + 1)]=2; ## set the values between x=0.5 and x=1 to be 2 m/s\n",
"print(u)\n",
"## add your code to plot u here"
]
},
{
"cell_type": "markdown",
"id": "aedde6ad-6e5f-4f88-a1ea-4f50c07adcfc",
"metadata": {},
"source": [
"Question (c) Now we want to calculate the change of velocity after 25 time steps. You are expected to fill in your codes at location #1 - #5. "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5ad8ffa5-a56c-4ae9-911d-dd104ff6222b",
"metadata": {},
"outputs": [],
"source": [
"un = np.ones(nx) #initialize a temporary array\n",
"#1 add for loop for values of n from 0 to nt, so it will run nt times\n",
"#2 within the for loop, copy the existing values of u into un\n",
" #3 loop for advancing the values of u at each point in x\n",
" #4 your formula for computing u at current time n+1\n",
" #5 plot your solutions at the last time step"
]
},
{
"cell_type": "markdown",
"id": "7ebdb484-04fc-414f-a3eb-51370450122b",
"metadata": {},
"source": [
"Question (d) Plot and compare velocity profiles at $t=0.625$ when you use $dt = 0.25$ and $dt=0.0025$. You are expected to have three curves on this plot. Comment on the differences. "
]
},
{
"cell_type": "markdown",
"id": "d1a62149-9fda-46ea-a655-9459d403ccc5",
"metadata": {},
"source": [
"Bonus (a), extra 2 points, repeat Questions (a)-(d) using a central difference in space."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a95140bf-f50a-4593-9e1d-337b101f783c",
"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.11.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}