Page MenuHomec4science

mineral_selector.cpp
No OneTemporary

File Metadata

Created
Sun, May 12, 01:45

mineral_selector.cpp

/*-------------------------------------------------------
- Module : database
- File : mineral_selector.cpp
- 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.
---------------------------------------------------------*/
#include "mineral_selector.hpp"
namespace specmicp {
namespace database {
void MineralSelector::keep_only(const std::vector<index_t>& minerals_to_keep)
{
// First we compute the new sizes
const uindex_t nb_to_keep = minerals_to_keep.size();
const uindex_t nb_to_remove = data->nb_mineral - nb_to_keep;
const uindex_t 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
uindex_t new_id = 0;
uindex_t new_id_kin = data->nb_mineral_kinetic;
for (index_t id: data->range_mineral())
{
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);
data->stability_mineral(new_id) = data->stability_mineral(id);
++new_id;
}
}
specmicp_assert(new_id == nb_to_keep); // simple test to assure that everything is ok
specmicp_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->_stability_mineral.resize(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<index_t> ids(minerals_to_keep.size());
for (uindex_t min=0; min<minerals_to_keep.size(); ++min)
{
index_t 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