Skip to content

Commit

Permalink
Refs #16. refactor: move violini model to its own module
Browse files Browse the repository at this point in the history
  • Loading branch information
yxqd committed Apr 16, 2019
1 parent 3ba2ee6 commit dc4e703
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 45 deletions.
50 changes: 5 additions & 45 deletions dgsres/singlextal/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
from . import use_covmat
from .pointcloud import PointCloud
from .simdata import McvineResolutionData
from .violini import VioliniModel
# backward compatible
AnalyticalModel = VioliniModel

# obsolete
def createARCSAnalyticalModel(
tau_P, tau_M, pix_r, pix_h,
sample_thickness,
Expand All @@ -26,56 +30,12 @@ def createARCSAnalyticalModel(
height = "meter*%s" % pix_h,
pressure = "10*atm",
)
return AnalyticalModel(
return VioliniModel(
instrument, pixel, tofwidths, beamdivs,
sample_yml, samplethickness,
Ei, psi_scan)


class AnalyticalModel:

"""analytical resolution model based on paper by Violini et al.
The main calculation is done in module .use_covmat.
"""

def __init__(
self, instrument, pixel, tofwidths, beamdivs,
sample_yml, samplethickness,
Ei, psi_scan):
self.instrument = instrument
self.pixel = pixel
self.tofwidths = tofwidths
self.beamdivs = beamdivs
self.sample_yml = sample_yml
self.samplethickness = samplethickness
self.Ei = Ei
self.psi_scan = psi_scan
return

def computePointCloud(self, hkl, E, N=int(1e6)):
covmat = self.computeCovMat(hkl, E)
events = np.random.multivariate_normal(np.zeros(4), covmat, size=N)
dhs, dks, dls, dEs = events.T
ws = np.ones(dhs.shape)
return PointCloud(dhs, dks, dls, dEs, ws)

def computeCovMat(self, hkl, E):
class dynamics:
hkl_dir = np.array([1.,0.,0.])
dq = 0
dynamics.hkl0 = hkl
dynamics.E = E
cm_res = use_covmat.compute(
self.sample_yml, self.Ei,
dynamics,
self.psi_scan,
self.instrument, self.pixel,
self.tofwidths, self.beamdivs, self.samplethickness,
plot=False)
# ellipsoid_trace = cm_res['u']
InvCov4D = cm_res['hklE_inv_cov']
return np.linalg.inv(InvCov4D)/2.355

def plotEllipsoid(covmat, q, symbol='.'):
"""plot 2d ellipsoid along a particular hkl direction, given the cov matrix.
"""
Expand Down
48 changes: 48 additions & 0 deletions dgsres/singlextal/violini.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import os, numpy as np
from . import use_covmat
from .pointcloud import PointCloud

class VioliniModel:

"""analytical resolution model based on paper by Violini et al.
The main calculation is done in module .use_covmat.
"""

def __init__(
self, instrument, pixel, tofwidths, beamdivs,
sample_yml, samplethickness,
Ei, psi_scan):
self.instrument = instrument
self.pixel = pixel
self.tofwidths = tofwidths
self.beamdivs = beamdivs
self.sample_yml = sample_yml
self.samplethickness = samplethickness
self.Ei = Ei
self.psi_scan = psi_scan
return

def computePointCloud(self, hkl, E, N=int(1e6)):
covmat = self.computeCovMat(hkl, E)
events = np.random.multivariate_normal(np.zeros(4), covmat, size=N)
dhs, dks, dls, dEs = events.T
ws = np.ones(dhs.shape)
return PointCloud(dhs, dks, dls, dEs, ws)

def computeCovMat(self, hkl, E):
class dynamics:
hkl_dir = np.array([1.,0.,0.])
dq = 0
dynamics.hkl0 = hkl
dynamics.E = E
cm_res = use_covmat.compute(
self.sample_yml, self.Ei,
dynamics,
self.psi_scan,
self.instrument, self.pixel,
self.tofwidths, self.beamdivs, self.samplethickness,
plot=False)
# ellipsoid_trace = cm_res['u']
InvCov4D = cm_res['hklE_inv_cov']
return np.linalg.inv(InvCov4D)/2.355

0 comments on commit dc4e703

Please sign in to comment.