Documentation for OpenFLUID 2.2.2
Loading...
Searching...
No Matches
InjectGenerator.hpp
Go to the documentation of this file.
1/*
2
3 This file is part of OpenFLUID software
4 Copyright(c) 2021-2026, INRAE
5
6
7 == GNU General Public License Usage ==
8
9 OpenFLUID is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 OpenFLUID is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with OpenFLUID. If not, see <http://www.gnu.org/licenses/>.
21
22
23 == Other Usage ==
24
25 Other Usage means a use of OpenFLUID that is inconsistent with the GPL
26 license, and requires a written agreement between You and INRA.
27 Licensees for Other Usage of OpenFLUID may use this file in accordance
28 with the terms contained in the written agreement between You and INRA.
29
30*/
31
32
33/**
34 @file InjectGenerator.hpp
35
36 @author Jean-Christophe FABRE <jean-christophe.fabre@inra.fr>
37 @author Armel THÖNI <armel.thoni@inrae.fr>
38*/
39
40
41#ifndef __OPENFLUID_MACHINE_INJECTGENERATOR_HPP__
42#define __OPENFLUID_MACHINE_INJECTGENERATOR_HPP__
43
44
45#include <queue>
46#include <map>
47
52
53
54namespace openfluid { namespace machine {
55
56
57template<class T=double, class TV=openfluid::core::DoubleValue>
59{
60 protected:
61
63
64 std::string m_SourcesFile;
65 std::string m_DistriFile;
66
68
69
70 public:
71
73 m_SourcesFile(""),m_DistriFile(""), m_DistriBindings(nullptr)
74 {
75
76 }
77
78
80 {
81 if (m_DistriBindings != nullptr)
82 {
83 delete m_DistriBindings;
84 }
85
86 }
87
88
90 {
91 if (!OPENFLUID_GetWareParameter(Params,"sources",m_SourcesFile))
92 {
93 throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,
94 "missing sources value for generator");
95 }
96
97 if (!OPENFLUID_GetWareParameter(Params,"distribution",m_DistriFile))
98 {
99 throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,
100 "missing distribution value for generator");
101 }
102 }
103
105 {
107 std::string InputDir;
108
109 OPENFLUID_GetRunEnvironment("dir.input",InputDir);
110
111 DistriTables.build(InputDir,m_SourcesFile,m_DistriFile);
112 m_DistriBindings = new openfluid::tools::SimpleDistributionBindings<T, TV>(DistriTables);
113 }
114
116
118 {
119 m_DistriBindings->advanceToTime(OPENFLUID_GetCurrentDate());
120 TV Value;
122 openfluid::core::DateTime CurrentDT(OPENFLUID_GetCurrentDate());
123
124 OPENFLUID_UNITS_ORDERED_LOOP(m_UnitsClass,LU)
125 {
126 if (m_DistriBindings->getValue(LU->getID(),CurrentDT,Value))
127 {
128
129 }
130 else
131 {
132 Value = m_Default;
133 }
134
135 OPENFLUID_InitializeVariable(LU,m_VarName,Value);
136 }
137
139
140 if (m_DistriBindings->advanceToNextTimeAfter(CurrentDT,NextDT))
141 {
142 return Duration(NextDT.diffInSeconds(CurrentDT));
143 }
144 else
145 {
146 return Never();
147 }
148 }
149
150
152 {
153 m_DistriBindings->advanceToTime(OPENFLUID_GetCurrentDate());
154
155 TV Value;
157 openfluid::core::DateTime CurrentDT(OPENFLUID_GetCurrentDate());
158
159 OPENFLUID_UNITS_ORDERED_LOOP(m_UnitsClass,LU)
160 {
161 if (m_DistriBindings->getValue(LU->getID(),CurrentDT,Value))
162 {
163 OPENFLUID_AppendVariable(LU,m_VarName,Value);
164 }
165 }
166
168
169 if (m_DistriBindings->advanceToNextTimeAfter(CurrentDT,NextDT))
170 {
171 return Duration(NextDT.diffInSeconds(CurrentDT));
172 }
173 else
174 {
175 return Never();
176 }
177 }
178
180 { }
181
182};
183
184
185template<class T=double, class TV=openfluid::core::DoubleValue>
187{
188 protected:
189
192
195
196 public:
197
199 {
200 this->m_Default = 0;
201 }
202
204 {
206
207 if (this->OPENFLUID_GetWareParameter(Params,"thresholdmin",m_Min))
208 {
209 m_IsMin = true;
210 }
211
212 if (this->OPENFLUID_GetWareParameter(Params,"thresholdmax",m_Max))
213 {
214 m_IsMax = true;
215 }
216 }
217
218 void checkConsistency() override
219 {
220 if (m_IsMin && m_IsMax && m_Min > m_Max)
221 {
222 throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,
223 "threshold max value must be greater or equal "
224 "to threshold min value for generator");
225 }
226 }
227
229 {
230 this->m_DistriBindings->advanceToTime(this->OPENFLUID_GetCurrentDate());
231
234
236 {
237 TV Value;
238 if (this->m_DistriBindings->getValue(LU->getID(),CurrentDT,Value))
239 {
240 if (m_IsMax && Value > m_Max)
241 {
242 Value = m_Max;
243 }
244 if (m_IsMin && Value < m_Min)
245 {
246 Value = m_Min;
247 }
248
249 this->OPENFLUID_AppendVariable(LU,this->m_VarName,Value);
250 }
251 }
252
253
255
256 if (this->m_DistriBindings->advanceToNextTimeAfter(CurrentDT,NextDT))
257 {
258 return this->Duration(NextDT.diffInSeconds(CurrentDT));
259 }
260 else
261 {
262 return this->Never();
263 }
264 }
265};
266
267
269 public NumericalInjectGenerator<double, openfluid::core::DoubleValue>, public LinearGeneratorMixin
270{
271 public:
272
274
276
277};
278
279} } //namespaces
280
281
282#endif /* __OPENFLUID_MACHINE_INJECTGENERATOR_HPP__ */
#define OPENFLUID_UNITS_ORDERED_LOOP(unitsclass, unitptr)
Definition LoopMacros.hpp:86
Definition FrameworkException.hpp:51
Definition SchedulingRequest.hpp:54
Class for management of date and time information.
Definition DateTime.hpp:88
RawTime_t diffInSeconds(const DateTime &DT) const
Definition SpatialUnit.hpp:111
UnitID_t getID() const
Definition SpatialUnit.hpp:165
Definition InjectGenerator.hpp:59
std::string m_DistriFile
Definition InjectGenerator.hpp:65
void finalizeRun()
Definition InjectGenerator.hpp:179
TV m_Default
Definition InjectGenerator.hpp:62
void initParams(const openfluid::ware::WareParams_t &Params)
Definition InjectGenerator.hpp:89
void prepareData()
Definition InjectGenerator.hpp:104
openfluid::base::SchedulingRequest runStep()
Definition InjectGenerator.hpp:151
openfluid::base::SchedulingRequest initializeRun()
Definition InjectGenerator.hpp:117
~GenericInjectGenerator()
Definition InjectGenerator.hpp:79
void checkConsistency()
Definition InjectGenerator.hpp:115
GenericInjectGenerator()
Definition InjectGenerator.hpp:72
std::string m_SourcesFile
Definition InjectGenerator.hpp:64
openfluid::tools::SimpleDistributionBindings< T, TV > * m_DistriBindings
Definition InjectGenerator.hpp:67
Definition InjectGenerator.hpp:270
openfluid::base::SchedulingRequest runStep()
openfluid::base::SchedulingRequest initializeRun()
Definition Generator.hpp:127
Definition Generator.hpp:92
openfluid::core::UnitsClass_t m_UnitsClass
Definition Generator.hpp:97
openfluid::core::VariableName_t m_VarName
Definition Generator.hpp:95
Definition InjectGenerator.hpp:187
NumericalInjectGenerator()
Definition InjectGenerator.hpp:198
T m_Max
Definition InjectGenerator.hpp:194
void initParams(const openfluid::ware::WareParams_t &Params)
Definition InjectGenerator.hpp:203
openfluid::base::SchedulingRequest runStep()
Definition InjectGenerator.hpp:228
bool m_IsMax
Definition InjectGenerator.hpp:191
bool m_IsMin
Definition InjectGenerator.hpp:190
void checkConsistency() override
Definition InjectGenerator.hpp:218
T m_Min
Definition InjectGenerator.hpp:193
Definition DistributionTables.hpp:55
void build(const std::string &BasePath, const std::string &SourcesFileName, const std::string &DistributionFileName)
void advanceToTime(const openfluid::core::DateTime &DT)
Definition DistributionBindings.hpp:155
bool advanceToNextTimeAfter(const openfluid::core::DateTime &DT, openfluid::core::DateTime &NextDT)
Definition DistributionBindings.hpp:180
Definition DistributionBindings.hpp:227
bool getValue(const openfluid::core::UnitID_t &UnitID, const openfluid::core::DateTime &DT, TV &Value)
Definition DistributionBindings.hpp:263
openfluid::base::SchedulingRequest Duration(const openfluid::core::Duration_t &D) const
Definition PluggableSimulator.hpp:404
openfluid::base::SchedulingRequest Never() const
Definition PluggableSimulator.hpp:348
void OPENFLUID_AppendVariable(openfluid::core::SpatialUnit *UnitPtr, const openfluid::core::VariableName_t &VarName, const openfluid::core::Value &Val)
openfluid::core::StringValue OPENFLUID_GetWareParameter(const openfluid::ware::WareParams_t &Params, const openfluid::ware::WareParamKey_t &ParamName) const
openfluid::core::DateTime OPENFLUID_GetCurrentDate() const
#define OPENFLUID_API
Definition dllexport.hpp:86
std::map< WareParamKey_t, WareParamValue_t > WareParams_t
Definition TypeDefs.hpp:146
Definition ApplicationException.hpp:47