Skip to content

Commit

Permalink
Add elementreporters (idaholab#24678)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxnezdyur committed Jun 14, 2023
1 parent 968eb46 commit 7428955
Show file tree
Hide file tree
Showing 10 changed files with 408 additions and 0 deletions.
@@ -0,0 +1,23 @@
# CoupledVarStatsElementReporter

!syntax description /Reporters/CoupledVarStatsElementReporter

## Overview

CoupledVarStatsElementReporter produces the following statistics for a
variable: Maximum, Minium, Average, Integral, Total Elements. The
[!param](/Reporters/StatsElementReporter/base_name) can be used to prepend a
name to each reporter.



## Example Input File Syntax

!listing element_reporters/coupledvarstatsreporter/coupledvarstats.i block=elem_stats
indent=2 header=[Reporters] footer=[]

!syntax parameters /Reporters/CoupledVarStatsElementReporter

!syntax inputs /Reporters/CoupledVarStatsElementReporter

!syntax children /Reporters/CoupledVarStatsElementReporter
25 changes: 25 additions & 0 deletions framework/include/reporters/CoupledVarStatsElementReporter.h
@@ -0,0 +1,25 @@
//* 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 "ElementStatsReporter.h"

class CoupledVarStatsElementReporter : public ElementStatsReporter
{
public:
static InputParameters validParams();

CoupledVarStatsElementReporter(const InputParameters & parameters);

private:
/// The coupled variable used.
const VariableValue & _v;
virtual Real computeValue();
};
43 changes: 43 additions & 0 deletions framework/include/reporters/ElementReporter.h
@@ -0,0 +1,43 @@
//* 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

// MOOSE includes
#include "ElementUserObject.h"
#include "Reporter.h"

/**
*/
class ElementReporter : public ElementUserObject, public Reporter
{
public:
static InputParameters validParams();

ElementReporter(const InputParameters & parameters);

/**
* @returns Whether or not this Reporter should store its value at this specific time.
*
* If the private parameter '_always_store' is true, this will always return true.
* Otherwise, it will return true if the current execute flag matches a flag
* that this ElementReporter has in its 'execute_on' parameter. Otherwise, it will
* return false.
*
* This enables ElementReporter objects that do not fill information ahead of time in
* execute() but instead fill their information in the to_json implementation.
* Without this, said ElementReporters would always output their information even though
* the user requested that they do not execute on a specific flag.
*/
bool shouldStore() const override final;

private:
/// Whether or not this ElementReporter should always store its information; see shouldStore()
const bool _always_store;
};
36 changes: 36 additions & 0 deletions framework/include/reporters/ElementStatsReporter.h
@@ -0,0 +1,36 @@
//* 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 "ElementReporter.h"

class ElementStatsReporter : public ElementReporter
{
public:
static InputParameters validParams();

ElementStatsReporter(const InputParameters & parameters);

protected:
virtual void initialize() override;
virtual void execute() override;
virtual void finalize() override;
virtual void threadJoin(const UserObject & uo) override;

virtual Real computeValue() = 0;

private:
const std::string _base_name;
Real & _max;
Real & _min;
Real & _average;
Real & _integral;
int & _number_elements;
};
40 changes: 40 additions & 0 deletions framework/src/reporters/CoupledVarStatsElementReporter.C
@@ -0,0 +1,40 @@
//* 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 "CoupledVarStatsElementReporter.h"

registerMooseObject("MooseApp", CoupledVarStatsElementReporter);

InputParameters
CoupledVarStatsElementReporter::validParams()
{
InputParameters params = ElementStatsReporter::validParams();

params.addRequiredCoupledVar("coupled_var", "Coupled variable whose value is used.");

params.addClassDescription("Element reporter to get statistics for a coupled variable. This can "
"be transfered to other apps.");
return params;
}

CoupledVarStatsElementReporter::CoupledVarStatsElementReporter(const InputParameters & parameters)
: ElementStatsReporter(parameters), _v(coupledValue("coupled_var"))
{
}
Real
CoupledVarStatsElementReporter::computeValue()
{
Real avg_val = 0;

for (unsigned int qp = 0; qp < _qrule->n_points(); ++qp)
avg_val += _v[qp] * _JxW[qp] * _coord[qp];
avg_val /= _current_elem_volume;

return avg_val;
}
36 changes: 36 additions & 0 deletions framework/src/reporters/ElementReporter.C
@@ -0,0 +1,36 @@
//* 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

// MOOSE includes
#include "ElementReporter.h"

InputParameters
ElementReporter::validParams()
{
InputParameters params = ElementUserObject::validParams();
params += Reporter::validParams();
// Whether or not to always store this object's value
// See the override for shouldStore() for more information
params.addPrivateParam<bool>("_always_store", true);

return params;
}

ElementReporter::ElementReporter(const InputParameters & parameters)
: ElementUserObject(parameters), Reporter(this), _always_store(getParam<bool>("_always_store"))
{
}

bool
ElementReporter::shouldStore() const
{
// Either we always store, or we store if the current execution flag matches
// a flag that is within this ElementReporter's 'execute_on'
return _always_store || getExecuteOnEnum().contains(_fe_problem.getCurrentExecuteOnFlag());
}
89 changes: 89 additions & 0 deletions framework/src/reporters/ElementStatsReporter.C
@@ -0,0 +1,89 @@
//* 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 "ElementStatsReporter.h"

#include "ElementReporter.h"
#include "libmesh/enum_eigen_solver_type.h"
#include <slepceps.h>
#include <string>

InputParameters
ElementStatsReporter::validParams()
{
InputParameters params = ElementReporter::validParams();
params.addParam<std::string>("base_name", "Name to append to reporters.");
return params;
}

ElementStatsReporter::ElementStatsReporter(const InputParameters & parameters)
: ElementReporter(parameters),
_base_name(isParamValid("base_name") ? getParam<std::string>("base_name") + "_" : ""),
_max(declareValueByName<Real>(_base_name + "max")),
_min(declareValueByName<Real>(_base_name + "min")),
_average(declareValueByName<Real>(_base_name + "average")),
_integral(declareValueByName<Real>(_base_name + "integral")),
_number_elements(declareValueByName<int>(_base_name + "number_elements"))
{
}
void
ElementStatsReporter::initialize()
{
_max = std::numeric_limits<Real>::min();
_min = std::numeric_limits<Real>::max();
_average = 0;
_integral = 0;
_number_elements = 0;
}

void
ElementStatsReporter::execute()
{
// Get value to to update statistics
Real value = computeValue();

if (_max < value)
_max = value;

if (_min > value)
_min = value;

_integral += value * _current_elem_volume;

// Update the total and the number to get the average finalize
_average += value;
_number_elements++;
}
void
ElementStatsReporter::threadJoin(const UserObject & uo)
{
const ElementStatsReporter & es = static_cast<const ElementStatsReporter &>(uo);
if (_max < es._max)
_max = es._max;

if (_min > es._min)
_min = es._min;

_integral += es._integral;

_average += es._average;
_number_elements += es._number_elements;
}
void
ElementStatsReporter::finalize()
{
_communicator.max(_max);
_communicator.min(_min);
_communicator.sum(_integral);
_communicator.sum(_average);
_communicator.sum(_number_elements);

// Compute the average;
_average /= _number_elements;
}
@@ -0,0 +1,57 @@
[Mesh]
type = GeneratedMesh
dim = 2
xmax = 2
ymax = 2
nx = 10
ny = 10
[]

[Variables]
[u]
[]
[]

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

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

[Reporters]
[elem_stats]
type = CoupledVarStatsElementReporter
coupled_var = u
base_name = diffusion
[]
[]

[Executioner]
type = Steady
solve_type = Newton
petsc_options_iname = '-pc_type'
petsc_options_value = 'lu'
[]

[Outputs]
[stats]
type = JSON
execute_system_information_on = none
[]
[]

0 comments on commit 7428955

Please sign in to comment.