From d82d5d610f7cdb91ab4258b98aeebc844b60cd11 Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Tue, 23 Mar 2021 19:24:09 -0600 Subject: [PATCH 01/53] vector llnl parameters, removed hash references --- PBasic.cpp | 6 +- Phreeqc.cpp | 66 +--------- Phreeqc.h | 10 +- basicsubs.cpp | 2 +- global_structures.h | 11 +- isotopes.cpp | 6 +- mainsubs.cpp | 20 --- model.cpp | 22 ++-- prep.cpp | 2 +- print.cpp | 2 +- read.cpp | 300 +++++++++++++------------------------------- structures.cpp | 24 ++-- utilities.cpp | 37 +----- 13 files changed, 125 insertions(+), 383 deletions(-) diff --git a/PBasic.cpp b/PBasic.cpp index 84744807..515712b2 100644 --- a/PBasic.cpp +++ b/PBasic.cpp @@ -723,7 +723,7 @@ parse(char * l_inbuf, tokenrec ** l_buf) * Note: Modification of string length may translate incorrectly [146] */ /* - * Search hash list + * Search list */ PhreeqcPtr->str_tolower(token); std::map::const_iterator item; @@ -2267,7 +2267,7 @@ factor(struct LOC_exec * LINK) case tokdh_a: { - if (PhreeqcPtr->llnl_count_temp > 0) + if (PhreeqcPtr->llnl_temp.size() > 0) { n.UU.val = PhreeqcPtr->a_llnl; } @@ -2293,7 +2293,7 @@ factor(struct LOC_exec * LINK) case tokdh_b: { - if (PhreeqcPtr->llnl_count_temp > 0) + if (PhreeqcPtr->llnl_temp.size() > 0) { n.UU.val = PhreeqcPtr->b_llnl; } diff --git a/Phreeqc.cpp b/Phreeqc.cpp index dc6829a6..afb3cd6d 100644 --- a/Phreeqc.cpp +++ b/Phreeqc.cpp @@ -889,16 +889,6 @@ void Phreeqc::init(void) count_total_steps = 0; phast = FALSE; output_newline = true; - llnl_temp = 0; - llnl_count_temp = 0; - llnl_adh = 0; - llnl_count_adh = 0; - llnl_bdh = 0; - llnl_count_bdh = 0; - llnl_bdot = 0; - llnl_count_bdot = 0; - llnl_co2_coefs = 0; - llnl_count_co2_coefs = 0; //selected_output_file_name = NULL; dump_file_name = NULL; remove_unstable_phases = FALSE; @@ -1985,46 +1975,11 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) llnl_co2_coefs = 0; llnl_count_co2_coefs = 0; */ - llnl_count_temp = pSrc->llnl_count_temp; - if (llnl_count_temp > 0) - { - llnl_temp = (LDBLE *) free_check_null(llnl_temp); - llnl_temp = (LDBLE *) PHRQ_malloc((size_t) llnl_count_temp * sizeof(LDBLE)); - if (llnl_temp == NULL) malloc_error(); - memcpy(llnl_temp, pSrc->llnl_temp, (size_t) llnl_count_temp * sizeof(LDBLE)); - } - llnl_count_adh = pSrc->llnl_count_adh; - if (llnl_count_adh > 0) - { - llnl_adh = (LDBLE *) free_check_null(llnl_adh); - llnl_adh = (LDBLE *) PHRQ_malloc((size_t) llnl_count_adh * sizeof(LDBLE)); - if (llnl_adh == NULL) malloc_error(); - memcpy(llnl_adh, pSrc->llnl_adh, (size_t) llnl_count_adh * sizeof(LDBLE)); - } - llnl_count_bdh = pSrc->llnl_count_bdh; - if (llnl_count_bdh > 0) - { - llnl_bdh = (LDBLE *) free_check_null(llnl_bdh); - llnl_bdh = (LDBLE *) PHRQ_malloc((size_t) llnl_count_bdh * sizeof(LDBLE)); - if (llnl_bdh == NULL) malloc_error(); - memcpy(llnl_bdh, pSrc->llnl_bdh, (size_t) llnl_count_bdh * sizeof(LDBLE)); - } - llnl_count_bdot = pSrc->llnl_count_bdot; - if (llnl_count_bdot > 0) - { - llnl_bdot = (LDBLE *) free_check_null(llnl_bdot); - llnl_bdot = (LDBLE *) PHRQ_malloc((size_t) llnl_count_bdot * sizeof(LDBLE)); - if (llnl_bdot == NULL) malloc_error(); - memcpy(llnl_bdot, pSrc->llnl_bdot, (size_t) llnl_count_bdot * sizeof(LDBLE)); - } - llnl_count_co2_coefs = pSrc->llnl_count_co2_coefs; - if (llnl_count_co2_coefs > 0) - { - llnl_co2_coefs = (LDBLE *) free_check_null(llnl_co2_coefs); - llnl_co2_coefs = (LDBLE *) PHRQ_malloc((size_t) llnl_count_co2_coefs * sizeof(LDBLE)); - if (llnl_co2_coefs == NULL) malloc_error(); - memcpy(llnl_co2_coefs, pSrc->llnl_co2_coefs, (size_t) llnl_count_co2_coefs * sizeof(LDBLE)); - } + llnl_temp = pSrc->llnl_temp; + llnl_adh = pSrc->llnl_adh; + llnl_bdh = pSrc->llnl_bdh; + llnl_bdot = pSrc->llnl_bdot; + llnl_co2_coefs = pSrc->llnl_co2_coefs; // Not implemented for now SelectedOutput_map = pSrc->SelectedOutput_map; @@ -2057,17 +2012,6 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) //remove_unstable_phases = FALSE; // auto screen_string; spread_length = 10; - /* ---------------------------------------------------------------------- */ - /* - * Hash definitions - */ - /* - elements_hash_table = NULL; - species_hash_table = NULL; - phases_hash_table = NULL; - logk_hash_table = NULL; - master_isotope_hash_table = NULL; - */ /* ---------------------------------------------------------------------- * ISOTOPES * ---------------------------------------------------------------------- */ diff --git a/Phreeqc.h b/Phreeqc.h index 195abfb3..66328b9c 100644 --- a/Phreeqc.h +++ b/Phreeqc.h @@ -721,11 +721,6 @@ public: int read_inv_isotopes(struct inverse *inverse_ptr, char *ptr); int read_inv_phases(struct inverse *inverse_ptr, char *next_char); int read_kinetics(void); - int read_line_doubles(char *next_char, LDBLE ** d, int *count_d, - int *count_alloc); - int read_lines_doubles(char *next_char, LDBLE ** d, int *count_d, - int *count_alloc, const char **opt_list, - int count_opt_list, int *opt); LDBLE *read_list_doubles(char **ptr, int *count_doubles); int *read_list_ints(char **ptr, int *count_ints, int positive); int *read_list_t_f(char **ptr, int *count_ints); @@ -1629,9 +1624,8 @@ protected: bool output_newline; inline void Set_output_newline(bool tf) { this->output_newline = tf;} inline bool Get_output_newline() { return this->output_newline;} - LDBLE *llnl_temp, *llnl_adh, *llnl_bdh, *llnl_bdot, *llnl_co2_coefs, a_llnl, b_llnl, bdot_llnl; - int llnl_count_temp, llnl_count_adh, llnl_count_bdh, llnl_count_bdot, - llnl_count_co2_coefs; + double a_llnl, b_llnl, bdot_llnl; + std::vector llnl_temp, llnl_adh, llnl_bdh, llnl_bdot, llnl_co2_coefs; //char *selected_output_file_name; std::map SelectedOutput_map; diff --git a/basicsubs.cpp b/basicsubs.cpp index 9ac4f92e..4a80889b 100644 --- a/basicsubs.cpp +++ b/basicsubs.cpp @@ -722,7 +722,7 @@ dh_bdot(const char* name) char token[MAX_LENGTH]; struct species* s_ptr; double b = -999.99; - if (llnl_count_temp > 0) + if (llnl_temp.size() > 0) { b = bdot_llnl; } diff --git a/global_structures.h b/global_structures.h index 13f0785f..8fdebcb2 100644 --- a/global_structures.h +++ b/global_structures.h @@ -149,16 +149,7 @@ #endif #define REF_PRES_PASCAL 1.01325E5 /* Reference pressure: 1 atm */ #define MAX_P_NONLLNL 1500.0 -/* - * Hash definitions - */ -# define SegmentSize 256 -# define SegmentSizeShift 8 /* log2(SegmentSize) */ -# define DirectorySize 256 -# define DirectorySizeShift 8 /* log2(DirectorySize) */ -# define Prime1 37 -# define Prime2 1048583 -# define DefaultMaxLoadFactor 5 + // // Typedefs and structure definitions // diff --git a/isotopes.cpp b/isotopes.cpp index a3d056a6..751eaeb9 100644 --- a/isotopes.cpp +++ b/isotopes.cpp @@ -1304,11 +1304,11 @@ master_isotope_store(const char *name, int replace_if_found) /* ---------------------------------------------------------------------- */ { /* - * Function locates the string "name" in the hash table for master_isotope. + * Function locates the string "name" in the map for master_isotope. * * Pointer to a master_isotope structure is always returned. * - * If the string is not found, a new entry is made in the hash table. Pointer to + * If the string is not found, a new entry is made in the map. Pointer to * the new structure is returned. * If "name" is found and replace is true, pointers in old master_isotope structure * are freed and replaced with additional input. @@ -1607,7 +1607,7 @@ isotope_ratio_store(const char *name_in, int replace_if_found) * * Pointer to a isotope_ratio structure is always returned. * - * If the string is not found, a new entry is made in the hash table. Pointer to + * If the string is not found, a new entry is made in the map. Pointer to * the new structure is returned. * If "name" is found and replace is true, pointers in old isotope_ratio structure * are freed and replaced with additional input. diff --git a/mainsubs.cpp b/mainsubs.cpp index 63268e7d..ec53940a 100644 --- a/mainsubs.cpp +++ b/mainsubs.cpp @@ -71,26 +71,6 @@ initialize(void) Initialize llnl aqueous model parameters */ a_llnl = b_llnl = 0.0; - llnl_temp = (LDBLE *) PHRQ_malloc(sizeof(LDBLE)); - if (llnl_temp == NULL) - malloc_error(); - llnl_count_temp = 0; - llnl_adh = (LDBLE *) PHRQ_malloc(sizeof(LDBLE)); - if (llnl_adh == NULL) - malloc_error(); - llnl_count_adh = 0; - llnl_bdh = (LDBLE *) PHRQ_malloc(sizeof(LDBLE)); - if (llnl_bdh == NULL) - malloc_error(); - llnl_count_bdh = 0; - llnl_bdot = (LDBLE *) PHRQ_malloc(sizeof(LDBLE)); - if (llnl_bdot == NULL) - malloc_error(); - llnl_count_bdot = 0; - llnl_co2_coefs = (LDBLE *) PHRQ_malloc(sizeof(LDBLE)); - if (llnl_co2_coefs == NULL) - malloc_error(); - llnl_count_co2_coefs = 0; // new PBasic basic_interpreter = new PBasic(this, phrq_io); // allocate one change_surf diff --git a/model.cpp b/model.cpp index bae9a09a..ee8d6e0d 100644 --- a/model.cpp +++ b/model.cpp @@ -53,7 +53,7 @@ model(void) input_error++; error_msg("Cannot use PITZER and SIT data blocks in same run (database + input file).", STOP); } - if ((pitzer_model == TRUE || sit_model == TRUE) && llnl_count_temp >0) + if ((pitzer_model == TRUE || sit_model == TRUE) && llnl_temp.size() > 0) { input_error++; error_msg("Cannot use LLNL_AQUEOUS_MODEL_PARAMETERS with PITZER or SIT data blocks in same run (database + input file).", STOP); @@ -570,17 +570,17 @@ gammas(LDBLE mu) /* * LLNL temperature dependence */ - if (llnl_count_temp > 0) + if (llnl_temp.size() > 0) { ifirst = 0; - ilast = llnl_count_temp; - if (tc_x < llnl_temp[0] || tc_x > llnl_temp[llnl_count_temp - 1]) + ilast = (int)llnl_temp.size(); + if (tc_x < llnl_temp[0] || tc_x > llnl_temp[llnl_temp.size() - 1]) { error_msg ("Temperature out of range of LLNL_AQUEOUS_MODEL parameters", STOP); } - for (i = 0; i < llnl_count_temp; i++) + for (i = 0; i < (int)llnl_temp.size(); i++) { if (tc_x >= llnl_temp[i]) ifirst = i; @@ -629,7 +629,7 @@ gammas(LDBLE mu) (2 * muhalf * (muhalf + 1.0) * (muhalf + 1.0)) - 0.3); c2 = -a / (2 * muhalf); - if (llnl_count_temp > 0) + if (llnl_temp.size() > 0) { c2_llnl = -a_llnl / (2 * muhalf); } @@ -725,7 +725,7 @@ gammas(LDBLE mu) } else if (s_x[i]->exch_gflag == 7 && s_x[i]->alk > 0) { - if (llnl_count_temp > 0) + if (llnl_temp.size() > 0) { s_x[i]->lg = coef * (-a_llnl * muhalf * z * z / @@ -805,7 +805,7 @@ gammas(LDBLE mu) } break; case 7: /* LLNL */ - if (llnl_count_temp > 0) + if (llnl_temp.size() > 0) { if (s_x[i]->z == 0) { @@ -834,7 +834,7 @@ gammas(LDBLE mu) } break; case 8: /* LLNL CO2 */ - if (llnl_count_temp > 0) + if (llnl_temp.size() > 0) { s_x[i]->lg = log_g_co2; s_x[i]->dg = dln_g_co2 * s_x[i]->moles; @@ -2690,7 +2690,7 @@ calc_gas_pressures(void) * Fixed-volume gas phase reacting with a solution * Change pressure used in logK to pressure of gas phase */ - if (gas_phase_ptr->Get_total_p() > MAX_P_NONLLNL && llnl_count_temp <= 0) + if (gas_phase_ptr->Get_total_p() > MAX_P_NONLLNL && llnl_temp.size() == 0) { gas_phase_ptr->Set_total_moles(0); for (size_t i = 0; i < gas_phase_ptr->Get_gas_comps().size(); i++) @@ -3686,7 +3686,7 @@ reset(void) //{ // patm_x = ( 1 * patm_x + p_sat) / 2.0; //} - if (llnl_count_temp <= 0) + if (llnl_temp.size() == 0) { if (patm_x > MAX_P_NONLLNL) patm_x = MAX_P_NONLLNL; diff --git a/prep.cpp b/prep.cpp index cf077f4a..0bfb091e 100644 --- a/prep.cpp +++ b/prep.cpp @@ -5480,7 +5480,7 @@ calc_vm(LDBLE tc, LDBLE pa) * b4 = logk[vmi4], or * coef(tc) = millero[3] + millero[4] * tc + millero[5] * tc^2 */ - if (llnl_count_temp > 0) return OK; + if (llnl_temp.size() > 0) return OK; LDBLE pb_s = 2600. + pa * 1.01325, TK_s = tc + 45.15, sqrt_mu = sqrt(mu_x); for (int i = 0; i < (int)this->s_x.size(); i++) { diff --git a/print.cpp b/print.cpp index 7051502a..617dbcfe 100644 --- a/print.cpp +++ b/print.cpp @@ -597,7 +597,7 @@ print_gas_phase(void) print_centered("Gas phase"); output_msg(sformatf("Total pressure: %5.2f atmospheres", (double) gas_phase_ptr->Get_total_p())); - if (gas_phase_ptr->Get_total_p() >= MAX_P_NONLLNL && llnl_count_temp <= 0) + if (gas_phase_ptr->Get_total_p() >= MAX_P_NONLLNL && llnl_temp.size() == 0) output_msg(" WARNING: Program limit.\n"); else if (PR) output_msg(" (Peng-Robinson calculation)\n"); diff --git a/read.cpp b/read.cpp index 18948350..6e813c2a 100644 --- a/read.cpp +++ b/read.cpp @@ -9243,25 +9243,22 @@ int Phreeqc:: read_llnl_aqueous_model_parameters(void) /* ---------------------------------------------------------------------- */ { -/* - * Reads aqueous model parameters - * - * Arguments: - * none - * - * Returns: - * KEYWORD if keyword encountered, input_error may be incremented if - * a keyword is encountered in an unexpected position - * EOF if eof encountered while reading mass balance concentrations - * ERROR if error occurred reading data - * - */ - int i, count_alloc; - char token[MAX_LENGTH]; - + /* + * Reads aqueous model parameters + * + * Arguments: + * none + * + * Returns: + * KEYWORD if keyword encountered, input_error may be incremented if + * a keyword is encountered in an unexpected position + * EOF if eof encountered while reading mass balance concentrations + * ERROR if error occurred reading data + * + */ int return_value, opt; - char *next_char; - const char *opt_list[] = { + char* next_char; + const char* opt_list[] = { "temperatures", /* 0 */ "temperature", /* 1 */ "temp", /* 2 */ @@ -9277,21 +9274,23 @@ read_llnl_aqueous_model_parameters(void) "co2_coefs" /* 12 */ }; int count_opt_list = 13; -/* - * Initialize - */ -/* - * Read aqueous model parameters - */ + /* + * Initialize + */ + /* + * Read aqueous model parameters + */ return_value = UNKNOWN; - opt = get_option(opt_list, count_opt_list, &next_char); + int opt_save = OPTION_DEFAULT; + opt_save = OPTION_DEFAULT; for (;;) { - next_char = line; - if (opt >= 0) + opt = get_option(opt_list, count_opt_list, &next_char); + if (opt == OPTION_DEFAULT) { - copy_token(token, &next_char, &i); + opt = opt_save; } + opt_save = OPTION_DEFAULT; switch (opt) { case OPTION_EOF: /* end of file */ @@ -9304,112 +9303,98 @@ read_llnl_aqueous_model_parameters(void) case OPTION_ERROR: input_error++; error_msg - ("Unknown input in LLNL_AQUEOUS_MODEL_PARAMETERS keyword.", - CONTINUE); + ("Unknown input in LLNL_AQUEOUS_MODEL_PARAMETERS keyword.", + CONTINUE); error_msg(line_save, CONTINUE); break; - -/* - * New component - */ case 0: /* temperatures */ case 1: /* temperature */ case 2: /* temp */ - count_alloc = 1; - llnl_count_temp = 0; - i = read_lines_doubles(next_char, &(llnl_temp), - &(llnl_count_temp), &(count_alloc), - opt_list, count_opt_list, &opt); - /* - ptr = next_char; - llnl_temp = read_list_doubles(&ptr, &count); - llnl_count_temp = count; - */ - break; + { + std::istringstream iss(next_char); + while (iss >> dummy) + { + llnl_temp.push_back(dummy); + } + opt_save = 2; + } + break; case 3: /* adh */ case 4: /* debye_huckel_a */ case 5: /* dh_a */ - count_alloc = 1; - llnl_count_adh = 0; - i = read_lines_doubles(next_char, &(llnl_adh), &(llnl_count_adh), - &(count_alloc), opt_list, count_opt_list, - &opt); - /* - ptr = next_char; - llnl_adh = read_list_doubles(&ptr, &count); - llnl_count_adh = count; - */ - break; + { + std::istringstream iss(next_char); + while (iss >> dummy) + { + llnl_adh.push_back(dummy); + } + opt_save = 5; + } + break; case 6: /* bdh */ case 7: /* debye_huckel_b */ case 8: /* dh_b */ - count_alloc = 1; - llnl_count_bdh = 0; - i = read_lines_doubles(next_char, &(llnl_bdh), &(llnl_count_bdh), - &(count_alloc), opt_list, count_opt_list, - &opt); - /* - ptr = next_char; - llnl_bdh = read_list_doubles(&ptr, &count); - llnl_count_bdh = count; - */ - break; + { + std::istringstream iss(next_char); + while (iss >> dummy) + { + llnl_bdh.push_back(dummy); + } + opt_save = 8; + } + break; case 9: /* bdot */ case 10: /* b_dot */ - count_alloc = 1; - llnl_count_bdot = 0; - i = read_lines_doubles(next_char, &(llnl_bdot), - &(llnl_count_bdot), &(count_alloc), - opt_list, count_opt_list, &opt); - /* - ptr = next_char; - llnl_bdot = read_list_doubles(&ptr, &count); - llnl_count_bdot = count; - */ - break; + { + std::istringstream iss(next_char); + while (iss >> dummy) + { + llnl_bdot.push_back(dummy); + } + opt_save = 10; + } + break; case 11: /* c_co2 */ case 12: /* co2_coefs */ - count_alloc = 1; - llnl_count_co2_coefs = 0; - i = read_lines_doubles(next_char, &(llnl_co2_coefs), - &(llnl_count_co2_coefs), &(count_alloc), - opt_list, count_opt_list, &opt); - /* - ptr = next_char; - llnl_co2_coefs = read_list_doubles(&ptr, &count); - llnl_count_co2_coefs = count; - */ - break; + { + std::istringstream iss(next_char); + while (iss >> dummy) + { + llnl_co2_coefs.push_back(dummy); + } + opt_save = 12; + } + break; } return_value = check_line_return; if (return_value == EOF || return_value == KEYWORD) break; } /* check consistency */ - if ((llnl_count_temp <= 0) || - (llnl_count_temp != llnl_count_adh) || - (llnl_count_temp != llnl_count_bdh) || - (llnl_count_temp != llnl_count_bdot)) + if ((llnl_temp.size() == 0) || + (llnl_temp.size() != llnl_adh.size()) || + (llnl_temp.size() != llnl_bdh.size()) || + (llnl_temp.size() != llnl_bdot.size())) { error_msg - ("Must define equal number (>0) of temperatures, dh_a, dh_b, and bdot parameters\nin LLNL_AQUEOUS_MODEL", - CONTINUE); + ("Must define equal number (>0) of temperatures, dh_a, dh_b, and bdot parameters\nin LLNL_AQUEOUS_MODEL", + CONTINUE); input_error++; } - if (llnl_count_co2_coefs != 5) + if (llnl_co2_coefs.size() != 5) { error_msg - ("Must define 5 CO2 activity coefficient parameters in LLNL_AQUEOUS_MODEL", - CONTINUE); + ("Must define 5 CO2 activity coefficient parameters in LLNL_AQUEOUS_MODEL", + CONTINUE); input_error++; } - for (i = 1; i < llnl_count_temp; i++) + for (size_t i = 1; i < llnl_temp.size(); i++) { if (llnl_temp[i - 1] > llnl_temp[i]) { error_msg - ("Temperatures must be in ascending order in LLNL_AQUEOUS_MODEL", - CONTINUE); + ("Temperatures must be in ascending order in LLNL_AQUEOUS_MODEL", + CONTINUE); input_error++; } } @@ -9417,119 +9402,6 @@ read_llnl_aqueous_model_parameters(void) return (return_value); } -/* ---------------------------------------------------------------------- */ -int Phreeqc:: -read_lines_doubles(char *next_char, LDBLE ** d, int *count_d, - int *count_alloc, const char **opt_list, - int count_opt_list, int *opt) -/* ---------------------------------------------------------------------- */ -{ -/* - * Reads LDBLEs on line starting at next_char - * and on succeeding lines. Appends to d. - * Stops at KEYWORD, OPTION, and EOF - * - * Input Arguments: - * next_char points to line to read from - * d points to array of LDBLEs, must be malloced - * count_d number of elements in array - * count_alloc number of elements malloced - * - * Output Arguments: - * d points to array of LDBLEs, may have been - * realloced - * count_d updated number of elements in array - * count_alloc updated of elements malloced - * - * Returns: - * KEYWORD - * OPTION - * EOF - * ERROR if any errors reading LDBLEs - */ - - if (read_line_doubles(next_char, d, count_d, count_alloc) == ERROR) - { - return (ERROR); - } - for (;;) - { - *opt = get_option(opt_list, count_opt_list, &next_char); - if (*opt == OPTION_KEYWORD || *opt == OPTION_EOF - || *opt == OPTION_ERROR) - { - break; - } - else if (*opt >= 0) - { - break; - } - next_char = line; - if (read_line_doubles(next_char, d, count_d, count_alloc) == ERROR) - { - return (ERROR); - } - } - return (OK); -} - -/* ---------------------------------------------------------------------- */ -int Phreeqc:: -read_line_doubles(char *next_char, LDBLE ** d, int *count_d, int *count_alloc) -/* ---------------------------------------------------------------------- */ -{ - int i, j, l, n; - LDBLE value; - char token[MAX_LENGTH]; - - for (;;) - { - j = copy_token(token, &next_char, &l); - if (j == EMPTY) - { - break; - } - if (j != DIGIT) - { - return (ERROR); - } - if (replace("*", " ", token) == TRUE) - { - if (sscanf(token, "%d" SCANFORMAT, &n, &value) != 2) - { - return (ERROR); - } - } - else - { - (void)sscanf(token, SCANFORMAT, &value); - n = 1; - } - for (;;) - { - if ((*count_d) + n > (*count_alloc)) - { - *count_alloc *= 2; - *d = (LDBLE *) PHRQ_realloc(*d, - (size_t) (*count_alloc) * - sizeof(LDBLE)); - if (*d == NULL) - malloc_error(); - } - else - { - break; - } - } - for (i = 0; i < n; i++) - { - (*d)[(*count_d) + i] = value; - } - *count_d += n; - } - return (OK); -} - /* ---------------------------------------------------------------------- */ int Phreeqc:: next_keyword_or_option(const char **opt_list, int count_opt_list) diff --git a/structures.cpp b/structures.cpp index a7bbdb3f..90da7207 100644 --- a/structures.cpp +++ b/structures.cpp @@ -137,7 +137,7 @@ clean_up(void) rate_free(&rates[j]); } rates.clear(); - /* logk hash table */ + /* logk table */ for (j = 0; j < (int)logk.size(); j++) { logk[j]->add_logk.clear(); @@ -168,13 +168,13 @@ clean_up(void) rate_free(user_print); user_print = (struct rate*)free_check_null(user_print); /* - Free llnl aqueous model parameters + Clear llnl aqueous model parameters */ - llnl_temp = (LDBLE*)free_check_null(llnl_temp); - llnl_adh = (LDBLE*)free_check_null(llnl_adh); - llnl_bdh = (LDBLE*)free_check_null(llnl_bdh); - llnl_bdot = (LDBLE*)free_check_null(llnl_bdot); - llnl_co2_coefs = (LDBLE*)free_check_null(llnl_co2_coefs); + llnl_temp.clear(); + llnl_adh.clear(); + llnl_bdh.clear(); + llnl_bdot.clear(); + llnl_co2_coefs.clear(); /* * Copier space */ @@ -226,7 +226,7 @@ clean_up(void) pitzer_clean_up(); /* sit */ sit_clean_up(); - /* hash tables */ + /* elements, species, phases*/ elements_map.clear(); species_map.clear(); phases_map.clear(); @@ -253,12 +253,6 @@ clean_up(void) last_title_x.clear(); count_inverse = 0; - llnl_count_temp = 0; - llnl_count_adh = 0; - llnl_count_bdh = 0; - llnl_count_bdot = 0; - llnl_count_co2_coefs = 0; - default_data_base = (char *) free_check_null(default_data_base); sformatf_buffer = (char *) free_check_null(sformatf_buffer); return (OK); @@ -3145,7 +3139,7 @@ logk_search(const char *name_in) /* ---------------------------------------------------------------------- */ { /* - * Function locates the string "name" in the hash table for logk. + * Function locates the string "name" in the map for logk. * * Arguments: * name input, character string to be found in "logk". diff --git a/utilities.cpp b/utilities.cpp index 9de59c8f..e43125e9 100644 --- a/utilities.cpp +++ b/utilities.cpp @@ -163,7 +163,7 @@ calc_rho_0(LDBLE tc, LDBLE pa) Wagner and Pruss, 2002, JPCRD 31, 387, eqn. 2.6, along the saturation pressure line + interpolation 0 - 300 oC, 0.006 - 1000 atm... */ - if (llnl_count_temp > 0) return OK; + if (llnl_temp.size() > 0) return OK; if (tc > 350.) { if (need_temp_msg < 1) @@ -223,7 +223,7 @@ calc_dielectrics(LDBLE tc, LDBLE pa) and Fernandez et al., 1997, JPCRD 26, 1125, show its correctness) + d(eps)/d(P), Debye-Hueckel A and B, and Av (for Av, see Pitzer et al., 1984, JPCRD 13, p. 4) */ - if (llnl_count_temp > 0) return OK; + if (llnl_temp.size() > 0) return OK; if (tc > 350.) { tc = 350.; @@ -1449,41 +1449,8 @@ status(int count, const char *str, bool rk_string) return (OK); } #endif /*PHREEQCI_GUI */ -/* -** Dynamic hashing, after CACM April 1988 pp 446-457, by Per-Ake Larson. -** Coded into C, with minor code improvements, and with hsearch(3) interface, -** by ejp@ausmelb.oz, Jul 26, 1988: 13:16; -** also, hcreate/hdestroy routines added to simulate hsearch(3). -** -** These routines simulate hsearch(3) and family, with the important -** difference that the hash table is dynamic - can grow indefinitely -** beyond its original size (as supplied to hcreate()). -** -** Performance appears to be comparable to that of hsearch(3). -** The 'source-code' options referred to in hsearch(3)'s 'man' page -** are not implemented; otherwise functionality is identical. -** -** Compilation controls: -** DEBUG controls some informative traces, mainly for debugging. -** HASH_STATISTICS causes HashAccesses and HashCollisions to be maintained; -** when combined with DEBUG, these are displayed by hdestroy(). -** -** Problems & fixes to ejp@ausmelb.oz. WARNING: relies on pre-processor -** concatenation property, in probably unnecessary code 'optimisation'. -** Esmond Pitt, Austec (Asia/Pacific) Ltd -** ...!uunet.UU.NET!munnari!ausmelb!ejp,ejp@ausmelb.oz -*/ - # include -/* -** Fast arithmetic, relying on powers of 2, -** and on pre-processor concatenation property -*/ - -/* rewrote to remove MUL and DIV */ -//# define MOD(x,y) ((x) & ((y)-1)) - /* ---------------------------------------------------------------------- */ int Phreeqc:: string_trim(char *str) From 78a83edabfdd3cd59e3c4d6a5ddb74693dbe60b2 Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Tue, 23 Mar 2021 19:35:54 -0600 Subject: [PATCH 02/53] c,d in polint --- integrate.cpp | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/integrate.cpp b/integrate.cpp index f38a334c..d1eb4bbd 100644 --- a/integrate.cpp +++ b/integrate.cpp @@ -298,21 +298,15 @@ polint(LDBLE * xa, LDBLE * ya, int n, LDBLE xv, LDBLE * yv, LDBLE * dy) { int i, m, ns; LDBLE den, dif, dift, ho, hp, w; - LDBLE *c, *d; ns = 1; dif = fabs(xv - xa[1]); /* * Malloc work space */ - c = (LDBLE *) PHRQ_malloc(((size_t)n + 1) * sizeof(LDBLE)); - if (c == NULL) - malloc_error(); - d = (LDBLE *) PHRQ_malloc(((size_t)n + 1) * sizeof(LDBLE)); - if (d == NULL) - malloc_error(); - - + std::vector c, d; + c.resize((size_t)n + 1); + d.resize((size_t)n + 1); for (i = 1; i <= n; i++) { @@ -354,8 +348,6 @@ polint(LDBLE * xa, LDBLE * ya, int n, LDBLE xv, LDBLE * yv, LDBLE * dy) /* *yv += (*dy = (2 * ns < (n-m) ? c[ns+1] : d[ns--])); */ } - c = (LDBLE *) free_check_null(c); - d = (LDBLE *) free_check_null(d); return; } From 1850c32c930f0a0c8fd627449032e5a6b379414f Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Tue, 23 Mar 2021 21:12:46 -0600 Subject: [PATCH 03/53] basic commands are now std::string --- PBasic.cpp | 14 +++---- PBasic.h | 6 +-- Phreeqc.cpp | 13 ++----- Phreeqc.h | 2 +- basicsubs.cpp | 4 +- global_structures.h | 4 +- isotopes.cpp | 52 +++++++++++-------------- kinetics.cpp | 2 +- mainsubs.cpp | 6 +-- print.cpp | 8 ++-- read.cpp | 94 +++++++++------------------------------------ structures.cpp | 6 +-- 12 files changed, 68 insertions(+), 143 deletions(-) diff --git a/PBasic.cpp b/PBasic.cpp index 515712b2..d57f313f 100644 --- a/PBasic.cpp +++ b/PBasic.cpp @@ -74,10 +74,10 @@ PBasic::~PBasic(void) } int PBasic:: -basic_compile(char *commands, void **lnbase, void **vbase, void **lpbase) +basic_compile(const char *commands, void **lnbase, void **vbase, void **lpbase) { /*main */ int l; - char *ptr; + const char *ptr; P_escapecode = 0; P_ioresult = 0; @@ -164,7 +164,7 @@ int PBasic:: basic_renumber(char *commands, void **lnbase, void **vbase, void **lpbase) { /*main */ int l, i; - char *ptr; + const char *ptr; P_escapecode = 0; P_ioresult = 0; @@ -245,7 +245,7 @@ int PBasic:: basic_run(char *commands, void *lnbase, void *vbase, void *lpbase) { /*main */ int l; - char *ptr; + const char *ptr; P_escapecode = 0; P_ioresult = 0; inbuf = (char *) PhreeqcPtr->PHRQ_calloc(PhreeqcPtr->max_line, sizeof(char)); @@ -317,10 +317,10 @@ basic_run(char *commands, void *lnbase, void *vbase, void *lpbase) } int PBasic:: -basic_main(char *commands) +basic_main(const char *commands) { /*main */ int l; - char *ptr; + const char *ptr; P_escapecode = 0; P_ioresult = 0; @@ -379,7 +379,7 @@ basic_main(char *commands) /* ---------------------------------------------------------------------- */ int PBasic:: -sget_logical_line(char **ptr, int *l, char *return_line) +sget_logical_line(const char **ptr, int *l, char *return_line) /* ---------------------------------------------------------------------- */ { /* diff --git a/PBasic.h b/PBasic.h index cc643a94..b3708b37 100644 --- a/PBasic.h +++ b/PBasic.h @@ -473,11 +473,11 @@ public: void cmddim(struct LOC_exec *LINK); void cmderase(struct LOC_exec *LINK); void cmdpoke(struct LOC_exec *LINK); - int basic_main(char *commands); - int basic_compile(char *commands, void **lnbase, void **vbase, void **lpbase); + int basic_main(const char *commands); + int basic_compile(const char *commands, void **lnbase, void **vbase, void **lpbase); int basic_run(char *commands, void *lnbase, void *vbase, void *lpbase); int basic_init(void); - int sget_logical_line(char **ptr, int *l, char *return_line); + int sget_logical_line(const char **ptr, int *l, char *return_line); long my_labs(long x); void * my_memmove(void * d, Const void * s, size_t n); void * my_memcpy(void * d, Const void * s, size_t n); diff --git a/Phreeqc.cpp b/Phreeqc.cpp index afb3cd6d..db03cede 100644 --- a/Phreeqc.cpp +++ b/Phreeqc.cpp @@ -1854,7 +1854,7 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) size_t count_rates = rates.size(); rates.resize(count_rates + 1); rates[i].name = string_hsave(pSrc->rates[i].name); - rates[i].commands = string_duplicate(pSrc->rates[i].commands); + rates[i].commands = pSrc->rates[i].commands; rates[i].new_def = TRUE; rates[i].linebase = NULL; rates[i].varbase = NULL; @@ -1869,11 +1869,7 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) */ { user_print->name = NULL; - user_print->commands = NULL; - if (pSrc->user_print->commands != NULL) - { - user_print->commands = string_duplicate(pSrc->user_print->commands); - } + user_print->commands = pSrc->user_print->commands; user_print->new_def = TRUE; user_print->linebase = NULL; user_print->varbase = NULL; @@ -2049,10 +2045,7 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) { struct calculate_value* calculate_value_ptr = calculate_value_store(pSrc->calculate_value[i]->name, FALSE); calculate_value_ptr->value = pSrc->calculate_value[i]->value; - if (pSrc->calculate_value[i]->commands) - { - calculate_value_ptr->commands = string_duplicate(pSrc->calculate_value[i]->commands); - } + calculate_value[i]->commands = pSrc->calculate_value[i]->commands; } for (int i = 0; i < (int)pSrc->isotope_ratio.size(); i++) diff --git a/Phreeqc.h b/Phreeqc.h index 66328b9c..26fdbae5 100644 --- a/Phreeqc.h +++ b/Phreeqc.h @@ -81,7 +81,7 @@ public: int advection(void); // basicsubs.cpp ------------------------------- - int basic_compile(char *commands, void **lnbase, void **vbase, void **lpbase); + int basic_compile(const char *commands, void **lnbase, void **vbase, void **lpbase); int basic_run(char *commands, void *lnbase, void *vbase, void *lpbase); void basic_free(void); #ifdef IPHREEQC_NO_FORTRAN_MODULE diff --git a/basicsubs.cpp b/basicsubs.cpp index 4a80889b..2b0c4f2b 100644 --- a/basicsubs.cpp +++ b/basicsubs.cpp @@ -1486,7 +1486,7 @@ get_calculate_value(const char *name) if (calculate_value_ptr->new_def == TRUE) { if (interp.basic_compile - (calculate_value_ptr->commands, + (calculate_value_ptr->commands.c_str(), &calculate_value_ptr->linebase, &calculate_value_ptr->varbase, &calculate_value_ptr->loopbase) != 0) @@ -3986,7 +3986,7 @@ iso_unit(const char *total_name) } int Phreeqc:: -basic_compile(char *commands, void **lnbase, void **vbase, void **lpbase) +basic_compile(const char *commands, void **lnbase, void **vbase, void **lpbase) { return this->basic_interpreter->basic_compile(commands, lnbase, vbase, lpbase); } diff --git a/global_structures.h b/global_structures.h index 8fdebcb2..3d9d075a 100644 --- a/global_structures.h +++ b/global_structures.h @@ -826,7 +826,7 @@ struct prints struct rate { const char *name; - char *commands; + std::string commands; int new_def; void *linebase; void *varbase; @@ -884,7 +884,7 @@ struct calculate_value { const char *name; LDBLE value; - char *commands; + std::string commands; int new_def; int calculated; void *linebase; diff --git a/isotopes.cpp b/isotopes.cpp index 751eaeb9..685eff1d 100644 --- a/isotopes.cpp +++ b/isotopes.cpp @@ -161,7 +161,7 @@ read_calculate_values(void) * */ char *ptr; - int l, length, line_length; + int l; int return_value, opt, opt_save; char token[MAX_LENGTH]; struct calculate_value *calculate_value_ptr; @@ -226,35 +226,28 @@ read_calculate_values(void) } calculate_value_ptr = calculate_value_store(token, TRUE); calculate_value_ptr->new_def = TRUE; - calculate_value_ptr->commands = - (char *) PHRQ_malloc(sizeof(char)); - if (calculate_value_ptr->commands == NULL) - { - malloc_error(); - } - else - { - calculate_value_ptr->commands[0] = '\0'; - calculate_value_ptr->linebase = NULL; - calculate_value_ptr->varbase = NULL; - calculate_value_ptr->loopbase = NULL; - } + calculate_value_ptr->commands.clear(); + calculate_value_ptr->linebase = NULL; + calculate_value_ptr->varbase = NULL; + calculate_value_ptr->loopbase = NULL; opt_save = OPT_1; break; case OPT_1: /* read command */ if (calculate_value_ptr) { - length = (int) strlen(calculate_value_ptr->commands); - line_length = (int) strlen(line); - calculate_value_ptr->commands = (char *)PHRQ_realloc(calculate_value_ptr->commands, - ((size_t)length + (size_t)line_length + 2) * sizeof(char)); - if (calculate_value_ptr->commands == NULL) - malloc_error(); - calculate_value_ptr->commands[length] = ';'; - calculate_value_ptr->commands[length + 1] = '\0'; - strcat((calculate_value_ptr->commands), line); - opt_save = OPT_1; + //length = (int) strlen(calculate_value_ptr->commands); + //line_length = (int) strlen(line); + //calculate_value_ptr->commands = (char *)PHRQ_realloc(calculate_value_ptr->commands, + // ((size_t)length + (size_t)line_length + 2) * sizeof(char)); + //if (calculate_value_ptr->commands == NULL) + // malloc_error(); + calculate_value_ptr->commands.append(";\0"); + calculate_value_ptr->commands.append(line); + //calculate_value_ptr->commands[length] = ';'; + //calculate_value_ptr->commands[length + 1] = '\0'; + //strcat((calculate_value_ptr->commands), line); + opt_save = OPT_1; } else { @@ -913,7 +906,7 @@ punch_calculate_values(void) if (calculate_value_ptr->new_def == TRUE) { if (basic_compile - (calculate_value_ptr->commands, &calculate_value_ptr->linebase, + (calculate_value_ptr->commands.c_str(), &calculate_value_ptr->linebase, &calculate_value_ptr->varbase, &calculate_value_ptr->loopbase) != 0) { @@ -1146,7 +1139,7 @@ calculate_values(void) if (calculate_value_ptr->new_def == TRUE) { if (basic_compile - (calculate_value_ptr->commands, &calculate_value_ptr->linebase, + (calculate_value_ptr->commands.c_str(), &calculate_value_ptr->linebase, &calculate_value_ptr->varbase, &calculate_value_ptr->loopbase) != 0) { @@ -1213,7 +1206,7 @@ calculate_values(void) if (calculate_value_ptr->new_def == TRUE) { if (basic_compile - (calculate_value_ptr->commands, &calculate_value_ptr->linebase, + (calculate_value_ptr->commands.c_str(), &calculate_value_ptr->linebase, &calculate_value_ptr->varbase, &calculate_value_ptr->loopbase) != 0) { @@ -1532,7 +1525,7 @@ calculate_value_init(struct calculate_value *calculate_value_ptr) { calculate_value_ptr->name = NULL; calculate_value_ptr->value = 0.0; - calculate_value_ptr->commands = NULL; + calculate_value_ptr->commands.clear(); calculate_value_ptr->new_def = TRUE; calculate_value_ptr->calculated = FALSE; calculate_value_ptr->linebase = NULL; @@ -1583,8 +1576,7 @@ calculate_value_free(struct calculate_value *calculate_value_ptr) if (calculate_value_ptr == NULL) return (ERROR); - calculate_value_ptr->commands = - (char *) free_check_null(calculate_value_ptr->commands); + calculate_value_ptr->commands.clear(); basic_run(cmd, calculate_value_ptr->linebase, calculate_value_ptr->varbase, calculate_value_ptr->loopbase); calculate_value_ptr->linebase = NULL; diff --git a/kinetics.cpp b/kinetics.cpp index b4f595bf..9c8bc141 100644 --- a/kinetics.cpp +++ b/kinetics.cpp @@ -88,7 +88,7 @@ calc_kinetic_reaction(cxxKinetics *kinetics_ptr, LDBLE time_step) if (rate_ptr->new_def == TRUE) { if (basic_compile - (rates[j].commands, &rates[j].linebase, &rates[j].varbase, + (rates[j].commands.c_str(), &rates[j].linebase, &rates[j].varbase, &rates[j].loopbase) != 0) { error_string = sformatf( "Fatal Basic error in rate %s.", diff --git a/mainsubs.cpp b/mainsubs.cpp index ec53940a..588394c4 100644 --- a/mainsubs.cpp +++ b/mainsubs.cpp @@ -60,10 +60,8 @@ initialize(void) stag_data->th_im = 0; // user_print - user_print = (struct rate *) PHRQ_malloc((size_t) sizeof(struct rate)); - if (user_print == NULL) - malloc_error(); - user_print->commands = NULL; + user_print = new struct rate; + user_print->commands.clear(); user_print->linebase = NULL; user_print->varbase = NULL; user_print->loopbase = NULL; diff --git a/print.cpp b/print.cpp index 617dbcfe..4a5d84b8 100644 --- a/print.cpp +++ b/print.cpp @@ -2360,7 +2360,7 @@ print_user_print(void) if (pr.user_print == FALSE || pr.all == FALSE) return (OK); - if (user_print->commands == NULL) + if (user_print->commands.size() == 0) return (OK); kinetics_ptr = NULL; if (use.Get_kinetics_in() == TRUE) @@ -2380,7 +2380,7 @@ print_user_print(void) { /* basic_renumber(user_print->commands, &user_print->linebase, &user_print->varbase, &user_print->loopbase); */ if (basic_compile - (user_print->commands, &user_print->linebase, + (user_print->commands.c_str(), &user_print->linebase, &user_print->varbase, &user_print->loopbase) != 0) { error_msg("Fatal Basic error in USER_PRINT.", STOP); @@ -3345,12 +3345,12 @@ punch_user_punch(void) struct rate * user_punch = current_user_punch->Get_rate(); - if (user_punch->commands == NULL) + if (user_punch->commands.c_str() == 0) return (OK); if (user_punch->new_def == TRUE) { if (basic_compile - (user_punch->commands, &user_punch->linebase, + (user_punch->commands.c_str(), &user_punch->linebase, &user_punch->varbase, &user_punch->loopbase) != 0) { error_msg("Fatal Basic error in USER_PUNCH.", STOP); diff --git a/read.cpp b/read.cpp index 6e813c2a..a9bb7898 100644 --- a/read.cpp +++ b/read.cpp @@ -8305,7 +8305,7 @@ read_rates(void) * */ char *ptr; - int l, length, line_length, n; + int l, n; int return_value, opt, opt_save; char token[MAX_LENGTH]; struct rate *rate_ptr; @@ -8375,19 +8375,11 @@ read_rates(void) rate_free(rate_ptr); } rate_ptr->new_def = TRUE; - rate_ptr->commands = (char *) PHRQ_malloc(sizeof(char)); - if (rate_ptr->commands == NULL) - { - malloc_error(); - } - else - { - rate_ptr->commands[0] = '\0'; - rate_ptr->name = string_hsave(token); - rate_ptr->linebase = NULL; - rate_ptr->varbase = NULL; - rate_ptr->loopbase = NULL; - } + rate_ptr->commands.clear(); + rate_ptr->name = string_hsave(token); + rate_ptr->linebase = NULL; + rate_ptr->varbase = NULL; + rate_ptr->loopbase = NULL; opt_save = OPT_1; break; case OPT_1: /* read command */ @@ -8399,20 +8391,8 @@ read_rates(void) opt_save = OPT_1; break; } - length = (int) strlen(rate_ptr->commands); - line_length = (int) strlen(line); - rate_ptr->commands = (char *) PHRQ_realloc(rate_ptr->commands, - ((size_t)length + (size_t)line_length + 2) * sizeof(char)); - if (rate_ptr->commands == NULL) - { - malloc_error(); - } - else - { - rate_ptr->commands[length] = ';'; - rate_ptr->commands[length + 1] = '\0'; - strcat((rate_ptr->commands), line); - } + rate_ptr->commands.append(";\0"); + rate_ptr->commands.append(line); opt_save = OPT_1; break; } @@ -8443,7 +8423,6 @@ read_user_print(void) * ERROR if error occurred reading data * */ - int length, line_length; int return_value, opt, opt_save; char *next_char; const char *opt_list[] = { @@ -8486,25 +8465,15 @@ read_user_print(void) case OPTION_DEFAULT: /* read first command */ rate_free(user_print); user_print->new_def = TRUE; - user_print->commands = (char *) PHRQ_malloc(sizeof(char)); - if (user_print->commands == NULL) - malloc_error(); - user_print->commands[0] = '\0'; + user_print->commands.clear(); user_print->linebase = NULL; user_print->varbase = NULL; user_print->loopbase = NULL; user_print->name = string_hsave("user defined Basic print routine"); case OPT_1: /* read command */ - length = (int) strlen(user_print->commands); - line_length = (int) strlen(line); - user_print->commands = (char *) PHRQ_realloc(user_print->commands, - ((size_t)length + (size_t)line_length + 2) * sizeof(char)); - if (user_print->commands == NULL) - malloc_error(); - user_print->commands[length] = ';'; - user_print->commands[length + 1] = '\0'; - strcat((user_print->commands), line); + user_print->commands.append(";\0"); + user_print->commands.append(line); opt_save = OPT_1; break; } @@ -8532,7 +8501,6 @@ read_user_punch(void) * ERROR if error occurred reading data * */ - int length, line_length; int return_value, opt, opt_save; std::string stdtoken; char *next_char; @@ -8568,9 +8536,8 @@ read_user_punch(void) //} // Malloc rate structure - struct rate *r = (struct rate *) PHRQ_malloc(sizeof(struct rate)); - if (r == NULL) malloc_error(); - r->commands = NULL; + struct rate* r = new struct rate; + r->commands.clear(); r->new_def = TRUE; r->linebase = NULL; r->varbase = NULL; @@ -8613,11 +8580,9 @@ read_user_punch(void) } break; case OPTION_DEFAULT: /* read first command */ - { - r->commands = (char *) PHRQ_malloc(sizeof(char)); - if (r->commands == NULL) malloc_error(); - else r->commands[0] = '\0'; - } + { + r->commands.clear(); + } //rate_free(user_punch); //user_punch->new_def = TRUE; //user_punch->commands = (char *) PHRQ_malloc(sizeof(char)); @@ -8630,31 +8595,8 @@ read_user_punch(void) //user_punch->name = // string_hsave("user defined Basic punch routine"); case OPT_1: /* read command */ - length = (int) strlen(r->commands); - line_length = (int) strlen(line); - r->commands = (char *) PHRQ_realloc(r->commands, - ((size_t)length + (size_t)line_length + 2) * sizeof(char)); - if (r->commands == NULL) - { - malloc_error(); - } - else - { - r->commands[length] = ';'; - r->commands[length + 1] = '\0'; - strcat((r->commands), line); - } - //length = (int) strlen(user_punch->commands); - //line_length = (int) strlen(line); - //user_punch->commands = - // (char *) PHRQ_realloc(user_punch->commands, - // (size_t) (length + line_length + - // 2) * sizeof(char)); - //if (user_punch->commands == NULL) - // malloc_error(); - //user_punch->commands[length] = ';'; - //user_punch->commands[length + 1] = '\0'; - //strcat((user_punch->commands), line); + r->commands.append(";\0"); + r->commands.append(line); opt_save = OPT_1; break; } diff --git a/structures.cpp b/structures.cpp index 90da7207..b99d8714 100644 --- a/structures.cpp +++ b/structures.cpp @@ -166,7 +166,7 @@ clean_up(void) /* user_print and user_punch */ UserPunch_map.clear(); rate_free(user_print); - user_print = (struct rate*)free_check_null(user_print); + delete user_print; /* Clear llnl aqueous model parameters */ @@ -1421,7 +1421,7 @@ rate_free(struct rate *rate_ptr) if (rate_ptr == NULL) return (ERROR); - rate_ptr->commands = (char *) free_check_null(rate_ptr->commands); + rate_ptr->commands.clear(); if (rate_ptr->linebase != NULL) { char cmd[] = "new; quit"; @@ -1445,7 +1445,7 @@ rate_copy(struct rate *rate_ptr) return (NULL); struct rate * rate_new = (struct rate *) PHRQ_malloc(sizeof(struct rate)); if (rate_new == NULL) malloc_error(); - rate_new->commands = string_duplicate(rate_ptr->commands); + rate_new->commands = rate_ptr->commands; rate_new->new_def = TRUE; rate_new->linebase = NULL; rate_new->varbase = NULL; From bd0cad943401dcd5e7984bb76e9003a87dbd8e8e Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Tue, 23 Mar 2021 22:04:45 -0600 Subject: [PATCH 04/53] vector kinetics arrays --- Phreeqc.cpp | 4 ---- Phreeqc.h | 5 +---- isotopes.cpp | 9 --------- kinetics.cpp | 49 +++++++++++++++++-------------------------------- structures.cpp | 8 ++++---- 5 files changed, 22 insertions(+), 53 deletions(-) diff --git a/Phreeqc.cpp b/Phreeqc.cpp index db03cede..693b05a8 100644 --- a/Phreeqc.cpp +++ b/Phreeqc.cpp @@ -1061,11 +1061,7 @@ void Phreeqc::init(void) kinetics_cvode_mem = NULL; cvode_pp_assemblage_save= NULL; cvode_ss_assemblage_save= NULL; - m_original = NULL; - m_temp = NULL; - rk_moles = NULL; set_and_run_attempt = 0; - x0_moles = NULL; /* model.cpp ------------------------------- */ gas_in = FALSE; min_value = 1e-10; diff --git a/Phreeqc.h b/Phreeqc.h index 26fdbae5..ac1bbcf0 100644 --- a/Phreeqc.h +++ b/Phreeqc.h @@ -1775,11 +1775,8 @@ public: cxxSSassemblage *cvode_ss_assemblage_save; cxxPPassemblage *cvode_pp_assemblage_save; protected: - LDBLE *m_original; - LDBLE *m_temp; - LDBLE *rk_moles; + std::vector m_temp, m_original, rk_moles, x0_moles; int set_and_run_attempt; - LDBLE *x0_moles; /* model.cpp ------------------------------- */ int gas_in; diff --git a/isotopes.cpp b/isotopes.cpp index 685eff1d..15b78017 100644 --- a/isotopes.cpp +++ b/isotopes.cpp @@ -236,17 +236,8 @@ read_calculate_values(void) case OPT_1: /* read command */ if (calculate_value_ptr) { - //length = (int) strlen(calculate_value_ptr->commands); - //line_length = (int) strlen(line); - //calculate_value_ptr->commands = (char *)PHRQ_realloc(calculate_value_ptr->commands, - // ((size_t)length + (size_t)line_length + 2) * sizeof(char)); - //if (calculate_value_ptr->commands == NULL) - // malloc_error(); calculate_value_ptr->commands.append(";\0"); calculate_value_ptr->commands.append(line); - //calculate_value_ptr->commands[length] = ';'; - //calculate_value_ptr->commands[length + 1] = '\0'; - //strcat((calculate_value_ptr->commands), line); opt_save = OPT_1; } else diff --git a/kinetics.cpp b/kinetics.cpp index 9c8bc141..bceaf1a0 100644 --- a/kinetics.cpp +++ b/kinetics.cpp @@ -338,9 +338,7 @@ rk_kinetics(int i, LDBLE kin_time, int use_mix, int nsaver, if (kinetics_ptr == NULL) return (OK); n_reactions = (int) kinetics_ptr->Get_kinetics_comps().size(); - rk_moles = (LDBLE *) free_check_null(rk_moles); - rk_moles = (LDBLE *) PHRQ_malloc((size_t) 6 * n_reactions * sizeof(LDBLE)); - if (rk_moles == NULL) malloc_error(); + rk_moles.resize(6 * (size_t)n_reactions); /*if (use_mix != NOMIX) last_model.force_prep = TRUE; */ set_and_run_wrapper(i, use_mix, FALSE, i, step_fraction); @@ -1112,7 +1110,7 @@ rk_kinetics(int i, LDBLE kin_time, int use_mix, int nsaver, { Utilities::Rxn_copy(Rxn_solution_map, save_old, i); } - rk_moles = (LDBLE *) free_check_null(rk_moles); + rk_moles.clear(); rate_sim_time = rate_sim_time_start + kin_time; use.Set_kinetics_in(true); @@ -2040,14 +2038,8 @@ run_reactions(int i, LDBLE kin_time, int use_mix, LDBLE step_fraction) * Save moles of kinetic reactants for printout... */ size_t count_comps = kinetics_ptr->Get_kinetics_comps().size(); - m_temp = (LDBLE *) PHRQ_malloc(count_comps * sizeof(LDBLE)); - if (m_temp == NULL) - malloc_error(); - - m_original = - (LDBLE *) PHRQ_malloc(count_comps * sizeof(LDBLE)); - if (m_original == NULL) - malloc_error(); + m_temp.resize(count_comps); + m_original.resize(count_comps); for (size_t j = 0; j < kinetics_ptr->Get_kinetics_comps().size(); j++) { @@ -2244,8 +2236,8 @@ run_reactions(int i, LDBLE kin_time, int use_mix, LDBLE step_fraction) cvode_last_good_time = 0; if (++m_iter >= kinetics_ptr->Get_bad_step_max()) { - m_temp = (LDBLE *) free_check_null(m_temp); - m_original = (LDBLE *) free_check_null(m_original); + m_temp.clear(); + m_original.clear(); error_string = sformatf( "CVode is at maximum calls: %d. Cell: %d. Time: %8.2e s\nERROR: Please increase the maximum calls with -bad_step_max.", m_iter, cell_no, (double)sum_t); @@ -2349,8 +2341,8 @@ run_reactions(int i, LDBLE kin_time, int use_mix, LDBLE step_fraction) kinetics_comp_ptr->Set_moles(m_original[j] - kinetics_comp_ptr->Get_m()); /* if (kinetics_ptr->comps[j].moles < 1.e-15) kinetics_ptr->comps[j].moles = 0.0; */ } - m_temp = (LDBLE *) free_check_null(m_temp); - m_original = (LDBLE *) free_check_null(m_original); + m_temp.clear(); + m_original.clear(); } iterations = run_reactions_iterations; if (cvode_pp_assemblage_save != NULL) @@ -2651,13 +2643,8 @@ store_get_equi_reactants(int l, int kin_end) if (k == 0) return (OK); - x0_moles = (LDBLE *) free_check_null(x0_moles); - x0_moles = (LDBLE *) PHRQ_malloc((size_t) k * sizeof(LDBLE)); - if (x0_moles == NULL) malloc_error(); - for (i = 0; i < k; i++) - { - x0_moles[i] = 0.0; - } + x0_moles.resize(k); + for (i = 0; i < k; i++) x0_moles[i] = 0.0; k = -1; if (pp_assemblage_ptr) { @@ -2740,7 +2727,7 @@ store_get_equi_reactants(int l, int kin_end) * This condition makes output equal for incremental_reactions TRUE/FALSE... * if (incremental_reactions == FALSE || reaction_step == count_total_steps) */ - x0_moles = (LDBLE *) free_check_null(x0_moles); + x0_moles.clear(); } return (OK); } @@ -2849,7 +2836,8 @@ Jac(integertype N, DenseMat J, RhsFn f, void *f_data, { int count_cvode_errors; int n_reactions, n_user; - LDBLE *initial_rates, del; + LDBLE del; + std::vector initial_rates; cxxKinetics *kinetics_ptr; LDBLE step_fraction; @@ -2862,10 +2850,7 @@ Jac(integertype N, DenseMat J, RhsFn f, void *f_data, step_fraction = pThis->cvode_step_fraction; pThis->rate_sim_time = pThis->cvode_rate_sim_time; - initial_rates = - (LDBLE *) pThis->PHRQ_malloc ((size_t) n_reactions * sizeof(LDBLE)); - if (initial_rates == NULL) - pThis->malloc_error(); + initial_rates.resize(n_reactions); for (size_t i = 0; i < kinetics_ptr->Get_kinetics_comps().size(); i++) { @@ -2907,7 +2892,7 @@ Jac(integertype N, DenseMat J, RhsFn f, void *f_data, /* error_msg("Mass balance error in jacobian", CONTINUE); */ - initial_rates = (LDBLE *) pThis->free_check_null(initial_rates); + initial_rates.clear(); return; } pThis->run_reactions_iterations += pThis->iterations; @@ -2979,7 +2964,7 @@ Jac(integertype N, DenseMat J, RhsFn f, void *f_data, pThis->cvode_error = TRUE; if (count_cvode_errors > 30) { - initial_rates = (LDBLE *) pThis->free_check_null(initial_rates); + initial_rates.clear(); return; } pThis->run_reactions_iterations += pThis->iterations; @@ -3010,7 +2995,7 @@ Jac(integertype N, DenseMat J, RhsFn f, void *f_data, cxxKineticsComp * kinetics_comp_ptr = &(kinetics_ptr->Get_kinetics_comps()[i]); kinetics_comp_ptr->Set_moles(0); } - initial_rates = (LDBLE *) pThis->free_check_null(initial_rates); + initial_rates.clear(); return; } diff --git a/structures.cpp b/structures.cpp index b99d8714..30635ebc 100644 --- a/structures.cpp +++ b/structures.cpp @@ -127,10 +127,10 @@ clean_up(void) Rxn_gas_phase_map.clear(); /* kinetics */ Rxn_kinetics_map.clear(); - x0_moles = (LDBLE*)free_check_null(x0_moles); - m_temp = (LDBLE*)free_check_null(m_temp); - m_original = (LDBLE*)free_check_null(m_original); - rk_moles = (LDBLE*)free_check_null(rk_moles); + x0_moles.clear(); + m_temp.clear(); + m_original.clear(); + rk_moles.clear(); /* rates */ for (j = 0; j < (int)rates.size(); j++) { From 90e8412074f9b76f7c0b84cb5f024ed082a6ed01 Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Tue, 23 Mar 2021 23:17:46 -0600 Subject: [PATCH 05/53] starting on pitzer --- Phreeqc.cpp | 7 ++----- Phreeqc.h | 6 +++--- pitzer.cpp | 35 ++++++++++------------------------- 3 files changed, 15 insertions(+), 33 deletions(-) diff --git a/Phreeqc.cpp b/Phreeqc.cpp index 693b05a8..90aa8c59 100644 --- a/Phreeqc.cpp +++ b/Phreeqc.cpp @@ -1090,7 +1090,7 @@ void Phreeqc::init(void) use_etheta = TRUE; OTEMP = -100.; OPRESS = -100.; - A0 = 0; + A0 = 0; aphi = NULL; spec = NULL; cations = NULL; @@ -1105,9 +1105,6 @@ void Phreeqc::init(void) mcb0 = NULL; mcb1 = NULL; mcc0 = NULL; - IPRSNT = NULL; - M = NULL; - LGAMMA = NULL; for (int i = 0; i < 23; i++) { BK[i] = 0.0; @@ -2109,7 +2106,7 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) */ if (pSrc->aphi != NULL) { - aphi = (struct pitz_param *) malloc(sizeof(struct pitz_param)); + aphi = (struct pitz_param*)malloc(sizeof(struct pitz_param)); memcpy(aphi, pSrc->aphi, sizeof(struct pitz_param)); } /* diff --git a/Phreeqc.h b/Phreeqc.h index ac1bbcf0..f96ad816 100644 --- a/Phreeqc.h +++ b/Phreeqc.h @@ -1801,13 +1801,13 @@ protected: int use_etheta; LDBLE OTEMP, OPRESS; LDBLE A0; - struct pitz_param *aphi; + struct pitz_param* aphi; struct species **spec, **cations, **anions, **neutrals; int count_cations, count_anions, count_neutrals; int MAXCATIONS, FIRSTANION, MAXNEUTRAL; struct pitz_param *mcb0, *mcb1, *mcc0; - int *IPRSNT; - LDBLE *M, *LGAMMA; + std::vector IPRSNT; + std::vector M, LGAMMA; LDBLE BK[23], DK[23]; LDBLE dummy; diff --git a/pitzer.cpp b/pitzer.cpp index 4036b9f7..6e1a1a33 100644 --- a/pitzer.cpp +++ b/pitzer.cpp @@ -78,22 +78,9 @@ pitzer_tidy(void) /* * allocate other arrays for Pitzer */ - if (IPRSNT != NULL) - IPRSNT = (int *) free_check_null(IPRSNT); - IPRSNT = (int *) PHRQ_malloc((size_t) (3 * s.size() * sizeof(int))); - if (IPRSNT == NULL) - malloc_error(); - if (M != NULL) - M = (LDBLE *) free_check_null(M); - M = (LDBLE *) PHRQ_malloc((size_t) (3 * s.size() * sizeof(LDBLE))); - if (M == NULL) - malloc_error(); - if (LGAMMA != NULL) - LGAMMA = (LDBLE *) free_check_null(LGAMMA); - LGAMMA = (LDBLE *) PHRQ_malloc((size_t) (3 * s.size() * sizeof(LDBLE))); - if (LGAMMA == NULL) - malloc_error(); - + IPRSNT.resize(3 * s.size()); + M.resize(3 * s.size()); + LGAMMA.resize(3 * s.size()); for (i = 0; i < (int)s.size(); i++) { @@ -1660,11 +1647,11 @@ pitzer_clean_up(void) (struct theta_param *) free_check_null(theta_params[i]); } theta_params.clear(); - LGAMMA = (LDBLE *) free_check_null(LGAMMA); - IPRSNT = (int *) free_check_null(IPRSNT); + LGAMMA.clear(); + IPRSNT.clear(); spec = (struct species **) free_check_null(spec); aphi = (struct pitz_param *) free_check_null(aphi); - M = (LDBLE *) free_check_null(M); + M.clear(); return OK; } @@ -1959,7 +1946,7 @@ int Phreeqc:: jacobian_pz(void) /* ---------------------------------------------------------------------- */ { // calculate the derivatives numerically - LDBLE *base; + std::vector base; LDBLE d, d1, d2; int i, j; @@ -1973,9 +1960,7 @@ Restart: pitzer(); residuals(); } - base = (LDBLE *) PHRQ_malloc((size_t) count_unknowns * sizeof(LDBLE)); - if (base == NULL) - malloc_error(); + base.resize(count_unknowns); for (i = 0; i < count_unknowns; i++) { base[i] = residual[i]; @@ -2073,7 +2058,7 @@ Restart: molalities(TRUE); if (max_unknowns > pz_max_unknowns) { - base = (LDBLE *) free_check_null(base); + base.clear(); gammas_pz(false); jacobian_sums(); goto Restart; @@ -2144,7 +2129,7 @@ Restart: pitzer(); mb_sums(); residuals(); - free_check_null(base); + base.clear(); calculating_deriv = 0; return OK; } From 6907bb06c41e234d61ea71c68f47eed33a6f3671 Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Wed, 24 Mar 2021 08:31:54 -0600 Subject: [PATCH 06/53] base, sit arrays --- Phreeqc.cpp | 4 ---- Phreeqc.h | 7 ++++--- pitzer.cpp | 14 ++++---------- prep.cpp | 5 ++--- sit.cpp | 44 ++++++++++++-------------------------------- 5 files changed, 22 insertions(+), 52 deletions(-) diff --git a/Phreeqc.cpp b/Phreeqc.cpp index 90aa8c59..0d7863b4 100644 --- a/Phreeqc.cpp +++ b/Phreeqc.cpp @@ -1092,7 +1092,6 @@ void Phreeqc::init(void) OPRESS = -100.; A0 = 0; aphi = NULL; - spec = NULL; cations = NULL; anions = NULL; neutrals = NULL; @@ -1137,9 +1136,6 @@ void Phreeqc::init(void) sit_MAXCATIONS = 0; sit_FIRSTANION = 0; sit_MAXNEUTRAL = 0; - sit_IPRSNT = NULL; - sit_M = NULL; - sit_LGAMMA = NULL; /* tidy.cpp ------------------------------- */ a0 = 0; a1 = 0; diff --git a/Phreeqc.h b/Phreeqc.h index f96ad816..177d64e4 100644 --- a/Phreeqc.h +++ b/Phreeqc.h @@ -1802,7 +1802,8 @@ protected: LDBLE OTEMP, OPRESS; LDBLE A0; struct pitz_param* aphi; - struct species **spec, **cations, **anions, **neutrals; + std::vector spec; + struct species ** cations, ** anions, ** neutrals; // pointers to spec int count_cations, count_anions, count_neutrals; int MAXCATIONS, FIRSTANION, MAXNEUTRAL; struct pitz_param *mcb0, *mcb1, *mcc0; @@ -1836,8 +1837,8 @@ protected: LDBLE sit_A0; int sit_count_cations, sit_count_anions, sit_count_neutrals; int sit_MAXCATIONS, sit_FIRSTANION, sit_MAXNEUTRAL; - int *sit_IPRSNT; - LDBLE *sit_M, *sit_LGAMMA; + std::vector sit_IPRSNT; + std::vector sit_M, sit_LGAMMA; std::vector s_list, cation_list, neutral_list, anion_list, ion_list, param_list; /* tidy.cpp ------------------------------- */ diff --git a/pitzer.cpp b/pitzer.cpp index 6e1a1a33..4da1eafd 100644 --- a/pitzer.cpp +++ b/pitzer.cpp @@ -56,15 +56,9 @@ pitzer_tidy(void) /* * allocate pointers to species structures */ - if (spec != NULL) - spec = (struct species **) free_check_null(spec); - spec = (struct species **) - PHRQ_malloc((size_t) (3 * s.size() * sizeof(struct species *))); - if (spec == NULL) - malloc_error(); - for (i = 0; i < 3 * (int)s.size(); i++) - spec[i] = NULL; - cations = spec; + spec.clear(); + spec.resize(3 * s.size(), NULL); + cations = &spec[0]; neutrals = &(spec[s.size()]); anions = &(spec[2 * s.size()]); MAXCATIONS = (int)s.size(); @@ -1649,7 +1643,7 @@ pitzer_clean_up(void) theta_params.clear(); LGAMMA.clear(); IPRSNT.clear(); - spec = (struct species **) free_check_null(spec); + spec.clear(); aphi = (struct pitz_param *) free_check_null(aphi); M.clear(); diff --git a/prep.cpp b/prep.cpp index 0bfb091e..eda023b0 100644 --- a/prep.cpp +++ b/prep.cpp @@ -5814,9 +5814,8 @@ save_model(void) } /* charge */ last_model.count_surface_charge = (int) use.Get_surface_ptr()->Get_surface_charges().size(); - last_model.surface_charge = - (const char **) PHRQ_malloc( use.Get_surface_ptr()->Get_surface_charges().size() * - sizeof(char *)); + last_model.surface_charge =(const char **) PHRQ_malloc( use.Get_surface_ptr()->Get_surface_charges().size() * + sizeof(char *)); if (last_model.surface_charge == NULL) malloc_error(); for (i = 0; i < (int) use.Get_surface_ptr()->Get_surface_charges().size(); i++) diff --git a/sit.cpp b/sit.cpp index a92a8313..74455f0e 100644 --- a/sit.cpp +++ b/sit.cpp @@ -35,12 +35,10 @@ sit_tidy(void) /* * allocate pointers to species structures */ - if (spec != NULL) spec = (struct species **) free_check_null(spec); - spec = (struct species **) PHRQ_malloc((size_t) (3 * s.size() * sizeof(struct species *))); - if (spec == NULL) malloc_error(); - for (i = 0; i < 3 * (int)s.size(); i++) spec[i] = NULL; + spec.clear(); + spec.resize(3 * s.size(), NULL); - cations = spec; + cations = &spec[0]; neutrals = &(spec[s.size()]); anions = &(spec[2 * s.size()]); sit_MAXCATIONS = (int)s.size(); @@ -53,16 +51,9 @@ sit_tidy(void) /* * allocate other arrays for SIT */ - if (sit_IPRSNT != NULL) sit_IPRSNT = (int *) free_check_null(sit_IPRSNT); - sit_IPRSNT = (int *) PHRQ_malloc((size_t) (3 * s.size() * sizeof(int))); - if (sit_IPRSNT == NULL) malloc_error(); - if (sit_M != NULL) sit_M = (LDBLE *) free_check_null(sit_M); - sit_M = (LDBLE *) PHRQ_malloc((size_t) (3 * s.size() * sizeof(LDBLE))); - if (sit_M == NULL) malloc_error(); - if (sit_LGAMMA != NULL) sit_LGAMMA = (LDBLE *) free_check_null(sit_LGAMMA); - sit_LGAMMA = (LDBLE *) PHRQ_malloc((size_t) (3 * s.size() * sizeof(LDBLE))); - if (sit_LGAMMA == NULL) malloc_error(); - + sit_IPRSNT.resize(3 * s.size()); + sit_M.resize(3 * s.size()); + sit_LGAMMA.resize(3 * s.size()); for (i = 0; i < (int)s.size(); i++) { @@ -509,11 +500,11 @@ sit_clean_up(void) } sit_params.clear(); sit_param_map.clear(); - sit_LGAMMA = (LDBLE *) free_check_null(sit_LGAMMA); - sit_IPRSNT = (int *) free_check_null(sit_IPRSNT); - spec = (struct species **) free_check_null(spec); + sit_LGAMMA.clear(); + sit_IPRSNT.clear(); + spec.clear(); aphi = (struct pitz_param *) free_check_null(aphi); - sit_M = (LDBLE *) free_check_null(sit_M); + sit_M.clear(); return OK; } @@ -805,7 +796,7 @@ int Phreeqc:: jacobian_sit(void) /* ---------------------------------------------------------------------- */ { - LDBLE *base; + std::vector base; LDBLE d, d1, d2; int i, j; Restart: @@ -817,16 +808,7 @@ Restart: sit(); residuals(); } - base = (LDBLE *) PHRQ_malloc((size_t) count_unknowns * sizeof(LDBLE)); - if (base == NULL) - { - malloc_error(); - return OK; - } - for (i = 0; i < count_unknowns; i++) - { - base[i] = residual[i]; - } + base = residual; // std::vectors d = 0.0001; d1 = d * LOG_10; d2 = 0; @@ -900,7 +882,6 @@ Restart: molalities(TRUE); if (max_unknowns > pz_max_unknowns) { - base = (LDBLE *) free_check_null(base); gammas_sit(); jacobian_sums(); goto Restart; @@ -960,7 +941,6 @@ Restart: sit(); mb_sums(); residuals(); - free_check_null(base); return OK; } From 2b14f8090e57182a2f7b30cc630c0d99cfea3486 Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Wed, 24 Mar 2021 11:20:03 -0600 Subject: [PATCH 07/53] last_model --- Phreeqc.cpp | 27 +++-------- advection.cpp | 2 +- global_structures.h | 40 ++++++++-------- model.cpp | 2 +- prep.cpp | 110 +++++++++++--------------------------------- structures.cpp | 23 +++------ tidy.cpp | 31 ++++--------- transport.cpp | 2 +- 8 files changed, 68 insertions(+), 169 deletions(-) diff --git a/Phreeqc.cpp b/Phreeqc.cpp index 0d7863b4..151a1fc2 100644 --- a/Phreeqc.cpp +++ b/Phreeqc.cpp @@ -445,30 +445,15 @@ void Phreeqc::init(void) /* * last model */ - last_model.force_prep = TRUE; - last_model.temperature = -100; - last_model.pressure = 0; - last_model.count_exchange = -1; - last_model.exchange = NULL; - last_model.count_kinetics = -1; - last_model.kinetics = NULL; - last_model.count_gas_phase = -1; + last_model.force_prep = true; last_model.gas_phase_type = cxxGasPhase::GP_UNKNOWN; - last_model.gas_phase = NULL; - last_model.count_ss_assemblage = -1; - last_model.ss_assemblage = NULL; - last_model.count_pp_assemblage = -1; - last_model.pp_assemblage = NULL; - last_model.add_formula = NULL; - last_model.si = NULL; + last_model.gas_phase.clear(); + last_model.ss_assemblage.clear(); + last_model.pp_assemblage.clear(); + last_model.add_formula.clear(); + last_model.si.clear(); last_model.dl_type = cxxSurface::NO_DL; last_model.surface_type = cxxSurface::UNKNOWN_DL; - last_model.only_counter_ions = FALSE; - last_model.thickness = 1e-8; - last_model.count_surface_comp = -1; - last_model.surface_comp = NULL; - last_model.count_surface_charge = -1; - last_model.surface_charge = NULL; current_selected_output = NULL; current_user_punch = NULL; diff --git a/advection.cpp b/advection.cpp index 276cb1a4..a184cd5c 100644 --- a/advection.cpp +++ b/advection.cpp @@ -60,7 +60,7 @@ advection(void) /* * Equilibrate solutions with phases, exchangers, surfaces */ - last_model.force_prep = TRUE; + last_model.force_prep = true; rate_sim_time_start = 0; for (advection_step = 1; advection_step <= count_ad_shifts; advection_step++) diff --git a/global_structures.h b/global_structures.h index 3d9d075a..48135711 100644 --- a/global_structures.h +++ b/global_structures.h @@ -193,37 +193,33 @@ typedef struct PHRQMemHeader struct model { - int force_prep; - LDBLE temperature; - int count_exchange; - struct master **exchange; + bool force_prep; + //LDBLE temperaturex; // not used + //LDBLE pressure; // not used - int count_kinetics; - struct kinetics *kinetics; + //int count_exchange; + //struct master **exchange; // not used - int count_gas_phase; + //int count_kinetics; + //struct kinetics *kinetics; // not used + + bool numerical_fixed_volume; cxxGasPhase::GP_TYPE gas_phase_type; - struct phase **gas_phase; + std::vector gas_phase; - int count_ss_assemblage; - const char **ss_assemblage; + std::vector ss_assemblage; - int count_pp_assemblage; - struct phase **pp_assemblage; - const char **add_formula; - LDBLE *si; + std::vector pp_assemblage; + std::vector si; + std::vector add_formula; cxxSurface::DIFFUSE_LAYER_TYPE dl_type; cxxSurface::SURFACE_TYPE surface_type; - int only_counter_ions; + //bool only_counter_ions; // not used - LDBLE thickness; - int count_surface_comp; - const char **surface_comp; - int count_surface_charge; - const char **surface_charge; - LDBLE pressure; - bool numerical_fixed_volume; + //LDBLE thickness; // not used + std::vector surface_comp; + std::vector surface_charge; }; struct name_master diff --git a/model.cpp b/model.cpp index ee8d6e0d..40fff3e2 100644 --- a/model.cpp +++ b/model.cpp @@ -5001,7 +5001,7 @@ surface_model(void) */ debug_diffuse_layer_save = debug_diffuse_layer; debug_model_save = debug_model; - if (last_model.force_prep == TRUE) + if (last_model.force_prep) { same_model = FALSE; } diff --git a/prep.cpp b/prep.cpp index eda023b0..fccc6e82 100644 --- a/prep.cpp +++ b/prep.cpp @@ -34,7 +34,7 @@ prep(void) else { same_model = FALSE; - last_model.force_prep = TRUE; + last_model.force_prep = true; } /*same_model = FALSE; */ /* @@ -5665,14 +5665,6 @@ save_model(void) /* ---------------------------------------------------------------------- */ { int i; -/* - * save temperature - */ - last_model.temperature = tc_x; -/* - * save pressure - */ - last_model.pressure = patm_x; /* * mark master species */ @@ -5695,18 +5687,11 @@ save_model(void) /* * save list of phase pointers for gas phase */ - last_model.gas_phase = - (struct phase **) free_check_null(last_model.gas_phase); if (use.Get_gas_phase_ptr() != NULL) { cxxGasPhase * gas_phase_ptr = use.Get_gas_phase_ptr(); - last_model.count_gas_phase = (int) gas_phase_ptr->Get_gas_comps().size(); last_model.gas_phase_type = gas_phase_ptr->Get_type(); - last_model.gas_phase = - (struct phase **) PHRQ_malloc((size_t) last_model.count_gas_phase * - sizeof(struct phase *)); - if (last_model.gas_phase == NULL) - malloc_error(); + last_model.gas_phase.resize(gas_phase_ptr->Get_gas_comps().size()); for (size_t i = 0; i < gas_phase_ptr->Get_gas_comps().size(); i++) { cxxGasComp *gc_ptr = &(gas_phase_ptr->Get_gas_comps()[i]); @@ -5718,23 +5703,15 @@ save_model(void) } else { - last_model.count_gas_phase = 0; last_model.gas_phase_type = cxxGasPhase::GP_UNKNOWN; - last_model.gas_phase = NULL; + last_model.gas_phase.clear(); } /* * save list of names of solid solutions */ - last_model.ss_assemblage = - (const char **) free_check_null(last_model.ss_assemblage); if (use.Get_ss_assemblage_ptr() != NULL) { - size_t count_ss = use.Get_ss_assemblage_ptr()->Get_SSs().size(); - last_model.count_ss_assemblage = (int) count_ss; - last_model.ss_assemblage = - (const char **) PHRQ_malloc(count_ss * sizeof(char *)); - if (last_model.ss_assemblage == NULL) - malloc_error(); + last_model.ss_assemblage.resize(use.Get_ss_assemblage_ptr()->Get_SSs().size()); std::vector ss_ptrs = use.Get_ss_assemblage_ptr()->Vectorize(); for (size_t j = 0; j < ss_ptrs.size(); j++) { @@ -5743,34 +5720,17 @@ save_model(void) } else { - last_model.count_ss_assemblage = 0; - last_model.ss_assemblage = NULL; + last_model.ss_assemblage.clear(); } /* * save list of phase pointers for pp_assemblage */ - last_model.pp_assemblage = - (struct phase **) free_check_null(last_model.pp_assemblage); - last_model.add_formula = - (const char **) free_check_null(last_model.add_formula); - last_model.si = (LDBLE *) free_check_null(last_model.si); if (use.Get_pp_assemblage_ptr() != NULL) { cxxPPassemblage * pp_assemblage_ptr = use.Get_pp_assemblage_ptr(); - last_model.count_pp_assemblage = (int) pp_assemblage_ptr->Get_pp_assemblage_comps().size(); - last_model.pp_assemblage = - (struct phase **) PHRQ_malloc((size_t) last_model.count_pp_assemblage * - sizeof(struct phase *)); - if (last_model.pp_assemblage == NULL) - malloc_error(); - last_model.add_formula = - (const char **) PHRQ_malloc((size_t)last_model.count_pp_assemblage * sizeof(char *)); - if (last_model.add_formula == NULL) - malloc_error(); - last_model.si = - (LDBLE *) PHRQ_malloc((size_t) last_model.count_pp_assemblage * sizeof(LDBLE)); - if (last_model.si == NULL) - malloc_error(); + last_model.pp_assemblage.resize(pp_assemblage_ptr->Get_pp_assemblage_comps().size()); + last_model.add_formula.resize(pp_assemblage_ptr->Get_pp_assemblage_comps().size()); + last_model.si.resize(pp_assemblage_ptr->Get_pp_assemblage_comps().size()); std::map::iterator it; it = pp_assemblage_ptr->Get_pp_assemblage_comps().begin(); i = 0; @@ -5787,37 +5747,23 @@ save_model(void) } else { - last_model.count_pp_assemblage = 0; - last_model.pp_assemblage = NULL; - last_model.add_formula = NULL; - last_model.si = NULL; + last_model.pp_assemblage.clear(); + last_model.add_formula.clear(); + last_model.si.clear(); } /* * save data for surface */ - last_model.surface_comp = - (const char **) free_check_null(last_model.surface_comp); - last_model.surface_charge = - (const char **) free_check_null(last_model.surface_charge); if (use.Get_surface_ptr() != NULL) { /* comps */ - last_model.count_surface_comp = (int) use.Get_surface_ptr()->Get_surface_comps().size(); - last_model.surface_comp = - (const char **) PHRQ_malloc(use.Get_surface_ptr()->Get_surface_comps().size() * - sizeof(char *)); - if (last_model.surface_comp == NULL) - malloc_error(); + last_model.surface_comp.resize(use.Get_surface_ptr()->Get_surface_comps().size()); for (i = 0; i < (int) use.Get_surface_ptr()->Get_surface_comps().size(); i++) { last_model.surface_comp[i] = string_hsave(use.Get_surface_ptr()->Get_surface_comps()[i].Get_formula().c_str()); } /* charge */ - last_model.count_surface_charge = (int) use.Get_surface_ptr()->Get_surface_charges().size(); - last_model.surface_charge =(const char **) PHRQ_malloc( use.Get_surface_ptr()->Get_surface_charges().size() * - sizeof(char *)); - if (last_model.surface_charge == NULL) - malloc_error(); + last_model.surface_charge.resize(use.Get_surface_ptr()->Get_surface_charges().size()); for (i = 0; i < (int) use.Get_surface_ptr()->Get_surface_charges().size(); i++) { last_model.surface_charge[i] = string_hsave(use.Get_surface_ptr()->Get_surface_charges()[i].Get_name().c_str()); @@ -5831,10 +5777,8 @@ save_model(void) last_model.dl_type = cxxSurface::NO_DL; /*last_model.edl = -1; */ last_model.surface_type = cxxSurface::UNKNOWN_DL; - last_model.count_surface_comp = 0; - last_model.surface_comp = NULL; - last_model.count_surface_charge = 0; - last_model.surface_charge = NULL; + last_model.surface_comp.clear(); + last_model.surface_charge.clear(); } current_tc = NAN; @@ -5856,9 +5800,9 @@ check_same_model(void) /* * Force new model to be built in prep */ - if (last_model.force_prep == TRUE) + if (last_model.force_prep) { - last_model.force_prep = FALSE; + last_model.force_prep = false; return (FALSE); } if (state == TRANSPORT && cell_data[cell_no].same_model) @@ -5897,12 +5841,10 @@ check_same_model(void) if (use.Get_gas_phase_ptr() != NULL) { cxxGasPhase * gas_phase_ptr = use.Get_gas_phase_ptr(); - if (last_model.gas_phase == NULL) + if (last_model.gas_phase.size() != (int)gas_phase_ptr->Get_gas_comps().size()) return (FALSE); if (last_model.numerical_fixed_volume != numerical_fixed_volume) return (FALSE); - if (last_model.count_gas_phase != (int) gas_phase_ptr->Get_gas_comps().size()) - return (FALSE); if (last_model.gas_phase_type != gas_phase_ptr->Get_type()) return (FALSE); for (i = 0; i < (int) gas_phase_ptr->Get_gas_comps().size(); i++) @@ -5919,7 +5861,7 @@ check_same_model(void) } else { - if (last_model.gas_phase != NULL) + if (last_model.gas_phase.size() > 0) return (FALSE); } /* @@ -5927,7 +5869,7 @@ check_same_model(void) */ if (use.Get_ss_assemblage_ptr() != NULL) { - if (last_model.count_ss_assemblage != (int) use.Get_ss_assemblage_ptr()->Get_SSs().size()) + if (last_model.ss_assemblage.size() != (int) use.Get_ss_assemblage_ptr()->Get_SSs().size()) return (FALSE); std::vector ss_ptrs = use.Get_ss_assemblage_ptr()->Vectorize(); for (size_t i = 0; i < ss_ptrs.size(); i++) @@ -5940,7 +5882,7 @@ check_same_model(void) } else { - if (last_model.ss_assemblage != NULL) + if (last_model.ss_assemblage.size() > 0) return (FALSE); } /* @@ -5949,7 +5891,7 @@ check_same_model(void) if (use.Get_pp_assemblage_ptr() != NULL) { cxxPPassemblage * pp_assemblage_ptr = use.Get_pp_assemblage_ptr(); - if (last_model.count_pp_assemblage != (int) pp_assemblage_ptr->Get_pp_assemblage_comps().size()) + if (last_model.pp_assemblage.size() != (int) pp_assemblage_ptr->Get_pp_assemblage_comps().size()) return (FALSE); std::map::iterator it; @@ -5980,7 +5922,7 @@ check_same_model(void) } else { - if (last_model.pp_assemblage != NULL) + if (last_model.pp_assemblage.size() > 0) return (FALSE); } /* @@ -5988,9 +5930,9 @@ check_same_model(void) */ if (use.Get_surface_ptr() != NULL) { - if (last_model.count_surface_comp != (int) use.Get_surface_ptr()->Get_surface_comps().size()) + if (last_model.surface_comp.size() != (int) use.Get_surface_ptr()->Get_surface_comps().size()) return (FALSE); - if (last_model.count_surface_charge != (int) use.Get_surface_ptr()->Get_surface_charges().size()) + if (last_model.surface_charge.size() != (int) use.Get_surface_ptr()->Get_surface_charges().size()) return (FALSE); if (last_model.dl_type != use.Get_surface_ptr()->Get_dl_type()) return (FALSE); @@ -6041,7 +5983,7 @@ check_same_model(void) } else { - if (last_model.surface_comp != NULL) + if (last_model.surface_comp.size() > 0) return (FALSE); } /* diff --git a/structures.cpp b/structures.cpp index 30635ebc..a5bbc25c 100644 --- a/structures.cpp +++ b/structures.cpp @@ -43,22 +43,13 @@ clean_up(void) (char*)free_check_null(moles_per_kilogram_string); pe_string = (char*)free_check_null(pe_string); /* model */ - last_model.exchange = - (struct master**)free_check_null(last_model.exchange); - last_model.gas_phase = - (struct phase**)free_check_null(last_model.gas_phase); - last_model.pp_assemblage = - (struct phase**)free_check_null(last_model.pp_assemblage); - last_model.ss_assemblage = - (const char**)free_check_null(last_model.ss_assemblage); - last_model.add_formula = - (const char**)free_check_null(last_model.add_formula); - last_model.si = (LDBLE*)free_check_null(last_model.si); - last_model.surface_comp = - (const char**)free_check_null(last_model.surface_comp); - last_model.surface_charge = - (const char**)free_check_null(last_model.surface_charge); - + last_model.gas_phase.clear(); + last_model.pp_assemblage.clear(); + last_model.add_formula.clear(); + last_model.si.clear(); + last_model.ss_assemblage.clear(); + last_model.surface_comp.clear(); + last_model.surface_charge.clear(); /* model */ free_model_allocs(); diff --git a/tidy.cpp b/tidy.cpp index d31278aa..0d2f14d4 100644 --- a/tidy.cpp +++ b/tidy.cpp @@ -5613,30 +5613,15 @@ reset_last_model(void) /* * Initialize model */ - last_model.force_prep = TRUE; - last_model.count_exchange = 0; - last_model.exchange = - (struct master **) free_check_null(last_model.exchange); - last_model.count_gas_phase = 0; - last_model.gas_phase_type = cxxGasPhase::GP_UNKNOWN; - last_model.gas_phase = - (struct phase **) free_check_null(last_model.gas_phase); - last_model.count_ss_assemblage = 0; - last_model.ss_assemblage = - (const char **) free_check_null(last_model.ss_assemblage); - last_model.count_pp_assemblage = 0; - last_model.pp_assemblage = - (struct phase **) free_check_null(last_model.pp_assemblage); - last_model.add_formula = - (const char **) free_check_null(last_model.add_formula); - last_model.si = (LDBLE *) free_check_null(last_model.si); + last_model.force_prep = true; + last_model.gas_phase.clear(); + last_model.ss_assemblage.clear(); + last_model.pp_assemblage.clear(); + last_model.add_formula.clear(); + last_model.si.clear(); last_model.dl_type = cxxSurface::NO_DL; - last_model.count_surface_comp = 0; - last_model.surface_comp = - (const char **) free_check_null(last_model.surface_comp); - last_model.count_surface_charge = 0; - last_model.surface_charge = - (const char **) free_check_null(last_model.surface_charge); + last_model.surface_comp.clear(); + last_model.surface_charge.clear(); return (OK); } /* ---------------------------------------------------------------------- */ diff --git a/transport.cpp b/transport.cpp index 23e45859..5f060834 100644 --- a/transport.cpp +++ b/transport.cpp @@ -397,7 +397,7 @@ transport(void) /* * Set boundary conditions, transport direction */ - last_model.force_prep = TRUE; + last_model.force_prep = true; if ((ishift == 0) || (bcon_first == 1) || (bcon_last == 1)) b_c = 1; else From 3c432d0535ae906b49182a169c79f316c8c172c9 Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Wed, 24 Mar 2021 12:09:31 -0600 Subject: [PATCH 08/53] user_graph commands, alk_list --- ChartObject.cpp | 10 +++------- print.cpp | 29 ++++++++++------------------- 2 files changed, 13 insertions(+), 26 deletions(-) diff --git a/ChartObject.cpp b/ChartObject.cpp index 4932fcdd..230c4805 100644 --- a/ChartObject.cpp +++ b/ChartObject.cpp @@ -87,7 +87,7 @@ cxxNumKeyword(io) point_added = false; user_graph = new rate; - user_graph->commands = NULL; + user_graph->commands.clear(); user_graph->name = NULL; user_graph->new_def = 0; user_graph->linebase = user_graph->loopbase = user_graph->varbase = NULL; @@ -837,11 +837,7 @@ ChartObject::Set_rate_struct(void) oss << *it << "\n"; } this->Rate_free(); - if (this->phreeqc_ptr) - { - this->user_graph->commands = (char *) phreeqc_ptr-> PHRQ_malloc((oss.str().size()) + 100 * sizeof(char)); - } - ::strcpy(this->user_graph->commands, oss.str().c_str()); + this->user_graph->commands = oss.str().c_str(); this->user_graph->new_def = this->rate_new_def; this->user_graph->loopbase = NULL; this->user_graph->varbase = NULL; @@ -1074,7 +1070,7 @@ ChartObject::Rate_free(void) if (this->phreeqc_ptr) { - user_graph->commands = (char *) phreeqc_ptr-> free_check_null(user_graph->commands); + user_graph->commands.clear(); } if (user_graph->linebase != NULL) { diff --git a/print.cpp b/print.cpp index 4a5d84b8..9825b4f1 100644 --- a/print.cpp +++ b/print.cpp @@ -3539,7 +3539,7 @@ punch_user_graph(void) if (chart->Get_rate_new_def()) { if (basic_compile - (chart->Get_user_graph()->commands, &chart->Get_user_graph()->linebase, + (chart->Get_user_graph()->commands.c_str(), &chart->Get_user_graph()->linebase, &chart->Get_user_graph()->varbase, &chart->Get_user_graph()->loopbase) != 0) { error_msg("Fatal Basic error in USER_GRAPH.", STOP); @@ -3606,46 +3606,38 @@ print_alkalinity(void) * Prints description of solution, uses array species_list for * order of aqueous species. */ - int i, j; - struct species_list *alk_list; - int count_alk_list; + int j; + std::vector alk_list; LDBLE min; if (pr.alkalinity == FALSE || pr.all == FALSE) return (OK); print_centered("Distribution of alkalinity"); - alk_list = (struct species_list *) - PHRQ_malloc((s_x.size() * sizeof(struct species_list))); - if (alk_list == NULL) - { - malloc_error(); - return (OK); - } + alk_list.clear(); j = 0; - for (i = 0; i < (int)this->s_x.size(); i++) + for (size_t i = 0; i < this->s_x.size(); i++) { if (s_x[i]->alk == 0.0) continue; + alk_list.resize(alk_list.size() + 1); alk_list[j].master_s = s_hplus; alk_list[j].s = s_x[i]; alk_list[j].coef = s_x[i]->alk; j++; } - count_alk_list = j; min = fabs(censor * total_alkalinity / mass_water_aq_x); - if (count_alk_list > 0) + if (alk_list.size() > 0) { output_msg(sformatf("\t%26s%11.3e\n\n", "Total alkalinity (eq/kgw) = ", (double) (total_alkalinity / mass_water_aq_x))); output_msg(sformatf("\t%-15s%12s%12s%10s\n\n", "Species", "Alkalinity", "Molality", "Alk/Mol")); - if (count_alk_list > 1) qsort(&alk_list[0], (size_t) count_alk_list, + if (alk_list.size() > 1) qsort(&alk_list[0], alk_list.size(), (size_t) sizeof(struct species_list), species_list_compare_alk); - for (i = 0; i < count_alk_list; i++) + for (size_t i = 0; i < alk_list.size(); i++) { - if (fabs - (alk_list[i].s->alk * (alk_list[i].s->moles) / + if (fabs(alk_list[i].s->alk * (alk_list[i].s->moles) / mass_water_aq_x) < min) continue; output_msg(sformatf("\t%-15s%12.3e%12.3e%10.2f\n", @@ -3658,7 +3650,6 @@ print_alkalinity(void) } output_msg(sformatf("\n")); - alk_list = (struct species_list *) free_check_null(alk_list); return (OK); } From 5f21dafa04047ae8086a7c1159ba3dccd3481742 Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Wed, 24 Mar 2021 21:54:37 -0600 Subject: [PATCH 09/53] unknown->master now a vector. Using size instead of a null to end list. --- Phreeqc.h | 8 ++-- global_structures.h | 5 +- model.cpp | 6 +-- prep.cpp | 111 +++++++++++++++----------------------------- print.cpp | 2 +- structures.cpp | 15 ++---- 6 files changed, 50 insertions(+), 97 deletions(-) diff --git a/Phreeqc.h b/Phreeqc.h index 177d64e4..1d434707 100644 --- a/Phreeqc.h +++ b/Phreeqc.h @@ -587,8 +587,7 @@ public: int convert_units(cxxSolution *solution_ptr); LDBLE f_Vm(LDBLE v1); struct unknown *find_surface_charge_unknown(std::string &str_ptr, int plane); - struct master **get_list_master_ptrs(char *ptr, - struct master *master_ptr); + std::vector get_list_master_ptrs(char* ptr, struct master* master_ptr); int inout(void); int is_special(struct species *spec); int mb_for_species_aq(int n); @@ -600,8 +599,8 @@ public: int setup_exchange(void); int setup_gas_phase(void); int setup_fixed_volume_gas(void); - int setup_master_rxn(struct master **master_ptr_list, - const std::string &pe_rxn); + int setup_master_rxn(const std::vector &master_ptr_list, + const std::string& pe_rxn); int setup_pure_phases(void); int adjust_setup_pure_phases(void); int setup_related_surface(void); @@ -619,7 +618,6 @@ public: LDBLE coef, LDBLE * gamma_ptr); int store_sum_deltas(LDBLE * source, LDBLE * target, LDBLE coef); int tidy_redox(void); - struct master **unknown_alloc_master(void); int write_mb_eqn_x(void); int write_mb_for_species_list(int n); int write_mass_action_eqn_x(int stop); diff --git a/global_structures.h b/global_structures.h index 48135711..e88ca75a 100644 --- a/global_structures.h +++ b/global_structures.h @@ -720,7 +720,7 @@ struct unknown LDBLE la; int number; const char *description; - struct master **master; + std::vector master; struct phase *phase; LDBLE si; int n_gas_phase_user; @@ -739,8 +739,7 @@ struct unknown LDBLE related_moles; struct unknown *potential_unknown, *potential_unknown1, *potential_unknown2; - int count_comp_unknowns; - struct unknown **comp_unknowns; /* list for CD_MUSIC of comps that contribute to 0 plane mass-balance term */ + std::vector comp_unknowns; /* list for CD_MUSIC of comps that contribute to 0 plane mass-balance term */ struct unknown *phase_unknown; LDBLE mass_water; int dissolve_only; diff --git a/model.cpp b/model.cpp index 40fff3e2..3b3ec003 100644 --- a/model.cpp +++ b/model.cpp @@ -4235,7 +4235,7 @@ residuals(void) cd_psi.push_back(-(master_ptr2->s->la * LOG_10) * R_KJ_DEG_MOL * tk_x / F_KJ_V_EQ); sum = 0; - for (j = 0; j < x[i]->count_comp_unknowns; j++) + for (size_t j = 0; j < x[i]->comp_unknowns.size(); j++) { sum += x[i]->comp_unknowns[j]->moles * @@ -4880,7 +4880,7 @@ sum_species(void) * * Sums total valence states and stores in master[i]->total. */ - int i, j; + int i; struct master *master_ptr; /* * Set global variables @@ -4962,7 +4962,7 @@ sum_species(void) (x[i]->type == CB && x[i] != ph_unknown && x[i] != pe_unknown)) { x[i]->sum = 0.0; - for (j = 0; x[i]->master[j] != NULL; j++) + for (size_t j = 0; j < x[i]->master.size(); j++) { x[i]->sum += x[i]->master[j]->total; } diff --git a/prep.cpp b/prep.cpp index fccc6e82..caa33534 100644 --- a/prep.cpp +++ b/prep.cpp @@ -832,7 +832,7 @@ build_ss_assemblage(void) { if (x[k]->type != MB) continue; - for (int l = 0; x[k]->master[l] != NULL; l++) + for (size_t l = 0; l < x[k]->master.size(); l++) { if (x[k]->master[l] == master_ptr) { @@ -1527,7 +1527,7 @@ build_pure_phases(void) { if (x[k]->type != MB) continue; - for (int l = 0; x[k]->master[l] != NULL; l++) + for (size_t l = 0; l < x[k]->master.size(); l++) { if (x[k]->master[l] == master_ptr) { @@ -1987,7 +1987,7 @@ convert_units(cxxSolution *solution_ptr) } /* ---------------------------------------------------------------------- */ -struct master ** Phreeqc:: +std::vector Phreeqc:: get_list_master_ptrs(char *ptr, struct master *master_ptr) /* ---------------------------------------------------------------------- */ { @@ -1998,13 +1998,13 @@ get_list_master_ptrs(char *ptr, struct master *master_ptr) */ int j, l, count_list; char token[MAX_LENGTH]; - struct master **master_ptr_list; + std::vector master_ptr_list; struct master *master_ptr0; /* * Make list of master species pointers */ count_list = 0; - master_ptr_list = unknown_alloc_master(); + //master_ptr_list = unknown_alloc_master(); master_ptr0 = master_ptr; if (master_ptr0 == master_ptr->s->primary) { @@ -2022,7 +2022,7 @@ get_list_master_ptrs(char *ptr, struct master *master_ptr) */ if (j >= (int)master.size() || master[j]->elt->primary != master_ptr0) { - master_ptr_list[count_list++] = master_ptr0; + master_ptr_list.push_back(master_ptr0); /* * Element has multiple valences */ @@ -2037,16 +2037,12 @@ get_list_master_ptrs(char *ptr, struct master *master_ptr) error_msg(error_string, CONTINUE); input_error++; } - master_ptr_list[count_list++] = master_ptr0->s->secondary; + master_ptr_list.push_back(master_ptr0->s->secondary); while (j < (int)master.size() && master[j]->elt->primary == master_ptr0) { if (master[j]->s->primary == NULL) { - master_ptr_list = (struct master **) PHRQ_realloc((void *) - master_ptr_list, ((size_t)count_list + 2) * sizeof(struct master *)); - if (master_ptr_list == NULL) - malloc_error(); - master_ptr_list[count_list++] = master[j]; + master_ptr_list.push_back(master[j]); } j++; } @@ -2057,21 +2053,16 @@ get_list_master_ptrs(char *ptr, struct master *master_ptr) /* * First in list is secondary species, Include all valences from input */ - master_ptr_list[count_list++] = master_ptr0; + master_ptr_list.push_back(master_ptr0); while (copy_token(token, &ptr, &l) != EMPTY) { master_ptr = master_bsearch(token); if (master_ptr != NULL) { - master_ptr_list = (struct master **) PHRQ_realloc((void *) master_ptr_list, - ((size_t)count_list + 2) * sizeof(struct master *)); - if (master_ptr_list == NULL) - malloc_error(); - master_ptr_list[count_list++] = master_ptr; + master_ptr_list.push_back(master_ptr); } } } - master_ptr_list[count_list] = NULL; return (master_ptr_list); } @@ -2608,8 +2599,9 @@ resetup_master(void) if (x[i]->type != MB) continue; master_ptr0 = x[i]->master[0]; - for (j = 0; (master_ptr = x[i]->master[j]) != NULL; j++) + for (j = 0; j < x[i]->master.size(); j++) { + master_ptr = x[i]->master[j]; /* * Set flags */ @@ -3165,7 +3157,8 @@ setup_exchange(void) * Fill in data for exchanger in unknowns structures */ struct master *master_ptr; - struct master **master_ptr_list; + //struct master **master_ptr_list; + std::vector master_ptr_list; if (use.Get_exchange_ptr() == NULL) return (OK); @@ -3216,8 +3209,8 @@ setup_exchange(void) /* * Set flags */ - master_ptr_list = unknown_alloc_master(); - master_ptr_list[0] = master_ptr; + master_ptr_list.clear(); + master_ptr_list.push_back(master_ptr); master_ptr->in = TRUE; /* * Set unknown data @@ -3332,7 +3325,7 @@ setup_surface(void) /* * Fill in data for surface assemblage in unknown structure */ - struct master **master_ptr_list; + std::vector master_ptr_list; int mb_unknown_number; if (use.Get_surface_ptr() == NULL) @@ -3374,8 +3367,8 @@ setup_surface(void) /* * Set flags */ - master_ptr_list = unknown_alloc_master(); - master_ptr_list[0] = master_ptr; + master_ptr_list.clear(); + master_ptr_list.push_back(master_ptr); master_ptr->in = TRUE; /* * Setup mass balance unknown @@ -3410,8 +3403,8 @@ setup_surface(void) */ replace("_CB", "_psi", token); master_ptr = master_bsearch(token.c_str()); - master_ptr_list = unknown_alloc_master(); - master_ptr_list[0] = master_ptr; + master_ptr_list.clear(); + master_ptr_list.push_back(master_ptr); master_ptr->in = TRUE; /* * Find surface charge structure @@ -3486,8 +3479,8 @@ setup_surface(void) */ replace(cb_suffix.c_str(), psi_suffix.c_str(), token); master_ptr = master_bsearch(token.c_str()); - master_ptr_list = unknown_alloc_master(); - master_ptr_list[0] = master_ptr; + master_ptr_list.clear(); + master_ptr_list.push_back(master_ptr); master_ptr->in = TRUE; /* * Find surface charge structure @@ -3532,12 +3525,8 @@ setup_surface(void) } /* Add SURFACE unknown to a list for SURF_PSI */ struct unknown *unknown_ptr = find_surface_charge_unknown(token, SURF_PSI); - unknown_ptr->comp_unknowns = (struct unknown **) PHRQ_realloc(unknown_ptr->comp_unknowns, - (((size_t)unknown_ptr->count_comp_unknowns + 1) * sizeof(struct unknown *))); - if (unknown_ptr->comp_unknowns == NULL) - malloc_error(); - unknown_ptr->comp_unknowns[unknown_ptr->count_comp_unknowns++] = - x[mb_unknown_number]; + unknown_ptr->comp_unknowns.push_back(x[mb_unknown_number]); + } } } @@ -3708,21 +3697,21 @@ find_surface_charge_unknown(std::string &str, int plane) } /* ---------------------------------------------------------------------- */ int Phreeqc:: -setup_master_rxn(struct master **master_ptr_list, const std::string &pe_rxn) +setup_master_rxn(const std::vector &master_ptr_list, const std::string &pe_rxn) /* ---------------------------------------------------------------------- */ { /* * Rewrites rxn_secondary for all redox states in list * First, in = TRUE; others, in = REWRITE */ - int j; struct master *master_ptr, *master_ptr0; /* * Set master_ptr->in, master_ptr->rxn */ master_ptr0 = master_ptr_list[0]; - for (j = 0; (master_ptr = master_ptr_list[j]) != NULL; j++) + for (size_t j = 0; j < master_ptr_list.size(); j++) { + master_ptr = master_ptr_list[j]; /* * Check that data not already given */ @@ -4290,7 +4279,7 @@ setup_solution(void) */ x[count_unknowns]->type = MB; x[count_unknowns]->description = string_hsave(it->first.c_str()); - for (int j = 0; x[count_unknowns]->master[j] != NULL; j++) + for (size_t j = 0; j < x[count_unknowns]->master.size(); j++) { x[count_unknowns]->master[j]->unknown = x[count_unknowns]; } @@ -4482,8 +4471,7 @@ setup_solution(void) ah2o_unknown->description = string_hsave("A(H2O)"); ah2o_unknown->type = AH2O; ah2o_unknown->number = count_unknowns; - ah2o_unknown->master = unknown_alloc_master(); - ah2o_unknown->master[0] = master_bsearch("O"); + ah2o_unknown->master.push_back(master_bsearch("O")); ah2o_unknown->master[0]->unknown = ah2o_unknown; ah2o_unknown->moles = 0.0; count_unknowns++; @@ -4499,8 +4487,7 @@ setup_solution(void) ph_unknown->type = CB; ph_unknown->moles = solution_ptr->Get_cb(); ph_unknown->number = count_unknowns; - ph_unknown->master = unknown_alloc_master(); - ph_unknown->master[0] = s_hplus->primary; + ph_unknown->master.push_back(s_hplus->primary); ph_unknown->master[0]->unknown = ph_unknown; charge_balance_unknown = ph_unknown; count_unknowns++; @@ -4518,8 +4505,7 @@ setup_solution(void) mass_hydrogen_unknown->moles = solution_ptr->total_h; #endif mass_hydrogen_unknown->number = count_unknowns; - mass_hydrogen_unknown->master = unknown_alloc_master(); - mass_hydrogen_unknown->master[0] = s_eminus->primary; + mass_hydrogen_unknown->master.push_back(s_eminus->primary); mass_hydrogen_unknown->master[0]->unknown = mass_hydrogen_unknown; count_unknowns++; /* @@ -4530,8 +4516,7 @@ setup_solution(void) mass_oxygen_unknown->type = MH2O; mass_oxygen_unknown->moles = solution_ptr->Get_total_o(); mass_oxygen_unknown->number = count_unknowns; - mass_oxygen_unknown->master = unknown_alloc_master(); - mass_oxygen_unknown->master[0] = s_h2o->primary; + mass_oxygen_unknown->master.push_back(s_h2o->primary); count_unknowns++; } /* @@ -4593,28 +4578,6 @@ adjust_setup_solution(void) return (OK); } -/* ---------------------------------------------------------------------- */ -struct master ** Phreeqc:: -unknown_alloc_master(void) -/* ---------------------------------------------------------------------- */ -{ -/* - * Allocates space for a list of 2 master pointers - */ - struct master **master_ptr; - - master_ptr = (struct master **) PHRQ_malloc(2 * sizeof(struct master *)); - if (master_ptr == NULL) - { - malloc_error(); - } - else - { - master_ptr[0] = NULL; - master_ptr[1] = NULL; - } - return (master_ptr); -} /* ---------------------------------------------------------------------- */ int Phreeqc:: @@ -4939,7 +4902,7 @@ switch_bases(void) * Check if activity of first master species is predominant among activities of * secondary master species included in mass balance. */ - int i, j; + int i; int first; int return_value; LDBLE la, la1; @@ -4954,18 +4917,18 @@ switch_bases(void) break; first = 0; la = x[i]->master[0]->s->la; - for (j = 1; x[i]->master[j] != NULL; j++) + for (size_t j = 1; j < x[i]->master.size(); j++) { la1 = x[i]->master[j]->s->lm + x[i]->master[j]->s->lg; if (first == 0 && la1 > la + 10.) { la = la1; - first = j; + first = (int)j; } else if (first != 0 && la1 > la) { la = la1; - first = j; + first = (int)j; } } if (first != 0) diff --git a/print.cpp b/print.cpp index 9825b4f1..6336766b 100644 --- a/print.cpp +++ b/print.cpp @@ -1919,7 +1919,7 @@ print_surface_cd_music(void) charge2 = unknown_ptr2->f; } sum = 0; - for (int k = 0; k < x[j]->count_comp_unknowns; k++) + for (size_t k = 0; k < x[j]->comp_unknowns.size(); k++) { sum += x[j]->comp_unknowns[k]->moles * diff --git a/structures.cpp b/structures.cpp index a5bbc25c..9ae32a49 100644 --- a/structures.cpp +++ b/structures.cpp @@ -2892,9 +2892,7 @@ unknown_alloc(void) /* * Allocate space */ - unknown_ptr = (struct unknown *) PHRQ_malloc(sizeof(struct unknown)); - if (unknown_ptr == NULL) - malloc_error(); + unknown_ptr = new struct unknown; /* * set pointers in structure to NULL */ @@ -2907,7 +2905,6 @@ unknown_alloc(void) unknown_ptr->la = 0.0; unknown_ptr->number = 0; unknown_ptr->description = NULL; - unknown_ptr->master = NULL; unknown_ptr->phase = NULL; unknown_ptr->si = 0.0; unknown_ptr->s = NULL; @@ -2925,8 +2922,6 @@ unknown_alloc(void) unknown_ptr->potential_unknown = NULL; unknown_ptr->potential_unknown1 = NULL; unknown_ptr->potential_unknown2 = NULL; - unknown_ptr->count_comp_unknowns = 0; - unknown_ptr->comp_unknowns = NULL; unknown_ptr->phase_unknown = NULL; unknown_ptr->surface_charge = NULL; unknown_ptr->mass_water = 0.0; @@ -2964,8 +2959,7 @@ unknown_free(struct unknown *unknown_ptr) */ if (unknown_ptr == NULL) return (ERROR); - unknown_ptr->master = - (struct master **) free_check_null(unknown_ptr->master); + unknown_ptr->master.clear(); if (unknown_ptr->type == SURFACE_CB) { /* @@ -2973,9 +2967,8 @@ unknown_free(struct unknown *unknown_ptr) unknown_ptr->surface_charge = (struct surface_charge *) free_check_null(unknown_ptr->surface_charge); */ } - unknown_ptr->comp_unknowns = - (struct unknown **) free_check_null(unknown_ptr->comp_unknowns); - unknown_ptr = (struct unknown *) free_check_null(unknown_ptr); + unknown_ptr->comp_unknowns.clear(); + delete unknown_ptr; return (OK); } From 48e6b939d29bc47a19a40d6ddd0e704711020e8b Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Wed, 24 Mar 2021 23:06:21 -0600 Subject: [PATCH 10/53] fixed a new master, advection punch_temp and print_temp, some tidying --- Phreeqc.cpp | 7 +++--- prep.cpp | 1 - read.cpp | 58 +++++++++++++++++++++----------------------------- structures.cpp | 47 +++++++++++++++++++--------------------- tally.cpp | 11 ++++------ 5 files changed, 53 insertions(+), 71 deletions(-) diff --git a/Phreeqc.cpp b/Phreeqc.cpp index 151a1fc2..1b981f13 100644 --- a/Phreeqc.cpp +++ b/Phreeqc.cpp @@ -1688,11 +1688,10 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) max_master = MAX_MASTER; */ int count_master = (int)pSrc->master.size(); - for (int i = 0; i < (int)master.size(); i++) + for (size_t i = 0; i < master.size(); i++) { - master.resize((size_t)i + 1); - master[i] = (struct master *) PHRQ_malloc( sizeof(struct master)); - if (master[i] == NULL) malloc_error(); + master.resize(i + 1); + master[i] = new struct master; memcpy(master[i], pSrc->master[i], sizeof(struct master)); // clean up pointers master[i]->gfw_formula = NULL; diff --git a/prep.cpp b/prep.cpp index caa33534..5fbd003b 100644 --- a/prep.cpp +++ b/prep.cpp @@ -3157,7 +3157,6 @@ setup_exchange(void) * Fill in data for exchanger in unknowns structures */ struct master *master_ptr; - //struct master **master_ptr_list; std::vector master_ptr_list; if (use.Get_exchange_ptr() == NULL) diff --git a/read.cpp b/read.cpp index a9bb7898..6378124e 100644 --- a/read.cpp +++ b/read.cpp @@ -7299,8 +7299,7 @@ read_advection(void) char *description; int n_user, n_user_end, i; - int count_punch, count_print; - int *punch_temp, *print_temp; + std::vector punch_temp, print_temp; int return_value, opt, opt_save; char *next_char; const char *opt_list[] = { @@ -7338,14 +7337,6 @@ read_advection(void) count_ad_shifts = 0; print_ad_modulus = 1; punch_ad_modulus = 1; - count_punch = 0; - count_print = 0; - punch_temp = (int *) PHRQ_malloc(sizeof(int)); - if (punch_temp == NULL) - malloc_error(); - print_temp = (int *) PHRQ_malloc(sizeof(int)); - if (print_temp == NULL) - malloc_error(); /* * Read lines */ @@ -7383,11 +7374,16 @@ read_advection(void) break; case 2: /* print */ case 5: /* print_cells */ - print_temp = - read_list_ints_range(&next_char, &count_print, TRUE, - print_temp); + { + std::istringstream iss(next_char); + int idummy; + while (iss >> idummy) + { + print_temp.push_back(idummy); + } opt_save = 2; - break; + } + break; case 3: /* selected_output */ case 11: /* selected_output_frequency */ case 12: /* punch_frequency */ @@ -7404,11 +7400,16 @@ read_advection(void) case 4: /* punch */ case 14: /* punch_cells */ case 6: /* selected_cells */ - punch_temp = - read_list_ints_range(&next_char, &count_punch, TRUE, - punch_temp); + { + std::istringstream iss(next_char); + int idummy; + while (iss >> idummy) + { + punch_temp.push_back(idummy); + } opt_save = 4; break; + } case 7: /* time_step */ case 8: /* timest */ (void)sscanf(next_char, SCANFORMAT, &advection_kin_time); @@ -7464,11 +7465,11 @@ read_advection(void) * Fill in data for punch */ advection_punch.resize((size_t)count_ad_cells + 1); - if (count_punch != 0) + if (punch_temp.size() != 0) { for (i = 0; i < count_ad_cells; i++) advection_punch[i] = FALSE; - for (i = 0; i < count_punch; i++) + for (size_t i = 0; i < punch_temp.size(); i++) { if (punch_temp[i] > count_ad_cells || punch_temp[i] < 1) { @@ -7488,16 +7489,16 @@ read_advection(void) for (i = 0; i < count_ad_cells; i++) advection_punch[i] = TRUE; } - punch_temp = (int *) free_check_null(punch_temp); + punch_temp.clear(); /* * Fill in data for print */ advection_print.resize((size_t)count_ad_cells + 1); - if (count_print != 0) + if (print_temp.size() != 0) { for (i = 0; i < count_ad_cells; i++) advection_print[i] = FALSE; - for (i = 0; i < count_print; i++) + for (i = 0; i < print_temp.size(); i++) { if (print_temp[i] > count_ad_cells || print_temp[i] < 1) { @@ -7517,7 +7518,7 @@ read_advection(void) for (i = 0; i < count_ad_cells; i++) advection_print[i] = TRUE; } - print_temp = (int *) free_check_null(print_temp); + print_temp.clear(); return (return_value); } @@ -8583,17 +8584,6 @@ read_user_punch(void) { r->commands.clear(); } - //rate_free(user_punch); - //user_punch->new_def = TRUE; - //user_punch->commands = (char *) PHRQ_malloc(sizeof(char)); - //if (user_punch->commands == NULL) - // malloc_error(); - //user_punch->commands[0] = '\0'; - //user_punch->linebase = NULL; - //user_punch->varbase = NULL; - //user_punch->loopbase = NULL; - //user_punch->name = - // string_hsave("user defined Basic punch routine"); case OPT_1: /* read command */ r->commands.append(";\0"); r->commands.append(line); diff --git a/structures.cpp b/structures.cpp index 9ae32a49..86ec2842 100644 --- a/structures.cpp +++ b/structures.cpp @@ -417,9 +417,8 @@ elt_list_dup(struct elt_list *elt_list_ptr_old) /* * Malloc space and store element data */ - elt_list_ptr_new = - (struct elt_list *) PHRQ_malloc(((size_t)count_totals + 1) * - sizeof(struct elt_list)); + elt_list_ptr_new = (struct elt_list *) PHRQ_malloc( + ((size_t)count_totals + 1) * sizeof(struct elt_list)); if (elt_list_ptr_new == NULL) malloc_error(); memcpy(elt_list_ptr_new, elt_list_ptr_old, @@ -483,8 +482,8 @@ elt_list_save(void) /* * Malloc space and store element data */ - elt_list_ptr = (struct elt_list*)PHRQ_malloc(((size_t)count_elts + 1) * - sizeof(struct elt_list)); + elt_list_ptr = (struct elt_list*)PHRQ_malloc( + ((size_t)count_elts + 1) * sizeof(struct elt_list)); if (elt_list_ptr == NULL) { malloc_error(); @@ -508,7 +507,8 @@ NameDouble2elt_list(const cxxNameDouble &nd) /* * Takes NameDouble allocates space and fills new elt_list struct */ - struct elt_list *elt_list_ptr = (struct elt_list *) PHRQ_malloc((nd.size() + 1) * sizeof(struct elt_list)); + struct elt_list *elt_list_ptr = (struct elt_list *) PHRQ_malloc( + (nd.size() + 1) * sizeof(struct elt_list)); if (elt_list_ptr == NULL) { malloc_error(); @@ -564,19 +564,19 @@ inverse_alloc(void) * allocate space for pointers in structure to NULL */ - inverse_ptr->uncertainties = (LDBLE *) PHRQ_malloc((size_t) sizeof(LDBLE)); + inverse_ptr->uncertainties = (LDBLE *) PHRQ_malloc(sizeof(LDBLE)); if (inverse_ptr->uncertainties == NULL) { malloc_error(); return inverse_ptr; } - inverse_ptr->ph_uncertainties = (LDBLE *) PHRQ_malloc((size_t) sizeof(LDBLE)); + inverse_ptr->ph_uncertainties = (LDBLE *) PHRQ_malloc(sizeof(LDBLE)); if (inverse_ptr->ph_uncertainties == NULL) { malloc_error(); return inverse_ptr; } - inverse_ptr->force_solns = (int *) PHRQ_malloc((size_t) sizeof(int)); + inverse_ptr->force_solns = (int *) PHRQ_malloc(sizeof(int)); if (inverse_ptr->force_solns == NULL) { malloc_error(); @@ -587,7 +587,7 @@ inverse_alloc(void) inverse_ptr->solns = NULL; - inverse_ptr->elts = (struct inv_elts *) PHRQ_malloc((size_t) sizeof(struct inv_elts)); + inverse_ptr->elts = (struct inv_elts *) PHRQ_malloc(sizeof(struct inv_elts)); if (inverse_ptr->elts == NULL) { malloc_error(); @@ -596,7 +596,7 @@ inverse_alloc(void) inverse_ptr->elts[0].name = NULL; inverse_ptr->elts[0].uncertainties = NULL; - inverse_ptr->isotopes = (struct inv_isotope *) PHRQ_malloc((size_t) + inverse_ptr->isotopes = (struct inv_isotope *) PHRQ_malloc( sizeof(struct inv_isotope)); if (inverse_ptr->isotopes == NULL) { @@ -607,8 +607,7 @@ inverse_alloc(void) inverse_ptr->isotopes[0].isotope_number = 0; inverse_ptr->isotopes[0].elt_name = NULL; - inverse_ptr->i_u = (struct inv_isotope *) PHRQ_malloc((size_t) - sizeof(struct inv_isotope)); + inverse_ptr->i_u = (struct inv_isotope *)PHRQ_malloc(sizeof(struct inv_isotope)); if (inverse_ptr->i_u == NULL) { malloc_error(); @@ -618,7 +617,8 @@ inverse_alloc(void) inverse_ptr->i_u[0].isotope_number = 0; inverse_ptr->i_u[0].elt_name = NULL; - inverse_ptr->phases = (struct inv_phases *) PHRQ_malloc((size_t) sizeof(struct inv_phases)); + inverse_ptr->phases = (struct inv_phases *) PHRQ_malloc( + sizeof(struct inv_phases)); if (inverse_ptr->phases == NULL) { malloc_error(); @@ -1434,8 +1434,7 @@ rate_copy(struct rate *rate_ptr) */ if (rate_ptr == NULL) return (NULL); - struct rate * rate_new = (struct rate *) PHRQ_malloc(sizeof(struct rate)); - if (rate_new == NULL) malloc_error(); + struct rate* rate_new = new struct rate; rate_new->commands = rate_ptr->commands; rate_new->new_def = TRUE; rate_new->linebase = NULL; @@ -1549,9 +1548,8 @@ rxn_alloc(int ntokens) /* * Malloc rxn_token structure */ - rxn_ptr->token = - (struct rxn_token *) PHRQ_malloc((size_t) ntokens * - sizeof(struct rxn_token)); + rxn_ptr->token = (struct rxn_token *) PHRQ_malloc( + (size_t) ntokens * sizeof(struct rxn_token)); for (i = 0; i < ntokens; i++) { rxn_ptr->token[i].s = NULL; @@ -3397,12 +3395,11 @@ copier_init(struct copier *copier_ptr) copier_ptr->count = 0; copier_ptr->max = 10; - copier_ptr->n_user = - (int *) PHRQ_malloc((size_t) (copier_ptr->max * sizeof(int))); - copier_ptr->start = - (int *) PHRQ_malloc((size_t) (copier_ptr->max * sizeof(int))); - copier_ptr->end = - (int *) PHRQ_malloc((size_t) (copier_ptr->max * sizeof(int))); + copier_ptr->n_user = (int *) PHRQ_malloc( + (size_t)copier_ptr->max * sizeof(int)); + copier_ptr->start = (int *) PHRQ_malloc( + (size_t)copier_ptr->max * sizeof(int)); + copier_ptr->end =(int *) PHRQ_malloc((size_t)copier_ptr->max * sizeof(int)); return (OK); } #include "StorageBin.h" diff --git a/tally.cpp b/tally.cpp index af3b6b4c..c998abb1 100644 --- a/tally.cpp +++ b/tally.cpp @@ -117,9 +117,8 @@ get_all_components(void) * Buffer contains an entry for every primary master * species that can be used in the transport problem. */ - t_buffer = - (struct tally_buffer *) PHRQ_malloc((size_t) tally_count_component * - sizeof(struct tally_buffer)); + t_buffer = (struct tally_buffer *) PHRQ_malloc( + (size_t)tally_count_component * sizeof(struct tally_buffer)); // store alkalinity j = 0; @@ -1218,10 +1217,8 @@ extend_tally_table(void) malloc_error(); for (i = 0; i < 3; i++) { - tally_table[count_tally_table_columns].total[i] = - (struct tally_buffer *) - PHRQ_malloc((size_t) (count_tally_table_rows) * - sizeof(struct tally_buffer)); + tally_table[count_tally_table_columns].total[i] = (struct tally_buffer *) + PHRQ_malloc((size_t)count_tally_table_rows * sizeof(struct tally_buffer)); if (tally_table[count_tally_table_columns].total[i] == NULL) malloc_error(); for (j = 0; j < count_tally_table_rows; j++) From 6d67e22e0c3a9fe7537e088408b4f018c4ae6f84 Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Thu, 25 Mar 2021 09:06:29 -0600 Subject: [PATCH 11/53] copier and dash --- Phreeqc.cpp | 23 --- Phreeqc.h | 3 +- global_structures.h | 6 +- mainsubs.cpp | 340 ++++++++++++++------------------------------ structures.cpp | 82 ++--------- utilities.cpp | 17 +-- 6 files changed, 120 insertions(+), 351 deletions(-) diff --git a/Phreeqc.cpp b/Phreeqc.cpp index 1b981f13..529aa61e 100644 --- a/Phreeqc.cpp +++ b/Phreeqc.cpp @@ -491,29 +491,6 @@ void Phreeqc::init(void) *---------------------------------------------------------------------- */ save_init(-1); // set initial save values - // copier structures - copy_solution.n_user = copy_solution.start = copy_solution.end = 0; - copy_solution.count = copy_solution.max = 0; - copy_pp_assemblage.n_user = copy_pp_assemblage.start = copy_pp_assemblage.end = 0; - copy_pp_assemblage.count = copy_pp_assemblage.max = 0; - copy_exchange.n_user = copy_exchange.start = copy_exchange.end = 0; - copy_exchange.count = copy_exchange.max = 0; - copy_surface.n_user = copy_surface.start = copy_surface.end = 0; - copy_surface.count = copy_surface.max = 0; - copy_ss_assemblage.n_user = copy_ss_assemblage.start = copy_ss_assemblage.end = 0; - copy_ss_assemblage.count = copy_ss_assemblage.max = 0; - copy_gas_phase.n_user = copy_gas_phase.start = copy_gas_phase.end = 0; - copy_gas_phase.count = copy_gas_phase.max = 0; - copy_kinetics.n_user = copy_kinetics.start = copy_kinetics.end = 0; - copy_kinetics.count = copy_kinetics.max = 0; - copy_mix.n_user = copy_mix.start = copy_mix.end = 0; - copy_mix.count = copy_mix.max = 0; - copy_reaction.n_user = copy_reaction.start = copy_reaction.end = 0; - copy_reaction.count = copy_reaction.max = 0; - copy_temperature.n_user = copy_temperature.start = copy_temperature.end = 0; - copy_temperature.count = copy_temperature.max = 0; - copy_pressure.n_user = copy_pressure.start = copy_pressure.end = 0; - copy_pressure.count = copy_pressure.max = 0; /*---------------------------------------------------------------------- * Inverse *---------------------------------------------------------------------- */ diff --git a/Phreeqc.h b/Phreeqc.h index 1d434707..7260d14a 100644 --- a/Phreeqc.h +++ b/Phreeqc.h @@ -847,8 +847,7 @@ public: int clean_up(void); int reinitialize(void); int copier_add(struct copier *copier_ptr, int n_user, int start, int end); - int copier_free(struct copier *copier_ptr); - int copier_init(struct copier *copier_ptr); + int copier_clear(struct copier* copier_ptr); static int element_compare(const void *ptr1, const void *ptr2); public: struct element *element_store(const char *element); diff --git a/global_structures.h b/global_structures.h index e88ca75a..80c58635 100644 --- a/global_structures.h +++ b/global_structures.h @@ -300,11 +300,7 @@ struct save *---------------------------------------------------------------------- */ struct copier { - int count; - int max; - int *n_user; - int *start; - int *end; + std::vector n_user, start, end; }; /*---------------------------------------------------------------------- diff --git a/mainsubs.cpp b/mainsubs.cpp index 588394c4..98561920 100644 --- a/mainsubs.cpp +++ b/mainsubs.cpp @@ -92,18 +92,6 @@ initialize(void) g_spread_sheet.defaults.iso = NULL; g_spread_sheet.defaults.redox = NULL; #endif - // allocate space for copier - copier_init(©_solution); - copier_init(©_pp_assemblage); - copier_init(©_exchange); - copier_init(©_surface); - copier_init(©_ss_assemblage); - copier_init(©_gas_phase); - copier_init(©_kinetics); - copier_init(©_mix); - copier_init(©_reaction); - copier_init(©_temperature); - copier_init(©_pressure); // Initialize cvode cvode_init(); @@ -1862,286 +1850,164 @@ int Phreeqc:: copy_entities(void) /* ---------------------------------------------------------------------- */ { - int i, j, return_value; - int verbose; - - verbose = FALSE; + int return_value; return_value = OK; - if (copy_solution.count > 0) + for (size_t j = 0; j < copy_solution.n_user.size(); j++) { - for (j = 0; j < copy_solution.count; j++) + if (Utilities::Rxn_find(Rxn_solution_map, copy_solution.n_user[j]) != NULL) { - if (Utilities::Rxn_find(Rxn_solution_map, copy_solution.n_user[j]) != NULL) + for (size_t i = copy_solution.start[j]; i <= copy_solution.end[j]; i++) { - for (i = copy_solution.start[j]; i <= copy_solution.end[j]; - i++) - { - if (i == copy_solution.n_user[j]) - continue; - Utilities::Rxn_copy(Rxn_solution_map, copy_solution.n_user[j], i); - } - } - else - { - if (verbose == TRUE) - { - warning_msg("SOLUTION to copy not found."); - return_value = ERROR; - } - } - } - } - if (copy_pp_assemblage.count > 0) - { - for (j = 0; j < copy_pp_assemblage.count; j++) - { - if (Utilities::Rxn_find(Rxn_pp_assemblage_map, copy_pp_assemblage.n_user[j]) != NULL) - { - for (i = copy_pp_assemblage.start[j]; - i <= copy_pp_assemblage.end[j]; i++) - { - if (i == copy_pp_assemblage.n_user[j]) - continue; - Utilities::Rxn_copy(Rxn_pp_assemblage_map, copy_pp_assemblage.n_user[j], i); - } - } - else - { - if (verbose == TRUE) - { - warning_msg("EQUILIBRIUM_PHASES to copy not found."); - return_value = ERROR; - } - } - } - } - if (copy_reaction.count > 0) - { - for (j = 0; j < copy_reaction.count; j++) - { - if (Utilities::Rxn_find(Rxn_reaction_map, copy_reaction.n_user[j]) != NULL) - { - for (i = copy_reaction.start[j]; i <= copy_reaction.end[j]; i++) - { - if (i == copy_reaction.n_user[j]) - continue; - Utilities::Rxn_copy(Rxn_reaction_map, copy_reaction.n_user[j], i); - } - } - else - { - if (verbose == TRUE) - { - warning_msg("REACTION to copy not found."); - return_value = ERROR; - } - } - } - } - if (copy_mix.count > 0) - { - for (j = 0; j < copy_mix.count; j++) - { - if (Utilities::Rxn_find(Rxn_mix_map, copy_mix.n_user[j]) != NULL) - { - for (i = copy_mix.start[j]; i <= copy_mix.end[j]; i++) - { - if (i != copy_mix.n_user[j]) - { - Utilities::Rxn_copy(Rxn_mix_map, copy_mix.n_user[j], i); - } - } - } - else - { - if (verbose == TRUE) - { - warning_msg("Mix to copy not found."); - return_value = ERROR; - } + if (i == copy_solution.n_user[j]) + continue; + Utilities::Rxn_copy(Rxn_solution_map, copy_solution.n_user[j], (int)i); } } } + copier_clear(©_solution); - if (copy_exchange.count > 0) + for (size_t j = 0; j < copy_pp_assemblage.n_user.size(); j++) { - for (j = 0; j < copy_exchange.count; j++) + if (Utilities::Rxn_find(Rxn_pp_assemblage_map, copy_pp_assemblage.n_user[j]) != NULL) { - if (Utilities::Rxn_find(Rxn_exchange_map, copy_exchange.n_user[j]) != NULL) + for (size_t i = copy_pp_assemblage.start[j]; i <= copy_pp_assemblage.end[j]; i++) { - for (i = copy_exchange.start[j]; i <= copy_exchange.end[j]; - i++) - { - if (i == copy_exchange.n_user[j]) - continue; - Utilities::Rxn_copy(Rxn_exchange_map, copy_exchange.n_user[j], i); - } - } - else - { - if (verbose == TRUE) - { - warning_msg("EXCHANGE to copy not found."); - return_value = ERROR; - } - } - } - } - if (copy_surface.count > 0) - { - for (j = 0; j < copy_surface.count; j++) - { - if (Utilities::Rxn_find(Rxn_surface_map, copy_surface.n_user[j]) != NULL) - { - for (i = copy_surface.start[j]; i <= copy_surface.end[j]; i++) - { - if (i == copy_surface.n_user[j]) - continue; - Utilities::Rxn_copy(Rxn_surface_map, copy_surface.n_user[j], i); - } - } - else - { - if (verbose == TRUE) - { - warning_msg("SURFACE to copy not found."); - return_value = ERROR; - } + if (i == copy_pp_assemblage.n_user[j]) + continue; + Utilities::Rxn_copy(Rxn_pp_assemblage_map, copy_pp_assemblage.n_user[j], (int)i); } } } + copier_clear(©_pp_assemblage); - if (copy_temperature.count > 0) + for (size_t j = 0; j < copy_reaction.n_user.size(); j++) { - for (j = 0; j < copy_temperature.count; j++) + if (Utilities::Rxn_find(Rxn_reaction_map, copy_reaction.n_user[j]) != NULL) { - if (Utilities::Rxn_find(Rxn_temperature_map, copy_temperature.n_user[j]) != NULL) + for (size_t i = copy_reaction.start[j]; i <= copy_reaction.end[j]; i++) { - for (i = copy_temperature.start[j]; i <= copy_temperature.end[j]; i++) - { - if (i != copy_temperature.n_user[j]) - { - Utilities::Rxn_copy(Rxn_temperature_map, copy_temperature.n_user[j], i); - } - } + if (i == copy_reaction.n_user[j]) + continue; + Utilities::Rxn_copy(Rxn_reaction_map, copy_reaction.n_user[j], (int)i); } - else + } + } + copier_clear(©_reaction); + + for (size_t j = 0; j < copy_mix.n_user.size(); j++) + { + if (Utilities::Rxn_find(Rxn_mix_map, copy_mix.n_user[j]) != NULL) + { + for (size_t i = copy_mix.start[j]; i <= copy_mix.end[j]; i++) { - if (verbose == TRUE) + if (i != copy_mix.n_user[j]) { - warning_msg("temperature to copy not found."); - return_value = ERROR; + Utilities::Rxn_copy(Rxn_mix_map, copy_mix.n_user[j], (int)i); } } } } - if (copy_pressure.count > 0) + copier_clear(©_mix); + + for (size_t j = 0; j < copy_exchange.n_user.size(); j++) { - for (j = 0; j < copy_pressure.count; j++) + if (Utilities::Rxn_find(Rxn_exchange_map, copy_exchange.n_user[j]) != NULL) { - if (Utilities::Rxn_find(Rxn_pressure_map, copy_pressure.n_user[j]) != NULL) + for (size_t i = copy_exchange.start[j]; i <= copy_exchange.end[j]; i++) { - for (i = copy_pressure.start[j]; i <= copy_pressure.end[j]; i++) - { - if (i != copy_pressure.n_user[j]) - { - Utilities::Rxn_copy(Rxn_pressure_map, copy_pressure.n_user[j], i); - } - } + if (i == copy_exchange.n_user[j]) continue; + Utilities::Rxn_copy(Rxn_exchange_map, copy_exchange.n_user[j], (int)i); } - else + } + } + copier_clear(©_exchange); + + for (size_t j = 0; j < copy_surface.n_user.size(); j++) + { + if (Utilities::Rxn_find(Rxn_surface_map, copy_surface.n_user[j]) != NULL) + { + for (size_t i = copy_surface.start[j]; i <= copy_surface.end[j]; i++) { - if (verbose == TRUE) + if (i == copy_surface.n_user[j]) + continue; + Utilities::Rxn_copy(Rxn_surface_map, copy_surface.n_user[j], (int)i); + } + } + } + copier_clear(©_surface); + + for (size_t j = 0; j < copy_temperature.n_user.size(); j++) + { + if (Utilities::Rxn_find(Rxn_temperature_map, copy_temperature.n_user[j]) != NULL) + { + for (size_t i = copy_temperature.start[j]; i <= copy_temperature.end[j]; i++) + { + if (i != copy_temperature.n_user[j]) { - warning_msg("pressure to copy not found."); - return_value = ERROR; + Utilities::Rxn_copy(Rxn_temperature_map, copy_temperature.n_user[j], (int)i); } } } } - if (copy_gas_phase.count > 0) + copier_clear(©_temperature); + + for (size_t j = 0; j < copy_pressure.n_user.size(); j++) { - for (j = 0; j < copy_gas_phase.count; j++) + if (Utilities::Rxn_find(Rxn_pressure_map, copy_pressure.n_user[j]) != NULL) { - if (Utilities::Rxn_find(Rxn_gas_phase_map, copy_gas_phase.n_user[j]) != NULL) + for (size_t i = copy_pressure.start[j]; i <= copy_pressure.end[j]; i++) { - for (i = copy_gas_phase.start[j]; i <= copy_gas_phase.end[j]; - i++) + if (i != copy_pressure.n_user[j]) { - if (i == copy_gas_phase.n_user[j]) - continue; - Utilities::Rxn_copy(Rxn_gas_phase_map, copy_gas_phase.n_user[j], i); - } - } - else - { - if (verbose == TRUE) - { - warning_msg("EXCHANGE to copy not found."); - return_value = ERROR; + Utilities::Rxn_copy(Rxn_pressure_map, copy_pressure.n_user[j], (int)i); } } } } - if (copy_kinetics.count > 0) + copier_clear(©_pressure); + + for (size_t j = 0; j < copy_gas_phase.n_user.size(); j++) { - for (j = 0; j < copy_kinetics.count; j++) + if (Utilities::Rxn_find(Rxn_gas_phase_map, copy_gas_phase.n_user[j]) != NULL) { - if (Utilities::Rxn_find(Rxn_kinetics_map, copy_kinetics.n_user[j]) != NULL) + for (size_t i = copy_gas_phase.start[j]; i <= copy_gas_phase.end[j]; i++) { - for (i = copy_kinetics.start[j]; i <= copy_kinetics.end[j]; - i++) - { - if (i == copy_kinetics.n_user[j]) - continue; - Utilities::Rxn_copy(Rxn_kinetics_map, copy_kinetics.n_user[j], i); - } - } - else - { - if (verbose == TRUE) - { - warning_msg("KINETICS to copy not found."); - return_value = ERROR; - } + if (i == copy_gas_phase.n_user[j]) + continue; + Utilities::Rxn_copy(Rxn_gas_phase_map, copy_gas_phase.n_user[j], (int)i); } } } - if (copy_ss_assemblage.count > 0) + copier_clear(©_gas_phase); + + for (size_t j = 0; j < copy_kinetics.n_user.size(); j++) { - for (j = 0; j < copy_ss_assemblage.count; j++) + if (Utilities::Rxn_find(Rxn_kinetics_map, copy_kinetics.n_user[j]) != NULL) { - if (Utilities::Rxn_find(Rxn_ss_assemblage_map, copy_ss_assemblage.n_user[j]) != NULL) + for (size_t i = copy_kinetics.start[j]; i <= copy_kinetics.end[j]; i++) { - for (i = copy_ss_assemblage.start[j]; - i <= copy_ss_assemblage.end[j]; i++) - { - if (i == copy_ss_assemblage.n_user[j]) - continue; - Utilities::Rxn_copy(Rxn_ss_assemblage_map, copy_ss_assemblage.n_user[j], i); - } - } - else - { - if (verbose == TRUE) - { - warning_msg("SOLID_SOLUTIONS to copy not found."); - return_value = ERROR; - } + if (i == copy_kinetics.n_user[j]) + continue; + Utilities::Rxn_copy(Rxn_kinetics_map, copy_kinetics.n_user[j], (int)i); } } } - copy_solution.count = 0; - copy_pp_assemblage.count = 0; - copy_exchange.count = 0; - copy_surface.count = 0; - copy_ss_assemblage.count = 0; - copy_gas_phase.count = 0; - copy_kinetics.count = 0; - copy_mix.count = 0; - copy_reaction.count = 0; - copy_temperature.count = 0; - copy_pressure.count = 0; + copier_clear(©_kinetics); + + for (size_t j = 0; j < copy_ss_assemblage.n_user.size(); j++) + { + if (Utilities::Rxn_find(Rxn_ss_assemblage_map, copy_ss_assemblage.n_user[j]) != NULL) + { + for (size_t i = copy_ss_assemblage.start[j]; i <= copy_ss_assemblage.end[j]; i++) + { + if (i == copy_ss_assemblage.n_user[j]) + continue; + Utilities::Rxn_copy(Rxn_ss_assemblage_map, copy_ss_assemblage.n_user[j], (int)i); + } + } + } + copier_clear(©_ss_assemblage); + new_copy = FALSE; return return_value; } diff --git a/structures.cpp b/structures.cpp index 86ec2842..ee58ad20 100644 --- a/structures.cpp +++ b/structures.cpp @@ -166,20 +166,6 @@ clean_up(void) llnl_bdh.clear(); llnl_bdot.clear(); llnl_co2_coefs.clear(); - /* - * Copier space - */ - copier_free(©_solution); - copier_free(©_pp_assemblage); - copier_free(©_exchange); - copier_free(©_surface); - copier_free(©_ss_assemblage); - copier_free(©_gas_phase); - copier_free(©_kinetics); - copier_free(©_mix); - copier_free(©_reaction); - copier_free(©_temperature); - copier_free(©_pressure); /* master_isotope */ for (i = 0; i < (int)master_isotope.size(); i++) { @@ -3333,79 +3319,31 @@ copier_add(struct copier *copier_ptr, int n_user, int start, int end) * add new set of copy instructions */ { - - if (copier_ptr->count >= copier_ptr->max) - { - copier_ptr->max = copier_ptr->count * 2; - copier_ptr->n_user = - (int *) PHRQ_realloc(copier_ptr->n_user, - (size_t) (copier_ptr->max * sizeof(int))); - if (copier_ptr->n_user == NULL) - { - malloc_error(); - return (OK); - } - copier_ptr->start = - (int *) PHRQ_realloc(copier_ptr->start, - (size_t) (copier_ptr->max * sizeof(int))); - if (copier_ptr->start == NULL) - { - malloc_error(); - return (OK); - } - copier_ptr->end = - (int *) PHRQ_realloc(copier_ptr->end, - (size_t) (copier_ptr->max * sizeof(int))); - if (copier_ptr->end == NULL) - { - malloc_error(); - return (OK); - } - } - copier_ptr->n_user[copier_ptr->count] = n_user; - copier_ptr->start[copier_ptr->count] = start; - copier_ptr->end[copier_ptr->count] = end; - copier_ptr->count++; + copier_ptr->n_user.push_back(n_user); + copier_ptr->start.push_back(start); + copier_ptr->end.push_back(end); return (OK); } - /* ---------------------------------------------------------------------- */ int Phreeqc:: -copier_free(struct copier *copier_ptr) +copier_clear(struct copier* copier_ptr) /* ---------------------------------------------------------------------- */ /* - * initialize copier structure + * clear copier */ { - - copier_ptr->n_user = (int *) free_check_null(copier_ptr->n_user); - copier_ptr->start = (int *) free_check_null(copier_ptr->start); - copier_ptr->end = (int *) free_check_null(copier_ptr->end); + copier_ptr->n_user.clear(); + copier_ptr->start.clear(); + copier_ptr->end.clear(); return (OK); } -/* ---------------------------------------------------------------------- */ -int Phreeqc:: -copier_init(struct copier *copier_ptr) -/* ---------------------------------------------------------------------- */ -/* - * initialize copier structure - */ -{ - - copier_ptr->count = 0; - copier_ptr->max = 10; - copier_ptr->n_user = (int *) PHRQ_malloc( - (size_t)copier_ptr->max * sizeof(int)); - copier_ptr->start = (int *) PHRQ_malloc( - (size_t)copier_ptr->max * sizeof(int)); - copier_ptr->end =(int *) PHRQ_malloc((size_t)copier_ptr->max * sizeof(int)); - return (OK); -} #include "StorageBin.h" +/* ---------------------------------------------------------------------- */ void Phreeqc:: Use2cxxStorageBin(cxxStorageBin & sb) +/* ---------------------------------------------------------------------- */ { //Add everything from use structure to storagebin sb diff --git a/utilities.cpp b/utilities.cpp index e43125e9..337e07ac 100644 --- a/utilities.cpp +++ b/utilities.cpp @@ -572,31 +572,24 @@ dup_print(const char *ptr, int emphasis) * a row of dashes before and after the character string. * */ - int l, i; - char *dash; + int l; if (pr.headings == FALSE) return (OK); std::string save_in(ptr); l = (int) strlen(ptr); - dash = (char *) PHRQ_malloc(((size_t)l + 2) * sizeof(char)); - if (dash == NULL) - malloc_error(); if (emphasis == TRUE) { - for (i = 0; i < l; i++) - dash[i] = '-'; - dash[i] = '\0'; - output_msg(sformatf("%s\n%s\n%s\n\n", dash, save_in.c_str(), dash)); - log_msg(sformatf("%s\n%s\n%s\n\n", dash, save_in.c_str(), dash)); + std::string dash; + dash.resize(l, '-'); + output_msg(sformatf("%s\n%s\n%s\n\n", dash.c_str(), save_in.c_str(), dash)); + log_msg(sformatf("%s\n%s\n%s\n\n", dash.c_str(), save_in.c_str(), dash)); } else { output_msg(sformatf("%s\n\n", save_in.c_str())); log_msg(sformatf("%s\n\n", save_in.c_str())); } - dash = (char *) free_check_null(dash); - return (OK); } From 380a6eaf06b3cf573a33475bcbe5a188de049bf7 Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Fri, 26 Mar 2021 11:10:09 -0600 Subject: [PATCH 12/53] methods set to const, variables need to follow --- Phreeqc.h | 1241 ++++++++++++++++++++++++------------------------- parse.cpp | 14 +- utilities.cpp | 9 +- 3 files changed, 611 insertions(+), 653 deletions(-) diff --git a/Phreeqc.h b/Phreeqc.h index 7260d14a..064b87e1 100644 --- a/Phreeqc.h +++ b/Phreeqc.h @@ -66,10 +66,10 @@ class PBasic; class Phreeqc { public: - Phreeqc(PHRQ_io *io = NULL); - Phreeqc(const Phreeqc &src); - void InternalCopy(const Phreeqc *pSrc); - Phreeqc &operator=(const Phreeqc &rhs); + Phreeqc(PHRQ_io* io = NULL); + Phreeqc(const Phreeqc& src); + void InternalCopy(const Phreeqc* pSrc); + Phreeqc& operator=(const Phreeqc& rhs); ~Phreeqc(void); public: @@ -81,84 +81,84 @@ public: int advection(void); // basicsubs.cpp ------------------------------- - int basic_compile(const char *commands, void **lnbase, void **vbase, void **lpbase); - int basic_run(char *commands, void *lnbase, void *vbase, void *lpbase); + int basic_compile(const char* commands, void** lnbase, void** vbase, void** lpbase); + int basic_run(char* commands, void* lnbase, void* vbase, void* lpbase); void basic_free(void); #ifdef IPHREEQC_NO_FORTRAN_MODULE - double basic_callback(double x1, double x2, char * str); + double basic_callback(double x1, double x2, char* str); #else - double basic_callback(double x1, double x2, const char * str); + double basic_callback(double x1, double x2, const char* str); #endif - void register_basic_callback(double ( *fcn)(double x1, double x2, const char *str, void *cookie), void *cookie1); + void register_basic_callback(double (*fcn)(double x1, double x2, const char* str, void* cookie), void* cookie1); #ifdef IPHREEQC_NO_FORTRAN_MODULE - void register_fortran_basic_callback(double ( *fcn)(double *x1, double *x2, char *str, size_t l)); + void register_fortran_basic_callback(double (*fcn)(double* x1, double* x2, char* str, size_t l)); #else - void register_fortran_basic_callback(double ( *fcn)(double *x1, double *x2, const char *str, int l)); + void register_fortran_basic_callback(double (*fcn)(double* x1, double* x2, const char* str, int l)); #endif - LDBLE activity(const char *species_name); - LDBLE activity_coefficient(const char *species_name); - LDBLE log_activity_coefficient(const char *species_name); - LDBLE aqueous_vm(const char *species_name); - LDBLE phase_vm(const char *phase_name); - LDBLE diff_c(const char *species_name); - LDBLE setdiff_c(const char *species_name, double d); + LDBLE activity(const char* species_name); + LDBLE activity_coefficient(const char* species_name); + LDBLE log_activity_coefficient(const char* species_name); + LDBLE aqueous_vm(const char* species_name); + LDBLE phase_vm(const char* phase_name); + LDBLE diff_c(const char* species_name); + LDBLE setdiff_c(const char* species_name, double d); LDBLE sa_declercq(double type, double sa, double d, double m, double m0, double gfw); LDBLE calc_SC(void); /* VP: Density Start */ LDBLE calc_dens(void); /* VP: Density End */ - LDBLE calc_logk_n(const char *name); - LDBLE calc_logk_p(const char *name); - LDBLE calc_logk_s(const char *name); + LDBLE calc_logk_n(const char* name); + LDBLE calc_logk_p(const char* name); + LDBLE calc_logk_s(const char* name); LDBLE calc_deltah_s(const char* name); LDBLE calc_deltah_p(const char* name); LDBLE dh_a0(const char* name); LDBLE dh_bdot(const char* name); - LDBLE calc_surface_charge(const char *surface_name); - LDBLE calc_t_sc(const char *name); - LDBLE diff_layer_total(const char *total_name, const char *surface_name); - LDBLE edl_species(const char *surf_name, LDBLE * count, char ***names, LDBLE ** moles, LDBLE * area, LDBLE * thickness); - int get_edl_species(cxxSurfaceCharge & charge_ref); - LDBLE equi_phase(const char *phase_name); - LDBLE equi_phase_delta(const char *phase_name); - LDBLE equivalent_fraction(const char *name, LDBLE *eq, std::string &elt_name); - LDBLE find_gas_comp(const char *gas_comp_name); + LDBLE calc_surface_charge(const char* surface_name); + LDBLE calc_t_sc(const char* name); + LDBLE diff_layer_total(const char* total_name, const char* surface_name); + LDBLE edl_species(const char* surf_name, LDBLE* count, char*** names, LDBLE** moles, LDBLE* area, LDBLE* thickness); + int get_edl_species(cxxSurfaceCharge& charge_ref); + LDBLE equi_phase(const char* phase_name); + LDBLE equi_phase_delta(const char* phase_name); + LDBLE equivalent_fraction(const char* name, LDBLE* eq, std::string& elt_name); + LDBLE find_gas_comp(const char* gas_comp_name); LDBLE find_gas_p(void); LDBLE find_gas_vm(void); - LDBLE find_misc1(const char *ss_name); - LDBLE find_misc2(const char *ss_name); - LDBLE find_ss_comp(const char *ss_comp_name); - LDBLE get_calculate_value(const char *name); - char * iso_unit(const char *total_name); - LDBLE iso_value(const char *total_name); - LDBLE kinetics_moles(const char *kinetics_name); - LDBLE kinetics_moles_delta(const char *kinetics_name); - LDBLE log_activity(const char *species_name); - LDBLE log_molality(const char *species_name); - LDBLE molality(const char *species_name); + LDBLE find_misc1(const char* ss_name); + LDBLE find_misc2(const char* ss_name); + LDBLE find_ss_comp(const char* ss_comp_name); + LDBLE get_calculate_value(const char* name); + char* iso_unit(const char* total_name); + LDBLE iso_value(const char* total_name); + LDBLE kinetics_moles(const char* kinetics_name); + LDBLE kinetics_moles_delta(const char* kinetics_name); + LDBLE log_activity(const char* species_name); + LDBLE log_molality(const char* species_name); + LDBLE molality(const char* species_name); LDBLE pressure(void); - LDBLE pr_pressure(const char *phase_name); - LDBLE pr_phi(const char *phase_name); - LDBLE saturation_ratio(const char *phase_name); - int saturation_index(const char *phase_name, LDBLE * iap, LDBLE * si); + LDBLE pr_pressure(const char* phase_name); + LDBLE pr_phi(const char* phase_name); + LDBLE saturation_ratio(const char* phase_name); + int saturation_index(const char* phase_name, LDBLE* iap, LDBLE* si); int solution_number(void); - LDBLE solution_sum_secondary(const char *total_name); - LDBLE sum_match_gases(const char *stemplate, const char *name); - LDBLE sum_match_species(const char *stemplate, const char *name); - LDBLE sum_match_ss(const char *stemplate, const char *name); - int match_elts_in_species(const char *name, const char *stemplate); - int extract_bracket(char **string, char *bracket_string); - LDBLE surf_total(const char *total_name, const char *surface_name); - LDBLE surf_total_no_redox(const char *total_name, const char *surface_name); - static int system_species_compare(const void *ptr1, const void *ptr2); + LDBLE solution_sum_secondary(const char* total_name); + LDBLE sum_match_gases(const char* stemplate, const char* name); + LDBLE sum_match_species(const char* stemplate, const char* name); + LDBLE sum_match_ss(const char* stemplate, const char* name); + int match_elts_in_species(const char* name, const char* stemplate); + int extract_bracket(const char** string, char* bracket_string); + LDBLE surf_total(const char* total_name, const char* surface_name); + LDBLE surf_total_no_redox(const char* total_name, const char* surface_name); + static int system_species_compare(const void* ptr1, const void* ptr2); static int system_species_compare_name(const void* ptr1, const void* ptr2); - LDBLE system_total(const char *total_name, LDBLE * count, char ***names, - char ***types, LDBLE ** moles, int i); - std::string kinetics_formula(std::string kinetics_name, cxxNameDouble &stoichiometry); - std::string phase_formula(std::string phase_name, cxxNameDouble &stoichiometry); - std::string species_formula(std::string phase_name, cxxNameDouble &stoichiometry); - LDBLE list_ss(std::string ss_name, cxxNameDouble &composition); + LDBLE system_total(const char* total_name, LDBLE* count, char*** names, + char*** types, LDBLE** moles, int i); + std::string kinetics_formula(std::string kinetics_name, cxxNameDouble& stoichiometry); + std::string phase_formula(std::string phase_name, cxxNameDouble& stoichiometry); + std::string species_formula(std::string phase_name, cxxNameDouble& stoichiometry); + LDBLE list_ss(std::string ss_name, cxxNameDouble& composition); int system_total_elements(void); int system_total_si(void); int system_total_aq(void); @@ -168,120 +168,102 @@ public: int system_total_equi(void); int system_total_kin(void); int system_total_ss(void); - int system_total_elt(const char *total_name); - int system_total_elt_secondary(const char *total_name); - LDBLE total(const char *total_name); - LDBLE total_mole(const char *total_name); - int system_total_solids(cxxExchange *exchange_ptr, - cxxPPassemblage *pp_assemblage_ptr, - cxxGasPhase *gas_phase_ptr, - cxxSSassemblage *ss_assemblage_ptr, - cxxSurface *surface_ptr); + int system_total_elt(const char* total_name); + int system_total_elt_secondary(const char* total_name); + LDBLE total(const char* total_name); + LDBLE total_mole(const char* total_name); + int system_total_solids(cxxExchange* exchange_ptr, + cxxPPassemblage* pp_assemblage_ptr, + cxxGasPhase* gas_phase_ptr, + cxxSSassemblage* ss_assemblage_ptr, + cxxSurface* surface_ptr); - static LDBLE f_rho(LDBLE rho_old, void *cookie); - static LDBLE f_Vm(LDBLE v1, void *cookie); + static LDBLE f_rho(LDBLE rho_old, void* cookie); + static LDBLE f_Vm(LDBLE v1, void* cookie); LDBLE calc_solution_volume(void); - // chart.cpp -#if defined PHREEQ98 - void DeleteCurves(void); - void ExtractCurveInfo(char *line, int curvenr); - void GridChar(char *s, char *a); - void MallocCurves(int nc, int ncxy); - int OpenCSVFile(char file_name[MAX_LENGTH]); - void SaveCurvesToFile(char file_name[MAX_LENGTH]); - void PlotXY(char *x, char *y); - void ReallocCurves(int new_nc); - void ReallocCurveXY(int i); - void SetAxisScale(char *a, int j, char *token, int true_); - void SetAxisTitles(char *s, int i); - void SetChartTitle(char *s); - void start_chart(bool end); -#endif - // cl1.cpp ------------------------------- int cl1(int k, int l, int m, int n, int nklmd, int n2d, - LDBLE * q, - int *kode, LDBLE toler, - int *iter, LDBLE * x, LDBLE * res, LDBLE * error, - LDBLE * cu, int *iu, int *s, int check); + LDBLE* q, + int* kode, LDBLE toler, + int* iter, LDBLE* x, LDBLE* res, LDBLE* error, + LDBLE* cu, int* iu, int* s, int check); void cl1_space(int check, int n2d, int klm, int nklmd); // cl1mp.cpp ------------------------------- int cl1mp(int k, int l, int m, int n, int nklmd, int n2d, - LDBLE * q_arg, - int *kode, LDBLE toler, - int *iter, LDBLE * x_arg, LDBLE * res_arg, LDBLE * error, - LDBLE * cu_arg, int *iu, int *s, int check, LDBLE censor_arg); + LDBLE* q_arg, + int* kode, LDBLE toler, + int* iter, LDBLE* x_arg, LDBLE* res_arg, LDBLE* error, + LDBLE* cu_arg, int* iu, int* s, int check, LDBLE censor_arg); // class_main.cpp ------------------------------- int write_banner(void); /* default.cpp */ public: - int close_input_files(void); - int close_output_files(void); - static int istream_getc(void *cookie); - int process_file_names(int argc, char *argv[], std::istream **db_cookie, - std::istream **input_cookie, int log); + //int close_input_files(void); + //int close_output_files(void); + //static int istream_getc(void* cookie); + int process_file_names(int argc, char* argv[], std::istream** db_cookie, + std::istream** input_cookie, int log); /* PHRQ_io_output.cpp */ - void screen_msg(const char * str); + void screen_msg(const char* str); - void echo_msg(const char *err_str); - int warning_msg(const char *err_str); + void echo_msg(const char* err_str); + int warning_msg(const char* err_str); void set_forward_output_to_log(int value); int get_forward_output_to_log(void); // dump_ostream - bool dump_open(const char *file_name); + bool dump_open(const char* file_name); void dump_flush(void); void dump_close(void); - void dump_msg(const char * str); + void dump_msg(const char* str); // log_ostream - bool log_open(const char *file_name); + bool log_open(const char* file_name); void log_flush(void); void log_close(void); - void log_msg(const char * str); + void log_msg(const char* str); // error_ostream - bool error_open(const char *file_name); + bool error_open(const char* file_name); void error_flush(void); void error_close(void); - void error_msg(const char * str, bool stop=false); + void error_msg(const char* str, bool stop = false); // output_ostream - bool output_open(const char *file_name); + bool output_open(const char* file_name); void output_flush(void); void output_close(void); - void output_msg(const char * str); + void output_msg(const char* str); // punch_ostream - bool punch_open(const char *file_name, int n_user); + bool punch_open(const char* file_name, int n_user); void punch_flush(void); void punch_close(void); - void punch_msg(const char * str); + void punch_msg(const char* str); - void fpunchf_heading(const char *name); - void fpunchf(const char *name, const char *format, double d); - void fpunchf(const char *name, const char *format, char * d); - void fpunchf(const char *name, const char *format, int d); - void fpunchf_user(int user_index, const char *format, double d); - void fpunchf_user(int user_index, const char *format, char * d); - int fpunchf_end_row(const char *format); + void fpunchf_heading(const char* name); + void fpunchf(const char* name, const char* format, double d); + void fpunchf(const char* name, const char* format, char* d); + void fpunchf(const char* name, const char* format, int d); + void fpunchf_user(int user_index, const char* format, double d); + void fpunchf_user(int user_index, const char* format, char* d); + int fpunchf_end_row(const char* format); // input.cpp ------------------------------- int reading_database(void); void set_reading_database(int reading_database); - int check_line(const char *string, int allow_empty, int allow_eof, + int check_line(const char* string, int allow_empty, int allow_eof, int allow_keyword, int print); - int add_char_to_line(int *i, char c); - int check_line_impl(const char *string, int allow_empty, + int check_line_impl(const char* string, int allow_empty, int allow_eof, int allow_keyword, int print); int get_line(void); - int get_logical_line(void *cookie, int *l); + //int get_logical_line(void* cookie, int* l); int read_database(void); int run_simulations(void); @@ -289,102 +271,100 @@ public: int calc_all_g(void); int calc_init_g(void); int initial_surface_water(void); - int sum_diffuse_layer(cxxSurfaceCharge *surface_charge_ptr1); + int sum_diffuse_layer(cxxSurfaceCharge* surface_charge_ptr1); int calc_all_donnan(void); int calc_init_donnan(void); LDBLE g_function(LDBLE x_value); LDBLE midpnt(LDBLE x1, LDBLE x2, int n); - void polint(LDBLE * xa, LDBLE * ya, int n, LDBLE xv, LDBLE * yv, - LDBLE * dy); - LDBLE qromb_midpnt(cxxSurfaceCharge *charge_ptr, LDBLE x1, LDBLE x2); - LDBLE calc_psi_avg(cxxSurfaceCharge *charge_ptr, LDBLE surf_chrg_eq); - int calc_all_donnan_music(void); - int calc_init_donnan_music(void); + void polint(LDBLE* xa, LDBLE* ya, int n, LDBLE xv, LDBLE* yv, + LDBLE* dy); + LDBLE qromb_midpnt(cxxSurfaceCharge* charge_ptr, LDBLE x1, LDBLE x2); + LDBLE calc_psi_avg(cxxSurfaceCharge* charge_ptr, LDBLE surf_chrg_eq); // inverse.cpp ------------------------------- int inverse_models(void); - int add_to_file(const char *filename, const char *string); + int add_to_file(const char* filename, const char* string); int bit_print(unsigned long bits, int l); - int carbon_derivs(struct inverse *inv_ptr); - int check_isotopes(struct inverse *inv_ptr); - int check_solns(struct inverse *inv_ptr); - int count_isotope_unknowns(struct inverse *inv_ptr, - struct isotope **isotope_unknowns); - cxxSolutionIsotope *get_isotope(cxxSolution *solution_ptr, const char *elt); - LDBLE get_inv_total(cxxSolution *solution_ptr, const char *elt); - int isotope_balance_equation(struct inverse *inv_ptr, int row, int n); + int carbon_derivs(struct inverse* inv_ptr); + int check_isotopes(struct inverse* inv_ptr); + int check_solns(struct inverse* inv_ptr); + int count_isotope_unknowns(struct inverse* inv_ptr, + struct isotope** isotope_unknowns); + cxxSolutionIsotope* get_isotope(cxxSolution* solution_ptr, const char* elt); + LDBLE get_inv_total(cxxSolution* solution_ptr, const char* elt); + int isotope_balance_equation(struct inverse* inv_ptr, int row, int n); int post_mortem(void); bool test_cl1_solution(void); unsigned long get_bits(unsigned long bits, int position, int number); - unsigned long minimal_solve(struct inverse *inv_ptr, + unsigned long minimal_solve(struct inverse* inv_ptr, unsigned long minimal_bits); - void dump_netpath(struct inverse *inv_ptr); - int dump_netpath_pat(struct inverse *inv_ptr); - int next_set_phases(struct inverse *inv_ptr, int first_of_model_size, + void dump_netpath(struct inverse* inv_ptr); + int dump_netpath_pat(struct inverse* inv_ptr); + int next_set_phases(struct inverse* inv_ptr, int first_of_model_size, int model_size); - int phase_isotope_inequalities(struct inverse *inv_ptr); - int print_model(struct inverse *inv_ptr); - int punch_model_heading(struct inverse *inv_ptr); - int punch_model(struct inverse *inv_ptr); - void print_isotope(FILE * netpath_file, cxxSolution *solution_ptr, - const char *elt, const char *string); - void print_total(FILE * netpath_file, cxxSolution *solution_ptr, - const char *elt, const char *string); - void print_total_multi(FILE * netpath_file, cxxSolution *solution_ptr, - const char *string, const char *elt0, - const char *elt1, const char *elt2, const char *elt3, - const char *elt4); + int phase_isotope_inequalities(struct inverse* inv_ptr); + int print_model(struct inverse* inv_ptr); + int punch_model_heading(struct inverse* inv_ptr); + int punch_model(struct inverse* inv_ptr); + void print_isotope(FILE* netpath_file, cxxSolution* solution_ptr, + const char* elt, const char* string); + void print_total(FILE* netpath_file, cxxSolution* solution_ptr, + const char* elt, const char* string); + void print_total_multi(FILE* netpath_file, cxxSolution* solution_ptr, + const char* string, const char* elt0, + const char* elt1, const char* elt2, const char* elt3, + const char* elt4); - void print_total_pat(FILE * netpath_file, const char *elt, - const char *string); - int range(struct inverse *inv_ptr, unsigned long cur_bits); + void print_total_pat(FILE* netpath_file, const char* elt, + const char* string); + int range(struct inverse* inv_ptr, unsigned long cur_bits); int save_bad(unsigned long bits); int save_good(unsigned long bits); int save_minimal(unsigned long bits); unsigned long set_bit(unsigned long bits, int position, int value); - int setup_inverse(struct inverse *inv_ptr); + int setup_inverse(struct inverse* inv_ptr); int set_initial_solution(int n_user_old, int n_user_new); - int set_ph_c(struct inverse *inv_ptr, - int i, cxxSolution *soln_ptr_orig, int n_user_new, + int set_ph_c(struct inverse* inv_ptr, + int i, cxxSolution* soln_ptr_orig, int n_user_new, LDBLE d_alk, LDBLE ph_factor, LDBLE alk_factor); - int shrink(struct inverse *inv_ptr, LDBLE * array_in, - LDBLE * array_out, int *k, int *l, int *m, int *n, - unsigned long cur_bits, LDBLE * delta_l, int *col_back_l, - int *row_back_l); - int solve_inverse(struct inverse *inv_ptr); - int solve_with_mask(struct inverse *inv_ptr, unsigned long cur_bits); + int shrink(struct inverse* inv_ptr, LDBLE* array_in, + LDBLE* array_out, int* k, int* l, int* m, int* n, + unsigned long cur_bits, LDBLE* delta_l, int* col_back_l, + int* row_back_l); + int solve_inverse(struct inverse* inv_ptr); + int solve_with_mask(struct inverse* inv_ptr, unsigned long cur_bits); int subset_bad(unsigned long bits); int subset_minimal(unsigned long bits); int superset_minimal(unsigned long bits); - int write_optimize_names(struct inverse *inv_ptr); + int write_optimize_names(struct inverse* inv_ptr); // isotopes.cpp ------------------------------- - int add_isotopes(cxxSolution &solution_ptr); + int add_isotopes(cxxSolution& solution_ptr); int calculate_values(void); - int calculate_isotope_moles(struct element *elt_ptr, - cxxSolution *solution_ptr, LDBLE total_moles); - LDBLE convert_isotope(struct master_isotope *master_isotope_ptr, LDBLE ratio); - int from_pcil(struct master_isotope *master_isotope_ptr); - int from_permil(struct master_isotope *master_isotope_ptr, LDBLE major_total); - int from_pct(struct master_isotope *master_isotope_ptr, LDBLE major_total); - int from_tu(struct master_isotope *master_isotope_ptr); - struct calculate_value *calculate_value_alloc(void); - int calculate_value_free(struct calculate_value *calculate_value_ptr); - struct calculate_value *calculate_value_search(const char *name); - struct calculate_value *calculate_value_store(const char *name, + int calculate_isotope_moles(struct element* elt_ptr, + cxxSolution* solution_ptr, LDBLE total_moles); + LDBLE convert_isotope(struct master_isotope* master_isotope_ptr, LDBLE ratio); + int from_pcil(struct master_isotope* master_isotope_ptr); + int from_permil(struct master_isotope* master_isotope_ptr, LDBLE major_total); + int from_pct(struct master_isotope* master_isotope_ptr, LDBLE major_total); + int from_tu(struct master_isotope* master_isotope_ptr); + struct calculate_value* calculate_value_alloc(void); + int calculate_value_free(struct calculate_value* calculate_value_ptr); + struct calculate_value* calculate_value_search(const char* name); + struct calculate_value* calculate_value_store(const char* name, int replace_if_found); - struct isotope_alpha *isotope_alpha_alloc(void); - struct isotope_alpha *isotope_alpha_search(const char *name); - struct isotope_alpha *isotope_alpha_store(const char *name, + struct isotope_alpha* isotope_alpha_alloc(void); + struct isotope_alpha* isotope_alpha_search(const char* name); + struct isotope_alpha* isotope_alpha_store(const char* name, int replace_if_found); - struct isotope_ratio *isotope_ratio_alloc(void); - struct isotope_ratio *isotope_ratio_search(const char *name); - struct isotope_ratio *isotope_ratio_store(const char *name, + struct isotope_ratio* isotope_ratio_alloc(void); + struct isotope_ratio* isotope_ratio_search(const char* name); + struct isotope_ratio* isotope_ratio_store(const char* name, int replace_if_found); - struct master_isotope *master_isotope_store(const char *name, + struct master_isotope* master_isotope_store(const char* name, int replace_if_found); - struct master_isotope *master_isotope_alloc(void); - struct master_isotope *master_isotope_search(const char *name); + struct master_isotope* master_isotope_alloc(void); + struct master_isotope* master_isotope_search(const char* name); int print_initial_solution_isotopes(void); int print_isotope_ratios(void); int print_isotope_alphas(void); @@ -394,10 +374,10 @@ public: int read_isotopes(void); int read_isotope_ratios(void); int read_isotope_alphas(void); - int calculate_value_init(struct calculate_value *calculate_value_ptr); - int isotope_alpha_init(struct isotope_alpha *isotope_alpha_ptr); - int isotope_ratio_init(struct isotope_ratio *isotope_ratio_ptr); - int master_isotope_init(struct master_isotope *master_isotope_ptr); + int calculate_value_init(struct calculate_value* calculate_value_ptr); + int isotope_alpha_init(struct isotope_alpha* isotope_alpha_ptr); + int isotope_ratio_init(struct isotope_ratio* isotope_ratio_ptr); + int master_isotope_init(struct master_isotope* master_isotope_ptr); // kinetics.cpp ------------------------------- void cvode_init(void); @@ -411,16 +391,16 @@ public: int free_cvode(void); public: static void f(integertype N, realtype t, N_Vector y, N_Vector ydot, - void *f_data); - static void Jac(integertype N, DenseMat J, RhsFn f, void *f_data, realtype t, + void* f_data); + static void Jac(integertype N, DenseMat J, RhsFn f, void* f_data, realtype t, N_Vector y, N_Vector fy, N_Vector ewt, realtype h, - realtype uround, void *jac_data, long int *nfePtr, + realtype uround, void* jac_data, long int* nfePtr, N_Vector vtemp1, N_Vector vtemp2, N_Vector vtemp3); - int calc_final_kinetic_reaction(cxxKinetics *kinetics_ptr); - int calc_kinetic_reaction(cxxKinetics *kinetics_ptr, + int calc_final_kinetic_reaction(cxxKinetics* kinetics_ptr); + int calc_kinetic_reaction(cxxKinetics* kinetics_ptr, LDBLE time_step); - bool limit_rates(cxxKinetics *kinetics_ptr); + bool limit_rates(cxxKinetics* kinetics_ptr); int rk_kinetics(int i, LDBLE kin_time, int use_mix, int nsaver, LDBLE step_fraction); int set_reaction(int i, int use_mix, int use_kinetics); @@ -428,15 +408,14 @@ public: int store_get_equi_reactants(int k, int kin_end); // mainsubs.cpp ------------------------------- - std::ifstream * open_input_stream(char *query, char *default_name, std::ios_base::openmode mode, bool batch); - std::ofstream * open_output_stream(char *query, char *default_name, std::ios_base::openmode mode, bool batch); + std::ifstream* open_input_stream(char* query, char* default_name, std::ios_base::openmode mode, bool batch); + std::ofstream* open_output_stream(char* query, char* default_name, std::ios_base::openmode mode, bool batch); int copy_entities(void); void do_mixes(void); void initialize(void); int initial_exchangers(int print); int initial_gas_phases(int print); int initial_solutions(int print); - int solution_mix(void); int step_save_exch(int n_user); int step_save_surf(int n_user); int initial_surfaces(int print); @@ -479,7 +458,7 @@ public: void set_inert_moles(void); void unset_inert_moles(void); #ifdef SLNQ - int add_trivial_eqns(int rows, int cols, LDBLE * matrix); + int add_trivial_eqns(int rows, int cols, LDBLE* matrix); //int slnq(int n, LDBLE * a, LDBLE * delta, int ncols, int print); #endif int calc_gas_pressures(void); @@ -489,46 +468,45 @@ public: int gammas_a_f(int i); int initial_guesses(void); int revise_guesses(void); - int ss_binary(cxxSS *ss_ptr); - int ss_ideal(cxxSS *ss_ptr); + int ss_binary(cxxSS* ss_ptr); + int ss_ideal(cxxSS* ss_ptr); // parse.cpp ------------------------------- int check_eqn(int association); - int get_charge(char *charge, LDBLE * z); - int get_elt(char **t_ptr, char *element, int *i); - int get_elts_in_species(char **t_ptr, LDBLE coef); - int get_num(char **t_ptr, LDBLE * num); - int get_secondary_in_species(char **t_ptr, LDBLE coef); - int parse_eq(char *eqn, struct elt_list **elt_ptr, int association); - int get_coef(LDBLE * coef, char **eqnaddr); - int get_secondary(char **t_ptr, char *element, int *i); - int get_species(char **ptr); + int get_charge(char* charge, LDBLE* z); + int get_elt(const char** t_ptr, char* element, int* i); + int get_elts_in_species(const char** t_ptr, LDBLE coef); + int get_num(const char** t_ptr, LDBLE* num); + int get_secondary_in_species(const char** t_ptr, LDBLE coef); + int parse_eq(char* eqn, struct elt_list** elt_ptr, int association); + int get_coef(LDBLE* coef, const char** eqnaddr); + int get_secondary(const char** t_ptr, char* element, int* i); + int get_species(const char** ptr); // phqalloc.cpp ------------------------------- public: #if !defined(NDEBUG) - void *PHRQ_malloc(size_t, const char *, int); - void *PHRQ_calloc(size_t, size_t, const char *, int); - void *PHRQ_realloc(void *, size_t, const char *, int); + void* PHRQ_malloc(size_t, const char*, int); + void* PHRQ_calloc(size_t, size_t, const char*, int); + void* PHRQ_realloc(void*, size_t, const char*, int); #else - void *PHRQ_malloc(size_t); - void *PHRQ_calloc(size_t, size_t); - void *PHRQ_realloc(void *, size_t); + void* PHRQ_malloc(size_t); + void* PHRQ_calloc(size_t, size_t); + void* PHRQ_realloc(void*, size_t); #endif - void PHRQ_free(void *ptr); + void PHRQ_free(void* ptr); void PHRQ_free_all(void); public: // pitzer.cpp ------------------------------- - struct pitz_param *pitz_param_read(char *string, int n); - void pitz_param_store(struct pitz_param *pzp_ptr, bool force_copy); - void sit_param_store(struct pitz_param *pzp_ptr, bool force_copy); - struct theta_param *theta_param_search(LDBLE zj, LDBLE zk); - struct theta_param *theta_param_alloc(void); - int theta_param_init(struct theta_param *theta_param_ptr); + struct pitz_param* pitz_param_read(char* string, int n); + void pitz_param_store(struct pitz_param* pzp_ptr, bool force_copy); + void sit_param_store(struct pitz_param* pzp_ptr, bool force_copy); + struct theta_param* theta_param_search(LDBLE zj, LDBLE zk); + struct theta_param* theta_param_alloc(void); + int theta_param_init(struct theta_param* theta_param_ptr); void pitzer_make_lists(void); - //int gammas_pz(void); int gammas_pz(bool exch_a_f); int model_pz(void); int pitzer(void); @@ -537,28 +515,25 @@ public: int pitzer_tidy(void); int read_pitzer(void); int set_pz(int initial); - int calc_pitz_param(struct pitz_param *pz_ptr, LDBLE TK, LDBLE TR); - int check_gammas_pz(void); - int ISPEC(const char *name); + int calc_pitz_param(struct pitz_param* pz_ptr, LDBLE TK, LDBLE TR); + int check_gammas_pz(void); + int ISPEC(const char* name); LDBLE G(LDBLE Y); LDBLE GP(LDBLE Y); - int ETHETAS(LDBLE ZJ, LDBLE ZK, LDBLE I, LDBLE * etheta, - LDBLE * ethetap); - void ETHETA_PARAMS(LDBLE X, LDBLE& JAY, LDBLE& JPRIME ); - //int BDK(LDBLE X); + int ETHETAS(LDBLE ZJ, LDBLE ZK, LDBLE I, LDBLE* etheta, + LDBLE* ethetap); + void ETHETA_PARAMS(LDBLE X, LDBLE& JAY, LDBLE& JPRIME); int pitzer_initial_guesses(void); int pitzer_revise_guesses(void); int PTEMP(LDBLE TK); - //LDBLE JAY(LDBLE X); - //LDBLE JPRIME(LDBLE Y); int jacobian_pz(void); // pitzer_structures.cpp ------------------------------- - struct pitz_param *pitz_param_alloc(void); - int pitz_param_init(struct pitz_param *pitz_param_ptr); - struct pitz_param *pitz_param_duplicate(struct pitz_param *old_ptr); - int pitz_param_copy(struct pitz_param *old_ptr, - struct pitz_param *new_ptr); + struct pitz_param* pitz_param_alloc(void); + int pitz_param_init(struct pitz_param* pitz_param_ptr); + struct pitz_param* pitz_param_duplicate(struct pitz_param* old_ptr); + int pitz_param_copy(struct pitz_param* old_ptr, + struct pitz_param* new_ptr); // prep.cpp ------------------------------- int add_potential_factor(void); @@ -576,20 +551,18 @@ public: int build_solution_phase_boundaries(void); int build_species_list(int n); int build_min_surface(void); - LDBLE calc_lk_phase(phase * p_ptr, LDBLE TK, LDBLE pa); - LDBLE calc_delta_v(reaction * r_ptr, bool phase); - LDBLE calc_PR(std::vector phase_ptrs, LDBLE P, LDBLE TK, LDBLE V_m); + LDBLE calc_lk_phase(phase* p_ptr, LDBLE TK, LDBLE pa); + LDBLE calc_delta_v(reaction* r_ptr, bool phase); + LDBLE calc_PR(std::vector phase_ptrs, LDBLE P, LDBLE TK, LDBLE V_m); LDBLE calc_PR(); int calc_vm(LDBLE tc, LDBLE pa); int change_hydrogen_in_elt_list(LDBLE charge); int clear(void); - //int convert_units(struct solution *solution_ptr); - int convert_units(cxxSolution *solution_ptr); - LDBLE f_Vm(LDBLE v1); - struct unknown *find_surface_charge_unknown(std::string &str_ptr, int plane); - std::vector get_list_master_ptrs(char* ptr, struct master* master_ptr); + int convert_units(cxxSolution* solution_ptr); + struct unknown* find_surface_charge_unknown(std::string& str_ptr, int plane); + std::vector get_list_master_ptrs(const char* ptr, struct master* master_ptr); int inout(void); - int is_special(struct species *spec); + int is_special(struct species* spec); int mb_for_species_aq(int n); int mb_for_species_ex(int n); int mb_for_species_surf(int n); @@ -599,7 +572,7 @@ public: int setup_exchange(void); int setup_gas_phase(void); int setup_fixed_volume_gas(void); - int setup_master_rxn(const std::vector &master_ptr_list, + int setup_master_rxn(const std::vector& master_ptr_list, const std::string& pe_rxn); int setup_pure_phases(void); int adjust_setup_pure_phases(void); @@ -609,14 +582,14 @@ public: int adjust_setup_solution(void); int setup_surface(void); int setup_unknowns(void); - int store_dn(int k, LDBLE * source, int row, LDBLE coef_in, - LDBLE * gamma_source); - int store_jacob(LDBLE * source, LDBLE * target, LDBLE coef); + int store_dn(int k, LDBLE* source, int row, LDBLE coef_in, + LDBLE* gamma_source); + int store_jacob(LDBLE* source, LDBLE* target, LDBLE coef); int store_jacob0(int row, int column, LDBLE coef); - int store_mb(LDBLE * source, LDBLE * target, LDBLE coef); - int store_mb_unknowns(struct unknown *unknown_ptr, LDBLE * LDBLE_ptr, - LDBLE coef, LDBLE * gamma_ptr); - int store_sum_deltas(LDBLE * source, LDBLE * target, LDBLE coef); + int store_mb(LDBLE* source, LDBLE* target, LDBLE coef); + int store_mb_unknowns(struct unknown* unknown_ptr, LDBLE* LDBLE_ptr, + LDBLE coef, LDBLE* gamma_ptr); + int store_sum_deltas(LDBLE* source, LDBLE* target, LDBLE coef); int tidy_redox(void); int write_mb_eqn_x(void); int write_mb_for_species_list(int n); @@ -624,30 +597,30 @@ public: int check_same_model(void); int k_temp(LDBLE tc, LDBLE pa); - LDBLE k_calc(LDBLE * logk, LDBLE tempk, LDBLE presPa); + LDBLE k_calc(LDBLE* logk, LDBLE tempk, LDBLE presPa); int prep(void); int reprep(void); - int rewrite_master_to_secondary(struct master *master_ptr1, - struct master *master_ptr2); + int rewrite_master_to_secondary(struct master* master_ptr1, + struct master* master_ptr2); int switch_bases(void); int write_phase_sys_total(int n); // print.cpp ------------------------------- - char *sformatf(const char *format, ...); - int array_print(LDBLE * array_l, int row_count, int column_count, + static char* sformatf(const char* format, ...); + int array_print(LDBLE* array_l, int row_count, int column_count, int max_column_count); int set_pr_in_false(void); int print_all(void); int print_exchange(void); int print_gas_phase(void); int print_master_reactions(void); - int print_reaction(struct reaction *rxn_ptr); + int print_reaction(struct reaction* rxn_ptr); int print_species(void); int print_surface(void); int print_user_print(void); int punch_all(void); int print_alkalinity(void); - int print_diffuse_layer(cxxSurfaceCharge *surface_charge_ptr); + int print_diffuse_layer(cxxSurfaceCharge* surface_charge_ptr); int print_eh(void); int print_reaction(void); int print_kinetics(void); @@ -658,7 +631,6 @@ public: int print_surface_cd_music(void); int print_totals(void); int print_using(void); - /*int print_user_print(void);*/ int punch_gas_phase(void); int punch_identifiers(void); int punch_kinetics(void); @@ -675,39 +647,38 @@ public: // read.cpp ------------------------------- int read_input(void); - int read_conc(cxxSolution *solution_ptr, int count_mass_balance, char *str); - int *read_list_ints_range(char **ptr, int *count_ints, int positive, - int *int_list); - int read_log_k_only(char *ptr, LDBLE * log_k); - int read_t_c_only(char *ptr, LDBLE *t_c); - int read_p_c_only(char *ptr, LDBLE * p_c); - int read_omega_only(char *ptr, LDBLE *omega); - int read_number_description(char *ptr, int *n_user, int *n_user_end, - char **description, int allow_negative=FALSE); - int check_key(const char *str); - int check_units(std::string &tot_units, bool alkalinity, bool check_compatibility, - const char *default_units, bool print); - int find_option(const char *item, int *n, const char **list, int count_list, + int* read_list_ints_range(const char** ptr, int* count_ints, int positive, + int* int_list); + int read_log_k_only(const char* ptr, LDBLE* log_k); + int read_t_c_only(const char* ptr, LDBLE* t_c); + int read_p_c_only(const char* ptr, LDBLE* p_c); + int read_omega_only(const char* ptr, LDBLE* omega); + int read_number_description(const char* ptr, int* n_user, int* n_user_end, + char** description, int allow_negative = FALSE); + int check_key(const char* str); + int check_units(std::string& tot_units, bool alkalinity, bool check_compatibility, + const char* default_units, bool print); + int find_option(const char* item, int* n, const char** list, int count_list, int exact); - int get_option(const char **opt_list, int count_opt_list, char **next_char); - int get_true_false(char *string, int default_value); + int get_option(const char** opt_list, int count_opt_list, const char** next_char); + int get_true_false(const char* string, int default_value); - int add_psi_master_species(char *token); + int add_psi_master_species(char* token); int read_advection(void); - int read_analytical_expression_only(char *ptr, LDBLE * log_k); + int read_analytical_expression_only(const char* ptr, LDBLE* log_k); /* VP: Density Start */ - int read_millero_abcdef (char *ptr, LDBLE * abcdef); + int read_millero_abcdef(const char* ptr, LDBLE* abcdef); /* VP: Density End */ - int read_viscosity_parms(char *ptr, LDBLE * Jones_Dole); + int read_viscosity_parms(const char* ptr, LDBLE* Jones_Dole); int read_copy(void); int read_debug(void); - int read_delta_h_only(char *ptr, LDBLE * delta_h, - DELTA_H_UNIT * units); - int read_aq_species_vm_parms(char *ptr, LDBLE * delta_v); - int read_vm_only(char *ptr, LDBLE * delta_v, - DELTA_V_UNIT * units); - int read_phase_vm(char *ptr, LDBLE * delta_v, - DELTA_V_UNIT * units); + int read_delta_h_only(const char* ptr, LDBLE* delta_h, + DELTA_H_UNIT* units); + int read_aq_species_vm_parms(const char* ptr, LDBLE* delta_v); + int read_vm_only(const char* ptr, LDBLE* delta_v, + DELTA_V_UNIT* units); + int read_phase_vm(const char* ptr, LDBLE* delta_v, + DELTA_V_UNIT* units); int read_llnl_aqueous_model_parameters(void); int read_exchange(void); int read_exchange_master_species(void); @@ -715,16 +686,16 @@ public: int read_gas_phase(void); int read_incremental_reactions(void); int read_inverse(void); - int read_inv_balances(struct inverse *inverse_ptr, char *next_char); - int read_inv_isotopes(struct inverse *inverse_ptr, char *ptr); - int read_inv_phases(struct inverse *inverse_ptr, char *next_char); + int read_inv_balances(struct inverse* inverse_ptr, const char* next_char); + int read_inv_isotopes(struct inverse* inverse_ptr, const char* ptr); + int read_inv_phases(struct inverse* inverse_ptr, const char* next_char); int read_kinetics(void); - LDBLE *read_list_doubles(char **ptr, int *count_doubles); - int *read_list_ints(char **ptr, int *count_ints, int positive); - int *read_list_t_f(char **ptr, int *count_ints); + LDBLE* read_list_doubles(const char** ptr, int* count_doubles); + int* read_list_ints(const char** ptr, int* count_ints, int positive); + int* read_list_t_f(const char** ptr, int* count_ints); int read_master_species(void); int read_mix(void); - int read_entity_mix(std::map &mix_map); + int read_entity_mix(std::map& mix_map); //int read_solution_mix(void); int read_named_logk(void); int read_phases(void); @@ -732,11 +703,11 @@ public: int read_pp_assemblage(void); int read_rates(void); int read_reaction(void); - int read_reaction_reactants(cxxReaction *reaction_ptr); - int read_reaction_steps(cxxReaction *reaction_ptr); + int read_reaction_reactants(cxxReaction* reaction_ptr); + int read_reaction_steps(cxxReaction* reaction_ptr); int read_solid_solutions(void); int read_temperature(void); - int read_reaction_temps(struct temperature *temperature_ptr); + //int read_reaction_temps(struct temperature* temperature_ptr); int read_reaction_pressure(void); int read_reaction_pressure_raw(void); int read_save(void); @@ -750,20 +721,17 @@ public: int read_title(void); int read_user_print(void); int read_user_punch(void); -#if defined PHREEQ98 - int read_user_graph(void); -#endif #if defined MULTICHART int read_user_graph_handler(); #endif - int next_keyword_or_option(const char **opt_list, int count_opt_list); - int cleanup_after_parser(CParser &parser); + int next_keyword_or_option(const char** opt_list, int count_opt_list); + int cleanup_after_parser(CParser& parser); // ReadClass.cxx int read_dump(void); int read_delete(void); int read_run_cells(void); - int streamify_to_next_keyword(std::istringstream & lines); + int streamify_to_next_keyword(std::istringstream& lines); int dump_entities(void); int delete_entities(void); int run_as_cells(void); @@ -772,18 +740,18 @@ public: // readtr.cpp ------------------------------- int read_transport(void); int dump(void); - int dump_exchange(int k); - int dump_gas_phase(int k); - int dump_kinetics(int k); - int dump_mix(int k); - int dump_pp_assemblage(int k); - int dump_reaction(int k); - int dump_ss_assemblage(int k); - int dump_solution(int k); - int dump_surface(int k); + //int dump_exchange(int k); + //int dump_gas_phase(int k); + //int dump_kinetics(int k); + //int dump_mix(int k); + //int dump_pp_assemblage(int k); + //int dump_reaction(int k); + //int dump_ss_assemblage(int k); + //int dump_solution(int k); + //int dump_surface(int k); int dump_cpp(void); - int read_line_LDBLEs(char *next_char, LDBLE ** d, int *count_d, - int *count_alloc); + int read_line_LDBLEs(const char* next_char, LDBLE** d, int* count_d, + int* count_alloc); // sit.cpp ------------------------------- int gammas_sit(void); @@ -794,9 +762,9 @@ public: int sit_tidy(void); int read_sit(void); int set_sit(int initial); - int calc_sit_param(struct pitz_param *pz_ptr, LDBLE TK, LDBLE TR); + int calc_sit_param(struct pitz_param* pz_ptr, LDBLE TK, LDBLE TR); int check_gammas_sit(void); - int sit_ISPEC(const char *name); + int sit_ISPEC(const char* name); /*int DH_AB (LDBLE TK, LDBLE *A, LDBLE *B);*/ int sit_initial_guesses(void); int sit_revise_guesses(void); @@ -806,184 +774,184 @@ public: // spread.cpp ------------------------------- int read_solution_spread(void); - int copy_token_tab(char *token_ptr, char **ptr, int *length); - int get_option_string(const char **opt_list, int count_opt_list, - char **next_char); - int spread_row_free(struct spread_row *spread_row_ptr); - int spread_row_to_solution(struct spread_row *heading, - struct spread_row *units, - struct spread_row *data, + int copy_token_tab(char* token_ptr, const char** ptr, int* length); + int get_option_string(const char** opt_list, int count_opt_list, + const char** next_char); + int spread_row_free(struct spread_row* spread_row_ptr); + int spread_row_to_solution(struct spread_row* heading, + struct spread_row* units, + struct spread_row* data, struct defaults defaults); - struct spread_row *string_to_spread_row(char *string); + struct spread_row* string_to_spread_row(char* string); #ifdef PHREEQCI_GUI - void add_row(struct spread_row *spread_row_ptr); - void copy_defaults(struct defaults *dest_ptr, - struct defaults *src_ptr); + void add_row(struct spread_row* spread_row_ptr); + void copy_defaults(struct defaults* dest_ptr, + struct defaults* src_ptr); void free_spread(void); - struct spread_row *copy_row(struct spread_row *spread_row_ptr); + struct spread_row* copy_row(struct spread_row* spread_row_ptr); #endif // step.cpp ------------------------------- int step(LDBLE step_fraction); int xsolution_zero(void); - int add_exchange(cxxExchange *exchange_ptr); - int add_gas_phase(cxxGasPhase *gas_phase_ptr); - int add_kinetics(cxxKinetics *kinetics_ptr); - int add_mix(cxxMix * mix_ptr); - int add_pp_assemblage(cxxPPassemblage *pp_assemblage_ptr); - int add_reaction(cxxReaction *reaction_ptr, int step_number, LDBLE step_fraction); - int add_ss_assemblage(cxxSSassemblage *ss_assemblage_ptr); - int add_solution(cxxSolution *solution_ptr, LDBLE extensive, + int add_exchange(cxxExchange* exchange_ptr); + int add_gas_phase(cxxGasPhase* gas_phase_ptr); + int add_kinetics(cxxKinetics* kinetics_ptr); + int add_mix(cxxMix* mix_ptr); + int add_pp_assemblage(cxxPPassemblage* pp_assemblage_ptr); + int add_reaction(cxxReaction* reaction_ptr, int step_number, LDBLE step_fraction); + int add_ss_assemblage(cxxSSassemblage* ss_assemblage_ptr); + int add_solution(cxxSolution* solution_ptr, LDBLE extensive, LDBLE intensive); - int add_surface(cxxSurface *surface_ptr); - int check_pp_assemblage(cxxPPassemblage *pp_assemblage_ptr); - int gas_phase_check(cxxGasPhase *gas_phase_ptr); - int pp_assemblage_check(cxxPPassemblage *pp_assemblage_ptr); - int reaction_calc(cxxReaction *reaction_ptr); + int add_surface(cxxSurface* surface_ptr); + int check_pp_assemblage(cxxPPassemblage* pp_assemblage_ptr); + int gas_phase_check(cxxGasPhase* gas_phase_ptr); + int pp_assemblage_check(cxxPPassemblage* pp_assemblage_ptr); + int reaction_calc(cxxReaction* reaction_ptr); int solution_check(void); - int ss_assemblage_check(cxxSSassemblage *ss_assemblage_ptr); + int ss_assemblage_check(cxxSSassemblage* ss_assemblage_ptr); // structures.cpp ------------------------------- int clean_up(void); int reinitialize(void); - int copier_add(struct copier *copier_ptr, int n_user, int start, int end); + int copier_add(struct copier* copier_ptr, int n_user, int start, int end); int copier_clear(struct copier* copier_ptr); - static int element_compare(const void *ptr1, const void *ptr2); + static int element_compare(const void* ptr1, const void* ptr2); public: - struct element *element_store(const char *element); + struct element* element_store(const char* element); int elt_list_combine(void); - static int elt_list_compare(const void *ptr1, const void *ptr2); + static int elt_list_compare(const void* ptr1, const void* ptr2); protected: - struct elt_list *elt_list_dup(struct elt_list *elt_list_ptr_old); - int elt_list_print(struct elt_list *elt_list_ptr); - struct elt_list *elt_list_save(void); + struct elt_list* elt_list_dup(struct elt_list* elt_list_ptr_old); + int elt_list_print(struct elt_list* elt_list_ptr); + struct elt_list* elt_list_save(void); cxxNameDouble elt_list_NameDouble(void); - struct elt_list * NameDouble2elt_list(const cxxNameDouble &nd); + struct elt_list* NameDouble2elt_list(const cxxNameDouble& nd); public: - enum entity_type get_entity_enum(char *name); - struct inverse *inverse_alloc(void); + enum entity_type get_entity_enum(char* name); + struct inverse* inverse_alloc(void); int inverse_delete(int i); - static int inverse_isotope_compare(const void *ptr1, const void *ptr2); - struct inverse *inverse_search(int n_user, int *n); + static int inverse_isotope_compare(const void* ptr1, const void* ptr2); + struct inverse* inverse_search(int n_user, int* n); int inverse_sort(void); protected: - struct logk *logk_alloc(void); - int logk_copy2orig(struct logk *logk_ptr); - struct logk *logk_store(const char *name, int replace_if_found); - struct logk *logk_search(const char *name); - struct master *master_alloc(void); - static int master_compare(const void *ptr1, const void *ptr2); - int master_delete(char *ptr); + struct logk* logk_alloc(void); + int logk_copy2orig(struct logk* logk_ptr); + struct logk* logk_store(const char* name, int replace_if_found); + struct logk* logk_search(const char* name); + struct master* master_alloc(void); + static int master_compare(const void* ptr1, const void* ptr2); + int master_delete(const char* ptr); public: - struct master *master_bsearch(const char *ptr); - struct master *master_bsearch_primary(const char *ptr); - struct master *master_bsearch_secondary(char *ptr); - struct master *master_search(char *ptr, int *n); - struct pe_data *pe_data_alloc(void); + struct master* master_bsearch(const char* ptr); + struct master* master_bsearch_primary(const char* ptr); + struct master* master_bsearch_secondary(const char* ptr); + struct master* master_search(const char* ptr, int* n); + struct pe_data* pe_data_alloc(void); public: - struct pe_data *pe_data_dup(struct pe_data *pe_ptr_old); - struct pe_data *pe_data_free(struct pe_data *pe_data_ptr); + struct pe_data* pe_data_dup(struct pe_data* pe_ptr_old); + struct pe_data* pe_data_free(struct pe_data* pe_data_ptr); protected: - int pe_data_store(struct pe_data **pe, const char *token); + int pe_data_store(struct pe_data** pe, const char* token); public: - struct phase *phase_bsearch(const char *ptr, int *j, int print); + struct phase* phase_bsearch(const char* ptr, int* j, int print); protected: - static int phase_compare(const void *ptr1, const void *ptr2); + static int phase_compare(const void* ptr1, const void* ptr2); int phase_delete(int i); - struct phase *phase_store(const char *name); + struct phase* phase_store(const char* name); public: - struct rate *rate_bsearch(char *ptr, int *j); - int rate_free(struct rate *rate_ptr); - struct rate * rate_copy(struct rate *rate_ptr); - struct rate *rate_search(const char *name, int *n); + struct rate* rate_bsearch(const char* ptr, int* j); + int rate_free(struct rate* rate_ptr); + struct rate* rate_copy(struct rate* rate_ptr); + struct rate* rate_search(const char* name, int* n); int rate_sort(void); - struct reaction *rxn_alloc(int ntokens); - struct reaction *rxn_dup(struct reaction *rxn_ptr_old); - struct reaction * cxxChemRxn2rxn(cxxChemRxn &cr); - LDBLE rxn_find_coef(struct reaction *r_ptr, const char *str); - int rxn_free(struct reaction *rxn_ptr); - int rxn_print(struct reaction *rxn_ptr); - static int s_compare(const void *ptr1, const void *ptr2); + struct reaction* rxn_alloc(int ntokens); + struct reaction* rxn_dup(struct reaction* rxn_ptr_old); + struct reaction* cxxChemRxn2rxn(cxxChemRxn& cr); + LDBLE rxn_find_coef(struct reaction* r_ptr, const char* str); + int rxn_free(struct reaction* rxn_ptr); + int rxn_print(struct reaction* rxn_ptr); + static int s_compare(const void* ptr1, const void* ptr2); int s_delete(int i); - struct species *s_search(const char *name); - struct species *s_store(const char *name, LDBLE z, int replace_if_found); + struct species* s_search(const char* name); + struct species* s_store(const char* name, LDBLE z, int replace_if_found); protected: - struct save_values *save_values_bsearch(struct save_values *k, int *n); - static int save_values_compare(const void *ptr1, const void *ptr2); + struct save_values* save_values_bsearch(struct save_values* k, int* n); + static int save_values_compare(const void* ptr1, const void* ptr2); int save_values_sort(void); - int save_values_store(struct save_values *s_v); - static int isotope_compare(const void *ptr1, const void *ptr2); - static int species_list_compare_alk(const void *ptr1, const void *ptr2); - static int species_list_compare_master(const void *ptr1, const void *ptr2); + int save_values_store(struct save_values* s_v); + static int isotope_compare(const void* ptr1, const void* ptr2); + static int species_list_compare_alk(const void* ptr1, const void* ptr2); + static int species_list_compare_master(const void* ptr1, const void* ptr2); int species_list_sort(void); - struct Change_Surf *change_surf_alloc(int count); + struct Change_Surf* change_surf_alloc(int count); public: - struct master *surface_get_psi_master(const char *name, int plane); + struct master* surface_get_psi_master(const char* name, int plane); int system_duplicate(int i, int save_old); - int trxn_add(struct reaction *r_ptr, LDBLE coef, int combine); - int trxn_add(cxxChemRxn &r_ptr, LDBLE coef, int combine); - int trxn_add_phase(struct reaction *r_ptr, LDBLE coef, int combine); + int trxn_add(struct reaction* r_ptr, LDBLE coef, int combine); + int trxn_add(cxxChemRxn& r_ptr, LDBLE coef, int combine); + int trxn_add_phase(struct reaction* r_ptr, LDBLE coef, int combine); int trxn_combine(void); - int trxn_copy(struct reaction *rxn_ptr); - LDBLE trxn_find_coef(const char *str, int start); + int trxn_copy(struct reaction* rxn_ptr); + LDBLE trxn_find_coef(const char* str, int start); int trxn_print(void); int trxn_reverse_k(void); int trxn_sort(void); - int trxn_swap(const char *token); - struct unknown *unknown_alloc(void); + int trxn_swap(const char* token); + struct unknown* unknown_alloc(void); int unknown_delete(int i); - int unknown_free(struct unknown *unknown_ptr); - int entity_exists(const char *name, int n_user); - static int inverse_compare(const void *ptr1, const void *ptr2); - int inverse_free(struct inverse *inverse_ptr); - static int kinetics_compare_int(const void *ptr1, const void *ptr2); - int logk_init(struct logk *logk_ptr); - static int master_compare_string(const void *ptr1, const void *ptr2); - int master_free(struct master *master_ptr); - struct phase *phase_alloc(void); - static int phase_compare_string(const void *ptr1, const void *ptr2); - int phase_free(struct phase *phase_ptr); - int phase_init(struct phase *phase_ptr); - static int rate_compare(const void *ptr1, const void *ptr2); - static int rate_compare_string(const void *ptr1, const void *ptr2); - struct species *s_alloc(void); - int s_free(struct species *s_ptr); - int s_init(struct species *s_ptr); - static int ss_assemblage_compare_int(const void *ptr1, const void *ptr2); - static int solution_compare(const void *ptr1, const void *ptr2); - static int solution_compare_int(const void *ptr1, const void *ptr2); - static int species_list_compare(const void *ptr1, const void *ptr2); - static int surface_compare_int(const void *ptr1, const void *ptr2); - static int rxn_token_temp_compare(const void *ptr1, const void *ptr2); + int unknown_free(struct unknown* unknown_ptr); + int entity_exists(const char* name, int n_user); + static int inverse_compare(const void* ptr1, const void* ptr2); + int inverse_free(struct inverse* inverse_ptr); + static int kinetics_compare_int(const void* ptr1, const void* ptr2); + int logk_init(struct logk* logk_ptr); + static int master_compare_string(const void* ptr1, const void* ptr2); + int master_free(struct master* master_ptr); + struct phase* phase_alloc(void); + static int phase_compare_string(const void* ptr1, const void* ptr2); + int phase_free(struct phase* phase_ptr); + int phase_init(struct phase* phase_ptr); + static int rate_compare(const void* ptr1, const void* ptr2); + static int rate_compare_string(const void* ptr1, const void* ptr2); + struct species* s_alloc(void); + int s_free(struct species* s_ptr); + int s_init(struct species* s_ptr); + static int ss_assemblage_compare_int(const void* ptr1, const void* ptr2); + static int solution_compare(const void* ptr1, const void* ptr2); + static int solution_compare_int(const void* ptr1, const void* ptr2); + static int species_list_compare(const void* ptr1, const void* ptr2); + static int surface_compare_int(const void* ptr1, const void* ptr2); + static int rxn_token_temp_compare(const void* ptr1, const void* ptr2); int trxn_multiply(LDBLE coef); - struct elt_list * cxxNameDouble2elt_list(const cxxNameDouble * nd); - struct name_coef * cxxNameDouble2name_coef(const cxxNameDouble * nd); - struct master_activity * cxxNameDouble2master_activity(const cxxNameDouble * nd); - struct master * cxxNameDouble2surface_master(const cxxNameDouble * totals); + struct elt_list* cxxNameDouble2elt_list(const cxxNameDouble* nd); + struct name_coef* cxxNameDouble2name_coef(const cxxNameDouble* nd); + struct master_activity* cxxNameDouble2master_activity(const cxxNameDouble* nd); + struct master* cxxNameDouble2surface_master(const cxxNameDouble* totals); - void Use2cxxStorageBin(cxxStorageBin & sb); - void phreeqc2cxxStorageBin(cxxStorageBin & sb); - void phreeqc2cxxStorageBin(cxxStorageBin & sb, int n); - void cxxStorageBin2phreeqc(cxxStorageBin & sb, int n); - void cxxStorageBin2phreeqc(cxxStorageBin & sb); + void Use2cxxStorageBin(cxxStorageBin& sb); + void phreeqc2cxxStorageBin(cxxStorageBin& sb); + void phreeqc2cxxStorageBin(cxxStorageBin& sb, int n); + void cxxStorageBin2phreeqc(cxxStorageBin& sb, int n); + void cxxStorageBin2phreeqc(cxxStorageBin& sb); /* tally.cpp */ void add_all_components_tally(void); int build_tally_table(void); - int calc_dummy_kinetic_reaction_tally(cxxKinetics *kinetics_ptr); + int calc_dummy_kinetic_reaction_tally(cxxKinetics* kinetics_ptr); int diff_tally_table(void); int extend_tally_table(void); int free_tally_table(void); - int fill_tally_table(int *n_user, int index_conservative, int n_buffer); - int get_tally_table_rows_columns(int *rows, int *columns); - int get_tally_table_column_heading(int column, int *type, char *string); - int get_tally_table_row_heading(int column, char *string); - int store_tally_table(LDBLE * array, int row_dim, int col_dim, + int fill_tally_table(int* n_user, int index_conservative, int n_buffer); + int get_tally_table_rows_columns(int* rows, int* columns); + int get_tally_table_column_heading(int column, int* type, char* string); + int get_tally_table_row_heading(int column, char* string); + int store_tally_table(LDBLE* array, int row_dim, int col_dim, LDBLE fill_factor); int zero_tally_table(void); - int elt_list_to_tally_table(struct tally_buffer *buffer_ptr); - int master_to_tally_table(struct tally_buffer *buffer_ptr); + int elt_list_to_tally_table(struct tally_buffer* buffer_ptr); + int master_to_tally_table(struct tally_buffer* buffer_ptr); int get_all_components(void); int print_tally_table(void); int set_reaction_moles(int n_user, LDBLE moles); @@ -991,24 +959,24 @@ public: int set_kinetics_time(int n_user, LDBLE step); // tidy.cpp ------------------------------- - int add_other_logk(LDBLE* source_k, std::vector &add_logk); - int add_logks(struct logk *logk_ptr, int repeats); - LDBLE halve(LDBLE f(LDBLE x, void *), LDBLE x0, LDBLE x1, LDBLE tol); + int add_other_logk(LDBLE* source_k, std::vector& add_logk); + int add_logks(struct logk* logk_ptr, int repeats); + LDBLE halve(LDBLE f(LDBLE x, void*), LDBLE x0, LDBLE x1, LDBLE tol); int replace_solids_gases(void); - int ss_prep(LDBLE t, cxxSS *ss_ptr, int print); - int select_log_k_expression(LDBLE * source_k, LDBLE * target_k); - int slnq(int n, LDBLE * a, LDBLE * delta, int ncols, int print); + int ss_prep(LDBLE t, cxxSS* ss_ptr, int print); + int select_log_k_expression(LDBLE* source_k, LDBLE* target_k); + int slnq(int n, LDBLE* a, LDBLE* delta, int ncols, int print); public: int tidy_punch(void); int tidy_model(void); int check_species_input(void); - LDBLE coef_in_master(struct master *master_ptr); - int phase_rxn_to_trxn(struct phase *phase_ptr, - struct reaction *rxn_ptr); + LDBLE coef_in_master(struct master* master_ptr); + int phase_rxn_to_trxn(struct phase* phase_ptr, + struct reaction* rxn_ptr); int reset_last_model(void); int rewrite_eqn_to_primary(void); int rewrite_eqn_to_secondary(void); - int species_rxn_to_trxn(struct species *s_ptr); + int species_rxn_to_trxn(struct species* s_ptr); int tidy_logk(void); int tidy_exchange(void); int tidy_min_exchange(void); @@ -1031,19 +999,19 @@ public: int tidy_ss_assemblage(void); int tidy_species(void); int tidy_surface(void); - int scan(LDBLE f(LDBLE x, void *), LDBLE * xx0, LDBLE * xx1); - static LDBLE f_spinodal(LDBLE x, void *); - int solve_misc(LDBLE * xxc1, LDBLE * xxc2, LDBLE tol); - int ss_calc_a0_a1(cxxSS *ss_ptr); + int scan(LDBLE f(LDBLE x, void*), LDBLE* xx0, LDBLE* xx1); + static LDBLE f_spinodal(LDBLE x, void*); + int solve_misc(LDBLE* xxc1, LDBLE* xxc2, LDBLE tol); + int ss_calc_a0_a1(cxxSS* ss_ptr); // transport.cpp ------------------------------- int transport(void); void print_punch(int i, boolean active); int set_initial_moles(int i); - cxxSurface sum_surface_comp(cxxSurface *source1, LDBLE f1, - cxxSurface *source2, std::string charge_name, LDBLE f2, + cxxSurface sum_surface_comp(cxxSurface* source1, LDBLE f1, + cxxSurface* source2, std::string charge_name, LDBLE f2, LDBLE new_Dw); - int reformat_surf(const char *comp_name, LDBLE fraction, const char *new_comp_name, + int reformat_surf(const char* comp_name, LDBLE fraction, const char* new_comp_name, LDBLE new_Dw, int cell); LDBLE viscosity(void); LDBLE calc_vm_Cl(void); @@ -1052,15 +1020,15 @@ public: void calc_b_ij(int icell, int jcell, int k, LDBLE b_i, LDBLE b_j, LDBLE g_i, LDBLE g_j, LDBLE free_i, LDBLE free_j, int stagnant); void diffuse_implicit(LDBLE DDt, int stagnant); int fill_spec(int cell_no, int ref_cell); - LDBLE moles_from_redox_states(cxxSolution *sptr, const char *name); - LDBLE moles_from_donnan_layer(cxxSurface *sptr, const char *name, LDBLE moles_needed); - LDBLE add_MCD_moles(LDBLE moles, LDBLE min_mol, int i, cxxSolution *sptr, const char *name); - int fill_m_s(struct J_ij *J_ij, int J_ij_count_spec, int i, int stagnant); - static int sort_species_name(const void *ptr1, const void *ptr2); + LDBLE moles_from_redox_states(cxxSolution* sptr, const char* name); + LDBLE moles_from_donnan_layer(cxxSurface* sptr, const char* name, LDBLE moles_needed); + LDBLE add_MCD_moles(LDBLE moles, LDBLE min_mol, int i, cxxSolution* sptr, const char* name); + int fill_m_s(struct J_ij* J_ij, int J_ij_count_spec, int i, int stagnant); + static int sort_species_name(const void* ptr1, const void* ptr2); int disp_surf(LDBLE stagkin_time); int diff_stag_surf(int mobile_cell); - int check_surfaces(cxxSurface *surface_ptr1, cxxSurface *surface_ptr2); - cxxSurface mobile_surface_copy(cxxSurface *surface_old_ptr, + int check_surfaces(cxxSurface* surface_ptr1, cxxSurface* surface_ptr2); + cxxSurface mobile_surface_copy(cxxSurface* surface_old_ptr, int n_user_new, bool move_old); void transport_cleanup(void); @@ -1072,85 +1040,84 @@ public: // utilities.cpp ------------------------------- public: - int add_elt_list(struct elt_list *elt_list_ptr, LDBLE coef); - int add_elt_list_multi_surf(struct elt_list *elt_list_ptr, LDBLE coef, struct element *surf_elt_ptr); - int add_elt_list(const cxxNameDouble & nd, LDBLE coef); + int add_elt_list(struct elt_list* elt_list_ptr, LDBLE coef); + int add_elt_list_multi_surf(struct elt_list* elt_list_ptr, LDBLE coef, struct element* surf_elt_ptr); + int add_elt_list(const cxxNameDouble& nd, LDBLE coef); protected: - int backspace_screen(int spaces); - LDBLE calc_alk(struct reaction *rxn_ptr); + LDBLE calc_alk(struct reaction* rxn_ptr); public: LDBLE calc_rho_0(LDBLE tc, LDBLE pa); LDBLE calc_dielectrics(LDBLE tc, LDBLE pa); - int compute_gfw(const char *string, LDBLE * gfw); + int compute_gfw(const char* string, LDBLE* gfw); #if defined PHREEQ98 - int copy_title(char *token_ptr, char **ptr, int *length); + int copy_title(char* token_ptr, char** ptr, int* length); #endif - int copy_token(char *token_ptr, char **ptr, int *length); - int copy_token(std::string &token, char **ptr); - int dup_print(const char *ptr, int emphasis); + int copy_token(char* token_ptr, const char** ptr, int* length); + int copy_token(std::string& token, const char** ptr); + int dup_print(const char* ptr, int emphasis); int equal(LDBLE a, LDBLE b, LDBLE eps); public: - void *free_check_null(void *ptr); + void* free_check_null(void* ptr); protected: - int get_token(char **eqnaddr, char *string, LDBLE * z, int *l); + int get_token(const char** eqnaddr, char* string, LDBLE* z, int* l); int islegit(const char c); public: void malloc_error(void); protected: - int parse_couple(char *token); - int print_centered(const char *string); + int parse_couple(char* token); + int print_centered(const char* string); public: - static int replace(const char *str1, const char *str2, char *str); - static bool replace(const char *str1, const char *str2, std::string & str); - static int strcmp_nocase(const char *str1, const char *str2); - static int strcmp_nocase_arg1(const char *str1, const char *str2); - static void str_tolower(std::string &name); + static int replace(const char* str1, const char* str2, char* str); + static bool replace(const char* str1, const char* str2, std::string& str); + static int strcmp_nocase(const char* str1, const char* str2); + static int strcmp_nocase_arg1(const char* str1, const char* str2); + static void str_tolower(std::string& name); protected: - void space(void **ptr, int i, int *max, int struct_size); - void squeeze_white(char *s_l); - int status(int count, const char *str, bool kinetics = false); - void str_tolower(char *str); - void str_toupper(char *str); + void space(void** ptr, int i, int* max, int struct_size); + void squeeze_white(char* s_l); + int status(int count, const char* str, bool kinetics = false); + void str_tolower(char* str); + void str_toupper(char* str); public: #if !defined(NDEBUG) && defined(WIN32_MEMORY_DEBUG) - char *_string_duplicate(const char *token, const char *szFileName, int nLine); + char* _string_duplicate(const char* token, const char* szFileName, int nLine); #else - char *string_duplicate(const char *token); + char* string_duplicate(const char* token); #endif - const char *string_hsave(const char *str); + const char* string_hsave(const char* str); void strings_map_clear(); protected: - char *string_pad(const char *str, int i); - int string_trim(char *str); - int string_trim_right(char *str); - int string_trim_left(char *str); + char* string_pad(const char* str, int i); + int string_trim(char* str); + int string_trim_right(char* str); + int string_trim_left(char* str); static LDBLE under(LDBLE xval); int get_input_errors(void); - int isamong(char c, const char *s_l); + int isamong(char c, const char* s_l); public: - int main_method(int argc, char *argv[]); + int main_method(int argc, char* argv[]); void set_phast(int); int next_user_number(Keywords::KEYWORDS key); - size_t list_components(std::list &list_c); - size_t list_EquilibriumPhases(std::list &list_pp); - size_t list_GasComponents(std::list &list_gc); - size_t list_KineticReactions(std::list &list_kr); - size_t list_SolidSolutions(std::list &list_comps, std::list &list_names); - size_t list_Surfaces(std::list &surftype, std::list &surf); - size_t list_Exchangers(std::list &ex); - PHRQ_io * Get_phrq_io(void) {return this->phrq_io;} - void Set_run_cells_one_step(const bool tf) {this->run_cells_one_step = tf;} + size_t list_components(std::list& list_c); + size_t list_EquilibriumPhases(std::list& list_pp); + size_t list_GasComponents(std::list& list_gc); + size_t list_KineticReactions(std::list& list_kr); + size_t list_SolidSolutions(std::list& list_comps, std::list& list_names); + size_t list_Surfaces(std::list& surftype, std::list& surf); + size_t list_Exchangers(std::list& ex); + PHRQ_io* Get_phrq_io(void) { return this->phrq_io; } + void Set_run_cells_one_step(const bool tf) { this->run_cells_one_step = tf; } - std::map & Get_Rxn_solution_map() {return this->Rxn_solution_map;} - std::map & Get_Rxn_exchange_map() {return this->Rxn_exchange_map;} - std::map & Get_Rxn_gas_phase_map() {return this->Rxn_gas_phase_map;} - std::map & Get_Rxn_kinetics_map() {return this->Rxn_kinetics_map;} - std::map & Get_Rxn_pp_assemblage_map() {return this->Rxn_pp_assemblage_map;} - std::map & Get_Rxn_ss_assemblage_map() {return this->Rxn_ss_assemblage_map;} - std::map & Get_Rxn_surface_map() {return this->Rxn_surface_map;} - std::map & Get_Rxn_temperature_map() {return this->Rxn_temperature_map;} - std::map & Get_Rxn_pressure_map() {return this->Rxn_pressure_map;} + std::map& Get_Rxn_solution_map() { return this->Rxn_solution_map; } + std::map& Get_Rxn_exchange_map() { return this->Rxn_exchange_map; } + std::map& Get_Rxn_gas_phase_map() { return this->Rxn_gas_phase_map; } + std::map& Get_Rxn_kinetics_map() { return this->Rxn_kinetics_map; } + std::map& Get_Rxn_pp_assemblage_map() { return this->Rxn_pp_assemblage_map; } + std::map& Get_Rxn_ss_assemblage_map() { return this->Rxn_ss_assemblage_map; } + std::map& Get_Rxn_surface_map() { return this->Rxn_surface_map; } + std::map& Get_Rxn_temperature_map() { return this->Rxn_temperature_map; } + std::map& Get_Rxn_pressure_map() { return this->Rxn_pressure_map; } protected: @@ -1160,7 +1127,7 @@ protected: //Data members // protected: - PHRQ_io *phrq_io; + PHRQ_io* phrq_io; PHRQ_io ioInstance; int same_model; @@ -1197,7 +1164,7 @@ protected: std::map Rxn_surface_map; std::map charge_group_map; int change_surf_count; - struct Change_Surf *change_surf; + struct Change_Surf* change_surf; /* ---------------------------------------------------------------------- * Exchange @@ -1301,16 +1268,16 @@ protected: std::vector sum_mb1; /* array of pointers to sources and targets for mass balance summations with coef = 1.0 */ std::vector sum_jacob1; /* array of pointers to sources and targets for array - equations with coef = 1.0 */ + equations with coef = 1.0 */ std::vector sum_mb2; /* array of coefficients and pointers to sources and - targets for mass balance summations with coef != 1.0 */ + targets for mass balance summations with coef != 1.0 */ std::vector sum_jacob2; /* array of coefficients and pointers to sources and - targets, coef != 1.0 */ + targets, coef != 1.0 */ std::vector sum_delta; /* array of pointers to sources, targets and coefficients for summing deltas for mass balance equations */ - /*---------------------------------------------------------------------- - * Solution - *---------------------------------------------------------------------- */ + /*---------------------------------------------------------------------- + * Solution + *---------------------------------------------------------------------- */ std::map Rxn_solution_map; std::vector unnumbered_solutions; bool save_species; @@ -1318,10 +1285,10 @@ protected: /*---------------------------------------------------------------------- * Global solution *---------------------------------------------------------------------- */ - char *title_x; + char* title_x; std::string last_title_x; int new_x; - char *description_x; + char* description_x; LDBLE tc_x; LDBLE tk_x; LDBLE patm_x; @@ -1342,7 +1309,7 @@ protected: LDBLE mass_water_aq_x; LDBLE mass_water_surfaces_x; LDBLE mass_water_bulk_x; - char *units_x; + char* units_x; std::map < std::string, cxxChemRxn > pe_x; std::map isotopes_x; std::string default_pe_x; @@ -1371,13 +1338,13 @@ protected: LDBLE heat_diffc; int cell; LDBLE mcd_substeps; - struct stag_data *stag_data; + struct stag_data* stag_data; int print_modulus; int punch_modulus; int dump_in; int dump_modulus; int transport_warnings; - struct cell_data *cell_data; + struct cell_data* cell_data; int old_cells, max_cells, all_cells; int multi_Dflag; /* signals calc'n of multicomponent diffusion */ int interlayer_Dflag; /* multicomponent diffusion and diffusion through interlayer porosity */ @@ -1420,7 +1387,7 @@ protected: * Elements *---------------------------------------------------------------------- */ std::vector elements; - struct element *element_h_one; + struct element* element_h_one; /*---------------------------------------------------------------------- * Element List @@ -1436,20 +1403,20 @@ protected: *---------------------------------------------------------------------- */ std::vector logk; - char *moles_per_kilogram_string; - char *pe_string; + char* moles_per_kilogram_string; + char* pe_string; std::vector s; std::vector< std::map < std::string, cxxSpeciesDL > > s_diff_layer; std::vector s_x; - struct species *s_h2o; - struct species *s_hplus; - struct species *s_h3oplus; - struct species *s_eminus; - struct species *s_co3; - struct species *s_h2; - struct species *s_o2; + struct species* s_h2o; + struct species* s_hplus; + struct species* s_h3oplus; + struct species* s_eminus; + struct species* s_co3; + struct species* s_h2; + struct species* s_o2; /*---------------------------------------------------------------------- * Phases @@ -1468,23 +1435,23 @@ protected: int count_unknowns; int max_unknowns; - struct unknown *ah2o_unknown; - struct unknown *alkalinity_unknown; - struct unknown *carbon_unknown; - struct unknown *charge_balance_unknown; - struct unknown *exchange_unknown; - struct unknown *mass_hydrogen_unknown; - struct unknown *mass_oxygen_unknown; - struct unknown *mb_unknown; - struct unknown *mu_unknown; - struct unknown *pe_unknown; - struct unknown *ph_unknown; - struct unknown *pure_phase_unknown; - struct unknown *solution_phase_boundary_unknown; - struct unknown *surface_unknown; - struct unknown *gas_unknown; - struct unknown *ss_unknown; - std::vector gas_unknowns; + struct unknown* ah2o_unknown; + struct unknown* alkalinity_unknown; + struct unknown* carbon_unknown; + struct unknown* charge_balance_unknown; + struct unknown* exchange_unknown; + struct unknown* mass_hydrogen_unknown; + struct unknown* mass_oxygen_unknown; + struct unknown* mb_unknown; + struct unknown* mu_unknown; + struct unknown* pe_unknown; + struct unknown* ph_unknown; + struct unknown* pure_phase_unknown; + struct unknown* solution_phase_boundary_unknown; + struct unknown* surface_unknown; + struct unknown* gas_unknown; + struct unknown* ss_unknown; + std::vector gas_unknowns; /*---------------------------------------------------------------------- * Reaction work space @@ -1517,20 +1484,12 @@ protected: /* ---------------------------------------------------------------------- * USER PRINT COMMANDS * ---------------------------------------------------------------------- */ - struct rate *user_print; - //struct rate *user_punch; - //const char **user_punch_headings; - //int user_punch_count_headings; + struct rate* user_print; int n_user_punch_index; int fpunchf_user_s_warning; char fpunchf_user_buffer[80]; -#if defined PHREEQ98 - struct rate *user_graph; - char **user_graph_headings; - int user_graph_count_headings; -#endif #if defined MULTICHART ChartHandler chart_handler; public: @@ -1548,7 +1507,7 @@ protected: /* ---------------------------------------------------------------------- * GLOBAL DECLARATIONS * ---------------------------------------------------------------------- */ - const char * error_string; + const char* error_string; int simulation; int state; int reaction_step; @@ -1581,8 +1540,8 @@ protected: int overall_iterations; int max_line; - char *line; - char *line_save; + char* line; + char* line_save; LDBLE LOG_10; @@ -1619,19 +1578,19 @@ protected: int count_total_steps; int phast; bool output_newline; - inline void Set_output_newline(bool tf) { this->output_newline = tf;} - inline bool Get_output_newline() { return this->output_newline;} + inline void Set_output_newline(bool tf) { this->output_newline = tf; } + inline bool Get_output_newline() { return this->output_newline; } double a_llnl, b_llnl, bdot_llnl; std::vector llnl_temp, llnl_adh, llnl_bdh, llnl_bdot, llnl_co2_coefs; //char *selected_output_file_name; std::map SelectedOutput_map; - SelectedOutput * current_selected_output; - - std::map UserPunch_map; - UserPunch * current_user_punch; + SelectedOutput* current_selected_output; - char *dump_file_name; + std::map UserPunch_map; + UserPunch* current_user_punch; + + char* dump_file_name; int remove_unstable_phases; std::string screen_string; #ifdef PHREEQCI_GUI @@ -1644,7 +1603,7 @@ protected: * Map definitions */ - std::map strings_map; + std::map strings_map; std::map elements_map; std::map species_map; std::map phases_map; @@ -1667,7 +1626,7 @@ protected: std::map isotope_alpha_map; int phreeqc_mpi_myself; int first_read_input; - char *user_database; + char* user_database; //int have_punch_name; /* VP: Density Start */ @@ -1695,20 +1654,20 @@ protected: LDBLE solution_mass, solution_volume; /* phqalloc.cpp ------------------------------- */ - PHRQMemHeader *s_pTail; + PHRQMemHeader* s_pTail; /* Basic */ - PBasic * basic_interpreter; - double (*basic_callback_ptr) (double x1, double x2, const char *str, void *cookie); - void *basic_callback_cookie; + PBasic* basic_interpreter; + double (*basic_callback_ptr) (double x1, double x2, const char* str, void* cookie); + void* basic_callback_cookie; #ifdef IPHREEQC_NO_FORTRAN_MODULE - double (*basic_fortran_callback_ptr) (double *x1, double *x2, char *str, size_t l); + double (*basic_fortran_callback_ptr) (double* x1, double* x2, char* str, size_t l); #else - double (*basic_fortran_callback_ptr) (double *x1, double *x2, const char *str, int l); + double (*basic_fortran_callback_ptr) (double* x1, double* x2, const char* str, int l); #endif #if defined(SWIG) || defined(SWIG_IPHREEQC) - class BasicCallback *basicCallback; - void SetCallback(BasicCallback *cb) { basicCallback = cb; } + class BasicCallback* basicCallback; + void SetCallback(BasicCallback* cb) { basicCallback = cb; } #endif /* cl1.cpp ------------------------------- */ @@ -1717,7 +1676,7 @@ protected: LDBLE a_aa_sum, b2, b_sum, R_TK; /* input.cpp ------------------------------- */ - int check_line_return; + int check_line_return; int reading_db; /* integrate.cpp ------------------------------- */ @@ -1727,26 +1686,26 @@ protected: /* inverse.cpp ------------------------------- */ int max_row_count, max_column_count; int carbon; - const char **col_name, **row_name; + const char** col_name, ** row_name; int count_rows, count_optimize; int col_phases, col_redox, col_epsilon, col_ph, col_water, col_isotopes, col_phase_isotopes; int row_mb, row_fract, row_charge, row_carbon, row_isotopes, row_epsilon, row_isotope_epsilon, row_water; - LDBLE *inv_zero, *array1, *inv_res, *inv_delta1, *delta2, *delta3, *inv_cu, - *delta_save; - LDBLE *min_delta, *max_delta; - int *inv_iu, *inv_is; + LDBLE* inv_zero, * array1, * inv_res, * inv_delta1, * delta2, * delta3, * inv_cu, + * delta_save; + LDBLE* min_delta, * max_delta; + int* inv_iu, * inv_is; int klmd, nklmd, n2d; int kode, iter; LDBLE toler, error, max_pct, scaled_error; - struct master *master_alk; - int *row_back, *col_back; - unsigned long *good, *bad, *minimal; + struct master* master_alk; + int* row_back, * col_back; + unsigned long* good, * bad, * minimal; int max_good, max_bad, max_minimal; int count_good, count_bad, count_minimal, count_calls; unsigned long soln_bits, phase_bits, current_bits, temp_bits; - FILE *netpath_file; + FILE* netpath_file; int count_inverse_models, count_pat_solutions; int min_position[32], max_position[32], now[32]; std::vector inverse_heading_names; @@ -1754,7 +1713,7 @@ protected: /* kinetics.cpp ------------------------------- */ public: int count_pp, count_pg, count_ss; - void *cvode_kinetics_ptr; + void* cvode_kinetics_ptr; int cvode_test; int cvode_error; int cvode_n_user; @@ -1768,9 +1727,9 @@ public: N_Vector cvode_prev_good_y; M_Env kinetics_machEnv; N_Vector kinetics_y, kinetics_abstol; - void *kinetics_cvode_mem; - cxxSSassemblage *cvode_ss_assemblage_save; - cxxPPassemblage *cvode_pp_assemblage_save; + void* kinetics_cvode_mem; + cxxSSassemblage* cvode_ss_assemblage_save; + cxxPPassemblage* cvode_pp_assemblage_save; protected: std::vector m_temp, m_original, rk_moles, x0_moles; int set_and_run_attempt; @@ -1785,7 +1744,7 @@ protected: int forward_output_to_log; /* phreeqc_files.cpp ------------------------------- */ - char *default_data_base; + char* default_data_base; /* Pitzer */ int pitzer_model, sit_model, pitzer_pe; int full_pitzer, always_full_pitzer, ICON, IC; @@ -1800,10 +1759,10 @@ protected: LDBLE A0; struct pitz_param* aphi; std::vector spec; - struct species ** cations, ** anions, ** neutrals; // pointers to spec + struct species** cations, ** anions, ** neutrals; // pointers to spec int count_cations, count_anions, count_neutrals; int MAXCATIONS, FIRSTANION, MAXNEUTRAL; - struct pitz_param *mcb0, *mcb1, *mcc0; + struct pitz_param* mcb0, * mcb1, * mcc0; std::vector IPRSNT; std::vector M, LGAMMA; LDBLE BK[23], DK[23]; @@ -1813,7 +1772,7 @@ protected: /* print.cpp ------------------------------- */ /* read.cpp */ - char *prev_next_char; + const char* prev_next_char; #if defined PHREEQ98 int shifts_as_points; #endif @@ -1822,7 +1781,7 @@ protected: dumper dump_info; StorageBinList delete_info; runner run_info; - char * sformatf_buffer; + char* sformatf_buffer; size_t sformatf_buffer_size; /* readtr.cpp */ @@ -1842,25 +1801,25 @@ protected: LDBLE a0, a1, kc, kb; /* tally.cpp ------------------------------- */ - struct tally_buffer *t_buffer; + struct tally_buffer* t_buffer; int tally_count_component; - struct tally *tally_table; + struct tally* tally_table; int count_tally_table_columns; int count_tally_table_rows; /* transport.cpp ------------------------------- */ - struct sol_D *sol_D; - struct sol_D *sol_D_dbg; - struct J_ij *J_ij, *J_ij_il; + struct sol_D* sol_D; + struct sol_D* sol_D_dbg; + struct J_ij* J_ij, * J_ij_il; int J_ij_count_spec; - struct M_S *m_s; + struct M_S* m_s; int count_m_s; LDBLE tot1_h, tot1_o, tot2_h, tot2_o; LDBLE diffc_max, diffc_tr, J_ij_sum; int transp_surf; - LDBLE *heat_mix_array; - LDBLE *temp1, *temp2; + LDBLE* heat_mix_array; + LDBLE* temp1, * temp2; int nmix, heat_nmix; LDBLE heat_mix_f_imm, heat_mix_f_m; int warn_MCD_X, warn_fixed_Surf; @@ -1869,10 +1828,10 @@ protected: /* utilities.cpp ------------------------------- */ int spinner; std::map gfw_map; - std::map rates_map; + std::map rates_map; /* new after release of Version 3 */ - std::map > sum_species_map; + std::map > sum_species_map; std::map > sum_species_map_db; friend class PBasic; @@ -1894,10 +1853,10 @@ public: #ifndef _INC_ISFINITE_H #define _INC_ISFINITE_H - /********************************* - isfinite handling - (Note: Should NOT be guarded) - **********************************/ +/********************************* +isfinite handling +(Note: Should NOT be guarded) +**********************************/ #if defined (PHREEQ98) || defined (_MSC_VER) # define HAVE_FINITE @@ -1931,7 +1890,7 @@ namespace Utilities // operations on maps of entities (Solution, Exchange, ...) template < typename T > - void Rxn_dump_raw(const T & b, std::ostream & s_oss, unsigned int indent) + void Rxn_dump_raw(const T& b, std::ostream& s_oss, unsigned int indent) { typename T::const_iterator it; for (it = b.begin(); it != b.end(); ++it) @@ -1946,7 +1905,7 @@ namespace Utilities } template < typename T > - void Rxn_dump_raw_range(const T & b, std::ostream & s_oss, int start, int end, unsigned int indent) + void Rxn_dump_raw_range(const T& b, std::ostream& s_oss, int start, int end, unsigned int indent) { typename T::const_iterator it; for (int i = start; i <= end; i++) @@ -1962,7 +1921,7 @@ namespace Utilities } template < typename T > - T * Rxn_find(std::map < int, T > &b, int i) + T* Rxn_find(std::map < int, T >& b, int i) { if (b.find(i) != b.end()) { @@ -1975,7 +1934,7 @@ namespace Utilities } template < typename T > - int Rxn_next_user_number(std::map < int, T > &b) + int Rxn_next_user_number(std::map < int, T >& b) { int ret = 0; if (b.size() != 0) @@ -1986,7 +1945,7 @@ namespace Utilities } template < typename T > - T * Rxn_copy(std::map < int, T > &b, int i, int j) + T* Rxn_copy(std::map < int, T >& b, int i, int j) { typename std::map < int, T >::iterator it; it = b.find(i); @@ -2005,7 +1964,7 @@ namespace Utilities } template < typename T > - void Rxn_copies(std::map < int, T > &b, int n_user, int n_user_end) + void Rxn_copies(std::map < int, T >& b, int n_user, int n_user_end) { if (n_user_end <= n_user) return; typename std::map < int, T >::iterator it; @@ -2022,7 +1981,7 @@ namespace Utilities } } template < typename T > - int Rxn_read_raw(std::map < int, T > &m, std::set < int > &s, Phreeqc * phreeqc_cookie) + int Rxn_read_raw(std::map < int, T >& m, std::set < int >& s, Phreeqc* phreeqc_cookie) { typename std::map < int, T >::iterator it; assert(!phreeqc_cookie->reading_database()); @@ -2048,10 +2007,10 @@ namespace Utilities } template < typename T > - int Rxn_read_modify(std::map < int, T > &m, std::set < int > &s, Phreeqc * phreeqc_cookie) + int Rxn_read_modify(std::map < int, T >& m, std::set < int >& s, Phreeqc* phreeqc_cookie) { typename std::map < int, T >::iterator it; - + CParser parser(phreeqc_cookie->Get_phrq_io()); std::string key_name; @@ -2061,11 +2020,11 @@ namespace Utilities cxxNumKeyword nk; nk.read_number_description(parser); - T * entity_ptr = Utilities::Rxn_find(m, nk.Get_n_user()); + T* entity_ptr = Utilities::Rxn_find(m, nk.Get_n_user()); if (!entity_ptr) { std::ostringstream errstr; - errstr << "Could not find " << key_name << " " << nk.Get_n_user() << ", ignoring modify data.\n"; + errstr << "Could not find " << key_name << " " << nk.Get_n_user() << ", ignoring modify data.\n"; phreeqc_cookie->warning_msg(errstr.str().c_str()); //phreeqc_cookie->error_msg(errstr.str().c_str(), PHRQ_io::OT_STOP); @@ -2086,7 +2045,7 @@ namespace Utilities } template < typename T > - int SB_read_modify(std::map < int, T > &m, CParser &parser) + int SB_read_modify(std::map < int, T >& m, CParser& parser) { typename std::map < int, T >::iterator it; @@ -2097,7 +2056,7 @@ namespace Utilities cxxNumKeyword nk; nk.read_number_description(parser); - T * entity_ptr = Utilities::Rxn_find(m, nk.Get_n_user()); + T* entity_ptr = Utilities::Rxn_find(m, nk.Get_n_user()); if (!entity_ptr) { std::ostringstream errstr; @@ -2120,7 +2079,7 @@ namespace Utilities } template < typename T > - void Rxn_mix(std::map &mix_map, std::map < int, T > &entity_map, Phreeqc * phreeqc_cookie) + void Rxn_mix(std::map & mix_map, std::map < int, T >& entity_map, Phreeqc* phreeqc_cookie) { std::map::iterator mix_it; for (mix_it = mix_map.begin(); mix_it != mix_map.end(); mix_it++) @@ -2136,14 +2095,14 @@ namespace Utilities #if defined(PHREEQCI_GUI) -void PhreeqcIWait(Phreeqc *phreeqc); +void PhreeqcIWait(Phreeqc* phreeqc); #endif #if !defined(NDEBUG) && defined(WIN32_MEMORY_DEBUG) #define string_duplicate(s) _string_duplicate(s, __FILE__, __LINE__) #endif #if defined(_DEBUG) - char * _string_duplicate(const char *token, const char *szFileName, int nLine); +char* _string_duplicate(const char* token, const char* szFileName, int nLine); #endif #endif //_INC_UTILITIES_NAMESPACE_H diff --git a/parse.cpp b/parse.cpp index 67fe9f76..3c5da231 100644 --- a/parse.cpp +++ b/parse.cpp @@ -385,7 +385,7 @@ get_charge(char *charge, LDBLE * l_z) /* ---------------------------------------------------------------------- */ int Phreeqc:: -get_coef(LDBLE * coef, char **eqnaddr) +get_coef(LDBLE * coef, const char **eqnaddr) /* ---------------------------------------------------------------------- */ /* * Function reads through eqn and determines the coefficient of the next @@ -485,7 +485,7 @@ get_coef(LDBLE * coef, char **eqnaddr) /* ---------------------------------------------------------------------- */ int Phreeqc:: -get_elt(char **t_ptr, char *element, int *i) +get_elt(const char **t_ptr, char *element, int *i) /* ---------------------------------------------------------------------- */ /* * Function reads an element name out of the equation string. @@ -557,7 +557,7 @@ get_elt(char **t_ptr, char *element, int *i) /* ---------------------------------------------------------------------- */ int Phreeqc:: -get_elts_in_species(char **t_ptr, LDBLE coef) +get_elts_in_species(const char **t_ptr, LDBLE coef) /* ---------------------------------------------------------------------- */ { /* @@ -692,7 +692,7 @@ get_elts_in_species(char **t_ptr, LDBLE coef) /* ---------------------------------------------------------------------- */ int Phreeqc:: -get_secondary(char **t_ptr, char *element, int *i) +get_secondary(const char **t_ptr, char *element, int *i) /* ---------------------------------------------------------------------- */ /* * Function reads an element name out of the equation string. @@ -812,7 +812,7 @@ get_secondary(char **t_ptr, char *element, int *i) /* ---------------------------------------------------------------------- */ int Phreeqc:: -get_secondary_in_species(char **t_ptr, LDBLE coef) +get_secondary_in_species(const char **t_ptr, LDBLE coef) /* ---------------------------------------------------------------------- */ { /* @@ -942,7 +942,7 @@ get_secondary_in_species(char **t_ptr, LDBLE coef) /* ---------------------------------------------------------------------- */ int Phreeqc:: -get_num(char **t_ptr, LDBLE * num) +get_num(const char **t_ptr, LDBLE * num) /* ---------------------------------------------------------------------- */ /* * Function reads through a string looking for leading numeric field @@ -1005,7 +1005,7 @@ get_num(char **t_ptr, LDBLE * num) /* ---------------------------------------------------------------------- */ int Phreeqc:: -get_species(char **ptr) +get_species(const char **ptr) /* ---------------------------------------------------------------------- */ { /* Function reads next species out of the equation, including optional diff --git a/utilities.cpp b/utilities.cpp index 337e07ac..574b69dc 100644 --- a/utilities.cpp +++ b/utilities.cpp @@ -337,7 +337,7 @@ compute_gfw(const char *string, LDBLE * gfw) /* ---------------------------------------------------------------------- */ int Phreeqc:: -copy_token(char *token_ptr, char **ptr, int *length) +copy_token(char *token_ptr, const char **ptr, int *length) /* ---------------------------------------------------------------------- */ { /* @@ -407,7 +407,7 @@ copy_token(char *token_ptr, char **ptr, int *length) } /* ---------------------------------------------------------------------- */ int Phreeqc:: -copy_token(std::string &token, char **ptr) +copy_token(std::string &token, const char **ptr) /* ---------------------------------------------------------------------- */ { /* @@ -620,7 +620,7 @@ free_check_null(void *ptr) /* ---------------------------------------------------------------------- */ int Phreeqc:: -get_token(char **eqnaddr, char *string, LDBLE * l_z, int *l) +get_token(const char **eqnaddr, char *string, LDBLE * l_z, int *l) /* ---------------------------------------------------------------------- */ /* * Function finds next species in equation, coefficient has already @@ -726,8 +726,7 @@ get_token(char **eqnaddr, char *string, LDBLE * l_z, int *l) /* error if no more space */ if (j >= MAX_LENGTH) { - error_msg - ("The charge on a species has exceeded MAX_LENGTH characters.", + error_msg("The charge on a species has exceeded MAX_LENGTH characters.", CONTINUE); return (ERROR); } From c748922b5ee6eca314542d3a6c28ee395cd97b9e Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Fri, 26 Mar 2021 15:16:04 -0600 Subject: [PATCH 13/53] added const qualifier for all the parsing --- Phreeqc.cpp | 4 +- Phreeqc.h | 56 ++-- basicsubs.cpp | 61 ++-- class_main.cpp | 24 +- inverse.cpp | 12 +- isotopes.cpp | 26 +- kinetics.cpp | 8 +- mainsubs.cpp | 3 + parse.cpp | 65 ++-- pitzer.cpp | 2 +- pitzer_structures.cpp | 12 +- prep.cpp | 100 +++--- read.cpp | 751 ++++++++++++++++++++++-------------------- readtr.cpp | 26 +- sit.cpp | 2 +- spread.cpp | 61 ++-- step.cpp | 24 +- structures.cpp | 68 ++-- tally.cpp | 18 +- tidy.cpp | 113 +++---- transport.cpp | 21 +- utilities.cpp | 227 +++++-------- 22 files changed, 831 insertions(+), 853 deletions(-) diff --git a/Phreeqc.cpp b/Phreeqc.cpp index 529aa61e..c4441577 100644 --- a/Phreeqc.cpp +++ b/Phreeqc.cpp @@ -1472,7 +1472,7 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) for (int i = 0; i < (int)pSrc->elements.size(); i++) { - const char * ptr = string_hsave(pSrc->elements[i]->name); + const char* ptr = string_hsave(pSrc->elements[i]->name); struct element *elt_ptr = element_store(ptr); elt_ptr->gfw = pSrc->elements[i]->gfw; } @@ -1561,7 +1561,7 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) count_elts = 0; paren_count = 0; char * string = string_duplicate(s_ptr->mole_balance); - char * ptr = string; + const char* ptr = string; get_secondary_in_species(&ptr, 1.0); s_ptr->next_secondary = elt_list_save(); free_check_null(string); diff --git a/Phreeqc.h b/Phreeqc.h index 064b87e1..89f1e57c 100644 --- a/Phreeqc.h +++ b/Phreeqc.h @@ -560,7 +560,7 @@ public: int clear(void); int convert_units(cxxSolution* solution_ptr); struct unknown* find_surface_charge_unknown(std::string& str_ptr, int plane); - std::vector get_list_master_ptrs(const char* ptr, struct master* master_ptr); + std::vector get_list_master_ptrs(const char* cptr, struct master* master_ptr); int inout(void); int is_special(struct species* spec); int mb_for_species_aq(int n); @@ -606,7 +606,7 @@ public: int write_phase_sys_total(int n); // print.cpp ------------------------------- - static char* sformatf(const char* format, ...); + char* sformatf(const char* format, ...); int array_print(LDBLE* array_l, int row_count, int column_count, int max_column_count); int set_pr_in_false(void); @@ -649,11 +649,11 @@ public: int read_input(void); int* read_list_ints_range(const char** ptr, int* count_ints, int positive, int* int_list); - int read_log_k_only(const char* ptr, LDBLE* log_k); - int read_t_c_only(const char* ptr, LDBLE* t_c); - int read_p_c_only(const char* ptr, LDBLE* p_c); - int read_omega_only(const char* ptr, LDBLE* omega); - int read_number_description(const char* ptr, int* n_user, int* n_user_end, + int read_log_k_only(const char* cptr, LDBLE* log_k); + int read_t_c_only(const char* cptr, LDBLE* t_c); + int read_p_c_only(const char* cptr, LDBLE* p_c); + int read_omega_only(const char* cptr, LDBLE* omega); + int read_number_description(const char* cptr, int* n_user, int* n_user_end, char** description, int allow_negative = FALSE); int check_key(const char* str); int check_units(std::string& tot_units, bool alkalinity, bool check_compatibility, @@ -665,19 +665,19 @@ public: int add_psi_master_species(char* token); int read_advection(void); - int read_analytical_expression_only(const char* ptr, LDBLE* log_k); + int read_analytical_expression_only(const char* cptr, LDBLE* log_k); /* VP: Density Start */ - int read_millero_abcdef(const char* ptr, LDBLE* abcdef); + int read_millero_abcdef(const char* cptr, LDBLE* abcdef); /* VP: Density End */ - int read_viscosity_parms(const char* ptr, LDBLE* Jones_Dole); + int read_viscosity_parms(const char* cptr, LDBLE* Jones_Dole); int read_copy(void); int read_debug(void); - int read_delta_h_only(const char* ptr, LDBLE* delta_h, + int read_delta_h_only(const char* cptr, LDBLE* delta_h, DELTA_H_UNIT* units); - int read_aq_species_vm_parms(const char* ptr, LDBLE* delta_v); - int read_vm_only(const char* ptr, LDBLE* delta_v, + int read_aq_species_vm_parms(const char* cptr, LDBLE* delta_v); + int read_vm_only(const char* cptr, LDBLE* delta_v, DELTA_V_UNIT* units); - int read_phase_vm(const char* ptr, LDBLE* delta_v, + int read_phase_vm(const char* cptr, LDBLE* delta_v, DELTA_V_UNIT* units); int read_llnl_aqueous_model_parameters(void); int read_exchange(void); @@ -687,7 +687,7 @@ public: int read_incremental_reactions(void); int read_inverse(void); int read_inv_balances(struct inverse* inverse_ptr, const char* next_char); - int read_inv_isotopes(struct inverse* inverse_ptr, const char* ptr); + int read_inv_isotopes(struct inverse* inverse_ptr, const char* cptr); int read_inv_phases(struct inverse* inverse_ptr, const char* next_char); int read_kinetics(void); LDBLE* read_list_doubles(const char** ptr, int* count_doubles); @@ -841,12 +841,12 @@ protected: struct logk* logk_search(const char* name); struct master* master_alloc(void); static int master_compare(const void* ptr1, const void* ptr2); - int master_delete(const char* ptr); + int master_delete(const char* cptr); public: - struct master* master_bsearch(const char* ptr); - struct master* master_bsearch_primary(const char* ptr); - struct master* master_bsearch_secondary(const char* ptr); - struct master* master_search(const char* ptr, int* n); + struct master* master_bsearch(const char* cptr); + struct master* master_bsearch_primary(const char* cptr); + struct master* master_bsearch_secondary(const char* cptr); + struct master* master_search(const char* cptr, int* n); struct pe_data* pe_data_alloc(void); public: struct pe_data* pe_data_dup(struct pe_data* pe_ptr_old); @@ -854,13 +854,13 @@ public: protected: int pe_data_store(struct pe_data** pe, const char* token); public: - struct phase* phase_bsearch(const char* ptr, int* j, int print); + struct phase* phase_bsearch(const char* cptr, int* j, int print); protected: static int phase_compare(const void* ptr1, const void* ptr2); int phase_delete(int i); struct phase* phase_store(const char* name); public: - struct rate* rate_bsearch(const char* ptr, int* j); + struct rate* rate_bsearch(const char* cptr, int* j); int rate_free(struct rate* rate_ptr); struct rate* rate_copy(struct rate* rate_ptr); struct rate* rate_search(const char* name, int* n); @@ -1054,7 +1054,7 @@ public: #endif int copy_token(char* token_ptr, const char** ptr, int* length); int copy_token(std::string& token, const char** ptr); - int dup_print(const char* ptr, int emphasis); + int dup_print(const char* cptr, int emphasis); int equal(LDBLE a, LDBLE b, LDBLE eps); public: void* free_check_null(void* ptr); @@ -1068,6 +1068,7 @@ protected: int print_centered(const char* string); public: static int replace(const char* str1, const char* str2, char* str); + static void replace(std::string &stds, const char* str1, const char* str2); static bool replace(const char* str1, const char* str2, std::string& str); static int strcmp_nocase(const char* str1, const char* str2); static int strcmp_nocase_arg1(const char* str1, const char* str2); @@ -1088,9 +1089,12 @@ public: void strings_map_clear(); protected: char* string_pad(const char* str, int i); - int string_trim(char* str); - int string_trim_right(char* str); - int string_trim_left(char* str); + static int string_trim(char* str); + static int string_trim_right(char* str); + static int string_trim_left(char* str); + static void string_trim(std::string& str); + static void string_trim_left(std::string& str); + static void string_trim_right(std::string& str); static LDBLE under(LDBLE xval); int get_input_errors(void); int isamong(char c, const char* s_l); diff --git a/basicsubs.cpp b/basicsubs.cpp index 2b0c4f2b..159a34b0 100644 --- a/basicsubs.cpp +++ b/basicsubs.cpp @@ -814,7 +814,7 @@ calc_surface_charge(const char *surface_name) /* ---------------------------------------------------------------------- */ { char token[MAX_LENGTH], token1[MAX_LENGTH]; - char *ptr; + const char* cptr; int i, j, k; LDBLE charge; struct rxn_token_temp *token_ptr; @@ -840,8 +840,8 @@ calc_surface_charge(const char *surface_name) master_ptr = trxn.token[i].s->primary; strcpy(token, master_ptr->elt->name); replace("_", " ", token); - ptr = token; - copy_token(token1, &ptr, &j); + cptr = token; + copy_token(token1, &cptr, &j); if (strcmp(surface_name, token1) == 0) { charge += s_x[k]->moles * s_x[k]->z; @@ -1966,7 +1966,7 @@ match_elts_in_species(const char *name, const char *mytemplate) */ int i, i1, l, case_no, match; char c, c1; - char *ptr, *ptr1; + const char* cptr, *ptr1; LDBLE d; char element[MAX_LENGTH]; char token[MAX_LENGTH], equal_list[MAX_LENGTH]; @@ -1993,11 +1993,11 @@ match_elts_in_species(const char *name, const char *mytemplate) replace("--", "-2", token); } - ptr = token; + cptr = token; std::vector< std::pair > match_vector; - while ((c = *ptr) != '\0') + while ((c = *cptr) != '\0') { - c1 = *(ptr + 1); + c1 = *(cptr + 1); str[0] = c; str[1] = '\0'; /* @@ -2008,11 +2008,11 @@ match_elts_in_species(const char *name, const char *mytemplate) /* * Get new element and subscript */ - if (get_elt(&ptr, element, &l) == ERROR) + if (get_elt(&cptr, element, &l) == ERROR) { return (ERROR); } - if (get_num(&ptr, &d) == ERROR) + if (get_num(&cptr, &d) == ERROR) { return (ERROR); } @@ -2023,7 +2023,7 @@ match_elts_in_species(const char *name, const char *mytemplate) { std::pair pr(str, 1.0); match_vector.push_back(pr); - ptr += 1; + cptr += 1; } } /* @@ -2031,8 +2031,8 @@ match_elts_in_species(const char *name, const char *mytemplate) */ strcpy(template1, mytemplate); squeeze_white(template1); - ptr = template1; - while (extract_bracket(&ptr, equal_list) == TRUE) + cptr = template1; + while (extract_bracket(&cptr, equal_list) == TRUE) { replace("{", "", equal_list); replace("}", "", equal_list); @@ -2102,8 +2102,8 @@ match_elts_in_species(const char *name, const char *mytemplate) */ strcpy(template1, mytemplate); squeeze_white(template1); - ptr = template1; - while (extract_bracket(&ptr, equal_list) == TRUE) + cptr = template1; + while (extract_bracket(&cptr, equal_list) == TRUE) { strcpy(equal_list1, equal_list); replace("{", "", equal_list); @@ -2124,7 +2124,7 @@ match_elts_in_species(const char *name, const char *mytemplate) } replace(equal_list1, elt_name.c_str(), template1); squeeze_white(template1); - ptr = template1; + cptr = template1; } /* * Compare string @@ -2160,13 +2160,13 @@ match_elts_in_species(const char *name, const char *mytemplate) break; case 1: /* leading wild card */ - if ((ptr = strstr(token, template1)) == NULL) + if ((cptr = strstr(token, template1)) == NULL) { match = FALSE; } else { - if (strcmp(ptr, template1) == 0) + if (strcmp(cptr, template1) == 0) match = TRUE; } break; @@ -2186,14 +2186,15 @@ match_elts_in_species(const char *name, const char *mytemplate) /* ---------------------------------------------------------------------- */ int Phreeqc:: -extract_bracket(char **string, char *bracket_string) +extract_bracket(const char **string, char *bracket_string) /* ---------------------------------------------------------------------- */ { - char *ptr, *ptr1; + const char* cptr; + char *ptr1; - if ((ptr = strstr(*string, "{")) == NULL) + if ((cptr = strstr(*string, "{")) == NULL) return (FALSE); - strcpy(bracket_string, ptr); + strcpy(bracket_string, cptr); if ((ptr1 = strstr(bracket_string, "}")) == NULL) { error_string = sformatf( @@ -2272,8 +2273,8 @@ surf_total(const char *total_name, const char *surface_name) //strcpy(token, s_x[j]->next_elt[i].elt->name); //replace("_", " ", token); - //ptr = token; - //copy_token(name, &ptr, &k); + //cptr = token; + //copy_token(name, &cptr, &k); token = s_x[j]->next_elt[i].elt->name; replace("_", " ", token); std::string::iterator b = token.begin(); @@ -2360,7 +2361,7 @@ surf_total_no_redox(const char *total_name, const char *surface_name) int i, j, k; char name[MAX_LENGTH], token[MAX_LENGTH]; char surface_name_local[MAX_LENGTH]; - char *ptr; + const char* cptr; if (use.Get_surface_ptr() == NULL) return (0); @@ -2374,8 +2375,8 @@ surf_total_no_redox(const char *total_name, const char *surface_name) continue; strcpy(token, x[j]->master[0]->elt->name); replace("_", " ", token); - ptr = token; - copy_token(name, &ptr, &k); + cptr = token; + copy_token(name, &cptr, &k); if (surface_name != NULL) { if (strcmp(name, surface_name) == 0) @@ -2404,8 +2405,8 @@ surf_total_no_redox(const char *total_name, const char *surface_name) strcpy(token, s_x[j]->next_elt[i].elt->name); replace("_", " ", token); - ptr = token; - copy_token(name, &ptr, &k); + cptr = token; + copy_token(name, &cptr, &k); if (strcmp(name, surface_name_local) == 0) { /* @@ -2821,8 +2822,8 @@ kinetics_formula(std::string kin_name, cxxNameDouble &stoichiometry) std::string name = it->first; LDBLE coef = it->second; char * temp_name = string_duplicate(name.c_str()); - char *ptr = temp_name; - get_elts_in_species(&ptr, coef); + const char* cptr = temp_name; + get_elts_in_species(&cptr, coef); free_check_null(temp_name); } } diff --git a/class_main.cpp b/class_main.cpp index 78f3d148..24fa50e0 100644 --- a/class_main.cpp +++ b/class_main.cpp @@ -332,7 +332,7 @@ process_file_names(int argc, char *argv[], std::istream **db_cookie, char query[2 * MAX_LENGTH]; char in_file[2 * MAX_LENGTH], out_file[2 * MAX_LENGTH], db_file[2 * MAX_LENGTH]; char *env_ptr; - char *ptr; + const char* cptr; /* * Prepare error handling */ @@ -383,8 +383,8 @@ process_file_names(int argc, char *argv[], std::istream **db_cookie, * Open file for output */ strcpy(query, "Name of output file?"); - ptr = default_name; - copy_token(token, &ptr, &l); + cptr = default_name; + copy_token(token, &cptr, &l); strcpy(token, default_name); strcat(token, ".out"); std::ofstream * local_output_stream = NULL; @@ -422,12 +422,12 @@ process_file_names(int argc, char *argv[], std::istream **db_cookie, phrq_io->push_istream(local_input_stream); if (get_line() == KEYWORD) { - ptr = line; - copy_token(token, &ptr, &l); + cptr = line; + copy_token(token, &cptr, &l); if (strcmp_nocase(token, "database") == 0) { user_database = (char *) free_check_null(user_database); - user_database = string_duplicate(ptr); + user_database = string_duplicate(cptr); if (string_trim(user_database) == EMPTY) { warning_msg("DATABASE file name is missing; default database will be used."); @@ -529,7 +529,7 @@ process_file_names(int argc, char *argv[], std::istream **db_cookie, char query[2 * MAX_LENGTH]; char in_file[2 * MAX_LENGTH], out_file[2 * MAX_LENGTH], db_file[2 * MAX_LENGTH]; char *env_ptr; - char *ptr; + const char* cptr; /* * Prepare error handling */ @@ -580,8 +580,8 @@ process_file_names(int argc, char *argv[], std::istream **db_cookie, * Open file for output */ strcpy(query, "Name of output file?"); - ptr = default_name; - copy_token(token, &ptr, &l); + cptr = default_name; + copy_token(token, &cptr, &l); strcat(token, ".out"); std::ofstream * local_output_stream; if (argc <= 1) @@ -618,12 +618,12 @@ process_file_names(int argc, char *argv[], std::istream **db_cookie, phrq_io->push_istream(local_input_stream); if (get_line() == KEYWORD) { - ptr = line; - copy_token(token, &ptr, &l); + cptr = line; + copy_token(token, &cptr, &l); if (strcmp_nocase(token, "database") == 0) { user_database = (char *) free_check_null(user_database); - user_database = string_duplicate(ptr); + user_database = string_duplicate(cptr); if (string_trim(user_database) == EMPTY) { warning_msg("DATABASE file name is missing; default database will be used."); diff --git a/inverse.cpp b/inverse.cpp index 26a6e824..f140974d 100644 --- a/inverse.cpp +++ b/inverse.cpp @@ -4044,7 +4044,7 @@ dump_netpath(struct inverse *inverse_ptr) { int j; std::string string; - char *ptr; + const char* cptr; if (inverse_ptr->netpath == NULL) return; @@ -4079,8 +4079,8 @@ dump_netpath(struct inverse *inverse_ptr) /* flags and description */ char * description = string_duplicate(it->second.Get_description().c_str()); - ptr = description; - j = copy_token(string, &ptr); + cptr = description; + j = copy_token(string, &cptr); if (j != EMPTY) { string = sformatf("%s", description); @@ -4397,7 +4397,7 @@ dump_netpath_pat(struct inverse *inv_ptr) cxxSolution *solution_ptr, *solution_ptr_orig; struct master *master_ptr; LDBLE d1, d2, d3; - char *ptr; + const char* cptr; LDBLE sum, sum1, sum_iso, d; std::vector array_save, l_delta_save; int count_unknowns_save, max_row_count_save, max_column_count_save, temp, @@ -4517,9 +4517,9 @@ dump_netpath_pat(struct inverse *inv_ptr) /* Header */ char * description = string_duplicate(solution_ptr_orig->Get_description().c_str()); - ptr = description; + cptr = description; std::string string; - if (copy_token(string, &ptr) != EMPTY) + if (copy_token(string, &cptr) != EMPTY) { fprintf(netpath_file, "%d. %s\n", count_inverse_models, solution_ptr_orig->Get_description().c_str()); diff --git a/isotopes.cpp b/isotopes.cpp index 15b78017..597ed2b0 100644 --- a/isotopes.cpp +++ b/isotopes.cpp @@ -28,7 +28,7 @@ read_isotopes(void) struct element *elt_ptr; int return_value, opt, opt_save; - char *next_char; + const char* next_char; const char *opt_list[] = { "isotope", /* 0 */ "total_is_major" /* 1 */ @@ -160,14 +160,14 @@ read_calculate_values(void) * ERROR if error occurred reading data * */ - char *ptr; + const char* cptr; int l; int return_value, opt, opt_save; char token[MAX_LENGTH]; struct calculate_value *calculate_value_ptr; char *description; int n_user, n_user_end; - char *next_char; + const char* next_char; const char *opt_list[] = { "start", /* 0 */ "end" /* 1 */ @@ -176,8 +176,8 @@ read_calculate_values(void) /* * Read advection number (not currently used) */ - ptr = line; - read_number_description(ptr, &n_user, &n_user_end, &description); + cptr = line; + read_number_description(cptr, &n_user, &n_user_end, &description); description = (char *) free_check_null(description); opt_save = OPTION_DEFAULT; /* @@ -276,14 +276,14 @@ read_isotope_ratios(void) * ERROR if error occurred reading data * */ - char *ptr; + const char* cptr; int l; int return_value, opt, opt_save; char token[MAX_LENGTH]; struct isotope_ratio *isotope_ratio_ptr; char *description; int n_user, n_user_end; - char *next_char; + const char* next_char; const char *opt_list[] = { "no_options" /* 0 */ }; @@ -291,8 +291,8 @@ read_isotope_ratios(void) /* * Read number (not currently used) */ - ptr = line; - read_number_description(ptr, &n_user, &n_user_end, &description); + cptr = line; + read_number_description(cptr, &n_user, &n_user_end, &description); description = (char *) free_check_null(description); opt_save = OPTION_DEFAULT; /* @@ -375,14 +375,14 @@ read_isotope_alphas(void) * ERROR if error occurred reading data * */ - char *ptr; + const char* cptr; int l; int return_value, opt, opt_save; char token[MAX_LENGTH]; struct isotope_alpha *isotope_alpha_ptr; char *description; int n_user, n_user_end; - char *next_char; + const char* next_char; const char *opt_list[] = { "no_options" /* 0 */ }; @@ -390,8 +390,8 @@ read_isotope_alphas(void) /* * Read number (not currently used) */ - ptr = line; - read_number_description(ptr, &n_user, &n_user_end, &description); + cptr = line; + read_number_description(cptr, &n_user, &n_user_end, &description); description = (char *) free_check_null(description); opt_save = OPTION_DEFAULT; /* diff --git a/kinetics.cpp b/kinetics.cpp index bceaf1a0..82d0f620 100644 --- a/kinetics.cpp +++ b/kinetics.cpp @@ -187,7 +187,7 @@ RESTART: // if limiting rates, jump to here else { char * temp_name = string_duplicate(name.c_str()); - char * ptr = temp_name; + const char* ptr = temp_name; if (get_elts_in_species(&ptr, coef * coef1) == ERROR) { error_string = sformatf("Error in -formula: %s", temp_name); @@ -211,7 +211,7 @@ RESTART: // if limiting rates, jump to here { /* found kinetics component */ char * formula = string_duplicate(exchange_ptr->Get_exchange_comps()[j].Get_formula().c_str()); - char * ptr = formula; + const char* ptr = formula; if (get_elts_in_species(&ptr, -coef*exchange_ptr->Get_exchange_comps()[j].Get_phase_proportion()) == ERROR) { error_string = sformatf("Error in -formula: %s", formula); @@ -236,7 +236,7 @@ RESTART: // if limiting rates, jump to here { /* found kinetics component */ char * temp_formula = string_duplicate(surface_comp_ptr->Get_formula().c_str()); - char *ptr = temp_formula; + const char* cptr = temp_formula; /* Surface = 0 when m becomes low ... */ if (0.9 * surface_comp_ptr->Get_phase_proportion() * @@ -251,7 +251,7 @@ RESTART: // if limiting rates, jump to here } else { - if (get_elts_in_species(&ptr, -coef * surface_comp_ptr->Get_phase_proportion()) == ERROR) + if (get_elts_in_species(&cptr, -coef * surface_comp_ptr->Get_phase_proportion()) == ERROR) { error_string = sformatf("Error in -formula: %s", temp_formula); error_msg(error_string, CONTINUE); diff --git a/mainsubs.cpp b/mainsubs.cpp index 98561920..5c2b7423 100644 --- a/mainsubs.cpp +++ b/mainsubs.cpp @@ -2233,8 +2233,11 @@ do_status(void) } //pr.headings = TRUE; // set in class_main; not set for IPhreeqc LDBLE ext = (double) clock() / CLOCKS_PER_SEC; +#define TESTING +#ifndef TESTING dup_print(sformatf("End of Run after %g Seconds.", ext), TRUE); screen_msg(sformatf("\nEnd of Run after %g Seconds.\n", ext)); +#endif // appt this gives output when the charts are active... phrq_io->output_flush(); phrq_io->error_flush(); diff --git a/parse.cpp b/parse.cpp index 3c5da231..70ff78d8 100644 --- a/parse.cpp +++ b/parse.cpp @@ -26,7 +26,7 @@ parse_eq(char *eqn, struct elt_list **elt_ptr, int association) int i; LDBLE coef, l_z; char c; - char *ptr; + const char* cptr; char token[MAX_LENGTH]; paren_count = 0; @@ -53,8 +53,8 @@ parse_eq(char *eqn, struct elt_list **elt_ptr, int association) */ count_trxn = 0; trxn.dz[0] = trxn.dz[1] = trxn.dz[2] = 0.0; - ptr = eqn; - c = ptr[0]; + cptr = eqn; + c = cptr[0]; for (;;) { if (c == '=') @@ -65,11 +65,11 @@ parse_eq(char *eqn, struct elt_list **elt_ptr, int association) error_msg(error_string, CONTINUE); return (ERROR); } - if (get_species(&ptr) == ERROR) + if (get_species(&cptr) == ERROR) { return (ERROR); } - c = ptr[0]; + c = cptr[0]; if (association == FALSE) { trxn.token[count_trxn].coef *= -1.0; @@ -79,10 +79,10 @@ parse_eq(char *eqn, struct elt_list **elt_ptr, int association) /* * Get coefficient, name, and charge of species for dissociation reaction */ - ptr++; + cptr++; if (association == TRUE) { - if (get_species(&ptr) == ERROR) + if (get_species(&cptr) == ERROR) { return (ERROR); } @@ -102,16 +102,16 @@ parse_eq(char *eqn, struct elt_list **elt_ptr, int association) /* * Get reaction species from rhs of equation */ - c = ptr[0]; + c = cptr[0]; for (;;) { if (c == '\0') break; - if (get_species(&ptr) == ERROR) + if (get_species(&cptr) == ERROR) { return (ERROR); } - c = ptr[0]; + c = cptr[0]; if (association == TRUE) { trxn.token[count_trxn].coef *= -1.0; @@ -131,7 +131,7 @@ parse_eq(char *eqn, struct elt_list **elt_ptr, int association) replace("(S)", "", token); replace("(g)", "", token); replace("(G)", "", token); - char *char_ptr = token; + const char *char_ptr = token; if (get_elts_in_species(&char_ptr, trxn.token[0].coef) == ERROR) { @@ -214,7 +214,7 @@ check_eqn(int association) { sumcharge += (trxn.token[i].coef) * (trxn.token[i].z); char * temp_name = string_duplicate(trxn.token[i].name); - char *t_ptr = temp_name; + const char *t_ptr = temp_name; if (get_elts_in_species(&t_ptr, trxn.token[i].coef) == ERROR) { free_check_null(temp_name); @@ -283,7 +283,7 @@ get_charge(char *charge, LDBLE * l_z) */ { int i; - char *ptr; + char* ptr; char c, c1; /* * Charge is zero @@ -333,6 +333,7 @@ get_charge(char *charge, LDBLE * l_z) { if (*ptr != '0') { + char* ptr; *l_z = strtod(charge, &ptr); return (OK); } @@ -404,12 +405,14 @@ get_coef(LDBLE * coef, const char **eqnaddr) { int i; char c, c1; - char *ptr, *ptr1, *rest; + const char* cptr; + const char* rest; + char* ptr1; char token[MAX_LENGTH];; rest = *eqnaddr; - ptr = *eqnaddr; /* address of a position in eqn */ - c = *ptr; /* character in eqn */ + cptr = *eqnaddr; /* address of a position in eqn */ + c = *cptr; /* character in eqn */ *coef = 0.0; /* * No leading sign or number @@ -423,12 +426,12 @@ get_coef(LDBLE * coef, const char **eqnaddr) /* * Leading +, no digits */ - c1 = *(ptr + 1); + c1 = *(cptr + 1); if (c == '+' && (isalpha((int) c1) || (c1 == '(') || (c1 == ')') || (c1 == '[') || (c1 == ']'))) { - *eqnaddr = ++ptr; + *eqnaddr = ++cptr; *coef = 1.0; return (OK); } @@ -439,7 +442,7 @@ get_coef(LDBLE * coef, const char **eqnaddr) (isalpha((int) c1) || (c1 == '(') || (c1 == ')') || (c1 == '[') || (c1 == ']'))) { - *eqnaddr = ++ptr; + *eqnaddr = ++cptr; *coef = -1.0; return (OK); } @@ -459,10 +462,10 @@ get_coef(LDBLE * coef, const char **eqnaddr) error_msg(error_string, CONTINUE); return (ERROR); } - c = *(++ptr); + c = *(++cptr); } token[i] = '\0'; - *eqnaddr = ptr; + *eqnaddr = cptr; errno = 0; *coef = strtod(token, &ptr1); if ((errno == ERANGE) || (*ptr1 != '\0')) @@ -575,7 +578,7 @@ get_elts_in_species(const char **t_ptr, LDBLE coef) char c, c1; LDBLE d; char element[MAX_LENGTH]; - char** t_ptr_save = t_ptr; + const char** t_ptr_save = t_ptr; while (((c = **t_ptr) != '+') && (c != '-') && (c != '\0')) { /* close parenthesis */ @@ -708,7 +711,7 @@ get_secondary(const char **t_ptr, char *element, int *i) { int j; char c; - char *ptr; + const char* cptr; c = *(*t_ptr)++; if (c == '\0') @@ -766,7 +769,7 @@ get_secondary(const char **t_ptr, char *element, int *i) * Check if secondary master species element */ j = *i; - ptr = *t_ptr; + cptr = *t_ptr; if (c == '(') { /* copy parenthesis */ @@ -796,7 +799,7 @@ get_secondary(const char **t_ptr, char *element, int *i) if (c != ')') { *i = j; - *t_ptr = ptr; + *t_ptr = cptr; /* put in closing parenthesis */ } else @@ -830,7 +833,7 @@ get_secondary_in_species(const char **t_ptr, LDBLE coef) char c, c1; LDBLE d; char element[MAX_LENGTH]; - char** t_ptr_save = t_ptr; + const char** t_ptr_save = t_ptr; while (((c = **t_ptr) != '+') && (c != '-') && (c != '\0')) { /* close parenthesis */ @@ -962,7 +965,7 @@ get_num(const char **t_ptr, LDBLE * num) { int i, decimal; char c; - char *ptr1; + char* ptr1; char token[MAX_LENGTH]; *num = 1.0; @@ -1005,7 +1008,7 @@ get_num(const char **t_ptr, LDBLE * num) /* ---------------------------------------------------------------------- */ int Phreeqc:: -get_species(const char **ptr) +get_species(const char **cptr) /* ---------------------------------------------------------------------- */ { /* Function reads next species out of the equation, including optional @@ -1013,7 +1016,7 @@ get_species(const char **ptr) * store in trxn.token[count]. * * Arguments: - * **ptr input, points to the position in the equation to pick up the species. + * **cptr input, points to the position in the equation to pick up the species. * output, points to the next character after the species charge. * */ @@ -1023,12 +1026,12 @@ get_species(const char **ptr) if ((size_t) count_trxn + 1 > trxn.token.size()) trxn.token.resize((size_t)count_trxn + 1); /* coefficient */ - if (get_coef(&(trxn.token[count_trxn].coef), ptr) == ERROR) + if (get_coef(&(trxn.token[count_trxn].coef), cptr) == ERROR) { return (ERROR); } /* name and charge */ - if (get_token(ptr, string, &trxn.token[count_trxn].z, &l) == ERROR) + if (get_token(cptr, string, &trxn.token[count_trxn].z, &l) == ERROR) { return (ERROR); } diff --git a/pitzer.cpp b/pitzer.cpp index 4da1eafd..c7b7247c 100644 --- a/pitzer.cpp +++ b/pitzer.cpp @@ -571,7 +571,7 @@ read_pitzer(void) pitz_param_type pzp_type; int return_value, opt, opt_save; - char *next_char; + const char* next_char; const char *opt_list[] = { "b0", /* 0 */ "b1", /* 1 */ diff --git a/pitzer_structures.cpp b/pitzer_structures.cpp index 93d8ccd3..1d9623c1 100644 --- a/pitzer_structures.cpp +++ b/pitzer_structures.cpp @@ -68,7 +68,7 @@ pitz_param_read(char *string, int n) * */ int l, i, j, k; - char *ptr; + const char* cptr; char token[2 * MAX_LENGTH]; struct pitz_param pzp, *pzp_ptr; @@ -78,13 +78,13 @@ pitz_param_read(char *string, int n) return (NULL); pitz_param_init(&pzp); - ptr = string; - if (copy_token(token, &ptr, &l) == EMPTY) + cptr = string; + if (copy_token(token, &cptr, &l) == EMPTY) return (NULL); - ptr = string; + cptr = string; for (i = 0; i < n; i++) { - int j = copy_token(token, &ptr, &l); + int j = copy_token(token, &cptr, &l); if (j == EMPTY) return (NULL); if (j != UPPER && token[0] != '(') @@ -99,7 +99,7 @@ pitz_param_read(char *string, int n) k = 0; for (i = 0; i < 6; i++) { - if (copy_token(token, &ptr, &l) == EMPTY) + if (copy_token(token, &cptr, &l) == EMPTY) break; j = sscanf(token, SCANFORMAT, &pzp.a[i]); if (j <= 0) diff --git a/prep.cpp b/prep.cpp index 5fbd003b..33aaeae6 100644 --- a/prep.cpp +++ b/prep.cpp @@ -309,9 +309,9 @@ quick_setup(void) /* test that charge and surface match */ cxxSurfaceComp *comp_ptr = use.Get_surface_ptr()->Find_comp(x[i]->surface_comp); char * temp_formula = string_duplicate(comp_ptr->Get_formula().c_str()); - char * ptr = temp_formula; + const char* ptr = temp_formula; copy_token(token, &ptr, &l); - char * ptr1 = token; + const char* ptr1 = token; get_elt(&ptr1, name, &l); ptr1 = strchr(name, '_'); if (ptr1 != NULL) @@ -656,7 +656,7 @@ build_ss_assemblage(void) int row, col; struct master *master_ptr; struct rxn_token *rxn_ptr; - char *ptr; + const char* cptr; if (ss_unknown == NULL) return (OK); @@ -760,8 +760,8 @@ build_ss_assemblage(void) count_elts = 0; paren_count = 0; char * token = string_duplicate(x[i]->phase->formula); - ptr = token; - get_elts_in_species(&ptr, 1.0); + cptr = token; + get_elts_in_species(&cptr, 1.0); free_check_null(token); /* * Go through elements in phase @@ -1373,7 +1373,7 @@ build_pure_phases(void) */ bool stop; std::string token; - char *ptr; + const char* cptr; struct master *master_ptr; struct rxn_token *rxn_ptr; /* @@ -1439,15 +1439,15 @@ build_pure_phases(void) if (comp_ptr->Get_add_formula().size() > 0) { char * char_name = string_duplicate(comp_ptr->Get_add_formula().c_str()); - ptr = char_name; - get_elts_in_species(&ptr, 1.0); + cptr = char_name; + get_elts_in_species(&cptr, 1.0); free_check_null(char_name); } else { char * char_name = string_duplicate(x[i]->phase->formula); - ptr = char_name; - get_elts_in_species(&ptr, 1.0); + cptr = char_name; + get_elts_in_species(&cptr, 1.0); free_check_null(char_name); } /* @@ -1889,8 +1889,8 @@ convert_units(cxxSolution *solution_ptr) else { char * temp_desc = string_duplicate(comp_ref.Get_description().c_str()); - char *ptr = temp_desc; - copy_token(token, &ptr); + const char* cptr = temp_desc; + copy_token(token, &cptr); master_ptr = master_bsearch(token.c_str()); free_check_null(temp_desc); if (master_ptr != NULL) @@ -1988,11 +1988,11 @@ convert_units(cxxSolution *solution_ptr) /* ---------------------------------------------------------------------- */ std::vector Phreeqc:: -get_list_master_ptrs(char *ptr, struct master *master_ptr) +get_list_master_ptrs(const char* cptr, struct master *master_ptr) /* ---------------------------------------------------------------------- */ { /* - * Input: ptr contains a list of one or more master species names + * Input: cptr contains a list of one or more master species names * Output: space is allocated and a list of master species pointers is * returned. */ @@ -2054,7 +2054,7 @@ get_list_master_ptrs(char *ptr, struct master *master_ptr) * First in list is secondary species, Include all valences from input */ master_ptr_list.push_back(master_ptr0); - while (copy_token(token, &ptr, &l) != EMPTY) + while (copy_token(token, &cptr, &l) != EMPTY) { master_ptr = master_bsearch(token); if (master_ptr != NULL) @@ -2947,7 +2947,7 @@ add_surface_charge_balance(void) * Include charge balance in list for mass-balance equations */ int i; - char *ptr; + const char* cptr; std::string token; struct master *master_ptr; @@ -2999,8 +2999,8 @@ add_surface_charge_balance(void) * Include charge balance in list for mass-balance equations */ char * temp_name = string_duplicate(master_ptr->elt->name); - ptr = temp_name; - get_secondary_in_species(&ptr, 1.0); + cptr = temp_name; + get_secondary_in_species(&cptr, 1.0); free_check_null(temp_name); return (OK); @@ -3062,8 +3062,8 @@ add_cd_music_charge_balances(int n) */ { char * temp_name = string_duplicate( master_ptr->elt->name); - char *ptr = temp_name; - get_secondary_in_species(&ptr, s[n]->dz[0]); + const char* cptr = temp_name; + get_secondary_in_species(&cptr, s[n]->dz[0]); free_check_null(temp_name); } /* @@ -3077,8 +3077,8 @@ add_cd_music_charge_balances(int n) */ { char * temp_name = string_duplicate( master_ptr->elt->name); - char *ptr = temp_name; - get_secondary_in_species(&ptr, s[n]->dz[1]); + const char* cptr = temp_name; + get_secondary_in_species(&cptr, s[n]->dz[1]); free_check_null(temp_name); } /* @@ -3092,8 +3092,8 @@ add_cd_music_charge_balances(int n) */ { char * temp_name = string_duplicate(master_ptr->elt->name); - char *ptr = temp_name; - get_secondary_in_species(&ptr, s[n]->dz[2]); + const char* cptr = temp_name; + get_secondary_in_species(&cptr, s[n]->dz[2]); free_check_null(temp_name); } @@ -4183,7 +4183,7 @@ setup_solution(void) */ struct master *master_ptr; cxxSolution *solution_ptr; - char *ptr; + const char* cptr; std::string token; struct master_isotope *master_isotope_ptr; struct phase *phase_ptr; @@ -4223,8 +4223,8 @@ setup_solution(void) comp_ptr = &(comp_it->second); } char * temp_desc = string_duplicate(it->first.c_str()); - ptr = temp_desc; - copy_token(token, &ptr); + cptr = temp_desc; + copy_token(token, &cptr); master_ptr = master_bsearch(token.c_str()); /* * Check that total not <= zero @@ -4263,7 +4263,7 @@ setup_solution(void) /* * Store list of master species pointers, set master[i].in and master[i].rxn for list */ - x[count_unknowns]->master = get_list_master_ptrs(ptr, master_ptr); + x[count_unknowns]->master = get_list_master_ptrs(cptr, master_ptr); if (comp_ptr) { setup_master_rxn(x[count_unknowns]->master, comp_ptr->Get_pe_reaction()); @@ -4288,8 +4288,8 @@ setup_solution(void) */ free_check_null(temp_desc); temp_desc = string_duplicate(it->first.c_str()); - ptr = temp_desc; - copy_token(token, &ptr); + cptr = temp_desc; + copy_token(token, &cptr); Utilities::str_tolower(token); if (strstr(token.c_str(), "alk") != NULL) { @@ -4347,8 +4347,8 @@ setup_solution(void) if (comp_ptr && comp_ptr->Get_equation_name().size() > 0) { char * temp_eq_name = string_duplicate(comp_ptr->Get_equation_name().c_str()); - ptr = temp_eq_name; - copy_token(token, &ptr); + cptr = temp_eq_name; + copy_token(token, &cptr); Utilities::str_tolower(token); if (strstr(token.c_str(), "charge") != NULL) { @@ -5131,8 +5131,8 @@ write_mb_eqn_x(void) { j = count_elts; char * temp_name = string_duplicate(trxn.token[i].s->name); - char * ptr = temp_name; - get_elts_in_species(&ptr, trxn.token[i].coef); + const char* cptr = temp_name; + get_elts_in_species(&cptr, trxn.token[i].coef); free_check_null(temp_name); for (k = j; k < count_elts; k++) { @@ -5153,15 +5153,15 @@ write_mb_eqn_x(void) if (trxn.token[i].s->secondary == NULL) { char * temp_name = string_duplicate(trxn.token[i].s->primary->elt->name); - char *ptr = temp_name; - get_secondary_in_species(&ptr, trxn.token[i].coef); + const char* cptr = temp_name; + get_secondary_in_species(&cptr, trxn.token[i].coef); free_check_null(temp_name); } else { char * temp_name = string_duplicate(trxn.token[i].s->secondary->elt->name); - ptr = temp_name; - get_secondary_in_species(&ptr, trxn.token[i].coef); + cptr = temp_name; + get_secondary_in_species(&cptr, trxn.token[i].coef); free_check_null(temp_name); } } @@ -5194,15 +5194,15 @@ write_mb_for_species_list(int n) if (trxn.token[i].s->secondary == NULL) { char * temp_name = string_duplicate(trxn.token[i].s->primary->elt->name); - char * ptr = temp_name; - get_secondary_in_species(&ptr, trxn.token[i].coef); + const char* cptr = temp_name; + get_secondary_in_species(&cptr, trxn.token[i].coef); free_check_null(temp_name); } else { char * temp_name = string_duplicate(trxn.token[i].s->secondary->elt->name); - char * ptr = temp_name; - if (get_secondary_in_species(&ptr, trxn.token[i].coef) == ERROR) + const char* cptr = temp_name; + if (get_secondary_in_species(&cptr, trxn.token[i].coef) == ERROR) { input_error++; error_string = sformatf( "Error parsing %s.", trxn.token[i].s->secondary->elt->name); @@ -5256,15 +5256,15 @@ write_phase_sys_total(int n) if (trxn.token[i].s->secondary == NULL) { char * temp_name = string_duplicate(trxn.token[i].s->primary->elt->name); - char *ptr = temp_name; - get_secondary_in_species(&ptr, trxn.token[i].coef); + const char* cptr = temp_name; + get_secondary_in_species(&cptr, trxn.token[i].coef); free_check_null(temp_name); } else { char * temp_name = string_duplicate(trxn.token[i].s->secondary->elt->name); - char *ptr = temp_name; - get_secondary_in_species(&ptr, trxn.token[i].coef); + const char* cptr = temp_name; + get_secondary_in_species(&cptr, trxn.token[i].coef); free_check_null(temp_name); } } @@ -6053,8 +6053,8 @@ build_min_exch(void) paren_count = 0; { char * formula = string_duplicate(comp_ref.Get_formula().c_str()); - char * ptr = formula; - get_elts_in_species(&ptr, 1.0); + const char* cptr = formula; + get_elts_in_species(&cptr, 1.0); free_check_null(formula); } #ifdef COMBINE @@ -6187,8 +6187,8 @@ build_min_surface(void) { /* Add specified formula for all types of surfaces */ char * formula = string_duplicate(comp_ptr->Get_formula().c_str()); - char *ptr1 = formula; - get_elts_in_species(&ptr1, 1.0); + const char* cptr1 = formula; + get_elts_in_species(&cptr1, 1.0); free_check_null(formula); } #ifdef COMBINE diff --git a/read.cpp b/read.cpp index 6378124e..88f35496 100644 --- a/read.cpp +++ b/read.cpp @@ -25,7 +25,7 @@ read_input(void) /* ---------------------------------------------------------------------- */ { int i, j, l; - char *ptr; + const char* cptr; char token[2 * MAX_LENGTH]; #define LAST_C_KEYWORD 61 @@ -252,14 +252,14 @@ read_input(void) } else { - ptr = line; - copy_token(token, &ptr, &l); + cptr = line; + copy_token(token, &cptr, &l); #if defined(SWIG_SHARED_OBJ) warning_msg("DATABASE keyword is ignored by IPhreeqc."); #else user_database = (char *) free_check_null(user_database); - user_database = string_duplicate(ptr); + user_database = string_duplicate(cptr); if (string_trim(user_database) == EMPTY) { error_msg("DATABASE file name is missing.", CONTINUE); @@ -393,7 +393,7 @@ read_exchange_species(void) int i; int association; char token[MAX_LENGTH]; - char *ptr; + const char* cptr; struct phase *phase_ptr; struct species *s_ptr; @@ -403,7 +403,7 @@ read_exchange_species(void) LDBLE offset; int return_value, opt, opt_save; - char *next_char; + const char* next_char; const char *opt_list[] = { "no_check", /* 0 */ "check", /* 1 */ @@ -496,8 +496,8 @@ read_exchange_species(void) paren_count = 0; copy_token(token, &next_char, &i); s_ptr->mole_balance = string_hsave(token); - ptr = token; - get_secondary_in_species(&ptr, 1.0); + cptr = token; + get_secondary_in_species(&cptr, 1.0); s_ptr->next_secondary = (struct elt_list *) free_check_null(s_ptr->next_secondary); s_ptr->next_secondary = elt_list_save(); @@ -824,11 +824,11 @@ read_exchange(void) */ int n_user, n_user_end; LDBLE conc; - char *ptr; + const char* cptr; char *description; int return_value, opt; - char *next_char; + const char* next_char; const char *opt_list[] = { "equilibrate", /* 0 */ "equil", /* 1 */ @@ -848,8 +848,8 @@ read_exchange(void) * Read exchange number and description */ - ptr = line; - read_number_description(ptr, &n_user, &n_user_end, &description); + cptr = line; + read_number_description(cptr, &n_user, &n_user_end, &description); /* * Default values + n_user, description */ @@ -927,8 +927,8 @@ read_exchange(void) case OPTION_DEFAULT: { std::string token; - ptr = line; - int i = copy_token(token, &ptr); + cptr = line; + int i = copy_token(token, &cptr); /* * Species formula is stored in token */ @@ -945,9 +945,9 @@ read_exchange(void) temp_exchange.Get_exchange_comps().push_back(temp_comp); comp_ptr = &(temp_exchange.Get_exchange_comps().back()); comp_ptr->Set_formula(token.c_str()); - prev_next_char = ptr; + prev_next_char = cptr; std::string token1; - i = copy_token(token1, &ptr); + i = copy_token(token1, &cptr); if (i == DIGIT) { /* @@ -964,12 +964,12 @@ read_exchange(void) input_error++; break; } - prev_next_char = ptr; - int j = copy_token(token1, &ptr); + prev_next_char = cptr; + int j = copy_token(token1, &cptr); if (j == UPPER || j == LOWER) { comp_ptr->Set_rate_name(token1.c_str()); - if (copy_token(token1, &ptr) != DIGIT) + if (copy_token(token1, &cptr) != DIGIT) { error_string = sformatf( "Expected a coefficient to relate exchange to kinetic reaction, but found:\n %s", @@ -991,8 +991,8 @@ read_exchange(void) /* exchanger conc. is related to mineral or kinetics */ comp_ptr->Set_phase_name(token1.c_str()); - prev_next_char = ptr; - int j = copy_token(token1, &ptr); + prev_next_char = cptr; + int j = copy_token(token1, &cptr); if (j != DIGIT) { if (token1[0] == 'K' || token1[0] == 'k') @@ -1009,8 +1009,8 @@ read_exchange(void) input_error++; break; } - prev_next_char = ptr; - j = copy_token(token1, &ptr); + prev_next_char = cptr; + j = copy_token(token1, &cptr); } @@ -1044,18 +1044,18 @@ read_exchange(void) count_elts = 0; paren_count = 0; char * formula = string_duplicate(token.c_str()); - ptr = formula; - get_elts_in_species(&ptr, conc); + cptr = formula; + get_elts_in_species(&cptr, conc); /* * save formula for adjusting number of exchange sites */ - ptr = formula; + cptr = formula; char *name = string_duplicate(token.c_str()); name[0] = '\0'; LDBLE z; int l; - get_token(&ptr, name, &z, &l); + get_token(&cptr, name, &z, &l); comp_ptr->Set_formula_z(z); free_check_null(formula); free_check_null(name); @@ -1083,7 +1083,7 @@ read_exchange_master_species(void) * Reads master species data from data file or input file */ int j, l; - char *ptr, *ptr1; + const char* cptr, *cptr1; LDBLE l_z; struct element *elts_ptr; struct species *s_ptr; @@ -1098,11 +1098,11 @@ read_exchange_master_species(void) /* * Get element name with valence, allocate space, store */ - ptr = line; + cptr = line; /* * Get element name and save pointer to character string */ - if (copy_token(token, &ptr, &l) != UPPER && token[0] != '[') + if (copy_token(token, &cptr, &l) != UPPER && token[0] != '[') { parse_error++; error_msg("Reading element for master species.", CONTINUE); @@ -1111,8 +1111,8 @@ read_exchange_master_species(void) } /* if (token[0] == '[') { - ptr1 = token; - get_elt(&ptr, element, &l); + cptr1 = token; + get_elt(&cptr, element, &l); strcpy(token, element); } */ @@ -1138,7 +1138,7 @@ read_exchange_master_species(void) /* * Save pointer to species data for master species */ - if ((copy_token(token, &ptr, &l) != UPPER) && + if ((copy_token(token, &cptr, &l) != UPPER) && token[0] != '[' && (strcmp_nocase_arg1(token, "e-") != 0)) { parse_error++; @@ -1153,8 +1153,8 @@ read_exchange_master_species(void) } else { - ptr1 = token; - get_token(&ptr1, token1, &l_z, &l); + cptr1 = token; + get_token(&cptr1, token1, &l_z, &l); master[count_master]->s = s_store(token1, l_z, FALSE); } /* @@ -1189,12 +1189,12 @@ read_gas_phase(void) */ int i, j, l; int n_user, n_user_end; - char *ptr; + const char* cptr; char *description; char token[MAX_LENGTH]; cxxGasPhase temp_gas_phase(this->phrq_io); int return_value, opt; - char *next_char; + const char* next_char; const char *opt_list[] = { "pressure", /* 0 */ "volume", /* 1 */ @@ -1210,8 +1210,8 @@ read_gas_phase(void) /* * Read gas_phase number */ - ptr = line; - read_number_description(ptr, &n_user, &n_user_end, &description); + cptr = line; + read_number_description(cptr, &n_user, &n_user_end, &description); temp_gas_phase.Set_n_user(n_user); temp_gas_phase.Set_n_user_end(n_user_end); @@ -1302,10 +1302,10 @@ read_gas_phase(void) /* * Read name */ - ptr = line; - copy_token(token, &ptr, &l); + cptr = line; + copy_token(token, &cptr, &l); temp_comp.Set_phase_name(token); - if ((j = copy_token(token, &ptr, &l)) == EMPTY) + if ((j = copy_token(token, &cptr, &l)) == EMPTY) { temp_comp.Set_p_read(NAN); temp_gas_phase.Get_gas_comps().push_back(temp_comp); @@ -1376,12 +1376,12 @@ read_inverse(void) */ int n, j; int n_user, n_user_end; - char *ptr; + const char* cptr; char *description; LDBLE range_max, inv_tol, water_uncertainty; int return_value, opt, opt_save; - char *next_char; + const char* next_char; const char *opt_list[] = { "solutions", /* 0 */ "uncertainty", /* 1 */ @@ -1413,11 +1413,11 @@ read_inverse(void) }; int count_opt_list = 27; - ptr = line; + cptr = line; /* * Read solution number and description */ - read_number_description(ptr, &n_user, &n_user_end, &description); + read_number_description(cptr, &n_user, &n_user_end, &description); /* * Malloc space for solution data */ @@ -1484,10 +1484,10 @@ read_inverse(void) case 1: /* uncertainty */ case 2: /* uncertainties */ inverse[n].uncertainties = - (LDBLE *) free_check_null(inverse[n].uncertainties); + (LDBLE*)free_check_null(inverse[n].uncertainties); inverse[n].uncertainties = read_list_doubles(&next_char, - &inverse[n].count_uncertainties); + &inverse[n].count_uncertainties); opt_save = OPTION_ERROR; break; case 3: /* balances */ @@ -1536,7 +1536,7 @@ read_inverse(void) case 17: /* force_solution */ case 18: /* force_solutions */ inverse[n].force_solns = - (int *) free_check_null(inverse[n].force_solns); + (int*)free_check_null(inverse[n].force_solns); inverse[n].force_solns = read_list_t_f(&next_char, &inverse[n].count_force_solns); opt_save = OPTION_ERROR; @@ -1569,29 +1569,35 @@ read_inverse(void) opt_save = OPTION_ERROR; break; case 25: /* lon_netpath */ - /*copy_token(file_name, &next_char, &l); */ - if (string_trim(next_char) != EMPTY) + { + std::string temp_name(next_char); + string_trim(temp_name); + if (temp_name.size() > 0) { - inverse[n].netpath = string_hsave(next_char); + inverse[n].netpath = string_hsave(temp_name.c_str()); } else { inverse[n].netpath = string_hsave("netpath"); } opt_save = OPTION_ERROR; - break; + } + break; case 26: /* pat_netpath */ - /*copy_token(file_name, &next_char, &l); */ - if (string_trim(next_char) != EMPTY) + { + std::string temp_name(next_char); + string_trim(temp_name); + if (temp_name.size() > 0) { - inverse[n].pat = string_hsave(next_char); + inverse[n].pat = string_hsave(temp_name.c_str()); } else { inverse[n].pat = string_hsave("netpath"); } opt_save = OPTION_ERROR; - break; + } + break; } if (return_value == EOF || return_value == KEYWORD) break; @@ -1630,7 +1636,7 @@ read_inverse(void) /* ---------------------------------------------------------------------- */ int Phreeqc:: -read_inv_balances(struct inverse *inverse_ptr, char *ptr) +read_inv_balances(struct inverse *inverse_ptr, const char* cptr) /* ---------------------------------------------------------------------- */ { int j, l, count; @@ -1638,7 +1644,7 @@ read_inv_balances(struct inverse *inverse_ptr, char *ptr) /* * Read element name */ - j = copy_token(token, &ptr, &l); + j = copy_token(token, &cptr, &l); if (j == EMPTY) { return (OK); @@ -1661,7 +1667,7 @@ read_inv_balances(struct inverse *inverse_ptr, char *ptr) * Read element uncertainties */ inverse_ptr->elts[inverse_ptr->count_elts].uncertainties = - read_list_doubles(&ptr, &count); + read_list_doubles(&cptr, &count); inverse_ptr->elts[inverse_ptr->count_elts].count_uncertainties = count; inverse_ptr->count_elts++; @@ -1670,7 +1676,7 @@ read_inv_balances(struct inverse *inverse_ptr, char *ptr) { inverse_ptr->ph_uncertainties = (LDBLE *) free_check_null(inverse_ptr->ph_uncertainties); - inverse_ptr->ph_uncertainties = read_list_doubles(&ptr, &count); + inverse_ptr->ph_uncertainties = read_list_doubles(&cptr, &count); inverse_ptr->count_ph_uncertainties = count; } return (OK); @@ -1678,21 +1684,21 @@ read_inv_balances(struct inverse *inverse_ptr, char *ptr) /* ---------------------------------------------------------------------- */ int Phreeqc:: -read_inv_isotopes(struct inverse *inverse_ptr, char *ptr) +read_inv_isotopes(struct inverse *inverse_ptr, const char* cptr) /* ---------------------------------------------------------------------- */ { int i, j, l, l1, l2, count; LDBLE isotope_number; char token[MAX_LENGTH], token1[MAX_LENGTH]; - char *ptr1, *ptr2; + const char* cptr1, *ptr2; const char * redox_name, *element_name; /* * Read element name */ - ptr1 = ptr; - j = copy_token(token, &ptr1, &l); + cptr1 = cptr; + j = copy_token(token, &cptr1, &l); /* - * ptr1 is start of uncertainties + * cptr1 is start of uncertainties */ if (j == EMPTY) { @@ -1769,24 +1775,24 @@ read_inv_isotopes(struct inverse *inverse_ptr, char *ptr) * Read isotope uncertainties */ inverse_ptr->i_u[inverse_ptr->count_i_u].uncertainties = - read_list_doubles(&ptr1, &count); + read_list_doubles(&cptr1, &count); inverse_ptr->i_u[inverse_ptr->count_i_u].count_uncertainties = count; inverse_ptr->count_i_u++; return (OK); } /* ---------------------------------------------------------------------- */ int Phreeqc:: -read_inv_phases(struct inverse *inverse_ptr, char *ptr) +read_inv_phases(struct inverse *inverse_ptr, const char* cptr) /* ---------------------------------------------------------------------- */ { int j, l; char token[MAX_LENGTH], token1[MAX_LENGTH]; - char *ptr1; + const char* cptr1; std::vector isotopes; /* * Read phase name */ - j = copy_token(token, &ptr, &l); + j = copy_token(token, &cptr, &l); if (j == EMPTY) return (OK); inverse_ptr->phases = (struct inv_phases *) PHRQ_realloc(inverse_ptr->phases, @@ -1802,7 +1808,7 @@ read_inv_phases(struct inverse *inverse_ptr, char *ptr) for (;;) { cxxSolutionIsotope temp_isotope; - j = copy_token(token, &ptr, &l); + j = copy_token(token, &cptr, &l); if (j == EMPTY) break; strcpy(token1, token); @@ -1826,14 +1832,14 @@ read_inv_phases(struct inverse *inverse_ptr, char *ptr) /* * read isotope data */ - ptr1 = token; + cptr1 = token; /* isotope number */ - get_num(&ptr1, &dummy); + get_num(&cptr1, &dummy); temp_isotope.Set_isotope_number(dummy); - if (ptr1[0] == '\0' || isupper((int) ptr1[0]) == FALSE) + if (cptr1[0] == '\0' || isupper((int) cptr1[0]) == FALSE) { - error_string = sformatf( "Expecting element name: %s.", ptr1); + error_string = sformatf( "Expecting element name: %s.", cptr1); error_msg(error_string, CONTINUE); error_msg(line_save, CONTINUE); input_error++; @@ -1841,10 +1847,10 @@ read_inv_phases(struct inverse *inverse_ptr, char *ptr) } /* element name */ - temp_isotope.Set_elt_name(ptr1); + temp_isotope.Set_elt_name(cptr1); /* ratio */ - j = copy_token(token, &ptr, &l); + j = copy_token(token, &cptr, &l); if (j != DIGIT) { error_msg("Expecting isotope ratio for phase.", CONTINUE); @@ -1856,8 +1862,8 @@ read_inv_phases(struct inverse *inverse_ptr, char *ptr) temp_isotope.Set_ratio(dummy); /* read and store isotope ratio uncertainty */ - prev_next_char = ptr; - if (copy_token(token, &ptr, &l) != DIGIT) + prev_next_char = cptr; + if (copy_token(token, &cptr, &l) != DIGIT) { input_error++; error_string = sformatf( @@ -1930,14 +1936,14 @@ read_kinetics(void) /* * Read kinetics */ - char *ptr; + const char* cptr; char *description; std::string token; int n_user, n_user_end; LDBLE step; int return_value, opt; - char *next_char; + const char* next_char; const char *opt_list[] = { "tol", /* 0 */ "m", /* 1 */ @@ -1961,8 +1967,8 @@ read_kinetics(void) /* * Read kinetics number */ - ptr = line; - read_number_description(ptr, &n_user, &n_user_end, &description); + cptr = line; + read_number_description(cptr, &n_user, &n_user_end, &description); cxxKinetics temp_kinetics(this->phrq_io); temp_kinetics.Set_n_user(n_user); temp_kinetics.Set_n_user_end(n_user_end); @@ -2000,8 +2006,8 @@ read_kinetics(void) delete kinetics_comp_ptr; } kinetics_comp_ptr = new cxxKineticsComp; - ptr = line; - copy_token(token, &ptr); + cptr = line; + copy_token(token, &cptr); kinetics_comp_ptr->Set_rate_name(token.c_str()); break; case OPTION_ERROR: @@ -2021,6 +2027,7 @@ read_kinetics(void) prev_next_char = next_char; if (copy_token(token, &next_char) == DIGIT) { + char* ptr; kinetics_comp_ptr->Set_tol(strtod(token.c_str(), &ptr)); } else @@ -2045,6 +2052,7 @@ read_kinetics(void) prev_next_char = next_char; if (copy_token(token, &next_char) == DIGIT) { + char* ptr; kinetics_comp_ptr->Set_m(strtod(token.c_str(), &ptr)); } else @@ -2069,6 +2077,7 @@ read_kinetics(void) prev_next_char = next_char; if (copy_token(token, &next_char) == DIGIT) { + char* ptr; kinetics_comp_ptr->Set_m0(strtod(token.c_str(), &ptr)); } else @@ -2099,6 +2108,7 @@ read_kinetics(void) */ if (j == DIGIT) { + char* ptr; kinetics_comp_ptr->Get_d_params().push_back(strtod(token.c_str(), &ptr)); } else @@ -2123,11 +2133,11 @@ read_kinetics(void) /* * Store reactant name, default coefficient */ - ptr = next_char; + cptr = next_char; bool have_name = false; std::string name; LDBLE coef = 1; - while (copy_token(token, &ptr) != EMPTY) + while (copy_token(token, &cptr) != EMPTY) { coef = 1; if (isalpha((int) token[0]) || (token[0] == '(') @@ -2207,6 +2217,7 @@ read_kinetics(void) } else { + char* ptr; step = strtod(token.c_str(), &ptr); temp_kinetics.Get_steps().push_back(step); } @@ -2278,6 +2289,7 @@ read_kinetics(void) int j = copy_token(token, &next_char); if (j == DIGIT) { + char* ptr; temp_kinetics.Set_rk((int) strtod(token.c_str(), &ptr)); } else if (j == EMPTY) @@ -2297,6 +2309,7 @@ read_kinetics(void) int j = copy_token(token, &next_char); if (j == DIGIT) { + char* ptr; temp_kinetics.Set_bad_step_max((int) strtod(token.c_str(), &ptr)); } else if (j == EMPTY) @@ -2318,6 +2331,7 @@ read_kinetics(void) int j = copy_token(token, &next_char); if (j == DIGIT) { + char* ptr; temp_kinetics.Set_cvode_steps((int) strtod(token.c_str(), &ptr)); } else if (j == EMPTY) @@ -2337,6 +2351,7 @@ read_kinetics(void) int j = copy_token(token, &next_char); if (j == DIGIT) { + char* ptr; temp_kinetics.Set_cvode_order((int) strtod(token.c_str(), &ptr)); } else if (j == EMPTY) @@ -2415,7 +2430,7 @@ read_kinetics(void) } /* ---------------------------------------------------------------------- */ LDBLE * Phreeqc:: -read_list_doubles(char **ptr, int *count_doubles) +read_list_doubles(const char **cptr, int *count_doubles) /* ---------------------------------------------------------------------- */ { /* @@ -2423,7 +2438,7 @@ read_list_doubles(char **ptr, int *count_doubles) * a LDBLE cannot be read from a token. * * Arguments: - * ptr entry: points to line to read from + * cptr entry: points to line to read from * exit: points to next non-LDBLE token or end of line * * count_doubles exit: number of LDBLEs read @@ -2435,7 +2450,7 @@ read_list_doubles(char **ptr, int *count_doubles) LDBLE *LDBLE_list; char token[MAX_LENGTH]; LDBLE value; - char *ptr_save; + const char* cptr_save; int l; LDBLE_list = (LDBLE *) PHRQ_malloc(sizeof(LDBLE)); @@ -2443,8 +2458,8 @@ read_list_doubles(char **ptr, int *count_doubles) malloc_error(); *count_doubles = 0; - ptr_save = *ptr; - while (copy_token(token, ptr, &l) != EMPTY) + cptr_save = *cptr; + while (copy_token(token, cptr, &l) != EMPTY) { if (sscanf(token, SCANFORMAT, &value) == 1) { @@ -2456,11 +2471,11 @@ read_list_doubles(char **ptr, int *count_doubles) if (LDBLE_list == NULL) malloc_error(); LDBLE_list[(*count_doubles) - 1] = value; - ptr_save = *ptr; + cptr_save = *cptr; } else { - *ptr = ptr_save; + *cptr = cptr_save; break; } } @@ -2469,7 +2484,7 @@ read_list_doubles(char **ptr, int *count_doubles) /* ---------------------------------------------------------------------- */ int * Phreeqc:: -read_list_ints(char **ptr, int *count_ints, int positive) +read_list_ints(const char **cptr, int *count_ints, int positive) /* ---------------------------------------------------------------------- */ { /* @@ -2477,7 +2492,7 @@ read_list_ints(char **ptr, int *count_ints, int positive) * an int cannot be read from a token. * * Arguments: - * ptr entry: points to line to read from + * cptr entry: points to line to read from * exit: points to next non-int token or end of line * * count_ints exit: number of LDBLEs read @@ -2491,15 +2506,15 @@ read_list_ints(char **ptr, int *count_ints, int positive) char token[MAX_LENGTH]; int value; int l; - char *ptr_save; + const char* cptr_save; int_list = (int *) PHRQ_malloc(sizeof(int)); if (int_list == NULL) malloc_error(); *count_ints = 0; - ptr_save = *ptr; - while (copy_token(token, ptr, &l) != EMPTY) + cptr_save = *cptr; + while (copy_token(token, cptr, &l) != EMPTY) { if (sscanf(token, "%d", &value) == 1) { @@ -2519,11 +2534,11 @@ read_list_ints(char **ptr, int *count_ints, int positive) error_msg(line_save, CONTINUE); input_error++; } - ptr_save = *ptr; + cptr_save = *cptr; } else { - *ptr = ptr_save; + *cptr = cptr_save; break; } } @@ -2532,7 +2547,7 @@ read_list_ints(char **ptr, int *count_ints, int positive) /* ---------------------------------------------------------------------- */ int * Phreeqc:: -read_list_ints_range(char **ptr, int *count_ints, int positive, int *int_list) +read_list_ints_range(const char **cptr, int *count_ints, int positive, int *int_list) /* ---------------------------------------------------------------------- */ { /* @@ -2540,7 +2555,7 @@ read_list_ints_range(char **ptr, int *count_ints, int positive, int *int_list) * an int cannot be read from a token. * * Arguments: - * ptr entry: points to line to read from + * cptr entry: points to line to read from * exit: points to next non-int token or end of line * * count_ints entry: number of ints already in list @@ -2553,7 +2568,7 @@ read_list_ints_range(char **ptr, int *count_ints, int positive, int *int_list) char token[MAX_LENGTH]; int value, value1, value2; int i, l; - char *ptr_save; + const char* cptr_save; if (int_list == NULL) { @@ -2565,8 +2580,8 @@ read_list_ints_range(char **ptr, int *count_ints, int positive, int *int_list) } *count_ints = 0; } - ptr_save = *ptr; - while (copy_token(token, ptr, &l) != EMPTY) + cptr_save = *cptr; + while (copy_token(token, cptr, &l) != EMPTY) { if (sscanf(token, "%d", &value) == 1) { @@ -2628,11 +2643,11 @@ read_list_ints_range(char **ptr, int *count_ints, int positive, int *int_list) } } } - ptr_save = *ptr; + cptr_save = *cptr; } else { - *ptr = ptr_save; + *cptr = cptr_save; break; } } @@ -2641,7 +2656,7 @@ read_list_ints_range(char **ptr, int *count_ints, int positive, int *int_list) /* ---------------------------------------------------------------------- */ int * Phreeqc:: -read_list_t_f(char **ptr, int *count_ints) +read_list_t_f(const char **cptr, int *count_ints) /* ---------------------------------------------------------------------- */ { /* @@ -2649,7 +2664,7 @@ read_list_t_f(char **ptr, int *count_ints) * until non- t or f is found * * Arguments: - * ptr entry: points to line to read from + * cptr entry: points to line to read from * exit: points to next non-int token or end of line * * count_ints exit: number of LDBLEs read @@ -2669,7 +2684,7 @@ read_list_t_f(char **ptr, int *count_ints) malloc_error(); *count_ints = 0; - while (copy_token(token, ptr, &l) != EMPTY) + while (copy_token(token, cptr, &l) != EMPTY) { str_tolower(token); if (token[0] == 't') @@ -2700,15 +2715,17 @@ read_list_t_f(char **ptr, int *count_ints) /* ---------------------------------------------------------------------- */ int Phreeqc:: -read_log_k_only(char *ptr, LDBLE * log_k) +read_log_k_only(const char* cptr_in, LDBLE * log_k) /* ---------------------------------------------------------------------- */ { /* * Read log k */ *log_k = 0.0; - replace("=", " ", ptr); - if (sscanf(ptr, SCANFORMAT, log_k) < 1) + std::string stds(cptr_in); + replace(stds, "=", " "); + //replace("=", " ", cptr); + if (sscanf(stds.c_str(), SCANFORMAT, log_k) < 1) { input_error++; error_msg("Expecting log k.", CONTINUE); @@ -2718,12 +2735,14 @@ read_log_k_only(char *ptr, LDBLE * log_k) } /* ---------------------------------------------------------------------- */ int Phreeqc:: -read_t_c_only(char *ptr, LDBLE *t_c) +read_t_c_only(const char* cptr_in, LDBLE *t_c) /* ---------------------------------------------------------------------- */ { *t_c = 0.0; - replace("=", " ", ptr); - if (sscanf(ptr, SCANFORMAT, t_c) < 1) + std::string stds(cptr_in); + replace(stds, "=", " "); + //replace("=", " ", cptr); + if (sscanf(stds.c_str(), SCANFORMAT, t_c) < 1) { input_error++; error_msg("Expecting numeric value for critical temperature T_c (K)", CONTINUE); @@ -2733,12 +2752,13 @@ read_t_c_only(char *ptr, LDBLE *t_c) } /* ---------------------------------------------------------------------- */ int Phreeqc:: -read_p_c_only(char *ptr, LDBLE * p_c) +read_p_c_only(const char* cptr, LDBLE * p_c) /* ---------------------------------------------------------------------- */ { *p_c = 0.0; - replace("=", " ", ptr); - if (sscanf(ptr, SCANFORMAT, p_c) < 1) + std::string stds(cptr); + replace(stds, "=", " "); + if (sscanf(stds.c_str(), SCANFORMAT, p_c) < 1) { input_error++; error_msg("Expecting numeric value for critical pressure P_c (atm)", CONTINUE); @@ -2748,12 +2768,13 @@ read_p_c_only(char *ptr, LDBLE * p_c) } /* ---------------------------------------------------------------------- */ int Phreeqc:: -read_omega_only(char *ptr, LDBLE *omega) +read_omega_only(const char* cptr, LDBLE *omega) /* ---------------------------------------------------------------------- */ { *omega = 0.0; - replace("=", " ", ptr); - if (sscanf(ptr, SCANFORMAT, omega) < 1) + std::string stds(cptr); + replace(stds, "=", " "); + if (sscanf(stds.c_str(), SCANFORMAT, omega) < 1) { input_error++; error_msg("Expecting numeric value for acentric factor Omega", CONTINUE); @@ -2763,7 +2784,7 @@ read_omega_only(char *ptr, LDBLE *omega) } /* ---------------------------------------------------------------------- */ int Phreeqc:: -read_aq_species_vm_parms(char *ptr, LDBLE * delta_v) +read_aq_species_vm_parms(const char* cptr, LDBLE * delta_v) /* ---------------------------------------------------------------------- */ { int j; @@ -2778,7 +2799,7 @@ read_aq_species_vm_parms(char *ptr, LDBLE * delta_v) /* Vmax, dmax... delta_v[10] = 999.0; delta_v[11] = 1.0; */ - j = sscanf(ptr, SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT /*SCANFORMAT SCANFORMAT */, + j = sscanf(cptr, SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT /*SCANFORMAT SCANFORMAT */, /* a1..a4 */ &(delta_v[0]), &(delta_v[1]), &(delta_v[2]), &(delta_v[3]), /* wref */ @@ -2809,7 +2830,7 @@ read_aq_species_vm_parms(char *ptr, LDBLE * delta_v) } /* ---------------------------------------------------------------------- */ int Phreeqc:: -read_vm_only(char *ptr, LDBLE * delta_v, DELTA_V_UNIT * units) +read_vm_only(const char* cptr, LDBLE * delta_v, DELTA_V_UNIT * units) /* ---------------------------------------------------------------------- */ { int j, l; @@ -2821,7 +2842,7 @@ read_vm_only(char *ptr, LDBLE * delta_v, DELTA_V_UNIT * units) { delta_v[j] = 0.0; } - j = sscanf(ptr, SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT, + j = sscanf(cptr, SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT, &(delta_v[0]), &(delta_v[1]), &(delta_v[2]), &(delta_v[3]), &(delta_v[4]), &(delta_v[5]), &(delta_v[6]), &(delta_v[7])); if (j < 1) @@ -2837,7 +2858,7 @@ read_vm_only(char *ptr, LDBLE * delta_v, DELTA_V_UNIT * units) *units = cm3_per_mol; do { - j = copy_token(token, &ptr, &l); + j = copy_token(token, &cptr, &l); } while (j == DIGIT); if (j == EMPTY) @@ -2875,7 +2896,7 @@ read_vm_only(char *ptr, LDBLE * delta_v, DELTA_V_UNIT * units) /* ---------------------------------------------------------------------- */ int Phreeqc:: -read_phase_vm(char *ptr, LDBLE * delta_v, DELTA_V_UNIT * units) +read_phase_vm(const char* cptr, LDBLE * delta_v, DELTA_V_UNIT * units) /* ---------------------------------------------------------------------- */ { int j, l; @@ -2887,7 +2908,7 @@ read_phase_vm(char *ptr, LDBLE * delta_v, DELTA_V_UNIT * units) { delta_v[j] = 0.0; } - j = sscanf(ptr, SCANFORMAT /*SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT*/, + j = sscanf(cptr, SCANFORMAT /*SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT*/, &(delta_v[0])/*, &(delta_v[1]), &(delta_v[2]), &(delta_v[3]), &(delta_v[4]), &(delta_v[5]), &(delta_v[6]), &(delta_v[7])*/); if (j < 1) @@ -2903,7 +2924,7 @@ read_phase_vm(char *ptr, LDBLE * delta_v, DELTA_V_UNIT * units) *units = cm3_per_mol; do { - j = copy_token(token, &ptr, &l); + j = copy_token(token, &cptr, &l); } while (j == DIGIT); if (j == EMPTY) @@ -2943,7 +2964,7 @@ read_phase_vm(char *ptr, LDBLE * delta_v, DELTA_V_UNIT * units) /* ---------------------------------------------------------------------- */ int Phreeqc:: -read_delta_h_only(char *ptr, LDBLE * delta_h, DELTA_H_UNIT * units) +read_delta_h_only(const char* cptr_in, LDBLE * delta_h, DELTA_H_UNIT * units) /* ---------------------------------------------------------------------- */ { int j, l, kilo, joul; @@ -2952,8 +2973,10 @@ read_delta_h_only(char *ptr, LDBLE * delta_h, DELTA_H_UNIT * units) * Read delta H */ *delta_h = 0.0; - replace("=", " ", ptr); - j = copy_token(token, &ptr, &l); + std::string stds(cptr_in); + replace(stds, "=", " "); + const char* cptr = stds.c_str(); + j = copy_token(token, &cptr, &l); if (j == EMPTY) { input_error++; @@ -2969,7 +2992,7 @@ read_delta_h_only(char *ptr, LDBLE * delta_h, DELTA_H_UNIT * units) /* * Read delta H units */ - j = copy_token(token, &ptr, &l); + j = copy_token(token, &cptr, &l); *units = kjoules; kilo = TRUE; joul = TRUE; @@ -3013,7 +3036,7 @@ read_delta_h_only(char *ptr, LDBLE * delta_h, DELTA_H_UNIT * units) } /* ---------------------------------------------------------------------- */ int Phreeqc:: -read_analytical_expression_only(char *ptr, LDBLE * log_k) +read_analytical_expression_only(const char* cptr, LDBLE * log_k) /* ---------------------------------------------------------------------- */ { int j; @@ -3025,7 +3048,7 @@ read_analytical_expression_only(char *ptr, LDBLE * log_k) { log_k[j] = 0.0; } - j = sscanf(ptr, SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT, + j = sscanf(cptr, SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT, &(log_k[0]), &(log_k[1]), &(log_k[2]), &(log_k[3]), &(log_k[4]), &(log_k[5])); if (j < 1) @@ -3041,7 +3064,7 @@ read_analytical_expression_only(char *ptr, LDBLE * log_k) /* VP: Density Start */ /* ---------------------------------------------------------------------- */ int Phreeqc:: -read_millero_abcdef (char *ptr, LDBLE * abcdef) +read_millero_abcdef (const char* cptr, LDBLE * abcdef) /* ---------------------------------------------------------------------- */ { int j; @@ -3052,7 +3075,7 @@ read_millero_abcdef (char *ptr, LDBLE * abcdef) { abcdef[j] = 0.0; } - j = sscanf (ptr, SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT, + j = sscanf (cptr, SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT, &(abcdef[0]), &(abcdef[1]), &(abcdef[2]), &(abcdef[3]), &(abcdef[4]), &(abcdef[5]), &(abcdef[6])); if (j < 1) { @@ -3067,7 +3090,7 @@ read_millero_abcdef (char *ptr, LDBLE * abcdef) /* ---------------------------------------------------------------------- */ int Phreeqc:: -read_viscosity_parms(char *ptr, LDBLE * Jones_Dole) +read_viscosity_parms(const char* cptr, LDBLE * Jones_Dole) /* ---------------------------------------------------------------------- */ { int j; @@ -3078,7 +3101,7 @@ read_viscosity_parms(char *ptr, LDBLE * Jones_Dole) { Jones_Dole[j] = 0.0; } - j = sscanf (ptr, SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT, + j = sscanf (cptr, SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT SCANFORMAT, &(Jones_Dole[0]), &(Jones_Dole[1]), &(Jones_Dole[2]), &(Jones_Dole[3]), &(Jones_Dole[4]), &(Jones_Dole[5]), &(Jones_Dole[6]), &(Jones_Dole[7]), &(Jones_Dole[8]), &(Jones_Dole[9])); if (j < 1) { @@ -3109,15 +3132,15 @@ read_incremental_reactions(void) * */ int j, l; - char *ptr; + const char* cptr; char token[MAX_LENGTH]; - ptr = line; + cptr = line; /* read keyword */ - copy_token(token, &ptr, &l); + copy_token(token, &cptr, &l); /* read true or false */ - incremental_reactions = get_true_false(ptr, TRUE); + incremental_reactions = get_true_false(cptr, TRUE); /* * find next keyword */ @@ -3144,7 +3167,7 @@ read_master_species(void) * Reads master species data from data file or input file */ int j, i, l; - char *ptr, *ptr1; + const char* cptr, *cptr1; LDBLE l_z; struct element *elts_ptr; struct species *s_ptr; @@ -3161,11 +3184,11 @@ read_master_species(void) /* * Get element name with valence, allocate space, store */ - ptr = line; + cptr = line; /* * Get element name and save pointer to character string */ - if (copy_token(token, &ptr, &l) != UPPER && token[0] != '[') + if (copy_token(token, &cptr, &l) != UPPER && token[0] != '[') { parse_error++; error_msg("Reading element for master species.", CONTINUE); @@ -3174,8 +3197,8 @@ read_master_species(void) } /* if (token[0] == '[') { - ptr1 = token; - get_elt(&ptr, element, &l); + cptr1 = token; + get_elt(&cptr, element, &l); strcpy(token, element); } */ @@ -3202,7 +3225,7 @@ read_master_species(void) /* * Save pointer to species data for master species */ - if ((copy_token(token, &ptr, &l) != UPPER) && + if ((copy_token(token, &cptr, &l) != UPPER) && token[0] != '[' && (strcmp_nocase_arg1(token, "e-") != 0)) { parse_error++; @@ -3218,8 +3241,8 @@ read_master_species(void) } else { - ptr1 = token; - get_token(&ptr1, token1, &l_z, &l); + cptr1 = token; + get_token(&cptr1, token1, &l_z, &l); master[count_master]->s = s_store(token1, l_z, FALSE); } @@ -3239,7 +3262,7 @@ read_master_species(void) /* * Read alkalinity for species */ - copy_token(token, &ptr, &l); + copy_token(token, &cptr, &l); i = sscanf(token, SCANFORMAT, &master[count_master]->alk); if (i != 1) { @@ -3261,7 +3284,7 @@ read_master_species(void) /* * Read default gfw for species */ - i = copy_token(token, &ptr, &l); + i = copy_token(token, &cptr, &l); if (i == DIGIT) { (void)sscanf(token, SCANFORMAT, &master[count_master]->gfw); @@ -3297,7 +3320,7 @@ read_master_species(void) if (strcmp(master[count_master]->elt->name, "E") != 0) { elts_ptr = master[count_master]->elt; - i = copy_token(token, &ptr, &l); + i = copy_token(token, &cptr, &l); if (i == DIGIT) { (void)sscanf(token, SCANFORMAT, &elts_ptr->gfw); @@ -3343,7 +3366,7 @@ read_mix(void) int n_solution; LDBLE fraction; int j, i, l; - char *ptr; + const char* cptr; char token[MAX_LENGTH]; char *description; cxxMix temp_mix; @@ -3351,8 +3374,8 @@ read_mix(void) /* * Read mix number */ - ptr = line; - read_number_description(ptr, &n_user, &n_user_end, &description); + cptr = line; + read_number_description(cptr, &n_user, &n_user_end, &description); temp_mix.Set_n_user(n_user); temp_mix.Set_n_user_end(n_user); @@ -3378,11 +3401,11 @@ read_mix(void) { break; } - ptr = line; + cptr = line; /* * Read n_user */ - i = copy_token(token, &ptr, &l); + i = copy_token(token, &cptr, &l); if (i == DIGIT) { (void)sscanf(token, "%d ", &n_solution); @@ -3397,7 +3420,7 @@ read_mix(void) /* * Read fraction for solution */ - copy_token(token, &ptr, &l); + copy_token(token, &cptr, &l); j = sscanf(token, SCANFORMAT, &fraction); if (j != 1) { @@ -3445,14 +3468,14 @@ read_entity_mix(std::map &mix_map) int n_solution; LDBLE fraction; int j, i, l; - char *ptr; + const char* cptr; char token[MAX_LENGTH]; cxxMix temp_mix; /* * Read mix number */ - ptr = line; + cptr = line; temp_mix.read_number_description(line); /* * Read mixture data @@ -3465,11 +3488,11 @@ read_entity_mix(std::map &mix_map) { break; } - ptr = line; + cptr = line; /* * Read n_user */ - i = copy_token(token, &ptr, &l); + i = copy_token(token, &cptr, &l); if (i == DIGIT) { (void)sscanf(token, "%d ", &n_solution); @@ -3484,7 +3507,7 @@ read_entity_mix(std::map &mix_map) /* * Read fraction for entity */ - copy_token(token, &ptr, &l); + copy_token(token, &cptr, &l); j = sscanf(token, SCANFORMAT, &fraction); if (j != 1) { @@ -3510,19 +3533,19 @@ read_entity_mix(std::map &mix_map) } /* ---------------------------------------------------------------------- */ int Phreeqc:: -read_number_description(char *ptr, int *n_user, +read_number_description(const char* cptr, int *n_user, int *n_user_end, char **description, int allow_negative) /* ---------------------------------------------------------------------- */ { int l, n; char token[MAX_LENGTH]; - char *ptr1; + const char* cptr1; /* * Read user number, allow negative numbers Oct 3, 2011 */ - copy_token(token, &ptr, &l); // keyword - ptr1 = ptr; - copy_token(token, &ptr, &l); + copy_token(token, &cptr, &l); // keyword + cptr1 = cptr; + copy_token(token, &cptr, &l); if (!isdigit(token[0]) && token[0] != '-') { @@ -3555,7 +3578,7 @@ read_number_description(char *ptr, int *n_user, error_msg(error_string, CONTINUE); input_error++; } - ptr1 = ptr; + cptr1 = cptr; } else { @@ -3574,7 +3597,7 @@ read_number_description(char *ptr, int *n_user, input_error++; } *n_user_end = *n_user; - ptr1 = ptr; + cptr1 = cptr; }; } if (*n_user < 0 && allow_negative == FALSE) @@ -3586,8 +3609,8 @@ read_number_description(char *ptr, int *n_user, /* * Read description */ - for (; isspace((int) ptr1[0]); ptr1++); - *description = string_duplicate(ptr1); + for (; isspace((int) cptr1[0]); cptr1++); + *description = string_duplicate(cptr1); return (OK); } @@ -3601,7 +3624,7 @@ read_phases(void) */ int j, i, l; int association; - char *ptr; + const char* cptr; char token[MAX_LENGTH]; char token1[MAX_LENGTH]; struct phase *phase_ptr; @@ -3609,7 +3632,7 @@ read_phases(void) struct rxn_token *token_ptr; int return_value, opt, opt_save; - char *next_char; + const char* next_char; const char *opt_list[] = { "no_check", /* 0 */ "check", /* 1 */ @@ -3771,8 +3794,8 @@ read_phases(void) * Get element name and save pointer to character string */ phase_ptr = NULL; - ptr = line; - copy_token(token, &ptr, &l); + cptr = line; + copy_token(token, &cptr, &l); /* * Get and parse equation */ @@ -3893,21 +3916,21 @@ read_pp_assemblage(void) int j; int return_value; int n_user, n_user_end; - char *ptr; + const char* cptr; char *description; std::string token; int opt, opt_save; - char *next_char; + const char* next_char; const char *opt_list[] = { "force_equality" /* 0 */ }; int count_opt_list = 1; - ptr = line; + cptr = line; /* * Read pp_assemblage number */ - read_number_description(ptr, &n_user, &n_user_end, &description); + read_number_description(cptr, &n_user, &n_user_end, &description); /* * Find pp_assemblage or realloc space for pp_assemblage */ @@ -3980,11 +4003,11 @@ read_pp_assemblage(void) /* * Read name */ - ptr = line; - copy_token(token, &ptr); + cptr = line; + copy_token(token, &cptr); comp->Set_name(token.c_str()); - if ((j = copy_token(token, &ptr)) == EMPTY) + if ((j = copy_token(token, &cptr)) == EMPTY) continue; /* * Read saturation index @@ -4002,12 +4025,12 @@ read_pp_assemblage(void) /* * Adding a reaction to the phase boundary */ - if ((j = copy_token(token, &ptr)) == EMPTY) + if ((j = copy_token(token, &cptr)) == EMPTY) continue; if (j == UPPER || j == LOWER) { comp->Set_add_formula(token.c_str()); - j = copy_token(token, &ptr); + j = copy_token(token, &cptr); } /* * Read amount @@ -4029,7 +4052,7 @@ read_pp_assemblage(void) input_error++; continue; } - if ((j = copy_token(token, &ptr)) == EMPTY) + if ((j = copy_token(token, &cptr)) == EMPTY) continue; Utilities::str_tolower(token); if (strstr(token.c_str(), "d") == token.c_str()) @@ -4087,7 +4110,7 @@ read_reaction(void) * Read reaction */ int l; - char *ptr; + const char* cptr; char *description; char token[MAX_LENGTH]; int return_value; @@ -4096,8 +4119,8 @@ read_reaction(void) /* * Read reaction number */ - ptr = line; - read_number_description(ptr, &n_user, &n_user_end, &description); + cptr = line; + read_number_description(cptr, &n_user, &n_user_end, &description); /* * Set use data to first read @@ -4129,8 +4152,8 @@ read_reaction(void) { break; } - ptr = line; - copy_token(token, &ptr, &l); + cptr = line; + copy_token(token, &cptr, &l); if (isalpha((int) token[0]) || (token[0] == '(') || (token[0] == '[')) { /* @@ -4181,12 +4204,12 @@ read_reaction_reactants(cxxReaction *reaction_ptr) */ std::string token, last_token; LDBLE coef; - char *ptr; + const char* cptr; /* * Read one or more reactants */ - ptr = line; - while (copy_token(token, &ptr) != EMPTY) + cptr = line; + while (copy_token(token, &cptr) != EMPTY) { /* * Store reactant name, default coefficient @@ -4235,16 +4258,16 @@ read_reaction_steps(cxxReaction *reaction_ptr) * INCREMENTAL_REACTIONS */ - char *ptr; + const char* cptr; std::string token, token1; - ptr = line; + cptr = line; /* * Read one or more reaction increments */ for (;;) { - if (copy_token(token, &ptr) == EMPTY) + if (copy_token(token, &cptr) == EMPTY) { return (OK); } @@ -4306,7 +4329,7 @@ read_reaction_steps(cxxReaction *reaction_ptr) { reaction_ptr->Set_units(t1.c_str()); } - if (copy_token(token, &ptr) == EMPTY) + if (copy_token(token, &cptr) == EMPTY) { return (OK); } @@ -4337,7 +4360,7 @@ read_reaction_steps(cxxReaction *reaction_ptr) break; } } - while (copy_token(token, &ptr) != EMPTY); + while (copy_token(token, &cptr) != EMPTY); error_msg("Expecting positive number for number of equal " "increments to add.", CONTINUE); @@ -4355,24 +4378,24 @@ read_save(void) * in reaction calculation */ int i, l, n, n_user, n_user_end; - char *ptr; + const char* cptr; char token[MAX_LENGTH]; /* * Read "save" */ - ptr = line; - copy_token(token, &ptr, &l); + cptr = line; + copy_token(token, &cptr, &l); /* * Read keyword */ - copy_token(token, &ptr, &l); + copy_token(token, &cptr, &l); check_key(token); /* * Read number */ for (;;) { - i = copy_token(token, &ptr, &l); + i = copy_token(token, &cptr, &l); if (i == DIGIT) { replace("-", " ", token); @@ -4456,7 +4479,7 @@ read_selected_output(void) */ int value; int return_value, opt, opt_save; - char *next_char; + const char* next_char; const char *opt_list[] = { "file", /* 0 */ "totals", /* 1 */ @@ -4513,13 +4536,14 @@ read_selected_output(void) int count_opt_list = 51; int i, l; - char file_name[MAX_LENGTH], token[MAX_LENGTH]; + char token[MAX_LENGTH]; + std::string file_name; - char *ptr; - ptr = line; + const char* cptr; + cptr = line; int n_user, n_user_end; char *description; - read_number_description(ptr, &n_user, &n_user_end, &description); + read_number_description(cptr, &n_user, &n_user_end, &description); SelectedOutput temp_selected_output; temp_selected_output.Set_new_def(false); @@ -4606,15 +4630,19 @@ read_selected_output(void) error_msg(line_save, CONTINUE); break; case 0: /* file name */ + { temp_selected_output.Set_new_def(true); - if (string_trim(next_char) != EMPTY) + std::string temp_name(next_char); + string_trim(temp_name); + if (temp_name.size() > 0) { - strcpy(file_name, next_char); + file_name = temp_name; temp_selected_output.Set_file_name(file_name); temp_selected_output.Set_have_punch_name(true); } opt_save = OPTION_ERROR; - break; + } + break; case 1: /* totals */ temp_selected_output.Set_new_def(true); while ((i = copy_token(token, &next_char, &l)) != EMPTY) @@ -4962,7 +4990,7 @@ read_solution(void) char *description; int return_value, opt; - char *next_char; + const char* next_char; const char *opt_list[] = { "temp", /* 0 */ "temperature", /* 1 */ @@ -4983,9 +5011,9 @@ read_solution(void) /* * Read solution number and description */ - char *ptr; - ptr = line; - read_number_description(ptr, &n_user, &n_user_end, &description); + const char* cptr; + cptr = line; + read_number_description(cptr, &n_user, &n_user_end, &description); cxxSolution temp_solution; temp_solution.Set_new_def(true); @@ -5014,8 +5042,8 @@ read_solution(void) opt = get_option(opt_list, count_opt_list, &next_char); if (opt == OPTION_DEFAULT) { - ptr = next_char; - if (copy_token(token, &ptr) == CParser::TT_DIGIT) + cptr = next_char; + if (copy_token(token, &cptr) == CParser::TT_DIGIT) { opt = 9; } @@ -5161,10 +5189,10 @@ read_solution(void) /* read and save element name */ { char *temp_iso_name = string_duplicate(token.c_str()); - char *ptr1 = temp_iso_name; - get_num(&ptr1, &dummy); + const char* cptr1 = temp_iso_name; + get_num(&cptr1, &dummy); temp_isotope.Set_isotope_number(dummy); - if (ptr1[0] == '\0' || isupper((int) ptr1[0]) == FALSE) + if (cptr1[0] == '\0' || isupper((int) cptr1[0]) == FALSE) { error_msg("Expecting element name.", PHRQ_io::OT_CONTINUE); error_msg(line_save, PHRQ_io::OT_CONTINUE); @@ -5172,7 +5200,7 @@ read_solution(void) temp_iso_name = (char*)free_check_null(temp_iso_name); return (CParser::PARSER_ERROR); } - temp_isotope.Set_elt_name(ptr1); + temp_isotope.Set_elt_name(cptr1); temp_iso_name = (char*)free_check_null(temp_iso_name); } /* read and store isotope ratio */ @@ -5324,10 +5352,11 @@ read_species(void) int association; struct species *s_ptr; struct elt_list *next_elt; - char *ptr, token[MAX_LENGTH]; + const char* cptr; + char token[MAX_LENGTH]; //bool vm_read = false; int return_value, opt, opt_save; - char *next_char; + const char* next_char; const char *opt_list[] = { "no_check", /* 0 */ "check", /* 1 */ @@ -5447,10 +5476,10 @@ read_species(void) paren_count = 0; copy_token(token, &next_char, &i); s_ptr->mole_balance = string_hsave(token); - ptr = token; + cptr = token; s_ptr->next_secondary = (struct elt_list *) free_check_null(s_ptr->next_secondary); - get_secondary_in_species(&ptr, 1.0); + get_secondary_in_species(&cptr, 1.0); s_ptr->next_secondary = elt_list_save(); /* debug for (i = 0; i < count_elts; i++) { @@ -5828,17 +5857,17 @@ read_use(void) * in reaction calculation */ int i, l, n_user, return_value; - char *ptr; + const char* cptr; char token[MAX_LENGTH], token1[MAX_LENGTH];; /* * Read "use" */ - ptr = line; - copy_token(token, &ptr, &l); + cptr = line; + copy_token(token, &cptr, &l); /* * Read keyword */ - copy_token(token, &ptr, &l); + copy_token(token, &cptr, &l); check_key(token); if (next_keyword != Keywords::KEY_SOLUTION && next_keyword != Keywords::KEY_MIX && @@ -5865,7 +5894,7 @@ read_use(void) strcpy(token1, token); for (;;) { - i = copy_token(token, &ptr, &l); + i = copy_token(token, &cptr, &l); if (i == DIGIT) { (void)sscanf(token, "%d", &n_user); @@ -6049,7 +6078,7 @@ read_surface_species(void) int i, j; int association; char token[MAX_LENGTH]; - char *ptr; + const char* cptr; LDBLE offset; struct species *s_ptr; @@ -6057,7 +6086,7 @@ read_surface_species(void) struct rxn_token *token_ptr; int return_value, opt, opt_save; - char *next_char; + const char* next_char; const char *opt_list[] = { "no_check", /* 0 */ "check", /* 1 */ @@ -6148,10 +6177,10 @@ read_surface_species(void) paren_count = 0; copy_token(token, &next_char, &i); s_ptr->mole_balance = string_hsave(token); - ptr = token; + cptr = token; s_ptr->next_secondary = (struct elt_list *) free_check_null(s_ptr->next_secondary); - get_secondary_in_species(&ptr, 1.0); + get_secondary_in_species(&cptr, 1.0); s_ptr->next_secondary = elt_list_save(); /* debug for (i = 0; i < count_elts; i++) { @@ -6419,12 +6448,12 @@ read_surface(void) */ int n_user, n_user_end; LDBLE conc; - char *ptr, *ptr1; + const char* cptr, *cptr1; char *description; std::string token, token1, name; int return_value, opt; - char *next_char; + const char* next_char; const char *opt_list[] = { "equilibrate", /* 0 */ "equil", /* 1 */ @@ -6454,8 +6483,8 @@ read_surface(void) /* * Read surface number and description */ - ptr = line; - read_number_description(ptr, &n_user, &n_user_end, &description); + cptr = line; + read_number_description(cptr, &n_user, &n_user_end, &description); cxxSurface temp_surface; cxxSurfaceComp *comp_ptr = NULL; cxxSurfaceCharge *charge_ptr = NULL; @@ -6717,8 +6746,8 @@ read_surface(void) * Read surface component */ { - ptr = line; - int i = copy_token(token, &ptr); + cptr = line; + int i = copy_token(token, &cptr); if (i != UPPER && token[0] != '[') { error_msg @@ -6735,7 +6764,7 @@ read_surface(void) comp_ptr = &(temp_surface.Get_surface_comps().back()); comp_ptr->Set_formula(token.c_str()); - i = copy_token(token1, &ptr); + i = copy_token(token1, &cptr); if (i == DIGIT) { /* @@ -6760,7 +6789,7 @@ read_surface(void) /* surface conc. is related to mineral or kinetics */ comp_ptr->Set_phase_name(token1.c_str()); - int j = copy_token(token1, &ptr); + int j = copy_token(token1, &cptr); /* read optional 'equilibrium_phases' or 'kinetics' */ if (j != DIGIT) @@ -6782,7 +6811,7 @@ read_surface(void) input_error++; break; } - j = copy_token(token1, &ptr); + j = copy_token(token1, &cptr); } /* read proportion */ @@ -6814,28 +6843,30 @@ read_surface(void) count_elts = 0; paren_count = 0; char * formula = string_duplicate(token.c_str()); - ptr1 = formula; - get_elts_in_species(&ptr1, conc); + cptr1 = formula; + get_elts_in_species(&cptr1, conc); /* * save formula for adjusting number of exchange sites */ - ptr1 = formula; + cptr1 = formula; int l; // name is work space char * name = string_duplicate(formula); name[0] = '\0'; - get_token(&ptr1, name, &dummy, &l); + get_token(&cptr1, name, &dummy, &l); comp_ptr->Set_formula_z(dummy); cxxNameDouble nd = elt_list_NameDouble(); comp_ptr->Set_totals(nd); /* * Search for charge structure */ - ptr1 = formula; - get_elt(&ptr1, name, &l); - ptr1 = strchr(name, '_'); - if (ptr1 != NULL) - ptr1[0] = '\0'; + cptr1 = formula; + get_elt(&cptr1, name, &l); + { + char* ptr = strchr(name, '_'); + if (ptr != NULL) + ptr[0] = '\0'; + } charge_ptr = temp_surface.Find_charge(name); formula = (char*)free_check_null(formula); if (charge_ptr == NULL) @@ -6861,7 +6892,7 @@ read_surface(void) /* * Read surface area (m2/g) */ - copy_token(token1, &ptr); + copy_token(token1, &cptr); if (sscanf(token1.c_str(), SCANFORMAT, &dummy) == 1) { charge_ptr->Set_specific_area(dummy); @@ -6873,17 +6904,17 @@ read_surface(void) /* * Read grams of solid (g) */ - copy_token(token1, &ptr); + copy_token(token1, &cptr); if (sscanf(token1.c_str(), SCANFORMAT, &dummy) == 1) { charge_ptr->Set_grams(dummy); } /* read Dw */ - copy_token(token1, &ptr); + copy_token(token1, &cptr); Utilities::str_tolower(token1); if (strcmp(token1.c_str(), "dw") == 0) { - int j = copy_token(token1, &ptr); + int j = copy_token(token1, &cptr); if (j != DIGIT) { error_msg @@ -7030,12 +7061,12 @@ read_surface_master_species(void) * Reads master species data from data file or input file */ int l, return_value; - char *ptr, *ptr1; + const char* cptr, *cptr1; LDBLE l_z; struct species *s_ptr; char token[MAX_LENGTH], token1[MAX_LENGTH]; int opt, opt_save; - char *next_char; + const char* next_char; const char *opt_list[] = { "capacitance", /* 0 */ "cd_music_capacitance" /* 1 */ @@ -7068,11 +7099,11 @@ read_surface_master_species(void) /* * Get "element" name with valence, allocate space, store */ - ptr = line; + cptr = line; /* * Get element name and save pointer to character string */ - if (copy_token(token, &ptr, &l) != UPPER && token[0] != '[') + if (copy_token(token, &cptr, &l) != UPPER && token[0] != '[') { parse_error++; error_msg("Reading element for master species.", CONTINUE); @@ -7092,7 +7123,7 @@ read_surface_master_species(void) master[count_master] = master_alloc(); master[count_master]->type = SURF; master[count_master]->elt = element_store(token); - if (copy_token(token, &ptr, &l) != UPPER && token[0] != '[') + if (copy_token(token, &cptr, &l) != UPPER && token[0] != '[') { parse_error++; error_msg("Reading surface master species name.", CONTINUE); @@ -7106,8 +7137,8 @@ read_surface_master_species(void) } else { - ptr1 = token; - get_token(&ptr1, token1, &l_z, &l); + cptr1 = token; + get_token(&cptr1, token1, &l_z, &l); master[count_master]->s = s_store(token1, l_z, FALSE); } master[count_master]->primary = TRUE; @@ -7118,8 +7149,8 @@ read_surface_master_species(void) */ strcpy(token1, token); replace("_", " ", token1); - ptr1 = token1; - copy_token(token, &ptr1, &l); + cptr1 = token1; + copy_token(token, &cptr1, &l); strcat(token, "_psi"); add_psi_master_species(token); opt_save = OPTION_DEFAULT; @@ -7138,7 +7169,7 @@ add_psi_master_species(char *token) { struct species *s_ptr; struct master *master_ptr; - char *ptr; + const char* cptr; char token1[MAX_LENGTH]; int i, n, plane; @@ -7176,8 +7207,8 @@ add_psi_master_species(char *token) } count_elts = 0; paren_count = 0; - ptr = token; - get_elts_in_species(&ptr, 1.0); + cptr = token; + get_elts_in_species(&cptr, 1.0); master[count_master]->s->next_elt = elt_list_save(); master[count_master]->s->type = plane; master[count_master]->primary = TRUE; @@ -7220,20 +7251,20 @@ read_title(void) * ERROR if error occurred reading data * */ - char *ptr, *ptr1; + const char* cptr, *cptr1; int l, title_x_length, line_length; int return_value; char token[MAX_LENGTH]; /* * Read anything after keyword */ - ptr = line; - copy_token(token, &ptr, &l); - ptr1 = ptr; + cptr = line; + copy_token(token, &cptr, &l); + cptr1 = cptr; title_x = (char *) free_check_null(title_x); - if (copy_token(token, &ptr, &l) != EMPTY) + if (copy_token(token, &cptr, &l) != EMPTY) { - title_x = string_duplicate(ptr1); + title_x = string_duplicate(cptr1); } else { @@ -7295,13 +7326,13 @@ read_advection(void) * number of cells; * number of shifts; */ - char *ptr; + const char* cptr; char *description; int n_user, n_user_end, i; std::vector punch_temp, print_temp; int return_value, opt, opt_save; - char *next_char; + const char* next_char; const char *opt_list[] = { "cells", /* 0 */ "shifts", /* 1 */ @@ -7326,8 +7357,8 @@ read_advection(void) /* * Read advection number (not currently used) */ - ptr = line; - read_number_description(ptr, &n_user, &n_user_end, &description); + cptr = line; + read_number_description(cptr, &n_user, &n_user_end, &description); description = (char *) free_check_null(description); /* * Set use data @@ -7541,7 +7572,7 @@ read_debug(void) * */ int return_value, opt; - char *next_char; + const char* next_char; const char *opt_list[] = { "iterations", /* 0 */ "tolerance", /* 1 */ @@ -7703,7 +7734,7 @@ read_print(void) * */ int return_value, opt, l; - char *next_char; + const char* next_char; char token[MAX_LENGTH]; LDBLE num; const char *opt_list[] = { @@ -7852,7 +7883,7 @@ read_print(void) } if (j == DIGIT) { - char * tptr = token; + const char * tptr = token; get_num(&tptr, &num); num = floor(num); if (num < 0.0) num = 0.0; @@ -7953,13 +7984,13 @@ check_key(const char *str) * TRUE, * FALSE. */ - char *ptr; + const char* cptr; std::string stdtoken; char * token1; token1 = string_duplicate(str); - ptr = token1; - int j = copy_token(stdtoken, &ptr); + cptr = token1; + int j = copy_token(stdtoken, &cptr); Utilities::str_tolower(stdtoken); std::string key(stdtoken); @@ -8185,7 +8216,7 @@ find_option(const char *item, int *n, const char **list, int count_list, int exa /* ---------------------------------------------------------------------- */ int Phreeqc:: -get_true_false(char *string, int default_value) +get_true_false(const char *string, int default_value) /* ---------------------------------------------------------------------- */ { /* @@ -8193,11 +8224,11 @@ get_true_false(char *string, int default_value) */ int l; char token[MAX_LENGTH]; - char *ptr; + const char* cptr; - ptr = string; + cptr = string; - if (copy_token(token, &ptr, &l) == EMPTY) + if (copy_token(token, &cptr, &l) == EMPTY) { return (default_value); } @@ -8213,7 +8244,7 @@ get_true_false(char *string, int default_value) /* ---------------------------------------------------------------------- */ int Phreeqc:: -get_option(const char **opt_list, int count_opt_list, char **next_char) +get_option(const char **opt_list, int count_opt_list, const char **next_char) /* ---------------------------------------------------------------------- */ { /* @@ -8221,7 +8252,7 @@ get_option(const char **opt_list, int count_opt_list, char **next_char) */ int j; int opt; - char *opt_ptr; + const char *opt_ptr; std::string stdoption; /* * Read line @@ -8305,14 +8336,14 @@ read_rates(void) * ERROR if error occurred reading data * */ - char *ptr; + const char* cptr; int l, n; int return_value, opt, opt_save; char token[MAX_LENGTH]; struct rate *rate_ptr; char *description; int n_user, n_user_end; - char *next_char; + const char* next_char; const char *opt_list[] = { "start", /* 0 */ "end" /* 1 */ @@ -8322,8 +8353,8 @@ read_rates(void) * Read advection number (not currently used) */ n = -1; - ptr = line; - read_number_description(ptr, &n_user, &n_user_end, &description); + cptr = line; + read_number_description(cptr, &n_user, &n_user_end, &description); description = (char *) free_check_null(description); opt_save = OPTION_DEFAULT; /* @@ -8359,8 +8390,8 @@ read_rates(void) opt_save = OPTION_DEFAULT; break; case OPTION_DEFAULT: /* read rate name */ - ptr = line; - copy_token(token, &ptr, &l); + cptr = line; + copy_token(token, &cptr, &l); { const char *name = string_hsave(token); rate_ptr = rate_search(name, &n); @@ -8425,7 +8456,7 @@ read_user_print(void) * */ int return_value, opt, opt_save; - char *next_char; + const char* next_char; const char *opt_list[] = { "start", /* 0 */ "end" /* 1 */ @@ -8504,7 +8535,7 @@ read_user_punch(void) */ int return_value, opt, opt_save; std::string stdtoken; - char *next_char; + const char* next_char; const char *opt_list[] = { "start", /* 0 */ "end", /* 1 */ @@ -8519,9 +8550,9 @@ read_user_punch(void) int n_user, n_user_end; char *description; - char *ptr; - ptr = line; - read_number_description(ptr, &n_user, &n_user_end, &description); + const char* cptr; + cptr = line; + read_number_description(cptr, &n_user, &n_user_end, &description); UserPunch temp_user_punch; temp_user_punch.Set_PhreeqcPtr(this); @@ -8618,12 +8649,12 @@ read_solid_solutions(void) * */ int n_user, n_user_end; - char *ptr; + const char* cptr; char *description; std::string token; int return_value, opt; - char *next_char; + const char* next_char; const char *opt_list[] = { "component", /* 0 */ "comp", /* 1 */ @@ -8648,8 +8679,8 @@ read_solid_solutions(void) /* * Read ss_assemblage number */ - ptr = line; - read_number_description(ptr, &n_user, &n_user_end, &description); + cptr = line; + read_number_description(cptr, &n_user, &n_user_end, &description); cxxSSassemblage temp_ss_assemblage; temp_ss_assemblage.Set_n_user(n_user); temp_ss_assemblage.Set_n_user_end(n_user_end); @@ -8699,14 +8730,14 @@ read_solid_solutions(void) /* * Read phase name of component */ - ptr = next_char; - copy_token(token, &ptr); + cptr = next_char; + copy_token(token, &cptr); comp.Set_name(token); /* * Read moles of component */ - if (copy_token(token, &ptr) == EMPTY) + if (copy_token(token, &cptr) == EMPTY) { comp.Set_moles(NAN); } @@ -8734,13 +8765,13 @@ read_solid_solutions(void) error_msg("Solid solution name has not been defined", CONTINUE); break; } - ptr = next_char; - if (copy_token(token, &ptr) != EMPTY) + cptr = next_char; + if (copy_token(token, &cptr) != EMPTY) { (void)sscanf(token.c_str(), SCANFORMAT, &dummy); ss_ptr->Get_p()[0] = dummy; } - if (copy_token(token, &ptr) != EMPTY) + if (copy_token(token, &cptr) != EMPTY) { (void)sscanf(token.c_str(), SCANFORMAT, &dummy); ss_ptr->Get_p()[1] = dummy; @@ -8753,13 +8784,13 @@ read_solid_solutions(void) error_msg("Solid solution name has not been defined", CONTINUE); break; } - ptr = next_char; - if (copy_token(token, &ptr) != EMPTY) + cptr = next_char; + if (copy_token(token, &cptr) != EMPTY) { (void)sscanf(token.c_str(), SCANFORMAT, &dummy); ss_ptr->Get_p()[0] = dummy; } - if (copy_token(token, &ptr) != EMPTY) + if (copy_token(token, &cptr) != EMPTY) { (void)sscanf(token.c_str(), SCANFORMAT, &dummy); ss_ptr->Get_p()[1] = dummy; @@ -8772,11 +8803,11 @@ read_solid_solutions(void) error_msg("Solid solution name has not been defined", CONTINUE); break; } - ptr = next_char; + cptr = next_char; ss_ptr->Get_p().clear(); for (int i = 0; i < 4; i++) { - if (copy_token(token, &ptr) != EMPTY) + if (copy_token(token, &cptr) != EMPTY) { (void)sscanf(token.c_str(), SCANFORMAT, &dummy); ss_ptr->Get_p().push_back(dummy); @@ -8799,11 +8830,11 @@ read_solid_solutions(void) error_msg("Solid solution name has not been defined", CONTINUE); break; } - ptr = next_char; + cptr = next_char; ss_ptr->Get_p().clear(); for (int i = 0; i < 4; i++) { - if (copy_token(token, &ptr) != EMPTY) + if (copy_token(token, &cptr) != EMPTY) { (void)sscanf(token.c_str(), SCANFORMAT, &dummy); ss_ptr->Get_p().push_back(dummy); @@ -8826,11 +8857,11 @@ read_solid_solutions(void) error_msg("Solid solution name has not been defined", CONTINUE); break; } - ptr = next_char; + cptr = next_char; ss_ptr->Get_p().clear(); for (int i = 0; i < 2; i++) { - if (copy_token(token, &ptr) != EMPTY) + if (copy_token(token, &cptr) != EMPTY) { (void)sscanf(token.c_str(), SCANFORMAT, &dummy); ss_ptr->Get_p().push_back(dummy); @@ -8853,11 +8884,11 @@ read_solid_solutions(void) error_msg("Solid solution name has not been defined", CONTINUE); break; } - ptr = next_char; + cptr = next_char; ss_ptr->Get_p().clear(); for (int i = 0; i < 2; i++) { - if (copy_token(token, &ptr) != EMPTY) + if (copy_token(token, &cptr) != EMPTY) { (void)sscanf(token.c_str(), SCANFORMAT, &dummy); ss_ptr->Get_p().push_back(dummy); @@ -8880,11 +8911,11 @@ read_solid_solutions(void) error_msg("Solid solution name has not been defined", CONTINUE); break; } - ptr = next_char; + cptr = next_char; ss_ptr->Get_p().clear(); for (int i = 0; i < 2; i++) { - if (copy_token(token, &ptr) != EMPTY) + if (copy_token(token, &cptr) != EMPTY) { (void)sscanf(token.c_str(), SCANFORMAT, &dummy); ss_ptr->Get_p().push_back(dummy); @@ -8907,11 +8938,11 @@ read_solid_solutions(void) error_msg("Solid solution name has not been defined", CONTINUE); break; } - ptr = next_char; + cptr = next_char; ss_ptr->Get_p().clear(); for (int i = 0; i < 2; i++) { - if (copy_token(token, &ptr) != EMPTY) + if (copy_token(token, &cptr) != EMPTY) { (void)sscanf(token.c_str(), SCANFORMAT, &dummy); ss_ptr->Get_p().push_back(dummy); @@ -8935,9 +8966,9 @@ read_solid_solutions(void) break; } { - ptr = next_char; + cptr = next_char; int j = 0; - if (copy_token(token, &ptr) != EMPTY) + if (copy_token(token, &cptr) != EMPTY) { j = sscanf(token.c_str(), SCANFORMAT, &dummy); ss_ptr->Set_tk(dummy); @@ -8961,9 +8992,9 @@ read_solid_solutions(void) break; } { - ptr = next_char; + cptr = next_char; int j = 0; - if (copy_token(token, &ptr) != EMPTY) + if (copy_token(token, &cptr) != EMPTY) { j = sscanf(token.c_str(), SCANFORMAT, &dummy); ss_ptr->Set_tk(dummy + 298.15); @@ -8985,11 +9016,11 @@ read_solid_solutions(void) error_msg("Solid solution name has not been defined", CONTINUE); break; } - ptr = next_char; + cptr = next_char; ss_ptr->Get_p().clear(); for (int i = 0; i < 2; i++) { - if (copy_token(token, &ptr) != EMPTY) + if (copy_token(token, &cptr) != EMPTY) { (void)sscanf(token.c_str(), SCANFORMAT, &dummy); ss_ptr->Get_p().push_back(dummy); @@ -9012,11 +9043,11 @@ read_solid_solutions(void) error_msg("Solid solution name has not been defined", CONTINUE); break; } - ptr = next_char; + cptr = next_char; ss_ptr->Get_p().clear(); for (int i = 0; i < 2; i++) { - if (copy_token(token, &ptr) != EMPTY) + if (copy_token(token, &cptr) != EMPTY) { (void)sscanf(token.c_str(), SCANFORMAT, &dummy); ss_ptr->Get_p().push_back(dummy); @@ -9040,13 +9071,13 @@ read_solid_solutions(void) */ delete comp0_ptr; comp0_ptr = new cxxSScomp; - ptr = next_char; - copy_token(token, &ptr); + cptr = next_char; + copy_token(token, &cptr); comp0_ptr->Set_name(token); /* * Read moles of component */ - if (copy_token(token, &ptr) == EMPTY) + if (copy_token(token, &cptr) == EMPTY) { comp0_ptr->Set_moles(NAN); } @@ -9068,13 +9099,13 @@ read_solid_solutions(void) /* * Read phase name of component */ - ptr = next_char; - copy_token(token, &ptr); + cptr = next_char; + copy_token(token, &cptr); comp1_ptr->Set_name(token); /* * Read moles of component */ - if (copy_token(token, &ptr) == EMPTY) + if (copy_token(token, &cptr) == EMPTY) { comp1_ptr->Set_moles(NAN); } @@ -9117,8 +9148,8 @@ read_solid_solutions(void) /* * Read solid solution name */ - ptr = line; - copy_token(token, &ptr); + cptr = line; + copy_token(token, &cptr); ss_ptr->Set_name(token); ss_ptr->Set_total_moles(0.0); break; @@ -9189,7 +9220,7 @@ read_llnl_aqueous_model_parameters(void) * */ int return_value, opt; - char* next_char; + const char* next_char; const char* opt_list[] = { "temperatures", /* 0 */ "temperature", /* 1 */ @@ -9348,7 +9379,7 @@ next_keyword_or_option(const char **opt_list, int count_opt_list) * EOF */ int opt; - char *next_char; + const char* next_char; for (;;) { @@ -9400,7 +9431,7 @@ read_named_logk(void) char token[MAX_LENGTH]; int return_value, opt, opt_save; - char *next_char; + const char* next_char; const char *opt_list[] = { "log_k", /* 0 */ "logk", /* 1 */ @@ -9612,17 +9643,17 @@ read_copy(void) * */ int i, l, n, n_user, n_user_start, n_user_end, return_value; - char *ptr; + const char* cptr; char token[MAX_LENGTH], token1[MAX_LENGTH], nonkeyword[MAX_LENGTH]; /* * Read "copy" */ - ptr = line; - copy_token(token, &ptr, &l); + cptr = line; + copy_token(token, &cptr, &l); /* * Read keyword */ - copy_token(token, &ptr, &l); + copy_token(token, &cptr, &l); check_key(token); switch (next_keyword) @@ -9656,7 +9687,7 @@ read_copy(void) * Read source index */ strcpy(token1, token); - i = copy_token(token, &ptr, &l); + i = copy_token(token, &cptr, &l); if (i == DIGIT) { (void)sscanf(token, "%d", &n_user); @@ -9689,7 +9720,7 @@ read_copy(void) /* * Read target index or range of indices */ - i = copy_token(token, &ptr, &l); + i = copy_token(token, &cptr, &l); if (i == DIGIT) { replace("-", " ", &token[1]); @@ -9795,10 +9826,10 @@ read_reaction_pressure(void) // Make instance, set n_user, n_user_end, description cxxPressure atm(this->phrq_io); - char *ptr = line; + const char* cptr = line; char *description; int n_user, n_user_end; - read_number_description(ptr, &n_user, &n_user_end, &description); + read_number_description(cptr, &n_user, &n_user_end, &description); atm.Set_n_user(n_user); atm.Set_n_user_end(n_user); atm.Set_description(description); @@ -9929,10 +9960,10 @@ read_temperature(void) // Make instance, set n_user, n_user_end, description cxxTemperature t_react(this->phrq_io); - char *ptr = line; + const char* cptr = line; char *description; int n_user, n_user_end; - read_number_description(ptr, &n_user, &n_user_end, &description); + read_number_description(cptr, &n_user, &n_user_end, &description); t_react.Set_n_user(n_user); t_react.Set_n_user_end(n_user); t_react.Set_description(description); diff --git a/readtr.cpp b/readtr.cpp index 4bfada27..de932f5f 100644 --- a/readtr.cpp +++ b/readtr.cpp @@ -35,7 +35,7 @@ read_transport(void) * ERROR if error occurred reading data * */ - char *ptr; + const char* cptr; int i, j, l; int count_length, count_disp, count_punch, count_print, count_por, count_same_model; int count_length_alloc, count_disp_alloc, count_por_alloc; @@ -45,8 +45,9 @@ read_transport(void) LDBLE *length, *disp, *pors; int *punch_temp, *print_temp, *same_model_temp; int return_value, opt, opt_save; - char *next_char, *next_char_save; - char file_name[MAX_LENGTH]; + const char* next_char; + //char file_name[MAX_LENGTH]; + std::string file_name("phreeqc.dmp"); const char *opt_list[] = { "cells", /* 0 */ @@ -100,7 +101,6 @@ read_transport(void) }; int count_opt_list = 48; - strcpy(file_name, "phreeqc.dmp"); /* * Initialize */ @@ -145,8 +145,8 @@ read_transport(void) /* * Read transport number (not currently used) */ - ptr = line; - read_number_description(ptr, &n_user, &n_user_end, &description); + cptr = line; + read_number_description(cptr, &n_user, &n_user_end, &description); description = (char *)free_check_null(description); /* * Set use data to last read @@ -442,17 +442,17 @@ read_transport(void) opt_save = OPTION_DEFAULT; break; case 26: /* dump */ + { dump_in = TRUE; - next_char_save = next_char; - if (copy_token(file_name, &next_char, &l) == EMPTY) - strcpy(file_name, "phreeqc.dmp"); - else + std::string temp_name(next_char); + string_trim(temp_name); + if (temp_name.size() > 0) { - string_trim(next_char_save); - strcpy(file_name, next_char_save); + file_name = temp_name; } opt_save = OPTION_DEFAULT; break; + } case 27: /* output */ case 28: /* output_frequency */ case 34: /* print_frequency */ @@ -1150,7 +1150,7 @@ read_transport(void) /* ---------------------------------------------------------------------- */ int Phreeqc:: -read_line_LDBLEs(char *next_char, LDBLE ** d, int *count_d, int *count_alloc) +read_line_LDBLEs(const char* next_char, LDBLE ** d, int *count_d, int *count_alloc) /* ---------------------------------------------------------------------- */ { int i, j, l, n; diff --git a/sit.cpp b/sit.cpp index 74455f0e..2e2f3745 100644 --- a/sit.cpp +++ b/sit.cpp @@ -177,7 +177,7 @@ read_sit(void) pitz_param_type pzp_type; int return_value, opt, opt_save; - char *next_char; + const char* next_char; const char *opt_list[] = { "epsilon", /* 0 */ "epsilon1" /* 1 */ diff --git a/spread.cpp b/spread.cpp index 3b3c7372..eb87f402 100644 --- a/spread.cpp +++ b/spread.cpp @@ -37,10 +37,10 @@ read_solution_spread(void) struct spread_row *heading, *row_ptr, *units; int count, strings, numbers; int spread_lines; - char *ptr; + const char* cptr; struct defaults soln_defaults; int return_value, opt; - char *next_char; + const char* next_char; const char *opt_list[] = { "temp", /* 0 */ "temperature", /* 1 */ @@ -103,10 +103,10 @@ read_solution_spread(void) if (spread_lines == 0 && opt != OPTION_DEFAULT) { row_ptr = string_to_spread_row(line); - ptr = line; + cptr = line; count = numbers = strings = 0; int j; - while (((j = copy_token(token, &ptr)) != EMPTY)) + while (((j = copy_token(token, &cptr)) != EMPTY)) { count++; if (j == UPPER || j == LOWER) @@ -117,14 +117,16 @@ read_solution_spread(void) /* * Is 2nd token all number */ - ptr = line; - copy_token(token, &ptr); - j = copy_token(token, &ptr); + cptr = line; + copy_token(token, &cptr); + j = copy_token(token, &cptr); bool num = false; if (j == DIGIT) { + char* ptr; strtod(token.c_str(), &ptr); - int j1 = copy_token(token1, &ptr); + cptr = ptr; + int j1 = copy_token(token1, &cptr); if (j1 != EMPTY) { num = FALSE; @@ -134,12 +136,11 @@ read_solution_spread(void) num = TRUE; } } - /* * Starts with hyphen */ - ptr = line; - copy_token(token, &ptr); + cptr = line; + copy_token(token, &cptr); if (token[0] == '-') { /* opt = opt; */ @@ -554,7 +555,7 @@ spread_row_to_solution(struct spread_row *heading, struct spread_row *units, CParser parser(this->phrq_io); int return_value, opt; - char *next_char; + const char* next_char; const char *opt_list[] = { "temp", /* 0 */ "temperature", /* 1 */ @@ -884,10 +885,10 @@ spread_row_to_solution(struct spread_row *heading, struct spread_row *units, /* read and save element name */ { char *temp_iso_name = string_duplicate(token.c_str()); - char *ptr1 = temp_iso_name; - get_num(&ptr1, &dummy); + const char* cptr1 = temp_iso_name; + get_num(&cptr1, &dummy); temp_isotope.Set_isotope_number(dummy); - if (ptr1[0] == '\0' || isupper((int) ptr1[0]) == FALSE) + if (cptr1[0] == '\0' || isupper((int)cptr1[0]) == FALSE) { error_msg("Expecting element name.", PHRQ_io::OT_CONTINUE); error_msg(line_save, PHRQ_io::OT_CONTINUE); @@ -896,7 +897,7 @@ spread_row_to_solution(struct spread_row *heading, struct spread_row *units, char_string = (char*)free_check_null(char_string); return (CParser::PARSER_ERROR); } - temp_isotope.Set_elt_name(ptr1); + temp_isotope.Set_elt_name(cptr1); temp_iso_name = (char*)free_check_null(temp_iso_name); } /* read and store isotope ratio */ @@ -934,8 +935,6 @@ spread_row_to_solution(struct spread_row *heading, struct spread_row *units, break; case 10: /* water */ { - //next_char = char_string; - //int j = copy_token(token, &next_char); // read identifier "water" int j = copy_token(token, &next_char); if (j == EMPTY) { @@ -1061,7 +1060,7 @@ string_to_spread_row(char *string) int j, l; /* possible memory error if length of line is smaller than previous line */ char *token; - char *ptr; + const char* cptr; struct spread_row *spread_row_ptr = NULL; /* * Allocate space @@ -1104,7 +1103,7 @@ string_to_spread_row(char *string) spread_row_ptr->empty = 0; spread_row_ptr->string = 0; spread_row_ptr->number = 0; - ptr = string; + cptr = string; /* * Split by tabs, reallocate space */ @@ -1141,7 +1140,7 @@ string_to_spread_row(char *string) return spread_row_ptr; } } - j = copy_token_tab(token, &ptr, &l); + j = copy_token_tab(token, &cptr, &l); if (j == EOL) break; spread_row_ptr->char_vector[spread_row_ptr->count] = @@ -1240,16 +1239,16 @@ spread_row_free(struct spread_row *spread_row_ptr) /* ---------------------------------------------------------------------- */ int Phreeqc:: -copy_token_tab(char *token_ptr, char **ptr, int *length) +copy_token_tab(char *token_ptr, const char **cptr, int *length) /* ---------------------------------------------------------------------- */ { /* - * Copies from **ptr to *token_ptr until first tab is encountered. + * Copies from **cptr to *token_ptr until first tab is encountered. * * Arguments: * *token_ptr output, place to store token * - * **ptr input, character string to read token from + * **cptr input, character string to read token from * output, next position after token * * length output, length of token @@ -1267,8 +1266,8 @@ copy_token_tab(char *token_ptr, char **ptr, int *length) /* * Strip leading spaces */ - while ((c = **ptr) == ' ') - (*ptr)++; + while ((c = **cptr) == ' ') + (*cptr)++; /* * Check what we have */ @@ -1303,10 +1302,10 @@ copy_token_tab(char *token_ptr, char **ptr, int *length) i = 0; for (;;) { - c = **ptr; + c = **cptr; if (c == '\t') { - (*ptr)++; + (*cptr)++; break; } else if (c == '\0') @@ -1316,7 +1315,7 @@ copy_token_tab(char *token_ptr, char **ptr, int *length) else { token_ptr[i] = c; - (*ptr)++; + (*cptr)++; i++; } } @@ -1340,7 +1339,7 @@ copy_token_tab(char *token_ptr, char **ptr, int *length) /* ---------------------------------------------------------------------- */ int Phreeqc:: -get_option_string(const char **opt_list, int count_opt_list, char **next_char) +get_option_string(const char **opt_list, int count_opt_list, const char **next_char) /* ---------------------------------------------------------------------- */ { /* @@ -1348,7 +1347,7 @@ get_option_string(const char **opt_list, int count_opt_list, char **next_char) */ int j; int opt_l, opt; - char *opt_ptr; + const char *opt_ptr; char option[MAX_LENGTH]; opt_ptr = *next_char; diff --git a/step.cpp b/step.cpp index 032468f9..f78be7d2 100644 --- a/step.cpp +++ b/step.cpp @@ -663,7 +663,7 @@ add_pp_assemblage(cxxPPassemblage *pp_assemblage_ptr) int i; LDBLE amount_to_add, total; char token[MAX_LENGTH]; - char *ptr; + const char* cptr; struct master *master_ptr; if (check_pp_assemblage(pp_assemblage_ptr) == OK) @@ -692,8 +692,8 @@ add_pp_assemblage(cxxPPassemblage *pp_assemblage_ptr) if (comp_ptr->Get_add_formula().size() > 0) { strcpy(token, comp_ptr->Get_add_formula().c_str()); - ptr = &(token[0]); - get_elts_in_species(&ptr, 1.0); + cptr = &(token[0]); + get_elts_in_species(&cptr, 1.0); } else { @@ -938,7 +938,7 @@ reaction_calc(cxxReaction *reaction_ptr) */ int return_value; LDBLE coef; - char *ptr; + const char* cptr; struct phase *phase_ptr; /* * Go through list and generate list of elements and @@ -965,8 +965,8 @@ reaction_calc(cxxReaction *reaction_ptr) else { char * token = string_duplicate(it->first.c_str()); - ptr = token; - get_elts_in_species(&ptr, coef); + cptr = token; + get_elts_in_species(&cptr, coef); free_check_null(token); } } @@ -1064,7 +1064,7 @@ add_ss_assemblage(cxxSSassemblage *ss_assemblage_ptr) int i, j, k; LDBLE amount_to_add, total; struct master *master_ptr; - char *ptr; + const char* cptr; if (ss_assemblage_ptr == NULL) return (OK); @@ -1090,9 +1090,9 @@ add_ss_assemblage(cxxSSassemblage *ss_assemblage_ptr) if (comp_ptr->Get_moles() > 0.0) { char * token = string_duplicate(phase_ptr->formula); - ptr = &(token[0]); + cptr = &(token[0]); count_elts = 0; // appt - get_elts_in_species(&ptr, 1.0); + get_elts_in_species(&cptr, 1.0); free_check_null(token); for (k = 0; k < count_elts; k++) { @@ -1270,7 +1270,7 @@ pp_assemblage_check(cxxPPassemblage *pp_assemblage_ptr) * Check for missing elements */ std::string token; - char *ptr; + const char* cptr; struct master *master_ptr; if (check_pp_assemblage(pp_assemblage_ptr) == OK) @@ -1293,8 +1293,8 @@ pp_assemblage_check(cxxPPassemblage *pp_assemblage_ptr) if (comp_ptr->Get_add_formula().size() > 0) { token = comp_ptr->Get_add_formula(); - ptr = &(token[0]); - get_elts_in_species(&ptr, 1.0); + cptr = &(token[0]); + get_elts_in_species(&cptr, 1.0); } else { diff --git a/structures.cpp b/structures.cpp index ee58ad20..501be22c 100644 --- a/structures.cpp +++ b/structures.cpp @@ -834,7 +834,7 @@ master_alloc(void) /* ---------------------------------------------------------------------- */ int Phreeqc:: -master_delete(char *ptr) +master_delete(const char* cptr) /* ---------------------------------------------------------------------- */ { /* @@ -849,7 +849,7 @@ master_delete(char *ptr) */ int n; - if (master_search(ptr, &n) == NULL) + if (master_search(cptr, &n) == NULL) return (FALSE); master_free(master[n]); master.erase(master.begin() + n); @@ -875,29 +875,29 @@ master_free(struct master *master_ptr) /* ---------------------------------------------------------------------- */ struct master * Phreeqc:: -master_bsearch(const char *ptr) +master_bsearch(const char* cptr) /* ---------------------------------------------------------------------- */ { /* * Uses binary search. Assumes master is in sort order. - * Find master species for string (*ptr) containing name of element or valence state. + * Find master species for string (*cptr) containing name of element or valence state. * - * Input: ptr pointer to string containing element name + * Input: cptr pointer to string containing element name * - * Return: pointer to master structure containing name ptr or NULL. + * Return: pointer to master structure containing name cptr or NULL. */ void *void_ptr; if (master.size() == 0) { return (NULL); } - void_ptr = bsearch((const char *) ptr, + void_ptr = bsearch((const char *) cptr, (char *) &master[0], master.size(), sizeof(struct master *), master_compare_string); if (void_ptr == NULL) { - char * dup = string_duplicate(ptr); + char * dup = string_duplicate(cptr); replace("(+","(", dup); void_ptr = bsearch((const char *) dup, (char*)&master[0], @@ -941,23 +941,23 @@ master_compare(const void *ptr1, const void *ptr2) /* ---------------------------------------------------------------------- */ struct master * Phreeqc:: -master_bsearch_primary(const char *ptr) +master_bsearch_primary(const char* cptr) /* ---------------------------------------------------------------------- */ { /* - * Find primary master species for first element in the string, ptr. + * Find primary master species for first element in the string, cptr. * Uses binary search. Assumes master is in sort order. */ int l; - char *ptr1; + const char* cptr1; char elt[MAX_LENGTH]; struct master *master_ptr_primary; /* * Find element name */ - char * temp_name = string_duplicate(ptr); - ptr1 = temp_name; - get_elt(&ptr1, elt, &l); + char * temp_name = string_duplicate(cptr); + cptr1 = temp_name; + get_elt(&cptr1, elt, &l); free_check_null(temp_name); /* * Search master species list @@ -967,14 +967,14 @@ master_bsearch_primary(const char *ptr) { input_error++; error_string = sformatf( - "Could not find primary master species for %s.", ptr); + "Could not find primary master species for %s.", cptr); error_msg(error_string, CONTINUE); } return (master_ptr_primary); } /* ---------------------------------------------------------------------- */ struct master * Phreeqc:: -master_bsearch_secondary(char *ptr) +master_bsearch_secondary(const char* cptr) /* ---------------------------------------------------------------------- */ { /* @@ -982,15 +982,15 @@ master_bsearch_secondary(char *ptr) * i.e. S(6) for S. */ int l; - char *ptr1; + const char* cptr1; char elt[MAX_LENGTH]; struct master *master_ptr_primary, *master_ptr=NULL, *master_ptr_secondary=NULL; int j; /* * Find element name */ - ptr1 = ptr; - get_elt(&ptr1, elt, &l); + cptr1 = cptr; + get_elt(&cptr1, elt, &l); /* * Search master species list */ @@ -999,7 +999,7 @@ master_bsearch_secondary(char *ptr) { input_error++; error_string = sformatf( - "Could not find primary master species for %s.", ptr); + "Could not find primary master species for %s.", cptr); error_msg(error_string, CONTINUE); } /* @@ -1035,7 +1035,7 @@ master_bsearch_secondary(char *ptr) { input_error++; error_string = sformatf( - "Could not find secondary master species for %s.", ptr); + "Could not find secondary master species for %s.", cptr); error_msg(error_string, STOP); } @@ -1044,11 +1044,11 @@ master_bsearch_secondary(char *ptr) } /* ---------------------------------------------------------------------- */ struct master * Phreeqc:: -master_search(char *ptr, int *n) +master_search(const char* cptr, int *n) /* ---------------------------------------------------------------------- */ { /* - * Linear search of master to find master species in string, ptr. + * Linear search of master to find master species in string, cptr. * Returns pointer if found. n contains position in array master. * Returns NULL if not found. */ @@ -1060,7 +1060,7 @@ master_search(char *ptr, int *n) *n = -999; for (i = 0; i < (int)master.size(); i++) { - if (strcmp(ptr, master[i]->elt->name) == 0) + if (strcmp(cptr, master[i]->elt->name) == 0) { *n = i; master_ptr = master[i]; @@ -1163,11 +1163,11 @@ phase_free(struct phase *phase_ptr) /* ---------------------------------------------------------------------- */ struct phase * Phreeqc:: -phase_bsearch(const char *ptr, int *j, int print) +phase_bsearch(const char* cptr, int *j, int print) /* ---------------------------------------------------------------------- */ { /* Binary search the structure array "phases" for a name that is equal to - * ptr. Assumes array phases is in sort order. + * cptr. Assumes array phases is in sort order. * * Arguments: * name input, a character string to be located in phases. @@ -1184,14 +1184,14 @@ phase_bsearch(const char *ptr, int *j, int print) if ((int)phases.size() > 0) { void_ptr = (void *) - bsearch((char *) ptr, + bsearch((char *) cptr, (char *) &phases[0], phases.size(), sizeof(struct phase *), phase_compare_string); } if (void_ptr == NULL && print == TRUE) { - error_string = sformatf( "Could not find phase in list, %s.", ptr); + error_string = sformatf( "Could not find phase in list, %s.", cptr); error_msg(error_string, CONTINUE); } @@ -1320,11 +1320,11 @@ phase_store(const char *name_in) * ********************************************************************** */ /* ---------------------------------------------------------------------- */ struct rate * Phreeqc:: -rate_bsearch(char *ptr, int *j) +rate_bsearch(const char* cptr, int *j) /* ---------------------------------------------------------------------- */ { /* Binary search the structure array "rates" for a name that is equal to - * ptr. Assumes array rates is in sort order. + * cptr. Assumes array rates is in sort order. * * Arguments: * name input, a character string to be located in rates. @@ -1343,7 +1343,7 @@ rate_bsearch(char *ptr, int *j) return (NULL); } void_ptr = (void *) - bsearch((char *) ptr, + bsearch((char *) cptr, (char *) &rates[0], rates.size(), sizeof(struct rate *), rate_compare_string); @@ -3256,13 +3256,13 @@ get_entity_enum(char *name) * */ int i; - char *ptr; + const char* cptr; char token[MAX_LENGTH]; /* * Read keyword */ - ptr = name; - copy_token(token, &ptr, &i); + cptr = name; + copy_token(token, &cptr, &i); check_key(token); switch (next_keyword) diff --git a/tally.cpp b/tally.cpp index c998abb1..fa186793 100644 --- a/tally.cpp +++ b/tally.cpp @@ -416,7 +416,7 @@ fill_tally_table(int *n_user, int index_conservative, int n_buffer) */ int found; LDBLE moles; - //char *ptr; + //const char* cptr; /* * Cycle through tally table columns */ @@ -792,7 +792,7 @@ build_tally_table(void) int count_tt_pure_phase, count_tt_ss_phase, count_tt_kinetics; struct phase *phase_ptr; char token[MAX_LENGTH]; - char *ptr; + const char* cptr; /* * make list of all elements in all entitites * defines the number of rows in the table @@ -906,8 +906,8 @@ build_tally_table(void) if (comp_ptr->Get_add_formula().size() > 0) { strcpy(token, comp_ptr->Get_add_formula().c_str()); - ptr = &(token[0]); - get_elts_in_species(&ptr, 1.0); + cptr = &(token[0]); + get_elts_in_species(&cptr, 1.0); } else { @@ -1025,8 +1025,8 @@ build_tally_table(void) std::string name = it->first; LDBLE coef = it->second; char * temp_name = string_duplicate(name.c_str()); - ptr = temp_name; - get_elts_in_species(&ptr, 1.0 * coef); + cptr = temp_name; + get_elts_in_species(&cptr, 1.0 * coef); free_check_null(temp_name); } } @@ -1157,7 +1157,7 @@ calc_dummy_kinetic_reaction_tally(cxxKinetics *kinetics_ptr) * Go through kinetic components and add positive amount of each reactant */ LDBLE coef; - char *ptr; + const char* cptr; struct phase *phase_ptr; /* * Go through list and generate list of elements and @@ -1191,8 +1191,8 @@ calc_dummy_kinetic_reaction_tally(cxxKinetics *kinetics_ptr) { std::string name = it->first; char * temp_name = string_duplicate(name.c_str()); - ptr = temp_name; - get_elts_in_species(&ptr, coef); + cptr = temp_name; + get_elts_in_species(&cptr, coef); free_check_null(temp_name); } } diff --git a/tidy.cpp b/tidy.cpp index 0d2f14d4..36c3ed97 100644 --- a/tidy.cpp +++ b/tidy.cpp @@ -663,14 +663,14 @@ coef_in_master(struct master * master_ptr) { int l; LDBLE coef; - char *ptr; + const char* cptr; char elt_name[MAX_LENGTH]; struct elt_list *next_elt; coef = 0.0; char * temp_name = string_duplicate(master_ptr->elt->name); - ptr = temp_name; - get_elt(&ptr, elt_name, &l); + cptr = temp_name; + get_elt(&cptr, elt_name, &l); free_check_null(temp_name); for (next_elt = master_ptr->s->next_elt; next_elt->elt != NULL; next_elt++) @@ -1563,7 +1563,7 @@ tidy_pp_assemblage(void) /* ---------------------------------------------------------------------- */ { LDBLE coef; - char *ptr; + const char* cptr; /* * Find pointers for pure phases */ @@ -1616,8 +1616,8 @@ tidy_pp_assemblage(void) } { char * temp_add = string_duplicate(it->second.Get_add_formula().c_str()); - ptr = temp_add; - get_elts_in_species(&ptr, coef); + cptr = temp_add; + get_elts_in_species(&cptr, coef); free_check_null(temp_add); } /* check that all elements are in the database */ @@ -2299,7 +2299,8 @@ tidy_species(void) { int i, j; struct master *master_ptr; - char c, *ptr; + char c; + const char* cptr; /* * Make sure species pointers are ok */ @@ -2331,10 +2332,10 @@ tidy_species(void) for (i = 0; i < (int)master.size(); i++) { char * temp_name = string_duplicate(master[i]->elt->name); - ptr = temp_name; - if (ptr[0] != '[') + cptr = temp_name; + if (cptr[0] != '[') { - while ((c = (int) *(++ptr)) != '\0') + while ((c = (int) *(++cptr)) != '\0') { if (isupper((int) c)) { @@ -2630,7 +2631,7 @@ tidy_surface(void) * After all of data are read, fill in master species for surface comps * Sort surface */ - char *ptr1; + const char* cptr1; cxxSurface *surface_ptr; //std::map::iterator kit; //for (kit = Rxn_surface_map.begin(); kit != Rxn_surface_map.end(); kit++) @@ -2715,8 +2716,8 @@ tidy_surface(void) paren_count = 0; { char * temp_formula = string_duplicate(comp_ptr->Get_formula().c_str()); - ptr1 = temp_formula; - get_elts_in_species(&ptr1, comp_ptr->Get_moles()); + cptr1 = temp_formula; + get_elts_in_species(&cptr1, comp_ptr->Get_moles()); free_check_null(temp_formula); } { @@ -2900,15 +2901,15 @@ phase_rxn_to_trxn(struct phase *phase_ptr, struct reaction *rxn_ptr) * temp reaction structure. */ int i, l; - char *ptr; + const char* cptr; char token[MAX_LENGTH]; LDBLE l_z; trxn.token[0].name = phase_ptr->formula; /* charge */ char * temp_formula = string_duplicate(phase_ptr->formula); - ptr = temp_formula; - get_token(&ptr, token, &l_z, &l); + cptr = temp_formula; + get_token(&cptr, token, &l_z, &l); free_check_null(temp_formula); trxn.token[0].z = l_z; trxn.token[0].s = NULL; @@ -3113,7 +3114,7 @@ tidy_kin_exchange(void) */ { cxxKinetics *kinetics_ptr; - char *ptr; + const char* cptr; LDBLE conc; //std::map::iterator it = Rxn_exchange_map.begin(); @@ -3209,8 +3210,8 @@ tidy_kin_exchange(void) paren_count = 0; { char * temp_formula = string_duplicate(comp_ref.Get_formula().c_str()); - ptr = temp_formula; - get_elts_in_species(&ptr, conc); + cptr = temp_formula; + get_elts_in_species(&cptr, conc); free_check_null(temp_formula); } comp_ref.Set_totals(elt_list_NameDouble()); @@ -3233,7 +3234,7 @@ update_kin_exchange(void) */ { cxxKinetics* kinetics_ptr; - char* ptr; + const char* cptr; LDBLE conc; std::map::iterator it = Rxn_exchange_map.begin(); @@ -3322,8 +3323,8 @@ update_kin_exchange(void) paren_count = 0; { char* temp_formula = string_duplicate(comp_ref.Get_formula().c_str()); - ptr = temp_formula; - get_elts_in_species(&ptr, 1.0); + cptr = temp_formula; + get_elts_in_species(&cptr, 1.0); free_check_null(temp_formula); } cxxNameDouble nd_formula = elt_list_NameDouble(); @@ -3345,8 +3346,8 @@ update_kin_exchange(void) paren_count = 0; { char* temp_formula = string_duplicate(comp_ref.Get_formula().c_str()); - ptr = temp_formula; - get_elts_in_species(&ptr, conc); + cptr = temp_formula; + get_elts_in_species(&cptr, conc); free_check_null(temp_formula); } comp_ref.Set_totals(elt_list_NameDouble()); @@ -3365,7 +3366,7 @@ tidy_min_exchange(void) */ { int n, jj; - char *ptr; + const char* cptr; LDBLE conc; //std::map::iterator it = Rxn_exchange_map.begin(); @@ -3461,8 +3462,8 @@ tidy_min_exchange(void) paren_count = 0; { char * temp_formula = string_duplicate(comp_ref.Get_formula().c_str()); - ptr = temp_formula; - get_elts_in_species(&ptr, conc); + cptr = temp_formula; + get_elts_in_species(&cptr, conc); free_check_null(temp_formula); } comp_ref.Set_totals(elt_list_NameDouble()); @@ -3473,8 +3474,8 @@ tidy_min_exchange(void) paren_count = 0; { char * temp_formula = string_duplicate(comp_ref.Get_formula().c_str()); - ptr = temp_formula; - get_elts_in_species(&ptr, -comp_ref.Get_phase_proportion()); + cptr = temp_formula; + get_elts_in_species(&cptr, -comp_ref.Get_phase_proportion()); free_check_null(temp_formula); } int l; @@ -3482,8 +3483,8 @@ tidy_min_exchange(void) if (phase_ptr != NULL) { char * temp_formula = string_duplicate(phase_ptr->formula); - ptr = temp_formula; - get_elts_in_species(&ptr, 1.0); + cptr = temp_formula; + get_elts_in_species(&cptr, 1.0); free_check_null(temp_formula); } else @@ -3527,7 +3528,7 @@ update_min_exchange(void) */ { int n, jj; - char* ptr; + const char* cptr; LDBLE conc; std::map::iterator it = Rxn_exchange_map.begin(); @@ -3616,8 +3617,8 @@ update_min_exchange(void) paren_count = 0; { char* temp_formula = string_duplicate(comp_ref.Get_formula().c_str()); - ptr = temp_formula; - get_elts_in_species(&ptr, 1.0); + cptr = temp_formula; + get_elts_in_species(&cptr, 1.0); free_check_null(temp_formula); } cxxNameDouble nd_formula = elt_list_NameDouble(); @@ -3639,8 +3640,8 @@ update_min_exchange(void) paren_count = 0; { char* temp_formula = string_duplicate(comp_ref.Get_formula().c_str()); - ptr = temp_formula; - get_elts_in_species(&ptr, conc); + cptr = temp_formula; + get_elts_in_species(&cptr, conc); free_check_null(temp_formula); } comp_ref.Set_totals(elt_list_NameDouble()); @@ -3651,8 +3652,8 @@ update_min_exchange(void) paren_count = 0; { char* temp_formula = string_duplicate(comp_ref.Get_formula().c_str()); - ptr = temp_formula; - get_elts_in_species(&ptr, -comp_ref.Get_phase_proportion()); + cptr = temp_formula; + get_elts_in_species(&cptr, -comp_ref.Get_phase_proportion()); free_check_null(temp_formula); } int l; @@ -3660,8 +3661,8 @@ update_min_exchange(void) if (phase_ptr != NULL) { char* temp_formula = string_duplicate(phase_ptr->formula); - ptr = temp_formula; - get_elts_in_species(&ptr, 1.0); + cptr = temp_formula; + get_elts_in_species(&cptr, 1.0); free_check_null(temp_formula); } else @@ -3810,10 +3811,10 @@ tidy_min_surface(void) /* if (conc < MIN_RELATED_SURFACE) conc = 0.0; */ { char * temp_formula = string_duplicate(surface_comp_ptr->Get_formula().c_str()); - char *ptr = temp_formula; + const char* cptr = temp_formula; count_elts = 0; paren_count = 0; - get_elts_in_species(&ptr, conc); + get_elts_in_species(&cptr, conc); free_check_null(temp_formula); } { @@ -3841,8 +3842,8 @@ tidy_min_surface(void) paren_count = 0; { char * temp_formula = string_duplicate(phase_ptr->formula); - char * ptr = temp_formula; - get_elts_in_species(&ptr, 1.0); + const char* cptr = temp_formula; + get_elts_in_species(&cptr, 1.0); free_check_null(temp_formula); } // Revise logic for surface related to mineral @@ -3852,8 +3853,8 @@ tidy_min_surface(void) // Use formula for all types of surfaces { char * temp_formula = string_duplicate(comp_jj_ptr->Get_formula().c_str()); - char *ptr = temp_formula; - get_elts_in_species(&ptr, + const char* cptr = temp_formula; + get_elts_in_species(&cptr, -comp_jj_ptr->Get_phase_proportion()); if (surface_ptr->Get_type() != cxxSurface::CD_MUSIC) @@ -4064,10 +4065,10 @@ update_min_surface(void) else /* need to generate from scratch */ { char* temp_formula = string_duplicate(surface_comp_ptr->Get_formula().c_str()); - char* ptr = temp_formula; + const char* cptr = temp_formula; count_elts = 0; paren_count = 0; - get_elts_in_species(&ptr, conc); + get_elts_in_species(&cptr, conc); free_check_null(temp_formula); cxxNameDouble nd = elt_list_NameDouble(); @@ -4197,10 +4198,10 @@ tidy_kin_surface(void) /* if (conc < MIN_RELATED_SURFACE) conc = 0.0; */ { char * temp_formula = string_duplicate(comp_ptr->Get_formula().c_str()); - char *ptr = temp_formula; + const char* cptr = temp_formula; count_elts = 0; paren_count = 0; - get_elts_in_species(&ptr, conc); + get_elts_in_species(&cptr, conc); free_check_null(temp_formula); } { @@ -4259,8 +4260,8 @@ tidy_kin_surface(void) else { char * temp_name = string_duplicate(name.c_str()); - char * ptr = temp_name; - get_elts_in_species(&ptr, coef); + const char* cptr = temp_name; + get_elts_in_species(&cptr, coef); free_check_null(temp_name); } } @@ -4287,8 +4288,8 @@ tidy_kin_surface(void) kin_comp_ptr->Get_rate_name().c_str()) == 0) { char * temp_formula = string_duplicate( comp_ptr->Get_formula().c_str()); - char *ptr = temp_formula; - get_elts_in_species(&ptr, -1 * comp_ptr->Get_phase_proportion()); + const char* cptr = temp_formula; + get_elts_in_species(&cptr, -1 * comp_ptr->Get_phase_proportion()); free_check_null(temp_formula); } } @@ -4477,10 +4478,10 @@ update_kin_surface(void) else /* need to generate from scratch */ { char* temp_formula = string_duplicate(comp_ptr->Get_formula().c_str()); - char* ptr = temp_formula; + const char* cptr = temp_formula; count_elts = 0; paren_count = 0; - get_elts_in_species(&ptr, conc); + get_elts_in_species(&cptr, conc); free_check_null(temp_formula); cxxNameDouble nd = elt_list_NameDouble(); diff --git a/transport.cpp b/transport.cpp index 5f060834..709d09ce 100644 --- a/transport.cpp +++ b/transport.cpp @@ -1678,7 +1678,8 @@ set_initial_moles(int i) /* ---------------------------------------------------------------------- */ { cxxKinetics *kinetics_ptr; - char token[MAX_LENGTH], token1[MAX_LENGTH], *ptr; + char token[MAX_LENGTH], token1[MAX_LENGTH]; + const char* cptr; int j, k, l; /* * Pure phase assemblage @@ -1763,11 +1764,11 @@ set_initial_moles(int i) count_elts = 0; paren_count = 0; strcpy(token, "X"); - ptr = token; - get_elts_in_species(&ptr, 2e-10); - ptr = token; + cptr = token; + get_elts_in_species(&cptr, 2e-10); + cptr = token; LDBLE z; - get_token(&ptr, token1, &z, &l); + get_token(&cptr, token1, &z, &l); comp.Set_formula(token1); comp.Set_formula_z(z); comp.Set_totals(elt_list_NameDouble()); @@ -2747,9 +2748,9 @@ diffuse_implicit(LDBLE DDt, int stagnant) break; } char * temp_name = string_duplicate(ct[1].J_ij[cp].name); - char * ptr = temp_name; + const char* cptr = temp_name; count_elts = 0; - get_elts_in_species(&ptr, 1); + get_elts_in_species(&cptr, 1); free_check_null(temp_name); for (int k = 0; k < count_elts; k++) { @@ -3589,15 +3590,15 @@ fill_m_s(struct J_ij *l_J_ij, int l_J_ij_count_spec, int icell, int stagnant) */ int j, k, l; LDBLE fraction; - char *ptr; + const char* cptr; for (j = 0; j < l_J_ij_count_spec; j++) { { char * temp_name = string_duplicate(l_J_ij[j].name); - ptr = temp_name; + cptr = temp_name; count_elts = 0; - get_elts_in_species(&ptr, 1); + get_elts_in_species(&cptr, 1); free_check_null(temp_name); } if (implicit && stagnant < 2) diff --git a/utilities.cpp b/utilities.cpp index 574b69dc..6010b0c1 100644 --- a/utilities.cpp +++ b/utilities.cpp @@ -312,13 +312,13 @@ compute_gfw(const char *string, LDBLE * gfw) int i; char token[MAX_LENGTH]; - char *ptr; + const char* cptr; count_elts = 0; paren_count = 0; strcpy(token, string); - ptr = token; - if (get_elts_in_species(&ptr, 1.0) == ERROR) + cptr = token; + if (get_elts_in_species(&cptr, 1.0) == ERROR) { return (ERROR); } @@ -337,16 +337,16 @@ compute_gfw(const char *string, LDBLE * gfw) /* ---------------------------------------------------------------------- */ int Phreeqc:: -copy_token(char *token_ptr, const char **ptr, int *length) +copy_token(char *token_ptr, const char **cptr, int *length) /* ---------------------------------------------------------------------- */ { /* - * Copies from **ptr to *token_ptr until first space is encountered. + * Copies from **cptr to *token_ptr until first space is encountered. * * Arguments: * *token_ptr output, place to store token * - * **ptr input, character string to read token from + * **cptr input, character string to read token from * output, next position after token * * length output, length of token @@ -364,8 +364,8 @@ copy_token(char *token_ptr, const char **ptr, int *length) /* * Read to end of whitespace */ - while (isspace((int) (c = **ptr))) - (*ptr)++; + while (isspace((int) (c = **cptr))) + (*cptr)++; /* * Check what we have */ @@ -393,12 +393,12 @@ copy_token(char *token_ptr, const char **ptr, int *length) * Begin copying to token */ i = 0; - while ((!isspace((int) (c = **ptr))) && + while ((!isspace((int) (c = **cptr))) && /* c != ',' && */ c != ';' && c != '\0') { token_ptr[i] = c; - (*ptr)++; + (*cptr)++; i++; } token_ptr[i] = '\0'; @@ -407,16 +407,16 @@ copy_token(char *token_ptr, const char **ptr, int *length) } /* ---------------------------------------------------------------------- */ int Phreeqc:: -copy_token(std::string &token, const char **ptr) +copy_token(std::string &token, const char **cptr) /* ---------------------------------------------------------------------- */ { /* - * Copies from **ptr to *token until first space is encountered. + * Copies from **cptr to *token until first space is encountered. * * Arguments: * &token_ptr output, place to store token * - * **ptr input, character string to read token from + * **cptr input, character string to read token from * output, next position after token * * Returns: @@ -433,8 +433,8 @@ copy_token(std::string &token, const char **ptr) * Read to end of whitespace */ token.clear(); - while (isspace((int) (c = **ptr))) - (*ptr)++; + while (isspace((int) (c = **cptr))) + (*cptr)++; /* * Check what we have */ @@ -463,107 +463,19 @@ copy_token(std::string &token, const char **ptr) */ char c_char[2]; c_char[1] = '\0'; - while ((!isspace((int) (c = **ptr))) && + while ((!isspace((int) (c = **cptr))) && /* c != ',' && */ c != ';' && c != '\0') { c_char[0] = c; token.append(c_char); - (*ptr)++; + (*cptr)++; } return (return_value); } -#if defined PHREEQ98 /* ---------------------------------------------------------------------- */ int Phreeqc:: -copy_title(char *token_ptr, char **ptr, int *length) -/* ---------------------------------------------------------------------- */ -{ -/* - * Copies from **ptr to *token_ptr until first space or comma is encountered. - * - * Arguments: - * *token_ptr output, place to store token - * - * **ptr input, character string to read token from - * output, next position after token - * - * length output, length of token - * - * Returns: - * UPPER, - * LOWER, - * DIGIT, - * EMPTY, - * UNKNOWN. - */ - int i, return_value; - char c; - int Quote = FALSE; - -/* - * Read to end of whitespace - */ - while (isspace((int) (c = **ptr)) || (c == ',') || (c == '"')) - { - if (c == '"') - Quote = TRUE; - (*ptr)++; - } -/* - * Check what we have - */ - if (isupper((int) c) || c == '[') - { - return_value = UPPER; - } - else if (islower((int) c)) - { - return_value = LOWER; - } - else if (isdigit((int) c) || c == '.' || c == '-') - { - return_value = DIGIT; - } - else if (c == '\0') - { - return_value = EMPTY; - } - else - { - return_value = UNKNOWN; - } -/* - * Begin copying to token - */ - i = 0; - if (Quote == TRUE) - { - while (((int) (c = **ptr) != '"') && c != '\0') - { - token_ptr[i] = c; - (*ptr)++; - i++; - } - } - else - { - while ((!isspace((int) (c = **ptr))) && - c != ',' && c != ';' && c != '\0') - { - token_ptr[i] = c; - (*ptr)++; - i++; - } - } - token_ptr[i] = '\0'; - *length = i; - return (return_value); -} -#endif -/* ---------------------------------------------------------------------- */ -int Phreeqc:: -dup_print(const char *ptr, int emphasis) +dup_print(const char* cptr, int emphasis) /* ---------------------------------------------------------------------- */ { /* @@ -576,14 +488,14 @@ dup_print(const char *ptr, int emphasis) if (pr.headings == FALSE) return (OK); - std::string save_in(ptr); - l = (int) strlen(ptr); + std::string save_in(cptr); + l = (int) strlen(cptr); if (emphasis == TRUE) { std::string dash; dash.resize(l, '-'); - output_msg(sformatf("%s\n%s\n%s\n\n", dash.c_str(), save_in.c_str(), dash)); - log_msg(sformatf("%s\n%s\n%s\n\n", dash.c_str(), save_in.c_str(), dash)); + output_msg(sformatf("%s\n%s\n%s\n\n", dash.c_str(), save_in.c_str(), dash.c_str())); + log_msg(sformatf("%s\n%s\n%s\n\n", dash.c_str(), save_in.c_str(), dash.c_str())); } else { @@ -642,22 +554,22 @@ get_token(const char **eqnaddr, char *string, LDBLE * l_z, int *l) int i, j; int ltoken, lcharge; char c; - char *ptr, *ptr1, *rest; + const char* cptr, *ptr1, *rest; char charge[MAX_LENGTH]; rest = *eqnaddr; - ptr = *eqnaddr; + cptr = *eqnaddr; i = 0; /* * Find end of token or begining of charge */ - while (((c = *ptr) != '+') && (c != '-') && (c != '=') && (c != '\0')) + while (((c = *cptr) != '+') && (c != '-') && (c != '=') && (c != '\0')) { string[i++] = c; if (c == '[') { - ptr++; - while ((c = *ptr) != ']') + cptr++; + while ((c = *cptr) != ']') { if (c == '\0') { @@ -675,7 +587,7 @@ get_token(const char **eqnaddr, char *string, LDBLE * l_z, int *l) MAX_LENGTH, string)); return (ERROR); } - ptr++; + cptr++; } string[i++] = c; } @@ -688,7 +600,7 @@ get_token(const char **eqnaddr, char *string, LDBLE * l_z, int *l) MAX_LENGTH, string)); return (ERROR); } - ptr++; + cptr++; } string[i] = '\0'; ltoken = i; @@ -706,7 +618,7 @@ get_token(const char **eqnaddr, char *string, LDBLE * l_z, int *l) */ if (c == '=' || c == '\0') { - *eqnaddr = ptr; + *eqnaddr = cptr; lcharge = 0; *l_z = 0.0; } @@ -716,7 +628,7 @@ get_token(const char **eqnaddr, char *string, LDBLE * l_z, int *l) * Copy characters into charge until next species or end is detected */ j = 0; - ptr1 = ptr; + ptr1 = cptr; while ((isalpha((int) (c = *ptr1)) == FALSE) && (c != '(') && (c != ')') && @@ -834,7 +746,7 @@ parse_couple(char *token) * order. */ int e1, e2, p1, p2; - char *ptr; + const char* cptr; char elt1[MAX_LENGTH], elt2[MAX_LENGTH], paren1[MAX_LENGTH], paren2[MAX_LENGTH]; @@ -844,9 +756,9 @@ parse_couple(char *token) return (OK); } while (replace("+", "", token) == TRUE); - ptr = token; - get_elt(&ptr, elt1, &e1); - if (*ptr != '(') + cptr = token; + get_elt(&cptr, elt1, &e1); + if (*cptr != '(') { error_string = sformatf( "Element name must be followed by " "parentheses in redox couple, %s.", token); @@ -857,10 +769,10 @@ parse_couple(char *token) paren_count = 1; paren1[0] = '('; p1 = 1; - while (*ptr != '\0') + while (*cptr != '\0') { - ptr++; - if (*ptr == '/' || *ptr == '\0') + cptr++; + if (*cptr == '/' || *cptr == '\0') { error_string = sformatf( "End of line or " "/" @@ -868,17 +780,17 @@ parse_couple(char *token) error_msg(error_string, CONTINUE); return (ERROR); } - paren1[p1++] = *ptr; - if (*ptr == '(') + paren1[p1++] = *cptr; + if (*cptr == '(') paren_count++; - if (*ptr == ')') + if (*cptr == ')') paren_count--; if (paren_count == 0) break; } paren1[p1] = '\0'; - ptr++; - if (*ptr != '/') + cptr++; + if (*cptr != '/') { error_string = sformatf( " " "/" " must follow parentheses " "ending first half of redox couple, %s.", token); @@ -886,8 +798,8 @@ parse_couple(char *token) parse_error++; return (ERROR); } - ptr++; - get_elt(&ptr, elt2, &e2); + cptr++; + get_elt(&cptr, elt2, &e2); if (strcmp(elt1, elt2) != 0) { error_string = sformatf( "Redox couple must be two redox states " @@ -895,7 +807,7 @@ parse_couple(char *token) error_msg(error_string, CONTINUE); return (ERROR); } - if (*ptr != '(') + if (*cptr != '(') { error_string = sformatf( "Element name must be followed by " "parentheses in redox couple, %s.", token); @@ -906,10 +818,10 @@ parse_couple(char *token) paren2[0] = '('; paren_count = 1; p2 = 1; - while (*ptr != '\0') + while (*cptr != '\0') { - ptr++; - if (*ptr == '/' || *ptr == '\0') + cptr++; + if (*cptr == '/' || *cptr == '\0') { error_string = sformatf( "End of line or " "/" " encountered" " before end of parentheses, %s.", token); @@ -917,10 +829,10 @@ parse_couple(char *token) return (ERROR); } - paren2[p2++] = *ptr; - if (*ptr == '(') + paren2[p2++] = *cptr; + if (*cptr == '(') paren_count++; - if (*ptr == ')') + if (*cptr == ')') paren_count--; if (paren_count == 0) break; @@ -1003,7 +915,7 @@ replace(const char *str1, const char *str2, char *str) * FALSE if string was not replaced */ int l, l1, l2; - char *ptr_start; + char* ptr_start; ptr_start = strstr(str, str1); /* @@ -1028,7 +940,14 @@ replace(const char *str1, const char *str2, char *str) memcpy(ptr_start, str2, l2); return (TRUE); } - +void Phreeqc:: +replace(std::string &stds, const char* str1, const char* str2) +{ + size_t pos; + while ((pos = stds.find(str1)) != std::string::npos) { + stds.replace(pos, 1, str2); + } +} /* ---------------------------------------------------------------------- */ void Phreeqc:: space(void **ptr, int i, int *max, int struct_size) @@ -1135,7 +1054,7 @@ str_tolower(char *str) /* * Replaces string, str, with same string, lower case */ - char *ptr; + char* ptr; ptr = str; while (*ptr != '\0') { @@ -1152,7 +1071,7 @@ str_toupper(char *str) /* * Replaces string, str, with same string, lower case */ - char *ptr; + char* ptr; ptr = str; while (*ptr != '\0') { @@ -1460,7 +1379,7 @@ string_trim(char *str) * EMPTY if string is all whitespace */ int i, l, start, end, length; - char *ptr_start; + char* ptr_start; l = (int) strlen(str); /* @@ -1494,6 +1413,22 @@ string_trim(char *str) return (TRUE); } +void Phreeqc::string_trim_left(std::string& str) +{ + const std::string& chars = "\t\n "; + str.erase(0, str.find_first_not_of(chars)); +} +void Phreeqc::string_trim_right(std::string& str) +{ + const std::string& chars = "\t\n "; + str.erase(str.find_last_not_of(chars) + 1); +} +void Phreeqc::string_trim(std::string& str) +{ + const std::string& chars = "\t\n "; + str.erase(0, str.find_first_not_of(chars)); + str.erase(str.find_last_not_of(chars) + 1); +} /* ---------------------------------------------------------------------- */ int Phreeqc:: @@ -1547,7 +1482,7 @@ string_trim_left(char *str) * EMPTY if string is all whitespace */ int i, l, start, end, length; - char *ptr_start; + char* ptr_start; l = (int) strlen(str); /* From 51fec19379a21e14d8120ea576a67b1c174bf346 Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Fri, 26 Mar 2021 21:21:20 -0600 Subject: [PATCH 14/53] class_main --- Phreeqc.cpp | 4 +- Phreeqc.h | 15 +- basicsubs.cpp | 4 +- class_main.cpp | 434 +++++++------------------------------------------ inverse.cpp | 20 +-- read.cpp | 8 +- structures.cpp | 2 - 7 files changed, 75 insertions(+), 412 deletions(-) diff --git a/Phreeqc.cpp b/Phreeqc.cpp index c4441577..699a05da 100644 --- a/Phreeqc.cpp +++ b/Phreeqc.cpp @@ -863,8 +863,6 @@ void Phreeqc::init(void) phreeqc_mpi_myself = 0; first_read_input = TRUE; - user_database = NULL; - //have_punch_name = FALSE; print_density = 0; print_viscosity = 0; cell_pore_volume = 0; @@ -2016,7 +2014,7 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) phreeqc_mpi_myself = 0; first_read_input = TRUE; - user_database = string_duplicate(pSrc->user_database); + user_database = pSrc->user_database; //have_punch_name = pSrc->have_punch_name; print_density = pSrc->print_density; print_viscosity = pSrc->print_viscosity; diff --git a/Phreeqc.h b/Phreeqc.h index 89f1e57c..aaf4770b 100644 --- a/Phreeqc.h +++ b/Phreeqc.h @@ -408,8 +408,8 @@ public: int store_get_equi_reactants(int k, int kin_end); // mainsubs.cpp ------------------------------- - std::ifstream* open_input_stream(char* query, char* default_name, std::ios_base::openmode mode, bool batch); - std::ofstream* open_output_stream(char* query, char* default_name, std::ios_base::openmode mode, bool batch); + std::ifstream* open_input_stream(std::string query, std::string& default_name, std::ios_base::openmode mode, bool batch); + std::ofstream* open_output_stream(std::string query, std::string& default_name, std::ios_base::openmode mode, bool batch); int copy_entities(void); void do_mixes(void); void initialize(void); @@ -1049,11 +1049,8 @@ public: LDBLE calc_rho_0(LDBLE tc, LDBLE pa); LDBLE calc_dielectrics(LDBLE tc, LDBLE pa); int compute_gfw(const char* string, LDBLE* gfw); -#if defined PHREEQ98 - int copy_title(char* token_ptr, char** ptr, int* length); -#endif - int copy_token(char* token_ptr, const char** ptr, int* length); - int copy_token(std::string& token, const char** ptr); + static int copy_token(char* token_ptr, const char** ptr, int* length); + static int copy_token(std::string& token, const char** ptr); int dup_print(const char* cptr, int emphasis); int equal(LDBLE a, LDBLE b, LDBLE eps); public: @@ -1630,7 +1627,7 @@ protected: std::map isotope_alpha_map; int phreeqc_mpi_myself; int first_read_input; - char* user_database; + std::string user_database; //int have_punch_name; /* VP: Density Start */ @@ -1748,7 +1745,7 @@ protected: int forward_output_to_log; /* phreeqc_files.cpp ------------------------------- */ - char* default_data_base; + std::string default_data_base; /* Pitzer */ int pitzer_model, sit_model, pitzer_pe; int full_pitzer, always_full_pitzer, ICON, IC; diff --git a/basicsubs.cpp b/basicsubs.cpp index 159a34b0..cefc1cc0 100644 --- a/basicsubs.cpp +++ b/basicsubs.cpp @@ -2821,10 +2821,8 @@ kinetics_formula(std::string kin_name, cxxNameDouble &stoichiometry) // add formula std::string name = it->first; LDBLE coef = it->second; - char * temp_name = string_duplicate(name.c_str()); - const char* cptr = temp_name; + const char* cptr = &name[0]; get_elts_in_species(&cptr, coef); - free_check_null(temp_name); } } formula.append(kin_name); diff --git a/class_main.cpp b/class_main.cpp index 24fa50e0..9506e128 100644 --- a/class_main.cpp +++ b/class_main.cpp @@ -320,17 +320,15 @@ write_banner(void) return 0; } -#ifdef ERROR_OSTREAM /* ---------------------------------------------------------------------- */ int Phreeqc:: process_file_names(int argc, char *argv[], std::istream **db_cookie, std::istream **input_cookie, int log) /* ---------------------------------------------------------------------- */ { - int l; - char token[2 * MAX_LENGTH], default_name[2 * MAX_LENGTH]; - char query[2 * MAX_LENGTH]; - char in_file[2 * MAX_LENGTH], out_file[2 * MAX_LENGTH], db_file[2 * MAX_LENGTH]; + std::string token, default_name; + std::string query; + std::string in_file, out_file, db_file; char *env_ptr; const char* cptr; /* @@ -365,28 +363,28 @@ process_file_names(int argc, char *argv[], std::istream **db_cookie, /* * Open user-input file */ - strcpy(query, "Name of input file?"); + query = "Name of input file?"; std::ifstream * local_input_stream = NULL; if (argc <= 1) { - default_name[0] = '\0'; + default_name.clear(); local_input_stream = open_input_stream(query, default_name, std::ios_base::in, false); } else { - strcpy(default_name, argv[1]); + default_name = argv[1]; local_input_stream = open_input_stream(query, default_name, std::ios_base::in, true); } - screen_msg(sformatf("Input file: %s\n\n", default_name)); - strcpy(in_file, default_name); + screen_msg(sformatf("Input file: %s\n\n", default_name.c_str())); + in_file = default_name; /* * Open file for output */ - strcpy(query, "Name of output file?"); - cptr = default_name; - copy_token(token, &cptr, &l); - strcpy(token, default_name); - strcat(token, ".out"); + query = "Name of output file?"; + cptr = default_name.c_str(); + copy_token(token, &cptr); + token = default_name; + token.append(".out"); std::ofstream * local_output_stream = NULL; if (argc <= 1) { @@ -398,11 +396,11 @@ process_file_names(int argc, char *argv[], std::istream **db_cookie, } else if (argc >= 3) { - strcpy(token, argv[2]); + token = argv[2]; local_output_stream = open_output_stream(query, token, std::ios_base::out, true); } - screen_msg(sformatf("Output file: %s\n\n", token)); - strcpy(out_file, token); + screen_msg(sformatf("Output file: %s\n\n", token.c_str())); + out_file = token; phrq_io->Set_output_ostream(local_output_stream); /* * Open log file @@ -423,15 +421,14 @@ process_file_names(int argc, char *argv[], std::istream **db_cookie, if (get_line() == KEYWORD) { cptr = line; - copy_token(token, &cptr, &l); - if (strcmp_nocase(token, "database") == 0) + copy_token(token, &cptr); + if (strcmp_nocase(token.c_str(), "database") == 0) { - user_database = (char *) free_check_null(user_database); - user_database = string_duplicate(cptr); - if (string_trim(user_database) == EMPTY) + user_database = cptr; + string_trim(user_database); + if (user_database.size() == 0) { warning_msg("DATABASE file name is missing; default database will be used."); - user_database = (char *) free_check_null(user_database); } } } @@ -440,26 +437,26 @@ process_file_names(int argc, char *argv[], std::istream **db_cookie, else { delete local_input_stream; - error_string = sformatf( "Error opening file, %s.", in_file); + error_string = sformatf( "Error opening file, %s.", in_file.c_str()); error_msg(error_string, STOP); } /* * Open data base */ - strcpy(query, "Name of database file?"); + query = "Name of database file?"; env_ptr = getenv("PHREEQC_DATABASE"); - if (user_database != NULL) + if (user_database.size() > 0) { - strcpy(token, user_database); + token = user_database; } else if (env_ptr != NULL) { - strcpy(token, env_ptr); + token = env_ptr; } else { - strcpy(token, default_data_base); + token = default_data_base; } std::ifstream * local_database_file = NULL; @@ -473,9 +470,9 @@ process_file_names(int argc, char *argv[], std::istream **db_cookie, } else if (argc >= 4) { - if (user_database == NULL) + if (user_database.size() == 0) { - strcpy(token, argv[3]); + token = argv[3]; } else { @@ -487,24 +484,21 @@ process_file_names(int argc, char *argv[], std::istream **db_cookie, } local_database_file->close(); delete local_database_file; - - user_database = (char *) free_check_null(user_database); - user_database = string_duplicate(token); - screen_msg(sformatf("Database file: %s\n\n", token)); - strcpy(db_file, token); - output_msg(sformatf(" Input file: %s\n", in_file)); - output_msg(sformatf(" Output file: %s\n", out_file)); + user_database = token; + screen_msg(sformatf("Database file: %s\n\n", token.c_str())); + db_file = token; + output_msg(sformatf(" Input file: %s\n", in_file.c_str())); + output_msg(sformatf(" Output file: %s\n", out_file.c_str())); #ifdef NPP output_msg(sformatf("Using PHREEQC: version 3.6.5, compiled February 24, 2021\n")); #endif - output_msg(sformatf("Database file: %s\n\n", token)); + output_msg(sformatf("Database file: %s\n\n", token.c_str())); #ifdef NPP output_flush(); #endif /* * local cleanup */ - user_database = (char *) free_check_null(user_database); line = (char *) free_check_null(line); line_save = (char *) free_check_null(line_save); @@ -517,239 +511,37 @@ process_file_names(int argc, char *argv[], std::istream **db_cookie, } return 0; } -#else -/* ---------------------------------------------------------------------- */ -int Phreeqc:: -process_file_names(int argc, char *argv[], std::istream **db_cookie, - std::istream **input_cookie, int log) -/* ---------------------------------------------------------------------- */ -{ - int l; - char token[2 * MAX_LENGTH], default_name[2 * MAX_LENGTH]; - char query[2 * MAX_LENGTH]; - char in_file[2 * MAX_LENGTH], out_file[2 * MAX_LENGTH], db_file[2 * MAX_LENGTH]; - char *env_ptr; - const char* cptr; -/* - * Prepare error handling - */ - try { - if (phrq_io == NULL) - { - std::cerr << "No PHRQ_io output handler defined in process_file_names" << "\n"; - } -/* - * Prep for get_line - */ - max_line = MAX_LINE; - space((void **) ((void *) &line), INIT, &max_line, sizeof(char)); - space((void **) ((void *) &line_save), INIT, &max_line, sizeof(char)); -/* - * Open error ostream - */ - if (argc > 4) - { - if (!phrq_io->error_open(argv[4])) - { - error_string = sformatf( "Error opening file, %s.", argv[4]); - warning_msg(error_string); - } - } - else - { - phrq_io->error_open(NULL); - } -/* - * Open user-input file - */ - strcpy(query, "Name of input file?"); - std::ifstream * local_input_stream = NULL; - if (argc <= 1) - { - default_name[0] = '\0'; - local_input_stream = open_input_stream(query, default_name, std::ios_base::in, false); - } - else - { - strcpy(default_name, argv[1]); - local_input_stream = open_input_stream(query, default_name, std::ios_base::in, true); - } - screen_msg(sformatf("Input file: %s\n\n", default_name)); - strcpy(in_file, default_name); -/* - * Open file for output - */ - strcpy(query, "Name of output file?"); - cptr = default_name; - copy_token(token, &cptr, &l); - strcat(token, ".out"); - std::ofstream * local_output_stream; - if (argc <= 1) - { - local_output_stream = open_output_stream(query, token, std::ios_base::out, false); - } - else if (argc == 2) - { - local_output_stream = open_output_stream(query, token, std::ios_base::out, true); - } - else if (argc >= 3) - { - strcpy(token, argv[2]); - local_output_stream = open_output_stream(query, token, std::ios_base::out, true); - } - screen_msg(sformatf("Output file: %s\n\n", token)); - strcpy(out_file, token); - phrq_io->Set_output_ostream(local_output_stream); -/* - * Open log file - */ - if (log == TRUE) - { - if (!phrq_io->log_open("phreeqc.log")) - { - error_msg("Cannot open log file, phreeqc.log.", STOP); - } - } -/* - * Read input file for DATABASE keyword - */ - if (local_input_stream->is_open()) - { - phrq_io->push_istream(local_input_stream); - if (get_line() == KEYWORD) - { - cptr = line; - copy_token(token, &cptr, &l); - if (strcmp_nocase(token, "database") == 0) - { - user_database = (char *) free_check_null(user_database); - user_database = string_duplicate(cptr); - if (string_trim(user_database) == EMPTY) - { - warning_msg("DATABASE file name is missing; default database will be used."); - user_database = (char *) free_check_null(user_database); - } - } - } - phrq_io->pop_istream(); - } - else - { - delete local_input_stream; - error_string = sformatf( "Error opening file, %s.", in_file); - error_msg(error_string, STOP); - } - -/* - * Open data base - */ - strcpy(query, "Name of database file?"); - env_ptr = getenv("PHREEQC_DATABASE"); - if (user_database != NULL) - { - strcpy(token, user_database); - } - else if (env_ptr != NULL) - { - strcpy(token, env_ptr); - } - else - { - strcpy(token, default_data_base); - } - - std::ifstream * local_database_file; - if (argc <= 1) - { - local_database_file = open_input_stream(query, token, std::ios_base::in, false); - } - else if (argc < 4) - { - local_database_file = open_input_stream(query, token, std::ios_base::in, true); - } - else if (argc >= 4) - { - if (user_database == NULL) - { - strcpy(token, argv[3]); - } - else - { -#ifndef PHREEQCI_GUI - warning_msg ("Database file from DATABASE keyword is used; command line argument ignored."); -#endif - } - local_database_file = open_input_stream(query, token, std::ios_base::in, true); - } - local_database_file->close(); - delete local_database_file; - screen_msg(sformatf("Database file: %s\n\n", token)); - strcpy(db_file, token); - - output_msg(sformatf(" Input file: %s\n", in_file)); - output_msg(sformatf(" Output file: %s\n", out_file)); - output_msg(sformatf("Database file: %s\n\n", token)); - /* - * local cleanup - */ - user_database = (char *) free_check_null(user_database); - user_database = string_duplicate(token); - line = (char *) free_check_null(line); - line_save = (char *) free_check_null(line_save); - - *db_cookie = new std::ifstream(db_file, std::ios_base::in); - *input_cookie = new std::ifstream(in_file, std::ios_base::in); - } - catch (const PhreeqcStop& e) - { - return get_input_errors(); - } - return 0; -} -#endif /* ---------------------------------------------------------------------- */ std::ifstream * Phreeqc:: -open_input_stream(char *query, char *default_name, std::ios_base::openmode mode, bool batch) +open_input_stream(std::string query, std::string& default_name, std::ios_base::openmode mode, bool batch) /* ---------------------------------------------------------------------- */ { - char name[MAX_LENGTH]; + std::string name; std::ifstream *new_stream; - int l; -#ifdef ERROR_OSTREAM std::ostream * error_ostream_save = phrq_io->Get_error_ostream(); -#else - FILE * error_file_save = phrq_io->Get_error_file(); -#endif for (;;) { /* * Get file name */ - strcpy(name, default_name); + name = default_name; if (!batch ) { -#ifdef ERROR_OSTREAM phrq_io->Set_error_ostream(&std::cerr); -#else - phrq_io->Set_error_file(stderr); -#endif - screen_msg(sformatf("%s\n", query)); - if (default_name[0] != '\0') + screen_msg(sformatf("%s\n", query.c_str())); + if (default_name.size() > 0) { - screen_msg(sformatf("Default: %s\n", default_name)); + screen_msg(sformatf("Default: %s\n", default_name.c_str())); } - char *s_ptr = fgets(name, MAX_LENGTH, stdin); - if (s_ptr == NULL) + std::getline(std::cin, name); + if (name.size() == 0) { std::cerr << "Failed defining name." << std::endl; } - - l = (int) strlen(name); - name[l - 1] = '\0'; - if (name[0] == '\0') + if (name.size() == 0) { - strcpy(name, default_name); + name = default_name; } } /* @@ -758,15 +550,11 @@ open_input_stream(char *query, char *default_name, std::ios_base::openmode mode, new_stream = new std::ifstream(name, mode); if (new_stream == NULL || !new_stream->is_open()) { -#ifdef ERROR_OSTREAM phrq_io->Set_error_ostream(&std::cerr); -#else - phrq_io->Set_error_file(stderr); -#endif - error_string = sformatf( "\nERROR: Cannot open file, %s.\n", name); + error_string = sformatf( "\nERROR: Cannot open file, %s.\n", name.c_str()); screen_msg(error_string); #ifdef NPP - error_msg(sformatf( "\nERROR: Cannot open file, %s.\n Please check, and give the correct, full path + name.\n", name), STOP); + error_msg(sformatf( "\nERROR: Cannot open file, %s.\n Please check, and give the correct, full path + name.\n", name.c_str()), STOP); break; #endif error_flush(); @@ -775,62 +563,44 @@ open_input_stream(char *query, char *default_name, std::ios_base::openmode mode, } break; } - strncpy(default_name, name, MAX_LENGTH); + default_name = name; if (!batch ) { - //phrq_io->Set_error_ostream(error_file_save); -#ifdef ERROR_OSTREAM phrq_io->Set_error_ostream(error_ostream_save); -#else - phrq_io->Set_error_file(error_file_save); -#endif } return (new_stream); } /* ---------------------------------------------------------------------- */ std::ofstream * Phreeqc:: -open_output_stream(char *query, char *default_name, std::ios_base::openmode mode, bool batch) +open_output_stream(std::string query, std::string& default_name, std::ios_base::openmode mode, bool batch) /* ---------------------------------------------------------------------- */ { - char name[MAX_LENGTH]; + std::string name; std::ofstream *new_stream; - int l; -#ifdef ERROR_OSTREAM std::ostream * error_ostream_save = phrq_io->Get_error_ostream(); -#else - FILE * error_file_save = phrq_io->Get_error_file(); -#endif for (;;) { /* * Get file name */ - strcpy(name, default_name); + name = default_name; if (!batch ) { -#ifdef ERROR_OSTREAM phrq_io->Set_error_ostream(&std::cerr); -#else - phrq_io->Set_error_file(stderr); -#endif - - screen_msg(sformatf("%s\n", query)); + screen_msg(sformatf("%s\n", query.c_str())); if (default_name[0] != '\0') { - screen_msg(sformatf("Default: %s\n", default_name)); + screen_msg(sformatf("Default: %s\n", default_name.c_str())); } - char *s_ptr = fgets(name, MAX_LENGTH, stdin); - if (s_ptr == NULL) + std::getline(std::cin, name); + if (name.size() == 0) { std::cerr << "Failed defining name." << std::endl; } - - l = (int) strlen(name); - name[l - 1] = '\0'; - if (name[0] == '\0') + if (name.size() == 0) { - strcpy(name, default_name); + name = default_name; } } /* @@ -839,12 +609,8 @@ open_output_stream(char *query, char *default_name, std::ios_base::openmode mode new_stream = new std::ofstream(name, mode); if (new_stream == NULL || !new_stream->is_open()) { -#ifdef ERROR_OSTREAM phrq_io->Set_error_ostream(&std::cerr); -#else - phrq_io->Set_error_file(stderr); -#endif - error_string = sformatf( "\nERROR: Cannot open file, %s.\n", name); + error_string = sformatf( "\nERROR: Cannot open file, %s.\n", name.c_str()); screen_msg(error_string); error_flush(); batch = FALSE; @@ -852,92 +618,10 @@ open_output_stream(char *query, char *default_name, std::ios_base::openmode mode } break; } - strncpy(default_name, name, MAX_LENGTH); + default_name = name; if (!batch ) { -#ifdef ERROR_OSTREAM phrq_io->Set_error_ostream(error_ostream_save); -#else - phrq_io->Set_error_file(error_file_save); -#endif } return (new_stream); } -#ifdef SKIP -/* ---------------------------------------------------------------------- */ -std::ofstream * Phreeqc:: -open_output_file(char *query, char *default_name, std::ios_base::openmode mode, bool batch) -/* ---------------------------------------------------------------------- */ -{ - char name[MAX_LENGTH]; - std::ofstream *new_stream; - int l; -#ifdef ERROR_OSTREAM - std::ostream * error_ostream_save = phrq_io->Get_error_ostream(); -#else - FILE * error_file_save = phrq_io->Get_error_file(); -#endif - - - for (;;) - { -/* - * Get file name - */ - strcpy(name, default_name); - if (!batch ) - { -#ifdef ERROR_OSTREAM - phrq_io->Set_error_ostream(&std::cerr); -#else - phrq_io->Set_error_file(stderr); -#endif - screen_msg(sformatf("%s\n", query)); - if (default_name[0] != '\0') - { - screen_msg(sformatf("Default: %s\n", default_name)); - } - char *s_ptr = fgets(name, MAX_LENGTH, stdin); - if (s_ptr == NULL) - { - std::cerr << "Failed defining name." << std::endl; - } - - l = (int) strlen(name); - name[l - 1] = '\0'; - if (name[0] == '\0') - { - strcpy(name, default_name); - } - } -/* - * Open existing file to read - */ - new_stream = new std::ofstream(name, mode); - if (new_stream == NULL || !new_stream->is_open()) - { -#ifdef ERROR_OSTREAM - phrq_io->Set_error_ostream(&std::cerr); -#else - phrq_io->Set_error_file(stderr); -#endif - error_string = sformatf( "\nERROR: Cannot open file, %s.\n", name); - screen_msg(error_string); - error_flush(); - batch = FALSE; - continue; - } - break; - } - strncpy(default_name, name, MAX_LENGTH); - if (!batch ) - { -#ifdef ERROR_OSTREAM - phrq_io->Set_error_ostream(error_ostream_save); -#else - phrq_io->Set_error_file(error_file_save); -#endif - } - return (new_stream); -} -#endif diff --git a/inverse.cpp b/inverse.cpp index f140974d..437d4607 100644 --- a/inverse.cpp +++ b/inverse.cpp @@ -4042,9 +4042,8 @@ void Phreeqc:: dump_netpath(struct inverse *inverse_ptr) /* ---------------------------------------------------------------------- */ { - int j; std::string string; - const char* cptr; + //const char* cptr; if (inverse_ptr->netpath == NULL) return; @@ -4076,21 +4075,16 @@ dump_netpath(struct inverse *inverse_ptr) { if (it->second.Get_n_user() < 0) continue; - - /* flags and description */ - char * description = string_duplicate(it->second.Get_description().c_str()); - cptr = description; - j = copy_token(string, &cptr); - if (j != EMPTY) + if (it->second.Get_description().size() > 0) { - string = sformatf("%s", description); + string = it->second.Get_description(); } else { string = sformatf("Solution %d", it->second.Get_n_user()); } fprintf(netpath_file, "4020%s\n", string.c_str()); - description = (char *) free_check_null(description); + //description = (char *) free_check_null(description); /* lat/lon */ fprintf(netpath_file, " # Lat/lon\n"); @@ -4397,7 +4391,6 @@ dump_netpath_pat(struct inverse *inv_ptr) cxxSolution *solution_ptr, *solution_ptr_orig; struct master *master_ptr; LDBLE d1, d2, d3; - const char* cptr; LDBLE sum, sum1, sum_iso, d; std::vector array_save, l_delta_save; int count_unknowns_save, max_row_count_save, max_column_count_save, temp, @@ -4516,10 +4509,8 @@ dump_netpath_pat(struct inverse *inv_ptr) solution_ptr = Utilities::Rxn_find(Rxn_solution_map, -7); /* Header */ - char * description = string_duplicate(solution_ptr_orig->Get_description().c_str()); - cptr = description; std::string string; - if (copy_token(string, &cptr) != EMPTY) + if (solution_ptr_orig->Get_description().size() > 0) { fprintf(netpath_file, "%d. %s\n", count_inverse_models, solution_ptr_orig->Get_description().c_str()); @@ -4529,7 +4520,6 @@ dump_netpath_pat(struct inverse *inv_ptr) fprintf(netpath_file, "%d. Solution %d\n", count_inverse_models, solution_ptr_orig->Get_n_user()); } - description = (char *) free_check_null(description); /* bookkeeping */ count_pat_solutions++; diff --git a/read.cpp b/read.cpp index 88f35496..ea7fe554 100644 --- a/read.cpp +++ b/read.cpp @@ -257,14 +257,12 @@ read_input(void) #if defined(SWIG_SHARED_OBJ) warning_msg("DATABASE keyword is ignored by IPhreeqc."); #else - - user_database = (char *) free_check_null(user_database); - user_database = string_duplicate(cptr); - if (string_trim(user_database) == EMPTY) + user_database = cptr; + string_trim(user_database); + if (user_database.size() == 0) { error_msg("DATABASE file name is missing.", CONTINUE); input_error++; - user_database = (char *) free_check_null(user_database); } first_read_input = FALSE; #endif diff --git a/structures.cpp b/structures.cpp index 501be22c..6a0e6c75 100644 --- a/structures.cpp +++ b/structures.cpp @@ -221,7 +221,6 @@ clean_up(void) line = (char *) free_check_null(line); line_save = (char *) free_check_null(line_save); /* free user database name if defined */ - user_database = (char *) free_check_null(user_database); dump_file_name = (char *) free_check_null(dump_file_name); #ifdef PHREEQCI_GUI free_spread(); @@ -230,7 +229,6 @@ clean_up(void) last_title_x.clear(); count_inverse = 0; - default_data_base = (char *) free_check_null(default_data_base); sformatf_buffer = (char *) free_check_null(sformatf_buffer); return (OK); } From 157a45844663d3a3b19ec814ef9898136a16b82e Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Fri, 26 Mar 2021 22:30:28 -0600 Subject: [PATCH 15/53] description_x --- Phreeqc.cpp | 7 ------- Phreeqc.h | 8 ++++---- kinetics.cpp | 17 ++++++----------- mainsubs.cpp | 5 ++--- prep.cpp | 3 +-- structures.cpp | 4 ---- 6 files changed, 13 insertions(+), 31 deletions(-) diff --git a/Phreeqc.cpp b/Phreeqc.cpp index 699a05da..d10ebfc7 100644 --- a/Phreeqc.cpp +++ b/Phreeqc.cpp @@ -542,7 +542,6 @@ void Phreeqc::init(void) *---------------------------------------------------------------------- */ title_x = NULL; new_x = FALSE; - description_x = NULL; tc_x = 0; tk_x = 0; patm_x = 1; @@ -563,7 +562,6 @@ void Phreeqc::init(void) mass_water_aq_x = 0; mass_water_surfaces_x = 0; mass_water_bulk_x = 0; - units_x = NULL; // auto pe_x // auto isotopes_x // auto default_pe_x @@ -654,8 +652,6 @@ void Phreeqc::init(void) /*---------------------------------------------------------------------- * Species *---------------------------------------------------------------------- */ - moles_per_kilogram_string= NULL; - pe_string = NULL; s_h2o = NULL; s_hplus = NULL; s_h3oplus = NULL; @@ -1352,7 +1348,6 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) mass_water_aq_x = 0; mass_water_surfaces_x = 0; mass_water_bulk_x = 0; - units_x = NULL; */ // auto pe_x // auto isotopes_x @@ -1494,8 +1489,6 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) logk = NULL; count_logk = 0; max_logk = MAX_S; - moles_per_kilogram_string= NULL; - pe_string = NULL; s = NULL; count_s = 0; max_s = MAX_S; diff --git a/Phreeqc.h b/Phreeqc.h index aaf4770b..e9152e1b 100644 --- a/Phreeqc.h +++ b/Phreeqc.h @@ -1289,7 +1289,7 @@ protected: char* title_x; std::string last_title_x; int new_x; - char* description_x; + std::string description_x; LDBLE tc_x; LDBLE tk_x; LDBLE patm_x; @@ -1310,7 +1310,7 @@ protected: LDBLE mass_water_aq_x; LDBLE mass_water_surfaces_x; LDBLE mass_water_bulk_x; - char* units_x; + std::string units_x; std::map < std::string, cxxChemRxn > pe_x; std::map isotopes_x; std::string default_pe_x; @@ -1404,8 +1404,8 @@ protected: *---------------------------------------------------------------------- */ std::vector logk; - char* moles_per_kilogram_string; - char* pe_string; + std::string moles_per_kilogram_string; + std::string pe_string; std::vector s; std::vector< std::map < std::string, cxxSpeciesDL > > s_diff_layer; diff --git a/kinetics.cpp b/kinetics.cpp index 82d0f620..89162e98 100644 --- a/kinetics.cpp +++ b/kinetics.cpp @@ -186,14 +186,12 @@ RESTART: // if limiting rates, jump to here } else { - char * temp_name = string_duplicate(name.c_str()); - const char* ptr = temp_name; + const char* ptr = name.c_str(); if (get_elts_in_species(&ptr, coef * coef1) == ERROR) { - error_string = sformatf("Error in -formula: %s", temp_name); + error_string = sformatf("Error in -formula: %s", name.c_str()); error_msg(error_string, CONTINUE); } - free_check_null(temp_name); } } if (use.Get_exchange_ptr() != NULL @@ -210,14 +208,13 @@ RESTART: // if limiting rates, jump to here name.c_str()) == 0) { /* found kinetics component */ - char * formula = string_duplicate(exchange_ptr->Get_exchange_comps()[j].Get_formula().c_str()); - const char* ptr = formula; + std::string formula = string_duplicate(exchange_ptr->Get_exchange_comps()[j].Get_formula().c_str()); + const char* ptr = formula.c_str(); if (get_elts_in_species(&ptr, -coef*exchange_ptr->Get_exchange_comps()[j].Get_phase_proportion()) == ERROR) { error_string = sformatf("Error in -formula: %s", formula); error_msg(error_string, CONTINUE); } - free_check_null(formula); } } } @@ -235,14 +232,13 @@ RESTART: // if limiting rates, jump to here surface_comp_ptr->Get_rate_name().c_str()) == 0) { /* found kinetics component */ - char * temp_formula = string_duplicate(surface_comp_ptr->Get_formula().c_str()); - const char* cptr = temp_formula; + std::string temp_formula = string_duplicate(surface_comp_ptr->Get_formula().c_str()); + const char* cptr = temp_formula.c_str(); /* Surface = 0 when m becomes low ... */ if (0.9 * surface_comp_ptr->Get_phase_proportion() * (kinetics_comp_ptr->Get_m()) < MIN_RELATED_SURFACE) { - //master_ptr = master_bsearch(ptr); master_ptr = master_bsearch(surface_comp_ptr->Get_master_element().c_str()); if (master_ptr != NULL) { @@ -257,7 +253,6 @@ RESTART: // if limiting rates, jump to here error_msg(error_string, CONTINUE); } } - free_check_null(temp_formula); } } } diff --git a/mainsubs.cpp b/mainsubs.cpp index 5c2b7423..eda33c41 100644 --- a/mainsubs.cpp +++ b/mainsubs.cpp @@ -25,7 +25,7 @@ initialize(void) /* * Initialize global variables */ - moles_per_kilogram_string = string_duplicate("Mol/kgw"); + moles_per_kilogram_string = "Mol/kgw"; pe_string = string_duplicate("pe"); /* * Allocate space @@ -935,8 +935,7 @@ saver(void) if (save.solution == TRUE) { sprintf(token, "Solution after simulation %d.", simulation); - description_x = (char *) free_check_null(description_x); - description_x = string_duplicate(token); + description_x = token; n = save.n_solution_user; xsolution_save(n); for (i = save.n_solution_user + 1; i <= save.n_solution_user_end; i++) diff --git a/prep.cpp b/prep.cpp index 33aaeae6..64fd70d2 100644 --- a/prep.cpp +++ b/prep.cpp @@ -47,8 +47,7 @@ prep(void) STOP); return ERROR; } - description_x = (char *) free_check_null(description_x); - description_x = string_duplicate(solution_ptr->Get_description().c_str()); + description_x = solution_ptr->Get_description(); /* * Allocate space for unknowns * Must allocate all necessary space before pointers to diff --git a/structures.cpp b/structures.cpp index 6a0e6c75..98ee01b0 100644 --- a/structures.cpp +++ b/structures.cpp @@ -37,11 +37,7 @@ clean_up(void) #endif #endif - description_x = (char*)free_check_null(description_x); isotopes_x.clear(); - moles_per_kilogram_string = - (char*)free_check_null(moles_per_kilogram_string); - pe_string = (char*)free_check_null(pe_string); /* model */ last_model.gas_phase.clear(); last_model.pp_assemblage.clear(); From 76366a6a5002b357100084d32a2a9c58a6d6bef3 Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Sat, 27 Mar 2021 08:22:22 -0600 Subject: [PATCH 16/53] fixed processing file names --- class_main.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/class_main.cpp b/class_main.cpp index 9506e128..0b42e769 100644 --- a/class_main.cpp +++ b/class_main.cpp @@ -535,9 +535,10 @@ open_input_stream(std::string query, std::string& default_name, std::ios_base::o screen_msg(sformatf("Default: %s\n", default_name.c_str())); } std::getline(std::cin, name); - if (name.size() == 0) + if (name.size() == 0 && default_name.size() == 0) { - std::cerr << "Failed defining name." << std::endl; + std::cerr << "No name defined." << std::endl; + continue; } if (name.size() == 0) { @@ -594,9 +595,9 @@ open_output_stream(std::string query, std::string& default_name, std::ios_base:: screen_msg(sformatf("Default: %s\n", default_name.c_str())); } std::getline(std::cin, name); - if (name.size() == 0) + if (name.size() == 0 && default_name.size() == 0) { - std::cerr << "Failed defining name." << std::endl; + std::cerr << "No name defined." << std::endl; } if (name.size() == 0) { From d7e3be4c209d8ed18246c428582ffd10c32673fa Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Sat, 27 Mar 2021 09:52:31 -0600 Subject: [PATCH 17/53] cleaned up some string_duplicate --- Phreeqc.cpp | 15 +++++---------- Phreeqc.h | 1 - kinetics.cpp | 4 ++-- mainsubs.cpp | 1 - parse.cpp | 5 +---- step.cpp | 8 ++------ structures.cpp | 9 ++------- tally.cpp | 9 ++------- 8 files changed, 14 insertions(+), 38 deletions(-) diff --git a/Phreeqc.cpp b/Phreeqc.cpp index d10ebfc7..d7fb6f59 100644 --- a/Phreeqc.cpp +++ b/Phreeqc.cpp @@ -1026,9 +1026,9 @@ void Phreeqc::init(void) forward_output_to_log = 0; /* phreeqc_files.cpp ------------------------------- */ #ifdef NPP - default_data_base = string_duplicate("c:\\phreeqc\\database\\phreeqc.dat"); + default_data_base = "c:\\phreeqc\\database\\phreeqc.dat"; #else - default_data_base = string_duplicate("phreeqc.dat"); + default_data_base = "phreeqc.dat"; #endif /* Pitzer */ pitzer_model = FALSE; @@ -1328,7 +1328,6 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) last_title_x = pSrc->last_title_x; /* new_x = FALSE; - description_x = NULL; tc_x = 0; tk_x = 0; patm_x = 1; @@ -1551,11 +1550,9 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) { count_elts = 0; paren_count = 0; - char * string = string_duplicate(s_ptr->mole_balance); - const char* ptr = string; - get_secondary_in_species(&ptr, 1.0); + const char* cptr = s_ptr->mole_balance; + get_secondary_in_species(&cptr, 1.0); s_ptr->next_secondary = elt_list_save(); - free_check_null(string); } //next_sys_total s_ptr->next_sys_total = NULL; @@ -1961,9 +1958,7 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) master_isotope_ptr->master = NULL; if (pSrc->master_isotope[i]->master) { - char * name = string_duplicate(pSrc->master_isotope[i]->master->elt->name); - master_isotope_ptr->master = master_search(name, &n); - free_check_null(name); + master_isotope_ptr->master = master_search(pSrc->master_isotope[i]->master->elt->name, &n); } if (master_isotope_ptr->master == NULL) { diff --git a/Phreeqc.h b/Phreeqc.h index e9152e1b..13984df9 100644 --- a/Phreeqc.h +++ b/Phreeqc.h @@ -1405,7 +1405,6 @@ protected: std::vector logk; std::string moles_per_kilogram_string; - std::string pe_string; std::vector s; std::vector< std::map < std::string, cxxSpeciesDL > > s_diff_layer; diff --git a/kinetics.cpp b/kinetics.cpp index 89162e98..ce88ae4e 100644 --- a/kinetics.cpp +++ b/kinetics.cpp @@ -208,7 +208,7 @@ RESTART: // if limiting rates, jump to here name.c_str()) == 0) { /* found kinetics component */ - std::string formula = string_duplicate(exchange_ptr->Get_exchange_comps()[j].Get_formula().c_str()); + std::string formula = exchange_ptr->Get_exchange_comps()[j].Get_formula().c_str(); const char* ptr = formula.c_str(); if (get_elts_in_species(&ptr, -coef*exchange_ptr->Get_exchange_comps()[j].Get_phase_proportion()) == ERROR) { @@ -232,7 +232,7 @@ RESTART: // if limiting rates, jump to here surface_comp_ptr->Get_rate_name().c_str()) == 0) { /* found kinetics component */ - std::string temp_formula = string_duplicate(surface_comp_ptr->Get_formula().c_str()); + std::string temp_formula = surface_comp_ptr->Get_formula().c_str(); const char* cptr = temp_formula.c_str(); /* Surface = 0 when m becomes low ... */ diff --git a/mainsubs.cpp b/mainsubs.cpp index eda33c41..b79f646f 100644 --- a/mainsubs.cpp +++ b/mainsubs.cpp @@ -26,7 +26,6 @@ initialize(void) * Initialize global variables */ moles_per_kilogram_string = "Mol/kgw"; - pe_string = string_duplicate("pe"); /* * Allocate space */ diff --git a/parse.cpp b/parse.cpp index 70ff78d8..91de8805 100644 --- a/parse.cpp +++ b/parse.cpp @@ -213,14 +213,11 @@ check_eqn(int association) for (i = 0; i < count_trxn; i++) { sumcharge += (trxn.token[i].coef) * (trxn.token[i].z); - char * temp_name = string_duplicate(trxn.token[i].name); - const char *t_ptr = temp_name; + const char* t_ptr = trxn.token[i].name; if (get_elts_in_species(&t_ptr, trxn.token[i].coef) == ERROR) { - free_check_null(temp_name); return (ERROR); } - free_check_null(temp_name); } /* * Sort elements in reaction and combine diff --git a/step.cpp b/step.cpp index f78be7d2..8fff4880 100644 --- a/step.cpp +++ b/step.cpp @@ -964,10 +964,8 @@ reaction_calc(cxxReaction *reaction_ptr) } else { - char * token = string_duplicate(it->first.c_str()); - cptr = token; + cptr = it->first.c_str(); get_elts_in_species(&cptr, coef); - free_check_null(token); } } /* @@ -1089,11 +1087,9 @@ add_ss_assemblage(cxxSSassemblage *ss_assemblage_ptr) comp_ptr->Set_delta(0.0); if (comp_ptr->Get_moles() > 0.0) { - char * token = string_duplicate(phase_ptr->formula); - cptr = &(token[0]); + cptr = phase_ptr->formula; count_elts = 0; // appt get_elts_in_species(&cptr, 1.0); - free_check_null(token); for (k = 0; k < count_elts; k++) { master_ptr = elt_list[k].elt->primary; diff --git a/structures.cpp b/structures.cpp index 98ee01b0..6afd18ca 100644 --- a/structures.cpp +++ b/structures.cpp @@ -891,13 +891,10 @@ master_bsearch(const char* cptr) sizeof(struct master *), master_compare_string); if (void_ptr == NULL) { - char * dup = string_duplicate(cptr); - replace("(+","(", dup); - void_ptr = bsearch((const char *) dup, + void_ptr = bsearch(cptr, (char*)&master[0], master.size(), sizeof(struct master*), master_compare_string); - dup = (char *) free_check_null(dup); } if (void_ptr == NULL) { @@ -949,10 +946,8 @@ master_bsearch_primary(const char* cptr) /* * Find element name */ - char * temp_name = string_duplicate(cptr); - cptr1 = temp_name; + cptr1 = cptr; get_elt(&cptr1, elt, &l); - free_check_null(temp_name); /* * Search master species list */ diff --git a/tally.cpp b/tally.cpp index fa186793..461f833c 100644 --- a/tally.cpp +++ b/tally.cpp @@ -1024,10 +1024,8 @@ build_tally_table(void) { std::string name = it->first; LDBLE coef = it->second; - char * temp_name = string_duplicate(name.c_str()); - cptr = temp_name; + cptr = name.c_str(); get_elts_in_species(&cptr, 1.0 * coef); - free_check_null(temp_name); } } elt_list_combine(); @@ -1189,11 +1187,8 @@ calc_dummy_kinetic_reaction_tally(cxxKinetics *kinetics_ptr) cxxNameDouble::iterator it = kinetics_comp_ptr->Get_namecoef().begin(); for ( ; it != kinetics_comp_ptr->Get_namecoef().end(); it++) { - std::string name = it->first; - char * temp_name = string_duplicate(name.c_str()); - cptr = temp_name; + cptr = it->first.c_str(); get_elts_in_species(&cptr, coef); - free_check_null(temp_name); } } } From 82a10d6e4fd483961db4c8ba66436ec11dd1cc9e Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Sat, 27 Mar 2021 14:00:45 -0600 Subject: [PATCH 18/53] revised get_elt and get_token --- Phreeqc.h | 4 +- basicsubs.cpp | 2 +- parse.cpp | 34 ++++++++--------- read.cpp | 34 ++++++++--------- structures.cpp | 17 +++++---- tidy.cpp | 14 +++---- transport.cpp | 9 +++-- utilities.cpp | 100 ++++++++++++++++++++++--------------------------- 8 files changed, 104 insertions(+), 110 deletions(-) diff --git a/Phreeqc.h b/Phreeqc.h index 13984df9..5fb1ad2b 100644 --- a/Phreeqc.h +++ b/Phreeqc.h @@ -474,7 +474,7 @@ public: // parse.cpp ------------------------------- int check_eqn(int association); int get_charge(char* charge, LDBLE* z); - int get_elt(const char** t_ptr, char* element, int* i); + int get_elt(const char** t_ptr, std::string& element, int* i); int get_elts_in_species(const char** t_ptr, LDBLE coef); int get_num(const char** t_ptr, LDBLE* num); int get_secondary_in_species(const char** t_ptr, LDBLE coef); @@ -1056,7 +1056,7 @@ public: public: void* free_check_null(void* ptr); protected: - int get_token(const char** eqnaddr, char* string, LDBLE* z, int* l); + int get_token(const char** eqnaddr, std::string& string, LDBLE* z, int* l); int islegit(const char c); public: void malloc_error(void); diff --git a/basicsubs.cpp b/basicsubs.cpp index cefc1cc0..084ba22a 100644 --- a/basicsubs.cpp +++ b/basicsubs.cpp @@ -1968,7 +1968,6 @@ match_elts_in_species(const char *name, const char *mytemplate) char c, c1; const char* cptr, *ptr1; LDBLE d; - char element[MAX_LENGTH]; char token[MAX_LENGTH], equal_list[MAX_LENGTH]; char token1[MAX_LENGTH], template1[MAX_LENGTH], equal_list1[MAX_LENGTH]; char str[2]; @@ -2008,6 +2007,7 @@ match_elts_in_species(const char *name, const char *mytemplate) /* * Get new element and subscript */ + std::string element; if (get_elt(&cptr, element, &l) == ERROR) { return (ERROR); diff --git a/parse.cpp b/parse.cpp index 91de8805..63883fce 100644 --- a/parse.cpp +++ b/parse.cpp @@ -485,7 +485,7 @@ get_coef(LDBLE * coef, const char **eqnaddr) /* ---------------------------------------------------------------------- */ int Phreeqc:: -get_elt(const char **t_ptr, char *element, int *i) +get_elt(const char** t_ptr, std::string& element, int* i) /* ---------------------------------------------------------------------- */ /* * Function reads an element name out of the equation string. @@ -501,29 +501,30 @@ get_elt(const char **t_ptr, char *element, int *i) { char c; + element.clear(); c = *(*t_ptr)++; if (c == '\0') { error_string = sformatf( - "Empty string in get_elt. Expected an element name."); + "Empty string in get_elt. Expected an element name."); error_msg(error_string, CONTINUE); return (ERROR); } -/* - * Load name into char array element - */ - element[0] = c; + /* + * Load name into char array element + */ + element.push_back(c); *i = 1; if (c == '[') { while ((c = (**t_ptr)) != ']') { - element[*i] = c; + element.push_back(c); (*i)++; (*t_ptr)++; if ((c = (**t_ptr)) == ']') { - element[*i] = c; + element.push_back(c); (*i)++; (*t_ptr)++; break; @@ -535,23 +536,22 @@ get_elt(const char **t_ptr, char *element, int *i) break; } } - while (islower((int) (c = (**t_ptr))) || c == '_') + while (islower((int)(c = (**t_ptr))) || c == '_') { - element[*i] = c; + element.push_back(c); (*i)++; (*t_ptr)++; } } else { - while (islower((int) (c = (**t_ptr))) || c == '_') + while (islower((int)(c = (**t_ptr))) || c == '_') { - element[*i] = c; + element.push_back(c); (*i)++; (*t_ptr)++; } } - element[*i] = '\0'; return (OK); } @@ -574,7 +574,7 @@ get_elts_in_species(const char **t_ptr, LDBLE coef) int i, count, l; char c, c1; LDBLE d; - char element[MAX_LENGTH]; + std::string element; const char** t_ptr_save = t_ptr; while (((c = **t_ptr) != '+') && (c != '-') && (c != '\0')) { @@ -606,7 +606,7 @@ get_elts_in_species(const char **t_ptr, LDBLE coef) { elt_list.resize((size_t)count_elts + 1); } - elt_list[count_elts].elt = element_store(element); + elt_list[count_elts].elt = element_store(element.c_str()); if (get_num(t_ptr, &d) == ERROR) { return (ERROR); @@ -1017,7 +1017,7 @@ get_species(const char **cptr) * output, points to the next character after the species charge. * */ - char string[MAX_LENGTH]; + std::string string; int l; if ((size_t) count_trxn + 1 > trxn.token.size()) @@ -1032,7 +1032,7 @@ get_species(const char **cptr) { return (ERROR); } - trxn.token[count_trxn].name = string_hsave(string); + trxn.token[count_trxn].name = string_hsave(string.c_str()); /* trxn.token[count_trxn].z = 0; trxn.token[count_trxn].s = NULL; diff --git a/read.cpp b/read.cpp index ea7fe554..cbfef00a 100644 --- a/read.cpp +++ b/read.cpp @@ -1049,14 +1049,12 @@ read_exchange(void) * save formula for adjusting number of exchange sites */ cptr = formula; - char *name = string_duplicate(token.c_str()); - name[0] = '\0'; + std::string name; LDBLE z; int l; get_token(&cptr, name, &z, &l); comp_ptr->Set_formula_z(z); free_check_null(formula); - free_check_null(name); /* * Save elt_list */ @@ -1085,7 +1083,7 @@ read_exchange_master_species(void) LDBLE l_z; struct element *elts_ptr; struct species *s_ptr; - char token[MAX_LENGTH], token1[MAX_LENGTH]; + char token[MAX_LENGTH]; for (;;) { j = check_line("Exchange species equation", FALSE, TRUE, TRUE, TRUE); @@ -1152,8 +1150,9 @@ read_exchange_master_species(void) else { cptr1 = token; + std::string token1; get_token(&cptr1, token1, &l_z, &l); - master[count_master]->s = s_store(token1, l_z, FALSE); + master[count_master]->s = s_store(token1.c_str(), l_z, FALSE); } /* * MAKE LISTS OF PRIMARY AND SECONDARY MASTER SPECIES @@ -3169,7 +3168,7 @@ read_master_species(void) LDBLE l_z; struct element *elts_ptr; struct species *s_ptr; - char token[MAX_LENGTH], token1[MAX_LENGTH]; + char token[MAX_LENGTH]; elts_ptr = NULL; for (;;) @@ -3240,8 +3239,9 @@ read_master_species(void) else { cptr1 = token; + std::string token1; get_token(&cptr1, token1, &l_z, &l); - master[count_master]->s = s_store(token1, l_z, FALSE); + master[count_master]->s = s_store(token1.c_str(), l_z, FALSE); } std::string sname = token; @@ -6848,9 +6848,7 @@ read_surface(void) */ cptr1 = formula; int l; - // name is work space - char * name = string_duplicate(formula); - name[0] = '\0'; + std::string name; get_token(&cptr1, name, &dummy, &l); comp_ptr->Set_formula_z(dummy); cxxNameDouble nd = elt_list_NameDouble(); @@ -6861,16 +6859,18 @@ read_surface(void) cptr1 = formula; get_elt(&cptr1, name, &l); { - char* ptr = strchr(name, '_'); - if (ptr != NULL) - ptr[0] = '\0'; + std::string::size_type pos = name.find('_'); + if (pos != std::string::npos) + { + name = name.substr(0, pos); + } } charge_ptr = temp_surface.Find_charge(name); formula = (char*)free_check_null(formula); if (charge_ptr == NULL) { cxxSurfaceCharge temp_charge(this->phrq_io); - temp_charge.Set_name(name); + temp_charge.Set_name(name.c_str()); if (comp_ptr->Get_phase_name().size() == 0 && comp_ptr->Get_rate_name().size() == 0) { @@ -6885,8 +6885,7 @@ read_surface(void) temp_surface.Get_surface_charges().push_back(temp_charge); charge_ptr = temp_surface.Find_charge(name); } - comp_ptr->Set_charge_name(name); - name = (char*)free_check_null(name); + comp_ptr->Set_charge_name(name.c_str()); /* * Read surface area (m2/g) */ @@ -7136,8 +7135,9 @@ read_surface_master_species(void) else { cptr1 = token; + std::string token1; get_token(&cptr1, token1, &l_z, &l); - master[count_master]->s = s_store(token1, l_z, FALSE); + master[count_master]->s = s_store(token1.c_str(), l_z, FALSE); } master[count_master]->primary = TRUE; strcpy(token, master[count_master]->elt->name); diff --git a/structures.cpp b/structures.cpp index 6afd18ca..c3678dc2 100644 --- a/structures.cpp +++ b/structures.cpp @@ -947,11 +947,14 @@ master_bsearch_primary(const char* cptr) * Find element name */ cptr1 = cptr; - get_elt(&cptr1, elt, &l); -/* - * Search master species list - */ - master_ptr_primary = master_bsearch(elt); + { + std::string elt; + get_elt(&cptr1, elt, &l); + /* + * Search master species list + */ + master_ptr_primary = master_bsearch(elt.c_str()); + } if (master_ptr_primary == NULL) { input_error++; @@ -972,7 +975,7 @@ master_bsearch_secondary(const char* cptr) */ int l; const char* cptr1; - char elt[MAX_LENGTH]; + std::string elt; struct master *master_ptr_primary, *master_ptr=NULL, *master_ptr_secondary=NULL; int j; /* @@ -983,7 +986,7 @@ master_bsearch_secondary(const char* cptr) /* * Search master species list */ - master_ptr_primary = master_bsearch(elt); + master_ptr_primary = master_bsearch(elt.c_str()); if (master_ptr_primary == NULL) { input_error++; diff --git a/tidy.cpp b/tidy.cpp index 36c3ed97..e1e3e609 100644 --- a/tidy.cpp +++ b/tidy.cpp @@ -664,7 +664,7 @@ coef_in_master(struct master * master_ptr) int l; LDBLE coef; const char* cptr; - char elt_name[MAX_LENGTH]; + std::string elt_name; struct elt_list *next_elt; coef = 0.0; @@ -675,7 +675,7 @@ coef_in_master(struct master * master_ptr) for (next_elt = master_ptr->s->next_elt; next_elt->elt != NULL; next_elt++) { - if (strcmp(elt_name, next_elt->elt->name) == 0) + if (strcmp(elt_name.c_str(), next_elt->elt->name) == 0) { coef = next_elt->coef; break; @@ -2902,15 +2902,15 @@ phase_rxn_to_trxn(struct phase *phase_ptr, struct reaction *rxn_ptr) */ int i, l; const char* cptr; - char token[MAX_LENGTH]; LDBLE l_z; trxn.token[0].name = phase_ptr->formula; /* charge */ - char * temp_formula = string_duplicate(phase_ptr->formula); - cptr = temp_formula; - get_token(&cptr, token, &l_z, &l); - free_check_null(temp_formula); + cptr = phase_ptr->formula; + { + std::string token; + get_token(&cptr, token, &l_z, &l); + } trxn.token[0].z = l_z; trxn.token[0].s = NULL; trxn.token[0].unknown = NULL; diff --git a/transport.cpp b/transport.cpp index 709d09ce..4f0fa901 100644 --- a/transport.cpp +++ b/transport.cpp @@ -1678,7 +1678,7 @@ set_initial_moles(int i) /* ---------------------------------------------------------------------- */ { cxxKinetics *kinetics_ptr; - char token[MAX_LENGTH], token1[MAX_LENGTH]; + char token[MAX_LENGTH]; const char* cptr; int j, k, l; /* @@ -1768,8 +1768,11 @@ set_initial_moles(int i) get_elts_in_species(&cptr, 2e-10); cptr = token; LDBLE z; - get_token(&cptr, token1, &z, &l); - comp.Set_formula(token1); + { + std::string token1; + get_token(&cptr, token1, &z, &l); + comp.Set_formula(token1.c_str()); + } comp.Set_formula_z(z); comp.Set_totals(elt_list_NameDouble()); comp.Set_charge_balance(0.0); diff --git a/utilities.cpp b/utilities.cpp index 6010b0c1..c022f811 100644 --- a/utilities.cpp +++ b/utilities.cpp @@ -532,7 +532,7 @@ free_check_null(void *ptr) /* ---------------------------------------------------------------------- */ int Phreeqc:: -get_token(const char **eqnaddr, char *string, LDBLE * l_z, int *l) +get_token(const char** eqnaddr, std::string& string, LDBLE* l_z, int* l) /* ---------------------------------------------------------------------- */ /* * Function finds next species in equation, coefficient has already @@ -554,18 +554,20 @@ get_token(const char **eqnaddr, char *string, LDBLE * l_z, int *l) int i, j; int ltoken, lcharge; char c; - const char* cptr, *ptr1, *rest; + const char* cptr, * ptr1, * rest; char charge[MAX_LENGTH]; + string.clear(); rest = *eqnaddr; cptr = *eqnaddr; i = 0; -/* - * Find end of token or begining of charge - */ + /* + * Find end of token or begining of charge + */ while (((c = *cptr) != '+') && (c != '-') && (c != '=') && (c != '\0')) { - string[i++] = c; + string.push_back(c); + i++; if (c == '[') { cptr++; @@ -574,48 +576,34 @@ get_token(const char **eqnaddr, char *string, LDBLE * l_z, int *l) if (c == '\0') { error_string = sformatf( - "No final bracket \"]\" for element name, %s.", - string); + "No final bracket \"]\" for element name, %s.", + string); error_msg(error_string, CONTINUE); return (ERROR); } - string[i++] = c; - if (i >= MAX_LENGTH) - { - output_msg(sformatf( - "Species name greater than MAX_LENGTH (%d) characters.\n%s\n", - MAX_LENGTH, string)); - return (ERROR); - } + string.push_back(c); + i++; cptr++; } - string[i++] = c; + string.push_back(c); + i++; } - /* check for overflow of space */ - if (i >= MAX_LENGTH) - { - output_msg(sformatf( - "Species name greater than MAX_LENGTH (%d) characters.\n%s\n", - MAX_LENGTH, string)); - return (ERROR); - } cptr++; } - string[i] = '\0'; ltoken = i; -/* - * Check for an empty string - */ + /* + * Check for an empty string + */ if (i == 0) { - error_string = sformatf( "NULL string detected in get_token, %s.", rest); + error_string = sformatf("NULL string detected in get_token, %s.", rest); error_msg(error_string, CONTINUE); return (ERROR); } -/* - * End of token is = or \0, charge is zero - */ + /* + * End of token is = or \0, charge is zero + */ if (c == '=' || c == '\0') { *eqnaddr = cptr; @@ -624,30 +612,30 @@ get_token(const char **eqnaddr, char *string, LDBLE * l_z, int *l) } else { -/* - * Copy characters into charge until next species or end is detected - */ + /* + * Copy characters into charge until next species or end is detected + */ j = 0; ptr1 = cptr; - while ((isalpha((int) (c = *ptr1)) == FALSE) && - (c != '(') && - (c != ')') && - (c != ']') && (c != '[') && (c != '=') && (c != '\0')) + while ((isalpha((int)(c = *ptr1)) == FALSE) && + (c != '(') && + (c != ')') && + (c != ']') && (c != '[') && (c != '=') && (c != '\0')) { charge[j++] = c; /* error if no more space */ if (j >= MAX_LENGTH) { error_msg("The charge on a species has exceeded MAX_LENGTH characters.", - CONTINUE); + CONTINUE); return (ERROR); } ptr1++; } -/* - * Go back to last + or - if not end of side, - * everything before the last + or - in charge is part of the charge - */ + /* + * Go back to last + or - if not end of side, + * everything before the last + or - in charge is part of the charge + */ if ((c != '=') && (c != '\0')) { while (((c = *ptr1) != '+') && (c != '-')) @@ -659,12 +647,12 @@ get_token(const char **eqnaddr, char *string, LDBLE * l_z, int *l) charge[j] = '\0'; lcharge = j; *eqnaddr = ptr1; -/* - * Charge has been written, now need to check if charge has legal format - */ + /* + * Charge has been written, now need to check if charge has legal format + */ if (get_charge(charge, l_z) == OK) { - strcat(string, charge); + string.append(charge); } else { @@ -747,8 +735,8 @@ parse_couple(char *token) */ int e1, e2, p1, p2; const char* cptr; - char elt1[MAX_LENGTH], elt2[MAX_LENGTH], paren1[MAX_LENGTH], - paren2[MAX_LENGTH]; + std::string elt1, elt2; + char paren1[MAX_LENGTH], paren2[MAX_LENGTH]; if (strcmp_nocase_arg1(token, "pe") == 0) { @@ -800,7 +788,7 @@ parse_couple(char *token) } cptr++; get_elt(&cptr, elt2, &e2); - if (strcmp(elt1, elt2) != 0) + if (strcmp(elt1.c_str(), elt2.c_str()) != 0) { error_string = sformatf( "Redox couple must be two redox states " "of the same element, %s.", token); @@ -840,18 +828,18 @@ parse_couple(char *token) paren2[p2] = '\0'; if (strcmp(paren1, paren2) < 0) { - strcpy(token, elt1); + strcpy(token, elt1.c_str()); strcat(token, paren1); strcat(token, "/"); - strcat(token, elt2); + strcat(token, elt2.c_str()); strcat(token, paren2); } else if (strcmp(paren1, paren2) > 0) { - strcpy(token, elt2); + strcpy(token, elt2.c_str()); strcat(token, paren2); strcat(token, "/"); - strcat(token, elt1); + strcat(token, elt1.c_str()); strcat(token, paren1); } else From 16fd18fe5eb4b8fc630143e8d67e7d3fd5bc5261 Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Sat, 27 Mar 2021 15:26:55 -0600 Subject: [PATCH 19/53] removed string_duplicate from prep.cpp --- prep.cpp | 107 +++++++++++-------------------------------------------- read.cpp | 22 +++++------- 2 files changed, 29 insertions(+), 100 deletions(-) diff --git a/prep.cpp b/prep.cpp index 64fd70d2..0bf606ac 100644 --- a/prep.cpp +++ b/prep.cpp @@ -304,28 +304,6 @@ quick_setup(void) cxxSurfaceCharge *charge_ptr = use.Get_surface_ptr()->Find_charge(x[i]->surface_charge); x[i]->related_moles = charge_ptr->Get_grams(); x[i]->mass_water = charge_ptr->Get_mass_water(); -#ifdef DEBUG - /* test that charge and surface match */ - cxxSurfaceComp *comp_ptr = use.Get_surface_ptr()->Find_comp(x[i]->surface_comp); - char * temp_formula = string_duplicate(comp_ptr->Get_formula().c_str()); - const char* ptr = temp_formula; - copy_token(token, &ptr, &l); - const char* ptr1 = token; - get_elt(&ptr1, name, &l); - ptr1 = strchr(name, '_'); - if (ptr1 != NULL) - ptr1[0] = '\0'; - if (strcmp(name, charge_ptr->Get_name().c_str()) != 0) - { - free_check_null(temp_formula); - error_string = sformatf( - "Internal error: Surface charge name %s does not match surface component name %s\nTry alphabetical order for surfaces in SURFACE", - charge_ptr->Get_name().c_str(), - comp_ptr->Get_formula().c_str()); - error_msg(error_string, STOP); - } - free_check_null(temp_formula); -#endif /* moles picked up from master->total */ } else if (x[i]->type == SURFACE_CB1 || x[i]->type == SURFACE_CB2) @@ -758,10 +736,8 @@ build_ss_assemblage(void) */ count_elts = 0; paren_count = 0; - char * token = string_duplicate(x[i]->phase->formula); - cptr = token; + cptr = x[i]->phase->formula; get_elts_in_species(&cptr, 1.0); - free_check_null(token); /* * Go through elements in phase */ @@ -1437,17 +1413,13 @@ build_pure_phases(void) cxxPPassemblageComp * comp_ptr = (cxxPPassemblageComp *) x[i]->pp_assemblage_comp_ptr; if (comp_ptr->Get_add_formula().size() > 0) { - char * char_name = string_duplicate(comp_ptr->Get_add_formula().c_str()); - cptr = char_name; + cptr = comp_ptr->Get_add_formula().c_str(); get_elts_in_species(&cptr, 1.0); - free_check_null(char_name); } else { - char * char_name = string_duplicate(x[i]->phase->formula); - cptr = char_name; + cptr = x[i]->phase->formula; get_elts_in_species(&cptr, 1.0); - free_check_null(char_name); } /* * Go through elements in phase @@ -1887,11 +1859,9 @@ convert_units(cxxSolution *solution_ptr) } else { - char * temp_desc = string_duplicate(comp_ref.Get_description().c_str()); - const char* cptr = temp_desc; + const char* cptr = comp_ref.Get_description().c_str(); copy_token(token, &cptr); master_ptr = master_bsearch(token.c_str()); - free_check_null(temp_desc); if (master_ptr != NULL) { /* use gfw for element redox state */ @@ -2997,10 +2967,8 @@ add_surface_charge_balance(void) /* * Include charge balance in list for mass-balance equations */ - char * temp_name = string_duplicate(master_ptr->elt->name); - cptr = temp_name; + cptr = master_ptr->elt->name; get_secondary_in_species(&cptr, 1.0); - free_check_null(temp_name); return (OK); } @@ -3060,10 +3028,8 @@ add_cd_music_charge_balances(int n) * Include charge balance in list for mass-balance equations */ { - char * temp_name = string_duplicate( master_ptr->elt->name); - const char* cptr = temp_name; + const char* cptr = master_ptr->elt->name; get_secondary_in_species(&cptr, s[n]->dz[0]); - free_check_null(temp_name); } /* * Find potential unknown for plane 1 @@ -3075,10 +3041,8 @@ add_cd_music_charge_balances(int n) * Include charge balance in list for mass-balance equations */ { - char * temp_name = string_duplicate( master_ptr->elt->name); - const char* cptr = temp_name; + const char* cptr = master_ptr->elt->name; get_secondary_in_species(&cptr, s[n]->dz[1]); - free_check_null(temp_name); } /* * Find potential unknown for plane 2 @@ -3090,10 +3054,8 @@ add_cd_music_charge_balances(int n) * Include charge balance in list for mass-balance equations */ { - char * temp_name = string_duplicate(master_ptr->elt->name); - const char* cptr = temp_name; + const char* cptr = master_ptr->elt->name; get_secondary_in_species(&cptr, s[n]->dz[2]); - free_check_null(temp_name); } return (OK); @@ -4221,8 +4183,7 @@ setup_solution(void) comp_it = solution_ptr->Get_initial_data()->Get_comps().find(it->first.c_str()); comp_ptr = &(comp_it->second); } - char * temp_desc = string_duplicate(it->first.c_str()); - cptr = temp_desc; + cptr = it->first.c_str(); copy_token(token, &cptr); master_ptr = master_bsearch(token.c_str()); /* @@ -4232,7 +4193,6 @@ setup_solution(void) { if (strcmp(token.c_str(), "H(1)") != 0 && strcmp(token.c_str(), "E") != 0) { - free_check_null(temp_desc); continue; } } @@ -4246,7 +4206,6 @@ setup_solution(void) "Master species not in database for %s, skipping element.", it->first.c_str()); warning_msg(error_string); - free_check_null(temp_desc); continue; } if (master_ptr->type != AQ) @@ -4256,7 +4215,6 @@ setup_solution(void) "Only aqueous concentrations are allowed in solution data, ignoring %s.", it->first.c_str()); warning_msg(error_string); - free_check_null(temp_desc); continue; } /* @@ -4285,9 +4243,7 @@ setup_solution(void) /* * Set pointers */ - free_check_null(temp_desc); - temp_desc = string_duplicate(it->first.c_str()); - cptr = temp_desc; + cptr = it->first.c_str(); copy_token(token, &cptr); Utilities::str_tolower(token); if (strstr(token.c_str(), "alk") != NULL) @@ -4339,14 +4295,12 @@ setup_solution(void) input_error++; } } - free_check_null(temp_desc); /* * Charge balance unknown */ if (comp_ptr && comp_ptr->Get_equation_name().size() > 0) { - char * temp_eq_name = string_duplicate(comp_ptr->Get_equation_name().c_str()); - cptr = temp_eq_name; + cptr = comp_ptr->Get_equation_name().c_str(); copy_token(token, &cptr); Utilities::str_tolower(token); if (strstr(token.c_str(), "charge") != NULL) @@ -4392,7 +4346,6 @@ setup_solution(void) solution_phase_boundary_unknown = x[count_unknowns]; } } - free_check_null(temp_eq_name); } count_unknowns++; } @@ -5129,10 +5082,8 @@ write_mb_eqn_x(void) for (i = 1; i < count_trxn; i++) { j = count_elts; - char * temp_name = string_duplicate(trxn.token[i].s->name); - const char* cptr = temp_name; + const char* cptr = trxn.token[i].s->name; get_elts_in_species(&cptr, trxn.token[i].coef); - free_check_null(temp_name); for (k = j; k < count_elts; k++) { if (trxn.token[i].s->secondary != NULL) @@ -5151,17 +5102,13 @@ write_mb_eqn_x(void) } if (trxn.token[i].s->secondary == NULL) { - char * temp_name = string_duplicate(trxn.token[i].s->primary->elt->name); - const char* cptr = temp_name; + const char* cptr = trxn.token[i].s->primary->elt->name; get_secondary_in_species(&cptr, trxn.token[i].coef); - free_check_null(temp_name); } else { - char * temp_name = string_duplicate(trxn.token[i].s->secondary->elt->name); - cptr = temp_name; + cptr = trxn.token[i].s->secondary->elt->name; get_secondary_in_species(&cptr, trxn.token[i].coef); - free_check_null(temp_name); } } elt_list_combine(); @@ -5192,22 +5139,18 @@ write_mb_for_species_list(int n) { if (trxn.token[i].s->secondary == NULL) { - char * temp_name = string_duplicate(trxn.token[i].s->primary->elt->name); - const char* cptr = temp_name; + const char* cptr = trxn.token[i].s->primary->elt->name; get_secondary_in_species(&cptr, trxn.token[i].coef); - free_check_null(temp_name); } else { - char * temp_name = string_duplicate(trxn.token[i].s->secondary->elt->name); - const char* cptr = temp_name; + const char* cptr = trxn.token[i].s->secondary->elt->name; if (get_secondary_in_species(&cptr, trxn.token[i].coef) == ERROR) { input_error++; error_string = sformatf( "Error parsing %s.", trxn.token[i].s->secondary->elt->name); error_msg(error_string, CONTINUE); } - free_check_null(temp_name); } } for (i = 0; i < count_elts; i++) @@ -5254,17 +5197,13 @@ write_phase_sys_total(int n) { if (trxn.token[i].s->secondary == NULL) { - char * temp_name = string_duplicate(trxn.token[i].s->primary->elt->name); - const char* cptr = temp_name; + const char* cptr = trxn.token[i].s->primary->elt->name; get_secondary_in_species(&cptr, trxn.token[i].coef); - free_check_null(temp_name); } else { - char * temp_name = string_duplicate(trxn.token[i].s->secondary->elt->name); - const char* cptr = temp_name; + const char* cptr = trxn.token[i].s->secondary->elt->name; get_secondary_in_species(&cptr, trxn.token[i].coef); - free_check_null(temp_name); } } for (i = 0; i < count_elts; i++) @@ -6051,10 +5990,8 @@ build_min_exch(void) count_elts = 0; paren_count = 0; { - char * formula = string_duplicate(comp_ref.Get_formula().c_str()); - const char* cptr = formula; + const char* cptr = comp_ref.Get_formula().c_str(); get_elts_in_species(&cptr, 1.0); - free_check_null(formula); } #ifdef COMBINE change_hydrogen_in_elt_list(0); @@ -6181,14 +6118,10 @@ build_min_surface(void) -comp_ptr->Get_formula_z() * comp_ptr->Get_phase_proportion()); count_elts = 0; paren_count = 0; - - // { /* Add specified formula for all types of surfaces */ - char * formula = string_duplicate(comp_ptr->Get_formula().c_str()); - const char* cptr1 = formula; + const char* cptr1 = comp_ptr->Get_formula().c_str(); get_elts_in_species(&cptr1, 1.0); - free_check_null(formula); } #ifdef COMBINE change_hydrogen_in_elt_list(0); diff --git a/read.cpp b/read.cpp index cbfef00a..c33f8dc8 100644 --- a/read.cpp +++ b/read.cpp @@ -1041,20 +1041,19 @@ read_exchange(void) */ count_elts = 0; paren_count = 0; - char * formula = string_duplicate(token.c_str()); - cptr = formula; + std::string formula = token.c_str(); + cptr = formula.c_str(); get_elts_in_species(&cptr, conc); /* * save formula for adjusting number of exchange sites */ - cptr = formula; + cptr = formula.c_str(); std::string name; LDBLE z; int l; get_token(&cptr, name, &z, &l); comp_ptr->Set_formula_z(z); - free_check_null(formula); /* * Save elt_list */ @@ -5186,8 +5185,8 @@ read_solution(void) temp_isotope.Set_isotope_name(token.c_str()); /* read and save element name */ { - char *temp_iso_name = string_duplicate(token.c_str()); - const char* cptr1 = temp_iso_name; + std::string temp_iso_name = token.c_str(); + const char* cptr1 = temp_iso_name.c_str(); get_num(&cptr1, &dummy); temp_isotope.Set_isotope_number(dummy); if (cptr1[0] == '\0' || isupper((int) cptr1[0]) == FALSE) @@ -5195,11 +5194,9 @@ read_solution(void) error_msg("Expecting element name.", PHRQ_io::OT_CONTINUE); error_msg(line_save, PHRQ_io::OT_CONTINUE); input_error++; - temp_iso_name = (char*)free_check_null(temp_iso_name); return (CParser::PARSER_ERROR); } temp_isotope.Set_elt_name(cptr1); - temp_iso_name = (char*)free_check_null(temp_iso_name); } /* read and store isotope ratio */ if (copy_token(token, &next_char) != CParser::TT_DIGIT) @@ -6840,13 +6837,13 @@ read_surface(void) */ count_elts = 0; paren_count = 0; - char * formula = string_duplicate(token.c_str()); - cptr1 = formula; + std::string formula = token.c_str(); + cptr1 = formula.c_str(); get_elts_in_species(&cptr1, conc); /* * save formula for adjusting number of exchange sites */ - cptr1 = formula; + cptr1 = formula.c_str(); int l; std::string name; get_token(&cptr1, name, &dummy, &l); @@ -6856,7 +6853,7 @@ read_surface(void) /* * Search for charge structure */ - cptr1 = formula; + cptr1 = formula.c_str(); get_elt(&cptr1, name, &l); { std::string::size_type pos = name.find('_'); @@ -6866,7 +6863,6 @@ read_surface(void) } } charge_ptr = temp_surface.Find_charge(name); - formula = (char*)free_check_null(formula); if (charge_ptr == NULL) { cxxSurfaceCharge temp_charge(this->phrq_io); From d575ade3ebe84d22299477437c6ec799406397d9 Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Sat, 27 Mar 2021 16:23:42 -0600 Subject: [PATCH 20/53] tidy.cpp, title_x --- Phreeqc.cpp | 4 --- Phreeqc.h | 2 +- mainsubs.cpp | 6 ++-- read.cpp | 28 +++++------------ structures.cpp | 3 +- tidy.cpp | 85 +++++++++++++------------------------------------- 6 files changed, 34 insertions(+), 94 deletions(-) diff --git a/Phreeqc.cpp b/Phreeqc.cpp index d7fb6f59..b53ce5ba 100644 --- a/Phreeqc.cpp +++ b/Phreeqc.cpp @@ -540,7 +540,6 @@ void Phreeqc::init(void) /*---------------------------------------------------------------------- * Global solution *---------------------------------------------------------------------- */ - title_x = NULL; new_x = FALSE; tc_x = 0; tk_x = 0; @@ -1322,9 +1321,6 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) /*---------------------------------------------------------------------- * Global solution *---------------------------------------------------------------------- */ - /* - title_x = NULL; - */ last_title_x = pSrc->last_title_x; /* new_x = FALSE; diff --git a/Phreeqc.h b/Phreeqc.h index 5fb1ad2b..18cbdd1a 100644 --- a/Phreeqc.h +++ b/Phreeqc.h @@ -1286,7 +1286,7 @@ protected: /*---------------------------------------------------------------------- * Global solution *---------------------------------------------------------------------- */ - char* title_x; + std::string title_x; std::string last_title_x; int new_x; std::string description_x; diff --git a/mainsubs.cpp b/mainsubs.cpp index b79f646f..22a7aea8 100644 --- a/mainsubs.cpp +++ b/mainsubs.cpp @@ -2102,12 +2102,14 @@ run_simulations(void) if (read_input() == EOF) break; - if (title_x != NULL) + if (title_x.size() > 0) { sprintf(token, "TITLE"); dup_print(token, TRUE); if (pr.headings == TRUE) - output_msg(sformatf( "%s\n\n", title_x)); + { + output_msg(sformatf("%s\n\n", title_x.c_str())); + } } tidy_model(); /* diff --git a/read.cpp b/read.cpp index c33f8dc8..40770625 100644 --- a/read.cpp +++ b/read.cpp @@ -67,7 +67,7 @@ read_input(void) save.surface = FALSE; save.gas_phase = FALSE; save.ss_assemblage = FALSE; - title_x = (char *) free_check_null(title_x); + title_x.clear(); while ((i = check_line("Subroutine Read", FALSE, TRUE, TRUE, TRUE)) != KEYWORD) { @@ -7246,7 +7246,7 @@ read_title(void) * */ const char* cptr, *cptr1; - int l, title_x_length, line_length; + int l; int return_value; char token[MAX_LENGTH]; /* @@ -7255,17 +7255,10 @@ read_title(void) cptr = line; copy_token(token, &cptr, &l); cptr1 = cptr; - title_x = (char *) free_check_null(title_x); + title_x.clear(); if (copy_token(token, &cptr, &l) != EMPTY) { - title_x = string_duplicate(cptr1); - } - else - { - title_x = (char *) PHRQ_malloc(sizeof(char)); - if (title_x == NULL) - malloc_error(); - title_x[0] = '\0'; + title_x = cptr1; } /* @@ -7280,18 +7273,11 @@ read_title(void) /* * append line to title_x */ - title_x_length = (int) strlen(title_x); - line_length = (int) strlen(line); - title_x = (char *) PHRQ_realloc(title_x, - ((size_t)title_x_length + (size_t)line_length + 2) * sizeof(char)); - if (title_x == NULL) - malloc_error(); - if (title_x_length > 0) + if (title_x.size() > 0) { - title_x[title_x_length] = '\n'; - title_x[title_x_length + 1] = '\0'; + title_x.append("\n"); } - strcat(title_x, line); + title_x.append(line); } last_title_x = title_x; return (return_value); diff --git a/structures.cpp b/structures.cpp index c3678dc2..6a62da2b 100644 --- a/structures.cpp +++ b/structures.cpp @@ -221,7 +221,7 @@ clean_up(void) #ifdef PHREEQCI_GUI free_spread(); #endif - title_x = (char *) free_check_null(title_x); + title_x.clear(); last_title_x.clear(); count_inverse = 0; @@ -941,7 +941,6 @@ master_bsearch_primary(const char* cptr) */ int l; const char* cptr1; - char elt[MAX_LENGTH]; struct master *master_ptr_primary; /* * Find element name diff --git a/tidy.cpp b/tidy.cpp index e1e3e609..45ae189b 100644 --- a/tidy.cpp +++ b/tidy.cpp @@ -668,10 +668,8 @@ coef_in_master(struct master * master_ptr) struct elt_list *next_elt; coef = 0.0; - char * temp_name = string_duplicate(master_ptr->elt->name); - cptr = temp_name; + cptr = master_ptr->elt->name; get_elt(&cptr, elt_name, &l); - free_check_null(temp_name); for (next_elt = master_ptr->s->next_elt; next_elt->elt != NULL; next_elt++) { @@ -1615,10 +1613,8 @@ tidy_pp_assemblage(void) it->second.Set_add_formula(phase_ptr->formula); } { - char * temp_add = string_duplicate(it->second.Get_add_formula().c_str()); - cptr = temp_add; + cptr = it->second.Get_add_formula().c_str(); get_elts_in_species(&cptr, coef); - free_check_null(temp_add); } /* check that all elements are in the database */ for (int l = first; l < count_elts; l++) @@ -2331,8 +2327,7 @@ tidy_species(void) } for (i = 0; i < (int)master.size(); i++) { - char * temp_name = string_duplicate(master[i]->elt->name); - cptr = temp_name; + cptr = master[i]->elt->name; if (cptr[0] != '[') { while ((c = (int) *(++cptr)) != '\0') @@ -2348,7 +2343,6 @@ tidy_species(void) } } } - free_check_null(temp_name); /* store sequence number in master structure */ master[i]->number = i; if (strcmp(master[i]->elt->name, "Alkalinity") != 0) @@ -2715,10 +2709,8 @@ tidy_surface(void) count_elts = 0; paren_count = 0; { - char * temp_formula = string_duplicate(comp_ptr->Get_formula().c_str()); - cptr1 = temp_formula; + cptr1 = comp_ptr->Get_formula().c_str(); get_elts_in_species(&cptr1, comp_ptr->Get_moles()); - free_check_null(temp_formula); } { cxxNameDouble nd = elt_list_NameDouble(); @@ -3209,10 +3201,8 @@ tidy_kin_exchange(void) count_elts = 0; paren_count = 0; { - char * temp_formula = string_duplicate(comp_ref.Get_formula().c_str()); - cptr = temp_formula; + cptr = comp_ref.Get_formula().c_str(); get_elts_in_species(&cptr, conc); - free_check_null(temp_formula); } comp_ref.Set_totals(elt_list_NameDouble()); /* @@ -3322,10 +3312,8 @@ update_kin_exchange(void) count_elts = 0; paren_count = 0; { - char* temp_formula = string_duplicate(comp_ref.Get_formula().c_str()); - cptr = temp_formula; + cptr = comp_ref.Get_formula().c_str(); get_elts_in_species(&cptr, 1.0); - free_check_null(temp_formula); } cxxNameDouble nd_formula = elt_list_NameDouble(); double comp_coef = 0; @@ -3345,10 +3333,8 @@ update_kin_exchange(void) count_elts = 0; paren_count = 0; { - char* temp_formula = string_duplicate(comp_ref.Get_formula().c_str()); - cptr = temp_formula; + cptr = comp_ref.Get_formula().c_str(); get_elts_in_species(&cptr, conc); - free_check_null(temp_formula); } comp_ref.Set_totals(elt_list_NameDouble()); } @@ -3461,10 +3447,8 @@ tidy_min_exchange(void) count_elts = 0; paren_count = 0; { - char * temp_formula = string_duplicate(comp_ref.Get_formula().c_str()); - cptr = temp_formula; + cptr = comp_ref.Get_formula().c_str(); get_elts_in_species(&cptr, conc); - free_check_null(temp_formula); } comp_ref.Set_totals(elt_list_NameDouble()); /* @@ -3473,19 +3457,15 @@ tidy_min_exchange(void) count_elts = 0; paren_count = 0; { - char * temp_formula = string_duplicate(comp_ref.Get_formula().c_str()); - cptr = temp_formula; + cptr = comp_ref.Get_formula().c_str(); get_elts_in_species(&cptr, -comp_ref.Get_phase_proportion()); - free_check_null(temp_formula); } int l; struct phase *phase_ptr = phase_bsearch(jit->first.c_str(), &l, FALSE); if (phase_ptr != NULL) { - char * temp_formula = string_duplicate(phase_ptr->formula); - cptr = temp_formula; + cptr = phase_ptr->formula; get_elts_in_species(&cptr, 1.0); - free_check_null(temp_formula); } else { @@ -3616,10 +3596,8 @@ update_min_exchange(void) count_elts = 0; paren_count = 0; { - char* temp_formula = string_duplicate(comp_ref.Get_formula().c_str()); - cptr = temp_formula; + cptr = comp_ref.Get_formula().c_str(); get_elts_in_species(&cptr, 1.0); - free_check_null(temp_formula); } cxxNameDouble nd_formula = elt_list_NameDouble(); double comp_coef = 0; @@ -3639,10 +3617,8 @@ update_min_exchange(void) count_elts = 0; paren_count = 0; { - char* temp_formula = string_duplicate(comp_ref.Get_formula().c_str()); - cptr = temp_formula; + cptr = comp_ref.Get_formula().c_str(); get_elts_in_species(&cptr, conc); - free_check_null(temp_formula); } comp_ref.Set_totals(elt_list_NameDouble()); /* @@ -3651,19 +3627,15 @@ update_min_exchange(void) count_elts = 0; paren_count = 0; { - char* temp_formula = string_duplicate(comp_ref.Get_formula().c_str()); - cptr = temp_formula; + cptr = comp_ref.Get_formula().c_str(); get_elts_in_species(&cptr, -comp_ref.Get_phase_proportion()); - free_check_null(temp_formula); } int l; struct phase* phase_ptr = phase_bsearch(jit->first.c_str(), &l, FALSE); if (phase_ptr != NULL) { - char* temp_formula = string_duplicate(phase_ptr->formula); - cptr = temp_formula; + cptr = phase_ptr->formula; get_elts_in_species(&cptr, 1.0); - free_check_null(temp_formula); } else { @@ -3841,10 +3813,8 @@ tidy_min_surface(void) count_elts = 0; paren_count = 0; { - char * temp_formula = string_duplicate(phase_ptr->formula); - const char* cptr = temp_formula; + const char* cptr = phase_ptr->formula; get_elts_in_species(&cptr, 1.0); - free_check_null(temp_formula); } // Revise logic for surface related to mineral for (size_t jj = 0; jj < surface_ptr->Get_surface_comps().size(); jj++) @@ -3852,10 +3822,8 @@ tidy_min_surface(void) cxxSurfaceComp *comp_jj_ptr = &(surface_ptr->Get_surface_comps()[jj]); // Use formula for all types of surfaces { - char * temp_formula = string_duplicate(comp_jj_ptr->Get_formula().c_str()); - const char* cptr = temp_formula; - get_elts_in_species(&cptr, - -comp_jj_ptr->Get_phase_proportion()); + const char* cptr = comp_jj_ptr->Get_formula().c_str(); + get_elts_in_species(&cptr, -comp_jj_ptr->Get_phase_proportion()); if (surface_ptr->Get_type() != cxxSurface::CD_MUSIC) { @@ -3868,7 +3836,6 @@ tidy_min_surface(void) error_string = sformatf("Unknown element definition in SURFACE \n\t for surface related to equilibrium_phase: SURFACE %d.", surface_ptr->Get_n_user()); error_msg(error_string); - free_check_null(temp_formula); continue; } if (elt_ptr->master->s == NULL || elt_ptr->master->s->name == NULL) @@ -3877,7 +3844,6 @@ tidy_min_surface(void) error_string = sformatf("Unknown master species definition in SURFACE \n\t for surface related to equilibrium_phase: SURFACE %d.", surface_ptr->Get_n_user()); error_msg(error_string); - free_check_null(temp_formula); continue; } //if (strcmp(elt_ptr->master->s->name, temp_formula) != 0) @@ -3894,7 +3860,6 @@ tidy_min_surface(void) warning_msg(error_string); } } - free_check_null(temp_formula); } } elt_list_combine(); @@ -4197,12 +4162,10 @@ tidy_kin_surface(void) /* if (conc < MIN_RELATED_SURFACE) conc = 0.0; */ { - char * temp_formula = string_duplicate(comp_ptr->Get_formula().c_str()); - const char* cptr = temp_formula; + const char* cptr = comp_ptr->Get_formula().c_str(); count_elts = 0; paren_count = 0; get_elts_in_species(&cptr, conc); - free_check_null(temp_formula); } { if (surface_ptr->Get_new_def()) @@ -4259,10 +4222,8 @@ tidy_kin_surface(void) } else { - char * temp_name = string_duplicate(name.c_str()); - const char* cptr = temp_name; + const char* cptr = name.c_str(); get_elts_in_species(&cptr, coef); - free_check_null(temp_name); } } /* save kinetics formula */ @@ -4287,10 +4248,8 @@ tidy_kin_surface(void) (comp_ptr->Get_rate_name().c_str(), kin_comp_ptr->Get_rate_name().c_str()) == 0) { - char * temp_formula = string_duplicate( comp_ptr->Get_formula().c_str()); - const char* cptr = temp_formula; + const char* cptr = comp_ptr->Get_formula().c_str(); get_elts_in_species(&cptr, -1 * comp_ptr->Get_phase_proportion()); - free_check_null(temp_formula); } } elt_list_combine(); @@ -4477,12 +4436,10 @@ update_kin_surface(void) } else /* need to generate from scratch */ { - char* temp_formula = string_duplicate(comp_ptr->Get_formula().c_str()); - const char* cptr = temp_formula; + const char* cptr = comp_ptr->Get_formula().c_str(); count_elts = 0; paren_count = 0; get_elts_in_species(&cptr, conc); - free_check_null(temp_formula); cxxNameDouble nd = elt_list_NameDouble(); comp_ptr->Set_totals(nd); From 89ab28d76b9e2467ef44a0f54813ec85581c28cb Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Sun, 28 Mar 2021 20:56:22 -0600 Subject: [PATCH 21/53] vector inverse elts --- Phreeqc.h | 3 ++ global_structures.h | 19 +++---- inverse.cpp | 13 +---- read.cpp | 121 ++++++++++++++++++++++++++++++++------------ structures.cpp | 53 +++---------------- tidy.cpp | 62 ++++++++--------------- 6 files changed, 130 insertions(+), 141 deletions(-) diff --git a/Phreeqc.h b/Phreeqc.h index 18cbdd1a..c7c92f9b 100644 --- a/Phreeqc.h +++ b/Phreeqc.h @@ -691,6 +691,9 @@ public: int read_inv_phases(struct inverse* inverse_ptr, const char* next_char); int read_kinetics(void); LDBLE* read_list_doubles(const char** ptr, int* count_doubles); + bool read_vector_doubles(const char** ptr, std::vector& v); + bool read_vector_ints(const char** cptr, std::vector& v, int positive); + bool read_vector_t_f(const char** ptr, std::vector& v); int* read_list_ints(const char** ptr, int* count_ints, int positive); int* read_list_t_f(const char** ptr, int* count_ints); int read_master_species(void); diff --git a/global_structures.h b/global_structures.h index 80c58635..73564ede 100644 --- a/global_structures.h +++ b/global_structures.h @@ -318,21 +318,18 @@ struct inverse LDBLE range_max; LDBLE tolerance; LDBLE mp_tolerance; - int count_uncertainties; - LDBLE *uncertainties; - int count_ph_uncertainties; - LDBLE *ph_uncertainties; + std::vector uncertainties; + std::vector ph_uncertainties; LDBLE water_uncertainty; int mineral_water; int carbon; - LDBLE *dalk_dph; - LDBLE *dalk_dc; - int count_solns; - int *solns; - int count_force_solns; - int *force_solns; + std::vector dalk_dph; + std::vector dalk_dc; + size_t count_solns; + std::vector solns; + std::vector force_solns; int count_elts; - struct inv_elts *elts; + std::vector elts; int count_phases; struct inv_phases *phases; int count_master_list; diff --git a/inverse.cpp b/inverse.cpp index 437d4607..c24e9878 100644 --- a/inverse.cpp +++ b/inverse.cpp @@ -3229,17 +3229,8 @@ carbon_derivs(struct inverse *inv_ptr) LDBLE c_uncertainty, d_carbon, alk_plus, alk_minus; cxxSolution *solution_ptr_orig, *solution_ptr; - inv_ptr->dalk_dph = (LDBLE *) free_check_null(inv_ptr->dalk_dph); - inv_ptr->dalk_dph = - (LDBLE *) PHRQ_malloc((size_t) inv_ptr->count_solns * sizeof(LDBLE)); - if (inv_ptr->dalk_dph == NULL) - malloc_error(); - - inv_ptr->dalk_dc = (LDBLE *) free_check_null(inv_ptr->dalk_dc); - inv_ptr->dalk_dc = - (LDBLE *) PHRQ_malloc((size_t) inv_ptr->count_solns * sizeof(LDBLE)); - if (inv_ptr->dalk_dc == NULL) - malloc_error(); + inv_ptr->dalk_dph.resize(inv_ptr->count_solns); + inv_ptr->dalk_dc.resize(inv_ptr->count_solns); for (i = 0; i < inv_ptr->count_solns; i++) { diff --git a/read.cpp b/read.cpp index 40770625..cabc8bf2 100644 --- a/read.cpp +++ b/read.cpp @@ -1433,10 +1433,6 @@ read_inverse(void) inverse[n].tolerance = 1e-10; inverse[n].minimal = FALSE; inverse[n].description = description; - inverse[n].count_uncertainties = 1; - inverse[n].uncertainties[0] = 0.05; - inverse[n].count_ph_uncertainties = 1; - inverse[n].ph_uncertainties[0] = 0.05; inverse[n].water_uncertainty = 0.0; inverse[n].mineral_water = TRUE; inverse[n].mp = FALSE; @@ -1473,17 +1469,13 @@ read_inverse(void) break; case 0: /* solutions */ case 10: /* solution */ - inverse[n].solns = - read_list_ints(&next_char, &inverse[n].count_solns, TRUE); + read_vector_ints(&next_char, inverse[n].solns, TRUE); + inverse[n].count_solns = (int)inverse[n].solns.size(); opt_save = OPTION_ERROR; break; case 1: /* uncertainty */ case 2: /* uncertainties */ - inverse[n].uncertainties = - (LDBLE*)free_check_null(inverse[n].uncertainties); - inverse[n].uncertainties = - read_list_doubles(&next_char, - &inverse[n].count_uncertainties); + read_vector_doubles(&next_char, inverse[n].uncertainties); opt_save = OPTION_ERROR; break; case 3: /* balances */ @@ -1531,10 +1523,8 @@ read_inverse(void) case 16: /* force */ case 17: /* force_solution */ case 18: /* force_solutions */ - inverse[n].force_solns = - (int*)free_check_null(inverse[n].force_solns); - inverse[n].force_solns = - read_list_t_f(&next_char, &inverse[n].count_force_solns); + inverse[n].force_solns.clear(); + read_vector_t_f(&next_char, inverse[n].force_solns); opt_save = OPTION_ERROR; break; case 19: /* isotope values */ @@ -1603,11 +1593,8 @@ read_inverse(void) */ if (inverse[n].count_solns == 0) { - inverse[n].solns = (int *) PHRQ_malloc(2 * sizeof(int)); - if (inverse[n].solns == NULL) - malloc_error(); - inverse[n].solns[0] = 1; - inverse[n].solns[1] = 2; + inverse[n].solns.push_back(1); + inverse[n].solns.push_back(2); inverse[n].count_solns = 2; } /* @@ -1653,27 +1640,23 @@ read_inv_balances(struct inverse *inverse_ptr, const char* cptr) } else if (strcmp_nocase_arg1(token, "ph") != 0) { - inverse_ptr->elts = (struct inv_elts *) PHRQ_realloc(inverse_ptr->elts, - ((size_t)inverse_ptr->count_elts + 1) * sizeof(struct inv_elts)); - if (inverse_ptr->elts == NULL) - malloc_error(); + size_t count_elts = inverse_ptr->elts.size(); + inverse_ptr->elts.resize(count_elts + 1); replace("(+", "(", token); - inverse_ptr->elts[inverse_ptr->count_elts].name = string_hsave(token); + inverse_ptr->elts[count_elts].name = string_hsave(token); /* * Read element uncertainties */ - inverse_ptr->elts[inverse_ptr->count_elts].uncertainties = + inverse_ptr->elts[count_elts].uncertainties = read_list_doubles(&cptr, &count); - inverse_ptr->elts[inverse_ptr->count_elts].count_uncertainties = + inverse_ptr->elts[count_elts].count_uncertainties = count; inverse_ptr->count_elts++; } else if (strcmp_nocase_arg1(token, "ph") == 0) { - inverse_ptr->ph_uncertainties = - (LDBLE *) free_check_null(inverse_ptr->ph_uncertainties); - inverse_ptr->ph_uncertainties = read_list_doubles(&cptr, &count); - inverse_ptr->count_ph_uncertainties = count; + inverse_ptr->ph_uncertainties.clear(); + read_vector_doubles(&cptr, inverse_ptr->ph_uncertainties); } return (OK); } @@ -2477,7 +2460,23 @@ read_list_doubles(const char **cptr, int *count_doubles) } return (LDBLE_list); } - +/* ---------------------------------------------------------------------- */ +bool Phreeqc:: +read_vector_doubles(const char** cptr, std::vector& v) +/* ---------------------------------------------------------------------- */ +{ + /* + * Reads a list of LDBLE numbers until end of line is reached or + * a LDBLE cannot be read from a token. + */ + double value; + std::istringstream iss(*cptr); + while (iss >> value) + { + v.push_back(value); + } + return true; +} /* ---------------------------------------------------------------------- */ int * Phreeqc:: read_list_ints(const char **cptr, int *count_ints, int positive) @@ -2540,6 +2539,30 @@ read_list_ints(const char **cptr, int *count_ints, int positive) } return (int_list); } +/* ---------------------------------------------------------------------- */ +bool Phreeqc:: +read_vector_ints(const char** cptr, std::vector& v, int positive) +/* ---------------------------------------------------------------------- */ +{ + /* + * Reads a list of int numbers until end of line is reached or + * an int cannot be read from a token. + */ + int value; + std::istringstream iss(*cptr); + while (iss >> value) + { + v.push_back(value); + if (value <= 0 && positive == TRUE) + { + error_msg("Expected an integer greater than zero.", CONTINUE); + error_msg(line_save, CONTINUE); + input_error++; + return false; + } + } + return true; +} /* ---------------------------------------------------------------------- */ int * Phreeqc:: @@ -2709,6 +2732,40 @@ read_list_t_f(const char **cptr, int *count_ints) return (int_list); } +/* ---------------------------------------------------------------------- */ +bool Phreeqc:: +read_vector_t_f(const char** cptr, std::vector& v) +/* ---------------------------------------------------------------------- */ +{ + /* + * Reads a list of true and false until end of line is reached or + * until non- t or f is found + */ + std::string token; + while (copy_token(token, cptr) != EMPTY) + { + str_tolower(token); + if (token[0] == 't') + { + v.push_back(true); + } + else if (token[0] == 'f') + { + v.push_back(false); + } + else + { + error_msg("Expected TRUE or FALSE.", CONTINUE); + error_msg(line_save, CONTINUE); + input_error++; + return false; + } + + } + return true; +} + + /* ---------------------------------------------------------------------- */ int Phreeqc:: read_log_k_only(const char* cptr_in, LDBLE * log_k) diff --git a/structures.cpp b/structures.cpp index 6a62da2b..3fc2684b 100644 --- a/structures.cpp +++ b/structures.cpp @@ -533,48 +533,15 @@ inverse_alloc(void) * Initialize variables */ inverse_ptr->description = NULL; - inverse_ptr->count_uncertainties = 0; inverse_ptr->count_solns = 0; inverse_ptr->count_elts = 0; inverse_ptr->count_isotopes = 0; inverse_ptr->count_i_u = 0; inverse_ptr->count_phases = 0; - inverse_ptr->count_force_solns = 0; /* * allocate space for pointers in structure to NULL */ - - inverse_ptr->uncertainties = (LDBLE *) PHRQ_malloc(sizeof(LDBLE)); - if (inverse_ptr->uncertainties == NULL) - { - malloc_error(); - return inverse_ptr; - } - inverse_ptr->ph_uncertainties = (LDBLE *) PHRQ_malloc(sizeof(LDBLE)); - if (inverse_ptr->ph_uncertainties == NULL) - { - malloc_error(); - return inverse_ptr; - } - inverse_ptr->force_solns = (int *) PHRQ_malloc(sizeof(int)); - if (inverse_ptr->force_solns == NULL) - { - malloc_error(); - return inverse_ptr; - } - inverse_ptr->dalk_dph = NULL; - inverse_ptr->dalk_dc = NULL; - - inverse_ptr->solns = NULL; - - inverse_ptr->elts = (struct inv_elts *) PHRQ_malloc(sizeof(struct inv_elts)); - if (inverse_ptr->elts == NULL) - { - malloc_error(); - return inverse_ptr; - } - inverse_ptr->elts[0].name = NULL; - inverse_ptr->elts[0].uncertainties = NULL; + inverse_ptr->count_solns = 0; inverse_ptr->isotopes = (struct inv_isotope *) PHRQ_malloc( sizeof(struct inv_isotope)); @@ -657,17 +624,14 @@ inverse_free(struct inverse *inverse_ptr) inverse_ptr->description = (char *) free_check_null(inverse_ptr->description); /* Free solns */ - inverse_ptr->solns = (int *) free_check_null(inverse_ptr->solns); + inverse_ptr->solns.clear(); /* Free uncertainties */ - inverse_ptr->uncertainties = - (LDBLE *) free_check_null(inverse_ptr->uncertainties); - inverse_ptr->ph_uncertainties = - (LDBLE *) free_check_null(inverse_ptr->ph_uncertainties); + inverse_ptr->uncertainties.clear(); + inverse_ptr->ph_uncertainties.clear(); /* Free force_solns */ - inverse_ptr->force_solns = - (int *) free_check_null(inverse_ptr->force_solns); + inverse_ptr->force_solns.clear(); /* Free elts */ for (i = 0; i < inverse_ptr->count_elts; i++) @@ -675,8 +639,7 @@ inverse_free(struct inverse *inverse_ptr) inverse_ptr->elts[i].uncertainties = (LDBLE *) free_check_null(inverse_ptr->elts[i].uncertainties); }; - inverse_ptr->elts = - (struct inv_elts *) free_check_null(inverse_ptr->elts); + inverse_ptr->elts.clear(); /* Free isotopes */ for (i = 0; i < inverse_ptr->count_isotopes; i++) @@ -706,8 +669,8 @@ inverse_free(struct inverse *inverse_ptr) (struct inv_phases *) free_check_null(inverse_ptr->phases); /* Free carbon derivatives */ - inverse_ptr->dalk_dph = (LDBLE *) free_check_null(inverse_ptr->dalk_dph); - inverse_ptr->dalk_dc = (LDBLE *) free_check_null(inverse_ptr->dalk_dc); + inverse_ptr->dalk_dph.clear(); + inverse_ptr->dalk_dc.clear(); return (OK); } diff --git a/tidy.cpp b/tidy.cpp index 45ae189b..d9c3dad8 100644 --- a/tidy.cpp +++ b/tidy.cpp @@ -1102,7 +1102,6 @@ tidy_inverse(void) int i, j, k, l; int count_in; LDBLE value; - struct inv_elts *inv_elts; struct master *master_ptr; struct master *master_alk_ptr; struct elt_list *elt_list_ptr; @@ -1114,56 +1113,39 @@ tidy_inverse(void) /* * Set default uncertainties for all solutions, if necessary */ - if (inverse[i].count_uncertainties < inverse[i].count_solns) + if (inverse[i].uncertainties.size() < inverse[i].count_solns) { - inverse[i].uncertainties = - (LDBLE *) PHRQ_realloc(inverse[i].uncertainties, - (size_t) inverse[i].count_solns * - sizeof(LDBLE)); - if (inverse[i].uncertainties == NULL) - malloc_error(); - for (j = inverse[i].count_uncertainties; - j < inverse[i].count_solns; j++) + size_t count = inverse[i].uncertainties.size(); + double value = (count > 0) ? inverse[i].uncertainties.back() : 0.05; + inverse[i].uncertainties.resize(inverse[i].count_solns); + for (size_t j = count; j < inverse[i].count_solns; j++) { - inverse[i].uncertainties[j] = - inverse[i].uncertainties[inverse[i].count_uncertainties - - 1]; + inverse[i].uncertainties[j] = value; } } /* * Set default ph uncertainties for all solutions, if necessary */ - if (inverse[i].count_ph_uncertainties < inverse[i].count_solns) + if (inverse[i].ph_uncertainties.size() < inverse[i].count_solns) { - inverse[i].ph_uncertainties = - (LDBLE *) PHRQ_realloc(inverse[i].ph_uncertainties, - (size_t) inverse[i].count_solns * - sizeof(LDBLE)); - if (inverse[i].ph_uncertainties == NULL) - malloc_error(); - for (j = inverse[i].count_ph_uncertainties; - j < inverse[i].count_solns; j++) + size_t count = inverse[i].ph_uncertainties.size(); + double value = (count > 0) ? inverse[i].ph_uncertainties.back() : 0.05; + inverse[i].ph_uncertainties.resize(inverse[i].count_solns); + for (size_t j = count; j < inverse[i].count_solns; j++) { - inverse[i].ph_uncertainties[j] = - inverse[i].ph_uncertainties[inverse[i]. - count_ph_uncertainties - 1]; + inverse[i].ph_uncertainties[j] = value; } } /* * Set default force for all solutions */ - if (inverse[i].count_force_solns < inverse[i].count_solns) + if (inverse[i].force_solns.size() < inverse[i].count_solns) { - inverse[i].force_solns = - (int *) PHRQ_realloc(inverse[i].force_solns, - (size_t) inverse[i].count_solns * - sizeof(int)); - if (inverse[i].force_solns == NULL) - malloc_error(); - for (j = inverse[i].count_force_solns; j < inverse[i].count_solns; - j++) + size_t count = inverse[i].force_solns.size(); + inverse[i].force_solns.resize(inverse[i].count_solns); + for (size_t j = count; j < inverse[i].count_solns; j++) { - inverse[i].force_solns[j] = FALSE; + inverse[i].force_solns[j] = false; } } /* @@ -1362,11 +1344,8 @@ tidy_inverse(void) /* * Save list of master species in inv_elts structure */ - inv_elts = - (struct inv_elts *) PHRQ_malloc((size_t) (count_in) * - sizeof(struct inv_elts)); - if (inv_elts == NULL) - malloc_error(); + std::vector inv_elts; + inv_elts.resize((size_t)count_in); count_in = 0; for (j = 0; j < (int)master.size(); j++) { @@ -1464,8 +1443,7 @@ tidy_inverse(void) /* * replace elts in inverse struct */ - inverse[i].elts = - (struct inv_elts *) free_check_null(inverse[i].elts); + inverse[i].elts.clear(); inverse[i].elts = inv_elts; inverse[i].count_elts = count_in; for (j = 0; j < inverse[i].count_elts; j++) From d13bb764084c2a67f175c4184bf966a8e9993eae Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Sun, 28 Mar 2021 23:06:13 -0600 Subject: [PATCH 22/53] removed count_elts --- global_structures.h | 1 - inverse.cpp | 42 +++++++++++++++++++++--------------------- read.cpp | 1 - structures.cpp | 3 +-- tidy.cpp | 12 ++++++------ 5 files changed, 28 insertions(+), 31 deletions(-) diff --git a/global_structures.h b/global_structures.h index 73564ede..528eaff1 100644 --- a/global_structures.h +++ b/global_structures.h @@ -328,7 +328,6 @@ struct inverse size_t count_solns; std::vector solns; std::vector force_solns; - int count_elts; std::vector elts; int count_phases; struct inv_phases *phases; diff --git a/inverse.cpp b/inverse.cpp index c24e9878..ebfc83ee 100644 --- a/inverse.cpp +++ b/inverse.cpp @@ -212,7 +212,7 @@ setup_inverse(struct inverse *inv_ptr) /* * count unknowns */ - max_column_count = inv_ptr->count_elts * inv_ptr->count_solns + /* epsilons */ + max_column_count = inv_ptr->elts.size() * inv_ptr->count_solns + /* epsilons */ inv_ptr->count_solns + /* solutions */ inv_ptr->count_phases + /* phases */ inv_ptr->count_redox_rxns + /* redox reactions */ @@ -225,33 +225,33 @@ setup_inverse(struct inverse *inv_ptr) col_phases = inv_ptr->count_solns; col_redox = col_phases + inv_ptr->count_phases; col_epsilon = col_redox + inv_ptr->count_redox_rxns; - col_ph = col_epsilon + inv_ptr->count_elts * inv_ptr->count_solns; + col_ph = col_epsilon + inv_ptr->elts.size() * inv_ptr->count_solns; col_water = col_ph + carbon * inv_ptr->count_solns; col_isotopes = col_water + 1; col_phase_isotopes = col_isotopes + inv_ptr->count_isotope_unknowns * inv_ptr->count_solns; - max_row_count = inv_ptr->count_solns * inv_ptr->count_elts + /* optimize */ + max_row_count = inv_ptr->count_solns * inv_ptr->elts.size() + /* optimize */ carbon * inv_ptr->count_solns + /* optimize ph */ 1 + /* optimize water */ inv_ptr->count_solns * inv_ptr->count_isotope_unknowns + /* optimize isotopes */ inv_ptr->count_isotopes * inv_ptr->count_phases + /* optimize phase isotopes */ - inv_ptr->count_elts + /* mass balances */ + inv_ptr->elts.size() + /* mass balances */ 1 + 1 + /* fractions, init and final */ inv_ptr->count_solns + /* charge balances */ carbon * inv_ptr->count_solns + /* dAlk = dC + dph */ inv_ptr->count_isotopes + /* isotopes */ - 2 * inv_ptr->count_solns * inv_ptr->count_elts + /* epsilon constraints */ + 2 * inv_ptr->count_solns * inv_ptr->elts.size() + /* epsilon constraints */ 2 * carbon * inv_ptr->count_solns + /* epsilon on ph */ 2 + /* epsilon for water */ 2 * inv_ptr->count_isotope_unknowns * inv_ptr->count_solns + /* epsilon for isotopes */ 2 * inv_ptr->count_isotopes * inv_ptr->count_phases + /* epsilon for isotopes in phases */ 2; /* work space */ - row_mb = inv_ptr->count_solns * inv_ptr->count_elts + + row_mb = inv_ptr->count_solns * inv_ptr->elts.size() + carbon * inv_ptr->count_solns + 1 + inv_ptr->count_solns * inv_ptr->count_isotope_unknowns + inv_ptr->count_isotopes * inv_ptr->count_phases; - row_fract = row_mb + inv_ptr->count_elts; + row_fract = row_mb + inv_ptr->elts.size(); row_charge = row_fract + 2; row_carbon = row_charge + inv_ptr->count_solns; row_isotopes = row_carbon + carbon * inv_ptr->count_solns; @@ -350,7 +350,7 @@ setup_inverse(struct inverse *inv_ptr) /* * optimization */ - count_optimize = inv_ptr->count_solns * inv_ptr->count_elts + /* optimize */ + count_optimize = inv_ptr->count_solns * inv_ptr->elts.size() + /* optimize */ carbon * inv_ptr->count_solns + /* optimize ph */ 1 + /* optimize water */ inv_ptr->count_solns * inv_ptr->count_isotope_unknowns + /* optimize isotopes */ @@ -383,7 +383,7 @@ setup_inverse(struct inverse *inv_ptr) count_rows_t = count_rows; i_alk = -1; i_carb = -1; - for (i = 0; i < inv_ptr->count_elts; i++) + for (i = 0; i < inv_ptr->elts.size(); i++) { master_ptr = inv_ptr->elts[i].master; if (master_ptr == master_alk) @@ -521,7 +521,7 @@ setup_inverse(struct inverse *inv_ptr) /* mass balance: redox reaction data */ k = 0; - for (i = 0; i < inv_ptr->count_elts; i++) + for (i = 0; i < inv_ptr->elts.size(); i++) { if (inv_ptr->elts[i].master->s->primary == NULL) { @@ -578,7 +578,7 @@ setup_inverse(struct inverse *inv_ptr) /* mass-balance: epsilons */ column = col_epsilon; - for (i = 0; i < inv_ptr->count_elts; i++) + for (i = 0; i < inv_ptr->elts.size(); i++) { row = inv_ptr->elts[i].master->in; for (j = 0; j < inv_ptr->count_solns; j++) @@ -600,7 +600,7 @@ setup_inverse(struct inverse *inv_ptr) column++; } } - count_rows += inv_ptr->count_elts; + count_rows += inv_ptr->elts.size(); /* put names in col_name for ph */ @@ -690,7 +690,7 @@ setup_inverse(struct inverse *inv_ptr) { /* solution_ptr = solution_bsearch(inv_ptr->solns[i], &j, TRUE); */ /* array[(size_t)count_rows * (size_t)max_column_count + (size_t)i] = solution_ptr->cb; */ - for (j = 0; j < inv_ptr->count_elts; j++) + for (j = 0; j < inv_ptr->elts.size(); j++) { column = col_epsilon + j * inv_ptr->count_solns + i; coef = @@ -757,7 +757,7 @@ setup_inverse(struct inverse *inv_ptr) row_epsilon = count_rows; for (i = 0; i < inv_ptr->count_solns; i++) { - for (j = 0; j < inv_ptr->count_elts; j++) + for (j = 0; j < inv_ptr->elts.size(); j++) { if (inv_ptr->elts[j].master->s == s_eminus) continue; @@ -1811,7 +1811,7 @@ print_model(struct inverse *inv_ptr) error_msg("Computing delta pH/uncertainty", CONTINUE); } } - for (j = 0; j < inv_ptr->count_elts; j++) + for (j = 0; j < inv_ptr->elts.size(); j++) { if (inv_ptr->elts[j].master->s == s_eminus) continue; @@ -2677,7 +2677,7 @@ shrink(struct inverse *inv_ptr, LDBLE * array_in, LDBLE * array_out, { col_back_l[i] = -1; /* drop all epsilons for the solution */ - for (j = 0; j < inv_ptr->count_elts; j++) + for (j = 0; j < inv_ptr->elts.size(); j++) { column = col_epsilon + j * inv_ptr->count_solns + i; col_back_l[column] = -1; @@ -3246,7 +3246,7 @@ carbon_derivs(struct inverse *inv_ptr) */ c_uncertainty = 0; d_carbon = 0; - for (j = 0; j < inv_ptr->count_elts; j++) + for (j = 0; j < inv_ptr->elts.size(); j++) { if (inv_ptr->elts[j].master == s_co3->secondary) { @@ -3448,7 +3448,7 @@ isotope_balance_equation(struct inverse *inv_ptr, int row, int n) jit->second.Get_isotope_number() == isotope_number) { /* find column of master for solution i */ - for (k = 0; k < inv_ptr->count_elts; k++) + for (k = 0; k < inv_ptr->elts.size(); k++) { if (master_jit == inv_ptr->elts[k].master) break; @@ -3967,7 +3967,7 @@ write_optimize_names(struct inverse *inv_ptr) /* * epsilons for analytical data */ - for (j = 0; j < inv_ptr->count_elts; j++) + for (j = 0; j < inv_ptr->elts.size(); j++) { for (i = 0; i < inv_ptr->count_solns; i++) { @@ -4442,7 +4442,7 @@ dump_netpath_pat(struct inverse *inv_ptr) /*master_alk->total = solution_ptr->total_alkalinity; */ /* update total in master */ - for (j = 0; j < inv_ptr->count_elts; j++) + for (j = 0; j < inv_ptr->elts.size(); j++) { if (inv_ptr->elts[j].master->s == s_eminus) continue; @@ -4883,7 +4883,7 @@ dump_netpath_pat(struct inverse *inv_ptr) { master[j]->in = FALSE; } - for (j = 0; j < inv_ptr->count_elts; j++) + for (j = 0; j < inv_ptr->elts.size(); j++) { master_ptr = inv_ptr->elts[j].master; master_ptr = master_ptr->elt->primary; diff --git a/read.cpp b/read.cpp index cabc8bf2..af438576 100644 --- a/read.cpp +++ b/read.cpp @@ -1651,7 +1651,6 @@ read_inv_balances(struct inverse *inverse_ptr, const char* cptr) read_list_doubles(&cptr, &count); inverse_ptr->elts[count_elts].count_uncertainties = count; - inverse_ptr->count_elts++; } else if (strcmp_nocase_arg1(token, "ph") == 0) { diff --git a/structures.cpp b/structures.cpp index 3fc2684b..5481d3bb 100644 --- a/structures.cpp +++ b/structures.cpp @@ -534,7 +534,6 @@ inverse_alloc(void) */ inverse_ptr->description = NULL; inverse_ptr->count_solns = 0; - inverse_ptr->count_elts = 0; inverse_ptr->count_isotopes = 0; inverse_ptr->count_i_u = 0; inverse_ptr->count_phases = 0; @@ -634,7 +633,7 @@ inverse_free(struct inverse *inverse_ptr) inverse_ptr->force_solns.clear(); /* Free elts */ - for (i = 0; i < inverse_ptr->count_elts; i++) + for (i = 0; i < inverse_ptr->elts.size(); i++) { inverse_ptr->elts[i].uncertainties = (LDBLE *) free_check_null(inverse_ptr->elts[i].uncertainties); diff --git a/tidy.cpp b/tidy.cpp index d9c3dad8..fbfe4ee8 100644 --- a/tidy.cpp +++ b/tidy.cpp @@ -1151,7 +1151,7 @@ tidy_inverse(void) /* * Find master species for element, set uncertainties */ - for (j = 0; j < inverse[i].count_elts; j++) + for (j = 0; j < inverse[i].elts.size(); j++) { inverse[i].elts[j].master = master_bsearch_primary(inverse[i].elts[j].name); @@ -1288,7 +1288,7 @@ tidy_inverse(void) elt_list[j].elt->master->in = TRUE; } /* Include all input elements */ - for (j = 0; j < inverse[i].count_elts; j++) + for (j = 0; j < inverse[i].elts.size(); j++) { inverse[i].elts[j].master->in = TRUE; } @@ -1382,7 +1382,7 @@ tidy_inverse(void) * copy in input uncertainties */ /* copy primary redox to all secondary redox */ - for (j = 0; j < inverse[i].count_elts; j++) + for (j = 0; j < inverse[i].elts.size(); j++) { master_ptr = master_bsearch(inverse[i].elts[j].name); if (master_ptr == NULL) @@ -1411,7 +1411,7 @@ tidy_inverse(void) (LDBLE *) free_check_null(inverse[i].elts[j].uncertainties); } /* copy masters that are not primary redox */ - for (j = 0; j < inverse[i].count_elts; j++) + for (j = 0; j < inverse[i].elts.size(); j++) { master_ptr = master_bsearch(inverse[i].elts[j].name); if (master_ptr == NULL) @@ -1445,8 +1445,8 @@ tidy_inverse(void) */ inverse[i].elts.clear(); inverse[i].elts = inv_elts; - inverse[i].count_elts = count_in; - for (j = 0; j < inverse[i].count_elts; j++) + inverse[i].elts.resize(count_in); + for (j = 0; j < inverse[i].elts.size(); j++) { /* debug output_msg(sformatf( "\t%d\t%s", j, inverse[i].elts[j].master->elt->name)); From 980d58ebdb37c6f714b8288817bd9483b548ad57 Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Mon, 29 Mar 2021 09:22:09 -0600 Subject: [PATCH 23/53] finished vectorizing struct inverse. Need to do sub structs --- Phreeqc.h | 3 +- global_structures.h | 16 +-- inverse.cpp | 253 ++++++++++++++++++++------------------------ read.cpp | 81 ++++++-------- structures.cpp | 46 ++------ tidy.cpp | 2 +- 6 files changed, 160 insertions(+), 241 deletions(-) diff --git a/Phreeqc.h b/Phreeqc.h index c7c92f9b..ed25afc9 100644 --- a/Phreeqc.h +++ b/Phreeqc.h @@ -288,8 +288,7 @@ public: int carbon_derivs(struct inverse* inv_ptr); int check_isotopes(struct inverse* inv_ptr); int check_solns(struct inverse* inv_ptr); - int count_isotope_unknowns(struct inverse* inv_ptr, - struct isotope** isotope_unknowns); + bool set_isotope_unknowns(struct inverse* inv_ptrs); cxxSolutionIsotope* get_isotope(cxxSolution* solution_ptr, const char* elt); LDBLE get_inv_total(cxxSolution* solution_ptr, const char* elt); int isotope_balance_equation(struct inverse* inv_ptr, int row, int n); diff --git a/global_structures.h b/global_structures.h index 528eaff1..62bcdd5f 100644 --- a/global_structures.h +++ b/global_structures.h @@ -329,17 +329,11 @@ struct inverse std::vector solns; std::vector force_solns; std::vector elts; - int count_phases; - struct inv_phases *phases; - int count_master_list; - struct master **master_list; - int count_redox_rxns; - int count_isotopes; - struct inv_isotope *isotopes; - int count_i_u; - struct inv_isotope *i_u; - int count_isotope_unknowns; - struct isotope *isotope_unknowns; + std::vector phases; + size_t count_redox_rxns; + std::vector isotopes; + std::vector i_u; + std::vector isotope_unknowns; const char *netpath; const char *pat; }; diff --git a/inverse.cpp b/inverse.cpp index ebfc83ee..d0f4c052 100644 --- a/inverse.cpp +++ b/inverse.cpp @@ -122,11 +122,9 @@ inverse_models(void) setup_inverse(&(inverse[n])); punch_model_heading(&inverse[n]); solve_inverse(&(inverse[n])); - if (inverse[n].count_isotope_unknowns > 0) + if (inverse[n].isotope_unknowns.size() > 0) { - inverse[n].isotope_unknowns = - (struct isotope *) free_check_null(inverse[n]. - isotope_unknowns); + inverse[n].isotope_unknowns.clear(); } inverse[n].new_def = FALSE; if (inverse[n].pat != NULL) @@ -193,11 +191,9 @@ setup_inverse(struct inverse *inv_ptr) /* * tidy isotopes if necessary */ - inv_ptr->count_isotope_unknowns = 0; - if (inv_ptr->count_isotopes > 0) + if (inv_ptr->isotopes.size() > 0) { - inv_ptr->count_isotope_unknowns = - count_isotope_unknowns(inv_ptr, &inv_ptr->isotope_unknowns); + set_isotope_unknowns(inv_ptr); if (get_input_errors() > 0) { error_msg("Stopping because of input errors.", STOP); @@ -214,48 +210,48 @@ setup_inverse(struct inverse *inv_ptr) */ max_column_count = inv_ptr->elts.size() * inv_ptr->count_solns + /* epsilons */ inv_ptr->count_solns + /* solutions */ - inv_ptr->count_phases + /* phases */ + inv_ptr->phases.size() + /* phases */ inv_ptr->count_redox_rxns + /* redox reactions */ carbon * inv_ptr->count_solns + /* pH */ 1 + /* water */ - inv_ptr->count_isotope_unknowns * inv_ptr->count_solns + /* isotopes in solution */ - inv_ptr->count_isotopes * inv_ptr->count_phases + /* isotopes in phases */ + inv_ptr->isotope_unknowns.size() * inv_ptr->count_solns + /* isotopes in solution */ + inv_ptr->isotopes.size() * inv_ptr->phases.size() + /* isotopes in phases */ 1 + 1; /* rhs, ineq */ count_unknowns = max_column_count - 2; col_phases = inv_ptr->count_solns; - col_redox = col_phases + inv_ptr->count_phases; + col_redox = col_phases + inv_ptr->phases.size(); col_epsilon = col_redox + inv_ptr->count_redox_rxns; col_ph = col_epsilon + inv_ptr->elts.size() * inv_ptr->count_solns; col_water = col_ph + carbon * inv_ptr->count_solns; col_isotopes = col_water + 1; col_phase_isotopes = - col_isotopes + inv_ptr->count_isotope_unknowns * inv_ptr->count_solns; + col_isotopes + inv_ptr->isotope_unknowns.size() * inv_ptr->count_solns; max_row_count = inv_ptr->count_solns * inv_ptr->elts.size() + /* optimize */ carbon * inv_ptr->count_solns + /* optimize ph */ 1 + /* optimize water */ - inv_ptr->count_solns * inv_ptr->count_isotope_unknowns + /* optimize isotopes */ - inv_ptr->count_isotopes * inv_ptr->count_phases + /* optimize phase isotopes */ + inv_ptr->count_solns * inv_ptr->isotope_unknowns.size() + /* optimize isotopes */ + inv_ptr->isotopes.size() * inv_ptr->phases.size() + /* optimize phase isotopes */ inv_ptr->elts.size() + /* mass balances */ 1 + 1 + /* fractions, init and final */ inv_ptr->count_solns + /* charge balances */ carbon * inv_ptr->count_solns + /* dAlk = dC + dph */ - inv_ptr->count_isotopes + /* isotopes */ + inv_ptr->isotopes.size() + /* isotopes */ 2 * inv_ptr->count_solns * inv_ptr->elts.size() + /* epsilon constraints */ 2 * carbon * inv_ptr->count_solns + /* epsilon on ph */ 2 + /* epsilon for water */ - 2 * inv_ptr->count_isotope_unknowns * inv_ptr->count_solns + /* epsilon for isotopes */ - 2 * inv_ptr->count_isotopes * inv_ptr->count_phases + /* epsilon for isotopes in phases */ + 2 * inv_ptr->isotope_unknowns.size() * inv_ptr->count_solns + /* epsilon for isotopes */ + 2 * inv_ptr->isotopes.size() * inv_ptr->phases.size() + /* epsilon for isotopes in phases */ 2; /* work space */ row_mb = inv_ptr->count_solns * inv_ptr->elts.size() + carbon * inv_ptr->count_solns + 1 + - inv_ptr->count_solns * inv_ptr->count_isotope_unknowns + - inv_ptr->count_isotopes * inv_ptr->count_phases; + inv_ptr->count_solns * inv_ptr->isotope_unknowns.size() + + inv_ptr->isotopes.size() * inv_ptr->phases.size(); row_fract = row_mb + inv_ptr->elts.size(); row_charge = row_fract + 2; row_carbon = row_charge + inv_ptr->count_solns; row_isotopes = row_carbon + carbon * inv_ptr->count_solns; - row_epsilon = row_isotopes + inv_ptr->count_isotopes; + row_epsilon = row_isotopes + inv_ptr->isotopes.size(); /* The next three are not right, some rows of epsilon are deleted */ /* row_ph_epsilon = row_epsilon + 2 * inv_ptr->count_solns * inv_ptr->count_elts; @@ -353,8 +349,8 @@ setup_inverse(struct inverse *inv_ptr) count_optimize = inv_ptr->count_solns * inv_ptr->elts.size() + /* optimize */ carbon * inv_ptr->count_solns + /* optimize ph */ 1 + /* optimize water */ - inv_ptr->count_solns * inv_ptr->count_isotope_unknowns + /* optimize isotopes */ - inv_ptr->count_isotopes * inv_ptr->count_phases; /* optimize phase isotopes */ + inv_ptr->count_solns * inv_ptr->isotope_unknowns.size() + /* optimize isotopes */ + inv_ptr->isotopes.size() * inv_ptr->phases.size(); /* optimize phase isotopes */ for (i = 0; i < count_optimize; i++) { @@ -470,7 +466,7 @@ setup_inverse(struct inverse *inv_ptr) /* mass_balance: phase data */ - for (i = 0; i < inv_ptr->count_phases; i++) + for (size_t i = 0; i < inv_ptr->phases.size(); i++) { phase_ptr = inv_ptr->phases[i].phase; rxn_ptr = phase_ptr->rxn_s; @@ -619,7 +615,7 @@ setup_inverse(struct inverse *inv_ptr) /* put names of isotopes in col_name */ for (i = 0; i < inv_ptr->count_solns; i++) { - for (j = 0; j < inv_ptr->count_isotope_unknowns; j++) + for (j = 0; j < inv_ptr->isotope_unknowns.size(); j++) { sprintf(token, "%d%s %d", (int) inv_ptr->isotope_unknowns[j].isotope_number, @@ -631,12 +627,12 @@ setup_inverse(struct inverse *inv_ptr) /* put phase isotopes in col_name */ - if (inv_ptr->count_isotopes > 0) + if (inv_ptr->isotopes.size() > 0) { /* isotopes of phases phases */ - for (i = 0; i < inv_ptr->count_phases; i++) + for (size_t i = 0; i < inv_ptr->phases.size(); i++) { - for (j = 0; j < inv_ptr->count_isotopes; j++) + for (j = 0; j < inv_ptr->isotopes.size(); j++) { sprintf(token, "%d%s %s", (int) inv_ptr->isotopes[j].isotope_number, @@ -740,9 +736,9 @@ setup_inverse(struct inverse *inv_ptr) { error_msg("Stopping because of input errors.", STOP); } - if (inv_ptr->count_isotopes != 0) + if (inv_ptr->isotopes.size() != 0) { - for (j = 0; j < inv_ptr->count_isotopes; j++) + for (j = 0; j < inv_ptr->isotopes.size(); j++) { isotope_balance_equation(inv_ptr, count_rows, j); sprintf(token, "%d%s", (int) inv_ptr->isotopes[j].isotope_number, @@ -918,15 +914,15 @@ setup_inverse(struct inverse *inv_ptr) * inequalities for isotopes */ row_isotope_epsilon = count_rows; - if (inv_ptr->count_isotopes > 0) + if (inv_ptr->isotopes.size() > 0) { for (i = 0; i < inv_ptr->count_solns; i++) { solution_ptr = Utilities::Rxn_find(Rxn_solution_map, inv_ptr->solns[i]); - for (j = 0; j < inv_ptr->count_isotope_unknowns; j++) + for (j = 0; j < inv_ptr->isotope_unknowns.size(); j++) { column = - col_isotopes + (i * inv_ptr->count_isotope_unknowns) + j; + col_isotopes + (i * inv_ptr->isotope_unknowns.size()) + j; master_ptr = inv_ptr->isotope_unknowns[j].master; isotope_number = inv_ptr->isotope_unknowns[j].isotope_number; std::map < std::string, cxxSolutionIsotope >::iterator kit = solution_ptr->Get_isotopes().begin(); @@ -981,7 +977,7 @@ setup_inverse(struct inverse *inv_ptr) * Set non-negativity constraints */ - for (i = 0; i < inv_ptr->count_phases; i++) + for (size_t i = 0; i < inv_ptr->phases.size(); i++) { if (inv_ptr->phases[i].constraint == PRECIPITATE) { @@ -1130,7 +1126,7 @@ solve_inverse(struct inverse *inv_ptr) * Set current bits to complete list. */ soln_bits = 0; - if (inv_ptr->count_solns + inv_ptr->count_phases > 32) + if (inv_ptr->count_solns + inv_ptr->phases.size() > 32) { error_msg ("For inverse modeling, sum of initial solutions and phases must be <= 32.\n\tFor all reasonable calculations, the sum should be much less than 32.", @@ -1159,7 +1155,7 @@ solve_inverse(struct inverse *inv_ptr) /* * Loop through all models of of descending size */ - for (model_size = inv_ptr->count_phases; model_size >= 0; + for (model_size = inv_ptr->phases.size(); model_size >= 0; model_size--) { first_of_model_size = TRUE; @@ -1169,7 +1165,7 @@ solve_inverse(struct inverse *inv_ptr) { first_of_model_size = FALSE; current_bits = - (soln_bits << inv_ptr->count_phases) + phase_bits; + (soln_bits << inv_ptr->phases.size()) + phase_bits; if (subset_bad(current_bits) == TRUE || subset_minimal(current_bits) == TRUE) @@ -1203,7 +1199,7 @@ solve_inverse(struct inverse *inv_ptr) * Model has been found, set bits */ good_bits = current_bits; - for (i = 0; i < inv_ptr->count_phases; i++) + for (size_t i = 0; i < inv_ptr->phases.size(); i++) { if (equal(inv_delta1[i + inv_ptr->count_solns], 0.0, TOL) == TRUE) @@ -1216,7 +1212,7 @@ solve_inverse(struct inverse *inv_ptr) if (equal(inv_delta1[i], 0.0, TOL) == TRUE) { good_bits = - set_bit(good_bits, i + inv_ptr->count_phases, 0); + set_bit(good_bits, i + inv_ptr->phases.size(), 0); } } /* @@ -1360,9 +1356,9 @@ minimal_solve(struct inverse *inv_ptr, unsigned long minimal_bits) if (debug_inverse == TRUE) { output_msg(sformatf( "Beginning minimal solve: \n")); - bit_print(minimal_bits, inv_ptr->count_phases + inv_ptr->count_solns); + bit_print(minimal_bits, inv_ptr->phases.size() + inv_ptr->count_solns); } - for (i = 0; i < inv_ptr->count_phases + inv_ptr->count_solns - 1; i++) + for (i = 0; i < inv_ptr->phases.size() + inv_ptr->count_solns - 1; i++) { if (get_bits(minimal_bits, i, 1) == 0) continue; @@ -1373,7 +1369,7 @@ minimal_solve(struct inverse *inv_ptr, unsigned long minimal_bits) { output_msg(sformatf( "Solving for minimal\n")); bit_print(minimal_bits, - inv_ptr->count_phases + inv_ptr->count_solns); + inv_ptr->phases.size() + inv_ptr->count_solns); } /* @@ -1398,7 +1394,7 @@ minimal_solve(struct inverse *inv_ptr, unsigned long minimal_bits) if (debug_inverse == TRUE) { output_msg(sformatf( "\n\nMINIMAL MODEL\n\n")); - bit_print(minimal_bits, inv_ptr->count_phases + inv_ptr->count_solns); + bit_print(minimal_bits, inv_ptr->phases.size() + inv_ptr->count_solns); } solve_with_mask(inv_ptr, minimal_bits); @@ -1407,10 +1403,10 @@ minimal_solve(struct inverse *inv_ptr, unsigned long minimal_bits) { if (equal(inv_delta1[i], 0.0, TOL) == FALSE) { - actual_bits = set_bit(actual_bits, i + inv_ptr->count_phases, 1); + actual_bits = set_bit(actual_bits, i + inv_ptr->phases.size(), 1); } } - for (i = 0; i < inv_ptr->count_phases; i++) + for (size_t i = 0; i < inv_ptr->phases.size(); i++) { if (equal(inv_delta1[i + inv_ptr->count_solns], 0.0, TOL) == FALSE) { @@ -1858,10 +1854,10 @@ print_model(struct inverse *inv_ptr) error_msg("Computing delta element/uncertainty", CONTINUE); } } - if (inv_ptr->count_isotopes > 0) + if (inv_ptr->isotopes.size() > 0) { /* adjustments to solution isotope composition */ - for (j = 0; j < inv_ptr->count_isotope_unknowns; j++) + for (j = 0; j < inv_ptr->isotope_unknowns.size(); j++) { std::map < std::string, cxxSolutionIsotope >::iterator kit = solution_ptr->Get_isotopes().begin(); for ( ; kit != solution_ptr->Get_isotopes().end(); kit++) @@ -1873,7 +1869,7 @@ print_model(struct inverse *inv_ptr) continue; d1 = kit->second.Get_ratio(); d2 = inv_delta1[col_isotopes + - i * inv_ptr->count_isotope_unknowns + + i * inv_ptr->isotope_unknowns.size() + j] / inv_delta1[i]; d3 = d1 + d2; @@ -1920,10 +1916,10 @@ print_model(struct inverse *inv_ptr) * Adjustments to phases */ print_msg = FALSE; - if (inv_ptr->count_isotopes > 0) + if (inv_ptr->isotopes.size() > 0) { output_msg(sformatf( "\nIsotopic composition of phases:\n")); - for (i = 0; i < inv_ptr->count_phases; i++) + for (size_t i = 0; i < inv_ptr->phases.size(); i++) { if (inv_ptr->phases[i].count_isotopes == 0) continue; @@ -1933,7 +1929,7 @@ print_model(struct inverse *inv_ptr) equal(max_delta[j], 0.0, toler) == TRUE) continue; isotope_ptr = inv_ptr->phases[i].isotopes; - for (j = 0; j < inv_ptr->count_isotopes; j++) + for (j = 0; j < inv_ptr->isotopes.size(); j++) { for (k = 0; k < inv_ptr->phases[i].count_isotopes; k++) { @@ -1944,7 +1940,7 @@ print_model(struct inverse *inv_ptr) continue; d1 = isotope_ptr[k].ratio; column = - col_phase_isotopes + i * inv_ptr->count_isotopes + j; + col_phase_isotopes + i * inv_ptr->isotopes.size() + j; if (inv_delta1[col_phases + i] != 0.0) { d2 = inv_delta1[column] / inv_delta1[col_phases + i]; @@ -2372,7 +2368,7 @@ next_set_phases(struct inverse *inv_ptr, { min_position[i] = i; now[i] = i; - max_position[i] = inv_ptr->count_phases - model_size + i; + max_position[i] = inv_ptr->phases.size() - model_size + i; } } else @@ -2430,9 +2426,9 @@ range(struct inverse *inv_ptr, unsigned long cur_bits) /* * Include forced solutions and phases in range calculation */ - for (i = 0; i < inv_ptr->count_solns + inv_ptr->count_phases; i++) + for (size_t i = 0; i < inv_ptr->count_solns + inv_ptr->phases.size(); i++) { - if (i < inv_ptr->count_phases) + if (i < inv_ptr->phases.size()) { if (inv_ptr->phases[i].force == TRUE) { @@ -2441,7 +2437,7 @@ range(struct inverse *inv_ptr, unsigned long cur_bits) } else { - if (inv_ptr->force_solns[i - inv_ptr->count_phases] == TRUE) + if (inv_ptr->force_solns[i - inv_ptr->phases.size()] == TRUE) { cur_bits = set_bit(cur_bits, i, 1); } @@ -2456,15 +2452,15 @@ range(struct inverse *inv_ptr, unsigned long cur_bits) * Switch bits so that phases are high and solutions are low */ bits = - get_bits(cur_bits, inv_ptr->count_phases + inv_ptr->count_solns - 1, + get_bits(cur_bits, inv_ptr->phases.size() + inv_ptr->count_solns - 1, inv_ptr->count_solns); bits += - (get_bits(cur_bits, inv_ptr->count_phases - 1, inv_ptr->count_phases) + (get_bits(cur_bits, inv_ptr->phases.size() - 1, inv_ptr->phases.size()) << inv_ptr->count_solns); /* * Do range calculation */ - for (i = 0; i < inv_ptr->count_solns + inv_ptr->count_phases; i++) + for (i = 0; i < inv_ptr->count_solns + inv_ptr->phases.size(); i++) { if (inv_ptr->count_solns == i + 1) { @@ -2651,18 +2647,18 @@ shrink(struct inverse *inv_ptr, LDBLE * array_in, LDBLE * array_out, /* * Drop phases not in model */ - for (i = 0; i < inv_ptr->count_phases; i++) + for (i = 0; i < inv_ptr->phases.size(); i++) { if (get_bits(cur_bits, i, 1) == 0) { col_back_l[col_phases + i] = -1; /* drop isotopes */ - if (inv_ptr->count_isotopes > 0) + if (inv_ptr->isotopes.size() > 0) { - for (j = 0; j < inv_ptr->count_isotopes; j++) + for (j = 0; j < inv_ptr->isotopes.size(); j++) { column = - col_phase_isotopes + i * inv_ptr->count_isotopes + j; + col_phase_isotopes + i * inv_ptr->isotopes.size() + j; col_back_l[column] = -1; } } @@ -2673,7 +2669,7 @@ shrink(struct inverse *inv_ptr, LDBLE * array_in, LDBLE * array_out, */ for (i = 0; i < (inv_ptr->count_solns - 1); i++) { - if (get_bits(cur_bits, inv_ptr->count_phases + i, 1) == 0) + if (get_bits(cur_bits, inv_ptr->phases.size() + i, 1) == 0) { col_back_l[i] = -1; /* drop all epsilons for the solution */ @@ -2689,12 +2685,12 @@ shrink(struct inverse *inv_ptr, LDBLE * array_in, LDBLE * array_out, col_back_l[column] = -1; } /* drop isotopes */ - if (inv_ptr->count_isotopes > 0) + if (inv_ptr->isotopes.size() > 0) { - for (j = 0; j < inv_ptr->count_isotope_unknowns; j++) + for (j = 0; j < inv_ptr->isotope_unknowns.size(); j++) { column = - col_isotopes + i * inv_ptr->count_isotope_unknowns + + col_isotopes + i * inv_ptr->isotope_unknowns.size() + j; col_back_l[column] = -1; } @@ -2908,7 +2904,7 @@ check_solns(struct inverse *inv_ptr) for (i = 0; i < inv_ptr->count_solns; i++) { bits = 0; - bits += 1 << (inv_ptr->count_phases + i); + bits += 1 << (inv_ptr->phases.size() + i); /* * Check for feasibility of charge balance with given uncertainties */ @@ -3470,7 +3466,7 @@ isotope_balance_equation(struct inverse *inv_ptr, int row, int n) { /* find column of epsilon for ratio of valence */ - for (k = 0; k < inv_ptr->count_isotope_unknowns; k++) + for (k = 0; k < inv_ptr->isotope_unknowns.size(); k++) { if (master_jit == inv_ptr->isotope_unknowns[k].master @@ -3479,7 +3475,7 @@ isotope_balance_equation(struct inverse *inv_ptr, int row, int n) { column = col_isotopes + - (i * inv_ptr->count_isotope_unknowns) + k; + (i * inv_ptr->isotope_unknowns.size()) + k; } } my_array[(size_t)row * (size_t)max_column_count + (size_t)column] += @@ -3490,7 +3486,7 @@ isotope_balance_equation(struct inverse *inv_ptr, int row, int n) /* * Fill in terms for each phase */ - for (i = 0; i < inv_ptr->count_phases; i++) + for (i = 0; i < inv_ptr->phases.size(); i++) { if (inv_ptr->phases[i].count_isotopes <= 0) continue; @@ -3505,7 +3501,7 @@ isotope_balance_equation(struct inverse *inv_ptr, int row, int n) my_array[(size_t)row * (size_t)max_column_count + (size_t)column] = isotope_ptr[j].ratio * isotope_ptr[j].coef; /* term for phase isotope uncertainty unknown */ - column = col_phase_isotopes + i * inv_ptr->count_isotopes + n; + column = col_phase_isotopes + i * inv_ptr->isotopes.size() + n; my_array[(size_t)row * (size_t)max_column_count + (size_t)column] = isotope_ptr[j].coef; break; } @@ -3515,55 +3511,47 @@ isotope_balance_equation(struct inverse *inv_ptr, int row, int n) return OK; } /* ---------------------------------------------------------------------- */ -int Phreeqc:: -count_isotope_unknowns(struct inverse *inv_ptr, - struct isotope **isotope_unknowns) -/* ---------------------------------------------------------------------- */ +bool Phreeqc:: +set_isotope_unknowns(struct inverse* inv_ptr) + /* ---------------------------------------------------------------------- */ { -/* - * Go through elements for which isotope balances are requested - * and make a array of isotope structures - * return total number of isotope unknowns and structure array - */ + /* + * Go through elements for which isotope balances are requested + * and make a array of isotope structures + * return total number of isotope unknowns and structure array + */ int i, k; LDBLE isotope_number; - struct master *primary_ptr; - int count_isotopes; - struct isotope *isotopes; + struct master* primary_ptr; + size_t count_isotopes; + std::vector& isotopes = inv_ptr->isotope_unknowns; - if (inv_ptr->count_isotopes == 0) + if (inv_ptr->isotopes.size() == 0) { - *isotope_unknowns = NULL; - return (0); - } - isotopes = - (struct isotope *) PHRQ_malloc((size_t) sizeof(struct isotope)); - if (isotopes == NULL) - { - malloc_error(); - return (0); + isotopes.clear(); + return true; } count_isotopes = 0; - for (i = 0; i < inv_ptr->count_isotopes; i++) + for (i = 0; i < inv_ptr->isotopes.size(); i++) { primary_ptr = master_bsearch(inv_ptr->isotopes[i].elt_name); isotope_number = inv_ptr->isotopes[i].isotope_number; if (primary_ptr == NULL) { error_string = sformatf( - "Element not found for isotope calculation: %s.", - inv_ptr->isotopes[i].elt_name); + "Element not found for isotope calculation: %s.", + inv_ptr->isotopes[i].elt_name); error_msg(error_string, CONTINUE); input_error++; break; } if (primary_ptr->primary != TRUE) { - error_string = sformatf( "Isotope mass-balance may only be used" - " for total element concentrations.\n" - "Secondary species not allowed: %s.", - inv_ptr->isotopes[i].elt_name); + error_string = sformatf("Isotope mass-balance may only be used" + " for total element concentrations.\n" + "Secondary species not allowed: %s.", + inv_ptr->isotopes[i].elt_name); error_msg(error_string, CONTINUE); input_error++; break; @@ -3572,13 +3560,7 @@ count_isotope_unknowns(struct inverse *inv_ptr, /* nonredox element */ if (primary_ptr->s->secondary == NULL) { - isotopes = (struct isotope *) PHRQ_realloc(isotopes, - ((size_t)count_isotopes + 1) * sizeof(struct isotope)); - if (isotopes == NULL) - { - malloc_error(); - return (0); - } + isotopes.resize(count_isotopes + 1); isotopes[count_isotopes].primary = primary_ptr; isotopes[count_isotopes].master = primary_ptr; isotopes[count_isotopes].isotope_number = isotope_number; @@ -3601,15 +3583,7 @@ count_isotope_unknowns(struct inverse *inv_ptr, k++; for (; k < (int)master.size(); k++) { - if (master[k]->elt->primary != primary_ptr) - break; - isotopes = (struct isotope *) PHRQ_realloc(isotopes, - ((size_t)count_isotopes + 1) * sizeof(struct isotope)); - if (isotopes == NULL) - { - malloc_error(); - return (0); - } + isotopes.resize(count_isotopes + 1); isotopes[count_isotopes].primary = primary_ptr; isotopes[count_isotopes].master = master[k]; isotopes[count_isotopes].isotope_number = isotope_number; @@ -3618,8 +3592,7 @@ count_isotope_unknowns(struct inverse *inv_ptr, } } } - *isotope_unknowns = isotopes; - return (count_isotopes); + return true; } /* ---------------------------------------------------------------------- */ int Phreeqc:: @@ -3650,7 +3623,7 @@ check_isotopes(struct inverse *inv_ptr) * Go through inverse isotopes and make sure isotope data for each solution * inv_ptr->isotopes has elements; inv_ptr->i_u has redox states and uncertainties */ - for (i = 0; i < inv_ptr->count_isotopes; i++) + for (i = 0; i < inv_ptr->isotopes.size(); i++) { err = FALSE; primary_ptr = master_bsearch(inv_ptr->isotopes[i].elt_name); @@ -3705,7 +3678,7 @@ check_isotopes(struct inverse *inv_ptr) * Search for secondary or primary master in inverse uncertainties */ ii = -1; - for (i = 0; i < inv_ptr->count_i_u; i++) + for (i = 0; i < inv_ptr->i_u.size(); i++) { master_ptr = master_bsearch(inv_ptr->i_u[i].elt_name); if (master_ptr == master_kit) @@ -3799,9 +3772,9 @@ check_isotopes(struct inverse *inv_ptr) /* * Check phases for necessary isotope data */ - for (j = 0; j < inv_ptr->count_phases; j++) + for (j = 0; j < inv_ptr->phases.size(); j++) { - for (i = 0; i < inv_ptr->count_isotopes; i++) + for (i = 0; i < inv_ptr->isotopes.size(); i++) { primary_ptr = master_bsearch(inv_ptr->isotopes[i].elt_name); isotope_number = inv_ptr->isotopes[i].isotope_number; @@ -3857,9 +3830,9 @@ phase_isotope_inequalities(struct inverse *inv_ptr) int i, j, k; int column; char token[MAX_LENGTH]; - if (inv_ptr->count_isotopes <= 0) + if (inv_ptr->isotopes.size() <= 0) return OK; - for (i = 0; i < inv_ptr->count_phases; i++) + for (i = 0; i < inv_ptr->phases.size(); i++) { if (inv_ptr->phases[i].count_isotopes <= 0) continue; @@ -3867,7 +3840,7 @@ phase_isotope_inequalities(struct inverse *inv_ptr) for (j = 0; j < inv_ptr->phases[i].count_isotopes; j++) { /* find index number */ - for (k = 0; k < inv_ptr->count_isotopes; k++) + for (k = 0; k < inv_ptr->isotopes.size(); k++) { if (inv_ptr->phases[i].isotopes[j].elt_name == inv_ptr->isotopes[k].elt_name @@ -3877,9 +3850,9 @@ phase_isotope_inequalities(struct inverse *inv_ptr) break; } } - if (k >= inv_ptr->count_isotopes) + if (k >= inv_ptr->isotopes.size()) break; - column = col_phase_isotopes + i * inv_ptr->count_isotopes + k; + column = col_phase_isotopes + i * inv_ptr->isotopes.size() + k; /* * zero column if uncertainty is zero */ @@ -4000,7 +3973,7 @@ write_optimize_names(struct inverse *inv_ptr) */ for (i = 0; i < inv_ptr->count_solns; i++) { - for (j = 0; j < inv_ptr->count_isotope_unknowns; j++) + for (j = 0; j < inv_ptr->isotope_unknowns.size(); j++) { sprintf(token, "%s %d%s %d", "optimize", (int) inv_ptr->isotope_unknowns[j].isotope_number, @@ -4013,9 +3986,9 @@ write_optimize_names(struct inverse *inv_ptr) * phase isotopes */ - for (i = 0; i < inv_ptr->count_phases; i++) + for (i = 0; i < inv_ptr->phases.size(); i++) { - for (j = 0; j < inv_ptr->count_isotopes; j++) + for (j = 0; j < inv_ptr->isotopes.size(); j++) { sprintf(token, "%s %s %d%s", "optimize", inv_ptr->phases[i].phase->name, @@ -4464,10 +4437,10 @@ dump_netpath_pat(struct inverse *inv_ptr) solution_ptr->Set_totals(nd); /* update isotopes in solution */ - if (inv_ptr->count_isotopes > 0) + if (inv_ptr->isotopes.size() > 0) { /* adjustments to solution isotope composition */ - for (j = 0; j < inv_ptr->count_isotope_unknowns; j++) + for (j = 0; j < inv_ptr->isotope_unknowns.size(); j++) { std::map < std::string, cxxSolutionIsotope >::iterator kit = solution_ptr->Get_isotopes().begin(); for ( ; kit != solution_ptr->Get_isotopes().end(); kit++) @@ -4479,7 +4452,7 @@ dump_netpath_pat(struct inverse *inv_ptr) continue; d1 = kit->second.Get_ratio(); d2 = inv_delta1[col_isotopes + - i * inv_ptr->count_isotope_unknowns + + i * inv_ptr->isotope_unknowns.size() + j] / inv_delta1[i]; d3 = d1 + d2; kit->second.Set_ratio(d3); @@ -4912,7 +4885,7 @@ dump_netpath_pat(struct inverse *inv_ptr) /* * Add isotope mole balance */ - for (j = 0; j < inv_ptr->count_isotopes; j++) + for (j = 0; j < inv_ptr->isotopes.size(); j++) { string = sformatf("%d%s", (int) inv_ptr->isotopes[j].isotope_number, inv_ptr->isotopes[j].elt_name); @@ -4940,7 +4913,7 @@ dump_netpath_pat(struct inverse *inv_ptr) /* * Write phase information */ - for (i = 0; i < inv_ptr->count_phases; i++) + for (i = 0; i < inv_ptr->phases.size(); i++) { j = col_phases + i; /* skip if not in model */ @@ -5081,7 +5054,7 @@ dump_netpath_pat(struct inverse *inv_ptr) { isotope_ptr = inv_ptr->phases[i].isotopes; d1 = isotope_ptr[k].ratio; - for (j = 0; j < inv_ptr->count_isotopes; j++) + for (j = 0; j < inv_ptr->isotopes.size(); j++) { if ((inv_ptr->isotopes[j].elt_name != isotope_ptr[k].elt_name) || (inv_ptr->isotopes[j].isotope_number != @@ -5090,9 +5063,9 @@ dump_netpath_pat(struct inverse *inv_ptr) break; } d2 = 0.0; - if (j < inv_ptr->count_isotopes) + if (j < inv_ptr->isotopes.size()) { - column = col_phase_isotopes + i * inv_ptr->count_isotopes + j; + column = col_phase_isotopes + i * inv_ptr->isotopes.size() + j; if (inv_delta1[col_phases + i] != 0.0) { d2 = inv_delta1[column] / inv_delta1[col_phases + i]; @@ -5146,7 +5119,7 @@ dump_netpath_pat(struct inverse *inv_ptr) /*fprintf(model_file,"%2d", i); */ /* not written, 1, mixing, number of mixing wells -1 */ fprintf(model_file, "%2d", 3); /* 2, exchange */ i = 0; - if (inv_ptr->count_isotopes > 0) + if (inv_ptr->isotopes.size() > 0) i = 1; fprintf(model_file, "%2d", i); /* 3, Rayleigh */ fprintf(model_file, "%2d", 1); /* 4, A0 model */ diff --git a/read.cpp b/read.cpp index af438576..9e7436ab 100644 --- a/read.cpp +++ b/read.cpp @@ -1600,17 +1600,17 @@ read_inverse(void) /* * Sort isotopes */ - if (inverse[n].count_isotopes > 1) + if (inverse[n].isotopes.size() > 1) { - qsort(inverse[n].isotopes, - (size_t) inverse[n].count_isotopes, + qsort(&inverse[n].isotopes[0], + inverse[n].isotopes.size(), sizeof(struct inv_isotope), inverse_isotope_compare); } - if (inverse[n].count_i_u > 1) + if (inverse[n].i_u.size() > 1) { - qsort(inverse[n].i_u, - (size_t) inverse[n].count_i_u, + qsort(&inverse[n].i_u[0], + inverse[n].i_u.size(), (size_t) sizeof(struct inv_isotope), inverse_isotope_compare); } @@ -1717,45 +1717,36 @@ read_inv_isotopes(struct inverse *inverse_ptr, const char* cptr) /* * add element name to inv_ptr->isotopes */ - for (i = 0; i < inverse_ptr->count_isotopes; i++) + for (i = 0; i < inverse_ptr->isotopes.size(); i++) { if (element_name == inverse_ptr->isotopes[i].elt_name) break; } - if (i == inverse_ptr->count_isotopes) + if (i == inverse_ptr->isotopes.size()) { - inverse_ptr->isotopes = (struct inv_isotope *) PHRQ_realloc(inverse_ptr->isotopes, - ((size_t)inverse_ptr->count_isotopes + 1) * sizeof(struct inv_isotope)); - if (inverse_ptr->isotopes == NULL) - malloc_error(); - inverse_ptr->isotopes[inverse_ptr->count_isotopes].isotope_number = isotope_number; - inverse_ptr->isotopes[inverse_ptr->count_isotopes].elt_name = element_name; - inverse_ptr->isotopes[inverse_ptr->count_isotopes].uncertainties = + size_t count_isotopes = inverse_ptr->isotopes.size(); + inverse_ptr->isotopes.resize(count_isotopes + 1); + inverse_ptr->isotopes[count_isotopes].isotope_number = isotope_number; + inverse_ptr->isotopes[count_isotopes].elt_name = element_name; + inverse_ptr->isotopes[count_isotopes].uncertainties = (LDBLE *) PHRQ_malloc((size_t) sizeof(LDBLE)); - if (inverse_ptr->isotopes[inverse_ptr->count_isotopes]. + if (inverse_ptr->isotopes[count_isotopes]. uncertainties == NULL) malloc_error(); - inverse_ptr->count_isotopes++; } /* * add redox state name to inv_ptr->i_u */ - inverse_ptr->i_u = (struct inv_isotope *) PHRQ_realloc(inverse_ptr->i_u, - ((size_t)inverse_ptr->count_i_u + 1) * sizeof(struct inv_isotope)); - if (inverse_ptr->i_u == NULL) - { - malloc_error(); - return (OK); - } - inverse_ptr->i_u[inverse_ptr->count_i_u].elt_name = redox_name; - inverse_ptr->i_u[inverse_ptr->count_i_u].isotope_number = isotope_number; + size_t count_i_u = inverse_ptr->i_u.size(); + inverse_ptr->i_u.resize(count_i_u + 1); + inverse_ptr->i_u[count_i_u].elt_name = redox_name; + inverse_ptr->i_u[count_i_u].isotope_number = isotope_number; /* * Read isotope uncertainties */ - inverse_ptr->i_u[inverse_ptr->count_i_u].uncertainties = + inverse_ptr->i_u[count_i_u].uncertainties = read_list_doubles(&cptr1, &count); - inverse_ptr->i_u[inverse_ptr->count_i_u].count_uncertainties = count; - inverse_ptr->count_i_u++; + inverse_ptr->i_u[count_i_u].count_uncertainties = count; return (OK); } /* ---------------------------------------------------------------------- */ @@ -1773,16 +1764,15 @@ read_inv_phases(struct inverse *inverse_ptr, const char* cptr) j = copy_token(token, &cptr, &l); if (j == EMPTY) return (OK); - inverse_ptr->phases = (struct inv_phases *) PHRQ_realloc(inverse_ptr->phases, - ((size_t)inverse_ptr->count_phases + 1) * sizeof(struct inv_phases)); - if (inverse_ptr->phases == NULL) - malloc_error(); - inverse_ptr->phases[inverse_ptr->count_phases].name = string_hsave(token); + + size_t count_phases = inverse_ptr->phases.size(); + inverse_ptr->phases.resize(count_phases + 1); + inverse_ptr->phases[count_phases].name = string_hsave(token); /* * Read constraint, force, and isotopes */ - inverse_ptr->phases[inverse_ptr->count_phases].constraint = EITHER; - inverse_ptr->phases[inverse_ptr->count_phases].force = FALSE; + inverse_ptr->phases[count_phases].constraint = EITHER; + inverse_ptr->phases[count_phases].force = FALSE; for (;;) { cxxSolutionIsotope temp_isotope; @@ -1793,17 +1783,15 @@ read_inv_phases(struct inverse *inverse_ptr, const char* cptr) str_tolower(token1); if (token1[0] == 'p') { - inverse_ptr->phases[inverse_ptr->count_phases].constraint = - PRECIPITATE; + inverse_ptr->phases[count_phases].constraint = PRECIPITATE; } else if (token1[0] == 'd') { - inverse_ptr->phases[inverse_ptr->count_phases].constraint = - DISSOLVE; + inverse_ptr->phases[count_phases].constraint = DISSOLVE; } else if (token[0] == 'f') { - inverse_ptr->phases[inverse_ptr->count_phases].force = TRUE; + inverse_ptr->phases[count_phases].force = TRUE; } else if (j == DIGIT) { @@ -1864,11 +1852,11 @@ read_inv_phases(struct inverse *inverse_ptr, const char* cptr) } if (isotopes.size() > 0) { - inverse_ptr->phases[inverse_ptr->count_phases].isotopes = + inverse_ptr->phases[count_phases].isotopes = (struct isotope *) PHRQ_malloc(isotopes.size() * sizeof(struct isotope)); for (size_t i = 0; i < isotopes.size(); i++) { - struct isotope *iso_ptr = &(inverse_ptr->phases[inverse_ptr->count_phases].isotopes[i]); + struct isotope *iso_ptr = &(inverse_ptr->phases[count_phases].isotopes[i]); iso_ptr->isotope_number = isotopes[i].Get_isotope_number(); iso_ptr->elt_name = string_hsave(isotopes[i].Get_elt_name().c_str()); iso_ptr->isotope_name = string_hsave(isotopes[i].Get_isotope_name().c_str()); @@ -1883,14 +1871,13 @@ read_inv_phases(struct inverse *inverse_ptr, const char* cptr) iso_ptr->master = NULL; iso_ptr->primary = NULL; } - inverse_ptr->phases[inverse_ptr->count_phases].count_isotopes = (int) isotopes.size(); + inverse_ptr->phases[count_phases].count_isotopes = (int) isotopes.size(); } else { - inverse_ptr->phases[inverse_ptr->count_phases].isotopes = NULL; - inverse_ptr->phases[inverse_ptr->count_phases].count_isotopes = 0; + inverse_ptr->phases[count_phases].isotopes = NULL; + inverse_ptr->phases[count_phases].count_isotopes = 0; } - inverse_ptr->count_phases++; return (OK); } /* ---------------------------------------------------------------------- */ diff --git a/structures.cpp b/structures.cpp index 5481d3bb..0e422709 100644 --- a/structures.cpp +++ b/structures.cpp @@ -534,42 +534,11 @@ inverse_alloc(void) */ inverse_ptr->description = NULL; inverse_ptr->count_solns = 0; - inverse_ptr->count_isotopes = 0; - inverse_ptr->count_i_u = 0; - inverse_ptr->count_phases = 0; /* * allocate space for pointers in structure to NULL */ inverse_ptr->count_solns = 0; - inverse_ptr->isotopes = (struct inv_isotope *) PHRQ_malloc( - sizeof(struct inv_isotope)); - if (inverse_ptr->isotopes == NULL) - { - malloc_error(); - return inverse_ptr; - } - inverse_ptr->isotopes[0].isotope_name = NULL; - inverse_ptr->isotopes[0].isotope_number = 0; - inverse_ptr->isotopes[0].elt_name = NULL; - - inverse_ptr->i_u = (struct inv_isotope *)PHRQ_malloc(sizeof(struct inv_isotope)); - if (inverse_ptr->i_u == NULL) - { - malloc_error(); - return inverse_ptr; - } - inverse_ptr->i_u[0].isotope_name = NULL; - inverse_ptr->i_u[0].isotope_number = 0; - inverse_ptr->i_u[0].elt_name = NULL; - - inverse_ptr->phases = (struct inv_phases *) PHRQ_malloc( - sizeof(struct inv_phases)); - if (inverse_ptr->phases == NULL) - { - malloc_error(); - return inverse_ptr; - } return (inverse_ptr); } @@ -641,31 +610,28 @@ inverse_free(struct inverse *inverse_ptr) inverse_ptr->elts.clear(); /* Free isotopes */ - for (i = 0; i < inverse_ptr->count_isotopes; i++) + for (i = 0; i < inverse_ptr->isotopes.size(); i++) { inverse_ptr->isotopes[i].uncertainties = (LDBLE *) free_check_null(inverse_ptr->isotopes[i].uncertainties); }; - inverse_ptr->isotopes = - (struct inv_isotope *) free_check_null(inverse_ptr->isotopes); + inverse_ptr->isotopes.clear(); - for (i = 0; i < inverse_ptr->count_i_u; i++) + for (i = 0; i < inverse_ptr->i_u.size(); i++) { inverse_ptr->i_u[i].uncertainties = (LDBLE *) free_check_null(inverse_ptr->i_u[i].uncertainties); }; - inverse_ptr->i_u = - (struct inv_isotope *) free_check_null(inverse_ptr->i_u); + inverse_ptr->i_u.clear(); /* Free phases */ - for (i = 0; i < inverse_ptr->count_phases; i++) + for (i = 0; i < inverse_ptr->phases.size(); i++) { inverse_ptr->phases[i].isotopes = (struct isotope *) free_check_null(inverse_ptr->phases[i]. isotopes); } - inverse_ptr->phases = - (struct inv_phases *) free_check_null(inverse_ptr->phases); + inverse_ptr->phases.clear(); /* Free carbon derivatives */ inverse_ptr->dalk_dph.clear(); diff --git a/tidy.cpp b/tidy.cpp index fbfe4ee8..5ad65966 100644 --- a/tidy.cpp +++ b/tidy.cpp @@ -1197,7 +1197,7 @@ tidy_inverse(void) */ count_elts = 0; paren_count = 0; - for (j = 0; j < inverse[i].count_phases; j++) + for (j = 0; j < inverse[i].phases.size(); j++) { inverse[i].phases[j].phase = phase_bsearch(inverse[i].phases[j].name, &k, FALSE); From 4c848b4e3dba3a36d81c85c7101b78c3a79f3c23 Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Mon, 29 Mar 2021 11:41:43 -0600 Subject: [PATCH 24/53] all inverse structures vectorized. Starting on solver workspace --- Phreeqc.cpp | 2 - Phreeqc.h | 12 ++- global_structures.h | 11 +-- inverse.cpp | 82 ++++++++---------- read.cpp | 200 ++------------------------------------------ structures.cpp | 13 +-- tidy.cpp | 42 ++++------ 7 files changed, 70 insertions(+), 292 deletions(-) diff --git a/Phreeqc.cpp b/Phreeqc.cpp index b53ce5ba..00484fa5 100644 --- a/Phreeqc.cpp +++ b/Phreeqc.cpp @@ -930,8 +930,6 @@ void Phreeqc::init(void) max_row_count = 50; max_column_count = 50; carbon = FALSE; - col_name = NULL; - row_name = NULL; count_rows = 0; count_optimize = 0; col_phases = 0; diff --git a/Phreeqc.h b/Phreeqc.h index ed25afc9..dd58c6ed 100644 --- a/Phreeqc.h +++ b/Phreeqc.h @@ -689,12 +689,9 @@ public: int read_inv_isotopes(struct inverse* inverse_ptr, const char* cptr); int read_inv_phases(struct inverse* inverse_ptr, const char* next_char); int read_kinetics(void); - LDBLE* read_list_doubles(const char** ptr, int* count_doubles); bool read_vector_doubles(const char** ptr, std::vector& v); bool read_vector_ints(const char** cptr, std::vector& v, int positive); bool read_vector_t_f(const char** ptr, std::vector& v); - int* read_list_ints(const char** ptr, int* count_ints, int positive); - int* read_list_t_f(const char** ptr, int* count_ints); int read_master_species(void); int read_mix(void); int read_entity_mix(std::map& mix_map); @@ -1688,11 +1685,12 @@ protected: /* inverse.cpp ------------------------------- */ int max_row_count, max_column_count; int carbon; - const char** col_name, ** row_name; - int count_rows, count_optimize; - int col_phases, col_redox, col_epsilon, col_ph, col_water, + //const char** col_name, ** row_name; + std::vector col_name, row_name; + size_t count_rows, count_optimize; + size_t col_phases, col_redox, col_epsilon, col_ph, col_water, col_isotopes, col_phase_isotopes; - int row_mb, row_fract, row_charge, row_carbon, row_isotopes, + size_t row_mb, row_fract, row_charge, row_carbon, row_isotopes, row_epsilon, row_isotope_epsilon, row_water; LDBLE* inv_zero, * array1, * inv_res, * inv_delta1, * delta2, * delta3, * inv_cu, * delta_save; diff --git a/global_structures.h b/global_structures.h index 62bcdd5f..11cc0890 100644 --- a/global_structures.h +++ b/global_structures.h @@ -341,17 +341,15 @@ struct inv_elts { const char *name; struct master *master; - int row; - int count_uncertainties; - LDBLE *uncertainties; + size_t row; + std::vector uncertainties; }; struct inv_isotope { const char *isotope_name; LDBLE isotope_number; const char *elt_name; - int count_uncertainties; - LDBLE *uncertainties; + std::vector uncertainties; }; struct inv_phases { @@ -360,8 +358,7 @@ struct inv_phases int column; int constraint; int force; - int count_isotopes; - struct isotope *isotopes; + std::vector isotopes; }; struct name_coef { diff --git a/inverse.cpp b/inverse.cpp index d0f4c052..086eac7d 100644 --- a/inverse.cpp +++ b/inverse.cpp @@ -43,8 +43,6 @@ inverse_models(void) inv_cu = NULL; inv_iu = NULL; inv_is = NULL; - col_name = NULL; - row_name = NULL; min_delta = NULL; max_delta = NULL; good = NULL; @@ -269,14 +267,8 @@ setup_inverse(struct inverse *inv_ptr) if (array1 == NULL) malloc_error(); - col_name = - (const char **) PHRQ_malloc((size_t) max_column_count * sizeof(char *)); - if (col_name == NULL) - malloc_error(); - - row_name = (const char **) PHRQ_malloc((size_t) max_row_count * sizeof(char *)); - if (row_name == NULL) - malloc_error(); + col_name.resize(max_column_count); + row_name.resize(max_row_count); delta.resize((size_t)max_column_count); @@ -1329,8 +1321,8 @@ solve_inverse(struct inverse *inv_ptr) inv_cu = (LDBLE *) free_check_null(inv_cu); inv_iu = (int *) free_check_null(inv_iu); inv_is = (int *) free_check_null(inv_is); - col_name = (const char **) free_check_null(col_name); - row_name = (const char **) free_check_null(row_name); + col_name.clear(); + row_name.clear(); col_back = (int *) free_check_null(col_back); row_back = (int *) free_check_null(row_back); min_delta = (LDBLE *) free_check_null(min_delta); @@ -1748,7 +1740,6 @@ print_model(struct inverse *inv_ptr) int print_msg; cxxSolution *solution_ptr; struct master *master_ptr; - struct isotope *isotope_ptr; LDBLE d1, d2, d3, d4; char token[MAX_LENGTH]; /* @@ -1921,24 +1912,24 @@ print_model(struct inverse *inv_ptr) output_msg(sformatf( "\nIsotopic composition of phases:\n")); for (size_t i = 0; i < inv_ptr->phases.size(); i++) { - if (inv_ptr->phases[i].count_isotopes == 0) + if (inv_ptr->phases[i].isotopes.size() == 0) continue; j = col_phases + i; if (equal(inv_delta1[j], 0.0, toler) == TRUE && equal(min_delta[j], 0.0, toler) == TRUE && equal(max_delta[j], 0.0, toler) == TRUE) continue; - isotope_ptr = inv_ptr->phases[i].isotopes; + std::vector& isotope_ref = inv_ptr->phases[i].isotopes; for (j = 0; j < inv_ptr->isotopes.size(); j++) { - for (k = 0; k < inv_ptr->phases[i].count_isotopes; k++) + for (k = 0; k < inv_ptr->phases[i].isotopes.size(); k++) { if (inv_ptr->isotopes[j].elt_name != - isotope_ptr[k].elt_name || + isotope_ref[k].elt_name || inv_ptr->isotopes[j].isotope_number != - isotope_ptr[k].isotope_number) + isotope_ref[k].isotope_number) continue; - d1 = isotope_ptr[k].ratio; + d1 = isotope_ref[k].ratio; column = col_phase_isotopes + i * inv_ptr->isotopes.size() + j; if (inv_delta1[col_phases + i] != 0.0) @@ -1963,16 +1954,16 @@ print_model(struct inverse *inv_ptr) output_msg(sformatf( "%15.15s %12g +%12g =%12g", token, (double) d1, (double) d2, (double) d3)); - if (fabs(d2) > (isotope_ptr[k].ratio_uncertainty + toler)) + if (fabs(d2) > (isotope_ref[k].ratio_uncertainty + toler)) { output_msg(sformatf( " **")); print_msg = TRUE; } output_msg(sformatf( "\n")); - if (isotope_ptr[k].ratio_uncertainty > 0) + if (isotope_ref[k].ratio_uncertainty > 0) { scaled_error += - fabs(d2) / isotope_ptr[k].ratio_uncertainty; + fabs(d2) / isotope_ref[k].ratio_uncertainty; /* debug output_msg(sformatf( "%e\t%e\t%e\n", fabs(d2) / isotope_ptr[k].ratio_uncertainty, fabs(d2), isotope_ptr[k].ratio_uncertainty)); */ @@ -3488,21 +3479,21 @@ isotope_balance_equation(struct inverse *inv_ptr, int row, int n) */ for (i = 0; i < inv_ptr->phases.size(); i++) { - if (inv_ptr->phases[i].count_isotopes <= 0) + if (inv_ptr->phases[i].isotopes.size() == 0) continue; - struct isotope *isotope_ptr = inv_ptr->phases[i].isotopes; - for (j = 0; j < inv_ptr->phases[i].count_isotopes; j++) + std::vector& isotope_ref = inv_ptr->phases[i].isotopes; + for (j = 0; j < inv_ptr->phases[i].isotopes.size(); j++) { - if (isotope_ptr[j].primary == primary_ptr && - isotope_ptr[j].isotope_number == isotope_number) + if (isotope_ref[j].primary == primary_ptr && + isotope_ref[j].isotope_number == isotope_number) { /* term for alpha phase unknowns */ column = col_phases + i; my_array[(size_t)row * (size_t)max_column_count + (size_t)column] = - isotope_ptr[j].ratio * isotope_ptr[j].coef; + isotope_ref[j].ratio * isotope_ref[j].coef; /* term for phase isotope uncertainty unknown */ column = col_phase_isotopes + i * inv_ptr->isotopes.size() + n; - my_array[(size_t)row * (size_t)max_column_count + (size_t)column] = isotope_ptr[j].coef; + my_array[(size_t)row * (size_t)max_column_count + (size_t)column] = isotope_ref[j].coef; break; } } @@ -3698,10 +3689,10 @@ check_isotopes(struct inverse *inv_ptr) i = ii; /* use inverse-defined uncertainties first */ #ifdef NPP - if (j < inv_ptr->i_u[i].count_uncertainties + if (j < inv_ptr->i_u[i].uncertainties.size() && !isnan(inv_ptr->i_u[i].uncertainties[j])) #else - if (j < inv_ptr->i_u[i].count_uncertainties + if (j < inv_ptr->i_u[i].uncertainties.size() && inv_ptr->i_u[i].uncertainties[j] != NAN) #endif { @@ -3711,13 +3702,13 @@ check_isotopes(struct inverse *inv_ptr) } #ifdef NPP else if (inv_ptr->i_u[i].count_uncertainties > 0 - && !isnan(inv_ptr->i_u[i].uncertainties[inv_ptr->i_u[i].count_uncertainties - 1])) + && !isnan(inv_ptr->i_u[i].uncertainties[inv_ptr->i_u[i].uncertainties.size() - 1])) #else - else if (inv_ptr->i_u[i].count_uncertainties > 0 - && inv_ptr->i_u[i].uncertainties[(size_t)inv_ptr->i_u[i].count_uncertainties - 1] != NAN) + else if (inv_ptr->i_u[i].uncertainties.size() > 0 + && inv_ptr->i_u[i].uncertainties[(size_t)inv_ptr->i_u[i].uncertainties.size() - 1] != NAN) #endif { - kit->second.Set_x_ratio_uncertainty(inv_ptr->i_u[i].uncertainties[inv_ptr->i_u[i].count_uncertainties - 1]); + kit->second.Set_x_ratio_uncertainty(inv_ptr->i_u[i].uncertainties[inv_ptr->i_u[i].uncertainties.size() - 1]); /* use solution-defined uncertainties second */ } @@ -3779,7 +3770,7 @@ check_isotopes(struct inverse *inv_ptr) primary_ptr = master_bsearch(inv_ptr->isotopes[i].elt_name); isotope_number = inv_ptr->isotopes[i].isotope_number; found_isotope = FALSE; - for (k = 0; k < inv_ptr->phases[j].count_isotopes; k++) + for (k = 0; k < inv_ptr->phases[j].isotopes.size(); k++) { if (inv_ptr->phases[j].isotopes[k].primary == primary_ptr && inv_ptr->phases[j].isotopes[k].isotope_number == @@ -3834,10 +3825,10 @@ phase_isotope_inequalities(struct inverse *inv_ptr) return OK; for (i = 0; i < inv_ptr->phases.size(); i++) { - if (inv_ptr->phases[i].count_isotopes <= 0) + if (inv_ptr->phases[i].isotopes.size() == 0) continue; - for (j = 0; j < inv_ptr->phases[i].count_isotopes; j++) + for (j = 0; j < inv_ptr->phases[i].isotopes.size(); j++) { /* find index number */ for (k = 0; k < inv_ptr->isotopes.size(); k++) @@ -4360,7 +4351,6 @@ dump_netpath_pat(struct inverse *inv_ptr) int count_unknowns_save, max_row_count_save, max_column_count_save, temp, count_current_solutions, temp_punch; int solnmap[10][2]; - struct isotope *isotope_ptr; FILE *model_file; struct elt_list *next_elt; int exch, column; @@ -5050,15 +5040,15 @@ dump_netpath_pat(struct inverse *inv_ptr) * Add isotopes */ - for (k = 0; k < inv_ptr->phases[i].count_isotopes; k++) + for (k = 0; k < inv_ptr->phases[i].isotopes.size(); k++) { - isotope_ptr = inv_ptr->phases[i].isotopes; - d1 = isotope_ptr[k].ratio; + std::vector& isotope_ref = inv_ptr->phases[i].isotopes; + d1 = isotope_ref[k].ratio; for (j = 0; j < inv_ptr->isotopes.size(); j++) { - if ((inv_ptr->isotopes[j].elt_name != isotope_ptr[k].elt_name) + if ((inv_ptr->isotopes[j].elt_name != isotope_ref[k].elt_name) || (inv_ptr->isotopes[j].isotope_number != - isotope_ptr[k].isotope_number)) + isotope_ref[k].isotope_number)) continue; break; } @@ -5072,8 +5062,8 @@ dump_netpath_pat(struct inverse *inv_ptr) } } d3 = d1 + d2; - string = sformatf("%d%s", (int) isotope_ptr[k].isotope_number, - isotope_ptr[k].elt_name); + string = sformatf("%d%s", (int)isotope_ref[k].isotope_number, + isotope_ref[k].elt_name); if (strcmp(string.c_str(), "13C") == 0) fprintf(model_file, " %-2s%12.7f", "I1", (double) d3); if (strcmp(string.c_str(), "14C") == 0) diff --git a/read.cpp b/read.cpp index 9e7436ab..407a0b45 100644 --- a/read.cpp +++ b/read.cpp @@ -1622,7 +1622,7 @@ int Phreeqc:: read_inv_balances(struct inverse *inverse_ptr, const char* cptr) /* ---------------------------------------------------------------------- */ { - int j, l, count; + int j, l; char token[MAX_LENGTH]; /* * Read element name @@ -1647,10 +1647,7 @@ read_inv_balances(struct inverse *inverse_ptr, const char* cptr) /* * Read element uncertainties */ - inverse_ptr->elts[count_elts].uncertainties = - read_list_doubles(&cptr, &count); - inverse_ptr->elts[count_elts].count_uncertainties = - count; + read_vector_doubles(&cptr, inverse_ptr->elts[count_elts].uncertainties); } else if (strcmp_nocase_arg1(token, "ph") == 0) { @@ -1665,7 +1662,7 @@ int Phreeqc:: read_inv_isotopes(struct inverse *inverse_ptr, const char* cptr) /* ---------------------------------------------------------------------- */ { - int i, j, l, l1, l2, count; + int i, j, l, l1, l2; LDBLE isotope_number; char token[MAX_LENGTH], token1[MAX_LENGTH]; const char* cptr1, *ptr2; @@ -1728,11 +1725,7 @@ read_inv_isotopes(struct inverse *inverse_ptr, const char* cptr) inverse_ptr->isotopes.resize(count_isotopes + 1); inverse_ptr->isotopes[count_isotopes].isotope_number = isotope_number; inverse_ptr->isotopes[count_isotopes].elt_name = element_name; - inverse_ptr->isotopes[count_isotopes].uncertainties = - (LDBLE *) PHRQ_malloc((size_t) sizeof(LDBLE)); - if (inverse_ptr->isotopes[count_isotopes]. - uncertainties == NULL) - malloc_error(); + inverse_ptr->isotopes[count_isotopes].uncertainties.clear(); } /* * add redox state name to inv_ptr->i_u @@ -1744,9 +1737,7 @@ read_inv_isotopes(struct inverse *inverse_ptr, const char* cptr) /* * Read isotope uncertainties */ - inverse_ptr->i_u[count_i_u].uncertainties = - read_list_doubles(&cptr1, &count); - inverse_ptr->i_u[count_i_u].count_uncertainties = count; + read_vector_doubles(&cptr1, inverse_ptr->i_u[count_i_u].uncertainties); return (OK); } /* ---------------------------------------------------------------------- */ @@ -1852,8 +1843,7 @@ read_inv_phases(struct inverse *inverse_ptr, const char* cptr) } if (isotopes.size() > 0) { - inverse_ptr->phases[count_phases].isotopes = - (struct isotope *) PHRQ_malloc(isotopes.size() * sizeof(struct isotope)); + inverse_ptr->phases[count_phases].isotopes.resize(isotopes.size()); for (size_t i = 0; i < isotopes.size(); i++) { struct isotope *iso_ptr = &(inverse_ptr->phases[count_phases].isotopes[i]); @@ -1871,12 +1861,10 @@ read_inv_phases(struct inverse *inverse_ptr, const char* cptr) iso_ptr->master = NULL; iso_ptr->primary = NULL; } - inverse_ptr->phases[count_phases].count_isotopes = (int) isotopes.size(); } else { - inverse_ptr->phases[count_phases].isotopes = NULL; - inverse_ptr->phases[count_phases].count_isotopes = 0; + inverse_ptr->phases[count_phases].isotopes.clear(); } return (OK); } @@ -2394,59 +2382,6 @@ read_kinetics(void) return (return_value); } /* ---------------------------------------------------------------------- */ -LDBLE * Phreeqc:: -read_list_doubles(const char **cptr, int *count_doubles) -/* ---------------------------------------------------------------------- */ -{ -/* - * Reads a list of LDBLE numbers until end of line is reached or - * a LDBLE cannot be read from a token. - * - * Arguments: - * cptr entry: points to line to read from - * exit: points to next non-LDBLE token or end of line - * - * count_doubles exit: number of LDBLEs read - * - * Returns: - * pointer to a list of count_doubles LDBLEs. - */ - - LDBLE *LDBLE_list; - char token[MAX_LENGTH]; - LDBLE value; - const char* cptr_save; - int l; - - LDBLE_list = (LDBLE *) PHRQ_malloc(sizeof(LDBLE)); - if (LDBLE_list == NULL) - malloc_error(); - *count_doubles = 0; - - cptr_save = *cptr; - while (copy_token(token, cptr, &l) != EMPTY) - { - if (sscanf(token, SCANFORMAT, &value) == 1) - { - *count_doubles = *count_doubles + 1; - LDBLE_list = - (LDBLE *) PHRQ_realloc(LDBLE_list, - (size_t) (*count_doubles) * - sizeof(LDBLE)); - if (LDBLE_list == NULL) - malloc_error(); - LDBLE_list[(*count_doubles) - 1] = value; - cptr_save = *cptr; - } - else - { - *cptr = cptr_save; - break; - } - } - return (LDBLE_list); -} -/* ---------------------------------------------------------------------- */ bool Phreeqc:: read_vector_doubles(const char** cptr, std::vector& v) /* ---------------------------------------------------------------------- */ @@ -2464,68 +2399,6 @@ read_vector_doubles(const char** cptr, std::vector& v) return true; } /* ---------------------------------------------------------------------- */ -int * Phreeqc:: -read_list_ints(const char **cptr, int *count_ints, int positive) -/* ---------------------------------------------------------------------- */ -{ -/* - * Reads a list of int numbers until end of line is reached or - * an int cannot be read from a token. - * - * Arguments: - * cptr entry: points to line to read from - * exit: points to next non-int token or end of line - * - * count_ints exit: number of LDBLEs read - * - * positive entry: if TRUE, expects to read only positive integers - * - * Returns: - * pointer to a list of count_ints ints. - */ - int *int_list; - char token[MAX_LENGTH]; - int value; - int l; - const char* cptr_save; - - int_list = (int *) PHRQ_malloc(sizeof(int)); - if (int_list == NULL) - malloc_error(); - *count_ints = 0; - - cptr_save = *cptr; - while (copy_token(token, cptr, &l) != EMPTY) - { - if (sscanf(token, "%d", &value) == 1) - { - (*count_ints)++; - int_list = - (int *) PHRQ_realloc(int_list, - (size_t) (*count_ints) * sizeof(int)); - if (int_list == NULL) - { - malloc_error(); - return (NULL); - } - int_list[(*count_ints) - 1] = value; - if (value <= 0 && positive == TRUE) - { - error_msg("Expected an integer greater than zero.", CONTINUE); - error_msg(line_save, CONTINUE); - input_error++; - } - cptr_save = *cptr; - } - else - { - *cptr = cptr_save; - break; - } - } - return (int_list); -} -/* ---------------------------------------------------------------------- */ bool Phreeqc:: read_vector_ints(const char** cptr, std::vector& v, int positive) /* ---------------------------------------------------------------------- */ @@ -2659,65 +2532,6 @@ read_list_ints_range(const char **cptr, int *count_ints, int positive, int *int_ return (int_list); } -/* ---------------------------------------------------------------------- */ -int * Phreeqc:: -read_list_t_f(const char **cptr, int *count_ints) -/* ---------------------------------------------------------------------- */ -{ -/* - * Reads a list of true and false until end of line is reached or - * until non- t or f is found - * - * Arguments: - * cptr entry: points to line to read from - * exit: points to next non-int token or end of line - * - * count_ints exit: number of LDBLEs read - * - * positive entry: if TRUE, expects to read only positive integers - * - * Returns: - * pointer to a list of count_ints ints. - */ - int *int_list; - char token[MAX_LENGTH]; - int value; - int l; - - int_list = (int *) PHRQ_malloc(sizeof(int)); - if (int_list == NULL) - malloc_error(); - *count_ints = 0; - - while (copy_token(token, cptr, &l) != EMPTY) - { - str_tolower(token); - if (token[0] == 't') - { - value = TRUE; - } - else if (token[0] == 'f') - { - value = FALSE; - } - else - { - error_msg("Expected TRUE or FALSE.", CONTINUE); - error_msg(line_save, CONTINUE); - input_error++; - break; - } - (*count_ints)++; - int_list = - (int *) PHRQ_realloc(int_list, - (size_t) (*count_ints) * sizeof(int)); - if (int_list == NULL) - malloc_error(); - int_list[(*count_ints) - 1] = value; - } - return (int_list); -} - /* ---------------------------------------------------------------------- */ bool Phreeqc:: read_vector_t_f(const char** cptr, std::vector& v) diff --git a/structures.cpp b/structures.cpp index 0e422709..c558f6f4 100644 --- a/structures.cpp +++ b/structures.cpp @@ -604,32 +604,27 @@ inverse_free(struct inverse *inverse_ptr) /* Free elts */ for (i = 0; i < inverse_ptr->elts.size(); i++) { - inverse_ptr->elts[i].uncertainties = - (LDBLE *) free_check_null(inverse_ptr->elts[i].uncertainties); + inverse_ptr->elts[i].uncertainties.clear(); }; inverse_ptr->elts.clear(); /* Free isotopes */ for (i = 0; i < inverse_ptr->isotopes.size(); i++) { - inverse_ptr->isotopes[i].uncertainties = - (LDBLE *) free_check_null(inverse_ptr->isotopes[i].uncertainties); + inverse_ptr->isotopes[i].uncertainties.clear(); }; inverse_ptr->isotopes.clear(); for (i = 0; i < inverse_ptr->i_u.size(); i++) { - inverse_ptr->i_u[i].uncertainties = - (LDBLE *) free_check_null(inverse_ptr->i_u[i].uncertainties); + inverse_ptr->i_u[i].uncertainties.clear(); }; inverse_ptr->i_u.clear(); /* Free phases */ for (i = 0; i < inverse_ptr->phases.size(); i++) { - inverse_ptr->phases[i].isotopes = - (struct isotope *) free_check_null(inverse_ptr->phases[i]. - isotopes); + inverse_ptr->phases[i].isotopes.clear(); } inverse_ptr->phases.clear(); diff --git a/tidy.cpp b/tidy.cpp index 5ad65966..0695c1ca 100644 --- a/tidy.cpp +++ b/tidy.cpp @@ -1163,13 +1163,9 @@ tidy_inverse(void) error_msg(error_string, CONTINUE); continue; } - inverse[i].elts[j].uncertainties = - (LDBLE *) PHRQ_realloc(inverse[i].elts[j].uncertainties, - (size_t) inverse[i].count_solns * - sizeof(LDBLE)); - if (inverse[i].elts[j].uncertainties == NULL) - malloc_error(); - if (inverse[i].elts[j].count_uncertainties == 0) + size_t count_uncertainties = inverse[i].elts[j].uncertainties.size(); + inverse[i].elts[j].uncertainties.resize((size_t)inverse[i].count_solns); + if (count_uncertainties == 0) { /* use default uncertainties for element */ for (k = 0; k < inverse[i].count_solns; k++) @@ -1178,15 +1174,11 @@ tidy_inverse(void) inverse[i].uncertainties[k]; } } - else if (inverse[i].elts[j].count_uncertainties < - inverse[i].count_solns) + else if (count_uncertainties < inverse[i].count_solns) { /* use input uncertainties, fill in any missing at end */ - value = - inverse[i].elts[j].uncertainties[inverse[i].elts[j]. - count_uncertainties - 1]; - for (k = inverse[i].elts[j].count_uncertainties; - k < inverse[i].count_solns; k++) + value = inverse[i].elts[j].uncertainties[count_uncertainties - 1]; + for (k = count_uncertainties; k < inverse[i].count_solns; k++) { inverse[i].elts[j].uncertainties[k] = value; } @@ -1212,9 +1204,9 @@ tidy_inverse(void) /* * Find isotope elements */ - if (inverse[i].phases[j].count_isotopes > 0) + if (inverse[i].phases[j].isotopes.size() > 0) { - for (k = 0; k < inverse[i].phases[j].count_isotopes; k++) + for (k = 0; k < inverse[i].phases[j].isotopes.size(); k++) { inverse[i].phases[j].isotopes[k].primary = NULL; inverse[i].phases[j].isotopes[k].master = NULL; @@ -1265,9 +1257,9 @@ tidy_inverse(void) continue; } } - qsort(inverse[i].phases[j].isotopes, - (size_t) inverse[i].phases[j].count_isotopes, - (size_t) sizeof(struct isotope), isotope_compare); + qsort(&inverse[i].phases[j].isotopes[0], + inverse[i].phases[j].isotopes.size(), + sizeof(struct isotope), isotope_compare); } add_elt_list(inverse[i].phases[j].phase->next_elt, 1.0); @@ -1357,11 +1349,7 @@ tidy_inverse(void) /* set master */ inv_elts[count_in].master = master[j]; /* alloc uncertainties and set default */ - inv_elts[count_in].uncertainties = - (LDBLE *) PHRQ_malloc((size_t) inverse[i].count_solns * - sizeof(LDBLE)); - if (inv_elts[count_in].uncertainties == NULL) - malloc_error(); + inv_elts[count_in].uncertainties.resize((size_t)inverse[i].count_solns); for (k = 0; k < inverse[i].count_solns; k++) { inv_elts[count_in].uncertainties[k] = @@ -1407,8 +1395,7 @@ tidy_inverse(void) } } } - inverse[i].elts[j].uncertainties = - (LDBLE *) free_check_null(inverse[i].elts[j].uncertainties); + inverse[i].elts[j].uncertainties.clear(); } /* copy masters that are not primary redox */ for (j = 0; j < inverse[i].elts.size(); j++) @@ -1437,8 +1424,7 @@ tidy_inverse(void) break; } } - inverse[i].elts[j].uncertainties = - (LDBLE *) free_check_null(inverse[i].elts[j].uncertainties); + inverse[i].elts[j].uncertainties.clear(); } /* * replace elts in inverse struct From b5c7ba4a8342a9e3a96723dfefe99493123ac430 Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Mon, 29 Mar 2021 14:48:51 -0600 Subject: [PATCH 25/53] going to work on warnings --- Phreeqc.cpp | 17 ---- Phreeqc.h | 23 +++-- basicsubs.cpp | 2 +- global_structures.h | 4 +- inverse.cpp | 213 +++++++++++++------------------------------- tidy.cpp | 6 +- 6 files changed, 77 insertions(+), 188 deletions(-) diff --git a/Phreeqc.cpp b/Phreeqc.cpp index 00484fa5..d9c5eca3 100644 --- a/Phreeqc.cpp +++ b/Phreeqc.cpp @@ -947,18 +947,6 @@ void Phreeqc::init(void) row_epsilon = 0; row_isotope_epsilon = 0; row_water = 0; - inv_zero = NULL; - array1 = 0; - inv_res = NULL; - inv_delta1 = NULL; - delta2 = NULL; - delta3 = NULL; - inv_cu = NULL; - delta_save = NULL; - min_delta = NULL; - max_delta = NULL; - inv_iu = NULL; - inv_is = NULL; klmd = 0; nklmd = 0; n2d = 0; @@ -969,11 +957,6 @@ void Phreeqc::init(void) max_pct = 0; scaled_error = 0; master_alk = NULL; - row_back = NULL; - col_back = NULL; - good = NULL; - bad = NULL; - minimal = NULL; max_good = 0; max_bad = 0; max_minimal = 0; diff --git a/Phreeqc.h b/Phreeqc.h index dd58c6ed..108446de 100644 --- a/Phreeqc.h +++ b/Phreeqc.h @@ -1431,8 +1431,8 @@ protected: * Unknowns *---------------------------------------------------------------------- */ std::vector x; - int count_unknowns; - int max_unknowns; + size_t count_unknowns; + size_t max_unknowns; struct unknown* ah2o_unknown; struct unknown* alkalinity_unknown; @@ -1683,26 +1683,25 @@ protected: LDBLE z_global, xd_global, alpha_global; /* inverse.cpp ------------------------------- */ - int max_row_count, max_column_count; + size_t max_row_count, max_column_count; int carbon; - //const char** col_name, ** row_name; std::vector col_name, row_name; size_t count_rows, count_optimize; size_t col_phases, col_redox, col_epsilon, col_ph, col_water, col_isotopes, col_phase_isotopes; size_t row_mb, row_fract, row_charge, row_carbon, row_isotopes, row_epsilon, row_isotope_epsilon, row_water; - LDBLE* inv_zero, * array1, * inv_res, * inv_delta1, * delta2, * delta3, * inv_cu, - * delta_save; - LDBLE* min_delta, * max_delta; - int* inv_iu, * inv_is; - int klmd, nklmd, n2d; + std::vector inv_zero, array1, inv_res, inv_delta1, delta2, + delta3, inv_cu, delta_save; + std::vector min_delta, max_delta; + std::vector inv_iu, inv_is; + size_t klmd, nklmd, n2d; int kode, iter; LDBLE toler, error, max_pct, scaled_error; struct master* master_alk; - int* row_back, * col_back; - unsigned long* good, * bad, * minimal; - int max_good, max_bad, max_minimal; + std::vector row_back, col_back; + std::vector good, bad, minimal; + size_t max_good, max_bad, max_minimal; int count_good, count_bad, count_minimal, count_calls; unsigned long soln_bits, phase_bits, current_bits, temp_bits; FILE* netpath_file; diff --git a/basicsubs.cpp b/basicsubs.cpp index 084ba22a..5f08f9c0 100644 --- a/basicsubs.cpp +++ b/basicsubs.cpp @@ -2484,7 +2484,7 @@ total(const char *total_name) else { t = 0; - for (i = master_ptr->number + 1; + for (size_t i = master_ptr->number + 1; (i < (int)master.size() && master[i]->elt->primary == master_ptr); i++) { diff --git a/global_structures.h b/global_structures.h index 11cc0890..712ee195 100644 --- a/global_structures.h +++ b/global_structures.h @@ -665,7 +665,7 @@ struct phase struct master { /* list of name and number of elements in an equation */ int in; /* TRUE if in model, FALSE if out, REWRITE if other mb eq */ - int number; /* sequence number in list of masters */ + size_t number; /* sequence number in list of masters */ int last_model; /* saved to determine if model has changed */ int type; /* AQ or EX */ int primary; /* TRUE if master species is primary */ @@ -701,7 +701,7 @@ struct unknown LDBLE sum; LDBLE delta; LDBLE la; - int number; + size_t number; const char *description; std::vector master; struct phase *phase; diff --git a/inverse.cpp b/inverse.cpp index 086eac7d..4e31819b 100644 --- a/inverse.cpp +++ b/inverse.cpp @@ -26,28 +26,6 @@ inverse_models(void) if (count_inverse <= 0) return OK; // Revert to previous headings after inverse modeling std::vector old_headings; - //int i; - - //for (i = 0; i < user_punch_count_headings; i++) - //{ - // old_headings.push_back(user_punch_headings[i]); - //} - - array1 = NULL; - inv_zero = NULL; - inv_res = NULL; - inv_delta1 = NULL; - delta2 = NULL; - delta3 = NULL; - delta_save = NULL; - inv_cu = NULL; - inv_iu = NULL; - inv_is = NULL; - min_delta = NULL; - max_delta = NULL; - good = NULL; - bad = NULL; - minimal = NULL; state = INVERSE; dl_type_x = cxxSurface::NO_DL; @@ -154,9 +132,9 @@ setup_inverse(struct inverse *inv_ptr) * Fill in array for an inverse problem */ int i, j, k, i_alk, i_carb; - int max; - int count_rows_t; - int column, row; + size_t max; + size_t count_rows_t; + size_t column, row; int temp; LDBLE isotope_number; LDBLE f, coef, cb, conc; @@ -260,48 +238,18 @@ setup_inverse(struct inverse *inv_ptr) /* * Malloc space for arrays */ - my_array.resize((size_t)max_column_count * (size_t)max_row_count); - - array1 = (LDBLE *) PHRQ_malloc((size_t) max_column_count * max_row_count * - sizeof(LDBLE)); - if (array1 == NULL) - malloc_error(); - + my_array.resize(max_column_count * max_row_count); + array1.resize(max_column_count * max_row_count); col_name.resize(max_column_count); row_name.resize(max_row_count); - - delta.resize((size_t)max_column_count); - - inv_delta1 = (LDBLE *) PHRQ_malloc((size_t) max_column_count * sizeof(LDBLE)); - if (inv_delta1 == NULL) - malloc_error(); - - delta2 = (LDBLE *) PHRQ_malloc((size_t) max_column_count * sizeof(LDBLE)); - if (delta2 == NULL) - malloc_error(); - - delta3 = (LDBLE *) PHRQ_malloc((size_t) max_column_count * sizeof(LDBLE)); - if (delta3 == NULL) - malloc_error(); - - delta_save = - (LDBLE *) PHRQ_malloc((size_t) max_column_count * sizeof(LDBLE)); - if (delta_save == NULL) - malloc_error(); - - min_delta = - (LDBLE *) PHRQ_malloc((size_t) max_column_count * sizeof(LDBLE)); - if (min_delta == NULL) - malloc_error(); - - max_delta = - (LDBLE *) PHRQ_malloc((size_t) max_column_count * sizeof(LDBLE)); - if (max_delta == NULL) - malloc_error(); - - inv_res = (LDBLE *) PHRQ_malloc((size_t) max_row_count * sizeof(LDBLE)); - if (inv_res == NULL) - malloc_error(); + delta.resize(max_column_count); + inv_delta1.resize(max_column_count); + delta2.resize(max_column_count); + delta3.resize(max_column_count); + delta_save.resize(max_column_count); + min_delta.resize(max_column_count); + max_delta.resize(max_column_count); + inv_res.resize(max_row_count); if (max_column_count < max_row_count) { @@ -311,9 +259,7 @@ setup_inverse(struct inverse *inv_ptr) { max = max_column_count; } - inv_zero = (LDBLE *) PHRQ_malloc((size_t) max * sizeof(LDBLE)); - if (inv_zero == NULL) - malloc_error(); + inv_zero.resize((size_t) max); /* * Define inv_zero and inv_zero array, delta */ @@ -1066,48 +1012,25 @@ solve_inverse(struct inverse *inv_ptr) max_bad = MAX_MODELS; max_minimal = MAX_MODELS; - good = - (unsigned long *) PHRQ_malloc((size_t) max_good * - sizeof(unsigned long)); - if (good == NULL) - malloc_error(); + good.resize(max_good); count_good = 0; - bad = - (unsigned long *) PHRQ_malloc((size_t) max_bad * - sizeof(unsigned long)); - if (bad == NULL) - malloc_error(); + bad.resize(max_bad); count_bad = 0; - minimal = - (unsigned long *) PHRQ_malloc((size_t) max_minimal * - sizeof(unsigned long)); - if (minimal == NULL) - malloc_error(); + minimal.resize(max_minimal); count_minimal = 0; - col_back = (int *) PHRQ_malloc((size_t) max_column_count * sizeof(int)); - if (col_back == NULL) - malloc_error(); - - row_back = (int *) PHRQ_malloc((size_t) max_row_count * sizeof(int)); - if (row_back == NULL) - malloc_error(); + col_back.resize(max_column_count); + row_back.resize(max_row_count); /* * Allocate space for arrays */ - inv_cu = (LDBLE *) PHRQ_malloc((size_t) 2 * nklmd * sizeof(LDBLE)); - if (inv_cu == NULL) - malloc_error(); - memset(inv_cu, 0, ((2 * (size_t)nklmd * sizeof(LDBLE)))); - inv_iu = (int *) PHRQ_malloc(2 * (size_t)nklmd * sizeof(int)); - if (inv_iu == NULL) - malloc_error(); - inv_is = (int *) PHRQ_malloc((size_t) klmd * sizeof(int)); - if (inv_is == NULL) - malloc_error(); + inv_cu.resize( 2 * (size_t)nklmd); + memset(&inv_cu[0], 0, ((2 * nklmd * sizeof(LDBLE)))); + inv_iu.resize(2 * nklmd); + inv_is.resize(klmd); for (i = 0; i < 79; i++) token[i] = '='; @@ -1124,7 +1047,7 @@ solve_inverse(struct inverse *inv_ptr) ("For inverse modeling, sum of initial solutions and phases must be <= 32.\n\tFor all reasonable calculations, the sum should be much less than 32.", STOP); } - for (i = inv_ptr->count_solns; i > 0; i--) + for (size_t i = inv_ptr->count_solns; i > 0; i--) { temp_bits = 1 << (i - 1); soln_bits += temp_bits; @@ -1140,9 +1063,8 @@ solve_inverse(struct inverse *inv_ptr) * All combinations of solutions */ first = TRUE; - for (; - get_bits(soln_bits, inv_ptr->count_solns - 2, - inv_ptr->count_solns - 1) > 0; soln_bits--) + for (; get_bits(soln_bits, inv_ptr->count_solns - 2, + inv_ptr->count_solns - 1) > 0; soln_bits--) { /* * Loop through all models of of descending size @@ -1311,25 +1233,25 @@ solve_inverse(struct inverse *inv_ptr) } my_array.clear(); delta.clear(); - array1 = (LDBLE *) free_check_null(array1); - inv_zero = (LDBLE *) free_check_null(inv_zero); - inv_res = (LDBLE *) free_check_null(inv_res); - inv_delta1 = (LDBLE *) free_check_null(inv_delta1); - delta2 = (LDBLE *) free_check_null(delta2); - delta3 = (LDBLE *) free_check_null(delta3); - delta_save = (LDBLE *) free_check_null(delta_save); - inv_cu = (LDBLE *) free_check_null(inv_cu); - inv_iu = (int *) free_check_null(inv_iu); - inv_is = (int *) free_check_null(inv_is); + array1.clear(); + inv_zero.clear(); + inv_res.clear(); + inv_delta1.clear(); + delta2.clear(); + delta3.clear(); + delta_save.clear(); + inv_cu.clear(); + inv_iu.clear(); + inv_is.clear(); col_name.clear(); row_name.clear(); - col_back = (int *) free_check_null(col_back); - row_back = (int *) free_check_null(row_back); - min_delta = (LDBLE *) free_check_null(min_delta); - max_delta = (LDBLE *) free_check_null(max_delta); - good = (unsigned long *) free_check_null(good); - bad = (unsigned long *) free_check_null(bad); - minimal = (unsigned long *) free_check_null(minimal); + col_back.clear(); + row_back.clear(); + min_delta.clear(); + max_delta.clear(); + good.clear(); + bad.clear(); + minimal.clear(); return (OK); } @@ -1439,8 +1361,8 @@ solve_with_mask(struct inverse *inv_ptr, unsigned long cur_bits) memcpy((void *) &(delta_save[0]), (void *) &(inv_zero[0]), (size_t) max_column_count * sizeof(LDBLE)); - shrink(inv_ptr, &my_array[0], array1, - &k, &l, &m, &n, cur_bits, delta2, col_back, row_back); + shrink(inv_ptr, &my_array[0], &array1[0], + &k, &l, &m, &n, cur_bits, &delta2[0], &col_back[0], &row_back[0]); /* * Save delta constraints */ @@ -1467,7 +1389,7 @@ solve_with_mask(struct inverse *inv_ptr, unsigned long cur_bits) } output_msg(sformatf( "\nA and B arrays:\n\n")); - array_print(array1, k + l + m, n + 1, max_column_count); + array_print(&array1[0], k + l + m, n + 1, max_column_count); output_msg(sformatf( "\nInput delta vector:\n")); for (i = 0; i < n; i++) @@ -1518,8 +1440,8 @@ solve_with_mask(struct inverse *inv_ptr, unsigned long cur_bits) } #else cl1(k, l, m, n, - nklmd, n2d, array1, - &kode, toler, &iter, delta2, inv_res, &error, inv_cu, inv_iu, inv_is, TRUE); + nklmd, n2d, &array1[0], + &kode, toler, &iter, &delta2[0], &inv_res[0], &error, &inv_cu[0], &inv_iu[0], &inv_is[0], TRUE); #endif if (kode == 3) { @@ -1591,12 +1513,7 @@ save_minimal(unsigned long bits) if (count_minimal >= max_minimal) { max_minimal *= 2; - minimal = - (unsigned long *) PHRQ_realloc(minimal, - (size_t) max_minimal * - sizeof(unsigned long)); - if (minimal == NULL) - malloc_error(); + minimal.resize(max_minimal); } return (TRUE); } @@ -1614,12 +1531,7 @@ save_good(unsigned long bits) if (count_good >= max_good) { max_good *= 2; - good = - (unsigned long *) PHRQ_realloc(good, - (size_t) max_good * - sizeof(unsigned long)); - if (good == NULL) - malloc_error(); + good.resize(max_good); } return (TRUE); } @@ -1637,12 +1549,7 @@ save_bad(unsigned long bits) if (count_bad >= max_bad) { max_bad *= 2; - bad = - (unsigned long *) PHRQ_realloc(bad, - (size_t) max_bad * - sizeof(unsigned long)); - if (bad == NULL) - malloc_error(); + bad.resize(max_bad); } return (TRUE); } @@ -2502,8 +2409,8 @@ range(struct inverse *inv_ptr, unsigned long cur_bits) { array1[n] = fabs(inv_ptr->range_max); } - shrink(inv_ptr, array1, array1, - &k, &l, &m, &n, cur_bits, delta2, col_back, row_back); + shrink(inv_ptr, &array1[0], &array1[0], + &k, &l, &m, &n, cur_bits, &delta2[0], &col_back[0], &row_back[0]); /* * Save delta constraints */ @@ -2520,7 +2427,7 @@ range(struct inverse *inv_ptr, unsigned long cur_bits) col_name[col_back[j]], (double) delta2[j])); } output_msg(sformatf( "\nA and B arrays:\n\n")); - array_print(array1, k + l + m, n + 1, max_column_count); + array_print(&array1[0], k + l + m, n + 1, max_column_count); } kode = 1; iter = 200; @@ -2543,8 +2450,8 @@ range(struct inverse *inv_ptr, unsigned long cur_bits) } #else cl1(k, l, m, n, - nklmd, n2d, array1, - &kode, toler, &iter, delta2, inv_res, &error2, inv_cu, inv_iu, inv_is, TRUE); + nklmd, n2d, &array1[0], + &kode, toler, &iter, &delta2[0], &inv_res[0], &error2, &inv_cu[0], &inv_iu[0], &inv_is[0], TRUE); #endif if (kode != 0) { @@ -2982,8 +2889,8 @@ check_solns(struct inverse *inv_ptr) * are which */ - shrink(inv_ptr, array1, array1, - &k, &l, &m, &n, bits, delta2, col_back, row_back); + shrink(inv_ptr, &array1[0], &array1[0], + &k, &l, &m, &n, bits, &delta2[0], &col_back[0], &row_back[0]); /* Debug output_msg(sformatf( "\nColumns\n")); @@ -3011,8 +2918,8 @@ check_solns(struct inverse *inv_ptr) iter = 200; count_calls++; cl1(k, l, m, n, - nklmd, n2d, array1, - &kode, toler, &iter, delta2, inv_res, &error2, inv_cu, inv_iu, inv_is, TRUE); + nklmd, n2d, &array1[0], + &kode, toler, &iter, &delta2[0], &inv_res[0], &error2, &inv_cu[0], &inv_iu[0], &inv_is[0], TRUE); if (kode != 0) { diff --git a/tidy.cpp b/tidy.cpp index 0695c1ca..2d19afc9 100644 --- a/tidy.cpp +++ b/tidy.cpp @@ -1178,7 +1178,7 @@ tidy_inverse(void) { /* use input uncertainties, fill in any missing at end */ value = inverse[i].elts[j].uncertainties[count_uncertainties - 1]; - for (k = count_uncertainties; k < inverse[i].count_solns; k++) + for (size_t k = count_uncertainties; k < inverse[i].count_solns; k++) { inverse[i].elts[j].uncertainties[k] = value; } @@ -2897,7 +2897,7 @@ tidy_isotopes(void) LDBLE isotope_number; struct master *master_ptr, *primary_ptr; - int primary_number = 0; + size_t primary_number = 0; primary_ptr = NULL; std::map::iterator it; for (it = Rxn_solution_map.begin(); it != Rxn_solution_map.end(); it++) @@ -2983,7 +2983,7 @@ tidy_isotopes(void) /* for primary, fill in ratio for all secondary species */ if (master_ptr->primary == TRUE && master_ptr->s->secondary != NULL) { - for (int k = primary_number + 1; k < (int)master.size(); k++) + for (size_t k = primary_number + 1; k < (int)master.size(); k++) { if (master[k]->elt->primary != primary_ptr) break; From 1547d91bf4e5db0c8f5cf7924cb6ae5b1689c2c9 Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Mon, 29 Mar 2021 18:44:24 -0600 Subject: [PATCH 26/53] finished up spread --- Phreeqc.h | 2 +- basicsubs.cpp | 1 - global_structures.h | 9 +- spread.cpp | 210 ++++++++------------------------------------ 4 files changed, 44 insertions(+), 178 deletions(-) diff --git a/Phreeqc.h b/Phreeqc.h index 108446de..f1c4b96a 100644 --- a/Phreeqc.h +++ b/Phreeqc.h @@ -773,7 +773,7 @@ public: // spread.cpp ------------------------------- int read_solution_spread(void); - int copy_token_tab(char* token_ptr, const char** ptr, int* length); + int copy_token_tab(std::string& token, const char** cptr); int get_option_string(const char** opt_list, int count_opt_list, const char** next_char); int spread_row_free(struct spread_row* spread_row_ptr); diff --git a/basicsubs.cpp b/basicsubs.cpp index 5f08f9c0..a09c81a7 100644 --- a/basicsubs.cpp +++ b/basicsubs.cpp @@ -2438,7 +2438,6 @@ total(const char *total_name) { struct master *master_ptr; LDBLE t; - int i; if (strcmp(total_name, "H") == 0) { diff --git a/global_structures.h b/global_structures.h index 712ee195..688d4fc1 100644 --- a/global_structures.h +++ b/global_structures.h @@ -817,9 +817,9 @@ struct spread_row { int count; int empty, string, number; - char **char_vector; - LDBLE *d_vector; - int *type_vector; + std::vector char_vector; + std::vector d_vector; + std::vector type_vector; }; struct defaults { @@ -831,8 +831,7 @@ struct defaults LDBLE ph; LDBLE pe; LDBLE water; - int count_iso; - struct iso *iso; + std::vector iso; LDBLE pressure; /* pressure in atm */ }; struct spread_sheet diff --git a/spread.cpp b/spread.cpp index eb87f402..4ba5578c 100644 --- a/spread.cpp +++ b/spread.cpp @@ -79,14 +79,11 @@ read_solution_spread(void) /* fill in soln_defaults.iso */ - soln_defaults.count_iso = count_iso_defaults; - soln_defaults.iso = (struct iso *) PHRQ_malloc((size_t) soln_defaults.count_iso * - sizeof(struct iso)); - if (soln_defaults.iso == NULL) - malloc_error(); + soln_defaults.iso.resize(count_iso_defaults); + /* all iso[i].name is hsave'd, so no conflicts */ - memcpy(soln_defaults.iso, iso_defaults, - (size_t) soln_defaults.count_iso * sizeof(struct iso)); + memcpy(&soln_defaults.iso[0], iso_defaults, + soln_defaults.iso.size() * sizeof(struct iso)); heading = NULL; units = NULL; @@ -368,29 +365,20 @@ read_solution_spread(void) error_msg(error_string, PHRQ_io::OT_CONTINUE); continue; } - int i; - for (i = 0; i < soln_defaults.count_iso; i++) + size_t i; + for (i = 0; i < soln_defaults.iso.size(); i++) { if (strcmp(token.c_str(), soln_defaults.iso[i].name) == 0) { break; } } - if (i == soln_defaults.count_iso) + if (i == soln_defaults.iso.size()) { - soln_defaults.iso = (struct iso *) PHRQ_realloc(soln_defaults.iso, - ((size_t)i + 1) * sizeof(struct iso)); - if (soln_defaults.iso == NULL) - { - malloc_error(); - } - else - { - soln_defaults.iso[i].name = string_hsave(token.c_str()); - soln_defaults.iso[i].value = NAN; - soln_defaults.iso[i].uncertainty = NAN; - soln_defaults.count_iso++; - } + soln_defaults.iso.resize((size_t)i + 1); + soln_defaults.iso[i].name = string_hsave(token.c_str()); + soln_defaults.iso[i].value = NAN; + soln_defaults.iso[i].uncertainty = NAN; } /* read and store isotope ratio uncertainty */ @@ -452,28 +440,19 @@ read_solution_spread(void) continue; } int i; - for (i = 0; i < soln_defaults.count_iso; i++) + for (i = 0; i < soln_defaults.iso.size(); i++) { if (strcmp(token.c_str(), soln_defaults.iso[i].name) == 0) { break; } } - if (i == soln_defaults.count_iso) + if (i == soln_defaults.iso.size()) { - soln_defaults.iso = (struct iso *) PHRQ_realloc(soln_defaults.iso, - ((size_t)i + 1) * sizeof(struct iso)); - if (soln_defaults.iso == NULL) - { - malloc_error(); - } - else - { - soln_defaults.iso[i].name = string_hsave(token.c_str()); - soln_defaults.iso[i].value = NAN; - soln_defaults.iso[i].uncertainty = NAN; - soln_defaults.count_iso++; - } + soln_defaults.iso.resize((size_t)i + 1); + soln_defaults.iso[i].name = string_hsave(token.c_str()); + soln_defaults.iso[i].value = NAN; + soln_defaults.iso[i].uncertainty = NAN; } /* read and store isotope ratio */ if (copy_token(token, &next_char) != DIGIT) @@ -539,7 +518,7 @@ read_solution_spread(void) spread_row_free(heading); spread_row_free(units); - soln_defaults.iso = (struct iso *) free_check_null(soln_defaults.iso); + soln_defaults.iso.clear(); return (return_value); } /* ---------------------------------------------------------------------- */ @@ -1057,48 +1036,18 @@ struct spread_row * Phreeqc:: string_to_spread_row(char *string) /* ---------------------------------------------------------------------- */ { - int j, l; - /* possible memory error if length of line is smaller than previous line */ - char *token; + int j; + std::string token; const char* cptr; - struct spread_row *spread_row_ptr = NULL; /* * Allocate space */ - token = (char *) PHRQ_malloc(strlen(line) + 1); - if (token == NULL) - { - malloc_error(); - return spread_row_ptr; - } - spread_row_ptr = - (struct spread_row *) PHRQ_malloc((size_t) sizeof(struct spread_row)); + struct spread_row* spread_row_ptr = new struct spread_row; if (spread_row_ptr == NULL) { malloc_error(); return spread_row_ptr; } - spread_row_ptr->char_vector = - (char **) PHRQ_malloc((size_t) spread_length * sizeof(char *)); - if (spread_row_ptr->char_vector == NULL) - { - malloc_error(); - return spread_row_ptr; - } - spread_row_ptr->d_vector = - (LDBLE *) PHRQ_malloc((size_t) spread_length * sizeof(LDBLE)); - if (spread_row_ptr->d_vector == NULL) - { - malloc_error(); - return spread_row_ptr; - } - spread_row_ptr->type_vector = - (int *) PHRQ_malloc((size_t) spread_length * sizeof(int)); - if (spread_row_ptr->type_vector == NULL) - { - malloc_error(); - return spread_row_ptr; - } spread_row_ptr->count = 0; spread_row_ptr->empty = 0; spread_row_ptr->string = 0; @@ -1109,59 +1058,26 @@ string_to_spread_row(char *string) */ for (;;) { - if (spread_row_ptr->count + 1 > spread_length) - { - spread_length *= 2; - - spread_row_ptr->char_vector = - (char **) PHRQ_realloc(spread_row_ptr->char_vector, - (size_t) spread_length * sizeof(char *)); - if (spread_row_ptr->char_vector == NULL) - { - malloc_error(); - return spread_row_ptr; - } - - spread_row_ptr->d_vector = - (LDBLE *) PHRQ_realloc(spread_row_ptr->d_vector, - (size_t) spread_length * sizeof(LDBLE)); - if (spread_row_ptr->d_vector == NULL) - { - malloc_error(); - return spread_row_ptr; - } - - spread_row_ptr->type_vector = - (int *) PHRQ_realloc(spread_row_ptr->type_vector, - (size_t) spread_length * sizeof(int)); - if (spread_row_ptr->type_vector == NULL) - { - malloc_error(); - return spread_row_ptr; - } - } - j = copy_token_tab(token, &cptr, &l); + j = copy_token_tab(token, &cptr); if (j == EOL) break; - spread_row_ptr->char_vector[spread_row_ptr->count] = - string_duplicate(token); - spread_row_ptr->d_vector[spread_row_ptr->count] = NAN; - if (j == EMPTY || l == 0) + spread_row_ptr->char_vector.push_back(string_duplicate(token.c_str())); + spread_row_ptr->d_vector.push_back(NAN); + if (j == EMPTY || token.size() == 0) { spread_row_ptr->empty++; - spread_row_ptr->type_vector[spread_row_ptr->count] = EMPTY; + spread_row_ptr->type_vector.push_back(EMPTY); } else if (j == UPPER || j == LOWER) { spread_row_ptr->string++; - spread_row_ptr->type_vector[spread_row_ptr->count] = STRING; + spread_row_ptr->type_vector.push_back(STRING); } else if (j == DIGIT) { spread_row_ptr->number++; - spread_row_ptr->d_vector[spread_row_ptr->count] = - strtod(token, NULL); - spread_row_ptr->type_vector[spread_row_ptr->count] = NUMBER; + spread_row_ptr->d_vector.push_back(strtod(token.c_str(), NULL)); + spread_row_ptr->type_vector.push_back(NUMBER); } else { @@ -1173,42 +1089,6 @@ string_to_spread_row(char *string) } spread_row_ptr->count++; } -/* - * Clean up and return - */ - if (spread_row_ptr->count == 0) - { - spread_row_ptr->char_vector = - (char **) free_check_null(spread_row_ptr->char_vector); - spread_row_ptr->d_vector = - (LDBLE *) free_check_null(spread_row_ptr->d_vector); - spread_row_ptr->type_vector = - (int *) free_check_null(spread_row_ptr->type_vector); - } - else - { -/* Do not realloc to smaller size, memory error */ -/* - spread_row_ptr->char_vector = - (char **) PHRQ_realloc (spread_row_ptr->char_vector, - (size_t) spread_row_ptr->count * - sizeof (char *)); - if (spread_row_ptr->char_vector == NULL) - malloc_error (); - spread_row_ptr->d_vector = - (LDBLE *) PHRQ_realloc (spread_row_ptr->d_vector, - (size_t) spread_row_ptr->count * - sizeof (LDBLE)); - if (spread_row_ptr->d_vector == NULL) - malloc_error (); - spread_row_ptr->type_vector = - (int *) PHRQ_realloc (spread_row_ptr->type_vector, - (size_t) spread_row_ptr->count * sizeof (int)); - if (spread_row_ptr->type_vector == NULL) - malloc_error (); -*/ - } - token = (char *) free_check_null(token); return (spread_row_ptr); } @@ -1227,32 +1107,26 @@ spread_row_free(struct spread_row *spread_row_ptr) (char *) free_check_null(spread_row_ptr->char_vector[i]); } - spread_row_ptr->char_vector = - (char **) free_check_null(spread_row_ptr->char_vector); - spread_row_ptr->d_vector = - (LDBLE *) free_check_null(spread_row_ptr->d_vector); - spread_row_ptr->type_vector = - (int *) free_check_null(spread_row_ptr->type_vector); - spread_row_ptr = (struct spread_row *) free_check_null(spread_row_ptr); + spread_row_ptr->char_vector.clear(); + spread_row_ptr->d_vector.clear(); + spread_row_ptr->type_vector.clear(); + delete spread_row_ptr; return (OK); } /* ---------------------------------------------------------------------- */ int Phreeqc:: -copy_token_tab(char *token_ptr, const char **cptr, int *length) +copy_token_tab(std::string& token, const char **cptr) /* ---------------------------------------------------------------------- */ { /* - * Copies from **cptr to *token_ptr until first tab is encountered. + * Copies from **cptr to *token until first tab is encountered. * * Arguments: - * *token_ptr output, place to store token + * *token output, place to store token * * **cptr input, character string to read token from - * output, next position after token - * - * length output, length of token - * + * output, next position after token * Returns: * UPPER, * LOWER, @@ -1266,6 +1140,7 @@ copy_token_tab(char *token_ptr, const char **cptr, int *length) /* * Strip leading spaces */ + token.clear(); while ((c = **cptr) == ' ') (*cptr)++; /* @@ -1314,13 +1189,11 @@ copy_token_tab(char *token_ptr, const char **cptr, int *length) } else { - token_ptr[i] = c; + token.push_back(c); (*cptr)++; i++; } } - token_ptr[i] = '\0'; - *length = i; /* * Strip trailing spaces */ @@ -1329,11 +1202,6 @@ copy_token_tab(char *token_ptr, const char **cptr, int *length) if (j != ' ') break; } - if (j != i - 1) - { - token_ptr[j + 1] = '\0'; - *length = j + 1; - } return (return_value); } From 318e267cf73aad21893740ce66daed02d0230b4b Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Mon, 29 Mar 2021 21:15:00 -0600 Subject: [PATCH 27/53] (size_t) max and count --- PBasic.cpp | 8 +- inverse.cpp | 160 +++++++++++++++++----------------- model.cpp | 226 ++++++++++++++++++++++++------------------------- parse.cpp | 10 +-- pitzer.cpp | 8 +- prep.cpp | 40 ++++----- read.cpp | 4 +- sit.cpp | 6 +- structures.cpp | 22 ++--- tally.cpp | 4 +- tidy.cpp | 10 +-- transport.cpp | 40 ++++----- utilities.cpp | 10 +-- 13 files changed, 274 insertions(+), 274 deletions(-) diff --git a/PBasic.cpp b/PBasic.cpp index d57f313f..08e12fb1 100644 --- a/PBasic.cpp +++ b/PBasic.cpp @@ -2415,7 +2415,7 @@ factor(struct LOC_exec * LINK) //PhreeqcPtr->count_sys = 1000; //int count_sys = PhreeqcPtr->count_sys; int count_sys = 1000; - names_arg = (char**)PhreeqcPtr->PHRQ_calloc(((size_t)count_sys + 1), sizeof(char*)); + names_arg = (char**)PhreeqcPtr->PHRQ_calloc((count_sys + 1), sizeof(char*)); if (names_arg == NULL) { PhreeqcPtr->malloc_error(); @@ -2423,7 +2423,7 @@ factor(struct LOC_exec * LINK) exit(4); #endif } - moles_arg = (LDBLE*)PhreeqcPtr->PHRQ_calloc(((size_t)count_sys + 1), sizeof(LDBLE)); + moles_arg = (LDBLE*)PhreeqcPtr->PHRQ_calloc((count_sys + 1), sizeof(LDBLE)); if (moles_arg == NULL) { PhreeqcPtr->malloc_error(); @@ -3642,7 +3642,7 @@ factor(struct LOC_exec * LINK) // Make work space int max_length = length < 256 ? 256 : length; - char* token = (char*)PhreeqcPtr->PHRQ_calloc(((size_t)max_length + 1), sizeof(char)); + char* token = (char*)PhreeqcPtr->PHRQ_calloc((max_length + 1), sizeof(char)); if (token == NULL) PhreeqcPtr->malloc_error(); std::string std_num; @@ -3685,7 +3685,7 @@ factor(struct LOC_exec * LINK) // Make work space int max_length = length < 256 ? 256 : length; - char* token = (char*)PhreeqcPtr->PHRQ_calloc(((size_t)max_length + 1), sizeof(char)); + char* token = (char*)PhreeqcPtr->PHRQ_calloc((max_length + 1), sizeof(char)); if (token == NULL) PhreeqcPtr->malloc_error(); std::string std_num; diff --git a/inverse.cpp b/inverse.cpp index 4e31819b..613fc041 100644 --- a/inverse.cpp +++ b/inverse.cpp @@ -274,8 +274,8 @@ setup_inverse(struct inverse *inv_ptr) (size_t) max_column_count * sizeof(LDBLE)); for (i = 0; i < max_row_count; i++) { - memcpy((void *) &(my_array[(size_t)i * (size_t)max_column_count]), (void *) &(inv_zero[0]), - (size_t)max_column_count * sizeof(LDBLE)); + memcpy((void *) &(my_array[(size_t)i * max_column_count]), (void *) &(inv_zero[0]), + max_column_count * sizeof(LDBLE)); } /* * begin filling array @@ -368,11 +368,11 @@ setup_inverse(struct inverse *inv_ptr) { if (master[j]->in >= 0) { - my_array[(size_t)master[j]->in * (size_t)max_column_count + (size_t)i] = + my_array[(size_t)master[j]->in * max_column_count + (size_t)i] = f * master[j]->total; if (master[j]->s == s_eminus) { - my_array[(size_t)master[j]->in * (size_t)max_column_count + (size_t)i] = 0.0; + my_array[(size_t)master[j]->in * max_column_count + (size_t)i] = 0.0; } } } @@ -399,7 +399,7 @@ setup_inverse(struct inverse *inv_ptr) } if (fabs(cb) < toler) cb = 0.0; - my_array[((size_t)row_charge + (size_t)i) * (size_t)max_column_count + (size_t)i] = cb; + my_array[((size_t)row_charge + (size_t)i) * max_column_count + (size_t)i] = cb; } /* mass_balance: phase data */ @@ -445,11 +445,11 @@ setup_inverse(struct inverse *inv_ptr) coef = master_ptr->coef; if (coef <= 0) coef = 1.0; - my_array[(size_t)row * (size_t)max_column_count + (size_t)column] = + my_array[(size_t)row * max_column_count + (size_t)column] = rxn_ptr->token[j].coef * coef; } row = master_alk->in; /* include alkalinity for phase */ - my_array[(size_t)row * (size_t)max_column_count + (size_t)column] = calc_alk(rxn_ptr); + my_array[(size_t)row * max_column_count + (size_t)column] = calc_alk(rxn_ptr); } /* mass balance: redox reaction data */ @@ -497,14 +497,14 @@ setup_inverse(struct inverse *inv_ptr) assert(row * max_column_count + column < max_column_count * max_row_count); assert(row >= 0); assert(column >= 0); - my_array[(size_t)row * (size_t)max_column_count + (size_t)column] = + my_array[(size_t)row * max_column_count + (size_t)column] = rxn_ptr->token[j].coef; /* if coefficient of element is not 1.0 in master species */ if (j != 0) - my_array[(size_t)row * (size_t)max_column_count + (size_t)column] /= coef; + my_array[(size_t)row * max_column_count + (size_t)column] /= coef; } row = master_alk->in; /* include alkalinity for redox reaction */ - my_array[(size_t)row * (size_t)max_column_count + (size_t)column] = + my_array[(size_t)row * max_column_count + (size_t)column] = (calc_alk(rxn_ptr) - inv_ptr->elts[i].master->s->alk) / coef; } } @@ -519,15 +519,15 @@ setup_inverse(struct inverse *inv_ptr) { if (j < (inv_ptr->count_solns - 1)) { - my_array[(size_t)row * (size_t)max_column_count + (size_t)column] = 1.0; + my_array[(size_t)row * max_column_count + (size_t)column] = 1.0; } else { - my_array[(size_t)row * (size_t)max_column_count + (size_t)column] = -1.0; + my_array[(size_t)row * max_column_count + (size_t)column] = -1.0; } if (inv_ptr->elts[i].master->s == s_eminus) { - my_array[(size_t)row * (size_t)max_column_count + (size_t)column] = 0.0; + my_array[(size_t)row * max_column_count + (size_t)column] = 0.0; } sprintf(token, "%s %d", row_name[row], j); col_name[column] = string_hsave(token); @@ -589,19 +589,19 @@ setup_inverse(struct inverse *inv_ptr) solution_ptr = Utilities::Rxn_find(Rxn_solution_map, inv_ptr->solns[i]); if (i < inv_ptr->count_solns - 1) { - my_array[(size_t)count_rows * (size_t)max_column_count + (size_t)i] = + my_array[count_rows * max_column_count + (size_t)i] = 1.0 / gfw_water * solution_ptr->Get_mass_water(); } else { - my_array[(size_t)count_rows * (size_t)max_column_count + (size_t)inv_ptr->count_solns - 1] = + my_array[count_rows * max_column_count + (size_t)inv_ptr->count_solns - 1] = -1.0 / gfw_water * solution_ptr->Get_mass_water(); } } /* coefficient for water uncertainty */ if (inv_ptr->water_uncertainty > 0) { - my_array[(size_t)count_rows * (size_t)max_column_count + (size_t)col_water] = 1.0; + my_array[count_rows * max_column_count + (size_t)col_water] = 1.0; } row_name[count_rows] = string_hsave("H2O"); row_water = count_rows; @@ -611,8 +611,8 @@ setup_inverse(struct inverse *inv_ptr) * Final solution fraction equals 1.0 */ - my_array[(size_t)count_rows * (size_t)max_column_count + (size_t)inv_ptr->count_solns - 1] = 1.0; - my_array[(size_t)count_rows * (size_t)max_column_count + (size_t)count_unknowns] = 1.0; + my_array[count_rows * max_column_count + (size_t)inv_ptr->count_solns - 1] = 1.0; + my_array[count_rows * max_column_count + count_unknowns] = 1.0; row_name[count_rows] = string_hsave("fract, final"); count_rows++; @@ -623,7 +623,7 @@ setup_inverse(struct inverse *inv_ptr) for (i = 0; i < inv_ptr->count_solns; i++) { /* solution_ptr = solution_bsearch(inv_ptr->solns[i], &j, TRUE); */ -/* array[(size_t)count_rows * (size_t)max_column_count + (size_t)i] = solution_ptr->cb; */ +/* array[count_rows * max_column_count + (size_t)i] = solution_ptr->cb; */ for (j = 0; j < inv_ptr->elts.size(); j++) { column = col_epsilon + j * inv_ptr->count_solns + i; @@ -634,10 +634,10 @@ setup_inverse(struct inverse *inv_ptr) { coef = -1.0; } - my_array[(size_t)count_rows * (size_t)max_column_count + (size_t)column] = coef; + my_array[count_rows * max_column_count + (size_t)column] = coef; if (inv_ptr->elts[j].master->s == s_eminus) { - my_array[(size_t)count_rows * (size_t)max_column_count + (size_t)column] = 0.0; + my_array[count_rows * max_column_count + (size_t)column] = 0.0; } } sprintf(token, "%s %d", "charge", i); @@ -655,12 +655,12 @@ setup_inverse(struct inverse *inv_ptr) if (inv_ptr->dalk_dph[i] != 0 || inv_ptr->dalk_dc[i] != 0) { column = col_ph + i; - my_array[(size_t)count_rows * (size_t)max_column_count + (size_t)column] = + my_array[count_rows * max_column_count + (size_t)column] = inv_ptr->dalk_dph[i]; column = col_epsilon + i_alk * inv_ptr->count_solns + i; - my_array[(size_t)count_rows * (size_t)max_column_count + (size_t)column] = -1.0; + my_array[count_rows * max_column_count + (size_t)column] = -1.0; column = col_epsilon + i_carb * inv_ptr->count_solns + i; - my_array[(size_t)count_rows * (size_t)max_column_count + (size_t)column] = + my_array[count_rows * max_column_count + (size_t)column] = inv_ptr->dalk_dc[i]; } sprintf(token, "%s %d", "dAlk", i); @@ -707,7 +707,7 @@ setup_inverse(struct inverse *inv_ptr) else { coef = my_array[(size_t)inv_ptr->elts[j].master->in * - (size_t)max_column_count + (size_t)i] * coef; + max_column_count + (size_t)i] * coef; coef = fabs(coef); } @@ -719,7 +719,7 @@ setup_inverse(struct inverse *inv_ptr) { for (k = 0; k < count_rows; k++) { - my_array[(size_t)k * (size_t)max_column_count + (size_t)column] = 0.0; + my_array[(size_t)k * max_column_count + (size_t)column] = 0.0; } continue; } @@ -731,12 +731,12 @@ setup_inverse(struct inverse *inv_ptr) if (coef < toler) { - my_array[((size_t)column - (size_t)col_epsilon) * (size_t)max_column_count + (size_t)column] = + my_array[((size_t)column - (size_t)col_epsilon) * max_column_count + (size_t)column] = SCALE_EPSILON / toler; } else { - my_array[((size_t)column - (size_t)col_epsilon) * (size_t)max_column_count + (size_t)column] = + my_array[((size_t)column - (size_t)col_epsilon) * max_column_count + (size_t)column] = SCALE_EPSILON / coef; } @@ -750,14 +750,14 @@ setup_inverse(struct inverse *inv_ptr) { f = 1.0; } - my_array[(size_t)count_rows * (size_t)max_column_count + (size_t)column] = 1.0 * f; - my_array[(size_t)count_rows * (size_t)max_column_count + (size_t)i] = -coef * f; + my_array[count_rows * max_column_count + (size_t)column] = 1.0 * f; + my_array[count_rows * max_column_count + (size_t)i] = -coef * f; sprintf(token, "%s %s", inv_ptr->elts[j].master->elt->name, "eps+"); row_name[count_rows] = string_hsave(token); count_rows++; /* set lower limit of change in negative direction */ - conc = my_array[(size_t)inv_ptr->elts[j].master->in * (size_t)max_column_count + (size_t)i]; + conc = my_array[(size_t)inv_ptr->elts[j].master->in * max_column_count + (size_t)i]; /* if concentration is zero, only positive direction allowed */ if (conc == 0.0) @@ -784,8 +784,8 @@ setup_inverse(struct inverse *inv_ptr) inv_ptr->elts[j].master->elt->name)) coef = fabs(conc) + toler; - my_array[(size_t)count_rows * (size_t)max_column_count + (size_t)i] = -coef * f; - my_array[(size_t)count_rows * (size_t)max_column_count + (size_t)column] = -1.0 * f; + my_array[count_rows * max_column_count + (size_t)i] = -coef * f; + my_array[count_rows * max_column_count + (size_t)column] = -1.0 * f; sprintf(token, "%s %s", inv_ptr->elts[j].master->elt->name, "eps-"); row_name[count_rows] = string_hsave(token); @@ -805,21 +805,21 @@ setup_inverse(struct inverse *inv_ptr) /* scale epsilon in optimization equation */ - my_array[((size_t)column - (size_t)col_epsilon) * (size_t)max_column_count + (size_t)column] = + my_array[((size_t)column - (size_t)col_epsilon) * max_column_count + (size_t)column] = SCALE_EPSILON / coef; /* set upper limit of change in positive direction */ - my_array[(size_t)count_rows * (size_t)max_column_count + (size_t)column] = 1.0; - my_array[(size_t)count_rows * (size_t)max_column_count + (size_t)i] = -coef; + my_array[count_rows * max_column_count + (size_t)column] = 1.0; + my_array[count_rows * max_column_count + (size_t)i] = -coef; sprintf(token, "%s %s", "pH", "eps+"); row_name[count_rows] = string_hsave(token); count_rows++; /* set lower limit of change in negative direction */ - my_array[(size_t)count_rows * (size_t)max_column_count + (size_t)column] = -1.0; - my_array[(size_t)count_rows * (size_t)max_column_count + (size_t)i] = -coef; + my_array[count_rows * max_column_count + (size_t)column] = -1.0; + my_array[count_rows * max_column_count + (size_t)i] = -coef; sprintf(token, "%s %s", "pH", "eps-"); row_name[count_rows] = string_hsave(token); count_rows++; @@ -834,16 +834,16 @@ setup_inverse(struct inverse *inv_ptr) if (coef > 0.0) { /* set upper limit of change in positive direction */ - my_array[(size_t)count_rows * (size_t)max_column_count + (size_t)column] = 1.0; - my_array[(size_t)count_rows * (size_t)max_column_count + (size_t)count_unknowns] = coef; + my_array[count_rows * max_column_count + (size_t)column] = 1.0; + my_array[count_rows * max_column_count + count_unknowns] = coef; sprintf(token, "%s %s", "water", "eps+"); row_name[count_rows] = string_hsave(token); count_rows++; /* set lower limit of change in negative direction */ - my_array[(size_t)count_rows * (size_t)max_column_count + (size_t)column] = -1.0; - my_array[(size_t)count_rows * (size_t)max_column_count + (size_t)count_unknowns] = coef; + my_array[count_rows * max_column_count + (size_t)column] = -1.0; + my_array[count_rows * max_column_count + count_unknowns] = coef; sprintf(token, "%s %s", "water", "eps-"); row_name[count_rows] = string_hsave(token); count_rows++; @@ -875,12 +875,12 @@ setup_inverse(struct inverse *inv_ptr) /* scale epsilon in optimization equation */ - my_array[((size_t)column - (size_t)col_epsilon) * (size_t)max_column_count + + my_array[((size_t)column - (size_t)col_epsilon) * max_column_count + (size_t)column] = SCALE_EPSILON / coef; /* set upper limit of change in positive direction */ - my_array[(size_t)count_rows * (size_t)max_column_count + (size_t)column] = 1.0; - my_array[(size_t)count_rows * (size_t)max_column_count + (size_t)i] = -coef; + my_array[count_rows * max_column_count + (size_t)column] = 1.0; + my_array[count_rows * max_column_count + (size_t)i] = -coef; sprintf(token, "%d%s %s", (int) kit->second.Get_isotope_number(), kit->second.Get_elt_name().c_str(), "eps+"); @@ -889,8 +889,8 @@ setup_inverse(struct inverse *inv_ptr) /* set lower limit of change in negative direction */ - my_array[(size_t)count_rows * (size_t)max_column_count + (size_t)column] = -1.0; - my_array[(size_t)count_rows * (size_t)max_column_count + (size_t)i] = -coef; + my_array[count_rows * max_column_count + (size_t)column] = -1.0; + my_array[count_rows * max_column_count + (size_t)i] = -coef; sprintf(token, "%d%s %s", (int) kit->second.Get_isotope_number(), kit->second.Get_elt_name().c_str(), "eps-"); @@ -935,7 +935,7 @@ setup_inverse(struct inverse *inv_ptr) */ for (i = 0; i < max_column_count; i++) { - my_array[(size_t)row_water * (size_t)max_column_count + (size_t)i] *= SCALE_WATER; + my_array[(size_t)row_water * max_column_count + (size_t)i] *= SCALE_WATER; } /* * Arrays are complete @@ -958,7 +958,7 @@ setup_inverse(struct inverse *inv_ptr) k = 0; } output_msg(sformatf("%11.2e", - (double)my_array[(size_t)i * (size_t)max_column_count + (size_t)j])); + (double)my_array[(size_t)i * max_column_count + (size_t)j])); k++; } if (k != 0) @@ -2661,7 +2661,7 @@ shrink(struct inverse *inv_ptr, LDBLE * array_in, LDBLE * array_out, memcpy(&(array_out[row * max_column_count]), - &(array_out[(size_t)i * (size_t)max_column_count]), + &(array_out[(size_t)i * max_column_count]), ((size_t)*n + 1) * sizeof(LDBLE)); } row_back_l[row] = i; @@ -2705,7 +2705,7 @@ shrink(struct inverse *inv_ptr, LDBLE * array_in, LDBLE * array_out, assert(false); } memcpy(&(array_out[row * max_column_count]), - &(array_out[(size_t)i * (size_t)max_column_count]), + &(array_out[(size_t)i * max_column_count]), ((size_t)*n + 1) * sizeof(LDBLE)); } row_back_l[row] = i; @@ -2747,8 +2747,8 @@ shrink(struct inverse *inv_ptr, LDBLE * array_in, LDBLE * array_out, { assert(false); } - memcpy(&(array_out[(size_t)row * (size_t)max_column_count]), - &(array_out[(size_t)i * (size_t)max_column_count]), + memcpy(&(array_out[(size_t)row * max_column_count]), + &(array_out[(size_t)i * max_column_count]), ((size_t)*n + 1) * sizeof(LDBLE)); } row_back_l[row] = i; @@ -2971,16 +2971,16 @@ post_mortem(void) sum = 0; for (j = 0; j < count_unknowns; j++) { - sum += inv_delta1[j] * my_array[(size_t)i * (size_t)max_column_count + (size_t)j]; + sum += inv_delta1[j] * my_array[(size_t)i * max_column_count + (size_t)j]; } - if (equal(sum, my_array[((size_t)i * (size_t)max_column_count) + (size_t)count_unknowns], toler) + if (equal(sum, my_array[((size_t)i * max_column_count) + count_unknowns], toler) == FALSE) { output_msg(sformatf( "\tERROR: equality not satisfied for %s, %e.\n", row_name[i], - (double) (sum - my_array[((size_t)i * (size_t)max_column_count) + (size_t)count_unknowns]))); + (double) (sum - my_array[((size_t)i * max_column_count) + count_unknowns]))); } } /* @@ -2991,15 +2991,15 @@ post_mortem(void) sum = 0; for (j = 0; j < count_unknowns; j++) { - sum += inv_delta1[j] * my_array[(size_t)i * (size_t)max_column_count + (size_t)j]; + sum += inv_delta1[j] * my_array[(size_t)i * max_column_count + (size_t)j]; } - if (sum > my_array[((size_t)i * (size_t)max_column_count) + (size_t)count_unknowns] + toler) + if (sum > my_array[((size_t)i * max_column_count) + count_unknowns] + toler) { output_msg(sformatf( "\tERROR: inequality not satisfied for %s, %e\n", row_name[i], - (double) (sum - my_array[((size_t)i * (size_t)max_column_count) + (size_t)count_unknowns]))); + (double) (sum - my_array[((size_t)i * max_column_count) + count_unknowns]))); } } /* @@ -3048,15 +3048,15 @@ test_cl1_solution(void) sum = 0; for (j = 0; j < count_unknowns; j++) { - sum += inv_delta1[j] * my_array[(size_t)i * (size_t)max_column_count + (size_t)j]; + sum += inv_delta1[j] * my_array[(size_t)i * max_column_count + (size_t)j]; } - if (equal(sum, my_array[((size_t)i * (size_t)max_column_count) + (size_t)count_unknowns], toler) == FALSE) + if (equal(sum, my_array[((size_t)i * max_column_count) + count_unknowns], toler) == FALSE) { if (debug_inverse) { output_msg(sformatf("\tERROR: equality not satisfied for %s, %e.\n", row_name[i], - (double) (sum - my_array[((size_t)i * (size_t)max_column_count) + (size_t)count_unknowns]))); + (double) (sum - my_array[((size_t)i * max_column_count) + count_unknowns]))); } rv = false; } @@ -3069,17 +3069,17 @@ test_cl1_solution(void) sum = 0; for (j = 0; j < count_unknowns; j++) { - sum += inv_delta1[j] * my_array[(size_t)i * (size_t)max_column_count + (size_t)j]; + sum += inv_delta1[j] * my_array[(size_t)i * max_column_count + (size_t)j]; } - if (sum > my_array[((size_t)i * (size_t)max_column_count) + (size_t)count_unknowns] + toler) + if (sum > my_array[((size_t)i * max_column_count) + count_unknowns] + toler) { if (debug_inverse) { output_msg(sformatf( "\tERROR: inequality not satisfied for %s, %e\n", row_name[i], - (double) (sum - my_array[((size_t)i * (size_t)max_column_count) + (size_t)count_unknowns]))); + (double) (sum - my_array[((size_t)i * max_column_count) + count_unknowns]))); } rv = false; } @@ -3323,7 +3323,7 @@ isotope_balance_equation(struct inverse *inv_ptr, int row, int n) if (primary_jit == primary_ptr && jit->second.Get_isotope_number() == isotope_number) { - my_array[(size_t)row * (size_t)max_column_count + (size_t)i] += + my_array[(size_t)row * max_column_count + (size_t)i] += f * jit->second.Get_total() * jit->second.Get_ratio(); } } @@ -3348,7 +3348,7 @@ isotope_balance_equation(struct inverse *inv_ptr, int row, int n) break; } column = col_epsilon + (k * inv_ptr->count_solns) + i; - my_array[(size_t)row * (size_t)max_column_count + (size_t)column] += + my_array[(size_t)row * max_column_count + (size_t)column] += f * jit->second.Get_ratio(); } } @@ -3376,7 +3376,7 @@ isotope_balance_equation(struct inverse *inv_ptr, int row, int n) (i * inv_ptr->isotope_unknowns.size()) + k; } } - my_array[(size_t)row * (size_t)max_column_count + (size_t)column] += + my_array[(size_t)row * max_column_count + (size_t)column] += f * jit->second.Get_total(); } } @@ -3396,11 +3396,11 @@ isotope_balance_equation(struct inverse *inv_ptr, int row, int n) { /* term for alpha phase unknowns */ column = col_phases + i; - my_array[(size_t)row * (size_t)max_column_count + (size_t)column] = + my_array[(size_t)row * max_column_count + (size_t)column] = isotope_ref[j].ratio * isotope_ref[j].coef; /* term for phase isotope uncertainty unknown */ column = col_phase_isotopes + i * inv_ptr->isotopes.size() + n; - my_array[(size_t)row * (size_t)max_column_count + (size_t)column] = isotope_ref[j].coef; + my_array[(size_t)row * max_column_count + (size_t)column] = isotope_ref[j].coef; break; } } @@ -3758,7 +3758,7 @@ phase_isotope_inequalities(struct inverse *inv_ptr) { for (k = 0; k < count_rows; k++) { - my_array[(size_t)k * (size_t)max_column_count + (size_t)column] = 0.0; + my_array[(size_t)k * max_column_count + (size_t)column] = 0.0; } continue; } @@ -3766,7 +3766,7 @@ phase_isotope_inequalities(struct inverse *inv_ptr) /* * optimization */ - my_array[((size_t)column - (size_t)col_epsilon) * (size_t)max_column_count + (size_t)column] = + my_array[((size_t)column - (size_t)col_epsilon) * max_column_count + (size_t)column] = SCALE_EPSILON / inv_ptr->phases[i].isotopes[j].ratio_uncertainty; /* * two inequalities to account for absolute value @@ -3774,17 +3774,17 @@ phase_isotope_inequalities(struct inverse *inv_ptr) /* for phases constrained to precipitate */ if (inv_ptr->phases[i].constraint == PRECIPITATE) { - my_array[(size_t)count_rows * (size_t)max_column_count + (size_t)col_phases + (size_t)i] = + my_array[count_rows * max_column_count + (size_t)col_phases + (size_t)i] = inv_ptr->phases[i].isotopes[j].ratio_uncertainty; - my_array[(size_t)count_rows * (size_t)max_column_count + (size_t)column] = 1.0; + my_array[count_rows * max_column_count + (size_t)column] = 1.0; sprintf(token, "%s %s", inv_ptr->phases[i].phase->name, "iso pos"); row_name[count_rows] = string_hsave(token); count_rows++; - my_array[(size_t)count_rows * (size_t)max_column_count + (size_t)col_phases + (size_t)i] = + my_array[count_rows * max_column_count + (size_t)col_phases + (size_t)i] = inv_ptr->phases[i].isotopes[j].ratio_uncertainty; - my_array[(size_t)count_rows * (size_t)max_column_count + (size_t)column] = -1.0; + my_array[count_rows * max_column_count + (size_t)column] = -1.0; sprintf(token, "%s %s", inv_ptr->phases[i].phase->name, "iso neg"); row_name[count_rows] = string_hsave(token); @@ -3794,17 +3794,17 @@ phase_isotope_inequalities(struct inverse *inv_ptr) } else if (inv_ptr->phases[i].constraint == DISSOLVE) { - my_array[(size_t)count_rows * (size_t)max_column_count + (size_t)col_phases + (size_t)i] = + my_array[count_rows * max_column_count + (size_t)col_phases + (size_t)i] = -inv_ptr->phases[i].isotopes[j].ratio_uncertainty; - my_array[(size_t)count_rows * (size_t)max_column_count + (size_t)column] = -1.0; + my_array[count_rows * max_column_count + (size_t)column] = -1.0; sprintf(token, "%s %s", inv_ptr->phases[i].phase->name, "iso pos"); row_name[count_rows] = string_hsave(token); count_rows++; - my_array[(size_t)count_rows * (size_t)max_column_count + (size_t)col_phases + (size_t)i] = + my_array[count_rows * max_column_count + (size_t)col_phases + (size_t)i] = -inv_ptr->phases[i].isotopes[j].ratio_uncertainty; - my_array[(size_t)count_rows * (size_t)max_column_count + (size_t)column] = 1.0; + my_array[count_rows * max_column_count + (size_t)column] = 1.0; sprintf(token, "%s %s", inv_ptr->phases[i].phase->name, "iso neg"); row_name[count_rows] = string_hsave(token); diff --git a/model.cpp b/model.cpp index 3b3ec003..567cdf35 100644 --- a/model.cpp +++ b/model.cpp @@ -1008,11 +1008,11 @@ ineq(int in_kode) { for (j = 0; j < count_unknowns; j++) { - my_array[(size_t)j * ((size_t)count_unknowns + 1) + i] = 0.0; + my_array[(size_t)j * (count_unknowns + 1) + i] = 0.0; } for (j = 0; j < count_unknowns + 1; j++) { - my_array[(size_t)i * ((size_t)count_unknowns + 1) + (size_t)j] = 0.0; + my_array[(size_t)i * (count_unknowns + 1) + (size_t)j] = 0.0; } } } @@ -1020,7 +1020,7 @@ ineq(int in_kode) /* * Normalize column */ - normal.resize((size_t)count_unknowns); + normal.resize(count_unknowns); std::fill(normal.begin(), normal.end(), 1.0); for (i = 0; i < count_unknowns; i++) @@ -1041,24 +1041,24 @@ ineq(int in_kode) if (x[i]->type == SURFACE_CB1 && x[j]->type == SURFACE_CB2) continue; - if (fabs(my_array[(size_t)j * ((size_t)count_unknowns + 1) + (size_t)i]) > max) + if (fabs(my_array[(size_t)j * (count_unknowns + 1) + (size_t)i]) > max) { - max = fabs(my_array[(size_t)j * ((size_t)count_unknowns + 1) + (size_t)i]); + max = fabs(my_array[(size_t)j * (count_unknowns + 1) + (size_t)i]); if (max > min_value) break; } } if (diagonal_scale == TRUE) { - if (fabs(my_array[(size_t)i * ((size_t)count_unknowns + 1) + (size_t)i]) < min_value) + if (fabs(my_array[(size_t)i * (count_unknowns + 1) + (size_t)i]) < min_value) { - max = fabs(my_array[(size_t)i * ((size_t)count_unknowns + 1) + (size_t)i]); + max = fabs(my_array[(size_t)i * (count_unknowns + 1) + (size_t)i]); } } if (max == 0) { - my_array[(size_t)i * ((size_t)count_unknowns + 1) + (size_t)i] = 1e-5 * x[i]->moles; + my_array[(size_t)i * (count_unknowns + 1) + (size_t)i] = 1e-5 * x[i]->moles; max = fabs(1e-5 * x[i]->moles); } } @@ -1069,9 +1069,9 @@ ineq(int in_kode) min = 1e-12; min = MIN_TOTAL; - my_array[(size_t)x[i]->number * ((size_t)count_unknowns + 1) + (size_t)x[i]->number] += min; - if (fabs(my_array[(size_t)x[i]->number * ((size_t)count_unknowns + 1) + (size_t)x[i]->number]) < min) - my_array[(size_t)x[i]->number * ((size_t)count_unknowns + 1) + (size_t)x[i]->number] = min; + my_array[(size_t)x[i]->number * (count_unknowns + 1) + (size_t)x[i]->number] += min; + if (fabs(my_array[(size_t)x[i]->number * (count_unknowns + 1) + (size_t)x[i]->number]) < min) + my_array[(size_t)x[i]->number * (count_unknowns + 1) + (size_t)x[i]->number] = min; max = 0.0; for (j = 0; j < count_unknowns; j++) @@ -1084,9 +1084,9 @@ ineq(int in_kode) x[j]->type != EXCH && x[j]->type != MH && x[j]->type != MH2O) continue; - if (fabs(my_array[(size_t)j * ((size_t)count_unknowns + 1) + (size_t)i]) > max) + if (fabs(my_array[(size_t)j * (count_unknowns + 1) + (size_t)i]) > max) { - max = fabs(my_array[(size_t)j * ((size_t)count_unknowns + 1) + (size_t)i]); + max = fabs(my_array[(size_t)j * (count_unknowns + 1) + (size_t)i]); if (max > min_value) break; } @@ -1103,7 +1103,7 @@ ineq(int in_kode) } for (j = 0; j < count_unknowns; j++) { - my_array[(size_t)j * ((size_t)count_unknowns + 1) + (size_t)i] *= min_value / max; + my_array[(size_t)j * (count_unknowns + 1) + (size_t)i] *= min_value / max; } normal[i] = min_value / max; } @@ -1114,14 +1114,14 @@ ineq(int in_kode) */ max_row_count = 2 * count_unknowns + 2; max_column_count = count_unknowns + 2; - ineq_array.resize((size_t)max_row_count * (size_t)max_column_count); - back_eq.resize((size_t)max_row_count); - zero.resize((size_t)max_row_count); - memset(&zero[0], 0, (size_t)max_row_count * sizeof(double)); - res.resize((size_t)max_row_count); - memset(&res[0], 0, (size_t)max_row_count * sizeof(double)); - delta1.resize((size_t)max_column_count); - memset(&delta1[0], 0,(size_t)max_column_count * sizeof(double)); + ineq_array.resize(max_row_count * max_column_count); + back_eq.resize(max_row_count); + zero.resize(max_row_count); + memset(&zero[0], 0, max_row_count * sizeof(double)); + res.resize(max_row_count); + memset(&res[0], 0, max_row_count * sizeof(double)); + delta1.resize(max_column_count); + memset(&delta1[0], 0,max_column_count * sizeof(double)); /* * Copy equations to optimize into ineq_array */ @@ -1166,9 +1166,9 @@ ineq(int in_kode) else { /* Copy in saturation index equation (has mass or supersaturated) */ - memcpy((void *) &(ineq_array[(size_t)l_count_rows * (size_t)max_column_count]), - (void *) &(my_array[(size_t)i * ((size_t)count_unknowns + 1)]), - ((size_t)count_unknowns + 1) * sizeof(LDBLE)); + memcpy((void *) &(ineq_array[(size_t)l_count_rows * max_column_count]), + (void *) &(my_array[(size_t)i * (count_unknowns + 1)]), + (count_unknowns + 1) * sizeof(LDBLE)); back_eq[l_count_rows] = i; //if (it->second.Get_add_formula().size() == 0 if (comp_ptr->Get_add_formula().size() == 0 @@ -1183,7 +1183,7 @@ ineq(int in_kode) { for (j = 0; j < count_unknowns + 1; j++) { - ineq_array[(size_t)l_count_rows * (size_t)max_column_count + (size_t)j] *= pp_scale; + ineq_array[(size_t)l_count_rows * max_column_count + (size_t)j] *= pp_scale; } } @@ -1199,9 +1199,9 @@ ineq(int in_kode) /* * Alkalinity and solution phase boundary */ - memcpy((void *) &(ineq_array[(size_t)l_count_rows * (size_t)max_column_count]), - (void *) &(my_array[(size_t)i * ((size_t)count_unknowns + 1)]), - ((size_t)count_unknowns + 1) * sizeof(LDBLE)); + memcpy((void *) &(ineq_array[(size_t)l_count_rows * max_column_count]), + (void *) &(my_array[(size_t)i * (count_unknowns + 1)]), + (count_unknowns + 1) * sizeof(LDBLE)); back_eq[l_count_rows] = i; l_count_rows++; /* @@ -1210,9 +1210,9 @@ ineq(int in_kode) } else if (x[i]->type == GAS_MOLES && gas_in == TRUE) { - memcpy((void *) &(ineq_array[(size_t)l_count_rows * (size_t)max_column_count]), - (void *) &(my_array[(size_t)i * ((size_t)count_unknowns + 1)]), - ((size_t)count_unknowns + 1) * sizeof(LDBLE)); + memcpy((void *) &(ineq_array[(size_t)l_count_rows * max_column_count]), + (void *) &(my_array[(size_t)i * (count_unknowns + 1)]), + (count_unknowns + 1) * sizeof(LDBLE)); back_eq[l_count_rows] = i; res[l_count_rows] = 1.0; @@ -1227,9 +1227,9 @@ ineq(int in_kode) } else if (x[i]->type == SS_MOLES && x[i]->ss_in == TRUE) { - memcpy((void *) &(ineq_array[(size_t)l_count_rows * (size_t)max_column_count]), - (void *) &(my_array[(size_t)i * ((size_t)count_unknowns + 1)]), - ((size_t)count_unknowns + 1) * sizeof(LDBLE)); + memcpy((void *) &(ineq_array[(size_t)l_count_rows * max_column_count]), + (void *) &(my_array[(size_t)i * (count_unknowns + 1)]), + (count_unknowns + 1) * sizeof(LDBLE)); back_eq[l_count_rows] = i; res[l_count_rows] = 1.0; if (in_kode != 1) @@ -1309,26 +1309,26 @@ ineq(int in_kode) continue; } } - memcpy((void *) &(ineq_array[(size_t)l_count_rows * (size_t)max_column_count]), - (void *) &(my_array[(size_t)i * ((size_t)count_unknowns + 1)]), - ((size_t)count_unknowns + 1) * sizeof(LDBLE)); + memcpy((void *) &(ineq_array[(size_t)l_count_rows * max_column_count]), + (void *) &(my_array[(size_t)i * (count_unknowns + 1)]), + (count_unknowns + 1) * sizeof(LDBLE)); back_eq[l_count_rows] = i; if (mass_water_switch == TRUE && x[i] == mass_hydrogen_unknown) { k = mass_oxygen_unknown->number; for (j = 0; j < count_unknowns; j++) { - ineq_array[(size_t)l_count_rows * (size_t)max_column_count + (size_t)j] -= - 2 * my_array[(size_t)k * ((size_t)count_unknowns + 1) + (size_t)j]; + ineq_array[(size_t)l_count_rows * max_column_count + (size_t)j] -= + 2 * my_array[(size_t)k * (count_unknowns + 1) + (size_t)j]; } } l_count_rows++; } else if (x[i]->type == PITZER_GAMMA && full_pitzer == TRUE) { - memcpy((void *) &(ineq_array[(size_t)l_count_rows * (size_t)max_column_count]), - (void *) &(my_array[(size_t)i * ((size_t)count_unknowns + 1)]), - ((size_t)count_unknowns + 1) * sizeof(LDBLE)); + memcpy((void *) &(ineq_array[(size_t)l_count_rows * max_column_count]), + (void *) &(my_array[(size_t)i * (count_unknowns + 1)]), + (count_unknowns + 1) * sizeof(LDBLE)); back_eq[l_count_rows] = i; l_count_rows++; } @@ -1370,12 +1370,12 @@ ineq(int in_kode) /* Pure phase is present, force Mass transfer to be <= amount of mineral remaining */ //memcpy((void *) - // &(ineq_array[(size_t)l_count_rows * (size_t)max_column_count]), + // &(ineq_array[(size_t)l_count_rows * max_column_count]), // (void *) &(zero[0]), // ((size_t) count_unknowns + 1) * sizeof(LDBLE)); - memset(&ineq_array[(size_t)l_count_rows * (size_t)max_column_count], 0, ((size_t) count_unknowns + 1) * sizeof(LDBLE)); - ineq_array[(size_t)l_count_rows * (size_t)max_column_count + i] = 1.0; - ineq_array[(size_t)l_count_rows * (size_t)max_column_count + (size_t)count_unknowns] = x[i]->moles; + memset(&ineq_array[(size_t)l_count_rows * max_column_count], 0, ((size_t) count_unknowns + 1) * sizeof(LDBLE)); + ineq_array[(size_t)l_count_rows * max_column_count + i] = 1.0; + ineq_array[(size_t)l_count_rows * max_column_count + count_unknowns] = x[i]->moles; back_eq[l_count_rows] = i; l_count_rows++; } @@ -1383,12 +1383,12 @@ ineq(int in_kode) if (x[i]->dissolve_only == TRUE) { //memcpy((void *) - // &(ineq_array[(size_t)l_count_rows * (size_t)max_column_count]), + // &(ineq_array[(size_t)l_count_rows * max_column_count]), // (void *) &(zero[0]), // ((size_t) count_unknowns + 1) * sizeof(LDBLE)); - memset(&(ineq_array[(size_t)l_count_rows * (size_t)max_column_count]), 0, ((size_t)count_unknowns + 1) * sizeof(LDBLE)); - ineq_array[(size_t)l_count_rows * (size_t)max_column_count + i] = -1.0; - ineq_array[(size_t)l_count_rows * (size_t)max_column_count + (size_t)count_unknowns] = + memset(&(ineq_array[(size_t)l_count_rows * max_column_count]), 0, (count_unknowns + 1) * sizeof(LDBLE)); + ineq_array[(size_t)l_count_rows * max_column_count + i] = -1.0; + ineq_array[(size_t)l_count_rows * max_column_count + count_unknowns] = comp_ptr->Get_initial_moles() - x[i]->moles; back_eq[l_count_rows] = i; l_count_rows++; @@ -1405,22 +1405,22 @@ ineq(int in_kode) { if (x[i]->type == MH2O) { - memcpy((void *) &(ineq_array[(size_t)l_count_rows * (size_t)max_column_count]), - (void *) &(my_array[(size_t)i * ((size_t)count_unknowns + 1)]), - ((size_t)count_unknowns + 1) * sizeof(LDBLE)); + memcpy((void *) &(ineq_array[(size_t)l_count_rows * max_column_count]), + (void *) &(my_array[(size_t)i * (count_unknowns + 1)]), + (count_unknowns + 1) * sizeof(LDBLE)); back_eq[l_count_rows] = i; for (j = 0; j < count_unknowns; j++) { if (x[j]->type < PP) { - ineq_array[(size_t)l_count_rows * (size_t)max_column_count + j] = 0.0; + ineq_array[(size_t)l_count_rows * max_column_count + j] = 0.0; } else { /*ineq_array[l_count_rows*max_column_count + j] = -ineq_array[l_count_rows*max_column_count + j]; */ } } - ineq_array[(size_t)l_count_rows * (size_t)max_column_count + (size_t)count_unknowns] = + ineq_array[(size_t)l_count_rows * max_column_count + count_unknowns] = 0.5 * x[i]->moles; l_count_rows++; } @@ -1448,7 +1448,7 @@ ineq(int in_kode) { for (j = 0; j < l_count_rows; j++) { - ineq_array[(size_t)j * (size_t)max_column_count + i] = 0.0; + ineq_array[(size_t)j * max_column_count + i] = 0.0; } } if (x[i]->dissolve_only == TRUE) @@ -1459,7 +1459,7 @@ ineq(int in_kode) { for (j = 0; j < l_count_rows; j++) { - ineq_array[(size_t)j * (size_t)max_column_count + (size_t)i] = 0.0; + ineq_array[(size_t)j * max_column_count + (size_t)i] = 0.0; } } } @@ -1479,7 +1479,7 @@ ineq(int in_kode) { for (j = 0; j < l_count_rows; j++) { - ineq_array[(size_t)j * (size_t)max_column_count + i] = 0.0; + ineq_array[(size_t)j * max_column_count + i] = 0.0; } } } @@ -1524,7 +1524,7 @@ ineq(int in_kode) { for (j = 0; j < l_count_rows; j++) { - ineq_array[(size_t)j * (size_t)max_column_count + (size_t)i] = 0.0; + ineq_array[(size_t)j * max_column_count + (size_t)i] = 0.0; } } } @@ -1553,7 +1553,7 @@ ineq(int in_kode) { for (j = 0; j < l_count_rows; j++) { - ineq_array[(size_t)j * (size_t)max_column_count + (size_t)i] = 0.0; + ineq_array[(size_t)j * max_column_count + (size_t)i] = 0.0; } } } @@ -1567,16 +1567,16 @@ ineq(int in_kode) { if (x[i]->type == GAS_MOLES) { - //memcpy((void *) &(ineq_array[(size_t)l_count_rows * (size_t)max_column_count]), + //memcpy((void *) &(ineq_array[(size_t)l_count_rows * max_column_count]), // (void *) &(zero[0]), // ((size_t) count_unknowns + 1) * sizeof(LDBLE)); - //std::fill(&(ineq_array[(size_t)l_count_rows * (size_t)max_column_count]), + //std::fill(&(ineq_array[(size_t)l_count_rows * max_column_count]), // &(ineq_array[l_count_rows * max_column_count + count_unknowns]), // 0.0e0); - memset(&(ineq_array[(size_t)l_count_rows * (size_t)max_column_count]), 0, - ((size_t)count_unknowns + 1) * sizeof(LDBLE)); - ineq_array[(size_t)l_count_rows * (size_t)max_column_count + (size_t)i] = -1.0; - ineq_array[(size_t)l_count_rows * (size_t)max_column_count + (size_t)count_unknowns] = + memset(&(ineq_array[(size_t)l_count_rows * max_column_count]), 0, + (count_unknowns + 1) * sizeof(LDBLE)); + ineq_array[(size_t)l_count_rows * max_column_count + (size_t)i] = -1.0; + ineq_array[(size_t)l_count_rows * max_column_count + count_unknowns] = x[i]->moles; back_eq[l_count_rows] = i; l_count_rows++; @@ -1595,7 +1595,7 @@ ineq(int in_kode) i = gas_unknown->number; for (j = 0; j < l_count_rows; j++) { - ineq_array[(size_t)j * (size_t)max_column_count + (size_t)i] = 0.0; + ineq_array[(size_t)j * max_column_count + (size_t)i] = 0.0; } } /* @@ -1610,13 +1610,13 @@ ineq(int in_kode) break; if (x[i]->phase->in == TRUE && x[i]->ss_in == TRUE) { - //memcpy((void *) &(ineq_array[(size_t)l_count_rows * (size_t)max_column_count]), + //memcpy((void *) &(ineq_array[(size_t)l_count_rows * max_column_count]), // (void *) &(zero[0]), // ((size_t) count_unknowns + 1) * sizeof(LDBLE)); - memset(&(ineq_array[(size_t)l_count_rows * (size_t)max_column_count]), 0, - ((size_t)count_unknowns + 1) * sizeof(LDBLE)); - ineq_array[(size_t)l_count_rows * (size_t)max_column_count + (size_t)i] = 1.0; - ineq_array[(size_t)l_count_rows * (size_t)max_column_count + (size_t)count_unknowns] = + memset(&(ineq_array[(size_t)l_count_rows * max_column_count]), 0, + (count_unknowns + 1) * sizeof(LDBLE)); + ineq_array[(size_t)l_count_rows * max_column_count + (size_t)i] = 1.0; + ineq_array[(size_t)l_count_rows * max_column_count + count_unknowns] = 0.99 * x[i]->moles - MIN_TOTAL_SS; back_eq[l_count_rows] = i; l_count_rows++; @@ -1625,7 +1625,7 @@ ineq(int in_kode) { for (j = 0; j < l_count_rows; j++) { - ineq_array[(size_t)j * (size_t)max_column_count + (size_t)i] = 0.0; + ineq_array[(size_t)j * max_column_count + (size_t)i] = 0.0; } } } @@ -1639,15 +1639,15 @@ ineq(int in_kode) { if (x[i]->type == MB && x[i]->moles < 0.0) { - memcpy((void *) &(ineq_array[(size_t)l_count_rows * (size_t)max_column_count]), - (void *) &(my_array[(size_t)i * ((size_t)count_unknowns + 1)]), + memcpy((void *) &(ineq_array[(size_t)l_count_rows * max_column_count]), + (void *) &(my_array[(size_t)i * (count_unknowns + 1)]), ((size_t) count_unknowns + 1) * sizeof(LDBLE)); back_eq[l_count_rows] = i; for (j = 0; j < count_unknowns; j++) { if (x[j]->type < PP) { - ineq_array[(size_t)l_count_rows * (size_t)max_column_count + (size_t)j] = 0.0; + ineq_array[(size_t)l_count_rows * max_column_count + (size_t)j] = 0.0; } } l_count_rows++; @@ -1662,7 +1662,7 @@ ineq(int in_kode) k = mass_oxygen_unknown->number; for (j = 0; j < l_count_rows + 1; j++) { - ineq_array[(size_t)j * (size_t)max_column_count + (size_t)k] = 0; + ineq_array[(size_t)j * max_column_count + (size_t)k] = 0; } } /* @@ -1675,7 +1675,7 @@ ineq(int in_kode) { for (j = 0; j < l_count_rows; j++) { - ineq_array[(size_t)j * (size_t)max_column_count + (size_t)i] *= pp_column_scale; + ineq_array[(size_t)j * max_column_count + (size_t)i] *= pp_column_scale; } normal[i] = pp_column_scale; } @@ -1709,13 +1709,13 @@ ineq(int in_kode) { for (int j = 0; j < n; j++) { - ineq_array[(size_t)i*((size_t)n+2) + (size_t)j] = ineq_array[(size_t)i*((size_t)count_unknowns+2) + (size_t)j]; + ineq_array[(size_t)i*((size_t)n+2) + (size_t)j] = ineq_array[(size_t)i*(count_unknowns+2) + (size_t)j]; } //if (i > 0) //{ // memcpy((void *) &ineq_array[i*(n+2)], (void *) &ineq_array[i*(count_unknowns+2)], (size_t) (n) * sizeof(LDBLE)); //} - ineq_array[(size_t)i*((size_t)n+2) + (size_t)n] = ineq_array[(size_t)i*((size_t)count_unknowns+2) + (size_t)count_unknowns]; + ineq_array[(size_t)i*((size_t)n+2) + (size_t)n] = ineq_array[(size_t)i*(count_unknowns+2) + count_unknowns]; } } else @@ -1840,7 +1840,7 @@ ineq(int in_kode) //memcpy((void *) &(delta[0]), (void *) &(zero[0]), // (size_t) count_unknowns * sizeof(LDBLE)); memset(&(delta[0]), 0, - (size_t)count_unknowns * sizeof(LDBLE)); + count_unknowns * sizeof(LDBLE)); #endif memcpy((void *) &(delta[0]), (void *) &(delta1[0]), (size_t) n * sizeof(LDBLE)); @@ -1855,7 +1855,7 @@ ineq(int in_kode) { for (j = 0; j < count_unknowns; j++) { - my_array[(size_t)j * ((size_t)count_unknowns + 1) + (size_t)i] /= normal[i]; + my_array[(size_t)j * (count_unknowns + 1) + (size_t)i] /= normal[i]; } } } @@ -1936,7 +1936,7 @@ jacobian_sums(void) } for (i = 1; i < count_unknowns; i++) { - memcpy((void *) &(my_array[(size_t)i * ((size_t)count_unknowns + 1)]), + memcpy((void *) &(my_array[(size_t)i * (count_unknowns + 1)]), (void *) &(my_array[0]), (size_t) count_unknowns * sizeof(LDBLE)); } /* @@ -1971,9 +1971,9 @@ jacobian_sums(void) for (i = 0; i < count_unknowns; i++) { // using straight mu equation - my_array[(size_t)mu_unknown->number * ((size_t)count_unknowns + 1) + i] *= 0.5; + my_array[(size_t)mu_unknown->number * (count_unknowns + 1) + i] *= 0.5; } - my_array[(size_t)mu_unknown->number * ((size_t)count_unknowns + 1) + + my_array[(size_t)mu_unknown->number * (count_unknowns + 1) + (size_t)mu_unknown->number] -= mass_water_aq_x; } /* @@ -1981,7 +1981,7 @@ jacobian_sums(void) */ if (mass_oxygen_unknown != NULL && mu_unknown != NULL) { - my_array[(size_t)mu_unknown->number * ((size_t)count_unknowns + 1) + + my_array[(size_t)mu_unknown->number * (count_unknowns + 1) + (size_t)mass_oxygen_unknown->number] -= mu_x * mass_water_aq_x; } /* @@ -2001,16 +2001,16 @@ jacobian_sums(void) for (i = 0; i < count_unknowns; i++) { - my_array[(size_t)ah2o_unknown->number * ((size_t)count_unknowns + 1) + (size_t)i] *= factor; + my_array[(size_t)ah2o_unknown->number * (count_unknowns + 1) + (size_t)i] *= factor; } // activity of water term - my_array[(size_t)ah2o_unknown->number * ((size_t)count_unknowns + 1) + + my_array[(size_t)ah2o_unknown->number * (count_unknowns + 1) + (size_t)ah2o_unknown->number] -= exp(s_h2o->la * LOG_10); // mass of water term if (mass_oxygen_unknown != NULL) { - my_array[(size_t)ah2o_unknown->number * ((size_t)count_unknowns + 1) + (size_t)mass_oxygen_unknown->number] -= + my_array[(size_t)ah2o_unknown->number * (count_unknowns + 1) + (size_t)mass_oxygen_unknown->number] -= a*y_sum*(x_h2o*(0.5*tanh(lim) + 0.5) + (47.5*x_h2o - 50.0*a*y_sum)/(cosh(lim)*cosh(lim))) / (x_h2o*x_h2o*x_h2o); } @@ -2019,13 +2019,13 @@ jacobian_sums(void) { for (i = 0; i < count_unknowns; i++) { - my_array[(size_t)ah2o_unknown->number * ((size_t)count_unknowns + 1) + i] *= -AH2O_FACTOR; + my_array[(size_t)ah2o_unknown->number * (count_unknowns + 1) + i] *= -AH2O_FACTOR; } - my_array[(size_t)ah2o_unknown->number * ((size_t)count_unknowns + 1) + (size_t)ah2o_unknown->number] -= + my_array[(size_t)ah2o_unknown->number * (count_unknowns + 1) + (size_t)ah2o_unknown->number] -= mass_water_aq_x * exp(s_h2o->la * LOG_10); if (mass_oxygen_unknown != NULL) { - my_array[(size_t)ah2o_unknown->number * ((size_t)count_unknowns + 1) + (size_t)mass_oxygen_unknown->number] -= + my_array[(size_t)ah2o_unknown->number * (count_unknowns + 1) + (size_t)mass_oxygen_unknown->number] -= (exp(s_h2o->la * LOG_10) - 1) * mass_water_aq_x; } } @@ -2054,14 +2054,14 @@ jacobian_sums(void) { for (j = 0; j < count_unknowns; j++) { - my_array[(size_t)x[i]->number * ((size_t)count_unknowns + 1) + (size_t)j] *= + my_array[(size_t)x[i]->number * (count_unknowns + 1) + (size_t)j] *= F_C_MOL / (charge_ptr->Get_specific_area() * charge_ptr->Get_grams()); } - my_array[(size_t)x[i]->number * ((size_t)count_unknowns + 1) + (size_t)x[i]->number] -= + my_array[(size_t)x[i]->number * (count_unknowns + 1) + (size_t)x[i]->number] -= sinh_constant * sqrt(mu_x) * cosh(x[i]->master[0]->s->la * LOG_10); if (mu_unknown != NULL) { - my_array[(size_t)x[i]->number * ((size_t)count_unknowns + 1) + + my_array[(size_t)x[i]->number * (count_unknowns + 1) + (size_t)mu_unknown->number] -= 0.5 * sinh_constant / sqrt(mu_x) * sinh(x[i]->master[0]->s->la * LOG_10); } @@ -2081,10 +2081,10 @@ jacobian_sums(void) { for (j = 0; j < count_unknowns; j++) { - my_array[(size_t)x[i]->number * ((size_t)count_unknowns + 1) + (size_t)j] *= + my_array[(size_t)x[i]->number * (count_unknowns + 1) + (size_t)j] *= F_C_MOL / (charge_ptr->Get_specific_area() * charge_ptr->Get_grams()); } - my_array[(size_t)x[i]->number * ((size_t)count_unknowns + 1) + (size_t)x[i]->number] -= + my_array[(size_t)x[i]->number * (count_unknowns + 1) + (size_t)x[i]->number] -= charge_ptr->Get_capacitance0() * 2 * R_KJ_DEG_MOL * tk_x * LOG_10 / F_KJ_V_EQ; } } @@ -4526,7 +4526,7 @@ residuals(void) /* * Store residuals in array */ - my_array[((size_t)i + 1) * ((size_t)count_unknowns + 1) - 1] = residual[i]; + my_array[((size_t)i + 1) * (count_unknowns + 1) - 1] = residual[i]; sum_residual += fabs(residual[i]); } /* @@ -5334,10 +5334,10 @@ numerical_jacobian(void) } for (i = 1; i < count_unknowns; i++) { - memcpy((void *) &(my_array[(size_t)i * ((size_t)count_unknowns + 1)]), - (void *) &(my_array[0]), (size_t)count_unknowns * sizeof(LDBLE)); + memcpy((void *) &(my_array[(size_t)i * (count_unknowns + 1)]), + (void *) &(my_array[0]), count_unknowns * sizeof(LDBLE)); } - base.resize((size_t)count_unknowns); + base.resize(count_unknowns); base = residual; d = 0.0001; d1 = d * LOG_10; @@ -5444,13 +5444,13 @@ numerical_jacobian(void) LDBLE t = (LDBLE) pow((LDBLE) 10.0, (LDBLE) (DBL_MAX_10_EXP - 50.0)); if (residual[j] > t) { - my_array[(size_t)j * ((size_t)count_unknowns + 1) + (size_t)i] = -pow(10.0, DBL_MAX_10_EXP - 50.0); + my_array[(size_t)j * (count_unknowns + 1) + (size_t)i] = -pow(10.0, DBL_MAX_10_EXP - 50.0); } else { - my_array[(size_t)j * ((size_t)count_unknowns + 1) + (size_t)i] = -(residual[j] - base[j]) / d2; + my_array[(size_t)j * (count_unknowns + 1) + (size_t)i] = -(residual[j] - base[j]) / d2; if (x[i]->type == MH2O) // DL_pitz - my_array[(size_t)j * ((size_t)count_unknowns + 1) + (size_t)i] *= mass_water_aq_x; + my_array[(size_t)j * (count_unknowns + 1) + (size_t)i] *= mass_water_aq_x; } } else if (residual[j] < -1.0e101) @@ -5458,21 +5458,21 @@ numerical_jacobian(void) LDBLE t = pow((LDBLE) 10.0, (LDBLE) (DBL_MIN_10_EXP + 50.0)); if (residual[j] < -t) { - my_array[(size_t)j * ((size_t)count_unknowns + 1) + (size_t)i] = pow(10.0, DBL_MIN_10_EXP + 50.0); + my_array[(size_t)j * (count_unknowns + 1) + (size_t)i] = pow(10.0, DBL_MIN_10_EXP + 50.0); } else { - my_array[(size_t)j * ((size_t)count_unknowns + 1) + (size_t)i] = -(residual[j] - base[j]) / d2; + my_array[(size_t)j * (count_unknowns + 1) + (size_t)i] = -(residual[j] - base[j]) / d2; if (x[i]->type == MH2O) // DL_pitz - my_array[(size_t)j * ((size_t)count_unknowns + 1) + (size_t)i] *= mass_water_aq_x; + my_array[(size_t)j * (count_unknowns + 1) + (size_t)i] *= mass_water_aq_x; } } else { - my_array[(size_t)j * ((size_t)count_unknowns + 1) + (size_t)i] = -(residual[j] - base[j]) / d2; + my_array[(size_t)j * (count_unknowns + 1) + (size_t)i] = -(residual[j] - base[j]) / d2; if (x[i]->type == MH2O) // DL_pitz - my_array[(size_t)j * ((size_t)count_unknowns + 1) + (size_t)i] *= mass_water_aq_x; - if (!PHR_ISFINITE(my_array[(size_t)j * ((size_t)count_unknowns + 1) + (size_t)i])) + my_array[(size_t)j * (count_unknowns + 1) + (size_t)i] *= mass_water_aq_x; + if (!PHR_ISFINITE(my_array[(size_t)j * (count_unknowns + 1) + (size_t)i])) { //fprintf(stderr, "oops, got NaN: %e, %e, %e, %e\n", residual[j], base[j], d2, array[j * (count_unknowns + 1) + i]); } @@ -5496,10 +5496,10 @@ numerical_jacobian(void) break; case MH: s_eminus->la -= d; - if (my_array[(size_t)i * ((size_t)count_unknowns + 1) + (size_t)i] == 0) + if (my_array[(size_t)i * (count_unknowns + 1) + (size_t)i] == 0) { /*output_msg(sformatf( "Zero diagonal for MH\n")); */ - my_array[(size_t)i * ((size_t)count_unknowns + 1) + (size_t)i] = + my_array[(size_t)i * (count_unknowns + 1) + (size_t)i] = under(s_h2->lm) * 2; } break; diff --git a/parse.cpp b/parse.cpp index 63883fce..beb7fc58 100644 --- a/parse.cpp +++ b/parse.cpp @@ -145,7 +145,7 @@ parse_eq(char *eqn, struct elt_list **elt_ptr, int association) /* * Malloc space and store element data for return */ - *elt_ptr = (struct elt_list *) PHRQ_malloc(((size_t)count_elts + 1) * + *elt_ptr = (struct elt_list *) PHRQ_malloc((count_elts + 1) * sizeof(struct elt_list)); if (*elt_ptr == NULL) { @@ -604,7 +604,7 @@ get_elts_in_species(const char **t_ptr, LDBLE coef) } if (count_elts >= (int)elt_list.size()) { - elt_list.resize((size_t)count_elts + 1); + elt_list.resize(count_elts + 1); } elt_list[count_elts].elt = element_store(element.c_str()); if (get_num(t_ptr, &d) == ERROR) @@ -618,7 +618,7 @@ get_elts_in_species(const char **t_ptr, LDBLE coef) */ if (count_elts >= (int)elt_list.size()) { - elt_list.resize((size_t)count_elts + 1); + elt_list.resize(count_elts + 1); } continue; } @@ -870,7 +870,7 @@ get_secondary_in_species(const char **t_ptr, LDBLE coef) */ if (count_elts >= (int)elt_list.size()) { - elt_list.resize((size_t)count_elts + 1); + elt_list.resize(count_elts + 1); } continue; } @@ -1021,7 +1021,7 @@ get_species(const char **cptr) int l; if ((size_t) count_trxn + 1 > trxn.token.size()) - trxn.token.resize((size_t)count_trxn + 1); + trxn.token.resize(count_trxn + 1); /* coefficient */ if (get_coef(&(trxn.token[count_trxn].coef), cptr) == ERROR) { diff --git a/pitzer.cpp b/pitzer.cpp index c7b7247c..182e2755 100644 --- a/pitzer.cpp +++ b/pitzer.cpp @@ -2063,9 +2063,9 @@ Restart: residuals(); for (j = 0; j < count_unknowns; j++) { - my_array[(size_t)j * ((size_t)count_unknowns + 1) + (size_t)i] = -(residual[j] - base[j]) / d2; + my_array[(size_t)j * (count_unknowns + 1) + (size_t)i] = -(residual[j] - base[j]) / d2; if (x[i]->type == MH2O) // DL_pitz - my_array[(size_t)j * ((size_t)count_unknowns + 1) + (size_t)i] *= mass_water_aq_x; + my_array[(size_t)j * (count_unknowns + 1) + (size_t)i] *= mass_water_aq_x; } switch (x[i]->type) { @@ -2083,9 +2083,9 @@ Restart: break; case MH: s_eminus->la -= d; - if (my_array[(size_t)i * ((size_t)count_unknowns + 1) + (size_t)i] == 0) + if (my_array[(size_t)i * (count_unknowns + 1) + (size_t)i] == 0) { - my_array[(size_t)i * ((size_t)count_unknowns + 1) + (size_t)i] = + my_array[(size_t)i * (count_unknowns + 1) + (size_t)i] = exp(s_h2->lm * LOG_10) * 2; } break; diff --git a/prep.cpp b/prep.cpp index 0bf606ac..da8a14d2 100644 --- a/prep.cpp +++ b/prep.cpp @@ -77,9 +77,9 @@ prep(void) /* * Allocate space for array */ - my_array.resize(((size_t)max_unknowns + 1) * (size_t)max_unknowns); - delta.resize((size_t)max_unknowns); - residual.resize((size_t)max_unknowns); + my_array.resize((max_unknowns + 1) * max_unknowns); + delta.resize(max_unknowns); + residual.resize(max_unknowns); for (int j = 0; j < max_unknowns; j++) { residual[j] = 0; @@ -881,7 +881,7 @@ build_jacobian_sums(int k) /* term for water, sum of all surfaces */ source = &s[k]->tot_dh2o_moles; target = &(my_array[(size_t)mb_unknowns[i].unknown->number * - ((size_t)count_unknowns + 1) + (size_t)mass_oxygen_unknown->number]); + (count_unknowns + 1) + (size_t)mass_oxygen_unknown->number]); if (debug_prep == TRUE) { output_msg(sformatf( "\t\t%-24s%10.3f\t%d\t%d", @@ -901,7 +901,7 @@ build_jacobian_sums(int k) cxxSurfaceCharge *charge_ptr = use.Get_surface_ptr()->Find_charge(x[j]->surface_charge); source = s_diff_layer[k][charge_ptr->Get_name()].Get_dx_moles_address(); target = &(my_array[(size_t)mb_unknowns[i].unknown->number * - ((size_t)count_unknowns + 1) + (size_t)x[j]->number]); + (count_unknowns + 1) + (size_t)x[j]->number]); if (debug_prep == TRUE) { output_msg(sformatf( "\t\t%-24s%10.3f\t%d\t%d", @@ -940,7 +940,7 @@ build_jacobian_sums(int k) { source = s_diff_layer[k][charge_ptr->Get_name()].Get_drelated_moles_address(); target = &(my_array[(size_t)mb_unknowns[i].unknown->number * - ((size_t)count_unknowns + 1) + (size_t)x[kk]->number]); + (count_unknowns + 1) + (size_t)x[kk]->number]); if (debug_prep == TRUE) { output_msg(sformatf( @@ -969,7 +969,7 @@ build_jacobian_sums(int k) { source = s_diff_layer[k][charge_ptr->Get_name()].Get_dx_moles_address(); target = &(my_array[(size_t)mb_unknowns[i].unknown->number * - ((size_t)count_unknowns + 1) + (size_t)x[j]->number]); + (count_unknowns + 1) + (size_t)x[j]->number]); if (debug_prep == TRUE) { output_msg(sformatf("\t\t%-24s%10.3f\t%d\t%d", "dg/dlny", @@ -997,7 +997,7 @@ build_jacobian_sums(int k) { source = s_diff_layer[k][charge_ptr->Get_name()].Get_drelated_moles_address(); target = &(my_array[(size_t)(size_t)mb_unknowns[i].unknown->number * - ((size_t)count_unknowns + 1) + (size_t)x[kk]->number]); + (count_unknowns + 1) + (size_t)x[kk]->number]); if (debug_prep == TRUE) { output_msg(sformatf( @@ -1015,7 +1015,7 @@ build_jacobian_sums(int k) /* term for water, for same surfaces */ source = s_diff_layer[k][charge_ptr->Get_name()].Get_dh2o_moles_address(); target = &(my_array[(size_t)mb_unknowns[i].unknown->number * - ((size_t)count_unknowns + 1) + + (count_unknowns + 1) + (size_t)mass_oxygen_unknown->number]); if (debug_prep == TRUE) { @@ -2757,8 +2757,8 @@ add_potential_factor(void) /* * Make sure there is space */ - if ((size_t)count_trxn + 1 > trxn.token.size()) - trxn.token.resize((size_t)count_trxn + 1); + if (count_trxn + 1 > trxn.token.size()) + trxn.token.resize(count_trxn + 1); /* * Include psi in mass action equation */ @@ -2851,8 +2851,8 @@ add_cd_music_factors(int n) /* * Make sure there is space */ - if ((size_t)count_trxn + 3 > trxn.token.size()) - trxn.token.resize((size_t)count_trxn + 3); + if (count_trxn + 3 > trxn.token.size()) + trxn.token.resize(count_trxn + 3); /* * Include psi in mass action equation */ @@ -3354,7 +3354,7 @@ setup_surface(void) struct unknown *unknown_ptr = find_surface_charge_unknown(token, SURF_PSI); if (unknown_ptr != NULL) { - x[(size_t)count_unknowns - 1]->potential_unknown = unknown_ptr; + x[count_unknowns - 1]->potential_unknown = unknown_ptr; } else { @@ -3386,8 +3386,8 @@ setup_surface(void) x[count_unknowns]->master = master_ptr_list; x[count_unknowns]->master[0]->unknown = x[count_unknowns]; x[count_unknowns]->moles = 0.0; - x[(size_t)count_unknowns - 1]->potential_unknown = x[count_unknowns]; - x[count_unknowns]->surface_comp = x[(size_t)count_unknowns - 1]->surface_comp; + x[count_unknowns - 1]->potential_unknown = x[count_unknowns]; + x[count_unknowns]->surface_comp = x[count_unknowns - 1]->surface_comp; count_unknowns++; } } @@ -4652,7 +4652,7 @@ setup_unknowns(void) /* * Allocate space for pointer array and structures */ - x.resize((size_t)max_unknowns); + x.resize(max_unknowns); for (i = 0; i < max_unknowns; i++) { x[i] = (struct unknown *) unknown_alloc(); @@ -4792,7 +4792,7 @@ store_jacob0(int row, int column, LDBLE coef) size_t count_sum_jacob0 = sum_jacob0.size(); sum_jacob0.resize(count_sum_jacob0 + 1); sum_jacob0[count_sum_jacob0].target = - &(my_array[(size_t)row * ((size_t)count_unknowns + 1) + (size_t)column]); + &(my_array[(size_t)row * (count_unknowns + 1) + (size_t)column]); sum_jacob0[count_sum_jacob0].coef = coef; return (OK); } @@ -5159,7 +5159,7 @@ write_mb_for_species_list(int n) { if (count_elts >= (int)elt_list.size()) { - elt_list.resize((size_t)count_elts + 1); + elt_list.resize(count_elts + 1); } elt_list[count_elts].elt = element_h_one; elt_list[count_elts].coef = elt_list[i].coef * 2; @@ -5212,7 +5212,7 @@ write_phase_sys_total(int n) { if (count_elts >= (int)elt_list.size()) { - elt_list.resize((size_t)count_elts + 1); + elt_list.resize(count_elts + 1); } elt_list[count_elts].elt = element_h_one; elt_list[count_elts].coef = elt_list[i].coef * 2; diff --git a/read.cpp b/read.cpp index 407a0b45..563e1847 100644 --- a/read.cpp +++ b/read.cpp @@ -7332,7 +7332,7 @@ read_advection(void) /* * Fill in data for punch */ - advection_punch.resize((size_t)count_ad_cells + 1); + advection_punch.resize(count_ad_cells + 1); if (punch_temp.size() != 0) { for (i = 0; i < count_ad_cells; i++) @@ -7361,7 +7361,7 @@ read_advection(void) /* * Fill in data for print */ - advection_print.resize((size_t)count_ad_cells + 1); + advection_print.resize(count_ad_cells + 1); if (print_temp.size() != 0) { for (i = 0; i < count_ad_cells; i++) diff --git a/sit.cpp b/sit.cpp index 2e2f3745..6328937e 100644 --- a/sit.cpp +++ b/sit.cpp @@ -892,7 +892,7 @@ Restart: residuals(); for (j = 0; j < count_unknowns; j++) { - my_array[(size_t)j * ((size_t)count_unknowns + 1) + (size_t)i] = + my_array[(size_t)j * (count_unknowns + 1) + (size_t)i] = -(residual[j] - base[j]) / d2; } switch (x[i]->type) @@ -911,9 +911,9 @@ Restart: break; case MH: s_eminus->la -= d; - if (my_array[(size_t)i * ((size_t)count_unknowns + 1) + (size_t)i] == 0) + if (my_array[(size_t)i * (count_unknowns + 1) + (size_t)i] == 0) { - my_array[(size_t)i * ((size_t)count_unknowns + 1) + (size_t)i] = + my_array[(size_t)i * (count_unknowns + 1) + (size_t)i] = exp(s_h2->lm * LOG_10) * 2; } break; diff --git a/structures.cpp b/structures.cpp index c558f6f4..ddbb3cb9 100644 --- a/structures.cpp +++ b/structures.cpp @@ -342,7 +342,7 @@ elt_list_combine(void) { return (OK); } - qsort(&elt_list[0], (size_t)count_elts, + qsort(&elt_list[0], count_elts, sizeof(struct elt_list), Phreeqc::elt_list_compare); j = 0; for (i = 1; i < count_elts; i++) @@ -398,11 +398,11 @@ elt_list_dup(struct elt_list *elt_list_ptr_old) * Malloc space and store element data */ elt_list_ptr_new = (struct elt_list *) PHRQ_malloc( - ((size_t)count_totals + 1) * sizeof(struct elt_list)); + (count_totals + 1) * sizeof(struct elt_list)); if (elt_list_ptr_new == NULL) malloc_error(); memcpy(elt_list_ptr_new, elt_list_ptr_old, - ((size_t)count_totals + 1) * sizeof(struct elt_list)); + (count_totals + 1) * sizeof(struct elt_list)); return (elt_list_ptr_new); } @@ -463,7 +463,7 @@ elt_list_save(void) * Malloc space and store element data */ elt_list_ptr = (struct elt_list*)PHRQ_malloc( - ((size_t)count_elts + 1) * sizeof(struct elt_list)); + (count_elts + 1) * sizeof(struct elt_list)); if (elt_list_ptr == NULL) { malloc_error(); @@ -527,7 +527,7 @@ inverse_alloc(void) */ { struct inverse *inverse_ptr = NULL; - inverse.resize((size_t)count_inverse + 1); + inverse.resize(count_inverse + 1); inverse_ptr = &(inverse[count_inverse++]); /* * Initialize variables @@ -2329,8 +2329,8 @@ trxn_add(cxxChemRxn &r_ptr, LDBLE coef, int combine) */ for (size_t j = 0; j < r_ptr.Get_tokens().size(); j++) { - if ((size_t)count_trxn + 1 > trxn.token.size()) - trxn.token.resize((size_t)count_trxn + 1); + if (count_trxn + 1 > trxn.token.size()) + trxn.token.resize(count_trxn + 1); trxn.token[count_trxn].name = r_ptr.Get_tokens()[j].name; trxn.token[count_trxn].s = r_ptr.Get_tokens()[j].s; trxn.token[count_trxn].coef = coef * r_ptr.Get_tokens()[j].coef; @@ -2391,8 +2391,8 @@ trxn_add(struct reaction *r_ptr, LDBLE coef, int combine) next_token = r_ptr->token; while (next_token->s != NULL) { - if ((size_t)count_trxn + 1 > trxn.token.size()) - trxn.token.resize((size_t)count_trxn + 1); + if (count_trxn + 1 > trxn.token.size()) + trxn.token.resize(count_trxn + 1); trxn.token[count_trxn].name = next_token->s->name; trxn.token[count_trxn].s = next_token->s; trxn.token[count_trxn].coef = coef * next_token->coef; @@ -2446,8 +2446,8 @@ trxn_add_phase(struct reaction *r_ptr, LDBLE coef, int combine) next_token = r_ptr->token; while (next_token->s != NULL || next_token->name != NULL) { - if ((size_t)count_trxn + 1 > trxn.token.size()) - trxn.token.resize((size_t)count_trxn + 1); + if (count_trxn + 1 > trxn.token.size()) + trxn.token.resize(count_trxn + 1); if (next_token->s != NULL) { trxn.token[count_trxn].name = next_token->s->name; diff --git a/tally.cpp b/tally.cpp index 461f833c..bd06c3cc 100644 --- a/tally.cpp +++ b/tally.cpp @@ -1207,13 +1207,13 @@ extend_tally_table(void) * increments number of columns */ tally_table = (struct tally *) PHRQ_realloc((void *) tally_table, - ((size_t)count_tally_table_columns + 1) * sizeof(struct tally)); + (count_tally_table_columns + 1) * sizeof(struct tally)); if (tally_table == NULL) malloc_error(); for (i = 0; i < 3; i++) { tally_table[count_tally_table_columns].total[i] = (struct tally_buffer *) - PHRQ_malloc((size_t)count_tally_table_rows * sizeof(struct tally_buffer)); + PHRQ_malloc(count_tally_table_rows * sizeof(struct tally_buffer)); if (tally_table[count_tally_table_columns].total[i] == NULL) malloc_error(); for (j = 0; j < count_tally_table_rows; j++) diff --git a/tidy.cpp b/tidy.cpp index 2d19afc9..a4256db1 100644 --- a/tidy.cpp +++ b/tidy.cpp @@ -1337,7 +1337,7 @@ tidy_inverse(void) * Save list of master species in inv_elts structure */ std::vector inv_elts; - inv_elts.resize((size_t)count_in); + inv_elts.resize(count_in); count_in = 0; for (j = 0; j < (int)master.size(); j++) { @@ -2841,8 +2841,8 @@ species_rxn_to_trxn(struct species *s_ptr) trxn.token[i].unknown = NULL; trxn.token[i].coef = s_ptr->rxn->token[i].coef; count_trxn = i + 1; - if ((size_t)count_trxn + 1 > trxn.token.size()) - trxn.token.resize((size_t)count_trxn + 1); + if (count_trxn + 1 > trxn.token.size()) + trxn.token.resize(count_trxn + 1); } return (OK); } @@ -2881,8 +2881,8 @@ phase_rxn_to_trxn(struct phase *phase_ptr, struct reaction *rxn_ptr) trxn.token[i].unknown = NULL; trxn.token[i].coef = rxn_ptr->token[i].coef; count_trxn = i + 1; - if ((size_t)count_trxn + 1 > trxn.token.size()) - trxn.token.resize((size_t)count_trxn + 1); + if (count_trxn + 1 > trxn.token.size()) + trxn.token.resize(count_trxn + 1); } return (OK); } diff --git a/transport.cpp b/transport.cpp index 4f0fa901..fce12792 100644 --- a/transport.cpp +++ b/transport.cpp @@ -248,7 +248,7 @@ transport(void) warning_msg(error_string); } current_cells = (struct CURRENT_CELLS *) PHRQ_malloc( - ((size_t)count_cells + 1) * sizeof(struct CURRENT_CELLS)); + (count_cells + 1) * sizeof(struct CURRENT_CELLS)); if (current_cells == NULL) malloc_error(); for (int i = 0; i < count_cells + 1; i++) @@ -289,7 +289,7 @@ transport(void) if (implicit && current_cells == NULL) { current_cells = (struct CURRENT_CELLS *) PHRQ_malloc( - ((size_t)count_cells + 1) * sizeof(struct CURRENT_CELLS)); + (count_cells + 1) * sizeof(struct CURRENT_CELLS)); if (current_cells == NULL) malloc_error(); for (int i = 0; i < count_cells + 1; i++) @@ -1102,10 +1102,10 @@ init_mix(void) bool warning = false; int i, l_nmix; LDBLE *m, *m1; - m = (LDBLE *)PHRQ_malloc(((size_t)count_cells + 1) * sizeof(LDBLE)); + m = (LDBLE *)PHRQ_malloc((count_cells + 1) * sizeof(LDBLE)); if (m == NULL) malloc_error(); - m1 = (LDBLE *)PHRQ_malloc(((size_t)count_cells + 1) * sizeof(LDBLE)); + m1 = (LDBLE *)PHRQ_malloc((count_cells + 1) * sizeof(LDBLE)); if (m1 == NULL) malloc_error(); for (i = 0; i < count_cells + 1; i++) @@ -1554,15 +1554,15 @@ init_heat_mix(int l_nmix) /* * Initialize arrays... */ - heat_mix_array = (LDBLE *)PHRQ_malloc(((size_t)count_cells + 2) * sizeof(LDBLE)); + heat_mix_array = (LDBLE *)PHRQ_malloc((count_cells + 2) * sizeof(LDBLE)); if (heat_mix_array == NULL) malloc_error(); - temp1 = (LDBLE *)PHRQ_malloc(((size_t)count_cells + 2) * sizeof(LDBLE)); + temp1 = (LDBLE *)PHRQ_malloc((count_cells + 2) * sizeof(LDBLE)); if (temp1 == NULL) malloc_error(); - temp2 = (LDBLE *)PHRQ_malloc(((size_t)count_cells + 2) * sizeof(LDBLE)); + temp2 = (LDBLE *)PHRQ_malloc((count_cells + 2) * sizeof(LDBLE)); if (temp2 == NULL) malloc_error(); /* @@ -2026,7 +2026,7 @@ fill_spec(int l_cell_no, int ref_cell) if (i3 + count_spec + 1 > sol_D[l_cell_no].spec_size) { sol_D[l_cell_no].spec = (struct spec *) PHRQ_realloc(sol_D[l_cell_no].spec, - ((size_t)i3 + (size_t)count_spec + 1 + (size_t)size_xt) * sizeof(struct spec)); + ((size_t)i3 + count_spec + 1 + (size_t)size_xt) * sizeof(struct spec)); if (sol_D[l_cell_no].spec == NULL) malloc_error(); sol_D[l_cell_no].spec_size = i3 + count_spec + 1 + size_xt; @@ -2048,7 +2048,7 @@ fill_spec(int l_cell_no, int ref_cell) if (count_spec >= sol_D[l_cell_no].spec_size) { sol_D[l_cell_no].spec = (struct spec *) PHRQ_realloc(sol_D[l_cell_no].spec, - ((size_t)count_spec + (size_t)size_xt) * sizeof(struct spec)); + (count_spec + (size_t)size_xt) * sizeof(struct spec)); if (sol_D[l_cell_no].spec == NULL) malloc_error(); sol_D[l_cell_no].spec_size = count_spec + size_xt; @@ -2220,20 +2220,20 @@ diffuse_implicit(LDBLE DDt, int stagnant) comp += 1; if (Ct2 == NULL) - Ct2 = (LDBLE *) PHRQ_malloc(((size_t)count_cells + 2 + (size_t)stagnant * (size_t)count_cells) * sizeof(LDBLE)); + Ct2 = (LDBLE *) PHRQ_malloc((count_cells + 2 + (size_t)stagnant * count_cells) * sizeof(LDBLE)); if (Ct2 == NULL) malloc_error(); if (l_tk_x2 == NULL) - l_tk_x2 = (LDBLE *) PHRQ_malloc(((size_t)count_cells + 2 + (size_t)stagnant * (size_t)count_cells) * sizeof(LDBLE)); + l_tk_x2 = (LDBLE *) PHRQ_malloc((count_cells + 2 + (size_t)stagnant * count_cells) * sizeof(LDBLE)); if (l_tk_x2 == NULL) malloc_error(); if (A == NULL) { - A = (LDBLE **)PHRQ_malloc(((size_t)count_cells + 2 + (size_t)stagnant * (size_t)count_cells) * sizeof(LDBLE *)); + A = (LDBLE **)PHRQ_malloc((count_cells + 2 + (size_t)stagnant * count_cells) * sizeof(LDBLE *)); if (A == NULL) malloc_error(); for (i = 0; i < count_cells + 2 + stagnant * count_cells; i++) { if (stagnant) - A[i] = (LDBLE *)PHRQ_calloc((2 * (size_t)count_cells + 2), sizeof(LDBLE)); + A[i] = (LDBLE *)PHRQ_calloc((2 * count_cells + 2), sizeof(LDBLE)); else A[i] = (LDBLE *)PHRQ_malloc(3 * sizeof(LDBLE)); if (A[i] == NULL) malloc_error(); @@ -2241,12 +2241,12 @@ diffuse_implicit(LDBLE DDt, int stagnant) } if (LU == NULL) { - LU = (LDBLE **)PHRQ_malloc(((size_t)count_cells + 2 + (size_t)stagnant * (size_t)count_cells) * sizeof(LDBLE *)); + LU = (LDBLE **)PHRQ_malloc((count_cells + 2 + (size_t)stagnant * count_cells) * sizeof(LDBLE *)); if (LU == NULL) malloc_error(); for (i = 0; i < count_cells + 2 + stagnant * count_cells; i++) { if (stagnant) - LU[i] = (LDBLE *)PHRQ_calloc((2 * (size_t)count_cells + 2), sizeof(LDBLE)); + LU[i] = (LDBLE *)PHRQ_calloc((2 * count_cells + 2), sizeof(LDBLE)); else LU[i] = (LDBLE *)PHRQ_malloc(3 * sizeof(LDBLE)); if (LU[i] == NULL) malloc_error(); @@ -2254,7 +2254,7 @@ diffuse_implicit(LDBLE DDt, int stagnant) } if (mixf == NULL) { - mixf = (LDBLE **)PHRQ_malloc(((size_t)count_cells + 2) * sizeof(LDBLE *)); + mixf = (LDBLE **)PHRQ_malloc((count_cells + 2) * sizeof(LDBLE *)); if (mixf == NULL) malloc_error(); for (i = 0; i < count_cells + 2; i++) { @@ -2265,7 +2265,7 @@ diffuse_implicit(LDBLE DDt, int stagnant) { if (mixf_stag == NULL) { - mixf_stag = (LDBLE **)PHRQ_malloc(((size_t)count_cells + 2) * sizeof(LDBLE *)); + mixf_stag = (LDBLE **)PHRQ_malloc((count_cells + 2) * sizeof(LDBLE *)); if (mixf_stag == NULL) malloc_error(); for (i = 0; i < count_cells + 2; i++) { @@ -2806,7 +2806,7 @@ diffuse_implicit(LDBLE DDt, int stagnant) ((dV_dcell > 0) && ((cell_data[i].potV + dVc) > cell_data[count_cells + 1].potV)) || ((dV_dcell < 0) && ((cell_data[i].potV + dVc) < cell_data[count_cells + 1].potV))) { - dVc = (cell_data[(size_t)count_cells + 1].potV - cell_data[i].potV) / ((double)count_cells + 1 - (double)i); + dVc = (cell_data[count_cells + 1].potV - cell_data[i].potV) / ((double)count_cells + 1 - (double)i); } cell_data[i + 1].potV = cell_data[i].potV + dVc; } @@ -2849,12 +2849,12 @@ diffuse_implicit(LDBLE DDt, int stagnant) ct[i].m_s = (struct M_S *) free_check_null(ct[i].m_s); if (ct[i].m_s == NULL) { - ct[i].m_s = (struct M_S *) PHRQ_malloc(((size_t)count_m_s + 5) * sizeof(struct M_S)); + ct[i].m_s = (struct M_S *) PHRQ_malloc((count_m_s + 5) * sizeof(struct M_S)); ct[i].m_s_size = count_m_s + 5; } else if (count_m_s > ct[i].m_s_size) { - ct[i].m_s = (struct M_S *) PHRQ_realloc(ct[i].m_s, ((size_t)count_m_s + 5) * sizeof(struct M_S)); + ct[i].m_s = (struct M_S *) PHRQ_realloc(ct[i].m_s, (count_m_s + 5) * sizeof(struct M_S)); ct[i].m_s_size = count_m_s + 5; } if (ct[i].m_s == NULL) diff --git a/utilities.cpp b/utilities.cpp index c022f811..577e9a63 100644 --- a/utilities.cpp +++ b/utilities.cpp @@ -21,7 +21,7 @@ add_elt_list(struct elt_list *elt_list_ptr, LDBLE coef) { if (count_elts >= (int)elt_list.size()) { - elt_list.resize((size_t)count_elts + 1); + elt_list.resize(count_elts + 1); } elt_list[count_elts].elt = elt_list_ptr1->elt; elt_list[count_elts].coef = elt_list_ptr1->coef * coef; @@ -66,7 +66,7 @@ add_elt_list_multi_surf(struct elt_list *elt_list_ptr, LDBLE coef, struct elemen { if (count_elts >= (int)elt_list.size()) { - elt_list.resize((size_t)count_elts + 1); + elt_list.resize(count_elts + 1); } if (elt_list_ptr1->elt == surf_elt_ptr) { @@ -93,7 +93,7 @@ add_elt_list_multi_surf(struct elt_list *elt_list_ptr, LDBLE coef, struct elemen { if (count_elts >= (int)elt_list.size()) { - elt_list.resize((size_t)count_elts + 1); + elt_list.resize(count_elts + 1); } if (elt_list_ptr1->elt == surf_elt_ptr) { @@ -114,7 +114,7 @@ add_elt_list(const cxxNameDouble & nd, LDBLE coef) { if (count_elts >= (int)elt_list.size()) { - elt_list.resize((size_t)count_elts + 1); + elt_list.resize(count_elts + 1); } elt_list[count_elts].elt = element_store(cit->first.c_str()); elt_list[count_elts].coef = cit->second * coef; @@ -1517,7 +1517,7 @@ string_pad(const char *str, int i) max = l; if (l < i) max = i; - str_ptr = (char *) PHRQ_malloc((((size_t)max + 1) * sizeof(char))); + str_ptr = (char *) PHRQ_malloc(((max + 1) * sizeof(char))); if (str_ptr == NULL) malloc_error(); strcpy(str_ptr, str); From 868522548682ba9dc5b4e7671258fe4956086fc4 Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Tue, 30 Mar 2021 08:39:43 -0600 Subject: [PATCH 28/53] fixed clang errors, needed .c_str --- Phreeqc.cpp | 1 - inverse.cpp | 4 +--- kinetics.cpp | 4 ++-- mainsubs.cpp | 2 +- spread.cpp | 2 +- tidy.cpp | 4 ++-- utilities.cpp | 2 +- 7 files changed, 8 insertions(+), 11 deletions(-) diff --git a/Phreeqc.cpp b/Phreeqc.cpp index d9c5eca3..ee2a3239 100644 --- a/Phreeqc.cpp +++ b/Phreeqc.cpp @@ -1629,7 +1629,6 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) count_master = 0; max_master = MAX_MASTER; */ - int count_master = (int)pSrc->master.size(); for (size_t i = 0; i < master.size(); i++) { master.resize(i + 1); diff --git a/inverse.cpp b/inverse.cpp index 613fc041..cfc38096 100644 --- a/inverse.cpp +++ b/inverse.cpp @@ -1955,9 +1955,7 @@ print_model(struct inverse *inv_ptr) { if (Utilities::strcmp_nocase(phases[i1]->name, col_name[i])) continue; - - reaction_ptr = phases[i1]->rxn_s; - + reaction_ptr = phases[i1]->rxn_s; for (i2 = 0; i2 < inv_ptr->count_solns; i2++) { solution_ptr = Utilities::Rxn_find(Rxn_solution_map, inv_ptr->solns[i2]); diff --git a/kinetics.cpp b/kinetics.cpp index ce88ae4e..15a667d2 100644 --- a/kinetics.cpp +++ b/kinetics.cpp @@ -212,7 +212,7 @@ RESTART: // if limiting rates, jump to here const char* ptr = formula.c_str(); if (get_elts_in_species(&ptr, -coef*exchange_ptr->Get_exchange_comps()[j].Get_phase_proportion()) == ERROR) { - error_string = sformatf("Error in -formula: %s", formula); + error_string = sformatf("Error in -formula: %s", formula.c_str()); error_msg(error_string, CONTINUE); } } @@ -249,7 +249,7 @@ RESTART: // if limiting rates, jump to here { if (get_elts_in_species(&cptr, -coef * surface_comp_ptr->Get_phase_proportion()) == ERROR) { - error_string = sformatf("Error in -formula: %s", temp_formula); + error_string = sformatf("Error in -formula: %s", temp_formula.c_str()); error_msg(error_string, CONTINUE); } } diff --git a/mainsubs.cpp b/mainsubs.cpp index 22a7aea8..7b4a5447 100644 --- a/mainsubs.cpp +++ b/mainsubs.cpp @@ -2232,9 +2232,9 @@ do_status(void) screen_msg("\n"); } //pr.headings = TRUE; // set in class_main; not set for IPhreeqc - LDBLE ext = (double) clock() / CLOCKS_PER_SEC; #define TESTING #ifndef TESTING + LDBLE ext = (double)clock() / CLOCKS_PER_SEC; dup_print(sformatf("End of Run after %g Seconds.", ext), TRUE); screen_msg(sformatf("\nEnd of Run after %g Seconds.\n", ext)); #endif diff --git a/spread.cpp b/spread.cpp index 4ba5578c..50500580 100644 --- a/spread.cpp +++ b/spread.cpp @@ -1083,7 +1083,7 @@ string_to_spread_row(char *string) { input_error++; error_msg("Unknown input in string_to_spread_row keyword.", CONTINUE); - error_string = sformatf("\tcopy_token j: %d, token: %s\n", j, token); + error_string = sformatf("\tcopy_token j: %d, token: %s\n", j, token.c_str()); error_msg(error_string, CONTINUE); error_msg(line_save, CONTINUE); } diff --git a/tidy.cpp b/tidy.cpp index a4256db1..ec39a57c 100644 --- a/tidy.cpp +++ b/tidy.cpp @@ -569,7 +569,7 @@ add_other_logk(LDBLE * source_k, std::vector &add_logk) input_error++; error_string = sformatf( "Could not find named temperature expression, %s\n", - token); + token.c_str()); error_msg(error_string, CONTINUE); return (ERROR); } @@ -634,7 +634,7 @@ add_logks(struct logk *logk_ptr, int repeats) input_error++; error_string = sformatf( "Could not find named temperature expression, %s\n", - token); + token.c_str()); error_msg(error_string, CONTINUE); return (ERROR); } diff --git a/utilities.cpp b/utilities.cpp index 577e9a63..7264d331 100644 --- a/utilities.cpp +++ b/utilities.cpp @@ -577,7 +577,7 @@ get_token(const char** eqnaddr, std::string& string, LDBLE* l_z, int* l) { error_string = sformatf( "No final bracket \"]\" for element name, %s.", - string); + string.c_str()); error_msg(error_string, CONTINUE); return (ERROR); } From 8a6cef5116c6235c77028d8152c4a3ffbfe0b8ca Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Tue, 30 Mar 2021 16:04:37 -0600 Subject: [PATCH 29/53] vectorized save_values --- PBasic.cpp | 97 ++++++++++----------------------------------- global_structures.h | 3 +- structures.cpp | 41 +++++-------------- 3 files changed, 32 insertions(+), 109 deletions(-) diff --git a/PBasic.cpp b/PBasic.cpp index 08e12fb1..0ed075f6 100644 --- a/PBasic.cpp +++ b/PBasic.cpp @@ -2039,9 +2039,6 @@ factor(struct LOC_exec * LINK) facttok = LINK->t; LINK->t = LINK->t->next; n.stringval = false; - s_v.count_subscripts = 0; - /*s_v.subscripts = (int *) PhreeqcPtr->PHRQ_malloc (sizeof (int)); */ - s_v.subscripts = NULL; switch (facttok->kind) { @@ -2563,28 +2560,14 @@ factor(struct LOC_exec * LINK) { require(toklp, LINK); - s_v.count_subscripts = 0; + s_v.subscripts.clear(); /* get first subscript */ if (LINK->t != NULL && LINK->t->kind != tokrp) { i = intexpr(LINK); - if (s_v.subscripts == NULL) - { - s_v.subscripts = (int*)PhreeqcPtr->PHRQ_malloc(sizeof(int)); - if (s_v.subscripts == NULL) - PhreeqcPtr->malloc_error(); - } - s_v.subscripts = (int*)PhreeqcPtr->PHRQ_realloc(s_v.subscripts, - ((size_t)s_v.count_subscripts + 1) * sizeof(int)); - if (s_v.subscripts == NULL) - { - PhreeqcPtr->malloc_error(); - } - else - { - s_v.subscripts[s_v.count_subscripts] = i; - s_v.count_subscripts++; - } + size_t count_subscripts = s_v.subscripts.size(); + s_v.subscripts.resize(count_subscripts + 1); + s_v.subscripts[count_subscripts] = i; } /* get other subscripts */ @@ -2594,23 +2577,10 @@ factor(struct LOC_exec * LINK) { LINK->t = LINK->t->next; j = intexpr(LINK); - if (s_v.subscripts == NULL) - { - s_v.subscripts = (int*)PhreeqcPtr->PHRQ_malloc(sizeof(int)); - if (s_v.subscripts == NULL) - PhreeqcPtr->malloc_error(); - } - s_v.subscripts = (int*)PhreeqcPtr->PHRQ_realloc(s_v.subscripts, - ((size_t)s_v.count_subscripts + 1) * sizeof(int)); - if (s_v.subscripts == NULL) - { - PhreeqcPtr->malloc_error(); - } - else - { - s_v.subscripts[s_v.count_subscripts] = j; - s_v.count_subscripts++; - } + + size_t count_subscripts = s_v.subscripts.size(); + s_v.subscripts.resize(count_subscripts + 1); + s_v.subscripts[count_subscripts] = j; } else { @@ -2674,23 +2644,15 @@ factor(struct LOC_exec * LINK) { require(toklp, LINK); - s_v.count_subscripts = 0; + s_v.subscripts.clear(); /* get first subscript */ if (LINK->t != NULL && LINK->t->kind != tokrp) { i = intexpr(LINK); - if (s_v.subscripts == NULL) - { - s_v.subscripts = (int*)PhreeqcPtr->PHRQ_malloc(sizeof(int)); - if (s_v.subscripts == NULL) - PhreeqcPtr->malloc_error(); - } - s_v.subscripts = (int*)PhreeqcPtr->PHRQ_realloc(s_v.subscripts, - ((size_t)s_v.count_subscripts + 1) * sizeof(int)); - if (s_v.subscripts == NULL) - PhreeqcPtr->malloc_error(); - s_v.subscripts[s_v.count_subscripts] = i; - s_v.count_subscripts++; + + size_t count_subscripts = s_v.subscripts.size(); + s_v.subscripts.resize(count_subscripts + 1); + s_v.subscripts[count_subscripts] = i; } /* get other subscripts */ @@ -2700,18 +2662,9 @@ factor(struct LOC_exec * LINK) { LINK->t = LINK->t->next; j = intexpr(LINK); - if (s_v.subscripts == NULL) - { - s_v.subscripts = (int*)PhreeqcPtr->PHRQ_malloc(sizeof(int)); - if (s_v.subscripts == NULL) - PhreeqcPtr->malloc_error(); - } - s_v.subscripts = (int*)PhreeqcPtr->PHRQ_realloc(s_v.subscripts, - ((size_t)s_v.count_subscripts + 1) * sizeof(int)); - if (s_v.subscripts == NULL) - PhreeqcPtr->malloc_error(); - s_v.subscripts[s_v.count_subscripts] = j; - s_v.count_subscripts++; + size_t count_subscripts = s_v.subscripts.size(); + s_v.subscripts.resize(count_subscripts + 1); + s_v.subscripts[count_subscripts] = j; } else { @@ -4312,7 +4265,7 @@ factor(struct LOC_exec * LINK) snerr(": missing \" or ("); break; } - s_v.subscripts = (int *) PhreeqcPtr->free_check_null(s_v.subscripts); + s_v.subscripts.clear(); return n; } @@ -4878,9 +4831,6 @@ cmdput(struct LOC_exec *LINK) int j; struct save_values s_v; - s_v.count_subscripts = 0; - s_v.subscripts = (int *) PhreeqcPtr->PHRQ_malloc(sizeof(int)); - /* get parentheses */ require(toklp, LINK); @@ -4893,14 +4843,9 @@ cmdput(struct LOC_exec *LINK) { LINK->t = LINK->t->next; j = intexpr(LINK); - s_v.count_subscripts++; - s_v.subscripts = - (int *) PhreeqcPtr->PHRQ_realloc(s_v.subscripts, - (size_t) s_v.count_subscripts * - sizeof(int)); - if (s_v.subscripts == NULL) - PhreeqcPtr->malloc_error(); - s_v.subscripts[s_v.count_subscripts - 1] = j; + size_t count_subscripts = s_v.subscripts.size(); + s_v.subscripts.resize(count_subscripts + 1); + s_v.subscripts[count_subscripts] = j; } else { @@ -4913,7 +4858,7 @@ cmdput(struct LOC_exec *LINK) { PhreeqcPtr->save_values_store(&s_v); } - s_v.subscripts = (int *) PhreeqcPtr->free_check_null(s_v.subscripts); + s_v.subscripts.clear(); } void PBasic:: diff --git a/global_structures.h b/global_structures.h index 688d4fc1..1bd44d8a 100644 --- a/global_structures.h +++ b/global_structures.h @@ -260,8 +260,7 @@ struct Charge_Group struct save_values { LDBLE value; - int count_subscripts; - int *subscripts; + std::vector subscripts; }; struct save diff --git a/structures.cpp b/structures.cpp index ddbb3cb9..d4ed4a90 100644 --- a/structures.cpp +++ b/structures.cpp @@ -132,10 +132,9 @@ clean_up(void) } logk.clear(); /* save_values */ - for (j = 0; j < (int)save_values.size(); j++) + for (size_t j = 0; j < save_values.size(); j++) { - save_values[j].subscripts = - (int*)free_check_null(save_values[j].subscripts); + save_values[j].subscripts.clear(); } save_values.clear(); /* working pe*/ @@ -899,7 +898,6 @@ master_bsearch_secondary(const char* cptr) const char* cptr1; std::string elt; struct master *master_ptr_primary, *master_ptr=NULL, *master_ptr_secondary=NULL; - int j; /* * Find element name */ @@ -930,7 +928,7 @@ master_bsearch_secondary(const char* cptr) * Find secondary master with same species as primary */ master_ptr = NULL; - for (j = master_ptr_primary->number + 1; j < (int)master.size(); j++) + for (size_t j = master_ptr_primary->number + 1; j < master.size(); j++) { if (master[j]->s == master_ptr_primary->s) { @@ -1922,19 +1920,19 @@ save_values_compare(const void *ptr1, const void *ptr2) const struct save_values *save_values_ptr1, *save_values_ptr2; save_values_ptr1 = (const struct save_values *) ptr1; save_values_ptr2 = (const struct save_values *) ptr2; - if (save_values_ptr1->count_subscripts < - save_values_ptr2->count_subscripts) + if (save_values_ptr1->subscripts.size() < + save_values_ptr2->subscripts.size()) { return (-1); } - else if (save_values_ptr1->count_subscripts > - save_values_ptr2->count_subscripts) + else if (save_values_ptr1->subscripts.size() > + save_values_ptr2->subscripts.size()) { return (1); } else { - for (i = 0; i < save_values_ptr1->count_subscripts; i++) + for (i = 0; i < save_values_ptr1->subscripts.size(); i++) { if (save_values_ptr1->subscripts[i] < save_values_ptr2->subscripts[i]) @@ -1975,7 +1973,7 @@ save_values_store(struct save_values *s_v) /* * Look for subscripts */ - int n, i; + int n; struct save_values *s_v_ptr; s_v_ptr = save_values_bsearch(s_v, &n); @@ -1985,29 +1983,10 @@ save_values_store(struct save_values *s_v) } else { - size_t count_save_values = save_values.size(); - save_values.resize(count_save_values + 1); - save_values[count_save_values].value = s_v->value; - save_values[count_save_values].count_subscripts = - s_v->count_subscripts; - i = s_v->count_subscripts; - if (i == 0) - i = 1; - save_values[count_save_values].subscripts = - (int *) PHRQ_malloc((size_t) i * sizeof(int)); - if (save_values[count_save_values].subscripts == NULL) - malloc_error(); - save_values[count_save_values].subscripts = - (int *) memcpy(save_values[count_save_values].subscripts, - s_v->subscripts, (size_t) i * sizeof(int)); + save_values.push_back(*s_v); save_values_sort(); } - if (save_values.size() > 1) - { - qsort(&save_values[0], save_values.size(), - sizeof(struct save_values), save_values_compare); - } return (OK); } /* ---------------------------------------------------------------------- */ From 9fd3f2a26a6f8055bbef76cbfb17146eb7266307 Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Tue, 30 Mar 2021 23:48:37 -0600 Subject: [PATCH 30/53] save_values rewritten with map --- PBasic.cpp | 61 ++++++----------------- Phreeqc.h | 11 ++--- basicsubs.cpp | 11 ++--- global_structures.h | 62 ----------------------- pitzer.cpp | 2 +- prep.cpp | 8 +-- sit.cpp | 2 +- structures.cpp | 118 -------------------------------------------- 8 files changed, 32 insertions(+), 243 deletions(-) diff --git a/PBasic.cpp b/PBasic.cpp index 0ed075f6..4a1a5bbd 100644 --- a/PBasic.cpp +++ b/PBasic.cpp @@ -70,7 +70,6 @@ PBasic::PBasic(Phreeqc * ptr, PHRQ_io *phrq_io) } PBasic::~PBasic(void) { - } int PBasic:: @@ -2021,8 +2020,6 @@ factor(struct LOC_exec * LINK) long i; char *c; } trick; - struct save_values s_v, *s_v_ptr; - int k; LDBLE TEMP; std::string STR1, STR2; const char *elt_name, *surface_name, *mytemplate, *name; @@ -2518,11 +2515,6 @@ factor(struct LOC_exec * LINK) LINK->t = LINK->t->next; require(tokrp, LINK); - // Make work space - //int max_length = length < 256 ? 256 : length; - //char *token = (char *) PhreeqcPtr->PHRQ_calloc(size_t (max_length + 1), sizeof(char)); - //if (token == NULL) PhreeqcPtr->malloc_error(); - // set function value LDBLE eq; std::string elt_name; @@ -2558,16 +2550,14 @@ factor(struct LOC_exec * LINK) case tokexists: { + std::ostringstream oss; require(toklp, LINK); - s_v.subscripts.clear(); /* get first subscript */ if (LINK->t != NULL && LINK->t->kind != tokrp) { i = intexpr(LINK); - size_t count_subscripts = s_v.subscripts.size(); - s_v.subscripts.resize(count_subscripts + 1); - s_v.subscripts[count_subscripts] = i; + oss << i << ","; } /* get other subscripts */ @@ -2577,10 +2567,7 @@ factor(struct LOC_exec * LINK) { LINK->t = LINK->t->next; j = intexpr(LINK); - - size_t count_subscripts = s_v.subscripts.size(); - s_v.subscripts.resize(count_subscripts + 1); - s_v.subscripts[count_subscripts] = j; + oss << j << ","; } else { @@ -2595,15 +2582,8 @@ factor(struct LOC_exec * LINK) } else { - s_v_ptr = PhreeqcPtr->save_values_bsearch(&s_v, &k); - if (s_v_ptr == NULL) - { - n.UU.val = 0; - } - else - { - n.UU.val = 1; - } + std::map::iterator it = PhreeqcPtr->save_values.find(oss.str()); + n.UU.val = (it == PhreeqcPtr->save_values.end()) ? 0 : 1; } } break; @@ -2642,17 +2622,14 @@ factor(struct LOC_exec * LINK) case tokget: { + std::ostringstream oss; require(toklp, LINK); - s_v.subscripts.clear(); /* get first subscript */ if (LINK->t != NULL && LINK->t->kind != tokrp) { i = intexpr(LINK); - - size_t count_subscripts = s_v.subscripts.size(); - s_v.subscripts.resize(count_subscripts + 1); - s_v.subscripts[count_subscripts] = i; + oss << i << ","; } /* get other subscripts */ @@ -2662,9 +2639,7 @@ factor(struct LOC_exec * LINK) { LINK->t = LINK->t->next; j = intexpr(LINK); - size_t count_subscripts = s_v.subscripts.size(); - s_v.subscripts.resize(count_subscripts + 1); - s_v.subscripts[count_subscripts] = j; + oss << j << ","; } else { @@ -2673,14 +2648,14 @@ factor(struct LOC_exec * LINK) break; } } - s_v_ptr = (parse_all) ? NULL : PhreeqcPtr->save_values_bsearch(&s_v, &k); - if (s_v_ptr == NULL) + if (parse_all) { - n.UU.val = (parse_all) ? 1 : 0; + n.UU.val = 1; } else { - n.UU.val = s_v_ptr->value; + std::map::iterator it = PhreeqcPtr->save_values.find(oss.str()); + n.UU.val = (it == PhreeqcPtr->save_values.end()) ? 0 : it->second; } break; } @@ -4265,7 +4240,6 @@ factor(struct LOC_exec * LINK) snerr(": missing \" or ("); break; } - s_v.subscripts.clear(); return n; } @@ -4829,13 +4803,13 @@ void PBasic:: cmdput(struct LOC_exec *LINK) { int j; - struct save_values s_v; + std::ostringstream oss; /* get parentheses */ require(toklp, LINK); /* get first argumen */ - s_v.value = realexpr(LINK); + double value = realexpr(LINK); for (;;) { @@ -4843,9 +4817,7 @@ cmdput(struct LOC_exec *LINK) { LINK->t = LINK->t->next; j = intexpr(LINK); - size_t count_subscripts = s_v.subscripts.size(); - s_v.subscripts.resize(count_subscripts + 1); - s_v.subscripts[count_subscripts] = j; + oss << j << ","; } else { @@ -4856,9 +4828,8 @@ cmdput(struct LOC_exec *LINK) } if (!parse_all) { - PhreeqcPtr->save_values_store(&s_v); + PhreeqcPtr->save_values[oss.str()] = value; } - s_v.subscripts.clear(); } void PBasic:: diff --git a/Phreeqc.h b/Phreeqc.h index f1c4b96a..c85f0d9a 100644 --- a/Phreeqc.h +++ b/Phreeqc.h @@ -60,6 +60,8 @@ class cxxSSassemblage; class cxxSS; class cxxStorageBin; + +#include "classes.h" #include "global_structures.h" class PBasic; @@ -875,10 +877,6 @@ public: struct species* s_search(const char* name); struct species* s_store(const char* name, LDBLE z, int replace_if_found); protected: - struct save_values* save_values_bsearch(struct save_values* k, int* n); - static int save_values_compare(const void* ptr1, const void* ptr2); - int save_values_sort(void); - int save_values_store(struct save_values* s_v); static int isotope_compare(const void* ptr1, const void* ptr2); static int species_list_compare_alk(const void* ptr1, const void* ptr2); static int species_list_compare_master(const void* ptr1, const void* ptr2); @@ -1140,7 +1138,7 @@ protected: * STRUCTURES * ---------------------------------------------------------------------- */ - struct model last_model; + Model last_model; //struct punch punch; bool high_precision; @@ -1180,7 +1178,7 @@ protected: /*---------------------------------------------------------------------- * Save *---------------------------------------------------------------------- */ - std::vector save_values; + std::map save_values; struct save save; /*---------------------------------------------------------------------- @@ -1657,6 +1655,7 @@ protected: /* Basic */ PBasic* basic_interpreter; + double (*basic_callback_ptr) (double x1, double x2, const char* str, void* cookie); void* basic_callback_cookie; #ifdef IPHREEQC_NO_FORTRAN_MODULE diff --git a/basicsubs.cpp b/basicsubs.cpp index a09c81a7..7871a365 100644 --- a/basicsubs.cpp +++ b/basicsubs.cpp @@ -2507,7 +2507,6 @@ total_mole(const char *total_name) { struct master *master_ptr; LDBLE t; - int i; if (strcmp(total_name, "H") == 0) { @@ -2553,8 +2552,8 @@ total_mole(const char *total_name) else { t = 0; - for (i = master_ptr->number + 1; - (i < (int)master.size() && master[i]->elt->primary == master_ptr); + for (size_t i = master_ptr->number + 1; + (i < master.size() && master[i]->elt->primary == master_ptr); i++) { t += master[i]->total; @@ -2737,7 +2736,7 @@ system_total(const char *total_name, LDBLE * count, char ***names, else if (sys.size() > 1) { qsort(&sys[0], sys.size(), - (size_t)sizeof(struct system_species), system_species_compare_name); + sizeof(struct system_species), system_species_compare_name); } /* * malloc space @@ -2895,7 +2894,7 @@ int Phreeqc:: system_total_elements(void) /* ---------------------------------------------------------------------- */ { - int i, j; + int i; LDBLE t; char name[MAX_LENGTH]; struct master *master_ptr; @@ -2970,7 +2969,7 @@ system_total_elements(void) else { t = 0; - for (j = master_ptr->number + 1; + for (size_t j = master_ptr->number + 1; master[j]->elt->primary == master_ptr; j++) { t += master[j]->total; diff --git a/global_structures.h b/global_structures.h index 1bd44d8a..61695dae 100644 --- a/global_structures.h +++ b/global_structures.h @@ -191,53 +191,6 @@ typedef struct PHRQMemHeader #endif } PHRQMemHeader; -struct model -{ - bool force_prep; - //LDBLE temperaturex; // not used - //LDBLE pressure; // not used - - //int count_exchange; - //struct master **exchange; // not used - - //int count_kinetics; - //struct kinetics *kinetics; // not used - - bool numerical_fixed_volume; - cxxGasPhase::GP_TYPE gas_phase_type; - std::vector gas_phase; - - std::vector ss_assemblage; - - std::vector pp_assemblage; - std::vector si; - std::vector add_formula; - - cxxSurface::DIFFUSE_LAYER_TYPE dl_type; - cxxSurface::SURFACE_TYPE surface_type; - //bool only_counter_ions; // not used - - //LDBLE thickness; // not used - std::vector surface_comp; - std::vector surface_charge; -}; - -struct name_master -{ - const char *name; - struct master *master; -}; -struct name_species -{ - const char *name; - struct species *s; -}; -struct name_phase -{ - const char *name; - struct phase *phase; -}; - struct Change_Surf { const char *comp_name; @@ -248,21 +201,6 @@ struct Change_Surf int next; }; -struct Charge_Group -{ - LDBLE z; - LDBLE eq; -}; - -/*---------------------------------------------------------------------- - * Save - *---------------------------------------------------------------------- */ -struct save_values -{ - LDBLE value; - std::vector subscripts; -}; - struct save { int solution; diff --git a/pitzer.cpp b/pitzer.cpp index 182e2755..d083f668 100644 --- a/pitzer.cpp +++ b/pitzer.cpp @@ -1946,7 +1946,7 @@ jacobian_pz(void) calculating_deriv = 1; Restart: - int pz_max_unknowns = max_unknowns; + size_t pz_max_unknowns = max_unknowns; //k_temp(tc_x, patm_x); if (full_pitzer == TRUE) { diff --git a/prep.cpp b/prep.cpp index da8a14d2..6514cde9 100644 --- a/prep.cpp +++ b/prep.cpp @@ -6163,7 +6163,7 @@ build_min_surface(void) } } LDBLE coef = elt_list[jj].coef; - int row; + size_t row; struct unknown *unknown_ptr; if (master_ptr->s == s_hplus) { @@ -6180,7 +6180,7 @@ build_min_surface(void) row = master_ptr->unknown->number; unknown_ptr = master_ptr->unknown; } - store_jacob0(row, x[k]->number, + store_jacob0((int)row, (int)x[k]->number, coef * comp_ptr->Get_phase_proportion()); store_sum_deltas(&delta[k], &unknown_ptr->delta, -coef * comp_ptr->Get_phase_proportion()); @@ -6210,7 +6210,7 @@ setup_related_surface(void) if (comp_ptr->Get_phase_name().size() > 0) { int k; - for (k = count_unknowns - 1; k >= 0; k--) + for (k = (int)count_unknowns - 1; k >= 0; k--) { if (x[k]->type != PP) continue; @@ -6234,7 +6234,7 @@ setup_related_surface(void) { cxxSurfaceComp *comp_i_ptr = use.Get_surface_ptr()->Find_comp(x[i]->surface_comp); int k; - for (k = count_unknowns - 1; k >= 0; k--) + for (k = (int)count_unknowns - 1; k >= 0; k--) { if (x[k]->type != PP) continue; diff --git a/sit.cpp b/sit.cpp index 6328937e..2838eac6 100644 --- a/sit.cpp +++ b/sit.cpp @@ -800,7 +800,7 @@ jacobian_sit(void) LDBLE d, d1, d2; int i, j; Restart: - int pz_max_unknowns = max_unknowns; + size_t pz_max_unknowns = max_unknowns; //k_temp(tc_x, patm_x); if (full_pitzer == TRUE) { diff --git a/structures.cpp b/structures.cpp index d4ed4a90..5b937e19 100644 --- a/structures.cpp +++ b/structures.cpp @@ -131,11 +131,6 @@ clean_up(void) delete logk[j]; } logk.clear(); - /* save_values */ - for (size_t j = 0; j < save_values.size(); j++) - { - save_values[j].subscripts.clear(); - } save_values.clear(); /* working pe*/ pe_x.clear(); @@ -1876,119 +1871,6 @@ s_store(const char *name, LDBLE l_z, int replace_if_found) species_map[name] = s_ptr; return (s_ptr); } -/* ********************************************************************** - * - * Routines related to structure "save_values" - * - * ********************************************************************** */ -/* ---------------------------------------------------------------------- */ -struct save_values * Phreeqc:: -save_values_bsearch(struct save_values *k, int *n) -/* ---------------------------------------------------------------------- */ -{ -/* - * Binary search save_values to find if one exists with given coefficients - * Save_Values is assumed to be in sort order by count_subscripts and - * values of subscripts - */ - void *void_ptr; - if (save_values.size() == 0) - { - *n = -999; - return (NULL); - } - void_ptr = (void *) - bsearch((char *) k, - (char *) &save_values[0], - save_values.size(), - (size_t) sizeof(struct save_values), save_values_compare); - if (void_ptr == NULL) - { - *n = -999; - return (NULL); - } - *n = (int) ((struct save_values *) void_ptr - &save_values[0]); - return ((struct save_values *) void_ptr); -} - -/* ---------------------------------------------------------------------- */ -int Phreeqc:: -save_values_compare(const void *ptr1, const void *ptr2) -/* ---------------------------------------------------------------------- */ -{ - int i; - const struct save_values *save_values_ptr1, *save_values_ptr2; - save_values_ptr1 = (const struct save_values *) ptr1; - save_values_ptr2 = (const struct save_values *) ptr2; - if (save_values_ptr1->subscripts.size() < - save_values_ptr2->subscripts.size()) - { - return (-1); - } - else if (save_values_ptr1->subscripts.size() > - save_values_ptr2->subscripts.size()) - { - return (1); - } - else - { - for (i = 0; i < save_values_ptr1->subscripts.size(); i++) - { - if (save_values_ptr1->subscripts[i] < - save_values_ptr2->subscripts[i]) - { - return (-1); - } - else if (save_values_ptr1->subscripts[i] > - save_values_ptr2->subscripts[i]) - { - return (1); - } - } - } - return (0); -} - -/* ---------------------------------------------------------------------- */ -int Phreeqc:: -save_values_sort(void) -/* ---------------------------------------------------------------------- */ -{ -/* - * Sort array of save_values structures - */ - if (save_values.size() > 1) - { - qsort(&save_values[0], save_values.size(), - sizeof(struct save_values), save_values_compare); - } - return (OK); -} - -/* ---------------------------------------------------------------------- */ -int Phreeqc:: -save_values_store(struct save_values *s_v) -/* ---------------------------------------------------------------------- */ -{ -/* - * Look for subscripts - */ - int n; - struct save_values *s_v_ptr; - - s_v_ptr = save_values_bsearch(s_v, &n); - if (s_v_ptr != NULL) - { - s_v_ptr->value = s_v->value; - } - else - { - save_values.push_back(*s_v); - save_values_sort(); - } - - return (OK); -} /* ---------------------------------------------------------------------- */ int Phreeqc:: isotope_compare(const void *ptr1, const void *ptr2) From dc2dc5312f0ac68bfa421aa8d47b69adcd520664 Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Thu, 1 Apr 2021 09:19:14 -0600 Subject: [PATCH 31/53] vectorized token --- basicsubs.cpp | 8 ++++---- gases.cpp | 6 +++--- global_structures.h | 4 ++-- inverse.cpp | 4 ++-- mainsubs.cpp | 2 +- model.cpp | 10 +++++----- prep.cpp | 26 +++++++++++++------------- print.cpp | 10 +++++----- read.cpp | 6 +++--- structures.cpp | 25 ++++++++++--------------- 10 files changed, 48 insertions(+), 53 deletions(-) diff --git a/basicsubs.cpp b/basicsubs.cpp index 7871a365..e305a75a 100644 --- a/basicsubs.cpp +++ b/basicsubs.cpp @@ -1735,7 +1735,7 @@ saturation_ratio(const char *phase_name) } else if (phase_ptr->in != FALSE) { - for (rxn_ptr = phase_ptr->rxn_x->token + 1; rxn_ptr->s != NULL; + for (rxn_ptr = &phase_ptr->rxn_x->token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { iap += rxn_ptr->s->la * rxn_ptr->coef; @@ -1767,7 +1767,7 @@ saturation_index(const char *phase_name, LDBLE * iap, LDBLE * si) } else if (phase_ptr->in != FALSE) { - for (rxn_ptr = phase_ptr->rxn_x->token + 1; rxn_ptr->s != NULL; + for (rxn_ptr = &phase_ptr->rxn_x->token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { *iap += rxn_ptr->s->la * rxn_ptr->coef; @@ -2293,7 +2293,7 @@ surf_total(const char *total_name, const char *surface_name) struct rxn_token *rxn_ptr; if (s_x[j]->mole_balance == NULL) { - for (rxn_ptr = s_x[j]->rxn_s->token + 1; rxn_ptr->s != NULL; rxn_ptr++) + for (rxn_ptr = &s_x[j]->rxn_s->token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { if (redox && rxn_ptr->s->secondary) { @@ -3024,7 +3024,7 @@ system_total_si(void) * Print saturation index */ iap = 0.0; - for (rxn_ptr = phases[i]->rxn_x->token + 1; rxn_ptr->s != NULL; + for (rxn_ptr = &phases[i]->rxn_x->token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { iap += rxn_ptr->s->la * rxn_ptr->coef; diff --git a/gases.cpp b/gases.cpp index e6c76d3e..a3581391 100644 --- a/gases.cpp +++ b/gases.cpp @@ -174,7 +174,7 @@ build_fixed_volume_gas(void) } row = unknown_ptr->number * (count_unknowns + 1); coef_elt = elt_list[j].coef; - for (rxn_ptr = phase_ptr->rxn_x->token + 1; + for (rxn_ptr = &phase_ptr->rxn_x->token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { @@ -246,7 +246,7 @@ build_fixed_volume_gas(void) } unknown_ptr = gas_unknown; row = unknown_ptr->number * (count_unknowns + 1); - for (rxn_ptr = phase_ptr->rxn_x->token + 1; rxn_ptr->s != NULL; rxn_ptr++) + for (rxn_ptr = &phase_ptr->rxn_x->token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { if (rxn_ptr->s != s_eminus && rxn_ptr->s->in == FALSE) { @@ -649,7 +649,7 @@ calc_fixed_volume_gas_pressures(void) { lp = -phase_ptr->lk; //lp = -k_calc(phase_ptr->rxn_x->logk, tk_x, use.Get_gas_phase_ptr()->total_p * PASCAL_PER_ATM); - for (rxn_ptr = phase_ptr->rxn_x->token + 1; rxn_ptr->s != NULL; + for (rxn_ptr = &phase_ptr->rxn_x->token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { lp += rxn_ptr->s->la * rxn_ptr->coef; diff --git a/global_structures.h b/global_structures.h index 61695dae..0d25736c 100644 --- a/global_structures.h +++ b/global_structures.h @@ -415,7 +415,7 @@ struct reaction { LDBLE logk[MAX_LOG_K_INDICES]; LDBLE dz[3]; - struct rxn_token *token; + std::vector token; }; struct rxn_token { @@ -450,7 +450,7 @@ public: dz[i] = rxn->dz[i]; } struct rxn_token *next_token; - next_token = rxn->token; + next_token = &rxn->token[0]; this->tokens.push_back(*next_token++); while (next_token->s != NULL || next_token->name != NULL) { diff --git a/inverse.cpp b/inverse.cpp index cfc38096..7fc350a3 100644 --- a/inverse.cpp +++ b/inverse.cpp @@ -1966,7 +1966,7 @@ print_model(struct inverse *inv_ptr) lk = k_calc(reaction_ptr->logk, t_i, p_i); iap = 0.0; - for (rxn_ptr = reaction_ptr->token + 1; rxn_ptr->s != NULL; rxn_ptr++) + for (rxn_ptr = &reaction_ptr->token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { t = 0; if (rxn_ptr->s == s_eminus) @@ -4905,7 +4905,7 @@ dump_netpath_pat(struct inverse *inv_ptr) */ std::string token; sum = 0; - for (rxn_ptr = inv_ptr->phases[i].phase->rxn_s->token + 1; + for (rxn_ptr = &inv_ptr->phases[i].phase->rxn_s->token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { if (rxn_ptr->s == s_hplus) diff --git a/mainsubs.cpp b/mainsubs.cpp index 7b4a5447..3074cec1 100644 --- a/mainsubs.cpp +++ b/mainsubs.cpp @@ -640,7 +640,7 @@ initial_gas_phases(int print) if (phase_ptr->in == TRUE) { lp = -phase_ptr->lk; - for (rxn_ptr = phase_ptr->rxn_x->token + 1; + for (rxn_ptr = &phase_ptr->rxn_x->token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { lp += rxn_ptr->s->la * rxn_ptr->coef; diff --git a/model.cpp b/model.cpp index 567cdf35..3135694e 100644 --- a/model.cpp +++ b/model.cpp @@ -2214,7 +2214,7 @@ mb_ss(void) if (phase0_ptr->in == TRUE && phase0_ptr->rxn_x != NULL) { log10_iap = 0; - for (rxn_ptr = phase0_ptr->rxn_x->token + 1; + for (rxn_ptr = &phase0_ptr->rxn_x->token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { log10_iap += rxn_ptr->s->la * rxn_ptr->coef; @@ -2228,7 +2228,7 @@ mb_ss(void) if (phase1_ptr->in == TRUE && phase1_ptr->rxn_x != NULL) { log10_iap = 0; - for (rxn_ptr = phase1_ptr->rxn_x->token + 1; + for (rxn_ptr = &phase1_ptr->rxn_x->token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { log10_iap += rxn_ptr->s->la * rxn_ptr->coef; @@ -2293,7 +2293,7 @@ mb_ss(void) if (phase_ptr->in == TRUE) { lp = -phase_ptr->lk; - for (rxn_ptr = phase_ptr->rxn_x->token + 1; + for (rxn_ptr = &phase_ptr->rxn_x->token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { lp += rxn_ptr->s->la * rxn_ptr->coef; @@ -2367,7 +2367,7 @@ molalities(int allow_overflow) * lm and moles for all aqueous species */ s_x[i]->lm = s_x[i]->lk - s_x[i]->lg; - for (rxn_ptr = s_x[i]->rxn_x->token + 1; rxn_ptr->s != NULL; + for (rxn_ptr = &s_x[i]->rxn_x->token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { s_x[i]->lm += rxn_ptr->s->la * rxn_ptr->coef; @@ -2639,7 +2639,7 @@ calc_gas_pressures(void) if (phase_ptr->in == TRUE) { lp = -phase_ptr->lk; - for (rxn_ptr = phase_ptr->rxn_x->token + 1; rxn_ptr->s != NULL; + for (rxn_ptr = &phase_ptr->rxn_x->token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { lp += rxn_ptr->s->la * rxn_ptr->coef; diff --git a/prep.cpp b/prep.cpp index 6514cde9..b8bc461b 100644 --- a/prep.cpp +++ b/prep.cpp @@ -477,7 +477,7 @@ build_gas_phase(void) } row = unknown_ptr->number * (count_unknowns + 1); coef_elt = elt_list[j].coef; - for (rxn_ptr = phase_ptr->rxn_x->token + 1; + for (rxn_ptr = &phase_ptr->rxn_x->token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { @@ -549,7 +549,7 @@ build_gas_phase(void) } unknown_ptr = gas_unknown; row = unknown_ptr->number * (count_unknowns + 1); - for (rxn_ptr = phase_ptr->rxn_x->token + 1; rxn_ptr->s != NULL; rxn_ptr++) + for (rxn_ptr = &phase_ptr->rxn_x->token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { if (rxn_ptr->s != s_eminus && rxn_ptr->s->in == FALSE) { @@ -657,7 +657,7 @@ build_ss_assemblage(void) if (x[i]->phase->rxn_x == NULL) continue; store_mb(&(x[i]->phase->lk), &(x[i]->f), 1.0); - for (rxn_ptr = x[i]->phase->rxn_x->token + 1; rxn_ptr->s != NULL; + for (rxn_ptr = &x[i]->phase->rxn_x->token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { store_mb(&(rxn_ptr->s->la), &(x[i]->f), -rxn_ptr->coef); @@ -671,7 +671,7 @@ build_ss_assemblage(void) * Put coefficients into mass action equations */ /* first IAP terms */ - for (rxn_ptr = x[i]->phase->rxn_x->token + 1; rxn_ptr->s != NULL; + for (rxn_ptr = &x[i]->phase->rxn_x->token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { if (rxn_ptr->s->secondary != NULL @@ -1371,7 +1371,7 @@ build_pure_phases(void) store_mb(&(x[i]->phase->lk), &(x[i]->f), 1.0); store_mb(&(x[i]->si), &(x[i]->f), 1.0); - for (rxn_ptr = x[i]->phase->rxn_x->token + 1; rxn_ptr->s != NULL; + for (rxn_ptr = &x[i]->phase->rxn_x->token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { store_mb(&(rxn_ptr->s->la), &(x[i]->f), -rxn_ptr->coef); @@ -1387,7 +1387,7 @@ build_pure_phases(void) /* * Put coefficients into IAP equations */ - for (rxn_ptr = x[i]->phase->rxn_x->token + 1; rxn_ptr->s != NULL; + for (rxn_ptr = &x[i]->phase->rxn_x->token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { if (rxn_ptr->s->secondary != NULL @@ -1554,7 +1554,7 @@ build_solution_phase_boundaries(void) input_error++; break; } - for (rxn_ptr = x[i]->phase->rxn_x->token + 1; rxn_ptr->s != NULL; + for (rxn_ptr = &x[i]->phase->rxn_x->token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { store_mb(&(rxn_ptr->s->la), &(x[i]->f), -rxn_ptr->coef); @@ -1569,7 +1569,7 @@ build_solution_phase_boundaries(void) { if (x[i]->type != SOLUTION_PHASE_BOUNDARY) continue; - for (rxn_ptr = x[i]->phase->rxn_x->token + 1; rxn_ptr->s != NULL; + for (rxn_ptr = &x[i]->phase->rxn_x->token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { if (rxn_ptr->s->secondary != NULL @@ -2083,7 +2083,7 @@ is_special(struct species *l_spec) struct rxn_token *token_ptr; special = TRUE; - for (token_ptr = l_spec->rxn_s->token + 1; token_ptr->s != NULL; + for (token_ptr = &l_spec->rxn_s->token[0] + 1; token_ptr->s != NULL; token_ptr++) { if (token_ptr->s != s_hplus && @@ -4715,7 +4715,7 @@ store_dn(int k, LDBLE * source, int row, LDBLE coef_in, LDBLE * gamma_source) } if (s[k] == s_h2o) return (OK); - for (rxn_ptr = s[k]->rxn_x->token + 1; rxn_ptr->s != NULL; rxn_ptr++) + for (rxn_ptr = &s[k]->rxn_x->token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { if (rxn_ptr->s->secondary != NULL && rxn_ptr->s->secondary->in == TRUE) @@ -6078,7 +6078,7 @@ build_min_surface(void) struct element *elt_ptr = element_store(comp_ptr->Get_master_element().c_str()); /* find unknown number */ int j; - for (j = count_unknowns - 1; j >= 0; j--) + for (j = (int)count_unknowns - 1; j >= 0; j--) { if (x[j]->type != SURFACE) continue; @@ -6086,7 +6086,7 @@ build_min_surface(void) break; } int k; - for (k = count_unknowns - 1; k >= 0; k--) + for (k = (int)count_unknowns - 1; k >= 0; k--) { if (x[k]->type != PP) continue; @@ -6112,7 +6112,7 @@ build_min_surface(void) } /* charge balance */ - store_jacob0(charge_balance_unknown->number, x[k]->number, + store_jacob0((int)charge_balance_unknown->number, (int)x[k]->number, comp_ptr->Get_formula_z() * comp_ptr->Get_phase_proportion()); store_sum_deltas(&delta[k], &charge_balance_unknown->delta, -comp_ptr->Get_formula_z() * comp_ptr->Get_phase_proportion()); diff --git a/print.cpp b/print.cpp index 6336766b..1782292b 100644 --- a/print.cpp +++ b/print.cpp @@ -648,7 +648,7 @@ print_gas_phase(void) { lp = -phase_ptr->lk; for (rxn_ptr = - phase_ptr->rxn_x->token + 1; + &phase_ptr->rxn_x->token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { lp += rxn_ptr->s->la * rxn_ptr->coef; @@ -1188,7 +1188,7 @@ print_reaction(struct reaction *rxn_ptr) output_msg(sformatf("\t%f", (double) rxn_ptr->logk[j])); } output_msg(sformatf("\n\nReaction:\n")); - for (next_token = rxn_ptr->token; next_token->s != NULL; next_token++) + for (next_token = &rxn_ptr->token[0]; next_token->s != NULL; next_token++) { output_msg(sformatf("\t\t%s\t%f\n", next_token->s->name, (double) next_token->coef)); @@ -1266,7 +1266,7 @@ print_saturation_indices(void) mu_terms_in_logk = true; lk = k_calc(reaction_ptr->logk, tk_x, patm_x * PASCAL_PER_ATM); iap = 0.0; - for (rxn_ptr = reaction_ptr->token + 1; rxn_ptr->s != NULL; + for (rxn_ptr = &reaction_ptr->token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { if (rxn_ptr->s != s_eminus) @@ -1369,7 +1369,7 @@ print_pp_assemblage(void) if (phase_ptr->rxn->logk[delta_v]) mu_terms_in_logk = true; lk = k_calc(phase_ptr->rxn->logk, tk_x, patm_x * PASCAL_PER_ATM); - for (rxn_ptr = phase_ptr->rxn->token + 1; rxn_ptr->s != NULL; + for (rxn_ptr = &phase_ptr->rxn->token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { if (rxn_ptr->s != s_eminus) @@ -3239,7 +3239,7 @@ punch_saturation_indices(void) * Print saturation index */ iap = 0.0; - for (rxn_ptr = ((struct phase *) current_selected_output->Get_si()[i].second)->rxn_x->token + 1; + for (rxn_ptr = &(((struct phase *) current_selected_output->Get_si()[i].second)->rxn_x->token[0]) + 1; rxn_ptr->s != NULL; rxn_ptr++) { iap += rxn_ptr->s->la * rxn_ptr->coef; diff --git a/read.cpp b/read.cpp index 563e1847..a2430fd5 100644 --- a/read.cpp +++ b/read.cpp @@ -757,7 +757,7 @@ read_exchange_species(void) /* * Copy reaction to reaction for species */ - token_ptr = trxn.token[0].s->rxn->token; + token_ptr = &trxn.token[0].s->rxn->token[0]; for (i = 0; i < count_trxn; i++) { token_ptr[i].s = trxn.token[i].s; @@ -3716,7 +3716,7 @@ read_phases(void) * Copy reaction to reaction for phase, first token (token[0]) is not used * except to check that coef of phase formula = 1.0 */ - token_ptr = phase_ptr->rxn->token; + token_ptr = &phase_ptr->rxn->token[0]; /* token_ptr[0].coef=0; */ token_ptr[0].coef = trxn.token[0].coef; token_ptr[0].s = trxn.token[1].s; @@ -6254,7 +6254,7 @@ read_surface_species(void) /* * Copy reaction to reaction for species */ - token_ptr = trxn.token[0].s->rxn->token; + token_ptr = &trxn.token[0].s->rxn->token[0]; for (i = 0; i < count_trxn; i++) { token_ptr[i].s = trxn.token[i].s; diff --git a/structures.cpp b/structures.cpp index 5b937e19..2fdb7c4a 100644 --- a/structures.cpp +++ b/structures.cpp @@ -1421,9 +1421,7 @@ rxn_alloc(int ntokens) /* * Malloc reaction structure */ - rxn_ptr = (struct reaction *) PHRQ_malloc(sizeof(struct reaction)); - if (rxn_ptr == NULL) - malloc_error(); + rxn_ptr = new struct reaction; /* * zero log k data */ @@ -1441,17 +1439,14 @@ rxn_alloc(int ntokens) /* * Malloc rxn_token structure */ - rxn_ptr->token = (struct rxn_token *) PHRQ_malloc( - (size_t) ntokens * sizeof(struct rxn_token)); + rxn_ptr->token.clear(); + rxn_ptr->token.resize((size_t)ntokens); for (i = 0; i < ntokens; i++) { rxn_ptr->token[i].s = NULL; rxn_ptr->token[i].name = NULL; rxn_ptr->token[i].coef = 0.0; } - - if (rxn_ptr->token == NULL) - malloc_error(); return (rxn_ptr); } @@ -1485,7 +1480,7 @@ rxn_dup(struct reaction *rxn_ptr_old) /* * Copy tokens */ - memcpy(rxn_ptr_new->token, rxn_ptr_old->token, + memcpy(&rxn_ptr_new->token[0], &rxn_ptr_old->token[0], ((size_t)i + 1) * sizeof(struct rxn_token)); return (rxn_ptr_new); @@ -1555,7 +1550,7 @@ rxn_find_coef(struct reaction * r_ptr, const char *str) struct rxn_token *r_token; LDBLE coef; - r_token = r_ptr->token + 1; + r_token = &r_ptr->token[0] + 1; coef = 0.0; while (r_token->s != NULL) { @@ -1582,8 +1577,8 @@ rxn_free(struct reaction *rxn_ptr) */ if (rxn_ptr == NULL) return (ERROR); - rxn_ptr->token = (struct rxn_token *) free_check_null(rxn_ptr->token); - rxn_ptr = (struct reaction *) free_check_null(rxn_ptr); + rxn_ptr->token.clear(); + delete rxn_ptr; return (OK); } @@ -1602,7 +1597,7 @@ rxn_print(struct reaction *rxn_ptr) int i; if (rxn_ptr == NULL) return (ERROR); - next_token = rxn_ptr->token; + next_token = &rxn_ptr->token[0]; output_msg(sformatf( "log k data:\n")); for (i = 0; i < MAX_LOG_K_INDICES; i++) { @@ -2249,7 +2244,7 @@ trxn_add(struct reaction *r_ptr, LDBLE coef, int combine) /* * Copy equation into work space */ - next_token = r_ptr->token; + next_token = &r_ptr->token[0]; while (next_token->s != NULL) { if (count_trxn + 1 > trxn.token.size()) @@ -2304,7 +2299,7 @@ trxn_add_phase(struct reaction *r_ptr, LDBLE coef, int combine) /* * Copy equation into work space */ - next_token = r_ptr->token; + next_token = &r_ptr->token[0]; while (next_token->s != NULL || next_token->name != NULL) { if (count_trxn + 1 > trxn.token.size()) From 028e90899ce276becf7377380ce37097d83dd2af Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Thu, 1 Apr 2021 16:58:43 -0600 Subject: [PATCH 32/53] moving to CReaction --- CReaction.cpp | 22 ++++ CReaction.h | 30 +++++ Classes.cpp | 279 ++++++++++++++++++++++++++++++++++++++++++++ Classes.h | 34 ++++++ Phreeqc.cpp | 115 +++++++++--------- Phreeqc.h | 20 +++- basicsubs.cpp | 28 ++--- gases.cpp | 10 +- global_structures.h | 44 +++++-- inverse.cpp | 18 +-- mainsubs.cpp | 2 +- model.cpp | 48 ++++---- pitzer.cpp | 20 ++-- prep.cpp | 111 +++++++++--------- print.cpp | 90 +++++++------- read.cpp | 86 +++++++------- sit.cpp | 22 ++-- structures.cpp | 78 +++++++------ tidy.cpp | 64 +++++----- transport.cpp | 6 +- 20 files changed, 765 insertions(+), 362 deletions(-) create mode 100644 CReaction.cpp create mode 100644 CReaction.h create mode 100644 Classes.cpp create mode 100644 Classes.h diff --git a/CReaction.cpp b/CReaction.cpp new file mode 100644 index 00000000..2c8f325b --- /dev/null +++ b/CReaction.cpp @@ -0,0 +1,22 @@ +#include "global_structures.h" +#include "Phreeqc.h" +#include "CReaction.h" +CReaction::CReaction(void) +{ + for (size_t i = 0; i < MAX_LOG_K_INDICES; i++) this->logk[i] = 0.0; + for (size_t i = 0; i < 3; i++) this->dz[i] = 0.0; +} +CReaction::CReaction(size_t ntoken) +{ + for (size_t i = 0; i < MAX_LOG_K_INDICES; i++) this->logk[i] = 0.0; + for (size_t i = 0; i < 3; i++) this->dz[i] = 0.0; + this->token.resize(ntoken); +} +void CReaction::Set_logk(double* d) +{ + for (size_t i = 0; i < MAX_LOG_K_INDICES; i++)logk[i] = d[i]; +} +void CReaction::Set_dz(double* d) +{ + for (size_t i = 0; i < 3; i++) dz[i] = d[i]; +} diff --git a/CReaction.h b/CReaction.h new file mode 100644 index 00000000..5f4335f5 --- /dev/null +++ b/CReaction.h @@ -0,0 +1,30 @@ +#if !defined(CREACTION_H_INCLUDED) +#define CREACTION_H_INCLUDED +#include +#include "global_structures.h" + +/*---------------------------------------------------------------------- + * Reaction + *---------------------------------------------------------------------- */ + +class CReaction +{ +public: + CReaction(void); + CReaction(size_t ntoken); + ~CReaction(void) {} + double* Get_logk(void) { return this->logk; } + void Set_logk(double* d); + double* Get_dz(void) { return this->dz; } + void Set_dz(double* d); + size_t size() { return token.size(); } + std::vector& Get_tokens(void) { return this->token; } + void Set_tokens(const std::vector& t) { this->token = t; } + +public: + double logk[21]; //LOG_K_INDICES::MAX_LOG_K_INDICES + //LDBLE logk[LOG_K_INDICES::MAX_LOG_K_INDICES]; + double dz[3]; + std::vector token; +}; +#endif // !defined(CREACTION_H_INCLUDED) \ No newline at end of file diff --git a/Classes.cpp b/Classes.cpp new file mode 100644 index 00000000..bdbadd05 --- /dev/null +++ b/Classes.cpp @@ -0,0 +1,279 @@ +#include "Phreeqc.h" +#include "CReaction.h" +/* ---------------------------------------------------------------------- */ +double Phreeqc:: +calc_delta_v(CReaction& r_ref, bool phase) +/* ---------------------------------------------------------------------- */ +{ + /* calculate delta_v from molar volumes */ + double d_v = 0.0; + if (phase) + { + /* for phases: reactants have coef's < 0, products have coef's > 0, v.v. for species */ + for (size_t i = 1; r_ref.Get_tokens()[i].s; i++) + { + if (!r_ref.Get_tokens()[i].s) + continue; + d_v += r_ref.Get_tokens()[i].coef * r_ref.Get_tokens()[i].s->logk[vm_tc]; + } + } + else + { + for (size_t i = 0; r_ref.token[i].name /*|| r_ptr->token[i].s*/; i++) + { + if (!r_ref.Get_tokens()[i].s) + continue; + d_v -= r_ref.Get_tokens()[i].coef * r_ref.Get_tokens()[i].s->logk[vm_tc]; + } + } + return d_v; +} +/* ---------------------------------------------------------------------- */ +bool Phreeqc:: +trxn_add(CReaction& r_ref, double coef, bool combine) +/* ---------------------------------------------------------------------- */ +{ + /* + * Adds reactions together. + * + * Global variable count_trxn determines which position in trxn is used. + * If count_trxn=0, then the equation effectively is copied into trxn. + * If count_trxn>0, then new equation is added to existing equation. + * + * Arguments: + * *r_ptr points to rxn structure to add. + * + * coef added equation is multiplied by coef. + * combine if TRUE, reaction is reaction is sorted and + * like terms combined. + */ + /* + * Accumulate log k for reaction + */ + if (count_trxn == 0) + { + for (int i = 0; i < MAX_LOG_K_INDICES; i++) trxn.logk[i] = r_ref.Get_logk()[i]; + for (int i = 0; i < 3; i++) trxn.dz[i] = r_ref.Get_dz()[i]; + } + else + { + for (int i = 0; i < MAX_LOG_K_INDICES; i++) trxn.logk[i] += coef * r_ref.Get_logk()[i]; + for (int i = 0; i < 3; i++) trxn.dz[i] += coef * r_ref.Get_dz()[i]; + } + /* + * Copy equation into work space + */ + struct rxn_token* next_token = &r_ref.token[0]; + while (next_token->s != NULL) + { + if (count_trxn + 1 > trxn.token.size()) + trxn.token.resize(count_trxn + 1); + trxn.token[count_trxn].name = next_token->s->name; + trxn.token[count_trxn].s = next_token->s; + trxn.token[count_trxn].coef = coef * next_token->coef; + count_trxn++; + next_token++; + } + if (combine == TRUE) + trxn_combine(); + return (OK); +} + + +/* ---------------------------------------------------------------------- */ +bool Phreeqc:: +trxn_add_phase(CReaction& r_ref, double coef, bool combine) +/* ---------------------------------------------------------------------- */ +{ + /* + * Adds reactions together. + * + * Global variable count_trxn determines which position in trxn is used. + * If count_trxn=0, then the equation effectively is copied into trxn. + * If count_trxn>0, then new equation is added to existing equation. + * + * Arguments: + * *r_ptr points to rxn structure to add. + * + * coef added equation is multiplied by coef. + * combine if TRUE, reaction is reaction is sorted and + * like terms combined. + */ + int i; + struct rxn_token* next_token; + /* + * Accumulate log k for reaction + */ + if (count_trxn == 0) + { + memcpy((void*)trxn.logk, (void*)r_ref.Get_logk(), + (size_t)MAX_LOG_K_INDICES * sizeof(double)); + } + else + { + for (i = 0; i < MAX_LOG_K_INDICES; i++) trxn.logk[i] += coef * r_ref.Get_logk()[i]; + } + /* + * Copy equation into work space + */ + next_token = &r_ref.token[0]; + while (next_token->s != NULL || next_token->name != NULL) + { + if (count_trxn + 1 > trxn.token.size()) + trxn.token.resize(count_trxn + 1); + if (next_token->s != NULL) + { + trxn.token[count_trxn].name = next_token->s->name; + trxn.token[count_trxn].s = next_token->s; + } + else + { + trxn.token[count_trxn].name = next_token->name; + trxn.token[count_trxn].s = NULL; + } + trxn.token[count_trxn].coef = coef * next_token->coef; + count_trxn++; + next_token++; + } + if (combine) + trxn_combine(); + return (OK); +} + +/* ---------------------------------------------------------------------- */ +bool Phreeqc:: +trxn_copy(CReaction& rxn_ref) +/* ---------------------------------------------------------------------- */ +{ + /* + * Copies trxn to a reaction structure. + * + * Input: rxn_ptr, pointer to reaction structure to copy trxn to. + * + */ + int i; + /* + * Copy logk data + */ + for (i = 0; i < MAX_LOG_K_INDICES; i++) + { + rxn_ref.logk[i] = trxn.logk[i]; + } + /* + * Copy dz data + */ + for (i = 0; i < 3; i++) + { + rxn_ref.dz[i] = trxn.dz[i]; + } + /* + * Copy tokens + */ + rxn_ref.Get_tokens().resize(count_trxn + 1); + for (size_t i = 0; i < count_trxn; i++) + { + rxn_ref.Get_tokens()[i].s = trxn.token[i].s; + rxn_ref.Get_tokens()[i].name = trxn.token[i].name; + rxn_ref.Get_tokens()[i].coef = trxn.token[i].coef; + } + rxn_ref.token[count_trxn].s = NULL; + rxn_ref.token[count_trxn].name = NULL; + return (OK); +} +/* ---------------------------------------------------------------------- */ +bool Phreeqc:: +phase_rxn_to_trxn(struct phase* phase_ptr, CReaction& rxn_ref) +/* ---------------------------------------------------------------------- */ +{ + /* + * Copy reaction from reaction structure to + * temp reaction structure. + */ + int l; + const char* cptr; + LDBLE l_z; + trxn.token.resize(rxn_ref.size()); + trxn.token[0].name = phase_ptr->formula; + /* charge */ + cptr = phase_ptr->formula; + { + std::string token; + get_token(&cptr, token, &l_z, &l); + } + trxn.token[0].z = l_z; + trxn.token[0].s = NULL; + trxn.token[0].unknown = NULL; + /*trxn.token[0].coef = -1.0; */ + /* check for leading coefficient of 1.0 for phase did not work */ + trxn.token[0].coef = phase_ptr->rxn.token[0].coef; + for (size_t i = 1; rxn_ref.token[i].s != NULL; i++) + { + trxn.token[i].name = rxn_ref.token[i].s->name; + trxn.token[i].z = rxn_ref.token[i].s->z; + trxn.token[i].s = NULL; + trxn.token[i].unknown = NULL; + trxn.token[i].coef = rxn_ref.token[i].coef; + count_trxn = i + 1; + } + return (OK); +} +/* ---------------------------------------------------------------------- */ +double Phreeqc:: +rxn_find_coef(CReaction& r_ref, const char* str) +/* ---------------------------------------------------------------------- */ +{ + /* + * Finds coefficient of token in reaction. + * input: r_ptr, pointer to a reaction structure + * str, string to find as reaction token + * + * Return: 0.0, if token not found + * coefficient of token, if found. + */ + struct rxn_token* r_token; + LDBLE coef; + + r_token = &r_ref.token[1]; + coef = 0.0; + while (r_token->s != NULL) + { + if (strcmp(r_token->s->name, str) == 0) + { + coef = r_token->coef; + break; + } + r_token++; + } + return (coef); +} +/* ---------------------------------------------------------------------- */ +double Phreeqc:: +calc_alk(CReaction& rxn_ref) +/* ---------------------------------------------------------------------- */ +{ + LDBLE return_value; + struct master* master_ptr; + + return_value = 0.0; + struct rxn_token* r_token = &rxn_ref.token[1]; + while (r_token->s != NULL) + { + master_ptr = r_token->s->secondary; + if (master_ptr == NULL) + { + master_ptr = r_token->s->primary; + } + if (master_ptr == NULL) + { + error_string = sformatf( + "Non-master species in secondary reaction, %s.", + rxn_ref.token[0].s->name); + error_msg(error_string, CONTINUE); + input_error++; + break; + } + return_value += r_token->coef * master_ptr->alk; + r_token++; + } + return (return_value); +} diff --git a/Classes.h b/Classes.h new file mode 100644 index 00000000..3099b642 --- /dev/null +++ b/Classes.h @@ -0,0 +1,34 @@ +#if !defined(CLASSES_H_INCLUDED) +#define CLASSES_H_INCLUDED +#include +#include "GasPhase.h" +#include "Surface.h" +class Model +{ +public: + Model() + { + force_prep = true; + gas_phase_type = cxxGasPhase::GP_UNKNOWN; + numerical_fixed_volume = false; + dl_type = cxxSurface::NO_DL; + surface_type = cxxSurface::UNKNOWN_DL; + }; + ~Model() + { + }; + + bool force_prep; + bool numerical_fixed_volume; + cxxGasPhase::GP_TYPE gas_phase_type; + std::vector gas_phase; + std::vector ss_assemblage; + std::vector pp_assemblage; + std::vector si; + std::vector add_formula; + cxxSurface::DIFFUSE_LAYER_TYPE dl_type; + cxxSurface::SURFACE_TYPE surface_type; + std::vector surface_comp; + std::vector surface_charge; +}; +#endif // !defined(CLASSES_H_INCLUDED) \ No newline at end of file diff --git a/Phreeqc.cpp b/Phreeqc.cpp index ee2a3239..00bb641d 100644 --- a/Phreeqc.cpp +++ b/Phreeqc.cpp @@ -1538,28 +1538,29 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) cxxNameDouble next_sys_total(pSrc->s[i]->next_sys_total); s_ptr->next_sys_total = NameDouble2elt_list(next_sys_total); } - //rxn - s_ptr->rxn = NULL; - if (pSrc->s[i]->rxn != NULL) - { - cxxChemRxn rxn(pSrc->s[i]->rxn); - s_ptr->rxn = cxxChemRxn2rxn(rxn); - //s_ptr->rxn = rxn_copy_operator(pSrc->s[i]->rxn); - } - //rxn_s - s_ptr->rxn_s = NULL; - if (pSrc->s[i]->rxn_s != NULL) - { - cxxChemRxn rxn_s(pSrc->s[i]->rxn_s); - s_ptr->rxn_s = cxxChemRxn2rxn(rxn_s); - } - //rxn_x - s_ptr->rxn_x = NULL; - if (pSrc->s[i]->rxn_x != NULL) - { - cxxChemRxn rxn_x(pSrc->s[i]->rxn_x); - s_ptr->rxn_x = cxxChemRxn2rxn(rxn_x); - } +// NEEDS REVISION + ////rxn + //s_ptr->rxn = NULL; + //if (pSrc->s[i]->rxn != NULL) + //{ + // cxxChemRxn rxn(pSrc->s[i]->rxn); + // s_ptr->rxn = cxxChemRxn2rxn(rxn); + // //s_ptr->rxn = rxn_copy_operator(pSrc->s[i]->rxn); + //} + ////rxn_s + //s_ptr->rxn_s = NULL; + //if (pSrc->s[i]->rxn_s != NULL) + //{ + // cxxChemRxn rxn_s(pSrc->s[i]->rxn_s); + // s_ptr->rxn_s = cxxChemRxn2rxn(rxn_s); + //} + ////rxn_x + //s_ptr->rxn_x = NULL; + //if (pSrc->s[i]->rxn_x != NULL) + //{ + // cxxChemRxn rxn_x(pSrc->s[i]->rxn_x); + // s_ptr->rxn_x = cxxChemRxn2rxn(rxn_x); + //} } s_h2o = s_search("H2O"); s_hplus = s_search("H+"); @@ -1598,27 +1599,28 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) cxxNameDouble next_sys_total(pSrc->phases[i]->next_sys_total); phase_ptr->next_sys_total = NameDouble2elt_list(next_sys_total); } +// NEEDS REVISION //rxn - phase_ptr->rxn = NULL; - if (pSrc->phases[i]->rxn != NULL) - { - cxxChemRxn rxn(pSrc->phases[i]->rxn); - phase_ptr->rxn = cxxChemRxn2rxn(rxn); - } - //rxn_s - //phase_ptr->rxn_s = NULL; - if (pSrc->phases[i]->rxn_s != NULL) - { - cxxChemRxn rxn_s(pSrc->phases[i]->rxn_s); - phase_ptr->rxn_s = cxxChemRxn2rxn(rxn_s); - } - //rxn_x - //phase_ptr->rxn_x = NULL; - if (pSrc->phases[i]->rxn_x != NULL) - { - cxxChemRxn rxn_x(pSrc->phases[i]->rxn_x); - phase_ptr->rxn_x = cxxChemRxn2rxn(rxn_x); - } + //phase_ptr->rxn = NULL; + //if (pSrc->phases[i]->rxn != NULL) + //{ + // cxxChemRxn rxn(pSrc->phases[i]->rxn); + // phase_ptr->rxn = cxxChemRxn2rxn(rxn); + //} + ////rxn_s + ////phase_ptr->rxn_s = NULL; + //if (pSrc->phases[i]->rxn_s != NULL) + //{ + // cxxChemRxn rxn_s(pSrc->phases[i]->rxn_s); + // phase_ptr->rxn_s = cxxChemRxn2rxn(rxn_s); + //} + ////rxn_x + ////phase_ptr->rxn_x = NULL; + //if (pSrc->phases[i]->rxn_x != NULL) + //{ + // cxxChemRxn rxn_x(pSrc->phases[i]->rxn_x); + // phase_ptr->rxn_x = cxxChemRxn2rxn(rxn_x); + //} } /*---------------------------------------------------------------------- * Master species @@ -1643,20 +1645,21 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) master[i]->elt = element_store(pSrc->master[i]->elt->name); master[i]->unknown = NULL; master[i]->s = s_store(pSrc->master[i]->s->name, pSrc->master[i]->s->z, false); - //rxn_primary - master[i]->rxn_primary = NULL; - if (pSrc->master[i]->rxn_primary != NULL) - { - cxxChemRxn rxn_primary(pSrc->master[i]->rxn_primary); - master[i]->rxn_primary = cxxChemRxn2rxn(rxn_primary); - } - //rxn_secondary - master[i]->rxn_secondary = NULL; - if (pSrc->master[i]->rxn_secondary != NULL) - { - cxxChemRxn rxn_secondary(pSrc->master[i]->rxn_secondary); - master[i]->rxn_secondary = cxxChemRxn2rxn(rxn_secondary); - } +// NEEDS REVISION + ////rxn_primary + //master[i]->rxn_primary = NULL; + //if (pSrc->master[i]->rxn_primary != NULL) + //{ + // cxxChemRxn rxn_primary(pSrc->master[i]->rxn_primary); + // master[i]->rxn_primary = cxxChemRxn2rxn(rxn_primary); + //} + ////rxn_secondary + //master[i]->rxn_secondary = NULL; + //if (pSrc->master[i]->rxn_secondary != NULL) + //{ + // cxxChemRxn rxn_secondary(pSrc->master[i]->rxn_secondary); + // master[i]->rxn_secondary = cxxChemRxn2rxn(rxn_secondary); + //} } /*---------------------------------------------------------------------- * Unknowns diff --git a/Phreeqc.h b/Phreeqc.h index c85f0d9a..d0198f88 100644 --- a/Phreeqc.h +++ b/Phreeqc.h @@ -59,6 +59,7 @@ class cxxSolutionIsotope; class cxxSSassemblage; class cxxSS; class cxxStorageBin; +class CReaction; #include "classes.h" @@ -553,7 +554,8 @@ public: int build_species_list(int n); int build_min_surface(void); LDBLE calc_lk_phase(phase* p_ptr, LDBLE TK, LDBLE pa); - LDBLE calc_delta_v(reaction* r_ptr, bool phase); + LDBLE calc_delta_v(struct reaction* r_ptr, bool phase); + double calc_delta_v(CReaction& r_ref, bool phase); LDBLE calc_PR(std::vector phase_ptrs, LDBLE P, LDBLE TK, LDBLE V_m); LDBLE calc_PR(); int calc_vm(LDBLE tc, LDBLE pa); @@ -615,7 +617,7 @@ public: int print_exchange(void); int print_gas_phase(void); int print_master_reactions(void); - int print_reaction(struct reaction* rxn_ptr); + //int print_reaction(struct reaction* rxn_ptr); int print_species(void); int print_surface(void); int print_user_print(void); @@ -866,9 +868,9 @@ public: struct rate* rate_copy(struct rate* rate_ptr); struct rate* rate_search(const char* name, int* n); int rate_sort(void); - struct reaction* rxn_alloc(int ntokens); - struct reaction* rxn_dup(struct reaction* rxn_ptr_old); - struct reaction* cxxChemRxn2rxn(cxxChemRxn& cr); + struct reaction rxn_alloc(int ntokens); + struct reaction rxn_dup(struct reaction& rxn_ptr_old); + struct reaction cxxChemRxn2rxn(cxxChemRxn& cr); LDBLE rxn_find_coef(struct reaction* r_ptr, const char* str); int rxn_free(struct reaction* rxn_ptr); int rxn_print(struct reaction* rxn_ptr); @@ -895,6 +897,12 @@ public: int trxn_reverse_k(void); int trxn_sort(void); int trxn_swap(const char* token); + bool trxn_add(CReaction& r_ptr, double coef, bool combine); + bool trxn_add_phase(CReaction& r_ref, double coef, bool combine); + bool trxn_copy(CReaction& rxn_ref); + double rxn_find_coef(CReaction& r_ptr, const char* str); + bool phase_rxn_to_trxn(struct phase* phase_ptr, CReaction& rxn_ptr); + double calc_alk(CReaction& rxn_ptr); struct unknown* unknown_alloc(void); int unknown_delete(int i); int unknown_free(struct unknown* unknown_ptr); @@ -1455,7 +1463,7 @@ protected: *---------------------------------------------------------------------- */ struct reaction_temp trxn; /* structure array of working space while reading equations species names are in "temp_strings" */ - int count_trxn; /* number of reactants in trxn = position of next */ + size_t count_trxn; /* number of reactants in trxn = position of next */ std::vector mb_unknowns; diff --git a/basicsubs.cpp b/basicsubs.cpp index e305a75a..c1bf8274 100644 --- a/basicsubs.cpp +++ b/basicsubs.cpp @@ -644,15 +644,15 @@ calc_logk_p(const char *name) if (phase_ptr != NULL) { - struct reaction *reaction_ptr; + CReaction* reaction_ptr; if (phase_ptr->replaced) - reaction_ptr = phase_ptr->rxn_s; + reaction_ptr = &phase_ptr->rxn_s; else - reaction_ptr = phase_ptr->rxn; + reaction_ptr = &phase_ptr->rxn; /* * Print saturation index */ - reaction_ptr->logk[delta_v] = calc_delta_v(reaction_ptr, true) - + reaction_ptr->logk[delta_v] = calc_delta_v(*reaction_ptr, true) - phase_ptr->logk[vm0]; if (reaction_ptr->logk[delta_v]) mu_terms_in_logk = true; @@ -684,7 +684,7 @@ calc_logk_s(const char *name) { //if (s_ptr->logk[vm_tc]) /* calculate delta_v for the reaction... */ - s_ptr->logk[delta_v] = calc_delta_v(s_ptr->rxn, false); + s_ptr->logk[delta_v] = calc_delta_v(*&s_ptr->rxn, false); for (i = 0; i < MAX_LOG_K_INDICES; i++) { l_logk[i] = 0.0; @@ -753,15 +753,15 @@ calc_deltah_p(const char* name) if (phase_ptr != NULL) { - struct reaction* reaction_ptr; + CReaction* reaction_ptr; if (phase_ptr->replaced) - reaction_ptr = phase_ptr->rxn_s; + reaction_ptr = &phase_ptr->rxn_s; else - reaction_ptr = phase_ptr->rxn; + reaction_ptr = &phase_ptr->rxn; /* * Print saturation index */ - reaction_ptr->logk[delta_v] = calc_delta_v(reaction_ptr, true) - + reaction_ptr->logk[delta_v] = calc_delta_v(*reaction_ptr, true) - phase_ptr->logk[vm0]; if (reaction_ptr->logk[delta_v]) mu_terms_in_logk = true; @@ -793,7 +793,7 @@ calc_deltah_s(const char* name) if (s_ptr != NULL) { /* calculate delta_v for the reaction... */ - s_ptr->logk[delta_v] = calc_delta_v(s_ptr->rxn, false); + s_ptr->logk[delta_v] = calc_delta_v(*&s_ptr->rxn, false); for (i = 0; i < MAX_LOG_K_INDICES; i++) { l_logk[i] = 0.0; @@ -1735,7 +1735,7 @@ saturation_ratio(const char *phase_name) } else if (phase_ptr->in != FALSE) { - for (rxn_ptr = &phase_ptr->rxn_x->token[0] + 1; rxn_ptr->s != NULL; + for (rxn_ptr = &phase_ptr->rxn_x.token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { iap += rxn_ptr->s->la * rxn_ptr->coef; @@ -1767,7 +1767,7 @@ saturation_index(const char *phase_name, LDBLE * iap, LDBLE * si) } else if (phase_ptr->in != FALSE) { - for (rxn_ptr = &phase_ptr->rxn_x->token[0] + 1; rxn_ptr->s != NULL; + for (rxn_ptr = &phase_ptr->rxn_x.token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { *iap += rxn_ptr->s->la * rxn_ptr->coef; @@ -2293,7 +2293,7 @@ surf_total(const char *total_name, const char *surface_name) struct rxn_token *rxn_ptr; if (s_x[j]->mole_balance == NULL) { - for (rxn_ptr = &s_x[j]->rxn_s->token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) + for (rxn_ptr = &s_x[j]->rxn_s.token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { if (redox && rxn_ptr->s->secondary) { @@ -3024,7 +3024,7 @@ system_total_si(void) * Print saturation index */ iap = 0.0; - for (rxn_ptr = &phases[i]->rxn_x->token[0] + 1; rxn_ptr->s != NULL; + for (rxn_ptr = &phases[i]->rxn_x.token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { iap += rxn_ptr->s->la * rxn_ptr->coef; diff --git a/gases.cpp b/gases.cpp index a3581391..f0b177fe 100644 --- a/gases.cpp +++ b/gases.cpp @@ -75,7 +75,7 @@ build_fixed_volume_gas(void) */ count_elts = 0; paren_count = 0; - if (phase_ptr->rxn_x == NULL) + if (phase_ptr->rxn_x.token.size() == 0) continue; add_elt_list(phase_ptr->next_elt, 1.0); #define COMBINE @@ -174,7 +174,7 @@ build_fixed_volume_gas(void) } row = unknown_ptr->number * (count_unknowns + 1); coef_elt = elt_list[j].coef; - for (rxn_ptr = &phase_ptr->rxn_x->token[0] + 1; + for (rxn_ptr = &phase_ptr->rxn_x.token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { @@ -246,7 +246,7 @@ build_fixed_volume_gas(void) } unknown_ptr = gas_unknown; row = unknown_ptr->number * (count_unknowns + 1); - for (rxn_ptr = &phase_ptr->rxn_x->token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) + for (rxn_ptr = &phase_ptr->rxn_x.token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { if (rxn_ptr->s != s_eminus && rxn_ptr->s->in == FALSE) { @@ -648,8 +648,8 @@ calc_fixed_volume_gas_pressures(void) if (phase_ptr->in == TRUE) { lp = -phase_ptr->lk; - //lp = -k_calc(phase_ptr->rxn_x->logk, tk_x, use.Get_gas_phase_ptr()->total_p * PASCAL_PER_ATM); - for (rxn_ptr = &phase_ptr->rxn_x->token[0] + 1; rxn_ptr->s != NULL; + //lp = -k_calc(phase_ptr->rxn_x.logk, tk_x, use.Get_gas_phase_ptr()->total_p * PASCAL_PER_ATM); + for (rxn_ptr = &phase_ptr->rxn_x.token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { lp += rxn_ptr->s->la * rxn_ptr->coef; diff --git a/global_structures.h b/global_structures.h index 0d25736c..b89f3729 100644 --- a/global_structures.h +++ b/global_structures.h @@ -2,6 +2,7 @@ #define _INC_GLOBAL_STRUCTURES_H #include "Surface.h" #include "GasPhase.h" +#include "CReaction.h" /* ---------------------------------------------------------------------- * #define DEFINITIONS * ---------------------------------------------------------------------- */ @@ -438,19 +439,38 @@ public: dz[i] =0.0; } } - cxxChemRxn(struct reaction *rxn) + cxxChemRxn(struct reaction* rxn_ptr) { logk[0] = dz[0] = 0.0; for (size_t i = 0; i < MAX_LOG_K_INDICES; i++) { - logk[i] = rxn->logk[i]; + logk[i] = rxn_ptr->logk[i]; } for (size_t i = 0; i < 3; i++) { - dz[i] = rxn->dz[i]; + dz[i] = rxn_ptr->dz[i]; } struct rxn_token *next_token; - next_token = &rxn->token[0]; + next_token = &rxn_ptr->token[0]; + this->tokens.push_back(*next_token++); + while (next_token->s != NULL || next_token->name != NULL) + { + this->tokens.push_back(*next_token++); + } + } + cxxChemRxn(CReaction& rxn_ref) + { + logk[0] = dz[0] = 0.0; + for (size_t i = 0; i < MAX_LOG_K_INDICES; i++) + { + logk[i] = rxn_ref.logk[i]; + } + for (size_t i = 0; i < 3; i++) + { + dz[i] = rxn_ref.dz[i]; + } + struct rxn_token* next_token; + next_token = &rxn_ref.token[0]; this->tokens.push_back(*next_token++); while (next_token->s != NULL || next_token->name != NULL) { @@ -533,10 +553,10 @@ struct species struct elt_list *next_secondary; struct elt_list *next_sys_total; int check_equation; /* switch to check equation for charge and element balance */ - struct reaction *rxn; /* pointer to data base reaction */ - struct reaction *rxn_s; /* pointer to reaction converted to secondary and primary + CReaction rxn; /* pointer to data base reaction */ + CReaction rxn_s; /* pointer to reaction converted to secondary and primary master species */ - struct reaction *rxn_x; /* reaction to be used in model */ + CReaction rxn_x; /* reaction to be used in model */ LDBLE tot_g_moles; /* (1 + sum(g)) * moles */ LDBLE tot_dh2o_moles; /* sum(moles*g*Ws/Waq) */ LDBLE cd_music[5]; @@ -589,10 +609,10 @@ struct phase struct elt_list *next_elt; /* pointer to list of elements in phase */ struct elt_list *next_sys_total; int check_equation; /* switch to check equation for charge and element balance */ - struct reaction *rxn; /* pointer to data base reaction */ - struct reaction *rxn_s; /* pointer to reaction converted to secondary and primary + CReaction rxn; /* pointer to data base reaction */ + CReaction rxn_s; /* pointer to reaction converted to secondary and primary master species */ - struct reaction *rxn_x; /* reaction to be used in model */ + CReaction rxn_x; /* reaction to be used in model */ int replaced; /* equation contains solids or gases */ int in_system; }; @@ -619,9 +639,9 @@ struct phase const char *gfw_formula; /* formula from which to calcuate gfw */ struct unknown *unknown; /* pointer to unknown structure */ struct species *s; /* pointer to species structure */ - struct reaction *rxn_primary; /* reaction writes master species in terms of primary + CReaction rxn_primary; /* reaction writes master species in terms of primary master species */ - struct reaction *rxn_secondary; /* reaction writes master species in terms of secondary + CReaction rxn_secondary; /* reaction writes master species in terms of secondary master species */ const char * pe_rxn; int minor_isotope; diff --git a/inverse.cpp b/inverse.cpp index 7fc350a3..0b0677a3 100644 --- a/inverse.cpp +++ b/inverse.cpp @@ -141,7 +141,7 @@ setup_inverse(struct inverse *inv_ptr) char token[MAX_LENGTH]; struct phase *phase_ptr; cxxSolution *solution_ptr; - struct reaction *rxn_ptr; + CReaction *rxn_ptr; struct master *master_ptr; /* * Determine array sizes, row and column positions @@ -407,7 +407,7 @@ setup_inverse(struct inverse *inv_ptr) for (size_t i = 0; i < inv_ptr->phases.size(); i++) { phase_ptr = inv_ptr->phases[i].phase; - rxn_ptr = phase_ptr->rxn_s; + rxn_ptr = &phase_ptr->rxn_s; column = col_phases + i; col_name[column] = phase_ptr->name; for (j = 1; rxn_ptr->token[j].s != NULL; j++) @@ -449,7 +449,7 @@ setup_inverse(struct inverse *inv_ptr) rxn_ptr->token[j].coef * coef; } row = master_alk->in; /* include alkalinity for phase */ - my_array[(size_t)row * max_column_count + (size_t)column] = calc_alk(rxn_ptr); + my_array[(size_t)row * max_column_count + (size_t)column] = calc_alk(*rxn_ptr); } /* mass balance: redox reaction data */ @@ -460,7 +460,7 @@ setup_inverse(struct inverse *inv_ptr) if (inv_ptr->elts[i].master->s->primary == NULL) { coef = inv_ptr->elts[i].master->coef; - rxn_ptr = inv_ptr->elts[i].master->rxn_primary; + rxn_ptr = &inv_ptr->elts[i].master->rxn_primary; column = col_redox + k; col_name[column] = inv_ptr->elts[i].master->elt->name; k++; @@ -505,7 +505,7 @@ setup_inverse(struct inverse *inv_ptr) } row = master_alk->in; /* include alkalinity for redox reaction */ my_array[(size_t)row * max_column_count + (size_t)column] = - (calc_alk(rxn_ptr) - inv_ptr->elts[i].master->s->alk) / coef; + (calc_alk(*rxn_ptr) - inv_ptr->elts[i].master->s->alk) / coef; } } @@ -1917,7 +1917,7 @@ print_model(struct inverse *inv_ptr) LDBLE t_i, p_i, iap, lk, t; const char *name; struct rxn_token *rxn_ptr; - struct reaction *reaction_ptr; + CReaction *reaction_ptr; output_msg(sformatf( "\n%-25.25s %2s %12.12s %12.12s %-18.18s (Approximate SI in solution ", "Phase mole transfers:", " ", "Minimum", "Maximum", "Formula")); @@ -1955,12 +1955,12 @@ print_model(struct inverse *inv_ptr) { if (Utilities::strcmp_nocase(phases[i1]->name, col_name[i])) continue; - reaction_ptr = phases[i1]->rxn_s; + reaction_ptr = &phases[i1]->rxn_s; for (i2 = 0; i2 < inv_ptr->count_solns; i2++) { solution_ptr = Utilities::Rxn_find(Rxn_solution_map, inv_ptr->solns[i2]); - reaction_ptr->logk[delta_v] = calc_delta_v(reaction_ptr, true) - phases[i1]->logk[vm0]; + reaction_ptr->logk[delta_v] = calc_delta_v(*reaction_ptr, true) - phases[i1]->logk[vm0]; if (reaction_ptr->logk[delta_v]) mu_terms_in_logk = true; lk = k_calc(reaction_ptr->logk, t_i, p_i); @@ -4905,7 +4905,7 @@ dump_netpath_pat(struct inverse *inv_ptr) */ std::string token; sum = 0; - for (rxn_ptr = &inv_ptr->phases[i].phase->rxn_s->token[0] + 1; + for (rxn_ptr = &inv_ptr->phases[i].phase->rxn_s.token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { if (rxn_ptr->s == s_hplus) diff --git a/mainsubs.cpp b/mainsubs.cpp index 3074cec1..93aeb4e6 100644 --- a/mainsubs.cpp +++ b/mainsubs.cpp @@ -640,7 +640,7 @@ initial_gas_phases(int print) if (phase_ptr->in == TRUE) { lp = -phase_ptr->lk; - for (rxn_ptr = &phase_ptr->rxn_x->token[0] + 1; + for (rxn_ptr = &phase_ptr->rxn_x.token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { lp += rxn_ptr->s->la * rxn_ptr->coef; diff --git a/model.cpp b/model.cpp index 3135694e..71981566 100644 --- a/model.cpp +++ b/model.cpp @@ -674,18 +674,18 @@ gammas(LDBLE mu) continue; { LDBLE coef = 0, z = 0; - for (j = 1; s_x[i]->rxn_x->token[j].s != NULL; j++) + for (j = 1; s_x[i]->rxn_x.token[j].s != NULL; j++) { - if (s_x[i]->rxn_x->token[j].s->type == EX) + if (s_x[i]->rxn_x.token[j].s->type == EX) { s_x[i]->alk = - s_x[i]->rxn_x->token[j].s->primary->unknown->moles; + s_x[i]->rxn_x.token[j].s->primary->unknown->moles; //break; } - else if (s_x[i]->rxn_x->token[j].s->type <= HPLUS) + else if (s_x[i]->rxn_x.token[j].s->type <= HPLUS) { - coef = s_x[i]->rxn_x->token[j].coef; - z = s_x[i]->rxn_x->token[j].s->z; + coef = s_x[i]->rxn_x.token[j].coef; + z = s_x[i]->rxn_x.token[j].s->z; } } if (!use.Get_exchange_ptr()->Get_pitzer_exchange_gammas()) @@ -775,12 +775,12 @@ gammas(LDBLE mu) * Find moles of sites. * s_x[i]->equiv is stoichiometric coefficient of sites in species */ - for (j = 1; s_x[i]->rxn_x->token[j].s != NULL; j++) + for (j = 1; s_x[i]->rxn_x.token[j].s != NULL; j++) { - if (s_x[i]->rxn_x->token[j].s->type == SURF) + if (s_x[i]->rxn_x.token[j].s->type == SURF) { s_x[i]->alk = - s_x[i]->rxn_x->token[j].s->primary->unknown->moles; + s_x[i]->rxn_x.token[j].s->primary->unknown->moles; break; } } @@ -872,13 +872,13 @@ int Phreeqc::gammas_a_f(int i1) //struct master *m_ptr; i = i1; - for (j = 1; s_x[i]->rxn_x->token[j].s != NULL; j++) + for (j = 1; s_x[i]->rxn_x.token[j].s != NULL; j++) { - if (s_x[i]->rxn_x->token[j].s->type == EX) + if (s_x[i]->rxn_x.token[j].s->type == EX) { - //strcpy(name, s_x[i]->rxn_x->token[j].s->name); - name = s_x[i]->rxn_x->token[j].s->name; - //m_ptr = s_x[i]->rxn_x->token[j].s->primary->elt->master; // appt debug + //strcpy(name, s_x[i]->rxn_x.token[j].s->name); + name = s_x[i]->rxn_x.token[j].s->name; + //m_ptr = s_x[i]->rxn_x.token[j].s->primary->elt->master; // appt debug break; } } @@ -887,11 +887,11 @@ int Phreeqc::gammas_a_f(int i1) { if (s_x[i]->gflag != 4 || s_x[i]->primary) continue; - for (j = 1; s_x[i]->rxn_x->token[j].s != NULL; j++) + for (j = 1; s_x[i]->rxn_x.token[j].s != NULL; j++) { - if (s_x[i]->rxn_x->token[j].s->type == EX) + if (s_x[i]->rxn_x.token[j].s->type == EX) { - if (!strcmp(name.c_str(), s_x[i]->rxn_x->token[j].s->name)) + if (!strcmp(name.c_str(), s_x[i]->rxn_x.token[j].s->name)) sum += s_x[i]->moles * s_x[i]->equiv; break; } @@ -2211,10 +2211,10 @@ mb_ss(void) /* * Calculate IAPc and IAPb */ - if (phase0_ptr->in == TRUE && phase0_ptr->rxn_x != NULL) + if (phase0_ptr->in == TRUE && phase0_ptr->rxn_x.token.size() != 0) { log10_iap = 0; - for (rxn_ptr = &phase0_ptr->rxn_x->token[0] + 1; + for (rxn_ptr = &phase0_ptr->rxn_x.token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { log10_iap += rxn_ptr->s->la * rxn_ptr->coef; @@ -2225,10 +2225,10 @@ mb_ss(void) { iapc = 1e-99; } - if (phase1_ptr->in == TRUE && phase1_ptr->rxn_x != NULL) + if (phase1_ptr->in == TRUE && phase1_ptr->rxn_x.token.size() != 0) { log10_iap = 0; - for (rxn_ptr = &phase1_ptr->rxn_x->token[0] + 1; + for (rxn_ptr = &phase1_ptr->rxn_x.token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { log10_iap += rxn_ptr->s->la * rxn_ptr->coef; @@ -2293,7 +2293,7 @@ mb_ss(void) if (phase_ptr->in == TRUE) { lp = -phase_ptr->lk; - for (rxn_ptr = &phase_ptr->rxn_x->token[0] + 1; + for (rxn_ptr = &phase_ptr->rxn_x.token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { lp += rxn_ptr->s->la * rxn_ptr->coef; @@ -2367,7 +2367,7 @@ molalities(int allow_overflow) * lm and moles for all aqueous species */ s_x[i]->lm = s_x[i]->lk - s_x[i]->lg; - for (rxn_ptr = &s_x[i]->rxn_x->token[0] + 1; rxn_ptr->s != NULL; + for (rxn_ptr = &s_x[i]->rxn_x.token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { s_x[i]->lm += rxn_ptr->s->la * rxn_ptr->coef; @@ -2639,7 +2639,7 @@ calc_gas_pressures(void) if (phase_ptr->in == TRUE) { lp = -phase_ptr->lk; - for (rxn_ptr = &phase_ptr->rxn_x->token[0] + 1; rxn_ptr->s != NULL; + for (rxn_ptr = &phase_ptr->rxn_x.token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { lp += rxn_ptr->s->la * rxn_ptr->coef; diff --git a/pitzer.cpp b/pitzer.cpp index d083f668..3e650d24 100644 --- a/pitzer.cpp +++ b/pitzer.cpp @@ -2440,12 +2440,12 @@ gammas_pz(bool exch_a_f) * Find moles of sites. * s_x[i]->equiv is stoichiometric coefficient of sites in species */ - for (j = 1; s_x[i]->rxn_x->token[j].s != NULL; j++) + for (j = 1; s_x[i]->rxn_x.token[j].s != NULL; j++) { - if (s_x[i]->rxn_x->token[j].s->type == SURF) + if (s_x[i]->rxn_x.token[j].s->type == SURF) { s_x[i]->alk = - s_x[i]->rxn_x->token[j].s->primary->unknown->moles; + s_x[i]->rxn_x.token[j].s->primary->unknown->moles; break; } } @@ -2512,11 +2512,11 @@ gammas_pz(bool exch_a_f) * Find CEC * z contains valence of cation for exchange species, alk contains cec */ - for (j = 1; s_x[i]->rxn_x->token[j].s != NULL; j++) + for (j = 1; s_x[i]->rxn_x.token[j].s != NULL; j++) { - if (s_x[i]->rxn_x->token[j].s->type == EX) + if (s_x[i]->rxn_x.token[j].s->type == EX) { - s_x[i]->alk = s_x[i]->rxn_x->token[j].s->primary->unknown->moles; + s_x[i]->alk = s_x[i]->rxn_x.token[j].s->primary->unknown->moles; break; } } @@ -2540,12 +2540,12 @@ gammas_pz(bool exch_a_f) if (use.Get_exchange_ptr()->Get_pitzer_exchange_gammas()) { /* Assume equal gamma's of solute and exchangeable species... */ - for (j = 1; s_x[i]->rxn_x->token[j].s != NULL; j++) + for (j = 1; s_x[i]->rxn_x.token[j].s != NULL; j++) { - if (s_x[i]->rxn_x->token[j].s->type == EX) + if (s_x[i]->rxn_x.token[j].s->type == EX) continue; - coef = s_x[i]->rxn_x->token[j].coef; - s_x[i]->lg += coef * s_x[i]->rxn_x->token[j].s->lg; + coef = s_x[i]->rxn_x.token[j].coef; + s_x[i]->lg += coef * s_x[i]->rxn_x.token[j].s->lg; } } if (s_x[i]->a_f && s_x[i]->primary == NULL && s_x[i]->moles) diff --git a/prep.cpp b/prep.cpp index b8bc461b..583515b5 100644 --- a/prep.cpp +++ b/prep.cpp @@ -380,7 +380,7 @@ build_gas_phase(void) */ count_elts = 0; paren_count = 0; - if (phase_ptr->rxn_x == NULL) + if (phase_ptr->rxn_x.token.size() == 0) continue; add_elt_list(phase_ptr->next_elt, 1.0); #ifdef COMBINE @@ -477,7 +477,7 @@ build_gas_phase(void) } row = unknown_ptr->number * (count_unknowns + 1); coef_elt = elt_list[j].coef; - for (rxn_ptr = &phase_ptr->rxn_x->token[0] + 1; + for (rxn_ptr = &phase_ptr->rxn_x.token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { @@ -549,7 +549,7 @@ build_gas_phase(void) } unknown_ptr = gas_unknown; row = unknown_ptr->number * (count_unknowns + 1); - for (rxn_ptr = &phase_ptr->rxn_x->token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) + for (rxn_ptr = &phase_ptr->rxn_x.token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { if (rxn_ptr->s != s_eminus && rxn_ptr->s->in == FALSE) { @@ -654,10 +654,10 @@ build_ss_assemblage(void) /* * Calculate function value (inverse saturation index) */ - if (x[i]->phase->rxn_x == NULL) + if (x[i]->phase->rxn_x.token.size() == 0) continue; store_mb(&(x[i]->phase->lk), &(x[i]->f), 1.0); - for (rxn_ptr = &x[i]->phase->rxn_x->token[0] + 1; rxn_ptr->s != NULL; + for (rxn_ptr = &x[i]->phase->rxn_x.token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { store_mb(&(rxn_ptr->s->la), &(x[i]->f), -rxn_ptr->coef); @@ -671,7 +671,7 @@ build_ss_assemblage(void) * Put coefficients into mass action equations */ /* first IAP terms */ - for (rxn_ptr = &x[i]->phase->rxn_x->token[0] + 1; rxn_ptr->s != NULL; + for (rxn_ptr = &x[i]->phase->rxn_x.token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { if (rxn_ptr->s->secondary != NULL @@ -1143,12 +1143,12 @@ build_model(void) add_potential_factor(); add_cd_music_factors(i); } - rxn_free(s[i]->rxn_x); - s[i]->rxn_x = rxn_alloc(count_trxn + 1); + //rxn_free(s[i]->rxn_x); + //s[i]->rxn_x = rxn_alloc(count_trxn + 1); trxn_copy(s[i]->rxn_x); for (j = 0; j < 3; j++) { - s[i]->dz[j] = s[i]->rxn_x->dz[j]; + s[i]->dz[j] = s[i]->rxn_x.dz[j]; } if (debug_mass_action == TRUE) { @@ -1304,13 +1304,13 @@ build_model(void) */ write_mass_action_eqn_x(STOP); trxn_reverse_k(); - rxn_free(phases[i]->rxn_x); + //rxn_free(phases[i]->rxn_x); //if (debug_prep == TRUE) //{ // output_msg(sformatf( "\nPhase: %s\n", phases[i]->name)); // trxn_print(); //} - phases[i]->rxn_x = rxn_alloc(count_trxn + 1); + //phases[i]->rxn_x = rxn_alloc(count_trxn + 1); trxn_copy(phases[i]->rxn_x); write_phase_sys_total(i); } @@ -1363,7 +1363,7 @@ build_pure_phases(void) */ for (int i = 0; i < count_unknowns; i++) { - if (x[i]->type != PP || x[i]->phase->rxn_x == NULL) + if (x[i]->type != PP || x[i]->phase->rxn_x.token.size() == 0) continue; if (pure_phase_unknown == NULL) pure_phase_unknown = x[i]; @@ -1371,7 +1371,7 @@ build_pure_phases(void) store_mb(&(x[i]->phase->lk), &(x[i]->f), 1.0); store_mb(&(x[i]->si), &(x[i]->f), 1.0); - for (rxn_ptr = &x[i]->phase->rxn_x->token[0] + 1; rxn_ptr->s != NULL; + for (rxn_ptr = &x[i]->phase->rxn_x.token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { store_mb(&(rxn_ptr->s->la), &(x[i]->f), -rxn_ptr->coef); @@ -1382,12 +1382,12 @@ build_pure_phases(void) /* * rxn_x is null if an element in phase is not in solution */ - if (x[i]->type != PP || x[i]->phase->rxn_x == NULL) + if (x[i]->type != PP || x[i]->phase->rxn_x.token.size() == 0) continue; /* * Put coefficients into IAP equations */ - for (rxn_ptr = &x[i]->phase->rxn_x->token[0] + 1; rxn_ptr->s != NULL; + for (rxn_ptr = &x[i]->phase->rxn_x.token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { if (rxn_ptr->s->secondary != NULL @@ -1554,7 +1554,7 @@ build_solution_phase_boundaries(void) input_error++; break; } - for (rxn_ptr = &x[i]->phase->rxn_x->token[0] + 1; rxn_ptr->s != NULL; + for (rxn_ptr = &x[i]->phase->rxn_x.token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { store_mb(&(rxn_ptr->s->la), &(x[i]->f), -rxn_ptr->coef); @@ -1569,7 +1569,7 @@ build_solution_phase_boundaries(void) { if (x[i]->type != SOLUTION_PHASE_BOUNDARY) continue; - for (rxn_ptr = &x[i]->phase->rxn_x->token[0] + 1; rxn_ptr->s != NULL; + for (rxn_ptr = &x[i]->phase->rxn_x.token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { if (rxn_ptr->s->secondary != NULL @@ -1741,8 +1741,8 @@ clear(void) /* * copy primary reaction to secondary reaction */ - rxn_free(master[i]->rxn_secondary); - master[i]->rxn_secondary = rxn_dup(master[i]->rxn_primary); + //rxn_free(master[i]->rxn_secondary); + master[i]->rxn_secondary = master[i]->rxn_primary; } if (state == INITIAL_SOLUTION) @@ -2083,7 +2083,7 @@ is_special(struct species *l_spec) struct rxn_token *token_ptr; special = TRUE; - for (token_ptr = &l_spec->rxn_s->token[0] + 1; token_ptr->s != NULL; + for (token_ptr = &l_spec->rxn_s.token[0] + 1; token_ptr->s != NULL; token_ptr++) { if (token_ptr->s != s_hplus && @@ -2518,8 +2518,8 @@ reprep(void) { if (master[i]->in == FALSE) continue; - rxn_free(master[i]->rxn_secondary); - master[i]->rxn_secondary = rxn_dup(master[i]->rxn_primary); + //rxn_free(master[i]->rxn_secondary); + master[i]->rxn_secondary = master[i]->rxn_primary; } resetup_master(); /* @@ -2578,8 +2578,8 @@ resetup_master(void) { if (master_ptr->s->primary == NULL) { - rxn_free(master_ptr->rxn_secondary); - master_ptr->rxn_secondary = rxn_dup(master_ptr->s->rxn_s); + //rxn_free(master_ptr->rxn_secondary); + master_ptr->rxn_secondary = master_ptr->s->rxn_s; } } else @@ -2587,8 +2587,8 @@ resetup_master(void) if (master_ptr0->s->primary == NULL) { rewrite_master_to_secondary(master_ptr, master_ptr0); - rxn_free(master_ptr->rxn_secondary); - master_ptr->rxn_secondary = rxn_alloc(count_trxn + 1); + //rxn_free(master_ptr->rxn_secondary); + //master_ptr->rxn_secondary = rxn_alloc(count_trxn + 1); trxn_copy(master_ptr->rxn_secondary); } } @@ -3704,8 +3704,8 @@ setup_master_rxn(const std::vector &master_ptr_list, const std: master_ptr->in = TRUE; if (master_ptr->s->primary == NULL) { - rxn_free(master_ptr->rxn_secondary); - master_ptr->rxn_secondary = rxn_dup(master_ptr->s->rxn_s); + //rxn_free(master_ptr->rxn_secondary); + master_ptr->rxn_secondary = master_ptr->s->rxn_s; /* debug trxn_print (); */ @@ -3717,8 +3717,8 @@ setup_master_rxn(const std::vector &master_ptr_list, const std: if (master_ptr0->s->primary == NULL) { rewrite_master_to_secondary(master_ptr, master_ptr0); - rxn_free(master_ptr->rxn_secondary); - master_ptr->rxn_secondary = rxn_alloc(count_trxn + 1); + //rxn_free(master_ptr->rxn_secondary); + //master_ptr->rxn_secondary = rxn_alloc(count_trxn + 1); trxn_copy(master_ptr->rxn_secondary); /* debug trxn_print (); @@ -4715,7 +4715,7 @@ store_dn(int k, LDBLE * source, int row, LDBLE coef_in, LDBLE * gamma_source) } if (s[k] == s_h2o) return (OK); - for (rxn_ptr = &s[k]->rxn_x->token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) + for (rxn_ptr = &s[k]->rxn_x.token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { if (rxn_ptr->s->secondary != NULL && rxn_ptr->s->secondary->in == TRUE) @@ -4988,11 +4988,12 @@ tidy_redox(void) } else { - struct reaction *rxn = rxn_alloc(count_trxn + 1); + //CReaction rxn = rxn_alloc(count_trxn + 1); + CReaction rxn(count_trxn + 1); trxn_copy(rxn); cxxChemRxn temp_rxn(rxn); it->second = temp_rxn; - rxn_free(rxn); + //rxn_free(rxn); } } } @@ -5017,11 +5018,11 @@ tidy_redox(void) } else { - struct reaction *rxn = rxn_alloc(count_trxn + 1); + CReaction rxn(count_trxn + 1); trxn_copy(rxn); cxxChemRxn temp_rxn(rxn); it->second = temp_rxn; - rxn_free(rxn); + //rxn_free(rxn); } } @@ -5228,7 +5229,7 @@ write_phase_sys_total(int n) /* ---------------------------------------------------------------------- */ LDBLE Phreeqc:: -calc_delta_v(reaction *r_ptr, bool phase) +calc_delta_v(struct reaction *r_ptr, bool phase) /* ---------------------------------------------------------------------- */ { /* calculate delta_v from molar volumes */ @@ -5276,8 +5277,8 @@ calc_lk_phase(phase *p_ptr, LDBLE TK, LDBLE pa) * see calc_vm (below) for details. */ - reaction *r_ptr = (p_ptr->rxn_x ? p_ptr->rxn_x :\ - (p_ptr->rxn_s ? p_ptr->rxn_s : NULL)); + CReaction *r_ptr = (p_ptr->rxn_x.size() ? &p_ptr->rxn_x :\ + (p_ptr->rxn_s.size() ? &p_ptr->rxn_s : NULL)); if (!r_ptr) return 0.0; if (!r_ptr->logk[vm0]) // in case Vm of the phase is 0... @@ -5393,7 +5394,7 @@ calc_vm(LDBLE tc, LDBLE pa) if (s_x[i]->logk[vma1]) { /* supcrt volume at I = 0... */ - s_x[i]->rxn_x->logk[vm_tc] = s_x[i]->logk[vma1] + s_x[i]->logk[vma2] / pb_s + + s_x[i]->rxn_x.logk[vm_tc] = s_x[i]->logk[vma1] + s_x[i]->logk[vma2] / pb_s + (s_x[i]->logk[vma3] + s_x[i]->logk[vma4] / pb_s) / TK_s - s_x[i]->logk[wref] * QBrn; /* A (small) correction by Shock et al., 1992, for 155 < tc < 255, P_sat < P < 1e3. @@ -5403,21 +5404,21 @@ calc_vm(LDBLE tc, LDBLE pa) //{ // LDBLE re = s_x[i]->z * s_x[i]->z / (s_x[i]->logk[wref] / 1.66027e5 + s_x[i]->z / 3.082); // LDBLE Z3 = fabs(pow(s_x[i]->z, 3)) / re / re - s_x[i]->z / 9.498724; - // s_x[i]->rxn_x->logk[vm_tc] += ZBrn * 1.66027e5 * Z3 * dgdP; + // s_x[i]->rxn_x.logk[vm_tc] += ZBrn * 1.66027e5 * Z3 * dgdP; //} if (s_x[i]->z) { /* the ionic strength term * I^0.5... */ if (s_x[i]->logk[b_Av] < 1e-5) - s_x[i]->rxn_x->logk[vm_tc] += s_x[i]->z * s_x[i]->z * 0.5 * DH_Av * sqrt_mu; + s_x[i]->rxn_x.logk[vm_tc] += s_x[i]->z * s_x[i]->z * 0.5 * DH_Av * sqrt_mu; else { /* limit the Debye-Hueckel slope by b... */ /* pitzer... */ - //s_x[i]->rxn_x->logk[vm_tc] += s_x[i]->z * s_x[i]->z * 0.5 * DH_Av * + //s_x[i]->rxn_x.logk[vm_tc] += s_x[i]->z * s_x[i]->z * 0.5 * DH_Av * // log(1 + s_x[i]->logk[b_Av] * sqrt(mu_x)) / s_x[i]->logk[b_Av]; /* extended DH... */ - s_x[i]->rxn_x->logk[vm_tc] += s_x[i]->z * s_x[i]->z * 0.5 * DH_Av * + s_x[i]->rxn_x.logk[vm_tc] += s_x[i]->z * s_x[i]->z * 0.5 * DH_Av * sqrt_mu / (1 + s_x[i]->logk[b_Av] * DH_B * sqrt_mu); } /* plus the volume terms * I... */ @@ -5425,20 +5426,20 @@ calc_vm(LDBLE tc, LDBLE pa) { LDBLE bi = s_x[i]->logk[vmi1] + s_x[i]->logk[vmi2] / TK_s + s_x[i]->logk[vmi3] * TK_s; if (s_x[i]->logk[vmi4] == 1.0) - s_x[i]->rxn_x->logk[vm_tc] += bi * mu_x; + s_x[i]->rxn_x.logk[vm_tc] += bi * mu_x; else - s_x[i]->rxn_x->logk[vm_tc] += bi * pow(mu_x, s_x[i]->logk[vmi4]); + s_x[i]->rxn_x.logk[vm_tc] += bi * pow(mu_x, s_x[i]->logk[vmi4]); } } } else if (s_x[i]->millero[0]) { /* Millero volume at I = 0... */ - s_x[i]->rxn_x->logk[vm_tc] = s_x[i]->millero[0] + tc * (s_x[i]->millero[1] + tc * s_x[i]->millero[2]); + s_x[i]->rxn_x.logk[vm_tc] = s_x[i]->millero[0] + tc * (s_x[i]->millero[1] + tc * s_x[i]->millero[2]); if (s_x[i]->z) { /* the ionic strength terms... */ - s_x[i]->rxn_x->logk[vm_tc] += s_x[i]->z * s_x[i]->z * 0.5 * DH_Av * sqrt_mu + + s_x[i]->rxn_x.logk[vm_tc] += s_x[i]->z * s_x[i]->z * 0.5 * DH_Av * sqrt_mu + (s_x[i]->millero[3] + tc * (s_x[i]->millero[4] + tc * s_x[i]->millero[5])) * mu_x; } } @@ -5446,7 +5447,7 @@ calc_vm(LDBLE tc, LDBLE pa) continue; /* for calculating delta_v of the reaction... */ - s_x[i]->logk[vm_tc] = s_x[i]->rxn_x->logk[vm_tc]; + s_x[i]->logk[vm_tc] = s_x[i]->rxn_x.logk[vm_tc]; } return OK; } @@ -5479,13 +5480,13 @@ k_temp(LDBLE tc, LDBLE pa) /* pa - pressure in atm */ mu_terms_in_logk = false; for (i = 0; i < (int)this->s_x.size(); i++) { - //if (s_x[i]->rxn_x->logk[vm_tc]) + //if (s_x[i]->rxn_x.logk[vm_tc]) /* calculate delta_v for the reaction... */ - s_x[i]->rxn_x->logk[delta_v] = calc_delta_v(s_x[i]->rxn_x, false); - if (tc == current_tc && s_x[i]->rxn_x->logk[delta_v] == 0) + s_x[i]->rxn_x.logk[delta_v] = calc_delta_v(*&s_x[i]->rxn_x, false); + if (tc == current_tc && s_x[i]->rxn_x.logk[delta_v] == 0) continue; mu_terms_in_logk = true; - s_x[i]->lk = k_calc(s_x[i]->rxn_x->logk, tempk, pa * PASCAL_PER_ATM); + s_x[i]->lk = k_calc(s_x[i]->rxn_x.logk, tempk, pa * PASCAL_PER_ATM); } /* * Calculate log k for all pure phases @@ -5495,11 +5496,11 @@ k_temp(LDBLE tc, LDBLE pa) /* pa - pressure in atm */ if (phases[i]->in == TRUE) { - phases[i]->rxn_x->logk[delta_v] = calc_delta_v(phases[i]->rxn_x, true) - + phases[i]->rxn_x.logk[delta_v] = calc_delta_v(*&phases[i]->rxn_x, true) - phases[i]->logk[vm0]; - if (phases[i]->rxn_x->logk[delta_v]) + if (phases[i]->rxn_x.logk[delta_v]) mu_terms_in_logk = true; - phases[i]->lk = k_calc(phases[i]->rxn_x->logk, tempk, pa * PASCAL_PER_ATM); + phases[i]->lk = k_calc(phases[i]->rxn_x.logk, tempk, pa * PASCAL_PER_ATM); } } diff --git a/print.cpp b/print.cpp index 1782292b..b54af8b3 100644 --- a/print.cpp +++ b/print.cpp @@ -648,7 +648,7 @@ print_gas_phase(void) { lp = -phase_ptr->lk; for (rxn_ptr = - &phase_ptr->rxn_x->token[0] + 1; + &phase_ptr->rxn_x.token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { lp += rxn_ptr->s->la * rxn_ptr->coef; @@ -1073,7 +1073,7 @@ print_master_reactions(void) { output_msg(sformatf("%s\t%s\n\tPrimary reaction\n", master[i]->elt->name, master[i]->s->name)); - next_token = master[i]->rxn_primary->token; + next_token = master[i]->rxn_primary.token; for (; next_token->s != NULL; next_token++) { output_msg(sformatf("\t\t%s\t%f\n", next_token->s->name, @@ -1082,7 +1082,7 @@ print_master_reactions(void) output_msg(sformatf("\n\tSecondary reaction:\n")); if (master[i]->rxn_secondary != NULL) { - next_token = master[i]->rxn_secondary->token; + next_token = master[i]->rxn_secondary.token; for (; next_token->s != NULL; next_token++) { output_msg(sformatf("\t\t%s\t%f\n", @@ -1165,37 +1165,37 @@ print_mix(void) output_msg(sformatf("\n")); return (OK); } - -/* ---------------------------------------------------------------------- */ -int Phreeqc:: -print_reaction(struct reaction *rxn_ptr) -/* ---------------------------------------------------------------------- */ -{ -/* - * Debugging print of individual chemical reactions for - * species or phases - */ - int j; - struct rxn_token *next_token; - - if (pr.use == FALSE || pr.all == FALSE) - return (OK); - - output_msg(sformatf("%s\t\n", rxn_ptr->token[0].s->name)); - output_msg(sformatf("\n\tlog k:\n")); - for (j = 0; j < MAX_LOG_K_INDICES; j++) - { - output_msg(sformatf("\t%f", (double) rxn_ptr->logk[j])); - } - output_msg(sformatf("\n\nReaction:\n")); - for (next_token = &rxn_ptr->token[0]; next_token->s != NULL; next_token++) - { - output_msg(sformatf("\t\t%s\t%f\n", next_token->s->name, - (double) next_token->coef)); - } - output_msg(sformatf("\n")); - return (OK); -} +// +///* ---------------------------------------------------------------------- */ +//int Phreeqc:: +//print_reaction(struct reaction *rxn_ptr) +///* ---------------------------------------------------------------------- */ +//{ +///* +// * Debugging print of individual chemical reactions for +// * species or phases +// */ +// int j; +// struct rxn_token *next_token; +// +// if (pr.use == FALSE || pr.all == FALSE) +// return (OK); +// +// output_msg(sformatf("%s\t\n", rxn_ptr->token[0].s->name)); +// output_msg(sformatf("\n\tlog k:\n")); +// for (j = 0; j < MAX_LOG_K_INDICES; j++) +// { +// output_msg(sformatf("\t%f", (double) rxn_ptr->logk[j])); +// } +// output_msg(sformatf("\n\nReaction:\n")); +// for (next_token = &rxn_ptr->token[0]; next_token->s != NULL; next_token++) +// { +// output_msg(sformatf("\t\t%s\t%f\n", next_token->s->name, +// (double) next_token->coef)); +// } +// output_msg(sformatf("\n")); +// return (OK); +//} /* ---------------------------------------------------------------------- */ int Phreeqc:: print_saturation_indices(void) @@ -1209,7 +1209,7 @@ print_saturation_indices(void) LDBLE lk; LDBLE la_eminus; struct rxn_token *rxn_ptr; - struct reaction *reaction_ptr; + CReaction *reaction_ptr; bool gas = true; if (pr.saturation_indices == FALSE || pr.all == FALSE) @@ -1254,13 +1254,13 @@ print_saturation_indices(void) continue; /* check for solids and gases in equation */ if (phases[i]->replaced) - reaction_ptr = phases[i]->rxn_s; + reaction_ptr = &phases[i]->rxn_s; else - reaction_ptr = phases[i]->rxn; + reaction_ptr = &phases[i]->rxn; /* * Print saturation index */ - reaction_ptr->logk[delta_v] = calc_delta_v(reaction_ptr, true) - + reaction_ptr->logk[delta_v] = calc_delta_v(*reaction_ptr, true) - phases[i]->logk[vm0]; if (reaction_ptr->logk[delta_v]) mu_terms_in_logk = true; @@ -1356,7 +1356,7 @@ print_pp_assemblage(void) */ iap = 0.0; phase_ptr = x[j]->phase; - if (x[j]->phase->rxn_x == NULL || phase_ptr->in == FALSE) + if (x[j]->phase->rxn_x.token.size() == 0 || phase_ptr->in == FALSE) { output_msg(sformatf("%-18s%23s", x[j]->phase->name, "Element not present.")); @@ -1364,12 +1364,12 @@ print_pp_assemblage(void) else { phase_ptr = x[j]->phase; - phase_ptr->rxn->logk[delta_v] = calc_delta_v(phase_ptr->rxn, true) - + phase_ptr->rxn.logk[delta_v] = calc_delta_v(*&phase_ptr->rxn, true) - phase_ptr->logk[vm0]; - if (phase_ptr->rxn->logk[delta_v]) + if (phase_ptr->rxn.logk[delta_v]) mu_terms_in_logk = true; - lk = k_calc(phase_ptr->rxn->logk, tk_x, patm_x * PASCAL_PER_ATM); - for (rxn_ptr = &phase_ptr->rxn->token[0] + 1; rxn_ptr->s != NULL; + lk = k_calc(phase_ptr->rxn.logk, tk_x, patm_x * PASCAL_PER_ATM); + for (rxn_ptr = &phase_ptr->rxn.token[0] + 1; rxn_ptr->s != NULL; rxn_ptr++) { if (rxn_ptr->s != s_eminus) @@ -1383,7 +1383,7 @@ print_pp_assemblage(void) } si = -lk + iap; /* - for (rxn_ptr = x[j]->phase->rxn_x->token + 1; rxn_ptr->s != NULL; rxn_ptr++) { + for (rxn_ptr = x[j]->phase->rxn_x.token + 1; rxn_ptr->s != NULL; rxn_ptr++) { iap += rxn_ptr->s->la * rxn_ptr->coef; } si = -x[j]->phase->lk + iap; @@ -3239,7 +3239,7 @@ punch_saturation_indices(void) * Print saturation index */ iap = 0.0; - for (rxn_ptr = &(((struct phase *) current_selected_output->Get_si()[i].second)->rxn_x->token[0]) + 1; + for (rxn_ptr = &(((struct phase *) current_selected_output->Get_si()[i].second)->rxn_x.token[0]) + 1; rxn_ptr->s != NULL; rxn_ptr++) { iap += rxn_ptr->s->la * rxn_ptr->coef; diff --git a/read.cpp b/read.cpp index a2430fd5..d0e7df00 100644 --- a/read.cpp +++ b/read.cpp @@ -753,17 +753,18 @@ read_exchange_species(void) /* * Malloc space for species reaction */ - trxn.token[0].s->rxn = rxn_alloc(count_trxn + 1); + //trxn.token[0].s->rxn = rxn_alloc(count_trxn + 1); /* * Copy reaction to reaction for species */ - token_ptr = &trxn.token[0].s->rxn->token[0]; - for (i = 0; i < count_trxn; i++) - { - token_ptr[i].s = trxn.token[i].s; - token_ptr[i].coef = trxn.token[i].coef; - } - token_ptr[i].s = NULL; + trxn_copy(trxn.token[0].s->rxn); + //token_ptr = &trxn.token[0].s->rxn.token[0]; + //for (i = 0; i < count_trxn; i++) + //{ + // token_ptr[i].s = trxn.token[i].s; + // token_ptr[i].coef = trxn.token[i].coef; + //} + //token_ptr[i].s = NULL; /* * Set type for species */ @@ -793,7 +794,7 @@ read_exchange_species(void) phase_ptr->check_equation = FALSE; phase_ptr->type = EX; phase_ptr->next_elt = elt_list_dup(s_ptr->next_elt); - phase_ptr->rxn = rxn_dup(s_ptr->rxn); + phase_ptr->rxn = s_ptr->rxn; } break; } @@ -3711,25 +3712,26 @@ read_phases(void) /* * Malloc space for phase reaction */ - phase_ptr->rxn = rxn_alloc(count_trxn + 1); + //phase_ptr->rxn = rxn_alloc(count_trxn + 1); /* * Copy reaction to reaction for phase, first token (token[0]) is not used * except to check that coef of phase formula = 1.0 */ - token_ptr = &phase_ptr->rxn->token[0]; - /* token_ptr[0].coef=0; */ - token_ptr[0].coef = trxn.token[0].coef; - token_ptr[0].s = trxn.token[1].s; - for (i = 1; i < count_trxn; i++) - { - token_ptr[i].name = NULL; - token_ptr[i].s = trxn.token[i].s; - token_ptr[i].coef = trxn.token[i].coef; - if (token_ptr[i].s == NULL) - { - token_ptr[i].name = trxn.token[i].name; - } - } + trxn_copy(phase_ptr->rxn); + token_ptr = &phase_ptr->rxn.token[0]; + ///* token_ptr[0].coef=0; */ + //token_ptr[0].coef = trxn.token[0].coef; + //token_ptr[0].s = trxn.token[1].s; + //for (i = 1; i < count_trxn; i++) + //{ + // token_ptr[i].name = NULL; + // token_ptr[i].s = trxn.token[i].s; + // token_ptr[i].coef = trxn.token[i].coef; + // if (token_ptr[i].s == NULL) + // { + // token_ptr[i].name = trxn.token[i].name; + // } + //} token_ptr[0].name = trxn.token[1].name; /* token_ptr[0].name=phase_ptr->name; @@ -5627,7 +5629,7 @@ read_species(void) /* * Malloc space for species reaction */ - trxn.token[0].s->rxn = rxn_alloc(count_trxn + 1); + //trxn.token[0].s->rxn = rxn_alloc(count_trxn + 1); /* * Copy reaction to reaction for species */ @@ -6188,7 +6190,7 @@ read_surface_species(void) s_ptr->dz[2] = s_ptr->cd_music[2]; for (j = 0; j < 3; j++) { - s_ptr->rxn->dz[j] = s_ptr->dz[j]; + s_ptr->rxn.dz[j] = s_ptr->dz[j]; } opt_save = OPTION_DEFAULT; break; @@ -6250,17 +6252,18 @@ read_surface_species(void) /* * Malloc space for species reaction */ - trxn.token[0].s->rxn = rxn_alloc(count_trxn + 1); + //trxn.token[0].s->rxn = rxn_alloc(count_trxn + 1); /* * Copy reaction to reaction for species */ - token_ptr = &trxn.token[0].s->rxn->token[0]; - for (i = 0; i < count_trxn; i++) - { - token_ptr[i].s = trxn.token[i].s; - token_ptr[i].coef = trxn.token[i].coef; - } - token_ptr[i].s = NULL; + trxn_copy(trxn.token[0].s->rxn); + //token_ptr = &trxn.token[0].s->rxn.token[0]; + //for (i = 0; i < count_trxn; i++) + //{ + // token_ptr[i].s = trxn.token[i].s; + // token_ptr[i].coef = trxn.token[i].coef; + //} + //token_ptr[i].s = NULL; /* * Set type for species */ @@ -7063,21 +7066,22 @@ add_psi_master_species(char *token) master[count_master]->s->next_elt = elt_list_save(); master[count_master]->s->type = plane; master[count_master]->primary = TRUE; - master[count_master]->s->rxn = rxn_alloc(3); + + master[count_master]->s->rxn.token.resize(3); /* * Define reaction for psi */ for (i = 0; i < MAX_LOG_K_INDICES; i++) { - master[count_master]->s->rxn->logk[i] = 0.0; + master[count_master]->s->rxn.logk[i] = 0.0; } - master[count_master]->s->rxn->token[0].s = + master[count_master]->s->rxn.token[0].s = master[count_master]->s; - master[count_master]->s->rxn->token[0].coef = -1.0; - master[count_master]->s->rxn->token[1].s = + master[count_master]->s->rxn.token[0].coef = -1.0; + master[count_master]->s->rxn.token[1].s = master[count_master]->s; - master[count_master]->s->rxn->token[1].coef = 1.0; - master[count_master]->s->rxn->token[2].s = NULL; + master[count_master]->s->rxn.token[1].coef = 1.0; + master[count_master]->s->rxn.token[2].s = NULL; count_master++; } } diff --git a/sit.cpp b/sit.cpp index 2838eac6..b9214296 100644 --- a/sit.cpp +++ b/sit.cpp @@ -1256,12 +1256,12 @@ gammas_sit() * Find moles of sites. * s_x[i]->equiv is stoichiometric coefficient of sites in species */ - for (j = 1; s_x[i]->rxn_x->token[j].s != NULL; j++) + for (j = 1; s_x[i]->rxn_x.token[j].s != NULL; j++) { - if (s_x[i]->rxn_x->token[j].s->type == SURF) + if (s_x[i]->rxn_x.token[j].s->type == SURF) { s_x[i]->alk = - s_x[i]->rxn_x->token[j].s->primary->unknown->moles; + s_x[i]->rxn_x.token[j].s->primary->unknown->moles; break; } } @@ -1321,12 +1321,12 @@ gammas_sit() * z contains valence of cation for exchange species, alk contains cec */ /* !!!!! */ - for (j = 1; s_x[i]->rxn_x->token[j].s != NULL; j++) + for (j = 1; s_x[i]->rxn_x.token[j].s != NULL; j++) { - if (s_x[i]->rxn_x->token[j].s->type == EX) + if (s_x[i]->rxn_x.token[j].s->type == EX) { s_x[i]->alk = - s_x[i]->rxn_x->token[j].s->primary->unknown-> + s_x[i]->rxn_x.token[j].s->primary->unknown-> moles; break; } @@ -1352,13 +1352,13 @@ gammas_sit() if (use.Get_exchange_ptr()->Get_pitzer_exchange_gammas()) { /* Assume equal gamma's of solute and exchangeable species... */ - for (j = 1; s_x[i]->rxn_x->token[j].s != NULL; j++) + for (j = 1; s_x[i]->rxn_x.token[j].s != NULL; j++) { - if (s_x[i]->rxn_x->token[j].s->type == EX) + if (s_x[i]->rxn_x.token[j].s->type == EX) continue; - coef = s_x[i]->rxn_x->token[j].coef; - s_x[i]->lg += coef * s_x[i]->rxn_x->token[j].s->lg; - s_x[i]->dg += coef * s_x[i]->rxn_x->token[j].s->dg; + coef = s_x[i]->rxn_x.token[j].coef; + s_x[i]->lg += coef * s_x[i]->rxn_x.token[j].s->lg; + s_x[i]->dg += coef * s_x[i]->rxn_x.token[j].s->dg; } } } diff --git a/structures.cpp b/structures.cpp index 2fdb7c4a..2d10d978 100644 --- a/structures.cpp +++ b/structures.cpp @@ -736,8 +736,8 @@ master_alloc(void) ptr->gfw_formula = NULL; ptr->unknown = NULL; ptr->s = NULL; - ptr->rxn_primary = NULL; - ptr->rxn_secondary = NULL; + //ptr->rxn_primary = NULL; + //ptr->rxn_secondary = NULL; ptr->pe_rxn = NULL; ptr->minor_isotope = FALSE; return (ptr); @@ -778,8 +778,8 @@ master_free(struct master *master_ptr) */ if (master_ptr == NULL) return (ERROR); - rxn_free(master_ptr->rxn_primary); - rxn_free(master_ptr->rxn_secondary); + //rxn_free(master_ptr->rxn_primary); + //rxn_free(master_ptr->rxn_secondary); delete master_ptr; return (OK); } @@ -1061,9 +1061,9 @@ phase_free(struct phase *phase_ptr) (struct elt_list *) free_check_null(phase_ptr->next_elt); phase_ptr->next_sys_total = (struct elt_list *) free_check_null(phase_ptr->next_sys_total); - rxn_free(phase_ptr->rxn); - rxn_free(phase_ptr->rxn_s); - rxn_free(phase_ptr->rxn_x); + //rxn_free(phase_ptr->rxn); + //rxn_free(phase_ptr->rxn_s); + //rxn_free(phase_ptr->rxn_x); phase_ptr->add_logk.clear(); return (OK); } @@ -1159,9 +1159,9 @@ phase_init(struct phase *phase_ptr) phase_ptr->next_elt = NULL; phase_ptr->next_sys_total = NULL; phase_ptr->check_equation = TRUE; - phase_ptr->rxn = NULL; - phase_ptr->rxn_s = NULL; - phase_ptr->rxn_x = NULL; + //phase_ptr->rxn = NULL; + //phase_ptr->rxn_s = NULL; + //phase_ptr->rxn_x = NULL; phase_ptr->replaced = 0; phase_ptr->in_system = 1; phase_ptr->original_deltav_units = cm3_per_mol; @@ -1407,7 +1407,7 @@ rate_sort(void) * * ********************************************************************** */ /* ---------------------------------------------------------------------- */ -struct reaction * Phreeqc:: +struct reaction Phreeqc:: rxn_alloc(int ntokens) /* ---------------------------------------------------------------------- */ { @@ -1417,11 +1417,11 @@ rxn_alloc(int ntokens) * input: ntokens, number of tokens in reaction * return: pointer to a species structure */ - struct reaction *rxn_ptr; + struct reaction rxn, * rxn_ptr; /* * Malloc reaction structure */ - rxn_ptr = new struct reaction; + rxn_ptr = &rxn; /* * zero log k data */ @@ -1447,12 +1447,12 @@ rxn_alloc(int ntokens) rxn_ptr->token[i].name = NULL; rxn_ptr->token[i].coef = 0.0; } - return (rxn_ptr); + return (rxn); } /* ---------------------------------------------------------------------- */ -struct reaction * Phreeqc:: -rxn_dup(struct reaction *rxn_ptr_old) +struct reaction Phreeqc:: +rxn_dup(struct reaction& rxn_ptr_old) /* ---------------------------------------------------------------------- */ { /* @@ -1461,32 +1461,31 @@ rxn_dup(struct reaction *rxn_ptr_old) * * Return: rxn_ptr_new, pointer to duplicated structure to copy */ - int i; - struct reaction *rxn_ptr_new; + struct reaction rxn_ptr_new; - if (rxn_ptr_old == NULL) - return (NULL); - for (i = 0; rxn_ptr_old->token[i].s != NULL; i++); + //if (rxn_ptr_old == NULL) + // return (NULL); + //for (i = 0; rxn_ptr_old.token[i].s != NULL; i++); - rxn_ptr_new = rxn_alloc(i + 1); + //rxn_ptr_new = rxn_alloc(i + 1); /* * Copy logk data */ - memcpy(rxn_ptr_new->logk, rxn_ptr_old->logk, (size_t) MAX_LOG_K_INDICES * sizeof(LDBLE)); + //memcpy(rxn_ptr_new->logk, rxn_ptr_old->logk, (size_t) MAX_LOG_K_INDICES * sizeof(LDBLE)); /* * Copy dz data */ - memcpy(rxn_ptr_new->dz, rxn_ptr_old->dz, (size_t) (3 * sizeof(LDBLE))); + //memcpy(rxn_ptr_new->dz, rxn_ptr_old->dz, (size_t) (3 * sizeof(LDBLE))); /* * Copy tokens */ - memcpy(&rxn_ptr_new->token[0], &rxn_ptr_old->token[0], - ((size_t)i + 1) * sizeof(struct rxn_token)); + //memcpy(&rxn_ptr_new->token[0], &rxn_ptr_old->token[0], + // ((size_t)i + 1) * sizeof(struct rxn_token)); - return (rxn_ptr_new); + return (rxn_ptr_old); } /* ---------------------------------------------------------------------- */ -struct reaction * Phreeqc:: +struct reaction Phreeqc:: cxxChemRxn2rxn(cxxChemRxn &cr) /* ---------------------------------------------------------------------- */ { @@ -1521,7 +1520,8 @@ cxxChemRxn2rxn(cxxChemRxn &cr) count_trxn = 0; trxn_add(cr, 1.0, 1); - struct reaction *rxn_ptr_new = rxn_alloc(count_trxn + 1); + struct reaction rxn_new = rxn_alloc(count_trxn + 1); + struct reaction* rxn_ptr_new = &rxn_new; trxn_copy(rxn_ptr_new); // cleanup pointers for copy operator name, and s may point into another instance @@ -1532,7 +1532,7 @@ cxxChemRxn2rxn(cxxChemRxn &cr) LDBLE z = rxn_ptr_new->token[i].s->z; rxn_ptr_new->token[i].s = s_store(rxn_ptr_new->token[i].name, z, false); } - return (rxn_ptr_new); + return (rxn_new); } /* ---------------------------------------------------------------------- */ LDBLE Phreeqc:: @@ -1695,9 +1695,9 @@ s_free(struct species *s_ptr) s_ptr->next_sys_total = (struct elt_list *) free_check_null(s_ptr->next_sys_total); s_ptr->add_logk.clear(); - rxn_free(s_ptr->rxn); - rxn_free(s_ptr->rxn_s); - rxn_free(s_ptr->rxn_x); + //rxn_free(s_ptr->rxn); + //rxn_free(s_ptr->rxn_s); + //rxn_free(s_ptr->rxn_x); return (OK); } @@ -1766,9 +1766,9 @@ s_init(struct species *s_ptr) s_ptr->next_secondary = NULL; s_ptr->next_sys_total = NULL; s_ptr->check_equation = TRUE; - s_ptr->rxn = NULL; - s_ptr->rxn_s = NULL; - s_ptr->rxn_x = NULL; + //s_ptr->rxn = NULL; + //s_ptr->rxn_s = NULL; + //s_ptr->rxn_x = NULL; s_ptr->tot_g_moles = 0; s_ptr->tot_dh2o_moles = 0; for (i = 0; i < 5; i++) @@ -2402,6 +2402,7 @@ trxn_copy(struct reaction *rxn_ptr) /* * Copy logk data */ + rxn_ptr->token.resize(count_trxn + 1); for (i = 0; i < MAX_LOG_K_INDICES; i++) { rxn_ptr->logk[i] = trxn.logk[i]; @@ -2564,8 +2565,9 @@ trxn_sort(void) if (count_trxn - 1 > 1) { qsort(&trxn.token[1], - (size_t) count_trxn - 1, - (size_t) sizeof(struct rxn_token_temp), rxn_token_temp_compare); + (size_t)count_trxn - 1, + sizeof(struct rxn_token_temp), + rxn_token_temp_compare); } return (OK); } diff --git a/tidy.cpp b/tidy.cpp index ec39a57c..1e80eb00 100644 --- a/tidy.cpp +++ b/tidy.cpp @@ -464,7 +464,7 @@ check_species_input(void) s[i]->name); error_msg(error_string, CONTINUE); } - if (s[i]->rxn == NULL) + if (s[i]->rxn.token.size() == 0) { input_error++; return_value = ERROR; @@ -475,8 +475,8 @@ check_species_input(void) } else { - select_log_k_expression(s[i]->logk, s[i]->rxn->logk); - add_other_logk(s[i]->rxn->logk, s[i]->add_logk); + select_log_k_expression(s[i]->logk, s[i]->rxn.logk); + add_other_logk(s[i]->rxn.logk, s[i]->add_logk); } } return (return_value); @@ -814,9 +814,9 @@ replace_solids_gases(void) trxn_add_phase(phase_ptr->rxn, coef, FALSE); /* remove solid/gas from trxn list */ - trxn.token[i].name = phase_ptr->rxn->token[0].name; - trxn.token[i].s = phase_ptr->rxn->token[0].s; - trxn.token[i].coef = -coef * phase_ptr->rxn->token[0].coef; + trxn.token[i].name = phase_ptr->rxn.token[0].name; + trxn.token[i].s = phase_ptr->rxn.token[0].s; + trxn.token[i].coef = -coef * phase_ptr->rxn.token[0].coef; repeat = TRUE; replaced = TRUE; /* debug @@ -1458,10 +1458,10 @@ tidy_phases(void) */ for (i = 0; i < (int)phases.size(); i++) { - select_log_k_expression(phases[i]->logk, phases[i]->rxn->logk); - add_other_logk(phases[i]->rxn->logk, phases[i]->add_logk); - phases[i]->rxn->token[0].name = phases[i]->name; - phases[i]->rxn->token[0].s = NULL; + select_log_k_expression(phases[i]->logk, phases[i]->rxn.logk); + add_other_logk(phases[i]->rxn.logk, phases[i]->add_logk); + phases[i]->rxn.token[0].name = phases[i]->name; + phases[i]->rxn.token[0].s = NULL; } /* * Rewrite all phases to secondary species @@ -1490,8 +1490,8 @@ tidy_phases(void) trxn_reverse_k(); rewrite_eqn_to_secondary(); trxn_reverse_k(); - rxn_free(phases[i]->rxn_s); - phases[i]->rxn_s = rxn_alloc(count_trxn + 1); + //rxn_free(phases[i].rxn_s); + //phases[i]->rxn_s(count_trxn + 1); trxn_copy(phases[i]->rxn_s); /* * Check equation @@ -2353,8 +2353,8 @@ tidy_species(void) trxn_add(master[i]->s->rxn, 1.0, FALSE); rewrite_eqn_to_primary(); } - rxn_free(master[i]->rxn_primary); - master[i]->rxn_primary = rxn_alloc(count_trxn + 1); + //rxn_free(master[i]->rxn_primary); + //master[i]->rxn_primary = rxn_alloc(count_trxn + 1); trxn_copy(master[i]->rxn_primary); master[i]->coef = coef_in_master(master[i]); } @@ -2374,8 +2374,8 @@ tidy_species(void) trxn_add(s[i]->rxn, 1.0, FALSE); rewrite_eqn_to_secondary(); } - rxn_free(s[i]->rxn_s); - s[i]->rxn_s = rxn_alloc(count_trxn + 1); + //rxn_free(s[i].rxn_s); + //s[i].rxn_s = rxn_alloc(count_trxn + 1); trxn_copy(s[i]->rxn_s); /* calculate alkalinity */ s[i]->alk = calc_alk(s[i]->rxn_s); @@ -2516,11 +2516,11 @@ tidy_species(void) * Changed to be coefficient of exchanger */ LDBLE exchange_coef = 0.0; - for (j = 1; s[i]->rxn_s->token[j].s != NULL; j++) + for (j = 1; s[i]->rxn_s.token[j].s != NULL; j++) { - if (s[i]->rxn_s->token[j].s->type == EX) + if (s[i]->rxn_s.token[j].s->type == EX) { - exchange_coef = s[i]->rxn_s->token[j].coef; + exchange_coef = s[i]->rxn_s.token[j].coef; break; } } @@ -2540,11 +2540,11 @@ tidy_species(void) /* * Find coefficient of surface in rxn, store in equiv */ - for (j = 1; s[i]->rxn_s->token[j].s != NULL; j++) + for (j = 1; s[i]->rxn_s.token[j].s != NULL; j++) { - if (s[i]->rxn_s->token[j].s->type == SURF) + if (s[i]->rxn_s.token[j].s->type == SURF) { - surface_coef = s[i]->rxn_s->token[j].coef; + surface_coef = s[i]->rxn_s.token[j].coef; break; } } @@ -2833,13 +2833,13 @@ species_rxn_to_trxn(struct species *s_ptr) */ int i; - for (i = 0; s_ptr->rxn->token[i].s != NULL; i++) + for (i = 0; s_ptr->rxn.token[i].s != NULL; i++) { - trxn.token[i].name = s_ptr->rxn->token[i].s->name; - trxn.token[i].z = s_ptr->rxn->token[i].s->z; - trxn.token[i].s = s_ptr->rxn->token[i].s; + trxn.token[i].name = s_ptr->rxn.token[i].s->name; + trxn.token[i].z = s_ptr->rxn.token[i].s->z; + trxn.token[i].s = s_ptr->rxn.token[i].s; trxn.token[i].unknown = NULL; - trxn.token[i].coef = s_ptr->rxn->token[i].coef; + trxn.token[i].coef = s_ptr->rxn.token[i].coef; count_trxn = i + 1; if (count_trxn + 1 > trxn.token.size()) trxn.token.resize(count_trxn + 1); @@ -2872,7 +2872,7 @@ phase_rxn_to_trxn(struct phase *phase_ptr, struct reaction *rxn_ptr) trxn.token[0].unknown = NULL; /*trxn.token[0].coef = -1.0; */ /* check for leading coefficient of 1.0 for phase did not work */ - trxn.token[0].coef = phase_ptr->rxn->token[0].coef; + trxn.token[0].coef = phase_ptr->rxn.token[0].coef; for (i = 1; rxn_ptr->token[i].s != NULL; i++) { trxn.token[i].name = rxn_ptr->token[i].s->name; @@ -4452,8 +4452,8 @@ ss_prep(LDBLE t, cxxSS *ss_ptr, int print) cxxSScomp *comp1_ptr = &(ss_ptr->Get_ss_comps()[1]); struct phase *phase0_ptr = phase_bsearch(comp0_ptr->Get_name().c_str(), &k, FALSE); struct phase *phase1_ptr = phase_bsearch(comp1_ptr->Get_name().c_str(), &k, FALSE); - kc = exp(k_calc(phase0_ptr->rxn->logk, t, REF_PRES_PASCAL) * LOG_10); - kb = exp(k_calc(phase1_ptr->rxn->logk, t, REF_PRES_PASCAL) * LOG_10); + kc = exp(k_calc(phase0_ptr->rxn.logk, t, REF_PRES_PASCAL) * LOG_10); + kb = exp(k_calc(phase1_ptr->rxn.logk, t, REF_PRES_PASCAL) * LOG_10); crit_pt = fabs(a0) + fabs(a1); /* * Default, no miscibility or spinodal gaps @@ -5072,9 +5072,9 @@ ss_calc_a0_a1(cxxSS *ss_ptr) error_msg(error_string, CONTINUE); return (ERROR); } - l_kc = exp(k_calc(phase0_ptr->rxn->logk, ss_ptr->Get_tk(), REF_PRES_PASCAL) * + l_kc = exp(k_calc(phase0_ptr->rxn.logk, ss_ptr->Get_tk(), REF_PRES_PASCAL) * LOG_10); - l_kb = exp(k_calc(phase1_ptr->rxn->logk, ss_ptr->Get_tk(), REF_PRES_PASCAL) * + l_kb = exp(k_calc(phase1_ptr->rxn.logk, ss_ptr->Get_tk(), REF_PRES_PASCAL) * LOG_10); p = ss_ptr->Get_p(); diff --git a/transport.cpp b/transport.cpp index fce12792..bc7e9368 100644 --- a/transport.cpp +++ b/transport.cpp @@ -1953,9 +1953,9 @@ fill_spec(int l_cell_no, int ref_cell) } /* find the aqueous species in the exchange reaction... */ - for (i2 = 0; (s_ptr->rxn->token[i2].s != NULL); i2++) + for (i2 = 0; (s_ptr->rxn.token[i2].s != NULL); i2++) { - if ((s_ptr2 = s_ptr->rxn->token[i2].s)->type == AQ) + if ((s_ptr2 = s_ptr->rxn.token[i2].s)->type == AQ) break; } /* copy its name and Dw and charge... */ @@ -6052,7 +6052,7 @@ calc_vm_Cl(void) { /* limit the Debye-Hueckel slope by b... */ /* pitzer... */ - //s_ptr->rxn_x->logk[vm_tc] += s_ptr->z * s_ptr->z * 0.5 * DH_Av * + //s_ptr->rxn_x.logk[vm_tc] += s_ptr->z * s_ptr->z * 0.5 * DH_Av * // log(1 + s_ptr->logk[b_Av] * sqrt(mu_x)) / s_ptr->logk[b_Av]; /* extended DH... */ V_Cl += s_ptr->z * s_ptr->z * 0.5 * DH_Av * From ce6472019611d4946e397be150f33fe9717a8aaf Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Thu, 1 Apr 2021 18:30:59 -0600 Subject: [PATCH 33/53] cleaned up, removed struct reaction --- Phreeqc.h | 16 -- basicsubs.cpp | 2 +- global_structures.h | 28 --- prep.cpp | 84 +-------- print.cpp | 31 ---- read.cpp | 43 ----- structures.cpp | 410 -------------------------------------------- tidy.cpp | 77 ++------- utilities.cpp | 31 ---- 9 files changed, 20 insertions(+), 702 deletions(-) diff --git a/Phreeqc.h b/Phreeqc.h index d0198f88..d3d896c8 100644 --- a/Phreeqc.h +++ b/Phreeqc.h @@ -554,7 +554,6 @@ public: int build_species_list(int n); int build_min_surface(void); LDBLE calc_lk_phase(phase* p_ptr, LDBLE TK, LDBLE pa); - LDBLE calc_delta_v(struct reaction* r_ptr, bool phase); double calc_delta_v(CReaction& r_ref, bool phase); LDBLE calc_PR(std::vector phase_ptrs, LDBLE P, LDBLE TK, LDBLE V_m); LDBLE calc_PR(); @@ -617,7 +616,6 @@ public: int print_exchange(void); int print_gas_phase(void); int print_master_reactions(void); - //int print_reaction(struct reaction* rxn_ptr); int print_species(void); int print_surface(void); int print_user_print(void); @@ -868,12 +866,6 @@ public: struct rate* rate_copy(struct rate* rate_ptr); struct rate* rate_search(const char* name, int* n); int rate_sort(void); - struct reaction rxn_alloc(int ntokens); - struct reaction rxn_dup(struct reaction& rxn_ptr_old); - struct reaction cxxChemRxn2rxn(cxxChemRxn& cr); - LDBLE rxn_find_coef(struct reaction* r_ptr, const char* str); - int rxn_free(struct reaction* rxn_ptr); - int rxn_print(struct reaction* rxn_ptr); static int s_compare(const void* ptr1, const void* ptr2); int s_delete(int i); struct species* s_search(const char* name); @@ -887,11 +879,8 @@ protected: public: struct master* surface_get_psi_master(const char* name, int plane); int system_duplicate(int i, int save_old); - int trxn_add(struct reaction* r_ptr, LDBLE coef, int combine); int trxn_add(cxxChemRxn& r_ptr, LDBLE coef, int combine); - int trxn_add_phase(struct reaction* r_ptr, LDBLE coef, int combine); int trxn_combine(void); - int trxn_copy(struct reaction* rxn_ptr); LDBLE trxn_find_coef(const char* str, int start); int trxn_print(void); int trxn_reverse_k(void); @@ -976,8 +965,6 @@ public: int tidy_model(void); int check_species_input(void); LDBLE coef_in_master(struct master* master_ptr); - int phase_rxn_to_trxn(struct phase* phase_ptr, - struct reaction* rxn_ptr); int reset_last_model(void); int rewrite_eqn_to_primary(void); int rewrite_eqn_to_secondary(void); @@ -1048,9 +1035,6 @@ public: int add_elt_list(struct elt_list* elt_list_ptr, LDBLE coef); int add_elt_list_multi_surf(struct elt_list* elt_list_ptr, LDBLE coef, struct element* surf_elt_ptr); int add_elt_list(const cxxNameDouble& nd, LDBLE coef); -protected: - LDBLE calc_alk(struct reaction* rxn_ptr); -public: LDBLE calc_rho_0(LDBLE tc, LDBLE pa); LDBLE calc_dielectrics(LDBLE tc, LDBLE pa); int compute_gfw(const char* string, LDBLE* gfw); diff --git a/basicsubs.cpp b/basicsubs.cpp index c1bf8274..0dd6ff51 100644 --- a/basicsubs.cpp +++ b/basicsubs.cpp @@ -831,7 +831,7 @@ calc_surface_charge(const char *surface_name) * Match surface_name */ count_trxn = 0; - trxn_add(s_x[k]->rxn_s, 1.0, FALSE); /* rxn_s is set in tidy_model */ + trxn_add(s_x[k]->rxn_s, 1.0, false); /* rxn_s is set in tidy_model */ for (i = 1; i < count_trxn; i++) { token_ptr = &(trxn.token[i]); diff --git a/global_structures.h b/global_structures.h index b89f3729..d36d6818 100644 --- a/global_structures.h +++ b/global_structures.h @@ -409,15 +409,6 @@ struct elt_list struct element *elt; /* pointer to element structure */ LDBLE coef; /* number of element e's in eqn */ }; -/*---------------------------------------------------------------------- - * Reaction - *---------------------------------------------------------------------- */ -struct reaction -{ - LDBLE logk[MAX_LOG_K_INDICES]; - LDBLE dz[3]; - std::vector token; -}; struct rxn_token { struct species *s; @@ -439,25 +430,6 @@ public: dz[i] =0.0; } } - cxxChemRxn(struct reaction* rxn_ptr) - { - logk[0] = dz[0] = 0.0; - for (size_t i = 0; i < MAX_LOG_K_INDICES; i++) - { - logk[i] = rxn_ptr->logk[i]; - } - for (size_t i = 0; i < 3; i++) - { - dz[i] = rxn_ptr->dz[i]; - } - struct rxn_token *next_token; - next_token = &rxn_ptr->token[0]; - this->tokens.push_back(*next_token++); - while (next_token->s != NULL || next_token->name != NULL) - { - this->tokens.push_back(*next_token++); - } - } cxxChemRxn(CReaction& rxn_ref) { logk[0] = dz[0] = 0.0; diff --git a/prep.cpp b/prep.cpp index 583515b5..7051d304 100644 --- a/prep.cpp +++ b/prep.cpp @@ -1114,7 +1114,7 @@ build_model(void) continue; s[i]->in = FALSE; count_trxn = 0; - trxn_add(s[i]->rxn_s, 1.0, FALSE); /* rxn_s is set in tidy_model */ + trxn_add(s[i]->rxn_s, 1.0, false); /* rxn_s is set in tidy_model */ /* * Check if species is in model */ @@ -1143,8 +1143,6 @@ build_model(void) add_potential_factor(); add_cd_music_factors(i); } - //rxn_free(s[i]->rxn_x); - //s[i]->rxn_x = rxn_alloc(count_trxn + 1); trxn_copy(s[i]->rxn_x); for (j = 0; j < 3; j++) { @@ -1160,7 +1158,7 @@ build_model(void) * Determine mass balance equations, build sums for mass balance, build sums for jacobian */ count_trxn = 0; - trxn_add(s[i]->rxn_s, 1.0, FALSE); + trxn_add(s[i]->rxn_s, 1.0, false); if (s[i]->next_secondary == NULL) { write_mb_eqn_x(); @@ -1286,7 +1284,7 @@ build_model(void) for (i = 0; i < (int)phases.size(); i++) { count_trxn = 0; - trxn_add_phase(phases[i]->rxn_s, 1.0, FALSE); + trxn_add_phase(phases[i]->rxn_s, 1.0, false); trxn_reverse_k(); phases[i]->in = inout(); if (phases[i]->in == TRUE) @@ -1304,13 +1302,6 @@ build_model(void) */ write_mass_action_eqn_x(STOP); trxn_reverse_k(); - //rxn_free(phases[i]->rxn_x); - //if (debug_prep == TRUE) - //{ - // output_msg(sformatf( "\nPhase: %s\n", phases[i]->name)); - // trxn_print(); - //} - //phases[i]->rxn_x = rxn_alloc(count_trxn + 1); trxn_copy(phases[i]->rxn_x); write_phase_sys_total(i); } @@ -1741,7 +1732,6 @@ clear(void) /* * copy primary reaction to secondary reaction */ - //rxn_free(master[i]->rxn_secondary); master[i]->rxn_secondary = master[i]->rxn_primary; } @@ -2518,7 +2508,6 @@ reprep(void) { if (master[i]->in == FALSE) continue; - //rxn_free(master[i]->rxn_secondary); master[i]->rxn_secondary = master[i]->rxn_primary; } resetup_master(); @@ -2578,7 +2567,6 @@ resetup_master(void) { if (master_ptr->s->primary == NULL) { - //rxn_free(master_ptr->rxn_secondary); master_ptr->rxn_secondary = master_ptr->s->rxn_s; } } @@ -2587,8 +2575,6 @@ resetup_master(void) if (master_ptr0->s->primary == NULL) { rewrite_master_to_secondary(master_ptr, master_ptr0); - //rxn_free(master_ptr->rxn_secondary); - //master_ptr->rxn_secondary = rxn_alloc(count_trxn + 1); trxn_copy(master_ptr->rxn_secondary); } } @@ -2653,7 +2639,7 @@ write_mass_action_eqn_x(int stop) rxn_find_coef(trxn.token[i].s->secondary->rxn_secondary, "e-"); trxn_add(trxn.token[i].s->secondary->rxn_secondary, - trxn.token[i].coef, FALSE); + trxn.token[i].coef, false); if (equal(coef_e, 0.0, TOL) == FALSE) { std::map < std::string, cxxChemRxn >::iterator chemRxnIt = pe_x.find(trxn.token[i].s->secondary->pe_rxn); @@ -3105,8 +3091,8 @@ rewrite_master_to_secondary(struct master *master_ptr1, * Rewrite equation to secondary master species */ count_trxn = 0; - trxn_add(master_ptr1->rxn_primary, 1.0, FALSE); - trxn_add(master_ptr2->rxn_primary, -coef1 / coef2, TRUE); + trxn_add(master_ptr1->rxn_primary, 1.0, false); + trxn_add(master_ptr2->rxn_primary, -coef1 / coef2, true); return (OK); } /* ---------------------------------------------------------------------- */ @@ -3704,11 +3690,7 @@ setup_master_rxn(const std::vector &master_ptr_list, const std: master_ptr->in = TRUE; if (master_ptr->s->primary == NULL) { - //rxn_free(master_ptr->rxn_secondary); master_ptr->rxn_secondary = master_ptr->s->rxn_s; -/* debug - trxn_print (); - */ } } else @@ -3717,12 +3699,7 @@ setup_master_rxn(const std::vector &master_ptr_list, const std: if (master_ptr0->s->primary == NULL) { rewrite_master_to_secondary(master_ptr, master_ptr0); - //rxn_free(master_ptr->rxn_secondary); - //master_ptr->rxn_secondary = rxn_alloc(count_trxn + 1); trxn_copy(master_ptr->rxn_secondary); -/* debug - trxn_print (); - */ } } master_ptr->pe_rxn = string_hsave(pe_rxn.c_str()); @@ -4988,12 +4965,10 @@ tidy_redox(void) } else { - //CReaction rxn = rxn_alloc(count_trxn + 1); CReaction rxn(count_trxn + 1); trxn_copy(rxn); cxxChemRxn temp_rxn(rxn); it->second = temp_rxn; - //rxn_free(rxn); } } } @@ -5070,7 +5045,7 @@ write_mb_eqn_x(void) { repeat = TRUE; trxn_add(trxn.token[i].s->secondary->rxn_secondary, - trxn.token[i].coef, FALSE); + trxn.token[i].coef, false); } } trxn_combine(); @@ -5130,7 +5105,7 @@ write_mb_for_species_list(int n) * Start with secondary reaction */ count_trxn = 0; - trxn_add(s[n]->rxn_s, 1.0, FALSE); + trxn_add(s[n]->rxn_s, 1.0, false); /* * Copy to elt_list */ @@ -5188,7 +5163,7 @@ write_phase_sys_total(int n) * Start with secondary reaction */ count_trxn = 0; - trxn_add_phase(phases[n]->rxn_s, 1.0, FALSE); + trxn_add_phase(phases[n]->rxn_s, 1.0, false); /* * Copy to elt_list */ @@ -5226,47 +5201,6 @@ write_phase_sys_total(int n) phases[n]->next_sys_total = elt_list_save(); return (OK); } - -/* ---------------------------------------------------------------------- */ -LDBLE Phreeqc:: -calc_delta_v(struct reaction *r_ptr, bool phase) -/* ---------------------------------------------------------------------- */ -{ -/* calculate delta_v from molar volumes */ -//dlp - LDBLE d_v = 0.0; - - if (phase) - { - /* for phases: reactants have coef's < 0, products have coef's > 0, v.v. for species */ - for (size_t i = 1; r_ptr->token[i].s /*|| r_ptr->token[i].s*/ ; i++) - { - //if (!r_ptr->token[i].s) - // continue; - //if (!strcmp(r_ptr->token[i].s->name, "H+")) - // continue; - //if (!strcmp(r_ptr->token[i].s->name, "e-")) - // continue; - //else if (r_ptr->token[i].s->logk[vm_tc]) - d_v += r_ptr->token[i].coef * r_ptr->token[i].s->logk[vm_tc]; - } - } - else - { - for (size_t i = 0; r_ptr->token[i].name /*|| r_ptr->token[i].s*/ ; i++) - { - if (!r_ptr->token[i].s) - continue; - //if (!strcmp(r_ptr->token[i].s->name, "H+")) - // continue; - //if (!strcmp(r_ptr->token[i].s->name, "e-")) - // continue; - //else if (r_ptr->token[i].s->logk[vm_tc]) - d_v -= r_ptr->token[i].coef * r_ptr->token[i].s->logk[vm_tc]; - } - } - return d_v; -} /* ---------------------------------------------------------------------- */ LDBLE Phreeqc:: calc_lk_phase(phase *p_ptr, LDBLE TK, LDBLE pa) diff --git a/print.cpp b/print.cpp index b54af8b3..0ddedefa 100644 --- a/print.cpp +++ b/print.cpp @@ -1165,37 +1165,6 @@ print_mix(void) output_msg(sformatf("\n")); return (OK); } -// -///* ---------------------------------------------------------------------- */ -//int Phreeqc:: -//print_reaction(struct reaction *rxn_ptr) -///* ---------------------------------------------------------------------- */ -//{ -///* -// * Debugging print of individual chemical reactions for -// * species or phases -// */ -// int j; -// struct rxn_token *next_token; -// -// if (pr.use == FALSE || pr.all == FALSE) -// return (OK); -// -// output_msg(sformatf("%s\t\n", rxn_ptr->token[0].s->name)); -// output_msg(sformatf("\n\tlog k:\n")); -// for (j = 0; j < MAX_LOG_K_INDICES; j++) -// { -// output_msg(sformatf("\t%f", (double) rxn_ptr->logk[j])); -// } -// output_msg(sformatf("\n\nReaction:\n")); -// for (next_token = &rxn_ptr->token[0]; next_token->s != NULL; next_token++) -// { -// output_msg(sformatf("\t\t%s\t%f\n", next_token->s->name, -// (double) next_token->coef)); -// } -// output_msg(sformatf("\n")); -// return (OK); -//} /* ---------------------------------------------------------------------- */ int Phreeqc:: print_saturation_indices(void) diff --git a/read.cpp b/read.cpp index d0e7df00..c6e004e9 100644 --- a/read.cpp +++ b/read.cpp @@ -750,21 +750,10 @@ read_exchange_species(void) trxn.token[0].s->o = next_elt->coef; } } -/* - * Malloc space for species reaction - */ - //trxn.token[0].s->rxn = rxn_alloc(count_trxn + 1); /* * Copy reaction to reaction for species */ trxn_copy(trxn.token[0].s->rxn); - //token_ptr = &trxn.token[0].s->rxn.token[0]; - //for (i = 0; i < count_trxn; i++) - //{ - // token_ptr[i].s = trxn.token[i].s; - // token_ptr[i].coef = trxn.token[i].coef; - //} - //token_ptr[i].s = NULL; /* * Set type for species */ @@ -3719,24 +3708,7 @@ read_phases(void) */ trxn_copy(phase_ptr->rxn); token_ptr = &phase_ptr->rxn.token[0]; - ///* token_ptr[0].coef=0; */ - //token_ptr[0].coef = trxn.token[0].coef; - //token_ptr[0].s = trxn.token[1].s; - //for (i = 1; i < count_trxn; i++) - //{ - // token_ptr[i].name = NULL; - // token_ptr[i].s = trxn.token[i].s; - // token_ptr[i].coef = trxn.token[i].coef; - // if (token_ptr[i].s == NULL) - // { - // token_ptr[i].name = trxn.token[i].name; - // } - //} token_ptr[0].name = trxn.token[1].name; - /* - token_ptr[0].name=phase_ptr->name; - token_ptr[0].s=NULL; - */ token_ptr[i].s = NULL; token_ptr[i].name = NULL; /* @@ -5626,10 +5598,6 @@ read_species(void) trxn.token[0].s->o = next_elt->coef; } } -/* - * Malloc space for species reaction - */ - //trxn.token[0].s->rxn = rxn_alloc(count_trxn + 1); /* * Copy reaction to reaction for species */ @@ -6249,21 +6217,10 @@ read_surface_species(void) trxn.token[0].s->o = next_elt->coef; } } - /* - * Malloc space for species reaction - */ - //trxn.token[0].s->rxn = rxn_alloc(count_trxn + 1); /* * Copy reaction to reaction for species */ trxn_copy(trxn.token[0].s->rxn); - //token_ptr = &trxn.token[0].s->rxn.token[0]; - //for (i = 0; i < count_trxn; i++) - //{ - // token_ptr[i].s = trxn.token[i].s; - // token_ptr[i].coef = trxn.token[i].coef; - //} - //token_ptr[i].s = NULL; /* * Set type for species */ diff --git a/structures.cpp b/structures.cpp index 2d10d978..2adef0ad 100644 --- a/structures.cpp +++ b/structures.cpp @@ -736,8 +736,6 @@ master_alloc(void) ptr->gfw_formula = NULL; ptr->unknown = NULL; ptr->s = NULL; - //ptr->rxn_primary = NULL; - //ptr->rxn_secondary = NULL; ptr->pe_rxn = NULL; ptr->minor_isotope = FALSE; return (ptr); @@ -778,8 +776,6 @@ master_free(struct master *master_ptr) */ if (master_ptr == NULL) return (ERROR); - //rxn_free(master_ptr->rxn_primary); - //rxn_free(master_ptr->rxn_secondary); delete master_ptr; return (OK); } @@ -1061,9 +1057,6 @@ phase_free(struct phase *phase_ptr) (struct elt_list *) free_check_null(phase_ptr->next_elt); phase_ptr->next_sys_total = (struct elt_list *) free_check_null(phase_ptr->next_sys_total); - //rxn_free(phase_ptr->rxn); - //rxn_free(phase_ptr->rxn_s); - //rxn_free(phase_ptr->rxn_x); phase_ptr->add_logk.clear(); return (OK); } @@ -1159,9 +1152,6 @@ phase_init(struct phase *phase_ptr) phase_ptr->next_elt = NULL; phase_ptr->next_sys_total = NULL; phase_ptr->check_equation = TRUE; - //phase_ptr->rxn = NULL; - //phase_ptr->rxn_s = NULL; - //phase_ptr->rxn_x = NULL; phase_ptr->replaced = 0; phase_ptr->in_system = 1; phase_ptr->original_deltav_units = cm3_per_mol; @@ -1400,234 +1390,6 @@ rate_sort(void) } return (OK); } - -/* ********************************************************************** - * - * Routines related to structure "reaction", balanced chemical reactions - * - * ********************************************************************** */ -/* ---------------------------------------------------------------------- */ -struct reaction Phreeqc:: -rxn_alloc(int ntokens) -/* ---------------------------------------------------------------------- */ -{ - int i; -/* - * Allocates space to a rxn structure - * input: ntokens, number of tokens in reaction - * return: pointer to a species structure - */ - struct reaction rxn, * rxn_ptr; -/* - * Malloc reaction structure - */ - rxn_ptr = &rxn; -/* - * zero log k data - */ - for (i = 0; i < MAX_LOG_K_INDICES; i++) - { - rxn_ptr->logk[i] = 0.0; - } -/* - * zero dz data - */ - for (i = 0; i < 3; i++) - { - rxn_ptr->dz[i] = 0.0; - } -/* - * Malloc rxn_token structure - */ - rxn_ptr->token.clear(); - rxn_ptr->token.resize((size_t)ntokens); - for (i = 0; i < ntokens; i++) - { - rxn_ptr->token[i].s = NULL; - rxn_ptr->token[i].name = NULL; - rxn_ptr->token[i].coef = 0.0; - } - return (rxn); -} - -/* ---------------------------------------------------------------------- */ -struct reaction Phreeqc:: -rxn_dup(struct reaction& rxn_ptr_old) -/* ---------------------------------------------------------------------- */ -{ -/* - * mallocs space for a reaction and copies the reaction - * input: rxn_ptr_old, pointer to a reaction structure to copy - * - * Return: rxn_ptr_new, pointer to duplicated structure to copy - */ - struct reaction rxn_ptr_new; - - //if (rxn_ptr_old == NULL) - // return (NULL); - //for (i = 0; rxn_ptr_old.token[i].s != NULL; i++); - - //rxn_ptr_new = rxn_alloc(i + 1); -/* - * Copy logk data - */ - //memcpy(rxn_ptr_new->logk, rxn_ptr_old->logk, (size_t) MAX_LOG_K_INDICES * sizeof(LDBLE)); -/* - * Copy dz data - */ - //memcpy(rxn_ptr_new->dz, rxn_ptr_old->dz, (size_t) (3 * sizeof(LDBLE))); -/* - * Copy tokens - */ - //memcpy(&rxn_ptr_new->token[0], &rxn_ptr_old->token[0], - // ((size_t)i + 1) * sizeof(struct rxn_token)); - - return (rxn_ptr_old); -} -/* ---------------------------------------------------------------------- */ -struct reaction Phreeqc:: -cxxChemRxn2rxn(cxxChemRxn &cr) -/* ---------------------------------------------------------------------- */ -{ -/* - * mallocs space for a reaction and copies the cxxChemRxn to a struct reaction - * - * Return: rxn_ptr_new, pointer to new structure - */ - for (int i = 0; i < (int) cr.Get_tokens().size(); i++) - { - if (cr.Get_tokens()[i].s != NULL) - { - cr.Get_tokens()[i].s = s_store(cr.Get_tokens()[i].s->name, cr.Get_tokens()[i].s->z, FALSE); - } - if (cr.Get_tokens()[i].name != NULL) - { - cr.Get_tokens()[i].name = string_hsave(cr.Get_tokens()[i].name); - } - else - { - if (cr.Get_tokens()[i].s != NULL) - { - cr.Get_tokens()[i].name = string_hsave(cr.Get_tokens()[i].s->name); - } - else - { - cr.Get_tokens()[i].name=NULL; - } - } - } - - count_trxn = 0; - trxn_add(cr, 1.0, 1); - - struct reaction rxn_new = rxn_alloc(count_trxn + 1); - struct reaction* rxn_ptr_new = &rxn_new; - trxn_copy(rxn_ptr_new); - - // cleanup pointers for copy operator name, and s may point into another instance - - for (int i = 0; rxn_ptr_new->token[i].s != NULL; i++) - { - rxn_ptr_new->token[i].name = string_hsave(rxn_ptr_new->token[i].name); - LDBLE z = rxn_ptr_new->token[i].s->z; - rxn_ptr_new->token[i].s = s_store(rxn_ptr_new->token[i].name, z, false); - } - return (rxn_new); -} -/* ---------------------------------------------------------------------- */ -LDBLE Phreeqc:: -rxn_find_coef(struct reaction * r_ptr, const char *str) -/* ---------------------------------------------------------------------- */ -{ -/* - * Finds coefficient of token in reaction. - * input: r_ptr, pointer to a reaction structure - * str, string to find as reaction token - * - * Return: 0.0, if token not found - * coefficient of token, if found. - */ - struct rxn_token *r_token; - LDBLE coef; - - r_token = &r_ptr->token[0] + 1; - coef = 0.0; - while (r_token->s != NULL) - { - if (strcmp(r_token->s->name, str) == 0) - { - coef = r_token->coef; - break; - } - r_token++; - } - return (coef); -} - -/* ---------------------------------------------------------------------- */ -int Phreeqc:: -rxn_free(struct reaction *rxn_ptr) -/* ---------------------------------------------------------------------- */ -{ -/* - * Frees space allocated for a reaction structure - * input: rxn_ptr, pointer to reaction structure - * return: ERROR, if pointer is NULL - * OK, otherwise. - */ - if (rxn_ptr == NULL) - return (ERROR); - rxn_ptr->token.clear(); - delete rxn_ptr; - return (OK); -} - -/* ---------------------------------------------------------------------- */ -int Phreeqc:: -rxn_print(struct reaction *rxn_ptr) -/* ---------------------------------------------------------------------- */ -{ -/* - * Frees space allocated for a reaction structure - * input: rxn_ptr, pointer to reaction structure - * return: ERROR, if pointer is NULL - * OK, otherwise. - */ - struct rxn_token *next_token; - int i; - if (rxn_ptr == NULL) - return (ERROR); - next_token = &rxn_ptr->token[0]; - output_msg(sformatf( "log k data:\n")); - for (i = 0; i < MAX_LOG_K_INDICES; i++) - { - output_msg(sformatf( "\t%f\n", (double) rxn_ptr->logk[i])); - } - output_msg(sformatf( "Reaction definition\n")); - while (next_token->s != NULL || next_token->name != NULL) - { - output_msg(sformatf( "\tcoef %f ", next_token->coef)); - if (next_token->s != NULL) - { - output_msg(sformatf( "\tspecies token: %s ", - next_token->s->name)); - } - if (next_token->name != NULL) - { - output_msg(sformatf( "\tname token: %s", next_token->name)); - } - output_msg(sformatf( "\n")); - next_token++; - } - output_msg(sformatf( "dz data\n")); - for (i = 0; i < 3; i++) - { - output_msg(sformatf( "\t%d %e\n", i, (double) rxn_ptr->dz[i])); - - } - return (OK); -} - /* ********************************************************************** * * Routines related to structure "species" @@ -1766,9 +1528,6 @@ s_init(struct species *s_ptr) s_ptr->next_secondary = NULL; s_ptr->next_sys_total = NULL; s_ptr->check_equation = TRUE; - //s_ptr->rxn = NULL; - //s_ptr->rxn_s = NULL; - //s_ptr->rxn_x = NULL; s_ptr->tot_g_moles = 0; s_ptr->tot_dh2o_moles = 0; for (i = 0; i < 5; i++) @@ -2196,133 +1955,6 @@ trxn_add(cxxChemRxn &r_ptr, LDBLE coef, int combine) trxn_combine(); return (OK); } - -/* ---------------------------------------------------------------------- */ -int Phreeqc:: -trxn_add(struct reaction *r_ptr, LDBLE coef, int combine) -/* ---------------------------------------------------------------------- */ -{ -/* - * Adds reactions together. - * - * Global variable count_trxn determines which position in trxn is used. - * If count_trxn=0, then the equation effectively is copied into trxn. - * If count_trxn>0, then new equation is added to existing equation. - * - * Arguments: - * *r_ptr points to rxn structure to add. - * - * coef added equation is multiplied by coef. - * combine if TRUE, reaction is reaction is sorted and - * like terms combined. - */ - int i; - struct rxn_token *next_token; -/* - * Accumulate log k for reaction - */ - if (count_trxn == 0) - { - memcpy((void *) trxn.logk, (void *) r_ptr->logk, - (size_t) MAX_LOG_K_INDICES * sizeof(LDBLE)); - for (i = 0; i < 3; i++) - { - trxn.dz[i] = r_ptr->dz[i]; - } - } - else - { - for (i = 0; i < MAX_LOG_K_INDICES; i++) - { - trxn.logk[i] += coef * (r_ptr->logk[i]); - } - for (i = 0; i < 3; i++) - { - trxn.dz[i] += coef * r_ptr->dz[i]; - } - } -/* - * Copy equation into work space - */ - next_token = &r_ptr->token[0]; - while (next_token->s != NULL) - { - if (count_trxn + 1 > trxn.token.size()) - trxn.token.resize(count_trxn + 1); - trxn.token[count_trxn].name = next_token->s->name; - trxn.token[count_trxn].s = next_token->s; - trxn.token[count_trxn].coef = coef * next_token->coef; - count_trxn++; - next_token++; - } - if (combine == TRUE) - trxn_combine(); - return (OK); -} - -/* ---------------------------------------------------------------------- */ -int Phreeqc:: -trxn_add_phase(struct reaction *r_ptr, LDBLE coef, int combine) -/* ---------------------------------------------------------------------- */ -{ -/* - * Adds reactions together. - * - * Global variable count_trxn determines which position in trxn is used. - * If count_trxn=0, then the equation effectively is copied into trxn. - * If count_trxn>0, then new equation is added to existing equation. - * - * Arguments: - * *r_ptr points to rxn structure to add. - * - * coef added equation is multiplied by coef. - * combine if TRUE, reaction is reaction is sorted and - * like terms combined. - */ - int i; - struct rxn_token *next_token; -/* - * Accumulate log k for reaction - */ - if (count_trxn == 0) - { - memcpy((void *) trxn.logk, (void *) r_ptr->logk, - (size_t) MAX_LOG_K_INDICES * sizeof(LDBLE)); - } - else - { - for (i = 0; i < MAX_LOG_K_INDICES; i++) - { - trxn.logk[i] += coef * (r_ptr->logk[i]); - } - } -/* - * Copy equation into work space - */ - next_token = &r_ptr->token[0]; - while (next_token->s != NULL || next_token->name != NULL) - { - if (count_trxn + 1 > trxn.token.size()) - trxn.token.resize(count_trxn + 1); - if (next_token->s != NULL) - { - trxn.token[count_trxn].name = next_token->s->name; - trxn.token[count_trxn].s = next_token->s; - } - else - { - trxn.token[count_trxn].name = next_token->name; - trxn.token[count_trxn].s = NULL; - } - trxn.token[count_trxn].coef = coef * next_token->coef; - count_trxn++; - next_token++; - } - if (combine == TRUE) - trxn_combine(); - return (OK); -} - /* ---------------------------------------------------------------------- */ int Phreeqc:: trxn_combine(void) @@ -2386,48 +2018,6 @@ trxn_combine(void) count_trxn = j + 1; /* number excluding final NULL */ return (OK); } - -/* ---------------------------------------------------------------------- */ -int Phreeqc:: -trxn_copy(struct reaction *rxn_ptr) -/* ---------------------------------------------------------------------- */ -{ -/* - * Copies trxn to a reaction structure. - * - * Input: rxn_ptr, pointer to reaction structure to copy trxn to. - * - */ - int i; -/* - * Copy logk data - */ - rxn_ptr->token.resize(count_trxn + 1); - for (i = 0; i < MAX_LOG_K_INDICES; i++) - { - rxn_ptr->logk[i] = trxn.logk[i]; - } -/* - * Copy dz data - */ - for (i = 0; i < 3; i++) - { - rxn_ptr->dz[i] = trxn.dz[i]; - } -/* - * Copy tokens - */ - for (i = 0; i < count_trxn; i++) - { - rxn_ptr->token[i].s = trxn.token[i].s; - rxn_ptr->token[i].name = trxn.token[i].name; - rxn_ptr->token[i].coef = trxn.token[i].coef; - } - rxn_ptr->token[count_trxn].s = NULL; - - return (OK); -} - /* ---------------------------------------------------------------------- */ LDBLE Phreeqc:: trxn_find_coef(const char *str, int start) diff --git a/tidy.cpp b/tidy.cpp index 1e80eb00..4165a823 100644 --- a/tidy.cpp +++ b/tidy.cpp @@ -732,7 +732,7 @@ rewrite_eqn_to_secondary(void) && token_ptr->s->primary == NULL) { coef = token_ptr->coef; - trxn_add(token_ptr->s->rxn, coef, TRUE); + trxn_add(token_ptr->s->rxn, coef, true); repeat = TRUE; break; } @@ -811,7 +811,7 @@ replace_solids_gases(void) output_msg(sformatf( "Reaction to add.\n")); rxn_print(phase_ptr->rxn); */ - trxn_add_phase(phase_ptr->rxn, coef, FALSE); + trxn_add_phase(phase_ptr->rxn, coef, false); /* remove solid/gas from trxn list */ trxn.token[i].name = phase_ptr->rxn.token[0].name; @@ -881,7 +881,7 @@ rewrite_eqn_to_primary(void) { if (trxn.token[j].s->primary == NULL) { - trxn_add(trxn.token[j].s->rxn, trxn.token[j].coef, TRUE); + trxn_add(trxn.token[j].s->rxn, trxn.token[j].coef, true); repeat = TRUE; break; } @@ -1472,7 +1472,7 @@ tidy_phases(void) * Rewrite equation */ count_trxn = 0; - trxn_add_phase(phases[i]->rxn, 1.0, FALSE); + trxn_add_phase(phases[i]->rxn, 1.0, false); trxn.token[0].name = phases[i]->name; /* debug output_msg(sformatf( "%s PHASE.\n", phases[i]->name)); @@ -1480,18 +1480,10 @@ tidy_phases(void) */ replaced = replace_solids_gases(); phases[i]->replaced = replaced; - /* save rxn */ - /* - rxn_free(phases[i]->rxn); - phases[i]->rxn = rxn_alloc(count_trxn + 1); - trxn_copy(phases[i]->rxn); - */ /* save rxn_s */ trxn_reverse_k(); rewrite_eqn_to_secondary(); trxn_reverse_k(); - //rxn_free(phases[i].rxn_s); - //phases[i]->rxn_s(count_trxn + 1); trxn_copy(phases[i]->rxn_s); /* * Check equation @@ -2230,14 +2222,6 @@ tidy_punch(void) } } fpunchf_heading("\n"); - //if (punch.user_punch == TRUE) - //{ - // for (i = 0; i < user_punch_count_headings; i++) - // { - // fpunchf_heading(sformatf("%*s\t", l, user_punch_headings[i])); - // } - //} - //fpunchf_heading("\n"); current_selected_output->Set_new_def(false); pr.punch = punch_save; @@ -2345,16 +2329,14 @@ tidy_species(void) count_trxn = 0; if (master[i]->s->primary != NULL) { - trxn_add(master[i]->s->rxn, 1.0, FALSE); - trxn_add(master[i]->s->rxn, -1.0, TRUE); + trxn_add(master[i]->s->rxn, 1.0, false); + trxn_add(master[i]->s->rxn, -1.0, true); } else { - trxn_add(master[i]->s->rxn, 1.0, FALSE); + trxn_add(master[i]->s->rxn, 1.0, false); rewrite_eqn_to_primary(); } - //rxn_free(master[i]->rxn_primary); - //master[i]->rxn_primary = rxn_alloc(count_trxn + 1); trxn_copy(master[i]->rxn_primary); master[i]->coef = coef_in_master(master[i]); } @@ -2366,12 +2348,12 @@ tidy_species(void) count_trxn = 0; if (s[i]->primary != NULL || s[i]->secondary != NULL) { - trxn_add(s[i]->rxn, 1.0, FALSE); - trxn_add(s[i]->rxn, -1.0, TRUE); + trxn_add(s[i]->rxn, 1.0, false); + trxn_add(s[i]->rxn, -1.0, true); } else { - trxn_add(s[i]->rxn, 1.0, FALSE); + trxn_add(s[i]->rxn, 1.0, false); rewrite_eqn_to_secondary(); } //rxn_free(s[i].rxn_s); @@ -2847,45 +2829,6 @@ species_rxn_to_trxn(struct species *s_ptr) return (OK); } -/* ---------------------------------------------------------------------- */ -int Phreeqc:: -phase_rxn_to_trxn(struct phase *phase_ptr, struct reaction *rxn_ptr) -/* ---------------------------------------------------------------------- */ -{ -/* - * Copy reaction from reaction structure to - * temp reaction structure. - */ - int i, l; - const char* cptr; - LDBLE l_z; - - trxn.token[0].name = phase_ptr->formula; - /* charge */ - cptr = phase_ptr->formula; - { - std::string token; - get_token(&cptr, token, &l_z, &l); - } - trxn.token[0].z = l_z; - trxn.token[0].s = NULL; - trxn.token[0].unknown = NULL; - /*trxn.token[0].coef = -1.0; */ - /* check for leading coefficient of 1.0 for phase did not work */ - trxn.token[0].coef = phase_ptr->rxn.token[0].coef; - for (i = 1; rxn_ptr->token[i].s != NULL; i++) - { - trxn.token[i].name = rxn_ptr->token[i].s->name; - trxn.token[i].z = rxn_ptr->token[i].s->z; - trxn.token[i].s = NULL; - trxn.token[i].unknown = NULL; - trxn.token[i].coef = rxn_ptr->token[i].coef; - count_trxn = i + 1; - if (count_trxn + 1 > trxn.token.size()) - trxn.token.resize(count_trxn + 1); - } - return (OK); -} /* ---------------------------------------------------------------------- */ int Phreeqc:: tidy_isotopes(void) diff --git a/utilities.cpp b/utilities.cpp index 7264d331..1f008b01 100644 --- a/utilities.cpp +++ b/utilities.cpp @@ -123,37 +123,6 @@ add_elt_list(const cxxNameDouble & nd, LDBLE coef) return (OK); } -/* ---------------------------------------------------------------------- */ -LDBLE Phreeqc:: -calc_alk(struct reaction * rxn_ptr) -/* ---------------------------------------------------------------------- */ -{ - int i; - LDBLE return_value; - struct master *master_ptr; - - return_value = 0.0; - for (i = 1; rxn_ptr->token[i].s != NULL; i++) - { - master_ptr = rxn_ptr->token[i].s->secondary; - if (master_ptr == NULL) - { - master_ptr = rxn_ptr->token[i].s->primary; - } - if (master_ptr == NULL) - { - error_string = sformatf( - "Non-master species in secondary reaction, %s.", - rxn_ptr->token[0].s->name); - error_msg(error_string, CONTINUE); - input_error++; - break; - } - return_value += rxn_ptr->token[i].coef * master_ptr->alk; - } - return (return_value); -} - /* ---------------------------------------------------------------------- */ LDBLE Phreeqc:: calc_rho_0(LDBLE tc, LDBLE pa) From d2e3a4e5b6b49fa3ae5338fcc072f66fd60fa39c Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Thu, 1 Apr 2021 20:24:32 -0600 Subject: [PATCH 34/53] Removed cxxChemRxn --- CReaction.cpp | 1 - Classes.cpp | 17 +++++++++++ ISolution.cxx | 2 +- ISolution.h | 8 ++---- Phreeqc.cpp | 69 +++++++-------------------------------------- Phreeqc.h | 4 +-- global_structures.h | 59 -------------------------------------- prep.cpp | 21 +++++++------- print.cpp | 2 +- read.cpp | 4 +-- spread.cpp | 6 ++-- structures.cpp | 60 --------------------------------------- 12 files changed, 49 insertions(+), 204 deletions(-) diff --git a/CReaction.cpp b/CReaction.cpp index 2c8f325b..8ddbd29c 100644 --- a/CReaction.cpp +++ b/CReaction.cpp @@ -1,5 +1,4 @@ #include "global_structures.h" -#include "Phreeqc.h" #include "CReaction.h" CReaction::CReaction(void) { diff --git a/Classes.cpp b/Classes.cpp index bdbadd05..ad4559ae 100644 --- a/Classes.cpp +++ b/Classes.cpp @@ -277,3 +277,20 @@ calc_alk(CReaction& rxn_ref) } return (return_value); } +CReaction Phreeqc::CReaction_internal_copy(CReaction& rxn_ref) +{ + CReaction rxn; + for (size_t i = 0; i < MAX_LOG_K_INDICES; i++) rxn.logk[i] = rxn_ref.logk[i]; + for (size_t i = 0; i < 3; i++) rxn.dz[i] = rxn_ref.dz[i]; + rxn.Get_tokens().resize(rxn_ref.Get_tokens().size()); + for (size_t i = 0; i < rxn_ref.Get_tokens().size(); i++) + { + rxn.token[i].s = (rxn_ref.token[i].s == NULL) ? NULL : + s_search(rxn_ref.token[i].s->name); + rxn.token[i].coef = rxn_ref.token[i].coef; + rxn.token[i].name = (rxn_ref.token[i].s == NULL) ? NULL : + string_hsave(rxn_ref.token[i].name); + } + return rxn; +} + diff --git a/ISolution.cxx b/ISolution.cxx index 6e771005..05aa2842 100644 --- a/ISolution.cxx +++ b/ISolution.cxx @@ -23,7 +23,7 @@ cxxISolution::cxxISolution(PHRQ_io *io) units("mMol/kgw") { default_pe = "pe"; - cxxChemRxn temp_pe_reactions; + CReaction temp_pe_reactions; pe_reactions[default_pe] = temp_pe_reactions; this->calc_density = false; diff --git a/ISolution.h b/ISolution.h index 1c7de94e..10fb7160 100644 --- a/ISolution.h +++ b/ISolution.h @@ -38,17 +38,15 @@ class cxxISolution: public PHRQ_base std::map < std::string, cxxISolutionComp > &Get_comps(void) {return this->comps;} const std::map < std::string, cxxISolutionComp > &Get_comps(void)const {return this->comps;} void Set_comps(std::map < std::string, cxxISolutionComp > &c) {this->comps = c;} - std::map < std::string, cxxChemRxn > &Get_pe_reactions(void) {return this->pe_reactions;} - void Set_pe_reactions(std::map < std::string, cxxChemRxn > &pe) {this->pe_reactions = pe;} - //void dump_xml(std::ostream& os, unsigned int indent = 0)const; - //void ConvertUnits(Phreeqc * phreeqc_ptr); + std::map& Get_pe_reactions(void) { return this->pe_reactions; } + void Set_pe_reactions(std::map < std::string, CReaction >& pe) { this->pe_reactions = pe; } protected: friend class cxxISolutionComp; // for this->pe access std::string units; bool calc_density; std::map < std::string, cxxISolutionComp > comps; - std::map pe_reactions; + std::map pe_reactions; const char * default_pe; }; diff --git a/Phreeqc.cpp b/Phreeqc.cpp index 00bb641d..b543aa5d 100644 --- a/Phreeqc.cpp +++ b/Phreeqc.cpp @@ -1538,29 +1538,10 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) cxxNameDouble next_sys_total(pSrc->s[i]->next_sys_total); s_ptr->next_sys_total = NameDouble2elt_list(next_sys_total); } -// NEEDS REVISION - ////rxn - //s_ptr->rxn = NULL; - //if (pSrc->s[i]->rxn != NULL) - //{ - // cxxChemRxn rxn(pSrc->s[i]->rxn); - // s_ptr->rxn = cxxChemRxn2rxn(rxn); - // //s_ptr->rxn = rxn_copy_operator(pSrc->s[i]->rxn); - //} - ////rxn_s - //s_ptr->rxn_s = NULL; - //if (pSrc->s[i]->rxn_s != NULL) - //{ - // cxxChemRxn rxn_s(pSrc->s[i]->rxn_s); - // s_ptr->rxn_s = cxxChemRxn2rxn(rxn_s); - //} - ////rxn_x - //s_ptr->rxn_x = NULL; - //if (pSrc->s[i]->rxn_x != NULL) - //{ - // cxxChemRxn rxn_x(pSrc->s[i]->rxn_x); - // s_ptr->rxn_x = cxxChemRxn2rxn(rxn_x); - //} + //rxn + s_ptr->rxn = CReaction_internal_copy(pSrc->s[i]->rxn); + s_ptr->rxn_s = CReaction_internal_copy(pSrc->s[i]->rxn_s); + s_ptr->rxn_x = CReaction_internal_copy(pSrc->s[i]->rxn_x); } s_h2o = s_search("H2O"); s_hplus = s_search("H+"); @@ -1599,28 +1580,10 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) cxxNameDouble next_sys_total(pSrc->phases[i]->next_sys_total); phase_ptr->next_sys_total = NameDouble2elt_list(next_sys_total); } -// NEEDS REVISION //rxn - //phase_ptr->rxn = NULL; - //if (pSrc->phases[i]->rxn != NULL) - //{ - // cxxChemRxn rxn(pSrc->phases[i]->rxn); - // phase_ptr->rxn = cxxChemRxn2rxn(rxn); - //} - ////rxn_s - ////phase_ptr->rxn_s = NULL; - //if (pSrc->phases[i]->rxn_s != NULL) - //{ - // cxxChemRxn rxn_s(pSrc->phases[i]->rxn_s); - // phase_ptr->rxn_s = cxxChemRxn2rxn(rxn_s); - //} - ////rxn_x - ////phase_ptr->rxn_x = NULL; - //if (pSrc->phases[i]->rxn_x != NULL) - //{ - // cxxChemRxn rxn_x(pSrc->phases[i]->rxn_x); - // phase_ptr->rxn_x = cxxChemRxn2rxn(rxn_x); - //} + phase_ptr->rxn = CReaction_internal_copy(pSrc->phases[i]->rxn); + phase_ptr->rxn_s = CReaction_internal_copy(pSrc->phases[i]->rxn_s); + phase_ptr->rxn_x = CReaction_internal_copy(pSrc->phases[i]->rxn_x); } /*---------------------------------------------------------------------- * Master species @@ -1645,21 +1608,9 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) master[i]->elt = element_store(pSrc->master[i]->elt->name); master[i]->unknown = NULL; master[i]->s = s_store(pSrc->master[i]->s->name, pSrc->master[i]->s->z, false); -// NEEDS REVISION - ////rxn_primary - //master[i]->rxn_primary = NULL; - //if (pSrc->master[i]->rxn_primary != NULL) - //{ - // cxxChemRxn rxn_primary(pSrc->master[i]->rxn_primary); - // master[i]->rxn_primary = cxxChemRxn2rxn(rxn_primary); - //} - ////rxn_secondary - //master[i]->rxn_secondary = NULL; - //if (pSrc->master[i]->rxn_secondary != NULL) - //{ - // cxxChemRxn rxn_secondary(pSrc->master[i]->rxn_secondary); - // master[i]->rxn_secondary = cxxChemRxn2rxn(rxn_secondary); - //} + //rxn_primary + master[i]->rxn_primary = CReaction_internal_copy(pSrc->master[i]->rxn_primary); + master[i]->rxn_secondary = CReaction_internal_copy(pSrc->master[i]->rxn_secondary); } /*---------------------------------------------------------------------- * Unknowns diff --git a/Phreeqc.h b/Phreeqc.h index d3d896c8..0552cdca 100644 --- a/Phreeqc.h +++ b/Phreeqc.h @@ -879,7 +879,6 @@ protected: public: struct master* surface_get_psi_master(const char* name, int plane); int system_duplicate(int i, int save_old); - int trxn_add(cxxChemRxn& r_ptr, LDBLE coef, int combine); int trxn_combine(void); LDBLE trxn_find_coef(const char* str, int start); int trxn_print(void); @@ -892,6 +891,7 @@ public: double rxn_find_coef(CReaction& r_ptr, const char* str); bool phase_rxn_to_trxn(struct phase* phase_ptr, CReaction& rxn_ptr); double calc_alk(CReaction& rxn_ptr); + CReaction CReaction_internal_copy(CReaction& rxn_ref); struct unknown* unknown_alloc(void); int unknown_delete(int i); int unknown_free(struct unknown* unknown_ptr); @@ -1300,7 +1300,7 @@ protected: LDBLE mass_water_surfaces_x; LDBLE mass_water_bulk_x; std::string units_x; - std::map < std::string, cxxChemRxn > pe_x; + std::map < std::string, CReaction > pe_x; std::map isotopes_x; std::string default_pe_x; cxxSurface::DIFFUSE_LAYER_TYPE dl_type_x; diff --git a/global_structures.h b/global_structures.h index d36d6818..beae8377 100644 --- a/global_structures.h +++ b/global_structures.h @@ -415,66 +415,7 @@ struct rxn_token LDBLE coef; const char *name; }; -class cxxChemRxn -{ -public: - cxxChemRxn(void) - { - logk[0] = dz[0] = 0.0; - for (size_t i = 0; i < MAX_LOG_K_INDICES; i++) - { - logk[i] = 0.0; - } - for (size_t i = 0; i < 3; i++) - { - dz[i] =0.0; - } - } - cxxChemRxn(CReaction& rxn_ref) - { - logk[0] = dz[0] = 0.0; - for (size_t i = 0; i < MAX_LOG_K_INDICES; i++) - { - logk[i] = rxn_ref.logk[i]; - } - for (size_t i = 0; i < 3; i++) - { - dz[i] = rxn_ref.dz[i]; - } - struct rxn_token* next_token; - next_token = &rxn_ref.token[0]; - this->tokens.push_back(*next_token++); - while (next_token->s != NULL || next_token->name != NULL) - { - this->tokens.push_back(*next_token++); - } - } - ~cxxChemRxn(void) {} - LDBLE *Get_logk(void) {return this->logk;} - void Set_logk(LDBLE *d) - { - for (size_t i = 0; i < MAX_LOG_K_INDICES; i++) - { - logk[i] = d[i]; - } - } - LDBLE *Get_dz(void) {return this->dz;} - void Set_dz(LDBLE *d) - { - for (size_t i = 0; i < 3; i++) - { - dz[i] = d[i]; - } - } - std::vector &Get_tokens(void) {return this->tokens;} - void Set_tokens(const std::vector &t) {this->tokens = t;} - -protected: - LDBLE logk[MAX_LOG_K_INDICES]; - LDBLE dz[3]; - std::vector tokens; -}; /*---------------------------------------------------------------------- * Species *---------------------------------------------------------------------- */ diff --git a/prep.cpp b/prep.cpp index 7051d304..c7d8a60e 100644 --- a/prep.cpp +++ b/prep.cpp @@ -1709,7 +1709,7 @@ clear(void) else { default_pe_x = "pe"; - cxxChemRxn chem_rxn; + CReaction chem_rxn; pe_x[default_pe_x] = chem_rxn; } @@ -2642,13 +2642,13 @@ write_mass_action_eqn_x(int stop) trxn.token[i].coef, false); if (equal(coef_e, 0.0, TOL) == FALSE) { - std::map < std::string, cxxChemRxn >::iterator chemRxnIt = pe_x.find(trxn.token[i].s->secondary->pe_rxn); + std::map < std::string, CReaction >::iterator chemRxnIt = pe_x.find(trxn.token[i].s->secondary->pe_rxn); if ( chemRxnIt == pe_x.end() ) { - cxxChemRxn &rxn_ref = pe_x[trxn.token[i].s->secondary->pe_rxn]; + CReaction& rxn_ref = pe_x[trxn.token[i].s->secondary->pe_rxn]; trxn_add(rxn_ref, trxn.token[i].coef * coef_e, FALSE); // Create temporary rxn object and add reactions together - cxxChemRxn rxn; + CReaction rxn; trxn_add(rxn, trxn.token[i].coef * coef_e, FALSE); } else @@ -4914,12 +4914,12 @@ tidy_redox(void) /* * Writes equations for e- for each redox couple used in solution n */ - std::map::iterator it; + std::map < std::string, CReaction >::iterator it; for (it = pe_x.begin(); it != pe_x.end(); it++) { if (strcmp_nocase(it->first.c_str(), "pe") == 0) { - cxxChemRxn temp_rxn(s_eminus->rxn); + CReaction temp_rxn(s_eminus->rxn); it->second = temp_rxn; } else @@ -4960,14 +4960,14 @@ tidy_redox(void) "Analytical data missing for redox couple, %s\n\t Using pe instead.", it->first.c_str()); warning_msg(error_string); - cxxChemRxn temp_rxn(s_eminus->rxn); + CReaction temp_rxn(s_eminus->rxn); it->second = temp_rxn; } else { CReaction rxn(count_trxn + 1); trxn_copy(rxn); - cxxChemRxn temp_rxn(rxn); + CReaction temp_rxn(rxn); it->second = temp_rxn; } } @@ -4988,16 +4988,15 @@ tidy_redox(void) error_string = sformatf( "Using pe instead of %s.", it->first.c_str()); warning_msg(error_string); - cxxChemRxn temp_rxn(s_eminus->rxn); + CReaction temp_rxn(s_eminus->rxn); it->second = temp_rxn; } else { CReaction rxn(count_trxn + 1); trxn_copy(rxn); - cxxChemRxn temp_rxn(rxn); + CReaction temp_rxn(rxn); it->second = temp_rxn; - //rxn_free(rxn); } } diff --git a/print.cpp b/print.cpp index 0ddedefa..a86b7ec1 100644 --- a/print.cpp +++ b/print.cpp @@ -1186,7 +1186,7 @@ print_saturation_indices(void) if (state == INITIAL_SOLUTION) { iap = 0; - for (size_t tok = 1; tok < pe_x[default_pe_x].Get_tokens().size(); tok++) + for (size_t tok = 1; tok < pe_x[default_pe_x].Get_tokens().size() - 1; tok++) { iap += pe_x[default_pe_x].Get_tokens()[tok].coef * pe_x[default_pe_x].Get_tokens()[tok].s->la; /* fprintf(output,"\t%s\t%f\t%f\n", rxn_ptr->s->name, rxn_ptr->coef, rxn_ptr->s->la ); */ diff --git a/read.cpp b/read.cpp index c6e004e9..2a7d075b 100644 --- a/read.cpp +++ b/read.cpp @@ -4949,7 +4949,7 @@ read_solution(void) const char * str = string_hsave(token.c_str()); //isoln_ptr->Set_default_pe(token); isoln_ptr->Set_default_pe(str); - cxxChemRxn temp_chem_reaction; + CReaction temp_chem_reaction; isoln_ptr->Get_pe_reactions()[token] = temp_chem_reaction; } else @@ -5121,7 +5121,7 @@ read_solution(void) isoln_ptr->Get_comps()[temp_comp.Get_description()] = temp_comp; if (temp_comp.Get_pe_reaction().size() > 0) { - cxxChemRxn temp_chem_reaction; + CReaction temp_chem_reaction; isoln_ptr->Get_pe_reactions()[temp_comp.Get_pe_reaction()] = temp_chem_reaction; } } diff --git a/spread.cpp b/spread.cpp index 50500580..c35448fd 100644 --- a/spread.cpp +++ b/spread.cpp @@ -629,7 +629,7 @@ spread_row_to_solution(struct spread_row *heading, struct spread_row *units, initial_data_ptr->Set_units(defaults.units); initial_data_ptr->Set_default_pe(defaults.redox); { - cxxChemRxn temp_chem_reaction; + CReaction temp_chem_reaction; initial_data_ptr->Get_pe_reactions()[defaults.redox] = temp_chem_reaction; } /* @@ -741,7 +741,7 @@ spread_row_to_solution(struct spread_row *heading, struct spread_row *units, { const char * pe_str = string_hsave(token.c_str()); initial_data_ptr->Set_default_pe(pe_str); - cxxChemRxn temp_chem_reaction; + CReaction temp_chem_reaction; initial_data_ptr->Get_pe_reactions()[token] = temp_chem_reaction; } else @@ -979,7 +979,7 @@ spread_row_to_solution(struct spread_row *heading, struct spread_row *units, initial_data_ptr->Get_comps()[temp_comp.Get_description()] = temp_comp; if (temp_comp.Get_pe_reaction().size() > 0) { - cxxChemRxn temp_chem_reaction; + CReaction temp_chem_reaction; initial_data_ptr->Get_pe_reactions()[temp_comp.Get_pe_reaction()] = temp_chem_reaction; } } diff --git a/structures.cpp b/structures.cpp index 2adef0ad..5d6b404b 100644 --- a/structures.cpp +++ b/structures.cpp @@ -1897,66 +1897,6 @@ rxn_token_temp_compare(const void *ptr1, const void *ptr2) } /* ---------------------------------------------------------------------- */ int Phreeqc:: -trxn_add(cxxChemRxn &r_ptr, LDBLE coef, int combine) -/* ---------------------------------------------------------------------- */ -{ -/* - * Adds reactions together. - * - * Global variable count_trxn determines which position in trxn is used. - * If count_trxn=0, then the equation effectively is copied into trxn. - * If count_trxn>0, then new equation is added to existing equation. - * - * Arguments: - * *r_ptr points to rxn structure to add. - * - * coef added equation is multiplied by coef. - * combine if TRUE, reaction is reaction is sorted and - * like terms combined. - */ -/* - * Accumulate log k for reaction - */ - if (count_trxn == 0) - { - for (int i = 0; i < MAX_LOG_K_INDICES; i++) - { - trxn.logk[i] = r_ptr.Get_logk()[i]; - } - for (int i = 0; i < 3; i++) - { - trxn.dz[i] = r_ptr.Get_dz()[i]; - } - } - else - { - for (int i = 0; i < MAX_LOG_K_INDICES; i++) - { - trxn.logk[i] += coef * (r_ptr.Get_logk()[i]); - } - for (int i = 0; i < 3; i++) - { - trxn.dz[i] += coef * r_ptr.Get_dz()[i]; - } - } -/* - * Copy equation into work space - */ - for (size_t j = 0; j < r_ptr.Get_tokens().size(); j++) - { - if (count_trxn + 1 > trxn.token.size()) - trxn.token.resize(count_trxn + 1); - trxn.token[count_trxn].name = r_ptr.Get_tokens()[j].name; - trxn.token[count_trxn].s = r_ptr.Get_tokens()[j].s; - trxn.token[count_trxn].coef = coef * r_ptr.Get_tokens()[j].coef; - count_trxn++; - } - if (combine == TRUE) - trxn_combine(); - return (OK); -} -/* ---------------------------------------------------------------------- */ -int Phreeqc:: trxn_combine(void) /* ---------------------------------------------------------------------- */ { From 28de8b5cdaef905a0c867c7461f21b6e0d8cb49a Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Thu, 1 Apr 2021 22:19:48 -0600 Subject: [PATCH 35/53] more size_t --- gases.cpp | 2 +- inverse.cpp | 16 +++++------ model.cpp | 22 ++++++++-------- prep.cpp | 76 ++++++++++++++++++++++++++--------------------------- read.cpp | 2 -- 5 files changed, 55 insertions(+), 63 deletions(-) diff --git a/gases.cpp b/gases.cpp index f0b177fe..b499bb00 100644 --- a/gases.cpp +++ b/gases.cpp @@ -56,7 +56,7 @@ build_fixed_volume_gas(void) * sum of partial pressures equation and * mass balance equations for elements contained in gases */ - int row, col; + size_t row, col; struct master *master_ptr; struct rxn_token *rxn_ptr; struct unknown *unknown_ptr; diff --git a/inverse.cpp b/inverse.cpp index 0b0677a3..a1b748d1 100644 --- a/inverse.cpp +++ b/inverse.cpp @@ -2441,15 +2441,12 @@ range(struct inverse *inv_ptr, unsigned long cur_bits) } else { - cl1(k, l, m, n, - nklmd, n2d, array1, - &kode, toler, &iter, delta2, inv_res, &error2, inv_cu, inv_iu, inv_is, - TRUE); + cl1(k, l, m, n, nklmd, n2d, array1, &kode, toler, &iter, delta2, + inv_res, &error2, inv_cu, inv_iu, inv_is, TRUE); } #else - cl1(k, l, m, n, - nklmd, n2d, &array1[0], - &kode, toler, &iter, &delta2[0], &inv_res[0], &error2, &inv_cu[0], &inv_iu[0], &inv_is[0], TRUE); + cl1(k, l, m, n, nklmd, n2d, &array1[0], &kode, toler, &iter, &delta2[0], + &inv_res[0], &error2, &inv_cu[0], &inv_iu[0], &inv_is[0], TRUE); #endif if (kode != 0) { @@ -2915,9 +2912,8 @@ check_solns(struct inverse *inv_ptr) kode = 1; iter = 200; count_calls++; - cl1(k, l, m, n, - nklmd, n2d, &array1[0], - &kode, toler, &iter, &delta2[0], &inv_res[0], &error2, &inv_cu[0], &inv_iu[0], &inv_is[0], TRUE); + cl1(k, l, m, n, nklmd, n2d, &array1[0], &kode, toler, &iter, + &delta2[0], &inv_res[0], &error2, &inv_cu[0], &inv_iu[0], &inv_is[0], TRUE); if (kode != 0) { diff --git a/model.cpp b/model.cpp index 71981566..62671318 100644 --- a/model.cpp +++ b/model.cpp @@ -1315,7 +1315,7 @@ ineq(int in_kode) back_eq[l_count_rows] = i; if (mass_water_switch == TRUE && x[i] == mass_hydrogen_unknown) { - k = mass_oxygen_unknown->number; + k = (int)mass_oxygen_unknown->number; for (j = 0; j < count_unknowns; j++) { ineq_array[(size_t)l_count_rows * max_column_count + (size_t)j] -= @@ -1563,7 +1563,7 @@ ineq(int in_kode) */ if (gas_in == TRUE) { - for (i = gas_unknown->number; i < count_unknowns; i++) + for (i = (int)gas_unknown->number; i < (int)count_unknowns; i++) { if (x[i]->type == GAS_MOLES) { @@ -1592,7 +1592,7 @@ ineq(int in_kode) /* * Moles of gas small and sum p < ptotal */ - i = gas_unknown->number; + i = (int)gas_unknown->number; for (j = 0; j < l_count_rows; j++) { ineq_array[(size_t)j * max_column_count + (size_t)i] = 0.0; @@ -1604,7 +1604,7 @@ ineq(int in_kode) if (ss_unknown != NULL) { - for (i = ss_unknown->number; i < count_unknowns; i++) + for (i = (int)ss_unknown->number; i < (int)count_unknowns; i++) { if (x[i]->type != SS_MOLES) break; @@ -1659,7 +1659,7 @@ ineq(int in_kode) */ if (mass_oxygen_unknown != NULL && mass_water_switch == TRUE) { - k = mass_oxygen_unknown->number; + k = (int)mass_oxygen_unknown->number; for (j = 0; j < l_count_rows + 1; j++) { ineq_array[(size_t)j * max_column_count + (size_t)k] = 0; @@ -1684,8 +1684,8 @@ ineq(int in_kode) if (debug_model == TRUE) { output_msg(sformatf( "\nA and B arrays:\n\n")); - array_print(&ineq_array[0], l_count_rows, count_unknowns + 1, - max_column_count); + array_print(&ineq_array[0], (int)l_count_rows, (int)count_unknowns + 1, + (int)max_column_count); } /* * Calculate dimensions @@ -1704,7 +1704,7 @@ ineq(int in_kode) #ifdef SHRINK_ARRAY if ((sit_model || pitzer_model) && full_pitzer == FALSE) { - n = count_unknowns - (int) s_list.size(); + n = (int)count_unknowns - (int)s_list.size(); for (int i = 0; i < l_count_rows; i++) { for (int j = 0; j < n; j++) @@ -1720,12 +1720,12 @@ ineq(int in_kode) } else { - n = count_unknowns; /* columns in A, C, E */ + n = (int)count_unknowns; /* columns in A, C, E */ } #else n = count_unknowns; /* columns in A, C, E */ #endif - l_klmd = max_row_count - 2; + l_klmd = (int)max_row_count - 2; l_nklmd = n + l_klmd; l_n2d = n + 2; /* @@ -2311,7 +2311,7 @@ mb_ss(void) } } } - for (int i = ss_unknown->number; i < count_unknowns; i++) + for (int i = (int)ss_unknown->number; i < (int)count_unknowns; i++) { if (x[i]->type != SS_MOLES) break; diff --git a/prep.cpp b/prep.cpp index c7d8a60e..0f52d5b6 100644 --- a/prep.cpp +++ b/prep.cpp @@ -685,8 +685,8 @@ build_ss_assemblage(void) } if (master_ptr == NULL || master_ptr->unknown == NULL) continue; - store_jacob0(x[i]->number, master_ptr->unknown->number, - rxn_ptr->coef); + store_jacob0((int)x[i]->number, (int)master_ptr->unknown->number, + rxn_ptr->coef); } if (ss_ptr->Get_a0() != 0.0 || ss_ptr->Get_a1() != 0.0) @@ -750,19 +750,19 @@ build_ss_assemblage(void) if (strcmp(elt_list[j].elt->name, "H") == 0 && mass_hydrogen_unknown != NULL) { - store_jacob0(mass_hydrogen_unknown->number, x[i]->number, - -elt_list[j].coef); + store_jacob0((int)mass_hydrogen_unknown->number, (int)x[i]->number, + -elt_list[j].coef); store_sum_deltas(&(delta[i]), &mass_hydrogen_unknown->delta, - elt_list[j].coef); + elt_list[j].coef); } else if (strcmp(elt_list[j].elt->name, "O") == 0 && mass_oxygen_unknown != NULL) { - store_jacob0(mass_oxygen_unknown->number, x[i]->number, - -elt_list[j].coef); + store_jacob0((int)mass_oxygen_unknown->number, (int)x[i]->number, + -elt_list[j].coef); store_sum_deltas(&(delta[i]), &mass_oxygen_unknown->delta, - elt_list[j].coef); + elt_list[j].coef); } else @@ -792,10 +792,10 @@ build_ss_assemblage(void) } else if (master_ptr->in == TRUE) { - store_jacob0(master_ptr->unknown->number, x[i]->number, - -elt_list[j].coef); + store_jacob0((int)master_ptr->unknown->number, (int)x[i]->number, + -elt_list[j].coef); store_sum_deltas(&delta[i], &master_ptr->unknown->delta, - elt_list[j].coef); + elt_list[j].coef); /* * Master species in equation needs to be rewritten */ @@ -811,12 +811,11 @@ build_ss_assemblage(void) { if (x[k]->master[l] == master_ptr) { - store_jacob0(x[k]->master[0]->unknown-> - number, x[i]->number, - -elt_list[j].coef); + store_jacob0((int)x[k]->master[0]->unknown->number, + (int)x[i]->number, -elt_list[j].coef); store_sum_deltas(&delta[i], - &x[k]->master[0]->unknown-> - delta, elt_list[j].coef); + &x[k]->master[0]->unknown-> + delta, elt_list[j].coef); stop = TRUE; break; } @@ -861,10 +860,10 @@ build_jacobian_sums(int k) } coef = mb_unknowns[i].coef; if (debug_prep == TRUE) - output_msg(sformatf( "\n\tMass balance eq: %-13s\t%f\trow\tcol\n", - mb_unknowns[i].unknown->description, (double) coef)); - store_dn(k, mb_unknowns[i].source, mb_unknowns[i].unknown->number, - coef, mb_unknowns[i].gamma_source); + output_msg(sformatf("\n\tMass balance eq: %-13s\t%f\trow\tcol\n", + mb_unknowns[i].unknown->description, (double)coef)); + store_dn(k, mb_unknowns[i].source, (int)mb_unknowns[i].unknown->number, + coef, mb_unknowns[i].gamma_source); /* * Add extra terms for change in dg/dx in diffuse layer model */ @@ -1392,8 +1391,8 @@ build_pure_phases(void) } if (master_ptr == NULL || master_ptr->unknown == NULL) continue; - store_jacob0(x[i]->number, master_ptr->unknown->number, - rxn_ptr->coef); + store_jacob0((int)x[i]->number, (int)master_ptr->unknown->number, + rxn_ptr->coef); } /* * Put coefficients into mass balance equations @@ -1425,19 +1424,19 @@ build_pure_phases(void) if (strcmp(elt_list[j].elt->name, "H") == 0 && mass_hydrogen_unknown != NULL) { - store_jacob0(mass_hydrogen_unknown->number, x[i]->number, - -elt_list[j].coef); + store_jacob0((int)mass_hydrogen_unknown->number, (int)x[i]->number, + -elt_list[j].coef); store_sum_deltas(&(delta[i]), &mass_hydrogen_unknown->delta, - elt_list[j].coef); + elt_list[j].coef); } else if (strcmp(elt_list[j].elt->name, "O") == 0 && mass_oxygen_unknown != NULL) { - store_jacob0(mass_oxygen_unknown->number, x[i]->number, - -elt_list[j].coef); + store_jacob0((int)mass_oxygen_unknown->number, (int)x[i]->number, + -elt_list[j].coef); store_sum_deltas(&(delta[i]), &mass_oxygen_unknown->delta, - elt_list[j].coef); + elt_list[j].coef); } else @@ -1474,10 +1473,10 @@ build_pure_phases(void) } else if (master_ptr->in == TRUE) { - store_jacob0(master_ptr->unknown->number, x[i]->number, - -elt_list[j].coef); + store_jacob0((int)master_ptr->unknown->number, (int)x[i]->number, + -elt_list[j].coef); store_sum_deltas(&delta[i], &master_ptr->unknown->delta, - elt_list[j].coef); + elt_list[j].coef); /* * Master species in equation needs to be rewritten */ @@ -1493,12 +1492,11 @@ build_pure_phases(void) { if (x[k]->master[l] == master_ptr) { - store_jacob0(x[k]->master[0]->unknown-> - number, x[i]->number, - -elt_list[j].coef); + store_jacob0((int)x[k]->master[0]->unknown->number, + (int)x[i]->number, -elt_list[j].coef); store_sum_deltas(&delta[i], - &x[k]->master[0]->unknown-> - delta, elt_list[j].coef); + &x[k]->master[0]->unknown-> + delta, elt_list[j].coef); stop = TRUE; break; } @@ -1574,7 +1572,7 @@ build_solution_phase_boundaries(void) } if (master_ptr->unknown == NULL) continue; - store_jacob0(x[i]->number, master_ptr->unknown->number, + store_jacob0((int)x[i]->number, (int)master_ptr->unknown->number, rxn_ptr->coef); } } @@ -5914,7 +5912,7 @@ build_min_exch(void) */ /* charge balance */ - store_jacob0(charge_balance_unknown->number, x[k]->number, + store_jacob0((int)charge_balance_unknown->number, (int)x[k]->number, comp_ref.Get_formula_z() * comp_ref.Get_phase_proportion()); store_sum_deltas(&delta[k], &charge_balance_unknown->delta, -comp_ref.Get_formula_z() * comp_ref.Get_phase_proportion()); @@ -5982,7 +5980,7 @@ build_min_exch(void) row = master_ptr->unknown->number; unknown_ptr = master_ptr->unknown; } - store_jacob0(row, x[k]->number, + store_jacob0(row, (int)x[k]->number, coef * comp_ref.Get_phase_proportion()); store_sum_deltas(&delta[k], &unknown_ptr->delta, -coef * comp_ref.Get_phase_proportion()); diff --git a/read.cpp b/read.cpp index 2a7d075b..5b2f4bdc 100644 --- a/read.cpp +++ b/read.cpp @@ -396,7 +396,6 @@ read_exchange_species(void) struct species *s_ptr; struct elt_list *next_elt; - struct rxn_token *token_ptr; //LDBLE exchange_coef; LDBLE offset; @@ -5905,7 +5904,6 @@ read_surface_species(void) struct species *s_ptr; struct elt_list *next_elt; - struct rxn_token *token_ptr; int return_value, opt, opt_save; const char* next_char; From 7228bd0d1eaf4bd78f5ecfefb8823f6656873da4 Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Thu, 1 Apr 2021 23:25:19 -0600 Subject: [PATCH 36/53] move struct rxn_token --- CReaction.h | 6 ++++++ cl1.cpp | 6 ------ global_structures.h | 6 ------ model.cpp | 4 ---- 4 files changed, 6 insertions(+), 16 deletions(-) diff --git a/CReaction.h b/CReaction.h index 5f4335f5..90375de7 100644 --- a/CReaction.h +++ b/CReaction.h @@ -27,4 +27,10 @@ public: double dz[3]; std::vector token; }; +struct rxn_token +{ + struct species* s; + LDBLE coef; + const char* name; +}; #endif // !defined(CREACTION_H_INCLUDED) \ No newline at end of file diff --git a/cl1.cpp b/cl1.cpp index fefba87d..358da191 100644 --- a/cl1.cpp +++ b/cl1.cpp @@ -188,13 +188,7 @@ cl1(int k, int l, int m, int n, output_msg(sformatf( "Set up phase 1 costs\n")); #endif /* Zero first row of cu and iu */ - //memcpy((void *) &(l_cu[0]), (void *) &(scratch[0]), - // (size_t) nklm * sizeof(LDBLE)); memset(&l_cu[0], 0, (size_t)nklm * sizeof(LDBLE)); - //for (j = 0; j < nklm; ++j) - //{ - // l_iu[j] = 0; - //} memset(&l_iu[0], 0, (size_t)nklm * sizeof(int)); /* L40: */ #ifdef DEBUG_CL1 diff --git a/global_structures.h b/global_structures.h index beae8377..1beaa089 100644 --- a/global_structures.h +++ b/global_structures.h @@ -409,12 +409,6 @@ struct elt_list struct element *elt; /* pointer to element structure */ LDBLE coef; /* number of element e's in eqn */ }; -struct rxn_token -{ - struct species *s; - LDBLE coef; - const char *name; -}; /*---------------------------------------------------------------------- * Species diff --git a/model.cpp b/model.cpp index 62671318..7918ae43 100644 --- a/model.cpp +++ b/model.cpp @@ -1369,10 +1369,6 @@ ineq(int in_kode) { /* Pure phase is present, force Mass transfer to be <= amount of mineral remaining */ - //memcpy((void *) - // &(ineq_array[(size_t)l_count_rows * max_column_count]), - // (void *) &(zero[0]), - // ((size_t) count_unknowns + 1) * sizeof(LDBLE)); memset(&ineq_array[(size_t)l_count_rows * max_column_count], 0, ((size_t) count_unknowns + 1) * sizeof(LDBLE)); ineq_array[(size_t)l_count_rows * max_column_count + i] = 1.0; ineq_array[(size_t)l_count_rows * max_column_count + count_unknowns] = x[i]->moles; From 287f81cdeab84c8a3e6b305be742ccd88b7c8100 Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Fri, 2 Apr 2021 12:23:30 -0600 Subject: [PATCH 37/53] elt_list vectorized --- Classes.cpp | 54 +++++++++++- ExchComp.h | 8 -- NameDouble.cxx | 34 ++------ NameDouble.h | 3 +- Phreeqc.cpp | 41 ++------- Phreeqc.h | 40 ++++----- basicsubs.cpp | 22 ++--- global_structures.h | 16 ++-- inverse.cpp | 6 +- parse.cpp | 107 +++++++++++------------ prep.cpp | 14 ++- read.cpp | 208 ++++++++++++++++++++++---------------------- step.cpp | 8 +- structures.cpp | 157 ++++++--------------------------- tally.cpp | 17 ++-- tidy.cpp | 25 +++--- utilities.cpp | 116 ------------------------ 17 files changed, 316 insertions(+), 560 deletions(-) diff --git a/Classes.cpp b/Classes.cpp index ad4559ae..8038ed2b 100644 --- a/Classes.cpp +++ b/Classes.cpp @@ -293,4 +293,56 @@ CReaction Phreeqc::CReaction_internal_copy(CReaction& rxn_ref) } return rxn; } - +int Phreeqc:: +add_elt_list(const cxxNameDouble& nd, LDBLE coef) +/* ---------------------------------------------------------------------- */ +{ + cxxNameDouble::const_iterator cit = nd.begin(); + for (; cit != nd.end(); cit++) + { + if (count_elts >= (int)elt_list.size()) + { + elt_list.resize(count_elts + 1); + } + elt_list[count_elts].elt = element_store(cit->first.c_str()); + elt_list[count_elts].coef = cit->second * coef; + count_elts++; + } + return (OK); +} +int Phreeqc:: +add_elt_list(const std::vector& el, double coef) +/* ---------------------------------------------------------------------- */ +{ + const struct elt_list* elt_list_ptr = &el[0]; + + for (; elt_list_ptr->elt != NULL; elt_list_ptr++) + { + if (count_elts >= elt_list.size()) + { + elt_list.resize(count_elts + 1); + } + elt_list[count_elts].elt = elt_list_ptr->elt; + elt_list[count_elts].coef = elt_list_ptr->coef * coef; + count_elts++; + } + return (OK); +} +std::vector Phreeqc:: +elt_list_internal_copy(const std::vector& el) +/* ---------------------------------------------------------------------- */ +{ + std::vector new_elt_list; + const struct elt_list* elt_list_ptr = &el[0]; + + new_elt_list.resize(el.size()); + size_t count = 0; + for (; elt_list_ptr->elt != NULL; elt_list_ptr++) + { + new_elt_list[count].elt = element_store(elt_list_ptr->elt->name); + new_elt_list[count].coef = elt_list_ptr->coef; + count++; + } + new_elt_list[count].elt = NULL; + return new_elt_list; +} \ No newline at end of file diff --git a/ExchComp.h b/ExchComp.h index c2846f7a..6af0718f 100644 --- a/ExchComp.h +++ b/ExchComp.h @@ -87,14 +87,6 @@ class cxxExchComp: public PHRQ_base { this->formula_z = d; } - void Set_totals(struct elt_list *e_l, int count) - { - this->totals = cxxNameDouble(e_l, count); - } - void Set_totals(struct elt_list *e_l) - { - this->totals = cxxNameDouble(e_l); - } void Set_totals(cxxNameDouble nd) { this->totals = nd; diff --git a/NameDouble.cxx b/NameDouble.cxx index e8b64ef4..0f8e667e 100644 --- a/NameDouble.cxx +++ b/NameDouble.cxx @@ -29,39 +29,17 @@ cxxNameDouble::cxxNameDouble() { this->type = ND_ELT_MOLES; } - -cxxNameDouble::cxxNameDouble(struct elt_list *elt_list_ptr) - // - // constructor for cxxNameDouble from list of elt_list - // +cxxNameDouble::cxxNameDouble(const std::vector& el) +// constructor for cxxNameDouble from vector of elt_list { - int i; - if (elt_list_ptr != NULL) + size_t i; + const struct elt_list* elt_list_ptr = &el[0]; + for (i = 0; elt_list_ptr[i].elt != NULL; i++) { - for (i = 0; elt_list_ptr[i].elt != NULL; i++) - { - (*this)[elt_list_ptr[i].elt->name] = elt_list_ptr[i].coef; - } + (*this)[elt_list_ptr[i].elt->name] = elt_list_ptr[i].coef; } this->type = ND_ELT_MOLES; } - -cxxNameDouble::cxxNameDouble(struct elt_list *elt_list_ptr, int count) - // - // constructor for cxxNameDouble from list of elt_list with known count - // -{ - int i; - if (elt_list_ptr != NULL) - { - for (i = 0; i < count; i++) - { - (*this)[elt_list_ptr[i].elt->name] = elt_list_ptr[i].coef; - } - } - this->type = ND_ELT_MOLES; -} - cxxNameDouble::cxxNameDouble(const cxxNameDouble & old, LDBLE factor) // // constructor for cxxNameDouble from list of elt_list diff --git a/NameDouble.h b/NameDouble.h index 9c7c12ba..7edb65ff 100644 --- a/NameDouble.h +++ b/NameDouble.h @@ -28,8 +28,7 @@ class IPQ_DLL_EXPORT cxxNameDouble:public }; cxxNameDouble(); - cxxNameDouble(struct elt_list *); - cxxNameDouble(struct elt_list *, int count); + cxxNameDouble(const std::vector& el); cxxNameDouble(std::map < std::string, cxxISolutionComp > &comps); cxxNameDouble(struct name_coef *nc, int count); diff --git a/Phreeqc.cpp b/Phreeqc.cpp index b543aa5d..942a6093 100644 --- a/Phreeqc.cpp +++ b/Phreeqc.cpp @@ -1515,29 +1515,11 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) s_ptr->add_logk[j].name = string_hsave(pSrc->s[i]->add_logk[j].name); } //next_elt - s_ptr->next_elt = NULL; - if (pSrc->s[i]->next_elt) - { - cxxNameDouble next_elt(pSrc->s[i]->next_elt); - s_ptr->next_elt = NameDouble2elt_list(next_elt); - } + s_ptr->next_elt = elt_list_internal_copy(pSrc->s[i]->next_elt); + //next_secondary - s_ptr->next_secondary = NULL; - if (pSrc->s[i]->next_secondary && pSrc->s[i]->mole_balance) - { - count_elts = 0; - paren_count = 0; - const char* cptr = s_ptr->mole_balance; - get_secondary_in_species(&cptr, 1.0); - s_ptr->next_secondary = elt_list_save(); - } - //next_sys_total - s_ptr->next_sys_total = NULL; - if (pSrc->s[i]->next_sys_total) - { - cxxNameDouble next_sys_total(pSrc->s[i]->next_sys_total); - s_ptr->next_sys_total = NameDouble2elt_list(next_sys_total); - } + s_ptr->next_secondary = elt_list_internal_copy(pSrc->s[i]->next_secondary); + s_ptr->next_sys_total = elt_list_internal_copy(pSrc->s[i]->next_sys_total); //rxn s_ptr->rxn = CReaction_internal_copy(pSrc->s[i]->rxn); s_ptr->rxn_s = CReaction_internal_copy(pSrc->s[i]->rxn_s); @@ -1567,19 +1549,8 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) phase_ptr->add_logk[j].name = string_hsave(pSrc->phases[i]->add_logk[j].name); } //next_elt - phase_ptr->next_elt = NULL; - if (pSrc->phases[i]->next_elt) - { - cxxNameDouble next_elt(pSrc->phases[i]->next_elt); - phase_ptr->next_elt = NameDouble2elt_list(next_elt); - } - //next_sys_total - phase_ptr->next_sys_total = NULL; - if (pSrc->phases[i]->next_sys_total) - { - cxxNameDouble next_sys_total(pSrc->phases[i]->next_sys_total); - phase_ptr->next_sys_total = NameDouble2elt_list(next_sys_total); - } + phase_ptr->next_elt = elt_list_internal_copy(pSrc->phases[i]->next_elt); + phase_ptr->next_sys_total = elt_list_internal_copy(pSrc->phases[i]->next_sys_total); //rxn phase_ptr->rxn = CReaction_internal_copy(pSrc->phases[i]->rxn); phase_ptr->rxn_s = CReaction_internal_copy(pSrc->phases[i]->rxn_s); diff --git a/Phreeqc.h b/Phreeqc.h index 0552cdca..4babcf7e 100644 --- a/Phreeqc.h +++ b/Phreeqc.h @@ -480,7 +480,7 @@ public: int get_elts_in_species(const char** t_ptr, LDBLE coef); int get_num(const char** t_ptr, LDBLE* num); int get_secondary_in_species(const char** t_ptr, LDBLE coef); - int parse_eq(char* eqn, struct elt_list** elt_ptr, int association); + int parse_eq(char* eqn, std::vector& new_elt_list, int association); int get_coef(LDBLE* coef, const char** eqnaddr); int get_secondary(const char** t_ptr, char* element, int* i); int get_species(const char** ptr); @@ -558,7 +558,6 @@ public: LDBLE calc_PR(std::vector phase_ptrs, LDBLE P, LDBLE TK, LDBLE V_m); LDBLE calc_PR(); int calc_vm(LDBLE tc, LDBLE pa); - int change_hydrogen_in_elt_list(LDBLE charge); int clear(void); int convert_units(cxxSolution* solution_ptr); struct unknown* find_surface_charge_unknown(std::string& str_ptr, int plane); @@ -818,16 +817,7 @@ public: int copier_add(struct copier* copier_ptr, int n_user, int start, int end); int copier_clear(struct copier* copier_ptr); static int element_compare(const void* ptr1, const void* ptr2); -public: - struct element* element_store(const char* element); - int elt_list_combine(void); - static int elt_list_compare(const void* ptr1, const void* ptr2); -protected: - struct elt_list* elt_list_dup(struct elt_list* elt_list_ptr_old); - int elt_list_print(struct elt_list* elt_list_ptr); - struct elt_list* elt_list_save(void); - cxxNameDouble elt_list_NameDouble(void); - struct elt_list* NameDouble2elt_list(const cxxNameDouble& nd); + public: enum entity_type get_entity_enum(char* name); struct inverse* inverse_alloc(void); @@ -911,19 +901,10 @@ public: struct species* s_alloc(void); int s_free(struct species* s_ptr); int s_init(struct species* s_ptr); - static int ss_assemblage_compare_int(const void* ptr1, const void* ptr2); - static int solution_compare(const void* ptr1, const void* ptr2); - static int solution_compare_int(const void* ptr1, const void* ptr2); static int species_list_compare(const void* ptr1, const void* ptr2); - static int surface_compare_int(const void* ptr1, const void* ptr2); static int rxn_token_temp_compare(const void* ptr1, const void* ptr2); int trxn_multiply(LDBLE coef); - struct elt_list* cxxNameDouble2elt_list(const cxxNameDouble* nd); - struct name_coef* cxxNameDouble2name_coef(const cxxNameDouble* nd); - struct master_activity* cxxNameDouble2master_activity(const cxxNameDouble* nd); - struct master* cxxNameDouble2surface_master(const cxxNameDouble* totals); - void Use2cxxStorageBin(cxxStorageBin& sb); void phreeqc2cxxStorageBin(cxxStorageBin& sb); void phreeqc2cxxStorageBin(cxxStorageBin& sb, int n); @@ -1030,11 +1011,20 @@ public: int mix_stag(int i, LDBLE stagkin_time, int punch, LDBLE step_fraction_kin); + // elt_list + int add_elt_list(const cxxNameDouble& nd, LDBLE coef); + int add_elt_list(const std::vector& el, double coef); + std::vector elt_list_internal_copy(const std::vector& el); + int change_hydrogen_in_elt_list(LDBLE charge); + struct element* element_store(const char* element); + int elt_list_combine(void); + static int elt_list_compare(const void* ptr1, const void* ptr2); + std::vector elt_list_vsave(void); + cxxNameDouble elt_list_NameDouble(void); + // utilities.cpp ------------------------------- public: - int add_elt_list(struct elt_list* elt_list_ptr, LDBLE coef); - int add_elt_list_multi_surf(struct elt_list* elt_list_ptr, LDBLE coef, struct element* surf_elt_ptr); - int add_elt_list(const cxxNameDouble& nd, LDBLE coef); + LDBLE calc_rho_0(LDBLE tc, LDBLE pa); LDBLE calc_dielectrics(LDBLE tc, LDBLE pa); int compute_gfw(const char* string, LDBLE* gfw); @@ -1383,7 +1373,7 @@ protected: * Element List *---------------------------------------------------------------------- */ std::vector elt_list; - int count_elts; /* number of elements in elt_list = position of next */ + size_t count_elts = 0; /* number of elements in elt_list = position of next */ /*---------------------------------------------------------------------- * Reaction *---------------------------------------------------------------------- */ diff --git a/basicsubs.cpp b/basicsubs.cpp index 0dd6ff51..440c0344 100644 --- a/basicsubs.cpp +++ b/basicsubs.cpp @@ -1288,9 +1288,9 @@ equivalent_fraction(const char *name, LDBLE *eq, std::string &elt_name) if (s_ptr != NULL && (s_ptr->type == EX || s_ptr->type == SURF)) { *eq = s_ptr->equiv; - struct elt_list *next_elt; + const struct elt_list *next_elt; LDBLE tot=0.0; - for (next_elt = s_ptr->next_elt; next_elt->elt != NULL; next_elt++) + for (next_elt = &s_ptr->next_elt[0]; next_elt->elt != NULL; next_elt++) { if (next_elt->elt->master->s->type == SURF || next_elt->elt->master->s->type == EX) @@ -1787,7 +1787,7 @@ sum_match_gases(const char *mytemplate, const char *name) { int i; LDBLE tot; - struct elt_list *next_elt; + const struct elt_list *next_elt; if (use.Get_gas_phase_in() == FALSE || use.Get_gas_phase_ptr() == NULL) return (0); @@ -1805,7 +1805,7 @@ sum_match_gases(const char *mytemplate, const char *name) } else { - for (next_elt = phase_ptr->next_elt; + for (next_elt = &phase_ptr->next_elt[0]; next_elt->elt != NULL; next_elt++) { if (strcmp(next_elt->elt->name, name) == 0) @@ -1827,7 +1827,7 @@ sum_match_species(const char *mytemplate, const char *name) { int i; LDBLE tot; - struct elt_list *next_elt; + const struct elt_list *next_elt; count_elts = 0; paren_count = 0; @@ -1856,7 +1856,7 @@ sum_match_species(const char *mytemplate, const char *name) } else { - for (next_elt = s_ptr->next_elt; next_elt->elt != NULL; + for (next_elt = &s_ptr->next_elt[0]; next_elt->elt != NULL; next_elt++) { if (strcmp(next_elt->elt->name, name) == 0) @@ -1878,7 +1878,7 @@ sum_match_ss(const char *mytemplate, const char *name) /* ---------------------------------------------------------------------- */ { LDBLE tot; - struct elt_list *next_elt; + const struct elt_list *next_elt; if (use.Get_ss_assemblage_in() == FALSE || use.Get_ss_assemblage_ptr() == NULL) return (0); @@ -1905,7 +1905,7 @@ sum_match_ss(const char *mytemplate, const char *name) { int l; struct phase *phase_ptr = phase_bsearch(comp_ptr->Get_name().c_str(), &l, FALSE); - for (next_elt = phase_ptr->next_elt; next_elt->elt != NULL; next_elt++) + for (next_elt = &phase_ptr->next_elt[0]; next_elt->elt != NULL; next_elt++) { if (strcmp(next_elt->elt->name, name) == 0) { @@ -3493,7 +3493,7 @@ system_total_elt_secondary(const char *total_name) { count_elts = 0; paren_count = 0; - if (s_x[i]->next_secondary != NULL) + if (s_x[i]->next_secondary.size() != 0) { add_elt_list(s_x[i]->next_secondary, s_x[i]->moles); } @@ -3566,7 +3566,7 @@ system_total_elt_secondary(const char *total_name) { count_elts = 0; paren_count = 0; - if (s_x[i]->next_secondary != NULL) + if (s_x[i]->next_secondary.size() != 0) { add_elt_list(s_x[i]->next_secondary, 1); } @@ -3779,7 +3779,7 @@ solution_sum_secondary(const char *total_name) continue; count_elts = 0; paren_count = 0; - if (s_x[i]->next_secondary != NULL) + if (s_x[i]->next_secondary.size() != 0) { add_elt_list(s_x[i]->next_secondary, s_x[i]->moles); } diff --git a/global_structures.h b/global_structures.h index 1beaa089..8d083c5b 100644 --- a/global_structures.h +++ b/global_structures.h @@ -406,8 +406,8 @@ struct element *---------------------------------------------------------------------- */ struct elt_list { /* list of name and number of elements in an equation */ - struct element *elt; /* pointer to element structure */ - LDBLE coef; /* number of element e's in eqn */ + struct element *elt = NULL; /* pointer to element structure */ + LDBLE coef = 0.0; /* number of element e's in eqn */ }; /*---------------------------------------------------------------------- @@ -456,9 +456,9 @@ struct species int type; /* flag indicating presence in model and types of equations */ int gflag; /* flag for preferred activity coef eqn */ int exch_gflag; /* flag for preferred activity coef eqn */ - struct elt_list *next_elt; /* pointer to next element */ - struct elt_list *next_secondary; - struct elt_list *next_sys_total; + std::vector next_elt; // vector of elements + std::vector next_secondary; + std::vector next_sys_total; int check_equation; /* switch to check equation for charge and element balance */ CReaction rxn; /* pointer to data base reaction */ CReaction rxn_s; /* pointer to reaction converted to secondary and primary @@ -513,8 +513,8 @@ struct phase bool pr_in; /* Peng-Robinson in the calc's, or not */ int type; /* flag indicating presence in model and types of equations */ - struct elt_list *next_elt; /* pointer to list of elements in phase */ - struct elt_list *next_sys_total; + std::vector next_elt; /* pointer to list of elements in phase */ + std::vector next_sys_total; int check_equation; /* switch to check equation for charge and element balance */ CReaction rxn; /* pointer to data base reaction */ CReaction rxn_s; /* pointer to reaction converted to secondary and primary @@ -766,7 +766,7 @@ struct tally enum entity_type type; const char *add_formula; LDBLE moles; - struct elt_list *formula; + std::vector formula; /* * first total is initial * second total is final diff --git a/inverse.cpp b/inverse.cpp index a1b748d1..5df8e355 100644 --- a/inverse.cpp +++ b/inverse.cpp @@ -4253,7 +4253,7 @@ dump_netpath_pat(struct inverse *inv_ptr) count_current_solutions, temp_punch; int solnmap[10][2]; FILE *model_file; - struct elt_list *next_elt; + const struct elt_list *next_elt; int exch, column; LDBLE f; struct rxn_token *rxn_ptr; @@ -4817,7 +4817,7 @@ dump_netpath_pat(struct inverse *inv_ptr) * Determine if exchange reaction */ exch = FALSE; - for (next_elt = inv_ptr->phases[i].phase->next_elt; + for (next_elt = &inv_ptr->phases[i].phase->next_elt[0]; next_elt->elt != NULL; next_elt++) { if (strcmp(next_elt->elt->name, "X") == 0) @@ -4870,7 +4870,7 @@ dump_netpath_pat(struct inverse *inv_ptr) /* * Write stoichiometry */ - for (next_elt = inv_ptr->phases[i].phase->next_elt; + for (next_elt = &inv_ptr->phases[i].phase->next_elt[0]; next_elt->elt != NULL; next_elt++) { f = 1.0; diff --git a/parse.cpp b/parse.cpp index beb7fc58..2ec53683 100644 --- a/parse.cpp +++ b/parse.cpp @@ -1,10 +1,9 @@ #include "Phreeqc.h" #include "phqalloc.h" - /* ---------------------------------------------------------------------- */ int Phreeqc:: -parse_eq(char *eqn, struct elt_list **elt_ptr, int association) +parse_eq(char* eqn, std::vector& new_elt_list, int association) /* ---------------------------------------------------------------------- */ /* * function to break equation up into component species @@ -30,27 +29,27 @@ parse_eq(char *eqn, struct elt_list **elt_ptr, int association) char token[MAX_LENGTH]; paren_count = 0; -/* - * Remove white space - */ + /* + * Remove white space + */ squeeze_white(eqn); -/* - * Check for illegal characters - */ + /* + * Check for illegal characters + */ for (i = 0; (c = eqn[i]) != '\0'; i++) { if (islegit(c) == FALSE) { - error_string = sformatf( "Character is not allowed,\ + error_string = sformatf("Character is not allowed,\ %c (octal: %o).", c, c); error_msg(error_string, CONTINUE); return (ERROR); } } -/* - * Find coefficients, name, and charge for each species for lhs - */ + /* + * Find coefficients, name, and charge for each species for lhs + */ count_trxn = 0; trxn.dz[0] = trxn.dz[1] = trxn.dz[2] = 0.0; cptr = eqn; @@ -61,7 +60,7 @@ parse_eq(char *eqn, struct elt_list **elt_ptr, int association) break; if (c == '\0') { - error_string = sformatf( "Equation has no equal sign.\n\t%s", eqn); + error_string = sformatf("Equation has no equal sign.\n\t%s", eqn); error_msg(error_string, CONTINUE); return (ERROR); } @@ -76,9 +75,9 @@ parse_eq(char *eqn, struct elt_list **elt_ptr, int association) } count_trxn++; } -/* - * Get coefficient, name, and charge of species for dissociation reaction - */ + /* + * Get coefficient, name, and charge of species for dissociation reaction + */ cptr++; if (association == TRUE) { @@ -88,7 +87,7 @@ parse_eq(char *eqn, struct elt_list **elt_ptr, int association) } trxn.token[count_trxn].coef *= -1.0; /* Swap species into first structure position */ - const char * char_ptr = trxn.token[0].name; + const char* char_ptr = trxn.token[0].name; coef = trxn.token[0].coef; l_z = trxn.token[0].z; trxn.token[0].name = trxn.token[count_trxn].name; @@ -99,9 +98,9 @@ parse_eq(char *eqn, struct elt_list **elt_ptr, int association) trxn.token[count_trxn].z = l_z; count_trxn++; } -/* - * Get reaction species from rhs of equation - */ + /* + * Get reaction species from rhs of equation + */ c = cptr[0]; for (;;) { @@ -118,55 +117,47 @@ parse_eq(char *eqn, struct elt_list **elt_ptr, int association) } count_trxn++; } -/* - * Sort list of reaction species - */ + /* + * Sort list of reaction species + */ trxn_sort(); -/* - * Get elements in species or mineral formula - */ + /* + * Get elements in species or mineral formula + */ count_elts = 0; strcpy(token, trxn.token[0].name); replace("(s)", "", token); replace("(S)", "", token); replace("(g)", "", token); replace("(G)", "", token); - const char *char_ptr = token; + const char* char_ptr = token; if (get_elts_in_species(&char_ptr, trxn.token[0].coef) == ERROR) { return (ERROR); } -/* - * Sort elements in reaction and combine - */ + /* + * Sort elements in reaction and combine + */ if (elt_list_combine() == ERROR) return (ERROR); -/* - * Malloc space and store element data for return - */ - *elt_ptr = (struct elt_list *) PHRQ_malloc((count_elts + 1) * - sizeof(struct elt_list)); - if (*elt_ptr == NULL) + /* + * Malloc space and store element data for return + */ + new_elt_list.resize(count_elts + 1); + for (i = 0; i < count_elts; i++) { - malloc_error(); + new_elt_list[i].elt = elt_list[i].elt; + new_elt_list[i].coef = -elt_list[i].coef; } - else - { - for (i = 0; i < count_elts; i++) - { - (*elt_ptr)[i].elt = elt_list[i].elt; - (*elt_ptr)[i].coef = -elt_list[i].coef; - } - (*elt_ptr)[count_elts].elt = NULL; - } -/* - * Debugging print of parsed equation - trxn_print(); - */ + new_elt_list[count_elts].elt = NULL; + + /* + * Debugging print of parsed equation + trxn_print(); + */ return (OK); } - /* ---------------------------------------------------------------------- */ int Phreeqc:: check_eqn(int association) @@ -571,7 +562,8 @@ get_elts_in_species(const char **t_ptr, LDBLE coef) * output, is next position to start looking * coef input, coefficient to multiply subscripts by */ - int i, count, l; + int l; + size_t count; char c, c1; LDBLE d; std::string element; @@ -643,7 +635,7 @@ get_elts_in_species(const char **t_ptr, LDBLE coef) { return (ERROR); } - for (i = count; i < count_elts; i++) + for (size_t i = count; i < count_elts; i++) { elt_list[i].coef *= d; } @@ -664,7 +656,7 @@ get_elts_in_species(const char **t_ptr, LDBLE coef) { return (ERROR); } - for (i = count; i < count_elts; i++) + for (size_t i = count; i < count_elts; i++) { elt_list[i].coef *= d; } @@ -826,7 +818,8 @@ get_secondary_in_species(const char **t_ptr, LDBLE coef) * output, is next position to start looking * coef input, coefficient to multiply subscripts by */ - int i, count, l; + int l; + size_t count; char c, c1; LDBLE d; char element[MAX_LENGTH]; @@ -895,7 +888,7 @@ get_secondary_in_species(const char **t_ptr, LDBLE coef) { return (ERROR); } - for (i = count; i < count_elts; i++) + for (size_t i = count; i < count_elts; i++) { elt_list[i].coef *= d; } @@ -916,7 +909,7 @@ get_secondary_in_species(const char **t_ptr, LDBLE coef) { return (ERROR); } - for (i = count; i < count_elts; i++) + for (size_t i = count; i < count_elts; i++) { elt_list[i].coef *= d; } diff --git a/prep.cpp b/prep.cpp index 0f52d5b6..8123150b 100644 --- a/prep.cpp +++ b/prep.cpp @@ -1158,7 +1158,7 @@ build_model(void) */ count_trxn = 0; trxn_add(s[i]->rxn_s, 1.0, false); - if (s[i]->next_secondary == NULL) + if (s[i]->next_secondary.size() == 0) { write_mb_eqn_x(); } @@ -1217,7 +1217,7 @@ build_model(void) /* * Build list of species for summing and printing */ - if (s[i]->next_secondary == NULL) + if (s[i]->next_secondary.size() == 0) { write_mb_for_species_list(i); } @@ -5140,9 +5140,8 @@ write_mb_for_species_list(int n) } } elt_list_combine(); - s[n]->next_sys_total = - (struct elt_list *) free_check_null(s[n]->next_sys_total); - s[n]->next_sys_total = elt_list_save(); + s[n]->next_sys_total.clear(); + s[n]->next_sys_total = elt_list_vsave(); return (OK); } @@ -5193,9 +5192,8 @@ write_phase_sys_total(int n) } } elt_list_combine(); - phases[n]->next_sys_total = - (struct elt_list *) free_check_null(phases[n]->next_sys_total); - phases[n]->next_sys_total = elt_list_save(); + phases[n]->next_sys_total.clear(); + phases[n]->next_sys_total = elt_list_vsave(); return (OK); } /* ---------------------------------------------------------------------- */ diff --git a/read.cpp b/read.cpp index 5b2f4bdc..89cc689f 100644 --- a/read.cpp +++ b/read.cpp @@ -395,7 +395,7 @@ read_exchange_species(void) struct phase *phase_ptr; struct species *s_ptr; - struct elt_list *next_elt; + const struct elt_list *next_elt; //LDBLE exchange_coef; LDBLE offset; @@ -495,9 +495,8 @@ read_exchange_species(void) s_ptr->mole_balance = string_hsave(token); cptr = token; get_secondary_in_species(&cptr, 1.0); - s_ptr->next_secondary = - (struct elt_list *) free_check_null(s_ptr->next_secondary); - s_ptr->next_secondary = elt_list_save(); + s_ptr->next_secondary.clear(); + s_ptr->next_secondary = elt_list_vsave(); /* debug for (i = 0; i < count_elts; i++) { output_msg(sformatf("%s\t%f\n", elt_list[i].elt->name, @@ -709,31 +708,32 @@ read_exchange_species(void) break; case OPTION_DEFAULT: -/* - * Get exchange species information and parse equation - */ + /* + * Get exchange species information and parse equation + */ + { s_ptr = NULL; - if (parse_eq(line, &next_elt, association) == ERROR) + std::vector new_elt_list; + if (parse_eq(line, new_elt_list, association) == ERROR) { - parse_error++; - error_msg("Parsing equation.", CONTINUE); - error_msg(line_save, CONTINUE); - break; - } -/* - * Get pointer to each species in the reaction, store new species if necessary - */ - trxn.token[0].s = - s_store(trxn.token[0].name, trxn.token[0].z, TRUE); + parse_error++; + error_msg("Parsing equation.", CONTINUE); + error_msg(line_save, CONTINUE); + break; + } + /* + * Get pointer to each species in the reaction, store new species if necessary + */ + trxn.token[0].s = s_store(trxn.token[0].name, trxn.token[0].z, TRUE); for (i = 1; i < count_trxn; i++) { - trxn.token[i].s = - s_store(trxn.token[i].name, trxn.token[i].z, FALSE); + trxn.token[i].s = s_store(trxn.token[i].name, trxn.token[i].z, FALSE); } -/* - * Save element list and carbon, hydrogen, and oxygen in species - */ - trxn.token[0].s->next_elt = next_elt; + /* + * Save element list and carbon, hydrogen, and oxygen in species + */ + trxn.token[0].s->next_elt = new_elt_list; + next_elt = &trxn.token[0].s->next_elt[0]; for (; next_elt->elt != NULL; next_elt++) { if (strcmp(next_elt->elt->name, "C") == 0) @@ -749,31 +749,31 @@ read_exchange_species(void) trxn.token[0].s->o = next_elt->coef; } } -/* - * Copy reaction to reaction for species - */ + /* + * Copy reaction to reaction for species + */ trxn_copy(trxn.token[0].s->rxn); -/* - * Set type for species - */ + /* + * Set type for species + */ trxn.token[0].s->type = EX; s_ptr = trxn.token[0].s; -/* - * Set gamma data - */ + /* + * Set gamma data + */ s_ptr->gflag = 4; s_ptr->exch_gflag = 3; s_ptr->dha = 0.0; s_ptr->dhb = 0.0; opt_save = OPTION_DEFAULT; -/* - * Save as a phase for inverse modeling only - */ + /* + * Save as a phase for inverse modeling only + */ phase_ptr = phase_store(s_ptr->name); if (phase_ptr == NULL) { input_error++; - error_string = sformatf( "Copying exchange to phases."); + error_string = sformatf("Copying exchange to phases."); error_msg(error_string, CONTINUE); } else @@ -781,10 +781,11 @@ read_exchange_species(void) phase_ptr->formula = s_ptr->name; phase_ptr->check_equation = FALSE; phase_ptr->type = EX; - phase_ptr->next_elt = elt_list_dup(s_ptr->next_elt); + phase_ptr->next_elt = s_ptr->next_elt; phase_ptr->rxn = s_ptr->rxn; } - break; + } + break; } if (return_value == EOF || return_value == KEYWORD) break; @@ -3471,7 +3472,6 @@ read_phases(void) char token[MAX_LENGTH]; char token1[MAX_LENGTH]; struct phase *phase_ptr; - struct elt_list *next_elt; struct rxn_token *token_ptr; int return_value, opt, opt_save; @@ -3633,15 +3633,16 @@ read_phases(void) opt_save = OPTION_DEFAULT; break; case OPTION_DEFAULT: -/* - * Get element name and save pointer to character string - */ + { + /* + * Get element name and save pointer to character string + */ phase_ptr = NULL; cptr = line; copy_token(token, &cptr, &l); -/* - * Get and parse equation - */ + /* + * Get and parse equation + */ j = check_line("Phase equation", FALSE, TRUE, TRUE, TRUE); if (j == EOF || j == KEYWORD) { @@ -3657,7 +3658,8 @@ read_phases(void) error_msg(line_save, CONTINUE); break; } - if (parse_eq(line, &next_elt, association) == ERROR) + std::vector new_elt_list; + if (parse_eq(line, new_elt_list, association) == ERROR) { parse_error++; error_msg("Parsing equation.", CONTINUE); @@ -3665,9 +3667,9 @@ read_phases(void) break; } phase_ptr = phase_store(token); -/* - * Get pointer to each species in the reaction, store new species if necessary - */ + /* + * Get pointer to each species in the reaction, store new species if necessary + */ strcpy(token1, trxn.token[0].name); replace("(g)", "", token1); replace("(s)", "", token1); @@ -3693,29 +3695,26 @@ read_phases(void) trxn.token[i].s = NULL; } } -/* - * Save element list - */ - phase_ptr->next_elt = next_elt; -/* - * Malloc space for phase reaction - */ - //phase_ptr->rxn = rxn_alloc(count_trxn + 1); -/* - * Copy reaction to reaction for phase, first token (token[0]) is not used - * except to check that coef of phase formula = 1.0 - */ + /* + * Save element list + */ + phase_ptr->next_elt = new_elt_list; + /* + * Copy reaction to reaction for phase, first token (token[0]) is not used + * except to check that coef of phase formula = 1.0 + */ trxn_copy(phase_ptr->rxn); token_ptr = &phase_ptr->rxn.token[0]; token_ptr[0].name = trxn.token[1].name; token_ptr[i].s = NULL; token_ptr[i].name = NULL; -/* - * Set type for phase - */ + /* + * Set type for phase + */ phase_ptr->type = SOLID; opt_save = OPTION_DEFAULT; - break; + } + break; } if (return_value == EOF || return_value == KEYWORD) break; @@ -5176,7 +5175,7 @@ read_species(void) int i; int association; struct species *s_ptr; - struct elt_list *next_elt; + const struct elt_list *next_elt; const char* cptr; char token[MAX_LENGTH]; //bool vm_read = false; @@ -5302,10 +5301,9 @@ read_species(void) copy_token(token, &next_char, &i); s_ptr->mole_balance = string_hsave(token); cptr = token; - s_ptr->next_secondary = - (struct elt_list *) free_check_null(s_ptr->next_secondary); + s_ptr->next_secondary.clear(); get_secondary_in_species(&cptr, 1.0); - s_ptr->next_secondary = elt_list_save(); + s_ptr->next_secondary = elt_list_vsave(); /* debug for (i = 0; i < count_elts; i++) { output_msg(sformatf("%s\t%f\n", elt_list[i].elt->name, @@ -5556,20 +5554,22 @@ read_species(void) opt_save = OPTION_DEFAULT; break; case OPTION_DEFAULT: -/* - * Get space for species information and parse equation - */ + { + /* + * Get space for species information and parse equation + */ s_ptr = NULL; - if (parse_eq(line, &next_elt, association) == ERROR) + std::vector new_elt_list; + if (parse_eq(line, new_elt_list, association) == ERROR) { parse_error++; error_msg("Parsing equation.", CONTINUE); error_msg(line_save, CONTINUE); break; } -/* - * Get pointer to each species in the reaction, store new species if necessary - */ + /* + * Get pointer to each species in the reaction, store new species if necessary + */ trxn.token[0].s = s_store(trxn.token[0].name, trxn.token[0].z, TRUE); for (i = 1; i < count_trxn; i++) @@ -5577,11 +5577,12 @@ read_species(void) trxn.token[i].s = s_store(trxn.token[i].name, trxn.token[i].z, FALSE); } -/* - * Save element list and carbon, hydrogen, and oxygen in species - */ - trxn.token[0].s->next_elt = next_elt; - trxn.token[0].s->next_secondary = NULL; + /* + * Save element list and carbon, hydrogen, and oxygen in species + */ + trxn.token[0].s->next_elt = new_elt_list; + trxn.token[0].s->next_secondary.clear(); + next_elt = &trxn.token[0].s->next_elt[0]; for (; next_elt->elt != NULL; next_elt++) { if (strcmp(next_elt->elt->name, "C") == 0) @@ -5597,14 +5598,14 @@ read_species(void) trxn.token[0].s->o = next_elt->coef; } } -/* - * Copy reaction to reaction for species - */ + /* + * Copy reaction to reaction for species + */ trxn_copy(trxn.token[0].s->rxn); s_ptr = trxn.token[0].s; -/* - * Default gamma data - */ + /* + * Default gamma data + */ s_ptr->dha = 0.0; s_ptr->dhb = 0.0; if (equal(s_ptr->z, 0.0, TOL) == TRUE) @@ -5616,9 +5617,9 @@ read_species(void) { s_ptr->gflag = 1; /* Davies */ } -/* - * Set type for species - */ + /* + * Set type for species + */ if (strcmp(trxn.token[0].s->name, "H+") == 0) { s_hplus = trxn.token[0].s; @@ -5660,7 +5661,8 @@ read_species(void) trxn.token[0].s->type = AQ; } opt_save = OPTION_DEFAULT; - break; + } + break; } if (return_value == EOF || return_value == KEYWORD) break; @@ -5903,7 +5905,7 @@ read_surface_species(void) LDBLE offset; struct species *s_ptr; - struct elt_list *next_elt; + const struct elt_list *next_elt; int return_value, opt, opt_save; const char* next_char; @@ -5998,10 +6000,9 @@ read_surface_species(void) copy_token(token, &next_char, &i); s_ptr->mole_balance = string_hsave(token); cptr = token; - s_ptr->next_secondary = - (struct elt_list *) free_check_null(s_ptr->next_secondary); + s_ptr->next_secondary.clear(); get_secondary_in_species(&cptr, 1.0); - s_ptr->next_secondary = elt_list_save(); + s_ptr->next_secondary = elt_list_vsave(); /* debug for (i = 0; i < count_elts; i++) { output_msg(sformatf("%s\t%f\n", elt_list[i].elt->name, @@ -6175,11 +6176,13 @@ read_surface_species(void) opt_save = OPTION_DEFAULT; break; case OPTION_DEFAULT: + { /* * Get surface species information and parse equation */ s_ptr = NULL; - if (parse_eq(line, &next_elt, association) == ERROR) + std::vector new_elt_list; + if (parse_eq(line, new_elt_list, association) == ERROR) { parse_error++; error_msg("Parsing equation.", CONTINUE); @@ -6189,8 +6192,7 @@ read_surface_species(void) /* * Get pointer to each species in the reaction, store new species if necessary */ - trxn.token[0].s = - s_store(trxn.token[0].name, trxn.token[0].z, TRUE); + trxn.token[0].s = s_store(trxn.token[0].name, trxn.token[0].z, TRUE); for (i = 1; i < count_trxn; i++) { trxn.token[i].s = @@ -6199,7 +6201,8 @@ read_surface_species(void) /* * Save element list and carbon, hydrogen, and oxygen in species */ - trxn.token[0].s->next_elt = next_elt; + trxn.token[0].s->next_elt = new_elt_list; + next_elt = &trxn.token[0].s->next_elt[0]; for (; next_elt->elt != NULL; next_elt++) { if (strcmp(next_elt->elt->name, "C") == 0) @@ -6231,7 +6234,8 @@ read_surface_species(void) s_ptr->dha = 0.0; s_ptr->dhb = 0.0; opt_save = OPTION_DEFAULT; - break; + } + break; } if (return_value == EOF || return_value == KEYWORD) break; @@ -7018,7 +7022,7 @@ add_psi_master_species(char *token) paren_count = 0; cptr = token; get_elts_in_species(&cptr, 1.0); - master[count_master]->s->next_elt = elt_list_save(); + master[count_master]->s->next_elt = elt_list_vsave(); master[count_master]->s->type = plane; master[count_master]->primary = TRUE; diff --git a/step.cpp b/step.cpp index 8fff4880..8ae534c2 100644 --- a/step.cpp +++ b/step.cpp @@ -238,9 +238,9 @@ step(LDBLE step_fraction) { int n; struct phase *p_ptr = phase_bsearch((it->first).c_str(), &n, FALSE); - struct elt_list *e_ptr; + const struct elt_list *e_ptr; LDBLE min = 1e10; - for (e_ptr = p_ptr->next_elt; e_ptr->elt != NULL; e_ptr++) + for (e_ptr = &p_ptr->next_elt[0]; e_ptr->elt != NULL; e_ptr++) { std::string e(e_ptr->elt->primary->elt->name); cxxNameDouble::iterator st = sys_tots.find(e.c_str()); @@ -266,9 +266,9 @@ step(LDBLE step_fraction) int n; struct phase *p_ptr = phase_bsearch(comp_ptr->Get_name().c_str(), &n, FALSE); - struct elt_list *e_ptr; + const struct elt_list *e_ptr; LDBLE min = 1e10; - for (e_ptr = p_ptr->next_elt; e_ptr->elt != NULL; e_ptr++) + for (e_ptr = &p_ptr->next_elt[0]; e_ptr->elt != NULL; e_ptr++) { std::string e(e_ptr->elt->primary->elt->name); cxxNameDouble::iterator st = sys_tots.find(e.c_str()); diff --git a/structures.cpp b/structures.cpp index 5d6b404b..7cada66b 100644 --- a/structures.cpp +++ b/structures.cpp @@ -370,58 +370,6 @@ elt_list_compare(const void *ptr1, const void *ptr2) b = (const struct elt_list *) ptr2; return (strncmp(a->elt->name, b->elt->name, MAX_LENGTH)); } - -/* ---------------------------------------------------------------------- */ -struct elt_list * Phreeqc:: -elt_list_dup(struct elt_list *elt_list_ptr_old) -/* ---------------------------------------------------------------------- */ -{ -/* - * Duplicates the elt_list structure pointed to by elt_list_ptr_old. - */ - int i, count_totals; - struct elt_list *elt_list_ptr_new; -/* - * Count totals data and copy - */ - if (elt_list_ptr_old == NULL) - return (NULL); - for (i = 0; elt_list_ptr_old[i].elt != NULL; i++); - count_totals = i; -/* - * Malloc space and store element data - */ - elt_list_ptr_new = (struct elt_list *) PHRQ_malloc( - (count_totals + 1) * sizeof(struct elt_list)); - if (elt_list_ptr_new == NULL) - malloc_error(); - memcpy(elt_list_ptr_new, elt_list_ptr_old, - (count_totals + 1) * sizeof(struct elt_list)); - return (elt_list_ptr_new); -} - -/* ---------------------------------------------------------------------- */ -int Phreeqc:: -elt_list_print(struct elt_list *elt_list_ptr) -/* ---------------------------------------------------------------------- */ -{ -/* - * Duplicates the elt_list structure pointed to by elt_list_ptr_old. - */ - int i; -/* - * Debug print for element list - */ - if (elt_list_ptr == NULL) - return (ERROR); - output_msg(sformatf( "Elt_list\n")); - for (i = 0; elt_list_ptr[i].elt != NULL; i++) - { - output_msg(sformatf( "\t%s\t%e\n", elt_list_ptr[i].elt->name, - (double) elt_list_ptr[i].coef)); - } - return (OK); -} /* ---------------------------------------------------------------------- */ cxxNameDouble Phreeqc:: elt_list_NameDouble(void) @@ -438,69 +386,32 @@ elt_list_NameDouble(void) return (nd); } /* ---------------------------------------------------------------------- */ -struct elt_list * Phreeqc:: -elt_list_save(void) -/* ---------------------------------------------------------------------- */ +std::vector Phreeqc:: + elt_list_vsave(void) + /* ---------------------------------------------------------------------- */ { -/* - * Takes data from work space elt_list, allocates a new elt_list structure, - * copies data from work space to new structure, and returns pointer to - * new structure. - */ - int j; - struct elt_list *elt_list_ptr; -/* - * Sort elements in reaction and combine - */ + /* + * Takes data from work space elt_list, allocates a new elt_list structure, + * copies data from work space to new structure, and returns pointer to + * new structure. + */ + size_t j; + std::vector new_elt_list; + /* + * Sort elements in reaction and combine + */ elt_list_combine(); -/* - * Malloc space and store element data - */ - elt_list_ptr = (struct elt_list*)PHRQ_malloc( - (count_elts + 1) * sizeof(struct elt_list)); - if (elt_list_ptr == NULL) + /* + * Malloc space and store element data + */ + new_elt_list.resize(count_elts + 1); + for (j = 0; j < count_elts; j++) { - malloc_error(); + new_elt_list[j].elt = elt_list[j].elt; + new_elt_list[j].coef = elt_list[j].coef; } - else - { - for (j = 0; j < count_elts; j++) - { - elt_list_ptr[j].elt = elt_list[j].elt; - elt_list_ptr[j].coef = elt_list[j].coef; - } - elt_list_ptr[count_elts].elt = NULL; - } - return (elt_list_ptr); -} -/* ---------------------------------------------------------------------- */ -struct elt_list * Phreeqc:: -NameDouble2elt_list(const cxxNameDouble &nd) -/* ---------------------------------------------------------------------- */ -{ -/* - * Takes NameDouble allocates space and fills new elt_list struct - */ - struct elt_list *elt_list_ptr = (struct elt_list *) PHRQ_malloc( - (nd.size() + 1) * sizeof(struct elt_list)); - if (elt_list_ptr == NULL) - { - malloc_error(); - } - else - { - cxxNameDouble::const_iterator it = nd.begin(); - int i = 0; - for( ; it != nd.end(); it++) - { - elt_list_ptr[i].elt = element_store(it->first.c_str()); - elt_list_ptr[i].coef = it->second; - i++; - } - elt_list_ptr[i].elt = NULL; - elt_list_ptr[i].coef = 0; - } - return (elt_list_ptr); + new_elt_list[count_elts].elt = NULL; + return new_elt_list; } /* ********************************************************************** * @@ -1053,10 +964,8 @@ phase_free(struct phase *phase_ptr) */ if (phase_ptr == NULL) return (ERROR); - phase_ptr->next_elt = - (struct elt_list *) free_check_null(phase_ptr->next_elt); - phase_ptr->next_sys_total = - (struct elt_list *) free_check_null(phase_ptr->next_sys_total); + phase_ptr->next_elt.clear(); + phase_ptr->next_sys_total.clear();; phase_ptr->add_logk.clear(); return (OK); } @@ -1149,8 +1058,6 @@ phase_init(struct phase *phase_ptr) phase_ptr->pr_si_f = 0; phase_ptr->pr_in = false; phase_ptr->type = SOLID; - phase_ptr->next_elt = NULL; - phase_ptr->next_sys_total = NULL; phase_ptr->check_equation = TRUE; phase_ptr->replaced = 0; phase_ptr->in_system = 1; @@ -1451,15 +1358,10 @@ s_free(struct species *s_ptr) */ if (s_ptr == NULL) return (ERROR); - s_ptr->next_elt = (struct elt_list *) free_check_null(s_ptr->next_elt); - s_ptr->next_secondary = - (struct elt_list *) free_check_null(s_ptr->next_secondary); - s_ptr->next_sys_total = - (struct elt_list *) free_check_null(s_ptr->next_sys_total); + s_ptr->next_elt.clear(); + s_ptr->next_secondary.clear(); + s_ptr->next_sys_total.clear(); s_ptr->add_logk.clear(); - //rxn_free(s_ptr->rxn); - //rxn_free(s_ptr->rxn_s); - //rxn_free(s_ptr->rxn_x); return (OK); } @@ -1524,9 +1426,6 @@ s_init(struct species *s_ptr) s_ptr->type = 0; s_ptr->gflag = 0; s_ptr->exch_gflag = 0; - s_ptr->next_elt = NULL; - s_ptr->next_secondary = NULL; - s_ptr->next_sys_total = NULL; s_ptr->check_equation = TRUE; s_ptr->tot_g_moles = 0; s_ptr->tot_dh2o_moles = 0; @@ -1654,7 +1553,7 @@ isotope_compare(const void *ptr1, const void *ptr2) * * ********************************************************************** */ /* ---------------------------------------------------------------------- */ - int Phreeqc:: +int Phreeqc:: species_list_compare(const void *ptr1, const void *ptr2) /* ---------------------------------------------------------------------- */ { diff --git a/tally.cpp b/tally.cpp index bd06c3cc..e666af3c 100644 --- a/tally.cpp +++ b/tally.cpp @@ -316,14 +316,12 @@ free_tally_table(void) return (OK); for (i = 0; i < count_tally_table_columns; i++) { - if (tally_table[i].formula != NULL) - tally_table[i].formula = - (struct elt_list *) free_check_null(tally_table[i].formula); + if (tally_table[i].formula.size() != 0) + tally_table[i].formula.clear(); for (k = 0; k < 3; k++) { - tally_table[i].total[k] = - (struct tally_buffer *) free_check_null(tally_table[i]. - total[k]); + tally_table[i].total[k] = (struct tally_buffer *) free_check_null( + tally_table[i].total[k]); } } tally_table = (struct tally *) free_check_null(tally_table); @@ -915,7 +913,7 @@ build_tally_table(void) add_elt_list(phase_ptr->next_elt, 1.0); } elt_list_combine(); - tally_table[n].formula = elt_list_save(); + tally_table[n].formula = elt_list_vsave(); } } } @@ -965,7 +963,7 @@ build_tally_table(void) strcpy(token, phase_ptr->formula); add_elt_list(phase_ptr->next_elt, 1.0); elt_list_combine(); - tally_table[n].formula = elt_list_save(); + tally_table[n].formula = elt_list_vsave(); } } } @@ -1029,7 +1027,7 @@ build_tally_table(void) } } elt_list_combine(); - tally_table[n].formula = elt_list_save(); + tally_table[n].formula = elt_list_vsave(); } } } @@ -1228,7 +1226,6 @@ extend_tally_table(void) tally_table[count_tally_table_columns].type = UnKnown; tally_table[count_tally_table_columns].add_formula = NULL; tally_table[count_tally_table_columns].moles = 0.0; - tally_table[count_tally_table_columns].formula = NULL; count_tally_table_columns++; return (OK); } diff --git a/tidy.cpp b/tidy.cpp index 4165a823..b521dc36 100644 --- a/tidy.cpp +++ b/tidy.cpp @@ -455,7 +455,7 @@ check_species_input(void) return_value = OK; for (i = 0; i < (int)s.size(); i++) { - if (s[i]->next_elt == NULL) + if (s[i]->next_elt.size() == 0) { input_error++; return_value = ERROR; @@ -665,12 +665,12 @@ coef_in_master(struct master * master_ptr) LDBLE coef; const char* cptr; std::string elt_name; - struct elt_list *next_elt; + const struct elt_list *next_elt; coef = 0.0; cptr = master_ptr->elt->name; get_elt(&cptr, elt_name, &l); - for (next_elt = master_ptr->s->next_elt; next_elt->elt != NULL; + for (next_elt = &master_ptr->s->next_elt[0]; next_elt->elt != NULL; next_elt++) { if (strcmp(elt_name.c_str(), next_elt->elt->name) == 0) @@ -1104,7 +1104,7 @@ tidy_inverse(void) LDBLE value; struct master *master_ptr; struct master *master_alk_ptr; - struct elt_list *elt_list_ptr; + const struct elt_list *elt_list_ptr; master_alk_ptr = master_bsearch("Alkalinity"); for (i = 0; i < count_inverse; i++) { @@ -1236,7 +1236,7 @@ tidy_inverse(void) inverse[i].phases[j].isotopes[k].primary = master_ptr; inverse[i].phases[j].isotopes[k].master = master_ptr; /* find coefficient for element */ - for (elt_list_ptr = inverse[i].phases[j].phase->next_elt; + for (elt_list_ptr = &inverse[i].phases[j].phase->next_elt[0]; elt_list_ptr->elt != NULL; elt_list_ptr++) { if (elt_list_ptr->elt == master_ptr->elt) @@ -1562,7 +1562,7 @@ tidy_pp_assemblage(void) } if (it->second.Get_add_formula().size() > 0) { - int first = count_elts; + size_t first = count_elts; phase_ptr = phase_bsearch(it->second.Get_add_formula().c_str(), &k, FALSE); if (phase_ptr != NULL) { @@ -1573,7 +1573,7 @@ tidy_pp_assemblage(void) get_elts_in_species(&cptr, coef); } /* check that all elements are in the database */ - for (int l = first; l < count_elts; l++) + for (size_t l = first; l < count_elts; l++) { if (elt_list[l].elt->master == NULL) { @@ -2436,7 +2436,7 @@ tidy_species(void) */ for (i = 0; i < (int)s.size(); i++) { - if (s[i]->next_secondary != NULL) + if (s[i]->next_secondary.size() != 0) { s[i]->h = 0.0; s[i]->o = 0.0; @@ -3970,8 +3970,8 @@ tidy_kin_surface(void) { cxxKinetics *kinetics_ptr; struct phase *phase_ptr; - struct elt_list *elt_list_kinetics; - int count_elts_kinetics; + std::vector elt_list_kinetics; + size_t count_elts_kinetics; //std::map::iterator it; //for (it = Rxn_surface_map.begin(); it != Rxn_surface_map.end(); it++) @@ -4138,7 +4138,7 @@ tidy_kin_surface(void) { elt_list_combine(); } - elt_list_kinetics = elt_list_save(); + elt_list_kinetics = elt_list_vsave(); count_elts_kinetics = count_elts; /* get surface formulas */ @@ -4223,8 +4223,7 @@ tidy_kin_surface(void) } } } - elt_list_kinetics = - (struct elt_list *) free_check_null(elt_list_kinetics); + elt_list_kinetics.clear(); } } return (OK); diff --git a/utilities.cpp b/utilities.cpp index 1f008b01..eaf63b18 100644 --- a/utilities.cpp +++ b/utilities.cpp @@ -6,122 +6,6 @@ #include "Solution.h" #include -/* ---------------------------------------------------------------------- */ -int Phreeqc:: -add_elt_list(struct elt_list *elt_list_ptr, LDBLE coef) -/* ---------------------------------------------------------------------- */ -{ - struct elt_list *elt_list_ptr1; - - if (elt_list_ptr == NULL) - return (OK); - - for (elt_list_ptr1 = elt_list_ptr; elt_list_ptr1->elt != NULL; - elt_list_ptr1++) - { - if (count_elts >= (int)elt_list.size()) - { - elt_list.resize(count_elts + 1); - } - elt_list[count_elts].elt = elt_list_ptr1->elt; - elt_list[count_elts].coef = elt_list_ptr1->coef * coef; - count_elts++; - } - return (OK); -} - -/* ---------------------------------------------------------------------- */ -int Phreeqc:: -add_elt_list_multi_surf(struct elt_list *elt_list_ptr, LDBLE coef, struct element *surf_elt_ptr) -/* ---------------------------------------------------------------------- */ -{ - struct elt_list *elt_list_ptr1; - - if (elt_list_ptr == NULL || surf_elt_ptr == NULL) - return (OK); - - // determine if surf_elt_ptr is first surface - bool first_surface = true; - for (elt_list_ptr1 = elt_list_ptr; elt_list_ptr1->elt != NULL; - elt_list_ptr1++) - { - if (elt_list_ptr1->elt->master->type == SURF) - { - if (elt_list_ptr1->elt == surf_elt_ptr) - { - first_surface = true; - break; - } - else - { - first_surface = false; - break; - } - } - } - if (first_surface) - { - for (elt_list_ptr1 = elt_list_ptr; elt_list_ptr1->elt != NULL; - elt_list_ptr1++) - { - if (count_elts >= (int)elt_list.size()) - { - elt_list.resize(count_elts + 1); - } - if (elt_list_ptr1->elt == surf_elt_ptr) - { - elt_list[count_elts].elt = elt_list_ptr1->elt; - elt_list[count_elts].coef = elt_list_ptr1->coef * coef; - count_elts++; - } - else if (elt_list_ptr1->elt->master->type == SURF) - { - continue; - } - else - { - elt_list[count_elts].elt = elt_list_ptr1->elt; - elt_list[count_elts].coef = elt_list_ptr1->coef * coef; - count_elts++; - } - } - } - else - { - for (elt_list_ptr1 = elt_list_ptr; elt_list_ptr1->elt != NULL; - elt_list_ptr1++) - { - if (count_elts >= (int)elt_list.size()) - { - elt_list.resize(count_elts + 1); - } - if (elt_list_ptr1->elt == surf_elt_ptr) - { - elt_list[count_elts].elt = elt_list_ptr1->elt; - elt_list[count_elts].coef = elt_list_ptr1->coef * coef; - count_elts++; - } - } - } - return (OK); -} -int Phreeqc:: -add_elt_list(const cxxNameDouble & nd, LDBLE coef) -/* ---------------------------------------------------------------------- */ -{ - cxxNameDouble::const_iterator cit = nd.begin(); - for ( ; cit != nd.end(); cit++) - { - if (count_elts >= (int)elt_list.size()) - { - elt_list.resize(count_elts + 1); - } - elt_list[count_elts].elt = element_store(cit->first.c_str()); - elt_list[count_elts].coef = cit->second * coef; - count_elts++; - } - return (OK); -} /* ---------------------------------------------------------------------- */ LDBLE Phreeqc:: From 006d1de970be236038975f06208e943d83857c47 Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Fri, 2 Apr 2021 13:52:30 -0600 Subject: [PATCH 38/53] reorganizing --- CReaction.cpp | 1 + CReaction.h | 6 +- Classes.cpp | 343 -------------------------------------------- Phreeqc.h | 86 +++++------ prep.cpp | 42 ------ structures.cpp | 381 +++++++++++++++++++++++++++++++++++++++++++++---- utilities.cpp | 215 ++++++++++++++++++---------- 7 files changed, 536 insertions(+), 538 deletions(-) diff --git a/CReaction.cpp b/CReaction.cpp index 8ddbd29c..f357b3d4 100644 --- a/CReaction.cpp +++ b/CReaction.cpp @@ -19,3 +19,4 @@ void CReaction::Set_dz(double* d) { for (size_t i = 0; i < 3; i++) dz[i] = d[i]; } + diff --git a/CReaction.h b/CReaction.h index 90375de7..7d4fae1b 100644 --- a/CReaction.h +++ b/CReaction.h @@ -29,8 +29,8 @@ public: }; struct rxn_token { - struct species* s; - LDBLE coef; - const char* name; + struct species* s = NULL; + LDBLE coef = 0.0; + const char* name = NULL; }; #endif // !defined(CREACTION_H_INCLUDED) \ No newline at end of file diff --git a/Classes.cpp b/Classes.cpp index 8038ed2b..f34e094c 100644 --- a/Classes.cpp +++ b/Classes.cpp @@ -1,348 +1,5 @@ #include "Phreeqc.h" #include "CReaction.h" -/* ---------------------------------------------------------------------- */ -double Phreeqc:: -calc_delta_v(CReaction& r_ref, bool phase) -/* ---------------------------------------------------------------------- */ -{ - /* calculate delta_v from molar volumes */ - double d_v = 0.0; - if (phase) - { - /* for phases: reactants have coef's < 0, products have coef's > 0, v.v. for species */ - for (size_t i = 1; r_ref.Get_tokens()[i].s; i++) - { - if (!r_ref.Get_tokens()[i].s) - continue; - d_v += r_ref.Get_tokens()[i].coef * r_ref.Get_tokens()[i].s->logk[vm_tc]; - } - } - else - { - for (size_t i = 0; r_ref.token[i].name /*|| r_ptr->token[i].s*/; i++) - { - if (!r_ref.Get_tokens()[i].s) - continue; - d_v -= r_ref.Get_tokens()[i].coef * r_ref.Get_tokens()[i].s->logk[vm_tc]; - } - } - return d_v; -} -/* ---------------------------------------------------------------------- */ -bool Phreeqc:: -trxn_add(CReaction& r_ref, double coef, bool combine) -/* ---------------------------------------------------------------------- */ -{ - /* - * Adds reactions together. - * - * Global variable count_trxn determines which position in trxn is used. - * If count_trxn=0, then the equation effectively is copied into trxn. - * If count_trxn>0, then new equation is added to existing equation. - * - * Arguments: - * *r_ptr points to rxn structure to add. - * - * coef added equation is multiplied by coef. - * combine if TRUE, reaction is reaction is sorted and - * like terms combined. - */ - /* - * Accumulate log k for reaction - */ - if (count_trxn == 0) - { - for (int i = 0; i < MAX_LOG_K_INDICES; i++) trxn.logk[i] = r_ref.Get_logk()[i]; - for (int i = 0; i < 3; i++) trxn.dz[i] = r_ref.Get_dz()[i]; - } - else - { - for (int i = 0; i < MAX_LOG_K_INDICES; i++) trxn.logk[i] += coef * r_ref.Get_logk()[i]; - for (int i = 0; i < 3; i++) trxn.dz[i] += coef * r_ref.Get_dz()[i]; - } - /* - * Copy equation into work space - */ - struct rxn_token* next_token = &r_ref.token[0]; - while (next_token->s != NULL) - { - if (count_trxn + 1 > trxn.token.size()) - trxn.token.resize(count_trxn + 1); - trxn.token[count_trxn].name = next_token->s->name; - trxn.token[count_trxn].s = next_token->s; - trxn.token[count_trxn].coef = coef * next_token->coef; - count_trxn++; - next_token++; - } - if (combine == TRUE) - trxn_combine(); - return (OK); -} -/* ---------------------------------------------------------------------- */ -bool Phreeqc:: -trxn_add_phase(CReaction& r_ref, double coef, bool combine) -/* ---------------------------------------------------------------------- */ -{ - /* - * Adds reactions together. - * - * Global variable count_trxn determines which position in trxn is used. - * If count_trxn=0, then the equation effectively is copied into trxn. - * If count_trxn>0, then new equation is added to existing equation. - * - * Arguments: - * *r_ptr points to rxn structure to add. - * - * coef added equation is multiplied by coef. - * combine if TRUE, reaction is reaction is sorted and - * like terms combined. - */ - int i; - struct rxn_token* next_token; - /* - * Accumulate log k for reaction - */ - if (count_trxn == 0) - { - memcpy((void*)trxn.logk, (void*)r_ref.Get_logk(), - (size_t)MAX_LOG_K_INDICES * sizeof(double)); - } - else - { - for (i = 0; i < MAX_LOG_K_INDICES; i++) trxn.logk[i] += coef * r_ref.Get_logk()[i]; - } - /* - * Copy equation into work space - */ - next_token = &r_ref.token[0]; - while (next_token->s != NULL || next_token->name != NULL) - { - if (count_trxn + 1 > trxn.token.size()) - trxn.token.resize(count_trxn + 1); - if (next_token->s != NULL) - { - trxn.token[count_trxn].name = next_token->s->name; - trxn.token[count_trxn].s = next_token->s; - } - else - { - trxn.token[count_trxn].name = next_token->name; - trxn.token[count_trxn].s = NULL; - } - trxn.token[count_trxn].coef = coef * next_token->coef; - count_trxn++; - next_token++; - } - if (combine) - trxn_combine(); - return (OK); -} -/* ---------------------------------------------------------------------- */ -bool Phreeqc:: -trxn_copy(CReaction& rxn_ref) -/* ---------------------------------------------------------------------- */ -{ - /* - * Copies trxn to a reaction structure. - * - * Input: rxn_ptr, pointer to reaction structure to copy trxn to. - * - */ - int i; - /* - * Copy logk data - */ - for (i = 0; i < MAX_LOG_K_INDICES; i++) - { - rxn_ref.logk[i] = trxn.logk[i]; - } - /* - * Copy dz data - */ - for (i = 0; i < 3; i++) - { - rxn_ref.dz[i] = trxn.dz[i]; - } - /* - * Copy tokens - */ - rxn_ref.Get_tokens().resize(count_trxn + 1); - for (size_t i = 0; i < count_trxn; i++) - { - rxn_ref.Get_tokens()[i].s = trxn.token[i].s; - rxn_ref.Get_tokens()[i].name = trxn.token[i].name; - rxn_ref.Get_tokens()[i].coef = trxn.token[i].coef; - } - rxn_ref.token[count_trxn].s = NULL; - rxn_ref.token[count_trxn].name = NULL; - return (OK); -} -/* ---------------------------------------------------------------------- */ -bool Phreeqc:: -phase_rxn_to_trxn(struct phase* phase_ptr, CReaction& rxn_ref) -/* ---------------------------------------------------------------------- */ -{ - /* - * Copy reaction from reaction structure to - * temp reaction structure. - */ - int l; - const char* cptr; - LDBLE l_z; - trxn.token.resize(rxn_ref.size()); - trxn.token[0].name = phase_ptr->formula; - /* charge */ - cptr = phase_ptr->formula; - { - std::string token; - get_token(&cptr, token, &l_z, &l); - } - trxn.token[0].z = l_z; - trxn.token[0].s = NULL; - trxn.token[0].unknown = NULL; - /*trxn.token[0].coef = -1.0; */ - /* check for leading coefficient of 1.0 for phase did not work */ - trxn.token[0].coef = phase_ptr->rxn.token[0].coef; - for (size_t i = 1; rxn_ref.token[i].s != NULL; i++) - { - trxn.token[i].name = rxn_ref.token[i].s->name; - trxn.token[i].z = rxn_ref.token[i].s->z; - trxn.token[i].s = NULL; - trxn.token[i].unknown = NULL; - trxn.token[i].coef = rxn_ref.token[i].coef; - count_trxn = i + 1; - } - return (OK); -} -/* ---------------------------------------------------------------------- */ -double Phreeqc:: -rxn_find_coef(CReaction& r_ref, const char* str) -/* ---------------------------------------------------------------------- */ -{ - /* - * Finds coefficient of token in reaction. - * input: r_ptr, pointer to a reaction structure - * str, string to find as reaction token - * - * Return: 0.0, if token not found - * coefficient of token, if found. - */ - struct rxn_token* r_token; - LDBLE coef; - - r_token = &r_ref.token[1]; - coef = 0.0; - while (r_token->s != NULL) - { - if (strcmp(r_token->s->name, str) == 0) - { - coef = r_token->coef; - break; - } - r_token++; - } - return (coef); -} -/* ---------------------------------------------------------------------- */ -double Phreeqc:: -calc_alk(CReaction& rxn_ref) -/* ---------------------------------------------------------------------- */ -{ - LDBLE return_value; - struct master* master_ptr; - - return_value = 0.0; - struct rxn_token* r_token = &rxn_ref.token[1]; - while (r_token->s != NULL) - { - master_ptr = r_token->s->secondary; - if (master_ptr == NULL) - { - master_ptr = r_token->s->primary; - } - if (master_ptr == NULL) - { - error_string = sformatf( - "Non-master species in secondary reaction, %s.", - rxn_ref.token[0].s->name); - error_msg(error_string, CONTINUE); - input_error++; - break; - } - return_value += r_token->coef * master_ptr->alk; - r_token++; - } - return (return_value); -} -CReaction Phreeqc::CReaction_internal_copy(CReaction& rxn_ref) -{ - CReaction rxn; - for (size_t i = 0; i < MAX_LOG_K_INDICES; i++) rxn.logk[i] = rxn_ref.logk[i]; - for (size_t i = 0; i < 3; i++) rxn.dz[i] = rxn_ref.dz[i]; - rxn.Get_tokens().resize(rxn_ref.Get_tokens().size()); - for (size_t i = 0; i < rxn_ref.Get_tokens().size(); i++) - { - rxn.token[i].s = (rxn_ref.token[i].s == NULL) ? NULL : - s_search(rxn_ref.token[i].s->name); - rxn.token[i].coef = rxn_ref.token[i].coef; - rxn.token[i].name = (rxn_ref.token[i].s == NULL) ? NULL : - string_hsave(rxn_ref.token[i].name); - } - return rxn; -} -int Phreeqc:: -add_elt_list(const cxxNameDouble& nd, LDBLE coef) -/* ---------------------------------------------------------------------- */ -{ - cxxNameDouble::const_iterator cit = nd.begin(); - for (; cit != nd.end(); cit++) - { - if (count_elts >= (int)elt_list.size()) - { - elt_list.resize(count_elts + 1); - } - elt_list[count_elts].elt = element_store(cit->first.c_str()); - elt_list[count_elts].coef = cit->second * coef; - count_elts++; - } - return (OK); -} -int Phreeqc:: -add_elt_list(const std::vector& el, double coef) -/* ---------------------------------------------------------------------- */ -{ - const struct elt_list* elt_list_ptr = &el[0]; - - for (; elt_list_ptr->elt != NULL; elt_list_ptr++) - { - if (count_elts >= elt_list.size()) - { - elt_list.resize(count_elts + 1); - } - elt_list[count_elts].elt = elt_list_ptr->elt; - elt_list[count_elts].coef = elt_list_ptr->coef * coef; - count_elts++; - } - return (OK); -} -std::vector Phreeqc:: -elt_list_internal_copy(const std::vector& el) -/* ---------------------------------------------------------------------- */ -{ - std::vector new_elt_list; - const struct elt_list* elt_list_ptr = &el[0]; - - new_elt_list.resize(el.size()); - size_t count = 0; - for (; elt_list_ptr->elt != NULL; elt_list_ptr++) - { - new_elt_list[count].elt = element_store(elt_list_ptr->elt->name); - new_elt_list[count].coef = elt_list_ptr->coef; - count++; - } - new_elt_list[count].elt = NULL; - return new_elt_list; -} \ No newline at end of file diff --git a/Phreeqc.h b/Phreeqc.h index 4babcf7e..c373b41e 100644 --- a/Phreeqc.h +++ b/Phreeqc.h @@ -554,7 +554,6 @@ public: int build_species_list(int n); int build_min_surface(void); LDBLE calc_lk_phase(phase* p_ptr, LDBLE TK, LDBLE pa); - double calc_delta_v(CReaction& r_ref, bool phase); LDBLE calc_PR(std::vector phase_ptrs, LDBLE P, LDBLE TK, LDBLE V_m); LDBLE calc_PR(); int calc_vm(LDBLE tc, LDBLE pa); @@ -814,81 +813,93 @@ public: // structures.cpp ------------------------------- int clean_up(void); int reinitialize(void); + int copier_add(struct copier* copier_ptr, int n_user, int start, int end); int copier_clear(struct copier* copier_ptr); + // + CReaction CReaction_internal_copy(CReaction& rxn_ref); + double rxn_find_coef(CReaction& r_ptr, const char* str); + // static int element_compare(const void* ptr1, const void* ptr2); - -public: + struct element* element_store(const char* element); + // + int add_elt_list(const cxxNameDouble& nd, LDBLE coef); + int add_elt_list(const std::vector& el, double coef); + int change_hydrogen_in_elt_list(LDBLE charge); + int elt_list_combine(void); + static int elt_list_compare(const void* ptr1, const void* ptr2); + std::vector elt_list_internal_copy(const std::vector& el); + std::vector elt_list_vsave(void); + cxxNameDouble elt_list_NameDouble(void); + // enum entity_type get_entity_enum(char* name); + // struct inverse* inverse_alloc(void); int inverse_delete(int i); static int inverse_isotope_compare(const void* ptr1, const void* ptr2); struct inverse* inverse_search(int n_user, int* n); int inverse_sort(void); -protected: + // struct logk* logk_alloc(void); int logk_copy2orig(struct logk* logk_ptr); struct logk* logk_store(const char* name, int replace_if_found); struct logk* logk_search(const char* name); + // struct master* master_alloc(void); static int master_compare(const void* ptr1, const void* ptr2); int master_delete(const char* cptr); -public: struct master* master_bsearch(const char* cptr); struct master* master_bsearch_primary(const char* cptr); struct master* master_bsearch_secondary(const char* cptr); struct master* master_search(const char* cptr, int* n); - struct pe_data* pe_data_alloc(void); -public: - struct pe_data* pe_data_dup(struct pe_data* pe_ptr_old); - struct pe_data* pe_data_free(struct pe_data* pe_data_ptr); -protected: - int pe_data_store(struct pe_data** pe, const char* token); -public: + struct master* surface_get_psi_master(const char* name, int plane); + // struct phase* phase_bsearch(const char* cptr, int* j, int print); -protected: static int phase_compare(const void* ptr1, const void* ptr2); int phase_delete(int i); struct phase* phase_store(const char* name); -public: + // struct rate* rate_bsearch(const char* cptr, int* j); int rate_free(struct rate* rate_ptr); struct rate* rate_copy(struct rate* rate_ptr); struct rate* rate_search(const char* name, int* n); int rate_sort(void); + // static int s_compare(const void* ptr1, const void* ptr2); int s_delete(int i); struct species* s_search(const char* name); struct species* s_store(const char* name, LDBLE z, int replace_if_found); -protected: + // static int isotope_compare(const void* ptr1, const void* ptr2); + // static int species_list_compare_alk(const void* ptr1, const void* ptr2); static int species_list_compare_master(const void* ptr1, const void* ptr2); int species_list_sort(void); + // struct Change_Surf* change_surf_alloc(int count); -public: - struct master* surface_get_psi_master(const char* name, int plane); + // int system_duplicate(int i, int save_old); + // + // + bool phase_rxn_to_trxn(struct phase* phase_ptr, CReaction& rxn_ptr); + bool trxn_add(CReaction& r_ptr, double coef, bool combine); + bool trxn_add_phase(CReaction& r_ref, double coef, bool combine); int trxn_combine(void); + static int trxn_compare(const void* ptr1, const void* ptr2); + bool trxn_copy(CReaction& rxn_ref); LDBLE trxn_find_coef(const char* str, int start); + int trxn_multiply(LDBLE coef); int trxn_print(void); int trxn_reverse_k(void); int trxn_sort(void); int trxn_swap(const char* token); - bool trxn_add(CReaction& r_ptr, double coef, bool combine); - bool trxn_add_phase(CReaction& r_ref, double coef, bool combine); - bool trxn_copy(CReaction& rxn_ref); - double rxn_find_coef(CReaction& r_ptr, const char* str); - bool phase_rxn_to_trxn(struct phase* phase_ptr, CReaction& rxn_ptr); - double calc_alk(CReaction& rxn_ptr); - CReaction CReaction_internal_copy(CReaction& rxn_ref); + struct unknown* unknown_alloc(void); int unknown_delete(int i); int unknown_free(struct unknown* unknown_ptr); int entity_exists(const char* name, int n_user); static int inverse_compare(const void* ptr1, const void* ptr2); int inverse_free(struct inverse* inverse_ptr); - static int kinetics_compare_int(const void* ptr1, const void* ptr2); int logk_init(struct logk* logk_ptr); static int master_compare_string(const void* ptr1, const void* ptr2); int master_free(struct master* master_ptr); @@ -902,8 +913,6 @@ public: int s_free(struct species* s_ptr); int s_init(struct species* s_ptr); static int species_list_compare(const void* ptr1, const void* ptr2); - static int rxn_token_temp_compare(const void* ptr1, const void* ptr2); - int trxn_multiply(LDBLE coef); void Use2cxxStorageBin(cxxStorageBin& sb); void phreeqc2cxxStorageBin(cxxStorageBin& sb); @@ -1011,51 +1020,34 @@ public: int mix_stag(int i, LDBLE stagkin_time, int punch, LDBLE step_fraction_kin); - // elt_list - int add_elt_list(const cxxNameDouble& nd, LDBLE coef); - int add_elt_list(const std::vector& el, double coef); - std::vector elt_list_internal_copy(const std::vector& el); - int change_hydrogen_in_elt_list(LDBLE charge); - struct element* element_store(const char* element); - int elt_list_combine(void); - static int elt_list_compare(const void* ptr1, const void* ptr2); - std::vector elt_list_vsave(void); - cxxNameDouble elt_list_NameDouble(void); - // utilities.cpp ------------------------------- public: - - LDBLE calc_rho_0(LDBLE tc, LDBLE pa); + double calc_alk(CReaction& rxn_ptr); + double calc_delta_v(CReaction& r_ref, bool phase); LDBLE calc_dielectrics(LDBLE tc, LDBLE pa); + LDBLE calc_rho_0(LDBLE tc, LDBLE pa); int compute_gfw(const char* string, LDBLE* gfw); static int copy_token(char* token_ptr, const char** ptr, int* length); static int copy_token(std::string& token, const char** ptr); int dup_print(const char* cptr, int emphasis); int equal(LDBLE a, LDBLE b, LDBLE eps); -public: void* free_check_null(void* ptr); -protected: int get_token(const char** eqnaddr, std::string& string, LDBLE* z, int* l); int islegit(const char c); -public: void malloc_error(void); -protected: int parse_couple(char* token); int print_centered(const char* string); -public: static int replace(const char* str1, const char* str2, char* str); static void replace(std::string &stds, const char* str1, const char* str2); static bool replace(const char* str1, const char* str2, std::string& str); static int strcmp_nocase(const char* str1, const char* str2); static int strcmp_nocase_arg1(const char* str1, const char* str2); static void str_tolower(std::string& name); -protected: void space(void** ptr, int i, int* max, int struct_size); void squeeze_white(char* s_l); int status(int count, const char* str, bool kinetics = false); void str_tolower(char* str); void str_toupper(char* str); -public: #if !defined(NDEBUG) && defined(WIN32_MEMORY_DEBUG) char* _string_duplicate(const char* token, const char* szFileName, int nLine); #else diff --git a/prep.cpp b/prep.cpp index 8123150b..47424e6d 100644 --- a/prep.cpp +++ b/prep.cpp @@ -6183,45 +6183,3 @@ setup_related_surface(void) } return (OK); } -/* ---------------------------------------------------------------------- */ -int Phreeqc:: -change_hydrogen_in_elt_list(LDBLE charge) -/* ---------------------------------------------------------------------- */ -{ - int j; - int found_h, found_o; - LDBLE coef_h, coef_o, coef; - found_h = -1; - found_o = -1; - coef_h = 0.0; - coef_o = 0.0; - elt_list_combine(); - for (j = 0; j < count_elts; j++) - { - if (strcmp(elt_list[j].elt->name, "H") == 0) - { - found_h = j; - coef_h = elt_list[j].coef; - } - else if (strcmp(elt_list[j].elt->name, "O") == 0) - { - found_o = j; - coef_o = elt_list[j].coef; - } - } - coef = coef_h - 2 * coef_o - charge; - if (found_h < 0 && found_o < 0) - return (OK); - if (found_h >= 0 && found_o < 0) - return (OK); - if (found_h < 0 && found_o >= 0) - { - elt_list[count_elts].elt = s_hplus->primary->elt; - elt_list[count_elts].coef = coef; - count_elts++; - elt_list_combine(); - return (OK); - } - elt_list[found_h].coef = coef; - return (OK); -} diff --git a/structures.cpp b/structures.cpp index 7cada66b..52ff11f9 100644 --- a/structures.cpp +++ b/structures.cpp @@ -249,7 +249,56 @@ reinitialize(void) Rxn_pressure_map.clear(); return (OK); } +/* ********************************************************************** + * + * Routines related to CReaction + * + * ********************************************************************** */ +CReaction Phreeqc::CReaction_internal_copy(CReaction& rxn_ref) +{ + CReaction rxn; + for (size_t i = 0; i < MAX_LOG_K_INDICES; i++) rxn.logk[i] = rxn_ref.logk[i]; + for (size_t i = 0; i < 3; i++) rxn.dz[i] = rxn_ref.dz[i]; + rxn.Get_tokens().resize(rxn_ref.Get_tokens().size()); + for (size_t i = 0; i < rxn_ref.Get_tokens().size(); i++) + { + rxn.token[i].s = (rxn_ref.token[i].s == NULL) ? NULL : + s_search(rxn_ref.token[i].s->name); + rxn.token[i].coef = rxn_ref.token[i].coef; + rxn.token[i].name = (rxn_ref.token[i].s == NULL) ? NULL : + string_hsave(rxn_ref.token[i].name); + } + return rxn; +} +/* ---------------------------------------------------------------------- */ +double Phreeqc:: +rxn_find_coef(CReaction& r_ref, const char* str) +/* ---------------------------------------------------------------------- */ +{ + /* + * Finds coefficient of token in reaction. + * input: r_ptr, pointer to a reaction structure + * str, string to find as reaction token + * + * Return: 0.0, if token not found + * coefficient of token, if found. + */ + struct rxn_token* r_token; + LDBLE coef; + r_token = &r_ref.token[1]; + coef = 0.0; + while (r_token->s != NULL) + { + if (strcmp(r_token->s->name, str) == 0) + { + coef = r_token->coef; + break; + } + r_token++; + } + return (coef); +} /* ********************************************************************** * * Routines related to structure "element" @@ -309,12 +358,89 @@ element_store(const char * element) elements_map[element] = elt_ptr; return (elt_ptr); } - /* ********************************************************************** * * Routines related to structure "elt_list" * * ********************************************************************** */ + /* ---------------------------------------------------------------------- */ +int Phreeqc:: +add_elt_list(const cxxNameDouble& nd, LDBLE coef) +/* ---------------------------------------------------------------------- */ +{ + cxxNameDouble::const_iterator cit = nd.begin(); + for (; cit != nd.end(); cit++) + { + if (count_elts >= (int)elt_list.size()) + { + elt_list.resize(count_elts + 1); + } + elt_list[count_elts].elt = element_store(cit->first.c_str()); + elt_list[count_elts].coef = cit->second * coef; + count_elts++; + } + return (OK); +} +int Phreeqc:: +add_elt_list(const std::vector& el, double coef) +/* ---------------------------------------------------------------------- */ +{ + const struct elt_list* elt_list_ptr = &el[0]; + + for (; elt_list_ptr->elt != NULL; elt_list_ptr++) + { + if (count_elts >= elt_list.size()) + { + elt_list.resize(count_elts + 1); + } + elt_list[count_elts].elt = elt_list_ptr->elt; + elt_list[count_elts].coef = elt_list_ptr->coef * coef; + count_elts++; + } + return (OK); +} +/* ---------------------------------------------------------------------- */ +int Phreeqc:: +change_hydrogen_in_elt_list(LDBLE charge) +/* ---------------------------------------------------------------------- */ +{ + int j; + int found_h, found_o; + LDBLE coef_h, coef_o, coef; + found_h = -1; + found_o = -1; + coef_h = 0.0; + coef_o = 0.0; + elt_list_combine(); + for (j = 0; j < count_elts; j++) + { + if (strcmp(elt_list[j].elt->name, "H") == 0) + { + found_h = j; + coef_h = elt_list[j].coef; + } + else if (strcmp(elt_list[j].elt->name, "O") == 0) + { + found_o = j; + coef_o = elt_list[j].coef; + } + } + coef = coef_h - 2 * coef_o - charge; + if (found_h < 0 && found_o < 0) + return (OK); + if (found_h >= 0 && found_o < 0) + return (OK); + if (found_h < 0 && found_o >= 0) + { + elt_list[count_elts].elt = s_hplus->primary->elt; + elt_list[count_elts].coef = coef; + count_elts++; + elt_list_combine(); + return (OK); + } + elt_list[found_h].coef = coef; + return (OK); +} /* ---------------------------------------------------------------------- */ int Phreeqc:: elt_list_combine(void) @@ -358,37 +484,40 @@ elt_list_combine(void) count_elts = j + 1; return (OK); } - /* ---------------------------------------------------------------------- */ int Phreeqc:: -elt_list_compare(const void *ptr1, const void *ptr2) +elt_list_compare(const void* ptr1, const void* ptr2) /* ---------------------------------------------------------------------- */ { - const struct elt_list *a, *b; + const struct elt_list* a, * b; - a = (const struct elt_list *) ptr1; - b = (const struct elt_list *) ptr2; + a = (const struct elt_list*)ptr1; + b = (const struct elt_list*)ptr2; return (strncmp(a->elt->name, b->elt->name, MAX_LENGTH)); } /* ---------------------------------------------------------------------- */ -cxxNameDouble Phreeqc:: -elt_list_NameDouble(void) +std::vector Phreeqc:: +elt_list_internal_copy(const std::vector& el) /* ---------------------------------------------------------------------- */ { -/* - * Takes data from work space elt_list, makes NameDouble - */ - cxxNameDouble nd; - for(int i = 0; i < count_elts; i++) + std::vector new_elt_list; + const struct elt_list* elt_list_ptr = &el[0]; + + new_elt_list.resize(el.size()); + size_t count = 0; + for (; elt_list_ptr->elt != NULL; elt_list_ptr++) { - nd.add(elt_list[i].elt->name, elt_list[i].coef); + new_elt_list[count].elt = element_store(elt_list_ptr->elt->name); + new_elt_list[count].coef = elt_list_ptr->coef; + count++; } - return (nd); + new_elt_list[count].elt = NULL; + return new_elt_list; } /* ---------------------------------------------------------------------- */ std::vector Phreeqc:: - elt_list_vsave(void) - /* ---------------------------------------------------------------------- */ +elt_list_vsave(void) +/* ---------------------------------------------------------------------- */ { /* * Takes data from work space elt_list, allocates a new elt_list structure, @@ -413,6 +542,22 @@ std::vector Phreeqc:: new_elt_list[count_elts].elt = NULL; return new_elt_list; } + +/* ---------------------------------------------------------------------- */ +cxxNameDouble Phreeqc:: +elt_list_NameDouble(void) +/* ---------------------------------------------------------------------- */ +{ + /* + * Takes data from work space elt_list, makes NameDouble + */ + cxxNameDouble nd; + for (int i = 0; i < count_elts; i++) + { + nd.add(elt_list[i].elt->name, elt_list[i].coef); + } + return (nd); +} /* ********************************************************************** * * Routines related to structure "inverse" @@ -1784,16 +1929,154 @@ surface_get_psi_master(const char *name, int plane) * Routines related to structure "trxn" * * ********************************************************************** */ + /* ---------------------------------------------------------------------- */ -int Phreeqc:: -rxn_token_temp_compare(const void *ptr1, const void *ptr2) +bool Phreeqc:: +phase_rxn_to_trxn(struct phase* phase_ptr, CReaction& rxn_ref) /* ---------------------------------------------------------------------- */ { - const struct rxn_token_temp *rxn_token_temp_ptr1, *rxn_token_temp_ptr2; - rxn_token_temp_ptr1 = (const struct rxn_token_temp *) ptr1; - rxn_token_temp_ptr2 = (const struct rxn_token_temp *) ptr2; - return (strcmp(rxn_token_temp_ptr1->name, rxn_token_temp_ptr2->name)); + /* + * Copy reaction from reaction structure to + * temp reaction structure. + */ + int l; + const char* cptr; + LDBLE l_z; + trxn.token.resize(rxn_ref.size()); + trxn.token[0].name = phase_ptr->formula; + /* charge */ + cptr = phase_ptr->formula; + { + std::string token; + get_token(&cptr, token, &l_z, &l); + } + trxn.token[0].z = l_z; + trxn.token[0].s = NULL; + trxn.token[0].unknown = NULL; + /*trxn.token[0].coef = -1.0; */ + /* check for leading coefficient of 1.0 for phase did not work */ + trxn.token[0].coef = phase_ptr->rxn.token[0].coef; + for (size_t i = 1; rxn_ref.token[i].s != NULL; i++) + { + trxn.token[i].name = rxn_ref.token[i].s->name; + trxn.token[i].z = rxn_ref.token[i].s->z; + trxn.token[i].s = NULL; + trxn.token[i].unknown = NULL; + trxn.token[i].coef = rxn_ref.token[i].coef; + count_trxn = i + 1; + } + return (OK); } +/* ---------------------------------------------------------------------- */ +bool Phreeqc:: +trxn_add(CReaction& r_ref, double coef, bool combine) +/* ---------------------------------------------------------------------- */ +{ + /* + * Adds reactions together. + * + * Global variable count_trxn determines which position in trxn is used. + * If count_trxn=0, then the equation effectively is copied into trxn. + * If count_trxn>0, then new equation is added to existing equation. + * + * Arguments: + * *r_ptr points to rxn structure to add. + * + * coef added equation is multiplied by coef. + * combine if TRUE, reaction is reaction is sorted and + * like terms combined. + */ + /* + * Accumulate log k for reaction + */ + if (count_trxn == 0) + { + for (int i = 0; i < MAX_LOG_K_INDICES; i++) trxn.logk[i] = r_ref.Get_logk()[i]; + for (int i = 0; i < 3; i++) trxn.dz[i] = r_ref.Get_dz()[i]; + } + else + { + for (int i = 0; i < MAX_LOG_K_INDICES; i++) trxn.logk[i] += coef * r_ref.Get_logk()[i]; + for (int i = 0; i < 3; i++) trxn.dz[i] += coef * r_ref.Get_dz()[i]; + } + /* + * Copy equation into work space + */ + struct rxn_token* next_token = &r_ref.token[0]; + while (next_token->s != NULL) + { + if (count_trxn + 1 > trxn.token.size()) + trxn.token.resize(count_trxn + 1); + trxn.token[count_trxn].name = next_token->s->name; + trxn.token[count_trxn].s = next_token->s; + trxn.token[count_trxn].coef = coef * next_token->coef; + count_trxn++; + next_token++; + } + if (combine == TRUE) + trxn_combine(); + return (OK); +} +/* ---------------------------------------------------------------------- */ +bool Phreeqc:: +trxn_add_phase(CReaction& r_ref, double coef, bool combine) +/* ---------------------------------------------------------------------- */ +{ + /* + * Adds reactions together. + * + * Global variable count_trxn determines which position in trxn is used. + * If count_trxn=0, then the equation effectively is copied into trxn. + * If count_trxn>0, then new equation is added to existing equation. + * + * Arguments: + * *r_ptr points to rxn structure to add. + * + * coef added equation is multiplied by coef. + * combine if TRUE, reaction is reaction is sorted and + * like terms combined. + */ + int i; + struct rxn_token* next_token; + /* + * Accumulate log k for reaction + */ + if (count_trxn == 0) + { + memcpy((void*)trxn.logk, (void*)r_ref.Get_logk(), + (size_t)MAX_LOG_K_INDICES * sizeof(double)); + } + else + { + for (i = 0; i < MAX_LOG_K_INDICES; i++) trxn.logk[i] += coef * r_ref.Get_logk()[i]; + } + /* + * Copy equation into work space + */ + next_token = &r_ref.token[0]; + while (next_token->s != NULL || next_token->name != NULL) + { + if (count_trxn + 1 > trxn.token.size()) + trxn.token.resize(count_trxn + 1); + if (next_token->s != NULL) + { + trxn.token[count_trxn].name = next_token->s->name; + trxn.token[count_trxn].s = next_token->s; + } + else + { + trxn.token[count_trxn].name = next_token->name; + trxn.token[count_trxn].s = NULL; + } + trxn.token[count_trxn].coef = coef * next_token->coef; + count_trxn++; + next_token++; + } + if (combine) + trxn_combine(); + return (OK); +} + /* ---------------------------------------------------------------------- */ int Phreeqc:: trxn_combine(void) @@ -1858,6 +2141,56 @@ trxn_combine(void) return (OK); } /* ---------------------------------------------------------------------- */ +int Phreeqc:: +trxn_compare(const void* ptr1, const void* ptr2) +/* ---------------------------------------------------------------------- */ +{ + const struct rxn_token_temp* rxn_token_temp_ptr1, * rxn_token_temp_ptr2; + rxn_token_temp_ptr1 = (const struct rxn_token_temp*)ptr1; + rxn_token_temp_ptr2 = (const struct rxn_token_temp*)ptr2; + return (strcmp(rxn_token_temp_ptr1->name, rxn_token_temp_ptr2->name)); +} +/* ---------------------------------------------------------------------- */ +bool Phreeqc:: +trxn_copy(CReaction& rxn_ref) +/* ---------------------------------------------------------------------- */ +{ + /* + * Copies trxn to a reaction structure. + * + * Input: rxn_ptr, pointer to reaction structure to copy trxn to. + * + */ + int i; + /* + * Copy logk data + */ + for (i = 0; i < MAX_LOG_K_INDICES; i++) + { + rxn_ref.logk[i] = trxn.logk[i]; + } + /* + * Copy dz data + */ + for (i = 0; i < 3; i++) + { + rxn_ref.dz[i] = trxn.dz[i]; + } + /* + * Copy tokens + */ + rxn_ref.Get_tokens().resize(count_trxn + 1); + for (size_t i = 0; i < count_trxn; i++) + { + rxn_ref.Get_tokens()[i].s = trxn.token[i].s; + rxn_ref.Get_tokens()[i].name = trxn.token[i].name; + rxn_ref.Get_tokens()[i].coef = trxn.token[i].coef; + } + rxn_ref.token[count_trxn].s = NULL; + rxn_ref.token[count_trxn].name = NULL; + return (OK); +} +/* ---------------------------------------------------------------------- */ LDBLE Phreeqc:: trxn_find_coef(const char *str, int start) /* ---------------------------------------------------------------------- */ @@ -1996,7 +2329,7 @@ trxn_sort(void) qsort(&trxn.token[1], (size_t)count_trxn - 1, sizeof(struct rxn_token_temp), - rxn_token_temp_compare); + trxn_compare); } return (OK); } diff --git a/utilities.cpp b/utilities.cpp index eaf63b18..4e9d730e 100644 --- a/utilities.cpp +++ b/utilities.cpp @@ -7,6 +7,142 @@ #include +/* ---------------------------------------------------------------------- */ +double Phreeqc:: +calc_alk(CReaction& rxn_ref) +/* ---------------------------------------------------------------------- */ +{ + LDBLE return_value; + struct master* master_ptr; + + return_value = 0.0; + struct rxn_token* r_token = &rxn_ref.token[1]; + while (r_token->s != NULL) + { + master_ptr = r_token->s->secondary; + if (master_ptr == NULL) + { + master_ptr = r_token->s->primary; + } + if (master_ptr == NULL) + { + error_string = sformatf( + "Non-master species in secondary reaction, %s.", + rxn_ref.token[0].s->name); + error_msg(error_string, CONTINUE); + input_error++; + break; + } + return_value += r_token->coef * master_ptr->alk; + r_token++; + } + return (return_value); +}/* ---------------------------------------------------------------------- */ +double Phreeqc:: +calc_delta_v(CReaction& r_ref, bool phase) +/* ---------------------------------------------------------------------- */ +{ + /* calculate delta_v from molar volumes */ + double d_v = 0.0; + if (phase) + { + /* for phases: reactants have coef's < 0, products have coef's > 0, v.v. for species */ + for (size_t i = 1; r_ref.Get_tokens()[i].s; i++) + { + if (!r_ref.Get_tokens()[i].s) + continue; + d_v += r_ref.Get_tokens()[i].coef * r_ref.Get_tokens()[i].s->logk[vm_tc]; + } + } + else + { + for (size_t i = 0; r_ref.token[i].name /*|| r_ptr->token[i].s*/; i++) + { + if (!r_ref.Get_tokens()[i].s) + continue; + d_v -= r_ref.Get_tokens()[i].coef * r_ref.Get_tokens()[i].s->logk[vm_tc]; + } + } + return d_v; +} +/* ---------------------------------------------------------------------- */ +LDBLE Phreeqc:: +calc_dielectrics(LDBLE tc, LDBLE pa) +/* ---------------------------------------------------------------------- */ +{ + /* Relative dielectric constant of pure water, eps as a function of (P, T) + Bradley and Pitzer, 1979, JPC 83, 1599. + (newer data in Fernandez et al., 1995, JPCRD 24, 33, + and Fernandez et al., 1997, JPCRD 26, 1125, show its correctness) + + d(eps)/d(P), Debye-Hueckel A and B, and Av (for Av, see Pitzer et al., 1984, JPCRD 13, p. 4) + */ + if (llnl_temp.size() > 0) return OK; + if (tc > 350.) + { + tc = 350.; + } + LDBLE T = tc + 273.15; + LDBLE u1 = 3.4279e2, u2 = -5.0866e-3, u3 = 9.469e-7, u4 = -2.0525, + u5 = 3.1159e3, u6 = -1.8289e2, u7 = -8.0325e3, u8 = 4.2142e6, + u9 = 2.1417; + LDBLE d1000 = u1 * exp(T * (u2 + T * u3)); // relative dielectric constant at 1000 bar + LDBLE c = u4 + u5 / (u6 + T); + LDBLE b = u7 + u8 / T + u9 * T; + LDBLE pb = pa * 1.01325; // pa in bar + eps_r = d1000 + c * log((b + pb) / (b + 1e3)); // relative dielectric constant + if (eps_r <= 0) + { + eps_r = 10.; + warning_msg("Relative dielectric constant is negative.\nTemperature is out of range of parameterization."); + } + + /* qe^2 / (eps_r * kB * T) = 4.803204e-10**2 / 1.38065e-16 / (eps_r * T) + = 1.671008e-3 (esu^2 / (erg/K)) / (eps_r * T) */ + LDBLE e2_DkT = 1.671008e-3 / (eps_r * T); + + DH_B = sqrt(8 * pi * AVOGADRO * e2_DkT * rho_0 / 1e3); // Debye length parameter, 1/cm(mol/kg)^-0.5 + + DH_A = DH_B * e2_DkT / (2. * LOG_10); //(mol/kg)^-0.5 + + /* A0 in pitzer */ + if (pitzer_model || sit_model) + { + A0 = DH_B * e2_DkT / 6.0; + if (pitzer_model && aphi != NULL) + { + calc_pitz_param(aphi, T, 298.15); + A0 = aphi->p; + } + } + + /* Debye-Hueckel limiting slope = DH_B * e2_DkT * RT * (d(ln(eps_r)) / d(P) - compressibility) */ + DH_Av = DH_B * e2_DkT * R_LITER_ATM * 1e3 * T * (c / (b + pb) * 1.01325 / eps_r - kappa_0 / 3.); // (cm3/mol)(mol/kg)^-0.5 + + DH_B /= 1e8; // kappa, 1/Angstrom(mol/kg)^-0.5 + + /* the Born functions, * 41.84 to give molal volumes in cm3/mol... */ + ZBrn = (-1 / eps_r + 1.0) * 41.84004; + QBrn = c / (b + pb) / eps_r / eps_r * 41.84004; + /* dgdP from subroutine gShok2 in supcrt92, g is neglected here (at tc < 300)... + and, dgdP is small. Better, adapt Wref to experimental Vm's */ + dgdP = 0; + //if (tc > 150 && rho_0 < 1.0) + //{ + // LDBLE sc[7] = {1, -0.2037662e+01, 0.5747000e-02, -0.6557892e-05, + // 0.6107361e+01, -0.1074377e-01, 0.1268348e-04}; + // LDBLE csc[4] = {1, 0.3666666e+02, -0.1504956e-9, 0.5017997e-13}; + // LDBLE sa = sc[1] + tc * (sc[2] + tc * sc[3]); + // LDBLE sb = sc[4] + tc * (sc[5] + tc * sc[6]); + + // dgdP = - sa * sb * pow(1.0 - rho_0, sb - 1.0) * rho_0 * kappa_0 / 1.01325; + + // LDBLE ft = pow((tc - 155.0)/300.0, 4.8) + csc[1] * pow((tc - 155.0)/300.0, 16.0); + // LDBLE dfdP = ft * (-3.0 * csc[2] * pow(1000.0 - pb, 2) - 4.0 * csc[3] * pow(1000.0 - pb, 3)); + // dgdP -= dfdP; + //} + + return (OK); +} /* ---------------------------------------------------------------------- */ LDBLE Phreeqc:: calc_rho_0(LDBLE tc, LDBLE pa) @@ -65,85 +201,6 @@ calc_rho_0(LDBLE tc, LDBLE pa) return (rho_0 / 1e3); } -/* ---------------------------------------------------------------------- */ -LDBLE Phreeqc:: -calc_dielectrics(LDBLE tc, LDBLE pa) -/* ---------------------------------------------------------------------- */ -{ - /* Relative dielectric constant of pure water, eps as a function of (P, T) - Bradley and Pitzer, 1979, JPC 83, 1599. - (newer data in Fernandez et al., 1995, JPCRD 24, 33, - and Fernandez et al., 1997, JPCRD 26, 1125, show its correctness) - + d(eps)/d(P), Debye-Hueckel A and B, and Av (for Av, see Pitzer et al., 1984, JPCRD 13, p. 4) - */ - if (llnl_temp.size() > 0) return OK; - if (tc > 350.) - { - tc = 350.; - } - LDBLE T = tc + 273.15; - LDBLE u1 = 3.4279e2, u2 = -5.0866e-3, u3 = 9.469e-7, u4 = -2.0525, - u5 = 3.1159e3, u6 = -1.8289e2, u7 = -8.0325e3, u8 = 4.2142e6, - u9 = 2.1417; - LDBLE d1000 = u1 * exp(T * (u2 + T * u3)); // relative dielectric constant at 1000 bar - LDBLE c = u4 + u5 / (u6 + T); - LDBLE b = u7 + u8 / T + u9 * T; - LDBLE pb = pa * 1.01325; // pa in bar - eps_r = d1000 + c * log((b + pb) / (b + 1e3)); // relative dielectric constant - if (eps_r <= 0) - { - eps_r = 10.; - warning_msg("Relative dielectric constant is negative.\nTemperature is out of range of parameterization."); - } - - /* qe^2 / (eps_r * kB * T) = 4.803204e-10**2 / 1.38065e-16 / (eps_r * T) - = 1.671008e-3 (esu^2 / (erg/K)) / (eps_r * T) */ - LDBLE e2_DkT = 1.671008e-3 / (eps_r * T); - - DH_B = sqrt(8 * pi * AVOGADRO * e2_DkT * rho_0 / 1e3); // Debye length parameter, 1/cm(mol/kg)^-0.5 - - DH_A = DH_B * e2_DkT / (2. * LOG_10); //(mol/kg)^-0.5 - - /* A0 in pitzer */ - if (pitzer_model || sit_model) - { - A0 = DH_B * e2_DkT / 6.0; - if (pitzer_model && aphi != NULL) - { - calc_pitz_param(aphi, T, 298.15); - A0 = aphi->p; - } - } - - /* Debye-Hueckel limiting slope = DH_B * e2_DkT * RT * (d(ln(eps_r)) / d(P) - compressibility) */ - DH_Av = DH_B * e2_DkT * R_LITER_ATM * 1e3 * T * (c / (b + pb) * 1.01325 / eps_r - kappa_0 / 3.); // (cm3/mol)(mol/kg)^-0.5 - - DH_B /= 1e8; // kappa, 1/Angstrom(mol/kg)^-0.5 - - /* the Born functions, * 41.84 to give molal volumes in cm3/mol... */ - ZBrn = (- 1 / eps_r + 1.0) * 41.84004; - QBrn = c / (b + pb) / eps_r / eps_r * 41.84004; - /* dgdP from subroutine gShok2 in supcrt92, g is neglected here (at tc < 300)... - and, dgdP is small. Better, adapt Wref to experimental Vm's */ - dgdP = 0; - //if (tc > 150 && rho_0 < 1.0) - //{ - // LDBLE sc[7] = {1, -0.2037662e+01, 0.5747000e-02, -0.6557892e-05, - // 0.6107361e+01, -0.1074377e-01, 0.1268348e-04}; - // LDBLE csc[4] = {1, 0.3666666e+02, -0.1504956e-9, 0.5017997e-13}; - // LDBLE sa = sc[1] + tc * (sc[2] + tc * sc[3]); - // LDBLE sb = sc[4] + tc * (sc[5] + tc * sc[6]); - - // dgdP = - sa * sb * pow(1.0 - rho_0, sb - 1.0) * rho_0 * kappa_0 / 1.01325; - - // LDBLE ft = pow((tc - 155.0)/300.0, 4.8) + csc[1] * pow((tc - 155.0)/300.0, 16.0); - // LDBLE dfdP = ft * (-3.0 * csc[2] * pow(1000.0 - pb, 2) - 4.0 * csc[3] * pow(1000.0 - pb, 3)); - // dgdP -= dfdP; - //} - - return (OK); -} - /* ---------------------------------------------------------------------- */ int Phreeqc:: From af1b7616aeaa75d3527ec75b1da3a1f17974c68d Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Fri, 2 Apr 2021 16:57:10 -0600 Subject: [PATCH 39/53] removing CReaction and Classes files --- CReaction.cpp | 20 +------------- CReaction.h | 29 -------------------- Classes.h | 27 ------------------- Phreeqc.h | 8 ++---- global_structures.h | 64 ++++++++++++++++++++++++++++++++++++++++++++- structures.cpp | 19 ++++++++++++++ 6 files changed, 85 insertions(+), 82 deletions(-) diff --git a/CReaction.cpp b/CReaction.cpp index f357b3d4..09035ba8 100644 --- a/CReaction.cpp +++ b/CReaction.cpp @@ -1,22 +1,4 @@ #include "global_structures.h" #include "CReaction.h" -CReaction::CReaction(void) -{ - for (size_t i = 0; i < MAX_LOG_K_INDICES; i++) this->logk[i] = 0.0; - for (size_t i = 0; i < 3; i++) this->dz[i] = 0.0; -} -CReaction::CReaction(size_t ntoken) -{ - for (size_t i = 0; i < MAX_LOG_K_INDICES; i++) this->logk[i] = 0.0; - for (size_t i = 0; i < 3; i++) this->dz[i] = 0.0; - this->token.resize(ntoken); -} -void CReaction::Set_logk(double* d) -{ - for (size_t i = 0; i < MAX_LOG_K_INDICES; i++)logk[i] = d[i]; -} -void CReaction::Set_dz(double* d) -{ - for (size_t i = 0; i < 3; i++) dz[i] = d[i]; -} + diff --git a/CReaction.h b/CReaction.h index 7d4fae1b..795d9621 100644 --- a/CReaction.h +++ b/CReaction.h @@ -3,34 +3,5 @@ #include #include "global_structures.h" -/*---------------------------------------------------------------------- - * Reaction - *---------------------------------------------------------------------- */ -class CReaction -{ -public: - CReaction(void); - CReaction(size_t ntoken); - ~CReaction(void) {} - double* Get_logk(void) { return this->logk; } - void Set_logk(double* d); - double* Get_dz(void) { return this->dz; } - void Set_dz(double* d); - size_t size() { return token.size(); } - std::vector& Get_tokens(void) { return this->token; } - void Set_tokens(const std::vector& t) { this->token = t; } - -public: - double logk[21]; //LOG_K_INDICES::MAX_LOG_K_INDICES - //LDBLE logk[LOG_K_INDICES::MAX_LOG_K_INDICES]; - double dz[3]; - std::vector token; -}; -struct rxn_token -{ - struct species* s = NULL; - LDBLE coef = 0.0; - const char* name = NULL; -}; #endif // !defined(CREACTION_H_INCLUDED) \ No newline at end of file diff --git a/Classes.h b/Classes.h index 3099b642..7d02c307 100644 --- a/Classes.h +++ b/Classes.h @@ -3,32 +3,5 @@ #include #include "GasPhase.h" #include "Surface.h" -class Model -{ -public: - Model() - { - force_prep = true; - gas_phase_type = cxxGasPhase::GP_UNKNOWN; - numerical_fixed_volume = false; - dl_type = cxxSurface::NO_DL; - surface_type = cxxSurface::UNKNOWN_DL; - }; - ~Model() - { - }; - bool force_prep; - bool numerical_fixed_volume; - cxxGasPhase::GP_TYPE gas_phase_type; - std::vector gas_phase; - std::vector ss_assemblage; - std::vector pp_assemblage; - std::vector si; - std::vector add_formula; - cxxSurface::DIFFUSE_LAYER_TYPE dl_type; - cxxSurface::SURFACE_TYPE surface_type; - std::vector surface_comp; - std::vector surface_charge; -}; #endif // !defined(CLASSES_H_INCLUDED) \ No newline at end of file diff --git a/Phreeqc.h b/Phreeqc.h index c373b41e..63b6ed02 100644 --- a/Phreeqc.h +++ b/Phreeqc.h @@ -44,7 +44,6 @@ typedef unsigned char boolean; class cxxNameDouble; class cxxKinetics; -//class cxxMix; class cxxKineticsComp; class cxxExchange; class cxxExchComp; @@ -59,13 +58,10 @@ class cxxSolutionIsotope; class cxxSSassemblage; class cxxSS; class cxxStorageBin; -class CReaction; - - -#include "classes.h" -#include "global_structures.h" class PBasic; +#include "global_structures.h" + class Phreeqc { public: diff --git a/global_structures.h b/global_structures.h index 8d083c5b..6584dd4a 100644 --- a/global_structures.h +++ b/global_structures.h @@ -201,7 +201,36 @@ struct Change_Surf int cell_no; int next; }; +/*---------------------------------------------------------------------- + * CReaction + *---------------------------------------------------------------------- */ +class CReaction +{ +public: + CReaction(void); + CReaction(size_t ntoken); + ~CReaction(void) {} + double* Get_logk(void) { return this->logk; } + void Set_logk(double* d); + double* Get_dz(void) { return this->dz; } + void Set_dz(double* d); + size_t size() { return token.size(); } + std::vector& Get_tokens(void) { return this->token; } + void Set_tokens(const std::vector& t) { this->token = t; } + +public: + double logk[21]; //LOG_K_INDICES::MAX_LOG_K_INDICES + //LDBLE logk[LOG_K_INDICES::MAX_LOG_K_INDICES]; + double dz[3]; + std::vector token; +}; +struct rxn_token +{ + struct species* s = NULL; + LDBLE coef = 0.0; + const char* name = NULL; +}; struct save { int solution; @@ -298,9 +327,42 @@ struct inv_phases int force; std::vector isotopes; }; +/*---------------------------------------------------------------------- + * Jacobian and Mass balance lists + *---------------------------------------------------------------------- */ + +class Model +{ +public: + Model() + { + force_prep = true; + gas_phase_type = cxxGasPhase::GP_UNKNOWN; + numerical_fixed_volume = false; + dl_type = cxxSurface::NO_DL; + surface_type = cxxSurface::UNKNOWN_DL; + }; + ~Model() + { + }; + + bool force_prep; + bool numerical_fixed_volume; + cxxGasPhase::GP_TYPE gas_phase_type; + std::vector gas_phase; + std::vector ss_assemblage; + std::vector pp_assemblage; + std::vector si; + std::vector add_formula; + cxxSurface::DIFFUSE_LAYER_TYPE dl_type; + cxxSurface::SURFACE_TYPE surface_type; + std::vector surface_comp; + std::vector surface_charge; +}; + struct name_coef { - const char *name; + const char* name; LDBLE coef; }; /*---------------------------------------------------------------------- diff --git a/structures.cpp b/structures.cpp index 52ff11f9..43cc5363 100644 --- a/structures.cpp +++ b/structures.cpp @@ -254,6 +254,25 @@ reinitialize(void) * Routines related to CReaction * * ********************************************************************** */ +CReaction::CReaction(void) +{ + for (size_t i = 0; i < MAX_LOG_K_INDICES; i++) this->logk[i] = 0.0; + for (size_t i = 0; i < 3; i++) this->dz[i] = 0.0; +} +CReaction::CReaction(size_t ntoken) +{ + for (size_t i = 0; i < MAX_LOG_K_INDICES; i++) this->logk[i] = 0.0; + for (size_t i = 0; i < 3; i++) this->dz[i] = 0.0; + this->token.resize(ntoken); +} +void CReaction::Set_logk(double* d) +{ + for (size_t i = 0; i < MAX_LOG_K_INDICES; i++)logk[i] = d[i]; +} +void CReaction::Set_dz(double* d) +{ + for (size_t i = 0; i < 3; i++) dz[i] = d[i]; +} CReaction Phreeqc::CReaction_internal_copy(CReaction& rxn_ref) { CReaction rxn; From f86f4304042dabaef398e4d119ce0e046de52d75 Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Fri, 2 Apr 2021 16:57:59 -0600 Subject: [PATCH 40/53] back to original set of files I think --- CReaction.cpp | 4 ---- CReaction.h | 7 ------- Classes.cpp | 5 ----- Classes.h | 7 ------- 4 files changed, 23 deletions(-) delete mode 100644 CReaction.cpp delete mode 100644 CReaction.h delete mode 100644 Classes.cpp delete mode 100644 Classes.h diff --git a/CReaction.cpp b/CReaction.cpp deleted file mode 100644 index 09035ba8..00000000 --- a/CReaction.cpp +++ /dev/null @@ -1,4 +0,0 @@ -#include "global_structures.h" -#include "CReaction.h" - - diff --git a/CReaction.h b/CReaction.h deleted file mode 100644 index 795d9621..00000000 --- a/CReaction.h +++ /dev/null @@ -1,7 +0,0 @@ -#if !defined(CREACTION_H_INCLUDED) -#define CREACTION_H_INCLUDED -#include -#include "global_structures.h" - - -#endif // !defined(CREACTION_H_INCLUDED) \ No newline at end of file diff --git a/Classes.cpp b/Classes.cpp deleted file mode 100644 index f34e094c..00000000 --- a/Classes.cpp +++ /dev/null @@ -1,5 +0,0 @@ -#include "Phreeqc.h" -#include "CReaction.h" - - - diff --git a/Classes.h b/Classes.h deleted file mode 100644 index 7d02c307..00000000 --- a/Classes.h +++ /dev/null @@ -1,7 +0,0 @@ -#if !defined(CLASSES_H_INCLUDED) -#define CLASSES_H_INCLUDED -#include -#include "GasPhase.h" -#include "Surface.h" - -#endif // !defined(CLASSES_H_INCLUDED) \ No newline at end of file From 33157a20119933860d268f7bb40f1cfa9d8982de Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Fri, 2 Apr 2021 23:55:59 -0600 Subject: [PATCH 41/53] fixed more size_t and initialized all structs --- Phreeqc.h | 6 +- advection.cpp | 2 +- global_structures.h | 880 ++++++++++++++++++++++++-------------------- integrate.cpp | 2 +- inverse.cpp | 241 ++++++------ prep.cpp | 53 +-- spread.cpp | 2 +- tally.cpp | 11 +- tidy.cpp | 4 +- utilities.cpp | 5 +- 10 files changed, 635 insertions(+), 571 deletions(-) diff --git a/Phreeqc.h b/Phreeqc.h index 63b6ed02..964f221d 100644 --- a/Phreeqc.h +++ b/Phreeqc.h @@ -1770,10 +1770,10 @@ protected: /* tally.cpp ------------------------------- */ struct tally_buffer* t_buffer; - int tally_count_component; + size_t tally_count_component; struct tally* tally_table; - int count_tally_table_columns; - int count_tally_table_rows; + size_t count_tally_table_columns; + size_t count_tally_table_rows; /* transport.cpp ------------------------------- */ struct sol_D* sol_D; diff --git a/advection.cpp b/advection.cpp index a184cd5c..352d25b5 100644 --- a/advection.cpp +++ b/advection.cpp @@ -89,7 +89,7 @@ advection(void) /* * Equilibrate and (or) mix */ - for (i = 1; i <= count_ad_cells; i++) + for (size_t i = 1; i <= count_ad_cells; i++) { set_initial_moles(i); cell_no = i; diff --git a/global_structures.h b/global_structures.h index 6584dd4a..c064b94a 100644 --- a/global_structures.h +++ b/global_structures.h @@ -2,7 +2,6 @@ #define _INC_GLOBAL_STRUCTURES_H #include "Surface.h" #include "GasPhase.h" -#include "CReaction.h" /* ---------------------------------------------------------------------- * #define DEFINITIONS * ---------------------------------------------------------------------- */ @@ -194,12 +193,12 @@ typedef struct PHRQMemHeader struct Change_Surf { - const char *comp_name; - LDBLE fraction; - const char *new_comp_name; - LDBLE new_Dw; - int cell_no; - int next; + const char *comp_name = NULL; + LDBLE fraction = 0.5; + const char *new_comp_name = NULL; + LDBLE new_Dw = 1e-9; + int cell_no = 0; + int next = 0; }; /*---------------------------------------------------------------------- * CReaction @@ -220,8 +219,7 @@ public: void Set_tokens(const std::vector& t) { this->token = t; } public: - double logk[21]; //LOG_K_INDICES::MAX_LOG_K_INDICES - //LDBLE logk[LOG_K_INDICES::MAX_LOG_K_INDICES]; + double logk[MAX_LOG_K_INDICES]; double dz[3]; std::vector token; }; @@ -233,33 +231,33 @@ struct rxn_token }; struct save { - int solution; - int n_solution_user; - int n_solution_user_end; - int mix; - int n_mix_user; - int n_mix_user_end; - int reaction; - int n_reaction_user; - int n_reaction_user_end; - int pp_assemblage; - int n_pp_assemblage_user; - int n_pp_assemblage_user_end; - int exchange; - int n_exchange_user; - int n_exchange_user_end; - int kinetics; - int n_kinetics_user; - int n_kinetics_user_end; - int surface; - int n_surface_user; - int n_surface_user_end; - int gas_phase; - int n_gas_phase_user; - int n_gas_phase_user_end; - int ss_assemblage; - int n_ss_assemblage_user; - int n_ss_assemblage_user_end; + int solution = 0; + int n_solution_user = 0; + int n_solution_user_end = 0; + int mix = 0; + int n_mix_user = 0; + int n_mix_user_end = 0; + int reaction = 0; + int n_reaction_user = 0; + int n_reaction_user_end = 0; + int pp_assemblage = 0; + int n_pp_assemblage_user = 0; + int n_pp_assemblage_user_end = 0; + int exchange = 0; + int n_exchange_user = 0; + int n_exchange_user_end = 0; + int kinetics = 0; + int n_kinetics_user = 0; + int n_kinetics_user_end = 0; + int surface = 0; + int n_surface_user = 0; + int n_surface_user_end = 0; + int gas_phase = 0; + int n_gas_phase_user = 0; + int n_gas_phase_user_end = 0; + int ss_assemblage = 0; + int n_ss_assemblage_user = 0; + int n_ss_assemblage_user_end = 0; }; /*---------------------------------------------------------------------- @@ -275,56 +273,56 @@ struct copier *---------------------------------------------------------------------- */ struct inverse { - int n_user; - char *description; - int new_def; - int minimal; - int range; - int mp; - LDBLE mp_censor; - LDBLE range_max; - LDBLE tolerance; - LDBLE mp_tolerance; + int n_user = -1; + char *description = NULL; + int new_def = FALSE; + int minimal = FALSE; + int range = FALSE; + int mp = FALSE; + LDBLE mp_censor = 1e-20; + LDBLE range_max = 1000.0; + LDBLE tolerance = 1e-10; + LDBLE mp_tolerance = 1e-12; std::vector uncertainties; std::vector ph_uncertainties; - LDBLE water_uncertainty; - int mineral_water; - int carbon; + LDBLE water_uncertainty = 0.0; + int mineral_water = TRUE; + int carbon = TRUE; std::vector dalk_dph; std::vector dalk_dc; - size_t count_solns; + size_t count_solns = 0; std::vector solns; std::vector force_solns; std::vector elts; std::vector phases; - size_t count_redox_rxns; + size_t count_redox_rxns = 0; std::vector isotopes; std::vector i_u; std::vector isotope_unknowns; - const char *netpath; - const char *pat; + const char *netpath = NULL; + const char *pat = NULL; }; struct inv_elts { - const char *name; - struct master *master; - size_t row; + const char *name = NULL; + struct master *master = NULL; + size_t row = 0; std::vector uncertainties; }; struct inv_isotope { - const char *isotope_name; - LDBLE isotope_number; - const char *elt_name; + const char *isotope_name = NULL; + LDBLE isotope_number = 0; + const char *elt_name = NULL; std::vector uncertainties; }; struct inv_phases { - const char *name; - struct phase *phase; - int column; - int constraint; - int force; + const char *name = NULL; + struct phase *phase = NULL; + int column = 0; + int constraint = EITHER; + int force = FALSE; std::vector isotopes; }; /*---------------------------------------------------------------------- @@ -346,33 +344,33 @@ public: { }; - bool force_prep; - bool numerical_fixed_volume; - cxxGasPhase::GP_TYPE gas_phase_type; + bool force_prep = false; + bool numerical_fixed_volume = false; + cxxGasPhase::GP_TYPE gas_phase_type = cxxGasPhase::GP_UNKNOWN; std::vector gas_phase; std::vector ss_assemblage; std::vector pp_assemblage; std::vector si; std::vector add_formula; - cxxSurface::DIFFUSE_LAYER_TYPE dl_type; - cxxSurface::SURFACE_TYPE surface_type; + cxxSurface::DIFFUSE_LAYER_TYPE dl_type = cxxSurface::NO_DL; + cxxSurface::SURFACE_TYPE surface_type = cxxSurface::UNKNOWN_DL; std::vector surface_comp; std::vector surface_charge; }; struct name_coef { - const char* name; - LDBLE coef; + const char* name = NULL; + LDBLE coef = 0; }; /*---------------------------------------------------------------------- * Species_list *---------------------------------------------------------------------- */ struct species_list { - struct species *master_s; - struct species *s; - LDBLE coef; + struct species *master_s = NULL; + struct species *s = NULL; + LDBLE coef = 0; }; /*---------------------------------------------------------------------- @@ -380,62 +378,65 @@ struct species_list *---------------------------------------------------------------------- */ struct list0 { - LDBLE *target; - LDBLE coef; + LDBLE *target = NULL; + LDBLE coef = 0; }; struct list1 { - LDBLE *source; - LDBLE *target; + LDBLE *source = NULL; + LDBLE *target = NULL; }; struct list2 { - LDBLE *source; - LDBLE *target; - LDBLE coef; + LDBLE *source = NULL; + LDBLE *target = NULL; + LDBLE coef = 0; }; struct isotope { - LDBLE isotope_number; - const char *elt_name; - const char *isotope_name; - LDBLE total; - LDBLE ratio; - LDBLE ratio_uncertainty; - LDBLE x_ratio_uncertainty; - struct master *master; - struct master *primary; - LDBLE coef; /* coefficient of element in phase */ + LDBLE isotope_number = 0; + const char *elt_name = NULL; + const char *isotope_name = NULL; + LDBLE total = 0; + LDBLE ratio = 0; + LDBLE ratio_uncertainty = 0; + LDBLE x_ratio_uncertainty = 0; + struct master *master = NULL; + struct master *primary = NULL; + LDBLE coef = 0; /* coefficient of element in phase */ }; struct iso { - const char *name; - LDBLE value; - LDBLE uncertainty; + const char *name = NULL; + LDBLE value = 0; + LDBLE uncertainty = 0.05; }; /*---------------------------------------------------------------------- * Transport data *---------------------------------------------------------------------- */ struct stag_data { - int count_stag; - LDBLE exch_f; - LDBLE th_m; - LDBLE th_im; + int count_stag = 0; + LDBLE exch_f = 0; + LDBLE th_m = 0; + LDBLE th_im = 0; }; struct cell_data { - LDBLE length; - LDBLE mid_cell_x; - LDBLE disp; - LDBLE temp; - LDBLE por; /* free (uncharged) porewater porosities */ - LDBLE por_il; /* interlayer water porosities */ - LDBLE potV; /* potential (V) */ - int punch; - int print; - int same_model; + LDBLE length = 1; + LDBLE mid_cell_x = 1.; + LDBLE disp = 1.0; + LDBLE temp = 25.; + // free (uncharged) porewater porosities + LDBLE por = 0.1; + // interlayer water porosities + LDBLE por_il = 0.01; + // potential (V) + LDBLE potV = 0; + int punch = FALSE; + int print = FALSE; + int same_model = FALSE; }; /*---------------------------------------------------------------------- @@ -443,13 +444,13 @@ struct cell_data *---------------------------------------------------------------------- */ struct key { - char *name; - int keycount; + char *name = NULL; + int keycount = 0; }; struct const_key { - const char *name; - int keycount; + const char *name = NULL; + int keycount = 0; }; /*---------------------------------------------------------------------- @@ -457,11 +458,12 @@ struct cell_data *---------------------------------------------------------------------- */ struct element { - const char *name; /* element name */ + // element name + const char *name = NULL; /* int in; */ - struct master *master; - struct master *primary; - LDBLE gfw; + struct master *master = NULL; + struct master *primary = NULL; + LDBLE gfw = 0; }; /*---------------------------------------------------------------------- * Element List @@ -477,186 +479,241 @@ struct elt_list *---------------------------------------------------------------------- */ struct species { /* all data pertinent to an aqueous species */ - const char *name; /* name of species */ - const char *mole_balance; /* formula for mole balance */ - int in; /* species used in model if TRUE */ - int number; - struct master *primary; /* points to master species list, NULL if not primary master */ - struct master *secondary; /* points to master species list, NULL if not secondary master */ - LDBLE gfw; /* gram formula wt of species */ - LDBLE z; /* charge of species */ - LDBLE dw; /* tracer diffusion coefficient in water at 25oC, m2/s */ - LDBLE dw_t; /* correct Dw for temperature: Dw(TK) = Dw(298.15) * exp(dw_t / TK - dw_t / 298.15) */ - LDBLE dw_a; /* parms for calc'ng SC = SC0 * exp(-dw_a * z * mu^0.5 / (1 + DH_B * dw_a2 * mu^0.5)) */ - LDBLE dw_a2; /* */ - LDBLE dw_a_visc; /* viscosity correction of SC */ - LDBLE dw_t_SC; /* contribution to SC, for calc'ng transport number with BASIC */ - LDBLE dw_corr; /* dw corrected for TK and mu */ - LDBLE erm_ddl; /* enrichment factor in DDL */ - LDBLE equiv; /* equivalents in exchange species */ - LDBLE alk; /* alkalinity of species, used for cec in exchange */ - LDBLE carbon; /* stoichiometric coefficient of carbon in species */ - LDBLE co2; /* stoichiometric coefficient of C(4) in species */ - LDBLE h; /* stoichiometric coefficient of H in species */ - LDBLE o; /* stoichiometric coefficient of O in species */ - LDBLE dha, dhb, a_f; /* WATEQ Debye Huckel a and b-dot; active_fraction coef for exchange species */ - LDBLE lk; /* log10 k at working temperature */ - LDBLE logk[MAX_LOG_K_INDICES]; /* log kt0, delh, 6 coefficients analytical expression + volume terms */ - LDBLE Jones_Dole[10]; /* 7 coefficients analytical expression for B, D, anion terms and pressure in Jones_Dole viscosity eqn */ + // name of species + const char *name = NULL; + // formula for mole balance + const char *mole_balance = NULL; + // set internally if species in model + int in = FALSE; + int number = 0; + // points to master species list, NULL if not primary master + struct master *primary = NULL; + // points to master species list, NULL if not secondary master + struct master *secondary = NULL; + // gram formula wt of species + LDBLE gfw = 0; + // charge of species + LDBLE z = 0; + // tracer diffusion coefficient in water at 25oC, m2/s + LDBLE dw = 0; + // correct Dw for temperature: Dw(TK) = Dw(298.15) * exp(dw_t / TK - dw_t / 298.15) + LDBLE dw_t = 0; + // parms for calc'ng SC = SC0 * exp(-dw_a * z * mu^0.5 / (1 + DH_B * dw_a2 * mu^0.5)) + LDBLE dw_a = 0; + LDBLE dw_a2 = 0; + // viscosity correction of SC + LDBLE dw_a_visc = 0; + // contribution to SC, for calc'ng transport number with BASIC + LDBLE dw_t_SC = 0; + // dw corrected for TK and mu + LDBLE dw_corr = 0; + // enrichment factor in DDL + LDBLE erm_ddl = 0; + // equivalents in exchange species + LDBLE equiv = 0; + // alkalinity of species, used for cec in exchange + LDBLE alk = 0; + // stoichiometric coefficient of carbon in species + LDBLE carbon = 0; + // stoichiometric coefficient of C(4) in species + LDBLE co2 = 0; + // stoichiometric coefficient of H in species + LDBLE h = 0; + // stoichiometric coefficient of O in species + LDBLE o = 0; + // WATEQ Debye Huckel a and b-dot; active_fraction coef for exchange species + LDBLE dha = 0, dhb = 0, a_f = 0; + // log10 k at working temperature + LDBLE lk = 0; + // log kt0, delh, 6 coefficients analytical expression + volume terms + LDBLE logk[MAX_LOG_K_INDICES] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }; + // 7 coefficients analytical expression for B, D, anion terms and pressure in Jones_Dole viscosity eqn + LDBLE Jones_Dole[10] = { 0,0,0,0,0,0,0,0,0,0 }; /* VP: Density Start */ - LDBLE millero[7]; /* regression coefficients to calculate temperature dependent phi_0 and b_v of Millero density model */ + // regression coefficients to calculate temperature dependent phi_0and b_v of Millero density model + LDBLE millero[7] = { 0,0,0,0,0,0,0 }; /* VP: Density End */ - DELTA_H_UNIT original_units; /* enum with original delta H units */ + // enum with original delta H units + DELTA_H_UNIT original_units = kjoules; std::vector add_logk; - LDBLE lg; /* log10 activity coefficient, gamma */ - LDBLE lg_pitzer; /* log10 activity coefficient, from pitzer calculation */ - LDBLE lm; /* log10 molality */ - LDBLE la; /* log10 activity */ - LDBLE dg; /* gamma term for jacobian */ - LDBLE dg_total_g; - LDBLE moles; /* moles in solution; moles/mass_water = molality */ - int type; /* flag indicating presence in model and types of equations */ - int gflag; /* flag for preferred activity coef eqn */ - int exch_gflag; /* flag for preferred activity coef eqn */ - std::vector next_elt; // vector of elements + // log10 activity coefficient, gamma + LDBLE lg = 0; + // log10 activity coefficient, from pitzer calculation + LDBLE lg_pitzer = 0; + // log10 molality + LDBLE lm = 0; + // log10 activity + LDBLE la = 0; + // gamma term for jacobian + LDBLE dg = 0; + LDBLE dg_total_g = 0; + // moles in solution; moles/mass_water = molality + LDBLE moles = 0; + // flag indicating presence in model and types of equations + int type = 0; + // flag for preferred activity coef eqn + int gflag = 0; + // flag for preferred activity coef eqn + int exch_gflag = 0; + // vector of elements + std::vector next_elt; std::vector next_secondary; std::vector next_sys_total; - int check_equation; /* switch to check equation for charge and element balance */ - CReaction rxn; /* pointer to data base reaction */ - CReaction rxn_s; /* pointer to reaction converted to secondary and primary - master species */ - CReaction rxn_x; /* reaction to be used in model */ - LDBLE tot_g_moles; /* (1 + sum(g)) * moles */ - LDBLE tot_dh2o_moles; /* sum(moles*g*Ws/Waq) */ - LDBLE cd_music[5]; - LDBLE dz[3]; - DELTA_V_UNIT original_deltav_units; + // switch to check equation for charge and element balance + int check_equation = TRUE; + // data base reaction + CReaction rxn; + // reaction converted to secondary and primary master species + CReaction rxn_s; + // reaction to be used in model + CReaction rxn_x; + // (1 + sum(g)) * moles + LDBLE tot_g_moles = 0; + // sum(moles*g*Ws/Waq) + LDBLE tot_dh2o_moles = 0; + LDBLE cd_music[5] = { 0,0,0,0,0 }; + LDBLE dz[3] = { 0,0,0 }; + DELTA_V_UNIT original_deltav_units = cm3_per_mol; }; struct logk { /* Named log K's */ - const char *name; /* name of species */ - LDBLE lk; /* log10 k at working temperature */ - LDBLE log_k[MAX_LOG_K_INDICES]; /* log kt0, delh, 6 coefficients analalytical expression */ - DELTA_H_UNIT original_units; /* enum with original delta H units */ - int done; + const char *name = NULL; // name of species + LDBLE lk = 0.0; // log10 k at working temperature + LDBLE log_k[MAX_LOG_K_INDICES] = // log kt0, delh, 6 coefficients analalytical expression + { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }; + DELTA_H_UNIT original_units = kjoules; // enum with original delta H units + int done = FALSE; std::vector add_logk; - LDBLE log_k_original[MAX_LOG_K_INDICES]; /* log kt0, delh, 5 coefficients analalytical expression */ - DELTA_V_UNIT original_deltav_units; + LDBLE log_k_original[MAX_LOG_K_INDICES] = // log kt0, delh, 5 coefficients analalytical expression */ + { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }; + DELTA_V_UNIT original_deltav_units = cm3_per_mol; }; /*---------------------------------------------------------------------- * Phases *---------------------------------------------------------------------- */ struct phase -{ /* all data pertinent to a pure solid phase */ - const char *name; /* name of species */ - const char *formula; /* chemical formula */ - int in; /* species used in model if TRUE */ - LDBLE lk; /* log10 k at working temperature */ - LDBLE logk[MAX_LOG_K_INDICES]; /* log kt0, delh, 6 coefficients analalytical expression */ - DELTA_H_UNIT original_units; /* enum with original delta H units */ - DELTA_V_UNIT original_deltav_units; +{ /* all data pertinent to a pure solid phase */ + const char *name = NULL; //name of species + const char *formula = NULL; // chemical formula + int in = FALSE; // species used in model if TRUE + LDBLE lk = 0; // log10 k at working temperature + LDBLE logk[MAX_LOG_K_INDICES] = // log kt0, delh, 6 coefficients analalytical expression + { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }; + DELTA_H_UNIT original_units = kjoules; // enum with original delta H units + DELTA_V_UNIT original_deltav_units = cm3_per_mol; std::vector add_logk; - LDBLE moles_x; - LDBLE delta_max; - LDBLE p_soln_x; - LDBLE fraction_x; - LDBLE log10_lambda, log10_fraction_x; - LDBLE dn, dnb, dnc; - LDBLE gn, gntot; - LDBLE gn_n, gntot_n; - LDBLE t_c, p_c, omega; /* gas: critical TK, critical P(atm), Pitzer acentric coeff */ - LDBLE pr_a, pr_b, pr_alpha; /* Peng-Robinson parm's */ - LDBLE pr_tk, pr_p; /* Temperature (K), Pressure (atm) */ - LDBLE pr_phi; /* fugacity coefficient (-) */ - LDBLE pr_aa_sum2; /* for calculating multicomponent phi */ - LDBLE delta_v[9]; /* delta_v[0] = [1] + [2]*T + [3]/T + [4]*log10(T) + [5]/T^2 + [6]*T^2 + [7]*P */ - LDBLE pr_si_f; /* si adapter: log10(phi) - delta_v[0] * (P - 1) /RT */ - bool pr_in; /* Peng-Robinson in the calc's, or not */ - - int type; /* flag indicating presence in model and types of equations */ - std::vector next_elt; /* pointer to list of elements in phase */ + LDBLE moles_x = 0; + LDBLE delta_max = 0; + LDBLE p_soln_x = 0; + LDBLE fraction_x = 0; + LDBLE log10_lambda = 0; + LDBLE log10_fraction_x = 0; + LDBLE dn = 0, dnb = 0, dnc = 0; + LDBLE gn = 0, gntot = 0; + LDBLE gn_n = 0, gntot_n = 0; + LDBLE t_c = 0, p_c = 0, omega = 0; // gas: critical TK, critical P(atm), Pitzer acentric coeff + LDBLE pr_a = 0, pr_b = 0, pr_alpha = 0; // Peng-Robinson parm's + LDBLE pr_tk = 0, pr_p = 0; // Temperature (K), Pressure (atm) + LDBLE pr_phi = 0; // fugacity coefficient (-) + LDBLE pr_aa_sum2 = 0; // for calculating multicomponent phi + LDBLE delta_v[9] = { 0,0,0,0,0,0,0,0,0 }; // delta_v[0] = [1] + [2]*T + [3]/T + [4]*log10(T) + [5]/T^2 + [6]*T^2 + [7]*P + LDBLE pr_si_f = 0; // si adapter: log10(phi) - delta_v[0] * (P - 1) /RT + bool pr_in = false; // Peng-Robinson in the calc's, or not + int type = SOLID; // flag indicating presence in model and types of equations + std::vector next_elt; // list of elements in phase std::vector next_sys_total; - int check_equation; /* switch to check equation for charge and element balance */ - CReaction rxn; /* pointer to data base reaction */ - CReaction rxn_s; /* pointer to reaction converted to secondary and primary - master species */ - CReaction rxn_x; /* reaction to be used in model */ - int replaced; /* equation contains solids or gases */ - int in_system; + int check_equation = TRUE; // switch to check equation for charge and element balance + CReaction rxn; // pointer to data base reaction + CReaction rxn_s; // pointer to reaction converted to secondary and primary master species + CReaction rxn_x; // reaction to be used in model + int replaced = FALSE; // equation contains solids or gases + int in_system = FALSE; }; /*---------------------------------------------------------------------- * Master species *---------------------------------------------------------------------- */ struct master - { /* list of name and number of elements in an equation */ - int in; /* TRUE if in model, FALSE if out, REWRITE if other mb eq */ - size_t number; /* sequence number in list of masters */ - int last_model; /* saved to determine if model has changed */ - int type; /* AQ or EX */ - int primary; /* TRUE if master species is primary */ - LDBLE coef; /* coefficient of element in master species */ - LDBLE total; /* total concentration for element or valence state */ - LDBLE isotope_ratio; - LDBLE isotope_ratio_uncertainty; - int isotope; - LDBLE total_primary; - /* LDBLE la; */ /* initial guess of master species log activity */ - struct element *elt; /* element structure */ - LDBLE alk; /* alkalinity of species */ - LDBLE gfw; /* default gfw for species */ - const char *gfw_formula; /* formula from which to calcuate gfw */ - struct unknown *unknown; /* pointer to unknown structure */ - struct species *s; /* pointer to species structure */ - CReaction rxn_primary; /* reaction writes master species in terms of primary - master species */ - CReaction rxn_secondary; /* reaction writes master species in terms of secondary - master species */ - const char * pe_rxn; - int minor_isotope; + { // list of name and number of elements in an equation + // TRUE if in model, FALSE if out, REWRITE if other mb eq + int in = 0; + // sequence number in list of masters + size_t number = 0; + // saved to determine if model has changed + int last_model = FALSE; + // AQ or EX + int type = 0; + // TRUE if master species is primary + int primary = FALSE; + // coefficient of element in master species + LDBLE coef = 0; + // total concentration for element or valence state + LDBLE total = 0; + LDBLE isotope_ratio = 0; + LDBLE isotope_ratio_uncertainty = 0; + int isotope = 0; + LDBLE total_primary = 0; + // element structure + struct element *elt = NULL; + // alkalinity of species + LDBLE alk = 0; + // default gfw for species + LDBLE gfw = 1; + // formula from which to calcuate gfw + const char *gfw_formula = NULL; + // pointer to unknown structure + struct unknown *unknown = NULL; + // pointer to species structure + struct species *s = NULL; + // reaction writes master species in terms of primary master species + CReaction rxn_primary; + // reaction writes master species in terms of secondary master species + CReaction rxn_secondary; + const char * pe_rxn = NULL; + int minor_isotope = FALSE; }; /*---------------------------------------------------------------------- * Unknowns *---------------------------------------------------------------------- */ struct unknown { - int type; - LDBLE moles; - LDBLE ln_moles; - LDBLE f; - LDBLE sum; - LDBLE delta; - LDBLE la; - size_t number; - const char *description; + int type = 0; + LDBLE moles = 0; + LDBLE ln_moles = 0; + LDBLE f = 0; + LDBLE sum = 0; + LDBLE delta = 0; + LDBLE la = 0; + size_t number = 0; + const char *description = NULL; std::vector master; - struct phase *phase; - LDBLE si; - int n_gas_phase_user; - struct species *s; - const char * exch_comp; - const char *pp_assemblage_comp_name; - void *pp_assemblage_comp_ptr; - const char * ss_name; - void *ss_ptr; - const char * ss_comp_name; - void *ss_comp_ptr; - int ss_comp_number; - int ss_in; - const char *surface_comp; - const char *surface_charge; - LDBLE related_moles; - struct unknown *potential_unknown, *potential_unknown1, - *potential_unknown2; + struct phase *phase = NULL; + LDBLE si = 0; + int n_gas_phase_user = 0; + struct species *s = NULL; + const char * exch_comp = NULL; + const char *pp_assemblage_comp_name = NULL; + void *pp_assemblage_comp_ptr = NULL; + const char * ss_name = NULL; + void *ss_ptr = NULL; + const char * ss_comp_name = NULL; + void *ss_comp_ptr = NULL; + int ss_comp_number = 0; + int ss_in = FALSE; + const char *surface_comp = NULL; + const char *surface_charge = NULL; + LDBLE related_moles = 0; + struct unknown *potential_unknown = NULL, *potential_unknown1 = NULL, + *potential_unknown2 = NULL; std::vector comp_unknowns; /* list for CD_MUSIC of comps that contribute to 0 plane mass-balance term */ - struct unknown *phase_unknown; - LDBLE mass_water; - int dissolve_only; - LDBLE inert_moles; - LDBLE V_m; - LDBLE pressure; - int mb_number; - int iteration; + struct unknown *phase_unknown = NULL; + LDBLE mass_water = 1; + int dissolve_only = FALSE; + LDBLE inert_moles = 0; + LDBLE V_m = 0; + LDBLE pressure = 1; + int mb_number = 0; + int iteration = 0; }; /*---------------------------------------------------------------------- @@ -664,108 +721,108 @@ struct unknown *---------------------------------------------------------------------- */ struct reaction_temp { - LDBLE logk[MAX_LOG_K_INDICES]; - LDBLE dz[3]; + LDBLE logk[MAX_LOG_K_INDICES] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }; + LDBLE dz[3] = { 0,0,0 }; std::vector token; }; struct rxn_token_temp { /* data for equations, aq. species or minerals */ - const char *name; /* pointer to a species name (formula) */ - LDBLE z; /* charge on species */ - struct species *s; - struct unknown *unknown; - LDBLE coef; /* coefficient of species name */ + const char *name = NULL; /* pointer to a species name (formula) */ + LDBLE z = 0; /* charge on species */ + struct species *s = NULL; + struct unknown *unknown = NULL; + LDBLE coef = 0; /* coefficient of species name */ }; struct unknown_list { - struct unknown *unknown; - LDBLE *source; - LDBLE *gamma_source; + struct unknown *unknown = NULL; + LDBLE *source = NULL; + LDBLE *gamma_source = NULL; /* int row; */ /* int col; */ - LDBLE coef; + LDBLE coef = 0; }; /* ---------------------------------------------------------------------- * Print * ---------------------------------------------------------------------- */ struct prints { - int all; - int initial_solutions; - int initial_exchangers; - int reactions; - int gas_phase; - int ss_assemblage; - int pp_assemblage; - int surface; - int exchange; - int kinetics; - int totals; - int eh; - int species; - int saturation_indices; - int irrev; - int mix; - int reaction; - int use; - int logfile; - int punch; - int status; - int inverse; - int dump; - int user_print; - int headings; - int user_graph; - int echo_input; - int warnings; - int initial_isotopes; - int isotope_ratios; - int isotope_alphas; - int hdf; - int alkalinity; + int all = 0; + int initial_solutions = 0; + int initial_exchangers = 0; + int reactions = 0; + int gas_phase = 0; + int ss_assemblage = 0; + int pp_assemblage = 0; + int surface = 0; + int exchange = 0; + int kinetics = 0; + int totals = 0; + int eh = 0; + int species = 0; + int saturation_indices = 0; + int irrev = 0; + int mix = 0; + int reaction = 0; + int use = 0; + int logfile = 0; + int punch = 0; + int status = 0; + int inverse = 0; + int dump = 0; + int user_print = 0; + int headings = 0; + int user_graph = 0; + int echo_input = 0; + int warnings = 0; + int initial_isotopes = 0; + int isotope_ratios = 0; + int isotope_alphas = 0; + int hdf = 0; + int alkalinity = 0; }; /* ---------------------------------------------------------------------- * RATES * ---------------------------------------------------------------------- */ struct rate { - const char *name; + const char *name = NULL; std::string commands; - int new_def; - void *linebase; - void *varbase; - void *loopbase; + int new_def = 0; + void *linebase = NULL; + void *varbase = NULL; + void *loopbase = NULL; }; /* ---------------------------------------------------------------------- * GLOBAL DECLARATIONS * ---------------------------------------------------------------------- */ struct spread_row { - int count; - int empty, string, number; + int count = 0; + int empty = 0, string = 0, number = 0; std::vector char_vector; std::vector d_vector; std::vector type_vector; }; struct defaults { - LDBLE temp; - LDBLE density; - bool calc_density; - const char *units; - const char *redox; - LDBLE ph; - LDBLE pe; - LDBLE water; + LDBLE temp = 0; + LDBLE density = 0; + bool calc_density = 0; + const char *units = NULL; + const char *redox = NULL; + LDBLE ph = 7; + LDBLE pe = 4; + LDBLE water = 1; std::vector iso; - LDBLE pressure; /* pressure in atm */ + LDBLE pressure = 1; /* pressure in atm */ }; struct spread_sheet { - struct spread_row *heading; - struct spread_row *units; - int count_rows; - struct spread_row **rows; + struct spread_row *heading = NULL; + struct spread_row *units = NULL; + int count_rows = 0; + struct spread_row **rows = NULL; struct defaults defaults; }; /* ---------------------------------------------------------------------- @@ -773,103 +830,120 @@ struct spread_sheet * ---------------------------------------------------------------------- */ struct master_isotope { - const char *name; - struct master *master; - struct element *elt; - const char *units; - LDBLE standard; - LDBLE ratio; - LDBLE moles; - int total_is_major; - int minor_isotope; + const char *name = NULL; + struct master *master = NULL; + struct element *elt = NULL; + const char *units = NULL; + LDBLE standard = 0; + LDBLE ratio = 0; + LDBLE moles = 0; + int total_is_major = 0; + int minor_isotope = 0; }; struct calculate_value { - const char *name; - LDBLE value; + const char *name = NULL; + LDBLE value = 0; std::string commands; - int new_def; - int calculated; - void *linebase; - void *varbase; - void *loopbase; + int new_def = 0; + int calculated = 0; + void *linebase = NULL; + void *varbase = NULL; + void *loopbase = NULL; }; struct isotope_ratio { - const char *name; - const char *isotope_name; - LDBLE ratio; - LDBLE converted_ratio; + const char *name = NULL; + const char *isotope_name = NULL; + LDBLE ratio = 0; + LDBLE converted_ratio = 0; }; struct isotope_alpha { - const char *name; - const char *named_logk; - LDBLE value; + const char *name = NULL; + const char *named_logk = NULL; + LDBLE value = 0; }; struct system_species { - char *name; - char *type; - LDBLE moles; + char *name = NULL; + char *type = NULL; + LDBLE moles = 0; }; /* tally.c ------------------------------- */ struct tally_buffer { - const char *name; - struct master *master; - LDBLE moles; - LDBLE gfw; + const char *name = NULL; + struct master *master = NULL; + LDBLE moles = 0; + LDBLE gfw = 0; }; struct tally { - const char *name; - enum entity_type type; - const char *add_formula; - LDBLE moles; + const char *name = NULL; + enum entity_type type = UnKnown; + const char *add_formula = NULL; + LDBLE moles = 0; std::vector formula; /* * first total is initial * second total is final * third total is difference (final - initial) */ - struct tally_buffer *total[3]; + struct tally_buffer* total[3] = { NULL, NULL, NULL }; }; /* transport.c ------------------------------- */ struct spec { - const char *name; /* name of species */ - const char *aq_name; /* name of aqueous species in EX species */ - int type; /* type: AQ or EX */ - LDBLE a; /* activity */ - LDBLE lm; /* log(concentration) */ - LDBLE lg; /* log(gamma) */ - LDBLE c; /* concentration for AQ, equivalent fraction for EX */ - LDBLE z; /* charge number */ - LDBLE Dwt; /* temperature corrected free water diffusion coefficient, m2/s */ - LDBLE dw_t; /* temperature factor for Dw */ - LDBLE erm_ddl; /* enrichment factor in ddl */ + // name of species + const char *name = NULL; + // name of aqueous species in EX species + const char *aq_name = NULL; + // type: AQ or EX + int type = 0; + // activity + LDBLE a = 0; + // log(concentration) + LDBLE lm = 0; + // log(gamma) + LDBLE lg = 0; + // concentration for AQ, equivalent fraction for EX + LDBLE c = 0; + // charge number + LDBLE z = 0; + // temperature corrected free water diffusion coefficient, m2/s + LDBLE Dwt = 0; + // temperature factor for Dw + LDBLE dw_t = 0; + // enrichment factor in ddl + LDBLE erm_ddl = 0; }; struct sol_D { - int count_spec; /* number of aqueous + exchange species */ - int count_exch_spec; /* number of exchange species */ - LDBLE exch_total, x_max, tk_x; /* total moles of X-, max X- in transport step in sol_D[1], tk */ - LDBLE viscos_f; /* (tk_x * viscos_0_25) / (298 * viscos) */ - struct spec *spec; - int spec_size; + // number of aqueous + exchange species + int count_spec = 0; + // number of exchange species + int count_exch_spec = 0; + // total moles of X-, max X- in transport step in sol_D[1], tk + LDBLE exch_total = 0, x_max = 0, tk_x = 0; + // (tk_x * viscos_0_25) / (298 * viscos) + LDBLE viscos_f = 0; + struct spec *spec = NULL; + int spec_size = 0; }; struct J_ij { - const char *name; - LDBLE tot1, tot2, tot_stag, charge; /* species change in cells i and j */ + const char *name = NULL; + // species change in cells i and j + LDBLE tot1 = 0, tot2 = 0, tot_stag = 0, charge = 0; }; struct M_S { - const char *name; - LDBLE tot1, tot2, tot_stag, charge; /* master species transport in cells i and j */ + const char *name = NULL; + // master species transport in cells i and j + LDBLE tot1 = 0, tot2 = 0, tot_stag = 0, charge = 0; }; // Pitzer definitions typedef enum @@ -880,13 +954,13 @@ typedef enum struct pitz_param { - const char *species[3]; - int ispec[3]; - pitz_param_type type; - LDBLE p; + const char* species[3] = { NULL,NULL,NULL }; + int ispec[3] = { 0,0,0 }; + pitz_param_type type = TYPE_B0; + LDBLE p = 0; union { - LDBLE b0; + LDBLE b0 = 0; LDBLE b1; LDBLE b2; LDBLE c0; @@ -901,26 +975,26 @@ struct pitz_param LDBLE eps1; LDBLE aphi; } U; - LDBLE a[6]; - LDBLE alpha; - LDBLE os_coef; - LDBLE ln_coef[3]; - struct theta_param *thetas; + LDBLE a[6] = { 0,0,0,0,0,0 }; + LDBLE alpha = 0; + LDBLE os_coef = 0; + LDBLE ln_coef[3] = { 0,0,0 }; + struct theta_param *thetas = NULL; }; struct theta_param { - LDBLE zj; - LDBLE zk; - LDBLE etheta; - LDBLE ethetap; + LDBLE zj = 0; + LDBLE zk = 0; + LDBLE etheta = 0; + LDBLE ethetap = 0; }; struct const_iso { - const char *name; - LDBLE value; - LDBLE uncertainty; + const char *name = NULL; + LDBLE value = 0; + LDBLE uncertainty = 0; }; #endif /* _INC_GLOBAL_STRUCTURES_H */ diff --git a/integrate.cpp b/integrate.cpp index d1eb4bbd..4af3c91b 100644 --- a/integrate.cpp +++ b/integrate.cpp @@ -338,7 +338,7 @@ polint(LDBLE * xa, LDBLE * ya, int n, LDBLE xv, LDBLE * yv, LDBLE * dy) } if (2 * ns < (n - m)) { - *dy = c[ns + 1]; + *dy = c[(size_t)ns + 1]; } else { diff --git a/inverse.cpp b/inverse.cpp index 5df8e355..e22c0f7b 100644 --- a/inverse.cpp +++ b/inverse.cpp @@ -324,7 +324,7 @@ setup_inverse(struct inverse *inv_ptr) i_alk = i; if (strcmp(master_ptr->elt->name, "C(4)") == 0) i_carb = i; - inv_ptr->elts[i].master->in = count_rows_t; + inv_ptr->elts[i].master->in = (int)count_rows_t; row_name[count_rows_t] = inv_ptr->elts[i].master->elt->name; count_rows_t++; } @@ -676,9 +676,9 @@ setup_inverse(struct inverse *inv_ptr) } if (inv_ptr->isotopes.size() != 0) { - for (j = 0; j < inv_ptr->isotopes.size(); j++) + for (size_t j = 0; j < inv_ptr->isotopes.size(); j++) { - isotope_balance_equation(inv_ptr, count_rows, j); + isotope_balance_equation(inv_ptr, (int)count_rows, (int)j); sprintf(token, "%d%s", (int) inv_ptr->isotopes[j].isotope_number, inv_ptr->isotopes[j].elt_name); row_name[count_rows] = string_hsave(token); @@ -1003,10 +1003,10 @@ solve_inverse(struct inverse *inv_ptr) unsigned long minimal_bits, good_bits; char token[MAX_LENGTH]; - n = count_unknowns; /* columns in A, C, E */ - klmd = max_row_count - 2; - nklmd = n + klmd; - n2d = n + 2; + n = (int)count_unknowns; /* columns in A, C, E */ + klmd = (max_row_count - 2); + nklmd = (n + klmd); + n2d = (size_t)n + 2; max_good = MAX_MODELS; max_bad = MAX_MODELS; @@ -1063,23 +1063,20 @@ solve_inverse(struct inverse *inv_ptr) * All combinations of solutions */ first = TRUE; - for (; get_bits(soln_bits, inv_ptr->count_solns - 2, - inv_ptr->count_solns - 1) > 0; soln_bits--) + for (; get_bits(soln_bits, (int)(inv_ptr->count_solns - 2), + (int)(inv_ptr->count_solns - 1)) > 0; soln_bits--) { /* * Loop through all models of of descending size */ - for (model_size = inv_ptr->phases.size(); model_size >= 0; - model_size--) + for (model_size = (int)inv_ptr->phases.size(); model_size >= 0; model_size--) { first_of_model_size = TRUE; quit = TRUE; - while (next_set_phases(inv_ptr, first_of_model_size, model_size) - == TRUE) + while (next_set_phases(inv_ptr, first_of_model_size, model_size) == TRUE) { first_of_model_size = FALSE; - current_bits = - (soln_bits << inv_ptr->phases.size()) + phase_bits; + current_bits = (soln_bits << inv_ptr->phases.size()) + phase_bits; if (subset_bad(current_bits) == TRUE || subset_minimal(current_bits) == TRUE) @@ -1115,18 +1112,16 @@ solve_inverse(struct inverse *inv_ptr) good_bits = current_bits; for (size_t i = 0; i < inv_ptr->phases.size(); i++) { - if (equal(inv_delta1[i + inv_ptr->count_solns], 0.0, TOL) == - TRUE) + if (equal(inv_delta1[i + inv_ptr->count_solns], 0.0, TOL) == TRUE) { - good_bits = set_bit(good_bits, i, 0); + good_bits = set_bit(good_bits, (int)i, 0); } } - for (i = 0; i < inv_ptr->count_solns; i++) + for (size_t i = 0; i < inv_ptr->count_solns; i++) { if (equal(inv_delta1[i], 0.0, TOL) == TRUE) { - good_bits = - set_bit(good_bits, i + inv_ptr->phases.size(), 0); + good_bits = set_bit(good_bits, (int)(i + inv_ptr->phases.size()), 0); } } /* @@ -1265,25 +1260,23 @@ minimal_solve(struct inverse *inv_ptr, unsigned long minimal_bits) * Starting with phases indicated in minimal bits, sequentially * remove phases to find minimal solution */ - int i; unsigned long temp_bits_l; if (debug_inverse == TRUE) { output_msg(sformatf( "Beginning minimal solve: \n")); - bit_print(minimal_bits, inv_ptr->phases.size() + inv_ptr->count_solns); + bit_print(minimal_bits, (int)(inv_ptr->phases.size() + inv_ptr->count_solns)); } - for (i = 0; i < inv_ptr->phases.size() + inv_ptr->count_solns - 1; i++) + for (size_t i = 0; i < inv_ptr->phases.size() + inv_ptr->count_solns - 1; i++) { - if (get_bits(minimal_bits, i, 1) == 0) + if (get_bits(minimal_bits, (int)i, 1) == 0) continue; - temp_bits_l = 1 << i; /* 0's and one 1 */ + temp_bits_l = 1 << (int)i; /* 0's and one 1 */ temp_bits_l = ~temp_bits_l; /* 1's and one 0 */ minimal_bits = minimal_bits & temp_bits_l; if (debug_inverse == TRUE) { output_msg(sformatf( "Solving for minimal\n")); - bit_print(minimal_bits, - inv_ptr->phases.size() + inv_ptr->count_solns); + bit_print(minimal_bits, (int)(inv_ptr->phases.size() + inv_ptr->count_solns)); } /* @@ -1308,23 +1301,23 @@ minimal_solve(struct inverse *inv_ptr, unsigned long minimal_bits) if (debug_inverse == TRUE) { output_msg(sformatf( "\n\nMINIMAL MODEL\n\n")); - bit_print(minimal_bits, inv_ptr->phases.size() + inv_ptr->count_solns); + bit_print(minimal_bits, (int)(inv_ptr->phases.size() + inv_ptr->count_solns)); } solve_with_mask(inv_ptr, minimal_bits); unsigned long actual_bits = 0; - for (i = 0; i < inv_ptr->count_solns; i++) + for (size_t i = 0; i < inv_ptr->count_solns; i++) { if (equal(inv_delta1[i], 0.0, TOL) == FALSE) { - actual_bits = set_bit(actual_bits, i + inv_ptr->phases.size(), 1); + actual_bits = set_bit(actual_bits, (int)(i + inv_ptr->phases.size()), 1); } } for (size_t i = 0; i < inv_ptr->phases.size(); i++) { if (equal(inv_delta1[i + inv_ptr->count_solns], 0.0, TOL) == FALSE) { - actual_bits = set_bit(actual_bits, i, 1); + actual_bits = set_bit(actual_bits, (int)i, 1); } } if (actual_bits != minimal_bits) @@ -1347,10 +1340,10 @@ solve_with_mask(struct inverse *inv_ptr, unsigned long cur_bits) /* * Calculate dimensions */ - k = row_mb; /* rows in A */ - l = row_epsilon - row_mb; /* rows in C */ - m = count_rows - row_epsilon; /* rows in E */ - n = count_unknowns; + k = (int)row_mb; /* rows in A */ + l = (int)(row_epsilon - row_mb); /* rows in C */ + m = (int)(count_rows - row_epsilon); /* rows in E */ + n = (int)count_unknowns; @@ -1389,7 +1382,7 @@ solve_with_mask(struct inverse *inv_ptr, unsigned long cur_bits) } output_msg(sformatf( "\nA and B arrays:\n\n")); - array_print(&array1[0], k + l + m, n + 1, max_column_count); + array_print(&array1[0], k + l + m, n + 1, (int)max_column_count); output_msg(sformatf( "\nInput delta vector:\n")); for (i = 0; i < n; i++) @@ -1439,9 +1432,9 @@ solve_with_mask(struct inverse *inv_ptr, unsigned long cur_bits) &kode, toler, &iter, delta2, inv_res, &error, inv_cu, inv_iu, inv_is, TRUE); } #else - cl1(k, l, m, n, - nklmd, n2d, &array1[0], - &kode, toler, &iter, &delta2[0], &inv_res[0], &error, &inv_cu[0], &inv_iu[0], &inv_is[0], TRUE); + cl1(k, l, m, n, (int)nklmd, (int)n2d, &array1[0], + &kode, toler, &iter, &delta2[0], &inv_res[0], + &error, &inv_cu[0], &inv_iu[0], &inv_is[0], TRUE); #endif if (kode == 3) { @@ -1642,8 +1635,8 @@ print_model(struct inverse *inv_ptr) /* * Prints model */ - int i, j, k; - int column; + int i, j; + size_t column; int print_msg; cxxSolution *solution_ptr; struct master *master_ptr; @@ -1821,15 +1814,15 @@ print_model(struct inverse *inv_ptr) { if (inv_ptr->phases[i].isotopes.size() == 0) continue; - j = col_phases + i; + size_t j = col_phases + i; if (equal(inv_delta1[j], 0.0, toler) == TRUE && equal(min_delta[j], 0.0, toler) == TRUE && equal(max_delta[j], 0.0, toler) == TRUE) continue; std::vector& isotope_ref = inv_ptr->phases[i].isotopes; - for (j = 0; j < inv_ptr->isotopes.size(); j++) + for (size_t j = 0; j < inv_ptr->isotopes.size(); j++) { - for (k = 0; k < inv_ptr->phases[i].isotopes.size(); k++) + for (size_t k = 0; k < inv_ptr->phases[i].isotopes.size(); k++) { if (inv_ptr->isotopes[j].elt_name != isotope_ref[k].elt_name || @@ -1837,8 +1830,7 @@ print_model(struct inverse *inv_ptr) isotope_ref[k].isotope_number) continue; d1 = isotope_ref[k].ratio; - column = - col_phase_isotopes + i * inv_ptr->isotopes.size() + j; + column = col_phase_isotopes + i * inv_ptr->isotopes.size() + j; if (inv_delta1[col_phases + i] != 0.0) { d2 = inv_delta1[column] / inv_delta1[col_phases + i]; @@ -1913,7 +1905,6 @@ print_model(struct inverse *inv_ptr) } // appt, calculate and print SI's - int i1, i2; LDBLE t_i, p_i, iap, lk, t; const char *name; struct rxn_token *rxn_ptr; @@ -1930,7 +1921,7 @@ print_model(struct inverse *inv_ptr) output_msg(sformatf("%d at %3d K, %3d atm)\n", inv_ptr->solns[i], int(t_i), int(floor(p_i + 0.5)))); p_i *= PASCAL_PER_ATM; - for (i = col_phases; i < col_redox; i++) + for (size_t i = col_phases; i < col_redox; i++) { if (equal(inv_delta1[i], 0.0, toler) == TRUE && equal(min_delta[i], 0.0, toler) == TRUE && @@ -1950,13 +1941,13 @@ print_model(struct inverse *inv_ptr) "%15.15s %12.3e %12.3e %12.3e %-25.25s (", col_name[i], (double)d1, (double)d2, (double)d3, inv_ptr->phases[i - col_phases].phase->formula)); - i1 = 0; - for (; i1 < (int)phases.size(); i1++) + size_t i1 = 0; + for (; i1 < phases.size(); i1++) { if (Utilities::strcmp_nocase(phases[i1]->name, col_name[i])) continue; reaction_ptr = &phases[i1]->rxn_s; - for (i2 = 0; i2 < inv_ptr->count_solns; i2++) + for (size_t i2 = 0; i2 < inv_ptr->count_solns; i2++) { solution_ptr = Utilities::Rxn_find(Rxn_solution_map, inv_ptr->solns[i2]); @@ -2002,7 +1993,7 @@ print_model(struct inverse *inv_ptr) output_msg(sformatf(")\n")); } output_msg(sformatf( "\n%-25.25s\n", "Redox mole transfers:")); - for (i = col_redox; i < col_epsilon; i++) + for (size_t i = col_redox; i < col_epsilon; i++) { if (equal(inv_delta1[i], 0.0, toler) == TRUE) continue; @@ -2075,7 +2066,7 @@ punch_model_heading(struct inverse *inv_ptr) /* * Print phase names */ - for (i = col_phases; i < col_redox; i++) + for (size_t i = col_phases; i < col_redox; i++) { std::string tok1(col_name[i]); @@ -2087,9 +2078,7 @@ punch_model_heading(struct inverse *inv_ptr) inverse_heading_names.push_back(sformatf("%*s\t", l, tok1.c_str())); inverse_heading_names.push_back(sformatf("%*s\t", l, tok2.c_str())); } - - size_t j; - for (j = 0; j < inverse_heading_names.size(); j++) + for (size_t j = 0; j < inverse_heading_names.size(); j++) { fpunchf_heading(inverse_heading_names[j].c_str()); //user_punch_headings[j] = string_hsave(heading_names[j].c_str()); @@ -2178,7 +2167,7 @@ punch_model(struct inverse *inv_ptr) /* * write phase transfers */ - for (i = col_phases; i < col_redox; i++) + for (size_t i = col_phases; i < col_redox; i++) { d1 = inv_delta1[i]; d2 = min_delta[i]; @@ -2264,7 +2253,7 @@ next_set_phases(struct inverse *inv_ptr, { min_position[i] = i; now[i] = i; - max_position[i] = inv_ptr->phases.size() - model_size + i; + max_position[i] = (int)inv_ptr->phases.size() - model_size + i; } } else @@ -2328,14 +2317,14 @@ range(struct inverse *inv_ptr, unsigned long cur_bits) { if (inv_ptr->phases[i].force == TRUE) { - cur_bits = set_bit(cur_bits, i, 1); + cur_bits = set_bit(cur_bits, (int)i, 1); } } else { if (inv_ptr->force_solns[i - inv_ptr->phases.size()] == TRUE) { - cur_bits = set_bit(cur_bits, i, 1); + cur_bits = set_bit(cur_bits, (int)i, 1); } } } @@ -2348,11 +2337,11 @@ range(struct inverse *inv_ptr, unsigned long cur_bits) * Switch bits so that phases are high and solutions are low */ bits = - get_bits(cur_bits, inv_ptr->phases.size() + inv_ptr->count_solns - 1, - inv_ptr->count_solns); + get_bits(cur_bits, (int)(inv_ptr->phases.size() + inv_ptr->count_solns) - 1, + (int)inv_ptr->count_solns); bits += - (get_bits(cur_bits, inv_ptr->phases.size() - 1, inv_ptr->phases.size()) - << inv_ptr->count_solns); + (get_bits(cur_bits, (int)inv_ptr->phases.size() - 1, (int)inv_ptr->phases.size()) + << (int)inv_ptr->count_solns); /* * Do range calculation */ @@ -2371,10 +2360,10 @@ range(struct inverse *inv_ptr, unsigned long cur_bits) */ for (f = -1; f < 2; f += 2) { - k = row_mb; /* rows in A */ - l = row_epsilon - row_mb; /* rows in C */ - m = count_rows - row_epsilon; /* rows in E */ - n = count_unknowns; /* number of variables */ + k = (int)row_mb; /* rows in A */ + l = (int)(row_epsilon - row_mb); /* rows in C */ + m = (int)(count_rows - row_epsilon); /* rows in E */ + n = (int)count_unknowns; /* number of variables */ /* * Copy equations */ @@ -2425,7 +2414,7 @@ range(struct inverse *inv_ptr, unsigned long cur_bits) col_name[col_back[j]], (double) delta2[j])); } output_msg(sformatf( "\nA and B arrays:\n\n")); - array_print(&array1[0], k + l + m, n + 1, max_column_count); + array_print(&array1[0], k + l + m, n + 1, (int)max_column_count); } kode = 1; iter = 200; @@ -2445,7 +2434,7 @@ range(struct inverse *inv_ptr, unsigned long cur_bits) inv_res, &error2, inv_cu, inv_iu, inv_is, TRUE); } #else - cl1(k, l, m, n, nklmd, n2d, &array1[0], &kode, toler, &iter, &delta2[0], + cl1(k, l, m, n, (int)nklmd, (int)n2d, &array1[0], &kode, toler, &iter, &delta2[0], &inv_res[0], &error2, &inv_cu[0], &inv_iu[0], &inv_is[0], TRUE); #endif if (kode != 0) @@ -2517,7 +2506,7 @@ shrink(struct inverse *inv_ptr, LDBLE * array_in, LDBLE * array_out, */ int i, j, row; int k1, l1, m1; - int cur_col, column; + size_t cur_col, column; int nonzero; /* * Copy array_in to array_out @@ -2562,7 +2551,7 @@ shrink(struct inverse *inv_ptr, LDBLE * array_in, LDBLE * array_out, */ for (i = 0; i < (inv_ptr->count_solns - 1); i++) { - if (get_bits(cur_bits, inv_ptr->phases.size() + i, 1) == 0) + if (get_bits(cur_bits, (int)inv_ptr->phases.size() + i, 1) == 0) { col_back_l[i] = -1; /* drop all epsilons for the solution */ @@ -2580,11 +2569,10 @@ shrink(struct inverse *inv_ptr, LDBLE * array_in, LDBLE * array_out, /* drop isotopes */ if (inv_ptr->isotopes.size() > 0) { - for (j = 0; j < inv_ptr->isotope_unknowns.size(); j++) + for (size_t j = 0; j < inv_ptr->isotope_unknowns.size(); j++) { - column = - col_isotopes + i * inv_ptr->isotope_unknowns.size() + - j; + column = col_isotopes + + i * inv_ptr->isotope_unknowns.size() + j; col_back_l[column] = -1; } } @@ -2594,13 +2582,13 @@ shrink(struct inverse *inv_ptr, LDBLE * array_in, LDBLE * array_out, /* * Drop epsilons not used */ - for (i = col_epsilon; i < *n; i++) + for (i = (int)col_epsilon; i < *n; i++) { if (col_back_l[i] < 0) continue; for (j = 0; j < (*k + *l + *m); j++) { - if (array_out[j * max_column_count + i] != 0) + if (array_out[(size_t)j * max_column_count + (size_t)i] != 0) break; } if (j == (*k + *l + *m)) @@ -2630,7 +2618,7 @@ shrink(struct inverse *inv_ptr, LDBLE * array_in, LDBLE * array_out, delta_l[cur_col] = delta_l[i]; cur_col++; } - *n = cur_col - 1; + *n = (int)cur_col - 1; /* * Eliminate unnecessary optimization eqns */ @@ -2801,10 +2789,10 @@ check_solns(struct inverse *inv_ptr) /* * Check for feasibility of charge balance with given uncertainties */ - k = row_mb; /* rows in A */ - l = row_epsilon - row_mb; /* rows in C */ - m = count_rows - row_epsilon; /* rows in E */ - n = count_unknowns; /* number of variables */ + k = (int)row_mb; /* rows in A */ + l = (int)(row_epsilon - row_mb); /* rows in C */ + m = (int)(count_rows - row_epsilon); /* rows in E */ + n = (int)count_unknowns; /* number of variables */ /* debug output_msg(sformatf( "\nColumns\n")); for (j = 0; j < n; j++) { @@ -2836,11 +2824,11 @@ check_solns(struct inverse *inv_ptr) /* * Zero out mass balance rows and fraction rows */ - for (j = row_mb; j < row_charge; j++) + for (size_t j = row_mb; j < row_charge; j++) { memcpy((void *) &(array1[j * max_column_count]), (void *) &(inv_zero[0]), - (size_t) max_column_count * sizeof(LDBLE)); + max_column_count * sizeof(LDBLE)); } /* * Set fraction of solution to 1.0 @@ -2863,21 +2851,21 @@ check_solns(struct inverse *inv_ptr) /* * Zero out isotope mole balance */ - for (j = row_isotopes; j < row_epsilon; j++) + for (size_t j = row_isotopes; j < row_epsilon; j++) { memcpy((void *) &(array1[j * max_column_count]), (void *) &(inv_zero[0]), - (size_t) max_column_count * sizeof(LDBLE)); + max_column_count * sizeof(LDBLE)); } /* * Zero out isotope uncertainties */ - for (j = row_isotope_epsilon; j < count_rows; j++) + for (size_t j = row_isotope_epsilon; j < count_rows; j++) { memcpy((void *) &(array1[j * max_column_count]), (void *) &(inv_zero[0]), - (size_t) max_column_count * sizeof(LDBLE)); + max_column_count * sizeof(LDBLE)); } /* * Can`t Zero out epsilon constraint rows for other solutions because not sure which @@ -2912,7 +2900,7 @@ check_solns(struct inverse *inv_ptr) kode = 1; iter = 200; count_calls++; - cl1(k, l, m, n, nklmd, n2d, &array1[0], &kode, toler, &iter, + cl1(k, l, m, n, (int)nklmd, (int)n2d, &array1[0], &kode, toler, &iter, &delta2[0], &inv_res[0], &error2, &inv_cu[0], &inv_iu[0], &inv_is[0], TRUE); if (kode != 0) @@ -2953,53 +2941,52 @@ post_mortem(void) * array have not been satisfied. * */ - int i, j; LDBLE sum; /* * Check equalities */ output_msg(sformatf( "\nPost_mortem examination of inverse modeling:\n\n")); - for (i = row_mb; i < row_epsilon; i++) + for (size_t i = row_mb; i < row_epsilon; i++) { sum = 0; - for (j = 0; j < count_unknowns; j++) + for (size_t j = 0; j < count_unknowns; j++) { - sum += inv_delta1[j] * my_array[(size_t)i * max_column_count + (size_t)j]; + sum += inv_delta1[j] * my_array[i * max_column_count + j]; } - if (equal(sum, my_array[((size_t)i * max_column_count) + count_unknowns], toler) + if (equal(sum, my_array[(i * max_column_count) + count_unknowns], toler) == FALSE) { output_msg(sformatf( "\tERROR: equality not satisfied for %s, %e.\n", row_name[i], - (double) (sum - my_array[((size_t)i * max_column_count) + count_unknowns]))); + (double) (sum - my_array[(i * max_column_count) + count_unknowns]))); } } /* * Check inequalities */ - for (i = row_epsilon; i < count_rows; i++) + for (size_t i = row_epsilon; i < count_rows; i++) { sum = 0; - for (j = 0; j < count_unknowns; j++) + for (size_t j = 0; j < count_unknowns; j++) { - sum += inv_delta1[j] * my_array[(size_t)i * max_column_count + (size_t)j]; + sum += inv_delta1[j] * my_array[i * max_column_count + j]; } - if (sum > my_array[((size_t)i * max_column_count) + count_unknowns] + toler) + if (sum > my_array[(i * max_column_count) + count_unknowns] + toler) { output_msg(sformatf( "\tERROR: inequality not satisfied for %s, %e\n", row_name[i], - (double) (sum - my_array[((size_t)i * max_column_count) + count_unknowns]))); + (double) (sum - my_array[(i * max_column_count) + count_unknowns]))); } } /* * Check dissolution/precipitation constraints */ - for (i = 0; i < count_unknowns; i++) + for (size_t i = 0; i < count_unknowns; i++) { if (delta_save[i] > 0.5 && inv_delta1[i] < -toler) { @@ -3026,7 +3013,7 @@ test_cl1_solution(void) * checks that equality and inequalities are satisfied * */ - int i, j; + int i; LDBLE sum; /* * Check equalities @@ -3037,20 +3024,20 @@ test_cl1_solution(void) output_msg(sformatf( "\nTesting cl1 inverse modeling:\n\n")); } - for (i = row_mb; i < row_epsilon; i++) + for (size_t i = row_mb; i < row_epsilon; i++) { sum = 0; - for (j = 0; j < count_unknowns; j++) + for (size_t j = 0; j < count_unknowns; j++) { - sum += inv_delta1[j] * my_array[(size_t)i * max_column_count + (size_t)j]; + sum += inv_delta1[j] * my_array[i * max_column_count + j]; } - if (equal(sum, my_array[((size_t)i * max_column_count) + count_unknowns], toler) == FALSE) + if (equal(sum, my_array[(i * max_column_count) + count_unknowns], toler) == FALSE) { if (debug_inverse) { output_msg(sformatf("\tERROR: equality not satisfied for %s, %e.\n", row_name[i], - (double) (sum - my_array[((size_t)i * max_column_count) + count_unknowns]))); + (double) (sum - my_array[(i * max_column_count) + count_unknowns]))); } rv = false; } @@ -3058,22 +3045,22 @@ test_cl1_solution(void) /* * Check inequalities */ - for (i = row_epsilon; i < count_rows; i++) + for (size_t i = row_epsilon; i < count_rows; i++) { sum = 0; - for (j = 0; j < count_unknowns; j++) + for (size_t j = 0; j < count_unknowns; j++) { - sum += inv_delta1[j] * my_array[(size_t)i * max_column_count + (size_t)j]; + sum += inv_delta1[j] * my_array[i * max_column_count + j]; } - if (sum > my_array[((size_t)i * max_column_count) + count_unknowns] + toler) + if (sum > my_array[(i * max_column_count) + count_unknowns] + toler) { if (debug_inverse) { output_msg(sformatf( "\tERROR: inequality not satisfied for %s, %e\n", row_name[i], - (double) (sum - my_array[((size_t)i * max_column_count) + count_unknowns]))); + (double) (sum - my_array[(i * max_column_count) + count_unknowns]))); } rv = false; } @@ -3262,7 +3249,7 @@ isotope_balance_equation(struct inverse *inv_ptr, int row, int n) { int i, j, k; LDBLE isotope_number; - int column; + size_t column; LDBLE f; struct master *primary_ptr; cxxSolution *solution_ptr; @@ -3393,8 +3380,8 @@ isotope_balance_equation(struct inverse *inv_ptr, int row, int n) my_array[(size_t)row * max_column_count + (size_t)column] = isotope_ref[j].ratio * isotope_ref[j].coef; /* term for phase isotope uncertainty unknown */ - column = col_phase_isotopes + i * inv_ptr->isotopes.size() + n; - my_array[(size_t)row * max_column_count + (size_t)column] = isotope_ref[j].coef; + column = col_phase_isotopes + i * inv_ptr->isotopes.size() + (size_t)n; + my_array[(size_t)row * max_column_count + column] = isotope_ref[j].coef; break; } } @@ -3719,19 +3706,19 @@ int Phreeqc:: phase_isotope_inequalities(struct inverse *inv_ptr) /* ---------------------------------------------------------------------- */ { - int i, j, k; - int column; + size_t column; char token[MAX_LENGTH]; if (inv_ptr->isotopes.size() <= 0) return OK; - for (i = 0; i < inv_ptr->phases.size(); i++) + for (size_t i = 0; i < inv_ptr->phases.size(); i++) { if (inv_ptr->phases[i].isotopes.size() == 0) continue; - for (j = 0; j < inv_ptr->phases[i].isotopes.size(); j++) + for (size_t j = 0; j < inv_ptr->phases[i].isotopes.size(); j++) { /* find index number */ + size_t k = 0; for (k = 0; k < inv_ptr->isotopes.size(); k++) { if (inv_ptr->phases[i].isotopes[j].elt_name == @@ -4249,12 +4236,14 @@ dump_netpath_pat(struct inverse *inv_ptr) LDBLE d1, d2, d3; LDBLE sum, sum1, sum_iso, d; std::vector array_save, l_delta_save; - int count_unknowns_save, max_row_count_save, max_column_count_save, temp, - count_current_solutions, temp_punch; + size_t count_unknowns_save, max_row_count_save, + max_column_count_save, count_current_solutions; + int temp, temp_punch; int solnmap[10][2]; FILE *model_file; const struct elt_list *next_elt; - int exch, column; + int exch; + size_t column; LDBLE f; struct rxn_token *rxn_ptr; /* @@ -4804,9 +4793,9 @@ dump_netpath_pat(struct inverse *inv_ptr) /* * Write phase information */ - for (i = 0; i < inv_ptr->phases.size(); i++) + for (size_t i = 0; i < inv_ptr->phases.size(); i++) { - j = col_phases + i; + size_t j = col_phases + i; /* skip if not in model */ /* if (equal (inv_delta1[j], 0.0, toler) == TRUE) continue;*/ diff --git a/prep.cpp b/prep.cpp index 47424e6d..a1477f15 100644 --- a/prep.cpp +++ b/prep.cpp @@ -354,7 +354,7 @@ build_gas_phase(void) * sum of partial pressures equation and * mass balance equations for elements contained in gases */ - int row, col; + size_t row, col; struct master *master_ptr; struct rxn_token *rxn_ptr; struct unknown *unknown_ptr; @@ -630,7 +630,7 @@ build_ss_assemblage(void) * mass balance equations for elements contained in solid solutions */ bool stop; - int row, col; + size_t row, col; struct master *master_ptr; struct rxn_token *rxn_ptr; const char* cptr; @@ -926,7 +926,7 @@ build_jacobian_sums(int k) continue; /* now find the related phase */ - for (kk = count_unknowns - 1; kk >= 0; kk--) + for (kk = (int)count_unknowns - 1; kk >= 0; kk--) { if (x[kk]->type != PP) continue; @@ -984,7 +984,7 @@ build_jacobian_sums(int k) if (comp_ptr->Get_phase_name().size() > 0) { /* now find the related phase */ - for (kk = count_unknowns - 1; kk >= 0; kk--) + for (kk = (int)count_unknowns - 1; kk >= 0; kk--) { if (x[kk]->type != PP) continue; @@ -1079,7 +1079,7 @@ build_model(void) * Guts of prep. Determines species in model, rewrites equations, * builds lists for mass balance and jacobian sums. */ - int i, j, j0, k; + int i, j; LDBLE coef_e; if (s_hplus == NULL || s_eminus == NULL || s_h2o == NULL) @@ -1260,19 +1260,19 @@ build_model(void) */ if (pitzer_model == TRUE || sit_model == TRUE) { - j0 = count_unknowns; - j = count_unknowns + (int)this->s_x.size(); - k = j0; - for (i = j0; i < j; i++) + size_t j0 = count_unknowns; + size_t j = count_unknowns + this->s_x.size(); + size_t k = j0; + for (size_t i = j0; i < j; i++) { - if (s_x[(size_t)i - (size_t)j0]->type == EX) + if (s_x[i - j0]->type == EX) continue; - if (s_x[(size_t)i - (size_t)j0]->type == SURF) + if (s_x[i - j0]->type == SURF) continue; x[k]->number = k; x[k]->type = PITZER_GAMMA; - x[k]->s = s_x[(size_t)i - (size_t)j0]; - x[k]->description = s_x[(size_t)i - (size_t)j0]->name; + x[k]->s = s_x[i - j0]; + x[k]->description = s_x[i - j0]->name; k++; count_unknowns++; } @@ -2591,7 +2591,8 @@ write_mass_action_eqn_x(int stop) */ LDBLE coef_e; int count, repeat; - int i, count_rxn_orig; + int i; + size_t count_rxn_orig; /* * Rewrite any secondary master species flagged REWRITE * Replace pe if necessary @@ -3270,7 +3271,7 @@ setup_surface(void) * Fill in data for surface assemblage in unknown structure */ std::vector master_ptr_list; - int mb_unknown_number; + size_t mb_unknown_number; if (use.Get_surface_ptr() == NULL) return (OK); @@ -4645,7 +4646,7 @@ store_dn(int k, LDBLE * source, int row, LDBLE coef_in, LDBLE * gamma_source) * Stores the terms for d moles of species k in solution into row, multiplied * by coef_in */ - int col; + size_t col; LDBLE coef; struct rxn_token *rxn_ptr; struct master *master_ptr; @@ -4657,7 +4658,7 @@ store_dn(int k, LDBLE * source, int row, LDBLE coef_in, LDBLE * gamma_source) /* Gamma term for d molality of species */ /* Note dg includes molality as a factor */ - row = row * (count_unknowns + 1); + row = row * ((int)count_unknowns + 1); if (s[k]->type != SURF && s[k] != s_h2o) { if (debug_prep == TRUE) @@ -5006,8 +5007,8 @@ write_mb_eqn_x(void) /* ---------------------------------------------------------------------- */ { int count, repeat; - int i, count_rxn_orig; - int j, k; + int i; + size_t count_rxn_orig; struct master *master_ptr; /* * Rewrite any secondary master species flagged REWRITE @@ -5052,12 +5053,12 @@ write_mb_eqn_x(void) */ count_elts = 0; paren_count = 0; - for (i = 1; i < count_trxn; i++) + for (size_t i = 1; i < count_trxn; i++) { - j = count_elts; + size_t j = count_elts; const char* cptr = trxn.token[i].s->name; get_elts_in_species(&cptr, trxn.token[i].coef); - for (k = j; k < count_elts; k++) + for (size_t k = j; k < count_elts; k++) { if (trxn.token[i].s->secondary != NULL) { @@ -5831,7 +5832,7 @@ build_min_exch(void) * jacob0 */ int j, k, jj; - int row; + size_t row; struct master *master_ptr; struct unknown *unknown_ptr; LDBLE coef; @@ -5880,14 +5881,14 @@ build_min_exch(void) continue; } /* find unknown number */ - for (j = count_unknowns - 1; j >= 0; j--) + for (j = (int)count_unknowns - 1; j >= 0; j--) { if (x[j]->type != EXCH) continue; if (x[j]->master[0] == exchange_master) break; } - for (k = count_unknowns - 1; k >= 0; k--) + for (k = (int)count_unknowns - 1; k >= 0; k--) { if (x[k]->type != PP) continue; @@ -5978,7 +5979,7 @@ build_min_exch(void) row = master_ptr->unknown->number; unknown_ptr = master_ptr->unknown; } - store_jacob0(row, (int)x[k]->number, + store_jacob0((int)row, (int)x[k]->number, coef * comp_ref.Get_phase_proportion()); store_sum_deltas(&delta[k], &unknown_ptr->delta, -coef * comp_ref.Get_phase_proportion()); diff --git a/spread.cpp b/spread.cpp index c35448fd..6ba237ce 100644 --- a/spread.cpp +++ b/spread.cpp @@ -121,7 +121,7 @@ read_solution_spread(void) if (j == DIGIT) { char* ptr; - strtod(token.c_str(), &ptr); + (void)strtod(token.c_str(), &ptr); cptr = ptr; int j1 = copy_token(token1, &cptr); if (j1 != EMPTY) diff --git a/tally.cpp b/tally.cpp index e666af3c..6e6f6d00 100644 --- a/tally.cpp +++ b/tally.cpp @@ -221,9 +221,9 @@ store_tally_table(LDBLE * l_array, int row_dim_in, int col_dim, LDBLE fill_facto /* * Add row for total moles of reactant */ - for (i = 0; i < count_tally_table_columns; i++) + for (size_t i = 0; i < count_tally_table_columns; i++) { - l_array[i * row_dim + count_tally_table_rows] = + l_array[i * (size_t)row_dim + count_tally_table_rows] = tally_table[i].moles / fill_factor; } return (OK); @@ -243,8 +243,8 @@ get_tally_table_rows_columns(int *rows, int *columns) CONTINUE); return (ERROR); } - *rows = count_tally_table_rows; - *columns = count_tally_table_columns; + *rows = (int)count_tally_table_rows; + *columns = (int)count_tally_table_columns; return (OK); } @@ -786,7 +786,8 @@ build_tally_table(void) * Also calculates a number greater than all user numbers and * stores in global variable first_user_number. */ - int j, k, l, n, p, save_print_use; + int j, k, l, p, save_print_use; + size_t n; int count_tt_pure_phase, count_tt_ss_phase, count_tt_kinetics; struct phase *phase_ptr; char token[MAX_LENGTH]; diff --git a/tidy.cpp b/tidy.cpp index b521dc36..9222b540 100644 --- a/tidy.cpp +++ b/tidy.cpp @@ -2813,9 +2813,7 @@ species_rxn_to_trxn(struct species *s_ptr) * Copy reaction from reaction structure to * temp reaction structure. */ - int i; - - for (i = 0; s_ptr->rxn.token[i].s != NULL; i++) + for (size_t i = 0; s_ptr->rxn.token[i].s != NULL; i++) { trxn.token[i].name = s_ptr->rxn.token[i].s->name; trxn.token[i].z = s_ptr->rxn.token[i].s->z; diff --git a/utilities.cpp b/utilities.cpp index 4e9d730e..3b0f8ec5 100644 --- a/utilities.cpp +++ b/utilities.cpp @@ -1427,10 +1427,11 @@ string_pad(const char *str, int i) max = l; if (l < i) max = i; - str_ptr = (char *) PHRQ_malloc(((max + 1) * sizeof(char))); + str_ptr = (char *) PHRQ_malloc((((size_t)max + 1) * sizeof(char))); if (str_ptr == NULL) malloc_error(); - strcpy(str_ptr, str); + else + strcpy(str_ptr, str); if (i > l) { for (j = l; j < i; j++) From 051ddba22be6ef9a0abd316f88f3682028754a6e Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Sat, 3 Apr 2021 07:54:10 -0600 Subject: [PATCH 42/53] stag_data --- PBasic.cpp | 4 +-- Phreeqc.cpp | 12 ++++---- Phreeqc.h | 2 +- kinetics.cpp | 4 +-- mainsubs.cpp | 9 +----- readtr.cpp | 36 +++++++++++------------ structures.cpp | 1 - transport.cpp | 80 +++++++++++++++++++++++++------------------------- 8 files changed, 69 insertions(+), 79 deletions(-) diff --git a/PBasic.cpp b/PBasic.cpp index 4a1a5bbd..d854a4cf 100644 --- a/PBasic.cpp +++ b/PBasic.cpp @@ -2671,7 +2671,7 @@ factor(struct LOC_exec * LINK) { if (PhreeqcPtr->phast != TRUE) { - if (i <= 0 || i > PhreeqcPtr->count_cells * (1 + PhreeqcPtr->stag_data->count_stag) + 1 + if (i <= 0 || i > PhreeqcPtr->count_cells * (1 + PhreeqcPtr->stag_data.count_stag) + 1 || i == PhreeqcPtr->count_cells + 1) { /* warning_msg("Note... no porosity for boundary solutions."); */ @@ -4844,7 +4844,7 @@ cmdchange_por(struct LOC_exec *LINK) /* get cell_no */ j = intexpr(LINK); require(tokrp, LINK); - if (j > 0 && j <= PhreeqcPtr->count_cells * (1 + PhreeqcPtr->stag_data->count_stag) + 1 + if (j > 0 && j <= PhreeqcPtr->count_cells * (1 + PhreeqcPtr->stag_data.count_stag) + 1 && j != PhreeqcPtr->count_cells + 1) PhreeqcPtr->cell_data[j].por = TEMP; } diff --git a/Phreeqc.cpp b/Phreeqc.cpp index 942a6093..5d9e3a2a 100644 --- a/Phreeqc.cpp +++ b/Phreeqc.cpp @@ -587,7 +587,6 @@ void Phreeqc::init(void) heat_diffc = -0.1; cell = 0; mcd_substeps = 1.0; - stag_data = NULL; print_modulus = 1; punch_modulus = 1; dump_in = FALSE; @@ -1355,9 +1354,8 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) cell = pSrc->cell; mcd_substeps = pSrc->mcd_substeps; /* stag_data */ - stag_data = (struct stag_data *) free_check_null(stag_data); - stag_data = (struct stag_data *) PHRQ_malloc(sizeof(struct stag_data)); - memcpy(stag_data, pSrc->stag_data, sizeof(struct stag_data)); + stag_data = pSrc->stag_data; + print_modulus = pSrc->print_modulus; punch_modulus = pSrc->punch_modulus; dump_in = pSrc->dump_in; @@ -1367,9 +1365,9 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) old_cells = pSrc->old_cells; max_cells = pSrc->max_cells; - if (stag_data->count_stag > 0) + if (stag_data.count_stag > 0) { - max_cells = (max_cells - 2) / (1 + stag_data->count_stag); + max_cells = (max_cells - 2) / (1 + stag_data.count_stag); } all_cells = pSrc->all_cells; @@ -1380,7 +1378,7 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) //cell_data = (struct cell_data *) PHRQ_malloc((size_t) ((count_cells + 2) * sizeof(struct cell_data))); //if (cell_data == NULL) malloc_error(); //memcpy(cell_data, pSrc->cell_data, ((size_t) ((count_cells + 2) * sizeof(struct cell_data - int all_cells_now = max_cells * (1 + stag_data->count_stag) + 2; + int all_cells_now = max_cells * (1 + stag_data.count_stag) + 2; space((void **)((void *)&cell_data), all_cells_now, &cell_data_max_cells, sizeof(struct cell_data)); memcpy(cell_data, pSrc->cell_data, ((size_t)(all_cells_now * sizeof(struct cell_data)))); } diff --git a/Phreeqc.h b/Phreeqc.h index 964f221d..d578c93b 100644 --- a/Phreeqc.h +++ b/Phreeqc.h @@ -1306,7 +1306,7 @@ protected: LDBLE heat_diffc; int cell; LDBLE mcd_substeps; - struct stag_data* stag_data; + struct stag_data stag_data; int print_modulus; int punch_modulus; int dump_in; diff --git a/kinetics.cpp b/kinetics.cpp index 15a667d2..5f604fcb 100644 --- a/kinetics.cpp +++ b/kinetics.cpp @@ -319,7 +319,7 @@ rk_kinetics(int i, LDBLE kin_time, int use_mix, int nsaver, /* * Save kinetics i and solution i, if necessary */ - save_old = -2 - (count_cells * (1 + stag_data->count_stag) + 2); + save_old = -2 - (count_cells * (1 + stag_data.count_stag) + 2); Utilities::Rxn_copy(Rxn_kinetics_map, i, save_old); if (nsaver != i) { @@ -2064,7 +2064,7 @@ run_reactions(int i, LDBLE kin_time, int use_mix, LDBLE step_fraction) } else { - save_old = -2 - (count_cells * (1 + stag_data->count_stag) + 2); + save_old = -2 - (count_cells * (1 + stag_data.count_stag) + 2); if (nsaver != i) { Utilities::Rxn_copy(Rxn_solution_map, i, save_old); diff --git a/mainsubs.cpp b/mainsubs.cpp index 93aeb4e6..9ca5523b 100644 --- a/mainsubs.cpp +++ b/mainsubs.cpp @@ -49,14 +49,7 @@ initialize(void) space((void **) ((void *) &line_save), INIT, &max_line, sizeof(char)); - // one stag_data - stag_data = (struct stag_data *) PHRQ_calloc(1, sizeof(struct stag_data)); - if (stag_data == NULL) - malloc_error(); - stag_data->count_stag = 0; - stag_data->exch_f = 0; - stag_data->th_m = 0; - stag_data->th_im = 0; + // one stag_data in phreeqc.h, initialized in global_structures // user_print user_print = new struct rate; diff --git a/readtr.cpp b/readtr.cpp index de932f5f..0ec217a9 100644 --- a/readtr.cpp +++ b/readtr.cpp @@ -376,7 +376,7 @@ read_transport(void) if (copy_token(token, &next_char, &l) != EMPTY) { /* exchange factor */ - if (sscanf(token, "%d", &(stag_data->count_stag)) != 1) + if (sscanf(token, "%d", &(stag_data.count_stag)) != 1) { input_error++; error_string = sformatf( @@ -389,7 +389,7 @@ read_transport(void) j = copy_token(token, &next_char, &l); if (j != EMPTY) { - if (sscanf(token, SCANFORMAT, &(stag_data->exch_f)) != 1) + if (sscanf(token, SCANFORMAT, &(stag_data.exch_f)) != 1) { input_error++; error_string = sformatf( @@ -398,7 +398,7 @@ read_transport(void) break; } copy_token(token, &next_char, &l); - if (sscanf(token, SCANFORMAT, &(stag_data->th_m)) != 1) + if (sscanf(token, SCANFORMAT, &(stag_data.th_m)) != 1) { input_error++; error_string = sformatf( @@ -407,7 +407,7 @@ read_transport(void) break; } copy_token(token, &next_char, &l); - if (sscanf(token, SCANFORMAT, &(stag_data->th_im)) != 1) + if (sscanf(token, SCANFORMAT, &(stag_data.th_im)) != 1) { input_error++; error_string = sformatf( @@ -744,8 +744,8 @@ read_transport(void) max_cells = count_length; if (count_disp > max_cells) max_cells = count_disp; - if (count_por > max_cells * (1 + stag_data->count_stag)) - max_cells = (int)ceil(((double)count_por / (1 + (double)stag_data->count_stag))); + if (count_por > max_cells * (1 + stag_data.count_stag)) + max_cells = (int)ceil(((double)count_por / (1 + (double)stag_data.count_stag))); if (max_cells > count_cells) { if (max_cells == count_length) @@ -766,14 +766,14 @@ read_transport(void) { sprintf(token, "Number of mobile cells is increased to (ceil)(number of porosities) / (1 + number of stagnant zones) = %d.", - (int) ceil(((double)count_por / (1 + (double)stag_data->count_stag)))); + (int) ceil(((double)count_por / (1 + (double)stag_data.count_stag)))); warning_msg(token); } } /* * Allocate space for cell_data */ - int all_cells_now = max_cells * (1 + stag_data->count_stag) + 2; + int all_cells_now = max_cells * (1 + stag_data.count_stag) + 2; space((void **)((void *)&cell_data), all_cells_now, &cell_data_max_cells, sizeof(struct cell_data)); @@ -888,15 +888,15 @@ read_transport(void) } else { - if ((stag_data->exch_f > 0) && (stag_data->count_stag == 1)) + if ((stag_data.exch_f > 0) && (stag_data.count_stag == 1)) { error_string = sformatf( "Mobile porosities were read, but mobile/immobile porosity was also defined in -stagnant. Using the values from -stagnant for mobile/immobile exchange and tortuosity factors."); warning_msg(error_string); for (i = 1; i <= max_cells; i++) - cell_data[i].por = stag_data->th_m; + cell_data[i].por = stag_data.th_m; for (i++; i <= 2 * max_cells + 1; i++) - cell_data[i].por = stag_data->th_im; + cell_data[i].por = stag_data.th_im; } else { @@ -908,7 +908,7 @@ read_transport(void) } if (all_cells - 2 > count_por) { - int st = stag_data->count_stag ? 1 : 0; + int st = stag_data.count_stag ? 1 : 0; error_string = sformatf( "Porosities were read for %d cells. Last value is used till cell %d.", count_por, all_cells - st); @@ -943,12 +943,12 @@ read_transport(void) /* * Account for stagnant cells */ - if (stag_data->count_stag > 0) + if (stag_data.count_stag > 0) { - max_cells = count_cells * (1 + stag_data->count_stag) + 2; + max_cells = count_cells * (1 + stag_data.count_stag) + 2; for (i = 1; i <= count_cells; i++) { - for (l = 1; l <= stag_data->count_stag; l++) + for (l = 1; l <= stag_data.count_stag; l++) cell_data[i + 1 + l * count_cells].mid_cell_x = cell_data[i].mid_cell_x; } @@ -1093,9 +1093,9 @@ read_transport(void) */ if (heat_diffc < 0) heat_diffc = diffc; - else if (stag_data->count_stag == 1) + else if (stag_data.count_stag == 1) { - if (stag_data->exch_f > 0) + if (stag_data.exch_f > 0) { if (diffc <= 0 && heat_diffc > 0) { @@ -1123,7 +1123,7 @@ read_transport(void) } } } - else if (stag_data->count_stag > 1 && heat_diffc > diffc) + else if (stag_data.count_stag > 1 && heat_diffc > diffc) { input_error++; error_string = sformatf( diff --git a/structures.cpp b/structures.cpp index 43cc5363..782b9edd 100644 --- a/structures.cpp +++ b/structures.cpp @@ -137,7 +137,6 @@ clean_up(void) /*species_list*/ species_list.clear(); /* transport data */ - stag_data = (struct stag_data*)free_check_null(stag_data); cell_data = (struct cell_data*)free_check_null(cell_data); /* advection */ advection_punch.clear(); diff --git a/transport.cpp b/transport.cpp index bc7e9368..3c8a00e4 100644 --- a/transport.cpp +++ b/transport.cpp @@ -191,7 +191,7 @@ transport(void) /* * Initialize temperature in stagnant cells ... */ - for (n = 1; n <= stag_data->count_stag; n++) + for (n = 1; n <= stag_data.count_stag; n++) { for (i = 1; i <= count_cells; i++) { @@ -280,7 +280,7 @@ transport(void) warning_msg(error_string); timest = 1; } - //if (implicit && stag_data->count_stag > 1) + //if (implicit && stag_data.count_stag > 1) //{ // error_string = sformatf( // "Sorry, implicit diffusion can handle only 1 stagnant layer for now. Please remove -implicit true, or set -stagnant 1."); @@ -333,7 +333,7 @@ transport(void) /* * Also stagnant cells */ - for (n = 1; n <= stag_data->count_stag; n++) + for (n = 1; n <= stag_data.count_stag; n++) { for (i = 1; i <= count_cells; i++) { @@ -358,7 +358,7 @@ transport(void) */ if (multi_Dflag) diffc_tr = diffc_max; - if ((stag_data->exch_f > 0) && (stag_data->count_stag == 1)) + if ((stag_data.exch_f > 0) && (stag_data.count_stag == 1)) { /* multi_D calc's are OK if all cells have the same amount of water */ if (multi_Dflag == TRUE) @@ -421,12 +421,12 @@ transport(void) * structure stag_data. * MIX 'cell_no' in input file can be an alternative for the calculation here. */ - if ((stag_data->exch_f > 0) && (stag_data->count_stag == 1)) + if ((stag_data.exch_f > 0) && (stag_data.count_stag == 1)) { - b = stag_data->th_m / (stag_data->th_m + stag_data->th_im); - f = exp(-stag_data->exch_f * stagkin_time / (b * stag_data->th_im)); + b = stag_data.th_m / (stag_data.th_m + stag_data.th_im); + f = exp(-stag_data.exch_f * stagkin_time / (b * stag_data.th_im)); mix_f_imm = b - b * f; - mix_f_m = mix_f_imm * stag_data->th_im / stag_data->th_m; + mix_f_m = mix_f_imm * stag_data.th_im / stag_data.th_m; for (j = 1; j <= count_cells; j++) { j_imm = j + (1 + count_cells); @@ -470,11 +470,11 @@ transport(void) /* * Assumption: D_e used for calculating exch_f in input file equals diffc */ - f = stag_data->exch_f * (heat_diffc - diffc) / diffc / tempr; - f = exp(-f * stagkin_time / (b * stag_data->th_im)); + f = stag_data.exch_f * (heat_diffc - diffc) / diffc / tempr; + f = exp(-f * stagkin_time / (b * stag_data.th_im)); heat_mix_f_imm = b - b * f; heat_mix_f_m = - heat_mix_f_imm * stag_data->th_im / stag_data->th_m; + heat_mix_f_imm * stag_data.th_im / stag_data.th_m; } } /* @@ -510,7 +510,7 @@ transport(void) /* * Also stagnant cells */ - for (n = 1; n <= stag_data->count_stag; n++) + for (n = 1; n <= stag_data.count_stag; n++) { for (i = 1; i <= count_cells; i++) { @@ -568,7 +568,7 @@ transport(void) STOP); } if (implicit) - diffuse_implicit(stagkin_time, stag_data->count_stag); + diffuse_implicit(stagkin_time, stag_data.count_stag); else if (multi_Dflag) multi_D(stagkin_time, 1, FALSE); @@ -576,7 +576,7 @@ transport(void) { if (!dV_dcell && (i == 0 || i == count_cells + 1) && !implicit) { - if (j == nmix && stag_data->count_stag == 0 && + if (j == nmix && stag_data.count_stag == 0 && (cell_data[0].print || cell_data[0].punch || cell_data[count_cells + 1].print || cell_data[count_cells + 1].punch)) print_punch(i, false); @@ -607,7 +607,7 @@ transport(void) fill_spec(i, 0); /* punch and output file */ - if (ishift == 0 && j == nmix && (stag_data->count_stag == 0 || (implicit && stag_data->count_stag == 1))) + if (ishift == 0 && j == nmix && (stag_data.count_stag == 0 || (implicit && stag_data.count_stag == 1))) print_punch(i, true); if (i > 1) Utilities::Rxn_copy(Rxn_solution_map, -2, i - 1); @@ -622,7 +622,7 @@ transport(void) timest + ((double)j - 1) * stagkin_time; rate_sim_time = rate_sim_time_start + stagkin_time; - if (stag_data->count_stag > 0) + if (stag_data.count_stag > 0) { if (ishift == 0 && j == nmix) punch_boolean = TRUE; @@ -633,9 +633,9 @@ transport(void) mix_stag(i, stagkin_time, punch_boolean, step_fraction); } } - if (ishift == 0 && j == nmix && stag_data->count_stag > 0) + if (ishift == 0 && j == nmix && stag_data.count_stag > 0) { - for (n = 1; n <= stag_data->count_stag; n++) + for (n = 1; n <= stag_data.count_stag; n++) { for (i = 1; i <= count_cells; i++) { @@ -759,21 +759,21 @@ transport(void) fill_spec(i, i - 1); if (overall_iterations > max_iter) max_iter = overall_iterations; - if (nmix == 0 && stag_data->count_stag == 0) + if (nmix == 0 && stag_data.count_stag == 0) print_punch(i, true); if (i == first_c && count_cells > 1) kin_time = kin_time_save; saver(); /* If nmix is zero, stagnant zone mixing after advective step ... */ - if ((nmix == 0) && (stag_data->count_stag > 0)) + if ((nmix == 0) && (stag_data.count_stag > 0)) { mix_stag(i, stagkin_time, TRUE, step_fraction); } } - if (nmix == 0 && stag_data->count_stag > 0) + if (nmix == 0 && stag_data.count_stag > 0) { - for (n = 1; n <= stag_data->count_stag; n++) + for (n = 1; n <= stag_data.count_stag; n++) { for (i = 1; i <= count_cells; i++) { @@ -831,7 +831,7 @@ transport(void) error_msg("Error in surface transport, stopping.", STOP); } if (implicit) - diffuse_implicit(stagkin_time, stag_data->count_stag); + diffuse_implicit(stagkin_time, stag_data.count_stag); else if (multi_Dflag) multi_D(stagkin_time, 1, FALSE); @@ -840,7 +840,7 @@ transport(void) { if (!dV_dcell && (i == 0 || i == count_cells + 1) && !implicit) { - if (j == nmix && stag_data->count_stag == 0 && + if (j == nmix && stag_data.count_stag == 0 && (cell_data[0].print || cell_data[0].punch || cell_data[count_cells + 1].print || cell_data[count_cells + 1].punch)) print_punch(i, false); @@ -867,7 +867,7 @@ transport(void) } if (multi_Dflag == TRUE) fill_spec(i, 0); - if (j == nmix && (stag_data->count_stag == 0 || (implicit && stag_data->count_stag == 1))) + if (j == nmix && (stag_data.count_stag == 0 || (implicit && stag_data.count_stag == 1))) print_punch(i, true); if (i > 1) Utilities::Rxn_copy(Rxn_solution_map, -2, i - 1); @@ -880,7 +880,7 @@ transport(void) timest + ((double)j - 1) * stagkin_time; rate_sim_time = rate_sim_time_start + stagkin_time; - if (stag_data->count_stag > 0) + if (stag_data.count_stag > 0) { if (j == nmix) punch_boolean = TRUE; @@ -891,9 +891,9 @@ transport(void) mix_stag(i, stagkin_time, punch_boolean, step_fraction); } } - if (j == nmix && ((stag_data->count_stag > 0/* && !implicit) || (implicit && stag_data->count_stag == 1*/))) + if (j == nmix && ((stag_data.count_stag > 0/* && !implicit) || (implicit && stag_data.count_stag == 1*/))) { - for (n = 1; n <= stag_data->count_stag; n++) + for (n = 1; n <= stag_data.count_stag; n++) { for (i = 1; i <= count_cells; i++) { @@ -979,7 +979,7 @@ transport_cleanup(void) * free mix structures */ Dispersion_mix_map.clear(); - if ((stag_data->exch_f > 0) && (stag_data->count_stag == 1)) + if ((stag_data.exch_f > 0) && (stag_data.count_stag == 1)) { Rxn_mix_map.clear(); } @@ -1014,7 +1014,7 @@ transport_cleanup(void) } if (implicit) { - int l_stag = (stag_data->count_stag < 2 ? stag_data->count_stag : 0); + int l_stag = (stag_data.count_stag < 2 ? stag_data.count_stag : 0); Ct2 = (LDBLE *)free_check_null(Ct2); l_tk_x2 = (LDBLE *)free_check_null(l_tk_x2); if (A) @@ -1202,7 +1202,7 @@ init_mix(void) if (maxmix == 0) { l_nmix = 0; - if (mcd_substeps > 1 && stag_data->count_stag > 0) + if (mcd_substeps > 1 && stag_data.count_stag > 0) l_nmix = (int) ceil(mcd_substeps); } else if (implicit) @@ -1393,7 +1393,7 @@ mix_stag(int i, LDBLE kin_time, int l_punch, LDBLE step_fraction) /* * Kinetics in transport cell is done while transporting */ - for (n = 1; n <= stag_data->count_stag; n++) + for (n = 1; n <= stag_data.count_stag; n++) { if (i == 0 || i == count_cells + 1) { @@ -1424,7 +1424,7 @@ mix_stag(int i, LDBLE kin_time, int l_punch, LDBLE step_fraction) { if (n == 1) { - if (heat_nmix > 0 && (!implicit || (implicit && stag_data->count_stag > 1))) + if (heat_nmix > 0 && (!implicit || (implicit && stag_data.count_stag > 1))) { ptr_m = Utilities::Rxn_find(Rxn_solution_map, i); t_imm = @@ -1454,7 +1454,7 @@ mix_stag(int i, LDBLE kin_time, int l_punch, LDBLE step_fraction) error_msg("Error in surface transport, stopping.", STOP); } - if (!implicit || (implicit && stag_data->count_stag > 1)) + if (!implicit || (implicit && stag_data.count_stag > 1)) { if (multi_Dflag == TRUE) multi_D(1.0, i, 2); @@ -1484,7 +1484,7 @@ mix_stag(int i, LDBLE kin_time, int l_punch, LDBLE step_fraction) if (done_mixing) // after all mixing is done, the temporal solution becomes the original for the next timestep { - for (n = 1; n <= stag_data->count_stag; n++) + for (n = 1; n <= stag_data.count_stag; n++) { k = i + 1 + n * count_cells; if (Utilities::Rxn_find(Rxn_solution_map, k) != 0) @@ -1533,7 +1533,7 @@ init_heat_mix(int l_nmix) { if (fabs(Utilities::Rxn_find(Rxn_solution_map, count_cells + 1)->Get_tc() - t0) > 1.0) l_heat_nmix = 1; - for (n = 1; n <= stag_data->count_stag; n++) + for (n = 1; n <= stag_data.count_stag; n++) { for (i = 1; i < count_cells; i++) { @@ -3272,7 +3272,7 @@ multi_D(LDBLE DDt, int mobile_cell, int stagnant) for (int f_c = 0; f_c <= loop_f_c; f_c++) { - for (n = 0; n <= (stagnant ? stag_data->count_stag : 0); n++) // allow for stagnant cell mixing with higher cells in the layer + for (n = 0; n <= (stagnant ? stag_data.count_stag : 0); n++) // allow for stagnant cell mixing with higher cells in the layer { icell = mobile_cell + 1 + n * count_cells; if (stagnant) @@ -5115,7 +5115,7 @@ Step (from cell 1 to count_cells + 1): { continue; } - if (i >= 0 && i <= 1 + count_cells * (1 + stag_data->count_stag)) + if (i >= 0 && i <= 1 + count_cells * (1 + stag_data.count_stag)) { surface_ptr1 = Utilities::Rxn_find(Rxn_surface_map, i); if (surface_ptr1 != NULL) @@ -5443,7 +5443,7 @@ diff_stag_surf(int mobile_cell) cxxSurface *surface_n2_ptr; std::map Rxn_temp_surface_map; - for (ns = 0; ns < stag_data->count_stag; ns++) + for (ns = 0; ns < stag_data.count_stag; ns++) { i1 = mobile_cell + 1 + ns * count_cells; @@ -5707,7 +5707,7 @@ diff_stag_surf(int mobile_cell) { continue; } - if (i >= 0 && i <= 1 + count_cells * (1 + stag_data->count_stag)) + if (i >= 0 && i <= 1 + count_cells * (1 + stag_data.count_stag)) { surface_ptr1 = Utilities::Rxn_find(Rxn_surface_map, i); //if (surface_ptr1 != NULL) From 25e062100a9c03f963bc6eb27c72db9f2e8648fc Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Sat, 3 Apr 2021 08:40:20 -0600 Subject: [PATCH 43/53] cell_data --- Phreeqc.cpp | 15 +-------------- Phreeqc.h | 3 +-- advection.cpp | 8 ++++---- mainsubs.cpp | 17 ++--------------- readtr.cpp | 6 ++---- structures.cpp | 2 +- 6 files changed, 11 insertions(+), 40 deletions(-) diff --git a/Phreeqc.cpp b/Phreeqc.cpp index 5d9e3a2a..e46a465d 100644 --- a/Phreeqc.cpp +++ b/Phreeqc.cpp @@ -575,7 +575,6 @@ void Phreeqc::init(void) * Transport data *---------------------------------------------------------------------- */ count_cells = 1; - cell_data_max_cells = 1; // count_cells; count_shifts = 1; ishift = 1; bcon_first = bcon_last = 3; @@ -592,7 +591,6 @@ void Phreeqc::init(void) dump_in = FALSE; dump_modulus = 0; transport_warnings = TRUE; - cell_data = NULL; old_cells = 0; max_cells = 0; all_cells = 0; @@ -1340,7 +1338,6 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) * Transport data *---------------------------------------------------------------------- */ count_cells = pSrc->count_cells; - cell_data_max_cells = 1; //pSrc->cell_data_max_cells; count_shifts = pSrc->count_shifts; ishift = pSrc->ishift; bcon_first = pSrc->bcon_first; @@ -1371,17 +1368,7 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) } all_cells = pSrc->all_cells; - cell_data_max_cells = 1; - if (count_cells > 0) - { - //cell_data = (struct cell_data *) free_check_null(cell_data); - //cell_data = (struct cell_data *) PHRQ_malloc((size_t) ((count_cells + 2) * sizeof(struct cell_data))); - //if (cell_data == NULL) malloc_error(); - //memcpy(cell_data, pSrc->cell_data, ((size_t) ((count_cells + 2) * sizeof(struct cell_data - int all_cells_now = max_cells * (1 + stag_data.count_stag) + 2; - space((void **)((void *)&cell_data), all_cells_now, &cell_data_max_cells, sizeof(struct cell_data)); - memcpy(cell_data, pSrc->cell_data, ((size_t)(all_cells_now * sizeof(struct cell_data)))); - } + cell_data = pSrc->cell_data; max_cells = pSrc->max_cells; multi_Dflag = pSrc->multi_Dflag; interlayer_Dflag = pSrc->interlayer_Dflag; diff --git a/Phreeqc.h b/Phreeqc.h index d578c93b..25092222 100644 --- a/Phreeqc.h +++ b/Phreeqc.h @@ -1293,7 +1293,6 @@ protected: * Transport data *---------------------------------------------------------------------- */ int count_cells; - int cell_data_max_cells; int count_shifts; int ishift; int bcon_first; @@ -1312,7 +1311,7 @@ protected: int dump_in; int dump_modulus; int transport_warnings; - struct cell_data* cell_data; + std::vector cell_data; int old_cells, max_cells, all_cells; int multi_Dflag; /* signals calc'n of multicomponent diffusion */ int interlayer_Dflag; /* multicomponent diffusion and diffusion through interlayer porosity */ diff --git a/advection.cpp b/advection.cpp index 352d25b5..464dd394 100644 --- a/advection.cpp +++ b/advection.cpp @@ -89,7 +89,7 @@ advection(void) /* * Equilibrate and (or) mix */ - for (size_t i = 1; i <= count_ad_cells; i++) + for (int i = 1; i <= count_ad_cells; i++) { set_initial_moles(i); cell_no = i; @@ -102,17 +102,17 @@ advection(void) log_msg(sformatf( "\nCell %d.\n\n", i)); if (pr.use == TRUE && pr.all == TRUE && advection_step % print_ad_modulus == 0 && - advection_print[i - 1] == TRUE) + advection_print[(size_t)i - 1] == TRUE) { output_msg(sformatf( "\nCell %d.\n\n", i)); } if (advection_step % punch_ad_modulus == 0 && - advection_punch[i - 1] == TRUE) + advection_punch[(size_t)i - 1] == TRUE) { punch_all(); } if (advection_step % print_ad_modulus == 0 && - advection_print[i - 1] == TRUE) + advection_print[(size_t)i - 1] == TRUE) { print_all(); } diff --git a/mainsubs.cpp b/mainsubs.cpp index 9ca5523b..38cd8c2f 100644 --- a/mainsubs.cpp +++ b/mainsubs.cpp @@ -29,21 +29,8 @@ initialize(void) /* * Allocate space */ - cell_data_max_cells = count_cells + 2; - space((void **) ((void *) &cell_data), INIT, &cell_data_max_cells, - sizeof(struct cell_data)); - for (int i = 0; i < cell_data_max_cells; i++) - { - cell_data[i].length = 1.0; - cell_data[i].mid_cell_x = 1.0; - cell_data[i].disp = 1.0; - cell_data[i].temp = 25.0; - cell_data[i].por = 0.1; - cell_data[i].por_il = 0.01; - cell_data[i].potV = 0; - cell_data[i].punch = FALSE; - cell_data[i].print = FALSE; - } + cell_data.resize((size_t)count_cells + 2); // initialized by global_structures.h + count_inverse = 0; space((void **) ((void *) &line), INIT, &max_line, sizeof(char)); diff --git a/readtr.cpp b/readtr.cpp index 0ec217a9..82c5b6c7 100644 --- a/readtr.cpp +++ b/readtr.cpp @@ -774,10 +774,8 @@ read_transport(void) * Allocate space for cell_data */ int all_cells_now = max_cells * (1 + stag_data.count_stag) + 2; - space((void **)((void *)&cell_data), all_cells_now, &cell_data_max_cells, - sizeof(struct cell_data)); - - // initialize new cells + cell_data.resize(all_cells_now); // initialized by global_structures.h + // But first two previously allocated if (all_cells_now > all_cells) { for (int i = all_cells; i < all_cells_now; i++) diff --git a/structures.cpp b/structures.cpp index 782b9edd..6355850b 100644 --- a/structures.cpp +++ b/structures.cpp @@ -137,7 +137,7 @@ clean_up(void) /*species_list*/ species_list.clear(); /* transport data */ - cell_data = (struct cell_data*)free_check_null(cell_data); + cell_data.clear(); /* advection */ advection_punch.clear(); advection_print.clear(); From 492df61dfcf804a7aca7bfd2e0e851e100503cae Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Sat, 3 Apr 2021 12:04:24 -0600 Subject: [PATCH 44/53] descriptions --- NumKeyword.cxx | 3 +- isotopes.cpp | 24 ----- read.cpp | 234 +++++++++++++------------------------------------ readtr.cpp | 8 -- 4 files changed, 64 insertions(+), 205 deletions(-) diff --git a/NumKeyword.cxx b/NumKeyword.cxx index 0dae9df6..3605af81 100644 --- a/NumKeyword.cxx +++ b/NumKeyword.cxx @@ -135,6 +135,7 @@ cxxNumKeyword::read_number_description(const std::string & line_in) std::string line = line_in; std::string::iterator b = line.begin(); std::string::iterator e = line.end(); + this->description.clear(); // skip keyword CParser::copy_token(keyword, b, e); @@ -168,11 +169,11 @@ cxxNumKeyword::read_number_description(const std::string & line_in) else { this->n_user = this->n_user_end = 1; + this->description = token; } // skip whitespace std::string::iterator ic; - this->description.clear(); for (ic = b; ic != e; ic++) { this->description += *ic; diff --git a/isotopes.cpp b/isotopes.cpp index 597ed2b0..6395779a 100644 --- a/isotopes.cpp +++ b/isotopes.cpp @@ -165,20 +165,12 @@ read_calculate_values(void) int return_value, opt, opt_save; char token[MAX_LENGTH]; struct calculate_value *calculate_value_ptr; - char *description; - int n_user, n_user_end; const char* next_char; const char *opt_list[] = { "start", /* 0 */ "end" /* 1 */ }; int count_opt_list = 2; -/* - * Read advection number (not currently used) - */ - cptr = line; - read_number_description(cptr, &n_user, &n_user_end, &description); - description = (char *) free_check_null(description); opt_save = OPTION_DEFAULT; /* * Read lines @@ -281,19 +273,11 @@ read_isotope_ratios(void) int return_value, opt, opt_save; char token[MAX_LENGTH]; struct isotope_ratio *isotope_ratio_ptr; - char *description; - int n_user, n_user_end; const char* next_char; const char *opt_list[] = { "no_options" /* 0 */ }; int count_opt_list = 0; -/* - * Read number (not currently used) - */ - cptr = line; - read_number_description(cptr, &n_user, &n_user_end, &description); - description = (char *) free_check_null(description); opt_save = OPTION_DEFAULT; /* * Read lines @@ -380,19 +364,11 @@ read_isotope_alphas(void) int return_value, opt, opt_save; char token[MAX_LENGTH]; struct isotope_alpha *isotope_alpha_ptr; - char *description; - int n_user, n_user_end; const char* next_char; const char *opt_list[] = { "no_options" /* 0 */ }; int count_opt_list = 0; -/* - * Read number (not currently used) - */ - cptr = line; - read_number_description(cptr, &n_user, &n_user_end, &description); - description = (char *) free_check_null(description); opt_save = OPTION_DEFAULT; /* * Read lines diff --git a/read.cpp b/read.cpp index 89cc689f..fbf6216c 100644 --- a/read.cpp +++ b/read.cpp @@ -810,11 +810,9 @@ read_exchange(void) * ERROR if error occurred reading data * */ - int n_user, n_user_end; + int n_user; LDBLE conc; const char* cptr; - char *description; - int return_value, opt; const char* next_char; const char *opt_list[] = { @@ -832,22 +830,15 @@ read_exchange(void) * Z Manganite ('equi' or 'kine') 0.25 * ^Name ^equi or kinetic mineral ^switch ^prop.factor */ -/* - * Read exchange number and description - */ - - cptr = line; - read_number_description(cptr, &n_user, &n_user_end, &description); /* * Default values + n_user, description */ cxxExchange temp_exchange; + cptr = line; + temp_exchange.read_number_description(cptr); + n_user = temp_exchange.Get_n_user(); cxxExchComp *comp_ptr = NULL; temp_exchange.Set_new_def(true); - temp_exchange.Set_n_user(n_user); - temp_exchange.Set_n_user_end(n_user_end); - temp_exchange.Set_description(description); - free_check_null(description); /* * Set use data */ @@ -1174,9 +1165,8 @@ read_gas_phase(void) * */ int i, j, l; - int n_user, n_user_end; + int n_user; const char* cptr; - char *description; char token[MAX_LENGTH]; cxxGasPhase temp_gas_phase(this->phrq_io); int return_value, opt; @@ -1193,17 +1183,11 @@ read_gas_phase(void) "equil" /* 8 */ }; int count_opt_list = 9; -/* - * Read gas_phase number - */ - cptr = line; - read_number_description(cptr, &n_user, &n_user_end, &description); - temp_gas_phase.Set_n_user(n_user); - temp_gas_phase.Set_n_user_end(n_user_end); - temp_gas_phase.Set_description(description); + cptr = line; + temp_gas_phase.read_number_description(cptr); + n_user = temp_gas_phase.Get_n_user(); temp_gas_phase.Set_new_def(true); - free_check_null(description); /* * Set use data to first read */ @@ -1880,9 +1864,8 @@ read_kinetics(void) * Read kinetics */ const char* cptr; - char *description; std::string token; - int n_user, n_user_end; + int n_user; LDBLE step; int return_value, opt; @@ -1907,16 +1890,10 @@ read_kinetics(void) }; int count_opt_list = 16; -/* - * Read kinetics number - */ - cptr = line; - read_number_description(cptr, &n_user, &n_user_end, &description); cxxKinetics temp_kinetics(this->phrq_io); - temp_kinetics.Set_n_user(n_user); - temp_kinetics.Set_n_user_end(n_user_end); - temp_kinetics.Set_description(description); - description = (char *) free_check_null(description); + cptr = line; + temp_kinetics.read_number_description(cptr); + n_user = temp_kinetics.Get_n_user(); cxxKineticsComp *kinetics_comp_ptr = NULL; std::string stdunits; /* @@ -3205,27 +3182,18 @@ read_mix(void) /* * Reads mixing fractions */ - int n_user, n_user_end; + int n_user; int return_value; int n_solution; LDBLE fraction; int j, i, l; const char* cptr; char token[MAX_LENGTH]; - char *description; cxxMix temp_mix; -/* - * Read mix number - */ cptr = line; - read_number_description(cptr, &n_user, &n_user_end, &description); - - temp_mix.Set_n_user(n_user); - temp_mix.Set_n_user_end(n_user); - temp_mix.Set_description(description); - free_check_null(description); - + temp_mix.read_number_description(cptr); + n_user = temp_mix.Get_n_user(); /* * Set use data to first read */ @@ -3289,10 +3257,10 @@ read_mix(void) Rxn_mix_map[n_user] = temp_mix; // copy if needed - if (n_user_end > n_user) + if (temp_mix.Get_n_user_end() > n_user) { int i; - for (i = n_user + 1; i <= n_user_end; i++) + for (i = n_user + 1; i <= temp_mix.Get_n_user_end(); i++) { Utilities::Rxn_copy(Rxn_mix_map, n_user, i); } @@ -3741,9 +3709,8 @@ read_pp_assemblage(void) */ int j; int return_value; - int n_user, n_user_end; + int n_user; const char* cptr; - char *description; std::string token; int opt, opt_save; const char* next_char; @@ -3751,23 +3718,16 @@ read_pp_assemblage(void) "force_equality" /* 0 */ }; int count_opt_list = 1; - - cptr = line; - /* - * Read pp_assemblage number - */ - read_number_description(cptr, &n_user, &n_user_end, &description); /* * Find pp_assemblage or realloc space for pp_assemblage */ cxxPPassemblage temp_pp_assemblage; + cptr = line; + temp_pp_assemblage.read_number_description(cptr); + n_user = temp_pp_assemblage.Get_n_user(); cxxPPassemblageComp *comp = NULL; std::map comps; temp_pp_assemblage.Set_new_def(true); - temp_pp_assemblage.Set_n_user(n_user); - temp_pp_assemblage.Set_n_user_end(n_user_end); - temp_pp_assemblage.Set_description(description); - free_check_null(description); /* * Set use data to first read */ @@ -3937,17 +3897,16 @@ read_reaction(void) */ int l; const char* cptr; - char *description; char token[MAX_LENGTH]; int return_value; - int n_user, n_user_end; - + int n_user; /* - * Read reaction number + * Defaults */ + cxxReaction temp_reaction; cptr = line; - read_number_description(cptr, &n_user, &n_user_end, &description); - + temp_reaction.read_number_description(cptr); + n_user = temp_reaction.Get_n_user(); /* * Set use data to first read */ @@ -3956,14 +3915,6 @@ read_reaction(void) use.Set_reaction_in(true); use.Set_n_reaction_user(n_user); } -/* - * Defaults - */ - cxxReaction temp_reaction; - temp_reaction.Set_n_user(n_user); - temp_reaction.Set_n_user_end(n_user_end); - temp_reaction.Set_description(description); - free_check_null(description); /* * Read reaction data */ @@ -4013,7 +3964,7 @@ read_reaction(void) } Rxn_reaction_map[n_user] = temp_reaction; // copy if needed - Utilities::Rxn_copies(Rxn_reaction_map, n_user, n_user_end); + Utilities::Rxn_copies(Rxn_reaction_map, n_user, temp_reaction.Get_n_user_end()); return (return_value); } @@ -4364,21 +4315,15 @@ read_selected_output(void) int i, l; char token[MAX_LENGTH]; std::string file_name; - const char* cptr; - cptr = line; - int n_user, n_user_end; - char *description; - read_number_description(cptr, &n_user, &n_user_end, &description); + int n_user; SelectedOutput temp_selected_output; + cptr = line; + temp_selected_output.read_number_description(cptr); + n_user = temp_selected_output.Get_n_user(); temp_selected_output.Set_new_def(false); temp_selected_output.Set_file_name(n_user); - temp_selected_output.Set_n_user(n_user); - temp_selected_output.Set_n_user_end(n_user_end); - temp_selected_output.Set_description(description); - free_check_null(description); - // find if it exists std::map< int, SelectedOutput >::iterator so = SelectedOutput_map.find(n_user); if (n_user == 1 && so != SelectedOutput_map.end()) @@ -4812,9 +4757,7 @@ read_solution(void) * ERROR if error occurred reading data * */ - int n_user, n_user_end; - char *description; - + int n_user; int return_value, opt; const char* next_char; const char *opt_list[] = { @@ -4834,24 +4777,16 @@ read_solution(void) "potential" /* 13 */ }; int count_opt_list = 14; -/* - * Read solution number and description - */ - const char* cptr; - cptr = line; - read_number_description(cptr, &n_user, &n_user_end, &description); cxxSolution temp_solution; + const char* cptr = line; + temp_solution.read_number_description(cptr); + n_user = temp_solution.Get_n_user(); temp_solution.Set_new_def(true); temp_solution.Create_initial_data(); cxxISolution *isoln_ptr = temp_solution.Get_initial_data(); CParser parser(this->phrq_io); - temp_solution.Set_n_user(n_user); - temp_solution.Set_n_user_end(n_user_end); - temp_solution.Set_description(description); - free_check_null(description); - if (!use.Get_solution_in()) { use.Set_solution_in(true); @@ -6260,12 +6195,10 @@ read_surface(void) * ERROR if error occurred reading data * */ - int n_user, n_user_end; + int n_user; LDBLE conc; const char* cptr, *cptr1; - char *description; std::string token, token1, name; - int return_value, opt; const char* next_char; const char *opt_list[] = { @@ -6294,19 +6227,13 @@ read_surface(void) * Surf_wOH Manganite [equilibrium_phases or kinetics] 0.25 4000 * ^Name mineral ^switch ^prop.factor ^m2/mol */ - /* - * Read surface number and description - */ - cptr = line; - read_number_description(cptr, &n_user, &n_user_end, &description); cxxSurface temp_surface; + cptr = line; + temp_surface.read_number_description(cptr); + n_user = temp_surface.Get_n_user(); cxxSurfaceComp *comp_ptr = NULL; cxxSurfaceCharge *charge_ptr = NULL; temp_surface.Set_new_def(true); - temp_surface.Set_n_user(n_user); - temp_surface.Set_n_user_end(n_user_end); - temp_surface.Set_description(description); - free_check_null(description); if (use.Get_surface_in() == FALSE) { @@ -7126,10 +7053,6 @@ read_advection(void) * number of cells; * number of shifts; */ - const char* cptr; - char *description; - int n_user, n_user_end, i; - std::vector punch_temp, print_temp; int return_value, opt, opt_save; const char* next_char; @@ -7154,12 +7077,6 @@ read_advection(void) "warnings" /* 17 */ }; int count_opt_list = 18; -/* - * Read advection number (not currently used) - */ - cptr = line; - read_number_description(cptr, &n_user, &n_user_end, &description); - description = (char *) free_check_null(description); /* * Set use data */ @@ -7298,7 +7215,7 @@ read_advection(void) advection_punch.resize(count_ad_cells + 1); if (punch_temp.size() != 0) { - for (i = 0; i < count_ad_cells; i++) + for (size_t i = 0; i < count_ad_cells; i++) advection_punch[i] = FALSE; for (size_t i = 0; i < punch_temp.size(); i++) { @@ -7317,7 +7234,7 @@ read_advection(void) } else { - for (i = 0; i < count_ad_cells; i++) + for (size_t i = 0; i < count_ad_cells; i++) advection_punch[i] = TRUE; } punch_temp.clear(); @@ -7327,9 +7244,9 @@ read_advection(void) advection_print.resize(count_ad_cells + 1); if (print_temp.size() != 0) { - for (i = 0; i < count_ad_cells; i++) + for (size_t i = 0; i < count_ad_cells; i++) advection_print[i] = FALSE; - for (i = 0; i < print_temp.size(); i++) + for (size_t i = 0; i < print_temp.size(); i++) { if (print_temp[i] > count_ad_cells || print_temp[i] < 1) { @@ -7346,7 +7263,7 @@ read_advection(void) } else { - for (i = 0; i < count_ad_cells; i++) + for (size_t i = 0; i < count_ad_cells; i++) advection_print[i] = TRUE; } print_temp.clear(); @@ -8141,21 +8058,14 @@ read_rates(void) int return_value, opt, opt_save; char token[MAX_LENGTH]; struct rate *rate_ptr; - char *description; - int n_user, n_user_end; const char* next_char; const char *opt_list[] = { "start", /* 0 */ "end" /* 1 */ }; int count_opt_list = 2; -/* - * Read advection number (not currently used) - */ + n = -1; - cptr = line; - read_number_description(cptr, &n_user, &n_user_end, &description); - description = (char *) free_check_null(description); opt_save = OPTION_DEFAULT; /* * Read lines @@ -8348,18 +8258,13 @@ read_user_punch(void) * Read lines */ - int n_user, n_user_end; - char *description; - const char* cptr; - cptr = line; - read_number_description(cptr, &n_user, &n_user_end, &description); + int n_user; UserPunch temp_user_punch; + const char* cptr = line; + temp_user_punch.read_number_description(cptr); + n_user = temp_user_punch.Get_n_user(); temp_user_punch.Set_PhreeqcPtr(this); - temp_user_punch.Set_n_user(n_user); - temp_user_punch.Set_n_user_end(n_user_end); - temp_user_punch.Set_description(description); - free_check_null(description); //std::map < int, UserPunch >::iterator up = UserPunch_map.find(n_user); //if (up != UserPunch_map.end()) @@ -8448,9 +8353,7 @@ read_solid_solutions(void) * ERROR if error occurred reading data * */ - int n_user, n_user_end; - const char* cptr; - char *description; + int n_user; std::string token; int return_value, opt; @@ -8479,13 +8382,11 @@ read_solid_solutions(void) /* * Read ss_assemblage number */ - cptr = line; - read_number_description(cptr, &n_user, &n_user_end, &description); cxxSSassemblage temp_ss_assemblage; - temp_ss_assemblage.Set_n_user(n_user); - temp_ss_assemblage.Set_n_user_end(n_user_end); - temp_ss_assemblage.Set_description(description); - free_check_null(description); + + const char* cptr = line; + temp_ss_assemblage.read_number_description(cptr); + n_user = temp_ss_assemblage.Get_n_user(); temp_ss_assemblage.Set_new_def(true); std::vector comps; @@ -9627,14 +9528,9 @@ read_reaction_pressure(void) // Make instance, set n_user, n_user_end, description cxxPressure atm(this->phrq_io); const char* cptr = line; - char *description; - int n_user, n_user_end; - read_number_description(cptr, &n_user, &n_user_end, &description); - atm.Set_n_user(n_user); - atm.Set_n_user_end(n_user); - atm.Set_description(description); - free_check_null(description); + atm.read_number_description(cptr); + int n_user = atm.Get_n_user(); /* * Make parser */ @@ -9653,10 +9549,10 @@ read_reaction_pressure(void) } // Make copies if necessary - if (n_user_end > n_user) + if (atm.Get_n_user_end() > n_user) { int i; - for (i = n_user + 1; i <= n_user_end; i++) + for (i = n_user + 1; i <= atm.Get_n_user_end(); i++) { Utilities::Rxn_copy(Rxn_pressure_map, n_user, i); } @@ -9761,14 +9657,8 @@ read_temperature(void) // Make instance, set n_user, n_user_end, description cxxTemperature t_react(this->phrq_io); const char* cptr = line; - char *description; - int n_user, n_user_end; - read_number_description(cptr, &n_user, &n_user_end, &description); - t_react.Set_n_user(n_user); - t_react.Set_n_user_end(n_user); - t_react.Set_description(description); - free_check_null(description); - + t_react.read_number_description(cptr); + int n_user = t_react.Get_n_user(); /* * Make parser */ @@ -9787,10 +9677,10 @@ read_temperature(void) } // Make copies if necessary - if (n_user_end > n_user) + if (t_react.Get_n_user_end() > n_user) { int i; - for (i = n_user + 1; i <= n_user_end; i++) + for (i = n_user + 1; i <= t_react.Get_n_user_end(); i++) { Utilities::Rxn_copy(Rxn_temperature_map, n_user, i); } diff --git a/readtr.cpp b/readtr.cpp index 82c5b6c7..5c0ad916 100644 --- a/readtr.cpp +++ b/readtr.cpp @@ -40,8 +40,6 @@ read_transport(void) int count_length, count_disp, count_punch, count_print, count_por, count_same_model; int count_length_alloc, count_disp_alloc, count_por_alloc; char token[MAX_LENGTH]; - char *description; - int n_user, n_user_end; LDBLE *length, *disp, *pors; int *punch_temp, *print_temp, *same_model_temp; int return_value, opt, opt_save; @@ -143,12 +141,6 @@ read_transport(void) count_length_alloc = count_disp_alloc = count_por_alloc = 1; transport_start = 1; /* - * Read transport number (not currently used) - */ - cptr = line; - read_number_description(cptr, &n_user, &n_user_end, &description); - description = (char *)free_check_null(description); - /* * Set use data to last read */ use.Set_trans_in(true); From 88782320187353218f00e6bbf20abe50d9759629 Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Sat, 3 Apr 2021 17:15:19 -0600 Subject: [PATCH 45/53] delete rate, unused cptr --- UserPunch.cpp | 2 +- isotopes.cpp | 3 --- readtr.cpp | 1 - 3 files changed, 1 insertion(+), 5 deletions(-) diff --git a/UserPunch.cpp b/UserPunch.cpp index 290a427f..a00ce280 100644 --- a/UserPunch.cpp +++ b/UserPunch.cpp @@ -15,7 +15,7 @@ UserPunch::~UserPunch(void) if (this->PhreeqcPtr != NULL) { this->PhreeqcPtr->rate_free(this->rate); - this->PhreeqcPtr->free_check_null(this->rate); + delete this->rate; } } this->PhreeqcPtr = NULL; diff --git a/isotopes.cpp b/isotopes.cpp index 6395779a..93aa551e 100644 --- a/isotopes.cpp +++ b/isotopes.cpp @@ -160,7 +160,6 @@ read_calculate_values(void) * ERROR if error occurred reading data * */ - const char* cptr; int l; int return_value, opt, opt_save; char token[MAX_LENGTH]; @@ -268,7 +267,6 @@ read_isotope_ratios(void) * ERROR if error occurred reading data * */ - const char* cptr; int l; int return_value, opt, opt_save; char token[MAX_LENGTH]; @@ -359,7 +357,6 @@ read_isotope_alphas(void) * ERROR if error occurred reading data * */ - const char* cptr; int l; int return_value, opt, opt_save; char token[MAX_LENGTH]; diff --git a/readtr.cpp b/readtr.cpp index 5c0ad916..87f82fed 100644 --- a/readtr.cpp +++ b/readtr.cpp @@ -35,7 +35,6 @@ read_transport(void) * ERROR if error occurred reading data * */ - const char* cptr; int i, j, l; int count_length, count_disp, count_punch, count_print, count_por, count_same_model; int count_length_alloc, count_disp_alloc, count_por_alloc; From ac3335ed16cf663a65957f2d3d16733b026d0f2d Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Sat, 3 Apr 2021 18:36:45 -0600 Subject: [PATCH 46/53] theta_params --- Phreeqc.cpp | 8 +------ Phreeqc.h | 4 ++-- global_structures.h | 2 +- pitzer.cpp | 54 ++++++++++++++++++++----------------------- pitzer_structures.cpp | 15 ++++++------ 5 files changed, 36 insertions(+), 47 deletions(-) diff --git a/Phreeqc.cpp b/Phreeqc.cpp index e46a465d..a101929f 100644 --- a/Phreeqc.cpp +++ b/Phreeqc.cpp @@ -1919,13 +1919,7 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) } pitz_param_map = pSrc->pitz_param_map; // auto pitz_param_map - for (int i = 0; i < (int)pSrc->theta_params.size(); i++) - { - size_t count_theta_params = theta_params.size(); - theta_params.resize(count_theta_params + 1); - theta_params[count_theta_params] = theta_param_alloc(); - memcpy(theta_params[count_theta_params], pSrc->theta_params[i], sizeof(struct theta_param)); - } + theta_params = pSrc->theta_params; use_etheta = pSrc->use_etheta; /* OTEMP = -100.0; diff --git a/Phreeqc.h b/Phreeqc.h index 25092222..e9de69d9 100644 --- a/Phreeqc.h +++ b/Phreeqc.h @@ -501,7 +501,7 @@ public: struct pitz_param* pitz_param_read(char* string, int n); void pitz_param_store(struct pitz_param* pzp_ptr, bool force_copy); void sit_param_store(struct pitz_param* pzp_ptr, bool force_copy); - struct theta_param* theta_param_search(LDBLE zj, LDBLE zk); + int theta_param_search(LDBLE zj, LDBLE zk); struct theta_param* theta_param_alloc(void); int theta_param_init(struct theta_param* theta_param_ptr); void pitzer_make_lists(void); @@ -1720,7 +1720,7 @@ protected: LDBLE VP, DW0; std::vector pitz_params; std::map< std::string, size_t > pitz_param_map; - std::vector theta_params; + std::vector theta_params; int use_etheta; LDBLE OTEMP, OPRESS; LDBLE A0; diff --git a/global_structures.h b/global_structures.h index c064b94a..fe59c9dd 100644 --- a/global_structures.h +++ b/global_structures.h @@ -979,7 +979,7 @@ struct pitz_param LDBLE alpha = 0; LDBLE os_coef = 0; LDBLE ln_coef[3] = { 0,0,0 }; - struct theta_param *thetas = NULL; + int theta_params_index = -1; }; struct theta_param diff --git a/pitzer.cpp b/pitzer.cpp index 3e650d24..b62489b4 100644 --- a/pitzer.cpp +++ b/pitzer.cpp @@ -47,7 +47,6 @@ pitzer_tidy(void) int count_pos, count_neg, count_neut, count[3], jj; LDBLE z0, z1; struct pitz_param *pzp_ptr; - struct theta_param *theta_param_ptr; /* * Ensure new parameters are calculated */ @@ -295,11 +294,6 @@ pitzer_tidy(void) /* * Add thetas pointer to etheta pitzer parameters */ - - for (i = 0; i < (int)theta_params.size(); i++) - { - theta_params[i] = (struct theta_param *) free_check_null(theta_params[i]); - } theta_params.clear(); for (i = 0; i < (int)pitz_params.size(); i++) { @@ -307,18 +301,16 @@ pitzer_tidy(void) { z0 = spec[pitz_params[i]->ispec[0]]->z; z1 = spec[pitz_params[i]->ispec[1]]->z; - theta_param_ptr = theta_param_search(z0, z1); - if (theta_param_ptr == NULL) + int index = theta_param_search(z0, z1); + pitz_params[i]->theta_params_index = index; + if (index < 0) { size_t count_theta_param = theta_params.size(); theta_params.resize(count_theta_param + 1); - theta_params[count_theta_param] = theta_param_alloc(); - theta_param_init(theta_params[count_theta_param]); - theta_params[count_theta_param]->zj = z0; - theta_params[count_theta_param]->zk = z1; - theta_param_ptr = theta_params[count_theta_param]; + theta_params[count_theta_param].zj = z0; + theta_params[count_theta_param].zk = z1; + pitz_params[i]->theta_params_index = (int)count_theta_param; } - pitz_params[i]->thetas = theta_param_ptr; } } /* @@ -1291,11 +1283,11 @@ pitzer(void) { for (i = 0; i < (int)theta_params.size(); i++) { - z0 = theta_params[i]->zj; - z1 = theta_params[i]->zk; + z0 = theta_params[i].zj; + z1 = theta_params[i].zk; ETHETAS(z0, z1, I, ðeta, ðetap); - theta_params[i]->etheta = etheta; - theta_params[i]->ethetap = ethetap; + theta_params[i].etheta = etheta; + theta_params[i].ethetap = ethetap; } } /* @@ -1356,12 +1348,16 @@ pitzer(void) */ if (use_etheta == TRUE) { - etheta = pitz_params[i]->thetas->etheta; - ethetap = pitz_params[i]->thetas->ethetap; - F_var = M[i0] * M[i1] * ethetap; - LGAMMA[i0] += 2.0 * M[i1] * etheta; - LGAMMA[i1] += 2.0 * M[i0] * etheta; - OSMOT += M[i0] * M[i1] * (etheta + I * ethetap); + int index = pitz_params[i]->theta_params_index; + if (index >= 0) + { + etheta = theta_params[index].etheta; + ethetap = theta_params[index].ethetap; + F_var = M[i0] * M[i1] * ethetap; + LGAMMA[i0] += 2.0 * M[i1] * etheta; + LGAMMA[i1] += 2.0 * M[i0] * etheta; + OSMOT += M[i0] * M[i1] * (etheta + I * ethetap); + } } break; case TYPE_PSI: @@ -1635,11 +1631,11 @@ pitzer_clean_up(void) } pitz_param_map.clear(); pitz_params.clear(); - for (i = 0; i < (int)theta_params.size(); i++) - { - theta_params[i] = - (struct theta_param *) free_check_null(theta_params[i]); - } + //for (i = 0; i < (int)theta_params.size(); i++) + //{ + // theta_params[i] = + // (struct theta_param *) free_check_null(theta_params[i]); + //} theta_params.clear(); LGAMMA.clear(); IPRSNT.clear(); diff --git a/pitzer_structures.cpp b/pitzer_structures.cpp index 1d9623c1..fbff5376 100644 --- a/pitzer_structures.cpp +++ b/pitzer_structures.cpp @@ -48,7 +48,6 @@ pitz_param_init(struct pitz_param *pitz_param_ptr) pitz_param_ptr->a[i] = 0.0; } pitz_param_ptr->alpha = 0.0; - pitz_param_ptr->thetas = NULL; pitz_param_ptr->os_coef = 0.; for (i = 0; i < 3; i++) { @@ -210,7 +209,7 @@ pitz_param_store(struct pitz_param *pzp_ptr, bool force_copy) } } // thetas - pitz_params[count_pitz_param]->thetas = NULL; + pitz_params[count_pitz_param]->theta_params_index = -1; pitz_param_map[key] = count_pitz_param; } else @@ -287,7 +286,7 @@ sit_param_store(struct pitz_param *pzp_ptr, bool force_copy) } } // thetas - sit_params[count_sit_param]->thetas = NULL; + sit_params[count_sit_param]->theta_params_index = -1; sit_param_map[key] = count_sit_param; } else @@ -337,7 +336,7 @@ theta_param_init(struct theta_param *theta_param_ptr) } /* ---------------------------------------------------------------------- */ -struct theta_param * Phreeqc:: +int Phreeqc:: theta_param_search(LDBLE zj, LDBLE zk) /* ---------------------------------------------------------------------- */ { @@ -348,11 +347,11 @@ theta_param_search(LDBLE zj, LDBLE zk) int i; for (i = 0; i < (int)theta_params.size(); i++) { - if ((theta_params[i]->zj == zj && theta_params[i]->zk == zk) || - (theta_params[i]->zj == zk && theta_params[i]->zk == zj)) + if ((theta_params[i].zj == zj && theta_params[i].zk == zk) || + (theta_params[i].zj == zk && theta_params[i].zk == zj)) { - return theta_params[i]; + return i; } } - return NULL; + return -1; } From dcb9efe0697c621d96ae1ee414c601f625d970aa Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Sat, 3 Apr 2021 19:28:35 -0600 Subject: [PATCH 47/53] sit_params --- Phreeqc.h | 4 ++-- global_structures.h | 2 +- pitzer_structures.cpp | 32 ++++++++++++++++---------------- sit.cpp | 42 +++++++++++++++++++++--------------------- 4 files changed, 40 insertions(+), 40 deletions(-) diff --git a/Phreeqc.h b/Phreeqc.h index e9de69d9..aed15e7f 100644 --- a/Phreeqc.h +++ b/Phreeqc.h @@ -500,7 +500,7 @@ public: // pitzer.cpp ------------------------------- struct pitz_param* pitz_param_read(char* string, int n); void pitz_param_store(struct pitz_param* pzp_ptr, bool force_copy); - void sit_param_store(struct pitz_param* pzp_ptr, bool force_copy); + void sit_param_store(struct pitz_param& pzp_ptr, bool force_copy); int theta_param_search(LDBLE zj, LDBLE zk); struct theta_param* theta_param_alloc(void); int theta_param_init(struct theta_param* theta_param_ptr); @@ -1755,7 +1755,7 @@ protected: std::string dump_file_name_cpp; /* sit.cpp ------------------------------- */ - std::vector sit_params; + std::vector sit_params; std::map< std::string, size_t > sit_param_map; LDBLE sit_A0; int sit_count_cations, sit_count_anions, sit_count_neutrals; diff --git a/global_structures.h b/global_structures.h index fe59c9dd..073cafb9 100644 --- a/global_structures.h +++ b/global_structures.h @@ -949,7 +949,7 @@ struct M_S typedef enum { TYPE_B0, TYPE_B1, TYPE_B2, TYPE_C0, TYPE_THETA, TYPE_LAMDA, TYPE_ZETA, TYPE_PSI, TYPE_ETHETA, TYPE_ALPHAS, TYPE_MU, TYPE_ETA, TYPE_Other, - TYPE_SIT_EPSILON, TYPE_SIT_EPSILON_MU, TYPE_APHI + TYPE_SIT_EPSILON, TYPE_SIT_EPSILON_MU, TYPE_APHI, TYPE_FAIL } pitz_param_type; struct pitz_param diff --git a/pitzer_structures.cpp b/pitzer_structures.cpp index fbff5376..b9ca8cf8 100644 --- a/pitzer_structures.cpp +++ b/pitzer_structures.cpp @@ -224,7 +224,7 @@ pitz_param_store(struct pitz_param *pzp_ptr, bool force_copy) /* ---------------------------------------------------------------------- */ void Phreeqc:: -sit_param_store(struct pitz_param *pzp_ptr, bool force_copy) +sit_param_store(struct pitz_param& pzp, bool force_copy) /* ---------------------------------------------------------------------- */ { /* @@ -232,19 +232,19 @@ sit_param_store(struct pitz_param *pzp_ptr, bool force_copy) * Returns -1 if not found, index number in pitz_params if found */ int i; - if (pzp_ptr == NULL) - return; - if (pzp_ptr->type == TYPE_Other) + //if (pzp_ptr == NULL) + // return; + if (pzp.type == TYPE_Other || pzp.type == TYPE_Other) return; std::set< std::string > header; for (i = 0; i < 3; i++) { - if (pzp_ptr->species[i] != NULL) header.insert(pzp_ptr->species[i]); + if (pzp.species[i] != NULL) header.insert(pzp.species[i]); } std::ostringstream key_str; - key_str << pzp_ptr->type << " "; + key_str << pzp.type << " "; std::set< std::string >::iterator it = header.begin(); for(; it != header.end(); ++it) { @@ -255,19 +255,19 @@ sit_param_store(struct pitz_param *pzp_ptr, bool force_copy) std::map< std::string, size_t>::iterator jit = sit_param_map.find(key); if (jit != sit_param_map.end()) { - if (pzp_ptr->species[2] != NULL) + if (pzp.species[2] != NULL) { error_string = sformatf( "Redefinition of parameter, %s %s %s\n", - pzp_ptr->species[0], pzp_ptr->species[1], pzp_ptr->species[2]); + pzp.species[0], pzp.species[1], pzp.species[2]); } else { error_string = sformatf( "Redefinition of parameter, %s %s\n", - pzp_ptr->species[0], pzp_ptr->species[1]); + pzp.species[0], pzp.species[1]); } warning_msg(error_string); - sit_params[(*jit).second] = (struct pitz_param *) free_check_null(sit_params[(*jit).second]); - sit_params[(*jit).second] = pzp_ptr; + //sit_params[(*jit).second] = (struct pitz_param *) free_check_null(sit_params[(*jit).second]); + sit_params[(*jit).second] = pzp; } else { @@ -275,25 +275,25 @@ sit_param_store(struct pitz_param *pzp_ptr, bool force_copy) { size_t count_sit_param = sit_params.size(); sit_params.resize(count_sit_param + 1); - sit_params[count_sit_param] = pitz_param_duplicate(pzp_ptr); + sit_params[count_sit_param] = pzp; // clean up pointers // species for (i = 0; i < 3; i++) { - if (pzp_ptr->species[i] != NULL) + if (pzp.species[i] != NULL) { - sit_params[count_sit_param]->species[i] = string_hsave(pzp_ptr->species[i]); + sit_params[count_sit_param].species[i] = string_hsave(pzp.species[i]); } } // thetas - sit_params[count_sit_param]->theta_params_index = -1; + sit_params[count_sit_param].theta_params_index = -1; sit_param_map[key] = count_sit_param; } else { size_t count_sit_param = sit_params.size(); sit_params.resize(count_sit_param + 1); - sit_params[count_sit_param] = pzp_ptr; + sit_params[count_sit_param] = pzp; sit_param_map[key] = count_sit_param; } } diff --git a/sit.cpp b/sit.cpp index b9214296..84bd9ac3 100644 --- a/sit.cpp +++ b/sit.cpp @@ -86,19 +86,19 @@ sit_tidy(void) { for (j = 0; j < 3; j++) { - if (sit_params[i]->species[j] == NULL) + if (sit_params[i].species[j] == NULL) continue; - sit_params[i]->ispec[j] = sit_ISPEC(sit_params[i]->species[j]); - if ((j < 2 && sit_params[i]->ispec[j] == -1) || + sit_params[i].ispec[j] = sit_ISPEC(sit_params[i].species[j]); + if ((j < 2 && sit_params[i].ispec[j] == -1) || (j == 3 - && (sit_params[i]->type == TYPE_PSI - || sit_params[i]->type == TYPE_ZETA) - && sit_params[i]->ispec[j] == -1)) + && (sit_params[i].type == TYPE_PSI + || sit_params[i].type == TYPE_ZETA) + && sit_params[i].ispec[j] == -1)) { input_error++; error_string = sformatf( "Species for Pitzer parameter not defined in SOLUTION_SPECIES, %s", - sit_params[i]->species[j]); + sit_params[i].species[j]); error_msg(error_string, CONTINUE); } } @@ -110,10 +110,10 @@ sit_tidy(void) std::set< std::string > header; for (int i = 0; i < 3; i++) { - if (sit_params[j]->species[i] != NULL) header.insert(sit_params[j]->species[i]); + if (sit_params[j].species[i] != NULL) header.insert(sit_params[j].species[i]); } std::ostringstream key_str; - key_str << sit_params[j]->type << " "; + key_str << sit_params[j].type << " "; std::set< std::string >::iterator it = header.begin(); for(; it != header.end(); ++it) { @@ -211,7 +211,7 @@ read_sit(void) if (pzp_ptr != NULL) { pzp_ptr->type = pzp_type; - sit_param_store(pzp_ptr, false); + sit_param_store(*pzp_ptr, false); } break; case OPTION_ERROR: @@ -394,13 +394,13 @@ sit(void) for (size_t j = 0; j < param_list.size(); j++) { int i = param_list[j]; - i0 = sit_params[i]->ispec[0]; - i1 = sit_params[i]->ispec[1]; + i0 = sit_params[i].ispec[0]; + i1 = sit_params[i].ispec[1]; //if (sit_IPRSNT[i0] == FALSE || sit_IPRSNT[i1] == FALSE) continue; z0 = spec[i0]->z; z1 = spec[i1]->z; - param = sit_params[i]->p; - switch (sit_params[i]->type) + param = sit_params[i].p; + switch (sit_params[i].type) { case TYPE_SIT_EPSILON: sit_LGAMMA[i0] += sit_M[i1] * param; @@ -494,10 +494,10 @@ sit_clean_up(void) */ int i; - for (i = 0; i < (int)sit_params.size(); i++) - { - sit_params[i] = (struct pitz_param *) free_check_null(sit_params[i]); - } + //for (i = 0; i < (int)sit_params.size(); i++) + //{ + // sit_params[i] = (struct pitz_param *) free_check_null(sit_params[i]); + //} sit_params.clear(); sit_param_map.clear(); sit_LGAMMA.clear(); @@ -1389,7 +1389,7 @@ C Set DW0 for (size_t j = 0; j < param_list.size(); j++) { int i = param_list[j]; - calc_sit_param(sit_params[i], TK, TR); + calc_sit_param(&sit_params[i], TK, TR); } calc_dielectrics(TK - 273.15, patm_x); sit_A0 = A0; @@ -1465,8 +1465,8 @@ sit_make_lists(void) } for (int i = 0; i < (int)sit_params.size(); i++) { - int i0 = sit_params[i]->ispec[0]; - int i1 = sit_params[i]->ispec[1]; + int i0 = sit_params[i].ispec[0]; + int i1 = sit_params[i].ispec[1]; if (sit_IPRSNT[i0] == FALSE || sit_IPRSNT[i1] == FALSE) continue; param_list.push_back(i); } From 87d67929c0a3e8129481f2761cca64ad1a1efdf9 Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Sat, 3 Apr 2021 22:26:30 -0600 Subject: [PATCH 48/53] reverting changes to sit_params and theta_params. Will consider using new and delet --- Phreeqc.cpp | 8 ++++++- Phreeqc.h | 8 +++---- global_structures.h | 4 ++-- pitzer.cpp | 54 +++++++++++++++++++++++-------------------- pitzer_structures.cpp | 45 ++++++++++++++++++------------------ sit.cpp | 42 ++++++++++++++++----------------- 6 files changed, 86 insertions(+), 75 deletions(-) diff --git a/Phreeqc.cpp b/Phreeqc.cpp index a101929f..e46a465d 100644 --- a/Phreeqc.cpp +++ b/Phreeqc.cpp @@ -1919,7 +1919,13 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) } pitz_param_map = pSrc->pitz_param_map; // auto pitz_param_map - theta_params = pSrc->theta_params; + for (int i = 0; i < (int)pSrc->theta_params.size(); i++) + { + size_t count_theta_params = theta_params.size(); + theta_params.resize(count_theta_params + 1); + theta_params[count_theta_params] = theta_param_alloc(); + memcpy(theta_params[count_theta_params], pSrc->theta_params[i], sizeof(struct theta_param)); + } use_etheta = pSrc->use_etheta; /* OTEMP = -100.0; diff --git a/Phreeqc.h b/Phreeqc.h index aed15e7f..25092222 100644 --- a/Phreeqc.h +++ b/Phreeqc.h @@ -500,8 +500,8 @@ public: // pitzer.cpp ------------------------------- struct pitz_param* pitz_param_read(char* string, int n); void pitz_param_store(struct pitz_param* pzp_ptr, bool force_copy); - void sit_param_store(struct pitz_param& pzp_ptr, bool force_copy); - int theta_param_search(LDBLE zj, LDBLE zk); + void sit_param_store(struct pitz_param* pzp_ptr, bool force_copy); + struct theta_param* theta_param_search(LDBLE zj, LDBLE zk); struct theta_param* theta_param_alloc(void); int theta_param_init(struct theta_param* theta_param_ptr); void pitzer_make_lists(void); @@ -1720,7 +1720,7 @@ protected: LDBLE VP, DW0; std::vector pitz_params; std::map< std::string, size_t > pitz_param_map; - std::vector theta_params; + std::vector theta_params; int use_etheta; LDBLE OTEMP, OPRESS; LDBLE A0; @@ -1755,7 +1755,7 @@ protected: std::string dump_file_name_cpp; /* sit.cpp ------------------------------- */ - std::vector sit_params; + std::vector sit_params; std::map< std::string, size_t > sit_param_map; LDBLE sit_A0; int sit_count_cations, sit_count_anions, sit_count_neutrals; diff --git a/global_structures.h b/global_structures.h index 073cafb9..c064b94a 100644 --- a/global_structures.h +++ b/global_structures.h @@ -949,7 +949,7 @@ struct M_S typedef enum { TYPE_B0, TYPE_B1, TYPE_B2, TYPE_C0, TYPE_THETA, TYPE_LAMDA, TYPE_ZETA, TYPE_PSI, TYPE_ETHETA, TYPE_ALPHAS, TYPE_MU, TYPE_ETA, TYPE_Other, - TYPE_SIT_EPSILON, TYPE_SIT_EPSILON_MU, TYPE_APHI, TYPE_FAIL + TYPE_SIT_EPSILON, TYPE_SIT_EPSILON_MU, TYPE_APHI } pitz_param_type; struct pitz_param @@ -979,7 +979,7 @@ struct pitz_param LDBLE alpha = 0; LDBLE os_coef = 0; LDBLE ln_coef[3] = { 0,0,0 }; - int theta_params_index = -1; + struct theta_param *thetas = NULL; }; struct theta_param diff --git a/pitzer.cpp b/pitzer.cpp index b62489b4..3e650d24 100644 --- a/pitzer.cpp +++ b/pitzer.cpp @@ -47,6 +47,7 @@ pitzer_tidy(void) int count_pos, count_neg, count_neut, count[3], jj; LDBLE z0, z1; struct pitz_param *pzp_ptr; + struct theta_param *theta_param_ptr; /* * Ensure new parameters are calculated */ @@ -294,6 +295,11 @@ pitzer_tidy(void) /* * Add thetas pointer to etheta pitzer parameters */ + + for (i = 0; i < (int)theta_params.size(); i++) + { + theta_params[i] = (struct theta_param *) free_check_null(theta_params[i]); + } theta_params.clear(); for (i = 0; i < (int)pitz_params.size(); i++) { @@ -301,16 +307,18 @@ pitzer_tidy(void) { z0 = spec[pitz_params[i]->ispec[0]]->z; z1 = spec[pitz_params[i]->ispec[1]]->z; - int index = theta_param_search(z0, z1); - pitz_params[i]->theta_params_index = index; - if (index < 0) + theta_param_ptr = theta_param_search(z0, z1); + if (theta_param_ptr == NULL) { size_t count_theta_param = theta_params.size(); theta_params.resize(count_theta_param + 1); - theta_params[count_theta_param].zj = z0; - theta_params[count_theta_param].zk = z1; - pitz_params[i]->theta_params_index = (int)count_theta_param; + theta_params[count_theta_param] = theta_param_alloc(); + theta_param_init(theta_params[count_theta_param]); + theta_params[count_theta_param]->zj = z0; + theta_params[count_theta_param]->zk = z1; + theta_param_ptr = theta_params[count_theta_param]; } + pitz_params[i]->thetas = theta_param_ptr; } } /* @@ -1283,11 +1291,11 @@ pitzer(void) { for (i = 0; i < (int)theta_params.size(); i++) { - z0 = theta_params[i].zj; - z1 = theta_params[i].zk; + z0 = theta_params[i]->zj; + z1 = theta_params[i]->zk; ETHETAS(z0, z1, I, ðeta, ðetap); - theta_params[i].etheta = etheta; - theta_params[i].ethetap = ethetap; + theta_params[i]->etheta = etheta; + theta_params[i]->ethetap = ethetap; } } /* @@ -1348,16 +1356,12 @@ pitzer(void) */ if (use_etheta == TRUE) { - int index = pitz_params[i]->theta_params_index; - if (index >= 0) - { - etheta = theta_params[index].etheta; - ethetap = theta_params[index].ethetap; - F_var = M[i0] * M[i1] * ethetap; - LGAMMA[i0] += 2.0 * M[i1] * etheta; - LGAMMA[i1] += 2.0 * M[i0] * etheta; - OSMOT += M[i0] * M[i1] * (etheta + I * ethetap); - } + etheta = pitz_params[i]->thetas->etheta; + ethetap = pitz_params[i]->thetas->ethetap; + F_var = M[i0] * M[i1] * ethetap; + LGAMMA[i0] += 2.0 * M[i1] * etheta; + LGAMMA[i1] += 2.0 * M[i0] * etheta; + OSMOT += M[i0] * M[i1] * (etheta + I * ethetap); } break; case TYPE_PSI: @@ -1631,11 +1635,11 @@ pitzer_clean_up(void) } pitz_param_map.clear(); pitz_params.clear(); - //for (i = 0; i < (int)theta_params.size(); i++) - //{ - // theta_params[i] = - // (struct theta_param *) free_check_null(theta_params[i]); - //} + for (i = 0; i < (int)theta_params.size(); i++) + { + theta_params[i] = + (struct theta_param *) free_check_null(theta_params[i]); + } theta_params.clear(); LGAMMA.clear(); IPRSNT.clear(); diff --git a/pitzer_structures.cpp b/pitzer_structures.cpp index b9ca8cf8..1d9623c1 100644 --- a/pitzer_structures.cpp +++ b/pitzer_structures.cpp @@ -48,6 +48,7 @@ pitz_param_init(struct pitz_param *pitz_param_ptr) pitz_param_ptr->a[i] = 0.0; } pitz_param_ptr->alpha = 0.0; + pitz_param_ptr->thetas = NULL; pitz_param_ptr->os_coef = 0.; for (i = 0; i < 3; i++) { @@ -209,7 +210,7 @@ pitz_param_store(struct pitz_param *pzp_ptr, bool force_copy) } } // thetas - pitz_params[count_pitz_param]->theta_params_index = -1; + pitz_params[count_pitz_param]->thetas = NULL; pitz_param_map[key] = count_pitz_param; } else @@ -224,7 +225,7 @@ pitz_param_store(struct pitz_param *pzp_ptr, bool force_copy) /* ---------------------------------------------------------------------- */ void Phreeqc:: -sit_param_store(struct pitz_param& pzp, bool force_copy) +sit_param_store(struct pitz_param *pzp_ptr, bool force_copy) /* ---------------------------------------------------------------------- */ { /* @@ -232,19 +233,19 @@ sit_param_store(struct pitz_param& pzp, bool force_copy) * Returns -1 if not found, index number in pitz_params if found */ int i; - //if (pzp_ptr == NULL) - // return; - if (pzp.type == TYPE_Other || pzp.type == TYPE_Other) + if (pzp_ptr == NULL) + return; + if (pzp_ptr->type == TYPE_Other) return; std::set< std::string > header; for (i = 0; i < 3; i++) { - if (pzp.species[i] != NULL) header.insert(pzp.species[i]); + if (pzp_ptr->species[i] != NULL) header.insert(pzp_ptr->species[i]); } std::ostringstream key_str; - key_str << pzp.type << " "; + key_str << pzp_ptr->type << " "; std::set< std::string >::iterator it = header.begin(); for(; it != header.end(); ++it) { @@ -255,19 +256,19 @@ sit_param_store(struct pitz_param& pzp, bool force_copy) std::map< std::string, size_t>::iterator jit = sit_param_map.find(key); if (jit != sit_param_map.end()) { - if (pzp.species[2] != NULL) + if (pzp_ptr->species[2] != NULL) { error_string = sformatf( "Redefinition of parameter, %s %s %s\n", - pzp.species[0], pzp.species[1], pzp.species[2]); + pzp_ptr->species[0], pzp_ptr->species[1], pzp_ptr->species[2]); } else { error_string = sformatf( "Redefinition of parameter, %s %s\n", - pzp.species[0], pzp.species[1]); + pzp_ptr->species[0], pzp_ptr->species[1]); } warning_msg(error_string); - //sit_params[(*jit).second] = (struct pitz_param *) free_check_null(sit_params[(*jit).second]); - sit_params[(*jit).second] = pzp; + sit_params[(*jit).second] = (struct pitz_param *) free_check_null(sit_params[(*jit).second]); + sit_params[(*jit).second] = pzp_ptr; } else { @@ -275,25 +276,25 @@ sit_param_store(struct pitz_param& pzp, bool force_copy) { size_t count_sit_param = sit_params.size(); sit_params.resize(count_sit_param + 1); - sit_params[count_sit_param] = pzp; + sit_params[count_sit_param] = pitz_param_duplicate(pzp_ptr); // clean up pointers // species for (i = 0; i < 3; i++) { - if (pzp.species[i] != NULL) + if (pzp_ptr->species[i] != NULL) { - sit_params[count_sit_param].species[i] = string_hsave(pzp.species[i]); + sit_params[count_sit_param]->species[i] = string_hsave(pzp_ptr->species[i]); } } // thetas - sit_params[count_sit_param].theta_params_index = -1; + sit_params[count_sit_param]->thetas = NULL; sit_param_map[key] = count_sit_param; } else { size_t count_sit_param = sit_params.size(); sit_params.resize(count_sit_param + 1); - sit_params[count_sit_param] = pzp; + sit_params[count_sit_param] = pzp_ptr; sit_param_map[key] = count_sit_param; } } @@ -336,7 +337,7 @@ theta_param_init(struct theta_param *theta_param_ptr) } /* ---------------------------------------------------------------------- */ -int Phreeqc:: +struct theta_param * Phreeqc:: theta_param_search(LDBLE zj, LDBLE zk) /* ---------------------------------------------------------------------- */ { @@ -347,11 +348,11 @@ theta_param_search(LDBLE zj, LDBLE zk) int i; for (i = 0; i < (int)theta_params.size(); i++) { - if ((theta_params[i].zj == zj && theta_params[i].zk == zk) || - (theta_params[i].zj == zk && theta_params[i].zk == zj)) + if ((theta_params[i]->zj == zj && theta_params[i]->zk == zk) || + (theta_params[i]->zj == zk && theta_params[i]->zk == zj)) { - return i; + return theta_params[i]; } } - return -1; + return NULL; } diff --git a/sit.cpp b/sit.cpp index 84bd9ac3..b9214296 100644 --- a/sit.cpp +++ b/sit.cpp @@ -86,19 +86,19 @@ sit_tidy(void) { for (j = 0; j < 3; j++) { - if (sit_params[i].species[j] == NULL) + if (sit_params[i]->species[j] == NULL) continue; - sit_params[i].ispec[j] = sit_ISPEC(sit_params[i].species[j]); - if ((j < 2 && sit_params[i].ispec[j] == -1) || + sit_params[i]->ispec[j] = sit_ISPEC(sit_params[i]->species[j]); + if ((j < 2 && sit_params[i]->ispec[j] == -1) || (j == 3 - && (sit_params[i].type == TYPE_PSI - || sit_params[i].type == TYPE_ZETA) - && sit_params[i].ispec[j] == -1)) + && (sit_params[i]->type == TYPE_PSI + || sit_params[i]->type == TYPE_ZETA) + && sit_params[i]->ispec[j] == -1)) { input_error++; error_string = sformatf( "Species for Pitzer parameter not defined in SOLUTION_SPECIES, %s", - sit_params[i].species[j]); + sit_params[i]->species[j]); error_msg(error_string, CONTINUE); } } @@ -110,10 +110,10 @@ sit_tidy(void) std::set< std::string > header; for (int i = 0; i < 3; i++) { - if (sit_params[j].species[i] != NULL) header.insert(sit_params[j].species[i]); + if (sit_params[j]->species[i] != NULL) header.insert(sit_params[j]->species[i]); } std::ostringstream key_str; - key_str << sit_params[j].type << " "; + key_str << sit_params[j]->type << " "; std::set< std::string >::iterator it = header.begin(); for(; it != header.end(); ++it) { @@ -211,7 +211,7 @@ read_sit(void) if (pzp_ptr != NULL) { pzp_ptr->type = pzp_type; - sit_param_store(*pzp_ptr, false); + sit_param_store(pzp_ptr, false); } break; case OPTION_ERROR: @@ -394,13 +394,13 @@ sit(void) for (size_t j = 0; j < param_list.size(); j++) { int i = param_list[j]; - i0 = sit_params[i].ispec[0]; - i1 = sit_params[i].ispec[1]; + i0 = sit_params[i]->ispec[0]; + i1 = sit_params[i]->ispec[1]; //if (sit_IPRSNT[i0] == FALSE || sit_IPRSNT[i1] == FALSE) continue; z0 = spec[i0]->z; z1 = spec[i1]->z; - param = sit_params[i].p; - switch (sit_params[i].type) + param = sit_params[i]->p; + switch (sit_params[i]->type) { case TYPE_SIT_EPSILON: sit_LGAMMA[i0] += sit_M[i1] * param; @@ -494,10 +494,10 @@ sit_clean_up(void) */ int i; - //for (i = 0; i < (int)sit_params.size(); i++) - //{ - // sit_params[i] = (struct pitz_param *) free_check_null(sit_params[i]); - //} + for (i = 0; i < (int)sit_params.size(); i++) + { + sit_params[i] = (struct pitz_param *) free_check_null(sit_params[i]); + } sit_params.clear(); sit_param_map.clear(); sit_LGAMMA.clear(); @@ -1389,7 +1389,7 @@ C Set DW0 for (size_t j = 0; j < param_list.size(); j++) { int i = param_list[j]; - calc_sit_param(&sit_params[i], TK, TR); + calc_sit_param(sit_params[i], TK, TR); } calc_dielectrics(TK - 273.15, patm_x); sit_A0 = A0; @@ -1465,8 +1465,8 @@ sit_make_lists(void) } for (int i = 0; i < (int)sit_params.size(); i++) { - int i0 = sit_params[i].ispec[0]; - int i1 = sit_params[i].ispec[1]; + int i0 = sit_params[i]->ispec[0]; + int i1 = sit_params[i]->ispec[1]; if (sit_IPRSNT[i0] == FALSE || sit_IPRSNT[i1] == FALSE) continue; param_list.push_back(i); } From 50e8903ea48a4c7255023f05d4f0b6168b04882c Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Sun, 4 Apr 2021 07:51:32 -0600 Subject: [PATCH 49/53] new/delete pitz_params --- Phreeqc.cpp | 2 +- Phreeqc.h | 7 ---- pitzer.cpp | 9 ++-- pitzer_structures.cpp | 97 ++++--------------------------------------- sit.cpp | 4 +- 5 files changed, 15 insertions(+), 104 deletions(-) diff --git a/Phreeqc.cpp b/Phreeqc.cpp index e46a465d..fda4a509 100644 --- a/Phreeqc.cpp +++ b/Phreeqc.cpp @@ -1935,7 +1935,7 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) */ if (pSrc->aphi != NULL) { - aphi = (struct pitz_param*)malloc(sizeof(struct pitz_param)); + aphi = new struct pitz_param; memcpy(aphi, pSrc->aphi, sizeof(struct pitz_param)); } /* diff --git a/Phreeqc.h b/Phreeqc.h index 25092222..c29e264f 100644 --- a/Phreeqc.h +++ b/Phreeqc.h @@ -526,13 +526,6 @@ public: int PTEMP(LDBLE TK); int jacobian_pz(void); - // pitzer_structures.cpp ------------------------------- - struct pitz_param* pitz_param_alloc(void); - int pitz_param_init(struct pitz_param* pitz_param_ptr); - struct pitz_param* pitz_param_duplicate(struct pitz_param* old_ptr); - int pitz_param_copy(struct pitz_param* old_ptr, - struct pitz_param* new_ptr); - // prep.cpp ------------------------------- int add_potential_factor(void); int add_cd_music_factors(int n); diff --git a/pitzer.cpp b/pitzer.cpp index 3e650d24..a5ac9a3c 100644 --- a/pitzer.cpp +++ b/pitzer.cpp @@ -110,7 +110,7 @@ pitzer_tidy(void) { if (pitz_params_temp[i]->type == TYPE_ETHETA) { - pitz_params_temp[i] = (struct pitz_param*)free_check_null(pitz_params_temp[i]); + delete pitz_params_temp[i]; } else { @@ -624,7 +624,7 @@ read_pitzer(void) pzp_ptr->type = pzp_type; if (pzp_type == TYPE_APHI) { - aphi = (struct pitz_param *) free_check_null(aphi); + delete aphi; aphi = pzp_ptr; } else @@ -1630,8 +1630,7 @@ pitzer_clean_up(void) int i; for (i = 0; i < (int)pitz_params.size(); i++) { - pitz_params[i] = - (struct pitz_param *) free_check_null(pitz_params[i]); + delete pitz_params[i]; } pitz_param_map.clear(); pitz_params.clear(); @@ -1644,7 +1643,7 @@ pitzer_clean_up(void) LGAMMA.clear(); IPRSNT.clear(); spec.clear(); - aphi = (struct pitz_param *) free_check_null(aphi); + delete aphi; M.clear(); return OK; diff --git a/pitzer_structures.cpp b/pitzer_structures.cpp index 1d9623c1..fc8ba7d5 100644 --- a/pitzer_structures.cpp +++ b/pitzer_structures.cpp @@ -9,53 +9,6 @@ * Routines related to structure "pitz_param" * * ********************************************************************** */ -/* ---------------------------------------------------------------------- */ -struct pitz_param * Phreeqc:: -pitz_param_alloc(void) -/* ---------------------------------------------------------------------- */ -{ - struct pitz_param *pitz_param_ptr; - pitz_param_ptr = - (struct pitz_param *) PHRQ_malloc(sizeof(struct pitz_param)); - if (pitz_param_ptr == NULL) - malloc_error(); - return (pitz_param_ptr); -} - -/* ---------------------------------------------------------------------- */ -int Phreeqc:: -pitz_param_init(struct pitz_param *pitz_param_ptr) -/* ---------------------------------------------------------------------- */ -{ - int i; -/* - * Frees all data associated with pitz_param structure. - */ - - if (pitz_param_ptr == NULL) - return (ERROR); - pitz_param_ptr->species[0] = NULL; - pitz_param_ptr->species[1] = NULL; - pitz_param_ptr->species[2] = NULL; - pitz_param_ptr->ispec[0] = -1; - pitz_param_ptr->ispec[1] = -1; - pitz_param_ptr->ispec[2] = -1; - pitz_param_ptr->type = TYPE_Other; - pitz_param_ptr->p = 0.0; - pitz_param_ptr->U.b0 = 0.0; - for (i = 0; i < 6; i++) - { - pitz_param_ptr->a[i] = 0.0; - } - pitz_param_ptr->alpha = 0.0; - pitz_param_ptr->thetas = NULL; - pitz_param_ptr->os_coef = 0.; - for (i = 0; i < 3; i++) - { - pitz_param_ptr->ln_coef[i] = 0.0; - } - return (OK); -} /* ---------------------------------------------------------------------- */ struct pitz_param * Phreeqc:: @@ -77,7 +30,6 @@ pitz_param_read(char *string, int n) if (string == NULL) return (NULL); - pitz_param_init(&pzp); cptr = string; if (copy_token(token, &cptr, &l) == EMPTY) return (NULL); @@ -108,45 +60,10 @@ pitz_param_read(char *string, int n) } if (k <= 0) return (NULL); - pzp_ptr = pitz_param_duplicate(&pzp); + pzp_ptr = new struct pitz_param; + *pzp_ptr = pzp; return (pzp_ptr); } - -/* ---------------------------------------------------------------------- */ -struct pitz_param * Phreeqc:: -pitz_param_duplicate(struct pitz_param *old_ptr) -/* ---------------------------------------------------------------------- */ -{ -/* - * Allocates space and makes duplicate copy of pitz_param structure - */ - struct pitz_param *new_ptr; - - new_ptr = pitz_param_alloc(); - pitz_param_init(new_ptr); -/* - * Copy data - */ - pitz_param_copy(old_ptr, new_ptr); - return (new_ptr); -} - -/* ---------------------------------------------------------------------- */ -int Phreeqc:: -pitz_param_copy(struct pitz_param *old_ptr, struct pitz_param *new_ptr) -/* ---------------------------------------------------------------------- */ -{ -/* - * Copies pitz_param data from old_ptr to new location, new_ptr. - * Space for the new_ptr structure must already be malloced. - */ -/* - * Store data for structure pitz_param - */ - memcpy(new_ptr, old_ptr, sizeof(struct pitz_param)); - return (OK); -} - /* ---------------------------------------------------------------------- */ void Phreeqc:: pitz_param_store(struct pitz_param *pzp_ptr, bool force_copy) @@ -190,7 +107,7 @@ pitz_param_store(struct pitz_param *pzp_ptr, bool force_copy) pzp_ptr->species[0], pzp_ptr->species[1]); } warning_msg(error_string); - pitz_params[(*jit).second] = (struct pitz_param *) free_check_null(pitz_params[(*jit).second]); + delete pitz_params[(*jit).second]; pitz_params[(*jit).second] = pzp_ptr; } else @@ -199,7 +116,8 @@ pitz_param_store(struct pitz_param *pzp_ptr, bool force_copy) { size_t count_pitz_param = pitz_params.size(); pitz_params.resize(count_pitz_param + 1); - pitz_params[count_pitz_param] = pitz_param_duplicate(pzp_ptr); + pitz_params[count_pitz_param] = new struct pitz_param; + *pitz_params[count_pitz_param] = *pzp_ptr; // clean up pointers // species for (i = 0; i < 3; i++) @@ -267,7 +185,7 @@ sit_param_store(struct pitz_param *pzp_ptr, bool force_copy) pzp_ptr->species[0], pzp_ptr->species[1]); } warning_msg(error_string); - sit_params[(*jit).second] = (struct pitz_param *) free_check_null(sit_params[(*jit).second]); + delete sit_params[(*jit).second]; sit_params[(*jit).second] = pzp_ptr; } else @@ -276,7 +194,8 @@ sit_param_store(struct pitz_param *pzp_ptr, bool force_copy) { size_t count_sit_param = sit_params.size(); sit_params.resize(count_sit_param + 1); - sit_params[count_sit_param] = pitz_param_duplicate(pzp_ptr); + sit_params[count_sit_param] = new struct pitz_param; + *sit_params[count_sit_param] = *pzp_ptr; // clean up pointers // species for (i = 0; i < 3; i++) diff --git a/sit.cpp b/sit.cpp index b9214296..38ce6464 100644 --- a/sit.cpp +++ b/sit.cpp @@ -496,14 +496,14 @@ sit_clean_up(void) for (i = 0; i < (int)sit_params.size(); i++) { - sit_params[i] = (struct pitz_param *) free_check_null(sit_params[i]); + delete sit_params[i]; } sit_params.clear(); sit_param_map.clear(); sit_LGAMMA.clear(); sit_IPRSNT.clear(); spec.clear(); - aphi = (struct pitz_param *) free_check_null(aphi); + delete aphi; sit_M.clear(); return OK; From 7bd13ffafa0348ede73abe11beced419eb00e903 Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Sun, 4 Apr 2021 08:31:58 -0600 Subject: [PATCH 50/53] new/delete theta params, pitz_param_copy --- Phreeqc.cpp | 10 ++---- Phreeqc.h | 3 +- global_structures.h | 4 +-- pitzer.cpp | 8 ++--- pitzer_structures.cpp | 75 +++++++++++-------------------------------- 5 files changed, 27 insertions(+), 73 deletions(-) diff --git a/Phreeqc.cpp b/Phreeqc.cpp index fda4a509..cb5f1185 100644 --- a/Phreeqc.cpp +++ b/Phreeqc.cpp @@ -1923,8 +1923,8 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) { size_t count_theta_params = theta_params.size(); theta_params.resize(count_theta_params + 1); - theta_params[count_theta_params] = theta_param_alloc(); - memcpy(theta_params[count_theta_params], pSrc->theta_params[i], sizeof(struct theta_param)); + theta_params[count_theta_params] = new struct theta_param; + *theta_params[count_theta_params] = *pSrc->theta_params[i]; } use_etheta = pSrc->use_etheta; /* @@ -1933,11 +1933,7 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) A0 = 0; aphi */ - if (pSrc->aphi != NULL) - { - aphi = new struct pitz_param; - memcpy(aphi, pSrc->aphi, sizeof(struct pitz_param)); - } + aphi = pitz_param_copy(pSrc->aphi); /* spec = NULL; cations = NULL; diff --git a/Phreeqc.h b/Phreeqc.h index c29e264f..98fa9cf0 100644 --- a/Phreeqc.h +++ b/Phreeqc.h @@ -501,9 +501,8 @@ public: struct pitz_param* pitz_param_read(char* string, int n); void pitz_param_store(struct pitz_param* pzp_ptr, bool force_copy); void sit_param_store(struct pitz_param* pzp_ptr, bool force_copy); + struct pitz_param* pitz_param_copy(const struct pitz_param* src); struct theta_param* theta_param_search(LDBLE zj, LDBLE zk); - struct theta_param* theta_param_alloc(void); - int theta_param_init(struct theta_param* theta_param_ptr); void pitzer_make_lists(void); int gammas_pz(bool exch_a_f); int model_pz(void); diff --git a/global_structures.h b/global_structures.h index c064b94a..0009613c 100644 --- a/global_structures.h +++ b/global_structures.h @@ -955,8 +955,8 @@ typedef enum struct pitz_param { const char* species[3] = { NULL,NULL,NULL }; - int ispec[3] = { 0,0,0 }; - pitz_param_type type = TYPE_B0; + int ispec[3] = { -1,-1,-1 }; + pitz_param_type type = TYPE_Other; LDBLE p = 0; union { diff --git a/pitzer.cpp b/pitzer.cpp index a5ac9a3c..2cd5827e 100644 --- a/pitzer.cpp +++ b/pitzer.cpp @@ -298,7 +298,7 @@ pitzer_tidy(void) for (i = 0; i < (int)theta_params.size(); i++) { - theta_params[i] = (struct theta_param *) free_check_null(theta_params[i]); + delete theta_params[i]; } theta_params.clear(); for (i = 0; i < (int)pitz_params.size(); i++) @@ -312,8 +312,7 @@ pitzer_tidy(void) { size_t count_theta_param = theta_params.size(); theta_params.resize(count_theta_param + 1); - theta_params[count_theta_param] = theta_param_alloc(); - theta_param_init(theta_params[count_theta_param]); + theta_params[count_theta_param] = new struct theta_param; theta_params[count_theta_param]->zj = z0; theta_params[count_theta_param]->zk = z1; theta_param_ptr = theta_params[count_theta_param]; @@ -1636,8 +1635,7 @@ pitzer_clean_up(void) pitz_params.clear(); for (i = 0; i < (int)theta_params.size(); i++) { - theta_params[i] = - (struct theta_param *) free_check_null(theta_params[i]); + delete theta_params[i]; } theta_params.clear(); LGAMMA.clear(); diff --git a/pitzer_structures.cpp b/pitzer_structures.cpp index fc8ba7d5..e3a16c52 100644 --- a/pitzer_structures.cpp +++ b/pitzer_structures.cpp @@ -116,19 +116,7 @@ pitz_param_store(struct pitz_param *pzp_ptr, bool force_copy) { size_t count_pitz_param = pitz_params.size(); pitz_params.resize(count_pitz_param + 1); - pitz_params[count_pitz_param] = new struct pitz_param; - *pitz_params[count_pitz_param] = *pzp_ptr; - // clean up pointers - // species - for (i = 0; i < 3; i++) - { - if (pzp_ptr->species[i] != NULL) - { - pitz_params[count_pitz_param]->species[i] = string_hsave(pzp_ptr->species[i]); - } - } - // thetas - pitz_params[count_pitz_param]->thetas = NULL; + pitz_params[count_pitz_param] = pitz_param_copy(pzp_ptr); pitz_param_map[key] = count_pitz_param; } else @@ -194,19 +182,7 @@ sit_param_store(struct pitz_param *pzp_ptr, bool force_copy) { size_t count_sit_param = sit_params.size(); sit_params.resize(count_sit_param + 1); - sit_params[count_sit_param] = new struct pitz_param; - *sit_params[count_sit_param] = *pzp_ptr; - // clean up pointers - // species - for (i = 0; i < 3; i++) - { - if (pzp_ptr->species[i] != NULL) - { - sit_params[count_sit_param]->species[i] = string_hsave(pzp_ptr->species[i]); - } - } - // thetas - sit_params[count_sit_param]->thetas = NULL; + sit_params[count_sit_param] = pitz_param_copy(pzp_ptr); sit_param_map[key] = count_sit_param; } else @@ -218,43 +194,28 @@ sit_param_store(struct pitz_param *pzp_ptr, bool force_copy) } } } +struct pitz_param* Phreeqc:: +pitz_param_copy(const struct pitz_param* src) +{ + if (src == NULL) return NULL; + struct pitz_param* dest = new struct pitz_param; + *dest = *src; + for (size_t i = 1; i < 3; i++) + { + if (src->species[i] != NULL) + { + dest->species[i] = string_hsave(src->species[i]); + } + } + dest->thetas = NULL; + return dest; +} /* ********************************************************************** * * Routines related to structure "theta_parm" * * ********************************************************************** */ -/* ---------------------------------------------------------------------- */ -struct theta_param * Phreeqc:: -theta_param_alloc(void) -/* ---------------------------------------------------------------------- */ -{ - struct theta_param *theta_param_ptr; - theta_param_ptr = - (struct theta_param *) PHRQ_malloc(sizeof(struct theta_param)); - if (theta_param_ptr == NULL) - malloc_error(); - return (theta_param_ptr); -} - -/* ---------------------------------------------------------------------- */ -int Phreeqc:: -theta_param_init(struct theta_param *theta_param_ptr) -/* ---------------------------------------------------------------------- */ -{ -/* - * Frees all data associated with theta_param structure. - */ - - if (theta_param_ptr == NULL) - return (ERROR); - theta_param_ptr->zj = 0; - theta_param_ptr->zk = 0; - theta_param_ptr->etheta = 0; - theta_param_ptr->ethetap = 0; - return (OK); -} - /* ---------------------------------------------------------------------- */ struct theta_param * Phreeqc:: theta_param_search(LDBLE zj, LDBLE zk) From 7ce894704118f65e68a6cb5bafbbf856a80a3d1e Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Mon, 5 Apr 2021 00:24:55 -0600 Subject: [PATCH 51/53] updated InternalCopy for operator equal --- Phreeqc.cpp | 1179 ++++++++++++++++++++++-------------------------- Phreeqc.h | 2 +- advection.cpp | 1 - structures.cpp | 3 +- utilities.cpp | 1 + 5 files changed, 554 insertions(+), 632 deletions(-) diff --git a/Phreeqc.cpp b/Phreeqc.cpp index cb5f1185..9a908634 100644 --- a/Phreeqc.cpp +++ b/Phreeqc.cpp @@ -1125,324 +1125,224 @@ Phreeqc::Phreeqc(const Phreeqc &src) InternalCopy(&src); } void -Phreeqc::InternalCopy(const Phreeqc *pSrc) +Phreeqc::InternalCopy(const Phreeqc* pSrc) { // phrq_io - /* - if (io) - { - this->phrq_io = io; - } - else - { - this->phrq_io = &this->ioInstance; - } - */ - same_model = FALSE; - current_tc = pSrc->current_tc; - current_pa = pSrc->current_pa; - current_mu = pSrc->current_mu; - mu_terms_in_logk = pSrc->mu_terms_in_logk; + same_model = FALSE; + current_tc = pSrc->current_tc; + current_pa = pSrc->current_pa; + current_mu = pSrc->current_mu; + mu_terms_in_logk = pSrc->mu_terms_in_logk; - MIN_LM = pSrc->MIN_LM; /* minimum log molality allowed before molality set to zero */ - LOG_ZERO_MOLALITY = pSrc->LOG_ZERO_MOLALITY; /* molalities <= LOG_ZERO_MOLALITY are considered equal to zero */ - MIN_RELATED_LOG_ACTIVITY = pSrc->MIN_RELATED_LOG_ACTIVITY; - MIN_TOTAL = pSrc->MIN_TOTAL; - MIN_TOTAL_SS = pSrc->MIN_TOTAL_SS; - MIN_RELATED_SURFACE = pSrc->MIN_RELATED_SURFACE; /* ---------------------------------------------------------------------- * STRUCTURES * ---------------------------------------------------------------------- */ -/* - * last model - */ - //-- skip last model, accept init - -/* - * Initialize punch - */ - //-- skip punch, accept init + //last_model, accept init high_precision = pSrc->high_precision; - + // Maps Rxn_temperature_map = pSrc->Rxn_temperature_map; Rxn_pressure_map = pSrc->Rxn_pressure_map; - - /* ---------------------------------------------------------------------- - * Surface - * --------------------------------------------------------------------- */ - g_iterations = -1; - G_TOL = 1e-8; + g_iterations = -1; + G_TOL = pSrc->G_TOL; Rxn_surface_map = pSrc->Rxn_surface_map; - // auto charge_group_map; - /* - change_surf_count = 0; - change_surf = NULL; - */ change_surf_count = pSrc->change_surf_count; change_surf = change_surf_alloc(change_surf_count + 1); - if (change_surf_count > 0) + for (int ii = 0; ii < change_surf_count; ii++) { - for (int ii = 0; ii < change_surf_count; ii++) - { - change_surf[ii].comp_name = string_hsave(pSrc->change_surf[ii].comp_name); - change_surf[ii].fraction = pSrc->change_surf[ii].fraction; - change_surf[ii].new_comp_name = string_hsave(pSrc->change_surf[ii].new_comp_name); - change_surf[ii].new_Dw = pSrc->change_surf[ii].new_Dw; - change_surf[ii].cell_no = pSrc->change_surf[ii].cell_no; - change_surf[ii].next = pSrc->change_surf[ii].next; - } + change_surf[ii].comp_name = string_hsave(pSrc->change_surf[ii].comp_name); + change_surf[ii].fraction = pSrc->change_surf[ii].fraction; + change_surf[ii].new_comp_name = string_hsave(pSrc->change_surf[ii].new_comp_name); + change_surf[ii].new_Dw = pSrc->change_surf[ii].new_Dw; + change_surf[ii].cell_no = pSrc->change_surf[ii].cell_no; + change_surf[ii].next = pSrc->change_surf[ii].next; } - - /* ---------------------------------------------------------------------- - * Exchange - * ---------------------------------------------------------------------- */ Rxn_exchange_map = pSrc->Rxn_exchange_map; - - /* ---------------------------------------------------------------------- - * Kinetics - * ---------------------------------------------------------------------- */ Rxn_kinetics_map = pSrc->Rxn_kinetics_map; - - /*---------------------------------------------------------------------- - * Save - *---------------------------------------------------------------------- */ - - /*---------------------------------------------------------------------- - * Inverse - *---------------------------------------------------------------------- */ - - /* - inverse = NULL; - */ - count_inverse = 0; - /*---------------------------------------------------------------------- - * Mix - *---------------------------------------------------------------------- */ - // Should be empty after each END - // auto Rxn_mix_map; + use_kinetics_limiter = pSrc->use_kinetics_limiter; + save_values = pSrc->save_values; + save = pSrc->save; + //struct copier copy_solution; + //struct copier copy_pp_assemblage; + //struct copier copy_exchange; + //struct copier copy_surface; + //struct copier copy_ss_assemblage; + //struct copier copy_gas_phase; + //struct copier copy_kinetics; + //struct copier copy_mix; + //struct copier copy_reaction; + //struct copier copy_temperature; + //struct copier copy_pressure; + // Inverse not implemented + //std::vector inverse; + count_inverse = 0; + // Mix Rxn_mix_map = pSrc->Rxn_mix_map; - // auto Dispersion_mix_map; Dispersion_mix_map = pSrc->Dispersion_mix_map; - // auto Rxn_solution_mix_map; Rxn_solution_mix_map = pSrc->Rxn_solution_mix_map; - // auto Rxn_exchange_mix_map; Rxn_exchange_mix_map = pSrc->Rxn_exchange_mix_map; - // auto Rxn_gas_phase_mix_map; Rxn_gas_phase_mix_map = pSrc->Rxn_gas_phase_mix_map; - // auto Rxn_kinetics_mix_map; Rxn_kinetics_mix_map = pSrc->Rxn_kinetics_mix_map; - // auto Rxn_pp_assemblage_mix_map; Rxn_pp_assemblage_mix_map = pSrc->Rxn_pp_assemblage_mix_map; - // auto Rxn_ss_assemblage_mix_map; Rxn_ss_assemblage_mix_map = pSrc->Rxn_ss_assemblage_mix_map; - // auto Rxn_surface_mix_map; Rxn_surface_mix_map = pSrc->Rxn_surface_mix_map; - /* - * List new definitions - */ - // Assume no new definitions - /*---------------------------------------------------------------------- - * Irreversible reaction - *---------------------------------------------------------------------- */ + //List new definitions + //std::set Rxn_new_exchange; + //std::set Rxn_new_gas_phase; + //std::set Rxn_new_kinetics; // not used + //std::set Rxn_new_mix; // not used + //std::set Rxn_new_pp_assemblage; + //std::set Rxn_new_pressure; // not used + //std::set Rxn_new_reaction; // not used + //std::set Rxn_new_solution; + //std::set Rxn_new_ss_assemblage; + //std::set Rxn_new_surface; + //std::set Rxn_new_temperature; // not used Rxn_reaction_map = pSrc->Rxn_reaction_map; - /*---------------------------------------------------------------------- - * Gas phase - *---------------------------------------------------------------------- */ Rxn_gas_phase_map = pSrc->Rxn_gas_phase_map; - /*---------------------------------------------------------------------- - * Solid solution - *---------------------------------------------------------------------- */ Rxn_ss_assemblage_map = pSrc->Rxn_ss_assemblage_map; - /*---------------------------------------------------------------------- - * Pure-phase assemblage - *---------------------------------------------------------------------- */ Rxn_pp_assemblage_map = pSrc->Rxn_pp_assemblage_map; - /*---------------------------------------------------------------------- - * Species_list - *---------------------------------------------------------------------- */ - /* - count_species_list = 0; - max_species_list = 0; - species_list = NULL; - */ - /*---------------------------------------------------------------------- - * Jacobian and Mass balance lists - *---------------------------------------------------------------------- */ - /* - count_sum_jacob0 = 0; - max_sum_jacob0 = 0; - sum_jacob0 = NULL; - count_sum_mb1 = 0; - max_sum_mb1 = 0; - sum_mb1 = NULL; - count_sum_jacob1 = 0; - max_sum_jacob1 = 0; - sum_jacob1 = NULL; - count_sum_mb2 = 0; - max_sum_mb2 = 0; - sum_mb2 = NULL; - count_sum_jacob2 = 0; - max_sum_jacob2 = 0; - sum_jacob2 = NULL; - count_sum_delta = 0; - max_sum_delta = 0; - sum_delta = NULL; - */ - /*---------------------------------------------------------------------- - * Solution - *---------------------------------------------------------------------- */ - Rxn_solution_map = pSrc->Rxn_solution_map; - save_species = pSrc->save_species; - // auto Rxn_solution_map; - // auto unnumbered_solutions; - /*---------------------------------------------------------------------- - * Global solution - *---------------------------------------------------------------------- */ - last_title_x = pSrc->last_title_x; - /* - new_x = FALSE; - tc_x = 0; - tk_x = 0; - patm_x = 1; - last_patm_x = 1; - numerical_fixed_volume = false; - force_numerical_fixed_volume = false; - //switch_numerical = false; - ph_x = 0; - solution_pe_x = 0; - mu_x = 0; - ah2o_x = 1.0; - density_x = 0; - total_h_x = 0; - total_o_x = 0; - cb_x = 0; - total_ions_x = 0; - mass_water_aq_x = 0; - mass_water_surfaces_x = 0; - mass_water_bulk_x = 0; - */ - // auto pe_x - // auto isotopes_x - // auto default_pe_x - /* - dl_type_x = cxxSurface::NO_DL; - total_carbon = 0; - total_co2 = 0; - total_alkalinity = 0; - gfw_water = 0; - step_x = 0; - kin_time_x = 0; - */ - /*---------------------------------------------------------------------- - * Transport data - *---------------------------------------------------------------------- */ - count_cells = pSrc->count_cells; - count_shifts = pSrc->count_shifts; - ishift = pSrc->ishift; - bcon_first = pSrc->bcon_first; - bcon_last = pSrc->bcon_last; - correct_disp = pSrc->correct_disp; - tempr = pSrc->tempr; - timest = pSrc->timest; - simul_tr = pSrc->simul_tr; - diffc = pSrc->diffc; - heat_diffc = pSrc->heat_diffc; - cell = pSrc->cell; - mcd_substeps = pSrc->mcd_substeps; - /* stag_data */ - stag_data = pSrc->stag_data; - print_modulus = pSrc->print_modulus; - punch_modulus = pSrc->punch_modulus; - dump_in = pSrc->dump_in; - dump_modulus = pSrc->dump_modulus; - transport_warnings = pSrc->transport_warnings; - /* cell_data */ + std::vector species_list; + // will be rebuilt + //std::vector sum_jacob0; + //std::vector sum_mb1; + //std::vector sum_jacob1; + //std::vector sum_mb2; + //std::vector sum_jacob2; + //std::vector sum_delta; + // Solution + Rxn_solution_map = pSrc->Rxn_solution_map; + unnumbered_solutions = pSrc->unnumbered_solutions; + save_species = pSrc->save_species; + // Global solution + title_x = pSrc->title_x; + last_title_x = pSrc->last_title_x; + //new_x = FALSE; + description_x = pSrc->description_x; + //new_x = FALSE; + description_x = pSrc->description_x; + //tc_x = 0; + //tk_x = 0; + //patm_x = 1; + //last_patm_x = 1; + //numerical_fixed_volume = false; + //force_numerical_fixed_volume = false; + //ph_x = 0; + //solution_pe_x = 0; + //mu_x = 0; + //ah2o_x = 1.0; + //density_x = 0; + //total_h_x = 0; + //total_o_x = 0; + //cb_x = 0; + //total_ions_x = 0; + //mass_water_aq_x = 0; + //mass_water_surfaces_x = 0; + //mass_water_bulk_x = 0; + //units_x + //pe_x + //isotopes_x + //default_pe_x + //dl_type_x = cxxSurface::NO_DL; + //total_carbon = 0; + //total_co2 = 0; + //total_alkalinity = 0; + gfw_water = pSrc->gfw_water; + //step_x = 0; + //kin_time_x = 0; + // Transport data + count_cells = pSrc->count_cells; + count_shifts = pSrc->count_shifts; + ishift = pSrc->ishift; + bcon_first = pSrc->bcon_first; + bcon_last = pSrc->bcon_last; + correct_disp = pSrc->correct_disp; + tempr = pSrc->tempr; + timest = pSrc->timest; + simul_tr = pSrc->simul_tr; + diffc = pSrc->diffc; + heat_diffc = pSrc->heat_diffc; + cell = pSrc->cell; + mcd_substeps = pSrc->mcd_substeps; + stag_data = pSrc->stag_data; + print_modulus = pSrc->print_modulus; + punch_modulus = pSrc->punch_modulus; + dump_in = pSrc->dump_in; + dump_modulus = pSrc->dump_modulus; + transport_warnings = pSrc->transport_warnings; + // cell_data + cell_data = pSrc->cell_data; old_cells = pSrc->old_cells; max_cells = pSrc->max_cells; - if (stag_data.count_stag > 0) { max_cells = (max_cells - 2) / (1 + stag_data.count_stag); } - all_cells = pSrc->all_cells; - cell_data = pSrc->cell_data; max_cells = pSrc->max_cells; - multi_Dflag = pSrc->multi_Dflag; - interlayer_Dflag = pSrc->interlayer_Dflag; - implicit = pSrc->implicit; - max_mixf = pSrc->max_mixf; - min_dif_LM = pSrc->min_dif_LM; - default_Dw = pSrc->default_Dw; - correct_Dw = pSrc->correct_Dw; - multi_Dpor = pSrc->multi_Dpor; - interlayer_Dpor = pSrc->interlayer_Dpor; - multi_Dpor_lim = pSrc->multi_Dpor_lim; - interlayer_Dpor_lim = pSrc->interlayer_Dpor_lim; - multi_Dn = pSrc->multi_Dn; - interlayer_tortf = pSrc->interlayer_tortf; - cell_no = pSrc->cell_no; - mixrun = pSrc->mixrun; - fix_current = pSrc->fix_current; - /*---------------------------------------------------------------------- - * Advection data - *---------------------------------------------------------------------- */ - count_ad_cells = pSrc->count_ad_cells; - count_ad_shifts = pSrc->count_ad_shifts; - print_ad_modulus = pSrc->print_ad_modulus; - punch_ad_modulus = pSrc->punch_ad_modulus; - /* advection_punch, advection_print */ + multi_Dflag = pSrc->multi_Dflag; + interlayer_Dflag = pSrc->interlayer_Dflag; + implicit = pSrc->implicit; + max_mixf = pSrc->max_mixf; + min_dif_LM = pSrc->min_dif_LM; + default_Dw = pSrc->default_Dw; + correct_Dw = pSrc->correct_Dw; + multi_Dpor = pSrc->multi_Dpor; + interlayer_Dpor = pSrc->interlayer_Dpor; + multi_Dpor_lim = pSrc->multi_Dpor_lim; + interlayer_Dpor_lim = pSrc->interlayer_Dpor_lim; + multi_Dn = pSrc->multi_Dn; + interlayer_tortf = pSrc->interlayer_tortf; + cell_no = pSrc->cell_no; + mixrun = pSrc->mixrun; + // Advection data + count_ad_cells = pSrc->count_ad_cells; + count_ad_shifts = pSrc->count_ad_shifts; + print_ad_modulus = pSrc->print_ad_modulus; + punch_ad_modulus = pSrc->punch_ad_modulus; advection_punch = pSrc->advection_punch; advection_print = pSrc->advection_print; - - advection_kin_time = pSrc->advection_kin_time; + advection_kin_time = pSrc->advection_kin_time; advection_kin_time_defined = pSrc->advection_kin_time_defined; - advection_warnings = pSrc->advection_warnings; - /*---------------------------------------------------------------------- - * Tidy data - *---------------------------------------------------------------------- */ - /* - new_model = TRUE; - new_exchange = FALSE; - new_pp_assemblage = FALSE; - new_surface = FALSE; - new_reaction = FALSE; - new_temperature = FALSE; - new_mix = FALSE; - new_solution = FALSE; - new_gas_phase = FALSE; - new_inverse = FALSE; - new_punch = FALSE; - new_ss_assemblage = FALSE; - new_kinetics = FALSE; - new_copy = FALSE; - new_pitzer = FALSE; - */ - /*---------------------------------------------------------------------- - * Elements - *---------------------------------------------------------------------- */ + advection_warnings = pSrc->advection_warnings; + // Tidy data + new_model = TRUE; + new_exchange = FALSE; + new_pp_assemblage = FALSE; + new_surface = FALSE; + new_reaction = FALSE; + new_temperature = FALSE; + new_mix = FALSE; + new_solution = FALSE; + new_gas_phase = FALSE; + new_inverse = FALSE; + new_punch = FALSE; + new_ss_assemblage = FALSE; + new_kinetics = FALSE; + new_copy = FALSE; + new_pitzer = FALSE; + // Elements for (int i = 0; i < (int)pSrc->elements.size(); i++) { - const char* ptr = string_hsave(pSrc->elements[i]->name); - struct element *elt_ptr = element_store(ptr); + struct element* elt_ptr = element_store(ptr); elt_ptr->gfw = pSrc->elements[i]->gfw; } element_h_one = element_store("H(1)"); - - /*---------------------------------------------------------------------- - * Element List - *---------------------------------------------------------------------- */ - /* - count_elts = 0; - */ - /*---------------------------------------------------------------------- - * Reaction - *---------------------------------------------------------------------- */ - //bool run_cells_one_step; + // Element List + count_elts = 0; + // Reaction run_cells_one_step = pSrc->run_cells_one_step; + // logk + logk.clear(); + for (size_t i = 0; i < pSrc->logk.size(); i++) + { + struct logk* tlk = new struct logk; + *tlk = *pSrc->logk[i]; + tlk->name = string_hsave(logk[i]->name); + logk.push_back(tlk); + } /*---------------------------------------------------------------------- * Species *---------------------------------------------------------------------- */ @@ -1464,12 +1364,12 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) s_co3 = NULL; s_h2 = NULL; s_o2 = NULL; - */ + */ // logk for (int i = 0; i < (int)pSrc->logk.size(); i++) { - struct logk *logk_ptr = logk_store(pSrc->logk[i]->name, FALSE); + struct logk* logk_ptr = logk_store(pSrc->logk[i]->name, FALSE); memcpy(logk_ptr, pSrc->logk[i], sizeof(struct logk)); logk_ptr->add_logk.resize(pSrc->logk[i]->add_logk.size()); for (size_t j = 0; j < logk_ptr->add_logk.size(); j++) @@ -1478,11 +1378,10 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) logk_ptr->add_logk[j].name = string_hsave(pSrc->logk[i]->add_logk[j].name); } } - // s, species for (int i = 0; i < (int)pSrc->s.size(); i++) { - struct species *s_ptr = s_store(pSrc->s[i]->name, pSrc->s[i]->z, FALSE); + struct species* s_ptr = s_store(pSrc->s[i]->name, pSrc->s[i]->z, FALSE); memcpy(s_ptr, pSrc->s[i], sizeof(struct species)); // fix up all pointers s_ptr->mole_balance = NULL; @@ -1492,7 +1391,6 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) } s_ptr->primary = NULL; s_ptr->secondary = NULL; - //add_logk s_ptr->add_logk.resize(pSrc->s[i]->add_logk.size()); for (size_t j = 0; j < s_ptr->add_logk.size(); j++) { @@ -1501,8 +1399,6 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) } //next_elt s_ptr->next_elt = elt_list_internal_copy(pSrc->s[i]->next_elt); - - //next_secondary s_ptr->next_secondary = elt_list_internal_copy(pSrc->s[i]->next_secondary); s_ptr->next_sys_total = elt_list_internal_copy(pSrc->s[i]->next_sys_total); //rxn @@ -1510,19 +1406,21 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) s_ptr->rxn_s = CReaction_internal_copy(pSrc->s[i]->rxn_s); s_ptr->rxn_x = CReaction_internal_copy(pSrc->s[i]->rxn_x); } - s_h2o = s_search("H2O"); - s_hplus = s_search("H+"); - s_h3oplus = s_search("H3O+"); - s_eminus = s_search("e-"); - s_co3 = s_search("CO3-2"); - s_h2 = s_search("H2"); - s_o2 = s_search("O2"); + s_diff_layer = pSrc->s_diff_layer; + //s_x will be built + s_h2o = s_search("H2O"); + s_hplus = s_search("H+"); + s_h3oplus = s_search("H3O+"); + s_eminus = s_search("e-"); + s_co3 = s_search("CO3-2"); + s_h2 = s_search("H2"); + s_o2 = s_search("O2"); /*---------------------------------------------------------------------- * Phases *---------------------------------------------------------------------- */ for (int i = 0; i < (int)pSrc->phases.size(); i++) { - struct phase *phase_ptr = phase_store(pSrc->phases[i]->name); + struct phase* phase_ptr = phase_store(pSrc->phases[i]->name); memcpy(phase_ptr, pSrc->phases[i], sizeof(struct phase)); // clean up pointers phase_ptr->formula = string_hsave(pSrc->phases[i]->formula); @@ -1541,26 +1439,14 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) phase_ptr->rxn_s = CReaction_internal_copy(pSrc->phases[i]->rxn_s); phase_ptr->rxn_x = CReaction_internal_copy(pSrc->phases[i]->rxn_x); } - /*---------------------------------------------------------------------- - * Master species - *---------------------------------------------------------------------- */ - /* - master = NULL; - dbg_master = NULL; - count_master = 0; - max_master = MAX_MASTER; - */ + // Master species for (size_t i = 0; i < master.size(); i++) { - master.resize(i + 1); + master.resize(i + 1); master[i] = new struct master; memcpy(master[i], pSrc->master[i], sizeof(struct master)); // clean up pointers - master[i]->gfw_formula = NULL; - if (pSrc->master[i]->gfw_formula != NULL) - { - master[i]->gfw_formula = string_hsave(pSrc->master[i]->gfw_formula); - } + master[i]->gfw_formula = string_hsave(pSrc->master[i]->gfw_formula); master[i]->elt = element_store(pSrc->master[i]->elt->name); master[i]->unknown = NULL; master[i]->s = s_store(pSrc->master[i]->s->name, pSrc->master[i]->s->z, false); @@ -1568,148 +1454,61 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) master[i]->rxn_primary = CReaction_internal_copy(pSrc->master[i]->rxn_primary); master[i]->rxn_secondary = CReaction_internal_copy(pSrc->master[i]->rxn_secondary); } - /*---------------------------------------------------------------------- - * Unknowns - *---------------------------------------------------------------------- */ - /* - x = NULL; - count_unknowns = 0; - max_unknowns = 0; - ah2o_unknown = NULL; - alkalinity_unknown = NULL; - carbon_unknown = NULL; - charge_balance_unknown = NULL; - exchange_unknown = NULL; - mass_hydrogen_unknown = NULL; - mass_oxygen_unknown = NULL; - mb_unknown = NULL; - mu_unknown = NULL; - pe_unknown = NULL; - ph_unknown = NULL; - pure_phase_unknown = NULL; - solution_phase_boundary_unknown = NULL; - surface_unknown = NULL; - gas_unknown = NULL; - ss_unknown = NULL; - */ - // auto gas_unknowns; - /*---------------------------------------------------------------------- - * Reaction work space - *---------------------------------------------------------------------- */ - // struct trxn; - /* - trxn.token = 0; - for (int i = 0; i < MAX_LOG_K_INDICES; i++) - { - trxn.logk[i] = 0; - } - for (int i = 0; i < 3; i++) - { - trxn.dz[i] = 0; - } - count_trxn = 0; - */ - /* ---------------------------------------------------------------------- - * Print - * ---------------------------------------------------------------------- */ - /* - pr.all = TRUE; - pr.initial_solutions = TRUE; - pr.initial_exchangers = TRUE; - pr.reactions = TRUE; - pr.gas_phase = TRUE; - pr.ss_assemblage = TRUE; - pr.pp_assemblage = TRUE; - pr.surface = TRUE; - pr.exchange = TRUE; - pr.kinetics = TRUE; - pr.totals = TRUE; - pr.eh = TRUE; - pr.species = TRUE; - pr.saturation_indices = TRUE; - pr.irrev = TRUE; - pr.mix = TRUE; - pr.reaction = TRUE; - pr.use = TRUE; - pr.logfile = FALSE; - pr.punch = TRUE; - pr.status = TRUE; - pr.inverse = TRUE; - pr.dump = TRUE; - pr.user_print = TRUE; - pr.headings = TRUE; - pr.user_graph = TRUE; - pr.echo_input = TRUE; - pr.warnings = 100; - pr.initial_isotopes = TRUE; - pr.isotope_ratios = TRUE; - pr.isotope_alphas = TRUE; - pr.hdf = FALSE; - pr.alkalinity = FALSE; - */ + // Unknowns will be built + //x = NULL; + //count_unknowns = 0; + //max_unknowns = 0; + //ah2o_unknown = NULL; + //alkalinity_unknown = NULL; + //carbon_unknown = NULL; + //charge_balance_unknown = NULL; + //exchange_unknown = NULL; + //mass_hydrogen_unknown = NULL; + //mass_oxygen_unknown = NULL; + //mb_unknown = NULL; + //mu_unknown = NULL; + //pe_unknown = NULL; + //ph_unknown = NULL; + //pure_phase_unknown = NULL; + //solution_phase_boundary_unknown = NULL; + //surface_unknown = NULL; + //gas_unknown = NULL; + //ss_unknown = NULL; + //gas_unknowns; + //mb_unknowns + // Reaction work space + // struct reaction_temp trxn; + count_trxn = 0; + // Print pr = pSrc->pr; - status_on = pSrc->status_on; - status_interval = pSrc->status_interval; - status_timer = clock(); - count_warnings = 0; - /* ---------------------------------------------------------------------- - * RATES - * ---------------------------------------------------------------------- */ - /* - rates = NULL; - count_rates = 0; - rate_m = 0; - rate_m0 = 0; - rate_time = 0; - rate_kin_time = 1.0; - rate_sim_time_start = 0; - rate_sim_time_end = 0; - rate_sim_time = 0; - rate_moles = 0; - initial_total_time = 0; - */ + status_on = pSrc->status_on; + status_interval = pSrc->status_interval; + status_timer = clock(); + status_string.clear(); + count_warnings = 0; + // RATES + rates = pSrc->rates; + for (size_t i = 0; i < pSrc->rates.size(); i++) + { + rates.push_back(*rate_copy(&pSrc->rates[i])); + } + //rate_m = 0; + //rate_m0 = 0; + //rate_time = 0; + //rate_kin_time = 1.0; + //rate_sim_time_start = 0; + //rate_sim_time_end = 0; + //rate_sim_time = 0; + //rate_moles = 0; initial_total_time = pSrc->initial_total_time; - /* - // auto rate_p - count_rate_p = 0; - */ - for (int i = 0; i < (int)pSrc->rates.size(); i++) - { - size_t count_rates = rates.size(); - rates.resize(count_rates + 1); - rates[i].name = string_hsave(pSrc->rates[i].name); - rates[i].commands = pSrc->rates[i].commands; - rates[i].new_def = TRUE; - rates[i].linebase = NULL; - rates[i].varbase = NULL; - rates[i].loopbase = NULL; - } - /* ---------------------------------------------------------------------- - * USER PRINT COMMANDS - * ---------------------------------------------------------------------- */ - - /* - user_print = NULL; - */ - { - user_print->name = NULL; - user_print->commands = pSrc->user_print->commands; - user_print->new_def = TRUE; - user_print->linebase = NULL; - user_print->varbase = NULL; - user_print->loopbase = NULL; - } - - // For now, User Punch is not copied - n_user_punch_index = pSrc->n_user_punch_index; - fpunchf_user_s_warning = pSrc->fpunchf_user_s_warning; + //rate_p + count_rate_p = 0; + // User print + user_print = rate_copy(pSrc->user_print); + // For now, User Punch is NOT copied + n_user_punch_index = pSrc->n_user_punch_index; + fpunchf_user_s_warning = pSrc->fpunchf_user_s_warning; //fpunchf_user_buffer[0] = 0; - -#if defined PHREEQ98 - struct rate *user_graph; - char **user_graph_headings; - int user_graph_count_headings; -#endif #if defined MULTICHART // auto chart_handler; chart_handler.Set_io(phrq_io); @@ -1717,89 +1516,80 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) /* ---------------------------------------------------------------------- * GLOBAL DECLARATIONS * ---------------------------------------------------------------------- */ - /* - error_string = NULL; - simulation = 0; - */ + error_string = NULL; simulation = pSrc->simulation; - /* - int state = INITIALIZE; - reaction_step = 0; - transport_step = 0; - transport_start = 0; - advection_step = 0; - stop_program = FALSE; - incremental_reactions = FALSE; - */ + //state = INITIALIZE; + //reaction_step = 0; + //transport_step = 0; + //transport_start = 0; + //advection_step = 0; + //stop_program = FALSE; incremental_reactions = pSrc->incremental_reactions; - /* - count_strings = 0; - array = NULL; - delta = NULL; - residual = NULL; - input_error = 0; - next_keyword = Keywords::KEY_NONE; - parse_error = 0; - paren_count = 0; - iterations = 0; - gamma_iterations = 0; - run_reactions_iterations= 0; - max_line = MAX_LINE; - line = NULL; - line_save = NULL; - LOG_10 = LOG_10; - debug_model = FALSE; - debug_prep = FALSE; - debug_set = FALSE; - debug_diffuse_layer = FALSE; - debug_inverse = FALSE; - */ + // Constants + MIN_LM = pSrc->MIN_LM; /* minimum log molality allowed before molality set to zero */ + LOG_ZERO_MOLALITY = pSrc->LOG_ZERO_MOLALITY; /* molalities <= LOG_ZERO_MOLALITY are considered equal to zero */ + MIN_RELATED_LOG_ACTIVITY = pSrc->MIN_RELATED_LOG_ACTIVITY; + MIN_TOTAL = pSrc->MIN_TOTAL; + MIN_TOTAL_SS = pSrc->MIN_TOTAL_SS; + MIN_RELATED_SURFACE = pSrc->MIN_RELATED_SURFACE; + simulation = pSrc->simulation; + //my_array, + //delta, + //residual + input_error = 0; + Keywords::KEYWORDS next_keyword = Keywords::KEY_NONE; + parse_error = 0; + paren_count = 0; + iterations = 0; + gamma_iterations = 0; + run_reactions_iterations = 0; + overall_iterations = 0; + free_check_null(line); + free_check_null(line_save); + max_line = pSrc->max_line; + line = (char*)PHRQ_malloc(max_line * sizeof(char)); + line_save = (char*)PHRQ_malloc(max_line * sizeof(char)); + LOG_10 = pSrc->LOG_10; + // Debug debug_model = pSrc->debug_model; debug_prep = pSrc->debug_prep; debug_set = pSrc->debug_set; debug_diffuse_layer = pSrc->debug_diffuse_layer; debug_inverse = pSrc->debug_inverse; - inv_tol_default = pSrc->inv_tol_default; - itmax = pSrc->itmax; - max_tries = pSrc->max_tries; - ineq_tol = pSrc->ineq_tol; - convergence_tolerance = pSrc->convergence_tolerance; - step_size = pSrc->step_size; - pe_step_size = pSrc->pe_step_size; - step_size_now = step_size; - pe_step_size_now = pe_step_size; - pp_scale = pSrc->pp_scale; - pp_column_scale = pSrc->pp_column_scale; - diagonal_scale = pSrc->diagonal_scale; - mass_water_switch = pSrc->mass_water_switch; - delay_mass_water = pSrc->delay_mass_water; - equi_delay = pSrc->equi_delay; - dampen_ah2o = pSrc->dampen_ah2o; - censor = pSrc->censor; - aqueous_only = pSrc->aqueous_only; + // + inv_tol_default = pSrc->inv_tol_default; + itmax = pSrc->itmax; + max_tries = pSrc->max_tries; + ineq_tol = pSrc->ineq_tol; + convergence_tolerance = pSrc->convergence_tolerance; + step_size = pSrc->step_size; + pe_step_size = pSrc->pe_step_size; + step_size_now = step_size; + pe_step_size_now = pe_step_size; + pp_scale = pSrc->pp_scale; + pp_column_scale = pSrc->pp_column_scale; + diagonal_scale = pSrc->diagonal_scale; + mass_water_switch = pSrc->mass_water_switch; + delay_mass_water = pSrc->delay_mass_water; + equi_delay = pSrc->equi_delay; + dampen_ah2o = pSrc->dampen_ah2o; + censor = pSrc->censor; + aqueous_only = pSrc->aqueous_only; negative_concentrations = pSrc->negative_concentrations; - calculating_deriv = pSrc->calculating_deriv; - numerical_deriv = pSrc->numerical_deriv; - count_total_steps = 0; - phast = FALSE; - output_newline = true; - /* - llnl_temp = 0; - llnl_count_temp = 0; - llnl_adh = 0; - llnl_count_adh = 0; - llnl_bdh = 0; - llnl_count_bdh = 0; - llnl_bdot = 0; - llnl_count_bdot = 0; - llnl_co2_coefs = 0; - llnl_count_co2_coefs = 0; - */ - llnl_temp = pSrc->llnl_temp; - llnl_adh = pSrc->llnl_adh; - llnl_bdh = pSrc->llnl_bdh; - llnl_bdot = pSrc->llnl_bdot; - llnl_co2_coefs = pSrc->llnl_co2_coefs; + calculating_deriv = pSrc->calculating_deriv; + numerical_deriv = pSrc->numerical_deriv; + count_total_steps = 0; + phast = FALSE; + output_newline = true; + // llnl + a_llnl = pSrc->a_llnl; + b_llnl = pSrc->b_llnl; + bdot_llnl = pSrc->bdot_llnl; + llnl_temp = pSrc->llnl_temp; + llnl_adh = pSrc->llnl_adh; + llnl_bdh = pSrc->llnl_bdh; + llnl_bdot = pSrc->llnl_bdot; + llnl_co2_coefs = pSrc->llnl_co2_coefs; // Not implemented for now SelectedOutput_map = pSrc->SelectedOutput_map; @@ -1816,28 +1606,31 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) //SelectedOutput_map.clear(); UserPunch_map = pSrc->UserPunch_map; + std::map::iterator it = UserPunch_map.begin(); + for (; it != UserPunch_map.end(); it++) { - std::map::iterator it = UserPunch_map.begin(); - for (; it != UserPunch_map.end(); it++) - { - struct rate *rate_new = rate_copy(it->second.Get_rate()); - it->second.Set_rate(rate_new); - it->second.Set_PhreeqcPtr(this); - } + struct rate* rate_new = new struct rate; + rate_new = rate_copy(it->second.Get_rate()); + it->second.Set_rate(rate_new); + it->second.Set_PhreeqcPtr(this); } - - //selected_output_file_name = NULL; - //dump_file_name = NULL; - //remove_unstable_phases = FALSE; - // auto screen_string; - spread_length = 10; + remove_unstable_phases = FALSE; + //screen_string; + spread_length = pSrc->spread_length; + //maps set by store below + //std::map strings_map; + //std::map elements_map; + //std::map species_map; + //std::map phases_map; + //std::map logk_map; + //std::map master_isotope_map; /* ---------------------------------------------------------------------- * ISOTOPES * ---------------------------------------------------------------------- */ for (int i = 0; i < (int)pSrc->master_isotope.size(); i++) { - struct master_isotope *master_isotope_ptr = master_isotope_store(pSrc->master_isotope[i]->name, FALSE); + struct master_isotope* master_isotope_ptr = master_isotope_store(pSrc->master_isotope[i]->name, FALSE); memcpy(master_isotope_ptr, pSrc->master_isotope[i], sizeof(struct master_isotope)); master_isotope_ptr->name = string_hsave(pSrc->master_isotope[i]->name); int n; @@ -1862,111 +1655,235 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) } } initial_solution_isotopes = pSrc->initial_solution_isotopes; - + // Calculate values for (int i = 0; i < pSrc->calculate_value.size(); i++) { struct calculate_value* calculate_value_ptr = calculate_value_store(pSrc->calculate_value[i]->name, FALSE); calculate_value_ptr->value = pSrc->calculate_value[i]->value; calculate_value[i]->commands = pSrc->calculate_value[i]->commands; } - + // More isotopes for (int i = 0; i < (int)pSrc->isotope_ratio.size(); i++) { - struct isotope_ratio *isotope_ratio_ptr = isotope_ratio_store(pSrc->isotope_ratio[i]->name, FALSE); + struct isotope_ratio* isotope_ratio_ptr = isotope_ratio_store(pSrc->isotope_ratio[i]->name, FALSE); isotope_ratio_ptr->name = string_hsave(pSrc->isotope_ratio[i]->name); isotope_ratio_ptr->isotope_name = string_hsave(pSrc->isotope_ratio[i]->isotope_name); isotope_ratio_ptr->ratio = pSrc->isotope_ratio[i]->ratio; isotope_ratio_ptr->converted_ratio = pSrc->isotope_ratio[i]->converted_ratio; } - + //std::map isotope_ratio_map; for (int i = 0; i < (int)pSrc->isotope_alpha.size(); i++) { - struct isotope_alpha *isotope_alpha_ptr = isotope_alpha_store(pSrc->isotope_alpha[i]->name, FALSE); + struct isotope_alpha* isotope_alpha_ptr = isotope_alpha_store(pSrc->isotope_alpha[i]->name, FALSE); isotope_alpha_ptr->named_logk = string_hsave(pSrc->isotope_alpha[i]->named_logk); isotope_alpha_ptr->value = pSrc->isotope_alpha[i]->value; } - - phreeqc_mpi_myself = 0; - first_read_input = TRUE; - user_database = pSrc->user_database; + //std::map isotope_alpha_map; + // Misc + phreeqc_mpi_myself = 0; + first_read_input = pSrc->first_read_input; + user_database = pSrc->user_database; //have_punch_name = pSrc->have_punch_name; - print_density = pSrc->print_density; - print_viscosity = pSrc->print_viscosity; + print_density = pSrc->print_density; + print_viscosity = pSrc->print_viscosity; viscos = pSrc->viscos; viscos_0 = pSrc->viscos_0; viscos_0_25 = pSrc->viscos_0_25; // viscosity of the solution, of pure water, of pure water at 25 C - /* Pitzer */ - pitzer_model = pSrc->pitzer_model; - sit_model = pSrc->sit_model; - pitzer_pe = pSrc->pitzer_pe; + cell_pore_volume = pSrc->cell_pore_volume;; + cell_porosity = pSrc->cell_porosity; + cell_volume = pSrc->cell_volume; + cell_saturation = pSrc->cell_saturation; + sys.clear(); + sys_tot = pSrc->sys_tot; + // solution properties + V_solutes = pSrc->V_solutes; + viscos = pSrc->viscos; + viscos_0 = pSrc->viscos_0; + viscos_0_25 = pSrc->viscos_0_25; + rho_0 = pSrc->rho_0; + kappa_0 = pSrc->kappa_0; + p_sat = pSrc->p_sat; + eps_r = pSrc->eps_r; + DH_A = pSrc->DH_A; + DH_B = pSrc->DH_B; + DH_Av = pSrc->DH_Av; + QBrn = pSrc->QBrn; + ZBrn = pSrc->ZBrn; + dgdP = pSrc->dgdP; + // + need_temp_msg = pSrc->need_temp_msg; + solution_mass = pSrc->solution_mass; + solution_volume = pSrc->solution_volume; + s_pTail = NULL; + basic_interpreter = NULL; + /* cl1.cpp ------------------------------- */ + //std::vector x_arg, res_arg, scratch; + // gases.cpp + a_aa_sum = pSrc->a_aa_sum; + b2 = pSrc->b2; + b_sum = pSrc->b_sum; + R_TK = pSrc->R_TK; + /* input.cpp ------------------------------- */ + check_line_return = 0; + reading_db = FALSE; + /* integrate.cpp ------------------------------- */ + midpoint_sv = pSrc->midpoint_sv; + z_global = pSrc->z_global; + xd_global = pSrc->xd_global; + alpha_global = pSrc->alpha_global; + /* inverse.cpp ------------------------------- */ /* integrate.cpp ------------------------------- */ + max_row_count = pSrc->max_row_count; + max_column_count = pSrc->max_column_count; + carbon = pSrc->carbon; + //std::vector col_name, row_name; + count_rows = pSrc->count_rows; + count_optimize = pSrc->count_optimize; + col_phases = pSrc->col_phases; + col_redox = pSrc->col_redox; + col_epsilon = pSrc->col_epsilon; + col_ph = pSrc->col_ph; + col_water = pSrc->col_water; + col_isotopes = pSrc->col_isotopes; + col_phase_isotopes = pSrc->col_phase_isotopes; + row_mb = pSrc->row_mb; + row_fract = pSrc->row_fract; + row_charge = pSrc->row_charge; + row_carbon = pSrc->row_carbon; + row_isotopes = pSrc->row_isotopes; + row_epsilon = pSrc->row_epsilon; + row_isotope_epsilon = pSrc->row_isotope_epsilon; + row_water = pSrc->row_water; + //std::vector inv_zero, array1, inv_res, inv_delta1, delta2, + // delta3, inv_cu, delta_save; + //std::vector min_delta, max_delta; + //std::vector inv_iu, inv_is; + klmd = pSrc->klmd; + nklmd = pSrc->nklmd; + n2d = pSrc->n2d; + kode = pSrc->kode; + iter = pSrc->iter; + toler = pSrc->toler; + error = pSrc->error; + max_pct = pSrc->max_pct; + scaled_error = pSrc->scaled_error; + master_alk = NULL; + //std::vector row_back, col_back; + //std::vector good, bad, minimal; + max_good = pSrc->max_good; + max_bad = pSrc->max_bad; + max_minimal = pSrc->max_minimal; + count_good = pSrc->count_good; + count_bad = pSrc->count_bad; + count_minimal = pSrc->count_minimal; + count_calls = pSrc->count_calls; + soln_bits = pSrc->soln_bits; + phase_bits = pSrc->phase_bits; + current_bits = pSrc->current_bits; + temp_bits = pSrc->temp_bits; + netpath_file = NULL; + count_inverse_models = pSrc->count_inverse_models; + count_pat_solutions = pSrc->count_pat_solutions; + for (int i = 0; i < 32; i++) + { + min_position[i] = pSrc->min_position[i]; + max_position[i] = pSrc->max_position[i]; + now[i] = pSrc->now[i]; + } + //std::vector inverse_heading_names; + /* kinetics.cpp ------------------------------- */ + count_pp = count_pg = count_ss = 0; + cvode_kinetics_ptr = NULL; + cvode_test = FALSE; + cvode_error = FALSE; + cvode_n_user = -99; + cvode_n_reactions = -99; + cvode_step_fraction = 0.0; + cvode_rate_sim_time = 0.0; + cvode_rate_sim_time_start = 0.0; + cvode_last_good_time = 0.0; + cvode_prev_good_time = 0.0; + cvode_last_good_y = NULL; + cvode_prev_good_y = NULL; + kinetics_machEnv = NULL; + kinetics_y = NULL; + kinetics_abstol = NULL; + kinetics_cvode_mem = NULL; + cvode_pp_assemblage_save = NULL; + cvode_ss_assemblage_save = NULL; + //std::vector m_temp, m_original, rk_moles, x0_moles; + set_and_run_attempt = 0; + /* model.cpp ------------------------------- */ + gas_in = FALSE; + min_value = 1e-10; + //std::vector normal, ineq_array, res, cu, zero, delta1; + //std::vector iu, is, back_eq; + /* phrq_io_output.cpp ------------------------------- */ + forward_output_to_log = pSrc->forward_output_to_log; + /* phreeqc_files.cpp ------------------------------- */ + default_data_base = pSrc->default_data_base; + // Pitzer + pitzer_model = pSrc->pitzer_model; + sit_model = pSrc->sit_model; + pitzer_pe = pSrc->pitzer_pe; - //full_pitzer = FALSE; - //always_full_pitzer = FALSE; - //ICON = TRUE; - //IC = -1; - //COSMOT = 0; - //AW = 0; - //VP = 0; - //DW0 = 0; - full_pitzer = pSrc->full_pitzer; - always_full_pitzer = pSrc->always_full_pitzer; - ICON = pSrc->ICON; - IC = pSrc->IC; + full_pitzer = pSrc->full_pitzer; + always_full_pitzer = pSrc->always_full_pitzer; + ICON = pSrc->ICON; + IC = pSrc->IC; + COSMOT = pSrc->COSMOT; + AW = pSrc->AW; + VP = pSrc->VP; + DW0 = pSrc->DW0; for (int i = 0; i < (int)pSrc->pitz_params.size(); i++) { pitz_param_store(pSrc->pitz_params[i], true); } - pitz_param_map = pSrc->pitz_param_map; - // auto pitz_param_map + //pitz_param_map = pSrc->pitz_param_map; created by store for (int i = 0; i < (int)pSrc->theta_params.size(); i++) { size_t count_theta_params = theta_params.size(); theta_params.resize(count_theta_params + 1); - theta_params[count_theta_params] = new struct theta_param; + theta_params[count_theta_params] = new struct theta_param; *theta_params[count_theta_params] = *pSrc->theta_params[i]; } - use_etheta = pSrc->use_etheta; - /* - OTEMP = -100.0; - OPRESS = -100.0; - A0 = 0; - aphi - */ + use_etheta = pSrc->use_etheta; + OTEMP = pSrc->OTEMP; + OPRESS = pSrc->OPRESS; + A0 = pSrc->A0; aphi = pitz_param_copy(pSrc->aphi); - /* - spec = NULL; - cations = NULL; - anions = NULL; - neutrals = NULL; - count_cations = 0; - count_anions = 0; - count_neutrals = 0; - MAXCATIONS = 0; - FIRSTANION = 0; - MAXNEUTRAL = 0; - mcb0 = NULL; - mcb1 = NULL; - mcc0 = NULL; - IPRSNT = NULL; - M = NULL; - LGAMMA = NULL; + // will be rebuilt + spec = pSrc->spec; + cations = pSrc->cations; + anions = pSrc->anions; + neutrals = pSrc->neutrals; + count_cations = pSrc->count_cations; + count_anions = pSrc->count_anions; + count_neutrals = pSrc->count_neutrals; + MAXCATIONS = pSrc->MAXCATIONS; + FIRSTANION = pSrc->FIRSTANION; + MAXNEUTRAL = pSrc->MAXNEUTRAL; + mcb0 = pSrc->mcb0; + mcb1 = pSrc->mcb1; + mcc0 = pSrc->mcc0; + IPRSNT = pSrc->IPRSNT; + M = pSrc->M; + LGAMMA = pSrc->LGAMMA; for (int i = 0; i < 23; i++) { - BK[i] = 0.0; - DK[i] = 0.0; + BK[i] = pSrc->BK[i]; + DK[i] = pSrc->DK[i]; } - */ - dummy = 0; + dummy = 0; /* print.cpp ------------------------------- */ /* sformatf_buffer = (char *) PHRQ_malloc(256 * sizeof(char)); - if (sformatf_buffer == NULL) + if (sformatf_buffer == NULL) malloc_error(); sformatf_buffer_size = 256; */ /* read.cpp */ - prev_next_char = NULL; + prev_next_char = NULL; #if defined PHREEQ98 int shifts_as_points; #endif @@ -1980,24 +1897,28 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) /* readtr.cpp */ // auto dump_file_name_cpp; /* sit.cpp ------------------------------- */ -/* - sit_params = NULL; - count_sit_param = 0; - max_sit_param = 100; - // auto sit_param_map - sit_A0 = 0; - sit_count_cations = 0; - sit_count_anions = 0; - sit_count_neutrals = 0; - sit_MAXCATIONS = 0; - sit_FIRSTANION = 0; - sit_MAXNEUTRAL = 0; - sit_IPRSNT = NULL; - sit_M = NULL; - sit_LGAMMA = NULL; -*/ - sit_params.clear(); - sit_param_map = pSrc->sit_param_map; + for (int i = 0; i < (int)pSrc->sit_params.size(); i++) + { + sit_param_store(pSrc->sit_params[i], true); + } + //sit_param_map = pSrc->sit_param_map; // filled by store + sit_A0 = pSrc->sit_A0; + sit_count_cations = pSrc->sit_count_cations; + sit_count_anions = pSrc->sit_count_anions; + sit_count_neutrals = pSrc->sit_count_neutrals; + sit_MAXCATIONS = pSrc->sit_MAXCATIONS; + sit_FIRSTANION = pSrc->sit_FIRSTANION; + sit_MAXNEUTRAL = pSrc->sit_MAXNEUTRAL; + sit_IPRSNT = pSrc->sit_IPRSNT; + sit_M = pSrc->sit_M; + sit_LGAMMA = pSrc->sit_LGAMMA; + s_list = pSrc->s_list; + cation_list = pSrc->cation_list; + neutral_list = pSrc->neutral_list; + anion_list = pSrc->anion_list; + ion_list = pSrc->ion_list; + param_list = pSrc->param_list; + /* tidy.cpp ------------------------------- */ //a0 = 0; //a1 = 0; @@ -2012,30 +1933,30 @@ Phreeqc::InternalCopy(const Phreeqc *pSrc) /* transport.cpp ------------------------------- */ /* storage is created and freed in tranport.cpp */ - sol_D = NULL; - sol_D_dbg = NULL; - J_ij = NULL; - J_ij_il = NULL; - J_ij_count_spec = pSrc->J_ij_count_spec; - m_s = NULL; - count_m_s = pSrc->count_m_s; - tot1_h = pSrc->tot1_h; - tot1_o = pSrc->tot1_o; - tot2_h = pSrc->tot2_h; - tot2_o = pSrc->tot2_o; - diffc_max = pSrc->diffc_max; - diffc_tr = pSrc->diffc_tr; - J_ij_sum = pSrc->J_ij_sum; - transp_surf = pSrc->transp_surf; - heat_mix_array = NULL; - temp1 = NULL; - temp2 = NULL; - nmix = pSrc->nmix; - heat_nmix = pSrc->heat_nmix; - heat_mix_f_imm = pSrc->heat_mix_f_imm; - heat_mix_f_m = pSrc->heat_mix_f_m; - warn_MCD_X = pSrc->warn_MCD_X; - warn_fixed_Surf = pSrc->warn_fixed_Surf; + sol_D = NULL; + sol_D_dbg = NULL; + J_ij = NULL; + J_ij_il = NULL; + J_ij_count_spec = pSrc->J_ij_count_spec; + m_s = NULL; + count_m_s = pSrc->count_m_s; + tot1_h = pSrc->tot1_h; + tot1_o = pSrc->tot1_o; + tot2_h = pSrc->tot2_h; + tot2_o = pSrc->tot2_o; + diffc_max = pSrc->diffc_max; + diffc_tr = pSrc->diffc_tr; + J_ij_sum = pSrc->J_ij_sum; + transp_surf = pSrc->transp_surf; + heat_mix_array = NULL; + temp1 = NULL; + temp2 = NULL; + nmix = pSrc->nmix; + heat_nmix = pSrc->heat_nmix; + heat_mix_f_imm = pSrc->heat_mix_f_imm; + heat_mix_f_m = pSrc->heat_mix_f_m; + warn_MCD_X = pSrc->warn_MCD_X; + warn_fixed_Surf = pSrc->warn_fixed_Surf; current_x = pSrc->current_x; current_A = pSrc->current_A; fix_current = pSrc->fix_current; diff --git a/Phreeqc.h b/Phreeqc.h index 98fa9cf0..a8331b33 100644 --- a/Phreeqc.h +++ b/Phreeqc.h @@ -849,7 +849,7 @@ public: // struct rate* rate_bsearch(const char* cptr, int* j); int rate_free(struct rate* rate_ptr); - struct rate* rate_copy(struct rate* rate_ptr); + struct rate* rate_copy(const struct rate* rate_ptr); struct rate* rate_search(const char* name, int* n); int rate_sort(void); // diff --git a/advection.cpp b/advection.cpp index 464dd394..86b80744 100644 --- a/advection.cpp +++ b/advection.cpp @@ -4,7 +4,6 @@ #include "cxxKinetics.h" #include "Solution.h" - /* ---------------------------------------------------------------------- */ int Phreeqc:: advection(void) diff --git a/structures.cpp b/structures.cpp index 6355850b..cad26bed 100644 --- a/structures.cpp +++ b/structures.cpp @@ -1379,7 +1379,7 @@ rate_free(struct rate *rate_ptr) /* ---------------------------------------------------------------------- */ struct rate * Phreeqc:: -rate_copy(struct rate *rate_ptr) +rate_copy(const struct rate *rate_ptr) /* ---------------------------------------------------------------------- */ { /* @@ -1388,6 +1388,7 @@ rate_copy(struct rate *rate_ptr) if (rate_ptr == NULL) return (NULL); struct rate* rate_new = new struct rate; + rate_new->name = string_hsave(rate_ptr->name); rate_new->commands = rate_ptr->commands; rate_new->new_def = TRUE; rate_new->linebase = NULL; diff --git a/utilities.cpp b/utilities.cpp index 3b0f8ec5..4e5a28cc 100644 --- a/utilities.cpp +++ b/utilities.cpp @@ -1060,6 +1060,7 @@ string_hsave(const char *str) * Returns: * starting address of saved string (str) */ + if (str == NULL) return (NULL); std::map::const_iterator it; it = strings_map.find(str); if (it != strings_map.end()) From 5d76f82542e2cbdbf1d65c7a6422edd82db1b2f1 Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Tue, 6 Apr 2021 00:20:52 -0600 Subject: [PATCH 52/53] copy operator works well enough --- Phreeqc.cpp | 98 ++++++++++++++++++++----------------------- Phreeqc.h | 14 +++---- basicsubs.cpp | 1 + class_main.cpp | 9 ++-- mainsubs.cpp | 5 +++ pitzer.cpp | 12 +++--- pitzer_structures.cpp | 49 +++++++--------------- sit.cpp | 4 +- structures.cpp | 6 +-- tidy.cpp | 5 +++ 10 files changed, 95 insertions(+), 108 deletions(-) diff --git a/Phreeqc.cpp b/Phreeqc.cpp index 9a908634..e23b3572 100644 --- a/Phreeqc.cpp +++ b/Phreeqc.cpp @@ -411,6 +411,9 @@ size_t Phreeqc::list_Exchangers(std::list &list_exname) } Phreeqc::Phreeqc(PHRQ_io *io) { + //user_print = NULL; + //sformatf_buffer = NULL; + //basic_interpreter = NULL; // phrq_io if (io) { @@ -760,7 +763,7 @@ void Phreeqc::init(void) /* ---------------------------------------------------------------------- * USER PRINT COMMANDS * ---------------------------------------------------------------------- */ - user_print = NULL; + //user_print = NULL; n_user_punch_index = 0; fpunchf_user_s_warning = 0; fpunchf_user_buffer[0] = 0; @@ -884,7 +887,7 @@ void Phreeqc::init(void) /* phqalloc.cpp ------------------------------- */ s_pTail = NULL; /* Basic */ - basic_interpreter = NULL; + //basic_interpreter = NULL; basic_callback_ptr = NULL; basic_callback_cookie = NULL; basic_fortran_callback_ptr = NULL; @@ -1024,7 +1027,6 @@ void Phreeqc::init(void) OTEMP = -100.; OPRESS = -100.; A0 = 0; - aphi = NULL; cations = NULL; anions = NULL; neutrals = NULL; @@ -1044,10 +1046,14 @@ void Phreeqc::init(void) } dummy = 0; /* print.cpp ------------------------------- */ - sformatf_buffer = (char *) PHRQ_malloc(256 * sizeof(char)); + if (sformatf_buffer != NULL) + { + sformatf_buffer = (char*)free_check_null(sformatf_buffer); + } + sformatf_buffer = (char *) PHRQ_calloc(256 , sizeof(char)); if (sformatf_buffer == NULL) malloc_error(); - sformatf_buffer_size = 256; + sformatf_buffer_size = 256; /* read.cpp */ prev_next_char = NULL; #if defined PHREEQ98 @@ -1119,7 +1125,11 @@ void Phreeqc::init(void) /*-----------------------------------------------------*/ Phreeqc::Phreeqc(const Phreeqc &src) { - this->phrq_io = src.phrq_io; + //user_print = NULL; + //sformatf_buffer = NULL; + //basic_interpreter = NULL; + //this->phrq_io = src.phrq_io; + this->phrq_io = &this->ioInstance; this->init(); this->initialize(); InternalCopy(&src); @@ -1128,7 +1138,7 @@ void Phreeqc::InternalCopy(const Phreeqc* pSrc) { // phrq_io - + //this->phrq_io = new PHRQ_io; same_model = FALSE; current_tc = pSrc->current_tc; current_pa = pSrc->current_pa; @@ -1334,43 +1344,21 @@ Phreeqc::InternalCopy(const Phreeqc* pSrc) count_elts = 0; // Reaction run_cells_one_step = pSrc->run_cells_one_step; - // logk - logk.clear(); - for (size_t i = 0; i < pSrc->logk.size(); i++) - { - struct logk* tlk = new struct logk; - *tlk = *pSrc->logk[i]; - tlk->name = string_hsave(logk[i]->name); - logk.push_back(tlk); - } - /*---------------------------------------------------------------------- - * Species - *---------------------------------------------------------------------- */ - /* - logk = NULL; - count_logk = 0; - max_logk = MAX_S; - s = NULL; - count_s = 0; - max_s = MAX_S; - // auto s_diff_layer; - s_x = NULL; - count_s_x = 0; - max_s_x = 0; - s_h2o = NULL; - s_hplus = NULL; - s_h3oplus = NULL; - s_eminus = NULL; - s_co3 = NULL; - s_h2 = NULL; - s_o2 = NULL; - */ - // logk - + //// logk + //logk.clear(); + //for (size_t i = 0; i < pSrc->logk.size(); i++) + //{ + // struct logk* tlk = new struct logk; + // *tlk = *pSrc->logk[i]; + // tlk->name = string_hsave(pSrc->logk[i]->name); + // logk.push_back(tlk); + //} for (int i = 0; i < (int)pSrc->logk.size(); i++) { struct logk* logk_ptr = logk_store(pSrc->logk[i]->name, FALSE); - memcpy(logk_ptr, pSrc->logk[i], sizeof(struct logk)); + //memcpy(logk_ptr, pSrc->logk[i], sizeof(struct logk)); + *logk_ptr = *pSrc->logk[i]; + logk_ptr->name = string_hsave(pSrc->logk[i]->name); logk_ptr->add_logk.resize(pSrc->logk[i]->add_logk.size()); for (size_t j = 0; j < logk_ptr->add_logk.size(); j++) { @@ -1382,8 +1370,10 @@ Phreeqc::InternalCopy(const Phreeqc* pSrc) for (int i = 0; i < (int)pSrc->s.size(); i++) { struct species* s_ptr = s_store(pSrc->s[i]->name, pSrc->s[i]->z, FALSE); - memcpy(s_ptr, pSrc->s[i], sizeof(struct species)); + //memcpy(s_ptr, pSrc->s[i], sizeof(struct species)); + *s_ptr = *pSrc->s[i]; // fix up all pointers + s_ptr->name = string_hsave(pSrc->s[i]->name); s_ptr->mole_balance = NULL; if (pSrc->s[i]->mole_balance != NULL) { @@ -1421,8 +1411,10 @@ Phreeqc::InternalCopy(const Phreeqc* pSrc) for (int i = 0; i < (int)pSrc->phases.size(); i++) { struct phase* phase_ptr = phase_store(pSrc->phases[i]->name); - memcpy(phase_ptr, pSrc->phases[i], sizeof(struct phase)); + //memcpy(phase_ptr, pSrc->phases[i], sizeof(struct phase)); + *phase_ptr = *pSrc->phases[i]; // clean up pointers + phase_ptr->name = string_hsave(pSrc->phases[i]->name); phase_ptr->formula = string_hsave(pSrc->phases[i]->formula); //add_logk phase_ptr->add_logk.resize(pSrc->phases[i]->add_logk.size()); @@ -1440,16 +1432,17 @@ Phreeqc::InternalCopy(const Phreeqc* pSrc) phase_ptr->rxn_x = CReaction_internal_copy(pSrc->phases[i]->rxn_x); } // Master species - for (size_t i = 0; i < master.size(); i++) + for (size_t i = 0; i < pSrc->master.size(); i++) { master.resize(i + 1); master[i] = new struct master; - memcpy(master[i], pSrc->master[i], sizeof(struct master)); + //memcpy(master[i], pSrc->master[i], sizeof(struct master)); + *master[i] = *pSrc->master[i]; // clean up pointers master[i]->gfw_formula = string_hsave(pSrc->master[i]->gfw_formula); master[i]->elt = element_store(pSrc->master[i]->elt->name); master[i]->unknown = NULL; - master[i]->s = s_store(pSrc->master[i]->s->name, pSrc->master[i]->s->z, false); + master[i]->s = s_store(pSrc->master[i]->s->name, pSrc->master[i]->s->z, FALSE); //rxn_primary master[i]->rxn_primary = CReaction_internal_copy(pSrc->master[i]->rxn_primary); master[i]->rxn_secondary = CReaction_internal_copy(pSrc->master[i]->rxn_secondary); @@ -1487,7 +1480,7 @@ Phreeqc::InternalCopy(const Phreeqc* pSrc) status_string.clear(); count_warnings = 0; // RATES - rates = pSrc->rates; + //rates = pSrc->rates; for (size_t i = 0; i < pSrc->rates.size(); i++) { rates.push_back(*rate_copy(&pSrc->rates[i])); @@ -1603,7 +1596,7 @@ Phreeqc::InternalCopy(const Phreeqc* pSrc) it->second.Set_punch_ostream(NULL); } } - //SelectedOutput_map.clear(); + SelectedOutput_map.clear(); UserPunch_map = pSrc->UserPunch_map; std::map::iterator it = UserPunch_map.begin(); @@ -1715,7 +1708,7 @@ Phreeqc::InternalCopy(const Phreeqc* pSrc) solution_mass = pSrc->solution_mass; solution_volume = pSrc->solution_volume; s_pTail = NULL; - basic_interpreter = NULL; + //basic_interpreter = NULL; /* cl1.cpp ------------------------------- */ //std::vector x_arg, res_arg, scratch; // gases.cpp @@ -1837,8 +1830,9 @@ Phreeqc::InternalCopy(const Phreeqc* pSrc) DW0 = pSrc->DW0; for (int i = 0; i < (int)pSrc->pitz_params.size(); i++) { - pitz_param_store(pSrc->pitz_params[i], true); + pitz_param_store(pSrc->pitz_params[i]); } + //pitz_param_map = pSrc->pitz_param_map; created by store for (int i = 0; i < (int)pSrc->theta_params.size(); i++) { @@ -1899,7 +1893,7 @@ Phreeqc::InternalCopy(const Phreeqc* pSrc) /* sit.cpp ------------------------------- */ for (int i = 0; i < (int)pSrc->sit_params.size(); i++) { - sit_param_store(pSrc->sit_params[i], true); + sit_param_store(pSrc->sit_params[i]); } //sit_param_map = pSrc->sit_param_map; // filled by store sit_A0 = pSrc->sit_A0; @@ -1970,7 +1964,7 @@ Phreeqc::InternalCopy(const Phreeqc* pSrc) //} spinner = pSrc->spinner; gfw_map = pSrc->gfw_map; - rates_map = pSrc->rates_map; + //rates_map = pSrc->rates_map; sum_species_map = pSrc->sum_species_map; sum_species_map_db = pSrc->sum_species_map_db; diff --git a/Phreeqc.h b/Phreeqc.h index a8331b33..c5cd1ca6 100644 --- a/Phreeqc.h +++ b/Phreeqc.h @@ -499,8 +499,8 @@ public: // pitzer.cpp ------------------------------- struct pitz_param* pitz_param_read(char* string, int n); - void pitz_param_store(struct pitz_param* pzp_ptr, bool force_copy); - void sit_param_store(struct pitz_param* pzp_ptr, bool force_copy); + void pitz_param_store(const struct pitz_param* pzp_ptr); + void sit_param_store(const struct pitz_param* pzp_ptr); struct pitz_param* pitz_param_copy(const struct pitz_param* src); struct theta_param* theta_param_search(LDBLE zj, LDBLE zk); void pitzer_make_lists(void); @@ -1442,7 +1442,7 @@ protected: /* ---------------------------------------------------------------------- * USER PRINT COMMANDS * ---------------------------------------------------------------------- */ - struct rate* user_print; + struct rate* user_print = 0; int n_user_punch_index; int fpunchf_user_s_warning; @@ -1615,7 +1615,7 @@ protected: PHRQMemHeader* s_pTail; /* Basic */ - PBasic* basic_interpreter; + PBasic* basic_interpreter = NULL; double (*basic_callback_ptr) (double x1, double x2, const char* str, void* cookie); void* basic_callback_cookie; @@ -1716,7 +1716,7 @@ protected: int use_etheta; LDBLE OTEMP, OPRESS; LDBLE A0; - struct pitz_param* aphi; + struct pitz_param* aphi = NULL; std::vector spec; struct species** cations, ** anions, ** neutrals; // pointers to spec int count_cations, count_anions, count_neutrals; @@ -1740,8 +1740,8 @@ protected: dumper dump_info; StorageBinList delete_info; runner run_info; - char* sformatf_buffer; - size_t sformatf_buffer_size; + char* sformatf_buffer = NULL; + size_t sformatf_buffer_size = 0; /* readtr.cpp */ std::string dump_file_name_cpp; diff --git a/basicsubs.cpp b/basicsubs.cpp index 440c0344..3b8a642a 100644 --- a/basicsubs.cpp +++ b/basicsubs.cpp @@ -3998,6 +3998,7 @@ void Phreeqc:: basic_free(void) { delete this->basic_interpreter; + this->basic_interpreter = NULL; } #if defined(SWIG) || defined(SWIG_IPHREEQC) diff --git a/class_main.cpp b/class_main.cpp index 0b42e769..c7e87216 100644 --- a/class_main.cpp +++ b/class_main.cpp @@ -135,11 +135,10 @@ main_method(int argc, char *argv[]) { return errors; } - Phreeqc MyCopy; - MyCopy = *this; + Phreeqc MyCopy = *this; this->clean_up(); - this->init(); - this->initialize(); + //this->init(); + //this->initialize(); /* * Read input data for simulation */ @@ -158,7 +157,7 @@ main_method(int argc, char *argv[]) * Display successful status */ pr.headings = TRUE; - errors = do_status(); + //errors = do_status(); if (errors != 0) { return errors; diff --git a/mainsubs.cpp b/mainsubs.cpp index 38cd8c2f..b2af77d9 100644 --- a/mainsubs.cpp +++ b/mainsubs.cpp @@ -40,6 +40,7 @@ initialize(void) // user_print user_print = new struct rate; + user_print->name = string_hsave("User_print"); user_print->commands.clear(); user_print->linebase = NULL; user_print->varbase = NULL; @@ -49,6 +50,10 @@ initialize(void) */ a_llnl = b_llnl = 0.0; // new PBasic + if (basic_interpreter != NULL) + { + basic_free(); + } basic_interpreter = new PBasic(this, phrq_io); // allocate one change_surf change_surf = diff --git a/pitzer.cpp b/pitzer.cpp index 2cd5827e..a8cc1d5e 100644 --- a/pitzer.cpp +++ b/pitzer.cpp @@ -152,9 +152,7 @@ pitzer_tidy(void) continue; pitz_params[i]->ispec[j] = ISPEC(pitz_params[i]->species[j]); if ((j < 2 && pitz_params[i]->ispec[j] == -1) || - (j == 2 - && (pitz_params[i]->type == TYPE_PSI - || pitz_params[i]->type == TYPE_ZETA) + (j == 2 && (pitz_params[i]->type == TYPE_PSI || pitz_params[i]->type == TYPE_ZETA) && pitz_params[i]->ispec[j] == -1)) { input_error++; @@ -628,7 +626,7 @@ read_pitzer(void) } else { - pitz_param_store(pzp_ptr, false); + pitz_param_store(pzp_ptr); } } break; @@ -1641,7 +1639,11 @@ pitzer_clean_up(void) LGAMMA.clear(); IPRSNT.clear(); spec.clear(); - delete aphi; + if (aphi != NULL) + { + delete aphi; + aphi = NULL; + } M.clear(); return OK; diff --git a/pitzer_structures.cpp b/pitzer_structures.cpp index e3a16c52..0d5c6a2f 100644 --- a/pitzer_structures.cpp +++ b/pitzer_structures.cpp @@ -66,7 +66,7 @@ pitz_param_read(char *string, int n) } /* ---------------------------------------------------------------------- */ void Phreeqc:: -pitz_param_store(struct pitz_param *pzp_ptr, bool force_copy) +pitz_param_store(const struct pitz_param *pzp_ptr) /* ---------------------------------------------------------------------- */ { /* @@ -78,7 +78,7 @@ pitz_param_store(struct pitz_param *pzp_ptr, bool force_copy) return; if (pzp_ptr->type == TYPE_Other) return; - + struct pitz_param* dest = pitz_param_copy(pzp_ptr); std::set< std::string > header; for (i = 0; i < 3; i++) { @@ -108,30 +108,20 @@ pitz_param_store(struct pitz_param *pzp_ptr, bool force_copy) } warning_msg(error_string); delete pitz_params[(*jit).second]; - pitz_params[(*jit).second] = pzp_ptr; + pitz_params[(*jit).second] = dest; } else { - if (force_copy) - { - size_t count_pitz_param = pitz_params.size(); - pitz_params.resize(count_pitz_param + 1); - pitz_params[count_pitz_param] = pitz_param_copy(pzp_ptr); - pitz_param_map[key] = count_pitz_param; - } - else - { - size_t count_pitz_param = pitz_params.size(); - pitz_params.resize(count_pitz_param + 1); - pitz_params[count_pitz_param] = pzp_ptr; - pitz_param_map[key] = count_pitz_param; - } + size_t count_pitz_param = pitz_params.size(); + pitz_params.resize(count_pitz_param + 1); + pitz_params[count_pitz_param] = pitz_param_copy(pzp_ptr); + pitz_param_map[key] = count_pitz_param; } } /* ---------------------------------------------------------------------- */ void Phreeqc:: -sit_param_store(struct pitz_param *pzp_ptr, bool force_copy) +sit_param_store(const struct pitz_param *pzp_ptr) /* ---------------------------------------------------------------------- */ { /* @@ -143,6 +133,7 @@ sit_param_store(struct pitz_param *pzp_ptr, bool force_copy) return; if (pzp_ptr->type == TYPE_Other) return; + struct pitz_param* dest = pitz_param_copy(pzp_ptr); std::set< std::string > header; for (i = 0; i < 3; i++) @@ -174,24 +165,14 @@ sit_param_store(struct pitz_param *pzp_ptr, bool force_copy) } warning_msg(error_string); delete sit_params[(*jit).second]; - sit_params[(*jit).second] = pzp_ptr; + sit_params[(*jit).second] = dest; } else { - if (force_copy) - { - size_t count_sit_param = sit_params.size(); - sit_params.resize(count_sit_param + 1); - sit_params[count_sit_param] = pitz_param_copy(pzp_ptr); - sit_param_map[key] = count_sit_param; - } - else - { - size_t count_sit_param = sit_params.size(); - sit_params.resize(count_sit_param + 1); - sit_params[count_sit_param] = pzp_ptr; - sit_param_map[key] = count_sit_param; - } + size_t count_sit_param = sit_params.size(); + sit_params.resize(count_sit_param + 1); + sit_params[count_sit_param] = dest; + sit_param_map[key] = count_sit_param; } } struct pitz_param* Phreeqc:: @@ -200,7 +181,7 @@ pitz_param_copy(const struct pitz_param* src) if (src == NULL) return NULL; struct pitz_param* dest = new struct pitz_param; *dest = *src; - for (size_t i = 1; i < 3; i++) + for (size_t i = 0; i < 3; i++) { if (src->species[i] != NULL) { diff --git a/sit.cpp b/sit.cpp index 38ce6464..0e89cbde 100644 --- a/sit.cpp +++ b/sit.cpp @@ -211,7 +211,7 @@ read_sit(void) if (pzp_ptr != NULL) { pzp_ptr->type = pzp_type; - sit_param_store(pzp_ptr, false); + sit_param_store(pzp_ptr); } break; case OPTION_ERROR: @@ -503,7 +503,7 @@ sit_clean_up(void) sit_LGAMMA.clear(); sit_IPRSNT.clear(); spec.clear(); - delete aphi; + //delete aphi; sit_M.clear(); return OK; diff --git a/structures.cpp b/structures.cpp index cad26bed..477d1cc7 100644 --- a/structures.cpp +++ b/structures.cpp @@ -146,7 +146,6 @@ clean_up(void) /* user_print and user_punch */ UserPunch_map.clear(); rate_free(user_print); - delete user_print; /* Clear llnl aqueous model parameters */ @@ -281,9 +280,9 @@ CReaction Phreeqc::CReaction_internal_copy(CReaction& rxn_ref) for (size_t i = 0; i < rxn_ref.Get_tokens().size(); i++) { rxn.token[i].s = (rxn_ref.token[i].s == NULL) ? NULL : - s_search(rxn_ref.token[i].s->name); + s_store(rxn_ref.token[i].s->name, rxn_ref.token[i].s->z, false); rxn.token[i].coef = rxn_ref.token[i].coef; - rxn.token[i].name = (rxn_ref.token[i].s == NULL) ? NULL : + rxn.token[i].name = (rxn_ref.token[i].name == NULL) ? NULL : string_hsave(rxn_ref.token[i].name); } return rxn; @@ -519,6 +518,7 @@ elt_list_internal_copy(const std::vector& el) /* ---------------------------------------------------------------------- */ { std::vector new_elt_list; + if (el.size() == 0) return new_elt_list; const struct elt_list* elt_list_ptr = &el[0]; new_elt_list.resize(el.size()); diff --git a/tidy.cpp b/tidy.cpp index 9222b540..a1b8ac11 100644 --- a/tidy.cpp +++ b/tidy.cpp @@ -2813,6 +2813,11 @@ species_rxn_to_trxn(struct species *s_ptr) * Copy reaction from reaction structure to * temp reaction structure. */ + if (trxn.token.size() <= s_ptr->rxn.token.size()) + { + trxn.token.resize(s_ptr->rxn.token.size()); + } + count_trxn = 0; for (size_t i = 0; s_ptr->rxn.token[i].s != NULL; i++) { trxn.token[i].name = s_ptr->rxn.token[i].s->name; From 7961b1686b4e064ec4ec886e76543074d060129f Mon Sep 17 00:00:00 2001 From: David Parkhurst Date: Tue, 6 Apr 2021 09:18:25 -0600 Subject: [PATCH 53/53] release.txt, couple size_t --- PBasic.cpp | 2 +- integrate.cpp | 2 +- readtr.cpp | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/PBasic.cpp b/PBasic.cpp index d854a4cf..8ef4c2f6 100644 --- a/PBasic.cpp +++ b/PBasic.cpp @@ -2408,7 +2408,7 @@ factor(struct LOC_exec * LINK) PhreeqcPtr->sys_tot = 0; //PhreeqcPtr->count_sys = 1000; //int count_sys = PhreeqcPtr->count_sys; - int count_sys = 1000; + size_t count_sys = 1000; names_arg = (char**)PhreeqcPtr->PHRQ_calloc((count_sys + 1), sizeof(char*)); if (names_arg == NULL) { diff --git a/integrate.cpp b/integrate.cpp index 4af3c91b..7318a54c 100644 --- a/integrate.cpp +++ b/integrate.cpp @@ -323,7 +323,7 @@ polint(LDBLE * xa, LDBLE * ya, int n, LDBLE xv, LDBLE * yv, LDBLE * dy) *yv = ya[ns--]; for (m = 1; m < n; m++) { - for (i = 1; i <= n - m; i++) + for (size_t i = 1; i <= n - m; i++) { ho = xa[i] - xv; hp = xa[i + m] - xv; diff --git a/readtr.cpp b/readtr.cpp index 87f82fed..b6d456f1 100644 --- a/readtr.cpp +++ b/readtr.cpp @@ -811,7 +811,7 @@ read_transport(void) "Cell-lengths were read for %d cells. Last value is used till cell %d.", count_length, max_cells); warning_msg(error_string); - for (i = count_length; i <= max_cells; i++) + for (size_t i = count_length; i <= max_cells; i++) cell_data[i + 1].length = length[count_length - 1]; } }