%% Matlab supplement to "The development of deep-ocean anoxia in a comprehensive ocean phosphorus model"
% Referenced in manuscript as Online Resource 1
% Authors: J. G. Donohue, B. J. Florio, and A. C. Fowler
% Contact: J. G. Donohue (john.donohue.nui@gmail.com)
% Journal: International Journal of Geomathematics
%
% The script below computes the value of the critical anoxic parameter (\lambda_6*s_3 - \nu) for a given set of physical parameters.


% Carbon cycle
kCbur_prox  = 0.0905660;     
kCF1        = 38.6597938;      
kCF10       = 0.820594208;       
kCF11       = (496.6/(3600+28.0125));        
kCF12       = 8.8109658*1E-3;
kCF13       = 1.6/496.6;     
kCF4        = 1.0;              
kCF5        = 1.0589392;
kCF6        = 2.19836 ;
kCF7        = 2.7/(560.25+4.66); 
kCF8        = 1.0;              
kCF9        = 0.71893;          
kCrel_prox  = 6.9383815;        

% Phosphorus cycle
kCaP_prox   = 0.056559;   
kFeP_prox   = 0.925;     
korgP_prox  = 1.0;       
Kp          = 0.124;
kPF10       = 2.69703*1e-3;
kPF11       = 1.0;         
kPF12a      = 0.01;        
kPF12b      = 0.5;         
kPF13       = 2.18342;     
kPF14       = 0.00675/(0.044+5.28538);         
kPF15       = 1;
kPF19       = 0.811116;        
kPF20       = 0.01;             
kPF22       = 0.1197461;        
kPF23       = 0.5;              
kPF24       = 8.8267*1e-3;     
kPF25       = 0.5;             

kPF26       = 6.75*1e9;         
kPF27       = 2.8858*1e-3;      
kPF28       = 1.0;              
kPF29       = 0.0;

kPF4        = 1.0;   
kPF5a       = 0.01;   
kPF5b       = 0.5;      
kPF8        = 1.0;       
kPF9        = 0.00135238; 
kPrel_prox  = 7.4338699;

Redfield_CO2= 106/138;
Redfield_CP = 106;
CPoxdeep    = 237.0;
CPandeep    = 1100;

k_evap          = 37e12;    
k_river_water   = 37e12;     
k_river_P       = 0.09e12;   
k_fb_prox       = 0.0;       
k_Pfish_dis_surf= 0.0;       
k_f12c          = 0.0;       
k_O2_surf       = 0.325;     

KmO2            = 0.0001;     
C_NO3_init   = 0.0;  
C_RP_max     = 0.03;  
dcoeff       = 0.0;   
area_deep    = 3.32e14; 
dx           = 0.1;   
sedvel_t0    = 0.000; 
C_bur_deep_t0= 1.6e12;
kredox       = 1e8;   
kprec        = 1e-3;  


%define mixing parameters
vmix_oce = 0.1;
vmix_cstl = 0.1;

%Water constants:
W1=36e12;    
W2=3600e12; 
W3=49830e12;
W4=1297670e12;

%Water fluxes
r = k_river_water;
w12 = k_river_water;
w43 = 3780e12*vmix_oce;
w42 = 378e12*vmix_cstl;
E =  k_evap;
w23 = w12 + w42;
w34 = w43 + w42;



% dimensional quantities:
h28 = kCF13;
Km = KmO2;
gs = k_O2_surf;
z0 = w34/W4;
z1 = kCF12/Redfield_CO2;
z2 = kredox;
z3 = (1-kCbur_prox)*kCF1*Redfield_CP;
z4 = (kCrel_prox*W1 + kCF4*w12)/W1;
z5 = ((1 - kCF7)*kCF4*w12)/W2;
z6 = (1 - kCF7)*kCF5*Redfield_CP;
z7 = (w23*kCF8 + kCF6*W2)/W2;
z8 = (1 - kCF11)*kCF9*Redfield_CP;
z9 = ((1 - kCF11)*w23*kCF8)/W3;
z10 = kCF10;
z11 = (kCF11*w23*kCF8)/W4;
z12 = (kCF11*kCF9*Redfield_CP*W3)/W4;
z13 = kCF12;
z14 = kPrel_prox*(1-kCaP_prox);
z15 = kPF5b;
z16 = ((kCF1+kFeP_prox)*W1 + kPF4*w12)/W1;
z17 = (kCF1*W1*(1-(korgP_prox/400)*kCbur_prox*Redfield_CP - kPF5a))/W1;
z18 = (kPrel_prox*W1 + kPF8*w12)/W1;
z19 = kPF5a*kCF1;
z20 = kPF5b;
z21 = kPF4*w12/W2;
z22 = (1-kPF10)*kPF13;
z23 = kPF12b;
z24 = (kPF11*w23+W2*(kPF9 + kCF5))/W2;
z25 = kCF5*(1-kPF14 - kPF12a);
z26 = (kPF8*w12*(1-kPF14))/W2;
z27 = (kPF15*w23 + kPF13*W2)/W2;
z28 = kPF12a*kCF5;
z29 = kPF12b;
z30 = kPF19;
z31 = (kPF11*w23)/W3;
z32 = (kCF9*W3 + w34)/W3;
z33 = (kCF9*(1 - kPF20 - kCF11));
z34 = (kPF15*w23)/W3;
z35 = (kCF11*kCF8*w23/Redfield_CP)/W3;
z36 = kPF19;
z37 = kPF20*kCF9;
z38 = (kPF23);
z39 = kPF24*(1 - kPF27);
z40 = kPF25;
z41 = kCF11*kCF8*w23/W4;
z42 = (kCF11*kCF9*W3*Redfield_CP)/W4;
z43 = kPF24;
z44 = W3*kPF23*(1-kPF29)/W4;
z45 = kPF25;
z46 = (kCF12/Redfield_CO2);
z47 = (dcoeff*C_RP_max*kCbur_prox*area_deep/dx)/W4;
z49 = (kCF8*w23*sedvel_t0/C_bur_deep_t0*area_deep*kCF13*kCF11)/W4;
z50 = (kCF9*Redfield_CP*W3*sedvel_t0/C_bur_deep_t0*area_deep*kCF13*kCF11)/W4;
z52 = (kprec*W4)/W4;
z53 = k_river_P/W1; 
z54 = w42/W2;
z55 = k_f12c/W2;
z56 = w43/W3;
z57 = k_Pfish_dis_surf/W3;
z58 = w34/W4;
z59 = kPF26/W4;
z60 = k_fb_prox/W1;

d16 = z16 - z14*z17/z18;
d24 = z24 - z22*z25/z27;
d32b = z32 - z30*z33/z36 - z50*z39/z58*z42/(z43*Redfield_CP);
f32b = d24*d32b/z31-z54*z42*z39/(z43*Redfield_CP*z58);
e32b = f32b - z23*z28*d32b/(z29*z31);

%scales
S1 = z53/d16;
P1 = z17/z18*S1;
C1 = z3/z4*S1;
F1 = z19/z20*S1;
S3 = z21/e32b*S1;
S2 = d32b/z31*S3;
S4 = z39/z58*z42/(Redfield_CP*z43)*S3;
P4 = z42/(Redfield_CP*z43)*S3;
P2 = z25/z27*S2;
F2 = z28/z29*S2;
F3 = z37/z38*S3;
F4 = z44/z45*F3;
P3 = z33/z36*S3;
C2 = z6/z7*S2;
C3 = z8/z10*S3;
C4 = z12/z13*S3;
R1 = C_RP_max;
G4 = 10^6*z0*gs/(z2*R1);
R2 = 0;

%dimensionless parameters;
%; lambda
lam1 = z53/(z16*S1);
lam2 = z14*P1/(z16*S1);
lam3 = z22*P2/(z24*S2);
lam4 = z30*P3/(z32*S3);
lam5 = z59/(z58*S4);
lam11 = Km/G4;
lam20 = z58*R1/(z46*C4);
%; delta
del1 = z56*S4/(z32*S3);
del2 = z31*S2/(z32*S3);
del3 = z2*R1*G4/(10^6*z52);
del4 = z5*C1/(z7*C2);
del5 = z26*P1/(z27*P2);
%epsilon
eps1 = z15*F1/(z16*S1);
eps2 = z21*S1/(z24*S2);
eps3 = z23*F2/(z24*S2);
eps4 = z54*S4/(z24*S2);
eps6 = z40*F4/(z58*S4);
eps7 = S3/S4;
eps8 = z34*P2/(z36*P3);
eps9 = z35*C2/(z36*P3);
eps10 = h28*z41*C2/(237*z43*P4);
eps11 = h28*z42*S3/(237*z43*P4);
eps13 = z9*C2/(z10*C3);
eps14 = z11*C2/(z13*C4);
eps15 = h28*z11*C2/(z13*C4);
eps16 = h28*z12*S3/(z13*C4);
eps19 = G4/gs;
eps20 = z58/z16;
eps21 = z58/z24;
eps22 = z58/z32;
eps23 = z58/z18;
eps24 = z58/z27;
eps25 = z58/z36;
eps26 = z58/z43;
eps27 = z58/z20;
eps28 = z58/z4;
eps29 = z58/z7;
eps30 = z58/z10;
eps31 = z58/z13;
eps32 = z58*G4/(z0*gs);
eps33 = z41*C2/(Redfield_CP*z43*P4);
eps34 = z58/z29;
eps35 = z58/z38;
eps36 = z58/z45;
eps37 = z58*R1/z52;
eps38 = z46*C4/z52;
eps39 = z1*C4/(z0*gs);

%Rescales:
Sb1 = 1;
Pb1 = 1;
Cb1 = 1;
Fb1 = 1;
Sb3 = 1/((1-lam4-del1)*(1-lam3-eps3));
Sb2 = 1/(1-lam3-eps3);
Sb4 = Sb3;
Pb4 = Sb3;
Pb2 = Sb2;
Fb2 = Sb2;
Fb3 = Sb3;
Fb4 = Sb3;
Pb3 = Sb3;
Cb2 = Sb2;
Cb3 = Sb3;
Cb4 = Sb3;
Rb1 = 1;
Gb4 = 1;
Rb2 = 0;

%rescaled ND coefficients
lam6 = eps39*Cb4;
lam8 = eps38*Sb3;
eps40 = del4/Sb2;
eps41 = eps13*Sb2/Sb3;
eps42 = eps14*Sb2/Sb3;
eps43 = eps15*Sb2/Sb3;
eps44 = eps2/Sb2;
eps45 = eps4*Sb3/Sb2;
eps46 = del5/Sb2;
eps47 = del2*Sb2/Sb3;
eps48 = eps8*Sb2/Sb3;
eps49 = eps9*Sb2/Sb3;
eps50 = lam5/Sb4;
eps51 = eps33*Sb2/Sb3;
eps52 = eps10*Sb2/Sb3;
eps53 = 1- lam3;
eps54 = 1 - del1 - lam4;

del6=  (eps44+eps46)/eps45;
del7 = (eps47+eps48-eps49)/del1;
lam9 = 1 +eps6+eps7;
lam10 = (eps53-eps3)/(eps45);
lam12n = 1+eps54/del1;
eps55=  (eps21+eps24)/(eps45);
eps56=   (eps22+eps25)/(del1);


s_4_approx = (lam9*del6*del7)/(lam10*(lam12n-lam9)-del7*lam9);
s_3_approx = ( (lam10+del7)*s_4_approx+del6*del7)/(lam10*lam12n);

anoxia_parameter = lam6*s_3_approx
