refactor(solution): consolidate cxxSolution constructors

This commit is contained in:
Max Lübke 2025-10-28 11:36:36 +01:00
parent 6f356bb9b6
commit 7806aad339

View File

@ -2,8 +2,8 @@
//
//////////////////////////////////////////////////////////////////////
#ifdef _DEBUG
#pragma warning( \
disable : 4786) // disable truncation warning (Only used by debugger)
#pragma warning(disable \
: 4786) // disable truncation warning (Only used by debugger)
#endif
#include "Solution.h"
@ -27,7 +27,6 @@ static char THIS_FILE[] = __FILE__;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
cxxSolution::cxxSolution(PHRQ_io *io)
//
// default constructor for cxxSolution
@ -123,154 +122,41 @@ cxxSolution::cxxSolution(std::map<int, cxxSolution> &solutions, cxxMix &mix,
this->potV = cxxsoln_ptr1->potV;
}
//
// Mix solutions
//
const std::map<int, LDBLE> &mixcomps = mix.Get_mixComps();
std::map<int, LDBLE>::const_iterator it;
for (it = mixcomps.begin(); it != mixcomps.end(); it++) {
sol = solutions.find(it->first);
if (sol == solutions.end()) {
std::ostringstream msg;
msg << "Solution " << it->first << " not found in mix_cxxSolutions.";
error_msg(msg.str(), CONTINUE);
} else {
cxxsoln_ptr1 = &(sol->second);
this->add(*cxxsoln_ptr1, it->second);
}
}
}
cxxSolution::~cxxSolution() { delete this->initial_data; }
cxxSolution::cxxSolution(PHRQ_io * io)
//
// default constructor for cxxSolution
//
: cxxNumKeyword(io)
{
this->io = io;
this->new_def = false;
this->patm = 1.0;
this->potV = 0.0;
this->tc = 25.0;
this->ph = 7.0;
this->pe = 4.0;
this->mu = 1e-7;
this->ah2o = 1.0;
this->total_h = 111.1;
this->total_o = 55.55;
this->cb = 0.0;
this->density = 1.0;
this->viscosity = 1.0;
this->viscos_0 = 1.0;
this->mass_water = 1.0;
this->soln_vol = 1.0;
this->total_alkalinity = 0.0;
this->totals.type = cxxNameDouble::ND_ELT_MOLES;
this->master_activity.type = cxxNameDouble::ND_SPECIES_LA;
this->species_gamma.type = cxxNameDouble::ND_SPECIES_GAMMA;
this->initial_data = NULL;
}
cxxSolution::cxxSolution(const cxxSolution &old_sol)
: initial_data(NULL)
{
*this = old_sol;
}
const cxxSolution &
cxxSolution::operator =(const cxxSolution &rhs)
{
if (this != &rhs)
{
this->io = rhs.io;
this->n_user = rhs.n_user;
this->n_user_end = rhs.n_user_end;
this->description = rhs.description;
this->new_def = rhs.new_def;
this->patm = rhs.patm;
this->potV = rhs.potV;
this->tc = rhs.tc;
this->ph = rhs.ph;
this->pe = rhs.pe;
this->mu = rhs.mu;
this->ah2o = rhs.ah2o;
this->total_h = rhs.total_h;
this->total_o = rhs.total_o;
this->density = rhs.density;
this->viscosity = rhs.viscosity;
this->viscos_0 = rhs.viscos_0;
this->cb = rhs.cb;
this->mass_water = rhs.mass_water;
this->soln_vol = rhs.soln_vol;
this->total_alkalinity = rhs.total_alkalinity;
this->totals = rhs.totals;
this->master_activity = rhs.master_activity;
this->species_gamma = rhs.species_gamma;
this->isotopes = rhs.isotopes;
this->species_map = rhs.species_map;
this->log_gamma_map = rhs.log_gamma_map;
this->log_molalities_map = rhs.log_molalities_map;
if (this->initial_data)
delete initial_data;
if (rhs.initial_data != NULL)
this->initial_data = new cxxISolution(*rhs.initial_data);
else
this->initial_data = NULL;
}
return *this;
}
cxxSolution::cxxSolution(std::map < int, cxxSolution > &solutions,
cxxMix & mix, int l_n_user, PHRQ_io * io)
//
// constructor for cxxSolution from mixture of solutions
//
: cxxNumKeyword(io)
{
//
// Zero out solution data
//
this->zero();
this->n_user = this->n_user_end = l_n_user;
this->new_def = false;
this->ah2o = 0;
// potV is an external variable, imposed in a given solution, not mixed.
std::map < int, cxxSolution >::const_iterator sol = solutions.find(mix.Get_n_user());
const cxxSolution *cxxsoln_ptr1;
if (sol != solutions.end())
{
cxxsoln_ptr1 = &(sol->second);
if (cxxsoln_ptr1->new_def)
this->potV = 0.0;
else
this->potV = cxxsoln_ptr1->potV;
}
//
// Sort to enable positive mixes first
const std::map < int, LDBLE >& mixcomps = mix.Get_mixComps();
std::set<std::pair<LDBLE, int> >s;
for (std::map < int, LDBLE >::const_iterator it = mixcomps.begin();
it != mixcomps.end(); it++)
{
const std::map<int, LDBLE> &mixcomps = mix.Get_mixComps();
std::set<std::pair<LDBLE, int>> s;
for (std::map<int, LDBLE>::const_iterator it = mixcomps.begin();
it != mixcomps.end(); it++) {
std::pair<LDBLE, int> p(it->second, it->first);
s.insert(p);
}
//
// Mix solutions
//
std::set < std::pair< LDBLE, int > >::const_reverse_iterator rit = s.rbegin();
for (rit = s.rbegin(); rit != s.rend(); rit++)
{
//
// Mix solutions
//
std::set<std::pair<LDBLE, int>>::const_reverse_iterator rit = s.rbegin();
for (rit = s.rbegin(); rit != s.rend(); rit++) {
sol = solutions.find(rit->second);
if (sol == solutions.end())
{
if (sol == solutions.end()) {
std::ostringstream msg;
msg << "Solution " << rit->second << " not found in mix_cxxSolutions.";
error_msg(msg.str(), CONTINUE);
}
else
{
} else {
cxxsoln_ptr1 = &(sol->second);
this->add(*cxxsoln_ptr1, rit->first);
}
}
}
cxxSolution::~cxxSolution() { delete this->initial_data; }
void cxxSolution::dump_xml(std::ostream &s_oss, unsigned int indent) const {
unsigned int i;
s_oss.precision(DBL_DIG - 1);
std::string indent0(""), indent1("");
for (i = 0; i < indent; ++i)
indent0.append(Utilities::INDENT);
for (i = 0; i < indent + 1; ++i)
indent1.append(Utilities::INDENT);
// Solution element and attributes
s_oss << indent0;
@ -1183,8 +1069,8 @@ void cxxSolution::read_raw(CParser &parser, bool check) {
return;
}
void cxxSolution::Update(LDBLE h_tot, LDBLE o_tot, LDBLE charge, LDBLE tc, LDBLE patm,
const cxxNameDouble &const_nd) {
void cxxSolution::Update(LDBLE h_tot, LDBLE o_tot, LDBLE charge, LDBLE tc,
LDBLE patm, const cxxNameDouble &const_nd) {
this->new_def = false;
this->patm = patm;
// this->potV = 0.0;
@ -1380,21 +1266,17 @@ void cxxSolution::add(const cxxSolution &addee, LDBLE extensive)
LDBLE ext1 = this->mass_water;
LDBLE ext2 = addee.mass_water * extensive;
this->mass_water += addee.mass_water * extensive;
if (this->mass_water <= 0.0)
{
if (this->mass_water <= 0.0) {
std::ostringstream msg;
msg << "Negative mass of water when mixing solutions.";
error_msg(msg.str(), STOP);
}
LDBLE fconc = 0.0, f1 = 0.0, f2 = 0.0;
if (extensive > 0.0)
{
if (extensive > 0.0) {
f1 = ext1 / (ext1 + ext2);
f2 = ext2 / (ext1 + ext2);
}
else
{
} else {
f1 = 1.0;
f2 = 0.0;
}
@ -1420,40 +1302,33 @@ void cxxSolution::add(const cxxSolution &addee, LDBLE extensive)
{
// Add species
std::map<int, double>::const_iterator it = addee.species_map.begin();
for ( ; it != addee.species_map.end(); it++)
{
if (this->species_map.find(it->first) != this->species_map.end())
{
this->species_map[it->first] = this->species_map[it->first] * f1 + it->second * f2;
}
else
{
for (; it != addee.species_map.end(); it++) {
if (this->species_map.find(it->first) != this->species_map.end()) {
this->species_map[it->first] =
this->species_map[it->first] * f1 + it->second * f2;
} else {
this->species_map[it->first] = it->second;
}
}
// Add gammas
std::map<int, double>::const_iterator git = addee.log_gamma_map.begin();
for ( ; git != addee.log_gamma_map.end(); git++)
{
if (this->log_gamma_map.find(git->first) != this->log_gamma_map.end())
{
this->log_gamma_map[git->first] = this->log_gamma_map[git->first] * f1 + git->second * f2;
}
else
{
for (; git != addee.log_gamma_map.end(); git++) {
if (this->log_gamma_map.find(git->first) != this->log_gamma_map.end()) {
this->log_gamma_map[git->first] =
this->log_gamma_map[git->first] * f1 + git->second * f2;
} else {
this->log_gamma_map[git->first] = git->second;
}
}
// Add molalities
std::map<int, double>::const_iterator mit = addee.log_molalities_map.begin();
for (; mit != addee.log_molalities_map.end(); mit++)
{
if (this->log_molalities_map.find(mit->first) != this->log_molalities_map.end())
{
this->log_molalities_map[mit->first] = this->log_molalities_map[mit->first] * f1 + mit->second * f2;
}
else
{
std::map<int, double>::const_iterator mit =
addee.log_molalities_map.begin();
for (; mit != addee.log_molalities_map.end(); mit++) {
if (this->log_molalities_map.find(mit->first) !=
this->log_molalities_map.end()) {
this->log_molalities_map[mit->first] =
this->log_molalities_map[mit->first] * f1 + mit->second * f2;
} else {
this->log_molalities_map[mit->first] = mit->second;
}
}