File: StateVariable.h

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 (292 lines) | stat: -rw-r--r-- 8,405 bytes parent folder | download | duplicates (2)
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
/* Copyright (c) 2020, Dyssol Development Team.
 * Copyright (c) 2023, DyssolTEC GmbH.
 * All rights reserved. This file is part of Dyssol. See LICENSE file for license information. */

#pragma once

#include "DyssolTypes.h"
#include <string>
#include <vector>
#include <limits>
#include <memory>

class CH5Handler;

////////////////////////////////////////////////////////////////////////////////
// CStateVariable
//

/**
 * \brief Description of time-dependent state variables of the unit.
 * \details Used either to track internal time-dependent variables of the unit or to output it to the user.
 */
class CStateVariable
{
	static constexpr unsigned m_saveVersion{ 1 };	// Current version of the saving procedure.

	inline static const double m_eps{ 16 * std::numeric_limits<double>::epsilon() };

	std::string m_name;					// Name of the state variable.
	double m_value;						// Current value of a time-dependent state variable.
	double m_valueStored;				// Memory for temporary storage of the state variable for cyclic recalculations.

	std::vector<STDValue> m_history;	// Stored history of time-dependent changes.

public:
	/**
	 * \private
	 * \param _name Name of the state variable.
	 * \param _initValue Initial value of the state variable.
	 */
	CStateVariable(std::string _name, double _initValue);

	/**
	 * \private
	 * \brief Clears state variable.
	 */
	void Clear();

	/**
	 * \brief Returns name of the state variable.
	 * \return Name of the state variable.
	 */
	std::string GetName() const;
	/**
	 * \private
	 * \brief Sets name of the state variable.
	 * \param _name Name of the state variable.
	 */
	void SetName(const std::string& _name);

	/**
	 * \brief Returns current value of the state variable.
	 * \return Current value of the state variable.
	 */
	double GetValue() const;
	/**
	 * \private
	 * \brief Sets new value of the state variable.
	 * \param _value New value.
	 */
	void SetValue(double _value);
	/**
	 * \private
	 * \brief Sets new value of the state variable and saves it in the history of changes.
	 * \param _time Time point.
	 * \param _value New value.
	 */
	void SetValue(double _time, double _value);

	/**
	 * \private
	 * \brief Stores current value of the state variable.
	 */
	void SaveState();
	/**
	 * \private
	 * \brief Restores previously stored value of the state variable.
	 */
	void LoadState();

	/**
	 * \brief Checks whether the state variable contains a stored history of time-dependent changes.
	 * \return Whether the state variable stores history.
	 */
	bool HasHistory() const;
	/**
	 * \brief Returns the stored history of time-dependent changes.
	 * \return History of time-dependent changes.
	 */
	std::vector<STDValue> GetHistory() const;
	/**
	 * \brief Returns a value for a given time point from the stored history.
	 * \details Interpolates the value if it is required.
	 * \param _time Time point.
	 * \return Value at the given time point.
	 */
	double GetHistoryValue(double _time) const;
	/**
	 * \private
	 * \brief Sets the history of time-dependent changes.
	 * \param _history New history.
	 */
	void SetHistory(const std::vector<STDValue>& _history);

	/**
	 * \private
	 * \brief Saves data to file.
	 * \param _h5File Reference to the file handler.
	 * \param _path Path to data in the file.
	 */
	void SaveToFile(CH5Handler& _h5File, const std::string& _path) const;
	/**
	 * \private
	 * \brief Loads data from file.
	 * \param _h5File Reference to the file handler.
	 * \param _path Path to data in the file.
	 */
	void LoadFromFile(CH5Handler& _h5File, const std::string& _path);
private:
	void AddToHistory(double _time, double _value);	// Adds the given value to the history and removes all data after the given time.
};

////////////////////////////////////////////////////////////////////////////////
// CStateVariablesManager
//

/**
* \brief Manager of state variables.
*/
class CStateVariablesManager
{
	static constexpr unsigned m_saveVersion{ 1 };	// Current version of the saving procedure.

	std::vector<std::unique_ptr<CStateVariable>> m_stateVariables;	// Defined state variables.

public:
	/**
	 * \private
	 * \brief Default constructor.
	 */
	CStateVariablesManager() = default;
	/**
	 * \private
	 * \brief Copy constructor.
	 */
	CStateVariablesManager(const CStateVariablesManager& _other);
	/**
	 * \private
	 * \brief Move constructor.
	 */
	CStateVariablesManager(CStateVariablesManager&& _other) noexcept;
	/**
	 * \private
	 * \brief Copy assignment operator.
	 */
	CStateVariablesManager& operator=(CStateVariablesManager _other);
	/**
	 * \private
	 * \brief Move assignment operator.
	 */
	CStateVariablesManager& operator=(CStateVariablesManager&& _other) noexcept;
	/**
	 * \private
	 * \brief Destructor.
	 */
	~CStateVariablesManager() = default;

	/**
	 * \brief Swaps the content of two managers.
	 * \param _first First manager.
	 * \param _second Second manager.
	 */
	friend void swap(CStateVariablesManager& _first, CStateVariablesManager& _second) noexcept;

	/**
	 * \private
	 * \brief Adds a new time-dependent state variable and returns a pointer to it.
	 * \details If a state variable with this name already exists, does nothing and returns nullptr.
	 * \param _name Name of the state variable.
	 * \param _initValue Initial value of the state variable.
	 * \return Pointer to added state variable.
	 */
	CStateVariable* AddStateVariable(const std::string& _name, double _initValue);
	/**
	 * \brief Returns a state variable with the specified index.
	 * \param _index Index of the state variable.
	 * \return Const pointer to state variable.
	 */
	const CStateVariable* GetStateVariable(size_t _index) const;
	/**
	 * \brief Returns a state variable with the specified index.
	 * \param _index Index of the state variable.
	 * \return Pointer to state variable.
	 */
	CStateVariable* GetStateVariable(size_t _index);
	/**
	 * \brief Returns a state variable with the specified name.
	 * \param _name Name of the state variable.
	 * \return Const pointer to state variable.
	 */
	const CStateVariable* GetStateVariable(const std::string& _name) const;
	/**
	 * \brief Returns a state variable with the specified name.
	 * \param _name Name of the state variable.
	 * \return Pointer to state variable.
	 */
	CStateVariable* GetStateVariable(const std::string& _name);

	/**
	 * \brief Returns const pointers to all defined state variables.
	 * \return Const pointers to all state variables.
	 */
	std::vector<const CStateVariable*> GetAllStateVariables() const;
	/**
	 * \brief Returns const pointers to all defined state variables.
	 * \return Pointers to all state variables.
	 */
	std::vector<CStateVariable*> GetAllStateVariables();

	/**
	 * \private
	 * \brief Returns const pointers to all defined state variables that track history of changes.
	 * \return Const pointers to all state variables with history.
	 */
	std::vector<const CStateVariable*> GetAllStateVariablesWithHistory() const;
	/**
	 * \private
	 * \brief Returns const pointers to all defined state variables that track history of changes.
	 * \return Pointers to all state variables with history.
	 */
	std::vector<CStateVariable*> GetAllStateVariablesWithHistory();

	/**
	 * \brief Returns number of defined state variables.
	 * \return Number of state variables.
	 */
	size_t GetStateVariablesNumber() const;

	/**
	 * \private
	 * \brief Clears all data in all state variables.
	 */
	void ClearData();
	/**
	 * \private
	 * \brief Removes all defined state variables.
	 */
	void Clear();

	/**
	 * \private
	 * \brief Stores the current state of all state variables.
	 */
	void SaveState();
	/**
	 * \private
	 * \brief Restores previously stored state of all state variables.
	 */
	void LoadState();

	/**
	 * \private
	 * \brief Saves data to file.
	 * \param _h5File Reference to the file handler.
	 * \param _path Path to data in the file.
	 */
	void SaveToFile(CH5Handler& _h5File, const std::string& _path) const;
	/**
	 * \private
	 * \brief Loads data from file.
	 * \param _h5File Reference to the file handler.
	 * \param _path Path to data in the file.
	 */
	void LoadFromFile(CH5Handler& _h5File, const std::string& _path);
	/**
	 * \private
	 * \brief Loads data from file. A compatibility version.
	 * \param _h5File Reference to the file handler.
	 * \param _path Path to data in the file.
	 */
	void LoadFromFile_v0(const CH5Handler& _h5File, const std::string& _path);
};