Page MenuHomec4science

diffusion_secondary.cpp
No OneTemporary

File Metadata

Created
Sun, Nov 10, 23:01

diffusion_secondary.cpp

/*-------------------------------------------------------
- Module : reactmicp/systems/diffusion
- File : diffusion_secondary.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 "diffusion_secondary.hpp"
#include "physics/laws.hpp"
namespace specmicp {
namespace reactmicp {
namespace systems {
namespace diffusion {
// ================================== //
// //
// Secondary variables wibbly-timey //
// //
// ================================== //
//! \brief Set secondary concentrations
void DiffusionSecondaryVariables::compute_secondary_concentrations(const Variables& variable)
{
for (ind_t node=0; node<m_secondary_concentration.cols(); ++node)
{
nodal_secondary_concentrations(node, variable);
}
}
//! \brief Set secondary concentrations for node 'node'
void DiffusionSecondaryVariables::nodal_secondary_concentrations(ind_t node, const Variables& variable)
{
for (int species: m_data->range_aqueous())
{
double sum = -m_data->logk_aqueous(species) - loggamma_secondary(node, species);
for (int component: m_data->range_aqueous_component())
{
sum += m_data->nu_aqueous(species, component)*(
loggamma_component(node, component)
+ m_ideq.component_concentration(node, component, variable)
);
}
secondary_concentration(node, species) = pow10(sum);
}
}
double DiffusionSecondaryVariables::norm_secondary_variables()
{
return m_secondary_variables.block(
1, 0,
m_data->nb_aqueous+m_data->nb_component,
m_secondary_concentration.cols()
).norm();
}
double DiffusionSecondaryVariables::nodal_norm_secondary_variables(ind_t node)
{
return m_secondary_variables.block(
1, node,
m_data->nb_aqueous+m_data->nb_component,
1
).norm();
}
//! \brief Solve for secondary variables
int DiffusionSecondaryVariables::solve_secondary_variables(const Variables& variable)
{
bool may_have_converged = false;
double previous_norm = norm_secondary_variables();
for (int i=0; i<6; ++i)
{
compute_secondary_concentrations(variable);
compute_secondary_variables(variable);
double new_norm = norm_secondary_variables();
if (std::abs(previous_norm - new_norm)/previous_norm < 1e-6) {may_have_converged=true; break;}
previous_norm = new_norm;
}
return may_have_converged;
}
//! \brief Solve for secondary variables
int DiffusionSecondaryVariables::nodal_solve_secondary_variables(ind_t node, const Variables& variable)
{
bool may_have_converged = false;
double previous_norm = nodal_norm_secondary_variables(node);
for (int i=0; i<6; ++i)
{
nodal_secondary_concentrations(node, variable);
nodal_secondary_variables(node, variable);
double new_norm = nodal_norm_secondary_variables(node);
if (std::abs(previous_norm - new_norm)/previous_norm < 1e-6) {may_have_converged=true; break;}
previous_norm = new_norm;
}
return may_have_converged;
}
//! \brief Compute secondary variables
void DiffusionSecondaryVariables::compute_secondary_variables(const Variables& variable)
{
for (ind_t node=0; node<m_secondary_concentration.cols(); ++node)
{
nodal_secondary_variables(node, variable);
}
}
void DiffusionSecondaryVariables::nodal_secondary_variables(ind_t node,
const Variables& variable)
{
nodal_ionic_strength(node, variable);
nodal_loggamma(node);
}
void DiffusionSecondaryVariables::compute_ionic_strength(const Variables& variable)
{
for (ind_t node=0; node<m_secondary_concentration.cols(); ++node)
{
nodal_ionic_strength(node, variable);
}
}
//! \brief compute the ionic strength for node 'node'
void DiffusionSecondaryVariables::nodal_ionic_strength(ind_t node, const Variables& variable)
{
double ionics = 0;
for (int component: m_data->range_aqueous_component())
{
if (m_data->param_aq(component, 0) == 0) continue;
ionics += std::pow(m_data->param_aq(component, 0), 2)*pow10(
m_ideq.component_concentration(node, component, variable));
}
for (int aqueous: m_data->range_aqueous())
{
ind_t idaq = m_data->nb_component + aqueous;
if (m_data->param_aq(idaq, 0) == 0) continue;
ionics += std::pow(m_data->param_aq(idaq, 0), 2)*secondary_concentration(node, aqueous);
}
ionic_strength(node) = ionics/2;
}
void DiffusionSecondaryVariables::compute_loggamma()
{
for (ind_t node=0; node<m_secondary_concentration.cols(); ++node)
{
nodal_loggamma(node);
}
}
//! \brief compute the logarithm of the activity coefficients for node 'node'
void DiffusionSecondaryVariables::nodal_loggamma(ind_t node)
{
nodal_loggamma_component(node);
nodal_loggamma_aqueous(node);
}
//! \brief compute the logarithm of the activity coefficients for node 'node'
void DiffusionSecondaryVariables::nodal_loggamma_component(ind_t node)
{
const double sqrti = std::sqrt(ionic_strength(node));
for (int component: m_data->range_aqueous_component())
{
loggamma_component(node, component) = laws::extended_debye_huckel(ionic_strength(node), sqrti,
m_data->param_aq(component, 0),
m_data->param_aq(component, 1),
m_data->param_aq(component, 2));
}
}
//! \brief compute the logarithm of the activity coefficients for node 'node'
void DiffusionSecondaryVariables::nodal_loggamma_aqueous(ind_t node)
{
const double sqrti = std::sqrt(ionic_strength(node));
for (int aqueous: m_data->range_aqueous())
{
ind_t idaq = m_data->nb_component+aqueous;
loggamma_secondary(node, aqueous) = laws::extended_debye_huckel(ionic_strength(node), sqrti,
m_data->param_aq(idaq, 0),
m_data->param_aq(idaq, 1),
m_data->param_aq(idaq, 2));
}
}
} // end namespace diffusion
} // end namespace systems
} // end namespace reactmicp
} // end namespace specmicp

Event Timeline