Page MenuHomec4science

canonicalizer.cpp
No OneTemporary

File Metadata

Created
Fri, Jun 28, 14:44

canonicalizer.cpp

/*-------------------------------------------------------
- Module : database
- File : canonicalizer.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 "canonicalizer.hpp"
#include "utils/log.hpp"
namespace specmicp {
namespace database {
void DatabaseCanonicalizer::make_canonical() {
if (data->is_canonical == true)
{
WARNING << "Database already in canonical form, aborting";
return;
}
make_aqueous_canonical();
make_mineral_canonical();
make_mineral_kinetic_canonical();
if (data->nb_gas != 0) make_gas_canonical();
data->is_canonical = true;
}
void DatabaseCanonicalizer::make_aqueous_canonical()
{
if (data->nu_aqueous.cols() == data->nb_component)
{
INFO << "Database already in canonical form";
return;
}
if (data->nu_aqueous.cols() != data->nb_component + data->nb_aqueous)
{
CRITICAL << "Canonicalization : invalid size for stoichiometric coefficients of aqueous species - abort";
throw invalid_database("Invalid size for stoichiometric coefficients of aqueous species");
}
for (int i=1; i<data->nb_aqueous; ++i)
{
for (int j=0; j<i; ++j)
{
const double stoech = data->nu_aqueous(i, data->nb_component+j);
if (stoech != 0)
{
data->nu_aqueous.row(i) += stoech*data->nu_aqueous.row(j);
data->logk_aqueous(i) += stoech*data->logk_aqueous(j);
data->nu_aqueous(i, data->nb_component+j) = 0;
}
}
}
data->nu_aqueous.conservativeResize(Eigen::NoChange_t(), data->nb_component);
}
void DatabaseCanonicalizer::make_mineral_canonical()
{
if (data->nu_mineral.cols() == data->nb_component)
{
INFO << "Database already in canonical form";
return;
}
if (data->nu_mineral.cols() != data->nb_component + data->nb_aqueous)
{
CRITICAL << "Canonicalization : invalid size for stoichiometric coefficients of mineral species - abort";
throw invalid_database("Invalid size for stoichiometric coefficients of mineral species");
}
for (int i=1; i<data->nb_mineral; ++i)
{
for (int j=0; j<data->nb_aqueous; ++j)
{
const double stoech = data->nu_mineral(i, data->nb_component+j);
if (stoech != 0)
{
data->nu_mineral.block(i, 0, 1, data->nb_component) +=
stoech*data->nu_aqueous.row(j);
data->logk_mineral(i) += stoech*data->logk_aqueous(j);
data->nu_mineral(i, data->nb_component+j) = 0;
}
}
}
data->nu_mineral.conservativeResize(Eigen::NoChange_t(), data->nb_component);
}
void DatabaseCanonicalizer::make_mineral_kinetic_canonical()
{
if (data->nu_mineral_kinetic.cols() == data->nb_component)
{
INFO << "Database already in canonical form";
return;
}
if (data->nu_mineral_kinetic.cols() != data->nb_component + data->nb_aqueous)
{
CRITICAL << "Canonicalization : invalid size for stoichiometric coefficients of mineral species - abort";
throw invalid_database("Invalid size for stoichiometric coefficients of mineral species");
}
for (int i=1; i<data->nb_mineral_kinetic; ++i)
{
for (int j=0; j<data->nb_aqueous; ++j)
{
const double stoech = data->nu_mineral_kinetic(i, data->nb_component+j);
if (stoech != 0)
{
data->nu_mineral_kinetic.block(i, 0, 1, data->nb_component) +=
stoech*data->nu_aqueous.row(j);
data->logk_mineral_kinetic(i) += stoech*data->logk_aqueous(j);
data->nu_mineral_kinetic(i, data->nb_component+j) = 0;
}
}
}
data->nu_mineral_kinetic.conservativeResize(Eigen::NoChange_t(), data->nb_component);
}
void DatabaseCanonicalizer::make_gas_canonical()
{
if (data->nu_gas.cols() == data->nb_component)
{
INFO << "Database already in canonical form";
return;
}
if (data->nu_gas.cols() != data->nb_component + data->nb_aqueous)
{
CRITICAL << "Canonicalization : invalid size for stoichiometric coefficients of gas species - abort";
throw invalid_database("Invalid size for stoichiometric coefficients of gas species");
}
for (int g=0; g<data->nb_gas; ++g)
{
for (int j=0; j<data->nb_aqueous; ++j)
{
const double stoech = data->nu_gas(g, data->nb_component+j);
if (stoech != 0)
{
data->nu_gas.block(g, 0, 1, data->nb_component) += stoech*data->nu_aqueous.row(j);
data->logk_gas(g) += stoech*data->logk_aqueous(j);
data->nu_gas(g, data->nb_component+j) = 0;
}
}
}
data->nu_gas.conservativeResize(Eigen::NoChange_t(), data->nb_component);
}
} // end namespace database
} // end namespace specmicp

Event Timeline