Gridded table builds

Signed-off-by: Ian Bell <ian.h.bell@gmail.com>
This commit is contained in:
Ian Bell
2014-08-18 19:28:54 +02:00
parent ad15288c23
commit e3fba36d81
2 changed files with 75 additions and 16 deletions

View File

@@ -1,18 +1,25 @@
#include "TabularBackends.h"
#include "CoolProp.h"
namespace CoolProp{
void GriddedTableBackend::build_tables(tabular_types type)
{
std::size_t Nx = 200, Ny = 200;
std::size_t Nx = 20, Ny = 20;
long double xmin, xmax, ymin, ymax, x, y;
bool logy, logx;
parameters xkey, ykey;
single_phase.resize(Nx, Ny);
switch(type){
case LOGPH_TABLE:
{
parameters xkey, ykey;
xkey = iHmolar;
ykey = iP;
logy = true;
logx = false;
// ---------------------------------
// Calculate the limits of the table
@@ -21,20 +28,17 @@ void GriddedTableBackend::build_tables(tabular_types type)
// Minimum enthalpy is the saturated liquid enthalpy
AS->update(QT_INPUTS, 0, AS->Ttriple());
xmin = AS->hmolar();
ymin = log(AS->p());
ymin = AS->p();
// Check both the enthalpies at the Tmax isotherm to see whether to use low or high pressure
AS->update(PT_INPUTS, 1e-10, AS->Tmax());
long double xmax1 = AS->hmolar();
AS->update(PT_INPUTS, AS->pmax(), AS->Tmax());
long double xmax2 = AS->hmolar();
xmax = std::max(xmax1, xmax2);
xmax = std::min(xmax1, xmax2);
ymax = AS->pmax();
ymax = log(AS->p());
xkey = iHmolar;
ykey = iP;
logy = true;
logx = false;
break;
}
default:
@@ -42,28 +46,68 @@ void GriddedTableBackend::build_tables(tabular_types type)
throw ValueError(format(""));
}
}
if (get_debug_level() > 5){
std::cout << format("***********************************************\n");
std::cout << format(" Single-Phase Table (%s) \n", AS->name().c_str());
std::cout << format("***********************************************\n");
}
// ------------------------
// Actually build the table
// ------------------------
for (std::size_t i = 0; i < Nx; ++i)
{
// Calculate the x value
x = xmin + (xmax - xmin)/(Nx-1)*i;
if (logx){
x = log(x);
// Log spaced
x = exp(log(xmin) + (log(xmax) - log(xmin))/(Nx-1)*i);
}
else{
// Linearly spaced
x = xmin + (xmax - xmin)/(Nx-1)*i;
}
for (std::size_t j = 0; j < Ny; ++j)
{
// Calculate the y value
y = ymin + (ymax - ymin)/(Ny-1)*j;
// Calculate the x value
if (logy){
y = log(y);
// Log spaced
y = exp(log(ymin) + (log(ymax/ymin))/(Ny-1)*j);
}
else{
// Linearly spaced
y = ymin + (ymax - ymin)/(Ny-1)*j;
}
// Update the state
if (get_debug_level() > 5){std::cout << "x:" << x << " y: " << y;}
if (xkey == iHmolar && ykey == iP){
try{
// Update the state
AS->update(HmolarP_INPUTS, x, y);
// Skip two-phase states - they will remain as _HUGE holes in the table
if (AS->phase() == iphase_twophase){
if (get_debug_level() > 5){std::cout << "2Phase" << std::endl;}
continue;
};
// Calculate each of the parameters, and their derivatives with respect to the input variables
single_phase.T[i][j] = AS->T();
single_phase.p[i][j] = AS->p();
single_phase.hmolar[i][j] = AS->hmolar();
single_phase.smolar[i][j] = AS->smolar();
single_phase.rhomolar[i][j] = AS->rhomolar();
if (get_debug_level() > 5){std::cout << "OK" << std::endl;}
}
catch(std::exception &e){
if (get_debug_level() > 5){std::cout << e.what() << std::endl;}
// If there is an error, go to next one
continue;
}
}
else{
throw ValueError("Cannot construct this type of table");
}
// Calculate the derivatives
}
}
}

View File

@@ -5,12 +5,27 @@
namespace CoolProp{
struct SinglePhaseGriddedTableData{
CoolProp::parameters xkey, ykey;
std::vector< std::vector<double> > T, p, rhomolar, hmolar, smolar;
void resize(std::size_t Nx, std::size_t Ny){
T.resize(Nx, std::vector<double>(Ny, _HUGE));
p.resize(Nx, std::vector<double>(Ny, _HUGE));
rhomolar.resize(Nx, std::vector<double>(Ny, _HUGE));
hmolar.resize(Nx, std::vector<double>(Ny, _HUGE));
smolar.resize(Nx, std::vector<double>(Ny, _HUGE));
};
};
class GriddedTableBackend : public AbstractState
{
protected:
enum tabular_types {LOGPH_TABLE};
AbstractState *AS;
public:
SinglePhaseGriddedTableData single_phase;
bool using_mole_fractions(void){return true;}
bool using_mass_fractions(void){return false;}
bool using_volu_fractions(void){return false;}