Skip to content

Commit

Permalink
Update OpenMCAuxKernel to add capabilities needed for gradient calcul…
Browse files Browse the repository at this point in the history
…ation.

Ref. neams-th-coe#1031
  • Loading branch information
nuclearkevin committed Jan 18, 2025
1 parent 473bab7 commit 0d36eab
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 10 deletions.
41 changes: 38 additions & 3 deletions include/auxkernels/OpenMCAuxKernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,49 @@
#include "AuxKernel.h"
#include "OpenMCCellAverageProblem.h"

// forward declarations
template <typename ComputeValueType>
class OpenMCAuxKernelTempl;

typedef OpenMCAuxKernelTempl<Real> OpenMCAuxKernel;
typedef OpenMCAuxKernelTempl<RealVectorValue> OpenMCVectorAuxKernel;
typedef OpenMCAuxKernelTempl<RealEigenVector> OpenMCArrayAuxKernel;

/**
* Base auxkernel from which to inherit auxkernels that query
* the OpenMC problem.
*/
class OpenMCAuxKernel : public AuxKernel
template <typename ComputeValueType>
class OpenMCAuxKernelTempl : public AuxKernelTempl<ComputeValueType>
{
public:
OpenMCAuxKernel(const InputParameters & parameters);
OpenMCAuxKernelTempl(const InputParameters & parameters);

static InputParameters validParams();

protected:
virtual Real computeValue() = 0;
virtual ComputeValueType computeValue() = 0;

/**
* Get the variable(s) associated with an OpenMC tally score.
* @param[in] score the OpenMC score
* @return a vector of variable values associated with score
*/
std::vector<const MooseVariableFE<Real> *> getTallyScoreVariables(const std::string & score);

/**
* Get the variable value(s) associated with an OpenMC tally score.
* @param[in] score the OpenMC score
* @return a vector of variable values associated with score
*/
std::vector<const VariableValue *> getTallyScoreVariableValues(const std::string & score);

/**
* Get the neighbor variable value(s) associated with an OpenMC tally score.
* @param[in] score the OpenMC score
* @return a vector of variable values associated with score
*/
std::vector<const VariableValue *> getTallyScoreNeighborVariableValues(const std::string & score);

/**
* Determine whether the MOOSE element maps to an OpenMC cell to make sure we don't call
Expand All @@ -44,3 +74,8 @@ class OpenMCAuxKernel : public AuxKernel

OpenMCCellAverageProblem * _openmc_problem;
};

// Prevent implicit instantiation in other translation units where these classes are used
extern template class OpenMCAuxKernelTempl<Real>;
extern template class OpenMCAuxKernelTempl<RealVectorValue>;
extern template class OpenMCAuxKernelTempl<RealEigenVector>;
91 changes: 84 additions & 7 deletions src/auxkernels/OpenMCAuxKernel.C
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,107 @@

#include "OpenMCAuxKernel.h"

template <typename ComputeValueType>
InputParameters
OpenMCAuxKernel::validParams()
OpenMCAuxKernelTempl<ComputeValueType>::validParams()
{
InputParameters params = AuxKernel::validParams();
InputParameters params = AuxKernelTempl<ComputeValueType>::validParams();
return params;
}

OpenMCAuxKernel::OpenMCAuxKernel(const InputParameters & parameters) : AuxKernel(parameters)
template <typename ComputeValueType>
OpenMCAuxKernelTempl<ComputeValueType>::OpenMCAuxKernelTempl(const InputParameters & parameters)
: AuxKernelTempl<ComputeValueType>(parameters)
{
_openmc_problem = dynamic_cast<OpenMCCellAverageProblem *>(&_subproblem);
_openmc_problem = dynamic_cast<OpenMCCellAverageProblem *>(&this->_subproblem);

if (!_openmc_problem)
mooseError("This auxkernel can only be used with problems of type 'OpenMCCellAverageProblem'!");

if (isNodal())
if (this->isNodal())
mooseError("This auxkernel can only be used with elemental variables!");
}

template <typename ComputeValueType>
std::vector<const MooseVariableFE<Real> *>
OpenMCAuxKernelTempl<ComputeValueType>::getTallyScoreVariables(const std::string & score)
{
std::vector<const MooseVariableFE<Real> *> score_vars;
const auto & tallies = _openmc_problem->getLocalTally();
for (const auto & t : tallies)
{
if (t->hasScore(score))
{
auto vars = t->getScoreVars(score);
for (const auto & v : vars)
score_vars.emplace_back(dynamic_cast<const MooseVariableFE<Real> *>(&this->_subproblem.getVariable(this->_tid, v)));
}
}

if (score_vars.size() == 0)
mooseError("No tallies contain the requested score " + score + "!");

return score_vars;
}

template <typename ComputeValueType>
std::vector<const VariableValue *>
OpenMCAuxKernelTempl<ComputeValueType>::getTallyScoreVariableValues(const std::string & score)
{
std::vector<const VariableValue *> score_vars;
const auto & tallies = _openmc_problem->getLocalTally();
for (const auto & t : tallies)
{
if (t->hasScore(score))
{
auto vars = t->getScoreVars(score);
for (const auto & v : vars)
score_vars.emplace_back(
&(dynamic_cast<MooseVariableFE<Real> *>(&this->_subproblem.getVariable(this->_tid, v))->sln()));
}
}

if (score_vars.size() == 0)
mooseError("No tallies contain the requested score " + score + "!");

return score_vars;
}

template <typename ComputeValueType>
std::vector<const VariableValue *>
OpenMCAuxKernelTempl<ComputeValueType>::getTallyScoreNeighborVariableValues(const std::string & score)
{
std::vector<const VariableValue *> score_vars;
const auto & tallies = _openmc_problem->getLocalTally();
for (const auto & t : tallies)
{
if (t->hasScore(score))
{
auto vars = t->getScoreVars(score);
for (const auto & v : vars)
score_vars.emplace_back(
&(dynamic_cast<MooseVariableFE<Real> *>(&this->_subproblem.getVariable(this->_tid, v))->slnNeighbor()));
}
}

if (score_vars.size() == 0)
mooseError("No tallies contain the requested score " + score + "!");

return score_vars;
}

template <typename ComputeValueType>
bool
OpenMCAuxKernel::mappedElement()
OpenMCAuxKernelTempl<ComputeValueType>::mappedElement()
{
OpenMCCellAverageProblem::cellInfo cell_info =
_openmc_problem->elemToCellInfo(_current_elem->id());
_openmc_problem->elemToCellInfo(this->_current_elem->id());
return !(cell_info.first == OpenMCCellAverageProblem::UNMAPPED);
}

// Explicitly instantiates the three versions of the OpenMCAuxKernelTempl class
template class OpenMCAuxKernelTempl<Real>;
template class OpenMCAuxKernelTempl<RealVectorValue>;
template class OpenMCAuxKernelTempl<RealEigenVector>;

#endif

0 comments on commit 0d36eab

Please sign in to comment.