Add 'app' directory from golem branch

This commit is contained in:
Max Lübke 2024-09-10 15:08:55 +02:00
parent 6fd2e96411
commit e42c120518
6 changed files with 2106 additions and 0 deletions

View File

@ -393,6 +393,7 @@ if (BUILD_CLR_LIBS)
endif()
add_subdirectory(poet)
add_subdirectory(app)
include (CTest)

25
app/CMakeLists.txt Normal file
View File

@ -0,0 +1,25 @@
## Time-stamp: "Last modified 2024-08-26 17:42:18 delucia"
add_executable(ipqMain
ipqMain.cpp
)
add_executable(ipqEngine
ipqEngine.cpp
)
# add_library(IPhreeqcPOET ${PQC_POET_SRC})
##target_link_libraries(IPhreeqcPOET PUBLIC IPhreeqc)
target_include_directories(IPhreeqcPOET PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
PRIVATE src)
target_link_libraries(ipqMain
IPhreeqcPOET
)
target_link_libraries(ipqEngine
IPhreeqcPOET
)

122
app/ipqEngine.cpp Normal file
View File

@ -0,0 +1,122 @@
#include <algorithm>
#include <cstddef>
#include <cstdio>
#include <iomanip>
#include <linux/limits.h>
#include <stdlib.h>
#include <string>
#include <unistd.h>
#include <vector>
#include <wordexp.h>
#include "POETInit.hpp"
#include "PhreeqcEngine.hpp"
#include "PhreeqcInit.hpp"
static std::string readFile(const std::string &path) {
std::string string_rpath(PATH_MAX, '\0');
if (realpath(path.c_str(), string_rpath.data()) == nullptr) {
throw std::runtime_error("Failed to resolve the realpath to file " + path);
}
std::ifstream file(string_rpath);
if (!file.is_open()) {
throw std::runtime_error("Failed to open file: " + path);
}
std::stringstream buffer;
buffer << file.rdbuf();
return buffer.str();
}
void printVectorsAsTable(const std::vector<std::string> &names,
const std::vector<double> &values) {
if (names.size() != values.size()) {
std::cerr << "Error: Vectors are not of the same length." << std::endl;
return;
}
std::cout << std::left << std::setw(20) << "Name" << std::setw(20) << "Value"
<< std::endl;
std::cout << std::string(40, '-') << std::endl;
for (std::size_t i = 0; i < names.size(); ++i) {
std::cout << std::left << std::setw(20) << names[i] << std::setw(20)
<< values[i] << std::endl;
}
}
template <class T> void printVector(const std::vector<T> &vec) {
for (const auto &elem : vec) {
std::cout << elem << " ";
}
std::cout << std::endl;
}
int main(int argc, char *argv[]) {
if (argc < 3) {
std::cout << "Two args needed, PHREEQC script and database" << std::endl;
return 1;
}
const std::string &script = argv[1];
const std::string &database = argv[2];
wordexp_t exp_result;
std::cout << "Reading script from file " << script << " using database "
<< database << std::endl;
// expand the script and database path
const std::string read_script = readFile(script);
const std::string read_database = readFile(database);
// initialize the module instance "ipqcmod"
PhreeqcInit ipqcmod(read_database, read_script);
// allocate tabular data
PhreeqcInit::PhreeqcMat pqc_mat = ipqcmod.getPhreeqcMat();
// for (std::size_t i = 0; i < nrows; ++i) {
auto pure_names = ipqcmod.getEquilibriumNames(pqc_mat.ids[0]);
auto totals_names = ipqcmod.getSolutionNames(pqc_mat.ids[0]);
POETConfig MyConfig;
MyConfig.database = read_database;
MyConfig.input_script = read_script;
MyConfig.cell = POETInitCell{
// H, O and charge need to be removed. I'm not sure why I did it this way.
std::vector<std::string>(totals_names.begin() + 3, totals_names.end()),
{},
{},
{},
pure_names,
{},
{}};
std::vector<std::string> full_names = {"ID"};
full_names.insert(full_names.end(), pqc_mat.names.begin(),
pqc_mat.names.end());
PhreeqcEngine MyEngine(MyConfig);
std::vector<double> NewInp = pqc_mat.values[0];
NewInp.insert(NewInp.begin(), 1);
std::cout << "Before running the cell:\n";
printVectorsAsTable(full_names, NewInp);
// std::for_each(NewInp.begin()+3, NewInp.end(),
// [](double a) { return a * 0.8;});
NewInp[7] += 0.2; // Cl
NewInp[9] += 0.2; // Na
MyEngine.runCell(NewInp, 200);
std::cout << "\n\nAfter running the cell:\n";
printVectorsAsTable(full_names, NewInp);
return 0;
}

92
app/ipqMain.cpp Normal file
View File

@ -0,0 +1,92 @@
// Time-stamp: "Last modified 2024-08-27 09:00:18 delucia"
#include <cstddef>
#include <linux/limits.h>
#include <stdlib.h>
#include <string>
#include <unistd.h>
#include <vector>
#include <wordexp.h>
#include "POETInit.hpp"
#include "PhreeqcEngine.hpp"
#include "PhreeqcInit.hpp"
static std::string readFile(const std::string &path) {
std::string string_rpath(PATH_MAX, '\0');
if (realpath(path.c_str(), string_rpath.data()) == nullptr) {
throw std::runtime_error("Failed to resolve the realpath to file " + path);
}
std::ifstream file(string_rpath);
if (!file.is_open()) {
throw std::runtime_error("Failed to open file: " + path);
}
std::stringstream buffer;
buffer << file.rdbuf();
return buffer.str();
}
int main(int argc, char *argv[]) {
const std::string &script = argv[1];
const std::string &database = argv[2];
// wordexp_t exp_result;
std::cout << "Reading script from file " << script << " using database "
<< database << std::endl;
// expand the script and database path
const std::string read_script = readFile(script);
const std::string read_database = readFile(database);
// initialize the module instance "ipqcmod"
PhreeqcInit ipqcmod(read_database, read_script);
// allocate tabular data
PhreeqcInit::PhreeqcMat pqc_mat = ipqcmod.getPhreeqcMat();
const std::size_t ncols = pqc_mat.names.size();
const std::size_t nrows = pqc_mat.values.size();
std::cout << "Script contains " << nrows << " rows and " << ncols
<< " columns" << std::endl;
// pqc_mat.names.insert(pqc_mat.names.begin(), "ID");
// Rcpp::NumericMatrix mat(nrows, ncols + 1);
std::cout << std::endl << "Number of CELLS/SOLUTIONS: " << nrows << std::endl;
for (std::size_t i = 0; i < nrows; ++i) {
auto pure_names = ipqcmod.getEquilibriumNames(pqc_mat.ids[i]);
auto totals_names = ipqcmod.getSolutionNames(pqc_mat.ids[i]);
std::cout << std::endl << "SOL " << pqc_mat.ids[i] << " - Pure phases : ";
for (std::size_t j = 0; j < pure_names.size(); j++) {
std::cout << pure_names[j] << ", ";
}
std::cout << std::endl << "SOL " << pqc_mat.ids[i] << " - Total concs: ";
for (std::size_t j = 0; j < totals_names.size(); j++) {
std::cout << totals_names[j] << ", ";
}
std::cout << std::endl;
}
// alternatively, use the pqc_mat object
std::cout << std::endl << "Total pqc_mat: " << std::endl;
for (std::size_t i = 0; i < nrows; ++i) {
std::cout << "ID: " << pqc_mat.ids[i] << " - ";
for (std::size_t j = 0; j < ncols; ++j) {
std::cout << pqc_mat.names[j] << ": " << pqc_mat.values[i][j] << ", ";
}
std::cout << std::endl;
}
std::cout << std::endl;
return 0;
}

1853
app/phreeqc.dat Normal file

File diff suppressed because it is too large Load Diff

13
app/testEngine.pqi Normal file
View File

@ -0,0 +1,13 @@
SOLUTION 1
units mol/kgw
temp 25
Ca 0.1
Mg 0.1
Cl 0.5 charge
Na 0.1
PURE 1
Calcite 0.0 1
Dolomite 0.0 0
## RUN_CELLS
## -cells 1
END