Switch calculation engine#

This notebook shows how we can switch calculation engine (external crystallographic library) and calculate diffraction profile using constant wavelength experiment type.

# EasyScience, technique-independent
from easyCore import np
from easyCore.Fitting.Fitting import Fitter

# EasyScience, diffraction
from easyDiffractionLib import Site, Phase, Phases
from easyDiffractionLib.Jobs import Powder1DCW, InterfaceFactory

# Vizualization
import py3Dmol
from bokeh.io import show, output_notebook
from bokeh.plotting import figure

# Misc
import xarray as xr
Warning: GSAS-2 is not installed
output_notebook()
FIGURE_WIDTH = 700
FIGURE_HEIGHT = 300
Loading BokehJS ...

Sample#

Create an atom using Site interface#

atom = Site.from_pars(label="Cl",
                      specie="Cl",
                      fract_x=0.0,
                      fract_y=0.0,
                      fract_z=0.0)

Create a phase, set space group, add atom#

phase = Phase(name="salt")

phase.spacegroup.space_group_HM_name = "F m -3 m"

phase.add_atom(atom)

Add another atom (using Phase interface)#

phase.add_atom('Na', 'Na', 0.5, 0.5, 0.5)

Visualise the structure#

viewer = py3Dmol.view()
viewer.addModel(phase.to_cif_str(),'cif',{'doAssembly':True,'duplicateAssemblyAtoms':True,'normalizeAssembly':True})
viewer.setStyle({'sphere':{'colorscheme':'Jmol','scale':.2},'stick':{'colorscheme':'Jmol', 'radius': 0.1}})
viewer.addUnitCell()
viewer.replicateUnitCell(2,2,2)
viewer.zoomTo()

You appear to be running in JupyterLab (or JavaScript failed to load for some other reason). You need to install the 3dmol extension:
jupyter labextension install jupyterlab_3dmol

<py3Dmol.view at 0x7f6d097bfc10>

Create Phases object#

phases = Phases()
phases.append(phase)

Experiment#

Create Dataset#

data = xr.Dataset()
x_data = np.linspace(20, 170, 500)

Create Job#

job = Powder1DCW('NaCl', data, phases=phases)
print(f"Current calculator engine: {job.interface.current_interface_name}")
Current calculator engine: CrysPy
print(f"Available calculator engines: {job.interface.available_interfaces}")
print(f"Available calculators for CW: {job.interface.interface_compatability('Npowder1DCWunp')}")
Available calculator engines: ['CrysPy', 'CrysFML']
Available calculators for CW: ['CrysPy', 'CrysFML']

Modify instrumental parameters#

job.parameters.resolution_u = 0.1447
job.parameters.resolution_v = -0.4252
job.parameters.resolution_w = 0.3864
job.parameters.resolution_x = 0.0
job.parameters.resolution_y = 0.0

Modify pattern parameters#

job.pattern.zero_shift = 0.0
job.pattern.scale = 100.0

Simulation#

Calculate the profile using the default calculation engine#

_ = job.create_simulation(x_data)
y_data_cryspy = np.array(data['sim_NaCl'])
print("some Ycalc from CrysPy:\n", y_data_cryspy[315:330])
some Ycalc from CrysPy:
 [   0.            0.            0.            0.            0.
    0.          150.30464692 2156.99342898   48.2711642     0.
    0.            0.            0.            0.            0.        ]
fig = figure(width=FIGURE_WIDTH, height=FIGURE_HEIGHT)
fig.line(x_data, y_data_cryspy, legend_label='CW Simulation', color='orangered', line_width=2)
show(fig)

Switch calculator#

# The following code generates AttributeError
#calculator.switch('CrysFML')
#_ = job.create_simulation(x_data)
# Temporary workaround
job.interface = InterfaceFactory(interface_name='CrysFML')
print(f"Current calculator engine: {job.interface.current_interface_name}")
Current calculator engine: CrysFML

Simulation#

Calculate the profile using the calculator engine just selected#

_ = job.create_simulation(x_data)
y_data_crysfml = np.array(data['sim_NaCl'])
 XRAY_2THE       
print("some Ycalc from CrysFML:\n", y_data_crysfml[315:330])
some Ycalc from CrysFML:
 [1.64068642e-05 2.23898096e-05 3.23457461e-05 5.07378245e-05
 9.05669222e-05 4.02579606e-02 3.68372955e+02 5.28348145e+03
 1.18166306e+02 4.29416215e-03 8.11971477e-05 4.69949628e-05
 3.06811417e-05 2.16827084e-05 1.62181295e-05]
fig = figure(width=FIGURE_WIDTH, height=FIGURE_HEIGHT)
fig.line(x_data, y_data_crysfml, legend_label='CW Simulation', color='olivedrab', line_width=2)
show(fig)