Goby v2
swstate.h
1 // Copyright 2009-2018 Toby Schneider (http://gobysoft.org/index.wt/people/toby)
2 // GobySoft, LLC (2013-)
3 // Massachusetts Institute of Technology (2007-2014)
4 // Community contributors (see AUTHORS file)
5 //
6 //
7 // This file is part of the Goby Underwater Autonomy Project Libraries
8 // ("The Goby Libraries").
9 //
10 // The Goby Libraries are free software: you can redistribute them and/or modify
11 // them under the terms of the GNU Lesser General Public License as published by
12 // the Free Software Foundation, either version 2.1 of the License, or
13 // (at your option) any later version.
14 //
15 // The Goby Libraries are distributed in the hope that they will be useful,
16 // but WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU Lesser General Public License for more details.
19 //
20 // You should have received a copy of the GNU Lesser General Public License
21 // along with Goby. If not, see <http://www.gnu.org/licenses/>.
22 
23 // modified for C++ by s. petillo spetillo@mit.edu
24 // ocean engineering graduate student - mit / whoi joint program
25 // massachusetts institute of technology (mit)
26 // laboratory for autonomous marine sensing systems (lamss)
27 
28 #ifndef SWSTATEH
29 #define SWSTATEH
30 
31 #include <cmath>
32 
33 // Calculate water density anomaly at a given Salinity, Temperature, Pressure using the seawater Equation of State.
34 // Taken directly from MATLAB OceansToolbox swstate.m
35 
36 inline double density_anomaly(double S, double T, double P0)
37 {
38  /*
39 
40  SIGMA = density_anomaly(S,T,P) returns the density anomaly SIGMA (kg/m^3)
41  given the salinity S (ppt), temperature T (deg C) and pressure
42  P (dbars).
43 
44  Notes: RP (WHOI 2/dec/91)
45 
46  This stuff is directly copied from the UNESCO algorithms, with some
47  minor changes to make it Matlab compatible (like adding ";" and changing
48  "*" to "*" when necessary.
49 
50  RP (WHOI 3/dec/91)
51 
52  ******************************************************
53  SPECIFIC VOLUME ANOMALY (STERIC ANOMALY) BASED ON 1980 EQUATION
54  OF STATE FOR SEAWATER AND 1978 PRACTICAL SALINITY SCALE.
55  REFERENCES
56  MILLERO, ET AL (1980) DEEP-SEA RES.,27A,255-264
57  MILLERO AND POISSON 1981,DEEP-SEA RES.,28A PP 625-629.
58  BOTH ABOVE REFERENCES ARE ALSO FOUND IN UNESCO REPORT 38 (1981)
59 
60  UNITS:
61  PRESSURE P0 DECIBARS
62  TEMPERATURE T DEG CELSIUS (IPTS-68)
63  SALINITY S (IPSS-78)
64  SPEC. VOL. ANA. SVAN M**3/KG *1.0E-8
65  DENSITY ANA. SIGMA KG/M**3
66  ******************************************************************
67  CHECK VALUE: SVAN=981.3021 E-8 M**3/KG. FOR S = 40 (IPSS-78) ,
68  T = 40 DEG C, P0= 10000 DECIBARS.
69  CHECK VALUE: SIGMA = 59.82037 KG/M**3 FOR S = 40 (IPSS-78) ,
70  T = 40 DEG C, P0= 10000 DECIBARS.
71  CHECK VALUE: FOR S = 40 (IPSS-78) , T = 40 DEG C, P0= 10000 DECIBARS.
72  DR/DP DR/DT DR/DS
73  DRV(1,7) DRV(2,3) DRV(1,8)
74 
75  FINITE DIFFERENCE WITH 3RD ORDER CORRECTION DONE IN DOUBLE PRECSION
76 
77  3.46969238E-3 -.43311722 .705110777
78 
79  EXPLICIT DIFFERENTIATION SINGLE PRECISION FORMULATION EOS80
80 
81  3.4696929E-3 -.4331173 .7051107
82 
83  (RP...I think this ---------^^^^^^ should be -.4431173!);
84  */
85 
86  // *******************************************************
87  using namespace std;
88 
89  // DATA
90  double R3500 = 1028.1063;
91  double R4 = 4.8314E-4;
92  double DR350 = 28.106331;
93 
94  // CONVERT PRESSURE TO BARS AND TAKE SQUARE ROOT SALINITY.
95  double P = P0 / 10.;
96  //double SAL=S;
97  double SR = sqrt(abs(S));
98  // *********************************************************
99  // PURE WATER DENSITY AT ATMOSPHERIC PRESSURE
100  // BIGG P.H.,(1967) BR. J. APPLIED PHYSICS 8 PP 521-537.
101 
102  double R1 = ((((6.536332E-9 * T - 1.120083E-6) * T + 1.001685E-4) * T - 9.095290E-3) * T +
103  6.793952E-2) *
104  T -
105  28.263737;
106  // SEAWATER DENSITY ATM PRESS.
107  // COEFFICIENTS INVOLVING SALINITY
108  double R2 = (((5.3875E-9 * T - 8.2467E-7) * T + 7.6438E-5) * T - 4.0899E-3) * T + 8.24493E-1;
109  double R3 = (-1.6546E-6 * T + 1.0227E-4) * T - 5.72466E-3;
110  // INTERNATIONAL ONE-ATMOSPHERE EQUATION OF STATE OF SEAWATER
111  double SIG = (R4 * S + R3 * SR + R2) * S + R1;
112  // SPECIFIC VOLUME AT ATMOSPHERIC PRESSURE
113  double V350P = 1.0 / R3500;
114  double SVA = -SIG * V350P / (R3500 + SIG);
115  double SIGMA = SIG + DR350;
116  //double V0 = 1.0/(1000.0 + SIGMA);
117  // SCALE SPECIFIC VOL. ANAMOLY TO NORMALLY REPORTED UNITS
118  // double SVAN=SVA*1.0E+8;
119 
120  // ******************************************************************
121  // ****** NEW HIGH PRESSURE EQUATION OF STATE FOR SEAWATER ********
122  // ******************************************************************
123  // MILLERO, ET AL , 1980 DSR 27A, PP 255-264
124  // CONSTANT NOTATION FOLLOWS ARTICLE
125  // ********************************************************
126  // COMPUTE COMPRESSION TERMS
127  double E = (9.1697E-10 * T + 2.0816E-8) * T - 9.9348E-7;
128  double BW = (5.2787E-8 * T - 6.12293E-6) * T + 3.47718E-5;
129  double B = BW + E * S; // Bulk Modulus (almost)
130  // CORRECT B FOR ANAMOLY BIAS CHANGE
131  // double Bout = B + 5.03217E-5;
132 
133  double D = 1.91075E-4;
134  double C = (-1.6078E-6 * T - 1.0981E-5) * T + 2.2838E-3;
135  double AW = ((-5.77905E-7 * T + 1.16092E-4) * T + 1.43713E-3) * T - 0.1194975;
136  double A = (D * SR + C) * S + AW;
137  // CORRECT A FOR ANAMOLY BIAS CHANGE
138  // double Aout = A + 3.3594055;
139 
140  double B1 = (-5.3009E-4 * T + 1.6483E-2) * T + 7.944E-2;
141  double A1 = ((-6.1670E-5 * T + 1.09987E-2) * T - 0.603459) * T + 54.6746;
142  double KW = (((-5.155288E-5 * T + 1.360477E-2) * T - 2.327105) * T + 148.4206) * T - 1930.06;
143  double K0 = (B1 * SR + A1) * S + KW;
144 
145  // EVALUATE PRESSURE POLYNOMIAL
146  // ***********************************************
147  // K EQUALS THE SECANT BULK MODULUS OF SEAWATER
148  // DK=K(S,T,P)-K(35,0,P)
149  // K35=K(35,0,P)
150  // ***********************************************
151  double DK = (B * P + A) * P + K0;
152  double K35 = (5.03217E-5 * P + 3.359406) * P + 21582.27;
153  double GAM = P / K35;
154  double PK = 1.0 - GAM;
155  SVA = SVA * PK + (V350P + SVA) * P * DK / (K35 * (K35 + DK));
156  // SCALE SPECIFIC VOL. ANAMOLY TO NORMALLY REPORTED UNITS
157  //SVAN=SVA*1.0E+8; // Volume anomaly
158  V350P = V350P * PK;
159  // ****************************************************
160  // COMPUTE DENSITY ANAMOLY WITH RESPECT TO 1000.0 KG/M**3
161  // 1) DR350: DENSITY ANAMOLY AT 35 (IPSS-78), 0 DEG. C AND 0 DECIBARS
162  // 2) DR35P: DENSITY ANAMOLY 35 (IPSS-78), 0 DEG. C , PRES. VARIATION
163  // 3) DVAN : DENSITY ANAMOLY VARIATIONS INVOLVING SPECFIC VOL. ANAMOLY
164  // ********************************************************************
165  // CHECK VALUE: SIGMA = 59.82037 KG/M**3 FOR S = 40 (IPSS-78),
166  // T = 40 DEG C, P0= 10000 DECIBARS.
167  // *******************************************************
168  double DR35P = GAM / V350P;
169  double DVAN = SVA / (V350P * (V350P + SVA));
170  SIGMA = DR350 + DR35P - DVAN; // Density anomaly
171 
172  return SIGMA;
173 }
174 
175 #endif
Definition: test2.pb.h:139
STL namespace.
Definition: test_a.pb.h:42