Page MenuHomec4science

mineral_selector.cpp
No OneTemporary

File Metadata

Created
Sat, Nov 16, 04:14

mineral_selector.cpp

/*-------------------------------------------------------
- Module : database
- File : mineral_selector.cpp
- Author : Fabien Georget
Copyright (c) 2014, Fabien Georget, Princeton University
---------------------------------------------------------*/
#include "mineral_selector.hpp"
namespace specmicp {
namespace database {
void MineralSelector::keep_only(const std::vector<int>& minerals_to_keep)
{
// First we compute the new sizes
const int nb_to_keep = minerals_to_keep.size();
const int nb_to_remove = data->nb_mineral - nb_to_keep;
const int nb_new_min_kin = data->nb_mineral_kinetic + nb_to_remove;
// Then we assure that there is place for moving data
data->logk_mineral_kinetic.conservativeResize(nb_new_min_kin);
data->labels_minerals_kinetic.reserve(nb_new_min_kin);
data->nu_mineral_kinetic.conservativeResize(nb_new_min_kin, Eigen::NoChange);
data->_molar_volume_mineral_kinetic.conservativeResize(nb_new_min_kin);
// Then we move data
int new_id = 0;
int new_id_kin = data->nb_mineral_kinetic;
for (int id=0; id<data->nb_mineral; ++id)
{
auto search = std::find(minerals_to_keep.begin(), minerals_to_keep.end(), id);
if (search == minerals_to_keep.end() )
{
data->logk_mineral_kinetic(new_id_kin) = data->logk_mineral(id);
data->labels_minerals_kinetic.push_back(data->labels_minerals[id]);
data->nu_mineral_kinetic.row(new_id_kin) = data->nu_mineral.row(id);
data->_molar_volume_mineral_kinetic(new_id_kin) = data->_molar_volume_mineral(id);
++new_id_kin;
}
else
{
data->logk_mineral(new_id) = data->logk_mineral(id);
data->labels_minerals[new_id] = data->labels_minerals[id];
data->nu_mineral.row(new_id) = data->nu_mineral.row(id);
data->_molar_volume_mineral(new_id) = data->_molar_volume_mineral(id);
++new_id;
}
}
assert(new_id == nb_to_keep); // simple test to assure that everything is ok
assert(new_id_kin == nb_new_min_kin);
// Finally we shrink the containers for the mineral at equilibrium
data->logk_mineral.conservativeResize(nb_to_keep);
data->nu_mineral.conservativeResize(nb_to_keep, Eigen::NoChange);
data->labels_minerals.resize(nb_to_keep);
data->_molar_volume_mineral.conservativeResize(nb_to_keep);
data->nb_mineral = nb_to_keep;
data->nb_mineral_kinetic = nb_new_min_kin;
}
void MineralSelector::keep_only(const std::vector<std::string>& minerals_to_keep)
{
std::vector<int> ids(minerals_to_keep.size());
for (unsigned int min=0; min<minerals_to_keep.size(); ++min)
{
int id = mineral_label_to_id(minerals_to_keep[min]);
if (id == no_species)
{
throw std::invalid_argument("Unknown species : '"+minerals_to_keep[min]+"'.");
}
ids[min] = id;
}
keep_only(ids);
}
} // end namespace database
} // end namespace specmicp

Event Timeline