\(\(\( (*This\ Mathematica\ notebook\ load\ the\ FORTRAN\ program\ advection\ output\ and\ generate\ the\ plots\ and\ animation*) \)\(
\)\( (*by\ Nasser\ Abbasi, \ June\ 13, \ 
      2006*) \)\(
\)\(
\)\(Remove["Global`*"]


    << Graphics`Graphics`

    << Graphics`MultipleListPlot`

    << Graphics`Colors`

    Needs["Graphics`Legend`"]
\ 

    SetDirectory["c:/nasser/nabbasi_Web_page/my_courses/UCI_COURSES/CREDIT_COURSES/spring_2006/spring_TA_MAE_185/exams/final/my_solution"]


    \(makeString[time_, rmse_] := 
        Module[{cpu, dt, dx, mode, 
            method}, 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ "current time     = " <> ToString[time] <> "  min.
current RMSE = " <> 
            ToString[
              rmse]
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ];\)

    \(makeString2[rest_, maxRmse_] := 
        Module[{cpu, dt, dx, mode, 
            method}, 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ cpu = 
            rest[\([1]\)]; 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ dt = rest[\([2]\)]; 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ dx = 
            rest[\([3]\)]; 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ If[rest[\([4]\)] == 1, mode = "speed U=2 ft/min", 
            mode = "speed U=t/20 ft/min"\ ]; 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ If[rest[\([5]\)] == 1, 
            method = "FTCS"]; 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ If[rest[\([5]\)] == 2, 
            method = "LAX"]; 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ If[rest[\([5]\)] == 3, 
            method = "Implicit FTCS"]; 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ If[rest[\([5]\)] == 4, 
            method = "C-N"]; 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ Return[
            method <> "
" <> "Max RMSE=" <> 
              ToString[maxRmse] <> "
" <> mode <> "
CPU=" <> 
              ToString[cpu] <> " sec.
" <> "dt=" <> 
              ToString[dt] <> " min.
" <> "dx=" <> 
              ToString[
                dx] <> " ft"]
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ];\)


    $TextStyle = {FontFamily -> "Times", 
        FontSize -> 11}


    \(process[fileName_] := 
        Module[{}, 

A = 
            Import[fileName <> ".txt", "Table"]; 
{nRow, nCol} = 
            Dimensions[A]; 

a = 
            Max[A[\([Range[1, nRow - 1], 
                  Range[3, 
                    nCol]]\)]]; 
Print["Max. element=", 
            a]; 
Print["Length[a]=", 
            Length[a]]; 
If[Length[a] > 1 || a > 1000, 
            Return[]]; 

b = 
            Min[A[\([Range[1, nRow - 1], 
                  Range[3, nCol]]\)]]; 
maxRmse = 
            Max[\ A[\([\ \ Range[1, nRow - 1], 
                  2]\)]\ \ ]; 

p = 
            Table[MultipleListPlot[\ A[\([1, Range[3, nCol]]\)], 
                A[\([i, 
                    Range[3, 
                      nCol]]\)], 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ PlotRange -> {All, {b, 
                      a}}\ , 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ PlotLabel -> 
                  makeString[A[\([i, 1]\)], 
                    A[\([i, 
                        2]\)]], 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ PlotJoined -> {True, 
                    True}, 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ Frame -> 
                  False, 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ PlotStyle -> {{Red}, {Black}}, 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ SymbolShape -> 
                  
                  None, \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ AxesLabel -> {"X ft", "C(x,t)"}, 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ DisplayFunction\  -> \ 
                  Identity, 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ Ticks -> {Table[{x, x/10. }, {x, 
                        0, 1000, 100}], 
                    Automatic}, 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ Epilog -> 
                  Text[makeString2[\ A[\([\ nRow, Range[1, 5]\ ]\)], \ 
                      maxRmse], 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ {155,  .8*
                        a}
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ]
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ], 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ {i, 
                1, nRow - 
                  1}
\ \ \ \ \ \ \ \ \ \ \ ]; 

Export[
            fileName <> ".gif", \ 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ p, \ 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ ConversionOptions\  -> \ {"AnimationDisplayTime"\  -> \ 0.5, \ "Loop"\  -> \ 
                  False, "GlobalColorReduction" -> 
                  True}, \ 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ ImageSize\  -> \ {460, \ 
                Automatic}
\ \ \ \ \ \ \ \ \ \ \ \ ];
];\)


    \(makeString3[name_] := 
        Module[{}, 
If[
            name == "exp" || name == "exp_extra", 
            Return["Explicit FTCS"]]; 
If[
            name == "lax" || name == "lax_extra", 
            Return["LAX solution"]]; 
If[
            name == "imp" || name == "imp_extra", 
            Return["Implicit FTCS"]]; 
If[
            name == "cr" || name == "cr_extra", 
            Return["Crank-Nicolson"]];
];\)


    \(makeString4[rest_] := 
        Module[{cpu, dt, dx, mode, 
            method}, 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ cpu = 
            rest[\([1]\)]; 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ dt = rest[\([2]\)]; 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ dx = 
            rest[\([3]\)]; 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ If[rest[\([4]\)] == 1, mode = "speed U=2 ft/min,  ", 
            mode = "speed U=t/20 ft/min,  "]; 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ mode <> "dt=" <> 
            ToString[dt] <> " min.   " <> "dx=" <> 
            ToString[
              dx] <> " ft."
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ];\)


    \(processSnaps[name_] := 
        Module[{}, 
A = 
            Import[name <> "All.txt", "Table"]; 
{nRow, nCol} = 
            Dimensions[A]; 

a = 
            Max[A[\([Range[1, nRow - 1], 
                  Range[3, nCol]]\)]]; 
If[
            Length[a] > 1 || a > 1000, 
            Return[ListPlot[{{0, 0}, {1, 1}}, 
                PlotLabel -> 
                  makeString3[name] <> " Blows Up, Not Shown !", 
                DisplayFunction\  -> \ 
                  Identity]]]; 

e1 = 
            Import[name <> "0.txt", "Table"]; 
e2 = 
            Import[name <> "15.txt", "Table"]; 
e3 = Import[name <> "30.txt", "Table"]; 
p = 
            MultipleListPlot[\ e1[\([All, All]\)], e2[\([All, All]\)], 
              e3[\([All, 
                  All]\)], 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ PlotRange -> {All, 
                  All}\ , 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ PlotLabel -> 
                makeString3[
                  
                  name], 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ PlotJoined -> {True, True, 
                  True}, 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ Frame -> 
                False, 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ PlotStyle -> {{Red}, {Black}, {Blue}}, 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ SymbolShape -> 
                None, \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ AxesLabel -> {"X ft", "C(x,t)"}, 
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ DisplayFunction\  -> \ 
                Identity
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ]; 
Return[
            p];
];\)


    \(processAll[case_] := 
        Module[{}, 
SetDirectory[
            case]; 

A = 
            Import["expAll.txt", "Table"]; 
{nRow, 
              nCol} = Dimensions[
              A]; 

p1 = 
            processSnaps["exp"]; 
p2 = 
            processSnaps["lax"]; 
p3 = 
            processSnaps["imp"]; 
p4 = 
            processSnaps["cr"]; 
p = 
            DisplayTogetherArray[{{p1, p2}, {p3, p4}}, 
              PlotLabel -> "Solution at t=0,15,30 minutes.
" <> 
                  makeString4[
                    A[\([nRow, 
                        Range[1, 
                          5]]\)]]]; 

Export[
            case <> "_SNAP.gif", \ 
\ \ \ \ \ \ \ \ \ \ p\ , \ ImageSize\  -> \ {720, \ 
                Automatic}]; 

A = 
            Import["exp_extraAll.txt", "Table"]; 
{nRow, nCol} = 
            Dimensions[
              A]; 


p1 = processSnaps["exp_extra"]; 
p2 = 
            processSnaps["lax_extra"]; 
p3 = 
            processSnaps["imp_extra"]; 
p4 = 
            processSnaps["cr_extra"]; 
p = 
            DisplayTogetherArray[{{p1, p2}, {p3, p4}}, 
              PlotLabel -> "Solution at t=0,15,30 minutes.
" <> 
                  makeString4[
                    A[\([nRow, 
                        Range[1, 
                          5]]\)]]]; 

Export[
            case <> "_SNAP_extra.gif", \ 
\ \ \ \ \ \ \ \ \ \ p\ , \ 
            ImageSize\  -> \ {720, \ 
                Automatic}]; 

process["expAll"]; 
process["exp_extraAll"]; 
process["laxAll"]; 
process["lax_extraAll"]; 
process["impAll"]; 
process["imp_extraAll"]; 
process["crAll"]; 
process["cr_extraAll"]; 

SetDirectory[".."];
];\)



    \)\)\)

processAll["CASE_1"];
processAll["CASE_2"];
processAll["CASE_3"];
processAll["CASE_4"];
processAll["CASE_5"];
processAll["CASE_6"];
processAll["CASE_7"];
processAll["CASE_8"];
processAll["CASE_9"];

expFTCS1 = {{0.001, 20}, {0.001, 2.42}, {0.0013, 1.9}, {0.0015, 
            1.7}, {0.045, 0.73}, {0.05025, 0.7}, {0.06, 0.65}, {0.07, 
            0.5}};

expFTCS = {{0.001, 2.42}, {0.0013, 1.9}, {0.0015, 1.7}, {0.045, 
            0.73}, {0.05025, 0.7}, {0.06, 0.65}, {0.07, 
            0.5}};
xRange = {0, 0.07};
yRange = {0, 
          Max[ expFTCS[[All, 2]] ] };
ListPlot[expFTCS, PlotJoined -> True, 
        PlotRange -> { xRange, 
            yRange}];

 lax1 = {{0.001, 31}, {0.001, 3.48}, {0.0013, 2.78}, {0.0015, 
            2.56}, {0.045, 0.281}, {0.05025, 0.25}, {0.06, 0.9}, {0.07, 
            0.89}};
 lax = {{0.001, 3.48}, {0.0013, 2.78}, {0.0015, 2.56}, {0.045, 
            0.281}, {0.05025, 0.25}, {0.06, 0.9}, {0.07, 
            0.89}};
xRange = {0, 0.07};
yRange = {0, 
          Max[ lax[[All, 
                2]] ] };

ListPlot[lax, PlotJoined -> True, 
        PlotRange -> { xRange, 
            yRange}];

 imp1 = {{0.001, 45}, {0.001, 4.7}, {0.0013, 3.7}, {0.0015, 
            3.34}, {0.045, 0.437}, {0.05025, 0.5}, {0.06, 0.41}, {0.07, 
            0.45}};
 imp = {{0.001, 4.7}, {0.0013, 3.7}, {0.0015, 3.34}, {0.045, 
            0.437}, {0.05025, 0.5}, {0.06, 0.41}, {0.07, 
            0.45}};
xRange = {0, 0.07};
yRange = {0, 
          Max[ imp[[All, 2]] ] };
ListPlot[imp, PlotJoined -> True, 
        PlotRange -> { xRange, 
            yRange}];

 cr1 = {{0.001, 49}, {0.001, 4.9}, {0.0013, 3.9}, {0.0015, 
            3.45}, {0.045, 0.4}, {0.05025, 0.468}, {0.06, 0.41}, {0.07, 
            0.36}};
  cr = {{0.001, 4.9}, {0.0013, 3.9}, {0.0015, 3.45}, {0.045, 
            0.4}, {0.05025, 0.468}, {0.06, 0.41}, {0.07, 
            0.36}};
xRange = {0, 0.07};
yRange = {0, 
          Max[ cr[[All, 2]] ] };
ListPlot[cr, PlotJoined -> True, 
        PlotRange -> { xRange, yRange}];
expFTCS1 // TableForm;
lax1 // TableForm;
imp1 // TableForm;
cr1 // TableForm;

\(MultipleListPlot[expFTCS, lax, imp, cr, PlotJoined -> True, 
      PlotStyle -> {{Red}, {Black}, {Red, 
            AbsoluteDashing[{5}]}, {Blue}}, \ \ \ \ \ SymbolShape -> 
        None, PlotLegend -> {"Explicit FTCS", "Explicit Lax", "Implicit FTCS", "C-R"}, 
      PlotLabel -> "CPU time for constant speed", 
      AxesLabel -> {"time step in minute", "CPU time in sec"}]

 (*VARYING\ SPEED*) 
\)
expFTCSExtra1 = {{0.001, 21}, {0.001, 2.5}, {0.0013, 2}, {0.0015, 
            1.8}, {0.045, 0.28}, {0.05025, 0.28}, {0.06, 0.26}, {0.07, 
            0.23}};
expFTCSExtra = {{0.001, 2.5}, {0.0013, 2}, {0.0015, 1.8}, {0.045, 
            0.28}, {0.05025, 0.28}, {0.06, 0.26}, {0.07, 
            0.23}};

xRange = {0, 0.07};
yRange = {0, 
          Max[ expFTCSExtra[[All, 
                2]] ] };
ListPlot[expFTCSExtra, PlotJoined -> True, 
        PlotRange -> { xRange, 
            yRange}];

 laxExtra1 = {{0.001, 31}, {0.001, 3.5}, {0.0013, 2.9}, {0.0015, 
            2.53}, {0.045, 0.54}, {0.05025, 0.31}, {0.06, 0.29}, {0.07, 
            0.3}};
 laxExtra = {{0.001, 3.5}, {0.0013, 2.9}, {0.0015, 2.53}, {0.045, 
            0.54}, {0.05025, 0.31}, {0.06, 0.29}, {0.07, 
            0.3}};
 
xRange = {0, 0.07};
yRange = {0, 
          Max[ laxExtra[[All, 
                2]] ] };

ListPlot[laxExtra, PlotJoined -> True, 
        PlotRange -> { xRange, 
            yRange}];

 impExtra1 = {{0.001, 67}, {0.001, 7}, {0.0013, 5.56}, {0.0015, 
            4.73}, {0.045, 0.45}, {0.05025, 0.45}, {0.06, 0.36}, {0.07, 
            0.31}};
 impExtra = {{0.001, 7}, {0.0013, 5.56}, {0.0015, 4.73}, {0.045, 
            0.45}, {0.05025, 0.45}, {0.06, 0.36}, {0.07, 
            0.31}};
 
xRange = {0, 0.07};
yRange = {0, 
          Max[ impExtra[[All, 2]] ] };
ListPlot[impExtra, PlotJoined -> True, 
        PlotRange -> { xRange, 
            yRange}];

 crExtra1 = {{0.001, 69}, {0.001, 7.5}, {0.0013, 6}, {0.0015, 
            5}, {0.045, 0.45}, {0.05025, 0.56}, {0.06, 0.36}, {0.07, 
            0.31}};
  crExtra = {{0.001, 7.5}, {0.0013, 6}, {0.0015, 5}, {0.045, 
            0.45}, {0.05025, 0.56}, {0.06, 0.36}, {0.07, 
            0.31}};
xRange = {0, 0.07};
yRange = {0, 
          Max[ crExtra[[All, 2]] ] };
ListPlot[crExtra, PlotJoined -> True, 
        PlotRange -> { xRange, 
            yRange}];

\(MultipleListPlot[expFTCSExtra, laxExtra, impExtra, crExtra, 
      PlotJoined -> True, 
      PlotStyle -> {{Red}, {Black}, {Red, 
            AbsoluteDashing[{5}]}, {Blue}}, \ \ \ \ \ SymbolShape -> 
        None, PlotLegend -> {"Explicit FTCS", "Explicit Lax", "Implicit FTCS", "C-R"}, 
      PlotLabel -> "CPU time for varying speed", 
      AxesLabel -> {"time step in minute", "CPU time in sec"}]

 (*constant\ speed\ vs\ varying*) \)
\(MultipleListPlot[expFTCS, expFTCSExtra, PlotJoined -> True, 
      PlotStyle -> {{Red}, {Black}}, \ \ \ \ \ SymbolShape -> None, 
      PlotLegend -> {"Explicit FTCS
 fixed speed", "Explicit FTCS
 varying speed"}, 
      PlotLabel -> "CPU for explicit FTCS. fixed vs varying speed", 
      AxesLabel -> {"time step in minute", "CPU time in sec"}]
\)
\(MultipleListPlot[lax, laxExtra, PlotJoined -> True, 
      PlotStyle -> {{Red}, {Black}}, \ \ \ \ \ SymbolShape -> None, 
      PlotLegend -> {"Explicit lax
 fixed speed", "Explicit lax
 varying speed"}, 
      PlotLabel -> "CPU for explicit lax. fixed vs varying speed", 
      AxesLabel -> {"time step in minute", "CPU time in sec"}]
\)
\(MultipleListPlot[imp, impExtra, PlotJoined -> True, 
      PlotStyle -> {{Red}, {Black}}, \ \ \ \ \ SymbolShape -> None, 
      PlotLegend -> {"Implicit FTCS
 fixed speed", "Implicit FTCS
 varying speed"}, 
      PlotLabel -> "CPU for implicit FTCS. fixed vs varying speed", 
      AxesLabel -> {"time step in minute", "CPU time in sec"}]
\)
\(MultipleListPlot[cr, crExtra, PlotJoined -> True, 
      PlotStyle -> {{Red}, {Black}}, \ \ \ \ \ SymbolShape -> None, 
      PlotLegend -> {"C-R
 fixed speed", "C-R
 varying speed"}, 
      PlotLabel -> "CPU for Crank-Nicolson. fixed vs varying speed", 
      AxesLabel -> {"time step in minute", "CPU time in sec"}]
\)

?MultipleListPlot

2*0.045/0.1