From 353f2e64eca6bdd862e0f53dad7f5db9aad385e6 Mon Sep 17 00:00:00 2001 From: Tim Bohm Date: Thu, 16 Nov 2017 16:57:28 -0600 Subject: [PATCH] Updates uwuwmatlib.py for new uw2 material naming convention and adds more verbosity and material file exists check to uwuw_preproc --- tools/uwuw_matlib.py | 24 +++++++--- uwuw/uwuw_preproc.cpp | 24 ++++++++-- uwuw/uwuw_preprocessor.cpp | 90 +++++++++++++++++++++++++------------- 3 files changed, 99 insertions(+), 39 deletions(-) diff --git a/tools/uwuw_matlib.py b/tools/uwuw_matlib.py index 459c31afc7..5fd46b2c5b 100755 --- a/tools/uwuw_matlib.py +++ b/tools/uwuw_matlib.py @@ -1,4 +1,9 @@ #!/usr/bin/env python +# +# add option to make more compatible with uwuw_prepoc +# -use default nucpath,datapath +# -use material names like 11 instead of m11 +# ################################################################################ # This script read an MCNP5 input file and creates a material library for use # with the UWUW workflow. The naming scheme in the material library is @@ -13,7 +18,7 @@ from pyne.material import Material, MultiMaterial, MaterialLibrary from pyne.mcnp import mats_from_inp -def uwuw_matlib(inp, out): +def uwuw_matlib(inp, out, enhanced_uwuw): mats = mats_from_inp(inp) uwuw_mats = {} for mat in mats.values(): @@ -21,23 +26,32 @@ def uwuw_matlib(inp, out): m = mat._mats.keys()[0] else: m = mat - mat_name = "m{0}".format(m.metadata["mat_number"]) + if enhanced_uwuw: + print "Using enhanced uwuw_preproc compatibility mat_name...(no leading m)" + mat_name = str(m.metadata["mat_number"]) + else: + mat_name = "m{0}".format(m.metadata["mat_number"]) # adds "m" to mat_name m.metadata["name"] = mat_name uwuw_mats[mat_name] = m - ml = MaterialLibrary(uwuw_mats) - ml.write_hdf5(out, datapath='/material_library/materials', + ml = MaterialLibrary(uwuw_mats) + if enhanced_uwuw: + print "\n Setting enhanced uwuw_preproc compatibility for writing material library..." + ml.write_hdf5(out) # don't set datapath,nucpath...will be pyne default values + else: + ml.write_hdf5(out, datapath='/material_library/materials', nucpath='/material_library/nucid') def main(): description = ("This program read MCNP5 input files and creates UWUW material libraries" " with naming schemes that match mcnp2cad with the -U flag.") parser = argparse.ArgumentParser(description=description) + parser.add_argument("-u","--uwuw_preproc", help="produce library more compatible with uwuw_preproc",action="store_true") parser.add_argument("inp", action='store', help="Name of the MCNP input file.") parser.add_argument("-o", "--output", action='store', dest='out', default="matlib.h5m", help="The name of the .h5m file to produce.") args = parser.parse_args() - uwuw_matlib(args.inp, args.out) + uwuw_matlib(args.inp, args.out, args.uwuw_preproc) if __name__ == '__main__': main() diff --git a/uwuw/uwuw_preproc.cpp b/uwuw/uwuw_preproc.cpp index 7638e53b66..2fc1d44cb6 100644 --- a/uwuw/uwuw_preproc.cpp +++ b/uwuw/uwuw_preproc.cpp @@ -1,7 +1,18 @@ #include "uwuw_preprocessor.hpp" #include "moab/ProgOptions.hpp" +#include +#include -int main(int argc, char* argv[]) { +// useful functions +bool file_exists(std::string filename) +{ + //this function returns true if a file can be opened + std::ifstream ifile(filename.c_str()); + return (bool)ifile; // casts file object to a boolean +}; + +int main(int argc, char* argv[]) +{ ProgOptions po("uwuw_preproc: a tool for preprocessing DAGMC files to incorporate UWUW workflow information"); @@ -17,8 +28,8 @@ int main(int argc, char* argv[]) { po.addOpt("fatal,f", "Fatal errors cause exit", &fatal_errors); po.addOpt("simulate,s", "Fatal errors cause exit", &simulate); - po.addRequiredArg("dag_file", "Path to DAGMC file to proccess", &dag_file); - po.addOpt("lib_file,l", "Path to PyNE Material Library file to proccess", &lib_file); + po.addRequiredArg("dag_file", "Path to DAGMC file to process", &dag_file); + po.addOpt("lib_file,l", "File name (with path) for PyNE Material Library file to process", &lib_file); po.addOpt("output,o", "Specify the output filename (default "")", &out_file); po.addOptionHelpHeading("Options for loading files"); @@ -26,7 +37,12 @@ int main(int argc, char* argv[]) { po.parseCommandLine(argc, argv); if (lib_file == "") { - std::cout << "need to set the library" << std::endl; + std::cout << "You need to set the PyNE material library filename" << std::endl; + exit(1); + } else if (file_exists(lib_file)) { + std::cout <<"Will read materials from file: "<< lib_file << std::endl; + } else { + std::cout << "Cannot open the material library filename:" << lib_file << std::endl; exit(1); } diff --git a/uwuw/uwuw_preprocessor.cpp b/uwuw/uwuw_preprocessor.cpp index a8dca2e048..0499533f4e 100644 --- a/uwuw/uwuw_preprocessor.cpp +++ b/uwuw/uwuw_preprocessor.cpp @@ -2,7 +2,8 @@ // constructor uwuw_preprocessor::uwuw_preprocessor(std::string material_library_filename, std::string dagmc_filename, - std::string output_file, bool verbosity, bool fatal_errors) { + std::string output_file, bool verbosity, bool fatal_errors) +{ // make new name concatenator class ncr = new name_concatenator(); @@ -32,11 +33,13 @@ uwuw_preprocessor::uwuw_preprocessor(std::string material_library_filename, std: } // destructor -uwuw_preprocessor::~uwuw_preprocessor() { +uwuw_preprocessor::~uwuw_preprocessor() +{ } // write the new material library -void uwuw_preprocessor::write_uwuw_materials() { +void uwuw_preprocessor::write_uwuw_materials() +{ std::map :: iterator it; // loop over the processed material library and write each one to the file @@ -45,9 +48,9 @@ void uwuw_preprocessor::write_uwuw_materials() { pyne::Material mat = it->second; // write the material to the file if (verbose) { - std::cout << "writing material, " << mat.metadata["name"].asString(); - std::cout << "writing material, " << mat.metadata["fluka_name"].asString(); - std::cout << " to file " << output_filename << std::endl; + std::cout << "writing material," << mat.metadata["name"].asString(); + std::cout << " writing material," << mat.metadata["fluka_name"].asString(); + std::cout << " to file: " << output_filename << std::endl; } // write the uwuw materials to the geometry mat.write_hdf5(output_filename, "/materials"); @@ -57,7 +60,8 @@ void uwuw_preprocessor::write_uwuw_materials() { } // write the new material library -void uwuw_preprocessor::write_uwuw_tallies() { +void uwuw_preprocessor::write_uwuw_tallies() +{ std::list :: iterator it; std::string tally_destination = "/tally"; // to avoid compiler warning @@ -80,7 +84,8 @@ void uwuw_preprocessor::write_uwuw_tallies() { // creates a new material using the Material material as a basis -pyne::Material uwuw_preprocessor::create_new_material(pyne::Material material, std::string density) { +pyne::Material uwuw_preprocessor::create_new_material(pyne::Material material, std::string density) +{ pyne::Material new_mat; // to return pyne::comp_map comp = material.comp; @@ -112,7 +117,8 @@ pyne::Material uwuw_preprocessor::create_new_material(pyne::Material material, s } // process the group names into unique material objects -void uwuw_preprocessor::process_materials() { +void uwuw_preprocessor::process_materials() +{ std::set material_names; // vol prop iterator @@ -123,7 +129,7 @@ void uwuw_preprocessor::process_materials() { for (it = volume_property_map.begin() ; it != volume_property_map.end() ; ++it) { std::string group_name = it->second; if (verbose) { - std::cout << "Making materiral group name "; + std::cout << "Making material group name="; std::cout << group_name << std::endl; } material_names.insert(group_name); @@ -133,7 +139,10 @@ void uwuw_preprocessor::process_materials() { std::set :: iterator s_it; if (verbose) { - std::cout << "Materials Present, :" << std::endl; + std::cout << std::endl; + int numinmatllib=material_library.size(); + std::cout << "Number of materials in material library=" << numinmatllib << std::endl; + std::cout << "Materials present in the material library:" << std::endl; std::map :: iterator m_it; for (m_it = material_library.begin() ; m_it != material_library.end() ; ++m_it) { pyne::Material mat = m_it->second; @@ -144,12 +153,20 @@ void uwuw_preprocessor::process_materials() { // loop over the unique material/density combinations for (s_it = material_names.begin() ; s_it != material_names.end() ; ++s_it) { - // material namesa are in the form mat:Name/rho:density - // if we find / then we have a density token + // material names are in the form mat:Name/rho:density + // if we find a / then we have a density token + + std::string mat_name = dmd->return_property(*s_it, "mat"); std::string density = dmd->return_property(*s_it, "rho"); + // print some info while looping + if (verbose) { + std::cout << " material_names iterator=" << *s_it << std::endl; + std::cout << " mat_name=" << mat_name << std::endl; + } + // find grave or vacuum std::size_t found_grave = mat_name.find("Graveyard"); std::size_t found_vacuum = mat_name.find("Vacuum"); @@ -157,13 +174,14 @@ void uwuw_preprocessor::process_materials() { // check for missing materials if (found_grave == std::string::npos && found_vacuum == std::string::npos) { if (material_library.count(mat_name) == 0) { - std::cout << "material " << mat_name << " was not found in the material library" << std::endl; + + std::cout << "material with name=" << mat_name << " was not found in the material library." << " Its density was=" << density << std::endl; exit(EXIT_FAILURE); } // make a new material object with the appropriate density & name pyne::Material new_material = create_new_material(material_library[mat_name], - density); + density); uwuw_material_library[*s_it] = new_material; } } @@ -171,7 +189,8 @@ void uwuw_preprocessor::process_materials() { } // process and create all the tally objects -void uwuw_preprocessor::process_tallies() { +void uwuw_preprocessor::process_tallies() +{ std::vector::iterator it; // first volumes @@ -225,7 +244,8 @@ void uwuw_preprocessor::process_tallies() { } void uwuw_preprocessor::check_material_props(std::vector material_props, - std::vector density_props, int cellid) { + std::vector density_props, int cellid) +{ if (material_props.size() == 0) { if (verbose || fatal) { @@ -279,7 +299,8 @@ void uwuw_preprocessor::check_material_props(std::vector material_p } } -void uwuw_preprocessor::print_summary() { +void uwuw_preprocessor::print_summary() +{ std::cout << "+----------------------------------------------------" << std::endl; std::cout << "| UWUW Summary " << std::endl; std::cout << "+----------------------------------------------------" << std::endl; @@ -300,7 +321,8 @@ void uwuw_preprocessor::print_summary() { } // used for printing & debugging only -void uwuw_preprocessor::property_vector(std::vector props) { +void uwuw_preprocessor::property_vector(std::vector props) +{ if (props.size() == 0) return; for (int i = 0 ; i < props.size() ; i++) { @@ -319,8 +341,9 @@ void uwuw_preprocessor::property_vector(std::vector props) { tally_info uwuw_preprocessor::make_tally_groupname(std::string tally_props, - int dimension, - moab::EntityHandle entity) { + int dimension, + moab::EntityHandle entity) +{ // tally props should be of the form Neutron and Flux or, Photon and Current etc // split tally_props into the particle and tally type @@ -398,9 +421,10 @@ tally_info uwuw_preprocessor::make_tally_groupname(std::string tally_props, } void uwuw_preprocessor::check_tally_props(std::string particle_name, - std::string tally_type, - int dimension, - int entityid) { + std::string tally_type, + int dimension, + int entityid) +{ // check to make sure the particle is allowed std::cout << "particle_name " << particle_name << std::endl; @@ -434,15 +458,18 @@ void uwuw_preprocessor::check_tally_props(std::string particle_name, // constructor -name_concatenator::name_concatenator() { +name_concatenator::name_concatenator() +{ } // destructor -name_concatenator::~name_concatenator() { +name_concatenator::~name_concatenator() +{ } // make the fluka name from the string -std::string name_concatenator::make_name_8bytes(std::string name) { +std::string name_concatenator::make_name_8bytes(std::string name) +{ // fluka name needs to be 8 chars long uppercase and unique std::string b8_name = name; std::transform(b8_name.begin(), b8_name.end(), b8_name.begin(), toupper); @@ -467,7 +494,8 @@ std::string name_concatenator::make_name_8bytes(std::string name) { } // function to extract only the letters [A-Z] and numbers [0-9] -std::string name_concatenator::extract_alpha_num(std::string name) { +std::string name_concatenator::extract_alpha_num(std::string name) +{ // loop over the string accepting only ascii std::string :: iterator it; std::string str; @@ -485,7 +513,8 @@ std::string name_concatenator::extract_alpha_num(std::string name) { // Generates a new name but incremented by 1 relative to the last time // the function was called. -std::string name_concatenator::shift_and_increment(std::string name) { +std::string name_concatenator::shift_and_increment(std::string name) +{ int count = 0; // the following statements in braces are intended to turn the 8 char long string // version of the name into a unique 8 character ID string, mainly for fluka @@ -533,7 +562,8 @@ std::string name_concatenator::shift_and_increment(std::string name) { return name; } -void name_concatenator::int_to_string(int convert, std::string& string) { +void name_concatenator::int_to_string(int convert, std::string& string) +{ std::stringstream ss; ss << convert; string = ss.str();