""" simulation_ballon_freescience.py
"""
#! /usr/bin/python3
# -*- coding: utf-8 -*-
import os
import sys
from math import pi, cos, sin, asin
import matplotlib.pyplot as pyplot
# pour : figure, plot, title, xlabel, ylabel, show

doc = """
################################################################################
# ballon 1d (m,k) : une masse (m) sur un ressort (k) rebondit sur le sol
#   on ne tient pas compte de la longueur du ressort,
#   c'est comme s'il était dans le sol :
#   le ballon s'enfonce dans le sol avant de rebondir
# paramètre numérique :
#   dt : pas de temps constant à régler pour obtenir un calcul précis
################################################################################
"""
g = -9.81
def ballon_1d( masse, k, z0, v0, tmax, dt ) :
    """ chute d'un ballon élastique depuis la hauteur h : z(t)
    (z0, v0) : position et vitesse verticales initales
    dt : pas de temps à régler suffisamment petit pour obtenir un calcul précis """
    tab_t, tab_z, tab_v, tab_a = [], [], [], []
    t = 0
    z = z0
    v = v0
    debut_rebond = None
    # nombre d'itérations
    n = int(tmax/dt)
    for i in range(n) :
        t += dt
        # force
        f = masse * g
        if z < 0 :
            # compression du ressort
            if debut_rebond == None :
                print(f"  début rebond : iteration={i:3d} z={z:6.3f} t={t:6.3f}")
                debut_rebond = i
            f -= k * z
        else : # z >= 0
            if debut_rebond != None :
                print(f"  fin   rebond : iteration={i:3d} z={z:6.3f} t={t:6.3f}")
                duree_rebond = i - debut_rebond
                if duree_rebond < 10 :
                    print(f" duree_rebond = {duree_rebond} itérations < 10 : pas assez d'itérations")
                debut_rebond = None
        # accélération
        a = f / masse
        # vitesse
        if i == 0 :
            v += a * dt/2
        else :
            v += a * dt
        # position
        z += v * dt
        # print(t, z, v, a)
        tab_t.append( t )
        # tab_a.append( a )
        # tab_v.append( v )
        tab_z.append( z )

    pyplot.plot(tab_t, tab_z)
    
    # pour visualiser l'écoulement du temps : 1 point toutes les 10 itérations
    tab_t1 = [ tab_t[i] for i in range(0,n,10) ]
    tab_z1 = [ tab_z[i] for i in range(0,n,10) ]
    pyplot.plot(tab_t1, tab_z1, 'ro') # ronds rouges
    return

def ballon_1d_test() :
    fig = pyplot.figure(1, figsize=(6, 6))
    pyplot.title(" 3 lancers de ballon : horizontal, vers le haut, vers le bas")
    tmax = 10
    ballon_1d( 0.5, 10, 100,   0, tmax, 0.05 )
    ballon_1d( 0.5, 10, 100, -50, tmax, 0.05 )
    ballon_1d( 0.5, 10, 100, +20, tmax, 0.05 )
    pyplot.xlabel("t ou x")
    pyplot.ylabel("z")
    pyplot.plot( [0,tmax], [0,0], color="black")
    pyplot.show()
    return None    

if __name__ == "__main__" :
    print( doc )
    ballon_1d_test()
"""
"""