From 584b79bd5d6074ab0a02aaa04e4bc61fce3c2016 Mon Sep 17 00:00:00 2001 From: David L Parkhurst Date: Wed, 30 Nov 2011 16:50:52 +0000 Subject: [PATCH] roughed in cxxPressure class. A lot more to do. Added screen_msg and echo_msg to PHRQ_base. Added screen_msg and echo_msg to PHRQ_io Added enum to direct echo to log or output. Switched back to 3 digit exponent in mainsubs.cpp. git-svn-id: svn://136.177.114.72/svn_GW/phreeqcpp/branches/ErrorHandling@5833 1feff8c3-07ed-0310-ac33-dd36852eb9cd --- PHRQ_base.cxx | 26 ++++ PHRQ_base.h | 3 + PHRQ_io.cpp | 31 +++++ PHRQ_io.h | 20 +++ Phreeqc.h | 10 +- Pressure.cxx | 351 ++++++++++++++++++++++++++++++++++++++++++++++++++ Pressure.h | 40 ++++++ 7 files changed, 480 insertions(+), 1 deletion(-) create mode 100644 Pressure.cxx create mode 100644 Pressure.h diff --git a/PHRQ_base.cxx b/PHRQ_base.cxx index 71dc39ce..0c62e6e0 100644 --- a/PHRQ_base.cxx +++ b/PHRQ_base.cxx @@ -70,3 +70,29 @@ output_msg(const std::string & stdstr) std::cout << stdstr << std::endl; } } + +void PHRQ_base:: +screen_msg(const std::string & stdstr) +{ + if (this->io) + { + this->io->screen_msg(stdstr.c_str()); + } + else + { + std::cerr << stdstr << std::endl; + } +} + +void PHRQ_base:: +echo_msg(const std::string & stdstr) +{ + if (this->io) + { + this->io->echo_msg(stdstr.c_str()); + } + else + { + std::cout << stdstr << std::endl; + } +} \ No newline at end of file diff --git a/PHRQ_base.h b/PHRQ_base.h index c4c92ec8..4750cb13 100644 --- a/PHRQ_base.h +++ b/PHRQ_base.h @@ -17,6 +17,9 @@ public: void output_msg(const std::string &); void error_msg(const std::string &, int stop=0); void warning_msg(const std::string &); + void screen_msg(const std::string &); + void echo_msg(const std::string &); + void Set_io(PHRQ_io * p_io) { diff --git a/PHRQ_io.cpp b/PHRQ_io.cpp index 40554089..ee3c5cb6 100644 --- a/PHRQ_io.cpp +++ b/PHRQ_io.cpp @@ -22,6 +22,9 @@ PHRQ_io(void) punch_file_on = true; error_file_on = true; dump_file_on = true; + screen_on = true; + echo_on = true; + echo_destination = ECHO_OUTPUT; } PHRQ_io:: @@ -562,3 +565,31 @@ safe_close(FILE ** file_ptr) } } +/* ---------------------------------------------------------------------- */ +void PHRQ_io:: +screen_msg(const char * str) +/* ---------------------------------------------------------------------- */ +{ + if (screen_on) + { + fprintf(stderr, "%s", str); + } +} +/* ---------------------------------------------------------------------- */ +void PHRQ_io:: +echo_msg(const char * str) +/* ---------------------------------------------------------------------- */ +{ + if (echo_on) + { + switch (this->echo_destination) + { + case ECHO_LOG: + log_msg(str); + break; + case ECHO_OUTPUT: + output_msg(str); + break; + } + } +} \ No newline at end of file diff --git a/PHRQ_io.h b/PHRQ_io.h index 9f07a889..75c21a0b 100644 --- a/PHRQ_io.h +++ b/PHRQ_io.h @@ -90,6 +90,22 @@ public: bool Get_error_file_on(void) {return this->error_file_on;}; bool Get_dump_file_on(void) {return this->dump_file_on;}; + void screen_msg(const char * str); + void Set_screen_on(bool tf) {this->screen_on = tf;}; + bool Get_screen_on(void) {return this->screen_on;}; + + enum ECHO_OPTION + { + ECHO_LOG, + ECHO_OUTPUT + }; + void echo_msg(const char * str); + void Set_echo_on(bool tf) {this->echo_on = tf;}; + bool Get_echo_on(void) {return this->echo_on;}; + void Set_echo_destination(ECHO_OPTION eo) {this->echo_destination = eo;}; + ECHO_OPTION Get_echo_destination(void) {return this->echo_destination;}; + + // data protected: FILE *input_file; @@ -106,6 +122,10 @@ protected: bool punch_file_on; bool error_file_on; bool dump_file_on; + bool echo_on; + bool screen_on; + ECHO_OPTION echo_destination; + }; #endif /* _PHRQIO_H */ diff --git a/Phreeqc.h b/Phreeqc.h index 8c3aa4cc..e26d0bd2 100644 --- a/Phreeqc.h +++ b/Phreeqc.h @@ -37,6 +37,7 @@ class cxxExchange; class cxxExchComp; class cxxGasPhase; class cxxTemperature; +class cxxPressure; class cxxPPassemblage; class cxxPPassemblageComp; class cxxReaction; @@ -767,6 +768,7 @@ public: int read_reaction_steps(struct irrev *irrev_ptr); int read_solid_solutions(void); int read_temperature(void); + int read_reaction_pressure(void); int read_reaction_temps(struct temperature *temperature_ptr); int read_save(void); int read_selected_output(void); @@ -1388,7 +1390,7 @@ protected: Address Hash_multi(HashTable * Table, char *Key); void ExpandTable_multi(HashTable * Table); public: - bool recursive_include(std::ifstream & input_stream, std::iostream & accumulated_stream); + //bool recursive_include(std::ifstream & input_stream, std::iostream & accumulated_stream); int main_method(int argc, char *argv[]); void set_phast(int); size_t list_components(std::list &list_c); @@ -1424,6 +1426,12 @@ protected: struct temperature *temperature; int count_temperature; + + /* ---------------------------------------------------------------------- + * Pressures + * ---------------------------------------------------------------------- */ + std::map Reaction_pressure_map; + /* ---------------------------------------------------------------------- * Surface * --------------------------------------------------------------------- */ diff --git a/Pressure.cxx b/Pressure.cxx new file mode 100644 index 00000000..8971e0ff --- /dev/null +++ b/Pressure.cxx @@ -0,0 +1,351 @@ +// Pressure.cxx: implementation of the cxxPressure class. +// +////////////////////////////////////////////////////////////////////// +#ifdef _DEBUG +#pragma warning(disable : 4786) // disable truncation warning (Only used by debugger) +#endif +#include // assert +#include // std::sort + +#include "Utils.h" // define first +#include "Parser.h" +#include "Phreeqc.h" +#include "Pressure.h" +#include "phqalloc.h" + +////////////////////////////////////////////////////////////////////// +// Construction/Destruction +////////////////////////////////////////////////////////////////////// + +cxxPressure::cxxPressure(PHRQ_io *io) + // + // default constructor for cxxPressure + // +: cxxNumKeyword(io) +{ + count = 0; + equalIncrements = false; + pressures.push_back(1.0); +} + +cxxPressure::~cxxPressure() +{ +} +void +cxxPressure::read(CParser & parser) +{ + double d; + CParser::TOKEN_TYPE k; + static std::vector < std::string > vopts; + if (vopts.empty()) + { + vopts.reserve(5); + vopts.push_back("pressures"); //0 + vopts.push_back("equal_increments"); //1 + vopts.push_back("count"); //2 + } + + std::istream::pos_type ptr; + std::istream::pos_type next_char; + std::string token; + int opt_save; + bool useLastLine(false); + + // Read reaction_pressure number and description + this->read_number_description(parser); + + opt_save = CParser::OPT_ERROR; + bool equalIncrements_defined(false); + bool count_defined(false); + + for (;;) + { + int opt; + if (useLastLine == false) + { + opt = parser.get_option(vopts, next_char); + } + else + { + opt = parser.getOptionFromLastLine(vopts, next_char); + } + if (opt == CParser::OPT_DEFAULT) + { + opt = opt_save; + } + switch (opt) + { + case CParser::OPT_EOF: + break; + case CParser::OPT_KEYWORD: + break; + case CParser::OPT_DEFAULT: + case CParser::OPT_ERROR: + opt = CParser::OPT_EOF; + parser.error_msg("Unknown input in REACTION_PRESSURE_RAW keyword.", + CParser::OT_CONTINUE); + parser.error_msg(parser.line().c_str(), CParser::OT_CONTINUE); + useLastLine = false; + break; + + case 0: // pressures + while ((k = + parser.copy_token(token, next_char)) == CParser::TT_DIGIT) + { + std::istringstream iss(token); + if (!(iss >> d)) + { + parser.incr_input_error(); + parser.error_msg("Expected numeric value for pressures.", + CParser::OT_CONTINUE); + } + else + { + this->pressures.push_back(d); + } + } + opt_save = 0; + useLastLine = false; + break; + + case 1: // equal_increments + if (!(parser.get_iss() >> this->equalIncrements)) + { + this->equalIncrements = 0; + parser.incr_input_error(); + parser.error_msg("Expected boolean value for equalIncrements.", CParser::OT_CONTINUE); + } + opt_save = CParser::OPT_DEFAULT; + useLastLine = false; + equalIncrements_defined = true; + break; + + case 2: // count + if (!(parser.get_iss() >> this->count)) + { + this->count = 0; + parser.incr_input_error(); + parser.error_msg("Expected integer value for count.", CParser::OT_CONTINUE); + } + opt_save = CParser::OPT_DEFAULT; + useLastLine = false; + count_defined = true; + break; + } + if (opt == CParser::OPT_EOF || opt == CParser::OPT_KEYWORD) + break; + } + // members that must be defined + if (equalIncrements_defined == false) + { + parser.incr_input_error(); + parser.error_msg("Equal_increments not defined for REACTION_PRESSURE_RAW input.", + CParser::OT_CONTINUE); + } + if (count_defined == false) + { + parser.incr_input_error(); + parser.error_msg("Count_temps not defined for REACTION_PRESSURE_RAW input.", + CParser::OT_CONTINUE); + } +} + +void +cxxPressure::dump_raw(std::ostream & s_oss, unsigned int indent, int *n_out) const +{ + //const char ERR_MESSAGE[] = "Packing temperature message: %s, element not found\n"; + unsigned int i; + s_oss.precision(DBL_DIG - 1); + std::string indent0(""), indent1(""), indent2(""); + for (i = 0; i < indent; ++i) + indent0.append(Utilities::INDENT); + for (i = 0; i < indent + 1; ++i) + indent1.append(Utilities::INDENT); + for (i = 0; i < indent + 2; ++i) + indent2.append(Utilities::INDENT); + + s_oss << indent0; + int n_user_local = (n_out != NULL) ? *n_out : this->n_user; + s_oss << "REACTION_PRESSURE_RAW " << n_user_local << " " << this->description << std::endl; + + s_oss << indent1; + s_oss << "-count " << this->count << std::endl; + + s_oss << indent1; + s_oss << "-equal_increments " << this->equalIncrements << std::endl; + + // Temperature element and attributes + + s_oss << indent1; + s_oss << "-pressures " << std::endl; + { + int i = 0; + s_oss << indent2; + for (std::vector < double >::const_iterator it = this->pressures.begin(); + it != this->pressures.end(); it++) + { + if (i++ == 5) + { + s_oss << std::endl; + s_oss << indent2; + i = 0; + } + s_oss << *it << " "; + } + s_oss << std::endl; + } +} + +void +cxxPressure::read_raw(CParser & parser) +{ + double d; + CParser::TOKEN_TYPE k; + static std::vector < std::string > vopts; + if (vopts.empty()) + { + vopts.reserve(5); + vopts.push_back("pressures"); //0 + vopts.push_back("equal_increments"); //1 + vopts.push_back("count"); //2 + } + + std::istream::pos_type ptr; + std::istream::pos_type next_char; + std::string token; + int opt_save; + bool useLastLine(false); + + // Read reaction_pressure number and description + this->read_number_description(parser); + + opt_save = CParser::OPT_ERROR; + bool equalIncrements_defined(false); + bool count_defined(false); + + for (;;) + { + int opt; + if (useLastLine == false) + { + opt = parser.get_option(vopts, next_char); + } + else + { + opt = parser.getOptionFromLastLine(vopts, next_char); + } + if (opt == CParser::OPT_DEFAULT) + { + opt = opt_save; + } + switch (opt) + { + case CParser::OPT_EOF: + break; + case CParser::OPT_KEYWORD: + break; + case CParser::OPT_DEFAULT: + case CParser::OPT_ERROR: + opt = CParser::OPT_EOF; + parser.error_msg("Unknown input in REACTION_PRESSURE_RAW keyword.", + CParser::OT_CONTINUE); + parser.error_msg(parser.line().c_str(), CParser::OT_CONTINUE); + useLastLine = false; + break; + + case 0: // pressures + while ((k = + parser.copy_token(token, next_char)) == CParser::TT_DIGIT) + { + std::istringstream iss(token); + if (!(iss >> d)) + { + parser.incr_input_error(); + parser.error_msg("Expected numeric value for pressures.", + CParser::OT_CONTINUE); + } + else + { + this->pressures.push_back(d); + } + } + opt_save = 0; + useLastLine = false; + break; + + case 1: // equal_increments + if (!(parser.get_iss() >> this->equalIncrements)) + { + this->equalIncrements = 0; + parser.incr_input_error(); + parser.error_msg("Expected boolean value for equalIncrements.", CParser::OT_CONTINUE); + } + opt_save = CParser::OPT_DEFAULT; + useLastLine = false; + equalIncrements_defined = true; + break; + + case 2: // count + if (!(parser.get_iss() >> this->count)) + { + this->count = 0; + parser.incr_input_error(); + parser.error_msg("Expected integer value for count.", CParser::OT_CONTINUE); + } + opt_save = CParser::OPT_DEFAULT; + useLastLine = false; + count_defined = true; + break; + } + if (opt == CParser::OPT_EOF || opt == CParser::OPT_KEYWORD) + break; + } + // members that must be defined + if (equalIncrements_defined == false) + { + parser.incr_input_error(); + parser.error_msg("Equal_increments not defined for REACTION_PRESSURE_RAW input.", + CParser::OT_CONTINUE); + } + if (count_defined == false) + { + parser.incr_input_error(); + parser.error_msg("Count_temps not defined for REACTION_PRESSURE_RAW input.", + CParser::OT_CONTINUE); + } +} +#ifdef SKIP +void +cxxPressure::dump_xml(std::ostream & s_oss, unsigned int indent) const const +{ + //const char ERR_MESSAGE[] = "Packing temperature message: %s, element not found\n"; + unsigned int i; + s_oss.precision(DBL_DIG - 1); + std::string indent0(""), indent1(""), indent2(""); + for (i = 0; i < indent; ++i) + indent0.append(Utilities::INDENT); + for (i = 0; i < indent + 1; ++i) + indent1.append(Utilities::INDENT); + for (i = 0; i < indent + 2; ++i) + indent2.append(Utilities::INDENT); + + // Temperature element and attributes + s_oss << indent0; + s_oss << " + pitzer_temperature_gammas << "\"" << std::endl; + + // components + s_oss << indent1; + s_oss << "::const_iterator it = + temperatureComps.begin(); it != temperatureComps.end(); ++it) + { + it->dump_xml(s_oss, indent + 2); + } + + return; +} +#endif \ No newline at end of file diff --git a/Pressure.h b/Pressure.h new file mode 100644 index 00000000..7adeb51a --- /dev/null +++ b/Pressure.h @@ -0,0 +1,40 @@ +#if !defined(PRESSURE_H_INCLUDED) +#define PRESSURE_H_INCLUDED + +#include // assert +#include // std::map +#include // std::string +#include // std::list +#include // std::vector + +#include "NumKeyword.h" + +class cxxPressure:public cxxNumKeyword +{ + + public: + cxxPressure(PHRQ_io *io=NULL); + cxxPressure(struct temperature *, PHRQ_io *io=NULL); + ~cxxPressure(); + + //void dump_xml(std::ostream& os, unsigned int indent = 0)const; + + void dump_raw(std::ostream & s_oss, unsigned int indent, int *n_out=NULL) const; + + void read(CParser & parser); + void read_raw(CParser & parser); + + std::vector & Get_pressures(void) {return pressures;}; + int Get_count(void) const {return count;}; + void Set_count(int i) {count = i;}; + bool Get_equalIncrements(void) const {return equalIncrements;}; + void Set_equalIncrements(bool tf) {equalIncrements = tf;}; + +protected: + std::vector < double >pressures; + int count; + bool equalIncrements; + +}; + +#endif // !defined(PRESSURE_H_INCLUDED)