|
| 1 | +import numpy as np |
| 2 | + |
| 3 | +def viscous(vessel): |
| 4 | + """ |
| 5 | + Placeholder function that computes or returns viscous damping (Bv) |
| 6 | + for the vessel. You must implement or replace with your actual code. |
| 7 | + """ |
| 8 | + # For now, just return an empty structure or placeholder array. |
| 9 | + Bv_placeholder = {} |
| 10 | + return Bv_placeholder |
| 11 | + |
| 12 | +def plot_speedterms(vessel, Anew, Bnew, Bv): |
| 13 | + """ |
| 14 | + Placeholder function that plots the raw data in MSS axes. |
| 15 | + Implement or replace with your custom plotting routine. |
| 16 | + """ |
| 17 | + print("[DEBUG] plot_speedterms(...) called") |
| 18 | + |
| 19 | +def ABCtransform(vessel, striptheory='veres', plot_flag=0): |
| 20 | + """ |
| 21 | + Python version of ABCtransform.m |
| 22 | +
|
| 23 | + Parameters |
| 24 | + ---------- |
| 25 | + vessel : dict |
| 26 | + The MSS vessel structure, which must contain at least: |
| 27 | + - vessel['freqs'] (array of wave frequencies) |
| 28 | + - vessel['velocities'] (array of forward speeds) |
| 29 | + - vessel['A'] and vessel['B'], each shaped [6,6,Nfreqs,Nspeeds] |
| 30 | + - vessel['main']['nabla'], vessel['main']['Lpp'], vessel['main']['rho'] |
| 31 | + striptheory : str, optional |
| 32 | + E.g. 'veres'. If 'veres', we do the special transformations for A11/B11. |
| 33 | + plot_flag : int, optional |
| 34 | + 1 to plot the raw data in MSS axes (calls plot_speedterms). |
| 35 | + 0 (or anything else) => no plotting. |
| 36 | +
|
| 37 | + Returns |
| 38 | + ------- |
| 39 | + Anew : ndarray |
| 40 | + Transformed added-mass array. |
| 41 | + Bnew : ndarray |
| 42 | + Transformed damping array. |
| 43 | + Bv : dict or ndarray |
| 44 | + Viscous damping structure (as returned by `viscous(...)`). |
| 45 | + """ |
| 46 | + |
| 47 | + # Extract relevant data |
| 48 | + freqs = vessel['freqs'] # shape (Nfreqs,) or similar |
| 49 | + speeds = vessel['velocities'] # shape (Nspeeds,) or similar |
| 50 | + A = vessel['A'] # shape (6,6,Nfreqs,Nspeeds) |
| 51 | + B = vessel['B'] # shape (6,6,Nfreqs,Nspeeds) |
| 52 | + |
| 53 | + Nfreqs = len(freqs) |
| 54 | + Nspeeds = len(speeds) |
| 55 | + |
| 56 | + # We will create copies for Anew and Bnew |
| 57 | + Anew = np.copy(A) |
| 58 | + Bnew = np.copy(B) |
| 59 | + |
| 60 | + # ------------------------------------------------------------------ |
| 61 | + # 1) Symmetrize zero-speed A( :, :, :, 0 ) and B( :, :, :, 0 ) |
| 62 | + # to remove numerical noise. |
| 63 | + # In MATLAB: for i=1..Nfreqs => A_0(1:6,1:6,i) = 0.5*(A_0 + A_0') |
| 64 | + # ------------------------------------------------------------------ |
| 65 | + speed0_index = 0 # zero-based => 'speed=1' in MATLAB |
| 66 | + |
| 67 | + for i in range(Nfreqs): |
| 68 | + # Symmetrize A( :,:, i, speed0_index ) |
| 69 | + A_slice = Anew[:, :, i, speed0_index] |
| 70 | + A_sym = 0.5 * (A_slice + A_slice.T) |
| 71 | + Anew[:, :, i, speed0_index] = A_sym |
| 72 | + |
| 73 | + # Symmetrize B( :,:, i, speed0_index ) |
| 74 | + B_slice = Bnew[:, :, i, speed0_index] |
| 75 | + B_sym = 0.5 * (B_slice + B_slice.T) |
| 76 | + Bnew[:, :, i, speed0_index] = B_sym |
| 77 | + |
| 78 | + # ------------------------------------------------------------------ |
| 79 | + # 2) If striptheory == 'veres', scale A11, B11 from A22, B22 |
| 80 | + # A11_0 = 2.7*(rho / L^2)*nabla^(5/3) |
| 81 | + # alpha = A11_0 / A22_0 |
| 82 | + # Then A11(w) = alpha * A22(w), B11(w) = alpha * B22(w) |
| 83 | + # ------------------------------------------------------------------ |
| 84 | + if striptheory.lower() == 'veres': |
| 85 | + nabla = vessel['main']['nabla'] |
| 86 | + Lpp = vessel['main']['Lpp'] |
| 87 | + rho = vessel['main']['rho'] |
| 88 | + |
| 89 | + # A11_0 => from the formula |
| 90 | + A11_0 = 2.7 * (rho / (Lpp**2)) * (nabla ** (5/3)) |
| 91 | + |
| 92 | + # A22_0 => from A(2,2,1,1) in MATLAB => zero-based => A[1,1,0,0] in Python |
| 93 | + A22_0 = Anew[1, 1, 0, 0] # freq=0, speed=0 |
| 94 | + |
| 95 | + alpha = A11_0 / A22_0 |
| 96 | + |
| 97 | + # Scale A(1,1,i,speed), B(1,1,i,speed) |
| 98 | + for s in range(Nspeeds): |
| 99 | + for i in range(Nfreqs): |
| 100 | + # A(1,1,i,s), B(1,1,i,s) |
| 101 | + Anew[0, 0, i, s] = alpha * Anew[1, 1, i, s] |
| 102 | + Bnew[0, 0, i, s] = alpha * Bnew[1, 1, i, s] |
| 103 | + |
| 104 | + # ------------------------------------------------------------------ |
| 105 | + # 3) Viscous damping |
| 106 | + # We create vessel_new with updated A & B and call `viscous(...)`. |
| 107 | + # ------------------------------------------------------------------ |
| 108 | + vessel_new = dict(vessel) # shallow copy |
| 109 | + vessel_new['A'] = Anew |
| 110 | + vessel_new['B'] = Bnew |
| 111 | + |
| 112 | + Bv = viscous(vessel_new) |
| 113 | + |
| 114 | + # ------------------------------------------------------------------ |
| 115 | + # 4) Optional plotting |
| 116 | + # ------------------------------------------------------------------ |
| 117 | + if plot_flag == 1: |
| 118 | + plot_speedterms(vessel, Anew, Bnew, Bv) |
| 119 | + |
| 120 | + return Anew, Bnew, Bv |
0 commit comments