Repository navigation
Expand file tree
/
Copy pathwaveFunctionsModule.f90
More file actions
1884 lines (1635 loc) · 59.6 KB
/
Copy pathwaveFunctionsModule.f90
File metadata and controls
1884 lines (1635 loc) · 59.6 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
!! SPDX-FileCopyrightText: 2023 SAP SE
!!
!! SPDX-License-Identifier: Apache-2.0
!!
!! This file is part of FEDEM - https://openfedem.org
!!==============================================================================
!> @file waveFunctionsModule.f90
!> @brief Subroutines for sea wave evaluation.
!!==============================================================================
!> @brief Module with subroutines for sea wave evaluation.
!>
!> @details This module contains subroutines for initialization of real
!> parameters for the explicit wave function (explicitfunctionsmodule::func4).
!> It also has subroutines for evaluating the wave function at a given point
!> in time and space. The FUNC4 interface of the @ref explicitfunctionsmodule
!> only yields the free surface level at a given time and @a x = 0, and
!> infinite water depth, whereas the subroutines herein can be used for any
!> value of @a x and finite depth. Subroutines returning the corresponding
!> velocity and acceleration for any depth @a z ≤ 0 are provided as well.
module WaveFunctionsModule
implicit none
private
integer , parameter :: dp = kind(1.0D0) !< 8-byte real (double)
real(dp), parameter :: pi = 3.141592653589793238_dp !< The value of π
real(dp), parameter :: twoPi = pi+pi !< The value of 2π
!> @brief Initializes parameters of a sea wave function.
interface initFUNC4
module procedure initFUNC4fromFile
module procedure initSpectrumDNV
module procedure initSpectrumOF
end interface
public :: dp, twoPi, initFUNC4, initFUNC7, initFUNC8, initFUNC9
public :: deepWave, deepWaves, finiteDepthWave, finiteDepthWaves
public :: stokes2Wave, stokes5Wave, streamWave, embeddedWave, userDefinedWave
public :: waveNumber, checkDepth
contains
!!============================================================================
!> @brief Initializes wave function parameters from the given file.
!>
!> @param[in] FNAME Name of file to read wave parameters from.
!> @param[in] G Gravity constant
!> @param[in] D Sea depth for finite depth waves
!> @param[out] RFUNC Real function parameters
!> @param[out] IERR Error flag
!> @param[in] RSEED Seed for random value generator
!>
!> @details The file may consist of either two, three or four columns of data.
!> The number of columns may be specified on the first line as `#ncol <ncol>`.
!> If the first line does not contain the keyword `#ncol`, a two-column file
!> is assumed. The phase shift is then generated randomly. The wave number
!> is calculated internally unless specified in the fourth column.
!> @code
!> col #1 : Amplitude [m]
!> col #2 : Frequency [Hz]
!> col #3 : Phase shift [0,1]
!> col #4 : Wave number [1/m]
!> @endcode
!>
!> @author Knut Morten Okstad
!>
!> @date 16 Jan 2007
subroutine initFUNC4fromFile (FNAME,G,D,RFUNC,IERR,RSEED)
use FFaMsgInterface, only : ffamsg_list
character(len=*) , intent(in) :: FNAME
real(dp) , intent(in) :: G, D
real(dp) , intent(out) :: RFUNC(:,:)
integer , intent(out) :: IERR
integer, optional, intent(in) :: RSEED
!! Local variables
integer :: iunit, i, lf, ncol, Nseed, seed(16)
logical :: lopen
real(dp) :: Hs
character(len=8) :: chnum
!! --- Logic section ---
ncol = size(RFUNC,1)
if (ncol < 3) then
ierr = -1
call ffamsg_list ('Error : Invalid dimension on RFUNC')
call ffamsg_list (' initFUNC4, size(RFUNC,1) =',ncol)
end if
RFUNC = 0.0_dp
iunit = 10
lopen = .true.
do while (lopen)
iunit = iunit + 1
inquire(iunit,OPENED=lopen)
end do
lf = len_trim(FNAME) ! Bugfix #481: trim(FNAME) crashes on windows
call ffamsg_list (' -> Reading wave spectrum file: '//FNAME(1:lf))
open(iunit,FILE=FNAME(1:lf),STATUS='OLD',ERR=900,IOSTAT=IERR)
read(iunit,'(a)',ERR=910,IOSTAT=IERR) chnum
if (chnum(1:5) == '#ncol' .or. chnum(1:5) == '#NCOL') then
read(chnum(6:),*,ERR=910) i
if (ncol > i) ncol = i
else
ncol = 2 ! Two-column file (default)
rewind(iunit)
end if
select case (ncol)
case (2); call ffamsg_list (' Two-column input (A,f)')
case (3); call ffamsg_list (' Three-column input (A,f,eps)')
case (4); call ffamsg_list (' Four-column input (A,f,eps,kappa)')
end select
call ffamsg_list (' Number of wave components: ',size(RFUNC,2))
do i = 1, size(RFUNC,2)
read(iunit,*,ERR=910,IOSTAT=IERR) RFUNC(1:ncol,i)
end do
close(iunit)
!! Calculate the significant wage height
Hs = 4.0_dp*sqrt(0.5_dp*sum(RFUNC(1,:)*RFUNC(1,:)))
write(chnum,"(F8.2)") Hs
call ffamsg_list (' Significant wave height, Hs='//adjustl(chnum))
if (ncol == 2) then
if (present(RSEED)) then
seed = RSEED
else
seed = 0
end if
!! Generate pseudo-random phase shift
call random_seed (SIZE=Nseed)
call random_seed (PUT=seed(1:Nseed))
call random_number (RFUNC(3,:))
end if
RFUNC(2:3,:) = RFUNC(2:3,:) * twoPi
IERR = 0
if (size(RFUNC,1) > 3 .and. ncol < 4) then
!! Calculate the wave number for each component
do i = 1, size(RFUNC,2)
RFUNC(4,i) = waveNumber(RFUNC(2,i),G,abs(D))
if (checkDepth(D,RFUNC(4,i))) IERR = IERR + 1
end do
end if
return
900 call ffamsg_list ('Error : Failed to open wave data file '//FNAME(1:lf))
call ffamsg_list (' initFUNC4, IERR =',IERR)
IERR = -abs(IERR)
return
910 call ffamsg_list ('Error : EOF detected while reading '//FNAME(1:lf))
call ffamsg_list (' initFUNC4, IERR =',IERR)
IERR = -abs(IERR)
end subroutine initFUNC4fromFile
!!============================================================================
!> @brief Initializes a wave function based on statistical parameters.
!>
!> @param[in] IOP Wave spectrum model:
!> 1. Pierson-Moskowitz with constant frequency intervals
!> 2. Pierson-Moskowitz with random frequency intervals
!> 3. JONSWAP spectrum with constant frequency intervals
!> 4. JONSWAP spectrum with random frequency intervals
!> @param[in] H3 Significant wave height (mean of the one third highest waves)
!> @param[in] T1 Mean wave period
!> @param[in] Gamma Spectral peakedness
!> @param[in] Omega0 Lowest frequency in wave spectrum
!> @param[in] dOmega Frequency increment between successive wave components
!> @param[in] g Gravity constant
!> @param[in] D Sea depth for finite depth waves
!> @param[out] RFUNC Real function parameters
!> @param[out] IERR Error flag
!> @param[in] RSEED Seed for random value generator
!>
!> @details This subroutine generates the wave function parameters from a
!> wave spectrum depending on the input argument @a IOP (see above).
!>
!> References:
!> -# O. Faltinsen, Sea Loads on Ships and Offshore Structures, pp. 23-26.
!>
!> @author Knut Morten Okstad
!>
!> @date 29 Jan 2007
subroutine initSpectrumOF (IOP,H3,T1,Gamma,Omega0,dOmega,g,D,RFUNC,IERR,RSEED)
use FFaMsgInterface, only : ffamsg_list
integer , intent(in) :: IOP
real(dp), intent(in) :: H3, T1, Gamma, Omega0, dOmega, g, D
real(dp), intent(out) :: RFUNC(:,:)
integer , intent(out) :: IERR
integer , optional, intent(in) :: RSEED
!! Local variables
integer :: i, N, Nseed, seed(16)
real(dp) :: A1, A2, OmegaT4, Y
character(len=128) :: chpar
!! --- Logic section ---
IERR = 0
N = size(RFUNC,2)
write(chpar,600) H3,T1,Gamma,Omega0,dOmega,N
600 format(5X,'H_1/3 =',F6.2,' T_1 =',F6.2,' gamma =',F6.2, &
' omega_min =',F6.2,' domega =',1PE10.3,' N =',I6)
call ffamsg_list (' -> Computing wave spectrum, IOP =',IOP)
call ffamsg_list (trim(chpar))
if (N < 1) IERR = IERR - 1
if (T1 <= 0.0_dp) IERR = IERR - 2
if (dOmega <= 0.0_dp) IERR = IERR - 8
if (size(RFUNC,1) < 3) IERR = IERR - 16
if (ierr < 0) then
call ffamsg_list ('Error : Invalid input parameters')
call ffamsg_list (' initFUNC4, IERR =',IERR)
return
end if
if (present(RSEED)) then
seed = RSEED
write(chpar,"(5X,'Random seed =',I6)") RSEED
call ffamsg_list (trim(chpar))
else
seed = 0
end if
call random_seed (SIZE=Nseed)
call random_seed (PUT=seed(1:Nseed))
if (IOP == 1 .or. IOP == 3) then
!! Constant frequency intervals
do i = 1, N
RFUNC(2,i) = Omega0 + dOmega*real(i-1,dp)
end do
else
!! Random frequency intervals
call random_number (RFUNC(2,:))
do i = 1, N
RFUNC(2,i) = Omega0 + dOmega*(RFUNC(2,i)+real(i,dp)-1.5_dp)
end do
end if
if (iop <= 2) then
!! Pierson-Moskowitz
A1 = 0.11_dp * twoPi ** 4.0_dp
A2 = 0.44_dp * twoPi ** 4.0_dp
else
!! JONSWAP
A1 = 155.0_dp
A2 = 944.0_dp
end if
do i = 1, N
if (RFUNC(2,i) <= 0.0_dp) then
RFUNC(:,i) = 0.0_dp
cycle
end if
OmegaT4 = (RFUNC(2,i)*T1) ** 4.0_dp
RFUNC(1,i) = A1 * (H3*H3 / (RFUNC(2,i)*OmegaT4)) * exp(-A2/OmegaT4)
if (IOP > 2) then
if (RFUNC(2,i) <= 5.24_dp/T1) then
Y = (0.191_dp*RFUNC(2,i)*T1-1.0_dp)/(sqrt(2.0_dp)*0.07_dp)
else
Y = (0.191_dp*RFUNC(2,i)*T1-1.0_dp)/(sqrt(2.0_dp)*0.09_dp)
end if
RFUNC(1,i) = RFUNC(1,i) * Gamma ** exp(-Y*Y)
end if
RFUNC(1,i) = sqrt(2.0_dp*RFUNC(1,i)*dOmega)
if (size(RFUNC,1) > 3) then
RFUNC(4,i) = waveNumber(RFUNC(2,i),g,abs(D))
if (checkDepth(D,RFUNC(4,i))) IERR = IERR + 1
end if
end do
if (IERR > 0) IERR = IERR + 3
call random_number (RFUNC(3,:))
RFUNC(3,:) = RFUNC(3,:) * twoPi
#ifdef FT_DEBUG
write(6,"('# i A_i omega_i epsilon_i k_i')")
do i = 1, N
write(6,"(I4,1P4E13.5)") i, RFUNC(:,i)
end do
call flush(6)
#endif
end subroutine initSpectrumOF
!!============================================================================
!> @brief Initializes a wave function based on statistical parameters.
!>
!> @param[in] Hs Significant wave height (mean of the one third highest waves)
!> @param[in] Tp The wave period corresponding to the peak frequency
!> @param[in] Gamma Spectral peakedness (see below).
!> @param[in] Omega0 Lowest frequency in wave spectrum
!> (if ≤ 0 use constant frequency intervals)
!> @param[in] dOmega Frequency increment between successive wave components
!> @param[in] g Gravity constant
!> @param[in] D Sea depth for finite depth waves
!> @param[out] RFUNC Real function parameters
!> @param[out] IERR Error flag
!> @param[in] RSEED Seed for random value generator
!> @param[in] NWdir Number of wave directions
!> @param[in] SprExp Wave spreading exponent (for @a Ndir > 1)
!>
!> @details This subroutine generates the wave function parameters from a
!> wave spectrum. If @a Gamma ≤ 1, a Pierson-Moskowitz spectrum is used,
!> otherwise a JONSWAP spectrum depending on @a Gamma is used.
!>
!> References:
!> -# DNV Recommended practice 205, October 2010.
!> -# S. K. Chakrabarti, "Hydrodynamics of Offshore Structures", pp. 113-116.
!>
!> @author Paul Anton Letnes
!>
!> @date 3 Jan 2013
!>
!> @author Knut Morten Okstad
!>
!> @date 20 Jul 2015
subroutine initSpectrumDNV (Hs,Tp,Gamma,Omega0,dOmega,g,D,RFUNC,IERR, &
& RSEED,NWdir,SprExp)
use FFaMsgInterface, only : ffamsg_list
real(dp), intent(in) :: Hs, Tp, Gamma, Omega0, dOmega, g, D
real(dp), intent(out) :: RFUNC(:,:)
integer , intent(out) :: IERR
integer , optional, intent(in) :: RSEED, NWdir, SprExp
!! Local variables
integer :: i, j, k, N, Ndir, Nseed, loc(1), seed(16)
real(dp) :: omega_p, m0, theta, dtheta, Df, GamRat
character(len=128) :: chpar
!! --- Logic section ---
IERR = 0
N = size(RFUNC,2)
Ndir = 1
if (present(NWdir)) N = N / NWdir
write(chpar,600) Hs,Tp,Gamma,abs(Omega0),dOmega,N
600 format(5X,'H_s =',F6.2,' T_p =',F6.2,' gamma =',F6.2, &
' omega_min =',F6.2,' domega =',1PE10.3,' N =',I6)
call ffamsg_list (' -> Computing JONSWAP wave spectrum')
call ffamsg_list (trim(chpar))
if (present(NWdir)) then
Ndir = NWdir
write(chpar,601) Ndir,SprExp
601 format(5X,'Ndir =',I3,' SpreadExp =',I2)
call ffamsg_list (trim(chpar))
end if
if (N < 1) IERR = IERR - 1
if (Tp <= 0.0_dp) IERR = IERR - 2
if (dOmega <= 0.0_dp) IERR = IERR - 8
if (size(RFUNC,1) < 3) IERR = IERR - 16
if (Ndir < 1 .or. mod(Ndir,2) == 0) IERR = IERR - 32
if (Ndir > 1 .and. present(SprExp)) then
if (sprExp < 2 .or. mod(SprExp,2) == 1) IERR = IERR - 64
end if
if (ierr < 0) then
call ffamsg_list ('Error : Invalid input parameters')
call ffamsg_list (' initFUNC4, IERR =',IERR)
return
end if
if (present(RSEED)) then
seed = RSEED
write(chpar,"(5X,'Random seed =',I6)") RSEED
call ffamsg_list (trim(chpar))
else
seed = 0
end if
call random_seed (SIZE=Nseed)
call random_seed (PUT=seed(1:Nseed))
if (Ndir > 1) then
!! Workaround: First call seems to always return 0.0 or 1.0
call random_number (dtheta)
end if
!! Calculate the peak angular frequency
omega_p = twoPi / Tp
if (Ndir > 1) then
!! Evaluate the gamma function ratio of the directionality function:
!! Gamma(1+k)/(sqrt(pi)*Gamma(0.5+k)) = (4^k * (k!)^2) / (pi * (2*n)!)
!! = (PI_i=1,k { 4*i/(k+i) })/pi
!! See https://en.wikipedia.org/wiki/Gamma_function
if (present(SprExp)) then
k = SprExp/2
GamRat = 1.0_dp / pi
do i = 1, k
GamRat = GamRat * real(4*i,dp)/real(k+i,dp)
end do
else ! SprExp = 2 (k = 1)
GamRat = 2.0_dp / pi ! = Gamma(2)/(sqrt(pi)*Gamma(1.5))
end if
dtheta = pi / real(Ndir+1,dp)
theta = dtheta - 0.5_dp*pi
else
Df = 1.0_dp
GamRat = 0.0_dp
theta = 0.0_dp
dtheta = 1.0_dp
end if
!! Loop over wave directions
j = 0
do k = 1, Ndir
if (Ndir > 1) then
!! Evaluate the directionality function D(theta)
if (present(SprExp)) then
Df = GamRat * cos(theta)**real(SprExp,dp)
else ! SprExp = 2
Df = GamRat * cos(theta)*cos(theta)
end if
write(chpar,602) theta*180_dp/pi,Df
602 format(5X,'theta =',F8.2,': D(theta) =',1PE12.5)
call ffamsg_list (trim(chpar))
end if
if (Omega0 <= 0.0_dp) then
!! Constant frequency intervals
RFUNC(2,j+1) = -Omega0
do i = j+2, j+N
RFUNC(2,i) = RFUNC(2,i-1) + dOmega
end do
else
!! Random frequency intervals
call random_number (RFUNC(2,j+1:j+N))
do i = j+1, j+N
RFUNC(2,i) = Omega0 + dOmega*(RFUNC(2,i)+real(i-j,dp)-1.5_dp)
end do
end if
!! Calculate the wave spectrum and wave numbers
do i = j+1, j+N
if (RFUNC(2,i) <= 0.0_dp) then
RFUNC(:,i) = 0.0_dp
else
if (Gamma <= 1.0_dp) then
RFUNC(1,i) = PiersonMoskowitz(Hs,RFUNC(2,i),omega_p)*Df
else
RFUNC(1,i) = JONSWAP(Hs,RFUNC(2,i),omega_p,Gamma)*Df
end if
if (size(RFUNC,1) > 3) then
RFUNC(4,i) = waveNumber(RFUNC(2,i),g,abs(D))
if (checkDepth(D,RFUNC(4,i))) IERR = IERR + 1
end if
end if
end do
if (IERR > 0) IERR = IERR + 3
!! Compute amplitudes from spectrum
RFUNC(1,j+1:j+N) = sqrt(RFUNC(1,j+1:j+N) * dOmega*dtheta * 2.0_dp)
!! TODO determine a better tolerance criterion?
!! Perform other checks, e.g., m1, m2, m3? Wave steepness? Mean period?
!! (Note: m4 and higher diverges)
m0 = 0.5_dp * sum(RFUNC(1,j+1:j+N)**2.0_dp)
if (abs(m0-Hs*Hs/16.0_dp) > 0.05_dp*m0 .and. Ndir == 1) then
IERR = IERR + 1
call ffamsg_list ('Warning : Cutoff in spectrum is too coarse '// &
& 'or number of wave components is too small,')
write(chpar,"('Hs =',F6.2,' m0 =',1PE12.5)") Hs,m0
call ffamsg_list (' '//trim(chpar))
end if
loc = maxloc(RFUNC(1,j+1:j+N))
if (abs(RFUNC(2,loc(1)+j) - omega_p) > dOmega) then
IERR = IERR + 2
write(chpar,*) abs(RFUNC(2,loc(1)) - omega_p), dOmega, omega_p
call ffamsg_list ('Warning : Spectrum peak is far off '// &
& 'the specified value,')
write(chpar,"('max{Omega} = Omega(',i6,') =',1PE12.5)") &
& loc(1), RFUNC(2,loc(1))
call ffamsg_list (' '//trim(chpar))
write(chpar,"('dOmega =',1PE12.5,' Omega_p = ',E12.5)") &
& dOmega, omega_p
end if
!! Set the random phase angle, no input required
call random_number (RFUNC(3,j+1:j+N))
RFUNC(3,j+1:j+N) = RFUNC(3,j+1:j+N) * twoPi
#ifdef FT_DEBUG
if (Ndir > 1) then
write(6,"(/'# Wave spectrum for theta =',F8.2)") theta*180_dp/pi
end if
write(6,"('# i A_i omega_i epsilon_i k_i')")
do i = 1, N
write(6,"(I4,1P4E13.5)") i, RFUNC(:,j+i)
end do
call flush(6)
#endif
theta = theta + dtheta
seed = seed + 1
j = j + N
end do
contains
!> @brief Returns a PiersonMoskowitz wave component amplitude.
function PiersonMoskowitz (Hs,omega,omega_p) result(S)
real(dp), intent(in) :: Hs, omega, omega_p
real(dp) :: S
S = 0.3125_dp * Hs*Hs * omega_p**4.0_dp * omega**(-5.0_dp) &
* exp(-1.25_dp * (omega/omega_p)**(-4.0_dp))
end function PiersonMoskowitz
!> @brief Returns a JONSWAP wave component amplitude.
function JONSWAP (Hs,omega,omega_p,Gamma) result(S)
real(dp), intent(in) :: Hs, omega, omega_p, Gamma
real(dp) :: S, sigma, A_gamma, Gamma_power
if (omega <= omega_p) then
sigma = 0.07_dp
else
sigma = 0.09_dp
end if
A_gamma = 1.0_dp - 0.287_dp * log(Gamma)
Gamma_power = exp(-0.5_dp * ((omega-omega_p) / (sigma*omega_p))**2.0_dp)
S = A_gamma * PiersonMoskowitz(Hs,omega,omega_p) * Gamma**Gamma_power
end function JONSWAP
end subroutine initSpectrumDNV
!!============================================================================
!> @brief Calculates the wave number for finite depth waves.
!>
!> @param[in] omega Angular frequency
!> @param[in] g Gravity constant
!> @param[in] d Water depth
!> @return The wave number @a k
!>
!> @details The wave number is calculated by solving a nonlinear equation by
!> Newton-Raphson iterations. If the water depth is zero, infinite depth is
!> assumed and the wave number is calculated explcitly.
!>
!> @callergraph
!>
!> @author Arne Rekdal
!>
!> @date 5 Oct 2010
function waveNumber (omega,g,d) result(k)
real(dp), intent(in) :: omega, g, d
!! Local variables
integer :: i
real(dp) :: k, f0, df0, tkd
!! Newton-Raphson tolerance criterion
integer , parameter :: maxIter_p = 100
real(dp), parameter :: tol_p = 1.0e-6_dp
!! --- Logic section ---
k = omega*omega/g
if (d < tol_p) return
do i = 1, maxIter_p
tkd = tanh(k*d)
f0 = omega*omega - g*k*tkd
df0 = g*(tkd + k*d*(1.0_dp - tkd*tkd))
k = k + f0/df0
if (abs(f0) < tol_p) exit
end do
end function waveNumber
!!============================================================================
!> @brief Checks that the finite water depth does not cause cause overflow.
!>
!> @param[in] d Water depth
!> @param[in] k The wave number
!>
!> @callergraph
!>
!> @author Knut Morten Okstad
!>
!> @date 25 Jan 2013
function checkDepth (d,k)
real(dp), intent(in) :: d, k
!! Local variables
logical :: checkDepth
real(dp), parameter :: shmax = asinh(huge(1.0_dp)) ! limit on sinh argument
!! --- Logic section ---
checkDepth = k*d >= shmax
end function checkDepth
!!============================================================================
!> @brief Evaluates the deep water wave profile.
!>
!> @param[in] RFUNC Real wave data, output from wavefunctionsmodule::initfunc4
!> @param[in] g Gravity constant
!> @param[in] x Current horizontal position
!> @param[in] z Current vertical position
!> @param[in] t Current time
!> @param[in] atSurface If .true., evaluate at water surface instead of @a z
!> @param[in] noWheelerStretching If .true., switch off Wheeler stretching
!> @param[out] h Current wave height (depends on @a x and @a t only)
!> @param[out] u Current particle velocity in local @a x direction
!> @param[out] w Current particle velocity in local @a z direction
!> @param[out] du Current particle acceleration in local @a x direction
!> @param[out] dw Current particle acceleration in local @a z direction
!> @param[out] p Current dynamic pressure
!>
!> @details This subroutine evaluates the wave profile height and associated
!> particle velocity and acceleration at a given spatial point and time.
!> Assuming deep water waves, i.e., 2*depth > largest wave length.
!> @a z=0 represents the mean free surface level and @a z is positive upwards.
!>
!> Reference:
!> -# O. M. Faltinsen, "Sea Loads on Ships and Offshore Structures",
!> Table 2.1, page 16.
!>
!> @callergraph
!>
!> @author Knut Morten Okstad
!>
!> @date 25 March 2009
subroutine deepWave (RFUNC,g,x,z,t,atSurface,noWheelerStretching, &
& h,u,w,du,dw,p)
real(dp), intent(in) :: RFUNC(:,:), g, x, z, t
logical , intent(in) :: atSurface, noWheelerStretching
real(dp), intent(out) :: h, u, w, du, dw
real(dp), optional, intent(out) :: p
!! Local variables
integer :: i, ncmp
real(dp) :: omega, eps, k, B, c, s, zz
!! --- Logic section ---
h = 0.0_dp
u = 0.0_dp
w = 0.0_dp
du = 0.0_dp
dw = 0.0_dp
if (present(p)) p = 0.0_dp
ncmp = size(RFUNC,2)
!$OMP PARALLEL DO PRIVATE(i,omega),REDUCTION(+:h)
do i = 1, ncmp
omega = RFUNC(2,i)
h = h + RFUNC(1,i)*sin(omega*(t-x*omega/g)+RFUNC(3,i))
end do
!$OMP END PARALLEL DO
!! Calculate the effective water depth
if (atSurface) then ! z = h
if (noWheelerStretching) then
zz = min(h,0.0_dp)
else
zz = 0.0_dp
end if
else
if (h < z) then
return ! we are above the water surface
else if (noWheelerStretching) then
zz = min(z,0.0_dp)
else
zz = z-h
end if
end if
!$OMP PARALLEL DO PRIVATE(i,omega,eps,k,B,c,s),REDUCTION(+:u,w,du,dw)
do i = 1, ncmp
omega = RFUNC(2,i)
eps = RFUNC(3,i)
k = omega*omega/g
B = omega*RFUNC(1,i)*exp(k*zz)
c = cos(omega*t+eps-k*x)
s = sin(omega*t+eps-k*x)
u = u + B*s
w = w + B*c
du = du + omega*B*c
dw = dw - omega*B*s
end do
!$OMP END PARALLEL DO
if (present(p)) then
!$OMP PARALLEL DO PRIVATE(i,omega,eps,k,B),REDUCTION(+:p)
do i = 1, ncmp
omega = RFUNC(2,i)
eps = RFUNC(3,i)
k = omega*omega/g
B = omega*RFUNC(1,i)*exp(k*zz)
p = p + B*sin(omega*t+eps-k*x)*g/omega
end do
!$OMP END PARALLEL DO
end if
end subroutine deepWave
!!============================================================================
!> @brief Evaluates the deep water wave profile.
!>
!> @param[in] RFUNC Real wave data, output from wavefunctionsmodule::initfunc4
!> @param[in] g Gravity constant
!> @param[in] x Current horizontal position
!> @param[in] y Current horizontal position normal to the @a x direction
!> @param[in] z Current vertical position
!> @param[in] t Current time
!> @param[in] atSurface If .true., evaluate at water surface instead of @a z
!> @param[in] noWheelerStretching If .true., switch off Wheeler stretching
!> @param[out] h Current wave height (depends on @a x, @a y and @a t only)
!> @param[out] u Current particle velocity in local @a x direction
!> @param[out] v Current particle velocity in local @a y direction
!> @param[out] w Current particle velocity in local @a z direction
!> @param[out] du Current particle acceleration in local @a x direction
!> @param[out] dv Current particle acceleration in local @a y direction
!> @param[out] dw Current particle acceleration in local @a z direction
!> @param[out] p Current dynamic pressure
!>
!> @details This subroutine evaluates the wave profile height and associated
!> particle velocity and acceleration at a given spatial point and time.
!> Assuming deep water waves, i.e., 2*depth > largest wave length.
!> @a z=0 represents the mean free surface level and @a z is positive upwards.
!> This subroutine also accounts for wave spreading through an outer loop.
!>
!> Reference:
!> -# O. M. Faltinsen, "Sea Loads on Ships and Offshore Structures",
!> Table 2.1, page 16.
!>
!> @callergraph
!>
!> @author Knut Morten Okstad
!>
!> @date 15 July 2015
subroutine deepWaves (RFUNC,g,x,y,z,t,atSurface,noWheelerStretching, &
& h,u,v,w,du,dv,dw,p)
real(dp), intent(in) :: RFUNC(:,:,:), g, x, y, z, t
logical , intent(in) :: atSurface, noWheelerStretching
real(dp), intent(out) :: h, u, v, w, du, dv, dw
real(dp), optional, intent(out) :: p
!! Local variables
integer :: i, j, ncmp, ndir
real(dp) :: omega, phase, theta, dtheta, k, B, c, s, zz, r, dr
!! --- Logic section ---
h = 0.0_dp
u = 0.0_dp
v = 0.0_dp
w = 0.0_dp
du = 0.0_dp
dv = 0.0_dp
dw = 0.0_dp
if (present(p)) p = 0.0_dp
ncmp = size(RFUNC,2)
ndir = size(RFUNC,3)
dtheta = pi / real(ndir+1,dp)
theta = dtheta - 0.5_dp*pi
do j = 1, ndir
!$OMP PARALLEL DO PRIVATE(i,omega,k,phase),REDUCTION(+:h)
do i = 1, ncmp
omega = RFUNC(2,i,j)
k = omega*omega/g
phase = RFUNC(3,i,j) - k*(x*cos(theta) + y*sin(theta))
h = h + RFUNC(1,i,j)*sin(omega*t + phase)
end do
!$OMP END PARALLEL DO
theta = theta + dtheta
end do
!! Calculate the effective water depth
if (atSurface) then ! z = h
if (noWheelerStretching) then
zz = min(h,0.0_dp)
else
zz = 0.0_dp
end if
else
if (h < z) then
return ! we are above the water surface
else if (noWheelerStretching) then
zz = min(z,0.0_dp)
else
zz = z-h
end if
end if
theta = dtheta - 0.5_dp*pi
do j = 1, ndir
r = 0.0_dp
dr = 0.0_dp
!$OMP PARALLEL DO PRIVATE(i,omega,phase,k,B,c,s), &
!$OMP REDUCTION(+:r,w,dr,dw)
do i = 1, ncmp
omega = RFUNC(2,i,j)
k = omega*omega/g
phase = RFUNC(3,i,j) - k*(x*cos(theta) + y*sin(theta))
k = omega*omega/g
B = omega*RFUNC(1,i,j)*exp(k*zz)
c = cos(omega*t + phase)
s = sin(omega*t + phase)
r = r + B*s
w = w + B*c
dr = dr + omega*B*c
dw = dw - omega*B*s
end do
!$OMP END PARALLEL DO
u = u + r*cos(theta)
v = v + r*sin(theta)
du = du + dr*cos(theta)
dv = dv + dr*sin(theta)
if (present(p)) then
!$OMP PARALLEL DO PRIVATE(i,omega,k,phase,B),REDUCTION(+:p)
do i = 1, ncmp
omega = RFUNC(2,i,j)
k = omega*omega/g
phase = RFUNC(3,i,j) - k*(x*cos(theta) + y*sin(theta))
B = omega*RFUNC(1,i,j)*exp(k*zz)
p = p + B*sin(omega*t + phase)*g/omega
end do
!$OMP END PARALLEL DO
end if
theta = theta + dtheta
end do
end subroutine deepWaves
!!============================================================================
!> @brief Evaluates the finite depth wave profile.
!>
!> @param[in] RFUNC Real wave data, output from wavefunctionsmodule::initfunc4
!> @param[in] g Gravity constant
!> @param[in] d Water depth
!> @param[in] x Current horizontal position
!> @param[in] z Current vertical position, positive upwards
!> (@a z= 0 means free surface level)
!> @param[in] t Current time
!> @param[in] atSurface If .true., evaluate at water surface instead of @a z
!> @param[in] noWheelerStretching If .true., switch off Wheeler stretching
!> @param[out] h Current wave height (depends on @a x and @a t only)
!> @param[out] u Current particle velocity in local @a x direction
!> @param[out] w Current particle velocity in local @a z direction
!> @param[out] du Current particle acceleration in local @a x direction
!> @param[out] dw Current particle acceleration in local @a z direction
!> @param[out] p Current dynamic pressure
!>
!> @details This subroutine evaluates the wave profile and the associated
!> particle velocity and acceleration at a given spatial point and time.
!> Assuming finite depth water waves and linear wave theory.
!>
!> References:
!> -# O. M. Faltinsen, "Sea Loads on Ships and Offshore Structures",
!> Table 2.1, page 16.
!>
!> @callergraph
!>
!> @author Arne Rekdal
!>
!> @date 5 Oct 2010
subroutine finiteDepthWave (RFUNC,g,d,x,z,t,atSurface,noWheelerStretching, &
& h,u,w,du,dw,p)
real(dp), intent(in) :: RFUNC(:,:), g, d, x, z, t
logical , intent(in) :: atSurface, noWheelerStretching
real(dp), intent(out) :: h, u, w, du, dw
real(dp), optional, intent(out) :: p
!! Local variables
integer :: i, ncmp
logical :: haveK
real(dp) :: zz, omega, k, B, c, s, ch, sh
!! --- Logic section ---
h = 0.0_dp
u = 0.0_dp
w = 0.0_dp
du = 0.0_dp
dw = 0.0_dp
if (present(p)) p = 0.0_dp
ncmp = size(RFUNC,2)
haveK = size(RFUNC,1) > 3
!$OMP PARALLEL DO PRIVATE(i,k),REDUCTION(+:h)
do i = 1, ncmp
if (haveK) then
k = RFUNC(4,i)
else
k = waveNumber(RFUNC(2,i),g,d)
end if
h = h + RFUNC(1,i)*sin(RFUNC(2,i)*t+RFUNC(3,i)-k*x)
end do
!$OMP END PARALLEL DO
!! Calculate the effective water depth
if (atSurface) then ! z = h
if (noWheelerStretching) then
zz = min(d+h,d)
else
zz = d
end if
else
if (z < -d .or. z > h) then
return ! we are below the bottom or above the surface
else if (noWheelerStretching) then
zz = min(d+z,d)
else
zz = d + (z-h)*d/(d+h)
end if
end if
!$OMP PARALLEL DO PRIVATE(i,omega,k,B,ch,sh,s,c),REDUCTION(+:u,w,du,dw)
do i = 1, ncmp
omega = RFUNC(2,i)
if (haveK) then
k = RFUNC(4,i)
else
k = waveNumber(omega,g,d)
end if
B = omega*RFUNC(1,i)/sinh(k*d)
ch = cosh(k*zz)
sh = sinh(k*zz)
s = sin(omega*t+RFUNC(3,i)-k*x)
c = cos(omega*t+RFUNC(3,i)-k*x)
u = u + B*ch*s
w = w + B*sh*c
du = du + omega*B*ch*c
dw = dw - omega*B*sh*s
end do
!$OMP END PARALLEL DO
if (present(p)) then
!$OMP PARALLEL DO PRIVATE(i,omega,k,s),REDUCTION(+:p)
do i = 1, ncmp
omega = RFUNC(2,i)
if (haveK) then
k = RFUNC(4,i)
else
k = waveNumber(omega,g,d)
end if
s = sin(omega*t+RFUNC(3,i)-k*x)
p = p + g*RFUNC(1,i)*cosh(k*zz)*s/cosh(k*d)
end do
!$OMP END PARALLEL DO
end if
end subroutine finiteDepthWave
!!============================================================================
!> @brief Evaluates the finite depth wave profile.
!>
!> @param[in] RFUNC Real wave data, output from wavefunctionsmodule::initfunc4
!> @param[in] g Gravity constant
!> @param[in] d Water depth
!> @param[in] x Current horizontal position
!> @param[in] y Current horizontal position normal to the @a x direction
!> @param[in] z Current vertical position, positive upwards
!> (@a z= 0 means free surface level)
!> @param[in] t Current time
!> @param[in] atSurface If .true., evaluate at water surface instead of @a z
!> @param[in] noWheelerStretching If .true., switch off Wheeler stretching