Program Listing for File convergence_measure.hpp

Return to documentation for file (cosm/convergence/convergence_measure.hpp)

#pragma once

/*******************************************************************************
 * Includes
 ******************************************************************************/
#include <algorithm>
#include <cmath>
#include <limits>

#include "rcppsw/math/expression.hpp"
#include "rcppsw/math/normalize.hpp"
#include "rcppsw/rcppsw.hpp"

/*******************************************************************************
 * Namespaces/Decls
 ******************************************************************************/
namespace cosm::convergence {

/*******************************************************************************
 * Class Definitions
 ******************************************************************************/
class convergence_measure : public rmath::expression<double> {
 public:
  explicit convergence_measure(double epsilon) : mc_epsilon(epsilon) {}

  void update_raw(double value) {
    m_raw = value;
    m_raw_min = std::min(m_raw, m_raw_min);
    m_raw_max = std::max(m_raw, m_raw_max);
  }
  double raw(void) const { return m_raw; }
  double raw_min(void) const { return m_raw_min; }
  double raw_max(void) const { return m_raw_max; }

  void set_norm(double value) { eval(value); }

  void reset(void) {
    rmath::expression<double>::reset();
    m_raw = 0.0;
    m_raw_min = std::numeric_limits<double>::max();
    m_raw_max = std::numeric_limits<double>::min();
    m_converged = false;
    m_norm_prev = 0.0;
  }

  bool update_convergence_state(void) {
    m_converged = (std::fabs(v() - m_norm_prev) <= mc_epsilon);
    m_norm_prev = v();
    return m_converged;
  }

  bool converged(void) const { return m_converged; }

 private:
  /* clang-format off */
  const double mc_epsilon{0.0};

  double m_norm_prev{0.0};
  bool   m_converged{false};

  double m_raw{0.0};
  double m_raw_min{std::numeric_limits<double>::max()};
  double m_raw_max{std::numeric_limits<double>::min()};
  /* clang-format on */
};

} /* namespace cosm::convergence */