Page MenuHomec4science

pressure_equation.cpp
No OneTemporary

File Metadata

Created
Sat, Jul 6, 10:28

pressure_equation.cpp

/*-------------------------------------------------------------------------------
Copyright (c) 2015 F. 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:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. 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.
3. Neither the name of the copyright holder 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 HOLDER 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 "pressure_equation.hpp"
#include "transport_constraints.hpp"
#include "../../../dfpm/meshes/mesh1d.hpp"
#include "../../../utils/compat.hpp"
#include "variables_box.hpp"
#include "../../../physics/constants.hpp"
#include <iostream>
namespace specmicp {
namespace reactmicp {
namespace systems {
namespace unsaturated {
struct SPECMICP_DLL_LOCAL PressureEquation::PressureEquationImpl
{
PressureVariableBox m_vars;
std::vector<index_t> m_ideq;
mesh::Mesh1DPtr m_mesh;
scalar_t m_scaling {1.0};
mesh::Mesh1D* mesh() {return m_mesh.get();}
PressureVariableBox& vars() {return m_vars;}
bool node_has_equation(index_t node) {
return m_ideq[node] != no_equation;
}
index_t id_equation(index_t node) {
return m_ideq[node];
}
void fix_node(index_t node) {
m_ideq[node] = no_equation;
}
PressureEquationImpl(
mesh::Mesh1DPtr the_mesh,
PressureVariableBox& the_vars
):
m_vars(the_vars),
m_ideq(the_mesh->nb_nodes(), -5),
m_mesh(the_mesh)
{}
void compute_transport_rate(scalar_t dt, const Vector& displacement);
};
PressureEquation::PressureEquation(
mesh::Mesh1DPtr the_mesh,
PressureVariableBox& variables,
const TransportConstraints& constraints
):
m_tot_ndf(the_mesh->nb_nodes()),
m_impl(make_unique<PressureEquationImpl>(the_mesh, variables))
{
number_equations(constraints);
}
PressureEquation::~PressureEquation() = default;
void PressureEquation::set_scaling(scalar_t value)
{
m_impl->m_scaling = value;
}
index_t PressureEquation::id_equation(index_t id_dof)
{
return m_impl->id_equation(id_dof);
}
//! \brief Compute the residuals inside 'element'
void PressureEquation::residuals_element(
index_t element,
const Vector& displacement,
const Vector& velocity,
Eigen::Vector2d& element_residual,
bool use_chemistry_rate
)
{
element_residual.setZero();
mesh::Mesh1D* m_mesh = m_impl->mesh();
PressureVariableBox& vars = m_impl->m_vars;
const scalar_t& rt = vars.constants.rt;
const scalar_t mass_coeff_0 = m_mesh->get_volume_cell_element(element, 0);
const scalar_t mass_coeff_1 = m_mesh->get_volume_cell_element(element, 1);
const index_t node_0 = m_mesh->get_node(element, 0);
const index_t node_1 = m_mesh->get_node(element, 1);
// Diffusion Cw
const scalar_t coeff_diff_0 = vars.resistance_gas_diffusivity(node_0)
* vars.relative_gas_diffusivity(node_0);
const scalar_t coeff_diff_1 = vars.resistance_gas_diffusivity(node_1)
* vars.relative_gas_diffusivity(node_1);
//const scalar_t coeff_diff = vars.binary_diffusion_coefficient * 2.0 / (
// 1.0/coeff_diff_0 + 1.0/coeff_diff_1 );
const scalar_t coeff_diff = vars.binary_diffusion_coefficient * (
coeff_diff_0 + coeff_diff_1 )/2;
const scalar_t diff_flux = coeff_diff*(
displacement(node_1) - displacement(node_0))
/ m_mesh->get_dx(element)
/ rt;
// Tot flux
const scalar_t tot_flux = m_mesh->get_face_area(element)*(diff_flux);
const scalar_t flux_0 = tot_flux;
const scalar_t flux_1 = -tot_flux;
// transient
if (m_impl->node_has_equation(node_0))
{
const scalar_t porosity_0 = vars.porosity(node_0);
const scalar_t pressure_0 = displacement(node_0);
const scalar_t saturation_0 = 1.0 - vars.liquid_saturation(node_0);
const scalar_t transient_0 = mass_coeff_0/rt * (
porosity_0 * saturation_0 * velocity(node_0)
+ saturation_0 * pressure_0 * vars.porosity.velocity(node_0)
- porosity_0 * pressure_0 * vars.liquid_saturation.velocity(node_0)
);
auto res = transient_0 - flux_0;
if (use_chemistry_rate)
{
res += mass_coeff_0*vars.partial_pressure.chemistry_rate(node_0);
}
element_residual(0) = res/m_impl->m_scaling;
}
if (m_impl->node_has_equation(node_1))
{
const scalar_t porosity_1 = vars.porosity(node_1);
const scalar_t pressure_1 = displacement(node_1);
const scalar_t saturation_1 = 1.0 - vars.liquid_saturation(node_1);
const scalar_t transient_1 = mass_coeff_1/rt * (
porosity_1 * saturation_1 * velocity(node_1)
+ saturation_1 * pressure_1 * vars.porosity.velocity(node_1)
- porosity_1 * pressure_1 * vars.liquid_saturation.velocity(node_1)
);
auto res = transient_1 - flux_1;
if (use_chemistry_rate)
{
res += mass_coeff_1*vars.partial_pressure.chemistry_rate(node_1);
}
element_residual(1) = res/m_impl->m_scaling;
}
}
//! \brief Compute the residuals
void PressureEquation::compute_residuals(
const Vector& displacement,
const Vector& velocity,
Vector& residuals,
bool use_chemistry_rate
)
{
mesh::Mesh1D* m_mesh = m_impl->mesh();
residuals.setZero(get_neq());
Eigen::Vector2d element_residual;
for(index_t element: m_mesh->range_elements())
{
residuals_element(element, displacement, velocity, element_residual, use_chemistry_rate);
const index_t node_0 = m_mesh->get_node(element, 0);
if (m_impl->node_has_equation(node_0))
{
residuals(m_impl->id_equation(node_0)) += element_residual(0);
}
const index_t node_1 = m_mesh->get_node(element, 1);
if (m_impl->node_has_equation(node_1))
{
residuals(m_impl->id_equation(node_1)) += element_residual(1);
}
}
}
//! \brief Compute the jacobian
void PressureEquation::compute_jacobian(Vector& displacement,
Vector& velocity,
Eigen::SparseMatrix<scalar_t>& jacobian,
scalar_t alphadt
)
{
mesh::Mesh1D* m_mesh = m_impl->mesh();
dfpm::list_triplet_t jacob;
const index_t estimation = 3*get_neq();
jacob.reserve(estimation);
// assume relative variables are set
for (index_t element: m_mesh->range_elements())
{
Eigen::Vector2d element_residual_orig;
residuals_element(element, displacement, velocity, element_residual_orig, false);
for (index_t enodec=0; enodec<2; ++enodec)
{
const index_t nodec = m_mesh->get_node(element, enodec);
if (not m_impl->node_has_equation(nodec)) continue;
const scalar_t tmp_d = displacement(nodec);
const scalar_t tmp_v = velocity(nodec);
scalar_t h = eps_jacobian*std::abs(tmp_v);
if (h<1e-4*eps_jacobian) h = eps_jacobian;
velocity(nodec) = tmp_v + h;
h = velocity(nodec) - tmp_v;
displacement(nodec) = tmp_d + alphadt*h;
Eigen::Vector2d element_residual;
residuals_element(element, displacement, velocity, element_residual, false);
displacement(nodec) = tmp_d;
velocity(nodec) = tmp_v;
for (index_t enoder=0; enoder<2; ++enoder)
{
const index_t noder = m_mesh->get_node(element, enoder);
if (not m_impl->node_has_equation(noder)) continue;
jacob.push_back(dfpm::triplet_t(
m_impl->id_equation(noder),
m_impl->id_equation(nodec),
(element_residual(enoder) - element_residual_orig(enoder))/h
));
}
}
}
jacobian = Eigen::SparseMatrix<scalar_t>(get_neq(), get_neq());
jacobian.setFromTriplets(jacob.begin(), jacob.end());
}
//! \brief Update the solution
void PressureEquation::update_solution(const Vector& update,
scalar_t lambda,
scalar_t alpha_dt,
Vector& predictor,
Vector& displacement,
Vector& velocity)
{
for (index_t node: m_impl->mesh()->range_nodes())
{
if (m_impl->node_has_equation(node))
{
velocity(node) += lambda*update(m_impl->id_equation(node));
}
}
displacement = predictor + alpha_dt*velocity;
m_impl->compute_transport_rate(alpha_dt, displacement);
}
void PressureEquation::number_equations(const TransportConstraints& constraints)
{
for (int fixed: constraints.fixed_nodes())
{
m_impl->fix_node(fixed);
}
index_t neq = 0;
for (index_t node: m_impl->mesh()->range_nodes())
{
if (m_impl->m_ideq[node] == -5)
{
m_impl->m_ideq[node] = neq;
++neq;
}
}
m_neq = neq;
}
void PressureEquation::PressureEquationImpl::compute_transport_rate(
scalar_t dt,
const Vector& displacement)
{
const scalar_t& rt = m_vars.constants.rt;
const MainVariable& saturation = m_vars.liquid_saturation;
MainVariable& pressure = m_vars.partial_pressure;
const SecondaryTransientVariable& porosity = m_vars.porosity;
for (index_t node: m_mesh->range_nodes())
{
if (! node_has_equation(node)) continue;
const scalar_t transient = (
(porosity(node)*(1.0-saturation(node))*displacement(node))
- (porosity.predictor(node)*(1.0-saturation.predictor(node))*pressure.predictor(node))
) / (rt*dt);
const scalar_t chem_rates = (
- pressure.chemistry_rate(node)
);
pressure.transport_fluxes(node) = transient - chem_rates;
}
}
} //end namespace unsaturated
} //end namespace systems
} //end namespace reactmicp
} //end namespace specmicp

Event Timeline