From 816c3fb8d2a1c8dae064618ba2556a7280b77704 Mon Sep 17 00:00:00 2001 From: Philip Sakievich Date: Tue, 12 Jul 2022 23:11:34 -0600 Subject: [PATCH 1/6] Refactor: boundary conditions --- include/BoundaryConditions.h | 70 ++++++++++++++++-------------------- src/BoundaryConditions.C | 32 +++++++---------- 2 files changed, 43 insertions(+), 59 deletions(-) diff --git a/include/BoundaryConditions.h b/include/BoundaryConditions.h index af9710faf..c6c428b7a 100644 --- a/include/BoundaryConditions.h +++ b/include/BoundaryConditions.h @@ -17,6 +17,7 @@ #include #include #include +#include namespace YAML { class Node; @@ -34,53 +35,42 @@ class BoundaryCondition { BoundaryCondition(BoundaryConditions& bcs) : boundaryConditions_(bcs) {} virtual ~BoundaryCondition() {} - - BoundaryCondition * load(const YAML::Node & node) ; + + std::unique_ptr load(const YAML::Node& node); Simulation *root(); BoundaryConditions *parent(); - - void breadboard() - { - // nothing - } - + std::string bcName_; std::string targetName_; BoundaryConditionType theBcType_; BoundaryConditions& boundaryConditions_; }; - - typedef std::vector BoundaryConditionVector; - - class BoundaryConditions { - public: - - BoundaryConditions(Realm& realm) - : realm_(realm) {} - ~BoundaryConditions() { - for ( size_t iboundary_condition = 0; iboundary_condition < boundaryConditionVector_.size(); ++iboundary_condition ) { - delete boundaryConditionVector_[iboundary_condition]; - } - } - - BoundaryConditions* load(const YAML::Node & node); - - void breadboard() - { - for ( size_t iboundary_condition = 0; iboundary_condition < boundaryConditionVector_.size(); ++iboundary_condition ) { - boundaryConditionVector_[iboundary_condition]->breadboard(); - } - } - - Simulation *root(); - Realm *parent(); - - // ease of access methods to particular boundary condition - size_t size() {return boundaryConditionVector_.size();} - BoundaryCondition *operator[](int i) { return boundaryConditionVector_[i];} - - Realm &realm_; - BoundaryConditionVector boundaryConditionVector_; + +typedef std::vector> BoundaryConditionVector; + +class BoundaryConditions +{ +public: + BoundaryConditions(Realm& realm) : realm_(realm) {} + ~BoundaryConditions() = default; + + void load(const YAML::Node& node); + + // TODO(psakiev) Delete this unused function + void breadboard() {} + + Simulation* root(); + Realm* parent(); + + // ease of access methods to particular boundary condition + size_t size() { return boundaryConditionVector_.size(); } + BoundaryCondition* operator[](int i) + { + return boundaryConditionVector_[i].get(); + } + + Realm& realm_; + BoundaryConditionVector boundaryConditionVector_; }; } // namespace nalu diff --git a/src/BoundaryConditions.C b/src/BoundaryConditions.C index e92184555..fd06ab8c1 100644 --- a/src/BoundaryConditions.C +++ b/src/BoundaryConditions.C @@ -42,42 +42,41 @@ namespace nalu{ /// the node is determined from some information, then a particular type /// of object is created and returned to the parent. -BoundaryCondition * BoundaryCondition::load(const YAML::Node & node) +std::unique_ptr BoundaryCondition::load(const YAML::Node & node) { + std::unique_ptr this_bc; if ( node["wall_boundary_condition"] ){ - WallBoundaryConditionData& wallBC = *new WallBoundaryConditionData(*parent()); - node >> wallBC; - NaluEnv::self().naluOutputP0() << "Wall BC name: " << wallBC.bcName_ - << " on " << wallBC.targetName_ << std::endl; - return &wallBC; + //this_bc.reset(new WallBoundaryConditionData(boundaryConditions_)); + this_bc = std::make_unique(boundaryConditions_); + auto* wallBC = dynamic_cast(this_bc.get()); + + node >> *wallBC; + NaluEnv::self().naluOutputP0() << "Wall BC name: " << wallBC->bcName_ + << " on " << wallBC->targetName_ << std::endl; } else if (node["inflow_boundary_condition"]) { InflowBoundaryConditionData& inflowBC = *new InflowBoundaryConditionData(*parent()); node >> inflowBC; NaluEnv::self().naluOutputP0() << "Inflow BC name: " << inflowBC.bcName_ << " on " << inflowBC.targetName_ << std::endl; - return &inflowBC; } else if (node["open_boundary_condition"]) { OpenBoundaryConditionData& openBC = *new OpenBoundaryConditionData(*parent()); node >> openBC; NaluEnv::self().naluOutputP0() << "Open BC name: " << openBC.bcName_ << " on " << openBC.targetName_ << std::endl; - return &openBC; } else if (node["symmetry_boundary_condition"]) { SymmetryBoundaryConditionData& symmetryBC = *new SymmetryBoundaryConditionData(*parent()); node >> symmetryBC; NaluEnv::self().naluOutputP0() << "Symmetry BC name: " << symmetryBC.bcName_ << " on " << symmetryBC.targetName_ << std::endl; - return &symmetryBC; } else if (node["abltop_boundary_condition"]) { ABLTopBoundaryConditionData& abltopBC = *new ABLTopBoundaryConditionData(*parent()); node >> abltopBC; NaluEnv::self().naluOutputP0() << "ABLTop BC name: " << abltopBC.bcName_ << " on " << abltopBC.targetName_ << std::endl; - return &abltopBC; } else if (node["periodic_boundary_condition"]) { PeriodicBoundaryConditionData& periodicBC = *new PeriodicBoundaryConditionData(*parent()); @@ -85,24 +84,22 @@ BoundaryCondition * BoundaryCondition::load(const YAML::Node & node) NaluEnv::self().naluOutputP0() << "Periodic BC name: " << periodicBC.bcName_ << " between " << periodicBC.masterSlave_.master_ << " and "<< periodicBC.masterSlave_.slave_ << std::endl; - return &periodicBC; } else if (node["non_conformal_boundary_condition"]) { NonConformalBoundaryConditionData& nonConformalBC = *new NonConformalBoundaryConditionData(*parent()); node >> nonConformalBC; NaluEnv::self().naluOutputP0() << "NonConformal BC name: " << nonConformalBC.bcName_ << " using " << nonConformalBC.targetName_ << std::endl; - return &nonConformalBC; } else if (node["overset_boundary_condition"]) { OversetBoundaryConditionData& oversetBC = *new OversetBoundaryConditionData(*parent()); node >> oversetBC; NaluEnv::self().naluOutputP0() << "Overset BC name: " << oversetBC.bcName_ << std::endl; - return &oversetBC; } else { throw std::runtime_error("parser error BoundaryConditions::load: no such bc type"); } + return this_bc; // Avoid nvcc unreachable statement warnings #ifndef __CUDACC__ return 0; @@ -115,23 +112,20 @@ BoundaryCondition * BoundaryCondition::load(const YAML::Node & node) Simulation* BoundaryConditions::root() { return parent()->root(); } Realm *BoundaryConditions::parent() { return &realm_; } -BoundaryConditions* BoundaryConditions::load(const YAML::Node &node) +void BoundaryConditions::load(const YAML::Node &node) { - BoundaryCondition tmp_boundary_condition(*this); + BoundaryCondition bc_factory(*this); if(node["boundary_conditions"]) { const YAML::Node boundary_conditions = node["boundary_conditions"]; for ( size_t iboundary_condition = 0; iboundary_condition < boundary_conditions.size(); ++iboundary_condition ) { const YAML::Node boundary_condition_node = boundary_conditions[iboundary_condition]; - BoundaryCondition* bc = tmp_boundary_condition.load(boundary_condition_node); - boundaryConditionVector_.push_back(bc); + boundaryConditionVector_.emplace_back(bc_factory.load(boundary_condition_node)); } } else { throw std::runtime_error("parser error BoundaryConditions::load"); } - - return this; } From ac493cef140bfe0705fab6529ff487e1caeaa707 Mon Sep 17 00:00:00 2001 From: Philip Sakievich Date: Tue, 12 Jul 2022 23:59:07 -0600 Subject: [PATCH 2/6] Refactor all the bcs to use smart pointers --- src/BoundaryConditions.C | 74 +++++++++++++++++----------------------- 1 file changed, 32 insertions(+), 42 deletions(-) diff --git a/src/BoundaryConditions.C b/src/BoundaryConditions.C index fd06ab8c1..a8be61bde 100644 --- a/src/BoundaryConditions.C +++ b/src/BoundaryConditions.C @@ -38,72 +38,62 @@ namespace nalu{ //-------------------------------------------------------------------------- -/// this is an example of a load() method with polymorphism - the type of -/// the node is determined from some information, then a particular type -/// of object is created and returned to the parent. +template +std::unique_ptr register_bc(const YAML::Node& node, BoundaryConditions& bcs){ + std::unique_ptrthis_bc = std::make_unique(bcs); + auto* cast_bc = dynamic_cast(this_bc.get()); + node >> *cast_bc; + return this_bc; +} std::unique_ptr BoundaryCondition::load(const YAML::Node & node) { std::unique_ptr this_bc; if ( node["wall_boundary_condition"] ){ - //this_bc.reset(new WallBoundaryConditionData(boundaryConditions_)); - this_bc = std::make_unique(boundaryConditions_); - auto* wallBC = dynamic_cast(this_bc.get()); - - node >> *wallBC; - NaluEnv::self().naluOutputP0() << "Wall BC name: " << wallBC->bcName_ - << " on " << wallBC->targetName_ << std::endl; + this_bc = std::move(register_bc(node, boundaryConditions_)); + NaluEnv::self().naluOutputP0()<<"Wall BC name: " << this_bc->bcName_ \ + << " on " << this_bc->targetName_ << std::endl; } else if (node["inflow_boundary_condition"]) { - InflowBoundaryConditionData& inflowBC = *new InflowBoundaryConditionData(*parent()); - node >> inflowBC; - NaluEnv::self().naluOutputP0() << "Inflow BC name: " << inflowBC.bcName_ - << " on " << inflowBC.targetName_ << std::endl; + this_bc = std::move(register_bc(node, boundaryConditions_)); + NaluEnv::self().naluOutputP0()<<"Inflow BC name: " << this_bc->bcName_ \ + << " on " << this_bc->targetName_ << std::endl; } else if (node["open_boundary_condition"]) { - OpenBoundaryConditionData& openBC = *new OpenBoundaryConditionData(*parent()); - node >> openBC; - NaluEnv::self().naluOutputP0() << "Open BC name: " << openBC.bcName_ - << " on " << openBC.targetName_ << std::endl; + this_bc = std::move(register_bc(node, boundaryConditions_)); + NaluEnv::self().naluOutputP0()<<"Open BC name: " << this_bc->bcName_ \ + << " on " << this_bc->targetName_ << std::endl; } else if (node["symmetry_boundary_condition"]) { - SymmetryBoundaryConditionData& symmetryBC = *new SymmetryBoundaryConditionData(*parent()); - node >> symmetryBC; - NaluEnv::self().naluOutputP0() << "Symmetry BC name: " << symmetryBC.bcName_ - << " on " << symmetryBC.targetName_ << std::endl; + this_bc = std::move(register_bc(node, boundaryConditions_)); + NaluEnv::self().naluOutputP0()<<"Symmetry BC name: " << this_bc->bcName_ \ + << " on " << this_bc->targetName_ << std::endl; } else if (node["abltop_boundary_condition"]) { - ABLTopBoundaryConditionData& abltopBC = *new ABLTopBoundaryConditionData(*parent()); - node >> abltopBC; - NaluEnv::self().naluOutputP0() << "ABLTop BC name: " << abltopBC.bcName_ - << " on " << abltopBC.targetName_ << std::endl; + this_bc = std::move(register_bc(node, boundaryConditions_)); + NaluEnv::self().naluOutputP0()<<"ABLTop BC name: " << this_bc->bcName_ \ + << " on " << this_bc->targetName_ << std::endl; } else if (node["periodic_boundary_condition"]) { - PeriodicBoundaryConditionData& periodicBC = *new PeriodicBoundaryConditionData(*parent()); - node >> periodicBC; - NaluEnv::self().naluOutputP0() << "Periodic BC name: " << periodicBC.bcName_ - << " between " << periodicBC.masterSlave_.master_ - << " and "<< periodicBC.masterSlave_.slave_ << std::endl; + this_bc = std::move(register_bc(node, boundaryConditions_)); + auto* periodicBC = dynamic_cast(this_bc.get()); + NaluEnv::self().naluOutputP0() << "Periodic BC name: " << periodicBC->bcName_ + << " between " << periodicBC->masterSlave_.master_ + << " and "<< periodicBC->masterSlave_.slave_ << std::endl; } else if (node["non_conformal_boundary_condition"]) { - NonConformalBoundaryConditionData& nonConformalBC = *new NonConformalBoundaryConditionData(*parent()); - node >> nonConformalBC; - NaluEnv::self().naluOutputP0() << "NonConformal BC name: " << nonConformalBC.bcName_ - << " using " << nonConformalBC.targetName_ << std::endl; + this_bc = std::move(register_bc(node, boundaryConditions_)); + NaluEnv::self().naluOutputP0() << "NonConformal BC name: " << this_bc->bcName_ + << " using " << this_bc->targetName_ << std::endl; } else if (node["overset_boundary_condition"]) { - OversetBoundaryConditionData& oversetBC = *new OversetBoundaryConditionData(*parent()); - node >> oversetBC; - NaluEnv::self().naluOutputP0() << "Overset BC name: " << oversetBC.bcName_ << std::endl; + this_bc = std::move(register_bc(node, boundaryConditions_)); + NaluEnv::self().naluOutputP0() << "Overset BC name: " << this_bc->bcName_ << std::endl; } else { throw std::runtime_error("parser error BoundaryConditions::load: no such bc type"); } return this_bc; -// Avoid nvcc unreachable statement warnings -#ifndef __CUDACC__ - return 0; -#endif } Simulation* BoundaryCondition::root() { return parent()->root(); } From 25e9835416f726959b08673a1cc7e55646242ec0 Mon Sep 17 00:00:00 2001 From: Philip Sakievich Date: Wed, 13 Jul 2022 00:03:05 -0600 Subject: [PATCH 3/6] Reformat for readability --- src/BoundaryConditions.C | 184 ++++++++++++++++++++++++--------------- 1 file changed, 115 insertions(+), 69 deletions(-) diff --git a/src/BoundaryConditions.C b/src/BoundaryConditions.C index a8be61bde..f6bd43f9a 100644 --- a/src/BoundaryConditions.C +++ b/src/BoundaryConditions.C @@ -7,8 +7,6 @@ // for more details. // - - #include #include #include @@ -17,8 +15,8 @@ #include #include -namespace sierra{ -namespace nalu{ +namespace sierra { +namespace nalu { //========================================================================== // Class Definition @@ -28,7 +26,7 @@ namespace nalu{ //-------------------------------------------------------------------------- //-------- constructor ----------------------------------------------------- //-------------------------------------------------------------------------- - + //-------------------------------------------------------------------------- //-------- destructor ------------------------------------------------------ //-------------------------------------------------------------------------- @@ -37,87 +35,135 @@ namespace nalu{ //-------- load ----------------------------------------------- //-------------------------------------------------------------------------- - -template -std::unique_ptr register_bc(const YAML::Node& node, BoundaryConditions& bcs){ - std::unique_ptrthis_bc = std::make_unique(bcs); - auto* cast_bc = dynamic_cast(this_bc.get()); - node >> *cast_bc; - return this_bc; +template +std::unique_ptr +register_bc(const YAML::Node& node, BoundaryConditions& bcs) +{ + std::unique_ptr this_bc = std::make_unique(bcs); + auto* cast_bc = dynamic_cast(this_bc.get()); + node >> *cast_bc; + return this_bc; } -std::unique_ptr BoundaryCondition::load(const YAML::Node & node) +std::unique_ptr +BoundaryCondition::load(const YAML::Node& node) { std::unique_ptr this_bc; - if ( node["wall_boundary_condition"] ){ - this_bc = std::move(register_bc(node, boundaryConditions_)); - NaluEnv::self().naluOutputP0()<<"Wall BC name: " << this_bc->bcName_ \ - << " on " << this_bc->targetName_ << std::endl; - } - else if (node["inflow_boundary_condition"]) { - this_bc = std::move(register_bc(node, boundaryConditions_)); - NaluEnv::self().naluOutputP0()<<"Inflow BC name: " << this_bc->bcName_ \ - << " on " << this_bc->targetName_ << std::endl; - } - else if (node["open_boundary_condition"]) { - this_bc = std::move(register_bc(node, boundaryConditions_)); - NaluEnv::self().naluOutputP0()<<"Open BC name: " << this_bc->bcName_ \ - << " on " << this_bc->targetName_ << std::endl; - } - else if (node["symmetry_boundary_condition"]) { - this_bc = std::move(register_bc(node, boundaryConditions_)); - NaluEnv::self().naluOutputP0()<<"Symmetry BC name: " << this_bc->bcName_ \ - << " on " << this_bc->targetName_ << std::endl; - } - else if (node["abltop_boundary_condition"]) { - this_bc = std::move(register_bc(node, boundaryConditions_)); - NaluEnv::self().naluOutputP0()<<"ABLTop BC name: " << this_bc->bcName_ \ - << " on " << this_bc->targetName_ << std::endl; - } - else if (node["periodic_boundary_condition"]) { - this_bc = std::move(register_bc(node, boundaryConditions_)); - auto* periodicBC = dynamic_cast(this_bc.get()); - NaluEnv::self().naluOutputP0() << "Periodic BC name: " << periodicBC->bcName_ - << " between " << periodicBC->masterSlave_.master_ - << " and "<< periodicBC->masterSlave_.slave_ << std::endl; - } - else if (node["non_conformal_boundary_condition"]) { - this_bc = std::move(register_bc(node, boundaryConditions_)); - NaluEnv::self().naluOutputP0() << "NonConformal BC name: " << this_bc->bcName_ - << " using " << this_bc->targetName_ << std::endl; - } - else if (node["overset_boundary_condition"]) { - this_bc = std::move(register_bc(node, boundaryConditions_)); - NaluEnv::self().naluOutputP0() << "Overset BC name: " << this_bc->bcName_ << std::endl; - } - else { - throw std::runtime_error("parser error BoundaryConditions::load: no such bc type"); + if (node["wall_boundary_condition"]) { + this_bc = std::move( + register_bc(node, boundaryConditions_)); + + NaluEnv::self().naluOutputP0() + << "Wall BC name: " << this_bc->bcName_ << " on " + << this_bc->targetName_ << std::endl; + + } else if (node["inflow_boundary_condition"]) { + this_bc = std::move( + register_bc(node, boundaryConditions_)); + + NaluEnv::self().naluOutputP0() + << "Inflow BC name: " << this_bc->bcName_ << " on " + << this_bc->targetName_ << std::endl; + + } else if (node["open_boundary_condition"]) { + this_bc = std::move( + register_bc(node, boundaryConditions_)); + + NaluEnv::self().naluOutputP0() + << "Open BC name: " << this_bc->bcName_ << " on " + << this_bc->targetName_ << std::endl; + + } else if (node["symmetry_boundary_condition"]) { + this_bc = std::move( + register_bc(node, boundaryConditions_)); + + NaluEnv::self().naluOutputP0() + << "Symmetry BC name: " << this_bc->bcName_ << " on " + << this_bc->targetName_ << std::endl; + + } else if (node["abltop_boundary_condition"]) { + this_bc = std::move( + register_bc(node, boundaryConditions_)); + + NaluEnv::self().naluOutputP0() + << "ABLTop BC name: " << this_bc->bcName_ << " on " + << this_bc->targetName_ << std::endl; + + } else if (node["periodic_boundary_condition"]) { + this_bc = std::move( + register_bc(node, boundaryConditions_)); + + auto* periodicBC = + dynamic_cast(this_bc.get()); + + NaluEnv::self().naluOutputP0() + << "Periodic BC name: " << periodicBC->bcName_ << " between " + << periodicBC->masterSlave_.master_ << " and " + << periodicBC->masterSlave_.slave_ << std::endl; + + } else if (node["non_conformal_boundary_condition"]) { + this_bc = std::move(register_bc( + node, boundaryConditions_)); + + NaluEnv::self().naluOutputP0() + << "NonConformal BC name: " << this_bc->bcName_ << " using " + << this_bc->targetName_ << std::endl; + + } else if (node["overset_boundary_condition"]) { + this_bc = std::move( + register_bc(node, boundaryConditions_)); + + NaluEnv::self().naluOutputP0() + << "Overset BC name: " << this_bc->bcName_ << std::endl; + + } else { + throw std::runtime_error( + "parser error BoundaryConditions::load: no such bc type"); } return this_bc; } - Simulation* BoundaryCondition::root() { return parent()->root(); } - BoundaryConditions *BoundaryCondition::parent() { return &boundaryConditions_; } +Simulation* +BoundaryCondition::root() +{ + return parent()->root(); +} +BoundaryConditions* +BoundaryCondition::parent() +{ + return &boundaryConditions_; +} - Simulation* BoundaryConditions::root() { return parent()->root(); } - Realm *BoundaryConditions::parent() { return &realm_; } +Simulation* +BoundaryConditions::root() +{ + return parent()->root(); +} +Realm* +BoundaryConditions::parent() +{ + return &realm_; +} -void BoundaryConditions::load(const YAML::Node &node) +void +BoundaryConditions::load(const YAML::Node& node) { BoundaryCondition bc_factory(*this); - if(node["boundary_conditions"]) { + if (node["boundary_conditions"]) { const YAML::Node boundary_conditions = node["boundary_conditions"]; - for ( size_t iboundary_condition = 0; iboundary_condition < boundary_conditions.size(); ++iboundary_condition ) { - const YAML::Node boundary_condition_node = boundary_conditions[iboundary_condition]; - boundaryConditionVector_.emplace_back(bc_factory.load(boundary_condition_node)); + for (size_t iboundary_condition = 0; + iboundary_condition < boundary_conditions.size(); + ++iboundary_condition) { + const YAML::Node boundary_condition_node = + boundary_conditions[iboundary_condition]; + boundaryConditionVector_.emplace_back( + bc_factory.load(boundary_condition_node)); } - } - else { + } else { throw std::runtime_error("parser error BoundaryConditions::load"); } } - } // namespace nalu -} // namespace Sierra +} // namespace sierra From 938e2e0deea0e4f9b9f43b2bb69bc8b87fe589d5 Mon Sep 17 00:00:00 2001 From: Philip Sakievich Date: Wed, 13 Jul 2022 00:14:04 -0600 Subject: [PATCH 4/6] Remove more stale code and update to modern c++ --- include/BoundaryConditions.h | 5 ----- src/BoundaryConditions.C | 33 ++++----------------------------- 2 files changed, 4 insertions(+), 34 deletions(-) diff --git a/include/BoundaryConditions.h b/include/BoundaryConditions.h index c6c428b7a..010187ef6 100644 --- a/include/BoundaryConditions.h +++ b/include/BoundaryConditions.h @@ -37,8 +37,6 @@ class BoundaryCondition { virtual ~BoundaryCondition() {} std::unique_ptr load(const YAML::Node& node); - Simulation *root(); - BoundaryConditions *parent(); std::string bcName_; std::string targetName_; @@ -59,9 +57,6 @@ class BoundaryConditions // TODO(psakiev) Delete this unused function void breadboard() {} - Simulation* root(); - Realm* parent(); - // ease of access methods to particular boundary condition size_t size() { return boundaryConditionVector_.size(); } BoundaryCondition* operator[](int i) diff --git a/src/BoundaryConditions.C b/src/BoundaryConditions.C index f6bd43f9a..d9572b5bd 100644 --- a/src/BoundaryConditions.C +++ b/src/BoundaryConditions.C @@ -123,28 +123,6 @@ BoundaryCondition::load(const YAML::Node& node) return this_bc; } -Simulation* -BoundaryCondition::root() -{ - return parent()->root(); -} -BoundaryConditions* -BoundaryCondition::parent() -{ - return &boundaryConditions_; -} - -Simulation* -BoundaryConditions::root() -{ - return parent()->root(); -} -Realm* -BoundaryConditions::parent() -{ - return &realm_; -} - void BoundaryConditions::load(const YAML::Node& node) { @@ -152,14 +130,11 @@ BoundaryConditions::load(const YAML::Node& node) if (node["boundary_conditions"]) { const YAML::Node boundary_conditions = node["boundary_conditions"]; - for (size_t iboundary_condition = 0; - iboundary_condition < boundary_conditions.size(); - ++iboundary_condition) { - const YAML::Node boundary_condition_node = - boundary_conditions[iboundary_condition]; - boundaryConditionVector_.emplace_back( - bc_factory.load(boundary_condition_node)); + + for (auto&& bc_node : boundary_conditions) { + boundaryConditionVector_.emplace_back(bc_factory.load(bc_node)); } + } else { throw std::runtime_error("parser error BoundaryConditions::load"); } From a2c1157ceb56a7d6aaa6c34bef45915bf2bb4891 Mon Sep 17 00:00:00 2001 From: Philip Sakievich Date: Wed, 13 Jul 2022 00:43:14 -0600 Subject: [PATCH 5/6] Remove more code --- include/BoundaryConditions.h | 4 ---- src/Realm.C | 12 +++++------- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/include/BoundaryConditions.h b/include/BoundaryConditions.h index 010187ef6..f8b180e4b 100644 --- a/include/BoundaryConditions.h +++ b/include/BoundaryConditions.h @@ -54,11 +54,7 @@ class BoundaryConditions void load(const YAML::Node& node); - // TODO(psakiev) Delete this unused function - void breadboard() {} - // ease of access methods to particular boundary condition - size_t size() { return boundaryConditionVector_.size(); } BoundaryCondition* operator[](int i) { return boundaryConditionVector_[i].get(); diff --git a/src/Realm.C b/src/Realm.C index ab9bbdb23..74fecbb39 100644 --- a/src/Realm.C +++ b/src/Realm.C @@ -1007,11 +1007,10 @@ void Realm::setup_bc() { // loop over all bcs and register - for (size_t ibc = 0; ibc < boundaryConditions_.size(); ++ibc) { - BoundaryCondition& bc = *boundaryConditions_[ibc]; - std::string name = physics_part_name(bc.targetName_); + for (auto&& bc : boundaryConditions_.boundaryConditionVector_) { + std::string name = physics_part_name(bc->targetName_); - switch(bc.theBcType_) { + switch(bc->theBcType_) { case WALL_BC: equationSystems_.register_wall_bc(name, *reinterpret_cast(&bc)); break; @@ -2314,9 +2313,8 @@ Realm::has_non_matching_boundary_face_alg() const bool Realm::query_for_overset() { - for (size_t ibc = 0; ibc < boundaryConditions_.size(); ++ibc) { - BoundaryCondition& bc = *boundaryConditions_[ibc]; - switch(bc.theBcType_) { + for (auto&& bc : boundaryConditions_.boundaryConditionVector_) { + switch(bc->theBcType_) { case OVERSET_BC: hasOverset_ = true; break; From c6773bd483eda90ddbb3f35baa6a0cf468e94911 Mon Sep 17 00:00:00 2001 From: Philip Sakievich Date: Wed, 13 Jul 2022 08:12:15 -0600 Subject: [PATCH 6/6] Remove BoundaryConditions class and use factory --- include/BoundaryConditions.h | 39 ++++++----------------- include/NaluParsing.h | 16 +++++----- include/Realm.h | 2 +- src/BoundaryConditions.C | 61 ++++++++++++------------------------ src/EquationSystem.C | 2 +- src/LowMachEquationSystem.C | 4 +-- src/Realm.C | 7 ++--- 7 files changed, 45 insertions(+), 86 deletions(-) diff --git a/include/BoundaryConditions.h b/include/BoundaryConditions.h index f8b180e4b..19e64992a 100644 --- a/include/BoundaryConditions.h +++ b/include/BoundaryConditions.h @@ -24,46 +24,27 @@ namespace YAML { } namespace sierra{ - namespace nalu{ - -class Realm; -class BoundaryConditions; -class Simulation; +namespace nalu { class BoundaryCondition { public: - BoundaryCondition(BoundaryConditions& bcs) : boundaryConditions_(bcs) {} - - virtual ~BoundaryCondition() {} - - std::unique_ptr load(const YAML::Node& node); + BoundaryCondition() {} + virtual ~BoundaryCondition() {} - std::string bcName_; - std::string targetName_; - BoundaryConditionType theBcType_; - BoundaryConditions& boundaryConditions_; + std::string bcName_; + std::string targetName_; + BoundaryConditionType theBcType_; }; typedef std::vector> BoundaryConditionVector; - -class BoundaryConditions +struct BoundaryConditionCreator { public: - BoundaryConditions(Realm& realm) : realm_(realm) {} - ~BoundaryConditions() = default; + BoundaryConditionVector create_bc_vector(const YAML::Node& node); - void load(const YAML::Node& node); - - // ease of access methods to particular boundary condition - BoundaryCondition* operator[](int i) - { - return boundaryConditionVector_[i].get(); - } - - Realm& realm_; - BoundaryConditionVector boundaryConditionVector_; + std::unique_ptr + load_single_bc_node(const YAML::Node& node); }; - } // namespace nalu } // namespace Sierra diff --git a/include/NaluParsing.h b/include/NaluParsing.h index 290f8f89a..2380ade4c 100644 --- a/include/NaluParsing.h +++ b/include/NaluParsing.h @@ -238,17 +238,17 @@ struct NonConformalUserData : public UserData { }; struct WallBoundaryConditionData : public BoundaryCondition { - WallBoundaryConditionData(BoundaryConditions& bcs) : BoundaryCondition(bcs){}; + WallBoundaryConditionData(){}; WallUserData userData_; }; struct InflowBoundaryConditionData : public BoundaryCondition { - InflowBoundaryConditionData(BoundaryConditions& bcs) : BoundaryCondition(bcs){}; + InflowBoundaryConditionData(){}; InflowUserData userData_; }; struct OpenBoundaryConditionData : public BoundaryCondition { - OpenBoundaryConditionData(BoundaryConditions& bcs) : BoundaryCondition(bcs){}; + OpenBoundaryConditionData(){}; OpenUserData userData_; }; @@ -258,30 +258,30 @@ struct OversetBoundaryConditionData : public BoundaryCondition { OVERSET_NONE = 1 ///< Guard for error messages }; - OversetBoundaryConditionData(BoundaryConditions& bcs) : BoundaryCondition(bcs){}; + OversetBoundaryConditionData(){}; OversetUserData userData_; OversetAPI oversetConnectivityType_; }; struct SymmetryBoundaryConditionData : public BoundaryCondition { - SymmetryBoundaryConditionData(BoundaryConditions& bcs) : BoundaryCondition(bcs){}; + SymmetryBoundaryConditionData(){}; SymmetryUserData userData_; }; struct ABLTopBoundaryConditionData : public BoundaryCondition { - ABLTopBoundaryConditionData(BoundaryConditions& bcs) : BoundaryCondition(bcs){}; + ABLTopBoundaryConditionData(){}; ABLTopUserData userData_; SymmetryUserData symmetryUserData_; }; struct PeriodicBoundaryConditionData : public BoundaryCondition { - PeriodicBoundaryConditionData(BoundaryConditions& bcs) : BoundaryCondition(bcs){}; + PeriodicBoundaryConditionData(){}; MasterSlave masterSlave_; PeriodicUserData userData_; }; struct NonConformalBoundaryConditionData : public BoundaryCondition { - NonConformalBoundaryConditionData(BoundaryConditions& bcs) : BoundaryCondition(bcs){}; + NonConformalBoundaryConditionData(){}; std::vector currentPartNameVec_; std::vector opposingPartNameVec_; NonConformalUserData userData_; diff --git a/include/Realm.h b/include/Realm.h index 79f9c96c3..a7257127a 100644 --- a/include/Realm.h +++ b/include/Realm.h @@ -432,7 +432,7 @@ class Realm { TimeIntegrator *timeIntegrator_; - BoundaryConditions boundaryConditions_; + BoundaryConditionVector boundaryConditions_; InitialConditions initialConditions_; MaterialPropertys materialPropertys_; diff --git a/src/BoundaryConditions.C b/src/BoundaryConditions.C index d9572b5bd..95d2ab9c0 100644 --- a/src/BoundaryConditions.C +++ b/src/BoundaryConditions.C @@ -7,7 +7,6 @@ // for more details. // -#include #include #include @@ -18,80 +17,59 @@ namespace sierra { namespace nalu { -//========================================================================== -// Class Definition -//========================================================================== -// BoundaryCondition - do some stuff -//========================================================================== -//-------------------------------------------------------------------------- -//-------- constructor ----------------------------------------------------- -//-------------------------------------------------------------------------- - -//-------------------------------------------------------------------------- -//-------- destructor ------------------------------------------------------ -//-------------------------------------------------------------------------- - -//-------------------------------------------------------------------------- -//-------- load ----------------------------------------------- -//-------------------------------------------------------------------------- - +// helper function for reducing code duplication in the construction process template std::unique_ptr -register_bc(const YAML::Node& node, BoundaryConditions& bcs) +register_bc(const YAML::Node& node) { - std::unique_ptr this_bc = std::make_unique(bcs); + std::unique_ptr this_bc = std::make_unique(); auto* cast_bc = dynamic_cast(this_bc.get()); node >> *cast_bc; return this_bc; } +// factory method to create any supported bc std::unique_ptr -BoundaryCondition::load(const YAML::Node& node) +BoundaryConditionCreator::load_single_bc_node(const YAML::Node& node) { std::unique_ptr this_bc; if (node["wall_boundary_condition"]) { - this_bc = std::move( - register_bc(node, boundaryConditions_)); + this_bc = std::move(register_bc(node)); NaluEnv::self().naluOutputP0() << "Wall BC name: " << this_bc->bcName_ << " on " << this_bc->targetName_ << std::endl; } else if (node["inflow_boundary_condition"]) { - this_bc = std::move( - register_bc(node, boundaryConditions_)); + this_bc = std::move(register_bc(node)); NaluEnv::self().naluOutputP0() << "Inflow BC name: " << this_bc->bcName_ << " on " << this_bc->targetName_ << std::endl; } else if (node["open_boundary_condition"]) { - this_bc = std::move( - register_bc(node, boundaryConditions_)); + this_bc = std::move(register_bc(node)); NaluEnv::self().naluOutputP0() << "Open BC name: " << this_bc->bcName_ << " on " << this_bc->targetName_ << std::endl; } else if (node["symmetry_boundary_condition"]) { - this_bc = std::move( - register_bc(node, boundaryConditions_)); + this_bc = std::move(register_bc(node)); NaluEnv::self().naluOutputP0() << "Symmetry BC name: " << this_bc->bcName_ << " on " << this_bc->targetName_ << std::endl; } else if (node["abltop_boundary_condition"]) { - this_bc = std::move( - register_bc(node, boundaryConditions_)); + this_bc = std::move(register_bc(node)); NaluEnv::self().naluOutputP0() << "ABLTop BC name: " << this_bc->bcName_ << " on " << this_bc->targetName_ << std::endl; } else if (node["periodic_boundary_condition"]) { - this_bc = std::move( - register_bc(node, boundaryConditions_)); + this_bc = std::move(register_bc(node)); auto* periodicBC = dynamic_cast(this_bc.get()); @@ -102,16 +80,14 @@ BoundaryCondition::load(const YAML::Node& node) << periodicBC->masterSlave_.slave_ << std::endl; } else if (node["non_conformal_boundary_condition"]) { - this_bc = std::move(register_bc( - node, boundaryConditions_)); + this_bc = std::move(register_bc(node)); NaluEnv::self().naluOutputP0() << "NonConformal BC name: " << this_bc->bcName_ << " using " << this_bc->targetName_ << std::endl; } else if (node["overset_boundary_condition"]) { - this_bc = std::move( - register_bc(node, boundaryConditions_)); + this_bc = std::move(register_bc(node)); NaluEnv::self().naluOutputP0() << "Overset BC name: " << this_bc->bcName_ << std::endl; @@ -123,21 +99,24 @@ BoundaryCondition::load(const YAML::Node& node) return this_bc; } -void -BoundaryConditions::load(const YAML::Node& node) +// convenience function to create a vector of bc's contianed in a single yaml +// node +BoundaryConditionVector +BoundaryConditionCreator::create_bc_vector(const YAML::Node& node) { - BoundaryCondition bc_factory(*this); + BoundaryConditionVector bc_vector; if (node["boundary_conditions"]) { const YAML::Node boundary_conditions = node["boundary_conditions"]; for (auto&& bc_node : boundary_conditions) { - boundaryConditionVector_.emplace_back(bc_factory.load(bc_node)); + bc_vector.emplace_back(load_single_bc_node(bc_node)); } } else { throw std::runtime_error("parser error BoundaryConditions::load"); } + return bc_vector; } } // namespace nalu diff --git a/src/EquationSystem.C b/src/EquationSystem.C index 077e03736..5b2e8a32c 100644 --- a/src/EquationSystem.C +++ b/src/EquationSystem.C @@ -562,7 +562,7 @@ void EquationSystem::register_abltop_bc( const stk::topology &theTopo, const ABLTopBoundaryConditionData &abltopBCData) { - SymmetryBoundaryConditionData simData(abltopBCData.boundaryConditions_); + SymmetryBoundaryConditionData simData; register_symmetry_bc( part, theTopo, simData ); } diff --git a/src/LowMachEquationSystem.C b/src/LowMachEquationSystem.C index c21f8ce3f..d677d2aea 100644 --- a/src/LowMachEquationSystem.C +++ b/src/LowMachEquationSystem.C @@ -2167,7 +2167,7 @@ MomentumEquationSystem::register_abltop_bc( auto userData = abltopBCData.userData_; if (!userData.ABLTopBC_) { - SymmetryBoundaryConditionData symData(abltopBCData.boundaryConditions_); + SymmetryBoundaryConditionData symData; symData.userData_ = abltopBCData.symmetryUserData_; register_symmetry_bc(part, partTopo, symData); return; @@ -3176,7 +3176,7 @@ ContinuityEquationSystem::register_abltop_bc( auto userData = abltopBCData.userData_; if (!userData.ABLTopBC_) { - SymmetryBoundaryConditionData symData(abltopBCData.boundaryConditions_); + SymmetryBoundaryConditionData symData; register_symmetry_bc(part, partTopo, symData); return; } diff --git a/src/Realm.C b/src/Realm.C index 74fecbb39..66a8cc7c8 100644 --- a/src/Realm.C +++ b/src/Realm.C @@ -190,7 +190,6 @@ Realm::Realm(Realms& realms, const YAML::Node& node) restartFileIndex_(99), numInitialElements_(0), timeIntegrator_(0), - boundaryConditions_(*this), initialConditions_(*this), materialPropertys_(*this), equationSystems_(*this), @@ -769,7 +768,7 @@ Realm::load(const YAML::Node & node) NaluEnv::self().naluOutputP0() << std::endl; NaluEnv::self().naluOutputP0() << "Boundary Condition Review: " << std::endl; NaluEnv::self().naluOutputP0() << "===========================" << std::endl; - boundaryConditions_.load(node); + boundaryConditions_ = BoundaryConditionCreator().create_bc_vector(node); NaluEnv::self().naluOutputP0() << std::endl; NaluEnv::self().naluOutputP0() << "Initial Condition Review: " << std::endl; NaluEnv::self().naluOutputP0() << "===========================" << std::endl; @@ -1007,7 +1006,7 @@ void Realm::setup_bc() { // loop over all bcs and register - for (auto&& bc : boundaryConditions_.boundaryConditionVector_) { + for (auto&& bc : boundaryConditions_) { std::string name = physics_part_name(bc->targetName_); switch(bc->theBcType_) { @@ -2313,7 +2312,7 @@ Realm::has_non_matching_boundary_face_alg() const bool Realm::query_for_overset() { - for (auto&& bc : boundaryConditions_.boundaryConditionVector_) { + for (auto&& bc : boundaryConditions_) { switch(bc->theBcType_) { case OVERSET_BC: hasOverset_ = true;