<commit_before>// Material.cpp
// The very central Material class
// -- <NAME>

#include "material.h"


// h5wrap template
template double h5wrap::get_array_index(H5::DataSet *, int, H5::DataType);



/***************************/
/*** Protected Functions ***/
/***************************/

double pyne::Material::get_comp_sum()
{
  // Sums the weights in the composition dictionary
  double sum = 0.0;
  for (pyne::comp_iter i = comp.begin(); i != comp.end(); i++)
  {
    sum = sum + i->second;
  }
  return sum;
};



void pyne::Material::norm_comp()
{
  double sum = get_comp_sum();
  if (sum != 1.0 && sum != 0.0)
  {
    for (comp_iter i = comp.begin(); i != comp.end(); i++)
      i->second = i->second / sum;
  }

  if (mass < 0.0)
    mass = sum;
}






void pyne::Material::_load_comp_protocol0(H5::H5File * db, std::string datapath, int row)
{
  H5::Group matgroup = (*db).openGroup(datapath);
  H5::DataSet nucset;

  double nucvalue;
  hsize_t matG = matgroup.getNumObjs();

  // Iterate over datasets in the group.
  for (int matg = 0; matg < matG; matg++)
  {
    std::string nuckey = matgroup.getObjnameByIdx(matg);
    nucset = matgroup.openDataSet(nuckey);
    nucvalue = h5wrap::get_array_index<double>(&nucset, row);

    if (nuckey == "Mass" || nuckey == "MASS" || nuckey == "mass")
      mass = nucvalue;
    else
      comp[pyne::nucname::zzaaam(nuckey)] = nucvalue;

    nucset.close();
  };

  // Set meta data
  name = datapath.substr(datapath.rfind("/")+1, datapath.length());
  atoms_per_mol = -1.0;
};



void pyne::Material::_load_comp_protocol1(H5::H5File * db, std::string datapath, int row)
{
  H5::DataSet data_set = (*db).openDataSet(datapath);

  hsize_t data_offset[1] = {row};
  if (row < 0)
  {
    // Handle negative row indecies
    H5::DataSpace data_space = data_set.getSpace();
    hsize_t data_dims[1];
    //hsize_t data_max_dims[1];
    //int data_rank = data_space.getSimpleExtentDims(data_dims, data_max_dims);
    int data_rank = data_space.getSimpleExtentDims(data_dims);
    data_offset[0] += data_dims[0];
  };

  // Grab the nucpath
  void * npath;
  H5::Attribute nuc_attr = data_set.openAttribute("nucpath");
  hsize_t nuc_attr_len = nuc_attr.getStorageSize() / sizeof(char);
  H5::StrType nuc_attr_type(0, nuc_attr_len);
  nuc_attr.read(nuc_attr_type, npath);
  std::string nucpath ((char *) npath, ((char *) npath)+nuc_attr_len);

  // Grab the nuclides
  std::vector<int> nuclides = h5wrap::h5_array_to_cpp_vector_1d<int>(db, nucpath, H5::PredType::NATIVE_INT);
  int nuc_size = nuclides.size();
  hsize_t nuc_dims[1] = {nuc_size};

  // Get the data hyperslab
  H5::DataSpace data_hyperslab = data_set.getSpace();
  hsize_t data_count[1] = {1};
  data_hyperslab.selectHyperslab(H5S_SELECT_SET, data_count, data_offset);

  // Get memory space for writing
  H5::DataSpace mem_space (1, data_count);

  // Get material type
  size_t material_struct_size = sizeof(pyne::material_struct) + sizeof(double)*(nuc_size);
  H5::CompType data_desc(material_struct_size);
  H5::ArrayType comp_values_array_type (H5::PredType::NATIVE_DOUBLE, 1, nuc_dims);

  // make the data table type
  data_desc.insertMember("name", HOFFSET(pyne::material_struct, name), H5::StrType(0, 20));
  data_desc.insertMember("mass", HOFFSET(pyne::material_struct, mass), H5::PredType::NATIVE_DOUBLE);
  data_desc.insertMember("atoms_per_mol", HOFFSET(pyne::material_struct, atoms_per_mol), H5::PredType::NATIVE_DOUBLE);
  data_desc.insertMember("comp", HOFFSET(pyne::material_struct, comp), comp_values_array_type);

  std::cout << "The\n";

  // make the data array, have to over-allocate
  material_struct * mat_data  = (material_struct *) malloc(material_struct_size);

  // Finally, get data and put in on this instance
  data_set.read(mat_data, data_desc);

  name = std::string((*mat_data).name);
  mass = (*mat_data).mass;
  atoms_per_mol = (*mat_data).atoms_per_mol;
  for (int i = 0; i < nuc_size; i++)
    comp[nuclides[i]] = (*mat_data).comp[i];

  free(mat_data);
};





void pyne::Material::from_hdf5(char * fchar, char * dchar, int row, int protocol)
{
  std::string filename (fchar);
  std::string datapath (dchar);
  from_hdf5(filename, datapath, row, protocol);  
};



void pyne::Material::from_hdf5(std::string filename, std::string datapath, int row, int protocol)
{
  // Turn off annoying HDF5 errors
  //H5::Exception::dontPrint();<commit_msg>Material hdf5 protocol 1 reader enabled.<commit_after>// Material.cpp
// The very central Material class
// -- <NAME>

#include "material.h"


// h5wrap template
template double h5wrap::get_array_index(H5::DataSet *, int, H5::DataType);



/***************************/
/*** Protected Functions ***/
/***************************/

double pyne::Material::get_comp_sum()
{
  // Sums the weights in the composition dictionary
  double sum = 0.0;
  for (pyne::comp_iter i = comp.begin(); i != comp.end(); i++)
  {
    sum = sum + i->second;
  }
  return sum;
};



void pyne::Material::norm_comp()
{
  double sum = get_comp_sum();
  if (sum != 1.0 && sum != 0.0)
  {
    for (comp_iter i = comp.begin(); i != comp.end(); i++)
      i->second = i->second / sum;
  }

  if (mass < 0.0)
    mass = sum;
}






void pyne::Material::_load_comp_protocol0(H5::H5File * db, std::string datapath, int row)
{
  H5::Group matgroup = (*db).openGroup(datapath);
  H5::DataSet nucset;

  double nucvalue;
  hsize_t matG = matgroup.getNumObjs();

  // Iterate over datasets in the group.
  for (int matg = 0; matg < matG; matg++)
  {
    std::string nuckey = matgroup.getObjnameByIdx(matg);
    nucset = matgroup.openDataSet(nuckey);
    nucvalue = h5wrap::get_array_index<double>(&nucset, row);

    if (nuckey == "Mass" || nuckey == "MASS" || nuckey == "mass")
      mass = nucvalue;
    else
      comp[pyne::nucname::zzaaam(nuckey)] = nucvalue;

    nucset.close();
  };

  // Set meta data
  name = datapath.substr(datapath.rfind("/")+1, datapath.length());
  atoms_per_mol = -1.0;
};



void pyne::Material::_load_comp_protocol1(H5::H5File * db, std::string datapath, int row)
{
  H5::DataSet data_set = (*db).openDataSet(datapath);

  hsize_t data_offset[1] = {row};
  if (row < 0)
  {
    // Handle negative row indecies
    H5::DataSpace data_space = data_set.getSpace();
    hsize_t data_dims[1];
    int data_rank = data_space.getSimpleExtentDims(data_dims);
    data_offset[0] += data_dims[0];
  };

  // Grab the nucpath
  void * npath;
  H5::Attribute nuc_attr = data_set.openAttribute("nucpath");
  hsize_t nuc_attr_len = nuc_attr.getStorageSize() / sizeof(char);
  H5::StrType nuc_attr_type(0, nuc_attr_len);
  nuc_attr.read(nuc_attr_type, npath);
  std::string nucpath ((char *) npath, ((char *) npath)+nuc_attr_len);

  // Grab the nuclides
  std::vector<int> nuclides = h5wrap::h5_array_to_cpp_vector_1d<int>(db, nucpath, H5::PredType::NATIVE_INT);
  int nuc_size = nuclides.size();
  hsize_t nuc_dims[1] = {nuc_size};

  // Get the data hyperslab
  H5::DataSpace data_hyperslab = data_set.getSpace();
  hsize_t data_count[1] = {1};
  data_hyperslab.selectHyperslab(H5S_SELECT_SET, data_count, data_offset);

  // Get memory space for writing
  H5::DataSpace mem_space (1, data_count);

  // Get material type
  size_t material_struct_size = sizeof(pyne::material_struct) + sizeof(double)*(nuc_size);
  H5::CompType data_desc(material_struct_size);
  H5::ArrayType comp_values_array_type (H5::PredType::NATIVE_DOUBLE, 1, nuc_dims);

  // make the data table type
  data_desc.insertMember("name", HOFFSET(pyne::material_struct, name), H5::StrType(0, 20));
  data_desc.insertMember("mass", HOFFSET(pyne::material_struct, mass), H5::PredType::NATIVE_DOUBLE);
  data_desc.insertMember("atoms_per_mol", HOFFSET(pyne::material_struct, atoms_per_mol), H5::PredType::NATIVE_DOUBLE);
  data_desc.insertMember("comp", HOFFSET(pyne::material_struct, comp), comp_values_array_type);

  // make the data array, have to over-allocate
  material_struct * mat_data = (material_struct *) malloc(material_struct_size);

  // Finally, get data and put in on this instance
  data_set.read(mat_data, data_desc, mem_space, data_hyperslab);

  name = std::string((*mat_data).name);
  mass = (*mat_data).mass;
  atoms_per_mol = (*mat_data).atoms_per_mol;
  for (int i = 0; i < nuc_size; i++)
    comp[nuclides[i]] = (double) (*mat_data).comp[i];

  free(mat_data);
};





void pyne::Material::from_hdf5(char * fchar, char * dchar, int row, int protocol)
{
  std::string filename (fchar);
  std::string datapath (dchar);
  from_hdf5(filename, datapath, row, protocol);  
};



void pyne::Material::from_hdf5(std::string filename, std::string datapath, int row, int protocol)
{
  // Turn off annoying HDF5 errors
  //H5::Exception::dontPrint();
{'fraction_non_alphanumeric': 0.16408491107286288, 'fraction_numerical': 0.014572576018359151, 'mean_word_length': 4.488664987405541, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1305213', 'n_tokens_mistral': 3322, 'n_tokens_neox': 3279, 'n_words': 663}