<commit_before>#include "openmc/scattdata.h"

#include <algorithm>
#include <numeric>
#include <cmath>

#include "xtensor/xbuilder.hpp"

#include "openmc/constants.h"
#include "openmc/error.h"
#include "openmc/math_functions.h"
#include "openmc/random_lcg.h"
#include "openmc/settings.h"

namespace openmc {

//==============================================================================
// ScattData base-class methods
//==============================================================================

void
ScattData::base_init(int order, const xt::xtensor<int, 1>& in_gmin,
     const xt::xtensor<int, 1>& in_gmax, const double_2dvec& in_energy,
     const double_2dvec& in_mult)
{
  size_t groups = in_energy.size();

  gmin = in_gmin;
  gmax = in_gmax;
  energy.resize(groups);
  mult.resize(groups);
  dist.resize(groups);

  for (int gin = 0; gin < groups; gin++) {
    // Store the inputted data
    energy[gin] = in_energy[gin];
    mult[gin] = in_mult[gin];

    // Make sure the energy is normalized
    double norm = std::accumulate(energy[gin].begin(), energy[gin].end(), 0.);

    if (norm != 0.) {
      for (auto& n : energy[gin]) n /= norm;
    }

    // Initialize the distribution data
    dist[gin].resize(in_gmax[gin] - in_gmin[gin] + 1);
    for (auto& v : dist[gin]) {
      v.resize(order);
    }
  }
}

//==============================================================================

void
ScattData::base_combine(size_t max_order,
     const std::vector<ScattData*>& those_scatts, const std::vector<double>& scalars,
     xt::xtensor<int, 1>& in_gmin, xt::xtensor<int, 1>& in_gmax, double_2dvec& sparse_mult,
     double_3dvec& sparse_scatter)
{
  size_t groups = those_scatts[0] -> energy.size();

  // Now allocate and zero our storage spaces
  xt::xtensor<double, 3> this_matrix({groups, groups, max_order}, 0.);
  xt::xtensor<double, 2> mult_numer({groups, groups}, 0.);
  xt::xtensor<double, 2> mult_denom({groups, groups}, 0.);

  // Build the dense scattering and multiplicity matrices
  // Get the multiplicity_matrix
  // To combine from nuclidic data we need to use the final relationship
  // mult_{gg'} = sum_i(N_i*nuscatt_{i,g,g'}) /
  //              sum_i(N_i*(nuscatt_{i,g,g'} / mult_{i,g,g'}))
  // Developed as follows:
  // mult_{gg'} = nuScatt{g,g'} / Scatt{g,g'}
  // mult_{gg'} = sum_i(N_i*nuscatt_{i,g,g'}) / sum(N_i*scatt_{i,g,g'})
  // mult_{gg'} = sum_i(N_i*nuscatt_{i,g,g'}) /
  //              sum_i(N_i*(nuscatt_{i,g,g'} / mult_{i,g,g'}))
  // nuscatt_{i,g,g'} can be reconstructed from the energy and scattxs member
  // variables
  for (int i = 0; i < those_scatts.size(); i++) {
    ScattData* that = those_scatts[i];

    // Build the dense matrix for that object
    xt::xtensor<double, 3> that_matrix = that->get_matrix(max_order);

    // Now add that to this for the scattering and multiplicity
    for (int gin = 0; gin < groups; gin++) {
      // Only spend time adding that's gmin to gmax data since the rest will
      // be zeros
      int i_gout = 0;
      for (int gout = that->gmin(gin); gout <= that->gmax(gin); gout++) {
        // Do the scattering matrix
        for (int l = 0; l < max_order; l++) {
          this_matrix(gin, gout, l) += scalars[i] * that_matrix(gin, gout, l);
        }

        // Incorporate that's contribution to the multiplicity matrix data
        double nuscatt = that->scattxs(gin) * that->energy[gin][i_gout];
        mult_numer(gin, gout) += scalars[i] * nuscatt;
        if (that->mult[gin][i_gout] > 0.) {
          mult_denom(gin, gout) += scalars[i] * nuscatt / that->mult[gin][i_gout];
        } else {
          mult_denom(gin, gout) += scalars[i];
        }
        i_gout++;
      }
    }
  }

  // Combine mult_numer and mult_denom into the combined multiplicity matrix
  xt::xtensor<double, 2> this_mult({groups, groups}, 1.);
  this_mult = xt::nan_to_num(mult_numer / mult_denom);

  // We have the data, now we need to convert to a jagged array and then use
  // the initialize function to store it on the object.
  for (int gin = 0; gin < groups; gin++) {
    // Find the minimum and maximum group boundaries
    int gmin_;
    for (gmin_ = 0; gmin_ < groups; gmin_++) {
      bool non_zero = false;
      for (int l = 0; l < this_matrix.shape()[2]; l++) {
        if (this_matrix(gin, gmin_, l) != 0.) {
          non_zero = true;
          break;
        }
      }
      if (non_zero) break;
    }
    int gmax_;
    for (gmax_ = groups - 1; gmax_ >= 0; gmax_--) {
      bool non_zero = false;
      for (int l = 0; l < this_matrix.shape()[2]; l++) {
        if (this_matrix(gin, gmax_, l) != 0.) {
          non_zero = true;
          break;
        }<commit_msg>start with initial value<commit_after>#include "openmc/scattdata.h"

#include <algorithm>
#include <numeric>
#include <cmath>

#include "xtensor/xbuilder.hpp"

#include "openmc/constants.h"
#include "openmc/error.h"
#include "openmc/math_functions.h"
#include "openmc/random_lcg.h"
#include "openmc/settings.h"

namespace openmc {

//==============================================================================
// ScattData base-class methods
//==============================================================================

void
ScattData::base_init(int order, const xt::xtensor<int, 1>& in_gmin,
     const xt::xtensor<int, 1>& in_gmax, const double_2dvec& in_energy,
     const double_2dvec& in_mult)
{
  size_t groups = in_energy.size();

  gmin = in_gmin;
  gmax = in_gmax;
  energy.resize(groups);
  mult.resize(groups);
  dist.resize(groups);

  for (int gin = 0; gin < groups; gin++) {
    // Store the inputted data
    energy[gin] = in_energy[gin];
    mult[gin] = in_mult[gin];

    // Make sure the energy is normalized
    double norm = std::accumulate(energy[gin].begin(), energy[gin].end(), 0.);

    if (norm != 0.) {
      for (auto& n : energy[gin]) n /= norm;
    }

    // Initialize the distribution data
    dist[gin].resize(in_gmax[gin] - in_gmin[gin] + 1);
    for (auto& v : dist[gin]) {
      v.resize(order);
    }
  }
}

//==============================================================================

void
ScattData::base_combine(size_t max_order,
     const std::vector<ScattData*>& those_scatts, const std::vector<double>& scalars,
     xt::xtensor<int, 1>& in_gmin, xt::xtensor<int, 1>& in_gmax, double_2dvec& sparse_mult,
     double_3dvec& sparse_scatter)
{
  size_t groups = those_scatts[0] -> energy.size();

  // Now allocate and zero our storage spaces
  xt::xtensor<double, 3> this_matrix({groups, groups, max_order}, 0.);
  xt::xtensor<double, 2> mult_numer({groups, groups}, 0.);
  xt::xtensor<double, 2> mult_denom({groups, groups}, 0.);
  // TODO: Need to review this:
  if (this->scattxs.size() > 0) {
    this_matrix = this->get_matrix(max_order);
  }
  // Build the dense scattering and multiplicity matrices
  // Get the multiplicity_matrix
  // To combine from nuclidic data we need to use the final relationship
  // mult_{gg'} = sum_i(N_i*nuscatt_{i,g,g'}) /
  //              sum_i(N_i*(nuscatt_{i,g,g'} / mult_{i,g,g'}))
  // Developed as follows:
  // mult_{gg'} = nuScatt{g,g'} / Scatt{g,g'}
  // mult_{gg'} = sum_i(N_i*nuscatt_{i,g,g'}) / sum(N_i*scatt_{i,g,g'})
  // mult_{gg'} = sum_i(N_i*nuscatt_{i,g,g'}) /
  //              sum_i(N_i*(nuscatt_{i,g,g'} / mult_{i,g,g'}))
  // nuscatt_{i,g,g'} can be reconstructed from the energy and scattxs member
  // variables
  for (int i = 0; i < those_scatts.size(); i++) {
    ScattData* that = those_scatts[i];

    // Build the dense matrix for that object
    xt::xtensor<double, 3> that_matrix = that->get_matrix(max_order);

    // Now add that to this for the scattering and multiplicity
    for (int gin = 0; gin < groups; gin++) {
      // Only spend time adding that's gmin to gmax data since the rest will
      // be zeros
      int i_gout = 0;
      for (int gout = that->gmin(gin); gout <= that->gmax(gin); gout++) {
        // Do the scattering matrix
        for (int l = 0; l < max_order; l++) {
          this_matrix(gin, gout, l) += scalars[i] * that_matrix(gin, gout, l);
        }

        // Incorporate that's contribution to the multiplicity matrix data
        double nuscatt = that->scattxs(gin) * that->energy[gin][i_gout];
        mult_numer(gin, gout) += scalars[i] * nuscatt;
        if (that->mult[gin][i_gout] > 0.) {
          mult_denom(gin, gout) += scalars[i] * nuscatt / that->mult[gin][i_gout];
        } else {
          mult_denom(gin, gout) += scalars[i];
        }
        i_gout++;
      }
    }
  }

  // Combine mult_numer and mult_denom into the combined multiplicity matrix
  xt::xtensor<double, 2> this_mult({groups, groups}, 1.);
  // TODO: Need to check this too
  xt::xtensor<double, 2> this_mult({groups, groups}, 1.);
  for (int gin = 0; gin < groups; gin++) {
    for (int gout = 0; gout < groups; gout++) {
      if (std::abs(mult_denom(gin, gout)) > 0.0) {
	   this_mult(gin, gout) = mult_numer(gin, gout) / mult_denom(gin, gout);
      } else {
	   if (mult_numer(gin, gout) == 0.0) {
	     this_mult(gin, gout) = 1.0;
	   }
      }
    }
  }

  // We have the data, now we need to convert to a jagged array and then use
  // the initialize function to store it on the object.
  for (int gin = 0; gin < groups; gin++) {
    // Find the minimum and maximum group boundaries
    int gmin_;
    for (gmin_ = 0; gmin_ < groups; gmin_++) {
      bool non_zero = false;
      for (int l = 0; l < this_matrix.shape()[2]; l++) {
        if (this_matrix(gin, gmin_, l) != 0.) {
          non_zero = true;
          break;
        }
      }
      if (non_zero) break;
    }
    int gmax_;
    for (gmax_ = groups - 1; gmax_ >= 0; gmax_--) {
      bool non_zero = false;
      for (int l = 0; l < this_matrix.shape()[2]; l++) {
        if (this_matrix(gin, gmax_, l) != 0.) {
          non_zero = true;
          break;
        }
{'fraction_non_alphanumeric': 0.19572051516073422, 'fraction_numerical': 0.008619815434540108, 'mean_word_length': 3.0203832042397063, 'pattern_counts': {'":': 0, '<': 52, '<?xml version=': 0, '>': 56, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3577913', 'n_tokens_mistral': 3594, 'n_tokens_neox': 3425, 'n_words': 887}