This repository was archived by the owner on May 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathSolverDefaultImplementation.cpp
More file actions
229 lines (196 loc) · 6.46 KB
/
SolverDefaultImplementation.cpp
File metadata and controls
229 lines (196 loc) · 6.46 KB
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
/** @addtogroup coreSolver
*
* @{
*/
#include <Core/ModelicaDefine.h>
#include <Core/Modelica.h>
#include <Core/Solver/FactoryExport.h>
#include <Core/Solver/SolverDefaultImplementation.h>
#include <Core/Solver/SolverSettings.h>
#include <Core/SimulationSettings/IGlobalSettings.h>
#include <Core/Math/Constants.h>
#include <Core/System/FactoryExport.h>
#include <Core/Utils/extension/logger.hpp>
SolverDefaultImplementation::SolverDefaultImplementation(IMixedSystem* system, ISolverSettings* settings)
: SimulationMonitor()
, _system (system)
, _settings (settings)
, _tInit (0.0)
, _tCurrent (0.0)
, _tEnd (0.0)
, _tLastSuccess (0.0)
, _tLastUnsucess (0.0)
, _tLargeStep (0.0)
, _h (0.0)
, _firstCall (false)
, _firstStep (true)
, _totStps (0)
, _accStps (0)
, _rejStps (0)
, _zeroStps (0)
, _zeros (0)
, _dimSys (0)
, _zeroStatus (ISolver::UNCHANGED_SIGN)
, _zeroValInit (NULL)
, _dimZeroFunc (0)
, _zeroVal (NULL)
, _zeroValLastSuccess (NULL)
, _events (NULL)
, _solverStatus (ISolver::UNDEF_STATUS)
, _outputCommand (IWriteOutput::WRITEOUT)
{
#ifdef RUNTIME_PROFILING
if(MeasureTime::getInstance() != NULL)
{
measureTimeFunctionsArray = new std::vector<MeasureTimeData*>(1, NULL); //0 write output
(*measureTimeFunctionsArray)[0] = new MeasureTimeData("writeOutput");
MeasureTime::addResultContentBlock(system->getModelName(),"solver",measureTimeFunctionsArray);
writeFunctionStartValues = MeasureTime::getZeroValues();
writeFunctionEndValues = MeasureTime::getZeroValues();
}
else
{
measureTimeFunctionsArray = new std::vector<MeasureTimeData*>();
writeFunctionStartValues = NULL;
writeFunctionEndValues = NULL;
}
#endif
}
SolverDefaultImplementation::~SolverDefaultImplementation()
{
if(_zeroVal)
delete [] _zeroVal;
if(_zeroValInit)
delete [] _zeroValInit;
if(_zeroValLastSuccess)
delete [] _zeroValLastSuccess;
if(_events)
delete [] _events;
#ifdef RUNTIME_PROFILING
if(writeFunctionStartValues)
delete writeFunctionStartValues;
if(writeFunctionEndValues)
delete writeFunctionEndValues;
#endif
}
void SolverDefaultImplementation::setStartTime(const double& t)
{
_tCurrent = t;
};
void SolverDefaultImplementation::setEndTime(const double& t)
{
_tEnd = t;
};
void SolverDefaultImplementation::setInitStepSize(const double& h)
{
_h = h;
};
const ISolver::SOLVERSTATUS SolverDefaultImplementation::getSolverStatus()
{
return _solverStatus;
};
void SolverDefaultImplementation::initialize()
{
SimulationMonitor::initialize();
IContinuous* continous_system = dynamic_cast<IContinuous*>(_system);
IEvent* event_system = dynamic_cast<IEvent*>(_system);
ITime* timeevent_system = dynamic_cast<ITime*>(_system);
IWriteOutput* writeoutput_system = dynamic_cast<IWriteOutput*>(_system);
// Set current start time to the system
timeevent_system->setTime(_tCurrent);
if(_settings->getGlobalSettings()->getOutputPointType() != OPT_NONE)
writeoutput_system->writeOutput(IWriteOutput::HEAD_LINE);
// Allocate array with values of zero functions
if (_dimZeroFunc != event_system->getDimZeroFunc())
{
// Number (dimension) of zero functions
_dimZeroFunc = event_system->getDimZeroFunc();
if(_zeroVal)
delete [] _zeroVal;
if(_zeroValInit)
delete [] _zeroValInit;
if(_zeroValLastSuccess)
delete [] _zeroValLastSuccess;
if(_events)
delete [] _events;
_zeroVal = new double[_dimZeroFunc];
_zeroValLastSuccess = new double[_dimZeroFunc];
_events = new bool[_dimZeroFunc];
_zeroValInit = new double[_dimZeroFunc];
continous_system->evaluateZeroFuncs(IContinuous::CONTINUOUS);
event_system->getZeroFunc(_zeroVal);
memcpy(_zeroValLastSuccess,_zeroVal,_dimZeroFunc*sizeof(double));
memcpy(_zeroValInit,_zeroVal,_dimZeroFunc*sizeof(double));
memset(_events,false,_dimZeroFunc*sizeof(bool));
}
// Set flags
_firstCall = true;
_firstStep = true;
// Reset counter
_totStps = 0;
_accStps = 0;
_rejStps = 0;
_zeroStps = 0;
_zeros = 0;
// Set initial step size
//_h = _settings->_globalSettings->_hOutput;
}
void SolverDefaultImplementation::setZeroState()
{
// Reset Zero-State
_zeroStatus = ISolver::UNCHANGED_SIGN;;
// Alle Elemente im ZeroFunction-Array durchgehen
for (int i=0; i<_dimZeroFunc; ++i)
{
// Überprüfung auf Vorzeichenwechsel
if ((_zeroVal[i] < 0 && _zeroValLastSuccess[i] > 0) || (_zeroVal[i] > 0 && _zeroValLastSuccess[i] < 0))
{
// Vorzeichenwechsel, aber Eintrag ist größer (oder kleiner) als Toleranzbereich
_zeroStatus = ISolver::EQUAL_ZERO;
// Rest ZeroSign
_events[i] = true;
// Zeitpunkt des letzten verworfenen Schrittes abspeichern
_tLastUnsucess = _tCurrent;
break;
}
else
_events[i] = false;
}
}
void SolverDefaultImplementation::writeToFile(const int& stp, const double& t, const double& h)
{
#ifdef RUNTIME_PROFILING
MEASURETIME_REGION_DEFINE(solverWriteOutputHandler, "solverWriteOutput");
if(MeasureTime::getInstance() != NULL)
{
MEASURETIME_START(writeFunctionStartValues, solverWriteOutputHandler, "solverWriteOutput");
}
#endif
LOGGER_STATUS("Running", t, h);
if(_settings->getGlobalSettings()->getOutputPointType()!= OPT_NONE)
{
IWriteOutput* writeoutput_system = dynamic_cast<IWriteOutput*>(_system);
if(_outputCommand & IWriteOutput::WRITEOUT)
{
writeoutput_system->writeOutput(_outputCommand);
}
}
checkTimeout();
#ifdef RUNTIME_PROFILING
if(MeasureTime::getInstance() != NULL)
{
MEASURETIME_END(writeFunctionStartValues, writeFunctionEndValues, (*measureTimeFunctionsArray)[0], solverWriteOutputHandler);
}
#endif
}
void SolverDefaultImplementation::updateEventState()
{
dynamic_cast<IEvent*>(_system)->getZeroFunc(_zeroVal);
setZeroState();
if (_zeroStatus == ISolver::ZERO_CROSSING) // An event triggered an other event
{
_tLastSuccess = _tCurrent; // Concurrently occured events are in the time tollerance
setZeroState(); // Upate status of events vector
}
}
/** @} */ // end of coreSolver