49 std::vector<double>& rYValues,
50 std::vector<double>& rWorkingMemory,
59 mError.resize(number_of_variables);
60 mk1.resize(number_of_variables);
61 mk2.resize(number_of_variables);
62 mk3.resize(number_of_variables);
63 mk4.resize(number_of_variables);
64 mk5.resize(number_of_variables);
65 mk6.resize(number_of_variables);
66 myk2.resize(number_of_variables);
67 myk3.resize(number_of_variables);
68 myk4.resize(number_of_variables);
69 myk5.resize(number_of_variables);
70 myk6.resize(number_of_variables);
72 double current_time = startTime;
73 double time_step = maxTimeStep;
74 bool got_to_end =
false;
75 bool accurate_enough =
false;
76 unsigned number_of_time_steps = 0;
80 rSolution.
rGetTimes().push_back(current_time);
89 while (!accurate_enough)
91 accurate_enough =
true;
101 double max_error = *std::max_element(
mError.begin(),
mError.end());
103 if (max_error > tolerance)
105 accurate_enough =
false;
111 current_time = current_time + time_step;
117 rSolution.
rGetTimes().push_back(current_time);
119 number_of_time_steps++;
124 AdjustStepSize(time_step, max_error, tolerance, maxTimeStep, minTimeStep);
128 if (current_time + time_step > endTime)
130 time_step = endTime - current_time;
134 rWorkingMemory) ==
true)
146 rYValues.assign(rWorkingMemory.begin(), rWorkingMemory.end());
147 accurate_enough =
false;
156 std::vector<double>& rCurrentYValues,
157 std::vector<double>& rNextYValues)
162 std::vector<double>& dy = rNextYValues;
166 for (
unsigned i=0; i<num_equations; i++)
168 mk1[i] = timeStep*dy[i];
169 myk2[i] = rCurrentYValues[i] + 0.25*
mk1[i];
173 for (
unsigned i=0; i<num_equations; i++)
175 mk2[i] = timeStep*dy[i];
176 myk3[i] = rCurrentYValues[i] + 0.09375*
mk1[i] + 0.28125*
mk2[i];
180 for (
unsigned i=0; i<num_equations; i++)
182 mk3[i] = timeStep*dy[i];
188 for (
unsigned i=0; i<num_equations; i++)
190 mk4[i] = timeStep*dy[i];
196 for (
unsigned i=0; i<num_equations; i++)
198 mk5[i] = timeStep*dy[i];
204 for (
unsigned i=0; i<num_equations; i++)
206 mk6[i] = timeStep*dy[i];
215 const double& rError,
216 const double& rTolerance,
217 const double& rMaxTimeStep,
218 const double& rMinTimeStep)
221 double delta = pow(rTolerance/(2.0*rError), 0.25);
226 rCurrentStepSize *= 0.1;
228 else if (delta >= 4.0)
230 rCurrentStepSize *= 4.0;
234 rCurrentStepSize *= delta;
237 if (rCurrentStepSize > rMaxTimeStep)
239 rCurrentStepSize = rMaxTimeStep;
242 if (rCurrentStepSize < rMinTimeStep)
244 std::cout <<
"rCurrentStepSize = " << rCurrentStepSize <<
"\n" << std::flush;
245 std::cout <<
"rMinTimeStep = " << rMinTimeStep <<
"\n" << std::flush;
247 EXCEPTION(
"RKF45 Solver: Ode needs a smaller timestep than the set minimum\n");
258 : m1932o2197(1932.0/2197.0),
259 m7200o2197(7200.0/2197.0),
260 m7296o2197(7296.0/2197.0),
262 m439o216(439.0/216.0),
263 m3680o513(3680.0/513.0),
264 m845o4104(845.0/4104.0),
266 m3544o2565(3544.0/2565.0),
267 m1859o4104(1859.0/4104.0),
269 m128o4275(128.0/4275.0),
270 m2197o75240(2197.0/75240.0),
273 m1408o2565(1408.0/2565.0),
274 m2197o4104(2197.0/4104.0)
279 std::vector<double>& rYValues,
286 assert(endTime > startTime);
287 assert(timeStep > 0.0);
292 EXCEPTION(
"(Solve with sampling) Stopping event is true for initial condition");
295 std::vector<double> working_memory(rYValues.size());
299 bool return_solution =
true;
300 InternalSolve(solutions, pOdeSystem, rYValues, working_memory, startTime, endTime, timeStep, 1e-5, tolerance, return_solution);
305 std::vector<double>& rYValues,
311 assert(endTime > startTime);
312 assert(timeStep > 0.0);
317 EXCEPTION(
"(Solve without sampling) Stopping event is true for initial condition");
320 std::vector<double> working_memory(rYValues.size());
323 bool return_solution =
false;
324 InternalSolve(not_required_solution, pOdeSystem, rYValues, working_memory, startTime, endTime, timeStep, 1e-4, 1e-5, return_solution);
void InternalSolve(OdeSolution &rSolution, AbstractOdeSystem *pAbstractOdeSystem, std::vector< double > &rCurrentYValues, std::vector< double > &rWorkingMemory, double startTime, double endTime, double maxTimeStep, double minTimeStep, double tolerance, bool outputSolution)