C# Wrapper

NuGet packages (3-party wrappers)

SharpFluids

This C# NuGet package uses CoolProp to perform all Fluid Properties lookups. It combines the speed from the low-level lookup with a units of measurement system packed into a easy-to-use system. If you are new to using CoolProp, this is a good place to start.

How to start

  • Create a new C# Console App(.NET Framework) project in Visual studio

  • Right click your new project and press ‘Manage NuGet Packages’

  • Go to ‘Browse’ and search for ‘SharpFluids’ and press ‘Install’

  • Add this to the top of your code

    using SharpFluids;
    using UnitsNet;
    
  • Add this example code in your main

    Fluid Water = new Fluid(FluidList.Water);
    Water.UpdatePT(Pressure.FromBars(1.013), Temperature.FromDegreesCelsius(13));
    Console.WriteLine("Density of water at 13°C: " + Water.RHO);
    Console.ReadLine();
    
  • Press ‘F5’ or ‘Start’ to check that it is working

  • If you have problems or questions, Find SharpFluids at Github.

SharpProp

It is a simple, full-featured, lightweight, cross-platform CoolProp wrapper for C#. SharpProp gets published on NuGet.

All CoolProp features are included: thermophysical properties of pure fluids, mixtures and humid air.

Calculations of thermophysical properties are unit safe (thanks to UnitsNet). This allows you to avoid errors associated with incorrect dimensions of quantities, and will help you save a lot of time on their search and elimination. In addition, you will be able to convert all values to many other dimensions without the slightest difficulty.

Also you can easily convert the results to a JSON string, add new properties or inputs for lookups, and more.

Examples

To calculate the specific heat of saturated water vapor at 1 atm:

using System;
using SharpProp;
using UnitsNet.NumberExtensions.NumberToPressure;
using UnitsNet.Units;
var waterVapour = new Fluid(FluidsList.Water)
    .DewPointAt((1).Atmospheres());
Console.WriteLine(waterVapour.SpecificHeat.JoulesPerKilogramKelvin); // 2079.937085633241
Console.WriteLine(waterVapour.SpecificHeat);                         // 2.08 kJ/kg·K
Console.WriteLine(waterVapour.SpecificHeat
    .ToUnit(SpecificEntropyUnit.CaloriePerGramKelvin));              // 0.5 cal/g·K

To calculate the dynamic viscosity of propylene glycol aqueous solution with 60 % mass fraction at 100 kPa and -20 °C:

using System;
using SharpProp;
using UnitsNet.NumberExtensions.NumberToPressure;
using UnitsNet.NumberExtensions.NumberToRatio;
using UnitsNet.NumberExtensions.NumberToTemperature;
using UnitsNet.Units;
var propyleneGlycol = new Fluid(FluidsList.MPG, (60).Percent())
    .WithState(Input.Pressure((100).Kilopascals()),
        Input.Temperature((-20).DegreesCelsius()));
Console.WriteLine(propyleneGlycol.DynamicViscosity?.PascalSeconds); // 0.13907391053938878
Console.WriteLine(propyleneGlycol.DynamicViscosity);                // 139.07 mPa·s
Console.WriteLine(propyleneGlycol.DynamicViscosity?
    .ToUnit(DynamicViscosityUnit.Poise));                           // 1.39 P

To calculate the density of ethanol aqueous solution (with ethanol 40 % mass fraction) at 200 kPa and 277.15 K:

using System;
using System.Collections.Generic;
using SharpProp;
using UnitsNet;
using UnitsNet.NumberExtensions.NumberToPressure;
using UnitsNet.NumberExtensions.NumberToRatio;
using UnitsNet.NumberExtensions.NumberToTemperature;
using UnitsNet.Units;
var mixture = new Mixture(
    new List<FluidsList> {FluidsList.Water, FluidsList.Ethanol},
    new List<Ratio> {(60).Percent(), (40).Percent()})
    .WithState(Input.Pressure((200).Kilopascals()),
        Input.Temperature((277.15).Kelvins()));
Console.WriteLine(mixture.Density.KilogramsPerCubicMeter);               // 883.3922771627759
Console.WriteLine(mixture.Density);                                      // 883.39 kg/m3
Console.WriteLine(mixture.Density.ToUnit(DensityUnit.GramPerDeciliter)); // 88.34 g/dl

To calculate the wet bulb temperature of humid air at 99 kPa, 30 °C and 50 % relative humidity:

using System;
using SharpProp;
using UnitsNet.NumberExtensions.NumberToPressure;
using UnitsNet.NumberExtensions.NumberToRelativeHumidity;
using UnitsNet.NumberExtensions.NumberToTemperature;
using UnitsNet.Units;
var humidAir = new HumidAir().WithState(
    InputHumidAir.Pressure((99).Kilopascals()),
    InputHumidAir.Temperature((30).DegreesCelsius()),
    InputHumidAir.RelativeHumidity((50).Percent()));
Console.WriteLine(humidAir.WetBulbTemperature.Kelvins); // 295.0965785590792
Console.WriteLine(humidAir.WetBulbTemperature);         // 21.95 °C
Console.WriteLine(humidAir.WetBulbTemperature
    .ToUnit(TemperatureUnit.DegreeFahrenheit));         // 71.5 °F

For any questions or more examples, see SharpProp on GitHub.

Pre-compiled Binaries

To Use

Pre-compiled binaries can be downloaded from Csharp. Development binaries coming from the buildbot server can be found at Csharp.

Download the platform-independent.7z file and expand it to a folder called platform-independent using 7-zip. Download the special C# shared library for your system architecture to the same location from either Csharp (release) or Csharp (development). Copy the Example.cs file to the same location. You will need to have a copy of some version of C#.

When you are finished, you should have a folder layout something like

main
 |- CoolProp.dll
 |- Example.cs
 |- platform-independent
    |- AbstractState.cs
    |- Configuration.cs
    |- ...

There is example code at the end of this page

Windows

At the command prompt, run:

call "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"
csc Example.cs platform-independent/*.cs -platform:x64
Example

where you might need to update the path to visual studio depending on your version installed. Use -platform:x86 to tell C# that your DLL is 32-bit if you are on 32-bit, or -platform:x64 if you are on 64-bit.

Alternatively, you can add all the .cs files to a visual studio project. If you do that, add the DLL to the project as well, right-click on the DLL, and select the option to copy it to the output directory.

Linux/OSX

Same idea as windows, but command line is just a bit different:

mcs Example.cs platform-independent/*.cs -platform:x64
./Example

Use -platform:x86 to tell C# that your shared library is 32-bit if you are on 32-bit, or -platform:x64 if you are on a 64-bit platform.

User-Compiled Binaries

Common Requirements

Compilation of the C# wrapper requires a few common wrapper pre-requisites

Additionally, you will need: * SWIG (see common wrapper pre-requisites) * C#

OSX

For OSX, to install the necessary tools using homebrew, you can do:

homebrew install mono

Linux

For ubuntu and friends, you will need to install Mono C# as well as the compiler (and other dependencies) using:

sudo apt-get install swig mono-mcs mono-runtime

Windows

For Windows, download the Visual Studio 2010 version of C# (other versions should probably be fine too)

Compile

Once mono c# is installed, you can run the builder and tests using:

# Check out the sources for CoolProp
git clone https://github.com/CoolProp/CoolProp --recursive
# Move into the folder you just created
cd CoolProp
# Make a build folder
mkdir build && cd build
# Build the makefile using CMake
cmake .. -DCOOLPROP_CSHARP_MODULE=ON -DBUILD_TESTING=ON
# Make the C# files (by default files will be generated in folder install_root/Csharp relative to CMakeLists.txt file)
cmake --build .
# Run the integration tests (optional)
ctest --extra-verbose

If you want to change the package that CoolProp resides in, you can do so by changing the cmake call to read:

cmake .. -DCOOLPROP_CSHARP_MODULE=ON -DBUILD_TESTING=ON -DCOOLPROP_SWIG_OPTIONS="-namespace package.name"

where package.name is replaced with the desired name

Example Code

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("**************** INFORMATION ***************" + "\n");
            Console.Write("This example was auto-generated by the language-agnostic dev/scripts/example_generator.py script written by Ian Bell" + "\n");
            Console.Write("CoolProp version:" + " " + CoolProp.get_global_param_string("version") + "\n");
            Console.Write("CoolProp gitrevision:" + " " + CoolProp.get_global_param_string("gitrevision") + "\n");
            Console.Write("CoolProp Fluids:" + " " + CoolProp.get_global_param_string("FluidsList") + "\n");
            // See http://www.coolprop.org/coolprop/HighLevelAPI.html#table-of-string-inputs-to-propssi-function for a list of inputs to high-level interface;
            Console.Write("*********** HIGH LEVEL INTERFACE *****************" + "\n");
            Console.Write("Critical temperature of water:" + " " + CoolProp.Props1SI("Water", "Tcrit") + " " + "K" + "\n");
            Console.Write("Boiling temperature of water at 101325 Pa:" + " " + CoolProp.PropsSI("T", "P", 101325, "Q", 0, "Water") + " " + "K" + "\n");
            Console.Write("Phase of water at 101325 Pa and 300 K:" + " " + CoolProp.PhaseSI("P", 101325, "T", 300, "Water") + "\n");
            Console.Write("c_p of water at 101325 Pa and 300 K:" + " " + CoolProp.PropsSI("C", "P", 101325, "T", 300, "Water") + " " + "J/kg/K" + "\n");
            Console.Write("c_p of water (using derivatives) at 101325 Pa and 300 K:" + " " + CoolProp.PropsSI("d(H)/d(T)|P", "P", 101325, "T", 300, "Water") + " " + "J/kg/K" + "\n");
            Console.Write("*********** HUMID AIR PROPERTIES *****************" + "\n");
            Console.Write("Humidity ratio of 50% rel. hum. air at 300 K, 101325 Pa:" + " " + CoolProp.HAPropsSI("W", "T", 300, "P", 101325, "R", 0.5) + " " + "kg_w/kg_da" + "\n");
            Console.Write("Relative humidity from last calculation:" + " " + CoolProp.HAPropsSI("R", "T", 300, "P", 101325, "W", CoolProp.HAPropsSI("W", "T", 300, "P", 101325, "R", 0.5)) + " " + "(fractional)" + "\n");
            Console.Write("*********** INCOMPRESSIBLE FLUID AND BRINES *****************" + "\n");
            Console.Write("Density of 50% (mass) ethylene glycol/water at 300 K, 101325 Pa:" + " " + CoolProp.PropsSI("D", "T", 300, "P", 101325, "INCOMP::MEG-50%") + " " + "kg/m^3" + "\n");
            Console.Write("Viscosity of Therminol D12 at 350 K, 101325 Pa:" + " " + CoolProp.PropsSI("V", "T", 350, "P", 101325, "INCOMP::TD12") + " " + "Pa-s" + "\n");
            // If you don't have REFPROP installed, disable the following lines;
            Console.Write("*********** REFPROP *****************" + "\n");
            Console.Write("REFPROP version:" + " " + CoolProp.get_global_param_string("REFPROP_version") + "\n");
            Console.Write("Critical temperature of water:" + " " + CoolProp.Props1SI("REFPROP::WATER", "Tcrit") + " " + "K" + "\n");
            Console.Write("Boiling temperature of water at 101325 Pa:" + " " + CoolProp.PropsSI("T", "P", 101325, "Q", 0, "REFPROP::WATER") + " " + "K" + "\n");
            Console.Write("c_p of water at 101325 Pa and 300 K:" + " " + CoolProp.PropsSI("C", "P", 101325, "T", 300, "REFPROP::WATER") + " " + "J/kg/K" + "\n");
            Console.Write("*********** TABULAR BACKENDS *****************" + "\n");
            AbstractState TAB = AbstractState.factory("BICUBIC&HEOS", "R245fa");
            TAB.update(input_pairs.PT_INPUTS, 101325, 300);
            Console.Write("Mass density of refrigerant R245fa at 300 K, 101325 Pa:" + " " + TAB.rhomass() + " " + "kg/m^3" + "\n");
            Console.Write("*********** SATURATION DERIVATIVES (LOW-LEVEL INTERFACE) ***************" + "\n");
            AbstractState AS_SAT = AbstractState.factory("HEOS", "R245fa");
            AS_SAT.update(input_pairs.PQ_INPUTS, 101325, 0);
            Console.Write("First saturation derivative:" + " " + AS_SAT.first_saturation_deriv(parameters.iP, parameters.iT) + " " + "Pa/K" + "\n");
            Console.Write("*********** LOW-LEVEL INTERFACE *****************" + "\n");
            AbstractState AS = AbstractState.factory("HEOS", "Water&Ethanol");
            DoubleVector z = new DoubleVector(new double[]{0.5, 0.5});
            AS.set_mole_fractions(z);
            AS.update(input_pairs.PQ_INPUTS, 101325, 1);
            Console.Write("Normal boiling point temperature of water and ethanol:" + " " + AS.T() + " " + "K" + "\n");
            // If you don't have REFPROP installed, disable the following block;
            Console.Write("*********** LOW-LEVEL INTERFACE (REFPROP) *****************" + "\n");
            AbstractState AS2 = AbstractState.factory("REFPROP", "METHANE&ETHANE");
            DoubleVector z2 = new DoubleVector(new double[]{0.2, 0.8});
            AS2.set_mole_fractions(z2);
            AS2.update(input_pairs.QT_INPUTS, 1, 120);
            Console.Write("Vapor molar density:" + " " + AS2.keyed_output(parameters.iDmolar) + " " + "mol/m^3" + "\n");
        }
    }
}

Example Code Output

**************** INFORMATION ***************
This example was auto-generated by the language-agnostic dev/scripts/example_generator.py script written by Ian Bell
CoolProp version: 6.6.0
CoolProp gitrevision: ba21c74fd1c3a4934bbecbf459936983dec424c2
CoolProp Fluids: n-Nonane,MethylLinolenate,DimethylCarbonate,R21,DiethylEther,trans-2-Butene,R245fa,ParaDeuterium,OrthoDeuterium,Isohexane,R365MFC,n-Dodecane,R410A,Deuterium,D4,R13,MD2M,n-Hexane,Methane,Ethane,CarbonylSulfide,EthylBenzene,CarbonMonoxide,Isopentane,Xenon,cis-2-Butene,R152A,Oxygen,EthyleneOxide,R1234ze(E),n-Octane,R404A,R236EA,CycloHexane,n-Heptane,R22,R113,n-Pentane,MethylLinoleate,R11,SulfurDioxide,R23,Helium,R32,R227EA,R407C,HydrogenSulfide,Air,R245ca,Novec649,R143a,D5,R507A,R134a,Dichloroethane,ParaHydrogen,R1233zd(E),Acetone,n-Decane,HeavyWater,MethylPalmitate,n-Propane,R115,R1234yf,R236FA,Ethylene,R116,MD4M,Benzene,Methanol,SulfurHexafluoride,o-Xylene,R125,Fluorine,R1234ze(Z),CarbonDioxide,IsoButane,n-Butane,NitrousOxide,DimethylEther,RC318,Toluene,IsoButene,MethylStearate,Ammonia,Argon,R218,R41,Neon,Propyne,CycloPropane,R12,Nitrogen,Water,MethylOleate,R161,D6,SES36,HFE143m,n-Undecane,R123,HydrogenChloride,m-Xylene,R141b,R124,1-Butene,Propylene,R14,p-Xylene,Cyclopentane,MDM,Hydrogen,Neopentane,Ethanol,OrthoHydrogen,R114,Krypton,MD3M,R1243zf,MM,R142b,R40,R13I1
*********** HIGH LEVEL INTERFACE *****************
Critical temperature of water: 647.096 K
Boiling temperature of water at 101325 Pa: 373.124295847684 K
Phase of water at 101325 Pa and 300 K: liquid
c_p of water at 101325 Pa and 300 K: 4180.63577655607 J/kg/K
c_p of water (using derivatives) at 101325 Pa and 300 K: 4180.63577655607 J/kg/K
*********** HUMID AIR PROPERTIES *****************
Humidity ratio of 50% rel. hum. air at 300 K, 101325 Pa: 0.0110955297051998 kg_w/kg_da
Relative humidity from last calculation: 0.5 (fractional)
*********** INCOMPRESSIBLE FLUID AND BRINES *****************
Density of 50% (mass) ethylene glycol/water at 300 K, 101325 Pa: 1061.17930772046 kg/m^3
Viscosity of Therminol D12 at 350 K, 101325 Pa: 0.000522883799095536 Pa-s
*********** REFPROP *****************
REFPROP version: 10.0
Critical temperature of water: 647.096 K
Boiling temperature of water at 101325 Pa: 373.124295847701 K
c_p of water at 101325 Pa and 300 K: 4180.63577657559 J/kg/K
*********** TABULAR BACKENDS *****************
Mass density of refrigerant R245fa at 300 K, 101325 Pa: 5.64812825704638 kg/m^3
*********** SATURATION DERIVATIVES (LOW-LEVEL INTERFACE) ***************
First saturation derivative: 4058.51975505072 Pa/K
*********** LOW-LEVEL INTERFACE *****************
Normal boiling point temperature of water and ethanol: 357.272980171265 K
*********** LOW-LEVEL INTERFACE (REFPROP) *****************
Vapor molar density: 0.441465626653878 mol/m^3