ListComponents is now cached

git-svn-id: svn://136.177.114.72/svn_GW/IPhreeqc/trunk@4464 1feff8c3-07ed-0310-ac33-dd36852eb9cd
This commit is contained in:
Scott R Charlton 2010-05-25 00:47:02 +00:00
parent eaf070d56b
commit 1f6e1f03b0
5 changed files with 165 additions and 94 deletions

View File

@ -557,7 +557,8 @@ protected:
protected:
bool DatabaseLoaded;
bool ClearAccumulatedLinesOnNextAccumulate;
bool ClearAccumulated;
bool UpdateComponents;
bool SelectedOutputOn;
bool OutputOn;
bool LogOn;

View File

@ -37,7 +37,8 @@ int istream_getc(void *cookie)
IPhreeqc::IPhreeqc(void)
: DatabaseLoaded(false)
, ClearAccumulatedLinesOnNextAccumulate(false)
, ClearAccumulated(false)
, UpdateComponents(true)
, SelectedOutputOn(false)
, OutputOn(false)
, LogOn(false)
@ -66,10 +67,10 @@ VRESULT IPhreeqc::AccumulateLine(const char *line)
{
try
{
if (this->ClearAccumulatedLinesOnNextAccumulate)
if (this->ClearAccumulated)
{
this->ClearAccumulatedLines();
this->ClearAccumulatedLinesOnNextAccumulate = false;
this->ClearAccumulated = false;
}
this->ErrorReporter->Clear();
@ -108,7 +109,7 @@ const std::string& IPhreeqc::GetAccumulatedLines(void)
const char* IPhreeqc::GetComponent(int n)
{
static const char empty[] = "";
this->Components = this->ListComponents();
this->ListComponents();
if (n < 0 || n >= (int)this->Components.size())
{
return empty;
@ -123,9 +124,8 @@ const char* IPhreeqc::GetComponent(int n)
size_t IPhreeqc::GetComponentCount(void)
{
std::list< std::string > comps;
this->PhreeqcPtr->list_components(comps);
return comps.size();
this->ListComponents();
return this->Components.size();
}
bool IPhreeqc::GetDumpFileOn(void)const
@ -272,9 +272,13 @@ int IPhreeqc::GetWarningStringLineCount(void)const
std::list< std::string > IPhreeqc::ListComponents(void)
{
std::list< std::string > comps;
this->PhreeqcPtr->list_components(comps);
return comps;
if (this->UpdateComponents)
{
this->Components.clear();
this->PhreeqcPtr->list_components(this->Components);
this->UpdateComponents = false;
}
return this->Components;
}
int IPhreeqc::LoadDatabase(const char* filename)
@ -410,7 +414,7 @@ int IPhreeqc::RunAccumulated(void)
}
}
this->ClearAccumulatedLinesOnNextAccumulate = true;
this->ClearAccumulated = true;
this->PhreeqcPtr->close_output_files();
this->update_errors();
@ -541,6 +545,8 @@ void IPhreeqc::UnLoadDatabase(void)
// init IPhreeqc
//
this->DatabaseLoaded = false;
this->UpdateComponents = true;
this->Components.clear();
// clear error state
//
@ -1262,6 +1268,7 @@ void IPhreeqc::do_run(const char* sz_routine, std::istream* pis, FILE* fp, PFN_P
pfn_post(cookie);
}
this->UpdateComponents = true;
this->update_errors();
}

View File

@ -88,24 +88,11 @@ const char*
GetComponent(int id, int n)
{
static const char err_msg[] = "GetComponent: Invalid instance id.\n";
static const char empty[] = "";
static std::string comp;
IPhreeqc* IPhreeqcPtr = IPhreeqcLib::GetInstance(id);
if (IPhreeqcPtr)
{
std::list< std::string > comps = IPhreeqcPtr->ListComponents();
if (n < 0 || n >= (int)comps.size())
{
return empty;
}
std::list< std::string >::iterator it = comps.begin();
for(int i = 0; i < n; ++i)
{
++it;
}
comp = (*it);
return comp.c_str();
return IPhreeqcPtr->GetComponent(n);
}
return err_msg;
}
@ -116,7 +103,7 @@ GetComponentCount(int id)
IPhreeqc* IPhreeqcPtr = IPhreeqcLib::GetInstance(id);
if (IPhreeqcPtr)
{
return (int)IPhreeqcPtr->ListComponents().size();
return (int)IPhreeqcPtr->GetComponentCount();
}
return IPQ_BADINSTANCE;
}

View File

@ -253,7 +253,7 @@
/>
<Tool
Name="VCPostBuildEventTool"
CommandLine="copy phreeqc.dat.in phreeqc.dat&#x0D;&#x0A;copy ex1.in ex1&#x0D;&#x0A;"
CommandLine="copy phreeqc.dat.in phreeqc.dat&#x0D;&#x0A;copy ex2.in ex2&#x0D;&#x0A;"
/>
</Configuration>
<Configuration

View File

@ -23,6 +23,73 @@ IPQ_RESULT USER_PUNCH(int n, const char* element, int max);
IPQ_RESULT SELECTED_OUTPUT(int n);
IPQ_RESULT DUMP(int n);
const char ex15_dat[] =
"SOLUTION_MASTER_SPECIES\n"
"C CO2 2.0 61.0173 12.0111\n"
"Cl Cl- 0.0 Cl 35.453\n"
"Co Co+2 0.0 58.93 58.93 \n"
"E e- 0.0 0.0 0.0\n"
"H H+ -1. 1.008 1.008\n"
"H(0) H2 0.0 1.008\n"
"H(1) H+ -1. 1.008\n"
"N NH4+ 0.0 14.0067 14.0067\n"
"Na Na+ 0.0 Na 22.9898\n"
"Nta Nta-3 3.0 1. 1.\n"
"O H2O 0.0 16.00 16.00\n"
"O(-2) H2O 0.0 18.016\n"
"O(0) O2 0.0 16.00\n"
"SOLUTION_SPECIES\n"
"2H2O = O2 + 4H+ + 4e- \n"
" log_k -86.08; -gamma 1e7 0.0\n"
"2 H+ + 2 e- = H2\n"
" log_k -3.15; -gamma 1e7 0.0\n"
"H+ = H+\n"
" log_k 0.0; -gamma 1e7 0.0\n"
"e- = e-\n"
" log_k 0.0; -gamma 1e7 0.0\n"
"H2O = H2O\n"
" log_k 0.0; -gamma 1e7 0.0\n"
"CO2 = CO2\n"
" log_k 0.0; -gamma 1e7 0.0\n"
"Na+ = Na+\n"
" log_k 0.0; -gamma 1e7 0.0\n"
"Cl- = Cl-\n"
" log_k 0.0; -gamma 1e7 0.0\n"
"Co+2 = Co+2\n"
" log_k 0.0; -gamma 1e7 0.0\n"
"NH4+ = NH4+\n"
" log_k 0.0; -gamma 1e7 0.0\n"
"Nta-3 = Nta-3\n"
" log_k 0.0; -gamma 1e7 0.0\n"
"Nta-3 + 3H+ = H3Nta\n"
" log_k 14.9; -gamma 1e7 0.0\n"
"Nta-3 + 2H+ = H2Nta-\n"
" log_k 13.3; -gamma 1e7 0.0\n"
"Nta-3 + H+ = HNta-2\n"
" log_k 10.3; -gamma 1e7 0.0\n"
"Nta-3 + Co+2 = CoNta-\n"
" log_k 11.7; -gamma 1e7 0.0\n"
"2 Nta-3 + Co+2 = CoNta2-4\n"
" log_k 14.5; -gamma 1e7 0.0\n"
"Nta-3 + Co+2 + H2O = CoOHNta-2 + H+\n"
" log_k 0.5; -gamma 1e7 0.0\n"
"Co+2 + H2O = CoOH+ + H+\n"
" log_k -9.7; -gamma 1e7 0.0\n"
"Co+2 + 2H2O = Co(OH)2 + 2H+\n"
" log_k -22.9; -gamma 1e7 0.0\n"
"Co+2 + 3H2O = Co(OH)3- + 3H+\n"
" log_k -31.5; -gamma 1e7 0.0\n"
"CO2 + H2O = HCO3- + H+\n"
" log_k -6.35; -gamma 1e7 0.0\n"
"CO2 + H2O = CO3-2 + 2H+\n"
" log_k -16.68; -gamma 1e7 0.0\n"
"NH4+ = NH3 + H+\n"
" log_k -9.3; -gamma 1e7 0.0\n"
"H2O = OH- + H+\n"
" log_k -14.0; -gamma 1e7 0.0\n"
"END\n";
TestIPhreeqcLib::TestIPhreeqcLib(void)
{
@ -78,72 +145,6 @@ void TestIPhreeqcLib::TestLoadDatabase(void)
void TestIPhreeqcLib::TestLoadDatabaseString(void)
{
const char ex15_dat[] =
"SOLUTION_MASTER_SPECIES\n"
"C CO2 2.0 61.0173 12.0111\n"
"Cl Cl- 0.0 Cl 35.453\n"
"Co Co+2 0.0 58.93 58.93 \n"
"E e- 0.0 0.0 0.0\n"
"H H+ -1. 1.008 1.008\n"
"H(0) H2 0.0 1.008\n"
"H(1) H+ -1. 1.008\n"
"N NH4+ 0.0 14.0067 14.0067\n"
"Na Na+ 0.0 Na 22.9898\n"
"Nta Nta-3 3.0 1. 1.\n"
"O H2O 0.0 16.00 16.00\n"
"O(-2) H2O 0.0 18.016\n"
"O(0) O2 0.0 16.00\n"
"SOLUTION_SPECIES\n"
"2H2O = O2 + 4H+ + 4e- \n"
" log_k -86.08; -gamma 1e7 0.0\n"
"2 H+ + 2 e- = H2\n"
" log_k -3.15; -gamma 1e7 0.0\n"
"H+ = H+\n"
" log_k 0.0; -gamma 1e7 0.0\n"
"e- = e-\n"
" log_k 0.0; -gamma 1e7 0.0\n"
"H2O = H2O\n"
" log_k 0.0; -gamma 1e7 0.0\n"
"CO2 = CO2\n"
" log_k 0.0; -gamma 1e7 0.0\n"
"Na+ = Na+\n"
" log_k 0.0; -gamma 1e7 0.0\n"
"Cl- = Cl-\n"
" log_k 0.0; -gamma 1e7 0.0\n"
"Co+2 = Co+2\n"
" log_k 0.0; -gamma 1e7 0.0\n"
"NH4+ = NH4+\n"
" log_k 0.0; -gamma 1e7 0.0\n"
"Nta-3 = Nta-3\n"
" log_k 0.0; -gamma 1e7 0.0\n"
"Nta-3 + 3H+ = H3Nta\n"
" log_k 14.9; -gamma 1e7 0.0\n"
"Nta-3 + 2H+ = H2Nta-\n"
" log_k 13.3; -gamma 1e7 0.0\n"
"Nta-3 + H+ = HNta-2\n"
" log_k 10.3; -gamma 1e7 0.0\n"
"Nta-3 + Co+2 = CoNta-\n"
" log_k 11.7; -gamma 1e7 0.0\n"
"2 Nta-3 + Co+2 = CoNta2-4\n"
" log_k 14.5; -gamma 1e7 0.0\n"
"Nta-3 + Co+2 + H2O = CoOHNta-2 + H+\n"
" log_k 0.5; -gamma 1e7 0.0\n"
"Co+2 + H2O = CoOH+ + H+\n"
" log_k -9.7; -gamma 1e7 0.0\n"
"Co+2 + 2H2O = Co(OH)2 + 2H+\n"
" log_k -22.9; -gamma 1e7 0.0\n"
"Co+2 + 3H2O = Co(OH)3- + 3H+\n"
" log_k -31.5; -gamma 1e7 0.0\n"
"CO2 + H2O = HCO3- + H+\n"
" log_k -6.35; -gamma 1e7 0.0\n"
"CO2 + H2O = CO3-2 + 2H+\n"
" log_k -16.68; -gamma 1e7 0.0\n"
"NH4+ = NH3 + H+\n"
" log_k -9.3; -gamma 1e7 0.0\n"
"H2O = OH- + H+\n"
" log_k -14.0; -gamma 1e7 0.0\n"
"END\n";
int n = ::CreateIPhreeqc();
CPPUNIT_ASSERT(n >= 0);
@ -1942,6 +1943,81 @@ void TestIPhreeqcLib::TestGetComponent(void)
CPPUNIT_ASSERT_EQUAL( std::string(""), std::string(::GetComponent(n, 4)) );
CPPUNIT_ASSERT_EQUAL( std::string(""), std::string(::GetComponent(n, 5)) );
// add solution block
CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::SOLUTION(n, 1.0, 1.0, 1.0) );
// run
CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::SetOutputFileOn(n, 0) );
CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::SetErrorFileOn(n, 0) );
CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::SetLogFileOn(n, 0) );
CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::SetSelectedOutputFileOn(n, 0) );
CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::SetDumpFileOn(n, 0) );
CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::SetDumpStringOn(n, 0) );
CPPUNIT_ASSERT_EQUAL( 0, ::RunAccumulated(n) );
CPPUNIT_ASSERT_EQUAL( 3, ::GetComponentCount(n) );
CPPUNIT_ASSERT_EQUAL( std::string(""), std::string(::GetComponent(n, -2)) );
CPPUNIT_ASSERT_EQUAL( std::string(""), std::string(::GetComponent(n, -1)) );
CPPUNIT_ASSERT_EQUAL( std::string("C"), std::string(::GetComponent(n, 0)) );
CPPUNIT_ASSERT_EQUAL( std::string("Ca"), std::string(::GetComponent(n, 1)) );
CPPUNIT_ASSERT_EQUAL( std::string("Na"), std::string(::GetComponent(n, 2)) );
CPPUNIT_ASSERT_EQUAL( std::string(""), std::string(::GetComponent(n, 3)) );
CPPUNIT_ASSERT_EQUAL( std::string(""), std::string(::GetComponent(n, 4)) );
CPPUNIT_ASSERT_EQUAL( std::string(""), std::string(::GetComponent(n, 5)) );
// clear using LoadDatabase
CPPUNIT_ASSERT_EQUAL( 0, ::LoadDatabase(n, "phreeqc.dat") );
CPPUNIT_ASSERT_EQUAL( 0, ::GetComponentCount(n) );
CPPUNIT_ASSERT_EQUAL( std::string(""), std::string(::GetComponent(n, -2)) );
CPPUNIT_ASSERT_EQUAL( std::string(""), std::string(::GetComponent(n, -1)) );
CPPUNIT_ASSERT_EQUAL( std::string(""), std::string(::GetComponent(n, 0)) );
CPPUNIT_ASSERT_EQUAL( std::string(""), std::string(::GetComponent(n, 1)) );
// add solution block
CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::SOLUTION(n, 1.0, 1.0, 1.0) );
CPPUNIT_ASSERT_EQUAL( 0, ::RunAccumulated(n) );
CPPUNIT_ASSERT_EQUAL( 3, ::GetComponentCount(n) );
CPPUNIT_ASSERT_EQUAL( std::string(""), std::string(::GetComponent(n, -2)) );
CPPUNIT_ASSERT_EQUAL( std::string(""), std::string(::GetComponent(n, -1)) );
CPPUNIT_ASSERT_EQUAL( std::string("C"), std::string(::GetComponent(n, 0)) );
CPPUNIT_ASSERT_EQUAL( std::string("Ca"), std::string(::GetComponent(n, 1)) );
CPPUNIT_ASSERT_EQUAL( std::string("Na"), std::string(::GetComponent(n, 2)) );
CPPUNIT_ASSERT_EQUAL( std::string(""), std::string(::GetComponent(n, 3)) );
CPPUNIT_ASSERT_EQUAL( std::string(""), std::string(::GetComponent(n, 4)) );
CPPUNIT_ASSERT_EQUAL( std::string(""), std::string(::GetComponent(n, 5)) );
// clear using LoadDatabaseString
CPPUNIT_ASSERT_EQUAL( 0, ::LoadDatabaseString(n, ex15_dat) );
CPPUNIT_ASSERT_EQUAL( 0, ::GetComponentCount(n) );
// add solution block
CPPUNIT_ASSERT_EQUAL( IPQ_OK, ::SOLUTION(n, 1.0, 1.0, 1.0) );
CPPUNIT_ASSERT_EQUAL( 0, ::RunAccumulated(n) );
// ex15.dat doesn't have Ca
CPPUNIT_ASSERT_EQUAL( 2, ::GetWarningStringLineCount(n));
CPPUNIT_ASSERT_EQUAL( std::string("WARNING: Could not find element in database, Ca."), std::string(::GetWarningStringLine(n, 0)) );
CPPUNIT_ASSERT_EQUAL( std::string("\tConcentration is set to zero."), std::string(::GetWarningStringLine(n, 1)) );
CPPUNIT_ASSERT_EQUAL( 2, ::GetComponentCount(n) );
CPPUNIT_ASSERT_EQUAL( std::string(""), std::string(::GetComponent(n, -2)) );
CPPUNIT_ASSERT_EQUAL( std::string(""), std::string(::GetComponent(n, -1)) );
CPPUNIT_ASSERT_EQUAL( std::string("C"), std::string(::GetComponent(n, 0)) );
CPPUNIT_ASSERT_EQUAL( std::string("Na"), std::string(::GetComponent(n, 1)) );
CPPUNIT_ASSERT_EQUAL( std::string(""), std::string(::GetComponent(n, 2)) );
CPPUNIT_ASSERT_EQUAL( std::string(""), std::string(::GetComponent(n, 3)) );
if (n >= 0)
{
CPPUNIT_ASSERT_EQUAL(IPQ_OK, ::DestroyIPhreeqc(n));