https://mooseframework.inl.gov
Loading...
Searching...
No Matches
WeightedAverageMaterial.C
Go to the documentation of this file.
1//* This file is part of the MOOSE framework
2//* https://mooseframework.inl.gov
3//*
4//* All rights reserved, see COPYRIGHT for full restrictions
5//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6//*
7//* Licensed under LGPL 2.1, please see LICENSE for details
8//* https://www.gnu.org/licenses/lgpl-2.1.html
9
11
13
16{
18
19 params.addClassDescription("Weighted average of material properties using variables as weights");
20
21 params.addRequiredParam<MaterialPropertyName>(
22 "prop_name", "The name of the material property where the average is stored");
23 params.addRequiredParam<std::vector<MaterialPropertyName>>("values",
24 "Vector of values to average");
25 params.addRequiredCoupledVar("weights", "Vector of weights for each value");
26
27 return params;
28}
29
31 : Material(parameters),
32 _prop(declareProperty<Real>(getParam<MaterialPropertyName>("prop_name"))),
33 _n_values(getParam<std::vector<MaterialPropertyName>>("values").size())
34{
35 // make sure that number of weights equals the number of values
36 if (coupledComponents("weights") != _n_values)
37 mooseError(name(), ": The number of weights must equal the number of values");
38
39 // get all of the variable values
40 const std::vector<MaterialPropertyName> & prop_names =
41 getParam<std::vector<MaterialPropertyName>>("values");
42 for (unsigned int i = 0; i < _n_values; i++)
43 {
44 _values.push_back(&getMaterialPropertyByName<Real>(prop_names[i]));
45 _weights.push_back(&coupledValue("weights", i));
46 }
47}
48
49void
51{
52 Real weight_total = 0;
53 for (unsigned int i = 0; i < _n_values; i++)
54 weight_total += (*(_weights[i]))[_qp];
55
56 Real weighted_sum = 0;
57 for (unsigned int i = 0; i < _n_values; i++)
58 weighted_sum += (*(_weights[i]))[_qp] * (*(_values[i]))[_qp];
59
60 _prop[_qp] = weighted_sum / weight_total;
61}
registerMooseObject("ThermalHydraulicsApp", WeightedAverageMaterial)
virtual const VariableValue & coupledValue(const std::string &var_name, unsigned int comp=0) const
unsigned int coupledComponents(const std::string &var_name) const
void addRequiredCoupledVar(const std::string &name, const std::string &doc_string)
void addRequiredParam(const std::string &name, const std::string &doc_string)
void addClassDescription(const std::string &doc_string)
unsigned int _qp
static InputParameters validParams()
const std::string & name() const
void mooseError(Args &&... args) const
Weighted average of material properties using aux variables as the weights.
const unsigned int _n_values
Number of components (i.e. values and weights)
WeightedAverageMaterial(const InputParameters &parameters)
MaterialProperty< Real > & _prop
The material property where the weighted average is stored.
static InputParameters validParams()
std::vector< const MaterialProperty< Real > * > _values
Values to average.
std::vector< const VariableValue * > _weights
Weights of the values.