Skip to content

Commit

Permalink
Example usage of FieldManager
Browse files Browse the repository at this point in the history
This PR demonstrates how a Kernel can be converted to use a
FieldManager.

The main goal is to move the field registration away from higher level
abstractions like realm and equationsystem, to lower ones (algs and
kernels).

This will essentially eliminate the need for `RegisterNodalFields` type
calls, and will eliminate a range of bugs that can come in from not
registering fields in the right place.  Having kernels and algs register
the fields they need will also reduce the amount of fixturing needed for
unit tests since constructing the object will register the fields for
you.  You can then finalize the mesh and populate the fields with
initial values. (Not demonstrated in this PR).
  • Loading branch information
psakievich committed May 3, 2023
1 parent 472317f commit b310e7e
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 36 deletions.
25 changes: 18 additions & 7 deletions include/FieldManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include <stk_mesh/base/FieldState.hpp>
#include "stk_mesh/base/GetNgpField.hpp"
#include <string>
#include <type_traits>

namespace stk {
namespace mesh {
Expand Down Expand Up @@ -62,12 +61,12 @@ class FieldManager
T* register_field(
const std::string& name,
const stk::mesh::PartVector& parts,
const void* init_val = nullptr,
const void* initVal = nullptr,
stk::mesh::FieldState state = stk::mesh::FieldState::StateNone)
{
const int numStates = 0;
const int numComponents = 0;
register_field(name, parts, numStates, numComponents, init_val);
register_field(name, parts, numStates, numComponents, initVal);
return get_field_ptr<T>(name, state);
}

Expand All @@ -84,10 +83,10 @@ class FieldManager
const stk::mesh::PartVector& parts,
const int numStates,
const int numComponents,
const void* init_val = nullptr,
const void* initVal = nullptr,
stk::mesh::FieldState state = stk::mesh::FieldState::StateNone)
{
register_field(name, parts, numStates, numComponents, init_val);
register_field(name, parts, numStates, numComponents, initVal);
return get_field_ptr<GenericFieldType>(name, state);
}

Expand Down Expand Up @@ -116,7 +115,7 @@ class FieldManager
/// would otherwise be defined in the field Registry class.
///
/// If numStates = 0 then the number of states comes from the
/// field Registry. Same for numComponents = 0 and init_val = nullptr.
/// field Registry. Same for numComponents = 0 and initVal = nullptr.
///
/// This is useful for dynamic fields that depend on the input
/// options to define the number of states or number of components since the
Expand All @@ -128,7 +127,19 @@ class FieldManager
const stk::mesh::PartVector& parts,
const int numStates = 0,
const int numComponents = 0,
const void* init_val = nullptr);
const void* initVal = nullptr) const;

FieldPointerTypes register_field(
const std::string& name,
stk::mesh::Part* part,
const int numStates = 0,
const int numComponents = 0,
const void* initVal = nullptr) const
{
stk::mesh::PartVector parts;
parts.push_back(part);
register_field(name, parts, numStates, numComponents, initVal);
}

/// Given the named field that has already been registered on the CPU
/// return the GPU version of the same field.
Expand Down
14 changes: 6 additions & 8 deletions include/node_kernels/TKESSTNodeKernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@
#ifndef TKESSTNODEKERNEL_H
#define TKESSTNODEKERNEL_H

#include "LinearSystem.h"
#include "node_kernels/NodeKernel.h"
#include "FieldTypeDef.h"
#include "stk_mesh/base/BulkData.hpp"
#include "stk_mesh/base/Ngp.hpp"
#include "stk_mesh/base/NgpField.hpp"
#include "stk_mesh/base/Types.hpp"
#include <FieldManager.h>

namespace sierra {
namespace nalu {
Expand All @@ -25,7 +27,10 @@ class Realm;
class TKESSTNodeKernel : public NGPNodeKernel<TKESSTNodeKernel>
{
public:
TKESSTNodeKernel(const stk::mesh::MetaData&);
TKESSTNodeKernel(
const stk::mesh::MetaData&,
const FieldManager&,
stk::mesh::Part* part);

TKESSTNodeKernel() = delete;

Expand All @@ -48,13 +53,6 @@ class TKESSTNodeKernel : public NGPNodeKernel<TKESSTNodeKernel>
stk::mesh::NgpField<double> dudx_;
stk::mesh::NgpField<double> dualNodalVolume_;

unsigned tkeID_{stk::mesh::InvalidOrdinal};
unsigned sdrID_{stk::mesh::InvalidOrdinal};
unsigned densityID_{stk::mesh::InvalidOrdinal};
unsigned tviscID_{stk::mesh::InvalidOrdinal};
unsigned dudxID_{stk::mesh::InvalidOrdinal};
unsigned dualNodalVolumeID_{stk::mesh::InvalidOrdinal};

NodeKernelTraits::DblType betaStar_;
NodeKernelTraits::DblType tkeProdLimitRatio_;
NodeKernelTraits::DblType tkeAmb_;
Expand Down
4 changes: 2 additions & 2 deletions src/FieldManager.C
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ FieldManager::register_field(
const stk::mesh::PartVector& parts,
const int numStates,
const int numComponents,
const void* init_val)
const void* initVal) const
{
auto definition = FieldRegistry::query(numDimensions_, numStates_, name);

Expand All @@ -48,7 +48,7 @@ FieldManager::register_field(
const int num_components =
numComponents ? numComponents : def.num_components;

const val_type* init = static_cast<const val_type*>(init_val);
const val_type* init = static_cast<const val_type*>(initVal);
auto* id = &(meta_.declare_field<field_type>(def.rank, name, num_states));
for (auto&& part : parts) {
stk::mesh::put_field_on_mesh(*id, *part, num_components, init);
Expand Down
3 changes: 2 additions & 1 deletion src/TurbKineticEnergyEquationSystem.C
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,8 @@ TurbKineticEnergyEquationSystem::register_interior_algorithm(
nodeAlg.add_kernel<TKEKsgsNodeKernel>(realm_.meta_data());
break;
case TurbulenceModel::SST:
nodeAlg.add_kernel<TKESSTNodeKernel>(realm_.meta_data());
nodeAlg.add_kernel<TKESSTNodeKernel>(
realm_.meta_data(), *(realm_.fieldManager_.get()), part);
break;
case TurbulenceModel::SSTLR:
nodeAlg.add_kernel<TKESSTLRNodeKernel>(realm_.meta_data());
Expand Down
34 changes: 18 additions & 16 deletions src/node_kernels/TKESSTNodeKernel.C
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,31 @@
namespace sierra {
namespace nalu {

TKESSTNodeKernel::TKESSTNodeKernel(const stk::mesh::MetaData& meta)
: NGPNodeKernel<TKESSTNodeKernel>(),
tkeID_(get_field_ordinal(meta, "turbulent_ke")),
sdrID_(get_field_ordinal(meta, "specific_dissipation_rate")),
densityID_(get_field_ordinal(meta, "density")),
tviscID_(get_field_ordinal(meta, "turbulent_viscosity")),
dudxID_(get_field_ordinal(meta, "dudx")),
dualNodalVolumeID_(get_field_ordinal(meta, "dual_nodal_volume")),
nDim_(meta.spatial_dimension())
TKESSTNodeKernel::TKESSTNodeKernel(
const stk::mesh::MetaData& meta,
const FieldManager& manager,
stk::mesh::Part* part)
: NGPNodeKernel<TKESSTNodeKernel>(), nDim_(meta.spatial_dimension())
{
manager.register_field("turbulent_ke", part);
manager.register_field("specific_dissipation_rate", part);
manager.register_field("density", part);
manager.register_field("turbulent_viscosity", part);
manager.register_field("dudx", part);
manager.register_field("dual_nodal_volume", part);
}

void
TKESSTNodeKernel::setup(Realm& realm)
{
const auto& fieldMgr = realm.ngp_field_manager();
const auto& fieldMgr = *(realm.fieldManager_.get());

tke_ = fieldMgr.get_field<double>(tkeID_);
sdr_ = fieldMgr.get_field<double>(sdrID_);
density_ = fieldMgr.get_field<double>(densityID_);
tvisc_ = fieldMgr.get_field<double>(tviscID_);
dudx_ = fieldMgr.get_field<double>(dudxID_);
dualNodalVolume_ = fieldMgr.get_field<double>(dualNodalVolumeID_);
tke_ = fieldMgr.get_ngp_field_ptr("turbulent_ke");
sdr_ = fieldMgr.get_ngp_field_ptr("specific_dissipation_rate");
density_ = fieldMgr.get_ngp_field_ptr("density");
tvisc_ = fieldMgr.get_ngp_field_ptr("turbulent_viscosity");
dudx_ = fieldMgr.get_ngp_field_ptr("dudx");
dualNodalVolume_ = fieldMgr.get_ngp_field_ptr("dual_nodal_volume");

// Update turbulence model constants
betaStar_ = realm.get_turb_model_constant(TM_betaStar);
Expand Down
8 changes: 6 additions & 2 deletions unit_tests/node_kernels/UnitTestSSTNodeKernel.C
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,8 @@ TEST_F(SSTKernelHex8Mesh, NGP_tke_sst_node)
if (bulk_->parallel_size() > 1)
return;

// TDODO we can eliminate all the excess fields if we decide to do our field
// init after we add the kernels
fill_mesh_and_init_fields();

// Setup solution options
Expand All @@ -646,7 +648,8 @@ TEST_F(SSTKernelHex8Mesh, NGP_tke_sst_node)
unit_test_utils::NodeHelperObjects helperObjs(
bulk_, stk::topology::HEX_8, 1, partVec_[0]);

helperObjs.nodeAlg->add_kernel<sierra::nalu::TKESSTNodeKernel>(*meta_);
helperObjs.nodeAlg->add_kernel<sierra::nalu::TKESSTNodeKernel>(
*meta_, *(helperObjs.realm.fieldManager_.get()), partVec_[0]);

helperObjs.execute();

Expand Down Expand Up @@ -685,7 +688,8 @@ TEST_F(SSTKernelHex8Mesh, NGP_tke_sst_sust_node)
realm.solutionOptions_->turbModelConstantMap_[sierra::nalu::TM_tkeAmb] = 5.0;
realm.solutionOptions_->turbModelConstantMap_[sierra::nalu::TM_sdrAmb] = 50.0;

helperObjs.nodeAlg->add_kernel<sierra::nalu::TKESSTNodeKernel>(*meta_);
helperObjs.nodeAlg->add_kernel<sierra::nalu::TKESSTNodeKernel>(
*meta_, *(realm.fieldManager_.get()), partVec_[0]);

helperObjs.execute();

Expand Down

0 comments on commit b310e7e

Please sign in to comment.