Skip to content

Commit

Permalink
Add MessageFromInput UserObject, test and doco
Browse files Browse the repository at this point in the history
  • Loading branch information
MengnanLi91 committed Mar 15, 2023
1 parent fa6b53a commit 0cdab3d
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 1 deletion.
17 changes: 17 additions & 0 deletions framework/doc/content/source/userobjects/MessageFromInput.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# MessageFromInput

!syntax description /UserObjects/MessageFromInput

## Description

This user object provides an option to print a message to the screen during the simulation. The message can be written in the input file. The user can customize *when* the message should print, the default option is to print it at the initialization of the simulation, with `execute_on = INITIAL`.

## Example Input Syntax

!listing test/tests/userobjects/message_from_input/message_from_input.i block=UserObjects

!syntax parameters /UserObjects/MessageFromInput

!syntax inputs /UserObjects/MessageFromInput

!syntax children /UserObjects/MessageFromInput
38 changes: 38 additions & 0 deletions framework/include/userobjects/MessageFromInput.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//* 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 "GeneralUserObject.h"

/**
* User object that get a message from an input file and
* print it out during the simulation
*/
class MessageFromInput : public GeneralUserObject
{
public:
static InputParameters validParams();

MessageFromInput(const InputParameters & parameters);
virtual ~MessageFromInput();

// Required pure virtual function(not used)
virtual void initialize() override {};

// Required pure virtual function(not used)
virtual void finalize() override {};

// Print out the message with specified color
virtual void execute() override;

private:
// hold the message from the input file
const std::string _input_message;
};
53 changes: 53 additions & 0 deletions framework/src/userobjects/MessageFromInput.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//* 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 "MessageFromInput.h"
#include "Console.h"
#include "ConsoleUtils.h"

registerMooseObject("MooseApp", MessageFromInput);

InputParameters
MessageFromInput::validParams()
{
// Get the input parameters from the parent class
InputParameters params = GeneralUserObject::validParams();

// Add parameters
params.addRequiredParam<std::string>("message", "The message to print out");

// we run this object once at the initialization by default
params.set<ExecFlagEnum>("execute_on") = EXEC_INITIAL;

params.addClassDescription("Print out a message from the input file");

return params;
}

MessageFromInput::MessageFromInput(const InputParameters & parameters)
: GeneralUserObject(parameters), _input_message(getParam<std::string>("message"))
{
}

MessageFromInput::~MessageFromInput() {}

void
MessageFromInput::execute()
{
// Only print out once
if (processor_id() == 0)
{
auto total_width = std::setw(ConsoleUtils::console_field_width);
_console << total_width << "\n";
_console << "Message: " << std::endl
<< ConsoleUtils::indent(2) << COLOR_YELLOW << _input_message << "\n";
_console << COLOR_DEFAULT << total_width << "\n" << std::endl;
_console << std ::flush;
}
}
1 change: 0 additions & 1 deletion modules/chemical_reactions/contrib/thermochimica
Submodule thermochimica deleted from cabaad
59 changes: 59 additions & 0 deletions test/tests/userobjects/message_from_input/message_from_input.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
[Mesh]
type = GeneratedMesh
dim = 2
nx = 2
ny = 2
xmax = 1
ymax = 1
[]

[Variables]
[u]
[]
[]


[BCs]
[left]
type = DirichletBC
variable = u
boundary = left
value = 1
[]
[right]
type = DirichletBC
variable = u
boundary = right
value = 0
[]
[]

[Kernels]
[diff]
type = Diffusion
variable = u
[]
[]

[Problem]
type = FEProblem
[]

[UserObjects]
[message_out]
type = MessageFromInput
execute_on = timestep_end
[]
[]

[Executioner]
type = Steady
solve_type = 'NEWTON'
petsc_options_iname = '-pc_type -pc_factor_mat_solver_type'
petsc_options_value = 'lu superlu_dist'
[]

[Outputs]
exodus = true
[]

16 changes: 16 additions & 0 deletions test/tests/userobjects/message_from_input/tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[Tests]
[test]
requirement = 'The system shall print out a message to screen from the input file'
design = 'MessageFromInput.md'
issues = '#21736'

[message_from_input]
type = 'RunApp'
input = 'message_from_input.i'
cli_args = "UserObjects/message_out/message='Test Message'"
expect_out = 'Test Message'

detail = "print out the user-defined message"
[]
[]
[]

0 comments on commit 0cdab3d

Please sign in to comment.