Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ticket 1156: rename dnn to lattice spacing for paracrystalline models #97

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
234 changes: 207 additions & 27 deletions explore/realspace.py

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions sasmodels/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,16 @@ def _hand_convert(name, oldpars, version=(3, 1, 2)):
if version == (3, 1, 2):
oldpars = _hand_convert_3_1_2_to_4_1(name, oldpars)
if version < (4, 2, 0):
oldpars = _hand_convert_4_1_to_4_2(name, oldpars)
oldpars = _rename_magnetic_pars(oldpars)
return oldpars

def _hand_convert_4_1_to_4_2(name, oldpars):
if name in ('bcc_paracrystal', 'fcc_paracrystal', 'sc_paracrystal'):
oldpars['lattice_spacing'] = oldpars.pop('dnn')
oldpars['lattice_distortion'] = oldpars.pop('d_factor')
return oldpars

def _rename_magnetic_pars(pars):
"""
Change from M0:par to par_M0, etc.
Expand Down
44 changes: 22 additions & 22 deletions sasmodels/models/bcc_paracrystal.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
static double
bcc_Zq(double qa, double qb, double qc, double dnn, double d_factor)
bcc_Zq(double qa, double qb, double qc, double lattice_spacing, double lattice_distortion)
{
// Equations from Matsuoka 26-27-28, multiplied by |q|
const double a1 = (-qa + qb + qc)/2.0;
Expand All @@ -16,12 +16,12 @@ bcc_Zq(double qa, double qb, double qc, double dnn, double d_factor)
// Rewriting denominator
// => exp(a)^2 - 2 cos(d ak) exp(a) + 1)
// => (exp(a) - 2 cos(d ak)) * exp(a) + 1
const double arg = -0.5*square(dnn*d_factor)*(a1*a1 + a2*a2 + a3*a3);
const double arg = -0.5*square(lattice_spacing*lattice_distortion)*(a1*a1 + a2*a2 + a3*a3);
const double exp_arg = exp(arg);
const double Zq = -cube(expm1(2.0*arg))
/ ( ((exp_arg - 2.0*cos(dnn*a1))*exp_arg + 1.0)
* ((exp_arg - 2.0*cos(dnn*a2))*exp_arg + 1.0)
* ((exp_arg - 2.0*cos(dnn*a3))*exp_arg + 1.0));
/ ( ((exp_arg - 2.0*cos(lattice_spacing*a1))*exp_arg + 1.0)
* ((exp_arg - 2.0*cos(lattice_spacing*a2))*exp_arg + 1.0)
* ((exp_arg - 2.0*cos(lattice_spacing*a3))*exp_arg + 1.0));

#elif 0
// ** Alternate form, which perhaps is more approachable
Expand All @@ -35,19 +35,19 @@ bcc_Zq(double qa, double qb, double qc, double dnn, double d_factor)
// One more step leads to the form in sasview 3.x for 2d models
// = tanh(-a) / [1 - cos(d a_k)/cosh(-a)]
//
const double arg = 0.5*square(dnn*d_factor)*(a1*a1 + a2*a2 + a3*a3);
const double arg = 0.5*square(lattice_spacing*lattice_distortion)*(a1*a1 + a2*a2 + a3*a3);
const double sinh_qd = sinh(arg);
const double cosh_qd = cosh(arg);
const double Zq = sinh_qd/(cosh_qd - cos(dnn*a1))
* sinh_qd/(cosh_qd - cos(dnn*a2))
* sinh_qd/(cosh_qd - cos(dnn*a3));
const double Zq = sinh_qd/(cosh_qd - cos(lattice_spacing*a1))
* sinh_qd/(cosh_qd - cos(lattice_spacing*a2))
* sinh_qd/(cosh_qd - cos(lattice_spacing*a3));
#else
const double arg = 0.5*square(dnn*d_factor)*(a1*a1 + a2*a2 + a3*a3);
const double arg = 0.5*square(lattice_spacing*lattice_distortion)*(a1*a1 + a2*a2 + a3*a3);
const double tanh_qd = tanh(arg);
const double cosh_qd = cosh(arg);
const double Zq = tanh_qd/(1.0 - cos(dnn*a1)/cosh_qd)
* tanh_qd/(1.0 - cos(dnn*a2)/cosh_qd)
* tanh_qd/(1.0 - cos(dnn*a3)/cosh_qd);
const double Zq = tanh_qd/(1.0 - cos(lattice_spacing*a1)/cosh_qd)
* tanh_qd/(1.0 - cos(lattice_spacing*a2)/cosh_qd)
* tanh_qd/(1.0 - cos(lattice_spacing*a3)/cosh_qd);
#endif

return Zq;
Expand All @@ -56,9 +56,9 @@ bcc_Zq(double qa, double qb, double qc, double dnn, double d_factor)

// occupied volume fraction calculated from lattice symmetry and sphere radius
static double
bcc_volume_fraction(double radius, double dnn)
bcc_volume_fraction(double radius, double lattice_spacing)
{
return 2.0*sphere_volume(sqrt(0.75)*radius/dnn);
return 2.0*sphere_volume(radius/lattice_spacing);
}

static double
Expand All @@ -68,8 +68,8 @@ form_volume(double radius)
}


static double Iq(double q, double dnn,
double d_factor, double radius,
static double Iq(double q, double lattice_spacing,
double lattice_distortion, double radius,
double sld, double solvent_sld)
{
// translate a point in [-1,1] to a point in [0, 2 pi]
Expand All @@ -93,7 +93,7 @@ static double Iq(double q, double dnn,
SINCOS(phi, sin_phi, cos_phi);
const double qa = qab*cos_phi;
const double qb = qab*sin_phi;
const double form = bcc_Zq(qa, qb, qc, dnn, d_factor);
const double form = bcc_Zq(qa, qb, qc, lattice_spacing, lattice_distortion);
inner_sum += GAUSS_W[j] * form;
}
inner_sum *= phi_m; // sum(f(x)dx) = sum(f(x)) dx
Expand All @@ -102,16 +102,16 @@ static double Iq(double q, double dnn,
outer_sum *= theta_m;
const double Zq = outer_sum/(4.0*M_PI);
const double Pq = sphere_form(q, radius, sld, solvent_sld);
return bcc_volume_fraction(radius, dnn) * Pq * Zq;
return bcc_volume_fraction(radius, lattice_spacing) * Pq * Zq;
}


static double Iqabc(double qa, double qb, double qc,
double dnn, double d_factor, double radius,
double lattice_spacing, double lattice_distortion, double radius,
double sld, double solvent_sld)
{
const double q = sqrt(qa*qa + qb*qb + qc*qc);
const double Zq = bcc_Zq(qa, qb, qc, dnn, d_factor);
const double Zq = bcc_Zq(qa, qb, qc, lattice_spacing, lattice_distortion);
const double Pq = sphere_form(q, radius, sld, solvent_sld);
return bcc_volume_fraction(radius, dnn) * Pq * Zq;
return bcc_volume_fraction(radius, lattice_spacing) * Pq * Zq;
}
18 changes: 9 additions & 9 deletions sasmodels/models/bcc_paracrystal.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

.. math::

V_\text{lattice} = \frac{16\pi}{3} \frac{R^3}{\left(D\sqrt{2}\right)^3}
V_\text{lattice} = \frac{8\pi}{3} \frac{R^3}{\left(2D/\sqrt{3}\right)^3}


The distortion factor (one standard deviation) of the paracrystal is included
Expand Down Expand Up @@ -104,7 +104,7 @@

* **Author:** NIST IGOR/DANSE **Date:** pre 2010
* **Last Modified by:** Paul Butler **Date:** September 29, 2016
* **Last Reviewed by:** Richard Heenan **Date:** March 21, 2016
* **Last Reviewed by:** Paul Butler **Date:** September 16, 2018
"""

import numpy as np
Expand All @@ -126,8 +126,8 @@

# pylint: disable=bad-whitespace, line-too-long
# ["name", "units", default, [lower, upper], "type","description" ],
parameters = [["dnn", "Ang", 220, [-inf, inf], "", "Nearest neighbour distance"],
["d_factor", "", 0.06, [-inf, inf], "", "Paracrystal distortion factor"],
parameters = [["lattice_spacing", "Ang", 220, [-inf, inf], "", "Lattice spacing"],
["lattice_distortion", "", 0.06, [-inf, inf], "", "Paracrystal distortion factor"],
["radius", "Ang", 40, [0, inf], "volume", "Particle radius"],
["sld", "1e-6/Ang^2", 4, [-inf, inf], "sld", "Particle scattering length density"],
["sld_solvent", "1e-6/Ang^2", 1, [-inf, inf], "sld", "Solvent scattering length density"],
Expand All @@ -149,13 +149,13 @@ def random():
# useful between 0.01 and 0.7. Use an exponential distribution
# in this range 'cuz its easy.
radius = 10**np.random.uniform(1.3, 4)
d_factor = 10**np.random.uniform(-2, -0.7) # sigma_d in 0.01-0.7
dnn_fraction = np.random.beta(a=10, b=1)
dnn = radius*4/np.sqrt(3)/dnn_fraction
lattice_distortion = 10**np.random.uniform(-2, -0.7) # sigma_d in 0.01-0.7
lattice_spacing_fraction = np.random.beta(a=10, b=1)
lattice_spacing = radius*4/np.sqrt(3)/lattice_spacing_fraction
pars = dict(
#sld=1, sld_solvent=0, scale=1, background=1e-32,
dnn=dnn,
d_factor=d_factor,
lattice_spacing=lattice_spacing,
lattice_distortion=lattice_distortion,
radius=radius,
)
return pars
Expand Down
30 changes: 15 additions & 15 deletions sasmodels/models/fcc_paracrystal.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
static double
fcc_Zq(double qa, double qb, double qc, double dnn, double d_factor)
fcc_Zq(double qa, double qb, double qc, double lattice_spacing, double lattice_distortion)
{
// Equations from Matsuoka 17-18-19, multiplied by |q|
const double a1 = ( qa + qb)/2.0;
Expand All @@ -15,22 +15,22 @@ fcc_Zq(double qa, double qb, double qc, double dnn, double d_factor)
// Rewriting denominator
// => exp(a)^2 - 2 cos(d ak) exp(a) + 1)
// => (exp(a) - 2 cos(d ak)) * exp(a) + 1
const double arg = -0.5*square(dnn*d_factor)*(a1*a1 + a2*a2 + a3*a3);
const double arg = -0.5*square(lattice_spacing*lattice_distortion)*(a1*a1 + a2*a2 + a3*a3);
const double exp_arg = exp(arg);
const double Zq = -cube(expm1(2.0*arg))
/ ( ((exp_arg - 2.0*cos(dnn*a1))*exp_arg + 1.0)
* ((exp_arg - 2.0*cos(dnn*a2))*exp_arg + 1.0)
* ((exp_arg - 2.0*cos(dnn*a3))*exp_arg + 1.0));
/ ( ((exp_arg - 2.0*cos(lattice_spacing*a1))*exp_arg + 1.0)
* ((exp_arg - 2.0*cos(lattice_spacing*a2))*exp_arg + 1.0)
* ((exp_arg - 2.0*cos(lattice_spacing*a3))*exp_arg + 1.0));

return Zq;
}


// occupied volume fraction calculated from lattice symmetry and sphere radius
static double
fcc_volume_fraction(double radius, double dnn)
fcc_volume_fraction(double radius, double lattice_spacing)
{
return 4.0*sphere_volume(M_SQRT1_2*radius/dnn);
return 4.0*sphere_volume(radius/lattice_spacing);
}

static double
Expand All @@ -40,8 +40,8 @@ form_volume(double radius)
}


static double Iq(double q, double dnn,
double d_factor, double radius,
static double Iq(double q, double lattice_spacing,
double lattice_distortion, double radius,
double sld, double solvent_sld)
{
// translate a point in [-1,1] to a point in [0, 2 pi]
Expand All @@ -65,7 +65,7 @@ static double Iq(double q, double dnn,
SINCOS(phi, sin_phi, cos_phi);
const double qa = qab*cos_phi;
const double qb = qab*sin_phi;
const double form = fcc_Zq(qa, qb, qc, dnn, d_factor);
const double form = fcc_Zq(qa, qb, qc, lattice_spacing, lattice_distortion);
inner_sum += GAUSS_W[j] * form;
}
inner_sum *= phi_m; // sum(f(x)dx) = sum(f(x)) dx
Expand All @@ -75,15 +75,15 @@ static double Iq(double q, double dnn,
const double Zq = outer_sum/(4.0*M_PI);
const double Pq = sphere_form(q, radius, sld, solvent_sld);

return fcc_volume_fraction(radius, dnn) * Pq * Zq;
return fcc_volume_fraction(radius, lattice_spacing) * Pq * Zq;
}

static double Iqabc(double qa, double qb, double qc,
double dnn, double d_factor, double radius,
double lattice_spacing, double lattice_distortion, double radius,
double sld, double solvent_sld)
{
const double q = sqrt(qa*qa + qb*qb + qc*qc);
const double Pq = sphere_form(q, radius, sld, solvent_sld);
const double Zq = fcc_Zq(qa, qb, qc, dnn, d_factor);
return fcc_volume_fraction(radius, dnn) * Pq * Zq;
}
const double Zq = fcc_Zq(qa, qb, qc, lattice_spacing, lattice_distortion);
return fcc_volume_fraction(radius, lattice_spacing) * Pq * Zq;
}
18 changes: 9 additions & 9 deletions sasmodels/models/fcc_paracrystal.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@
(Corrections to FCC and BCC lattice structure calculation)

Authorship and Verification
---------------------------
----------------------------

* **Author:** NIST IGOR/DANSE **Date:** pre 2010
* **Last Modified by:** Paul Butler **Date:** September 29, 2016
* **Last Reviewed by:** Richard Heenan **Date:** March 21, 2016
* **Last Modified by:** Paul Butler **Date:** September 16, 2018
"""

import numpy as np
Expand All @@ -122,8 +122,8 @@

# pylint: disable=bad-whitespace, line-too-long
# ["name", "units", default, [lower, upper], "type","description"],
parameters = [["dnn", "Ang", 220, [-inf, inf], "", "Nearest neighbour distance"],
["d_factor", "", 0.06, [-inf, inf], "", "Paracrystal distortion factor"],
parameters = [["lattice_spacing", "Ang", 220, [-inf, inf], "", "Lattice spacing"],
["lattice_distortion", "", 0.06, [-inf, inf], "", "Paracrystal distortion factor"],
["radius", "Ang", 40, [0, inf], "volume", "Particle radius"],
["sld", "1e-6/Ang^2", 4, [-inf, inf], "sld", "Particle scattering length density"],
["sld_solvent", "1e-6/Ang^2", 1, [-inf, inf], "sld", "Solvent scattering length density"],
Expand All @@ -139,13 +139,13 @@ def random():
"""Return a random parameter set for the model."""
# copied from bcc_paracrystal
radius = 10**np.random.uniform(1.3, 4)
d_factor = 10**np.random.uniform(-2, -0.7) # sigma_d in 0.01-0.7
dnn_fraction = np.random.beta(a=10, b=1)
dnn = radius*4/np.sqrt(2)/dnn_fraction
lattice_distortion = 10**np.random.uniform(-2, -0.7) # sigma_d in 0.01-0.7
lattice_spacing_fraction = np.random.beta(a=10, b=1)
lattice_spacing = radius*4/np.sqrt(2)/lattice_spacing_fraction
pars = dict(
#sld=1, sld_solvent=0, scale=1, background=1e-32,
dnn=dnn,
d_factor=d_factor,
latice_spacing=lattice_spacing,
lattice_distortion=d_factor,
radius=radius,
)
return pars
Expand Down
30 changes: 15 additions & 15 deletions sasmodels/models/sc_paracrystal.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
static double
sc_Zq(double qa, double qb, double qc, double dnn, double d_factor)
sc_Zq(double qa, double qb, double qc, double lattice_spacing, double lattice_distortion)
{
// Equations from Matsuoka 9-10-11, multiplied by |q|
const double a1 = qa;
Expand All @@ -15,21 +15,21 @@ sc_Zq(double qa, double qb, double qc, double dnn, double d_factor)
// Rewriting denominator
// => exp(a)^2 - 2 cos(d ak) exp(a) + 1)
// => (exp(a) - 2 cos(d ak)) * exp(a) + 1
const double arg = -0.5*square(dnn*d_factor)*(a1*a1 + a2*a2 + a3*a3);
const double arg = -0.5*square(lattice_spacing*lattice_distortion)*(a1*a1 + a2*a2 + a3*a3);
const double exp_arg = exp(arg);
const double Zq = -cube(expm1(2.0*arg))
/ ( ((exp_arg - 2.0*cos(dnn*a1))*exp_arg + 1.0)
* ((exp_arg - 2.0*cos(dnn*a2))*exp_arg + 1.0)
* ((exp_arg - 2.0*cos(dnn*a3))*exp_arg + 1.0));
/ ( ((exp_arg - 2.0*cos(lattice_spacing*a1))*exp_arg + 1.0)
* ((exp_arg - 2.0*cos(lattice_spacing*a2))*exp_arg + 1.0)
* ((exp_arg - 2.0*cos(lattice_spacing*a3))*exp_arg + 1.0));

return Zq;
}

// occupied volume fraction calculated from lattice symmetry and sphere radius
static double
sc_volume_fraction(double radius, double dnn)
sc_volume_fraction(double radius, double lattice_spacing)
{
return sphere_volume(radius/dnn);
return sphere_volume(radius/lattice_spacing);
}

static double
Expand All @@ -40,8 +40,8 @@ form_volume(double radius)


static double
Iq(double q, double dnn,
double d_factor, double radius,
Iq(double q, double lattice_spacing,
double lattice_distortion, double radius,
double sld, double solvent_sld)
{
// translate a point in [-1,1] to a point in [0, 2 pi]
Expand All @@ -66,7 +66,7 @@ Iq(double q, double dnn,
SINCOS(phi, sin_phi, cos_phi);
const double qa = qab*cos_phi;
const double qb = qab*sin_phi;
const double form = sc_Zq(qa, qb, qc, dnn, d_factor);
const double form = sc_Zq(qa, qb, qc, lattice_spacing, lattice_distortion);
inner_sum += GAUSS_W[j] * form;
}
inner_sum *= phi_m; // sum(f(x)dx) = sum(f(x)) dx
Expand All @@ -76,16 +76,16 @@ Iq(double q, double dnn,
const double Zq = outer_sum/M_PI_2;
const double Pq = sphere_form(q, radius, sld, solvent_sld);

return sc_volume_fraction(radius, dnn) * Pq * Zq;
return sc_volume_fraction(radius, lattice_spacing) * Pq * Zq;
}

static double
Iqabc(double qa, double qb, double qc,
double dnn, double d_factor, double radius,
double lattice_spacing, double lattice_distortion, double radius,
double sld, double solvent_sld)
{
const double q = sqrt(qa*qa + qb*qb + qc*qc);
const double Pq = sphere_form(q, radius, sld, solvent_sld);
const double Zq = sc_Zq(qa, qb, qc, dnn, d_factor);
return sc_volume_fraction(radius, dnn) * Pq * Zq;
}
const double Zq = sc_Zq(qa, qb, qc, lattice_spacing, lattice_distortion);
return sc_volume_fraction(radius, lattice_spacing) * Pq * Zq;
}
Loading