diff --git a/docs/sphinx/source/whatsnew/v0.16.0.rst b/docs/sphinx/source/whatsnew/v0.16.0.rst index 78fe64be82..d7ea94ae8d 100644 --- a/docs/sphinx/source/whatsnew/v0.16.0.rst +++ b/docs/sphinx/source/whatsnew/v0.16.0.rst @@ -70,6 +70,9 @@ Enhancements (:issue:`2828`, :pull:`2832`) * Allow variables from multiple datasets to be requested at once in :py:func:`~pvlib.iotools.get_merra2`. (:pull:`2839`) +* Add support for diffuse IAM in the :py:class:`pvlib.pvsystem.Array` and + :py:class:`pvlib.pvsystem.PVSystem` classes (see `pvlib.pvsystem.Array.get_iam_diffuse` + and `pvlib.pvsystem.PVSystem.get_iam_diffuse`). (:issue:`2812`, :pull:`2845`) Documentation diff --git a/pvlib/irradiance.py b/pvlib/irradiance.py index 9d6ddbe665..73f6ddf5b0 100644 --- a/pvlib/irradiance.py +++ b/pvlib/irradiance.py @@ -697,6 +697,11 @@ def isotropic(surface_tilt, dhi, return_components=False): * poa_isotropic: The portion of sky diffuse irradiance on a tilted plane from the isotropic sky dome. [Wm⁻²] + diffuse_components : Dict (array input) or DataFrame (Series input) + Keys/columns are: + * poa_sky_diffuse: Total sky diffuse + * poa_isotropic + References ---------- .. [1] Loutzenhiser P.G. et al. "Empirical validation of models to diff --git a/pvlib/pvsystem.py b/pvlib/pvsystem.py index 02b16edaf1..f69a7fb578 100644 --- a/pvlib/pvsystem.py +++ b/pvlib/pvsystem.py @@ -307,7 +307,7 @@ def get_aoi(self, solar_zenith, solar_azimuth): @_unwrap_single_value def get_irradiance(self, solar_zenith, solar_azimuth, dni, ghi, dhi, dni_extra=None, airmass=None, albedo=None, - model='haydavies', **kwargs): + model='haydavies', diffuse_components=False, **kwargs): """ Uses :py:func:`pvlib.irradiance.get_total_irradiance` to calculate the plane of array irradiance components on the tilted @@ -334,6 +334,11 @@ def get_irradiance(self, solar_zenith, solar_azimuth, dni, ghi, dhi, Ground surface albedo. [unitless] model : String, default 'haydavies' Irradiance model. + diffuse_components : bool, default False + If `True`, returns values for the different diffuse irradiance + components available from the selected model + (e.g., isotropic, circumsolar, horizon brightening). + If `False`, only the total diffuse irradiance is returned. kwargs Extra parameters passed to @@ -373,7 +378,9 @@ def get_irradiance(self, solar_zenith, solar_azimuth, dni, ghi, dhi, array.get_irradiance(solar_zenith, solar_azimuth, dni, ghi, dhi, dni_extra=dni_extra, airmass=airmass, - albedo=albedo, model=model, **kwargs) + albedo=albedo, model=model, + diffuse_components=diffuse_components, + **kwargs) for array, dni, ghi, dhi, albedo in zip( self.arrays, dni, ghi, dhi, albedo ) @@ -382,8 +389,8 @@ def get_irradiance(self, solar_zenith, solar_azimuth, dni, ghi, dhi, @_unwrap_single_value def get_iam(self, aoi, iam_model='physical'): """ - Determine the incidence angle modifier using the method specified by - ``iam_model``. + Determine the incidence angle modifier for direct irradiance + using the method specified by ``iam_model``. Parameters for the selected IAM model are expected to be in ``PVSystem.module_parameters``. Default parameters are available for @@ -411,6 +418,48 @@ def get_iam(self, aoi, iam_model='physical'): return tuple(array.get_iam(aoi, iam_model) for array, aoi in zip(self.arrays, aoi)) + @_unwrap_single_value + def get_iam_diffuse(self, surface_tilt, iam_model='marion_diffuse', + marion_model=None, **kwargs): + """ + Determine the incidence angle modifier for diffuse irradiance using the + method specified by ``iam_model``. + + Parameters for the selected IAM model are expected to be in + ``Array.module_parameters``. Default parameters are available for + the 'marion_diffuse' and 'martin_ruiz_diffuse' models. + + Parameters + ---------- + surface_tilt : float or Series + The tilt angle of the surface in degrees. + iam_model : string, default 'marion_diffuse' + The IAM model to be used. Valid strings are 'marion_diffuse', + 'martin_ruiz_diffuse', and 'schlick_diffuse'. + marion_model : string, default None + The IAM function to evaluate across a solid angle. Only used when + ``iam_model='marion_diffuse'``. Must be one of 'ashrae', + 'physical', 'martin_ruiz', 'sapm', and 'schlick'. + + kwargs : dict, optional + Additional keyword arguments passed to the IAM model function. + + Returns + ------- + iam_diffuse : dict or DataFrame + The AOI modifiers for different diffuse irradiance components. + Included components depend on the selected ``iam_model``. + + Raises + ------ + ValueError + if `iam_model` is not a valid model name. + """ + surface_tilt = self._validate_per_array(surface_tilt) + return tuple(array.get_iam_diffuse(tilt, iam_model=iam_model, + marion_model=marion_model, **kwargs) + for array, tilt in zip(self.arrays, surface_tilt)) + @_unwrap_single_value def get_cell_temperature(self, poa_global, temp_air, wind_speed, model, effective_irradiance=None, longwave_down=None): @@ -1093,7 +1142,7 @@ def get_aoi(self, solar_zenith, solar_azimuth): def get_irradiance(self, solar_zenith, solar_azimuth, dni, ghi, dhi, dni_extra=None, airmass=None, albedo=None, - model='haydavies', **kwargs): + model='haydavies', diffuse_components=False, **kwargs): """ Get plane of array irradiance components. @@ -1121,6 +1170,11 @@ def get_irradiance(self, solar_zenith, solar_azimuth, dni, ghi, dhi, Ground surface albedo. [unitless] model : String, default 'haydavies' Irradiance model. + diffuse_components : bool, default False + If `True`, returns values for the different diffuse irradiance + components available from the selected model + (e.g., isotropic, circumsolar, horizon brightening). + If `False`, only the total diffuse irradiance is returned. kwargs Extra parameters passed to @@ -1161,20 +1215,23 @@ def get_irradiance(self, solar_zenith, solar_azimuth, dni, ghi, dhi, airmass = atmosphere.get_relative_airmass(solar_zenith) orientation = self.mount.get_orientation(solar_zenith, solar_azimuth) - return irradiance.get_total_irradiance(orientation['surface_tilt'], - orientation['surface_azimuth'], - solar_zenith, solar_azimuth, - dni, ghi, dhi, - dni_extra=dni_extra, - airmass=airmass, - albedo=albedo, - model=model, - **kwargs) + return irradiance.get_total_irradiance( + orientation['surface_tilt'], + orientation['surface_azimuth'], + solar_zenith, solar_azimuth, + dni, ghi, dhi, + dni_extra=dni_extra, + airmass=airmass, + albedo=albedo, + model=model, + diffuse_components=diffuse_components, + **kwargs + ) def get_iam(self, aoi, iam_model='physical'): """ - Determine the incidence angle modifier using the method specified by - ``iam_model``. + Determine the incidence angle modifier for direct irradiance + using the method specified by ``iam_model``. Parameters for the selected IAM model are expected to be in ``Array.module_parameters``. Default parameters are available for @@ -1213,6 +1270,79 @@ def get_iam(self, aoi, iam_model='physical'): else: raise ValueError(model + ' is not a valid IAM model') + def get_iam_diffuse(self, surface_tilt, iam_model='marion_diffuse', + marion_model=None): + """ + Determine the incidence angle modifier for various diffuse irradiance + components using the method specified by ``iam_model``. + + Parameters for the selected IAM model are expected to be in + ``Array.module_parameters``. Default parameters are available for + the 'marion_diffuse' and 'martin_ruiz_diffuse' models. + + Parameters + ---------- + surface_tilt : float or Series + The tilt angle of the surface in degrees. + iam_model : string, default 'marion_diffuse' + The IAM model to be used. Valid strings are 'marion_diffuse', + 'martin_ruiz_diffuse' and 'schlick_diffuse'. + marion_model : string, default None + The IAM function to evaluate across a solid angle. Only used when + ``iam_model='marion_diffuse'``. Must be one of 'ashrae', + 'physical', 'martin_ruiz', 'sapm', and 'schlick'. + + Returns + ------- + iam_diffuse : dict or DataFrame + The AOI modifiers for different diffuse irradiance components. + Included components depend on the selected ``iam_model``. + + Raises + ------ + ValueError + if `iam_model` is not a valid model name. + ValueError + if `iam_model` is 'marion_diffuse' and `marion_model` is None. + """ + model = iam_model.lower() + if model == 'marion_diffuse' and marion_model is None: + raise ValueError('marion_model must be specified when ' + 'iam_model="marion_diffuse"') + if model == 'marion_diffuse': + if marion_model in ['ashrae', 'physical', 'martin_ruiz', + 'schlick']: + func = getattr(iam, marion_model) + params = set(inspect.signature(func).parameters.keys()) + params.discard('aoi') + kwargs = _build_kwargs(params, self.module_parameters) + iams = iam.marion_diffuse(model=marion_model, + surface_tilt=surface_tilt, + **kwargs) + elif marion_model == 'sapm': + iams = iam.marion_diffuse(model='sapm', + surface_tilt=surface_tilt, + module=self.module_parameters) + else: + raise ValueError(marion_model + ' is not a valid IAM model') + elif model == 'martin_ruiz_diffuse': + func = getattr(iam, model) # get function at pvlib.iam + # get all parameters from function signature to retrieve them from + # module_parameters if present + params = set(inspect.signature(func).parameters.keys()) + params.discard('aoi') + kwargs = _build_kwargs(params, self.module_parameters) + iams = iam.martin_ruiz_diffuse(surface_tilt=surface_tilt, **kwargs) + elif model == 'schlick_diffuse': + iams = iam.schlick_diffuse(surface_tilt=surface_tilt) + else: + raise ValueError(model + ' is not a valid diffuse IAM model') + + if isinstance(surface_tilt, pd.Series): + iams = pd.DataFrame(iams, index=surface_tilt.index) + + return iams + def get_cell_temperature(self, poa_global, temp_air, wind_speed, model, effective_irradiance=None, longwave_down=None): """ diff --git a/tests/test_pvsystem.py b/tests/test_pvsystem.py index 78aaed70c4..1ba409600d 100644 --- a/tests/test_pvsystem.py +++ b/tests/test_pvsystem.py @@ -113,6 +113,74 @@ def test_PVSystem_get_iam_invalid(sapm_module_params, mocker): system.get_iam(45, iam_model='not_a_model') +def test_PVSystem_get_iam_diffuse_marion(sapm_module_params, mocker): + model_params = {'b': 0.05} + m = mocker.spy(_iam, 'marion_diffuse') + system = pvsystem.PVSystem(module_parameters=model_params) + tilt = 30 + iam = system.get_iam_diffuse(tilt, iam_model='marion_diffuse', + marion_model='ashrae') + m.assert_called_with(model='ashrae', surface_tilt=tilt, + **model_params) + assert isinstance(iam, dict) + assert set(iam.keys()) == {'sky', 'ground', 'horizon'} + + system = pvsystem.PVSystem(module_parameters=sapm_module_params) + tilt = pd.Series([30, 60]) + iam = system.get_iam_diffuse(tilt, iam_model='marion_diffuse', + marion_model='sapm') + assert isinstance(iam, pd.DataFrame) + + +@pytest.mark.parametrize('iam_model', ['martin_ruiz_diffuse', + 'schlick_diffuse']) +def test_PVSystem_get_iam_diffuse(iam_model, mocker): + model_params = {'a_r': 0.16} if iam_model == 'martin_ruiz_diffuse' else {} + m = mocker.spy(_iam, iam_model) + system = pvsystem.PVSystem(module_parameters=model_params) + tilt = 30 + iam = system.get_iam_diffuse(tilt, iam_model=iam_model) + m.assert_called_with(surface_tilt=tilt, **model_params) + assert isinstance(iam, dict) + + +def test_PVSystem_multi_array_get_iam_diffuse(): + model_params = {'b': 0.05} + system = pvsystem.PVSystem( + arrays=[pvsystem.Array(mount=pvsystem.FixedMount(0, 180), + module_parameters=model_params), + pvsystem.Array(mount=pvsystem.FixedMount(0, 180), + module_parameters=model_params)] + ) + iam = system.get_iam_diffuse((30, 60), iam_model='marion_diffuse', + marion_model='ashrae') + assert len(iam) == 2 + assert iam[0] != iam[1] + with pytest.raises(ValueError, + match="Length mismatch for per-array parameter"): + system.get_iam_diffuse((30,), iam_model='marion_diffuse', + marion_model='ashrae') + + +def test_PVSystem_get_iam_diffuse_invalid(sapm_module_params): + system = pvsystem.PVSystem(module_parameters=sapm_module_params) + with pytest.raises(ValueError): + system.get_iam_diffuse(45, iam_model='not_a_model') + + +def test_PVSystem_get_iam_diffuse_marion_invalid(sapm_module_params): + system = pvsystem.PVSystem(module_parameters=sapm_module_params) + with pytest.raises(ValueError): + system.get_iam_diffuse(45, iam_model='marion_diffuse', + marion_model='not_a_model') + + +def test_PVSystem_get_iam_diffuse_marion_missing_model(sapm_module_params): + system = pvsystem.PVSystem(module_parameters=sapm_module_params) + with pytest.raises(ValueError, match="marion_model must be specified"): + system.get_iam_diffuse(45, iam_model='marion_diffuse') + + def test_retrieve_sam_raises_exceptions(): """ Raise an exception if an invalid parameter is provided to `retrieve_sam()`.