1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
|
/* Copyright (c) 2020, Dyssol Development Team.
* Copyright (c) 2024, DyssolTEC GmbH.
* All rights reserved. This file is part of Dyssol. See LICENSE file for license information. */
#include "StateVariable.h"
#include "ContainerFunctions.h"
#include "DyssolStringConstants.h"
#include "DyssolUtilities.h"
#include "H5Handler.h"
////////////////////////////////////////////////////////////////////////////////
// CStateVariable
//
CStateVariable::CStateVariable(std::string _name, double _initValue) :
m_name{ std::move(_name) },
m_value{ _initValue },
m_valueStored{ 0.0 }
{
}
void CStateVariable::Clear()
{
m_value = 0.0;
m_valueStored = 0.0;
m_history.clear();
}
std::string CStateVariable::GetName() const
{
return m_name;
}
void CStateVariable::SetName(const std::string& _name)
{
m_name = _name;
}
double CStateVariable::GetValue() const
{
return m_value;
}
void CStateVariable::SetValue(double _value)
{
m_value = _value;
}
void CStateVariable::SetValue(double _time, double _value)
{
SetValue(_value);
AddToHistory(_time, _value);
}
void CStateVariable::SaveState()
{
m_valueStored = m_value;
}
void CStateVariable::LoadState()
{
m_value = m_valueStored;
}
bool CStateVariable::HasHistory() const
{
return m_history.size() > 1;
}
std::vector<STDValue> CStateVariable::GetHistory() const
{
return m_history;
}
double CStateVariable::GetHistoryValue(double _time) const
{
if (!HasHistory()) return {};
return Interpolate(m_history, _time);
}
void CStateVariable::SetHistory(const std::vector<STDValue>& _history)
{
m_history = _history;
}
void CStateVariable::SaveToFile(CH5Handler& _h5File, const std::string& _path) const
{
if (!_h5File.IsValid()) return;
// current version of save procedure
_h5File.WriteAttribute(_path, StrConst::H5AttrSaveVersion, m_saveVersion);
_h5File.WriteData(_path, StrConst::SVar_H5Name, m_name);
_h5File.WriteData(_path, StrConst::SVar_H5Value, m_value);
_h5File.WriteData(_path, StrConst::SVar_H5History, m_history);
}
void CStateVariable::LoadFromFile(CH5Handler& _h5File, const std::string& _path)
{
if (!_h5File.IsValid()) return;
// current version of save procedure
//const int version = _h5File.ReadAttribute(_path, StrConst::H5AttrSaveVersion);
_h5File.ReadData(_path, StrConst::SVar_H5Name, m_name);
_h5File.ReadData(_path, StrConst::SVar_H5Value, m_value);
_h5File.ReadData(_path, StrConst::SVar_H5History, m_history);
}
void CStateVariable::AddToHistory(double _time, double _value)
{
if (_time < 0) return;
if (m_history.empty() || m_history.back().time < _time) // time is larger as all already stored
m_history.emplace_back(_time , _value); // add it to the end
else if (std::abs(m_history.back().time - _time) <= m_eps) // this time is the last stored
m_history.back() = { _time, _value }; // replace it
else // there are larger time points
{
// clear larger time points
m_history.erase(std::lower_bound(m_history.begin(), m_history.end(), STDValue{ _time, 0.0 }), m_history.end());
// add value
m_history.emplace_back(_time, _value);
}
}
////////////////////////////////////////////////////////////////////////////////
// CStateVariablesManager
//
CStateVariablesManager::CStateVariablesManager(const CStateVariablesManager& _other)
: m_stateVariables{ DeepCopy(_other.m_stateVariables) }
{
}
CStateVariablesManager::CStateVariablesManager(CStateVariablesManager&& _other) noexcept
{
swap(*this, _other);
}
CStateVariablesManager& CStateVariablesManager::operator=(CStateVariablesManager _other)
{
swap(*this, _other);
return *this;
}
CStateVariablesManager& CStateVariablesManager::operator=(CStateVariablesManager&& _other) noexcept
{
CStateVariablesManager tmp{ std::move(_other) };
swap(*this, tmp);
return *this;
}
void swap(CStateVariablesManager& _first, CStateVariablesManager& _second) noexcept
{
using std::swap;
swap(_first.m_stateVariables, _second.m_stateVariables);
}
CStateVariable* CStateVariablesManager::AddStateVariable(const std::string& _name, double _initValue)
{
if (GetStateVariable(_name)) return nullptr;
m_stateVariables.emplace_back(new CStateVariable{ _name, _initValue });
return m_stateVariables.back().get();
}
const CStateVariable* CStateVariablesManager::GetStateVariable(size_t _index) const
{
if (_index >= m_stateVariables.size()) return {};
return m_stateVariables[_index].get();
}
CStateVariable* CStateVariablesManager::GetStateVariable(size_t _index)
{
return const_cast<CStateVariable*>(const_cast<const CStateVariablesManager&>(*this).GetStateVariable(_index));
}
const CStateVariable* CStateVariablesManager::GetStateVariable(const std::string& _name) const
{
for (const auto& v : m_stateVariables)
if (v->GetName() == _name)
return v.get();
return nullptr;
}
CStateVariable* CStateVariablesManager::GetStateVariable(const std::string& _name)
{
return const_cast<CStateVariable*>(const_cast<const CStateVariablesManager&>(*this).GetStateVariable(_name));
}
std::vector<const CStateVariable*> CStateVariablesManager::GetAllStateVariables() const
{
std::vector<const CStateVariable*> res;
for (const auto& v : m_stateVariables)
res.push_back(v.get());
return res;
}
std::vector<CStateVariable*> CStateVariablesManager::GetAllStateVariables()
{
std::vector<CStateVariable*> res;
for (auto& v : m_stateVariables)
res.push_back(v.get());
return res;
}
std::vector<const CStateVariable*> CStateVariablesManager::GetAllStateVariablesWithHistory() const
{
std::vector<const CStateVariable*> res;
for (const auto& v : m_stateVariables)
if (v->HasHistory())
res.push_back(v.get());
return res;
}
std::vector<CStateVariable*> CStateVariablesManager::GetAllStateVariablesWithHistory()
{
std::vector<CStateVariable*> res;
for (const auto& v : m_stateVariables)
if (v->HasHistory())
res.push_back(v.get());
return res;
}
size_t CStateVariablesManager::GetStateVariablesNumber() const
{
return m_stateVariables.size();
}
void CStateVariablesManager::ClearData()
{
for (auto& v : m_stateVariables)
v->Clear();
}
void CStateVariablesManager::Clear()
{
m_stateVariables.clear();
}
void CStateVariablesManager::SaveState()
{
for (auto& var : m_stateVariables)
var->SaveState();
}
void CStateVariablesManager::LoadState()
{
for (auto& var : m_stateVariables)
var->LoadState();
}
void CStateVariablesManager::SaveToFile(CH5Handler& _h5File, const std::string& _path) const
{
if (!_h5File.IsValid()) return;
// current version of save procedure
_h5File.WriteAttribute(_path, StrConst::H5AttrSaveVersion, m_saveVersion);
_h5File.WriteAttribute(_path, StrConst::SVMngr_H5AttrStateVarsNum, static_cast<int>(m_stateVariables.size()));
for (size_t i = 0; i < m_stateVariables.size(); ++i)
{
const std::string variablePath = _h5File.CreateGroup(_path, StrConst::SVMngr_H5GroupStateVarName + std::to_string(i));
m_stateVariables[i]->SaveToFile(_h5File, variablePath);
}
}
void CStateVariablesManager::LoadFromFile(CH5Handler& _h5File, const std::string& _path)
{
Clear();
if (!_h5File.IsValid()) return;
// current version of save procedure
//const int version = _h5File.ReadAttribute(_path, StrConst::H5AttrSaveVersion);
const size_t nVariables = _h5File.ReadAttribute(_path, StrConst::SVMngr_H5AttrStateVarsNum);
for (size_t i = 0; i < nVariables; ++i)
{
const std::string variablePath = _path + "/" + StrConst::SVMngr_H5GroupStateVarName + std::to_string(i);
AddStateVariable("", {})->LoadFromFile(_h5File, variablePath);
}
}
void CStateVariablesManager::LoadFromFile_v0(const CH5Handler& _h5File, const std::string& _path)
{
Clear();
if (!_h5File.IsValid()) return;
const size_t nVariables = _h5File.ReadAttribute(_path, StrConst::SVMngr_H5AttrStateVarsNum);
if (nVariables == static_cast<size_t>(-1)) return;
for (size_t i = 0; i < nVariables; ++i)
{
std::string variablePath = _path + "/" + StrConst::BUnit_H5GroupStateVars + "/" + StrConst::SVMngr_H5GroupStateVarName + std::to_string(i);
auto* variable = AddStateVariable("", {});
std::string name;
_h5File.ReadData(variablePath, StrConst::SVar_H5Name, name);
variable->SetName(name);
double value;
_h5File.ReadData(variablePath, StrConst::SVar_H5Value, value);
variable->SetValue(value);
bool hasHistory;
_h5File.ReadData(variablePath, StrConst::BUnit_H5StateVarIsSaved, hasHistory);
if (hasHistory)
{
std::vector<double> times, values;
_h5File.ReadData(variablePath, StrConst::BUnit_H5StateVarTimes, times);
_h5File.ReadData(variablePath, StrConst::BUnit_H5StateVarValues, values);
if (!times.empty() && times.size() == values.size())
{
std::vector<STDValue> data(times.size());
for (size_t i = 0; i < data.size(); ++i)
data[i] = { times[i], values[i] };
variable->SetHistory(data);
}
}
}
}
|