|
| 1 | +#include <iostream> |
| 2 | +#include <experimental/filesystem> |
| 3 | + |
| 4 | +#include "pugixml.hpp" |
| 5 | + |
| 6 | +#include "openmc/bank.h" |
| 7 | +#include "openmc/constants.h" |
| 8 | +#include "openmc/message_passing.h" |
| 9 | +#include "openmc/settings.h" |
| 10 | +#include "openmc/simulation.h" |
| 11 | +#include "openmc/source.h" |
| 12 | + |
| 13 | +namespace source_generator |
| 14 | +{ |
| 15 | + void print_settings(pugi::xml_node &root) |
| 16 | + { |
| 17 | + using namespace openmc; |
| 18 | + |
| 19 | + std::cout << "Settings:" << std::endl; |
| 20 | + std::cout << " Number of particles: " << settings::n_particles << std::endl; |
| 21 | + std::cout << " Source library: " << root.child("source").attribute("library").value() << std::endl; |
| 22 | + std::cout << " Source parameters: " << root.child("source").attribute("parameters").value() << std::endl; |
| 23 | + std::cout << " Output path: " << settings::path_output << std::endl; |
| 24 | + std::cout << " Verbosity: " << settings::verbosity << std::endl; |
| 25 | + std::cout << std::endl; |
| 26 | + } |
| 27 | + |
| 28 | + void print_help() |
| 29 | + { |
| 30 | + std::cout << "Usage:" << std::endl; |
| 31 | + std::cout << "source_generator [OPTIONS]" << std::endl; |
| 32 | + std::cout << std::endl; |
| 33 | + std::cout << "Options:" << std::endl; |
| 34 | + std::cout << " -l,--library Source library, mandatory" << std::endl; |
| 35 | + std::cout << " -i,--input Source definition, mandatory" << std::endl; |
| 36 | + std::cout << " -n,--particles Number of particles, default 1000" << std::endl; |
| 37 | + std::cout << " -o,--output Output directory, default {current directory}" << std::endl; |
| 38 | + std::cout << " -v,--verbosity Verbosity, default 5" << std::endl; |
| 39 | + } |
| 40 | + |
| 41 | + void set_defaults() |
| 42 | + { |
| 43 | + using namespace openmc; |
| 44 | + using namespace std::experimental; |
| 45 | + |
| 46 | + std::string current_path = filesystem::current_path().string(); |
| 47 | + |
| 48 | + // These are static to make sure we get some output... |
| 49 | + settings::run_mode = RunMode::EIGENVALUE; |
| 50 | + settings::write_initial_source = true; |
| 51 | + |
| 52 | + // Variables with sensible default values |
| 53 | + settings::n_particles = 1000; |
| 54 | + settings::path_output = current_path + "/"; |
| 55 | + settings::verbosity = 5; |
| 56 | + } |
| 57 | + |
| 58 | + int parse_command_line(int argc, char* argv[], pugi::xml_node &root) |
| 59 | + { |
| 60 | + using namespace openmc; |
| 61 | + |
| 62 | + root.append_child("source"); |
| 63 | + |
| 64 | + for (int i=1; i < argc; ++i) |
| 65 | + { |
| 66 | + std::string arg {argv[i]}; |
| 67 | + if (arg[0] == '-') |
| 68 | + { |
| 69 | + if (arg == "-n" || arg == "--particles") |
| 70 | + { |
| 71 | + i += 1; |
| 72 | + settings::n_particles = std::stoll(argv[i]); |
| 73 | + } |
| 74 | + else if (arg == "-l" || arg == "--library") |
| 75 | + { |
| 76 | + i += 1; |
| 77 | + root.child("source").append_attribute("library").set_value(argv[i]); |
| 78 | + settings::path_source_library = argv[i]; |
| 79 | + } |
| 80 | + else if (arg == "-i" || arg == "--input") |
| 81 | + { |
| 82 | + i += 1; |
| 83 | + root.child("source").append_attribute("parameters").set_value(argv[i]); |
| 84 | + } |
| 85 | + else if (arg == "-o" || arg == "--output") |
| 86 | + { |
| 87 | + i += 1; |
| 88 | + settings::path_output = argv[i]; |
| 89 | + } |
| 90 | + else if (arg == "-v" || arg == "--verbosity") |
| 91 | + { |
| 92 | + i += 1; |
| 93 | + settings::verbosity = std::stoi(argv[i]); |
| 94 | + } |
| 95 | + else |
| 96 | + { |
| 97 | + source_generator::print_help(); |
| 98 | + return -1; |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + bool missing_arg = false; |
| 104 | + |
| 105 | + if (!root.child("source").attribute("library")) |
| 106 | + { |
| 107 | + std::cout << "The --library or -l argument is mandatory and must be set." << std::endl; |
| 108 | + missing_arg = true; |
| 109 | + } |
| 110 | + |
| 111 | + if (!root.child("source").attribute("parameters")) |
| 112 | + { |
| 113 | + std::cout << "The --input or -i argument is mandatory and must be set." << std::endl; |
| 114 | + missing_arg = true; |
| 115 | + } |
| 116 | + |
| 117 | + if (missing_arg) |
| 118 | + { |
| 119 | + source_generator::print_help(); |
| 120 | + return -1; |
| 121 | + } |
| 122 | + else |
| 123 | + { |
| 124 | + return 0; |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +int main(int argc, char* argv[]) |
| 130 | +{ |
| 131 | + pugi::xml_document doc; |
| 132 | + pugi::xml_node root = doc.append_child("settings"); |
| 133 | + |
| 134 | + source_generator::set_defaults(); |
| 135 | + |
| 136 | + int run = source_generator::parse_command_line(argc, argv, root); |
| 137 | + |
| 138 | + if (run < 0) |
| 139 | + { |
| 140 | + return run; |
| 141 | + } |
| 142 | + |
| 143 | + if (openmc::settings::verbosity >= 5) |
| 144 | + { |
| 145 | + source_generator::print_settings(root); |
| 146 | + } |
| 147 | + |
| 148 | + std::cout << "Sampling source:" << std::endl; |
| 149 | + openmc::SourceDistribution source = openmc::SourceDistribution(root.child("source")); |
| 150 | + openmc::calculate_work(); |
| 151 | + openmc::allocate_banks(); |
| 152 | + openmc::initialize_source(); |
| 153 | + |
| 154 | + return 0; |
| 155 | +} |
0 commit comments