VirtualFluids 0.2.0
Parallel CFD LBM Solver
Loading...
Searching...
No Matches
UbScheduler.h
Go to the documentation of this file.
1//=======================================================================================
2// ____ ____ __ ______ __________ __ __ __ __
3// \ \ | | | | | _ \ |___ ___| | | | | / \ | |
4// \ \ | | | | | |_) | | | | | | | / \ | |
5// \ \ | | | | | _ / | | | | | | / /\ \ | |
6// \ \ | | | | | | \ \ | | | \__/ | / ____ \ | |____
7// \ \ | | |__| |__| \__\ |__| \________/ /__/ \__\ |_______|
8// \ \ | | ________________________________________________________________
9// \ \ | | | ______________________________________________________________|
10// \ \| | | | __ __ __ __ ______ _______
11// \ | | |_____ | | | | | | | | | _ \ / _____)
12// \ | | _____| | | | | | | | | | | \ \ \_______
13// \ | | | | |_____ | \_/ | | | | |_/ / _____ |
14// \ _____| |__| |________| \_______/ |__| |______/ (_______/
15//
16// This file is part of VirtualFluids. VirtualFluids is free software: you can
17// redistribute it and/or modify it under the terms of the GNU General Public
18// License as published by the Free Software Foundation, either version 3 of
19// the License, or (at your option) any later version.
20//
21// VirtualFluids is distributed in the hope that it will be useful, but WITHOUT
22// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24// for more details.
25//
26// SPDX-License-Identifier: GPL-3.0-or-later
27// SPDX-FileCopyrightText: Copyright © VirtualFluids Project contributors, see AUTHORS.md in root folder
28//
33//=======================================================================================
34#ifndef UBSCHEDULER_H
35#define UBSCHEDULER_H
36
37#include <algorithm>
38#include <cassert>
39#include <iomanip>
40#include <iostream>
41#include <limits>
42#include <sstream>
43#include <string>
44
49
56
58{
59public:
61 {
62 friend class UbScheduler;
63
64 public:
65 UbSchedule() : step(ub_math::inf), begin(ub_math::inf), end(ub_math::inf) {}
66 UbSchedule(const double &step, const double &begin = 0.0, const double &end = ub_math::inf)
67 : step(step), begin(begin), end(end)
68 {
69 }
70 double getStep() const { return this->step; }
71 double getBegin() const { return this->begin; }
72 double getEnd() const { return this->end; }
73
74 /*==========================================================*/
75 std::string toString()
76 {
77 std::stringstream text;
78 text << *this;
79 return text.str();
80 }
81 /*==========================================================*/
82 friend inline std::ostream &operator<<(std::ostream &os, const UbSchedule &schedule)
83 {
84 os << "Schedule[start,end,step]=[" << schedule.begin << ", " << schedule.end << ", " << schedule.step
85 << "]";
86 return os;
87 }
88
89 private:
90 double step, begin, end;
91 };
92
93public:
94 UbScheduler() { this->initVals(); }
95 /*==========================================================*/
96 UbScheduler(const double &step, const double &begin = 0, const double &end = ub_math::inf)
97 {
98 this->initVals();
99 this->addSchedule(step, begin, end);
100 }
101 /*==========================================================*/
103 {
104 this->initVals();
105 this->addSchedule(schedule);
106 }
107 /*==========================================================*/
108 virtual ~UbScheduler() = default;
109 /*==========================================================*/
110 inline void addSchedule(const UbSchedule &schedule)
111 {
112 this->addSchedule(schedule.step, schedule.begin, schedule.end);
113 }
114 /*==========================================================*/
115 bool addSchedule(const double &step, const double &begin, double end)
116 {
117 if (ub_math::zero(step) || begin > end) {
118 std::cerr << "UbScheduler::addSchedule - invalid Schedule:\n\t" << UbSchedule(step, begin, end)
119 << std::endl;
120 return false;
121 }
122
123 if (ub_math::less(end, (double)ub_math::inf)) {
124 // es kann vorkommen, dass man mit dem intervall nicht genau auf den letzten wert kommt
125 //(z.B. step=2; start=0; end=9; -> ende wird angepasst)
126 // also wenn end-begin>ub_math::inf ist, dann geht es halt nicht.. ein cast in long double half hier nichts
127 double multiplier = 0.0;
128 double fractpart = modf((end - begin) / step, &multiplier);
129 if (!ub_math::zero(fractpart)) {
130 // tmp-speicherung (fuer cerr)
131 fractpart = end;
132 // neues ende
133 end = begin + multiplier * step;
134
135 std::cerr << "Warning: UbScheduler::addSchedule - "
136 << "end of schedule was adapted to intervall \n\t"
137 << "from " << UbSchedule(step, begin, fractpart) << " to " << UbSchedule(step, begin, end)
138 << std::endl;
139 }
140 }
141
142 // nu aber:
143 schedules.emplace_back(step, begin, end);
144
145 if (end > maxT)
146 maxT = end;
147
148 double potentialDueTime;
152 }
153
154 return true;
155 }
156 /*==========================================================*/
157 // returns true if scheduler contains schedules
158 bool hasSchedules() const { return !schedules.empty(); }
159 /*==========================================================*/
160 // time bei dem das letzte mal isDue(time) true war
161 double getLastDueTime() const { return lastDueTime; }
162 /*==========================================================*/
163 // time bei dem das naechste mal isDue(time) true ergibt
164 double getNextDueTime() const { return nextDueTime; }
165 /*==========================================================*/
166 // maxDueTime (maxTime der Schedules!
167 double getMaxDueTime() const { return this->maxT; }
168 /*==========================================================*/
169 bool isDue(const double &t)
170 {
171 lastUsedT = t;
173 // groesser maxT is nicht
174 if (ub_math::greater(t, maxT))
175 return false;
176
177 // temp var
178 double actDueTime = nextDueTime;
179
180 // um Suche nach nextDueTime bei "Zukunfts-t" zu optimieren, setzt man die "start"-suchzeit auf "t-1":
181 nextDueTime = t - 1; // t-1 deshlab, damit falls z.B. while Schleife nicht durchlaufen wird
182 // die folgende if Abfrage nicht faelschlicher Weise true ist!
184 double tmpNextDueTime = maxT, potentialDueTime = -1.0;
185 for (std::size_t i = 0; i < schedules.size(); i++) {
190 }
191 }
194 }
195
196 // wenn t = der aktuuellen oder gar schon der naechstmoeglichen ist (hierbei wurde
197 // zuvor actDueTime und nextDueTime ggf. angepasst)
198 // Bsp.: nextDuTime war 5, aber fuer t=400 gilt andere schedule -> Bsp actDue=350 und nextDue 405
200 lastDueTime = t;
201 return true;
202 }
203 } else if (ub_math::lessEqual(t, lastDueTime)) {
205 return true; // braucht man, wenn man fuer dasselbe t isDue(t) aufruft
206 else {
207 // Fall: Zeit liegt faktisch in der Vergangenheit -> neu initialsisieren
208 double tmpNextDueTime = maxT, potentialDueTime = -1.0;
209 for (size_t i = 0; i < schedules.size(); i++) {
213 }
214 }
216
218 }
219 }
220
221 return false;
222 }
223 /*==========================================================*/
224 inline double getMinBegin() const
225 {
226 if (schedules.empty())
227 return ub_math::inf;
228 return std::min_element(schedules.begin(), schedules.end(), ub_comparators::membercomp(&UbSchedule::getBegin))
229 ->getBegin();
230 }
231 /*==========================================================*/
232 inline double getMaxBegin() const
233 {
234 if (schedules.empty())
235 return ub_math::inf;
236 return std::max_element(schedules.begin(), schedules.end(), ub_comparators::membercomp(&UbSchedule::getBegin))
237 ->getBegin();
238 }
239 /*==========================================================*/
240 inline double getMinEnd() const
241 {
242 if (schedules.empty())
243 return ub_math::inf;
244 return std::min_element(schedules.begin(), schedules.end(), ub_comparators::membercomp(&UbSchedule::getEnd))
245 ->getEnd();
246 }
247 /*==========================================================*/
248 inline double getMaxEnd() const
249 {
250 if (schedules.empty())
251 return ub_math::inf;
252 return std::max_element(schedules.begin(), schedules.end(), ub_comparators::membercomp(&UbSchedule::getEnd))
253 ->getEnd();
254 }
255 /*==========================================================*/
256 inline double getMinStep() const
257 {
258 if (schedules.empty())
259 return ub_math::inf;
260 return std::min_element(schedules.begin(), schedules.end(), ub_comparators::membercomp(&UbSchedule::getStep))
261 ->getStep();
262 }
263 /*==========================================================*/
264 inline double getMaxStep() const
265 {
266 if (schedules.empty())
267 return ub_math::inf;
268 return std::max_element(schedules.begin(), schedules.end(), ub_comparators::membercomp(&UbSchedule::getStep))
269 ->getStep();
270 }
271 /*==========================================================*/
272 inline std::string toString() const
273 {
274 std::stringstream text;
275 text << *this;
276 return text.str();
277 }
278 /*==========================================================*/
279 friend inline std::ostream &operator<<(std::ostream &os, const UbScheduler &scheduler)
280 {
281 os << "UbScheduler\n";
282 os << "Schedule | start | end | intervall " << std::endl;
283 for (std::size_t i = 0; i < scheduler.schedules.size(); i++)
284 os << std::setw(9) << i << "|" << std::setw(19) << scheduler.schedules[i].getBegin() << "|" << std::setw(19)
285 << scheduler.schedules[i].getEnd() << "|" << std::setw(19) << scheduler.schedules[i].getStep()
286 << std::endl;
287 return os;
288 }
289
290protected:
291 /*==========================================================*/
292 void initVals()
293 {
294 lastUsedT = -ub_math::inf;
295 lastDueTime = -ub_math::inf;
296 nextDueTime = ub_math::inf;
297 maxT = -ub_math::inf;
298 }
299 /*==========================================================*/
300 // calculates next due time for a schedule
301 // with nextDueTime > searchStart
303 {
305 return false;
306 else if (ub_math::less(searchStart, schedule.begin))
307 nextDueTime = schedule.begin;
308 else {
309 nextDueTime = schedule.begin + ((int)((searchStart - schedule.begin) / schedule.step) + 1) * schedule.step;
311 return false;
312 }
313 }
314 return true;
315 }
316
317protected:
318 double lastUsedT;
321 double maxT;
322
323 std::vector<UbSchedule> schedules;
324};
325
327
328#endif // UBSCHEDULER_H
329
330// int main(int argc, char** argv)
331//{
332// UbScheduler writeSchedule;
337// writeSchedule.addSchedule(0,2,1);
338// writeSchedule.addSchedule(0,100001,200);
339//
340// for(int t = 0; t < 1001; t++)
341// {
342// if(writeSchedule.isDue(t))
343// {
344// cout<<"due@ "<<t<<endl;
345// }
346// }
347// return 0;
348//}
349
std::string toString()
Definition UbScheduler.h:75
friend std::ostream & operator<<(std::ostream &os, const UbSchedule &schedule)
Definition UbScheduler.h:82
double getStep() const
Definition UbScheduler.h:70
UbSchedule(const double &step, const double &begin=0.0, const double &end=ub_math::inf)
Definition UbScheduler.h:66
double getBegin() const
Definition UbScheduler.h:71
double getEnd() const
Definition UbScheduler.h:72
A class implements scheduling.
Definition UbScheduler.h:58
void addSchedule(const UbSchedule &schedule)
void initVals()
double lastDueTime
double getMaxEnd() const
double getNextDueTime() const
double getMinEnd() const
virtual ~UbScheduler()=default
UbScheduler(const UbSchedule &schedule)
std::vector< UbSchedule > schedules
double getMaxDueTime() const
double getMinBegin() const
double getMaxBegin() const
double getMinStep() const
friend std::ostream & operator<<(std::ostream &os, const UbScheduler &scheduler)
bool hasSchedules() const
bool calcNextDueTimeForSchedule(const UbSchedule &schedule, const double &searchStart, double &nextDueTime)
double lastUsedT
double getMaxStep() const
bool addSchedule(const double &step, const double &begin, double end)
double getLastDueTime() const
std::string toString() const
bool isDue(const double &t)
double nextDueTime
UbScheduler(const double &step, const double &begin=0, const double &end=ub_math::inf)
Definition UbScheduler.h:96
std::shared_ptr< T > SPtr
MemComp< Ptr > membercomp(Ptr p)
bool greaterEqual(const T1 &value, const T2 &reference)
Definition UbMath.h:223
bool lessEqual(const T1 &value, const T2 &reference)
Definition UbMath.h:209
bool equal(const T1 &value, const T2 &reference)
Definition UbMath.h:189
bool greater(const T1 &value, const T2 &reference)
Definition UbMath.h:216
bool zero(const T &value)
Definition UbMath.h:139
bool less(const T1 &value, const T2 &reference)
Definition UbMath.h:202