Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates uwuwmatlib.py for new uw2 material naming convention and adds… #531

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions tools/uwuw_matlib.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -13,31 +18,40 @@
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():
if isinstance(mat, MultiMaterial):
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()
24 changes: 20 additions & 4 deletions uwuw/uwuw_preproc.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
#include "uwuw_preprocessor.hpp"
#include "moab/ProgOptions.hpp"
#include <stdlib.h>
#include <fstream>

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");

Expand All @@ -17,16 +28,21 @@ int main(int argc, char* argv[]) {
po.addOpt<void>("fatal,f", "Fatal errors cause exit", &fatal_errors);
po.addOpt<void>("simulate,s", "Fatal errors cause exit", &simulate);

po.addRequiredArg<std::string>("dag_file", "Path to DAGMC file to proccess", &dag_file);
po.addOpt<std::string>("lib_file,l", "Path to PyNE Material Library file to proccess", &lib_file);
po.addRequiredArg<std::string>("dag_file", "Path to DAGMC file to process", &dag_file);
po.addOpt<std::string>("lib_file,l", "File name (with path) for PyNE Material Library file to process", &lib_file);
po.addOpt<std::string>("output,o", "Specify the output filename (default "")", &out_file);

po.addOptionHelpHeading("Options for loading files");

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);
}

Expand Down
90 changes: 60 additions & 30 deletions uwuw/uwuw_preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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<std::string, pyne::Material> :: iterator it;

// loop over the processed material library and write each one to the file
Expand All @@ -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");
Expand All @@ -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<pyne::Tally> :: iterator it;

std::string tally_destination = "/tally"; // to avoid compiler warning
Expand All @@ -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;

Expand Down Expand Up @@ -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<std::string> material_names;

// vol prop iterator
Expand All @@ -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);
Expand All @@ -133,7 +139,10 @@ void uwuw_preprocessor::process_materials() {
std::set<std::string> :: 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<std::string, pyne::Material> :: iterator m_it;
for (m_it = material_library.begin() ; m_it != material_library.end() ; ++m_it) {
pyne::Material mat = m_it->second;
Expand All @@ -144,34 +153,44 @@ 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");

// 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;
}
}
return;
}

// process and create all the tally objects
void uwuw_preprocessor::process_tallies() {
void uwuw_preprocessor::process_tallies()
{
std::vector<std::string>::iterator it;

// first volumes
Expand Down Expand Up @@ -225,7 +244,8 @@ void uwuw_preprocessor::process_tallies() {
}

void uwuw_preprocessor::check_material_props(std::vector<std::string> material_props,
std::vector<std::string> density_props, int cellid) {
std::vector<std::string> density_props, int cellid)
{

if (material_props.size() == 0) {
if (verbose || fatal) {
Expand Down Expand Up @@ -279,7 +299,8 @@ void uwuw_preprocessor::check_material_props(std::vector<std::string> 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;
Expand All @@ -300,7 +321,8 @@ void uwuw_preprocessor::print_summary() {
}

// used for printing & debugging only
void uwuw_preprocessor::property_vector(std::vector<int> props) {
void uwuw_preprocessor::property_vector(std::vector<int> props)
{
if (props.size() == 0)
return;
for (int i = 0 ; i < props.size() ; i++) {
Expand All @@ -319,8 +341,9 @@ void uwuw_preprocessor::property_vector(std::vector<int> 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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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();
Expand Down