13. Examples

showExampleList() Show a list of all examples.
showExample([example]) Opens example in default editor.
runExample(example) Runs example
runAll() Run all examples.

Image quality is for HTML. Fileformats can jpg, png eps,pdf…. in high resolution in publication quality. For publication use .eps or .pdf with image width 8.6 cm and 600 dpi (see .save of a plot).

Examples are mainly based on XmGrace for ploting. Change plot commands for other plot tools.

13.1. In a hurry and short

Daily use example to show how short it can be.

Comments are shown in next examples.

# example in fast hurry without fine tuning of plots

import numpy as np
import jscatter as js

# read data
i5=js.dL(js.examples.datapath+'/iqt_1hho.dat')

# define model
diffusion=lambda A,D,t,elastic,wavevector=0:A*np.exp(-wavevector**2*D*t)+elastic

# make ErrPlot to see progress of intermediate steps with residuals (updated all 2 seconds)
i5.makeErrPlot(title='diffusion model residual plot')
# fit it
i5.fit(model=diffusion,                      # the fit function
       freepar={'D':[0.2],'A':1},            # freepar with start parameters; [..] to get D depend on list element
       fixpar={'elastic':0.0},               # fixed parameters
       mapNames= {'t':'X','wavevector':'q'}, # map names of the model to names of the data
       condition=lambda a:a.X>0.01  )        # a condition to include only values with X larger 0.01

# plot it together with lastfit result
p=js.grace()
p.plot(i5,symbol=[-1,0.4,-1], legend='Q=$q')
p.plot(i5.lastfit,symbol=0,line=[1,1,-1])

p1=js.grace(2,2)                          # plot with a defined size
p1.plot(i5.q,i5.D,i5.D_err,symbol=[2,1,1,''],legend='average effective D')

# D is a list because it got list of start values in brackets [2] which is expanded to [2,2,2,2,2,2....]
Picture about diffusion fit with residuals Picture about diffusion fit diffusion fit result

13.2. How to build simple models

# How to build simple models
# which are actually not so simple....

import numpy as np
import jscatter as js

# Build models in one line using lambda
# directly calc the values and return only Y values
diffusion=lambda A,D,t,wavevector,elastic=0:A*np.exp(-wavevector**2*D*t)+elastic

# use a model from the libraries
# here Teubner-Strey adding background and power law
# this returns as above only Y values
tbpower=lambda q,B,xi,dd,A,beta,bgr:js.ff.teubnerStrey(q=q,xi=xi,d=dd).Y*B+A*q**beta+bgr

# The same as above in a function definition
def diffusion2(A,D,t,elastic,wavevector=0):
    Y= A*np.exp(-wavevector**2*D*t)+elastic
    return Y

# returning dataArray allows additional attributes to be included in the result
# this returns a dataArray with X, Y values and attributes
def diffusion3(A,D,t,wavevector,elastic=0):
    Y=A*np.exp(-wavevector**2*D*t)+elastic
    result=js.dA(np.c_[t,Y].T)
    result.diffusioncoefficient=D
    result.wavevector=q
    result.columnname='time;Iqt'
    return result

def tbpower2(q, B, xi, dd, A, beta, bgr):
    """Model Teubner Strey  + power law and background"""
    # save different contributions for later analysis
    tb = js.ff.teubnerStrey(q=q, xi=xi, d=dd)
    pl=A * q ** beta            # power law
    tb=tb.addZeroColumns(2)
    tb[-2]=pl                   # save powerlaw in new last column
    tb[-1]=tb.Y                 # save Teubner-Strey in last column
    tb.Y = B * tb.Y + pl + bgr  # put full model to Y values (usually tb[1])
    # save the additional parameters ; xi and d already included in teubnerStrey
    tb.A = A
    tb.bgr = bgr
    tb.beta = beta
    tb.columnname='q;Iq,IqTb,Iqpower'
    return tb


# How to add a numpy like docstring see in the example "How to build a complex model".

13.3. How to build a more complex model

# How to build a complex model

import jscatter as js

#build a complex model of different components

def particlesWithInteraction(q,Ra,Rb,molarity,bgr,contrast,collimation=None,beta=True):
    """
    Particles with interaction and ellipsoid form factor as a model for e.g. dense protein solutions.

    Document your model if needed for later use that you know what you did and why.
    Or make it short without all the nasty documentation for testing.
    The example neglects the protein exact shape and non constant scattering length density.
    Proteins are more potato shaped  and nearly never like a ellipsoid or sphere.
    So this model is only valid at low Q as an approximation.

    Parameters
    ----------
    q : float
        Wavevector
    Ra,Rb : float
        Radius
    molarity : float
        Concentration in mol/l
    contrast : float
        Contrast between ellipsoid and solvent.
    bgr : float
        Background e.g. incoherent scattering
    collimation : float
        Collimation length for SANS. For SAXS use None.
    beta : bool
        True include asymmetry factor beta of
        M. Kotlarchyk and S.-H. Chen, J. Chem. Phys. 79, 2461 (1983).

    Returns
    -------
        dataArray

    Notes
    -----
    Explicitly:
    **The return value can be a dataArray OR only Y values**. Both is working for fitting.

    """
    # We need to multiply form factor and structure factor and add an additional background.
    # formfactor of ellipsoid returns dataArray with beta at last column.
    ff=js.ff.ellipsoid(q, Ra, Rb,SLD=contrast,beta=beta)
    V=ff.EllipsoidVolume
    # the structure factor returns also dataArray
    # we need to supply a radius calculated from Ra Rb, this is an assumption of effective radius for the interaction.
    R=(Ra*Rb*Rb)**(1/3.)
    # the volume fraction is concentration * volume
    # the units have to be converted as V is usually nm**3 and concentration is mol/l
    sf=js.sf.PercusYevick(q,R,molarity=molarity)
    if beta:
        # beta is asymmetry factor according to M. Kotlarchyk and S.-H. Chen, J. Chem. Phys. 79, 2461 (1983).
        # correction to apply for the structure factor
        # noinspection PyProtectedMember
        sf.Y=1+ff._beta*(sf.Y-1)
    #
    # molarity (mol/l) with conversion to number/nm**3 result is in cm**-1
    ff.Y=molarity*6.023e23/(1000*1e7**2)*ff.Y*sf.Y+bgr
    # add parameters for later use; ellipsoid parameters are already included in ff
    # if data are saved these are included in the file as documentation
    # or can be used for further calculations e.g. if volume fraction is needed (V*molarity)
    ff.R=R
    ff.bgr=bgr
    ff.Volume=V
    ff.molarity=molarity
    # for small angle neutron scattering we may need instrument resolution smearing
    if collimation is not None:
        # For SAX we set collimation=None and this is ignored
        # For SANS we set some reasonable values as 2000 (mm) or 20000 (mm)
        # as attribute in the data we want to fit.
        result=js.sas.resFunct(ff, collimation, 10, collimation, 10, 0.7, 0.15)
    else:
        result=ff
    #we return the complex model for fitting
    return result

p=js.grace()
q=js.loglist(0.1,5,300)
for m in [0.01,0.1,1,2,3,4]:
    data=particlesWithInteraction(q,Ra=3,Rb=4,molarity=m*1e-3,bgr=0,contrast=1)
    p.plot(data,le='c= $molarity M')

p.legend()
p.yaxis(min=100,scale='l',label='I(Q) / cm\S-1',tick=[10,9])
p.xaxis(scale='n',label='Q / nm\S-1')
p.title('Ellipsoidal particles with interaction')
p.subtitle('Ra=3, Rb=4 and PercusYevick structure factor')
# Hint for fitting SANS data or other parameter dependent fit:
# For a combined fit of several collimation distances each dataset should contain an attribute data.collimation.
# This is automatically used in the fit, if there is not explicit fit parameter with this name.

if 0:
    p.save('interactingParticles.agr')
    p.save('interactingParticles.jpeg')
Image of interacting particles scattering

13.4. Some Sinusoidial fits with different kinds to use data atrributes

import jscatter as js
import numpy as np

# Basic fit examples with synthetic data. Usually data are loaded from a file.

# Fit sine to simulated data
x=np.r_[0:10:0.1]
data=js.dA(np.c_[x,np.sin(x)+0.2*np.random.randn(len(x)),x*0+0.2].T)           # simulate data with error
data.fit(lambda x,A,a,B:A*np.sin(a*x)+B,{'A':1.2,'a':1.2,'B':0},{},{'x':'X'})  # fit data
data.showlastErrPlot()                                                         # show fit
data.errPlotTitle('Fit Sine')

# Fit sine to simulated data using an attribute in data with same name
data=js.dA(np.c_[x,1.234*np.sin(x)+0.1*np.random.randn(len(x)),x*0+0.1].T)     # create data
data.A=1.234                                                                   # add attribute
data.makeErrPlot()                                                             # makes erroplot prior to fit
data.fit(lambda x,A,a,B:A*np.sin(a*x)+B,{'a':1.2,'B':0},{},{'x':'X'})          # fit using .A
data.errPlotTitle('Fit Sine with attribute')

# Fit sine to simulated data using an attribute in data with different name and fixed B
data=js.dA(np.c_[x,1.234*np.sin(x)+0.1*np.random.randn(len(x)),x*0+0.1].T)       # create data
data.dd=1.234                                                                    # add attribute
data.fit(lambda x,A,a,B:A*np.sin(a*x)+B,{'a':1.2,},{'B':0},{'x':'X','A':'dd'})   # fit data
data.showlastErrPlot()                                                           # show fit
data.errPlotTitle('Fit Sine with attribut and fixed B')

# Fit sine to simulated dataList using an attribute in data with different name and fixed B from data.
# first one common parameter then as parameter list
data=js.dL()
ef=0.1  # increase this to increase error bars of final result
for ff in [0.001,0.4,0.8,1.2,1.6]:                                                      # create data
   data.append( js.dA(np.c_[x,(1.234+ff)*np.sin(x+ff)+ef*ff*np.random.randn(len(x)),x*0+ef*ff].T) )
   data[-1].B=0.2*ff/2                                                                 # add attributes
# fit with a single parameter for all data, obviously wrong result
data.fit(lambda x,A,a,B,p:A*np.sin(a*x+p)+B,{'a':1.2,'p':0,'A':1.2},{},{'x':'X'})
data.showlastErrPlot()                                                                 # show fit
data.errPlotTitle('Fit Sine with attribut and common fit parameter')
# now allowing multiple p,A,B as indicated by the list starting value
data.fit(lambda x,A,a,B,p:A*np.sin(a*x+p)+B,{'a':1.2,'p':[0],'B':[0,0.1],'A':[1]},{},{'x':'X'})
data.errPlotTitle('Fit Sine with attribut and non common fit parameter')
# plot p against A , just as demonstration
p=js.grace()
p.plot(data.A,data.p,data.p_err)


# 3D fit data with an X,Z grid data and Y values
# For 3D fit we calc Y values from X,Z coordinates (only for scalar Y data).
# For fitting we need data in X,Z,Y column format.
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
#
# create 3D data with X,Z axes and Y values as Y=f(X,Z)
x,z=np.mgrid[-5:5:0.25,-5:5:0.25]
xyz=js.dA(np.c_[x.flatten(),z.flatten(),0.3*np.sin(x*z/np.pi).flatten()+0.01*np.random.randn(len(x.flatten())),0.01*np.ones_like(x).flatten() ].T)
# set columns where to find X,Y,Z )
xyz.setColumnIndex(ix=0,iz=1,iy=2,iey=3)
#
ff=lambda x,z,a,b:a*np.sin(b*x*z)
xyz.fit(ff,{'a':1,'b':1/3.},{},{'x':'X','z':'Z'})
#
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.set_title('3D Sinusoidal fit')
ax.scatter(xyz.X,xyz.Z,xyz.Y)
ax.tricontour(xyz.lastfit.X,xyz.lastfit.Z,xyz.lastfit.Y, cmap=cm.coolwarm,linewidth=0, antialiased=False)
plt.show(block=False)
SinusoidialFit Sinusoidial3D

13.5. Simple diffusion fit of not so simple diffusion case

Here the long part with description from first example.

This is the diffusion of a protein in solution. This is NOT constant as for Einstein diffusion.

These simulated data are similar to data measured by Neutron Spinecho Spectroscopy, which measures on the length scale of the protein and therefore also rotational diffusion contributes to the signal. At low wavevectors additional the influence of the structure factor leads to an upturn, which is neglected in the simulated data. To include look at later Hydrodynamic function.

# -*- coding: utf-8 -*-

# import jscatter and numpy
import numpy as np
import jscatter as js

# read the data (16 sets) with attributes as q, Dtrans .... into dataList
i5=js.dL(js.examples.datapath+'/iqt_1hho.dat')

# define a model for the fit
diffusion=lambda A,D,t,elastic,wavevector=0:A*np.exp(-wavevector**2*D*t)+elastic
# do the fit
i5.fit(model=diffusion,                     # the fit function
       freepar={'D':[0.08],'A':0.98},       # start parameters, "[]" -> independent fit
       fixpar={'elastic':0.0},              # fixed parameters
       mapNames={'t':'X','wavevector':'q'}) # map names from the model to names from the data
# single valued start parameters are the same for all dataArrays
# list start parameters indicate independent fitting for datasets
# the command line shows progress and the final result, which is found in .lastfit
i5.showlastErrPlot(yscale='l') # opens plot with residuals

# open a plot with fixed size and plot
p=js.grace(1.2,0.8)
# plot the data with Q values in legend as symbols
p.plot(i5,symbol=[-1,0.4,-1],legend='Q=$q')
# plot fit results in lastfit as lines without symbol or legend
p.plot(i5.lastfit,symbol=0,line=[1,1,-1])

# pretty up if needed
p.yaxis(min=0.02,max=1.1,scale='log',charsize=1.5,label='I(Q,t)/I(Q,0)')
p.xaxis(min=0,max=130,charsize=1.5,label='t / ns')
p.legend(x=110,y=0.9,charsize=1)
p.title('I(Q,t) as measured by Neutron Spinecho Spectroscopy',size=1.3)
p.text('for diffusion a single exp. decay',x=60,y=0.35,rot=360-20,color=4)
p.text(r'f(t)=A*e\S-Q\S2\N\SDt',x=100,y=0.025,rot=0,charsize=1.5)

if 0: # optional; save in different formats
    p.save('DiffusionFit.agr')
    p.save('DiffusionFit.jpg')

# This is the basis of the simulated data above
D=js.dA('./exampleData/1hho.Dq')
Picture about diffusion fit
p1=js.grace(1,1)                          # plot with a defined size
p1.plot(i5.q,i5.D,i5.D_err,symbol=[2,1,1,''],legend='average effective D')
p1.plot(D.X,D.Y*1000.,sy=0,li=1,legend='diffusion coefficient 1hho')
# pretty up if needed
p1.title('diffusion constant of a dilute protein in solution',size=1.5)
p1.subtitle('the increase is due to rotational diffusion on top of translational diffusion at Q=0',size=1)
p1.xlabel('Q / nm\S-1',1.5)              # xlabel in size 1.5
p1.xaxis(min=0,max=3,charsize=1.5)                   # xaxis numbers in size 1.5
p1.ylabel(r'D\seff\N / nm\S2\N/ns',1.5)
p1.yaxis(min=0.05,max=0.15,charsize=1.5)
p1.legend(x=1,y=0.14)

if 0:
    p1.save('effectiveDiffusion.agr')
    p1.save('effectiveDiffusion.jpg')
diffusion fit result

13.6. How to smooth Xray data and make an inset in the plot

# -*- coding: utf-8 -*-

import jscatter as js
import numpy as np
from scipy.interpolate import LSQUnivariateSpline

# load data
ao50=js.dA(js.examples.datapath+'/a0_336.dat')
ao50.conc=50
ao10=js.dA(js.examples.datapath+'/a0_338.dat')
ao10.conc=2

p=js.grace(1.5,1)
p.clear()

p.plot(ao50.X,ao50.Y, legend='50 mg/ml')
p.plot(ao10.X,ao10.Y,line=[0],symbol=[1,0.05,2], legend='2mg/ml')
p.xaxis(0,6)
p.yaxis(0.05,200,scale='logarithmic')
p.ylabel('I(Q) / a.u.')
p.xlabel('Q / nm\S-1')
p.title('smoothed X-ray data')
p.subtitle('inset is the extracted structure factor at low Q')


#smoothing with a spline
# determine the knots of the spline 
#less points than data points
t=np.r_[ao10.X[1]:ao10.X[-2]:30j]
# calculate the spline
f = LSQUnivariateSpline(ao10.X,ao10.Y,t)
# calculate the new y values of the spline at the x points
ys = f(ao10.X)
p.plot(ao10.X,ys,symbol=[1,0.2,5,5],legend='2 mg/ml spline ')
p.plot(t,f(t),line=[0],symbol=[1,0.2,2,1],legend='knot of spline')

# other idea: use lower number of points with averages in intervals
# this makes 100 intervals with average X and Y values and errors if wanted. Check prune how to use it!
# this is the best solution and additionally creates good error estimate!!!
p.plot(ao10.prune(number=100),line=0,symbol=[1,0.3,4], legend='2mg/ml prune to 100 points')
p.legend(x=1,y=100,charsize=0.7)
p.xaxis(0,6)
p.yaxis(0.05,200,scale='logarithmic')

# make a smaller plot inside for the structure factor
p.new_graph()
p[1].SetView(0.6,0.4,0.9,0.8)
p[1].plot(ao50.X,ao50.Y/ao10.Y,symbol=[1,0.2,1,''],legend='structure factor')
p[1].yaxis(0.5,1.3)
p[1].xaxis(0,2)
p[1].xlabel('Q / nm\S-1')
p[1].ylabel('S(Q)')
p[1].legend(x=0.5,y=0.8)




Picture about diffusion fit

13.7. How to fit SANS data including the resolution for different detector distances

First this example shows the influence of smearing, then how to do a fit including
smearing a la Pedersen in 2 versions.
import jscatter as js
import numpy as np

# prepare profiles SANS for typical 2m and 8m measurement
# smear calls resFunc with the respective parameters; smear also works with line collimation SAXS if needed
resol2m = js.sas.prepareBeamProfile('SANS', detDist=2000, collDist=2000., wavelength=0.4, wavespread=0.15)
resol8m = js.sas.prepareBeamProfile('SANS', detDist=8000, collDist=8000., wavelength=0.4, wavespread=0.15)

# demonstration smearing effects

# generate some data, or load them from measurement
a,b=2,3
obj=js.dL()
temp=js.ff.ellipsoid(np.r_[0.01:1:0.01],a,b)
temp.Y+=2 # add background
obj.append(js.sas.smear(temp, resol8m))
temp=js.ff.ellipsoid(np.r_[0.5:5:0.05],a,b)
temp.Y+=2
obj.append(js.sas.smear(temp, resol2m))

# here we compare the difference between the 2 profiles using for both the full q range
obj2=js.dL()
temp=js.ff.ellipsoid(np.r_[0.01:5:0.01],a,b)
temp.Y+=2
obj2.append(js.sas.smear(temp, resol8m))
temp=js.ff.ellipsoid(np.r_[0.01:5:0.01],a,b)
temp.Y+=2
obj2.append(js.sas.smear(temp, resol2m))

# plot it
p=js.grace()
ellip=js.ff.ellipsoid(np.r_[0.01:5:0.01],a,b)
ellip.Y+=2
p.plot(ellip,sy=[1,0.3,1],legend='unsmeared ellipsoid')
p.yaxis(label='Intensity / a.u.',scale='l')
p.xaxis(label='Q / nm\S-1',scale='n')
p.plot(obj,legend='smeared $rf_detDist')
p.plot(obj2[0],li=[1,1,4],sy=0,legend='8m smeared full range')
p.plot(obj2[1],li=[3,1,4],sy=0,legend='2m smeared full range')
p.legend(x=2.5,y=8000)
p.title('SANS smearing of ellipsoid')
#p.save('SANSsmearing.jpg')

# now we use the simulated data to fit this to a model

# first possibility
# obj has the information about the used settings in rf_detDist (set in resFunct )
# for experimental data this needs to be added to the loaded data
# another possibility is to use resFunc directly and have the detDist attribute in the data as shown below
def smearedellipsoid(q,A,a,b,rf_detDist,bgr):
    ff=js.ff.ellipsoid(q,a,b)  # calc model
    ff.Y=ff.Y*A+bgr  # multiply amplitude factor and add bgr
    # smear
    if rf_detDist==2000:
        ffs=js.sas.smear(ff, resol2m)
    elif rf_detDist==8000:
        ffs=js.sas.smear(ff, resol8m)
    elif rf_detDist==0:
        # this shows unsmeared model
        ffs=ff
    return ffs

# fit it , here no errors
obj.makeErrPlot(yscale='l',fitlinecolor=[1,2,5])
obj.fit(smearedellipsoid,{'A':1,'a':2.5,'b':3.5,'bgr':0},{},{'q':'X'})
# show the unsmeared model
obj.errPlot(obj.modelValues(rf_detDist=0),li=[3,2,4],sy=0,legend='unsmeared fit')

if 0:
    # second possibility for model: use resFunc directly
    obj[0].detDist=8000   # set detDist for your data
    obj[1].detDist=2000

    def smearedellipsoid2(q,A,a,b,detDist,bgr):
        ff=js.ff.ellipsoid(q,a,b)  # calc model
        ff.Y=ff.Y*A+bgr  # multiply amplitude factor and add bgr
        # smear
        if detDist>0:
            ffs=js.sas.resFunct(ff, detDist=detDist, collDist=detDist, wavelength=0.4, wavespread=0.15)
        elif detDist==0:
            # this shows unsmeared model
            ffs=ff
        return ffs

    # fit it , here no errors
    obj.makeErrPlot(yscale='l',fitlinecolor=5)
    obj.fit(smearedellipsoid2,
            {'A':1,'a':2.5,'b':2.5,'bgr':0},{},{'q':'X'})
Picture about SANS smearing

13.8. Smearing and desmearing of SAX and SANS data

import jscatter as js
import numpy as np

# Here we examine the effect of instrumental smearing for SAX (Kratky Camera, line! ) and SANS
# and how we can use the Lake algorithm for desmearing.

# some data
q=np.r_[0.01:7:0.01]
#obj=js.ff.sphere(q,5)
obj=js.ff.ellipsoid(q,2,3)
# add background
obj.Y+=2
#load data for beam width profile
empty=js.dA(js.examples.datapath+'/buffer_averaged_corrected_despiked.pdh',usecols=[0,1],lines2parameter=[2,3,4])
# beam length profile measurement for a slit (Kratky Camera)
beam = js.dA(js.examples.datapath+'/BeamProfile.pdh', usecols=[0, 1], lines2parameter=[2, 3, 4])

# fit beam width
bwidth=js.sas.getBeamWidth(empty, 'auto')
# prepare measured beamprofile
mbeam = js.sas.prepareBeamProfile(beam, a=2, b=1, bxw=bwidth, dIW=1.)
# prepare profile with trapezoidal shape
tbeam = js.sas.prepareBeamProfile('trapz', a=mbeam.a, b=mbeam.b, bxw=bwidth, dIW=1)
# prepare profile SANS a la Pedersen
Sbeam = js.sas.prepareBeamProfile('SANS', detDist=2000, wavelength=0.4, wavespread=0.15)

if 0:
    p=js.sas.plotBeamProfile(mbeam)
    p=js.sas.plotBeamProfile(mbeam, p)

# smear
datasm= js.sas.smear(obj, mbeam)
datast= js.sas.smear(obj, tbeam)
datasS= js.sas.smear(obj, Sbeam)
# add noise
datasm.Y+=np.random.normal(0,0.5,len(datasm.X))
datast.Y+=np.random.normal(0,0.5,len(datast.X))
datasS.Y+=np.random.normal(0,0.5,len(datasS.X))

# desmear
ws=11
NI=-15
dsm=js.sas.desmear(datasm, mbeam, NIterations=NI, windowsize=ws)
dst=js.sas.desmear(datast, tbeam, NIterations=NI, windowsize=ws)
dsS=js.sas.desmear(datasS, Sbeam, NIterations=NI, windowsize=ws)

p=js.grace(2,1.4)
p.plot(obj,sy=[1,0.3,3],le='original ellipsoid')
p.plot(dst,sy=0,li=[1,2,1],le='desmeared SAX line colimation')
p.plot(dsS,sy=0,li=[1,2,2],le='desmeared SANS')
p.plot(datasm,li=[3,2,1],sy=0,le='smeared SAX line colimation')
p.plot(datasS,li=[3,2,4],sy=0,le='smeared SANS')
p.yaxis(max=1e4,min=0.1,scale='l',label='Intensity / a.u.',size=1.7)
p.xaxis(label='q / nm\S-1',size=1.7)
p.legend(x=3,y=5500,charsize=1.7)
p.title('Smeared ellipsoid and desmearing by Lake algorithm')

# The conclusion is to better fit smeared models than to desmear and fit unsmeared models.
# p.save('SASdesmearing.png')
Picture about smearing/desmearing

13.9. A long example for diffusion and how to analyze step by step

This is a long example to show possibilities.

A main feature of the fit is that we can change from a constant fit parameters to a parameter dependent one by simply changing A to [A].

# -*- coding: utf-8 -*-

from __future__ import print_function
import jscatter as js
import numpy as np

# load example data and show them in a nice plot as
i5=js.dL(js.examples.datapath+'/iqt_1hho.dat')

#make a fixed size plot with the data
p=js.grace(1.5,1)
p.plot(i5,symbol=[-1,0.4,-1], legend='Q=$q')
p.legend(charsize=1.)
p.yaxis(0.01,1.1,scale='log',charsize=1.5)
p.title('Intermediate scattering function',size=2)
p.xlabel('t / ns',1.5)
p.xaxis(charsize=1.5)
p.ylabel('I(Q,t)/I(Q,0)',1.5)
p.xaxis(charsize=1.5)

#definig model to use in fit
# simple diffusion
diffusion=lambda A,D,t,wavevector,elastic=0:A*np.exp(-wavevector**2*D*t)+elastic
#or if you want to include in a library with description
# see examples in formel and formfactor

# in the data we have X as coordinate for time so we have to map the name
# same for the wavevector which is usually 'q' in these data
# the wavevector is available in the data for all i as i5[i].q
# or as a list as i5.q
# so test these

# analyzing the data
# to see the results we open an errorplot with Y-log scale
i5.makeErrPlot(yscale='l')
#'----------------------------------------'
#' a first try model which is bad because of fixed high elastic fraction'
i5.fit(model=diffusion,
       freepar={'D':0.1,'A':1},
       fixpar={'elastic':0.5} ,
       mapNames= {'t':'X','wavevector':'q'})
#'--------------------------------------'
#' Now we try it with constant D and a worse A as starting parameters'
i5.fit(model=diffusion,
       freepar={'D':0.1,'A':18},
       fixpar={'elastic':0.0} ,
       mapNames= {'t':'X','wavevector':'q'})
print(i5.lastfit.D, i5.lastfit.D_err)
print(i5.lastfit.A, i5.lastfit.A_err)
# A is close to 1 (as it should be here) but the fits dont look good
#'--------------------------------------'
#' A free amplitude dependent on wavevector might improve '
i5.fit(model=diffusion,
       freepar={'D':0.1,'A':[1]},
       fixpar={'elastic':0.0} ,
       mapNames= {'t':'X','wavevector':'q'})
#and a second plot to see the results of A
pr=js.grace()
pr.plot(i5.lastfit.wavevector,i5.lastfit.A,i5.lastfit.A_err,legend='A')
# The fit is ok only the chi^2 is to high in this case of simulated data
#'--------------------------------------'
#' now with free diffusion coefficient dependent on wavevector; is this the best solution?'
i5.fit(model=diffusion,
       freepar={'D':[0.1],'A':[1]},
       fixpar={'elastic':0.0} ,
       mapNames= {'t':'X','wavevector':'q'})

pr.clear() #removes the old stuff
pr.plot(i5.lastfit.wavevector,i5.lastfit.D,i5.lastfit.D_err,legend='D')
pr.plot(i5.lastfit.wavevector,i5.lastfit.A,i5.lastfit.A_err,legend='A')
# Ahh
#Now the amplitude is nearly constant and the diffusion is changing
#the fit is ok
#'--------------------------------------'
#' now with changing diffusion and constant amplitude '
i5.fit(model=diffusion,
       freepar={'D':[0.1],'A':1},
       fixpar={'elastic':0.0} ,
       mapNames= {'t':'X','wavevector':'q'})
pr.clear() #removes the old stuff
pr.plot(i5.lastfit.wavevector,i5.lastfit.D,i5.lastfit.D_err,legend='D')

# Booth fits are very good, but the last has less parameter.
# From simulation i know it should be equal to 1 for all amplitudes :-))))).

13.10. Sedimentation of two particle sizes and resulting scattering: a Simulation

# I had the question how long do i need to centrifuge to get rid of
# the larger aggregates and not just guess somewhat.

import numpy as np
import jscatter as js

t1=np.r_[100:2e3:11j]  # time in seconds

# open plot()
p=js.grace(1.5,1.5)
p.SetView(0.15,0.12,0.9,0.85)
# calculate sedimentation profile for two different particles
# data correspond to Fresco 21 with dual rotor
# default is solvent='h2o',temp=293
Rh1=2        # nm
Rh2=40       # nm
g=21000. # g # RZB number
omega=g*246/21000
profiles1=js.formel.sedimentationProfile(t=t1,Rh=Rh1,c0=0.05,omega=omega,rm=48,rb=85)
profiles2=js.formel.sedimentationProfile(t=t1,Rh=Rh2,c0=0.05,omega=omega,rm=48,rb=85)

# plot it
p.plot(profiles1,li=-1,sy=0,legend='%s nm -> t=$time s' %Rh1)
p.plot(profiles2,li=[2,2,-1],sy=0,legend='%s nm-> t=$time s' %Rh2)

# label the plot with some explanations
p.title(r'sedimentation of %s nm species and %s nm species \nafter t seconds centrifugation '%(Rh1,Rh2),size=1)
p.subtitle(r'rotor speed %s rps=%sg, r\smeniscus\N=48mm, r\sbottom\N=85mm'%(omega,g))
p.yaxis(max=0.2,label='concentration')
p.xaxis(label='position in cell / mm')
p.legend(x=40,y=0.2,charsize=0.5)
p.text(r'%s nm particles \nnearly not sedimented \nin sedimentation time of %s nm'%(Rh1,Rh2),44,0.07)
Picture about diffusion fit
p.text(r'%snm sediment\nquite fast'%Rh2,73,0.105)
p[0].line(80,0.1,84,0.08,5,arrow=2)

# corresponding small angle scattering for the above
# centrifugation is done to remove the large fraction
# how long do you need to centrifuge and how does it look without centrifugation?
# scattering for a 2 nm particle and 40 nm particle with same intensity in DLS
# with DLS you can see easily the aggregates

p=js.grace()
# equal intensity  of both species in DLS as in SANS
p.plot(js.ff.scatteringFromSizeDistribution(js.loglist(1e-2,4,100),[[Rh1,Rh2],[1,1]]))
# larger has 10% of smaller species intensity
p.plot(js.ff.scatteringFromSizeDistribution(js.loglist(1e-2,4,100),[[Rh1,Rh2],[1,0.1]]))
# larger species particles
p.plot(js.ff.scatteringFromSizeDistribution(js.loglist(1e-2,4,100),[[Rh1,Rh2],[1,0]]))

p.xaxis(scale='l',label=r'Q / nm\S-1')
p.yaxis(label='I(Q)')
p.title('How does %.2g nm aggregates influence SANS scattering' %Rh2,size=1)
p.subtitle('Beaucage form factor with d=3 for spherical particles')
p.text('Here Rg is determined',x=0.2,y=1)
p.text(r'10%% intensity as\n %.2g nm in DLS'%Rh1,x=0.011,y=1.2)
p.text(r'same intensity as\n %.2g nm in DLS'%Rh1,x=0.02,y=1.9)
p.text('no big aggregates',x=0.011,y=0.9)
_images/bimodalScattering.jpg

13.11. Create a stacked chart of some curves

import numpy as np
import jscatter as js

# create a stacked chart of 10 plots

# create some data
mean=5
x=np.r_[mean-3*3:mean+3*3:200j]
data=js.dL()  # empty dataList
for sigma in np.r_[3:0.3:10j]:
    temp=js.dA(np.c_[x,np.exp(-0.5*(x-mean)**2/sigma**2)/sigma/np.sqrt(2*np.pi)].T)
    temp.sigma=sigma
    data.append(temp)

p=js.grace()
# each shifted by hshift,vshift
# the yaxis is switched off for all except the first
p.stacked(10,hshift=0.02,vshift=0.01,yaxis='off')
# plot some Gaussians in each graph
for i,dat in enumerate(data):
    p[i].plot(dat,li=[1,2,i+1],sy=0,legend='sigma=$sigma')
# choose the same yscale for the data but no ticks for the later plots
# adjusting the scale and the size of the xaxis ticks

p[0].yaxis(min=0,max=1,tick=[0.2,5,0.3,0.1])
p[0].xaxis(min=min(x),max=max(x),tick=[1,1,0.3,0.1])
for pp in p.g[1:]:
    pp.yaxis(min=0,max=1,tick=False)
    pp.xaxis(min=min(x),max=max(x),tick=[1,1,0.3,0.1])

# This plot is shown below; no fine tuning
if 0:
Picture about diffusion fit

13.12. A comparison of different dynamic models in frequency domain

# -*- coding: utf-8 -*-

# Comparison inelastic neutron scattering models
# Compare different kinds of diffusion in restricted geometry by the HWHM from the spectra.

import jscatter as js
import numpy as np

# make a plot of the spectrum
w=np.r_[-100:100:1]
ql=np.r_[1:15:.5]
p=js.grace()
p.title('Inelastic neutron scattering ')
p.subtitle('diffusion in a sphere')
iqw=js.dL([js.dynamic.diffusionInSphere_w(w=w,q=q,D=0.14,R=0.2) for q in ql])
p.plot(iqw)
p.yaxis(scale='l',label='I(q,w) / a.u.')
p.xaxis(scale='n',label='w / ns\S-1')

# Parameters
ql=np.r_[0.5:15.:0.2]
D=0.1;R=0.5                     # diffusion coefficient and radius
w=np.r_[-100:100:0.1]
u0=R/4.33**0.5;t0=R**2/4.33/D   # corresponding values for Gaussian restriction

# In the following we calc the spectra and then extract the FWHM to plot it

# calc spectra
iqwD=js.dL([js.dynamic.transDiff_w(w=w,q=q,D=D) for q in ql[5:]])
iqwDD=js.dL([js.dynamic.time2frequencyFF(js.dynamic.simpleDiffusion,'elastic',w=w,q=q, D=D) for q in ql])
iqwS=js.dL([js.dynamic.diffusionInSphere_w(w=w,q=q,D=D,R=R) for q in ql])
iqwG3=js.dL([js.dynamic.diffusionHarmonicPotential_w(w=w, q=q, rmsd=u0, tau=t0,ndim=3) for q in ql])
iqwG2=js.dL([js.dynamic.diffusionHarmonicPotential_w(w=w, q=q, rmsd=u0, tau=t0,ndim=2) for q in ql])
iqwG11=js.dL([js.dynamic.t2fFF(js.dynamic.diffusionHarmonicPotential,'elastic',w=np.r_[-100:100:0.01],q=q, rmsd=u0, tau=t0 ,ndim=1) for q in ql])
iqwG22=js.dL([js.dynamic.t2fFF(js.dynamic.diffusionHarmonicPotential,'elastic',w=np.r_[-100:100:0.01],q=q, rmsd=u0, tau=t0,ndim=2 ) for q in ql])
iqwG33=js.dL([js.dynamic.t2fFF(js.dynamic.diffusionHarmonicPotential,'elastic',w=np.r_[-100:100:0.01],q=q, rmsd=u0, tau=t0,ndim=3 ) for q in ql])
#iqwCH3=js.dL([js.dynamic.t2fFF(js.dynamic.methylRotation,'elastic',w=np.r_[-100:100:0.1],dw=0,q=q ) for q in ql])

# plot HWHM  in a scaled plot
p1=js.grace(1.5,1.5)
p1.title('Inelastic neutron scattering models')
p1.subtitle('Comparison of HWHM for different types of diffusion')
p1.plot([0.1,60],[4.33296]*2,li=[1,1,1])
p1.plot((R*iqwD.wavevector.array)**2,[js.dynamic.getHWHM(dat)[0]/(D/R**2) for dat in iqwD], le='free diffusion')
p1.plot((R*iqwS.wavevector.array)**2,[js.dynamic.getHWHM(dat)[0]/(D/R**2) for dat in iqwS], le='in sphere')
p1.plot((R*iqwG3.wavevector.array)**2,[js.dynamic.getHWHM(dat)[0]/(D/R**2) for dat in iqwG3], le='3D Gauss')
p1.plot((R*iqwG2.wavevector.array)**2,[js.dynamic.getHWHM(dat)[0]/(D/R**2) for dat in iqwG2], le='2D Gauss')
p1.plot((R*iqwDD.wavevector.array)**2,[js.dynamic.getHWHM(dat)[0]/(D/R**2) for dat in iqwDD],sy=0,li=[1,2,1], le='free ')
p1.plot((R*iqwG11.wavevector.array)**2,[js.dynamic.getHWHM(dat,gap=0.04)[0]/(D/R**2) for dat in iqwG11],sy=0,li=[1,2,2], le='1D Gauss t2fFF')
p1.plot((R*iqwG22.wavevector.array)**2,[js.dynamic.getHWHM(dat,gap=0.04)[0]/(D/R**2) for dat in iqwG22],sy=0,li=[1,2,3], le='2D Gauss t2fFF')
p1.plot((R*iqwG33.wavevector.array)**2,[js.dynamic.getHWHM(dat,gap=0.04)[0]/(D/R**2) for dat in iqwG33],sy=0,li=[1,2,4], le='3D Gauss t2fFF')
# 1DGauss as given in the reference (see help for this function) seems to have a mistake
# Use the t2fFF from time domain
#p1.plot((0.12*iqwCH3.wavevector.array)**2,[js.dynamic.getHWHM(dat,gap=0.04)[0]/(D/0.12**2) for dat in iqwCH3],sy=0,li=[1,2,4], le='1D Gauss')


# jump diffusion
r0=.5;t0=r0**2/2/D
w=np.r_[-100:100:0.02]
iqwJ=js.dL([js.dynamic.jumpDiff_w(w=w,q=q,r0=r0,t0=t0) for q in ql])
ii=54;p1.plot((r0*iqwJ.wavevector.array[:ii])**2,[js.dynamic.getHWHM(dat)[0]/(D/r0**2) for dat in iqwJ[:ii]], le='jump diffusion')
p1.plot((R*iqwG33.wavevector.array)**2,(R*iqwG33.wavevector.array)**2,sy=0,li=[3,2,1], le='free (infinite q resolution)')
p1.yaxis(min=0.1,max=100,scale='l',label='HWHM/(D/R**2)')
Picture about diffusion fit

13.13. Protein incoherent scattering in frequency domain

# -*- coding: utf-8 -*-
# A model for protein incoherent scattering
# neglecting coherent and maybe some other contributions

import jscatter as js
import numpy as np
import urllib

# get pdb structure file with 3rn3 atom coordinates and filter for CA positions in A
# this results in C-alpha representation of protein Ribonuclease A.
url='https://files.rcsb.org/download/3RN3.pdb'
try:
    pdbtxt=urllib.urlopen(url).read()
except:
    # python3
    pdbtxt=urllib.request.urlopen(url).read().decode('utf8')
CA=[line[31:55].split() for line in pdbtxt.split('\n') if line[13:16].rstrip()=='CA' and line[:4]=='ATOM']
# conversion to nm and center of mass
ca=np.array(CA,float)/10.
cloud=ca-ca.mean(axis=0)
Dtrans=0.086  # nm**2/ns  for 3rn3 in D2O at 20°C
Drot  =0.0163 # 1/ns

# A list of wavevectors and frequencies and a resolution.
# If bgr is zero we have to add it later after the convolution with resolution as constant instrument background.
# As resolution a single q independent (unrealistic) Gaussian is used.
ql = np.r_[0.1,0.5, 1, 2:15.:2,20]
w = np.r_[-100:100:0.1]
start={'s0':0.5,'m0':0,'a0':1,'bgr':0.00}
resolution=lambda w:js.dynamic.resolution_w(w=w, **start)
res=resolution(w)

# translational diffusion
p=js.grace(3,0.75)
p.multi(1,4)
p[0].title(' '*50+'Inelastic neutron scattering for protein Ribonuclease A',size=2)
iqwRtr=js.dL([js.dynamic.convolve(js.dynamic.transDiff_w(w,q=q,D=Dtrans), res,normB=True) for q in ql])
p[0].plot(iqwRtr,le='$wavevector')
iqwRt=js.dL([js.dynamic.transDiff_w(w,q=q,D=Dtrans) for q in ql])
p[0].plot(iqwRt,li=1,sy=0)
p[0].yaxis(min=1e-8,max=1e3,scale='l',ticklabel=['power',0,1],label=[r'I(Q,\xw\f{})',1.5])
p[0].xaxis(min=-149,max=99,label=r'\xw\f{} / 1/ns',charsize=1)
p[0].legend(charsize=1,x=-140,y=1)
p[0].text(r'translational diffusion',y=80,x=-130,charsize=1.5)

# rotational diffusion
iqwRrr=js.dL([js.dynamic.convolve(js.dynamic.rotDiffusion_w(w,q=q,cloud=cloud,Dr=Drot), res,normB=True) for q in ql])
p[1].plot(iqwRrr,le='$wavevector')
iqwRr=js.dL([js.dynamic.rotDiffusion_w(w,q=q,cloud=cloud,Dr=Drot) for q in ql])
p[1].plot(iqwRr,li=1,sy=0)
p[1].yaxis(min=1e-8,max=1e3,scale='l',ticklabel=['power',0,1,'normal'])
p[1].xaxis(min=-149,max=99,label=r'\xw\f{} / 1/ns',charsize=1)
#p[1].legend()
p[1].text(r'rotational diffusion',y=50,x=-130,charsize=1.5)

# restricted diffusion
iqwRgr=js.dL([js.dynamic.convolve(js.dynamic.diffusionHarmonicPotential_w(w, q=q, tau=0.15, rmsd=0.3), res, normB=True) for q in ql])
p[2].plot(iqwRgr,le='$wavevector')
iqwRg=js.dL([js.dynamic.diffusionHarmonicPotential_w(w, q=q, tau=0.15, rmsd=0.3) for q in ql])
p[2].plot(iqwRg,li=1,sy=0)
p[2].yaxis(min=1e-8,max=1e3,scale='l',ticklabel=['power',0,1],label='')
p[2].xaxis(min=-149,max=99,label=r'\xw\f{} / 1/ns',charsize=1)
#p[2].legend()
p[2].text(r'restricted diffusion \n(harmonic)',y=50,x=-130,charsize=1.5)

# amplitudes at w=0 and w=10
p[3].title('amplitudes w=[0, 10]')
p[3].subtitle(r'\xw\f{}>10 restricted diffusion > translational diffusion')
ww=10
wi=np.abs(w-ww).argmin()
p[3].plot(iqwRtr.wavevector,iqwRtr.Y.array.max(axis=1),sy=[1,0.3,1],li=[1,2,1],le='trans + res')
p[3].plot(iqwRt.wavevector,iqwRt.Y.array.max(axis=1)  ,sy=[1,0.3,1],li=[2,2,1],le='trans ')
p[3].plot(iqwRt.wavevector,iqwRt.Y.array[:,wi]  ,sy=[2,0.3,1],li=[3,3,1],le='trans w=%.2g' %ww)
p[3].plot(iqwRrr.wavevector,iqwRrr.Y.array.max(axis=1),sy=[1,0.3,2],li=[1,2,2],le='rot + res')
p[3].plot(iqwRr.wavevector,iqwRr.Y.array.max(axis=1)  ,sy=[1,0.3,2],li=[2,2,2],le='rot')
p[3].plot(iqwRr.wavevector,iqwRr.Y.array[:,wi]  ,sy=[2,0.3,2],li=[3,3,2],le='rot w=%.2g' %ww)
p[3].plot(iqwRgr.wavevector,iqwRgr.Y.array.max(axis=1),sy=[1,0.3,3],li=[1,2,3],le='rest + res')
p[3].plot(iqwRg.wavevector,iqwRg.Y.array.max(axis=1)  ,sy=[8,0.3,1],li=[2,2,3],le='rest')
p[3].plot(iqwRg.wavevector,iqwRg.Y.array[:,wi]  ,sy=[3,0.3,3],li=[3,3,3],le='rest w=%.2g' %ww)

p[3].yaxis(min=1e-6,max=1e3,scale='l',ticklabel=['power',0,1,'opposite'],label=[r'I(Q,\xw\f{}=[0,10])',1,'opposite'])
p[3].xaxis(min=0.1,max=50,scale='l',label='Q / nm\S-1',charsize=1)
p[3].legend(charsize=1.,x=5,y=1e-3)

# p.save('inelasticNeutronScattering.png',size=(2400,600),dpi=200)

# all together in a combined model  ----------------------------
conv=js.dynamic.convolve
start={'s0':0.5,'m0':0,'a0':1,'bgr':0.00}
resolution=lambda w:js.dynamic.resolution_w(w=w, **start)
def transrotsurfModel(w,q,Dt,Dr,exR,tau,rmsd):
    """
    A model for trans/rot diffusion with a partial local restricted diffusion at the protein surface.

    See Fast internal dynamics in alcohol dehydrogenase The Journal of Chemical Physics 143, 075101 (2015);
    https://doi.org/10.1063/1.4928512

    Parameters
    ----------
    w   frequncies
    q   wavevector
    Dt  tranlational diffusion
    Dr  rotational diffusion
    exR outside this radius additional restricted diffusion with t0 u0
    tau  correlation time
    rmsd  Root mean square displacement Ds=u0**2/t0

    Returns
    -------

    """
    natoms=cloud.shape[0]
    trans=js.dynamic.transDiff_w(w, q, Dt)
    rot=js.dynamic.rotDiffusion_w(w,q,cloud,Dr)
    fsurf=((cloud**2).sum(axis=1)**0.5>exR).sum()*1./natoms # fraction close to surface
    loc=js.dynamic.diffusionHarmonicPotential_w(w, q, tau, rmsd)
    # only a fraction at surface contributes to local restricted diffusion
    loc.Y=js.dynamic.elastic_w(w).Y*(1-fsurf)+fsurf*loc.Y
    final=conv(trans,rot)
    final=conv(final, loc)
    final.setattr(rot,'rot_')
    final.setattr(loc,'loc_')
    res=resolution(w)
    finalres=conv(final, res,normB=True)
    #finalres.Y+=0.0073
    finalres.q=q
    finalres.fsurf=fsurf
    return finalres

ql = np.r_[0.1,0.5, 1, 2:15.:4,20]
p=js.grace(1,1)
p.title('Protein incoherent scattering')
p.subtitle('Ribonuclease A')
iqwR=js.dL([transrotsurfModel(w, q=q, Dt=Dtrans, Dr=Drot, exR=0, tau=0.15, rmsd=0.3) for q in ql])
p.plot(iqwR[0],sy=0,li=[1,2,1],le='exR=0')
p.plot(iqwR[1:],sy=0,li=[1,2,1])
iqwR=js.dL([transrotsurfModel(w, q=q, Dt=Dtrans, Dr=Drot, exR=1, tau=0.15, rmsd=0.3) for q in ql])
p.plot(iqwR[0],sy=0,li=[1,2,2],le='exR=1')
p.plot(iqwR[1:],sy=0,li=[1,2,2])
iqwR=js.dL([transrotsurfModel(w, q=q, Dt=Dtrans, Dr=Drot, exR=1.4, tau=0.15, rmsd=0.3) for q in ql])
p.plot(iqwR[0],sy=0,li=[3,3,3],le='exR=1.4')
p.plot(iqwR[1:],sy=0,li=[3,3,3])
p.yaxis(min=1e-4,max=10,label=r'I(q,\xw\f{})',scale='l',ticklabel=['power',0,1])
p.xaxis(min=0.1,max=100,label=r'\xw\f{} / ns\S-1',scale='l')
p.legend(x=0.5,y=0.001)
p[0].line(1,5e-2,1,5e-3,2,arrow=1)
p.text('resolution',x=0.9,y=7e-3,rot=90)
p.text('q=0.1 nm\S-1',x=0.2,y=6,rot=0)
p.text('q=20 nm\S-1',x=0.2,y=0.04,rot=0)
p[0].line(0.17,6,0.17,0.04,2,arrow=2)
p.text(r'Variation \nfixed/harmonic protons',x=0.5,y=2e-3,rot=0)
p[0].line(1.4,2.3e-3,3,2.3e-3,arrow=2)

#p.save('Ribonuclease_inelasticNeutronScattering.png',size=(1200,1200),dpi=150)

Picture about diffusion fit Picture about diffusion fit

13.14. Fitting a multiShellcylinder in various ways

# -*- coding: utf-8 -*-
import jscatter as js
import numpy as np
# Can we trust a fit?

# simulate some data with noise
# in reality read some data
x=js.loglist(0.1,7,1000)
R1=2
R2=2
L=20
# this is a three shell cylinder with the outer as a kind of "hydration layer"
simcyl=js.ff.multiShellCylinder(x,L,[R1,R2,0.5],[4e-4,2e-4,6.5e-4],solventSLD=6e-4)

p=js.grace()
p.plot(simcyl,li=1,sy=0)
# noinspection PyArgumentList
simcyl.Y+=np.random.randn(len(simcyl.Y))*simcyl.Y[simcyl.X>4].mean()
simcyl=simcyl.addColumn(1,simcyl.Y[simcyl.X>4].mean())
simcyl.setColumnIndex(iey=2)
p.plot(simcyl,li=0,sy=1)
p.yaxis(min=2e-7,max=0.1,scale='l')

# create a model to fit
# We use the model of a double cylinder with background (The intention is to use a wrong but close model).

def dcylinder(q,L,R1,R2,b1,b2,bgr):
    #assume D2O for the solvent
    result=js.ff.multiShellCylinder(q,L,[R1,R2],[b1*1e-4,b2*1e-4],solventSLD=6.335e-4)
    result.Y+=bgr
    return result

simcyl.makeErrPlot(yscale='l')
simcyl.fit(dcylinder,
           freepar={'L':20,'R1':1,'R2':2,'b1':2,'b2':3},
           fixpar={'bgr':0},
           mapNames={'q':'X'})


# There are still systematic deviations in the residuals due to the missing layer
# but the result is very promising
# So can we trust such a fit :-)
# The outer 0.5 nm layer modifies the layer thicknesses and scattering length density.
# Here prior knowledge about the system might help.

def dcylinder3(q,L,R1,R2,R3,b1,b2,b3,bgr):
    #assume D2O for the solvent
    result=js.ff.multiShellCylinder(q,L,[R1,R2,R3],[b1*1e-4,b2*1e-4,b3*1e-4],solventSLD=6.335e-4)
    result.Y+=bgr
    return result

simcyl.makeErrPlot(yscale='l')
try:
    # The fit will need quite long and fails as it runs in a wrong direction.
    simcyl.fit(dcylinder3,
           freepar={'L':20,'R1':1,'R2':2,'R3':2,'b1':2,'b2':3,'b3':0},
           fixpar={'bgr':0},
           mapNames={'q':'X'},maxfev=3000)
except:
    # this try : except is only to make the script runas it is
    pass


# Try the fit with a better guess for the starting parameters.
# Prior knowledge by a good idea what is fitted helps to get a good result and
# prevents from running in a wrong minimum of the fit.

simcyl.fit(dcylinder3,
           freepar={'L':20,'R1':2,'R2':2,'R3':0.5,'b1':4,'b2':2,'b3':6.5},
           fixpar={'bgr':0},ftol=1e-5,
           mapNames={'q':'X'},condition =lambda a:a.X<4)

# Finally look at the errors.

13.15. Hydrodynamic function

import numpy as np
import jscatter as js

# Hydrodynamic function and structure factor of hard spheres
# The hydrodynamic function describes how diffusion of spheres in concentrated suspension
# is changed due to hydrodynamic interactions.
# The diffusion is changed according to D(Q)=D0*H(Q)/S(Q)
# with the hydrodynamic function H(Q), structure factor S(Q)
# and Einstein diffusion of sphere D0

# some  wavevectors
q=np.r_[0:5:0.03]

p=js.grace(2,1.5)
p.multi(1,2)
p[0].title('Hydrodynamic function H(Q)')
p[0].subtitle('concentration dependence')
Rh=2.2
for ii,mol in enumerate(np.r_[0.1:20:5]):
    H = js.sf.hydrodynamicFunct(q, Rh,molarity=0.001*mol, )
    p[0].plot(H,sy=[1,0.3,ii+1],legend='H(Q) c=%.2g mM'%mol)
    p[0].plot(H.X,H[3],sy=0,li=[1,2,ii+1],legend='structure factor')

p[0].legend(x=2,y=2.4)
p[0].yaxis(min=0.,max=2.5,label='S(Q); H(Q)')
p[0].xaxis(min=0.,max=5.,label='Q / nm\S-1')

# hydrodynamic function and structure factor of charged spheres
p[1].title('Hydrodynamic function H(Q)')
p[1].subtitle('screening length dependence')
for ii,scl in enumerate(np.r_[0.1:30:6]):
    H = js.sf.hydrodynamicFunct(q, Rh,molarity=0.0005,structureFactor=js.sf.RMSA,structureFactorArgs={'R':Rh*2, 'scl':scl, 'gamma':5}, )
    p[1].plot(H,sy=[1,0.3,ii+1],legend='H(Q) scl=%.2g nm'%scl)
    p[1].plot(H.X,H[3],sy=0,li=[1,2,ii+1],legend='structure factor')

p[1].legend(x=2,y=2.4)
p[1].yaxis(min=0.,max=2.5,label='S(Q); H(Q)')
p[1].xaxis(min=0.,max=5.,label='Q / nm\S-1')

p[0].text(r'high Q shows \nreduction in D\sself',x=3,y=0.22)
p[1].text(r'low Q shows reduction \ndue to stronger interaction',x=0.5,y=0.25)

Picture HydrodynamicFunction

13.16. 2D oriented scattering

# coding=utf-8
import jscatter as js
import numpy as np

# Examples for scattering of 2D scattering of some spheres oriented in space relative to incoming beam
# incoming beam along Y
# detector plane X,Z
# For latter possibility to fit 2D data we have Y=f(X,Z)

# two points
rod0 = np.zeros([2, 3])
rod0[:, 1] = np.r_[0, np.pi]
qxz = np.mgrid[-6:6:50j, -6:6:50j].reshape(2, -1).T
ffe = js.ff.orientedCloudScattering(qxz, rod0, coneangle=10, nCone=10, rms=0)
fig = js.mpl.surface(ffe.X, ffe.Z, ffe.Y)
fig.axes[0].set_title('cos**2 for Z and slow decay for X')
fig.show()
# noise in positions
ffe = js.ff.orientedCloudScattering(qxz, rod0, coneangle=10, nCone=100, rms=0.1)
fig = js.mpl.surface(ffe.X, ffe.Z, ffe.Y)
fig.axes[0].set_title('cos**2 for Y and slow decay for X with position noise')
fig.show()
#
# two points along z result in symmetric pattern around zero
# asymetry is due to small nCone and reflects the used Fibonacci lattice
rod0 = np.zeros([2, 3])
rod0[:, 2] = np.r_[0, np.pi]
ffe = js.ff.orientedCloudScattering(qxz, rod0, coneangle=45, nCone=10, rms=0.05)
fig2 = js.mpl.surface(ffe.X, ffe.Z, ffe.Y)
fig2.axes[0].set_title('symmetric around zero')
fig2.show()
#
# 5 spheres in line with small position distortion
rod0 = np.zeros([5, 3])
rod0[:, 1] = np.r_[0, 1, 2, 3, 4] * 3
qxz = np.mgrid[-6:6:50j, -6:6:50j].reshape(2, -1).T
ffe = js.ff.orientedCloudScattering(qxz, rod0, formfactor='sphere', V=4/3.*np.pi*2**3, coneangle=20, nCone=30, rms=0.02)
fig4 = js.mpl.surface(ffe.X, ffe.Z, np.log10(ffe.Y), colorMap='gnuplot')
fig4.axes[0].set_title('5 spheres with R=2 along Z with noise (rms=0.02)')
fig4.show()
#
# 5 core shell particles in line with small position distortion (Gaussian)
rod0 = np.zeros([5, 3])
rod0[:, 1] = np.r_[0, 1, 2, 3, 4] * 3
qxz = np.mgrid[-6:6:50j, -6:6:50j].reshape(2, -1).T
# only as demo : extract q from qxz
qxzy = np.c_[qxz, np.zeros_like(qxz[:, 0])]
qrpt = js.formel.xyz2rphitheta(qxzy)
q = np.unique(sorted(qrpt[:, 0]))
# or use interpolation
q = js.loglist(0.01, 7, 100)
# explicitly given isotropic form factor
cs = js.ff.sphereCoreShell(q=q, Rc=1, Rs=2, bc=0.1, bs=1, solventSLD=0)
ffe = js.ff.orientedCloudScattering(qxz, rod0, formfactor=cs, coneangle=20, nCone=100, rms=0.05)
fig4 = js.mpl.surface(ffe.X, ffe.Z, np.log10(ffe.Y), colorMap='gnuplot')
fig4.axes[0].set_title('5 core shell particles with R=2 along Z with noise (rms=0.05)')
fig4.show()

# Extracting 1D data
# 1. average angular region (similar to experimental detector data)
# 2. direct calculation
# Here with higher resolution to see the additional peaks due to allignement.
#
# 1:
rod0 = np.zeros([5, 3])
rod0[:, 1] = np.r_[0, 1, 2, 3, 4] * 3
qxz = np.mgrid[-4:4:150j, -4:4:150j].reshape(2, -1).T
# only as demo : extract q from qxz
qxzy = np.c_[qxz, np.zeros_like(qxz[:, 0])]
qrpt = js.formel.xyz2rphitheta(qxzy)
q = np.unique(sorted(qrpt[:, 0]))
# or use interpolation
q = js.loglist(0.01, 7, 100)
cs = js.ff.sphereCoreShell(q=q, Rc=1, Rs=2, bc=0.1, bs=1, solventSLD=0)
ffe = js.ff.orientedCloudScattering(qxz, rod0, formfactor=cs, coneangle=20, nCone=100, rms=0.05)
fig4 = js.mpl.surface(ffe.X, ffe.Z, np.log10(ffe.Y), colorMap='gnuplot')
fig4.axes[0].set_title('5 core shell particles with R=2 along Z with noise (rms=0.05)')
fig4.show()
#
# transform X,Z to spherical coordinates
qphi=js.formel.xyz2rphitheta([ffe.X,ffe.Z,abs(ffe.X*0)],transpose=True )[:,:2]
# add qphi or use later rp[1] for selection
ffb=ffe.addColumn(2,qphi.T)
# select a portion of the phi angles
phi=np.pi/2
dphi=0.2
ffn=ffb[:,(ffb[-1]<phi+dphi)&(ffb[-1]>phi-dphi)]
ffn.isort(-2)    # sort along radial q
p=js.grace()
p.plot(ffn[-2],ffn.Y,le='oriented spheres form factor')
# compare to coreshell formfactor scaled
p.plot(cs.X,cs.Y/cs.Y[0]*25,li=1,le='coreshell form factor')
p.yaxis(label='F(Q,phi=90°+-11°)', scale='log')
p.title('5 alligned core shell particle with additional interferences',size=1.)
p.subtitle(' due to sphere allignement dependent on observation angle')

# 2: direct way with 2D q in xz plane
rod0 = np.zeros([5, 3])
rod0[:, 1] = np.r_[0, 1, 2, 3, 4] * 3
x=np.r_[0.0:6:0.05]
qxzy = np.c_[x, x*0,x*0]
for alpha in np.r_[0:91:30]:
    R=js.formel.rotationMatrix(np.r_[0,0,1],np.deg2rad(alpha)) # rotate around Z axis
    qa=np.dot(R,qxzy.T).T[:,:2]
    ffe = js.ff.orientedCloudScattering(qa, rod0, formfactor=cs, coneangle=20, nCone=100, rms=0.05)
    p.plot(x,ffe.Y,li=[1,2,-1],sy=0,le='alpha=%g' %alpha)
p.xaxis(label=r'Q / nm\S-1')
p.legend()

2D scattering coreshell 2D scattering

13.17. Multilamellar Vesicles

# coding=utf-8
import jscatter as js
import numpy as np

Q=js.loglist(0.001,5,500)#np.r_[0.01:5:0.01]

ffmV=js.ff.multilamellarVesicles
save=0

# correlation peak sharpness depends on disorder
dR=20
nG=200
p=js.grace(1,1)
for dd in [0.1,6,10]:
    p.plot(ffmV(Q=Q, R=100, displace=dd, dR=dR,N=10,dN=0, phi=0.2,shellthickness=0, SLD=1e-4,nGauss=nG), le='displace= %g ' % dd)

p.legend(x=0.3,y=10)
p.title('Scattering of multilamellar vesicle')
p.subtitle('shellnumber N=10, shellthickness 0 nm, dR=20, R=100')
p.yaxis(label='S(Q)',scale='l',min=1e-7,max=1e2,ticklabel=['power',0])
p.xaxis(label='Q / nm\S-1',scale='l',min=1e-3,max=5,ticklabel=['power',0])
p.text('Guinier range',x=0.005,y=10)
p.text(r'Correlation peaks\nsharpness decreases with disorder',x=0.02,y=0.00001)
if save:p.save('multilamellar1.png')

# Correlation peak position depends on average layer distance
dd=0
dR=20
nG=200
p=js.grace(1,1)
for N in [1,3,10,30,100]:
    p.plot(ffmV(Q=Q, R=100, displace=dd, dR=dR,N=N,dN=0, phi=0.2,shellthickness=0, SLD=1e-4,nGauss=nG), le='N= %g ' % N)

p.legend(x=0.3,y=10)
p.title('Scattering of multilamellar vesicle')
p.subtitle('shellnumber N, shellthickness 0 nm, dR=20, R=100')
p.yaxis(label='S(Q)',scale='l',min=1e-7,max=1e2,ticklabel=['power',0])
p.xaxis(label='Q / nm\S-1',scale='l',min=1e-3,max=5,ticklabel=['power',0])

p.text('Guinier range',x=0.005,y=40)
p.text(r'Correlation peaks\n at 2\xp\f{}N/R',x=0.2,y=0.01)
if save:p.save('multilamellar2.png')

# including the shell formfactor with shape fluctuations
dd=2
dR=20
nG=200
p=js.grace(1,1)
for i,ds in enumerate([0.001,0.1,0.6,1.2],1):
    mV=ffmV(Q=Q, R=100, displace=dd, dR=dR,N=10,dN=0, phi=0.2,shellthickness=6,ds=ds, SLD=1e-4,nGauss=nG)
    p.plot(mV, sy=[i,0.3,i], le='ds= %g ' % ds)
    p.plot(mV.X,mV[-1]*1000,sy=0,li=[3,3,i])
    p.plot(mV.X,mV[-2]*100,sy=0,li=[2,3,i])

p.legend(x=0.003,y=1)
p.title('Scattering of multilamellar vesicle')
p.subtitle('shellnumber N=10, shellthickness 6 nm, dR=20, R=100')
p.yaxis(label='S(Q)',scale='l',min=1e-10,max=1e6,ticklabel=['power',0])
p.xaxis(label='Q / nm\S-1',scale='l',min=1e-3,max=5,ticklabel=['power',0])

p.text('Guinier range',x=0.005,y=40000)
p.text(r'Correlation peaks\n at 2\xp\f{}N/R',x=0.2,y=10)
p.text('Shell form factor',x=0.1,y=1e-4)
p[0].line(0.2,4e-5,0.8,4e-5,2,arrow=2)
p.text(r'Shell structure factor\n x100',x=0.002,y=5e1)
p.text('Shell form factor x1000',x=0.01,y=1e-6)
if save:p.save('multilamellar3.png')


# Comparing multilamellar and unilamellar vesicle
dd=2
dR=5
nG=100
ds=0.2
p=js.grace(1,1)
for i,R in enumerate([40,50,60],1):
    mV=ffmV(Q=Q, R=R, displace=dd, dR=dR,N=4,dN=0, phi=0.2,shellthickness=6,ds=ds, SLD=1e-4,nGauss=nG)
    p.plot(mV, sy=[i,0.3,i], le='R= %g ' % R)
    p.plot(mV.X,mV[-1]*100,sy=0,li=[3,3,i])
    p.plot(mV.X,mV[-2]*0.01,sy=0,li=[2,3,i])


# comparison double sphere
mV=ffmV(Q=Q, R=50., displace=0, dR=5,N=1,dN=0, phi=1,shellthickness=6,ds=ds, SLD=1e-4,nGauss=100)
p.plot(mV,sy=0,li=[1,2,4],le='unilamellar R=50 nm')
mV=ffmV(Q=Q, R=60., displace=0, dR=5,N=1,dN=0, phi=1,shellthickness=6,ds=ds, SLD=1e-4,nGauss=100)
p.plot(mV,sy=0,li=[3,2,4],le='unilamellar R=60 nm')


p.legend(x=0.3,y=1e5)
p.title('Comparing multilamellar and unilamellar vesicle')
p.subtitle('shellnumber N=4, shellthickness 6 nm, dR=5, ds=0.2')
p.yaxis(label='S(Q)',scale='l',min=1e-10,max=1e6,ticklabel=['power',0])
p.xaxis(label='Q / nm\S-1',scale='l',min=1e-2,max=5,ticklabel=['power',0])

p.text('Guinier range',x=0.05,y=40000)
p.text(r'Correlation peaks\n at 2\xp\f{}N/R',x=0.2,y=10)
p.text('Shell form factor',x=0.2,y=1e-4)
p[0].line(0.2,4e-5,0.8,4e-5,2,arrow=2)
p.text(r'Shell structure factor\n x0.01',x=0.011,y=0.1)
p.text('Shell form factor x100',x=0.02,y=1e-6)
if save:p.save('multilamellar4.png')


# Lipid bilayer in SAXS/SANS
# Values for layer thickness can be found in
# Structure of lipid bilayers
# John F. Nagle et al Biochim Biophys Acta. 1469, 159–195. (2000)
Q=js.loglist(0.01,5,500)
dd=1.5
dR=5
nG=100
ds=0
R=50
N=5
st=[3.5,(6.5-3.5)/2]
p=js.grace(1,1)
p.title('Lipid bilayer in SAXS/SANS')
# SAXS
saxm=ffmV(Q=Q, R=R, displace=dd, dR=dR,N=N,dN=0, phi=0.2,shellthickness=st,ds=ds, SLD=[0.6e-3,0.07e-3],solventSLD=0.94e-3,nGauss=nG)
p.plot(saxm,sy=[1,0.3,1],le='SAXS multilamellar')
saxu=ffmV(Q=Q, R=R, displace=0, dR=dR,N=1,dN=0, phi=0.2,shellthickness=st,ds=ds,SLD=[0.6e-3,0.07e-3],solventSLD=0.94e-3,nGauss=100)
p.plot(saxu,sy=0,li=[3,2,1],le='SAXS unilamellar')
# SANS
sanm=ffmV(Q=Q, R=R, displace=dd, dR=dR,N=N,dN=0, phi=0.2,shellthickness=st,ds=ds, SLD=[1.5e-4,0.3e-4],solventSLD=6.335e-4,nGauss=nG)
p.plot( sanm,sy=[1,0.3,2],le='SANS multilamellar')
sanu=ffmV(Q=Q, R=R, displace=0, dR=dR,N=1,dN=0, phi=0.2,shellthickness=st,ds=ds,SLD=[1.5e-4,0.3e-4],solventSLD=6.335e-4,nGauss=100)
p.plot(sanu,sy=0,li=[3,2,2],le='SANS unilamellar')


p.legend(x=0.015,y=1e-1)
p.subtitle('R=50 nm, N=5, shellthickness=[1.5,3.5,1.5] nm, dR=5, ds=0.')
p.yaxis(label='S(Q)',scale='l',min=1e-6,max=1e4,ticklabel=['power',0])
p.xaxis(label='Q / nm\S-1',scale='l',min=1e-2,max=5,ticklabel=['power',0])

p.text('Guinier range',x=0.03,y=3000)
p.text(r'Correlation peak\n at 2\xp\f{}N/R\n vanishes for SAXS',x=0.5,y=4)
p.text('Shell form factor',x=0.1,y=1e-4)
if save:p.save('multilamellar5.png')

# Example for a liposome doubleLayer for SAX and SANS with ds>0
dd=1.5
dR=5
nG=100
ds=0.01
R=50
sd=[3,1]

p=js.grace()
p.title('Multilamellar/unilamellar vesicle for SAXS/SANS')
# SAXS
saxm=ffmV(Q=Q, R=R, displace=dd, dR=dR,N=4,dN=0, phi=0.2,shellthickness=sd,ds=ds, SLD=[0.6e-3,0.07e-3],solventSLD=0.94e-3,nGauss=nG)
p.plot(saxm,sy=0,li=[1,1,1],le='SAXS multilamellar')
saxu=ffmV(Q=Q, R=R, displace=0, dR=dR,N=1,dN=0, phi=0.2,shellthickness=sd,ds=ds,SLD=[0.6e-3,0.07e-3],solventSLD=0.94e-3,nGauss=100)
p.plot(saxu,sy=0,li=[3,2,1],le='SAXS unilamellar')
saxu=ffmV(Q=Q, R=R, displace=0, dR=dR,N=1,dN=0, phi=0.2,shellthickness=sd,ds=0,SLD=[0.6e-3,0.07e-3],solventSLD=0.94e-3,nGauss=100)
p.plot(saxu,sy=0,li=[2,0.3,1],le='SAXS unilamellar ds=0')

# SANS
sanm=ffmV(Q=Q, R=R, displace=dd, dR=dR,N=4,dN=0, phi=0.2,shellthickness=sd,ds=ds, SLD=[1.5e-4,0.3e-4],solventSLD=6.335e-4,nGauss=nG)
p.plot( sanm,sy=0,li=[1,1,2],le='SANS multilamellar')
sanu=ffmV(Q=Q, R=R, displace=0, dR=dR,N=1,dN=0, phi=0.2,shellthickness=sd,ds=ds,SLD=[1.5e-4,0.3e-4],solventSLD=6.335e-4,nGauss=100)
p.plot(sanu,sy=0,li=[3,2,2],le='SANS unilamellar')
sanu=ffmV(Q=Q, R=R, displace=0, dR=dR,N=1,dN=0, phi=0.2,shellthickness=sd,ds=0,SLD=[1.5e-4,0.3e-4],solventSLD=6.335e-4,nGauss=100)
p.plot(sanu,sy=0,li=[2,0.3,2],le='SANS unilamellar ds=0')

p.legend(x=0.013,y=3e-1)
p.title('Comparing multilamellar and unilamellar vesicle')
p.subtitle('R=50 nm, N=4, shellthickness=[1,3,1] nm, dR=5, ds=0.2')
p.yaxis(label='S(Q)',scale='l',min=1e-7,max=1e4,ticklabel=['power',0])
p.xaxis(label='Q / nm\S-1',scale='l',min=1e-2,max=5,ticklabel=['power',0])

p.text('Guinier range',x=0.03,y=2000)
p.text(r'Correlation peaks\n at 2\xp\f{}N/R',x=0.3,y=10)
p.text('Shell form factor',x=0.1,y=1e-4)
p[0].line(0.2,4e-4,0.6,4e-4,2,arrow=2)
p.text('Shell form factor ds=0',x=0.1,y=1e-4)
p[0].line(0.2,4e-5,0.6,4e-5,2,arrow=2)


Picture multilamellar1 Picture multilamellar2 Picture multilamellar3 Picture multilamellar5

13.18. A nano cube build of different lattices

Here we build a nano cube with differnt crystal structures.

We can observe the cube formfactor and at larger Q the crystal peaks broadend by the small size. The scattering is explicitly calculated from the grids by ff.cloudScattering. Smoothing with a Gaussian considers experimental broadening (and smooth the result:-).

relError is set to higher value to speed it up. For precise calculation decrease it. For the pictures it was 0.02,this takes about 26 s on 6 core CPU for last bcc example with about 10000 atoms and 900 Q values.

Model with explicit atom positions in small grid

The peaks at large Q are Bragg peaks. Due to the small size extinction rules dont apply complety, which is best visible at fist peaks where we still observe forbiden peaks for bcc and fcc. All Bragg peaks are shown in the second example. The formfactor is reduced due to rms and the number of grid atoms. The analytical solution has infinite high density of atoms and a sharp interface.

import jscatter as js
import numpy as np

q = np.r_[js.loglist(0.01, 3, 200), 3:40:800j]
unitcelllength = 1.5
N = 8
rms=0.05
relError=20   # 0.02 for picture below

# make grids and calc scattering
scgrid = js.lattice.scLattice(unitcelllength, N)
sc = js.ff.cloudScattering(q, scgrid.points, relError=relError, rms=rms)
bccgrid = js.lattice.bccLattice(unitcelllength, N)
bcc = js.ff.cloudScattering(q, bccgrid.points, relError=relError, rms=rms)
fccgrid = js.lattice.fccLattice(unitcelllength, N)
fcc = js.ff.cloudScattering(q, fccgrid.points, relError=relError, rms=rms)
#
# plot  data
p = js.grace(1.5, 1)
# smooth with Gaussian to include instrument resolution
p.plot(sc.X, js.formel.smooth(sc, 10, window='gaussian'), legend='sc')
p.plot(bcc.X, js.formel.smooth(bcc, 10, window='gaussian'), legend='bcc')
p.plot(fcc.X, js.formel.smooth(fcc, 10, window='gaussian'), legend='fcc')
#
# diffusive scattering
# while cloudscattering is normalized to one (without normalizytion ~ N**2),
# diffusive is proportianl to scattering of single atoms (incoherent ~ N)
q = q = js.loglist(1, 35, 100)
p.plot(q, (1 - np.exp(-q * q * rms ** 2)) / scgrid.numberOfAtoms(), li=1, sy=0, le='sc diffusive')
p.plot(q, (1 - np.exp(-q * q * rms ** 2)) / bccgrid.numberOfAtoms(), li=2, sy=0, le='bcc diffusive')
p.plot(q, (1 - np.exp(-q * q * rms ** 2)) / fccgrid.numberOfAtoms(), li=3, sy=0, le='fcc diffusive')
#
# cuboid formfactor for small Q
q = js.loglist(0.01, 2, 300)
cube=js.ff.cuboid(q,unitcelllength*(2*N+1))
p.plot(cube.X,cube.Y/cube.Y[0],sy=0,li=1, le='cube form factor')
#
p.title('Comparison sc, bcc, fcc lattice for a nano cube')
p.yaxis(scale='l', label='I(Q)')
p.xaxis(scale='l', label='Q / A\S-1')
p.legend(x=0.02, y=0.001, charsize=1.5)
p.text('cube formfactor', x=0.02, y=0.05, charsize=1.4)
p.text('Bragg peaks', x=4, y=0.05, charsize=1.4)
p.text('diffusive scattering', x=4, y=1e-6, charsize=1.4)
# p.save('jscatter/examples/LatticeComparison.png')


LatticeComparison

Analytical model asumming crystal lattice with limited size

This shows the Bragg peaks of crystal structures with broadening due to limited size.

The low Q scattering from the chrystal shape is not covered well as only the asymptotic behaviour is governed.


import jscatter as js
import numpy as np
q = np.r_[js.loglist(0.5, 3, 50), 3:80:1200j]
N = 8
a = 1.5 # unitcelllength
domainsize=a* (2*N+1)
rms=0.05
p = js.grace(1.5, 1)
p.title('structure factor for sc, bcc and fcc 3D lattice')
p.subtitle('with diffusive scattering,asymmetry factor beta=1')

sc = js.sf.latticeStructureFactor(q, a=a, rmsd=rms , domainsize=domainsize,type='sc')
p.plot(sc, li=[1, 3, 1], sy=0, le='sc')
p.plot(sc.X,1-sc[-3], li=[3, 2, 1], sy=0)  # diffusive scattering

bcc = js.sf.latticeStructureFactor(q, a=a, rmsd=rms, domainsize=domainsize,type='bcc')
p.plot(bcc, li=[1, 3, 2], sy=0, le='bcc')
p.plot(bcc.X,1-bcc[-3], li=[3, 2, 2], sy=0)

fcc = js.sf.latticeStructureFactor(q, a=a, rmsd=rms, domainsize=domainsize,type='fcc')
p.plot(fcc, li=[1, 3, 3], sy=0, le='fcc')
p.plot(fcc.X,1-fcc[-3], li=[3, 2, 3], sy=0)

p.text(r'broken lines \nshow diffusive scattering',x=10,y=0.1)
p.yaxis(label='S(q)',scale='l',max=50,min=0.05)
p.xaxis(label='q / nm',scale='l',max=50,min=0.5)
p.legend(x=1,y=30,charsize=1.5)
# p.save('jscatter/examples/LatticeComparison2.png')


LatticeComparison2

A direct comparison between both models for bcc cube

Differences are due to incomplete extinction of peaks and due to explicit dependence on the edges (incomplete elementary cells)

relError=0.02 samples until the error is smaller 0.02 for a q point with pseudorandom numbers. The same can be done with relError=400 on a fixed Fibonacci lattice. Both need longer for the computation.

import jscatter as js
import numpy as np
q = np.r_[js.loglist(0.1, 3, 100), 3:40:800j]
unitcelllength = 1.5
N = 8
rms=0.05
relError=20         #  0.02 for the picture below
domainsize=unitcelllength*(2*N+1)

bccgrid = js.lattice.bccLattice(unitcelllength, N)
bcc = js.ff.cloudScattering(q, bccgrid.points, relError=relError, rms=rms)
p = js.grace(1.5, 1)
p.plot(bcc.X, js.formel.smooth(bcc, 10, window='gaussian')*bccgrid.numberOfAtoms(), legend='bcc explicit')

q = np.r_[js.loglist(0.1, 3, 200), 3:40:1600j]
sc = js.sf.latticeStructureFactor(q, a=unitcelllength, rmsd=rms , domainsize=domainsize,type='bcc')
p.plot(sc, li=[1, 3, 4], sy=0, le='bcc analytic')
p.yaxis(scale='l', label='I(Q)')
p.xaxis(scale='l', label='Q / A\S-1')
p.legend(x=0.5, y=1000, charsize=1.5)
p.title('Comparison explicit and implicit model for a crystal cube')
p.text('cube formfactor', x=0.11, y=1, charsize=1.4)
p.text('bcc Bragg peaks', x=4, y=100, charsize=1.4)
p.text('diffusive scattering', x=10, y=0.1, charsize=1.4)
# p.save('jscatter/examples/LatticeComparison3.png')

LatticeComparison3
jscatter.examples.runAll()[source]

Run all examples.

jscatter.examples.runExample(example)[source]

Runs example

Parameters:

example: string,int

Filename or number of the example to run

jscatter.examples.showExample(example='.')[source]

Opens example in default editor.

Parameters:

example : string, int

Filename or number. If ‘.’ the folder with the examples is opened.

jscatter.examples.showExampleList()[source]

Show a list of all examples.