Page MenuHomec4science

selector.cpp
No OneTemporary

File Metadata

Created
Sat, Jun 22, 16: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"
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_components();
}
void DatabaseSelector::analyse_component(const std::vector<index_t>& id_component_to_remove)
{
for (auto it=id_component_to_remove.begin(); it!=id_component_to_remove.end(); ++it)
{
m_is_component_to_remove[*it] = true;
}
m_nb_component_to_keep = data->nb_component - id_component_to_remove.size();
uindex_t new_i = 0;
for (index_t i: data->range_component())
{
if (is_component_to_remove(i)) continue;
data->param_aq.row(new_i) = data->param_aq.row(i);
data->_molar_mass_basis(new_i) = data->molar_mass_basis(i);
++new_i;
}
specmicp_assert(new_i == nb_component_to_keep());
data->_molar_mass_basis.conservativeResize(nb_component_to_keep());
}
void DatabaseSelector::select_aqueous(const std::vector<index_t>& id_component_to_remove)
{
// first we select which aqueous species we should remove
std::vector<bool> aqueous_to_remove(data->nb_aqueous, false);
for (index_t j: data->range_aqueous())
{
for (auto it=id_component_to_remove.begin(); it!=id_component_to_remove.end(); ++it)
{
if (data->nu_aqueous(j, *it) != 0)
{
aqueous_to_remove[j] = true;
break;
}
}
}
// then we remove them, in two steps
const uindex_t nb_aq_to_keep = std::count(aqueous_to_remove.begin(), aqueous_to_remove.end(), false);
uindex_t new_j = 0;
// 1) first we copy data in the beginning of the arrays
for (index_t j: data->range_aqueous())
{
if (aqueous_to_remove[j]) continue;
data->logk_aqueous(new_j) = data->logk_aqueous(j);
data->param_aq.row(nb_component_to_keep()+new_j) = data->param_aq.row(data->nb_component+j);
data->labels_aqueous[new_j] = data->labels_aqueous[j];
uindex_t new_i = 0;
for (index_t i: data->range_component())
{
if (is_component_to_remove(i)) continue;
data->nu_aqueous(new_j, new_i) = data->nu_aqueous(j, i);
++new_i;
}
specmicp_assert(new_i == nb_component_to_keep());
++new_j;
}
specmicp_assert(new_j == nb_aq_to_keep);
// 2) then we resize the arrays
data->nu_aqueous.conservativeResize(nb_aq_to_keep, nb_component_to_keep());
data->logk_aqueous.conservativeResize(nb_aq_to_keep);
data->labels_aqueous.resize(nb_aq_to_keep);
data->param_aq.conservativeResize(nb_component_to_keep()+nb_aq_to_keep, Eigen::NoChange);
data->nb_aqueous = nb_aq_to_keep;
// By doing in this order, we avoid bulk copy of arrays
}
void DatabaseSelector::select_minerals(const std::vector<index_t>& id_component_to_remove)
{
// first we select which minerals we should remove
std::vector<bool> minerals_to_remove(data->nb_mineral, false);
for (index_t m: data->range_mineral())
{
for (auto it=id_component_to_remove.begin(); it!=id_component_to_remove.end(); ++it)
{
if (data->nu_mineral(m, *it) != 0)
{
minerals_to_remove[m] = true;
break;
}
}
}
// then we remove them, in two steps
const uindex_t nb_min_to_keep = std::count(minerals_to_remove.begin(), minerals_to_remove.end(), false);
uindex_t new_m = 0;
// 1) first we copy data in the beginning of the arrays
for (index_t m: data->range_mineral())
{
if (minerals_to_remove[m]) continue;
data->logk_mineral(new_m) = data->logk_mineral(m);
data->labels_minerals[new_m] = data->labels_minerals[m];
data->_molar_volume_mineral(new_m) = data->_molar_volume_mineral(m);
data->stability_mineral(new_m) = data->stability_mineral(m);
uindex_t new_i = 0;
for (index_t i: data->range_component())
{
if (is_component_to_remove(i)) continue;
data->nu_mineral(new_m, new_i) = data->nu_mineral(m, i);
++new_i;
}
specmicp_assert(new_i == nb_component_to_keep());
++new_m;
}
specmicp_assert(new_m == nb_min_to_keep);
// 2) then we resize the arrays
data->nu_mineral.conservativeResize(nb_min_to_keep, nb_component_to_keep());
data->logk_mineral.conservativeResize(nb_min_to_keep);
data->labels_minerals.resize(nb_min_to_keep);
data->_molar_volume_mineral.conservativeResize(nb_min_to_keep);
data->_stability_mineral.resize(new_m);
data->nb_mineral = nb_min_to_keep;
// By doing in this order, we avoid bulk copy of arrays
}
void DatabaseSelector::select_minerals_kinetic(const std::vector<index_t>& id_component_to_remove)
{
// first we select which minerals we should remove
std::vector<bool> minerals_to_remove(data->nb_mineral_kinetic, false);
for (index_t m: data->range_mineral_kinetic())
{
for (auto it=id_component_to_remove.begin(); it!=id_component_to_remove.end(); ++it)
{
if (data->nu_mineral_kinetic(m, *it) != 0)
{
minerals_to_remove[m] = true;
break;
}
}
}
// then we remove them, in two steps
const uindex_t nb_min_to_keep = std::count(minerals_to_remove.begin(), minerals_to_remove.end(), false);
uindex_t new_m = 0;
// 1) first we copy data in the beginning of the arrays
for (index_t m: data->range_mineral_kinetic())
{
if (minerals_to_remove[m]) continue;
data->logk_mineral_kinetic(new_m) = data->logk_mineral_kinetic(m);
data->labels_minerals_kinetic[new_m] = data->labels_minerals_kinetic[m];
data->_molar_volume_mineral_kinetic(new_m) = data->_molar_volume_mineral_kinetic(m);
uindex_t new_i = 0;
for (index_t i: data->range_component())
{
if (is_component_to_remove(i)) continue;
data->nu_mineral_kinetic(new_m, new_i) = data->nu_mineral_kinetic(m, i);
++new_i;
}
specmicp_assert(new_i == nb_component_to_keep());
++new_m;
}
specmicp_assert(new_m == nb_min_to_keep);
// 2) then we resize the arrays
data->nu_mineral_kinetic.conservativeResize(nb_min_to_keep, nb_component_to_keep());
data->logk_mineral_kinetic.conservativeResize(nb_min_to_keep);
data->labels_minerals_kinetic.resize(nb_min_to_keep);
data->_molar_volume_mineral_kinetic.conservativeResize(nb_min_to_keep);
data->nb_mineral_kinetic = nb_min_to_keep;
// By doing in this order, we avoid bulk copy of arrays
}
void DatabaseSelector::select_gas(const std::vector<index_t>& id_component_to_remove)
{
// first we select which aqueous species we should remove
std::vector<bool> gas_to_remove(data->nb_gas, false);
for (index_t j: data->range_gas())
{
for (auto it=id_component_to_remove.begin(); it!=id_component_to_remove.end(); ++it)
{
if (data->nu_gas(j, *it) != 0)
{
gas_to_remove[j] = true;
break;
}
}
}
// then we remove them, in two steps
const uindex_t nb_gas_to_keep = std::count(gas_to_remove.begin(), gas_to_remove.end(), false);
uindex_t new_j = 0;
// 1) first we copy data in the beginning of the arrays
for (index_t j: data->range_gas())
{
if (gas_to_remove[j]) continue;
data->logk_gas(new_j) = data->logk_gas(j);
data->labels_gas[new_j] = data->labels_gas[j];
uindex_t new_i = 0;
for (index_t i: data->range_component())
{
if (is_component_to_remove(i)) continue;
data->nu_gas(new_j, new_i) = data->nu_gas(j, i);
++new_i;
}
specmicp_assert(new_i == nb_component_to_keep());
++new_j;
}
specmicp_assert(new_j == nb_gas_to_keep);
// 2) then we resize the arrays
data->nu_gas.conservativeResize(nb_gas_to_keep, nb_component_to_keep());
data->logk_gas.conservativeResize(nb_gas_to_keep);
data->labels_gas.resize(nb_gas_to_keep);
data->nb_gas = nb_gas_to_keep;
// By doing in this order, we avoid bulk copy of arrays
}
void DatabaseSelector::select_components()
{
uindex_t new_i = 0;
for (index_t i: data->range_component())
{
if (is_component_to_remove(i)) continue;
data->labels_basis[new_i] = data->labels_basis[i];
++new_i;
}
specmicp_assert(new_i == nb_component_to_keep());
data->labels_basis.resize(nb_component_to_keep());
data->nb_component = nb_component_to_keep();
}
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);
}
} // end namespace database
} // end namespace specmicp

Event Timeline