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

External sources alias sampler #3201

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
54ec402
trying to understand what it does!
magnoxemo Nov 7, 2024
693302b
Reset values of lattice offset tables when allocated (#3188)
pshriwise Nov 8, 2024
26f08cb
allowing varible offsets for polygon.offset (#3120)
shimwell Nov 8, 2024
5fe3191
Update surface_composite.py (#3189)
azimgivron Nov 9, 2024
c87f749
add export_model_xml arguments to Model.plot_geometry and Model.calcu…
nplinden Nov 9, 2024
454df35
Fixes in MicroXS.from_multigroup_flux (#3192)
paulromano Nov 11, 2024
3ad81c5
Fix documentation typo in `boundary_type` (#3196)
pshriwise Nov 12, 2024
70347ff
Add a vessel composite surface with ellipsoids on top and bottom. (#3…
nsedayilmaz Nov 12, 2024
030b3d8
Add PointCloud spatial distribution (#3161)
gonuke Nov 13, 2024
6fd184a
Fix docstring for Model.plot (#3198)
paulromano Nov 13, 2024
3cd9ca9
init_alias is private. I have to figure how to acces that in the sett…
magnoxemo Nov 8, 2024
ba55c03
ig it works?
magnoxemo Nov 9, 2024
2647dc9
not sure
magnoxemo Nov 9, 2024
648510c
implementing the suggested changes
magnoxemo Nov 10, 2024
7a1b4ad
don't need the int i again cause already decleared the data type
magnoxemo Nov 10, 2024
93afa21
fixing the typos
magnoxemo Nov 10, 2024
7dccde7
removing unnecessary spaces
magnoxemo Nov 11, 2024
1643150
defining the external_sources_alias_sample in souce.cpp cause it's fa…
magnoxemo Nov 15, 2024
f0656bd
needed to use the extern in source.h and redefine in the source.cpp i…
magnoxemo Nov 16, 2024
763dd74
Merge branch 'develop' into external_sources_alias_sampler
magnoxemo Nov 22, 2024
b8e0ca6
Fix uniform source sampling
paulromano Nov 23, 2024
8ef3379
Add test for multiple sources
paulromano Nov 23, 2024
47db5a7
Change name to external_sources_probability
paulromano Nov 23, 2024
8dba43e
Two comments
paulromano Nov 23, 2024
2fdf150
Update result for source regression test
paulromano Nov 23, 2024
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
3 changes: 3 additions & 0 deletions include/openmc/source.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ namespace model {

extern vector<unique_ptr<Source>> external_sources;

// Probability distribution for selecting external sources
extern DiscreteIndex external_sources_probability;

} // namespace model

//==============================================================================
Expand Down
4 changes: 2 additions & 2 deletions openmc/model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,7 @@ def sample_external_source(
n_samples: int = 1000,
prn_seed: int | None = None,
**init_kwargs
) -> list[openmc.SourceParticle]:
) -> openmc.ParticleList:
"""Sample external source and return source particles.

.. versionadded:: 0.15.1
Expand All @@ -902,7 +902,7 @@ def sample_external_source(

Returns
-------
list of openmc.SourceParticle
openmc.ParticleList
List of samples source particles
"""
import openmc.lib
Expand Down
7 changes: 7 additions & 0 deletions src/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,13 @@ void read_settings_xml(pugi::xml_node root)
model::external_sources.push_back(make_unique<FileSource>(path));
}

// Build probability mass function for sampling external sources
vector<double> source_strengths;
for (auto& s : model::external_sources) {
source_strengths.push_back(s->strength());
}
model::external_sources_probability.assign(source_strengths);

// If no source specified, default to isotropic point source at origin with
// Watt spectrum. No default source is needed in random ray mode.
if (model::external_sources.empty() &&
Expand Down
18 changes: 5 additions & 13 deletions src/source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ namespace openmc {
namespace model {

vector<unique_ptr<Source>> external_sources;
}

DiscreteIndex external_sources_probability;

} // namespace model

//==============================================================================
// Source implementation
Expand Down Expand Up @@ -608,24 +611,13 @@ void initialize_source()

SourceSite sample_external_source(uint64_t* seed)
{
// Determine total source strength
double total_strength = 0.0;
for (auto& s : model::external_sources)
total_strength += s->strength();

// Sample from among multiple source distributions
int i = 0;
if (model::external_sources.size() > 1) {
if (settings::uniform_source_sampling) {
i = prn(seed) * model::external_sources.size();
} else {
double xi = prn(seed) * total_strength;
double c = 0.0;
for (; i < model::external_sources.size(); ++i) {
c += model::external_sources[i]->strength();
if (xi < c)
break;
}
i = model::external_sources_probability.sample(seed);
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/regression_tests/source/results_true.dat
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
k-combined:
3.054101E-01 1.167865E-03
2.959436E-01 2.782384E-03
12 changes: 12 additions & 0 deletions tests/unit_tests/test_uniform_source_sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,15 @@ def test_tally_mean(run_in_tmpdir, sphere_model):

# Check that tally means match
assert mean == pytest.approx(reference_mean)


def test_multiple_sources(sphere_model):
low_strength_src = openmc.IndependentSource(
energy=openmc.stats.delta_function(1.0e6), strength=1e-7)
sphere_model.settings.source.append(low_strength_src)
sphere_model.settings.uniform_source_sampling = True

# Sample particles from source and make sure 1 MeV shows up despite
# negligible strength
particles = sphere_model.sample_external_source(100)
assert {p.E for p in particles} == {1.0e3, 1.0e6}