diff --git a/src/database/common_def.hpp b/src/database/common_def.hpp index b9c7ade..210b66a 100644 --- a/src/database/common_def.hpp +++ b/src/database/common_def.hpp @@ -1,38 +1,74 @@ /*------------------------------------------------------- - Module : database - File : common_def.hpp - Author : Fabien Georget Copyright (c) 2014, Fabien Georget, Princeton University ---------------------------------------------------------*/ #ifndef SPECMICP_DATABASE_COMMONDEF_HPP #define SPECMICP_DATABASE_COMMONDEF_HPP #include #include +#include //! \file common_def.hpp common type definitions namespace specmicp { namespace database { //! ID of a species using id_species_t = unsigned int; //! Vector containing a list of species (by ID) using list_species_t = std::vector; //! Vector containing each strings using vector_labels_t = std::vector; //! Stoechiometric coefficients for the reactions using reaction_mat_t = Eigen::Matrix; //! Vector containing logK values using logK_vector_t = Eigen::Matrix; + +//! \brief This error is thrown when a bad syntax is detected in the database +class db_invalid_syntax : public std::exception +{ + std::string m_msg; + const char *m_msg_cstr; + +public: + db_invalid_syntax (const std::string &msg): + m_msg("DB Invalid Syntax :" + msg), m_msg_cstr(m_msg.c_str()) + {} + + const char *what() throw() + { + return m_msg_cstr; + } +}; + + +//! \brief This error is thrown when the database is not as expected +class invalid_database : public std::exception +{ + std::string m_msg; + const char *m_msg_cstr; + +public: + invalid_database (const std::string &msg): + m_msg("Invalid Database:" + msg), m_msg_cstr(m_msg.c_str()) + {} + + const char *what() throw() + { + return m_msg_cstr; + } +}; } // end namespace database } // end namespace specmicp #endif // SPECMICP_DATABASE_COMMONDEF_HPP diff --git a/src/database/reader.hpp b/src/database/reader.hpp index 20adf5c..15c90d9 100644 --- a/src/database/reader.hpp +++ b/src/database/reader.hpp @@ -1,147 +1,129 @@ /*------------------------------------------------------- - Module : database - File : reader.hpp - Author : Fabien Georget Copyright (c) 2014, Fabien Georget, Princeton University ---------------------------------------------------------*/ #ifndef DATABASE_READER_H #define DATABASE_READER_H //! \file reader.hpp Read the json database #include #include #include "data_container.hpp" namespace specmicp { namespace database { //! \brief Read a json file containing a database //! //! This is a reader class, it does not transform //! (reduce to canonical form, swap the basis) the data class DataReader { public: //! Type of a smart pointer containing the database using DataContainerPtr = std::shared_ptr; //! \brief Default constructor //! //! Be sure to set the path to the database using set_database_path DataReader() { data = std::make_shared(); } //! \brief Constructor //! //! @param filepath string containing the path to the database DataReader(std::string filepath): m_filepath(filepath) { data = std::make_shared(); } //! \brief Set the database path void set_database_path(std::string filepath){ m_filepath = filepath; } //! \brief Parse database //! //! Throw db_invalid_syntax if a syntax error was detected. //! Throws std::invalid_argument if the path to the database is not correct void parse(); //! Return the databes DataContainerPtr get_database() {return data;} private: //! \brief Parse the metadata section //! //! we don't do much with them for now.... void parse_metadata(); //! \brief Parse the basis section //! //! Contains the list of primary species void parse_basis(); //! \brief Parse the aqueous section //! //! Contains the list of secondary species void parse_aqueous(); //! \brief Parse the mineral section //! //! Contains the list of minerals void parse_minerals(); //! \brief To be sure that the database is og void check_database(); //! \brief Check the size of the basis void parse_size_db(); std::string m_filepath; //! The path to the database Json::Value m_root; //! Root of the json structure DataContainerPtr data; //! The database to fill }; //! \brief Parse an equation void parse_equation(const std::string &equation, std::map& compo); //! \brief Get the charge of a species by parsing the label //! //! Examples : //! - neutral -> 0 //! - neutral[] -> 0 //! - charge[+] -> +1 //! - charge[-1] -> -1 //! - charge[+2] -> +2 //! - charge[2-] -> -2 double charge_from_label(const std::string& label); - -//! \brief This error is thrown when a bad syntax is detected in the database -class db_invalid_syntax : public std::exception -{ - std::string m_msg; - const char *m_msg_cstr; - -public: - db_invalid_syntax (const std::string &msg): - m_msg("DB Invalid Syntax :" + msg), m_msg_cstr(m_msg.c_str()) - {} - - const char *what() throw() - { - return m_msg_cstr; - } -}; - //! \brief Check if a mandatory section/attribute exists //! //! Throw db_invalid_syntax if not. inline void check_for_mandatory_value(const Json::Value& root, const std::string& attribute) { if (not root.isMember(attribute)) { throw db_invalid_syntax("Missing : mandatory parameter '" + attribute +"'."); } } //! \brief Return a mandatory section //! //! Throw db_invalid_syntax if the section does not exist inline Json::Value& get_mandatory_section(Json::Value& root, const std::string& section) { check_for_mandatory_value(root, section); return root[section]; } } // end namespace database } // end namespace specmicp #endif // DATABASE_READER_H