Skip to content

Commit

Permalink
Merge pull request pyne#1113 from zxkjack123/improve_source_sampling
Browse files Browse the repository at this point in the history
use new sampler for both default and subvoxel r2s
  • Loading branch information
gonuke authored Mar 20, 2019
2 parents be8f420 + 7704193 commit 4d3c644
Show file tree
Hide file tree
Showing 4 changed files with 165 additions and 112 deletions.
13 changes: 13 additions & 0 deletions news/improve_source_sampling.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
**Added:** None

**Changed:**

* Use new sampler for both voxel and sub-voxel R2S.

**Deprecated:** None

**Removed:** None

**Fixed:** None

**Security:** None
70 changes: 28 additions & 42 deletions src/source_sampling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,7 @@ void pyne::sampling_setup_(int* mode) {
"cell_number"));
tag_names.insert(std::pair<std::string, std::string> ("cell_fracs_tag_name",
"cell_fracs"));
if (*mode == 0) {
sampler = new pyne::Sampler(filename, src_tag_name, e_bounds, false);
} else if (*mode == 1) {
sampler = new pyne::Sampler(filename, src_tag_name, e_bounds, true);
} else if (*mode == 2) {
std::string bias_tag_name ("biased_source_density");
sampler = new pyne::Sampler(filename, src_tag_name, e_bounds, bias_tag_name);
} else {
sampler = new pyne::Sampler(filename, tag_names, e_bounds, *mode);
}
sampler = new pyne::Sampler(filename, tag_names, e_bounds, *mode);
}
}

Expand Down Expand Up @@ -73,7 +64,6 @@ pyne::Sampler::Sampler(std::string filename,
bool uniform)
: filename(filename), src_tag_name(src_tag_name), e_bounds(e_bounds) {
bias_mode = (uniform) ? UNIFORM : ANALOG;
sub_mode = DEFAULT;
setup();
}

Expand All @@ -86,7 +76,6 @@ pyne::Sampler::Sampler(std::string filename,
e_bounds(e_bounds),
bias_tag_name(bias_tag_name) {
bias_mode = USER;
sub_mode = DEFAULT;
setup();
}

Expand All @@ -95,26 +84,21 @@ pyne::Sampler::Sampler(std::string filename,
std::vector<double> e_bounds,
int mode)
: filename(filename),
tag_names(tag_names),
e_bounds(e_bounds) {
// determine the bias_mode and sub_mode
// determine the bias_mode
if (mode == 0){
bias_mode = ANALOG;
sub_mode = DEFAULT;
} else if (mode == 1) {
bias_mode = UNIFORM;
sub_mode = DEFAULT;
} else if (mode == 2) {
bias_mode = USER;
sub_mode = DEFAULT;
} else if (mode == 3) {
bias_mode = ANALOG;
sub_mode = SUBVOXEL;
} else if (mode == 4) {
bias_mode = UNIFORM;
sub_mode = SUBVOXEL;
} else if (mode == 5) {
bias_mode = USER;
sub_mode = SUBVOXEL;
}

// find out the src_tag_name and bias_tag_name
Expand All @@ -135,24 +119,7 @@ pyne::Sampler::Sampler(std::string filename,
bias_tag_name = tag_names["bias_tag_name"];
}
}
if (sub_mode == SUBVOXEL) {
// cell_number_tag
if (tag_names.find("cell_number_tag_name") == tag_names.end()) {
// cell_number_tag_name not found
throw std::invalid_argument("cell_number_tag_name not found");
} else {
// found cell_number_tag_name
cell_number_tag_name = tag_names["cell_number_tag_name"];
}
// cell_fracs_tag
if (tag_names.find("cell_fracs_tag_name") == tag_names.end()) {
// cell_fracs_tag_name not found
throw std::invalid_argument("cell_fracs_tag_name not found");
} else {
// found cell_fracs_tag_name
cell_fracs_tag_name = tag_names["cell_fracs_tag_name"];
}
}

setup();
}

Expand All @@ -173,8 +140,8 @@ pyne::SourceParticle pyne::Sampler::particle_birth(std::vector<double> rands) {
xyz_rands.push_back(rands[4]);
moab::CartVect pos = sample_xyz(ve_idx, xyz_rands);
// cell_number
if (sub_mode == SUBVOXEL) {
cell_id = cell_number[ve_idx*max_num_cells + c_idx];
if (ve_type == moab::MBHEX) {
cell_id = cell_number[ve_idx*max_num_cells + c_idx];
} else {
cell_id = -1;
}
Expand All @@ -183,6 +150,7 @@ pyne::SourceParticle pyne::Sampler::particle_birth(std::vector<double> rands) {
return src;
}


void pyne::Sampler::setup() {
moab::ErrorCode rval;
moab::EntityHandle loaded_file_set;
Expand All @@ -193,7 +161,7 @@ void pyne::Sampler::setup() {
if (rval != moab::MB_SUCCESS)
throw std::invalid_argument("Could not load mesh file.");

// Get mesh volume elemebts
// Get mesh volume elements
moab::Range ves;
rval = mesh->get_entities_by_dimension(loaded_file_set, 3, ves);
if (rval != moab::MB_SUCCESS)
Expand All @@ -211,6 +179,25 @@ void pyne::Sampler::setup() {
}
else throw std::invalid_argument("Mesh file must contain only tets or hexes.");

if (ve_type == moab::MBHEX){
// cell_number_tag
if (tag_names.find("cell_number_tag_name") == tag_names.end()) {
// cell_number_tag_name not found
throw std::invalid_argument("cell_number_tag_name not found");
} else {
// found cell_number_tag_name
cell_number_tag_name = tag_names["cell_number_tag_name"];
}
// cell_fracs_tag
if (tag_names.find("cell_fracs_tag_name") == tag_names.end()) {
// cell_fracs_tag_name not found
throw std::invalid_argument("cell_fracs_tag_name not found");
} else {
// found cell_fracs_tag_name
cell_fracs_tag_name = tag_names["cell_fracs_tag_name"];
}
}

// Process all the spatial and tag data and create an alias table.
std::vector<double> volumes(num_ves);
mesh_geom_data(ves, volumes);
Expand Down Expand Up @@ -275,7 +262,7 @@ void pyne::Sampler::mesh_tag_data(moab::Range ves,
for(int i=0; i<cell_fracs.size(); i++){
cell_fracs[i] = 1.0;
}
if (sub_mode == SUBVOXEL) {
if (ve_type == moab::MBHEX) {
// Read the cell_number tag and cell_fracs tag
rval = mesh->tag_get_handle(cell_number_tag_name.c_str(),
cell_number_tag);
Expand Down Expand Up @@ -581,4 +568,3 @@ pyne::SourceParticle::SourceParticle(double _x, double _y, double _z,
}

pyne::SourceParticle::~SourceParticle() {};

3 changes: 1 addition & 2 deletions src/source_sampling.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ namespace pyne {

/// Problem modes
enum BiasMode {USER, ANALOG, UNIFORM};
enum SubMode {DEFAULT, SUBVOXEL};

/// Mesh based Monte Carlo source sampling.
class Sampler {
Expand Down Expand Up @@ -194,12 +193,12 @@ namespace pyne {
std::string bias_tag_name; ///< Biased source density distribution
std::string cell_number_tag_name; ///< Cell number tag
std::string cell_fracs_tag_name; ///< Cell volume fraction tag
std::map<std::string, std::string> tag_names; /// < tag names
std::vector<double> e_bounds; ///< Energy boundaries
int num_e_groups; ///< Number of groups in tag \a _src_tag_name
int num_bias_groups; ///< Number of groups tag \a _bias_tag_name
int max_num_cells; /// Max number of cells in voxels
BiasMode bias_mode; ///< Bias mode: ANALOG, UNIFORM, USER
SubMode sub_mode; ///< Subvoxel/Voxel mode: DEFAULT, SUBVOXEL
// mesh
moab::Interface* mesh; ///< MOAB mesh
int num_ves; ///< Number of mesh volume elements on \a mesh.
Expand Down
Loading

0 comments on commit 4d3c644

Please sign in to comment.