---
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 2nd 

Introduces the process for refining multi-phase systems.

## 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=22,poly_n=8,bac_split=8,bac_num=600)
```
> **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.**



> **4. After background subtraction, the next step is to parse the reference structure.**
>
> Save the reference `.cif` files in the root directory. For example, if your structures are Mn₂O₃ and RuO₂, place files named `Mn2O3.cif` and `RuO2.cif` in the root directory as the reference phases.
>
> If you are unsure of the reference phases, you must first perform phase identification. For assistance, please visit our website: [https://xqueryer.caobin.asia/](https://xqueryer.caobin.asia/)


```{code-cell}
MnO_latt, AtomCoordinates,des = WPEM.CIFpreprocess(filepath='Mn2O3.cif',two_theta_range=(15,75))
```

```{code-cell}
RuO_latt, AtomCoordinates,des = WPEM.CIFpreprocess(filepath='RuO2.cif',two_theta_range=(15,75))
```


> **5. After running the code, a new folder named `output_xrd` will be generated.**
>
> Inside this folder, find the file named `xxxHKL.csv`. Copy it to the root directory and rename it sequentially as `peak0.csv`, `peak1.csv`, and so on. These files will be used in the refinement step.


```{code-cell}
# The wavelength is set according to the actual light source
wavelength = [1.540593, 1.544414]
# 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 = [MnO_latt,RuO_latt]

# Execute the model

WPEM.XRDfit(
    wavelength, var, Lattice_constants,no_bac_intensity_file, original_file, bacground_file, 
    subset_number=11,low_bound=20,up_bound=70,bta = 0.85,iter_max = 5, asy_C = 0,InitializationEpoch=0, 
    )
```



```{seealso}
For demonstration purposes, the code uses `iter_max = 5` to reduce computational cost. However, for practical applications, it is recommended to set `iter_max` to at least 50 for more reliable results.
```