Skip to content

Commit

Permalink
Merge pull request #28177 from gsgall/single-trace-line-source
Browse files Browse the repository at this point in the history
Single trace line source
  • Loading branch information
GiudGiud authored Nov 25, 2024
2 parents 9446d5f + eb31f3f commit 2a74993
Show file tree
Hide file tree
Showing 15 changed files with 371 additions and 92 deletions.
19 changes: 19 additions & 0 deletions framework/include/problems/SubProblem.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,22 @@ class SubProblem : public Problem
virtual TagID addVectorTag(const TagName & tag_name,
const Moose::VectorTagType type = Moose::VECTOR_TAG_RESIDUAL);

/**
* Adds a vector tag to the list of vectors that will not be zeroed
* when other tagged vectors are
* @param tag the TagID of the vector that will be manually managed
*/
void addNotZeroedVectorTag(const TagID tag);

/**
* Checks if a vector tag is in the list of vectors that will not be zeroed
* when other tagged vectors are
* @param tag the TagID of the vector that is currently being checked
* @returns false if the tag is not within the set of vectors that are
* intended to not be zero or if the set is empty. returns true otherwise
*/
bool vectorTagNotZeroed(const TagID tag) const;

/**
* Get a VectorTag from a TagID.
*/
Expand Down Expand Up @@ -1080,6 +1096,9 @@ class SubProblem : public Problem
/// AD flag indicating whether **any** AD objects have been added
bool _have_ad_objects;

/// the list of vector tags that will not be zeroed when all other tags are
std::unordered_set<TagID> _not_zeroed_tagged_vectors;

private:
/**
* @return whether a given variable name is in the solver systems (reflected by the first
Expand Down
15 changes: 15 additions & 0 deletions framework/src/problems/FEProblemBase.C
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,12 @@ FEProblemBase::validParams()
"and Jacobians (Kernels, BCs, etc.) by setting tags on them. The outer index is for which "
"nonlinear system the extra tag vectors should be added for");

params.addParam<std::vector<std::vector<TagName>>>(
"not_zeroed_tag_vectors",
{},
"Extra vector tags which the sytem will not zero when other vector tags are zeroed. "
"The outer index is for which nonlinear system the extra tag vectors should be added for");

params.addParam<std::vector<std::vector<TagName>>>(
"extra_tag_matrices",
{},
Expand Down Expand Up @@ -615,6 +621,15 @@ FEProblemBase::createTagVectors()
_nl[nl_sys_num]->addVector(tag, false, GHOSTED);
}

auto & not_zeroed_vectors = getParam<std::vector<std::vector<TagName>>>("not_zeroed_tag_vectors");
for (const auto nl_sys_num : index_range(not_zeroed_vectors))
for (auto & vector : not_zeroed_vectors[nl_sys_num])
{
auto tag = addVectorTag(vector);
_nl[nl_sys_num]->addVector(tag, false, GHOSTED);
addNotZeroedVectorTag(tag);
}

// add matrices and their tags
auto & matrices = getParam<std::vector<std::vector<TagName>>>("extra_tag_matrices");
for (const auto nl_sys_num : index_range(matrices))
Expand Down
12 changes: 12 additions & 0 deletions framework/src/problems/SubProblem.C
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ SubProblem::vectorTagExists(const TagName & tag_name) const
return false;
}

void
SubProblem::addNotZeroedVectorTag(const TagID tag)
{
_not_zeroed_tagged_vectors.insert(tag);
}

bool
SubProblem::vectorTagNotZeroed(const TagID tag) const
{
return _not_zeroed_tagged_vectors.count(tag);
}

const VectorTag &
SubProblem::getVectorTag(const TagID tag_id) const
{
Expand Down
5 changes: 2 additions & 3 deletions framework/src/systems/SystemBase.C
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,6 @@ SystemBase::closeTaggedVector(const TagID tag)
"' in system '",
name(),
"' because there is no vector associated with that tag");

getVector(tag).close();
}

Expand All @@ -677,8 +676,8 @@ SystemBase::zeroTaggedVector(const TagID tag)
"' in system '",
name(),
"' because there is no vector associated with that tag");

getVector(tag).zero();
if (!_subproblem.vectorTagNotZeroed(tag))
getVector(tag).zero();
}

void
Expand Down
8 changes: 7 additions & 1 deletion modules/ray_tracing/src/userobjects/RayTracingStudy.C
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ RayTracingStudy::validParams()
"Trace intersections are not verified regardless of this parameter in "
"optimized modes (opt, oprof).");

params.addParam<bool>("allow_other_flags_with_prekernels",
false,
"Whether or not to allow the list of execution flags to have PRE_KERNELS "
"mixed with other flags. If this parameter is not set then if PRE_KERNELS "
"is provided it must be the only execution flag.");

ExecFlagEnum & exec_enum = params.set<ExecFlagEnum>("execute_on", true);
exec_enum.addAvailableFlags(EXEC_PRE_KERNELS);

Expand Down Expand Up @@ -195,7 +201,7 @@ RayTracingStudy::RayTracingStudy(const InputParameters & parameters)
// Evaluating on residual and Jacobian evaluation
if (_execute_enum.isValueSet(EXEC_PRE_KERNELS))
{
if (_execute_enum.size() > 1)
if (!getParam<bool>("allow_other_flags_with_prekernels") && _execute_enum.size() > 1)
paramError("execute_on",
"PRE_KERNELS cannot be mixed with any other execution flag.\nThat is, you cannot "
"currently "
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html

#pragma once

#include "RepeatableRayStudy.h"

class SingleTraceLineSourceTest : public RepeatableRayStudy
{
public:
SingleTraceLineSourceTest(const InputParameters & parameters);

static InputParameters validParams();

virtual void execute() override;

protected:
/// the name of the tag that stores the residuals calculated by ray kernels
const TagName & _residual_tag_name;
/// whether or not the raytracing study has moved the rays on the current time step
bool _has_traced;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "SingleTraceLineSourceTest.h"
#include "NonlinearSystemBase.h"
#include "SystemBase.h"

registerMooseObject("RayTracingTestApp", SingleTraceLineSourceTest);

InputParameters
SingleTraceLineSourceTest::validParams()
{
auto params = RepeatableRayStudy::validParams();
params.addParam<TagName>("residual_vector_tag",
"the vector tag for the residual tag you will accumulate into");
params.set<bool>("allow_other_flags_with_prekernels") = true;
return params;
}

SingleTraceLineSourceTest::SingleTraceLineSourceTest(const InputParameters & parameters)
: RepeatableRayStudy(parameters),
_residual_tag_name(getParam<TagName>("residual_vector_tag")),
_has_traced(false)
{
}

void
SingleTraceLineSourceTest::execute()
{
if (_current_execute_flag == EXEC_TIMESTEP_BEGIN)
{
_has_traced = false;
return;
}

if (_current_execute_flag == EXEC_NONLINEAR)
{
mooseAssert(_fe_problem.currentlyComputingJacobian(),
"Should be computing jacobian but is not.");
return;
}

const auto contribution_tag_id = _fe_problem.getVectorTagID(_residual_tag_name);
auto & nl = _fe_problem.getNonlinearSystemBase(_sys.number());
auto & contribution_vec = nl.getVector(contribution_tag_id);
if (!_has_traced)
{
contribution_vec.zero();
RayTracingStudy::execute();
contribution_vec.close();
_has_traced = true;
}

auto & residual_vec = nl.getVector(nl.residualVectorTag());
residual_vec.close();
residual_vec += contribution_vec;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
[Mesh]
type = GeneratedMesh
dim = 2
nx = 5
ny = 5
xmax = 5
ymax = 5
[]

[Variables/u]
order = FIRST
family = LAGRANGE
[]

[BCs/zero]
type = DirichletBC
variable = u
value = 0
boundary = 'top right bottom left'
[]

[Kernels/diffusion]
type = Diffusion
variable = u
[]

[Postprocessors/postprocessor]
type = FunctionValuePostprocessor
function = 3
execute_on = initial
[]

[RayKernels]
[constant_source]
type = LineSourceRayKernel
variable = u
value = 5
rays = constant_source
[]
[pp_source]
type = LineSourceRayKernel
variable = u
postprocessor = postprocessor
rays = pp_source
[]
[function_source]
type = LineSourceRayKernel
variable = u
function = 'x + 2 * y'
rays = function_source
[]
[mixed_source]
type = LineSourceRayKernel
variable = u
value = 5
postprocessor = postprocessor
function = 'x + 2 * y'
rays = mixed_source
[]
[data_source]
type = LineSourceRayKernel
variable = u
ray_data_factor_names = data
rays = data_source
[]
[aux_data_source]
type = LineSourceRayKernel
variable = u
ray_aux_data_factor_names = aux_data
rays = aux_data_source
[]
[]

[Executioner]
type = Steady
solve_type = PJFNK
petsc_options_iname = '-pc_type -pc_hypre_type'
petsc_options_value = 'hypre boomeramg'
[]

[Outputs]
exodus = true
[]
Original file line number Diff line number Diff line change
@@ -1,75 +1,4 @@
[Mesh]
type = GeneratedMesh
dim = 2
nx = 5
ny = 5
xmax = 5
ymax = 5
[]

[Variables/u]
order = FIRST
family = LAGRANGE
[]

[BCs/zero]
type = DirichletBC
variable = u
value = 0
boundary = 'top right bottom left'
[]

[Kernels/diffusion]
type = Diffusion
variable = u
[]

[Postprocessors/postprocessor]
type = FunctionValuePostprocessor
function = 3
execute_on = initial
[]

[RayKernels]
[constant_source]
type = LineSourceRayKernel
variable = u
value = 5
rays = constant_source
[]
[pp_source]
type = LineSourceRayKernel
variable = u
postprocessor = postprocessor
rays = pp_source
[]
[function_source]
type = LineSourceRayKernel
variable = u
function = 'x + 2 * y'
rays = function_source
[]
[mixed_source]
type = LineSourceRayKernel
variable = u
value = 5
postprocessor = postprocessor
function = 'x + 2 * y'
rays = mixed_source
[]
[data_source]
type = LineSourceRayKernel
variable = u
ray_data_factor_names = data
rays = data_source
[]
[aux_data_source]
type = LineSourceRayKernel
variable = u
ray_aux_data_factor_names = aux_data
rays = aux_data_source
[]
[]
!include line_source_base.i

[UserObjects/study]
type = RepeatableRayStudy
Expand Down Expand Up @@ -97,14 +26,3 @@
initial_ray_aux_data = '0; 0; 0; 0; 0; 10'
execute_on = PRE_KERNELS
[]

[Executioner]
type = Steady
solve_type = PJFNK
petsc_options_iname = '-pc_type -pc_hypre_type'
petsc_options_value = 'hypre boomeramg'
[]

[Outputs]
exodus = true
[]
Loading

0 comments on commit 2a74993

Please sign in to comment.