Skip to content
Snippets Groups Projects
Commit 8de78e64 authored by renchao.lu's avatar renchao.lu
Browse files

[CL] Use base class ostream for output; Operator overloading on class templates.

parent 97be8fd7
No related branches found
No related tags found
No related merge requests found
Showing with 120 additions and 123 deletions
...@@ -7,8 +7,6 @@ ...@@ -7,8 +7,6 @@
* *
*/ */
#include <fstream>
#include "BaseLib/ConfigTreeUtil.h" #include "BaseLib/ConfigTreeUtil.h"
#include "BaseLib/Error.h" #include "BaseLib/Error.h"
#include "CreateOutput.h" #include "CreateOutput.h"
......
...@@ -7,69 +7,69 @@ ...@@ -7,69 +7,69 @@
* *
*/ */
#include <fstream> #include <ostream>
#include "Output.h" #include "Output.h"
namespace ChemistryLib namespace ChemistryLib
{ {
std::ofstream& operator<<(std::ofstream& out, std::ostream& operator<<(std::ostream& os,
BasicOutputSetups const& basic_output_setups) BasicOutputSetups const& basic_output_setups)
{ {
out << "-file " << basic_output_setups.output_file << "\n"; os << "-file " << basic_output_setups.output_file << "\n";
out << "-high_precision " << std::boolalpha os << "-high_precision " << std::boolalpha
<< basic_output_setups.use_high_precision << "\n"; << basic_output_setups.use_high_precision << "\n";
out << "-simulation " << std::boolalpha os << "-simulation " << std::boolalpha
<< basic_output_setups.display_simulation_id << "\n"; << basic_output_setups.display_simulation_id << "\n";
out << "-state " << std::boolalpha << basic_output_setups.display_state os << "-state " << std::boolalpha << basic_output_setups.display_state
<< "\n"; << "\n";
out << "-distance " << std::boolalpha os << "-distance " << std::boolalpha << basic_output_setups.display_distance
<< basic_output_setups.display_distance << "\n"; << "\n";
out << "-time " << std::boolalpha os << "-time " << std::boolalpha << basic_output_setups.display_current_time
<< basic_output_setups.display_current_time << "\n"; << "\n";
out << "-step " << std::boolalpha << basic_output_setups.display_time_step os << "-step " << std::boolalpha << basic_output_setups.display_time_step
<< "\n"; << "\n";
return out; return os;
} }
std::ofstream& operator<<(std::ofstream& out, Output const& output) std::ostream& operator<<(std::ostream& os, Output const& output)
{ {
out << output.basic_output_setups; os << output.basic_output_setups;
auto const component_items = auto const component_items =
output.getOutputItemsByItemType(ItemType::Component); output.getOutputItemsByItemType(ItemType::Component);
out << "-totals"; os << "-totals";
for (auto const& component_item : component_items) for (auto const& component_item : component_items)
{ {
out << " " << component_item.name; os << " " << component_item.name;
} }
out << "\n"; os << "\n";
auto const equilibrium_phase_items = auto const equilibrium_phase_items =
output.getOutputItemsByItemType(ItemType::EquilibriumPhase); output.getOutputItemsByItemType(ItemType::EquilibriumPhase);
if (!equilibrium_phase_items.empty()) if (!equilibrium_phase_items.empty())
{ {
out << "-equilibrium_phases"; os << "-equilibrium_phases";
for (auto const& equilibrium_phase_item : equilibrium_phase_items) for (auto const& equilibrium_phase_item : equilibrium_phase_items)
{ {
out << " " << equilibrium_phase_item.name; os << " " << equilibrium_phase_item.name;
} }
out << "\n"; os << "\n";
} }
auto const kinetic_reactant_items = auto const kinetic_reactant_items =
output.getOutputItemsByItemType(ItemType::KineticReactant); output.getOutputItemsByItemType(ItemType::KineticReactant);
if (!kinetic_reactant_items.empty()) if (!kinetic_reactant_items.empty())
{ {
out << "-kinetic_reactants"; os << "-kinetic_reactants";
for (auto const& kinetic_reactant_item : kinetic_reactant_items) for (auto const& kinetic_reactant_item : kinetic_reactant_items)
{ {
out << " " << kinetic_reactant_item.name; os << " " << kinetic_reactant_item.name;
} }
out << "\n"; os << "\n";
} }
return out; return os;
} }
} // namespace ChemistryLib } // namespace ChemistryLib
...@@ -37,8 +37,8 @@ public: ...@@ -37,8 +37,8 @@ public:
display_distance + display_current_time + display_time_step; display_distance + display_current_time + display_time_step;
} }
friend std::ofstream& operator<<( friend std::ostream& operator<<(
std::ofstream& out, BasicOutputSetups const& basic_output_setups); std::ostream& out, BasicOutputSetups const& basic_output_setups);
std::string const output_file; std::string const output_file;
...@@ -97,7 +97,7 @@ struct Output ...@@ -97,7 +97,7 @@ struct Output
return matching_items; return matching_items;
} }
friend std::ofstream& operator<<(std::ofstream& out, Output const& output); friend std::ostream& operator<<(std::ostream& os, Output const& output);
BasicOutputSetups const basic_output_setups; BasicOutputSetups const basic_output_setups;
std::vector<OutputItem> const accepted_items; std::vector<OutputItem> const accepted_items;
......
...@@ -9,7 +9,7 @@ ...@@ -9,7 +9,7 @@
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>
#include <cmath> #include <cmath>
#include <fstream> #include <iostream>
#include "BaseLib/Algorithm.h" #include "BaseLib/Algorithm.h"
#include "BaseLib/ConfigTreeUtil.h" #include "BaseLib/ConfigTreeUtil.h"
...@@ -19,6 +19,18 @@ ...@@ -19,6 +19,18 @@
namespace ChemistryLib namespace ChemistryLib
{ {
namespace
{
template <typename DataBlock>
std::ostream& operator<<(std::ostream& os,
std::vector<DataBlock> const& data_blocks)
{
std::copy(data_blocks.begin(), data_blocks.end(),
std::ostream_iterator<DataBlock>(os));
return os;
}
} // namespace
void PhreeqcIO::doWaterChemistryCalculation( void PhreeqcIO::doWaterChemistryCalculation(
std::vector<GlobalVector*>& process_solutions, double const dt) std::vector<GlobalVector*>& process_solutions, double const dt)
{ {
...@@ -134,16 +146,16 @@ void PhreeqcIO::writeInputsToFile() ...@@ -134,16 +146,16 @@ void PhreeqcIO::writeInputsToFile()
out.close(); out.close();
} }
std::ofstream& operator<<(std::ofstream& out, PhreeqcIO const& phreeqc_io) std::ostream& operator<<(std::ostream& os, PhreeqcIO const& phreeqc_io)
{ {
out << "SELECTED_OUTPUT" << "\n"; os << "SELECTED_OUTPUT" << "\n";
out << *phreeqc_io._output << "\n"; os << *phreeqc_io._output << "\n";
auto const& reaction_rates = phreeqc_io._reaction_rates; auto const& reaction_rates = phreeqc_io._reaction_rates;
if (!reaction_rates.empty()) if (!reaction_rates.empty())
{ {
out << "RATES" << "\n"; os << "RATES" << "\n";
out << reaction_rates << "\n"; os << reaction_rates << "\n";
} }
std::size_t const num_chemical_systems = std::size_t const num_chemical_systems =
...@@ -154,30 +166,30 @@ std::ofstream& operator<<(std::ofstream& out, PhreeqcIO const& phreeqc_io) ...@@ -154,30 +166,30 @@ std::ofstream& operator<<(std::ofstream& out, PhreeqcIO const& phreeqc_io)
{ {
auto const& aqueous_solution = auto const& aqueous_solution =
phreeqc_io._aqueous_solutions[chemical_system_id]; phreeqc_io._aqueous_solutions[chemical_system_id];
out << "SOLUTION " << chemical_system_id + 1 << "\n"; os << "SOLUTION " << chemical_system_id + 1 << "\n";
out << aqueous_solution << "\n"; os << aqueous_solution << "\n";
auto const& equilibrium_phases = auto const& equilibrium_phases =
phreeqc_io._equilibrium_phases[chemical_system_id]; phreeqc_io._equilibrium_phases[chemical_system_id];
if (!equilibrium_phases.empty()) if (!equilibrium_phases.empty())
{ {
out << "EQUILIBRIUM_PHASES " << chemical_system_id + 1 << "\n"; os << "EQUILIBRIUM_PHASES " << chemical_system_id + 1 << "\n";
out << equilibrium_phases << "\n"; os << equilibrium_phases << "\n";
} }
auto const& kinetic_reactants = auto const& kinetic_reactants =
phreeqc_io._kinetic_reactants[chemical_system_id]; phreeqc_io._kinetic_reactants[chemical_system_id];
if (!kinetic_reactants.empty()) if (!kinetic_reactants.empty())
{ {
out << "KINETICS " << chemical_system_id + 1 << "\n"; os << "KINETICS " << chemical_system_id + 1 << "\n";
out << kinetic_reactants; os << kinetic_reactants;
out << "-steps " << phreeqc_io._dt << "\n" << "\n"; os << "-steps " << phreeqc_io._dt << "\n" << "\n";
} }
out << "END" << "\n" << "\n"; os << "END" << "\n" << "\n";
} }
return out; return os;
} }
void PhreeqcIO::execute() void PhreeqcIO::execute()
...@@ -231,7 +243,7 @@ void PhreeqcIO::readOutputsFromFile() ...@@ -231,7 +243,7 @@ void PhreeqcIO::readOutputsFromFile()
in.close(); in.close();
} }
std::ifstream& operator>>(std::ifstream& in, PhreeqcIO& phreeqc_io) std::istream& operator>>(std::istream& in, PhreeqcIO& phreeqc_io)
{ {
// Skip the headline // Skip the headline
in.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); in.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
......
...@@ -64,10 +64,10 @@ public: ...@@ -64,10 +64,10 @@ public:
void readOutputsFromFile(); void readOutputsFromFile();
friend std::ofstream& operator<<(std::ofstream& out, friend std::ostream& operator<<(std::ostream& os,
PhreeqcIO const& phreeqc_io); PhreeqcIO const& phreeqc_io);
friend std::ifstream& operator>>(std::ifstream& in, PhreeqcIO& phreeqc_io); friend std::istream& operator>>(std::istream& in, PhreeqcIO& phreeqc_io);
std::string const _phreeqc_input_file; std::string const _phreeqc_input_file;
......
...@@ -7,44 +7,44 @@ ...@@ -7,44 +7,44 @@
* *
*/ */
#include <fstream> #include <ostream>
#include "AqueousSolution.h" #include "AqueousSolution.h"
namespace ChemistryLib namespace ChemistryLib
{ {
std::ofstream& operator<<(std::ofstream& out, std::ostream& operator<<(std::ostream& os,
AqueousSolution const& aqueous_solution) AqueousSolution const& aqueous_solution)
{ {
out << "temp " << aqueous_solution.temperature << "\n"; os << "temp " << aqueous_solution.temperature << "\n";
out << "pressure " << aqueous_solution.pressure << "\n"; os << "pressure " << aqueous_solution.pressure << "\n";
switch (aqueous_solution.means_of_adjusting_charge) switch (aqueous_solution.means_of_adjusting_charge)
{ {
case MeansOfAdjustingCharge::pH: case MeansOfAdjustingCharge::pH:
out << "pH " << aqueous_solution.pH << " charge" os << "pH " << aqueous_solution.pH << " charge"
<< "\n"; << "\n";
out << "pe " << aqueous_solution.pe << "\n"; os << "pe " << aqueous_solution.pe << "\n";
break; break;
case MeansOfAdjustingCharge::pe: case MeansOfAdjustingCharge::pe:
out << "pH " << aqueous_solution.pH << "\n"; os << "pH " << aqueous_solution.pH << "\n";
out << "pe " << aqueous_solution.pe << " charge" os << "pe " << aqueous_solution.pe << " charge"
<< "\n"; << "\n";
break; break;
case MeansOfAdjustingCharge::Unspecified: case MeansOfAdjustingCharge::Unspecified:
out << "pH " << aqueous_solution.pH << "\n"; os << "pH " << aqueous_solution.pH << "\n";
out << "pe " << aqueous_solution.pe << "\n"; os << "pe " << aqueous_solution.pe << "\n";
break; break;
} }
out << "units mol/kgw\n"; os << "units mol/kgw\n";
for (auto const& component : aqueous_solution.components) for (auto const& component : aqueous_solution.components)
{ {
out << component.name << " " << component.amount << "\n"; os << component.name << " " << component.amount << "\n";
} }
return out; return os;
} }
} // namespace ChemistryLib } // namespace ChemistryLib
...@@ -48,8 +48,8 @@ struct AqueousSolution ...@@ -48,8 +48,8 @@ struct AqueousSolution
{ {
} }
friend std::ofstream& operator<<(std::ofstream& out, friend std::ostream& operator<<(std::ostream& os,
AqueousSolution const& aqueous_solution); AqueousSolution const& aqueous_solution);
double temperature; double temperature;
double pressure; double pressure;
......
...@@ -7,24 +7,21 @@ ...@@ -7,24 +7,21 @@
* *
*/ */
#include <fstream> #include <ostream>
#include "EquilibriumPhase.h" #include "EquilibriumPhase.h"
namespace ChemistryLib namespace ChemistryLib
{ {
std::ofstream& operator<<( std::ostream& operator<<(std::ostream& os,
std::ofstream& out, std::vector<EquilibriumPhase> const& equilibrium_phases) EquilibriumPhase const& equilibrium_phase)
{ {
for (auto const& equilibrium_phase : equilibrium_phases) os << equilibrium_phase.name;
{
out << equilibrium_phase.name;
out << " " << equilibrium_phase.saturation_index; os << " " << equilibrium_phase.saturation_index;
out << " " << equilibrium_phase.amount << "\n"; os << " " << equilibrium_phase.amount << "\n";
}
return out; return os;
} }
} // namespace ChemistryLib } // namespace ChemistryLib
...@@ -33,9 +33,8 @@ struct EquilibriumPhase ...@@ -33,9 +33,8 @@ struct EquilibriumPhase
{ {
} }
friend std::ofstream& operator<<( friend std::ostream& operator<<(std::ostream& os,
std::ofstream& out, EquilibriumPhase const& equilibrium_phase);
std::vector<EquilibriumPhase> const& equilibrium_phases);
std::string const name; std::string const name;
double amount; double amount;
......
...@@ -7,37 +7,34 @@ ...@@ -7,37 +7,34 @@
* *
*/ */
#include <fstream> #include <ostream>
#include "KineticReactant.h" #include "KineticReactant.h"
namespace ChemistryLib namespace ChemistryLib
{ {
std::ofstream& operator<<(std::ofstream& out, std::ostream& operator<<(std::ostream& os,
std::vector<KineticReactant> const& kinetic_reactants) KineticReactant const& kinetic_reactant)
{ {
for (auto const& kinetic_reactant : kinetic_reactants) os << kinetic_reactant.name << "\n";
{
out << kinetic_reactant.name << "\n";
if (!kinetic_reactant.chemical_formula.empty()) if (!kinetic_reactant.chemical_formula.empty())
{ {
out << "-formula " << kinetic_reactant.chemical_formula << "\n"; os << "-formula " << kinetic_reactant.chemical_formula << "\n";
} }
out << "-m " << kinetic_reactant.amount << "\n"; os << "-m " << kinetic_reactant.amount << "\n";
if (!kinetic_reactant.parameters.empty()) if (!kinetic_reactant.parameters.empty())
{
os << "-parms";
for (auto const& parameter : kinetic_reactant.parameters)
{ {
out << "-parms"; os << " " << parameter;
for (auto const& parameter : kinetic_reactant.parameters)
{
out << " " << parameter;
}
out << "\n";
} }
os << "\n";
} }
return out; return os;
} }
} // namespace ChemistryLib } // namespace ChemistryLib
...@@ -31,9 +31,8 @@ struct KineticReactant ...@@ -31,9 +31,8 @@ struct KineticReactant
{ {
} }
friend std::ofstream& operator<<( friend std::ostream& operator<<(std::ostream& os,
std::ofstream& out, KineticReactant const& kinetic_reactant);
std::vector<KineticReactant> const& kinetic_reactants);
std::string const name; std::string const name;
std::string const chemical_formula; std::string const chemical_formula;
......
...@@ -7,29 +7,24 @@ ...@@ -7,29 +7,24 @@
* *
*/ */
#include <fstream> #include <ostream>
#include "ReactionRate.h" #include "ReactionRate.h"
namespace ChemistryLib namespace ChemistryLib
{ {
std::ofstream& operator<<(std::ofstream& out, std::ostream& operator<<(std::ostream& os, ReactionRate const& reaction_rate)
std::vector<ReactionRate> const& reaction_rates)
{ {
for (auto const& reaction_rate : reaction_rates) os << reaction_rate.kinetic_reactant << "\n";
os << "-start" << "\n";
int line_number = 1;
for (auto const& expression_statement : reaction_rate.expression_statements)
{ {
out << reaction_rate.kinetic_reactant << "\n"; os << line_number << " " << expression_statement << "\n";
out << "-start" << "\n"; ++line_number;
int line_number = 1;
for (auto const& expression_statement :
reaction_rate.expression_statements)
{
out << line_number << " " << expression_statement << "\n";
++line_number;
}
out << "-end" << "\n";
} }
os << "-end" << "\n";
return out; return os;
} }
} // namespace ChemistryLib } // namespace ChemistryLib
...@@ -25,8 +25,8 @@ struct ReactionRate ...@@ -25,8 +25,8 @@ struct ReactionRate
{ {
} }
friend std::ofstream& operator<<( friend std::ostream& operator<<(std::ostream& os,
std::ofstream& out, std::vector<ReactionRate> const& reaction_rate); ReactionRate const& reaction_rate);
std::string const kinetic_reactant; std::string const kinetic_reactant;
std::vector<std::string> const expression_statements; std::vector<std::string> const expression_statements;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment