2from typing
import Optional
4from .wrappers
import UGlobalWrapper
as global_wrapper
7 """Process-wide helios-core functions that belong to no Context or plug-in model."""
15 """Check whether a GPU is required by the ``HELIOS_REQUIRE_GPU`` environment variable.
17 True when ``HELIOS_REQUIRE_GPU`` is set to any value other than ``"0"``.
18 This is the counterpart to the ``HELIOS_NO_GPU`` veto: rather than changing
19 what hardware probes report, it makes the absence of a GPU an error.
21 The environment is read on every call rather than cached, so a change made
22 via ``os.environ`` is observed immediately — **except on Windows**, where it
23 is never observed at all. ``libhelios.dll`` links the MSVC C runtime
24 statically, so it holds a private copy of the environment snapshotted when
25 the DLL was loaded, while ``os.environ`` writes go through Python's own
26 runtime. On Windows ``HELIOS_REQUIRE_GPU`` must therefore be set before the
27 interpreter starts (``set HELIOS_REQUIRE_GPU=1`` in the shell, or the ``env:``
28 block of a CI job); setting it from Python has no effect on this function.
29 The same applies to ``HELIOS_NO_GPU``.
32 True if a usable GPU is mandatory for this process
35 >>> from pyhelios import Global
36 >>> Global.gpuRequiredByEnvironment()
39 return global_wrapper.gpuRequiredByEnvironment()
43 """Raise if ``HELIOS_REQUIRE_GPU`` is set but no usable GPU was found.
45 Call at the point code would otherwise skip for lack of a GPU. Does nothing
46 unless ``HELIOS_REQUIRE_GPU`` is set. Setting both ``HELIOS_REQUIRE_GPU`` and
47 ``HELIOS_NO_GPU`` is contradictory and is reported as such rather than letting
50 This function does not itself probe for hardware: reaching it is taken as
51 proof the caller already determined no GPU was usable, so when
52 ``HELIOS_REQUIRE_GPU`` is set it always raises.
55 context_message: Description of what was about to be skipped, included in
59 HeliosError: If a GPU is required by the environment but none was found
62 >>> from pyhelios import Global, RadiationModel
63 >>> if not RadiationModel.probeAnyGPUBackend():
64 ... Global.requireGPUOrFail("radiation ray tracing")
66 global_wrapper.requireGPUOrFail(context_message)
74 """Seed the process-wide random number generator so a run can be reproduced.
76 helios-core has two generators. Each ``Context`` owns one, seeded with
77 :meth:`Context.seedRandomGenerator`, and it drives everything drawn through
78 the Context: primitive placement helpers, ``Context.randu()``, LiDAR
79 synthetic-scan noise, and the plant-architecture library's parameter
80 sampling. The other is a single process-wide generator behind the free
81 function ``helios::randu()``, which plug-in code uses where no Context is at
82 hand: the LiDAR leaf-group and triangle index draws in
83 ``calculateLeafArea()``, and the placement of berries within a grape
84 cluster in PlantArchitecture. This method seeds that second generator.
86 By default it is seeded from ``std::random_device`` and every run differs.
87 Seeding it from Python makes those draws repeatable; seed the Context too
88 if the rest of the simulation must repeat as well.
90 The generator is shared by all threads and access to it is synchronized, so
91 a seed set from any thread applies to every subsequent draw. Seeding fixes
92 the sequence of values drawn, not which thread draws which value, so a
93 parallel region that draws from it is still scheduling-dependent.
96 seed: Value used to seed the generator (unsigned 32-bit)
99 ValueError: If ``seed`` is not an int in ``[0, 2**32 - 1]``
100 RuntimeError: If the native library predates helios-core v1.3.85
103 >>> from pyhelios import Global
104 >>> Global.seedRandomGenerator(42)
105 >>> a = [Global.randu() for _ in range(3)]
106 >>> Global.seedRandomGenerator(42)
107 >>> assert a == [Global.randu() for _ in range(3)]
109 global_wrapper.seedGlobalRandomGenerator(seed)
112 def randu(imin: Optional[int] =
None, imax: Optional[int] =
None):
113 """Draw from the process-wide random number generator.
115 With no arguments, returns a uniform float in ``[0, 1)``. With ``imin`` and
116 ``imax``, returns a uniform integer over the **inclusive** range
117 ``[imin, imax]``; every value, endpoints included, is equally likely, and if
118 ``imin >= imax`` then ``imin`` is returned.
120 This draws from the same generator that :meth:`seedRandomGenerator` seeds,
121 not from any Context's generator; use :meth:`Context.randu` for that one.
124 imin: Lower bound of the integer range (inclusive). Must be given with ``imax``.
125 imax: Upper bound of the integer range (inclusive). Must be given with ``imin``.
128 A float in ``[0, 1)`` when called without arguments, otherwise an int in
132 ValueError: If exactly one of ``imin``/``imax`` is given, or either is not an int
133 RuntimeError: If the native library predates helios-core v1.3.85
135 if imin
is None and imax
is None:
136 return global_wrapper.globalRandu()
137 if imin
is None or imax
is None:
138 raise ValueError(
"randu() takes either no arguments or both imin and imax")
139 return global_wrapper.globalRanduInt(imin, imax)
143 """Cumulative probability that a Beta-distributed leaf inclination is at most ``theta``.
145 This is the CDF of the same Beta leaf-inclination distribution that
146 PlantArchitecture's leaf angle distribution methods sample, in the same
147 parameterization: ``nu`` is the first shape parameter of the underlying Beta
148 variate and ``mu`` the second, so the mean inclination is
149 ``(pi/2) * nu / (mu + nu)``.
151 ``theta`` is measured from vertical and saturates outside ``[0, pi/2]``: a
152 negative angle gives 0 and an angle at or above ``pi/2`` gives 1.
155 theta: Leaf inclination angle (radians)
156 mu: First parameter of the Beta distribution; must be positive
157 nu: Second parameter of the Beta distribution; must be positive
160 Cumulative probability in ``[0, 1]``
163 HeliosError: If ``mu`` or ``nu`` is not positive
164 RuntimeError: If the native library predates helios-core v1.3.87
167 >>> from pyhelios import Global
169 >>> Global.evaluateBetaDistributionCDF(math.pi / 2, 1.0, 1.0)
172 return global_wrapper.evaluateBetaDistributionCDF(theta, mu, nu)
176 """Leaf inclination angle at a given cumulative probability of the Beta distribution.
178 The inverse of :meth:`evaluateBetaDistributionCDF`, useful for laying out a
179 prescribed inclination distribution over a known number of leaves.
182 probability: Cumulative probability; must be in ``[0, 1]``
183 mu: First parameter of the Beta distribution; must be positive
184 nu: Second parameter of the Beta distribution; must be positive
187 Leaf inclination angle (radians) in ``[0, pi/2]``
190 HeliosError: If ``probability`` is outside ``[0, 1]``, or ``mu``/``nu`` is not positive
191 RuntimeError: If the native library predates helios-core v1.3.87
193 return global_wrapper.invertBetaDistributionCDF(probability, mu, nu)
197 """Cumulative probability that an ellipsoidally distributed leaf azimuth is at most ``phi``.
199 The probability is measured from the ellipse rotation ``phi0_degrees``, and
200 ``phi`` is wrapped into ``[0, 2*pi)``.
203 phi: Azimuth angle (radians)
204 e: Eccentricity of the ellipsoidal distribution; must be in ``[0, 1]``
205 phi0_degrees: Azimuthal rotation of the ellipse (degrees)
208 Cumulative probability in ``[0, 1]``
211 HeliosError: If ``e`` is outside ``[0, 1]``
212 RuntimeError: If the native library predates helios-core v1.3.87
214 return global_wrapper.evaluateEllipsoidalAzimuthCDF(phi, e, phi0_degrees)
218 """Leaf azimuth angle at a given cumulative probability of the ellipsoidal distribution.
220 The inverse of :meth:`evaluateEllipsoidalAzimuthCDF`.
223 probability: Cumulative probability; must be in ``[0, 1]``
224 e: Eccentricity of the ellipsoidal distribution; must be in ``[0, 1]``
225 phi0_degrees: Azimuthal rotation of the ellipse (degrees)
228 Azimuth angle (radians) in ``[0, 2*pi)``
231 HeliosError: If ``probability`` or ``e`` is outside ``[0, 1]``
232 RuntimeError: If the native library predates helios-core v1.3.87
234 return global_wrapper.invertEllipsoidalAzimuthCDF(probability, e, phi0_degrees)
241 def blackbodyBandFraction(wavelength_min_nm: float, wavelength_max_nm: float, temperature_K: float) -> float:
242 """Fraction of a blackbody's total emissive power emitted between two wavelengths.
244 This is the integral of Planck's law between the two wavelengths divided by its
245 integral over all wavelengths (``sigma * T**4``). Multiplying the result by
246 ``emissivity * sigma * T**4`` gives the in-band emitted flux of a gray surface,
247 which is what a RadiationModel emission band added with wavelength bounds emits.
250 wavelength_min_nm: Lower wavelength bound (nm); must be >= 0
251 wavelength_max_nm: Upper wavelength bound (nm); must be greater than ``wavelength_min_nm``
252 temperature_K: Blackbody temperature (K); must be > 0
255 Fraction of blackbody emissive power in the band, in ``[0, 1]``
258 ValueError: If an argument is not a number
259 HeliosError: If the bounds or the temperature are out of range
260 RuntimeError: If the native library predates helios-core v1.3.90
263 >>> from pyhelios import Global
264 >>> fraction = Global.blackbodyBandFraction(8000, 14000, 300.0) # 8-14 um at 300 K
265 >>> in_band_flux = 0.98 * 5.670374e-8 * 300.0**4 * fraction # W/m^2
267 return global_wrapper.blackbodyBandFraction(wavelength_min_nm, wavelength_max_nm, temperature_K)
271 """Spectral radiance of a blackbody (Planck's law).
273 Its integral over all wavelengths is ``sigma * T**4 / pi``.
276 wavelength_nm: Wavelength (nm); must be > 0
277 temperature_K: Blackbody temperature (K); must be > 0
280 Spectral radiance in W/m^2/sr/nm
283 ValueError: If an argument is not a number
284 HeliosError: If the wavelength or the temperature is not positive
285 RuntimeError: If the native library predates helios-core v1.3.90
287 return global_wrapper.blackbodySpectralRadiance(wavelength_nm, temperature_K)
Process-wide helios-core functions that belong to no Context or plug-in model.
bool gpuRequiredByEnvironment()
Check whether a GPU is required by the HELIOS_REQUIRE_GPU environment variable.
randu(Optional[int] imin=None, Optional[int] imax=None)
Draw from the process-wide random number generator.
float evaluateBetaDistributionCDF(float theta, float mu, float nu)
Cumulative probability that a Beta-distributed leaf inclination is at most theta.
float blackbodySpectralRadiance(float wavelength_nm, float temperature_K)
Spectral radiance of a blackbody (Planck's law).
float invertEllipsoidalAzimuthCDF(float probability, float e, float phi0_degrees)
Leaf azimuth angle at a given cumulative probability of the ellipsoidal distribution.
None requireGPUOrFail(str context_message)
Raise if HELIOS_REQUIRE_GPU is set but no usable GPU was found.
float evaluateEllipsoidalAzimuthCDF(float phi, float e, float phi0_degrees)
Cumulative probability that an ellipsoidally distributed leaf azimuth is at most phi.
float blackbodyBandFraction(float wavelength_min_nm, float wavelength_max_nm, float temperature_K)
Fraction of a blackbody's total emissive power emitted between two wavelengths.
float invertBetaDistributionCDF(float probability, float mu, float nu)
Leaf inclination angle at a given cumulative probability of the Beta distribution.
None seedRandomGenerator(int seed)
Seed the process-wide random number generator so a run can be reproduced.