Page MenuHomec4science

selector.cpp
No OneTemporary

File Metadata

Created
Fri, May 17, 21:23

selector.cpp

/*-------------------------------------------------------
- Module : database
- File : 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 "selector.hpp"
#include <iostream>
namespace specmicp {
namespace database {
void DatabaseSelector::remove_component(const std::vector<index_t>& id_component_to_remove)
{
analyse_component(id_component_to_remove);
select_aqueous(id_component_to_remove);
select_minerals(id_component_to_remove);
select_minerals_kinetic(id_component_to_remove);
if (data->nb_gas() != 0) select_gas(id_component_to_remove);
select_sorbed(id_component_to_remove);
select_components();
}
void DatabaseSelector::analyse_component(const std::vector<index_t>& id_component_to_remove)
{
for (auto it: id_component_to_remove)
{
m_is_component_to_remove[it] = true;
}
m_nb_component_to_keep = data->nb_component() - id_component_to_remove.size();
}
void DatabaseSelector::select_secondary(
ReactiveSpeciesList* toselect,
const std::vector<index_t>& id_component_to_remove
)
{
// first we select which aqueous species we should remove
std::vector<bool> to_remove(toselect->size(), false);
for (index_t j: toselect->range())
{
for (const auto& it: id_component_to_remove)
{
if (toselect->nu_ji(j, it) != 0.0)
{
to_remove[j] = true;
break;
}
}
}
// then we remove them, in two steps
const auto nb_to_keep = std::count(to_remove.cbegin(), to_remove.cend(), false);
auto new_j = 0;
// 1) first we copy data in the beginning of the arrays
for (index_t j: toselect->range())
{
if (to_remove[j]) continue;
toselect->move_erase(j, new_j, m_is_component_to_remove);
++new_j;
}
specmicp_assert(new_j == nb_to_keep);
// 2) then we resize the arrays
toselect->resize(nb_to_keep, nb_component_to_keep());
toselect->set_valid();
// By doing in this order, we avoid bulk copy of arrays
}
void DatabaseSelector::select_aqueous(const std::vector<index_t>& id_component_to_remove)
{
select_secondary(&(data->aqueous), id_component_to_remove);
}
void DatabaseSelector::select_minerals(const std::vector<index_t>& id_component_to_remove)
{
select_secondary(&(data->minerals), id_component_to_remove);
}
void DatabaseSelector::select_minerals_kinetic(const std::vector<index_t>& id_component_to_remove)
{
select_secondary(&(data->minerals_kinetic), id_component_to_remove);
}
void DatabaseSelector::select_gas(const std::vector<index_t>& id_component_to_remove)
{
select_secondary(&(data->gas), id_component_to_remove);
}
void DatabaseSelector::select_sorbed(const std::vector<index_t>& id_component_to_remove)
{
select_secondary(&(data->sorbed), id_component_to_remove);
}
void DatabaseSelector::select_components()
{
uindex_t new_i = 0;
for (index_t i: data->range_component())
{
if (is_component_to_remove(i)) continue;
data->components.move_erase(i, new_i);
++new_i;
}
specmicp_assert(new_i == nb_component_to_keep());
data->components.resize(nb_component_to_keep());
data->components.set_valid();
}
void DatabaseSelector::keep_only_component(const std::vector<index_t>& id_to_keep)
{
// First build the list of components to remove
std::vector<index_t> id_to_remove;
id_to_remove.reserve(data->nb_component() - id_to_keep.size());
for (index_t id: data->range_component())
{
auto search = std::find(id_to_keep.begin(), id_to_keep.end(), id);
if (search == id_to_keep.end())
id_to_remove.push_back(id);
}
// Then remove them
remove_component(id_to_remove);
}
void DatabaseSelector::remove_all_gas()
{
data->gas = GasList(0, data->nb_component());
}
void DatabaseSelector::remove_all_sorbed()
{
data->sorbed = SorbedList(0, data->nb_component());
}
//! \brief Remove some specific aqueous species
void DatabaseSelector::remove_aqueous(const std::vector<index_t>& id_aqueous)
{
const index_t new_nb_aqueous = data->nb_aqueous() - id_aqueous.size();
index_t new_id = 0;
for (index_t aqueous: data->range_aqueous())
{
auto it = std::find(id_aqueous.cbegin(), id_aqueous.cend(), aqueous);
if (it == id_aqueous.end()) // if we need to keep the species
{
if (*it == aqueous)
{
++new_id;
continue; // nothing to change
}
data->aqueous.move_erase(aqueous, new_id);
++new_id;
}
}
specmicp_assert(new_id == new_nb_aqueous);
data->aqueous.resize(new_nb_aqueous);
}
// -------------------
//
// Oxydo-reduction
//
// -------------------
//void DatabaseSelector::remove_half_cells_reactions()
//{
// remove_half_cells_aqueous();
// remove_half_cells_sorbed();
// remove_half_cells_mineral();
// remove_half_cells_mineral_kinetic();
//}
void DatabaseSelector::remove_half_cells_aqueous()
{
index_t new_id {0};
// reorganize the data
for (index_t aqueous: data->range_aqueous())
{
// if (not data->is_half_cell_reaction(aqueous))
// {
// data->aqueous.move_erase(aqueous, new_id);
// ++new_id;
// }
}
data->aqueous.resize(new_id);
}
void DatabaseSelector::remove_half_cells_sorbed()
{
// index_t new_id {0};
// // reorganizing
// for (index_t sorbed: data->range_sorbed())
// {
// if (data->is_sorbed_half_cell_reaction(sorbed))
// {
// data->nu_sorbed.row(new_id) = data->nu_sorbed.row(sorbed);
// data->logk_sorbed(new_id) = data->logk_sorbed(sorbed);
// data->labels_sorbed[new_id] = data->labels_sorbed[sorbed];
// ++new_id;
// }
// }
// data->nb_sorbed = new_id;
// // resizing
// data->labels_sorbed.resize(new_id);
// data->nu_sorbed.conservativeResize(new_id, Eigen::NoChange);
// data->logk_sorbed.conservativeResize(new_id);
}
void DatabaseSelector::remove_half_cells_gas()
{
// index_t new_id {0};
// // reorganizing
// for (index_t gas: data->range_gas())
// {
// if (data->is_gas_half_cell_reaction(gas))
// {
// data->nu_gas.row(new_id) = data->nu_gas.row(gas);
// data->logk_gas(new_id) = data->logk_gas(gas);
// data->labels_gas[new_id] = data->labels_gas[gas];
// ++new_id;
// }
// }
// data->nb_gas = new_id;
// // resizing
// data->labels_gas.resize(new_id);
// data->nu_gas.conservativeResize(new_id, Eigen::NoChange);
// data->logk_gas.conservativeResize(new_id);
}
//void DatabaseSelector::remove_half_cells_mineral()
//{
// index_t new_id {0};
// // reorganizing
// for (index_t mineral: data->range_mineral())
// {
// if (data->is_mineral_half_cell_reaction(mineral))
// {
// data->nu_mineral.row(new_id) = data->nu_mineral.row(mineral);
// data->logk_mineral(new_id) = data->logk_mineral(mineral);
// data->labels_minerals[new_id] = data->labels_minerals[mineral];
// data->_molar_volume_mineral(new_id) = data->_molar_volume_mineral(mineral);
// ++new_id;
// }
// }
// data->nb_mineral = new_id;
// //resizing
// data->labels_minerals.resize(new_id);
// data->nu_mineral.conservativeResize(new_id, Eigen::NoChange);
// data->logk_mineral.conservativeResize(new_id);
// data->_molar_volume_mineral.conservativeResize(new_id);
//}
//void DatabaseSelector::remove_half_cells_mineral_kinetic()
//{
// index_t new_id {0};
// // reorganizing
// for (index_t mineral: data->range_mineral_kinetic())
// {
// if (data->is_mineral_kinetic_half_cell_reaction(mineral))
// {
// data->nu_mineral_kinetic.row(new_id) = data->nu_mineral_kinetic.row(mineral);
// data->logk_mineral_kinetic(new_id) = data->logk_mineral_kinetic(mineral);
// data->labels_minerals_kinetic[new_id] = data->labels_minerals_kinetic[mineral];
// data->_molar_volume_mineral_kinetic(new_id) = data->_molar_volume_mineral_kinetic(mineral);
// ++new_id;
// }
// }
// data->nb_mineral_kinetic = new_id;
// //resizing
// data->labels_minerals_kinetic.resize(new_id);
// data->nu_mineral_kinetic.conservativeResize(new_id, Eigen::NoChange);
// data->logk_mineral_kinetic.conservativeResize(new_id);
// data->_molar_volume_mineral_kinetic.conservativeResize(new_id);
//}
} // end namespace database
} // end namespace specmicp

Event Timeline