Upload
tom
View
49
Download
0
Embed Size (px)
DESCRIPTION
Solving the diffusion equation in 1D
Citation preview
10/02/2015 The 1D diffusion equation
http://hplgit.github.io/num-methods-for-PDEs/doc/pub/diffu/sphinx/._main_diffu001.html 1/23
(1)
(2)
(3)
(4)
The 1D diffusion equationThefamousdiffusionequation,alsoknownastheheatequation,reads
where is the unknown function to be solved for, is a coordinate in space, and is time. Thecoefficient isthediffusioncoefficientanddetermineshowfast changesintime.Aquickshortformforthediffusionequationis .
Comparedtothewaveequation, ,whichlooksverysimilar,butthediffusionequationfeaturessolutions that are very different from those of thewaveequation.Also, the diffusion equationmakes quitedifferentdemandstothenumericalmethods.
Typicaldiffusionproblemsmayexperience rapidchange in theverybeginning,but then theevolutionofbecomesslowerandslower.Thesolutionisusuallyverysmooth,andaftersometime,onecannotrecognizethe initialshapeof .This is insharpcontrast tosolutionsof thewaveequationwhere the initial shape ispreservedthesolutionisbasicallyamovinginitialcondition.Thestandardwaveequation hassolutions that propagates with speed forever, without changing shape, while the diffusion equationconverges to a stationary solution as . In this limit, , and is governed by
.ThisstationarylimitofthediffusionequationiscalledtheLaplaceequationandarisesinaverywiderangeofapplicationsthroughoutthesciences.
It ispossibletosolvefor usingaexplicitscheme,butthetimesteprestrictionssoonbecomemuchlessfavorablethanforanexplicitschemeforthewaveequation.Andofmoreimportance,sincethesolutionof the diffusion equation is very smooth and changes slowly, small time steps are not convenient and notrequiredbyaccuracyasthediffusionprocessconvergestoastationarystate.
The initial-boundary value problem for 1D diffusionToobtainauniquesolutionof thediffusionequation,orequivalently, toapplynumericalmethods,weneedinitialandboundaryconditions.Thediffusionequationgoeswithoneinitialcondition ,where isaprescribed function.Oneboundarycondition is requiredateachpointon theboundary,which in1D
meansthat mustbeknown, mustbeknown,orsomecombinationofthem.
We shall start with the simplest boundary condition: . The complete initialboundary value diffusionprobleminonespacedimensioncanthenbespecifiedas
Equation (28) isknownasaonedimensionaldiffusionequation, alsooften referred toasaheatequation.Withonlyafirstorderderivativeintime,onlyoneinitialconditionisneeded,whilethesecondorderderivativeinspaceleadstoademandfortwoboundaryconditions.Theparameter mustbegivenandisreferredtoasthediffusioncoefficient.
Diffusionequationslike(28)haveawiderangeofapplications throughoutphysical,biological,and financialsciences. One of the most common applications is propagation of heat, where represents thetemperatureofsomesubstanceatpoint andtime .Thesectiondiffu:appgoesintoseveralwidelyoccurringapplications.
C
1
0
1
4
14 0 4 0
C 1
C1
0
1
44
1
00
1
44
1
1
1
00
1
44
41c 0 1
0
1c
4 1c
14 0
1
14 4
1 1
4
1
C 4
`0 >
1
0
1
4
14 4
4
1 0 0
1 0 0
C
14 0
4 0
10/02/2015 The 1D diffusion equation
http://hplgit.github.io/num-methods-for-PDEs/doc/pub/diffu/sphinx/._main_diffu001.html 2/23
(5)
(6)
(7)
(8)
Forward Euler schemeThefirststepinthediscretizationprocedureistoreplacethedomain byasetofmeshpoints.Hereweapplyequallyspacedmeshpoints
and
Moreover, denotes the mesh function that approximates for and.RequiringthePDE(28)tobefulfilledatameshpoint leadstotheequation
Thenextstep is toreplacethederivativesbyfinitedifferenceapproximations.Thecomputationallysimplestmethodarisesfromusingaforwarddifferenceintimeandacentraldifferenceinspace:
Writtenout,
WehaveturnedthePDEintoalgebraicequations,alsooftencalleddiscreteequations.Thekeypropertyoftheequationsisthattheyarealgebraic,whichmakesthemeasytosolve.Asusual,weanticipatethat isalreadycomputedsuchthat istheonlyunknownin(7).Solvingwithrespecttothisunknowniseasy:
isthekeyparameterinthediscretediffusionequation: Notethat isadimensionlessnumberthatlumpsthekeyphysicalparameterintheproblem, ,andthediscretizationparameters andintoasingleparameter.Allthepropertiesofthenumericalmethodarecriticallydependentuponthevalueof
(seethesectiondiffu:pde1:analysisfordetails).
Thecomputationalalgorithmthenbecomes
1. compute$u^0_i=I(x_i)$for2. for :
1. apply(8)foralltheinternalspatialpoints2. settheboundaryvalues for and
ThealgorithmiscompactlyfullyspecifiedinPython:
x = linspace(0, L, Nx+1) # mesh points in spacedx = x[1] - x[0]t = linspace(0, T, Nt+1) # mesh points in timedt = t[1] - t[0]F = a*dt/dx**2u = zeros(Nx+1) # unknown u at new time levelu_1 = zeros(Nx+1) # u at the previous time level
# Set initial condition u(x,0) = I(x)for i in range(0, Nx+1): u_1[i] = I(x[i])
for n in range(0, Nt): # Compute u at inner mesh points
g
%4 %
4
%
4
*0 *
0
*
0
1
*
%
1 4
%
0
*
%
4
*
0
4
%
0
*
1 C 1
0
4
%
0
*
4
4
%
0
*
< 1 C 1
0
4
4
>
*
%
C
1
*
%
1
*
%
0
1
*
%
1
*
%
1
*
%
4
1
*
%
1
*
%
1
*
%
1
*
%
1
*
%
1
*
%
1
*
%
C 4 0
%
4
*
0
%
4
1
*
%
% %
4
10/02/2015 The 1D diffusion equation
http://hplgit.github.io/num-methods-for-PDEs/doc/pub/diffu/sphinx/._main_diffu001.html 3/23
(9)
(10)
for i in range(1, Nx): u[i] = u_1[i] + F*(u_1[i-1] - 2*u_1[i] + u_1[i+1])
# Insert boundary conditions u[0] = 0; u[Nx] = 0
# Update u_1 before next step u_1[:]= u
Theprogramdiffu1D_u0.pycontainsafunctionsolver_FEforsolvingthe1Ddiffusionequationwith ontheboundary.Thefunctionsplugandgaussianrunsthecasewith asadiscontinuousplugorasmoothGaussianfunction,respectively.Experimentswiththesetwofunctionsrevealsomeimportantobservations:
TheForwardEulerschemeleadstogrowingsolutionsif .asadiscontinuousplugleadstoasawtoothlikenoisefor ,seemovie,which
isabsentfor ,seemovie.ThesmoothGaussianinitialfunctionleadstoasmoothsolution,seemoviefor .
Backward Euler schemeWenowapplyabackwarddifferenceintimein(5),butthesamecentraldifferenceinspace:
whichwrittenoutreads
Nowweassume iscomputed,butallquantitiesatthenewtimelevel areunknown.Thistimeitisnotpossibletosolvewithrespectto becausethisvaluecouplestoitsneighborsinspace, and ,which are also unknown. Let us examine this fact for the casewhen . Equation (10) written for
becomes
Theboundaryvalues and areknownaszero.Collectingtheunknownnewvalues and onthelefthandsidegives
Thisisacoupled systemofalgebraicequationsfortheunknowns and .Theequivalentmatrixformis
Implicitvs.explicitmethods: Discretizationmethodsthatleadtoacoupledsystemofequationsfortheunknownfunctionatanewtimelevelaresaidtobeimplicitmethods.Thecounterpart,explicitmethods,referstodiscretizationmethodswherethereisasimpleexplicitformulaforthevaluesoftheunknownfunctionateachofthespatialmeshpointsatthenewtimelevel.Fromanimplementationalpointofview,implicitmethodsaremorecomprehensivetocodesincetheyrequirethesolutionofcoupledequations,i.e.,
1
4
4
< 1 1
0
4
4
>
*
%
C
1
*
%
1
*
%
0
1
*
%
1
*
%
1
*
%
4
1
*
%
*
1
*
%
1
*
%
1
*
%
4
%
4
C
1
*
1
*
0
1
*
1
*
1
*
4
C
1
*
1
*
0
1
*
1
*
1
*
4
1
*
1
*
1
*
1
*
1
*
1
*
1
*
1
*
1
*
1
*
g 1
*
1
*
1
*
1
*
1
*
1
*
10/02/2015 The 1D diffusion equation
http://hplgit.github.io/num-methods-for-PDEs/doc/pub/diffu/sphinx/._main_diffu001.html 4/23
(11)
(12)
(13)
(14)
amatrixsystem,ateachtimelevel.
Inthegeneralcase,(10)givesrisetoacoupled systemofalgebraicequationsforalltheunknown attheinteriorspatialpoints .Collectingtheunknownsonthelefthandside,(10)canbewritten
for .Here,wehaveintroducedthemeshFouriernumber
One can either view these equations as a system for where the values at the internal mesh points,,areunknown,orwemayappendtheboundaryvalues and tothesystem.In
the latter case,all for are unknownandwemust add the boundary equations to theequationsin(11):
Acoupledsystemofalgebraicequationscanbewrittenonmatrixform,andthisisimportantifwewanttocallupreadymadesoftware forsolving thesystem.Theequations(11)and(12)(13)correspond to thematrixequation
where ,andthematrix hasthefollowingstructure:
Thenonzeroelementsaregivenby
fortheequationsforinternalpoints, .Theequationsfortheboundarypointscorrespondto
4 g 4
1
*
%
%
4
1
*
%
1
*
%
1
*
%
1
*
%
%
4
C
0
4
1
*
%
%
4
1
*
1
*
4
1
*
%
%
4
4
1
*
1
*
4
1
*
1
*
4
%%
%%
%%
4
4
4
4
4
4
%%
%%
%%
%
4
10/02/2015 The 1D diffusion equation
http://hplgit.github.io/num-methods-for-PDEs/doc/pub/diffu/sphinx/._main_diffu001.html 5/23
Therighthandside iswrittenas
with
Weobserve that thematrix containsquantities thatdonotchange in time.Therefore, canbe formedonceandforallbeforeweentertherecursiveformulasforthetimeevolution.Therighthandside ,however,mustbeupdatedateachtimestep.This leadstothefollowingcomputationalalgorithm,heresketchedwithPythoncode:
x = linspace(0, L, Nx+1) # mesh points in spacedx = x[1] - x[0]t = linspace(0, T, N+1) # mesh points in timeu = zeros(Nx+1) # unknown u at new time levelu_1 = zeros(Nx+1) # u at the previous time level
# Data structures for the linear systemA = zeros((Nx+1, Nx+1))b = zeros(Nx+1)
for i in range(1, Nx): A[i,i-1] = -F A[i,i+1] = -F A[i,i] = 1 + 2*FA[0,0] = A[Nx,Nx] = 1
# Set initial condition u(x,0) = I(x)for i in range(0, Nx+1): u_1[i] = I(x[i])
import scipy.linalg
for n in range(0, Nt): # Compute b and solve linear system for i in range(1, Nx): b[i] = -u_1[i] b[0] = b[Nx] = 0 u[:] = scipy.linalg.solve(A, b)
# Update u_1 before next step u_1[:] = u
Sparse matrix implementation
4
4
4
4
%
4
%
%
1
*
%
4
4
10/02/2015 The 1D diffusion equation
http://hplgit.github.io/num-methods-for-PDEs/doc/pub/diffu/sphinx/._main_diffu001.html 6/23
Wehaveseenfrom(14)thatthematrix istridiagonal.Thecodesegmentaboveusedafull,densematrixrepresentation of , which stores a lot of values we know are zero beforehand, and worse, the solutionalgorithm computes with all these zeros.With unknowns, the work by the solution algorithm is
and the storage requirements . By utilizing the fact that is tridiagonal andemployingcorrespondingsoftwaretools,theworkandstoragedemandscanbeproportionalto only.
Thekey idea is toapplyadatastructure fora tridiagonalorsparsematrix.Thescipy.sparsepackagehasrelevantutilities.Forexample,wecanstorethenonzerodiagonalsofamatrix.Thepackagealsohaslinearsystemsolvers thatoperateonsparsematrixdatastructures.Thecodebelow illustrateshowwecanstoreonlythemaindiagonalandtheupperandlowerdiagonals.
# Representation of sparse matrix and right-hand sidemain = zeros(Nx+1)lower = zeros(Nx-1)upper = zeros(Nx-1)b = zeros(Nx+1)
# Precompute sparse matrixmain[:] = 1 + 2*Flower[:] = -F #1upper[:] = -F #1# Insert boundary conditionsmain[0] = 1main[Nx] = 1
A = scipy.sparse.diags( diagonals=[main, lower, upper], offsets=[0, -1, 1], shape=(Nx+1, Nx+1), format='csr')print A.todense() # Check that A is correct
# Set initial conditionfor i in range(0,Nx+1): u_1[i] = I(x[i])
for n in range(0, Nt): b = u_1 b[0] = b[-1] = 0.0 # boundary conditions u[:] = scipy.sparse.linalg.spsolve(A, b) u_1[:] = u
Thescipy.sparse.linalg.spsolvefunctionutilizesthesparsestoragestructureofAandperformsinthiscaseaveryefficientGaussianeliminationsolve.
The program diffu1D_u0.py contains a function solver_BE, which implements the Backward Euler schemesketchedabove.AsmentionedinthesectionForwardEulerscheme,thefunctionsplugandgaussianrunsthecase with as a discontinuous plug or a smooth Gaussian function. All experiments point to twocharacteristicfeaturesoftheBackwardEulerscheme:1)itisalwaysstable,and2)italwaysgivesasmooth,decayingsolution.
Crank-Nicolson schemeTheideaintheCrankNicolsonschemeistoapplycentereddifferencesinspaceandtime,combinedwithanaverageintime.WedemandthePDEtobefulfilledatthespatialmeshpoints,butinbetweenthepointsinthetimemesh:
for and .
Withcentereddifferencesinspaceandtime,weget
4
4
4
4
4
1 C 1
0
4
%
0
*
4
4
%
0
*
%
4
*
0
< 1 C 1
0
4
4
>
*
%
10/02/2015 The 1D diffusion equation
http://hplgit.github.io/num-methods-for-PDEs/doc/pub/diffu/sphinx/._main_diffu001.html 7/23
Ontherighthandsidewegetanexpression
Thisexpressionisproblematicsince isnotoneoftheunknownwecompute.Apossibilityistoreplace
byanarithmeticaverage:
Inthecompactnotation,wecanusethearithmeticaveragenotation :
Afterwritingoutthedifferencesandaverage,multiplyingby ,andcollectingallunknowntermsonthelefthandside,weget
Alsohere,asintheBackwardEulerscheme,thenewunknowns , ,and arecoupled inalinearsystem ,where hasthesamestructureasin(14),butwithslightlydifferententries:
fortheequationsforinternalpoints, .Theequationsfortheboundarypointscorrespondto
Therighthandside hasentries
The ruleThe ruleprovidesafamilyoffinitedifferenceapproximationsintime:
givestheForwardEulerschemeintimegivestheBackwardEulerschemeintime
4
1
*
%
1
*
%
1
*
%
1
*
%
1
*
%
1
*
%
1
*
%
1
*
%
1
ccc
0
< 1 C
0
4
4
1
ccc
0
>
*
%
0
1
*
%
1
*
%
1
*
%
1
*
%
1
*
%
1
*
%
1
*
%
1
*
%
1
*
%
1
*
%
1
*
%
%%
%%
%%
%
4
4
4
4
4
%
%
1
*
%
4
4
J
J
J
J
J
10/02/2015 The 1D diffusion equation
http://hplgit.github.io/num-methods-for-PDEs/doc/pub/diffu/sphinx/._main_diffu001.html 8/23
givestheCrankNicolsonschemeintime
Appliedtothe1Ddiffusionproblemwehave
Thisschemealsoleadstoamatrixsystemwithentries
whilerighthandsideentry is
ThecorrespondingentriesfortheboundarypointsareasintheBackwardEulerandCrankNicolsonschemeslistedearlier.
The Laplace and Poisson equationTheLaplace equation, , or thePoisson equation, , occur in numerous applicationsthroughout science and engineering. In 1D these equations read and ,respectively.Wecan solve1Dvariantsof theLaplaceequationswith the listed software, becausewecaninterpret asthelimitingsolutionof when reachasteadystatelimitwhere .Similarly,Poissonsequation arisesfromsolving andletting so .
Technicallyinaprogram,wecansimulate byjusttakingonelargetimestep,orequivalently,settoalargevalue.Allweneedistohave large.As ,wecanfromtheschemesseethatthelimitingdiscreteequationbecomes
whichisnothingbutthediscretization of .
TheBackwardEuler scheme can solve the limit equation directly andhenceproducea solution of the 1DLaplaceequation.With theForwardEulerschemewemustdo the timesteppingsince is illegaland leads to instability.Wemay interpret this time stepping as solving the equation system from byiteratingonatimepseudotimevariable.
ExtensionsThese extensions are performed exactly as for awave equation as they only affect the spatial derivatives(whicharethesameasinthewaveequation).
VariablecoefficientsNeumannandRobinconditions2Dand3D
Future versions of this documentwill for completeness and independenceof thewaveequation documentfeatureinfoonthethreepoints.TheRobinconditionisnew,butstraightforwardtohandle:
J
CJ J
1
*
%
1
*
%
0
1
*
%
1
*
%
1
*
%
4
1
*
%
1
*
%
1
*
%
4
J J J
%%
%%
%%
%
J
%
1
*
%
1
*
%
1
*
%
1
*
%
4
1
1 "
4 1
4 "41
1
44
C1
0
1
44
1 1
0
"1
44
"1
0
1
44
0 1
0
0 C
1
*
%
1
*
%
1
*
%
4
< 1
4
4
>
*
%
1
44
1
44
C 1
*
%
10/02/2015 The 1D diffusion equation
http://hplgit.github.io/num-methods-for-PDEs/doc/pub/diffu/sphinx/._main_diffu001.html 9/23
(16)
(17)
(18)
(19)
(20)
(15)
Analysis of schemes for the diffusion equation
Properties of the solutionAparticularcharacteristicofdiffusiveprocesses,governedbyanequationlike
is that the initialshape spreadsout inspacewith time,alongwithadecayingamplitude.Threedifferentexampleswillillustratethespreadingof inspaceandthedecayintime.
Similarity solution
Thediffusionequation (15)admits solutions that dependon for a given value of .Oneparticularsolutionis
where
istheerrorfunction,and and arearbitraryconstants.Theerrorfunctionliesin , isoddaround,andgoesrelativelyquicklyto :
As ,theerrorfunctionapproachesastepfunctioncenteredat .Foradiffusionproblemposedon the unit interval , we may choose the step at (meaning ), ,
.Then
where we have introduced the complementary error function . The solution (18)impliestheboundaryconditions
Forsmallenough , and ,butas , on .
C 1
0
1
44
14 4
1
I 4 C0
14 0 FSGI
FSGI H
R
I
!
H
I e
FSGIMJN
I
FSGIMJN
I
FSGI
FSG
FSG
FSG
FSGI
0 4
4
14 0 FSG FSGD
4
C0
4
C0
FSGDI FSGI
1 0 FSG
C0
1 0 FSG
C0
0 1 0 1 0 0 14 0
10/02/2015 The 1D diffusion equation
http://hplgit.github.io/num-methods-for-PDEs/doc/pub/diffu/sphinx/._main_diffu001.html 10/23
(21)
(22)
(23)
Solution for a Gaussian pulseThestandarddiffusionequation admitsaGaussianfunctionassolution:
At this isaDiracdeltafunction,soforcomputationalpurposesonemuststarttoviewthesolutionatsometime .Replacing by in(21)makesiteasytooperatewitha(new) thatstartsat
withaninitialconditionwithafinitewidth.Theimportantfeatureof(21)isthatthestandarddeviationofasharp initialGaussianpulse increases in timeaccording to ,making thepulsediffuseandflattenout.
Solution for a sine componentForexample,(15)admitsasolutionoftheform
Theparameters and canbefreelychosen,whileinserting(22)in(15)givestheconstraint
Avery important feature is that the initial shape undergoesadamping ,meaningthatrapidoscillationsinspace,correspondingtolarge ,areverymuchfasterdampenedthanslowoscillationsinspace,correspondingtosmall .Thisfeatureleadstoasmoothingoftheinitialconditionwithtime.
Thefollowingexamplesillustratesthedampingpropertiesof(22).Weconsiderthespecificproblem
Theinitialconditionhasbeenchosensuchthataddingtwosolutionslike(22)constructsananalyticalsolutiontotheproblem:
FigureEvolutionofthesolutionofadiffusionproblem:initialcondition(upperleft),1/100reductionofthesmallwaves(upperright),1/10reductionofthelongwave(lowerleft),and1/100reductionofthelongwave(lowerright)illustratestherapiddampingofrapidoscillations andtheverymuchslowerdampingoftheslowly varying term. After about the rapid oscillations do not have a visibleamplitude,whilewehavetowaituntil beforetheamplitudeofthelongwave becomesverysmall.
C1
0
1
44
14 0 FYQ
RC0
4
C0
0
0 0
`
0 00
`
0
0 U
U C0
14 0 TJO '4!
0
'
C '
4 TJO'4 FYQ C 0'
'
'
1
0
1 0
14
4
`0 >1
44
1 0 0 >
TJOR4 TJOR4
14 0 TJOR4 TJOR4!
0R
!
0R
TJOR4
TJOR4 0
0 TJOR4
10/02/2015 The 1D diffusion equation
http://hplgit.github.io/num-methods-for-PDEs/doc/pub/diffu/sphinx/._main_diffu001.html 11/23
Evolutionofthesolutionofadiffusionproblem:initialcondition(upperleft),1/100reductionofthesmallwaves(upperright),1/10reductionofthelongwave(lowerleft),and1/100reductionofthelongwave(lowerright)
Example: Diffusion of a discontinues profileWeshallseehowdifferentschemespredicttheevolutionofadiscontinuousinitialcondition:
Suchadiscontinuousinitialconditionmayarisewhentwoinsulatedblocksofmetalsatdifferenttemperatureare brought in contact at . Alternatively, signaling in the brain is based on release of a huge ionconcentration on one side of a synapse,which implies diffusive transport of a discontinuous concentrationfunction.
Moretobewritten...
Analysis of discrete equationsAcounterpartto(22)isthecomplexrepresentationofthesamefunction:
where istheimaginaryunit.Wecanaddsuchfunctions,oftenreferredtoaswavecomponents,tomakeaFourierrepresentationofageneralsolutionofthediffusionequation:
14
4
4
0
14 0 !
0
!
%'4
%
14 0
'
C 0
%'4
10/02/2015 The 1D diffusion equation
http://hplgit.github.io/num-methods-for-PDEs/doc/pub/diffu/sphinx/._main_diffu001.html 12/23
(24)
(25)
where isasetofaninfinitenumberof valuesneededtoconstructthesolution.Inpractice,however,theseriesistruncatedand isafinitesetof valuesneedbuildagoodapproximatesolution.Notethat(23)isaspecialcaseof(24)where , ,and .
Theamplitudes oftheindividualFourierwavesmustbedeterminedfromtheinitialcondition.At wehave andfind and suchthat
(The relevant formulas for come from Fourier analysis, or equivalently, a leastsquares method forapproximating inafunctionspacewithbasis .)
Much insight about the behavior of numerical methods can be obtained by investigating how a wavecomponent is treated by the numerical scheme. It appears that such wavecomponents are also solutions of the schemes, but the damping factor varies among theschemes.Toeasetheforthcomingalgebra,wewritethedampingfactoras .Theexactamplificationfactorcorrespondingto is .
Analysis of the finite difference schemesWehaveseenthatageneralsolutionofthediffusionequationcanbebuiltasalinearcombinationofbasiccomponents
Afundamentalquestioniswhethersuchcomponentsarealsosolutionsofthefinitedifferenceschemes.Thisisindeedthecase,buttheamplitude mightbemodified(whichalsohappenswhensolvingtheODEcounterpart ).Wethereforelookfornumericalsolutionsoftheform
wheretheamplificationfactor mustbedeterminedbyinsertingthecomponentintoanactualscheme.
Stability (1)
Theexactamplificationfactoris .Weshouldthereforerequire tohaveadecayingnumericalsolutionaswell.If , willchangesignfromtimeleveltotimelevel,andwegetstable,nonphysicaloscillationsinthenumericalsolutionsthatarenotpresentintheexactsolution.
Accuracy (1)Todeterminehowaccurately a finite difference scheme treats onewave component (25),we see that thebasic deviation from the exact solution is reflected in how well approximates , or how wellapproximates .
Analysis of the Forward Euler schemeTheForwardEulerfinitedifferenceschemefor canbewrittenas
Insertingawavecomponent(25)intheschemedemandscalculatingtheterms
14 0
'
'
!
C 0'
!
%'4
'
'
\R R^
R
R
'
0
1 FYQ %'4
'
'
'
4
'
'
!
%'4
'
4 FYQ %'4
FYQ C 0 FYQ %'4'
FYQ C 0'
*
FYQ C 0
F
'
!
C 0'
!
%'4
FYQ C 0'
C11
1
*
-
*
!
%'-4
*
!
%'4
FYQ 0
F
C
'
]]
*
*
F
*
F
C1
0
1
44
< 1 C 1
0
4
4
>
*
-
<
%'-4 * %'-4 *
10/02/2015 The 1D diffusion equation
http://hplgit.github.io/num-methods-for-PDEs/doc/pub/diffu/sphinx/._main_diffu001.html 13/23
and
Insertingthesetermsinthediscreteequationanddividingby leadsto
andconsequently
where
isthenumericalFouriernumber.Thecompletenumericalsolutionisthen
Stability (2)
Weeasilyseethat .However,the canbelessthan ,whichwill leadtogrowthofanumericalwavecomponent.Thecriterion implies
Theworstcaseiswhen ,soasufficientcriterionforstabilityis
orexpressedasaconditionon :
Notethathalvingthespatialmeshsize, ,requires tobereducedbyafactorof .Themethodhencebecomesveryexpensiveforfinespatialmeshes.
Accuracy (2)
Since isexpressedintermsof andtheparameterwenowcall ,wealsoexpress byand :
ComputingtheTaylorseriesexpansionof intermsof caneasilybedonewithaidofsympy:
< !
%'-4
0
>
*
!
%'-4
*
0
<
*
4
4
!
%'4
>
-
*
!
%'-4
4
TJO
'4
*
!
%'-4
C
0
4
TJO
'4
TJO
'4
C0
4
1
*
-
TJO
'4
*
!
%'-4
, TJO
, TJO
0
0
4
C
4 4
0
, '4
F
,
FYQ C 0 FYQ
F
'
,
F
10/02/2015 The 1D diffusion equation
http://hplgit.github.io/num-methods-for-PDEs/doc/pub/diffu/sphinx/._main_diffu001.html 14/23
(26)
def A_exact(F, p): return exp(-4*F*p**2)
def A_FE(F, p): return 1 - 4*F*sin(p)**2
from sympy import *F, p = symbols('F p')A_err_FE = A_FE(F, p)/A_exact(F, p)print A_err_FE.series(F, 0, 6)
Theresultis
Recalling that , , and that , we realize that the dominating errortermsareatmost
Analysis of the Backward Euler schemeDiscretizing byaBackwardEulerscheme,
and insertingawavecomponent (25), leads tocalculationssimilar to thosearising from theForwardEulerscheme,butsince
weget
andthen
Thecompletenumericalsolutioncanbewritten
Stability (3)
Weseefrom(26)that ,whichmeansthatallnumericalwavecomponentsarestableandnonoscillatoryforany .
Analysis of the Crank-Nicolson schemeTheCrankNicolsonschemecanbewrittenas
, ,
F
TJO
,
,
TJO
,
C04 , '4 , TJO
C C0
0
4
C
0
C
0
4
C1
0
1
44
< 1 C 1
0
4
4
>
*
-
< !
%'-4
0
>
*
*
!
%'-4
0
C
0
4
TJO
'4
,TJO
1
*
-
,TJO
*
!
%'-4
0
< 1 C
0
4
4
1
ccc
4
>
*
-
10/02/2015 The 1D diffusion equation
http://hplgit.github.io/num-methods-for-PDEs/doc/pub/diffu/sphinx/._main_diffu001.html 15/23
or
Inserting(25)inthetimederivativeapproximationleadsto
Inserting(25)intheothertermsanddividingby givestherelation
andaftersomemorealgebra,
Theexactnumericalsolutionishence
Stability (4)
Thecriteria and arefulfilledforany .
Summary of accuracy of amplification factorsWecanplotthevariousamplificationfactorsagainst fordifferentchoicesofthe parameter.FiguresAmplificationfactorsforlargetimesteps,AmplificationfactorsfortimestepsaroundtheForwardEulerstabilitylimit,andAmplificationfactorsforsmall timestepsshowhowlongandsmallwavesaredampedbythevariousschemescompared to theexactdamping.As longasall schemesarestable, theamplificationfactorispositive,exceptforCrankNicolsonwhen .
Amplificationfactorsforlargetimesteps
< 1 C < 1 < 1
0
>
*
-
4
4
>
*
-
4
4
>
*
-
<
0
*
!
%'-4
>
*
*
!
%'-4
0
*
!
%'-4
0
*
!
%'-4
C
0
4
TJO
'4
,TJO
,TJO
1
*
-
,TJO
,TJO
*
!
%',4
0
, '4
10/02/2015 The 1D diffusion equation
http://hplgit.github.io/num-methods-for-PDEs/doc/pub/diffu/sphinx/._main_diffu001.html 16/23
AmplificationfactorsfortimestepsaroundtheForwardEulerstabilitylimit
Amplificationfactorsforsmalltimesteps
Theeffectofnegativeamplificationfactorsisthat changessignfromonetimeleveltothenext,therebygivingrisetooscillationsintimeinananimationofthesolution.WeseefromFigureAmplificationfactorsforlargetimestepsthatfor ,waveswith undergoadampingcloseto ,whichmeansthattheamplitudedoesnotdecayandthatthewavecomponentjumpsupanddownintime.For wehavea damping of a factor of 0.5 from one time level to the next, which is very much smaller than the exactdamping.Shortwaveswillthereforefailtobeeffectivelydampened.Thesewaveswillmanifestthemselvesashighfrequencyoscillatorynoiseinthesolution.
Avalue correspondstofourmeshpointsperwavelengthof ,while impliesonlytwopointsperwave length,which is thesmallestnumberofpointswecanhave to represent thewaveon themesh.
Todemonstrate the oscillatory behavior of theCrankNicolson scheme,we choosean initial condition thatleadstoshortwaveswithsignificantamplitude.Adiscontinuous willinparticularservethispurpose.
Run ...
Exercise 1: Use an analytical solution to formulate a 1D testThisexerciseexplorestheexactsolution(21).Weshallformulateadiffusionprobleminhalfofthedomainforhalf of theGaussianpulse.Thenweshall investigate the impact of usingan incorrect boundary condition,which we in general cases often are forced due if the solution needs to pass through finite boundariesundisturbed.
*
, R
, R !
%'4
, R
4
14
10/02/2015 The 1D diffusion equation
http://hplgit.github.io/num-methods-for-PDEs/doc/pub/diffu/sphinx/._main_diffu001.html 17/23
(27)
a)Thesolution(21) isseentobesymmetricat ,because alwaysvanishesfor .Usethispropertytoformulateacompleteinitialboundaryvalueproblemin1Dinvolvingthediffusionequation
on with and known.
b)Usetheexactsolutiontosetupaconvergenceratetestforanimplementationoftheproblem.Investigateifaonesideddifferencefor ,say ,destroysthesecondorderaccuracyinspace.
c) Imaginethatwewanttosolvetheproblemnumericallyon ,butwedonotknowtheexactsolutionand cannot of that reason assign a correct Dirichlet condition at . One idea is to simply set
since thiswill be an accurate approximation before the diffused pulse reaches andeventhereafteritmightbeasatisfactorycondition.Let betheexactsolutionandlet bethesolutionof
withaninitialGaussianpulseandtheboundaryconditions .Deriveadiffusionproblemfortheerror .SolvethisproblemnumericallyusinganexactDirichletconditionat .Animatetheevolutionoftheerrorandmakeacurveplotoftheerrormeasure
Isthisasuitableerrormeasureforthepresentproblem?
d)Insteadofusing asapproximateboundaryconditionforlettingthediffusedGaussianpulseoutofourfinitedomain,onemaytry sincethesolutionforlarge isquiteflat.Arguethatthisconditiongivesacompletelywrongasymptoticsolutionas .Todothis,integratethediffusionequationfrom to , integrate by parts (or useGauss divergence theorem in 1D) to arrive at the importantproperty
implyingthat mustbeconstantintime,andtherefore
Theintegraloftheinitialpulseis1.
e)Anotherideaforanartificialboundaryconditionat istouseacoolinglaw
where isanunknownheattransfercoefficientand isthesurroundingtemperatureinthemediumoutsideof .(Notethatarguingthat isapproximately givesthe conditionfromtheprevioussubexercise that isqualitativelywrong for large .)Developadiffusionproblem for theerror in thesolutionusing (27) as boundary condition. Assume one can take outside the domain as for
.Findafunction suchthattheexactsolutionobeysthecondition(27).Testsomeconstantvalues of and animate how the corresponding error function behaves. Also compute curves assuggestedinsubexerciseb).
Filename:diffu_symmetric_gaussian.py.
Exercise 2: Use an analytical solution to formulate a 2D testGeneralize(21) tomultidimensionsbyassuming thatonedimensionalsolutionscanbemultiplied tosolve
.Usethissolutiontoformulatea2DtestcasewherethepeakoftheGaussianisattheoriginandwherethedomainisarectanguleinthefirstquadrant.Usesymmetryboundaryconditionswhereever possible, and use exact Dirichlet conditions on the remaining boundaries. Filename:
4 14 4
C1
0
1
44
0 1
4
1 0
01
4
1
1
4
1 0 4
1
F
1
C1
0
1
44
0 1 0 1
4
! 11
F
4
0
4
!
1 4
+
'
*
*
1 0
0 1
4
0
0
1
44
14 0 4
0
1 4
14 0 4 4 4
4
C -1
1
4
1
- 1
1
1 0 1
4
0
1
1
4 - -0
- 0
C 11
0
1*
10/02/2015 The 1D diffusion equation
http://hplgit.github.io/num-methods-for-PDEs/doc/pub/diffu/sphinx/._main_diffu001.html 18/23
(28)
(29)
(30)
(31)
(32)
(33)
diffu_symmetric_gaussian_2D.pdf.
Exercise 3: Examine stability of a diffusion model with a source termConsideradiffusionequationwithalinear term:
a)DeriveindetailaForwardEulerscheme,aBackwardEulerscheme,andaCrankNicolsonforthistypeofdiffusionmodel.Thereafter,formulatea ruletosummarizethethreeschemes.
b)Assumeasolutionlike(22)andfindtherelationbetween , , ,and .
c)CalculatethestabilityoftheForwardEulerscheme.Designnumericalexperimentstoconfirmtheresults.
d)Repeatc)fortheBackwardEulerscheme.
e)Repeatc)fortheCrankNicolsonscheme.
f)Howdoestheextraterm impacttheaccuracyofthethreeschemes?
Hint.Comparethenumericalandexactamplificationfactors,either ingraphsorbyTaylorseriesexpansion(orboth).
Filename:diffu_stab_uterm.pdf.
Diffusion in heterogeneous mediaDiffusion inheterogeneousmediawillnormally implyanonconstantdiffusioncoefficient .A1Ddiffusionmodelwithsuchavariablediffusioncoefficientreads
Ashortformofthediffusionequationwithvariablecoefficientsis .
Stationary solutionAs , the solution of the above problem will approach a stationary limit where . Thegoverningequationisthen
withboundaryconditions and .Itispossibletoobtainanexactsolutionof(32) forany .Integratingtwiceandapplyingtheboundaryconditionstodeterminetheintegrationconstantsgives
Piecewise constant medium
1
C D1 1
0
1
44
J
' C D
1
C C4
C4 4
`0 >
1
0
4
1
4
14 4
4
1 0 0
1 0 0
C1
0
1
4
4
0 10
C
4
1
4
1
1 1
C
14
CP P
4
CP P
10/02/2015 The 1D diffusion equation
http://hplgit.github.io/num-methods-for-PDEs/doc/pub/diffu/sphinx/._main_diffu001.html 19/23
(34)
(35)
Consider a medium built of layers. The boundaries between the layers are denoted by ,where and .Ifthematerialineachlayerpotentiallydiffersfromtheothers,butisotherwiseconstant,wecanexpress asapiecewiseconstantfunctionaccordingto
Theexactsolution(33)incaseofsuchapiecewiseconstant functioniseasytoderive.Assumethat isinthe thlayer: .Intheintegral wemustintegratethroughthefirstlayersandthenaddthecontributionfromtheremainingpart intothe thlayer:
Remark. It may sound strange to have a discontinuous in a differential equation where one is todifferentiate,butadiscontinuous iscompensatedbyadiscontinuous suchthat iscontinuesandthereforecanbedifferentiatedas .
ImplementationProgrammingwithpiecewisefunctiondefinitionquicklybecomescumbersomeasthemostnaiveapproachistotestforwhichinterval lies,andthenstartevaluatingaformulalike(35).InPython,vectorizedexpressionsmay help to speed up the computations. The convenience classes PiecewiseConstant andIntegratedPiecewiseConstantweremadetosimplifyprogrammingwithfunctionslike(34)andexpressionslike(35). These utilities not only represent piecewise constant functions, but also smoothed versions of themwhere the discontinuities can be smoothed out in a controlled fashion. This is advantageous in manycomputationalcontexts(althoughseldomforpurefinitedifferencecomputationsofthesolution ).
ThePiecewiseConstant class is created by sending in the domain as a 2tuple or 2list and a dataobjectdescribing the boundaries and the corresponding function values . Moreprecisely,dataisanestedlist,wheredata[i][0]holds anddata[i][1]holdsthecorrespondingvalue ,for .Given and inarraysbanda,itiseasytofilloutthenestedlistdata.
Inourapplication,wewanttorepresent and aspiecewiseconstantfunction,inadditiontothefunction which involves the integrals of . A class creating the functions we need and a method forevaluating ,cantaketheform
class SerialLayers: """ b: coordinates of boundaries of layers, b[0] is left boundary and b[-1] is right boundary of the domain [0,L]. a: values of the functions in each layer (len(a) = len(b)-1). U_0: u(x) value at left boundary x=0=b[0]. U_L: u(x) value at right boundary x=L=b[0]. """
def __init__(self, a, b, U_0, U_L, eps=0): self.a, self.b = np.asarray(a), np.asarray(b) self.eps = eps # smoothing parameter for smoothed a self.U_0, self.U_L = U_0, U_L
a_data = [[bi, ai] for bi, ai in zip(self.b, self.a)] domain = [b[0], b[-1]] self.a_func = PiecewiseConstant(domain, a_data, eps)
C
C4
"
!
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
C
C
%
C
4
4
%
%
4
C 4
) 4 < >
)
)
P P
4
)
4
)
)
14
C 4 C
)
&
&
&
&
)
)
C
&
&
&
&
C
C 1
4
C1
4
C1
4
4
4
1
C
C
%
C
%
%
%
C
%
C C 14
C
1
10/02/2015 The 1D diffusion equation
http://hplgit.github.io/num-methods-for-PDEs/doc/pub/diffu/sphinx/._main_diffu001.html 20/23
# inv_a = 1/a is needed in formulas inv_a_data = [[bi, 1./ai] for bi, ai in zip(self.b, self.a)] self.inv_a_func = \ PiecewiseConstant(domain, inv_a_data, eps) self.integral_of_inv_a_func = \ IntegratedPiecewiseConstant(domain, inv_a_data, eps) # Denominator in the exact formula is constant self.inv_a_0L = self.integral_of_inv_a_func(b[-1])
def __call__(self, x): solution = self.U_0 + (self.U_L-self.U_0)*\ self.integral_of_inv_a_func(x)/self.inv_a_0L return solution
Avisualizationmethodisalsoconvenienttohave.Belowweplot alongwith (whichworkswellaslongas isofthesamesizeas ).
class SerialLayers: ...
def plot(self): x, y_a = self.a_func.plot() x = np.asarray(x); y_a = np.asarray(y_a) y_u = self.u_exact(x) import matplotlib.pyplot as plt plt.figure() plt.plot(x, y_u, 'b') plt.hold('on') # Matlab style plt.plot(x, y_a, 'r') ymin = -0.1 ymax = 1.2*max(y_u.max(), y_a.max()) plt.axis([x[0], x[-1], ymin, ymax]) plt.legend(['solution $u$', 'coefficient $a$'], loc='upper left') if self.eps > 0: plt.title('Smoothing eps: %s' % self.eps) plt.savefig('tmp.pdf') plt.savefig('tmp.png') plt.show()
FigureSolutionofthestationarydiffusionequationcorrespondingtoapiecewiseconstantdiffusioncoefficientshowsthecasewhere
b = [0, 0.25, 0.5, 1] # material boundariesa = [0.2, 0.4, 4] # material valuesU_0 = 0.5; U_L = 5 # boundary conditions
Solutionofthestationarydiffusionequationcorrespondingtoapiecewiseconstantdiffusioncoefficient
ByaddingtheepsparametertotheconstructoroftheSerialLayersclass,wecanexperimentwithsmoothedversions of and see the (small) impact on . Figure Solution of the stationary diffusion equationcorrespondingtoa*smoothedpiecewiseconstantdiffusioncoefficient*showstheresult.
14 C4
NBYC4 NBY1 NBY
C 1
10/02/2015 The 1D diffusion equation
http://hplgit.github.io/num-methods-for-PDEs/doc/pub/diffu/sphinx/._main_diffu001.html 21/23
(36)
(37)
(38)
(39)
Solutionofthestationarydiffusionequationcorrespondingtoa*smoothedpiecewiseconstantdiffusioncoefficient*
Exercises
Exercise 4: Stabilizing the Crank-Nicolson method by Rannacher timesteppingIt iswellknownthat theCrankNicolsonmethodmaygiverise tononphysicaloscillations in thesolutionofdiffusionequationsif the initialdataexhibit jumps(seethesectionAnalysisof theCrankNicolsonscheme).Rannacher[Ref1]suggestedastabilizing techniqueconsistingofusing theBackwardEulerscheme for thefirsttwotimestepswithsteplength .Onecangeneralizethisideatotaking timestepsofsizewith theBackwardEulermethodand thencontinuingwith theCrankNicolsonmethod,which isof secondorder in time. The idea is that the high frequencies of the initial solution are quickly damped out, and theBackwardEulerschemetreatsthesehighfrequenciescorrectly.Thereafter,thehighfrequencycontentofthesolutionisgoneandtheCrankNicolsonmethodwilldowell.
Test this idea for on a diffusion problem with a discontinuous initial condition. Measure theconvergence rateusing the solution (18)with the boundary conditions (19)(20) for values such that theconditionsareinthevicinityof .Forexample, makesthesolutiondiffusionfromasteptoalmostastraightline.Theprogramdiffu_erf_sol.pyshowshowtocomputetheanalyticalsolution.
Project 5: Energy estimates for diffusion problemsThis project concerns socalled energy estimates for diffusion problems that can be used for qualitativeanalyticalinsightandforverificationofimplementations.
a)Westartwitha1DhomogeneousdiffusionequationwithzeroDirichletconditions:
Theenergyestimateforthisproblemreads
wherethe normisdefinedby
0
) 0
)
0
e 0
C 44
`0 >1
0
1
4
1 0 1 0 0 >
14 4
4
]]1] ]]] ]
]
]] ]]
]]#]
10/02/2015 The 1D diffusion equation
http://hplgit.github.io/num-methods-for-PDEs/doc/pub/diffu/sphinx/._main_diffu001.html 22/23
(40)
(41)
(42)
(43)
(44)
(45)
(46)
(47)
(48)
Thequantify or isknownastheenergyofthesolution,althoughitisnotthephysicalenergyofthesystem.Amathematicaltraditionhasintroducedthenotionenergyinthiscontext.
Theestimate(39)saysthatthesizeof$u$neverexceedsthatoftheinitialcondition,ormoreequivalently,thattheareaunderthe curvedecreaseswithtime.
Toshow(39),multiplythePDEby andintegratefrom to .Usethat canbeexpressedasthetimederivative of and that can integrated by parts to form an integrand . Show that the timederivativeof mustbelessthanorequaltozero.Integratethisexpressionandderive(39).
b)Nowweaddressaslightlydifferentproblem,
Theassociatedenergyestimateis
(Thisresultismoredifficulttoderive.)
Nowconsiderthecompoundproblemwithaninitialcondition andarighthandside :
Show that if fulfills (36)(38)and fulfills (41)(43), then is thesolutionof (45)(47).Usingthetriangleinequalityfornorms,
showthattheenergyestimatefor(45)(47)becomes
c)Oneapplicationof(48) is toproveuniquenessof thesolution.Suppose and both fulfill (45)(47).Showthat thenfulfills(45)(47)with and .Use(48) todeducethat theenergymustbezeroforalltimesandthereforethat ,whichprovesthatthesolutionisunique.
d)Generalize(48)toa2D/3Ddiffusionequation for .
Hint.Useintegrationbypartsinmultidimensions:
where , beingtheoutwardunitnormaltotheboundary ofthedomain .
e)NowwealsoconsiderthemultidimensionalPDE .Integratebothsidesover anduse
]]#] ]
4
#
]]1]]
]]1]
]
1
1 11
0
1
411
4
1
4
]]1]]
C 4 "4 0
4
`0 >1
0
1
4
1 0 1 0 0 >
14 4
]]1] ]]"] ]
]
4 "4 0
C 4 "4 0
4
`0 >1
0
1
4
1 0 1 0 0 >
14 4
4
3
3
1 3
3
]] ]] ]]]] ]]]]
]]1] ]]] ]]"] ]
]
]
1
1
1 1
1
"
1
1
C11
0
4
1 C1 E4 C1 1E4 1C
1
*
* 1
1
*
*
C11
0
-E4 - *E/
10/02/2015 The 1D diffusion equation
http://hplgit.github.io/num-methods-for-PDEs/doc/pub/diffu/sphinx/._main_diffu001.html 23/23
(49)
Gauss divergence theorem, for a vector field . Show that if we havehomogeneous Neumann conditions on the boundary, , area under the surface remainsconstantintimeand
f)Establishacodein1D,2D,or3Dthatcansolveadiffusionequationwithasourceterm ,initialcondition,andzeroDirichletorNeumannconditionsonthewholeboundary.
Wecanuse(48)and(49)asapartialverificationofthecode.Choosesomefunctions and andcheckthat(48) isobeyedatany timewhenzeroDirichlet conditionsareused. Iterateover thesame functionsandcheckthat(49)isfulfilledwhenusingzeroNeumannconditions.
g)Makealistofsomepossiblebugsinthecode,suchasindexingerrorsinarrays,failuretosetthecorrectboundaryconditions,evaluationofatermatawrongtimelevel,andsimilar.Foreachofthebugs,seeiftheverificationtestsfromtheprevioussubexercisepassorfail.Thisinvestigationshowshowstrongtheenergyestimatesandtheestimate(49)areforpointingouterrorsintheimplementation.
Filename:diffu_energy.pdf.
-E4 - *E/
-
1* 1
1E4 E4
"
"