From f015759286b56643980076b384518046e407c23a Mon Sep 17 00:00:00 2001 From: David L Parkhurst Date: Tue, 27 Oct 2009 22:57:49 +0000 Subject: [PATCH] exchange_modify is working. changed list of comps to map of comps. made sure that description changes were captured for solution, equilibrium_phases, and exchange. surface is next. git-svn-id: svn://136.177.114.72/svn_GW/phreeqcpp/trunk@3725 1feff8c3-07ed-0310-ac33-dd36852eb9cd --- ExchComp.cxx | 132 ++++++++++++++++++++++++----------------------- ExchComp.h | 4 +- Exchange.cxx | 104 ++++++++++++++++++++++++++++++------- Exchange.h | 4 +- PPassemblage.cxx | 13 +++-- ReadClass.cxx | 45 +++++++++------- Solution.cxx | 6 +-- 7 files changed, 196 insertions(+), 112 deletions(-) diff --git a/ExchComp.cxx b/ExchComp.cxx index 5e40a254..959837ad 100644 --- a/ExchComp.cxx +++ b/ExchComp.cxx @@ -188,7 +188,7 @@ cxxExchComp::get_master() } struct exch_comp * -cxxExchComp::cxxExchComp2exch_comp(std::list < cxxExchComp > &el) + cxxExchComp::cxxExchComp2exch_comp(std::map < std::string, cxxExchComp > &el) // // Builds exch_comp structure from of cxxExchComp // @@ -200,20 +200,20 @@ cxxExchComp::cxxExchComp2exch_comp(std::list < cxxExchComp > &el) malloc_error(); int i = 0; - for (std::list < cxxExchComp >::iterator it = el.begin(); it != el.end(); + for (std::map < std::string, cxxExchComp >::iterator it = el.begin(); it != el.end(); ++it) { - exch_comp_ptr[i].formula = it->formula; - exch_comp_ptr[i].formula_z = it->formula_z; - exch_comp_ptr[i].totals = it->totals.elt_list(); - exch_comp_ptr[i].moles = it->moles; - exch_comp_ptr[i].formula_totals = it->formula_totals.elt_list(); - exch_comp_ptr[i].la = it->la; - exch_comp_ptr[i].charge_balance = it->charge_balance; - exch_comp_ptr[i].phase_name = it->phase_name; - exch_comp_ptr[i].phase_proportion = it->phase_proportion; - exch_comp_ptr[i].rate_name = it->rate_name; - exch_comp_ptr[i].master = it->get_master(); + exch_comp_ptr[i].formula = (*it).second.formula; + exch_comp_ptr[i].formula_z = (*it).second.formula_z; + exch_comp_ptr[i].totals = (*it).second.totals.elt_list(); + exch_comp_ptr[i].moles = (*it).second.moles; + exch_comp_ptr[i].formula_totals = (*it).second.formula_totals.elt_list(); + exch_comp_ptr[i].la = (*it).second.la; + exch_comp_ptr[i].charge_balance = (*it).second.charge_balance; + exch_comp_ptr[i].phase_name = (*it).second.phase_name; + exch_comp_ptr[i].phase_proportion = (*it).second.phase_proportion; + exch_comp_ptr[i].rate_name = (*it).second.rate_name; + exch_comp_ptr[i].master = (*it).second.get_master(); i++; } return (exch_comp_ptr); @@ -282,40 +282,41 @@ cxxExchComp::dump_raw(std::ostream & s_oss, unsigned int indent) const // Exch_Comp element and attributes - s_oss << indent0 << "-formula " << this-> - formula << std::endl; - s_oss << indent0 << "-moles " << this->moles << std::endl; - s_oss << indent0 << "-la " << this->la << std::endl; - s_oss << indent0 << "-charge_balance " << this-> - charge_balance << std::endl; + s_oss << indent0 << "-formula " << this->formula << std::endl; + + //s_oss << indent1 << "# critical values" << std::endl; + + // totals + s_oss << indent1; + s_oss << "-totals" << std::endl; + this->totals.dump_raw(s_oss, indent + 2); + + s_oss << indent1 << "-charge_balance " << this->charge_balance << std::endl; + + //s_oss << indent1 << "# Noncritical values" << std::endl; + s_oss << indent1 << "-moles " << this->moles << std::endl; + s_oss << indent1 << "-la " << this->la << std::endl; + if (this->phase_name != NULL) { - s_oss << indent0 << "-phase_name " << this-> - phase_name << std::endl; + s_oss << indent1 << "-phase_name " << this->phase_name << std::endl; } if (this->rate_name != NULL) { - s_oss << indent0 << "-rate_name " << this-> - rate_name << std::endl; + s_oss << indent1 << "-rate_name " << this->rate_name << std::endl; } - s_oss << indent0 << "-phase_proportion " << this-> - phase_proportion << std::endl; - s_oss << indent0 << "-formula_z " << this-> - formula_z << std::endl; - - // totals - s_oss << indent0; - s_oss << "-totals" << std::endl; - this->totals.dump_raw(s_oss, indent + 1); + s_oss << indent1 << "-phase_proportion " << this->phase_proportion << std::endl; + s_oss << indent1 << "-formula_z " << this->formula_z << std::endl; // formula_totals - s_oss << indent0; + s_oss << indent1; s_oss << "-formula_totals" << std::endl; - this->formula_totals.dump_raw(s_oss, indent + 1); + this->formula_totals.dump_raw(s_oss, indent + 2); + } void -cxxExchComp::read_raw(CParser & parser) +cxxExchComp::read_raw(CParser & parser, bool check) { std::string str; @@ -496,36 +497,39 @@ cxxExchComp::read_raw(CParser & parser) if (opt == CParser::OPT_EOF || opt == CParser::OPT_KEYWORD) break; } - // members that must be defined - if (formula_defined == false) + if (check) { - parser.incr_input_error(); - parser.error_msg("Formula not defined for ExchComp input.", - CParser::OT_CONTINUE); - } - if (moles_defined == false) - { - parser.incr_input_error(); - parser.error_msg("Moles not defined for ExchComp input.", - CParser::OT_CONTINUE); - } - if (la_defined == false) - { - parser.incr_input_error(); - parser.error_msg("La not defined for ExchComp input.", - CParser::OT_CONTINUE); - } - if (charge_balance_defined == false) - { - parser.incr_input_error(); - parser.error_msg("Charge_balance not defined for ExchComp input.", - CParser::OT_CONTINUE); - } - if (formula_z_defined == false) - { - parser.incr_input_error(); - parser.error_msg("Formula_z not defined for ExchComp input.", - CParser::OT_CONTINUE); + // members that must be defined + if (formula_defined == false) + { + parser.incr_input_error(); + parser.error_msg("Formula not defined for ExchComp input.", + CParser::OT_CONTINUE); + } + if (moles_defined == false) + { + parser.incr_input_error(); + parser.error_msg("Moles not defined for ExchComp input.", + CParser::OT_CONTINUE); + } + if (la_defined == false) + { + parser.incr_input_error(); + parser.error_msg("La not defined for ExchComp input.", + CParser::OT_CONTINUE); + } + if (charge_balance_defined == false) + { + parser.incr_input_error(); + parser.error_msg("Charge_balance not defined for ExchComp input.", + CParser::OT_CONTINUE); + } + if (formula_z_defined == false) + { + parser.incr_input_error(); + parser.error_msg("Formula_z not defined for ExchComp input.", + CParser::OT_CONTINUE); + } } } void diff --git a/ExchComp.h b/ExchComp.h index 353b496a..2cb543d8 100644 --- a/ExchComp.h +++ b/ExchComp.h @@ -22,14 +22,14 @@ class cxxExchComp struct master *get_master(); - static struct exch_comp *cxxExchComp2exch_comp(std::list < cxxExchComp > + static struct exch_comp *cxxExchComp2exch_comp(std::map < std::string, cxxExchComp > &el); void dump_xml(std::ostream & os, unsigned int indent = 0) const; void dump_raw(std::ostream & s_oss, unsigned int indent) const; - void read_raw(CParser & parser); + void read_raw(CParser & parser, bool check=true); char *get_formula() const { diff --git a/Exchange.cxx b/Exchange.cxx index c16043a7..e16f6ecc 100644 --- a/Exchange.cxx +++ b/Exchange.cxx @@ -46,7 +46,8 @@ cxxNumKeyword() for (i = 0; i < exchange_ptr->count_comps; i++) { cxxExchComp ec(&(exchange_ptr->comps[i])); - exchComps.push_back(ec); + std::string str(ec.get_formula()); + exchComps[str] = ec; } @@ -160,7 +161,8 @@ cxxNumKeyword() ec.set_totals(elt_list, count_elts); // add to comp list - this->exchComps.push_back(ec); + std::string str(ec.get_formula()); + this->exchComps[str] = ec; } } } @@ -172,10 +174,10 @@ cxxExchange::~cxxExchange() bool cxxExchange::get_related_phases() { - for (std::list < cxxExchComp >::const_iterator it = + for (std::map < std::string, cxxExchComp >::const_iterator it = this->exchComps.begin(); it != this->exchComps.end(); ++it) { - if (it->get_phase_name() == NULL) + if ((*it).second.get_phase_name() == NULL) continue; return (true); } @@ -185,10 +187,10 @@ cxxExchange::get_related_phases() bool cxxExchange::get_related_rate() { - for (std::list < cxxExchComp >::const_iterator it = + for (std::map < std::string, cxxExchComp >::const_iterator it = this->exchComps.begin(); it != this->exchComps.end(); ++it) { - if (it->get_rate_name() == NULL) + if ((*it).second.get_rate_name() == NULL) continue; return (true); } @@ -213,8 +215,7 @@ cxxExchange::cxxExchange2exchange() exchange_ptr->related_rate = (int) this->get_related_rate(); exchange_ptr->pitzer_exchange_gammas = (int) this->pitzer_exchange_gammas; exchange_ptr->count_comps = (int) this->exchComps.size(); - exchange_ptr->comps = - (struct exch_comp *) free_check_null(exchange_ptr->comps); + exchange_ptr->comps = (struct exch_comp *) free_check_null(exchange_ptr->comps); exchange_ptr->comps = cxxExchComp::cxxExchComp2exch_comp(this->exchComps); return (exchange_ptr); } @@ -244,10 +245,10 @@ cxxExchange::dump_xml(std::ostream & s_oss, unsigned int indent) const // components s_oss << indent1; s_oss << "::const_iterator it = exchComps.begin(); + for (std::map < std::string, cxxExchComp >::const_iterator it = exchComps.begin(); it != exchComps.end(); ++it) { - it->dump_xml(s_oss, indent + 2); + (*it).second.dump_xml(s_oss, indent + 2); } return; @@ -277,12 +278,12 @@ cxxExchange::dump_raw(std::ostream & s_oss, unsigned int indent) const pitzer_exchange_gammas << std::endl; // exchComps structures - for (std::list < cxxExchComp >::const_iterator it = exchComps.begin(); + for (std::map < std::string, cxxExchComp >::const_iterator it = exchComps.begin(); it != exchComps.end(); ++it) { s_oss << indent1; s_oss << "-component" << std::endl; - it->dump_raw(s_oss, indent + 2); + (*it).second.dump_raw(s_oss, indent + 2); } return; @@ -353,8 +354,33 @@ cxxExchange::read_raw(CParser & parser, bool check) case 1: // component { cxxExchComp ec; - ec.read_raw(parser); - this->exchComps.push_back(ec); + + // preliminary read + std::istream::pos_type pos = parser.tellg(); + CParser::ECHO_OPTION eo = parser.get_echo_file(); + parser.set_echo_file(CParser::EO_NONE); + CParser::ECHO_OPTION eo_s = parser.get_echo_stream(); + parser.set_echo_stream(CParser::EO_NONE); + ec.read_raw(parser, false); + parser.set_echo_file(eo); + parser.set_echo_file(eo_s); + + if (this->exchComps.find(ec.get_formula()) != this->exchComps.end()) + { + cxxExchComp & comp = this->exchComps.find(ec.get_formula())->second; + parser.seekg(pos).clear(); + parser.seekg(pos); + comp.read_raw(parser, false); + } + else + { + parser.seekg(pos).clear(); + parser.seekg(pos); + cxxExchComp ec1; + ec1.read_raw(parser, false); + std::string str(ec1.get_formula()); + this->exchComps[str] = ec1; + } } useLastLine = true; break; @@ -375,6 +401,7 @@ cxxExchange::read_raw(CParser & parser, bool check) } } } +#ifdef SKIP void cxxExchange::add(const cxxExchange & addee, double extensive) // @@ -410,7 +437,48 @@ cxxExchange::add(const cxxExchange & addee, double extensive) //bool pitzer_exchange_gammas; this->pitzer_exchange_gammas = addee.pitzer_exchange_gammas; } - +#endif +void +cxxExchange::add(const cxxExchange & addee, double extensive) + // + // Add existing exchange to "this" exchange + // +{ + // exchComps + if (extensive == 0.0) + return; + for (std::map < std::string, cxxExchComp >::const_iterator itadd = + addee.exchComps.begin(); itadd != addee.exchComps.end(); ++itadd) + { + std::map < std::string, cxxExchComp >::iterator it = this->exchComps.find((*itadd).first); + if (it != this->exchComps.end()) + { + (*it).second.add((*itadd).second, extensive); + //bool found = false; + //for (std::list < cxxExchComp >::iterator it = this->exchComps.begin(); + // it != this->exchComps.end(); ++it) + //{ + // if (it->get_formula() == itadd->get_formula()) + // { + // it->add((*itadd), extensive); + // found = true; + // break; + // } + //} + /*if (!found)*/ + + } + else + { + cxxExchComp exc = (*itadd).second; + exc.multiply(extensive); + //exc.add(*itadd, extensive); + this->exchComps[(*itadd).first] = exc; + } + } + //bool pitzer_exchange_gammas; + this->pitzer_exchange_gammas = addee.pitzer_exchange_gammas; +} #ifdef USE_MPI /* ---------------------------------------------------------------------- */ void @@ -461,11 +529,11 @@ cxxExchange::totalize() { this->totals.clear(); // component structures - for (std::list < cxxExchComp >::const_iterator it = exchComps.begin(); + for (std::map < std::string, cxxExchComp >::const_iterator it = exchComps.begin(); it != exchComps.end(); ++it) { - this->totals.add_extensive(it->get_totals(), 1.0); - this->totals.add("Charge", it->get_charge_balance()); + this->totals.add_extensive((*it).second.get_totals(), 1.0); + this->totals.add("Charge", (*it).second.get_charge_balance()); } return; } diff --git a/Exchange.h b/Exchange.h index af23f7af..eab812e0 100644 --- a/Exchange.h +++ b/Exchange.h @@ -43,7 +43,7 @@ class cxxExchange:public cxxNumKeyword this->pitzer_exchange_gammas = b; } - std::list < cxxExchComp > &get_exchComps(void) + std::map < std::string, cxxExchComp > &get_exchComps(void) { return (this->exchComps); } @@ -64,7 +64,7 @@ class cxxExchange:public cxxNumKeyword void dump_xml(std::ostream & os, unsigned int indent = 0) const; protected: - std::list < cxxExchComp > exchComps; + std::map < std::string, cxxExchComp > exchComps; bool pitzer_exchange_gammas; cxxNameDouble totals; public: diff --git a/PPassemblage.cxx b/PPassemblage.cxx index 884672e0..4417c2a0 100644 --- a/PPassemblage.cxx +++ b/PPassemblage.cxx @@ -230,16 +230,21 @@ cxxPPassemblage::read_raw(CParser & parser, bool check) case 1: // component { - std::istream::pos_type pos = parser.tellg(); cxxPPassemblageComp ppComp; - CParser::ECHO_OPTION eo = parser.get_echo_stream(); + + // preliminary read + std::istream::pos_type pos = parser.tellg(); + CParser::ECHO_OPTION eo = parser.get_echo_file(); + parser.set_echo_file(CParser::EO_NONE); + CParser::ECHO_OPTION eo_s = parser.get_echo_stream(); parser.set_echo_stream(CParser::EO_NONE); ppComp.read_raw(parser, false); - parser.set_echo_stream(eo); + parser.set_echo_file(eo); + parser.set_echo_file(eo_s); if (this->ppAssemblageComps.find(ppComp.get_name()) != this->ppAssemblageComps.end()) { - cxxPPassemblageComp & comp = ppAssemblageComps.find(ppComp.get_name())->second; + cxxPPassemblageComp & comp = this->ppAssemblageComps.find(ppComp.get_name())->second; parser.seekg(pos).clear(); parser.seekg(pos); comp.read_raw(parser, false); diff --git a/ReadClass.cxx b/ReadClass.cxx index 5f248eae..bb45031b 100644 --- a/ReadClass.cxx +++ b/ReadClass.cxx @@ -373,8 +373,7 @@ read_equilibrium_phases_raw(void) cxxPPassemblage ex; ex.read_raw(parser); - struct pp_assemblage *pp_assemblage_ptr = - ex.cxxPPassemblage2pp_assemblage(); + struct pp_assemblage *pp_assemblage_ptr = ex.cxxPPassemblage2pp_assemblage(); int n; /* @@ -1250,6 +1249,8 @@ read_equilibrium_phases_modify(void) struct pp_assemblage *entity_ptr = entity.cxxPPassemblage2pp_assemblage(); pp_assemblage_free(&(pp_assemblage[n])); pp_assemblage_copy(entity_ptr, &(pp_assemblage[n]), entity_ptr->n_user); + free_check_null(pp_assemblage[n].description); + pp_assemblage[n].description = string_duplicate(entity_ptr->description); pp_assemblage_free(entity_ptr); free_check_null(entity_ptr); @@ -1276,6 +1277,25 @@ read_exchange_modify(void) * */ int return_value; + + // find exchange number + char token[MAX_LENGTH]; + char *next; + int l, n_user, n; + next = line; + copy_token(token, &next, &l); + if (copy_token(token, &next, &l) != DIGIT) + { + input_error++; + sprintf(error_string, "Expected exchange number following EXCHANGE_MODIFY.\n%s\n", line_save); + error_msg(error_string, CONTINUE); + return (ERROR); + } + else + { + sscanf(token,"%d", &n_user); + } + /* * Make parser */ @@ -1301,23 +1321,6 @@ read_exchange_modify(void) parser.set_echo_file(CParser::EO_NOKEYWORDS); } - // find exchange number - char token[MAX_LENGTH]; - char *next; - int l, n_user, n; - next = line; - copy_token(token, &next, &l); - if (copy_token(token, &next, &l) != DIGIT) - { - input_error++; - sprintf(error_string, "Expected exchange number following EXCHANGE_MODIFY.\n%s\n", line_save); - error_msg(error_string, CONTINUE); - return (ERROR); - } - else - { - sscanf(token,"%d", &n_user); - } if (exchange_bsearch(n_user, &n) == NULL) { input_error++; @@ -1334,7 +1337,11 @@ read_exchange_modify(void) struct exchange *entity_ptr = entity.cxxExchange2exchange(); exchange_free(&(exchange[n])); exchange_copy(entity_ptr, &(exchange[n]), entity_ptr->n_user); + free_check_null(exchange[n].description); + exchange[n].description = string_duplicate(entity_ptr->description); exchange_free(entity_ptr); + free_check_null(entity_ptr); + // Need to output the next keyword output_msg(OUTPUT_CHECKLINE, "\t%s\n", line); diff --git a/Solution.cxx b/Solution.cxx index 9cb70ca5..4ee96859 100644 --- a/Solution.cxx +++ b/Solution.cxx @@ -454,7 +454,7 @@ cxxSolution::dump_raw(std::ostream & s_oss, unsigned int indent) const s_oss << "SOLUTION_RAW " << this->n_user << " " << this-> description << std::endl; - s_oss << "# Critical values" << std::endl; + //s_oss << "# Critical values" << std::endl; s_oss << indent1; s_oss << "-temp " << this->tc << std::endl; @@ -476,7 +476,7 @@ cxxSolution::dump_raw(std::ostream & s_oss, unsigned int indent) const s_oss << "-totals" << std::endl; this->totals.dump_raw(s_oss, indent + 2); - s_oss << "# Optional critical values" << std::endl; + //s_oss << "# Optional critical values" << std::endl; // Isotopes s_oss << indent1; @@ -495,7 +495,7 @@ cxxSolution::dump_raw(std::ostream & s_oss, unsigned int indent) const } */ - s_oss << "# Non-critical values" << std::endl; + //s_oss << "# Non-critical values" << std::endl; s_oss << indent1; s_oss << "-pH " << this->ph << std::endl;