From 171681bf1376a70da0e61da2161bddbbe1906f56 Mon Sep 17 00:00:00 2001 From: Victor Brunini Date: Mon, 3 Feb 2014 19:48:02 +0000 Subject: [PATCH] Cache result of expensive function call in HMWSoln for reuse. Went from 440s runtime down to 65s for test case of interest. --- include/cantera/thermo/HMWSoln.h | 3 +++ src/thermo/HMWSoln.cpp | 28 +++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/include/cantera/thermo/HMWSoln.h b/include/cantera/thermo/HMWSoln.h index d85f42880..df300e686 100644 --- a/include/cantera/thermo/HMWSoln.h +++ b/include/cantera/thermo/HMWSoln.h @@ -2402,6 +2402,9 @@ private: * = 1.0E3 at 25C */ mutable double m_A_Debye; + mutable double m_last_dA_DebyedP_TP; + mutable double m_last_dA_DebyedP_TP_T; + mutable double m_last_dA_DebyedP_TP_P; //! Water standard state calculator /*! diff --git a/src/thermo/HMWSoln.cpp b/src/thermo/HMWSoln.cpp index 918f90e29..10a826f4b 100644 --- a/src/thermo/HMWSoln.cpp +++ b/src/thermo/HMWSoln.cpp @@ -39,6 +39,9 @@ HMWSoln::HMWSoln() : m_IionicMolalityStoich(0.0), m_form_A_Debye(A_DEBYE_WATER), m_A_Debye(1.172576), // units = sqrt(kg/gmol) + m_last_dA_DebyedP_TP(-1.0), + m_last_dA_DebyedP_TP_T(-1.0), + m_last_dA_DebyedP_TP_P(-1.0), m_waterSS(0), m_densWaterSS(1000.), m_waterProps(0), @@ -89,6 +92,9 @@ HMWSoln::HMWSoln(const std::string& inputFile, const std::string& id_) : m_IionicMolalityStoich(0.0), m_form_A_Debye(A_DEBYE_WATER), m_A_Debye(1.172576), // units = sqrt(kg/gmol) + m_last_dA_DebyedP_TP(-1.0), + m_last_dA_DebyedP_TP_T(-1.0), + m_last_dA_DebyedP_TP_P(-1.0), m_waterSS(0), m_densWaterSS(1000.), m_waterProps(0), @@ -140,6 +146,9 @@ HMWSoln::HMWSoln(XML_Node& phaseRoot, const std::string& id_) : m_IionicMolalityStoich(0.0), m_form_A_Debye(A_DEBYE_WATER), m_A_Debye(1.172576), // units = sqrt(kg/gmol) + m_last_dA_DebyedP_TP(-1.0), + m_last_dA_DebyedP_TP_T(-1.0), + m_last_dA_DebyedP_TP_P(-1.0), m_waterSS(0), m_densWaterSS(1000.), m_waterProps(0), @@ -191,6 +200,9 @@ HMWSoln::HMWSoln(const HMWSoln& b) : m_IionicMolalityStoich(0.0), m_form_A_Debye(A_DEBYE_WATER), m_A_Debye(1.172576), // units = sqrt(kg/gmol) + m_last_dA_DebyedP_TP(-1.0), + m_last_dA_DebyedP_TP_T(-1.0), + m_last_dA_DebyedP_TP_P(-1.0), m_waterSS(0), m_densWaterSS(1000.), m_waterProps(0), @@ -401,6 +413,9 @@ HMWSoln::HMWSoln(int testProb) : m_IionicMolalityStoich(0.0), m_form_A_Debye(A_DEBYE_WATER), m_A_Debye(1.172576), // units = sqrt(kg/gmol) + m_last_dA_DebyedP_TP(-1.0), + m_last_dA_DebyedP_TP_T(-1.0), + m_last_dA_DebyedP_TP_P(-1.0), m_waterSS(0), m_densWaterSS(1000.), m_waterProps(0), @@ -1089,12 +1104,23 @@ double HMWSoln::dA_DebyedP_TP(double tempArg, double presArg) const P = presArg; } double dAdP; + const double tol = 1.e-6; switch (m_form_A_Debye) { case A_DEBYE_CONST: dAdP = 0.0; break; case A_DEBYE_WATER: - dAdP = m_waterProps->ADebye(T, P, 3); + if( std::abs(T - m_last_dA_DebyedP_TP_T) > tol || std::abs(P - m_last_dA_DebyedP_TP_P) > tol ) + { + dAdP = m_waterProps->ADebye(T, P, 3); + m_last_dA_DebyedP_TP_T = T; + m_last_dA_DebyedP_TP_P = P; + m_last_dA_DebyedP_TP = dAdP; + } + else + { + dAdP = m_last_dA_DebyedP_TP; + } break; default: printf("shouldn't be here\n");