Source code for jcmsuite.loaders.powerflux

# SPDX-License-Identifier: MIT
# Copyright (c) 2001-{{ COPYRIGHT_END_YEAR }} JCMwave GmbH

"""Fourier-result to power-flux conversion helpers."""

from __future__ import annotations

from collections.abc import Mapping

import numpy as np

from ..exceptions import InvalidConfigurationError


[docs] def convert_to_powerflux( fourier_results: Mapping[str, object], ) -> dict[str, object]: """Convert Fourier transform results into power-flux density. Parameters ---------- fourier_results : Mapping[str, object] Fourier-transform postprocess result. Returns ------- dict[str, object] Power-flux density result. Raises ------ InvalidConfigurationError If the Fourier-transform result does not contain the required title, header, wave-vector, or field data. """ title_object: object | None = fourier_results.get("title") if not isinstance(title_object, str): raise InvalidConfigurationError( "Invalid Fourier result payload: missing `title`." ) title: str = title_object field_type: str if "ElectricFieldStrength" in title: field_type = "ElectricFieldStrength" elif "MagneticFieldStrength" in title: field_type = "MagneticFieldStrength" else: raise InvalidConfigurationError( "Invalid Fourier result payload: title does not include field type." ) header_object: object | None = fourier_results.get("header") if not isinstance(header_object, Mapping): raise InvalidConfigurationError( "Invalid Fourier result payload: missing `header`." ) header: Mapping[str, object] = header_object if "K" not in fourier_results: return { "title": title.replace(field_type, "PowerFluxDensity"), "header": dict(header), } k_object: object | None = fourier_results.get("K") fields_object: object | None = fourier_results.get(field_type) if not isinstance(k_object, np.ndarray): raise InvalidConfigurationError( "Invalid Fourier result payload: `K` must be ndarray." ) if not isinstance(fields_object, Mapping): raise InvalidConfigurationError( f"Invalid Fourier result payload: `{field_type}` must be a mapping." ) rel_eps_object: object | None = header.get("RelPermittivity") rel_mu_object: object | None = header.get("RelPermeability") if not isinstance(rel_eps_object, (int, float, complex)): raise InvalidConfigurationError( "Invalid Fourier result payload: header missing numeric `RelPermittivity`." ) if not isinstance(rel_mu_object, (int, float, complex)): raise InvalidConfigurationError( "Invalid Fourier result payload: header missing numeric `RelPermeability`." ) try: from scipy import constants eps0: float = constants.epsilon_0 mu0: float = constants.mu_0 except Exception: eps0 = 8.85418781762039e-12 mu0 = 1.25663706143592e-06 rel_eps: complex = complex(rel_eps_object) rel_mu: complex = complex(rel_mu_object) eps: float = float(np.real(rel_eps * eps0)) mu: float = float(np.real(rel_mu * mu0)) k: np.ndarray = k_object factor: float if field_type == "ElectricFieldStrength": factor = 0.5 * float(np.sqrt(eps / mu)) else: factor = 0.5 * float(np.sqrt(mu / eps)) power_fields: dict[int, np.ndarray] = {} k_holographic_norm: np.ndarray = np.linalg.norm(k, axis=1) ones: np.ndarray = np.ones((3, 1)) field_index: object field_value: object for field_index, field_value in fields_object.items(): if not isinstance(field_index, int): raise InvalidConfigurationError( "Invalid Fourier result payload: field index keys must be integers." ) if not isinstance(field_value, np.ndarray): raise InvalidConfigurationError( "Invalid Fourier result payload: field values must be ndarray." ) norm_field: np.ndarray = ( np.sum(np.square(np.abs(field_value)), axis=1) / k_holographic_norm ) kron_field: np.ndarray = np.kron(ones, norm_field).T power_fields[field_index] = factor * kron_field * k return { "title": title.replace(field_type, "PowerFluxDensity"), "header": dict(header), "PowerFluxDensity": power_fields, }