-
Notifications
You must be signed in to change notification settings - Fork 1
/
makjonswap.cpp
1419 lines (1115 loc) · 37.1 KB
/
makjonswap.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
//////////////////////////////////////////////////////////////////////////////////
//XBeach_GPU //
//Copyright (C) 2013 Bosserelle //
// //
//This program is free software: you can redistribute it and/or modify //
//it under the terms of the GNU General Public License as published by //
//the Free Software Foundation. //
// //
//This program is distributed in the hope that it will be useful, //
//but WITHOUT ANY WARRANTY; without even the implied warranty of //
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
//GNU General Public License for more details. //
// //
//You should have received a copy of the GNU General Public License //
//along with this program. If not, see <http://www.gnu.org/licenses/>. //
//////////////////////////////////////////////////////////////////////////////////
#include "XBeachGPU.h"
#define pi 3.14159265
using DECNUM = float;
void GenCstWave(XBGPUParam Param, std::vector<Wavebndparam> wavebnd, float * theta, double * &Stfile, double * &qfile, double * &Tpfile)
{
int ny, ntheta,nwavbnd;
double dtheta;
ny = Param.ny;
ntheta = Param.ntheta;
dtheta = Param.dtheta;
nwavbnd = wavebnd.size();
for (int n = 0; n < nwavbnd; n++)
{
Tpfile[n] = wavebnd[n].Tp;
double eetot = wavebnd[n].Hs*wavebnd[n].Hs*Param.rho*Param.g / (16.0*Param.dtheta);
for (int j = 0; j < Param.ny; j++)
{
qfile[j + 0 * ny + n*ny * 4] = 0.0;
qfile[j + 1 * ny + n*ny * 4] = 0.0;
qfile[j + 2 * ny + n*ny * 4] = 0.0;
qfile[j + 3 * ny + n*ny * 4] = 0.0;
}
double sumcos = 0.0;
double * scaledir;
scaledir = (double *)malloc(ntheta*sizeof(double));
for (int t = 0; t < ntheta; t++)
{
scaledir[t] = pow(cos((theta[t] - wavebnd[n].Dp) / 2.0), 2.0*wavebnd[n].s);
sumcos = sumcos + scaledir[t];
}
for (int t = 0; t < ntheta; t++)
{
double Stdir = scaledir[t] / sumcos*eetot;
for (int j = 0; j < ny; j++)
{
Stfile[j + t*ny + n*ny*ntheta] = Stdir;
}
}
free(scaledir);
}
}
void makjonswap(XBGPUParam Param, std::vector<Wavebndparam> wavebnd, int step, int &nfreq, int &ntheta, double * &HRfreq, double * &HRdir, double * &HRSpec)
{
double * x, *y;
double Hs = wavebnd[step].Hs;
double Tp = max(wavebnd[step].Tp,1.5); // for very small Tp the wave group generator will request a Giant amount of memory so we need to cap it here
double Dp = wavebnd[step].Dp; // converted to the main angle already
double mainang = Dp;
double fp = 1 / Tp;
double gam = wavebnd[step].gamma;
double scoeff = max(round(min(wavebnd[step].s,1000.0)),10.0);
printf("Generating JONSWAP spectrum: Hs=%f, Tp=%f, Dp=%f, gam=%f, s=%f\n",Hs,Tp,Dp,gam,scoeff);
write_text_to_log_file("Generating JONSWAP spectrum: Hs=" + std::to_string(Hs) + " Tp=" + std::to_string(Tp) + " Dp=" + std::to_string(Dp) + " gam=" + std::to_string(gam) + " s=" + std::to_string(scoeff) + ";");
////
double fnyq = 3.0f*fp;
double dfj= fp/20.0f;
//
nfreq=ceil((fnyq-dfj)/dfj);
HRfreq=(double *)malloc(nfreq*sizeof(double));
//f= new DECNUM[nfreq];
//x= new DECNUM[nfreq];
//y= new DECNUM[nfreq];
x=(double *)malloc(nfreq*sizeof(double));
y=(double *)malloc(nfreq*sizeof(double));
//printf("Hello world!");
double xa,ymax,ysum;
double sigma,fac1,fac2,fac3;
ysum=0.0f;
ymax=0.0f;
for (int i=0; i<nfreq ;i++)
{
HRfreq[i] = (i + 1)*dfj;
// x: nondimensional frequency, divided by the peak frequency
x[i] = HRfreq[i] / fp;
xa=abs(x[i]);
if (xa==0)
{
xa=1e-7f;
}
if (xa<1)
{
sigma=0.07f;
}
else
{
sigma=0.09f;
}
fac1=pow(xa,-5);
fac2=exp(-1.25*(pow(xa,-4.0f)));
fac3=pow(gam,(exp(-(pow(xa-1.0f,2)/(2*pow(sigma,2))))));
y[i]=fac1*fac2*fac3;
//printf("fac1=%f\nfac2=%f\nfac3=%f\n",fac1,fac2,fac3);
ysum=ysum+y[i];
if (y[i]>=ymax)
{
ymax=y[i];
}
}
ysum=ysum/ymax;
for (int i=0; i<nfreq;i++)
{
y[i]=y[i]/ymax;
y[i]=y[i]*pow((Hs/(4*sqrt(ysum*dfj))),2);
//printf("y[%d]=%f\n",i,y[i]);
}
////
//DECNUM t1=-(pi)/2;
//int ntheta=101;
//DECNUM *theta;
double * HRtheta;
ntheta = 90;
double dtheta = 2*pi / ntheta;
double *theta;
double * Dd;
theta = (double *)malloc(ntheta*sizeof(double));
Dd = (double *)malloc(ntheta*sizeof(double));
HRdir = (double *)malloc(ntheta*sizeof(double));
//Dd= new DECNUM[ntheta];
//theta= (DECNUM *)malloc(ntheta*sizeof(DECNUM));//new DECNUM[ntheta];
double ddsum = 0.0f;
//theta=(0:100)*((pi)/100)+t1;
for(int i=0; i<ntheta; i++)
{
theta[i]=i*dtheta-pi; // cover the full circle
HRdir[i] = theta[i];
Dd[i] = pow(cos((theta[i]-mainang)/2.0f),2.0*scoeff);
ddsum=ddsum+Dd[i];
//printf("theta[%d]=%f\n",i,theta[i]);
}
double dang=theta[1]-theta[0];
//mainang=(1.5d0*p1-alfa)-mainang*atan(1.d0)/45.0d0;
for(int i=0; i<ntheta; i++)
{
Dd[i] = Dd[i] / (ddsum*dang);
//printf("Dd[%d]=%f\n",i,Dd[i]);
}
//DECNUM nang=ntheta;
HRSpec = (double *)malloc(nfreq*ntheta*sizeof(double));
for (int i=0; i<ntheta; i++) //! Fill S_array
{
for (int ii=0; ii<nfreq; ii++)
{
HRSpec[ii + i*nfreq] = y[ii] * Dd[i];// m2/Hz/rad
//printf("S_array[%d,%d]=%f\n",ii+1,i+1,S_array[ii+i*nfreq]);
if (HRSpec[ii + i*nfreq] != HRSpec[ii + i*nfreq])
{
printf("Error in generating JONSWAP Spectrum: #NAN or #IND detected");
write_text_to_log_file("Error in generating JONSWAP Spectrum: #NAN or #IND detected");
exit(EXIT_FAILURE);
}
}
}
free(x);
free(y);
free(theta);
free(Dd);
}
void GenWGnLBW(XBGPUParam Param, int nf, int ndir,double * HRfreq,double * HRdir, double * HRSpec, float &Trep, double * &qfile, double * &Stfile)
{
// Generating Boundary condition: Energy from wave group and Long bound waves
printf("Generating Boundary condition: Energy from wave group and Long bound waves.\n");
write_text_to_log_file("Generating Boundary condition: Energy from wave group and Long bound waves");
int ny = Param.ny;
int K; //Number of wave components
//int nhf;
//int nhd;
double * Sf; // size of nf
double trepfac = 0.01;
double temptrep;
double *fgen, *phigen, *thetagen, *kgen, *wgen, *vargen,*vargenq;
int *Findex , *WDindex; // size of K
//double *CompFn;//size of ny*K // now as a 2D vector of complex
double *Sd,*pdf, *cdf; // size of ndir
double fmax,Sfmax; // Should be in Param
//int nspr = 0; // Should be in Param (need test for nspr ==1)
double * binedgeleft, * binedgeright; // size of ntheta
//double * zeta, *Ampzeta; //water elevation ny*ntheta*tslen
double *eta, *Amp; //water elevation integrated over directions ny*tslen
double *stdzeta; //size of ntheta
double *E_tdir; // tslen
double *qx, *qy, *qtot, *qtempx, *qtempy;
int Kmin = 200;
double dtheta = HRdir[1] - HRdir[0];
double dfreq = HRfreq[1] - HRfreq[0];
double Hm0=0.0;
Sf = (double *)malloc(nf*sizeof(double));
Sd = (double *)malloc(ndir*sizeof(double));
pdf = (double *)malloc(ndir*sizeof(double));
cdf = (double *)malloc(ndir*sizeof(double));
for (int n = 0; n < nf; n++)
{
Sf[n] = 0.0;
for (int d = 0; d < ndir; d++)
{
//
Sf[n] = Sf[n] + HRSpec[n+d*nf];
}
Sf[n] = Sf[n] * dtheta;
Hm0 = Hm0 + Sf[n];
}
Hm0 = 4.0*sqrt(Hm0*dfreq);
//printf("Hm0=%f\n", Hm0);
//Need a sanity check here!
for (int d = 0; d < ndir; d++)
{
Sd[d] = 0.0;
for (int n = 0; n < nf; n++)
{
Sd[d] = Sd[d] + HRSpec[n + d*nf];
}
Sd[d] = Sd[d] * dfreq;
}
//////////////////////////////////////
// Generate wave train component
//////////////////////////////////////
fmax = 0.0;
Sfmax = 0.0;
for (int n = 0; n < nf; n++)
{
fmax = max(fmax,HRfreq[n]);
Sfmax = max(Sfmax, Sf[n]);
}
fmax = 2.0*fmax; //????
int ind1 = 0;
int ind2 = nf - 1;
int first = 0;
for (int n = 0; n < nf; n++)
{
if (Sf[n]>Sfmax*Param.sprdthr && HRfreq[n]<fmax )
{
if (first == 0)
{
ind1 = n;
first = 1;
}
ind2 = n;
}
}
//Calculate number of wave components to be included in determination of the
//wave boundary conditions based on the wave record length and width of the
//wave frequency range
K = ceil(Param.rtlength*(HRfreq[ind2] - HRfreq[ind1]) + 1);
//also include minimum number of components
K = (int)max(K*1.0, Kmin*1.0);// workaround because template for int not compiling for some reason
fgen = (double *)malloc(K*sizeof(double));
phigen = (double *)malloc(K*sizeof(double));
thetagen = (double *)malloc(K*sizeof(double));
kgen = (double *)malloc(K*sizeof(double));
wgen = (double *)malloc(K*sizeof(double));
vargen = (double *)malloc(K*sizeof(double));
vargenq = (double *)malloc(K*sizeof(double));
Findex = (int *)malloc(K*sizeof(int));
WDindex = (int *)malloc(K*sizeof(int));
//CompFn = (double *)malloc(K*Param.ny*sizeof(double));
double dfgen = (HRfreq[ind2] - HRfreq[ind1]) / K;
for (int i = 0; i < K; i++)
{
//
fgen[i] = HRfreq[ind1] + i*dfgen;
}
unsigned seed;
if (Param.random == 1)
{
seed = std::chrono::system_clock::now().time_since_epoch().count();
}
else
{
seed = 0;
}
std::default_random_engine generator (seed);
std::uniform_real_distribution<double> distribution(0.0, 1.0);
//Determine a random phase for each wave train component between 0 and 2pi
for (int i = 0; i < K; i++)
{
//
phigen[i] = distribution(generator)*2.0*pi;
}
//double number = distribution(generator);
//Determine random directions for each wave train component, based on the CDF of
//the directional spectrum.For each wave train we will interpolate the directional
//distribution at that frequency, generate a CDF, and then interpolate the wave
//train direction from a random number draw and the CDF.
for (int i = 0; i < K; i++)
{
//int fprev = ceil((fgen[i] - HRfreq[ind1]) / dfreq) + ind1;
for (int d = 0; d < ndir; d++)
{
//pdf[d] = interptime(HRSpec[d + (fprev + 1)*nf], HRSpec[d + (fprev)*nf], dfreq, fgen[i] - HRfreq[fprev]);
pdf[d] = Interp2(nf, ndir,HRfreq, HRdir,HRSpec, fgen[i],HRdir[d]);
}
//convert to pdf by ensuring total integral == 1, assuming constant directional bin size
double sumpdf = 0;
for (int d = 0; d < ndir; d++)
{
sumpdf = sumpdf + pdf[d];
}
for (int d = 0; d < ndir; d++)
{
pdf[d] = pdf[d] / sumpdf;
}
//convert to cdf by trapezoidal integration
//Note: this only works if the directional
//bins are constant in size.Assumed multiplication by one.
cdf[0] = pdf[0];
for (int d = 1; d < ndir; d++)
{
cdf[d] = cdf[d - 1] + 0.5*(pdf[d - 1] + pdf[d]);
}
double number = distribution(generator);
//int dprev = ndir-1;
//for (int d = 1; d < ndir; d++)
//{
// double diff = number - cdf[d];
// if (diff < 0.0)
// {
// dprev = d - 1;
// break;
// }
//}
//interp1D(double *x, double *y, double xx)
//thetagen[i] = interptime(HRdir[dprev + 1], HRdir[dprev], dtheta, dtheta*((number - cdf[dprev]) / (cdf[dprev + 1] - cdf[dprev])));
//Cannot use interp1DMono here because cdf is not monotonic
thetagen[i] = interp1D(ndir, cdf, HRdir, number);
//printf("thetagen[i]=%f\n", thetagen[i]);
}
//determine wave number for each wave train component
// RL Soulsby(2006) "Simplified calculation of wave orbital velocities"
// HR Wallingford Report TR 155, February 2006
// Eqns. 12a - 14
// csherwood@usgs.gov
// Sept 10, 2006
for (int i = 0; i < K; i++)
{
double L, L0,w,x,y,h,t;
h = Param.offdepth;
L0 = Param.g*(1 / fgen[i])*(1 / fgen[i]) / 2 / pi;
w = 2 * pi / ((1 / fgen[i]));//2pi/T
x = w*w * h / Param.g;
y = sqrt(x) * (x<1) + x* (x >= 1);
t = tanh(y);
y = y - ((y*t - x) / (t + y*(1 - t*t)));
t = tanh(y);
y = y - ((y*t - x) / (t + y*(1 - t*t)));
t = tanh(y);
y = y - ((y*t - x) / (t + y*(1 - t*t)));
kgen[i] = y / h;
wgen[i] = w;
}
//////////////////////////////////////
// Calculate Trep
//////////////////////////////////////
temptrep = 0.0;
double tempf = 0.0;
for (int n = 0; n < nf; n++)
{
if (Sf[n] >= Sfmax*trepfac)
{
temptrep += (Sf[n] / max(HRfreq[n], 0.001));
tempf += Sf[n];
}
}
Trep = (float) (temptrep / tempf);
//////////////////////////////////////
// Generate wave time axis
//////////////////////////////////////
//First assume that internal and bc - writing time step is the same
double dtin = Param.dtbc;
int tslenbc = ceil(Param.rtlength / Param.dtbc) + 1;// (int)(Param.rtlength / Param.dtbc) + 1;
//Check whether the internal frequency is high enough to describe the highest frequency
//wave train returned from frange(which can be used in the boundary conditions)
if (dtin > 0.5 / fgen[K - 1])
{
dtin = 0.5 / fgen[K - 1];
}
//! The length of the internal time axis should be even (for Fourier transform) and
//depends on the internal time step needed and the internal duration(~1 / dfgen) :
//incidently tslen*dtin>=rtlength
int tslen = (int)(ceil(1 / dfgen / dtin) + 1);
if ((ceil(tslen/2.0f)-tslen/2.0f)>0)
{
tslen = tslen + 1;
}
//int tsfft = pow(2,ceil(ceil(log(tslen) / log(2)))); /// for fft
//Now we can make the internal time axis
double rtin = tslen * dtin;
double * tin, *taperf, *taperw;
tin = (double *)malloc(tslen*sizeof(double));
for (int n = 0; n < tslen; n++)
{
tin[n] = n*dtin;
}
//Make a taper function to slowly increase and decrease the boundary condition forcing
//at the start and the end of the boundary condition file(including any time beyond
//the external rtbc
taperf = (double *)malloc(tslen*sizeof(double));
taperw = (double *)malloc(tslen*sizeof(double));
for (int n = 0; n < tslen; n++)
{
taperf[n] = 1;
taperw[n] = 1;
}
double Tbc = 1 / fgen[0]; //Should be Trep or 1/fpeak...
int ntaper = (int)((5.0*Tbc) / dtin);
for (int n = 0; n < (int)min(1.0*ntaper, 1.0*tslen); n++)
{
taperf[n] = tanh(5.0*n / ntaper); //
taperw[n] = tanh(5.0*n / ntaper); //
}
//We do not want to taperw the end anymore.Instead we pass the wave height at the end of rtbc to
//the next wave generation iteration.
//end taper by finding where tin = rtbc, taper before that and set everything to zero after
//that.
for (int n = tslen; n > 0; n--)
{
if (tin[n - 1] > (Param.rtlength - ntaper*dtin))
{
taperf[n - 1] = tanh(5.0*(Param.rtlength-tin[n-1])/dtin/ntaper);
}
if (tin[n - 1] > Param.rtlength)
{
taperf[n - 1] = 0;
}
}
//////////////////////////////////////
//Generate wave train variance
//////////////////////////////////////
//! Determine variance at each spectrum location
double sumvargen=0.0;
for (int i = 0; i < K; i++)
{
//interptime(double next, double prev, double timenext, double time)
//vargen[i] = interp1D(HRfreq, Sf, fgen[i]);
vargen[i] = Interp2( nf, ndir, HRfreq, HRdir, HRSpec, fgen[i], thetagen[i]);
sumvargen = sumvargen + vargen[i];
}
// scale vargen so that 4*sqrt(sum(vargen)*dfgen)==4*sqrt(sum(Sf)*df)
double Hm0post = 4.0 * sqrt(sumvargen*dfgen);
double scalefactor = pow(Hm0 / Hm0post, 2); // squared
for (int i = 0; i < K; i++)
{
vargen[i] = vargen[i] * scalefactor;
}
//Not sure why this is done here (the prev values should alway be the min anyways)
double dummy;
for (int i = 0; i < K; i++)
{
dummy = interp1DMono(nf, HRfreq, Sf, fgen[i]);
vargenq[i] = min(vargen[i] ,dummy);
}
//////////////////////////////////////
//Generate wave train properties at each offshore points
//////////////////////////////////////
//Skip A=sqrt(2*vargen[i]*dfgen) and Hmo=4.0 * sqrt(sumvargen*dfgen)
//////////////////////////////////////
//Generate wave train fourier
//////////////////////////////////////
// ! Determine indices of wave train components in frequency axis and
// !Fourier transform result
int tempi = floor(fgen[0] / dfgen);
for (int i = 0; i < K; i++)
{
Findex[i] = tempi + i;
}
// ! Determine first half of complex Fourier coefficients of wave train
// !components using random phase and amplitudes from sampled spectrum
// !until Nyquist frequency.The amplitudes are represented in a
// !two - sided spectrum, which results in the factor 1 / 2.
// !Unroll Fourier components along offshore boundary, assuming all wave trains
// !start at x(1, 1), y(1, 1).
std::complex<double> par_compi (0.0,1.0);
//std::complex<double> tempcmplx;
CArray tempcmplx(tslen / 2-1);
//= 0.0 + 1.0*I;
TwoDee<std::complex<double>> CompFn(ny, tslen);
std::valarray<double> zeta(ny*Param.ntheta*tslen);
//zeta = (double *)malloc(ny*Param.ntheta*tslen*sizeof(double));
//Ampzeta = (double *)malloc(ny*Param.ntheta*tslen*sizeof(double));
eta = (double *)malloc(ny*tslen*sizeof(double));
Amp = (double *)malloc(ny*tslen*sizeof(double));
stdzeta = (double *)malloc(Param.ntheta*sizeof(double));
E_tdir = (double *)malloc(tslen*sizeof(double));
qx = (double *)malloc(ny*tslen*sizeof(double));
qy = (double *)malloc(ny*tslen*sizeof(double));
qtot = (double *)malloc(ny*tslen*sizeof(double));
qtempx = (double *)malloc(tslen*sizeof(double));
qtempy = (double *)malloc(tslen*sizeof(double));
//initialise the variables
for (int n = 0; n < ny*Param.ntheta*tslen;n++)
{
zeta[n] = 0.0;
//Ampzeta[n] = 0.0;
}
for (int n = 0; n < ny*tslen; n++)
{
eta[n] = 0.0;
Amp[n] = 0.0;
}
for (int n = 0; n < Param.ntheta; n++)
{
stdzeta[n] = 0.0;
}
for (int n = 0; n <tslen; n++)
{
E_tdir[n] = 0.0;
}
for (int i = 0; i < K; i++)
{
for (int j = 0; j < Param.ny; j++)
{
CompFn(j,Findex[i]) = sqrt(2.0 * vargen[i] * dfgen) / 2 * exp(par_compi*phigen[i])* //Bas: wp%Findex used in time dimension because t = j*dt in frequency space
exp(-par_compi*kgen[i] * (sin(thetagen[i])*(j*Param.dx))); //dsin
//+ cos(thetagen[i])*(xb[j] - x0))); //dcos
//!Determine Fourier coefficients beyond Nyquist frequency(equal to
//!coefficients at negative frequency axis) of relevant wave components for
//!first y - coordinate by mirroring
for (int n = 1; n < (tslen / 2); n++)
{
tempcmplx[n-1] = std::conj(CompFn(j, n));
}
flipiv(tempcmplx);
for (int n = (tslen/2+1); n < tslen; n++)
{
CompFn(j, n) = tempcmplx[n - (tslen / 2 + 1)]; //Not sure this is right
}
}
}
//create2dnc(nfHR, ndHR, HRfreq[1] - HRfreq[0], HRdir[1] - HRdir[0], 0.0, HRfreq, HRdir, HRSpec);
//////////////////////////////////////
//Distribute wave train direction
//////////////////////////////////////
//!Calculate the bin edges of all the computational wave bins in the
//!XBeach model(not the input spectrum)
binedgeleft = (double *)malloc(Param.ntheta*sizeof(double));
binedgeright = (double *)malloc(Param.ntheta*sizeof(double));
for (int i = 0; i < Param.ntheta; i++)
{
binedgeleft[i] = fmod((i*Param.dtheta + Param.thetamin),2*pi);
binedgeright[i] = fmod(((i+1)*Param.dtheta + Param.thetamin),2*pi);
}
//All generated wave components are in the rang 0 <= theta<2pi.
// !We link wave components to a wave direction bin if the direction falls
// !within the bin boundaries.Note the >= and <= , ranther than >= and <.This
// !is not necessarily a problem, but solves having to make an exception for the
// !highest wave direction bin, in which >= and <= should be applicable.
// !In the case of a central bin and a wave direction exactly(!) on the bin
// !interface, the wave will be linked to the first wave bin in the ordering,
// !rather than the higher of the two bins.
// !
// !Initially set WDindex to zero.This marks a wave direction outside the
// !computational wave bins.In case it does not fit in a directional wave
// !bin, it remains zero at the end of the loops.
// !Note: this does not ensure all energy is included in the wave bins,
// !as wave energy may still fall outside the computational domain.
for (int i = 0; i < K; i++)
{
WDindex[i] = -1;
for (int itheta = 0; itheta < Param.ntheta; itheta++)
{
// special case if this bin spans 0 degrees
if (binedgeleft[itheta]>binedgeright[itheta])
{
if ((thetagen[i] >= binedgeleft[itheta] && thetagen[i] <= (2 * pi)) || (thetagen[i] >= 0.0 && thetagen[i] <= binedgeright[itheta]))
{
WDindex[i] = itheta;
// We now have the correct wave bin, move to next wave component K
break;
}
}
else
{
if (thetagen[i] >= binedgeleft[itheta] && thetagen[i] <= binedgeright[itheta])
{
WDindex[i] = itheta;
// We now have the correct wave bin, move to next wave component K
break;
}
}
}
}
// If the user has set nspr == 1 then the randomly drawn wave directions
// should be set to the centres of the wave directional bins.
// Also move all wave energy falling outside the computational bins, into
// the computational domain(in the outer wave direction bins)
if (Param.nspr == 1)
{
for (int i = 0; i < K; i++)
{
if (WDindex[i]>0)
{
thetagen[i] = binedgeleft[WDindex[i]] + Param.dtheta*0.5;
}
}
}
// Check the amount of energy lost to wave trains falling outside the computational domain
double lostvar = 0.0;
double keptvar = 0.0;
for (int i = 0; i < K; i++)
{
if (WDindex[i] < 0)
{
lostvar = lostvar + sqrt(2 * vargen[i] * dfgen);
}
else
{
keptvar = keptvar + sqrt(2 * vargen[i] * dfgen);
}
}
double perclost = 100 * (lostvar / (lostvar + keptvar));
if (perclost > 10.0)
{
write_text_to_log_file("Large amounts of energy (" + std::to_string(perclost) + "%) fall outside computational domain at the offshore boundary");
}
else
{
write_text_to_log_file("Wave energy outside computational domain at offshore boundary: " + std::to_string(perclost) + "%");
}
//////////////////////////////////////
// Generate e (STfile)
//-------- - Calculate energy envelope time series from-------- -
//--------Fourier components, and write to output file--------
//////////////////////////////////////
// Calculate wave energy for each y - coordinate along seaside boundary for
/// current computational directional bin
std::vector<int> wcompindx;
CArray tempcplxarr(tslen*0.5 - 1);
//std::valarray<double> zeta(tslen);
CArray Gn(tslen);
fftw_complex *out, *in;
out = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * tslen);
in = (fftw_complex*)fftw_malloc(sizeof(fftw_complex) * tslen);
fftw_plan p;
p = fftw_plan_dft_1d(tslen, in, out, FFTW_BACKWARD, FFTW_ESTIMATE);
for (int itheta = 0; itheta < Param.ntheta; itheta++)
{
//Check whether any wave components are in the current computational
//directional bin
for (int i = 0; i < K; i++)
{
if (WDindex[i] == itheta)
{
wcompindx.push_back(i);
}
}
if (wcompindx.empty())//(waveinbin == 0)
{
// no wave component in bin so nothing to do!
continue;
}
// else There are some wave component in the bin
for (int j = 0; j < Param.ny; j++)
{
Gn = 0;// Reset the whole array
//GnForFFT = 0;
// Determine Fourier coefficients of all wave components for current
// y - coordinate in the current computational directional bin
for (int n = 0; n < wcompindx.size(); n++)
{
//printf("wcompindx[%d]=%d; Findex[%d]=%d\n", n, wcompindx[n], n, Findex[n]);
Gn[Findex[wcompindx[n]]] = (CompFn(j, Findex[wcompindx[n]]));
}
tempcplxarr = Gn[std::slice(1, tslen*0.5 - 1, 1)];
for (int jcplx = 0; jcplx < tempcplxarr.size(); jcplx++)
{
tempcplxarr[jcplx] = std::conj(tempcplxarr[jcplx]);
}
flipiv(tempcplxarr);
Gn[std::slice(tslen / 2 + 1, tempcplxarr.size(), 1)] = tempcplxarr;
for (int n = 0; n < tslen; n++)
{
in[n][0] = std::real(Gn[n]);
in[n][1] = std::imag(Gn[n]);
}
// Inverse Discrete Fourier transformation to transform back to time
// domain from frequency domain
fftw_execute(p);
//store the results in zeta
for (int n = 0; n < tslen; n++)
{
zeta[j + itheta*ny + n*ny*Param.ntheta] = out[n][0]* taperw[n];
}
}
wcompindx.clear();
}
//Temporarily output results for debugging
/*double * yyfx, *thetafx;
yyfx=(double *)malloc(ny*sizeof(double));
thetafx=(double *)malloc(Param.ntheta*sizeof(double));
for (int j = 0; j < Param.ny; j++)
{
yyfx[j] = j*Param.dx;
}
for (int itheta = 0; itheta < Param.ntheta; itheta++)
{
thetafx[itheta] = itheta*(Param.dtheta) + Param.thetamin + 0.5f*Param.dtheta;
}
*/
// Calculate energy envelope amplitude
//Integrate instantaneous water level excitation of wave
//components over directions
double temp = 0.0;
CArray tmpcplx(tslen);
for (int j = 0; j < Param.ny; j++)
{
tmpcplx = 0.0; //need to reset
for (int n = 0; n < tslen; n++)
{
temp = 0.0;
for (int itheta = 0; itheta < Param.ntheta; itheta++)
{
temp = temp + zeta[j + itheta*ny + n*ny*Param.ntheta];
}
eta[j + n*ny] = temp;
tmpcplx[n] = temp;
}
hilbert(tmpcplx);
for (int n = 0; n < tslen; n++)
{
Amp[j + n*ny] = abs(tmpcplx[n]);
}
double stdeta = 0.0;
for (int n = 0; n < tslen; n++)
{
stdeta = stdeta + eta[j + n*ny] * eta[j + n*ny];
}
stdeta = sqrt(stdeta / (tslen - 1));
if (stdeta > 0.0)
{
for (int itheta = 0; itheta < Param.ntheta; itheta++)
{
temp = 0.0;
for (int n = 0; n < tslen; n++)
{
temp = temp + zeta[j + itheta*ny + n*ny*Param.ntheta] * zeta[j + itheta*ny + n*ny*Param.ntheta];
}
stdzeta[itheta] = sqrt(temp / (tslen - 1));
for (int n = 0; n < tslen; n++)
{
zeta[j + itheta*ny + n*ny*Param.ntheta] = Amp[j + n*ny] * stdzeta[itheta] / stdeta;
}
}
}
//Calculate energy and interpolate to the output time step
// (Maybe dtin==dtbc or maybe not)
for (int itheta = 0; itheta < Param.ntheta; itheta++)
{
for (int n = 0; n < tslen; n++)
{
E_tdir[n] = zeta[j + itheta*ny + n*ny*Param.ntheta] * zeta[j + itheta*ny + n*ny*Param.ntheta] * 0.5*Param.rho*Param.g / (Param.dtheta);
if (E_tdir[n] != E_tdir[n])
{
printf("Error in generating Wave component eebc: #NAN or #IND detected");
write_text_to_log_file("Error in generating Wave component eebc: #NAN or #IND detected");
exit(EXIT_FAILURE);
}
}
//interpolate to boundary timeseries
for (int m = 0; m < tslenbc; m++)
{
Stfile[j + itheta*ny + m*ny*Param.ntheta] = interp1DMono(tslen, tin, E_tdir, m*Param.dtbc);
}
}
}
//Temporarily output results for debugging
/*
double * yyfx, *thetafx, *bctimin;
yyfx = (double *)malloc(ny*sizeof(double));
thetafx = (double *)malloc(Param.ntheta*sizeof(double));
bctimin = (double *)malloc(tslenbc*sizeof(double));
for (int j = 0; j < Param.ny; j++)
{