- Joined
- May 13, 2017
- Messages
- 16
- Reaction score
- 13
Hi Everyone,
Small background of myself so you can understand why I am asking for help.
I am electronic technician by trade. Over the years I have worked with robotics and vacuum control chamber for semiconductor field.
Presently I am supervisor of a WWTP, we treat around 6 mgd.
I have reef tank with around 240 gallons of total volume.
My system is heavy automated with Siemens PLC. I have single axis valves, VFD pumps and various controllers (pH,Temperature, Conductivity) .
Now my question. I would like to convert conductivity (ms/cm) to ppt and specific gravity.
I would like to display those values on my HMI screens. I can handle the PLC side, if I had working formulas.
My goal is making something like this that works in the background.
I went to the reference pages that website uses, but the math is not adding up and I cannot get specific gravity to work.
My friend and I modify that program from that page to test the math, but ppt is not calculating like the web page does.
Can anyone here double check the math, I am missing something and this over anything I have done.
Thank you so much if anyone can help.
This code will work in Microsoft Visual Studio.
Small background of myself so you can understand why I am asking for help.
I am electronic technician by trade. Over the years I have worked with robotics and vacuum control chamber for semiconductor field.
Presently I am supervisor of a WWTP, we treat around 6 mgd.
I have reef tank with around 240 gallons of total volume.
My system is heavy automated with Siemens PLC. I have single axis valves, VFD pumps and various controllers (pH,Temperature, Conductivity) .
Now my question. I would like to convert conductivity (ms/cm) to ppt and specific gravity.
I would like to display those values on my HMI screens. I can handle the PLC side, if I had working formulas.
My goal is making something like this that works in the background.
HTML:
https://reefapp.net/en/maintenance/calculator/unitconversion
I went to the reference pages that website uses, but the math is not adding up and I cannot get specific gravity to work.
My friend and I modify that program from that page to test the math, but ppt is not calculating like the web page does.
HTML:
http://www.code10.info/index.php%3Foption%3Dcom_content%26view%3Darticle%26id%3D65:conversion-between-conductivity-and-pss-78-salinity%26catid%3D54:cat_coding_algorithms_seawater%26Itemid%3D79
Can anyone here double check the math, I am missing something and this over anything I have done.
Thank you so much if anyone can help.
This code will work in Microsoft Visual Studio.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LuisProject
{
class Program
{
static void Main(string[] args)
{
double sal;
Luis luisclass= new Luis();
bool exit = false;
while (!exit)
{
double salinity;
double conductivity, temperature;
Console.Write("Please enter cond in ms/sec-1 : ");
conductivity = Convert.ToDouble(Console.ReadLine());
Console.Write("Please enter temperature in C : ");
temperature = Convert.ToDouble(Console.ReadLine());
salinity = luisclass.Sal78(conductivity / 42.914, temperature, 10000, 0);
Console.WriteLine("Salinity = " + salinity);
Console.WriteLine("");
Console.Write("Do you want to exit Y/N? ");
string response = Console.ReadLine();
if ((response== "Y")|| (response=="y"))
{ exit = true; }
}
}
}
class Luis
{
public double Sal78(double CND, double T, double P, int Ma)
{
// THE CONDUCTIVITY RATIO (CND)=1.0000000 FOR SALINITY=35 PSS-78
// TEMPERATURE=15.0 DEG. CELSIUS AND ATMOSPHERIC PRESSURE.
// FUNCTION TO CONVERT CONDUCTIVITY RATIO TO SALINITY (M=0)
// SALINITY TO CONDUCTIVITY RATIO (M=1, CND BECOMES INPUT SALINITY)
// REFERENCES: ALSO LOCATED IN UNESCO REPORT NO. 37 1981
// PRACTICAL SALINITY SCALE 1978: E.L. LEWIS IEEE OCEAN ENG. JAN. 1980
// ----------------------------------------------------------
// UNITS:
// PRESSURE P DECIBARS
// TEMPERATURE T DEG CELSIUS IPTS-68
// CONDUCTIVITY CND RATIO (M=0)
// SALINITY SAL78 PSS-78 (M=0)
// ----------------------------------------------------------
// CHECKVALUES:
// 1.) SAL78=1.888091 for CND=40.0000, T=40 DEG C, P=10000 DECIBARS: M=1
// 2.) SAL78=40.00000 for CND=1.888091, T=40 DEG C, P=10000 DECIBARS: M=0
// ----------------------------------------------------------
// SAL78 RATIO: RETURNS ZERO FOR CONDUCTIVITY RATIO: < 0.0005
// SAL78: RETURNS ZERO FOR SALINITY: < 0.02
// ----------------------------------------------------------
// Original fortran code is found in:
// UNESCO technical papers in marine science 44 (1983) -
// 'Algorithms for computation of fundamental properties of seawater'
// ----------------------------------------------------------
// Translated to object pascal by:
// Dr. Jan Schulz, 19. May 2008, www.code10.info
double DT;
double Res;
double RT;
double SI;
int N;
double DELS;
double RTT;
double AT;
double BT;
double CP;
// ZERO SALINITY/CONDUCTIVITY TRAP
if (((Ma == 0) && (CND <= 5e-4)) || ((Ma == 1) && (CND <= 0.2)))
{ return 0; }
DT = T - 15;
// SELECT BRANCH FOR SALINITY (M=0) OR CONDUCTIVITY (M=1)
if (Ma == 0) {
// CONVERT CONDUCTIVITY TO SALINITY
Res = CND;
RT = Res / (RT35(T) * (1.0 + C(P) / (B(T) + A(T) * Res)));
RT = Math.Sqrt(Math.Abs(RT));
return SAL(RT, DT);
}
if (Ma == 1)
{
// INVERT SALINITY TO CONDUCTIVITY BY THE
// NEWTON-RAPHSON ITERATIVE METHOD
// FIRST APPROXIMATION
RT = Math.Sqrt(CND / 35);
SI = SAL(RT, DT);
N = 0;
DELS = 1;
// ITERATION LOOP BEGINS HERE WITH A MAXIMUM OF 10 CYCLES
while ((N < 10) && DELS > 1e-4)
{
RT = RT + (CND - SI) / DSAL(RT, DT);
SI = SAL(RT, DT);
N = N + 1;
DELS = Math.Abs(SI - CND);
}
//IF((DELS.GT.1.0E-4).AND.(N.LT.10)) GO TO 15
//Until not ((DELS > 1.0E-4) AND (N <10));
//COMPUTE CONDUCTIVITY RATIO
RTT = RT35(T) * RT * RT;
AT = A(T);
BT = B(T);
CP = C(P);
CP = RTT * (CP + BT);
BT = BT - RTT * AT;
// SOLVE QUADRATIC EQUATION FOR R: R=RT35*RT*(1+C/AR+B)
// R := SQRT (ABS (BT * BT + 4.0 * AT * CP)) - BT;
Res = Math.Sqrt(Math.Abs(BT * BT + 4 * AT * CP)) - BT;
// CONDUCTIVITY RETURN
return 0.5 * Res / AT;
}
return 0;
}
double SAL(double XR, double XT)
// PRACTICAL SALINITY SCALE 1978 DEFINITION WITH TEMPERATURE
// CORRECTION;XT :=T-15.0; XR:=SQRT(RT);
{
double tempSAL;
tempSAL = ((((2.7081 * XR - 7.0261) * XR + 14.0941) * XR + 25.3851) * XR
- 0.1692) * XR + 0.0080
+ (XT / (1.0 + 0.0162 * XT)) * (((((-0.0144 * XR
+ 0.0636) * XR - 0.0375) * XR - 0.0066) * XR - 0.0056) * XR + 0.0005);
return tempSAL;
}
double DSAL(double XR, double XT)
// FUNCTION FOR DERIVATIVE OF SAL(XR,XT) WITH XR
{
double tempDSAL;
tempDSAL = ((((13.5405 * XR - 28.1044) * XR + 42.2823) * XR + 50.7702) * XR
- 0.1692) + (XT / (1.0 + 0.0162 * XT)) * ((((-0.0720 * XR + 0.2544)
* XR - 0.1125) * XR - 0.0132) * XR - 0.0056);
return tempDSAL;
}
double RT35(double XT)
// FUNCTION RT35: C(35,T,0)/C(35,15,0) VARIATION WITH TEMPERATURE
{
double tempRT35;
tempRT35 = (((1.0031E-9 * XT - 6.9698E-7) * XT + 1.104259E-4) * XT
+ 2.00564E-2) * XT + 0.6766097;
return tempRT35;
}
double C(double XP)
// C(XP) POLYNOMIAL CORRESPONDS TO A1-A3 CONSTANTS: LEWIS 1980
{
return ((3.989E-15 * XP - 6.370E-10) * XP + 2.070E-5) * XP;
}
double B(double XT)
{
return (4.464E-4 * XT + 3.426E-2) * XT + 1.0;
}
double A(double XT)
//A(XT) POLYNOMIAL CORRESPONDS TO B3 AND B4 CONSTANTS: LEWIS 1980
{
return -3.107E-3 * XT + 0.4215;
}
}
//The function Cond2Sal78 converts conductivity to salinity.Required input parameters are aConductivity in S* m-1, Temp in °C and Press in decibars.In contrast to the function Sal78 it returns the salinity value as a parameter, while the return value of the function is a boolean.The function returns TRUE, if the passed conductivity value is >0.2 and the result of the conversion is between 2 and 40 PSS-78. In other cases FALSE is returned, indicating that the result might be unreliable (see above: Equation).
class Luis2
{
public bool Cond2Sal78(double aConductivity, double Temp, double Press, out double aSalinity)
//// Function Cond2Sal converts a conductivity value of seawater to a value
//// of the pratical-salinity-scale 1978 (PSS-78) for given values of
//// conductivity, temperature and pressure. Result is returned as
//// parameter in aSalinity. A returned boolean result TRUE of the
//// function indicates that the result is reliable.
//// UNITS:
//// PRESSURE Press DECIBARS
//// TEMPERATURE Temp DEG CELSIUS IPTS-68
//// CONDUCTIVITY aConductivity S/m
//// SALINITY aSalinity PSS-78
//// ----------------------------------------------------------
//// CHECKVALUES:
//// 2.) aSalinity=40.00000 for CND=1.888091, T=40 DEG C, P=10000 DECIBARS
//// ----------------------------------------------------------
//// SAL78 RATIO: RETURNS ZERO FOR CONDUCTIVITY RATIO: < 0.0005
//// ----------------------------------------------------------
//// This source code is based on the original fortran code in:
//// UNESCO technical papers in marine science 44 (1983) -
//// 'Algorithms for computation of fundamental properties of seawater'
//// ----------------------------------------------------------
//// Written in object pascal by:
//// Dr. Jan Schulz, 26. May 2008, www.code10.info
{
double DT;
double RT;
// // we expect the best
// Cond2Sal78 := True;
aSalinity = 0;
// equation is not defined for conductivity values below 5e-4
if (aConductivity <= 0.2)
{
return false;
}
// start conversion
DT = Temp - 15;
aSalinity = aConductivity / 4.2914;
RT = aSalinity / (RT35(Temp) * (1.0 + C(Press) / (B(Temp) + A(Temp) * aSalinity)));
RT = Math.Sqrt(Math.Abs(RT));
aSalinity= SAL(RT, DT);
// control, whether result is in the validity range of PSS-78
if ((aSalinity < 2) || (aSalinity > 42))
{
return false;
}
return true;
}
double SAL(double XR, double XT)
// PRACTICAL SALINITY SCALE 1978 DEFINITION WITH TEMPERATURE
// CORRECTION;XT :=T-15.0; XR:=SQRT(RT);
{
double tempSAL;
tempSAL = ((((2.7081 * XR - 7.0261) * XR + 14.0941) * XR + 25.3851) * XR
- 0.1692) * XR + 0.0080
+ (XT / (1.0 + 0.0162 * XT)) * (((((-0.0144 * XR
+ 0.0636) * XR - 0.0375) * XR - 0.0066) * XR - 0.0056) * XR + 0.0005);
return tempSAL;
}
double RT35(double XT)
// FUNCTION RT35: C(35,T,0)/C(35,15,0) VARIATION WITH TEMPERATURE
{
double tempRT35;
tempRT35 = (((1.0031E-9 * XT - 6.9698E-7) * XT + 1.104259E-4) * XT
+ 2.00564E-2) * XT + 0.6766097;
return tempRT35;
}
double C(double XP)
// C(XP) POLYNOMIAL CORRESPONDS TO A1-A3 CONSTANTS: LEWIS 1980
{
return ((3.989E-15 * XP - 6.370E-10) * XP + 2.070E-5) * XP;
}
double B(double XT)
{
return (4.464E-4 * XT + 3.426E-2) * XT + 1.0;
}
double A(double XT)
//A(XT) POLYNOMIAL CORRESPONDS TO B3 AND B4 CONSTANTS: LEWIS 1980
{
return -3.107E-3 * XT + 0.4215;
}
}
}