Chapter: 15. Polymer chains in simulation: Kremer–Grest FENE-WCA and CABS pore partitioning
Each solution below reproduces the chapter state and verifies the exercise targets with explicit assert checks.
import sys
from pathlib import Path
import numpy as np
import matplotlib.pyplot as plt
REPO_ROOT = Path(__file__).resolve().parents[2] if "__file__" in globals() else Path("../..").resolve()
if str(REPO_ROOT) not in sys.path:
sys.path.insert(0, str(REPO_ROOT))
import engsim as es
from engsim.plotting import panel_label, use_style
use_style()
rng = np.random.default_rng(2026)chains = es.sample_freely_jointed_chains(n_chains=2500, n_bonds=200, bond_len=1.0, rng=rng)
re2_num = float(np.mean(np.sum((chains[:, -1, :] - chains[:, 0, :]) ** 2, axis=1)))
rg2_num = float(np.mean([es.radius_of_gyration_sq(c) for c in chains]))
re2_ex, rg2_ex = es.reference.ideal_chain_rg_re_exact(200, 1.0)
H_grid = np.linspace(6.0, 50.0, 20)
K_cabs = es.cabs_slit_partition_coefficient(chains, H_grid)
K_casassa = es.reference.casassa_slit_partition_exact(np.sqrt(rg2_ex), H_grid)print(f"Ideal Chain (N=200): <R_e^2> = {re2_num:.2f} (exact {re2_ex:.2f}), <R_g^2> = {rg2_num:.2f} (exact {rg2_ex:.2f})")
assert np.isclose(re2_num, re2_ex, rtol=0.05) and np.isclose(rg2_num, rg2_ex, rtol=0.05)
max_k_err = float(np.max(np.abs(K_cabs - K_casassa)))
print(f"Max CABS vs Casassa slit-pore partition difference across H in [6, 50]: {max_k_err:.4f}")
assert max_k_err < 0.04
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10.8, 4.1))
ax1.plot(H_grid, K_casassa, "k-", label="Casassa (1967) Exact")
ax1.plot(H_grid, K_cabs, "bo", label="CABS (1 Bulk Sample)")
ax1.set_xlabel("Slit Pore Width H [b]"); ax1.set_ylabel("Partition Coefficient K(H) [-]")
ax1.set_title("(a) CABS vs Casassa Slit-Pore Partitioning"); panel_label(ax1, "(a)")
r_fene = np.linspace(0.75, 1.44, 60)
u_fene_c = -0.5 * 30.0 * (1.5**2) * np.log(1.0 - (r_fene / 1.5)**2)
u_wca_c = np.where(r_fene < 2.0**(1/6), 4.0 * (r_fene**-12 - r_fene**-6) + 1.0, 0.0)
ax2.plot(r_fene, u_fene_c + u_wca_c, "r-", label="Kremer-Grest FENE+WCA")
ax2.set_xlabel("Bond Length r [sigma]"); ax2.set_ylabel("Bonded Potential U(r) [eps]")
ax2.set_title("(b) FENE + WCA Anharmonic Bond Well"); panel_label(ax2, "(b)")
plt.show()Ideal Chain (N=200): <R_e^2> = 195.57 (exact 200.00), <R_g^2> = 33.41 (exact 33.50)
Max CABS vs Casassa slit-pore partition difference across H in [6, 50]: 0.0300

Worked solutions to Exercises 1–3¶
# Worked Solutions Ch 15
z_spans = np.max(chains[:, :, 2], axis=1) - np.min(chains[:, :, 2], axis=1)
span_ratio = float(np.mean(z_spans) / np.sqrt(rg2_ex))
exact_bridge = float(4.0 / np.sqrt(np.pi))
print(f"Ch 15 Exercises — <S_z>/R_g = {span_ratio:.4f} vs. exact Brownian bridge 4/sqrt(pi) = {exact_bridge:.4f}")
assert np.isclose(span_ratio, exact_bridge, rtol=0.05)Ch 15 Exercises — <S_z>/R_g = 2.1514 vs. exact Brownian bridge 4/sqrt(pi) = 2.2568