Page MenuHomec4science

jacobian_approximation.hpp
No OneTemporary

File Metadata

Created
Sat, Oct 5, 13:59

jacobian_approximation.hpp

/* =============================================================================
Copyright (c) 2014-2017 F. Georget <fabieng@princeton.edu> Princeton University
Copyright (c) 2017 F. Georget <fabien.georget@epfl.ch> EPFL
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. *
============================================================================= */
#ifndef REACTMICP_CHLORIDE_JACOBAPPROXIMATION_HPP
#define REACTMICP_CHLORIDE_JACOBAPPROXIMATION_HPP
#include "specmicp_common/types.hpp"
#include "variablesfwd.hpp"
#include "variables.hpp"
#include <memory>
#include "specmicp_common/compat.hpp"
#include <functional>
#include "specmicp/adimensional/adimensional_system_solution.hpp"
namespace specmicp {
struct AdimensionalSystemConstraints;
namespace micpsolver {
enum class MiCPSolverReturnCode;
} // namespace micpsolver
namespace reactmicp {
namespace systems {
namespace chloride {
using specmicp_solver_f = std::function<micpsolver::MiCPSolverReturnCode (index_t, const AdimensionalSystemConstraints&, const AdimensionalSystemSolution&, AdimensionalSystemSolution)>;
using specmicp_constraints_f = std::function<AdimensionalSystemConstraints (index_t, ChlorideVariables*)>;
//! \brief Return code for the jacobian approximation
enum class ApproxReturnCode {
Ok,
Error
};
//! \brief Abstract base class for a Jacobian approximation
//!
//! This class is the interface used when computing the jacobian of the transport program
class JacobianApproximation
{
public:
virtual ~JacobianApproximation() {}
//! \brief Perturb the adim solution to compute the perturbed residual
//!
//! \param component index of the component in the database
//! \param perturbation numerical perturbation applied to variable
//! \param dt timestep
//! \param sol the solution to perturb
//!
//! The modification is *in-place*
//!
//! \sa JacobianApproximation::restore
virtual ApproxReturnCode perturb(
index_t node,
index_t component,
scalar_t perturbation,
scalar_t dt,
AdimensionalSystemSolution& sol
) = 0;
//! \brief Restore the adim solution after the jacobian calculation
//!
//! \param sol the solution to restore
//!
//! sol should be the same as before the call to perturb
//!
//! \sa JacobianApproximation::perturb
virtual void restore(AdimensionalSystemSolution& sol) = 0;
};
class NoApproximation:
public JacobianApproximation
{
public:
static std::unique_ptr<JacobianApproximation> make(ChlorideVariablesPtr vars) {
return make_unique<NoApproximation>(vars);
}
~NoApproximation()= default;
NoApproximation(ChlorideVariablesPtr vars):
m_vars(vars)
{}
void register_solver(const specmicp_solver_f& func) {
m_solver = func;
}
void register_constraints(const specmicp_constraints_f& func) {
m_get_constraints = func;
}
ApproxReturnCode perturb(
index_t node,
index_t component,
scalar_t perturbation,
scalar_t dt,
AdimensionalSystemSolution& sol) override;
void restore(AdimensionalSystemSolution& sol) override;
private:
ChlorideVariablesPtr m_vars;
AdimensionalSystemSolution m_sol_cache;
specmicp_solver_f m_solver;
specmicp_constraints_f m_get_constraints;
};
class DominantSpeciesApproximation:
public JacobianApproximation
{
public:
static std::unique_ptr<JacobianApproximation> make(ChlorideVariablesPtr vars) {
return make_unique<DominantSpeciesApproximation>(vars);
}
DominantSpeciesApproximation(ChlorideVariablesPtr vars):
m_vars(vars)
{}
ApproxReturnCode perturb(
index_t node,
index_t component,
scalar_t perturbation,
scalar_t dt,
AdimensionalSystemSolution& sol) override;
void restore(AdimensionalSystemSolution& sol) override;
private:
ChlorideVariablesPtr m_vars;
};
} // namespace chloride
} // namespace systems
} // namespace reactmicp
} // namespace specmicp
#endif // REACTMICP_CHLORIDE_JACOBAPPROXIMATION_HPP

Event Timeline