Page MenuHomec4science

transport_stagger.cpp
No OneTemporary

File Metadata

Created
Tue, May 28, 15:33

transport_stagger.cpp

/*-------------------------------------------------------------------------------
Copyright (c) 2014,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 "transport_stagger.hpp"
#include "variables.hpp"
#include "../../solver/staggers_base/stagger_structs.hpp"
namespace specmicp {
namespace reactmicp {
namespace systems {
namespace satdiff {
// class SaturatedTransportStagger::
namespace internal {
//! \brief Translate a ParabolicDriverReturnCode to a StaggerReturnCode
solver::StaggerReturnCode Parabolic2StaggerReturnCode(dfpmsolver::ParabolicDriverReturnCode retcode)
{
using ParRC = dfpmsolver::ParabolicDriverReturnCode;
using StaRC = solver::StaggerReturnCode;
switch (retcode)
{
case ParRC::ResidualMinimized:
return StaRC::ResidualMinimized;
break;
case ParRC::ErrorMinimized:
return StaRC::ErrorMinimized;
break;
default: // error
switch(retcode)
{
case ParRC::MaxIterations:
return StaRC::MaximumIterationsReached;
break;
case ParRC::StationaryPoint:
return StaRC::StationaryPoint;
break;
case ParRC::NotConvergedYet:
return StaRC::NotConvergedYet;
break;
default:
return StaRC::UnknownError;
}
}
}
} // end namespace internal
SaturatedTransportStagger::SaturatedTransportStagger(SaturatedVariablesPtr variables,
std::vector<index_t> list_fixed_nodes):
m_program(variables, list_fixed_nodes),
m_solver(m_program)
{
}
SaturatedTransportStagger::SaturatedTransportStagger(
SaturatedVariablesPtr variables,
std::vector<index_t> list_fixed_nodes,
std::map<index_t, index_t> list_slave_nodes,
std::vector<index_t> list_immobile_components):
m_program(variables, list_fixed_nodes, list_slave_nodes, list_immobile_components),
m_solver(m_program)
{
}
//! \brief Initialize the stagger at the beginning of an iteration
void SaturatedTransportStagger::initialize_timestep(scalar_t dt, VariablesBasePtr var)
{
m_dt = dt;
SaturatedVariablesPtr true_var = cast_var_from_base(var);
true_var->predictor() = true_var->displacement();
true_var->velocity().setZero();
true_var->transport_rate().setZero();
m_program.set_variables(true_var);
m_solver.initialize_timestep(dt, true_var->displacement());
// TODO : flag instead of copy ?
Vector tmp(Vector::Zero(true_var->chemistry_rate().rows()));
tmp.swap(true_var->chemistry_rate());
Eigen::VectorXd residuals;
m_program.compute_residuals(true_var->displacement(), true_var->velocity(), residuals);
m_residual_0 = residuals.norm();
true_var->chemistry_rate().swap(tmp);
}
//! \brief Solve the equation for the timestep
solver::StaggerReturnCode SaturatedTransportStagger::restart_timestep(VariablesBasePtr var)
{
SaturatedVariablesPtr true_var = cast_var_from_base(var);
m_solver.velocity() = true_var->velocity();
dfpmsolver::ParabolicDriverReturnCode retcode = m_solver.restart_timestep(true_var->displacement());
// copy variables if successful
if (retcode > dfpmsolver::ParabolicDriverReturnCode::NotConvergedYet)
{
true_var->velocity() = m_solver.velocity();
}
return internal::Parabolic2StaggerReturnCode(retcode);
}
scalar_t SaturatedTransportStagger::get_update(VariablesBasePtr var)
{
return cast_var_from_base(var)->velocity().norm();
}
//! \brief Compute the residuals norm
scalar_t SaturatedTransportStagger::get_residual(VariablesBasePtr var)
{
SaturatedVariablesPtr true_var = cast_var_from_base(var);
Eigen::VectorXd residuals;
m_program.compute_residuals(true_var->displacement(), true_var->velocity(), residuals);
return residuals.norm();
}
} // end namespace satdiff
} // end namespace systems
} // end namespace reactmicp
} // end namespace specmicp

Event Timeline