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
|
/* Copyright (c) 2021, Dyssol Development Team.
* Copyright (c) 2023, DyssolTEC GmbH.
* All rights reserved. This file is part of Dyssol. See LICENSE file for license information. */
#include "ChemicalReaction.h"
#include "MaterialsDatabase.h"
#include "H5Handler.h"
#include "ContainerFunctions.h"
#include "DyssolStringConstants.h"
#include "DyssolUtilities.h"
#include <memory>
CChemicalReaction::CChemicalReaction(const CChemicalReaction& _other)
: m_name{ _other.m_name }
, m_enthalpy{ _other.m_enthalpy }
, m_iBase{_other.m_iBase}
{
m_substances.clear();
m_substances.reserve(_other.m_substances.size());
for (const auto& s : _other.m_substances)
m_substances.emplace_back(new SChemicalSubstance{ *s });
}
CChemicalReaction& CChemicalReaction::operator=(const CChemicalReaction& _other)
{
CChemicalReaction temp{ _other };
Swap(temp);
return *this;
}
void CChemicalReaction::Swap(CChemicalReaction& _other) noexcept
{
std::swap(m_name , _other.m_name);
std::swap(m_enthalpy , _other.m_enthalpy);
std::swap(m_iBase , _other.m_iBase);
std::swap(m_substances, _other.m_substances);
}
bool CChemicalReaction::operator==(const CChemicalReaction& _other) const
{
if (m_name != _other.m_name) return false;
if (m_enthalpy != _other.m_enthalpy) return false;
if (m_iBase != _other.m_iBase) return false;
if (m_substances.size() != _other.m_substances.size()) return false;
for (size_t i = 0; i < m_substances.size(); ++i)
if (*m_substances[i] != *_other.m_substances[i])
return false;
return true;
}
void CChemicalReaction::SetName(const std::string& _name)
{
m_name = _name;
}
std::string CChemicalReaction::GetName() const
{
return m_name;
}
CChemicalReaction::SChemicalSubstance* CChemicalReaction::AddSubstance()
{
return m_substances.emplace_back().get();
}
void CChemicalReaction::AddSubstance(const SChemicalSubstance& _substance)
{
m_substances.emplace_back(new SChemicalSubstance{ _substance });
}
void CChemicalReaction::RemoveSubstance(size_t _index)
{
if (_index >= m_substances.size()) return;
m_substances.erase(m_substances.begin() + _index);
}
std::vector<const CChemicalReaction::SChemicalSubstance*> CChemicalReaction::GetSubstances() const
{
auto res = ReservedVector<const SChemicalSubstance*>(m_substances.size());
for (const auto& s : m_substances)
res.emplace_back(s.get());
return res;
}
std::vector<CChemicalReaction::SChemicalSubstance*> CChemicalReaction::GetSubstances()
{
auto res = ReservedVector<SChemicalSubstance*>(m_substances.size());
for (auto& s : m_substances)
res.emplace_back(s.get());
return res;
}
size_t CChemicalReaction::GetSubstancesNumber() const
{
return m_substances.size();
}
std::vector<const CChemicalReaction::SChemicalSubstance*> CChemicalReaction::GetSubstances(EPhase _phase) const
{
auto res = ReservedVector<const SChemicalSubstance*>(m_substances.size());
for (const auto& s : m_substances)
if (s->phase == _phase)
res.emplace_back(s.get());
return res;
}
std::vector<CChemicalReaction::SChemicalSubstance*> CChemicalReaction::GetSubstances(EPhase _phase)
{
auto res = ReservedVector<SChemicalSubstance*>(m_substances.size());
for (auto& s : m_substances)
if (s->phase == _phase)
res.emplace_back(s.get());
return res;
}
std::vector<const CChemicalReaction::SChemicalSubstance*> CChemicalReaction::GetSubstances(ESubstance _type) const
{
auto res = ReservedVector<const SChemicalSubstance*>(m_substances.size());
for (const auto& s : m_substances)
if (_type == s->GetType())
res.emplace_back(s.get());
return res;
}
std::vector<CChemicalReaction::SChemicalSubstance*> CChemicalReaction::GetSubstances(ESubstance _type)
{
auto res = ReservedVector<SChemicalSubstance*>(m_substances.size());
for (auto& s : m_substances)
if (_type == s->GetType())
res.emplace_back(s.get());
return res;
}
std::vector<const CChemicalReaction::SChemicalSubstance*> CChemicalReaction::GetSubstances(EPhase _phase, ESubstance _type) const
{
auto res = ReservedVector<const SChemicalSubstance*>(m_substances.size());
for (const auto& s : m_substances)
if (s->phase == _phase && _type == s->GetType())
res.emplace_back(s.get());
return res;
}
std::vector<CChemicalReaction::SChemicalSubstance*> CChemicalReaction::GetSubstances(EPhase _phase, ESubstance _type)
{
auto res = ReservedVector<SChemicalSubstance*>(m_substances.size());
for (auto& s : m_substances)
if (s->phase == _phase && _type == s->GetType())
res.emplace_back(s.get());
return res;
}
void CChemicalReaction::SetBaseSubstance(size_t _index)
{
if (_index >= m_substances.size()) return;
m_iBase = _index;
}
const CChemicalReaction::SChemicalSubstance* CChemicalReaction::GetBaseSubstance() const
{
if (m_iBase >= m_substances.size()) return nullptr;
return m_substances[m_iBase].get();
}
CChemicalReaction::SChemicalSubstance* CChemicalReaction::GetBaseSubstance()
{
if (m_iBase >= m_substances.size()) return nullptr;
return m_substances[m_iBase].get();
}
size_t CChemicalReaction::GetBaseSubstanceIndex() const
{
return m_iBase;
}
void CChemicalReaction::SetEnthalpy(double _enthalpy)
{
m_enthalpy = _enthalpy;
}
double CChemicalReaction::GetEnthalpy() const
{
return m_enthalpy;
}
void CChemicalReaction::Initialize(const CMaterialsDatabase& _materials)
{
m_enthalpy = 0.0;
for (const auto& s : m_substances)
m_enthalpy += s->nu * _materials.GetConstPropertyValue(s->key, STANDARD_FORMATION_ENTHALPY);
for (auto& s : m_substances)
s->MM = _materials.GetConstPropertyValue(s->key, MOLAR_MASS);
}
void CChemicalReaction::SaveToFile(CH5Handler& _h5File, const std::string& _path) const
{
// current version of save procedure
_h5File.WriteAttribute(_path, StrConst::H5AttrSaveVersion, m_saveVersion);
_h5File.WriteData(_path, StrConst::Reac_H5Name, m_name);
_h5File.WriteData(_path, StrConst::Reac_H5Base, m_iBase);
_h5File.WriteAttribute(_path, StrConst::Reac_H5SubstancesNumber, static_cast<int>(m_substances.size()));
for (size_t i = 0; i < m_substances.size(); ++i)
{
const std::string group = _h5File.CreateGroup(_path, StrConst::Reac_H5Substance + std::to_string(i));
_h5File.WriteData(group, StrConst::Reac_H5SubstanceKey , m_substances[i]->key);
_h5File.WriteData(group, StrConst::Reac_H5SubstanceNu , m_substances[i]->nu);
_h5File.WriteData(group, StrConst::Reac_H5SubstanceOrder, m_substances[i]->order);
_h5File.WriteData(group, StrConst::Reac_H5SubstancePhase, E2I(m_substances[i]->phase));
}
}
void CChemicalReaction::LoadFromFile(const CH5Handler& _h5File, const std::string& _path)
{
if (!_h5File.IsValid()) return;
// load version of save procedure
//const int version = _h5File.ReadAttribute(_path, StrConst::H5AttrSaveVersion);
_h5File.ReadData(_path, StrConst::Reac_H5Name, m_name);
_h5File.ReadData(_path, StrConst::Reac_H5Base, m_iBase);
const size_t number = _h5File.ReadAttribute(_path, StrConst::Reac_H5SubstancesNumber);
m_substances.reserve(number);
for (size_t i = 0; i < number; ++i)
{
m_substances.emplace_back(new SChemicalSubstance);
const std::string group = _path + "/" + StrConst::Reac_H5Substance + std::to_string(i);
_h5File.ReadData(group, StrConst::Reac_H5SubstanceKey , m_substances.back()->key);
_h5File.ReadData(group, StrConst::Reac_H5SubstanceNu , m_substances.back()->nu);
_h5File.ReadData(group, StrConst::Reac_H5SubstanceOrder, m_substances.back()->order);
uint32_t temp;
_h5File.ReadData(group, StrConst::Reac_H5SubstancePhase, temp);
m_substances.back()->phase = static_cast<EPhase>(temp);
}
}
|