Skip to content

Commit

Permalink
Database records can be changed by reference (C++, Python) (#70)
Browse files Browse the repository at this point in the history
* removed setuptools and version of chemicalfun

* Fixed refresh all_elements data after setElement, addElement

* Added reference functions to C API

* Added trace output functionality for the database API

* Added new functionality to the python API

* Implemented test script of new functions into the Python API
---------

Co-authored-by: svetad <sv.dmytr@gmail.com>
Co-authored-by: G. Dan Miron <dan.miron@psi.com>
Co-authored-by: Allan Leal <allanmulin@gmail.com>
  • Loading branch information
4 people authored Jul 23, 2024
1 parent 151c04a commit c5e692c
Show file tree
Hide file tree
Showing 19 changed files with 1,607 additions and 31 deletions.
142 changes: 118 additions & 24 deletions ThermoFun/Database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,14 @@ struct Database::Impl
{
//jsonio::FJson file (filename);
//type_ = file.Type();
fromFile( filename );
if (elements_map.size()>0)
setDBElements( elements_map );
fromFile(filename);
setDBElements(elements_map);
}

Impl(std::vector<std::string> jsons, std::string _label)
{
fromJSONs(jsons, _label);
if (elements_map.size()>0)
setDBElements( elements_map );
setDBElements(elements_map);
}

template<typename Key, typename Value>
Expand All @@ -68,15 +66,18 @@ struct Database::Impl
return collection;
}

auto setDBElements(ElementsMap elements ) -> void
auto setDBElement(Element& element) -> void
{
thfun_logger->debug("Database::setDBElements() elements {}", elements.size());

ChemicalFun::ElementValues eldata;
for (auto& e : elements)
{
auto elkey = e.second.toElementKey(eldata);
all_elements.addElement(elkey, eldata);
auto elkey = element.toElementKey(eldata);
all_elements.addElement(elkey, eldata);
}

auto setDBElements(ElementsMap elements) -> void
{
thfun_logger->debug("Database::setDBElements() elements {}", elements.size());
for (auto& e : elements) {
setDBElement(e.second);
}
}

Expand All @@ -91,12 +92,14 @@ struct Database::Impl
auto addElement(const Element& element) -> void
{
elements_map.insert({element.symbol(), element});
setDBElement(elements_map[element.symbol()]);
}

auto setElement(const Element& element) -> void
{
checkIfSymbolExists(elements_map, "element", element.symbol());
elements_map[element.symbol()] = element;
setDBElement(elements_map[element.symbol()]);
}

auto addSubstance(const Substance& substance) -> void
Expand All @@ -113,6 +116,7 @@ struct Database::Impl
auto addMapElements(const ElementsMap& elements) -> void
{
elements_map = elements;
setDBElements(elements_map);
}

auto addMapSubstances(const SubstancesMap& substances) -> void
Expand Down Expand Up @@ -166,28 +170,70 @@ struct Database::Impl
return reactions_map.size();
}

auto getElement(std::string symbol) -> Element&
auto getElement(std::string symbol) -> const Element&
{
if(elements_map.count(symbol) == 0)
if(!containsElement(symbol)) {
errorNonExistent("element", symbol, __LINE__);

return elements_map.find(symbol)->second;
}
return elements_map[symbol];
}

auto getSubstance(std::string symbol) -> Substance&
auto getSubstance(std::string symbol) -> const Substance&
{
if(substances_map.count(symbol) == 0)
if(!containsSubstance(symbol)) {
errorNonExistent("substance", symbol, __LINE__);

return substances_map.find(symbol)->second;
}
return substances_map[symbol];
}

auto getReaction(std::string symbol) -> Reaction&
auto getReaction(std::string symbol) -> const Reaction&
{
if(reactions_map.count(symbol) == 0)
if(!containsReaction(symbol)) {
errorNonExistent("reaction", symbol, __LINE__);
}
return reactions_map[symbol];
}

auto element(std::string symbol) -> Element&
{
if(!containsElement(symbol)) { // try restore data from defaults
auto el_key =ChemicalFun::ElementKey(symbol,0);
Element empty_element;
if( all_elements.elements().find(el_key) != all_elements.elements().end()) {
empty_element = elementKeyToElement(el_key);
}
else {
empty_element.setSymbol(symbol);
empty_element.setName(symbol);
}
elements_map[symbol] = empty_element;
}
return elements_map[symbol];
}

return reactions_map.at(symbol);
auto substance(std::string symbol) -> Substance&
{
if(!containsSubstance(symbol)) {
Substance empty_subst;
empty_subst.setSymbol(symbol);
empty_subst.setName(symbol);
// ... set other default data
substances_map[symbol]=empty_subst;
}
return substances_map[symbol];
}

auto reaction(std::string symbol) -> Reaction&
{
if(!containsReaction(symbol))
{
Reaction empty_react;
empty_react.setSymbol(symbol);
empty_react.setName(symbol);
// ... set other default data
reactions_map[symbol]=empty_react;
}
return reactions_map[symbol];
}

auto mapElements() -> ElementsMap&
Expand Down Expand Up @@ -466,6 +512,22 @@ auto Database::getReaction(std::string symbol) const -> const Reaction&
return pimpl->getReaction(symbol);
}

auto Database::element(std::string symbol) -> Element&
{
return pimpl->element(symbol);
}

auto Database::substance(std::string symbol) -> Substance&
{
return pimpl->substance(symbol);
}

auto Database::reaction(std::string symbol) -> Reaction&
{
return pimpl->reaction(symbol);
}


auto Database::mapElements() const -> const ElementsMap&
{
return pimpl->mapElements();
Expand Down Expand Up @@ -496,6 +558,38 @@ auto Database::getReactions() const -> std::vector<Reaction>
return pimpl->getReactions();
}

// Return all elements in the database
auto Database::getElementsList() const -> std::vector<std::string>
{
std::vector<std::string> list;
for(const auto& item: pimpl->elements_map) {
list.push_back(item.first);
}
return list;
}

// Return all substances in the database
auto Database::getSubstancesList() const -> std::vector<std::string>
{
std::vector<std::string> list;
for(const auto& item: pimpl->substances_map) {
list.push_back(item.first);
}
return list;
}

// Return all reactions in the database
auto Database::getReactionsList() const -> std::vector<std::string>
{
std::vector<std::string> list;
for(const auto& item: pimpl->reactions_map) {
list.push_back(item.first);
}
return list;
}



auto Database::numberOfElements() const -> size_t
{
return pimpl->numberOfElements();
Expand Down Expand Up @@ -531,7 +625,7 @@ auto Database::parseSubstanceFormula(std::string formula_) const -> std::map<Ele
std::map<Element, double> map;
ChemicalFun::FormulaToken formula(formula_);
// ??? Do we need props, do not save
auto props = formula.properties(pimpl->all_elements.elements());
//auto props = formula.properties(pimpl->all_elements.elements());

for (const auto& element : formula.getStoichCoefficients())
{
Expand Down
18 changes: 18 additions & 0 deletions ThermoFun/Database.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@ class Database
/// Return all reactions in the database
auto getReactions() const -> std::vector<Reaction>;

/// Return list of all elements in the database
auto getElementsList() const -> std::vector<std::string>;

/// Return list of all substances in the database
auto getSubstancesList() const -> std::vector<std::string>;

/// Return list og all reactions in the database
auto getReactionsList() const -> std::vector<std::string>;

/// Returns the map of elements in the database
auto mapElements() const -> const ElementsMap&;

Expand Down Expand Up @@ -166,6 +175,15 @@ class Database
/// Return a reactions in the database
auto getReaction(std::string symbol) const -> const Reaction&;

/// Reference to the element in the database
auto element(std::string symbol) -> Element&;

/// Reference to the substance in the database
auto substance(std::string symbol) -> Substance&;

/// Reference to the reaction in the database
auto reaction(std::string symbol) -> Reaction&;

/// Check if the database contains a given element
/// @param symbol The name of the element
auto containsElement(std::string symbol) const -> bool;
Expand Down
21 changes: 20 additions & 1 deletion ThermoFun/Element.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <iostream>
#include "Element.h"
#include "ChemicalFun/FormulaParser/ChemicalData.h"
#include "Common/ParseJsonToData.h"
Expand Down Expand Up @@ -206,7 +207,7 @@ ChemicalFun::ElementKey Element::toElementKey(ChemicalFun::ElementValues &eldata
eldata.number = number();
eldata.name = symbol(); // was e.name();

return ChemicalFun::ElementKey(symbol(), class_(), isotopeMass() );
return ChemicalFun::ElementKey(symbol(), class_(), isotopeMass());
}

auto operator<(const Element& lhs, const Element& rhs) -> bool
Expand Down Expand Up @@ -265,4 +266,22 @@ auto operator==(const Element& lhs, const Element& rhs) -> bool
(lhs.isotopeMass() == rhs.isotopeMass());
}


auto operator<<(std::ostream& stream, const Element& element) -> std::ostream&
{
stream << "Element(\n";
stream << " name: " << element.name() << "\n";
stream << " symbol: " << element.symbol() << "\n";
stream << " molarMass: " << element.molarMass() << "\n";
stream << " entropy: " << element.entropy() << "\n";
stream << " heatCapacity: " << element.heatCapacity() << "\n";
stream << " volume: " << element.volume() << "\n";
stream << " valence: " << element.valence() << "\n";
stream << " class: " << element.class_() << "\n";
stream << " isotopeMass: " << element.isotopeMass() << "\n";
stream << " number: " << element.number() << "\n)" << std::endl;
return stream;
}


} // namespace ThermoFun
2 changes: 2 additions & 0 deletions ThermoFun/Element.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ auto operator<(const Element& lhs, const Element& rhs) -> bool;
/// Compare two Element instances for equality
auto operator==(const Element& lhs, const Element& rhs) -> bool;

auto operator<<(std::ostream& stream, const Element& element) -> std::ostream&;

} // namespace ThermoFun

#endif // ELEMENT_H
12 changes: 12 additions & 0 deletions ThermoFun/GlobalVariables.h
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,18 @@ static const char * reacFMcoeff = "dr_marshall_franck_coeffs.values";

//typedef std::vector<struct ReactionData> Reactions;

/// Output a array instance
template<typename V>
auto operator<<(std::ostream& out, const std::vector<V>& array) -> std::ostream&
{
out << std::string("[ ");
for(const auto& val: array) {
out << val << " ";
}
out << std::string("]");
return out;
}

}
///@endcond

Expand Down
66 changes: 66 additions & 0 deletions ThermoFun/Reaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,73 @@ auto Reaction::calc_logK_fT_coefficients() -> vd
// setThermoParameters(th_param);
}

auto operator<<(std::ostream& stream, const ThermoPropertiesReaction& data) -> std::ostream&
{
stream << "( " << data.ln_equilibrium_constant << ", " << data.log_equilibrium_constant << ", "
<< data.reaction_gibbs_energy << ", "<< data.reaction_helmholtz_energy << ", "
<< data.reaction_internal_energy << ", "<< data.reaction_enthalpy << ", "
<< data.reaction_entropy << ", "<< data.reaction_volume << ", "
<< data.reaction_heat_capacity_cp << ", "<< data.reaction_heat_capacity_cv << " )";
return stream;
}

auto operator<<(std::ostream& stream, const ThermoParametersReaction& data) -> std::ostream&
{
if( !data.temperature_intervals.empty() ) {
stream << " temperature_intervals: " << data.temperature_intervals << "\n";
}
if( !data.pressure_intervals.empty() ) {
stream << " pressure_intervals: " << data.pressure_intervals << "\n";
}
if( !data.reaction_logK_fT_coeff.empty() ) {
stream << " reaction_logK_fT_coeff: " << data.reaction_logK_fT_coeff << "\n";
}
if( !data.logK_TP_array.empty() ) {
stream << " logK_TP_array: " << data.logK_TP_array << "\n";
}
if( !data.reaction_Cp_fT_coeff.empty() ) {
stream << " reaction_Cp_fT_coeff: " << data.reaction_Cp_fT_coeff << "\n";
}
if( !data.reaction_V_fT_coeff.empty() ) {
stream << " reaction_V_fT_coeff: " << data.reaction_V_fT_coeff << "\n";
}
if( !data.reaction_RB_coeff.empty() ) {
stream << " reaction_RB_coeff: " << data.reaction_RB_coeff << "\n";
}
if( !data.reaction_RB_coeff.empty() ) {
stream << " reaction_RB_coeff: " << data.reaction_RB_coeff << "\n";
}
if( !data.reaction_FM_coeff.empty() ) {
stream << " reaction_FM_coeff: " << data.reaction_FM_coeff << "\n";
}
if( !data.reaction_DM10_coeff.empty() ) {
stream << " reaction_DM10_coeff: " << data.reaction_DM10_coeff << "\n";
}
return stream;
}

auto operator<<(std::ostream& stream, const Reaction& react) -> std::ostream&
{
stream << "Reaction(\n";
stream << " name: " << react.name() << "\n";
stream << " symbol: " << react.symbol() << "\n";
stream << " equation: " << react.equation() << "\n";
stream << " reactants: ";
stream << std::string("[ ");
for(const auto& val: react.reactants()) {
stream << "( " << val.first << " : " << val.second << " ) ";
}
stream << std::string("]\n");
stream << " thermoReferenceProperties: " << react.thermoReferenceProperties() << "\n";
stream << " thermo_ref_prop: " << react.thermo_ref_prop() << "\n";
stream << " thermoParameters: \n" << react.thermoParameters();
stream << " T: ( " << react.referenceT() << ", " << react.lowerT() << ", " << react.upperT() << " )\n";
stream << " P: ( " << react.referenceP() << ", " << react.lowerP() << ", " << react.upperP() << " )\n";
stream << " methodGenEOS: " << react.methodGenEOS() << "\n";
stream << " method_T: " << react.method_T() << "\n";
stream << " method_P: " << react.method_P() << "\n)" << std::endl;
return stream;
}

} // namespace ThermoFun

Loading

0 comments on commit c5e692c

Please sign in to comment.