Conceptual Models > Program coupling
IPHREEQC module - Empty string output
(1/1)
halfonsog:
Hello everybody,
I have compiled IPHREEQC 3.8.6 on Debian 12. Compilation was well: no errors. I made a simple cpp testing program and it is running without issues, but program ends with empty output string when traying to calculate the pH.
Any help is highly appreciated.
cpp program code:
--- Code: ---#include <iostream>
#include <fstream>
#include "IPhreeqc.hpp"
int main() {
// Create an IPHREEQC instance
IPhreeqc iphreeqc;
// Load the database
if (iphreeqc.LoadDatabase("phreeqc.dat") != 0) {
std::cerr << "Failed to load database: " << iphreeqc.GetErrorString() << std::endl;
return 1;
}
std::cout << "Database loaded successfully." << std::endl;
// Read the input file
std::ifstream inputFile("input.txt");
if (!inputFile) {
std::cerr << "Failed to open input file." << std::endl;
return 1;
}
std::string input((std::istreambuf_iterator<char>(inputFile)), std::istreambuf_iterator<char>());
// Run the calculation
iphreeqc.SetSelectedOutputStringOn(true);
int c = iphreeqc.GetSelectedOutputStringLineCount();
std::cout << "Number of selected output lines: " << c << std::endl;
std::string ret;
if (c >= 3)
{
ret = iphreeqc.GetSelectedOutputStringLine(2); // Assuming the pH value is on the third line
}
else if (c == 1)
{
ret = iphreeqc.GetSelectedOutputString();
}
else
{
ret = "NaN";
std::cerr << "Unexpected number of output lines: " << c << std::endl;
}
std::cout << "Selected output:\n" << ret << std::endl;
return 0;
}
--- End code ---
input.text file:
--- Code: ---TITLE pH Calculation - PhreeqC 3.8.6 - 2024
SOLUTION_MASTER_SPECIES
Ac Ac- 0 Ac 59.041
SOLUTION_SPECIES
Ac- = Ac-
log_k 0
-gamma 4.5 0
Ac- + H+ = HAc
log_k 4.757
delta_h 0.1136 kcal
-analytical_expression -18.67257 0.0076792 1500.65 6.50923 0 0
GAS_PHASE 1
fixed_pressure
pressure 138.169257338
temperature 78
volume 1000
CO2(g) 7.737478410927999
H2S(g) 2.901554404098
CH4(g) 127.53022452297398
SOLUTION 1
# Acetate being treated as Sodium Acetate
density 1.05
temp 78.00
pH 7 charge
units mg/L
Na 648.4585225509829
K 0
Ca 0
Mg 0
Ba 0
Sr 0
Fe 0
Cl 1000
Ac 0
SO4 0
HCO3 0
EQUILIBRIUM_PHASES 1
SELECTED_OUTPUT
-reset false
USER_PUNCH 1
-headings pH
-start
10 PUNCH -1*Log10(ACT("H+"))
-end
END
--- End code ---
Output when program is run:
Database loaded successfully.
Number of selected output lines: 0
Unexpected number of output lines: 0
Selected output:
NaN
dlparkhurst:
You need either RunFile or RunString.
Look at examples/cpp/advect/advect.cpp or the documentation in doc/phreeqc3.chm or doc/html.
halfonsog:
Thank you very much for your catch!
Right! I've missed it on that one. I have corrected, recompiled and running with the same result :-(
I would expect some error that guides me to resolution, but no errors found.
Here you can see the updated code:
--- Code: ---#include <iostream>
#include <fstream>
#include "IPhreeqc.hpp"
int main() {
// Create an IPHREEQC instance
IPhreeqc iphreeqc;
// Load the database
if (iphreeqc.LoadDatabase("phreeqc.dat") != 0) {
std::cerr << "Failed to load database: " << iphreeqc.GetErrorString() << std::endl;
return 1;
}
std::cout << "Database loaded successfully." << std::endl;
// Read the input file
std::ifstream inputFile("input.txt");
if (!inputFile) {
std::cerr << "Failed to open input file." << std::endl;
return 1;
}
std::string input((std::istreambuf_iterator<char>(inputFile)), std::istreambuf_iterator<char>());
// Run the calculation
if (iphreeqc.RunString(input.c_str()) != 0) {
std::cerr << "Failed to run PHREEQC calculation: " << iphreeqc.GetErrorString() << std::endl;
return 1;
}
std::cout << "PHREEQC calculation completed successfully." << std::endl;
iphreeqc.SetSelectedOutputStringOn(true);
int c = iphreeqc.GetSelectedOutputStringLineCount();
std::cout << "Number of selected output lines: " << c << std::endl;
std::string ret;
if (c >= 3)
{
ret = iphreeqc.GetSelectedOutputStringLine(2); // Assuming the pH value is on the third line
}
else if (c == 1)
{
ret = iphreeqc.GetSelectedOutputString();
}
else
{
ret = "NaN";
std::cerr << "Unexpected number of output lines: " << c << std::endl;
}
std::cout << "Selected output:\n" << ret << std::endl;
return 0;
}
--- End code ---
Maybe there is another way to check if library is well compiled and working.
Any help is very welcome
halfonsog:
Dear dlparkhurst,
Thank you for your input that make me to review all doc.
I've found the problem: the call to SetSelectedOutputStringOn(true) should be done before calling to RunString(data).
Now is working well :-)
Navigation
[0] Message Index
Go to full version