import math
from scipy import integrate
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.cm import viridis
disp = 2214
L = 100
rho = 1.025
phi = 30.964 / 360 * 2 * math.pi
d0 = 6
# Tirant d'eau ($d(\theta)$)
def d(theta, phi, d0):
r = d0*math.tan(phi) * pow((1/math.tan(phi))**2 - (math.tan(theta))**2, 0.5)
return r
# $x_h$
def xh(theta, phi, d0):
return d(theta, phi, d0)/(math.tan(theta) - 1/math.tan(phi))
# $x_l$
def xl(theta, phi, d0):
r = d(theta, phi, d0)/(math.tan(theta) + 1/math.tan(phi))
return r
# (x_h + x_l) / 2
def x_bar(theta, phi, d0):
return (xh(theta, phi, d0) + xl(theta, phi, d0))/2
# Structure de la coque
def h(x, phi):
h_c = 0
if x < 0:
h_c = -1/math.tan(phi)*x
else:
h_c = 1/math.tan(phi)*x
return h_c
# Structure de la coque (inversée)
def h_inv(z, phi):
x_c = z*math.tan(phi)
return x_c
# Tranche d'aire selon l'axe des z
def slice(x, theta, phi, d0):
return d(theta, phi, d0) - math.tan(theta)*x - h(x, phi)
# Tranche d'aire selon l'axe des x
def slice_v(z, theta, phi, d0):
z_l = h(xl(theta, phi, d0), phi)
z_h = h(xh(theta, phi, d0), phi)
retour = 0
if z < z_l:
retour = 2*h_inv(z, phi)
else:
retour = h_inv(z, phi) - (z - d(theta, phi, d0))/math.tan(theta)
return retour
# Intégrale de x_b
def integrale(theta, phi, d0):
bounds = [xh(theta, phi, d0), xl(theta, phi, d0)]
f = lambda x, t = theta, p = phi, d1 = d0: x*slice(x, t, p, d1)
result = integrate.quad(f, bounds[0], bounds[1])
result = result[0]
return result
# Intégrale de z_b
def integrale_v(theta, phi, d0):
z_h = h(xh(theta, phi, d0), phi)
bounds = [0, z_h]
f = lambda z, t = theta, p = phi, d1 = d0: z*slice_v(z, t, p, d1)
result = integrate.quad(f, bounds[0], bounds[1])
result = result[0]
return result
# Intégrale de volume
def volume(theta, phi, d0):
bounds = [xh(theta, phi, d0), xl(theta, phi, d0)]
print(bounds)
f = lambda x, t = theta, p = phi, d1 = d0: slice(x, t, p, d1)
result = integrate.quad(f, bounds[0], bounds[1])
print(result)
result = result[0]
return result
# Intégrale de volume (calculé en z)
def volume_v(theta, phi, d0):
z_l = h(xl(theta, phi, d0), phi)
z_h = h(xh(theta, phi, d0), phi)
bounds = [0, z_h]
f = lambda z, t = theta, p = phi, d1 = d0: slice_v(z, t, p, d1)
result = integrate.quad(f, bounds[0], bounds[1])
result = result[0]
return result
# Calcul de x_b
def xb(theta, phi, d0, rho, L, disp):
inte = integrale(theta, phi, d0)
x_b = inte * rho * L / disp
return x_b
# Calcul de z_b
def zb(theta, phi, d0, rho, L, disp):
inte = integrale_v(theta, phi, d0)
z_b = inte * rho * L / disp
return z_b
"""
angle = 0/360*math.pi*2
test = volume(angle, phi, d0)
print("Valeur du volume: " + str(test))
test = xb(angle, phi, d0, rho, L, disp)
print("Valeur de xb: " + str(test))
test = volume_v(angle, phi, d0)
print("Valeur du volume vertical: " + str(test))
test = zb(angle, phi, d0, rho, L, disp)
print("Valeur de zb: " + str(test))
"""
#Données pour le graphique
n = 10
step = phi/n
a_store = []
d_store = []
z_store = []
x_store = []
for i in range(0, n):
angle = 0 + i * step
a_store.append(angle)
d_c = d(angle, phi, d0)
d_store.append(d_c)
z_c = zb(angle, phi, d0, rho, L, disp)
z_store.append(z_c)
x_c = xb(angle, phi, d0, rho, L, disp)
x_store.append(x_c)
z_hull_store = []
x_hull_store = []
step = 20/200
for i in range(0, 200):
x_c = -10 + step * i
z_c = h(x_c, phi)
z_hull_store.append(z_c)
x_hull_store.append(x_c)
## Graphique
fig, ax = plt.subplots(figsize=(7, 6))
# Dessin de la coque
ax.plot(x_hull_store, z_hull_store, color='black', linewidth=2, label='Coque $h(x)$')
ax.fill_between(x_hull_store, z_hull_store, 7, color='lightgray', alpha=0.3)
# Couleurs pour les points
colors = viridis(np.linspace(0, 1, len(a_store)))
# Équation 1 : Tirant d'eau (Croix mince)
for i in range(len(a_store)):
ax.scatter(0, d_store[i], marker='+', color=colors[i], s=60, linewidths=0.8, zorder=3)
# Équation 2 & 3 : Centre de flottaison (Points)
for i in range(len(a_store)):
ax.scatter(x_store[i], z_store[i], color=colors[i], s=35, edgecolors='white', linewidths=0.5, zorder=4)
# --- Mise en forme (Ajustée selon les nouvelles contraintes) ---
ax.set_xlim(-6, 6)
ax.set_ylim(0, 7)
ax.set_xlabel('x')
ax.set_ylabel('z')
#ax.set_title('Hydrostatique : Tirant d\'eau et Centre de Flottaison')
ax.grid(True, which='both', linestyle='--', alpha=0.5)
# Colorbar
sm = plt.cm.ScalarMappable(cmap=viridis, norm=plt.Normalize(vmin=0, vmax=44))
sm.set_array([])
cbar = fig.colorbar(sm, ax=ax)
cbar.set_label('Angle $\\theta$ (degrés)')
# Légendes
from matplotlib.lines import Line2D
legend_elements = [
#Line2D([0], [0], color='black', lw=2, label='Coque'),
Line2D([0], [0], marker='+', color='b', markerfacecolor='w', markersize=6, label=r"Tirant d'eau $d(\theta)$", markeredgewidth=2),
Line2D([0], [0], marker='o', color='w', markerfacecolor='blue', markersize=6, label='Centre de flottaison $(x_b, z_b)$'),
]
ax.legend(handles=legend_elements, loc='upper right', fontsize='small')
plt.tight_layout()
plt.show()