Page MenuHomec4science

module.hpp
No OneTemporary

File Metadata

Created
Fri, Nov 1, 14:50

module.hpp

/*-------------------------------------------------------
- Module : database
- File : module
- Author : Fabien Georget
Copyright (c) 2014, Fabien Georget <fabieng@princeton.edu>, Princeton University
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Princeton University nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
---------------------------------------------------------*/
#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_id(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