---
jupytext:
  formats: md:myst
  text_representation:
    extension: .md
    format_name: myst
    format_version: 0.13
    jupytext_version: 1.11.5
kernelspec:
  display_name: Python 3
  language: python
  name: python3
---

# The tutorial 5th 

Shows how to calculate atomic distances based on the radial distribution function derived from diffraction patterns.


## coding

> **1. Save your diffraction data to the root directory and rename the file to `intensity.csv`.**





```{code-cell}
# import PyXplore package
from PyXplore import WPEM
import pandas as pd
```

> **2. Parse your diffraction data (`2θ`, intensity) and perform background processing.**

```{code-cell}
intensity_csv = pd.read_csv(r'intensity.csv',header=None )
var = WPEM.BackgroundFit(intensity_csv,lowAngleRange=3.8,poly_n=12,bac_split=8,bac_num=100)
```
> **3. After running the code, a new folder named `ConvertedDocuments` will be created in the root directory. This folder contains the background information.**

> **Copy the two important files — `bac.csv` and `no_bac_intensity.csv` — from `ConvertedDocuments` into the root directory, as they are required for the next steps.**


> **Parse the `.cif` file as demonstrated in the crystal fitting section, and generate the `peak0.csv` file.**






```{code-cell}
# The wavelength is set according to the actual light source
wavelength = [1.03]
# The file name of non-background data (2theta-intensity data)
no_bac_intensity_file = "no_bac_intensity.csv" 
# The file name of raw/original data (2theta-intensity data)
original_file = "intensity.csv"  
# The file name of background data (2theta-intensity data)
bacground_file = "bac.csv"  


# Input the initial lattice constants {a, b, c, α, β, γ}, whose values need to be assumed at initialization.
Lattice_constants = [[17.53,17.53,6.47,90,90,120],]

# Execute the model

WPEM.XRDfit(
    wavelength, var, Lattice_constants,no_bac_intensity_file, original_file, bacground_file, 
    subset_number=3,low_bound=6,up_bound=16,bta = 0.78,iter_max = 50, asy_C = 0,InitializationEpoch=0, 
    )
```





> After processing the crystalline signals, the remaining signals corresponding to the amorphous phase are saved.

> Fit the amorphous signal using the following code:


```{code-cell}
WPEM.Amorphous_fit(mix_component=3, sigma2_coef = 0.5, max_iter = 5000,peak_location = None,Wavelength=1.03
                )
```



> After coverage, the amorphous components (referred to as "holes") are derived. You can visualize each amorphous hole using the provided plotting functions. The results are saved in the `DecomposedComponents` folder.

```{code-cell}
WPEM.Plot_Components(lowboundary = 4, upboundary = 19, wavelength = wavelength, Macromolecule = True,phase = 1)
```

> The relative bulk crystallinity can be estimated from the diffraction intensity ratio
> 
> after subtracting the amorphous signal, which is saved in the 'DecomposedComponents' folder as 'Amorphous.csv'.
> 
> Additionally, the radial distribution function (RDF) can be applied to the remaining signal to calculate the nearest-neighbor atomic distance based on features within the diffraction pattern.


```{code-cell}
WPEM.AmorphousRDFun( r_max = 4,density_zero=None,Nf2=1,highlight= 6,)
```


```{seealso}
The peak positions at 0.42, 0.93, 1.36, 1.80, 2.23, and 2.68 correspond to a series of atomic clusters.
```