Skip to content

Commit 81a8706

Browse files
committed
Element user object and postprocessor #30655
1 parent fc10ad3 commit 81a8706

File tree

9 files changed

+400
-0
lines changed

9 files changed

+400
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 "KokkosElementPostprocessor.h"
13+
14+
class KokkosElementIntegralPostprocessor : public Moose::Kokkos::ElementPostprocessor
15+
{
16+
public:
17+
static InputParameters validParams();
18+
19+
KokkosElementIntegralPostprocessor(const InputParameters & parameters);
20+
21+
virtual void initialize() override;
22+
virtual Real getValue() const override;
23+
virtual void finalize() override;
24+
25+
template <typename Derived>
26+
KOKKOS_FUNCTION void
27+
computeInternal(const Derived & postprocessor, Datum & datum, Real * result) const;
28+
29+
KOKKOS_FUNCTION void join(DefaultLoop, Real * result, const Real * source) const
30+
{
31+
result[0] += source[0];
32+
}
33+
34+
KOKKOS_FUNCTION void init(DefaultLoop, Real * result) const { result[0] = 0; }
35+
};
36+
37+
template <typename Derived>
38+
KOKKOS_FUNCTION void
39+
KokkosElementIntegralPostprocessor::computeInternal(const Derived & postprocessor,
40+
Datum & datum,
41+
Real * result) const
42+
{
43+
Real sum = 0;
44+
45+
for (unsigned int qp = 0; qp < datum.n_qps(); ++qp)
46+
{
47+
datum.reinit();
48+
sum += datum.JxW(qp) * postprocessor.computeQpIntegral(qp, datum);
49+
}
50+
51+
result[0] += sum;
52+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 "KokkosElementPostprocessor.h"
13+
14+
class KokkosElementIntegralVariablePostprocessor : public KokkosElementIntegralPostprocessor
15+
{
16+
public:
17+
static InputParameters validParams();
18+
19+
KokkosElementIntegralVariablePostprocessor(const InputParameters & parameters);
20+
21+
KOKKOS_FUNCTION Real computeQpIntegral(const unsigned int qp, Datum & datum) const;
22+
23+
protected:
24+
/// Holds the solution at current quadrature points
25+
Moose::Kokkos::VariableValue _u;
26+
/// Holds the solution gradient at the current quadrature points
27+
Moose::Kokkos::VariableGradient _grad_u;
28+
/// Option to use absolute variable value
29+
const bool _use_abs_value;
30+
};
31+
32+
Real
33+
KokkosElementIntegralVariablePostprocessor::computeQpIntegral(const unsigned int qp,
34+
Datum & datum) const
35+
{
36+
if (_use_abs_value)
37+
return Kokkos::abs(_u(datum, qp));
38+
else
39+
return _u(datum, qp);
40+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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 "KokkosPostprocessor.h"
13+
#include "KokkosElementUserObject.h"
14+
15+
namespace Moose
16+
{
17+
namespace Kokkos
18+
{
19+
20+
class ElementPostprocessor : public ElementUserObject, public Postprocessor
21+
{
22+
public:
23+
static InputParameters validParams();
24+
25+
ElementPostprocessor(const InputParameters & parameters);
26+
27+
virtual void execute() override;
28+
29+
/**
30+
* Finalize is not required for Postprocessor implementations since work may be done in
31+
* getValue().
32+
*/
33+
virtual void finalize() override {}
34+
35+
/**
36+
* The parallel computation entry function called by Kokkos
37+
*/
38+
template <typename Derived>
39+
KOKKOS_FUNCTION void
40+
operator()(DefaultLoop, const ThreadID tid, const Derived & postprocessor, Real * result) const;
41+
42+
/**
43+
* Evaluate postprocessor
44+
* @param postprocessor The postprocessor object of the final derived type
45+
* @param datum The Datum object of the current thread
46+
* @param result The result buffer
47+
*/
48+
template <typename Derived>
49+
KOKKOS_FUNCTION void
50+
computeInternal(const Derived & postprocessor, Datum & datum, Real * result) const;
51+
52+
protected:
53+
/**
54+
* Reduction buffer
55+
*/
56+
::Kokkos::View<Real *, ::Kokkos::HostSpace> _buffer;
57+
/**
58+
* Allocate reduction buffer
59+
*/
60+
void allocateBuffer(const unsigned int size) { ::Kokkos::realloc(_buffer, size); }
61+
};
62+
63+
template <typename Derived>
64+
KOKKOS_FUNCTION void
65+
ElementPostprocessor::operator()(DefaultLoop,
66+
const ThreadID tid,
67+
const Derived & postprocessor,
68+
Real * result) const
69+
{
70+
auto elem = kokkosBlockElementID(tid);
71+
72+
Datum datum(elem, libMesh::invalid_uint, kokkosAssembly(), kokkosSystems());
73+
74+
postprocessor.computeInternal(postprocessor, datum, result);
75+
}
76+
77+
template <typename Derived>
78+
KOKKOS_FUNCTION void
79+
ElementPostprocessor::computeInternal(const Derived & postprocessor,
80+
Datum & datum,
81+
Real * result) const
82+
{
83+
for (unsigned int qp = 0; qp < datum.n_qps(); ++qp)
84+
{
85+
datum.reinit();
86+
postprocessor.compute(qp, datum, result);
87+
}
88+
}
89+
90+
} // namespace Kokkos
91+
} // namespace Moose
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 "KokkosUserObject.h"
13+
14+
#include "BlockRestrictable.h"
15+
#include "MaterialPropertyInterface.h"
16+
#include "CoupleableMooseVariableDependencyIntermediateInterface.h"
17+
#include "TransientInterface.h"
18+
#include "RandomInterface.h"
19+
#include "ElementIDInterface.h"
20+
21+
namespace Moose
22+
{
23+
namespace Kokkos
24+
{
25+
26+
class ElementUserObject : public UserObject,
27+
public ::BlockRestrictable,
28+
public ::MaterialPropertyInterface,
29+
public ::CoupleableMooseVariableDependencyIntermediateInterface,
30+
public ::TransientInterface,
31+
public ::RandomInterface,
32+
public ::ElementIDInterface
33+
{
34+
public:
35+
static InputParameters validParams();
36+
37+
ElementUserObject(const InputParameters & parameters);
38+
39+
/**
40+
* Copy constructor for parallel dispatch
41+
*/
42+
ElementUserObject(const ElementUserObject & object);
43+
};
44+
45+
} // namespace Kokkos
46+
} // namespace Moose

framework/include/utils/MooseTypes.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,6 +1251,7 @@ class FunctorCopy
12511251
friend class AuxKernel;
12521252
friend class FunctionBase;
12531253
friend class UserObject;
1254+
friend class ElementUserObject;
12541255
friend class Postprocessor;
12551256

12561257
FunctorCopy() {}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
#include "KokkosElementIntegralPostprocessor.h"
11+
12+
InputParameters
13+
KokkosElementIntegralPostprocessor::validParams()
14+
{
15+
InputParameters params = ElementPostprocessor::validParams();
16+
return params;
17+
}
18+
19+
KokkosElementIntegralPostprocessor::KokkosElementIntegralPostprocessor(
20+
const InputParameters & parameters)
21+
: ElementPostprocessor(parameters)
22+
{
23+
}
24+
25+
void
26+
KokkosElementIntegralPostprocessor::initialize()
27+
{
28+
allocateBuffer(1);
29+
}
30+
31+
Real
32+
KokkosElementIntegralPostprocessor::getValue() const
33+
{
34+
return _buffer[0];
35+
}
36+
37+
void
38+
KokkosElementIntegralPostprocessor::finalize()
39+
{
40+
gatherSum(_buffer[0]);
41+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
#include "KokkosElementIntegralVariablePostprocessor.h"
11+
12+
registerKokkosPostprocessor("MooseApp", KokkosElementIntegralVariablePostprocessor);
13+
14+
InputParameters
15+
KokkosElementIntegralVariablePostprocessor::validParams()
16+
{
17+
InputParameters params = KokkosElementIntegralPostprocessor::validParams();
18+
params.addRequiredCoupledVar("variable", "The name of the variable that this object operates on");
19+
params.addClassDescription("Computes a volume integral of the specified variable");
20+
params.addParam<bool>(
21+
"use_absolute_value", false, "Whether to use absolute value of the variable or not");
22+
return params;
23+
}
24+
25+
KokkosElementIntegralVariablePostprocessor::KokkosElementIntegralVariablePostprocessor(
26+
const InputParameters & parameters)
27+
: KokkosElementIntegralPostprocessor(parameters),
28+
_u(kokkosCoupledValue("variable")),
29+
_grad_u(kokkosCoupledGradient("variable")),
30+
_use_abs_value(getParam<bool>("use_absolute_value"))
31+
{
32+
addMooseVariableDependency(&_fe_problem.getVariable(_tid, getParam<std::string>("variable")));
33+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
#include "KokkosElementPostprocessor.h"
11+
12+
namespace Moose
13+
{
14+
namespace Kokkos
15+
{
16+
17+
InputParameters
18+
ElementPostprocessor::validParams()
19+
{
20+
InputParameters params = ElementUserObject::validParams();
21+
params += Postprocessor::validParams();
22+
return params;
23+
}
24+
25+
ElementPostprocessor::ElementPostprocessor(const InputParameters & parameters)
26+
: ElementUserObject(parameters), Postprocessor(this)
27+
{
28+
}
29+
30+
void
31+
ElementPostprocessor::execute()
32+
{
33+
if (!_buffer.is_allocated())
34+
mooseError("Reduction buffer was not allocated.");
35+
36+
Policy policy(0, numKokkosBlockElements());
37+
38+
if (!_dispatcher)
39+
_dispatcher = DispatcherRegistry::build<DefaultLoop>(this, type());
40+
41+
_dispatcher->parallelReduce(policy, _buffer);
42+
}
43+
44+
} // namespace Kokkos
45+
} // namespace Moose

0 commit comments

Comments
 (0)