1#ifndef _CPOTENTIAL_DATA_H_
2#define _CPOTENTIAL_DATA_H_
14#include <Eigen/Eigenvalues>
18#include "../HullData/HullData.h"
20#include "./Wave/FroudeKrylov.h"
21#include "./Wave/WaveDrift.h"
23#include "Identification/FreqDomainIdent.h"
30 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
34 EIGEN_MAKE_ALIGNED_OPERATOR_NEW
39 Ainf = Eigen::Matrix< double, 6, 6 >::Zero( );
41 for(
int i=0; i <6; i++)
42 for(
int j=0; j <6; j++)
44 A[i][j] = Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic >::Zero( 1, 1 );;
45 B[i][j] = Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic >::Zero( 1, 1 );;
46 C[i][j] = Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic >::Zero( 1, 1 );;
47 D[i][j] = Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic >::Zero( 1, 1 );;
52 Eigen::Matrix<double, 6, 6> Ainf;
57 Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> A[6][6];
58 Eigen::Matrix<double, Eigen::Dynamic, 1 > B[6][6];
59 Eigen::Matrix<double, 1 , Eigen::Dynamic> C[6][6];
60 Eigen::Matrix<double, Eigen::Dynamic, 1 > D[6][6];
71 virtual void Read(
const std::string &fname,
int flipPhaseForOSC =0) = 0;
72 virtual Eigen::Matrix<double,6,6> GetHToO() = 0;
74 virtual void SetOriginWorigin() = 0;
75 virtual void SetOriginGorigin() = 0;
76 virtual void SetOrigin( Eigen::Matrix<double,3,1> &r ) = 0;
78 Eigen::Matrix<double,6,6> GetMassRigidBody()
80 return GetHToO().transpose()*Mrbeig*GetHToO();
83 Eigen::Matrix<double,6,6> GetAinf(
unsigned int speedno=0)
85 return RadiationApproximation[speedno].Ainf;
88 Eigen::Matrix<double,6,6> GetMassTotal(
unsigned int speedno=0)
90 return RadiationApproximation[speedno].Ainf+GetHToO().transpose()*Mrbeig*GetHToO();
93 Eigen::Matrix<double,6,6> GetAw(
unsigned int FreqNo,
unsigned int SpeedNo=0)
95 return GetHToO().transpose()*A[SpeedNo][FreqNo]*GetHToO();
98 Eigen::Matrix<double,6,6> GetBw(
unsigned int FreqNo,
unsigned int SpeedNo=0)
100 return GetHToO().transpose()*B[SpeedNo][FreqNo]*GetHToO();
103 virtual Eigen::Matrix<double,6,6> GetCw(
unsigned int FreqNo,
unsigned int SpeedNo=0)
105 return GetHToO().transpose()*C[SpeedNo][FreqNo]*GetHToO();
108 Eigen::Matrix<double,1,Eigen::Dynamic> GetAij(
unsigned int DOF1,
unsigned int DOF2,
unsigned int SpeedNo=0)
110 Eigen::Matrix<double,1,Eigen::Dynamic> A = Eigen::Matrix<double,1,Eigen::Dynamic>::Zero(1,NoFrequency);
112 for(
unsigned int i=0; i < NoFrequency; i++)
114 Eigen::Matrix<double,6,6> Aw = GetAw(i,SpeedNo);
115 A(i) = Aw(DOF1,DOF2);
120 Eigen::Matrix<double,1,Eigen::Dynamic> GetBij(
unsigned int DOF1,
unsigned int DOF2,
unsigned int SpeedNo=0)
122 Eigen::Matrix<double,1,Eigen::Dynamic> B = Eigen::Matrix<double,1,Eigen::Dynamic>::Zero(1,NoFrequency);
124 for(
unsigned int i=0; i < NoFrequency; i++)
126 Eigen::Matrix<double,6,6> Bw = GetBw(i,SpeedNo);
127 B(i) = Bw(DOF1,DOF2);
132 Eigen::Matrix<double,1,Eigen::Dynamic> GetCij(
unsigned int DOF1,
unsigned int DOF2,
unsigned int SpeedNo=0)
134 Eigen::Matrix<double,1,Eigen::Dynamic> C = Eigen::Matrix<double,1,Eigen::Dynamic>::Zero(1,NoFrequency);
136 for(
unsigned int i=0; i < NoFrequency; i++)
138 Eigen::Matrix<double,6,6> Cw = GetCw(i,SpeedNo);
139 C(i) = Cw(DOF1,DOF2);
144 Eigen::Matrix<double,1,Eigen::Dynamic> GetSpeed()
146 Eigen::Matrix<double,1,Eigen::Dynamic> speed = Eigen::Matrix<double,1,Eigen::Dynamic>::Zero(1,NoSpeed);
147 for(
unsigned int i=0; i < NoSpeed; i++ ) speed(i) = Speed[i];
151 Eigen::Matrix<double,1,Eigen::Dynamic> GetHeading()
153 Eigen::Matrix<double,1,Eigen::Dynamic> head = Eigen::Matrix<double,1,Eigen::Dynamic>::Zero(1,NoHeading);
154 for(
unsigned int i=0; i < NoHeading; i++ ) head(i) = Heading[i];
158 Eigen::Matrix<double,1,Eigen::Dynamic> GetFrequency(
int SpeedNo=0)
160 Eigen::Matrix<double,1,Eigen::Dynamic> freq = Eigen::Matrix<double,1,Eigen::Dynamic>::Zero(1,NoFrequency);
161 for(
unsigned int i=0; i < NoFrequency; i++ ) freq(i) = Frequency[SpeedNo][i];
165 virtual Eigen::Matrix<double,3,1> GetRg()
170 virtual Eigen::Matrix<double,3,1> GetRo()
175 Eigen::Matrix<double,6,1> GetFirstOrderForce(
const Eigen::Matrix<double,3,1> &Kxy,
double ShipSpeed,
176 double WaveDirection,
double WaveAmplitude,
double WaveFrequency,
double WaveNumber,
double WavePhase,
181 double EncounterFrequency = WaveFrequency-WaveNumber*ShipSpeed;
182 double Direction = EncounterFrequency/std::abs(EncounterFrequency);
183 EncounterFrequency = std::abs(EncounterFrequency);
187 for(
int DOF=0; DOF < 6; DOF++)
190 std::complex<double> RAO = FroudeFrylov->GetRAOValue(DOF,WaveDirection,std::abs(ShipSpeed),WaveFrequency);
193 double Amplitude = std::abs(RAO);
194 double Phase = atan2( RAO.imag(), RAO.real() );
198 double ThisWaveHeight = WaveAmplitude*sin(EncounterFrequency *Time -Kxy(0) -Kxy(1) + Phase - WavePhase );
199 Fptr[DOF] = Direction*Amplitude*ThisWaveHeight;
202 Eigen::Map<Eigen::Matrix<double,6,1>,Eigen::Unaligned> tmp(Fptr);
203 return GetHToO().transpose()*tmp;
206 Eigen::Matrix<double,6,1> GetSecondOrderForce(Eigen::Matrix<double,3,1> ShipPosition,
double ShipSpeed,
207 double WaveDirection,
double WaveHeight,
double WaveFrequency,
double WaveNumber,
double WavePhase,
211 for(
int DOF=0; DOF < 6; DOF++)
215 std::complex<double> Amplitude = WaveDrift->GetRAOValue(DOF,ShipSpeed,WaveFrequency,WaveDirection);
216 Fptr[DOF] = std::pow(WaveHeight,2)*Amplitude.real();
219 Eigen::Map<Eigen::Matrix<double,6,1>,Eigen::Unaligned> tmp(Fptr);
220 return GetHToO().transpose()*tmp;
223 void GenerateZeroSpeedTimeDomainApproximation()
226 if( !RadiationApproximation.empty() ) RadiationApproximation.clear();
227 if( ZeroSpeedIdentificationFromCache() ){
234 FreqDomainIdent Identification;
235 Identification.SetPotentialData(
this);
236 Identification.IdentificationMethod(FreqDomainIdent::VECTOR_FITTING);
238 TimeDomainRadiation zeroSpeedSystem;
241 for(
int i=0; i < 6; i++)
244 for(
int j=0; j <= i; j++)
246 if( (i == 1 || i == 3 || i == 5) && (j==0 || j==2 || j==4) )
continue;
247 if( (j == 1 || j == 3 || j == 5) && (i==0 || i==2 || i==4) )
continue;
249 std::cout <<
"Identifying (i/j) = (" << i <<
"/" << j <<
") ";
251 if( is2Ddata() && (i!=0 && j!=0)){
252 FreqDomainIdent::StateSpace SS = Identification.IdentifySS(i,j);
253 zeroSpeedSystem.Ainf(i,j) = Identification.GetAinf(i,j);
254 zeroSpeedSystem.A[i][j] = SS.A;
255 zeroSpeedSystem.B[i][j] = SS.B;
256 zeroSpeedSystem.C[i][j] = SS.C;
257 zeroSpeedSystem.D[i][j] = SS.D;
259 zeroSpeedSystem.Ainf(j,i) = zeroSpeedSystem.Ainf(i,j);
260 zeroSpeedSystem.A[j][i] = zeroSpeedSystem.A[i][j];
261 zeroSpeedSystem.B[j][i] = zeroSpeedSystem.B[i][j];
262 zeroSpeedSystem.C[j][i] = zeroSpeedSystem.C[i][j];
263 zeroSpeedSystem.D[j][i] = zeroSpeedSystem.D[i][j];
265 std::cout <<
" Zero data" << std::endl;
266 zeroSpeedSystem.Ainf(i,j) = GetAij(i,j,0)[0];
267 zeroSpeedSystem.A[i][j] = Eigen::Matrix<double,1,1>::Zero();
268 zeroSpeedSystem.B[i][j] = Eigen::Matrix<double,1,1>::Zero();
269 zeroSpeedSystem.C[i][j] = Eigen::Matrix<double,1,1>::Zero();
270 zeroSpeedSystem.D[i][j] = Eigen::Matrix<double,1,1>::Zero();
272 zeroSpeedSystem.Ainf(j,i) = GetAij(i,j,0)[0];
273 zeroSpeedSystem.A[j][i] = Eigen::Matrix<double,1,1>::Zero();
274 zeroSpeedSystem.B[j][i] = Eigen::Matrix<double,1,1>::Zero();
275 zeroSpeedSystem.C[j][i] = Eigen::Matrix<double,1,1>::Zero();
276 zeroSpeedSystem.D[j][i] = Eigen::Matrix<double,1,1>::Zero();
280 RadiationApproximation.insert( std::make_pair(0,zeroSpeedSystem));
281 ZeroSpeedIdentificaitonToCache();
284 void GenerateTimeDomainApproximation()
287 if( IdentificationFromCache() )
return;
289 FreqDomainIdent Identification;
290 Identification.SetPotentialData(
this);
291 Identification.IdentificationMethod(FreqDomainIdent::VECTOR_FITTING);
293 for (
unsigned int speed=0; speed < NoSpeed; speed++ )
295 TimeDomainRadiation speedSystem;
297 for(
int i=0; i < 6; i++)
299 for(
int j=0; j <= i; j++)
301 if( (i == 1 || i == 3 || i == 5) && (j==0 || j==2 || j==4) )
continue;
302 if( (j == 1 || j == 3 || j == 5) && (i==0 || i==2 || i==4) )
continue;
304 std::cout <<
"Identifying (i/j) = ("<<speed<<
")(" << i <<
"/" << j <<
") ";
306 if( is2Ddata() && (i==0||j==0)){
307 FreqDomainIdent::StateSpace SS = Identification.IdentifySS(i,j,speed);
308 speedSystem.Ainf(i,j) = Identification.GetAinf(i,j,speed);
309 speedSystem.A[i][j] = SS.A;
310 speedSystem.B[i][j] = SS.B;
311 speedSystem.C[i][j] = SS.C;
312 speedSystem.D[i][j] = SS.D;
314 speedSystem.Ainf(j,i) = speedSystem.Ainf(i,j);
315 speedSystem.A[j][i] = speedSystem.A[i][j];
316 speedSystem.B[j][i] = speedSystem.B[i][j];
317 speedSystem.C[j][i] = speedSystem.C[i][j];
318 speedSystem.D[j][i] = speedSystem.D[i][j];
320 FreqDomainIdent::StateSpace SS = Identification.IdentifySS(i,j);
321 speedSystem.Ainf(i,j) = GetAij(i,j,speed)[0];
322 speedSystem.A[i][j] = Eigen::Matrix<double,1,1>::Zero();
323 speedSystem.B[i][j] = Eigen::Matrix<double,1,1>::Zero();
324 speedSystem.C[i][j] = Eigen::Matrix<double,1,1>::Zero();
325 speedSystem.D[i][j] = Eigen::Matrix<double,1,1>::Zero();
327 speedSystem.Ainf(j,i) = GetAij(i,j,speed)[0];
328 speedSystem.A[j][i] = Eigen::Matrix<double,1,1>::Zero();
329 speedSystem.B[j][i] = Eigen::Matrix<double,1,1>::Zero();
330 speedSystem.C[j][i] = Eigen::Matrix<double,1,1>::Zero();
331 speedSystem.D[j][i] = Eigen::Matrix<double,1,1>::Zero();
334 std::cout <<
"identified new data - " << std::endl;
337 RadiationApproximation.insert( std::make_pair(speed,speedSystem));
339 IdentificaitonToCache();
345 Eigen::Matrix<double,6,6> M = GetMassRigidBody();
347 std::cout <<
"%Ship information form file: " << fname << std::endl;
348 std::cout <<
"%\tLpp:\t" << Lpp << std::endl;
349 std::cout <<
"%\tB:\t" << Bredth << std::endl;
350 std::cout <<
"%\tT:\t" << Draught << std::endl;
351 std::cout <<
"%\tr_cg\t" << LCG<<
"\t,\t0\t,\t"<<VCG<<
" (from AP and base line)" << std::endl;
352 std::cout <<
"%\tr_cb\t" << LCB<<
"\t,\t0\t,\t"<<VCB<<
" (from AP and base line)" << std::endl;
354 std::cout <<
"%\tPotential data set information: " << std::endl;
355 std::cout <<
"%\t\tNumber of speeds:\t" <<NoSpeed<< std::endl;
356 std::cout <<
"speed = [\t\t" ;
357 for(
unsigned int i=0; i < NoSpeed; i++ ) std::cout << Speed[i] <<
"\t";
358 std::cout <<
"];" << std::endl;
360 std::cout <<
"%\t\tNumber of headings:\t" <<NoHeading<< std::endl;
361 std::cout <<
"head =[\t\t" ;
362 for(
unsigned int i=0; i < NoHeading; i++ ) std::cout << Heading[i] <<
"\t";
363 std::cout <<
"];" << std::endl;
365 std::cout <<
"%\t\tNumber of frequencies:\t" <<NoFrequency<< std::endl;
366 std::cout <<
"freq =[\t\t" ;
367 for(
unsigned int i=0; i < NoFrequency; i++ ) std::cout << Frequency[i] <<
"\t";
368 std::cout <<
"];" << std::endl;
370 for(
unsigned int i=0; i < NoSpeed; i++ )
372 std::cout <<
"%Total mass matrix for Speed " << i <<
" V="<<Speed[i] <<
" m/s" << std::endl;
374 Eigen::Matrix<double,6,6> Ainf = GetAinf(i);
376 Eigen::Matrix<double,6,6> Mtotal = M + Ainf;
377 std::cout << std::scientific;
378 for(
int j=0; j < 6; j++)
380 if( j== 0) std::cout <<
"M{" <<i+1<<
"}\t=[";
381 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);
382 if( j== 5) std::cout <<
"];";
383 std::cout <<
";" <<std::endl;
389 for(
unsigned int i=0; i < NoSpeed; i++)
391 std::cout <<
"%Fluid memory model for Speed " << i <<
" V="<<Speed[i] <<
" m/s" << std::endl;
392 TimeDomainRadiation mod = RadiationApproximation[i];
394 for(
int j=0; j < 6; j++ )
396 for(
int k=0; k < 6; k++ )
398 size_t num_states = mod.A[k][j].cols();
400 if( num_states == 1 )
402 std::cout <<
"A{"<<i+1<<
"}{"<<j+1<<
"}{"<<k+1<<
"}" <<
"=\t" <<
"[" << mod.A[j][k](0,0) <<
"];"<< std::endl;
403 std::cout << std::endl;
404 std::cout <<
"B{"<<i+1<<
"}{"<<j+1<<
"}{"<<k+1<<
"}" <<
" =\t" <<
"[" << mod.B[j][k](0,0) <<
"];"<< std::endl;
405 std::cout << std::endl;
406 std::cout <<
"C{"<<i+1<<
"}{"<<j+1<<
"}{"<<k+1<<
"}" <<
" =\t" <<
"[" << mod.C[j][k](0,0) <<
"];"<< std::endl;
407 std::cout << std::endl;
408 std::cout <<
"D{"<<i+1<<
"}{"<<j+1<<
"}{"<<k+1<<
"}" <<
" =\t" <<
"[" << mod.D[j][k](0,0) <<
"];"<< std::endl;
410 else if (num_states == 2 )
412 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;
413 std::cout << std::endl;
414 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;
415 std::cout << std::endl;
416 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;
417 std::cout << std::endl;
418 std::cout <<
"D{"<<i+1<<
"}{"<<j+1<<
"}{"<<k+1<<
"}"<<
" =\t" <<
"[" << mod.D[j][k](0,0) <<
"];"<< std::endl;
423 std::cout <<
"A{"<<i+1<<
"}{"<<j+1<<
"}{"<<k+1<<
"}"<<
" =\t" <<
"[";
424 for(
size_t s=0; s < num_states; s++)
426 if(s!=0) std::cout <<
" \t ";
427 for(
size_t c=0; c < num_states; c++)
429 std::cout << mod.A[j][k](s,c);
430 if( c < (num_states-1) ) std::cout <<
"\t,";
432 if( s < (num_states-1) ) std::cout << std::endl;
434 std::cout <<
"];"<< std::endl;
436 std::cout << std::endl;
437 std::cout <<
"B{"<<i+1<<
"}{"<<j+1<<
"}{"<<k+1<<
"}"<<
" =\t" <<
"[";
438 for(
size_t s=0; s < num_states; s++)
440 if( s != 0 )std::cout <<
" \t ";
441 std::cout << mod.B[j][k](s,0);
442 if( s < (num_states-1) ) std::cout <<
";" <<std::endl;
444 std::cout <<
"];"<< std::endl;
445 std::cout << std::endl;
447 std::cout <<
"C{"<<i+1<<
"}{"<<j+1<<
"}{"<<k+1<<
"}"<<
" =\t" <<
"[";
448 for(
size_t s=0; s < num_states; s++)
450 std::cout << mod.C[j][k](0,s);
451 if( s < (num_states-1) ) std::cout <<
",";
453 std::cout <<
"];"<< std::endl;
455 std::cout << std::endl;
456 std::cout <<
"D{"<<i+1<<
"}{"<<j+1<<
"}{"<<k+1<<
"}"<<
" =\t" <<
"[" << mod.D[j][k](0,0) <<
"];"<< std::endl;
463 size_t GetTimeDomainStateSize(
int DOF1,
int DOF2,
int SpeedNo=0 )
465 return RadiationApproximation[SpeedNo].A[DOF1][DOF2].cols();
468 void ZeroSpeedIdentificaitonToCache()
470 std::string cachebase = fname;
471 std::stringstream ss; ss << cachebase <<
".identcache0";
473 std::fstream cache(ss.str().c_str(),std::ios::out|std::ios::binary);
480 for(
int i=0; i < 6; i++)
482 for(
int j=0; j < 6; j++)
487 a_size[0] =
static_cast<int>(RadiationApproximation[0].A[i][j].rows());
488 a_size[1] =
static_cast<int>(RadiationApproximation[0].A[i][j].cols());
490 cache.write(
reinterpret_cast<char*
>(a_size),
sizeof(a_size) );
491 cache.write(
reinterpret_cast<char*
>(RadiationApproximation[0].A[i][j].data()),
sizeof(
double)*a_size[0]*a_size[1] );
496 b_size[0] =
static_cast<int>(RadiationApproximation[0].B[i][j].rows());
497 b_size[1] =
static_cast<int>(RadiationApproximation[0].B[i][j].cols());
499 cache.write(
reinterpret_cast<char*
>(b_size),
sizeof(b_size) );
500 cache.write(
reinterpret_cast<char*
>(RadiationApproximation[0].B[i][j].data()),
sizeof(
double)*b_size[0]*b_size[1] );
506 c_size[0] =
static_cast<int>(RadiationApproximation[0].C[i][j].rows());
507 c_size[1] =
static_cast<int>(RadiationApproximation[0].C[i][j].cols());
509 cache.write(
reinterpret_cast<char*
>(c_size),
sizeof(c_size) );
510 cache.write(
reinterpret_cast<char*
>(RadiationApproximation[0].C[i][j].data()),
sizeof(
double)*c_size[0]*c_size[1] );
515 d_size[0] =
static_cast<int>(RadiationApproximation[0].D[i][j].rows());
516 d_size[1] =
static_cast<int>(RadiationApproximation[0].D[i][j].cols());
518 cache.write(
reinterpret_cast<char*
>(d_size),
sizeof(d_size) );
519 cache.write(
reinterpret_cast<char*
>(RadiationApproximation[0].D[i][j].data()),
sizeof(
double)*d_size[0]*d_size[1] );
525 cache.write(
reinterpret_cast<char*
>(RadiationApproximation[0].Ainf.data()),
sizeof(
double)*6*6 );
529 bool ZeroSpeedIdentificationFromCache()
531 std::string cachebase = fname;
532 std::stringstream ss; ss << cachebase <<
".identcache0";
533 std::fstream cache(ss.str().c_str(),std::ios::in|std::ios::binary);
534 if( !cache.good() )
return false;
545 double Ainf_tmp[6*6];
546 TimeDomainRadiation radiationModel;
547 for(
int i=0; i < 6; i++)
549 for(
int j=0; j < 6; j++)
551 cache.read(
reinterpret_cast<char*
>(a_size),
sizeof(a_size) );
552 A_tmp =
new double[a_size[0]*a_size[1]];
554 cache.read(
reinterpret_cast<char*
>(A_tmp),
sizeof(
double)*a_size[0]*a_size[1] );
557 cache.read(
reinterpret_cast<char*
>(b_size),
sizeof(b_size) );
558 B_tmp =
new double[b_size[0]*b_size[1]];
560 cache.read(
reinterpret_cast<char*
>(B_tmp),
sizeof(
double)*b_size[0]*b_size[1] );
562 cache.read(
reinterpret_cast<char*
>(c_size),
sizeof(c_size) );
563 C_tmp =
new double[c_size[0]*c_size[1]];
565 cache.read(
reinterpret_cast<char*
>(C_tmp),
sizeof(
double)*c_size[0]*c_size[1] );
568 cache.read(
reinterpret_cast<char*
>(d_size),
sizeof(d_size) );
569 D_tmp =
new double[d_size[0]*d_size[1]];
571 cache.read(
reinterpret_cast<char*
>(D_tmp),
sizeof(
double)*d_size[0]*d_size[1] );
575 radiationModel.A[i][j] = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>::Zero(a_size[0],a_size[1]);
576 radiationModel.B[i][j] = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>::Zero(b_size[0],b_size[1]);
577 radiationModel.C[i][j] = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>::Zero(c_size[0],c_size[1]);
578 radiationModel.D[i][j] = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>::Zero(d_size[0],d_size[1]);
580 for(
int k=0; k < a_size[0]; k++)
581 for(
int l=0; l < a_size[1]; l++)
583 assert(a_size[0]*a_size[1] > (l+k*a_size[1]) );
584 radiationModel.A[i][j](k,l) = A_tmp[k+l*a_size[0]];
586 for(
int k=0; k < b_size[0]; k++)
587 for(
int l=0; l < b_size[1]; l++)
589 assert(b_size[0]*b_size[1] > (l+k*b_size[1]) );
590 radiationModel.B[i][j](k,l) = B_tmp[k+l*b_size[0]];
593 for(
int k=0; k < c_size[0]; k++)
594 for(
int l=0; l < c_size[1]; l++)
596 assert(c_size[0]*c_size[1] > (l+k*c_size[1]) );
597 radiationModel.C[i][j](k,l) = C_tmp[k+l*c_size[0]];
599 for(
int k=0; k < d_size[0]; k++)
600 for(
int l=0; l < d_size[1]; l++)
602 assert(d_size[0]*d_size[1] > (l+k*d_size[1]) );
603 radiationModel.D[i][j](k,l) = D_tmp[k+l*d_size[0]];
612 cache.read(
reinterpret_cast<char*
>(Ainf_tmp),
sizeof(double)*6*6);
614 for(
int i=0; i < 6; i++)
615 for(
int j=0; j < 6; j++)
616 radiationModel.Ainf(i,j)=Ainf_tmp[j+i*6];
618 radiationModel.Speed = 0;
619 RadiationApproximation.insert( std::make_pair(0,radiationModel) );
624 void IdentificaitonToCache()
626 std::string cachebase = fname;
627 std::stringstream ss; ss << cachebase <<
".identcache1";
629 std::fstream cache(ss.str().c_str(),std::ios::out|std::ios::binary);
636 cache.write(
reinterpret_cast<char*
>(&NoSpeed),
sizeof(
unsigned int) );
638 for(
unsigned int SpeedIdx=0; SpeedIdx < NoSpeed; SpeedIdx++)
640 double MySpeed = Speed[SpeedIdx];
642 cache.write(
reinterpret_cast<char*
>(&MySpeed),
sizeof(
double) );
643 for(
int i=0; i < 6; i++)
645 for(
int j=0; j < 6; j++)
650 a_size[0] =
static_cast<int>(RadiationApproximation[SpeedIdx].A[i][j].rows());
651 a_size[1] =
static_cast<int>(RadiationApproximation[SpeedIdx].A[i][j].cols());
653 cache.write(
reinterpret_cast<char*
>(a_size),
sizeof(a_size) );
654 cache.write(
reinterpret_cast<char*
>(RadiationApproximation[SpeedIdx].A[i][j].data()),
sizeof(
double)*a_size[0]*a_size[1] );
659 b_size[0] =
static_cast<int>(RadiationApproximation[SpeedIdx].B[i][j].rows());
660 b_size[1] =
static_cast<int>(RadiationApproximation[SpeedIdx].B[i][j].cols());
662 cache.write(
reinterpret_cast<char*
>(b_size),
sizeof(b_size) );
663 cache.write(
reinterpret_cast<char*
>(RadiationApproximation[SpeedIdx].B[i][j].data()),
sizeof(
double)*b_size[0]*b_size[1] );
668 c_size[0] =
static_cast<int>(RadiationApproximation[SpeedIdx].C[i][j].rows());
669 c_size[1] =
static_cast<int>(RadiationApproximation[SpeedIdx].C[i][j].cols());
671 cache.write(
reinterpret_cast<char*
>(c_size),
sizeof(c_size) );
672 cache.write(
reinterpret_cast<char*
>(RadiationApproximation[SpeedIdx].C[i][j].data()),
sizeof(
double)*c_size[0]*c_size[1] );
677 d_size[0] =
static_cast<int>(RadiationApproximation[SpeedIdx].D[i][j].rows());
678 d_size[1] =
static_cast<int>(RadiationApproximation[SpeedIdx].D[i][j].cols());
680 cache.write(
reinterpret_cast<char*
>(d_size),
sizeof(d_size) );
681 cache.write(
reinterpret_cast<char*
>(RadiationApproximation[SpeedIdx].D[i][j].data()),
sizeof(
double)*d_size[0]*d_size[1] );
686 cache.write(
reinterpret_cast<char*
>(RadiationApproximation[SpeedIdx].Ainf.data()),
sizeof(
double)*6*6 );
691 bool IdentificationFromCache()
693 std::string cachebase = fname;
694 std::stringstream ss; ss << cachebase <<
".identcache1";
695 std::fstream cache(ss.str().c_str(),std::ios::in|std::ios::binary);
696 if( !cache.good() )
return false;
707 double Ainf_tmp[6*6];
709 cache.read(
reinterpret_cast<char*
>(&NoSpeed),
sizeof(
unsigned int) );
711 for(
unsigned int SpeedIdx=0; SpeedIdx < NoSpeed; SpeedIdx++)
715 cache.read(
reinterpret_cast<char*
>(&MySpeed),
sizeof(
double) );
717 TimeDomainRadiation radiationModel;
718 for(
int i=0; i < 6; i++)
720 for(
int j=0; j < 6; j++)
722 cache.read(
reinterpret_cast<char*
>(a_size),
sizeof(a_size) );
723 A_tmp =
new double[a_size[0]*a_size[1]];
725 cache.read(
reinterpret_cast<char*
>(A_tmp),
sizeof(
double)*a_size[0]*a_size[1] );
727 cache.read(
reinterpret_cast<char*
>(b_size),
sizeof(b_size) );
728 B_tmp =
new double[b_size[0]*b_size[1]];
730 cache.read(
reinterpret_cast<char*
>(B_tmp),
sizeof(
double)*b_size[0]*b_size[1] );
732 cache.read(
reinterpret_cast<char*
>(c_size),
sizeof(c_size) );
733 C_tmp =
new double[c_size[0]*c_size[1]];
735 cache.read(
reinterpret_cast<char*
>(C_tmp),
sizeof(
double)*c_size[0]*c_size[1] );
737 cache.read(
reinterpret_cast<char*
>(d_size),
sizeof(d_size) );
738 D_tmp =
new double[d_size[0]*d_size[1]];
740 cache.read(
reinterpret_cast<char*
>(D_tmp),
sizeof(
double)*d_size[0]*d_size[1] );
743 radiationModel.A[i][j] = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>::Zero(a_size[0],a_size[1]);
744 radiationModel.B[i][j] = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>::Zero(b_size[0],b_size[1]);
745 radiationModel.C[i][j] = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>::Zero(c_size[0],c_size[1]);
746 radiationModel.D[i][j] = Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic>::Zero(d_size[0],d_size[1]);
748 for(
int k=0; k < a_size[0]; k++)
749 for(
int l=0; l < a_size[1]; l++)
751 assert(a_size[0]*a_size[1] > (l+k*a_size[1]) );
752 radiationModel.A[i][j](k,l) = A_tmp[k+l*a_size[0]];
754 for(
int k=0; k < b_size[0]; k++)
755 for(
int l=0; l < b_size[1]; l++)
757 assert(b_size[0]*b_size[1] > (l+k*b_size[1]) );
758 radiationModel.B[i][j](k,l) = B_tmp[k+l*b_size[0]];
761 for(
int k=0; k < c_size[0]; k++)
762 for(
int l=0; l < c_size[1]; l++)
764 assert(c_size[0]*c_size[1] > (l+k*c_size[1]) );
765 radiationModel.C[i][j](k,l) = C_tmp[k+l*c_size[0]];
767 for(
int k=0; k < d_size[0]; k++)
768 for(
int l=0; l < d_size[1]; l++)
770 assert(d_size[0]*d_size[1] > (l+k*d_size[1]) );
771 radiationModel.D[i][j](k,l) = D_tmp[k+l*d_size[0]];
780 cache.read(
reinterpret_cast<char*
>(Ainf_tmp),
sizeof(double)*6*6);
782 for(
int i=0; i < 6; i++)
783 for(
int j=0; j < 6; j++)
784 radiationModel.Ainf(i,j)=Ainf_tmp[j+i*6];
786 radiationModel.Speed = MySpeed;
787 RadiationApproximation.insert( std::make_pair(SpeedIdx, radiationModel ));
794 void RadiationDerivative(
int DOF1,
int DOF2,
double *derivative,
const double *
const radiation_state,
const double *ship_state,
unsigned int SpeedNo=0 )
796 const TimeDomainRadiation& radiationModel = RadiationApproximation[SpeedNo];
798 const size_t state_size = radiationModel.A[DOF1][DOF2].cols();
799 Eigen::Map<const Eigen::MatrixXd,Eigen::Unaligned> X(radiation_state,state_size,1);
800 Eigen::Map<Eigen::MatrixXd,Eigen::Unaligned> dotX(derivative,state_size,1);
801 dotX.noalias() = radiationModel.A[DOF1][DOF2]*X + radiationModel.B[DOF1][DOF2]*ship_state[DOF2];
804 double RadiationOutput(
int DOF1,
int DOF2,
const double *
const radiation_state,
const double *
const ship_state,
unsigned int SpeedNo=0)
806 const TimeDomainRadiation& radiationModel = RadiationApproximation[SpeedNo];
808 const size_t state_size = radiationModel.A[DOF1][DOF2].cols();
809 Eigen::Map<const Eigen::MatrixXd,Eigen::Unaligned> X(radiation_state,state_size,1);
810 return (radiationModel.C[DOF1][DOF2]*X + radiationModel.D[DOF1][DOF2]*ship_state[DOF2]).value();
814 std::map<int,TimeDomainRadiation,std::less<int>,Eigen::aligned_allocator<std::pair<const int,TimeDomainRadiation> > > RadiationApproximation;
818 virtual bool is2Ddata(){
824 double Lpp,Bredth,Draught;
830 unsigned int NoSpeed;
831 unsigned int NoHeading;
833 unsigned int NoFrequency;
839 FroudeKrylovForce *FroudeFrylov;
840 WaveDriftForce *WaveDrift;
842 Eigen::Matrix<double,6,6> Mrbeig;
843 Eigen::Matrix<double,6,6> **A;
844 Eigen::Matrix<double,6,6> **B;
845 Eigen::Matrix<double,6,6> **C;
846 Eigen::Matrix<double,6,6> **Aadd;
847 Eigen::Matrix<double,6,6> **Badd;
848 Eigen::Matrix<double,6,6> **Cadd;
850 Eigen::Matrix<double,3,1> rg;
851 Eigen::Matrix<double,3,1> ro;
852 std::map<std::string,double> param;
Definition: PotentialData.h:28
Simple waypoint object.
Definition: CableAttach.h:16
Definition: PotentialData.h:33