FhSim  3.1.0
Marine systems simulation
Loading...
Searching...
No Matches
PotentialData.h
1#ifndef _CPOTENTIAL_DATA_H_
2#define _CPOTENTIAL_DATA_H_
3
4#include <iostream>
5#include <iomanip>
6#include <fstream>
7#include <algorithm>
8#include <stdexcept>
9#include <string>
10#include <vector>
11#include <complex>
12#include <limits>
13#include <map>
14#include <memory>
15
16#include <Eigen/Eigen>
17#include <Eigen/Eigenvalues>
18#include <Eigen/Core>
19
20
21#include "../HullData/HullData.h"
22
23#include "./Wave/FroudeKrylov.h"
24#include "./Wave/WaveDrift.h"
25
26#include "Identification/FreqDomainIdent.h"
27
28
29namespace Ship{
31 {
32 public:
33 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
34
36 {
37 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
38
40 {
41 Speed = -10;
42 Ainf = Eigen::Matrix< double, 6, 6 >::Zero( );
43
44 for( int i=0; i <6; i++)
45 for( int j=0; j <6; j++)
46 {
47 A[i][j] = Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic >::Zero( 1, 1 );;
48 B[i][j] = Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic >::Zero( 1, 1 );;
49 C[i][j] = Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic >::Zero( 1, 1 );;
50 D[i][j] = Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic >::Zero( 1, 1 );;
51 }
52 }
53
54 // Ainf
55 Eigen::Matrix<double, 6, 6> Ainf;
56
57 double Speed;
58
59 // companion system matrices
60 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> A[6][6];
61 Eigen::Matrix<double, Eigen::Dynamic, 1 > B[6][6];
62 Eigen::Matrix<double, 1 , Eigen::Dynamic> C[6][6];
63 Eigen::Matrix<double, Eigen::Dynamic, 1 > D[6][6];
64 double Direction;
65 };
66
67 virtual ~PotentialData() = default;
68
69 virtual void Read( const std::string &fname, int flipPhaseForOSC =0) = 0;
70 virtual Eigen::Matrix<double,6,6> GetHToO() = 0;
71
73 const std::vector<std::string>& Warnings() const { return m_warnings; }
74 void ClearWarnings() { m_warnings.clear(); }
75
76 virtual void SetOriginWorigin() = 0;
77 virtual void SetOriginGorigin() = 0;
78 virtual void SetOrigin( Eigen::Matrix<double,3,1> &r ) = 0;
79
80 Eigen::Matrix<double,6,6> GetMassRigidBody()
81 {
82 return GetHToO().transpose()*Mrbeig*GetHToO();
83 }
84
85 Eigen::Matrix<double,6,6> GetAinf(unsigned int speedno=0)
86 {
87 return RadiationApproximation[speedno].Ainf;
88 }
89
90 Eigen::Matrix<double,6,6> GetMassTotal(unsigned int speedno=0)
91 {
92 return RadiationApproximation[speedno].Ainf+GetHToO().transpose()*Mrbeig*GetHToO();
93 }
94
95 Eigen::Matrix<double,6,6> GetAw(unsigned int FreqNo, unsigned int SpeedNo=0)
96 {
97 return GetHToO().transpose()*A[SpeedNo][FreqNo]*GetHToO();
98 }
99
100 Eigen::Matrix<double,6,6> GetBw(unsigned int FreqNo, unsigned int SpeedNo=0)
101 {
102 return GetHToO().transpose()*B[SpeedNo][FreqNo]*GetHToO();
103 }
104
105 virtual Eigen::Matrix<double,6,6> GetCw(unsigned int FreqNo,unsigned int SpeedNo=0)
106 {
107 return GetHToO().transpose()*C[SpeedNo][FreqNo]*GetHToO();
108 }
109
110 Eigen::Matrix<double,1,Eigen::Dynamic> GetAij(unsigned int DOF1, unsigned int DOF2, unsigned int SpeedNo=0)
111 {
112 Eigen::Matrix<double,1,Eigen::Dynamic> A = Eigen::Matrix<double,1,Eigen::Dynamic>::Zero(1,NoFrequency);
113
114 for( unsigned int i=0; i < NoFrequency; i++)
115 {
116 Eigen::Matrix<double,6,6> Aw = GetAw(i,SpeedNo);
117 A(i) = Aw(DOF1,DOF2);
118 }
119 return A;
120 }
121
122 Eigen::Matrix<double,1,Eigen::Dynamic> GetBij(unsigned int DOF1, unsigned int DOF2, unsigned int SpeedNo=0)
123 {
124 Eigen::Matrix<double,1,Eigen::Dynamic> B = Eigen::Matrix<double,1,Eigen::Dynamic>::Zero(1,NoFrequency);
125
126 for( unsigned int i=0; i < NoFrequency; i++)
127 {
128 Eigen::Matrix<double,6,6> Bw = GetBw(i,SpeedNo);
129 B(i) = Bw(DOF1,DOF2);
130 }
131 return B;
132 }
133
134 Eigen::Matrix<double,1,Eigen::Dynamic> GetCij(unsigned int DOF1, unsigned int DOF2, unsigned int SpeedNo=0)
135 {
136 Eigen::Matrix<double,1,Eigen::Dynamic> C = Eigen::Matrix<double,1,Eigen::Dynamic>::Zero(1,NoFrequency);
137
138 for( unsigned int i=0; i < NoFrequency; i++)
139 {
140 Eigen::Matrix<double,6,6> Cw = GetCw(i,SpeedNo);
141 C(i) = Cw(DOF1,DOF2);
142 }
143 return C;
144 }
145
146 Eigen::Matrix<double,1,Eigen::Dynamic> GetSpeed()
147 {
148 Eigen::Matrix<double,1,Eigen::Dynamic> speed = Eigen::Matrix<double,1,Eigen::Dynamic>::Zero(1,NoSpeed);
149 for(unsigned int i=0; i < NoSpeed; i++ ) speed(i) = Speed[i];
150 return speed;
151 }
152
153 Eigen::Matrix<double,1,Eigen::Dynamic> GetHeading()
154 {
155 Eigen::Matrix<double,1,Eigen::Dynamic> head = Eigen::Matrix<double,1,Eigen::Dynamic>::Zero(1,NoHeading);
156 for(unsigned int i=0; i < NoHeading; i++ ) head(i) = Heading[i];
157 return head;
158 }
159
160 Eigen::Matrix<double,1,Eigen::Dynamic> GetFrequency(int SpeedNo=0)
161 {
162 Eigen::Matrix<double,1,Eigen::Dynamic> freq = Eigen::Matrix<double,1,Eigen::Dynamic>::Zero(1,NoFrequency);
163 for(unsigned int i=0; i < NoFrequency; i++ ) freq(i) = Frequency[SpeedNo][i];
164 return freq;
165 }
166
167 virtual Eigen::Matrix<double,3,1> GetRg()
168 {
169 return rg;
170 }
171
172 virtual Eigen::Matrix<double,3,1> GetRo()
173 {
174 return ro;
175 }
176
179 Eigen::Matrix<double,6,1> GetFirstOrderForce( const Eigen::Matrix<double,3,1> &Kxy, double ShipSpeed, double RaoSpeed,
180 double WaveDirection, double WaveAmplitude, double WaveFrequency, double WaveNumber, double WavePhase,
181 double Time )
182 {
183 double Fptr[6];
184
185 double EncounterFrequency = WaveFrequency-WaveNumber*ShipSpeed;
186 double Direction = EncounterFrequency/std::abs(EncounterFrequency);
187 EncounterFrequency = std::abs(EncounterFrequency);
188
189 //std::cout << "T: " << Time << "\t";
190
191 for( int DOF=0; DOF < 6; DOF++)
192 {
193
194 std::complex<double> RAO = FroudeFrylov->GetRAOValue(DOF,WaveDirection,std::abs(RaoSpeed),WaveFrequency);
195
196
197 double Amplitude = std::abs(RAO);
198 double Phase = atan2( RAO.imag(), RAO.real() );
199
200 //std::cout << DOF << " " <<RAO.real() << " " << RAO.imag() << "i" << " , Lag:" << 1/WaveFrequency*(Kxy(0) - Kxy(1) - Phase - WavePhase) << "s ";
201
202 double ThisWaveHeight = WaveAmplitude*sin(EncounterFrequency *Time -Kxy(0) -Kxy(1) + Phase - WavePhase );
203 Fptr[DOF] = Direction*Amplitude*ThisWaveHeight;
204 }
205 //std::cout << std::endl;
206 Eigen::Map<Eigen::Matrix<double,6,1>,Eigen::Unaligned> tmp(Fptr);
207 return GetHToO().transpose()*tmp;
208 }
209
210 Eigen::Matrix<double,6,1> GetSecondOrderForce(Eigen::Matrix<double,3,1> ShipPosition, double ShipSpeed,
211 double WaveDirection, double WaveHeight, double WaveFrequency, double WaveNumber, double WavePhase,
212 double Time )
213 {
214 double Fptr[6] = {}; // zero without a wave drift data set (owner ruling D42)
215 for( int DOF=0; DOF < 6; DOF++)
216 {
217 if( WaveDrift )
218 {
219 std::complex<double> Amplitude = WaveDrift->GetRAOValue(DOF,ShipSpeed,WaveFrequency,WaveDirection);
220 Fptr[DOF] = std::pow(WaveHeight,2)*Amplitude.real();
221 }
222 }
223 Eigen::Map<Eigen::Matrix<double,6,1>,Eigen::Unaligned> tmp(Fptr);
224 return GetHToO().transpose()*tmp;
225 }
226
227 void GenerateZeroSpeedTimeDomainApproximation()
228 {
229 // Only 2D (VERES) data is supported. A reader for 3D data must fit the memory terms of every pair, surge included.
230 RequireTwoDimensionalData();
231 //std::cout << "Generating zero speed time domain state radiation model"<<std::endl;
232 if( !RadiationApproximation.empty() ) RadiationApproximation.clear();
233 if( ZeroSpeedIdentificationFromCache() ){
234 //std::cout << "using model from cache" << std::endl;
235 return;
236 }else{
237 //std::cout << "identifying new model" << std::endl;
238 }
239
240 FreqDomainIdent Identification;
241 Identification.SetPotentialData(this);
242 Identification.IdentificationMethod(FreqDomainIdent::VECTOR_FITTING);
243
244 TimeDomainRadiation zeroSpeedSystem;
245
246
247 for( int i=0; i < 6; i++)
248 {
249
250 for( int j=0; j <= i; j++)
251 {
252 if( (i == 1 || i == 3 || i == 5) && (j==0 || j==2 || j==4) ) continue;
253 if( (j == 1 || j == 3 || j == 5) && (i==0 || i==2 || i==4) ) continue;
254
255 std::cout << "Identifying (i/j) = (" << i << "/" << j << ") ";
256 fflush(0);
257 if( is2Ddata() && (i!=0 && j!=0)){
258 FreqDomainIdent::StateSpace SS = Identification.IdentifySS(i,j);
259 zeroSpeedSystem.Ainf(i,j) = Identification.GetAinf(i,j);
260 zeroSpeedSystem.A[i][j] = SS.A;
261 zeroSpeedSystem.B[i][j] = SS.B;
262 zeroSpeedSystem.C[i][j] = SS.C;
263 zeroSpeedSystem.D[i][j] = SS.D;
264
265 zeroSpeedSystem.Ainf(j,i) = zeroSpeedSystem.Ainf(i,j);
266 zeroSpeedSystem.A[j][i] = zeroSpeedSystem.A[i][j];
267 zeroSpeedSystem.B[j][i] = zeroSpeedSystem.B[i][j];
268 zeroSpeedSystem.C[j][i] = zeroSpeedSystem.C[i][j];
269 zeroSpeedSystem.D[j][i] = zeroSpeedSystem.D[i][j];
270 }else{
271 std::cout << " Zero data" << std::endl;
272 zeroSpeedSystem.Ainf(i,j) = GetAij(i,j,0)[0];
273 zeroSpeedSystem.A[i][j] = Eigen::Matrix<double,1,1>::Zero();
274 zeroSpeedSystem.B[i][j] = Eigen::Matrix<double,1,1>::Zero();
275 zeroSpeedSystem.C[i][j] = Eigen::Matrix<double,1,1>::Zero();
276 zeroSpeedSystem.D[i][j] = Eigen::Matrix<double,1,1>::Zero();
277
278 zeroSpeedSystem.Ainf(j,i) = GetAij(i,j,0)[0];
279 zeroSpeedSystem.A[j][i] = Eigen::Matrix<double,1,1>::Zero();
280 zeroSpeedSystem.B[j][i] = Eigen::Matrix<double,1,1>::Zero();
281 zeroSpeedSystem.C[j][i] = Eigen::Matrix<double,1,1>::Zero();
282 zeroSpeedSystem.D[j][i] = Eigen::Matrix<double,1,1>::Zero();
283 }
284 }
285 }
286 RadiationApproximation.insert( std::make_pair(0,zeroSpeedSystem));
287 ZeroSpeedIdentificaitonToCache();
288 }
289
290 void GenerateTimeDomainApproximation()
291 {
292 // Only 2D (VERES) data is supported. A reader for 3D data must fit the memory terms of every pair, surge included.
293 RequireTwoDimensionalData();
294 //std::cout << "Generating time domain state radiation model"<<std::endl;
295 if( IdentificationFromCache() ) return;
296
297 FreqDomainIdent Identification;
298 Identification.SetPotentialData(this);
299 Identification.IdentificationMethod(FreqDomainIdent::VECTOR_FITTING);
300
301 for (unsigned int speed=0; speed < NoSpeed; speed++ )
302 {
303 TimeDomainRadiation speedSystem;
304 speedSystem.Speed = Speed[speed];
305
306 for( int i=0; i < 6; i++)
307 {
308 for( int j=0; j <= i; j++)
309 {
310 if( (i == 1 || i == 3 || i == 5) && (j==0 || j==2 || j==4) ) continue;
311 if( (j == 1 || j == 3 || j == 5) && (i==0 || i==2 || i==4) ) continue;
312
313 std::cout << "Identifying (i/j) = ("<<speed<<")(" << i << "/" << j << ") ";
314 //CPrecisionTimer T; T.Start();
315 if( is2Ddata() && (i!=0 && j!=0)){
316 FreqDomainIdent::StateSpace SS = Identification.IdentifySS(i,j,speed);
317 speedSystem.Ainf(i,j) = Identification.GetAinf(i,j,speed);
318 speedSystem.A[i][j] = SS.A;
319 speedSystem.B[i][j] = SS.B;
320 speedSystem.C[i][j] = SS.C;
321 speedSystem.D[i][j] = SS.D;
322
323 speedSystem.Ainf(j,i) = speedSystem.Ainf(i,j);
324 speedSystem.A[j][i] = speedSystem.A[i][j];
325 speedSystem.B[j][i] = speedSystem.B[i][j];
326 speedSystem.C[j][i] = speedSystem.C[i][j];
327 speedSystem.D[j][i] = speedSystem.D[i][j];
328 }else{
329 speedSystem.Ainf(i,j) = GetAij(i,j,speed)[0];
330 speedSystem.A[i][j] = Eigen::Matrix<double,1,1>::Zero();
331 speedSystem.B[i][j] = Eigen::Matrix<double,1,1>::Zero();
332 speedSystem.C[i][j] = Eigen::Matrix<double,1,1>::Zero();
333 speedSystem.D[i][j] = Eigen::Matrix<double,1,1>::Zero();
334
335 speedSystem.Ainf(j,i) = GetAij(i,j,speed)[0];
336 speedSystem.A[j][i] = Eigen::Matrix<double,1,1>::Zero();
337 speedSystem.B[j][i] = Eigen::Matrix<double,1,1>::Zero();
338 speedSystem.C[j][i] = Eigen::Matrix<double,1,1>::Zero();
339 speedSystem.D[j][i] = Eigen::Matrix<double,1,1>::Zero();
340 }
341
342 std::cout << "identified new data - " /*<< T.Stop() << " seconds"*/ << std::endl;
343 }
344 }
345 RadiationApproximation.insert( std::make_pair(speed,speedSystem));
346 }
347 IdentificaitonToCache();
348 }
349
350
351 void PrintShip()
352 {
353 Eigen::Matrix<double,6,6> M = GetMassRigidBody();
354
355 std::cout << "%Ship information form file: " << fname << std::endl;
356 std::cout << "%\tLpp:\t" << Lpp << std::endl;
357 std::cout << "%\tB:\t" << Bredth << std::endl;
358 std::cout << "%\tT:\t" << Draught << std::endl;
359 std::cout << "%\tr_cg\t" << LCG<<"\t,\t0\t,\t"<<VCG<< " (from AP and base line)" << std::endl;
360 std::cout << "%\tr_cb\t" << LCB<<"\t,\t0\t,\t"<<VCB<< " (from AP and base line)" << std::endl;
361
362 std::cout << "%\tPotential data set information: " << std::endl;
363 std::cout << "%\t\tNumber of speeds:\t" <<NoSpeed<< std::endl;
364 std::cout << "speed = [\t\t" ;
365 for(unsigned int i=0; i < NoSpeed; i++ ) std::cout << Speed[i] << "\t";
366 std::cout << "];" << std::endl;
367
368 std::cout << "%\t\tNumber of headings:\t" <<NoHeading<< std::endl;
369 std::cout << "head =[\t\t" ;
370 for(unsigned int i=0; i < NoHeading; i++ ) std::cout << Heading[i] << "\t";
371 std::cout << "];" << std::endl;
372
373 std::cout << "%\t\tNumber of frequencies:\t" <<NoFrequency<< std::endl;
374 std::cout << "freq =[\t\t" ;
375 // (this printed the address of the row, not the frequencies; it prints the first speed's row)
376 if( !Frequency.empty() ) for(unsigned int i=0; i < Frequency[0].size(); i++ ) std::cout << Frequency[0][i] << "\t";
377 std::cout << "];" << std::endl;
378
379 for(unsigned int i=0; i < NoSpeed; i++ )
380 {
381 std::cout << "%Total mass matrix for Speed " << i << " V="<<Speed[i] << " m/s" << std::endl;
382
383 Eigen::Matrix<double,6,6> Ainf = GetAinf(i);//FIXME: Direction
384
385 Eigen::Matrix<double,6,6> Mtotal = M + Ainf;
386 std::cout << std::scientific;
387 for( int j=0; j < 6; j++)
388 {
389 if( j== 0) std::cout << "M{" <<i+1<<"}\t=[";
390 std::cout <<std::setprecision(4) <<"\t"<< Mtotal(j,0) << "\t," << Mtotal(j,1) << "\t," << Mtotal(j,2) << "\t," << Mtotal(j,3) << "\t," << Mtotal(j,4) << "\t,"<<Mtotal(j,5);
391 if( j== 5) std::cout <<"];";
392 std::cout <<";" <<std::endl;
393 }
394
395 }
396
397
398 for(unsigned int i=0; i < NoSpeed; i++)
399 {
400 std::cout << "%Fluid memory model for Speed " << i << " V="<<Speed[i] << " m/s" << std::endl;
401 TimeDomainRadiation mod = RadiationApproximation[i];
402
403 for( int j=0; j < 6; j++ )
404 {
405 for( int k=0; k < 6; k++ )
406 {
407 size_t num_states = mod.A[k][j].cols();
408
409 if( num_states == 1 )
410 {
411 std::cout << "A{"<<i+1<<"}{"<<j+1<<"}{"<<k+1<<"}" <<"=\t" << "[" << mod.A[j][k](0,0) << "];"<< std::endl;
412 std::cout << std::endl;
413 std::cout << "B{"<<i+1<<"}{"<<j+1<<"}{"<<k+1<<"}" <<" =\t" << "[" << mod.B[j][k](0,0) << "];"<< std::endl;
414 std::cout << std::endl;
415 std::cout << "C{"<<i+1<<"}{"<<j+1<<"}{"<<k+1<<"}" <<" =\t" << "[" << mod.C[j][k](0,0) << "];"<< std::endl;
416 std::cout << std::endl;
417 std::cout << "D{"<<i+1<<"}{"<<j+1<<"}{"<<k+1<<"}" <<" =\t" << "[" << mod.D[j][k](0,0) << "];"<< std::endl;
418 }
419 else if (num_states == 2 )
420 {
421 std::cout << "A{"<<i+1<<"}{"<<j+1<<"}{"<<k+1<<"}"<<" =\t" << "[" << mod.A[j][k](0,0) << "\t,"<<mod.A[j][k](0,1)<<"\t;" << mod.A[j][k](1,0) << "\t,"<<mod.A[j][k](1,1)<<"\t;"<< "];"<< std::endl;
422 std::cout << std::endl;
423 std::cout << "B{"<<i+1<<"}{"<<j+1<<"}{"<<k+1<<"}"<<" =\t" << "[" << mod.B[j][k](0,0) << "\t;" << mod.B[j][k](1,0) <<"];"<< std::endl;
424 std::cout << std::endl;
425 std::cout << "C{"<<i+1<<"}{"<<j+1<<"}{"<<k+1<<"}"<<" =\t" << "[" << mod.C[j][k](0,0) << "\t," << mod.C[j][k](0,1) <<"];"<< std::endl;
426 std::cout << std::endl;
427 std::cout << "D{"<<i+1<<"}{"<<j+1<<"}{"<<k+1<<"}"<<" =\t" << "[" << mod.D[j][k](0,0) <<"];"<< std::endl;
428 }
429 else
430 {
431
432 std::cout << "A{"<<i+1<<"}{"<<j+1<<"}{"<<k+1<<"}"<<" =\t" << "[";
433 for( size_t s=0; s < num_states; s++)
434 {
435 if(s!=0) std::cout <<" \t ";
436 for( size_t c=0; c < num_states; c++)
437 {
438 std::cout << mod.A[j][k](s,c);
439 if( c < (num_states-1) ) std::cout << "\t,";
440 }
441 if( s < (num_states-1) ) std::cout << std::endl;
442 }
443 std::cout << "];"<< std::endl;
444
445 std::cout << std::endl;
446 std::cout << "B{"<<i+1<<"}{"<<j+1<<"}{"<<k+1<<"}"<<" =\t" << "[";
447 for( size_t s=0; s < num_states; s++)
448 {
449 if( s != 0 )std::cout <<" \t ";
450 std::cout << mod.B[j][k](s,0);
451 if( s < (num_states-1) ) std::cout << ";" <<std::endl;
452 }
453 std::cout << "];"<< std::endl;
454 std::cout << std::endl;
455
456 std::cout << "C{"<<i+1<<"}{"<<j+1<<"}{"<<k+1<<"}"<<" =\t" << "[";
457 for( size_t s=0; s < num_states; s++)
458 {
459 std::cout << mod.C[j][k](0,s);
460 if( s < (num_states-1) ) std::cout << ",";
461 }
462 std::cout << "];"<< std::endl;
463
464 std::cout << std::endl;
465 std::cout << "D{"<<i+1<<"}{"<<j+1<<"}{"<<k+1<<"}"<<" =\t" << "[" << mod.D[j][k](0,0) << "];"<< std::endl;
466 }
467 }
468 }
469 }
470 }
471
472 size_t GetTimeDomainStateSize( int DOF1, int DOF2, int SpeedNo=0 )
473 {
474 return RadiationApproximation[SpeedNo].A[DOF1][DOF2].cols();
475 }
476
477 void ZeroSpeedIdentificaitonToCache()
478 {
479 std::string cachebase = fname;
480 std::stringstream ss; ss << cachebase << ".identcache0";
481
482 std::fstream cache(ss.str().c_str(),std::ios::out|std::ios::binary);
483
484 int a_size[2];
485 int b_size[2];
486 int c_size[2];
487 int d_size[2];
488
489 for( int i=0; i < 6; i++)
490 {
491 for( int j=0; j < 6; j++)
492 {
493// a_size[0] = static_cast<int>(RadiationApproximation[0].A[i][j].rows());
494// a_size[1] = static_cast<int>(RadiationApproximation[0].A[i][j].cols());
495
496 a_size[0] = static_cast<int>(RadiationApproximation[0].A[i][j].rows());
497 a_size[1] = static_cast<int>(RadiationApproximation[0].A[i][j].cols());
498
499 cache.write( reinterpret_cast<char*>(a_size), sizeof(a_size) );
500 cache.write( reinterpret_cast<char*>(RadiationApproximation[0].A[i][j].data()),sizeof(double)*a_size[0]*a_size[1] );
501
502// b_size[0] = static_cast<int>(RadiationApproximation[0].B[i][j].rows());
503// b_size[1] = static_cast<int>(RadiationApproximation[0].B[i][j].cols());
504
505 b_size[0] = static_cast<int>(RadiationApproximation[0].B[i][j].rows());
506 b_size[1] = static_cast<int>(RadiationApproximation[0].B[i][j].cols());
507
508 cache.write( reinterpret_cast<char*>(b_size), sizeof(b_size) );
509 cache.write( reinterpret_cast<char*>(RadiationApproximation[0].B[i][j].data()),sizeof(double)*b_size[0]*b_size[1] );
510
511
512 // c_size[0] = static_cast<int>(RadiationApproximation[0].C[i][j].rows());
513 // c_size[1] = static_cast<int>(RadiationApproximation[0].C[i][j].cols());
514
515 c_size[0] = static_cast<int>(RadiationApproximation[0].C[i][j].rows());
516 c_size[1] = static_cast<int>(RadiationApproximation[0].C[i][j].cols());
517
518 cache.write( reinterpret_cast<char*>(c_size), sizeof(c_size) );
519 cache.write( reinterpret_cast<char*>(RadiationApproximation[0].C[i][j].data()),sizeof(double)*c_size[0]*c_size[1] );
520
521// d_size[0] = static_cast<int>(RadiationApproximation[0].D[i][j].rows());
522// d_size[1] = static_cast<int>(RadiationApproximation[0].D[i][j].cols());
523//
524 d_size[0] = static_cast<int>(RadiationApproximation[0].D[i][j].rows());
525 d_size[1] = static_cast<int>(RadiationApproximation[0].D[i][j].cols());
526
527 cache.write( reinterpret_cast<char*>(d_size), sizeof(d_size) );
528 cache.write( reinterpret_cast<char*>(RadiationApproximation[0].D[i][j].data()),sizeof(double)*d_size[0]*d_size[1] );
529
530 //std::cout << "Caching speed: " << MySpeed << " ("<<i<<","<<j<<") - " << " wrote A[ "<<a_size[0]<<" x "<<a_size[1]<<" ]"<<std::endl;
531 }
532 }
533
534 cache.write( reinterpret_cast<char*>(RadiationApproximation[0].Ainf.data()),sizeof(double)*6*6 );
535 cache.close();
536 }
537
538 bool ZeroSpeedIdentificationFromCache()
539 {
540 std::string cachebase = fname;
541 std::stringstream ss; ss << cachebase <<".identcache0";
542 std::fstream cache(ss.str().c_str(),std::ios::in|std::ios::binary);
543 if( !cache.good() ) return false;
544
545 int a_size[2];
546 int b_size[2];
547 int c_size[2];
548 int d_size[2];
549
550 double Ainf_tmp[6*6];
551 TimeDomainRadiation radiationModel;
552 for( int i=0; i < 6; i++)
553 {
554 for( int j=0; j < 6; j++)
555 {
556 cache.read( reinterpret_cast<char*>(a_size), sizeof(a_size) );
557 std::vector<double> A_tmp( a_size[0]*a_size[1] );
558
559 cache.read( reinterpret_cast<char*>(A_tmp.data()),sizeof(double)*a_size[0]*a_size[1] );
560
561
562 cache.read( reinterpret_cast<char*>(b_size), sizeof(b_size) );
563 std::vector<double> B_tmp( b_size[0]*b_size[1] );
564
565 cache.read( reinterpret_cast<char*>(B_tmp.data()),sizeof(double)*b_size[0]*b_size[1] );
566
567 cache.read( reinterpret_cast<char*>(c_size), sizeof(c_size) );
568 std::vector<double> C_tmp( c_size[0]*c_size[1] );
569
570 cache.read( reinterpret_cast<char*>(C_tmp.data()),sizeof(double)*c_size[0]*c_size[1] );
571
572
573 cache.read( reinterpret_cast<char*>(d_size), sizeof(d_size) );
574 std::vector<double> D_tmp( d_size[0]*d_size[1] );
575
576 cache.read( reinterpret_cast<char*>(D_tmp.data()),sizeof(double)*d_size[0]*d_size[1] );
577
578 //std::cout << "Recovered speed: " << MySpeed << " ("<<i<<","<<j<<") - " << " wrote A["<<a_size[0]<<"x"<<a_size[1]<<"]"<<std::endl;
579
580 radiationModel.A[i][j] = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>::Zero(a_size[0],a_size[1]);
581 radiationModel.B[i][j] = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>::Zero(b_size[0],b_size[1]);
582 radiationModel.C[i][j] = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>::Zero(c_size[0],c_size[1]);
583 radiationModel.D[i][j] = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>::Zero(d_size[0],d_size[1]);
584
585 for( int k=0; k < a_size[0]; k++)
586 for(int l=0; l < a_size[1]; l++)
587 {
588 assert(a_size[0]*a_size[1] > (l+k*a_size[1]) );
589 radiationModel.A[i][j](k,l) = A_tmp[k+l*a_size[0]];
590 }
591 for( int k=0; k < b_size[0]; k++)
592 for(int l=0; l < b_size[1]; l++)
593 {
594 assert(b_size[0]*b_size[1] > (l+k*b_size[1]) );
595 radiationModel.B[i][j](k,l) = B_tmp[k+l*b_size[0]];
596 }
597
598 for( int k=0; k < c_size[0]; k++)
599 for(int l=0; l < c_size[1]; l++)
600 {
601 assert(c_size[0]*c_size[1] > (l+k*c_size[1]) );
602 radiationModel.C[i][j](k,l) = C_tmp[k+l*c_size[0]];
603 }
604 for( int k=0; k < d_size[0]; k++)
605 for(int l=0; l < d_size[1]; l++)
606 {
607 assert(d_size[0]*d_size[1] > (l+k*d_size[1]) );
608 radiationModel.D[i][j](k,l) = D_tmp[k+l*d_size[0]];
609 }
610 }
611 }
612
613 cache.read( reinterpret_cast<char*>(Ainf_tmp),sizeof(double)*6*6);
614
615 for(int i=0; i < 6; i++)
616 for(int j=0; j < 6; j++)
617 radiationModel.Ainf(i,j)=Ainf_tmp[j+i*6];
618
619 radiationModel.Speed = 0;
620 RadiationApproximation.insert( std::make_pair(0,radiationModel) );
621 cache.close();
622 return true;
623 }
624
625 void IdentificaitonToCache()
626 {
627 std::string cachebase = fname;
628 std::stringstream ss; ss << cachebase << ".identcache1";
629
630 std::fstream cache(ss.str().c_str(),std::ios::out|std::ios::binary);
631
632 int a_size[2];
633 int b_size[2];
634 int c_size[2];
635 int d_size[2];
636
637 cache.write( reinterpret_cast<char*>(&NoSpeed),sizeof(unsigned int) );
638
639 for(unsigned int SpeedIdx=0; SpeedIdx < NoSpeed; SpeedIdx++)
640 {
641 double MySpeed = Speed[SpeedIdx];
642
643 cache.write( reinterpret_cast<char*>(&MySpeed),sizeof(double) );
644 for( int i=0; i < 6; i++)
645 {
646 for( int j=0; j < 6; j++)
647 {
648// a_size[0] = static_cast<int>(RadiationApproximation[SpeedIdx].A[i][j].rows());
649// a_size[1] = static_cast<int>(RadiationApproximation[SpeedIdx].A[i][j].cols());
650
651 a_size[0] = static_cast<int>(RadiationApproximation[SpeedIdx].A[i][j].rows());
652 a_size[1] = static_cast<int>(RadiationApproximation[SpeedIdx].A[i][j].cols());
653
654 cache.write( reinterpret_cast<char*>(a_size), sizeof(a_size) );
655 cache.write( reinterpret_cast<char*>(RadiationApproximation[SpeedIdx].A[i][j].data()),sizeof(double)*a_size[0]*a_size[1] );
656
657// b_size[0] = static_cast<int>(RadiationApproximation[SpeedIdx].B[i][j].rows());
658// b_size[1] = static_cast<int>(RadiationApproximation[SpeedIdx].B[i][j].cols());
659
660 b_size[0] = static_cast<int>(RadiationApproximation[SpeedIdx].B[i][j].rows());
661 b_size[1] = static_cast<int>(RadiationApproximation[SpeedIdx].B[i][j].cols());
662
663 cache.write( reinterpret_cast<char*>(b_size), sizeof(b_size) );
664 cache.write( reinterpret_cast<char*>(RadiationApproximation[SpeedIdx].B[i][j].data()),sizeof(double)*b_size[0]*b_size[1] );
665
666// c_size[0] = static_cast<int>(RadiationApproximation[SpeedIdx].C[i][j].rows());
667// c_size[1] = static_cast<int>(RadiationApproximation[SpeedIdx].C[i][j].cols());
668
669 c_size[0] = static_cast<int>(RadiationApproximation[SpeedIdx].C[i][j].rows());
670 c_size[1] = static_cast<int>(RadiationApproximation[SpeedIdx].C[i][j].cols());
671
672 cache.write( reinterpret_cast<char*>(c_size), sizeof(c_size) );
673 cache.write( reinterpret_cast<char*>(RadiationApproximation[SpeedIdx].C[i][j].data()),sizeof(double)*c_size[0]*c_size[1] );
674
675// d_size[0] = static_cast<int>(RadiationApproximation[SpeedIdx].D[i][j].rows());
676// d_size[1] = static_cast<int>(RadiationApproximation[SpeedIdx].D[i][j].cols());
677
678 d_size[0] = static_cast<int>(RadiationApproximation[SpeedIdx].D[i][j].rows());
679 d_size[1] = static_cast<int>(RadiationApproximation[SpeedIdx].D[i][j].cols());
680
681 cache.write( reinterpret_cast<char*>(d_size), sizeof(d_size) );
682 cache.write( reinterpret_cast<char*>(RadiationApproximation[SpeedIdx].D[i][j].data()),sizeof(double)*d_size[0]*d_size[1] );
683
684 //std::cout << "Caching speed: " << MySpeed << " ("<<i<<","<<j<<") - " << " wrote A[ "<<a_size[0]<<" x "<<a_size[1]<<" ]"<<std::endl;
685 }
686 }
687 cache.write( reinterpret_cast<char*>(RadiationApproximation[SpeedIdx].Ainf.data()),sizeof(double)*6*6 );
688 }
689 cache.close();
690 }
691
692 bool IdentificationFromCache()
693 {
694 std::string cachebase = fname;
695 std::stringstream ss; ss << cachebase <<".identcache1";
696 std::fstream cache(ss.str().c_str(),std::ios::in|std::ios::binary);
697 if( !cache.good() ) return false;
698
699 int a_size[2];
700 int b_size[2];
701 int c_size[2];
702 int d_size[2];
703
704 double Ainf_tmp[6*6];
705
706 // A cache made for other speeds is not used (MAR-0027): the model is identified again.
707 unsigned int cachedNoSpeed = 0;
708 cache.read( reinterpret_cast<char*>(&cachedNoSpeed),sizeof(unsigned int) );
709 if( !cache.good() || cachedNoSpeed != NoSpeed ) return false;
710 RadiationApproximation.clear();
711
712 for(unsigned int SpeedIdx=0; SpeedIdx < NoSpeed; SpeedIdx++)
713 {
714 double MySpeed;
715
716 cache.read( reinterpret_cast<char*>(&MySpeed),sizeof(double) );
717 if( !cache.good() || MySpeed != Speed[SpeedIdx] ){ RadiationApproximation.clear(); return false; }
718
719 TimeDomainRadiation radiationModel;
720 for( int i=0; i < 6; i++)
721 {
722 for( int j=0; j < 6; j++)
723 {
724 cache.read( reinterpret_cast<char*>(a_size), sizeof(a_size) );
725 std::vector<double> A_tmp( a_size[0]*a_size[1] );
726
727 cache.read( reinterpret_cast<char*>(A_tmp.data()),sizeof(double)*a_size[0]*a_size[1] );
728
729 cache.read( reinterpret_cast<char*>(b_size), sizeof(b_size) );
730 std::vector<double> B_tmp( b_size[0]*b_size[1] );
731
732 cache.read( reinterpret_cast<char*>(B_tmp.data()),sizeof(double)*b_size[0]*b_size[1] );
733
734 cache.read( reinterpret_cast<char*>(c_size), sizeof(c_size) );
735 std::vector<double> C_tmp( c_size[0]*c_size[1] );
736
737 cache.read( reinterpret_cast<char*>(C_tmp.data()),sizeof(double)*c_size[0]*c_size[1] );
738
739 cache.read( reinterpret_cast<char*>(d_size), sizeof(d_size) );
740 std::vector<double> D_tmp( d_size[0]*d_size[1] );
741
742 cache.read( reinterpret_cast<char*>(D_tmp.data()),sizeof(double)*d_size[0]*d_size[1] );
743 //std::cout << "Recovered speed: " << MySpeed << " ("<<i<<","<<j<<") - " << " wrote A["<<a_size[0]<<"x"<<a_size[1]<<"]"<<std::endl;
744
745 radiationModel.A[i][j] = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>::Zero(a_size[0],a_size[1]);
746 radiationModel.B[i][j] = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>::Zero(b_size[0],b_size[1]);
747 radiationModel.C[i][j] = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>::Zero(c_size[0],c_size[1]);
748 radiationModel.D[i][j] = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>::Zero(d_size[0],d_size[1]);
749
750 for( int k=0; k < a_size[0]; k++)
751 for(int l=0; l < a_size[1]; l++)
752 {
753 assert(a_size[0]*a_size[1] > (l+k*a_size[1]) );
754 radiationModel.A[i][j](k,l) = A_tmp[k+l*a_size[0]];
755 }
756 for( int k=0; k < b_size[0]; k++)
757 for(int l=0; l < b_size[1]; l++)
758 {
759 assert(b_size[0]*b_size[1] > (l+k*b_size[1]) );
760 radiationModel.B[i][j](k,l) = B_tmp[k+l*b_size[0]];
761 }
762
763 for( int k=0; k < c_size[0]; k++)
764 for(int l=0; l < c_size[1]; l++)
765 {
766 assert(c_size[0]*c_size[1] > (l+k*c_size[1]) );
767 radiationModel.C[i][j](k,l) = C_tmp[k+l*c_size[0]];
768 }
769 for( int k=0; k < d_size[0]; k++)
770 for(int l=0; l < d_size[1]; l++)
771 {
772 assert(d_size[0]*d_size[1] > (l+k*d_size[1]) );
773 radiationModel.D[i][j](k,l) = D_tmp[k+l*d_size[0]];
774 }
775 }
776 }
777
778 cache.read( reinterpret_cast<char*>(Ainf_tmp),sizeof(double)*6*6);
779
780 for(int i=0; i < 6; i++)
781 for(int j=0; j < 6; j++)
782 radiationModel.Ainf(i,j)=Ainf_tmp[j+i*6];
783
784 radiationModel.Speed = MySpeed;
785 RadiationApproximation.insert( std::make_pair(SpeedIdx, radiationModel ));
786 }
787 cache.close();
788 return true;
789 }
790
791
792 void RadiationDerivative(int DOF1, int DOF2, double *derivative, const double *const radiation_state, const double *ship_state, unsigned int SpeedNo=0 )
793 {
794 const TimeDomainRadiation& radiationModel = RadiationApproximation[SpeedNo];
795
796 const size_t state_size = radiationModel.A[DOF1][DOF2].cols();
797 Eigen::Map<const Eigen::MatrixXd,Eigen::Unaligned> X(radiation_state,state_size,1);
798 Eigen::Map<Eigen::MatrixXd,Eigen::Unaligned> dotX(derivative,state_size,1);
799 dotX.noalias() = radiationModel.A[DOF1][DOF2]*X + radiationModel.B[DOF1][DOF2]*ship_state[DOF2];
800 }
801
802 double RadiationOutput(int DOF1, int DOF2, const double *const radiation_state, const double * const ship_state, unsigned int SpeedNo=0)
803 {
804 const TimeDomainRadiation& radiationModel = RadiationApproximation[SpeedNo];
805
806 const size_t state_size = radiationModel.A[DOF1][DOF2].cols();
807 Eigen::Map<const Eigen::MatrixXd,Eigen::Unaligned> X(radiation_state,state_size,1);
808 return (radiationModel.C[DOF1][DOF2]*X + radiationModel.D[DOF1][DOF2]*ship_state[DOF2]).value();
809 }
810
811
812 std::map<int,TimeDomainRadiation,std::less<int>,Eigen::aligned_allocator<std::pair<const int,TimeDomainRadiation> > > RadiationApproximation;
813
814 protected:
815
816 virtual bool is2Ddata(){
817 return false;
818 }
819
820 void RequireTwoDimensionalData()
821 {
822 if( !is2Ddata() ) throw std::runtime_error("Radiation identification supports only 2D (VERES) potential data: other data would get no memory terms");
823 }
824
825 std::string fname;
826
827 std::vector<std::string> m_warnings;
828
829 // NaN until read from the file
830 double Lpp = std::numeric_limits<double>::quiet_NaN(), Bredth = std::numeric_limits<double>::quiet_NaN(), Draught = std::numeric_limits<double>::quiet_NaN();
831
832 double LCG = std::numeric_limits<double>::quiet_NaN(), VCG = std::numeric_limits<double>::quiet_NaN();
833 double LCB = std::numeric_limits<double>::quiet_NaN(), VCB = std::numeric_limits<double>::quiet_NaN();
834
835
836 unsigned int NoSpeed = 0;
837 unsigned int NoHeading;
838 unsigned int NoDOF;
839 unsigned int NoFrequency;
840
841 std::vector<double> Speed;
842 std::vector<double> Heading;
843 std::vector<std::vector<double> > Frequency;
844
845 std::unique_ptr<FroudeKrylovForce> FroudeFrylov;
846 std::unique_ptr<WaveDriftForce> WaveDrift;
847
848 Eigen::Matrix<double,6,6> Mrbeig;
849 std::vector<std::vector<Eigen::Matrix<double,6,6> > > A;
850 std::vector<std::vector<Eigen::Matrix<double,6,6> > > B;
851 std::vector<std::vector<Eigen::Matrix<double,6,6> > > C;
852 std::vector<std::vector<Eigen::Matrix<double,6,6> > > Aadd;
853 std::vector<std::vector<Eigen::Matrix<double,6,6> > > Badd;
854 std::vector<std::vector<Eigen::Matrix<double,6,6> > > Cadd;
855
856 Eigen::Matrix<double,3,1> rg;
857 Eigen::Matrix<double,3,1> ro;
858 std::map<std::string,double> param;
859 };
860}
861#endif
Definition PotentialData.h:31
std::vector< std::vector< double > > Frequency
one row per speed; a row stays empty until it is read
Definition PotentialData.h:843
Eigen::Matrix< double, 6, 1 > GetFirstOrderForce(const Eigen::Matrix< double, 3, 1 > &Kxy, double ShipSpeed, double RaoSpeed, double WaveDirection, double WaveAmplitude, double WaveFrequency, double WaveNumber, double WavePhase, double Time)
Definition PotentialData.h:179
const std::vector< std::string > & Warnings() const
What the reader noted while it read the data (a missing optional file). The ship logs each once.
Definition PotentialData.h:73
Superclass of control algorithms for the control system.
Definition CableAttach.h:58
Definition PotentialData.h:36