forked from Yurlungur/wave_equation
/
rkf45.cpp
1090 lines (977 loc) · 33.3 KB
/
rkf45.cpp
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// rkf45.cpp
// Author: Jonah Miller (jonah.maxwell.miller@gmail.com)
// Time-stamp: <2013-11-23 22:26:54 (jonah)>
// This is my implementation of the 4-5 Runge-Kutta-Fehlberg adaptive
// step size integrator. For simplicity, and so that I can bundle
// public and private methods together, I take an object-oriented
// approach.
// For background and usage, see the header file.
// ----------------------------------------------------------------------
// Includes
#include <vector> // for output and internal variables
#include <iostream> // For printing a given ode system
#include <iomanip> // For manipulating the output.
#include <float.h> // For machine precision information
#include <cmath> // for math
#include <cassert> // For error checking
#include <iomanip> // for controlling io
#include "rkf45.hpp" // the header for this program
// Namespace specification. For convenience.
using std::cout;
using std::endl;
using std::vector;
using std::ostream;
using std::istream;
using std::right;
using std::abs;
// Constructors
// ----------------------------------------------------------------------
// Creates an empty integrator, to be initialized later.
RKF45::RKF45() {
set_defaults();
}
// Creates an integrator for the ode system with y'=f(t,y). The other
// properties are assumed to be set by setter methods. The size of
// the vector is assumed to be appropriate.
RKF45::RKF45(dVector (*y)(double,const dVector&)) {
set_defaults();
set_f(y);
}
// Creates an integrator for the ode system with y'=f(t,y), where
// f(t,y) = f(t,y,optional_args). The size of the vectors is assumed
// to be appropriate.
RKF45::RKF45(dVector (*y)(double,const dVector&,const dVector&)) {
set_defaults();
set_f(y);
}
// Creates an integrator for an ode system with y'=f(t,y), with
// initial time t0 and initial conditions y0. The size of the system
// is inferred from the size of the initial conditions vector.
RKF45::RKF45(dVector (*y)(double, const dVector&), double t0,
const dVector& y0) {
set_defaults();
set_f(y);
set_t0(t0);
set_y0(y0);
set_dt0();
}
// Creates an integrator for an ode system with
// y'=f(t,y)=f(t,y,optional_args), with initial time t0 and initial
// conditions y0. The size of the system is inferred from the size
// of the initial conditions vector. The optional_args are passed in
// now too. The size is inferred from the input vector.
RKF45::RKF45(dVector (*y)(double,const dVector&,const dVector&),
double t0, const dVector& y0,
const dVector& optional_args) {
set_defaults();
set_f(y);
set_t0(t0);
set_y0(y0);
set_optional_args(optional_args);
set_dt0();
}
// Creates an integrator of an ode system with y'=f(t,y), initial
// time t0, initial conditions y0, initial step size delta_t0,
// relative error tolerance relative_tolerance, and absolute error
// tolerance absolute_tolerance. The dimension of the system is
// inferred from the size of y0.
RKF45::RKF45(dVector (*y)(double,const dVector&),
double t0, const dVector& y0,
double delta_t0, double relative_tolerance,
double absolute_tolerance) {
set_defaults();
set_f(y);
set_t0(t0);
set_y0(y0);
set_dt0(delta_t0);
set_absolute_error(absolute_tolerance);
set_relative_error_factor(relative_tolerance);
}
// Creates an integrator of an ode system with y'=f(t,y), initial
// time t0 = 0, initial conditions y0, initial step size delta_t0,
// relative error tolerance relative_tolerance, and absolute error
// tolerance absolute_tolerance. The size of the system is inferred
// from the initial data.
RKF45::RKF45(dVector (*y)(double,const dVector&),
const dVector& y0, double delta_t0,
double relative_tolerance, double absolute_tolerance) {
set_defaults();
set_f(y);
set_y0(y0);
set_dt0(delta_t0);
set_absolute_error(absolute_tolerance);
set_relative_error_factor(relative_tolerance);
}
// Creates an integrator of an ode system with y'=f(t,y), initial
// time t0, initial conditions y0, initial step size delta_t0,
// relative error tolerance relative_tolerance, and absolute error
// tolerance absolute_tolerance. f(t,y) =
// f(t,y,optional_args). There are m-optional arguments. The
// dimension of the system and the number of optional arguments are
// inferred from size of the vectors.
RKF45::RKF45(dVector (*y)(double, const dVector&,const dVector&),
double t0,
const dVector& y0, const dVector& optional_args,
double delta_t0,
double relative_tolerance,double absolute_tolerance) {
set_defaults();
set_f(y);
set_t0(t0);
set_y0(y0);
set_dt0(delta_t0);
set_absolute_error(absolute_tolerance);
set_relative_error_factor(relative_tolerance);
set_optional_args(optional_args);
}
// Creates an integrator of an ode system with y'=f(t,y), initial
// time t0=0, initial conditions y0, initial step size delta_t0,
// relative error tolerance relative_tolerance, and absolute error
// tolerance absolute_tolerance. f(t,y) = f(t,y,optional_args).
RKF45::RKF45(dVector (*y)(double,const dVector&,const dVector&),
const dVector& y0, const dVector& optional_args,
double delta_t0,
double relative_tolerance, double absolute_tolerance) {
set_defaults();
set_f(y);
set_y0(y0);
set_optional_args(optional_args);
set_dt0(delta_t0);
set_relative_error_factor(relative_tolerance);
set_absolute_error(absolute_tolerance);
}
// Creates an integrator of an ode system with y'=f(t,y), initial
// time t0, initial conditions y0, and initial step size
// delta_t0. The error tolerance is the default error tolerance.
RKF45::RKF45(dVector (*y)(double,const dVector&),
double t0, const dVector y0, double delta_t0) {
set_defaults();
set_f(y);
set_t0(t0);
set_y0(y0);
set_dt0(delta_t0);
}
// Creates an integrator of the ode system with y'=f(t,y), initial
// time t0, initial conditions y0, initial step size delta_t0.
// f(t,y) = f(t,y,optional_args). The error tolerance is the
// default. f(t,y) = f(t,y,optional_args)
RKF45::RKF45(dVector (*y)(double,const dVector&,const dVector&),
double t0, const dVector& y0, const dVector& optional_args,
double delta_t0) {
set_defaults();
set_f(y);
set_t0(t0);
set_y0(y0);
set_optional_args(optional_args);
set_dt0(delta_t0);
}
// Copy constructor. Generates a copy of the integrator as it was
// before the first step was made. Includes all initial data. Useful
// for the shooting method.
RKF45::RKF45(const RKF45 &integrator) {
set_defaults();
set_f(integrator.get_f());
set_t0(integrator.get_t0());
set_y0(integrator.get_y0());
set_max_dt(integrator.get_max_dt());
set_dt0(integrator.get_dt0());
set_absolute_error(integrator.get_absolute_error());
set_relative_error_factor(integrator.get_relative_error_factor());
set_safety_margin(integrator.get_safety_margin());
if ( integrator.using_optional_args() ) {
set_optional_args(integrator.get_optional_args());
}
}
// ES: There are many constructors that look very similar to each
// other, and just differ in their arguments. Instead, you can define
// a new class "integrator options", and define a default initializer
// for this class that sets good default values. Additional setter
// methods allow the user to modify the integrator options. The RKF45
// constructor then takes such an integrator option argument, maybe
// defaulting to NULL which would use the default integrator options.
// Assignment operator. Copies one object into another like the copy
// constructor. Useful for the shooting method. Only copies initial
// data. Integration data is not copied.
RKF45& RKF45::operator= (const RKF45 &integrator) {
set_defaults();
set_f(integrator.get_f());
set_t0(integrator.get_t0());
set_y0(integrator.get_y0());
set_max_dt(integrator.get_max_dt());
set_dt0(integrator.get_dt0());
set_absolute_error(integrator.get_absolute_error());
set_relative_error_factor(integrator.get_relative_error_factor());
set_safety_margin(integrator.get_safety_margin());
if ( integrator.using_optional_args() ) {
set_optional_args(integrator.get_optional_args());
}
return *this;
}
// Destructor. Empty/
RKF45::~RKF45() { }
// ----------------------------------------------------------------------
// Getters
// ----------------------------------------------------------------------
// Returns the total number of steps the integrated has iterated through
int RKF45::steps() const {
return ys.size();
}
// Finds the error tolerance based on the current state of the system.
double RKF45::get_error_tolerance() const {
return get_relative_error_tolerance() + get_absolute_error();
}
// Returns the size of the system. Assumes the system stays the same
// size. If initial data has not yet been set, will return zero.
int RKF45::size() const {
if ( ys.size() > 0 ) {
return ys.front().size();
}
else {
return 0;
}
}
// Returns a container class containing the function and whether or
// not optional arguments are in use.
Functor RKF45::get_f() const {
Functor output;
if ( using_optional_args() ) {
output.use_optional_arguments = true;
output.f_with_optional_args = f_with_optional_args;
}
else {
output.use_optional_arguments = false;
output.f_no_optional_args = f_no_optional_args;
}
return output;
}
// Returns the initial data. Returns an empty vector if no initial
// data has been set yet. Does NOT pass by reference. If no initial
// data has been set, returns an empty vector.
dVector RKF45::get_y0() const {
dVector output;
if ( size() > 0 ) {
output = y0;
}
return output;
}
// Returns the start time.
double RKF45::get_t0() const {
return t0;
}
// Returns true if the system is set to use optional arguments,
// false otherwise.
bool RKF45::using_optional_args() const {
return use_optional_arguments;
}
// Returns the optional arguments if they are in use. If they are
// not in use or if they haven't been set yet, returns an empty
// array otherwise.
dVector RKF45::get_optional_args() const {
dVector output;
if ( using_optional_args() ) {
output = optional_args;
}
return output;
}
// Returns the maximum step size allowed.
double RKF45::get_max_dt() const {
return max_dt;
}
// Returns the initial step size.
double RKF45::get_dt0() const {
return dt0;
}
// Returns the previous step size. If no steps have been performed,
// returns -1.
double RKF45::get_last_dt() const {
if ( steps() > 0 ) {
return last_dt;
}
else {
return -1;
}
}
// Returns the next step size.
double RKF45::get_next_dt() const {
return next_dt;
}
// Returns the current absolute error
double RKF45::get_absolute_error() const {
return absolute_error;
}
// Returns the current relative error factor
double RKF45::get_relative_error_factor() const {
return relative_error_factor;
}
// Returns the safety margin for step-size choice
double RKF45::get_safety_margin() const {
return safety_margin;
}
// Check the consistency of the butcher tableau
void RKF45::check_consistency() const {
double sum_of_as;
bool is_consistent = true;
if ( debug_level >= 3 ) {
cout << "Checking if Butcher tableau is consistent." << endl;
}
for (int i = 0; i < ORDER; i++) {
sum_of_as = 0;
for (int j = 0; j < i; j++) {
sum_of_as += BUTCHER_A[i][j];
}
if ( debug_level >= 3 ) {
cout << "Row " << i << ": "
<< sum_of_as - BUTCHER_C[i] << endl;
}
is_consistent = is_consistent
&& (sum_of_as - BUTCHER_C[i] - default_min_dt() );
assert ( is_consistent && "Butcher tableau is not consistent!" );
}
if ( debug_level >= 3 ) {
cout << "Butcher tableau is consistent." << endl;
}
}
// Get whether or not we are using 5th order terms
bool RKF45::using_5th_order() const {
return use_5th_order;
}
// ----------------------------------------------------------------------
// Setters
// ----------------------------------------------------------------------
// Sets the function y'=f. One version takes optional arguments. One
// does not. The second vector is optional arguments.
void RKF45::set_f(dVector (*f)(double,const dVector&)) {
use_optional_arguments = false;
f_no_optional_args = f;
}
void RKF45::set_f(dVector (*f)(double,const dVector&,const dVector&)) {
use_optional_arguments = true;
f_with_optional_args = f;
}
void RKF45::set_f(Functor f_func) {
if ( f_func.use_optional_arguments ) {
set_f(f_func.f_with_optional_args);
}
else {
set_f(f_func.f_no_optional_args);
}
}
// Sets the initial y vector.
void RKF45::set_y0(const dVector& y0) {
this->y0 = y0;
if ( ys.size() == 0 ) {
ys.push_back(y0);
}
else {
ys[0] = y0;
}
// The initial error vector must be zero.
if ( errors.size() == 0) {
errors.push_back(0);
}
else {
errors[0] = 0;
}
}
// Sets the start time. Passing in no arguments resets to the
// default.
void RKF45::set_t0(double t0) {
this->t0 = t0;
if ( ts.size() == 0 ) {
ts.push_back(t0);
}
else {
ts[0] = t0;
}
}
void RKF45::set_t0() {
set_t0(DEFAULT_T0);
}
// Sets the optional arguments. If the function f does not take
// optional arguments, raises an error.
void RKF45::set_optional_args(const dVector& optional_args) {
assert ( using_optional_args()
&& "Set the function to take optional arguments first." );
this->optional_args = optional_args;
}
// Sets the maximum allowed step size. Passing in no arguments
// resets to the default.
void RKF45::set_max_dt(double max_dt) {
assert ( max_dt > 0 && "Steps must be positive." );
if ( max_dt < get_next_dt() && debug_level >= 1 ) {
// Don't do anything about it since its not a fatal error. But
// warn the user.
cout << "WARNING: Next step size larger than max step size." << "\n"
<< "\tMax step size: " << max_dt << "\n"
<< "\tNext step size: " << get_next_dt() << endl;
}
this->max_dt = max_dt;
}
void RKF45::set_max_dt() {
set_max_dt(default_max_dt());
}
// Sets the minimum allowed step size.
void RKF45::set_min_dt(double min_dt) {
assert (min_dt > 0 && "Steps must be positive." );
if (min_dt < get_next_dt() ) {
// Don't do anything about it since its not a fatal error. But
// warn the user.
cout << "WARNING: Next step size smaller than min step size." << endl;
}
this->min_dt = min_dt;
}
void RKF45::set_min_dt() {
set_min_dt(default_min_dt());
}
// Sets the initial step size. Passing in no arguments resets to the
// default. If the input dt0 is too big, sets to max_dt.
void RKF45::set_dt0(double dt0) {
assert ( dt0 > 0 && "Steps must be positive." );
if ( dt0 > get_max_dt() ) {
this->dt0 = get_max_dt();
}
else if ( dt0 < get_min_dt() ) {
this->dt0 = get_min_dt();
}
else {
this->dt0 = dt0;
}
// By definition, the first next_dt needs to be dt0.
if ( ys.size() < 2 ) {
set_next_dt(this->dt0);
}
}
// Sets the initial step size automatically. The function and initial
// y data must be set for this to work. f, t0 and y0 must be set
// before you can call this method. Also sets the max dt to the same
// value.
void RKF45::set_dt0() {
// locals
double yprime = norm(f(t0,y0));
double y = norm(y0);
double length_scale;
if ( debug_level >= 3) {
cout << "y = " << y << "\n" << "yprime = " << yprime << endl;
}
// y/y' = <change in t> * y/<change in y>. A good approximation of dt0.
length_scale = y/yprime;
if ( debug_level >= 3 ) {
cout << "length scale = " << length_scale << endl;
}
dt0 = abs(get_relative_error_factor() * length_scale);
if ( debug_level >= 1 ) {
cout << "dt0 = " << dt0 << endl;
// cout << "Max step size: " << get_max_dt() << endl;
}
// dt0 must be a finite, positive number. If it's zero or NaN, make
// it sqrt(machine epsilon).
if ( !(dt0 > 0) ) {
if ( debug_level >= 1 ) {
cout << "WARNING: dt0 non-finite. Setting it to the default minimum dt"
<< endl;
}
set_dt0(default_min_dt());
}
else {
set_dt0(dt0);
}
}
// Sets the maximum change in dt. No choice sets it to the default.
void RKF45::set_max_delta_dt(double max_delta) {
assert ( 0 < max_delta
&& "max change in dt is current dt multiplied by max_delta_dt. So max_delta_dt must be positive." );
max_delta_dt = max_delta;
}
void RKF45::set_max_delta_dt() {
set_max_delta_dt(DEFAULT_MAX_DELTA_DT);
}
// Sets the next step size. Use with caution! If you set the next
// step size, the value chosen automatically is forgotten!
void RKF45::set_next_dt(double next_dt) {
if ( ! (next_dt > 0) && ( debug_level >= 1 || steps() < 2 ) ) {
cout << "WARNING: Steps must be positive!" << endl;
}
if ( next_dt > get_max_dt() ) {
this->next_dt = get_max_dt();
}
else if ( next_dt < get_min_dt() || ! ( next_dt > 0 ) ) {
this->next_dt = get_min_dt();
}
else {
this->next_dt = next_dt;
}
}
// Sets the absolute error. Passing in no arguments resets to the
// default.
void RKF45::set_absolute_error(double absolute_error) {
assert ( absolute_error >= 0 && "Error must be non-negative." );
this->absolute_error = absolute_error;
}
void RKF45::set_absolute_error() {
set_absolute_error(default_absolute_error());
}
// Sets the relative error factor. Passing in no arguments resets to
// the default.
void RKF45::set_relative_error_factor(double relative_error_factor) {
assert ( relative_error_factor >= 0 && "Error must be non-negative." );
this->relative_error_factor = relative_error_factor;
}
void RKF45::set_relative_error_factor() {
set_relative_error_factor(DEFAULT_RELATIVE_ERROR_FACTOR);
}
// Sets the safety margin for step size choices. Passing in no
// arguments resets to the default.
void RKF45::set_safety_margin(double safety_margin) {
assert ( safety_margin > 0 && "Error must be positive." );
this->safety_margin = safety_margin;
}
void RKF45::set_safety_margin() {
set_safety_margin(DEFAULT_SAFETY_MARGIN);
}
// Set use fifth order terms. No arguments sets it to the default.
// We can choose to return fifth-order terms instead of fourth-order
// terms in the Runge-Kutta approximation to make the system
// fifth-order accurate. However, there is much less of a guarantee
// that the error will be truncated correctly. This is mostly for
// testing purposes. It also works if the user knows the appropriate
// step size. Default is false.
void RKF45::set_use_5th_order_terms(bool use_terms) {
this->use_5th_order = use_terms;
}
void RKF45::set_use_5th_order_terms() {
set_use_5th_order_terms(DEFAULT_USE_5TH_ORDER);
}
// ----------------------------------------------------------------------
// Data Output
// ----------------------------------------------------------------------
// Returns the current time step.
double RKF45::get_t() const {
return ts.back();
}
// Returns the time after n steps.
double RKF45::get_t(int n) const {
assert ( n < (int)ts.size() && "We have not integrated that far yet!" );
return ts[n];
}
// Returns the y vector after n steps. Passes by value, so very
// slow.
dVector RKF45::get_y(int n) const {
assert ( n < (int)ts.size() && "We have not integrated that far yet!" );
return ys[n];
}
// Returns the current state of the y vector. Passes by value, so
// very slow.
dVector RKF45::get_y() const {
return ys.back();
}
// Fills an input vector with the current state of the y
// vector.
void RKF45::get_y(dVector& y_data) const {
y_data = ys.back();
}
// Fills an input vector with the state of the y vector after n
// steps.
void RKF45::get_y(dVector& y_data, int n) const {
assert ( n < (int)ts.size() && "We have not integrated that far yet!" );
y_data = ys[n];
}
// Fills an input array with the current state of the y
// vector. Assumes that the array is the appropriate size. If the
// array size is wrong, you will sefgault.
void RKF45::get_y(double y_data[]) const {
for (unsigned int i = 0; i < ys.back().size(); i++) {
y_data[i] = ys.back()[i];
}
}
// Returns the local truncation error after n steps. Entering no
// value returns the most recent truncation error.
double RKF45::get_local_truncation_error(int n) const {
return errors[n];
}
double RKF45::get_local_truncation_error() const {
return errors.back();
}
// Tests whether or not a given set of constraints is
// satisfied. Takes a boolean function of the vector y and tests
// whether y(t) satisfies the constraint function. This is the fast,
// safe solution to the problem of monitoring constriants. t is
// chosen as t after n time steps.
bool RKF45::test_constraints(bool (*constraints)(double,const dVector),
int n) const {
assert ( n < (int)ts.size() && "We have not integrated that far yet!" );
return constraints(ts[n],ys[n]);
}
// Tests whether or not a given set of constraints is
// satisfied. Takes a boolean function of the vector y and tests
// whether y(t) satisfies the constraint function. t is set to the
// current time. This is the fast, safe solution to the problem of
// monitoring constriants.
bool RKF45::test_constraints(bool (*constraints)(double,const dVector)) const {
return constraints(ts.back(),ys.back());
}
// Tests by how much the y vector at time t fails to satisfy the
// constraint function passed in. The constraint function should
// return a vector<double> where each element shows how much the y
// vector failed to satisfy the appropriate constraint
// equation.
dVector RKF45::test_constraint_degree(dVector (*constraints)(double,const dVector)) const {
return constraints(ts.back(),ys.back());
}
// Evaluates a function double function on the y vector and on the
// time and returns the output of the function. Useful for energy
// methods. Uses the most recent time by default. But any time is
// possible. Feed the number of time steps n in last
double RKF45::eval_function(double (*to_eval)(double,const dVector)) const {
return to_eval(ts.back(),ys.back());
}
double RKF45::eval_function(double (*to_eval)(double,const dVector),
int n) const {
return to_eval(ts[n],ys[n]);
}
// Tests by how much the y vector at time t fails to satisfy the
// constraint function passed in. The constraint function should
// return a vector<double> where each element shows how much the y
// vector failed to satisfy the appropriate constraint
// equation. Passed by reference.
dVector RKF45::test_constraint_degree(dVector (*constraints)(double,const dVector), int n) const {
assert ( n < (int)ts.size() && "We have not integrated that far yet!" );
return constraints(ts[n],ys[n]);
}
// Prints the integration history to an output stream. No stream
// choice means it prints to cout. This is not the just the current
// state of the system. This is everything.
void RKF45::print(ostream& out) const {
out << "#<time>\t<y vector>\t<local error>" << endl;
for (unsigned int i = 0; i < ts.size(); i++) {
out << ts[i] << " ";
for (unsigned int j = 0; j < ys[i].size() - 1; j++) {
out << ys[i][j] << " ";
}
out << ys[i].back() << " ";
out << errors.back();
out << endl;
}
}
void RKF45::print() const {
print(cout);
}
// Prints the current state of the system to ostream out. For
// debugging really.
void RKF45::print_state(ostream& out) const {
out << "#<time>\t<y vector>\t<local error>" << endl;
out << ts.back() << " ";
for (unsigned int i = 0; i < ys.back().size() - 1; i++) {
out << ys.back()[i] << " ";
}
out << ys.back().back() << " ";
out << errors.back();
out << endl;
}
void RKF45::print_state() const {
print_state(cout);
}
// Overload the stream input operator. Works like print(out).
ostream& operator <<(ostream& out, const RKF45& in) {
in.print(out);
return out;
}
// Print the settings and current state of the system to the given
// stream. Useful for debugging. Default stream is cout.
void RKF45::print_settings(ostream& s) const {
s << "Current system settings:\n"
<< "\tAbsolute error: " << get_absolute_error() << "\n"
<< "\tRelative error factor: " << get_relative_error_factor() << "\n"
<< "Current system status\n"
<< "\tNumber of steps: " << steps() << "\n"
<< "\tdt0: " << get_dt0() << "\n"
<< "\tNext dt: " << get_next_dt() << "\n"
<< "\tMax delta dt factor: " << get_max_delta_dt_factor() << "\n"
<< "----------------------------------------------------\n"
<< "current system state:\n";
print_state(s);
s << endl;
}
void RKF45::print_settings() const {
print_settings(cout);
}
// ----------------------------------------------------------------------
// integration control
// ----------------------------------------------------------------------
// Integrates by a single time step. The time step is either chosen
// internally from the last integration step or set by set_dt0 or
// set_next_dt.
void RKF45::step() {
// If this is the first step and if dt0 is still the special
// defualt, choose a dt0 automatically.
if ( steps() < 2 && get_dt0() == DEFAULT_DT0 ) {
set_dt0();
if ( debug_level >= 1 ) {
cout << "WARNING: No dt0 set. Initializing a reasonable value.\n"
<< "Chosen value: "
<< get_dt0()
<< endl;
}
}
// A few convenience terms
double h = get_next_dt();
double t = ts.back();
// The k vectors
dVector k[ORDER];
// The new orders for the dynamic step size
dVector y_new_order4;
dVector y_new_order5;
double new_step_size;
double error;
// Calculate the error from a step. If therror is unacceptably large,
// try again and shrink the step size.
do {
// Make the k terms.
make_ks(k,h);
// Now we can safely compute the new y values.
y_new_order4 = ys.back();
y_new_order5 = ys.back();
for (int i = 0; i < ORDER; i++) {
linear_combination_in_place(y_new_order4,BUTCHER_B[1][i],k[i]);
linear_combination_in_place(y_new_order5,BUTCHER_B[0][i],k[i]);
}
error = norm_difference(y_new_order5,y_new_order4);
if ( debug_level >= 2 ) {
cout << "\tSafety margin: " << get_safety_margin() << endl;
cout << "\tCurrent step size: " << h << endl;
cout << "\tError tolerance: " << get_error_tolerance() << endl;
cout << "\ty order 4: [ ";
for (dVector::const_iterator it=y_new_order4.begin();
it != y_new_order4.end(); ++it) {
cout << (*it) << " ";
}
cout << "]" << endl;
cout << "\ty order 5: [ ";
for (dVector::const_iterator it = y_new_order5.begin();
it != y_new_order5.end(); ++it) {
cout << (*it) << " ";
}
cout << "]" << endl;
cout << "\tDifference: " << error << endl;
}
if ( error > get_error_tolerance() ) {
h = h/2.0;
if ( debug_level >= 2 ) {
cout << "Step size too large. Reducing by factor of 2." << endl;
}
}
} while ( error > get_error_tolerance() );
// Then we can compute the next step size
new_step_size = calculate_new_step_size(y_new_order5,y_new_order4,h);
// Finally, set the new step size and report it.
set_next_dt(new_step_size);
if ( debug_level >= 2 ) {
cout << "New step size: " << get_next_dt() << endl;
}
// set y and t, and end.
last_dt = h;
ts.push_back(t+h);
ys.push_back( using_5th_order() ? y_new_order5 : y_new_order4 );
errors.push_back(error);
if (debug_level >= 2 ) {
print_state();
}
}
// Integrates up to to time t_final. If the current time is after
// t_final, does nothing. The initial step size is set by set_dt0 or
// by set_next_dt.
void RKF45::integrate(double t_final) {
while ( ts.back() < t_final ) {
step();
}
}
// Resets the integrator to t0. This is useful for approaches like
// the shooting method.
void RKF45::reset() {
ys.resize(0);
ys.push_back(y0);
ts.resize(0);
ts.push_back(t0);
errors.resize(0);
errors.push_back(0);
set_next_dt(get_dt0());
assert ( ys.size() == 1 && ts.size() == 1
&& "Reset succesfull." );
}
// ----------------------------------------------------------------------
// Private methods
// ----------------------------------------------------------------------
// A convenience function. Sets all the fields to their default values.
void RKF45::set_defaults() {
set_use_5th_order_terms(DEFAULT_USE_5TH_ORDER);
t0 = DEFAULT_T0;
use_optional_arguments = DEFAULT_USE_OPTIONAL_ARGS;
max_dt = default_max_dt();
dt0 = DEFAULT_DT0;
next_dt = dt0;
absolute_error = default_absolute_error();
relative_error_factor = DEFAULT_RELATIVE_ERROR_FACTOR;
safety_margin = DEFAULT_SAFETY_MARGIN;
debug_level = DEFAULT_DEBUG_LEVEL;
min_dt = default_min_dt();
set_max_delta_dt();
check_consistency();
}
// A convenience function. Wraps the function f = y'. Depending on
// whether or not f takes optional arguments does the correct thing.
dVector RKF45::f(int t, const dVector& y) const {
if ( using_optional_args() ) {
return f_with_optional_args(t,y,optional_args);
}
else {
return f_no_optional_args(t,y);
}
}
// Adds two dVectors a and b and outputs a new dVector that is their
// sum. Assumes that the vectors are the same size. If they're not,
// raises an error.
dVector RKF45::sum(const dVector& a, const dVector& b) const {
assert ( a.size() == b.size()
&& "To sum two vectors, they must be the same size.");
dVector output;
int size = a.size();
output.resize(size);
for (int i = 0; i < size; i++) {
output[i] = a[i] + b[i];
}
return output;
}
// Adds the dVector b to the dVector a. a is modified in-place.
void RKF45::sum_in_place(dVector& a, const dVector& b) const {
assert ( a.size() == b.size()
&& "To sum two vectors, they must be the same size.");
for (unsigned int i = 0; i < a.size(); i++) {
a[i] += b[i];
}
}
// Adds k*b to the dVector a. k is a scalar. b is a dVector.
void RKF45::linear_combination_in_place(dVector& a,
double k, const dVector& b) const {
assert ( a.size() == b.size()
&& "To sum two vectors, they must be the same size.");
for (unsigned int i = 0; i < a.size(); i++) {
a[i] += k*b[i];
}
}
// Takes the norm of the difference between two dVectors.
double RKF45::norm_difference(const dVector& a, const dVector& b) const {
assert ( a.size() == b.size()
&& "To sum two vectors, they must be the same size.");
double output = 0;
for (unsigned int i = 0; i < a.size(); i++) {
output += (a[i] - b[i]) * (a[i] - b[i]);
}
return sqrt(output);
}
// Subtracts dVector b from a. Outputs a new vector that is their
// difference. Assumes that the vectors are the same size. If
// they're not, raises an error.
dVector RKF45::difference(const dVector& a, dVector& b) const {
return sum(a,scalar_multiplication(-1,b));
}
// Takes the scalar multiplicationof a dVector v with a scalar k
dVector RKF45::scalar_multiplication(double k, const dVector& v) const {
dVector output;
for (dVector::const_iterator it=v.begin(); it != v.end(); ++it) {
output.push_back(k * (*it));
}
return output;
}
// Calculuates the sum of all elements in a dVector.
double RKF45::sum(const dVector& v) const {
double output = 0;
for (dVector::const_iterator it=v.begin(); it != v.end(); ++it) {
output += (*it);
}
return output;
}
// Calculates the 2-norm of the dVector v
double RKF45::norm(const dVector& v) const {
double output = 0;
for (dVector::const_iterator it=v.begin(); it != v.end(); ++it) {
output += (*it) * (*it);
}
return sqrt(output);