Skip to content

Code examples

Tom Schoonjans edited this page Oct 13, 2014 · 11 revisions

In this section we will demonstrate how to use xraylib in some real-world situations. You should be able to compile (if necessary) and run these examples after installing xraylib with the required bindings.

Fundamental parameter method (Fortran 2003)

This first example, written in Fortran 2003, demonstrates how one can determine the expected first order net-line intensity of a particular XRF line after irradiating a sample (apatite) with an X-ray beam. Compilation instructions can be found in the Fortran bindings section.

The equation that has been used here can be found in every handbook on quantitative X-ray fluorescence and many scientific articles. Try for example Spectrochim. Acta Part B, 67:32–42, 2012.

PROGRAM fpm

USE :: xraylib
USE, INTRINSIC :: ISO_C_BINDING

REAL (C_DOUBLE) :: flux = 1E9 !photons/s
REAL (C_DOUBLE) :: G = 1E-5
REAL (C_DOUBLE) :: density = 3.19 !g/cm3
REAL (C_DOUBLE) :: thickness = 0.1 !cm
REAL (C_DOUBLE) :: xrf_intensity, chi
REAL (C_DOUBLE) :: mu_0, mu_1, w_Ca, A_corr, Q
REAL (C_DOUBLE) :: alpha = 45.0, beta = 45.0 !degrees
REAL (C_DOUBLE) :: beam_energy = 20.0 !keV
TYPE (compoundData), POINTER :: cd
CHARACTER (len=50) :: apatite = 'Ca5(PO4)3(OH)0.33F0.33Cl0.33'
REAL (C_DOUBLE), PARAMETER :: deg2rad = 3.14159265359/180.0

cd => compoundParser(apatite)
w_Ca = cd%massFractions(6)

mu_0 = CS_Total_CP(apatite, beam_energy)
mu_1 = CS_Total_CP(apatite, LineEnergy(20, KL3_LINE))
chi = mu_0/SIN(deg2rad*alpha) + mu1/SIN(deg2rad*beta)
A_corr = (1.0-EXP(-chi*density*thickness))/chi*density*thickness
Q = CS_FluorLine_Kissel(20, KL3_LINE, beam_energy)

xrf_intensity = flux*G*Q*w_Ca*density*thickness*A_corr

CALL FreeCompoundData(cd)

WRITE (*, '(A, ES12.4)') 'xrf_intensity: ', xrf_intensity
END PROGRAM fpm

Transmission efficiency using Monte-Carlo method (Ruby)

The Monte-Carlo method is often used in the field of X-rays as it can be used to predict the outcome of experiments, provided that all the relevant physical datasets are present. xraylib can be used to this effect as is shown in the following example, written in Ruby, in which one determines the fraction of photons than manages to penetrate through a sample of a particular thickness along the beampath.

require 'xraylib'

compound = "Uranium Monocarbide"

cdn = Xraylib.GetCompoundDataNISTByName(compound)
density = cdn['density'] #g/cm3
thickness = 0.01 #cm
energy = 50.0 #keV

mu_rho = Xraylib.CS_Total_CP(compound, energy)*density

transmitted = 0
total = 100000

total.times {|i|
x = -Math.log(rand())/mu_rho
transmitted += 1 if x > thickness
}

printf("transmitted: %i\n", transmitted)
printf("MC fraction: %f\n", Float(transmitted)/total)
printf("True fraction: %f\n", Math.exp(-mu_rho*thickness))

More examples

The xraylib example folder contains example files for all the languages that are officially supported. Click on the following to links to access them directly.

Clone this wiki locally