Page MenuHomec4science

module.hpp
No OneTemporary

File Metadata

Created
Thu, Nov 14, 05:54

module.hpp

/*-------------------------------------------------------
- Module : database
- File : module
- Author : Fabien Georget
Copyright (c) 2014, Fabien Georget, Princeton University
---------------------------------------------------------*/
#ifndef SPECMICP_DATAABASE_MODULE_HPP
#define SPECMICP_DATAABASE_MODULE_HPP
//! \file module.hpp base class for a database module
#include <memory>
#include "data_container.hpp"
namespace specmicp {
namespace database {
//! \brief Base class for a database module
class DatabaseModule
{
public:
//! The type of a smart pointer to a raw database
using RawDatabasePtr = std::shared_ptr<DataContainer>;
//! Index that indicates no known species
static const int no_species = -1;
//! Create a blank database
DatabaseModule() {
reset_database();
}
//! Use the database thedata
DatabaseModule(RawDatabasePtr thedata): data(thedata) {}
//! \brief set the database
void set_database(RawDatabasePtr thedata) {data = thedata;}
//! \brief (Re)set the database - create a blank database
void reset_database() {data = std::make_shared<DataContainer>();}
//! \brief Return true if the database is canonical
bool is_database_canonical() {return data->is_canonical;}
//! \brief Return the id of species "label" in list_labels
//!
//! return no_species if the species cannot be found in the database
static int label_to_id(const std::string& label, const std::vector<std::string>& list_labels)
{
auto search = std::find(list_labels.begin(), list_labels.end(), label);
if (search == list_labels.end()) return DatabaseModule::no_species;
else return (search - list_labels.begin());
}
//! \brief Return the id of species "label" in list_labels
//!
//! Throw std::invalid_argument if the species cannot be found in the list
static int safe_label_to_id(const std::string& label, const std::vector<std::string>& list_labels)
{
auto search = std::find(list_labels.begin(), list_labels.end(), label);
if (search == list_labels.end()) throw std::invalid_argument("Unknown species : '"+label+"'.");
else return (search - list_labels.begin());
}
//! \brief Return the id of the mineral "label"
//!
//! Return no_species if label is not a mineral (at equilibrium)
int mineral_label_to_id(const std::string& label)
{
return label_to_id(label, data->labels_minerals);
}
//! \brief Return the id of the mineral "label"
//!
//! Return no_species if label is not a mineral (at equilibrium)
int mineral_kinetic_label_to_id(const std::string& label)
{
return label_to_id(label, data->labels_minerals_kinetic);
}
//! \brief Return the id of the component "label"
//!
//! Return no_species if "label" is not a component
int component_label_to_id(const std::string& label)
{
return label_to_id(label, data->labels_basis);
}
//! \brief Return the id of the secondary aqueous species "label"
//!
//! Return no_species if "label" is not an aqueous species.
//! A component (of the basis) is not considered as a secondary aqueous species.
int aqueous_label_to_it(const std::string& label)
{
return label_to_id(label, data->labels_aqueous);
}
protected:
RawDatabasePtr data; //! The database
};
} // end namespace database
} // end namespace specmicp
#endif // SPECMICP_DATAABASE_MODULE_HPP

Event Timeline