Code examples - tschoonj/xraylib Wiki

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.


USE :: xraylib


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 (xrl_error), POINTER :: error => NULL()
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, error)
	! If the compound is invalid, an error message will be printed
	WRITE (error_unit, '(A,A)') 'Error message: ', TRIM(error%message)

w_Ca = cd%massFractions(6) ! fortran array indexing starts at 1!!!!

mu_0 = CS_Total_CP(apatite, beam_energy)
mu_1 = CS_Total_CP(apatite, LineEnergy(20, KL3_LINE))
chi = mu_0/SIN(deg2rad*alpha) + mu_1/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


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

Save as fpm.f90 and compile with:

gfortran -o fpm `pkg-config --cflags libxrlf03` fpm.f90 `pkg-config --libs libxrlf03`

Executing fpm should produce the following output:

xrf_intensity:   1.0849E+01

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))

Save as mc.rb and execute with:

ruby mc.rb

This should produce something similar (remember: it's a Monte Carlo simulation!) to the following output:

transmitted: 23438
MC fraction: 0.234380
True fraction: 0.233201

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.