Update app.py
Browse files122345674891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441457146147148149150151131521531515417155156192021157158159160161162163164165166167168169170171172231731742617517617717817918032181import streamlit as stimport osimport numpy as npimport matplotlib.pyplot as pltimport mathimport ioimport sys
# 1. Affichage de l'image PetroOXY optimisée en haut de page# =====================================================================# 1. FONCTIONS GRAPHIQUES ET OUTILS (VOTRE CODE ENTRÉE)# =====================================================================def idiese(nbd): for i in range(nbd): print("#", end="") print()
def TRACE1D(x, y1, y2, label_x, label_y1, label_y2, title): fig, ax1 = plt.subplots(figsize=(8, 5)) x_log = np.where(x <= 0, 1e-3, x) ax1.set_xscale('log') ax1.set_xlabel(label_x) # Axe principal (Gauche) : Pression ax1.plot(x_log, y1, color='red', label=label_y1) ax1.set_ylabel(label_y1, color='red') ax1.tick_params(axis='y', labelcolor='red') # Axe secondaire (Droite) : Concentration ax2 = ax1.twinx() ax2.plot(x_log, y2, 'o', color='blue', label=label_y2) ax2.set_ylabel(label_y2, color='blue') ax2.tick_params(axis='y', labelcolor='blue') plt.title(title) fig.tight_layout() return fig
def TRACE2D(x, y1, y2, label_x, label_y1, label_y2, title): fig, ax1 = plt.subplots(figsize=(8, 5)) x_log = np.where(x <= 0, 1e-3, x) ax1.set_xscale('log') ax1.set_xlabel(label_x) # AXE Y1 : TEMPÉRATURE (À gauche) ax1.set_ylabel(label_y1, color='blue') ax1.plot(x_log, y1, color='blue') ax1.tick_params(axis='y', labelcolor='blue') ax1.set_ylim(0, 180) # AXE Y2 : PRESSION (À droite) ax2 = ax1.twinx() ax2.set_ylabel(label_y2, color='red') ax2.plot(x_log, y2, color='red') ax2.tick_params(axis='y', labelcolor='red') ax2.set_ylim(7e5, 10.5e5) plt.title(title) fig.tight_layout() return fig
def executer_simulation(): stdout_capture = io.StringIO() sys.stdout = stdout_capture try: print("00 - IMPORT DES BIBLIOTHEQUES TERMINÉ") print("01 - PARAMETRES DE DIMENSIONNEMENT ET VARIABLES UTILES") un = 1.E+00 ; zero = 0.E+00 ; mill = 1.000E+03 rgp = 8.314 ; Tref = 273.15 ; Tamb = 25.E+00 MmolRH = 167E+00 ; MmolO2 = 32.E+00 Venc = 0.18070 ; Vech = 0.00500 ; Pini = 700. Dtsec = 500. ; Pexpo = 1.E+09 ; Ea = 90000 Tset = 160.E+00 ; roRH = 780.E+00 ; Solub = 2.8E-03 niter = 500 ; niterd = 100 print("02 - MASSE MOLAIRE FUEL SATURE ET NBE DE MOLES") MassRH = roRH * Vech MmolRHs = (MmolRH + Solub * MmolO2) / (un + Solub) nmolRH = MassRH / MmolRHs TsetK = Tref + Tset k_app = Pexpo * math.exp(-Ea/rgp/TsetK) / mill Tcou = np.zeros(niter) TcouK = np.zeros(niter) Tps = np.zeros(niter) Pres = np.zeros(niter) Cfuel = np.zeros(niter) print("03 - MONTEE EN TEMPERATURE : EVOLUTION P et T ") VencSI = Venc / mill ; VechSI = Vech / mill Volg = (VencSI - VechSI) nO2g = mill * Pini * Volg / rgp / (Tamb + Tref) Pas_montee = Dtsec / float(niterd - 1) ezt = Tset - Tamb for i in range(niterd): Tps[i] = float(i) * Pas_montee zz = math.exp(-4 * Tps[i] / Dtsec) Tcou[i] = Tamb + ezt * (un - zz) TcouK[i] = Tcou[i] + Tref Pres[i] = nO2g * rgp * TcouK[i] / Volg Cfuel[i] = nmolRH / Vech print("04 - RACCORDEMENT / TRANSITOIRE ET INSTANT DE FIN D'ESSAI") TK = TcouK[niterd-1] Pmax = Pres[niterd-1] Pend = 0.9 * Pmax deltaP = Pmax - Pend Cfuel0 = Cfuel[niterd-1] Xi = Cfuel0 * rgp * TK * (Vech / Volg) ezp = Xi / (Xi - deltaP) trmax = math.log(ezp) / k_app print(f"DUREE DE L'ESSAI CALCULÉE : {trmax/3600:.4f} heures") print("05 - DETERMINATION T, P, C_RH EN PHASE DE THERMO-OXYDATION") ntest = niter - niterd Pas_test = trmax / float(ntest - 1) ii = 1 for i in range(niterd, niter): timeloc = float(ii) * Pas_test Tps[i] = Tps[niterd-1] + timeloc Tcou[i] = Tcou[niterd-1] termExp = math.exp(-k_app * timeloc) ezP = un - termExp Pres[i] = Pmax - Xi * ezP Cfuel[i] = Cfuel[niterd-1] * termExp ii += 1 print("06 - ENREGISTREMENT DES FIGURES") fig1 = TRACE2D(Tps, Tcou, Pres, "Temps (s)", "TEMPERATURE (°C)", "PRESSION (Pa)", "EVOLUTION TEMPORELLE T & P") fig2 = TRACE1D(Tps, Pres, Cfuel, "Temps (s)", "PRESSION (Pa)", "Concentration (mol/l)", "EVOLUTION TEMPORELLE P ET Cfuel") finally: sys.stdout = sys.__stdout__ texte_deroule = stdout_capture.getvalue() return texte_deroule, fig1, fig2
# =====================================================================# 2. INTERFACE UTILISATEUR STREAMLIT# =====================================================================
# Affichage sécurisé de l'imagest.image("PetroOXY.jpg", use_container_width=True)
# 2. Intégration de l'introduction (Principe de fonctionnement)# Affichage de l'introductiontry: with open("intropetrooxy.txt", "r", encoding="utf-8") as f: intro_text = f.read() st.markdown(intro_text)except FileNotFoundError: st.warning("Le fichier 'intropetrooxy.txt' est introuvable dans l'onglet Files.") st.warning("Le fichier 'intropetrooxy.txt' est manquant dans les fichiers sources.")
# En-tête visuelle pour séparer l'introduction de la zone de simulationst.write("---")st.subheader("💻 Zone de Simulation & Calculs")st.subheader("💻 Exécution du code numérique")
# =====================================================================# VOS INPUTS ET CALCULS SONT ICI# =====================================================================# Bouton d'action pour lancer la simulationif st.button("🚀 Lancer la simulation PetroOXY"): with st.spinner("Calcul des équations cinétiques en cours..."): # Exécution du moteur physique log_texte, figure1, figure2 = executer_simulation() st.success("Simulation terminée avec succès !") # Affichage des deux graphiques côte à côte ou l'un sous l'autre st.pyplot(figure1) st.pyplot(figure2) # Optionnel : Affichage des lignes de log de la console with st.expander("📄 Voir le rapport détaillé d'exécution"): st.code(log_texte)
# En-tête visuelle pour séparer les résultats de la conclusionst.write("---")
# 3. Intégration de la conclusion et des perspectives# Affichage de la conclusiontry: with open("conclupetrooxy.txt", "r", encoding="utf-8") as f: conclusion_text = f.read() st.markdown(conclusion_text)except FileNotFoundError: st.warning("Le fichier 'conclupetrooxy.txt' est introuvable dans l'onglet Files.") st.warning("Le fichier 'conclupetrooxy.txt' est manquant dans les fichiers sources.")app.py+161-12