Reporter System
The Reporter system may be considered a generalization of the Postprocessor and VectorPostprocessor systems. Each Reporter object may declare any number of values with any types. By contrast, post-processors each declare a single, scalar, Real
value, and while vector post-processors declare any number of values, they must all be of type std::vector<Real>
. Reporters can declare both scalar and vector data of any type, including complex data and arbitrary classes/structs. The only requirement on the data type is that the types must have associated dataLoad
and dataStore
specializations (see DataIO).
The reporter system uses a producer/consumer relationship: reporter objects "produce" data values, which then may be "consumed" by other objects.
Producing Reporter Data
As noted above, Reporter objects declare any number of values of any type. Note that these values are automatically registered as restartable. For complex types, data serialization routines might be needed; see DataIO for more information.
In the Reporter header file, Reporter values are declared as non-const reference members of the desired types, for example:
int & _int;
(moose/test/include/reporters/TestReporter.h)These references are initialized using the declareValue
and declareValueByName
methods. Note that it is possible to indicate how the value is to be computed, with respect to parallelism, by setting the calculation mode; see Reporter Context and Modes for more information. The declareValueByName
method uses the supplied string directly as the value name, while the declareValue
method gets the value name from the supplied ReporterValueName
parameter declared in validParams
. For example, in validParams
,
params.addParam<ReporterValueName>("int_name", "int", "The name of the integer data");
(moose/test/src/reporters/TestReporter.C)Then the Reporter data can be declared using declareValue
:
_int(declareValue<int>("int_name", 1980)),
(moose/test/src/reporters/TestReporter.C)Note that in this example, an initial value is supplied for the data.
The calculation of the value(s) occurs by overriding the execute
method and updating the values:
void
TestDeclareReporter::execute()
{
_int += 1;
_real = 1.2345;
_vector = {1, 1.1, 1.2};
_string = "string";
if (processor_id() == 0)
_bcast_value = 42;
_gather_value.resize(1, processor_id());
if (_distributed_vector)
{
_distributed_vector->resize(_vector.size());
(*_distributed_vector)[0] = processor_id();
for (unsigned int i = 1; i < _distributed_vector->size(); ++i)
(*_distributed_vector)[i] = (*_distributed_vector)[i - 1] * 10;
}
}
(moose/test/src/reporters/TestReporter.C)Consuming Reporter Data
Any object that inherits from the ReporterInterface
may consume a value produced by a Reporter. Values are retrieved in a similar fashion as declared, but use a constant reference. For example, values to be consumed should create a reference in the class definition:
const int & _int;
(moose/test/include/reporters/TestReporter.h)In the initialization list, the getReporterValue
or getReporterValueByName
method is used to initialize the reference:
_int(getReporterValue<int>("int_reporter")),
(moose/test/src/reporters/TestReporter.C)Similarly to declareValue
and declareValueByName
, getReporterValue
uses the provided string for the value name, whereas getReporterValueByName
gets the value name from the parameter named by the provided string. In the example above, the following appears in validParams
:
params.addRequiredParam<ReporterName>("int_reporter", "'int' reporter name");
(moose/test/src/reporters/TestReporter.C)The get methods accept a ReporterName
object, which is simply the combination of the name of the producing Reporter object and the name of the reporter value. In the input file, the ReporterName is provided as follows, where "a" is the name of the Reporter object in the [Reporters]
block of the input file that is producing data with the name "int", which is the name given to the data within the declareValue
/declareValueByName
method of that object:
int_reporter = a/int
(moose/test/tests/reporters/base/base.i)Outputting Reporter Data
Reporter values may be output in two formats: comma-separated values (CSV) and JSON. CSV output is limited to Reporter values with a type of Real
or std::vector<Real>
. JSON output will work for any type that has a to_json
function; see JSON for more details.
Reporter Context and Modes
Reporter values use a context system for performing parallel operations automatically. The default context allows Reporter values to be produced and consumed in various modes. Depending on the mode produced/consumed, parallel operations will be performed automatically. The following modes exist for the default context:
REPORTER_MODE_ROOT: Values exist only on the root processor.
REPORTER_MODE_REPLICATED: Values exist and are identical on all processors.
REPORTER_MODE_DISTRIBUTED: Values exist and are different across processors.
Values can be produced or consumed in any of the prescribed modes. When consumed, the mode of production is checked against the mode of consumption. Table 1 details the actions taken by the various possible modes of production and consumption for a Reporter value.
Producer Mode | Consumer Mode | Operation |
---|---|---|
ROOT | ROOT | Do nothing |
REPLICATED | ROOT | Do nothing |
REPLICATED | REPLICATED | Do nothing |
DISTRIBUTED | DISTRIBUTED | Do nothing |
ROOT | REPLICATED | MPI Broadcast |
ROOT | DISTRIBUTED | Error |
REPLICATED | DISTRIBUTED | Error |
DISTRIBUTED | ROOT | Error |
DISTRIBUTED | REPLICATED | Error |
The declareValue
and declareValueByName
methods allow for non-default context to be defined. For example, the following line declares a Reporter value to use the gather context object. A list of available contexts follows the code snippet.
_gather_value(declareValueByName<std::vector<dof_id_type>, ReporterGatherContext>("gather")),
(moose/test/src/reporters/TestReporter.C)ReporterBroadcastContext
Automatically performs an MPI broadcast of a specified value on the root processor to all processors.
ReporterScatterContext
Automatically performs an MPI scatter of a vector of data on the root processor to all processors.
ReporterGatherContext
Automatically performs an MPI gather to a vector of data on the root processor from all processors.
Reporter Debug Output
The ReporterDebugOutput output can be added to output to screen all of the Reporter values that were declared and requested, along with their types, producers, contexts, consumers, and consumer modes. This debug output can also be enabled with the Debug/show_reporters
parameter.
Available Objects
- Moose App
- AccumulateReporterReporter which accumulates the value of a inputted reporter value over time into a vector reporter value of the same type.
- ConstantReporterReporter with constant values to be accessed by other objects, can be modified using transfers.
- ElementVariableStatisticsElement reporter to get statistics for a coupled variable. This can be transfered to other apps.
- ExtraIDIntegralReporterThis ExtraIDIntegralReporter source code is to integrate variables based on parsed extra IDs based on reporter system.
- IterationInfoReport the time and iteration information for the simulation.
- MeshInfoReport mesh information, such as the number of elements, nodes, and degrees of freedom.
- MeshMetaDataReporterReports the mesh meta data.
- NodalVariableStatisticsNodal reporter to get statistics for a coupled variable. This can be transfered to other apps.
- PerfGraphReporterReports the full performance graph from the PerfGraph.
- RestartableDataReporterReports restartable data and restartable meta data.
- SolutionInvalidityReporterReports the Summary Table of Solution Invalid Counts.
Available Actions
- Moose App
- AddReporterActionAdd a Reporter object to the simulation.
(moose/test/include/reporters/TestReporter.h)
// 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 "GeneralReporter.h"
class TestDeclareReporter : public GeneralReporter
{
public:
static InputParameters validParams();
TestDeclareReporter(const InputParameters & parameters);
virtual void initialize() override {}
virtual void finalize() override {}
virtual void execute() override;
protected:
int & _int; // MooseDocs:producer
Real & _real;
std::vector<Real> & _vector;
std::string & _string;
Real & _bcast_value;
std::vector<dof_id_type> _values_to_scatter;
dof_id_type & _scatter_value;
std::vector<dof_id_type> _values_to_gather;
std::vector<dof_id_type> & _gather_value;
std::vector<dof_id_type> * _distributed_vector;
};
class TestGetReporter : public GeneralReporter
{
public:
static InputParameters validParams();
TestGetReporter(const InputParameters & parameters);
virtual void initialize() override {}
virtual void finalize() override {}
virtual void execute() override;
protected:
const int & _int; // MooseDocs:consumer
const int & _int_old;
const Real & _real;
const std::vector<Real> & _vector;
const std::string & _string;
const Real & _bcast_value;
const dof_id_type & _scatter_value;
const std::vector<dof_id_type> & _gather_value;
};
class TestDeclareInitialSetupReporter : public GeneralReporter
{
public:
static InputParameters validParams();
TestDeclareInitialSetupReporter(const InputParameters & parameters);
virtual void initialSetup() override;
virtual void initialize() override {}
virtual void finalize() override {}
virtual void execute() override {}
};
class TestGetReporterDeclaredInInitialSetupReporter : public GeneralReporter
{
public:
static InputParameters validParams();
TestGetReporterDeclaredInInitialSetupReporter(const InputParameters & parameters);
virtual void initialize() override {}
virtual void finalize() override {}
virtual void execute() override;
protected:
const Real & _value_declared_in_initial_setup;
Real & _the_value_of_the_reporter;
};
class TestDeclareErrorsReporter : public GeneralReporter
{
public:
static InputParameters validParams();
TestDeclareErrorsReporter(const InputParameters & parameters);
virtual void initialize() override {}
virtual void finalize() override {}
virtual void execute() override {}
};
(moose/test/src/reporters/TestReporter.C)
// 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
#include "TestReporter.h"
registerMooseObject("MooseTestApp", TestDeclareReporter);
registerMooseObject("MooseTestApp", TestGetReporter);
registerMooseObject("MooseTestApp", TestDeclareInitialSetupReporter);
registerMooseObject("MooseTestApp", TestGetReporterDeclaredInInitialSetupReporter);
registerMooseObject("MooseTestApp", TestDeclareErrorsReporter);
InputParameters
TestDeclareReporter::validParams()
{
InputParameters params = GeneralReporter::validParams();
// MooseDocs:param_begin
params.addParam<ReporterValueName>("int_name", "int", "The name of the integer data");
// MooseDocs:param_end
params.addParam<ReporterValueName>("distributed_vector_name", "Distributed vector to produce.");
return params;
}
TestDeclareReporter::TestDeclareReporter(const InputParameters & parameters)
: GeneralReporter(parameters),
_int(declareValue<int>("int_name", 1980)), // MooseDocs:producer
_real(declareValueByName<Real>("real")),
_vector(declareValueByName<std::vector<Real>>("vector")),
_string(declareValueByName<std::string>("string")),
_bcast_value(declareValueByName<Real, ReporterBroadcastContext>("broadcast")),
_scatter_value(
declareValueByName<dof_id_type, ReporterScatterContext>("scatter", _values_to_scatter)),
_gather_value(declareValueByName<std::vector<dof_id_type>, ReporterGatherContext>(
"gather")), // MooseDocs:gather
_distributed_vector(isParamValid("distributed_vector_name")
? &declareValue<std::vector<dof_id_type>>("distributed_vector_name",
REPORTER_MODE_DISTRIBUTED)
: nullptr)
{
if (processor_id() == 0)
for (dof_id_type rank = 0; rank < n_processors(); ++rank)
_values_to_scatter.push_back(rank);
}
// MooseDocs:execute_begin
void
TestDeclareReporter::execute()
{
_int += 1;
_real = 1.2345;
_vector = {1, 1.1, 1.2};
_string = "string";
if (processor_id() == 0)
_bcast_value = 42;
_gather_value.resize(1, processor_id());
if (_distributed_vector)
{
_distributed_vector->resize(_vector.size());
(*_distributed_vector)[0] = processor_id();
for (unsigned int i = 1; i < _distributed_vector->size(); ++i)
(*_distributed_vector)[i] = (*_distributed_vector)[i - 1] * 10;
}
}
// MooseDocs:execute_end
InputParameters
TestGetReporter::validParams()
{
InputParameters params = GeneralReporter::validParams();
params.addRequiredParam<ReporterName>("int_reporter", "'int' reporter name");
params.addRequiredParam<ReporterName>("real_reporter", "'real' reporter name");
params.addRequiredParam<ReporterName>("vector_reporter", "'vector' reporter name");
params.addRequiredParam<ReporterName>("string_reporter", "'string' reporter name");
params.addRequiredParam<ReporterName>("broadcast_reporter", "'broadcast' reporter name");
params.addRequiredParam<ReporterName>("scatter_reporter", "'scatter' reporter name");
params.addRequiredParam<ReporterName>("gather_reporter", "'gather' reporter name");
return params;
}
TestGetReporter::TestGetReporter(const InputParameters & parameters)
: GeneralReporter(parameters),
_int(getReporterValue<int>("int_reporter")), // MooseDocs:consumer
_int_old(getReporterValue<int>("int_reporter", 1)),
_real(getReporterValue<Real>("real_reporter")),
_vector(getReporterValue<std::vector<Real>>("vector_reporter")),
_string(getReporterValue<std::string>("string_reporter")),
_bcast_value(getReporterValue<Real>("broadcast_reporter")),
_scatter_value(getReporterValue<dof_id_type>("scatter_reporter")),
_gather_value(getReporterValue<std::vector<dof_id_type>>("gather_reporter"))
{
}
void
TestGetReporter::execute()
{
if (_int != 1980 + _t_step)
mooseError("int reporter test failed: ", _int, " != ", 1980 + _t_step);
if (_real != 1.2345)
mooseError("Real reporter test failed");
if (_vector != std::vector<Real>({1., 1.1, 1.2}))
mooseError("std::vector<Real> reporter test failed");
if (_string != "string")
mooseError("std::string reporter test failed");
if (_t_step == 0 && _int_old != 1980)
mooseError("int_old on timestep 0 failed: ", _int_old, " != ", 1980);
if (_t_step > 0 && _int_old != 1980 + (_t_step - 1))
mooseError(
"int_old on timestep ", _t_step, " failed: ", _int_old, " != ", 1980 + (_t_step - 1));
if (_bcast_value != 42)
mooseError("Broadcast reporter test failed");
if (_scatter_value != processor_id())
mooseError("Scatter reporter test failed");
if (processor_id() == 0)
{
std::vector<dof_id_type> gold;
for (dof_id_type id = 0; id < n_processors(); ++id)
gold.push_back(id);
if (_gather_value != gold)
mooseError("Gather reporter test failed!");
}
}
InputParameters
TestDeclareInitialSetupReporter::validParams()
{
InputParameters params = GeneralReporter::validParams();
params.addRequiredParam<Real>("value", "The value to report.");
return params;
}
TestDeclareInitialSetupReporter::TestDeclareInitialSetupReporter(const InputParameters & parameters)
: GeneralReporter(parameters)
{
}
void
TestDeclareInitialSetupReporter::initialSetup()
{
Real & value = declareValueByName<Real>("value");
value = getParam<Real>("value");
}
InputParameters
TestGetReporterDeclaredInInitialSetupReporter::validParams()
{
InputParameters params = GeneralReporter::validParams();
params.addRequiredParam<ReporterName>("other_reporter",
"The reporter name that was declared in initialSetup");
return params;
}
TestGetReporterDeclaredInInitialSetupReporter::TestGetReporterDeclaredInInitialSetupReporter(
const InputParameters & parameters)
: GeneralReporter(parameters),
_value_declared_in_initial_setup(getReporterValue<Real>("other_reporter")),
_the_value_of_the_reporter(declareValueByName<Real>("other_value"))
{
}
void
TestGetReporterDeclaredInInitialSetupReporter::execute()
{
_the_value_of_the_reporter = _value_declared_in_initial_setup;
}
InputParameters
TestDeclareErrorsReporter::validParams()
{
InputParameters params = GeneralReporter::validParams();
params.addRequiredParam<ReporterValueName>("value", "A reporter value name");
params.addParam<bool>("missing_param", false, "True to test the error for a missing parameter");
params.addParam<bool>("bad_param", false, "True to test the error for a bad parameter type");
params.addParam<bool>("already_declared", false, "Test declaring a value multiple times");
params.addParam<bool>("requested_different_type",
false,
"Test declaring a value that has been requested with a differentt type");
return params;
}
TestDeclareErrorsReporter::TestDeclareErrorsReporter(const InputParameters & parameters)
: GeneralReporter(parameters)
{
if (getParam<bool>("missing_param"))
declareValue<int>("some_missing_parm");
if (getParam<bool>("bad_param"))
declareValue<int>("bad_param");
if (getParam<bool>("already_declared"))
{
declareValueByName<int>("value_name");
declareValueByName<Real>("value_name");
}
if (getParam<bool>("requested_different_type"))
{
getReporterValueByName<int>(name() + "/value_name");
declareValueByName<Real>("value_name");
}
}
(moose/test/src/reporters/TestReporter.C)
// 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
#include "TestReporter.h"
registerMooseObject("MooseTestApp", TestDeclareReporter);
registerMooseObject("MooseTestApp", TestGetReporter);
registerMooseObject("MooseTestApp", TestDeclareInitialSetupReporter);
registerMooseObject("MooseTestApp", TestGetReporterDeclaredInInitialSetupReporter);
registerMooseObject("MooseTestApp", TestDeclareErrorsReporter);
InputParameters
TestDeclareReporter::validParams()
{
InputParameters params = GeneralReporter::validParams();
// MooseDocs:param_begin
params.addParam<ReporterValueName>("int_name", "int", "The name of the integer data");
// MooseDocs:param_end
params.addParam<ReporterValueName>("distributed_vector_name", "Distributed vector to produce.");
return params;
}
TestDeclareReporter::TestDeclareReporter(const InputParameters & parameters)
: GeneralReporter(parameters),
_int(declareValue<int>("int_name", 1980)), // MooseDocs:producer
_real(declareValueByName<Real>("real")),
_vector(declareValueByName<std::vector<Real>>("vector")),
_string(declareValueByName<std::string>("string")),
_bcast_value(declareValueByName<Real, ReporterBroadcastContext>("broadcast")),
_scatter_value(
declareValueByName<dof_id_type, ReporterScatterContext>("scatter", _values_to_scatter)),
_gather_value(declareValueByName<std::vector<dof_id_type>, ReporterGatherContext>(
"gather")), // MooseDocs:gather
_distributed_vector(isParamValid("distributed_vector_name")
? &declareValue<std::vector<dof_id_type>>("distributed_vector_name",
REPORTER_MODE_DISTRIBUTED)
: nullptr)
{
if (processor_id() == 0)
for (dof_id_type rank = 0; rank < n_processors(); ++rank)
_values_to_scatter.push_back(rank);
}
// MooseDocs:execute_begin
void
TestDeclareReporter::execute()
{
_int += 1;
_real = 1.2345;
_vector = {1, 1.1, 1.2};
_string = "string";
if (processor_id() == 0)
_bcast_value = 42;
_gather_value.resize(1, processor_id());
if (_distributed_vector)
{
_distributed_vector->resize(_vector.size());
(*_distributed_vector)[0] = processor_id();
for (unsigned int i = 1; i < _distributed_vector->size(); ++i)
(*_distributed_vector)[i] = (*_distributed_vector)[i - 1] * 10;
}
}
// MooseDocs:execute_end
InputParameters
TestGetReporter::validParams()
{
InputParameters params = GeneralReporter::validParams();
params.addRequiredParam<ReporterName>("int_reporter", "'int' reporter name");
params.addRequiredParam<ReporterName>("real_reporter", "'real' reporter name");
params.addRequiredParam<ReporterName>("vector_reporter", "'vector' reporter name");
params.addRequiredParam<ReporterName>("string_reporter", "'string' reporter name");
params.addRequiredParam<ReporterName>("broadcast_reporter", "'broadcast' reporter name");
params.addRequiredParam<ReporterName>("scatter_reporter", "'scatter' reporter name");
params.addRequiredParam<ReporterName>("gather_reporter", "'gather' reporter name");
return params;
}
TestGetReporter::TestGetReporter(const InputParameters & parameters)
: GeneralReporter(parameters),
_int(getReporterValue<int>("int_reporter")), // MooseDocs:consumer
_int_old(getReporterValue<int>("int_reporter", 1)),
_real(getReporterValue<Real>("real_reporter")),
_vector(getReporterValue<std::vector<Real>>("vector_reporter")),
_string(getReporterValue<std::string>("string_reporter")),
_bcast_value(getReporterValue<Real>("broadcast_reporter")),
_scatter_value(getReporterValue<dof_id_type>("scatter_reporter")),
_gather_value(getReporterValue<std::vector<dof_id_type>>("gather_reporter"))
{
}
void
TestGetReporter::execute()
{
if (_int != 1980 + _t_step)
mooseError("int reporter test failed: ", _int, " != ", 1980 + _t_step);
if (_real != 1.2345)
mooseError("Real reporter test failed");
if (_vector != std::vector<Real>({1., 1.1, 1.2}))
mooseError("std::vector<Real> reporter test failed");
if (_string != "string")
mooseError("std::string reporter test failed");
if (_t_step == 0 && _int_old != 1980)
mooseError("int_old on timestep 0 failed: ", _int_old, " != ", 1980);
if (_t_step > 0 && _int_old != 1980 + (_t_step - 1))
mooseError(
"int_old on timestep ", _t_step, " failed: ", _int_old, " != ", 1980 + (_t_step - 1));
if (_bcast_value != 42)
mooseError("Broadcast reporter test failed");
if (_scatter_value != processor_id())
mooseError("Scatter reporter test failed");
if (processor_id() == 0)
{
std::vector<dof_id_type> gold;
for (dof_id_type id = 0; id < n_processors(); ++id)
gold.push_back(id);
if (_gather_value != gold)
mooseError("Gather reporter test failed!");
}
}
InputParameters
TestDeclareInitialSetupReporter::validParams()
{
InputParameters params = GeneralReporter::validParams();
params.addRequiredParam<Real>("value", "The value to report.");
return params;
}
TestDeclareInitialSetupReporter::TestDeclareInitialSetupReporter(const InputParameters & parameters)
: GeneralReporter(parameters)
{
}
void
TestDeclareInitialSetupReporter::initialSetup()
{
Real & value = declareValueByName<Real>("value");
value = getParam<Real>("value");
}
InputParameters
TestGetReporterDeclaredInInitialSetupReporter::validParams()
{
InputParameters params = GeneralReporter::validParams();
params.addRequiredParam<ReporterName>("other_reporter",
"The reporter name that was declared in initialSetup");
return params;
}
TestGetReporterDeclaredInInitialSetupReporter::TestGetReporterDeclaredInInitialSetupReporter(
const InputParameters & parameters)
: GeneralReporter(parameters),
_value_declared_in_initial_setup(getReporterValue<Real>("other_reporter")),
_the_value_of_the_reporter(declareValueByName<Real>("other_value"))
{
}
void
TestGetReporterDeclaredInInitialSetupReporter::execute()
{
_the_value_of_the_reporter = _value_declared_in_initial_setup;
}
InputParameters
TestDeclareErrorsReporter::validParams()
{
InputParameters params = GeneralReporter::validParams();
params.addRequiredParam<ReporterValueName>("value", "A reporter value name");
params.addParam<bool>("missing_param", false, "True to test the error for a missing parameter");
params.addParam<bool>("bad_param", false, "True to test the error for a bad parameter type");
params.addParam<bool>("already_declared", false, "Test declaring a value multiple times");
params.addParam<bool>("requested_different_type",
false,
"Test declaring a value that has been requested with a differentt type");
return params;
}
TestDeclareErrorsReporter::TestDeclareErrorsReporter(const InputParameters & parameters)
: GeneralReporter(parameters)
{
if (getParam<bool>("missing_param"))
declareValue<int>("some_missing_parm");
if (getParam<bool>("bad_param"))
declareValue<int>("bad_param");
if (getParam<bool>("already_declared"))
{
declareValueByName<int>("value_name");
declareValueByName<Real>("value_name");
}
if (getParam<bool>("requested_different_type"))
{
getReporterValueByName<int>(name() + "/value_name");
declareValueByName<Real>("value_name");
}
}
(moose/test/src/reporters/TestReporter.C)
// 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
#include "TestReporter.h"
registerMooseObject("MooseTestApp", TestDeclareReporter);
registerMooseObject("MooseTestApp", TestGetReporter);
registerMooseObject("MooseTestApp", TestDeclareInitialSetupReporter);
registerMooseObject("MooseTestApp", TestGetReporterDeclaredInInitialSetupReporter);
registerMooseObject("MooseTestApp", TestDeclareErrorsReporter);
InputParameters
TestDeclareReporter::validParams()
{
InputParameters params = GeneralReporter::validParams();
// MooseDocs:param_begin
params.addParam<ReporterValueName>("int_name", "int", "The name of the integer data");
// MooseDocs:param_end
params.addParam<ReporterValueName>("distributed_vector_name", "Distributed vector to produce.");
return params;
}
TestDeclareReporter::TestDeclareReporter(const InputParameters & parameters)
: GeneralReporter(parameters),
_int(declareValue<int>("int_name", 1980)), // MooseDocs:producer
_real(declareValueByName<Real>("real")),
_vector(declareValueByName<std::vector<Real>>("vector")),
_string(declareValueByName<std::string>("string")),
_bcast_value(declareValueByName<Real, ReporterBroadcastContext>("broadcast")),
_scatter_value(
declareValueByName<dof_id_type, ReporterScatterContext>("scatter", _values_to_scatter)),
_gather_value(declareValueByName<std::vector<dof_id_type>, ReporterGatherContext>(
"gather")), // MooseDocs:gather
_distributed_vector(isParamValid("distributed_vector_name")
? &declareValue<std::vector<dof_id_type>>("distributed_vector_name",
REPORTER_MODE_DISTRIBUTED)
: nullptr)
{
if (processor_id() == 0)
for (dof_id_type rank = 0; rank < n_processors(); ++rank)
_values_to_scatter.push_back(rank);
}
// MooseDocs:execute_begin
void
TestDeclareReporter::execute()
{
_int += 1;
_real = 1.2345;
_vector = {1, 1.1, 1.2};
_string = "string";
if (processor_id() == 0)
_bcast_value = 42;
_gather_value.resize(1, processor_id());
if (_distributed_vector)
{
_distributed_vector->resize(_vector.size());
(*_distributed_vector)[0] = processor_id();
for (unsigned int i = 1; i < _distributed_vector->size(); ++i)
(*_distributed_vector)[i] = (*_distributed_vector)[i - 1] * 10;
}
}
// MooseDocs:execute_end
InputParameters
TestGetReporter::validParams()
{
InputParameters params = GeneralReporter::validParams();
params.addRequiredParam<ReporterName>("int_reporter", "'int' reporter name");
params.addRequiredParam<ReporterName>("real_reporter", "'real' reporter name");
params.addRequiredParam<ReporterName>("vector_reporter", "'vector' reporter name");
params.addRequiredParam<ReporterName>("string_reporter", "'string' reporter name");
params.addRequiredParam<ReporterName>("broadcast_reporter", "'broadcast' reporter name");
params.addRequiredParam<ReporterName>("scatter_reporter", "'scatter' reporter name");
params.addRequiredParam<ReporterName>("gather_reporter", "'gather' reporter name");
return params;
}
TestGetReporter::TestGetReporter(const InputParameters & parameters)
: GeneralReporter(parameters),
_int(getReporterValue<int>("int_reporter")), // MooseDocs:consumer
_int_old(getReporterValue<int>("int_reporter", 1)),
_real(getReporterValue<Real>("real_reporter")),
_vector(getReporterValue<std::vector<Real>>("vector_reporter")),
_string(getReporterValue<std::string>("string_reporter")),
_bcast_value(getReporterValue<Real>("broadcast_reporter")),
_scatter_value(getReporterValue<dof_id_type>("scatter_reporter")),
_gather_value(getReporterValue<std::vector<dof_id_type>>("gather_reporter"))
{
}
void
TestGetReporter::execute()
{
if (_int != 1980 + _t_step)
mooseError("int reporter test failed: ", _int, " != ", 1980 + _t_step);
if (_real != 1.2345)
mooseError("Real reporter test failed");
if (_vector != std::vector<Real>({1., 1.1, 1.2}))
mooseError("std::vector<Real> reporter test failed");
if (_string != "string")
mooseError("std::string reporter test failed");
if (_t_step == 0 && _int_old != 1980)
mooseError("int_old on timestep 0 failed: ", _int_old, " != ", 1980);
if (_t_step > 0 && _int_old != 1980 + (_t_step - 1))
mooseError(
"int_old on timestep ", _t_step, " failed: ", _int_old, " != ", 1980 + (_t_step - 1));
if (_bcast_value != 42)
mooseError("Broadcast reporter test failed");
if (_scatter_value != processor_id())
mooseError("Scatter reporter test failed");
if (processor_id() == 0)
{
std::vector<dof_id_type> gold;
for (dof_id_type id = 0; id < n_processors(); ++id)
gold.push_back(id);
if (_gather_value != gold)
mooseError("Gather reporter test failed!");
}
}
InputParameters
TestDeclareInitialSetupReporter::validParams()
{
InputParameters params = GeneralReporter::validParams();
params.addRequiredParam<Real>("value", "The value to report.");
return params;
}
TestDeclareInitialSetupReporter::TestDeclareInitialSetupReporter(const InputParameters & parameters)
: GeneralReporter(parameters)
{
}
void
TestDeclareInitialSetupReporter::initialSetup()
{
Real & value = declareValueByName<Real>("value");
value = getParam<Real>("value");
}
InputParameters
TestGetReporterDeclaredInInitialSetupReporter::validParams()
{
InputParameters params = GeneralReporter::validParams();
params.addRequiredParam<ReporterName>("other_reporter",
"The reporter name that was declared in initialSetup");
return params;
}
TestGetReporterDeclaredInInitialSetupReporter::TestGetReporterDeclaredInInitialSetupReporter(
const InputParameters & parameters)
: GeneralReporter(parameters),
_value_declared_in_initial_setup(getReporterValue<Real>("other_reporter")),
_the_value_of_the_reporter(declareValueByName<Real>("other_value"))
{
}
void
TestGetReporterDeclaredInInitialSetupReporter::execute()
{
_the_value_of_the_reporter = _value_declared_in_initial_setup;
}
InputParameters
TestDeclareErrorsReporter::validParams()
{
InputParameters params = GeneralReporter::validParams();
params.addRequiredParam<ReporterValueName>("value", "A reporter value name");
params.addParam<bool>("missing_param", false, "True to test the error for a missing parameter");
params.addParam<bool>("bad_param", false, "True to test the error for a bad parameter type");
params.addParam<bool>("already_declared", false, "Test declaring a value multiple times");
params.addParam<bool>("requested_different_type",
false,
"Test declaring a value that has been requested with a differentt type");
return params;
}
TestDeclareErrorsReporter::TestDeclareErrorsReporter(const InputParameters & parameters)
: GeneralReporter(parameters)
{
if (getParam<bool>("missing_param"))
declareValue<int>("some_missing_parm");
if (getParam<bool>("bad_param"))
declareValue<int>("bad_param");
if (getParam<bool>("already_declared"))
{
declareValueByName<int>("value_name");
declareValueByName<Real>("value_name");
}
if (getParam<bool>("requested_different_type"))
{
getReporterValueByName<int>(name() + "/value_name");
declareValueByName<Real>("value_name");
}
}
(moose/test/include/reporters/TestReporter.h)
// 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 "GeneralReporter.h"
class TestDeclareReporter : public GeneralReporter
{
public:
static InputParameters validParams();
TestDeclareReporter(const InputParameters & parameters);
virtual void initialize() override {}
virtual void finalize() override {}
virtual void execute() override;
protected:
int & _int; // MooseDocs:producer
Real & _real;
std::vector<Real> & _vector;
std::string & _string;
Real & _bcast_value;
std::vector<dof_id_type> _values_to_scatter;
dof_id_type & _scatter_value;
std::vector<dof_id_type> _values_to_gather;
std::vector<dof_id_type> & _gather_value;
std::vector<dof_id_type> * _distributed_vector;
};
class TestGetReporter : public GeneralReporter
{
public:
static InputParameters validParams();
TestGetReporter(const InputParameters & parameters);
virtual void initialize() override {}
virtual void finalize() override {}
virtual void execute() override;
protected:
const int & _int; // MooseDocs:consumer
const int & _int_old;
const Real & _real;
const std::vector<Real> & _vector;
const std::string & _string;
const Real & _bcast_value;
const dof_id_type & _scatter_value;
const std::vector<dof_id_type> & _gather_value;
};
class TestDeclareInitialSetupReporter : public GeneralReporter
{
public:
static InputParameters validParams();
TestDeclareInitialSetupReporter(const InputParameters & parameters);
virtual void initialSetup() override;
virtual void initialize() override {}
virtual void finalize() override {}
virtual void execute() override {}
};
class TestGetReporterDeclaredInInitialSetupReporter : public GeneralReporter
{
public:
static InputParameters validParams();
TestGetReporterDeclaredInInitialSetupReporter(const InputParameters & parameters);
virtual void initialize() override {}
virtual void finalize() override {}
virtual void execute() override;
protected:
const Real & _value_declared_in_initial_setup;
Real & _the_value_of_the_reporter;
};
class TestDeclareErrorsReporter : public GeneralReporter
{
public:
static InputParameters validParams();
TestDeclareErrorsReporter(const InputParameters & parameters);
virtual void initialize() override {}
virtual void finalize() override {}
virtual void execute() override {}
};
(moose/test/src/reporters/TestReporter.C)
// 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
#include "TestReporter.h"
registerMooseObject("MooseTestApp", TestDeclareReporter);
registerMooseObject("MooseTestApp", TestGetReporter);
registerMooseObject("MooseTestApp", TestDeclareInitialSetupReporter);
registerMooseObject("MooseTestApp", TestGetReporterDeclaredInInitialSetupReporter);
registerMooseObject("MooseTestApp", TestDeclareErrorsReporter);
InputParameters
TestDeclareReporter::validParams()
{
InputParameters params = GeneralReporter::validParams();
// MooseDocs:param_begin
params.addParam<ReporterValueName>("int_name", "int", "The name of the integer data");
// MooseDocs:param_end
params.addParam<ReporterValueName>("distributed_vector_name", "Distributed vector to produce.");
return params;
}
TestDeclareReporter::TestDeclareReporter(const InputParameters & parameters)
: GeneralReporter(parameters),
_int(declareValue<int>("int_name", 1980)), // MooseDocs:producer
_real(declareValueByName<Real>("real")),
_vector(declareValueByName<std::vector<Real>>("vector")),
_string(declareValueByName<std::string>("string")),
_bcast_value(declareValueByName<Real, ReporterBroadcastContext>("broadcast")),
_scatter_value(
declareValueByName<dof_id_type, ReporterScatterContext>("scatter", _values_to_scatter)),
_gather_value(declareValueByName<std::vector<dof_id_type>, ReporterGatherContext>(
"gather")), // MooseDocs:gather
_distributed_vector(isParamValid("distributed_vector_name")
? &declareValue<std::vector<dof_id_type>>("distributed_vector_name",
REPORTER_MODE_DISTRIBUTED)
: nullptr)
{
if (processor_id() == 0)
for (dof_id_type rank = 0; rank < n_processors(); ++rank)
_values_to_scatter.push_back(rank);
}
// MooseDocs:execute_begin
void
TestDeclareReporter::execute()
{
_int += 1;
_real = 1.2345;
_vector = {1, 1.1, 1.2};
_string = "string";
if (processor_id() == 0)
_bcast_value = 42;
_gather_value.resize(1, processor_id());
if (_distributed_vector)
{
_distributed_vector->resize(_vector.size());
(*_distributed_vector)[0] = processor_id();
for (unsigned int i = 1; i < _distributed_vector->size(); ++i)
(*_distributed_vector)[i] = (*_distributed_vector)[i - 1] * 10;
}
}
// MooseDocs:execute_end
InputParameters
TestGetReporter::validParams()
{
InputParameters params = GeneralReporter::validParams();
params.addRequiredParam<ReporterName>("int_reporter", "'int' reporter name");
params.addRequiredParam<ReporterName>("real_reporter", "'real' reporter name");
params.addRequiredParam<ReporterName>("vector_reporter", "'vector' reporter name");
params.addRequiredParam<ReporterName>("string_reporter", "'string' reporter name");
params.addRequiredParam<ReporterName>("broadcast_reporter", "'broadcast' reporter name");
params.addRequiredParam<ReporterName>("scatter_reporter", "'scatter' reporter name");
params.addRequiredParam<ReporterName>("gather_reporter", "'gather' reporter name");
return params;
}
TestGetReporter::TestGetReporter(const InputParameters & parameters)
: GeneralReporter(parameters),
_int(getReporterValue<int>("int_reporter")), // MooseDocs:consumer
_int_old(getReporterValue<int>("int_reporter", 1)),
_real(getReporterValue<Real>("real_reporter")),
_vector(getReporterValue<std::vector<Real>>("vector_reporter")),
_string(getReporterValue<std::string>("string_reporter")),
_bcast_value(getReporterValue<Real>("broadcast_reporter")),
_scatter_value(getReporterValue<dof_id_type>("scatter_reporter")),
_gather_value(getReporterValue<std::vector<dof_id_type>>("gather_reporter"))
{
}
void
TestGetReporter::execute()
{
if (_int != 1980 + _t_step)
mooseError("int reporter test failed: ", _int, " != ", 1980 + _t_step);
if (_real != 1.2345)
mooseError("Real reporter test failed");
if (_vector != std::vector<Real>({1., 1.1, 1.2}))
mooseError("std::vector<Real> reporter test failed");
if (_string != "string")
mooseError("std::string reporter test failed");
if (_t_step == 0 && _int_old != 1980)
mooseError("int_old on timestep 0 failed: ", _int_old, " != ", 1980);
if (_t_step > 0 && _int_old != 1980 + (_t_step - 1))
mooseError(
"int_old on timestep ", _t_step, " failed: ", _int_old, " != ", 1980 + (_t_step - 1));
if (_bcast_value != 42)
mooseError("Broadcast reporter test failed");
if (_scatter_value != processor_id())
mooseError("Scatter reporter test failed");
if (processor_id() == 0)
{
std::vector<dof_id_type> gold;
for (dof_id_type id = 0; id < n_processors(); ++id)
gold.push_back(id);
if (_gather_value != gold)
mooseError("Gather reporter test failed!");
}
}
InputParameters
TestDeclareInitialSetupReporter::validParams()
{
InputParameters params = GeneralReporter::validParams();
params.addRequiredParam<Real>("value", "The value to report.");
return params;
}
TestDeclareInitialSetupReporter::TestDeclareInitialSetupReporter(const InputParameters & parameters)
: GeneralReporter(parameters)
{
}
void
TestDeclareInitialSetupReporter::initialSetup()
{
Real & value = declareValueByName<Real>("value");
value = getParam<Real>("value");
}
InputParameters
TestGetReporterDeclaredInInitialSetupReporter::validParams()
{
InputParameters params = GeneralReporter::validParams();
params.addRequiredParam<ReporterName>("other_reporter",
"The reporter name that was declared in initialSetup");
return params;
}
TestGetReporterDeclaredInInitialSetupReporter::TestGetReporterDeclaredInInitialSetupReporter(
const InputParameters & parameters)
: GeneralReporter(parameters),
_value_declared_in_initial_setup(getReporterValue<Real>("other_reporter")),
_the_value_of_the_reporter(declareValueByName<Real>("other_value"))
{
}
void
TestGetReporterDeclaredInInitialSetupReporter::execute()
{
_the_value_of_the_reporter = _value_declared_in_initial_setup;
}
InputParameters
TestDeclareErrorsReporter::validParams()
{
InputParameters params = GeneralReporter::validParams();
params.addRequiredParam<ReporterValueName>("value", "A reporter value name");
params.addParam<bool>("missing_param", false, "True to test the error for a missing parameter");
params.addParam<bool>("bad_param", false, "True to test the error for a bad parameter type");
params.addParam<bool>("already_declared", false, "Test declaring a value multiple times");
params.addParam<bool>("requested_different_type",
false,
"Test declaring a value that has been requested with a differentt type");
return params;
}
TestDeclareErrorsReporter::TestDeclareErrorsReporter(const InputParameters & parameters)
: GeneralReporter(parameters)
{
if (getParam<bool>("missing_param"))
declareValue<int>("some_missing_parm");
if (getParam<bool>("bad_param"))
declareValue<int>("bad_param");
if (getParam<bool>("already_declared"))
{
declareValueByName<int>("value_name");
declareValueByName<Real>("value_name");
}
if (getParam<bool>("requested_different_type"))
{
getReporterValueByName<int>(name() + "/value_name");
declareValueByName<Real>("value_name");
}
}
(moose/test/src/reporters/TestReporter.C)
// 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
#include "TestReporter.h"
registerMooseObject("MooseTestApp", TestDeclareReporter);
registerMooseObject("MooseTestApp", TestGetReporter);
registerMooseObject("MooseTestApp", TestDeclareInitialSetupReporter);
registerMooseObject("MooseTestApp", TestGetReporterDeclaredInInitialSetupReporter);
registerMooseObject("MooseTestApp", TestDeclareErrorsReporter);
InputParameters
TestDeclareReporter::validParams()
{
InputParameters params = GeneralReporter::validParams();
// MooseDocs:param_begin
params.addParam<ReporterValueName>("int_name", "int", "The name of the integer data");
// MooseDocs:param_end
params.addParam<ReporterValueName>("distributed_vector_name", "Distributed vector to produce.");
return params;
}
TestDeclareReporter::TestDeclareReporter(const InputParameters & parameters)
: GeneralReporter(parameters),
_int(declareValue<int>("int_name", 1980)), // MooseDocs:producer
_real(declareValueByName<Real>("real")),
_vector(declareValueByName<std::vector<Real>>("vector")),
_string(declareValueByName<std::string>("string")),
_bcast_value(declareValueByName<Real, ReporterBroadcastContext>("broadcast")),
_scatter_value(
declareValueByName<dof_id_type, ReporterScatterContext>("scatter", _values_to_scatter)),
_gather_value(declareValueByName<std::vector<dof_id_type>, ReporterGatherContext>(
"gather")), // MooseDocs:gather
_distributed_vector(isParamValid("distributed_vector_name")
? &declareValue<std::vector<dof_id_type>>("distributed_vector_name",
REPORTER_MODE_DISTRIBUTED)
: nullptr)
{
if (processor_id() == 0)
for (dof_id_type rank = 0; rank < n_processors(); ++rank)
_values_to_scatter.push_back(rank);
}
// MooseDocs:execute_begin
void
TestDeclareReporter::execute()
{
_int += 1;
_real = 1.2345;
_vector = {1, 1.1, 1.2};
_string = "string";
if (processor_id() == 0)
_bcast_value = 42;
_gather_value.resize(1, processor_id());
if (_distributed_vector)
{
_distributed_vector->resize(_vector.size());
(*_distributed_vector)[0] = processor_id();
for (unsigned int i = 1; i < _distributed_vector->size(); ++i)
(*_distributed_vector)[i] = (*_distributed_vector)[i - 1] * 10;
}
}
// MooseDocs:execute_end
InputParameters
TestGetReporter::validParams()
{
InputParameters params = GeneralReporter::validParams();
params.addRequiredParam<ReporterName>("int_reporter", "'int' reporter name");
params.addRequiredParam<ReporterName>("real_reporter", "'real' reporter name");
params.addRequiredParam<ReporterName>("vector_reporter", "'vector' reporter name");
params.addRequiredParam<ReporterName>("string_reporter", "'string' reporter name");
params.addRequiredParam<ReporterName>("broadcast_reporter", "'broadcast' reporter name");
params.addRequiredParam<ReporterName>("scatter_reporter", "'scatter' reporter name");
params.addRequiredParam<ReporterName>("gather_reporter", "'gather' reporter name");
return params;
}
TestGetReporter::TestGetReporter(const InputParameters & parameters)
: GeneralReporter(parameters),
_int(getReporterValue<int>("int_reporter")), // MooseDocs:consumer
_int_old(getReporterValue<int>("int_reporter", 1)),
_real(getReporterValue<Real>("real_reporter")),
_vector(getReporterValue<std::vector<Real>>("vector_reporter")),
_string(getReporterValue<std::string>("string_reporter")),
_bcast_value(getReporterValue<Real>("broadcast_reporter")),
_scatter_value(getReporterValue<dof_id_type>("scatter_reporter")),
_gather_value(getReporterValue<std::vector<dof_id_type>>("gather_reporter"))
{
}
void
TestGetReporter::execute()
{
if (_int != 1980 + _t_step)
mooseError("int reporter test failed: ", _int, " != ", 1980 + _t_step);
if (_real != 1.2345)
mooseError("Real reporter test failed");
if (_vector != std::vector<Real>({1., 1.1, 1.2}))
mooseError("std::vector<Real> reporter test failed");
if (_string != "string")
mooseError("std::string reporter test failed");
if (_t_step == 0 && _int_old != 1980)
mooseError("int_old on timestep 0 failed: ", _int_old, " != ", 1980);
if (_t_step > 0 && _int_old != 1980 + (_t_step - 1))
mooseError(
"int_old on timestep ", _t_step, " failed: ", _int_old, " != ", 1980 + (_t_step - 1));
if (_bcast_value != 42)
mooseError("Broadcast reporter test failed");
if (_scatter_value != processor_id())
mooseError("Scatter reporter test failed");
if (processor_id() == 0)
{
std::vector<dof_id_type> gold;
for (dof_id_type id = 0; id < n_processors(); ++id)
gold.push_back(id);
if (_gather_value != gold)
mooseError("Gather reporter test failed!");
}
}
InputParameters
TestDeclareInitialSetupReporter::validParams()
{
InputParameters params = GeneralReporter::validParams();
params.addRequiredParam<Real>("value", "The value to report.");
return params;
}
TestDeclareInitialSetupReporter::TestDeclareInitialSetupReporter(const InputParameters & parameters)
: GeneralReporter(parameters)
{
}
void
TestDeclareInitialSetupReporter::initialSetup()
{
Real & value = declareValueByName<Real>("value");
value = getParam<Real>("value");
}
InputParameters
TestGetReporterDeclaredInInitialSetupReporter::validParams()
{
InputParameters params = GeneralReporter::validParams();
params.addRequiredParam<ReporterName>("other_reporter",
"The reporter name that was declared in initialSetup");
return params;
}
TestGetReporterDeclaredInInitialSetupReporter::TestGetReporterDeclaredInInitialSetupReporter(
const InputParameters & parameters)
: GeneralReporter(parameters),
_value_declared_in_initial_setup(getReporterValue<Real>("other_reporter")),
_the_value_of_the_reporter(declareValueByName<Real>("other_value"))
{
}
void
TestGetReporterDeclaredInInitialSetupReporter::execute()
{
_the_value_of_the_reporter = _value_declared_in_initial_setup;
}
InputParameters
TestDeclareErrorsReporter::validParams()
{
InputParameters params = GeneralReporter::validParams();
params.addRequiredParam<ReporterValueName>("value", "A reporter value name");
params.addParam<bool>("missing_param", false, "True to test the error for a missing parameter");
params.addParam<bool>("bad_param", false, "True to test the error for a bad parameter type");
params.addParam<bool>("already_declared", false, "Test declaring a value multiple times");
params.addParam<bool>("requested_different_type",
false,
"Test declaring a value that has been requested with a differentt type");
return params;
}
TestDeclareErrorsReporter::TestDeclareErrorsReporter(const InputParameters & parameters)
: GeneralReporter(parameters)
{
if (getParam<bool>("missing_param"))
declareValue<int>("some_missing_parm");
if (getParam<bool>("bad_param"))
declareValue<int>("bad_param");
if (getParam<bool>("already_declared"))
{
declareValueByName<int>("value_name");
declareValueByName<Real>("value_name");
}
if (getParam<bool>("requested_different_type"))
{
getReporterValueByName<int>(name() + "/value_name");
declareValueByName<Real>("value_name");
}
}
(moose/test/tests/reporters/base/base.i)
[Mesh]
type = GeneratedMesh
dim = 1
[]
[Variables/u]
[]
[Problem]
solve = false
kernel_coverage_check = false
[]
[Reporters]
[b]
type = TestGetReporter
int_reporter = a/int
real_reporter = a/real
vector_reporter = a/vector
string_reporter = a/string
broadcast_reporter = a/broadcast
scatter_reporter = a/scatter
gather_reporter = a/gather
[]
[a]
type = TestDeclareReporter
[]
[]
[Executioner]
type = Transient
num_steps = 3
[]
(moose/test/src/reporters/TestReporter.C)
// 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
#include "TestReporter.h"
registerMooseObject("MooseTestApp", TestDeclareReporter);
registerMooseObject("MooseTestApp", TestGetReporter);
registerMooseObject("MooseTestApp", TestDeclareInitialSetupReporter);
registerMooseObject("MooseTestApp", TestGetReporterDeclaredInInitialSetupReporter);
registerMooseObject("MooseTestApp", TestDeclareErrorsReporter);
InputParameters
TestDeclareReporter::validParams()
{
InputParameters params = GeneralReporter::validParams();
// MooseDocs:param_begin
params.addParam<ReporterValueName>("int_name", "int", "The name of the integer data");
// MooseDocs:param_end
params.addParam<ReporterValueName>("distributed_vector_name", "Distributed vector to produce.");
return params;
}
TestDeclareReporter::TestDeclareReporter(const InputParameters & parameters)
: GeneralReporter(parameters),
_int(declareValue<int>("int_name", 1980)), // MooseDocs:producer
_real(declareValueByName<Real>("real")),
_vector(declareValueByName<std::vector<Real>>("vector")),
_string(declareValueByName<std::string>("string")),
_bcast_value(declareValueByName<Real, ReporterBroadcastContext>("broadcast")),
_scatter_value(
declareValueByName<dof_id_type, ReporterScatterContext>("scatter", _values_to_scatter)),
_gather_value(declareValueByName<std::vector<dof_id_type>, ReporterGatherContext>(
"gather")), // MooseDocs:gather
_distributed_vector(isParamValid("distributed_vector_name")
? &declareValue<std::vector<dof_id_type>>("distributed_vector_name",
REPORTER_MODE_DISTRIBUTED)
: nullptr)
{
if (processor_id() == 0)
for (dof_id_type rank = 0; rank < n_processors(); ++rank)
_values_to_scatter.push_back(rank);
}
// MooseDocs:execute_begin
void
TestDeclareReporter::execute()
{
_int += 1;
_real = 1.2345;
_vector = {1, 1.1, 1.2};
_string = "string";
if (processor_id() == 0)
_bcast_value = 42;
_gather_value.resize(1, processor_id());
if (_distributed_vector)
{
_distributed_vector->resize(_vector.size());
(*_distributed_vector)[0] = processor_id();
for (unsigned int i = 1; i < _distributed_vector->size(); ++i)
(*_distributed_vector)[i] = (*_distributed_vector)[i - 1] * 10;
}
}
// MooseDocs:execute_end
InputParameters
TestGetReporter::validParams()
{
InputParameters params = GeneralReporter::validParams();
params.addRequiredParam<ReporterName>("int_reporter", "'int' reporter name");
params.addRequiredParam<ReporterName>("real_reporter", "'real' reporter name");
params.addRequiredParam<ReporterName>("vector_reporter", "'vector' reporter name");
params.addRequiredParam<ReporterName>("string_reporter", "'string' reporter name");
params.addRequiredParam<ReporterName>("broadcast_reporter", "'broadcast' reporter name");
params.addRequiredParam<ReporterName>("scatter_reporter", "'scatter' reporter name");
params.addRequiredParam<ReporterName>("gather_reporter", "'gather' reporter name");
return params;
}
TestGetReporter::TestGetReporter(const InputParameters & parameters)
: GeneralReporter(parameters),
_int(getReporterValue<int>("int_reporter")), // MooseDocs:consumer
_int_old(getReporterValue<int>("int_reporter", 1)),
_real(getReporterValue<Real>("real_reporter")),
_vector(getReporterValue<std::vector<Real>>("vector_reporter")),
_string(getReporterValue<std::string>("string_reporter")),
_bcast_value(getReporterValue<Real>("broadcast_reporter")),
_scatter_value(getReporterValue<dof_id_type>("scatter_reporter")),
_gather_value(getReporterValue<std::vector<dof_id_type>>("gather_reporter"))
{
}
void
TestGetReporter::execute()
{
if (_int != 1980 + _t_step)
mooseError("int reporter test failed: ", _int, " != ", 1980 + _t_step);
if (_real != 1.2345)
mooseError("Real reporter test failed");
if (_vector != std::vector<Real>({1., 1.1, 1.2}))
mooseError("std::vector<Real> reporter test failed");
if (_string != "string")
mooseError("std::string reporter test failed");
if (_t_step == 0 && _int_old != 1980)
mooseError("int_old on timestep 0 failed: ", _int_old, " != ", 1980);
if (_t_step > 0 && _int_old != 1980 + (_t_step - 1))
mooseError(
"int_old on timestep ", _t_step, " failed: ", _int_old, " != ", 1980 + (_t_step - 1));
if (_bcast_value != 42)
mooseError("Broadcast reporter test failed");
if (_scatter_value != processor_id())
mooseError("Scatter reporter test failed");
if (processor_id() == 0)
{
std::vector<dof_id_type> gold;
for (dof_id_type id = 0; id < n_processors(); ++id)
gold.push_back(id);
if (_gather_value != gold)
mooseError("Gather reporter test failed!");
}
}
InputParameters
TestDeclareInitialSetupReporter::validParams()
{
InputParameters params = GeneralReporter::validParams();
params.addRequiredParam<Real>("value", "The value to report.");
return params;
}
TestDeclareInitialSetupReporter::TestDeclareInitialSetupReporter(const InputParameters & parameters)
: GeneralReporter(parameters)
{
}
void
TestDeclareInitialSetupReporter::initialSetup()
{
Real & value = declareValueByName<Real>("value");
value = getParam<Real>("value");
}
InputParameters
TestGetReporterDeclaredInInitialSetupReporter::validParams()
{
InputParameters params = GeneralReporter::validParams();
params.addRequiredParam<ReporterName>("other_reporter",
"The reporter name that was declared in initialSetup");
return params;
}
TestGetReporterDeclaredInInitialSetupReporter::TestGetReporterDeclaredInInitialSetupReporter(
const InputParameters & parameters)
: GeneralReporter(parameters),
_value_declared_in_initial_setup(getReporterValue<Real>("other_reporter")),
_the_value_of_the_reporter(declareValueByName<Real>("other_value"))
{
}
void
TestGetReporterDeclaredInInitialSetupReporter::execute()
{
_the_value_of_the_reporter = _value_declared_in_initial_setup;
}
InputParameters
TestDeclareErrorsReporter::validParams()
{
InputParameters params = GeneralReporter::validParams();
params.addRequiredParam<ReporterValueName>("value", "A reporter value name");
params.addParam<bool>("missing_param", false, "True to test the error for a missing parameter");
params.addParam<bool>("bad_param", false, "True to test the error for a bad parameter type");
params.addParam<bool>("already_declared", false, "Test declaring a value multiple times");
params.addParam<bool>("requested_different_type",
false,
"Test declaring a value that has been requested with a differentt type");
return params;
}
TestDeclareErrorsReporter::TestDeclareErrorsReporter(const InputParameters & parameters)
: GeneralReporter(parameters)
{
if (getParam<bool>("missing_param"))
declareValue<int>("some_missing_parm");
if (getParam<bool>("bad_param"))
declareValue<int>("bad_param");
if (getParam<bool>("already_declared"))
{
declareValueByName<int>("value_name");
declareValueByName<Real>("value_name");
}
if (getParam<bool>("requested_different_type"))
{
getReporterValueByName<int>(name() + "/value_name");
declareValueByName<Real>("value_name");
}
}