Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ARCHITECTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ src/
CableDyn_Hydro.f90 Morison loads, Airy and JONSWAP waves, currents
CableDyn_WaveSpectra.f90 spectra, spreading, and multi-train seas
CableDyn_StreamWave.f90 stream-function regular nonlinear waves
CableDyn_WaveMath.f90 block sine/cosine and exponential of the wave kinematics
CableDyn_WaveMath.f90 block sine/cosine, exponential and lane sums of the wave kinematics
CableDyn_Vessel.f90 vessel kinematics, 6-DOF motion, displacement RAOs
CableDyn_SeabedContact.f90 C1 normal contact and stick-slip friction laws
CableDyn_Bathymetry.f90 gridded bathymetry
Expand Down
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,12 @@ target_link_libraries(test_wave_math PRIVATE cabledyn_core)
add_test(NAME wave_math COMMAND test_wave_math)
set_tests_properties(wave_math PROPERTIES LABELS "fortran;unit;hydro")

# The fast sum of the component wave kernel against a direct quadruple-precision sum.
add_executable(test_wave_component_sum tests/test_wave_component_sum.f90)
target_link_libraries(test_wave_component_sum PRIVATE cabledyn_core)
add_test(NAME wave_component_sum COMMAND test_wave_component_sum)
set_tests_properties(wave_component_sum PROPERTIES LABELS "fortran;unit;hydro" TIMEOUT 120)

add_executable(test_cable_static tests/test_cable_static.f90)
target_link_libraries(test_cable_static PRIVATE cabledyn_core)
add_test(NAME cable_static COMMAND test_cable_static)
Expand Down
5 changes: 4 additions & 1 deletion doc/file_formats.rst
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,10 @@ two columns:
``TMax / (2 dtWave)`` with ``none`` (600 at ``dtWave`` 0.25 s), against the 200 of a
``jonswap`` sea. Every node sums all of them for its surface elevation. Below the surface,
the components too short to reach the node's depth are dropped once they cannot change its
velocity and acceleration beyond round-off (:ref:`theory-waves`).
velocity and acceleration beyond round-off (:ref:`theory-waves`). The sum costs about 4 ns
per node, component and step on one core of a current desktop: 3 ms per step for 300 nodes
and 2,400 components, the 4,200 s record at ``WvHiCOff`` 3.6, against 2.5 ms for the solve
of that line. A longer record costs proportionally more, and a larger ``dtM`` less.

Example
~~~~~~~
Expand Down
7 changes: 5 additions & 2 deletions doc/theory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1112,8 +1112,11 @@ commensurate, the record does not repeat (an equally spaced comb would repeat af
its component table (amplitude, frequency, phase), for example a WaterKin ``WaveKinMod`` 1
elevation history, which is reduced once to its Fourier components on the run time base.

Every point sums every component of the elevation. The velocity, acceleration and dynamic
pressure sums run in order of increasing :math:`k_i`. For :math:`-h \le z' \le 0` both depth
Every point sums every component of the elevation. The sums run in interleaved partial sums
added up in a fixed order, so a result does not depend on the vector width or the thread
count, and each point is a function of its position and time alone: nothing carries from one
step to the next. The velocity, acceleration and dynamic pressure sums run in order of
increasing :math:`k_i`. For :math:`-h \le z' \le 0` both depth
ratios :math:`\cosh k(z' + h)/\sinh kh` and :math:`\sinh k(z' + h)/\sinh kh` fall as
:math:`k` grows, so the cosh ratio of the last summed component times the remaining
:math:`\sum_{j \ge i} a_j\omega_j`, :math:`\sum a_j\omega_j^2` and :math:`\sum a_j\omega_j^2/k_j`
Expand Down
252 changes: 172 additions & 80 deletions src/CableDyn_Hydro.f90

Large diffs are not rendered by default.

129 changes: 83 additions & 46 deletions src/CableDyn_WaveMath.f90
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
! SPDX-License-Identifier: Apache-2.0
! Copyright (c) 2026 Jae Hoon Seo, SMI Lab, Inha University
MODULE CableDyn_WaveMath
!! The sines, cosines and exponentials of the wave kinematics, evaluated in blocks.
!! The sines, cosines and exponentials of the wave kinematics, evaluated in blocks, and the
!! lanes that the sums over the components run in.
!!
!! An irregular sea sums, at every point and time, one phase cosine and sine and one depth
!! exponential per component: thousands of transcendental evaluations per node and step. The
Expand All @@ -17,14 +18,19 @@ MODULE CableDyn_WaveMath
!! theta = n pi/2 + r with n the integer nearest 2 theta/pi and
!! r = ((theta - n P1) - n P2) - n P3, the Cody-Waite reduction with pi/2 split in three
!! parts whose products with |n| < 2^20 are exact (P1 and P2 carry 33 bits); sin r and cos r
!! by their Taylor series through r^17 and r^18 (|r| <= pi/4, truncation below 2e-19); the
!! quadrant n mod 4 selects and signs them;
!! by near-minimax polynomials of degree 13 and 14 (|r| <= pi/4; the fit is within 2.1e-17
!! of sin r relative and 9e-19 of cos r); the quadrant n mod 4 selects and signs them;
!! * exp(x) = 2^n exp(r) with n the integer nearest x/ln 2, r = (x - n L1) - n L2 (ln 2 in two
!! parts, |r| <= ln(2)/2), exp r by its Taylor series through r^13 (truncation below 1e-17)
!! and 2^n from its IEEE bits.
!! parts, |r| <= ln(2)/2), exp r by a near-minimax polynomial of degree 11 (within 1.7e-17
!! relative) and 2^n from its IEEE bits.
!!
!! n never leaves the floating-point registers: the rounding to an integer leaves it in the low
!! bits of y + ROUNDER, whose bits give the quadrant and the exponent. A conversion to INTEGER
!! keeps GNU Fortran from vectorising the loop (AVX2 has none to 64 bits), as does a MERGE on a
!! floating-point comparison, so the signs are set on the bits too.
!!
!! Against quadruple precision (test_wave_math, and sweeps of 10^6 phases per decade) the
!! results are within 2.4 ulp over |theta| <= 2^20 pi/2 (sine, cosine) and 1.1 ulp over
!! results are within 3 ulp over |theta| <= 2^20 pi/2 (sine, cosine) and 2 ulp over
!! -708 <= x <= 709 (exponential). Next to a zero of the sine or cosine, the double nearest
!! n pi/2, the value is as small as theta's distance from the multiple (down to 6.2e-19) and the
!! part of pi/2 below P1 + P2 + P3, 8.5e-32, adds |n| 8.5e-32 (at most 9e-26) to the error:
Expand All @@ -40,7 +46,12 @@ MODULE CableDyn_WaveMath
IMPLICIT NONE
PRIVATE

PUBLIC :: CD_Wave_SinCos, CD_Wave_Exp
PUBLIC :: CD_Wave_SinCos, CD_Wave_Exp, CD_Wave_Sum_Lanes

! interleaved partial sums of a wave kinematics sum: component i in lane
! MOD(i - 1, CD_WAVE_LANES) + 1. An element-by-element loop over the lanes vectorises under
! a value-safe model, where a running sum cannot, and keeps a fixed order of additions.
INTEGER, PARAMETER, PUBLIC :: CD_WAVE_LANES = 64

REAL(wp), PARAMETER :: TWO_OVER_PI = 0.63661977236758134308_wp
! pi/2 = P1 + P2 + P3 (+ 8.5e-32): P1 and P2 carry 33 significant bits (fdlibm's pio2_1, pio2_2)
Expand All @@ -56,14 +67,20 @@ MODULE CableDyn_WaveMath
! (y + ROUNDER) - ROUNDER is y rounded to the nearest integer for |y| < 2^51, in plain IEEE
! arithmetic (ANINT can become a library call that blocks the vector loop)
REAL(wp), PARAMETER :: ROUNDER = 6755399441055744.0_wp ! 1.5 * 2^52
! Taylor coefficients 1/m!
REAL(wp), PARAMETER :: F2 = 0.5_wp, F3 = 1.0_wp/6.0_wp, F4 = 1.0_wp/24.0_wp, F5 = 1.0_wp/120.0_wp, &
F6 = 1.0_wp/720.0_wp, F7 = 1.0_wp/5040.0_wp, F8 = 1.0_wp/40320.0_wp, &
F9 = 1.0_wp/362880.0_wp, F10 = 1.0_wp/3628800.0_wp, F11 = 1.0_wp/39916800.0_wp, &
F12 = 1.0_wp/479001600.0_wp, F13 = 1.0_wp/6227020800.0_wp, &
F14 = 1.0_wp/87178291200.0_wp, F15 = 1.0_wp/1307674368000.0_wp, &
F16 = 1.0_wp/20922789888000.0_wp, F17 = 1.0_wp/355687428096000.0_wp, &
F18 = 1.0_wp/6402373705728000.0_wp
! sin r = r + r^3 S(r^2), cos r = 1 - r^2/2 + r^4 C(r^2) and exp r = 1 + r + r^2 E(r): the
! polynomials S, C and E, interpolated at the Chebyshev nodes of the reduced range
REAL(wp), PARAMETER :: S0 = -0.16666666666666666_wp, S1 = 0.008333333333330901_wp, &
S2 = -0.00019841269836686008_wp, S3 = 2.7557316064986773e-06_wp, &
S4 = -2.505112403138208e-08_wp, S5 = 1.59175674789068e-10_wp
REAL(wp), PARAMETER :: C0 = 0.041666666666666664_wp, C1 = -0.0013888888888887367_wp, &
C2 = 2.480158729872029e-05_wp, C3 = -2.7557317248229367e-07_wp, &
C4 = 2.0876141380294955e-09_wp, C5 = -1.1382280957702634e-11_wp
REAL(wp), PARAMETER :: E0 = 0.5000000000000001_wp, E1 = 0.16666666666666669_wp, &
E2 = 0.04166666666662348_wp, E3 = 0.008333333333330011_wp, &
E4 = 0.0013888888917538296_wp, E5 = 0.00019841269863303223_wp, &
E6 = 2.4801520792572694e-05_wp, E7 = 2.75572680728901e-06_wp, &
E8 = 2.762032742826824e-07_wp, E9 = 2.5100569275813683e-08_wp
INTEGER(INT64), PARAMETER :: SIGN_BIT = ISHFT(1_INT64, 63)

CONTAINS

Expand All @@ -73,34 +90,35 @@ PURE SUBROUTINE CD_Wave_SinCos(n, theta, s, c)
INTEGER, INTENT(IN) :: n
REAL(wp), INTENT(IN) :: theta(n)
REAL(wp), INTENT(OUT) :: s(n), c(n)
INTEGER :: i, q
REAL(wp) :: th, fn, r, r2, ps, pc, ss, cc
LOGICAL :: far
INTEGER :: i, n_far
INTEGER(INT64) :: bits
REAL(wp) :: th, y, fn, r, r2, r4, ps, pc, ss, cc

DO i = 1, n
th = MAX(-THETA_MAX, MIN(THETA_MAX, theta(i)))
fn = (th*TWO_OVER_PI + ROUNDER) - ROUNDER
y = th*TWO_OVER_PI + ROUNDER
fn = y - ROUNDER
r = ((th - fn*P1) - fn*P2) - fn*P3
r2 = r*r
! Horner's scheme, its innermost terms first
ps = F13 + r2*(-F15 + r2*F17)
ps = r + (r*r2)*(-F3 + r2*(F5 + r2*(-F7 + r2*(F9 + r2*(-F11 + r2*ps)))))
pc = F12 + r2*(-F14 + r2*(F16 - r2*F18))
pc = (1.0_wp - F2*r2) + (r2*r2)*(F4 + r2*(-F6 + r2*(F8 + r2*(-F10 + r2*pc))))
ps = MERGE(ps, r, ABS(r) > 0.0_wp) ! sin(-0) = -0 (ifx's SIGN would drop the sign)
r4 = r2*r2
! Estrin's scheme: pairs of terms, for a shorter dependency chain than Horner's
ps = r + (r*r2)*((S0 + r2*S1) + r4*((S2 + r2*S3) + r4*(S4 + r2*S5)))
pc = (1.0_wp - 0.5_wp*r2) + r4*((C0 + r2*C1) + r4*((C2 + r2*C3) + r4*(C4 + r2*C5)))
! sin(-0) = -0: the sum above gives +0, and the sign of sin r is that of r
ps = TRANSFER(IOR(TRANSFER(ps, 0_INT64), IAND(TRANSFER(r, 0_INT64), SIGN_BIT)), 0.0_wp)
! quadrant q = n mod 4: (sin, cos) = (ps, pc), (pc, -ps), (-ps, -pc), (-pc, ps)
q = IAND(INT(fn), 3)
ss = MERGE(pc, ps, BTEST(q, 0))
cc = MERGE(ps, pc, BTEST(q, 0))
s(i) = MERGE(-ss, ss, BTEST(q, 1))
c(i) = MERGE(-cc, cc, BTEST(q + 1, 1))
bits = TRANSFER(y, 0_INT64)
ss = MERGE(pc, ps, BTEST(bits, 0))
cc = MERGE(ps, pc, BTEST(bits, 0))
s(i) = TRANSFER(IEOR(TRANSFER(ss, 0_INT64), IAND(ISHFT(bits, 62), SIGN_BIT)), 0.0_wp)
c(i) = TRANSFER(IEOR(TRANSFER(cc, 0_INT64), IAND(ISHFT(bits + 1_INT64, 62), SIGN_BIT)), 0.0_wp)
END DO
! beyond the reduction, or NaN (every comparison with a NaN is false)
far = .FALSE.
n_far = 0
DO i = 1, n
far = far .OR. .NOT. (ABS(theta(i)) <= THETA_MAX)
n_far = n_far + MERGE(0, 1, ABS(theta(i)) <= THETA_MAX)
END DO
IF (.NOT. far) RETURN
IF (n_far == 0) RETURN
DO i = 1, n
IF (.NOT. (ABS(theta(i)) <= THETA_MAX)) THEN
s(i) = SIN(theta(i))
Expand All @@ -114,31 +132,50 @@ PURE SUBROUTINE CD_Wave_Exp(n, x, e)
INTEGER, INTENT(IN) :: n
REAL(wp), INTENT(IN) :: x(n)
REAL(wp), INTENT(OUT) :: e(n)
INTEGER :: i
INTEGER(INT64) :: m
REAL(wp) :: xc, fn, r, p
LOGICAL :: outside
INTEGER :: i, n_out
REAL(wp) :: xc, y, fn, r, r2, r4, p

DO i = 1, n
xc = MAX(X_LOW, MIN(X_HIGH, x(i)))
fn = (xc*INV_LN2 + ROUNDER) - ROUNDER
y = xc*INV_LN2 + ROUNDER
fn = y - ROUNDER
r = (xc - fn*L1) - fn*L2
p = F9 + r*(F10 + r*(F11 + r*(F12 + r*F13)))
p = 1.0_wp + r*(1.0_wp + r*(F2 + r*(F3 + r*(F4 + r*(F5 + r*(F6 + r*(F7 + r*(F8 + r*p))))))))
! times 2^m from its IEEE bits, m in [-1022, 1023] for the clamped argument (SCALE can
r2 = r*r
r4 = r2*r2
p = ((E0 + r*E1) + r2*(E2 + r*E3)) + r4*(((E4 + r*E5) + r2*(E6 + r*E7)) + r4*(E8 + r*E9))
p = (1.0_wp + r) + r2*p
! times 2^m from its IEEE bits, m in [-1022, 1023] for the clamped argument: the low bits
! of y hold m, and the shift leaves its biased value in the exponent field (SCALE can
! become a library call per element)
m = INT(fn, INT64) + 1023_INT64
e(i) = p*TRANSFER(ISHFT(m, 52), p)
e(i) = p*TRANSFER(ISHFT(TRANSFER(y, 0_INT64) + 1023_INT64, 52), p)
END DO
! outside the normal range, or NaN
outside = .FALSE.
n_out = 0
DO i = 1, n
outside = outside .OR. .NOT. (x(i) >= X_LOW .AND. x(i) <= X_HIGH)
n_out = n_out + MERGE(0, 1, x(i) >= X_LOW .AND. x(i) <= X_HIGH)
END DO
IF (.NOT. outside) RETURN
IF (n_out == 0) RETURN
DO i = 1, n
IF (.NOT. (x(i) >= X_LOW .AND. x(i) <= X_HIGH)) e(i) = EXP(x(i))
END DO
END SUBROUTINE CD_Wave_Exp

PURE FUNCTION CD_Wave_Sum_Lanes(lane, n) RESULT(total)
!! The first n <= CD_WAVE_LANES lanes added up pairwise, in a fixed order.
REAL(wp), INTENT(IN) :: lane(:)
INTEGER, INTENT(IN) :: n
REAL(wp) :: total, w(CD_WAVE_LANES)
INTEGER :: h, j, m
w(1:n) = lane(1:n)
m = n
DO WHILE (m > 1)
h = m/2
DO j = 1, h
w(j) = w(j) + w(m - j + 1)
END DO
m = m - h
END DO
total = w(1)
END FUNCTION CD_Wave_Sum_Lanes

END MODULE CableDyn_WaveMath
Loading
Loading