-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxrd.jl
36 lines (25 loc) · 974 Bytes
/
xrd.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Bond length statistics from X-ray diffraction data
# S. Hessam M. Mehr, 2017, MIT license
# Terrible terrible code that happens to work
module XRD
using Cubature:hcubature
gaussian(mu,sigma) = x-> exp(-(x-mu)^2/(2sigma^2))/sqrt(2pi*sigma^2)
normalize_p(f,min,max) = begin a = hquadrature(f,min,max)[1]; x->f(x)/a end
mean_p(f,min,max) = begin ff = normalize_p(f,min,max); hquadrature(x->x*ff(x),min,max)[1] end
std_p(f,min,max) = begin ff=normalize_p(f,min,max); μ = mean_p(ff,min,max); sqrt(hquadrature(x->(x-μ)^2*ff(x),min,max)[1]) end
function parse_bond(line)
v1,v2 = split(strip(line),"(")
v3=length(split(v1,".")[2])
(float(v1),float(v2[1:end-1])*10.0^-v3)
end
function distribution(line)
b=parse_bond(line)
gaussian(b[1],b[2])
end
function stats(lines, min, max; delim=",")
l = split(lines,delim)
ds = map(distribution,l)
overall = x->sum(d(x) for d in ds)
(mean_p(overall,min,max),std_p(overall,min,max))
end
end