<jupyter_start><jupyter_text>Creating sampleThis notebook shows how we can create a sample from atoms and calculate diffraction profiles using both constant wavelength and time-of-flight experiment types.<jupyter_code>from easyCore import np
from easyDiffractionLib.sample import Sample
from easyDiffractionLib import Site, Phase, Phases
from easyDiffractionLib.interface import InterfaceFactory as Calculator

from easyDiffractionLib.Elements.Experiments.Pattern import Pattern1D
from easyDiffractionLib.Profiles.P1D import Instrument1DCWParameters as CWParams
from easyDiffractionLib.Profiles.P1D import Instrument1DTOFParameters as TOFParams
from easyDiffractionLib.Profiles.P1D import Powder1DParameters

from easyCore.Fitting.Fitting import Fitter

import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>**Preparing the sample and data**<jupyter_code>calculator = Calculator()
calculator.switch('CrysPy') # this is the default<jupyter_output><empty_output><jupyter_text>Create an atom using `Site` interface<jupyter_code>atom = Site.from_pars(label="Cl1",
                      specie='Cl',
                      fract_x=0.0,
                      fract_y=0.0,
                      fract_z=0.0)<jupyter_output><empty_output><jupyter_text>Create a phase<jupyter_code>phase = Phase(name="salt")

# set the space group
phase.spacegroup.space_group_HM_name = "F m -3 m"

# add the atom
phase.add_atom(atom)<jupyter_output><empty_output><jupyter_text>Add another atom (using `Phase` interface)<jupyter_code>phase.add_atom('Na1', 'Na', 0.5, 0.5, 0.5)<jupyter_output><empty_output><jupyter_text>Visualise the structure<jupyter_code>import py3Dmol
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.setStyle({'sym':2},{'sphere':{'scale':.5,'color':'blue'},'stick':{'color':'cyan'}})
viewer.zoomTo()<jupyter_output><empty_output><jupyter_text>Create Phases object<jupyter_code>phases = Phases()
phases.append(phase)<jupyter_output><empty_output><jupyter_text>Create and modify a Parameters object<jupyter_code>parameters = CWParams.default()

parameters.length_a = 5.6402
parameters.resolution_u = 0.1447
parameters.resolution_v = -0.4252
parameters.resolution_w = 0.3864
parameters.resolution_x = 0.0
parameters.resolution_y = 0.0<jupyter_output><empty_output><jupyter_text>Create and modify a Pattern object<jupyter_code>pattern = Powder1DParameters.default()
pattern.zero_shift = 0.0
pattern.scale = 100.0<jupyter_output><empty_output><jupyter_text>Finally, create the Sample<jupyter_code>S = Sample(phases=phases, parameters=parameters, calculator=calculator)<jupyter_output><empty_output><jupyter_text>Calculate the profile - using the calculator we defined previously.Note that we don't run anything on the `Sample` object but on a separate `Calculator` instance<jupyter_code>x_data = np.linspace(1, 120, 500)
y_data = calculator.fit_func(x_data)

%matplotlib notebook
plt.plot(x_data, y_data, label='CW Simulation')
plt.legend()<jupyter_output><empty_output><jupyter_text>Switch to TOF Retrieve the phases and pattern<jupyter_code>phases = S.phases[0]
pattern = S.pattern<jupyter_output><empty_output><jupyter_text>Modify the parameters to correspond to a TOF experiment<jupyter_code>parameters = TOFParams.default()
parameters.length_a = 5.6402
parameters.dtt1 = 6167.24700
parameters.dtt2 = -2.28000
parameters.ttheta_bank = 145.00
pattern.zero_shift = 0.0
pattern.scale = 100.0

tof_str = 'Npowder1DTOF'
interfaces = calculator.interface_compatability(tof_str)
print(interfaces[0])
calculator.switch(interfaces[0])

S = Sample(phases=phases, parameters=parameters, calculator=calculator)<jupyter_output><empty_output><jupyter_text>Calculate the profile again, this time based on the TOF parameters<jupyter_code>x_data = np.linspace(5000, 60000, 500)
y_data = calculator.fit_func(x_data)

%matplotlib notebook

plt.plot(x_data, y_data, label='TOF Simulation')
plt.legend()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11177454024361118, 'fraction_numerical': 0.02842130403630284, 'mean_word_length': 7.546938775510204, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '93420', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1527, 'n_tokens_neox': 1408, 'n_words': 265, 'path': 'tests/CreateSample.ipynb', 'repo_name': 'arm61/easyDiffractionLib', 'size': '4187'}