Solving the advection PDE in explicit FTCS, Lax, Implicit FTCS and Crank-Nicolson methods for constant and varying speed.

Similar documents
Mixed and Implicit Schemes Implicit Schemes. Exercise: Verify that ρ is unimodular: ρ = 1.

Building a Dynamic Two Dimensional Heat Transfer Model part #1

Kernel Density Estimation. Let X be a random variable with continuous distribution F (x) and density f(x) = d

Limits of sequences. Contents 1. Introduction 2 2. Some notation for sequences The behaviour of infinite sequences 3

1 Estimating sensitivities

Statistics for Economics & Business


Hopscotch and Explicit difference method for solving Black-Scholes PDE

Combining imperfect data, and an introduction to data assimilation Ross Bannister, NCEO, September 2010

Chapter 8. Confidence Interval Estimation. Copyright 2015, 2012, 2009 Pearson Education, Inc. Chapter 8, Slide 1

Overlapping Generations

The material in this chapter is motivated by Experiment 9.

Calculation of the Annual Equivalent Rate (AER)

point estimator a random variable (like P or X) whose values are used to estimate a population parameter

Sequences and Series

11.7 (TAYLOR SERIES) NAME: SOLUTIONS 31 July 2018

An Empirical Study of the Behaviour of the Sample Kurtosis in Samples from Symmetric Stable Distributions

We learned: $100 cash today is preferred over $100 a year from now

Outline. Plotting discrete-time signals. Sampling Process. Discrete-Time Signal Representations Important D-T Signals Digital Signals

CAPITAL PROJECT SCREENING AND SELECTION

Using Math to Understand Our World Project 5 Building Up Savings And Debt

When you click on Unit V in your course, you will see a TO DO LIST to assist you in starting your course.

Research Article The Probability That a Measurement Falls within a Range of n Standard Deviations from an Estimate of the Mean

APPLICATION OF GEOMETRIC SEQUENCES AND SERIES: COMPOUND INTEREST AND ANNUITIES

EXERCISE - BINOMIAL THEOREM

Models of Asset Pricing

Models of Asset Pricing

5. Best Unbiased Estimators

Minhyun Yoo, Darae Jeong, Seungsuk Seo, and Junseok Kim


FOUNDATION ACTED COURSE (FAC)

Models of Asset Pricing

The Time Value of Money in Financial Management

AUTOMATIC GENERATION OF FUZZY PAYOFF MATRIX IN GAME THEORY

Subject CT5 Contingencies Core Technical. Syllabus. for the 2011 Examinations. The Faculty of Actuaries and Institute of Actuaries.

Online appendices from The xva Challenge by Jon Gregory. APPENDIX 10A: Exposure and swaption analogy.

A random variable is a variable whose value is a numerical outcome of a random phenomenon.

Appendix 1 to Chapter 5

EVEN NUMBERED EXERCISES IN CHAPTER 4

Estimating Proportions with Confidence

Rafa l Kulik and Marc Raimondo. University of Ottawa and University of Sydney. Supplementary material

Maximum Empirical Likelihood Estimation (MELE)

of Asset Pricing R e = expected return

Chapter 8: Estimation of Mean & Proportion. Introduction

Institute of Actuaries of India Subject CT5 General Insurance, Life and Health Contingencies

Topic-7. Large Sample Estimation

Online appendices from Counterparty Risk and Credit Value Adjustment a continuing challenge for global financial markets by Jon Gregory

ii. Interval estimation:

Today: Finish Chapter 9 (Sections 9.6 to 9.8 and 9.9 Lesson 3)

x satisfying all regularity conditions. Then

CHAPTER 2 PRICING OF BONDS

Monetary Economics: Problem Set #5 Solutions

Sampling Distributions and Estimation

of Asset Pricing APPENDIX 1 TO CHAPTER EXPECTED RETURN APPLICATION Expected Return

The Limit of a Sequence (Brief Summary) 1

STRAND: FINANCE. Unit 3 Loans and Mortgages TEXT. Contents. Section. 3.1 Annual Percentage Rate (APR) 3.2 APR for Repayment of Loans

Faculdade de Economia da Universidade de Coimbra

MA Lesson 11 Section 1.3. Solving Applied Problems with Linear Equations of one Variable

MATH : EXAM 2 REVIEW. A = P 1 + AP R ) ny

INTERVAL GAMES. and player 2 selects 1, then player 2 would give player 1 a payoff of, 1) = 0.

Chapter 5: Sequences and Series

Introduction to Probability and Statistics Chapter 7

Inferential Statistics and Probability a Holistic Approach. Inference Process. Inference Process. Chapter 8 Slides. Maurice Geraghty,

Problem Set 1a - Oligopoly

CAPITAL ASSET PRICING MODEL

Journal of Statistical Software

0.1 Valuation Formula:

Parametric Density Estimation: Maximum Likelihood Estimation

0.07. i PV Qa Q Q i n. Chapter 3, Section 2

1 + r. k=1. (1 + r) k = A r 1

DESCRIPTION OF MATHEMATICAL MODELS USED IN RATING ACTIVITIES

A New Approach to Obtain an Optimal Solution for the Assignment Problem

Further Pure 1 Revision Topic 5: Sums of Series

A point estimate is the value of a statistic that estimates the value of a parameter.

Binomial Model. Stock Price Dynamics. The Key Idea Riskless Hedge

4.5 Generalized likelihood ratio test

CreditRisk + Download document from CSFB web site:

1 ECON4415: International Economics Problem Set 4 - Solutions

Optimizing of the Investment Structure of the Telecommunication Sector Company

Economic Computation and Economic Cybernetics Studies and Research, Issue 2/2016, Vol. 50

Math 312, Intro. to Real Analysis: Homework #4 Solutions

setting up the business in sage

Anomaly Correction by Optimal Trading Frequency

Chapter 4 - Consumer. Household Demand and Supply. Solving the max-utility problem. Working out consumer responses. The response function

Multi-Criteria Flow-Shop Scheduling Optimization

r i = a i + b i f b i = Cov[r i, f] The only parameters to be estimated for this model are a i 's, b i 's, σe 2 i

A New Constructive Proof of Graham's Theorem and More New Classes of Functionally Complete Functions

ACTUARIAL RESEARCH CLEARING HOUSE 1990 VOL. 2 INTEREST, AMORTIZATION AND SIMPLICITY. by Thomas M. Zavist, A.S.A.

Lecture 9: The law of large numbers and central limit theorem

18.S096 Problem Set 5 Fall 2013 Volatility Modeling Due Date: 10/29/2013

Positivity Preserving Schemes for Black-Scholes Equation

Subject CT1 Financial Mathematics Core Technical Syllabus

Estimating Forward Looking Distribution with the Ross Recovery Theorem

Solutions to Problem Sheet 1

These characteristics are expressed in terms of statistical properties which are estimated from the sample data.

A Hybrid Finite Difference Method for Valuing American Puts

ad covexity Defie Macaulay duratio D Mod = r 1 = ( CF i i k (1 + r k) i ) (1.) (1 + r k) C = ( r ) = 1 ( CF i i(i + 1) (1 + r k) i+ k ) ( ( i k ) CF i

5 Statistical Inference

This article is part of a series providing

Forecasting bad debt losses using clustering algorithms and Markov chains

Transcription:

Solvig te advectio PDE i explicit FTCS, Lax, Implicit FTCS ad Crak-Nicolso metods for costat ad varyig speed. Accuracy, stability ad software aimatio Report submitted for fulfillmet of te Requiremets for MAE 294 Masters degree project Supervisor: Dr Doald Dabdub, UCI. Writte by Nasser M. Abbasi. Masters degree cadidate studet. Mecaical egieerig departmet Uiversity of Califoria, Irvie Cotets 1 Itroductio 3 1.1 Backward differece (Upwid)...................................... 4 1.2 Forward differece (dowwid)..................................... 5 1.3 Ceter differece............................................. 5 2 Numerical scemes 6 2.1 Explicit Metods............................................. 6 2.1.1 FTCS............................................... 6 2.1.2 Dowwid............................................. 6 2.1.3 Upwid.............................................. 6 2.1.4 LAX................................................ 6 2.1.5 Lax-Wedroff........................................... 7 2.1.6 Leap-frog............................................. 7 2.2 Implicit Metods............................................. 7 2.2.1 Implicit FTCS.......................................... 7 2.2.2 Wedrof.............................................. 8 2.2.3 Crak-Nicolso.......................................... 8 3 Stability aalysis 9 3.1 Stability aalysis for FTCS....................................... 10 3.2 Stability aalysis of te dowwid metod............................... 10 3.3 Stability aalysis of te upwid metod................................ 11 3.4 Stability aalysis of Lax......................................... 12 3.5 Stability of Lax-Wedroff........................................ 12 1

3.6 Stability aalysis of te Implicit FTCS................................. 12 4 Solutio Results ad Output 14 4.1 Case 1................................................... 14 4.2 Case 2................................................... 15 4.3 Case 3................................................... 15 4.4 Case 4................................................... 15 4.5 case 5................................................... 16 4.6 case 6................................................... 18 4.7 case 7................................................... 18 4.8 case 8................................................... 19 4.9 CPU compariso tables......................................... 20 4.10 Accuracy compariso tables....................................... 23 5 Coclusio 24 6 Appedix 25 6.1 Plots.................................................... 25 6.1.1 case 1............................................... 25 6.1.2 case 2............................................... 27 6.1.3 case 3............................................... 29 6.1.4 case 4............................................... 31 6.1.5 case 5............................................... 33 6.1.6 case 6............................................... 35 6.1.7 case 7............................................... 37 6.1.8 case 8............................................... 39 6.2 Source code................................................ 41 7 Refereces 48 2

1 Itroductio Te goal of tis project is to aalyze ad compare differet umerical metods for solvig te first order advectio PDE. Followig te aalytical aalysis for stability of te umerical sceme, aimatio were doe to visually illustrate ad cofirm tese results. Tis was carried for differet parameters. Te aimatio was programmed i Matematica ad saved to aimated gif files wic was te loaded ito te HTML versio of tis report located ere. Fortra 95 was used for te computatio part, wile Matematica was used for te aimatio ad grapics part. Te above lik cotais all te supportig material for te project, icludig te Fortra program (i source ad widows executable format) used to carry te mai computatio, ad te Matematica program used to do te aimatio ad te Uix bas file used to process te computatio for differet parameters. Te specific PDE example used for te aalysis ad aimatio was te oe provided by Professor Doald Dabdub for te fial exam for is MAE 185 course (Numerical metods for mecaical egieers) i sprig 2006. Tis PDE is described below: Solve umerically c t + u c x = 0 Were c(x, t) is te cocetratio of a give material as a fuctio of time ad space. Te above is solved for te followig 2 cases 1. u (te advectio speed, or te speed at wic te mass is beig trasported) is a costat value give as (2 ft/mi. 2. u is a fuctio of time defied as u(t) = t 20 ft/mi Te problem parameters are: t 0 0 x L Were L = 100 feet.. Iitial coditios are { ( ( 1 + cos π x 30 )) 5 25 x 25 c (x, 0) = F (x) = 0 oterwise Te boudary coditios are c (0, t) = 0 c(l, t) = 0 Tis PDE is a example of a IBVP (Iitial ad Boudary Value Problem). Differet umerical metods are used to solve te above PDE. Te metods are compared for stability usig Vo Neuma stability aalysis. Te umerical metods are also compared for accuracy. Tis was doe by comparig te umerical solutio to te kow aalytical solutio at eac time step. Te compariso was doe by computig te root mea square error (RMSE) betwee te umerical ad te aalytical solutio at eac time step. Te metod wit te least RMSE at te ed of te simulatio is cosidered te most accurate. Te above PDE as a kow aalytical solutio wic is C (x, t) = F (x ut) 3

Te above aalytical solutio idicates tat te iitial cocetratio will move from left to rigt wit te advectio speed u. Te formulatio of eac umerical metod is sow below. is used to represet x, te space betwee 2 space grid poit, or te space step size, ad τ is used to represet t, te time step. Te space lie as N grid poits. Te spacig was fixed at 0.01 ft for all te metods ad for all te test cases, wile τ was caged. Tis made comparig te differet metods simpler. Te followig diagram illustrates te discretizatio used. Sould we cosider te lower left ad te lower rigt grid poits above as part of te iitial coditios, or part of te boudary coditios? Stability of eac metod is derived. Stability is importat, sice by te Lax-Rictmyer equivalece teorem 1, stability implies covergece of te solutio. Covergece of te umerical solutios implies tat as te step size becomes smaller, te umerical solutio coverges to te aalytical solutio. Explicit ad implicit umerical metods are used. We solvig for te future value of te solutio at a sigle ode i terms of oly past values, te metod is called a explicit metod. I oter words, we te oly ukow is te future value of te solutio at a sigle ode, ad everytig else o te rigt ad side of te fiite differece equatio is a solutio derived at earlier time step, te metod is explicit. A implicit metod is oe i wic te fiite differece equatio cotais te solutio at a at future time at more ta oe ode. I oter words, future solutio are beig solved for at more ta oe ode i terms of te solutio at earlier time. Implicit metods terefor are usually solved by matrix metods by solvig Ax = b were b represets preset preset kow solutio values, ad x are te ukow future solutio values, ad A is te coefficiet matrix wic will usually be block diagoal (or tri diagoal) i sape. I te derivatios below, te otatio of Ci is used to idicate te solutio at time step ad at space ode i. Hece C (x i, t ) is writte as Ci. Tis otatio seems to be more clear ta te C i, otatio. Differet fiite differece scemes for solvig a PDE are obtaied by usig differet metods of approximatig te derivative terms i te PDE. Tis will be illustrated usig te space derivative c x. Tis derivative ca be approximated i oe of te followig 3 ways (all at time step ) 1.1 Backward differece (Upwid) c x C i Ci 1 1 Rictmyer ad Morto 1967. p45): Give a properly posed liear iitial value problem ad a fiite differece approximatio to it tat satisfies te cosistecy coditio, stability is te ecessary ad sufficiet coditio for covergece. 4

1.2 Forward differece (dowwid) c x C i+1 C i 1.3 Ceter differece c x C i+1 C i 1 2 Te followig are te derivatio of a umber of metods for solvig te advectio PDE obtaied by usig te above defiitios for te derivative we applied to bot space ad time. 5

2 Numerical scemes 2.1 Explicit Metods 2.1.1 FTCS Wit FTCS, te forward time derivative, ad te cetered space derivative are used. Hece te advectio PDE ca be writte as Solvig for C +1 i results i C +1 i C +1 i Ci τ ( C = u i+1 Ci 1 ) 2 = Ci uτ ( C 2 i+1 Ci 1) Tis metod will be sow to be ucoditioally ustable. 2.1.2 Dowwid Here, te forward time derivative for C t is used ad also te forward space derivative for C x. Tis results i ) C +1 i C +1 i Ci τ ( C = u i+1 Ci = C i uτ ( C i+1 Ci ) Tis metod will be sow to be ucoditioally ustable as well. 2.1.3 Upwid Here, te forward time derivative for C t is used, ad te backward derivative for C x is used. Tis results i ( C i Ci 1 ) C +1 i C +1 i Tis will be sow to be stable if uτ 1 2.1.4 LAX Ci τ = u = C i uτ Lookig at te FTCS eq (1) above, ad sow below agai Te term C i C +1 i ( C i C i 1) = Ci uτ ( C 2 i+1 Ci 1) above is replaced by its average value C i+1 +C i 1 2 to obtai te LAX metod (0) (1) Tis metod will be sow to be stable if uτ 1 Ci +1 = 1 ( C 2 i+1 + Ci 1) uτ ( C 2 i+1 Ci 1) (4) 6

2.1.5 Lax-Wedroff By usig te secod-order fiite differece sceme for te time derivative, te metod of Lax-Wedroff metod is obtaied 2.1.6 Leap-frog C +1 i = Ci uτ ( C 2 i+1 Ci 1) u 2 τ 2 ( + C 2 2 i+1 + Ci 1 2Ci ) I tis metod, te cetered derivative is used for bot time ad space. Tis results i C +1 i Ci 1 2τ ( C = u i+1 Ci 1 ) 2 Tis metod requires a special startig procedure due to te term Ci 1. Aoter sceme suc as Lax ca be used to kick start tis metod. 2.2 Implicit Metods 2.2.1 Implicit FTCS Give te explicit FTCS derived above C +1 i Ci τ ( C = u i+1 Ci 1 ) 2 Te above is modified it by evaluatig te space ceter derivative at time step + 1 istead of at time step, tis results i ( Ci +1 Ci C +1 i+1 = u ) C+1 i 1 (5A) τ 2 Hece C +1 i Writig it i matrix form, first lettig α = uτ 2 results i 1 0 0 0 0 0 α 1 α 0 0 0 0 α 1 α 0 0 0 0 α 1 α 0. + uτ 2 C+1 i+1 uτ 2 C+1 i 1 = C i (5B) 0 0 0 0 0 0 1 Were N is te umber of space grid poits. Te above is writte as Ax = b C +1 0 C +1 1 C +1 2 C +1 3. C +1 N 1 = Solvig for x, wic represets te solutio at time step + 1 or at time t = ( + 1) τ. b represets te curret solutio at time step, ad A is te matrix of te coefficiets sow above. Due to te form of te A matrix, (Called tri diagoal, or Block diagoal), a algoritm tat takes advatages of tis form is used. Tis is called te Tomas algoritm. Tis greatly speeds up te solutio. If we ad used a geeral algoritm to solve tis system suc as te Gauss elimiatio metod, it would ave bee muc slower, C 0 C 1 C 2 C 3. C N 1 7

makig te implicit metod ot practical to use. (Some tests o te same data sowed te Tomas algoritm to be 50 times faster ta Gaussia elimiatio). 2.2.2 Wedrof Tis metod uses ceter differece for te derivative aroud te space step ( i + 1 2) ad te time step ( + 1 2) τ Tis leads to te followig sceme ( 1 uτ ) ( Ci +1 + 1 + uτ ) C +1 i+1 = ( 1 + uτ ) ( Ci + 1 uτ ) C i+1 Tis ca also be solved usig similar matrix metod to tat used for te implicit FTCS. Tis metod is ot used i tis report. 2.2.3 Crak-Nicolso By takig te average of te explicit FTCS ad te implicit FTCS formulatios (sow agai below), te C-N sceme is derived C +1 i C +1 i Ci τ Ci τ ( C = u i+1 Ci 1 ) 2 ( C +1 i+1 = u ) C+1 i 1 2 Takig te average of te above results i Ci +1 Ci = u ( C i+1 C ) ( i 1 u C +1 i+1 ) C+1 i 1 τ 2 2 2 2 as C +1 i + uτ 4 C+1 i+1 uτ 4 C+1 i 1 = C i uτ 4 C i+1 + uτ 4 C i 1 Now te system Ax = b is setup to solve for future values as follows. Let α = uτ 4, te system ca be writte 1 0 0 0 0 0 α 1 α 0 0 0 0 α 1 α 0 0 0 0 α 1 α 0 0 0 0 α 1 0 0 0 0 0 0 1 C +1 0 C +1 1 C +1 2 C +1 3. C +1 N 1 Tomas algoritm is used to solve te above system for C +1 i. C 0 C1 αc 2 + αc 0 C = 2 αc 3 + αc 1 C3 αc 4 + αc 2. C N 1 8

3 Stability aalysis A umerical solutio is stable if te eergy cotet remai below some limitig value o matter ow log te solutio is itegrated. I essece, tis meas tat te solutio does ot blow up after some time. Tis ca be called BIBO stability (Bouded I Bouded Out). Hece oe way to aalyze te stability of te umerical solutio is to determie a expressio tat relates te amplitude of te solutio betwee 2 time steps, ad to determie if tis ratio remai less ta or equal to a uity as more ad more time steps are take. Tis type of aalysis is called Vo Neuma stability aalysis for umerical metods. Te aalysis is based of fidig a expressio for te magificatio factor of te wave amplitude at eac step. Te solutio will be stable if tis magificatio factor is less ta oe. Let te magificatio factor be ξ. Te umerical sceme is stable iff ζ 1 Te Courat Friedrics Lewy (CFL) criteria for stability says tat ζ 1 uτ 1 Were u,, ad τ are as defied above: u is te wave speed, = x ad τ = t. Te umber uτ is also called te courat umber. Some umerical metods will be sow to be ucoditioally ustable (suc as explicit FTCS ad te explicit upwid). Tis meas tat eve if courat umber was 1, te umerical solutios will evetually become ustable. Some explicit metods suc as LAX, are coditioally stable if te courat umber was 1. Implicit metods are ucoditioally stable, ece courat umber is ot used for tese metods. However, tis does ot mea oe ca take as large step as oe wats wit te implicit metods, sice accuracy will be affected eve if te solutio remai stable. Hece, te best umerical sceme is oe i wic te largest step size ca be take, wit te least amout of iaccuracy i te umerical solutio wile remaiig stable. For umerical sceme tat are coditioally stable, it ca be see from te CFL coditio tat for a fixed speed u ad fixed, te maximum time step tat ca be take is give by τ max u It ca be immediately see from above, tat for te case we te advectio speed is varyig ad is a fuctio of time suc as te case we u (t) = t 20 implyig tat te speed is icreasig wit time, te we usig a fixed time step τ it will evetually become larger ta u ad te umerical sceme will be ustable. Tis is because as u (t) is becomig larger ad larger, wile is fixed, te term u will become smaller ad smaller. Hece to keep te courat umber uτ 1, te time step take must remai less ta u, ece usig a fixed time step wit icreasig u will evetually lead to istability. Tis will affect te explicit metods tat are coditioally stable suc as te LAX metod, sice te Lax metod is explicit ad depeds o satisfyig te CFL all te time for its stability. Implicit metods are stable for ay time step. I te followig we derive te details of te stability aalysis ad use Vo Neuma aalysis to derive a expressio for te amplificatio factor ζ for differet umerical scemes. So to summarize: 1. Explicit FTCS is ucoditioally ustable. 2. Explicit LAX is stable if uτ 3. Implicit FTCS ad C-R are stable for all τ 1, or i oter words, τ max u 9

3.1 Stability aalysis for FTCS Usig Vo Neuma metod, te followig trial solutio to te PDE is assumed c (x, t) = A (t) e jkx were j = 1 ad k is te wave umber ad A is te amplitude of te wave, as a fuctio of time. Hece te solutio at time step ad at x = x i = i is writte as A e jki (2) Substitute tis trial solutio (2) ito te (1) results i A +1 e jki = A e jki uτ ( A e jk(i+1) A e jk(i 1)) (3) 2 Let ξ be te ratio of te amplitude of te wave at time step + 1 relative to tat at time step. ece Divide (3) by A results i Divide te above by e jki ξ = A+1 A ξe jki = e jki uτ (e jk(i+1) e jk(i 1)) 2 ξ = 1 uτ (e jk e jk) 2 = 1 uτ j si (k) Hece ( uτ ) 2 ξ = 1 + si (k) Tis implies tat ξ 1 regardless of te time step τ selected or te space step, ece FTCS is ucoditioally ustable. For a fixed speed u, te istability ca be delayed by makig τ smaller, but it could ot be preveted. Evetually tis umerical solutio will blow up. Tis will be illustrated below i a aimatio. See case 3 ad 4 as examples. Te istability ca be delayed by makig τ smaller for a fixed, or by makig larger for a fixed τ. 3.2 Stability aalysis of te dowwid metod Ci +1 = Ci uτ ( C i+1 Ci ) Substitute te trial solutio A e jki ito te above A +1 e jki = A e jki uτ (A e jk(i+1) A e jki) ξ = 1 uτ ( ) e jk 1 = 1 + uτ uτ ejk = 1 + uτ uτ (cos (k) + j si (k)) = 1 + uτ (1 cos k) j uτ 10 si k

Let uτ = λ Hece ξ = 1 + λ (1 cos k) jλ si k ξ 2 = (1 + λ (1 cos k)) 2 + (λ si k) 2 = 1 + 2λ (1 cos k) + λ 2 (1 cos k) 2 + λ 2 si 2 k = 1 + 2λ (1 cos k) + λ 2 ( 1 2 cos k + cos 2 k ) + λ 2 si 2 k = 1 + 2λ 2λ cos k + λ 2 2λ 2 cos k + λ 2 cos 2 k + λ 2 si 2 k = 1 + 2λ 2λ cos k + 2λ 2 2λ 2 cos k = 1 + 2λ (1 + λ) (1 cos k) Hece for stability it is required tat 1 + 2λ (1 + λ) (1 cos k) 1 or 2λ (1 + λ) (1 cos k) 0 sice λ = uτ, a positive quatity, te te above coditio ca ot be satisfied. Hece te dowwid metod is ucoditioally ustable. 3.3 Stability aalysis of te upwid metod Ci +1 = Ci uτ Substitute te trial solutio A e jki ito te above ( C i C i 1) Let uτ Hece Hece = λ A +1 e jki = A e jki uτ (A e jki A e jk(i 1)) ξ = 1 uτ (1 e jk) = 1 uτ + uτ e jk = 1 uτ + uτ (cos (k) j si (k)) = 1 uτ (1 cos k) j uτ ξ = 1 λ (1 cos k) jλ si k si k ξ 2 = (1 λ (1 cos k)) 2 + (λ si k) 2 = 1 2λ (1 cos k) + λ 2 (1 cos k) 2 + λ 2 si 2 k = 1 2λ + 2λ cos k + λ 2 ( 1 + cos 2 k 2 cos k ) + λ 2 si 2 k = 1 2λ + 2λ cos k + λ 2 + λ 2 cos 2 k 2λ 2 cos k + λ 2 si 2 k = 1 2λ + 2λ cos k + 2λ 2 2λ 2 cos k = 1 2λ (1 λ) (1 cos k) 11

Hece for stability it is required tat or 1 2λ (1 λ) (1 cos k) 1 2λ (1 λ) (1 cos k) 0 Wic will be true oly if (1 λ) 0 or λ 1 ece tis implies uτ 1 Hece te upwid metod is stable if te CFL coditio is satisfied. Tis will be see as te same stability coditio for te Lax metod below. 3.4 Stability aalysis of Lax Replace te trial fuctio from (2) i Lax formulatio i (4) ad obtai A +1 e jki = 1 2 ( A e jk(i+1) + A e jk(i 1)) uτ (A e jk(i+1) A e jk(i 1)) 2 Divide by A e jki, te magificatio factor ζ is obtaied Hece ζ = 1 ( e jk + e jk) uτ (e jk e jk) 2 2 = cos (k) j uτ si (k) ζ = cos 2 (k) + ( uτ ) 2 si 2 (k) Sice cos 2 (k) 1 ad si 2 (k) 1, te it is see tat ζ 1 if uτ 1 Hece te followig is te coditio for stability τ u As metioed earlier, tis is called te CFL coditio. Te Lax metod is stable for τ u owever, a modified versio of tis metod is more accurate, wic is te Lax-Wedroff metod. 3.5 Stability of Lax-Wedroff Tis is te same as te Lax metod. Te metod is stable if τ u 3.6 Stability aalysis of te Implicit FTCS Replace te trial fuctio from (2) i (5B) results i Divide by A e jki A +1 e jki + uτ 2 A+1 e jk(i+1) uτ 2 A+1 e jk(i 1) = A e jki 12

Hece Hece tis sows tat te ξ + uτ 2 ξejk uτ 2 ξe jk = 1 ( ξ 1 + uτ 2 ejk uτ 2 e jk) = 1 ( ξ 1 + j uτ ) si (k) = 1 ξ = 1 ξ = 1 + j uτ si (k) = 1 j uτ si (k) 1 + uτ si (k) 1 + ( ) uτ 2 si 2 (k) 1 + uτ si (k) < 1 Implicit FTCS metod is ucoditioally stable. Tis property is commo to all implicit metods. Eve toug te implicit FTCS is stable, it is ot very accurate. See case 8 below for a example. 13

4 Solutio Results ad Output For te Fortra implemetatio, te followig metods are implemeted. Te explicit FTCS, Explicit Lax, Implicit FTCS, ad Implicit Crak-Nicolso. For eac metod, te followig was geerated 1. CPU time used for te ru. 2. sap sot of te solutio at t = 0, t = 15, ad t = 30 miutes. 3. RMSE betwee te umerical solutio ad te aalytical solutio. 4. Aimatio of te umerical solutio. Te aimatio was doe by takig sapsots of te solutio at regular itervals i Fortra. Tese were saved to disk. Te Matematica was used to geerate te aimatio ad te plots. To compare te stability ad accuracy of te metods, te time step was caged (icreased) ad a ew ru was made. 8 differet values of time steps are used. So tere are 8 tests cases. Tese 8 test cases were ru for bot fixed speed (u = 2 ft/mi) ad for u = t 20 ft/mi. Tis table below summarizes tese cases. Te appedix cotais all te plots. Te aimatios are added as HTML liks. 4.1 Case 1 τ = 0.0001 sec, = 0.1 ft Speed Metod CPU time (sec) RMSE Aimatio (2D) plots U=2 Explicit FTCS 20 0.0546 Explicit LAX 31 0.0543 Implicit FTCS 45 0.0548 C-R 49 0.0544 U=t/20 Explicit FTCS 21 0.003 Explicit LAX 31 0.0031 Implicit FTCS 67 0.0031 C-R 69 0.0032 Note te followig: Te explicit FTCS remaied stable trougout te ru due to te small time step. All oter metods were stable as well durig te ru. For te CPU for te varyig u case, otice tat for te implicit metods tis value is larger ta te CPU for te same metods but we u is fixed. Tis is due to te fact tat te matrix A is o loger costat, ad must be recomputed at eac time step before callig Tomas algoritm to solve Ax = b system. Also otice tat te CPU time for te implicit metods is larger ta te explicit metods. Tis is due to te extra computatioal cost i solvig Ax = b. Eve we usig Tomas algoritm, tis is still more expesive ta te explicit metods we umber of time steps is large. 14

4.2 Case 2 τ = 0.001 sec, = 0.1 ft Speed Metod CPU time (sec) RMSE Aimatio (2D) plots U=2 Explicit FTCS 2.42 0.01264 Explicit LAX 3.48 0.0057 Implicit FTCS 4.7 0.00742 C-R 4.9 0.00575 U=t/20 Explicit FTCS 2.5 0.00352 Explicit LAX 3.5 0.00329 Implicit FTCS 7 0.00337 C-R 7.5 0.0033 Te explicit FTCS is stable for most of te ru, ear te ed it is startig to be become ustable. Notice tat aroud 26 miutes tat bubbles are startig to sow up i te umerical solutio dowstream. Tis is a caracteristic of ow tis metod becomes ustable. Tis will be more clear i te ext test cases we te time step is made larger. For te varyig speed case, te explicit metod usig te same time step remaied stable durig te wole 30 miutes. Tis is because te average speed was less ta 2 ft/mi, ece te mass did ot ave to travel as log a distace as wit fixed speed of u = 2, ad so te istability did ot sow up. Matematically tis ca be explaied by lookig at te term uτ, ece for smaller u, te courat umber is smaller. Notice also te RMSE is smaller for variable speed compared to fixed speed. Agai tis is related to te smaller average speed makig te courat umber smaller. 4.3 Case 3 I tis case, we sligtly make te time step loger ta before. We start to see te istability of FTCS. τ = 0.0013 sec, = 0.1 ft, uτ = 0.026 1 for fixed u Speed Metod CPU time (sec) RMSE Aimatio (2D) plots U=2 Explicit FTCS 1.9 0.0494 Explicit LAX 2.78 0.01125 Implicit FTCS 3.7 0.01245 C-R 3.9 0.01128 U=t/20 Explicit FTCS 2.0 0.00365 Explicit LAX 2.9 0.00331 Implicit FTCS 5.56 0.00346 C-R 6 0.00331 For explicit FTCS, Te solutio ow startig to sow istability at 25 miutes. Lax remaied stable sice CFL is satisfied. Explicit FTCS is becomig less accurate as well. Explicit Lax is most accurate at tis time step. 4.4 Case 4 I tis case, we sligtly make te time step eve loger ta before. Now FTCS becomes more ustable. τ = 0.0015 sec, = 0.1 ft, uτ = 0.03 1. 15

Speed Metod CPU time (sec) RMSE Aimatio (2D) plots U=2 Explicit FTCS 1.73 0.15249 Explicit LAX 2.56 0.000563 Implicit FTCS 3.34 0.009005 C-R 3.45 0.00565 U=t/20 Explicit FTCS 1.84 0.00380 Explicit LAX 2.53 0.00336 Implicit FTCS 4,73 0.00358 C-R 5 0.003373 FTCS Istability starts at aroud 20 miutes. LAX remaied stable sice CFL is satisfied. Lax remaied te most accurate at tis time step. It accuracy actually improved as te time step became larger. 4.5 case 5 Agai te time step is made loger ta before. Now te explicit FTCS is completely ustable. τ = 0.045 sec, = 0.1 ft For te case of fixed U, we ave uτ = 2 0.045 0.1 = 0.9 1, wile for varyig U, te maximum value will be at te ed of te ru, wic is 30/20 = 1.5 ft/mi., ece te CFL coditio is cagig, wit a value of 1.5 0.045 0.1 = 0.675 at te ed of te ru wic is still 1 Speed Metod CPU time (sec) RMSE Aimatio (2D) plots U=2 Explicit FTCS 0.73 blows up Explicit LAX 0.281 0.000162 Implicit FTCS 0.437 0.1306 C-R 0.4 0.01028 U=t/20 Explicit FTCS 0.28 blow up Explicit LAX 0.3 0.01117 Implicit FTCS 0.40 0.0386 C-R 0.4 0.01197 For te varyig speed case, te explicit FTCS remaied stable for te duratio of te ru as compared to te case wit te fixed speed. Tis is because te average wave speed is less ta wit te fixed wave speed case. Te magificatio factor depeds o te speed of te wave. ( uτ ) 2 ξ = 1 + si (k) Wit te varyig speed case, te coefficiet uτ was smaller durig te wole ru, sice te maximum speed u attaied will be 1.5 ft/mi. as compared to 2 ft/mi. i te fixed u case. We see ta te smaller te speed u te smaller te magificatio (wit everytig else beig fixed). If we ave ru te simulatio a little loger for te varyig speed case, we will see te istability wit explicit FTCS. Tis below is a diagram sowig 2 rus usig te explicit FTCS bot wit u = t 20 ft/mi, oe was ru for 30 miutes, ad te secod for 53 miutes. Te ru to 30 miutes sowed o istability wile te ru for 53 miutes sowed te istability. Tis sow te explicit FTCS will evetually become ustable. 16

Tis is a aimatio of te above 17

4.6 case 6 I tis case, te time step is icreased so tat uτ is just above te CFL coditio. Notice ow tat te Explicit LAX metod become ustable as expected. Te oter implicit metods remai stable. te explicit FTCS metod ow is completely ustable. Te implicit FTCS metod is startig to become less accurate. τ = 0.05025 sec, = 0.1 ft, uτ = 2 0.05025 0.1 = 1. 005 > 1 Speed Metod CPU time (sec) RMSE Aimatio (2D) plots U=2 Explicit FTCS 0.7 blows up N/A blows up Explicit LAX 0.25 0.1006 Implicit FTCS 0.5 0.13945 C-R 0.468 0.01104 U=t/20 Explicit FTCS 0.28 blows up N/A blows up Explicit LAX 0.31 0.04385 Implicit FTCS 0.45 0.0428 C-R 0.56 0.01317 Notice tat explicit LAX takes muc less CPU ta ay oter metod. 4.7 case 7 τ = 0.06 sec, = 0.1 ft, uτ = 2 0.06 0.1 = 1. 2 > 1 Speed Metod CPU time (sec) RMSE Aimatio (2D) plots U=2 Explicit FTCS 0.65 blows up N/A blows up Explicit LAX 0.9 blows up Implicit FTCS 0.42 0.1531 C-R 0.41 0.01244 U=t/20 Explicit FTCS 0.265 blows up N/A blows up Explicit LAX 0.29 0.01389 Implicit FTCS 0.36 0.0493 C-R 0.36 0.01525 Notice tat te CPU for te implicit metod we speed is fixed is ow iger ta te CPU for te explicit metods. Tis ca be explaied as follows: sice te time step ow is larger ta before, te umber of times to solve Ax = b as bee reduced. Tis made te implicit metods faster. Tis implies tat Usig a relatively large time step, implicit metods become faster ta te explicit metods. 18

4.8 case 8 τ = 0.07 sec, = 0.1 ft, uτ = 2 0.07 0.1 = 1. 4 > 1 Speed Metod CPU time (sec) RMSE Aimatio (2D) plots U=2 Explicit FTCS 0.5 blows up N/A blows up Explicit LAX 0.89 blows up Implicit FTCS 0.453 0.1653 C-R 0.36 0.01403 U=t/20 Explicit FTCS 0.234 blows up N/A blows up Explicit LAX 0.2187 0.01564 Implicit FTCS 0.344 0.0557 C-R 0.312 0.0174 19

4.9 CPU compariso tables As expected, CPU time usage will be less as te time step is icreased. Tere is a aomaly cased oticed were te CPU time icreased for te Lax metod we te time step is icreased from 0.05025 to 0.06, Tis eeds furter ivestigatio. Tis table below summarizes te CPU time i secods used by eac metod for te case of costat speed as time step is icreased. τ sec Explicit F T CS Explicit LAX Implicit F T CS C R 0.0001 20 31 45 49 0.001 2.42 3.48 4.7 4.9 0.0013 1.9 2.78 3.7 3.9 0.0015 1.7 2.56 3.34 3.45 0.045 0.73 0.281 0.43 0.4 0.05025 0.7 0.25 0.5 0.468 0.06 0.65 0.9 0.4 0.41 0.07 0.5 0.89 0.45 0.36 Tis is te plot of te above table Tis table below summarizes te CPU time i secods used by eac metod for te case of varyig speed as time step is icreased. τ sec Explicit F T CS Explicit LAX Implicit F T CS C R 0.0001 21 31 67 69 0.001 2.5 3.5 7 7.5 0.0013 2 2.9 5.56 6 0.0015 1.8 2.53 4.73 5 0.045 0.28 0.54 0.45 0.45 0.05025 0.28 0.31 0.45 0.56 0.06 0.265 0.29 0.36 0.36 0.07 0.23 0.22 0.33 0.31 Tis is te plot of te above table 20

Tis plot below compares te CPU time for eac metod we te speed is costat vs. we te speed was cagig wit time. 21

22

4.10 Accuracy compariso tables Tis table below summarizes te RMS error from eac umerical metod as a fuctio of cagig te time step size. Tis is for case of costat speed. time step Explicit F T CS Explicit LAX Implicit F T CS C R 0.0001 0.0546 0.0543 0.0548 0.0544 0.001 0.01264 0.0057 0.00742 0.00575 0.0013 0.0494 0.01125 0.01245 0.00128 0.0015 0.15249 0.00056 0.009 0.0056 0.045 blows up 0.000162 0.1306 0.01028 0.05025 blows up 0.1006 0.1394 0.011 0.06 blows up blows up 0.1531 0.01244 0.07 blows up blows up 0.1653 0.01403 Notice tat te Lax metod became more accurate we te time step was icreased from 0.0001 to 0.04 secods, te it starts to become less accurate as time step is icreased. Tis is couter ituitive to wat oe ca expect. It will be iterestig to ivestigate tis furter to obtai a matematical explaatio for tis strage peomea. Te accuracy of te implicit FTCS, ad C-R also icreased sligtly as te time step became larger from 0.0001 to 0.0015, te te implicit FTCS became worst i terms of accuracy as te time step icreased. C-R metod accuracy did ot deteriorate as muc wit icreasig te time step. Tis sows te C-R sceme to be more robust. Tis table below summarizes te RMS error from eac umerical metod as a fuctio of cagig te time step size. Tis is for case of cagig speed. time step Explicit F T CS Explicit LAX Implicit F T CS C R 0.0001 0.003 0.003 0.003 0.0030 0.001 0.00352 0.00329 0.0033 0.0033 0.0013 0.00365 0.00331 0.00346 0.0033 0.0015 0.0038 0.00336 0.0035 0.00337 0.045 blows up 0.01117 0.0386 0.0119 0.05025 blows up 0.04385 0.0428 0.01317 0.06 blows up 0.01389 0.0493 0.01525 0.07 blows up 0.01564 0.0557 0.0174 Te effect of avig te speed defied as µ = t 20is to delay istability for te explicit metods as time step is icreased. Notice also ere te case were te Lax metod became more accurate as te time step is icreased from 0.0001 to 0.0015. 23

5 Coclusio 4 differet umerical fiite differece scemes are examied for CPU time, stability ad accuracy i solvig te advectio PDE for costat speed ad for a speed wic is a fuctio of time. For accuracy, a iterestig result is observed. Te Lax sceme is te most accurate for Courat umber close to uity. Tis meas as te time step is icreased, te Lax become more accurate of te 4 metods. But beyod te CFL coditio, Bot explicit metods (FTCS ad Lax) became less accurate. Explicit FTCS became ustable sooer ta Lax, wile te implicit metods remaied stable. Te implicit FTCS was less accurate ta te C-R metod. Tis implies tat oe sould use te Lax metod if oe ca be satisfied wit a time step suc tat te courat umber is close to a uit. For stability, Crak-Nicolso was te most stable of all metods. Stability by itself is ot sufficiet coditio to use to select a umerical sceme. It must also be accurate. Te C-R metod as bot tese properties for te rage of te time steps cosidered. But as metioed above, tere is a rage of time steps i wic te Lax metod is more accurate ta all te oter metods. For CPU usage, te explicit metods used less CPU time we te time step was small, up to 0.0015 sec. Tis ca be explaied as follows: for small step size, te umber of time to solve Ax = b is large. Hece te implicit metods will be slower. As te time step is icreased to te rage of 0.045 sec ad over, te implicit metods actually became more CPU efficiet due to te fact tat te umber of times to solve Ax = b is less because te umber of steps is less. I coclusio, te selectio of a fiite differece sceme depeds o may factors. Stability ad accuracy beig te most importat. Te time step size plays a critical rule. For Courat umber close to a uity, te Lax metod is te most attractive. For larger time steps, te C-R metod sould be cosidered. 24

6 Appedix 6.1 Plots 6.1.1 case 1 25

26

6.1.2 case 2 27

28

6.1.3 case 3 29

30

6.1.4 case 4 31

32

6.1.5 case 5 33

34

6.1.6 case 6 35

36

6.1.7 case 7 37

38

6.1.8 case 8 39

40

6.2 Source code 1!******************************************* 2!* 3!* Solve te advectio PDE usig Explicit FTCS, 4!* Explicit Lax, Implicit FTCS, ad implicit Crak-Nicolso 5!* metods for costat ad varyig speed. 6!* 7!* Solve dc/dt = -u dc/dx 8!* u = t/20 ft/miute 9!* ad 10!* u costat 11!* 12!* Compiler used: gu 95 (g95) o Cygwi. Gcc 3.4.4 13!* Date: Jue 20 2006 14!* 15!* by Nasser Abbasi 16!******************************************* 17 18 PROGRAM advectio 19 IMPLICIT NONE 20 21 REAL :: DT,DX,max_ru_time,legt,sapsot_, & 22 first_limit,secod_limit 23 INTEGER :: N,SNAPSHOTS 24 caracter(10) :: cmd_arg! to read time step from commad lie 25 26 INTEGER :: metod! 1=FTCS, 2=LAX, 3=Implicit FTCS, 4=C-R 27 INTEGER :: mode! 1=Fixed wid speed, 2=speed fuctio of time 28 29 REAL :: t_start, t_ed, cpu_time_used,ed_lie(1002) 30 INTEGER :: ALL_DATA_FILE_ID 31 PARAMETER(ALL_DATA_FILE_ID=900) 32 33! Iitialize data. All metods will use te same 34! parameters to make comparig tem easier 35 36! read t from commad lie. 37 CALL getarg(1,cmd_arg) 38 cmd_arg=trim(cmd_arg) 39 prit *,'= ', cmd_arg 40 read(cmd_arg,*)dt! i time, i miutes 41 42 prit *,'Dt=',DT 43 44 N = 1000! umber of grid poits i space 45 legt = 100! legt of space solutio i feet 46 47 first_limit = 0.25*legt 48 secod_limit = 0.35*legt 49 50 DX = legt/n! i space, i feets 51 52 max_ru_time = 30.0! ow log to ru for i miutes 53 SNAPSHOTS = 200! umber of sapsots per ru. Used for aimatio 54 55 sapsot_ = max_ru_time / SNAPSHOTS! time betwee eac sap sot 56 57 prit *,'DT=',DT,' miutes, DX=',DX,' feets' 58 prit *,'takig sapsots every ', sapsot_,' miutes' 59 60 DO mode=1,2 61 prit*,'=======> processig mode ',mode 62 DO metod=1,4! No eumeratio data types i Fotra 90 63 64 CALL CPU_TIME(t_start)! get curret CPU time 65 CALL process(mode,metod,n,dt,dx,max_ru_time,sapsot_,& 66 first_limit,secod_limit) 67 CALL CPU_TIME(t_ed)! get curret CPU time 68 69 cpu_time_used = t_ed - t_start 70 71 WRITE(*,FMT='(A,I2,A,F12.5)') 'CPU TIME used for metod', metod, ' = ', cpu_time_used 72! Now record test case parameters i last lie 41

73 ed_lie=0 74 ed_lie(1)=cpu_time_used 75 ed_lie(2)=dt 76 ed_lie(3)=dx 77 ed_lie(4)=mode 78 ed_lie(5)=metod 79 80 WRITE(UNIT=ALL_DATA_FILE_ID,FMT=*) ed_lie 81 CLOSE(ALL_DATA_FILE_ID) 82 83 END DO 84 END DO 85 86 END PROGRAM advectio 87!************************************ 88!* 89!* 90!************************************ 91 SUBROUTINE process(mode,metod,n,dt,dx,max_ru_time,sapsot_,& 92 first_limit,secod_limit) 93 IMPLICIT NONE 94 95 INTEGER, INTENT(IN) :: mode,metod,n 96 REAL, INTENT(IN) :: DT,DX,max_ru_time,sapsot_,& 97 first_limit,secod_limit 98 99 INTEGER :: I 100 LOGICAL :: sap_sot_at_15_take 101 INTEGER :: ALL_DATA_FILE_ID 102 PARAMETER(ALL_DATA_FILE_ID=900) 103 REAL :: sap_curret_time 104 REAL :: curret_time 105 REAL :: C(N)! curret solutio 106 REAL :: CNEW(N)! future solutio 107 REAL :: CEXACT(N)! curret exact solutio 108 REAL :: curret_first_limit 109 REAL :: A(N,N),aa(N),b(2:N),cc(N-1),CTEMP(N)! for C-R ad implicit FTCS 110 REAL :: K,speed 111 REAL :: error,rms! root mea square error betwee curret ad iitial sol. 112 113 curret_time = 0. 114 sap_curret_time = 0. 115 116 CALL iitialize_solutio(c,n,dx,first_limit,secod_limit) 117 CEXACT = C 118 curret_first_limit = first_limit 119 120 121 CALL pre_loop_iitializatio(mode,metod,curret_time,k, & DT,DX,N,C,ALL_DATA_FILE_ID, & 122 A,aa,b,cc ) 123 124 sap_sot_at_15_take=.false. 125 126 DO WHILE(curret_time < max_ru_time) 127 128 IF( sap_curret_time sapsot_ ) THEN 129 sap_curret_time = 0. 130 WRITE( UNIT=ALL_DATA_FILE_ID, FMT=*) curret_time, error, C 131 END IF 132 133 SELECT CASE(metod) 134 135 CASE( 1:2 ) 136 137 IF(metod==1) THEN! ftcs 138 IF(mode==2)THEN 139 K = speed(mode,curret_time)*dt/(2.*dx) 140 ENDIF 141 142 DO I = 2,N-1 143 CNEW(I) = C(I) - K * ( C(I+1) - C(I-1) ) 144 END DO 145 ELSE!lax 146 IF(mode == 2) THEN 147 K = speed(mode,curret_time)*dt/(dx) 148 ENDIF 42

149 150 DO I = 2,N-1 151 CNEW(I) = C(I) - K/2. * ( C(I+1) - C(I-1) ) + & 152 (K**2.)/2 * ( C(I+1) +C(I-1)-2.*C(I) ) 153 END DO 154 END IF 155 156 CNEW(1) = C(1) 157 CNEW(N) = C(N)! Boudary coditios 158 C=CNEW 159 160 CASE( 3 )! implicit ftcs 161 162 IF( mode == 2) THEN! oly eed to update Matrix for varyig U 163 K = speed(mode,curret_time)*dt/(2.*dx) 164 165 166 CALL iit_a_matrix(a,k,n) CALL iit_diagoal_vectors(n,a,cc,aa,b) 167 END IF 168 169 CALL solve_tomas_algoritm(n,aa,b,cc,c,cnew) 170 C = CNEW 171 172 CASE( 4 )! C-R 173 174 IF(mode == 2) THEN!oly eed to update A if U cages 175 K = speed(mode,curret_time)*dt/(4*dx)! C-R 176 CALL iit_a_matrix(a,k,n) 177 CALL iit_diagoal_vectors(n,a,cc,aa,b) 178 END IF 179 180 CTEMP(1) = C(1) 181 CTEMP(N) = C(N) 182 183 DO I=2,N-1 184 CTEMP(I)=C(I)+K*C(I-1)-K*C(I+1) 185 END DO 186 187 CALL solve_tomas_algoritm(n,aa,b,cc,ctemp,c) 188 189 END SELECT 190 191 IF( curret_time 15.0.AND. (.NOT. sap_sot_at_15_take)) THEN 192 sap_sot_at_15_take =.TRUE. 193 CALL take_oe_sap_sot(mode,metod,15,n,c,dx) 194 END IF 195 196 curret_time = curret_time + DT 197 curret_first_limit = curret_first_limit + speed(mode,curret_time)*dt 198 CALL get_curret_exact_solutio(cexact,n,curret_first_limit,dx) 199 error = RMS(CEXACT,C,N) 200 201 sap_curret_time = sap_curret_time + DT 202 203 END DO 204 205 CALL take_oe_sap_sot(mode,metod,30,n,c,dx) 206 207 END SUBROUTINE process 208!************************************ 209!* 210!* 211!************************************ 212 SUBROUTINE pre_loop_iitializatio(mode,metod,curret_time,k,& 213 DT,DX,N,C,ALL_DATA_FILE_ID,& 214 A,aa,b,cc) 215 IMPLICIT NONE 216 217 INTEGER, INTENT(IN) 218 REAL, INTENT(IN) :: mode,metod,n,all_data_file_id :: C(N),DT,DX,curret_time 219 REAL, INTENT(OUT) :: K,A(N,N),aa(N),b(2:N),cc(N-1) 220 REAL :: speed 221 222 SELECT CASE(metod) 223 CASE( 1 )! FTCS 224 43

225 K = speed(mode,curret_time)*dt/(2.*dx) 226 227 IF(mode==1) THEN 228 OPEN(UNIT=ALL_DATA_FILE_ID, file='expall.txt')! all time sots 229 CALL prit_to_file(c,'exp0.txt',n,dx) 230 ELSE 231 OPEN(UNIT=ALL_DATA_FILE_ID, file='exp_extraall.txt')! all time sots 232 CALL prit_to_file(c,'exp_extra0.txt',n,dx) 233 END IF 234 235 CASE( 2 )! Lax 236 237 K = speed(mode,curret_time)*dt/(dx) 238 239 IF(mode==1) THEN 240 OPEN(UNIT=ALL_DATA_FILE_ID, file='laxall.txt')! all time sots 241 CALL prit_to_file(c,'lax0.txt',n,dx) 242 ELSE 243 OPEN(UNIT=ALL_DATA_FILE_ID, file='lax_extraall.txt')! all time sots 244 CALL prit_to_file(c,'lax_extra0.txt',n,dx) 245 END IF 246 247 CASE( 3 )! Implicit FTCS 248 249 K = speed(mode,curret_time)*dt/(2.*dx) 250 251 CALL iit_a_matrix(a,k,n) 252 CALL iit_diagoal_vectors(n,a,cc,aa,b) 253 254 IF(mode==1) THEN 255 OPEN(UNIT=ALL_DATA_FILE_ID, file='impall.txt')! all time sots 256 CALL prit_to_file(c,'imp0.txt',n,dx) 257 ELSE 258 OPEN(UNIT=ALL_DATA_FILE_ID, file='imp_extraall.txt')! all time sots 259 CALL prit_to_file(c,'imp_extra0.txt',n,dx) 260 END IF 261 262 CASE( 4 )! C-R 263 264 K = speed(mode,curret_time)*dt/(4*dx)! C-R 265 266 CALL iit_a_matrix(a,k,n) 267 CALL iit_diagoal_vectors(n,a,cc,aa,b) 268 269 IF(mode==1) THEN 270 OPEN(UNIT=ALL_DATA_FILE_ID, file='crall.txt')! all time sots 271 CALL prit_to_file(c,'cr0.txt',n,dx) 272 ELSE 273 OPEN(UNIT=ALL_DATA_FILE_ID, file='cr_extraall.txt')! all time sots 274 CALL prit_to_file(c,'cr_extra0.txt',n,dx) 275 END IF 276 END SELECT 277 278 WRITE( UNIT=ALL_DATA_FILE_ID, FMT=*) curret_time,0, C 279 280 END SUBROUTINE pre_loop_iitializatio 281!************************************ 282!* 283!* 284!************************************ 285 SUBROUTINE iit_diagoal_vectors(n,a,cc,aa,b) 286 IMPLICIT NONE 287 288 INTEGER, INTENT(IN) ::N 289 REAL, INTENT(IN) ::A(N,N) 290 REAL, INTENT(OUT) ::aa(n),b(2:n),cc(n-1) 291 292 INTEGER ::I,J 293 294 J=2 295 DO I=1,N-1 296 cc(i)=a(i,j) 297 J=J+1 298 END DO 299 cc(1)=0 300 44

301 DO I=1,N 302 aa(i)=a(i,i) 303 END DO 304 305 J=1 306 DO I=2,N 307 b(i)=a(i,j) 308 J=J+1 309 END DO 310 311 END SUBROUTINE iit_diagoal_vectors 312!************************************ 313!* 314!* 315!************************************ 316 SUBROUTINE iitialize_solutio(c,n,dx,first_limit,secod_limit) 317 IMPLICIT NONE 318 319 INTEGER, INTENT(IN) :: N 320 REAL, INTENT(IN) :: DX,first_limit,secod_limit 321 REAL, INTENT(INOUT) :: C(0:N-1) 322 323 INTEGER :: I 324 REAL :: x, PI,av,R 325 326 PARAMETER( PI = ACOS(-1.) ) 327 328 x = 0 329 av = (secod_limit+first_limit)/2.0 330 R = av - first_limit 331 332 C = 0.0 333 334 DO I=0,N-1 335 336 IF( x first_limit.and. x secod_limit ) THEN 337 C(I) = 1 + COS( PI * (x-av)/r ) 338 END IF 339 340 x = x + DX 341 END DO 342 343 END SUBROUTINE iitialize_solutio 344!************************************ 345!* 346!* 347!************************************ 348 SUBROUTINE prit_to_file(c,file_ame,n,dx) 349 IMPLICIT NONE 350 351 352 REAL, INTENT(IN) :: C(N),DX 353 INTEGER, INTENT(IN) :: N 354 355 CHARACTER* (*), INTENT(IN) :: file_ame 356 357 INTEGER :: I 358 INTEGER :: FILE_ID 359 PARAMETER(FILE_ID=999) 360 REAL :: curret_positio 361 362 OPEN(UNIT=FILE_ID, file=file_ame) 363 364 curret_positio = 0; 365 DO I=1,N 366 367 WRITE( UNIT=FILE_ID, FMT=* ) curret_positio,'\t', C(I) 368 curret_positio = curret_positio + DX 369 370 END DO 371 372 CLOSE(FILE_ID) 373 374 END SUBROUTINE prit_to_file 375 376!************************************!* 45

377!* 378!************************************ 379 SUBROUTINE iit_a_matrix(a,k,n) 380 IMPLICIT NONE 381 382 INTEGER, INTENT(IN) ::N 383 REAL, INTENT(IN) ::K 384 REAL, INTENT(OUT) ::A(N,N) 385 386 INTEGER ::I 387 388 DO I = 2,N-1 389 A(I,I-1) = -K 390 A(I,I) = 1 391 A(I,I+1) = K 392 END DO 393 394 A(1,1) = 1 395 A(N,N) = 1 396 397 END SUBROUTINE iit_a_matrix 398!************************************ 399!* 400!* 401!************************************ 402 SUBROUTINE solve_tomas_algoritm(n,aa,b,c,old_c,ew_c) 403 IMPLICIT NONE 404 405 REAL, INTENT(IN) :: aa(n),b(2:n),c(n-1),old_c(n) 406 INTEGER, INTENT(IN) :: N 407 REAL, INTENT(INOUT) :: ew_c(n) 408 409 INTEGER :: I 410 REAL :: alpa(n),beta(2:n),g(n) 411 412 alpa(1) = aa(1) 413 DO I=2,N 414 beta(i)=b(i)/alpa(i-1) 415 alpa(i)=aa(i)-beta(i)*c(i-1) 416 END DO 417 418 g(1)=old_c(1) 419 DO I=2,N 420 g(i)=old_c(i)-beta(i)*g(i-1) 421 END DO 422 423 ew_c(n)=g(n)/alpa(n) 424 DO I=N-1,1,-1 425 ew_c(i)=(g(i)-c(i)*ew_c(i+1))/alpa(i) 426 END DO 427 428 END SUBROUTINE solve_tomas_algoritm 429!************************************ 430!* 431!* 432!************************************ 433 REAL FUNCTION speed(mode,time) 434 IMPLICIT NONE 435 436 INTEGER, INTENT(IN) :: MODE 437 REAL, INTENT(IN) :: time 438 439 IF( MODE == 1 ) THEN 440 speed=2.0 441 ELSE 442 speed=time/20.0 443 END IF 444 445 END FUNCTION speed 446!************************************ 447!* 448!* 449!************************************ 450 SUBROUTINE take_oe_sap_sot(mode,metod,time,n,c,dx) 451 IMPLICIT NONE 452 46

453 INTEGER, INTENT(IN) ::TIME,mode,metod,N 454 REAL, INTENT(IN) ::C(N),DX 455 456 IF(TIME==15) THEN 457 SELECT CASE(metod) 458 CASE(1) 459 IF(mode==1) THEN 460 CALL prit_to_file(c,'exp15.txt',n,dx) 461 ELSE 462 CALL prit_to_file(c,'exp_extra15.txt',n,dx) 463 END IF 464 CASE(2) 465 IF(mode==1) THEN 466 CALL prit_to_file(c,'lax15.txt',n,dx) 467 ELSE 468 CALL prit_to_file(c,'lax_extra15.txt',n,dx) 469 ENDIF 470 CASE(3) 471 IF(mode==1) THEN 472 CALL prit_to_file(c,'imp15.txt',n,dx) 473 ELSE 474 CALL prit_to_file(c,'imp_extra15.txt',n,dx) 475 END IF 476 CASE(4) 477 IF(mode==1) THEN 478 CALL prit_to_file(c,'cr15.txt',n,dx) 479 ELSE 480 CALL prit_to_file(c,'cr_extra15.txt',n,dx) 481 END IF 482 END SELECT 483 ELSE 484 SELECT CASE(metod) 485 CASE(1) 486 IF(mode==1) THEN 487 CALL prit_to_file(c,'exp30.txt',n,dx) 488 ELSE 489 CALL prit_to_file(c,'exp_extra30.txt',n,dx) 490 END IF 491 CASE(2) 492 IF(mode==1) THEN 493 CALL prit_to_file(c,'lax30.txt',n,dx) 494 ELSE 495 CALL prit_to_file(c,'lax_extra30.txt',n,dx) 496 ENDIF 497 CASE(3) 498 IF(mode==1) THEN 499 CALL prit_to_file(c,'imp30.txt',n,dx) 500 ELSE 501 CALL prit_to_file(c,'imp_extra30.txt',n,dx) 502 END IF 503 CASE(4) 504 IF(mode==1) THEN 505 CALL prit_to_file(c,'cr30.txt',n,dx) 506 ELSE 507 CALL prit_to_file(c,'cr_extra30.txt',n,dx) 508 END IF 509 END SELECT 510 END IF 511 END SUBROUTINE take_oe_sap_sot 512!************************************ 513!* 514!* 515!************************************ 516 REAL FUNCTION RMS(CEXACT,C,N) 517 IMPLICIT NONE 518 519 REAL, INTENT(IN) :: CEXACT(N),C(N) 520 INTEGER, INTENT(IN) :: N 521 522 INTEGER :: I 523 524 RMS=0. 525 DO I=1,N 526 RMS = RMS+(CEXACT(I)-C(I))**2 527 END DO 528 47

529 RMS = RMS/N 530 RMS = SQRT(RMS) 531 END FUNCTION RMS 532!************************************ 533!* 534 535!************************************ 536 SUBROUTINE get_curret_exact_solutio(cexact,n,curret_first_limit,dx) 537 IMPLICIT NONE 538 REAL, INTENT(IN) :: curret_first_limit,dx 539 REAL, INTENT(OUT) :: CEXACT(0:N-1) 540 INTEGER, INTENT(IN) :: N 541 542 INTEGER :: I 543 REAL :: first_limit 544 REAL :: secod_limit 545 REAL :: av,r,sift,x,pi 546 547 PARAMETER( PI = ACOS(-1.) ) 548 549 first_limit = 25.0 550 secod_limit = 35.0 551 552 sift = curret_first_limit - FIRST_LIMIT 553 first_limit = curret_first_limit 554 secod_limit = secod_limit + sift 555 556 av = (secod_limit+first_limit)/2.0 557 R = av - first_limit 558 559 CEXACT = 0. 560 x = 0. 561 DO I = 0,N-1 562 563 IF( x first_limit.and. x secod_limit ) THEN 564 CEXACT(I) = 1 + COS( PI * (x -av)/r ) 565 END IF 566 567 x = x + DX 568 END DO 569 END SUBROUTINE get_curret_exact_solutio 7 Refereces 1. Numerical Metods for pysics. Secod editio. Alejadro Garcia 2. Applied Numerical Metods for Egieers. Terrece Akal. 3. Computatioal Teciques for fluid dyamics. Secod editio. C.A.J.Fletcer 48