Reorganized to put callback (and everything) in class.

git-svn-id: svn://136.177.114.72/svn_GW/IPhreeqc/trunk@7918 1feff8c3-07ed-0310-ac33-dd36852eb9cd
This commit is contained in:
David L Parkhurst 2013-08-12 20:40:32 +00:00
parent 7cb9b0ff1f
commit 02deb40ab3

View File

@ -8,13 +8,17 @@
class MyData
{
public:
void Mydata() {};
void Mydata()
{
this->IPhreeqc_ptr = NULL;
this->year = 0.0;
}
~MyData() {};
// data members
IPhreeqc * IPhreeqc_ptr;
IPhreeqc * IPhreeqc_ptr;
std::vector < VAR > results;
double year;
double year;
// methods
void ExtractWrite(int cell)
@ -37,67 +41,70 @@ public:
IPhreeqc_ptr->OutputErrorString();
exit(EXIT_FAILURE);
}
static double MyCallback(double x1, double x2, const char * str1, void *my_ptr)
{
/*
Use of a callback is optional.
The callback provides a way to obtain data from a Basic program
through the variables x1, x2, and str1, and send data to a
Basic program through the return value of the callback.
The void pointer mydata can be used to obtain data from the
calling program; in this example, it points to a structure.
The callback function is called whenever CALLBACK(x1, x2, str$)
is used in a Basic program (usually USER_PUNCH). See file "ic".
*/
if (strcmp(str1, "Year") == 0)
{
fprintf(stderr, "\nCallback for cell %d: pH %8.2f\n", (int) x1, x2);
return ((MyData *) my_ptr)->year;
}
return -1;
}
void Exec(void)
{
// Create module
this->IPhreeqc_ptr = new IPhreeqc;
// Load database
if (this->IPhreeqc_ptr->LoadDatabase("phreeqc.dat") != 0) this->EHandler();
// Set callback
this->IPhreeqc_ptr->SetBasicCallback(this->MyCallback, (void *) this);
// Define initial conditions and selected output
this->year = 2014.0;
if (this->IPhreeqc_ptr->RunFile("ic") != 0) this->EHandler();
// Run cell 1
if (this->IPhreeqc_ptr->RunString("RUN_CELLS; -cells; 1; END") != 0) this->EHandler();
// Extract/write results
this->ExtractWrite(1);
// Advect cell 1 solution to cell 2
this->year += 1.0;
// Define new solution composition for cell 2 from cell 1 selected-output
std::ostringstream oss;
oss << "SOLUTION_MODIFY 2" << "\n";
oss << " -cb " << this->results[0].dVal << "\n";
oss << " -total_h " << this->results[1].dVal << "\n";
oss << " -total_o " << this->results[2].dVal << "\n";
oss << " -totals " << "\n";
oss << " C " << this->results[3].dVal << "\n";
oss << " Ca " << this->results[4].dVal << "\n";
// run cell 2
oss << "RUN_CELLS; -cells; 2; END\n";
if (this->IPhreeqc_ptr->RunString(oss.str().c_str()) != 0) this->EHandler();
// Extract/write results
this->ExtractWrite(2);
// Destroy module
delete this->IPhreeqc_ptr;
}
};
static double MyCallback(double x1, double x2, const char * str1, void *my_ptr)
{
/*
Use of a callback is optional.
The callback provides a way to obtain data from a Basic program
through the variables x1, x2, and str1, and send data to a
Basic program through the return value of the callback.
The void pointer mydata can be used to obtain data from the
calling program; in this example, it points to a structure.
The callback function is called whenever CALLBACK(x1, x2, str$)
is used in a Basic program (usually USER_PUNCH). See file "ic".
*/
if (strcmp(str1, "Year") == 0)
{
fprintf(stderr, "\nCallback for cell %d: pH %8.2f\n", (int) x1, x2);
return ((MyData *) my_ptr)->year;
}
return -1;
}
int main(void)
{
MyData mydata;
mydata.year = 2012.0;
// Create module
IPhreeqc * myIPhreeqc = new IPhreeqc;
mydata.IPhreeqc_ptr = myIPhreeqc;
// Load database
if (myIPhreeqc->LoadDatabase("phreeqc.dat") != 0) mydata.EHandler();
// Set callback
myIPhreeqc->SetBasicCallback(&MyCallback, (void *) &mydata);
// Define initial conditions and selected output
if (myIPhreeqc->RunFile("ic") != 0) mydata.EHandler();
// Run cell 1
if (myIPhreeqc->RunString("RUN_CELLS; -cells; 1; END") != 0) mydata.EHandler();
// Extract/write results
mydata.ExtractWrite(1);
// Advect cell 1 solution to cell 2
mydata.year += 1.0;
// Define new solution composition for cell 2
std::ostringstream oss;
oss << "SOLUTION_MODIFY 2" << "\n";
oss << " -cb " << mydata.results[0].dVal << "\n";
oss << " -total_h " << mydata.results[1].dVal << "\n";
oss << " -total_o " << mydata.results[2].dVal << "\n";
oss << " -totals " << "\n";
oss << " C " << mydata.results[3].dVal << "\n";
oss << " Ca " << mydata.results[4].dVal << "\n";
// run cell 2
oss << "RUN_CELLS; -cells; 2; END\n";
if (myIPhreeqc->RunString(oss.str().c_str()) != 0) mydata.EHandler();
// Extract/write results
mydata.ExtractWrite(2);
// Destroy module
delete myIPhreeqc;
mydata.Exec();
}