Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example usage of FieldManager in an NGP Kernel #1182

Draft
wants to merge 17 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
12 changes: 4 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,8 @@ 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 +51,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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As @overfelt pointed out, if this is in the constructor it needs to take a partvec so all the parts are registered. Or we need a secondary registration process. Planning to pursue the former with #1183

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually looking at this closer, I'm not so sure we need a part vec. I think the field registry process will just add more parts each time it is called after a field is registered since multiple identical registrations are a no-op in stk. It would be good to test it though.

: 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);
psakievich marked this conversation as resolved.
Show resolved Hide resolved
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
6 changes: 6 additions & 0 deletions unit_tests/UnitTestHelperObjects.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <stk_topology/topology.hpp>

#include <memory>
#include <FieldManager.h>

namespace unit_test_utils {

Expand All @@ -33,6 +34,11 @@ struct HelperObjectsBase
eqSystem(eqSystems)
{
realm.bulkData_ = bulk;
// hack
// realm.setup_field_manager();
const int numStates = 3;
realm.fieldManager_ = std::make_unique<sierra::nalu::FieldManager>(
realm.meta_data(), numStates);
}

virtual ~HelperObjectsBase() { delete naluObj; }
Expand Down
12 changes: 8 additions & 4 deletions unit_tests/node_kernels/UnitTestSSTNodeKernel.C
Original file line number Diff line number Diff line change
Expand Up @@ -636,8 +636,6 @@ TEST_F(SSTKernelHex8Mesh, NGP_tke_sst_node)
if (bulk_->parallel_size() > 1)
return;

fill_mesh_and_init_fields();

// Setup solution options
solnOpts_.meshMotion_ = false;
solnOpts_.externalMeshDeformation_ = false;
Expand All @@ -646,7 +644,12 @@ 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]);

// 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();

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