diff --git a/framework/include/problems/SubProblem.h b/framework/include/problems/SubProblem.h index 9cc95d40f6a6..8428e1d8c1ca 100644 --- a/framework/include/problems/SubProblem.h +++ b/framework/include/problems/SubProblem.h @@ -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. */ @@ -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 _not_zeroed_tagged_vectors; + private: /** * @return whether a given variable name is in the solver systems (reflected by the first diff --git a/framework/src/problems/FEProblemBase.C b/framework/src/problems/FEProblemBase.C index b13e022793e3..31bbac8f9827 100644 --- a/framework/src/problems/FEProblemBase.C +++ b/framework/src/problems/FEProblemBase.C @@ -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>>( + "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>>( "extra_tag_matrices", {}, @@ -615,6 +621,15 @@ FEProblemBase::createTagVectors() _nl[nl_sys_num]->addVector(tag, false, GHOSTED); } + auto & not_zeroed_vectors = getParam>>("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>>("extra_tag_matrices"); for (const auto nl_sys_num : index_range(matrices)) diff --git a/framework/src/problems/SubProblem.C b/framework/src/problems/SubProblem.C index 84b6e5a08342..d86d001b05b3 100644 --- a/framework/src/problems/SubProblem.C +++ b/framework/src/problems/SubProblem.C @@ -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 { diff --git a/framework/src/systems/SystemBase.C b/framework/src/systems/SystemBase.C index b42544c57568..7bcce5793f52 100644 --- a/framework/src/systems/SystemBase.C +++ b/framework/src/systems/SystemBase.C @@ -651,7 +651,6 @@ SystemBase::closeTaggedVector(const TagID tag) "' in system '", name(), "' because there is no vector associated with that tag"); - getVector(tag).close(); } @@ -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 diff --git a/modules/ray_tracing/src/userobjects/RayTracingStudy.C b/modules/ray_tracing/src/userobjects/RayTracingStudy.C index 609571691094..902cb33db454 100644 --- a/modules/ray_tracing/src/userobjects/RayTracingStudy.C +++ b/modules/ray_tracing/src/userobjects/RayTracingStudy.C @@ -92,6 +92,12 @@ RayTracingStudy::validParams() "Trace intersections are not verified regardless of this parameter in " "optimized modes (opt, oprof)."); + params.addParam("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("execute_on", true); exec_enum.addAvailableFlags(EXEC_PRE_KERNELS); @@ -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("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 " diff --git a/modules/ray_tracing/test/include/userobjects/SingleTraceLineSourceTest.h b/modules/ray_tracing/test/include/userobjects/SingleTraceLineSourceTest.h new file mode 100644 index 000000000000..e45f7ced07eb --- /dev/null +++ b/modules/ray_tracing/test/include/userobjects/SingleTraceLineSourceTest.h @@ -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; +}; diff --git a/modules/ray_tracing/test/src/userobjects/SingleTraceLineSourceTest.C b/modules/ray_tracing/test/src/userobjects/SingleTraceLineSourceTest.C new file mode 100644 index 000000000000..cf1235c713ae --- /dev/null +++ b/modules/ray_tracing/test/src/userobjects/SingleTraceLineSourceTest.C @@ -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("residual_vector_tag", + "the vector tag for the residual tag you will accumulate into"); + params.set("allow_other_flags_with_prekernels") = true; + return params; +} + +SingleTraceLineSourceTest::SingleTraceLineSourceTest(const InputParameters & parameters) + : RepeatableRayStudy(parameters), + _residual_tag_name(getParam("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; +} diff --git a/modules/ray_tracing/test/tests/raykernels/line_source_ray_kernel/gold/single_trace_line_source_ray_kernel_out.e b/modules/ray_tracing/test/tests/raykernels/line_source_ray_kernel/gold/single_trace_line_source_ray_kernel_out.e new file mode 120000 index 000000000000..6ed15e9fe25d --- /dev/null +++ b/modules/ray_tracing/test/tests/raykernels/line_source_ray_kernel/gold/single_trace_line_source_ray_kernel_out.e @@ -0,0 +1 @@ +line_source_ray_kernel_out.e \ No newline at end of file diff --git a/modules/ray_tracing/test/tests/raykernels/line_source_ray_kernel/line_source_base.i b/modules/ray_tracing/test/tests/raykernels/line_source_ray_kernel/line_source_base.i new file mode 100644 index 000000000000..660f2c740958 --- /dev/null +++ b/modules/ray_tracing/test/tests/raykernels/line_source_ray_kernel/line_source_base.i @@ -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 +[] diff --git a/modules/ray_tracing/test/tests/raykernels/line_source_ray_kernel/line_source_ray_kernel.i b/modules/ray_tracing/test/tests/raykernels/line_source_ray_kernel/line_source_ray_kernel.i index a4e88bcca460..5a278449a461 100644 --- a/modules/ray_tracing/test/tests/raykernels/line_source_ray_kernel/line_source_ray_kernel.i +++ b/modules/ray_tracing/test/tests/raykernels/line_source_ray_kernel/line_source_ray_kernel.i @@ -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 @@ -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 -[] diff --git a/modules/ray_tracing/test/tests/raykernels/line_source_ray_kernel/single_trace_line_source_ray_kernel.i b/modules/ray_tracing/test/tests/raykernels/line_source_ray_kernel/single_trace_line_source_ray_kernel.i new file mode 100644 index 000000000000..500fb46fd20f --- /dev/null +++ b/modules/ray_tracing/test/tests/raykernels/line_source_ray_kernel/single_trace_line_source_ray_kernel.i @@ -0,0 +1,54 @@ +!include line_source_base.i + +[Problem] + not_zeroed_tag_vectors = ray_residual +[] + +[RayKernels] + [constant_source] + vector_tags = ray_residual + [] + [pp_source] + vector_tags = ray_residual + [] + [function_source] + vector_tags = ray_residual + [] + [mixed_source] + vector_tags = ray_residual + [] + [data_source] + vector_tags = ray_residual + [] + [aux_data_source] + vector_tags = ray_residual + [] +[] + +[UserObjects/study] + type = SingleTraceLineSourceTest + start_points = '0 2 0 + 0.5 0.5 0 + 1 1 0 + 5 5 0 + 2 2 0 + 3 3 0' + end_points = '3 5 0 + 4.5 1.5 0 + 2 2 0 + 4 1 0 + 3 1 0 + 3 2 0' + names = 'constant_source + pp_source + function_source + mixed_source + data_source + aux_data_source' + ray_data_names = 'data' + ray_aux_data_names = 'aux_data' + initial_ray_data = '0; 0; 0; 0; 8; 0' + initial_ray_aux_data = '0; 0; 0; 0; 0; 10' + residual_vector_tag = ray_residual + execute_on = 'TIMESTEP_BEGIN PRE_KERNELS' +[] diff --git a/modules/ray_tracing/test/tests/raykernels/line_source_ray_kernel/tests b/modules/ray_tracing/test/tests/raykernels/line_source_ray_kernel/tests index 543c582847bd..8b6056149e76 100644 --- a/modules/ray_tracing/test/tests/raykernels/line_source_ray_kernel/tests +++ b/modules/ray_tracing/test/tests/raykernels/line_source_ray_kernel/tests @@ -2,11 +2,21 @@ design = 'LineSourceRayKernel.md' issues = '#16028' - [test] - type = 'Exodiff' - input = 'line_source_ray_kernel.i' - exodiff = 'line_source_ray_kernel_out.e' - requirement = 'The system shall provide an ability to produce a source term along a line.' + [line_source] + requirement = 'The system shall provide an ability to produce a source term along a line' + [test] + type = 'Exodiff' + input = 'line_source_ray_kernel.i' + exodiff = 'line_source_ray_kernel_out.e' + detail = 'while tracing the ray on every residual evaluation, and' + [] + [single_trace_test] + type = 'Exodiff' + input = 'single_trace_line_source_ray_kernel.i' + exodiff = 'single_trace_line_source_ray_kernel_out.e' + detail = 'while only tracing the ray once.' + allow_test_objects = true + [] [] [simple_diffusion] requirement = 'The system shall provide an ability to solve a 2D diffusion problem with a line source term, with ' diff --git a/test/tests/tag/gold/not_zeroed_tag_vector_out.e b/test/tests/tag/gold/not_zeroed_tag_vector_out.e new file mode 100644 index 000000000000..0c48424e9b0f Binary files /dev/null and b/test/tests/tag/gold/not_zeroed_tag_vector_out.e differ diff --git a/test/tests/tag/not_zeroed_tag_vector.i b/test/tests/tag/not_zeroed_tag_vector.i new file mode 100644 index 000000000000..6d52f56eaab0 --- /dev/null +++ b/test/tests/tag/not_zeroed_tag_vector.i @@ -0,0 +1,73 @@ +[Problem] + extra_tag_vectors = zeroed_tag + not_zeroed_tag_vectors = not_zeroed_tag +[] + +[Mesh] + type = GeneratedMesh + dim = 1 + nx = 2 + xmin = -1 + xmax = 1 +[] + +[Variables] + [u] + [] +[] + +[Kernels] + [null] + type = NullKernel + variable = u + [] +[] + +[Functions] + [switch_off] + type = ParsedFunction + expression = 'if(t < 1.0001, 1, 0)' + [] +[] + +[DiracKernels] + [point_source1] + type = FunctionDiracSource + variable = u + function = switch_off + point = '0 0 0' + vector_tags = 'zeroed_tag not_zeroed_tag' + [] +[] + +[AuxVariables] + [not_zeroed_tag] + [] + [zeroed_tag] + [] +[] + +[AuxKernels] + [not_zeroed_tag_value] + type = TagVectorAux + variable = not_zeroed_tag + vector_tag = not_zeroed_tag + v = u + [] + [zeroed_tag_value] + type = TagVectorAux + variable = zeroed_tag + vector_tag = zeroed_tag + v = u + [] +[] + +[Executioner] + type = Transient + dt = 1 + num_steps = 2 +[] + +[Outputs] + exodus = true +[] diff --git a/test/tests/tag/tests b/test/tests/tag/tests index 93d1fca836c8..cacd6769d4e5 100644 --- a/test/tests/tag/tests +++ b/test/tests/tag/tests @@ -2,6 +2,13 @@ issues = '#9669' design = 'interfaces/TaggingInterface.md' + [not_zeroed_vector_tag] + type = Exodiff + requirement = 'The system shall support the ability for a the user to create a tagged vector which is not automatically zeroed.' + input = 'not_zeroed_tag_vector.i' + exodiff = 'not_zeroed_tag_vector_out.e' + [] + [general] requirement = 'The system shall support the ability for a specific calculation to fill a labeled ' 'or "tagged"'