# Fityk 1.1.2
## This script shows the Pawley method for powder pattern which can be called
## a multi-peak fitting, with equality constraints.

# Load data file from directory of this script (SiC+Zn data)
@0 < '_SCRIPT_DIR_/SiC_Zn.dat'

# the background was obtained by clicking on the plot in the "background mode"
%bg0 = Spline(20.4823,43.3474,  28.004,24.3128,  31.4412,23.6984,  33.7911,36.0138,  38.6477,30.5403,  49.9335,17.263,  65.1888,17.968,  79.5965,22.1979, 98.9835,23.6079)
Y = y - %bg0(x)

A = 31 < x and x < 90 # only data with 2theta in (31, 90) are active

# The wavelength is known and we do not want to fit it.
# If you would like to fit wavelength, put '~' before the number.
$lambda = 1.54051 # Cu Ka1

# initial lattice parameters
$sic_a = ~4.35                #cubic SiC
$zn_a = ~2.67; $zn_c = ~4.924 #hexagonal Zn

# the peak positions are given by the following formulae:
#1/d = sqrt(h^2 + k^2 + l^2) / a # for cubic phase
#1/d = sqrt(4./3. * (h^2 + k^2 + h*k) / a^2 + l^2 / c^2) # for hexagonal phase
#peak_center = 2 * asin((1/d) / 2 * lambda) [in RAD, must be converted to DEG]


## All initial peak width and heights are the same (0.2 and 200, respectively).

# peak hkl=(220) of SiC
$c_sic220 = 360/pi * asin(sqrt(2^2+2^2+0) / $sic_a / 2 * $lambda)
%sic220 = PseudoVoigt(center=$c_sic220, hwhm=~0.2, height=~200)

# Zn (002)
$c_zn002 = 360/pi * asin(sqrt(4/3 * (0+0+0*0) / $zn_a^2 + 2^2/$zn_c^2 ) / 2 * $lambda)
%zn002 = PseudoVoigt(center=$c_zn002, hwhm=~0.2, height=~200)

# SiC (111)
$c_sic111 = 360/pi * asin(sqrt(1^2+1^2+1^2) / $sic_a / 2 * $lambda)
%sic111 = PseudoVoigt(center=$c_sic111, hwhm=~0.2, height=~200)

# Zn (100)
$c_zn100 = 360/pi * asin(sqrt(4/3 * (1^2+0+1*0) / $zn_a^2 + 0/$zn_c^2 ) / 2 * $lambda)
%zn100 = PseudoVoigt(center=$c_zn100, hwhm=~0.2, height=~200)

# Zn (101)
$c_zn101 = 360/pi * asin(sqrt(4/3 * (1^2+0+1*0) / $zn_a^2 + 1^2/$zn_c^2 ) / 2 * $lambda)
%zn101 = PseudoVoigt(center=$c_zn101, hwhm=~0.2, height=~200)

# Zn (102)
$c_zn102 = 360/pi * asin(sqrt(4/3 * (1^2+0+1*0) / $zn_a^2 + 2^2/$zn_c^2 ) / 2 * $lambda)
%zn102 = PseudoVoigt(center=$c_zn102, hwhm=~0.2, height=~200)

F = %sic220 + %zn002 + %sic111 + %zn100 + %zn101 + %zn102

# after adding a few peaks it can be a good idea to fit it,
# to get a better approximation of the lattice parameters
fit 10

# Zn (103)
$c_zn103 = 360/pi * asin(sqrt(4/3 * (1^2+0+1*0) / $zn_a^2 + 3^2/$zn_c^2 ) / 2 * $lambda)
%zn103 = PseudoVoigt(center=$c_zn103, hwhm=~0.2, height=~200)

# Zn (110)
$c_zn110 = 360/pi * asin(sqrt(4/3 * (1^2+1^2+1*1) / $zn_a^2 + 0^2/$zn_c^2 ) / 2 * $lambda)
%zn110 = PseudoVoigt(center=$c_zn110, hwhm=~0.2, height=~200)

# Zn (112)
$c_zn112 = 360/pi * asin(sqrt(4/3 * (1^2+1^2+1*1) / $zn_a^2 + 2^2/$zn_c^2 ) / 2 * $lambda)
%zn112 = PseudoVoigt(center=$c_zn112, hwhm=~0.2, height=~200)

# Zn (200)
$c_zn200 = 360/pi * asin(sqrt(4/3 * (2^2+0^2+2*0) / $zn_a^2 + 0^2/$zn_c^2 ) / 2 * $lambda)
%zn200 = PseudoVoigt(center=$c_zn200, hwhm=~0.2, height=~200)
# constraints: this small peak will have the same width and shape as %zn112
%zn200.shape = %zn112.shape
%zn200.hwhm = %zn112.hwhm

# Zn (201)
$c_zn201 = 360/pi * asin(sqrt(4/3 * (2^2+0^2+2*0) / $zn_a^2 + 1^2/$zn_c^2 ) / 2 * $lambda)
%zn201 = PseudoVoigt(center=$c_zn201, hwhm=~0.2, height=~200)

# SiC (311)
$c_sic311 = 360/pi * asin(sqrt(3^2+1^2+1^2) / $sic_a / 2 * $lambda)
%sic311 = PseudoVoigt(center=$c_sic311, hwhm=~0.2, height=~200)

# add these functions to the model
F += %zn103 + %zn110 + %zn112 + %zn200 + %zn201 + %sic311

# add zero-shift
Z = Constant(~0.)

# fit   # least-squares fitting

