Source code for rheojax.models.fractional.fractional_kv_zener

"""Fractional Kelvin-Voigt Zener (FKVZ) Model.

This model consists of a Fractional Kelvin-Voigt element in series with a spring,
providing retardation behavior with equilibrium compliance.

Theory
------
The FKVZ model consists of:
- Spring (G_e) in series with
- Fractional Kelvin-Voigt element (spring G_k in parallel with SpringPot)

Creep compliance:
    J(t) = 1/G_e + (1/G_k) * (1 - E_α(-(t/τ)^α))

Complex compliance:
    J*(ω) = 1/G_e + (1/G_k) / (1 + (iωτ)^α)

where E_α is the one-parameter Mittag-Leffler function.

Parameters
----------
Ge : float
    Series spring modulus (Pa), bounds [1e-3, 1e9]
Gk : float
    KV element modulus (Pa), bounds [1e-3, 1e9]
alpha : float
    Fractional order, bounds [0.0, 1.0]
tau : float
    Retardation time (s), bounds [1e-6, 1e6]

Limit Cases
-----------
- alpha → 0: Two springs in series (J = 1/G_e + 1/G_k)
- alpha → 1: Classical Zener solid in creep formulation

References
----------
- Mainardi, F. (2010). Fractional Calculus and Waves in Linear Viscoelasticity
- Bagley, R.L. & Torvik, P.J. (1986). J. Rheol. 30, 133-155
"""

from __future__ import annotations

from rheojax.core.jax_config import safe_import_jax
from rheojax.logging import get_logger, log_fit
from rheojax.models.fractional.fractional_mixin import FRACTIONAL_ORDER_BOUNDS

jax, jnp = safe_import_jax()


from rheojax.core.base import BaseModel
from rheojax.core.inventory import Protocol
from rheojax.core.parameters import ParameterSet
from rheojax.core.registry import ModelRegistry
from rheojax.core.test_modes import DeformationMode
from rheojax.utils.mittag_leffler import mittag_leffler_e

# Module logger
logger = get_logger(__name__)


[docs] @ModelRegistry.register( "fractional_kv_zener", protocols=[ Protocol.RELAXATION, Protocol.CREEP, Protocol.OSCILLATION, ], deformation_modes=[ DeformationMode.SHEAR, DeformationMode.TENSION, DeformationMode.BENDING, DeformationMode.COMPRESSION, ], ) class FractionalKelvinVoigtZener(BaseModel): """Fractional Kelvin-Voigt Zener model. A fractional viscoelastic model emphasizing retardation behavior with finite equilibrium compliance. Test Modes ---------- - Relaxation: Supported (via inversion) - Creep: Supported (primary mode) - Oscillation: Supported - Rotation: Not supported (no steady-state flow) Examples -------- >>> import jax.numpy as jnp >>> from rheojax.models import FractionalKelvinVoigtZener >>> >>> # Create model >>> model = FractionalKelvinVoigtZener() >>> >>> # Set parameters >>> model.set_params(Ge=1000.0, Gk=500.0, alpha=0.5, tau=1.0) >>> >>> # Predict creep compliance >>> t = jnp.logspace(-2, 2, 50) >>> J_t = model.predict(t) """
[docs] def __init__(self): """Initialize Fractional Kelvin-Voigt Zener model.""" super().__init__() # Define parameters with bounds and descriptions self.parameters = ParameterSet() self.parameters.add( name="Ge", value=1000.0, bounds=(1e-3, 1e9), units="Pa", description="Series spring modulus", ) self.parameters.add( name="Gk", value=500.0, bounds=(1e-3, 1e9), units="Pa", description="KV element modulus", ) self.parameters.add( name="alpha", value=0.5, bounds=FRACTIONAL_ORDER_BOUNDS, units="", description="Fractional order", ) self.parameters.add( name="tau", value=1.0, bounds=(1e-6, 1e6), units="s", description="Retardation time", )
@staticmethod @jax.jit def _predict_creep( t: jnp.ndarray, Ge: float, Gk: float, alpha: float, tau: float, ) -> jnp.ndarray: """Predict creep compliance J(t). J(t) = 1/G_e + (1/G_k) * (1 - E_α(-(t/τ)^α)) Parameters ---------- t : jnp.ndarray Time array (s) Ge : float Series spring modulus (Pa) Gk : float KV element modulus (Pa) alpha : float Fractional order tau : float Retardation time (s) Returns ------- jnp.ndarray Creep compliance J(t) (1/Pa) """ # Add small epsilon to prevent issues epsilon = 1e-12 # Clip alpha to safe range (works with JAX tracers) alpha_safe = jnp.clip(alpha, epsilon, 1.0 - epsilon) tau_safe = tau + epsilon # Instantaneous compliance (elastic response) J_inst = 1.0 / (Ge + epsilon) # Retarded compliance amplitude J_retard_amp = 1.0 / (Gk + epsilon) # Compute argument: z = -(t/τ)^α z = -jnp.power(t / tau_safe, alpha_safe) # Mittag-Leffler function E_α(z) with concrete alpha ml_term = mittag_leffler_e(z, alpha=alpha_safe) # J(t) = 1/G_e + (1/G_k) * (1 - E_α(-(t/τ)^α)) J_t = J_inst + J_retard_amp * (1.0 - ml_term) return J_t @staticmethod @jax.jit def _predict_relaxation( t: jnp.ndarray, Ge: float, Gk: float, alpha: float, tau: float, ) -> jnp.ndarray: """Predict relaxation modulus G(t). Note: Analytical relaxation modulus requires numerical inversion. This provides an approximation based on the creep-relaxation relationship. Parameters ---------- t : jnp.ndarray Time array (s) Ge : float Series spring modulus (Pa) Gk : float KV element modulus (Pa) alpha : float Fractional order tau : float Retardation time (s) Returns ------- jnp.ndarray Relaxation modulus G(t) (Pa) """ # Add small epsilon to prevent issues epsilon = 1e-12 # Clip alpha to safe range (works with JAX tracers) alpha_safe = jnp.clip(alpha, epsilon, 1.0 - epsilon) tau_safe = tau + epsilon # Compute transition function z = -jnp.power(t / tau_safe, alpha_safe) ml_term = mittag_leffler_e(z, alpha=alpha_safe) # Short time modulus G_short = Ge # Long time modulus (series combination) G_long = (Ge * Gk) / (Ge + Gk + epsilon) # Interpolate using Mittag-Leffler decay G_t = G_long + (G_short - G_long) * ml_term return G_t @staticmethod @jax.jit def _predict_oscillation( omega: jnp.ndarray, Ge: float, Gk: float, alpha: float, tau: float, ) -> jnp.ndarray: """Predict complex modulus G*(ω). Convert from complex compliance: J*(ω) = 1/G_e + (1/G_k) / (1 + (iωτ)^α) G*(ω) = 1 / J*(ω) Parameters ---------- omega : jnp.ndarray Angular frequency array (rad/s) Ge : float Series spring modulus (Pa) Gk : float KV element modulus (Pa) alpha : float Fractional order tau : float Retardation time (s) Returns ------- jnp.ndarray Complex modulus array with shape (..., 2) where [:, 0] is G' and [:, 1] is G'' """ # Add small epsilon to prevent issues epsilon = 1e-12 # Clip alpha to safe range (works with JAX tracers) alpha_safe = jnp.clip(alpha, epsilon, 1.0 - epsilon) tau_safe = tau + epsilon # Compute (iωτ)^α omega_tau_alpha = jnp.power(omega * tau_safe, alpha_safe) phase = jnp.pi * alpha_safe / 2.0 i_omega_tau_alpha = omega_tau_alpha * (jnp.cos(phase) + 1j * jnp.sin(phase)) # Complex compliance J_inst = 1.0 / (Ge + epsilon) J_kv = (1.0 / (Gk + epsilon)) / (1.0 + i_omega_tau_alpha) J_star = J_inst + J_kv # Complex modulus (inverse of compliance) G_star = 1.0 / (J_star + epsilon) # Extract storage and loss moduli G_prime = jnp.real(G_star) G_double_prime = jnp.imag(G_star) return jnp.stack([G_prime, G_double_prime], axis=-1) def _fit( self, X: jnp.ndarray, y: jnp.ndarray, **kwargs ) -> FractionalKelvinVoigtZener: """Fit model to data using NLSQ TRF optimization. Parameters ---------- X : jnp.ndarray Independent variable (time or frequency) y : jnp.ndarray Dependent variable (modulus or compliance) **kwargs : dict Additional fitting options Returns ------- self Fitted model instance """ from rheojax.core.test_modes import TestMode from rheojax.utils.optimization import ( create_least_squares_objective, nlsq_optimize, ) # Detect test mode test_mode_str = kwargs.get("test_mode", "creep") # Convert string to TestMode enum if isinstance(test_mode_str, str): test_mode_map = { "relaxation": TestMode.RELAXATION, "creep": TestMode.CREEP, "oscillation": TestMode.OSCILLATION, } test_mode = test_mode_map.get(test_mode_str, TestMode.CREEP) else: test_mode = test_mode_str # Store test mode for model_function self._test_mode = test_mode # Determine data shape for logging data_shape = (len(X),) if hasattr(X, "__len__") else None with log_fit( logger, model="FractionalKelvinVoigtZener", data_shape=data_shape, test_mode=( test_mode_str if isinstance(test_mode_str, str) else str(test_mode) ), ) as ctx: logger.debug( "Starting FKVZ fit", n_points=len(X) if hasattr(X, "__len__") else 1, test_mode=str(test_mode), initial_params=self.parameters.to_dict(), ) # Smart initialization for oscillation mode (Issue #9) if test_mode == TestMode.OSCILLATION: try: import numpy as np from rheojax.utils.initialization import ( initialize_fractional_kv_zener, ) success = initialize_fractional_kv_zener( np.array(X), np.array(y), self.parameters ) if success: logger.debug( "Smart initialization applied from frequency-domain features", initialized_params=self.parameters.to_dict(), ) except Exception as e: logger.debug( "Smart initialization failed, using defaults", error=str(e), ) # Create stateless model function for optimization def model_fn(x, params): """Model function for optimization (stateless).""" Ge, Gk, alpha, tau = params[0], params[1], params[2], params[3] # Direct prediction based on test mode (stateless) if test_mode == TestMode.RELAXATION: return self._predict_relaxation(x, Ge, Gk, alpha, tau) elif test_mode == TestMode.CREEP: return self._predict_creep(x, Ge, Gk, alpha, tau) elif test_mode == TestMode.OSCILLATION: return self._predict_oscillation(x, Ge, Gk, alpha, tau) else: raise ValueError(f"Unsupported test mode: {test_mode}") # Create objective function logger.debug("Creating least squares objective", normalize=True) objective = create_least_squares_objective( model_fn, jnp.array(X), jnp.array(y), normalize=True ) # Optimize using NLSQ TRF logger.debug( "Starting NLSQ optimization", method=kwargs.get("method", "auto"), max_iter=kwargs.get("max_iter", 1000), ) try: result = nlsq_optimize( objective, self.parameters, use_jax=kwargs.get("use_jax", True), method=kwargs.get("method", "auto"), max_iter=kwargs.get("max_iter", 1000), ) except Exception as e: logger.error( "NLSQ optimization raised exception", error_type=type(e).__name__, error=str(e), exc_info=True, ) raise # Validate optimization succeeded if not result.success: logger.error( "Optimization failed", message=result.message, final_params=self.parameters.to_dict(), ) raise RuntimeError( f"Optimization failed: {result.message}. " f"Try adjusting initial values, bounds, or max_iter." ) self.fitted_ = True ctx["final_params"] = self.parameters.to_dict() ctx["success"] = True logger.debug( "FKVZ fit completed successfully", final_params=self.parameters.to_dict(), ) return self def _predict(self, X: jnp.ndarray, **kwargs) -> jnp.ndarray: """Predict response for given input. Parameters ---------- X : jnp.ndarray Independent variable **kwargs Additional arguments (test_mode handled via self._test_mode) Returns ------- jnp.ndarray Predicted values """ from rheojax.core.test_modes import TestMode # Get parameters Ge = self.parameters.get_value("Ge") Gk = self.parameters.get_value("Gk") alpha = self.parameters.get_value("alpha") tau = self.parameters.get_value("tau") # Dispatch based on test_mode if set, otherwise auto-detect _kw_mode = kwargs.get("test_mode") test_mode = ( _kw_mode if _kw_mode is not None else getattr(self, "_test_mode", None) ) if test_mode in ("oscillation", TestMode.OSCILLATION): return self._predict_oscillation(X, Ge, Gk, alpha, tau) elif test_mode in ("relaxation", TestMode.RELAXATION): return self._predict_relaxation(X, Ge, Gk, alpha, tau) elif test_mode in ("creep", TestMode.CREEP): return self._predict_creep(X, Ge, Gk, alpha, tau) # Auto-detect test mode (legacy fallback) if jnp.all(X > 0) and len(X) > 1: log_range = jnp.log10(jnp.max(X)) - jnp.log10(jnp.min(X) + 1e-12) if log_range > 3: return self._predict_oscillation(X, Ge, Gk, alpha, tau) # Default to creep (primary mode for FKVZ) return self._predict_creep(X, Ge, Gk, alpha, tau)
[docs] def model_function(self, X, params, test_mode=None, **kwargs): """Model function for Bayesian inference. This method is required by BayesianMixin for NumPyro NUTS sampling. It computes predictions given input X and a parameter array. Args: X: Independent variable (time or frequency) params: Array of parameter values [Ge, Gk, alpha, tau] Returns: Model predictions as JAX array """ from rheojax.core.test_modes import TestMode # Extract parameters from array (in order they were added to ParameterSet) Ge = params[0] Gk = params[1] alpha = params[2] tau = params[3] # Use test_mode from last fit if available, otherwise default to CREEP # Use explicit test_mode parameter (closure-captured in fit_bayesian) # Fall back to self._test_mode only for backward compatibility if test_mode is None: test_mode = getattr(self, "_test_mode", TestMode.CREEP) # Normalize test_mode to handle both string and TestMode enum if hasattr(test_mode, "value"): test_mode = test_mode.value # Call appropriate prediction function based on test mode if test_mode == TestMode.RELAXATION: return self._predict_relaxation(X, Ge, Gk, alpha, tau) elif test_mode == TestMode.CREEP: return self._predict_creep(X, Ge, Gk, alpha, tau) elif test_mode == TestMode.OSCILLATION: stacked = self._predict_oscillation(X, Ge, Gk, alpha, tau) return stacked[..., 0] + 1j * stacked[..., 1] else: # Default to creep mode for FKVZ model return self._predict_creep(X, Ge, Gk, alpha, tau)
# Convenience alias FKVZ = FractionalKelvinVoigtZener __all__ = ["FractionalKelvinVoigtZener", "FKVZ"]