-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDSFunctions.h
177 lines (147 loc) · 3.51 KB
/
DSFunctions.h
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
//
// DSFunctions.h
// ims
//
// Created by xkaisl00, xmatej42.
//
#ifndef ims_DSFunctions_h
#define ims_DSFunctions_h
#include "DSTime.h"
#include "DSIntegratorBlock.h"
#include <cstdio>
#include <iostream>
#include <cstdarg>
#include <fstream>
#include <cstdlib>
std::string output = "";
// Nastaveni typu integratoru.
void setMethod(intType it)
{
integratorType = it;
}
// Inicializace casu vypoctu od do.
void init(double start, double stop)
{
DSTime oldTime = t;
if(runSampler == NULL)
{
std::cerr << "Doplnte Sampler" << std::endl;
exit(1);
}
t = DSTime(start, stop, fmin(runSampler->step, t.getMaxStep()));
t.setMinMaxStep(oldTime.getMinStep(), oldTime.getMaxStep());
runSampler->setStartTime(start);
double step;
if(runSampler->step <= t.getMaxStep() && runSampler->step >= t.getMinStep())
step = runSampler->step;
else if(runSampler->step >= t.getMinStep())
{
int steps = runSampler->step / t.getMaxStep() + 1;
step = runSampler->step / steps;
}
else
{
fprintf(stderr, "chyba, minimalni krok je vetsi nez krok vypisu\n");
exit(1);
}
t.setBaseStep(step);
}
// Provedeni jednoho kroku simulace.
void runSimulation()
{
for(std::vector<DSIntegratorBlock *>::iterator it = integrators.begin(); it != integrators.end(); ++it)
{
(*it)->run();
}
}
// Provedeni simulace.
void run()
{
if(runSampler == NULL)
{
std::cerr << "Doplnte Sampler" << std::endl;
exit(1);
}
std::ofstream ofs;
ofs.open(output.c_str(), std::ofstream::out);
ofs.close();
double newStep;
double tempTime;
DSTime tempDSTime = t;
while (t.isCurrentTimeValid())
{
if(flagReset)
{
for(std::vector<DSIntegratorBlock *>::iterator it = integrators.begin(); it != integrators.end(); ++it)
(*it)->reset();
}
runSimulation();
flagReset = false;
if(t.value() >= runSampler->value())
{
tempTime = t.value();
t.setTime(runSampler->value());
runSampler->function();
t.setTime(tempTime);
runSampler->incrementTime();
}
if(flagDecrementStep)
{
flagDecrementStep = false;
flagReset = true;
t = tempDSTime;
if((newStep = t.getStep()/2) < t.getMinStep())
newStep = t.getMinStep();
t.setStep(newStep);
}
tempDSTime = t;
t.incrementTime();
}
}
// Nastaveni presnosti pouze relativni hodnotou.
void setAccuracy(double rel)
{
eRel = rel;
eAbs = 0;
}
// Nastaveni presnosti relativne i absolutne.
void setAccuracy(double abs, double rel)
{
eAbs = abs;
eRel = rel;
}
// Nastaveni minimalniho a maximalniho kroku vypoctu.
void setMinMaxStep(double min, double max)
{
t.setMinMaxStep(min, max);
}
// Vytisknuti hodnot z bloku.
int print(const char *format, ...)
{
va_list arg;
int status;
char string[256];
va_start(arg, format);
status = vsprintf (string, format, arg);
va_end(arg);
if(!flagDecrementStep)
{
if(!output.empty())
{
std::ofstream ofs;
ofs.open(output.c_str(), std::ofstream::app);
ofs << string;
ofs.close();
}
else
std::cout << string;
t.clearStep();
}
return status;
}
// Nastaveni vystupu do souboru
void setOutput(std::string name)
{
output = name;
}
#endif