import os
os.environ["SAS_OPENCL"] = "cuda" # use CUDA GPU backend for sasmodels
import escape as esc
import numpy as np
esc.require("0.9.8")
Loading material database from C:\dev\escape-core\python\src\escape\scattering\..\data\mdb\materials.db
SAXS. Form-factors. Core-shell cylinder (SasView-aligned)¶
A right circular cylinder with a uniform cylindrical core and a shell of equal thickness on the side wall and both end caps.
Reference: https://www.sasview.org/docs/user/models/core_shell_cylinder.html
Parameters (SasView defaults)¶
| Parameter | Variable | Value |
|---|---|---|
| Scale | scale |
1 |
| Background (cm⁻¹) | background |
0.001 |
| Core SLD (10⁻⁶ Å⁻²) | sld_core |
4 |
| Shell SLD (10⁻⁶ Å⁻²) | sld_shell |
4 |
| Solvent SLD (10⁻⁶ Å⁻²) | sld_solvent |
1 |
| Core radius (Å) | radius |
20 |
| Shell thickness (Å) | thickness |
20 |
| Core length (Å) | length |
400 |
| Theta (deg), 2D only | theta |
60 |
| Phi (deg), 2D only | phi |
60 |
Form-factor (SasView core_shell_cylinder.c)¶
The amplitude is a sum of two cylinder contributions (core and total shell):
$$F(q,\alpha) = (\rho_c-\rho_s)\,V_c\,\mathrm{sinc}\!\left(\tfrac{L}{2}q_\parallel\right)\frac{2J_1(r\,q_\perp)}{r\,q_\perp} + (\rho_s-\rho_{\mathrm{sol}})\,V_s\,\mathrm{sinc}\!\left((\tfrac{L}{2}+T)q_\parallel\right)\frac{2J_1((r+T)q_\perp)}{(r+T)q_\perp}$$
$$I(q) = \frac{\mathrm{scale}}{V_s}\int_0^{\pi/2} F^2\,\sin\alpha\,d\alpha + \mathrm{background}$$
# ── Variables ──────────────────────────────────────────────────────────────
q = esc.var("Q")
alpha = esc.var("alpha") # angle between cylinder axis and q
# ── Parameters ─────────────────────────────────────────────────────────────
scale = esc.par("Scale", 1.0, scale=1e8, fixed=True)
radius = esc.par("Radius", 20.0, units=esc.angstr)
thickness = esc.par("Thickness", 20.0, units=esc.angstr)
length = esc.par("Length", 400.0, units=esc.angstr)
sld_core = esc.par("SLD core", 4.0, scale=1e-6, units=f"{esc.angstr}^-2")
sld_shell = esc.par("SLD shell", 4.0, scale=1e-6, units=f"{esc.angstr}^-2")
sld_solvent = esc.par("SLD solvent", 1.0, scale=1e-6, units=f"{esc.angstr}^-2")
background = esc.par("Background", 0.001, userlim=[0.0, 0.03])
# ── Geometry ───────────────────────────────────────────────────────────────
r_outer = radius + thickness # outer (shell) radius
h_core = 0.5 * length # core half-length
h_shell = h_core + thickness # shell half-length (includes end caps)
v_core = np.pi * esc.pow(radius, 2) * length
v_shell = np.pi * esc.pow(r_outer, 2) * (length + 2.0 * thickness)
# ── Oriented amplitude ─────────────────────────────────────────────────────
# q_axial = q * cos(alpha): component along cylinder axis
# q_radial = q * sin(alpha): component perpendicular to axis
q_axial = q * esc.cos(alpha)
q_radial = q * esc.sin(alpha)
# Core contribution: (ρ_core − ρ_shell) * V_core * sinc(h_core*q_axial) * 2J1(r*q_radial)/(r*q_radial)
f_core = ((sld_core - sld_shell) * v_core
* esc.sinc(h_core * q_axial)
* 2.0 * esc.j1_over_x(radius * q_radial))
# Shell contribution: (ρ_shell − ρ_solvent) * V_shell * sinc(h_shell*q_axial) * 2J1(r_outer*q_radial)/(r_outer*q_radial)
f_shell = ((sld_shell - sld_solvent) * v_shell
* esc.sinc(h_shell * q_axial)
* 2.0 * esc.j1_over_x(r_outer * q_radial))
f_tot = f_core + f_shell
# ── Powder average ─────────────────────────────────────────────────────────
i1d = (scale / v_shell
* esc.integral(esc.pow(f_tot, 2) * esc.sin(alpha),
alpha, 0.0, np.pi / 2.0,
numpoints=61, maxiter=5, epsabs=1e-5)
+ background)
i1d.device = "gpu"
qs = np.linspace(0.001, 1.0, 300)
i1d.show(coordinates=qs).config(
title="Core-shell cylinder — powder average (1D)",
xlog=True, ylog=True,
xlabel=f"Q [{esc.angstr}^-1]", ylabel="I(q) [cm^-1]")
2D oriented scattering (qx, qy)¶
For a fixed orientation $(\theta, \phi)$ the amplitude is evaluated directly at detector coordinates. The cylinder axis unit vector is $\hat{\mathbf{u}} = (\sin\theta\cos\phi,\;\sin\theta\sin\phi,\;\cos\theta)$.
$$I_{\mathrm{2D}}(q_x,q_y) = \frac{\mathrm{scale}}{V_s}\,F^2(q_\parallel, q_\perp) + \mathrm{background}$$
qx = esc.var("qx")
qy = esc.var("qy")
theta = esc.par("Theta", 60.0, userlim=[0.0, 180.0], units="deg")
phi = esc.par("Phi", 60.0, userlim=[0.0, 360.0], units="deg")
deg = np.pi / 180.0
sin_t = esc.sin(theta * deg)
ux = sin_t * esc.cos(phi * deg)
uy = sin_t * esc.sin(phi * deg)
q_sq = esc.pow(qx, 2) + esc.pow(qy, 2)
q_par_2d = qx * ux + qy * uy
q_perp_2d = esc.sqrt(q_sq - esc.pow(q_par_2d, 2))
f_core_2d = ((sld_core - sld_shell) * v_core
* esc.sinc(h_core * q_par_2d)
* 2.0 * esc.j1_over_x(radius * q_perp_2d))
f_shell_2d = ((sld_shell - sld_solvent) * v_shell
* esc.sinc(h_shell * q_par_2d)
* 2.0 * esc.j1_over_x(r_outer * q_perp_2d))
f_tot_2d = f_core_2d + f_shell_2d
i2d = scale / v_shell * esc.pow(f_tot_2d, 2) + background
i2d.device = "gpu"
xs = np.linspace(-1.0, 1.0, 300); ys = np.linspace(-1.0, 1.0, 300)
xv, yv = np.meshgrid(xs, ys)
coords_2d = np.column_stack([xv.flatten(), yv.flatten()]).flatten()
i2d.show(coordinates=coords_2d).config(
title="Core-shell cylinder — oriented 2D SAXS (qx, qy)",
xlabel=f"qx [{esc.angstr}^-1]", ylabel=f"qy [{esc.angstr}^-1]",
cblog=True, colorscale="jet")
SasView reference model & comparison¶
| ESCAPE parameter | SasView parameter | Notes |
|---|---|---|
sld_core * 1e-6 |
sld_core |
core SLD (Å⁻²) |
sld_shell * 1e-6 |
sld_shell |
shell SLD (Å⁻²) |
sld_solvent * 1e-6 |
sld_solvent |
solvent SLD (Å⁻²) |
radius |
radius |
core radius (Å) |
thickness |
thickness |
shell thickness (Å) |
length |
length |
core length (Å) |
import time
import matplotlib.pyplot as plt
from sasmodels.core import load_model
from sasmodels.data import empty_data1D
from sasmodels.direct_model import DirectModel
qs = np.linspace(0.001, 1.0, 300, ).copy()
kernel = load_model("core_shell_cylinder")
f_sas = DirectModel(empty_data1D(qs), kernel)
sas_pars = dict(scale=1.0, background=0.001,
sld_core=4.0, sld_shell=4.0, sld_solvent=1.0,
radius=20.0, thickness=20.0, length=400.0)
f_sas(**sas_pars)
i1d.device = "gpu"; i1d(qs[:5])
def timeit(fn, n=5):
t0 = time.perf_counter()
for _ in range(n): result = fn()
return (time.perf_counter() - t0) / n * 1e3, result
t_sas, Iq_sas = timeit(lambda: f_sas(**sas_pars))
i1d.device = "gpu"
t_gpu, Iq_gpu = timeit(lambda: i1d(qs), n=3)
i1d.device = "cpu"
t_cpu, Iq_cpu = timeit(lambda: i1d(qs))
i1d.device = "gpu"
print(f"SASView GPU : {t_sas:.0f} ms")
print(f"ESCAPE GPU : {t_gpu:.0f} ms")
print(f"ESCAPE CPU : {t_cpu:.0f} ms ({len(qs)} q-pts)")
rel = np.max(np.abs((Iq_gpu - Iq_sas) / Iq_sas)) * 100
print(f"Max relative diff vs SasView: {rel:.2f}%")
esc.overlay(Iq_sas, Iq_gpu, Iq_cpu, coordinates=qs).config(
xlabel="Q (1/A)", ylabel="I(q) (1/cm)",
xlog=True, ylog=True,
fig_title=f"Core shell cylinder I(q) — {len(qs)} pts",
labels=["SASView", "ESCAPE GPU", "ESCAPE CPU"],
line_styles=["solid", "dash", "dot"],
line_widths=[2, 3, 3]
)
SASView GPU : 11 ms ESCAPE GPU : 1 ms ESCAPE CPU : 6 ms (300 q-pts) Max relative diff vs SasView: 2.89%
C:\Users\User\AppData\Local\Temp\ipykernel_64064\1689119006.py:16: UserWarning: Input array does not own its data (e.g. it is a view or slice); data will be copied