1 (* ****************************************************************
3 Provide basic capabilities to
read PLUTO binary files
5 Last Modified: Jan 23, 2013
6 Author: A. Mignone (mignone@ph.unito.it)
7 **************************************************************** *)
9 readFloatMatrix[fileName_,nx_,ny_,nv_,type_] := Module[{
file, obj},
10 file = OpenRead[fileName, BinaryFormat -> True];
11 If [type == 4, obj = BinaryReadList[file, "Real32", nx*ny*nv]];
12 If [type == 8, obj = BinaryReadList[file, "Real64", nx*ny*nv]];
14 Partition[Partition[obj, nx],ny]
17 (* *******************************************************
18 Read
Grid information from grid.out
19 ******************************************************* *)
21 file = OpenRead[
"grid.out"];
23 {str=Read[file,String],
24 If[UnsameQ[StringTake[str,1],"#"], Break[]]
27 nx = Read[file,Number];
28 xgrid = Partition[ReadList[file,Number,nx*3],3];
30 ny = Read[file,Number];
31 ygrid = Partition[ReadList[file,Number,ny*3],3];
35 (* *******************************************************
36 Read .out
file and
get various information
37 ******************************************************* *)
39 nout = Input[
"Enter output number"]
40 type = Input[
"Enter precision type (4 = real/8 = double)"]
42 If [type == 4, {
file = OpenRead["flt.out"], suf =
".flt"} ]
43 If [type == 8, {file = OpenRead["dbl.out"], suf = ".dbl"} ]
45 For[i=0, i <= nout, i++, {str=Read[file,String]}];
48 strlist = StringSplit[str]
55 Print["Sim. time: "<>ToString[t]]
56 Print["Grid size: "<>ToString[nx]<>" x "<>ToString[ny]]
57 Print["File mode: "<>mode]
58 Print["Variables: "<>ToString[nvar]]
61 Print[ToString[i-6]<>" -> "<>strlist[[i]]];
65 varnames = strlist[[7;;n]]
67 (* *******************************************************
69 ******************************************************* *)
71 If [mode === "single_file",
72 If [nout < 10, fname = "data.000"<>ToString[nout]<>suf,
73 If [nout < 100, fname = "data.00"<>ToString[nout]<>suf,
74 If [nout < 1000, fname = "data.0"<>ToString[nout]<>suf,
75 If [nout < 1000, fname = "data."<>ToString[nout]<>suf];
79 data = readFloatMatrix[fname,nx,ny,nvar,type];
83 If [mode === "multiple_files",
85 For[n=1, n <= nvar, n++,
86 {If [nout < 10, fname = ".000"<>ToString[nout]<>suf,
87 If [nout < 100, fname = ".00"<>ToString[nout]<>suf,
88 If [nout < 1000, fname = ".0"<>ToString[nout]<>suf,
89 If [nout < 1000, fname = "."<>ToString[nout]<>suf];
93 fname = varnames[[n]]<>fname;
95 data = Join[data,readFloatMatrix[fname,nx,ny,1,type]];
101 (* ** Pload binary data ** *)
103 xmin = 0.5*(xgrid[[ 1,2]] + xgrid[[ 1,3]])
104 xmax = 0.5*(xgrid[[nx,2]] + xgrid[[nx,3]])
106 ymin = 0.5*(ygrid[[ 1,2]] + ygrid[[ 1,3]])
107 ymax = 0.5*(ygrid[[ny,2]] + ygrid[[ny,3]])
115 nv = Input["Enter variable to plot"]
117 ArrayPlot[data[[nv]], ColorFunction -> "Rainbow",
118 DataReversed -> True,
119 DataRange -> {{xmin,xmax},{ymin,ymax}},
120 Frame -> True, FrameTicks -> Automatic]
122 (* the following should plot
a vertical cut along y at
i = 40 of rho
123 ListPlot[data[[1, 1 ;; ny, 40]]]
127 (* the following should plot
a horizontal cut along x at
j = 20 of vx
128 ListPlot[data[[2,20]]]
(*****************************************************************)(*xmin=0.5 *(xgrid[[1, 2]]+xgrid[[1, 3]]) xmax=0.5 *(xgrid[[nx, 2]]+xgrid[[nx, 3]]) ymin=0.5 *(ygrid[[1, 2]]+ygrid[[1, 3]]) ymax=0.5 *(ygrid[[ny, 2]]+ygrid[[ny, 3]])*) pload(mignone @ph.unito.it) *****************************************************************) readFloatMatrix[fileName_, nx_, ny_, nv_, type_]:=Module[{file, obj}, file=OpenRead[fileName, BinaryFormat-> True];If[type==4, obj=BinaryReadList[file,"Real32", nx *ny *nv]];If[type==8, obj=BinaryReadList[file,"Real64", nx *ny *nv]];Close[file];Partition[Partition[obj, nx], ny]](******************************************************** Read Grid information from grid.out ********************************************************) file=OpenRead["grid.out"];For[i=1, i< 10, i++, {str=Read[file, String], If[UnsameQ[StringTake[str, 1],"#"], Break[]] }]; nx =Read[file, Number];xgrid=Partition[ReadList[file, Number, nx *3], 3];ny =Read[file, Number];ygrid=Partition[ReadList[file, Number, ny *3], 3];Close[file](******************************************************** Read.out file and get various information ********************************************************) nout=Input["Enter output number"]type=Input["Enter precision type (4 = real/8 = double)"]If[type==4,{file=OpenRead["flt.out"], suf=".flt"}]If[type==8,{file=OpenRead["dbl.out"], suf=".dbl"}]For[i=0, i<=nout, i++,{str=Read[file, String]}];Close[file]strlist=StringSplit[str]t =strlist[[2]]dt =strlist[[3]]mode =strlist[[5]]n =Length[strlist]nvar=n-6Print["Sim. time: "<>ToString[t]]Print["Grid size: "<>ToString[nx]<>" x "<>ToString[ny]]Print["File mode: "<>mode]Print["Variables: "<>ToString[nvar]]For[i=7, i<=n, i++, {name=strlist[[i]]; Print[ToString[i-6]<>" -> "<>strlist[[i]]]; }];varnames=strlist[[7;;n]](******************************************************** Set file name(s) and read data ********************************************************) If[mode==="single_file", If[nout< 10, fname="data.000"<>ToString[nout]<>suf, If[nout< 100, fname="data.00"<>ToString[nout]<>suf, If[nout< 1000, fname="data.0"<>ToString[nout]<>suf, If[nout< 1000, fname="data."<>ToString[nout]<>suf]; ]; ];];data=readFloatMatrix[fname, nx, ny, nvar, type];];If[mode==="multiple_files", data={};For[n=1, n<=nvar, n++, {If[nout< 10, fname=".000"<>ToString[nout]<>suf, If[nout< 100, fname=".00"<>ToString[nout]<>suf, If[nout< 1000, fname=".0"<>ToString[nout]<>suf, If[nout< 1000, fname="."<>ToString[nout]<>suf]; ]; ]; ]; fname=varnames[[n]]<>fname; Print[fname]; data=Join[data, readFloatMatrix[fname, nx, ny, 1, type]]; };];](***Pload binary data ***