read_snap.m 11 KB
Newer Older
Lena Noack's avatar
Lena Noack committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
%
% Read snapshot of one time step for plot_snap
% These fields have additional boundary cells, that are cut in get_data.m before plotting the fields
%
function [data,input,program_dir,fname,fpath] = read_snap(finput=' ')

  if (finput==' ') 
    [fname, fpath, fltidx] = uigetfile("data_snapshot*.res"); 
    fname
    fpath
  else
    finput
    [fpath,name,ext] = fileparts(finput);
    fname = sprintf('%s%s', name, ext);
  endif
  
  input = read_input(fpath);

  
  program_dir = cd();  % store otave path to be able to return
  cd(fpath);

  data_snap = fopen(fname, 'rb');
  
  bin_b=fread(data_snap,1,'int32');
  name=char(fread(data_snap,2,'uchar')); % 2 character string
  name=strcat([name(1),name(2)]);
  bin_e=fread(data_snap,1,'int32');

  % dummy values if not in snapshot
  data.D=1;, data.kappa=1;, data.gsurf=1;, data.rho=1;, data.Cpref=1;, data.alpha=1;, data.k=1; 
  data.t_yr=1;, data.grain=1;, data.DT=1;, data.Ts=1;, data.eta=1;, data.Rg=1;
  data.vel_v=1;, data.c=1;, data.w=1;, data.wr=1;, data.fa=1;, data.fc=1;, data.g=1;
  data.rec=1;, data.Cp=1;, data.cond=1;, data.heat=1;, data.visc_diff=1;, data.ref_T=1;
  data.ref_r=1;, data.ref_a=1;, data.depl=1;, data.solidus=1;, data.solidus_ref=1;
  data.solidus_diff_w=1;, data.liquidus=1;, data.ds_dz=1;, data.dl_dz=1;, data.DeltaF=1;
  data.serp=1;, data.total_mass_H2O=1;, data.oc_vol_tot=1;, data.fc_vol_tot=1;, data.cc_vol_tot=1;
  data.cr_age=1;, data.output_tracer=1;, data.ref_g=1;, data.shear=1;, data.bulk_mod=1;
Lena Noack's avatar
Lena Noack committed
39
  data.ref_p=1;, data.T_old=1;, data.entropy=1;, data.FP=1;, data.FG=0;, data.MP=0;, data.CO2=1;
Lena Noack's avatar
Lena Noack committed
40
41
42
43
  data.U238=0;, data.U235=0;, data.Th232=0;, data.K40=0;
  data.m_vel_u=0.0;,data.m_vel_v=0.0;,data.m_vel_w=0.0;,data.mprod=0.0;,data.poros=0.0;

  while(strncmp(name,"EF",2)==0)
44
    %name
Lena Noack's avatar
Lena Noack committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
    if(name == "nl"), bin_b=fread(data_snap,1,'int32');, nl=fread(data_snap,1,'int32'), bin_e=fread(data_snap,1,'int32');
    elseif(name == "ny"), bin_b=fread(data_snap,1,'int32');, ny=fread(data_snap,1,'int32'), bin_e=fread(data_snap,1,'int32');
    elseif(name == "nr")
      bin_b=fread(data_snap,1,'int32');, nr=fread(data_snap,1,'int32'), bin_e=fread(data_snap,1,'int32');
      if(input.per == 1),nper=1;,else,nper=0;,endif
      if(ny == 1),n3D=0;,else,n3D=1;,endif
      data.nl = nl;
      data.ny = ny;
      data.nr = nr;
    elseif(name == "ND")
      bin_b=fread(data_snap,1,'int32');, data.D=fread(data_snap,1,'double');, data.kappa=fread(data_snap,1,'double');
      data.gsurf=fread(data_snap,1,'double');, data.rho=fread(data_snap,1,'double');, data.Cpref=fread(data_snap,1,'double');
      data.alpha=fread(data_snap,1,'double');, data.k=fread(data_snap,1,'double');, data.t_yr=fread(data_snap,1,'double');
      data.grain=fread(data_snap,1,'double');, data.DT=fread(data_snap,1,'double');, data.Ts=fread(data_snap,1,'double');
      data.eta=fread(data_snap,1,'double');, data.Rg=fread(data_snap,1,'double');, bin_e=fread(data_snap,1,'int32');
    elseif(name == "p" ), hnl=nl+2*nper;,hny=ny+2*n3D*nper;,hnr=nr;,data.p=read_matrix(hnl,hny,hnr,data_snap);
    elseif(name == "u" ), hnl=nl+1+nper;,hny=ny+2*n3D;,hnr=nr+2;,data.vel_u=read_matrix(hnl,hny,hnr,data_snap);
    elseif(name == "v" ), hnl=nl+2;,hny=ny+1+nper;,hnr=nr+2;,data.vel_v=read_matrix(hnl,hny,hnr,data_snap); %only called if 3D
    elseif(name == "w" ), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+1;,data.vel_w=read_matrix(hnl,hny,hnr,data_snap);
    elseif(name == "T" ), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+2;,data.T=read_matrix(hnl,hny,hnr,data_snap);
    elseif(name == "S" ), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+2;,data.strR=read_matrix(hnl,hny,hnr,data_snap);
    elseif(name == "eV"), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+2;,data.visc=read_matrix(hnl,hny,hnr,data_snap);
    elseif(name == "cm"), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+2;,data.c=read_matrix(hnl,hny,hnr,data_snap);
    elseif(name == "wa"), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+2;,data.w=read_matrix(hnl,hny,hnr,data_snap);
    elseif(name == "wr"), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+2;,data.wr=read_matrix(hnl,hny,hnr,data_snap);
    elseif(name == "fa"), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+2;,data.fa=read_matrix(hnl,hny,hnr,data_snap);
    elseif(name == "fc"), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+2;,data.fc=read_matrix(hnl,hny,hnr,data_snap);
    elseif(name == "g" ), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+2;,data.g=read_matrix(hnl,hny,hnr,data_snap);
    elseif(name == "r" ), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+2;,data.rec=read_matrix(hnl,hny,hnr,data_snap);
    elseif(name == "Cp"), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+2;,data.Cp=read_matrix(hnl,hny,hnr,data_snap);
    elseif(name == "k" ), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+2;,data.cond=read_matrix(hnl,hny,hnr,data_snap);
    elseif(name == "h" ), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+2;,data.heat=read_matrix(hnl,hny,hnr,data_snap);
    elseif(name == "vc"), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+2;,data.visc_diff=read_matrix(hnl,hny,hnr,data_snap);
    elseif(name == "Tr"), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+2;,data.ref_T=read_matrix(hnl,hny,hnr,data_snap);
    elseif(name == "rh"), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+2;,data.ref_r=read_matrix(hnl,hny,hnr,data_snap);
    elseif(name == "a" ), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+2;,data.ref_a=read_matrix(hnl,hny,hnr,data_snap);
    elseif(name == "d" ), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+2;,data.depl=read_matrix(hnl,hny,hnr,data_snap);
    elseif(name == "it"), bin_b=fread(data_snap,1,'int32');, data.iter=fread(data_snap,1,'int32');, bin_e=fread(data_snap,1,'int32');
    elseif(name == "t" ), bin_b=fread(data_snap,1,'int32');, data.time=fread(data_snap,1,'double');, bin_e=fread(data_snap,1,'int32');
    elseif(name == "dt"), bin_b=fread(data_snap,1,'int32');, data.time_step=fread(data_snap,1,'double');, bin_e=fread(data_snap,1,'int32');
    elseif(name == "um"), bin_b=fread(data_snap,1,'int32');, data.use_melt=fread(data_snap,1,'int32');, bin_e=fread(data_snap,1,'int32');
    elseif(name == "so"), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+2;,data.solidus=read_matrix(hnl,hny,hnr,data_snap);
    elseif(name == "sr"), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+2;,data.solidus_ref=read_matrix(hnl,hny,hnr,data_snap);
    elseif(name == "sw"), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+2;,data.solidus_diff_w=read_matrix(hnl,hny,hnr,data_snap);
    elseif(name == "li"), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+2;,data.liquidus=read_matrix(hnl,hny,hnr,data_snap);
    elseif(name == "ds"), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+2;,data.ds_dz=read_matrix(hnl,hny,hnr,data_snap);
    elseif(name == "dl"), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+2;,data.dl_dz=read_matrix(hnl,hny,hnr,data_snap);
    elseif(name == "df"), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+2;,data.DeltaF=read_matrix(hnl,hny,hnr,data_snap);
    elseif(name == "sp"), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+2;,data.serp=read_matrix(hnl,hny,hnr,data_snap);
94
95
96
97
98
    elseif(name == "tH"), bin_b=fread(data_snap,1,'int32');, data.total_mass_H2O=fread(data_snap,1,'double');, bin_e=fread(data_snap,1,'int32'); % old version, now M1-M4
    elseif(name == "M1"), bin_b=fread(data_snap,1,'int32');, data.total_mass_H2O=fread(data_snap,1,'double');, bin_e=fread(data_snap,1,'int32');
    elseif(name == "M2"), bin_b=fread(data_snap,1,'int32');, data.total_mass_CO2=fread(data_snap,1,'double');, bin_e=fread(data_snap,1,'int32');
    elseif(name == "M3"), bin_b=fread(data_snap,1,'int32');, data.total_mass_H2=fread(data_snap,1,'double');, bin_e=fread(data_snap,1,'int32');
    elseif(name == "M4"), bin_b=fread(data_snap,1,'int32');, data.total_mass_CO=fread(data_snap,1,'double');, bin_e=fread(data_snap,1,'int32');
Lena Noack's avatar
Lena Noack committed
99
100
101
102
103
104
105
106
107
108
109
110
111
    elseif(name == "oc"), hnl=nl+2;,hny=ny+2*n3D;,hnr=1;,data.oc_vol_tot=read_matrix(hnl,hny,hnr,data_snap); %2D matrix, no radial dependence
    elseif(name == "fe"), hnl=nl+2;,hny=ny+2*n3D;,hnr=1;,data.fc_vol_tot=read_matrix(hnl,hny,hnr,data_snap); %2D matrix, no radial dependence
    elseif(name == "cc"), hnl=nl+2;,hny=ny+2*n3D;,hnr=1;,data.cc_vol_tot=read_matrix(hnl,hny,hnr,data_snap); %2D matrix, no radial dependence
    elseif(name == "cr"), hnl=nl+2;,hny=ny+2*n3D;,hnr=1;,data.cr_age=read_matrix(hnl,hny,hnr,data_snap); %2D matrix, no radial dependence
    elseif(name == "o" ), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+2;,data.output_tracer=read_matrix(hnl,hny,hnr,data_snap);
    elseif(name == "rg"), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+2;,data.ref_g=read_matrix(hnl,hny,hnr,data_snap);
    elseif(name == "sh"), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+2;,data.shear=read_matrix(hnl,hny,hnr,data_snap);
    elseif(name == "bu"), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+2;,data.bulk_mod=read_matrix(hnl,hny,hnr,data_snap);
    elseif(name == "rp"), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+2;,data.ref_p=read_matrix(hnl,hny,hnr,data_snap);
    elseif(name == "To"), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+2;,data.T_old=read_matrix(hnl,hny,hnr,data_snap);
    elseif(name == "en"), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+2;,data.entropy=read_matrix(hnl,hny,hnr,data_snap);
    elseif(name == "fp"), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+2;,data.FP=read_matrix(hnl,hny,hnr,data_snap);
    elseif(name == "fg"), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+2;,data.FG=read_matrix(hnl,hny,hnr,data_snap);
Lena Noack's avatar
Lena Noack committed
112
    elseif(name == "ph"), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+2;,data.MP=read_matrix(hnl,hny,hnr,data_snap);
Lena Noack's avatar
Lena Noack committed
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
    elseif(name == "co"), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+2;,data.CO2=read_matrix(hnl,hny,hnr,data_snap);
    elseif(name == "ul"), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+2;,data.U238=read_matrix(hnl,hny,hnr,data_snap); 
    elseif(name == "us"), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+2;,data.U235=read_matrix(hnl,hny,hnr,data_snap); 
    elseif(name == "th"), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+2;,data.Th232=read_matrix(hnl,hny,hnr,data_snap);
    elseif(name == "kp"), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+2;,data.K40=read_matrix(hnl,hny,hnr,data_snap); 
    elseif(name == "mu"), hnl=nl+1+nper;,hny=ny+2*n3D;,hnr=nr+2;,data.m_vel_u=read_matrix(hnl,hny,hnr,data_snap);
    elseif(name == "mv"), hnl=nl+2;,hny=ny+1+nper;,hnr=nr+2;,data.m_vel_v=read_matrix(hnl,hny,hnr,data_snap); %only called if 3D
    elseif(name == "mw"), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+1;,data.m_vel_w=read_matrix(hnl,hny,hnr,data_snap);
    elseif(name == "mp"), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+2;,data.mprod=read_matrix(hnl,hny,hnr,data_snap);
    elseif(name == "po"), hnl=nl+2;,hny=ny+2*n3D;,hnr=nr+2;,data.poros=read_matrix(hnl,hny,hnr,data_snap);
    elseif(name == "Tt"), bin_b=fread(data_snap,1,'int32');, data.Ttop=fread(data_snap,1,'double');, bin_e=fread(data_snap,1,'int32');
    elseif(name == "Tb"), bin_b=fread(data_snap,1,'int32');, data.Tbottom=fread(data_snap,1,'double');, bin_e=fread(data_snap,1,'int32');
    else
      fprintf('Error: %s not known\n', name)
    endif

    bin_b=fread(data_snap,1,'int32');
    name=char(fread(data_snap,2,'uchar')); % 2 character string
    name=strcat([name(1),name(2)]);
    bin_e=fread(data_snap,1,'int32');
  endwhile

   Tbottom = data.Tbottom*data.DT+data.Ts
  
end 
  
function A = read_matrix(n1,n2,n3,data_snap)
      i=1;,j=1;,k=1;,A=zeros(n1,n2,n3);
      do,do,do
          bin_b=fread(data_snap,1,'int32');, A(i,j,k)=fread(data_snap,1,'double');, bin_e=fread(data_snap,1,'int32');,k=k+1;
          %fprintf("%d %d %d %f\n",i,j,k,A(i,j,k))
      until (k>n3),k=1;,j=j+1;,until (j>n2),j=1;,i=i+1;,until (i>n1)
end