<commit_before>#include "iteration/outer/outer_iteration.hpp"

#include "convergence/status.hpp"

namespace bart::iteration::outer {

template <typename ConvergenceType>
OuterIteration<ConvergenceType>::OuterIteration(std::unique_ptr<GroupIterator> group_iterator_ptr,
                                                std::unique_ptr<ConvergenceChecker> convergence_checker_ptr)
    : group_iterator_ptr_(std::move(group_iterator_ptr)),
      convergence_checker_ptr_(std::move(convergence_checker_ptr)) {
  AssertThrow(group_iterator_ptr_ != nullptr,
              dealii::ExcMessage("GroupSolveIteration pointer passed to OuterIteration constructor is null"))

  AssertThrow(convergence_checker_ptr_ != nullptr,
              dealii::ExcMessage("Convergence checker pointer passed to OuterIteration constructor is null"))
}

template <typename ConvergenceType>
void OuterIteration<ConvergenceType>::IterateToConvergence(system::System &system) {
  bool is_complete{ false };
  this->convergence_checker_ptr_->Reset();
  do {
    is_complete = Iterate(system);
    if (post_iteration_subroutine_ptr_ != nullptr) {
      data_names::StatusPort::Expose("===================== COMMENCING SUBROUTINE =====================\n");
      post_iteration_subroutine_ptr_->Execute(system);
      data_names::StatusPort::Expose("===================== COMPLETED SUBROUTINE  =====================\n");
    }
    ExposeIterationData(system);
  } while (!is_complete);
}

template <typename ConvergenceType>
void OuterIteration<ConvergenceType>::InnerIterationToConvergence(system::System &system) {
  group_iterator_ptr_->Iterate(system);
}

template<typename ConvergenceType>
auto OuterIteration<ConvergenceType>::Iterate(system::System &system) -> bool {
  for (int group = 0; group < system.total_groups; ++group) {
    for (int angle = 0; angle < system.total_angles; ++angle) {
      UpdateSystem(system, group, angle);
    }
  }

  InnerIterationToConvergence(system);

  auto convergence_status = CheckConvergence(system);
  if (convergence_status.delta.has_value()) {
    data_names::IterationErrorPort::Expose({convergence_status.iteration_number,
                                            convergence_status.delta.value()});
  }

  data_names::StatusPort::Expose("Outer iteration Status: ");
  data_names::ConvergenceStatusPort::Expose(convergence_status);

  return convergence_status.is_complete;
}

template<typename ConvergenceType>
auto OuterIteration<ConvergenceType>::ExposeIterationData(system::System &system) -> void {
  if (system.current_moments != nullptr) {
    data_names::SolutionMomentsPort::Expose(*system.current_moments);
    data_names::ScalarFluxPort::Expose(system.current_moments->GetMoment({0, 0, 0}));
  }

  if (system.right_hand_side_ptr_ != nullptr) {
    auto scattering_source_ptr =
        system.right_hand_side_ptr_->GetVariableTermPtr(0, system::terms::VariableLinearTerms::kScatteringSource);
    if (scattering_source_ptr != nullptr) {
      dealii::Vector<double> scattering_source(*scattering_source_ptr);
      data_names::ScatteringSourcePort::Expose(scattering_source);
    }
    auto fission_source_ptr =
        system.right_hand_side_ptr_->GetVariableTermPtr(0, system::terms::VariableLinearTerms::kFissionSource);
    if (fission_source_ptr != nullptr) {
      dealii::Vector<double> fission_source(*fission_source_ptr);
      data_names::FissionSourcePort::Expose(fission_source);
    }
  }
}

template class OuterIteration<double>;

} // namespace bart::iteration::outer<commit_msg>added check to outer iteration to verify VariableTerms are present before accessing them and exposing<commit_after>#include "iteration/outer/outer_iteration.hpp"

#include "convergence/status.hpp"

namespace bart::iteration::outer {

template <typename ConvergenceType>
OuterIteration<ConvergenceType>::OuterIteration(std::unique_ptr<GroupIterator> group_iterator_ptr,
                                                std::unique_ptr<ConvergenceChecker> convergence_checker_ptr)
    : group_iterator_ptr_(std::move(group_iterator_ptr)),
      convergence_checker_ptr_(std::move(convergence_checker_ptr)) {
  AssertThrow(group_iterator_ptr_ != nullptr,
              dealii::ExcMessage("GroupSolveIteration pointer passed to OuterIteration constructor is null"))

  AssertThrow(convergence_checker_ptr_ != nullptr,
              dealii::ExcMessage("Convergence checker pointer passed to OuterIteration constructor is null"))
}

template <typename ConvergenceType>
void OuterIteration<ConvergenceType>::IterateToConvergence(system::System &system) {
  bool is_complete{ false };
  this->convergence_checker_ptr_->Reset();
  do {
    is_complete = Iterate(system);
    if (post_iteration_subroutine_ptr_ != nullptr) {
      data_names::StatusPort::Expose("===================== COMMENCING SUBROUTINE =====================\n");
      post_iteration_subroutine_ptr_->Execute(system);
      data_names::StatusPort::Expose("===================== COMPLETED SUBROUTINE  =====================\n");
    }
    ExposeIterationData(system);
  } while (!is_complete);
}

template <typename ConvergenceType>
void OuterIteration<ConvergenceType>::InnerIterationToConvergence(system::System &system) {
  group_iterator_ptr_->Iterate(system);
}

template<typename ConvergenceType>
auto OuterIteration<ConvergenceType>::Iterate(system::System &system) -> bool {
  for (int group = 0; group < system.total_groups; ++group) {
    for (int angle = 0; angle < system.total_angles; ++angle) {
      UpdateSystem(system, group, angle);
    }
  }

  InnerIterationToConvergence(system);

  auto convergence_status = CheckConvergence(system);
  if (convergence_status.delta.has_value()) {
    data_names::IterationErrorPort::Expose({convergence_status.iteration_number,
                                            convergence_status.delta.value()});
  }

  data_names::StatusPort::Expose("Outer iteration Status: ");
  data_names::ConvergenceStatusPort::Expose(convergence_status);

  return convergence_status.is_complete;
}

template<typename ConvergenceType>
auto OuterIteration<ConvergenceType>::ExposeIterationData(system::System &system) -> void {
  if (system.current_moments != nullptr) {
    data_names::SolutionMomentsPort::Expose(*system.current_moments);
    data_names::ScalarFluxPort::Expose(system.current_moments->GetMoment({0, 0, 0}));
  }

  if (system.right_hand_side_ptr_ != nullptr) {
    auto variable_terms = system.right_hand_side_ptr_->GetVariableTerms();

    if (variable_terms.contains(system::terms::VariableLinearTerms::kScatteringSource)) {
      auto scattering_source_ptr =
          system.right_hand_side_ptr_->GetVariableTermPtr(0, system::terms::VariableLinearTerms::kScatteringSource);
      if (scattering_source_ptr != nullptr) {
        dealii::Vector<double> scattering_source(*scattering_source_ptr);
        data_names::ScatteringSourcePort::Expose(scattering_source);
      }
    }
    if (variable_terms.contains(system::terms::VariableLinearTerms::kFissionSource)) {
      auto fission_source_ptr =
          system.right_hand_side_ptr_->GetVariableTermPtr(0, system::terms::VariableLinearTerms::kFissionSource);
      if (fission_source_ptr != nullptr) {
        dealii::Vector<double> fission_source(*fission_source_ptr);
        data_names::FissionSourcePort::Expose(fission_source);
      }
    }
  }
}

template class OuterIteration<double>;

} // namespace bart::iteration::outer
{'fraction_non_alphanumeric': 0.13043478260869565, 'fraction_numerical': 0.00188450666307713, 'mean_word_length': 5.416234887737478, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 52, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3733949', 'n_tokens_mistral': 2248, 'n_tokens_neox': 2134, 'n_words': 318}