Skip to content

Sensitivity analysis for tallies #3479

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

Draft
wants to merge 16 commits into
base: develop
Choose a base branch
from
Draft
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
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ list(APPEND libopenmc_SOURCES
src/tallies/filter_distribcell.cpp
src/tallies/filter_energy.cpp
src/tallies/filter_energyfunc.cpp
src/tallies/filter_importance.cpp
src/tallies/filter_legendre.cpp
src/tallies/filter_material.cpp
src/tallies/filter_materialfrom.cpp
Expand All @@ -452,6 +453,7 @@ list(APPEND libopenmc_SOURCES
src/tallies/filter_zernike.cpp
src/tallies/tally.cpp
src/tallies/tally_scoring.cpp
src/tallies/sensitivity.cpp
src/tallies/trigger.cpp
src/thermal.cpp
src/timer.cpp
Expand Down
3 changes: 3 additions & 0 deletions docs/source/pythonapi/base.rst
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ Constructing Tallies
openmc.SurfaceFilter
openmc.MeshFilter
openmc.MeshBornFilter
openmc.ImportanceFilter
openmc.MeshMaterialFilter
openmc.MeshSurfaceFilter
openmc.EnergyFilter
Expand All @@ -151,6 +152,8 @@ Constructing Tallies
openmc.MeshMaterialVolumes
openmc.Trigger
openmc.TallyDerivative
openmc.SensitivityTally
openmc.Sensitivity
openmc.Tally
openmc.Tallies

Expand Down
2 changes: 2 additions & 0 deletions docs/source/pythonapi/capi.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ Classes
Mesh
MeshFilter
MeshBornFilter
ImportanceFilter
MeshSurfaceFilter
MuFilter
Nuclide
Expand All @@ -89,6 +90,7 @@ Classes
SphericalMesh
SurfaceFilter
Tally
SensitivityTally
TemporarySession
UniverseFilter
UnstructuredMesh
Expand Down
2 changes: 2 additions & 0 deletions include/openmc/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ enum class MgxsType {

enum class TallyResult { VALUE, SUM, SUM_SQ, SIZE };

enum class SensitivityTallyResult { VALUE, SUM, SUM_SQ, PREVIOUS_VALUE };

enum class TallyType { VOLUME, MESH_SURFACE, SURFACE, PULSE_HEIGHT };

enum class TallyEstimator { ANALOG, TRACKLENGTH, COLLISION };
Expand Down
58 changes: 58 additions & 0 deletions include/openmc/filter_importance.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#ifndef OPENMC_TALLIES_FILTER_IMPORTANCE_H
#define OPENMC_TALLIES_FILTER_IMPORTANCE_H

#include <cstdint>

#include <gsl/gsl-lite.hpp>
#include "openmc/vector.h"
#include "openmc/tallies/filter.h"

namespace openmc {

//==============================================================================
//! Multiplies each tally by the importance corresponding to the mesh index
//==============================================================================

class ImportanceFilter : public Filter {
public:
//----------------------------------------------------------------------------
// Constructors, destructors

~ImportanceFilter() = default;

//----------------------------------------------------------------------------
// Methods

std::string type_str() const override {return "importance";}
FilterType type() const override { return FilterType::IMPORTANCE; }

void from_xml(pugi::xml_node node) override;

void get_all_bins(const Particle& p, TallyEstimator estimator, FilterMatch& match)
const override;

void to_statepoint(hid_t filter_group) const override;

std::string text_label(int bin) const override;

//----------------------------------------------------------------------------
// Accessors

const std::vector<double>& importance() const { return importance_; }
void set_importance(gsl::span<const double> importance);

virtual int32_t mesh() const {return mesh_;}

virtual void set_mesh(int32_t mesh);

protected:
//----------------------------------------------------------------------------
// Data members

std::vector<double> importance_;

int32_t mesh_;
};

} // namespace openmc
#endif // OPENMC_TALLIES_FILTER_IMPORTANCE_H
52 changes: 52 additions & 0 deletions include/openmc/particle_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,12 @@ struct SourceSite {
Position r;
Direction u;
double E;
double E_parent;
double time {0.0};
double wgt {1.0};
int delayed_group {0};
int surf_id {0};
int fission_nuclide;
int surf_id {SURFACE_NONE};
ParticleType particle;

Expand Down Expand Up @@ -430,8 +433,13 @@ class ParticleData : public GeometryState {

double E_;
double E_last_;
double E_parent_; //!< energy of parent neutron in eV
int g_ {0};
int g_last_;

// Other birth data
int fission_nuclide_; //!< this particle was born as a result of this nuclide fissioning
// a double for fission cross section at birth? if so, I need to also add it to the bank...

double wgt_ {1.0};
double wgt_born_ {1.0};
Expand Down Expand Up @@ -470,6 +478,8 @@ class ParticleData : public GeometryState {
int64_t current_work_;

vector<double> flux_derivs_;

std::vector<std::vector<double>> cumulative_sensitivities_; // for sensitivities for this particle

vector<FilterMatch> filter_matches_;

Expand Down Expand Up @@ -532,6 +542,8 @@ class ParticleData : public GeometryState {
const double& E() const { return E_; }
double& E_last() { return E_last_; }
const double& E_last() const { return E_last_; }
double& E_parent() { return E_parent_; } // for sensitivity analysis
const double& E_parent() const { return E_parent_; } // for SA
int& g() { return g_; }
const int& g() const { return g_; }
int& g_last() { return g_last_; }
Expand Down Expand Up @@ -629,6 +641,12 @@ class ParticleData : public GeometryState {
// Used in tally derivatives
double& flux_derivs(int i) { return flux_derivs_[i]; }
const double& flux_derivs(int i) const { return flux_derivs_[i]; }

// Used in sensitivity analysis
std::vector<double>& cumulative_sensitivities(int i) { return cumulative_sensitivities_[i]; }
const std::vector<double>& cumulative_sensitivities(int i) const { return cumulative_sensitivities_[i]; }
int& fission_nuclide() { return fission_nuclide_; }
const int& fission_nuclide() const { return fission_nuclide_; }

// Matches of tallies
decltype(filter_matches_)& filter_matches() { return filter_matches_; }
Expand Down Expand Up @@ -697,6 +715,40 @@ class ParticleData : public GeometryState {
d = 0;
}
}

void resize_flux_derivs(int newSize)
{
flux_derivs_.resize(newSize, 0.0);
}

// Resize and initialize sensitivity vectors
void resize_init_cumulative_sensitivities(int newSize)
{
cumulative_sensitivities_.resize(newSize, {0.0});
}

void resize_init_cumulative_sensitivities_vec(int indx, int newSize)
{
cumulative_sensitivities_[indx].resize(newSize, 0.0);
}

void resize_alloc_filter_matches(int newSize)
{
filter_matches_.resize(newSize);
for (auto &match: filter_matches_){
match.bins_.resize(1, 0.0);
match.weights_.resize(1, 0.0);
match.i_bin_ = 0;
// bins_present_ default is false from header
}
}

void initialize_cumulative_sensitivities()
{
for (auto& it : cumulative_sensitivities_){
std::fill(it.begin(), it.end(), 0.0);
}
}
};

} // namespace openmc
Expand Down
3 changes: 3 additions & 0 deletions include/openmc/simulation.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ void initialize_generation();
//! Full initialization of a particle history
void initialize_history(Particle& p, int64_t index_source);

//! Helper function for initialize_history() that is called independently elsewhere
void initialize_history_partial(Particle& p);

//! Finalize a batch
//!
//! Handles synchronization and accumulation of tallies, calculation of Shannon
Expand Down
1 change: 1 addition & 0 deletions include/openmc/tallies/filter.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ enum class FilterType {
MATERIALFROM,
MESH,
MESHBORN,
IMPORTANCE,
MESH_MATERIAL,
MESH_SURFACE,
MU,
Expand Down
58 changes: 58 additions & 0 deletions include/openmc/tallies/filter_importance.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#ifndef OPENMC_TALLIES_FILTER_IMPORTANCE_H
#define OPENMC_TALLIES_FILTER_IMPORTANCE_H

#include <cstdint>

#include <gsl/gsl-lite.hpp>
#include "openmc/vector.h"
#include "openmc/tallies/filter.h"

namespace openmc {

//==============================================================================
//! Multiplies each tally by the importance corresponding to the mesh index
//==============================================================================

class ImportanceFilter : public Filter {
public:
//----------------------------------------------------------------------------
// Constructors, destructors

~ImportanceFilter() = default;

//----------------------------------------------------------------------------
// Methods

std::string type_str() const override {return "importance";}
FilterType type() const override { return FilterType::IMPORTANCE; }

void from_xml(pugi::xml_node node) override;

void get_all_bins(const Particle& p, TallyEstimator estimator, FilterMatch& match)
const override;

void to_statepoint(hid_t filter_group) const override;

std::string text_label(int bin) const override;

//----------------------------------------------------------------------------
// Accessors

const std::vector<double>& importance() const { return importance_; }
void set_importance(gsl::span<const double> importance);

virtual int32_t mesh() const {return mesh_;}

virtual void set_mesh(int32_t mesh);

protected:
//----------------------------------------------------------------------------
// Data members

std::vector<double> importance_;

int32_t mesh_;
};

} // namespace openmc
#endif // OPENMC_TALLIES_FILTER_IMPORTANCE_H
Loading