
Compute the physics-informed residual loss (Norouzi et al. 2025)
Source:R/physics.R
compute_physics_loss.RdEvaluates four physics constraints against the current model weights:
- S1 (L_lin)
Second derivative \(|\partial^2\theta/\partial pF^2|\) in the dry end \(pF \in [5, 7.6]\) should be near zero (linearity).
- S2 (L_pos)
\(\theta(pF = 6.2) \geq 0\) (non-negativity).
- S3 (L_neg)
\(\theta(pF = 7.6) \leq 0\) (non-positivity), or \(\theta(pF = 7.6) = 0\) when
s3_form = "equality".- S4 (L_sat)
First derivative \(|\partial\theta/\partial pF|\) near zero in the saturated plateau \(pF \in [-2, -0.3]\).
Usage
compute_physics_loss(
theta_model,
res_tensors,
lambda3 = 1,
lambda4 = 1000,
lambda5 = 1000,
lambda6 = 1,
s3_form = c("inequality", "equality"),
pf_left = -2,
pf_right = 7.6,
training = TRUE
)Arguments
- theta_model
A Keras model with two inputs:
Xseqandpf_norm.- res_tensors
A named list with four sublists (
set1–set4), each containingXseqandpftensors (output ofresidual_to_tensors()).- lambda3
Weight for S1 linearity loss (default 1.0).
- lambda4
Weight for S2 non-negativity loss (default 1000.0).
- lambda5
Weight for S3 non-positivity loss (default 1000.0).
- lambda6
Weight for S4 saturation loss (default 1.0).
- s3_form
Form of the S3 dry-end residual.
"inequality"(default) reproduces Norouzi et al. (2025) exactly: only \(\theta(pF_1) > 0\) is penalised, through \(\mathrm{mean}\,\mathrm{relu}(\theta)\)."equality"penalises \(\mathrm{mean}\,|\theta|\) instead, pinning \(\theta(pF_1)\) at zero from both sides.The published inequality is satisfied by any non-positive value, and under a monotone decreasing curve it therefore pushes the whole dry end below zero, which in turn forces \(\theta(pF_0)\) negative and puts S3 in direct conflict with S2. Since the physical statement is that water content at oven dryness is zero, not that it is at most zero, the equality form expresses the intended constraint without that conflict.
It does NOT remove the degenerate optimum \(\theta \equiv 0\): the null curve satisfies all four residuals exactly under either form, since its first and second derivatives vanish and \(|\theta| = 0\) everywhere. Nothing in the residual set distinguishes the correct curve from the null one – only the data term does.
- pf_left
Left boundary of the normalised pF domain (default -2).
- pf_right
Right boundary (default 7.6).
- training
Logical passed to the model call (default
TRUE).
Examples
# \donttest{
if (reticulate::py_module_available("tensorflow")) {
df <- prepare_swrc_data(swrc_example, depth_col = "depth")
x <- c("clay", "silt", "bd_gcm3", "soc", "Depth_num")
sc <- fit_minmax(df, x)
rs <- build_residual_sets(df[, x], x, S1 = 20L, S2 = 20L,
S3 = 20L, S4 = 20L)
rt <- lapply(rs, residual_to_tensors, scaler = sc, K = 16L)
mod <- build_swrc_model(n_covariates = length(x), K = 16L)
# The published inequality leaves S3 at zero cost for any negative
# theta(pF 7.6); the equality penalises the distance from zero in
# either direction. L_neg differs between the two for the same weights.
ineq <- compute_physics_loss(mod$theta_model, rt, s3_form = "inequality")
eqty <- compute_physics_loss(mod$theta_model, rt, s3_form = "equality")
c(inequality = as.numeric(ineq$L_neg), equality = as.numeric(eqty$L_neg))
}
# }