Skip to content
Snippets Groups Projects
Commit f5062e82 authored by Joan Solà Ortega's avatar Joan Solà Ortega
Browse files

Use StateBlockComposite in HasStateBlocks

parent 5aeca3f2
No related branches found
No related tags found
2 merge requests!358WIP: Resolve "Complete state vector new data structure?",!343WIP: Resolve "Complete state vector new data structure?"
Pipeline #5136 passed
...@@ -21,15 +21,15 @@ class HasStateBlocks ...@@ -21,15 +21,15 @@ class HasStateBlocks
{ {
public: public:
HasStateBlocks(); HasStateBlocks();
HasStateBlocks(const StateStructure& _structure) : structure_(_structure), state_block_map_() {} HasStateBlocks(const StateStructure& _structure) : structure_(_structure), state_block_composite_() {}
virtual ~HasStateBlocks(); virtual ~HasStateBlocks();
const StateStructure& getStructure() const { return structure_; } const StateStructure& getStructure() const { return structure_; }
void appendToStructure(const std::string& _frame_type){ structure_ += _frame_type; } void appendToStructure(const std::string& _frame_type){ structure_ += _frame_type; }
bool isInStructure(const std::string& _sb_type) { return structure_.find(_sb_type) != std::string::npos; } bool isInStructure(const std::string& _sb_type) { return structure_.find(_sb_type) != std::string::npos; }
const StateBlockMap& getStateBlockMap() const; const StateBlockMap& getStateBlockMap() const;
std::vector<StateBlockPtr> getStateBlockVec() const; std::vector<StateBlockPtr> getStateBlockVec() const;
// Some typical shortcuts -- not all should be coded here, see notes below. // Some typical shortcuts -- not all should be coded here, see notes below.
StateBlockPtr getP() const; StateBlockPtr getP() const;
...@@ -44,7 +44,7 @@ class HasStateBlocks ...@@ -44,7 +44,7 @@ class HasStateBlocks
virtual StateBlockPtr addStateBlock(const char _sb_type, const StateBlockPtr& _sb, ProblemPtr _problem) { return addStateBlock(std::string(1,_sb_type), _sb, _problem); } virtual StateBlockPtr addStateBlock(const char _sb_type, const StateBlockPtr& _sb, ProblemPtr _problem) { return addStateBlock(std::string(1,_sb_type), _sb, _problem); }
virtual unsigned int removeStateBlock(const std::string& _sb_type); virtual unsigned int removeStateBlock(const std::string& _sb_type);
virtual unsigned int removeStateBlock(const char _sb_type); virtual unsigned int removeStateBlock(const char _sb_type);
bool hasStateBlock(const std::string& _sb_type) const { return state_block_map_.count(_sb_type) > 0; } bool hasStateBlock(const std::string& _sb_type) const { return state_block_composite_.has(_sb_type); }
bool hasStateBlock(const char _sb_type) const { return hasStateBlock(std::string(1, _sb_type)); } bool hasStateBlock(const char _sb_type) const { return hasStateBlock(std::string(1, _sb_type)); }
bool hasStateBlock(const StateBlockPtr& _sb) const; bool hasStateBlock(const StateBlockPtr& _sb) const;
StateBlockPtr getStateBlock(const std::string& _sb_type) const; StateBlockPtr getStateBlock(const std::string& _sb_type) const;
...@@ -77,6 +77,9 @@ class HasStateBlocks ...@@ -77,6 +77,9 @@ class HasStateBlocks
bool getVectorComposite(VectorComposite& _state) const; bool getVectorComposite(VectorComposite& _state) const;
void setVectorComposite(const VectorComposite& _state); void setVectorComposite(const VectorComposite& _state);
StateBlockComposite& getStateBlockComposite();
const StateBlockComposite& getStateBlockComposite() const;
// Perturb state with noise // Perturb state with noise
void perturb(double amplitude = 0.01); void perturb(double amplitude = 0.01);
...@@ -85,8 +88,8 @@ class HasStateBlocks ...@@ -85,8 +88,8 @@ class HasStateBlocks
private: private:
StateStructure structure_; // TBR StateStructure structure_; // TODO: remove?
StateBlockMap state_block_map_; StateBlockComposite state_block_composite_;
}; };
...@@ -101,7 +104,8 @@ namespace wolf{ ...@@ -101,7 +104,8 @@ namespace wolf{
inline HasStateBlocks::HasStateBlocks() : inline HasStateBlocks::HasStateBlocks() :
structure_(), structure_(),
state_block_map_() // state_block_map_(),
state_block_composite_()
{ {
// //
} }
...@@ -113,7 +117,7 @@ inline HasStateBlocks::~HasStateBlocks() ...@@ -113,7 +117,7 @@ inline HasStateBlocks::~HasStateBlocks()
inline const StateBlockMap& HasStateBlocks::getStateBlockMap() const inline const StateBlockMap& HasStateBlocks::getStateBlockMap() const
{ {
return state_block_map_; return state_block_composite_.getStateBlockMap();
} }
inline std::vector<StateBlockPtr> HasStateBlocks::getStateBlockVec() const inline std::vector<StateBlockPtr> HasStateBlocks::getStateBlockVec() const
...@@ -133,13 +137,15 @@ inline unsigned int HasStateBlocks::removeStateBlock(const char _sb_type) ...@@ -133,13 +137,15 @@ inline unsigned int HasStateBlocks::removeStateBlock(const char _sb_type)
inline unsigned int HasStateBlocks::removeStateBlock(const std::string& _sb_type) inline unsigned int HasStateBlocks::removeStateBlock(const std::string& _sb_type)
{ {
return state_block_map_.erase(_sb_type); return state_block_composite_.remove(_sb_type);
} }
template<typename SB, typename ... Args> template<typename SB, typename ... Args>
inline std::shared_ptr<SB> HasStateBlocks::emplaceStateBlock(const std::string& _sb_type, ProblemPtr _problem, Args&&... _args_of_derived_state_block_constructor) inline std::shared_ptr<SB> HasStateBlocks::emplaceStateBlock(const std::string& _sb_type, ProblemPtr _problem, Args&&... _args_of_derived_state_block_constructor)
{ {
assert(state_block_map_.count(_sb_type) == 0 && "Trying to add a state block with an existing type! Use setStateBlock instead."); assert(state_block_composite_.has(_sb_type) == false && "Trying to add a state block with an existing type! Use setStateBlock instead.");
// assert(state_block_map_.count(_sb_type) == 0 && "Trying to add a state block with an existing type! Use setStateBlock instead.");
std::shared_ptr<SB> sb = std::make_shared<SB>(std::forward<Args>(_args_of_derived_state_block_constructor)...); std::shared_ptr<SB> sb = std::make_shared<SB>(std::forward<Args>(_args_of_derived_state_block_constructor)...);
addStateBlock(_sb_type, sb, _problem); addStateBlock(_sb_type, sb, _problem);
...@@ -150,7 +156,8 @@ inline std::shared_ptr<SB> HasStateBlocks::emplaceStateBlock(const std::string& ...@@ -150,7 +156,8 @@ inline std::shared_ptr<SB> HasStateBlocks::emplaceStateBlock(const std::string&
template<typename ... Args> template<typename ... Args>
inline StateBlockPtr HasStateBlocks::emplaceStateBlock(const std::string& _sb_type, ProblemPtr _problem, Args&&... _args_of_base_state_block_constructor) inline StateBlockPtr HasStateBlocks::emplaceStateBlock(const std::string& _sb_type, ProblemPtr _problem, Args&&... _args_of_base_state_block_constructor)
{ {
assert(state_block_map_.count(_sb_type) == 0 && "Trying to add a state block with an existing type! Use setStateBlock instead."); assert(state_block_composite_.has(_sb_type) == false && "Trying to add a state block with an existing type! Use setStateBlock instead.");
// assert(state_block_map_.count(_sb_type) == 0 && "Trying to add a state block with an existing type! Use setStateBlock instead.");
auto sb = std::make_shared<StateBlock>(std::forward<Args>(_args_of_base_state_block_constructor)...); auto sb = std::make_shared<StateBlock>(std::forward<Args>(_args_of_base_state_block_constructor)...);
addStateBlock(_sb_type, sb, _problem); addStateBlock(_sb_type, sb, _problem);
...@@ -161,18 +168,14 @@ inline StateBlockPtr HasStateBlocks::emplaceStateBlock(const std::string& _sb_ty ...@@ -161,18 +168,14 @@ inline StateBlockPtr HasStateBlocks::emplaceStateBlock(const std::string& _sb_ty
inline bool HasStateBlocks::setStateBlock(const std::string _sb_type, const StateBlockPtr& _sb) inline bool HasStateBlocks::setStateBlock(const std::string _sb_type, const StateBlockPtr& _sb)
{ {
assert (structure_.find(_sb_type) != std::string::npos && "Cannot set a state block out of the state structure! Use addStateBlock instead."); assert (structure_.find(_sb_type) != std::string::npos && "Cannot set a state block out of the state structure! Use addStateBlock instead.");
assert ( state_block_map_.count(_sb_type) > 0 && "Cannot set an inexistent state block! Use addStateBlock instead."); assert ( state_block_composite_.has(_sb_type) && "Cannot set an inexistent state block! Use addStateBlock instead.");
state_block_map_.at(_sb_type) = _sb; state_block_composite_.set(_sb_type, _sb);
return true; // success return true; // success
} }
inline wolf::StateBlockPtr HasStateBlocks::getStateBlock(const std::string& _sb_type) const inline wolf::StateBlockPtr HasStateBlocks::getStateBlock(const std::string& _sb_type) const
{ {
auto it = state_block_map_.find(_sb_type); return state_block_composite_.at(_sb_type);
if (it != state_block_map_.end())
return it->second;
else
return nullptr;
} }
inline wolf::StateBlockPtr HasStateBlocks::getP() const inline wolf::StateBlockPtr HasStateBlocks::getP() const
...@@ -187,27 +190,17 @@ inline wolf::StateBlockPtr HasStateBlocks::getO() const ...@@ -187,27 +190,17 @@ inline wolf::StateBlockPtr HasStateBlocks::getO() const
inline void HasStateBlocks::fix() inline void HasStateBlocks::fix()
{ {
for (auto pair_key_sbp : state_block_map_) state_block_composite_.fix();
if (pair_key_sbp.second != nullptr)
pair_key_sbp.second->fix();
} }
inline void HasStateBlocks::unfix() inline void HasStateBlocks::unfix()
{ {
for (auto pair_key_sbp : state_block_map_) state_block_composite_.unfix();
if (pair_key_sbp.second != nullptr)
pair_key_sbp.second->unfix();
} }
inline bool HasStateBlocks::isFixed() const inline bool HasStateBlocks::isFixed() const
{ {
bool fixed = true; return state_block_composite_.isFixed();
for (auto pair_key_sbp : state_block_map_)
{
if (pair_key_sbp.second)
fixed &= pair_key_sbp.second->isFixed();
}
return fixed;
} }
inline void HasStateBlocks::setState(const Eigen::VectorXd& _state, const StateStructure& _sub_structure, const bool _notify) inline void HasStateBlocks::setState(const Eigen::VectorXd& _state, const StateStructure& _sub_structure, const bool _notify)
...@@ -268,40 +261,17 @@ inline Eigen::VectorXd HasStateBlocks::getState(const StateStructure& _sub_struc ...@@ -268,40 +261,17 @@ inline Eigen::VectorXd HasStateBlocks::getState(const StateStructure& _sub_struc
inline StateBlockMap::const_iterator HasStateBlocks::find(const StateBlockPtr& _sb) const inline StateBlockMap::const_iterator HasStateBlocks::find(const StateBlockPtr& _sb) const
{ {
const auto& it = std::find_if(state_block_map_.begin(), return state_block_composite_.find(_sb);
state_block_map_.end(),
[_sb](const std::pair<std::string, StateBlockPtr>& pair)
{
return pair.second == _sb;
}
);
return it;
} }
inline bool HasStateBlocks::hasStateBlock(const StateBlockPtr &_sb) const inline bool HasStateBlocks::hasStateBlock(const StateBlockPtr &_sb) const
{ {
const auto& it = this->find(_sb); return state_block_composite_.has(_sb);
return it != state_block_map_.end();
} }
inline bool HasStateBlocks::stateBlockKey(const StateBlockPtr &_sb, std::string& _key) const inline bool HasStateBlocks::stateBlockKey(const StateBlockPtr &_sb, std::string& _key) const
{ {
const auto& it = this->find(_sb); return state_block_composite_.key(_sb, _key);
bool found = (it != state_block_map_.end());
if (found)
{
_key = it->first;
return true;
}
else
{
_key = "";
return false;
}
} }
inline unsigned int HasStateBlocks::getSize(const StateStructure& _sub_structure) const inline unsigned int HasStateBlocks::getSize(const StateStructure& _sub_structure) const
...@@ -344,6 +314,16 @@ inline unsigned int HasStateBlocks::getLocalSize(const StateStructure& _sub_stru ...@@ -344,6 +314,16 @@ inline unsigned int HasStateBlocks::getLocalSize(const StateStructure& _sub_stru
return size; return size;
} }
inline StateBlockComposite& HasStateBlocks::getStateBlockComposite()
{
return state_block_composite_;
}
inline const StateBlockComposite& HasStateBlocks::getStateBlockComposite() const
{
return state_block_composite_;
}
} // namespace wolf } // namespace wolf
#endif /* STATE_BLOCK_HAS_STATE_BLOCKS_H_ */ #endif /* STATE_BLOCK_HAS_STATE_BLOCKS_H_ */
...@@ -6,8 +6,12 @@ namespace wolf ...@@ -6,8 +6,12 @@ namespace wolf
StateBlockPtr HasStateBlocks::addStateBlock(const std::string& _sb_type, const StateBlockPtr& _sb, ProblemPtr _problem) StateBlockPtr HasStateBlocks::addStateBlock(const std::string& _sb_type, const StateBlockPtr& _sb, ProblemPtr _problem)
{ {
assert(state_block_map_.count(_sb_type) == 0 && "Trying to add a state block with an existing type! Use setStateBlock instead."); assert(state_block_composite_.has(_sb) == false && "Trying to add a state block that already exists!");
state_block_map_.emplace(_sb_type, _sb);
assert(state_block_composite_.has(_sb_type) == false && "Trying to add a state block with an existing type! Use setStateBlock instead.");
state_block_composite_.add(_sb_type, _sb);
if (!isInStructure(_sb_type)) if (!isInStructure(_sb_type))
appendToStructure(_sb_type); appendToStructure(_sb_type);
...@@ -46,7 +50,7 @@ void HasStateBlocks::removeStateBlocks(ProblemPtr _problem) ...@@ -46,7 +50,7 @@ void HasStateBlocks::removeStateBlocks(ProblemPtr _problem)
bool HasStateBlocks::getVectorComposite(VectorComposite &_state) const bool HasStateBlocks::getVectorComposite(VectorComposite &_state) const
{ {
for (auto &pair_key_sb : state_block_map_) for (auto &pair_key_sb : state_block_composite_.getStateBlockMap())
{ {
_state.emplace(pair_key_sb.first, pair_key_sb.second->getState()); _state.emplace(pair_key_sb.first, pair_key_sb.second->getState());
} }
...@@ -64,13 +68,16 @@ void HasStateBlocks::setVectorComposite(const VectorComposite &_state) ...@@ -64,13 +68,16 @@ void HasStateBlocks::setVectorComposite(const VectorComposite &_state)
{ {
for (const auto &pair_key_sb : _state) for (const auto &pair_key_sb : _state)
{ {
state_block_map_[pair_key_sb.first]->setState(pair_key_sb.second); const auto& sb = state_block_composite_.at(pair_key_sb.first);
if(sb)
sb->setState(pair_key_sb.second);
} }
} }
void HasStateBlocks::perturb(double amplitude) void HasStateBlocks::perturb(double amplitude)
{ {
for (const auto& pair_key_sb : state_block_map_) for (const auto& pair_key_sb : state_block_composite_.getStateBlockMap())
{ {
auto& sb = pair_key_sb.second; auto& sb = pair_key_sb.second;
if (!sb->isFixed()) if (!sb->isFixed())
...@@ -80,7 +87,7 @@ void HasStateBlocks::perturb(double amplitude) ...@@ -80,7 +87,7 @@ void HasStateBlocks::perturb(double amplitude)
void HasStateBlocks::plus(const VectorComposite& _dx) void HasStateBlocks::plus(const VectorComposite& _dx)
{ {
for (const auto& pair_key_sb : state_block_map_) for (const auto& pair_key_sb : state_block_composite_.getStateBlockMap())
{ {
auto& sb = pair_key_sb.second; auto& sb = pair_key_sb.second;
auto& dv = _dx.at(pair_key_sb.first); auto& dv = _dx.at(pair_key_sb.first);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment