2PhotosynthesisModel Plugin for PyHelios.
4This module provides a high-level interface to the Helios photosynthesis modeling
5plugin, enabling simulation of plant photosynthesis processes using both empirical
9from typing
import List, Optional, Union
10from .Context
import Context
11from .wrappers
import UPhotosynthesisWrapper
as photosynthesis_wrapper
12from .types.photosynthesis
import (
13 PhotosyntheticTemperatureResponseParameters,
14 EmpiricalModelCoefficients,
15 FarquharModelCoefficients,
16 PHOTOSYNTHESIS_SPECIES,
17 validate_species_name,
18 get_available_species,
21from .validation.plugin_decorators
import (
22 validate_photosynthesis_species_params,
23 validate_empirical_model_params,
24 validate_farquhar_model_params,
25 validate_photosynthesis_uuid_params
30 """Exception raised by PhotosynthesisModel operations."""
36 High-level interface for Helios photosynthesis modeling.
38 The PhotosynthesisModel provides methods for configuring and running
39 photosynthesis simulations using various models including empirical
40 and mechanistic (Farquhar-von Caemmerer-Berry) approaches.
43 - Support for empirical and FvCB photosynthesis models
44 - Built-in species library with 21+ plant species
45 - Comprehensive parameter validation
46 - Context manager support for proper cleanup
49 >>> from pyhelios import Context, PhotosynthesisModel
50 >>> from pyhelios.types import EmpiricalModelCoefficients
51 >>> context = Context()
52 >>> with PhotosynthesisModel(context) as photosynthesis:
53 ... # Configure empirical model
54 ... coeffs = EmpiricalModelCoefficients(
55 ... Tref=298.0, # Reference temperature (K)
56 ... Ci_ref=290.0, # Reference CO2 concentration (μmol/mol)
57 ... Asat=20.0, # Light-saturated photosynthesis rate (μmol/m²/s)
58 ... theta=65.0 # Light response curvature (W/m²)
60 ... photosynthesis.setEmpiricalModelCoefficients(coeffs)
61 ... photosynthesis.run()
63 Available species can be queried using:
64 >>> PhotosynthesisModel.get_available_species()
65 ['ALMOND', 'APPLE', 'AVOCADO', ...]
68 def __init__(self, context: Context):
70 Initialize PhotosynthesisModel.
73 context: PyHelios Context instance containing the 3D geometry
76 PhotosynthesisModelError: If plugin is not available or initialization fails
78 if not isinstance(context, Context):
80 f
"Context parameter must be a Context instance, got {type(context).__name__}"
89 if context_ptr
is None:
93 self.
_native_ptr = photosynthesis_wrapper.createPhotosynthesisModel(context_ptr)
97 except Exception
as e:
98 if "photosynthesis plugin is not available" in str(e).lower():
100 "Photosynthesis plugin is not available. "
101 "Please rebuild PyHelios with photosynthesis plugin enabled:\n"
102 " build_scripts/build_helios --plugins photosynthesis"
104 elif "mock mode" in str(e).lower():
106 "PhotosynthesisModel requires native Helios libraries. "
107 "Currently running in mock mode. Please build native libraries:\n"
108 " build_scripts/build_helios --plugins photosynthesis"
114 """Context manager entry."""
117 def __exit__(self, exc_type, exc_value, traceback):
118 """Context manager exit with cleanup."""
122 """Clean up native resources."""
123 if hasattr(self,
'_native_ptr')
and self.
_native_ptr is not None:
125 photosynthesis_wrapper.destroyPhotosynthesisModel(self.
_native_ptr)
132 """Get the native C++ pointer for advanced operations."""
136 """Destructor to ensure cleanup."""
142 Set the photosynthesis model type to empirical.
144 The empirical model uses light response curves with saturation kinetics.
146 photosynthesis_wrapper.setModelTypeEmpirical(self.
_native_ptr)
150 Set the photosynthesis model type to Farquhar-von Caemmerer-Berry.
152 The FvCB model is a mechanistic model accounting for biochemical
153 limitations of C3 photosynthesis.
155 photosynthesis_wrapper.setModelTypeFarquhar(self.
_native_ptr)
160 Run photosynthesis calculations for all primitives in the context.
162 The model must be configured with appropriate coefficients before running.
166 @validate_photosynthesis_uuid_params
169 Run photosynthesis calculations for specific primitives.
172 uuids: Single UUID (integer) or list of UUIDs for primitives
174 if isinstance(uuids, int):
176 photosynthesis_wrapper.runForUUIDs(self.
_native_ptr, uuids)
179 @validate_photosynthesis_species_params
182 Set Farquhar model coefficients from built-in species library.
185 species: Species name from the built-in library
186 uuids: Optional list of primitive UUIDs. If None, applies to all primitives.
189 >>> model.setSpeciesCoefficients("APPLE")
190 >>> model.setSpeciesCoefficients("SOYBEAN", [uuid1, uuid2])
193 photosynthesis_wrapper.setFarquharCoefficientsFromLibrary(self.
_native_ptr, species)
195 photosynthesis_wrapper.setFarquharCoefficientsFromLibraryForUUIDs(self.
_native_ptr, species, uuids)
199 Set Farquhar model coefficients from built-in species library.
201 This method matches the C++ API naming: setFarquharCoefficientsFromLibrary()
204 species: Species name from the built-in library
205 uuids: Optional list of primitive UUIDs. If None, applies to all primitives.
208 >>> model.setFarquharCoefficientsFromLibrary("APPLE")
209 >>> model.setFarquharCoefficientsFromLibrary("SOYBEAN", [uuid1, uuid2])
212 photosynthesis_wrapper.setFarquharCoefficientsFromLibrary(self.
_native_ptr, species)
214 photosynthesis_wrapper.setFarquharCoefficientsFromLibraryForUUIDs(self.
_native_ptr, species, uuids)
218 Get Farquhar model coefficients for a species from the library.
221 species: Species name
224 List of Farquhar model coefficients for the species
226 species = validate_species_name(species)
227 return photosynthesis_wrapper.getFarquharCoefficientsFromLibrary(self.
_native_ptr, species)
232 Static method to get available species without creating a model instance.
235 List of species names available in the photosynthesis library
242 Static method to get species aliases mapping.
245 Dictionary mapping aliases to canonical species names
250 @validate_empirical_model_params
252 uuids: Optional[List[int]] =
None):
254 Set empirical model coefficients.
257 coefficients: EmpiricalModelCoefficients instance with model parameters
258 uuids: Optional list of primitive UUIDs. If None, applies to all primitives.
261 coeff_list = coefficients.to_array()
264 photosynthesis_wrapper.setEmpiricalModelCoefficients(self.
_native_ptr, coeff_list)
266 photosynthesis_wrapper.setEmpiricalModelCoefficientsForUUIDs(self.
_native_ptr, coeff_list, uuids)
268 @validate_farquhar_model_params
270 uuids: Optional[List[int]] =
None):
272 Set Farquhar model coefficients.
275 coefficients: FarquharModelCoefficients instance with FvCB parameters
276 uuids: Optional list of primitive UUIDs. If None, applies to all primitives.
279 coeff_list = coefficients.to_array()
282 photosynthesis_wrapper.setFarquharModelCoefficients(self.
_native_ptr, coeff_list)
284 photosynthesis_wrapper.setFarquharModelCoefficientsForUUIDs(self.
_native_ptr, coeff_list, uuids)
287 def setVcmax(self, vcmax: float, uuids: List[int], dha: Optional[float] =
None,
288 topt: Optional[float] =
None, dhd: Optional[float] =
None):
290 Set maximum carboxylation rate for Farquhar model.
292 This method modifies only the Vcmax parameter while preserving all
293 other existing Farquhar model parameters for each primitive.
296 vcmax: Maximum carboxylation rate at 25°C (μmol m⁻² s⁻¹)
297 uuids: List of primitive UUIDs to modify (required)
298 dha: Activation energy (optional, kJ/mol)
299 topt: Optimal temperature (optional, °C)
300 dhd: Deactivation energy (optional, kJ/mol)
303 Primitives must have existing Farquhar model coefficients set before
304 calling this method. Use setFarquharCoefficientsFromLibrary() first
305 if needed. To modify all primitives, use setFarquharModelCoefficients()
306 with complete coefficient objects.
308 from .types
import FarquharModelCoefficients
316 existing_coeffs = FarquharModelCoefficients.from_array(existing_array)
320 existing_coeffs.Vcmax = vcmax
323 from .types
import PhotosyntheticTemperatureResponseParameters
324 if dhd
is None and topt
is None:
332 existing_coeffs.Vcmax = temp_response.value_at_25C
335 existing_coeffs.Vcmax = vcmax
340 def setJmax(self, jmax: float, uuids: List[int], dha: Optional[float] =
None,
341 topt: Optional[float] =
None, dhd: Optional[float] =
None):
343 Set maximum electron transport rate for Farquhar model.
345 This method modifies only the Jmax parameter while preserving all
346 other existing Farquhar model parameters for each primitive.
349 jmax: Maximum electron transport rate at 25°C (μmol m⁻² s⁻¹)
350 uuids: List of primitive UUIDs to modify (required)
351 dha: Activation energy (optional, kJ/mol)
352 topt: Optimal temperature (optional, °C)
353 dhd: Deactivation energy (optional, kJ/mol)
356 Primitives must have existing Farquhar model coefficients set before
357 calling this method. Use setFarquharCoefficientsFromLibrary() first
358 if needed. To modify all primitives, use setFarquharModelCoefficients()
359 with complete coefficient objects.
361 from .types
import FarquharModelCoefficients
369 existing_coeffs = FarquharModelCoefficients.from_array(existing_array)
372 existing_coeffs.Jmax = jmax
377 def setDarkRespiration(self, respiration: float, uuids: List[int], dha: Optional[float] =
None,
378 topt: Optional[float] =
None, dhd: Optional[float] =
None):
380 Set dark respiration rate.
382 This method modifies only the Rd parameter while preserving all
383 other existing Farquhar model parameters for each primitive.
386 respiration: Dark respiration rate at 25°C (μmol m⁻² s⁻¹)
387 uuids: List of primitive UUIDs to modify (required)
388 dha: Activation energy (optional, kJ/mol)
389 topt: Optimal temperature (optional, °C)
390 dhd: Deactivation energy (optional, kJ/mol)
393 Primitives must have existing Farquhar model coefficients set before
394 calling this method. Use setFarquharCoefficientsFromLibrary() first
395 if needed. To modify all primitives, use setFarquharModelCoefficients()
396 with complete coefficient objects.
398 from .types
import FarquharModelCoefficients
406 existing_coeffs = FarquharModelCoefficients.from_array(existing_array)
409 existing_coeffs.Rd = respiration
414 def setQuantumEfficiency(self, efficiency: float, uuids: List[int], dha: Optional[float] =
None,
415 topt: Optional[float] =
None, dhd: Optional[float] =
None):
417 Set quantum efficiency of photosystem II.
419 This method modifies only the alpha parameter while preserving all
420 other existing Farquhar model parameters for each primitive.
423 efficiency: Quantum efficiency at 25°C (dimensionless, 0-1)
424 uuids: List of primitive UUIDs to modify (required)
425 dha: Activation energy (optional, kJ/mol)
426 topt: Optimal temperature (optional, °C)
427 dhd: Deactivation energy (optional, kJ/mol)
430 Primitives must have existing Farquhar model coefficients set before
431 calling this method. Use setFarquharCoefficientsFromLibrary() first
432 if needed. To modify all primitives, use setFarquharModelCoefficients()
433 with complete coefficient objects.
435 from .types
import FarquharModelCoefficients
443 existing_coeffs = FarquharModelCoefficients.from_array(existing_array)
446 existing_coeffs.alpha = efficiency
452 topt: Optional[float] =
None, dhd: Optional[float] =
None):
454 Set light response curvature parameter.
456 This method modifies only the theta parameter while preserving all
457 other existing Farquhar model parameters for each primitive.
460 curvature: Light response curvature at 25°C (dimensionless)
461 uuids: List of primitive UUIDs to modify (required)
462 dha: Activation energy (optional, kJ/mol)
463 topt: Optimal temperature (optional, °C)
464 dhd: Deactivation energy (optional, kJ/mol)
467 Primitives must have existing Farquhar model coefficients set before
468 calling this method. Use setFarquharCoefficientsFromLibrary() first
469 if needed. To modify all primitives, use setFarquharModelCoefficients()
470 with complete coefficient objects.
473 The theta parameter is stored in the coefficient array but may not be
474 directly exposed in the current FarquharModelCoefficients structure.
475 This method sets the basic curvature value.
477 from .types
import FarquharModelCoefficients
485 existing_coeffs = FarquharModelCoefficients.from_array(existing_array)
497 Get empirical model coefficients for a specific primitive.
503 List of empirical model coefficients
505 return photosynthesis_wrapper.getEmpiricalModelCoefficients(self.
_native_ptr, uuid)
509 Get Farquhar model coefficients for a specific primitive.
515 List of Farquhar model coefficients
517 return photosynthesis_wrapper.getFarquharModelCoefficients(self.
_native_ptr, uuid)
521 Export photosynthesis results with optional label.
524 label: Data label for export
526 photosynthesis_wrapper.optionalOutputPrimitiveData(self.
_native_ptr, label)
530 """Enable photosynthesis model status messages."""
531 photosynthesis_wrapper.enableMessages(self.
_native_ptr)
534 """Disable photosynthesis model status messages."""
535 photosynthesis_wrapper.disableMessages(self.
_native_ptr)
539 Print model configuration report.
542 uuids: Optional list of UUIDs. If None, prints report for all primitives.
545 photosynthesis_wrapper.printDefaultValueReport(self.
_native_ptr)
547 photosynthesis_wrapper.printDefaultValueReportForUUIDs(self.
_native_ptr, uuids)
552 Basic validation that model has been configured.
555 True if model appears to be configured (has native pointer)
561 Reset the model by recreating it.
562 Note: This will clear all configured parameters.
567 context_ptr = self.
context.getNativePtr()
568 self.
_native_ptr = photosynthesis_wrapper.createPhotosynthesisModel(context_ptr)
572 photosynthesis_wrapper.destroyPhotosynthesisModel(old_ptr)
Exception raised by PhotosynthesisModel operations.
High-level interface for Helios photosynthesis modeling.
disableMessages(self)
Disable photosynthesis model status messages.
printModelReport(self, Optional[List[int]] uuids=None)
Print model configuration report.
enableMessages(self)
Enable photosynthesis model status messages.
bool validateConfiguration(self)
Basic validation that model has been configured.
setFarquharCoefficientsFromLibrary(self, str species, Optional[List[int]] uuids=None)
Set Farquhar model coefficients from built-in species library.
setJmax(self, float jmax, List[int] uuids, Optional[float] dha=None, Optional[float] topt=None, Optional[float] dhd=None)
Set maximum electron transport rate for Farquhar model.
cleanup(self)
Clean up native resources.
__enter__(self)
Context manager entry.
List[float] getFarquharModelCoefficients(self, int uuid)
Get Farquhar model coefficients for a specific primitive.
get_native_ptr(self)
Get the native C++ pointer for advanced operations.
setDarkRespiration(self, float respiration, List[int] uuids, Optional[float] dha=None, Optional[float] topt=None, Optional[float] dhd=None)
Set dark respiration rate.
setModelTypeFarquhar(self)
Set the photosynthesis model type to Farquhar-von Caemmerer-Berry.
setVcmax(self, float vcmax, List[int] uuids, Optional[float] dha=None, Optional[float] topt=None, Optional[float] dhd=None)
Set maximum carboxylation rate for Farquhar model.
setModelTypeEmpirical(self)
Set the photosynthesis model type to empirical.
exportResults(self, str label)
Export photosynthesis results with optional label.
resetModel(self)
Reset the model by recreating it.
setQuantumEfficiency(self, float efficiency, List[int] uuids, Optional[float] dha=None, Optional[float] topt=None, Optional[float] dhd=None)
Set quantum efficiency of photosystem II.
setEmpiricalModelCoefficients(self, EmpiricalModelCoefficients coefficients, Optional[List[int]] uuids=None)
Set empirical model coefficients.
dict get_species_aliases()
Static method to get species aliases mapping.
run(self)
Run photosynthesis calculations for all primitives in the context.
runForPrimitives(self, Union[List[int], int] uuids)
Run photosynthesis calculations for specific primitives.
setFarquharModelCoefficients(self, FarquharModelCoefficients coefficients, Optional[List[int]] uuids=None)
Set Farquhar model coefficients.
List[str] get_available_species()
Static method to get available species without creating a model instance.
List[float] getEmpiricalModelCoefficients(self, int uuid)
Get empirical model coefficients for a specific primitive.
List[float] getSpeciesCoefficients(self, str species)
Get Farquhar model coefficients for a species from the library.
setSpeciesCoefficients(self, str species, Optional[List[int]] uuids=None)
Set Farquhar model coefficients from built-in species library.
__exit__(self, exc_type, exc_value, traceback)
Context manager exit with cleanup.
__del__(self)
Destructor to ensure cleanup.
setLightResponseCurvature(self, float curvature, List[int] uuids, Optional[float] dha=None, Optional[float] topt=None, Optional[float] dhd=None)
Set light response curvature parameter.
Temperature response parameters for photosynthetic processes.