forked from FloatingArrayDesign/MoorDyn
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTime.hpp
More file actions
1537 lines (1383 loc) · 43.1 KB
/
Copy pathTime.hpp
File metadata and controls
1537 lines (1383 loc) · 43.1 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
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
/*
* Copyright (c) 2022, Jose Luis Cercos-Pita <jlc@core-marine.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/** @file Time.hpp
* C++ API for the time integration
*/
#pragma once
#include "Misc.hpp"
#include "IO.hpp"
#include "State.hpp"
#include "Line.hpp"
#include "Point.hpp"
#include "Rod.hpp"
#include "Body.hpp"
#include "Waves.hpp"
#include <vector>
#include <string>
namespace moordyn {
namespace time {
/** @class Scheme Time.hpp
* @brief Time scheme abstraction
*
* This class is helping mooring::MoorDyn to can consider every single time
* scheme in the very same way
*/
class Scheme : public io::IO
{
public:
/// @brief Destructor
virtual ~Scheme() {}
/** @brief Set the ground body
* @param obj The ground body
*/
inline void SetGround(Body* obj) { ground = obj; }
/** @brief Add a line
* @param obj The line
* @throw moordyn::invalid_value_error If it has been already registered
*/
virtual void AddLine(Line* obj)
{
if (std::find(lines.begin(), lines.end(), obj) != lines.end()) {
LOGERR << "The line " << obj->number << " was already registered"
<< endl;
throw moordyn::invalid_value_error("Repeated object");
}
lines.push_back(obj);
}
/** @brief Remove a line
* @param obj The line
* @return The index of the removed entity
* @throw moordyn::invalid_value_error If the line has not been registered,
* or it was already removed
*/
virtual unsigned int RemoveLine(Line* obj)
{
auto it = std::find(lines.begin(), lines.end(), obj);
if (it == lines.end()) {
LOGERR << "The line " << obj->number << " was not registered"
<< endl;
throw moordyn::invalid_value_error("Missing object");
}
const unsigned int i = std::distance(lines.begin(), it);
lines.erase(it);
return i;
}
/** @brief Add a point
* @param obj The point
* @throw moordyn::invalid_value_error If it has been already registered
*/
virtual void AddPoint(Point* obj)
{
if (std::find(points.begin(), points.end(), obj) != points.end()) {
LOGERR << "The point " << obj->number << " was already registered"
<< endl;
throw moordyn::invalid_value_error("Repeated object");
}
points.push_back(obj);
}
/** @brief Remove a point
* @param obj The point
* @return The index of the removed entity
* @throw moordyn::invalid_value_error If the point has not been
* registered, or it was already removed
*/
virtual unsigned int RemovePoint(Point* obj)
{
auto it = std::find(points.begin(), points.end(), obj);
if (it == points.end()) {
LOGERR << "The point " << obj->number << " was not registered"
<< endl;
throw moordyn::invalid_value_error("Missing object");
}
const unsigned int i = std::distance(points.begin(), it);
points.erase(it);
return i;
}
/** @brief Add a rod
* @param obj The rod
* @throw moordyn::invalid_value_error If it has been already registered
*/
virtual void AddRod(Rod* obj)
{
if (std::find(rods.begin(), rods.end(), obj) != rods.end()) {
LOGERR << "The rod " << obj->number << " was already registered"
<< endl;
throw moordyn::invalid_value_error("Repeated object");
}
rods.push_back(obj);
}
/** @brief Remove a rod
* @param obj The rod
* @return The index of the removed entity
* @throw moordyn::invalid_value_error If the rod has not been registered,
* or it was already removed
*/
virtual unsigned int RemoveRod(Rod* obj)
{
auto it = std::find(rods.begin(), rods.end(), obj);
if (it == rods.end()) {
LOGERR << "The rod " << obj->number << " was not registered"
<< endl;
throw moordyn::invalid_value_error("Missing object");
}
const unsigned int i = std::distance(rods.begin(), it);
rods.erase(it);
return i;
}
/** @brief Add a body
* @param obj The body
* @throw moordyn::invalid_value_error If it has been already registered
*/
virtual void AddBody(Body* obj)
{
if (std::find(bodies.begin(), bodies.end(), obj) != bodies.end()) {
LOGERR << "The body " << obj->number << " was already registered"
<< endl;
throw moordyn::invalid_value_error("Repeated object");
}
bodies.push_back(obj);
}
/** @brief Remove a body
* @param obj The body
* @return The index of the removed entity
* @throw moordyn::invalid_value_error If the body has not been registered,
* or it was already removed
*/
virtual unsigned int RemoveBody(Body* obj)
{
auto it = std::find(bodies.begin(), bodies.end(), obj);
if (it == bodies.end()) {
LOGERR << "The body " << obj->number << " was not registered"
<< endl;
throw moordyn::invalid_value_error("Missing object");
}
const unsigned int i = std::distance(bodies.begin(), it);
bodies.erase(it);
return i;
}
/** @brief Get the name of the scheme
* @return The name
*/
inline std::string GetName() const { return name; }
/** @brief Get the simulation time
* @return The time
*/
inline real GetTime() const { return t; }
/** @brief Set the simulation time
* @param time The time
* @note This method is also calling to Scheme::Next()
*/
inline void SetTime(const real& time)
{
t = time;
Next();
}
/** @brief Get the CFL factor
* @return The CFL factor
*/
inline real GetCFL() const { return cfl; }
/** @brief Set the CFL factor
* @param cfl The CFL factor
*/
inline void SetCFL(const real& cfl) { this->cfl = cfl; }
/** @brief Prepare everything for the next outer time step
*
* Always call this method before start calling Scheme::Step()
*/
inline void Next()
{
t_local = 0.0;
for (unsigned int i = 0; i < lines.size(); i++) {
lines[i]->updateUnstretchedLength();
}
}
/** @brief Create an initial state for all the entities
* @note Just the first state is written. None of the following states, nor
* the derivatives are initialized in any way.
* @note It is assumed that the coupled entities were already initialized
*/
virtual void Init() = 0;
/** @brief Run a time step
*
* This function is the one that must be specialized on each time scheme,
* but remember to call it at the end of the inherited function to increment
* Scheme::t_local
* @param dt Time step
*/
virtual void Step(real& dt) { t_local += dt; };
/** @brief Get the number of state variables
* @return The number of state variables
*/
virtual const unsigned int GetNState() const = 0;
/** @brief Get the number of state derivative variables
* @return The number of state derivative variables
*/
virtual const unsigned int GetNDeriv() const = 0;
/** @brief Get the state variable
* @param i The index of the state variable to take
* @return The state variable
* @{
*/
virtual moordyn::state::State GetState(unsigned int i = 0) = 0;
virtual moordyn::state::State* r(unsigned int i = 0) = 0;
/**
* @}
*/
/** @brief Resume the simulation from the stationary solution
* @param state The stationary solution
* @param i The index of the state variable to take
*/
inline virtual void SetState(const moordyn::state::State& state,
unsigned int i = 0) {};
/** @brief Save the system state on a file
* @param filepath The output file path
* @param i The index of the state variable to serialize
* @see ::SerializeState()
*/
inline virtual void SaveState(const std::string filepath,
unsigned int i = 0)
{
}
/** @brief Load the system state from a file
* @param filepath The output file path
* @param i The index of the state variable to overwrite
*/
inline virtual void LoadState(const std::string filepath,
unsigned int i = 0)
{
}
/** @brief Get the state derivative variable
* @param i The index of the state derivative variable to take
* @return The state derivative variable
* @{
*/
virtual moordyn::state::State GetDeriv(unsigned int i = 0) = 0;
virtual moordyn::state::State* rd(unsigned int i = 0) = 0;
/**
* @}
*/
protected:
/** @brief Constructor
* @param log Logging handler
*/
Scheme(moordyn::Log* log)
: io::IO(log)
, name("None")
, t(0.0)
{
}
/// The ground body
Body* ground;
/// The lines
std::vector<Line*> lines;
/// The points
std::vector<Point*> points;
/// The rods
std::vector<Rod*> rods;
/// The bodies
std::vector<Body*> bodies;
/// The scheme name
std::string name;
/// The simulation time
real t;
/// The local time, within the outer time step
real t_local;
/// Maximum CFL factor
real cfl;
};
/** Definition to explicitely let the compiler know the type inside the
* std::array.
*
* This is required to avoid problems with the templates
*/
#define AS_STATE(R) ((moordyn::state::State*)R)
/** @class SchemeBase Time.hpp
* @brief A generic abstract integration scheme
*
* This class can be later overloaded to implement a plethora of time schemes
*/
template<unsigned int NSTATE, unsigned int NDERIV>
class SchemeBase : public Scheme
{
public:
/// @brief Destructor
virtual ~SchemeBase()
{
for (unsigned int substep = 0; substep < NSTATE; substep++) {
delete _r[substep];
}
for (unsigned int substep = 0; substep < NDERIV; substep++) {
delete _rd[substep];
}
}
/** @brief Add a line
* @param obj The line
* @throw moordyn::invalid_value_error If it has been already registered
*/
virtual void AddLine(Line* obj)
{
try {
Scheme::AddLine(obj);
} catch (...) {
throw;
}
for (unsigned int i = 0; i < NSTATE; i++)
AS_STATE(_r[i])->addInstance(obj);
for (unsigned int i = 0; i < NDERIV; i++)
AS_STATE(_rd[i])->addInstance(obj);
// Add the mask value
_calc_mask.lines.push_back(true);
}
// virtual void AddLine(Line* obj)
// {
// try {
// TimeScheme::AddLine(obj);
// } catch (...) {
// throw;
// }
// // Build up the states and states derivatives
// unsigned int n = obj->getN() - 1;
// LineState state;
// LineState mstate; // misc state stuff
// state.pos.assign(n, vec::Zero());
// state.vel.assign(n, vec::Zero());
// mstate.pos.assign(n+2, vec::Zero());
// mstate.vel.assign(n+2, vec::Zero());
// for (unsigned int i = 0; i < r.size(); i++) {
// r[i].lines.push_back(state);
// r[i].misc.push_back(mstate);
// }
// DLineStateDt dstate;
// DLineStateDt mdstate; // misc state stuff
// dstate.vel.assign(n, vec::Zero());
// dstate.acc.assign(n, vec::Zero());
// mdstate.vel.assign(n+2, vec::Zero());
// mdstate.acc.assign(n+2, vec::Zero());
// for (unsigned int i = 0; i < rd.size(); i++) {
// rd[i].lines.push_back(dstate);
// rd[i].misc.push_back(mdstate);
// }
// // Add the mask value
// _calc_mask.lines.push_back(true);
// }
/** @brief Remove a line
* @param obj The line
* @return The index of the removed entity
* @throw moordyn::invalid_value_error If the line has not been registered,
* or it was already removed
*/
virtual unsigned int RemoveLine(Line* obj)
{
unsigned int i;
try {
i = Scheme::RemoveLine(obj);
} catch (...) {
throw;
}
for (unsigned int i = 0; i < NSTATE; i++)
AS_STATE(_r[i])->removeInstance(obj);
for (unsigned int i = 0; i < NDERIV; i++)
AS_STATE(_rd[i])->removeInstance(obj);
_calc_mask.lines.erase(_calc_mask.lines.begin() + i);
return i;
}
// virtual unsigned int RemoveLine(Line* obj)
// {
// unsigned int i;
// try {
// i = TimeScheme::RemoveLine(obj);
// } catch (...) {
// throw;
// }
// for (unsigned int i = 0; i < r.size(); i++) {
// r[i].lines.erase(r[i].lines.begin() + i);
// r[i].misc.erase(r[i].misc.begin() + i);
// }
// for (unsigned int i = 0; i < rd.size(); i++) {
// rd[i].lines.erase(rd[i].lines.begin() + i);
// rd[i].misc.erase(rd[i].misc.begin() + i);
// }
// _calc_mask.lines.erase(_calc_mask.lines.begin() + i);
// return i;
// }
/** @brief Add a point
* @param obj The point
* @throw moordyn::invalid_value_error If it has been already registered
*/
virtual void AddPoint(Point* obj)
{
try {
Scheme::AddPoint(obj);
} catch (...) {
throw;
}
for (unsigned int i = 0; i < NSTATE; i++)
AS_STATE(_r[i])->addInstance(obj);
for (unsigned int i = 0; i < NDERIV; i++)
AS_STATE(_rd[i])->addInstance(obj);
// Add the mask value
_calc_mask.points.push_back(true);
}
/** @brief Remove a point
* @param obj The point
* @return The index of the removed entity
* @throw moordyn::invalid_value_error If the point has not been
* registered, or it was already removed
*/
virtual unsigned int RemovePoint(Point* obj)
{
unsigned int i;
try {
i = Scheme::RemovePoint(obj);
} catch (...) {
throw;
}
for (unsigned int i = 0; i < NSTATE; i++)
AS_STATE(_r[i])->removeInstance(obj);
for (unsigned int i = 0; i < NDERIV; i++)
AS_STATE(_rd[i])->removeInstance(obj);
_calc_mask.points.erase(_calc_mask.points.begin() + i);
return i;
}
/** @brief Add a rod
* @param obj The rod
* @throw moordyn::invalid_value_error If it has been already registered
*/
virtual void AddRod(Rod* obj)
{
try {
Scheme::AddRod(obj);
} catch (...) {
throw;
}
for (unsigned int i = 0; i < NSTATE; i++)
AS_STATE(_r[i])->addInstance(obj);
for (unsigned int i = 0; i < NDERIV; i++)
AS_STATE(_rd[i])->addInstance(obj);
// Add the mask value
_calc_mask.rods.push_back(true);
}
/** @brief Remove a rod
* @param obj The rod
* @return The index of the removed entity
* @throw moordyn::invalid_value_error If the rod has not been registered,
* or it was already removed
*/
virtual unsigned int RemoveRod(Rod* obj)
{
unsigned int i;
try {
i = Scheme::RemoveRod(obj);
} catch (...) {
throw;
}
for (unsigned int i = 0; i < NSTATE; i++)
AS_STATE(_r[i])->removeInstance(obj);
for (unsigned int i = 0; i < NDERIV; i++)
AS_STATE(_rd[i])->removeInstance(obj);
_calc_mask.rods.erase(_calc_mask.rods.begin() + i);
return i;
}
/** @brief Add a body
* @param obj The body
* @throw moordyn::invalid_value_error If it has been already registered
*/
virtual void AddBody(Body* obj)
{
try {
Scheme::AddBody(obj);
} catch (...) {
throw;
}
for (unsigned int i = 0; i < NSTATE; i++)
AS_STATE(_r[i])->addInstance(obj);
for (unsigned int i = 0; i < NDERIV; i++)
AS_STATE(_rd[i])->addInstance(obj);
// Add the mask value
_calc_mask.bodies.push_back(true);
}
/** @brief Remove a body
* @param obj The body
* @return The index of the removed entity
* @throw moordyn::invalid_value_error If the body has not been registered,
* or it was already removed
*/
virtual unsigned int RemoveBody(Body* obj)
{
unsigned int i;
try {
i = Scheme::RemoveBody(obj);
} catch (...) {
throw;
}
for (unsigned int i = 0; i < NSTATE; i++)
AS_STATE(_r[i])->removeInstance(obj);
for (unsigned int i = 0; i < NDERIV; i++)
AS_STATE(_rd[i])->removeInstance(obj);
_calc_mask.bodies.erase(_calc_mask.bodies.begin() + i);
return i;
}
/** @brief Create an initial state for all the entities
* @note Just the first state is written. None of the following states nor
* the derivatives are initialized in any way.
* @note It is assumed that the coupled entities were already initialized
*/
virtual void Init()
{
// NOTE: Probably is best to populate all the entities to the time
// integrator, no matter if they are free or not. Thus they can change
// types (mutate) without needing to micromanage them in the time
// scheme
for (unsigned int i = 0; i < bodies.size(); i++) {
// Only fully coupled bodies are intialized in MD2.cpp
if ((bodies[i]->type != Body::FREE) &&
(bodies[i]->type != Body::CPLDPIN))
continue;
bodies[i]->initialize(AS_STATE(_r[0])->get(bodies[i]));
for (unsigned int j = 0; j < NDERIV; j++)
AS_STATE(_rd[j])->get(bodies[i]).setZero();
}
for (unsigned int i = 0; i < rods.size(); i++) {
if ((rods[i]->type != Rod::FREE) && (rods[i]->type != Rod::PINNED))
continue;
rods[i]->initialize(AS_STATE(_r[0])->get(rods[i]));
for (unsigned int j = 0; j < NDERIV; j++)
AS_STATE(_rd[j])->get(rods[i]).setZero();
}
for (unsigned int i = 0; i < points.size(); i++) {
if (points[i]->type != Point::FREE)
continue;
points[i]->initialize(AS_STATE(_r[0])->get(points[i]));
for (unsigned int j = 0; j < NDERIV; j++)
AS_STATE(_rd[j])->get(points[i]).setZero();
}
for (unsigned int i = 0; i < lines.size(); i++) {
lines[i]->initialize(AS_STATE(_r[0])->get(lines[i]));
for (unsigned int j = 0; j < NDERIV; j++)
AS_STATE(_rd[j])->get(lines[i]).setZero();
}
}
/** @brief Run a time step
*
* This function is the one that must be specialized on each time scheme,
* but remember to call it at the end of the inherited function to increment
* Scheme::t_local
* @param dt Time step
*/
virtual void Step(real& dt) { Scheme::Step(dt); };
/** @brief Get the number of state variables
* @return The number of state variables
*/
inline const unsigned int GetNState() const { return NSTATE; }
/** @brief Get the number of state derivative variables
* @return The number of state derivative variables
*/
inline const unsigned int GetNDeriv() const { return NDERIV; }
/** @brief Get the state
* @param i The index of the state variable to take
* @return The state variable
* @throws moordyn::invalid_value_error if the @p i index is greater or
* equal than the number of states
* @{
*/
inline state::State* r(unsigned int i = 0)
{
if (i >= NSTATE) {
LOGERR << "State " << i << " cannot be got on a '" << name
<< "' scheme that has " << NSTATE << "states" << endl;
throw moordyn::invalid_value_error("Invalid state");
}
return _r[i];
}
inline state::State GetState(unsigned int i = 0) { return *r(i); }
/**
* @}
*/
/** @brief Resume the simulation from the stationary solution
* @param state The stationary solution
* @param i The index of the state variable to take
* @throws moordyn::invalid_value_error if the @p i index is greater or
* equal than the number of states
*/
inline virtual void SetState(const state::State& state, unsigned int i = 0)
{
if (i >= NSTATE) {
LOGERR << "State " << i << " cannot be setted on a '" << name
<< "' scheme that has " << NSTATE << " states" << endl;
throw moordyn::invalid_value_error("Invalid state");
}
*_r[i] = state;
}
/** @brief Save the system state on a file
* @param filepath The output file path
* @param i The index of the state variable to serialize
* @throws moordyn::invalid_value_error if the @p i index is greater or
* equal than the number of states
*/
inline virtual void SaveState(const std::string filepath,
unsigned int i = 0)
{
auto f = MakeFile(filepath);
if (i >= NSTATE) {
LOGERR << "State " << i << " cannot be saved on a '" << name
<< "' scheme that has " << NSTATE << "states" << endl;
throw moordyn::invalid_value_error("Invalid state");
}
std::vector<uint64_t> data = AS_STATE(_r[i])->Serialize();
const uint64_t size = data.size();
f.write((char*)&size, sizeof(uint64_t));
for (auto v : data) {
f.write((char*)&v, sizeof(uint64_t));
}
f.close();
}
/** @brief Load the system state from a file
* @param filepath The output file path
* @param i The index of the state variable to overwrite
* @throws moordyn::invalid_value_error if the @p i index is greater or
* equal than the number of states
* @throws moordyn::mem_error if the expected size of the serialized data
* is not met
*/
inline virtual void LoadState(const std::string filepath,
unsigned int i = 0)
{
auto [length, data] = LoadFile(filepath);
if (i >= NSTATE) {
LOGERR << "State " << i << " cannot be loaded on a '" << name
<< "' scheme that has " << NSTATE << "states" << endl;
throw moordyn::invalid_value_error("Invalid state");
}
const uint64_t* end = AS_STATE(_r[i])->Deserialize(data);
if (data + length != end) {
const uint64_t l = end - data;
LOGERR << l * sizeof(uint64_t) << " bytes (vs. "
<< length * sizeof(uint64_t)
<< " bytes expected) unpacked from '" << filepath << "'"
<< endl;
throw moordyn::mem_error("Mismatching data size");
}
free(data);
}
/** @brief Get the state derivative
* @param i The index of the state derivative to take
* @return The state derivative
* @throws moordyn::invalid_value_error if the @p i index is greater or
* equal than the number of state derivatives
* @{
*/
inline state::State* rd(unsigned int i = 0)
{
if (i >= NDERIV) {
LOGERR << "State derivative " << i << " cannot be got on a '"
<< name << "' scheme that has " << NDERIV
<< "state derivatives" << endl;
throw moordyn::invalid_value_error("Invalid state derivative");
}
return _rd[i];
}
inline state::State GetDeriv(unsigned int i = 0) { return *rd(i); }
/**
* @}
*/
/** @brief Produce the packed data to be saved
*
* The produced data can be used afterwards to restore the saved information
* afterwards calling Deserialize(void).
* @return The packed data
*/
virtual std::vector<uint64_t> Serialize(void)
{
std::vector<uint64_t> data, subdata;
data.push_back(io::IO::Serialize(t));
// We do not need to save the number of states or derivatives, since
// that information is already known by each specific time scheme.
// Along the same line, we do not need to same information about the
// number of lines, rods and so on. That information is already
// collected from the definition file
for (unsigned int substep = 0; substep < NSTATE; substep++) {
subdata = _r[substep]->Serialize();
data.insert(data.end(), subdata.begin(), subdata.end());
}
for (unsigned int substep = 0; substep < NDERIV; substep++) {
subdata = _rd[substep]->Serialize();
data.insert(data.end(), subdata.begin(), subdata.end());
}
return data;
}
/** @brief Unpack the data to restore the Serialized information
*
* This is the function that each inherited class must implement, and should
* be the inverse of Serialize(void)
* @param data The packed data
* @return A pointer to the end of the file, for debugging purposes
*/
virtual uint64_t* Deserialize(const uint64_t* data)
{
uint64_t* ptr = (uint64_t*)data;
ptr = io::IO::Deserialize(ptr, t);
// We did not save the number of states or derivatives, since that
// information is already known by each specific time scheme.
// Along the same line, we did not save information about the number of
// lines, rods and so on
for (unsigned int substep = 0; substep < NSTATE; substep++) {
ptr = _r[substep]->Deserialize(ptr);
}
for (unsigned int substep = 0; substep < NDERIV; substep++) {
ptr = _rd[substep]->Deserialize(ptr);
}
return ptr;
}
protected:
/** @brief Constructor
* @param log Logging handler
* @param waves The simulation waves object, needed so that we can tell it
* about substeps
*/
SchemeBase(moordyn::Log* log, moordyn::WavesRef waves)
: Scheme(log)
, waves(waves)
{
// Register some basic variables that we know are always present
// The position, and its associated derivative, have several different
// types depending on the instance (e.g. moordyn::vec for lines and
// points, moordyn::XYZQuat for rods and bodies), so better using lists
// that we can resize on demand
for (unsigned int substep = 0; substep < NSTATE; substep++) {
_r[substep] = new moordyn::state::State(log);
}
for (unsigned int substep = 0; substep < NDERIV; substep++) {
_rd[substep] = new moordyn::state::State(log);
}
}
/** @brief Update all the entities to set the state
* @param substep The index within moordyn::SchemeBase::r that will be
* applied to set the states
* @param t_local The local time, within the inner time step (from 0 to dt)
* @note This is only affecting to the free entities
* @see Scheme::Next()
* @see Scheme::Step()
*/
void Update(real t_local, unsigned int substep = 0);
/** @brief Compute the time derivatives and store them
* @param substep The index within moordyn::SchemeBase::rd where the
* info will be saved
*/
void CalcStateDeriv(unsigned int substep = 0);
/// The list of states
std::array<moordyn::state::State*, NSTATE> _r;
/// The list of state derivatives
std::array<moordyn::state::State*, NDERIV> _rd;
/// The waves instance
std::shared_ptr<Waves> waves;
/** @brief A mask to determine which entities shall be computed.
*
* Useful for local time steps
*/
typedef struct _mask
{
/// The lines mask
std::vector<bool> lines;
/// The points mask
std::vector<bool> points;
/// The rods mask
std::vector<bool> rods;
/// The bodies mask
std::vector<bool> bodies;
} mask;
/// The SchemeBase::CalcStateDeriv() mask
mask _calc_mask;
};
/** @class StationaryScheme Time.hpp
* @brief A stationary solution
*
* The stationary solution is featured by the lack of velocity on the system,
* i.e. the system positions are integrating directly from the accelerations
*/
class StationaryScheme final : public SchemeBase<2, 1>
{
public:
/** @brief Constructor
* @param log Logging handler
* @param waves Waves instance
*/
StationaryScheme(moordyn::Log* log, WavesRef waves);
/// @brief Destructor
~StationaryScheme() {}
/** @brief Run a time step
*
* This function is the one that must be specialized on each time scheme
* @param dt Time step
*/
void Step(real& dt);
/** @brief Get the error computed at the last time step
* @return The error, StationaryScheme::_error
*/
inline real Error() const { return _error; }
/** @brief Compute the number of state variables
*
* This can be used to renormalize the error, so it makes more sense to the
* final user
* @return The number of state variables
* @note Each entry on the states is considered a single variable, that is
* no matter if the state is a scalar, a vector or a quaternion, it is
* considered as a single entry
*/
inline size_t NStates() const
{
size_t n =
bodies.size() + rods.size() +
points.size(); // one row (state) per object here. <objects>.size()
// gives the length of the list, i.e. the number of
// that kind of object
for (size_t i = 0; i < lines.size(); i++)
n += lines[i]->stateN();
return n;
}
private:
/** @brief Make the state derivative stationary
*
* Making the derivative stationary means replacing the velocity by half
* the acceleration by the time step
* @param dt The time step
* @param i The index of the state
* @param id The index of the state derivative
*/
void MakeStationary(real& dt, unsigned int i = 0, unsigned int id = 0);
/** The last computed acceleration module
* @see DMoorDynStateDt::MakeStationary()
* @see StationaryScheme::Error()
*/
real _error;
/// The convergence boosting rate
real _booster;
};
/** @class EulerScheme Time.hpp
* @brief The simplest 1st order Euler's time scheme
*
* This time scheme is strongly discourage, and use only for testing/debugging
* purposes
*/
class EulerScheme final : public SchemeBase<1, 1>
{
public:
/** @brief Constructor
* @param log Logging handler
* @param waves Waves instance
*/
EulerScheme(moordyn::Log* log, WavesRef waves);
/// @brief Destructor
virtual ~EulerScheme() {}
/** @brief Run a time step
*
* This function is the one that must be specialized on each time scheme
* @param dt Time step
*/
virtual void Step(real& dt);
};
/** @class LocalSchemeBase Time.hpp
* @brief A generic abstract integration scheme
*