From 2a0dc9377898706eadf3aa1fa3ef20c9f152e2b0 Mon Sep 17 00:00:00 2001 From: Max Luebke Date: Wed, 13 Mar 2024 22:05:06 +0100 Subject: [PATCH] Update RAW Keywords to always refer to ID 1 --- src/Init/GridInit.cpp | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/src/Init/GridInit.cpp b/src/Init/GridInit.cpp index f392ad6f3..3576e3352 100644 --- a/src/Init/GridInit.cpp +++ b/src/Init/GridInit.cpp @@ -3,12 +3,12 @@ #include #include #include +#include +#include +#include namespace poet { -static Rcpp::NumericMatrix -pqcScriptToGrid(RInside &R, const std::string &script, const std::string &db, - std::map &raw_dumps) { - IPhreeqcPOET phreeqc(db, script); +static Rcpp::NumericMatrix pqcScriptToGrid(IPhreeqcPOET &phreeqc, RInside &R) { const auto solution_ids = phreeqc.getSolutionIds(); auto col_names = phreeqc.getInitNames(); @@ -32,24 +32,29 @@ pqcScriptToGrid(RInside &R, const std::string &script, const std::string &db, Rcpp::colnames(mat) = Rcpp::wrap(col_names); - raw_dumps = phreeqc.raw_dumps(); - return mat; } static inline Rcpp::List matToGrid(RInside &R, const Rcpp::NumericMatrix &mat, const Rcpp::NumericMatrix &grid) { - // Rcpp::List res; - // res["init_mat"] = mat; - // res["grid_mat"] = grid; - Rcpp::Function pqc_to_grid("pqc_to_grid"); - // R.parseEvalQ("res_df <- pqc_to_grid(init_mat, grid_mat)"); - return pqc_to_grid(mat, grid); } +static inline std::map +replaceRawSolutionsIDs(std::map raws) { + for (auto &raw : raws) { + std::string &s = raw.second; + // find at beginning of line '*_RAW' followed by a number and change this + // number to 1 + std::regex re(R"((RAW\s+)(\d+))"); + s = std::regex_replace(s, re, "RAW 1"); + } + + return raws; +} + void InitialList::initGrid(const Rcpp::List &grid_input) { // parse input values const std::string script = Rcpp::as(grid_input["pqc_in"]); @@ -95,10 +100,13 @@ void InitialList::initGrid(const Rcpp::List &grid_input) { throw std::runtime_error("Grid size must be positive."); } - this->phreeqc_mat = - pqcScriptToGrid(R, script_rp, database_rp, this->pqc_raw_dumps); + IPhreeqcPOET phreeqc(database_rp, script_rp); + + this->phreeqc_mat = pqcScriptToGrid(phreeqc, R); this->initial_grid = matToGrid(R, this->phreeqc_mat, grid_def); + this->pqc_raw_dumps = replaceRawSolutionsIDs(phreeqc.raw_dumps()); + R["pqc_mat"] = this->phreeqc_mat; R["grid_def"] = initial_grid;