Skip to content

Commit 904b326

Browse files
authored
Merge pull request idaholab#32826 from NamjaeChoi/kokkos_parsed
Kokkos Parsed Objects
2 parents 0bef659 + 4f3452a commit 904b326

31 files changed

Lines changed: 1303 additions & 54 deletions
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# KokkosParsedAux
2+
3+
!if! function=hasCapability('kokkos')
4+
5+
This is the Kokkos version of [ParsedAux](ParsedAux.md). See the original document for details.
6+
7+
!alert note
8+
The Kokkos version does not support functors yet. Instead, postprocessors, variables, material properties, and functions can be specified for the use in the parsed expression through separate parameters.
9+
10+
## Example Input Syntax
11+
12+
!listing test/tests/kokkos/auxkernels/parsed/kokkos_parsed_aux_test.i start=[set_parsed] end=[] include-end=true
13+
14+
!syntax parameters /AuxKernels/KokkosParsedAux
15+
16+
!syntax inputs /AuxKernels/KokkosParsedAux
17+
18+
!syntax children /AuxKernels/KokkosParsedAux
19+
20+
!if-end!
21+
22+
!else
23+
!include kokkos/kokkos_warning.md
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# KokkosMatNeumannBC
2+
3+
!if! function=hasCapability('kokkos')
4+
5+
This is the Kokkos version of [MatNeumannBC](MatNeumannBC.md). See the original document for details.
6+
7+
## Example Input Syntax
8+
9+
!listing test/tests/kokkos/materials/parsed/kokkos_parsed_material.i start=[top] end=[] include-end=true
10+
11+
!syntax parameters /BCs/KokkosMatNeumannBC
12+
13+
!syntax inputs /BCs/KokkosMatNeumannBC
14+
15+
!syntax children /BCs/KokkosMatNeumannBC
16+
17+
!if-end!
18+
19+
!else
20+
!include kokkos/kokkos_warning.md

framework/doc/content/source/kokkos/functions/KokkosParsedFunction.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
This is the Kokkos version of [ParsedFunction](MooseParsedFunction.md). See the original document for details.
66

77
!alert note
8-
The Kokkos version does not support using scalar variables or other functions for [!param](/Functions/KokkosParsedFunction/symbol_values) yet.
8+
The Kokkos version does not support using scalar variables for [!param](/Functions/KokkosParsedFunction/symbol_values) yet.
99

1010
## Example Syntax
1111

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# KokkosParsedMaterial
2+
3+
!if! function=hasCapability('kokkos')
4+
5+
This is the Kokkos version of [ParsedMaterial](ParsedMaterial.md). See the original document for details.
6+
7+
!alert note
8+
The Kokkos version does not support functors yet. Instead, postprocessors, variables, material properties, and functions can be specified for the use in the parsed expression through separate parameters.
9+
10+
## Example Input Syntax
11+
12+
!listing test/tests/kokkos/materials/parsed/kokkos_parsed_material.i start=[mat] end=[] include-end=true
13+
14+
!syntax parameters /Materials/KokkosParsedMaterial
15+
16+
!syntax inputs /Materials/KokkosParsedMaterial
17+
18+
!syntax children /Materials/KokkosParsedMaterial
19+
20+
!if-end!
21+
22+
!else
23+
!include kokkos/kokkos_warning.md
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
10+
#pragma once
11+
12+
#include "KokkosAuxKernel.h"
13+
#include "KokkosParsedObjectBase.h"
14+
15+
/**
16+
* AuxKernel that evaluates a parsed function expression
17+
*/
18+
class KokkosParsedAux : public Moose::Kokkos::AuxKernel, public Moose::Kokkos::ParsedObjectBase
19+
{
20+
public:
21+
static InputParameters validParams();
22+
23+
KokkosParsedAux(const InputParameters & parameters);
24+
25+
template <typename Derived>
26+
KOKKOS_FUNCTION Real computeValue(const unsigned int qp, Datum & datum) const;
27+
};
28+
29+
template <typename Derived>
30+
KOKKOS_FUNCTION Real
31+
KokkosParsedAux::computeValue(const unsigned int qp, Datum & datum) const
32+
{
33+
return _evaluator.eval(_t, datum.q_point(qp), qp, &datum);
34+
}
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
//* This file is part of the MOOSE framework
2+
//* https://www.mooseframework.org
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+
10+
#pragma once
11+
12+
#include "KokkosFunctionParser.h"
13+
14+
#include "MooseObject.h"
15+
16+
namespace Moose::Kokkos
17+
{
18+
19+
class ParsedObjectBase
20+
{
21+
public:
22+
static InputParameters validParams();
23+
24+
ParsedObjectBase(const MooseObject * object);
25+
26+
protected:
27+
/**
28+
* Check if duplicate symbols were added
29+
* @param symbols The list of symbols
30+
* @param param The parameter name containing symbols
31+
*/
32+
template <typename T>
33+
void checkDuplicateSymbols(const std::vector<T> & symbols, const std::string & param);
34+
35+
/**
36+
* Add a constant
37+
* @param name The variable name
38+
* @param constant The constant value
39+
*/
40+
void addConstant(const std::string & name, const Real constant);
41+
/**
42+
* Add a scalar variable
43+
* @param name The variable name
44+
* @param scalar The pointer to the scalar variable
45+
*/
46+
void addScalar(const std::string & name, const Real & scalar);
47+
/**
48+
* Add a field variable
49+
* @param name The variable name
50+
* @param field The coupled field variable
51+
*/
52+
void addField(const std::string & name, const VariableValue & field);
53+
/**
54+
* Add a material property
55+
* @param name The variable name
56+
* @param property The material property
57+
*/
58+
void addProperty(const std::string & name, const MaterialProperty<Real> & property);
59+
/**
60+
* Add a function
61+
* @param name The variable name
62+
* @param function The function
63+
*/
64+
void addFunction(const std::string & name, const Function & function);
65+
66+
/**
67+
* Initialize symbols from parsed parameters. \p variable_names must be supplied by
68+
* the caller because Coupleable::coupledNames() is protected.
69+
*/
70+
template <typename T>
71+
void initParsed(T * obj, const std::vector<VariableName> & variable_names);
72+
73+
/**
74+
* Parsed expression
75+
*/
76+
const std::string & _expression;
77+
/**
78+
* Parsed function builder
79+
*/
80+
std::shared_ptr<RPNBuilder> _builder;
81+
/**
82+
* Parsed function evaluator
83+
*/
84+
RPNEvaluator _evaluator;
85+
/**
86+
* Constants used in the parsed expression
87+
*/
88+
std::unordered_map<std::string, Real> _constants;
89+
/**
90+
* Scalar variables used in the parsed expression
91+
*/
92+
std::unordered_map<std::string, std::reference_wrapper<const Real>> _scalars;
93+
/**
94+
* Field variables used in the parsed expression
95+
*/
96+
std::unordered_map<std::string, VariableValue> _fields;
97+
/**
98+
* Material properties used in the parsed expression
99+
*/
100+
std::unordered_map<std::string, MaterialProperty<Real>> _properties;
101+
/**
102+
* Functions used in the parsed expression
103+
*/
104+
std::unordered_map<std::string, Function> _functions;
105+
106+
private:
107+
/**
108+
* Finalize parsed function
109+
*/
110+
void finalize();
111+
112+
/**
113+
* Parsed object
114+
*/
115+
const MooseObject * _parsed_object;
116+
/**
117+
* All symbols added to the parsed function
118+
*/
119+
std::unordered_set<std::string> _all_symbols;
120+
};
121+
122+
template <typename T>
123+
void
124+
ParsedObjectBase::checkDuplicateSymbols(const std::vector<T> & symbols, const std::string & param)
125+
{
126+
for (const auto & symbol : symbols)
127+
{
128+
if (_all_symbols.count(symbol))
129+
_parsed_object->paramError(param, "Symbol '", symbol, "' was added multiple times.");
130+
131+
_all_symbols.insert(symbol);
132+
}
133+
}
134+
135+
template <typename T>
136+
void
137+
ParsedObjectBase::initParsed(T * obj, const std::vector<VariableName> & variable_names)
138+
{
139+
const auto & constant_names = obj->template getParam<std::vector<std::string>>("constant_names");
140+
const auto & postprocessor_names =
141+
obj->template getParam<std::vector<PostprocessorName>>("postprocessor_names");
142+
const auto & property_names =
143+
obj->template getParam<std::vector<MaterialPropertyName>>("material_property_names");
144+
const auto & function_names = obj->template getParam<std::vector<FunctionName>>("function_names");
145+
const auto & constant_expressions =
146+
obj->template getParam<std::vector<Real>>("constant_expressions");
147+
148+
for (const auto i : make_range(constant_names.size()))
149+
addConstant(constant_names[i], constant_expressions[i]);
150+
151+
for (const auto & pp : postprocessor_names)
152+
addScalar(pp, obj->getPostprocessorValueByName(pp));
153+
154+
for (const auto i : make_range(variable_names.size()))
155+
addField(variable_names[i], obj->kokkosCoupledValue("coupled_variables", i));
156+
157+
for (const auto & prop : property_names)
158+
addProperty(prop, obj->template getKokkosMaterialPropertyByName<Real>(prop));
159+
160+
for (const auto & func : function_names)
161+
addFunction(func, obj->getKokkosFunctionByName(func));
162+
163+
finalize();
164+
}
165+
166+
} // namespace Moose::Kokkos
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
10+
#pragma once
11+
12+
#include "KokkosNeumannBC.h"
13+
14+
/**
15+
* Implements a Neumann BC where D grad(u) = value * M on the boundary, where
16+
* value is a constant and M is a material property.
17+
* Uses the term produced from integrating the diffusion operator by parts.
18+
*/
19+
class KokkosMatNeumannBC : public KokkosNeumannBC
20+
{
21+
public:
22+
static InputParameters validParams();
23+
24+
KokkosMatNeumannBC(const InputParameters & parameters);
25+
26+
template <typename Derived>
27+
KOKKOS_FUNCTION Real computeQpResidual(const unsigned int qp, AssemblyDatum & datum) const;
28+
29+
protected:
30+
/// Value of material property on the boundary.
31+
const Moose::Kokkos::MaterialProperty<Real> _boundary_prop;
32+
};
33+
34+
template <typename Derived>
35+
KOKKOS_FUNCTION Real
36+
KokkosMatNeumannBC::computeQpResidual(const unsigned int qp, AssemblyDatum & datum) const
37+
{
38+
return -_value * _boundary_prop(datum, qp);
39+
}

framework/include/kokkos/bcs/KokkosNeumannBC.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class KokkosNeumannBC : public Moose::Kokkos::IntegratedBCValue
2121
template <typename Derived>
2222
KOKKOS_FUNCTION Real computeQpResidual(const unsigned int, AssemblyDatum &) const;
2323

24-
private:
24+
protected:
2525
/// Value of grad(u) on the boundary.
2626
const Real _value;
2727
};

0 commit comments

Comments
 (0)