From 569ebc43ac2f0e014769d12d3311cbc93043f305 Mon Sep 17 00:00:00 2001 From: Ian Bell Date: Mon, 18 Aug 2014 22:00:25 +0200 Subject: [PATCH] First derivatives are calculated by gridded tabular backend Signed-off-by: Ian Bell --- src/Backends/Tabular/TabularBackends.cpp | 63 +++++++++++++++--------- src/Backends/Tabular/TabularBackends.h | 15 +++++- 2 files changed, 55 insertions(+), 23 deletions(-) diff --git a/src/Backends/Tabular/TabularBackends.cpp b/src/Backends/Tabular/TabularBackends.cpp index 2d8bf1d3..d76a05f8 100644 --- a/src/Backends/Tabular/TabularBackends.cpp +++ b/src/Backends/Tabular/TabularBackends.cpp @@ -77,37 +77,56 @@ void GriddedTableBackend::build_tables(tabular_types type) y = ymin + (ymax - ymin)/(Ny-1)*j; } - if (get_debug_level() > 5){std::cout << "x:" << x << " y: " << y;} - if (xkey == iHmolar && ykey == iP){ + if (get_debug_level() > 5){std::cout << "x: " << x << " y: " << y;} + + if (xkey == iHmolar && ykey == iP) + { + // -------------------- + // Update the state + // -------------------- 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 + // That failed for some reason, go to the next pair + if (get_debug_level() > 5){std::cout << " " << e.what() << std::endl;} continue; } + + // 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; + }; + + // -------------------- + // State variables + // -------------------- + single_phase.T[i][j] = AS->T(); + single_phase.p[i][j] = AS->p(); + single_phase.rhomolar[i][j] = AS->rhomolar(); + single_phase.hmolar[i][j] = AS->hmolar(); + single_phase.smolar[i][j] = AS->smolar(); + + // ---------------------------------------- + // First derivatives of state variables + // ---------------------------------------- + single_phase.dTdx[i][j] = AS->first_partial_deriv(iT, xkey, ykey); + single_phase.dTdy[i][j] = AS->first_partial_deriv(iT, ykey, xkey); + single_phase.dpdx[i][j] = AS->first_partial_deriv(iP, xkey, ykey); + single_phase.dpdy[i][j] = AS->first_partial_deriv(iP, ykey, xkey); + single_phase.drhomolardx[i][j] = AS->first_partial_deriv(iDmolar, xkey, ykey); + single_phase.drhomolardy[i][j] = AS->first_partial_deriv(iDmolar, ykey, xkey); + single_phase.dhmolardx[i][j] = AS->first_partial_deriv(iHmolar, xkey, ykey); + single_phase.dhmolardy[i][j] = AS->first_partial_deriv(iHmolar, ykey, xkey); + single_phase.dsmolardx[i][j] = AS->first_partial_deriv(iSmolar, xkey, ykey); + single_phase.dsmolardy[i][j] = AS->first_partial_deriv(iSmolar, ykey, xkey); + + if (get_debug_level() > 5){std::cout << " OK" << std::endl;} } else{ - throw ValueError("Cannot construct this type of table"); + throw ValueError("Cannot construct this type of table (yet)"); } - - // Calculate the derivatives } } } diff --git a/src/Backends/Tabular/TabularBackends.h b/src/Backends/Tabular/TabularBackends.h index b3025cb9..db437526 100644 --- a/src/Backends/Tabular/TabularBackends.h +++ b/src/Backends/Tabular/TabularBackends.h @@ -8,14 +8,27 @@ namespace CoolProp{ struct SinglePhaseGriddedTableData{ CoolProp::parameters xkey, ykey; std::vector< std::vector > T, p, rhomolar, hmolar, smolar; + std::vector< std::vector > dTdx, dTdy, dpdx, dpdy, drhomolardx, drhomolardy, dhmolardx, dhmolardy, dsmolardx, dsmolardy; void resize(std::size_t Nx, std::size_t Ny){ - + // State variables T.resize(Nx, std::vector(Ny, _HUGE)); p.resize(Nx, std::vector(Ny, _HUGE)); rhomolar.resize(Nx, std::vector(Ny, _HUGE)); hmolar.resize(Nx, std::vector(Ny, _HUGE)); smolar.resize(Nx, std::vector(Ny, _HUGE)); + + // First derivatives of state variables + dTdx.resize(Nx, std::vector(Ny, _HUGE)); + dpdx.resize(Nx, std::vector(Ny, _HUGE)); + drhomolardx.resize(Nx, std::vector(Ny, _HUGE)); + dhmolardx.resize(Nx, std::vector(Ny, _HUGE)); + dsmolardx.resize(Nx, std::vector(Ny, _HUGE)); + dTdy.resize(Nx, std::vector(Ny, _HUGE)); + dpdy.resize(Nx, std::vector(Ny, _HUGE)); + drhomolardy.resize(Nx, std::vector(Ny, _HUGE)); + dhmolardy.resize(Nx, std::vector(Ny, _HUGE)); + dsmolardy.resize(Nx, std::vector(Ny, _HUGE)); }; }; class GriddedTableBackend : public AbstractState