2018-07-17 06:43:35 -05:00
|
|
|
#include "openmc/source.h"
|
|
|
|
|
|
|
|
|
|
#include <algorithm> // for move
|
|
|
|
|
#include <sstream> // for stringstream
|
|
|
|
|
|
|
|
|
|
#include "xtensor/xadapt.hpp"
|
|
|
|
|
|
2018-11-15 08:13:14 -06:00
|
|
|
#include "openmc/bank.h"
|
2018-07-17 06:43:35 -05:00
|
|
|
#include "openmc/cell.h"
|
|
|
|
|
#include "openmc/error.h"
|
|
|
|
|
#include "openmc/file_utils.h"
|
|
|
|
|
#include "openmc/hdf5_interface.h"
|
|
|
|
|
#include "openmc/material.h"
|
|
|
|
|
#include "openmc/message_passing.h"
|
|
|
|
|
#include "openmc/mgxs_interface.h"
|
|
|
|
|
#include "openmc/nuclide.h"
|
|
|
|
|
#include "openmc/capi.h"
|
|
|
|
|
#include "openmc/random_lcg.h"
|
|
|
|
|
#include "openmc/search.h"
|
|
|
|
|
#include "openmc/settings.h"
|
|
|
|
|
#include "openmc/simulation.h"
|
|
|
|
|
#include "openmc/state_point.h"
|
|
|
|
|
#include "openmc/xml_interface.h"
|
|
|
|
|
|
|
|
|
|
namespace openmc {
|
|
|
|
|
|
|
|
|
|
//==============================================================================
|
|
|
|
|
// Global variables
|
|
|
|
|
//==============================================================================
|
|
|
|
|
|
2018-11-08 15:29:46 -06:00
|
|
|
namespace model {
|
|
|
|
|
|
2018-07-17 06:43:35 -05:00
|
|
|
std::vector<SourceDistribution> external_sources;
|
|
|
|
|
|
2018-11-08 15:29:46 -06:00
|
|
|
}
|
|
|
|
|
|
2018-07-17 06:43:35 -05:00
|
|
|
//==============================================================================
|
|
|
|
|
// SourceDistribution implementation
|
|
|
|
|
//==============================================================================
|
|
|
|
|
|
|
|
|
|
SourceDistribution::SourceDistribution(UPtrSpace space, UPtrAngle angle, UPtrDist energy)
|
|
|
|
|
: space_{std::move(space)}, angle_{std::move(angle)}, energy_{std::move(energy)} { }
|
|
|
|
|
|
|
|
|
|
SourceDistribution::SourceDistribution(pugi::xml_node node)
|
|
|
|
|
{
|
|
|
|
|
// Check for particle type
|
|
|
|
|
if (check_for_node(node, "particle")) {
|
|
|
|
|
auto temp_str = get_node_value(node, "particle", true, true);
|
|
|
|
|
if (temp_str == "neutron") {
|
2019-02-27 09:00:59 -06:00
|
|
|
particle_ = Particle::Type::neutron;
|
2018-07-17 06:43:35 -05:00
|
|
|
} else if (temp_str == "photon") {
|
2019-02-27 09:00:59 -06:00
|
|
|
particle_ = Particle::Type::photon;
|
2018-08-24 09:38:13 -05:00
|
|
|
settings::photon_transport = true;
|
2018-07-17 06:43:35 -05:00
|
|
|
} else {
|
|
|
|
|
fatal_error(std::string("Unknown source particle type: ") + temp_str);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check for source strength
|
|
|
|
|
if (check_for_node(node, "strength")) {
|
|
|
|
|
strength_ = std::stod(get_node_value(node, "strength"));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check for external source file
|
|
|
|
|
if (check_for_node(node, "file")) {
|
|
|
|
|
// Copy path of source file
|
2018-08-24 09:38:13 -05:00
|
|
|
settings::path_source = get_node_value(node, "file", false, true);
|
2018-07-17 06:43:35 -05:00
|
|
|
|
|
|
|
|
// Check if source file exists
|
2018-08-24 09:38:13 -05:00
|
|
|
if (!file_exists(settings::path_source)) {
|
2018-07-17 06:43:35 -05:00
|
|
|
std::stringstream msg;
|
2018-08-24 09:38:13 -05:00
|
|
|
msg << "Source file '" << settings::path_source << "' does not exist.";
|
2018-07-17 06:43:35 -05:00
|
|
|
fatal_error(msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
// Spatial distribution for external source
|
|
|
|
|
if (check_for_node(node, "space")) {
|
|
|
|
|
// Get pointer to spatial distribution
|
|
|
|
|
pugi::xml_node node_space = node.child("space");
|
|
|
|
|
|
|
|
|
|
// Check for type of spatial distribution and read
|
|
|
|
|
std::string type;
|
|
|
|
|
if (check_for_node(node_space, "type"))
|
|
|
|
|
type = get_node_value(node_space, "type", true, true);
|
|
|
|
|
if (type == "cartesian") {
|
|
|
|
|
space_ = UPtrSpace{new CartesianIndependent(node_space)};
|
|
|
|
|
} else if (type == "box") {
|
|
|
|
|
space_ = UPtrSpace{new SpatialBox(node_space)};
|
|
|
|
|
} else if (type == "fission") {
|
|
|
|
|
space_ = UPtrSpace{new SpatialBox(node_space, true)};
|
|
|
|
|
} else if (type == "point") {
|
|
|
|
|
space_ = UPtrSpace{new SpatialPoint(node_space)};
|
|
|
|
|
} else {
|
|
|
|
|
std::stringstream msg;
|
|
|
|
|
msg << "Invalid spatial distribution for external source: " << type;
|
|
|
|
|
fatal_error(msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
// If no spatial distribution specified, make it a point source
|
|
|
|
|
space_ = UPtrSpace{new SpatialPoint()};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Determine external source angular distribution
|
|
|
|
|
if (check_for_node(node, "angle")) {
|
|
|
|
|
// Get pointer to angular distribution
|
|
|
|
|
pugi::xml_node node_angle = node.child("angle");
|
|
|
|
|
|
|
|
|
|
// Check for type of angular distribution
|
|
|
|
|
std::string type;
|
|
|
|
|
if (check_for_node(node_angle, "type"))
|
|
|
|
|
type = get_node_value(node_angle, "type", true, true);
|
|
|
|
|
if (type == "isotropic") {
|
|
|
|
|
angle_ = UPtrAngle{new Isotropic()};
|
|
|
|
|
} else if (type == "monodirectional") {
|
|
|
|
|
angle_ = UPtrAngle{new Monodirectional(node_angle)};
|
|
|
|
|
} else if (type == "mu-phi") {
|
|
|
|
|
angle_ = UPtrAngle{new PolarAzimuthal(node_angle)};
|
|
|
|
|
} else {
|
|
|
|
|
std::stringstream msg;
|
|
|
|
|
msg << "Invalid angular distribution for external source: " << type;
|
|
|
|
|
fatal_error(msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
angle_ = UPtrAngle{new Isotropic()};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Determine external source energy distribution
|
|
|
|
|
if (check_for_node(node, "energy")) {
|
|
|
|
|
pugi::xml_node node_dist = node.child("energy");
|
|
|
|
|
energy_ = distribution_from_xml(node_dist);
|
|
|
|
|
} else {
|
|
|
|
|
// Default to a Watt spectrum with parameters 0.988 MeV and 2.249 MeV^-1
|
|
|
|
|
energy_ = UPtrDist{new Watt(0.988e6, 2.249e-6)};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2019-03-01 14:46:21 -06:00
|
|
|
Particle::Bank SourceDistribution::sample() const
|
2018-07-17 06:43:35 -05:00
|
|
|
{
|
2019-03-01 14:46:21 -06:00
|
|
|
Particle::Bank site;
|
2018-07-17 06:43:35 -05:00
|
|
|
|
|
|
|
|
// Set weight to one by default
|
|
|
|
|
site.wgt = 1.0;
|
|
|
|
|
|
|
|
|
|
// Repeat sampling source location until a good site has been found
|
|
|
|
|
bool found = false;
|
|
|
|
|
int n_reject = 0;
|
|
|
|
|
static int n_accept = 0;
|
|
|
|
|
while (!found) {
|
|
|
|
|
// Set particle type
|
2019-03-01 14:46:21 -06:00
|
|
|
site.particle = particle_;
|
2018-07-17 06:43:35 -05:00
|
|
|
|
|
|
|
|
// Sample spatial distribution
|
2019-03-01 14:46:21 -06:00
|
|
|
site.r = space_->sample();
|
2018-07-17 06:43:35 -05:00
|
|
|
|
|
|
|
|
// Now search to see if location exists in geometry
|
|
|
|
|
int32_t cell_index, instance;
|
2019-03-01 14:46:21 -06:00
|
|
|
int err = openmc_find_cell(&site.r.x, &cell_index, &instance);
|
2018-07-17 06:43:35 -05:00
|
|
|
found = (err != OPENMC_E_GEOMETRY);
|
|
|
|
|
|
|
|
|
|
// Check if spatial site is in fissionable material
|
|
|
|
|
if (found) {
|
|
|
|
|
auto space_box = dynamic_cast<SpatialBox*>(space_.get());
|
|
|
|
|
if (space_box) {
|
|
|
|
|
if (space_box->only_fissionable()) {
|
|
|
|
|
// Determine material
|
2019-02-26 22:24:07 -06:00
|
|
|
const auto& c = model::cells[cell_index];
|
2019-02-14 16:15:58 -05:00
|
|
|
auto mat_index = c->material_.size() == 1
|
|
|
|
|
? c->material_[0] : c->material_[instance];
|
2018-07-17 06:43:35 -05:00
|
|
|
|
|
|
|
|
if (mat_index == MATERIAL_VOID) {
|
|
|
|
|
found = false;
|
|
|
|
|
} else {
|
2019-01-31 22:54:32 -06:00
|
|
|
if (!model::materials[mat_index]->fissionable_) found = false;
|
2018-07-17 06:43:35 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check for rejection
|
|
|
|
|
if (!found) {
|
|
|
|
|
++n_reject;
|
|
|
|
|
if (n_reject >= EXTSRC_REJECT_THRESHOLD &&
|
|
|
|
|
static_cast<double>(n_accept)/n_reject <= EXTSRC_REJECT_FRACTION) {
|
|
|
|
|
fatal_error("More than 95% of external source sites sampled were "
|
|
|
|
|
"rejected. Please check your external source definition.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Increment number of accepted samples
|
|
|
|
|
++n_accept;
|
|
|
|
|
|
|
|
|
|
// Sample angle
|
2019-03-01 14:46:21 -06:00
|
|
|
site.u = angle_->sample();
|
2018-07-17 06:43:35 -05:00
|
|
|
|
|
|
|
|
// Check for monoenergetic source above maximum particle energy
|
|
|
|
|
auto p = static_cast<int>(particle_);
|
|
|
|
|
auto energy_ptr = dynamic_cast<Discrete*>(energy_.get());
|
|
|
|
|
if (energy_ptr) {
|
|
|
|
|
auto energies = xt::adapt(energy_ptr->x());
|
2019-01-31 22:54:32 -06:00
|
|
|
if (xt::any(energies > data::energy_max[p])) {
|
2018-07-17 06:43:35 -05:00
|
|
|
fatal_error("Source energy above range of energies of at least "
|
|
|
|
|
"one cross section table");
|
2019-01-31 22:54:32 -06:00
|
|
|
} else if (xt::any(energies < data::energy_min[p])) {
|
2018-07-17 06:43:35 -05:00
|
|
|
fatal_error("Source energy below range of energies of at least "
|
|
|
|
|
"one cross section table");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
|
// Sample energy spectrum
|
|
|
|
|
site.E = energy_->sample();
|
|
|
|
|
|
|
|
|
|
// Resample if energy falls outside minimum or maximum particle energy
|
2019-01-31 22:54:32 -06:00
|
|
|
if (site.E < data::energy_max[p] && site.E > data::energy_min[p]) break;
|
2018-07-17 06:43:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set delayed group
|
|
|
|
|
site.delayed_group = 0;
|
|
|
|
|
|
|
|
|
|
return site;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//==============================================================================
|
|
|
|
|
// Non-member functions
|
|
|
|
|
//==============================================================================
|
|
|
|
|
|
|
|
|
|
void initialize_source()
|
|
|
|
|
{
|
|
|
|
|
write_message("Initializing source particles...", 5);
|
|
|
|
|
|
2018-08-24 09:38:13 -05:00
|
|
|
if (settings::path_source != "") {
|
2018-07-17 06:43:35 -05:00
|
|
|
// Read the source from a binary file instead of sampling from some
|
|
|
|
|
// assumed source distribution
|
|
|
|
|
|
|
|
|
|
std::stringstream msg;
|
2018-08-24 09:38:13 -05:00
|
|
|
msg << "Reading source file from " << settings::path_source << "...";
|
2018-07-17 06:43:35 -05:00
|
|
|
write_message(msg, 6);
|
|
|
|
|
|
|
|
|
|
// Open the binary file
|
2018-08-24 09:38:13 -05:00
|
|
|
hid_t file_id = file_open(settings::path_source, 'r', true);
|
2018-07-17 06:43:35 -05:00
|
|
|
|
|
|
|
|
// Read the file type
|
|
|
|
|
std::string filetype;
|
|
|
|
|
read_attribute(file_id, "filetype", filetype);
|
|
|
|
|
|
|
|
|
|
// Check to make sure this is a source file
|
|
|
|
|
if (filetype != "source" && filetype != "statepoint") {
|
|
|
|
|
fatal_error("Specified starting source file not a source file type.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Read in the source bank
|
2018-11-15 08:13:14 -06:00
|
|
|
read_source_bank(file_id);
|
2018-07-17 06:43:35 -05:00
|
|
|
|
|
|
|
|
// Close file
|
|
|
|
|
file_close(file_id);
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
// Generation source sites from specified distribution in user input
|
2018-10-09 22:34:08 -05:00
|
|
|
for (int64_t i = 0; i < simulation::work; ++i) {
|
2018-07-17 06:43:35 -05:00
|
|
|
// initialize random number seed
|
2018-10-09 22:34:08 -05:00
|
|
|
int64_t id = simulation::total_gen*settings::n_particles +
|
|
|
|
|
simulation::work_index[mpi::rank] + i + 1;
|
2018-07-17 06:43:35 -05:00
|
|
|
set_particle_seed(id);
|
|
|
|
|
|
|
|
|
|
// sample external source distribution
|
2018-11-15 08:13:14 -06:00
|
|
|
simulation::source_bank[i] = sample_external_source();
|
2018-07-17 06:43:35 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Write out initial source
|
2018-08-24 09:38:13 -05:00
|
|
|
if (settings::write_initial_source) {
|
2018-07-17 06:43:35 -05:00
|
|
|
write_message("Writing out initial source...", 5);
|
2018-08-24 09:38:13 -05:00
|
|
|
std::string filename = settings::path_output + "initial_source.h5";
|
2018-07-17 06:43:35 -05:00
|
|
|
hid_t file_id = file_open(filename, 'w', true);
|
2018-11-15 08:13:14 -06:00
|
|
|
write_source_bank(file_id);
|
2018-07-17 06:43:35 -05:00
|
|
|
file_close(file_id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-01 14:46:21 -06:00
|
|
|
Particle::Bank sample_external_source()
|
2018-07-17 06:43:35 -05:00
|
|
|
{
|
|
|
|
|
// Set the random number generator to the source stream.
|
|
|
|
|
prn_set_stream(STREAM_SOURCE);
|
|
|
|
|
|
|
|
|
|
// Determine total source strength
|
|
|
|
|
double total_strength = 0.0;
|
2018-11-08 15:29:46 -06:00
|
|
|
for (auto& s : model::external_sources)
|
2018-07-17 06:43:35 -05:00
|
|
|
total_strength += s.strength();
|
|
|
|
|
|
|
|
|
|
// Sample from among multiple source distributions
|
|
|
|
|
int i = 0;
|
2018-11-08 15:29:46 -06:00
|
|
|
if (model::external_sources.size() > 1) {
|
2018-07-17 06:43:35 -05:00
|
|
|
double xi = prn()*total_strength;
|
|
|
|
|
double c = 0.0;
|
2018-11-08 15:29:46 -06:00
|
|
|
for (; i < model::external_sources.size(); ++i) {
|
|
|
|
|
c += model::external_sources[i].strength();
|
2018-07-17 06:43:35 -05:00
|
|
|
if (xi < c) break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Sample source site from i-th source distribution
|
2019-03-01 14:46:21 -06:00
|
|
|
Particle::Bank site {model::external_sources[i].sample()};
|
2018-07-17 06:43:35 -05:00
|
|
|
|
|
|
|
|
// If running in MG, convert site % E to group
|
2018-08-24 09:38:13 -05:00
|
|
|
if (!settings::run_CE) {
|
2018-11-08 15:00:40 -06:00
|
|
|
site.E = lower_bound_index(data::rev_energy_bins.begin(),
|
|
|
|
|
data::rev_energy_bins.end(), site.E);
|
|
|
|
|
site.E = data::num_energy_groups - site.E;
|
2018-07-17 06:43:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set the random number generator back to the tracking stream.
|
|
|
|
|
prn_set_stream(STREAM_TRACKING);
|
|
|
|
|
|
|
|
|
|
return site;
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-21 08:32:32 -06:00
|
|
|
void free_memory_source()
|
2018-07-17 06:43:35 -05:00
|
|
|
{
|
2018-11-08 15:29:46 -06:00
|
|
|
model::external_sources.clear();
|
2018-07-17 06:43:35 -05:00
|
|
|
}
|
|
|
|
|
|
2018-10-16 19:48:26 -05:00
|
|
|
void fill_source_bank_fixedsource()
|
2018-08-23 13:40:38 -05:00
|
|
|
{
|
2018-08-24 09:38:13 -05:00
|
|
|
if (settings::path_source.empty()) {
|
2018-10-09 22:34:08 -05:00
|
|
|
for (int64_t i = 0; i < simulation::work; ++i) {
|
2018-08-23 13:40:38 -05:00
|
|
|
// initialize random number seed
|
2018-10-09 22:34:08 -05:00
|
|
|
int64_t id = (simulation::total_gen + overall_generation()) *
|
|
|
|
|
settings::n_particles + simulation::work_index[mpi::rank] + i + 1;
|
2018-08-23 13:40:38 -05:00
|
|
|
set_particle_seed(id);
|
|
|
|
|
|
|
|
|
|
// sample external source distribution
|
2018-11-15 08:13:14 -06:00
|
|
|
simulation::source_bank[i] = sample_external_source();
|
2018-08-23 13:40:38 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-17 06:43:35 -05:00
|
|
|
} // namespace openmc
|