2High-level EnergyBalance interface for PyHelios.
4This module provides a user-friendly interface to the energy balance modeling
5capabilities with graceful plugin handling and informative error messages.
9from typing
import List, Optional, Union
10from contextlib
import contextmanager
12from .plugins.registry
import get_plugin_registry
13from .wrappers
import UEnergyBalanceWrapper
as energy_wrapper
14from .Context
import Context, check_context_alive
15from .exceptions
import HeliosError
16from .validation.plugin_decorators
import (
17 validate_energy_run_params, validate_energy_band_params, validate_air_energy_params,
18 validate_evaluate_air_energy_params, validate_output_data_params, validate_print_report_params
21logger = logging.getLogger(__name__)
25 """Exception raised for EnergyBalance-specific errors."""
31 High-level interface for energy balance modeling and thermal calculations.
33 This class provides a user-friendly wrapper around the native Helios
34 energy balance plugin with automatic plugin availability checking and
35 graceful error handling.
37 The energy balance model computes surface temperatures based on local energy
38 balance equations, including radiation absorption, convection, and transpiration.
39 It supports both steady-state and dynamic (time-stepping) calculations.
42 - NVIDIA GPU with CUDA support
43 - CUDA Toolkit installed
44 - Energy balance plugin compiled into PyHelios
47 >>> with Context() as context:
48 ... # Add some geometry
49 ... patch_uuid = context.addPatch(center=[0, 0, 1], size=[1, 1])
51 ... with EnergyBalanceModel(context) as energy_balance:
52 ... # Add radiation band for flux calculations
53 ... energy_balance.addRadiationBand("SW")
55 ... # Run steady-state energy balance
56 ... energy_balance.run()
58 ... # Or run dynamic simulation with timestep
59 ... energy_balance.run(dt=60.0) # 60 second timestep
62 def __init__(self, context: Context):
64 Initialize EnergyBalanceModel with graceful plugin handling.
67 context: Helios Context instance
70 TypeError: If context is not a Context instance
71 EnergyBalanceModelError: If energy balance plugin is not available
74 if not isinstance(context, Context):
75 raise TypeError(f
"EnergyBalanceModel requires a Context instance, got {type(context).__name__}")
81 registry = get_plugin_registry()
83 if not registry.is_plugin_available(
'energybalance'):
85 plugin_info = registry.get_plugin_capabilities()
86 available_plugins = registry.get_available_plugins()
89 "EnergyBalanceModel requires the 'energybalance' plugin which is not available.\n\n"
90 "The energy balance plugin provides GPU-accelerated thermal modeling and surface temperature calculations.\n"
91 "System requirements:\n"
92 "- NVIDIA GPU with CUDA support\n"
93 "- CUDA Toolkit installed\n"
94 "- Energy balance plugin compiled into PyHelios\n\n"
95 "To enable energy balance modeling:\n"
96 "1. Build PyHelios with energy balance plugin:\n"
97 " build_scripts/build_helios --plugins energybalance\n"
98 "2. Or build with multiple plugins:\n"
99 " build_scripts/build_helios --plugins energybalance,visualizer,weberpenntree\n"
100 f
"\nCurrently available plugins: {available_plugins}"
104 alternatives = registry.suggest_alternatives(
'energybalance')
106 error_msg += f
"\n\nAlternative plugins available: {alternatives}"
107 error_msg +=
"\nConsider using radiation or photosynthesis for related thermal modeling."
113 self.
energy_model = energy_wrapper.createEnergyBalanceModel(context.getNativePtr())
116 "Failed to create EnergyBalanceModel instance. "
117 "This may indicate a problem with the native library or CUDA initialization."
119 logger.info(
"EnergyBalanceModel created successfully")
121 except Exception
as e:
125 """Raise if the owning Context has been destroyed (see Context.check_context_alive)."""
126 check_context_alive(self.
context,
"EnergyBalanceModel")
129 """Context manager entry."""
132 def __exit__(self, exc_type, exc_value, traceback):
133 """Context manager exit with proper cleanup."""
136 energy_wrapper.destroyEnergyBalanceModel(self.
energy_model)
137 logger.debug(
"EnergyBalanceModel destroyed successfully")
138 except Exception
as e:
139 logger.warning(f
"Error destroying EnergyBalanceModel: {e}")
144 """Destructor to ensure C++ resources freed even without 'with' statement."""
145 if hasattr(self,
'energy_model')
and self.
energy_model is not None:
147 energy_wrapper.destroyEnergyBalanceModel(self.
energy_model)
149 except Exception
as e:
151 warnings.warn(f
"Error in EnergyBalanceModel.__del__: {e}")
154 """Get the native pointer for advanced operations."""
159 Enable console output messages from the energy balance model.
162 EnergyBalanceModelError: If operation fails
167 except Exception
as e:
172 Disable console output messages from the energy balance model.
175 EnergyBalanceModelError: If operation fails
180 except Exception
as e:
183 @validate_energy_run_params
184 def run(self, uuids: Optional[List[int]] =
None, dt: Optional[float] =
None) ->
None:
186 Run the energy balance model.
188 This method supports multiple execution modes:
189 - Steady state for all primitives: run()
190 - Dynamic with timestep for all primitives: run(dt=60.0)
191 - Steady state for specific primitives: run(uuids=[1, 2, 3])
192 - Dynamic with timestep for specific primitives: run(uuids=[1, 2, 3], dt=60.0)
195 uuids: Optional list of primitive UUIDs to process. If None, processes all primitives.
196 dt: Optional timestep in seconds for dynamic simulation. If None, runs steady-state.
199 ValueError: If parameters are invalid
200 EnergyBalanceModelError: If energy balance calculation fails, if a UUID does not
201 exist, or if the same UUID appears more than once in ``uuids``
202 (helios-core 1.3.92+), or if the emitting radiation bands do not account for
203 the blackbody spectrum exactly once -- overlapping bounded bands, bands holding
204 less than 95% of the emission, or more than one emitting band without
205 wavelength bounds (helios-core 1.3.93+)
208 >>> # Steady state for all primitives
209 >>> energy_balance.run()
211 >>> # Dynamic simulation with 60-second timestep
212 >>> energy_balance.run(dt=60.0)
214 >>> # Steady state for specific patches
215 >>> energy_balance.run(uuids=[patch1_uuid, patch2_uuid])
217 >>> # Dynamic simulation for specific patches
218 >>> energy_balance.run(uuids=[patch1_uuid, patch2_uuid], dt=30.0)
222 if uuids
is None and dt
is None:
225 elif uuids
is None and dt
is not None:
228 elif uuids
is not None and dt
is None:
233 energy_wrapper.runForUUIDsDynamic(self.
energy_model, uuids, dt)
235 except Exception
as e:
238 @validate_energy_band_params
241 Add a radiation band or bands for absorbed flux calculations.
243 The energy balance model uses radiation bands from the RadiationModel
244 plugin to calculate absorbed radiation flux for each primitive.
247 band: Name of radiation band (e.g., "SW", "PAR", "NIR", "LW")
248 or list of band names
251 ValueError: If band name is invalid
252 EnergyBalanceModelError: If operation fails
255 >>> energy_balance.addRadiationBand("SW") # Single band
256 >>> energy_balance.addRadiationBand(["SW", "LW", "PAR"]) # Multiple bands
258 if isinstance(band, str):
260 raise ValueError(
"Band name must be a non-empty string")
263 energy_wrapper.addRadiationBand(self.
energy_model, band)
264 except Exception
as e:
266 elif isinstance(band, list):
268 raise ValueError(
"Bands list cannot be empty")
270 if not isinstance(b, str)
or not b:
271 raise ValueError(
"All band names must be non-empty strings")
274 energy_wrapper.addRadiationBands(self.
energy_model, band)
275 except Exception
as e:
278 raise ValueError(
"Band must be a string or list of strings")
280 @validate_air_energy_params
282 reference_height_m: Optional[float] =
None) ->
None:
284 Enable air energy balance model for canopy-scale thermal calculations.
286 The air energy balance computes average air temperature and water vapor
287 mole fraction based on the energy balance of the air layer in the canopy.
290 canopy_height_m: Optional canopy height in meters. If not provided,
291 computed automatically from primitive bounding box.
292 reference_height_m: Optional reference height in meters where ambient
293 conditions are measured. If not provided, assumes
294 reference height is at canopy top.
297 ValueError: If parameters are invalid
298 EnergyBalanceModelError: If operation fails
301 >>> # Automatic canopy height detection
302 >>> energy_balance.enable_air_energy_balance()
304 >>> # Manual canopy and reference heights
305 >>> energy_balance.enable_air_energy_balance(canopy_height_m=5.0, reference_height_m=10.0)
307 if canopy_height_m
is not None and canopy_height_m <= 0:
308 raise ValueError(
"Canopy height must be positive")
309 if reference_height_m
is not None and reference_height_m <= 0:
310 raise ValueError(
"Reference height must be positive")
314 if canopy_height_m
is None and reference_height_m
is None:
315 energy_wrapper.enableAirEnergyBalance(self.
energy_model)
316 elif canopy_height_m
is not None and reference_height_m
is not None:
317 energy_wrapper.enableAirEnergyBalanceWithParameters(
320 raise ValueError(
"Both canopy_height_m and reference_height_m must be provided together, or both None")
322 except Exception
as e:
326 reference_height_m: float, leaf_area_index: float,
328 ground_UUIDs: Optional[List[int]] =
None) ->
None:
330 Enable the canopy airspace model.
332 Resolves the within-canopy air temperature and humidity surrounding leaves from a
333 vertically layered resistance network, instead of holding them at a prescribed
334 value, so the canopy feeds back on the air driving its own transpiration. The
335 airspace is divided into vertical layers of equal leaf area index, each exchanging
336 sensible heat and water vapor with its leaves, its neighboring layers and -- for
337 the outermost layers -- the soil surface and the above-canopy reference air.
339 Unlike :meth:`enableAirEnergyBalance`, which evolves a prognostic boundary layer
340 and thereby assumes a horizontally infinite canopy, this model suits canopies of
341 limited extent subject to advection, such as an orchard block. The two determine
342 the same air state and **may not both be enabled**.
344 Because it solves for a steady state, it cannot be combined with the form of
345 :meth:`run` that takes a timestep.
347 The above-canopy boundary condition is read from global data
348 ``air_temperature_reference``, ``air_humidity_reference`` and
349 ``wind_speed_reference`` when present, otherwise from this model's defaults. Set
350 them with :meth:`Context.setGlobalData` before calling :meth:`run`.
352 After :meth:`run`, primitive data ``air_temperature``, ``air_humidity`` and
353 ``wind_speed`` are set on the canopy and ground primitives, and global data
354 ``canopy_air_temperature``, ``canopy_air_humidity``, their per-layer counterparts,
355 ``aerodynamic_resistance``, ``canopy_airspace_iterations``, ``friction_velocity`` and
356 ``canopy_stability_parameter`` are reported.
358 Turbulent transport above and at the top of the canopy is corrected for atmospheric
359 stability (Monin-Obukhov) by default: exchange is enhanced over a canopy warmer than
360 the reference air and suppressed over one cooler than it. Use
361 :meth:`disableCanopyAirspaceStabilityCorrection` to assume neutral stability.
363 The soil exchanges sensible heat and water vapor with the lowest layer through the
364 same boundary-layer and moisture conductances its own surface energy balance uses,
365 so the heat and water released by the soil are exactly what the airspace receives.
366 Ground primitives without ``boundarylayer_conductance`` primitive data use the
367 bare-soil conductance of Kustas and Norman (1999) rather than the flat-plate
368 relation used for other primitives; a value set by the user or by the
369 boundary-layer conductance plug-in takes precedence. Soil evaporation is governed
370 by the ``moisture_conductance`` (zero by default, i.e. a dry soil) and
371 ``surface_humidity`` primitive data of the ground primitives. Soil fluxes are
372 averaged over the area of the ground primitives, which are taken to represent the
373 soil surface beneath the whole canopy footprint.
376 canopy_UUIDs: Canopy (leaf) primitives exchanging heat and moisture with the airspace.
377 canopy_height_m: Height of the canopy in meters.
378 reference_height_m: Height at which above-canopy conditions are measured, in
379 meters. Must be greater than ``canopy_height_m``.
380 leaf_area_index: One-sided leaf area index on a ground-area basis.
381 num_layers: Number of vertical layers of equal leaf area index. 1 gives a
382 single within-canopy node.
383 ground_UUIDs: Ground primitives forming the soil node beneath the canopy. When
384 omitted there is no exchange with the soil surface.
387 ValueError: If parameters are invalid.
388 EnergyBalanceModelError: If the operation fails.
391 >>> energy_balance.enableCanopyAirspaceModel(
392 ... canopy_UUIDs=leaf_uuids, canopy_height_m=3.0, reference_height_m=5.0,
393 ... leaf_area_index=2.5, num_layers=5, ground_UUIDs=ground_uuids)
394 >>> energy_balance.run()
397 raise ValueError(
"canopy_UUIDs must contain at least one UUID")
398 if canopy_height_m <= 0:
399 raise ValueError(f
"Canopy height must be positive, got {canopy_height_m}")
400 if reference_height_m <= canopy_height_m:
402 f
"Reference height ({reference_height_m}) must be greater than "
403 f
"canopy height ({canopy_height_m})")
404 if leaf_area_index <= 0:
405 raise ValueError(f
"Leaf area index must be positive, got {leaf_area_index}")
407 raise ValueError(f
"Number of layers must be at least 1, got {num_layers}")
411 energy_wrapper.enableCanopyAirspaceModel(
413 canopy_height_m, reference_height_m, leaf_area_index, num_layers)
414 except Exception
as e:
419 Disable the canopy airspace model.
421 Subsequent calls to :meth:`run` perform a single surface energy balance pass using
422 whatever ``air_temperature`` and ``air_humidity`` primitive data are currently set.
425 EnergyBalanceModelError: If the operation fails.
429 energy_wrapper.disableCanopyAirspaceModel(self.
energy_model)
430 except Exception
as e:
435 Enable Monin-Obukhov stability corrections in the canopy airspace model (the default).
437 The aerodynamic resistance between the canopy top and the reference height, and the
438 eddy diffusivity at the canopy top, are corrected for atmospheric stability using the
439 Obukhov length implied by the sensible and latent heat fluxes leaving the canopy.
440 Unstable conditions (canopy warmer than the reference air, typically daytime) enhance
441 turbulent exchange, and stable conditions (night) suppress it. The model reports the
442 resulting global data ``friction_velocity`` (m/s) and ``canopy_stability_parameter``.
445 NotImplementedError: If the native library predates helios-core v1.3.93.
446 EnergyBalanceModelError: If the operation fails.
450 energy_wrapper.enableCanopyAirspaceStabilityCorrection(self.
energy_model)
451 except NotImplementedError:
453 except Exception
as e:
458 Disable Monin-Obukhov stability corrections in the canopy airspace model.
460 Turbulent transport then assumes neutral stability, as before helios-core 1.3.93,
461 which overestimates the above-canopy resistance over a canopy warmer than the air and
462 underestimates it over one cooler than the air.
465 NotImplementedError: If the native library predates helios-core v1.3.93.
466 EnergyBalanceModelError: If the operation fails.
470 energy_wrapper.disableCanopyAirspaceStabilityCorrection(self.
energy_model)
471 except NotImplementedError:
473 except Exception
as e:
477 max_iterations: int = 50) ->
None:
479 Set convergence criteria for the canopy airspace iteration.
482 tolerance_K: Temperature convergence tolerance in Kelvin. Iteration stops when
483 the maximum change in any layer's air temperature falls below this value.
484 max_iterations: Maximum number of coupled surface-energy-balance and airspace
488 ValueError: If parameters are invalid.
489 EnergyBalanceModelError: If the operation fails.
492 raise ValueError(f
"Convergence tolerance must be positive, got {tolerance_K}")
493 if max_iterations < 1:
494 raise ValueError(f
"Maximum iterations must be at least 1, got {max_iterations}")
498 energy_wrapper.setCanopyAirspaceConvergence(
500 except Exception
as e:
503 @validate_evaluate_air_energy_params
505 UUIDs: Optional[List[int]] =
None) ->
None:
507 Advance the air energy balance over time.
509 This method advances the air energy balance model by integrating over
510 multiple timesteps to reach the target time advancement.
513 dt_sec: Timestep in seconds for integration
514 time_advance_sec: Total time to advance in seconds (must be >= dt_sec)
515 UUIDs: Optional list of primitive UUIDs. If None, processes all primitives.
518 ValueError: If parameters are invalid
519 EnergyBalanceModelError: If operation fails
522 >>> # Advance air energy balance by 1 hour using 60-second timesteps
523 >>> energy_balance.evaluate_air_energy_balance(dt_sec=60.0, time_advance_sec=3600.0)
525 >>> # Advance for specific primitives
526 >>> energy_balance.evaluate_air_energy_balance(
527 ... dt_sec=30.0, time_advance_sec=1800.0, uuids=[patch1_uuid, patch2_uuid])
530 raise ValueError(
"Time step must be positive")
531 if time_advance_sec < dt_sec:
532 raise ValueError(
"Total time advance must be greater than or equal to time step")
536 energy_wrapper.evaluateAirEnergyBalance(self.
energy_model, dt_sec, time_advance_sec)
538 energy_wrapper.evaluateAirEnergyBalanceForUUIDs(
541 except Exception
as e:
544 @validate_output_data_params
547 Add optional output primitive data to the Context.
549 This method adds additional data fields to primitives that will be
550 calculated and stored during energy balance execution.
553 label: Name of the data field to add (e.g., "vapor_pressure_deficit",
554 "boundary_layer_conductance", "net_radiation")
557 ValueError: If label is invalid
558 EnergyBalanceModelError: If operation fails
561 >>> energy_balance.add_optional_output_data("vapor_pressure_deficit")
562 >>> energy_balance.add_optional_output_data("net_radiation")
564 if not label
or not isinstance(label, str):
565 raise ValueError(
"Label must be a non-empty string")
569 energy_wrapper.optionalOutputPrimitiveData(self.
energy_model, label)
570 except Exception
as e:
573 @validate_print_report_params
576 Print a report detailing usage of default input values.
578 This diagnostic method prints information about which primitives are
579 using default values for energy balance parameters, helping identify
580 where additional parameter specification might be needed.
583 UUIDs: Optional list of primitive UUIDs to report on. If None,
584 reports on all primitives.
587 EnergyBalanceModelError: If operation fails
590 >>> # Report on all primitives
591 >>> energy_balance.print_default_value_report()
593 >>> # Report on specific primitives
594 >>> energy_balance.print_default_value_report(uuids=[patch1_uuid, patch2_uuid])
598 energy_wrapper.printDefaultValueReport(self.
energy_model)
600 energy_wrapper.printDefaultValueReportForUUIDs(self.
energy_model, UUIDs)
602 except Exception
as e:
607 Check if EnergyBalanceModel is available in current build.
610 True if plugin is available, False otherwise
612 registry = get_plugin_registry()
613 return registry.is_plugin_available(
'energybalance')
617 Enable GPU acceleration for energy balance calculations.
619 Attempts to enable GPU acceleration using CUDA. If GPU is not available at runtime,
620 this will raise an error. The energy balance model will use three-tier execution:
621 GPU (CUDA), OpenMP (parallel CPU), or serial CPU fallback.
624 NotImplementedError: If library not compiled with CUDA support
625 EnergyBalanceModelError: If GPU acceleration cannot be enabled
628 >>> with EnergyBalanceModel(context) as energy_balance:
630 ... energy_balance.enableGPUAcceleration()
631 ... print("GPU acceleration enabled")
632 ... except NotImplementedError:
633 ... print("GPU not available - using CPU mode")
636 Only available when PyHelios is compiled with CUDA support.
637 OpenMP CPU mode is recommended for most workloads without GPU.
642 except NotImplementedError:
644 except Exception
as e:
649 Disable GPU acceleration and force CPU mode.
651 Forces the use of OpenMP CPU implementation even if GPU is available.
652 Useful for testing, benchmarking, or when CPU performance is preferred.
655 EnergyBalanceModelError: If operation fails
658 >>> energy_balance.disableGPUAcceleration()
661 Only available when PyHelios is compiled with CUDA support.
662 Has no effect if GPU support is not compiled in.
666 energy_wrapper.disableGPUAcceleration(self.
energy_model)
667 except Exception
as e:
672 Check if GPU acceleration is currently enabled.
675 True if GPU acceleration is enabled and available, False otherwise
678 >>> if energy_balance.isGPUAccelerationEnabled():
679 ... print("Using GPU acceleration")
681 ... print("Using CPU mode")
684 Returns False if library not compiled with CUDA support.
688 except NotImplementedError:
690 except Exception
as e:
696 Check if GPU acceleration functions are available in this build.
699 True if GPU acceleration support is compiled in, False otherwise
702 >>> if EnergyBalanceModel.isGPUAccelerationAvailable():
703 ... print("GPU acceleration supported")
705 ... print("GPU acceleration not compiled in - CPU mode only")
707 return energy_wrapper.isGPUAccelerationAvailable()
713 Create EnergyBalanceModel instance with context.
716 context: Helios Context
719 EnergyBalanceModel instance
Exception raised for EnergyBalance-specific errors.
High-level interface for energy balance modeling and thermal calculations.
__init__(self, Context context)
Initialize EnergyBalanceModel with graceful plugin handling.
None optionalOutputPrimitiveData(self, str label)
Add optional output primitive data to the Context.
__del__(self)
Destructor to ensure C++ resources freed even without 'with' statement.
None enableCanopyAirspaceStabilityCorrection(self)
Enable Monin-Obukhov stability corrections in the canopy airspace model (the default).
bool is_available(self)
Check if EnergyBalanceModel is available in current build.
None disableMessages(self)
Disable console output messages from the energy balance model.
None enableMessages(self)
Enable console output messages from the energy balance model.
None disableCanopyAirspaceStabilityCorrection(self)
Disable Monin-Obukhov stability corrections in the canopy airspace model.
bool isGPUAccelerationEnabled(self)
Check if GPU acceleration is currently enabled.
bool isGPUAccelerationAvailable()
Check if GPU acceleration functions are available in this build.
None run(self, Optional[List[int]] uuids=None, Optional[float] dt=None)
Run the energy balance model.
None printDefaultValueReport(self, Optional[List[int]] UUIDs=None)
Print a report detailing usage of default input values.
None setCanopyAirspaceConvergence(self, float tolerance_K=0.01, int max_iterations=50)
Set convergence criteria for the canopy airspace iteration.
__enter__(self)
Context manager entry.
None addRadiationBand(self, Union[str, List[str]] band)
Add a radiation band or bands for absorbed flux calculations.
None enableCanopyAirspaceModel(self, List[int] canopy_UUIDs, float canopy_height_m, float reference_height_m, float leaf_area_index, int num_layers=1, Optional[List[int]] ground_UUIDs=None)
Enable the canopy airspace model.
__exit__(self, exc_type, exc_value, traceback)
Context manager exit with proper cleanup.
_check_context_alive(self)
Raise if the owning Context has been destroyed (see Context.check_context_alive).
getNativePtr(self)
Get the native pointer for advanced operations.
None enableGPUAcceleration(self)
Enable GPU acceleration for energy balance calculations.
None disableGPUAcceleration(self)
Disable GPU acceleration and force CPU mode.
None disableCanopyAirspaceModel(self)
Disable the canopy airspace model.
None evaluateAirEnergyBalance(self, float dt_sec, float time_advance_sec, Optional[List[int]] UUIDs=None)
Advance the air energy balance over time.
None enableAirEnergyBalance(self, Optional[float] canopy_height_m=None, Optional[float] reference_height_m=None)
Enable air energy balance model for canopy-scale thermal calculations.
Exception classes for PyHelios library.
EnergyBalanceModel create_energy_balance_model(Context context)
Create EnergyBalanceModel instance with context.