Page MenuHomec4science

diffusion.cpp
No OneTemporary

File Metadata

Created
Mon, Jul 22, 22:20

diffusion.cpp

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

Event Timeline