File: MixtureEnthalpyLookup.cpp

package info (click to toggle)
dyssol 1.5.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 18,184 kB
  • sloc: cpp: 53,870; sh: 85; python: 59; makefile: 11
file content (196 lines) | stat: -rw-r--r-- 5,489 bytes parent folder | download | duplicates (3)
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
/* Copyright (c) 2020, Dyssol Development Team. All rights reserved. This file is part of Dyssol. See LICENSE file for license information. */

#include "MixtureEnthalpyLookup.h"
#include "MaterialsDatabase.h"
#include "DyssolUtilities.h"
#include <utility>

CMixtureEnthalpyLookup::CMixtureEnthalpyLookup(const CMaterialsDatabase* _materialsDB, std::vector<std::string> _compounds)
	: CMixtureEnthalpyLookup{ _materialsDB, std::move(_compounds), { DEFAULT_ENTHALPY_MIN_T, DEFAULT_ENTHALPY_MAX_T }, DEFAULT_ENTHALPY_INTERVALS }
{
}

CMixtureEnthalpyLookup::CMixtureEnthalpyLookup(const CMaterialsDatabase* _materialsDB, std::vector<std::string> _compounds, const SInterval& _limits, size_t _intervalsNumber)
	: m_limits{ _limits }
	, m_intervals{ _intervalsNumber }
	, m_materialsDB{ _materialsDB }
	, m_compounds{ std::move(_compounds) }
{
	UpdateCompoundsEnthalpies();
}

void CMixtureEnthalpyLookup::SetLimits(const SInterval& _limits, size_t _number)
{
	m_limits = _limits;
	m_intervals = _number;
	UpdateCompoundsEnthalpies();
}

SInterval CMixtureEnthalpyLookup::GetLimits() const
{
	return m_limits;
}

size_t CMixtureEnthalpyLookup::GetIntervalsNumber() const
{
	return m_intervals;
}

void CMixtureEnthalpyLookup::SetMaterialsDatabase(const CMaterialsDatabase* _materialsDB)
{
	m_materialsDB = _materialsDB;
	UpdateCompoundsEnthalpies();
}

void CMixtureEnthalpyLookup::SetCompounds(const std::vector<std::string>& _compounds)
{
	m_compounds = _compounds;
	UpdateCompoundsEnthalpies();
}

void CMixtureEnthalpyLookup::SetCompoundFractions(const std::vector<double>& _fractions)
{
	std::vector<double> filteredFractions(_fractions.size());
	std::replace_copy_if(_fractions.begin(), _fractions.end(), filteredFractions.begin(), [](double v) { return v < 0; }, 0.0);
	Normalize(filteredFractions);
	if (_fractions.size() != m_compounds.size()) return;
	m_mixtureLookup.SetWeights(filteredFractions);
}

std::vector<double> CMixtureEnthalpyLookup::GetCompoundFractions() const
{
	return m_mixtureLookup.GetWeights();
}

size_t CMixtureEnthalpyLookup::Size() const
{
	return m_mixtureLookup.Size();
}

double CMixtureEnthalpyLookup::GetEnthalpy(double _temperature) const
{
	return m_mixtureLookup.GetRight(_temperature);
}

double CMixtureEnthalpyLookup::GetTemperature(double _enthalpy) const
{
	return m_mixtureLookup.GetLeft(_enthalpy);
}

double CMixtureEnthalpyLookup::GetEnthalpy(double _temperature, const std::vector<double>& _fractions)
{
	SetCompoundFractions(_fractions);
	return GetEnthalpy(_temperature);
}

double CMixtureEnthalpyLookup::GetTemperature(double _enthalpy, const std::vector<double>& _fractions)
{
	SetCompoundFractions(_fractions);
	return GetTemperature(_enthalpy);
}

void CMixtureEnthalpyLookup::Clear()
{
	m_mixtureLookup.Clear();
	m_compounds.clear();
}

void CMixtureEnthalpyLookup::Add(double _value)
{
	m_mixtureLookup.Add(_value);
}

void CMixtureEnthalpyLookup::Add(const CDependentValues& _component, double _weight)
{
	m_mixtureLookup.Add(_component, _weight);
}

void CMixtureEnthalpyLookup::Add(const CMixtureEnthalpyLookup& _table, double _weight)
{
	m_mixtureLookup.Add(_table.m_mixtureLookup, _weight);
}

void CMixtureEnthalpyLookup::Multiply(double _value)
{
	m_mixtureLookup.Multiply(_value);
}

CMixtureEnthalpyLookup CMixtureEnthalpyLookup::operator+(double _d) const
{
	CMixtureEnthalpyLookup res(*this);
	res.Add(_d);
	return res;
}

CMixtureEnthalpyLookup CMixtureEnthalpyLookup::operator*(double _d) const
{
	CMixtureEnthalpyLookup res(*this);
	res.Multiply(_d);
	return res;
}

CMixtureEnthalpyLookup CMixtureEnthalpyLookup::operator+(const CMixtureEnthalpyLookup& _t) const
{
	CMixtureEnthalpyLookup res(*this);
	res.Add(_t);
	return res;
}

CMixtureEnthalpyLookup& CMixtureEnthalpyLookup::operator+=(double _d)
{
	Add(_d);
	return *this;
}

CMixtureEnthalpyLookup& CMixtureEnthalpyLookup::operator*=(double _d)
{
	Multiply(_d);
	return *this;
}

CMixtureEnthalpyLookup& CMixtureEnthalpyLookup::operator+=(const CMixtureEnthalpyLookup& _t)
{
	Add(_t);
	return *this;
}

bool CMixtureEnthalpyLookup::operator==(const CMixtureEnthalpyLookup& _t) const
{
	return m_mixtureLookup == _t.m_mixtureLookup && m_compounds == _t.m_compounds;
}

void CMixtureEnthalpyLookup::UpdateCompoundsEnthalpies()
{
	if (!m_materialsDB) return;

	// temperature step
	const double deltaT = (m_limits.max - m_limits.min) / static_cast<double>(m_intervals);
	// keep old weights
	auto weights = m_mixtureLookup.GetWeights();
	// if weights are wrong or not defined, set same weights
	if (weights.size() != m_compounds.size())
		weights.assign(m_compounds.size(), 1.0 / static_cast<double>(m_compounds.size()));
	// clear all previous data
	m_mixtureLookup.Clear();
	// fill compound tables
	for (size_t iCmp = 0; iCmp < m_compounds.size(); ++iCmp)
	{
		CDependentValues table;
		for (size_t iInt = 0; iInt <= m_intervals; ++iInt)
		{
			const double T = m_limits.min + deltaT * static_cast<double>(iInt);
			const double enthalpy = m_materialsDB->GetTPPropertyValue(m_compounds[iCmp], ENTHALPY, T, STANDARD_CONDITION_P);
			table.SetValue(T, enthalpy);
		}
		// check if it contains a constant, because of the requirements of CMixtureLookup on elements uniqueness
		if (table.IsConst())
		{
			// leave only one value
			const auto enthalpy = table.GetValueAt(0);
			table.Clear();
			table.SetValue((m_limits.max + m_limits.min) / 2.0, enthalpy);
		}
		// add compound table with its weight
		m_mixtureLookup.AddComponent(table, weights[iCmp]);
	}
}