Spaces:
Sleeping
Sleeping
Download app.py from FBumann/tutorial-01-basics: direct link, hf CLI and curl.
- Browser
- Download file 8.6 kB
-
https://huggingface.co/spaces/FBumann/tutorial-01-basics/resolve/main/app.py
- Command line
-
hf download hf://spaces/FBumann/tutorial-01-basics/app.py
-
curl -L -o app.py https://huggingface.co/spaces/FBumann/tutorial-01-basics/resolve/main/app.py
8.6 kB
| import marimo | |
| __generated_with = "0.15.2" | |
| app = marimo.App(width="columns") | |
| def _(mo): | |
| mo.md( | |
| """ | |
| # Energy System Modeling with Flixopt | |
| This notebook demonstrates how to use the flixopt framework to model a minimalistic energy system | |
| with a boiler, heat demand, and natural gas supply. | |
| THis has been updated again, again, and again. | |
| """ | |
| ) | |
| return | |
| def _(mo): | |
| """ | |
| ## System Configuration | |
| Define the basic parameters for our energy system simulation. | |
| """ | |
| mo.md(""" | |
| **Configure your energy system parameters:** | |
| """) | |
| return | |
| def _(solve_button): | |
| solve_button | |
| return | |
| def _( | |
| boiler_efficiency, | |
| calculations, | |
| fx, | |
| gas_price, | |
| mo, | |
| solve_button, | |
| thermal_load_profile, | |
| time, | |
| timesteps, | |
| ): | |
| """ | |
| ## Create and solve FLowSystem | |
| """ | |
| solve_status = mo.md('') | |
| flow_system_status = mo.md('Click the button to build the FLow System and solve the Optimization') | |
| if not hasattr(solve_button, '_last_processed'): | |
| solve_button._last_processed = 0 | |
| # Only run if button value has changed (new click) | |
| if solve_button.value > solve_button._last_processed: | |
| try: | |
| solve_button._last_processed = solve_button.value | |
| _sleep = 0.2 | |
| with mo.status.spinner(title="Creating and Solving Model ...") as _spinner: | |
| _spinner.update(subtitle="Creating FLowSystem and adding Components ...") | |
| time.sleep(_sleep) | |
| _fs = fx.FlowSystem(timesteps) | |
| _fs.add_elements( | |
| fx.Effect('costs', 'β¬', 'Cost', is_standard=True, is_objective=True), | |
| fx.Source( | |
| 'Natural Gas Tariff', | |
| source=fx.Flow( | |
| label='Gas Flow', | |
| bus='Natural Gas', | |
| size=1000, | |
| effects_per_flow_hour=gas_price.value | |
| ), | |
| ), | |
| fx.Sink( | |
| 'Heat Demand', | |
| sink=fx.Flow( | |
| label='Thermal Load', | |
| bus='District Heating', | |
| size=1, | |
| fixed_relative_profile=thermal_load_profile | |
| ), | |
| ), | |
| fx.linear_converters.Boiler( | |
| 'Boiler', | |
| eta=boiler_efficiency.value, | |
| Q_th=fx.Flow(label='Thermal Output', bus='District Heating', size=50), | |
| Q_fu=fx.Flow(label='Fuel Input', bus='Natural Gas'), | |
| ), | |
| fx.Bus('District Heating'), | |
| fx.Bus('Natural Gas'), | |
| ) | |
| # Create and solve the calculation | |
| _spinner.update(subtitle="Creating Calculation ...") | |
| time.sleep(_sleep) | |
| _calculation = fx.FullCalculation('Simulation1', _fs) | |
| _spinner.update(subtitle="Modeling Variables and Constraints ...") | |
| time.sleep(_sleep) | |
| _calculation.do_modeling() | |
| _spinner.update(subtitle="Solving the Model ...") | |
| time.sleep(_sleep) | |
| _calculation.solve(fx.solvers.HighsSolver(0.01, 60)) | |
| solve_status = mo.callout( | |
| mo.md(f"β **Optimization completed successfully!** (Run #{solve_button.value})"), | |
| kind="success" | |
| ) | |
| flow_system_status = mo.md(f""" | |
| β Flow system built successfully | |
| - Total elements: {len(_fs.all_elements)} | |
| """) | |
| calculations.append(_calculation) | |
| except Exception as e: | |
| solve_status = mo.callout( | |
| mo.md(f"β **Optimization failed:** {str(e)}"), | |
| kind="danger" | |
| ) | |
| return (solve_status,) | |
| def _(boiler_efficiency, gas_price, mo, num_timesteps): | |
| mo.hstack([num_timesteps, boiler_efficiency, gas_price], justify="space-around") | |
| return | |
| def _(mo, solve_status, total_costs): | |
| # Display current parameter values in a callout | |
| mo.hstack( | |
| [solve_status,total_costs], | |
| align='center', | |
| widths='equal', | |
| justify='center' | |
| ) | |
| return | |
| def _(results_overview): | |
| results_overview | |
| return | |
| def _(dh_analysis): | |
| dh_analysis | |
| return | |
| def _(): | |
| # Import required libraries | |
| import marimo as mo | |
| import matplotlib.pyplot as plt | |
| import numpy as np | |
| import pandas as pd | |
| from rich.pretty import pprint | |
| import time | |
| import flixopt as fx | |
| calculations = [] | |
| return calculations, fx, mo, np, pd, time | |
| def _(): | |
| return | |
| def _(calculations, solve_button): | |
| """Refresh to latest""" | |
| solve_button | |
| if len(calculations) == 0: | |
| calculation = None | |
| results = None | |
| else: | |
| calculation = calculations[-1] | |
| results = calculation.results | |
| return calculation, results | |
| def _(mo): | |
| solve_button = mo.ui.button( | |
| label="π Run Optimization", | |
| on_click=lambda value: value + 1, | |
| value = 0, | |
| kind="success", | |
| ) | |
| return (solve_button,) | |
| def _(calculation, mo, results): | |
| results | |
| if calculation is not None: | |
| results_overview = mo.vstack([ | |
| mo.md("### π° Cost Analysis"), | |
| calculation.results.solution['costs(operation)|total_per_timestep'].round(3).to_pandas()[:-1], | |
| ]) | |
| else: | |
| results_overview = None | |
| return (results_overview,) | |
| def _(mo): | |
| # Interactive parameter controls using latest Marimo UI elements | |
| boiler_efficiency = mo.ui.slider( | |
| start=0.3, | |
| stop=0.9, | |
| value=0.5, | |
| step=0.05, | |
| label="Boiler efficiency", | |
| show_value=True | |
| ) | |
| gas_price = mo.ui.slider( | |
| start=0.02, | |
| stop=0.08, | |
| value=0.04, | |
| step=0.01, | |
| label="Gas price (β¬/kWh)", | |
| show_value=True | |
| ) | |
| return boiler_efficiency, gas_price | |
| def _(mo): | |
| num_timesteps = mo.ui.slider( | |
| start=3, | |
| stop=24, | |
| value=3, | |
| label="Number of timesteps", | |
| show_value=True | |
| ) | |
| return (num_timesteps,) | |
| def _(np, num_timesteps, pd): | |
| """ | |
| ## Time Series and Load Profile Setup | |
| """ | |
| # Define timesteps | |
| timesteps = pd.date_range('2020-01-01', periods=num_timesteps.value, freq='h') | |
| # Define thermal load profile (extend or truncate based on timesteps) | |
| base_profile = np.array([30, 0, 20, 25, 15, 35, 10, 40]) # Extended base profile | |
| if len(timesteps) <= len(base_profile): | |
| thermal_load_profile = base_profile[:len(timesteps)] | |
| else: | |
| # Repeat pattern if we need more timesteps | |
| thermal_load_profile = np.tile(base_profile, (len(timesteps) // len(base_profile)) + 1)[:len(timesteps)] | |
| return thermal_load_profile, timesteps | |
| def _(mo, results): | |
| """ | |
| ## District Heating Balance Analysis | |
| """ | |
| if results is None: | |
| dh_analysis = mo.callout( | |
| mo.md("No calculation available for district heating analysis"), | |
| kind="warn" | |
| ) | |
| dh_results = None | |
| else: | |
| try: | |
| # Get district heating results | |
| dh_results = results['District Heating'].node_balance().to_dataframe() | |
| dh_analysis = mo.vstack([ | |
| mo.md("### π₯ District Heating Balance"), | |
| mo.carousel([ | |
| mo.ui.table( | |
| data=dh_results.round(3), | |
| selection=None | |
| ), | |
| results['District Heating'].plot_node_balance(show=False) | |
| ]) | |
| ]) | |
| except Exception as e: | |
| dh_analysis = mo.callout( | |
| mo.md(f"Error retrieving district heating results: {str(e)}"), | |
| kind="danger" | |
| ) | |
| dh_results = None | |
| return (dh_analysis,) | |
| def _(calculation, mo): | |
| total_costs = mo.callout( | |
| mo.md(f"**Total System Cost: {calculation.results.solution['costs|total'].item():.2f} β¬**"), | |
| kind="info", | |
| ) if calculation is not None else None | |
| return (total_costs,) | |
| if __name__ == "__main__": | |
| app.run() | |